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