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"
65 #define QUANT_BIAS_SHIFT 8
67 #define QMAT_SHIFT_MMX 16
70 static int encode_picture(MpegEncContext *s, int picture_number);
71 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
72 static int sse_mb(MpegEncContext *s);
73 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
74 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
76 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
77 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
79 const AVOption ff_mpv_generic_options[] = {
84 void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
85 uint16_t (*qmat16)[2][64],
86 const uint16_t *quant_matrix,
87 int bias, int qmin, int qmax, int intra)
89 FDCTDSPContext *fdsp = &s->fdsp;
93 for (qscale = qmin; qscale <= qmax; qscale++) {
95 if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
97 fdsp->fdct == ff_faandct ||
98 #endif /* CONFIG_FAANDCT */
99 fdsp->fdct == ff_jpeg_fdct_islow_10) {
100 for (i = 0; i < 64; i++) {
101 const int j = s->idsp.idct_permutation[i];
102 int64_t den = (int64_t) qscale * quant_matrix[j];
103 /* 16 <= qscale * quant_matrix[i] <= 7905
104 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
105 * 19952 <= x <= 249205026
106 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
107 * 3444240 >= (1 << 36) / (x) >= 275 */
109 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / den);
111 } else if (fdsp->fdct == ff_fdct_ifast) {
112 for (i = 0; i < 64; i++) {
113 const int j = s->idsp.idct_permutation[i];
114 int64_t den = ff_aanscales[i] * (int64_t) qscale * quant_matrix[j];
115 /* 16 <= qscale * quant_matrix[i] <= 7905
116 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
117 * 19952 <= x <= 249205026
118 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
119 * 3444240 >= (1 << 36) / (x) >= 275 */
121 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) / den);
124 for (i = 0; i < 64; i++) {
125 const int j = s->idsp.idct_permutation[i];
126 int64_t den = (int64_t) qscale * quant_matrix[j];
127 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
128 * Assume x = qscale * quant_matrix[i]
130 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
131 * so 32768 >= (1 << 19) / (x) >= 67 */
132 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / den);
133 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
134 // (qscale * quant_matrix[i]);
135 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / den;
137 if (qmat16[qscale][0][i] == 0 ||
138 qmat16[qscale][0][i] == 128 * 256)
139 qmat16[qscale][0][i] = 128 * 256 - 1;
140 qmat16[qscale][1][i] =
141 ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
142 qmat16[qscale][0][i]);
146 for (i = intra; i < 64; i++) {
148 if (fdsp->fdct == ff_fdct_ifast) {
149 max = (8191LL * ff_aanscales[i]) >> 14;
151 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
157 av_log(NULL, AV_LOG_INFO,
158 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
163 static inline void update_qscale(MpegEncContext *s)
165 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
166 (FF_LAMBDA_SHIFT + 7);
167 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
169 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
173 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
179 for (i = 0; i < 64; i++) {
180 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
187 * init s->current_picture.qscale_table from s->lambda_table
189 void ff_init_qscale_tab(MpegEncContext *s)
191 int8_t * const qscale_table = s->current_picture.qscale_table;
194 for (i = 0; i < s->mb_num; i++) {
195 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
196 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
197 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
202 static void update_duplicate_context_after_me(MpegEncContext *dst,
205 #define COPY(a) dst->a= src->a
207 COPY(current_picture);
213 COPY(picture_in_gop_number);
214 COPY(gop_picture_number);
215 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
216 COPY(progressive_frame); // FIXME don't set in encode_header
217 COPY(partitioned_frame); // FIXME don't set in encode_header
222 * Set the given MpegEncContext to defaults for encoding.
223 * the changed fields will not depend upon the prior state of the MpegEncContext.
225 static void mpv_encode_defaults(MpegEncContext *s)
228 ff_mpv_common_defaults(s);
230 for (i = -16; i < 16; i++) {
231 default_fcode_tab[i + MAX_MV] = 1;
233 s->me.mv_penalty = default_mv_penalty;
234 s->fcode_tab = default_fcode_tab;
236 s->input_picture_number = 0;
237 s->picture_in_gop_number = 0;
240 /* init video encoder */
241 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
243 MpegEncContext *s = avctx->priv_data;
244 AVCPBProperties *cpb_props;
245 int i, ret, format_supported;
247 mpv_encode_defaults(s);
249 switch (avctx->codec_id) {
250 case AV_CODEC_ID_MPEG2VIDEO:
251 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
252 avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
253 av_log(avctx, AV_LOG_ERROR,
254 "only YUV420 and YUV422 are supported\n");
258 case AV_CODEC_ID_MJPEG:
259 format_supported = 0;
260 /* JPEG color space */
261 if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
262 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
263 (avctx->color_range == AVCOL_RANGE_JPEG &&
264 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
265 avctx->pix_fmt == AV_PIX_FMT_YUV422P)))
266 format_supported = 1;
267 /* MPEG color space */
268 else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
269 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
270 avctx->pix_fmt == AV_PIX_FMT_YUV422P))
271 format_supported = 1;
273 if (!format_supported) {
274 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
279 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
280 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
285 switch (avctx->pix_fmt) {
286 case AV_PIX_FMT_YUVJ422P:
287 case AV_PIX_FMT_YUV422P:
288 s->chroma_format = CHROMA_422;
290 case AV_PIX_FMT_YUVJ420P:
291 case AV_PIX_FMT_YUV420P:
293 s->chroma_format = CHROMA_420;
297 #if FF_API_PRIVATE_OPT
298 FF_DISABLE_DEPRECATION_WARNINGS
299 if (avctx->rtp_payload_size)
300 s->rtp_payload_size = avctx->rtp_payload_size;
301 if (avctx->me_penalty_compensation)
302 s->me_penalty_compensation = avctx->me_penalty_compensation;
304 s->me_pre = avctx->pre_me;
305 FF_ENABLE_DEPRECATION_WARNINGS
308 s->bit_rate = avctx->bit_rate;
309 s->width = avctx->width;
310 s->height = avctx->height;
311 if (avctx->gop_size > 600 &&
312 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
313 av_log(avctx, AV_LOG_ERROR,
314 "Warning keyframe interval too large! reducing it ...\n");
315 avctx->gop_size = 600;
317 s->gop_size = avctx->gop_size;
319 if (avctx->max_b_frames > MAX_B_FRAMES) {
320 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
321 "is %d.\n", MAX_B_FRAMES);
323 s->max_b_frames = avctx->max_b_frames;
324 s->codec_id = avctx->codec->id;
325 s->strict_std_compliance = avctx->strict_std_compliance;
326 s->quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
327 s->rtp_mode = !!s->rtp_payload_size;
328 s->intra_dc_precision = avctx->intra_dc_precision;
329 s->user_specified_pts = AV_NOPTS_VALUE;
331 if (s->gop_size <= 1) {
339 s->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
341 s->adaptive_quant = (s->avctx->lumi_masking ||
342 s->avctx->dark_masking ||
343 s->avctx->temporal_cplx_masking ||
344 s->avctx->spatial_cplx_masking ||
345 s->avctx->p_masking ||
347 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
350 s->loop_filter = !!(s->avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
352 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
353 av_log(avctx, AV_LOG_ERROR,
354 "a vbv buffer size is needed, "
355 "for encoding with a maximum bitrate\n");
359 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
360 av_log(avctx, AV_LOG_INFO,
361 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
364 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
365 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
369 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
370 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
374 if (avctx->rc_max_rate &&
375 avctx->rc_max_rate == avctx->bit_rate &&
376 avctx->rc_max_rate != avctx->rc_min_rate) {
377 av_log(avctx, AV_LOG_INFO,
378 "impossible bitrate constraints, this will fail\n");
381 if (avctx->rc_buffer_size &&
382 avctx->bit_rate * (int64_t)avctx->time_base.num >
383 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
384 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
388 if (!s->fixed_qscale &&
389 avctx->bit_rate * av_q2d(avctx->time_base) >
390 avctx->bit_rate_tolerance) {
391 av_log(avctx, AV_LOG_ERROR,
392 "bitrate tolerance too small for bitrate\n");
396 if (s->avctx->rc_max_rate &&
397 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
398 (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
399 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
400 90000LL * (avctx->rc_buffer_size - 1) >
401 s->avctx->rc_max_rate * 0xFFFFLL) {
402 av_log(avctx, AV_LOG_INFO,
403 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
404 "specified vbv buffer is too large for the given bitrate!\n");
407 if ((s->avctx->flags & AV_CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
408 s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
409 s->codec_id != AV_CODEC_ID_FLV1) {
410 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
414 if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
415 av_log(avctx, AV_LOG_ERROR,
416 "OBMC is only supported with simple mb decision\n");
420 if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
421 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
425 if (s->max_b_frames &&
426 s->codec_id != AV_CODEC_ID_MPEG4 &&
427 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
428 s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
429 av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
433 if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
434 s->codec_id == AV_CODEC_ID_H263 ||
435 s->codec_id == AV_CODEC_ID_H263P) &&
436 (avctx->sample_aspect_ratio.num > 255 ||
437 avctx->sample_aspect_ratio.den > 255)) {
438 av_log(avctx, AV_LOG_ERROR,
439 "Invalid pixel aspect ratio %i/%i, limit is 255/255\n",
440 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
444 if ((s->avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)) &&
445 s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
446 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
450 #if FF_API_PRIVATE_OPT
451 FF_DISABLE_DEPRECATION_WARNINGS
452 if (avctx->mpeg_quant)
453 s->mpeg_quant = avctx->mpeg_quant;
454 FF_ENABLE_DEPRECATION_WARNINGS
457 // FIXME mpeg2 uses that too
458 if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
459 av_log(avctx, AV_LOG_ERROR,
460 "mpeg2 style quantization not supported by codec\n");
464 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
465 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
469 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
470 s->avctx->mb_decision != FF_MB_DECISION_RD) {
471 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
475 #if FF_API_PRIVATE_OPT
476 FF_DISABLE_DEPRECATION_WARNINGS
477 if (avctx->scenechange_threshold)
478 s->scenechange_threshold = avctx->scenechange_threshold;
479 FF_ENABLE_DEPRECATION_WARNINGS
482 if (s->scenechange_threshold < 1000000000 &&
483 (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
484 av_log(avctx, AV_LOG_ERROR,
485 "closed gop with scene change detection are not supported yet, "
486 "set threshold to 1000000000\n");
490 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY) {
491 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
492 av_log(avctx, AV_LOG_ERROR,
493 "low delay forcing is only available for mpeg2\n");
496 if (s->max_b_frames != 0) {
497 av_log(avctx, AV_LOG_ERROR,
498 "B-frames cannot be used with low delay\n");
503 if (s->q_scale_type == 1) {
504 if (avctx->qmax > 12) {
505 av_log(avctx, AV_LOG_ERROR,
506 "non linear quant only supports qmax <= 12 currently\n");
511 if (avctx->slices > 1 &&
512 (avctx->codec_id == AV_CODEC_ID_FLV1 || avctx->codec_id == AV_CODEC_ID_H261)) {
513 av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
514 return AVERROR(EINVAL);
517 if (s->avctx->thread_count > 1 &&
518 s->codec_id != AV_CODEC_ID_MPEG4 &&
519 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
520 s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
521 (s->codec_id != AV_CODEC_ID_H263P)) {
522 av_log(avctx, AV_LOG_ERROR,
523 "multi threaded encoding not supported by codec\n");
527 if (s->avctx->thread_count < 1) {
528 av_log(avctx, AV_LOG_ERROR,
529 "automatic thread number detection not supported by codec,"
534 if (!avctx->time_base.den || !avctx->time_base.num) {
535 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
539 #if FF_API_PRIVATE_OPT
540 FF_DISABLE_DEPRECATION_WARNINGS
541 if (avctx->b_frame_strategy)
542 s->b_frame_strategy = avctx->b_frame_strategy;
543 if (avctx->b_sensitivity != 40)
544 s->b_sensitivity = avctx->b_sensitivity;
545 FF_ENABLE_DEPRECATION_WARNINGS
548 if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
549 av_log(avctx, AV_LOG_INFO,
550 "notice: b_frame_strategy only affects the first pass\n");
551 s->b_frame_strategy = 0;
554 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
556 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
557 avctx->time_base.den /= i;
558 avctx->time_base.num /= i;
562 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
563 s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG) {
564 // (a + x * 3 / 8) / x
565 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
566 s->inter_quant_bias = 0;
568 s->intra_quant_bias = 0;
570 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
573 if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
574 s->avctx->time_base.den > (1 << 16) - 1) {
575 av_log(avctx, AV_LOG_ERROR,
576 "timebase %d/%d not supported by MPEG 4 standard, "
577 "the maximum admitted value for the timebase denominator "
578 "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
582 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
584 switch (avctx->codec->id) {
585 case AV_CODEC_ID_MPEG1VIDEO:
586 s->out_format = FMT_MPEG1;
587 s->low_delay = !!(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
588 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
590 case AV_CODEC_ID_MPEG2VIDEO:
591 s->out_format = FMT_MPEG1;
592 s->low_delay = !!(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
593 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
596 case AV_CODEC_ID_MJPEG:
597 s->out_format = FMT_MJPEG;
598 s->intra_only = 1; /* force intra only for jpeg */
599 if (!CONFIG_MJPEG_ENCODER ||
600 ff_mjpeg_encode_init(s) < 0)
605 case AV_CODEC_ID_H261:
606 if (!CONFIG_H261_ENCODER)
608 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
609 av_log(avctx, AV_LOG_ERROR,
610 "The specified picture size of %dx%d is not valid for the "
611 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
612 s->width, s->height);
615 s->out_format = FMT_H261;
618 s->rtp_mode = 0; /* Sliced encoding not supported */
620 case AV_CODEC_ID_H263:
621 if (!CONFIG_H263_ENCODER)
623 if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
624 s->width, s->height) == 8) {
625 av_log(avctx, AV_LOG_INFO,
626 "The specified picture size of %dx%d is not valid for "
627 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
628 "352x288, 704x576, and 1408x1152."
629 "Try H.263+.\n", s->width, s->height);
632 s->out_format = FMT_H263;
636 case AV_CODEC_ID_H263P:
637 s->out_format = FMT_H263;
640 s->h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
641 s->modified_quant = s->h263_aic;
642 s->loop_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
643 s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
646 /* These are just to be sure */
650 case AV_CODEC_ID_FLV1:
651 s->out_format = FMT_H263;
652 s->h263_flv = 2; /* format = 1; 11-bit codes */
653 s->unrestricted_mv = 1;
654 s->rtp_mode = 0; /* don't allow GOB */
658 case AV_CODEC_ID_RV10:
659 s->out_format = FMT_H263;
663 case AV_CODEC_ID_RV20:
664 s->out_format = FMT_H263;
667 s->modified_quant = 1;
671 s->unrestricted_mv = 0;
673 case AV_CODEC_ID_MPEG4:
674 s->out_format = FMT_H263;
676 s->unrestricted_mv = 1;
677 s->low_delay = s->max_b_frames ? 0 : 1;
678 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
680 case AV_CODEC_ID_MSMPEG4V2:
681 s->out_format = FMT_H263;
683 s->unrestricted_mv = 1;
684 s->msmpeg4_version = 2;
688 case AV_CODEC_ID_MSMPEG4V3:
689 s->out_format = FMT_H263;
691 s->unrestricted_mv = 1;
692 s->msmpeg4_version = 3;
693 s->flipflop_rounding = 1;
697 case AV_CODEC_ID_WMV1:
698 s->out_format = FMT_H263;
700 s->unrestricted_mv = 1;
701 s->msmpeg4_version = 4;
702 s->flipflop_rounding = 1;
706 case AV_CODEC_ID_WMV2:
707 s->out_format = FMT_H263;
709 s->unrestricted_mv = 1;
710 s->msmpeg4_version = 5;
711 s->flipflop_rounding = 1;
719 #if FF_API_PRIVATE_OPT
720 FF_DISABLE_DEPRECATION_WARNINGS
721 if (avctx->noise_reduction)
722 s->noise_reduction = avctx->noise_reduction;
723 FF_ENABLE_DEPRECATION_WARNINGS
726 avctx->has_b_frames = !s->low_delay;
730 s->progressive_frame =
731 s->progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
732 AV_CODEC_FLAG_INTERLACED_ME) ||
737 if (ff_mpv_common_init(s) < 0)
741 ff_mpv_encode_init_x86(s);
743 ff_fdctdsp_init(&s->fdsp, avctx);
744 ff_me_cmp_init(&s->mecc, avctx);
745 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
746 ff_pixblockdsp_init(&s->pdsp, avctx);
747 ff_qpeldsp_init(&s->qdsp);
749 if (s->msmpeg4_version) {
750 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
751 2 * 2 * (MAX_LEVEL + 1) *
752 (MAX_RUN + 1) * 2 * sizeof(int), fail);
754 FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
756 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail);
757 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail);
758 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
759 FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
760 FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
761 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
762 FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
763 MAX_PICTURE_COUNT * sizeof(Picture *), fail);
766 if (s->noise_reduction) {
767 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
768 2 * 64 * sizeof(uint16_t), fail);
771 if (CONFIG_H263_ENCODER)
772 ff_h263dsp_init(&s->h263dsp);
773 if (!s->dct_quantize)
774 s->dct_quantize = ff_dct_quantize_c;
776 s->denoise_dct = denoise_dct_c;
777 s->fast_dct_quantize = s->dct_quantize;
779 s->dct_quantize = dct_quantize_trellis_c;
781 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
782 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
784 if (s->slice_context_count > 1) {
787 if (avctx->codec_id == AV_CODEC_ID_H263 || avctx->codec_id == AV_CODEC_ID_H263P)
788 s->h263_slice_structured = 1;
791 s->quant_precision = 5;
793 #if FF_API_PRIVATE_OPT
794 FF_DISABLE_DEPRECATION_WARNINGS
795 if (avctx->frame_skip_threshold)
796 s->frame_skip_threshold = avctx->frame_skip_threshold;
797 if (avctx->frame_skip_factor)
798 s->frame_skip_factor = avctx->frame_skip_factor;
799 if (avctx->frame_skip_exp)
800 s->frame_skip_exp = avctx->frame_skip_exp;
801 if (avctx->frame_skip_cmp != FF_CMP_DCTMAX)
802 s->frame_skip_cmp = avctx->frame_skip_cmp;
803 FF_ENABLE_DEPRECATION_WARNINGS
806 ff_set_cmp(&s->mecc, s->mecc.ildct_cmp, s->avctx->ildct_cmp);
807 ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->frame_skip_cmp);
809 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
810 ff_h261_encode_init(s);
811 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
812 ff_h263_encode_init(s);
813 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
814 if ((ret = ff_msmpeg4_encode_init(s)) < 0)
816 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
817 && s->out_format == FMT_MPEG1)
818 ff_mpeg1_encode_init(s);
821 for (i = 0; i < 64; i++) {
822 int j = s->idsp.idct_permutation[i];
823 if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
825 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
826 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
827 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
829 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
832 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
833 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
835 if (s->avctx->intra_matrix)
836 s->intra_matrix[j] = s->avctx->intra_matrix[i];
837 if (s->avctx->inter_matrix)
838 s->inter_matrix[j] = s->avctx->inter_matrix[i];
841 /* precompute matrix */
842 /* for mjpeg, we do include qscale in the matrix */
843 if (s->out_format != FMT_MJPEG) {
844 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
845 s->intra_matrix, s->intra_quant_bias, avctx->qmin,
847 ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
848 s->inter_matrix, s->inter_quant_bias, avctx->qmin,
852 if (ff_rate_control_init(s) < 0)
855 #if FF_API_PRIVATE_OPT
856 FF_DISABLE_DEPRECATION_WARNINGS
857 if (avctx->brd_scale)
858 s->brd_scale = avctx->brd_scale;
860 if (avctx->prediction_method)
861 s->pred = avctx->prediction_method + 1;
862 FF_ENABLE_DEPRECATION_WARNINGS
865 if (s->b_frame_strategy == 2) {
866 for (i = 0; i < s->max_b_frames + 2; i++) {
867 s->tmp_frames[i] = av_frame_alloc();
868 if (!s->tmp_frames[i])
869 return AVERROR(ENOMEM);
871 s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
872 s->tmp_frames[i]->width = s->width >> s->brd_scale;
873 s->tmp_frames[i]->height = s->height >> s->brd_scale;
875 ret = av_frame_get_buffer(s->tmp_frames[i], 32);
881 cpb_props = ff_add_cpb_side_data(avctx);
883 return AVERROR(ENOMEM);
884 cpb_props->max_bitrate = avctx->rc_max_rate;
885 cpb_props->min_bitrate = avctx->rc_min_rate;
886 cpb_props->avg_bitrate = avctx->bit_rate;
887 cpb_props->buffer_size = avctx->rc_buffer_size;
891 ff_mpv_encode_end(avctx);
892 return AVERROR_UNKNOWN;
895 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
897 MpegEncContext *s = avctx->priv_data;
900 ff_rate_control_uninit(s);
901 ff_mpv_common_end(s);
902 if (CONFIG_MJPEG_ENCODER &&
903 s->out_format == FMT_MJPEG)
904 ff_mjpeg_encode_close(s);
906 av_freep(&avctx->extradata);
908 for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
909 av_frame_free(&s->tmp_frames[i]);
911 ff_free_picture_tables(&s->new_picture);
912 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
914 av_freep(&s->avctx->stats_out);
915 av_freep(&s->ac_stats);
917 av_freep(&s->q_intra_matrix);
918 av_freep(&s->q_inter_matrix);
919 av_freep(&s->q_intra_matrix16);
920 av_freep(&s->q_inter_matrix16);
921 av_freep(&s->input_picture);
922 av_freep(&s->reordered_input_picture);
923 av_freep(&s->dct_offset);
928 static int get_sae(uint8_t *src, int ref, int stride)
933 for (y = 0; y < 16; y++) {
934 for (x = 0; x < 16; x++) {
935 acc += FFABS(src[x + y * stride] - ref);
942 static int get_intra_count(MpegEncContext *s, uint8_t *src,
943 uint8_t *ref, int stride)
951 for (y = 0; y < h; y += 16) {
952 for (x = 0; x < w; x += 16) {
953 int offset = x + y * stride;
954 int sad = s->mecc.sad[0](NULL, src + offset, ref + offset,
956 int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
957 int sae = get_sae(src + offset, mean, stride);
959 acc += sae + 500 < sad;
965 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
967 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,
968 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
969 s->mb_stride, s->mb_height, s->b8_stride,
970 &s->linesize, &s->uvlinesize);
973 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
977 int i, display_picture_number = 0, ret;
978 int encoding_delay = s->max_b_frames ? s->max_b_frames
979 : (s->low_delay ? 0 : 1);
980 int flush_offset = 1;
985 display_picture_number = s->input_picture_number++;
987 if (pts != AV_NOPTS_VALUE) {
988 if (s->user_specified_pts != AV_NOPTS_VALUE) {
990 int64_t last = s->user_specified_pts;
993 av_log(s->avctx, AV_LOG_ERROR,
994 "Error, Invalid timestamp=%"PRId64", "
995 "last=%"PRId64"\n", pts, s->user_specified_pts);
999 if (!s->low_delay && display_picture_number == 1)
1000 s->dts_delta = time - last;
1002 s->user_specified_pts = pts;
1004 if (s->user_specified_pts != AV_NOPTS_VALUE) {
1005 s->user_specified_pts =
1006 pts = s->user_specified_pts + 1;
1007 av_log(s->avctx, AV_LOG_INFO,
1008 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1011 pts = display_picture_number;
1015 if (!pic_arg->buf[0] ||
1016 pic_arg->linesize[0] != s->linesize ||
1017 pic_arg->linesize[1] != s->uvlinesize ||
1018 pic_arg->linesize[2] != s->uvlinesize)
1020 if ((s->width & 15) || (s->height & 15))
1023 ff_dlog(s->avctx, "%d %d %td %td\n", pic_arg->linesize[0],
1024 pic_arg->linesize[1], s->linesize, s->uvlinesize);
1026 i = ff_find_unused_picture(s->avctx, s->picture, direct);
1030 pic = &s->picture[i];
1034 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1037 ret = alloc_picture(s, pic, direct);
1042 if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1043 pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1044 pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1047 int h_chroma_shift, v_chroma_shift;
1048 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1052 for (i = 0; i < 3; i++) {
1053 int src_stride = pic_arg->linesize[i];
1054 int dst_stride = i ? s->uvlinesize : s->linesize;
1055 int h_shift = i ? h_chroma_shift : 0;
1056 int v_shift = i ? v_chroma_shift : 0;
1057 int w = s->width >> h_shift;
1058 int h = s->height >> v_shift;
1059 uint8_t *src = pic_arg->data[i];
1060 uint8_t *dst = pic->f->data[i];
1062 if (!s->avctx->rc_buffer_size)
1063 dst += INPLACE_OFFSET;
1065 if (src_stride == dst_stride)
1066 memcpy(dst, src, src_stride * h);
1069 uint8_t *dst2 = dst;
1071 memcpy(dst2, src, w);
1076 if ((s->width & 15) || (s->height & 15)) {
1077 s->mpvencdsp.draw_edges(dst, dst_stride,
1086 ret = av_frame_copy_props(pic->f, pic_arg);
1090 pic->f->display_picture_number = display_picture_number;
1091 pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1093 /* Flushing: When we have not received enough input frames,
1094 * ensure s->input_picture[0] contains the first picture */
1095 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1096 if (s->input_picture[flush_offset])
1099 if (flush_offset <= 1)
1102 encoding_delay = encoding_delay - flush_offset + 1;
1105 /* shift buffer entries */
1106 for (i = flush_offset; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1107 s->input_picture[i - flush_offset] = s->input_picture[i];
1109 s->input_picture[encoding_delay] = (Picture*) pic;
1114 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1118 int64_t score64 = 0;
1120 for (plane = 0; plane < 3; plane++) {
1121 const int stride = p->f->linesize[plane];
1122 const int bw = plane ? 1 : 2;
1123 for (y = 0; y < s->mb_height * bw; y++) {
1124 for (x = 0; x < s->mb_width * bw; x++) {
1125 int off = p->shared ? 0 : 16;
1126 uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1127 uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1128 int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1130 switch (s->frame_skip_exp) {
1131 case 0: score = FFMAX(score, v); break;
1132 case 1: score += FFABS(v); break;
1133 case 2: score += v * v; break;
1134 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1135 case 4: score64 += v * v * (int64_t)(v * v); break;
1144 if (score64 < s->frame_skip_threshold)
1146 if (score64 < ((s->frame_skip_factor * (int64_t) s->lambda) >> 8))
1151 static int encode_frame(AVCodecContext *c, AVFrame *frame)
1153 AVPacket pkt = { 0 };
1157 av_init_packet(&pkt);
1159 ret = avcodec_send_frame(c, frame);
1164 ret = avcodec_receive_packet(c, &pkt);
1167 av_packet_unref(&pkt);
1168 } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1175 static int estimate_best_b_count(MpegEncContext *s)
1177 const AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1178 const int scale = s->brd_scale;
1179 int width = s->width >> scale;
1180 int height = s->height >> scale;
1181 int i, j, out_size, p_lambda, b_lambda, lambda2;
1182 int64_t best_rd = INT64_MAX;
1183 int best_b_count = -1;
1186 assert(scale >= 0 && scale <= 3);
1189 //s->next_picture_ptr->quality;
1190 p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1191 //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1192 b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1193 if (!b_lambda) // FIXME we should do this somewhere else
1194 b_lambda = p_lambda;
1195 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1198 for (i = 0; i < s->max_b_frames + 2; i++) {
1199 Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1200 s->next_picture_ptr;
1202 if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1203 pre_input = *pre_input_ptr;
1205 if (!pre_input.shared && i) {
1206 pre_input.f->data[0] += INPLACE_OFFSET;
1207 pre_input.f->data[1] += INPLACE_OFFSET;
1208 pre_input.f->data[2] += INPLACE_OFFSET;
1211 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1212 s->tmp_frames[i]->linesize[0],
1213 pre_input.f->data[0],
1214 pre_input.f->linesize[0],
1216 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1217 s->tmp_frames[i]->linesize[1],
1218 pre_input.f->data[1],
1219 pre_input.f->linesize[1],
1220 width >> 1, height >> 1);
1221 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1222 s->tmp_frames[i]->linesize[2],
1223 pre_input.f->data[2],
1224 pre_input.f->linesize[2],
1225 width >> 1, height >> 1);
1229 for (j = 0; j < s->max_b_frames + 1; j++) {
1233 if (!s->input_picture[j])
1236 c = avcodec_alloc_context3(NULL);
1238 return AVERROR(ENOMEM);
1242 c->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
1243 c->flags |= s->avctx->flags & AV_CODEC_FLAG_QPEL;
1244 c->mb_decision = s->avctx->mb_decision;
1245 c->me_cmp = s->avctx->me_cmp;
1246 c->mb_cmp = s->avctx->mb_cmp;
1247 c->me_sub_cmp = s->avctx->me_sub_cmp;
1248 c->pix_fmt = AV_PIX_FMT_YUV420P;
1249 c->time_base = s->avctx->time_base;
1250 c->max_b_frames = s->max_b_frames;
1252 ret = avcodec_open2(c, codec, NULL);
1256 s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1257 s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1259 out_size = encode_frame(c, s->tmp_frames[0]);
1265 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1267 for (i = 0; i < s->max_b_frames + 1; i++) {
1268 int is_p = i % (j + 1) == j || i == s->max_b_frames;
1270 s->tmp_frames[i + 1]->pict_type = is_p ?
1271 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1272 s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1274 out_size = encode_frame(c, s->tmp_frames[i + 1]);
1280 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1283 /* get the delayed frames */
1284 out_size = encode_frame(c, NULL);
1289 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1291 rd += c->error[0] + c->error[1] + c->error[2];
1299 avcodec_free_context(&c);
1304 return best_b_count;
1307 static int select_input_picture(MpegEncContext *s)
1311 for (i = 1; i < MAX_PICTURE_COUNT; i++)
1312 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1313 s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1315 /* set next picture type & ordering */
1316 if (!s->reordered_input_picture[0] && s->input_picture[0]) {
1317 if (/*s->picture_in_gop_number >= s->gop_size ||*/
1318 !s->next_picture_ptr || s->intra_only) {
1319 s->reordered_input_picture[0] = s->input_picture[0];
1320 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1321 s->reordered_input_picture[0]->f->coded_picture_number =
1322 s->coded_picture_number++;
1326 if (s->frame_skip_threshold || s->frame_skip_factor) {
1327 if (s->picture_in_gop_number < s->gop_size &&
1328 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1329 // FIXME check that the gop check above is +-1 correct
1330 av_frame_unref(s->input_picture[0]->f);
1333 ff_vbv_update(s, 0);
1339 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
1340 for (i = 0; i < s->max_b_frames + 1; i++) {
1341 int pict_num = s->input_picture[0]->f->display_picture_number + i;
1343 if (pict_num >= s->rc_context.num_entries)
1345 if (!s->input_picture[i]) {
1346 s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1350 s->input_picture[i]->f->pict_type =
1351 s->rc_context.entry[pict_num].new_pict_type;
1355 if (s->b_frame_strategy == 0) {
1356 b_frames = s->max_b_frames;
1357 while (b_frames && !s->input_picture[b_frames])
1359 } else if (s->b_frame_strategy == 1) {
1360 for (i = 1; i < s->max_b_frames + 1; i++) {
1361 if (s->input_picture[i] &&
1362 s->input_picture[i]->b_frame_score == 0) {
1363 s->input_picture[i]->b_frame_score =
1365 s->input_picture[i ]->f->data[0],
1366 s->input_picture[i - 1]->f->data[0],
1370 for (i = 0; i < s->max_b_frames + 1; i++) {
1371 if (!s->input_picture[i] ||
1372 s->input_picture[i]->b_frame_score - 1 >
1373 s->mb_num / s->b_sensitivity)
1377 b_frames = FFMAX(0, i - 1);
1380 for (i = 0; i < b_frames + 1; i++) {
1381 s->input_picture[i]->b_frame_score = 0;
1383 } else if (s->b_frame_strategy == 2) {
1384 b_frames = estimate_best_b_count(s);
1391 for (i = b_frames - 1; i >= 0; i--) {
1392 int type = s->input_picture[i]->f->pict_type;
1393 if (type && type != AV_PICTURE_TYPE_B)
1396 if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1397 b_frames == s->max_b_frames) {
1398 av_log(s->avctx, AV_LOG_ERROR,
1399 "warning, too many B-frames in a row\n");
1402 if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1403 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1404 s->gop_size > s->picture_in_gop_number) {
1405 b_frames = s->gop_size - s->picture_in_gop_number - 1;
1407 if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
1409 s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1413 if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1414 s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1417 s->reordered_input_picture[0] = s->input_picture[b_frames];
1418 if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1419 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1420 s->reordered_input_picture[0]->f->coded_picture_number =
1421 s->coded_picture_number++;
1422 for (i = 0; i < b_frames; i++) {
1423 s->reordered_input_picture[i + 1] = s->input_picture[i];
1424 s->reordered_input_picture[i + 1]->f->pict_type =
1426 s->reordered_input_picture[i + 1]->f->coded_picture_number =
1427 s->coded_picture_number++;
1432 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1434 if (s->reordered_input_picture[0]) {
1435 s->reordered_input_picture[0]->reference =
1436 s->reordered_input_picture[0]->f->pict_type !=
1437 AV_PICTURE_TYPE_B ? 3 : 0;
1439 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))
1442 if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1443 // input is a shared pix, so we can't modify it -> allocate a new
1444 // one & ensure that the shared one is reuseable
1447 int i = ff_find_unused_picture(s->avctx, s->picture, 0);
1450 pic = &s->picture[i];
1452 pic->reference = s->reordered_input_picture[0]->reference;
1453 if (alloc_picture(s, pic, 0) < 0) {
1457 ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1461 /* mark us unused / free shared pic */
1462 av_frame_unref(s->reordered_input_picture[0]->f);
1463 s->reordered_input_picture[0]->shared = 0;
1465 s->current_picture_ptr = pic;
1467 // input is not a shared pix -> reuse buffer for current_pix
1468 s->current_picture_ptr = s->reordered_input_picture[0];
1469 for (i = 0; i < 4; i++) {
1470 s->new_picture.f->data[i] += INPLACE_OFFSET;
1473 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1474 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1475 s->current_picture_ptr)) < 0)
1478 s->picture_number = s->new_picture.f->display_picture_number;
1483 static void frame_end(MpegEncContext *s)
1487 if (s->unrestricted_mv &&
1488 s->current_picture.reference &&
1490 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1491 int hshift = desc->log2_chroma_w;
1492 int vshift = desc->log2_chroma_h;
1493 s->mpvencdsp.draw_edges(s->current_picture.f->data[0], s->linesize,
1494 s->h_edge_pos, s->v_edge_pos,
1495 EDGE_WIDTH, EDGE_WIDTH,
1496 EDGE_TOP | EDGE_BOTTOM);
1497 s->mpvencdsp.draw_edges(s->current_picture.f->data[1], s->uvlinesize,
1498 s->h_edge_pos >> hshift,
1499 s->v_edge_pos >> vshift,
1500 EDGE_WIDTH >> hshift,
1501 EDGE_WIDTH >> vshift,
1502 EDGE_TOP | EDGE_BOTTOM);
1503 s->mpvencdsp.draw_edges(s->current_picture.f->data[2], s->uvlinesize,
1504 s->h_edge_pos >> hshift,
1505 s->v_edge_pos >> vshift,
1506 EDGE_WIDTH >> hshift,
1507 EDGE_WIDTH >> vshift,
1508 EDGE_TOP | EDGE_BOTTOM);
1513 s->last_pict_type = s->pict_type;
1514 s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1515 if (s->pict_type!= AV_PICTURE_TYPE_B)
1516 s->last_non_b_pict_type = s->pict_type;
1519 /* release non-reference frames */
1520 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1521 if (!s->picture[i].reference)
1522 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1526 #if FF_API_CODED_FRAME
1527 FF_DISABLE_DEPRECATION_WARNINGS
1528 av_frame_copy_props(s->avctx->coded_frame, s->current_picture.f);
1529 FF_ENABLE_DEPRECATION_WARNINGS
1531 #if FF_API_ERROR_FRAME
1532 FF_DISABLE_DEPRECATION_WARNINGS
1533 memcpy(s->current_picture.f->error, s->current_picture.encoding_error,
1534 sizeof(s->current_picture.encoding_error));
1535 FF_ENABLE_DEPRECATION_WARNINGS
1539 static void update_noise_reduction(MpegEncContext *s)
1543 for (intra = 0; intra < 2; intra++) {
1544 if (s->dct_count[intra] > (1 << 16)) {
1545 for (i = 0; i < 64; i++) {
1546 s->dct_error_sum[intra][i] >>= 1;
1548 s->dct_count[intra] >>= 1;
1551 for (i = 0; i < 64; i++) {
1552 s->dct_offset[intra][i] = (s->noise_reduction *
1553 s->dct_count[intra] +
1554 s->dct_error_sum[intra][i] / 2) /
1555 (s->dct_error_sum[intra][i] + 1);
1560 static int frame_start(MpegEncContext *s)
1564 /* mark & release old frames */
1565 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1566 s->last_picture_ptr != s->next_picture_ptr &&
1567 s->last_picture_ptr->f->buf[0]) {
1568 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1571 s->current_picture_ptr->f->pict_type = s->pict_type;
1572 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1574 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1575 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1576 s->current_picture_ptr)) < 0)
1579 if (s->pict_type != AV_PICTURE_TYPE_B) {
1580 s->last_picture_ptr = s->next_picture_ptr;
1582 s->next_picture_ptr = s->current_picture_ptr;
1585 if (s->last_picture_ptr) {
1586 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1587 if (s->last_picture_ptr->f->buf[0] &&
1588 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1589 s->last_picture_ptr)) < 0)
1592 if (s->next_picture_ptr) {
1593 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1594 if (s->next_picture_ptr->f->buf[0] &&
1595 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1596 s->next_picture_ptr)) < 0)
1600 if (s->picture_structure!= PICT_FRAME) {
1602 for (i = 0; i < 4; i++) {
1603 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1604 s->current_picture.f->data[i] +=
1605 s->current_picture.f->linesize[i];
1607 s->current_picture.f->linesize[i] *= 2;
1608 s->last_picture.f->linesize[i] *= 2;
1609 s->next_picture.f->linesize[i] *= 2;
1613 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1614 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1615 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1616 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1617 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1618 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1620 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1621 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1624 if (s->dct_error_sum) {
1625 assert(s->noise_reduction && s->encoding);
1626 update_noise_reduction(s);
1632 static void write_pass1_stats(MpegEncContext *s)
1634 snprintf(s->avctx->stats_out, 256,
1635 "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d "
1636 "fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d "
1638 s->current_picture_ptr->f->display_picture_number,
1639 s->current_picture_ptr->f->coded_picture_number,
1641 s->current_picture.f->quality,
1648 s->current_picture.mc_mb_var_sum,
1649 s->current_picture.mb_var_sum,
1650 s->i_count, s->skip_count,
1654 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1655 const AVFrame *pic_arg, int *got_packet)
1657 MpegEncContext *s = avctx->priv_data;
1658 int i, stuffing_count, ret;
1659 int context_count = s->slice_context_count;
1661 s->picture_in_gop_number++;
1663 if (load_input_picture(s, pic_arg) < 0)
1666 if (select_input_picture(s) < 0) {
1671 if (s->new_picture.f->data[0]) {
1674 (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
1677 s->mb_info_ptr = av_packet_new_side_data(pkt,
1678 AV_PKT_DATA_H263_MB_INFO,
1679 s->mb_width*s->mb_height*12);
1680 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1683 for (i = 0; i < context_count; i++) {
1684 int start_y = s->thread_context[i]->start_mb_y;
1685 int end_y = s->thread_context[i]-> end_mb_y;
1686 int h = s->mb_height;
1687 uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1688 uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1690 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1693 s->pict_type = s->new_picture.f->pict_type;
1695 ret = frame_start(s);
1699 if (encode_picture(s, s->picture_number) < 0)
1702 #if FF_API_STAT_BITS
1703 FF_DISABLE_DEPRECATION_WARNINGS
1704 avctx->header_bits = s->header_bits;
1705 avctx->mv_bits = s->mv_bits;
1706 avctx->misc_bits = s->misc_bits;
1707 avctx->i_tex_bits = s->i_tex_bits;
1708 avctx->p_tex_bits = s->p_tex_bits;
1709 avctx->i_count = s->i_count;
1710 // FIXME f/b_count in avctx
1711 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
1712 avctx->skip_count = s->skip_count;
1713 FF_ENABLE_DEPRECATION_WARNINGS
1718 sd = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
1721 return AVERROR(ENOMEM);
1722 *(int *)sd = s->current_picture.f->quality;
1724 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1725 ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1727 if (avctx->rc_buffer_size) {
1728 RateControlContext *rcc = &s->rc_context;
1729 int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
1731 if (put_bits_count(&s->pb) > max_size &&
1732 s->lambda < s->lmax) {
1733 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1734 (s->qscale + 1) / s->qscale);
1735 if (s->adaptive_quant) {
1737 for (i = 0; i < s->mb_height * s->mb_stride; i++)
1738 s->lambda_table[i] =
1739 FFMAX(s->lambda_table[i] + 1,
1740 s->lambda_table[i] * (s->qscale + 1) /
1743 s->mb_skipped = 0; // done in frame_start()
1744 // done in encode_picture() so we must undo it
1745 if (s->pict_type == AV_PICTURE_TYPE_P) {
1746 if (s->flipflop_rounding ||
1747 s->codec_id == AV_CODEC_ID_H263P ||
1748 s->codec_id == AV_CODEC_ID_MPEG4)
1749 s->no_rounding ^= 1;
1751 if (s->pict_type != AV_PICTURE_TYPE_B) {
1752 s->time_base = s->last_time_base;
1753 s->last_non_b_time = s->time - s->pp_time;
1755 for (i = 0; i < context_count; i++) {
1756 PutBitContext *pb = &s->thread_context[i]->pb;
1757 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1762 assert(s->avctx->rc_max_rate);
1765 if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
1766 write_pass1_stats(s);
1768 for (i = 0; i < 4; i++) {
1769 s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
1770 avctx->error[i] += s->current_picture_ptr->encoding_error[i];
1773 if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
1774 assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
1775 s->misc_bits + s->i_tex_bits +
1777 flush_put_bits(&s->pb);
1778 s->frame_bits = put_bits_count(&s->pb);
1780 stuffing_count = ff_vbv_update(s, s->frame_bits);
1781 if (stuffing_count) {
1782 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1783 stuffing_count + 50) {
1784 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1788 switch (s->codec_id) {
1789 case AV_CODEC_ID_MPEG1VIDEO:
1790 case AV_CODEC_ID_MPEG2VIDEO:
1791 while (stuffing_count--) {
1792 put_bits(&s->pb, 8, 0);
1795 case AV_CODEC_ID_MPEG4:
1796 put_bits(&s->pb, 16, 0);
1797 put_bits(&s->pb, 16, 0x1C3);
1798 stuffing_count -= 4;
1799 while (stuffing_count--) {
1800 put_bits(&s->pb, 8, 0xFF);
1804 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1806 flush_put_bits(&s->pb);
1807 s->frame_bits = put_bits_count(&s->pb);
1810 /* update MPEG-1/2 vbv_delay for CBR */
1811 if (s->avctx->rc_max_rate &&
1812 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1813 s->out_format == FMT_MPEG1 &&
1814 90000LL * (avctx->rc_buffer_size - 1) <=
1815 s->avctx->rc_max_rate * 0xFFFFLL) {
1816 AVCPBProperties *props;
1819 int vbv_delay, min_delay;
1820 double inbits = s->avctx->rc_max_rate *
1821 av_q2d(s->avctx->time_base);
1822 int minbits = s->frame_bits - 8 *
1823 (s->vbv_delay_ptr - s->pb.buf - 1);
1824 double bits = s->rc_context.buffer_index + minbits - inbits;
1827 av_log(s->avctx, AV_LOG_ERROR,
1828 "Internal error, negative bits\n");
1830 assert(s->repeat_first_field == 0);
1832 vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1833 min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1834 s->avctx->rc_max_rate;
1836 vbv_delay = FFMAX(vbv_delay, min_delay);
1838 assert(vbv_delay < 0xFFFF);
1840 s->vbv_delay_ptr[0] &= 0xF8;
1841 s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1842 s->vbv_delay_ptr[1] = vbv_delay >> 5;
1843 s->vbv_delay_ptr[2] &= 0x07;
1844 s->vbv_delay_ptr[2] |= vbv_delay << 3;
1846 props = av_cpb_properties_alloc(&props_size);
1848 return AVERROR(ENOMEM);
1849 props->vbv_delay = vbv_delay * 300;
1851 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
1852 (uint8_t*)props, props_size);
1858 #if FF_API_VBV_DELAY
1859 FF_DISABLE_DEPRECATION_WARNINGS
1860 avctx->vbv_delay = vbv_delay * 300;
1861 FF_ENABLE_DEPRECATION_WARNINGS
1864 s->total_bits += s->frame_bits;
1865 #if FF_API_STAT_BITS
1866 FF_DISABLE_DEPRECATION_WARNINGS
1867 avctx->frame_bits = s->frame_bits;
1868 FF_ENABLE_DEPRECATION_WARNINGS
1872 pkt->pts = s->current_picture.f->pts;
1873 if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1874 if (!s->current_picture.f->coded_picture_number)
1875 pkt->dts = pkt->pts - s->dts_delta;
1877 pkt->dts = s->reordered_pts;
1878 s->reordered_pts = pkt->pts;
1880 pkt->dts = pkt->pts;
1881 if (s->current_picture.f->key_frame)
1882 pkt->flags |= AV_PKT_FLAG_KEY;
1884 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1888 assert((s->frame_bits & 7) == 0);
1890 pkt->size = s->frame_bits / 8;
1891 *got_packet = !!pkt->size;
1895 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1896 int n, int threshold)
1898 static const char tab[64] = {
1899 3, 2, 2, 1, 1, 1, 1, 1,
1900 1, 1, 1, 1, 1, 1, 1, 1,
1901 1, 1, 1, 1, 1, 1, 1, 1,
1902 0, 0, 0, 0, 0, 0, 0, 0,
1903 0, 0, 0, 0, 0, 0, 0, 0,
1904 0, 0, 0, 0, 0, 0, 0, 0,
1905 0, 0, 0, 0, 0, 0, 0, 0,
1906 0, 0, 0, 0, 0, 0, 0, 0
1911 int16_t *block = s->block[n];
1912 const int last_index = s->block_last_index[n];
1915 if (threshold < 0) {
1917 threshold = -threshold;
1921 /* Are all we could set to zero already zero? */
1922 if (last_index <= skip_dc - 1)
1925 for (i = 0; i <= last_index; i++) {
1926 const int j = s->intra_scantable.permutated[i];
1927 const int level = FFABS(block[j]);
1929 if (skip_dc && i == 0)
1933 } else if (level > 1) {
1939 if (score >= threshold)
1941 for (i = skip_dc; i <= last_index; i++) {
1942 const int j = s->intra_scantable.permutated[i];
1946 s->block_last_index[n] = 0;
1948 s->block_last_index[n] = -1;
1951 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1955 const int maxlevel = s->max_qcoeff;
1956 const int minlevel = s->min_qcoeff;
1960 i = 1; // skip clipping of intra dc
1964 for (; i <= last_index; i++) {
1965 const int j = s->intra_scantable.permutated[i];
1966 int level = block[j];
1968 if (level > maxlevel) {
1971 } else if (level < minlevel) {
1979 if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1980 av_log(s->avctx, AV_LOG_INFO,
1981 "warning, clipping %d dct coefficients to %d..%d\n",
1982 overflow, minlevel, maxlevel);
1985 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1989 for (y = 0; y < 8; y++) {
1990 for (x = 0; x < 8; x++) {
1996 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1997 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1998 int v = ptr[x2 + y2 * stride];
2004 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2009 static av_always_inline void encode_mb_internal(MpegEncContext *s,
2010 int motion_x, int motion_y,
2011 int mb_block_height,
2014 int16_t weight[8][64];
2015 int16_t orig[8][64];
2016 const int mb_x = s->mb_x;
2017 const int mb_y = s->mb_y;
2020 int dct_offset = s->linesize * 8; // default for progressive frames
2021 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2022 ptrdiff_t wrap_y, wrap_c;
2024 for (i = 0; i < mb_block_count; i++)
2025 skip_dct[i] = s->skipdct;
2027 if (s->adaptive_quant) {
2028 const int last_qp = s->qscale;
2029 const int mb_xy = mb_x + mb_y * s->mb_stride;
2031 s->lambda = s->lambda_table[mb_xy];
2034 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2035 s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
2036 s->dquant = s->qscale - last_qp;
2038 if (s->out_format == FMT_H263) {
2039 s->dquant = av_clip(s->dquant, -2, 2);
2041 if (s->codec_id == AV_CODEC_ID_MPEG4) {
2043 if (s->pict_type == AV_PICTURE_TYPE_B) {
2044 if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
2047 if (s->mv_type == MV_TYPE_8X8)
2053 ff_set_qscale(s, last_qp + s->dquant);
2054 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2055 ff_set_qscale(s, s->qscale + s->dquant);
2057 wrap_y = s->linesize;
2058 wrap_c = s->uvlinesize;
2059 ptr_y = s->new_picture.f->data[0] +
2060 (mb_y * 16 * wrap_y) + mb_x * 16;
2061 ptr_cb = s->new_picture.f->data[1] +
2062 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
2063 ptr_cr = s->new_picture.f->data[2] +
2064 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
2066 if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
2067 uint8_t *ebuf = s->sc.edge_emu_buffer + 32;
2068 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2070 16, 16, mb_x * 16, mb_y * 16,
2071 s->width, s->height);
2073 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
2075 8, mb_block_height, mb_x * 8, mb_y * 8,
2076 s->width >> 1, s->height >> 1);
2077 ptr_cb = ebuf + 18 * wrap_y;
2078 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr,
2080 8, mb_block_height, mb_x * 8, mb_y * 8,
2081 s->width >> 1, s->height >> 1);
2082 ptr_cr = ebuf + 18 * wrap_y + 8;
2086 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2087 int progressive_score, interlaced_score;
2089 s->interlaced_dct = 0;
2090 progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
2091 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
2092 NULL, wrap_y, 8) - 400;
2094 if (progressive_score > 0) {
2095 interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
2096 NULL, wrap_y * 2, 8) +
2097 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
2098 NULL, wrap_y * 2, 8);
2099 if (progressive_score > interlaced_score) {
2100 s->interlaced_dct = 1;
2102 dct_offset = wrap_y;
2104 if (s->chroma_format == CHROMA_422)
2110 s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
2111 s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
2112 s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
2113 s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2115 if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2119 s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2120 s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2121 if (!s->chroma_y_shift) { /* 422 */
2122 s->pdsp.get_pixels(s->block[6],
2123 ptr_cb + (dct_offset >> 1), wrap_c);
2124 s->pdsp.get_pixels(s->block[7],
2125 ptr_cr + (dct_offset >> 1), wrap_c);
2129 op_pixels_func (*op_pix)[4];
2130 qpel_mc_func (*op_qpix)[16];
2131 uint8_t *dest_y, *dest_cb, *dest_cr;
2133 dest_y = s->dest[0];
2134 dest_cb = s->dest[1];
2135 dest_cr = s->dest[2];
2137 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
2138 op_pix = s->hdsp.put_pixels_tab;
2139 op_qpix = s->qdsp.put_qpel_pixels_tab;
2141 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2142 op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
2145 if (s->mv_dir & MV_DIR_FORWARD) {
2146 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
2147 s->last_picture.f->data,
2149 op_pix = s->hdsp.avg_pixels_tab;
2150 op_qpix = s->qdsp.avg_qpel_pixels_tab;
2152 if (s->mv_dir & MV_DIR_BACKWARD) {
2153 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
2154 s->next_picture.f->data,
2158 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2159 int progressive_score, interlaced_score;
2161 s->interlaced_dct = 0;
2162 progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2163 s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
2167 if (s->avctx->ildct_cmp == FF_CMP_VSSE)
2168 progressive_score -= 400;
2170 if (progressive_score > 0) {
2171 interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
2173 s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
2177 if (progressive_score > interlaced_score) {
2178 s->interlaced_dct = 1;
2180 dct_offset = wrap_y;
2182 if (s->chroma_format == CHROMA_422)
2188 s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2189 s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2190 s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2191 dest_y + dct_offset, wrap_y);
2192 s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2193 dest_y + dct_offset + 8, wrap_y);
2195 if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2199 s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2200 s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2201 if (!s->chroma_y_shift) { /* 422 */
2202 s->pdsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
2203 dest_cb + (dct_offset >> 1), wrap_c);
2204 s->pdsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
2205 dest_cr + (dct_offset >> 1), wrap_c);
2208 /* pre quantization */
2209 if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2210 2 * s->qscale * s->qscale) {
2212 if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2214 if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2216 if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2217 wrap_y, 8) < 20 * s->qscale)
2219 if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2220 wrap_y, 8) < 20 * s->qscale)
2222 if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2224 if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2226 if (!s->chroma_y_shift) { /* 422 */
2227 if (s->mecc.sad[1](NULL, ptr_cb + (dct_offset >> 1),
2228 dest_cb + (dct_offset >> 1),
2229 wrap_c, 8) < 20 * s->qscale)
2231 if (s->mecc.sad[1](NULL, ptr_cr + (dct_offset >> 1),
2232 dest_cr + (dct_offset >> 1),
2233 wrap_c, 8) < 20 * s->qscale)
2239 if (s->quantizer_noise_shaping) {
2241 get_visual_weight(weight[0], ptr_y , wrap_y);
2243 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2245 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2247 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2249 get_visual_weight(weight[4], ptr_cb , wrap_c);
2251 get_visual_weight(weight[5], ptr_cr , wrap_c);
2252 if (!s->chroma_y_shift) { /* 422 */
2254 get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
2257 get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
2260 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2263 /* DCT & quantize */
2264 assert(s->out_format != FMT_MJPEG || s->qscale == 8);
2266 for (i = 0; i < mb_block_count; i++) {
2269 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2270 // FIXME we could decide to change to quantizer instead of
2272 // JS: I don't think that would be a good idea it could lower
2273 // quality instead of improve it. Just INTRADC clipping
2274 // deserves changes in quantizer
2276 clip_coeffs(s, s->block[i], s->block_last_index[i]);
2278 s->block_last_index[i] = -1;
2280 if (s->quantizer_noise_shaping) {
2281 for (i = 0; i < mb_block_count; i++) {
2283 s->block_last_index[i] =
2284 dct_quantize_refine(s, s->block[i], weight[i],
2285 orig[i], i, s->qscale);
2290 if (s->luma_elim_threshold && !s->mb_intra)
2291 for (i = 0; i < 4; i++)
2292 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2293 if (s->chroma_elim_threshold && !s->mb_intra)
2294 for (i = 4; i < mb_block_count; i++)
2295 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2297 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2298 for (i = 0; i < mb_block_count; i++) {
2299 if (s->block_last_index[i] == -1)
2300 s->coded_score[i] = INT_MAX / 256;
2305 if ((s->avctx->flags & AV_CODEC_FLAG_GRAY) && s->mb_intra) {
2306 s->block_last_index[4] =
2307 s->block_last_index[5] = 0;
2309 s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2312 // non c quantize code returns incorrect block_last_index FIXME
2313 if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2314 for (i = 0; i < mb_block_count; i++) {
2316 if (s->block_last_index[i] > 0) {
2317 for (j = 63; j > 0; j--) {
2318 if (s->block[i][s->intra_scantable.permutated[j]])
2321 s->block_last_index[i] = j;
2326 /* huffman encode */
2327 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2328 case AV_CODEC_ID_MPEG1VIDEO:
2329 case AV_CODEC_ID_MPEG2VIDEO:
2330 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2331 ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2333 case AV_CODEC_ID_MPEG4:
2334 if (CONFIG_MPEG4_ENCODER)
2335 ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2337 case AV_CODEC_ID_MSMPEG4V2:
2338 case AV_CODEC_ID_MSMPEG4V3:
2339 case AV_CODEC_ID_WMV1:
2340 if (CONFIG_MSMPEG4_ENCODER)
2341 ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2343 case AV_CODEC_ID_WMV2:
2344 if (CONFIG_WMV2_ENCODER)
2345 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2347 case AV_CODEC_ID_H261:
2348 if (CONFIG_H261_ENCODER)
2349 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2351 case AV_CODEC_ID_H263:
2352 case AV_CODEC_ID_H263P:
2353 case AV_CODEC_ID_FLV1:
2354 case AV_CODEC_ID_RV10:
2355 case AV_CODEC_ID_RV20:
2356 if (CONFIG_H263_ENCODER)
2357 ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2359 case AV_CODEC_ID_MJPEG:
2360 if (CONFIG_MJPEG_ENCODER)
2361 ff_mjpeg_encode_mb(s, s->block);
2368 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2370 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
2371 else encode_mb_internal(s, motion_x, motion_y, 16, 8);
2374 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2377 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2380 d->mb_skip_run= s->mb_skip_run;
2382 d->last_dc[i] = s->last_dc[i];
2385 d->mv_bits= s->mv_bits;
2386 d->i_tex_bits= s->i_tex_bits;
2387 d->p_tex_bits= s->p_tex_bits;
2388 d->i_count= s->i_count;
2389 d->f_count= s->f_count;
2390 d->b_count= s->b_count;
2391 d->skip_count= s->skip_count;
2392 d->misc_bits= s->misc_bits;
2396 d->qscale= s->qscale;
2397 d->dquant= s->dquant;
2399 d->esc3_level_length= s->esc3_level_length;
2402 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2405 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2406 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2409 d->mb_skip_run= s->mb_skip_run;
2411 d->last_dc[i] = s->last_dc[i];
2414 d->mv_bits= s->mv_bits;
2415 d->i_tex_bits= s->i_tex_bits;
2416 d->p_tex_bits= s->p_tex_bits;
2417 d->i_count= s->i_count;
2418 d->f_count= s->f_count;
2419 d->b_count= s->b_count;
2420 d->skip_count= s->skip_count;
2421 d->misc_bits= s->misc_bits;
2423 d->mb_intra= s->mb_intra;
2424 d->mb_skipped= s->mb_skipped;
2425 d->mv_type= s->mv_type;
2426 d->mv_dir= s->mv_dir;
2428 if(s->data_partitioning){
2430 d->tex_pb= s->tex_pb;
2434 d->block_last_index[i]= s->block_last_index[i];
2435 d->interlaced_dct= s->interlaced_dct;
2436 d->qscale= s->qscale;
2438 d->esc3_level_length= s->esc3_level_length;
2441 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2442 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2443 int *dmin, int *next_block, int motion_x, int motion_y)
2446 uint8_t *dest_backup[3];
2448 copy_context_before_encode(s, backup, type);
2450 s->block= s->blocks[*next_block];
2451 s->pb= pb[*next_block];
2452 if(s->data_partitioning){
2453 s->pb2 = pb2 [*next_block];
2454 s->tex_pb= tex_pb[*next_block];
2458 memcpy(dest_backup, s->dest, sizeof(s->dest));
2459 s->dest[0] = s->sc.rd_scratchpad;
2460 s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
2461 s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
2462 assert(s->linesize >= 32); //FIXME
2465 encode_mb(s, motion_x, motion_y);
2467 score= put_bits_count(&s->pb);
2468 if(s->data_partitioning){
2469 score+= put_bits_count(&s->pb2);
2470 score+= put_bits_count(&s->tex_pb);
2473 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2474 ff_mpv_decode_mb(s, s->block);
2476 score *= s->lambda2;
2477 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2481 memcpy(s->dest, dest_backup, sizeof(s->dest));
2488 copy_context_after_encode(best, s, type);
2492 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2493 uint32_t *sq = ff_square_tab + 256;
2498 return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2499 else if(w==8 && h==8)
2500 return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2504 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2513 static int sse_mb(MpegEncContext *s){
2517 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2518 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2521 if(s->avctx->mb_cmp == FF_CMP_NSSE){
2522 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) +
2523 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) +
2524 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);
2526 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) +
2527 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) +
2528 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);
2531 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)
2532 +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)
2533 +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);
2536 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2537 MpegEncContext *s= *(void**)arg;
2541 s->me.dia_size= s->avctx->pre_dia_size;
2542 s->first_slice_line=1;
2543 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2544 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2545 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2547 s->first_slice_line=0;
2555 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2556 MpegEncContext *s= *(void**)arg;
2558 s->me.dia_size= s->avctx->dia_size;
2559 s->first_slice_line=1;
2560 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2561 s->mb_x=0; //for block init below
2562 ff_init_block_index(s);
2563 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2564 s->block_index[0]+=2;
2565 s->block_index[1]+=2;
2566 s->block_index[2]+=2;
2567 s->block_index[3]+=2;
2569 /* compute motion vector & mb_type and store in context */
2570 if(s->pict_type==AV_PICTURE_TYPE_B)
2571 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2573 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2575 s->first_slice_line=0;
2580 static int mb_var_thread(AVCodecContext *c, void *arg){
2581 MpegEncContext *s= *(void**)arg;
2584 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2585 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2588 uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2590 int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2592 varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
2593 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2595 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2596 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2597 s->me.mb_var_sum_temp += varc;
2603 static void write_slice_end(MpegEncContext *s){
2604 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2605 if(s->partitioned_frame){
2606 ff_mpeg4_merge_partitions(s);
2609 ff_mpeg4_stuffing(&s->pb);
2610 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2611 ff_mjpeg_encode_stuffing(&s->pb);
2614 avpriv_align_put_bits(&s->pb);
2615 flush_put_bits(&s->pb);
2617 if ((s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
2618 s->misc_bits+= get_bits_diff(s);
2621 static void write_mb_info(MpegEncContext *s)
2623 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2624 int offset = put_bits_count(&s->pb);
2625 int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2626 int gobn = s->mb_y / s->gob_index;
2628 if (CONFIG_H263_ENCODER)
2629 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2630 bytestream_put_le32(&ptr, offset);
2631 bytestream_put_byte(&ptr, s->qscale);
2632 bytestream_put_byte(&ptr, gobn);
2633 bytestream_put_le16(&ptr, mba);
2634 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2635 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2636 /* 4MV not implemented */
2637 bytestream_put_byte(&ptr, 0); /* hmv2 */
2638 bytestream_put_byte(&ptr, 0); /* vmv2 */
2641 static void update_mb_info(MpegEncContext *s, int startcode)
2645 if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2646 s->mb_info_size += 12;
2647 s->prev_mb_info = s->last_mb_info;
2650 s->prev_mb_info = put_bits_count(&s->pb)/8;
2651 /* This might have incremented mb_info_size above, and we return without
2652 * actually writing any info into that slot yet. But in that case,
2653 * this will be called again at the start of the after writing the
2654 * start code, actually writing the mb info. */
2658 s->last_mb_info = put_bits_count(&s->pb)/8;
2659 if (!s->mb_info_size)
2660 s->mb_info_size += 12;
2664 static int encode_thread(AVCodecContext *c, void *arg){
2665 MpegEncContext *s= *(void**)arg;
2667 int chr_h= 16>>s->chroma_y_shift;
2669 MpegEncContext best_s = { 0 }, backup_s;
2670 uint8_t bit_buf[2][MAX_MB_BYTES];
2671 uint8_t bit_buf2[2][MAX_MB_BYTES];
2672 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2673 PutBitContext pb[2], pb2[2], tex_pb[2];
2676 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2677 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2678 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2681 s->last_bits= put_bits_count(&s->pb);
2692 /* init last dc values */
2693 /* note: quant matrix value (8) is implied here */
2694 s->last_dc[i] = 128 << s->intra_dc_precision;
2696 s->current_picture.encoding_error[i] = 0;
2699 memset(s->last_mv, 0, sizeof(s->last_mv));
2703 switch(s->codec_id){
2704 case AV_CODEC_ID_H263:
2705 case AV_CODEC_ID_H263P:
2706 case AV_CODEC_ID_FLV1:
2707 if (CONFIG_H263_ENCODER)
2708 s->gob_index = H263_GOB_HEIGHT(s->height);
2710 case AV_CODEC_ID_MPEG4:
2711 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2712 ff_mpeg4_init_partitions(s);
2718 s->first_slice_line = 1;
2719 s->ptr_lastgob = s->pb.buf;
2720 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2724 ff_set_qscale(s, s->qscale);
2725 ff_init_block_index(s);
2727 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2728 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2729 int mb_type= s->mb_type[xy];
2734 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2735 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2738 if(s->data_partitioning){
2739 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2740 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2741 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2747 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2748 ff_update_block_index(s);
2750 if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2751 ff_h261_reorder_mb_index(s);
2752 xy= s->mb_y*s->mb_stride + s->mb_x;
2753 mb_type= s->mb_type[xy];
2756 /* write gob / video packet header */
2758 int current_packet_size, is_gob_start;
2760 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2762 is_gob_start = s->rtp_payload_size &&
2763 current_packet_size >= s->rtp_payload_size &&
2766 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2768 switch(s->codec_id){
2769 case AV_CODEC_ID_H263:
2770 case AV_CODEC_ID_H263P:
2771 if(!s->h263_slice_structured)
2772 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2774 case AV_CODEC_ID_MPEG2VIDEO:
2775 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2776 case AV_CODEC_ID_MPEG1VIDEO:
2777 if(s->mb_skip_run) is_gob_start=0;
2782 if(s->start_mb_y != mb_y || mb_x!=0){
2785 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2786 ff_mpeg4_init_partitions(s);
2790 assert((put_bits_count(&s->pb)&7) == 0);
2791 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2793 if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2794 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2795 int d = 100 / s->error_rate;
2797 current_packet_size=0;
2798 s->pb.buf_ptr= s->ptr_lastgob;
2799 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2803 #if FF_API_RTP_CALLBACK
2804 FF_DISABLE_DEPRECATION_WARNINGS
2805 if (s->avctx->rtp_callback){
2806 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2807 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2809 FF_ENABLE_DEPRECATION_WARNINGS
2811 update_mb_info(s, 1);
2813 switch(s->codec_id){
2814 case AV_CODEC_ID_MPEG4:
2815 if (CONFIG_MPEG4_ENCODER) {
2816 ff_mpeg4_encode_video_packet_header(s);
2817 ff_mpeg4_clean_buffers(s);
2820 case AV_CODEC_ID_MPEG1VIDEO:
2821 case AV_CODEC_ID_MPEG2VIDEO:
2822 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2823 ff_mpeg1_encode_slice_header(s);
2824 ff_mpeg1_clean_buffers(s);
2827 case AV_CODEC_ID_H263:
2828 case AV_CODEC_ID_H263P:
2829 if (CONFIG_H263_ENCODER)
2830 ff_h263_encode_gob_header(s, mb_y);
2834 if (s->avctx->flags & AV_CODEC_FLAG_PASS1) {
2835 int bits= put_bits_count(&s->pb);
2836 s->misc_bits+= bits - s->last_bits;
2840 s->ptr_lastgob += current_packet_size;
2841 s->first_slice_line=1;
2842 s->resync_mb_x=mb_x;
2843 s->resync_mb_y=mb_y;
2847 if( (s->resync_mb_x == s->mb_x)
2848 && s->resync_mb_y+1 == s->mb_y){
2849 s->first_slice_line=0;
2853 s->dquant=0; //only for QP_RD
2855 update_mb_info(s, 0);
2857 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
2859 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2861 copy_context_before_encode(&backup_s, s, -1);
2863 best_s.data_partitioning= s->data_partitioning;
2864 best_s.partitioned_frame= s->partitioned_frame;
2865 if(s->data_partitioning){
2866 backup_s.pb2= s->pb2;
2867 backup_s.tex_pb= s->tex_pb;
2870 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2871 s->mv_dir = MV_DIR_FORWARD;
2872 s->mv_type = MV_TYPE_16X16;
2874 s->mv[0][0][0] = s->p_mv_table[xy][0];
2875 s->mv[0][0][1] = s->p_mv_table[xy][1];
2876 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2877 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2879 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2880 s->mv_dir = MV_DIR_FORWARD;
2881 s->mv_type = MV_TYPE_FIELD;
2884 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2885 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2886 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2888 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2889 &dmin, &next_block, 0, 0);
2891 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2892 s->mv_dir = MV_DIR_FORWARD;
2893 s->mv_type = MV_TYPE_16X16;
2897 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2898 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2900 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2901 s->mv_dir = MV_DIR_FORWARD;
2902 s->mv_type = MV_TYPE_8X8;
2905 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2906 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2908 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2909 &dmin, &next_block, 0, 0);
2911 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2912 s->mv_dir = MV_DIR_FORWARD;
2913 s->mv_type = MV_TYPE_16X16;
2915 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2916 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2917 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2918 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2920 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2921 s->mv_dir = MV_DIR_BACKWARD;
2922 s->mv_type = MV_TYPE_16X16;
2924 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2925 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2926 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2927 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2929 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2930 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2931 s->mv_type = MV_TYPE_16X16;
2933 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2934 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2935 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2936 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2937 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2938 &dmin, &next_block, 0, 0);
2940 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2941 s->mv_dir = MV_DIR_FORWARD;
2942 s->mv_type = MV_TYPE_FIELD;
2945 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2946 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2947 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2949 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2950 &dmin, &next_block, 0, 0);
2952 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2953 s->mv_dir = MV_DIR_BACKWARD;
2954 s->mv_type = MV_TYPE_FIELD;
2957 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2958 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2959 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2961 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2962 &dmin, &next_block, 0, 0);
2964 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2965 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2966 s->mv_type = MV_TYPE_FIELD;
2968 for(dir=0; dir<2; dir++){
2970 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2971 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2972 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2975 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2976 &dmin, &next_block, 0, 0);
2978 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2980 s->mv_type = MV_TYPE_16X16;
2984 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2985 &dmin, &next_block, 0, 0);
2986 if(s->h263_pred || s->h263_aic){
2988 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2990 ff_clean_intra_table_entries(s); //old mode?
2994 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2995 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2996 const int last_qp= backup_s.qscale;
2999 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
3000 static const int dquant_tab[4]={-1,1,-2,2};
3002 assert(backup_s.dquant == 0);
3005 s->mv_dir= best_s.mv_dir;
3006 s->mv_type = MV_TYPE_16X16;
3007 s->mb_intra= best_s.mb_intra;
3008 s->mv[0][0][0] = best_s.mv[0][0][0];
3009 s->mv[0][0][1] = best_s.mv[0][0][1];
3010 s->mv[1][0][0] = best_s.mv[1][0][0];
3011 s->mv[1][0][1] = best_s.mv[1][0][1];
3013 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3014 for(; qpi<4; qpi++){
3015 int dquant= dquant_tab[qpi];
3016 qp= last_qp + dquant;
3017 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
3019 backup_s.dquant= dquant;
3020 if(s->mb_intra && s->dc_val[0]){
3022 dc[i]= s->dc_val[0][ s->block_index[i] ];
3023 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
3027 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3028 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
3029 if(best_s.qscale != qp){
3030 if(s->mb_intra && s->dc_val[0]){
3032 s->dc_val[0][ s->block_index[i] ]= dc[i];
3033 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
3040 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3041 int mx= s->b_direct_mv_table[xy][0];
3042 int my= s->b_direct_mv_table[xy][1];
3044 backup_s.dquant = 0;
3045 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3047 ff_mpeg4_set_direct_mv(s, mx, my);
3048 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3049 &dmin, &next_block, mx, my);
3051 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3052 backup_s.dquant = 0;
3053 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3055 ff_mpeg4_set_direct_mv(s, 0, 0);
3056 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3057 &dmin, &next_block, 0, 0);
3059 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3062 coded |= s->block_last_index[i];
3065 memcpy(s->mv, best_s.mv, sizeof(s->mv));
3066 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
3067 mx=my=0; //FIXME find the one we actually used
3068 ff_mpeg4_set_direct_mv(s, mx, my);
3069 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
3077 s->mv_dir= best_s.mv_dir;
3078 s->mv_type = best_s.mv_type;
3080 /* s->mv[0][0][0] = best_s.mv[0][0][0];
3081 s->mv[0][0][1] = best_s.mv[0][0][1];
3082 s->mv[1][0][0] = best_s.mv[1][0][0];
3083 s->mv[1][0][1] = best_s.mv[1][0][1];*/
3086 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3087 &dmin, &next_block, mx, my);
3092 s->current_picture.qscale_table[xy] = best_s.qscale;
3094 copy_context_after_encode(s, &best_s, -1);
3096 pb_bits_count= put_bits_count(&s->pb);
3097 flush_put_bits(&s->pb);
3098 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3101 if(s->data_partitioning){
3102 pb2_bits_count= put_bits_count(&s->pb2);
3103 flush_put_bits(&s->pb2);
3104 avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3105 s->pb2= backup_s.pb2;
3107 tex_pb_bits_count= put_bits_count(&s->tex_pb);
3108 flush_put_bits(&s->tex_pb);
3109 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3110 s->tex_pb= backup_s.tex_pb;
3112 s->last_bits= put_bits_count(&s->pb);
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 if(next_block==0){ //FIXME 16 vs linesize16
3119 s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
3120 s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
3121 s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
3124 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
3125 ff_mpv_decode_mb(s, s->block);
3127 int motion_x = 0, motion_y = 0;
3128 s->mv_type=MV_TYPE_16X16;
3129 // only one MB-Type possible
3132 case CANDIDATE_MB_TYPE_INTRA:
3135 motion_x= s->mv[0][0][0] = 0;
3136 motion_y= s->mv[0][0][1] = 0;
3138 case CANDIDATE_MB_TYPE_INTER:
3139 s->mv_dir = MV_DIR_FORWARD;
3141 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3142 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3144 case CANDIDATE_MB_TYPE_INTER_I:
3145 s->mv_dir = MV_DIR_FORWARD;
3146 s->mv_type = MV_TYPE_FIELD;
3149 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3150 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3151 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3154 case CANDIDATE_MB_TYPE_INTER4V:
3155 s->mv_dir = MV_DIR_FORWARD;
3156 s->mv_type = MV_TYPE_8X8;
3159 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3160 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3163 case CANDIDATE_MB_TYPE_DIRECT:
3164 if (CONFIG_MPEG4_ENCODER) {
3165 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3167 motion_x=s->b_direct_mv_table[xy][0];
3168 motion_y=s->b_direct_mv_table[xy][1];
3169 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3172 case CANDIDATE_MB_TYPE_DIRECT0:
3173 if (CONFIG_MPEG4_ENCODER) {
3174 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3176 ff_mpeg4_set_direct_mv(s, 0, 0);
3179 case CANDIDATE_MB_TYPE_BIDIR:
3180 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3182 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3183 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3184 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3185 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3187 case CANDIDATE_MB_TYPE_BACKWARD:
3188 s->mv_dir = MV_DIR_BACKWARD;
3190 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3191 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3193 case CANDIDATE_MB_TYPE_FORWARD:
3194 s->mv_dir = MV_DIR_FORWARD;
3196 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3197 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3199 case CANDIDATE_MB_TYPE_FORWARD_I:
3200 s->mv_dir = MV_DIR_FORWARD;
3201 s->mv_type = MV_TYPE_FIELD;
3204 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3205 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3206 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3209 case CANDIDATE_MB_TYPE_BACKWARD_I:
3210 s->mv_dir = MV_DIR_BACKWARD;
3211 s->mv_type = MV_TYPE_FIELD;
3214 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3215 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3216 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3219 case CANDIDATE_MB_TYPE_BIDIR_I:
3220 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3221 s->mv_type = MV_TYPE_FIELD;
3223 for(dir=0; dir<2; dir++){
3225 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3226 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3227 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3232 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3235 encode_mb(s, motion_x, motion_y);
3237 // RAL: Update last macroblock type
3238 s->last_mv_dir = s->mv_dir;
3240 if (CONFIG_H263_ENCODER &&
3241 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3242 ff_h263_update_motion_val(s);
3244 ff_mpv_decode_mb(s, s->block);
3247 /* clean the MV table in IPS frames for direct mode in B-frames */
3248 if(s->mb_intra /* && I,P,S_TYPE */){
3249 s->p_mv_table[xy][0]=0;
3250 s->p_mv_table[xy][1]=0;
3253 if (s->avctx->flags & AV_CODEC_FLAG_PSNR) {
3257 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3258 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3260 s->current_picture.encoding_error[0] += sse(
3261 s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3262 s->dest[0], w, h, s->linesize);
3263 s->current_picture.encoding_error[1] += sse(
3264 s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3265 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3266 s->current_picture.encoding_error[2] += sse(
3267 s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3268 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3271 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3272 ff_h263_loop_filter(s);
3274 ff_dlog(s->avctx, "MB %d %d bits\n",
3275 s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3279 //not beautiful here but we must write it before flushing so it has to be here
3280 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3281 ff_msmpeg4_encode_ext_header(s);
3285 #if FF_API_RTP_CALLBACK
3286 FF_DISABLE_DEPRECATION_WARNINGS
3287 /* Send the last GOB if RTP */
3288 if (s->avctx->rtp_callback) {
3289 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3290 int pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3291 /* Call the RTP callback to send the last GOB */
3293 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3295 FF_ENABLE_DEPRECATION_WARNINGS
3301 #define MERGE(field) dst->field += src->field; src->field=0
3302 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3303 MERGE(me.scene_change_score);
3304 MERGE(me.mc_mb_var_sum_temp);
3305 MERGE(me.mb_var_sum_temp);
3308 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3311 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3312 MERGE(dct_count[1]);
3321 MERGE(er.error_count);
3322 MERGE(padding_bug_score);
3323 MERGE(current_picture.encoding_error[0]);
3324 MERGE(current_picture.encoding_error[1]);
3325 MERGE(current_picture.encoding_error[2]);
3327 if (dst->noise_reduction){
3328 for(i=0; i<64; i++){
3329 MERGE(dct_error_sum[0][i]);
3330 MERGE(dct_error_sum[1][i]);
3334 assert(put_bits_count(&src->pb) % 8 ==0);
3335 assert(put_bits_count(&dst->pb) % 8 ==0);
3336 avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3337 flush_put_bits(&dst->pb);
3340 static int estimate_qp(MpegEncContext *s, int dry_run){
3341 if (s->next_lambda){
3342 s->current_picture_ptr->f->quality =
3343 s->current_picture.f->quality = s->next_lambda;
3344 if(!dry_run) s->next_lambda= 0;
3345 } else if (!s->fixed_qscale) {
3347 quality = ff_rate_estimate_qscale(s, dry_run);
3348 s->current_picture_ptr->f->quality =
3349 s->current_picture.f->quality = quality;
3350 if (s->current_picture.f->quality < 0)
3354 if(s->adaptive_quant){
3355 switch(s->codec_id){
3356 case AV_CODEC_ID_MPEG4:
3357 if (CONFIG_MPEG4_ENCODER)
3358 ff_clean_mpeg4_qscales(s);
3360 case AV_CODEC_ID_H263:
3361 case AV_CODEC_ID_H263P:
3362 case AV_CODEC_ID_FLV1:
3363 if (CONFIG_H263_ENCODER)
3364 ff_clean_h263_qscales(s);
3367 ff_init_qscale_tab(s);
3370 s->lambda= s->lambda_table[0];
3373 s->lambda = s->current_picture.f->quality;
3378 /* must be called before writing the header */
3379 static void set_frame_distances(MpegEncContext * s){
3380 assert(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
3381 s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3383 if(s->pict_type==AV_PICTURE_TYPE_B){
3384 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3385 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3387 s->pp_time= s->time - s->last_non_b_time;
3388 s->last_non_b_time= s->time;
3389 assert(s->picture_number==0 || s->pp_time > 0);
3393 static int encode_picture(MpegEncContext *s, int picture_number)
3397 int context_count = s->slice_context_count;
3399 s->picture_number = picture_number;
3401 /* Reset the average MB variance */
3402 s->me.mb_var_sum_temp =
3403 s->me.mc_mb_var_sum_temp = 0;
3405 /* we need to initialize some time vars before we can encode B-frames */
3406 // RAL: Condition added for MPEG1VIDEO
3407 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3408 set_frame_distances(s);
3409 if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3410 ff_set_mpeg4_time(s);
3412 s->me.scene_change_score=0;
3414 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3416 if(s->pict_type==AV_PICTURE_TYPE_I){
3417 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3418 else s->no_rounding=0;
3419 }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3420 if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3421 s->no_rounding ^= 1;
3424 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
3425 if (estimate_qp(s,1) < 0)
3427 ff_get_2pass_fcode(s);
3428 } else if (!(s->avctx->flags & AV_CODEC_FLAG_QSCALE)) {
3429 if(s->pict_type==AV_PICTURE_TYPE_B)
3430 s->lambda= s->last_lambda_for[s->pict_type];
3432 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3436 s->mb_intra=0; //for the rate distortion & bit compare functions
3437 for(i=1; i<context_count; i++){
3438 ret = ff_update_duplicate_context(s->thread_context[i], s);
3446 /* Estimate motion for every MB */
3447 if(s->pict_type != AV_PICTURE_TYPE_I){
3448 s->lambda = (s->lambda * s->me_penalty_compensation + 128) >> 8;
3449 s->lambda2 = (s->lambda2 * (int64_t) s->me_penalty_compensation + 128) >> 8;
3450 if (s->pict_type != AV_PICTURE_TYPE_B) {
3451 if ((s->me_pre && s->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
3453 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3457 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3458 }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3460 for(i=0; i<s->mb_stride*s->mb_height; i++)
3461 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3463 if(!s->fixed_qscale){
3464 /* finding spatial complexity for I-frame rate control */
3465 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3468 for(i=1; i<context_count; i++){
3469 merge_context_after_me(s, s->thread_context[i]);
3471 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3472 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
3475 if (s->me.scene_change_score > s->scenechange_threshold &&
3476 s->pict_type == AV_PICTURE_TYPE_P) {
3477 s->pict_type= AV_PICTURE_TYPE_I;
3478 for(i=0; i<s->mb_stride*s->mb_height; i++)
3479 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3480 ff_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
3481 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3485 if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3486 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3488 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3490 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3491 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3492 s->f_code= FFMAX3(s->f_code, a, b);
3495 ff_fix_long_p_mvs(s);
3496 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3497 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3501 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3502 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3507 if(s->pict_type==AV_PICTURE_TYPE_B){
3510 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3511 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3512 s->f_code = FFMAX(a, b);
3514 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3515 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3516 s->b_code = FFMAX(a, b);
3518 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3519 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3520 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3521 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3522 if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3524 for(dir=0; dir<2; dir++){
3527 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3528 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3529 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3530 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3538 if (estimate_qp(s, 0) < 0)
3541 if (s->qscale < 3 && s->max_qcoeff <= 128 &&
3542 s->pict_type == AV_PICTURE_TYPE_I &&
3543 !(s->avctx->flags & AV_CODEC_FLAG_QSCALE))
3544 s->qscale= 3; //reduce clipping problems
3546 if (s->out_format == FMT_MJPEG) {
3547 /* for mjpeg, we do include qscale in the matrix */
3549 int j = s->idsp.idct_permutation[i];
3551 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3553 s->y_dc_scale_table=
3554 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3555 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3556 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3557 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3561 //FIXME var duplication
3562 s->current_picture_ptr->f->key_frame =
3563 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3564 s->current_picture_ptr->f->pict_type =
3565 s->current_picture.f->pict_type = s->pict_type;
3567 if (s->current_picture.f->key_frame)
3568 s->picture_in_gop_number=0;
3570 s->last_bits= put_bits_count(&s->pb);
3571 switch(s->out_format) {
3573 if (CONFIG_MJPEG_ENCODER)
3574 ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
3575 s->pred, s->intra_matrix);
3578 if (CONFIG_H261_ENCODER)
3579 ff_h261_encode_picture_header(s, picture_number);
3582 if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3583 ff_wmv2_encode_picture_header(s, picture_number);
3584 else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3585 ff_msmpeg4_encode_picture_header(s, picture_number);
3586 else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3587 ff_mpeg4_encode_picture_header(s, picture_number);
3588 else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
3589 ret = ff_rv10_encode_picture_header(s, picture_number);
3593 else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3594 ff_rv20_encode_picture_header(s, picture_number);
3595 else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3596 ff_flv_encode_picture_header(s, picture_number);
3597 else if (CONFIG_H263_ENCODER)
3598 ff_h263_encode_picture_header(s, picture_number);
3601 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3602 ff_mpeg1_encode_picture_header(s, picture_number);
3607 bits= put_bits_count(&s->pb);
3608 s->header_bits= bits - s->last_bits;
3610 for(i=1; i<context_count; i++){
3611 update_duplicate_context_after_me(s->thread_context[i], s);
3613 s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3614 for(i=1; i<context_count; i++){
3615 merge_context_after_encode(s, s->thread_context[i]);
3621 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3622 const int intra= s->mb_intra;
3625 s->dct_count[intra]++;
3627 for(i=0; i<64; i++){
3628 int level= block[i];
3632 s->dct_error_sum[intra][i] += level;
3633 level -= s->dct_offset[intra][i];
3634 if(level<0) level=0;
3636 s->dct_error_sum[intra][i] -= level;
3637 level += s->dct_offset[intra][i];
3638 if(level>0) level=0;
3645 static int dct_quantize_trellis_c(MpegEncContext *s,
3646 int16_t *block, int n,
3647 int qscale, int *overflow){
3649 const uint8_t *scantable= s->intra_scantable.scantable;
3650 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3652 unsigned int threshold1, threshold2;
3664 int coeff_count[64];
3665 int qmul, qadd, start_i, last_non_zero, i, dc;
3666 const int esc_length= s->ac_esc_length;
3668 uint8_t * last_length;
3669 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3671 s->fdsp.fdct(block);
3673 if(s->dct_error_sum)
3674 s->denoise_dct(s, block);
3676 qadd= ((qscale-1)|1)*8;
3687 /* For AIC we skip quant/dequant of INTRADC */
3692 /* note: block[0] is assumed to be positive */
3693 block[0] = (block[0] + (q >> 1)) / q;
3696 qmat = s->q_intra_matrix[qscale];
3697 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3698 bias= 1<<(QMAT_SHIFT-1);
3699 length = s->intra_ac_vlc_length;
3700 last_length= s->intra_ac_vlc_last_length;
3704 qmat = s->q_inter_matrix[qscale];
3705 length = s->inter_ac_vlc_length;
3706 last_length= s->inter_ac_vlc_last_length;
3710 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3711 threshold2= (threshold1<<1);
3713 for(i=63; i>=start_i; i--) {
3714 const int j = scantable[i];
3715 int level = block[j] * qmat[j];
3717 if(((unsigned)(level+threshold1))>threshold2){
3723 for(i=start_i; i<=last_non_zero; i++) {
3724 const int j = scantable[i];
3725 int level = block[j] * qmat[j];
3727 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3728 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3729 if(((unsigned)(level+threshold1))>threshold2){
3731 level= (bias + level)>>QMAT_SHIFT;
3733 coeff[1][i]= level-1;
3734 // coeff[2][k]= level-2;
3736 level= (bias - level)>>QMAT_SHIFT;
3737 coeff[0][i]= -level;
3738 coeff[1][i]= -level+1;
3739 // coeff[2][k]= -level+2;
3741 coeff_count[i]= FFMIN(level, 2);
3742 assert(coeff_count[i]);
3745 coeff[0][i]= (level>>31)|1;
3750 *overflow= s->max_qcoeff < max; //overflow might have happened
3752 if(last_non_zero < start_i){
3753 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3754 return last_non_zero;
3757 score_tab[start_i]= 0;
3758 survivor[0]= start_i;
3761 for(i=start_i; i<=last_non_zero; i++){
3762 int level_index, j, zero_distortion;
3763 int dct_coeff= FFABS(block[ scantable[i] ]);
3764 int best_score=256*256*256*120;
3766 if (s->fdsp.fdct == ff_fdct_ifast)
3767 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3768 zero_distortion= dct_coeff*dct_coeff;
3770 for(level_index=0; level_index < coeff_count[i]; level_index++){
3772 int level= coeff[level_index][i];
3773 const int alevel= FFABS(level);
3778 if(s->out_format == FMT_H263){
3779 unquant_coeff= alevel*qmul + qadd;
3781 j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
3783 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
3784 unquant_coeff = (unquant_coeff - 1) | 1;
3786 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3787 unquant_coeff = (unquant_coeff - 1) | 1;
3792 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3794 if((level&(~127)) == 0){
3795 for(j=survivor_count-1; j>=0; j--){
3796 int run= i - survivor[j];
3797 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3798 score += score_tab[i-run];
3800 if(score < best_score){
3803 level_tab[i+1]= level-64;
3807 if(s->out_format == FMT_H263){
3808 for(j=survivor_count-1; j>=0; j--){
3809 int run= i - survivor[j];
3810 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3811 score += score_tab[i-run];
3812 if(score < last_score){
3815 last_level= level-64;
3821 distortion += esc_length*lambda;
3822 for(j=survivor_count-1; j>=0; j--){
3823 int run= i - survivor[j];
3824 int score= distortion + score_tab[i-run];
3826 if(score < best_score){
3829 level_tab[i+1]= level-64;
3833 if(s->out_format == FMT_H263){
3834 for(j=survivor_count-1; j>=0; j--){
3835 int run= i - survivor[j];
3836 int score= distortion + score_tab[i-run];
3837 if(score < last_score){
3840 last_level= level-64;
3848 score_tab[i+1]= best_score;
3850 // Note: there is a vlc code in MPEG-4 which is 1 bit shorter then another one with a shorter run and the same level
3851 if(last_non_zero <= 27){
3852 for(; survivor_count; survivor_count--){
3853 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3857 for(; survivor_count; survivor_count--){
3858 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3863 survivor[ survivor_count++ ]= i+1;
3866 if(s->out_format != FMT_H263){
3867 last_score= 256*256*256*120;
3868 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3869 int score= score_tab[i];
3871 score += lambda * 2; // FIXME more exact?
3873 if(score < last_score){
3876 last_level= level_tab[i];
3877 last_run= run_tab[i];
3882 s->coded_score[n] = last_score;
3884 dc= FFABS(block[0]);
3885 last_non_zero= last_i - 1;
3886 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3888 if(last_non_zero < start_i)
3889 return last_non_zero;
3891 if(last_non_zero == 0 && start_i == 0){
3893 int best_score= dc * dc;
3895 for(i=0; i<coeff_count[0]; i++){
3896 int level= coeff[i][0];
3897 int alevel= FFABS(level);
3898 int unquant_coeff, score, distortion;
3900 if(s->out_format == FMT_H263){
3901 unquant_coeff= (alevel*qmul + qadd)>>3;
3903 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3904 unquant_coeff = (unquant_coeff - 1) | 1;
3906 unquant_coeff = (unquant_coeff + 4) >> 3;
3907 unquant_coeff<<= 3 + 3;
3909 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3911 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3912 else score= distortion + esc_length*lambda;
3914 if(score < best_score){
3916 best_level= level - 64;
3919 block[0]= best_level;
3920 s->coded_score[n] = best_score - dc*dc;
3921 if(best_level == 0) return -1;
3922 else return last_non_zero;
3928 block[ perm_scantable[last_non_zero] ]= last_level;
3931 for(; i>start_i; i -= run_tab[i] + 1){
3932 block[ perm_scantable[i-1] ]= level_tab[i];
3935 return last_non_zero;
3938 //#define REFINE_STATS 1
3939 static int16_t basis[64][64];
3941 static void build_basis(uint8_t *perm){
3948 double s= 0.25*(1<<BASIS_SHIFT);
3950 int perm_index= perm[index];
3951 if(i==0) s*= sqrt(0.5);
3952 if(j==0) s*= sqrt(0.5);
3953 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)));
3960 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3961 int16_t *block, int16_t *weight, int16_t *orig,
3964 LOCAL_ALIGNED_16(int16_t, d1, [64]);
3965 const uint8_t *scantable= s->intra_scantable.scantable;
3966 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3967 // unsigned int threshold1, threshold2;
3972 int qmul, qadd, start_i, last_non_zero, i, dc;
3974 uint8_t * last_length;
3976 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3979 static int after_last=0;
3980 static int to_zero=0;
3981 static int from_zero=0;
3984 static int messed_sign=0;
3987 if(basis[0][0] == 0)
3988 build_basis(s->idsp.idct_permutation);
3999 /* For AIC we skip quant/dequant of INTRADC */
4003 q <<= RECON_SHIFT-3;
4004 /* note: block[0] is assumed to be positive */
4006 // block[0] = (block[0] + (q >> 1)) / q;
4008 // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
4009 // bias= 1<<(QMAT_SHIFT-1);
4010 length = s->intra_ac_vlc_length;
4011 last_length= s->intra_ac_vlc_last_length;
4015 length = s->inter_ac_vlc_length;
4016 last_length= s->inter_ac_vlc_last_length;
4018 last_non_zero = s->block_last_index[n];
4023 dc += (1<<(RECON_SHIFT-1));
4024 for(i=0; i<64; i++){
4025 rem[i] = dc - (orig[i] << RECON_SHIFT); // FIXME use orig directly instead of copying to rem[]
4028 STOP_TIMER("memset rem[]")}
4031 for(i=0; i<64; i++){
4036 w= FFABS(weight[i]) + qns*one;
4037 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
4040 // w=weight[i] = (63*qns + (w/2)) / w;
4046 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
4052 for(i=start_i; i<=last_non_zero; i++){
4053 int j= perm_scantable[i];
4054 const int level= block[j];
4058 if(level<0) coeff= qmul*level - qadd;
4059 else coeff= qmul*level + qadd;
4060 run_tab[rle_index++]=run;
4063 s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
4069 if(last_non_zero>0){
4070 STOP_TIMER("init rem[]")
4077 int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
4080 int run2, best_unquant_change=0, analyze_gradient;
4084 analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
4086 if(analyze_gradient){
4090 for(i=0; i<64; i++){
4093 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
4096 STOP_TIMER("rem*w*w")}
4106 const int level= block[0];
4107 int change, old_coeff;
4109 assert(s->mb_intra);
4113 for(change=-1; change<=1; change+=2){
4114 int new_level= level + change;
4115 int score, new_coeff;
4117 new_coeff= q*new_level;
4118 if(new_coeff >= 2048 || new_coeff < 0)
4121 score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
4122 new_coeff - old_coeff);
4123 if(score<best_score){
4126 best_change= change;
4127 best_unquant_change= new_coeff - old_coeff;
4134 run2= run_tab[rle_index++];
4138 for(i=start_i; i<64; i++){
4139 int j= perm_scantable[i];
4140 const int level= block[j];
4141 int change, old_coeff;
4143 if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4147 if(level<0) old_coeff= qmul*level - qadd;
4148 else old_coeff= qmul*level + qadd;
4149 run2= run_tab[rle_index++]; //FIXME ! maybe after last
4153 assert(run2>=0 || i >= last_non_zero );
4156 for(change=-1; change<=1; change+=2){
4157 int new_level= level + change;
4158 int score, new_coeff, unquant_change;
4161 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4165 if(new_level<0) new_coeff= qmul*new_level - qadd;
4166 else new_coeff= qmul*new_level + qadd;
4167 if(new_coeff >= 2048 || new_coeff <= -2048)
4169 //FIXME check for overflow
4172 if(level < 63 && level > -63){
4173 if(i < last_non_zero)
4174 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
4175 - length[UNI_AC_ENC_INDEX(run, level+64)];
4177 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4178 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4181 assert(FFABS(new_level)==1);
4183 if(analyze_gradient){
4184 int g= d1[ scantable[i] ];
4185 if(g && (g^new_level) >= 0)
4189 if(i < last_non_zero){
4190 int next_i= i + run2 + 1;
4191 int next_level= block[ perm_scantable[next_i] ] + 64;
4193 if(next_level&(~127))
4196 if(next_i < last_non_zero)
4197 score += length[UNI_AC_ENC_INDEX(run, 65)]
4198 + length[UNI_AC_ENC_INDEX(run2, next_level)]
4199 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4201 score += length[UNI_AC_ENC_INDEX(run, 65)]
4202 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4203 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4205 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4207 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4208 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4214 assert(FFABS(level)==1);
4216 if(i < last_non_zero){
4217 int next_i= i + run2 + 1;
4218 int next_level= block[ perm_scantable[next_i] ] + 64;
4220 if(next_level&(~127))
4223 if(next_i < last_non_zero)
4224 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4225 - length[UNI_AC_ENC_INDEX(run2, next_level)]
4226 - length[UNI_AC_ENC_INDEX(run, 65)];
4228 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4229 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4230 - length[UNI_AC_ENC_INDEX(run, 65)];
4232 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4234 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4235 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4242 unquant_change= new_coeff - old_coeff;
4243 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
4245 score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4247 if(score<best_score){
4250 best_change= change;
4251 best_unquant_change= unquant_change;
4255 prev_level= level + 64;
4256 if(prev_level&(~127))
4265 STOP_TIMER("iterative step")}
4269 int j= perm_scantable[ best_coeff ];
4271 block[j] += best_change;
4273 if(best_coeff > last_non_zero){
4274 last_non_zero= best_coeff;
4282 if(block[j] - best_change){
4283 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
4295 for(; last_non_zero>=start_i; last_non_zero--){
4296 if(block[perm_scantable[last_non_zero]])
4302 if(256*256*256*64 % count == 0){
4303 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);
4308 for(i=start_i; i<=last_non_zero; i++){
4309 int j= perm_scantable[i];
4310 const int level= block[j];
4313 run_tab[rle_index++]=run;
4320 s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4326 if(last_non_zero>0){
4327 STOP_TIMER("iterative search")
4332 return last_non_zero;
4336 * Permute an 8x8 block according to permutation.
4337 * @param block the block which will be permuted according to
4338 * the given permutation vector
4339 * @param permutation the permutation vector
4340 * @param last the last non zero coefficient in scantable order, used to
4341 * speed the permutation up
4342 * @param scantable the used scantable, this is only used to speed the
4343 * permutation up, the block is not (inverse) permutated
4344 * to scantable order!
4346 static void block_permute(int16_t *block, uint8_t *permutation,
4347 const uint8_t *scantable, int last)
4354 //FIXME it is ok but not clean and might fail for some permutations
4355 // if (permutation[1] == 1)
4358 for (i = 0; i <= last; i++) {
4359 const int j = scantable[i];
4364 for (i = 0; i <= last; i++) {
4365 const int j = scantable[i];
4366 const int perm_j = permutation[j];
4367 block[perm_j] = temp[j];
4371 int ff_dct_quantize_c(MpegEncContext *s,
4372 int16_t *block, int n,
4373 int qscale, int *overflow)
4375 int i, j, level, last_non_zero, q, start_i;
4377 const uint8_t *scantable= s->intra_scantable.scantable;
4380 unsigned int threshold1, threshold2;
4382 s->fdsp.fdct(block);
4384 if(s->dct_error_sum)
4385 s->denoise_dct(s, block);
4395 /* For AIC we skip quant/dequant of INTRADC */
4398 /* note: block[0] is assumed to be positive */
4399 block[0] = (block[0] + (q >> 1)) / q;
4402 qmat = s->q_intra_matrix[qscale];
4403 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4407 qmat = s->q_inter_matrix[qscale];
4408 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4410 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4411 threshold2= (threshold1<<1);
4412 for(i=63;i>=start_i;i--) {
4414 level = block[j] * qmat[j];
4416 if(((unsigned)(level+threshold1))>threshold2){
4423 for(i=start_i; i<=last_non_zero; i++) {
4425 level = block[j] * qmat[j];
4427 // if( bias+level >= (1<<QMAT_SHIFT)
4428 // || bias-level >= (1<<QMAT_SHIFT)){
4429 if(((unsigned)(level+threshold1))>threshold2){
4431 level= (bias + level)>>QMAT_SHIFT;
4434 level= (bias - level)>>QMAT_SHIFT;
4442 *overflow= s->max_qcoeff < max; //overflow might have happened
4444 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4445 if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
4446 block_permute(block, s->idsp.idct_permutation,
4447 scantable, last_non_zero);
4449 return last_non_zero;
4452 #define OFFSET(x) offsetof(MpegEncContext, x)
4453 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4454 static const AVOption h263_options[] = {
4455 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4456 { "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},
4457 { "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 },
4462 static const AVClass h263_class = {
4463 .class_name = "H.263 encoder",
4464 .item_name = av_default_item_name,
4465 .option = h263_options,
4466 .version = LIBAVUTIL_VERSION_INT,
4469 AVCodec ff_h263_encoder = {
4471 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4472 .type = AVMEDIA_TYPE_VIDEO,
4473 .id = AV_CODEC_ID_H263,
4474 .priv_data_size = sizeof(MpegEncContext),
4475 .init = ff_mpv_encode_init,
4476 .encode2 = ff_mpv_encode_picture,
4477 .close = ff_mpv_encode_end,
4478 .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4479 .priv_class = &h263_class,
4482 static const AVOption h263p_options[] = {
4483 { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4484 { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4485 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4486 { "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},
4490 static const AVClass h263p_class = {
4491 .class_name = "H.263p encoder",
4492 .item_name = av_default_item_name,
4493 .option = h263p_options,
4494 .version = LIBAVUTIL_VERSION_INT,
4497 AVCodec ff_h263p_encoder = {
4499 .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4500 .type = AVMEDIA_TYPE_VIDEO,
4501 .id = AV_CODEC_ID_H263P,
4502 .priv_data_size = sizeof(MpegEncContext),
4503 .init = ff_mpv_encode_init,
4504 .encode2 = ff_mpv_encode_picture,
4505 .close = ff_mpv_encode_end,
4506 .capabilities = AV_CODEC_CAP_SLICE_THREADS,
4507 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4508 .priv_class = &h263p_class,
4511 static const AVClass msmpeg4v2_class = {
4512 .class_name = "msmpeg4v2 encoder",
4513 .item_name = av_default_item_name,
4514 .option = ff_mpv_generic_options,
4515 .version = LIBAVUTIL_VERSION_INT,
4518 AVCodec ff_msmpeg4v2_encoder = {
4519 .name = "msmpeg4v2",
4520 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
4521 .type = AVMEDIA_TYPE_VIDEO,
4522 .id = AV_CODEC_ID_MSMPEG4V2,
4523 .priv_data_size = sizeof(MpegEncContext),
4524 .init = ff_mpv_encode_init,
4525 .encode2 = ff_mpv_encode_picture,
4526 .close = ff_mpv_encode_end,
4527 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4528 .priv_class = &msmpeg4v2_class,
4531 static const AVClass msmpeg4v3_class = {
4532 .class_name = "msmpeg4v3 encoder",
4533 .item_name = av_default_item_name,
4534 .option = ff_mpv_generic_options,
4535 .version = LIBAVUTIL_VERSION_INT,
4538 AVCodec ff_msmpeg4v3_encoder = {
4540 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
4541 .type = AVMEDIA_TYPE_VIDEO,
4542 .id = AV_CODEC_ID_MSMPEG4V3,
4543 .priv_data_size = sizeof(MpegEncContext),
4544 .init = ff_mpv_encode_init,
4545 .encode2 = ff_mpv_encode_picture,
4546 .close = ff_mpv_encode_end,
4547 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4548 .priv_class = &msmpeg4v3_class,
4551 static const AVClass wmv1_class = {
4552 .class_name = "wmv1 encoder",
4553 .item_name = av_default_item_name,
4554 .option = ff_mpv_generic_options,
4555 .version = LIBAVUTIL_VERSION_INT,
4558 AVCodec ff_wmv1_encoder = {
4560 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
4561 .type = AVMEDIA_TYPE_VIDEO,
4562 .id = AV_CODEC_ID_WMV1,
4563 .priv_data_size = sizeof(MpegEncContext),
4564 .init = ff_mpv_encode_init,
4565 .encode2 = ff_mpv_encode_picture,
4566 .close = ff_mpv_encode_end,
4567 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4568 .priv_class = &wmv1_class,