avpacket: Add a function for shrinking already allocated side data
[libav.git] / libavcodec / mpegvideo_enc.c
CommitLineData
3ada94ba
BF
1/*
2 * The simplest mpeg encoder (well, it was the simplest!)
406792e7 3 * Copyright (c) 2000,2001 Fabrice Bellard
3ada94ba
BF
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
7b94177e
DB
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
2912e87a 8 * This file is part of Libav.
3ada94ba 9 *
2912e87a 10 * Libav is free software; you can redistribute it and/or
3ada94ba
BF
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.
14 *
2912e87a 15 * Libav is distributed in the hope that it will be useful,
3ada94ba
BF
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.
19 *
20 * You should have received a copy of the GNU Lesser General Public
2912e87a 21 * License along with Libav; if not, write to the Free Software
3ada94ba 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3ada94ba
BF
23 */
24
25/**
ba87f080 26 * @file
3ada94ba
BF
27 * The simplest mpeg encoder (well, it was the simplest!).
28 */
29
94ca624f 30#include "libavutil/intmath.h"
0ebcdf5c 31#include "libavutil/mathematics.h"
9bb2d1a3 32#include "libavutil/opt.h"
3ada94ba
BF
33#include "avcodec.h"
34#include "dsputil.h"
35#include "mpegvideo.h"
36#include "mpegvideo_common.h"
c46eeae2 37#include "h263.h"
3ada94ba
BF
38#include "mjpegenc.h"
39#include "msmpeg4.h"
40#include "faandct.h"
6a9c8594 41#include "thread.h"
8b22017f 42#include "aandcttab.h"
eb523769 43#include "flv.h"
ca334dd1 44#include "mpeg4video.h"
7ffd8332 45#include "internal.h"
3ada94ba
BF
46#include <limits.h>
47
48//#undef NDEBUG
49//#include <assert.h>
50
51static int encode_picture(MpegEncContext *s, int picture_number);
52static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
53static int sse_mb(MpegEncContext *s);
1d4da6a4
DEP
54static void denoise_dct_c(MpegEncContext *s, DCTELEM *block);
55static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
3ada94ba
BF
56
57/* enable all paranoid tests for rounding, overflows, etc... */
58//#define PARANOID
59
60//#define DEBUG
61
bd96be6e
AD
62static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
63static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
3ada94ba 64
ed019b8e
AK
65const AVOption ff_mpv_generic_options[] = {
66 FF_MPV_COMMON_OPTS
67 { NULL },
68};
69
bd96be6e
AD
70void 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)
3ada94ba
BF
74{
75 int qscale;
bd96be6e 76 int shift = 0;
3ada94ba 77
bd96be6e 78 for (qscale = qmin; qscale <= qmax; qscale++) {
3ada94ba 79 int i;
0a72533e
MR
80 if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
81 dsp->fdct == ff_jpeg_fdct_islow_10
3ada94ba
BF
82#ifdef FAAN_POSTSCALE
83 || dsp->fdct == ff_faandct
84#endif
85 ) {
bd96be6e
AD
86 for (i = 0; i < 64; i++) {
87 const int j = dsp->idct_permutation[i];
88 /* 16 <= qscale * quant_matrix[i] <= 7905
89 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
90 * 19952 <= x <= 249205026
91 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
92 * 3444240 >= (1 << 36) / (x) >= 275 */
3ada94ba
BF
93
94 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
bd96be6e 95 (qscale * quant_matrix[j]));
3ada94ba 96 }
3e2efacd 97 } else if (dsp->fdct == ff_fdct_ifast
3ada94ba
BF
98#ifndef FAAN_POSTSCALE
99 || dsp->fdct == ff_faandct
100#endif
101 ) {
bd96be6e
AD
102 for (i = 0; i < 64; i++) {
103 const int j = dsp->idct_permutation[i];
104 /* 16 <= qscale * quant_matrix[i] <= 7905
105 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
106 * 19952 <= x <= 249205026
107 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
108 * 3444240 >= (1 << 36) / (x) >= 275 */
3ada94ba
BF
109
110 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
bd96be6e
AD
111 (ff_aanscales[i] * qscale *
112 quant_matrix[j]));
3ada94ba
BF
113 }
114 } else {
bd96be6e
AD
115 for (i = 0; i < 64; i++) {
116 const int j = dsp->idct_permutation[i];
3ada94ba 117 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
bd96be6e
AD
118 * Assume x = qscale * quant_matrix[i]
119 * So 16 <= x <= 7905
120 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
121 * so 32768 >= (1 << 19) / (x) >= 67 */
122 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
123 (qscale * quant_matrix[j]));
124 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
125 // (qscale * quant_matrix[i]);
126 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
127 (qscale * quant_matrix[j]);
128
129 if (qmat16[qscale][0][i] == 0 ||
130 qmat16[qscale][0][i] == 128 * 256)
131 qmat16[qscale][0][i] = 128 * 256 - 1;
132 qmat16[qscale][1][i] =
133 ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
134 qmat16[qscale][0][i]);
3ada94ba
BF
135 }
136 }
137
bd96be6e
AD
138 for (i = intra; i < 64; i++) {
139 int64_t max = 8191;
3e2efacd 140 if (dsp->fdct == ff_fdct_ifast
3ada94ba 141#ifndef FAAN_POSTSCALE
bd96be6e 142 || dsp->fdct == ff_faandct
3ada94ba 143#endif
bd96be6e
AD
144 ) {
145 max = (8191LL * ff_aanscales[i]) >> 14;
3ada94ba 146 }
bd96be6e 147 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
3ada94ba
BF
148 shift++;
149 }
150 }
151 }
bd96be6e
AD
152 if (shift) {
153 av_log(NULL, AV_LOG_INFO,
154 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
155 QMAT_SHIFT - shift);
3ada94ba
BF
156 }
157}
158
bd96be6e
AD
159static inline void update_qscale(MpegEncContext *s)
160{
161 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
162 (FF_LAMBDA_SHIFT + 7);
163 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
3ada94ba 164
bd96be6e
AD
165 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
166 FF_LAMBDA_SHIFT;
3ada94ba
BF
167}
168
bd96be6e
AD
169void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
170{
3ada94ba
BF
171 int i;
172
bd96be6e 173 if (matrix) {
3ada94ba 174 put_bits(pb, 1, 1);
bd96be6e
AD
175 for (i = 0; i < 64; i++) {
176 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
3ada94ba 177 }
bd96be6e 178 } else
3ada94ba
BF
179 put_bits(pb, 1, 0);
180}
181
9015b095
MN
182/**
183 * init s->current_picture.qscale_table from s->lambda_table
184 */
bd96be6e
AD
185void ff_init_qscale_tab(MpegEncContext *s)
186{
657ccb5a 187 int8_t * const qscale_table = s->current_picture.f.qscale_table;
9015b095
MN
188 int i;
189
bd96be6e
AD
190 for (i = 0; i < s->mb_num; i++) {
191 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
192 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
193 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
194 s->avctx->qmax);
9015b095
MN
195 }
196}
197
bd96be6e
AD
198static void copy_picture_attributes(MpegEncContext *s,
199 AVFrame *dst,
200 AVFrame *src)
201{
3ada94ba
BF
202 int i;
203
204 dst->pict_type = src->pict_type;
205 dst->quality = src->quality;
206 dst->coded_picture_number = src->coded_picture_number;
207 dst->display_picture_number = src->display_picture_number;
bd96be6e 208 //dst->reference = src->reference;
3ada94ba
BF
209 dst->pts = src->pts;
210 dst->interlaced_frame = src->interlaced_frame;
211 dst->top_field_first = src->top_field_first;
212
bd96be6e
AD
213 if (s->avctx->me_threshold) {
214 if (!src->motion_val[0])
3ada94ba 215 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
bd96be6e 216 if (!src->mb_type)
3ada94ba 217 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
bd96be6e 218 if (!src->ref_index[0])
3ada94ba 219 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
bd96be6e
AD
220 if (src->motion_subsample_log2 != dst->motion_subsample_log2)
221 av_log(s->avctx, AV_LOG_ERROR,
222 "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
223 src->motion_subsample_log2, dst->motion_subsample_log2);
224
225 memcpy(dst->mb_type, src->mb_type,
226 s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
227
228 for (i = 0; i < 2; i++) {
229 int stride = ((16 * s->mb_width ) >>
230 src->motion_subsample_log2) + 1;
231 int height = ((16 * s->mb_height) >> src->motion_subsample_log2);
232
233 if (src->motion_val[i] &&
234 src->motion_val[i] != dst->motion_val[i]) {
235 memcpy(dst->motion_val[i], src->motion_val[i],
236 2 * stride * height * sizeof(int16_t));
3ada94ba 237 }
bd96be6e
AD
238 if (src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]) {
239 memcpy(dst->ref_index[i], src->ref_index[i],
240 s->mb_stride * 4 * s->mb_height * sizeof(int8_t));
3ada94ba
BF
241 }
242 }
243 }
244}
245
bd96be6e
AD
246static void update_duplicate_context_after_me(MpegEncContext *dst,
247 MpegEncContext *src)
248{
3ada94ba
BF
249#define COPY(a) dst->a= src->a
250 COPY(pict_type);
251 COPY(current_picture);
252 COPY(f_code);
253 COPY(b_code);
254 COPY(qscale);
255 COPY(lambda);
256 COPY(lambda2);
257 COPY(picture_in_gop_number);
258 COPY(gop_picture_number);
bd96be6e
AD
259 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
260 COPY(progressive_frame); // FIXME don't set in encode_header
261 COPY(partitioned_frame); // FIXME don't set in encode_header
3ada94ba
BF
262#undef COPY
263}
264
265/**
58c42af7 266 * Set the given MpegEncContext to defaults for encoding.
3ada94ba
BF
267 * the changed fields will not depend upon the prior state of the MpegEncContext.
268 */
bd96be6e
AD
269static void MPV_encode_defaults(MpegEncContext *s)
270{
3ada94ba 271 int i;
efd29844 272 ff_MPV_common_defaults(s);
3ada94ba 273
bd96be6e
AD
274 for (i = -16; i < 16; i++) {
275 default_fcode_tab[i + MAX_MV] = 1;
3ada94ba 276 }
bd96be6e
AD
277 s->me.mv_penalty = default_mv_penalty;
278 s->fcode_tab = default_fcode_tab;
3ada94ba
BF
279}
280
281/* init video encoder */
efd29844 282av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
3ada94ba
BF
283{
284 MpegEncContext *s = avctx->priv_data;
285 int i;
286 int chroma_h_shift, chroma_v_shift;
287
288 MPV_encode_defaults(s);
289
290 switch (avctx->codec_id) {
291 case CODEC_ID_MPEG2VIDEO:
bd96be6e
AD
292 if (avctx->pix_fmt != PIX_FMT_YUV420P &&
293 avctx->pix_fmt != PIX_FMT_YUV422P) {
294 av_log(avctx, AV_LOG_ERROR,
295 "only YUV420 and YUV422 are supported\n");
3ada94ba
BF
296 return -1;
297 }
298 break;
299 case CODEC_ID_LJPEG:
bd96be6e
AD
300 if (avctx->pix_fmt != PIX_FMT_YUVJ420P &&
301 avctx->pix_fmt != PIX_FMT_YUVJ422P &&
302 avctx->pix_fmt != PIX_FMT_YUVJ444P &&
303 avctx->pix_fmt != PIX_FMT_BGRA &&
304 ((avctx->pix_fmt != PIX_FMT_YUV420P &&
305 avctx->pix_fmt != PIX_FMT_YUV422P &&
306 avctx->pix_fmt != PIX_FMT_YUV444P) ||
307 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
e27bf2c7
MN
308 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
309 return -1;
310 }
311 break;
3ada94ba 312 case CODEC_ID_MJPEG:
bd96be6e
AD
313 if (avctx->pix_fmt != PIX_FMT_YUVJ420P &&
314 avctx->pix_fmt != PIX_FMT_YUVJ422P &&
315 ((avctx->pix_fmt != PIX_FMT_YUV420P &&
316 avctx->pix_fmt != PIX_FMT_YUV422P) ||
317 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
3ada94ba
BF
318 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
319 return -1;
320 }
321 break;
322 default:
bd96be6e 323 if (avctx->pix_fmt != PIX_FMT_YUV420P) {
3ada94ba
BF
324 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
325 return -1;
326 }
327 }
328
329 switch (avctx->pix_fmt) {
330 case PIX_FMT_YUVJ422P:
331 case PIX_FMT_YUV422P:
332 s->chroma_format = CHROMA_422;
333 break;
334 case PIX_FMT_YUVJ420P:
335 case PIX_FMT_YUV420P:
336 default:
337 s->chroma_format = CHROMA_420;
338 break;
339 }
340
341 s->bit_rate = avctx->bit_rate;
bd96be6e
AD
342 s->width = avctx->width;
343 s->height = avctx->height;
344 if (avctx->gop_size > 600 &&
345 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
346 av_log(avctx, AV_LOG_ERROR,
347 "Warning keyframe interval too large! reducing it ...\n");
348 avctx->gop_size = 600;
349 }
350 s->gop_size = avctx->gop_size;
351 s->avctx = avctx;
352 s->flags = avctx->flags;
353 s->flags2 = avctx->flags2;
354 s->max_b_frames = avctx->max_b_frames;
355 s->codec_id = avctx->codec->id;
63efd83a
AK
356#if FF_API_MPV_GLOBAL_OPTS
357 if (avctx->luma_elim_threshold)
358 s->luma_elim_threshold = avctx->luma_elim_threshold;
359 if (avctx->chroma_elim_threshold)
360 s->chroma_elim_threshold = avctx->chroma_elim_threshold;
361#endif
bd96be6e 362 s->strict_std_compliance = avctx->strict_std_compliance;
bd96be6e
AD
363 s->quarter_sample = (avctx->flags & CODEC_FLAG_QPEL) != 0;
364 s->mpeg_quant = avctx->mpeg_quant;
365 s->rtp_mode = !!avctx->rtp_payload_size;
366 s->intra_dc_precision = avctx->intra_dc_precision;
3ada94ba
BF
367 s->user_specified_pts = AV_NOPTS_VALUE;
368
369 if (s->gop_size <= 1) {
370 s->intra_only = 1;
bd96be6e 371 s->gop_size = 12;
3ada94ba
BF
372 } else {
373 s->intra_only = 0;
374 }
375
376 s->me_method = avctx->me_method;
377
378 /* Fixed QSCALE */
379 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
380
ff71a383
AK
381#if FF_API_MPV_GLOBAL_OPTS
382 if (s->flags & CODEC_FLAG_QP_RD)
383 s->mpv_flags |= FF_MPV_FLAG_QP_RD;
384#endif
385
bd96be6e
AD
386 s->adaptive_quant = (s->avctx->lumi_masking ||
387 s->avctx->dark_masking ||
388 s->avctx->temporal_cplx_masking ||
389 s->avctx->spatial_cplx_masking ||
390 s->avctx->p_masking ||
391 s->avctx->border_masking ||
ff71a383 392 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
bd96be6e 393 !s->fixed_qscale;
3ada94ba 394
bd96be6e 395 s->loop_filter = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
3ada94ba 396
bd96be6e
AD
397 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
398 av_log(avctx, AV_LOG_ERROR,
399 "a vbv buffer size is needed, "
400 "for encoding with a maximum bitrate\n");
3ada94ba
BF
401 return -1;
402 }
403
bd96be6e
AD
404 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
405 av_log(avctx, AV_LOG_INFO,
406 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
3ada94ba
BF
407 }
408
bd96be6e 409 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
3ada94ba
BF
410 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
411 return -1;
412 }
413
bd96be6e 414 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
3ada94ba
BF
415 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
416 return -1;
417 }
418
bd96be6e
AD
419 if (avctx->rc_max_rate &&
420 avctx->rc_max_rate == avctx->bit_rate &&
421 avctx->rc_max_rate != avctx->rc_min_rate) {
422 av_log(avctx, AV_LOG_INFO,
423 "impossible bitrate constraints, this will fail\n");
3dd2a1c5
MN
424 }
425
bd96be6e
AD
426 if (avctx->rc_buffer_size &&
427 avctx->bit_rate * (int64_t)avctx->time_base.num >
428 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
3ada94ba
BF
429 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
430 return -1;
431 }
432
bd96be6e
AD
433 if (!s->fixed_qscale &&
434 avctx->bit_rate * av_q2d(avctx->time_base) >
435 avctx->bit_rate_tolerance) {
436 av_log(avctx, AV_LOG_ERROR,
437 "bitrate tolerance too small for bitrate\n");
3ada94ba
BF
438 return -1;
439 }
440
bd96be6e
AD
441 if (s->avctx->rc_max_rate &&
442 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
443 (s->codec_id == CODEC_ID_MPEG1VIDEO ||
444 s->codec_id == CODEC_ID_MPEG2VIDEO) &&
445 90000LL * (avctx->rc_buffer_size - 1) >
446 s->avctx->rc_max_rate * 0xFFFFLL) {
447 av_log(avctx, AV_LOG_INFO,
448 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
449 "specified vbv buffer is too large for the given bitrate!\n");
3ada94ba
BF
450 }
451
bd96be6e
AD
452 if ((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4 &&
453 s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P &&
454 s->codec_id != CODEC_ID_FLV1) {
3ada94ba
BF
455 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
456 return -1;
457 }
458
bd96be6e
AD
459 if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
460 av_log(avctx, AV_LOG_ERROR,
461 "OBMC is only supported with simple mb decision\n");
3ada94ba
BF
462 return -1;
463 }
464
bd96be6e 465 if (s->quarter_sample && s->codec_id != CODEC_ID_MPEG4) {
3ada94ba
BF
466 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
467 return -1;
468 }
469
bd96be6e
AD
470 if (s->max_b_frames &&
471 s->codec_id != CODEC_ID_MPEG4 &&
472 s->codec_id != CODEC_ID_MPEG1VIDEO &&
473 s->codec_id != CODEC_ID_MPEG2VIDEO) {
3ada94ba
BF
474 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
475 return -1;
476 }
477
bd96be6e
AD
478 if ((s->codec_id == CODEC_ID_MPEG4 ||
479 s->codec_id == CODEC_ID_H263 ||
954a0b48 480 s->codec_id == CODEC_ID_H263P) &&
bd96be6e
AD
481 (avctx->sample_aspect_ratio.num > 255 ||
482 avctx->sample_aspect_ratio.den > 255)) {
483 av_log(avctx, AV_LOG_ERROR,
484 "Invalid pixel aspect ratio %i/%i, limit is 255/255\n",
954a0b48
MN
485 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
486 return -1;
487 }
488
9ce2a91b 489 if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
bd96be6e 490 s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO) {
3ada94ba
BF
491 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
492 return -1;
493 }
494
bd96be6e
AD
495 // FIXME mpeg2 uses that too
496 if (s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4) {
497 av_log(avctx, AV_LOG_ERROR,
498 "mpeg2 style quantization not supported by codec\n");
3ada94ba
BF
499 return -1;
500 }
501
af3d804f
AK
502#if FF_API_MPV_GLOBAL_OPTS
503 if (s->flags & CODEC_FLAG_CBP_RD)
504 s->mpv_flags |= FF_MPV_FLAG_CBP_RD;
505#endif
506
507 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
3ada94ba
BF
508 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
509 return -1;
510 }
511
ff71a383 512 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
bd96be6e 513 s->avctx->mb_decision != FF_MB_DECISION_RD) {
3ada94ba
BF
514 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
515 return -1;
516 }
517
bd96be6e
AD
518 if (s->avctx->scenechange_threshold < 1000000000 &&
519 (s->flags & CODEC_FLAG_CLOSED_GOP)) {
520 av_log(avctx, AV_LOG_ERROR,
521 "closed gop with scene change detection are not supported yet, "
522 "set threshold to 1000000000\n");
3ada94ba
BF
523 return -1;
524 }
525
bd96be6e
AD
526 if (s->flags & CODEC_FLAG_LOW_DELAY) {
527 if (s->codec_id != CODEC_ID_MPEG2VIDEO) {
528 av_log(avctx, AV_LOG_ERROR,
529 "low delay forcing is only available for mpeg2\n");
3ada94ba
BF
530 return -1;
531 }
bd96be6e
AD
532 if (s->max_b_frames != 0) {
533 av_log(avctx, AV_LOG_ERROR,
534 "b frames cannot be used with low delay\n");
3ada94ba
BF
535 return -1;
536 }
537 }
538
bd96be6e 539 if (s->q_scale_type == 1) {
bd96be6e
AD
540 if (avctx->qmax > 12) {
541 av_log(avctx, AV_LOG_ERROR,
542 "non linear quant only supports qmax <= 12 currently\n");
3ada94ba
BF
543 return -1;
544 }
545 }
546
bd96be6e
AD
547 if (s->avctx->thread_count > 1 &&
548 s->codec_id != CODEC_ID_MPEG4 &&
549 s->codec_id != CODEC_ID_MPEG1VIDEO &&
550 s->codec_id != CODEC_ID_MPEG2VIDEO &&
9ce2a91b 551 (s->codec_id != CODEC_ID_H263P)) {
bd96be6e
AD
552 av_log(avctx, AV_LOG_ERROR,
553 "multi threaded encoding not supported by codec\n");
3ada94ba
BF
554 return -1;
555 }
556
bd96be6e
AD
557 if (s->avctx->thread_count < 1) {
558 av_log(avctx, AV_LOG_ERROR,
559 "automatic thread number detection not supported by codec,"
560 "patch welcome\n");
b52b0913
MN
561 return -1;
562 }
563
bd96be6e
AD
564 if (s->avctx->thread_count > 1)
565 s->rtp_mode = 1;
3ada94ba 566
bd96be6e 567 if (!avctx->time_base.den || !avctx->time_base.num) {
3ada94ba
BF
568 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
569 return -1;
570 }
571
bd96be6e
AD
572 i = (INT_MAX / 2 + 128) >> 8;
573 if (avctx->me_threshold >= i) {
574 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n",
575 i - 1);
3ada94ba
BF
576 return -1;
577 }
bd96be6e
AD
578 if (avctx->mb_threshold >= i) {
579 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
580 i - 1);
3ada94ba
BF
581 return -1;
582 }
583
bd96be6e
AD
584 if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
585 av_log(avctx, AV_LOG_INFO,
586 "notice: b_frame_strategy only affects the first pass\n");
3ada94ba
BF
587 avctx->b_frame_strategy = 0;
588 }
589
bd96be6e
AD
590 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
591 if (i > 1) {
3ada94ba
BF
592 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
593 avctx->time_base.den /= i;
594 avctx->time_base.num /= i;
bd96be6e 595 //return -1;
3ada94ba
BF
596 }
597
bd96be6e
AD
598 if (s->mpeg_quant || s->codec_id == CODEC_ID_MPEG1VIDEO ||
599 s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG) {
600 // (a + x * 3 / 8) / x
601 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
602 s->inter_quant_bias = 0;
603 } else {
604 s->intra_quant_bias = 0;
605 // (a - x / 4) / x
606 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
3ada94ba
BF
607 }
608
bd96be6e
AD
609 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
610 s->intra_quant_bias = avctx->intra_quant_bias;
611 if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
612 s->inter_quant_bias = avctx->inter_quant_bias;
613
614 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
615 &chroma_v_shift);
616
617 if (avctx->codec_id == CODEC_ID_MPEG4 &&
618 s->avctx->time_base.den > (1 << 16) - 1) {
619 av_log(avctx, AV_LOG_ERROR,
620 "timebase %d/%d not supported by MPEG 4 standard, "
621 "the maximum admitted value for the timebase denominator "
622 "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
623 (1 << 16) - 1);
3ada94ba
BF
624 return -1;
625 }
626 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
627
ed019b8e
AK
628#if FF_API_MPV_GLOBAL_OPTS
629 if (avctx->flags2 & CODEC_FLAG2_SKIP_RD)
630 s->mpv_flags |= FF_MPV_FLAG_SKIP_RD;
a249f0cc
AK
631 if (avctx->flags2 & CODEC_FLAG2_STRICT_GOP)
632 s->mpv_flags |= FF_MPV_FLAG_STRICT_GOP;
23bfcc06
AK
633 if (avctx->quantizer_noise_shaping)
634 s->quantizer_noise_shaping = avctx->quantizer_noise_shaping;
ed019b8e
AK
635#endif
636
bd96be6e 637 switch (avctx->codec->id) {
3ada94ba
BF
638 case CODEC_ID_MPEG1VIDEO:
639 s->out_format = FMT_MPEG1;
bd96be6e
AD
640 s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
641 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
3ada94ba
BF
642 break;
643 case CODEC_ID_MPEG2VIDEO:
644 s->out_format = FMT_MPEG1;
bd96be6e
AD
645 s->low_delay = !!(s->flags & CODEC_FLAG_LOW_DELAY);
646 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
647 s->rtp_mode = 1;
3ada94ba
BF
648 break;
649 case CODEC_ID_LJPEG:
650 case CODEC_ID_MJPEG:
651 s->out_format = FMT_MJPEG;
652 s->intra_only = 1; /* force intra only for jpeg */
bd96be6e
AD
653 if (avctx->codec->id == CODEC_ID_LJPEG &&
654 avctx->pix_fmt == PIX_FMT_BGRA) {
e0b176ad
MN
655 s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
656 s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
657 s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
bd96be6e 658 } else {
a1a63143 659 s->mjpeg_vsample[0] = 2;
bd96be6e
AD
660 s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
661 s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
a1a63143 662 s->mjpeg_hsample[0] = 2;
bd96be6e
AD
663 s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
664 s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
e0b176ad 665 }
bd96be6e
AD
666 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
667 ff_mjpeg_encode_init(s) < 0)
3ada94ba 668 return -1;
bd96be6e
AD
669 avctx->delay = 0;
670 s->low_delay = 1;
3ada94ba
BF
671 break;
672 case CODEC_ID_H261:
bd96be6e
AD
673 if (!CONFIG_H261_ENCODER)
674 return -1;
3ada94ba 675 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
bd96be6e
AD
676 av_log(avctx, AV_LOG_ERROR,
677 "The specified picture size of %dx%d is not valid for the "
678 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
679 s->width, s->height);
3ada94ba
BF
680 return -1;
681 }
682 s->out_format = FMT_H261;
bd96be6e
AD
683 avctx->delay = 0;
684 s->low_delay = 1;
3ada94ba
BF
685 break;
686 case CODEC_ID_H263:
bd96be6e
AD
687 if (!CONFIG_H263_ENCODER)
688 return -1;
ddce8953 689 if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
bd96be6e
AD
690 s->width, s->height) == 8) {
691 av_log(avctx, AV_LOG_INFO,
692 "The specified picture size of %dx%d is not valid for "
693 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
694 "352x288, 704x576, and 1408x1152."
695 "Try H.263+.\n", s->width, s->height);
3ada94ba
BF
696 return -1;
697 }
698 s->out_format = FMT_H263;
bd96be6e
AD
699 avctx->delay = 0;
700 s->low_delay = 1;
3ada94ba
BF
701 break;
702 case CODEC_ID_H263P:
703 s->out_format = FMT_H263;
bd96be6e 704 s->h263_plus = 1;
3ada94ba 705 /* Fx */
bd96be6e
AD
706 s->h263_aic = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
707 s->modified_quant = s->h263_aic;
708 s->loop_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
709 s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
3ada94ba
BF
710
711 /* /Fx */
712 /* These are just to be sure */
bd96be6e
AD
713 avctx->delay = 0;
714 s->low_delay = 1;
3ada94ba
BF
715 break;
716 case CODEC_ID_FLV1:
bd96be6e
AD
717 s->out_format = FMT_H263;
718 s->h263_flv = 2; /* format = 1; 11-bit codes */
3ada94ba 719 s->unrestricted_mv = 1;
bd96be6e
AD
720 s->rtp_mode = 0; /* don't allow GOB */
721 avctx->delay = 0;
722 s->low_delay = 1;
3ada94ba
BF
723 break;
724 case CODEC_ID_RV10:
725 s->out_format = FMT_H263;
bd96be6e
AD
726 avctx->delay = 0;
727 s->low_delay = 1;
3ada94ba
BF
728 break;
729 case CODEC_ID_RV20:
bd96be6e
AD
730 s->out_format = FMT_H263;
731 avctx->delay = 0;
732 s->low_delay = 1;
733 s->modified_quant = 1;
734 s->h263_aic = 1;
735 s->h263_plus = 1;
736 s->loop_filter = 1;
737 s->unrestricted_mv = 0;
3ada94ba
BF
738 break;
739 case CODEC_ID_MPEG4:
bd96be6e
AD
740 s->out_format = FMT_H263;
741 s->h263_pred = 1;
3ada94ba 742 s->unrestricted_mv = 1;
bd96be6e
AD
743 s->low_delay = s->max_b_frames ? 0 : 1;
744 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
3ada94ba 745 break;
3ada94ba 746 case CODEC_ID_MSMPEG4V2:
bd96be6e
AD
747 s->out_format = FMT_H263;
748 s->h263_pred = 1;
3ada94ba 749 s->unrestricted_mv = 1;
bd96be6e
AD
750 s->msmpeg4_version = 2;
751 avctx->delay = 0;
752 s->low_delay = 1;
3ada94ba
BF
753 break;
754 case CODEC_ID_MSMPEG4V3:
bd96be6e
AD
755 s->out_format = FMT_H263;
756 s->h263_pred = 1;
757 s->unrestricted_mv = 1;
758 s->msmpeg4_version = 3;
759 s->flipflop_rounding = 1;
760 avctx->delay = 0;
761 s->low_delay = 1;
3ada94ba
BF
762 break;
763 case CODEC_ID_WMV1:
bd96be6e
AD
764 s->out_format = FMT_H263;
765 s->h263_pred = 1;
766 s->unrestricted_mv = 1;
767 s->msmpeg4_version = 4;
768 s->flipflop_rounding = 1;
769 avctx->delay = 0;
770 s->low_delay = 1;
3ada94ba
BF
771 break;
772 case CODEC_ID_WMV2:
bd96be6e
AD
773 s->out_format = FMT_H263;
774 s->h263_pred = 1;
775 s->unrestricted_mv = 1;
776 s->msmpeg4_version = 5;
777 s->flipflop_rounding = 1;
778 avctx->delay = 0;
779 s->low_delay = 1;
3ada94ba
BF
780 break;
781 default:
782 return -1;
783 }
784
bd96be6e 785 avctx->has_b_frames = !s->low_delay;
3ada94ba
BF
786
787 s->encoding = 1;
788
bd96be6e
AD
789 s->progressive_frame =
790 s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
0c71cc65
AK
791 CODEC_FLAG_INTERLACED_ME) ||
792 s->alternate_scan);
01bc48f4 793
3ada94ba 794 /* init */
efd29844 795 if (ff_MPV_common_init(s) < 0)
3ada94ba
BF
796 return -1;
797
bd96be6e 798 if (!s->dct_quantize)
99560a4c 799 s->dct_quantize = ff_dct_quantize_c;
bd96be6e
AD
800 if (!s->denoise_dct)
801 s->denoise_dct = denoise_dct_c;
53727262 802 s->fast_dct_quantize = s->dct_quantize;
bd96be6e
AD
803 if (avctx->trellis)
804 s->dct_quantize = dct_quantize_trellis_c;
53727262 805
bd96be6e
AD
806 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
807 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
01bc48f4 808
bd96be6e 809 s->quant_precision = 5;
3ada94ba
BF
810
811 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
812 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
813
49fb20cb 814 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
3ada94ba 815 ff_h261_encode_init(s);
f34121f3 816 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
ddce8953 817 ff_h263_encode_init(s);
49fb20cb 818 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3ada94ba 819 ff_msmpeg4_encode_init(s);
49fb20cb 820 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
a6bc5731 821 && s->out_format == FMT_MPEG1)
3ada94ba
BF
822 ff_mpeg1_encode_init(s);
823
824 /* init q matrix */
bd96be6e
AD
825 for (i = 0; i < 64; i++) {
826 int j = s->dsp.idct_permutation[i];
827 if (CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4 &&
828 s->mpeg_quant) {
3ada94ba
BF
829 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
830 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
bd96be6e 831 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
3ada94ba
BF
832 s->intra_matrix[j] =
833 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
bd96be6e
AD
834 } else {
835 /* mpeg1/2 */
3ada94ba
BF
836 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
837 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
838 }
bd96be6e 839 if (s->avctx->intra_matrix)
3ada94ba 840 s->intra_matrix[j] = s->avctx->intra_matrix[i];
bd96be6e 841 if (s->avctx->inter_matrix)
3ada94ba
BF
842 s->inter_matrix[j] = s->avctx->inter_matrix[i];
843 }
844
845 /* precompute matrix */
846 /* for mjpeg, we do include qscale in the matrix */
847 if (s->out_format != FMT_MJPEG) {
69cea75f 848 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
bd96be6e
AD
849 s->intra_matrix, s->intra_quant_bias, avctx->qmin,
850 31, 1);
69cea75f 851 ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
bd96be6e
AD
852 s->inter_matrix, s->inter_quant_bias, avctx->qmin,
853 31, 0);
3ada94ba
BF
854 }
855
bd96be6e 856 if (ff_rate_control_init(s) < 0)
3ada94ba
BF
857 return -1;
858
859 return 0;
860}
861
efd29844 862av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
3ada94ba
BF
863{
864 MpegEncContext *s = avctx->priv_data;
865
866 ff_rate_control_uninit(s);
867
efd29844 868 ff_MPV_common_end(s);
bd96be6e
AD
869 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
870 s->out_format == FMT_MJPEG)
3ada94ba
BF
871 ff_mjpeg_encode_close(s);
872
873 av_freep(&avctx->extradata);
874
875 return 0;
876}
877
bd96be6e
AD
878static int get_sae(uint8_t *src, int ref, int stride)
879{
3ada94ba 880 int x,y;
bd96be6e 881 int acc = 0;
3ada94ba 882
bd96be6e
AD
883 for (y = 0; y < 16; y++) {
884 for (x = 0; x < 16; x++) {
885 acc += FFABS(src[x + y * stride] - ref);
3ada94ba
BF
886 }
887 }
888
889 return acc;
890}
891
bd96be6e
AD
892static int get_intra_count(MpegEncContext *s, uint8_t *src,
893 uint8_t *ref, int stride)
894{
3ada94ba 895 int x, y, w, h;
bd96be6e 896 int acc = 0;
3ada94ba 897
bd96be6e
AD
898 w = s->width & ~15;
899 h = s->height & ~15;
3ada94ba 900
bd96be6e
AD
901 for (y = 0; y < h; y += 16) {
902 for (x = 0; x < w; x += 16) {
903 int offset = x + y * stride;
904 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
905 16);
906 int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
907 int sae = get_sae(src + offset, mean, stride);
3ada94ba 908
bd96be6e 909 acc += sae + 500 < sad;
3ada94ba
BF
910 }
911 }
912 return acc;
913}
914
915
bd96be6e
AD
916static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg)
917{
918 AVFrame *pic = NULL;
3ada94ba
BF
919 int64_t pts;
920 int i;
54553842
AK
921 const int encoding_delay = s->max_b_frames ? s->max_b_frames :
922 (s->low_delay ? 0 : 1);
bd96be6e
AD
923 int direct = 1;
924
925 if (pic_arg) {
926 pts = pic_arg->pts;
927 pic_arg->display_picture_number = s->input_picture_number++;
928
929 if (pts != AV_NOPTS_VALUE) {
930 if (s->user_specified_pts != AV_NOPTS_VALUE) {
931 int64_t time = pts;
932 int64_t last = s->user_specified_pts;
933
934 if (time <= last) {
935 av_log(s->avctx, AV_LOG_ERROR,
936 "Error, Invalid timestamp=%"PRId64", "
937 "last=%"PRId64"\n", pts, s->user_specified_pts);
3ada94ba
BF
938 return -1;
939 }
445a7d48
AK
940
941 if (!s->low_delay && pic_arg->display_picture_number == 1)
942 s->dts_delta = time - last;
3ada94ba 943 }
bd96be6e
AD
944 s->user_specified_pts = pts;
945 } else {
946 if (s->user_specified_pts != AV_NOPTS_VALUE) {
947 s->user_specified_pts =
948 pts = s->user_specified_pts + 1;
949 av_log(s->avctx, AV_LOG_INFO,
950 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
951 pts);
952 } else {
953 pts = pic_arg->display_picture_number;
3ada94ba
BF
954 }
955 }
956 }
957
bd96be6e
AD
958 if (pic_arg) {
959 if (encoding_delay && !(s->flags & CODEC_FLAG_INPUT_PRESERVED))
960 direct = 0;
961 if (pic_arg->linesize[0] != s->linesize)
962 direct = 0;
963 if (pic_arg->linesize[1] != s->uvlinesize)
964 direct = 0;
965 if (pic_arg->linesize[2] != s->uvlinesize)
966 direct = 0;
967
968 //av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0],
969 // pic_arg->linesize[1], s->linesize, s->uvlinesize);
970
971 if (direct) {
972 i = ff_find_unused_picture(s, 1);
4f820131
AU
973 if (i < 0)
974 return i;
3ada94ba 975
bd96be6e
AD
976 pic = (AVFrame *) &s->picture[i];
977 pic->reference = 3;
3ada94ba 978
bd96be6e
AD
979 for (i = 0; i < 4; i++) {
980 pic->data[i] = pic_arg->data[i];
981 pic->linesize[i] = pic_arg->linesize[i];
3ada94ba 982 }
bd96be6e 983 if (ff_alloc_picture(s, (Picture *) pic, 1) < 0) {
be548816
PP
984 return -1;
985 }
bd96be6e
AD
986 } else {
987 i = ff_find_unused_picture(s, 0);
4f820131
AU
988 if (i < 0)
989 return i;
3ada94ba 990
bd96be6e
AD
991 pic = (AVFrame *) &s->picture[i];
992 pic->reference = 3;
3ada94ba 993
bd96be6e 994 if (ff_alloc_picture(s, (Picture *) pic, 0) < 0) {
be548816
PP
995 return -1;
996 }
3ada94ba 997
bd96be6e
AD
998 if (pic->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
999 pic->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1000 pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1001 // empty
1002 } else {
3ada94ba 1003 int h_chroma_shift, v_chroma_shift;
bd96be6e
AD
1004 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift,
1005 &v_chroma_shift);
1006
1007 for (i = 0; i < 3; i++) {
1008 int src_stride = pic_arg->linesize[i];
1009 int dst_stride = i ? s->uvlinesize : s->linesize;
1010 int h_shift = i ? h_chroma_shift : 0;
1011 int v_shift = i ? v_chroma_shift : 0;
1012 int w = s->width >> h_shift;
1013 int h = s->height >> v_shift;
1014 uint8_t *src = pic_arg->data[i];
1015 uint8_t *dst = pic->data[i];
1016
1017 if (!s->avctx->rc_buffer_size)
1018 dst += INPLACE_OFFSET;
1019
1020 if (src_stride == dst_stride)
1021 memcpy(dst, src, src_stride * h);
1022 else {
1023 while (h--) {
3ada94ba
BF
1024 memcpy(dst, src, w);
1025 dst += dst_stride;
1026 src += src_stride;
1027 }
1028 }
1029 }
1030 }
1031 }
1032 copy_picture_attributes(s, pic, pic_arg);
bd96be6e 1033 pic->pts = pts; // we set this here to avoid modifiying pic_arg
3ada94ba
BF
1034 }
1035
1036 /* shift buffer entries */
bd96be6e
AD
1037 for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1038 s->input_picture[i - 1] = s->input_picture[i];
3ada94ba 1039
bd96be6e 1040 s->input_picture[encoding_delay] = (Picture*) pic;
3ada94ba
BF
1041
1042 return 0;
1043}
1044
bd96be6e
AD
1045static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1046{
3ada94ba 1047 int x, y, plane;
bd96be6e
AD
1048 int score = 0;
1049 int64_t score64 = 0;
3ada94ba 1050
bd96be6e 1051 for (plane = 0; plane < 3; plane++) {
657ccb5a 1052 const int stride = p->f.linesize[plane];
bd96be6e
AD
1053 const int bw = plane ? 1 : 2;
1054 for (y = 0; y < s->mb_height * bw; y++) {
1055 for (x = 0; x < s->mb_width * bw; x++) {
1056 int off = p->f.type == FF_BUFFER_TYPE_SHARED ? 0 : 16;
1057 uint8_t *dptr = p->f.data[plane] + 8 * (x + y * stride) + off;
1058 uint8_t *rptr = ref->f.data[plane] + 8 * (x + y * stride);
1059 int v = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1060
1061 switch (s->avctx->frame_skip_exp) {
1062 case 0: score = FFMAX(score, v); break;
1063 case 1: score += FFABS(v); break;
1064 case 2: score += v * v; break;
1065 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1066 case 4: score64 += v * v * (int64_t)(v * v); break;
3ada94ba
BF
1067 }
1068 }
1069 }
1070 }
1071
bd96be6e
AD
1072 if (score)
1073 score64 = score;
3ada94ba 1074
bd96be6e 1075 if (score64 < s->avctx->frame_skip_threshold)
3ada94ba 1076 return 1;
bd96be6e 1077 if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
3ada94ba
BF
1078 return 1;
1079 return 0;
1080}
1081
bd96be6e
AD
1082static int estimate_best_b_count(MpegEncContext *s)
1083{
9342ecf0 1084 AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
71a861cf 1085 AVCodecContext *c = avcodec_alloc_context3(NULL);
bd96be6e
AD
1086 AVFrame input[FF_MAX_B_FRAMES + 2];
1087 const int scale = s->avctx->brd_scale;
3ada94ba 1088 int i, j, out_size, p_lambda, b_lambda, lambda2;
bd96be6e
AD
1089 int outbuf_size = s->width * s->height; // FIXME
1090 uint8_t *outbuf = av_malloc(outbuf_size);
1091 int64_t best_rd = INT64_MAX;
1092 int best_b_count = -1;
1093
1094 assert(scale >= 0 && scale <= 3);
1095
1096 //emms_c();
1097 //s->next_picture_ptr->quality;
1098 p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1099 //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1100 b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1101 if (!b_lambda) // FIXME we should do this somewhere else
1102 b_lambda = p_lambda;
1103 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1104 FF_LAMBDA_SHIFT;
1105
1106 c->width = s->width >> scale;
1107 c->height = s->height >> scale;
1108 c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |
1109 CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
1110 c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;
1111 c->mb_decision = s->avctx->mb_decision;
1112 c->me_cmp = s->avctx->me_cmp;
1113 c->mb_cmp = s->avctx->mb_cmp;
1114 c->me_sub_cmp = s->avctx->me_sub_cmp;
1115 c->pix_fmt = PIX_FMT_YUV420P;
1116 c->time_base = s->avctx->time_base;
1117 c->max_b_frames = s->max_b_frames;
3ada94ba 1118
0b950fe2 1119 if (avcodec_open2(c, codec, NULL) < 0)
3ada94ba
BF
1120 return -1;
1121
9342ecf0
AD
1122 for (i = 0; i < s->max_b_frames + 2; i++) {
1123 int ysize = c->width * c->height;
1124 int csize = (c->width / 2) * (c->height / 2);
1125 Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1126 s->next_picture_ptr;
3ada94ba
BF
1127
1128 avcodec_get_frame_defaults(&input[i]);
9342ecf0
AD
1129 input[i].data[0] = av_malloc(ysize + 2 * csize);
1130 input[i].data[1] = input[i].data[0] + ysize;
1131 input[i].data[2] = input[i].data[1] + csize;
1132 input[i].linesize[0] = c->width;
1133 input[i].linesize[1] =
1134 input[i].linesize[2] = c->width / 2;
3ada94ba 1135
9342ecf0
AD
1136 if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1137 pre_input = *pre_input_ptr;
3ada94ba 1138
657ccb5a
DB
1139 if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {
1140 pre_input.f.data[0] += INPLACE_OFFSET;
1141 pre_input.f.data[1] += INPLACE_OFFSET;
1142 pre_input.f.data[2] += INPLACE_OFFSET;
3ada94ba
BF
1143 }
1144
9342ecf0
AD
1145 s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
1146 pre_input.f.data[0], pre_input.f.linesize[0],
1147 c->width, c->height);
1148 s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
1149 pre_input.f.data[1], pre_input.f.linesize[1],
1150 c->width >> 1, c->height >> 1);
1151 s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
1152 pre_input.f.data[2], pre_input.f.linesize[2],
1153 c->width >> 1, c->height >> 1);
3ada94ba
BF
1154 }
1155 }
1156
9342ecf0
AD
1157 for (j = 0; j < s->max_b_frames + 1; j++) {
1158 int64_t rd = 0;
3ada94ba 1159
9342ecf0 1160 if (!s->input_picture[j])
3ada94ba
BF
1161 break;
1162
9342ecf0 1163 c->error[0] = c->error[1] = c->error[2] = 0;
3ada94ba 1164
9342ecf0
AD
1165 input[0].pict_type = AV_PICTURE_TYPE_I;
1166 input[0].quality = 1 * FF_QP2LAMBDA;
1167 out_size = avcodec_encode_video(c, outbuf,
1168 outbuf_size, &input[0]);
1169 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
3ada94ba 1170
9342ecf0
AD
1171 for (i = 0; i < s->max_b_frames + 1; i++) {
1172 int is_p = i % (j + 1) == j || i == s->max_b_frames;
3ada94ba 1173
9342ecf0
AD
1174 input[i + 1].pict_type = is_p ?
1175 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1176 input[i + 1].quality = is_p ? p_lambda : b_lambda;
1177 out_size = avcodec_encode_video(c, outbuf, outbuf_size,
1178 &input[i + 1]);
3ada94ba
BF
1179 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1180 }
1181
1182 /* get the delayed frames */
9342ecf0 1183 while (out_size) {
3ada94ba
BF
1184 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
1185 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1186 }
1187
1188 rd += c->error[0] + c->error[1] + c->error[2];
1189
9342ecf0
AD
1190 if (rd < best_rd) {
1191 best_rd = rd;
1192 best_b_count = j;
3ada94ba
BF
1193 }
1194 }
1195
1196 av_freep(&outbuf);
1197 avcodec_close(c);
1198 av_freep(&c);
1199
9342ecf0 1200 for (i = 0; i < s->max_b_frames + 2; i++) {
3ada94ba
BF
1201 av_freep(&input[i].data[0]);
1202 }
1203
1204 return best_b_count;
1205}
1206
9342ecf0
AD
1207static int select_input_picture(MpegEncContext *s)
1208{
3ada94ba
BF
1209 int i;
1210
9342ecf0
AD
1211 for (i = 1; i < MAX_PICTURE_COUNT; i++)
1212 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1213 s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
3ada94ba
BF
1214
1215 /* set next picture type & ordering */
9342ecf0
AD
1216 if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
1217 if (/*s->picture_in_gop_number >= s->gop_size ||*/
1218 s->next_picture_ptr == NULL || s->intra_only) {
1219 s->reordered_input_picture[0] = s->input_picture[0];
657ccb5a 1220 s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_I;
9342ecf0
AD
1221 s->reordered_input_picture[0]->f.coded_picture_number =
1222 s->coded_picture_number++;
1223 } else {
3ada94ba
BF
1224 int b_frames;
1225
9342ecf0
AD
1226 if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1227 if (s->picture_in_gop_number < s->gop_size &&
1228 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1229 // FIXME check that te gop check above is +-1 correct
1230 //av_log(NULL, AV_LOG_DEBUG, "skip %p %"PRId64"\n",
1231 // s->input_picture[0]->f.data[0],
1232 // s->input_picture[0]->pts);
3ada94ba 1233
657ccb5a 1234 if (s->input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED) {
9342ecf0 1235 for (i = 0; i < 4; i++)
657ccb5a
DB
1236 s->input_picture[0]->f.data[i] = NULL;
1237 s->input_picture[0]->f.type = 0;
9342ecf0
AD
1238 } else {
1239 assert(s->input_picture[0]->f.type == FF_BUFFER_TYPE_USER ||
1240 s->input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL);
3ada94ba 1241
9342ecf0
AD
1242 s->avctx->release_buffer(s->avctx,
1243 (AVFrame *) s->input_picture[0]);
3ada94ba
BF
1244 }
1245
1246 emms_c();
1247 ff_vbv_update(s, 0);
1248
1249 goto no_output_pic;
1250 }
1251 }
1252
9342ecf0
AD
1253 if (s->flags & CODEC_FLAG_PASS2) {
1254 for (i = 0; i < s->max_b_frames + 1; i++) {
657ccb5a 1255 int pict_num = s->input_picture[0]->f.display_picture_number + i;
3ada94ba 1256
9342ecf0 1257 if (pict_num >= s->rc_context.num_entries)
3ada94ba 1258 break;
9342ecf0
AD
1259 if (!s->input_picture[i]) {
1260 s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
3ada94ba
BF
1261 break;
1262 }
1263
657ccb5a 1264 s->input_picture[i]->f.pict_type =
3ada94ba
BF
1265 s->rc_context.entry[pict_num].new_pict_type;
1266 }
1267 }
1268
9342ecf0
AD
1269 if (s->avctx->b_frame_strategy == 0) {
1270 b_frames = s->max_b_frames;
1271 while (b_frames && !s->input_picture[b_frames])
1272 b_frames--;
1273 } else if (s->avctx->b_frame_strategy == 1) {
1274 for (i = 1; i < s->max_b_frames + 1; i++) {
1275 if (s->input_picture[i] &&
1276 s->input_picture[i]->b_frame_score == 0) {
1277 s->input_picture[i]->b_frame_score =
1278 get_intra_count(s,
1279 s->input_picture[i ]->f.data[0],
1280 s->input_picture[i - 1]->f.data[0],
1281 s->linesize) + 1;
3ada94ba
BF
1282 }
1283 }
9342ecf0
AD
1284 for (i = 0; i < s->max_b_frames + 1; i++) {
1285 if (s->input_picture[i] == NULL ||
1286 s->input_picture[i]->b_frame_score - 1 >
1287 s->mb_num / s->avctx->b_sensitivity)
1288 break;
3ada94ba
BF
1289 }
1290
9342ecf0 1291 b_frames = FFMAX(0, i - 1);
3ada94ba
BF
1292
1293 /* reset scores */
9342ecf0
AD
1294 for (i = 0; i < b_frames + 1; i++) {
1295 s->input_picture[i]->b_frame_score = 0;
3ada94ba 1296 }
9342ecf0
AD
1297 } else if (s->avctx->b_frame_strategy == 2) {
1298 b_frames = estimate_best_b_count(s);
1299 } else {
3ada94ba 1300 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
9342ecf0 1301 b_frames = 0;
3ada94ba
BF
1302 }
1303
1304 emms_c();
9342ecf0
AD
1305 //static int b_count = 0;
1306 //b_count += b_frames;
1307 //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
3ada94ba 1308
9342ecf0 1309 for (i = b_frames - 1; i >= 0; i--) {
657ccb5a 1310 int type = s->input_picture[i]->f.pict_type;
9342ecf0
AD
1311 if (type && type != AV_PICTURE_TYPE_B)
1312 b_frames = i;
3ada94ba 1313 }
9342ecf0
AD
1314 if (s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_B &&
1315 b_frames == s->max_b_frames) {
1316 av_log(s->avctx, AV_LOG_ERROR,
1317 "warning, too many b frames in a row\n");
3ada94ba
BF
1318 }
1319
9342ecf0 1320 if (s->picture_in_gop_number + b_frames >= s->gop_size) {
a249f0cc 1321 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
9342ecf0
AD
1322 s->gop_size > s->picture_in_gop_number) {
1323 b_frames = s->gop_size - s->picture_in_gop_number - 1;
1324 } else {
1325 if (s->flags & CODEC_FLAG_CLOSED_GOP)
1326 b_frames = 0;
1327 s->input_picture[b_frames]->f.pict_type = AV_PICTURE_TYPE_I;
1328 }
3ada94ba
BF
1329 }
1330
9342ecf0
AD
1331 if ((s->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1332 s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_I)
3ada94ba
BF
1333 b_frames--;
1334
9342ecf0 1335 s->reordered_input_picture[0] = s->input_picture[b_frames];
657ccb5a
DB
1336 if (s->reordered_input_picture[0]->f.pict_type != AV_PICTURE_TYPE_I)
1337 s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_P;
9342ecf0
AD
1338 s->reordered_input_picture[0]->f.coded_picture_number =
1339 s->coded_picture_number++;
1340 for (i = 0; i < b_frames; i++) {
657ccb5a 1341 s->reordered_input_picture[i + 1] = s->input_picture[i];
9342ecf0
AD
1342 s->reordered_input_picture[i + 1]->f.pict_type =
1343 AV_PICTURE_TYPE_B;
1344 s->reordered_input_picture[i + 1]->f.coded_picture_number =
1345 s->coded_picture_number++;
3ada94ba
BF
1346 }
1347 }
1348 }
1349no_output_pic:
9342ecf0
AD
1350 if (s->reordered_input_picture[0]) {
1351 s->reordered_input_picture[0]->f.reference =
1352 s->reordered_input_picture[0]->f.pict_type !=
1353 AV_PICTURE_TYPE_B ? 3 : 0;
3ada94ba 1354
8d2fc163 1355 ff_copy_picture(&s->new_picture, s->reordered_input_picture[0]);
3ada94ba 1356
9342ecf0
AD
1357 if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED ||
1358 s->avctx->rc_buffer_size) {
1359 // input is a shared pix, so we can't modifiy it -> alloc a new
1360 // one & ensure that the shared one is reuseable
3ada94ba 1361
1ce1578e 1362 Picture *pic;
9342ecf0 1363 int i = ff_find_unused_picture(s, 0);
4f820131
AU
1364 if (i < 0)
1365 return i;
1ce1578e 1366 pic = &s->picture[i];
3ada94ba 1367
657ccb5a 1368 pic->f.reference = s->reordered_input_picture[0]->f.reference;
9342ecf0 1369 if (ff_alloc_picture(s, pic, 0) < 0) {
be548816
PP
1370 return -1;
1371 }
3ada94ba
BF
1372
1373 /* mark us unused / free shared pic */
657ccb5a 1374 if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL)
9342ecf0
AD
1375 s->avctx->release_buffer(s->avctx,
1376 (AVFrame *) s->reordered_input_picture[0]);
1377 for (i = 0; i < 4; i++)
657ccb5a
DB
1378 s->reordered_input_picture[0]->f.data[i] = NULL;
1379 s->reordered_input_picture[0]->f.type = 0;
3ada94ba 1380
9342ecf0
AD
1381 copy_picture_attributes(s, (AVFrame *) pic,
1382 (AVFrame *) s->reordered_input_picture[0]);
3ada94ba 1383
9342ecf0
AD
1384 s->current_picture_ptr = pic;
1385 } else {
3ada94ba
BF
1386 // input is not a shared pix -> reuse buffer for current_pix
1387
9342ecf0
AD
1388 assert(s->reordered_input_picture[0]->f.type ==
1389 FF_BUFFER_TYPE_USER ||
1390 s->reordered_input_picture[0]->f.type ==
1391 FF_BUFFER_TYPE_INTERNAL);
3ada94ba 1392
9342ecf0
AD
1393 s->current_picture_ptr = s->reordered_input_picture[0];
1394 for (i = 0; i < 4; i++) {
657ccb5a 1395 s->new_picture.f.data[i] += INPLACE_OFFSET;
3ada94ba
BF
1396 }
1397 }
8d2fc163 1398 ff_copy_picture(&s->current_picture, s->current_picture_ptr);
3ada94ba 1399
657ccb5a 1400 s->picture_number = s->new_picture.f.display_picture_number;
9342ecf0
AD
1401 //printf("dpn:%d\n", s->picture_number);
1402 } else {
1403 memset(&s->new_picture, 0, sizeof(Picture));
3ada94ba 1404 }
be548816 1405 return 0;
3ada94ba
BF
1406}
1407
445a7d48
AK
1408int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1409 const AVFrame *pic_arg, int *got_packet)
3ada94ba
BF
1410{
1411 MpegEncContext *s = avctx->priv_data;
445a7d48 1412 int i, stuffing_count, ret;
881a5e04 1413 int context_count = s->slice_context_count;
3ada94ba 1414
3ada94ba
BF
1415 s->picture_in_gop_number++;
1416
9342ecf0 1417 if (load_input_picture(s, pic_arg) < 0)
3ada94ba
BF
1418 return -1;
1419
9342ecf0 1420 if (select_input_picture(s) < 0) {
be548816
PP
1421 return -1;
1422 }
3ada94ba
BF
1423
1424 /* output? */
657ccb5a 1425 if (s->new_picture.f.data[0]) {
5d42ac7f
AK
1426 if (!pkt->data &&
1427 (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
1428 return ret;
1429
1430 for (i = 0; i < context_count; i++) {
1431 int start_y = s->thread_context[i]->start_mb_y;
1432 int end_y = s->thread_context[i]-> end_mb_y;
1433 int h = s->mb_height;
1434 uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1435 uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1436
1437 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1438 }
1439
657ccb5a 1440 s->pict_type = s->new_picture.f.pict_type;
9342ecf0
AD
1441 //emms_c();
1442 //printf("qs:%f %f %d\n", s->new_picture.quality,
1443 // s->current_picture.quality, s->qscale);
efd29844 1444 ff_MPV_frame_start(s, avctx);
3ada94ba
BF
1445vbv_retry:
1446 if (encode_picture(s, s->picture_number) < 0)
1447 return -1;
1448
3ada94ba
BF
1449 avctx->header_bits = s->header_bits;
1450 avctx->mv_bits = s->mv_bits;
1451 avctx->misc_bits = s->misc_bits;
1452 avctx->i_tex_bits = s->i_tex_bits;
1453 avctx->p_tex_bits = s->p_tex_bits;
1454 avctx->i_count = s->i_count;
9342ecf0
AD
1455 // FIXME f/b_count in avctx
1456 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
3ada94ba
BF
1457 avctx->skip_count = s->skip_count;
1458
efd29844 1459 ff_MPV_frame_end(s);
3ada94ba 1460
49fb20cb 1461 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
3ada94ba
BF
1462 ff_mjpeg_encode_picture_trailer(s);
1463
9342ecf0
AD
1464 if (avctx->rc_buffer_size) {
1465 RateControlContext *rcc = &s->rc_context;
1466 int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
3ada94ba 1467
9342ecf0
AD
1468 if (put_bits_count(&s->pb) > max_size &&
1469 s->lambda < s->avctx->lmax) {
1470 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1471 (s->qscale + 1) / s->qscale);
1472 if (s->adaptive_quant) {
3ada94ba 1473 int i;
9342ecf0
AD
1474 for (i = 0; i < s->mb_height * s->mb_stride; i++)
1475 s->lambda_table[i] =
1476 FFMAX(s->lambda_table[i] + 1,
1477 s->lambda_table[i] * (s->qscale + 1) /
1478 s->qscale);
3ada94ba 1479 }
9342ecf0
AD
1480 s->mb_skipped = 0; // done in MPV_frame_start()
1481 // done in encode_picture() so we must undo it
1482 if (s->pict_type == AV_PICTURE_TYPE_P) {
1483 if (s->flipflop_rounding ||
1484 s->codec_id == CODEC_ID_H263P ||
1485 s->codec_id == CODEC_ID_MPEG4)
3ada94ba
BF
1486 s->no_rounding ^= 1;
1487 }
9342ecf0
AD
1488 if (s->pict_type != AV_PICTURE_TYPE_B) {
1489 s->time_base = s->last_time_base;
1490 s->last_non_b_time = s->time - s->pp_time;
3ada94ba 1491 }
9342ecf0
AD
1492 //av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda);
1493 for (i = 0; i < context_count; i++) {
1494 PutBitContext *pb = &s->thread_context[i]->pb;
3ada94ba
BF
1495 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1496 }
1497 goto vbv_retry;
1498 }
1499
1500 assert(s->avctx->rc_max_rate);
1501 }
1502
9342ecf0 1503 if (s->flags & CODEC_FLAG_PASS1)
3ada94ba
BF
1504 ff_write_pass1_stats(s);
1505
9342ecf0
AD
1506 for (i = 0; i < 4; i++) {
1507 s->current_picture_ptr->f.error[i] = s->current_picture.f.error[i];
1508 avctx->error[i] += s->current_picture_ptr->f.error[i];
3ada94ba
BF
1509 }
1510
9342ecf0
AD
1511 if (s->flags & CODEC_FLAG_PASS1)
1512 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1513 avctx->i_tex_bits + avctx->p_tex_bits ==
1514 put_bits_count(&s->pb));
3ada94ba
BF
1515 flush_put_bits(&s->pb);
1516 s->frame_bits = put_bits_count(&s->pb);
1517
9342ecf0
AD
1518 stuffing_count = ff_vbv_update(s, s->frame_bits);
1519 if (stuffing_count) {
1520 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1521 stuffing_count + 50) {
3ada94ba
BF
1522 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1523 return -1;
1524 }
1525
9342ecf0 1526 switch (s->codec_id) {
3ada94ba
BF
1527 case CODEC_ID_MPEG1VIDEO:
1528 case CODEC_ID_MPEG2VIDEO:
9342ecf0 1529 while (stuffing_count--) {
3ada94ba
BF
1530 put_bits(&s->pb, 8, 0);
1531 }
1532 break;
1533 case CODEC_ID_MPEG4:
1534 put_bits(&s->pb, 16, 0);
1535 put_bits(&s->pb, 16, 0x1C3);
1536 stuffing_count -= 4;
9342ecf0 1537 while (stuffing_count--) {
3ada94ba
BF
1538 put_bits(&s->pb, 8, 0xFF);
1539 }
1540 break;
1541 default:
1542 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1543 }
1544 flush_put_bits(&s->pb);
1545 s->frame_bits = put_bits_count(&s->pb);
1546 }
1547
1548 /* update mpeg1/2 vbv_delay for CBR */
9342ecf0
AD
1549 if (s->avctx->rc_max_rate &&
1550 s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1551 s->out_format == FMT_MPEG1 &&
1552 90000LL * (avctx->rc_buffer_size - 1) <=
1553 s->avctx->rc_max_rate * 0xFFFFLL) {
4601e76a 1554 int vbv_delay, min_delay;
9342ecf0
AD
1555 double inbits = s->avctx->rc_max_rate *
1556 av_q2d(s->avctx->time_base);
1557 int minbits = s->frame_bits - 8 *
1558 (s->vbv_delay_ptr - s->pb.buf - 1);
1559 double bits = s->rc_context.buffer_index + minbits - inbits;
4601e76a 1560
9342ecf0
AD
1561 if (bits < 0)
1562 av_log(s->avctx, AV_LOG_ERROR,
1563 "Internal error, negative bits\n");
3ada94ba 1564
9342ecf0 1565 assert(s->repeat_first_field == 0);
3ada94ba 1566
9342ecf0
AD
1567 vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1568 min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1569 s->avctx->rc_max_rate;
4601e76a 1570
9342ecf0 1571 vbv_delay = FFMAX(vbv_delay, min_delay);
4601e76a 1572
3ada94ba
BF
1573 assert(vbv_delay < 0xFFFF);
1574
1575 s->vbv_delay_ptr[0] &= 0xF8;
9342ecf0
AD
1576 s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1577 s->vbv_delay_ptr[1] = vbv_delay >> 5;
3ada94ba 1578 s->vbv_delay_ptr[2] &= 0x07;
9342ecf0
AD
1579 s->vbv_delay_ptr[2] |= vbv_delay << 3;
1580 avctx->vbv_delay = vbv_delay * 300;
3ada94ba 1581 }
9342ecf0 1582 s->total_bits += s->frame_bits;
3ada94ba 1583 avctx->frame_bits = s->frame_bits;
445a7d48
AK
1584
1585 pkt->pts = s->current_picture.f.pts;
1586 if (!s->low_delay) {
1587 if (!s->current_picture.f.coded_picture_number)
1588 pkt->dts = pkt->pts - s->dts_delta;
1589 else
1590 pkt->dts = s->reordered_pts;
1591 s->reordered_pts = s->input_picture[0]->f.pts;
1592 } else
1593 pkt->dts = pkt->pts;
1594 if (s->current_picture.f.key_frame)
1595 pkt->flags |= AV_PKT_FLAG_KEY;
9342ecf0 1596 } else {
fb53b4a0 1597 assert((put_bits_ptr(&s->pb) == s->pb.buf));
9342ecf0 1598 s->frame_bits = 0;
3ada94ba 1599 }
9342ecf0 1600 assert((s->frame_bits & 7) == 0);
3ada94ba 1601
445a7d48
AK
1602 pkt->size = s->frame_bits / 8;
1603 *got_packet = !!pkt->size;
1604 return 0;
3ada94ba
BF
1605}
1606
9342ecf0
AD
1607static inline void dct_single_coeff_elimination(MpegEncContext *s,
1608 int n, int threshold)
3ada94ba 1609{
9342ecf0
AD
1610 static const char tab[64] = {
1611 3, 2, 2, 1, 1, 1, 1, 1,
1612 1, 1, 1, 1, 1, 1, 1, 1,
1613 1, 1, 1, 1, 1, 1, 1, 1,
1614 0, 0, 0, 0, 0, 0, 0, 0,
1615 0, 0, 0, 0, 0, 0, 0, 0,
1616 0, 0, 0, 0, 0, 0, 0, 0,
1617 0, 0, 0, 0, 0, 0, 0, 0,
1618 0, 0, 0, 0, 0, 0, 0, 0
1619 };
1620 int score = 0;
1621 int run = 0;
3ada94ba 1622 int i;
9342ecf0
AD
1623 DCTELEM *block = s->block[n];
1624 const int last_index = s->block_last_index[n];
3ada94ba
BF
1625 int skip_dc;
1626
9342ecf0
AD
1627 if (threshold < 0) {
1628 skip_dc = 0;
1629 threshold = -threshold;
1630 } else
1631 skip_dc = 1;
3ada94ba 1632
bd107136 1633 /* Are all we could set to zero already zero? */
9342ecf0
AD
1634 if (last_index <= skip_dc - 1)
1635 return;
3ada94ba 1636
9342ecf0 1637 for (i = 0; i <= last_index; i++) {
3ada94ba
BF
1638 const int j = s->intra_scantable.permutated[i];
1639 const int level = FFABS(block[j]);
9342ecf0
AD
1640 if (level == 1) {
1641 if (skip_dc && i == 0)
1642 continue;
1643 score += tab[run];
1644 run = 0;
1645 } else if (level > 1) {
3ada94ba 1646 return;
9342ecf0 1647 } else {
3ada94ba
BF
1648 run++;
1649 }
1650 }
9342ecf0
AD
1651 if (score >= threshold)
1652 return;
1653 for (i = skip_dc; i <= last_index; i++) {
3ada94ba 1654 const int j = s->intra_scantable.permutated[i];
9342ecf0 1655 block[j] = 0;
3ada94ba 1656 }
9342ecf0
AD
1657 if (block[0])
1658 s->block_last_index[n] = 0;
1659 else
1660 s->block_last_index[n] = -1;
3ada94ba
BF
1661}
1662
9342ecf0
AD
1663static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block,
1664 int last_index)
3ada94ba
BF
1665{
1666 int i;
9342ecf0
AD
1667 const int maxlevel = s->max_qcoeff;
1668 const int minlevel = s->min_qcoeff;
1669 int overflow = 0;
3ada94ba 1670
9342ecf0
AD
1671 if (s->mb_intra) {
1672 i = 1; // skip clipping of intra dc
1673 } else
1674 i = 0;
3ada94ba 1675
9342ecf0
AD
1676 for (; i <= last_index; i++) {
1677 const int j = s->intra_scantable.permutated[i];
3ada94ba
BF
1678 int level = block[j];
1679
9342ecf0
AD
1680 if (level > maxlevel) {
1681 level = maxlevel;
3ada94ba 1682 overflow++;
9342ecf0
AD
1683 } else if (level < minlevel) {
1684 level = minlevel;
3ada94ba
BF
1685 overflow++;
1686 }
1687
9342ecf0 1688 block[j] = level;
3ada94ba
BF
1689 }
1690
9342ecf0
AD
1691 if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1692 av_log(s->avctx, AV_LOG_INFO,
1693 "warning, clipping %d dct coefficients to %d..%d\n",
1694 overflow, minlevel, maxlevel);
3ada94ba
BF
1695}
1696
9342ecf0
AD
1697static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1698{
3ada94ba 1699 int x, y;
9342ecf0
AD
1700 // FIXME optimize
1701 for (y = 0; y < 8; y++) {
1702 for (x = 0; x < 8; x++) {
3ada94ba 1703 int x2, y2;
9342ecf0
AD
1704 int sum = 0;
1705 int sqr = 0;
1706 int count = 0;
3ada94ba 1707
9342ecf0
AD
1708 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1709 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1710 int v = ptr[x2 + y2 * stride];
3ada94ba 1711 sum += v;
9342ecf0 1712 sqr += v * v;
3ada94ba
BF
1713 count++;
1714 }
1715 }
9342ecf0 1716 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
3ada94ba
BF
1717 }
1718 }
1719}
1720
9342ecf0
AD
1721static av_always_inline void encode_mb_internal(MpegEncContext *s,
1722 int motion_x, int motion_y,
1723 int mb_block_height,
1724 int mb_block_count)
3ada94ba
BF
1725{
1726 int16_t weight[8][64];
1727 DCTELEM orig[8][64];
9342ecf0
AD
1728 const int mb_x = s->mb_x;
1729 const int mb_y = s->mb_y;
3ada94ba
BF
1730 int i;
1731 int skip_dct[8];
9342ecf0 1732 int dct_offset = s->linesize * 8; // default for progressive frames
3ada94ba
BF
1733 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1734 int wrap_y, wrap_c;
1735
9342ecf0
AD
1736 for (i = 0; i < mb_block_count; i++)
1737 skip_dct[i] = s->skipdct;
3ada94ba 1738
9342ecf0
AD
1739 if (s->adaptive_quant) {
1740 const int last_qp = s->qscale;
1741 const int mb_xy = mb_x + mb_y * s->mb_stride;
3ada94ba 1742
9342ecf0 1743 s->lambda = s->lambda_table[mb_xy];
3ada94ba
BF
1744 update_qscale(s);
1745
ff71a383 1746 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
657ccb5a 1747 s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];
9342ecf0 1748 s->dquant = s->qscale - last_qp;
3ada94ba 1749
9342ecf0
AD
1750 if (s->out_format == FMT_H263) {
1751 s->dquant = av_clip(s->dquant, -2, 2);
3ada94ba 1752
9342ecf0
AD
1753 if (s->codec_id == CODEC_ID_MPEG4) {
1754 if (!s->mb_intra) {
1755 if (s->pict_type == AV_PICTURE_TYPE_B) {
1756 if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1757 s->dquant = 0;
3ada94ba 1758 }
9342ecf0
AD
1759 if (s->mv_type == MV_TYPE_8X8)
1760 s->dquant = 0;
3ada94ba
BF
1761 }
1762 }
1763 }
1764 }
1765 ff_set_qscale(s, last_qp + s->dquant);
ff71a383 1766 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
3ada94ba
BF
1767 ff_set_qscale(s, s->qscale + s->dquant);
1768
1769 wrap_y = s->linesize;
1770 wrap_c = s->uvlinesize;
9342ecf0
AD
1771 ptr_y = s->new_picture.f.data[0] +
1772 (mb_y * 16 * wrap_y) + mb_x * 16;
1773 ptr_cb = s->new_picture.f.data[1] +
1774 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1775 ptr_cr = s->new_picture.f.data[2] +
1776 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1777
1778 if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
1779 uint8_t *ebuf = s->edge_emu_buffer + 32;
1780 s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,
1781 mb_y * 16, s->width, s->height);
1782 ptr_y = ebuf;
1783 s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8,
1784 mb_block_height, mb_x * 8, mb_y * 8,
1785 s->width >> 1, s->height >> 1);
1786 ptr_cb = ebuf + 18 * wrap_y;
1787 s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8,
1788 mb_block_height, mb_x * 8, mb_y * 8,
1789 s->width >> 1, s->height >> 1);
1790 ptr_cr = ebuf + 18 * wrap_y + 8;
3ada94ba
BF
1791 }
1792
1793 if (s->mb_intra) {
9342ecf0 1794 if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
3ada94ba
BF
1795 int progressive_score, interlaced_score;
1796
9342ecf0
AD
1797 s->interlaced_dct = 0;
1798 progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,
1799 NULL, wrap_y, 8) +
1800 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1801 NULL, wrap_y, 8) - 400;
1802
1803 if (progressive_score > 0) {
1804 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,
1805 NULL, wrap_y * 2, 8) +
1806 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,
1807 NULL, wrap_y * 2, 8);
1808 if (progressive_score > interlaced_score) {
1809 s->interlaced_dct = 1;
1810
1811 dct_offset = wrap_y;
1812 wrap_y <<= 1;
3ada94ba 1813 if (s->chroma_format == CHROMA_422)
9342ecf0 1814 wrap_c <<= 1;
3ada94ba
BF
1815 }
1816 }
1817 }
1818
9342ecf0
AD
1819 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
1820 s->dsp.get_pixels(s->block[1], ptr_y + 8 , wrap_y);
1821 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
1822 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);
3ada94ba 1823
9342ecf0
AD
1824 if (s->flags & CODEC_FLAG_GRAY) {
1825 skip_dct[4] = 1;
1826 skip_dct[5] = 1;
1827 } else {
3ada94ba
BF
1828 s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1829 s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
9342ecf0
AD
1830 if (!s->chroma_y_shift) { /* 422 */
1831 s->dsp.get_pixels(s->block[6],
1832 ptr_cb + (dct_offset >> 1), wrap_c);
1833 s->dsp.get_pixels(s->block[7],
1834 ptr_cr + (dct_offset >> 1), wrap_c);
3ada94ba
BF
1835 }
1836 }
9342ecf0 1837 } else {
3ada94ba
BF
1838 op_pixels_func (*op_pix)[4];
1839 qpel_mc_func (*op_qpix)[16];
1840 uint8_t *dest_y, *dest_cb, *dest_cr;
1841
1842 dest_y = s->dest[0];
1843 dest_cb = s->dest[1];
1844 dest_cr = s->dest[2];
1845
9342ecf0
AD
1846 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1847 op_pix = s->dsp.put_pixels_tab;
1848 op_qpix = s->dsp.put_qpel_pixels_tab;
1849 } else {
1850 op_pix = s->dsp.put_no_rnd_pixels_tab;
1851 op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;
3ada94ba
BF
1852 }
1853
1854 if (s->mv_dir & MV_DIR_FORWARD) {
9342ecf0
AD
1855 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data,
1856 op_pix, op_qpix);
1857 op_pix = s->dsp.avg_pixels_tab;
1858 op_qpix = s->dsp.avg_qpel_pixels_tab;
3ada94ba
BF
1859 }
1860 if (s->mv_dir & MV_DIR_BACKWARD) {
9342ecf0
AD
1861 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data,
1862 op_pix, op_qpix);
3ada94ba
BF
1863 }
1864
9342ecf0 1865 if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
3ada94ba
BF
1866 int progressive_score, interlaced_score;
1867
9342ecf0
AD
1868 s->interlaced_dct = 0;
1869 progressive_score = s->dsp.ildct_cmp[0](s, dest_y,
1870 ptr_y, wrap_y,
1871 8) +
1872 s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,
1873 ptr_y + wrap_y * 8, wrap_y,
1874 8) - 400;
1875
1876 if (s->avctx->ildct_cmp == FF_CMP_VSSE)
1877 progressive_score -= 400;
1878
1879 if (progressive_score > 0) {
1880 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,
1881 ptr_y,
1882 wrap_y * 2, 8) +
1883 s->dsp.ildct_cmp[0](s, dest_y + wrap_y,
1884 ptr_y + wrap_y,
1885 wrap_y * 2, 8);
1886
1887 if (progressive_score > interlaced_score) {
1888 s->interlaced_dct = 1;
1889
1890 dct_offset = wrap_y;
1891 wrap_y <<= 1;
3ada94ba 1892 if (s->chroma_format == CHROMA_422)
9342ecf0 1893 wrap_c <<= 1;
3ada94ba
BF
1894 }
1895 }
1896 }
1897
9342ecf0
AD
1898 s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
1899 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
1900 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,
1901 dest_y + dct_offset, wrap_y);
1902 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
1903 dest_y + dct_offset + 8, wrap_y);
3ada94ba 1904
9342ecf0
AD
1905 if (s->flags & CODEC_FLAG_GRAY) {
1906 skip_dct[4] = 1;
1907 skip_dct[5] = 1;
1908 } else {
3ada94ba
BF
1909 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1910 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
9342ecf0
AD
1911 if (!s->chroma_y_shift) { /* 422 */
1912 s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
1913 dest_cb + (dct_offset >> 1), wrap_c);
1914 s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
1915 dest_cr + (dct_offset >> 1), wrap_c);
3ada94ba
BF
1916 }
1917 }
1918 /* pre quantization */
9342ecf0
AD
1919 if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
1920 2 * s->qscale * s->qscale) {
1921 // FIXME optimize
1922 if (s->dsp.sad[1](NULL, ptr_y , dest_y,
1923 wrap_y, 8) < 20 * s->qscale)
1924 skip_dct[0] = 1;
1925 if (s->dsp.sad[1](NULL, ptr_y + 8,
1926 dest_y + 8, wrap_y, 8) < 20 * s->qscale)
1927 skip_dct[1] = 1;
1928 if (s->dsp.sad[1](NULL, ptr_y + dct_offset,
1929 dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)
1930 skip_dct[2] = 1;
1931 if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,
1932 dest_y + dct_offset + 8,
1933 wrap_y, 8) < 20 * s->qscale)
1934 skip_dct[3] = 1;
1935 if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,
1936 wrap_c, 8) < 20 * s->qscale)
1937 skip_dct[4] = 1;
1938 if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,
1939 wrap_c, 8) < 20 * s->qscale)
1940 skip_dct[5] = 1;
1941 if (!s->chroma_y_shift) { /* 422 */
1942 if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),
1943 dest_cb + (dct_offset >> 1),
1944 wrap_c, 8) < 20 * s->qscale)
1945 skip_dct[6] = 1;
1946 if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),
1947 dest_cr + (dct_offset >> 1),
1948 wrap_c, 8) < 20 * s->qscale)
1949 skip_dct[7] = 1;
3ada94ba
BF
1950 }
1951 }
1952 }
1953
23bfcc06 1954 if (s->quantizer_noise_shaping) {
9342ecf0
AD
1955 if (!skip_dct[0])
1956 get_visual_weight(weight[0], ptr_y , wrap_y);
1957 if (!skip_dct[1])
1958 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
1959 if (!skip_dct[2])
1960 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
1961 if (!skip_dct[3])
1962 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
1963 if (!skip_dct[4])
1964 get_visual_weight(weight[4], ptr_cb , wrap_c);
1965 if (!skip_dct[5])
1966 get_visual_weight(weight[5], ptr_cr , wrap_c);
1967 if (!s->chroma_y_shift) { /* 422 */
1968 if (!skip_dct[6])
1969 get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
1970 wrap_c);
1971 if (!skip_dct[7])
1972 get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
1973 wrap_c);
3ada94ba 1974 }
9342ecf0 1975 memcpy(orig[0], s->block[0], sizeof(DCTELEM) * 64 * mb_block_count);
3ada94ba
BF
1976 }
1977
1978 /* DCT & quantize */
9342ecf0 1979 assert(s->out_format != FMT_MJPEG || s->qscale == 8);
3ada94ba 1980 {
9342ecf0
AD
1981 for (i = 0; i < mb_block_count; i++) {
1982 if (!skip_dct[i]) {
3ada94ba
BF
1983 int overflow;
1984 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
9342ecf0
AD
1985 // FIXME we could decide to change to quantizer instead of
1986 // clipping
1987 // JS: I don't think that would be a good idea it could lower
1988 // quality instead of improve it. Just INTRADC clipping
1989 // deserves changes in quantizer
1990 if (overflow)
1991 clip_coeffs(s, s->block[i], s->block_last_index[i]);
1992 } else
1993 s->block_last_index[i] = -1;
3ada94ba 1994 }
23bfcc06 1995 if (s->quantizer_noise_shaping) {
9342ecf0
AD
1996 for (i = 0; i < mb_block_count; i++) {
1997 if (!skip_dct[i]) {
1998 s->block_last_index[i] =
1999 dct_quantize_refine(s, s->block[i], weight[i],
2000 orig[i], i, s->qscale);
3ada94ba
BF
2001 }
2002 }
2003 }
2004
9342ecf0
AD
2005 if (s->luma_elim_threshold && !s->mb_intra)
2006 for (i = 0; i < 4; i++)
3ada94ba 2007 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
9342ecf0
AD
2008 if (s->chroma_elim_threshold && !s->mb_intra)
2009 for (i = 4; i < mb_block_count; i++)
3ada94ba
BF
2010 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2011
af3d804f 2012 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
9342ecf0
AD
2013 for (i = 0; i < mb_block_count; i++) {
2014 if (s->block_last_index[i] == -1)
2015 s->coded_score[i] = INT_MAX / 256;
3ada94ba
BF
2016 }
2017 }
2018 }
2019
9342ecf0
AD
2020 if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2021 s->block_last_index[4] =
2022 s->block_last_index[5] = 0;
2023 s->block[4][0] =
2024 s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
3ada94ba
BF
2025 }
2026
9342ecf0 2027 // non c quantize code returns incorrect block_last_index FIXME
99560a4c 2028 if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
9342ecf0 2029 for (i = 0; i < mb_block_count; i++) {
3ada94ba 2030 int j;
9342ecf0
AD
2031 if (s->block_last_index[i] > 0) {
2032 for (j = 63; j > 0; j--) {
2033 if (s->block[i][s->intra_scantable.permutated[j]])
2034 break;
3ada94ba 2035 }
9342ecf0 2036 s->block_last_index[i] = j;
3ada94ba
BF
2037 }
2038 }
2039 }
2040
2041 /* huffman encode */
2042 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2043 case CODEC_ID_MPEG1VIDEO:
2044 case CODEC_ID_MPEG2VIDEO:
49fb20cb 2045 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
d6c8dcb8 2046 ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
56be950a 2047 break;
3ada94ba 2048 case CODEC_ID_MPEG4:
49fb20cb 2049 if (CONFIG_MPEG4_ENCODER)
6f13a371 2050 ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
674eeb5f 2051 break;
3ada94ba
BF
2052 case CODEC_ID_MSMPEG4V2:
2053 case CODEC_ID_MSMPEG4V3:
2054 case CODEC_ID_WMV1:
49fb20cb 2055 if (CONFIG_MSMPEG4_ENCODER)
04d38225 2056 ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
3ada94ba
BF
2057 break;
2058 case CODEC_ID_WMV2:
49fb20cb 2059 if (CONFIG_WMV2_ENCODER)
3ada94ba
BF
2060 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2061 break;
2062 case CODEC_ID_H261:
49fb20cb 2063 if (CONFIG_H261_ENCODER)
3ada94ba
BF
2064 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2065 break;
2066 case CODEC_ID_H263:
2067 case CODEC_ID_H263P:
2068 case CODEC_ID_FLV1:
2069 case CODEC_ID_RV10:
2070 case CODEC_ID_RV20:
0bd48530 2071 if (CONFIG_H263_ENCODER)
ddce8953 2072 ff_h263_encode_mb(s, s->block, motion_x, motion_y);
674eeb5f 2073 break;
3ada94ba 2074 case CODEC_ID_MJPEG:
49fb20cb 2075 if (CONFIG_MJPEG_ENCODER)
3ada94ba
BF
2076 ff_mjpeg_encode_mb(s, s->block);
2077 break;
2078 default:
2079 assert(0);
2080 }
2081}
2082
2083static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2084{
2085 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
2086 else encode_mb_internal(s, motion_x, motion_y, 16, 8);
2087}
2088
3ada94ba
BF
2089static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2090 int i;
2091
be73d76b 2092 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
3ada94ba
BF
2093
2094 /* mpeg1 */
2095 d->mb_skip_run= s->mb_skip_run;
2096 for(i=0; i<3; i++)
9342ecf0 2097 d->last_dc[i] = s->last_dc[i];
3ada94ba
BF
2098
2099 /* statistics */
2100 d->mv_bits= s->mv_bits;
2101 d->i_tex_bits= s->i_tex_bits;
2102 d->p_tex_bits= s->p_tex_bits;
2103 d->i_count= s->i_count;
2104 d->f_count= s->f_count;
2105 d->b_count= s->b_count;
2106 d->skip_count= s->skip_count;
2107 d->misc_bits= s->misc_bits;
2108 d->last_bits= 0;
2109
2110 d->mb_skipped= 0;
2111 d->qscale= s->qscale;
2112 d->dquant= s->dquant;
9f175aa0
MN
2113
2114 d->esc3_level_length= s->esc3_level_length;
3ada94ba
BF
2115}
2116
2117static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2118 int i;
2119
2120 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
be73d76b 2121 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
3ada94ba
BF
2122
2123 /* mpeg1 */
2124 d->mb_skip_run= s->mb_skip_run;
2125 for(i=0; i<3; i++)
9342ecf0 2126 d->last_dc[i] = s->last_dc[i];
3ada94ba
BF
2127
2128 /* statistics */
2129 d->mv_bits= s->mv_bits;
2130 d->i_tex_bits= s->i_tex_bits;
2131 d->p_tex_bits= s->p_tex_bits;
2132 d->i_count= s->i_count;
2133 d->f_count= s->f_count;
2134 d->b_count= s->b_count;
2135 d->skip_count= s->skip_count;
2136 d->misc_bits= s->misc_bits;
2137
2138 d->mb_intra= s->mb_intra;
2139 d->mb_skipped= s->mb_skipped;
2140 d->mv_type= s->mv_type;
2141 d->mv_dir= s->mv_dir;
2142 d->pb= s->pb;
2143 if(s->data_partitioning){
2144 d->pb2= s->pb2;
2145 d->tex_pb= s->tex_pb;
2146 }
2147 d->block= s->block;
2148 for(i=0; i<8; i++)
2149 d->block_last_index[i]= s->block_last_index[i];
2150 d->interlaced_dct= s->interlaced_dct;
2151 d->qscale= s->qscale;
9f175aa0
MN
2152
2153 d->esc3_level_length= s->esc3_level_length;
3ada94ba
BF
2154}
2155
2156static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2157 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2158 int *dmin, int *next_block, int motion_x, int motion_y)
2159{
2160 int score;
2161 uint8_t *dest_backup[3];
2162
2163 copy_context_before_encode(s, backup, type);
2164
2165 s->block= s->blocks[*next_block];
2166 s->pb= pb[*next_block];
2167 if(s->data_partitioning){
2168 s->pb2 = pb2 [*next_block];
2169 s->tex_pb= tex_pb[*next_block];
2170 }
2171
2172 if(*next_block){
2173 memcpy(dest_backup, s->dest, sizeof(s->dest));
2174 s->dest[0] = s->rd_scratchpad;
2175 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
2176 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
2177 assert(s->linesize >= 32); //FIXME
2178 }
2179
2180 encode_mb(s, motion_x, motion_y);
2181
2182 score= put_bits_count(&s->pb);
2183 if(s->data_partitioning){
2184 score+= put_bits_count(&s->pb2);
2185 score+= put_bits_count(&s->tex_pb);
2186 }
2187
2188 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
efd29844 2189 ff_MPV_decode_mb(s, s->block);
3ada94ba
BF
2190
2191 score *= s->lambda2;
2192 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2193 }
2194
2195 if(*next_block){
2196 memcpy(s->dest, dest_backup, sizeof(s->dest));
2197 }
2198
2199 if(score<*dmin){
2200 *dmin= score;
2201 *next_block^=1;
2202
2203 copy_context_after_encode(best, s, type);
2204 }
2205}
2206
2207static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2208 uint32_t *sq = ff_squareTbl + 256;
2209 int acc=0;
2210 int x,y;
2211
2212 if(w==16 && h==16)
2213 return s->dsp.sse[0](NULL, src1, src2, stride, 16);
2214 else if(w==8 && h==8)
2215 return s->dsp.sse[1](NULL, src1, src2, stride, 8);
2216
2217 for(y=0; y<h; y++){
2218 for(x=0; x<w; x++){
2219 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2220 }
2221 }
2222
2223 assert(acc>=0);
2224
2225 return acc;
2226}
2227
2228static int sse_mb(MpegEncContext *s){
2229 int w= 16;
2230 int h= 16;
2231
2232 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2233 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2234
2235 if(w==16 && h==16)
2236 if(s->avctx->mb_cmp == FF_CMP_NSSE){
657ccb5a
DB
2237 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)
2238 +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)
2239 +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);
3ada94ba 2240 }else{
657ccb5a
DB
2241 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)
2242 +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)
2243 +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);
3ada94ba
BF
2244 }
2245 else
657ccb5a
DB
2246 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)
2247 +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)
2248 +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);
3ada94ba
BF
2249}
2250
2251static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
3a84713a 2252 MpegEncContext *s= *(void**)arg;
3ada94ba
BF
2253
2254
2255 s->me.pre_pass=1;
2256 s->me.dia_size= s->avctx->pre_dia_size;
2257 s->first_slice_line=1;
2258 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2259 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2260 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2261 }
2262 s->first_slice_line=0;
2263 }
2264
2265 s->me.pre_pass=0;
2266
2267 return 0;
2268}
2269
2270static int estimate_motion_thread(AVCodecContext *c, void *arg){
3a84713a 2271 MpegEncContext *s= *(void**)arg;
3ada94ba
BF
2272
2273 ff_check_alignment();
2274
2275 s->me.dia_size= s->avctx->dia_size;
2276 s->first_slice_line=1;
2277 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2278 s->mb_x=0; //for block init below
2279 ff_init_block_index(s);
2280 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2281 s->block_index[0]+=2;
2282 s->block_index[1]+=2;
2283 s->block_index[2]+=2;
2284 s->block_index[3]+=2;
2285
2286 /* compute motion vector & mb_type and store in context */
975a1447 2287 if(s->pict_type==AV_PICTURE_TYPE_B)
3ada94ba
BF
2288 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2289 else
2290 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2291 }
2292 s->first_slice_line=0;
2293 }
2294 return 0;
2295}
2296
2297static int mb_var_thread(AVCodecContext *c, void *arg){
3a84713a 2298 MpegEncContext *s= *(void**)arg;
3ada94ba
BF
2299 int mb_x, mb_y;
2300
2301 ff_check_alignment();
2302
2303 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2304 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2305 int xx = mb_x * 16;
2306 int yy = mb_y * 16;
657ccb5a 2307 uint8_t *pix = s->new_picture.f.data[0] + (yy * s->linesize) + xx;
3ada94ba
BF
2308 int varc;
2309 int sum = s->dsp.pix_sum(pix, s->linesize);
2310
05795f35 2311 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500 + 128)>>8;
3ada94ba
BF
2312
2313 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2314 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2315 s->me.mb_var_sum_temp += varc;
2316 }
2317 }
2318 return 0;
2319}
2320
2321static void write_slice_end(MpegEncContext *s){
49fb20cb 2322 if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4){
3ada94ba
BF
2323 if(s->partitioned_frame){
2324 ff_mpeg4_merge_partitions(s);
2325 }
2326
2327 ff_mpeg4_stuffing(&s->pb);
49fb20cb 2328 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
3ada94ba
BF
2329 ff_mjpeg_encode_stuffing(&s->pb);
2330 }
2331
9f51c682 2332 avpriv_align_put_bits(&s->pb);
3ada94ba
BF
2333 flush_put_bits(&s->pb);
2334
2335 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2336 s->misc_bits+= get_bits_diff(s);
2337}
2338
2339static int encode_thread(AVCodecContext *c, void *arg){
3a84713a 2340 MpegEncContext *s= *(void**)arg;
3ada94ba 2341 int mb_x, mb_y, pdif = 0;
ec7e2582 2342 int chr_h= 16>>s->chroma_y_shift;
3ada94ba
BF
2343 int i, j;
2344 MpegEncContext best_s, backup_s;
2345 uint8_t bit_buf[2][MAX_MB_BYTES];
2346 uint8_t bit_buf2[2][MAX_MB_BYTES];
2347 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2348 PutBitContext pb[2], pb2[2], tex_pb[2];
2349//printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
2350
2351 ff_check_alignment();
2352
2353 for(i=0; i<2; i++){
2354 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2355 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2356 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2357 }
2358
2359 s->last_bits= put_bits_count(&s->pb);
2360 s->mv_bits=0;
2361 s->misc_bits=0;
2362 s->i_tex_bits=0;
2363 s->p_tex_bits=0;
2364 s->i_count=0;
2365 s->f_count=0;
2366 s->b_count=0;
2367 s->skip_count=0;
2368
2369 for(i=0; i<3; i++){
2370 /* init last dc values */
2371 /* note: quant matrix value (8) is implied here */
2372 s->last_dc[i] = 128 << s->intra_dc_precision;
2373
657ccb5a 2374 s->current_picture.f.error[i] = 0;
3ada94ba
BF
2375 }
2376 s->mb_skip_run = 0;
2377 memset(s->last_mv, 0, sizeof(s->last_mv));
2378
2379 s->last_mv_dir = 0;
2380
2381 switch(s->codec_id){
2382 case CODEC_ID_H263:
2383 case CODEC_ID_H263P:
2384 case CODEC_ID_FLV1:
0bd48530 2385 if (CONFIG_H263_ENCODER)
674eeb5f 2386 s->gob_index = ff_h263_get_gob_height(s);
3ada94ba
BF
2387 break;
2388 case CODEC_ID_MPEG4:
49fb20cb 2389 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
3ada94ba
BF
2390 ff_mpeg4_init_partitions(s);
2391 break;
2392 }
2393
2394 s->resync_mb_x=0;
2395 s->resync_mb_y=0;
2396 s->first_slice_line = 1;
2397 s->ptr_lastgob = s->pb.buf;
2398 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2399// printf("row %d at %X\n", s->mb_y, (int)s);
2400 s->mb_x=0;
2401 s->mb_y= mb_y;
2402
2403 ff_set_qscale(s, s->qscale);
2404 ff_init_block_index(s);
2405
2406 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2407 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2408 int mb_type= s->mb_type[xy];
2409// int d;
2410 int dmin= INT_MAX;
2411 int dir;
2412
2413 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2414 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2415 return -1;
2416 }
2417 if(s->data_partitioning){
2418 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2419 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2420 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2421 return -1;
2422 }
2423 }
2424
2425 s->mb_x = mb_x;
2426 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2427 ff_update_block_index(s);
2428
49fb20cb 2429 if(CONFIG_H261_ENCODER && s->codec_id == CODEC_ID_H261){
3ada94ba
BF
2430 ff_h261_reorder_mb_index(s);
2431 xy= s->mb_y*s->mb_stride + s->mb_x;
2432 mb_type= s->mb_type[xy];
2433 }
2434
2435 /* write gob / video packet header */
2436 if(s->rtp_mode){
2437 int current_packet_size, is_gob_start;
2438
2439 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2440
2441 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2442
2443 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2444
2445 switch(s->codec_id){
2446 case CODEC_ID_H263:
2447 case CODEC_ID_H263P:
2448 if(!s->h263_slice_structured)
2449 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2450 break;
2451 case CODEC_ID_MPEG2VIDEO:
2452 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2453 case CODEC_ID_MPEG1VIDEO:
2454 if(s->mb_skip_run) is_gob_start=0;
2455 break;
2456 }
2457
2458 if(is_gob_start){
2459 if(s->start_mb_y != mb_y || mb_x!=0){
2460 write_slice_end(s);
2461
49fb20cb 2462 if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
3ada94ba
BF
2463 ff_mpeg4_init_partitions(s);
2464 }
2465 }
2466
2467 assert((put_bits_count(&s->pb)&7) == 0);
fb53b4a0 2468 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
3ada94ba
BF
2469
2470 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
2471 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2472 int d= 100 / s->avctx->error_rate;
2473 if(r % d == 0){
2474 current_packet_size=0;
3ada94ba 2475 s->pb.buf_ptr= s->ptr_lastgob;
fb53b4a0 2476 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
3ada94ba
BF
2477 }
2478 }
2479
2480 if (s->avctx->rtp_callback){
2481 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2482 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2483 }
2484
2485 switch(s->codec_id){
2486 case CODEC_ID_MPEG4:
49fb20cb 2487 if (CONFIG_MPEG4_ENCODER) {
674eeb5f
AJ
2488 ff_mpeg4_encode_video_packet_header(s);
2489 ff_mpeg4_clean_buffers(s);
eb75a698 2490 }
3ada94ba
BF
2491 break;
2492 case CODEC_ID_MPEG1VIDEO:
2493 case CODEC_ID_MPEG2VIDEO:
49fb20cb 2494 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
56be950a
AJ
2495 ff_mpeg1_encode_slice_header(s);
2496 ff_mpeg1_clean_buffers(s);
a6bc5731 2497 }
3ada94ba
BF
2498 break;
2499 case CODEC_ID_H263:
2500 case CODEC_ID_H263P:
965424da 2501 if (CONFIG_H263_ENCODER)
ddce8953 2502 ff_h263_encode_gob_header(s, mb_y);
3ada94ba
BF
2503 break;
2504 }
2505
2506 if(s->flags&CODEC_FLAG_PASS1){
2507 int bits= put_bits_count(&s->pb);
2508 s->misc_bits+= bits - s->last_bits;
2509 s->last_bits= bits;
2510 }
2511
2512 s->ptr_lastgob += current_packet_size;
2513 s->first_slice_line=1;
2514 s->resync_mb_x=mb_x;
2515 s->resync_mb_y=mb_y;
2516 }
2517 }
2518
2519 if( (s->resync_mb_x == s->mb_x)
2520 && s->resync_mb_y+1 == s->mb_y){
2521 s->first_slice_line=0;
2522 }
2523
2524 s->mb_skipped=0;
2525 s->dquant=0; //only for QP_RD
2526
ff71a383 2527 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
3ada94ba
BF
2528 int next_block=0;
2529 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2530
2531 copy_context_before_encode(&backup_s, s, -1);
2532 backup_s.pb= s->pb;
2533 best_s.data_partitioning= s->data_partitioning;
2534 best_s.partitioned_frame= s->partitioned_frame;
2535 if(s->data_partitioning){
2536 backup_s.pb2= s->pb2;
2537 backup_s.tex_pb= s->tex_pb;
2538 }
2539
2540 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2541 s->mv_dir = MV_DIR_FORWARD;
2542 s->mv_type = MV_TYPE_16X16;
2543 s->mb_intra= 0;
2544 s->mv[0][0][0] = s->p_mv_table[xy][0];
2545 s->mv[0][0][1] = s->p_mv_table[xy][1];
2546 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2547 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2548 }
2549 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2550 s->mv_dir = MV_DIR_FORWARD;
2551 s->mv_type = MV_TYPE_FIELD;
2552 s->mb_intra= 0;
2553 for(i=0; i<2; i++){
2554 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2555 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2556 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2557 }
2558 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2559 &dmin, &next_block, 0, 0);
2560 }
2561 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2562 s->mv_dir = MV_DIR_FORWARD;
2563 s->mv_type = MV_TYPE_16X16;
2564 s->mb_intra= 0;
2565 s->mv[0][0][0] = 0;
2566 s->mv[0][0][1] = 0;
2567 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2568 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2569 }
2570 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2571 s->mv_dir = MV_DIR_FORWARD;
2572 s->mv_type = MV_TYPE_8X8;
2573 s->mb_intra= 0;
2574 for(i=0; i<4; i++){
657ccb5a
DB
2575 s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
2576 s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
3ada94ba
BF
2577 }
2578 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2579 &dmin, &next_block, 0, 0);
2580 }
2581 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2582 s->mv_dir = MV_DIR_FORWARD;
2583 s->mv_type = MV_TYPE_16X16;
2584 s->mb_intra= 0;
2585 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2586 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2587 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2588 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2589 }
2590 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2591 s->mv_dir = MV_DIR_BACKWARD;
2592 s->mv_type = MV_TYPE_16X16;
2593 s->mb_intra= 0;
2594 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2595 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2596 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2597 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2598 }
2599 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2600 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2601 s->mv_type = MV_TYPE_16X16;
2602 s->mb_intra= 0;
2603 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2604 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2605 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2606 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2607 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2608 &dmin, &next_block, 0, 0);
2609 }
2610 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2611 s->mv_dir = MV_DIR_FORWARD;
2612 s->mv_type = MV_TYPE_FIELD;
2613 s->mb_intra= 0;
2614 for(i=0; i<2; i++){
2615 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2616 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2617 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2618 }
2619 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2620 &dmin, &next_block, 0, 0);
2621 }
2622 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2623 s->mv_dir = MV_DIR_BACKWARD;
2624 s->mv_type = MV_TYPE_FIELD;
2625 s->mb_intra= 0;
2626 for(i=0; i<2; i++){
2627 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2628 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2629 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2630 }
2631 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2632 &dmin, &next_block, 0, 0);
2633 }
2634 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2635 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2636 s->mv_type = MV_TYPE_FIELD;
2637 s->mb_intra= 0;
2638 for(dir=0; dir<2; dir++){
2639 for(i=0; i<2; i++){
2640 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2641 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2642 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2643 }
2644 }
2645 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2646 &dmin, &next_block, 0, 0);
2647 }
2648 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2649 s->mv_dir = 0;
2650 s->mv_type = MV_TYPE_16X16;
2651 s->mb_intra= 1;
2652 s->mv[0][0][0] = 0;
2653 s->mv[0][0][1] = 0;
2654 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2655 &dmin, &next_block, 0, 0);
2656 if(s->h263_pred || s->h263_aic){
2657 if(best_s.mb_intra)
2658 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2659 else
2660 ff_clean_intra_table_entries(s); //old mode?
2661 }
2662 }
2663
ff71a383 2664 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3ada94ba
BF
2665 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2666 const int last_qp= backup_s.qscale;
2667 int qpi, qp, dc[6];
2668 DCTELEM ac[6][16];
2669 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2670 static const int dquant_tab[4]={-1,1,-2,2};
2671
2672 assert(backup_s.dquant == 0);
2673
2674 //FIXME intra
2675 s->mv_dir= best_s.mv_dir;
2676 s->mv_type = MV_TYPE_16X16;
2677 s->mb_intra= best_s.mb_intra;
2678 s->mv[0][0][0] = best_s.mv[0][0][0];
2679 s->mv[0][0][1] = best_s.mv[0][0][1];
2680 s->mv[1][0][0] = best_s.mv[1][0][0];
2681 s->mv[1][0][1] = best_s.mv[1][0][1];
2682
975a1447 2683 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3ada94ba
BF
2684 for(; qpi<4; qpi++){
2685 int dquant= dquant_tab[qpi];
2686 qp= last_qp + dquant;
2687 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2688 continue;
2689 backup_s.dquant= dquant;
2690 if(s->mb_intra && s->dc_val[0]){
2691 for(i=0; i<6; i++){
2692 dc[i]= s->dc_val[0][ s->block_index[i] ];
2693 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
2694 }
2695 }
2696
2697 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2698 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2699 if(best_s.qscale != qp){
2700 if(s->mb_intra && s->dc_val[0]){
2701 for(i=0; i<6; i++){
2702 s->dc_val[0][ s->block_index[i] ]= dc[i];
2703 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
2704 }
2705 }
2706 }
2707 }
2708 }
2709 }
49fb20cb 2710 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3ada94ba
BF
2711 int mx= s->b_direct_mv_table[xy][0];
2712 int my= s->b_direct_mv_table[xy][1];
2713
2714 backup_s.dquant = 0;
2715 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2716 s->mb_intra= 0;
2717 ff_mpeg4_set_direct_mv(s, mx, my);
2718 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2719 &dmin, &next_block, mx, my);
2720 }
49fb20cb 2721 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3ada94ba
BF
2722 backup_s.dquant = 0;
2723 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2724 s->mb_intra= 0;
2725 ff_mpeg4_set_direct_mv(s, 0, 0);
2726 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2727 &dmin, &next_block, 0, 0);
2728 }
ed019b8e 2729 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3ada94ba
BF
2730 int coded=0;
2731 for(i=0; i<6; i++)
2732 coded |= s->block_last_index[i];
2733 if(coded){
2734 int mx,my;
2735 memcpy(s->mv, best_s.mv, sizeof(s->mv));
49fb20cb 2736 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
3ada94ba
BF
2737 mx=my=0; //FIXME find the one we actually used
2738 ff_mpeg4_set_direct_mv(s, mx, my);
2739 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2740 mx= s->mv[1][0][0];
2741 my= s->mv[1][0][1];
2742 }else{
2743 mx= s->mv[0][0][0];
2744 my= s->mv[0][0][1];
2745 }
2746
2747 s->mv_dir= best_s.mv_dir;
2748 s->mv_type = best_s.mv_type;
2749 s->mb_intra= 0;
2750/* s->mv[0][0][0] = best_s.mv[0][0][0];
2751 s->mv[0][0][1] = best_s.mv[0][0][1];
2752 s->mv[1][0][0] = best_s.mv[1][0][0];
2753 s->mv[1][0][1] = best_s.mv[1][0][1];*/
2754 backup_s.dquant= 0;
2755 s->skipdct=1;
2756 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2757 &dmin, &next_block, mx, my);
2758 s->skipdct=0;
2759 }
2760 }
2761
657ccb5a 2762 s->current_picture.f.qscale_table[xy] = best_s.qscale;
3ada94ba
BF
2763
2764 copy_context_after_encode(s, &best_s, -1);
2765
2766 pb_bits_count= put_bits_count(&s->pb);
2767 flush_put_bits(&s->pb);
9f51c682 2768 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3ada94ba
BF
2769 s->pb= backup_s.pb;
2770
2771 if(s->data_partitioning){
2772 pb2_bits_count= put_bits_count(&s->pb2);
2773 flush_put_bits(&s->pb2);
9f51c682 2774 avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3ada94ba
BF
2775 s->pb2= backup_s.pb2;
2776
2777 tex_pb_bits_count= put_bits_count(&s->tex_pb);
2778 flush_put_bits(&s->tex_pb);
9f51c682 2779 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3ada94ba
BF
2780 s->tex_pb= backup_s.tex_pb;
2781 }
2782 s->last_bits= put_bits_count(&s->pb);
2783
f34121f3 2784 if (CONFIG_H263_ENCODER &&
975a1447 2785 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3ada94ba
BF
2786 ff_h263_update_motion_val(s);
2787
2788 if(next_block==0){ //FIXME 16 vs linesize16
2789 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
2790 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
2791 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2792 }
2793
2794 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
efd29844 2795 ff_MPV_decode_mb(s, s->block);
3ada94ba
BF
2796 } else {
2797 int motion_x = 0, motion_y = 0;
2798 s->mv_type=MV_TYPE_16X16;
2799 // only one MB-Type possible
2800
2801 switch(mb_type){
2802 case CANDIDATE_MB_TYPE_INTRA:
2803 s->mv_dir = 0;
2804 s->mb_intra= 1;
2805 motion_x= s->mv[0][0][0] = 0;
2806 motion_y= s->mv[0][0][1] = 0;
2807 break;
2808 case CANDIDATE_MB_TYPE_INTER:
2809 s->mv_dir = MV_DIR_FORWARD;
2810 s->mb_intra= 0;
2811 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2812 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2813 break;
2814 case CANDIDATE_MB_TYPE_INTER_I:
2815 s->mv_dir = MV_DIR_FORWARD;
2816 s->mv_type = MV_TYPE_FIELD;
2817 s->mb_intra= 0;
2818 for(i=0; i<2; i++){
2819 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2820 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2821 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2822 }
2823 break;
2824 case CANDIDATE_MB_TYPE_INTER4V:
2825 s->mv_dir = MV_DIR_FORWARD;
2826 s->mv_type = MV_TYPE_8X8;
2827 s->mb_intra= 0;
2828 for(i=0; i<4; i++){
657ccb5a
DB
2829 s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
2830 s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
3ada94ba
BF
2831 }
2832 break;
2833 case CANDIDATE_MB_TYPE_DIRECT:
49fb20cb 2834 if (CONFIG_MPEG4_ENCODER) {
674eeb5f
AJ
2835 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2836 s->mb_intra= 0;
2837 motion_x=s->b_direct_mv_table[xy][0];
2838 motion_y=s->b_direct_mv_table[xy][1];
2839 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
eb75a698 2840 }
3ada94ba
BF
2841 break;
2842 case CANDIDATE_MB_TYPE_DIRECT0:
49fb20cb 2843 if (CONFIG_MPEG4_ENCODER) {
674eeb5f
AJ
2844 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2845 s->mb_intra= 0;
2846 ff_mpeg4_set_direct_mv(s, 0, 0);
eb75a698 2847 }
3ada94ba
BF
2848 break;
2849 case CANDIDATE_MB_TYPE_BIDIR:
2850 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2851 s->mb_intra= 0;
2852 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2853 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2854 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2855 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2856 break;
2857 case CANDIDATE_MB_TYPE_BACKWARD:
2858 s->mv_dir = MV_DIR_BACKWARD;
2859 s->mb_intra= 0;
2860 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2861 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2862 break;
2863 case CANDIDATE_MB_TYPE_FORWARD:
2864 s->mv_dir = MV_DIR_FORWARD;
2865 s->mb_intra= 0;
2866 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2867 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2868// printf(" %d %d ", motion_x, motion_y);
2869 break;
2870 case CANDIDATE_MB_TYPE_FORWARD_I:
2871 s->mv_dir = MV_DIR_FORWARD;
2872 s->mv_type = MV_TYPE_FIELD;
2873 s->mb_intra= 0;
2874 for(i=0; i<2; i++){
2875 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2876 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2877 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2878 }
2879 break;
2880 case CANDIDATE_MB_TYPE_BACKWARD_I:
2881 s->mv_dir = MV_DIR_BACKWARD;
2882 s->mv_type = MV_TYPE_FIELD;
2883 s->mb_intra= 0;
2884 for(i=0; i<2; i++){
2885 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2886 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2887 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2888 }
2889 break;
2890 case CANDIDATE_MB_TYPE_BIDIR_I:
2891 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2892 s->mv_type = MV_TYPE_FIELD;
2893 s->mb_intra= 0;
2894 for(dir=0; dir<2; dir++){
2895 for(i=0; i<2; i++){
2896 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2897 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2898 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2899 }
2900 }
2901 break;
2902 default:
2903 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
2904 }
2905
2906 encode_mb(s, motion_x, motion_y);
2907
2908 // RAL: Update last macroblock type
2909 s->last_mv_dir = s->mv_dir;
2910
f34121f3 2911 if (CONFIG_H263_ENCODER &&
975a1447 2912 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3ada94ba
BF
2913 ff_h263_update_motion_val(s);
2914
efd29844 2915 ff_MPV_decode_mb(s, s->block);
3ada94ba
BF
2916 }
2917
2918 /* clean the MV table in IPS frames for direct mode in B frames */
2919 if(s->mb_intra /* && I,P,S_TYPE */){
2920 s->p_mv_table[xy][0]=0;
2921 s->p_mv_table[xy][1]=0;
2922 }
2923
2924 if(s->flags&CODEC_FLAG_PSNR){
2925 int w= 16;
2926 int h= 16;
2927
2928 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2929 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2930
657ccb5a
DB
2931 s->current_picture.f.error[0] += sse(
2932 s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3ada94ba 2933 s->dest[0], w, h, s->linesize);
657ccb5a
DB
2934 s->current_picture.f.error[1] += sse(
2935 s, s->new_picture.f.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
ec7e2582 2936 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
657ccb5a
DB
2937 s->current_picture.f.error[2] += sse(
2938 s, s->new_picture.f.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
ec7e2582 2939 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3ada94ba
BF
2940 }
2941 if(s->loop_filter){
f34121f3 2942 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3ada94ba
BF
2943 ff_h263_loop_filter(s);
2944 }
2945//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
2946 }
2947 }
2948
2949 //not beautiful here but we must write it before flushing so it has to be here
975a1447 2950 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
04d38225 2951 ff_msmpeg4_encode_ext_header(s);
3ada94ba
BF
2952
2953 write_slice_end(s);
2954
2955 /* Send the last GOB if RTP */
2956 if (s->avctx->rtp_callback) {
2957 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
fb53b4a0 2958 pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3ada94ba
BF
2959 /* Call the RTP callback to send the last GOB */
2960 emms_c();
2961 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
2962 }
2963
2964 return 0;
2965}
2966
2967#define MERGE(field) dst->field += src->field; src->field=0
2968static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
2969 MERGE(me.scene_change_score);
2970 MERGE(me.mc_mb_var_sum_temp);
2971 MERGE(me.mb_var_sum_temp);
2972}
2973
2974static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
2975 int i;
2976
2977 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
2978 MERGE(dct_count[1]);
2979 MERGE(mv_bits);
2980 MERGE(i_tex_bits);
2981 MERGE(p_tex_bits);
2982 MERGE(i_count);
2983 MERGE(f_count);
2984 MERGE(b_count);
2985 MERGE(skip_count);
2986 MERGE(misc_bits);
2987 MERGE(error_count);
2988 MERGE(padding_bug_score);
657ccb5a
DB
2989 MERGE(current_picture.f.error[0]);
2990 MERGE(current_picture.f.error[1]);
2991 MERGE(current_picture.f.error[2]);
3ada94ba
BF
2992
2993 if(dst->avctx->noise_reduction){
2994 for(i=0; i<64; i++){
2995 MERGE(dct_error_sum[0][i]);
2996 MERGE(dct_error_sum[1][i]);
2997 }
2998 }
2999
3000 assert(put_bits_count(&src->pb) % 8 ==0);
3001 assert(put_bits_count(&dst->pb) % 8 ==0);
9f51c682 3002 avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3ada94ba
BF
3003 flush_put_bits(&dst->pb);
3004}
3005
3006static int estimate_qp(MpegEncContext *s, int dry_run){
3007 if (s->next_lambda){
657ccb5a
DB
3008 s->current_picture_ptr->f.quality =
3009 s->current_picture.f.quality = s->next_lambda;
3ada94ba
BF
3010 if(!dry_run) s->next_lambda= 0;
3011 } else if (!s->fixed_qscale) {
657ccb5a
DB
3012 s->current_picture_ptr->f.quality =
3013 s->current_picture.f.quality = ff_rate_estimate_qscale(s, dry_run);
3014 if (s->current_picture.f.quality < 0)
3ada94ba
BF
3015 return -1;
3016 }
3017
3018 if(s->adaptive_quant){
3019 switch(s->codec_id){
3020 case CODEC_ID_MPEG4:
49fb20cb 3021 if (CONFIG_MPEG4_ENCODER)
674eeb5f 3022 ff_clean_mpeg4_qscales(s);
3ada94ba
BF
3023 break;
3024 case CODEC_ID_H263:
3025 case CODEC_ID_H263P:
3026 case CODEC_ID_FLV1:
0bd48530 3027 if (CONFIG_H263_ENCODER)
674eeb5f 3028 ff_clean_h263_qscales(s);
3ada94ba 3029 break;
ccc4b918
MN
3030 default:
3031 ff_init_qscale_tab(s);
3ada94ba
BF
3032 }
3033
3034 s->lambda= s->lambda_table[0];
3035 //FIXME broken
3036 }else
657ccb5a 3037 s->lambda = s->current_picture.f.quality;
3ada94ba
BF
3038//printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
3039 update_qscale(s);
3040 return 0;
3041}
3042
7da31a80
AJ
3043/* must be called before writing the header */
3044static void set_frame_distances(MpegEncContext * s){
95a06eb4 3045 assert(s->current_picture_ptr->f.pts != AV_NOPTS_VALUE);
657ccb5a 3046 s->time = s->current_picture_ptr->f.pts * s->avctx->time_base.num;
7da31a80 3047
975a1447 3048 if(s->pict_type==AV_PICTURE_TYPE_B){
7da31a80
AJ
3049 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3050 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3051 }else{
3052 s->pp_time= s->time - s->last_non_b_time;
3053 s->last_non_b_time= s->time;
3054 assert(s->picture_number==0 || s->pp_time > 0);
3055 }
3056}
3057
3ada94ba
BF
3058static int encode_picture(MpegEncContext *s, int picture_number)
3059{
3060 int i;
3061 int bits;
881a5e04 3062 int context_count = s->slice_context_count;
3ada94ba
BF
3063
3064 s->picture_number = picture_number;
3065
3066 /* Reset the average MB variance */
3067 s->me.mb_var_sum_temp =
3068 s->me.mc_mb_var_sum_temp = 0;
3069
3070 /* we need to initialize some time vars before we can encode b-frames */
3071 // RAL: Condition added for MPEG1VIDEO
8c51620f 3072 if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
7da31a80 3073 set_frame_distances(s);
49fb20cb 3074 if(CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4)
7da31a80 3075 ff_set_mpeg4_time(s);
3ada94ba
BF
3076
3077 s->me.scene_change_score=0;
3078
e6dba5df 3079// s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3ada94ba 3080
975a1447 3081 if(s->pict_type==AV_PICTURE_TYPE_I){
3ada94ba
BF
3082 if(s->msmpeg4_version >= 3) s->no_rounding=1;
3083 else s->no_rounding=0;
975a1447 3084 }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3ada94ba
BF
3085 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
3086 s->no_rounding ^= 1;
3087 }
3088
3089 if(s->flags & CODEC_FLAG_PASS2){
3090 if (estimate_qp(s,1) < 0)
3091 return -1;
3092 ff_get_2pass_fcode(s);
3093 }else if(!(s->flags & CODEC_FLAG_QSCALE)){
975a1447 3094 if(s->pict_type==AV_PICTURE_TYPE_B)
3ada94ba
BF
3095 s->lambda= s->last_lambda_for[s->pict_type];
3096 else
3097 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3098 update_qscale(s);
3099 }
3100
3101 s->mb_intra=0; //for the rate distortion & bit compare functions
6a9c8594 3102 for(i=1; i<context_count; i++){
3ada94ba
BF
3103 ff_update_duplicate_context(s->thread_context[i], s);
3104 }
3105
719f3702
MN
3106 if(ff_init_me(s)<0)
3107 return -1;
3ada94ba
BF
3108
3109 /* Estimate motion for every MB */
975a1447 3110 if(s->pict_type != AV_PICTURE_TYPE_I){
3ada94ba
BF
3111 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3112 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
975a1447
SS
3113 if(s->pict_type != AV_PICTURE_TYPE_B && s->avctx->me_threshold==0){
3114 if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
6a9c8594 3115 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3ada94ba
BF
3116 }
3117 }
3118
6a9c8594 3119 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
975a1447 3120 }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3ada94ba
BF
3121 /* I-Frame */
3122 for(i=0; i<s->mb_stride*s->mb_height; i++)
3123 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3124
3125 if(!s->fixed_qscale){
3126 /* finding spatial complexity for I-frame rate control */
6a9c8594 3127 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3ada94ba
BF
3128 }
3129 }
6a9c8594 3130 for(i=1; i<context_count; i++){
3ada94ba
BF
3131 merge_context_after_me(s, s->thread_context[i]);
3132 }
3133 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3134 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
3135 emms_c();
3136
975a1447
SS
3137 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
3138 s->pict_type= AV_PICTURE_TYPE_I;
3ada94ba
BF
3139 for(i=0; i<s->mb_stride*s->mb_height; i++)
3140 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3141//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3142 }
3143
3144 if(!s->umvplus){
975a1447 3145 if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3ada94ba
BF
3146 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3147
3148 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3149 int a,b;
3150 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3151 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
159ef4b0 3152 s->f_code= FFMAX3(s->f_code, a, b);
3ada94ba
BF
3153 }
3154
3155 ff_fix_long_p_mvs(s);
3156 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3157 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3158 int j;
3159 for(i=0; i<2; i++){
3160 for(j=0; j<2; j++)
3161 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3162 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3163 }
3164 }
3165 }
3166
975a1447 3167 if(s->pict_type==AV_PICTURE_TYPE_B){
3ada94ba
BF
3168 int a, b;
3169
3170 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3171 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3172 s->f_code = FFMAX(a, b);
3173
3174 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3175 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3176 s->b_code = FFMAX(a, b);
3177
3178 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3179 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3180 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3181 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3182 if(s->flags & CODEC_FLAG_INTERLACED_ME){
3183 int dir, j;
3184 for(dir=0; dir<2; dir++){
3185 for(i=0; i<2; i++){
3186 for(j=0; j<2; j++){
3187 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3188 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3189 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3190 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3191 }
3192 }
3193 }
3194 }
3195 }
3196 }
3197
3198 if (estimate_qp(s, 0) < 0)
3199 return -1;
3200
975a1447 3201 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==AV_PICTURE_TYPE_I && !(s->flags & CODEC_FLAG_QSCALE))
3ada94ba
BF
3202 s->qscale= 3; //reduce clipping problems
3203
3204 if (s->out_format == FMT_MJPEG) {
3205 /* for mjpeg, we do include qscale in the matrix */
3ada94ba
BF
3206 for(i=1;i<64;i++){
3207 int j= s->dsp.idct_permutation[i];
3208
3209 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3210 }
6506c3d2 3211 s->y_dc_scale_table=
53f66cee
MN
3212 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3213 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
69cea75f 3214 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
3ada94ba
BF
3215 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3216 s->qscale= 8;
3217 }
3218
3219 //FIXME var duplication
657ccb5a
DB
3220 s->current_picture_ptr->f.key_frame =
3221 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3222 s->current_picture_ptr->f.pict_type =
3223 s->current_picture.f.pict_type = s->pict_type;
3ada94ba 3224
657ccb5a 3225 if (s->current_picture.f.key_frame)
3ada94ba
BF
3226 s->picture_in_gop_number=0;
3227
3228 s->last_bits= put_bits_count(&s->pb);
3229 switch(s->out_format) {
3230 case FMT_MJPEG:
49fb20cb 3231 if (CONFIG_MJPEG_ENCODER)
3ada94ba
BF
3232 ff_mjpeg_encode_picture_header(s);
3233 break;
3234 case FMT_H261:
49fb20cb 3235 if (CONFIG_H261_ENCODER)
3ada94ba
BF
3236 ff_h261_encode_picture_header(s, picture_number);
3237 break;
3238 case FMT_H263:
49fb20cb 3239 if (CONFIG_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
3ada94ba 3240 ff_wmv2_encode_picture_header(s, picture_number);
8c51620f 3241 else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
04d38225 3242 ff_msmpeg4_encode_picture_header(s, picture_number);
49fb20cb 3243 else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
6f13a371 3244 ff_mpeg4_encode_picture_header(s, picture_number);
49fb20cb 3245 else if (CONFIG_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
6c28d657 3246 ff_rv10_encode_picture_header(s, picture_number);
49fb20cb 3247 else if (CONFIG_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
6c28d657 3248 ff_rv20_encode_picture_header(s, picture_number);
49fb20cb 3249