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 Libav.
10 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * The simplest mpeg encoder (well, it was the simplest!).
32 #include "libavutil/internal.h"
33 #include "libavutil/intmath.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/timer.h"
42 #include "mpegvideo.h"
46 #include "mpegutils.h"
51 #include "aandcttab.h"
53 #include "mpeg4video.h"
55 #include "bytestream.h"
58 static int encode_picture(MpegEncContext *s, int picture_number);
59 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
60 static int sse_mb(MpegEncContext *s);
61 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
62 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
64 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
65 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
67 const AVOption ff_mpv_generic_options[] = {
72 void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
73 uint16_t (*qmat16)[2][64],
74 const uint16_t *quant_matrix,
75 int bias, int qmin, int qmax, int intra)
80 for (qscale = qmin; qscale <= qmax; qscale++) {
82 if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
83 dsp->fdct == ff_jpeg_fdct_islow_10 ||
84 dsp->fdct == ff_faandct) {
85 for (i = 0; i < 64; i++) {
86 const int j = dsp->idct_permutation[i];
87 /* 16 <= qscale * quant_matrix[i] <= 7905
88 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
89 * 19952 <= x <= 249205026
90 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
91 * 3444240 >= (1 << 36) / (x) >= 275 */
93 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
94 (qscale * quant_matrix[j]));
96 } else if (dsp->fdct == ff_fdct_ifast) {
97 for (i = 0; i < 64; i++) {
98 const int j = dsp->idct_permutation[i];
99 /* 16 <= qscale * quant_matrix[i] <= 7905
100 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
101 * 19952 <= x <= 249205026
102 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
103 * 3444240 >= (1 << 36) / (x) >= 275 */
105 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
106 (ff_aanscales[i] * qscale *
110 for (i = 0; i < 64; i++) {
111 const int j = dsp->idct_permutation[i];
112 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
113 * Assume x = qscale * quant_matrix[i]
115 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
116 * so 32768 >= (1 << 19) / (x) >= 67 */
117 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
118 (qscale * quant_matrix[j]));
119 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
120 // (qscale * quant_matrix[i]);
121 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
122 (qscale * quant_matrix[j]);
124 if (qmat16[qscale][0][i] == 0 ||
125 qmat16[qscale][0][i] == 128 * 256)
126 qmat16[qscale][0][i] = 128 * 256 - 1;
127 qmat16[qscale][1][i] =
128 ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
129 qmat16[qscale][0][i]);
133 for (i = intra; i < 64; i++) {
135 if (dsp->fdct == ff_fdct_ifast) {
136 max = (8191LL * ff_aanscales[i]) >> 14;
138 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
144 av_log(NULL, AV_LOG_INFO,
145 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
150 static inline void update_qscale(MpegEncContext *s)
152 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
153 (FF_LAMBDA_SHIFT + 7);
154 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
156 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
160 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
166 for (i = 0; i < 64; i++) {
167 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
174 * init s->current_picture.qscale_table from s->lambda_table
176 void ff_init_qscale_tab(MpegEncContext *s)
178 int8_t * const qscale_table = s->current_picture.qscale_table;
181 for (i = 0; i < s->mb_num; i++) {
182 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
183 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
184 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
189 static void update_duplicate_context_after_me(MpegEncContext *dst,
192 #define COPY(a) dst->a= src->a
194 COPY(current_picture);
200 COPY(picture_in_gop_number);
201 COPY(gop_picture_number);
202 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
203 COPY(progressive_frame); // FIXME don't set in encode_header
204 COPY(partitioned_frame); // FIXME don't set in encode_header
209 * Set the given MpegEncContext to defaults for encoding.
210 * the changed fields will not depend upon the prior state of the MpegEncContext.
212 static void MPV_encode_defaults(MpegEncContext *s)
215 ff_MPV_common_defaults(s);
217 for (i = -16; i < 16; i++) {
218 default_fcode_tab[i + MAX_MV] = 1;
220 s->me.mv_penalty = default_mv_penalty;
221 s->fcode_tab = default_fcode_tab;
223 s->input_picture_number = 0;
224 s->picture_in_gop_number = 0;
227 /* init video encoder */
228 av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
230 MpegEncContext *s = avctx->priv_data;
233 MPV_encode_defaults(s);
235 switch (avctx->codec_id) {
236 case AV_CODEC_ID_MPEG2VIDEO:
237 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
238 avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
239 av_log(avctx, AV_LOG_ERROR,
240 "only YUV420 and YUV422 are supported\n");
244 case AV_CODEC_ID_MJPEG:
245 if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
246 avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
247 ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
248 avctx->pix_fmt != AV_PIX_FMT_YUV422P) ||
249 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
250 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
255 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
256 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
261 switch (avctx->pix_fmt) {
262 case AV_PIX_FMT_YUVJ422P:
263 case AV_PIX_FMT_YUV422P:
264 s->chroma_format = CHROMA_422;
266 case AV_PIX_FMT_YUVJ420P:
267 case AV_PIX_FMT_YUV420P:
269 s->chroma_format = CHROMA_420;
273 s->bit_rate = avctx->bit_rate;
274 s->width = avctx->width;
275 s->height = avctx->height;
276 if (avctx->gop_size > 600 &&
277 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
278 av_log(avctx, AV_LOG_ERROR,
279 "Warning keyframe interval too large! reducing it ...\n");
280 avctx->gop_size = 600;
282 s->gop_size = avctx->gop_size;
284 s->flags = avctx->flags;
285 s->flags2 = avctx->flags2;
286 if (avctx->max_b_frames > MAX_B_FRAMES) {
287 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
288 "is %d.\n", MAX_B_FRAMES);
290 s->max_b_frames = avctx->max_b_frames;
291 s->codec_id = avctx->codec->id;
292 s->strict_std_compliance = avctx->strict_std_compliance;
293 s->quarter_sample = (avctx->flags & CODEC_FLAG_QPEL) != 0;
294 s->mpeg_quant = avctx->mpeg_quant;
295 s->rtp_mode = !!avctx->rtp_payload_size;
296 s->intra_dc_precision = avctx->intra_dc_precision;
297 s->user_specified_pts = AV_NOPTS_VALUE;
299 if (s->gop_size <= 1) {
306 s->me_method = avctx->me_method;
309 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
311 s->adaptive_quant = (s->avctx->lumi_masking ||
312 s->avctx->dark_masking ||
313 s->avctx->temporal_cplx_masking ||
314 s->avctx->spatial_cplx_masking ||
315 s->avctx->p_masking ||
316 s->avctx->border_masking ||
317 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
320 s->loop_filter = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
322 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
323 av_log(avctx, AV_LOG_ERROR,
324 "a vbv buffer size is needed, "
325 "for encoding with a maximum bitrate\n");
329 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
330 av_log(avctx, AV_LOG_INFO,
331 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
334 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
335 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
339 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
340 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
344 if (avctx->rc_max_rate &&
345 avctx->rc_max_rate == avctx->bit_rate &&
346 avctx->rc_max_rate != avctx->rc_min_rate) {
347 av_log(avctx, AV_LOG_INFO,
348 "impossible bitrate constraints, this will fail\n");
351 if (avctx->rc_buffer_size &&
352 avctx->bit_rate * (int64_t)avctx->time_base.num >
353 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
354 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
358 if (!s->fixed_qscale &&
359 avctx->bit_rate * av_q2d(avctx->time_base) >
360 avctx->bit_rate_tolerance) {
361 av_log(avctx, AV_LOG_ERROR,
362 "bitrate tolerance too small for bitrate\n");
366 if (s->avctx->rc_max_rate &&
367 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
368 (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
369 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
370 90000LL * (avctx->rc_buffer_size - 1) >
371 s->avctx->rc_max_rate * 0xFFFFLL) {
372 av_log(avctx, AV_LOG_INFO,
373 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
374 "specified vbv buffer is too large for the given bitrate!\n");
377 if ((s->flags & CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
378 s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
379 s->codec_id != AV_CODEC_ID_FLV1) {
380 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
384 if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
385 av_log(avctx, AV_LOG_ERROR,
386 "OBMC is only supported with simple mb decision\n");
390 if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
391 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
395 if (s->max_b_frames &&
396 s->codec_id != AV_CODEC_ID_MPEG4 &&
397 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
398 s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
399 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
403 if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
404 s->codec_id == AV_CODEC_ID_H263 ||
405 s->codec_id == AV_CODEC_ID_H263P) &&
406 (avctx->sample_aspect_ratio.num > 255 ||
407 avctx->sample_aspect_ratio.den > 255)) {
408 av_log(avctx, AV_LOG_ERROR,
409 "Invalid pixel aspect ratio %i/%i, limit is 255/255\n",
410 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
414 if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
415 s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
416 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
420 // FIXME mpeg2 uses that too
421 if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
422 av_log(avctx, AV_LOG_ERROR,
423 "mpeg2 style quantization not supported by codec\n");
427 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
428 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
432 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
433 s->avctx->mb_decision != FF_MB_DECISION_RD) {
434 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
438 if (s->avctx->scenechange_threshold < 1000000000 &&
439 (s->flags & CODEC_FLAG_CLOSED_GOP)) {
440 av_log(avctx, AV_LOG_ERROR,
441 "closed gop with scene change detection are not supported yet, "
442 "set threshold to 1000000000\n");
446 if (s->flags & CODEC_FLAG_LOW_DELAY) {
447 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
448 av_log(avctx, AV_LOG_ERROR,
449 "low delay forcing is only available for mpeg2\n");
452 if (s->max_b_frames != 0) {
453 av_log(avctx, AV_LOG_ERROR,
454 "b frames cannot be used with low delay\n");
459 if (s->q_scale_type == 1) {
460 if (avctx->qmax > 12) {
461 av_log(avctx, AV_LOG_ERROR,
462 "non linear quant only supports qmax <= 12 currently\n");
467 if (s->avctx->thread_count > 1 &&
468 s->codec_id != AV_CODEC_ID_MPEG4 &&
469 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
470 s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
471 (s->codec_id != AV_CODEC_ID_H263P)) {
472 av_log(avctx, AV_LOG_ERROR,
473 "multi threaded encoding not supported by codec\n");
477 if (s->avctx->thread_count < 1) {
478 av_log(avctx, AV_LOG_ERROR,
479 "automatic thread number detection not supported by codec,"
484 if (s->avctx->thread_count > 1)
487 if (!avctx->time_base.den || !avctx->time_base.num) {
488 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
492 i = (INT_MAX / 2 + 128) >> 8;
493 if (avctx->mb_threshold >= i) {
494 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
499 if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
500 av_log(avctx, AV_LOG_INFO,
501 "notice: b_frame_strategy only affects the first pass\n");
502 avctx->b_frame_strategy = 0;
505 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
507 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
508 avctx->time_base.den /= i;
509 avctx->time_base.num /= i;
513 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
514 s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG) {
515 // (a + x * 3 / 8) / x
516 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
517 s->inter_quant_bias = 0;
519 s->intra_quant_bias = 0;
521 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
524 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
525 s->intra_quant_bias = avctx->intra_quant_bias;
526 if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
527 s->inter_quant_bias = avctx->inter_quant_bias;
529 if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
530 s->avctx->time_base.den > (1 << 16) - 1) {
531 av_log(avctx, AV_LOG_ERROR,
532 "timebase %d/%d not supported by MPEG 4 standard, "
533 "the maximum admitted value for the timebase denominator "
534 "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
538 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
540 switch (avctx->codec->id) {
541 case AV_CODEC_ID_MPEG1VIDEO:
542 s->out_format = FMT_MPEG1;
543 s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
544 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
546 case AV_CODEC_ID_MPEG2VIDEO:
547 s->out_format = FMT_MPEG1;
548 s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
549 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
552 case AV_CODEC_ID_MJPEG:
553 s->out_format = FMT_MJPEG;
554 s->intra_only = 1; /* force intra only for jpeg */
555 if (!CONFIG_MJPEG_ENCODER ||
556 ff_mjpeg_encode_init(s) < 0)
561 case AV_CODEC_ID_H261:
562 if (!CONFIG_H261_ENCODER)
564 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
565 av_log(avctx, AV_LOG_ERROR,
566 "The specified picture size of %dx%d is not valid for the "
567 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
568 s->width, s->height);
571 s->out_format = FMT_H261;
575 case AV_CODEC_ID_H263:
576 if (!CONFIG_H263_ENCODER)
578 if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
579 s->width, s->height) == 8) {
580 av_log(avctx, AV_LOG_INFO,
581 "The specified picture size of %dx%d is not valid for "
582 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
583 "352x288, 704x576, and 1408x1152."
584 "Try H.263+.\n", s->width, s->height);
587 s->out_format = FMT_H263;
591 case AV_CODEC_ID_H263P:
592 s->out_format = FMT_H263;
595 s->h263_aic = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
596 s->modified_quant = s->h263_aic;
597 s->loop_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
598 s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
601 /* These are just to be sure */
605 case AV_CODEC_ID_FLV1:
606 s->out_format = FMT_H263;
607 s->h263_flv = 2; /* format = 1; 11-bit codes */
608 s->unrestricted_mv = 1;
609 s->rtp_mode = 0; /* don't allow GOB */
613 case AV_CODEC_ID_RV10:
614 s->out_format = FMT_H263;
618 case AV_CODEC_ID_RV20:
619 s->out_format = FMT_H263;
622 s->modified_quant = 1;
626 s->unrestricted_mv = 0;
628 case AV_CODEC_ID_MPEG4:
629 s->out_format = FMT_H263;
631 s->unrestricted_mv = 1;
632 s->low_delay = s->max_b_frames ? 0 : 1;
633 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
635 case AV_CODEC_ID_MSMPEG4V2:
636 s->out_format = FMT_H263;
638 s->unrestricted_mv = 1;
639 s->msmpeg4_version = 2;
643 case AV_CODEC_ID_MSMPEG4V3:
644 s->out_format = FMT_H263;
646 s->unrestricted_mv = 1;
647 s->msmpeg4_version = 3;
648 s->flipflop_rounding = 1;
652 case AV_CODEC_ID_WMV1:
653 s->out_format = FMT_H263;
655 s->unrestricted_mv = 1;
656 s->msmpeg4_version = 4;
657 s->flipflop_rounding = 1;
661 case AV_CODEC_ID_WMV2:
662 s->out_format = FMT_H263;
664 s->unrestricted_mv = 1;
665 s->msmpeg4_version = 5;
666 s->flipflop_rounding = 1;
674 avctx->has_b_frames = !s->low_delay;
678 s->progressive_frame =
679 s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
680 CODEC_FLAG_INTERLACED_ME) ||
684 if (ff_MPV_common_init(s) < 0)
688 ff_MPV_encode_init_x86(s);
690 s->avctx->coded_frame = s->current_picture.f;
692 if (s->msmpeg4_version) {
693 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
694 2 * 2 * (MAX_LEVEL + 1) *
695 (MAX_RUN + 1) * 2 * sizeof(int), fail);
697 FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
699 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail);
700 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail);
701 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
702 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
703 FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
704 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
705 FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
706 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
708 if (s->avctx->noise_reduction) {
709 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
710 2 * 64 * sizeof(uint16_t), fail);
713 if (CONFIG_H263_ENCODER)
714 ff_h263dsp_init(&s->h263dsp);
715 if (!s->dct_quantize)
716 s->dct_quantize = ff_dct_quantize_c;
718 s->denoise_dct = denoise_dct_c;
719 s->fast_dct_quantize = s->dct_quantize;
721 s->dct_quantize = dct_quantize_trellis_c;
723 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
724 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
726 s->quant_precision = 5;
728 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
729 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
731 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
732 ff_h261_encode_init(s);
733 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
734 ff_h263_encode_init(s);
735 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
736 ff_msmpeg4_encode_init(s);
737 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
738 && s->out_format == FMT_MPEG1)
739 ff_mpeg1_encode_init(s);
742 for (i = 0; i < 64; i++) {
743 int j = s->dsp.idct_permutation[i];
744 if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
746 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
747 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
748 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
750 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
753 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
754 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
756 if (s->avctx->intra_matrix)
757 s->intra_matrix[j] = s->avctx->intra_matrix[i];
758 if (s->avctx->inter_matrix)
759 s->inter_matrix[j] = s->avctx->inter_matrix[i];
762 /* precompute matrix */
763 /* for mjpeg, we do include qscale in the matrix */
764 if (s->out_format != FMT_MJPEG) {
765 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
766 s->intra_matrix, s->intra_quant_bias, avctx->qmin,
768 ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
769 s->inter_matrix, s->inter_quant_bias, avctx->qmin,
773 if (ff_rate_control_init(s) < 0)
776 #if FF_API_ERROR_RATE
777 FF_DISABLE_DEPRECATION_WARNINGS
778 if (avctx->error_rate)
779 s->error_rate = avctx->error_rate;
780 FF_ENABLE_DEPRECATION_WARNINGS;
783 #if FF_API_NORMALIZE_AQP
784 FF_DISABLE_DEPRECATION_WARNINGS
785 if (avctx->flags & CODEC_FLAG_NORMALIZE_AQP)
786 s->mpv_flags |= FF_MPV_FLAG_NAQ;
787 FF_ENABLE_DEPRECATION_WARNINGS;
791 FF_DISABLE_DEPRECATION_WARNINGS
792 if (avctx->flags & CODEC_FLAG_MV0)
793 s->mpv_flags |= FF_MPV_FLAG_MV0;
794 FF_ENABLE_DEPRECATION_WARNINGS
797 if (avctx->b_frame_strategy == 2) {
798 for (i = 0; i < s->max_b_frames + 2; i++) {
799 s->tmp_frames[i] = av_frame_alloc();
800 if (!s->tmp_frames[i])
801 return AVERROR(ENOMEM);
803 s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
804 s->tmp_frames[i]->width = s->width >> avctx->brd_scale;
805 s->tmp_frames[i]->height = s->height >> avctx->brd_scale;
807 ret = av_frame_get_buffer(s->tmp_frames[i], 32);
815 ff_MPV_encode_end(avctx);
816 return AVERROR_UNKNOWN;
819 av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
821 MpegEncContext *s = avctx->priv_data;
824 ff_rate_control_uninit(s);
826 ff_MPV_common_end(s);
827 if (CONFIG_MJPEG_ENCODER &&
828 s->out_format == FMT_MJPEG)
829 ff_mjpeg_encode_close(s);
831 av_freep(&avctx->extradata);
833 for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
834 av_frame_free(&s->tmp_frames[i]);
836 ff_free_picture_tables(&s->new_picture);
837 ff_mpeg_unref_picture(s, &s->new_picture);
839 av_freep(&s->avctx->stats_out);
840 av_freep(&s->ac_stats);
842 av_freep(&s->q_intra_matrix);
843 av_freep(&s->q_inter_matrix);
844 av_freep(&s->q_intra_matrix16);
845 av_freep(&s->q_inter_matrix16);
846 av_freep(&s->input_picture);
847 av_freep(&s->reordered_input_picture);
848 av_freep(&s->dct_offset);
853 static int get_sae(uint8_t *src, int ref, int stride)
858 for (y = 0; y < 16; y++) {
859 for (x = 0; x < 16; x++) {
860 acc += FFABS(src[x + y * stride] - ref);
867 static int get_intra_count(MpegEncContext *s, uint8_t *src,
868 uint8_t *ref, int stride)
876 for (y = 0; y < h; y += 16) {
877 for (x = 0; x < w; x += 16) {
878 int offset = x + y * stride;
879 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
881 int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
882 int sae = get_sae(src + offset, mean, stride);
884 acc += sae + 500 < sad;
891 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
895 int i, display_picture_number = 0, ret;
896 const int encoding_delay = s->max_b_frames ? s->max_b_frames :
897 (s->low_delay ? 0 : 1);
902 display_picture_number = s->input_picture_number++;
904 if (pts != AV_NOPTS_VALUE) {
905 if (s->user_specified_pts != AV_NOPTS_VALUE) {
907 int64_t last = s->user_specified_pts;
910 av_log(s->avctx, AV_LOG_ERROR,
911 "Error, Invalid timestamp=%"PRId64", "
912 "last=%"PRId64"\n", pts, s->user_specified_pts);
916 if (!s->low_delay && display_picture_number == 1)
917 s->dts_delta = time - last;
919 s->user_specified_pts = pts;
921 if (s->user_specified_pts != AV_NOPTS_VALUE) {
922 s->user_specified_pts =
923 pts = s->user_specified_pts + 1;
924 av_log(s->avctx, AV_LOG_INFO,
925 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
928 pts = display_picture_number;
934 if (!pic_arg->buf[0]);
936 if (pic_arg->linesize[0] != s->linesize)
938 if (pic_arg->linesize[1] != s->uvlinesize)
940 if (pic_arg->linesize[2] != s->uvlinesize)
943 av_dlog(s->avctx, "%d %d %td %td\n", pic_arg->linesize[0],
944 pic_arg->linesize[1], s->linesize, s->uvlinesize);
947 i = ff_find_unused_picture(s, 1);
951 pic = &s->picture[i];
954 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
956 if (ff_alloc_picture(s, pic, 1) < 0) {
960 i = ff_find_unused_picture(s, 0);
964 pic = &s->picture[i];
967 if (ff_alloc_picture(s, pic, 0) < 0) {
971 if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
972 pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
973 pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
976 int h_chroma_shift, v_chroma_shift;
977 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
981 for (i = 0; i < 3; i++) {
982 int src_stride = pic_arg->linesize[i];
983 int dst_stride = i ? s->uvlinesize : s->linesize;
984 int h_shift = i ? h_chroma_shift : 0;
985 int v_shift = i ? v_chroma_shift : 0;
986 int w = s->width >> h_shift;
987 int h = s->height >> v_shift;
988 uint8_t *src = pic_arg->data[i];
989 uint8_t *dst = pic->f->data[i];
991 if (!s->avctx->rc_buffer_size)
992 dst += INPLACE_OFFSET;
994 if (src_stride == dst_stride)
995 memcpy(dst, src, src_stride * h);
1006 ret = av_frame_copy_props(pic->f, pic_arg);
1010 pic->f->display_picture_number = display_picture_number;
1011 pic->f->pts = pts; // we set this here to avoid modifiying pic_arg
1014 /* shift buffer entries */
1015 for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1016 s->input_picture[i - 1] = s->input_picture[i];
1018 s->input_picture[encoding_delay] = (Picture*) pic;
1023 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1027 int64_t score64 = 0;
1029 for (plane = 0; plane < 3; plane++) {
1030 const int stride = p->f->linesize[plane];
1031 const int bw = plane ? 1 : 2;
1032 for (y = 0; y < s->mb_height * bw; y++) {
1033 for (x = 0; x < s->mb_width * bw; x++) {
1034 int off = p->shared ? 0 : 16;
1035 uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1036 uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1037 int v = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1039 switch (s->avctx->frame_skip_exp) {
1040 case 0: score = FFMAX(score, v); break;
1041 case 1: score += FFABS(v); break;
1042 case 2: score += v * v; break;
1043 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1044 case 4: score64 += v * v * (int64_t)(v * v); break;
1053 if (score64 < s->avctx->frame_skip_threshold)
1055 if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
1060 static int encode_frame(AVCodecContext *c, AVFrame *frame)
1062 AVPacket pkt = { 0 };
1063 int ret, got_output;
1065 av_init_packet(&pkt);
1066 ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
1071 av_free_packet(&pkt);
1075 static int estimate_best_b_count(MpegEncContext *s)
1077 AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1078 AVCodecContext *c = avcodec_alloc_context3(NULL);
1079 const int scale = s->avctx->brd_scale;
1080 int i, j, out_size, p_lambda, b_lambda, lambda2;
1081 int64_t best_rd = INT64_MAX;
1082 int best_b_count = -1;
1084 assert(scale >= 0 && scale <= 3);
1087 //s->next_picture_ptr->quality;
1088 p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1089 //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1090 b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1091 if (!b_lambda) // FIXME we should do this somewhere else
1092 b_lambda = p_lambda;
1093 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1096 c->width = s->width >> scale;
1097 c->height = s->height >> scale;
1098 c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;
1099 c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;
1100 c->mb_decision = s->avctx->mb_decision;
1101 c->me_cmp = s->avctx->me_cmp;
1102 c->mb_cmp = s->avctx->mb_cmp;
1103 c->me_sub_cmp = s->avctx->me_sub_cmp;
1104 c->pix_fmt = AV_PIX_FMT_YUV420P;
1105 c->time_base = s->avctx->time_base;
1106 c->max_b_frames = s->max_b_frames;
1108 if (avcodec_open2(c, codec, NULL) < 0)
1111 for (i = 0; i < s->max_b_frames + 2; i++) {
1112 Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1113 s->next_picture_ptr;
1115 if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1116 pre_input = *pre_input_ptr;
1118 if (!pre_input.shared && i) {
1119 pre_input.f->data[0] += INPLACE_OFFSET;
1120 pre_input.f->data[1] += INPLACE_OFFSET;
1121 pre_input.f->data[2] += INPLACE_OFFSET;
1124 s->dsp.shrink[scale](s->tmp_frames[i]->data[0], s->tmp_frames[i]->linesize[0],
1125 pre_input.f->data[0], pre_input.f->linesize[0],
1126 c->width, c->height);
1127 s->dsp.shrink[scale](s->tmp_frames[i]->data[1], s->tmp_frames[i]->linesize[1],
1128 pre_input.f->data[1], pre_input.f->linesize[1],
1129 c->width >> 1, c->height >> 1);
1130 s->dsp.shrink[scale](s->tmp_frames[i]->data[2], s->tmp_frames[i]->linesize[2],
1131 pre_input.f->data[2], pre_input.f->linesize[2],
1132 c->width >> 1, c->height >> 1);
1136 for (j = 0; j < s->max_b_frames + 1; j++) {
1139 if (!s->input_picture[j])
1142 c->error[0] = c->error[1] = c->error[2] = 0;
1144 s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1145 s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1147 out_size = encode_frame(c, s->tmp_frames[0]);
1149 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1151 for (i = 0; i < s->max_b_frames + 1; i++) {
1152 int is_p = i % (j + 1) == j || i == s->max_b_frames;
1154 s->tmp_frames[i + 1]->pict_type = is_p ?
1155 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1156 s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1158 out_size = encode_frame(c, s->tmp_frames[i + 1]);
1160 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1163 /* get the delayed frames */
1165 out_size = encode_frame(c, NULL);
1166 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1169 rd += c->error[0] + c->error[1] + c->error[2];
1180 return best_b_count;
1183 static int select_input_picture(MpegEncContext *s)
1187 for (i = 1; i < MAX_PICTURE_COUNT; i++)
1188 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1189 s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1191 /* set next picture type & ordering */
1192 if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
1193 if (/*s->picture_in_gop_number >= s->gop_size ||*/
1194 s->next_picture_ptr == NULL || s->intra_only) {
1195 s->reordered_input_picture[0] = s->input_picture[0];
1196 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1197 s->reordered_input_picture[0]->f->coded_picture_number =
1198 s->coded_picture_number++;
1202 if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1203 if (s->picture_in_gop_number < s->gop_size &&
1204 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1205 // FIXME check that te gop check above is +-1 correct
1206 av_frame_unref(s->input_picture[0]->f);
1209 ff_vbv_update(s, 0);
1215 if (s->flags & CODEC_FLAG_PASS2) {
1216 for (i = 0; i < s->max_b_frames + 1; i++) {
1217 int pict_num = s->input_picture[0]->f->display_picture_number + i;
1219 if (pict_num >= s->rc_context.num_entries)
1221 if (!s->input_picture[i]) {
1222 s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1226 s->input_picture[i]->f->pict_type =
1227 s->rc_context.entry[pict_num].new_pict_type;
1231 if (s->avctx->b_frame_strategy == 0) {
1232 b_frames = s->max_b_frames;
1233 while (b_frames && !s->input_picture[b_frames])
1235 } else if (s->avctx->b_frame_strategy == 1) {
1236 for (i = 1; i < s->max_b_frames + 1; i++) {
1237 if (s->input_picture[i] &&
1238 s->input_picture[i]->b_frame_score == 0) {
1239 s->input_picture[i]->b_frame_score =
1241 s->input_picture[i ]->f->data[0],
1242 s->input_picture[i - 1]->f->data[0],
1246 for (i = 0; i < s->max_b_frames + 1; i++) {
1247 if (s->input_picture[i] == NULL ||
1248 s->input_picture[i]->b_frame_score - 1 >
1249 s->mb_num / s->avctx->b_sensitivity)
1253 b_frames = FFMAX(0, i - 1);
1256 for (i = 0; i < b_frames + 1; i++) {
1257 s->input_picture[i]->b_frame_score = 0;
1259 } else if (s->avctx->b_frame_strategy == 2) {
1260 b_frames = estimate_best_b_count(s);
1262 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1268 for (i = b_frames - 1; i >= 0; i--) {
1269 int type = s->input_picture[i]->f->pict_type;
1270 if (type && type != AV_PICTURE_TYPE_B)
1273 if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1274 b_frames == s->max_b_frames) {
1275 av_log(s->avctx, AV_LOG_ERROR,
1276 "warning, too many b frames in a row\n");
1279 if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1280 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1281 s->gop_size > s->picture_in_gop_number) {
1282 b_frames = s->gop_size - s->picture_in_gop_number - 1;
1284 if (s->flags & CODEC_FLAG_CLOSED_GOP)
1286 s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1290 if ((s->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1291 s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1294 s->reordered_input_picture[0] = s->input_picture[b_frames];
1295 if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1296 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1297 s->reordered_input_picture[0]->f->coded_picture_number =
1298 s->coded_picture_number++;
1299 for (i = 0; i < b_frames; i++) {
1300 s->reordered_input_picture[i + 1] = s->input_picture[i];
1301 s->reordered_input_picture[i + 1]->f->pict_type =
1303 s->reordered_input_picture[i + 1]->f->coded_picture_number =
1304 s->coded_picture_number++;
1309 if (s->reordered_input_picture[0]) {
1310 s->reordered_input_picture[0]->reference =
1311 s->reordered_input_picture[0]->f->pict_type !=
1312 AV_PICTURE_TYPE_B ? 3 : 0;
1314 ff_mpeg_unref_picture(s, &s->new_picture);
1315 if ((ret = ff_mpeg_ref_picture(s, &s->new_picture, s->reordered_input_picture[0])))
1318 if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1319 // input is a shared pix, so we can't modifiy it -> alloc a new
1320 // one & ensure that the shared one is reuseable
1323 int i = ff_find_unused_picture(s, 0);
1326 pic = &s->picture[i];
1328 pic->reference = s->reordered_input_picture[0]->reference;
1329 if (ff_alloc_picture(s, pic, 0) < 0) {
1333 ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1337 /* mark us unused / free shared pic */
1338 av_frame_unref(s->reordered_input_picture[0]->f);
1339 s->reordered_input_picture[0]->shared = 0;
1341 s->current_picture_ptr = pic;
1343 // input is not a shared pix -> reuse buffer for current_pix
1344 s->current_picture_ptr = s->reordered_input_picture[0];
1345 for (i = 0; i < 4; i++) {
1346 s->new_picture.f->data[i] += INPLACE_OFFSET;
1349 ff_mpeg_unref_picture(s, &s->current_picture);
1350 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1351 s->current_picture_ptr)) < 0)
1354 s->picture_number = s->new_picture.f->display_picture_number;
1356 ff_mpeg_unref_picture(s, &s->new_picture);
1361 static void frame_end(MpegEncContext *s)
1365 if (s->unrestricted_mv &&
1366 s->current_picture.reference &&
1368 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1369 int hshift = desc->log2_chroma_w;
1370 int vshift = desc->log2_chroma_h;
1371 s->dsp.draw_edges(s->current_picture.f->data[0], s->linesize,
1372 s->h_edge_pos, s->v_edge_pos,
1373 EDGE_WIDTH, EDGE_WIDTH,
1374 EDGE_TOP | EDGE_BOTTOM);
1375 s->dsp.draw_edges(s->current_picture.f->data[1], s->uvlinesize,
1376 s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1377 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1378 EDGE_TOP | EDGE_BOTTOM);
1379 s->dsp.draw_edges(s->current_picture.f->data[2], s->uvlinesize,
1380 s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1381 EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1382 EDGE_TOP | EDGE_BOTTOM);
1387 s->last_pict_type = s->pict_type;
1388 s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1389 if (s->pict_type!= AV_PICTURE_TYPE_B)
1390 s->last_non_b_pict_type = s->pict_type;
1393 /* release non-reference frames */
1394 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1395 if (!s->picture[i].reference)
1396 ff_mpeg_unref_picture(s, &s->picture[i]);
1400 s->avctx->coded_frame = s->current_picture_ptr->f;
1404 static void update_noise_reduction(MpegEncContext *s)
1408 for (intra = 0; intra < 2; intra++) {
1409 if (s->dct_count[intra] > (1 << 16)) {
1410 for (i = 0; i < 64; i++) {
1411 s->dct_error_sum[intra][i] >>= 1;
1413 s->dct_count[intra] >>= 1;
1416 for (i = 0; i < 64; i++) {
1417 s->dct_offset[intra][i] = (s->avctx->noise_reduction *
1418 s->dct_count[intra] +
1419 s->dct_error_sum[intra][i] / 2) /
1420 (s->dct_error_sum[intra][i] + 1);
1425 static int frame_start(MpegEncContext *s)
1429 /* mark & release old frames */
1430 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1431 s->last_picture_ptr != s->next_picture_ptr &&
1432 s->last_picture_ptr->f->buf[0]) {
1433 ff_mpeg_unref_picture(s, s->last_picture_ptr);
1436 s->current_picture_ptr->f->pict_type = s->pict_type;
1437 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1439 ff_mpeg_unref_picture(s, &s->current_picture);
1440 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1441 s->current_picture_ptr)) < 0)
1444 if (s->pict_type != AV_PICTURE_TYPE_B) {
1445 s->last_picture_ptr = s->next_picture_ptr;
1447 s->next_picture_ptr = s->current_picture_ptr;
1450 if (s->last_picture_ptr) {
1451 ff_mpeg_unref_picture(s, &s->last_picture);
1452 if (s->last_picture_ptr->f->buf[0] &&
1453 (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1454 s->last_picture_ptr)) < 0)
1457 if (s->next_picture_ptr) {
1458 ff_mpeg_unref_picture(s, &s->next_picture);
1459 if (s->next_picture_ptr->f->buf[0] &&
1460 (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1461 s->next_picture_ptr)) < 0)
1465 if (s->picture_structure!= PICT_FRAME) {
1467 for (i = 0; i < 4; i++) {
1468 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1469 s->current_picture.f->data[i] +=
1470 s->current_picture.f->linesize[i];
1472 s->current_picture.f->linesize[i] *= 2;
1473 s->last_picture.f->linesize[i] *= 2;
1474 s->next_picture.f->linesize[i] *= 2;
1478 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1479 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1480 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1481 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1482 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1483 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1485 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1486 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1489 if (s->dct_error_sum) {
1490 assert(s->avctx->noise_reduction && s->encoding);
1491 update_noise_reduction(s);
1497 int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1498 const AVFrame *pic_arg, int *got_packet)
1500 MpegEncContext *s = avctx->priv_data;
1501 int i, stuffing_count, ret;
1502 int context_count = s->slice_context_count;
1504 s->picture_in_gop_number++;
1506 if (load_input_picture(s, pic_arg) < 0)
1509 if (select_input_picture(s) < 0) {
1514 if (s->new_picture.f->data[0]) {
1516 (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
1519 s->mb_info_ptr = av_packet_new_side_data(pkt,
1520 AV_PKT_DATA_H263_MB_INFO,
1521 s->mb_width*s->mb_height*12);
1522 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1525 for (i = 0; i < context_count; i++) {
1526 int start_y = s->thread_context[i]->start_mb_y;
1527 int end_y = s->thread_context[i]-> end_mb_y;
1528 int h = s->mb_height;
1529 uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1530 uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1532 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1535 s->pict_type = s->new_picture.f->pict_type;
1537 ret = frame_start(s);
1541 if (encode_picture(s, s->picture_number) < 0)
1544 avctx->header_bits = s->header_bits;
1545 avctx->mv_bits = s->mv_bits;
1546 avctx->misc_bits = s->misc_bits;
1547 avctx->i_tex_bits = s->i_tex_bits;
1548 avctx->p_tex_bits = s->p_tex_bits;
1549 avctx->i_count = s->i_count;
1550 // FIXME f/b_count in avctx
1551 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
1552 avctx->skip_count = s->skip_count;
1556 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1557 ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1559 if (avctx->rc_buffer_size) {
1560 RateControlContext *rcc = &s->rc_context;
1561 int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
1563 if (put_bits_count(&s->pb) > max_size &&
1564 s->lambda < s->avctx->lmax) {
1565 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1566 (s->qscale + 1) / s->qscale);
1567 if (s->adaptive_quant) {
1569 for (i = 0; i < s->mb_height * s->mb_stride; i++)
1570 s->lambda_table[i] =
1571 FFMAX(s->lambda_table[i] + 1,
1572 s->lambda_table[i] * (s->qscale + 1) /
1575 s->mb_skipped = 0; // done in frame_start()
1576 // done in encode_picture() so we must undo it
1577 if (s->pict_type == AV_PICTURE_TYPE_P) {
1578 if (s->flipflop_rounding ||
1579 s->codec_id == AV_CODEC_ID_H263P ||
1580 s->codec_id == AV_CODEC_ID_MPEG4)
1581 s->no_rounding ^= 1;
1583 if (s->pict_type != AV_PICTURE_TYPE_B) {
1584 s->time_base = s->last_time_base;
1585 s->last_non_b_time = s->time - s->pp_time;
1587 for (i = 0; i < context_count; i++) {
1588 PutBitContext *pb = &s->thread_context[i]->pb;
1589 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1594 assert(s->avctx->rc_max_rate);
1597 if (s->flags & CODEC_FLAG_PASS1)
1598 ff_write_pass1_stats(s);
1600 for (i = 0; i < 4; i++) {
1601 s->current_picture_ptr->f->error[i] = s->current_picture.f->error[i];
1602 avctx->error[i] += s->current_picture_ptr->f->error[i];
1605 if (s->flags & CODEC_FLAG_PASS1)
1606 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1607 avctx->i_tex_bits + avctx->p_tex_bits ==
1608 put_bits_count(&s->pb));
1609 flush_put_bits(&s->pb);
1610 s->frame_bits = put_bits_count(&s->pb);
1612 stuffing_count = ff_vbv_update(s, s->frame_bits);
1613 if (stuffing_count) {
1614 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1615 stuffing_count + 50) {
1616 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1620 switch (s->codec_id) {
1621 case AV_CODEC_ID_MPEG1VIDEO:
1622 case AV_CODEC_ID_MPEG2VIDEO:
1623 while (stuffing_count--) {
1624 put_bits(&s->pb, 8, 0);
1627 case AV_CODEC_ID_MPEG4:
1628 put_bits(&s->pb, 16, 0);
1629 put_bits(&s->pb, 16, 0x1C3);
1630 stuffing_count -= 4;
1631 while (stuffing_count--) {
1632 put_bits(&s->pb, 8, 0xFF);
1636 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1638 flush_put_bits(&s->pb);
1639 s->frame_bits = put_bits_count(&s->pb);
1642 /* update mpeg1/2 vbv_delay for CBR */
1643 if (s->avctx->rc_max_rate &&
1644 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1645 s->out_format == FMT_MPEG1 &&
1646 90000LL * (avctx->rc_buffer_size - 1) <=
1647 s->avctx->rc_max_rate * 0xFFFFLL) {
1648 int vbv_delay, min_delay;
1649 double inbits = s->avctx->rc_max_rate *
1650 av_q2d(s->avctx->time_base);
1651 int minbits = s->frame_bits - 8 *
1652 (s->vbv_delay_ptr - s->pb.buf - 1);
1653 double bits = s->rc_context.buffer_index + minbits - inbits;
1656 av_log(s->avctx, AV_LOG_ERROR,
1657 "Internal error, negative bits\n");
1659 assert(s->repeat_first_field == 0);
1661 vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1662 min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1663 s->avctx->rc_max_rate;
1665 vbv_delay = FFMAX(vbv_delay, min_delay);
1667 assert(vbv_delay < 0xFFFF);
1669 s->vbv_delay_ptr[0] &= 0xF8;
1670 s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1671 s->vbv_delay_ptr[1] = vbv_delay >> 5;
1672 s->vbv_delay_ptr[2] &= 0x07;
1673 s->vbv_delay_ptr[2] |= vbv_delay << 3;
1674 avctx->vbv_delay = vbv_delay * 300;
1676 s->total_bits += s->frame_bits;
1677 avctx->frame_bits = s->frame_bits;
1679 pkt->pts = s->current_picture.f->pts;
1680 if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1681 if (!s->current_picture.f->coded_picture_number)
1682 pkt->dts = pkt->pts - s->dts_delta;
1684 pkt->dts = s->reordered_pts;
1685 s->reordered_pts = pkt->pts;
1687 pkt->dts = pkt->pts;
1688 if (s->current_picture.f->key_frame)
1689 pkt->flags |= AV_PKT_FLAG_KEY;
1691 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1695 assert((s->frame_bits & 7) == 0);
1697 pkt->size = s->frame_bits / 8;
1698 *got_packet = !!pkt->size;
1702 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1703 int n, int threshold)
1705 static const char tab[64] = {
1706 3, 2, 2, 1, 1, 1, 1, 1,
1707 1, 1, 1, 1, 1, 1, 1, 1,
1708 1, 1, 1, 1, 1, 1, 1, 1,
1709 0, 0, 0, 0, 0, 0, 0, 0,
1710 0, 0, 0, 0, 0, 0, 0, 0,
1711 0, 0, 0, 0, 0, 0, 0, 0,
1712 0, 0, 0, 0, 0, 0, 0, 0,
1713 0, 0, 0, 0, 0, 0, 0, 0
1718 int16_t *block = s->block[n];
1719 const int last_index = s->block_last_index[n];
1722 if (threshold < 0) {
1724 threshold = -threshold;
1728 /* Are all we could set to zero already zero? */
1729 if (last_index <= skip_dc - 1)
1732 for (i = 0; i <= last_index; i++) {
1733 const int j = s->intra_scantable.permutated[i];
1734 const int level = FFABS(block[j]);
1736 if (skip_dc && i == 0)
1740 } else if (level > 1) {
1746 if (score >= threshold)
1748 for (i = skip_dc; i <= last_index; i++) {
1749 const int j = s->intra_scantable.permutated[i];
1753 s->block_last_index[n] = 0;
1755 s->block_last_index[n] = -1;
1758 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1762 const int maxlevel = s->max_qcoeff;
1763 const int minlevel = s->min_qcoeff;
1767 i = 1; // skip clipping of intra dc
1771 for (; i <= last_index; i++) {
1772 const int j = s->intra_scantable.permutated[i];
1773 int level = block[j];
1775 if (level > maxlevel) {
1778 } else if (level < minlevel) {
1786 if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1787 av_log(s->avctx, AV_LOG_INFO,
1788 "warning, clipping %d dct coefficients to %d..%d\n",
1789 overflow, minlevel, maxlevel);
1792 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1796 for (y = 0; y < 8; y++) {
1797 for (x = 0; x < 8; x++) {
1803 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1804 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1805 int v = ptr[x2 + y2 * stride];
1811 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
1816 static av_always_inline void encode_mb_internal(MpegEncContext *s,
1817 int motion_x, int motion_y,
1818 int mb_block_height,
1821 int16_t weight[8][64];
1822 int16_t orig[8][64];
1823 const int mb_x = s->mb_x;
1824 const int mb_y = s->mb_y;
1827 int dct_offset = s->linesize * 8; // default for progressive frames
1828 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1829 ptrdiff_t wrap_y, wrap_c;
1831 for (i = 0; i < mb_block_count; i++)
1832 skip_dct[i] = s->skipdct;
1834 if (s->adaptive_quant) {
1835 const int last_qp = s->qscale;
1836 const int mb_xy = mb_x + mb_y * s->mb_stride;
1838 s->lambda = s->lambda_table[mb_xy];
1841 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
1842 s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
1843 s->dquant = s->qscale - last_qp;
1845 if (s->out_format == FMT_H263) {
1846 s->dquant = av_clip(s->dquant, -2, 2);
1848 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1850 if (s->pict_type == AV_PICTURE_TYPE_B) {
1851 if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1854 if (s->mv_type == MV_TYPE_8X8)
1860 ff_set_qscale(s, last_qp + s->dquant);
1861 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
1862 ff_set_qscale(s, s->qscale + s->dquant);
1864 wrap_y = s->linesize;
1865 wrap_c = s->uvlinesize;
1866 ptr_y = s->new_picture.f->data[0] +
1867 (mb_y * 16 * wrap_y) + mb_x * 16;
1868 ptr_cb = s->new_picture.f->data[1] +
1869 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1870 ptr_cr = s->new_picture.f->data[2] +
1871 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1873 if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
1874 uint8_t *ebuf = s->edge_emu_buffer + 32;
1875 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
1877 16, 16, mb_x * 16, mb_y * 16,
1878 s->width, s->height);
1880 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
1882 8, mb_block_height, mb_x * 8, mb_y * 8,
1883 s->width >> 1, s->height >> 1);
1884 ptr_cb = ebuf + 18 * wrap_y;
1885 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr,
1887 8, mb_block_height, mb_x * 8, mb_y * 8,
1888 s->width >> 1, s->height >> 1);
1889 ptr_cr = ebuf + 18 * wrap_y + 8;
1893 if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1894 int progressive_score, interlaced_score;
1896 s->interlaced_dct = 0;
1897 progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,
1899 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1900 NULL, wrap_y, 8) - 400;
1902 if (progressive_score > 0) {
1903 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,
1904 NULL, wrap_y * 2, 8) +
1905 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,
1906 NULL, wrap_y * 2, 8);
1907 if (progressive_score > interlaced_score) {
1908 s->interlaced_dct = 1;
1910 dct_offset = wrap_y;
1912 if (s->chroma_format == CHROMA_422)
1918 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
1919 s->dsp.get_pixels(s->block[1], ptr_y + 8 , wrap_y);
1920 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
1921 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);
1923 if (s->flags & CODEC_FLAG_GRAY) {
1927 s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1928 s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1929 if (!s->chroma_y_shift) { /* 422 */
1930 s->dsp.get_pixels(s->block[6],
1931 ptr_cb + (dct_offset >> 1), wrap_c);
1932 s->dsp.get_pixels(s->block[7],
1933 ptr_cr + (dct_offset >> 1), wrap_c);
1937 op_pixels_func (*op_pix)[4];
1938 qpel_mc_func (*op_qpix)[16];
1939 uint8_t *dest_y, *dest_cb, *dest_cr;
1941 dest_y = s->dest[0];
1942 dest_cb = s->dest[1];
1943 dest_cr = s->dest[2];
1945 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1946 op_pix = s->hdsp.put_pixels_tab;
1947 op_qpix = s->dsp.put_qpel_pixels_tab;
1949 op_pix = s->hdsp.put_no_rnd_pixels_tab;
1950 op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;
1953 if (s->mv_dir & MV_DIR_FORWARD) {
1954 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0,
1955 s->last_picture.f->data,
1957 op_pix = s->hdsp.avg_pixels_tab;
1958 op_qpix = s->dsp.avg_qpel_pixels_tab;
1960 if (s->mv_dir & MV_DIR_BACKWARD) {
1961 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1,
1962 s->next_picture.f->data,
1966 if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1967 int progressive_score, interlaced_score;
1969 s->interlaced_dct = 0;
1970 progressive_score = s->dsp.ildct_cmp[0](s, dest_y,
1973 s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,
1974 ptr_y + wrap_y * 8, wrap_y,
1977 if (s->avctx->ildct_cmp == FF_CMP_VSSE)
1978 progressive_score -= 400;
1980 if (progressive_score > 0) {
1981 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,
1984 s->dsp.ildct_cmp[0](s, dest_y + wrap_y,
1988 if (progressive_score > interlaced_score) {
1989 s->interlaced_dct = 1;
1991 dct_offset = wrap_y;
1993 if (s->chroma_format == CHROMA_422)
1999 s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2000 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2001 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2002 dest_y + dct_offset, wrap_y);
2003 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2004 dest_y + dct_offset + 8, wrap_y);
2006 if (s->flags & CODEC_FLAG_GRAY) {
2010 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2011 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2012 if (!s->chroma_y_shift) { /* 422 */
2013 s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
2014 dest_cb + (dct_offset >> 1), wrap_c);
2015 s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
2016 dest_cr + (dct_offset >> 1), wrap_c);
2019 /* pre quantization */
2020 if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2021 2 * s->qscale * s->qscale) {
2023 if (s->dsp.sad[1](NULL, ptr_y , dest_y,
2024 wrap_y, 8) < 20 * s->qscale)
2026 if (s->dsp.sad[1](NULL, ptr_y + 8,
2027 dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2029 if (s->dsp.sad[1](NULL, ptr_y + dct_offset,
2030 dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)
2032 if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,
2033 dest_y + dct_offset + 8,
2034 wrap_y, 8) < 20 * s->qscale)
2036 if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,
2037 wrap_c, 8) < 20 * s->qscale)
2039 if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,
2040 wrap_c, 8) < 20 * s->qscale)
2042 if (!s->chroma_y_shift) { /* 422 */
2043 if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),
2044 dest_cb + (dct_offset >> 1),
2045 wrap_c, 8) < 20 * s->qscale)
2047 if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),
2048 dest_cr + (dct_offset >> 1),
2049 wrap_c, 8) < 20 * s->qscale)
2055 if (s->quantizer_noise_shaping) {
2057 get_visual_weight(weight[0], ptr_y , wrap_y);
2059 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2061 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2063 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2065 get_visual_weight(weight[4], ptr_cb , wrap_c);
2067 get_visual_weight(weight[5], ptr_cr , wrap_c);
2068 if (!s->chroma_y_shift) { /* 422 */
2070 get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
2073 get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
2076 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2079 /* DCT & quantize */
2080 assert(s->out_format != FMT_MJPEG || s->qscale == 8);
2082 for (i = 0; i < mb_block_count; i++) {
2085 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2086 // FIXME we could decide to change to quantizer instead of
2088 // JS: I don't think that would be a good idea it could lower
2089 // quality instead of improve it. Just INTRADC clipping
2090 // deserves changes in quantizer
2092 clip_coeffs(s, s->block[i], s->block_last_index[i]);
2094 s->block_last_index[i] = -1;
2096 if (s->quantizer_noise_shaping) {
2097 for (i = 0; i < mb_block_count; i++) {
2099 s->block_last_index[i] =
2100 dct_quantize_refine(s, s->block[i], weight[i],
2101 orig[i], i, s->qscale);
2106 if (s->luma_elim_threshold && !s->mb_intra)
2107 for (i = 0; i < 4; i++)
2108 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2109 if (s->chroma_elim_threshold && !s->mb_intra)
2110 for (i = 4; i < mb_block_count; i++)
2111 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2113 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2114 for (i = 0; i < mb_block_count; i++) {
2115 if (s->block_last_index[i] == -1)
2116 s->coded_score[i] = INT_MAX / 256;
2121 if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2122 s->block_last_index[4] =
2123 s->block_last_index[5] = 0;
2125 s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2128 // non c quantize code returns incorrect block_last_index FIXME
2129 if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2130 for (i = 0; i < mb_block_count; i++) {
2132 if (s->block_last_index[i] > 0) {
2133 for (j = 63; j > 0; j--) {
2134 if (s->block[i][s->intra_scantable.permutated[j]])
2137 s->block_last_index[i] = j;
2142 /* huffman encode */
2143 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2144 case AV_CODEC_ID_MPEG1VIDEO:
2145 case AV_CODEC_ID_MPEG2VIDEO:
2146 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2147 ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2149 case AV_CODEC_ID_MPEG4:
2150 if (CONFIG_MPEG4_ENCODER)
2151 ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2153 case AV_CODEC_ID_MSMPEG4V2:
2154 case AV_CODEC_ID_MSMPEG4V3:
2155 case AV_CODEC_ID_WMV1:
2156 if (CONFIG_MSMPEG4_ENCODER)
2157 ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2159 case AV_CODEC_ID_WMV2:
2160 if (CONFIG_WMV2_ENCODER)
2161 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2163 case AV_CODEC_ID_H261:
2164 if (CONFIG_H261_ENCODER)
2165 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2167 case AV_CODEC_ID_H263:
2168 case AV_CODEC_ID_H263P:
2169 case AV_CODEC_ID_FLV1:
2170 case AV_CODEC_ID_RV10:
2171 case AV_CODEC_ID_RV20:
2172 if (CONFIG_H263_ENCODER)
2173 ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2175 case AV_CODEC_ID_MJPEG:
2176 if (CONFIG_MJPEG_ENCODER)
2177 ff_mjpeg_encode_mb(s, s->block);
2184 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2186 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
2187 else encode_mb_internal(s, motion_x, motion_y, 16, 8);
2190 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2193 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2196 d->mb_skip_run= s->mb_skip_run;
2198 d->last_dc[i] = s->last_dc[i];
2201 d->mv_bits= s->mv_bits;
2202 d->i_tex_bits= s->i_tex_bits;
2203 d->p_tex_bits= s->p_tex_bits;
2204 d->i_count= s->i_count;
2205 d->f_count= s->f_count;
2206 d->b_count= s->b_count;
2207 d->skip_count= s->skip_count;
2208 d->misc_bits= s->misc_bits;
2212 d->qscale= s->qscale;
2213 d->dquant= s->dquant;
2215 d->esc3_level_length= s->esc3_level_length;
2218 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2221 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2222 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2225 d->mb_skip_run= s->mb_skip_run;
2227 d->last_dc[i] = s->last_dc[i];
2230 d->mv_bits= s->mv_bits;
2231 d->i_tex_bits= s->i_tex_bits;
2232 d->p_tex_bits= s->p_tex_bits;
2233 d->i_count= s->i_count;
2234 d->f_count= s->f_count;
2235 d->b_count= s->b_count;
2236 d->skip_count= s->skip_count;
2237 d->misc_bits= s->misc_bits;
2239 d->mb_intra= s->mb_intra;
2240 d->mb_skipped= s->mb_skipped;
2241 d->mv_type= s->mv_type;
2242 d->mv_dir= s->mv_dir;
2244 if(s->data_partitioning){
2246 d->tex_pb= s->tex_pb;
2250 d->block_last_index[i]= s->block_last_index[i];
2251 d->interlaced_dct= s->interlaced_dct;
2252 d->qscale= s->qscale;
2254 d->esc3_level_length= s->esc3_level_length;
2257 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2258 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2259 int *dmin, int *next_block, int motion_x, int motion_y)
2262 uint8_t *dest_backup[3];
2264 copy_context_before_encode(s, backup, type);
2266 s->block= s->blocks[*next_block];
2267 s->pb= pb[*next_block];
2268 if(s->data_partitioning){
2269 s->pb2 = pb2 [*next_block];
2270 s->tex_pb= tex_pb[*next_block];
2274 memcpy(dest_backup, s->dest, sizeof(s->dest));
2275 s->dest[0] = s->rd_scratchpad;
2276 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
2277 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
2278 assert(s->linesize >= 32); //FIXME
2281 encode_mb(s, motion_x, motion_y);
2283 score= put_bits_count(&s->pb);
2284 if(s->data_partitioning){
2285 score+= put_bits_count(&s->pb2);
2286 score+= put_bits_count(&s->tex_pb);
2289 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2290 ff_MPV_decode_mb(s, s->block);
2292 score *= s->lambda2;
2293 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2297 memcpy(s->dest, dest_backup, sizeof(s->dest));
2304 copy_context_after_encode(best, s, type);
2308 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2309 uint32_t *sq = ff_square_tab + 256;
2314 return s->dsp.sse[0](NULL, src1, src2, stride, 16);
2315 else if(w==8 && h==8)
2316 return s->dsp.sse[1](NULL, src1, src2, stride, 8);
2320 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2329 static int sse_mb(MpegEncContext *s){
2333 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2334 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2337 if(s->avctx->mb_cmp == FF_CMP_NSSE){
2338 return s->dsp.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)
2339 +s->dsp.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)
2340 +s->dsp.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);
2342 return s->dsp.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)
2343 +s->dsp.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)
2344 +s->dsp.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);
2347 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)
2348 +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)
2349 +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);
2352 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2353 MpegEncContext *s= *(void**)arg;
2357 s->me.dia_size= s->avctx->pre_dia_size;
2358 s->first_slice_line=1;
2359 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2360 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2361 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2363 s->first_slice_line=0;
2371 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2372 MpegEncContext *s= *(void**)arg;
2374 s->me.dia_size= s->avctx->dia_size;
2375 s->first_slice_line=1;
2376 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2377 s->mb_x=0; //for block init below
2378 ff_init_block_index(s);
2379 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2380 s->block_index[0]+=2;
2381 s->block_index[1]+=2;
2382 s->block_index[2]+=2;
2383 s->block_index[3]+=2;
2385 /* compute motion vector & mb_type and store in context */
2386 if(s->pict_type==AV_PICTURE_TYPE_B)
2387 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2389 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2391 s->first_slice_line=0;
2396 static int mb_var_thread(AVCodecContext *c, void *arg){
2397 MpegEncContext *s= *(void**)arg;
2400 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2401 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2404 uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2406 int sum = s->dsp.pix_sum(pix, s->linesize);
2408 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500 + 128)>>8;
2410 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2411 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2412 s->me.mb_var_sum_temp += varc;
2418 static void write_slice_end(MpegEncContext *s){
2419 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2420 if(s->partitioned_frame){
2421 ff_mpeg4_merge_partitions(s);
2424 ff_mpeg4_stuffing(&s->pb);
2425 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2426 ff_mjpeg_encode_stuffing(&s->pb);
2429 avpriv_align_put_bits(&s->pb);
2430 flush_put_bits(&s->pb);
2432 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2433 s->misc_bits+= get_bits_diff(s);
2436 static void write_mb_info(MpegEncContext *s)
2438 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2439 int offset = put_bits_count(&s->pb);
2440 int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2441 int gobn = s->mb_y / s->gob_index;
2443 if (CONFIG_H263_ENCODER)
2444 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2445 bytestream_put_le32(&ptr, offset);
2446 bytestream_put_byte(&ptr, s->qscale);
2447 bytestream_put_byte(&ptr, gobn);
2448 bytestream_put_le16(&ptr, mba);
2449 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2450 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2451 /* 4MV not implemented */
2452 bytestream_put_byte(&ptr, 0); /* hmv2 */
2453 bytestream_put_byte(&ptr, 0); /* vmv2 */
2456 static void update_mb_info(MpegEncContext *s, int startcode)
2460 if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2461 s->mb_info_size += 12;
2462 s->prev_mb_info = s->last_mb_info;
2465 s->prev_mb_info = put_bits_count(&s->pb)/8;
2466 /* This might have incremented mb_info_size above, and we return without
2467 * actually writing any info into that slot yet. But in that case,
2468 * this will be called again at the start of the after writing the
2469 * start code, actually writing the mb info. */
2473 s->last_mb_info = put_bits_count(&s->pb)/8;
2474 if (!s->mb_info_size)
2475 s->mb_info_size += 12;
2479 static int encode_thread(AVCodecContext *c, void *arg){
2480 MpegEncContext *s= *(void**)arg;
2481 int mb_x, mb_y, pdif = 0;
2482 int chr_h= 16>>s->chroma_y_shift;
2484 MpegEncContext best_s, backup_s;
2485 uint8_t bit_buf[2][MAX_MB_BYTES];
2486 uint8_t bit_buf2[2][MAX_MB_BYTES];
2487 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2488 PutBitContext pb[2], pb2[2], tex_pb[2];
2491 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2492 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2493 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2496 s->last_bits= put_bits_count(&s->pb);
2507 /* init last dc values */
2508 /* note: quant matrix value (8) is implied here */
2509 s->last_dc[i] = 128 << s->intra_dc_precision;
2511 s->current_picture.f->error[i] = 0;
2514 memset(s->last_mv, 0, sizeof(s->last_mv));
2518 switch(s->codec_id){
2519 case AV_CODEC_ID_H263:
2520 case AV_CODEC_ID_H263P:
2521 case AV_CODEC_ID_FLV1:
2522 if (CONFIG_H263_ENCODER)
2523 s->gob_index = ff_h263_get_gob_height(s);
2525 case AV_CODEC_ID_MPEG4:
2526 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2527 ff_mpeg4_init_partitions(s);
2533 s->first_slice_line = 1;
2534 s->ptr_lastgob = s->pb.buf;
2535 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2539 ff_set_qscale(s, s->qscale);
2540 ff_init_block_index(s);
2542 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2543 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2544 int mb_type= s->mb_type[xy];
2549 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2550 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2553 if(s->data_partitioning){
2554 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2555 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2556 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2562 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2563 ff_update_block_index(s);
2565 if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2566 ff_h261_reorder_mb_index(s);
2567 xy= s->mb_y*s->mb_stride + s->mb_x;
2568 mb_type= s->mb_type[xy];
2571 /* write gob / video packet header */
2573 int current_packet_size, is_gob_start;
2575 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2577 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2579 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2581 switch(s->codec_id){
2582 case AV_CODEC_ID_H263:
2583 case AV_CODEC_ID_H263P:
2584 if(!s->h263_slice_structured)
2585 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2587 case AV_CODEC_ID_MPEG2VIDEO:
2588 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2589 case AV_CODEC_ID_MPEG1VIDEO:
2590 if(s->mb_skip_run) is_gob_start=0;
2595 if(s->start_mb_y != mb_y || mb_x!=0){
2598 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2599 ff_mpeg4_init_partitions(s);
2603 assert((put_bits_count(&s->pb)&7) == 0);
2604 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2606 if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2607 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2608 int d = 100 / s->error_rate;
2610 current_packet_size=0;
2611 s->pb.buf_ptr= s->ptr_lastgob;
2612 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2616 if (s->avctx->rtp_callback){
2617 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2618 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2620 update_mb_info(s, 1);
2622 switch(s->codec_id){
2623 case AV_CODEC_ID_MPEG4:
2624 if (CONFIG_MPEG4_ENCODER) {
2625 ff_mpeg4_encode_video_packet_header(s);
2626 ff_mpeg4_clean_buffers(s);
2629 case AV_CODEC_ID_MPEG1VIDEO:
2630 case AV_CODEC_ID_MPEG2VIDEO:
2631 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2632 ff_mpeg1_encode_slice_header(s);
2633 ff_mpeg1_clean_buffers(s);
2636 case AV_CODEC_ID_H263:
2637 case AV_CODEC_ID_H263P:
2638 if (CONFIG_H263_ENCODER)
2639 ff_h263_encode_gob_header(s, mb_y);
2643 if(s->flags&CODEC_FLAG_PASS1){
2644 int bits= put_bits_count(&s->pb);
2645 s->misc_bits+= bits - s->last_bits;
2649 s->ptr_lastgob += current_packet_size;
2650 s->first_slice_line=1;
2651 s->resync_mb_x=mb_x;
2652 s->resync_mb_y=mb_y;
2656 if( (s->resync_mb_x == s->mb_x)
2657 && s->resync_mb_y+1 == s->mb_y){
2658 s->first_slice_line=0;
2662 s->dquant=0; //only for QP_RD
2664 update_mb_info(s, 0);
2666 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
2668 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2670 copy_context_before_encode(&backup_s, s, -1);
2672 best_s.data_partitioning= s->data_partitioning;
2673 best_s.partitioned_frame= s->partitioned_frame;
2674 if(s->data_partitioning){
2675 backup_s.pb2= s->pb2;
2676 backup_s.tex_pb= s->tex_pb;
2679 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2680 s->mv_dir = MV_DIR_FORWARD;
2681 s->mv_type = MV_TYPE_16X16;
2683 s->mv[0][0][0] = s->p_mv_table[xy][0];
2684 s->mv[0][0][1] = s->p_mv_table[xy][1];
2685 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2686 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2688 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2689 s->mv_dir = MV_DIR_FORWARD;
2690 s->mv_type = MV_TYPE_FIELD;
2693 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2694 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2695 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2697 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2698 &dmin, &next_block, 0, 0);
2700 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2701 s->mv_dir = MV_DIR_FORWARD;
2702 s->mv_type = MV_TYPE_16X16;
2706 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2707 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2709 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2710 s->mv_dir = MV_DIR_FORWARD;
2711 s->mv_type = MV_TYPE_8X8;
2714 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2715 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2717 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2718 &dmin, &next_block, 0, 0);
2720 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2721 s->mv_dir = MV_DIR_FORWARD;
2722 s->mv_type = MV_TYPE_16X16;
2724 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2725 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2726 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2727 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2729 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2730 s->mv_dir = MV_DIR_BACKWARD;
2731 s->mv_type = MV_TYPE_16X16;
2733 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2734 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2735 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2736 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2738 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2739 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2740 s->mv_type = MV_TYPE_16X16;
2742 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2743 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2744 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2745 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2746 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2747 &dmin, &next_block, 0, 0);
2749 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2750 s->mv_dir = MV_DIR_FORWARD;
2751 s->mv_type = MV_TYPE_FIELD;
2754 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2755 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2756 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2758 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2759 &dmin, &next_block, 0, 0);
2761 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2762 s->mv_dir = MV_DIR_BACKWARD;
2763 s->mv_type = MV_TYPE_FIELD;
2766 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2767 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2768 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2770 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2771 &dmin, &next_block, 0, 0);
2773 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2774 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2775 s->mv_type = MV_TYPE_FIELD;
2777 for(dir=0; dir<2; dir++){
2779 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2780 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2781 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2784 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2785 &dmin, &next_block, 0, 0);
2787 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2789 s->mv_type = MV_TYPE_16X16;
2793 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2794 &dmin, &next_block, 0, 0);
2795 if(s->h263_pred || s->h263_aic){
2797 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2799 ff_clean_intra_table_entries(s); //old mode?
2803 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2804 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2805 const int last_qp= backup_s.qscale;
2808 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2809 static const int dquant_tab[4]={-1,1,-2,2};
2811 assert(backup_s.dquant == 0);
2814 s->mv_dir= best_s.mv_dir;
2815 s->mv_type = MV_TYPE_16X16;
2816 s->mb_intra= best_s.mb_intra;
2817 s->mv[0][0][0] = best_s.mv[0][0][0];
2818 s->mv[0][0][1] = best_s.mv[0][0][1];
2819 s->mv[1][0][0] = best_s.mv[1][0][0];
2820 s->mv[1][0][1] = best_s.mv[1][0][1];
2822 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
2823 for(; qpi<4; qpi++){
2824 int dquant= dquant_tab[qpi];
2825 qp= last_qp + dquant;
2826 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2828 backup_s.dquant= dquant;
2829 if(s->mb_intra && s->dc_val[0]){
2831 dc[i]= s->dc_val[0][ s->block_index[i] ];
2832 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
2836 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2837 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2838 if(best_s.qscale != qp){
2839 if(s->mb_intra && s->dc_val[0]){
2841 s->dc_val[0][ s->block_index[i] ]= dc[i];
2842 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
2849 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2850 int mx= s->b_direct_mv_table[xy][0];
2851 int my= s->b_direct_mv_table[xy][1];
2853 backup_s.dquant = 0;
2854 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2856 ff_mpeg4_set_direct_mv(s, mx, my);
2857 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2858 &dmin, &next_block, mx, my);
2860 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2861 backup_s.dquant = 0;
2862 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2864 ff_mpeg4_set_direct_mv(s, 0, 0);
2865 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2866 &dmin, &next_block, 0, 0);
2868 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
2871 coded |= s->block_last_index[i];
2874 memcpy(s->mv, best_s.mv, sizeof(s->mv));
2875 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2876 mx=my=0; //FIXME find the one we actually used
2877 ff_mpeg4_set_direct_mv(s, mx, my);
2878 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2886 s->mv_dir= best_s.mv_dir;
2887 s->mv_type = best_s.mv_type;
2889 /* s->mv[0][0][0] = best_s.mv[0][0][0];
2890 s->mv[0][0][1] = best_s.mv[0][0][1];
2891 s->mv[1][0][0] = best_s.mv[1][0][0];
2892 s->mv[1][0][1] = best_s.mv[1][0][1];*/
2895 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2896 &dmin, &next_block, mx, my);
2901 s->current_picture.qscale_table[xy] = best_s.qscale;
2903 copy_context_after_encode(s, &best_s, -1);
2905 pb_bits_count= put_bits_count(&s->pb);
2906 flush_put_bits(&s->pb);
2907 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2910 if(s->data_partitioning){
2911 pb2_bits_count= put_bits_count(&s->pb2);
2912 flush_put_bits(&s->pb2);
2913 avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2914 s->pb2= backup_s.pb2;
2916 tex_pb_bits_count= put_bits_count(&s->tex_pb);
2917 flush_put_bits(&s->tex_pb);
2918 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2919 s->tex_pb= backup_s.tex_pb;
2921 s->last_bits= put_bits_count(&s->pb);
2923 if (CONFIG_H263_ENCODER &&
2924 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2925 ff_h263_update_motion_val(s);
2927 if(next_block==0){ //FIXME 16 vs linesize16
2928 s->hdsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
2929 s->hdsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
2930 s->hdsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2933 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2934 ff_MPV_decode_mb(s, s->block);
2936 int motion_x = 0, motion_y = 0;
2937 s->mv_type=MV_TYPE_16X16;
2938 // only one MB-Type possible
2941 case CANDIDATE_MB_TYPE_INTRA:
2944 motion_x= s->mv[0][0][0] = 0;
2945 motion_y= s->mv[0][0][1] = 0;
2947 case CANDIDATE_MB_TYPE_INTER:
2948 s->mv_dir = MV_DIR_FORWARD;
2950 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2951 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2953 case CANDIDATE_MB_TYPE_INTER_I:
2954 s->mv_dir = MV_DIR_FORWARD;
2955 s->mv_type = MV_TYPE_FIELD;
2958 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2959 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2960 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2963 case CANDIDATE_MB_TYPE_INTER4V:
2964 s->mv_dir = MV_DIR_FORWARD;
2965 s->mv_type = MV_TYPE_8X8;
2968 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2969 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2972 case CANDIDATE_MB_TYPE_DIRECT:
2973 if (CONFIG_MPEG4_ENCODER) {
2974 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2976 motion_x=s->b_direct_mv_table[xy][0];
2977 motion_y=s->b_direct_mv_table[xy][1];
2978 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
2981 case CANDIDATE_MB_TYPE_DIRECT0:
2982 if (CONFIG_MPEG4_ENCODER) {
2983 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2985 ff_mpeg4_set_direct_mv(s, 0, 0);
2988 case CANDIDATE_MB_TYPE_BIDIR:
2989 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2991 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2992 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2993 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2994 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2996 case CANDIDATE_MB_TYPE_BACKWARD:
2997 s->mv_dir = MV_DIR_BACKWARD;
2999 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3000 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3002 case CANDIDATE_MB_TYPE_FORWARD:
3003 s->mv_dir = MV_DIR_FORWARD;
3005 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3006 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3008 case CANDIDATE_MB_TYPE_FORWARD_I:
3009 s->mv_dir = MV_DIR_FORWARD;
3010 s->mv_type = MV_TYPE_FIELD;
3013 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3014 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3015 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3018 case CANDIDATE_MB_TYPE_BACKWARD_I:
3019 s->mv_dir = MV_DIR_BACKWARD;
3020 s->mv_type = MV_TYPE_FIELD;
3023 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3024 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3025 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3028 case CANDIDATE_MB_TYPE_BIDIR_I:
3029 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3030 s->mv_type = MV_TYPE_FIELD;
3032 for(dir=0; dir<2; dir++){
3034 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3035 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3036 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3041 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3044 encode_mb(s, motion_x, motion_y);
3046 // RAL: Update last macroblock type
3047 s->last_mv_dir = s->mv_dir;
3049 if (CONFIG_H263_ENCODER &&
3050 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3051 ff_h263_update_motion_val(s);
3053 ff_MPV_decode_mb(s, s->block);
3056 /* clean the MV table in IPS frames for direct mode in B frames */
3057 if(s->mb_intra /* && I,P,S_TYPE */){
3058 s->p_mv_table[xy][0]=0;
3059 s->p_mv_table[xy][1]=0;
3062 if(s->flags&CODEC_FLAG_PSNR){
3066 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3067 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3069 s->current_picture.f->error[0] += sse(
3070 s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3071 s->dest[0], w, h, s->linesize);
3072 s->current_picture.f->error[1] += sse(
3073 s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3074 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3075 s->current_picture.f->error[2] += sse(
3076 s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3077 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3080 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3081 ff_h263_loop_filter(s);
3083 av_dlog(s->avctx, "MB %d %d bits\n",
3084 s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3088 //not beautiful here but we must write it before flushing so it has to be here
3089 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3090 ff_msmpeg4_encode_ext_header(s);
3094 /* Send the last GOB if RTP */
3095 if (s->avctx->rtp_callback) {
3096 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3097 pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3098 /* Call the RTP callback to send the last GOB */
3100 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3106 #define MERGE(field) dst->field += src->field; src->field=0
3107 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3108 MERGE(me.scene_change_score);
3109 MERGE(me.mc_mb_var_sum_temp);
3110 MERGE(me.mb_var_sum_temp);
3113 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3116 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3117 MERGE(dct_count[1]);
3126 MERGE(er.error_count);
3127 MERGE(padding_bug_score);
3128 MERGE(current_picture.f->error[0]);
3129 MERGE(current_picture.f->error[1]);
3130 MERGE(current_picture.f->error[2]);
3132 if(dst->avctx->noise_reduction){
3133 for(i=0; i<64; i++){
3134 MERGE(dct_error_sum[0][i]);
3135 MERGE(dct_error_sum[1][i]);
3139 assert(put_bits_count(&src->pb) % 8 ==0);
3140 assert(put_bits_count(&dst->pb) % 8 ==0);
3141 avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3142 flush_put_bits(&dst->pb);
3145 static int estimate_qp(MpegEncContext *s, int dry_run){
3146 if (s->next_lambda){
3147 s->current_picture_ptr->f->quality =
3148 s->current_picture.f->quality = s->next_lambda;
3149 if(!dry_run) s->next_lambda= 0;
3150 } else if (!s->fixed_qscale) {
3151 s->current_picture_ptr->f->quality =
3152 s->current_picture.f->quality = ff_rate_estimate_qscale(s, dry_run);
3153 if (s->current_picture.f->quality < 0)
3157 if(s->adaptive_quant){
3158 switch(s->codec_id){
3159 case AV_CODEC_ID_MPEG4:
3160 if (CONFIG_MPEG4_ENCODER)
3161 ff_clean_mpeg4_qscales(s);
3163 case AV_CODEC_ID_H263:
3164 case AV_CODEC_ID_H263P:
3165 case AV_CODEC_ID_FLV1:
3166 if (CONFIG_H263_ENCODER)
3167 ff_clean_h263_qscales(s);
3170 ff_init_qscale_tab(s);
3173 s->lambda= s->lambda_table[0];
3176 s->lambda = s->current_picture.f->quality;
3181 /* must be called before writing the header */
3182 static void set_frame_distances(MpegEncContext * s){
3183 assert(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
3184 s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3186 if(s->pict_type==AV_PICTURE_TYPE_B){
3187 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3188 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3190 s->pp_time= s->time - s->last_non_b_time;
3191 s->last_non_b_time= s->time;
3192 assert(s->picture_number==0 || s->pp_time > 0);
3196 static int encode_picture(MpegEncContext *s, int picture_number)
3200 int context_count = s->slice_context_count;
3202 s->picture_number = picture_number;
3204 /* Reset the average MB variance */
3205 s->me.mb_var_sum_temp =
3206 s->me.mc_mb_var_sum_temp = 0;
3208 /* we need to initialize some time vars before we can encode b-frames */
3209 // RAL: Condition added for MPEG1VIDEO
3210 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3211 set_frame_distances(s);
3212 if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3213 ff_set_mpeg4_time(s);
3215 s->me.scene_change_score=0;
3217 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3219 if(s->pict_type==AV_PICTURE_TYPE_I){
3220 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3221 else s->no_rounding=0;
3222 }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3223 if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3224 s->no_rounding ^= 1;
3227 if(s->flags & CODEC_FLAG_PASS2){
3228 if (estimate_qp(s,1) < 0)
3230 ff_get_2pass_fcode(s);
3231 }else if(!(s->flags & CODEC_FLAG_QSCALE)){
3232 if(s->pict_type==AV_PICTURE_TYPE_B)
3233 s->lambda= s->last_lambda_for[s->pict_type];
3235 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3239 s->mb_intra=0; //for the rate distortion & bit compare functions
3240 for(i=1; i<context_count; i++){
3241 ret = ff_update_duplicate_context(s->thread_context[i], s);
3249 /* Estimate motion for every MB */
3250 if(s->pict_type != AV_PICTURE_TYPE_I){
3251 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3252 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
3253 if (s->pict_type != AV_PICTURE_TYPE_B) {
3254 if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
3255 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3259 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3260 }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3262 for(i=0; i<s->mb_stride*s->mb_height; i++)
3263 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3265 if(!s->fixed_qscale){
3266 /* finding spatial complexity for I-frame rate control */
3267 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3270 for(i=1; i<context_count; i++){
3271 merge_context_after_me(s, s->thread_context[i]);
3273 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3274 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
3277 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
3278 s->pict_type= AV_PICTURE_TYPE_I;
3279 for(i=0; i<s->mb_stride*s->mb_height; i++)
3280 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3281 av_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
3282 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3286 if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3287 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3289 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3291 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3292 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3293 s->f_code= FFMAX3(s->f_code, a, b);
3296 ff_fix_long_p_mvs(s);
3297 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3298 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3302 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3303 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3308 if(s->pict_type==AV_PICTURE_TYPE_B){
3311 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3312 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3313 s->f_code = FFMAX(a, b);
3315 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3316 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3317 s->b_code = FFMAX(a, b);
3319 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3320 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3321 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3322 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3323 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3325 for(dir=0; dir<2; dir++){
3328 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3329 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3330 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3331 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3339 if (estimate_qp(s, 0) < 0)
3342 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==AV_PICTURE_TYPE_I && !(s->flags & CODEC_FLAG_QSCALE))
3343 s->qscale= 3; //reduce clipping problems
3345 if (s->out_format == FMT_MJPEG) {
3346 /* for mjpeg, we do include qscale in the matrix */
3348 int j= s->dsp.idct_permutation[i];
3350 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3352 s->y_dc_scale_table=
3353 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3354 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3355 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
3356 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3360 //FIXME var duplication
3361 s->current_picture_ptr->f->key_frame =
3362 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3363 s->current_picture_ptr->f->pict_type =
3364 s->current_picture.f->pict_type = s->pict_type;
3366 if (s->current_picture.f->key_frame)
3367 s->picture_in_gop_number=0;
3369 s->last_bits= put_bits_count(&s->pb);
3370 switch(s->out_format) {
3372 if (CONFIG_MJPEG_ENCODER)
3373 ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
3377 if (CONFIG_H261_ENCODER)
3378 ff_h261_encode_picture_header(s, picture_number);
3381 if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3382 ff_wmv2_encode_picture_header(s, picture_number);
3383 else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3384 ff_msmpeg4_encode_picture_header(s, picture_number);
3385 else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3386 ff_mpeg4_encode_picture_header(s, picture_number);
3387 else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10)
3388 ff_rv10_encode_picture_header(s, picture_number);
3389 else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3390 ff_rv20_encode_picture_header(s, picture_number);
3391 else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3392 ff_flv_encode_picture_header(s, picture_number);
3393 else if (CONFIG_H263_ENCODER)
3394 ff_h263_encode_picture_header(s, picture_number);
3397 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3398 ff_mpeg1_encode_picture_header(s, picture_number);
3403 bits= put_bits_count(&s->pb);
3404 s->header_bits= bits - s->last_bits;
3406 for(i=1; i<context_count; i++){
3407 update_duplicate_context_after_me(s->thread_context[i], s);
3409 s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3410 for(i=1; i<context_count; i++){
3411 merge_context_after_encode(s, s->thread_context[i]);
3417 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3418 const int intra= s->mb_intra;
3421 s->dct_count[intra]++;
3423 for(i=0; i<64; i++){
3424 int level= block[i];
3428 s->dct_error_sum[intra][i] += level;
3429 level -= s->dct_offset[intra][i];
3430 if(level<0) level=0;
3432 s->dct_error_sum[intra][i] -= level;
3433 level += s->dct_offset[intra][i];
3434 if(level>0) level=0;
3441 static int dct_quantize_trellis_c(MpegEncContext *s,
3442 int16_t *block, int n,
3443 int qscale, int *overflow){
3445 const uint8_t *scantable= s->intra_scantable.scantable;
3446 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3448 unsigned int threshold1, threshold2;
3460 int coeff_count[64];
3461 int qmul, qadd, start_i, last_non_zero, i, dc;
3462 const int esc_length= s->ac_esc_length;
3464 uint8_t * last_length;
3465 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3467 s->dsp.fdct (block);
3469 if(s->dct_error_sum)
3470 s->denoise_dct(s, block);
3472 qadd= ((qscale-1)|1)*8;
3483 /* For AIC we skip quant/dequant of INTRADC */
3488 /* note: block[0] is assumed to be positive */
3489 block[0] = (block[0] + (q >> 1)) / q;
3492 qmat = s->q_intra_matrix[qscale];
3493 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3494 bias= 1<<(QMAT_SHIFT-1);
3495 length = s->intra_ac_vlc_length;
3496 last_length= s->intra_ac_vlc_last_length;
3500 qmat = s->q_inter_matrix[qscale];
3501 length = s->inter_ac_vlc_length;
3502 last_length= s->inter_ac_vlc_last_length;
3506 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3507 threshold2= (threshold1<<1);
3509 for(i=63; i>=start_i; i--) {
3510 const int j = scantable[i];
3511 int level = block[j] * qmat[j];
3513 if(((unsigned)(level+threshold1))>threshold2){
3519 for(i=start_i; i<=last_non_zero; i++) {
3520 const int j = scantable[i];
3521 int level = block[j] * qmat[j];
3523 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3524 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3525 if(((unsigned)(level+threshold1))>threshold2){
3527 level= (bias + level)>>QMAT_SHIFT;
3529 coeff[1][i]= level-1;
3530 // coeff[2][k]= level-2;
3532 level= (bias - level)>>QMAT_SHIFT;
3533 coeff[0][i]= -level;
3534 coeff[1][i]= -level+1;
3535 // coeff[2][k]= -level+2;
3537 coeff_count[i]= FFMIN(level, 2);
3538 assert(coeff_count[i]);
3541 coeff[0][i]= (level>>31)|1;
3546 *overflow= s->max_qcoeff < max; //overflow might have happened
3548 if(last_non_zero < start_i){
3549 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3550 return last_non_zero;
3553 score_tab[start_i]= 0;
3554 survivor[0]= start_i;
3557 for(i=start_i; i<=last_non_zero; i++){
3558 int level_index, j, zero_distortion;
3559 int dct_coeff= FFABS(block[ scantable[i] ]);
3560 int best_score=256*256*256*120;
3562 if (s->dsp.fdct == ff_fdct_ifast)
3563 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3564 zero_distortion= dct_coeff*dct_coeff;
3566 for(level_index=0; level_index < coeff_count[i]; level_index++){
3568 int level= coeff[level_index][i];
3569 const int alevel= FFABS(level);
3574 if(s->out_format == FMT_H263){
3575 unquant_coeff= alevel*qmul + qadd;
3577 j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
3579 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
3580 unquant_coeff = (unquant_coeff - 1) | 1;
3582 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3583 unquant_coeff = (unquant_coeff - 1) | 1;
3588 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3590 if((level&(~127)) == 0){
3591 for(j=survivor_count-1; j>=0; j--){
3592 int run= i - survivor[j];
3593 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3594 score += score_tab[i-run];
3596 if(score < best_score){
3599 level_tab[i+1]= level-64;
3603 if(s->out_format == FMT_H263){
3604 for(j=survivor_count-1; j>=0; j--){
3605 int run= i - survivor[j];
3606 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3607 score += score_tab[i-run];
3608 if(score < last_score){
3611 last_level= level-64;
3617 distortion += esc_length*lambda;
3618 for(j=survivor_count-1; j>=0; j--){
3619 int run= i - survivor[j];
3620 int score= distortion + score_tab[i-run];
3622 if(score < best_score){
3625 level_tab[i+1]= level-64;
3629 if(s->out_format == FMT_H263){
3630 for(j=survivor_count-1; j>=0; j--){
3631 int run= i - survivor[j];
3632 int score= distortion + score_tab[i-run];
3633 if(score < last_score){
3636 last_level= level-64;
3644 score_tab[i+1]= best_score;
3646 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3647 if(last_non_zero <= 27){
3648 for(; survivor_count; survivor_count--){
3649 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3653 for(; survivor_count; survivor_count--){
3654 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3659 survivor[ survivor_count++ ]= i+1;
3662 if(s->out_format != FMT_H263){
3663 last_score= 256*256*256*120;
3664 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3665 int score= score_tab[i];
3666 if(i) score += lambda*2; //FIXME exacter?
3668 if(score < last_score){
3671 last_level= level_tab[i];
3672 last_run= run_tab[i];
3677 s->coded_score[n] = last_score;
3679 dc= FFABS(block[0]);
3680 last_non_zero= last_i - 1;
3681 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3683 if(last_non_zero < start_i)
3684 return last_non_zero;
3686 if(last_non_zero == 0 && start_i == 0){
3688 int best_score= dc * dc;
3690 for(i=0; i<coeff_count[0]; i++){
3691 int level= coeff[i][0];
3692 int alevel= FFABS(level);
3693 int unquant_coeff, score, distortion;
3695 if(s->out_format == FMT_H263){
3696 unquant_coeff= (alevel*qmul + qadd)>>3;
3698 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3699 unquant_coeff = (unquant_coeff - 1) | 1;
3701 unquant_coeff = (unquant_coeff + 4) >> 3;
3702 unquant_coeff<<= 3 + 3;
3704 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3706 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3707 else score= distortion + esc_length*lambda;
3709 if(score < best_score){
3711 best_level= level - 64;
3714 block[0]= best_level;
3715 s->coded_score[n] = best_score - dc*dc;
3716 if(best_level == 0) return -1;
3717 else return last_non_zero;
3723 block[ perm_scantable[last_non_zero] ]= last_level;
3726 for(; i>start_i; i -= run_tab[i] + 1){
3727 block[ perm_scantable[i-1] ]= level_tab[i];
3730 return last_non_zero;
3733 //#define REFINE_STATS 1
3734 static int16_t basis[64][64];
3736 static void build_basis(uint8_t *perm){
3743 double s= 0.25*(1<<BASIS_SHIFT);
3745 int perm_index= perm[index];
3746 if(i==0) s*= sqrt(0.5);
3747 if(j==0) s*= sqrt(0.5);
3748 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)));
3755 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3756 int16_t *block, int16_t *weight, int16_t *orig,
3759 LOCAL_ALIGNED_16(int16_t, d1, [64]);
3760 const uint8_t *scantable= s->intra_scantable.scantable;
3761 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3762 // unsigned int threshold1, threshold2;
3767 int qmul, qadd, start_i, last_non_zero, i, dc;
3769 uint8_t * last_length;
3771 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3774 static int after_last=0;
3775 static int to_zero=0;
3776 static int from_zero=0;
3779 static int messed_sign=0;
3782 if(basis[0][0] == 0)
3783 build_basis(s->dsp.idct_permutation);
3794 /* For AIC we skip quant/dequant of INTRADC */
3798 q <<= RECON_SHIFT-3;
3799 /* note: block[0] is assumed to be positive */
3801 // block[0] = (block[0] + (q >> 1)) / q;
3803 // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3804 // bias= 1<<(QMAT_SHIFT-1);
3805 length = s->intra_ac_vlc_length;
3806 last_length= s->intra_ac_vlc_last_length;
3810 length = s->inter_ac_vlc_length;
3811 last_length= s->inter_ac_vlc_last_length;
3813 last_non_zero = s->block_last_index[n];
3818 dc += (1<<(RECON_SHIFT-1));
3819 for(i=0; i<64; i++){
3820 rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
3823 STOP_TIMER("memset rem[]")}
3826 for(i=0; i<64; i++){
3831 w= FFABS(weight[i]) + qns*one;
3832 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3835 // w=weight[i] = (63*qns + (w/2)) / w;
3841 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3847 for(i=start_i; i<=last_non_zero; i++){
3848 int j= perm_scantable[i];
3849 const int level= block[j];
3853 if(level<0) coeff= qmul*level - qadd;
3854 else coeff= qmul*level + qadd;
3855 run_tab[rle_index++]=run;
3858 s->dsp.add_8x8basis(rem, basis[j], coeff);
3864 if(last_non_zero>0){
3865 STOP_TIMER("init rem[]")
3872 int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
3875 int run2, best_unquant_change=0, analyze_gradient;
3879 analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
3881 if(analyze_gradient){
3885 for(i=0; i<64; i++){
3888 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
3891 STOP_TIMER("rem*w*w")}
3901 const int level= block[0];
3902 int change, old_coeff;
3904 assert(s->mb_intra);
3908 for(change=-1; change<=1; change+=2){
3909 int new_level= level + change;
3910 int score, new_coeff;
3912 new_coeff= q*new_level;
3913 if(new_coeff >= 2048 || new_coeff < 0)
3916 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3917 if(score<best_score){
3920 best_change= change;
3921 best_unquant_change= new_coeff - old_coeff;
3928 run2= run_tab[rle_index++];
3932 for(i=start_i; i<64; i++){
3933 int j= perm_scantable[i];
3934 const int level= block[j];
3935 int change, old_coeff;
3937 if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
3941 if(level<0) old_coeff= qmul*level - qadd;
3942 else old_coeff= qmul*level + qadd;
3943 run2= run_tab[rle_index++]; //FIXME ! maybe after last
3947 assert(run2>=0 || i >= last_non_zero );
3950 for(change=-1; change<=1; change+=2){
3951 int new_level= level + change;
3952 int score, new_coeff, unquant_change;
3955 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
3959 if(new_level<0) new_coeff= qmul*new_level - qadd;
3960 else new_coeff= qmul*new_level + qadd;
3961 if(new_coeff >= 2048 || new_coeff <= -2048)
3963 //FIXME check for overflow
3966 if(level < 63 && level > -63){
3967 if(i < last_non_zero)
3968 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
3969 - length[UNI_AC_ENC_INDEX(run, level+64)];
3971 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
3972 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
3975 assert(FFABS(new_level)==1);
3977 if(analyze_gradient){
3978 int g= d1[ scantable[i] ];
3979 if(g && (g^new_level) >= 0)
3983 if(i < last_non_zero){
3984 int next_i= i + run2 + 1;
3985 int next_level= block[ perm_scantable[next_i] ] + 64;
3987 if(next_level&(~127))
3990 if(next_i < last_non_zero)
3991 score += length[UNI_AC_ENC_INDEX(run, 65)]
3992 + length[UNI_AC_ENC_INDEX(run2, next_level)]
3993 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
3995 score += length[UNI_AC_ENC_INDEX(run, 65)]
3996 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
3997 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
3999 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4001 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4002 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4008 assert(FFABS(level)==1);
4010 if(i < last_non_zero){
4011 int next_i= i + run2 + 1;
4012 int next_level= block[ perm_scantable[next_i] ] + 64;
4014 if(next_level&(~127))
4017 if(next_i < last_non_zero)
4018 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4019 - length[UNI_AC_ENC_INDEX(run2, next_level)]
4020 - length[UNI_AC_ENC_INDEX(run, 65)];
4022 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4023 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4024 - length[UNI_AC_ENC_INDEX(run, 65)];
4026 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4028 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4029 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4036 unquant_change= new_coeff - old_coeff;
4037 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
4039 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
4040 if(score<best_score){
4043 best_change= change;
4044 best_unquant_change= unquant_change;
4048 prev_level= level + 64;
4049 if(prev_level&(~127))
4058 STOP_TIMER("iterative step")}
4062 int j= perm_scantable[ best_coeff ];
4064 block[j] += best_change;
4066 if(best_coeff > last_non_zero){
4067 last_non_zero= best_coeff;
4075 if(block[j] - best_change){
4076 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
4088 for(; last_non_zero>=start_i; last_non_zero--){
4089 if(block[perm_scantable[last_non_zero]])
4095 if(256*256*256*64 % count == 0){
4096 printf("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);
4101 for(i=start_i; i<=last_non_zero; i++){
4102 int j= perm_scantable[i];
4103 const int level= block[j];
4106 run_tab[rle_index++]=run;
4113 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
4119 if(last_non_zero>0){
4120 STOP_TIMER("iterative search")
4125 return last_non_zero;
4128 int ff_dct_quantize_c(MpegEncContext *s,
4129 int16_t *block, int n,
4130 int qscale, int *overflow)
4132 int i, j, level, last_non_zero, q, start_i;
4134 const uint8_t *scantable= s->intra_scantable.scantable;
4137 unsigned int threshold1, threshold2;
4139 s->dsp.fdct (block);
4141 if(s->dct_error_sum)
4142 s->denoise_dct(s, block);
4152 /* For AIC we skip quant/dequant of INTRADC */
4155 /* note: block[0] is assumed to be positive */
4156 block[0] = (block[0] + (q >> 1)) / q;
4159 qmat = s->q_intra_matrix[qscale];
4160 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4164 qmat = s->q_inter_matrix[qscale];
4165 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4167 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4168 threshold2= (threshold1<<1);
4169 for(i=63;i>=start_i;i--) {
4171 level = block[j] * qmat[j];
4173 if(((unsigned)(level+threshold1))>threshold2){
4180 for(i=start_i; i<=last_non_zero; i++) {
4182 level = block[j] * qmat[j];
4184 // if( bias+level >= (1<<QMAT_SHIFT)
4185 // || bias-level >= (1<<QMAT_SHIFT)){
4186 if(((unsigned)(level+threshold1))>threshold2){
4188 level= (bias + level)>>QMAT_SHIFT;
4191 level= (bias - level)>>QMAT_SHIFT;
4199 *overflow= s->max_qcoeff < max; //overflow might have happened
4201 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4202 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
4203 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
4205 return last_non_zero;
4208 #define OFFSET(x) offsetof(MpegEncContext, x)
4209 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4210 static const AVOption h263_options[] = {
4211 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4212 { "structured_slices","Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE},
4213 { "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 },
4218 static const AVClass h263_class = {
4219 .class_name = "H.263 encoder",
4220 .item_name = av_default_item_name,
4221 .option = h263_options,
4222 .version = LIBAVUTIL_VERSION_INT,
4225 AVCodec ff_h263_encoder = {
4227 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4228 .type = AVMEDIA_TYPE_VIDEO,
4229 .id = AV_CODEC_ID_H263,
4230 .priv_data_size = sizeof(MpegEncContext),
4231 .init = ff_MPV_encode_init,
4232 .encode2 = ff_MPV_encode_picture,
4233 .close = ff_MPV_encode_end,
4234 .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4235 .priv_class = &h263_class,
4238 static const AVOption h263p_options[] = {
4239 { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4240 { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4241 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4242 { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE},
4246 static const AVClass h263p_class = {
4247 .class_name = "H.263p encoder",
4248 .item_name = av_default_item_name,
4249 .option = h263p_options,
4250 .version = LIBAVUTIL_VERSION_INT,
4253 AVCodec ff_h263p_encoder = {
4255 .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4256 .type = AVMEDIA_TYPE_VIDEO,
4257 .id = AV_CODEC_ID_H263P,
4258 .priv_data_size = sizeof(MpegEncContext),
4259 .init = ff_MPV_encode_init,
4260 .encode2 = ff_MPV_encode_picture,
4261 .close = ff_MPV_encode_end,
4262 .capabilities = CODEC_CAP_SLICE_THREADS,
4263 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4264 .priv_class = &h263p_class,
4267 FF_MPV_GENERIC_CLASS(msmpeg4v2)
4269 AVCodec ff_msmpeg4v2_encoder = {
4270 .name = "msmpeg4v2",
4271 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
4272 .type = AVMEDIA_TYPE_VIDEO,
4273 .id = AV_CODEC_ID_MSMPEG4V2,
4274 .priv_data_size = sizeof(MpegEncContext),
4275 .init = ff_MPV_encode_init,
4276 .encode2 = ff_MPV_encode_picture,
4277 .close = ff_MPV_encode_end,
4278 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4279 .priv_class = &msmpeg4v2_class,
4282 FF_MPV_GENERIC_CLASS(msmpeg4v3)
4284 AVCodec ff_msmpeg4v3_encoder = {
4286 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
4287 .type = AVMEDIA_TYPE_VIDEO,
4288 .id = AV_CODEC_ID_MSMPEG4V3,
4289 .priv_data_size = sizeof(MpegEncContext),
4290 .init = ff_MPV_encode_init,
4291 .encode2 = ff_MPV_encode_picture,
4292 .close = ff_MPV_encode_end,
4293 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4294 .priv_class = &msmpeg4v3_class,
4297 FF_MPV_GENERIC_CLASS(wmv1)
4299 AVCodec ff_wmv1_encoder = {
4301 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
4302 .type = AVMEDIA_TYPE_VIDEO,
4303 .id = AV_CODEC_ID_WMV1,
4304 .priv_data_size = sizeof(MpegEncContext),
4305 .init = ff_MPV_encode_init,
4306 .encode2 = ff_MPV_encode_picture,
4307 .close = ff_MPV_encode_end,
4308 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4309 .priv_class = &wmv1_class,