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