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