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