2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * The simplest mpeg encoder (well, it was the simplest!).
30 #include "libavutil/internal.h"
31 #include "libavutil/intmath.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/opt.h"
39 #include "mpegvideo.h"
47 #include "aandcttab.h"
49 #include "mpeg4video.h"
51 #include "bytestream.h"
55 static int encode_picture(MpegEncContext *s, int picture_number);
56 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
57 static int sse_mb(MpegEncContext *s);
58 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
59 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
61 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
62 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
64 const AVOption ff_mpv_generic_options[] = {
69 void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
70 uint16_t (*qmat16)[2][64],
71 const uint16_t *quant_matrix,
72 int bias, int qmin, int qmax, int intra)
77 for (qscale = qmin; qscale <= qmax; qscale++) {
79 if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
80 dsp->fdct == ff_jpeg_fdct_islow_10 ||
81 dsp->fdct == ff_faandct) {
82 for (i = 0; i < 64; i++) {
83 const int j = dsp->idct_permutation[i];
84 /* 16 <= qscale * quant_matrix[i] <= 7905
85 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
86 * 19952 <= x <= 249205026
87 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
88 * 3444240 >= (1 << 36) / (x) >= 275 */
90 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
91 (qscale * quant_matrix[j]));
93 } else if (dsp->fdct == ff_fdct_ifast) {
94 for (i = 0; i < 64; i++) {
95 const int j = dsp->idct_permutation[i];
96 /* 16 <= qscale * quant_matrix[i] <= 7905
97 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
98 * 19952 <= x <= 249205026
99 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
100 * 3444240 >= (1 << 36) / (x) >= 275 */
102 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
103 (ff_aanscales[i] * (int64_t)qscale * quant_matrix[j]));
106 for (i = 0; i < 64; i++) {
107 const int j = dsp->idct_permutation[i];
108 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
109 * Assume x = qscale * quant_matrix[i]
111 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
112 * so 32768 >= (1 << 19) / (x) >= 67 */
113 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
114 (qscale * quant_matrix[j]));
115 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
116 // (qscale * quant_matrix[i]);
117 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
118 (qscale * quant_matrix[j]);
120 if (qmat16[qscale][0][i] == 0 ||
121 qmat16[qscale][0][i] == 128 * 256)
122 qmat16[qscale][0][i] = 128 * 256 - 1;
123 qmat16[qscale][1][i] =
124 ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
125 qmat16[qscale][0][i]);
129 for (i = intra; i < 64; i++) {
131 if (dsp->fdct == ff_fdct_ifast) {
132 max = (8191LL * ff_aanscales[i]) >> 14;
134 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
140 av_log(NULL, AV_LOG_INFO,
141 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
146 static inline void update_qscale(MpegEncContext *s)
148 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
149 (FF_LAMBDA_SHIFT + 7);
150 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
152 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
156 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
162 for (i = 0; i < 64; i++) {
163 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
170 * init s->current_picture.qscale_table from s->lambda_table
172 void ff_init_qscale_tab(MpegEncContext *s)
174 int8_t * const qscale_table = s->current_picture.qscale_table;
177 for (i = 0; i < s->mb_num; i++) {
178 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
179 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
180 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
185 static void update_duplicate_context_after_me(MpegEncContext *dst,
188 #define COPY(a) dst->a= src->a
190 COPY(current_picture);
196 COPY(picture_in_gop_number);
197 COPY(gop_picture_number);
198 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
199 COPY(progressive_frame); // FIXME don't set in encode_header
200 COPY(partitioned_frame); // FIXME don't set in encode_header
205 * Set the given MpegEncContext to defaults for encoding.
206 * the changed fields will not depend upon the prior state of the MpegEncContext.
208 static void MPV_encode_defaults(MpegEncContext *s)
211 ff_MPV_common_defaults(s);
213 for (i = -16; i < 16; i++) {
214 default_fcode_tab[i + MAX_MV] = 1;
216 s->me.mv_penalty = default_mv_penalty;
217 s->fcode_tab = default_fcode_tab;
220 av_cold int ff_dct_encode_init(MpegEncContext *s) {
222 ff_dct_encode_init_x86(s);
224 ff_h263dsp_init(&s->h263dsp);
225 if (!s->dct_quantize)
226 s->dct_quantize = ff_dct_quantize_c;
228 s->denoise_dct = denoise_dct_c;
229 s->fast_dct_quantize = s->dct_quantize;
230 if (s->avctx->trellis)
231 s->dct_quantize = dct_quantize_trellis_c;
236 /* init video encoder */
237 av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
239 MpegEncContext *s = avctx->priv_data;
241 int chroma_h_shift, chroma_v_shift;
243 MPV_encode_defaults(s);
245 switch (avctx->codec_id) {
246 case AV_CODEC_ID_MPEG2VIDEO:
247 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
248 avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
249 av_log(avctx, AV_LOG_ERROR,
250 "only YUV420 and YUV422 are supported\n");
254 case AV_CODEC_ID_LJPEG:
255 if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
256 avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
257 avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
258 avctx->pix_fmt != AV_PIX_FMT_BGR0 &&
259 avctx->pix_fmt != AV_PIX_FMT_BGRA &&
260 avctx->pix_fmt != AV_PIX_FMT_BGR24 &&
261 ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
262 avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
263 avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
264 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
265 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
269 case AV_CODEC_ID_MJPEG:
270 case AV_CODEC_ID_AMV:
271 if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
272 avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
273 avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
274 ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
275 avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
276 avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
277 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
278 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
283 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
284 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
289 switch (avctx->pix_fmt) {
290 case AV_PIX_FMT_YUVJ444P:
291 case AV_PIX_FMT_YUV444P:
292 s->chroma_format = CHROMA_444;
294 case AV_PIX_FMT_YUVJ422P:
295 case AV_PIX_FMT_YUV422P:
296 s->chroma_format = CHROMA_422;
298 case AV_PIX_FMT_YUVJ420P:
299 case AV_PIX_FMT_YUV420P:
301 s->chroma_format = CHROMA_420;
305 s->bit_rate = avctx->bit_rate;
306 s->width = avctx->width;
307 s->height = avctx->height;
308 if (avctx->gop_size > 600 &&
309 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
310 av_log(avctx, AV_LOG_WARNING,
311 "keyframe interval too large!, reducing it from %d to %d\n",
312 avctx->gop_size, 600);
313 avctx->gop_size = 600;
315 s->gop_size = avctx->gop_size;
317 s->flags = avctx->flags;
318 s->flags2 = avctx->flags2;
319 s->max_b_frames = avctx->max_b_frames;
320 s->codec_id = avctx->codec->id;
321 s->strict_std_compliance = avctx->strict_std_compliance;
322 s->quarter_sample = (avctx->flags & CODEC_FLAG_QPEL) != 0;
323 s->mpeg_quant = avctx->mpeg_quant;
324 s->rtp_mode = !!avctx->rtp_payload_size;
325 s->intra_dc_precision = avctx->intra_dc_precision;
326 s->user_specified_pts = AV_NOPTS_VALUE;
328 if (s->gop_size <= 1) {
335 s->me_method = avctx->me_method;
338 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
340 s->adaptive_quant = (s->avctx->lumi_masking ||
341 s->avctx->dark_masking ||
342 s->avctx->temporal_cplx_masking ||
343 s->avctx->spatial_cplx_masking ||
344 s->avctx->p_masking ||
345 s->avctx->border_masking ||
346 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
349 s->loop_filter = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
351 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
352 switch(avctx->codec_id) {
353 case AV_CODEC_ID_MPEG1VIDEO:
354 case AV_CODEC_ID_MPEG2VIDEO:
355 avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112L / 15000000 * 16384;
357 case AV_CODEC_ID_MPEG4:
358 case AV_CODEC_ID_MSMPEG4V1:
359 case AV_CODEC_ID_MSMPEG4V2:
360 case AV_CODEC_ID_MSMPEG4V3:
361 if (avctx->rc_max_rate >= 15000000) {
362 avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000L) * (760-320) / (38400000 - 15000000);
363 } else if(avctx->rc_max_rate >= 2000000) {
364 avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000L) * (320- 80) / (15000000 - 2000000);
365 } else if(avctx->rc_max_rate >= 384000) {
366 avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000L) * ( 80- 40) / ( 2000000 - 384000);
368 avctx->rc_buffer_size = 40;
369 avctx->rc_buffer_size *= 16384;
372 if (avctx->rc_buffer_size) {
373 av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
377 if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
378 av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
379 if (avctx->rc_max_rate && !avctx->rc_buffer_size)
383 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
384 av_log(avctx, AV_LOG_INFO,
385 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
388 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
389 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
393 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
394 av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
398 if (avctx->rc_max_rate &&
399 avctx->rc_max_rate == avctx->bit_rate &&
400 avctx->rc_max_rate != avctx->rc_min_rate) {
401 av_log(avctx, AV_LOG_INFO,
402 "impossible bitrate constraints, this will fail\n");
405 if (avctx->rc_buffer_size &&
406 avctx->bit_rate * (int64_t)avctx->time_base.num >
407 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
408 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
412 if (!s->fixed_qscale &&
413 avctx->bit_rate * av_q2d(avctx->time_base) >
414 avctx->bit_rate_tolerance) {
415 av_log(avctx, AV_LOG_ERROR,
416 "bitrate tolerance too small for bitrate\n");
420 if (s->avctx->rc_max_rate &&
421 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
422 (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
423 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
424 90000LL * (avctx->rc_buffer_size - 1) >
425 s->avctx->rc_max_rate * 0xFFFFLL) {
426 av_log(avctx, AV_LOG_INFO,
427 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
428 "specified vbv buffer is too large for the given bitrate!\n");
431 if ((s->flags & CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
432 s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
433 s->codec_id != AV_CODEC_ID_FLV1) {
434 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
438 if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
439 av_log(avctx, AV_LOG_ERROR,
440 "OBMC is only supported with simple mb decision\n");
444 if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
445 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
449 if (s->max_b_frames &&
450 s->codec_id != AV_CODEC_ID_MPEG4 &&
451 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
452 s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
453 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
456 if (s->max_b_frames < 0) {
457 av_log(avctx, AV_LOG_ERROR,
458 "max b frames must be 0 or positive for mpegvideo based encoders\n");
462 if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
463 s->codec_id == AV_CODEC_ID_H263 ||
464 s->codec_id == AV_CODEC_ID_H263P) &&
465 (avctx->sample_aspect_ratio.num > 255 ||
466 avctx->sample_aspect_ratio.den > 255)) {
467 av_log(avctx, AV_LOG_WARNING,
468 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
469 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
470 av_reduce(&avctx->sample_aspect