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"
45 #include "mjpegenc_common.h"
47 #include "mpegutils.h"
50 #include "pixblockdsp.h"
54 #include "aandcttab.h"
56 #include "mpeg4video.h"
58 #include "bytestream.h"
61 #define QUANT_BIAS_SHIFT 8
63 static int encode_picture(MpegEncContext *s, int picture_number);
64 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
65 static int sse_mb(MpegEncContext *s);
66 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
67 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
69 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
70 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
72 const AVOption ff_mpv_generic_options[] = {
77 void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
78 uint16_t (*qmat16)[2][64],
79 const uint16_t *quant_matrix,
80 int bias, int qmin, int qmax, int intra)
82 FDCTDSPContext *fdsp = &s->fdsp;
86 for (qscale = qmin; qscale <= qmax; qscale++) {
88 if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
89 fdsp->fdct == ff_jpeg_fdct_islow_10 ||
90 fdsp->fdct == ff_faandct) {
91 for (i = 0; i < 64; i++) {
92 const int j = s->idsp.idct_permutation[i];
93 /* 16 <= qscale * quant_matrix[i] <= 7905
94 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
95 * 19952 <= x <= 249205026
96 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
97 * 3444240 >= (1 << 36) / (x) >= 275 */
99 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
100 (qscale * quant_matrix[j]));
102 } else if (fdsp->fdct == ff_fdct_ifast) {
103 for (i = 0; i < 64; i++) {
104 const int j = s->idsp.idct_permutation[i];
105 /* 16 <= qscale * quant_matrix[i] <= 7905
106 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
107 * 19952 <= x <= 249205026
108 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
109 * 3444240 >= (1 << 36) / (x) >= 275 */
111 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
112 (ff_aanscales[i] * qscale *
116 for (i = 0; i < 64; i++) {
117 const int j = s->idsp.idct_permutation[i];
118 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
119 * Assume x = qscale * quant_matrix[i]
121 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
122 * so 32768 >= (1 << 19) / (x) >= 67 */
123 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
124 (qscale * quant_matrix[j]));
125 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
126 // (qscale * quant_matrix[i]);
127 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
128 (qscale * quant_matrix[j]);
130 if (qmat16[qscale][0][i] == 0 ||
131 qmat16[qscale][0][i] == 128 * 256)
132 qmat16[qscale][0][i] = 128 * 256 - 1;
133 qmat16[qscale][1][i] =
134 ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
135 qmat16[qscale][0][i]);
139 for (i = intra; i < 64; i++) {
141 if (fdsp->fdct == ff_fdct_ifast) {
142 max = (8191LL * ff_aanscales[i]) >> 14;
144 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
150 av_log(NULL, AV_LOG_INFO,
151 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
156 static inline void update_qscale(MpegEncContext *s)
158 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
159 (FF_LAMBDA_SHIFT + 7);
160 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
162 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
166 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
172 for (i = 0; i < 64; i++) {
173 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
180 * init s->current_picture.qscale_table from s->lambda_table
182 void ff_init_qscale_tab(MpegEncContext *s)
184 int8_t * const qscale_table = s->current_picture.qscale_table;
187 for (i = 0; i < s->mb_num; i++) {
188 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
189 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
190 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
195 static void update_duplicate_context_after_me(MpegEncContext *dst,
198 #define COPY(a) dst->a= src->a
200 COPY(current_picture);
206 COPY(picture_in_gop_number);
207 COPY(gop_picture_number);
208 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
209 COPY(progressive_frame); // FIXME don't set in encode_header
210 COPY(partitioned_frame); // FIXME don't set in encode_header
215 * Set the given MpegEncContext to defaults for encoding.
216 * the changed fields will not depend upon the prior state of the MpegEncContext.
218 static void MPV_encode_defaults(MpegEncContext *s)
221 ff_MPV_common_defaults(s);
223 for (i = -16; i < 16; i++) {
224 default_fcode_tab[i + MAX_MV] = 1;
226 s->me.mv_penalty = default_mv_penalty;
227 s->fcode_tab = default_fcode_tab;
229 s->input_picture_number = 0;
230 s->picture_in_gop_number = 0;
233 /* init video encoder */
234 av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
236 MpegEncContext *s = avctx->priv_data;
237 int i, ret, format_supported;
239 MPV_encode_defaults(s);
241 switch (avctx->codec_id) {
242 case AV_CODEC_ID_MPEG2VIDEO:
243 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
244 avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
245 av_log(avctx, AV_LOG_ERROR,
246 "only YUV420 and YUV422 are supported\n");
250 case AV_CODEC_ID_MJPEG:
251 format_supported = 0;
252 /* JPEG color space */
253 if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
254 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
255 (avctx->color_range == AVCOL_RANGE_JPEG &&
256 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
257 avctx->pix_fmt == AV_PIX_FMT_YUV422P)))
258 format_supported = 1;
259 /* MPEG color space */
260 else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
261 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
262 avctx->pix_fmt == AV_PIX_FMT_YUV422P))
263 format_supported = 1;
265 if (!format_supported) {
266 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
271 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
272 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
277 switch (avctx->pix_fmt) {
278 case AV_PIX_FMT_YUVJ422P:
279 case AV_PIX_FMT_YUV422P:
280 s->chroma_format = CHROMA_422;
282 case AV_PIX_FMT_YUVJ420P:
283 case AV_PIX_FMT_YUV420P:
285 s->chroma_format = CHROMA_420;
289 s->bit_rate = avctx->bit_rate;
290 s->width = avctx->width;
291 s->height = avctx->height;
292 if (avctx->gop_size > 600 &&
293 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
294 av_log(avctx, AV_LOG_ERROR,
295 "Warning keyframe interval too large! reducing it ...\n");
296 avctx->gop_size = 600;
298 s->gop_size = avctx->gop_size;
300 s->flags = avctx->flags;
301 s->flags2 = avctx->flags2;
302 if (avctx->max_b_frames > MAX_B_FRAMES) {
303 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
304 "is %d.\n", MAX_B_FRAMES);
306 s->max_b_frames = avctx->max_b_frames;
307 s->codec_id = avctx->codec->id;
308 s->strict_std_compliance = avctx->strict_std_compliance;
309 s->quarter_sample = (avctx->flags & CODEC_FLAG_QPEL) != 0;
310 s->mpeg_quant = avctx->mpeg_quant;
311 s->rtp_mode = !!avctx->rtp_payload_size;
312 s->intra_dc_precision = avctx->intra_dc_precision;
313 s->user_specified_pts = AV_NOPTS_VALUE;
315 if (s->gop_size <= 1) {
322 s->me_method = avctx->me_method;
325 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
327 s->adaptive_quant = (s->avctx->lumi_masking ||
328 s->avctx->dark_masking ||
329 s->avctx->temporal_cplx_masking ||
330 s->avctx->spatial_cplx_masking ||
331 s->avctx->p_masking ||
332 s->avctx->border_masking ||
333 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
336 s->loop_filter = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
338 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
339 av_log(avctx, AV_LOG_ERROR,
340 "a vbv buffer size is needed, "
341 "for encoding with a maximum bitrate\n");
345 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
346 av_log(avctx, AV_LOG_INFO,
347 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
350 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
351 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
355 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
356 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
360 if (avctx->rc_max_rate &&
361 avctx->rc_max_rate == avctx->bit_rate &&
362 avctx->rc_max_rate != avctx->rc_min_rate) {
363 av_log(avctx, AV_LOG_INFO,
364 "impossible bitrate constraints, this will fail\n");
367 if (avctx->rc_buffer_size &&
368 avctx->bit_rate * (int64_t)avctx->time_base.num >
369 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
370 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
374 if (!s->fixed_qscale &&
375 avctx->bit_rate * av_q2d(avctx->time_base) >
376 avctx->bit_rate_tolerance) {
377 av_log(avctx, AV_LOG_ERROR,
378 "bitrate tolerance too small for bitrate\n");
382 if (s->avctx->rc_max_rate &&
383 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
384 (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
385 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
386 90000LL * (avctx->rc_buffer_size - 1) >
387 s->avctx->rc_max_rate * 0xFFFFLL) {
388 av_log(avctx, AV_LOG_INFO,
389 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
390 "specified vbv buffer is too large for the given bitrate!\n");
393 if ((s->flags & CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
394 s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
395 s->codec_id != AV_CODEC_ID_FLV1) {
396 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
400 if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
401 av_log(avctx, AV_LOG_ERROR,
402 "OBMC is only supported with simple mb decision\n");
406 if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
407 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
411 if (s->max_b_frames &&
412 s->codec_id != AV_CODEC_ID_MPEG4 &&
413 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
414 s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
415 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
419 if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
420 s->codec_id == AV_CODEC_ID_H263 ||
421 s->codec_id == AV_CODEC_ID_H263P) &&
422 (avctx->sample_aspect_ratio.num > 255 ||
423 avctx->sample_aspect_ratio.den > 255)) {
424 av_log(avctx, AV_LOG_ERROR,
425 "Invalid pixel aspect ratio %i/%i, limit is 255/255\n",
426 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
430 if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
431 s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
432 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
436 // FIXME mpeg2 uses that too
437 if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
438 av_log(avctx, AV_LOG_ERROR,
439 "mpeg2 style quantization not supported by codec\n");
443 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
444 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
448 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
449 s->avctx->mb_decision != FF_MB_DECISION_RD) {
450 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
454 if (s->avctx->scenechange_threshold < 1000000000 &&
455 (s->flags & CODEC_FLAG_CLOSED_GOP)) {
456 av_log(avctx, AV_LOG_ERROR,
457 "closed gop with scene change detection are not supported yet, "
458 "set threshold to 1000000000\n");
462 if (s->flags & CODEC_FLAG_LOW_DELAY) {
463 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
464 av_log(avctx, AV_LOG_ERROR,
465 "low delay forcing is only available for mpeg2\n");
468 if (s->max_b_frames != 0) {
469 av_log(avctx, AV_LOG_ERROR,
470 "b frames cannot be used with low delay\n");
475 if (s->q_scale_type == 1) {
476 if (avctx->qmax > 12) {
477 av_log(avctx, AV_LOG_ERROR,
478 "non linear quant only supports qmax <= 12 currently\n");
483 if (s->avctx->thread_count > 1 &&
484 s->codec_id != AV_CODEC_ID_MPEG4 &&
485 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
486 s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
487 (s->codec_id != AV_CODEC_ID_H263P)) {
488 av_log(avctx, AV_LOG_ERROR,
489 "multi threaded encoding not supported by codec\n");
493 if (s->avctx->thread_count < 1) {
494 av_log(avctx, AV_LOG_ERROR,
495 "automatic thread number detection not supported by codec,"
500 if (s->avctx->thread_count > 1)
503 if (!avctx->time_base.den || !avctx->time_base.num) {
504 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
508 i = (INT_MAX / 2 + 128) >> 8;
509 if (avctx->mb_threshold >= i) {
510 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
515 if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
516 av_log(avctx, AV_LOG_INFO,
517 "notice: b_frame_strategy only affects the first pass\n");
518 avctx->b_frame_strategy = 0;
521 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
523 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
524 avctx->time_base.den /= i;
525 avctx->time_base.num /= i;
529 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
530 s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG) {
531 // (a + x * 3 / 8) / x
532 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
533 s->inter_quant_bias = 0;
535 s->intra_quant_bias = 0;
537 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
540 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
541 s->intra_quant_bias = avctx->intra_quant_bias;
542 if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
543 s->inter_quant_bias = avctx->inter_quant_bias;
545 if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
546 s->avctx->time_base.den > (1 << 16) - 1) {
547 av_log(avctx, AV_LOG_ERROR,
548 "timebase %d/%d not supported by MPEG 4 standard, "
549 "the maximum admitted value for the timebase denominator "
550 "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
554 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
556 switch (avctx->codec->id) {
557 case AV_CODEC_ID_MPEG1VIDEO:
558 s->out_format = FMT_MPEG1;
559 s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
560 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
562 case AV_CODEC_ID_MPEG2VIDEO:
563 s->out_format = FMT_MPEG1;
564 s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
565 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
568 case AV_CODEC_ID_MJPEG:
569 s->out_format = FMT_MJPEG;
570 s->intra_only = 1; /* force intra only for jpeg */
571 if (!CONFIG_MJPEG_ENCODER ||
572 ff_mjpeg_encode_init(s) < 0)
577 case AV_CODEC_ID_H261:
578 if (!CONFIG_H261_ENCODER)
580 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
581 av_log(avctx, AV_LOG_ERROR,
582 "The specified picture size of %dx%d is not valid for the "
583 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
584 s->width, s->height);
587 s->out_format = FMT_H261;
591 case AV_CODEC_ID_H263:
592 if (!CONFIG_H263_ENCODER)
594 if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
595 s->width, s->height) == 8) {
596 av_log(avctx, AV_LOG_INFO,
597 "The specified picture size of %dx%d is not valid for "
598 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
599 "352x288, 704x576, and 1408x1152."
600 "Try H.263+.\n", s->width, s->height);
603 s->out_format = FMT_H263;
607 case AV_CODEC_ID_H263P:
608 s->out_format = FMT_H263;
611 s->h263_aic = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
612 s->modified_quant = s->h263_aic;
613 s->loop_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
614 s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
617 /* These are just to be sure */
621 case AV_CODEC_ID_FLV1:
622 s->out_format = FMT_H263;
623 s->h263_flv = 2; /* format = 1; 11-bit codes */
624 s->unrestricted_mv = 1;
625 s->rtp_mode = 0; /* don't allow GOB */
629 case AV_CODEC_ID_RV10:
630 s->out_format = FMT_H263;
634 case AV_CODEC_ID_RV20:
635 s->out_format = FMT_H263;
638 s->modified_quant = 1;
642 s->unrestricted_mv = 0;
644 case AV_CODEC_ID_MPEG4:
645 s->out_format = FMT_H263;
647 s->unrestricted_mv = 1;
648 s->low_delay = s->max_b_frames ? 0 : 1;
649 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
651 case AV_CODEC_ID_MSMPEG4V2:
652 s->out_format = FMT_H263;
654 s->unrestricted_mv = 1;
655 s->msmpeg4_version = 2;
659 case AV_CODEC_ID_MSMPEG4V3:
660 s->out_format = FMT_H263;
662 s->unrestricted_mv = 1;
663 s->msmpeg4_version = 3;
664 s->flipflop_rounding = 1;
668 case AV_CODEC_ID_WMV1:
669 s->out_format = FMT_H263;
671 s->unrestricted_mv = 1;
672 s->msmpeg4_version = 4;
673 s->flipflop_rounding = 1;
677 case AV_CODEC_ID_WMV2:
678 s->out_format = FMT_H263;
680 s->unrestricted_mv = 1;
681 s->msmpeg4_version = 5;
682 s->flipflop_rounding = 1;
690 avctx->has_b_frames = !s->low_delay;
694 s->progressive_frame =
695 s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
696 CODEC_FLAG_INTERLACED_ME) ||
700 if (ff_MPV_common_init(s) < 0)
704 ff_MPV_encode_init_x86(s);
706 ff_fdctdsp_init(&s->fdsp, avctx);
707 ff_me_cmp_init(&s->mecc, avctx);
708 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
709 ff_pixblockdsp_init(&s->pdsp, avctx);
710 ff_qpeldsp_init(&s->qdsp);
712 s->avctx->coded_frame = s->current_picture.f;
714 if (s->msmpeg4_version) {
715 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
716 2 * 2 * (MAX_LEVEL + 1) *
717 (MAX_RUN + 1) * 2 * sizeof(int), fail);
719 FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
721 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail);
722 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail);
723 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
724 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
725 FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
726 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
727 FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
728 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
730 if (s->avctx->noise_reduction) {
731 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
732 2 * 64 * sizeof(uint16_t), fail);
735 if (CONFIG_H263_ENCODER)
736 ff_h263dsp_init(&s->h263dsp);
737 if (!s->dct_quantize)
738 s->dct_quantize = ff_dct_quantize_c;
740 s->denoise_dct = denoise_dct_c;
741 s->fast_dct_quantize = s->dct_quantize;
743 s->dct_quantize = dct_quantize_trellis_c;
745 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
746 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
748 s->quant_precision = 5;
750 ff_set_cmp(&s->mecc, s->mecc.ildct_cmp, s->avctx->ildct_cmp);
751 ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->avctx->frame_skip_cmp);
753 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
754 ff_h261_encode_init(s);
755 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
756 ff_h263_encode_init(s);
757 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
758 ff_msmpeg4_encode_init(s);
759 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
760 && s->out_format == FMT_MPEG1)
761 ff_mpeg1_encode_init(s);
764 for (i = 0; i < 64; i++) {
765 int j = s->idsp.idct_permutation[i];
766 if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
768 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
769 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
770 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
772 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
775 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
776 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
778 if (s->avctx->intra_matrix)
779 s->intra_matrix[j] = s->avctx->intra_matrix[i];
780 if (s->avctx->inter_matrix)
781 s->inter_matrix[j] = s->avctx->inter_matrix[i];
784 /* precompute matrix */
785 /* for mjpeg, we do include qscale in the matrix */
786 if (s->out_format != FMT_MJPEG) {
787 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
788 s->intra_matrix, s->intra_quant_bias, avctx->qmin,
790 ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
791 s->inter_matrix, s->inter_quant_bias, avctx->qmin,
795 if (ff_rate_control_init(s) < 0)
798 #if FF_API_ERROR_RATE
799 FF_DISABLE_DEPRECATION_WARNINGS
800 if (avctx->error_rate)
801 s->error_rate = avctx->error_rate;
802 FF_ENABLE_DEPRECATION_WARNINGS;
805 #if FF_API_NORMALIZE_AQP
806 FF_DISABLE_DEPRECATION_WARNINGS
807 if (avctx->flags & CODEC_FLAG_NORMALIZE_AQP)
808 s->mpv_flags |= FF_MPV_FLAG_NAQ;
809 FF_ENABLE_DEPRECATION_WARNINGS;
813 FF_DISABLE_DEPRECATION_WARNINGS
814 if (avctx->flags & CODEC_FLAG_MV0)
815 s->mpv_flags |= FF_MPV_FLAG_MV0;
816 FF_ENABLE_DEPRECATION_WARNINGS
819 if (avctx->b_frame_strategy == 2) {
820 for (i = 0; i < s->max_b_frames + 2; i++) {
821 s->tmp_frames[i] = av_frame_alloc();
822 if (!s->tmp_frames[i])
823 return AVERROR(ENOMEM);
825 s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
826 s->tmp_frames[i]->width = s->width >> avctx->brd_scale;
827 s->tmp_frames[i]->height = s->height >> avctx->brd_scale;
829 ret = av_frame_get_buffer(s->tmp_frames[i], 32);
837 ff_MPV_encode_end(avctx);
838 return AVERROR_UNKNOWN;
841 av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
843 MpegEncContext *s = avctx->priv_data;
846 ff_rate_control_uninit(s);
848 ff_MPV_common_end(s);
849 if (CONFIG_MJPEG_ENCODER &&
850 s->out_format == FMT_MJPEG)
851 ff_mjpeg_encode_close(s);
853 av_freep(&avctx->extradata);
855 for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
856 av_frame_free(&s->tmp_frames[i]);
858 ff_free_picture_tables(&s->new_picture);
859 ff_mpeg_unref_picture(s, &s->new_picture);
861 av_freep(&s->avctx->stats_out);
862 av_freep(&s->ac_stats);
864 av_freep(&s->q_intra_matrix);
865 av_freep(&s->q_inter_matrix);
866 av_freep(&s->q_intra_matrix16);
867 av_freep(&s->q_inter_matrix16);
868 av_freep(&s->input_picture);
869 av_freep(&s->reordered_input_picture);
870 av_freep(&s->dct_offset);
875 static int get_sae(uint8_t *src, int ref, int stride)
880 for (y = 0; y < 16; y++) {
881 for (x = 0; x < 16; x++) {
882 acc += FFABS(src[x + y * stride] - ref);
889 static int get_intra_count(MpegEncContext *s, uint8_t *src,
890 uint8_t *ref, int stride)
898 for (y = 0; y < h; y += 16) {
899 for (x = 0; x < w; x += 16) {
900 int offset = x + y * stride;
901 int sad = s->mecc.sad[0](NULL, src + offset, ref + offset,
903 int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
904 int sae = get_sae(src + offset, mean, stride);
906 acc += sae + 500 < sad;
913 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
917 int i, display_picture_number = 0, ret;
918 const int encoding_delay = s->max_b_frames ? s->max_b_frames :
919 (s->low_delay ? 0 : 1);
924 display_picture_number = s->input_picture_number++;
926 if (pts != AV_NOPTS_VALUE) {
927 if (s->user_specified_pts != AV_NOPTS_VALUE) {
929 int64_t last = s->user_specified_pts;
932 av_log(s->avctx, AV_LOG_ERROR,
933 "Error, Invalid timestamp=%"PRId64", "
934 "last=%"PRId64"\n", pts, s->user_specified_pts);
938 if (!s->low_delay && display_picture_number == 1)
939 s->dts_delta = time - last;
941 s->user_specified_pts = pts;
943 if (s->user_specified_pts != AV_NOPTS_VALUE) {
944 s->user_specified_pts =
945 pts = s->user_specified_pts + 1;
946 av_log(s->avctx, AV_LOG_INFO,
947 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
950 pts = display_picture_number;
956 if (!pic_arg->buf[0]);
958 if (pic_arg->linesize[0] != s->linesize)
960 if (pic_arg->linesize[1] != s->uvlinesize)
962 if (pic_arg->linesize[2] != s->uvlinesize)
965 av_dlog(s->avctx, "%d %d %td %td\n", pic_arg->linesize[0],
966 pic_arg->linesize[1], s->linesize, s->uvlinesize);
969 i = ff_find_unused_picture(s, 1);
973 pic = &s->picture[i];
976 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
978 if (ff_alloc_picture(s, pic, 1) < 0) {
982 i = ff_find_unused_picture(s, 0);
986 pic = &s->picture[i];
989 if (ff_alloc_picture(s, pic, 0) < 0) {
993 if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
994 pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
995 pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
998 int h_chroma_shift, v_chroma_shift;
999 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1003 for (i = 0; i < 3; i++) {
1004 int src_stride = pic_arg->linesize[i];
1005 int dst_stride = i ? s->uvlinesize : s->linesize;
1006 int h_shift = i ? h_chroma_shift : 0;
1007 int v_shift = i ? v_chroma_shift : 0;
1008 int w = s->width >> h_shift;
1009 int h = s->height >> v_shift;
1010 uint8_t *src = pic_arg->data[i];
1011 uint8_t *dst = pic->f->data[i];
1013 if (!s->avctx->rc_buffer_size)
1014 dst += INPLACE_OFFSET;
1016 if (src_stride == dst_stride)
1017 memcpy(dst, src, src_stride * h);
1020 memcpy(dst, src, w);
1028 ret = av_frame_copy_props(pic->f, pic_arg);
1032 pic->f->display_picture_number = display_picture_number;
1033 pic->f->pts = pts; // we set this here to avoid modifiying pic_arg
1036 /* shift buffer entries */
1037 for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1038 s->input_picture[i - 1] = s->input_picture[i];
1040 s->input_picture[encoding_delay] = (Picture*) pic;
1045 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1049 int64_t score64 = 0;
1051 for (plane = 0; plane < 3; plane++) {
1052 const int stride = p->f->linesize[plane];
1053 const int bw = plane ? 1 : 2;
1054 for (y = 0; y < s->mb_height * bw; y++) {
1055 for (x = 0; x < s->mb_width * bw; x++) {
1056 int off = p->shared ? 0 : 16;
1057 uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1058 uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1059 int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1061 switch (s->avctx->frame_skip_exp) {
1062 case 0: score = FFMAX(score, v); break;
1063 case 1: score += FFABS(v); break;
1064 case 2: score += v * v; break;
1065 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1066 case 4: score64 += v * v * (int64_t)(v * v); break;
1075 if (score64 < s->avctx->frame_skip_threshold)
1077 if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
1082 static int encode_frame(AVCodecContext *c, AVFrame *frame)
1084 AVPacket pkt = { 0 };
1085 int ret, got_output;
1087 av_init_packet(&pkt);
1088 ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
1093 av_free_packet(&pkt);
1097 static int estimate_best_b_count(MpegEncContext *s)
1099 AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1100 AVCodecContext *c = avcodec_alloc_context3(NULL);
1101 const int scale = s->avctx->brd_scale;
1102 int i, j, out_size, p_lambda, b_lambda, lambda2;
1103 int64_t best_rd = INT64_MAX;
1104 int best_b_count = -1;
1106 assert(scale >= 0 && scale <= 3);
1109 //s->next_picture_ptr->quality;
1110 p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1111 //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1112 b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1113 if (!b_lambda) // FIXME we should do this somewhere else
1114 b_lambda = p_lambda;
1115 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1118 c->width = s->width >> scale;
1119 c->height = s->height >> scale;
1120 c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;
1121 c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;
1122 c->mb_decision = s->avctx->mb_decision;
1123 c->me_cmp = s->avctx->me_cmp;
1124 c->mb_cmp = s->avctx->mb_cmp;
1125 c->me_sub_cmp = s->avctx->me_sub_cmp;
1126 c->pix_fmt = AV_PIX_FMT_YUV420P;
1127 c->time_base = s->avctx->time_base;
1128 c->max_b_frames = s->max_b_frames;
1130 if (avcodec_open2(c, codec, NULL) < 0)
1133 for (i = 0; i < s->max_b_frames + 2; i++) {
1134 Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1135 s->next_picture_ptr;
1137 if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1138 pre_input = *pre_input_ptr;
1140 if (!pre_input.shared && i) {
1141 pre_input.f->data[0] += INPLACE_OFFSET;
1142 pre_input.f->data[1] += INPLACE_OFFSET;
1143 pre_input.f->data[2] += INPLACE_OFFSET;
1146 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1147 s->tmp_frames[i]->linesize[0],
1148 pre_input.f->data[0],
1149 pre_input.f->linesize[0],
1150 c->width, c->height);
1151 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1152 s->tmp_frames[i]->linesize[1],
1153 pre_input.f->data[1],
1154 pre_input.f->linesize[1],
1155 c->width >> 1, c->height >> 1);
1156 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1157 s->tmp_frames[i]->linesize[2],
1158 pre_input.f->data[2],
1159 pre_input.f->linesize[2],
1160 c->width >> 1, c->height >> 1);
1164 for (j = 0; j < s->max_b_frames + 1; j++) {
1167 if (!s->input_picture[j])
1170 c->error[0] = c->error[1] = c->error[2] = 0;
1172 s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1173 s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1175 out_size = encode_frame(c, s->tmp_frames[0]);
1177 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1179 for (i = 0; i < s->max_b_frames + 1; i++) {
1180 int is_p = i % (j + 1) == j || i == s->max_b_frames;
1182 s->tmp_frames[i + 1]->pict_type = is_p ?
1183 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1184 s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1186 out_size = encode_frame(c, s->tmp_frames[i + 1]);
1188 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1191 /* get the delayed frames */
1193 out_size = encode_frame(c, NULL);
1194 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1197 rd += c->error[0] + c->error[1] + c->error[2];
1208 return best_b_count;
1211 static int select_input_picture(MpegEncContext *s)
1215 for (i = 1; i < MAX_PICTURE_COUNT; i++)
1216 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1217 s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1219 /* set next picture type & ordering */
1220 if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
1221 if (/*s->picture_in_gop_number >= s->gop_size ||*/
1222 s->next_picture_ptr == NULL || s->intra_only) {
1223 s->reordered_input_picture[0] = s->input_picture[0];
1224 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1225 s->reordered_input_picture[0]->f->coded_picture_number =
1226 s->coded_picture_number++;
1230 if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1231 if (s->picture_in_gop_number < s->gop_size &&
1232 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1233 // FIXME check that te gop check above is +-1 correct
1234 av_frame_unref(s->input_picture[0]->f);
1237 ff_vbv_update(s, 0);
1243 if (s->flags & CODEC_FLAG_PASS2) {
1244 for (i = 0; i < s->max_b_frames + 1; i++) {
1245 int pict_num = s->input_picture[0]->f->display_picture_number + i;
1247 if (pict_num >= s->rc_context.num_entries)
1249 if (!s->input_picture[i]) {
1250 s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1254 s->input_picture[i]->f->pict_type =
1255 s->rc_context.entry[pict_num].new_pict_type;
1259 if (s->avctx->b_frame_strategy == 0) {
1260 b_frames = s->max_b_frames;
1261 while (b_frames && !s->input_picture[b_frames])
1263 } else if (s->avctx->b_frame_strategy == 1) {
1264 for (i = 1; i < s->max_b_frames + 1; i++) {
1265 if (s->input_picture[i] &&
1266 s->input_picture[i]->b_frame_score == 0) {
1267 s->input_picture[i]->b_frame_score =
1269 s->input_picture[i ]->f->data[0],
1270 s->input_picture[i - 1]->f->data[0],
1274 for (i = 0; i < s->max_b_frames + 1; i++) {
1275 if (s->input_picture[i] == NULL ||
1276 s->input_picture[i]->b_frame_score - 1 >
1277 s->mb_num / s->avctx->b_sensitivity)
1281 b_frames = FFMAX(0, i - 1);
1284 for (i = 0; i < b_frames + 1; i++) {
1285 s->input_picture[i]->b_frame_score = 0;
1287 } else if (s->avctx->b_frame_strategy == 2) {
1288 b_frames = estimate_best_b_count(s);
1290 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1296 for (i = b_frames - 1; i >= 0; i--) {
1297 int type = s->input_picture[i]->f->pict_type;
1298 if (type && type != AV_PICTURE_TYPE_B)
1301 if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1302 b_frames == s->max_b_frames) {
1303 av_log(s->avctx, AV_LOG_ERROR,
1304 "warning, too many b frames in a row\n");
1307 if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1308 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1309 s->gop_size > s->picture_in_gop_number) {
1310 b_frames = s->gop_size - s->picture_in_gop_number - 1;
1312 if (s->flags & CODEC_FLAG_CLOSED_GOP)
1314 s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1318 if ((s->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1319 s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1322 s->reordered_input_picture[0] = s->input_picture[b_frames];
1323 if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1324 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1325 s->reordered_input_picture[0]->f->coded_picture_number =
1326 s->coded_picture_number++;
1327 for (i = 0; i < b_frames; i++) {
1328 s->reordered_input_picture[i + 1] = s->input_picture[i];
1329 s->reordered_input_picture[i + 1]->f->pict_type =
1331 s->reordered_input_picture[i + 1]->f->coded_picture_number =
1332 s->coded_picture_number++;
1337 if (s->reordered_input_picture[0]) {
1338 s->reordered_input_picture[0]->reference =
1339 s->reordered_input_picture[0]->f->pict_type !=
1340 AV_PICTURE_TYPE_B ? 3 : 0;
1342 ff_mpeg_unref_picture(s, &s->new_picture);
1343 if ((ret = ff_mpeg_ref_picture(s, &s->new_picture, s->reordered_input_picture[0])))
1346 if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1347 // input is a shared pix, so we can't modifiy it -> alloc a new
1348 // one & ensure that the shared one is reuseable
1351 int i = ff_find_unused_picture(s, 0);
1354 pic = &s->picture[i];
1356 pic->reference = s->reordered_input_picture[0]->reference;
1357 if (ff_alloc_picture(s, pic, 0) < 0) {
1361 ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1365 /* mark us unused / free shared pic */
1366 av_frame_unref(s->reordered_input_picture[0]->f);
1367 s->reordered_input_picture[0]->shared = 0;
1369 s->current_picture_ptr = pic;
1371 // input is not a shared pix -> reuse buffer for current_pix
1372 s->current_picture_ptr = s->reordered_input_picture[0];
1373 for (i = 0; i < 4; i++) {
1374 s->new_picture.f->data[i] += INPLACE_OFFSET;
1377 ff_mpeg_unref_picture(s, &s->current_picture);
1378 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1379 s->current_picture_ptr)) < 0)
1382 s->picture_number = s->new_picture.f->display_picture_number;
1384 ff_mpeg_unref_picture(s, &s->new_picture);
1389 static void frame_end(MpegEncContext *s)
1393 if (s->unrestricted_mv &&
1394 s->current_picture.reference &&
1396 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1397 int hshift = desc->log2_chroma_w;
1398 int vshift = desc->log2_chroma_h;
1399 s->mpvencdsp.draw_edges(s->current_picture.f->data[0], s->linesize,
1400 s->h_edge_pos, s->v_edge_pos,
1401 EDGE_WIDTH, EDGE_WIDTH,
1402 EDGE_TOP | EDGE_BOTTOM);
1403 s->mpvencdsp.draw_edges(s->current_picture.f->data[1], s->uvlinesize,
1404 s->h_edge_pos >> hshift,
1405 s->v_edge_pos >> vshift,
1406 EDGE_WIDTH >> hshift,
1407 EDGE_WIDTH >> vshift,
1408 EDGE_TOP | EDGE_BOTTOM);
1409 s->mpvencdsp.draw_edges(s->current_picture.f->data[2], s->uvlinesize,
1410 s->h_edge_pos >> hshift,
1411 s->v_edge_pos >> vshift,
1412 EDGE_WIDTH >> hshift,
1413 EDGE_WIDTH >> vshift,
1414 EDGE_TOP | EDGE_BOTTOM);
1419 s->last_pict_type = s->pict_type;
1420 s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1421 if (s->pict_type!= AV_PICTURE_TYPE_B)
1422 s->last_non_b_pict_type = s->pict_type;
1425 /* release non-reference frames */
1426 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1427 if (!s->picture[i].reference)
1428 ff_mpeg_unref_picture(s, &s->picture[i]);
1432 s->avctx->coded_frame = s->current_picture_ptr->f;
1436 static void update_noise_reduction(MpegEncContext *s)
1440 for (intra = 0; intra < 2; intra++) {
1441 if (s->dct_count[intra] > (1 << 16)) {
1442 for (i = 0; i < 64; i++) {
1443 s->dct_error_sum[intra][i] >>= 1;
1445 s->dct_count[intra] >>= 1;
1448 for (i = 0; i < 64; i++) {
1449 s->dct_offset[intra][i] = (s->avctx->noise_reduction *
1450 s->dct_count[intra] +
1451 s->dct_error_sum[intra][i] / 2) /
1452 (s->dct_error_sum[intra][i] + 1);
1457 static int frame_start(MpegEncContext *s)
1461 /* mark & release old frames */
1462 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1463 s->last_picture_ptr != s->next_picture_ptr &&
1464 s->last_picture_ptr->f->buf[0]) {
1465 ff_mpeg_unref_picture(s, s->last_picture_ptr);
1468 s->current_picture_ptr->f->pict_type = s->pict_type;
1469 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1471 ff_mpeg_unref_picture(s, &s->current_picture);
1472 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1473 s->current_picture_ptr)) < 0)
1476 if (s->pict_type != AV_PICTURE_TYPE_B) {
1477 s->last_picture_ptr = s->next_picture_ptr;
1479 s->next_picture_ptr = s->current_picture_ptr;
1482 if (s->last_picture_ptr) {
1483 ff_mpeg_unref_picture(s, &s->last_picture);
1484 if (s->last_picture_ptr->f->buf[0] &&
1485 (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1486 s->last_picture_ptr)) < 0)
1489 if (s->next_picture_ptr) {
1490 ff_mpeg_unref_picture(s, &s->next_picture);
1491 if (s->next_picture_ptr->f->buf[0] &&
1492 (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1493 s->next_picture_ptr)) < 0)
1497 if (s->picture_structure!= PICT_FRAME) {
1499 for (i = 0; i < 4; i++) {
1500 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1501 s->current_picture.f->data[i] +=
1502 s->current_picture.f->linesize[i];
1504 s->current_picture.f->linesize[i] *= 2;
1505 s->last_picture.f->linesize[i] *= 2;
1506 s->next_picture.f->linesize[i] *= 2;
1510 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1511 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1512 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1513 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1514 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1515 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1517 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1518 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1521 if (s->dct_error_sum) {
1522 assert(s->avctx->noise_reduction && s->encoding);
1523 update_noise_reduction(s);
1529 int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1530 const AVFrame *pic_arg, int *got_packet)
1532 MpegEncContext *s = avctx->priv_data;
1533 int i, stuffing_count, ret;
1534 int context_count = s->slice_context_count;
1536 s->picture_in_gop_number++;
1538 if (load_input_picture(s, pic_arg) < 0)
1541 if (select_input_picture(s) < 0) {
1546 if (s->new_picture.f->data[0]) {
1548 (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
1551 s->mb_info_ptr = av_packet_new_side_data(pkt,
1552 AV_PKT_DATA_H263_MB_INFO,
1553 s->mb_width*s->mb_height*12);
1554 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1557 for (i = 0; i < context_count; i++) {
1558 int start_y = s->thread_context[i]->start_mb_y;
1559 int end_y = s->thread_context[i]-> end_mb_y;
1560 int h = s->mb_height;
1561 uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1562 uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1564 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1567 s->pict_type = s->new_picture.f->pict_type;
1569 ret = frame_start(s);
1573 if (encode_picture(s, s->picture_number) < 0)
1576 avctx->header_bits = s->header_bits;
1577 avctx->mv_bits = s->mv_bits;
1578 avctx->misc_bits = s->misc_bits;
1579 avctx->i_tex_bits = s->i_tex_bits;
1580 avctx->p_tex_bits = s->p_tex_bits;
1581 avctx->i_count = s->i_count;
1582 // FIXME f/b_count in avctx
1583 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
1584 avctx->skip_count = s->skip_count;
1588 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1589 ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1591 if (avctx->rc_buffer_size) {
1592 RateControlContext *rcc = &s->rc_context;
1593 int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
1595 if (put_bits_count(&s->pb) > max_size &&
1596 s->lambda < s->avctx->lmax) {
1597 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1598 (s->qscale + 1) / s->qscale);
1599 if (s->adaptive_quant) {
1601 for (i = 0; i < s->mb_height * s->mb_stride; i++)
1602 s->lambda_table[i] =
1603 FFMAX(s->lambda_table[i] + 1,
1604 s->lambda_table[i] * (s->qscale + 1) /
1607 s->mb_skipped = 0; // done in frame_start()
1608 // done in encode_picture() so we must undo it
1609 if (s->pict_type == AV_PICTURE_TYPE_P) {
1610 if (s->flipflop_rounding ||
1611 s->codec_id == AV_CODEC_ID_H263P ||
1612 s->codec_id == AV_CODEC_ID_MPEG4)
1613 s->no_rounding ^= 1;
1615 if (s->pict_type != AV_PICTURE_TYPE_B) {
1616 s->time_base = s->last_time_base;
1617 s->last_non_b_time = s->time - s->pp_time;
1619 for (i = 0; i < context_count; i++) {
1620 PutBitContext *pb = &s->thread_context[i]->pb;
1621 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1626 assert(s->avctx->rc_max_rate);
1629 if (s->flags & CODEC_FLAG_PASS1)
1630 ff_write_pass1_stats(s);
1632 for (i = 0; i < 4; i++) {
1633 s->current_picture_ptr->f->error[i] = s->current_picture.f->error[i];
1634 avctx->error[i] += s->current_picture_ptr->f->error[i];
1637 if (s->flags & CODEC_FLAG_PASS1)
1638 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1639 avctx->i_tex_bits + avctx->p_tex_bits ==
1640 put_bits_count(&s->pb));
1641 flush_put_bits(&s->pb);
1642 s->frame_bits = put_bits_count(&s->pb);
1644 stuffing_count = ff_vbv_update(s, s->frame_bits);
1645 if (stuffing_count) {
1646 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1647 stuffing_count + 50) {
1648 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1652 switch (s->codec_id) {
1653 case AV_CODEC_ID_MPEG1VIDEO:
1654 case AV_CODEC_ID_MPEG2VIDEO:
1655 while (stuffing_count--) {
1656 put_bits(&s->pb, 8, 0);
1659 case AV_CODEC_ID_MPEG4:
1660 put_bits(&s->pb, 16, 0);
1661 put_bits(&s->pb, 16, 0x1C3);
1662 stuffing_count -= 4;
1663 while (stuffing_count--) {
1664 put_bits(&s->pb, 8, 0xFF);
1668 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1670 flush_put_bits(&s->pb);
1671 s->frame_bits = put_bits_count(&s->pb);
1674 /* update mpeg1/2 vbv_delay for CBR */
1675 if (s->avctx->rc_max_rate &&
1676 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1677 s->out_format == FMT_MPEG1 &&
1678 90000LL * (avctx->rc_buffer_size - 1) <=
1679 s->avctx->rc_max_rate * 0xFFFFLL) {
1680 int vbv_delay, min_delay;
1681 double inbits = s->avctx->rc_max_rate *
1682 av_q2d(s->avctx->time_base);
1683 int minbits = s->frame_bits - 8 *
1684 (s->vbv_delay_ptr - s->pb.buf - 1);
1685 double bits = s->rc_context.buffer_index + minbits - inbits;
1688 av_log(s->avctx, AV_LOG_ERROR,
1689 "Internal error, negative bits\n");
1691 assert(s->repeat_first_field == 0);
1693 vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1694 min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1695 s->avctx->rc_max_rate;
1697 vbv_delay = FFMAX(vbv_delay, min_delay);
1699 assert(vbv_delay < 0xFFFF);
1701 s->vbv_delay_ptr[0] &= 0xF8;
1702 s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1703 s->vbv_delay_ptr[1] = vbv_delay >> 5;
1704 s->vbv_delay_ptr[2] &= 0x07;
1705 s->vbv_delay_ptr[2] |= vbv_delay << 3;
1706 avctx->vbv_delay = vbv_delay * 300;
1708 s->total_bits += s->frame_bits;
1709 avctx->frame_bits = s->frame_bits;
1711 pkt->pts = s->current_picture.f->pts;
1712 if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1713 if (!s->current_picture.f->coded_picture_number)
1714 pkt->dts = pkt->pts - s->dts_delta;
1716 pkt->dts = s->reordered_pts;
1717 s->reordered_pts = pkt->pts;
1719 pkt->dts = pkt->pts;
1720 if (s->current_picture.f->key_frame)
1721 pkt->flags |= AV_PKT_FLAG_KEY;
1723 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1727 assert((s->frame_bits & 7) == 0);
1729 pkt->size = s->frame_bits / 8;
1730 *got_packet = !!pkt->size;
1734 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1735 int n, int threshold)
1737 static const char tab[64] = {
1738 3, 2, 2, 1, 1, 1, 1, 1,
1739 1, 1, 1, 1, 1, 1, 1, 1,
1740 1, 1, 1, 1, 1, 1, 1, 1,
1741 0, 0, 0, 0, 0, 0, 0, 0,
1742 0, 0, 0, 0, 0, 0, 0, 0,
1743 0, 0, 0, 0, 0, 0, 0, 0,
1744 0, 0, 0, 0, 0, 0, 0, 0,
1745 0, 0, 0, 0, 0, 0, 0, 0
1750 int16_t *block = s->block[n];
1751 const int last_index = s->block_last_index[n];
1754 if (threshold < 0) {
1756 threshold = -threshold;
1760 /* Are all we could set to zero already zero? */
1761 if (last_index <= skip_dc - 1)
1764 for (i = 0; i <= last_index; i++) {
1765 const int j = s->intra_scantable.permutated[i];
1766 const int level = FFABS(block[j]);
1768 if (skip_dc && i == 0)
1772 } else if (level > 1) {
1778 if (score >= threshold)
1780 for (i = skip_dc; i <= last_index; i++) {
1781 const int j = s->intra_scantable.permutated[i];
1785 s->block_last_index[n] = 0;
1787 s->block_last_index[n] = -1;
1790 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1794 const int maxlevel = s->max_qcoeff;
1795 const int minlevel = s->min_qcoeff;
1799 i = 1; // skip clipping of intra dc
1803 for (; i <= last_index; i++) {
1804 const int j = s->intra_scantable.permutated[i];
1805 int level = block[j];
1807 if (level > maxlevel) {
1810 } else if (level < minlevel) {
1818 if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1819 av_log(s->avctx, AV_LOG_INFO,
1820 "warning, clipping %d dct coefficients to %d..%d\n",
1821 overflow, minlevel, maxlevel);
1824 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1828 for (y = 0; y < 8; y++) {
1829 for (x = 0; x < 8; x++) {
1835 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1836 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1837 int v = ptr[x2 + y2 * stride];
1843 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
1848 static av_always_inline void encode_mb_internal(MpegEncContext *s,
1849 int motion_x, int motion_y,
1850 int mb_block_height,
1853 int16_t weight[8][64];
1854 int16_t orig[8][64];
1855 const int mb_x = s->mb_x;
1856 const int mb_y = s->mb_y;
1859 int dct_offset = s->linesize * 8; // default for progressive frames
1860 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1861 ptrdiff_t wrap_y, wrap_c;
1863 for (i = 0; i < mb_block_count; i++)
1864 skip_dct[i] = s->skipdct;
1866 if (s->adaptive_quant) {
1867 const int last_qp = s->qscale;
1868 const int mb_xy = mb_x + mb_y * s->mb_stride;
1870 s->lambda = s->lambda_table[mb_xy];
1873 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
1874 s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
1875 s->dquant = s->qscale - last_qp;
1877 if (s->out_format == FMT_H263) {
1878 s->dquant = av_clip(s->dquant, -2, 2);
1880 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1882 if (s->pict_type == AV_PICTURE_TYPE_B) {
1883 if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1886 if (s->mv_type == MV_TYPE_8X8)
1892 ff_set_qscale(s, last_qp + s->dquant);
1893 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
1894 ff_set_qscale(s, s->qscale + s->dquant);
1896 wrap_y = s->linesize;
1897 wrap_c = s->uvlinesize;
1898 ptr_y = s->new_picture.f->data[0] +
1899 (mb_y * 16 * wrap_y) + mb_x * 16;
1900 ptr_cb = s->new_picture.f->data[1] +
1901 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1902 ptr_cr = s->new_picture.f->data[2] +
1903 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1905 if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
1906 uint8_t *ebuf = s->edge_emu_buffer + 32;
1907 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
1909 16, 16, mb_x * 16, mb_y * 16,
1910 s->width, s->height);
1912 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
1914 8, mb_block_height, mb_x * 8, mb_y * 8,
1915 s->width >> 1, s->height >> 1);
1916 ptr_cb = ebuf + 18 * wrap_y;
1917 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr,
1919 8, mb_block_height, mb_x * 8, mb_y * 8,
1920 s->width >> 1, s->height >> 1);
1921 ptr_cr = ebuf + 18 * wrap_y + 8;
1925 if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1926 int progressive_score, interlaced_score;
1928 s->interlaced_dct = 0;
1929 progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
1930 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1931 NULL, wrap_y, 8) - 400;
1933 if (progressive_score > 0) {
1934 interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
1935 NULL, wrap_y * 2, 8) +
1936 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
1937 NULL, wrap_y * 2, 8);
1938 if (progressive_score > interlaced_score) {
1939 s->interlaced_dct = 1;
1941 dct_offset = wrap_y;
1943 if (s->chroma_format == CHROMA_422)
1949 s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
1950 s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
1951 s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
1952 s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
1954 if (s->flags & CODEC_FLAG_GRAY) {
1958 s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1959 s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1960 if (!s->chroma_y_shift) { /* 422 */
1961 s->pdsp.get_pixels(s->block[6],
1962 ptr_cb + (dct_offset >> 1), wrap_c);
1963 s->pdsp.get_pixels(s->block[7],
1964 ptr_cr + (dct_offset >> 1), wrap_c);
1968 op_pixels_func (*op_pix)[4];
1969 qpel_mc_func (*op_qpix)[16];
1970 uint8_t *dest_y, *dest_cb, *dest_cr;
1972 dest_y = s->dest[0];
1973 dest_cb = s->dest[1];
1974 dest_cr = s->dest[2];
1976 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1977 op_pix = s->hdsp.put_pixels_tab;
1978 op_qpix = s->qdsp.put_qpel_pixels_tab;
1980 op_pix = s->hdsp.put_no_rnd_pixels_tab;
1981 op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
1984 if (s->mv_dir & MV_DIR_FORWARD) {
1985 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0,
1986 s->last_picture.f->data,
1988 op_pix = s->hdsp.avg_pixels_tab;
1989 op_qpix = s->qdsp.avg_qpel_pixels_tab;
1991 if (s->mv_dir & MV_DIR_BACKWARD) {
1992 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1,
1993 s->next_picture.f->data,
1997 if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1998 int progressive_score, interlaced_score;
2000 s->interlaced_dct = 0;
2001 progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2002 s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
2006 if (s->avctx->ildct_cmp == FF_CMP_VSSE)
2007 progressive_score -= 400;
2009 if (progressive_score > 0) {
2010 interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
2012 s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
2016 if (progressive_score > interlaced_score) {
2017 s->interlaced_dct = 1;
2019 dct_offset = wrap_y;
2021 if (s->chroma_format == CHROMA_422)
2027 s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2028 s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2029 s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2030 dest_y + dct_offset, wrap_y);
2031 s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2032 dest_y + dct_offset + 8, wrap_y);
2034 if (s->flags & CODEC_FLAG_GRAY) {
2038 s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2039 s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2040 if (!s->chroma_y_shift) { /* 422 */
2041 s->pdsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
2042 dest_cb + (dct_offset >> 1), wrap_c);
2043 s->pdsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
2044 dest_cr + (dct_offset >> 1), wrap_c);
2047 /* pre quantization */
2048 if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2049 2 * s->qscale * s->qscale) {
2051 if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2053 if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2055 if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2056 wrap_y, 8) < 20 * s->qscale)
2058 if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2059 wrap_y, 8) < 20 * s->qscale)
2061 if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2063 if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2065 if (!s->chroma_y_shift) { /* 422 */
2066 if (s->mecc.sad[1](NULL, ptr_cb + (dct_offset >> 1),
2067 dest_cb + (dct_offset >> 1),
2068 wrap_c, 8) < 20 * s->qscale)
2070 if (s->mecc.sad[1](NULL, ptr_cr + (dct_offset >> 1),
2071 dest_cr + (dct_offset >> 1),
2072 wrap_c, 8) < 20 * s->qscale)
2078 if (s->quantizer_noise_shaping) {
2080 get_visual_weight(weight[0], ptr_y , wrap_y);
2082 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2084 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2086 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2088 get_visual_weight(weight[4], ptr_cb , wrap_c);
2090 get_visual_weight(weight[5], ptr_cr , wrap_c);
2091 if (!s->chroma_y_shift) { /* 422 */
2093 get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
2096 get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
2099 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2102 /* DCT & quantize */
2103 assert(s->out_format != FMT_MJPEG || s->qscale == 8);
2105 for (i = 0; i < mb_block_count; i++) {
2108 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2109 // FIXME we could decide to change to quantizer instead of
2111 // JS: I don't think that would be a good idea it could lower
2112 // quality instead of improve it. Just INTRADC clipping
2113 // deserves changes in quantizer
2115 clip_coeffs(s, s->block[i], s->block_last_index[i]);
2117 s->block_last_index[i] = -1;
2119 if (s->quantizer_noise_shaping) {
2120 for (i = 0; i < mb_block_count; i++) {
2122 s->block_last_index[i] =
2123 dct_quantize_refine(s, s->block[i], weight[i],
2124 orig[i], i, s->qscale);
2129 if (s->luma_elim_threshold && !s->mb_intra)
2130 for (i = 0; i < 4; i++)
2131 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2132 if (s->chroma_elim_threshold && !s->mb_intra)
2133 for (i = 4; i < mb_block_count; i++)
2134 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2136 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2137 for (i = 0; i < mb_block_count; i++) {
2138 if (s->block_last_index[i] == -1)
2139 s->coded_score[i] = INT_MAX / 256;
2144 if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2145 s->block_last_index[4] =
2146 s->block_last_index[5] = 0;
2148 s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2151 // non c quantize code returns incorrect block_last_index FIXME
2152 if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2153 for (i = 0; i < mb_block_count; i++) {
2155 if (s->block_last_index[i] > 0) {
2156 for (j = 63; j > 0; j--) {
2157 if (s->block[i][s->intra_scantable.permutated[j]])
2160 s->block_last_index[i] = j;
2165 /* huffman encode */
2166 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2167 case AV_CODEC_ID_MPEG1VIDEO:
2168 case AV_CODEC_ID_MPEG2VIDEO:
2169 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2170 ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2172 case AV_CODEC_ID_MPEG4:
2173 if (CONFIG_MPEG4_ENCODER)
2174 ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2176 case AV_CODEC_ID_MSMPEG4V2:
2177 case AV_CODEC_ID_MSMPEG4V3:
2178 case AV_CODEC_ID_WMV1:
2179 if (CONFIG_MSMPEG4_ENCODER)
2180 ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2182 case AV_CODEC_ID_WMV2:
2183 if (CONFIG_WMV2_ENCODER)
2184 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2186 case AV_CODEC_ID_H261:
2187 if (CONFIG_H261_ENCODER)
2188 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2190 case AV_CODEC_ID_H263:
2191 case AV_CODEC_ID_H263P:
2192 case AV_CODEC_ID_FLV1:
2193 case AV_CODEC_ID_RV10:
2194 case AV_CODEC_ID_RV20:
2195 if (CONFIG_H263_ENCODER)
2196 ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2198 case AV_CODEC_ID_MJPEG:
2199 if (CONFIG_MJPEG_ENCODER)
2200 ff_mjpeg_encode_mb(s, s->block);
2207 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2209 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
2210 else encode_mb_internal(s, motion_x, motion_y, 16, 8);
2213 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2216 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2219 d->mb_skip_run= s->mb_skip_run;
2221 d->last_dc[i] = s->last_dc[i];
2224 d->mv_bits= s->mv_bits;
2225 d->i_tex_bits= s->i_tex_bits;
2226 d->p_tex_bits= s->p_tex_bits;
2227 d->i_count= s->i_count;
2228 d->f_count= s->f_count;
2229 d->b_count= s->b_count;
2230 d->skip_count= s->skip_count;
2231 d->misc_bits= s->misc_bits;
2235 d->qscale= s->qscale;
2236 d->dquant= s->dquant;
2238 d->esc3_level_length= s->esc3_level_length;
2241 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2244 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2245 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2248 d->mb_skip_run= s->mb_skip_run;
2250 d->last_dc[i] = s->last_dc[i];
2253 d->mv_bits= s->mv_bits;
2254 d->i_tex_bits= s->i_tex_bits;
2255 d->p_tex_bits= s->p_tex_bits;
2256 d->i_count= s->i_count;
2257 d->f_count= s->f_count;
2258 d->b_count= s->b_count;
2259 d->skip_count= s->skip_count;
2260 d->misc_bits= s->misc_bits;
2262 d->mb_intra= s->mb_intra;
2263 d->mb_skipped= s->mb_skipped;
2264 d->mv_type= s->mv_type;
2265 d->mv_dir= s->mv_dir;
2267 if(s->data_partitioning){
2269 d->tex_pb= s->tex_pb;
2273 d->block_last_index[i]= s->block_last_index[i];
2274 d->interlaced_dct= s->interlaced_dct;
2275 d->qscale= s->qscale;
2277 d->esc3_level_length= s->esc3_level_length;
2280 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2281 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2282 int *dmin, int *next_block, int motion_x, int motion_y)
2285 uint8_t *dest_backup[3];
2287 copy_context_before_encode(s, backup, type);
2289 s->block= s->blocks[*next_block];
2290 s->pb= pb[*next_block];
2291 if(s->data_partitioning){
2292 s->pb2 = pb2 [*next_block];
2293 s->tex_pb= tex_pb[*next_block];
2297 memcpy(dest_backup, s->dest, sizeof(s->dest));
2298 s->dest[0] = s->rd_scratchpad;
2299 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
2300 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
2301 assert(s->linesize >= 32); //FIXME
2304 encode_mb(s, motion_x, motion_y);
2306 score= put_bits_count(&s->pb);
2307 if(s->data_partitioning){
2308 score+= put_bits_count(&s->pb2);
2309 score+= put_bits_count(&s->tex_pb);
2312 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2313 ff_MPV_decode_mb(s, s->block);
2315 score *= s->lambda2;
2316 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2320 memcpy(s->dest, dest_backup, sizeof(s->dest));
2327 copy_context_after_encode(best, s, type);
2331 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2332 uint32_t *sq = ff_square_tab + 256;
2337 return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2338 else if(w==8 && h==8)
2339 return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2343 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2352 static int sse_mb(MpegEncContext *s){
2356 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2357 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2360 if(s->avctx->mb_cmp == FF_CMP_NSSE){
2361 return s->mecc.nsse[0](s, s->new_picture.f->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16, s->dest[0], s->linesize, 16) +
2362 s->mecc.nsse[1](s, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[1], s->uvlinesize, 8) +
2363 s->mecc.nsse[1](s, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[2], s->uvlinesize, 8);
2365 return s->mecc.sse[0](NULL, s->new_picture.f->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16, s->dest[0], s->linesize, 16) +
2366 s->mecc.sse[1](NULL, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[1], s->uvlinesize, 8) +
2367 s->mecc.sse[1](NULL, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[2], s->uvlinesize, 8);
2370 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)
2371 +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)
2372 +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);
2375 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2376 MpegEncContext *s= *(void**)arg;
2380 s->me.dia_size= s->avctx->pre_dia_size;
2381 s->first_slice_line=1;
2382 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2383 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2384 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2386 s->first_slice_line=0;
2394 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2395 MpegEncContext *s= *(void**)arg;
2397 s->me.dia_size= s->avctx->dia_size;
2398 s->first_slice_line=1;
2399 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2400 s->mb_x=0; //for block init below
2401 ff_init_block_index(s);
2402 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2403 s->block_index[0]+=2;
2404 s->block_index[1]+=2;
2405 s->block_index[2]+=2;
2406 s->block_index[3]+=2;
2408 /* compute motion vector & mb_type and store in context */
2409 if(s->pict_type==AV_PICTURE_TYPE_B)
2410 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2412 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2414 s->first_slice_line=0;
2419 static int mb_var_thread(AVCodecContext *c, void *arg){
2420 MpegEncContext *s= *(void**)arg;
2423 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2424 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2427 uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2429 int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2431 varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
2432 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2434 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2435 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2436 s->me.mb_var_sum_temp += varc;
2442 static void write_slice_end(MpegEncContext *s){
2443 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2444 if(s->partitioned_frame){
2445 ff_mpeg4_merge_partitions(s);
2448 ff_mpeg4_stuffing(&s->pb);
2449 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2450 ff_mjpeg_encode_stuffing(&s->pb);
2453 avpriv_align_put_bits(&s->pb);
2454 flush_put_bits(&s->pb);
2456 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2457 s->misc_bits+= get_bits_diff(s);
2460 static void write_mb_info(MpegEncContext *s)
2462 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2463 int offset = put_bits_count(&s->pb);
2464 int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2465 int gobn = s->mb_y / s->gob_index;
2467 if (CONFIG_H263_ENCODER)
2468 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2469 bytestream_put_le32(&ptr, offset);
2470 bytestream_put_byte(&ptr, s->qscale);
2471 bytestream_put_byte(&ptr, gobn);
2472 bytestream_put_le16(&ptr, mba);
2473 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2474 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2475 /* 4MV not implemented */
2476 bytestream_put_byte(&ptr, 0); /* hmv2 */
2477 bytestream_put_byte(&ptr, 0); /* vmv2 */
2480 static void update_mb_info(MpegEncContext *s, int startcode)
2484 if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2485 s->mb_info_size += 12;
2486 s->prev_mb_info = s->last_mb_info;
2489 s->prev_mb_info = put_bits_count(&s->pb)/8;
2490 /* This might have incremented mb_info_size above, and we return without
2491 * actually writing any info into that slot yet. But in that case,
2492 * this will be called again at the start of the after writing the
2493 * start code, actually writing the mb info. */
2497 s->last_mb_info = put_bits_count(&s->pb)/8;
2498 if (!s->mb_info_size)
2499 s->mb_info_size += 12;
2503 static int encode_thread(AVCodecContext *c, void *arg){
2504 MpegEncContext *s= *(void**)arg;
2505 int mb_x, mb_y, pdif = 0;
2506 int chr_h= 16>>s->chroma_y_shift;
2508 MpegEncContext best_s, backup_s;
2509 uint8_t bit_buf[2][MAX_MB_BYTES];
2510 uint8_t bit_buf2[2][MAX_MB_BYTES];
2511 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2512 PutBitContext pb[2], pb2[2], tex_pb[2];
2515 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2516 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2517 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2520 s->last_bits= put_bits_count(&s->pb);
2531 /* init last dc values */
2532 /* note: quant matrix value (8) is implied here */
2533 s->last_dc[i] = 128 << s->intra_dc_precision;
2535 s->current_picture.f->error[i] = 0;
2538 memset(s->last_mv, 0, sizeof(s->last_mv));
2542 switch(s->codec_id){
2543 case AV_CODEC_ID_H263:
2544 case AV_CODEC_ID_H263P:
2545 case AV_CODEC_ID_FLV1:
2546 if (CONFIG_H263_ENCODER)
2547 s->gob_index = ff_h263_get_gob_height(s);
2549 case AV_CODEC_ID_MPEG4:
2550 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2551 ff_mpeg4_init_partitions(s);
2557 s->first_slice_line = 1;
2558 s->ptr_lastgob = s->pb.buf;
2559 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2563 ff_set_qscale(s, s->qscale);
2564 ff_init_block_index(s);
2566 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2567 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2568 int mb_type= s->mb_type[xy];
2573 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2574 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2577 if(s->data_partitioning){
2578 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2579 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2580 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2586 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2587 ff_update_block_index(s);
2589 if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2590 ff_h261_reorder_mb_index(s);
2591 xy= s->mb_y*s->mb_stride + s->mb_x;
2592 mb_type= s->mb_type[xy];
2595 /* write gob / video packet header */
2597 int current_packet_size, is_gob_start;
2599 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2601 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2603 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2605 switch(s->codec_id){
2606 case AV_CODEC_ID_H263:
2607 case AV_CODEC_ID_H263P:
2608 if(!s->h263_slice_structured)
2609 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2611 case AV_CODEC_ID_MPEG2VIDEO:
2612 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2613 case AV_CODEC_ID_MPEG1VIDEO:
2614 if(s->mb_skip_run) is_gob_start=0;
2619 if(s->start_mb_y != mb_y || mb_x!=0){
2622 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2623 ff_mpeg4_init_partitions(s);
2627 assert((put_bits_count(&s->pb)&7) == 0);
2628 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2630 if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2631 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2632 int d = 100 / s->error_rate;
2634 current_packet_size=0;
2635 s->pb.buf_ptr= s->ptr_lastgob;
2636 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2640 if (s->avctx->rtp_callback){
2641 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2642 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2644 update_mb_info(s, 1);
2646 switch(s->codec_id){
2647 case AV_CODEC_ID_MPEG4:
2648 if (CONFIG_MPEG4_ENCODER) {
2649 ff_mpeg4_encode_video_packet_header(s);
2650 ff_mpeg4_clean_buffers(s);
2653 case AV_CODEC_ID_MPEG1VIDEO:
2654 case AV_CODEC_ID_MPEG2VIDEO:
2655 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2656 ff_mpeg1_encode_slice_header(s);
2657 ff_mpeg1_clean_buffers(s);
2660 case AV_CODEC_ID_H263:
2661 case AV_CODEC_ID_H263P:
2662 if (CONFIG_H263_ENCODER)
2663 ff_h263_encode_gob_header(s, mb_y);
2667 if(s->flags&CODEC_FLAG_PASS1){
2668 int bits= put_bits_count(&s->pb);
2669 s->misc_bits+= bits - s->last_bits;
2673 s->ptr_lastgob += current_packet_size;
2674 s->first_slice_line=1;
2675 s->resync_mb_x=mb_x;
2676 s->resync_mb_y=mb_y;
2680 if( (s->resync_mb_x == s->mb_x)
2681 && s->resync_mb_y+1 == s->mb_y){
2682 s->first_slice_line=0;
2686 s->dquant=0; //only for QP_RD
2688 update_mb_info(s, 0);
2690 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
2692 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2694 copy_context_before_encode(&backup_s, s, -1);
2696 best_s.data_partitioning= s->data_partitioning;
2697 best_s.partitioned_frame= s->partitioned_frame;
2698 if(s->data_partitioning){
2699 backup_s.pb2= s->pb2;
2700 backup_s.tex_pb= s->tex_pb;
2703 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2704 s->mv_dir = MV_DIR_FORWARD;
2705 s->mv_type = MV_TYPE_16X16;
2707 s->mv[0][0][0] = s->p_mv_table[xy][0];
2708 s->mv[0][0][1] = s->p_mv_table[xy][1];
2709 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2710 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2712 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2713 s->mv_dir = MV_DIR_FORWARD;
2714 s->mv_type = MV_TYPE_FIELD;
2717 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2718 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2719 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2721 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2722 &dmin, &next_block, 0, 0);
2724 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2725 s->mv_dir = MV_DIR_FORWARD;
2726 s->mv_type = MV_TYPE_16X16;
2730 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2731 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2733 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2734 s->mv_dir = MV_DIR_FORWARD;
2735 s->mv_type = MV_TYPE_8X8;
2738 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2739 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2741 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2742 &dmin, &next_block, 0, 0);
2744 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2745 s->mv_dir = MV_DIR_FORWARD;
2746 s->mv_type = MV_TYPE_16X16;
2748 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2749 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2750 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2751 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2753 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2754 s->mv_dir = MV_DIR_BACKWARD;
2755 s->mv_type = MV_TYPE_16X16;
2757 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2758 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2759 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2760 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2762 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2763 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2764 s->mv_type = MV_TYPE_16X16;
2766 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2767 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2768 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2769 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2770 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2771 &dmin, &next_block, 0, 0);
2773 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2774 s->mv_dir = MV_DIR_FORWARD;
2775 s->mv_type = MV_TYPE_FIELD;
2778 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2779 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2780 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2782 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2783 &dmin, &next_block, 0, 0);
2785 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2786 s->mv_dir = MV_DIR_BACKWARD;
2787 s->mv_type = MV_TYPE_FIELD;
2790 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2791 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2792 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2794 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2795 &dmin, &next_block, 0, 0);
2797 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2798 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2799 s->mv_type = MV_TYPE_FIELD;
2801 for(dir=0; dir<2; dir++){
2803 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2804 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2805 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2808 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2809 &dmin, &next_block, 0, 0);
2811 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2813 s->mv_type = MV_TYPE_16X16;
2817 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2818 &dmin, &next_block, 0, 0);
2819 if(s->h263_pred || s->h263_aic){
2821 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2823 ff_clean_intra_table_entries(s); //old mode?
2827 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2828 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2829 const int last_qp= backup_s.qscale;
2832 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2833 static const int dquant_tab[4]={-1,1,-2,2};
2835 assert(backup_s.dquant == 0);
2838 s->mv_dir= best_s.mv_dir;
2839 s->mv_type = MV_TYPE_16X16;
2840 s->mb_intra= best_s.mb_intra;
2841 s->mv[0][0][0] = best_s.mv[0][0][0];
2842 s->mv[0][0][1] = best_s.mv[0][0][1];
2843 s->mv[1][0][0] = best_s.mv[1][0][0];
2844 s->mv[1][0][1] = best_s.mv[1][0][1];
2846 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
2847 for(; qpi<4; qpi++){
2848 int dquant= dquant_tab[qpi];
2849 qp= last_qp + dquant;
2850 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2852 backup_s.dquant= dquant;
2853 if(s->mb_intra && s->dc_val[0]){
2855 dc[i]= s->dc_val[0][ s->block_index[i] ];
2856 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
2860 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2861 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2862 if(best_s.qscale != qp){
2863 if(s->mb_intra && s->dc_val[0]){
2865 s->dc_val[0][ s->block_index[i] ]= dc[i];
2866 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
2873 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2874 int mx= s->b_direct_mv_table[xy][0];
2875 int my= s->b_direct_mv_table[xy][1];
2877 backup_s.dquant = 0;
2878 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2880 ff_mpeg4_set_direct_mv(s, mx, my);
2881 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2882 &dmin, &next_block, mx, my);
2884 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2885 backup_s.dquant = 0;
2886 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2888 ff_mpeg4_set_direct_mv(s, 0, 0);
2889 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2890 &dmin, &next_block, 0, 0);
2892 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
2895 coded |= s->block_last_index[i];
2898 memcpy(s->mv, best_s.mv, sizeof(s->mv));
2899 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2900 mx=my=0; //FIXME find the one we actually used
2901 ff_mpeg4_set_direct_mv(s, mx, my);
2902 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2910 s->mv_dir= best_s.mv_dir;
2911 s->mv_type = best_s.mv_type;
2913 /* s->mv[0][0][0] = best_s.mv[0][0][0];
2914 s->mv[0][0][1] = best_s.mv[0][0][1];
2915 s->mv[1][0][0] = best_s.mv[1][0][0];
2916 s->mv[1][0][1] = best_s.mv[1][0][1];*/
2919 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2920 &dmin, &next_block, mx, my);
2925 s->current_picture.qscale_table[xy] = best_s.qscale;
2927 copy_context_after_encode(s, &best_s, -1);
2929 pb_bits_count= put_bits_count(&s->pb);
2930 flush_put_bits(&s->pb);
2931 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2934 if(s->data_partitioning){
2935 pb2_bits_count= put_bits_count(&s->pb2);
2936 flush_put_bits(&s->pb2);
2937 avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2938 s->pb2= backup_s.pb2;
2940 tex_pb_bits_count= put_bits_count(&s->tex_pb);
2941 flush_put_bits(&s->tex_pb);
2942 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2943 s->tex_pb= backup_s.tex_pb;
2945 s->last_bits= put_bits_count(&s->pb);
2947 if (CONFIG_H263_ENCODER &&
2948 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2949 ff_h263_update_motion_val(s);
2951 if(next_block==0){ //FIXME 16 vs linesize16
2952 s->hdsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
2953 s->hdsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
2954 s->hdsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2957 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2958 ff_MPV_decode_mb(s, s->block);
2960 int motion_x = 0, motion_y = 0;
2961 s->mv_type=MV_TYPE_16X16;
2962 // only one MB-Type possible
2965 case CANDIDATE_MB_TYPE_INTRA:
2968 motion_x= s->mv[0][0][0] = 0;
2969 motion_y= s->mv[0][0][1] = 0;
2971 case CANDIDATE_MB_TYPE_INTER:
2972 s->mv_dir = MV_DIR_FORWARD;
2974 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2975 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2977 case CANDIDATE_MB_TYPE_INTER_I:
2978 s->mv_dir = MV_DIR_FORWARD;
2979 s->mv_type = MV_TYPE_FIELD;
2982 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2983 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2984 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2987 case CANDIDATE_MB_TYPE_INTER4V:
2988 s->mv_dir = MV_DIR_FORWARD;
2989 s->mv_type = MV_TYPE_8X8;
2992 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2993 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2996 case CANDIDATE_MB_TYPE_DIRECT:
2997 if (CONFIG_MPEG4_ENCODER) {
2998 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3000 motion_x=s->b_direct_mv_table[xy][0];
3001 motion_y=s->b_direct_mv_table[xy][1];
3002 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3005 case CANDIDATE_MB_TYPE_DIRECT0:
3006 if (CONFIG_MPEG4_ENCODER) {
3007 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3009 ff_mpeg4_set_direct_mv(s, 0, 0);
3012 case CANDIDATE_MB_TYPE_BIDIR:
3013 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3015 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3016 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3017 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3018 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3020 case CANDIDATE_MB_TYPE_BACKWARD:
3021 s->mv_dir = MV_DIR_BACKWARD;
3023 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3024 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3026 case CANDIDATE_MB_TYPE_FORWARD:
3027 s->mv_dir = MV_DIR_FORWARD;
3029 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3030 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3032 case CANDIDATE_MB_TYPE_FORWARD_I:
3033 s->mv_dir = MV_DIR_FORWARD;
3034 s->mv_type = MV_TYPE_FIELD;
3037 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3038 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3039 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3042 case CANDIDATE_MB_TYPE_BACKWARD_I:
3043 s->mv_dir = MV_DIR_BACKWARD;
3044 s->mv_type = MV_TYPE_FIELD;
3047 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3048 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3049 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3052 case CANDIDATE_MB_TYPE_BIDIR_I:
3053 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3054 s->mv_type = MV_TYPE_FIELD;
3056 for(dir=0; dir<2; dir++){
3058 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3059 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3060 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3065 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3068 encode_mb(s, motion_x, motion_y);
3070 // RAL: Update last macroblock type
3071 s->last_mv_dir = s->mv_dir;
3073 if (CONFIG_H263_ENCODER &&
3074 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3075 ff_h263_update_motion_val(s);
3077 ff_MPV_decode_mb(s, s->block);
3080 /* clean the MV table in IPS frames for direct mode in B frames */
3081 if(s->mb_intra /* && I,P,S_TYPE */){
3082 s->p_mv_table[xy][0]=0;
3083 s->p_mv_table[xy][1]=0;
3086 if(s->flags&CODEC_FLAG_PSNR){
3090 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3091 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3093 s->current_picture.f->error[0] += sse(
3094 s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3095 s->dest[0], w, h, s->linesize);
3096 s->current_picture.f->error[1] += sse(
3097 s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3098 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3099 s->current_picture.f->error[2] += sse(
3100 s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3101 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3104 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3105 ff_h263_loop_filter(s);
3107 av_dlog(s->avctx, "MB %d %d bits\n",
3108 s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3112 //not beautiful here but we must write it before flushing so it has to be here
3113 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3114 ff_msmpeg4_encode_ext_header(s);
3118 /* Send the last GOB if RTP */
3119 if (s->avctx->rtp_callback) {
3120 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3121 pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3122 /* Call the RTP callback to send the last GOB */
3124 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3130 #define MERGE(field) dst->field += src->field; src->field=0
3131 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3132 MERGE(me.scene_change_score);
3133 MERGE(me.mc_mb_var_sum_temp);
3134 MERGE(me.mb_var_sum_temp);
3137 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3140 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3141 MERGE(dct_count[1]);
3150 MERGE(er.error_count);
3151 MERGE(padding_bug_score);
3152 MERGE(current_picture.f->error[0]);
3153 MERGE(current_picture.f->error[1]);
3154 MERGE(current_picture.f->error[2]);
3156 if(dst->avctx->noise_reduction){
3157 for(i=0; i<64; i++){
3158 MERGE(dct_error_sum[0][i]);
3159 MERGE(dct_error_sum[1][i]);
3163 assert(put_bits_count(&src->pb) % 8 ==0);
3164 assert(put_bits_count(&dst->pb) % 8 ==0);
3165 avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3166 flush_put_bits(&dst->pb);
3169 static int estimate_qp(MpegEncContext *s, int dry_run){
3170 if (s->next_lambda){
3171 s->current_picture_ptr->f->quality =
3172 s->current_picture.f->quality = s->next_lambda;
3173 if(!dry_run) s->next_lambda= 0;
3174 } else if (!s->fixed_qscale) {
3175 s->current_picture_ptr->f->quality =
3176 s->current_picture.f->quality = ff_rate_estimate_qscale(s, dry_run);
3177 if (s->current_picture.f->quality < 0)
3181 if(s->adaptive_quant){
3182 switch(s->codec_id){
3183 case AV_CODEC_ID_MPEG4:
3184 if (CONFIG_MPEG4_ENCODER)
3185 ff_clean_mpeg4_qscales(s);
3187 case AV_CODEC_ID_H263:
3188 case AV_CODEC_ID_H263P:
3189 case AV_CODEC_ID_FLV1:
3190 if (CONFIG_H263_ENCODER)
3191 ff_clean_h263_qscales(s);
3194 ff_init_qscale_tab(s);
3197 s->lambda= s->lambda_table[0];
3200 s->lambda = s->current_picture.f->quality;
3205 /* must be called before writing the header */
3206 static void set_frame_distances(MpegEncContext * s){
3207 assert(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
3208 s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3210 if(s->pict_type==AV_PICTURE_TYPE_B){
3211 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3212 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3214 s->pp_time= s->time - s->last_non_b_time;
3215 s->last_non_b_time= s->time;
3216 assert(s->picture_number==0 || s->pp_time > 0);
3220 static int encode_picture(MpegEncContext *s, int picture_number)
3224 int context_count = s->slice_context_count;
3226 s->picture_number = picture_number;
3228 /* Reset the average MB variance */
3229 s->me.mb_var_sum_temp =
3230 s->me.mc_mb_var_sum_temp = 0;
3232 /* we need to initialize some time vars before we can encode b-frames */
3233 // RAL: Condition added for MPEG1VIDEO
3234 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3235 set_frame_distances(s);
3236 if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3237 ff_set_mpeg4_time(s);
3239 s->me.scene_change_score=0;
3241 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3243 if(s->pict_type==AV_PICTURE_TYPE_I){
3244 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3245 else s->no_rounding=0;
3246 }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3247 if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3248 s->no_rounding ^= 1;
3251 if(s->flags & CODEC_FLAG_PASS2){
3252 if (estimate_qp(s,1) < 0)
3254 ff_get_2pass_fcode(s);
3255 }else if(!(s->flags & CODEC_FLAG_QSCALE)){
3256 if(s->pict_type==AV_PICTURE_TYPE_B)
3257 s->lambda= s->last_lambda_for[s->pict_type];
3259 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3263 s->mb_intra=0; //for the rate distortion & bit compare functions
3264 for(i=1; i<context_count; i++){
3265 ret = ff_update_duplicate_context(s->thread_context[i], s);
3273 /* Estimate motion for every MB */
3274 if(s->pict_type != AV_PICTURE_TYPE_I){
3275 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3276 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
3277 if (s->pict_type != AV_PICTURE_TYPE_B) {
3278 if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
3279 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3283 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3284 }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3286 for(i=0; i<s->mb_stride*s->mb_height; i++)
3287 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3289 if(!s->fixed_qscale){
3290 /* finding spatial complexity for I-frame rate control */
3291 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3294 for(i=1; i<context_count; i++){
3295 merge_context_after_me(s, s->thread_context[i]);
3297 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3298 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
3301 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
3302 s->pict_type= AV_PICTURE_TYPE_I;
3303 for(i=0; i<s->mb_stride*s->mb_height; i++)
3304 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3305 av_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
3306 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3310 if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3311 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3313 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3315 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3316 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3317 s->f_code= FFMAX3(s->f_code, a, b);
3320 ff_fix_long_p_mvs(s);
3321 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3322 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3326 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3327 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3332 if(s->pict_type==AV_PICTURE_TYPE_B){
3335 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3336 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3337 s->f_code = FFMAX(a, b);
3339 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3340 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3341 s->b_code = FFMAX(a, b);
3343 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3344 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3345 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3346 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3347 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3349 for(dir=0; dir<2; dir++){
3352 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3353 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3354 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3355 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3363 if (estimate_qp(s, 0) < 0)
3366 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==AV_PICTURE_TYPE_I && !(s->flags & CODEC_FLAG_QSCALE))
3367 s->qscale= 3; //reduce clipping problems
3369 if (s->out_format == FMT_MJPEG) {
3370 /* for mjpeg, we do include qscale in the matrix */
3372 int j = s->idsp.idct_permutation[i];
3374 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3376 s->y_dc_scale_table=
3377 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3378 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3379 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3380 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3384 //FIXME var duplication
3385 s->current_picture_ptr->f->key_frame =
3386 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3387 s->current_picture_ptr->f->pict_type =
3388 s->current_picture.f->pict_type = s->pict_type;
3390 if (s->current_picture.f->key_frame)
3391 s->picture_in_gop_number=0;
3393 s->last_bits= put_bits_count(&s->pb);
3394 switch(s->out_format) {
3396 if (CONFIG_MJPEG_ENCODER)
3397 ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
3401 if (CONFIG_H261_ENCODER)
3402 ff_h261_encode_picture_header(s, picture_number);
3405 if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3406 ff_wmv2_encode_picture_header(s, picture_number);
3407 else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3408 ff_msmpeg4_encode_picture_header(s, picture_number);
3409 else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3410 ff_mpeg4_encode_picture_header(s, picture_number);
3411 else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10)
3412 ff_rv10_encode_picture_header(s, picture_number);
3413 else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3414 ff_rv20_encode_picture_header(s, picture_number);
3415 else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3416 ff_flv_encode_picture_header(s, picture_number);
3417 else if (CONFIG_H263_ENCODER)
3418 ff_h263_encode_picture_header(s, picture_number);
3421 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3422 ff_mpeg1_encode_picture_header(s, picture_number);
3427 bits= put_bits_count(&s->pb);
3428 s->header_bits= bits - s->last_bits;
3430 for(i=1; i<context_count; i++){
3431 update_duplicate_context_after_me(s->thread_context[i], s);
3433 s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3434 for(i=1; i<context_count; i++){
3435 merge_context_after_encode(s, s->thread_context[i]);
3441 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3442 const int intra= s->mb_intra;
3445 s->dct_count[intra]++;
3447 for(i=0; i<64; i++){
3448 int level= block[i];
3452 s->dct_error_sum[intra][i] += level;
3453 level -= s->dct_offset[intra][i];
3454 if(level<0) level=0;
3456 s->dct_error_sum[intra][i] -= level;
3457 level += s->dct_offset[intra][i];
3458 if(level>0) level=0;
3465 static int dct_quantize_trellis_c(MpegEncContext *s,
3466 int16_t *block, int n,
3467 int qscale, int *overflow){
3469 const uint8_t *scantable= s->intra_scantable.scantable;
3470 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3472 unsigned int threshold1, threshold2;
3484 int coeff_count[64];
3485 int qmul, qadd, start_i, last_non_zero, i, dc;
3486 const int esc_length= s->ac_esc_length;
3488 uint8_t * last_length;
3489 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3491 s->fdsp.fdct(block);
3493 if(s->dct_error_sum)
3494 s->denoise_dct(s, block);
3496 qadd= ((qscale-1)|1)*8;
3507 /* For AIC we skip quant/dequant of INTRADC */
3512 /* note: block[0] is assumed to be positive */
3513 block[0] = (block[0] + (q >> 1)) / q;
3516 qmat = s->q_intra_matrix[qscale];
3517 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3518 bias= 1<<(QMAT_SHIFT-1);
3519 length = s->intra_ac_vlc_length;
3520 last_length= s->intra_ac_vlc_last_length;
3524 qmat = s->q_inter_matrix[qscale];
3525 length = s->inter_ac_vlc_length;
3526 last_length= s->inter_ac_vlc_last_length;
3530 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3531 threshold2= (threshold1<<1);
3533 for(i=63; i>=start_i; i--) {
3534 const int j = scantable[i];
3535 int level = block[j] * qmat[j];
3537 if(((unsigned)(level+threshold1))>threshold2){
3543 for(i=start_i; i<=last_non_zero; i++) {
3544 const int j = scantable[i];
3545 int level = block[j] * qmat[j];
3547 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3548 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3549 if(((unsigned)(level+threshold1))>threshold2){
3551 level= (bias + level)>>QMAT_SHIFT;
3553 coeff[1][i]= level-1;
3554 // coeff[2][k]= level-2;
3556 level= (bias - level)>>QMAT_SHIFT;
3557 coeff[0][i]= -level;
3558 coeff[1][i]= -level+1;
3559 // coeff[2][k]= -level+2;
3561 coeff_count[i]= FFMIN(level, 2);
3562 assert(coeff_count[i]);
3565 coeff[0][i]= (level>>31)|1;
3570 *overflow= s->max_qcoeff < max; //overflow might have happened
3572 if(last_non_zero < start_i){
3573 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3574 return last_non_zero;
3577 score_tab[start_i]= 0;
3578 survivor[0]= start_i;
3581 for(i=start_i; i<=last_non_zero; i++){
3582 int level_index, j, zero_distortion;
3583 int dct_coeff= FFABS(block[ scantable[i] ]);
3584 int best_score=256*256*256*120;
3586 if (s->fdsp.fdct == ff_fdct_ifast)
3587 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3588 zero_distortion= dct_coeff*dct_coeff;
3590 for(level_index=0; level_index < coeff_count[i]; level_index++){
3592 int level= coeff[level_index][i];
3593 const int alevel= FFABS(level);
3598 if(s->out_format == FMT_H263){
3599 unquant_coeff= alevel*qmul + qadd;
3601 j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
3603 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
3604 unquant_coeff = (unquant_coeff - 1) | 1;
3606 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3607 unquant_coeff = (unquant_coeff - 1) | 1;
3612 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3614 if((level&(~127)) == 0){
3615 for(j=survivor_count-1; j>=0; j--){
3616 int run= i - survivor[j];
3617 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3618 score += score_tab[i-run];
3620 if(score < best_score){
3623 level_tab[i+1]= level-64;
3627 if(s->out_format == FMT_H263){
3628 for(j=survivor_count-1; j>=0; j--){
3629 int run= i - survivor[j];
3630 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3631 score += score_tab[i-run];
3632 if(score < last_score){
3635 last_level= level-64;
3641 distortion += esc_length*lambda;
3642 for(j=survivor_count-1; j>=0; j--){
3643 int run= i - survivor[j];
3644 int score= distortion + score_tab[i-run];
3646 if(score < best_score){
3649 level_tab[i+1]= level-64;
3653 if(s->out_format == FMT_H263){
3654 for(j=survivor_count-1; j>=0; j--){
3655 int run= i - survivor[j];
3656 int score= distortion + score_tab[i-run];
3657 if(score < last_score){
3660 last_level= level-64;
3668 score_tab[i+1]= best_score;
3670 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3671 if(last_non_zero <= 27){
3672 for(; survivor_count; survivor_count--){
3673 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3677 for(; survivor_count; survivor_count--){
3678 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3683 survivor[ survivor_count++ ]= i+1;
3686 if(s->out_format != FMT_H263){
3687 last_score= 256*256*256*120;
3688 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3689 int score= score_tab[i];
3690 if(i) score += lambda*2; //FIXME exacter?
3692 if(score < last_score){
3695 last_level= level_tab[i];
3696 last_run= run_tab[i];
3701 s->coded_score[n] = last_score;
3703 dc= FFABS(block[0]);
3704 last_non_zero= last_i - 1;
3705 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3707 if(last_non_zero < start_i)
3708 return last_non_zero;
3710 if(last_non_zero == 0 && start_i == 0){
3712 int best_score= dc * dc;
3714 for(i=0; i<coeff_count[0]; i++){
3715 int level= coeff[i][0];
3716 int alevel= FFABS(level);
3717 int unquant_coeff, score, distortion;
3719 if(s->out_format == FMT_H263){
3720 unquant_coeff= (alevel*qmul + qadd)>>3;
3722 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3723 unquant_coeff = (unquant_coeff - 1) | 1;
3725 unquant_coeff = (unquant_coeff + 4) >> 3;
3726 unquant_coeff<<= 3 + 3;
3728 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3730 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3731 else score= distortion + esc_length*lambda;
3733 if(score < best_score){
3735 best_level= level - 64;
3738 block[0]= best_level;
3739 s->coded_score[n] = best_score - dc*dc;
3740 if(best_level == 0) return -1;
3741 else return last_non_zero;
3747 block[ perm_scantable[last_non_zero] ]= last_level;
3750 for(; i>start_i; i -= run_tab[i] + 1){
3751 block[ perm_scantable[i-1] ]= level_tab[i];
3754 return last_non_zero;
3757 //#define REFINE_STATS 1
3758 static int16_t basis[64][64];
3760 static void build_basis(uint8_t *perm){
3767 double s= 0.25*(1<<BASIS_SHIFT);
3769 int perm_index= perm[index];
3770 if(i==0) s*= sqrt(0.5);
3771 if(j==0) s*= sqrt(0.5);
3772 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)));
3779 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3780 int16_t *block, int16_t *weight, int16_t *orig,
3783 LOCAL_ALIGNED_16(int16_t, d1, [64]);
3784 const uint8_t *scantable= s->intra_scantable.scantable;
3785 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3786 // unsigned int threshold1, threshold2;
3791 int qmul, qadd, start_i, last_non_zero, i, dc;
3793 uint8_t * last_length;
3795 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3798 static int after_last=0;
3799 static int to_zero=0;
3800 static int from_zero=0;
3803 static int messed_sign=0;
3806 if(basis[0][0] == 0)
3807 build_basis(s->idsp.idct_permutation);
3818 /* For AIC we skip quant/dequant of INTRADC */
3822 q <<= RECON_SHIFT-3;
3823 /* note: block[0] is assumed to be positive */
3825 // block[0] = (block[0] + (q >> 1)) / q;
3827 // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3828 // bias= 1<<(QMAT_SHIFT-1);
3829 length = s->intra_ac_vlc_length;
3830 last_length= s->intra_ac_vlc_last_length;
3834 length = s->inter_ac_vlc_length;
3835 last_length= s->inter_ac_vlc_last_length;
3837 last_non_zero = s->block_last_index[n];
3842 dc += (1<<(RECON_SHIFT-1));
3843 for(i=0; i<64; i++){
3844 rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
3847 STOP_TIMER("memset rem[]")}
3850 for(i=0; i<64; i++){
3855 w= FFABS(weight[i]) + qns*one;
3856 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3859 // w=weight[i] = (63*qns + (w/2)) / w;
3865 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3871 for(i=start_i; i<=last_non_zero; i++){
3872 int j= perm_scantable[i];
3873 const int level= block[j];
3877 if(level<0) coeff= qmul*level - qadd;
3878 else coeff= qmul*level + qadd;
3879 run_tab[rle_index++]=run;
3882 s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
3888 if(last_non_zero>0){
3889 STOP_TIMER("init rem[]")
3896 int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
3899 int run2, best_unquant_change=0, analyze_gradient;
3903 analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
3905 if(analyze_gradient){
3909 for(i=0; i<64; i++){
3912 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
3915 STOP_TIMER("rem*w*w")}
3925 const int level= block[0];
3926 int change, old_coeff;
3928 assert(s->mb_intra);
3932 for(change=-1; change<=1; change+=2){
3933 int new_level= level + change;
3934 int score, new_coeff;
3936 new_coeff= q*new_level;
3937 if(new_coeff >= 2048 || new_coeff < 0)
3940 score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
3941 new_coeff - old_coeff);
3942 if(score<best_score){
3945 best_change= change;
3946 best_unquant_change= new_coeff - old_coeff;
3953 run2= run_tab[rle_index++];
3957 for(i=start_i; i<64; i++){
3958 int j= perm_scantable[i];
3959 const int level= block[j];
3960 int change, old_coeff;
3962 if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
3966 if(level<0) old_coeff= qmul*level - qadd;
3967 else old_coeff= qmul*level + qadd;
3968 run2= run_tab[rle_index++]; //FIXME ! maybe after last
3972 assert(run2>=0 || i >= last_non_zero );
3975 for(change=-1; change<=1; change+=2){
3976 int new_level= level + change;
3977 int score, new_coeff, unquant_change;
3980 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
3984 if(new_level<0) new_coeff= qmul*new_level - qadd;
3985 else new_coeff= qmul*new_level + qadd;
3986 if(new_coeff >= 2048 || new_coeff <= -2048)
3988 //FIXME check for overflow
3991 if(level < 63 && level > -63){
3992 if(i < last_non_zero)
3993 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
3994 - length[UNI_AC_ENC_INDEX(run, level+64)];
3996 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
3997 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4000 assert(FFABS(new_level)==1);
4002 if(analyze_gradient){
4003 int g= d1[ scantable[i] ];
4004 if(g && (g^new_level) >= 0)
4008 if(i < last_non_zero){
4009 int next_i= i + run2 + 1;
4010 int next_level= block[ perm_scantable[next_i] ] + 64;
4012 if(next_level&(~127))
4015 if(next_i < last_non_zero)
4016 score += length[UNI_AC_ENC_INDEX(run, 65)]
4017 + length[UNI_AC_ENC_INDEX(run2, next_level)]
4018 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4020 score += length[UNI_AC_ENC_INDEX(run, 65)]
4021 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4022 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4024 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4026 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4027 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4033 assert(FFABS(level)==1);
4035 if(i < last_non_zero){
4036 int next_i= i + run2 + 1;
4037 int next_level= block[ perm_scantable[next_i] ] + 64;
4039 if(next_level&(~127))
4042 if(next_i < last_non_zero)
4043 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4044 - length[UNI_AC_ENC_INDEX(run2, next_level)]
4045 - length[UNI_AC_ENC_INDEX(run, 65)];
4047 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4048 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4049 - length[UNI_AC_ENC_INDEX(run, 65)];
4051 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4053 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4054 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4061 unquant_change= new_coeff - old_coeff;
4062 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
4064 score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4066 if(score<best_score){
4069 best_change= change;
4070 best_unquant_change= unquant_change;
4074 prev_level= level + 64;
4075 if(prev_level&(~127))
4084 STOP_TIMER("iterative step")}
4088 int j= perm_scantable[ best_coeff ];
4090 block[j] += best_change;
4092 if(best_coeff > last_non_zero){
4093 last_non_zero= best_coeff;
4101 if(block[j] - best_change){
4102 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
4114 for(; last_non_zero>=start_i; last_non_zero--){
4115 if(block[perm_scantable[last_non_zero]])
4121 if(256*256*256*64 % count == 0){
4122 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);
4127 for(i=start_i; i<=last_non_zero; i++){
4128 int j= perm_scantable[i];
4129 const int level= block[j];
4132 run_tab[rle_index++]=run;
4139 s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4145 if(last_non_zero>0){
4146 STOP_TIMER("iterative search")
4151 return last_non_zero;
4154 int ff_dct_quantize_c(MpegEncContext *s,
4155 int16_t *block, int n,
4156 int qscale, int *overflow)
4158 int i, j, level, last_non_zero, q, start_i;
4160 const uint8_t *scantable= s->intra_scantable.scantable;
4163 unsigned int threshold1, threshold2;
4165 s->fdsp.fdct(block);
4167 if(s->dct_error_sum)
4168 s->denoise_dct(s, block);
4178 /* For AIC we skip quant/dequant of INTRADC */
4181 /* note: block[0] is assumed to be positive */
4182 block[0] = (block[0] + (q >> 1)) / q;
4185 qmat = s->q_intra_matrix[qscale];
4186 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4190 qmat = s->q_inter_matrix[qscale];
4191 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4193 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4194 threshold2= (threshold1<<1);
4195 for(i=63;i>=start_i;i--) {
4197 level = block[j] * qmat[j];
4199 if(((unsigned)(level+threshold1))>threshold2){
4206 for(i=start_i; i<=last_non_zero; i++) {
4208 level = block[j] * qmat[j];
4210 // if( bias+level >= (1<<QMAT_SHIFT)
4211 // || bias-level >= (1<<QMAT_SHIFT)){
4212 if(((unsigned)(level+threshold1))>threshold2){
4214 level= (bias + level)>>QMAT_SHIFT;
4217 level= (bias - level)>>QMAT_SHIFT;
4225 *overflow= s->max_qcoeff < max; //overflow might have happened
4227 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4228 if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
4229 ff_block_permute(block, s->idsp.idct_permutation,
4230 scantable, last_non_zero);
4232 return last_non_zero;
4235 #define OFFSET(x) offsetof(MpegEncContext, x)
4236 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4237 static const AVOption h263_options[] = {
4238 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4239 { "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},
4240 { "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 },
4245 static const AVClass h263_class = {
4246 .class_name = "H.263 encoder",
4247 .item_name = av_default_item_name,
4248 .option = h263_options,
4249 .version = LIBAVUTIL_VERSION_INT,
4252 AVCodec ff_h263_encoder = {
4254 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4255 .type = AVMEDIA_TYPE_VIDEO,
4256 .id = AV_CODEC_ID_H263,
4257 .priv_data_size = sizeof(MpegEncContext),
4258 .init = ff_MPV_encode_init,
4259 .encode2 = ff_MPV_encode_picture,
4260 .close = ff_MPV_encode_end,
4261 .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4262 .priv_class = &h263_class,
4265 static const AVOption h263p_options[] = {
4266 { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4267 { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4268 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4269 { "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},
4273 static const AVClass h263p_class = {
4274 .class_name = "H.263p encoder",
4275 .item_name = av_default_item_name,
4276 .option = h263p_options,
4277 .version = LIBAVUTIL_VERSION_INT,
4280 AVCodec ff_h263p_encoder = {
4282 .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4283 .type = AVMEDIA_TYPE_VIDEO,
4284 .id = AV_CODEC_ID_H263P,
4285 .priv_data_size = sizeof(MpegEncContext),
4286 .init = ff_MPV_encode_init,
4287 .encode2 = ff_MPV_encode_picture,
4288 .close = ff_MPV_encode_end,
4289 .capabilities = CODEC_CAP_SLICE_THREADS,
4290 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4291 .priv_class = &h263p_class,
4294 FF_MPV_GENERIC_CLASS(msmpeg4v2)
4296 AVCodec ff_msmpeg4v2_encoder = {
4297 .name = "msmpeg4v2",
4298 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
4299 .type = AVMEDIA_TYPE_VIDEO,
4300 .id = AV_CODEC_ID_MSMPEG4V2,
4301 .priv_data_size = sizeof(MpegEncContext),
4302 .init = ff_MPV_encode_init,
4303 .encode2 = ff_MPV_encode_picture,
4304 .close = ff_MPV_encode_end,
4305 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4306 .priv_class = &msmpeg4v2_class,
4309 FF_MPV_GENERIC_CLASS(msmpeg4v3)
4311 AVCodec ff_msmpeg4v3_encoder = {
4313 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
4314 .type = AVMEDIA_TYPE_VIDEO,
4315 .id = AV_CODEC_ID_MSMPEG4V3,
4316 .priv_data_size = sizeof(MpegEncContext),
4317 .init = ff_MPV_encode_init,
4318 .encode2 = ff_MPV_encode_picture,
4319 .close = ff_MPV_encode_end,
4320 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4321 .priv_class = &msmpeg4v3_class,
4324 FF_MPV_GENERIC_CLASS(wmv1)
4326 AVCodec ff_wmv1_encoder = {
4328 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
4329 .type = AVMEDIA_TYPE_VIDEO,
4330 .id = AV_CODEC_ID_WMV1,
4331 .priv_data_size = sizeof(MpegEncContext),
4332 .init = ff_MPV_encode_init,
4333 .encode2 = ff_MPV_encode_picture,
4334 .close = ff_MPV_encode_end,
4335 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4336 .priv_class = &wmv1_class,