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