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"
43 #include "mpegvideodata.h"
47 #include "mjpegenc_common.h"
49 #include "mpegutils.h"
52 #include "pixblockdsp.h"
56 #include "aandcttab.h"
58 #include "mpeg4video.h"
60 #include "bytestream.h"
64 #define QUANT_BIAS_SHIFT 8
66 #define QMAT_SHIFT_MMX 16
69 static int encode_picture(MpegEncContext *s, int picture_number);
70 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
71 static int sse_mb(MpegEncContext *s);
72 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
73 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
75 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
76 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
78 const AVOption ff_mpv_generic_options[] = {
83 void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
84 uint16_t (*qmat16)[2][64],
85 const uint16_t *quant_matrix,
86 int bias, int qmin, int qmax, int intra)
88 FDCTDSPContext *fdsp = &s->fdsp;
92 for (qscale = qmin; qscale <= qmax; qscale++) {
94 if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
96 fdsp->fdct == ff_faandct ||
97 #endif /* CONFIG_FAANDCT */
98 fdsp->fdct == ff_jpeg_fdct_islow_10) {
99 for (i = 0; i < 64; i++) {
100 const int j = s->idsp.idct_permutation[i];
101 int64_t den = (int64_t) qscale * quant_matrix[j];
102 /* 16 <= qscale * quant_matrix[i] <= 7905
103 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
104 * 19952 <= x <= 249205026
105 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
106 * 3444240 >= (1 << 36) / (x) >= 275 */
108 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / den);
110 } else if (fdsp->fdct == ff_fdct_ifast) {
111 for (i = 0; i < 64; i++) {
112 const int j = s->idsp.idct_permutation[i];
113 int64_t den = ff_aanscales[i] * (int64_t) qscale * quant_matrix[j];
114 /* 16 <= qscale * quant_matrix[i] <= 7905
115 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
116 * 19952 <= x <= 249205026
117 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
118 * 3444240 >= (1 << 36) / (x) >= 275 */
120 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) / den);
123 for (i = 0; i < 64; i++) {
124 const int j = s->idsp.idct_permutation[i];
125 int64_t den = (int64_t) qscale * quant_matrix[j];
126 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
127 * Assume x = qscale * quant_matrix[i]
129 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
130 * so 32768 >= (1 << 19) / (x) >= 67 */
131 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / den);
132 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
133 // (qscale * quant_matrix[i]);
134 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / den;
136 if (qmat16[qscale][0][i] == 0 ||
137 qmat16[qscale][0][i] == 128 * 256)
138 qmat16[qscale][0][i] = 128 * 256 - 1;
139 qmat16[qscale][1][i] =
140 ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
141 qmat16[qscale][0][i]);
145 for (i = intra; i < 64; i++) {
147 if (fdsp->fdct == ff_fdct_ifast) {
148 max = (8191LL * ff_aanscales[i]) >> 14;
150 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
156 av_log(NULL, AV_LOG_INFO,
157 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
162 static inline void update_qscale(MpegEncContext *s)
164 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
165 (FF_LAMBDA_SHIFT + 7);
166 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
168 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
172 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
178 for (i = 0; i < 64; i++) {
179 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
186 * init s->current_picture.qscale_table from s->lambda_table
188 void ff_init_qscale_tab(MpegEncContext *s)
190 int8_t * const qscale_table = s->current_picture.qscale_table;
193 for (i = 0; i < s->mb_num; i++) {
194 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
195 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
196 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
201 static void update_duplicate_context_after_me(MpegEncContext *dst,
204 #define COPY(a) dst->a= src->a
206 COPY(current_picture);
212 COPY(picture_in_gop_number);
213 COPY(gop_picture_number);
214 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
215 COPY(progressive_frame); // FIXME don't set in encode_header
216 COPY(partitioned_frame); // FIXME don't set in encode_header
221 * Set the given MpegEncContext to defaults for encoding.
222 * the changed fields will not depend upon the prior state of the MpegEncContext.
224 static void mpv_encode_defaults(MpegEncContext *s)
227 ff_mpv_common_defaults(s);
229 for (i = -16; i < 16; i++) {
230 default_fcode_tab[i + MAX_MV] = 1;
232 s->me.mv_penalty = default_mv_penalty;
233 s->fcode_tab = default_fcode_tab;
235 s->input_picture_number = 0;
236 s->picture_in_gop_number = 0;
239 /* init video encoder */
240 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
242 MpegEncContext *s = avctx->priv_data;
243 int i, ret, format_supported;
245 mpv_encode_defaults(s);
247 switch (avctx->codec_id) {
248 case AV_CODEC_ID_MPEG2VIDEO:
249 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
250 avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
251 av_log(avctx, AV_LOG_ERROR,
252 "only YUV420 and YUV422 are supported\n");
256 case AV_CODEC_ID_MJPEG:
257 format_supported = 0;
258 /* JPEG color space */
259 if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
260 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
261 (avctx->color_range == AVCOL_RANGE_JPEG &&
262 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
263 avctx->pix_fmt == AV_PIX_FMT_YUV422P)))
264 format_supported = 1;
265 /* MPEG color space */
266 else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
267 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
268 avctx->pix_fmt == AV_PIX_FMT_YUV422P))
269 format_supported = 1;
271 if (!format_supported) {
272 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
277 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
278 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
283 switch (avctx->pix_fmt) {
284 case AV_PIX_FMT_YUVJ422P:
285 case AV_PIX_FMT_YUV422P:
286 s->chroma_format = CHROMA_422;
288 case AV_PIX_FMT_YUVJ420P:
289 case AV_PIX_FMT_YUV420P:
291 s->chroma_format = CHROMA_420;
295 s->bit_rate = avctx->bit_rate;
296 s->width = avctx->width;
297 s->height = avctx->height;
298 if (avctx->gop_size > 600 &&
299 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
300 av_log(avctx, AV_LOG_ERROR,
301 "Warning keyframe interval too large! reducing it ...\n");
302 avctx->gop_size = 600;
304 s->gop_size = avctx->gop_size;
306 if (avctx->max_b_frames > MAX_B_FRAMES) {
307 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
308 "is %d.\n", MAX_B_FRAMES);
310 s->max_b_frames = avctx->max_b_frames;
311 s->codec_id = avctx->codec->id;
312 s->strict_std_compliance = avctx->strict_std_compliance;
313 s->quarter_sample = (avctx->flags & CODEC_FLAG_QPEL) != 0;
314 s->mpeg_quant = avctx->mpeg_quant;
315 s->rtp_mode = !!avctx->rtp_payload_size;
316 s->intra_dc_precision = avctx->intra_dc_precision;
317 s->user_specified_pts = AV_NOPTS_VALUE;
319 if (s->gop_size <= 1) {
326 s->me_method = avctx->me_method;
329 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
332 FF_DISABLE_DEPRECATION_WARNINGS
333 if (avctx->border_masking != 0.0)
334 s->border_masking = avctx->border_masking;
335 FF_ENABLE_DEPRECATION_WARNINGS
338 s->adaptive_quant = (s->avctx->lumi_masking ||
339 s->avctx->dark_masking ||
340 s->avctx->temporal_cplx_masking ||
341 s->avctx->spatial_cplx_masking ||
342 s->avctx->p_masking ||
344 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
347 s->loop_filter = !!(s->avctx->flags & CODEC_FLAG_LOOP_FILTER);
349 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
350 av_log(avctx, AV_LOG_ERROR,
351 "a vbv buffer size is needed, "
352 "for encoding with a maximum bitrate\n");
356 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
357 av_log(avctx, AV_LOG_INFO,
358 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
361 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
362 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
366 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
367 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
371 if (avctx->rc_max_rate &&
372 avctx->rc_max_rate == avctx->bit_rate &&
373 avctx->rc_max_rate != avctx->rc_min_rate) {
374 av_log(avctx, AV_LOG_INFO,
375 "impossible bitrate constraints, this will fail\n");
378 if (avctx->rc_buffer_size &&
379 avctx->bit_rate * (int64_t)avctx->time_base.num >
380 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
381 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
385 if (!s->fixed_qscale &&
386 avctx->bit_rate * av_q2d(avctx->time_base) >
387 avctx->bit_rate_tolerance) {
388 av_log(avctx, AV_LOG_ERROR,
389 "bitrate tolerance too small for bitrate\n");
393 if (s->avctx->rc_max_rate &&
394 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
395 (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
396 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
397 90000LL * (avctx->rc_buffer_size - 1) >
398 s->avctx->rc_max_rate * 0xFFFFLL) {
399 av_log(avctx, AV_LOG_INFO,
400 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
401 "specified vbv buffer is too large for the given bitrate!\n");
404 if ((s->avctx->flags & CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
405 s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
406 s->codec_id != AV_CODEC_ID_FLV1) {
407 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
411 if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
412 av_log(avctx, AV_LOG_ERROR,
413 "OBMC is only supported with simple mb decision\n");
417 if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
418 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
422 if (s->max_b_frames &&
423 s->codec_id != AV_CODEC_ID_MPEG4 &&
424 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
425 s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
426 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
430 if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
431 s->codec_id == AV_CODEC_ID_H263 ||
432 s->codec_id == AV_CODEC_ID_H263P) &&
433 (avctx->sample_aspect_ratio.num > 255 ||
434 avctx->sample_aspect_ratio.den > 255)) {
435 av_log(avctx, AV_LOG_ERROR,
436 "Invalid pixel aspect ratio %i/%i, limit is 255/255\n",
437 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
441 if ((s->avctx->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
442 s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
443 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
447 // FIXME mpeg2 uses that too
448 if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
449 av_log(avctx, AV_LOG_ERROR,
450 "mpeg2 style quantization not supported by codec\n");
454 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
455 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
459 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
460 s->avctx->mb_decision != FF_MB_DECISION_RD) {
461 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
465 if (s->avctx->scenechange_threshold < 1000000000 &&
466 (s->avctx->flags & CODEC_FLAG_CLOSED_GOP)) {
467 av_log(avctx, AV_LOG_ERROR,
468 "closed gop with scene change detection are not supported yet, "
469 "set threshold to 1000000000\n");
473 if (s->avctx->flags & CODEC_FLAG_LOW_DELAY) {
474 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
475 av_log(avctx, AV_LOG_ERROR,
476 "low delay forcing is only available for mpeg2\n");
479 if (s->max_b_frames != 0) {
480 av_log(avctx, AV_LOG_ERROR,
481 "b frames cannot be used with low delay\n");
486 if (s->q_scale_type == 1) {
487 if (avctx->qmax > 12) {
488 av_log(avctx, AV_LOG_ERROR,
489 "non linear quant only supports qmax <= 12 currently\n");
494 if (s->avctx->thread_count > 1 &&
495 s->codec_id != AV_CODEC_ID_MPEG4 &&
496 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
497 s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
498 (s->codec_id != AV_CODEC_ID_H263P)) {
499 av_log(avctx, AV_LOG_ERROR,
500 "multi threaded encoding not supported by codec\n");
504 if (s->avctx->thread_count < 1) {
505 av_log(avctx, AV_LOG_ERROR,
506 "automatic thread number detection not supported by codec,"
511 if (s->avctx->thread_count > 1)
514 if (!avctx->time_base.den || !avctx->time_base.num) {
515 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
519 if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
520 av_log(avctx, AV_LOG_INFO,
521 "notice: b_frame_strategy only affects the first pass\n");
522 avctx->b_frame_strategy = 0;
525 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
527 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
528 avctx->time_base.den /= i;
529 avctx->time_base.num /= i;
533 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
534 s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG) {
535 // (a + x * 3 / 8) / x
536 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
537 s->inter_quant_bias = 0;
539 s->intra_quant_bias = 0;
541 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
544 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
545 s->intra_quant_bias = avctx->intra_quant_bias;
546 if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
547 s->inter_quant_bias = avctx->inter_quant_bias;
549 if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
550 s->avctx->time_base.den > (1 << 16) - 1) {
551 av_log(avctx, AV_LOG_ERROR,
552 "timebase %d/%d not supported by MPEG 4 standard, "
553 "the maximum admitted value for the timebase denominator "
554 "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
558 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
560 switch (avctx->codec->id) {
561 case AV_CODEC_ID_MPEG1VIDEO:
562 s->out_format = FMT_MPEG1;
563 s->low_delay = !!(s->avctx->flags & CODEC_FLAG_LOW_DELAY);
564 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
566 case AV_CODEC_ID_MPEG2VIDEO:
567 s->out_format = FMT_MPEG1;
568 s->low_delay = !!(s->avctx->flags & CODEC_FLAG_LOW_DELAY);
569 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
572 case AV_CODEC_ID_MJPEG:
573 s->out_format = FMT_MJPEG;
574 s->intra_only = 1; /* force intra only for jpeg */
575 if (!CONFIG_MJPEG_ENCODER ||
576 ff_mjpeg_encode_init(s) < 0)
581 case AV_CODEC_ID_H261:
582 if (!CONFIG_H261_ENCODER)
584 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
585 av_log(avctx, AV_LOG_ERROR,
586 "The specified picture size of %dx%d is not valid for the "
587 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
588 s->width, s->height);
591 s->out_format = FMT_H261;
594 s->rtp_mode = 0; /* Sliced encoding not supported */
596 case AV_CODEC_ID_H263:
597 if (!CONFIG_H263_ENCODER)
599 if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
600 s->width, s->height) == 8) {
601 av_log(avctx, AV_LOG_INFO,
602 "The specified picture size of %dx%d is not valid for "
603 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
604 "352x288, 704x576, and 1408x1152."
605 "Try H.263+.\n", s->width, s->height);
608 s->out_format = FMT_H263;
612 case AV_CODEC_ID_H263P:
613 s->out_format = FMT_H263;
616 s->h263_aic = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
617 s->modified_quant = s->h263_aic;
618 s->loop_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
619 s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
622 /* These are just to be sure */
626 case AV_CODEC_ID_FLV1:
627 s->out_format = FMT_H263;
628 s->h263_flv = 2; /* format = 1; 11-bit codes */
629 s->unrestricted_mv = 1;
630 s->rtp_mode = 0; /* don't allow GOB */
634 case AV_CODEC_ID_RV10:
635 s->out_format = FMT_H263;
639 case AV_CODEC_ID_RV20:
640 s->out_format = FMT_H263;
643 s->modified_quant = 1;
647 s->unrestricted_mv = 0;
649 case AV_CODEC_ID_MPEG4:
650 s->out_format = FMT_H263;
652 s->unrestricted_mv = 1;
653 s->low_delay = s->max_b_frames ? 0 : 1;
654 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
656 case AV_CODEC_ID_MSMPEG4V2:
657 s->out_format = FMT_H263;
659 s->unrestricted_mv = 1;
660 s->msmpeg4_version = 2;
664 case AV_CODEC_ID_MSMPEG4V3:
665 s->out_format = FMT_H263;
667 s->unrestricted_mv = 1;
668 s->msmpeg4_version = 3;
669 s->flipflop_rounding = 1;
673 case AV_CODEC_ID_WMV1:
674 s->out_format = FMT_H263;
676 s->unrestricted_mv = 1;
677 s->msmpeg4_version = 4;
678 s->flipflop_rounding = 1;
682 case AV_CODEC_ID_WMV2:
683 s->out_format = FMT_H263;
685 s->unrestricted_mv = 1;
686 s->msmpeg4_version = 5;
687 s->flipflop_rounding = 1;
695 avctx->has_b_frames = !s->low_delay;
699 s->progressive_frame =
700 s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
701 CODEC_FLAG_INTERLACED_ME) ||
706 if (ff_mpv_common_init(s) < 0)
710 ff_mpv_encode_init_x86(s);
712 ff_fdctdsp_init(&s->fdsp, avctx);
713 ff_me_cmp_init(&s->mecc, avctx);
714 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
715 ff_pixblockdsp_init(&s->pdsp, avctx);
716 ff_qpeldsp_init(&s->qdsp);
718 s->avctx->coded_frame = s->current_picture.f;
720 if (s->msmpeg4_version) {
721 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
722 2 * 2 * (MAX_LEVEL + 1) *
723 (MAX_RUN + 1) * 2 * sizeof(int), fail);
725 FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
727 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail);
728 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail);
729 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
730 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
731 FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
732 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
733 FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
734 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
736 if (s->avctx->noise_reduction) {
737 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
738 2 * 64 * sizeof(uint16_t), fail);
741 if (CONFIG_H263_ENCODER)
742 ff_h263dsp_init(&s->h263dsp);
743 if (!s->dct_quantize)
744 s->dct_quantize = ff_dct_quantize_c;
746 s->denoise_dct = denoise_dct_c;
747 s->fast_dct_quantize = s->dct_quantize;
749 s->dct_quantize = dct_quantize_trellis_c;
751 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
752 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
754 s->quant_precision = 5;
756 ff_set_cmp(&s->mecc, s->mecc.ildct_cmp, s->avctx->ildct_cmp);
757 ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->avctx->frame_skip_cmp);
759 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
760 ff_h261_encode_init(s);
761 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
762 ff_h263_encode_init(s);
763 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
764 if ((ret = ff_msmpeg4_encode_init(s)) < 0)
766 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
767 && s->out_format == FMT_MPEG1)
768 ff_mpeg1_encode_init(s);
771 for (i = 0; i < 64; i++) {
772 int j = s->idsp.idct_permutation[i];
773 if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
775 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
776 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
777 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
779 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
782 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
783 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
785 if (s->avctx->intra_matrix)
786 s->intra_matrix[j] = s->avctx->intra_matrix[i];
787 if (s->avctx->inter_matrix)
788 s->inter_matrix[j] = s->avctx->inter_matrix[i];
791 /* precompute matrix */
792 /* for mjpeg, we do include qscale in the matrix */
793 if (s->out_format != FMT_MJPEG) {
794 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
795 s->intra_matrix, s->intra_quant_bias, avctx->qmin,
797 ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
798 s->inter_matrix, s->inter_quant_bias, avctx->qmin,
802 if (ff_rate_control_init(s) < 0)
805 #if FF_API_ERROR_RATE
806 FF_DISABLE_DEPRECATION_WARNINGS
807 if (avctx->error_rate)
808 s->error_rate = avctx->error_rate;
809 FF_ENABLE_DEPRECATION_WARNINGS;
812 #if FF_API_NORMALIZE_AQP
813 FF_DISABLE_DEPRECATION_WARNINGS
814 if (avctx->flags & CODEC_FLAG_NORMALIZE_AQP)
815 s->mpv_flags |= FF_MPV_FLAG_NAQ;
816 FF_ENABLE_DEPRECATION_WARNINGS;
820 FF_DISABLE_DEPRECATION_WARNINGS
821 if (avctx->flags & CODEC_FLAG_MV0)
822 s->mpv_flags |= FF_MPV_FLAG_MV0;
823 FF_ENABLE_DEPRECATION_WARNINGS
827 FF_DISABLE_DEPRECATION_WARNINGS
828 if (avctx->rc_qsquish != 0.0)
829 s->rc_qsquish = avctx->rc_qsquish;
830 if (avctx->rc_qmod_amp != 0.0)
831 s->rc_qmod_amp = avctx->rc_qmod_amp;
832 if (avctx->rc_qmod_freq)
833 s->rc_qmod_freq = avctx->rc_qmod_freq;
834 if (avctx->rc_buffer_aggressivity != 1.0)
835 s->rc_buffer_aggressivity = avctx->rc_buffer_aggressivity;
836 if (avctx->rc_initial_cplx != 0.0)
837 s->rc_initial_cplx = avctx->rc_initial_cplx;
839 s->lmin = avctx->lmin;
841 s->lmax = avctx->lmax;
845 s->rc_eq = av_strdup(avctx->rc_eq);
847 return AVERROR(ENOMEM);
849 FF_ENABLE_DEPRECATION_WARNINGS
852 if (avctx->b_frame_strategy == 2) {
853 for (i = 0; i < s->max_b_frames + 2; i++) {
854 s->tmp_frames[i] = av_frame_alloc();
855 if (!s->tmp_frames[i])
856 return AVERROR(ENOMEM);
858 s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
859 s->tmp_frames[i]->width = s->width >> avctx->brd_scale;
860 s->tmp_frames[i]->height = s->height >> avctx->brd_scale;
862 ret = av_frame_get_buffer(s->tmp_frames[i], 32);
870 ff_mpv_encode_end(avctx);
871 return AVERROR_UNKNOWN;
874 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
876 MpegEncContext *s = avctx->priv_data;
879 ff_rate_control_uninit(s);
881 ff_mpv_common_end(s);
882 if (CONFIG_MJPEG_ENCODER &&
883 s->out_format == FMT_MJPEG)
884 ff_mjpeg_encode_close(s);
886 av_freep(&avctx->extradata);
888 for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
889 av_frame_free(&s->tmp_frames[i]);
891 ff_free_picture_tables(&s->new_picture);
892 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
894 av_freep(&s->avctx->stats_out);
895 av_freep(&s->ac_stats);
897 av_freep(&s->q_intra_matrix);
898 av_freep(&s->q_inter_matrix);
899 av_freep(&s->q_intra_matrix16);
900 av_freep(&s->q_inter_matrix16);
901 av_freep(&s->input_picture);
902 av_freep(&s->reordered_input_picture);
903 av_freep(&s->dct_offset);
908 static int get_sae(uint8_t *src, int ref, int stride)
913 for (y = 0; y < 16; y++) {
914 for (x = 0; x < 16; x++) {
915 acc += FFABS(src[x + y * stride] - ref);
922 static int get_intra_count(MpegEncContext *s, uint8_t *src,
923 uint8_t *ref, int stride)
931 for (y = 0; y < h; y += 16) {
932 for (x = 0; x < w; x += 16) {
933 int offset = x + y * stride;
934 int sad = s->mecc.sad[0](NULL, src + offset, ref + offset,
936 int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
937 int sae = get_sae(src + offset, mean, stride);
939 acc += sae + 500 < sad;
945 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
947 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,
948 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
949 s->mb_stride, s->mb_height, s->b8_stride,
950 &s->linesize, &s->uvlinesize);
953 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
957 int i, display_picture_number = 0, ret;
958 const int encoding_delay = s->max_b_frames ? s->max_b_frames :
959 (s->low_delay ? 0 : 1);
964 display_picture_number = s->input_picture_number++;
966 if (pts != AV_NOPTS_VALUE) {
967 if (s->user_specified_pts != AV_NOPTS_VALUE) {
969 int64_t last = s->user_specified_pts;
972 av_log(s->avctx, AV_LOG_ERROR,
973 "Error, Invalid timestamp=%"PRId64", "
974 "last=%"PRId64"\n", pts, s->user_specified_pts);
978 if (!s->low_delay && display_picture_number == 1)
979 s->dts_delta = time - last;
981 s->user_specified_pts = pts;
983 if (s->user_specified_pts != AV_NOPTS_VALUE) {
984 s->user_specified_pts =
985 pts = s->user_specified_pts + 1;
986 av_log(s->avctx, AV_LOG_INFO,
987 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
990 pts = display_picture_number;
996 if (!pic_arg->buf[0] ||
997 pic_arg->linesize[0] != s->linesize ||
998 pic_arg->linesize[1] != s->uvlinesize ||
999 pic_arg->linesize[2] != s->uvlinesize)
1001 if ((s->width & 15) || (s->height & 15))
1004 ff_dlog(s->avctx, "%d %d %td %td\n", pic_arg->linesize[0],
1005 pic_arg->linesize[1], s->linesize, s->uvlinesize);
1007 i = ff_find_unused_picture(s->avctx, s->picture, direct);
1011 pic = &s->picture[i];
1015 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1018 ret = alloc_picture(s, pic, direct);
1023 if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1024 pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1025 pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1028 int h_chroma_shift, v_chroma_shift;
1029 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1033 for (i = 0; i < 3; i++) {
1034 int src_stride = pic_arg->linesize[i];
1035 int dst_stride = i ? s->uvlinesize : s->linesize;
1036 int h_shift = i ? h_chroma_shift : 0;
1037 int v_shift = i ? v_chroma_shift : 0;
1038 int w = s->width >> h_shift;
1039 int h = s->height >> v_shift;
1040 uint8_t *src = pic_arg->data[i];
1041 uint8_t *dst = pic->f->data[i];
1043 if (!s->avctx->rc_buffer_size)
1044 dst += INPLACE_OFFSET;
1046 if (src_stride == dst_stride)
1047 memcpy(dst, src, src_stride * h);
1050 uint8_t *dst2 = dst;
1052 memcpy(dst2, src, w);
1057 if ((s->width & 15) || (s->height & 15)) {
1058 s->mpvencdsp.draw_edges(dst, dst_stride,
1067 ret = av_frame_copy_props(pic->f, pic_arg);
1071 pic->f->display_picture_number = display_picture_number;
1072 pic->f->pts = pts; // we set this here to avoid modifiying pic_arg
1075 /* shift buffer entries */
1076 for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1077 s->input_picture[i - 1] = s->input_picture[i];
1079 s->input_picture[encoding_delay] = (Picture*) pic;
1084 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1088 int64_t score64 = 0;
1090 for (plane = 0; plane < 3; plane++) {
1091 const int stride = p->f->linesize[plane];
1092 const int bw = plane ? 1 : 2;
1093 for (y = 0; y < s->mb_height * bw; y++) {
1094 for (x = 0; x < s->mb_width * bw; x++) {
1095 int off = p->shared ? 0 : 16;
1096 uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1097 uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1098 int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1100 switch (s->avctx->frame_skip_exp) {
1101 case 0: score = FFMAX(score, v); break;
1102 case 1: score += FFABS(v); break;
1103 case 2: score += v * v; break;
1104 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1105 case 4: score64 += v * v * (int64_t)(v * v); break;
1114 if (score64 < s->avctx->frame_skip_threshold)
1116 if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
1121 static int encode_frame(AVCodecContext *c, AVFrame *frame)
1123 AVPacket pkt = { 0 };
1124 int ret, got_output;
1126 av_init_packet(&pkt);
1127 ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
1132 av_free_packet(&pkt);
1136 static int estimate_best_b_count(MpegEncContext *s)
1138 AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1139 AVCodecContext *c = avcodec_alloc_context3(NULL);
1140 const int scale = s->avctx->brd_scale;
1141 int i, j, out_size, p_lambda, b_lambda, lambda2;
1142 int64_t best_rd = INT64_MAX;
1143 int best_b_count = -1;
1146 return AVERROR(ENOMEM);
1147 assert(scale >= 0 && scale <= 3);
1150 //s->next_picture_ptr->quality;
1151 p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1152 //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1153 b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1154 if (!b_lambda) // FIXME we should do this somewhere else
1155 b_lambda = p_lambda;
1156 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1159 c->width = s->width >> scale;
1160 c->height = s->height >> scale;
1161 c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;
1162 c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;
1163 c->mb_decision = s->avctx->mb_decision;
1164 c->me_cmp = s->avctx->me_cmp;
1165 c->mb_cmp = s->avctx->mb_cmp;
1166 c->me_sub_cmp = s->avctx->me_sub_cmp;
1167 c->pix_fmt = AV_PIX_FMT_YUV420P;
1168 c->time_base = s->avctx->time_base;
1169 c->max_b_frames = s->max_b_frames;
1171 if (avcodec_open2(c, codec, NULL) < 0)
1174 for (i = 0; i < s->max_b_frames + 2; i++) {
1175 Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1176 s->next_picture_ptr;
1178 if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1179 pre_input = *pre_input_ptr;
1181 if (!pre_input.shared && i) {
1182 pre_input.f->data[0] += INPLACE_OFFSET;
1183 pre_input.f->data[1] += INPLACE_OFFSET;
1184 pre_input.f->data[2] += INPLACE_OFFSET;
1187 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1188 s->tmp_frames[i]->linesize[0],
1189 pre_input.f->data[0],
1190 pre_input.f->linesize[0],
1191 c->width, c->height);
1192 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1193 s->tmp_frames[i]->linesize[1],
1194 pre_input.f->data[1],
1195 pre_input.f->linesize[1],
1196 c->width >> 1, c->height >> 1);
1197 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1198 s->tmp_frames[i]->linesize[2],
1199 pre_input.f->data[2],
1200 pre_input.f->linesize[2],
1201 c->width >> 1, c->height >> 1);
1205 for (j = 0; j < s->max_b_frames + 1; j++) {
1208 if (!s->input_picture[j])
1211 c->error[0] = c->error[1] = c->error[2] = 0;
1213 s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1214 s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1216 out_size = encode_frame(c, s->tmp_frames[0]);
1218 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1220 for (i = 0; i < s->max_b_frames + 1; i++) {
1221 int is_p = i % (j + 1) == j || i == s->max_b_frames;
1223 s->tmp_frames[i + 1]->pict_type = is_p ?
1224 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1225 s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1227 out_size = encode_frame(c, s->tmp_frames[i + 1]);
1229 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1232 /* get the delayed frames */
1234 out_size = encode_frame(c, NULL);
1235 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1238 rd += c->error[0] + c->error[1] + c->error[2];
1249 return best_b_count;
1252 static int select_input_picture(MpegEncContext *s)
1256 for (i = 1; i < MAX_PICTURE_COUNT; i++)
1257 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1258 s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1260 /* set next picture type & ordering */
1261 if (!s->reordered_input_picture[0] && s->input_picture[0]) {
1262 if (/*s->picture_in_gop_number >= s->gop_size ||*/
1263 !s->next_picture_ptr || s->intra_only) {
1264 s->reordered_input_picture[0] = s->input_picture[0];
1265 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1266 s->reordered_input_picture[0]->f->coded_picture_number =
1267 s->coded_picture_number++;
1271 if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1272 if (s->picture_in_gop_number < s->gop_size &&
1273 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1274 // FIXME check that te gop check above is +-1 correct
1275 av_frame_unref(s->input_picture[0]->f);
1278 ff_vbv_update(s, 0);
1284 if (s->avctx->flags & CODEC_FLAG_PASS2) {
1285 for (i = 0; i < s->max_b_frames + 1; i++) {
1286 int pict_num = s->input_picture[0]->f->display_picture_number + i;
1288 if (pict_num >= s->rc_context.num_entries)
1290 if (!s->input_picture[i]) {
1291 s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1295 s->input_picture[i]->f->pict_type =
1296 s->rc_context.entry[pict_num].new_pict_type;
1300 if (s->avctx->b_frame_strategy == 0) {
1301 b_frames = s->max_b_frames;
1302 while (b_frames && !s->input_picture[b_frames])
1304 } else if (s->avctx->b_frame_strategy == 1) {
1305 for (i = 1; i < s->max_b_frames + 1; i++) {
1306 if (s->input_picture[i] &&
1307 s->input_picture[i]->b_frame_score == 0) {
1308 s->input_picture[i]->b_frame_score =
1310 s->input_picture[i ]->f->data[0],
1311 s->input_picture[i - 1]->f->data[0],
1315 for (i = 0; i < s->max_b_frames + 1; i++) {
1316 if (!s->input_picture[i] ||
1317 s->input_picture[i]->b_frame_score - 1 >
1318 s->mb_num / s->avctx->b_sensitivity)
1322 b_frames = FFMAX(0, i - 1);
1325 for (i = 0; i < b_frames + 1; i++) {
1326 s->input_picture[i]->b_frame_score = 0;
1328 } else if (s->avctx->b_frame_strategy == 2) {
1329 b_frames = estimate_best_b_count(s);
1331 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1337 for (i = b_frames - 1; i >= 0; i--) {
1338 int type = s->input_picture[i]->f->pict_type;
1339 if (type && type != AV_PICTURE_TYPE_B)
1342 if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1343 b_frames == s->max_b_frames) {
1344 av_log(s->avctx, AV_LOG_ERROR,
1345 "warning, too many b frames in a row\n");
1348 if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1349 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1350 s->gop_size > s->picture_in_gop_number) {
1351 b_frames = s->gop_size - s->picture_in_gop_number - 1;
1353 if (s->avctx->flags & CODEC_FLAG_CLOSED_GOP)
1355 s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1359 if ((s->avctx->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1360 s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1363 s->reordered_input_picture[0] = s->input_picture[b_frames];
1364 if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1365 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1366 s->reordered_input_picture[0]->f->coded_picture_number =
1367 s->coded_picture_number++;
1368 for (i = 0; i < b_frames; i++) {
1369 s->reordered_input_picture[i + 1] = s->input_picture[i];
1370 s->reordered_input_picture[i + 1]->f->pict_type =
1372 s->reordered_input_picture[i + 1]->f->coded_picture_number =
1373 s->coded_picture_number++;
1378 if (s->reordered_input_picture[0]) {
1379 s->reordered_input_picture[0]->reference =
1380 s->reordered_input_picture[0]->f->pict_type !=
1381 AV_PICTURE_TYPE_B ? 3 : 0;
1383 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1384 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))
1387 if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1388 // input is a shared pix, so we can't modifiy it -> alloc a new
1389 // one & ensure that the shared one is reuseable
1392 int i = ff_find_unused_picture(s->avctx, s->picture, 0);
1395 pic = &s->picture[i];
1397 pic->reference = s->reordered_input_picture[0]->reference;
1398 if (alloc_picture(s, pic, 0) < 0) {
1402 ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1406 /* mark us unused / free shared pic */
1407 av_frame_unref(s->reordered_input_picture[0]->f);
1408 s->reordered_input_picture[0]->shared = 0;
1410 s->current_picture_ptr = pic;
1412 // input is not a shared pix -> reuse buffer for current_pix
1413 s->current_picture_ptr = s->reordered_input_picture[0];
1414 for (i = 0; i < 4; i++) {
1415 s->new_picture.f->data[i] += INPLACE_OFFSET;
1418 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1419 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1420 s->current_picture_ptr)) < 0)
1423 s->picture_number = s->new_picture.f->display_picture_number;
1425 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1430 static void frame_end(MpegEncContext *s)
1434 if (s->unrestricted_mv &&
1435 s->current_picture.reference &&
1437 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1438 int hshift = desc->log2_chroma_w;
1439 int vshift = desc->log2_chroma_h;
1440 s->mpvencdsp.draw_edges(s->current_picture.f->data[0], s->linesize,
1441 s->h_edge_pos, s->v_edge_pos,
1442 EDGE_WIDTH, EDGE_WIDTH,
1443 EDGE_TOP | EDGE_BOTTOM);
1444 s->mpvencdsp.draw_edges(s->current_picture.f->data[1], s->uvlinesize,
1445 s->h_edge_pos >> hshift,
1446 s->v_edge_pos >> vshift,
1447 EDGE_WIDTH >> hshift,
1448 EDGE_WIDTH >> vshift,
1449 EDGE_TOP | EDGE_BOTTOM);
1450 s->mpvencdsp.draw_edges(s->current_picture.f->data[2], s->uvlinesize,
1451 s->h_edge_pos >> hshift,
1452 s->v_edge_pos >> vshift,
1453 EDGE_WIDTH >> hshift,
1454 EDGE_WIDTH >> vshift,
1455 EDGE_TOP | EDGE_BOTTOM);
1460 s->last_pict_type = s->pict_type;
1461 s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1462 if (s->pict_type!= AV_PICTURE_TYPE_B)
1463 s->last_non_b_pict_type = s->pict_type;
1466 /* release non-reference frames */
1467 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1468 if (!s->picture[i].reference)
1469 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1473 s->avctx->coded_frame = s->current_picture_ptr->f;
1477 static void update_noise_reduction(MpegEncContext *s)
1481 for (intra = 0; intra < 2; intra++) {
1482 if (s->dct_count[intra] > (1 << 16)) {
1483 for (i = 0; i < 64; i++) {
1484 s->dct_error_sum[intra][i] >>= 1;
1486 s->dct_count[intra] >>= 1;
1489 for (i = 0; i < 64; i++) {
1490 s->dct_offset[intra][i] = (s->avctx->noise_reduction *
1491 s->dct_count[intra] +
1492 s->dct_error_sum[intra][i] / 2) /
1493 (s->dct_error_sum[intra][i] + 1);
1498 static int frame_start(MpegEncContext *s)
1502 /* mark & release old frames */
1503 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1504 s->last_picture_ptr != s->next_picture_ptr &&
1505 s->last_picture_ptr->f->buf[0]) {
1506 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1509 s->current_picture_ptr->f->pict_type = s->pict_type;
1510 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1512 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1513 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1514 s->current_picture_ptr)) < 0)
1517 if (s->pict_type != AV_PICTURE_TYPE_B) {
1518 s->last_picture_ptr = s->next_picture_ptr;
1520 s->next_picture_ptr = s->current_picture_ptr;
1523 if (s->last_picture_ptr) {
1524 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1525 if (s->last_picture_ptr->f->buf[0] &&
1526 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1527 s->last_picture_ptr)) < 0)
1530 if (s->next_picture_ptr) {
1531 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1532 if (s->next_picture_ptr->f->buf[0] &&
1533 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1534 s->next_picture_ptr)) < 0)
1538 if (s->picture_structure!= PICT_FRAME) {
1540 for (i = 0; i < 4; i++) {
1541 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1542 s->current_picture.f->data[i] +=
1543 s->current_picture.f->linesize[i];
1545 s->current_picture.f->linesize[i] *= 2;
1546 s->last_picture.f->linesize[i] *= 2;
1547 s->next_picture.f->linesize[i] *= 2;
1551 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1552 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1553 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1554 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1555 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1556 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1558 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1559 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1562 if (s->dct_error_sum) {
1563 assert(s->avctx->noise_reduction && s->encoding);
1564 update_noise_reduction(s);
1570 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1571 const AVFrame *pic_arg, int *got_packet)
1573 MpegEncContext *s = avctx->priv_data;
1574 int i, stuffing_count, ret;
1575 int context_count = s->slice_context_count;
1577 s->picture_in_gop_number++;
1579 if (load_input_picture(s, pic_arg) < 0)
1582 if (select_input_picture(s) < 0) {
1587 if (s->new_picture.f->data[0]) {
1589 (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
1592 s->mb_info_ptr = av_packet_new_side_data(pkt,
1593 AV_PKT_DATA_H263_MB_INFO,
1594 s->mb_width*s->mb_height*12);
1595 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1598 for (i = 0; i < context_count; i++) {
1599 int start_y = s->thread_context[i]->start_mb_y;
1600 int end_y = s->thread_context[i]-> end_mb_y;
1601 int h = s->mb_height;
1602 uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1603 uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1605 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1608 s->pict_type = s->new_picture.f->pict_type;
1610 ret = frame_start(s);
1614 if (encode_picture(s, s->picture_number) < 0)
1617 avctx->header_bits = s->header_bits;
1618 avctx->mv_bits = s->mv_bits;
1619 avctx->misc_bits = s->misc_bits;
1620 avctx->i_tex_bits = s->i_tex_bits;
1621 avctx->p_tex_bits = s->p_tex_bits;
1622 avctx->i_count = s->i_count;
1623 // FIXME f/b_count in avctx
1624 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
1625 avctx->skip_count = s->skip_count;
1629 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1630 ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1632 if (avctx->rc_buffer_size) {
1633 RateControlContext *rcc = &s->rc_context;
1634 int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
1636 if (put_bits_count(&s->pb) > max_size &&
1637 s->lambda < s->lmax) {
1638 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1639 (s->qscale + 1) / s->qscale);
1640 if (s->adaptive_quant) {
1642 for (i = 0; i < s->mb_height * s->mb_stride; i++)
1643 s->lambda_table[i] =
1644 FFMAX(s->lambda_table[i] + 1,
1645 s->lambda_table[i] * (s->qscale + 1) /
1648 s->mb_skipped = 0; // done in frame_start()
1649 // done in encode_picture() so we must undo it
1650 if (s->pict_type == AV_PICTURE_TYPE_P) {
1651 if (s->flipflop_rounding ||
1652 s->codec_id == AV_CODEC_ID_H263P ||
1653 s->codec_id == AV_CODEC_ID_MPEG4)
1654 s->no_rounding ^= 1;
1656 if (s->pict_type != AV_PICTURE_TYPE_B) {
1657 s->time_base = s->last_time_base;
1658 s->last_non_b_time = s->time - s->pp_time;
1660 for (i = 0; i < context_count; i++) {
1661 PutBitContext *pb = &s->thread_context[i]->pb;
1662 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1667 assert(s->avctx->rc_max_rate);
1670 if (s->avctx->flags & CODEC_FLAG_PASS1)
1671 ff_write_pass1_stats(s);
1673 for (i = 0; i < 4; i++) {
1674 s->current_picture_ptr->f->error[i] = s->current_picture.f->error[i];
1675 avctx->error[i] += s->current_picture_ptr->f->error[i];
1678 if (s->avctx->flags & CODEC_FLAG_PASS1)
1679 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1680 avctx->i_tex_bits + avctx->p_tex_bits ==
1681 put_bits_count(&s->pb));
1682 flush_put_bits(&s->pb);
1683 s->frame_bits = put_bits_count(&s->pb);
1685 stuffing_count = ff_vbv_update(s, s->frame_bits);
1686 if (stuffing_count) {
1687 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1688 stuffing_count + 50) {
1689 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1693 switch (s->codec_id) {
1694 case AV_CODEC_ID_MPEG1VIDEO:
1695 case AV_CODEC_ID_MPEG2VIDEO:
1696 while (stuffing_count--) {
1697 put_bits(&s->pb, 8, 0);
1700 case AV_CODEC_ID_MPEG4:
1701 put_bits(&s->pb, 16, 0);
1702 put_bits(&s->pb, 16, 0x1C3);
1703 stuffing_count -= 4;
1704 while (stuffing_count--) {
1705 put_bits(&s->pb, 8, 0xFF);
1709 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1711 flush_put_bits(&s->pb);
1712 s->frame_bits = put_bits_count(&s->pb);
1715 /* update mpeg1/2 vbv_delay for CBR */
1716 if (s->avctx->rc_max_rate &&
1717 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1718 s->out_format == FMT_MPEG1 &&
1719 90000LL * (avctx->rc_buffer_size - 1) <=
1720 s->avctx->rc_max_rate * 0xFFFFLL) {
1721 int vbv_delay, min_delay;
1722 double inbits = s->avctx->rc_max_rate *
1723 av_q2d(s->avctx->time_base);
1724 int minbits = s->frame_bits - 8 *
1725 (s->vbv_delay_ptr - s->pb.buf - 1);
1726 double bits = s->rc_context.buffer_index + minbits - inbits;
1729 av_log(s->avctx, AV_LOG_ERROR,
1730 "Internal error, negative bits\n");
1732 assert(s->repeat_first_field == 0);
1734 vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1735 min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1736 s->avctx->rc_max_rate;
1738 vbv_delay = FFMAX(vbv_delay, min_delay);
1740 assert(vbv_delay < 0xFFFF);
1742 s->vbv_delay_ptr[0] &= 0xF8;
1743 s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1744 s->vbv_delay_ptr[1] = vbv_delay >> 5;
1745 s->vbv_delay_ptr[2] &= 0x07;
1746 s->vbv_delay_ptr[2] |= vbv_delay << 3;
1747 avctx->vbv_delay = vbv_delay * 300;
1749 s->total_bits += s->frame_bits;
1750 avctx->frame_bits = s->frame_bits;
1752 pkt->pts = s->current_picture.f->pts;
1753 if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1754 if (!s->current_picture.f->coded_picture_number)
1755 pkt->dts = pkt->pts - s->dts_delta;
1757 pkt->dts = s->reordered_pts;
1758 s->reordered_pts = pkt->pts;
1760 pkt->dts = pkt->pts;
1761 if (s->current_picture.f->key_frame)
1762 pkt->flags |= AV_PKT_FLAG_KEY;
1764 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1768 assert((s->frame_bits & 7) == 0);
1770 pkt->size = s->frame_bits / 8;
1771 *got_packet = !!pkt->size;
1775 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1776 int n, int threshold)
1778 static const char tab[64] = {
1779 3, 2, 2, 1, 1, 1, 1, 1,
1780 1, 1, 1, 1, 1, 1, 1, 1,
1781 1, 1, 1, 1, 1, 1, 1, 1,
1782 0, 0, 0, 0, 0, 0, 0, 0,
1783 0, 0, 0, 0, 0, 0, 0, 0,
1784 0, 0, 0, 0, 0, 0, 0, 0,
1785 0, 0, 0, 0, 0, 0, 0, 0,
1786 0, 0, 0, 0, 0, 0, 0, 0
1791 int16_t *block = s->block[n];
1792 const int last_index = s->block_last_index[n];
1795 if (threshold < 0) {
1797 threshold = -threshold;
1801 /* Are all we could set to zero already zero? */
1802 if (last_index <= skip_dc - 1)
1805 for (i = 0; i <= last_index; i++) {
1806 const int j = s->intra_scantable.permutated[i];
1807 const int level = FFABS(block[j]);
1809 if (skip_dc && i == 0)
1813 } else if (level > 1) {
1819 if (score >= threshold)
1821 for (i = skip_dc; i <= last_index; i++) {
1822 const int j = s->intra_scantable.permutated[i];
1826 s->block_last_index[n] = 0;
1828 s->block_last_index[n] = -1;
1831 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1835 const int maxlevel = s->max_qcoeff;
1836 const int minlevel = s->min_qcoeff;
1840 i = 1; // skip clipping of intra dc
1844 for (; i <= last_index; i++) {
1845 const int j = s->intra_scantable.permutated[i];
1846 int level = block[j];
1848 if (level > maxlevel) {
1851 } else if (level < minlevel) {
1859 if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1860 av_log(s->avctx, AV_LOG_INFO,
1861 "warning, clipping %d dct coefficients to %d..%d\n",
1862 overflow, minlevel, maxlevel);
1865 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1869 for (y = 0; y < 8; y++) {
1870 for (x = 0; x < 8; x++) {
1876 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1877 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1878 int v = ptr[x2 + y2 * stride];
1884 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
1889 static av_always_inline void encode_mb_internal(MpegEncContext *s,
1890 int motion_x, int motion_y,
1891 int mb_block_height,
1894 int16_t weight[8][64];
1895 int16_t orig[8][64];
1896 const int mb_x = s->mb_x;
1897 const int mb_y = s->mb_y;
1900 int dct_offset = s->linesize * 8; // default for progressive frames
1901 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1902 ptrdiff_t wrap_y, wrap_c;
1904 for (i = 0; i < mb_block_count; i++)
1905 skip_dct[i] = s->skipdct;
1907 if (s->adaptive_quant) {
1908 const int last_qp = s->qscale;
1909 const int mb_xy = mb_x + mb_y * s->mb_stride;
1911 s->lambda = s->lambda_table[mb_xy];
1914 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
1915 s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
1916 s->dquant = s->qscale - last_qp;
1918 if (s->out_format == FMT_H263) {
1919 s->dquant = av_clip(s->dquant, -2, 2);
1921 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1923 if (s->pict_type == AV_PICTURE_TYPE_B) {
1924 if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1927 if (s->mv_type == MV_TYPE_8X8)
1933 ff_set_qscale(s, last_qp + s->dquant);
1934 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
1935 ff_set_qscale(s, s->qscale + s->dquant);
1937 wrap_y = s->linesize;
1938 wrap_c = s->uvlinesize;
1939 ptr_y = s->new_picture.f->data[0] +
1940 (mb_y * 16 * wrap_y) + mb_x * 16;
1941 ptr_cb = s->new_picture.f->data[1] +
1942 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1943 ptr_cr = s->new_picture.f->data[2] +
1944 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1946 if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
1947 uint8_t *ebuf = s->sc.edge_emu_buffer + 32;
1948 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
1950 16, 16, mb_x * 16, mb_y * 16,
1951 s->width, s->height);
1953 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
1955 8, mb_block_height, mb_x * 8, mb_y * 8,
1956 s->width >> 1, s->height >> 1);
1957 ptr_cb = ebuf + 18 * wrap_y;
1958 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr,
1960 8, mb_block_height, mb_x * 8, mb_y * 8,
1961 s->width >> 1, s->height >> 1);
1962 ptr_cr = ebuf + 18 * wrap_y + 8;
1966 if (s->avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
1967 int progressive_score, interlaced_score;
1969 s->interlaced_dct = 0;
1970 progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
1971 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1972 NULL, wrap_y, 8) - 400;
1974 if (progressive_score > 0) {
1975 interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
1976 NULL, wrap_y * 2, 8) +
1977 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
1978 NULL, wrap_y * 2, 8);
1979 if (progressive_score > interlaced_score) {
1980 s->interlaced_dct = 1;
1982 dct_offset = wrap_y;
1984 if (s->chroma_format == CHROMA_422)
1990 s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
1991 s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
1992 s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
1993 s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
1995 if (s->avctx->flags & CODEC_FLAG_GRAY) {
1999 s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2000 s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2001 if (!s->chroma_y_shift) { /* 422 */
2002 s->pdsp.get_pixels(s->block[6],
2003 ptr_cb + (dct_offset >> 1), wrap_c);
2004 s->pdsp.get_pixels(s->block[7],
2005 ptr_cr + (dct_offset >> 1), wrap_c);
2009 op_pixels_func (*op_pix)[4];
2010 qpel_mc_func (*op_qpix)[16];
2011 uint8_t *dest_y, *dest_cb, *dest_cr;
2013 dest_y = s->dest[0];
2014 dest_cb = s->dest[1];
2015 dest_cr = s->dest[2];
2017 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
2018 op_pix = s->hdsp.put_pixels_tab;
2019 op_qpix = s->qdsp.put_qpel_pixels_tab;
2021 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2022 op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
2025 if (s->mv_dir & MV_DIR_FORWARD) {
2026 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
2027 s->last_picture.f->data,
2029 op_pix = s->hdsp.avg_pixels_tab;
2030 op_qpix = s->qdsp.avg_qpel_pixels_tab;
2032 if (s->mv_dir & MV_DIR_BACKWARD) {
2033 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
2034 s->next_picture.f->data,
2038 if (s->avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
2039 int progressive_score, interlaced_score;
2041 s->interlaced_dct = 0;
2042 progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2043 s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
2047 if (s->avctx->ildct_cmp == FF_CMP_VSSE)
2048 progressive_score -= 400;
2050 if (progressive_score > 0) {
2051 interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
2053 s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
2057 if (progressive_score > interlaced_score) {
2058 s->interlaced_dct = 1;
2060 dct_offset = wrap_y;
2062 if (s->chroma_format == CHROMA_422)
2068 s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2069 s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2070 s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2071 dest_y + dct_offset, wrap_y);
2072 s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2073 dest_y + dct_offset + 8, wrap_y);
2075 if (s->avctx->flags & CODEC_FLAG_GRAY) {
2079 s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2080 s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2081 if (!s->chroma_y_shift) { /* 422 */
2082 s->pdsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
2083 dest_cb + (dct_offset >> 1), wrap_c);
2084 s->pdsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
2085 dest_cr + (dct_offset >> 1), wrap_c);
2088 /* pre quantization */
2089 if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2090 2 * s->qscale * s->qscale) {
2092 if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2094 if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2096 if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2097 wrap_y, 8) < 20 * s->qscale)
2099 if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2100 wrap_y, 8) < 20 * s->qscale)
2102 if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2104 if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2106 if (!s->chroma_y_shift) { /* 422 */
2107 if (s->mecc.sad[1](NULL, ptr_cb + (dct_offset >> 1),
2108 dest_cb + (dct_offset >> 1),
2109 wrap_c, 8) < 20 * s->qscale)
2111 if (s->mecc.sad[1](NULL, ptr_cr + (dct_offset >> 1),
2112 dest_cr + (dct_offset >> 1),
2113 wrap_c, 8) < 20 * s->qscale)
2119 if (s->quantizer_noise_shaping) {
2121 get_visual_weight(weight[0], ptr_y , wrap_y);
2123 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2125 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2127 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2129 get_visual_weight(weight[4], ptr_cb , wrap_c);
2131 get_visual_weight(weight[5], ptr_cr , wrap_c);
2132 if (!s->chroma_y_shift) { /* 422 */
2134 get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
2137 get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
2140 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2143 /* DCT & quantize */
2144 assert(s->out_format != FMT_MJPEG || s->qscale == 8);
2146 for (i = 0; i < mb_block_count; i++) {
2149 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2150 // FIXME we could decide to change to quantizer instead of
2152 // JS: I don't think that would be a good idea it could lower
2153 // quality instead of improve it. Just INTRADC clipping
2154 // deserves changes in quantizer
2156 clip_coeffs(s, s->block[i], s->block_last_index[i]);
2158 s->block_last_index[i] = -1;
2160 if (s->quantizer_noise_shaping) {
2161 for (i = 0; i < mb_block_count; i++) {
2163 s->block_last_index[i] =
2164 dct_quantize_refine(s, s->block[i], weight[i],
2165 orig[i], i, s->qscale);
2170 if (s->luma_elim_threshold && !s->mb_intra)
2171 for (i = 0; i < 4; i++)
2172 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2173 if (s->chroma_elim_threshold && !s->mb_intra)
2174 for (i = 4; i < mb_block_count; i++)
2175 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2177 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2178 for (i = 0; i < mb_block_count; i++) {
2179 if (s->block_last_index[i] == -1)
2180 s->coded_score[i] = INT_MAX / 256;
2185 if ((s->avctx->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2186 s->block_last_index[4] =
2187 s->block_last_index[5] = 0;
2189 s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2192 // non c quantize code returns incorrect block_last_index FIXME
2193 if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2194 for (i = 0; i < mb_block_count; i++) {
2196 if (s->block_last_index[i] > 0) {
2197 for (j = 63; j > 0; j--) {
2198 if (s->block[i][s->intra_scantable.permutated[j]])
2201 s->block_last_index[i] = j;
2206 /* huffman encode */
2207 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2208 case AV_CODEC_ID_MPEG1VIDEO:
2209 case AV_CODEC_ID_MPEG2VIDEO:
2210 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2211 ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2213 case AV_CODEC_ID_MPEG4:
2214 if (CONFIG_MPEG4_ENCODER)
2215 ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2217 case AV_CODEC_ID_MSMPEG4V2:
2218 case AV_CODEC_ID_MSMPEG4V3:
2219 case AV_CODEC_ID_WMV1:
2220 if (CONFIG_MSMPEG4_ENCODER)
2221 ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2223 case AV_CODEC_ID_WMV2:
2224 if (CONFIG_WMV2_ENCODER)
2225 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2227 case AV_CODEC_ID_H261:
2228 if (CONFIG_H261_ENCODER)
2229 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2231 case AV_CODEC_ID_H263:
2232 case AV_CODEC_ID_H263P:
2233 case AV_CODEC_ID_FLV1:
2234 case AV_CODEC_ID_RV10:
2235 case AV_CODEC_ID_RV20:
2236 if (CONFIG_H263_ENCODER)
2237 ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2239 case AV_CODEC_ID_MJPEG:
2240 if (CONFIG_MJPEG_ENCODER)
2241 ff_mjpeg_encode_mb(s, s->block);
2248 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2250 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
2251 else encode_mb_internal(s, motion_x, motion_y, 16, 8);
2254 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2257 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2260 d->mb_skip_run= s->mb_skip_run;
2262 d->last_dc[i] = s->last_dc[i];
2265 d->mv_bits= s->mv_bits;
2266 d->i_tex_bits= s->i_tex_bits;
2267 d->p_tex_bits= s->p_tex_bits;
2268 d->i_count= s->i_count;
2269 d->f_count= s->f_count;
2270 d->b_count= s->b_count;
2271 d->skip_count= s->skip_count;
2272 d->misc_bits= s->misc_bits;
2276 d->qscale= s->qscale;
2277 d->dquant= s->dquant;
2279 d->esc3_level_length= s->esc3_level_length;
2282 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2285 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2286 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2289 d->mb_skip_run= s->mb_skip_run;
2291 d->last_dc[i] = s->last_dc[i];
2294 d->mv_bits= s->mv_bits;
2295 d->i_tex_bits= s->i_tex_bits;
2296 d->p_tex_bits= s->p_tex_bits;
2297 d->i_count= s->i_count;
2298 d->f_count= s->f_count;
2299 d->b_count= s->b_count;
2300 d->skip_count= s->skip_count;
2301 d->misc_bits= s->misc_bits;
2303 d->mb_intra= s->mb_intra;
2304 d->mb_skipped= s->mb_skipped;
2305 d->mv_type= s->mv_type;
2306 d->mv_dir= s->mv_dir;
2308 if(s->data_partitioning){
2310 d->tex_pb= s->tex_pb;
2314 d->block_last_index[i]= s->block_last_index[i];
2315 d->interlaced_dct= s->interlaced_dct;
2316 d->qscale= s->qscale;
2318 d->esc3_level_length= s->esc3_level_length;
2321 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2322 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2323 int *dmin, int *next_block, int motion_x, int motion_y)
2326 uint8_t *dest_backup[3];
2328 copy_context_before_encode(s, backup, type);
2330 s->block= s->blocks[*next_block];
2331 s->pb= pb[*next_block];
2332 if(s->data_partitioning){
2333 s->pb2 = pb2 [*next_block];
2334 s->tex_pb= tex_pb[*next_block];
2338 memcpy(dest_backup, s->dest, sizeof(s->dest));
2339 s->dest[0] = s->sc.rd_scratchpad;
2340 s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
2341 s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
2342 assert(s->linesize >= 32); //FIXME
2345 encode_mb(s, motion_x, motion_y);
2347 score= put_bits_count(&s->pb);
2348 if(s->data_partitioning){
2349 score+= put_bits_count(&s->pb2);
2350 score+= put_bits_count(&s->tex_pb);
2353 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2354 ff_mpv_decode_mb(s, s->block);
2356 score *= s->lambda2;
2357 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2361 memcpy(s->dest, dest_backup, sizeof(s->dest));
2368 copy_context_after_encode(best, s, type);
2372 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2373 uint32_t *sq = ff_square_tab + 256;
2378 return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2379 else if(w==8 && h==8)
2380 return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2384 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2393 static int sse_mb(MpegEncContext *s){
2397 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2398 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2401 if(s->avctx->mb_cmp == FF_CMP_NSSE){
2402 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) +
2403 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) +
2404 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);
2406 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) +
2407 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) +
2408 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);
2411 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)
2412 +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)
2413 +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);
2416 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2417 MpegEncContext *s= *(void**)arg;
2421 s->me.dia_size= s->avctx->pre_dia_size;
2422 s->first_slice_line=1;
2423 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2424 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2425 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2427 s->first_slice_line=0;
2435 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2436 MpegEncContext *s= *(void**)arg;
2438 s->me.dia_size= s->avctx->dia_size;
2439 s->first_slice_line=1;
2440 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2441 s->mb_x=0; //for block init below
2442 ff_init_block_index(s);
2443 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2444 s->block_index[0]+=2;
2445 s->block_index[1]+=2;
2446 s->block_index[2]+=2;
2447 s->block_index[3]+=2;
2449 /* compute motion vector & mb_type and store in context */
2450 if(s->pict_type==AV_PICTURE_TYPE_B)
2451 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2453 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2455 s->first_slice_line=0;
2460 static int mb_var_thread(AVCodecContext *c, void *arg){
2461 MpegEncContext *s= *(void**)arg;
2464 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2465 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2468 uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2470 int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2472 varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
2473 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2475 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2476 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2477 s->me.mb_var_sum_temp += varc;
2483 static void write_slice_end(MpegEncContext *s){
2484 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2485 if(s->partitioned_frame){
2486 ff_mpeg4_merge_partitions(s);
2489 ff_mpeg4_stuffing(&s->pb);
2490 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2491 ff_mjpeg_encode_stuffing(&s->pb);
2494 avpriv_align_put_bits(&s->pb);
2495 flush_put_bits(&s->pb);
2497 if ((s->avctx->flags & CODEC_FLAG_PASS1) && !s->partitioned_frame)
2498 s->misc_bits+= get_bits_diff(s);
2501 static void write_mb_info(MpegEncContext *s)
2503 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2504 int offset = put_bits_count(&s->pb);
2505 int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2506 int gobn = s->mb_y / s->gob_index;
2508 if (CONFIG_H263_ENCODER)
2509 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2510 bytestream_put_le32(&ptr, offset);
2511 bytestream_put_byte(&ptr, s->qscale);
2512 bytestream_put_byte(&ptr, gobn);
2513 bytestream_put_le16(&ptr, mba);
2514 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2515 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2516 /* 4MV not implemented */
2517 bytestream_put_byte(&ptr, 0); /* hmv2 */
2518 bytestream_put_byte(&ptr, 0); /* vmv2 */
2521 static void update_mb_info(MpegEncContext *s, int startcode)
2525 if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2526 s->mb_info_size += 12;
2527 s->prev_mb_info = s->last_mb_info;
2530 s->prev_mb_info = put_bits_count(&s->pb)/8;
2531 /* This might have incremented mb_info_size above, and we return without
2532 * actually writing any info into that slot yet. But in that case,
2533 * this will be called again at the start of the after writing the
2534 * start code, actually writing the mb info. */
2538 s->last_mb_info = put_bits_count(&s->pb)/8;
2539 if (!s->mb_info_size)
2540 s->mb_info_size += 12;
2544 static int encode_thread(AVCodecContext *c, void *arg){
2545 MpegEncContext *s= *(void**)arg;
2546 int mb_x, mb_y, pdif = 0;
2547 int chr_h= 16>>s->chroma_y_shift;
2549 MpegEncContext best_s = { 0 }, backup_s;
2550 uint8_t bit_buf[2][MAX_MB_BYTES];
2551 uint8_t bit_buf2[2][MAX_MB_BYTES];
2552 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2553 PutBitContext pb[2], pb2[2], tex_pb[2];
2556 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2557 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2558 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2561 s->last_bits= put_bits_count(&s->pb);
2572 /* init last dc values */
2573 /* note: quant matrix value (8) is implied here */
2574 s->last_dc[i] = 128 << s->intra_dc_precision;
2576 s->current_picture.f->error[i] = 0;
2579 memset(s->last_mv, 0, sizeof(s->last_mv));
2583 switch(s->codec_id){
2584 case AV_CODEC_ID_H263:
2585 case AV_CODEC_ID_H263P:
2586 case AV_CODEC_ID_FLV1:
2587 if (CONFIG_H263_ENCODER)
2588 s->gob_index = H263_GOB_HEIGHT(s->height);
2590 case AV_CODEC_ID_MPEG4:
2591 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2592 ff_mpeg4_init_partitions(s);
2598 s->first_slice_line = 1;
2599 s->ptr_lastgob = s->pb.buf;
2600 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2604 ff_set_qscale(s, s->qscale);
2605 ff_init_block_index(s);
2607 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2608 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2609 int mb_type= s->mb_type[xy];
2614 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2615 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2618 if(s->data_partitioning){
2619 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2620 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2621 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2627 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2628 ff_update_block_index(s);
2630 if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2631 ff_h261_reorder_mb_index(s);
2632 xy= s->mb_y*s->mb_stride + s->mb_x;
2633 mb_type= s->mb_type[xy];
2636 /* write gob / video packet header */
2638 int current_packet_size, is_gob_start;
2640 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2642 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2644 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2646 switch(s->codec_id){
2647 case AV_CODEC_ID_H263:
2648 case AV_CODEC_ID_H263P:
2649 if(!s->h263_slice_structured)
2650 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2652 case AV_CODEC_ID_MPEG2VIDEO:
2653 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2654 case AV_CODEC_ID_MPEG1VIDEO:
2655 if(s->mb_skip_run) is_gob_start=0;
2660 if(s->start_mb_y != mb_y || mb_x!=0){
2663 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2664 ff_mpeg4_init_partitions(s);
2668 assert((put_bits_count(&s->pb)&7) == 0);
2669 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2671 if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2672 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2673 int d = 100 / s->error_rate;
2675 current_packet_size=0;
2676 s->pb.buf_ptr= s->ptr_lastgob;
2677 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2681 if (s->avctx->rtp_callback){
2682 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2683 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2685 update_mb_info(s, 1);
2687 switch(s->codec_id){
2688 case AV_CODEC_ID_MPEG4:
2689 if (CONFIG_MPEG4_ENCODER) {
2690 ff_mpeg4_encode_video_packet_header(s);
2691 ff_mpeg4_clean_buffers(s);
2694 case AV_CODEC_ID_MPEG1VIDEO:
2695 case AV_CODEC_ID_MPEG2VIDEO:
2696 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2697 ff_mpeg1_encode_slice_header(s);
2698 ff_mpeg1_clean_buffers(s);
2701 case AV_CODEC_ID_H263:
2702 case AV_CODEC_ID_H263P:
2703 if (CONFIG_H263_ENCODER)
2704 ff_h263_encode_gob_header(s, mb_y);
2708 if (s->avctx->flags & CODEC_FLAG_PASS1) {
2709 int bits= put_bits_count(&s->pb);
2710 s->misc_bits+= bits - s->last_bits;
2714 s->ptr_lastgob += current_packet_size;
2715 s->first_slice_line=1;
2716 s->resync_mb_x=mb_x;
2717 s->resync_mb_y=mb_y;
2721 if( (s->resync_mb_x == s->mb_x)
2722 && s->resync_mb_y+1 == s->mb_y){
2723 s->first_slice_line=0;
2727 s->dquant=0; //only for QP_RD
2729 update_mb_info(s, 0);
2731 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
2733 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2735 copy_context_before_encode(&backup_s, s, -1);
2737 best_s.data_partitioning= s->data_partitioning;
2738 best_s.partitioned_frame= s->partitioned_frame;
2739 if(s->data_partitioning){
2740 backup_s.pb2= s->pb2;
2741 backup_s.tex_pb= s->tex_pb;
2744 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2745 s->mv_dir = MV_DIR_FORWARD;
2746 s->mv_type = MV_TYPE_16X16;
2748 s->mv[0][0][0] = s->p_mv_table[xy][0];
2749 s->mv[0][0][1] = s->p_mv_table[xy][1];
2750 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, 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_INTER_I){
2754 s->mv_dir = MV_DIR_FORWARD;
2755 s->mv_type = MV_TYPE_FIELD;
2758 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2759 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2760 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2762 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2763 &dmin, &next_block, 0, 0);
2765 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2766 s->mv_dir = MV_DIR_FORWARD;
2767 s->mv_type = MV_TYPE_16X16;
2771 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2772 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2774 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2775 s->mv_dir = MV_DIR_FORWARD;
2776 s->mv_type = MV_TYPE_8X8;
2779 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2780 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2782 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2783 &dmin, &next_block, 0, 0);
2785 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2786 s->mv_dir = MV_DIR_FORWARD;
2787 s->mv_type = MV_TYPE_16X16;
2789 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2790 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2791 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2792 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2794 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2795 s->mv_dir = MV_DIR_BACKWARD;
2796 s->mv_type = MV_TYPE_16X16;
2798 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2799 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2800 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2801 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2803 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2804 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2805 s->mv_type = MV_TYPE_16X16;
2807 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2808 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2809 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2810 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2811 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2812 &dmin, &next_block, 0, 0);
2814 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2815 s->mv_dir = MV_DIR_FORWARD;
2816 s->mv_type = MV_TYPE_FIELD;
2819 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2820 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2821 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2823 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2824 &dmin, &next_block, 0, 0);
2826 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2827 s->mv_dir = MV_DIR_BACKWARD;
2828 s->mv_type = MV_TYPE_FIELD;
2831 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2832 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2833 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2835 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2836 &dmin, &next_block, 0, 0);
2838 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2839 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2840 s->mv_type = MV_TYPE_FIELD;
2842 for(dir=0; dir<2; dir++){
2844 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2845 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2846 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2849 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2850 &dmin, &next_block, 0, 0);
2852 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2854 s->mv_type = MV_TYPE_16X16;
2858 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2859 &dmin, &next_block, 0, 0);
2860 if(s->h263_pred || s->h263_aic){
2862 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2864 ff_clean_intra_table_entries(s); //old mode?
2868 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2869 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2870 const int last_qp= backup_s.qscale;
2873 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2874 static const int dquant_tab[4]={-1,1,-2,2};
2876 assert(backup_s.dquant == 0);
2879 s->mv_dir= best_s.mv_dir;
2880 s->mv_type = MV_TYPE_16X16;
2881 s->mb_intra= best_s.mb_intra;
2882 s->mv[0][0][0] = best_s.mv[0][0][0];
2883 s->mv[0][0][1] = best_s.mv[0][0][1];
2884 s->mv[1][0][0] = best_s.mv[1][0][0];
2885 s->mv[1][0][1] = best_s.mv[1][0][1];
2887 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
2888 for(; qpi<4; qpi++){
2889 int dquant= dquant_tab[qpi];
2890 qp= last_qp + dquant;
2891 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2893 backup_s.dquant= dquant;
2894 if(s->mb_intra && s->dc_val[0]){
2896 dc[i]= s->dc_val[0][ s->block_index[i] ];
2897 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
2901 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2902 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2903 if(best_s.qscale != qp){
2904 if(s->mb_intra && s->dc_val[0]){
2906 s->dc_val[0][ s->block_index[i] ]= dc[i];
2907 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
2914 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2915 int mx= s->b_direct_mv_table[xy][0];
2916 int my= s->b_direct_mv_table[xy][1];
2918 backup_s.dquant = 0;
2919 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2921 ff_mpeg4_set_direct_mv(s, mx, my);
2922 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2923 &dmin, &next_block, mx, my);
2925 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2926 backup_s.dquant = 0;
2927 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2929 ff_mpeg4_set_direct_mv(s, 0, 0);
2930 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2931 &dmin, &next_block, 0, 0);
2933 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
2936 coded |= s->block_last_index[i];
2939 memcpy(s->mv, best_s.mv, sizeof(s->mv));
2940 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2941 mx=my=0; //FIXME find the one we actually used
2942 ff_mpeg4_set_direct_mv(s, mx, my);
2943 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2951 s->mv_dir= best_s.mv_dir;
2952 s->mv_type = best_s.mv_type;
2954 /* s->mv[0][0][0] = best_s.mv[0][0][0];
2955 s->mv[0][0][1] = best_s.mv[0][0][1];
2956 s->mv[1][0][0] = best_s.mv[1][0][0];
2957 s->mv[1][0][1] = best_s.mv[1][0][1];*/
2960 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2961 &dmin, &next_block, mx, my);
2966 s->current_picture.qscale_table[xy] = best_s.qscale;
2968 copy_context_after_encode(s, &best_s, -1);
2970 pb_bits_count= put_bits_count(&s->pb);
2971 flush_put_bits(&s->pb);
2972 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2975 if(s->data_partitioning){
2976 pb2_bits_count= put_bits_count(&s->pb2);
2977 flush_put_bits(&s->pb2);
2978 avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2979 s->pb2= backup_s.pb2;
2981 tex_pb_bits_count= put_bits_count(&s->tex_pb);
2982 flush_put_bits(&s->tex_pb);
2983 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2984 s->tex_pb= backup_s.tex_pb;
2986 s->last_bits= put_bits_count(&s->pb);
2988 if (CONFIG_H263_ENCODER &&
2989 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2990 ff_h263_update_motion_val(s);
2992 if(next_block==0){ //FIXME 16 vs linesize16
2993 s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
2994 s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
2995 s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2998 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2999 ff_mpv_decode_mb(s, s->block);
3001 int motion_x = 0, motion_y = 0;
3002 s->mv_type=MV_TYPE_16X16;
3003 // only one MB-Type possible
3006 case CANDIDATE_MB_TYPE_INTRA:
3009 motion_x= s->mv[0][0][0] = 0;
3010 motion_y= s->mv[0][0][1] = 0;
3012 case CANDIDATE_MB_TYPE_INTER:
3013 s->mv_dir = MV_DIR_FORWARD;
3015 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3016 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3018 case CANDIDATE_MB_TYPE_INTER_I:
3019 s->mv_dir = MV_DIR_FORWARD;
3020 s->mv_type = MV_TYPE_FIELD;
3023 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3024 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3025 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3028 case CANDIDATE_MB_TYPE_INTER4V:
3029 s->mv_dir = MV_DIR_FORWARD;
3030 s->mv_type = MV_TYPE_8X8;
3033 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3034 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3037 case CANDIDATE_MB_TYPE_DIRECT:
3038 if (CONFIG_MPEG4_ENCODER) {
3039 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3041 motion_x=s->b_direct_mv_table[xy][0];
3042 motion_y=s->b_direct_mv_table[xy][1];
3043 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3046 case CANDIDATE_MB_TYPE_DIRECT0:
3047 if (CONFIG_MPEG4_ENCODER) {
3048 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3050 ff_mpeg4_set_direct_mv(s, 0, 0);
3053 case CANDIDATE_MB_TYPE_BIDIR:
3054 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3056 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3057 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3058 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3059 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3061 case CANDIDATE_MB_TYPE_BACKWARD:
3062 s->mv_dir = MV_DIR_BACKWARD;
3064 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3065 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3067 case CANDIDATE_MB_TYPE_FORWARD:
3068 s->mv_dir = MV_DIR_FORWARD;
3070 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3071 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3073 case CANDIDATE_MB_TYPE_FORWARD_I:
3074 s->mv_dir = MV_DIR_FORWARD;
3075 s->mv_type = MV_TYPE_FIELD;
3078 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3079 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3080 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3083 case CANDIDATE_MB_TYPE_BACKWARD_I:
3084 s->mv_dir = MV_DIR_BACKWARD;
3085 s->mv_type = MV_TYPE_FIELD;
3088 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3089 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3090 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3093 case CANDIDATE_MB_TYPE_BIDIR_I:
3094 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3095 s->mv_type = MV_TYPE_FIELD;
3097 for(dir=0; dir<2; dir++){
3099 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3100 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3101 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3106 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3109 encode_mb(s, motion_x, motion_y);
3111 // RAL: Update last macroblock type
3112 s->last_mv_dir = s->mv_dir;
3114 if (CONFIG_H263_ENCODER &&
3115 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3116 ff_h263_update_motion_val(s);
3118 ff_mpv_decode_mb(s, s->block);
3121 /* clean the MV table in IPS frames for direct mode in B frames */
3122 if(s->mb_intra /* && I,P,S_TYPE */){
3123 s->p_mv_table[xy][0]=0;
3124 s->p_mv_table[xy][1]=0;
3127 if (s->avctx->flags & CODEC_FLAG_PSNR) {
3131 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3132 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3134 s->current_picture.f->error[0] += sse(
3135 s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3136 s->dest[0], w, h, s->linesize);
3137 s->current_picture.f->error[1] += sse(
3138 s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3139 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3140 s->current_picture.f->error[2] += sse(
3141 s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3142 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3145 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3146 ff_h263_loop_filter(s);
3148 ff_dlog(s->avctx, "MB %d %d bits\n",
3149 s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3153 //not beautiful here but we must write it before flushing so it has to be here
3154 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3155 ff_msmpeg4_encode_ext_header(s);
3159 /* Send the last GOB if RTP */
3160 if (s->avctx->rtp_callback) {
3161 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3162 pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3163 /* Call the RTP callback to send the last GOB */
3165 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3171 #define MERGE(field) dst->field += src->field; src->field=0
3172 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3173 MERGE(me.scene_change_score);
3174 MERGE(me.mc_mb_var_sum_temp);
3175 MERGE(me.mb_var_sum_temp);
3178 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3181 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3182 MERGE(dct_count[1]);
3191 MERGE(er.error_count);
3192 MERGE(padding_bug_score);
3193 MERGE(current_picture.f->error[0]);
3194 MERGE(current_picture.f->error[1]);
3195 MERGE(current_picture.f->error[2]);
3197 if(dst->avctx->noise_reduction){
3198 for(i=0; i<64; i++){
3199 MERGE(dct_error_sum[0][i]);
3200 MERGE(dct_error_sum[1][i]);
3204 assert(put_bits_count(&src->pb) % 8 ==0);
3205 assert(put_bits_count(&dst->pb) % 8 ==0);
3206 avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3207 flush_put_bits(&dst->pb);
3210 static int estimate_qp(MpegEncContext *s, int dry_run){
3211 if (s->next_lambda){
3212 s->current_picture_ptr->f->quality =
3213 s->current_picture.f->quality = s->next_lambda;
3214 if(!dry_run) s->next_lambda= 0;
3215 } else if (!s->fixed_qscale) {
3216 s->current_picture_ptr->f->quality =
3217 s->current_picture.f->quality = ff_rate_estimate_qscale(s, dry_run);
3218 if (s->current_picture.f->quality < 0)
3222 if(s->adaptive_quant){
3223 switch(s->codec_id){
3224 case AV_CODEC_ID_MPEG4:
3225 if (CONFIG_MPEG4_ENCODER)
3226 ff_clean_mpeg4_qscales(s);
3228 case AV_CODEC_ID_H263:
3229 case AV_CODEC_ID_H263P:
3230 case AV_CODEC_ID_FLV1:
3231 if (CONFIG_H263_ENCODER)
3232 ff_clean_h263_qscales(s);
3235 ff_init_qscale_tab(s);
3238 s->lambda= s->lambda_table[0];
3241 s->lambda = s->current_picture.f->quality;
3246 /* must be called before writing the header */
3247 static void set_frame_distances(MpegEncContext * s){
3248 assert(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
3249 s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3251 if(s->pict_type==AV_PICTURE_TYPE_B){
3252 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3253 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3255 s->pp_time= s->time - s->last_non_b_time;
3256 s->last_non_b_time= s->time;
3257 assert(s->picture_number==0 || s->pp_time > 0);
3261 static int encode_picture(MpegEncContext *s, int picture_number)
3265 int context_count = s->slice_context_count;
3267 s->picture_number = picture_number;
3269 /* Reset the average MB variance */
3270 s->me.mb_var_sum_temp =
3271 s->me.mc_mb_var_sum_temp = 0;
3273 /* we need to initialize some time vars before we can encode b-frames */
3274 // RAL: Condition added for MPEG1VIDEO
3275 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3276 set_frame_distances(s);
3277 if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3278 ff_set_mpeg4_time(s);
3280 s->me.scene_change_score=0;
3282 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3284 if(s->pict_type==AV_PICTURE_TYPE_I){
3285 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3286 else s->no_rounding=0;
3287 }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3288 if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3289 s->no_rounding ^= 1;
3292 if (s->avctx->flags & CODEC_FLAG_PASS2) {
3293 if (estimate_qp(s,1) < 0)
3295 ff_get_2pass_fcode(s);
3296 } else if (!(s->avctx->flags & CODEC_FLAG_QSCALE)) {
3297 if(s->pict_type==AV_PICTURE_TYPE_B)
3298 s->lambda= s->last_lambda_for[s->pict_type];
3300 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3304 s->mb_intra=0; //for the rate distortion & bit compare functions
3305 for(i=1; i<context_count; i++){
3306 ret = ff_update_duplicate_context(s->thread_context[i], s);
3314 /* Estimate motion for every MB */
3315 if(s->pict_type != AV_PICTURE_TYPE_I){
3316 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3317 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
3318 if (s->pict_type != AV_PICTURE_TYPE_B) {
3319 if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
3320 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3324 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3325 }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3327 for(i=0; i<s->mb_stride*s->mb_height; i++)
3328 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3330 if(!s->fixed_qscale){
3331 /* finding spatial complexity for I-frame rate control */
3332 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3335 for(i=1; i<context_count; i++){
3336 merge_context_after_me(s, s->thread_context[i]);
3338 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3339 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
3342 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
3343 s->pict_type= AV_PICTURE_TYPE_I;
3344 for(i=0; i<s->mb_stride*s->mb_height; i++)
3345 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3346 ff_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
3347 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3351 if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3352 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3354 if (s->avctx->flags & CODEC_FLAG_INTERLACED_ME) {
3356 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3357 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3358 s->f_code= FFMAX3(s->f_code, a, b);
3361 ff_fix_long_p_mvs(s);
3362 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3363 if (s->avctx->flags & CODEC_FLAG_INTERLACED_ME) {
3367 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3368 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3373 if(s->pict_type==AV_PICTURE_TYPE_B){
3376 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3377 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3378 s->f_code = FFMAX(a, b);
3380 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3381 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3382 s->b_code = FFMAX(a, b);
3384 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3385 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3386 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3387 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3388 if (s->avctx->flags & CODEC_FLAG_INTERLACED_ME) {
3390 for(dir=0; dir<2; dir++){
3393 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3394 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3395 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3396 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3404 if (estimate_qp(s, 0) < 0)
3407 if (s->qscale < 3 && s->max_qcoeff <= 128 &&
3408 s->pict_type == AV_PICTURE_TYPE_I &&
3409 !(s->avctx->flags & CODEC_FLAG_QSCALE))
3410 s->qscale= 3; //reduce clipping problems
3412 if (s->out_format == FMT_MJPEG) {
3413 /* for mjpeg, we do include qscale in the matrix */
3415 int j = s->idsp.idct_permutation[i];
3417 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3419 s->y_dc_scale_table=
3420 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3421 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3422 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3423 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3427 //FIXME var duplication
3428 s->current_picture_ptr->f->key_frame =
3429 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3430 s->current_picture_ptr->f->pict_type =
3431 s->current_picture.f->pict_type = s->pict_type;
3433 if (s->current_picture.f->key_frame)
3434 s->picture_in_gop_number=0;
3436 s->last_bits= put_bits_count(&s->pb);
3437 switch(s->out_format) {
3439 if (CONFIG_MJPEG_ENCODER)
3440 ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
3444 if (CONFIG_H261_ENCODER)
3445 ff_h261_encode_picture_header(s, picture_number);
3448 if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3449 ff_wmv2_encode_picture_header(s, picture_number);
3450 else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3451 ff_msmpeg4_encode_picture_header(s, picture_number);
3452 else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3453 ff_mpeg4_encode_picture_header(s, picture_number);
3454 else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
3455 ret = ff_rv10_encode_picture_header(s, picture_number);
3459 else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3460 ff_rv20_encode_picture_header(s, picture_number);
3461 else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3462 ff_flv_encode_picture_header(s, picture_number);
3463 else if (CONFIG_H263_ENCODER)
3464 ff_h263_encode_picture_header(s, picture_number);
3467 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3468 ff_mpeg1_encode_picture_header(s, picture_number);
3473 bits= put_bits_count(&s->pb);
3474 s->header_bits= bits - s->last_bits;
3476 for(i=1; i<context_count; i++){
3477 update_duplicate_context_after_me(s->thread_context[i], s);
3479 s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3480 for(i=1; i<context_count; i++){
3481 merge_context_after_encode(s, s->thread_context[i]);
3487 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3488 const int intra= s->mb_intra;
3491 s->dct_count[intra]++;
3493 for(i=0; i<64; i++){
3494 int level= block[i];
3498 s->dct_error_sum[intra][i] += level;
3499 level -= s->dct_offset[intra][i];
3500 if(level<0) level=0;
3502 s->dct_error_sum[intra][i] -= level;
3503 level += s->dct_offset[intra][i];
3504 if(level>0) level=0;
3511 static int dct_quantize_trellis_c(MpegEncContext *s,
3512 int16_t *block, int n,
3513 int qscale, int *overflow){
3515 const uint8_t *scantable= s->intra_scantable.scantable;
3516 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3518 unsigned int threshold1, threshold2;
3530 int coeff_count[64];
3531 int qmul, qadd, start_i, last_non_zero, i, dc;
3532 const int esc_length= s->ac_esc_length;
3534 uint8_t * last_length;
3535 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3537 s->fdsp.fdct(block);
3539 if(s->dct_error_sum)
3540 s->denoise_dct(s, block);
3542 qadd= ((qscale-1)|1)*8;
3553 /* For AIC we skip quant/dequant of INTRADC */
3558 /* note: block[0] is assumed to be positive */
3559 block[0] = (block[0] + (q >> 1)) / q;
3562 qmat = s->q_intra_matrix[qscale];
3563 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3564 bias= 1<<(QMAT_SHIFT-1);
3565 length = s->intra_ac_vlc_length;
3566 last_length= s->intra_ac_vlc_last_length;
3570 qmat = s->q_inter_matrix[qscale];
3571 length = s->inter_ac_vlc_length;
3572 last_length= s->inter_ac_vlc_last_length;
3576 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3577 threshold2= (threshold1<<1);
3579 for(i=63; i>=start_i; i--) {
3580 const int j = scantable[i];
3581 int level = block[j] * qmat[j];
3583 if(((unsigned)(level+threshold1))>threshold2){
3589 for(i=start_i; i<=last_non_zero; i++) {
3590 const int j = scantable[i];
3591 int level = block[j] * qmat[j];
3593 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3594 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3595 if(((unsigned)(level+threshold1))>threshold2){
3597 level= (bias + level)>>QMAT_SHIFT;
3599 coeff[1][i]= level-1;
3600 // coeff[2][k]= level-2;
3602 level= (bias - level)>>QMAT_SHIFT;
3603 coeff[0][i]= -level;
3604 coeff[1][i]= -level+1;
3605 // coeff[2][k]= -level+2;
3607 coeff_count[i]= FFMIN(level, 2);
3608 assert(coeff_count[i]);
3611 coeff[0][i]= (level>>31)|1;
3616 *overflow= s->max_qcoeff < max; //overflow might have happened
3618 if(last_non_zero < start_i){
3619 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3620 return last_non_zero;
3623 score_tab[start_i]= 0;
3624 survivor[0]= start_i;
3627 for(i=start_i; i<=last_non_zero; i++){
3628 int level_index, j, zero_distortion;
3629 int dct_coeff= FFABS(block[ scantable[i] ]);
3630 int best_score=256*256*256*120;
3632 if (s->fdsp.fdct == ff_fdct_ifast)
3633 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3634 zero_distortion= dct_coeff*dct_coeff;
3636 for(level_index=0; level_index < coeff_count[i]; level_index++){
3638 int level= coeff[level_index][i];
3639 const int alevel= FFABS(level);
3644 if(s->out_format == FMT_H263){
3645 unquant_coeff= alevel*qmul + qadd;
3647 j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
3649 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
3650 unquant_coeff = (unquant_coeff - 1) | 1;
3652 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3653 unquant_coeff = (unquant_coeff - 1) | 1;
3658 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3660 if((level&(~127)) == 0){
3661 for(j=survivor_count-1; j>=0; j--){
3662 int run= i - survivor[j];
3663 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3664 score += score_tab[i-run];
3666 if(score < best_score){
3669 level_tab[i+1]= level-64;
3673 if(s->out_format == FMT_H263){
3674 for(j=survivor_count-1; j>=0; j--){
3675 int run= i - survivor[j];
3676 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3677 score += score_tab[i-run];
3678 if(score < last_score){
3681 last_level= level-64;
3687 distortion += esc_length*lambda;
3688 for(j=survivor_count-1; j>=0; j--){
3689 int run= i - survivor[j];
3690 int score= distortion + score_tab[i-run];
3692 if(score < best_score){
3695 level_tab[i+1]= level-64;
3699 if(s->out_format == FMT_H263){
3700 for(j=survivor_count-1; j>=0; j--){
3701 int run= i - survivor[j];
3702 int score= distortion + score_tab[i-run];
3703 if(score < last_score){
3706 last_level= level-64;
3714 score_tab[i+1]= best_score;
3716 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3717 if(last_non_zero <= 27){
3718 for(; survivor_count; survivor_count--){
3719 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3723 for(; survivor_count; survivor_count--){
3724 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3729 survivor[ survivor_count++ ]= i+1;
3732 if(s->out_format != FMT_H263){
3733 last_score= 256*256*256*120;
3734 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3735 int score= score_tab[i];
3736 if(i) score += lambda*2; //FIXME exacter?
3738 if(score < last_score){
3741 last_level= level_tab[i];
3742 last_run= run_tab[i];
3747 s->coded_score[n] = last_score;
3749 dc= FFABS(block[0]);
3750 last_non_zero= last_i - 1;
3751 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3753 if(last_non_zero < start_i)
3754 return last_non_zero;
3756 if(last_non_zero == 0 && start_i == 0){
3758 int best_score= dc * dc;
3760 for(i=0; i<coeff_count[0]; i++){
3761 int level= coeff[i][0];
3762 int alevel= FFABS(level);
3763 int unquant_coeff, score, distortion;
3765 if(s->out_format == FMT_H263){
3766 unquant_coeff= (alevel*qmul + qadd)>>3;
3768 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3769 unquant_coeff = (unquant_coeff - 1) | 1;
3771 unquant_coeff = (unquant_coeff + 4) >> 3;
3772 unquant_coeff<<= 3 + 3;
3774 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3776 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3777 else score= distortion + esc_length*lambda;
3779 if(score < best_score){
3781 best_level= level - 64;
3784 block[0]= best_level;
3785 s->coded_score[n] = best_score - dc*dc;
3786 if(best_level == 0) return -1;
3787 else return last_non_zero;
3793 block[ perm_scantable[last_non_zero] ]= last_level;
3796 for(; i>start_i; i -= run_tab[i] + 1){
3797 block[ perm_scantable[i-1] ]= level_tab[i];
3800 return last_non_zero;
3803 //#define REFINE_STATS 1
3804 static int16_t basis[64][64];
3806 static void build_basis(uint8_t *perm){
3813 double s= 0.25*(1<<BASIS_SHIFT);
3815 int perm_index= perm[index];
3816 if(i==0) s*= sqrt(0.5);
3817 if(j==0) s*= sqrt(0.5);
3818 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)));
3825 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3826 int16_t *block, int16_t *weight, int16_t *orig,
3829 LOCAL_ALIGNED_16(int16_t, d1, [64]);
3830 const uint8_t *scantable= s->intra_scantable.scantable;
3831 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3832 // unsigned int threshold1, threshold2;
3837 int qmul, qadd, start_i, last_non_zero, i, dc;
3839 uint8_t * last_length;
3841 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3844 static int after_last=0;
3845 static int to_zero=0;
3846 static int from_zero=0;
3849 static int messed_sign=0;
3852 if(basis[0][0] == 0)
3853 build_basis(s->idsp.idct_permutation);
3864 /* For AIC we skip quant/dequant of INTRADC */
3868 q <<= RECON_SHIFT-3;
3869 /* note: block[0] is assumed to be positive */
3871 // block[0] = (block[0] + (q >> 1)) / q;
3873 // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3874 // bias= 1<<(QMAT_SHIFT-1);
3875 length = s->intra_ac_vlc_length;
3876 last_length= s->intra_ac_vlc_last_length;
3880 length = s->inter_ac_vlc_length;
3881 last_length= s->inter_ac_vlc_last_length;
3883 last_non_zero = s->block_last_index[n];
3888 dc += (1<<(RECON_SHIFT-1));
3889 for(i=0; i<64; i++){
3890 rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
3893 STOP_TIMER("memset rem[]")}
3896 for(i=0; i<64; i++){
3901 w= FFABS(weight[i]) + qns*one;
3902 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3905 // w=weight[i] = (63*qns + (w/2)) / w;
3911 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3917 for(i=start_i; i<=last_non_zero; i++){
3918 int j= perm_scantable[i];
3919 const int level= block[j];
3923 if(level<0) coeff= qmul*level - qadd;
3924 else coeff= qmul*level + qadd;
3925 run_tab[rle_index++]=run;
3928 s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
3934 if(last_non_zero>0){
3935 STOP_TIMER("init rem[]")
3942 int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
3945 int run2, best_unquant_change=0, analyze_gradient;
3949 analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
3951 if(analyze_gradient){
3955 for(i=0; i<64; i++){
3958 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
3961 STOP_TIMER("rem*w*w")}
3971 const int level= block[0];
3972 int change, old_coeff;
3974 assert(s->mb_intra);
3978 for(change=-1; change<=1; change+=2){
3979 int new_level= level + change;
3980 int score, new_coeff;
3982 new_coeff= q*new_level;
3983 if(new_coeff >= 2048 || new_coeff < 0)
3986 score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
3987 new_coeff - old_coeff);
3988 if(score<best_score){
3991 best_change= change;
3992 best_unquant_change= new_coeff - old_coeff;
3999 run2= run_tab[rle_index++];
4003 for(i=start_i; i<64; i++){
4004 int j= perm_scantable[i];
4005 const int level= block[j];
4006 int change, old_coeff;
4008 if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4012 if(level<0) old_coeff= qmul*level - qadd;
4013 else old_coeff= qmul*level + qadd;
4014 run2= run_tab[rle_index++]; //FIXME ! maybe after last
4018 assert(run2>=0 || i >= last_non_zero );
4021 for(change=-1; change<=1; change+=2){
4022 int new_level= level + change;
4023 int score, new_coeff, unquant_change;
4026 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4030 if(new_level<0) new_coeff= qmul*new_level - qadd;
4031 else new_coeff= qmul*new_level + qadd;
4032 if(new_coeff >= 2048 || new_coeff <= -2048)
4034 //FIXME check for overflow
4037 if(level < 63 && level > -63){
4038 if(i < last_non_zero)
4039 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
4040 - length[UNI_AC_ENC_INDEX(run, level+64)];
4042 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4043 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4046 assert(FFABS(new_level)==1);
4048 if(analyze_gradient){
4049 int g= d1[ scantable[i] ];
4050 if(g && (g^new_level) >= 0)
4054 if(i < last_non_zero){
4055 int next_i= i + run2 + 1;
4056 int next_level= block[ perm_scantable[next_i] ] + 64;
4058 if(next_level&(~127))
4061 if(next_i < last_non_zero)
4062 score += length[UNI_AC_ENC_INDEX(run, 65)]
4063 + length[UNI_AC_ENC_INDEX(run2, next_level)]
4064 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4066 score += length[UNI_AC_ENC_INDEX(run, 65)]
4067 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4068 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4070 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4072 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4073 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4079 assert(FFABS(level)==1);
4081 if(i < last_non_zero){
4082 int next_i= i + run2 + 1;
4083 int next_level= block[ perm_scantable[next_i] ] + 64;
4085 if(next_level&(~127))
4088 if(next_i < last_non_zero)
4089 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4090 - length[UNI_AC_ENC_INDEX(run2, next_level)]
4091 - length[UNI_AC_ENC_INDEX(run, 65)];
4093 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4094 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4095 - length[UNI_AC_ENC_INDEX(run, 65)];
4097 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4099 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4100 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4107 unquant_change= new_coeff - old_coeff;
4108 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
4110 score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4112 if(score<best_score){
4115 best_change= change;
4116 best_unquant_change= unquant_change;
4120 prev_level= level + 64;
4121 if(prev_level&(~127))
4130 STOP_TIMER("iterative step")}
4134 int j= perm_scantable[ best_coeff ];
4136 block[j] += best_change;
4138 if(best_coeff > last_non_zero){
4139 last_non_zero= best_coeff;
4147 if(block[j] - best_change){
4148 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
4160 for(; last_non_zero>=start_i; last_non_zero--){
4161 if(block[perm_scantable[last_non_zero]])
4167 if(256*256*256*64 % count == 0){
4168 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);
4173 for(i=start_i; i<=last_non_zero; i++){
4174 int j= perm_scantable[i];
4175 const int level= block[j];
4178 run_tab[rle_index++]=run;
4185 s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4191 if(last_non_zero>0){
4192 STOP_TIMER("iterative search")
4197 return last_non_zero;
4201 * Permute an 8x8 block according to permuatation.
4202 * @param block the block which will be permuted according to
4203 * the given permutation vector
4204 * @param permutation the permutation vector
4205 * @param last the last non zero coefficient in scantable order, used to
4206 * speed the permutation up
4207 * @param scantable the used scantable, this is only used to speed the
4208 * permutation up, the block is not (inverse) permutated
4209 * to scantable order!
4211 static void block_permute(int16_t *block, uint8_t *permutation,
4212 const uint8_t *scantable, int last)
4219 //FIXME it is ok but not clean and might fail for some permutations
4220 // if (permutation[1] == 1)
4223 for (i = 0; i <= last; i++) {
4224 const int j = scantable[i];
4229 for (i = 0; i <= last; i++) {
4230 const int j = scantable[i];
4231 const int perm_j = permutation[j];
4232 block[perm_j] = temp[j];
4236 int ff_dct_quantize_c(MpegEncContext *s,
4237 int16_t *block, int n,
4238 int qscale, int *overflow)
4240 int i, j, level, last_non_zero, q, start_i;
4242 const uint8_t *scantable= s->intra_scantable.scantable;
4245 unsigned int threshold1, threshold2;
4247 s->fdsp.fdct(block);
4249 if(s->dct_error_sum)
4250 s->denoise_dct(s, block);
4260 /* For AIC we skip quant/dequant of INTRADC */
4263 /* note: block[0] is assumed to be positive */
4264 block[0] = (block[0] + (q >> 1)) / q;
4267 qmat = s->q_intra_matrix[qscale];
4268 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4272 qmat = s->q_inter_matrix[qscale];
4273 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4275 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4276 threshold2= (threshold1<<1);
4277 for(i=63;i>=start_i;i--) {
4279 level = block[j] * qmat[j];
4281 if(((unsigned)(level+threshold1))>threshold2){
4288 for(i=start_i; i<=last_non_zero; i++) {
4290 level = block[j] * qmat[j];
4292 // if( bias+level >= (1<<QMAT_SHIFT)
4293 // || bias-level >= (1<<QMAT_SHIFT)){
4294 if(((unsigned)(level+threshold1))>threshold2){
4296 level= (bias + level)>>QMAT_SHIFT;
4299 level= (bias - level)>>QMAT_SHIFT;
4307 *overflow= s->max_qcoeff < max; //overflow might have happened
4309 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4310 if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
4311 block_permute(block, s->idsp.idct_permutation,
4312 scantable, last_non_zero);
4314 return last_non_zero;
4317 #define OFFSET(x) offsetof(MpegEncContext, x)
4318 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4319 static const AVOption h263_options[] = {
4320 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4321 { "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},
4322 { "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 },
4327 static const AVClass h263_class = {
4328 .class_name = "H.263 encoder",
4329 .item_name = av_default_item_name,
4330 .option = h263_options,
4331 .version = LIBAVUTIL_VERSION_INT,
4334 AVCodec ff_h263_encoder = {
4336 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4337 .type = AVMEDIA_TYPE_VIDEO,
4338 .id = AV_CODEC_ID_H263,
4339 .priv_data_size = sizeof(MpegEncContext),
4340 .init = ff_mpv_encode_init,
4341 .encode2 = ff_mpv_encode_picture,
4342 .close = ff_mpv_encode_end,
4343 .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4344 .priv_class = &h263_class,
4347 static const AVOption h263p_options[] = {
4348 { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4349 { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4350 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4351 { "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},
4355 static const AVClass h263p_class = {
4356 .class_name = "H.263p encoder",
4357 .item_name = av_default_item_name,
4358 .option = h263p_options,
4359 .version = LIBAVUTIL_VERSION_INT,
4362 AVCodec ff_h263p_encoder = {
4364 .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4365 .type = AVMEDIA_TYPE_VIDEO,
4366 .id = AV_CODEC_ID_H263P,
4367 .priv_data_size = sizeof(MpegEncContext),
4368 .init = ff_mpv_encode_init,
4369 .encode2 = ff_mpv_encode_picture,
4370 .close = ff_mpv_encode_end,
4371 .capabilities = CODEC_CAP_SLICE_THREADS,
4372 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4373 .priv_class = &h263p_class,
4376 FF_MPV_GENERIC_CLASS(msmpeg4v2)
4378 AVCodec ff_msmpeg4v2_encoder = {
4379 .name = "msmpeg4v2",
4380 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
4381 .type = AVMEDIA_TYPE_VIDEO,
4382 .id = AV_CODEC_ID_MSMPEG4V2,
4383 .priv_data_size = sizeof(MpegEncContext),
4384 .init = ff_mpv_encode_init,
4385 .encode2 = ff_mpv_encode_picture,
4386 .close = ff_mpv_encode_end,
4387 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4388 .priv_class = &msmpeg4v2_class,
4391 FF_MPV_GENERIC_CLASS(msmpeg4v3)
4393 AVCodec ff_msmpeg4v3_encoder = {
4395 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
4396 .type = AVMEDIA_TYPE_VIDEO,
4397 .id = AV_CODEC_ID_MSMPEG4V3,
4398 .priv_data_size = sizeof(MpegEncContext),
4399 .init = ff_mpv_encode_init,
4400 .encode2 = ff_mpv_encode_picture,
4401 .close = ff_mpv_encode_end,
4402 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4403 .priv_class = &msmpeg4v3_class,
4406 FF_MPV_GENERIC_CLASS(wmv1)
4408 AVCodec ff_wmv1_encoder = {
4410 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
4411 .type = AVMEDIA_TYPE_VIDEO,
4412 .id = AV_CODEC_ID_WMV1,
4413 .priv_data_size = sizeof(MpegEncContext),
4414 .init = ff_mpv_encode_init,
4415 .encode2 = ff_mpv_encode_picture,
4416 .close = ff_mpv_encode_end,
4417 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4418 .priv_class = &wmv1_class,