2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * The simplest mpeg encoder (well, it was the simplest!).
30 #include "libavutil/intmath.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/opt.h"
36 #include "mpegvideo.h"
43 #include "aandcttab.h"
45 #include "mpeg4video.h"
47 #include "bytestream.h"
54 static int encode_picture(MpegEncContext *s, int picture_number);
55 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
56 static int sse_mb(MpegEncContext *s);
57 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
58 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
62 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
63 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
65 const AVOption ff_mpv_generic_options[] = {
70 void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
71 uint16_t (*qmat16)[2][64],
72 const uint16_t *quant_matrix,
73 int bias, int qmin, int qmax, int intra)
78 for (qscale = qmin; qscale <= qmax; qscale++) {
80 if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
81 dsp->fdct == ff_jpeg_fdct_islow_10 ||
82 dsp->fdct == ff_faandct) {
83 for (i = 0; i < 64; i++) {
84 const int j = dsp->idct_permutation[i];
85 /* 16 <= qscale * quant_matrix[i] <= 7905
86 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
87 * 19952 <= x <= 249205026
88 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
89 * 3444240 >= (1 << 36) / (x) >= 275 */
91 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
92 (qscale * quant_matrix[j]));
94 } else if (dsp->fdct == ff_fdct_ifast) {
95 for (i = 0; i < 64; i++) {
96 const int j = dsp->idct_permutation[i];
97 /* 16 <= qscale * quant_matrix[i] <= 7905
98 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
99 * 19952 <= x <= 249205026
100 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
101 * 3444240 >= (1 << 36) / (x) >= 275 */
103 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
104 (ff_aanscales[i] * (int64_t)qscale * quant_matrix[j]));
107 for (i = 0; i < 64; i++) {
108 const int j = dsp->idct_permutation[i];
109 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
110 * Assume x = qscale * quant_matrix[i]
112 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
113 * so 32768 >= (1 << 19) / (x) >= 67 */
114 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
115 (qscale * quant_matrix[j]));
116 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
117 // (qscale * quant_matrix[i]);
118 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
119 (qscale * quant_matrix[j]);
121 if (qmat16[qscale][0][i] == 0 ||
122 qmat16[qscale][0][i] == 128 * 256)
123 qmat16[qscale][0][i] = 128 * 256 - 1;
124 qmat16[qscale][1][i] =
125 ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
126 qmat16[qscale][0][i]);
130 for (i = intra; i < 64; i++) {
132 if (dsp->fdct == ff_fdct_ifast) {
133 max = (8191LL * ff_aanscales[i]) >> 14;
135 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
141 av_log(NULL, AV_LOG_INFO,
142 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
147 static inline void update_qscale(MpegEncContext *s)
149 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
150 (FF_LAMBDA_SHIFT + 7);
151 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
153 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
157 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
163 for (i = 0; i < 64; i++) {
164 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
171 * init s->current_picture.qscale_table from s->lambda_table
173 void ff_init_qscale_tab(MpegEncContext *s)
175 int8_t * const qscale_table = s->current_picture.f.qscale_table;
178 for (i = 0; i < s->mb_num; i++) {
179 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
180 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
181 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
186 static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst,
191 dst->pict_type = src->pict_type;
192 dst->quality = src->quality;
193 dst->coded_picture_number = src->coded_picture_number;
194 dst->display_picture_number = src->display_picture_number;
195 //dst->reference = src->reference;
197 dst->interlaced_frame = src->interlaced_frame;
198 dst->top_field_first = src->top_field_first;
200 if (s->avctx->me_threshold) {
201 if (!src->motion_val[0])
202 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
204 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
205 if (!src->ref_index[0])
206 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
207 if (src->motion_subsample_log2 != dst->motion_subsample_log2)
208 av_log(s->avctx, AV_LOG_ERROR,
209 "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
210 src->motion_subsample_log2, dst->motion_subsample_log2);
212 memcpy(dst->mb_type, src->mb_type,
213 s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
215 for (i = 0; i < 2; i++) {
216 int stride = ((16 * s->mb_width ) >>
217 src->motion_subsample_log2) + 1;
218 int height = ((16 * s->mb_height) >> src->motion_subsample_log2);
220 if (src->motion_val[i] &&
221 src->motion_val[i] != dst->motion_val[i]) {
222 memcpy(dst->motion_val[i], src->motion_val[i],
223 2 * stride * height * sizeof(int16_t));
225 if (src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]) {
226 memcpy(dst->ref_index[i], src->ref_index[i],
227 s->mb_stride * 4 * s->mb_height * sizeof(int8_t));
233 static void update_duplicate_context_after_me(MpegEncContext *dst,
236 #define COPY(a) dst->a= src->a
238 COPY(current_picture);
244 COPY(picture_in_gop_number);
245 COPY(gop_picture_number);
246 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
247 COPY(progressive_frame); // FIXME don't set in encode_header
248 COPY(partitioned_frame); // FIXME don't set in encode_header
253 * Set the given MpegEncContext to defaults for encoding.
254 * the changed fields will not depend upon the prior state of the MpegEncContext.
256 static void MPV_encode_defaults(MpegEncContext *s)
259 ff_MPV_common_defaults(s);
261 for (i = -16; i < 16; i++) {
262 default_fcode_tab[i + MAX_MV] = 1;
264 s->me.mv_penalty = default_mv_penalty;
265 s->fcode_tab = default_fcode_tab;
268 av_cold int ff_dct_encode_init(MpegEncContext *s) {
270 ff_dct_encode_init_x86(s);
272 if (!s->dct_quantize)
273 s->dct_quantize = ff_dct_quantize_c;
275 s->denoise_dct = denoise_dct_c;
276 s->fast_dct_quantize = s->dct_quantize;
277 if (s->avctx->trellis)
278 s->dct_quantize = dct_quantize_trellis_c;
283 /* init video encoder */
284 av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
286 MpegEncContext *s = avctx->priv_data;
288 int chroma_h_shift, chroma_v_shift;
290 MPV_encode_defaults(s);
292 switch (avctx->codec_id) {
293 case AV_CODEC_ID_MPEG2VIDEO:
294 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
295 avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
296 av_log(avctx, AV_LOG_ERROR,
297 "only YUV420 and YUV422 are supported\n");
301 case AV_CODEC_ID_LJPEG:
302 if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
303 avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
304 avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
305 avctx->pix_fmt != AV_PIX_FMT_BGR0 &&
306 avctx->pix_fmt != AV_PIX_FMT_BGRA &&
307 avctx->pix_fmt != AV_PIX_FMT_BGR24 &&
308 ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
309 avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
310 avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
311 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
312 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
316 case AV_CODEC_ID_MJPEG:
317 case AV_CODEC_ID_AMV:
318 if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
319 avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
320 avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
321 ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
322 avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
323 avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
324 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
325 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
330 if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
331 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
336 switch (avctx->pix_fmt) {
337 case AV_PIX_FMT_YUVJ444P:
338 case AV_PIX_FMT_YUV444P:
339 s->chroma_format = CHROMA_444;
341 case AV_PIX_FMT_YUVJ422P:
342 case AV_PIX_FMT_YUV422P:
343 s->chroma_format = CHROMA_422;
345 case AV_PIX_FMT_YUVJ420P:
346 case AV_PIX_FMT_YUV420P:
348 s->chroma_format = CHROMA_420;
352 s->bit_rate = avctx->bit_rate;
353 s->width = avctx->width;
354 s->height = avctx->height;
355 if (avctx->gop_size > 600 &&
356 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
357 av_log(avctx, AV_LOG_WARNING,
358 "keyframe interval too large!, reducing it from %d to %d\n",
359 avctx->gop_size, 600);
360 avctx->gop_size = 600;
362 s->gop_size = avctx->gop_size;
364 s->flags = avctx->flags;
365 s->flags2 = avctx->flags2;
366 s->max_b_frames = avctx->max_b_frames;
367 s->codec_id = avctx->codec->id;
368 #if FF_API_MPV_GLOBAL_OPTS
369 if (avctx->luma_elim_threshold)
370 s->luma_elim_threshold = avctx->luma_elim_threshold;
371 if (avctx->chroma_elim_threshold)
372 s->chroma_elim_threshold = avctx->chroma_elim_threshold;
374 s->strict_std_compliance = avctx->strict_std_compliance;
375 s->quarter_sample = (avctx->flags & CODEC_FLAG_QPEL) != 0;
376 s->mpeg_quant = avctx->mpeg_quant;
377 s->rtp_mode = !!avctx->rtp_payload_size;
378 s->intra_dc_precision = avctx->intra_dc_precision;
379 s->user_specified_pts = AV_NOPTS_VALUE;
381 if (s->gop_size <= 1) {
388 s->me_method = avctx->me_method;
391 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
393 #if FF_API_MPV_GLOBAL_OPTS
394 if (s->flags & CODEC_FLAG_QP_RD)
395 s->mpv_flags |= FF_MPV_FLAG_QP_RD;
398 s->adaptive_quant = (s->avctx->lumi_masking ||
399 s->avctx->dark_masking ||
400 s->avctx->temporal_cplx_masking ||
401 s->avctx->spatial_cplx_masking ||
402 s->avctx->p_masking ||
403 s->avctx->border_masking ||
404 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
407 s->loop_filter = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
409 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
410 switch(avctx->codec_id) {
411 case AV_CODEC_ID_MPEG1VIDEO:
412 case AV_CODEC_ID_MPEG2VIDEO:
413 avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112L / 15000000 * 16384;
415 case AV_CODEC_ID_MPEG4:
416 case AV_CODEC_ID_MSMPEG4V1:
417 case AV_CODEC_ID_MSMPEG4V2:
418 case AV_CODEC_ID_MSMPEG4V3:
419 if (avctx->rc_max_rate >= 15000000) {
420 avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000L) * (760-320) / (38400000 - 15000000);
421 } else if(avctx->rc_max_rate >= 2000000) {
422 avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000L) * (320- 80) / (15000000 - 2000000);
423 } else if(avctx->rc_max_rate >= 384000) {
424 avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000L) * ( 80- 40) / ( 2000000 - 384000);
426 avctx->rc_buffer_size = 40;
427 avctx->rc_buffer_size *= 16384;
430 if (avctx->rc_buffer_size) {
431 av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
435 if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
436 av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
437 if (avctx->rc_max_rate && !avctx->rc_buffer_size)
441 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
442 av_log(avctx, AV_LOG_INFO,
443 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
446 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
447 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
451 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
452 av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
456 if (avctx->rc_max_rate &&
457 avctx->rc_max_rate == avctx->bit_rate &&
458 avctx->rc_max_rate != avctx->rc_min_rate) {
459 av_log(avctx, AV_LOG_INFO,
460 "impossible bitrate constraints, this will fail\n");
463 if (avctx->rc_buffer_size &&
464 avctx->bit_rate * (int64_t)avctx->time_base.num >
465 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
466 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
470 if (!s->fixed_qscale &&
471 avctx->bit_rate * av_q2d(avctx->time_base) >
472 avctx->bit_rate_tolerance) {
473 av_log(avctx, AV_LOG_ERROR,
474 "bitrate tolerance too small for bitrate\n");
478 if (s->avctx->rc_max_rate &&
479 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
480 (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
481 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
482 90000LL * (avctx->rc_buffer_size - 1) >
483 s->avctx->rc_max_rate * 0xFFFFLL) {
484 av_log(avctx, AV_LOG_INFO,
485 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
486 "specified vbv buffer is too large for the given bitrate!\n");
489 if ((s->flags & CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
490 s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
491 s->codec_id != AV_CODEC_ID_FLV1) {
492 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
496 if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
497 av_log(avctx, AV_LOG_ERROR,
498 "OBMC is only supported with simple mb decision\n");
502 if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
503 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
507 if (s->max_b_frames &&
508 s->codec_id != AV_CODEC_ID_MPEG4 &&
509 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
510 s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
511 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
515 if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
516 s->codec_id == AV_CODEC_ID_H263 ||
517 s->codec_id == AV_CODEC_ID_H263P) &&
518 (avctx->sample_aspect_ratio.num > 255 ||
519 avctx->sample_aspect_ratio.den > 255)) {
520 av_log(avctx, AV_LOG_WARNING,
521 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
522 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
523 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
524 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
527 if ((s->codec_id == AV_CODEC_ID_H263 ||
528 s->codec_id == AV_CODEC_ID_H263P) &&
529 (avctx->width > 2048 ||
530 avctx->height > 1152 )) {
531 av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
534 if ((s->codec_id == AV_CODEC_ID_H263 ||
535 s->codec_id == AV_CODEC_ID_H263P) &&
536 ((avctx->width &3) ||
537 (avctx->height&3) )) {
538 av_log(avctx, AV_LOG_ERROR, "w/h must be a multiple of 4\n");
542 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO &&
543 (avctx->width > 4095 ||
544 avctx->height > 4095 )) {
545 av_log(avctx, AV_LOG_ERROR, "MPEG-1 does not support resolutions above 4095x4095\n");
549 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
550 (avctx->width > 16383 ||
551 avctx->height > 16383 )) {
552 av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support resolutions above 16383x16383\n");
556 if (s->codec_id == AV_CODEC_ID_RV10 &&
558 avctx->height&15 )) {
559 av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
560 return AVERROR(EINVAL);
563 if (s->codec_id == AV_CODEC_ID_RV20 &&
566 av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
567 return AVERROR(EINVAL);
570 if ((s->codec_id == AV_CODEC_ID_WMV1 ||
571 s->codec_id == AV_CODEC_ID_WMV2) &&
573 av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
577 if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
578 s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
579 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
583 // FIXME mpeg2 uses that too
584 if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
585 av_log(avctx, AV_LOG_ERROR,
586 "mpeg2 style quantization not supported by codec\n");
590 #if FF_API_MPV_GLOBAL_OPTS
591 if (s->flags & CODEC_FLAG_CBP_RD)
592 s->mpv_flags |= FF_MPV_FLAG_CBP_RD;
595 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
596 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
600 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
601 s->avctx->mb_decision != FF_MB_DECISION_RD) {
602 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
606 if (s->avctx->scenechange_threshold < 1000000000 &&
607 (s->flags & CODEC_FLAG_CLOSED_GOP)) {
608 av_log(avctx, AV_LOG_ERROR,
609 "closed gop with scene change detection are not supported yet, "
610 "set threshold to 1000000000\n");
614 if (s->flags & CODEC_FLAG_LOW_DELAY) {
615 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
616 av_log(avctx, AV_LOG_ERROR,
617 "low delay forcing is only available for mpeg2\n");
620 if (s->max_b_frames != 0) {
621 av_log(avctx, AV_LOG_ERROR,
622 "b frames cannot be used with low delay\n");
627 if (s->q_scale_type == 1) {
628 if (avctx->qmax > 12) {
629 av_log(avctx, AV_LOG_ERROR,
630 "non linear quant only supports qmax <= 12 currently\n");
635 if (s->avctx->thread_count > 1 &&
636 s->codec_id != AV_CODEC_ID_MPEG4 &&
637 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
638 s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
639 s->codec_id != AV_CODEC_ID_MJPEG &&
640 (s->codec_id != AV_CODEC_ID_H263P)) {
641 av_log(avctx, AV_LOG_ERROR,
642 "multi threaded encoding not supported by codec\n");
646 if (s->avctx->thread_count < 1) {
647 av_log(avctx, AV_LOG_ERROR,
648 "automatic thread number detection not supported by codec, "
653 if (s->avctx->slices > 1 || s->avctx->thread_count > 1)
656 if (s->avctx->thread_count > 1 && s->codec_id == AV_CODEC_ID_H263P)
657 s->h263_slice_structured = 1;
659 if (!avctx->time_base.den || !avctx->time_base.num) {
660 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
664 i = (INT_MAX / 2 + 128) >> 8;
665 if (avctx->me_threshold >= i) {
666 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n",
670 if (avctx->mb_threshold >= i) {
671 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
676 if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
677 av_log(avctx, AV_LOG_INFO,
678 "notice: b_frame_strategy only affects the first pass\n");
679 avctx->b_frame_strategy = 0;
682 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
684 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
685 avctx->time_base.den /= i;
686 avctx->time_base.num /= i;
690 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG || s->codec_id==AV_CODEC_ID_AMV) {
691 // (a + x * 3 / 8) / x
692 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
693 s->inter_quant_bias = 0;
695 s->intra_quant_bias = 0;
697 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
700 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
701 s->intra_quant_bias = avctx->intra_quant_bias;
702 if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
703 s->inter_quant_bias = avctx->inter_quant_bias;
705 av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
707 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
709 if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
710 s->avctx->time_base.den > (1 << 16) - 1) {
711 av_log(avctx, AV_LOG_ERROR,
712 "timebase %d/%d not supported by MPEG 4 standard, "
713 "the maximum admitted value for the timebase denominator "
714 "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
718 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
720 #if FF_API_MPV_GLOBAL_OPTS
721 if (avctx->flags2 & CODEC_FLAG2_SKIP_RD)
722 s->mpv_flags |= FF_MPV_FLAG_SKIP_RD;
723 if (avctx->flags2 & CODEC_FLAG2_STRICT_GOP)
724 s->mpv_flags |= FF_MPV_FLAG_STRICT_GOP;
725 if (avctx->quantizer_noise_shaping)
726 s->quantizer_noise_shaping = avctx->quantizer_noise_shaping;
729 switch (avctx->codec->id) {
730 case AV_CODEC_ID_MPEG1VIDEO:
731 s->out_format = FMT_MPEG1;
732 s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
733 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
735 case AV_CODEC_ID_MPEG2VIDEO:
736 s->out_format = FMT_MPEG1;
737 s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
738 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
741 case AV_CODEC_ID_LJPEG:
742 case AV_CODEC_ID_MJPEG:
743 case AV_CODEC_ID_AMV:
744 s->out_format = FMT_MJPEG;
745 s->intra_only = 1; /* force intra only for jpeg */
746 if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
747 (avctx->pix_fmt == AV_PIX_FMT_BGR0
748 || s->avctx->pix_fmt == AV_PIX_FMT_BGRA
749 || s->avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
750 s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
751 s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
752 s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
753 } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
754 s->mjpeg_vsample[0] = s->mjpeg_vsample[1] = s->mjpeg_vsample[2] = 2;
755 s->mjpeg_hsample[0] = s->mjpeg_hsample[1] = s->mjpeg_hsample[2] = 1;
757 s->mjpeg_vsample[0] = 2;
758 s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
759 s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
760 s->mjpeg_hsample[0] = 2;
761 s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
762 s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
764 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
765 ff_mjpeg_encode_init(s) < 0)
770 case AV_CODEC_ID_H261:
771 if (!CONFIG_H261_ENCODER)
773 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
774 av_log(avctx, AV_LOG_ERROR,
775 "The specified picture size of %dx%d is not valid for the "
776 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
777 s->width, s->height);
780 s->out_format = FMT_H261;
784 case AV_CODEC_ID_H263:
785 if (!CONFIG_H263_ENCODER)
787 if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
788 s->width, s->height) == 8) {
789 av_log(avctx, AV_LOG_ERROR,
790 "The specified picture size of %dx%d is not valid for "
791 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
792 "352x288, 704x576, and 1408x1152. "
793 "Try H.263+.\n", s->width, s->height);
796 s->out_format = FMT_H263;
800 case AV_CODEC_ID_H263P:
801 s->out_format = FMT_H263;
804 s->h263_aic = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
805 s->modified_quant = s->h263_aic;
806 s->loop_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
807 s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
810 /* These are just to be sure */
814 case AV_CODEC_ID_FLV1:
815 s->out_format = FMT_H263;
816 s->h263_flv = 2; /* format = 1; 11-bit codes */
817 s->unrestricted_mv = 1;
818 s->rtp_mode = 0; /* don't allow GOB */
822 case AV_CODEC_ID_RV10:
823 s->out_format = FMT_H263;
827 case AV_CODEC_ID_RV20:
828 s->out_format = FMT_H263;
831 s->modified_quant = 1;
835 s->unrestricted_mv = 0;
837 case AV_CODEC_ID_MPEG4:
838 s->out_format = FMT_H263;
840 s->unrestricted_mv = 1;
841 s->low_delay = s->max_b_frames ? 0 : 1;
842 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
844 case AV_CODEC_ID_MSMPEG4V2:
845 s->out_format = FMT_H263;
847 s->unrestricted_mv = 1;
848 s->msmpeg4_version = 2;
852 case AV_CODEC_ID_MSMPEG4V3:
853 s->out_format = FMT_H263;
855 s->unrestricted_mv = 1;
856 s->msmpeg4_version = 3;
857 s->flipflop_rounding = 1;
861 case AV_CODEC_ID_WMV1:
862 s->out_format = FMT_H263;
864 s->unrestricted_mv = 1;
865 s->msmpeg4_version = 4;
866 s->flipflop_rounding = 1;
870 case AV_CODEC_ID_WMV2:
871 s->out_format = FMT_H263;
873 s->unrestricted_mv = 1;
874 s->msmpeg4_version = 5;
875 s->flipflop_rounding = 1;
883 avctx->has_b_frames = !s->low_delay;
887 s->progressive_frame =
888 s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
889 CODEC_FLAG_INTERLACED_ME) ||
893 if (ff_MPV_common_init(s) < 0)
896 ff_dct_encode_init(s);
898 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
899 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
901 s->quant_precision = 5;
903 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
904 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
906 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
907 ff_h261_encode_init(s);
908 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
909 ff_h263_encode_init(s);
910 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
911 ff_msmpeg4_encode_init(s);
912 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
913 && s->out_format == FMT_MPEG1)
914 ff_mpeg1_encode_init(s);
917 for (i = 0; i < 64; i++) {
918 int j = s->dsp.idct_permutation[i];
919 if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
921 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
922 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
923 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
925 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
928 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
929 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
931 if (s->avctx->intra_matrix)
932 s->intra_matrix[j] = s->avctx->intra_matrix[i];
933 if (s->avctx->inter_matrix)
934 s->inter_matrix[j] = s->avctx->inter_matrix[i];
937 /* precompute matrix */
938 /* for mjpeg, we do include qscale in the matrix */
939 if (s->out_format != FMT_MJPEG) {
940 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
941 s->intra_matrix, s->intra_quant_bias, avctx->qmin,
943 ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
944 s->inter_matrix, s->inter_quant_bias, avctx->qmin,
948 if (ff_rate_control_init(s) < 0)
954 av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
956 MpegEncContext *s = avctx->priv_data;
958 ff_rate_control_uninit(s);
960 ff_MPV_common_end(s);
961 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
962 s->out_format == FMT_MJPEG)
963 ff_mjpeg_encode_close(s);
965 av_freep(&avctx->extradata);
970 static int get_sae(uint8_t *src, int ref, int stride)
975 for (y = 0; y < 16; y++) {
976 for (x = 0; x < 16; x++) {
977 acc += FFABS(src[x + y * stride] - ref);
984 static int get_intra_count(MpegEncContext *s, uint8_t *src,
985 uint8_t *ref, int stride)
993 for (y = 0; y < h; y += 16) {
994 for (x = 0; x < w; x += 16) {
995 int offset = x + y * stride;
996 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
998 int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
999 int sae = get_sae(src + offset, mean, stride);
1001 acc += sae + 500 < sad;
1008 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
1010 AVFrame *pic = NULL;
1012 int i, display_picture_number = 0;
1013 const int encoding_delay = s->max_b_frames ? s->max_b_frames :
1014 (s->low_delay ? 0 : 1);
1019 display_picture_number = s->input_picture_number++;
1021 if (pts != AV_NOPTS_VALUE) {
1022 if (s->user_specified_pts != AV_NOPTS_VALUE) {
1024 int64_t last = s->user_specified_pts;
1027 av_log(s->avctx, AV_LOG_ERROR,
1028 "Error, Invalid timestamp=%"PRId64", "
1029 "last=%"PRId64"\n", pts, s->user_specified_pts);
1033 if (!s->low_delay && display_picture_number == 1)
1034 s->dts_delta = time - last;
1036 s->user_specified_pts = pts;
1038 if (s->user_specified_pts != AV_NOPTS_VALUE) {
1039 s->user_specified_pts =
1040 pts = s->user_specified_pts + 1;
1041 av_log(s->avctx, AV_LOG_INFO,
1042 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1045 pts = display_picture_number;
1051 if (encoding_delay && !(s->flags & CODEC_FLAG_INPUT_PRESERVED))
1053 if (pic_arg->linesize[0] != s->linesize)
1055 if (pic_arg->linesize[1] != s->uvlinesize)
1057 if (pic_arg->linesize[2] != s->uvlinesize)
1060 av_dlog(s->avctx, "%d %d %d %d\n", pic_arg->linesize[0],
1061 pic_arg->linesize[1], s->linesize, s->uvlinesize);
1064 i = ff_find_unused_picture(s, 1);
1068 pic = &s->picture[i].f;
1071 for (i = 0; i < 4; i++) {
1072 pic->data[i] = pic_arg->data[i];
1073 pic->linesize[i] = pic_arg->linesize[i];
1075 if (ff_alloc_picture(s, (Picture *) pic, 1) < 0) {
1079 i = ff_find_unused_picture(s, 0);
1083 pic = &s->picture[i].f;
1086 if (ff_alloc_picture(s, (Picture *) pic, 0) < 0) {
1090 if (pic->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1091 pic->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1092 pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1095 int h_chroma_shift, v_chroma_shift;
1096 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1100 for (i = 0; i < 3; i++) {
1101 int src_stride = pic_arg->linesize[i];
1102 int dst_stride = i ? s->uvlinesize : s->linesize;
1103 int h_shift = i ? h_chroma_shift : 0;
1104 int v_shift = i ? v_chroma_shift : 0;
1105 int w = s->width >> h_shift;
1106 int h = s->height >> v_shift;
1107 uint8_t *src = pic_arg->data[i];
1108 uint8_t *dst = pic->data[i];
1110 if (s->codec_id == AV_CODEC_ID_AMV && !(s->avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1111 h = ((s->height + 15)/16*16) >> v_shift;
1114 if (!s->avctx->rc_buffer_size)
1115 dst += INPLACE_OFFSET;
1117 if (src_stride == dst_stride)
1118 memcpy(dst, src, src_stride * h);
1121 memcpy(dst, src, w);
1129 copy_picture_attributes(s, pic, pic_arg);
1130 pic->display_picture_number = display_picture_number;
1131 pic->pts = pts; // we set this here to avoid modifiying pic_arg
1134 /* shift buffer entries */
1135 for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1136 s->input_picture[i - 1] = s->input_picture[i];
1138 s->input_picture[encoding_delay] = (Picture*) pic;
1143 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1147 int64_t score64 = 0;
1149 for (plane = 0; plane < 3; plane++) {
1150 const int stride = p->f.linesize[plane];
1151 const int bw = plane ? 1 : 2;
1152 for (y = 0; y < s->mb_height * bw; y++) {
1153 for (x = 0; x < s->mb_width * bw; x++) {
1154 int off = p->f.type == FF_BUFFER_TYPE_SHARED ? 0 : 16;
1155 uint8_t *dptr = p->f.data[plane] + 8 * (x + y * stride) + off;
1156 uint8_t *rptr = ref->f.data[plane] + 8 * (x + y * stride);
1157 int v = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1159 switch (s->avctx->frame_skip_exp) {
1160 case 0: score = FFMAX(score, v); break;
1161 case 1: score += FFABS(v); break;
1162 case 2: score += v * v; break;
1163 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1164 case 4: score64 += v * v * (int64_t)(v * v); break;
1173 if (score64 < s->avctx->frame_skip_threshold)
1175 if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
1180 static int encode_frame(AVCodecContext *c, AVFrame *frame)
1182 AVPacket pkt = { 0 };
1183 int ret, got_output;
1185 av_init_packet(&pkt);
1186 ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
1191 av_free_packet(&pkt);
1195 static int estimate_best_b_count(MpegEncContext *s)
1197 AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1198 AVCodecContext *c = avcodec_alloc_context3(NULL);
1199 AVFrame input[FF_MAX_B_FRAMES + 2];
1200 const int scale = s->avctx->brd_scale;
1201 int i, j, out_size, p_lambda, b_lambda, lambda2;
1202 int64_t best_rd = INT64_MAX;
1203 int best_b_count = -1;
1205 av_assert0(scale >= 0 && scale <= 3);
1208 //s->next_picture_ptr->quality;
1209 p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1210 //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1211 b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1212 if (!b_lambda) // FIXME we should do this somewhere else
1213 b_lambda = p_lambda;
1214 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1217 c->width = s->width >> scale;
1218 c->height = s->height >> scale;
1219 c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |
1220 CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
1221 c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;
1222 c->mb_decision = s->avctx->mb_decision;
1223 c->me_cmp = s->avctx->me_cmp;
1224 c->mb_cmp = s->avctx->mb_cmp;
1225 c->me_sub_cmp = s->avctx->me_sub_cmp;
1226 c->pix_fmt = AV_PIX_FMT_YUV420P;
1227 c->time_base = s->avctx->time_base;
1228 c->max_b_frames = s->max_b_frames;
1230 if (avcodec_open2(c, codec, NULL) < 0)
1233 for (i = 0; i < s->max_b_frames + 2; i++) {
1234 int ysize = c->width * c->height;
1235 int csize = (c->width / 2) * (c->height / 2);
1236 Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1237 s->next_picture_ptr;
1239 avcodec_get_frame_defaults(&input[i]);
1240 input[i].data[0] = av_malloc(ysize + 2 * csize);
1241 input[i].data[1] = input[i].data[0] + ysize;
1242 input[i].data[2] = input[i].data[1] + csize;
1243 input[i].linesize[0] = c->width;
1244 input[i].linesize[1] =
1245 input[i].linesize[2] = c->width / 2;
1247 if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1248 pre_input = *pre_input_ptr;
1250 if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {
1251 pre_input.f.data[0] += INPLACE_OFFSET;
1252 pre_input.f.data[1] += INPLACE_OFFSET;
1253 pre_input.f.data[2] += INPLACE_OFFSET;
1256 s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
1257 pre_input.f.data[0], pre_input.f.linesize[0],
1258 c->width, c->height);
1259 s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
1260 pre_input.f.data[1], pre_input.f.linesize[1],
1261 c->width >> 1, c->height >> 1);
1262 s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
1263 pre_input.f.data[2], pre_input.f.linesize[2],
1264 c->width >> 1, c->height >> 1);
1268 for (j = 0; j < s->max_b_frames + 1; j++) {
1271 if (!s->input_picture[j])
1274 c->error[0] = c->error[1] = c->error[2] = 0;
1276 input[0].pict_type = AV_PICTURE_TYPE_I;
1277 input[0].quality = 1 * FF_QP2LAMBDA;
1279 out_size = encode_frame(c, &input[0]);
1281 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1283 for (i = 0; i < s->max_b_frames + 1; i++) {
1284 int is_p = i % (j + 1) == j || i == s->max_b_frames;
1286 input[i + 1].pict_type = is_p ?
1287 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1288 input[i + 1].quality = is_p ? p_lambda : b_lambda;
1290 out_size = encode_frame(c, &input[i + 1]);
1292 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1295 /* get the delayed frames */
1297 out_size = encode_frame(c, NULL);
1298 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1301 rd += c->error[0] + c->error[1] + c->error[2];
1312 for (i = 0; i < s->max_b_frames + 2; i++) {
1313 av_freep(&input[i].data[0]);
1316 return best_b_count;
1319 static int select_input_picture(MpegEncContext *s)
1323 for (i = 1; i < MAX_PICTURE_COUNT; i++)
1324 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1325 s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1327 /* set next picture type & ordering */
1328 if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
1329 if (/*s->picture_in_gop_number >= s->gop_size ||*/
1330 s->next_picture_ptr == NULL || s->intra_only) {
1331 s->reordered_input_picture[0] = s->input_picture[0];
1332 s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_I;
1333 s->reordered_input_picture[0]->f.coded_picture_number =
1334 s->coded_picture_number++;
1338 if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1339 if (s->picture_in_gop_number < s->gop_size &&
1340 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1341 // FIXME check that te gop check above is +-1 correct
1342 if (s->input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED) {
1343 for (i = 0; i < 4; i++)
1344 s->input_picture[0]->f.data[i] = NULL;
1345 s->input_picture[0]->f.type = 0;
1347 assert(s->input_picture[0]->f.type == FF_BUFFER_TYPE_USER ||
1348 s->input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL);
1350 s->avctx->release_buffer(s->avctx,
1351 &s->input_picture[0]->f);
1355 ff_vbv_update(s, 0);
1361 if (s->flags & CODEC_FLAG_PASS2) {
1362 for (i = 0; i < s->max_b_frames + 1; i++) {
1363 int pict_num = s->input_picture[0]->f.display_picture_number + i;
1365 if (pict_num >= s->rc_context.num_entries)
1367 if (!s->input_picture[i]) {
1368 s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1372 s->input_picture[i]->f.pict_type =
1373 s->rc_context.entry[pict_num].new_pict_type;
1377 if (s->avctx->b_frame_strategy == 0) {
1378 b_frames = s->max_b_frames;
1379 while (b_frames && !s->input_picture[b_frames])
1381 } else if (s->avctx->b_frame_strategy == 1) {
1382 for (i = 1; i < s->max_b_frames + 1; i++) {
1383 if (s->input_picture[i] &&
1384 s->input_picture[i]->b_frame_score == 0) {
1385 s->input_picture[i]->b_frame_score =
1387 s->input_picture[i ]->f.data[0],
1388 s->input_picture[i - 1]->f.data[0],
1392 for (i = 0; i < s->max_b_frames + 1; i++) {
1393 if (s->input_picture[i] == NULL ||
1394 s->input_picture[i]->b_frame_score - 1 >
1395 s->mb_num / s->avctx->b_sensitivity)
1399 b_frames = FFMAX(0, i - 1);
1402 for (i = 0; i < b_frames + 1; i++) {
1403 s->input_picture[i]->b_frame_score = 0;
1405 } else if (s->avctx->b_frame_strategy == 2) {
1406 b_frames = estimate_best_b_count(s);
1408 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1414 for (i = b_frames - 1; i >= 0; i--) {
1415 int type = s->input_picture[i]->f.pict_type;
1416 if (type && type != AV_PICTURE_TYPE_B)
1419 if (s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_B &&
1420 b_frames == s->max_b_frames) {
1421 av_log(s->avctx, AV_LOG_ERROR,
1422 "warning, too many b frames in a row\n");
1425 if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1426 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1427 s->gop_size > s->picture_in_gop_number) {
1428 b_frames = s->gop_size - s->picture_in_gop_number - 1;
1430 if (s->flags & CODEC_FLAG_CLOSED_GOP)
1432 s->input_picture[b_frames]->f.pict_type = AV_PICTURE_TYPE_I;
1436 if ((s->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1437 s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_I)
1440 s->reordered_input_picture[0] = s->input_picture[b_frames];
1441 if (s->reordered_input_picture[0]->f.pict_type != AV_PICTURE_TYPE_I)
1442 s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_P;
1443 s->reordered_input_picture[0]->f.coded_picture_number =
1444 s->coded_picture_number++;
1445 for (i = 0; i < b_frames; i++) {
1446 s->reordered_input_picture[i + 1] = s->input_picture[i];
1447 s->reordered_input_picture[i + 1]->f.pict_type =
1449 s->reordered_input_picture[i + 1]->f.coded_picture_number =
1450 s->coded_picture_number++;
1455 if (s->reordered_input_picture[0]) {
1456 s->reordered_input_picture[0]->f.reference =
1457 s->reordered_input_picture[0]->f.pict_type !=
1458 AV_PICTURE_TYPE_B ? 3 : 0;
1460 ff_copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1462 if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED ||
1463 s->avctx->rc_buffer_size) {
1464 // input is a shared pix, so we can't modifiy it -> alloc a new
1465 // one & ensure that the shared one is reuseable
1468 int i = ff_find_unused_picture(s, 0);
1471 pic = &s->picture[i];
1473 pic->f.reference = s->reordered_input_picture[0]->f.reference;
1474 if (ff_alloc_picture(s, pic, 0) < 0) {
1478 /* mark us unused / free shared pic */
1479 if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL)
1480 s->avctx->release_buffer(s->avctx,
1481 &s->reordered_input_picture[0]->f);
1482 for (i = 0; i < 4; i++)
1483 s->reordered_input_picture[0]->f.data[i] = NULL;
1484 s->reordered_input_picture[0]->f.type = 0;
1486 copy_picture_attributes(s, &pic->f,
1487 &s->reordered_input_picture[0]->f);
1489 s->current_picture_ptr = pic;
1491 // input is not a shared pix -> reuse buffer for current_pix
1493 assert(s->reordered_input_picture[0]->f.type ==
1494 FF_BUFFER_TYPE_USER ||
1495 s->reordered_input_picture[0]->f.type ==
1496 FF_BUFFER_TYPE_INTERNAL);
1498 s->current_picture_ptr = s->reordered_input_picture[0];
1499 for (i = 0; i < 4; i++) {
1500 s->new_picture.f.data[i] += INPLACE_OFFSET;
1503 ff_copy_picture(&s->current_picture, s->current_picture_ptr);
1505 s->picture_number = s->new_picture.f.display_picture_number;
1507 memset(&s->new_picture, 0, sizeof(Picture));
1512 int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1513 AVFrame *pic_arg, int *got_packet)
1515 MpegEncContext *s = avctx->priv_data;
1516 int i, stuffing_count, ret;
1517 int context_count = s->slice_context_count;
1519 s->picture_in_gop_number++;
1521 if (load_input_picture(s, pic_arg) < 0)
1524 if (select_input_picture(s) < 0) {
1529 if (s->new_picture.f.data[0]) {
1530 if ((ret = ff_alloc_packet2(avctx, pkt, s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000)) < 0)
1533 s->mb_info_ptr = av_packet_new_side_data(pkt,
1534 AV_PKT_DATA_H263_MB_INFO,
1535 s->mb_width*s->mb_height*12);
1536 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1539 for (i = 0; i < context_count; i++) {
1540 int start_y = s->thread_context[i]->start_mb_y;
1541 int end_y = s->thread_context[i]-> end_mb_y;
1542 int h = s->mb_height;
1543 uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1544 uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1546 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1549 s->pict_type = s->new_picture.f.pict_type;
1551 if (ff_MPV_frame_start(s, avctx) < 0)
1554 if (encode_picture(s, s->picture_number) < 0)
1557 avctx->header_bits = s->header_bits;
1558 avctx->mv_bits = s->mv_bits;
1559 avctx->misc_bits = s->misc_bits;
1560 avctx->i_tex_bits = s->i_tex_bits;
1561 avctx->p_tex_bits = s->p_tex_bits;
1562 avctx->i_count = s->i_count;
1563 // FIXME f/b_count in avctx
1564 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
1565 avctx->skip_count = s->skip_count;
1567 ff_MPV_frame_end(s);
1569 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1570 ff_mjpeg_encode_picture_trailer(s);
1572 if (avctx->rc_buffer_size) {
1573 RateControlContext *rcc = &s->rc_context;
1574 int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
1576 if (put_bits_count(&s->pb) > max_size &&
1577 s->lambda < s->avctx->lmax) {
1578 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1579 (s->qscale + 1) / s->qscale);
1580 if (s->adaptive_quant) {
1582 for (i = 0; i < s->mb_height * s->mb_stride; i++)
1583 s->lambda_table[i] =
1584 FFMAX(s->lambda_table[i] + 1,
1585 s->lambda_table[i] * (s->qscale + 1) /
1588 s->mb_skipped = 0; // done in MPV_frame_start()
1589 // done in encode_picture() so we must undo it
1590 if (s->pict_type == AV_PICTURE_TYPE_P) {
1591 if (s->flipflop_rounding ||
1592 s->codec_id == AV_CODEC_ID_H263P ||
1593 s->codec_id == AV_CODEC_ID_MPEG4)
1594 s->no_rounding ^= 1;
1596 if (s->pict_type != AV_PICTURE_TYPE_B) {
1597 s->time_base = s->last_time_base;
1598 s->last_non_b_time = s->time - s->pp_time;
1600 for (i = 0; i < context_count; i++) {
1601 PutBitContext *pb = &s->thread_context[i]->pb;
1602 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1607 assert(s->avctx->rc_max_rate);
1610 if (s->flags & CODEC_FLAG_PASS1)
1611 ff_write_pass1_stats(s);
1613 for (i = 0; i < 4; i++) {
1614 s->current_picture_ptr->f.error[i] = s->current_picture.f.error[i];
1615 avctx->error[i] += s->current_picture_ptr->f.error[i];
1618 if (s->flags & CODEC_FLAG_PASS1)
1619 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1620 avctx->i_tex_bits + avctx->p_tex_bits ==
1621 put_bits_count(&s->pb));
1622 flush_put_bits(&s->pb);
1623 s->frame_bits = put_bits_count(&s->pb);
1625 stuffing_count = ff_vbv_update(s, s->frame_bits);
1626 s->stuffing_bits = 8*stuffing_count;
1627 if (stuffing_count) {
1628 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1629 stuffing_count + 50) {
1630 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1634 switch (s->codec_id) {
1635 case AV_CODEC_ID_MPEG1VIDEO:
1636 case AV_CODEC_ID_MPEG2VIDEO:
1637 while (stuffing_count--) {
1638 put_bits(&s->pb, 8, 0);
1641 case AV_CODEC_ID_MPEG4:
1642 put_bits(&s->pb, 16, 0);
1643 put_bits(&s->pb, 16, 0x1C3);
1644 stuffing_count -= 4;
1645 while (stuffing_count--) {
1646 put_bits(&s->pb, 8, 0xFF);
1650 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1652 flush_put_bits(&s->pb);
1653 s->frame_bits = put_bits_count(&s->pb);
1656 /* update mpeg1/2 vbv_delay for CBR */
1657 if (s->avctx->rc_max_rate &&
1658 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1659 s->out_format == FMT_MPEG1 &&
1660 90000LL * (avctx->rc_buffer_size - 1) <=
1661 s->avctx->rc_max_rate * 0xFFFFLL) {
1662 int vbv_delay, min_delay;
1663 double inbits = s->avctx->rc_max_rate *
1664 av_q2d(s->avctx->time_base);
1665 int minbits = s->frame_bits - 8 *
1666 (s->vbv_delay_ptr - s->pb.buf - 1);
1667 double bits = s->rc_context.buffer_index + minbits - inbits;
1670 av_log(s->avctx, AV_LOG_ERROR,
1671 "Internal error, negative bits\n");
1673 assert(s->repeat_first_field == 0);
1675 vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1676 min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1677 s->avctx->rc_max_rate;
1679 vbv_delay = FFMAX(vbv_delay, min_delay);
1681 av_assert0(vbv_delay < 0xFFFF);
1683 s->vbv_delay_ptr[0] &= 0xF8;
1684 s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1685 s->vbv_delay_ptr[1] = vbv_delay >> 5;
1686 s->vbv_delay_ptr[2] &= 0x07;
1687 s->vbv_delay_ptr[2] |= vbv_delay << 3;
1688 avctx->vbv_delay = vbv_delay * 300;
1690 s->total_bits += s->frame_bits;
1691 avctx->frame_bits = s->frame_bits;
1693 pkt->pts = s->current_picture.f.pts;
1694 if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1695 if (!s->current_picture.f.coded_picture_number)
1696 pkt->dts = pkt->pts - s->dts_delta;
1698 pkt->dts = s->reordered_pts;
1699 s->reordered_pts = pkt->pts;
1701 pkt->dts = pkt->pts;
1702 if (s->current_picture.f.key_frame)
1703 pkt->flags |= AV_PKT_FLAG_KEY;
1705 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1709 assert((s->frame_bits & 7) == 0);
1711 pkt->size = s->frame_bits / 8;
1712 *got_packet = !!pkt->size;
1716 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1717 int n, int threshold)
1719 static const char tab[64] = {
1720 3, 2, 2, 1, 1, 1, 1, 1,
1721 1, 1, 1, 1, 1, 1, 1, 1,
1722 1, 1, 1, 1, 1, 1, 1, 1,
1723 0, 0, 0, 0, 0, 0, 0, 0,
1724 0, 0, 0, 0, 0, 0, 0, 0,
1725 0, 0, 0, 0, 0, 0, 0, 0,
1726 0, 0, 0, 0, 0, 0, 0, 0,
1727 0, 0, 0, 0, 0, 0, 0, 0
1732 int16_t *block = s->block[n];
1733 const int last_index = s->block_last_index[n];
1736 if (threshold < 0) {
1738 threshold = -threshold;
1742 /* Are all we could set to zero already zero? */
1743 if (last_index <= skip_dc - 1)
1746 for (i = 0; i <= last_index; i++) {
1747 const int j = s->intra_scantable.permutated[i];
1748 const int level = FFABS(block[j]);
1750 if (skip_dc && i == 0)
1754 } else if (level > 1) {
1760 if (score >= threshold)
1762 for (i = skip_dc; i <= last_index; i++) {
1763 const int j = s->intra_scantable.permutated[i];
1767 s->block_last_index[n] = 0;
1769 s->block_last_index[n] = -1;
1772 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1776 const int maxlevel = s->max_qcoeff;
1777 const int minlevel = s->min_qcoeff;
1781 i = 1; // skip clipping of intra dc
1785 for (; i <= last_index; i++) {
1786 const int j = s->intra_scantable.permutated[i];
1787 int level = block[j];
1789 if (level > maxlevel) {
1792 } else if (level < minlevel) {
1800 if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1801 av_log(s->avctx, AV_LOG_INFO,
1802 "warning, clipping %d dct coefficients to %d..%d\n",
1803 overflow, minlevel, maxlevel);
1806 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1810 for (y = 0; y < 8; y++) {
1811 for (x = 0; x < 8; x++) {
1817 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1818 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1819 int v = ptr[x2 + y2 * stride];
1825 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
1830 static av_always_inline void encode_mb_internal(MpegEncContext *s,
1831 int motion_x, int motion_y,
1832 int mb_block_height,
1836 int16_t weight[12][64];
1837 int16_t orig[12][64];
1838 const int mb_x = s->mb_x;
1839 const int mb_y = s->mb_y;
1842 int dct_offset = s->linesize * 8; // default for progressive frames
1843 int uv_dct_offset = s->uvlinesize * 8;
1844 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1847 for (i = 0; i < mb_block_count; i++)
1848 skip_dct[i] = s->skipdct;
1850 if (s->adaptive_quant) {
1851 const int last_qp = s->qscale;
1852 const int mb_xy = mb_x + mb_y * s->mb_stride;
1854 s->lambda = s->lambda_table[mb_xy];
1857 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
1858 s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];
1859 s->dquant = s->qscale - last_qp;
1861 if (s->out_format == FMT_H263) {
1862 s->dquant = av_clip(s->dquant, -2, 2);
1864 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1866 if (s->pict_type == AV_PICTURE_TYPE_B) {
1867 if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1870 if (s->mv_type == MV_TYPE_8X8)
1876 ff_set_qscale(s, last_qp + s->dquant);
1877 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
1878 ff_set_qscale(s, s->qscale + s->dquant);
1880 wrap_y = s->linesize;
1881 wrap_c = s->uvlinesize;
1882 ptr_y = s->new_picture.f.data[0] +
1883 (mb_y * 16 * wrap_y) + mb_x * 16;
1884 ptr_cb = s->new_picture.f.data[1] +
1885 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1886 ptr_cr = s->new_picture.f.data[2] +
1887 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1889 if((mb_x*16+16 > s->width || mb_y*16+16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
1890 uint8_t *ebuf = s->edge_emu_buffer + 32;
1891 int cw = (s->width + s->chroma_x_shift) >> s->chroma_x_shift;
1892 int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
1893 s->vdsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,
1894 mb_y * 16, s->width, s->height);
1896 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, mb_block_width,
1897 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1899 ptr_cb = ebuf + 18 * wrap_y;
1900 s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 16, ptr_cr, wrap_c, mb_block_width,
1901 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1903 ptr_cr = ebuf + 18 * wrap_y + 16;
1907 if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1908 int progressive_score, interlaced_score;
1910 s->interlaced_dct = 0;
1911 progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,
1913 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1914 NULL, wrap_y, 8) - 400;
1916 if (progressive_score > 0) {
1917 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,
1918 NULL, wrap_y * 2, 8) +
1919 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,
1920 NULL, wrap_y * 2, 8);
1921 if (progressive_score > interlaced_score) {
1922 s->interlaced_dct = 1;
1924 dct_offset = wrap_y;
1925 uv_dct_offset = wrap_c;
1927 if (s->chroma_format == CHROMA_422 ||
1928 s->chroma_format == CHROMA_444)
1934 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
1935 s->dsp.get_pixels(s->block[1], ptr_y + 8 , wrap_y);
1936 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
1937 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);
1939 if (s->flags & CODEC_FLAG_GRAY) {
1943 s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1944 s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1945 if (!s->chroma_y_shift && s->chroma_x_shift) { /* 422 */
1946 s->dsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
1947 s->dsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
1948 } else if (!s->chroma_y_shift && !s->chroma_x_shift) { /* 444 */
1949 s->dsp.get_pixels(s->block[6], ptr_cb + 8, wrap_c);
1950 s->dsp.get_pixels(s->block[7], ptr_cr + 8, wrap_c);
1951 s->dsp.get_pixels(s->block[8], ptr_cb + uv_dct_offset, wrap_c);
1952 s->dsp.get_pixels(s->block[9], ptr_cr + uv_dct_offset, wrap_c);
1953 s->dsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
1954 s->dsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
1958 op_pixels_func (*op_pix)[4];
1959 qpel_mc_func (*op_qpix)[16];
1960 uint8_t *dest_y, *dest_cb, *dest_cr;
1962 dest_y = s->dest[0];
1963 dest_cb = s->dest[1];
1964 dest_cr = s->dest[2];
1966 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1967 op_pix = s->dsp.put_pixels_tab;
1968 op_qpix = s->dsp.put_qpel_pixels_tab;
1970 op_pix = s->dsp.put_no_rnd_pixels_tab;
1971 op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;
1974 if (s->mv_dir & MV_DIR_FORWARD) {
1975 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0,
1976 s->last_picture.f.data,
1978 op_pix = s->dsp.avg_pixels_tab;
1979 op_qpix = s->dsp.avg_qpel_pixels_tab;
1981 if (s->mv_dir & MV_DIR_BACKWARD) {
1982 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1,
1983 s->next_picture.f.data,
1987 if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1988 int progressive_score, interlaced_score;
1990 s->interlaced_dct = 0;
1991 progressive_score = s->dsp.ildct_cmp[0](s, dest_y,
1994 s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,
1995 ptr_y + wrap_y * 8, wrap_y,
1998 if (s->avctx->ildct_cmp == FF_CMP_VSSE)
1999 progressive_score -= 400;
2001 if (progressive_score > 0) {
2002 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,
2005 s->dsp.ildct_cmp[0](s, dest_y + wrap_y,
2009 if (progressive_score > interlaced_score) {
2010 s->interlaced_dct = 1;
2012 dct_offset = wrap_y;
2013 uv_dct_offset = wrap_c;
2015 if (s->chroma_format == CHROMA_422)
2021 s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2022 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2023 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2024 dest_y + dct_offset, wrap_y);
2025 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2026 dest_y + dct_offset + 8, wrap_y);
2028 if (s->flags & CODEC_FLAG_GRAY) {
2032 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2033 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2034 if (!s->chroma_y_shift) { /* 422 */
2035 s->dsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2036 dest_cb + uv_dct_offset, wrap_c);
2037 s->dsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2038 dest_cr + uv_dct_offset, wrap_c);
2041 /* pre quantization */
2042 if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2043 2 * s->qscale * s->qscale) {
2045 if (s->dsp.sad[1](NULL, ptr_y , dest_y,
2046 wrap_y, 8) < 20 * s->qscale)
2048 if (s->dsp.sad[1](NULL, ptr_y + 8,
2049 dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2051 if (s->dsp.sad[1](NULL, ptr_y + dct_offset,
2052 dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)
2054 if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,
2055 dest_y + dct_offset + 8,
2056 wrap_y, 8) < 20 * s->qscale)
2058 if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,
2059 wrap_c, 8) < 20 * s->qscale)
2061 if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,
2062 wrap_c, 8) < 20 * s->qscale)
2064 if (!s->chroma_y_shift) { /* 422 */
2065 if (s->dsp.sad[1](NULL, ptr_cb + uv_dct_offset,
2066 dest_cb + uv_dct_offset,
2067 wrap_c, 8) < 20 * s->qscale)
2069 if (s->dsp.sad[1](NULL, ptr_cr + uv_dct_offset,
2070 dest_cr + uv_dct_offset,
2071 wrap_c, 8) < 20 * s->qscale)
2077 if (s->quantizer_noise_shaping) {
2079 get_visual_weight(weight[0], ptr_y , wrap_y);
2081 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2083 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2085 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2087 get_visual_weight(weight[4], ptr_cb , wrap_c);
2089 get_visual_weight(weight[5], ptr_cr , wrap_c);
2090 if (!s->chroma_y_shift) { /* 422 */
2092 get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2095 get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2098 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2101 /* DCT & quantize */
2102 av_assert2(s->out_format != FMT_MJPEG || s->qscale == 8);
2104 for (i = 0; i < mb_block_count; i++) {
2107 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2108 // FIXME we could decide to change to quantizer instead of
2110 // JS: I don't think that would be a good idea it could lower
2111 // quality instead of improve it. Just INTRADC clipping
2112 // deserves changes in quantizer
2114 clip_coeffs(s, s->block[i], s->block_last_index[i]);
2116 s->block_last_index[i] = -1;
2118 if (s->quantizer_noise_shaping) {
2119 for (i = 0; i < mb_block_count; i++) {
2121 s->block_last_index[i] =
2122 dct_quantize_refine(s, s->block[i], weight[i],
2123 orig[i], i, s->qscale);
2128 if (s->luma_elim_threshold && !s->mb_intra)
2129 for (i = 0; i < 4; i++)
2130 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2131 if (s->chroma_elim_threshold && !s->mb_intra)
2132 for (i = 4; i < mb_block_count; i++)
2133 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2135 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2136 for (i = 0; i < mb_block_count; i++) {
2137 if (s->block_last_index[i] == -1)
2138 s->coded_score[i] = INT_MAX / 256;
2143 if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2144 s->block_last_index[4] =
2145 s->block_last_index[5] = 0;
2147 s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2150 // non c quantize code returns incorrect block_last_index FIXME
2151 if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2152 for (i = 0; i < mb_block_count; i++) {
2154 if (s->block_last_index[i] > 0) {
2155 for (j = 63; j > 0; j--) {
2156 if (s->block[i][s->intra_scantable.permutated[j]])
2159 s->block_last_index[i] = j;
2164 /* huffman encode */
2165 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2166 case AV_CODEC_ID_MPEG1VIDEO:
2167 case AV_CODEC_ID_MPEG2VIDEO:
2168 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2169 ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2171 case AV_CODEC_ID_MPEG4:
2172 if (CONFIG_MPEG4_ENCODER)
2173 ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2175 case AV_CODEC_ID_MSMPEG4V2:
2176 case AV_CODEC_ID_MSMPEG4V3:
2177 case AV_CODEC_ID_WMV1:
2178 if (CONFIG_MSMPEG4_ENCODER)
2179 ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2181 case AV_CODEC_ID_WMV2:
2182 if (CONFIG_WMV2_ENCODER)
2183 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2185 case AV_CODEC_ID_H261:
2186 if (CONFIG_H261_ENCODER)
2187 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2189 case AV_CODEC_ID_H263:
2190 case AV_CODEC_ID_H263P:
2191 case AV_CODEC_ID_FLV1:
2192 case AV_CODEC_ID_RV10:
2193 case AV_CODEC_ID_RV20:
2194 if (CONFIG_H263_ENCODER)
2195 ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2197 case AV_CODEC_ID_MJPEG:
2198 case AV_CODEC_ID_AMV:
2199 if (CONFIG_MJPEG_ENCODER)
2200 ff_mjpeg_encode_mb(s, s->block);
2207 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2209 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 8, 6);
2210 else if (s->chroma_format == CHROMA_422) encode_mb_internal(s, motion_x, motion_y, 16, 8, 8);
2211 else encode_mb_internal(s, motion_x, motion_y, 16, 16, 12);
2214 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2217 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2220 d->mb_skip_run= s->mb_skip_run;
2222 d->last_dc[i] = s->last_dc[i];
2225 d->mv_bits= s->mv_bits;
2226 d->i_tex_bits= s->i_tex_bits;
2227 d->p_tex_bits= s->p_tex_bits;
2228 d->i_count= s->i_count;
2229 d->f_count= s->f_count;
2230 d->b_count= s->b_count;
2231 d->skip_count= s->skip_count;
2232 d->misc_bits= s->misc_bits;
2236 d->qscale= s->qscale;
2237 d->dquant= s->dquant;
2239 d->esc3_level_length= s->esc3_level_length;
2242 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2245 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2246 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2249 d->mb_skip_run= s->mb_skip_run;
2251 d->last_dc[i] = s->last_dc[i];
2254 d->mv_bits= s->mv_bits;
2255 d->i_tex_bits= s->i_tex_bits;
2256 d->p_tex_bits= s->p_tex_bits;
2257 d->i_count= s->i_count;
2258 d->f_count= s->f_count;
2259 d->b_count= s->b_count;
2260 d->skip_count= s->skip_count;
2261 d->misc_bits= s->misc_bits;
2263 d->mb_intra= s->mb_intra;
2264 d->mb_skipped= s->mb_skipped;
2265 d->mv_type= s->mv_type;
2266 d->mv_dir= s->mv_dir;
2268 if(s->data_partitioning){
2270 d->tex_pb= s->tex_pb;
2274 d->block_last_index[i]= s->block_last_index[i];
2275 d->interlaced_dct= s->interlaced_dct;
2276 d->qscale= s->qscale;
2278 d->esc3_level_length= s->esc3_level_length;
2281 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2282 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2283 int *dmin, int *next_block, int motion_x, int motion_y)
2286 uint8_t *dest_backup[3];
2288 copy_context_before_encode(s, backup, type);
2290 s->block= s->blocks[*next_block];
2291 s->pb= pb[*next_block];
2292 if(s->data_partitioning){
2293 s->pb2 = pb2 [*next_block];
2294 s->tex_pb= tex_pb[*next_block];
2298 memcpy(dest_backup, s->dest, sizeof(s->dest));
2299 s->dest[0] = s->rd_scratchpad;
2300 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
2301 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
2302 assert(s->linesize >= 32); //FIXME
2305 encode_mb(s, motion_x, motion_y);
2307 score= put_bits_count(&s->pb);
2308 if(s->data_partitioning){
2309 score+= put_bits_count(&s->pb2);
2310 score+= put_bits_count(&s->tex_pb);
2313 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2314 ff_MPV_decode_mb(s, s->block);
2316 score *= s->lambda2;
2317 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2321 memcpy(s->dest, dest_backup, sizeof(s->dest));
2328 copy_context_after_encode(best, s, type);
2332 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2333 uint32_t *sq = ff_squareTbl + 256;
2338 return s->dsp.sse[0](NULL, src1, src2, stride, 16);
2339 else if(w==8 && h==8)
2340 return s->dsp.sse[1](NULL, src1, src2, stride, 8);
2344 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2353 static int sse_mb(MpegEncContext *s){
2357 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2358 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2361 if(s->avctx->mb_cmp == FF_CMP_NSSE){
2362 return s->dsp.nsse[0](s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
2363 +s->dsp.nsse[1](s, s->new_picture.f.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
2364 +s->dsp.nsse[1](s, s->new_picture.f.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
2366 return s->dsp.sse[0](NULL, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
2367 +s->dsp.sse[1](NULL, s->new_picture.f.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
2368 +s->dsp.sse[1](NULL, s->new_picture.f.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
2371 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)
2372 +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)
2373 +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);
2376 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2377 MpegEncContext *s= *(void**)arg;
2381 s->me.dia_size= s->avctx->pre_dia_size;
2382 s->first_slice_line=1;
2383 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2384 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2385 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2387 s->first_slice_line=0;
2395 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2396 MpegEncContext *s= *(void**)arg;
2398 ff_check_alignment();
2400 s->me.dia_size= s->avctx->dia_size;
2401 s->first_slice_line=1;
2402 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2403 s->mb_x=0; //for block init below
2404 ff_init_block_index(s);
2405 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2406 s->block_index[0]+=2;
2407 s->block_index[1]+=2;
2408 s->block_index[2]+=2;
2409 s->block_index[3]+=2;
2411 /* compute motion vector & mb_type and store in context */
2412 if(s->pict_type==AV_PICTURE_TYPE_B)
2413 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2415 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2417 s->first_slice_line=0;
2422 static int mb_var_thread(AVCodecContext *c, void *arg){
2423 MpegEncContext *s= *(void**)arg;
2426 ff_check_alignment();
2428 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2429 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2432 uint8_t *pix = s->new_picture.f.data[0] + (yy * s->linesize) + xx;
2434 int sum = s->dsp.pix_sum(pix, s->linesize);
2436 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500 + 128)>>8;
2438 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2439 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2440 s->me.mb_var_sum_temp += varc;
2446 static void write_slice_end(MpegEncContext *s){
2447 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2448 if(s->partitioned_frame){
2449 ff_mpeg4_merge_partitions(s);
2452 ff_mpeg4_stuffing(&s->pb);
2453 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2454 ff_mjpeg_encode_stuffing(s);
2457 avpriv_align_put_bits(&s->pb);
2458 flush_put_bits(&s->pb);
2460 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2461 s->misc_bits+= get_bits_diff(s);
2464 static void write_mb_info(MpegEncContext *s)
2466 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2467 int offset = put_bits_count(&s->pb);
2468 int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2469 int gobn = s->mb_y / s->gob_index;
2471 if (CONFIG_H263_ENCODER)
2472 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2473 bytestream_put_le32(&ptr, offset);
2474 bytestream_put_byte(&ptr, s->qscale);
2475 bytestream_put_byte(&ptr, gobn);
2476 bytestream_put_le16(&ptr, mba);
2477 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2478 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2479 /* 4MV not implemented */
2480 bytestream_put_byte(&ptr, 0); /* hmv2 */
2481 bytestream_put_byte(&ptr, 0); /* vmv2 */
2484 static void update_mb_info(MpegEncContext *s, int startcode)
2488 if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2489 s->mb_info_size += 12;
2490 s->prev_mb_info = s->last_mb_info;
2493 s->prev_mb_info = put_bits_count(&s->pb)/8;
2494 /* This might have incremented mb_info_size above, and we return without
2495 * actually writing any info into that slot yet. But in that case,
2496 * this will be called again at the start of the after writing the
2497 * start code, actually writing the mb info. */
2501 s->last_mb_info = put_bits_count(&s->pb)/8;
2502 if (!s->mb_info_size)
2503 s->mb_info_size += 12;
2507 static int encode_thread(AVCodecContext *c, void *arg){
2508 MpegEncContext *s= *(void**)arg;
2509 int mb_x, mb_y, pdif = 0;
2510 int chr_h= 16>>s->chroma_y_shift;
2512 MpegEncContext best_s, backup_s;
2513 uint8_t bit_buf[2][MAX_MB_BYTES];
2514 uint8_t bit_buf2[2][MAX_MB_BYTES];
2515 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2516 PutBitContext pb[2], pb2[2], tex_pb[2];
2518 ff_check_alignment();
2521 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2522 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2523 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2526 s->last_bits= put_bits_count(&s->pb);
2537 /* init last dc values */
2538 /* note: quant matrix value (8) is implied here */
2539 s->last_dc[i] = 128 << s->intra_dc_precision;
2541 s->current_picture.f.error[i] = 0;
2543 if(s->codec_id==AV_CODEC_ID_AMV){
2544 s->last_dc[0] = 128*8/13;
2545 s->last_dc[1] = 128*8/14;
2546 s->last_dc[2] = 128*8/14;
2549 memset(s->last_mv, 0, sizeof(s->last_mv));
2553 switch(s->codec_id){
2554 case AV_CODEC_ID_H263:
2555 case AV_CODEC_ID_H263P:
2556 case AV_CODEC_ID_FLV1:
2557 if (CONFIG_H263_ENCODER)
2558 s->gob_index = ff_h263_get_gob_height(s);
2560 case AV_CODEC_ID_MPEG4:
2561 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2562 ff_mpeg4_init_partitions(s);
2568 s->first_slice_line = 1;
2569 s->ptr_lastgob = s->pb.buf;
2570 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2574 ff_set_qscale(s, s->qscale);
2575 ff_init_block_index(s);
2577 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2578 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2579 int mb_type= s->mb_type[xy];
2584 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2585 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2588 if(s->data_partitioning){
2589 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2590 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2591 av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
2597 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2598 ff_update_block_index(s);
2600 if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2601 ff_h261_reorder_mb_index(s);
2602 xy= s->mb_y*s->mb_stride + s->mb_x;
2603 mb_type= s->mb_type[xy];
2606 /* write gob / video packet header */
2608 int current_packet_size, is_gob_start;
2610 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2612 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2614 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2616 switch(s->codec_id){
2617 case AV_CODEC_ID_H263:
2618 case AV_CODEC_ID_H263P:
2619 if(!s->h263_slice_structured)
2620 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2622 case AV_CODEC_ID_MPEG2VIDEO:
2623 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2624 case AV_CODEC_ID_MPEG1VIDEO:
2625 if(s->mb_skip_run) is_gob_start=0;
2627 case AV_CODEC_ID_MJPEG:
2628 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2633 if(s->start_mb_y != mb_y || mb_x!=0){
2635 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2636 ff_mpeg4_init_partitions(s);
2640 av_assert2((put_bits_count(&s->pb)&7) == 0);
2641 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2643 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
2644 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2645 int d= 100 / s->avctx->error_rate;
2647 current_packet_size=0;
2648 s->pb.buf_ptr= s->ptr_lastgob;
2649 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2653 if (s->avctx->rtp_callback){
2654 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2655 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2657 update_mb_info(s, 1);
2659 switch(s->codec_id){
2660 case AV_CODEC_ID_MPEG4:
2661 if (CONFIG_MPEG4_ENCODER) {
2662 ff_mpeg4_encode_video_packet_header(s);
2663 ff_mpeg4_clean_buffers(s);
2666 case AV_CODEC_ID_MPEG1VIDEO:
2667 case AV_CODEC_ID_MPEG2VIDEO:
2668 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2669 ff_mpeg1_encode_slice_header(s);
2670 ff_mpeg1_clean_buffers(s);
2673 case AV_CODEC_ID_H263:
2674 case AV_CODEC_ID_H263P:
2675 if (CONFIG_H263_ENCODER)
2676 ff_h263_encode_gob_header(s, mb_y);
2680 if(s->flags&CODEC_FLAG_PASS1){
2681 int bits= put_bits_count(&s->pb);
2682 s->misc_bits+= bits - s->last_bits;
2686 s->ptr_lastgob += current_packet_size;
2687 s->first_slice_line=1;
2688 s->resync_mb_x=mb_x;
2689 s->resync_mb_y=mb_y;
2693 if( (s->resync_mb_x == s->mb_x)
2694 && s->resync_mb_y+1 == s->mb_y){
2695 s->first_slice_line=0;
2699 s->dquant=0; //only for QP_RD
2701 update_mb_info(s, 0);
2703 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
2705 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2707 copy_context_before_encode(&backup_s, s, -1);
2709 best_s.data_partitioning= s->data_partitioning;
2710 best_s.partitioned_frame= s->partitioned_frame;
2711 if(s->data_partitioning){
2712 backup_s.pb2= s->pb2;
2713 backup_s.tex_pb= s->tex_pb;
2716 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2717 s->mv_dir = MV_DIR_FORWARD;
2718 s->mv_type = MV_TYPE_16X16;
2720 s->mv[0][0][0] = s->p_mv_table[xy][0];
2721 s->mv[0][0][1] = s->p_mv_table[xy][1];
2722 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2723 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2725 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2726 s->mv_dir = MV_DIR_FORWARD;
2727 s->mv_type = MV_TYPE_FIELD;
2730 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2731 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2732 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2734 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2735 &dmin, &next_block, 0, 0);
2737 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2738 s->mv_dir = MV_DIR_FORWARD;
2739 s->mv_type = MV_TYPE_16X16;
2743 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2744 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2746 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2747 s->mv_dir = MV_DIR_FORWARD;
2748 s->mv_type = MV_TYPE_8X8;
2751 s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
2752 s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
2754 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2755 &dmin, &next_block, 0, 0);
2757 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2758 s->mv_dir = MV_DIR_FORWARD;
2759 s->mv_type = MV_TYPE_16X16;
2761 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2762 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2763 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2764 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2766 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2767 s->mv_dir = MV_DIR_BACKWARD;
2768 s->mv_type = MV_TYPE_16X16;
2770 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2771 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2772 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2773 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2775 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2776 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2777 s->mv_type = MV_TYPE_16X16;
2779 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2780 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2781 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2782 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2783 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2784 &dmin, &next_block, 0, 0);
2786 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2787 s->mv_dir = MV_DIR_FORWARD;
2788 s->mv_type = MV_TYPE_FIELD;
2791 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2792 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2793 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2795 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2796 &dmin, &next_block, 0, 0);
2798 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2799 s->mv_dir = MV_DIR_BACKWARD;
2800 s->mv_type = MV_TYPE_FIELD;
2803 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2804 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2805 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2807 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2808 &dmin, &next_block, 0, 0);
2810 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2811 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2812 s->mv_type = MV_TYPE_FIELD;
2814 for(dir=0; dir<2; dir++){
2816 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2817 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2818 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2821 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2822 &dmin, &next_block, 0, 0);
2824 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2826 s->mv_type = MV_TYPE_16X16;
2830 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2831 &dmin, &next_block, 0, 0);
2832 if(s->h263_pred || s->h263_aic){
2834 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2836 ff_clean_intra_table_entries(s); //old mode?
2840 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2841 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2842 const int last_qp= backup_s.qscale;
2845 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2846 static const int dquant_tab[4]={-1,1,-2,2};
2847 int storecoefs = s->mb_intra && s->dc_val[0];
2849 av_assert2(backup_s.dquant == 0);
2852 s->mv_dir= best_s.mv_dir;
2853 s->mv_type = MV_TYPE_16X16;
2854 s->mb_intra= best_s.mb_intra;
2855 s->mv[0][0][0] = best_s.mv[0][0][0];
2856 s->mv[0][0][1] = best_s.mv[0][0][1];
2857 s->mv[1][0][0] = best_s.mv[1][0][0];
2858 s->mv[1][0][1] = best_s.mv[1][0][1];
2860 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
2861 for(; qpi<4; qpi++){
2862 int dquant= dquant_tab[qpi];
2863 qp= last_qp + dquant;
2864 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2866 backup_s.dquant= dquant;
2869 dc[i]= s->dc_val[0][ s->block_index[i] ];
2870 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
2874 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2875 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2876 if(best_s.qscale != qp){
2879 s->dc_val[0][ s->block_index[i] ]= dc[i];
2880 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
2887 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2888 int mx= s->b_direct_mv_table[xy][0];
2889 int my= s->b_direct_mv_table[xy][1];
2891 backup_s.dquant = 0;
2892 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2894 ff_mpeg4_set_direct_mv(s, mx, my);
2895 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2896 &dmin, &next_block, mx, my);
2898 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2899 backup_s.dquant = 0;
2900 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2902 ff_mpeg4_set_direct_mv(s, 0, 0);
2903 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2904 &dmin, &next_block, 0, 0);
2906 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
2909 coded |= s->block_last_index[i];
2912 memcpy(s->mv, best_s.mv, sizeof(s->mv));
2913 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2914 mx=my=0; //FIXME find the one we actually used
2915 ff_mpeg4_set_direct_mv(s, mx, my);
2916 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2924 s->mv_dir= best_s.mv_dir;
2925 s->mv_type = best_s.mv_type;
2927 /* s->mv[0][0][0] = best_s.mv[0][0][0];
2928 s->mv[0][0][1] = best_s.mv[0][0][1];
2929 s->mv[1][0][0] = best_s.mv[1][0][0];
2930 s->mv[1][0][1] = best_s.mv[1][0][1];*/
2933 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2934 &dmin, &next_block, mx, my);
2939 s->current_picture.f.qscale_table[xy] = best_s.qscale;
2941 copy_context_after_encode(s, &best_s, -1);
2943 pb_bits_count= put_bits_count(&s->pb);
2944 flush_put_bits(&s->pb);
2945 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2948 if(s->data_partitioning){
2949 pb2_bits_count= put_bits_count(&s->pb2);
2950 flush_put_bits(&s->pb2);
2951 avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2952 s->pb2= backup_s.pb2;
2954 tex_pb_bits_count= put_bits_count(&s->tex_pb);
2955 flush_put_bits(&s->tex_pb);
2956 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2957 s->tex_pb= backup_s.tex_pb;
2959 s->last_bits= put_bits_count(&s->pb);
2961 if (CONFIG_H263_ENCODER &&
2962 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2963 ff_h263_update_motion_val(s);
2965 if(next_block==0){ //FIXME 16 vs linesize16
2966 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
2967 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
2968 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2971 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2972 ff_MPV_decode_mb(s, s->block);
2974 int motion_x = 0, motion_y = 0;
2975 s->mv_type=MV_TYPE_16X16;
2976 // only one MB-Type possible
2979 case CANDIDATE_MB_TYPE_INTRA:
2982 motion_x= s->mv[0][0][0] = 0;
2983 motion_y= s->mv[0][0][1] = 0;
2985 case CANDIDATE_MB_TYPE_INTER:
2986 s->mv_dir = MV_DIR_FORWARD;
2988 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2989 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2991 case CANDIDATE_MB_TYPE_INTER_I:
2992 s->mv_dir = MV_DIR_FORWARD;
2993 s->mv_type = MV_TYPE_FIELD;
2996 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2997 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2998 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3001 case CANDIDATE_MB_TYPE_INTER4V:
3002 s->mv_dir = MV_DIR_FORWARD;
3003 s->mv_type = MV_TYPE_8X8;
3006 s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
3007 s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
3010 case CANDIDATE_MB_TYPE_DIRECT:
3011 if (CONFIG_MPEG4_ENCODER) {
3012 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3014 motion_x=s->b_direct_mv_table[xy][0];
3015 motion_y=s->b_direct_mv_table[xy][1];
3016 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3019 case CANDIDATE_MB_TYPE_DIRECT0:
3020 if (CONFIG_MPEG4_ENCODER) {
3021 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3023 ff_mpeg4_set_direct_mv(s, 0, 0);
3026 case CANDIDATE_MB_TYPE_BIDIR:
3027 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3029 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3030 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3031 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3032 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3034 case CANDIDATE_MB_TYPE_BACKWARD:
3035 s->mv_dir = MV_DIR_BACKWARD;
3037 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3038 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3040 case CANDIDATE_MB_TYPE_FORWARD:
3041 s->mv_dir = MV_DIR_FORWARD;
3043 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3044 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3046 case CANDIDATE_MB_TYPE_FORWARD_I:
3047 s->mv_dir = MV_DIR_FORWARD;
3048 s->mv_type = MV_TYPE_FIELD;
3051 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3052 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3053 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3056 case CANDIDATE_MB_TYPE_BACKWARD_I:
3057 s->mv_dir = MV_DIR_BACKWARD;
3058 s->mv_type = MV_TYPE_FIELD;
3061 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3062 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3063 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3066 case CANDIDATE_MB_TYPE_BIDIR_I:
3067 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3068 s->mv_type = MV_TYPE_FIELD;
3070 for(dir=0; dir<2; dir++){
3072 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3073 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3074 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3079 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3082 encode_mb(s, motion_x, motion_y);
3084 // RAL: Update last macroblock type
3085 s->last_mv_dir = s->mv_dir;
3087 if (CONFIG_H263_ENCODER &&
3088 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3089 ff_h263_update_motion_val(s);
3091 ff_MPV_decode_mb(s, s->block);
3094 /* clean the MV table in IPS frames for direct mode in B frames */
3095 if(s->mb_intra /* && I,P,S_TYPE */){
3096 s->p_mv_table[xy][0]=0;
3097 s->p_mv_table[xy][1]=0;
3100 if(s->flags&CODEC_FLAG_PSNR){
3104 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3105 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3107 s->current_picture.f.error[0] += sse(
3108 s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3109 s->dest[0], w, h, s->linesize);
3110 s->current_picture.f.error[1] += sse(
3111 s, s->new_picture.f.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3112 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3113 s->current_picture.f.error[2] += sse(
3114 s, s->new_picture.f.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
3115 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3118 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3119 ff_h263_loop_filter(s);
3121 av_dlog(s->avctx, "MB %d %d bits\n",
3122 s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3126 //not beautiful here but we must write it before flushing so it has to be here
3127 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3128 ff_msmpeg4_encode_ext_header(s);
3132 /* Send the last GOB if RTP */
3133 if (s->avctx->rtp_callback) {
3134 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3135 pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3136 /* Call the RTP callback to send the last GOB */
3138 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3144 #define MERGE(field) dst->field += src->field; src->field=0
3145 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3146 MERGE(me.scene_change_score);
3147 MERGE(me.mc_mb_var_sum_temp);
3148 MERGE(me.mb_var_sum_temp);
3151 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3154 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3155 MERGE(dct_count[1]);
3165 MERGE(padding_bug_score);
3166 MERGE(current_picture.f.error[0]);
3167 MERGE(current_picture.f.error[1]);
3168 MERGE(current_picture.f.error[2]);
3170 if(dst->avctx->noise_reduction){
3171 for(i=0; i<64; i++){
3172 MERGE(dct_error_sum[0][i]);
3173 MERGE(dct_error_sum[1][i]);
3177 assert(put_bits_count(&src->pb) % 8 ==0);
3178 assert(put_bits_count(&dst->pb) % 8 ==0);
3179 avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3180 flush_put_bits(&dst->pb);
3183 static int estimate_qp(MpegEncContext *s, int dry_run){
3184 if (s->next_lambda){
3185 s->current_picture_ptr->f.quality =
3186 s->current_picture.f.quality = s->next_lambda;
3187 if(!dry_run) s->next_lambda= 0;
3188 } else if (!s->fixed_qscale) {
3189 s->current_picture_ptr->f.quality =
3190 s->current_picture.f.quality = ff_rate_estimate_qscale(s, dry_run);
3191 if (s->current_picture.f.quality < 0)
3195 if(s->adaptive_quant){
3196 switch(s->codec_id){
3197 case AV_CODEC_ID_MPEG4:
3198 if (CONFIG_MPEG4_ENCODER)
3199 ff_clean_mpeg4_qscales(s);
3201 case AV_CODEC_ID_H263:
3202 case AV_CODEC_ID_H263P:
3203 case AV_CODEC_ID_FLV1:
3204 if (CONFIG_H263_ENCODER)
3205 ff_clean_h263_qscales(s);
3208 ff_init_qscale_tab(s);
3211 s->lambda= s->lambda_table[0];
3214 s->lambda = s->current_picture.f.quality;
3219 /* must be called before writing the header */
3220 static void set_frame_distances(MpegEncContext * s){
3221 assert(s->current_picture_ptr->f.pts != AV_NOPTS_VALUE);
3222 s->time = s->current_picture_ptr->f.pts * s->avctx->time_base.num;
3224 if(s->pict_type==AV_PICTURE_TYPE_B){
3225 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3226 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3228 s->pp_time= s->time - s->last_non_b_time;
3229 s->last_non_b_time= s->time;
3230 assert(s->picture_number==0 || s->pp_time > 0);
3234 static int encode_picture(MpegEncContext *s, int picture_number)
3238 int context_count = s->slice_context_count;
3240 s->picture_number = picture_number;
3242 /* Reset the average MB variance */
3243 s->me.mb_var_sum_temp =
3244 s->me.mc_mb_var_sum_temp = 0;
3246 /* we need to initialize some time vars before we can encode b-frames */
3247 // RAL: Condition added for MPEG1VIDEO
3248 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3249 set_frame_distances(s);
3250 if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3251 ff_set_mpeg4_time(s);
3253 s->me.scene_change_score=0;
3255 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3257 if(s->pict_type==AV_PICTURE_TYPE_I){
3258 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3259 else s->no_rounding=0;
3260 }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3261 if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3262 s->no_rounding ^= 1;
3265 if(s->flags & CODEC_FLAG_PASS2){
3266 if (estimate_qp(s,1) < 0)
3268 ff_get_2pass_fcode(s);
3269 }else if(!(s->flags & CODEC_FLAG_QSCALE)){
3270 if(s->pict_type==AV_PICTURE_TYPE_B)
3271 s->lambda= s->last_lambda_for[s->pict_type];
3273 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3277 if(s->codec_id != AV_CODEC_ID_AMV){
3278 if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
3279 if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
3280 s->q_chroma_intra_matrix = s->q_intra_matrix;
3281 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
3284 s->mb_intra=0; //for the rate distortion & bit compare functions
3285 for(i=1; i<context_count; i++){
3286 ret = ff_update_duplicate_context(s->thread_context[i], s);
3294 /* Estimate motion for every MB */
3295 if(s->pict_type != AV_PICTURE_TYPE_I){
3296 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3297 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
3298 if(s->pict_type != AV_PICTURE_TYPE_B && s->avctx->me_threshold==0){
3299 if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
3300 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3304 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3305 }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3307 for(i=0; i<s->mb_stride*s->mb_height; i++)
3308 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3310 if(!s->fixed_qscale){
3311 /* finding spatial complexity for I-frame rate control */
3312 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3315 for(i=1; i<context_count; i++){
3316 merge_context_after_me(s, s->thread_context[i]);
3318 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3319 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
3322 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
3323 s->pict_type= AV_PICTURE_TYPE_I;
3324 for(i=0; i<s->mb_stride*s->mb_height; i++)
3325 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3326 if(s->msmpeg4_version >= 3)
3328 av_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
3329 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3333 if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3334 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3336 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3338 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3339 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3340 s->f_code= FFMAX3(s->f_code, a, b);
3343 ff_fix_long_p_mvs(s);
3344 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3345 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3349 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3350 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3355 if(s->pict_type==AV_PICTURE_TYPE_B){
3358 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3359 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3360 s->f_code = FFMAX(a, b);
3362 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3363 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3364 s->b_code = FFMAX(a, b);
3366 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3367 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3368 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3369 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3370 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3372 for(dir=0; dir<2; dir++){
3375 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3376 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3377 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3378 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3386 if (estimate_qp(s, 0) < 0)
3389 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==AV_PICTURE_TYPE_I && !(s->flags & CODEC_FLAG_QSCALE))
3390 s->qscale= 3; //reduce clipping problems
3392 if (s->out_format == FMT_MJPEG) {
3393 /* for mjpeg, we do include qscale in the matrix */
3395 int j= s->dsp.idct_permutation[i];
3397 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3399 s->y_dc_scale_table=
3400 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3401 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3402 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
3403 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3406 if(s->codec_id == AV_CODEC_ID_AMV){
3407 static const uint8_t y[32]={13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
3408 static const uint8_t c[32]={14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14};
3410 int j= s->dsp.idct_permutation[ff_zigzag_direct[i]];
3412 s->intra_matrix[j] = sp5x_quant_table[5*2+0][i];
3413 s->chroma_intra_matrix[j] = sp5x_quant_table[5*2+1][i];
3415 s->y_dc_scale_table= y;
3416 s->c_dc_scale_table= c;
3417 s->intra_matrix[0] = 13;
3418 s->chroma_intra_matrix[0] = 14;
3419 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
3420 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3421 ff_convert_matrix(&s->dsp, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3422 s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3426 //FIXME var duplication
3427 s->current_picture_ptr->f.key_frame =
3428 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3429 s->current_picture_ptr->f.pict_type =
3430 s->current_picture.f.pict_type = s->pict_type;
3432 if (s->current_picture.f.key_frame)
3433 s->picture_in_gop_number=0;
3435 s->mb_x = s->mb_y = 0;
3436 s->last_bits= put_bits_count(&s->pb);
3437 switch(s->out_format) {
3439 if (CONFIG_MJPEG_ENCODER)
3440 ff_mjpeg_encode_picture_header(s);
3443 if (CONFIG_H261_ENCODER)
3444 ff_h261_encode_picture_header(s, picture_number);
3447 if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3448 ff_wmv2_encode_picture_header(s, picture_number);
3449 else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3450 ff_msmpeg4_encode_picture_header(s, picture_number);
3451 else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3452 ff_mpeg4_encode_picture_header(s, picture_number);
3453 else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10)
3454 ff_rv10_encode_picture_header(s, picture_number);
3455 else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3456 ff_rv20_encode_picture_header(s, picture_number);
3457 else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3458 ff_flv_encode_picture_header(s, picture_number);
3459 else if (CONFIG_H263_ENCODER)
3460 ff_h263_encode_picture_header(s, picture_number);
3463 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3464 ff_mpeg1_encode_picture_header(s, picture_number);
3471 bits= put_bits_count(&s->pb);
3472 s->header_bits= bits - s->last_bits;
3474 for(i=1; i<context_count; i++){
3475 update_duplicate_context_after_me(s->thread_context[i], s);
3477 s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3478 for(i=1; i<context_count; i++){
3479 merge_context_after_encode(s, s->thread_context[i]);
3485 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3486 const int intra= s->mb_intra;
3489 s->dct_count[intra]++;
3491 for(i=0; i<64; i++){
3492 int level= block[i];
3496 s->dct_error_sum[intra][i] += level;
3497 level -= s->dct_offset[intra][i];
3498 if(level<0) level=0;
3500 s->dct_error_sum[intra][i] -= level;
3501 level += s->dct_offset[intra][i];
3502 if(level>0) level=0;
3509 static int dct_quantize_trellis_c(MpegEncContext *s,
3510 int16_t *block, int n,
3511 int qscale, int *overflow){
3513 const uint8_t *scantable= s->intra_scantable.scantable;
3514 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3516 unsigned int threshold1, threshold2;
3528 int coeff_count[64];
3529 int qmul, qadd, start_i, last_non_zero, i, dc;
3530 const int esc_length= s->ac_esc_length;
3532 uint8_t * last_length;
3533 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3535 s->dsp.fdct (block);
3537 if(s->dct_error_sum)
3538 s->denoise_dct(s, block);
3540 qadd= ((qscale-1)|1)*8;
3551 /* For AIC we skip quant/dequant of INTRADC */
3556 /* note: block[0] is assumed to be positive */
3557 block[0] = (block[0] + (q >> 1)) / q;
3560 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3561 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3562 bias= 1<<(QMAT_SHIFT-1);
3563 length = s->intra_ac_vlc_length;
3564 last_length= s->intra_ac_vlc_last_length;
3568 qmat = s->q_inter_matrix[qscale];
3569 length = s->inter_ac_vlc_length;
3570 last_length= s->inter_ac_vlc_last_length;
3574 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3575 threshold2= (threshold1<<1);
3577 for(i=63; i>=start_i; i--) {
3578 const int j = scantable[i];
3579 int level = block[j] * qmat[j];
3581 if(((unsigned)(level+threshold1))>threshold2){
3587 for(i=start_i; i<=last_non_zero; i++) {
3588 const int j = scantable[i];
3589 int level = block[j] * qmat[j];
3591 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3592 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3593 if(((unsigned)(level+threshold1))>threshold2){
3595 level= (bias + level)>>QMAT_SHIFT;
3597 coeff[1][i]= level-1;
3598 // coeff[2][k]= level-2;
3600 level= (bias - level)>>QMAT_SHIFT;
3601 coeff[0][i]= -level;
3602 coeff[1][i]= -level+1;
3603 // coeff[2][k]= -level+2;
3605 coeff_count[i]= FFMIN(level, 2);
3606 av_assert2(coeff_count[i]);
3609 coeff[0][i]= (level>>31)|1;
3614 *overflow= s->max_qcoeff < max; //overflow might have happened
3616 if(last_non_zero < start_i){
3617 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3618 return last_non_zero;
3621 score_tab[start_i]= 0;
3622 survivor[0]= start_i;
3625 for(i=start_i; i<=last_non_zero; i++){
3626 int level_index, j, zero_distortion;
3627 int dct_coeff= FFABS(block[ scantable[i] ]);
3628 int best_score=256*256*256*120;
3630 if (s->dsp.fdct == ff_fdct_ifast)
3631 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3632 zero_distortion= dct_coeff*dct_coeff;
3634 for(level_index=0; level_index < coeff_count[i]; level_index++){
3636 int level= coeff[level_index][i];
3637 const int alevel= FFABS(level);
3642 if(s->out_format == FMT_H263){
3643 unquant_coeff= alevel*qmul + qadd;
3645 j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
3647 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
3648 unquant_coeff = (unquant_coeff - 1) | 1;
3650 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3651 unquant_coeff = (unquant_coeff - 1) | 1;
3656 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3658 if((level&(~127)) == 0){
3659 for(j=survivor_count-1; j>=0; j--){
3660 int run= i - survivor[j];
3661 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3662 score += score_tab[i-run];
3664 if(score < best_score){
3667 level_tab[i+1]= level-64;
3671 if(s->out_format == FMT_H263){
3672 for(j=survivor_count-1; j>=0; j--){
3673 int run= i - survivor[j];
3674 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3675 score += score_tab[i-run];
3676 if(score < last_score){
3679 last_level= level-64;
3685 distortion += esc_length*lambda;
3686 for(j=survivor_count-1; j>=0; j--){
3687 int run= i - survivor[j];
3688 int score= distortion + score_tab[i-run];
3690 if(score < best_score){
3693 level_tab[i+1]= level-64;
3697 if(s->out_format == FMT_H263){
3698 for(j=survivor_count-1; j>=0; j--){
3699 int run= i - survivor[j];
3700 int score= distortion + score_tab[i-run];
3701 if(score < last_score){
3704 last_level= level-64;
3712 score_tab[i+1]= best_score;
3714 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3715 if(last_non_zero <= 27){
3716 for(; survivor_count; survivor_count--){
3717 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3721 for(; survivor_count; survivor_count--){
3722 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3727 survivor[ survivor_count++ ]= i+1;
3730 if(s->out_format != FMT_H263){
3731 last_score= 256*256*256*120;
3732 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3733 int score= score_tab[i];
3734 if(i) score += lambda*2; //FIXME exacter?
3736 if(score < last_score){
3739 last_level= level_tab[i];
3740 last_run= run_tab[i];
3745 s->coded_score[n] = last_score;
3747 dc= FFABS(block[0]);
3748 last_non_zero= last_i - 1;
3749 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3751 if(last_non_zero < start_i)
3752 return last_non_zero;
3754 if(last_non_zero == 0 && start_i == 0){
3756 int best_score= dc * dc;
3758 for(i=0; i<coeff_count[0]; i++){
3759 int level= coeff[i][0];
3760 int alevel= FFABS(level);
3761 int unquant_coeff, score, distortion;
3763 if(s->out_format == FMT_H263){
3764 unquant_coeff= (alevel*qmul + qadd)>>3;
3766 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3767 unquant_coeff = (unquant_coeff - 1) | 1;
3769 unquant_coeff = (unquant_coeff + 4) >> 3;
3770 unquant_coeff<<= 3 + 3;
3772 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3774 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3775 else score= distortion + esc_length*lambda;
3777 if(score < best_score){
3779 best_level= level - 64;
3782 block[0]= best_level;
3783 s->coded_score[n] = best_score - dc*dc;
3784 if(best_level == 0) return -1;
3785 else return last_non_zero;
3789 av_assert2(last_level);
3791 block[ perm_scantable[last_non_zero] ]= last_level;
3794 for(; i>start_i; i -= run_tab[i] + 1){
3795 block[ perm_scantable[i-1] ]= level_tab[i];
3798 return last_non_zero;
3801 //#define REFINE_STATS 1
3802 static int16_t basis[64][64];
3804 static void build_basis(uint8_t *perm){
3811 double s= 0.25*(1<<BASIS_SHIFT);
3813 int perm_index= perm[index];
3814 if(i==0) s*= sqrt(0.5);
3815 if(j==0) s*= sqrt(0.5);
3816 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)));
3823 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3824 int16_t *block, int16_t *weight, int16_t *orig,
3827 LOCAL_ALIGNED_16(int16_t, d1, [64]);
3828 const uint8_t *scantable= s->intra_scantable.scantable;
3829 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3830 // unsigned int threshold1, threshold2;
3835 int qmul, qadd, start_i, last_non_zero, i, dc;
3837 uint8_t * last_length;
3839 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3842 static int after_last=0;
3843 static int to_zero=0;
3844 static int from_zero=0;
3847 static int messed_sign=0;
3850 if(basis[0][0] == 0)
3851 build_basis(s->dsp.idct_permutation);
3862 /* For AIC we skip quant/dequant of INTRADC */
3866 q <<= RECON_SHIFT-3;
3867 /* note: block[0] is assumed to be positive */
3869 // block[0] = (block[0] + (q >> 1)) / q;
3871 // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3872 // bias= 1<<(QMAT_SHIFT-1);
3873 length = s->intra_ac_vlc_length;
3874 last_length= s->intra_ac_vlc_last_length;
3878 length = s->inter_ac_vlc_length;
3879 last_length= s->inter_ac_vlc_last_length;
3881 last_non_zero = s->block_last_index[n];
3886 dc += (1<<(RECON_SHIFT-1));
3887 for(i=0; i<64; i++){
3888 rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
3891 STOP_TIMER("memset rem[]")}
3894 for(i=0; i<64; i++){
3899 w= FFABS(weight[i]) + qns*one;
3900 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3903 // w=weight[i] = (63*qns + (w/2)) / w;
3906 av_assert2(w<(1<<6));
3909 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3915 for(i=start_i; i<=last_non_zero; i++){
3916 int j= perm_scantable[i];
3917 const int level= block[j];
3921 if(level<0) coeff= qmul*level - qadd;
3922 else coeff= qmul*level + qadd;
3923 run_tab[rle_index++]=run;
3926 s->dsp.add_8x8basis(rem, basis[j], coeff);
3932 if(last_non_zero>0){
3933 STOP_TIMER("init rem[]")
3940 int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
3943 int run2, best_unquant_change=0, analyze_gradient;
3947 analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
3949 if(analyze_gradient){
3953 for(i=0; i<64; i++){
3956 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
3959 STOP_TIMER("rem*w*w")}
3969 const int level= block[0];
3970 int change, old_coeff;
3972 av_assert2(s->mb_intra);
3976 for(change=-1; change<=1; change+=2){
3977 int new_level= level + change;
3978 int score, new_coeff;
3980 new_coeff= q*new_level;
3981 if(new_coeff >= 2048 || new_coeff < 0)
3984 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3985 if(score<best_score){
3988 best_change= change;
3989 best_unquant_change= new_coeff - old_coeff;
3996 run2= run_tab[rle_index++];
4000 for(i=start_i; i<64; i++){
4001 int j= perm_scantable[i];
4002 const int level= block[j];
4003 int change, old_coeff;
4005 if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4009 if(level<0) old_coeff= qmul*level - qadd;
4010 else old_coeff= qmul*level + qadd;
4011 run2= run_tab[rle_index++]; //FIXME ! maybe after last
4015 av_assert2(run2>=0 || i >= last_non_zero );
4018 for(change=-1; change<=1; change+=2){
4019 int new_level= level + change;
4020 int score, new_coeff, unquant_change;
4023 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4027 if(new_level<0) new_coeff= qmul*new_level - qadd;
4028 else new_coeff= qmul*new_level + qadd;
4029 if(new_coeff >= 2048 || new_coeff <= -2048)
4031 //FIXME check for overflow
4034 if(level < 63 && level > -63){
4035 if(i < last_non_zero)
4036 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
4037 - length[UNI_AC_ENC_INDEX(run, level+64)];
4039 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4040 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4043 av_assert2(FFABS(new_level)==1);
4045 if(analyze_gradient){
4046 int g= d1[ scantable[i] ];
4047 if(g && (g^new_level) >= 0)
4051 if(i < last_non_zero){
4052 int next_i= i + run2 + 1;
4053 int next_level= block[ perm_scantable[next_i] ] + 64;
4055 if(next_level&(~127))
4058 if(next_i < last_non_zero)
4059 score += length[UNI_AC_ENC_INDEX(run, 65)]
4060 + length[UNI_AC_ENC_INDEX(run2, next_level)]
4061 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4063 score += length[UNI_AC_ENC_INDEX(run, 65)]
4064 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4065 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4067 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4069 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4070 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4076 av_assert2(FFABS(level)==1);
4078 if(i < last_non_zero){
4079 int next_i= i + run2 + 1;
4080 int next_level= block[ perm_scantable[next_i] ] + 64;
4082 if(next_level&(~127))
4085 if(next_i < last_non_zero)
4086 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4087 - length[UNI_AC_ENC_INDEX(run2, next_level)]
4088 - length[UNI_AC_ENC_INDEX(run, 65)];
4090 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4091 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4092 - length[UNI_AC_ENC_INDEX(run, 65)];
4094 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4096 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4097 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4104 unquant_change= new_coeff - old_coeff;
4105 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4107 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
4108 if(score<best_score){
4111 best_change= change;
4112 best_unquant_change= unquant_change;
4116 prev_level= level + 64;
4117 if(prev_level&(~127))
4126 STOP_TIMER("iterative step")}
4130 int j= perm_scantable[ best_coeff ];
4132 block[j] += best_change;
4134 if(best_coeff > last_non_zero){
4135 last_non_zero= best_coeff;
4136 av_assert2(block[j]);
4143 if(block[j] - best_change){
4144 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
4156 for(; last_non_zero>=start_i; last_non_zero--){
4157 if(block[perm_scantable[last_non_zero]])
4163 if(256*256*256*64 % count == 0){
4164 av_log(s->avctx, AV_LOG_DEBUG, "after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
4169 for(i=start_i; i<=last_non_zero; i++){
4170 int j= perm_scantable[i];
4171 const int level= block[j];
4174 run_tab[rle_index++]=run;
4181 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
4187 if(last_non_zero>0){
4188 STOP_TIMER("iterative search")
4193 return last_non_zero;
4196 int ff_dct_quantize_c(MpegEncContext *s,
4197 int16_t *block, int n,
4198 int qscale, int *overflow)
4200 int i, j, level, last_non_zero, q, start_i;
4202 const uint8_t *scantable= s->intra_scantable.scantable;
4205 unsigned int threshold1, threshold2;
4207 s->dsp.fdct (block);
4209 if(s->dct_error_sum)
4210 s->denoise_dct(s, block);
4220 /* For AIC we skip quant/dequant of INTRADC */
4223 /* note: block[0] is assumed to be positive */
4224 block[0] = (block[0] + (q >> 1)) / q;
4227 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
4228 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4232 qmat = s->q_inter_matrix[qscale];
4233 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
4235 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4236 threshold2= (threshold1<<1);
4237 for(i=63;i>=start_i;i--) {
4239 level = block[j] * qmat[j];
4241 if(((unsigned)(level+threshold1))>threshold2){
4248 for(i=start_i; i<=last_non_zero; i++) {
4250 level = block[j] * qmat[j];
4252 // if( bias+level >= (1<<QMAT_SHIFT)
4253 // || bias-level >= (1<<QMAT_SHIFT)){
4254 if(((unsigned)(level+threshold1))>threshold2){
4256 level= (bias + level)>>QMAT_SHIFT;
4259 level= (bias - level)>>QMAT_SHIFT;
4267 *overflow= s->max_qcoeff < max; //overflow might have happened
4269 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4270 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
4271 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
4273 return last_non_zero;
4276 #define OFFSET(x) offsetof(MpegEncContext, x)
4277 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4278 static const AVOption h263_options[] = {
4279 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4280 { "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},
4281 { "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 },
4286 static const AVClass h263_class = {
4287 .class_name = "H.263 encoder",
4288 .item_name = av_default_item_name,
4289 .option = h263_options,
4290 .version = LIBAVUTIL_VERSION_INT,
4293 AVCodec ff_h263_encoder = {
4295 .type = AVMEDIA_TYPE_VIDEO,
4296 .id = AV_CODEC_ID_H263,
4297 .priv_data_size = sizeof(MpegEncContext),
4298 .init = ff_MPV_encode_init,
4299 .encode2 = ff_MPV_encode_picture,
4300 .close = ff_MPV_encode_end,
4301 .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4302 .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4303 .priv_class = &h263_class,
4306 static const AVOption h263p_options[] = {
4307 { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4308 { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4309 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
4310 { "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},
4314 static const AVClass h263p_class = {
4315 .class_name = "H.263p encoder",
4316 .item_name = av_default_item_name,
4317 .option = h263p_options,
4318 .version = LIBAVUTIL_VERSION_INT,
4321 AVCodec ff_h263p_encoder = {
4323 .type = AVMEDIA_TYPE_VIDEO,
4324 .id = AV_CODEC_ID_H263P,
4325 .priv_data_size = sizeof(MpegEncContext),
4326 .init = ff_MPV_encode_init,
4327 .encode2 = ff_MPV_encode_picture,
4328 .close = ff_MPV_encode_end,
4329 .capabilities = CODEC_CAP_SLICE_THREADS,
4330 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4331 .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4332 .priv_class = &h263p_class,
4335 FF_MPV_GENERIC_CLASS(msmpeg4v2)
4337 AVCodec ff_msmpeg4v2_encoder = {
4338 .name = "msmpeg4v2",
4339 .type = AVMEDIA_TYPE_VIDEO,
4340 .id = AV_CODEC_ID_MSMPEG4V2,
4341 .priv_data_size = sizeof(MpegEncContext),
4342 .init = ff_MPV_encode_init,
4343 .encode2 = ff_MPV_encode_picture,
4344 .close = ff_MPV_encode_end,
4345 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4346 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
4347 .priv_class = &msmpeg4v2_class,
4350 FF_MPV_GENERIC_CLASS(msmpeg4v3)
4352 AVCodec ff_msmpeg4v3_encoder = {
4354 .type = AVMEDIA_TYPE_VIDEO,
4355 .id = AV_CODEC_ID_MSMPEG4V3,
4356 .priv_data_size = sizeof(MpegEncContext),
4357 .init = ff_MPV_encode_init,
4358 .encode2 = ff_MPV_encode_picture,
4359 .close = ff_MPV_encode_end,
4360 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4361 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
4362 .priv_class = &msmpeg4v3_class,
4365 FF_MPV_GENERIC_CLASS(wmv1)
4367 AVCodec ff_wmv1_encoder = {
4369 .type = AVMEDIA_TYPE_VIDEO,
4370 .id = AV_CODEC_ID_WMV1,
4371 .priv_data_size = sizeof(MpegEncContext),
4372 .init = ff_MPV_encode_init,
4373 .encode2 = ff_MPV_encode_picture,
4374 .close = ff_MPV_encode_end,
4375 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4376 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
4377 .priv_class = &wmv1_class,