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
26 * non linear quantizers with large QPs and VBV with restrictive qmin fixes sponsored by NOA GmbH
31 * The simplest mpeg encoder (well, it was the simplest!).
36 #include "libavutil/internal.h"
37 #include "libavutil/intmath.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/timer.h"
46 #include "mpegvideo.h"
47 #include "mpegvideodata.h"
51 #include "mjpegenc_common.h"
53 #include "mpegutils.h"
56 #include "pixblockdsp.h"
60 #include "aandcttab.h"
62 #include "mpeg4video.h"
64 #include "bytestream.h"
71 #define QUANT_BIAS_SHIFT 8
73 #define QMAT_SHIFT_MMX 16
76 static int encode_picture(MpegEncContext *s, int picture_number);
77 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
78 static int sse_mb(MpegEncContext *s);
79 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
80 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
82 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
83 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
85 const AVOption ff_mpv_generic_options[] = {
90 void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
91 uint16_t (*qmat16)[2][64],
92 const uint16_t *quant_matrix,
93 int bias, int qmin, int qmax, int intra)
95 FDCTDSPContext *fdsp = &s->fdsp;
99 for (qscale = qmin; qscale <= qmax; qscale++) {
103 if (s->q_scale_type) qscale2 = ff_mpeg2_non_linear_qscale[qscale];
104 else qscale2 = qscale << 1;
106 if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
108 fdsp->fdct == ff_faandct ||
109 #endif /* CONFIG_FAANDCT */
110 fdsp->fdct == ff_jpeg_fdct_islow_10) {
111 for (i = 0; i < 64; i++) {
112 const int j = s->idsp.idct_permutation[i];
113 int64_t den = (int64_t) qscale2 * quant_matrix[j];
114 /* 16 <= qscale * quant_matrix[i] <= 7905
115 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
116 * 19952 <= x <= 249205026
117 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
118 * 3444240 >= (1 << 36) / (x) >= 275 */
120 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
122 } else if (fdsp->fdct == ff_fdct_ifast) {
123 for (i = 0; i < 64; i++) {
124 const int j = s->idsp.idct_permutation[i];
125 int64_t den = ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
126 /* 16 <= qscale * quant_matrix[i] <= 7905
127 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
128 * 19952 <= x <= 249205026
129 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
130 * 3444240 >= (1 << 36) / (x) >= 275 */
132 qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den);
135 for (i = 0; i < 64; i++) {
136 const int j = s->idsp.idct_permutation[i];
137 int64_t den = (int64_t) qscale2 * quant_matrix[j];
138 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
139 * Assume x = qscale * quant_matrix[i]
141 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
142 * so 32768 >= (1 << 19) / (x) >= 67 */
143 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
144 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
145 // (qscale * quant_matrix[i]);
146 qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den;
148 if (qmat16[qscale][0][i] == 0 ||
149 qmat16[qscale][0][i] == 128 * 256)
150 qmat16[qscale][0][i] = 128 * 256 - 1;
151 qmat16[qscale][1][i] =
152 ROUNDED_DIV(bias * (1<<(16 - QUANT_BIAS_SHIFT)),
153 qmat16[qscale][0][i]);
157 for (i = intra; i < 64; i++) {
159 if (fdsp->fdct == ff_fdct_ifast) {
160 max = (8191LL * ff_aanscales[i]) >> 14;
162 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
168 av_log(NULL, AV_LOG_INFO,
169 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
174 static inline void update_qscale(MpegEncContext *s)
176 if (s->q_scale_type == 1 && 0) {
178 int bestdiff=INT_MAX;
181 for (i = 0 ; i<FF_ARRAY_ELEMS(ff_mpeg2_non_linear_qscale); i++) {
182 int diff = FFABS((ff_mpeg2_non_linear_qscale[i]<<(FF_LAMBDA_SHIFT + 6)) - (int)s->lambda * 139);
183 if (ff_mpeg2_non_linear_qscale[i] < s->avctx->qmin ||
184 (ff_mpeg2_non_linear_qscale[i] > s->avctx->qmax && !s->vbv_ignore_qmax))
186 if (diff < bestdiff) {
193 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
194 (FF_LAMBDA_SHIFT + 7);
195 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->vbv_ignore_qmax ? 31 : s->avctx->qmax);
198 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
202 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
208 for (i = 0; i < 64; i++) {
209 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
216 * init s->current_picture.qscale_table from s->lambda_table
218 void ff_init_qscale_tab(MpegEncContext *s)
220 int8_t * const qscale_table = s->current_picture.qscale_table;
223 for (i = 0; i < s->mb_num; i++) {
224 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
225 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
226 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
231 static void update_duplicate_context_after_me(MpegEncContext *dst,
234 #define COPY(a) dst->a= src->a
236 COPY(current_picture);
242 COPY(picture_in_gop_number);
243 COPY(gop_picture_number);
244 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
245 COPY(progressive_frame); // FIXME don't set in encode_header
246 COPY(partitioned_frame); // FIXME don't set in encode_header
251 * Set the given MpegEncContext to defaults for encoding.
252 * the changed fields will not depend upon the prior state of the MpegEncContext.
254 static void mpv_encode_defaults(MpegEncContext *s)
257 ff_mpv_common_defaults(s);
259 for (i = -16; i < 16; i++) {
260 default_fcode_tab[i + MAX_MV] = 1;
262 s->me.mv_penalty = default_mv_penalty;
263 s->fcode_tab = default_fcode_tab;
265 s->input_picture_number = 0;
266 s->picture_in_gop_number = 0;
269 av_cold int ff_dct_encode_init(MpegEncContext *s) {
271 ff_dct_encode_init_x86(s);
273 if (CONFIG_H263_ENCODER)
274 ff_h263dsp_init(&s->h263dsp);
275 if (!s->dct_quantize)
276 s->dct_quantize = ff_dct_quantize_c;
278 s->denoise_dct = denoise_dct_c;
279 s->fast_dct_quantize = s->dct_quantize;
280 if (s->avctx->trellis)
281 s->dct_quantize = dct_quantize_trellis_c;
286 /* init video encoder */
287 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
289 MpegEncContext *s = avctx->priv_data;
290 AVCPBProperties *cpb_props;
291 int i, ret, format_supported;
293 mpv_encode_defaults(s);
295 switch (avctx->codec_id) {
296 case AV_CODEC_ID_MPEG2VIDEO:
297 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
298 avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
299 av_log(avctx, AV_LOG_ERROR,
300 "only YUV420 and YUV422 are supported\n");
304 case AV_CODEC_ID_MJPEG:
305 case AV_CODEC_ID_AMV:
306 format_supported = 0;
307 /* JPEG color space */
308 if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
309 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
310 avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
311 (avctx->color_range == AVCOL_RANGE_JPEG &&
312 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
313 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
314 avctx->pix_fmt == AV_PIX_FMT_YUV444P)))
315 format_supported = 1;
316 /* MPEG color space */
317 else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
318 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
319 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
320 avctx->pix_fmt == AV_PIX_FMT_YUV444P))
321 format_supported = 1;
323 if (!format_supported) {
324 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
329 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
330 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
335 switch (avctx->pix_fmt) {
336 case AV_PIX_FMT_YUVJ444P:
337 case AV_PIX_FMT_YUV444P:
338 s->chroma_format = CHROMA_444;
340 case AV_PIX_FMT_YUVJ422P:
341 case AV_PIX_FMT_YUV422P:
342 s->chroma_format = CHROMA_422;
344 case AV_PIX_FMT_YUVJ420P:
345 case AV_PIX_FMT_YUV420P:
347 s->chroma_format = CHROMA_420;
351 avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
353 #if FF_API_PRIVATE_OPT
354 FF_DISABLE_DEPRECATION_WARNINGS
355 if (avctx->rtp_payload_size)
356 s->rtp_payload_size = avctx->rtp_payload_size;
357 if (avctx->me_penalty_compensation)
358 s->me_penalty_compensation = avctx->me_penalty_compensation;
360 s->me_pre = avctx->pre_me;
361 FF_ENABLE_DEPRECATION_WARNINGS
364 s->bit_rate = avctx->bit_rate;
365 s->width = avctx->width;
366 s->height = avctx->height;
367 if (avctx->gop_size > 600 &&
368 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
369 av_log(avctx, AV_LOG_WARNING,
370 "keyframe interval too large!, reducing it from %d to %d\n",
371 avctx->gop_size, 600);
372 avctx->gop_size = 600;
374 s->gop_size = avctx->gop_size;
376 if (avctx->max_b_frames > MAX_B_FRAMES) {
377 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
378 "is %d.\n", MAX_B_FRAMES);
379 avctx->max_b_frames = MAX_B_FRAMES;
381 s->max_b_frames = avctx->max_b_frames;
382 s->codec_id = avctx->codec->id;
383 s->strict_std_compliance = avctx->strict_std_compliance;
384 s->quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
385 s->rtp_mode = !!s->rtp_payload_size;
386 s->intra_dc_precision = avctx->intra_dc_precision;
388 // workaround some differences between how applications specify dc precision
389 if (s->intra_dc_precision < 0) {
390 s->intra_dc_precision += 8;
391 } else if (s->intra_dc_precision >= 8)
392 s->intra_dc_precision -= 8;
394 if (s->intra_dc_precision < 0) {
395 av_log(avctx, AV_LOG_ERROR,
396 "intra dc precision must be positive, note some applications use"
397 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
398 return AVERROR(EINVAL);
401 if (s->intra_dc_precision > (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 3 : 0)) {
402 av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
403 return AVERROR(EINVAL);
405 s->user_specified_pts = AV_NOPTS_VALUE;
407 if (s->gop_size <= 1) {
414 #if FF_API_MOTION_EST
415 FF_DISABLE_DEPRECATION_WARNINGS
416 s->me_method = avctx->me_method;
417 FF_ENABLE_DEPRECATION_WARNINGS
421 s->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
424 FF_DISABLE_DEPRECATION_WARNINGS
425 if (avctx->border_masking != 0.0)
426 s->border_masking = avctx->border_masking;
427 FF_ENABLE_DEPRECATION_WARNINGS
430 s->adaptive_quant = (s->avctx->lumi_masking ||
431 s->avctx->dark_masking ||
432 s->avctx->temporal_cplx_masking ||
433 s->avctx->spatial_cplx_masking ||
434 s->avctx->p_masking ||
436 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
439 s->loop_filter = !!(s->avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
441 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
442 switch(avctx->codec_id) {
443 case AV_CODEC_ID_MPEG1VIDEO:
444 case AV_CODEC_ID_MPEG2VIDEO:
445 avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112LL / 15000000 * 16384;
447 case AV_CODEC_ID_MPEG4:
448 case AV_CODEC_ID_MSMPEG4V1:
449 case AV_CODEC_ID_MSMPEG4V2:
450 case AV_CODEC_ID_MSMPEG4V3:
451 if (avctx->rc_max_rate >= 15000000) {
452 avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000LL) * (760-320) / (38400000 - 15000000);
453 } else if(avctx->rc_max_rate >= 2000000) {
454 avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000LL) * (320- 80) / (15000000 - 2000000);
455 } else if(avctx->rc_max_rate >= 384000) {
456 avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000LL) * ( 80- 40) / ( 2000000 - 384000);
458 avctx->rc_buffer_size = 40;
459 avctx->rc_buffer_size *= 16384;
462 if (avctx->rc_buffer_size) {
463 av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
467 if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
468 av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
472 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
473 av_log(avctx, AV_LOG_INFO,
474 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
477 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
478 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
482 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
483 av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
487 if (avctx->rc_max_rate &&
488 avctx->rc_max_rate == avctx->bit_rate &&
489 avctx->rc_max_rate != avctx->rc_min_rate) {
490 av_log(avctx, AV_LOG_INFO,
491 "impossible bitrate constraints, this will fail\n");
494 if (avctx->rc_buffer_size &&
495 avctx->bit_rate * (int64_t)avctx->time_base.num >
496 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
497 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
501 if (!s->fixed_qscale &&
502 avctx->bit_rate * av_q2d(avctx->time_base) >
503 avctx->bit_rate_tolerance) {
504 av_log(avctx, AV_LOG_WARNING,
505 "bitrate tolerance %d too small for bitrate %"PRId64", overriding\n", avctx->bit_rate_tolerance, (int64_t)avctx->bit_rate);
506 avctx->bit_rate_tolerance = 5 * avctx->bit_rate * av_q2d(avctx->time_base);
509 if (s->avctx->rc_max_rate &&
510 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
511 (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
512 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
513 90000LL * (avctx->rc_buffer_size - 1) >
514 s->avctx->rc_max_rate * 0xFFFFLL) {
515 av_log(avctx, AV_LOG_INFO,
516 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
517 "specified vbv buffer is too large for the given bitrate!\n");
520 if ((s->avctx->flags & AV_CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
521 s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
522 s->codec_id != AV_CODEC_ID_FLV1) {
523 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
527 if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
528 av_log(avctx, AV_LOG_ERROR,
529 "OBMC is only supported with simple mb decision\n");
533 if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
534 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
538 if (s->max_b_frames &&
539 s->codec_id != AV_CODEC_ID_MPEG4 &&
540 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
541 s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
542 av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
545 if (s->max_b_frames < 0) {
546 av_log(avctx, AV_LOG_ERROR,
547 "max b frames must be 0 or positive for mpegvideo based encoders\n");
551 if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
552 s->codec_id == AV_CODEC_ID_H263 ||
553 s->codec_id == AV_CODEC_ID_H263P) &&
554 (avctx->sample_aspect_ratio.num > 255 ||
555 avctx->sample_aspect_ratio.den > 255)) {
556 av_log(avctx, AV_LOG_WARNING,
557 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
558 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
559 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
560 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
563 if ((s->codec_id == AV_CODEC_ID_H263 ||
564 s->codec_id == AV_CODEC_ID_H263P) &&
565 (avctx->width > 2048 ||
566 avctx->height > 1152 )) {
567 av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
570 if ((s->codec_id == AV_CODEC_ID_H263 ||
571 s->codec_id == AV_CODEC_ID_H263P) &&
572 ((avctx->width &3) ||
573 (avctx->height&3) )) {
574 av_log(avctx, AV_LOG_ERROR, "w/h must be a multiple of 4\n");
578 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
579 (avctx->width > 4095 ||
580 avctx->height > 4095 )) {
581 av_log(avctx, AV_LOG_ERROR, "MPEG-1 does not support resolutions above 4095x4095\n");
585 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
586 (avctx->width > 16383 ||
587 avctx->height > 16383 )) {
588 av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support resolutions above 16383x16383\n");
592 if (s->codec_id == AV_CODEC_ID_RV10 &&
594 avctx->height&15 )) {
595 av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
596 return AVERROR(EINVAL);
599 if (s->codec_id == AV_CODEC_ID_RV20 &&
602 av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
603 return AVERROR(EINVAL);
606 if ((s->codec_id == AV_CODEC_ID_WMV1 ||
607 s->codec_id == AV_CODEC_ID_WMV2) &&
609 av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
613 if ((s->avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)) &&
614 s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
615 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
619 #if FF_API_PRIVATE_OPT
620 FF_DISABLE_DEPRECATION_WARNINGS
621 if (avctx->mpeg_quant)
622 s->mpeg_quant = avctx->mpeg_quant;
623 FF_ENABLE_DEPRECATION_WARNINGS
626 // FIXME mpeg2 uses that too
627 if (s->mpeg_quant && ( s->codec_id != AV_CODEC_ID_MPEG4
628 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO)) {
629 av_log(avctx, AV_LOG_ERROR,
630 "mpeg2 style quantization not supported by codec\n");
634 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
635 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
639 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
640 s->avctx->mb_decision != FF_MB_DECISION_RD) {
641 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
645 #if FF_API_PRIVATE_OPT
646 FF_DISABLE_DEPRECATION_WARNINGS
647 if (avctx->scenechange_threshold)
648 s->scenechange_threshold = avctx->scenechange_threshold;
649 FF_ENABLE_DEPRECATION_WARNINGS
652 if (s->scenechange_threshold < 1000000000 &&
653 (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
654 av_log(avctx, AV_LOG_ERROR,
655 "closed gop with scene change detection are not supported yet, "
656 "set threshold to 1000000000\n");
660 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY) {
661 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
662 av_log(avctx, AV_LOG_ERROR,
663 "low delay forcing is only available for mpeg2\n");
666 if (s->max_b_frames != 0) {
667 av_log(avctx, AV_LOG_ERROR,
668 "B-frames cannot be used with low delay\n");
673 if (s->q_scale_type == 1) {
674 if (avctx->qmax > 28) {
675 av_log(avctx, AV_LOG_ERROR,
676 "non linear quant only supports qmax <= 28 currently\n");
681 if (avctx->slices > 1 &&
682 (avctx->codec_id == AV_CODEC_ID_FLV1 || avctx->codec_id == AV_CODEC_ID_H261)) {
683 av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
684 return AVERROR(EINVAL);
687 if (s->avctx->thread_count > 1 &&
688 s->codec_id != AV_CODEC_ID_MPEG4 &&
689 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
690 s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
691 s->codec_id != AV_CODEC_ID_MJPEG &&
692 (s->codec_id != AV_CODEC_ID_H263P)) {
693 av_log(avctx, AV_LOG_ERROR,
694 "multi threaded encoding not supported by codec\n");
698 if (s->avctx->thread_count < 1) {
699 av_log(avctx, AV_LOG_ERROR,
700 "automatic thread number detection not supported by codec, "
705 if (!avctx->time_base.den || !avctx->time_base.num) {
706 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
710 #if FF_API_PRIVATE_OPT
711 FF_DISABLE_DEPRECATION_WARNINGS
712 if (avctx->b_frame_strategy)
713 s->b_frame_strategy = avctx->b_frame_strategy;
714 if (avctx->b_sensitivity != 40)
715 s->b_sensitivity = avctx->b_sensitivity;
716 FF_ENABLE_DEPRECATION_WARNINGS
719 if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
720 av_log(avctx, AV_LOG_INFO,
721 "notice: b_frame_strategy only affects the first pass\n");
722 s->b_frame_strategy = 0;
725 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
727 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
728 avctx->time_base.den /= i;
729 avctx->time_base.num /= i;
733 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG || s->codec_id==AV_CODEC_ID_AMV) {
734 // (a + x * 3 / 8) / x
735 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
736 s->inter_quant_bias = 0;
738 s->intra_quant_bias = 0;
740 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
743 if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
744 av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
745 return AVERROR(EINVAL);
748 #if FF_API_QUANT_BIAS
749 FF_DISABLE_DEPRECATION_WARNINGS
750 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
751 s->intra_quant_bias = avctx->intra_quant_bias;
752 if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
753 s->inter_quant_bias = avctx->inter_quant_bias;
754 FF_ENABLE_DEPRECATION_WARNINGS
757 av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
759 if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
760 s->avctx->time_base.den > (1 << 16) - 1) {
761 av_log(avctx, AV_LOG_ERROR,
762 "timebase %d/%d not supported by MPEG 4 standard, "
763 "the maximum admitted value for the timebase denominator "
764 "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
768 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
770 switch (avctx->codec->id) {
771 case AV_CODEC_ID_MPEG1VIDEO:
772 s->out_format = FMT_MPEG1;
773 s->low_delay = !!(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
774 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
776 case AV_CODEC_ID_MPEG2VIDEO:
777 s->out_format = FMT_MPEG1;
778 s->low_delay = !!(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
779 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
782 case AV_CODEC_ID_MJPEG:
783 case AV_CODEC_ID_AMV:
784 s->out_format = FMT_MJPEG;
785 s->intra_only = 1; /* force intra only for jpeg */
786 if (!CONFIG_MJPEG_ENCODER ||
787 ff_mjpeg_encode_init(s) < 0)
792 case AV_CODEC_ID_H261:
793 if (!CONFIG_H261_ENCODER)
795 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
796 av_log(avctx, AV_LOG_ERROR,
797 "The specified picture size of %dx%d is not valid for the "
798 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
799 s->width, s->height);
802 s->out_format = FMT_H261;
805 s->rtp_mode = 0; /* Sliced encoding not supported */
807 case AV_CODEC_ID_H263:
808 if (!CONFIG_H263_ENCODER)
810 if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
811 s->width, s->height) == 8) {
812 av_log(avctx, AV_LOG_ERROR,
813 "The specified picture size of %dx%d is not valid for "
814 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
815 "352x288, 704x576, and 1408x1152. "
816 "Try H.263+.\n", s->width, s->height);
819 s->out_format = FMT_H263;
823 case AV_CODEC_ID_H263P:
824 s->out_format = FMT_H263;
827 s->h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
828 s->modified_quant = s->h263_aic;
829 s->loop_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
830 s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
833 /* These are just to be sure */
837 case AV_CODEC_ID_FLV1:
838 s->out_format = FMT_H263;
839 s->h263_flv = 2; /* format = 1; 11-bit codes */
840 s->unrestricted_mv = 1;
841 s->rtp_mode = 0; /* don't allow GOB */
845 case AV_CODEC_ID_RV10:
846 s->out_format = FMT_H263;
850 case AV_CODEC_ID_RV20:
851 s->out_format = FMT_H263;
854 s->modified_quant = 1;
858 s->unrestricted_mv = 0;
860 case AV_CODEC_ID_MPEG4:
861 s->out_format = FMT_H263;
863 s->unrestricted_mv = 1;
864 s->low_delay = s->max_b_frames ? 0 : 1;
865 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
867 case AV_CODEC_ID_MSMPEG4V2:
868 s->out_format = FMT_H263;
870 s->unrestricted_mv = 1;
871 s->msmpeg4_version = 2;
875 case AV_CODEC_ID_MSMPEG4V3:
876 s->out_format = FMT_H263;
878 s->unrestricted_mv = 1;
879 s->msmpeg4_version = 3;
880 s->flipflop_rounding = 1;
884 case AV_CODEC_ID_WMV1:
885 s->out_format = FMT_H263;
887 s->unrestricted_mv = 1;
888 s->msmpeg4_version = 4;
889 s->flipflop_rounding = 1;
893 case AV_CODEC_ID_WMV2:
894 s->out_format = FMT_H263;
896 s->unrestricted_mv = 1;
897 s->msmpeg4_version = 5;
898 s->flipflop_rounding = 1;
906 #if FF_API_PRIVATE_OPT
907 FF_DISABLE_DEPRECATION_WARNINGS
908 if (avctx->noise_reduction)
909 s->noise_reduction = avctx->noise_reduction;
910 FF_ENABLE_DEPRECATION_WARNINGS
913 avctx->has_b_frames = !s->low_delay;
917 s->progressive_frame =
918 s->progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
919 AV_CODEC_FLAG_INTERLACED_ME) ||
924 if (ff_mpv_common_init(s) < 0)
927 ff_fdctdsp_init(&s->fdsp, avctx);
928 ff_me_cmp_init(&s->mecc, avctx);
929 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
930 ff_pixblockdsp_init(&s->pdsp, avctx);
931 ff_qpeldsp_init(&s->qdsp);
933 if (s->msmpeg4_version) {
934 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
935 2 * 2 * (MAX_LEVEL + 1) *
936 (MAX_RUN + 1) * 2 * sizeof(int), fail);
938 FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
940 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail);
941 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail);
942 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail);
943 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
944 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
945 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
946 FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
947 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
948 FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
949 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
952 if (s->noise_reduction) {
953 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
954 2 * 64 * sizeof(uint16_t), fail);
957 ff_dct_encode_init(s);
959 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
960 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
962 if (s->slice_context_count > 1) {
965 if (avctx->codec_id == AV_CODEC_ID_H263P)
966 s->h263_slice_structured = 1;
969 s->quant_precision = 5;
971 #if FF_API_PRIVATE_OPT
972 FF_DISABLE_DEPRECATION_WARNINGS
973 if (avctx->frame_skip_threshold)
974 s->frame_skip_threshold = avctx->frame_skip_threshold;
975 if (avctx->frame_skip_factor)
976 s->frame_skip_factor = avctx->frame_skip_factor;
977 if (avctx->frame_skip_exp)
978 s->frame_skip_exp = avctx->frame_skip_exp;
979 if (avctx->frame_skip_cmp != FF_CMP_DCTMAX)
980 s->frame_skip_cmp = avctx->frame_skip_cmp;
981 FF_ENABLE_DEPRECATION_WARNINGS
984 ff_set_cmp(&s->mecc, s->mecc.ildct_cmp, s->avctx->ildct_cmp);
985 ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->frame_skip_cmp);
987 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
988 ff_h261_encode_init(s);
989 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
990 ff_h263_encode_init(s);
991 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
992 if ((ret = ff_msmpeg4_encode_init(s)) < 0)
994 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
995 && s->out_format == FMT_MPEG1)
996 ff_mpeg1_encode_init(s);
999 for (i = 0; i < 64; i++) {
1000 int j = s->idsp.idct_permutation[i];
1001 if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
1003 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
1004 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
1005 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1006 s->intra_matrix[j] =
1007 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
1010 s->chroma_intra_matrix[j] =
1011 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
1012 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
1014 if (s->avctx->intra_matrix)
1015 s->intra_matrix[j] = s->avctx->intra_matrix[i];
1016 if (s->avctx->inter_matrix)
1017 s->inter_matrix[j] = s->avctx->inter_matrix[i];
1020 /* precompute matrix */
1021 /* for mjpeg, we do include qscale in the matrix */
1022 if (s->out_format != FMT_MJPEG) {
1023 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
1024 s->intra_matrix, s->intra_quant_bias, avctx->qmin,
1026 ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
1027 s->inter_matrix, s->inter_quant_bias, avctx->qmin,
1031 #if FF_API_RC_STRATEGY
1032 FF_DISABLE_DEPRECATION_WARNINGS
1033 if (!s->rc_strategy)
1034 s->rc_strategy = s->avctx->rc_strategy;
1035 FF_ENABLE_DEPRECATION_WARNINGS
1038 if (ff_rate_control_init(s) < 0)
1041 #if FF_API_RC_STRATEGY
1042 av_assert0(MPV_RC_STRATEGY_XVID == FF_RC_STRATEGY_XVID);
1045 if ((s->avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == MPV_RC_STRATEGY_XVID) {
1047 ret = ff_xvid_rate_control_init(s);
1049 ret = AVERROR(ENOSYS);
1050 av_log(s->avctx, AV_LOG_ERROR,
1051 "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
1057 #if FF_API_ERROR_RATE
1058 FF_DISABLE_DEPRECATION_WARNINGS
1059 if (avctx->error_rate)
1060 s->error_rate = avctx->error_rate;
1061 FF_ENABLE_DEPRECATION_WARNINGS;
1064 #if FF_API_NORMALIZE_AQP
1065 FF_DISABLE_DEPRECATION_WARNINGS
1066 if (avctx->flags & CODEC_FLAG_NORMALIZE_AQP)
1067 s->mpv_flags |= FF_MPV_FLAG_NAQ;
1068 FF_ENABLE_DEPRECATION_WARNINGS;
1072 FF_DISABLE_DEPRECATION_WARNINGS
1073 if (avctx->flags & CODEC_FLAG_MV0)
1074 s->mpv_flags |= FF_MPV_FLAG_MV0;
1075 FF_ENABLE_DEPRECATION_WARNINGS
1079 FF_DISABLE_DEPRECATION_WARNINGS
1080 if (avctx->rc_qsquish != 0.0)
1081 s->rc_qsquish = avctx->rc_qsquish;
1082 if (avctx->rc_qmod_amp != 0.0)
1083 s->rc_qmod_amp = avctx->rc_qmod_amp;
1084 if (avctx->rc_qmod_freq)
1085 s->rc_qmod_freq = avctx->rc_qmod_freq;
1086 if (avctx->rc_buffer_aggressivity != 1.0)
1087 s->rc_buffer_aggressivity = avctx->rc_buffer_aggressivity;
1088 if (avctx->rc_initial_cplx != 0.0)
1089 s->rc_initial_cplx = avctx->rc_initial_cplx;
1091 s->lmin = avctx->lmin;
1093 s->lmax = avctx->lmax;
1096 av_freep(&s->rc_eq);
1097 s->rc_eq = av_strdup(avctx->rc_eq);
1099 return AVERROR(ENOMEM);
1101 FF_ENABLE_DEPRECATION_WARNINGS
1104 #if FF_API_PRIVATE_OPT
1105 FF_DISABLE_DEPRECATION_WARNINGS
1106 if (avctx->brd_scale)
1107 s->brd_scale = avctx->brd_scale;
1109 if (avctx->prediction_method)
1110 s->pred = avctx->prediction_method + 1;
1111 FF_ENABLE_DEPRECATION_WARNINGS
1114 if (s->b_frame_strategy == 2) {
1115 for (i = 0; i < s->max_b_frames + 2; i++) {
1116 s->tmp_frames[i] = av_frame_alloc();
1117 if (!s->tmp_frames[i])
1118 return AVERROR(ENOMEM);
1120 s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
1121 s->tmp_frames[i]->width = s->width >> s->brd_scale;
1122 s->tmp_frames[i]->height = s->height >> s->brd_scale;
1124 ret = av_frame_get_buffer(s->tmp_frames[i], 32);
1130 cpb_props = ff_add_cpb_side_data(avctx);
1132 return AVERROR(ENOMEM);
1133 cpb_props->max_bitrate = avctx->rc_max_rate;
1134 cpb_props->min_bitrate = avctx->rc_min_rate;
1135 cpb_props->avg_bitrate = avctx->bit_rate;
1136 cpb_props->buffer_size = avctx->rc_buffer_size;
1140 ff_mpv_encode_end(avctx);
1141 return AVERROR_UNKNOWN;
1144 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
1146 MpegEncContext *s = avctx->priv_data;
1149 ff_rate_control_uninit(s);
1151 if ((avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == MPV_RC_STRATEGY_XVID)
1152 ff_xvid_rate_control_uninit(s);
1155 ff_mpv_common_end(s);
1156 if (CONFIG_MJPEG_ENCODER &&
1157 s->out_format == FMT_MJPEG)
1158 ff_mjpeg_encode_close(s);
1160 av_freep(&avctx->extradata);
1162 for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
1163 av_frame_free(&s->tmp_frames[i]);
1165 ff_free_picture_tables(&s->new_picture);
1166 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1168 av_freep(&s->avctx->stats_out);
1169 av_freep(&s->ac_stats);
1171 if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
1172 if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
1173 s->q_chroma_intra_matrix= NULL;
1174 s->q_chroma_intra_matrix16= NULL;
1175 av_freep(&s->q_intra_matrix);
1176 av_freep(&s->q_inter_matrix);
1177 av_freep(&s->q_intra_matrix16);
1178 av_freep(&s->q_inter_matrix16);
1179 av_freep(&s->input_picture);
1180 av_freep(&s->reordered_input_picture);
1181 av_freep(&s->dct_offset);
1186 static int get_sae(uint8_t *src, int ref, int stride)
1191 for (y = 0; y < 16; y++) {
1192 for (x = 0; x < 16; x++) {
1193 acc += FFABS(src[x + y * stride] - ref);
1200 static int get_intra_count(MpegEncContext *s, uint8_t *src,
1201 uint8_t *ref, int stride)
1207 h = s->height & ~15;
1209 for (y = 0; y < h; y += 16) {
1210 for (x = 0; x < w; x += 16) {
1211 int offset = x + y * stride;
1212 int sad = s->mecc.sad[0](NULL, src + offset, ref + offset,
1214 int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
1215 int sae = get_sae(src + offset, mean, stride);
1217 acc += sae + 500 < sad;
1223 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
1225 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,
1226 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
1227 s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
1228 &s->linesize, &s->uvlinesize);
1231 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
1233 Picture *pic = NULL;
1235 int i, display_picture_number = 0, ret;
1236 int encoding_delay = s->max_b_frames ? s->max_b_frames
1237 : (s->low_delay ? 0 : 1);
1238 int flush_offset = 1;
1243 display_picture_number = s->input_picture_number++;
1245 if (pts != AV_NOPTS_VALUE) {
1246 if (s->user_specified_pts != AV_NOPTS_VALUE) {
1247 int64_t last = s->user_specified_pts;
1250 av_log(s->avctx, AV_LOG_ERROR,
1251 "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
1253 return AVERROR(EINVAL);
1256 if (!s->low_delay && display_picture_number == 1)
1257 s->dts_delta = pts - last;
1259 s->user_specified_pts = pts;
1261 if (s->user_specified_pts != AV_NOPTS_VALUE) {
1262 s->user_specified_pts =
1263 pts = s->user_specified_pts + 1;
1264 av_log(s->avctx, AV_LOG_INFO,
1265 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1268 pts = display_picture_number;
1272 if (!pic_arg->buf[0] ||
1273 pic_arg->linesize[0] != s->linesize ||
1274 pic_arg->linesize[1] != s->uvlinesize ||
1275 pic_arg->linesize[2] != s->uvlinesize)
1277 if ((s->width & 15) || (s->height & 15))
1279 if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
1281 if (s->linesize & (STRIDE_ALIGN-1))
1284 ff_dlog(s->avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
1285 pic_arg->linesize[1], s->linesize, s->uvlinesize);
1287 i = ff_find_unused_picture(s->avctx, s->picture, direct);
1291 pic = &s->picture[i];
1295 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1298 ret = alloc_picture(s, pic, direct);
1303 if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1304 pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1305 pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1308 int h_chroma_shift, v_chroma_shift;
1309 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1313 for (i = 0; i < 3; i++) {
1314 int src_stride = pic_arg->linesize[i];
1315 int dst_stride = i ? s->uvlinesize : s->linesize;
1316 int h_shift = i ? h_chroma_shift : 0;
1317 int v_shift = i ? v_chroma_shift : 0;
1318 int w = s->width >> h_shift;
1319 int h = s->height >> v_shift;
1320 uint8_t *src = pic_arg->data[i];
1321 uint8_t *dst = pic->f->data[i];
1324 if ( s->codec_id == AV_CODEC_ID_MPEG2VIDEO
1325 && !s->progressive_sequence
1326 && FFALIGN(s->height, 32) - s->height > 16)
1329 if (!s->avctx->rc_buffer_size)
1330 dst += INPLACE_OFFSET;
1332 if (src_stride == dst_stride)
1333 memcpy(dst, src, src_stride * h);
1336 uint8_t *dst2 = dst;
1338 memcpy(dst2, src, w);
1343 if ((s->width & 15) || (s->height & (vpad-1))) {
1344 s->mpvencdsp.draw_edges(dst, dst_stride,
1354 ret = av_frame_copy_props(pic->f, pic_arg);
1358 pic->f->display_picture_number = display_picture_number;
1359 pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1361 /* Flushing: When we have not received enough input frames,
1362 * ensure s->input_picture[0] contains the first picture */
1363 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1364 if (s->input_picture[flush_offset])
1367 if (flush_offset <= 1)
1370 encoding_delay = encoding_delay - flush_offset + 1;
1373 /* shift buffer entries */
1374 for (i = flush_offset; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1375 s->input_picture[i - flush_offset] = s->input_picture[i];
1377 s->input_picture[encoding_delay] = (Picture*) pic;
1382 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1386 int64_t score64 = 0;
1388 for (plane = 0; plane < 3; plane++) {
1389 const int stride = p->f->linesize[plane];
1390 const int bw = plane ? 1 : 2;
1391 for (y = 0; y < s->mb_height * bw; y++) {
1392 for (x = 0; x < s->mb_width * bw; x++) {
1393 int off = p->shared ? 0 : 16;
1394 uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1395 uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1396 int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1398 switch (FFABS(s->frame_skip_exp)) {
1399 case 0: score = FFMAX(score, v); break;
1400 case 1: score += FFABS(v); break;
1401 case 2: score64 += v * (int64_t)v; break;
1402 case 3: score64 += FFABS(v * (int64_t)v * v); break;
1403 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
1412 if (s->frame_skip_exp < 0)
1413 score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
1414 -1.0/s->frame_skip_exp);
1416 if (score64 < s->frame_skip_threshold)
1418 if (score64 < ((s->frame_skip_factor * (int64_t) s->lambda) >> 8))
1423 static int encode_frame(AVCodecContext *c, AVFrame *frame)
1425 AVPacket pkt = { 0 };
1426 int ret, got_output;
1428 av_init_packet(&pkt);
1429 ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
1434 av_packet_unref(&pkt);
1438 static int estimate_best_b_count(MpegEncContext *s)
1440 AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1441 AVCodecContext *c = avcodec_alloc_context3(NULL);
1442 const int scale = s->brd_scale;
1443 int i, j, out_size, p_lambda, b_lambda, lambda2;
1444 int64_t best_rd = INT64_MAX;
1445 int best_b_count = -1;
1448 return AVERROR(ENOMEM);
1449 av_assert0(scale >= 0 && scale <= 3);
1452 //s->next_picture_ptr->quality;
1453 p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1454 //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1455 b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1456 if (!b_lambda) // FIXME we should do this somewhere else
1457 b_lambda = p_lambda;
1458 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1461 c->width = s->width >> scale;
1462 c->height = s->height >> scale;
1463 c->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
1464 c->flags |= s->avctx->flags & AV_CODEC_FLAG_QPEL;
1465 c->mb_decision = s->avctx->mb_decision;
1466 c->me_cmp = s->avctx->me_cmp;
1467 c->mb_cmp = s->avctx->mb_cmp;
1468 c->me_sub_cmp = s->avctx->me_sub_cmp;
1469 c->pix_fmt = AV_PIX_FMT_YUV420P;
1470 c->time_base = s->avctx->time_base;
1471 c->max_b_frames = s->max_b_frames;
1473 if (avcodec_open2(c, codec, NULL) < 0)
1476 for (i = 0; i < s->max_b_frames + 2; i++) {
1477 Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1478 s->next_picture_ptr;
1481 if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1482 pre_input = *pre_input_ptr;
1483 memcpy(data, pre_input_ptr->f->data, sizeof(data));
1485 if (!pre_input.shared && i) {
1486 data[0] += INPLACE_OFFSET;
1487 data[1] += INPLACE_OFFSET;
1488 data[2] += INPLACE_OFFSET;
1491 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1492 s->tmp_frames[i]->linesize[0],
1494 pre_input.f->linesize[0],
1495 c->width, c->height);
1496 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1497 s->tmp_frames[i]->linesize[1],
1499 pre_input.f->linesize[1],
1500 c->width >> 1, c->height >> 1);
1501 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1502 s->tmp_frames[i]->linesize[2],
1504 pre_input.f->linesize[2],
1505 c->width >> 1, c->height >> 1);
1509 for (j = 0; j < s->max_b_frames + 1; j++) {
1512 if (!s->input_picture[j])
1515 c->error[0] = c->error[1] = c->error[2] = 0;
1517 s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1518 s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1520 out_size = encode_frame(c, s->tmp_frames[0]);
1522 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1524 for (i = 0; i < s->max_b_frames + 1; i++) {
1525 int is_p = i % (j + 1) == j || i == s->max_b_frames;
1527 s->tmp_frames[i + 1]->pict_type = is_p ?
1528 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1529 s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1531 out_size = encode_frame(c, s->tmp_frames[i + 1]);
1533 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1536 /* get the delayed frames */
1538 out_size = encode_frame(c, NULL);
1539 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1542 rd += c->error[0] + c->error[1] + c->error[2];
1550 avcodec_free_context(&c);
1552 return best_b_count;
1555 static int select_input_picture(MpegEncContext *s)
1559 for (i = 1; i < MAX_PICTURE_COUNT; i++)
1560 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1561 s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1563 /* set next picture type & ordering */
1564 if (!s->reordered_input_picture[0] && s->input_picture[0]) {
1565 if (s->frame_skip_threshold || s->frame_skip_factor) {
1566 if (s->picture_in_gop_number < s->gop_size &&
1567 s->next_picture_ptr &&
1568 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1569 // FIXME check that the gop check above is +-1 correct
1570 av_frame_unref(s->input_picture[0]->f);
1572 ff_vbv_update(s, 0);
1578 if (/*s->picture_in_gop_number >= s->gop_size ||*/
1579 !s->next_picture_ptr || s->intra_only) {
1580 s->reordered_input_picture[0] = s->input_picture[0];
1581 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1582 s->reordered_input_picture[0]->f->coded_picture_number =
1583 s->coded_picture_number++;
1587 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
1588 for (i = 0; i < s->max_b_frames + 1; i++) {
1589 int pict_num = s->input_picture[0]->f->display_picture_number + i;
1591 if (pict_num >= s->rc_context.num_entries)
1593 if (!s->input_picture[i]) {
1594 s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1598 s->input_picture[i]->f->pict_type =
1599 s->rc_context.entry[pict_num].new_pict_type;
1603 if (s->b_frame_strategy == 0) {
1604 b_frames = s->max_b_frames;
1605 while (b_frames && !s->input_picture[b_frames])
1607 } else if (s->b_frame_strategy == 1) {
1608 for (i = 1; i < s->max_b_frames + 1; i++) {
1609 if (s->input_picture[i] &&
1610 s->input_picture[i]->b_frame_score == 0) {
1611 s->input_picture[i]->b_frame_score =
1613 s->input_picture[i ]->f->data[0],
1614 s->input_picture[i - 1]->f->data[0],
1618 for (i = 0; i < s->max_b_frames + 1; i++) {
1619 if (!s->input_picture[i] ||
1620 s->input_picture[i]->b_frame_score - 1 >
1621 s->mb_num / s->b_sensitivity)
1625 b_frames = FFMAX(0, i - 1);
1628 for (i = 0; i < b_frames + 1; i++) {
1629 s->input_picture[i]->b_frame_score = 0;
1631 } else if (s->b_frame_strategy == 2) {
1632 b_frames = estimate_best_b_count(s);
1637 for (i = b_frames - 1; i >= 0; i--) {
1638 int type = s->input_picture[i]->f->pict_type;
1639 if (type && type != AV_PICTURE_TYPE_B)
1642 if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1643 b_frames == s->max_b_frames) {
1644 av_log(s->avctx, AV_LOG_ERROR,
1645 "warning, too many B-frames in a row\n");
1648 if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1649 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1650 s->gop_size > s->picture_in_gop_number) {
1651 b_frames = s->gop_size - s->picture_in_gop_number - 1;
1653 if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
1655 s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1659 if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1660 s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1663 s->reordered_input_picture[0] = s->input_picture[b_frames];
1664 if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1665 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1666 s->reordered_input_picture[0]->f->coded_picture_number =
1667 s->coded_picture_number++;
1668 for (i = 0; i < b_frames; i++) {
1669 s->reordered_input_picture[i + 1] = s->input_picture[i];
1670 s->reordered_input_picture[i + 1]->f->pict_type =
1672 s->reordered_input_picture[i + 1]->f->coded_picture_number =
1673 s->coded_picture_number++;
1678 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1680 if (s->reordered_input_picture[0]) {
1681 s->reordered_input_picture[0]->reference =
1682 s->reordered_input_picture[0]->f->pict_type !=
1683 AV_PICTURE_TYPE_B ? 3 : 0;
1685 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))
1688 if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1689 // input is a shared pix, so we can't modify it -> allocate a new
1690 // one & ensure that the shared one is reuseable
1693 int i = ff_find_unused_picture(s->avctx, s->picture, 0);
1696 pic = &s->picture[i];
1698 pic->reference = s->reordered_input_picture[0]->reference;
1699 if (alloc_picture(s, pic, 0) < 0) {
1703 ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1707 /* mark us unused / free shared pic */
1708 av_frame_unref(s->reordered_input_picture[0]->f);
1709 s->reordered_input_picture[0]->shared = 0;
1711 s->current_picture_ptr = pic;
1713 // input is not a shared pix -> reuse buffer for current_pix
1714 s->current_picture_ptr = s->reordered_input_picture[0];
1715 for (i = 0; i < 4; i++) {
1716 s->new_picture.f->data[i] += INPLACE_OFFSET;
1719 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1720 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1721 s->current_picture_ptr)) < 0)
1724 s->picture_number = s->new_picture.f->display_picture_number;
1729 static void frame_end(MpegEncContext *s)
1731 if (s->unrestricted_mv &&
1732 s->current_picture.reference &&
1734 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1735 int hshift = desc->log2_chroma_w;
1736 int vshift = desc->log2_chroma_h;
1737 s->mpvencdsp.draw_edges(s->current_picture.f->data[0],
1738 s->current_picture.f->linesize[0],
1739 s->h_edge_pos, s->v_edge_pos,
1740 EDGE_WIDTH, EDGE_WIDTH,
1741 EDGE_TOP | EDGE_BOTTOM);
1742 s->mpvencdsp.draw_edges(s->current_picture.f->data[1],
1743 s->current_picture.f->linesize[1],
1744 s->h_edge_pos >> hshift,
1745 s->v_edge_pos >> vshift,
1746 EDGE_WIDTH >> hshift,
1747 EDGE_WIDTH >> vshift,
1748 EDGE_TOP | EDGE_BOTTOM);
1749 s->mpvencdsp.draw_edges(s->current_picture.f->data[2],
1750 s->current_picture.f->linesize[2],
1751 s->h_edge_pos >> hshift,
1752 s->v_edge_pos >> vshift,
1753 EDGE_WIDTH >> hshift,
1754 EDGE_WIDTH >> vshift,
1755 EDGE_TOP | EDGE_BOTTOM);
1760 s->last_pict_type = s->pict_type;
1761 s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1762 if (s->pict_type!= AV_PICTURE_TYPE_B)
1763 s->last_non_b_pict_type = s->pict_type;
1765 #if FF_API_CODED_FRAME
1766 FF_DISABLE_DEPRECATION_WARNINGS
1767 av_frame_unref(s->avctx->coded_frame);
1768 av_frame_copy_props(s->avctx->coded_frame, s->current_picture.f);
1769 FF_ENABLE_DEPRECATION_WARNINGS
1771 #if FF_API_ERROR_FRAME
1772 FF_DISABLE_DEPRECATION_WARNINGS
1773 memcpy(s->current_picture.f->error, s->current_picture.encoding_error,
1774 sizeof(s->current_picture.encoding_error));
1775 FF_ENABLE_DEPRECATION_WARNINGS
1779 static void update_noise_reduction(MpegEncContext *s)
1783 for (intra = 0; intra < 2; intra++) {
1784 if (s->dct_count[intra] > (1 << 16)) {
1785 for (i = 0; i < 64; i++) {
1786 s->dct_error_sum[intra][i] >>= 1;
1788 s->dct_count[intra] >>= 1;
1791 for (i = 0; i < 64; i++) {
1792 s->dct_offset[intra][i] = (s->noise_reduction *
1793 s->dct_count[intra] +
1794 s->dct_error_sum[intra][i] / 2) /
1795 (s->dct_error_sum[intra][i] + 1);
1800 static int frame_start(MpegEncContext *s)
1804 /* mark & release old frames */
1805 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1806 s->last_picture_ptr != s->next_picture_ptr &&
1807 s->last_picture_ptr->f->buf[0]) {
1808 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1811 s->current_picture_ptr->f->pict_type = s->pict_type;
1812 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1814 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1815 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1816 s->current_picture_ptr)) < 0)
1819 if (s->pict_type != AV_PICTURE_TYPE_B) {
1820 s->last_picture_ptr = s->next_picture_ptr;
1822 s->next_picture_ptr = s->current_picture_ptr;
1825 if (s->last_picture_ptr) {
1826 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1827 if (s->last_picture_ptr->f->buf[0] &&
1828 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1829 s->last_picture_ptr)) < 0)
1832 if (s->next_picture_ptr) {
1833 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1834 if (s->next_picture_ptr->f->buf[0] &&
1835 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1836 s->next_picture_ptr)) < 0)
1840 if (s->picture_structure!= PICT_FRAME) {
1842 for (i = 0; i < 4; i++) {
1843 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1844 s->current_picture.f->data[i] +=
1845 s->current_picture.f->linesize[i];
1847 s->current_picture.f->linesize[i] *= 2;
1848 s->last_picture.f->linesize[i] *= 2;
1849 s->next_picture.f->linesize[i] *= 2;
1853 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1854 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1855 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1856 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1857 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1858 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1860 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1861 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1864 if (s->dct_error_sum) {
1865 av_assert2(s->noise_reduction && s->encoding);
1866 update_noise_reduction(s);
1872 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1873 const AVFrame *pic_arg, int *got_packet)
1875 MpegEncContext *s = avctx->priv_data;
1876 int i, stuffing_count, ret;
1877 int context_count = s->slice_context_count;
1879 s->vbv_ignore_qmax = 0;
1881 s->picture_in_gop_number++;
1883 if (load_input_picture(s, pic_arg) < 0)
1886 if (select_input_picture(s) < 0) {
1891 if (s->new_picture.f->data[0]) {
1892 int growing_buffer = context_count == 1 && !pkt->data && !s->data_partitioning;
1893 int pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, avctx->internal->byte_buffer_size) - AV_INPUT_BUFFER_PADDING_SIZE
1895 s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000;
1896 if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
1899 s->mb_info_ptr = av_packet_new_side_data(pkt,
1900 AV_PKT_DATA_H263_MB_INFO,
1901 s->mb_width*s->mb_height*12);
1902 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1905 for (i = 0; i < context_count; i++) {
1906 int start_y = s->thread_context[i]->start_mb_y;
1907 int end_y = s->thread_context[i]-> end_mb_y;
1908 int h = s->mb_height;
1909 uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1910 uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1912 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1915 s->pict_type = s->new_picture.f->pict_type;
1917 ret = frame_start(s);
1921 ret = encode_picture(s, s->picture_number);
1922 if (growing_buffer) {
1923 av_assert0(s->pb.buf == avctx->internal->byte_buffer);
1924 pkt->data = s->pb.buf;
1925 pkt->size = avctx->internal->byte_buffer_size;
1930 #if FF_API_STAT_BITS
1931 FF_DISABLE_DEPRECATION_WARNINGS
1932 avctx->header_bits = s->header_bits;
1933 avctx->mv_bits = s->mv_bits;
1934 avctx->misc_bits = s->misc_bits;
1935 avctx->i_tex_bits = s->i_tex_bits;
1936 avctx->p_tex_bits = s->p_tex_bits;
1937 avctx->i_count = s->i_count;
1938 // FIXME f/b_count in avctx
1939 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
1940 avctx->skip_count = s->skip_count;
1941 FF_ENABLE_DEPRECATION_WARNINGS
1946 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1947 ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1949 if (avctx->rc_buffer_size) {
1950 RateControlContext *rcc = &s->rc_context;
1951 int max_size = FFMAX(rcc->buffer_index * avctx->rc_max_available_vbv_use, rcc->buffer_index - 500);
1952 int hq = (s->avctx->mb_decision == FF_MB_DECISION_RD || s->avctx->trellis);
1953 int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
1955 if (put_bits_count(&s->pb) > max_size &&
1956 s->lambda < s->lmax) {
1957 s->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
1958 (s->qscale + 1) / s->qscale);
1959 if (s->adaptive_quant) {
1961 for (i = 0; i < s->mb_height * s->mb_stride; i++)
1962 s->lambda_table[i] =
1963 FFMAX(s->lambda_table[i] + min_step,
1964 s->lambda_table[i] * (s->qscale + 1) /
1967 s->mb_skipped = 0; // done in frame_start()
1968 // done in encode_picture() so we must undo it
1969 if (s->pict_type == AV_PICTURE_TYPE_P) {
1970 if (s->flipflop_rounding ||
1971 s->codec_id == AV_CODEC_ID_H263P ||
1972 s->codec_id == AV_CODEC_ID_MPEG4)
1973 s->no_rounding ^= 1;
1975 if (s->pict_type != AV_PICTURE_TYPE_B) {
1976 s->time_base = s->last_time_base;
1977 s->last_non_b_time = s->time - s->pp_time;
1979 for (i = 0; i < context_count; i++) {
1980 PutBitContext *pb = &s->thread_context[i]->pb;
1981 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1983 s->vbv_ignore_qmax = 1;
1984 av_log(s->avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
1988 av_assert0(s->avctx->rc_max_rate);
1991 if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
1992 ff_write_pass1_stats(s);
1994 for (i = 0; i < 4; i++) {
1995 s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
1996 avctx->error[i] += s->current_picture_ptr->encoding_error[i];
1998 ff_side_data_set_encoder_stats(pkt, s->current_picture.f->quality,
1999 s->current_picture_ptr->encoding_error,
2000 (s->avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
2003 if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
2004 assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
2005 s->misc_bits + s->i_tex_bits +
2007 flush_put_bits(&s->pb);
2008 s->frame_bits = put_bits_count(&s->pb);
2010 stuffing_count = ff_vbv_update(s, s->frame_bits);
2011 s->stuffing_bits = 8*stuffing_count;
2012 if (stuffing_count) {
2013 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
2014 stuffing_count + 50) {
2015 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
2019 switch (s->codec_id) {
2020 case AV_CODEC_ID_MPEG1VIDEO:
2021 case AV_CODEC_ID_MPEG2VIDEO:
2022 while (stuffing_count--) {
2023 put_bits(&s->pb, 8, 0);
2026 case AV_CODEC_ID_MPEG4:
2027 put_bits(&s->pb, 16, 0);
2028 put_bits(&s->pb, 16, 0x1C3);
2029 stuffing_count -= 4;
2030 while (stuffing_count--) {
2031 put_bits(&s->pb, 8, 0xFF);
2035 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
2037 flush_put_bits(&s->pb);
2038 s->frame_bits = put_bits_count(&s->pb);
2041 /* update MPEG-1/2 vbv_delay for CBR */
2042 if (s->avctx->rc_max_rate &&
2043 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
2044 s->out_format == FMT_MPEG1 &&
2045 90000LL * (avctx->rc_buffer_size - 1) <=
2046 s->avctx->rc_max_rate * 0xFFFFLL) {
2047 AVCPBProperties *props;
2050 int vbv_delay, min_delay;
2051 double inbits = s->avctx->rc_max_rate *
2052 av_q2d(s->avctx->time_base);
2053 int minbits = s->frame_bits - 8 *
2054 (s->vbv_delay_ptr - s->pb.buf - 1);
2055 double bits = s->rc_context.buffer_index + minbits - inbits;
2058 av_log(s->avctx, AV_LOG_ERROR,
2059 "Internal error, negative bits\n");
2061 assert(s->repeat_first_field == 0);
2063 vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
2064 min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
2065 s->avctx->rc_max_rate;
2067 vbv_delay = FFMAX(vbv_delay, min_delay);
2069 av_assert0(vbv_delay < 0xFFFF);
2071 s->vbv_delay_ptr[0] &= 0xF8;
2072 s->vbv_delay_ptr[0] |= vbv_delay >> 13;
2073 s->vbv_delay_ptr[1] = vbv_delay >> 5;
2074 s->vbv_delay_ptr[2] &= 0x07;
2075 s->vbv_delay_ptr[2] |= vbv_delay << 3;
2077 props = av_cpb_properties_alloc(&props_size);
2079 return AVERROR(ENOMEM);
2080 props->vbv_delay = vbv_delay * 300;
2082 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
2083 (uint8_t*)props, props_size);
2089 #if FF_API_VBV_DELAY
2090 FF_DISABLE_DEPRECATION_WARNINGS
2091 avctx->vbv_delay = vbv_delay * 300;
2092 FF_ENABLE_DEPRECATION_WARNINGS
2095 s->total_bits += s->frame_bits;
2096 #if FF_API_STAT_BITS
2097 FF_DISABLE_DEPRECATION_WARNINGS
2098 avctx->frame_bits = s->frame_bits;
2099 FF_ENABLE_DEPRECATION_WARNINGS
2103 pkt->pts = s->current_picture.f->pts;
2104 if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
2105 if (!s->current_picture.f->coded_picture_number)
2106 pkt->dts = pkt->pts - s->dts_delta;
2108 pkt->dts = s->reordered_pts;
2109 s->reordered_pts = pkt->pts;
2111 pkt->dts = pkt->pts;
2112 if (s->current_picture.f->key_frame)
2113 pkt->flags |= AV_PKT_FLAG_KEY;
2115 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
2120 /* release non-reference frames */
2121 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
2122 if (!s->picture[i].reference)
2123 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
2126 av_assert1((s->frame_bits & 7) == 0);
2128 pkt->size = s->frame_bits / 8;
2129 *got_packet = !!pkt->size;
2133 static inline void dct_single_coeff_elimination(MpegEncContext *s,
2134 int n, int threshold)
2136 static const char tab[64] = {
2137 3, 2, 2, 1, 1, 1, 1, 1,
2138 1, 1, 1, 1, 1, 1, 1, 1,
2139 1, 1, 1, 1, 1, 1, 1, 1,
2140 0, 0, 0, 0, 0, 0, 0, 0,
2141 0, 0, 0, 0, 0, 0, 0, 0,
2142 0, 0, 0, 0, 0, 0, 0, 0,
2143 0, 0, 0, 0, 0, 0, 0, 0,
2144 0, 0, 0, 0, 0, 0, 0, 0
2149 int16_t *block = s->block[n];
2150 const int last_index = s->block_last_index[n];
2153 if (threshold < 0) {
2155 threshold = -threshold;
2159 /* Are all we could set to zero already zero? */
2160 if (last_index <= skip_dc - 1)
2163 for (i = 0; i <= last_index; i++) {
2164 const int j = s->intra_scantable.permutated[i];
2165 const int level = FFABS(block[j]);
2167 if (skip_dc && i == 0)
2171 } else if (level > 1) {
2177 if (score >= threshold)
2179 for (i = skip_dc; i <= last_index; i++) {
2180 const int j = s->intra_scantable.permutated[i];
2184 s->block_last_index[n] = 0;
2186 s->block_last_index[n] = -1;
2189 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
2193 const int maxlevel = s->max_qcoeff;
2194 const int minlevel = s->min_qcoeff;
2198 i = 1; // skip clipping of intra dc
2202 for (; i <= last_index; i++) {
2203 const int j = s->intra_scantable.permutated[i];
2204 int level = block[j];
2206 if (level > maxlevel) {
2209 } else if (level < minlevel) {
2217 if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
2218 av_log(s->avctx, AV_LOG_INFO,
2219 "warning, clipping %d dct coefficients to %d..%d\n",
2220 overflow, minlevel, maxlevel);
2223 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
2227 for (y = 0; y < 8; y++) {
2228 for (x = 0; x < 8; x++) {
2234 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
2235 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
2236 int v = ptr[x2 + y2 * stride];
2242 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2247 static av_always_inline void encode_mb_internal(MpegEncContext *s,
2248 int motion_x, int motion_y,
2249 int mb_block_height,
2253 int16_t weight[12][64];
2254 int16_t orig[12][64];
2255 const int mb_x = s->mb_x;
2256 const int mb_y = s->mb_y;
2259 int dct_offset = s->linesize * 8; // default for progressive frames
2260 int uv_dct_offset = s->uvlinesize * 8;
2261 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2262 ptrdiff_t wrap_y, wrap_c;
2264 for (i = 0; i < mb_block_count; i++)
2265 skip_dct[i] = s->skipdct;
2267 if (s->adaptive_quant) {
2268 const int last_qp = s->qscale;
2269 const int mb_xy = mb_x + mb_y * s->mb_stride;
2271 s->lambda = s->lambda_table[mb_xy];
2274 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2275 s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
2276 s->dquant = s->qscale - last_qp;
2278 if (s->out_format == FMT_H263) {
2279 s->dquant = av_clip(s->dquant, -2, 2);
2281 if (s->codec_id == AV_CODEC_ID_MPEG4) {
2283 if (s->pict_type == AV_PICTURE_TYPE_B) {
2284 if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
2287 if (s->mv_type == MV_TYPE_8X8)
2293 ff_set_qscale(s, last_qp + s->dquant);
2294 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2295 ff_set_qscale(s, s->qscale + s->dquant);
2297 wrap_y = s->linesize;
2298 wrap_c = s->uvlinesize;
2299 ptr_y = s->new_picture.f->data[0] +
2300 (mb_y * 16 * wrap_y) + mb_x * 16;
2301 ptr_cb = s->new_picture.f->data[1] +
2302 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2303 ptr_cr = s->new_picture.f->data[2] +
2304 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2306 if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
2307 uint8_t *ebuf = s->sc.edge_emu_buffer + 38 * wrap_y;
2308 int cw = (s->width + s->chroma_x_shift) >> s->chroma_x_shift;
2309 int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
2310 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2312 16, 16, mb_x * 16, mb_y * 16,
2313 s->width, s->height);
2315 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2317 mb_block_width, mb_block_height,
2318 mb_x * mb_block_width, mb_y * mb_block_height,
2320 ptr_cb = ebuf + 16 * wrap_y;
2321 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2323 mb_block_width, mb_block_height,
2324 mb_x * mb_block_width, mb_y * mb_block_height,
2326 ptr_cr = ebuf + 16 * wrap_y + 16;
2330 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2331 int progressive_score, interlaced_score;
2333 s->interlaced_dct = 0;
2334 progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
2335 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
2336 NULL, wrap_y, 8) - 400;
2338 if (progressive_score > 0) {
2339 interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
2340 NULL, wrap_y * 2, 8) +
2341 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
2342 NULL, wrap_y * 2, 8);
2343 if (progressive_score > interlaced_score) {
2344 s->interlaced_dct = 1;
2346 dct_offset = wrap_y;
2347 uv_dct_offset = wrap_c;
2349 if (s->chroma_format == CHROMA_422 ||
2350 s->chroma_format == CHROMA_444)
2356 s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
2357 s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
2358 s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
2359 s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2361 if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2365 s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2366 s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2367 if (!s->chroma_y_shift && s->chroma_x_shift) { /* 422 */
2368 s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2369 s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2370 } else if (!s->chroma_y_shift && !s->chroma_x_shift) { /* 444 */
2371 s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
2372 s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
2373 s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2374 s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2375 s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2376 s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2380 op_pixels_func (*op_pix)[4];
2381 qpel_mc_func (*op_qpix)[16];
2382 uint8_t *dest_y, *dest_cb, *dest_cr;
2384 dest_y = s->dest[0];
2385 dest_cb = s->dest[1];
2386 dest_cr = s->dest[2];
2388 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
2389 op_pix = s->hdsp.put_pixels_tab;
2390 op_qpix = s->qdsp.put_qpel_pixels_tab;
2392 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2393 op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
2396 if (s->mv_dir & MV_DIR_FORWARD) {
2397 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
2398 s->last_picture.f->data,
2400 op_pix = s->hdsp.avg_pixels_tab;
2401 op_qpix = s->qdsp.avg_qpel_pixels_tab;
2403 if (s->mv_dir & MV_DIR_BACKWARD) {
2404 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
2405 s->next_picture.f->data,
2409 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2410 int progressive_score, interlaced_score;
2412 s->interlaced_dct = 0;
2413 progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2414 s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
2418 if (s->avctx->ildct_cmp == FF_CMP_VSSE)
2419 progressive_score -= 400;
2421 if (progressive_score > 0) {
2422 interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
2424 s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
2428 if (progressive_score > interlaced_score) {
2429 s->interlaced_dct = 1;
2431 dct_offset = wrap_y;
2432 uv_dct_offset = wrap_c;
2434 if (s->chroma_format == CHROMA_422)
2440 s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2441 s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2442 s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2443 dest_y + dct_offset, wrap_y);
2444 s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2445 dest_y + dct_offset + 8, wrap_y);
2447 if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2451 s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2452 s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2453 if (!s->chroma_y_shift) { /* 422 */
2454 s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2455 dest_cb + uv_dct_offset, wrap_c);
2456 s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2457 dest_cr + uv_dct_offset, wrap_c);
2460 /* pre quantization */
2461 if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2462 2 * s->qscale * s->qscale) {
2464 if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2466 if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2468 if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2469 wrap_y, 8) < 20 * s->qscale)
2471 if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2472 wrap_y, 8) < 20 * s->qscale)
2474 if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2476 if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2478 if (!s->chroma_y_shift) { /* 422 */
2479 if (s->mecc.sad[1](NULL, ptr_cb + uv_dct_offset,
2480 dest_cb + uv_dct_offset,
2481 wrap_c, 8) < 20 * s->qscale)
2483 if (s->mecc.sad[1](NULL, ptr_cr + uv_dct_offset,
2484 dest_cr + uv_dct_offset,
2485 wrap_c, 8) < 20 * s->qscale)
2491 if (s->quantizer_noise_shaping) {
2493 get_visual_weight(weight[0], ptr_y , wrap_y);
2495 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2497 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2499 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2501 get_visual_weight(weight[4], ptr_cb , wrap_c);
2503 get_visual_weight(weight[5], ptr_cr , wrap_c);
2504 if (!s->chroma_y_shift) { /* 422 */
2506 get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2509 get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2512 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2515 /* DCT & quantize */
2516 av_assert2(s->out_format != FMT_MJPEG || s->qscale == 8);
2518 for (i = 0; i < mb_block_count; i++) {
2521 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2522 // FIXME we could decide to change to quantizer instead of
2524 // JS: I don't think that would be a good idea it could lower
2525 // quality instead of improve it. Just INTRADC clipping
2526 // deserves changes in quantizer
2528 clip_coeffs(s, s->block[i], s->block_last_index[i]);
2530 s->block_last_index[i] = -1;
2532 if (s->quantizer_noise_shaping) {
2533 for (i = 0; i < mb_block_count; i++) {
2535 s->block_last_index[i] =
2536 dct_quantize_refine(s, s->block[i], weight[i],
2537 orig[i], i, s->qscale);
2542 if (s->luma_elim_threshold && !s->mb_intra)
2543 for (i = 0; i < 4; i++)
2544 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2545 if (s->chroma_elim_threshold && !s->mb_intra)
2546 for (i = 4; i < mb_block_count; i++)
2547 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2549 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2550 for (i = 0; i < mb_block_count; i++) {
2551 if (s->block_last_index[i] == -1)
2552 s->coded_score[i] = INT_MAX / 256;
2557 if ((s->avctx->flags & AV_CODEC_FLAG_GRAY) && s->mb_intra) {
2558 s->block_last_index[4] =
2559 s->block_last_index[5] = 0;
2561 s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2562 if (!s->chroma_y_shift) { /* 422 / 444 */
2563 for (i=6; i<12; i++) {
2564 s->block_last_index[i] = 0;
2565 s->block[i][0] = s->block[4][0];
2570 // non c quantize code returns incorrect block_last_index FIXME
2571 if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2572 for (i = 0; i < mb_block_count; i++) {
2574 if (s->block_last_index[i] > 0) {
2575 for (j = 63; j > 0; j--) {
2576 if (s->block[i][s->intra_scantable.permutated[j]])
2579 s->block_last_index[i] = j;
2584 /* huffman encode */
2585 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2586 case AV_CODEC_ID_MPEG1VIDEO:
2587 case AV_CODEC_ID_MPEG2VIDEO:
2588 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2589 ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2591 case AV_CODEC_ID_MPEG4:
2592 if (CONFIG_MPEG4_ENCODER)
2593 ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2595 case AV_CODEC_ID_MSMPEG4V2:
2596 case AV_CODEC_ID_MSMPEG4V3:
2597 case AV_CODEC_ID_WMV1:
2598 if (CONFIG_MSMPEG4_ENCODER)
2599 ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2601 case AV_CODEC_ID_WMV2:
2602 if (CONFIG_WMV2_ENCODER)
2603 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2605 case AV_CODEC_ID_H261:
2606 if (CONFIG_H261_ENCODER)
2607 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2609 case AV_CODEC_ID_H263:
2610 case AV_CODEC_ID_H263P:
2611 case AV_CODEC_ID_FLV1:
2612 case AV_CODEC_ID_RV10:
2613 case AV_CODEC_ID_RV20:
2614 if (CONFIG_H263_ENCODER)
2615 ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2617 case AV_CODEC_ID_MJPEG:
2618 case AV_CODEC_ID_AMV:
2619 if (CONFIG_MJPEG_ENCODER)
2620 ff_mjpeg_encode_mb(s, s->block);
2627 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2629 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 8, 6);
2630 else if (s->chroma_format == CHROMA_422) encode_mb_internal(s, motion_x, motion_y, 16, 8, 8);
2631 else encode_mb_internal(s, motion_x, motion_y, 16, 16, 12);
2634 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2637 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2640 d->mb_skip_run= s->mb_skip_run;
2642 d->last_dc[i] = s->last_dc[i];
2645 d->mv_bits= s->mv_bits;
2646 d->i_tex_bits= s->i_tex_bits;
2647 d->p_tex_bits= s->p_tex_bits;
2648 d->i_count= s->i_count;
2649 d->f_count= s->f_count;
2650 d->b_count= s->b_count;
2651 d->skip_count= s->skip_count;
2652 d->misc_bits= s->misc_bits;
2656 d->qscale= s->qscale;
2657 d->dquant= s->dquant;
2659 d->esc3_level_length= s->esc3_level_length;
2662 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2665 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2666 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2669 d->mb_skip_run= s->mb_skip_run;
2671 d->last_dc[i] = s->last_dc[i];
2674 d->mv_bits= s->mv_bits;
2675 d->i_tex_bits= s->i_tex_bits;
2676 d->p_tex_bits= s->p_tex_bits;
2677 d->i_count= s->i_count;
2678 d->f_count= s->f_count;
2679 d->b_count= s->b_count;
2680 d->skip_count= s->skip_count;
2681 d->misc_bits= s->misc_bits;
2683 d->mb_intra= s->mb_intra;
2684 d->mb_skipped= s->mb_skipped;
2685 d->mv_type= s->mv_type;
2686 d->mv_dir= s->mv_dir;
2688 if(s->data_partitioning){
2690 d->tex_pb= s->tex_pb;
2694 d->block_last_index[i]= s->block_last_index[i];
2695 d->interlaced_dct= s->interlaced_dct;
2696 d->qscale= s->qscale;
2698 d->esc3_level_length= s->esc3_level_length;
2701 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2702 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2703 int *dmin, int *next_block, int motion_x, int motion_y)
2706 uint8_t *dest_backup[3];
2708 copy_context_before_encode(s, backup, type);
2710 s->block= s->blocks[*next_block];
2711 s->pb= pb[*next_block];
2712 if(s->data_partitioning){
2713 s->pb2 = pb2 [*next_block];
2714 s->tex_pb= tex_pb[*next_block];
2718 memcpy(dest_backup, s->dest, sizeof(s->dest));
2719 s->dest[0] = s->sc.rd_scratchpad;
2720 s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
2721 s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
2722 av_assert0(s->linesize >= 32); //FIXME
2725 encode_mb(s, motion_x, motion_y);
2727 score= put_bits_count(&s->pb);
2728 if(s->data_partitioning){
2729 score+= put_bits_count(&s->pb2);
2730 score+= put_bits_count(&s->tex_pb);
2733 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2734 ff_mpv_decode_mb(s, s->block);
2736 score *= s->lambda2;
2737 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2741 memcpy(s->dest, dest_backup, sizeof(s->dest));
2748 copy_context_after_encode(best, s, type);
2752 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2753 uint32_t *sq = ff_square_tab + 256;
2758 return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2759 else if(w==8 && h==8)
2760 return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2764 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2773 static int sse_mb(MpegEncContext *s){
2777 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2778 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2781 if(s->avctx->mb_cmp == FF_CMP_NSSE){
2782 return s->mecc.nsse[0](s, s->new_picture.f->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16, s->dest[0], s->linesize, 16) +
2783 s->mecc.nsse[1](s, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[1], s->uvlinesize, 8) +
2784 s->mecc.nsse[1](s, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[2], s->uvlinesize, 8);
2786 return s->mecc.sse[0](NULL, s->new_picture.f->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16, s->dest[0], s->linesize, 16) +
2787 s->mecc.sse[1](NULL, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[1], s->uvlinesize, 8) +
2788 s->mecc.sse[1](NULL, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[2], s->uvlinesize, 8);
2791 return sse(s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
2792 +sse(s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
2793 +sse(s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
2796 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2797 MpegEncContext *s= *(void**)arg;
2801 s->me.dia_size= s->avctx->pre_dia_size;
2802 s->first_slice_line=1;
2803 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2804 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2805 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2807 s->first_slice_line=0;
2815 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2816 MpegEncContext *s= *(void**)arg;
2818 ff_check_alignment();
2820 s->me.dia_size= s->avctx->dia_size;
2821 s->first_slice_line=1;
2822 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2823 s->mb_x=0; //for block init below
2824 ff_init_block_index(s);
2825 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2826 s->block_index[0]+=2;
2827 s->block_index[1]+=2;
2828 s->block_index[2]+=2;
2829 s->block_index[3]+=2;
2831 /* compute motion vector & mb_type and store in context */
2832 if(s->pict_type==AV_PICTURE_TYPE_B)
2833 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2835 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2837 s->first_slice_line=0;
2842 static int mb_var_thread(AVCodecContext *c, void *arg){
2843 MpegEncContext *s= *(void**)arg;
2846 ff_check_alignment();
2848 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2849 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2852 uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2854 int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2856 varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
2857 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2859 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2860 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2861 s->me.mb_var_sum_temp += varc;
2867 static void write_slice_end(MpegEncContext *s){
2868 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2869 if(s->partitioned_frame){
2870 ff_mpeg4_merge_partitions(s);
2873 ff_mpeg4_stuffing(&s->pb);
2874 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2875 ff_mjpeg_encode_stuffing(s);
2878 avpriv_align_put_bits(&s->pb);
2879 flush_put_bits(&s->pb);
2881 if ((s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
2882 s->misc_bits+= get_bits_diff(s);
2885 static void write_mb_info(MpegEncContext *s)
2887 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2888 int offset = put_bits_count(&s->pb);
2889 int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2890 int gobn = s->mb_y / s->gob_index;
2892 if (CONFIG_H263_ENCODER)
2893 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2894 bytestream_put_le32(&ptr, offset);
2895 bytestream_put_byte(&ptr, s->qscale);
2896 bytestream_put_byte(&ptr, gobn);
2897 bytestream_put_le16(&ptr, mba);
2898 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2899 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2900 /* 4MV not implemented */
2901 bytestream_put_byte(&ptr, 0); /* hmv2 */
2902 bytestream_put_byte(&ptr, 0); /* vmv2 */
2905 static void update_mb_info(MpegEncContext *s, int startcode)
2909 if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2910 s->mb_info_size += 12;
2911 s->prev_mb_info = s->last_mb_info;
2914 s->prev_mb_info = put_bits_count(&s->pb)/8;
2915 /* This might have incremented mb_info_size above, and we return without
2916 * actually writing any info into that slot yet. But in that case,
2917 * this will be called again at the start of the after writing the
2918 * start code, actually writing the mb info. */
2922 s->last_mb_info = put_bits_count(&s->pb)/8;
2923 if (!s->mb_info_size)
2924 s->mb_info_size += 12;
2928 int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
2930 if ( s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold
2931 && s->slice_context_count == 1
2932 && s->pb.buf == s->avctx->internal->byte_buffer) {
2933 int lastgob_pos = s->ptr_lastgob - s->pb.buf;
2934 int vbv_pos = s->vbv_delay_ptr - s->pb.buf;
2936 uint8_t *new_buffer = NULL;
2937 int new_buffer_size = 0;
2939 if ((s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2940 av_log(s->avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
2941 return AVERROR(ENOMEM);
2946 av_fast_padded_malloc(&new_buffer, &new_buffer_size,
2947 s->avctx->internal->byte_buffer_size + size_increase);
2949 return AVERROR(ENOMEM);
2951 memcpy(new_buffer, s->avctx->internal->byte_buffer, s->avctx->internal->byte_buffer_size);
2952 av_free(s->avctx->internal->byte_buffer);
2953 s->avctx->internal->byte_buffer = new_buffer;
2954 s->avctx->internal->byte_buffer_size = new_buffer_size;
2955 rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
2956 s->ptr_lastgob = s->pb.buf + lastgob_pos;
2957 s->vbv_delay_ptr = s->pb.buf + vbv_pos;
2959 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < threshold)
2960 return AVERROR(EINVAL);
2964 static int encode_thread(AVCodecContext *c, void *arg){
2965 MpegEncContext *s= *(void**)arg;
2967 int chr_h= 16>>s->chroma_y_shift;
2969 MpegEncContext best_s = { 0 }, backup_s;
2970 uint8_t bit_buf[2][MAX_MB_BYTES];
2971 uint8_t bit_buf2[2][MAX_MB_BYTES];
2972 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2973 PutBitContext pb[2], pb2[2], tex_pb[2];
2975 ff_check_alignment();
2978 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2979 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2980 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2983 s->last_bits= put_bits_count(&s->pb);
2994 /* init last dc values */
2995 /* note: quant matrix value (8) is implied here */
2996 s->last_dc[i] = 128 << s->intra_dc_precision;
2998 s->current_picture.encoding_error[i] = 0;
3000 if(s->codec_id==AV_CODEC_ID_AMV){
3001 s->last_dc[0] = 128*8/13;
3002 s->last_dc[1] = 128*8/14;
3003 s->last_dc[2] = 128*8/14;
3006 memset(s->last_mv, 0, sizeof(s->last_mv));
3010 switch(s->codec_id){
3011 case AV_CODEC_ID_H263:
3012 case AV_CODEC_ID_H263P:
3013 case AV_CODEC_ID_FLV1:
3014 if (CONFIG_H263_ENCODER)
3015 s->gob_index = H263_GOB_HEIGHT(s->height);
3017 case AV_CODEC_ID_MPEG4:
3018 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
3019 ff_mpeg4_init_partitions(s);
3025 s->first_slice_line = 1;
3026 s->ptr_lastgob = s->pb.buf;
3027 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
3031 ff_set_qscale(s, s->qscale);
3032 ff_init_block_index(s);
3034 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
3035 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
3036 int mb_type= s->mb_type[xy];
3040 int size_increase = s->avctx->internal->byte_buffer_size/4
3041 + s->mb_width*MAX_MB_BYTES;
3043 ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
3044 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
3045 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
3048 if(s->data_partitioning){
3049 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
3050 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
3051 av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
3057 s->mb_y = mb_y; // moved into loop, can get changed by H.261
3058 ff_update_block_index(s);
3060 if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
3061 ff_h261_reorder_mb_index(s);
3062 xy= s->mb_y*s->mb_stride + s->mb_x;
3063 mb_type= s->mb_type[xy];
3066 /* write gob / video packet header */
3068 int current_packet_size, is_gob_start;
3070 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
3072 is_gob_start = s->rtp_payload_size &&
3073 current_packet_size >= s->rtp_payload_size &&
3076 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
3078 switch(s->codec_id){
3079 case AV_CODEC_ID_H263:
3080 case AV_CODEC_ID_H263P:
3081 if(!s->h263_slice_structured)
3082 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
3084 case AV_CODEC_ID_MPEG2VIDEO:
3085 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
3086 case AV_CODEC_ID_MPEG1VIDEO:
3087 if(s->mb_skip_run) is_gob_start=0;
3089 case AV_CODEC_ID_MJPEG:
3090 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
3095 if(s->start_mb_y != mb_y || mb_x!=0){
3098 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
3099 ff_mpeg4_init_partitions(s);
3103 av_assert2((put_bits_count(&s->pb)&7) == 0);
3104 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
3106 if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
3107 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
3108 int d = 100 / s->error_rate;
3110 current_packet_size=0;
3111 s->pb.buf_ptr= s->ptr_lastgob;
3112 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
3116 #if FF_API_RTP_CALLBACK
3117 FF_DISABLE_DEPRECATION_WARNINGS
3118 if (s->avctx->rtp_callback){
3119 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
3120 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
3122 FF_ENABLE_DEPRECATION_WARNINGS
3124 update_mb_info(s, 1);
3126 switch(s->codec_id){
3127 case AV_CODEC_ID_MPEG4:
3128 if (CONFIG_MPEG4_ENCODER) {
3129 ff_mpeg4_encode_video_packet_header(s);
3130 ff_mpeg4_clean_buffers(s);
3133 case AV_CODEC_ID_MPEG1VIDEO:
3134 case AV_CODEC_ID_MPEG2VIDEO:
3135 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3136 ff_mpeg1_encode_slice_header(s);
3137 ff_mpeg1_clean_buffers(s);
3140 case AV_CODEC_ID_H263:
3141 case AV_CODEC_ID_H263P:
3142 if (CONFIG_H263_ENCODER)
3143 ff_h263_encode_gob_header(s, mb_y);
3147 if (s->avctx->flags & AV_CODEC_FLAG_PASS1) {
3148 int bits= put_bits_count(&s->pb);
3149 s->misc_bits+= bits - s->last_bits;
3153 s->ptr_lastgob += current_packet_size;
3154 s->first_slice_line=1;
3155 s->resync_mb_x=mb_x;
3156 s->resync_mb_y=mb_y;
3160 if( (s->resync_mb_x == s->mb_x)
3161 && s->resync_mb_y+1 == s->mb_y){
3162 s->first_slice_line=0;
3166 s->dquant=0; //only for QP_RD
3168 update_mb_info(s, 0);
3170 if (mb_type & (mb_type-1) || (s->mpv_flags & FF_MPV_FLAG_QP_RD)) { // more than 1 MB type possible or FF_MPV_FLAG_QP_RD
3172 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3174 copy_context_before_encode(&backup_s, s, -1);
3176 best_s.data_partitioning= s->data_partitioning;
3177 best_s.partitioned_frame= s->partitioned_frame;
3178 if(s->data_partitioning){
3179 backup_s.pb2= s->pb2;
3180 backup_s.tex_pb= s->tex_pb;
3183 if(mb_type&CANDIDATE_MB_TYPE_INTER){
3184 s->mv_dir = MV_DIR_FORWARD;
3185 s->mv_type = MV_TYPE_16X16;
3187 s->mv[0][0][0] = s->p_mv_table[xy][0];
3188 s->mv[0][0][1] = s->p_mv_table[xy][1];
3189 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
3190 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3192 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
3193 s->mv_dir = MV_DIR_FORWARD;
3194 s->mv_type = MV_TYPE_FIELD;
3197 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3198 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3199 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3201 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
3202 &dmin, &next_block, 0, 0);
3204 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
3205 s->mv_dir = MV_DIR_FORWARD;
3206 s->mv_type = MV_TYPE_16X16;
3210 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3211 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3213 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
3214 s->mv_dir = MV_DIR_FORWARD;
3215 s->mv_type = MV_TYPE_8X8;
3218 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3219 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3221 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3222 &dmin, &next_block, 0, 0);
3224 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
3225 s->mv_dir = MV_DIR_FORWARD;
3226 s->mv_type = MV_TYPE_16X16;
3228 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3229 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3230 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3231 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3233 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
3234 s->mv_dir = MV_DIR_BACKWARD;
3235 s->mv_type = MV_TYPE_16X16;
3237 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3238 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3239 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3240 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3242 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
3243 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3244 s->mv_type = MV_TYPE_16X16;
3246 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3247 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3248 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3249 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3250 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3251 &dmin, &next_block, 0, 0);
3253 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
3254 s->mv_dir = MV_DIR_FORWARD;
3255 s->mv_type = MV_TYPE_FIELD;
3258 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3259 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3260 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3262 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3263 &dmin, &next_block, 0, 0);
3265 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
3266 s->mv_dir = MV_DIR_BACKWARD;
3267 s->mv_type = MV_TYPE_FIELD;
3270 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3271 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3272 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3274 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3275 &dmin, &next_block, 0, 0);
3277 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
3278 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3279 s->mv_type = MV_TYPE_FIELD;
3281 for(dir=0; dir<2; dir++){
3283 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3284 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3285 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3288 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3289 &dmin, &next_block, 0, 0);
3291 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
3293 s->mv_type = MV_TYPE_16X16;
3297 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3298 &dmin, &next_block, 0, 0);
3299 if(s->h263_pred || s->h263_aic){
3301 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3303 ff_clean_intra_table_entries(s); //old mode?
3307 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3308 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
3309 const int last_qp= backup_s.qscale;
3312 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
3313 static const int dquant_tab[4]={-1,1,-2,2};
3314 int storecoefs = s->mb_intra && s->dc_val[0];
3316 av_assert2(backup_s.dquant == 0);
3319 s->mv_dir= best_s.mv_dir;
3320 s->mv_type = MV_TYPE_16X16;
3321 s->mb_intra= best_s.mb_intra;
3322 s->mv[0][0][0] = best_s.mv[0][0][0];
3323 s->mv[0][0][1] = best_s.mv[0][0][1];
3324 s->mv[1][0][0] = best_s.mv[1][0][0];
3325 s->mv[1][0][1] = best_s.mv[1][0][1];
3327 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3328 for(; qpi<4; qpi++){
3329 int dquant= dquant_tab[qpi];
3330 qp= last_qp + dquant;
3331 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
3333 backup_s.dquant= dquant;
3336 dc[i]= s->dc_val[0][ s->block_index[i] ];
3337 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
3341 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3342 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
3343 if(best_s.qscale != qp){
3346 s->dc_val[0][ s->block_index[i] ]= dc[i];
3347 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
3354 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3355 int mx= s->b_direct_mv_table[xy][0];
3356 int my= s->b_direct_mv_table[xy][1];
3358 backup_s.dquant = 0;
3359 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3361 ff_mpeg4_set_direct_mv(s, mx, my);
3362 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3363 &dmin, &next_block, mx, my);
3365 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3366 backup_s.dquant = 0;
3367 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3369 ff_mpeg4_set_direct_mv(s, 0, 0);
3370 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3371 &dmin, &next_block, 0, 0);
3373 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3376 coded |= s->block_last_index[i];
3379 memcpy(s->mv, best_s.mv, sizeof(s->mv));
3380 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
3381 mx=my=0; //FIXME find the one we actually used
3382 ff_mpeg4_set_direct_mv(s, mx, my);
3383 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
3391 s->mv_dir= best_s.mv_dir;
3392 s->mv_type = best_s.mv_type;
3394 /* s->mv[0][0][0] = best_s.mv[0][0][0];
3395 s->mv[0][0][1] = best_s.mv[0][0][1];
3396 s->mv[1][0][0] = best_s.mv[1][0][0];
3397 s->mv[1][0][1] = best_s.mv[1][0][1];*/
3400 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3401 &dmin, &next_block, mx, my);
3406 s->current_picture.qscale_table[xy] = best_s.qscale;
3408 copy_context_after_encode(s, &best_s, -1);
3410 pb_bits_count= put_bits_count(&s->pb);
3411 flush_put_bits(&s->pb);
3412 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3415 if(s->data_partitioning){
3416 pb2_bits_count= put_bits_count(&s->pb2);
3417 flush_put_bits(&s->pb2);
3418 avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3419 s->pb2= backup_s.pb2;
3421 tex_pb_bits_count= put_bits_count(&s->tex_pb);
3422 flush_put_bits(&s->tex_pb);
3423 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3424 s->tex_pb= backup_s.tex_pb;
3426 s->last_bits= put_bits_count(&s->pb);
3428 if (CONFIG_H263_ENCODER &&
3429 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3430 ff_h263_update_motion_val(s);
3432 if(next_block==0){ //FIXME 16 vs linesize16
3433 s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
3434 s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
3435 s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
3438 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
3439 ff_mpv_decode_mb(s, s->block);
3441 int motion_x = 0, motion_y = 0;
3442 s->mv_type=MV_TYPE_16X16;
3443 // only one MB-Type possible
3446 case CANDIDATE_MB_TYPE_INTRA:
3449 motion_x= s->mv[0][0][0] = 0;
3450 motion_y= s->mv[0][0][1] = 0;
3452 case CANDIDATE_MB_TYPE_INTER:
3453 s->mv_dir = MV_DIR_FORWARD;
3455 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3456 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3458 case CANDIDATE_MB_TYPE_INTER_I:
3459 s->mv_dir = MV_DIR_FORWARD;
3460 s->mv_type = MV_TYPE_FIELD;
3463 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3464 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3465 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3468 case CANDIDATE_MB_TYPE_INTER4V:
3469 s->mv_dir = MV_DIR_FORWARD;
3470 s->mv_type = MV_TYPE_8X8;
3473 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3474 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3477 case CANDIDATE_MB_TYPE_DIRECT:
3478 if (CONFIG_MPEG4_ENCODER) {
3479 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3481 motion_x=s->b_direct_mv_table[xy][0];
3482 motion_y=s->b_direct_mv_table[xy][1];
3483 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3486 case CANDIDATE_MB_TYPE_DIRECT0:
3487 if (CONFIG_MPEG4_ENCODER) {
3488 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3490 ff_mpeg4_set_direct_mv(s, 0, 0);
3493 case CANDIDATE_MB_TYPE_BIDIR:
3494 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3496 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3497 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3498 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3499 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3501 case CANDIDATE_MB_TYPE_BACKWARD:
3502 s->mv_dir = MV_DIR_BACKWARD;
3504 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3505 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3507 case CANDIDATE_MB_TYPE_FORWARD:
3508 s->mv_dir = MV_DIR_FORWARD;
3510 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3511 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3513 case CANDIDATE_MB_TYPE_FORWARD_I:
3514 s->mv_dir = MV_DIR_FORWARD;
3515 s->mv_type = MV_TYPE_FIELD;
3518 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3519 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3520 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3523 case CANDIDATE_MB_TYPE_BACKWARD_I:
3524 s->mv_dir = MV_DIR_BACKWARD;
3525 s->mv_type = MV_TYPE_FIELD;
3528 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3529 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3530 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3533 case CANDIDATE_MB_TYPE_BIDIR_I:
3534 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3535 s->mv_type = MV_TYPE_FIELD;
3537 for(dir=0; dir<2; dir++){
3539 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3540 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3541 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3546 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3549 encode_mb(s, motion_x, motion_y);
3551 // RAL: Update last macroblock type
3552 s->last_mv_dir = s->mv_dir;
3554 if (CONFIG_H263_ENCODER &&
3555 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3556 ff_h263_update_motion_val(s);
3558 ff_mpv_decode_mb(s, s->block);
3561 /* clean the MV table in IPS frames for direct mode in B-frames */
3562 if(s->mb_intra /* && I,P,S_TYPE */){
3563 s->p_mv_table[xy][0]=0;
3564 s->p_mv_table[xy][1]=0;
3567 if (s->avctx->flags & AV_CODEC_FLAG_PSNR) {
3571 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3572 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3574 s->current_picture.encoding_error[0] += sse(
3575 s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3576 s->dest[0], w, h, s->linesize);
3577 s->current_picture.encoding_error[1] += sse(
3578 s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3579 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3580 s->current_picture.encoding_error[2] += sse(
3581 s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3582 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3585 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3586 ff_h263_loop_filter(s);
3588 ff_dlog(s->avctx, "MB %d %d bits\n",
3589 s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3593 //not beautiful here but we must write it before flushing so it has to be here
3594 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3595 ff_msmpeg4_encode_ext_header(s);
3599 #if FF_API_RTP_CALLBACK
3600 FF_DISABLE_DEPRECATION_WARNINGS
3601 /* Send the last GOB if RTP */
3602 if (s->avctx->rtp_callback) {
3603 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3604 int pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3605 /* Call the RTP callback to send the last GOB */
3607 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3609 FF_ENABLE_DEPRECATION_WARNINGS
3615 #define MERGE(field) dst->field += src->field; src->field=0
3616 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3617 MERGE(me.scene_change_score);
3618 MERGE(me.mc_mb_var_sum_temp);
3619 MERGE(me.mb_var_sum_temp);
3622 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3625 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3626 MERGE(dct_count[1]);
3635 MERGE(er.error_count);
3636 MERGE(padding_bug_score);
3637 MERGE(current_picture.encoding_error[0]);
3638 MERGE(current_picture.encoding_error[1]);
3639 MERGE(current_picture.encoding_error[2]);
3641 if (dst->noise_reduction){
3642 for(i=0; i<64; i++){
3643 MERGE(dct_error_sum[0][i]);
3644 MERGE(dct_error_sum[1][i]);
3648 assert(put_bits_count(&src->pb) % 8 ==0);
3649 assert(put_bits_count(&dst->pb) % 8 ==0);
3650 avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3651 flush_put_bits(&dst->pb);
3654 static int estimate_qp(MpegEncContext *s, int dry_run){
3655 if (s->next_lambda){
3656 s->current_picture_ptr->f->quality =
3657 s->current_picture.f->quality = s->next_lambda;
3658 if(!dry_run) s->next_lambda= 0;
3659 } else if (!s->fixed_qscale) {
3662 if ((s->avctx->flags & AV_CODEC_FLAG_PASS2) && s->rc_strategy == MPV_RC_STRATEGY_XVID)
3663 quality = ff_xvid_rate_estimate_qscale(s, dry_run);
3666 quality = ff_rate_estimate_qscale(s, dry_run);
3667 s->current_picture_ptr->f->quality =
3668 s->current_picture.f->quality = quality;
3669 if (s->current_picture.f->quality < 0)
3673 if(s->adaptive_quant){
3674 switch(s->codec_id){
3675 case AV_CODEC_ID_MPEG4:
3676 if (CONFIG_MPEG4_ENCODER)
3677 ff_clean_mpeg4_qscales(s);
3679 case AV_CODEC_ID_H263:
3680 case AV_CODEC_ID_H263P:
3681 case AV_CODEC_ID_FLV1:
3682 if (CONFIG_H263_ENCODER)
3683 ff_clean_h263_qscales(s);
3686 ff_init_qscale_tab(s);
3689 s->lambda= s->lambda_table[0];
3692 s->lambda = s->current_picture.f->quality;
3697 /* must be called before writing the header */
3698 static void set_frame_distances(MpegEncContext * s){
3699 av_assert1(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
3700 s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3702 if(s->pict_type==AV_PICTURE_TYPE_B){
3703 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3704 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3706 s->pp_time= s->time - s->last_non_b_time;
3707 s->last_non_b_time= s->time;
3708 assert(s->picture_number==0 || s->pp_time > 0);
3712 static int encode_picture(MpegEncContext *s, int picture_number)
3716 int context_count = s->slice_context_count;
3718 s->picture_number = picture_number;
3720 /* Reset the average MB variance */
3721 s->me.mb_var_sum_temp =
3722 s->me.mc_mb_var_sum_temp = 0;
3724 /* we need to initialize some time vars before we can encode B-frames */
3725 // RAL: Condition added for MPEG1VIDEO
3726 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3727 set_frame_distances(s);
3728 if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3729 ff_set_mpeg4_time(s);
3731 s->me.scene_change_score=0;
3733 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3735 if(s->pict_type==AV_PICTURE_TYPE_I){
3736 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3737 else s->no_rounding=0;
3738 }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3739 if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3740 s->no_rounding ^= 1;
3743 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
3744 if (estimate_qp(s,1) < 0)
3746 ff_get_2pass_fcode(s);
3747 } else if (!(s->avctx->flags & AV_CODEC_FLAG_QSCALE)) {
3748 if(s->pict_type==AV_PICTURE_TYPE_B)
3749 s->lambda= s->last_lambda_for[s->pict_type];
3751 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3755 if(s->codec_id != AV_CODEC_ID_AMV && s->codec_id != AV_CODEC_ID_MJPEG){
3756 if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
3757 if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
3758 s->q_chroma_intra_matrix = s->q_intra_matrix;
3759 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
3762 s->mb_intra=0; //for the rate distortion & bit compare functions
3763 for(i=1; i<context_count; i++){
3764 ret = ff_update_duplicate_context(s->thread_context[i], s);
3772 /* Estimate motion for every MB */
3773 if(s->pict_type != AV_PICTURE_TYPE_I){
3774 s->lambda = (s->lambda * s->me_penalty_compensation + 128) >> 8;
3775 s->lambda2 = (s->lambda2 * (int64_t) s->me_penalty_compensation + 128) >> 8;
3776 if (s->pict_type != AV_PICTURE_TYPE_B) {
3777 if ((s->me_pre && s->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
3779 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3783 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3784 }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3786 for(i=0; i<s->mb_stride*s->mb_height; i++)
3787 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3789 if(!s->fixed_qscale){
3790 /* finding spatial complexity for I-frame rate control */
3791 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3794 for(i=1; i<context_count; i++){
3795 merge_context_after_me(s, s->thread_context[i]);
3797 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3798 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
3801 if (s->me.scene_change_score > s->scenechange_threshold &&
3802 s->pict_type == AV_PICTURE_TYPE_P) {
3803 s->pict_type= AV_PICTURE_TYPE_I;
3804 for(i=0; i<s->mb_stride*s->mb_height; i++)
3805 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3806 if(s->msmpeg4_version >= 3)
3808 ff_dlog(s, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
3809 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3813 if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3814 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3816 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3818 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3819 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3820 s->f_code= FFMAX3(s->f_code, a, b);
3823 ff_fix_long_p_mvs(s);
3824 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3825 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3829 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3830 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3835 if(s->pict_type==AV_PICTURE_TYPE_B){
3838 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3839 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3840 s->f_code = FFMAX(a, b);
3842 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3843 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3844 s->b_code = FFMAX(a, b);
3846 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3847 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3848 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3849 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3850 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3852 for(dir=0; dir<2; dir++){
3855 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3856 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3857 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3858 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3866 if (estimate_qp(s, 0) < 0)
3869 if (s->qscale < 3 && s->max_qcoeff <= 128 &&
3870 s->pict_type == AV_PICTURE_TYPE_I &&
3871 !(s->avctx->flags & AV_CODEC_FLAG_QSCALE))
3872 s->qscale= 3; //reduce clipping problems
3874 if (s->out_format == FMT_MJPEG) {
3875 const uint16_t * luma_matrix = ff_mpeg1_default_intra_matrix;
3876 const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
3878 if (s->avctx->intra_matrix) {
3880 luma_matrix = s->avctx->intra_matrix;
3882 if (s->avctx->chroma_intra_matrix)
3883 chroma_matrix = s->avctx->chroma_intra_matrix;
3885 /* for mjpeg, we do include qscale in the matrix */
3887 int j = s->idsp.idct_permutation[i];
3889 s->chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->qscale) >> 3);
3890 s-> intra_matrix[j] = av_clip_uint8(( luma_matrix[i] * s->qscale) >> 3);
3892 s->y_dc_scale_table=
3893 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3894 s->chroma_intra_matrix[0] =
3895 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3896 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3897 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3898 ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3899 s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3902 if(s->codec_id == AV_CODEC_ID_AMV){
3903 static const uint8_t y[32]={13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
3904 static const uint8_t c[32]={14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14};
3906 int j= s->idsp.idct_permutation[ff_zigzag_direct[i]];
3908 s->intra_matrix[j] = sp5x_quant_table[5*2+0][i];
3909 s->chroma_intra_matrix[j] = sp5x_quant_table[5*2+1][i];
3911 s->y_dc_scale_table= y;
3912 s->c_dc_scale_table= c;
3913 s->intra_matrix[0] = 13;
3914 s->chroma_intra_matrix[0] = 14;
3915 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3916 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3917 ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3918 s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3922 //FIXME var duplication
3923 s->current_picture_ptr->f->key_frame =
3924 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3925 s->current_picture_ptr->f->pict_type =
3926 s->current_picture.f->pict_type = s->pict_type;
3928 if (s->current_picture.f->key_frame)
3929 s->picture_in_gop_number=0;
3931 s->mb_x = s->mb_y = 0;
3932 s->last_bits= put_bits_count(&s->pb);
3933 switch(s->out_format) {
3935 if (CONFIG_MJPEG_ENCODER)
3936 ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
3937 s->pred, s->intra_matrix, s->chroma_intra_matrix);
3940 if (CONFIG_H261_ENCODER)
3941 ff_h261_encode_picture_header(s, picture_number);
3944 if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3945 ff_wmv2_encode_picture_header(s, picture_number);
3946 else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3947 ff_msmpeg4_encode_picture_header(s, picture_number);
3948 else if (CONFIG_MPEG4_ENCODER && s->h263_pred) {
3949 ret = ff_mpeg4_encode_picture_header(s, picture_number);
3952 } else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
3953 ret = ff_rv10_encode_picture_header(s, picture_number);
3957 else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3958 ff_rv20_encode_picture_header(s, picture_number);
3959 else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3960 ff_flv_encode_picture_header(s, picture_number);
3961 else if (CONFIG_H263_ENCODER)
3962 ff_h263_encode_picture_header(s, picture_number);
3965 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3966 ff_mpeg1_encode_picture_header(s, picture_number);
3971 bits= put_bits_count(&s->pb);
3972 s->header_bits= bits - s->last_bits;
3974 for(i=1; i<context_count; i++){
3975 update_duplicate_context_after_me(s->thread_context[i], s);
3977 s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3978 for(i=1; i<context_count; i++){
3979 if (s->pb.buf_end == s->thread_context[i]->pb.buf)
3980 set_put_bits_buffer_size(&s->pb, FFMIN(s->thread_context[i]->pb.buf_end - s->pb.buf, INT_MAX/8-32));
3981 merge_context_after_encode(s, s->thread_context[i]);
3987 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3988 const int intra= s->mb_intra;
3991 s->dct_count[intra]++;
3993 for(i=0; i<64; i++){
3994 int level= block[i];
3998 s->dct_error_sum[intra][i] += level;
3999 level -= s->dct_offset[intra][i];
4000 if(level<0) level=0;
4002 s->dct_error_sum[intra][i] -= level;
4003 level += s->dct_offset[intra][i];
4004 if(level>0) level=0;
4011 static int dct_quantize_trellis_c(MpegEncContext *s,
4012 int16_t *block, int n,
4013 int qscale, int *overflow){
4015 const uint16_t *matrix;
4016 const uint8_t *scantable= s->intra_scantable.scantable;
4017 const uint8_t *perm_scantable= s->intra_scantable.permutated;
4019 unsigned int threshold1, threshold2;
4031 int coeff_count[64];
4032 int qmul, qadd, start_i, last_non_zero, i, dc;
4033 const int esc_length= s->ac_esc_length;
4035 uint8_t * last_length;
4036 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
4039 s->fdsp.fdct(block);
4041 if(s->dct_error_sum)
4042 s->denoise_dct(s, block);
4044 qadd= ((qscale-1)|1)*8;
4046 if (s->q_scale_type) mpeg2_qscale = ff_mpeg2_non_linear_qscale[qscale];
4047 else mpeg2_qscale = qscale << 1;
4058 /* For AIC we skip quant/dequant of INTRADC */
4063 /* note: block[0] is assumed to be positive */
4064 block[0] = (block[0] + (q >> 1)) / q;
4067 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
4068 matrix = n < 4 ? s->intra_matrix : s->chroma_intra_matrix;
4069 if(s->mpeg_quant || s->out_format == FMT_MPEG1 || s->out_format == FMT_MJPEG)
4070 bias= 1<<(QMAT_SHIFT-1);
4072 if (n > 3 && s->intra_chroma_ac_vlc_length) {
4073 length = s->intra_chroma_ac_vlc_length;
4074 last_length= s->intra_chroma_ac_vlc_last_length;
4076 length = s->intra_ac_vlc_length;
4077 last_length= s->intra_ac_vlc_last_length;
4082 qmat = s->q_inter_matrix[qscale];
4083 matrix = s->inter_matrix;
4084 length = s->inter_ac_vlc_length;
4085 last_length= s->inter_ac_vlc_last_length;
4089 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4090 threshold2= (threshold1<<1);
4092 for(i=63; i>=start_i; i--) {
4093 const int j = scantable[i];
4094 int level = block[j] * qmat[j];
4096 if(((unsigned)(level+threshold1))>threshold2){
4102 for(i=start_i; i<=last_non_zero; i++) {
4103 const int j = scantable[i];
4104 int level = block[j] * qmat[j];
4106 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
4107 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
4108 if(((unsigned)(level+threshold1))>threshold2){
4110 level= (bias + level)>>QMAT_SHIFT;
4112 coeff[1][i]= level-1;
4113 // coeff[2][k]= level-2;
4115 level= (bias - level)>>QMAT_SHIFT;
4116 coeff[0][i]= -level;
4117 coeff[1][i]= -level+1;
4118 // coeff[2][k]= -level+2;
4120 coeff_count[i]= FFMIN(level, 2);
4121 av_assert2(coeff_count[i]);
4124 coeff[0][i]= (level>>31)|1;
4129 *overflow= s->max_qcoeff < max; //overflow might have happened
4131 if(last_non_zero < start_i){
4132 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4133 return last_non_zero;
4136 score_tab[start_i]= 0;
4137 survivor[0]= start_i;
4140 for(i=start_i; i<=last_non_zero; i++){
4141 int level_index, j, zero_distortion;
4142 int dct_coeff= FFABS(block[ scantable[i] ]);
4143 int best_score=256*256*256*120;
4145 if (s->fdsp.fdct == ff_fdct_ifast)
4146 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
4147 zero_distortion= dct_coeff*dct_coeff;
4149 for(level_index=0; level_index < coeff_count[i]; level_index++){
4151 int level= coeff[level_index][i];
4152 const int alevel= FFABS(level);
4157 if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4158 unquant_coeff= alevel*qmul + qadd;
4159 } else if(s->out_format == FMT_MJPEG) {
4160 j = s->idsp.idct_permutation[scantable[i]];
4161 unquant_coeff = alevel * matrix[j] * 8;
4163 j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
4165 unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4166 unquant_coeff = (unquant_coeff - 1) | 1;
4168 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4169 unquant_coeff = (unquant_coeff - 1) | 1;
4174 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4176 if((level&(~127)) == 0){
4177 for(j=survivor_count-1; j>=0; j--){
4178 int run= i - survivor[j];
4179 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4180 score += score_tab[i-run];
4182 if(score < best_score){
4185 level_tab[i+1]= level-64;
4189 if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4190 for(j=survivor_count-1; j>=0; j--){
4191 int run= i - survivor[j];
4192 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4193 score += score_tab[i-run];
4194 if(score < last_score){
4197 last_level= level-64;
4203 distortion += esc_length*lambda;
4204 for(j=survivor_count-1; j>=0; j--){
4205 int run= i - survivor[j];
4206 int score= distortion + score_tab[i-run];
4208 if(score < best_score){
4211 level_tab[i+1]= level-64;
4215 if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4216 for(j=survivor_count-1; j>=0; j--){
4217 int run= i - survivor[j];
4218 int score= distortion + score_tab[i-run];
4219 if(score < last_score){
4222 last_level= level-64;
4230 score_tab[i+1]= best_score;
4232 // Note: there is a vlc code in MPEG-4 which is 1 bit shorter then another one with a shorter run and the same level
4233 if(last_non_zero <= 27){
4234 for(; survivor_count; survivor_count--){
4235 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4239 for(; survivor_count; survivor_count--){
4240 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4245 survivor[ survivor_count++ ]= i+1;
4248 if(s->out_format != FMT_H263 && s->out_format != FMT_H261){
4249 last_score= 256*256*256*120;
4250 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4251 int score= score_tab[i];
4253 score += lambda * 2; // FIXME more exact?
4255 if(score < last_score){
4258 last_level= level_tab[i];
4259 last_run= run_tab[i];
4264 s->coded_score[n] = last_score;
4266 dc= FFABS(block[0]);
4267 last_non_zero= last_i - 1;
4268 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4270 if(last_non_zero < start_i)
4271 return last_non_zero;
4273 if(last_non_zero == 0 && start_i == 0){
4275 int best_score= dc * dc;
4277 for(i=0; i<coeff_count[0]; i++){
4278 int level= coeff[i][0];
4279 int alevel= FFABS(level);
4280 int unquant_coeff, score, distortion;
4282 if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4283 unquant_coeff= (alevel*qmul + qadd)>>3;
4285 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4286 unquant_coeff = (unquant_coeff - 1) | 1;
4288 unquant_coeff = (unquant_coeff + 4) >> 3;
4289 unquant_coeff<<= 3 + 3;
4291 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
4293 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
4294 else score= distortion + esc_length*lambda;
4296 if(score < best_score){
4298 best_level= level - 64;
4301 block[0]= best_level;
4302 s->coded_score[n] = best_score - dc*dc;
4303 if(best_level == 0) return -1;
4304 else return last_non_zero;
4308 av_assert2(last_level);
4310 block[ perm_scantable[last_non_zero] ]= last_level;
4313 for(; i>start_i; i -= run_tab[i] + 1){
4314 block[ perm_scantable[i-1] ]= level_tab[i];
4317 return last_non_zero;
4320 //#define REFINE_STATS 1
4321 static int16_t basis[64][64];
4323 static void build_basis(uint8_t *perm){
4330 double s= 0.25*(1<<BASIS_SHIFT);
4332 int perm_index= perm[index];
4333 if(i==0) s*= sqrt(0.5);
4334 if(j==0) s*= sqrt(0.5);
4335 basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
4342 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
4343 int16_t *block, int16_t *weight, int16_t *orig,
4346 LOCAL_ALIGNED_16(int16_t, d1, [64]);
4347 const uint8_t *scantable= s->intra_scantable.scantable;
4348 const uint8_t *perm_scantable= s->intra_scantable.permutated;
4349 // unsigned int threshold1, threshold2;
4354 int qmul, qadd, start_i, last_non_zero, i, dc;
4356 uint8_t * last_length;
4358 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
4361 static int after_last=0;
4362 static int to_zero=0;
4363 static int from_zero=0;
4366 static int messed_sign=0;
4369 if(basis[0][0] == 0)
4370 build_basis(s->idsp.idct_permutation);
4381 /* For AIC we skip quant/dequant of INTRADC */
4385 q <<= RECON_SHIFT-3;
4386 /* note: block[0] is assumed to be positive */
4388 // block[0] = (block[0] + (q >> 1)) / q;
4390 // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
4391 // bias= 1<<(QMAT_SHIFT-1);
4392 if (n > 3 && s->intra_chroma_ac_vlc_length) {
4393 length = s->intra_chroma_ac_vlc_length;
4394 last_length= s->intra_chroma_ac_vlc_last_length;
4396 length = s->intra_ac_vlc_length;
4397 last_length= s->intra_ac_vlc_last_length;
4402 length = s->inter_ac_vlc_length;
4403 last_length= s->inter_ac_vlc_last_length;
4405 last_non_zero = s->block_last_index[n];
4410 dc += (1<<(RECON_SHIFT-1));
4411 for(i=0; i<64; i++){
4412 rem[i] = dc - (orig[i] << RECON_SHIFT); // FIXME use orig directly instead of copying to rem[]
4415 STOP_TIMER("memset rem[]")}
4418 for(i=0; i<64; i++){
4423 w= FFABS(weight[i]) + qns*one;
4424 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
4427 // w=weight[i] = (63*qns + (w/2)) / w;
4430 av_assert2(w<(1<<6));
4433 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
4439 for(i=start_i; i<=last_non_zero; i++){
4440 int j= perm_scantable[i];
4441 const int level= block[j];
4445 if(level<0) coeff= qmul*level - qadd;
4446 else coeff= qmul*level + qadd;
4447 run_tab[rle_index++]=run;
4450 s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
4456 if(last_non_zero>0){
4457 STOP_TIMER("init rem[]")
4464 int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
4467 int run2, best_unquant_change=0, analyze_gradient;
4471 analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
4473 if(analyze_gradient){
4477 for(i=0; i<64; i++){
4480 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
4483 STOP_TIMER("rem*w*w")}
4493 const int level= block[0];
4494 int change, old_coeff;
4496 av_assert2(s->mb_intra);
4500 for(change=-1; change<=1; change+=2){
4501 int new_level= level + change;
4502 int score, new_coeff;
4504 new_coeff= q*new_level;
4505 if(new_coeff >= 2048 || new_coeff < 0)
4508 score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
4509 new_coeff - old_coeff);
4510 if(score<best_score){
4513 best_change= change;
4514 best_unquant_change= new_coeff - old_coeff;
4521 run2= run_tab[rle_index++];
4525 for(i=start_i; i<64; i++){
4526 int j= perm_scantable[i];
4527 const int level= block[j];
4528 int change, old_coeff;
4530 if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4534 if(level<0) old_coeff= qmul*level - qadd;
4535 else old_coeff= qmul*level + qadd;
4536 run2= run_tab[rle_index++]; //FIXME ! maybe after last
4540 av_assert2(run2>=0 || i >= last_non_zero );
4543 for(change=-1; change<=1; change+=2){
4544 int new_level= level + change;
4545 int score, new_coeff, unquant_change;
4548 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4552 if(new_level<0) new_coeff= qmul*new_level - qadd;
4553 else new_coeff= qmul*new_level + qadd;
4554 if(new_coeff >= 2048 || new_coeff <= -2048)
4556 //FIXME check for overflow
4559 if(level < 63 && level > -63){
4560 if(i < last_non_zero)
4561 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
4562 - length[UNI_AC_ENC_INDEX(run, level+64)];
4564 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4565 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4568 av_assert2(FFABS(new_level)==1);
4570 if(analyze_gradient){
4571 int g= d1[ scantable[i] ];
4572 if(g && (g^new_level) >= 0)
4576 if(i < last_non_zero){
4577 int next_i= i + run2 + 1;
4578 int next_level= block[ perm_scantable[next_i] ] + 64;
4580 if(next_level&(~127))
4583 if(next_i < last_non_zero)
4584 score += length[UNI_AC_ENC_INDEX(run, 65)]
4585 + length[UNI_AC_ENC_INDEX(run2, next_level)]
4586 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4588 score += length[UNI_AC_ENC_INDEX(run, 65)]
4589 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4590 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4592 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4594 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4595 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4601 av_assert2(FFABS(level)==1);
4603 if(i < last_non_zero){
4604 int next_i= i + run2 + 1;
4605 int next_level= block[ perm_scantable[next_i] ] + 64;
4607 if(next_level&(~127))
4610 if(next_i < last_non_zero)
4611 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4612 - length[UNI_AC_ENC_INDEX(run2, next_level)]
4613 - length[UNI_AC_ENC_INDEX(run, 65)];
4615 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4616 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4617 - length[UNI_AC_ENC_INDEX(run, 65)];
4619 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4621 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4622 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4629 unquant_change= new_coeff - old_coeff;
4630 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4632 score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4634 if(score<best_score){
4637 best_change= change;
4638 best_unquant_change= unquant_change;
4642 prev_level= level + 64;
4643 if(prev_level&(~127))
4652 STOP_TIMER("iterative step")}
4656 int j= perm_scantable[ best_coeff ];
4658 block[j] += best_change;
4660 if(best_coeff > last_non_zero){
4661 last_non_zero= best_coeff;
4662 av_assert2(block[j]);
4669 if(block[j] - best_change){
4670 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
4682 for(; last_non_zero>=start_i; last_non_zero--){
4683 if(block[perm_scantable[last_non_zero]])
4689 if(256*256*256*64 % count == 0){
4690 av_log(s->avctx, AV_LOG_DEBUG, "after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
4695 for(i=start_i; i<=last_non_zero; i++){
4696 int j= perm_scantable[i];
4697 const int level= block[j];
4700 run_tab[rle_index++]=run;
4707 s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4713 if(last_non_zero>0){
4714 STOP_TIMER("iterative search")
4719 return last_non_zero;
4723 * Permute an 8x8 block according to permutation.
4724 * @param block the block which will be permuted according to
4725 * the given permutation vector
4726 * @param permutation the permutation vector
4727 * @param last the last non zero coefficient in scantable order, used to
4728 * speed the permutation up
4729 * @param scantable the used scantable, this is only used to speed the
4730 * permutation up, the block is not (inverse) permutated
4731 * to scantable order!
4733 void ff_block_permute(int16_t *block, uint8_t *permutation,
4734 const uint8_t *scantable, int last)
4741 //FIXME it is ok but not clean and might fail for some permutations
4742 // if (permutation[1] == 1)
4745 for (i = 0; i <= last; i++) {
4746 const int j = scantable[i];
4751 for (i = 0; i <= last; i++) {
4752 const int j = scantable[i];
4753 const int perm_j = permutation[j];
4754 block[perm_j] = temp[j];
4758 int ff_dct_quantize_c(MpegEncContext *s,
4759 int16_t *block, int n,
4760 int qscale, int *overflow)
4762 int i, j, level, last_non_zero, q, start_i;
4764 const uint8_t *scantable= s->intra_scantable.scantable;
4767 unsigned int threshold1, threshold2;
4769 s->fdsp.fdct(block);
4771 if(s->dct_error_sum)
4772 s->denoise_dct(s, block);
4782 /* For AIC we skip quant/dequant of INTRADC */
4785 /* note: block[0] is assumed to be positive */
4786 block[0] = (block[0] + (q >> 1)) / q;
4789 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
4790 bias= s->intra_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4794 qmat = s->q_inter_matrix[qscale];
4795 bias= s->inter_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4797 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4798 threshold2= (threshold1<<1);
4799 for(i=63;i>=start_i;i--) {
4801 level = block[j] * qmat[j];
4803 if(((unsigned)(level+threshold1))>threshold2){
4810 for(i=start_i; i<=last_non_zero; i++) {
4812 level = block[j] * qmat[j];
4814 // if( bias+level >= (1<<QMAT_SHIFT)
4815 // || bias-level >= (1<<QMAT_SHIFT)){
4816 if(((unsigned)(level+threshold1))>threshold2){
4818 level= (bias + level)>>QMAT_SHIFT;
4821 level= (bias - level)>>QMAT_SHIFT;
4829 *overflow= s->max_qcoeff < max; //overflow might have happened
4831 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4832 if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
4833 ff_block_permute(block, s->idsp.idct_permutation,
4834 scantable, last_non_zero);
4836 return last_non_zero;
4839 #define OFFSET(x) offsetof(MpegEncContext, x)
4840 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4841 static const AVOption h263_options[] = {
4842 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
4843 { "mb_info", "emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size", OFFSET(mb_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
4848 static const AVClass h263_class = {
4849 .class_name = "H.263 encoder",
4850 .item_name = av_default_item_name,
4851 .option = h263_options,
4852 .version = LIBAVUTIL_VERSION_INT,
4855 AVCodec ff_h263_encoder = {
4857 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4858 .type = AVMEDIA_TYPE_VIDEO,
4859 .id = AV_CODEC_ID_H263,
4860 .priv_data_size = sizeof(MpegEncContext),
4861 .init = ff_mpv_encode_init,
4862 .encode2 = ff_mpv_encode_picture,
4863 .close = ff_mpv_encode_end,
4864 .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4865 .priv_class = &h263_class,
4868 static const AVOption h263p_options[] = {
4869 { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
4870 { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
4871 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
4872 { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE},
4876 static const AVClass h263p_class = {
4877 .class_name = "H.263p encoder",
4878 .item_name = av_default_item_name,
4879 .option = h263p_options,
4880 .version = LIBAVUTIL_VERSION_INT,
4883 AVCodec ff_h263p_encoder = {
4885 .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4886 .type = AVMEDIA_TYPE_VIDEO,
4887 .id = AV_CODEC_ID_H263P,
4888 .priv_data_size = sizeof(MpegEncContext),
4889 .init = ff_mpv_encode_init,
4890 .encode2 = ff_mpv_encode_picture,
4891 .close = ff_mpv_encode_end,
4892 .capabilities = AV_CODEC_CAP_SLICE_THREADS,
4893 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4894 .priv_class = &h263p_class,
4897 static const AVClass msmpeg4v2_class = {
4898 .class_name = "msmpeg4v2 encoder",
4899 .item_name = av_default_item_name,
4900 .option = ff_mpv_generic_options,
4901 .version = LIBAVUTIL_VERSION_INT,
4904 AVCodec ff_msmpeg4v2_encoder = {
4905 .name = "msmpeg4v2",
4906 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
4907 .type = AVMEDIA_TYPE_VIDEO,
4908 .id = AV_CODEC_ID_MSMPEG4V2,
4909 .priv_data_size = sizeof(MpegEncContext),
4910 .init = ff_mpv_encode_init,
4911 .encode2 = ff_mpv_encode_picture,
4912 .close = ff_mpv_encode_end,
4913 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4914 .priv_class = &msmpeg4v2_class,
4917 static const AVClass msmpeg4v3_class = {
4918 .class_name = "msmpeg4v3 encoder",
4919 .item_name = av_default_item_name,
4920 .option = ff_mpv_generic_options,
4921 .version = LIBAVUTIL_VERSION_INT,
4924 AVCodec ff_msmpeg4v3_encoder = {
4926 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
4927 .type = AVMEDIA_TYPE_VIDEO,
4928 .id = AV_CODEC_ID_MSMPEG4V3,
4929 .priv_data_size = sizeof(MpegEncContext),
4930 .init = ff_mpv_encode_init,
4931 .encode2 = ff_mpv_encode_picture,
4932 .close = ff_mpv_encode_end,
4933 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4934 .priv_class = &msmpeg4v3_class,
4937 static const AVClass wmv1_class = {
4938 .class_name = "wmv1 encoder",
4939 .item_name = av_default_item_name,
4940 .option = ff_mpv_generic_options,
4941 .version = LIBAVUTIL_VERSION_INT,
4944 AVCodec ff_wmv1_encoder = {
4946 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
4947 .type = AVMEDIA_TYPE_VIDEO,
4948 .id = AV_CODEC_ID_WMV1,
4949 .priv_data_size = sizeof(MpegEncContext),
4950 .init = ff_mpv_encode_init,
4951 .encode2 = ff_mpv_encode_picture,
4952 .close = ff_mpv_encode_end,
4953 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4954 .priv_class = &wmv1_class,