hwaccel: Rename priv_data_size to frame_priv_data_size
[libav.git] / libavcodec / mpegvideo.c
CommitLineData
de6d9b64
FB
1/*
2 * The simplest mpeg encoder (well, it was the simplest!)
406792e7 3 * Copyright (c) 2000,2001 Fabrice Bellard
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 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.
b78e7197 9 *
2912e87a 10 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
b78e7197 13 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 14 *
2912e87a 15 * Libav is distributed in the hope that it will be useful,
de6d9b64 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
de6d9b64 19 *
ff4ec49e 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
5509bffa 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 23 */
115329f1 24
983e3246 25/**
ba87f080 26 * @file
983e3246 27 * The simplest mpeg encoder (well, it was the simplest!).
115329f1
DB
28 */
29
6fee1b90 30#include "libavutil/attributes.h"
759001c5 31#include "libavutil/avassert.h"
737eb597 32#include "libavutil/imgutils.h"
19e30a58 33#include "libavutil/internal.h"
fb0c9d41 34#include "libavutil/timer.h"
de6d9b64
FB
35#include "avcodec.h"
36#include "dsputil.h"
603a5f04 37#include "internal.h"
9734b8ba 38#include "mathops.h"
e0c16e4e 39#include "mpegutils.h"
de6d9b64 40#include "mpegvideo.h"
d9c9259f 41#include "mjpegenc.h"
15025553 42#include "msmpeg4.h"
4440bd0d 43#include "xvmc_internal.h"
6a9c8594 44#include "thread.h"
e96682e6 45#include <limits.h>
de6d9b64 46
363114e8
KT
47static const uint8_t ff_default_chroma_qscale_table[32] = {
48// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
49 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
50 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
332f9ac4
MN
51};
52
363114e8
KT
53const uint8_t ff_mpeg1_dc_scale_table[128] = {
54// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
55 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
56 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
57 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
59 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
60 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
61 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
62 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
6cbe71bd
AJ
63};
64
363114e8
KT
65static const uint8_t mpeg2_dc_scale_table1[128] = {
66// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
67 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
68 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
69 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
70 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
71 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
72 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
73 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
74 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
6506c3d2
MN
75};
76
363114e8
KT
77static const uint8_t mpeg2_dc_scale_table2[128] = {
78// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
79 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
80 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
81 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
84 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
86 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
6506c3d2
MN
87};
88
363114e8
KT
89static const uint8_t mpeg2_dc_scale_table3[128] = {
90// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
91 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
92 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
93 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
94 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
95 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
96 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
97 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
98 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
6506c3d2
MN
99};
100
363114e8 101const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
6506c3d2
MN
102 ff_mpeg1_dc_scale_table,
103 mpeg2_dc_scale_table1,
104 mpeg2_dc_scale_table2,
105 mpeg2_dc_scale_table3,
106};
107
a4d0c6e0
AK
108static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
109 int16_t *block, int n, int qscale)
110{
111 int i, level, nCoeffs;
112 const uint16_t *quant_matrix;
113
114 nCoeffs= s->block_last_index[n];
115
116 if (n < 4)
117 block[0] = block[0] * s->y_dc_scale;
118 else
119 block[0] = block[0] * s->c_dc_scale;
120 /* XXX: only mpeg1 */
121 quant_matrix = s->intra_matrix;
122 for(i=1;i<=nCoeffs;i++) {
123 int j= s->intra_scantable.permutated[i];
124 level = block[j];
125 if (level) {
126 if (level < 0) {
127 level = -level;
128 level = (int)(level * qscale * quant_matrix[j]) >> 3;
129 level = (level - 1) | 1;
130 level = -level;
131 } else {
132 level = (int)(level * qscale * quant_matrix[j]) >> 3;
133 level = (level - 1) | 1;
134 }
135 block[j] = level;
136 }
137 }
138}
139
140static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
141 int16_t *block, int n, int qscale)
142{
143 int i, level, nCoeffs;
144 const uint16_t *quant_matrix;
145
146 nCoeffs= s->block_last_index[n];
147
148 quant_matrix = s->inter_matrix;
149 for(i=0; i<=nCoeffs; i++) {
150 int j= s->intra_scantable.permutated[i];
151 level = block[j];
152 if (level) {
153 if (level < 0) {
154 level = -level;
155 level = (((level << 1) + 1) * qscale *
156 ((int) (quant_matrix[j]))) >> 4;
157 level = (level - 1) | 1;
158 level = -level;
159 } else {
160 level = (((level << 1) + 1) * qscale *
161 ((int) (quant_matrix[j]))) >> 4;
162 level = (level - 1) | 1;
163 }
164 block[j] = level;
165 }
166 }
167}
168
169static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
170 int16_t *block, int n, int qscale)
171{
172 int i, level, nCoeffs;
173 const uint16_t *quant_matrix;
174
175 if(s->alternate_scan) nCoeffs= 63;
176 else nCoeffs= s->block_last_index[n];
177
178 if (n < 4)
179 block[0] = block[0] * s->y_dc_scale;
180 else
181 block[0] = block[0] * s->c_dc_scale;
182 quant_matrix = s->intra_matrix;
183 for(i=1;i<=nCoeffs;i++) {
184 int j= s->intra_scantable.permutated[i];
185 level = block[j];
186 if (level) {
187 if (level < 0) {
188 level = -level;
189 level = (int)(level * qscale * quant_matrix[j]) >> 3;
190 level = -level;
191 } else {
192 level = (int)(level * qscale * quant_matrix[j]) >> 3;
193 }
194 block[j] = level;
195 }
196 }
197}
198
199static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
200 int16_t *block, int n, int qscale)
201{
202 int i, level, nCoeffs;
203 const uint16_t *quant_matrix;
204 int sum=-1;
205
206 if(s->alternate_scan) nCoeffs= 63;
207 else nCoeffs= s->block_last_index[n];
208
209 if (n < 4)
210 block[0] = block[0] * s->y_dc_scale;
211 else
212 block[0] = block[0] * s->c_dc_scale;
213 quant_matrix = s->intra_matrix;
214 for(i=1;i<=nCoeffs;i++) {
215 int j= s->intra_scantable.permutated[i];
216 level = block[j];
217 if (level) {
218 if (level < 0) {
219 level = -level;
220 level = (int)(level * qscale * quant_matrix[j]) >> 3;
221 level = -level;
222 } else {
223 level = (int)(level * qscale * quant_matrix[j]) >> 3;
224 }
225 block[j] = level;
226 sum+=level;
227 }
228 }
229 block[63]^=sum&1;
230}
231
232static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
233 int16_t *block, int n, int qscale)
234{
235 int i, level, nCoeffs;
236 const uint16_t *quant_matrix;
237 int sum=-1;
238
239 if(s->alternate_scan) nCoeffs= 63;
240 else nCoeffs= s->block_last_index[n];
241
242 quant_matrix = s->inter_matrix;
243 for(i=0; i<=nCoeffs; i++) {
244 int j= s->intra_scantable.permutated[i];
245 level = block[j];
246 if (level) {
247 if (level < 0) {
248 level = -level;
249 level = (((level << 1) + 1) * qscale *
250 ((int) (quant_matrix[j]))) >> 4;
251 level = -level;
252 } else {
253 level = (((level << 1) + 1) * qscale *
254 ((int) (quant_matrix[j]))) >> 4;
255 }
256 block[j] = level;
257 sum+=level;
258 }
259 }
260 block[63]^=sum&1;
261}
262
263static void dct_unquantize_h263_intra_c(MpegEncContext *s,
264 int16_t *block, int n, int qscale)
265{
266 int i, level, qmul, qadd;
267 int nCoeffs;
268
269 assert(s->block_last_index[n]>=0);
270
271 qmul = qscale << 1;
272
273 if (!s->h263_aic) {
274 if (n < 4)
275 block[0] = block[0] * s->y_dc_scale;
276 else
277 block[0] = block[0] * s->c_dc_scale;
278 qadd = (qscale - 1) | 1;
279 }else{
280 qadd = 0;
281 }
282 if(s->ac_pred)
283 nCoeffs=63;
284 else
285 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
286
287 for(i=1; i<=nCoeffs; i++) {
288 level = block[i];
289 if (level) {
290 if (level < 0) {
291 level = level * qmul - qadd;
292 } else {
293 level = level * qmul + qadd;
294 }
295 block[i] = level;
296 }
297 }
298}
299
300static void dct_unquantize_h263_inter_c(MpegEncContext *s,
301 int16_t *block, int n, int qscale)
302{
303 int i, level, qmul, qadd;
304 int nCoeffs;
305
306 assert(s->block_last_index[n]>=0);
307
308 qadd = (qscale - 1) | 1;
309 qmul = qscale << 1;
310
311 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
312
313 for(i=0; i<=nCoeffs; i++) {
314 level = block[i];
315 if (level) {
316 if (level < 0) {
317 level = level * qmul - qadd;
318 } else {
319 level = level * qmul + qadd;
320 }
321 block[i] = level;
322 }
323 }
324}
325
54974c62
AK
326static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
327 int (*mv)[2][4][2],
328 int mb_x, int mb_y, int mb_intra, int mb_skipped)
329{
330 MpegEncContext *s = opaque;
331
332 s->mv_dir = mv_dir;
333 s->mv_type = mv_type;
334 s->mb_intra = mb_intra;
335 s->mb_skipped = mb_skipped;
336 s->mb_x = mb_x;
337 s->mb_y = mb_y;
338 memcpy(s->mv, mv, sizeof(*mv));
339
340 ff_init_block_index(s);
341 ff_update_block_index(s);
342
343 s->dsp.clear_blocks(s->block[0]);
344
f6774f90 345 s->dest[0] = s->current_picture.f->data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
346 s->dest[1] = s->current_picture.f->data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
347 s->dest[2] = s->current_picture.f->data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
54974c62
AK
348
349 assert(ref == 0);
350 ff_MPV_decode_mb(s, s->block);
351}
352
defdfc9a 353/* init common dct for both encoder and decoder */
5ef251e5 354av_cold int ff_dct_common_init(MpegEncContext *s)
de6d9b64 355{
9cf0841e 356 ff_dsputil_init(&s->dsp, s->avctx);
f4fed5a2 357 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
1f4ea4e0 358 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
c3027b4d 359
d50635cd
MN
360 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
361 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
362 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
363 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
364 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
363114e8 365 if (s->flags & CODEC_FLAG_BITEXACT)
e27b6e62 366 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
d50635cd 367 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
b0368839 368
70dd8892
DB
369 if (ARCH_ARM)
370 ff_MPV_common_init_arm(s);
70dd8892
DB
371 if (ARCH_PPC)
372 ff_MPV_common_init_ppc(s);
373 if (ARCH_X86)
374 ff_MPV_common_init_x86(s);
676e200c 375
2ad1516a 376 /* load & permutate scantables
363114e8
KT
377 * note: only wmv uses different ones
378 */
379 if (s->alternate_scan) {
bb198e19
MN
380 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
381 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
363114e8 382 } else {
bb198e19
MN
383 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
384 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
385 }
3d2e8cce
MN
386 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
387 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
d930ef19 388
defdfc9a
AB
389 return 0;
390}
391
c99307ca 392static int frame_size_alloc(MpegEncContext *s, int linesize)
f1d8763a
JG
393{
394 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
395
396 // edge emu needs blocksize + filter length - 1
397 // (= 17x17 for halfpel / 21x21 for h264)
45635885
JG
398 // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
399 // at uvlinesize. It supports only YUV420 so 24x24 is enough
f1d8763a 400 // linesize * interlaced * MBsize
45635885 401 FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
f1d8763a
JG
402 fail);
403
259af1b9 404 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3,
f1d8763a
JG
405 fail)
406 s->me.temp = s->me.scratchpad;
407 s->rd_scratchpad = s->me.scratchpad;
408 s->b_scratchpad = s->me.scratchpad;
409 s->obmc_scratchpad = s->me.scratchpad + 16;
410
411 return 0;
412fail:
413 av_freep(&s->edge_emu_buffer);
414 return AVERROR(ENOMEM);
415}
416
34e46c44 417/**
49bd8e4b 418 * Allocate a frame buffer
34e46c44
GB
419 */
420static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
421{
024db249 422 int edges_needed = av_codec_is_encoder(s->avctx->codec);
f1d8763a 423 int r, ret;
34e46c44 424
f6774f90 425 pic->tf.f = pic->f;
ee769c6a
AD
426 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
427 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
024db249
AK
428 s->codec_id != AV_CODEC_ID_MSS2) {
429 if (edges_needed) {
f6774f90 430 pic->f->width = s->avctx->width + 2 * EDGE_WIDTH;
431 pic->f->height = s->avctx->height + 2 * EDGE_WIDTH;
024db249
AK
432 }
433
759001c5
AK
434 r = ff_thread_get_buffer(s->avctx, &pic->tf,
435 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
024db249 436 } else {
f6774f90 437 pic->f->width = s->avctx->width;
438 pic->f->height = s->avctx->height;
439 pic->f->format = s->avctx->pix_fmt;
440 r = avcodec_default_get_buffer2(s->avctx, pic->f, 0);
759001c5
AK
441 }
442
f6774f90 443 if (r < 0 || !pic->f->buf[0]) {
759001c5 444 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
f6774f90 445 r, pic->f->data[0]);
34e46c44
GB
446 return -1;
447 }
448
024db249
AK
449 if (edges_needed) {
450 int i;
f6774f90 451 for (i = 0; pic->f->data[i]; i++) {
024db249 452 int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
f6774f90 453 pic->f->linesize[i] +
024db249 454 (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
f6774f90 455 pic->f->data[i] += offset;
024db249 456 }
f6774f90 457 pic->f->width = s->avctx->width;
458 pic->f->height = s->avctx->height;
024db249
AK
459 }
460
c3ebfcd6
HL
461 if (s->avctx->hwaccel) {
462 assert(!pic->hwaccel_picture_private);
a871ef0c
AK
463 if (s->avctx->hwaccel->frame_priv_data_size) {
464 pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->frame_priv_data_size);
c3ebfcd6
HL
465 if (!pic->hwaccel_priv_buf) {
466 av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
467 return -1;
468 }
469 pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
470 }
471 }
472
f6774f90 473 if (s->linesize && (s->linesize != pic->f->linesize[0] ||
474 s->uvlinesize != pic->f->linesize[1])) {
363114e8
KT
475 av_log(s->avctx, AV_LOG_ERROR,
476 "get_buffer() failed (stride changed)\n");
759001c5 477 ff_mpeg_unref_picture(s, pic);
34e46c44
GB
478 return -1;
479 }
480
f6774f90 481 if (pic->f->linesize[1] != pic->f->linesize[2]) {
363114e8
KT
482 av_log(s->avctx, AV_LOG_ERROR,
483 "get_buffer() failed (uv stride mismatch)\n");
759001c5 484 ff_mpeg_unref_picture(s, pic);
34e46c44
GB
485 return -1;
486 }
487
f1d8763a 488 if (!s->edge_emu_buffer &&
f6774f90 489 (ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) {
f1d8763a
JG
490 av_log(s->avctx, AV_LOG_ERROR,
491 "get_buffer() failed to allocate context scratch buffers.\n");
759001c5 492 ff_mpeg_unref_picture(s, pic);
f1d8763a
JG
493 return ret;
494 }
495
34e46c44
GB
496 return 0;
497}
498
0b0a7a75 499void ff_free_picture_tables(Picture *pic)
363114e8 500{
759001c5 501 int i;
363114e8 502
759001c5
AK
503 av_buffer_unref(&pic->mb_var_buf);
504 av_buffer_unref(&pic->mc_mb_var_buf);
505 av_buffer_unref(&pic->mb_mean_buf);
506 av_buffer_unref(&pic->mbskip_table_buf);
507 av_buffer_unref(&pic->qscale_table_buf);
508 av_buffer_unref(&pic->mb_type_buf);
363114e8 509
759001c5
AK
510 for (i = 0; i < 2; i++) {
511 av_buffer_unref(&pic->motion_val_buf[i]);
512 av_buffer_unref(&pic->ref_index_buf[i]);
513 }
514}
515
516static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
517{
518 const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
363114e8
KT
519 const int mb_array_size = s->mb_stride * s->mb_height;
520 const int b8_array_size = s->b8_stride * s->mb_height * 2;
0da71265 521 int i;
759001c5
AK
522
523
524 pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
525 pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
526 pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
527 sizeof(uint32_t));
528 if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
529 return AVERROR(ENOMEM);
530
531 if (s->encoding) {
532 pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
533 pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
534 pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
535 if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
536 return AVERROR(ENOMEM);
537 }
538
ccc71298 539 if (s->out_format == FMT_H263 || s->encoding) {
759001c5
AK
540 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
541 int ref_index_size = 4 * mb_array_size;
542
543 for (i = 0; mv_size && i < 2; i++) {
544 pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
545 pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
546 if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
547 return AVERROR(ENOMEM);
548 }
549 }
550
551 return 0;
552}
553
554static int make_tables_writable(Picture *pic)
555{
556 int ret, i;
557#define MAKE_WRITABLE(table) \
558do {\
559 if (pic->table &&\
560 (ret = av_buffer_make_writable(&pic->table)) < 0)\
561 return ret;\
562} while (0)
563
564 MAKE_WRITABLE(mb_var_buf);
565 MAKE_WRITABLE(mc_mb_var_buf);
566 MAKE_WRITABLE(mb_mean_buf);
567 MAKE_WRITABLE(mbskip_table_buf);
568 MAKE_WRITABLE(qscale_table_buf);
569 MAKE_WRITABLE(mb_type_buf);
570
571 for (i = 0; i < 2; i++) {
572 MAKE_WRITABLE(motion_val_buf[i]);
573 MAKE_WRITABLE(ref_index_buf[i]);
574 }
575
576 return 0;
577}
578
579/**
580 * Allocate a Picture.
581 * The pixels are allocated/set by calling get_buffer() if shared = 0
582 */
583int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
584{
585 int i, ret;
115329f1 586
363114e8 587 if (shared) {
f6774f90 588 assert(pic->f->data[0]);
759001c5 589 pic->shared = 1;
363114e8 590 } else {
f6774f90 591 assert(!pic->f->buf[0]);
115329f1 592
34e46c44 593 if (alloc_frame_buffer(s, pic) < 0)
4e00e76b 594 return -1;
4e00e76b 595
f6774f90 596 s->linesize = pic->f->linesize[0];
597 s->uvlinesize = pic->f->linesize[1];
1e491e29 598 }
115329f1 599
759001c5
AK
600 if (!pic->qscale_table_buf)
601 ret = alloc_picture_tables(s, pic);
602 else
603 ret = make_tables_writable(pic);
604 if (ret < 0)
605 goto fail;
1e491e29 606
759001c5
AK
607 if (s->encoding) {
608 pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
609 pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
610 pic->mb_mean = pic->mb_mean_buf->data;
4e00e76b 611 }
0da71265 612
759001c5
AK
613 pic->mbskip_table = pic->mbskip_table_buf->data;
614 pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
615 pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
616
617 if (pic->motion_val_buf[0]) {
618 for (i = 0; i < 2; i++) {
619 pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
620 pic->ref_index[i] = pic->ref_index_buf[i]->data;
621 }
622 }
115329f1 623
1e491e29 624 return 0;
759001c5
AK
625fail:
626 av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
627 ff_mpeg_unref_picture(s, pic);
0b0a7a75 628 ff_free_picture_tables(pic);
759001c5 629 return AVERROR(ENOMEM);
1e491e29
MN
630}
631
4e00e76b 632/**
58c42af7 633 * Deallocate a picture.
4e00e76b 634 */
759001c5 635void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
363114e8 636{
759001c5
AK
637 int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
638
f6774f90 639 pic->tf.f = pic->f;
759001c5
AK
640 /* WM Image / Screen codecs allocate internal buffers with different
641 * dimensions / colorspaces; ignore user-defined callbacks for these. */
642 if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
643 s->codec_id != AV_CODEC_ID_VC1IMAGE &&
644 s->codec_id != AV_CODEC_ID_MSS2)
645 ff_thread_release_buffer(s->avctx, &pic->tf);
f6774f90 646 else if (pic->f)
647 av_frame_unref(pic->f);
759001c5
AK
648
649 av_buffer_unref(&pic->hwaccel_priv_buf);
4e00e76b 650
3eae9b03 651 if (pic->needs_realloc)
0b0a7a75 652 ff_free_picture_tables(pic);
3eae9b03 653
759001c5
AK
654 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
655}
656
657static int update_picture_tables(Picture *dst, Picture *src)
658{
659 int i;
660
661#define UPDATE_TABLE(table)\
662do {\
663 if (src->table &&\
664 (!dst->table || dst->table->buffer != src->table->buffer)) {\
665 av_buffer_unref(&dst->table);\
666 dst->table = av_buffer_ref(src->table);\
667 if (!dst->table) {\
0b0a7a75 668 ff_free_picture_tables(dst);\
759001c5
AK
669 return AVERROR(ENOMEM);\
670 }\
671 }\
672} while (0)
673
674 UPDATE_TABLE(mb_var_buf);
675 UPDATE_TABLE(mc_mb_var_buf);
676 UPDATE_TABLE(mb_mean_buf);
677 UPDATE_TABLE(mbskip_table_buf);
678 UPDATE_TABLE(qscale_table_buf);
679 UPDATE_TABLE(mb_type_buf);
363114e8 680 for (i = 0; i < 2; i++) {
759001c5
AK
681 UPDATE_TABLE(motion_val_buf[i]);
682 UPDATE_TABLE(ref_index_buf[i]);
0da71265 683 }
115329f1 684
759001c5
AK
685 dst->mb_var = src->mb_var;
686 dst->mc_mb_var = src->mc_mb_var;
687 dst->mb_mean = src->mb_mean;
688 dst->mbskip_table = src->mbskip_table;
689 dst->qscale_table = src->qscale_table;
690 dst->mb_type = src->mb_type;
691 for (i = 0; i < 2; i++) {
692 dst->motion_val[i] = src->motion_val[i];
693 dst->ref_index[i] = src->ref_index[i];
1e491e29 694 }
759001c5
AK
695
696 return 0;
697}
698
699int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
700{
701 int ret;
702
f6774f90 703 av_assert0(!dst->f->buf[0]);
704 av_assert0(src->f->buf[0]);
759001c5 705
f6774f90 706 src->tf.f = src->f;
707 dst->tf.f = dst->f;
759001c5
AK
708 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
709 if (ret < 0)
710 goto fail;
711
712 ret = update_picture_tables(dst, src);
713 if (ret < 0)
714 goto fail;
715
716 if (src->hwaccel_picture_private) {
717 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
718 if (!dst->hwaccel_priv_buf)
719 goto fail;
720 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
721 }
722
723 dst->field_picture = src->field_picture;
724 dst->mb_var_sum = src->mb_var_sum;
725 dst->mc_mb_var_sum = src->mc_mb_var_sum;
726 dst->b_frame_score = src->b_frame_score;
727 dst->needs_realloc = src->needs_realloc;
728 dst->reference = src->reference;
729 dst->shared = src->shared;
730
731 return 0;
732fail:
733 ff_mpeg_unref_picture(s, dst);
734 return ret;
1e491e29
MN
735}
736
16e7b189
KS
737static void exchange_uv(MpegEncContext *s)
738{
739 int16_t (*tmp)[64];
740
741 tmp = s->pblocks[4];
742 s->pblocks[4] = s->pblocks[5];
743 s->pblocks[5] = tmp;
744}
745
12b54a1f 746static int init_duplicate_context(MpegEncContext *s)
363114e8 747{
2cbd734a
MR
748 int y_size = s->b8_stride * (2 * s->mb_height + 1);
749 int c_size = s->mb_stride * (s->mb_height + 1);
750 int yc_size = y_size + 2 * c_size;
9c3d33d6
MN
751 int i;
752
f1d8763a
JG
753 s->edge_emu_buffer =
754 s->me.scratchpad =
755 s->me.temp =
756 s->rd_scratchpad =
757 s->b_scratchpad =
758 s->obmc_scratchpad = NULL;
363114e8 759
9c3d33d6 760 if (s->encoding) {
363114e8
KT
761 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
762 ME_MAP_SIZE * sizeof(uint32_t), fail)
763 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
764 ME_MAP_SIZE * sizeof(uint32_t), fail)
765 if (s->avctx->noise_reduction) {
766 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
767 2 * 64 * sizeof(int), fail)
9c3d33d6 768 }
115329f1 769 }
88bd7fdc 770 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
363114e8 771 s->block = s->blocks[0];
9c3d33d6 772
363114e8 773 for (i = 0; i < 12; i++) {
21effaa4 774 s->pblocks[i] = &s->block[i];
9c3d33d6 775 }
16e7b189
KS
776 if (s->avctx->codec_tag == AV_RL32("VCR2"))
777 exchange_uv(s);
2cbd734a 778
79042a6e
MR
779 if (s->out_format == FMT_H263) {
780 /* ac values */
363114e8
KT
781 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
782 yc_size * sizeof(int16_t) * 16, fail);
2cbd734a
MR
783 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
784 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
785 s->ac_val[2] = s->ac_val[1] + c_size;
786 }
787
9c3d33d6
MN
788 return 0;
789fail:
efd29844 790 return -1; // free() through ff_MPV_common_end()
9c3d33d6
MN
791}
792
363114e8
KT
793static void free_duplicate_context(MpegEncContext *s)
794{
795 if (s == NULL)
796 return;
9c3d33d6 797
330deb75 798 av_freep(&s->edge_emu_buffer);
9c3d33d6 799 av_freep(&s->me.scratchpad);
363114e8
KT
800 s->me.temp =
801 s->rd_scratchpad =
802 s->b_scratchpad =
803 s->obmc_scratchpad = NULL;
115329f1 804
9c3d33d6
MN
805 av_freep(&s->dct_error_sum);
806 av_freep(&s->me.map);
807 av_freep(&s->me.score_map);
808 av_freep(&s->blocks);
2cbd734a 809 av_freep(&s->ac_val_base);
363114e8 810 s->block = NULL;
9c3d33d6
MN
811}
812
363114e8
KT
813static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
814{
815#define COPY(a) bak->a = src->a
9c3d33d6
MN
816 COPY(edge_emu_buffer);
817 COPY(me.scratchpad);
a6f2c0d6 818 COPY(me.temp);
9c3d33d6
MN
819 COPY(rd_scratchpad);
820 COPY(b_scratchpad);
821 COPY(obmc_scratchpad);
822 COPY(me.map);
823 COPY(me.score_map);
824 COPY(blocks);
825 COPY(block);
826 COPY(start_mb_y);
827 COPY(end_mb_y);
828 COPY(me.map_generation);
829 COPY(pb);
830 COPY(dct_error_sum);
da16b204
MN
831 COPY(dct_count[0]);
832 COPY(dct_count[1]);
2cbd734a
MR
833 COPY(ac_val_base);
834 COPY(ac_val[0]);
835 COPY(ac_val[1]);
836 COPY(ac_val[2]);
9c3d33d6
MN
837#undef COPY
838}
839
f1d8763a 840int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
363114e8 841{
9c3d33d6 842 MpegEncContext bak;
f1d8763a 843 int i, ret;
363114e8
KT
844 // FIXME copy only needed parts
845 // START_TIMER
9c3d33d6
MN
846 backup_duplicate_context(&bak, dst);
847 memcpy(dst, src, sizeof(MpegEncContext));
848 backup_duplicate_context(dst, &bak);
363114e8 849 for (i = 0; i < 12; i++) {
21effaa4 850 dst->pblocks[i] = &dst->block[i];
c62c07d3 851 }
16e7b189
KS
852 if (dst->avctx->codec_tag == AV_RL32("VCR2"))
853 exchange_uv(dst);
f1d8763a 854 if (!dst->edge_emu_buffer &&
c99307ca 855 (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
f1d8763a
JG
856 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
857 "scratch buffers.\n");
858 return ret;
859 }
363114e8
KT
860 // STOP_TIMER("update_duplicate_context")
861 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
f1d8763a 862 return 0;
9c3d33d6
MN
863}
864
363114e8
KT
865int ff_mpeg_update_thread_context(AVCodecContext *dst,
866 const AVCodecContext *src)
6a9c8594 867{
759001c5 868 int i, ret;
6a9c8594
AS
869 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
870
363114e8
KT
871 if (dst == src || !s1->context_initialized)
872 return 0;
6a9c8594 873
363114e8
KT
874 // FIXME can parameters change on I-frames?
875 // in that case dst may need a reinit
876 if (!s->context_initialized) {
6a9c8594
AS
877 memcpy(s, s1, sizeof(MpegEncContext));
878
879 s->avctx = dst;
6a9c8594
AS
880 s->bitstream_buffer = NULL;
881 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
882
efd29844 883 ff_MPV_common_init(s);
6a9c8594
AS
884 }
885
8701f4f8
JG
886 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
887 int err;
888 s->context_reinit = 0;
889 s->height = s1->height;
890 s->width = s1->width;
891 if ((err = ff_MPV_common_frame_size_change(s)) < 0)
892 return err;
893 }
894
6a9c8594
AS
895 s->avctx->coded_height = s1->avctx->coded_height;
896 s->avctx->coded_width = s1->avctx->coded_width;
897 s->avctx->width = s1->avctx->width;
898 s->avctx->height = s1->avctx->height;
899
900 s->coded_picture_number = s1->coded_picture_number;
901 s->picture_number = s1->picture_number;
6a9c8594 902
759001c5
AK
903 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
904 ff_mpeg_unref_picture(s, &s->picture[i]);
f6774f90 905 if (s1->picture[i].f->buf[0] &&
759001c5
AK
906 (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
907 return ret;
908 }
909
910#define UPDATE_PICTURE(pic)\
911do {\
912 ff_mpeg_unref_picture(s, &s->pic);\
f6774f90 913 if (s1->pic.f->buf[0])\
759001c5
AK
914 ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
915 else\
916 ret = update_picture_tables(&s->pic, &s1->pic);\
917 if (ret < 0)\
918 return ret;\
919} while (0)
920
921 UPDATE_PICTURE(current_picture);
922 UPDATE_PICTURE(last_picture);
923 UPDATE_PICTURE(next_picture);
1481e198 924
4d9ec050
KT
925 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
926 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
927 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
6a9c8594 928
4d9ec050 929 // Error/bug resilience
6a9c8594
AS
930 s->next_p_frame_damaged = s1->next_p_frame_damaged;
931 s->workaround_bugs = s1->workaround_bugs;
932
4d9ec050 933 // MPEG4 timing info
e62a43f6 934 memcpy(&s->last_time_base, &s1->last_time_base,
ee8af2dd 935 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
e62a43f6 936 (char *) &s1->last_time_base);
4d9ec050
KT
937
938 // B-frame info
939 s->max_b_frames = s1->max_b_frames;
940 s->low_delay = s1->low_delay;
ba0c8981 941 s->droppable = s1->droppable;
4d9ec050
KT
942
943 // DivX handling (doesn't work)
944 s->divx_packed = s1->divx_packed;
945
946 if (s1->bitstream_buffer) {
947 if (s1->bitstream_buffer_size +
948 FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
949 av_fast_malloc(&s->bitstream_buffer,
950 &s->allocated_bitstream_buffer_size,
951 s1->allocated_bitstream_buffer_size);
952 s->bitstream_buffer_size = s1->bitstream_buffer_size;
953 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
954 s1->bitstream_buffer_size);
955 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
956 FF_INPUT_BUFFER_PADDING_SIZE);
6a9c8594
AS
957 }
958
f1d8763a
JG
959 // linesize dependend scratch buffer allocation
960 if (!s->edge_emu_buffer)
961 if (s1->linesize) {
c99307ca 962 if (frame_size_alloc(s, s1->linesize) < 0) {
f1d8763a
JG
963 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
964 "scratch buffers.\n");
965 return AVERROR(ENOMEM);
966 }
967 } else {
968 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
969 "be allocated due to unknown size.\n");
970 return AVERROR_BUG;
971 }
972
4d9ec050
KT
973 // MPEG2/interlacing info
974 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
975 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
6a9c8594 976
4d9ec050
KT
977 if (!s1->first_field) {
978 s->last_pict_type = s1->pict_type;
979 if (s1->current_picture_ptr)
f6774f90 980 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
6a9c8594
AS
981 }
982
983 return 0;
984}
985
3edcacde 986/**
58c42af7 987 * Set the given MpegEncContext to common defaults
4d9ec050 988 * (same for encoding and decoding).
58c42af7 989 * The changed fields will not depend upon the
4d9ec050 990 * prior state of the MpegEncContext.
3edcacde 991 */
efd29844 992void ff_MPV_common_defaults(MpegEncContext *s)
4d9ec050
KT
993{
994 s->y_dc_scale_table =
995 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
996 s->chroma_qscale_table = ff_default_chroma_qscale_table;
997 s->progressive_frame = 1;
998 s->progressive_sequence = 1;
999 s->picture_structure = PICT_FRAME;
1000
1001 s->coded_picture_number = 0;
1002 s->picture_number = 0;
7976241a 1003
4d9ec050
KT
1004 s->f_code = 1;
1005 s->b_code = 1;
6a9c8594 1006
881a5e04 1007 s->slice_context_count = 1;
3edcacde
MN
1008}
1009
1010/**
58c42af7 1011 * Set the given MpegEncContext to defaults for decoding.
4d9ec050
KT
1012 * the changed fields will not depend upon
1013 * the prior state of the MpegEncContext.
3edcacde 1014 */
efd29844 1015void ff_MPV_decode_defaults(MpegEncContext *s)
4d9ec050 1016{
efd29844 1017 ff_MPV_common_defaults(s);
3edcacde
MN
1018}
1019
54974c62
AK
1020static int init_er(MpegEncContext *s)
1021{
1022 ERContext *er = &s->er;
1023 int mb_array_size = s->mb_height * s->mb_stride;
1024 int i;
1025
1026 er->avctx = s->avctx;
1027 er->dsp = &s->dsp;
1028
1029 er->mb_index2xy = s->mb_index2xy;
1030 er->mb_num = s->mb_num;
1031 er->mb_width = s->mb_width;
1032 er->mb_height = s->mb_height;
1033 er->mb_stride = s->mb_stride;
1034 er->b8_stride = s->b8_stride;
1035
1036 er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
1037 er->error_status_table = av_mallocz(mb_array_size);
1038 if (!er->er_temp_buffer || !er->error_status_table)
1039 goto fail;
1040
1041 er->mbskip_table = s->mbskip_table;
1042 er->mbintra_table = s->mbintra_table;
1043
1044 for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
1045 er->dc_val[i] = s->dc_val[i];
1046
1047 er->decode_mb = mpeg_er_decode_mb;
1048 er->opaque = s;
1049
1050 return 0;
1051fail:
1052 av_freep(&er->er_temp_buffer);
1053 av_freep(&er->error_status_table);
1054 return AVERROR(ENOMEM);
1055}
1056
3edcacde 1057/**
1b3439b3
JG
1058 * Initialize and allocates MpegEncContext fields dependent on the resolution.
1059 */
1060static int init_context_frame(MpegEncContext *s)
1061{
1062 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
1063
1064 s->mb_width = (s->width + 15) / 16;
1065 s->mb_stride = s->mb_width + 1;
1066 s->b8_stride = s->mb_width * 2 + 1;
1b3439b3
JG
1067 mb_array_size = s->mb_height * s->mb_stride;
1068 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
1069
1070 /* set default edge pos, will be overriden
1071 * in decode_header if needed */
1072 s->h_edge_pos = s->mb_width * 16;
1073 s->v_edge_pos = s->mb_height * 16;
1074
1075 s->mb_num = s->mb_width * s->mb_height;
1076
1077 s->block_wrap[0] =
1078 s->block_wrap[1] =
1079 s->block_wrap[2] =
1080 s->block_wrap[3] = s->b8_stride;
1081 s->block_wrap[4] =
1082 s->block_wrap[5] = s->mb_stride;
1083
1084 y_size = s->b8_stride * (2 * s->mb_height + 1);
1085 c_size = s->mb_stride * (s->mb_height + 1);
1086 yc_size = y_size + 2 * c_size;
1087
1088 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
1089 fail); // error ressilience code looks cleaner with this
1090 for (y = 0; y < s->mb_height; y++)
1091 for (x = 0; x < s->mb_width; x++)
1092 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
1093
1094 s->mb_index2xy[s->mb_height * s->mb_width] =
1095 (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
1096
1097 if (s->encoding) {
1098 /* Allocate MV tables */
1099 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
1100 mv_table_size * 2 * sizeof(int16_t), fail);
1101 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
1102 mv_table_size * 2 * sizeof(int16_t), fail);
1103 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
1104 mv_table_size * 2 * sizeof(int16_t), fail);
1105 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
1106 mv_table_size * 2 * sizeof(int16_t), fail);
1107 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
1108 mv_table_size * 2 * sizeof(int16_t), fail);
1109 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
1110 mv_table_size * 2 * sizeof(int16_t), fail);
1111 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
1112 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
1113 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
1114 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
1115 s->mb_stride + 1;
1116 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
1117 s->mb_stride + 1;
1118 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
1119
1120 /* Allocate MB type table */
1121 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
1122 sizeof(uint16_t), fail); // needed for encoding
1123
1124 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
1125 sizeof(int), fail);
1126
1127 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
1128 mb_array_size * sizeof(float), fail);
1129 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
1130 mb_array_size * sizeof(float), fail);
1131
1132 }
1133
1b3439b3
JG
1134 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
1135 (s->flags & CODEC_FLAG_INTERLACED_ME)) {
1136 /* interlaced direct mode decoding tables */
1137 for (i = 0; i < 2; i++) {
1138 int j, k;
1139 for (j = 0; j < 2; j++) {
1140 for (k = 0; k < 2; k++) {
1141 FF_ALLOCZ_OR_GOTO(s->avctx,
1142 s->b_field_mv_table_base[i][j][k],
1143 mv_table_size * 2 * sizeof(int16_t),
1144 fail);
1145 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
1146 s->mb_stride + 1;
1147 }
1148 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
1149 mb_array_size * 2 * sizeof(uint8_t), fail);
1150 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
1151 mv_table_size * 2 * sizeof(int16_t), fail);
1152 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
1153 + s->mb_stride + 1;
1154 }
1155 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
1156 mb_array_size * 2 * sizeof(uint8_t), fail);
1157 }
1158 }
1159 if (s->out_format == FMT_H263) {
1160 /* cbp values */
1161 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
1162 s->coded_block = s->coded_block_base + s->b8_stride + 1;
1163
1164 /* cbp, ac_pred, pred_dir */
1165 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
1166 mb_array_size * sizeof(uint8_t), fail);
1167 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
1168 mb_array_size * sizeof(uint8_t), fail);
1169 }
1170
1171 if (s->h263_pred || s->h263_plus || !s->encoding) {
1172 /* dc values */
1173 // MN: we need these for error resilience of intra-frames
1174 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
1175 yc_size * sizeof(int16_t), fail);
1176 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
1177 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
1178 s->dc_val[2] = s->dc_val[1] + c_size;
1179 for (i = 0; i < yc_size; i++)
1180 s->dc_val_base[i] = 1024;
1181 }
1182
1183 /* which mb is a intra block */
1184 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
1185 memset(s->mbintra_table, 1, mb_array_size);
1186
1187 /* init macroblock skip table */
1188 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
1189 // Note the + 1 is for a quicker mpeg4 slice_end detection
1190
54974c62 1191 return init_er(s);
1b3439b3
JG
1192fail:
1193 return AVERROR(ENOMEM);
1194}
1195
1196/**
3edcacde
MN
1197 * init common structure for both encoder and decoder.
1198 * this assumes that some variables like width/height are already set
1199 */
efd29844 1200av_cold int ff_MPV_common_init(MpegEncContext *s)
defdfc9a 1201{
7e76fc52 1202 int i;
881a5e04
JG
1203 int nb_slices = (HAVE_THREADS &&
1204 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
1205 s->avctx->thread_count : 1;
1206
1207 if (s->encoding && s->avctx->slices)
1208 nb_slices = s->avctx->slices;
defdfc9a 1209
36ef5369 1210 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
0127b861 1211 s->mb_height = (s->height + 31) / 32 * 2;
19cac8e3 1212 else
1b661802 1213 s->mb_height = (s->height + 15) / 16;
fdb52bcc 1214
716d413c 1215 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
4d9ec050 1216 av_log(s->avctx, AV_LOG_ERROR,
716d413c 1217 "decoding to AV_PIX_FMT_NONE is not supported.\n");
9cfc1b3a
IK
1218 return -1;
1219 }
1220
881a5e04
JG
1221 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
1222 int max_slices;
1223 if (s->mb_height)
1224 max_slices = FFMIN(MAX_THREADS, s->mb_height);
1225 else
1226 max_slices = MAX_THREADS;
1227 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
1228 " reducing to %d\n", nb_slices, max_slices);
1229 nb_slices = max_slices;
000a9c02
MN
1230 }
1231
4d9ec050
KT
1232 if ((s->width || s->height) &&
1233 av_image_check_size(s->width, s->height, 0, s->avctx))
0ecca7a4
MN
1234 return -1;
1235
6180ade7 1236 ff_dct_common_init(s);
eb4b3dd3 1237
4d9ec050
KT
1238 s->flags = s->avctx->flags;
1239 s->flags2 = s->avctx->flags2;
defdfc9a 1240
5f24fe82
MS
1241 /* set chroma shifts */
1242 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1243 &s->chroma_x_shift,
1244 &s->chroma_y_shift);
eb4b3dd3 1245
5f24fe82
MS
1246 /* convert fourcc to upper case */
1247 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
115329f1 1248
5f24fe82 1249 s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
603a5f04 1250
4d9ec050 1251 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
759001c5
AK
1252 MAX_PICTURE_COUNT * sizeof(Picture), fail);
1253 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
f6774f90 1254 s->picture[i].f = av_frame_alloc();
1255 if (!s->picture[i].f)
1256 goto fail;
747069e2 1257 }
759001c5
AK
1258 memset(&s->next_picture, 0, sizeof(s->next_picture));
1259 memset(&s->last_picture, 0, sizeof(s->last_picture));
1260 memset(&s->current_picture, 0, sizeof(s->current_picture));
f6774f90 1261 memset(&s->new_picture, 0, sizeof(s->new_picture));
1262 s->next_picture.f = av_frame_alloc();
1263 if (!s->next_picture.f)
1264 goto fail;
1265 s->last_picture.f = av_frame_alloc();
1266 if (!s->last_picture.f)
1267 goto fail;
1268 s->current_picture.f = av_frame_alloc();
1269 if (!s->current_picture.f)
1270 goto fail;
1271 s->new_picture.f = av_frame_alloc();
1272 if (!s->new_picture.f)
1273 goto fail;
b465449e 1274
fb22c237 1275 if (s->width && s->height) {
7e76fc52 1276 if (init_context_frame(s))
1b3439b3 1277 goto fail;
4d9ec050
KT
1278
1279 s->parse_context.state = -1;
fb22c237 1280 }
d7425f59 1281
de6d9b64 1282 s->context_initialized = 1;
4d9ec050 1283 s->thread_context[0] = s;
9c3d33d6 1284
fb22c237 1285 if (s->width && s->height) {
881a5e04
JG
1286 if (nb_slices > 1) {
1287 for (i = 1; i < nb_slices; i++) {
4d9ec050
KT
1288 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1289 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1290 }
9c3d33d6 1291
881a5e04 1292 for (i = 0; i < nb_slices; i++) {
12b54a1f 1293 if (init_duplicate_context(s->thread_context[i]) < 0)
4d9ec050
KT
1294 goto fail;
1295 s->thread_context[i]->start_mb_y =
881a5e04 1296 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
4d9ec050 1297 s->thread_context[i]->end_mb_y =
881a5e04 1298 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
4d9ec050
KT
1299 }
1300 } else {
12b54a1f 1301 if (init_duplicate_context(s) < 0)
d969e93a 1302 goto fail;
4d9ec050
KT
1303 s->start_mb_y = 0;
1304 s->end_mb_y = s->mb_height;
d969e93a 1305 }
881a5e04 1306 s->slice_context_count = nb_slices;
fb22c237
RB
1307 }
1308
de6d9b64
FB
1309 return 0;
1310 fail:
efd29844 1311 ff_MPV_common_end(s);
de6d9b64
FB
1312 return -1;
1313}
1314
1b3439b3
JG
1315/**
1316 * Frees and resets MpegEncContext fields depending on the resolution.
1317 * Is used during resolution changes to avoid a full reinitialization of the
1318 * codec.
1319 */
1320static int free_context_frame(MpegEncContext *s)
de6d9b64 1321{
bb198e19 1322 int i, j, k;
de6d9b64 1323
6000abfa 1324 av_freep(&s->mb_type);
7bc9090a
MN
1325 av_freep(&s->p_mv_table_base);
1326 av_freep(&s->b_forw_mv_table_base);
1327 av_freep(&s->b_back_mv_table_base);
1328 av_freep(&s->b_bidir_forw_mv_table_base);
1329 av_freep(&s->b_bidir_back_mv_table_base);
1330 av_freep(&s->b_direct_mv_table_base);
4d9ec050
KT
1331 s->p_mv_table = NULL;
1332 s->b_forw_mv_table = NULL;
1333 s->b_back_mv_table = NULL;
1334 s->b_bidir_forw_mv_table = NULL;
1335 s->b_bidir_back_mv_table = NULL;
1336 s->b_direct_mv_table = NULL;
1337 for (i = 0; i < 2; i++) {
1338 for (j = 0; j < 2; j++) {
1339 for (k = 0; k < 2; k++) {
bb198e19 1340 av_freep(&s->b_field_mv_table_base[i][j][k]);
4d9ec050 1341 s->b_field_mv_table[i][j][k] = NULL;
bb198e19
MN
1342 }
1343 av_freep(&s->b_field_select_table[i][j]);
1344 av_freep(&s->p_field_mv_table_base[i][j]);
4d9ec050 1345 s->p_field_mv_table[i][j] = NULL;
bb198e19
MN
1346 }
1347 av_freep(&s->p_field_select_table[i]);
1348 }
115329f1 1349
137c8468 1350 av_freep(&s->dc_val_base);
137c8468 1351 av_freep(&s->coded_block_base);
6000abfa 1352 av_freep(&s->mbintra_table);
7f2fe444
MN
1353 av_freep(&s->cbp_table);
1354 av_freep(&s->pred_dir_table);
115329f1 1355
6000abfa 1356 av_freep(&s->mbskip_table);
0ecca7a4 1357
54974c62
AK
1358 av_freep(&s->er.error_status_table);
1359 av_freep(&s->er.er_temp_buffer);
7bc9090a 1360 av_freep(&s->mb_index2xy);
158c7f05 1361 av_freep(&s->lambda_table);
1b3439b3
JG
1362 av_freep(&s->cplx_tab);
1363 av_freep(&s->bits_tab);
1364
1365 s->linesize = s->uvlinesize = 0;
1366
1b3439b3
JG
1367 return 0;
1368}
1369
435c0b87
JG
1370int ff_MPV_common_frame_size_change(MpegEncContext *s)
1371{
1372 int i, err = 0;
1373
1374 if (s->slice_context_count > 1) {
1375 for (i = 0; i < s->slice_context_count; i++) {
1376 free_duplicate_context(s->thread_context[i]);
1377 }
1378 for (i = 1; i < s->slice_context_count; i++) {
1379 av_freep(&s->thread_context[i]);
1380 }
1381 } else
1382 free_duplicate_context(s);
1383
759001c5
AK
1384 if ((err = free_context_frame(s)) < 0)
1385 return err;
435c0b87
JG
1386
1387 if (s->picture)
759001c5 1388 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
435c0b87
JG
1389 s->picture[i].needs_realloc = 1;
1390 }
1391
1392 s->last_picture_ptr =
1393 s->next_picture_ptr =
1394 s->current_picture_ptr = NULL;
1395
1396 // init
1397 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1398 s->mb_height = (s->height + 31) / 32 * 2;
19cac8e3 1399 else
435c0b87
JG
1400 s->mb_height = (s->height + 15) / 16;
1401
1402 if ((s->width || s->height) &&
1403 av_image_check_size(s->width, s->height, 0, s->avctx))
1404 return AVERROR_INVALIDDATA;
1405
1406 if ((err = init_context_frame(s)))
1407 goto fail;
1408
1409 s->thread_context[0] = s;
1410
1411 if (s->width && s->height) {
1412 int nb_slices = s->slice_context_count;
1413 if (nb_slices > 1) {
1414 for (i = 1; i < nb_slices; i++) {
1415 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1416 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1417 }
1418
1419 for (i = 0; i < nb_slices; i++) {
12b54a1f 1420 if (init_duplicate_context(s->thread_context[i]) < 0)
435c0b87
JG
1421 goto fail;
1422 s->thread_context[i]->start_mb_y =
1423 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1424 s->thread_context[i]->end_mb_y =
1425 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1426 }
1427 } else {
12b54a1f 1428 if (init_duplicate_context(s) < 0)
435c0b87
JG
1429 goto fail;
1430 s->start_mb_y = 0;
1431 s->end_mb_y = s->mb_height;
1432 }
1433 s->slice_context_count = nb_slices;
1434 }
1435
1436 return 0;
1437 fail:
1438 ff_MPV_common_end(s);
1439 return err;
1440}
1441
1b3439b3
JG
1442/* init common structure for both encoder and decoder */
1443void ff_MPV_common_end(MpegEncContext *s)
1444{
1445 int i;
1446
1447 if (s->slice_context_count > 1) {
1448 for (i = 0; i < s->slice_context_count; i++) {
1449 free_duplicate_context(s->thread_context[i]);
1450 }
1451 for (i = 1; i < s->slice_context_count; i++) {
1452 av_freep(&s->thread_context[i]);
1453 }
1454 s->slice_context_count = 1;
1455 } else free_duplicate_context(s);
1456
1457 av_freep(&s->parse_context.buffer);
1458 s->parse_context.buffer_size = 0;
1459
1460 av_freep(&s->bitstream_buffer);
1461 s->allocated_bitstream_buffer_size = 0;
1462
759001c5
AK
1463 if (s->picture) {
1464 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
0b0a7a75 1465 ff_free_picture_tables(&s->picture[i]);
759001c5 1466 ff_mpeg_unref_picture(s, &s->picture[i]);
f6774f90 1467 av_frame_free(&s->picture[i].f);
9b4b6e09 1468 }
de6d9b64 1469 }
b465449e 1470 av_freep(&s->picture);
0b0a7a75 1471 ff_free_picture_tables(&s->last_picture);
759001c5 1472 ff_mpeg_unref_picture(s, &s->last_picture);
f6774f90 1473 av_frame_free(&s->last_picture.f);
0b0a7a75 1474 ff_free_picture_tables(&s->current_picture);
759001c5 1475 ff_mpeg_unref_picture(s, &s->current_picture);
f6774f90 1476 av_frame_free(&s->current_picture.f);
0b0a7a75 1477 ff_free_picture_tables(&s->next_picture);
759001c5 1478 ff_mpeg_unref_picture(s, &s->next_picture);
f6774f90 1479 av_frame_free(&s->next_picture.f);
1480 ff_free_picture_tables(&s->new_picture);
1481 ff_mpeg_unref_picture(s, &s->new_picture);
1482 av_frame_free(&s->new_picture.f);
32c7589b
JG
1483
1484 free_context_frame(s);
1485
4d9ec050
KT
1486 s->context_initialized = 0;
1487 s->last_picture_ptr =
1488 s->next_picture_ptr =
1489 s->current_picture_ptr = NULL;
1490 s->linesize = s->uvlinesize = 0;
de6d9b64
FB
1491}
1492
6fee1b90
DB
1493av_cold void ff_init_rl(RLTable *rl,
1494 uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1d0d55da 1495{
4d9ec050
KT
1496 int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
1497 uint8_t index_run[MAX_RUN + 1];
1d0d55da
MN
1498 int last, run, level, start, end, i;
1499
c65dfac4 1500 /* If table is static, we can quit if rl->max_level[0] is not NULL */
4d9ec050 1501 if (static_store && rl->max_level[0])
073c2593
BP
1502 return;
1503
1d0d55da 1504 /* compute max_level[], max_run[] and index_run[] */
4d9ec050 1505 for (last = 0; last < 2; last++) {
1d0d55da
MN
1506 if (last == 0) {
1507 start = 0;
1508 end = rl->last;
1509 } else {
1510 start = rl->last;
1511 end = rl->n;
1512 }
1513
1514 memset(max_level, 0, MAX_RUN + 1);
1515 memset(max_run, 0, MAX_LEVEL + 1);
1516 memset(index_run, rl->n, MAX_RUN + 1);
4d9ec050
KT
1517 for (i = start; i < end; i++) {
1518 run = rl->table_run[i];
1d0d55da
MN
1519 level = rl->table_level[i];
1520 if (index_run[run] == rl->n)
1521 index_run[run] = i;
1522 if (level > max_level[run])
1523 max_level[run] = level;
1524 if (run > max_run[level])
1525 max_run[level] = run;
1526 }
4d9ec050 1527 if (static_store)
3502a54f 1528 rl->max_level[last] = static_store[last];
073c2593
BP
1529 else
1530 rl->max_level[last] = av_malloc(MAX_RUN + 1);
1d0d55da 1531 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
4d9ec050
KT
1532 if (static_store)
1533 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
073c2593 1534 else
4d9ec050 1535 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1d0d55da 1536 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
4d9ec050 1537 if (static_store)
3502a54f 1538 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
073c2593
BP
1539 else
1540 rl->index_run[last] = av_malloc(MAX_RUN + 1);
1d0d55da
MN
1541 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1542 }
1543}
1544
6fee1b90 1545av_cold void ff_init_vlc_rl(RLTable *rl)
898d5d5d
AJ
1546{
1547 int i, q;
1548
4d9ec050
KT
1549 for (q = 0; q < 32; q++) {
1550 int qmul = q * 2;
1551 int qadd = (q - 1) | 1;
898d5d5d 1552
4d9ec050
KT
1553 if (q == 0) {
1554 qmul = 1;
1555 qadd = 0;
898d5d5d 1556 }
4d9ec050
KT
1557 for (i = 0; i < rl->vlc.table_size; i++) {
1558 int code = rl->vlc.table[i][0];
1559 int len = rl->vlc.table[i][1];
898d5d5d
AJ
1560 int level, run;
1561
4d9ec050
KT
1562 if (len == 0) { // illegal code
1563 run = 66;
1564 level = MAX_LEVEL;
1565 } else if (len < 0) { // more bits needed
1566 run = 0;
1567 level = code;
1568 } else {
1569 if (code == rl->n) { // esc
1570 run = 66;
1571 level = 0;
1572 } else {
1573 run = rl->table_run[code] + 1;
1574 level = rl->table_level[code] * qmul + qadd;
1575 if (code >= rl->last) run += 192;
898d5d5d
AJ
1576 }
1577 }
4d9ec050
KT
1578 rl->rl_vlc[q][i].len = len;
1579 rl->rl_vlc[q][i].level = level;
1580 rl->rl_vlc[q][i].run = run;
898d5d5d
AJ
1581 }
1582 }
1583}
1584
282c6a1a 1585static void release_unused_pictures(MpegEncContext *s)
6a9c8594
AS
1586{
1587 int i;
1588
1589 /* release non reference frames */
759001c5 1590 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
282c6a1a 1591 if (!s->picture[i].reference)
759001c5 1592 ff_mpeg_unref_picture(s, &s->picture[i]);
6a9c8594
AS
1593 }
1594}
1595
435c0b87
JG
1596static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
1597{
f6774f90 1598 if (pic->f->buf[0] == NULL)
435c0b87 1599 return 1;
759001c5
AK
1600 if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
1601 return 1;
435c0b87
JG
1602 return 0;
1603}
1604
1605static int find_unused_picture(MpegEncContext *s, int shared)
4d9ec050 1606{
4e00e76b 1607 int i;
115329f1 1608
4d9ec050 1609 if (shared) {
759001c5 1610 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
f6774f90 1611 if (s->picture[i].f->buf[0] == NULL)
657ccb5a 1612 return i;
4e00e76b 1613 }
4d9ec050 1614 } else {
759001c5 1615 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
435c0b87 1616 if (pic_is_unused(s, &s->picture[i]))
657ccb5a 1617 return i;
4e00e76b
MN
1618 }
1619 }
1620
4f820131 1621 return AVERROR_INVALIDDATA;
4e00e76b
MN
1622}
1623
435c0b87
JG
1624int ff_find_unused_picture(MpegEncContext *s, int shared)
1625{
1626 int ret = find_unused_picture(s, shared);
1627
759001c5 1628 if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
435c0b87
JG
1629 if (s->picture[ret].needs_realloc) {
1630 s->picture[ret].needs_realloc = 0;
0b0a7a75 1631 ff_free_picture_tables(&s->picture[ret]);
759001c5 1632 ff_mpeg_unref_picture(s, &s->picture[ret]);
435c0b87
JG
1633 }
1634 }
1635 return ret;
1636}
1637
5f194811 1638/**
aec25b1c
AK
1639 * generic function called after decoding
1640 * the header and before a frame is decoded.
5f194811 1641 */
efd29844 1642int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
de6d9b64 1643{
759001c5 1644 int i, ret;
357ec71f 1645 Picture *pic;
160d679c 1646 s->mb_skipped = 0;
0da71265 1647
c65dfac4 1648 /* mark & release old frames */
ee870491
AK
1649 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1650 s->last_picture_ptr != s->next_picture_ptr &&
f6774f90 1651 s->last_picture_ptr->f->buf[0]) {
ee870491
AK
1652 ff_mpeg_unref_picture(s, s->last_picture_ptr);
1653 }
c65dfac4 1654
ee870491
AK
1655 /* release forgotten pictures */
1656 /* if (mpeg124/h263) */
aec25b1c
AK
1657 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1658 if (&s->picture[i] != s->last_picture_ptr &&
1659 &s->picture[i] != s->next_picture_ptr &&
1660 s->picture[i].reference && !s->picture[i].needs_realloc) {
1661 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1662 av_log(avctx, AV_LOG_ERROR,
1663 "releasing zombie picture\n");
1664 ff_mpeg_unref_picture(s, &s->picture[i]);
d6db1c9c 1665 }
ee870491 1666 }
d52b4abe 1667
4b796681
JG
1668 ff_mpeg_unref_picture(s, &s->current_picture);
1669
aec25b1c 1670 release_unused_pictures(s);
e20c4069 1671
aec25b1c 1672 if (s->current_picture_ptr &&
f6774f90 1673 s->current_picture_ptr->f->buf[0] == NULL) {
aec25b1c
AK
1674 // we already have a unused image
1675 // (maybe it was set before reading the header)
1676 pic = s->current_picture_ptr;
1677 } else {
1678 i = ff_find_unused_picture(s, 0);
1679 if (i < 0) {
1680 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1681 return i;
5f194811 1682 }
aec25b1c
AK
1683 pic = &s->picture[i];
1684 }
5f194811 1685
aec25b1c
AK
1686 pic->reference = 0;
1687 if (!s->droppable) {
1688 if (s->pict_type != AV_PICTURE_TYPE_B)
1689 pic->reference = 3;
1690 }
b536d0aa 1691
f6774f90 1692 pic->f->coded_picture_number = s->coded_picture_number++;
115329f1 1693
aec25b1c
AK
1694 if (ff_alloc_picture(s, pic, 0) < 0)
1695 return -1;
93a21abd 1696
aec25b1c
AK
1697 s->current_picture_ptr = pic;
1698 // FIXME use only the vars from current_pic
f6774f90 1699 s->current_picture_ptr->f->top_field_first = s->top_field_first;
aec25b1c
AK
1700 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1701 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1702 if (s->picture_structure != PICT_FRAME)
f6774f90 1703 s->current_picture_ptr->f->top_field_first =
aec25b1c 1704 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1e491e29 1705 }
f6774f90 1706 s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
aec25b1c
AK
1707 !s->progressive_sequence;
1708 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
b7adc711 1709
f6774f90 1710 s->current_picture_ptr->f->pict_type = s->pict_type;
c65dfac4
KT
1711 // if (s->flags && CODEC_FLAG_QSCALE)
1712 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
f6774f90 1713 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
9f2e61b6 1714
759001c5
AK
1715 if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1716 s->current_picture_ptr)) < 0)
1717 return ret;
115329f1 1718
19cac8e3 1719 if (s->pict_type != AV_PICTURE_TYPE_B) {
c65dfac4 1720 s->last_picture_ptr = s->next_picture_ptr;
ba0c8981 1721 if (!s->droppable)
c65dfac4 1722 s->next_picture_ptr = s->current_picture_ptr;
de6d9b64 1723 }
1218777f
DB
1724 av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1725 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
f6774f90 1726 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
1727 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
1728 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
ba0c8981 1729 s->pict_type, s->droppable);
c65dfac4 1730
ee870491 1731 if ((s->last_picture_ptr == NULL ||
f6774f90 1732 s->last_picture_ptr->f->buf[0] == NULL) &&
ee870491
AK
1733 (s->pict_type != AV_PICTURE_TYPE_I ||
1734 s->picture_structure != PICT_FRAME)) {
1735 int h_chroma_shift, v_chroma_shift;
1736 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1737 &h_chroma_shift, &v_chroma_shift);
1738 if (s->pict_type != AV_PICTURE_TYPE_I)
1739 av_log(avctx, AV_LOG_ERROR,
1740 "warning: first frame is no keyframe\n");
1741 else if (s->picture_structure != PICT_FRAME)
1742 av_log(avctx, AV_LOG_INFO,
1743 "allocate dummy last picture for field based first keyframe\n");
1744
1745 /* Allocate a dummy frame */
1746 i = ff_find_unused_picture(s, 0);
1747 if (i < 0) {
1748 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1749 return i;
d52b4abe 1750 }
ee870491 1751 s->last_picture_ptr = &s->picture[i];
feded990
AK
1752
1753 s->last_picture_ptr->reference = 3;
f6774f90 1754 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
feded990 1755
ee870491
AK
1756 if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1757 s->last_picture_ptr = NULL;
1758 return -1;
d52b4abe 1759 }
d52b4abe 1760
f6774f90 1761 memset(s->last_picture_ptr->f->data[0], 0,
1762 avctx->height * s->last_picture_ptr->f->linesize[0]);
1763 memset(s->last_picture_ptr->f->data[1], 0x80,
ee870491 1764 (avctx->height >> v_chroma_shift) *
f6774f90 1765 s->last_picture_ptr->f->linesize[1]);
1766 memset(s->last_picture_ptr->f->data[2], 0x80,
ee870491 1767 (avctx->height >> v_chroma_shift) *
f6774f90 1768 s->last_picture_ptr->f->linesize[2]);
ee870491
AK
1769
1770 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1771 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1772 }
1773 if ((s->next_picture_ptr == NULL ||
f6774f90 1774 s->next_picture_ptr->f->buf[0] == NULL) &&
ee870491
AK
1775 s->pict_type == AV_PICTURE_TYPE_B) {
1776 /* Allocate a dummy frame */
1777 i = ff_find_unused_picture(s, 0);
1778 if (i < 0) {
1779 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1780 return i;
759001c5 1781 }
ee870491 1782 s->next_picture_ptr = &s->picture[i];
feded990
AK
1783
1784 s->next_picture_ptr->reference = 3;
f6774f90 1785 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
feded990 1786
ee870491
AK
1787 if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1788 s->next_picture_ptr = NULL;
1789 return -1;
759001c5 1790 }
ee870491
AK
1791 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1792 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1793 }
1794
1795 if (s->last_picture_ptr) {
1796 ff_mpeg_unref_picture(s, &s->last_picture);
f6774f90 1797 if (s->last_picture_ptr->f->buf[0] &&
ee870491
AK
1798 (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1799 s->last_picture_ptr)) < 0)
1800 return ret;
1801 }
1802 if (s->next_picture_ptr) {
1803 ff_mpeg_unref_picture(s, &s->next_picture);
f6774f90 1804 if (s->next_picture_ptr->f->buf[0] &&
ee870491
AK
1805 (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1806 s->next_picture_ptr)) < 0)
1807 return ret;
1808 }
115329f1 1809
72072bf9 1810 if (s->pict_type != AV_PICTURE_TYPE_I &&
f6774f90 1811 !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
72072bf9
LB
1812 av_log(s, AV_LOG_ERROR,
1813 "Non-reference picture received and no reference available\n");
1814 return AVERROR_INVALIDDATA;
1815 }
3ab77000 1816
19cac8e3 1817 if (s->picture_structure!= PICT_FRAME) {
b536d0aa 1818 int i;
c65dfac4
KT
1819 for (i = 0; i < 4; i++) {
1820 if (s->picture_structure == PICT_BOTTOM_FIELD) {
f6774f90 1821 s->current_picture.f->data[i] +=
1822 s->current_picture.f->linesize[i];
115329f1 1823 }
f6774f90 1824 s->current_picture.f->linesize[i] *= 2;
1825 s->last_picture.f->linesize[i] *= 2;
1826 s->next_picture.f->linesize[i] *= 2;
b536d0aa
MN
1827 }
1828 }
115329f1 1829
5b22d6e1 1830 s->err_recognition = avctx->err_recognition;
aa388dba 1831
c65dfac4
KT
1832 /* set dequantizer, we can't do it during init as
1833 * it might change for mpeg4 and we can't do it in the header
1834 * decode as init is not called for mpeg4 there yet */
36ef5369 1835 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
d50635cd
MN
1836 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1837 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
c65dfac4 1838 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
d50635cd
MN
1839 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1840 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
c65dfac4 1841 } else {
d50635cd
MN
1842 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1843 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1844 }
d6db1c9c 1845
19e30a58
DB
1846#if FF_API_XVMC
1847FF_DISABLE_DEPRECATION_WARNINGS
c65dfac4 1848 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
a002da79 1849 return ff_xvmc_field_start(s, avctx);
19e30a58
DB
1850FF_ENABLE_DEPRECATION_WARNINGS
1851#endif /* FF_API_XVMC */
83344066 1852
d6db1c9c 1853 return 0;
de6d9b64 1854}
21af69f7 1855
381a7225 1856/* called after a frame has been decoded. */
efd29844 1857void ff_MPV_frame_end(MpegEncContext *s)
de6d9b64 1858{
19e30a58
DB
1859#if FF_API_XVMC
1860FF_DISABLE_DEPRECATION_WARNINGS
6a9c8594 1861 /* redraw edges for the frame if decoding didn't complete */
c65dfac4
KT
1862 // just to make sure that all data is rendered.
1863 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
a002da79 1864 ff_xvmc_field_end(s);
19e30a58
DB
1865 } else
1866FF_ENABLE_DEPRECATION_WARNINGS
1867#endif /* FF_API_XVMC */
6a9c8594 1868
5975626d 1869 emms_c();
115329f1 1870
19cac8e3 1871 if (s->current_picture.reference)
759001c5 1872 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
de6d9b64
FB
1873}
1874
7bc9090a 1875/**
c65dfac4 1876 * Print debugging info for the given picture.
7bc9090a 1877 */
759001c5 1878void ff_print_debug_info(MpegEncContext *s, Picture *p)
c65dfac4 1879{
759001c5
AK
1880 AVFrame *pict;
1881 if (s->avctx->hwaccel || !p || !p->mb_type)
c65dfac4 1882 return;
f6774f90 1883 pict = p->f;
7bc9090a 1884
c65dfac4 1885 if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
7bc9090a 1886 int x,y;
115329f1 1887
0c9bbaec
WH
1888 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1889 switch (pict->pict_type) {
c65dfac4
KT
1890 case AV_PICTURE_TYPE_I:
1891 av_log(s->avctx,AV_LOG_DEBUG,"I\n");
1892 break;
1893 case AV_PICTURE_TYPE_P:
1894 av_log(s->avctx,AV_LOG_DEBUG,"P\n");
1895 break;
1896 case AV_PICTURE_TYPE_B:
1897 av_log(s->avctx,AV_LOG_DEBUG,"B\n");
1898 break;
1899 case AV_PICTURE_TYPE_S:
1900 av_log(s->avctx,AV_LOG_DEBUG,"S\n");
1901 break;
1902 case AV_PICTURE_TYPE_SI:
1903 av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
1904 break;
1905 case AV_PICTURE_TYPE_SP:
1906 av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
1907 break;
0c9bbaec 1908 }
c65dfac4
KT
1909 for (y = 0; y < s->mb_height; y++) {
1910 for (x = 0; x < s->mb_width; x++) {
1911 if (s->avctx->debug & FF_DEBUG_SKIP) {
1912 int count = s->mbskip_table[x + y * s->mb_stride];
1913 if (count > 9)
1914 count = 9;
9b879566 1915 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
7bc9090a 1916 }
c65dfac4
KT
1917 if (s->avctx->debug & FF_DEBUG_QP) {
1918 av_log(s->avctx, AV_LOG_DEBUG, "%2d",
759001c5 1919 p->qscale_table[x + y * s->mb_stride]);
7bc9090a 1920 }
c65dfac4 1921 if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
759001c5 1922 int mb_type = p->mb_type[x + y * s->mb_stride];
c65dfac4
KT
1923 // Type & MV direction
1924 if (IS_PCM(mb_type))
9b879566 1925 av_log(s->avctx, AV_LOG_DEBUG, "P");
c65dfac4 1926 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
9b879566 1927 av_log(s->avctx, AV_LOG_DEBUG, "A");
c65dfac4 1928 else if (IS_INTRA4x4(mb_type))
9b879566 1929 av_log(s->avctx, AV_LOG_DEBUG, "i");
c65dfac4 1930 else if (IS_INTRA16x16(mb_type))
9b879566 1931 av_log(s->avctx, AV_LOG_DEBUG, "I");
c65dfac4 1932 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
9b879566 1933 av_log(s->avctx, AV_LOG_DEBUG, "d");
c65dfac4 1934 else if (IS_DIRECT(mb_type))
9b879566 1935 av_log(s->avctx, AV_LOG_DEBUG, "D");
c65dfac4 1936 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
9b879566 1937 av_log(s->avctx, AV_LOG_DEBUG, "g");
c65dfac4 1938 else if (IS_GMC(mb_type))
9b879566 1939 av_log(s->avctx, AV_LOG_DEBUG, "G");
c65dfac4 1940 else if (IS_SKIP(mb_type))
9b879566 1941 av_log(s->avctx, AV_LOG_DEBUG, "S");
c65dfac4 1942 else if (!USES_LIST(mb_type, 1))
9b879566 1943 av_log(s->avctx, AV_LOG_DEBUG, ">");
c65dfac4 1944 else if (!USES_LIST(mb_type, 0))
9b879566 1945 av_log(s->avctx, AV_LOG_DEBUG, "<");
c65dfac4 1946 else {
7bc9090a 1947 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
9b879566 1948 av_log(s->avctx, AV_LOG_DEBUG, "X");
7bc9090a 1949 }
115329f1 1950
c65dfac4
KT
1951 // segmentation
1952 if (IS_8X8(mb_type))
9b879566 1953 av_log(s->avctx, AV_LOG_DEBUG, "+");
c65dfac4 1954 else if (IS_16X8(mb_type))
9b879566 1955 av_log(s->avctx, AV_LOG_DEBUG, "-");
c65dfac4 1956 else if (IS_8X16(mb_type))
30344a83 1957 av_log(s->avctx, AV_LOG_DEBUG, "|");
c65dfac4 1958 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
9b879566 1959 av_log(s->avctx, AV_LOG_DEBUG, " ");
7bc9090a 1960 else
9b879566 1961 av_log(s->avctx, AV_LOG_DEBUG, "?");
115329f1
DB
1962
1963
c65dfac4 1964 if (IS_INTERLACED(mb_type))
9b879566 1965 av_log(s->avctx, AV_LOG_DEBUG, "=");
7bc9090a 1966 else
9b879566 1967 av_log(s->avctx, AV_LOG_DEBUG, " ");
7bc9090a 1968 }
7bc9090a 1969 }
9b879566 1970 av_log(s->avctx, AV_LOG_DEBUG, "\n");
7bc9090a
MN
1971 }
1972 }
1973}
1974
6a9c8594
AS
1975/**
1976 * find the lowest MB row referenced in the MVs
1977 */
efd29844 1978int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
6a9c8594
AS
1979{
1980 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1981 int my, off, i, mvs;
1982
6bcdfe48
MN
1983 if (s->picture_structure != PICT_FRAME || s->mcsel)
1984 goto unhandled;
6a9c8594
AS
1985
1986 switch (s->mv_type) {
1987 case MV_TYPE_16X16:
1988 mvs = 1;
1989 break;
1990 case MV_TYPE_16X8:
1991 mvs = 2;
1992 break;
1993 case MV_TYPE_8X8:
1994 mvs = 4;
1995 break;
1996 default:
1997 goto unhandled;
1998 }
1999
2000 for (i = 0; i < mvs; i++) {
2001 my = s->mv[dir][i][1]<<qpel_shift;
2002 my_max = FFMAX(my_max, my);
2003 my_min = FFMIN(my_min, my);
2004 }
2005
2006 off = (FFMAX(-my_min, my_max) + 63) >> 6;
2007
2008 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
2009unhandled:
2010 return s->mb_height-1;
2011}
2012
3ada94ba
BF
2013/* put block[] to dest[] */
2014static inline void put_dct(MpegEncContext *s,
88bd7fdc 2015 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
178fcca8 2016{
3ada94ba
BF
2017 s->dct_unquantize_intra(s, block, i, qscale);
2018 s->dsp.idct_put (dest, line_size, block);
2019}
da9c9637 2020
3ada94ba
BF
2021/* add block[] to dest[] */
2022static inline void add_dct(MpegEncContext *s,
88bd7fdc 2023 int16_t *block, int i, uint8_t *dest, int line_size)
3ada94ba
BF
2024{
2025 if (s->block_last_index[i] >= 0) {
2026 s->dsp.idct_add (dest, line_size, block);
2027 }
2028}
2417652e 2029
115329f1 2030static inline void add_dequant_dct(MpegEncContext *s,
88bd7fdc 2031 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
0f440e02 2032{
de6d9b64 2033 if (s->block_last_index[i] >= 0) {
d50635cd 2034 s->dct_unquantize_inter(s, block, i, qscale);
9dbcbd92 2035
b0368839 2036 s->dsp.idct_add (dest, line_size, block);
de6d9b64
FB
2037 }
2038}
2039
7f2fe444 2040/**
58c42af7 2041 * Clean dc, ac, coded_block for the current non-intra MB.
7f2fe444
MN
2042 */
2043void ff_clean_intra_table_entries(MpegEncContext *s)
2044{
137c8468 2045 int wrap = s->b8_stride;
7f2fe444 2046 int xy = s->block_index[0];
115329f1
DB
2047
2048 s->dc_val[0][xy ] =
2049 s->dc_val[0][xy + 1 ] =
7f2fe444
MN
2050 s->dc_val[0][xy + wrap] =
2051 s->dc_val[0][xy + 1 + wrap] = 1024;
2052 /* ac pred */
0c1a9eda
ZK
2053 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2054 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
7f2fe444
MN
2055 if (s->msmpeg4_version>=3) {
2056 s->coded_block[xy ] =
2057 s->coded_block[xy + 1 ] =
2058 s->coded_block[xy + wrap] =
2059 s->coded_block[xy + 1 + wrap] = 0;
2060 }
2061 /* chroma */
137c8468
MN
2062 wrap = s->mb_stride;
2063 xy = s->mb_x + s->mb_y * wrap;
7f2fe444
MN
2064 s->dc_val[1][xy] =
2065 s->dc_val[2][xy] = 1024;
2066 /* ac pred */
0c1a9eda
ZK
2067 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2068 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
115329f1 2069
137c8468 2070 s->mbintra_table[xy]= 0;
7f2fe444
MN
2071}
2072
de6d9b64
FB
2073/* generic function called after a macroblock has been parsed by the
2074 decoder or after it has been encoded by the encoder.
2075
2076 Important variables used:
2077 s->mb_intra : true if intra macroblock
2078 s->mv_dir : motion vector direction
2079 s->mv_type : motion vector type
2080 s->mv : motion vector
2081 s->interlaced_dct : true if interlaced dct used (mpeg2)
2082 */
54816a3e 2083static av_always_inline
88bd7fdc 2084void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2bcbd984 2085 int is_mpeg12)
de6d9b64 2086{
7bc9090a 2087 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
19e30a58
DB
2088
2089#if FF_API_XVMC
2090FF_DISABLE_DEPRECATION_WARNINGS
83344066 2091 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
78f9a878 2092 ff_xvmc_decode_mb(s);//xvmc uses pblocks
2e7b4c84
IK
2093 return;
2094 }
19e30a58
DB
2095FF_ENABLE_DEPRECATION_WARNINGS
2096#endif /* FF_API_XVMC */
de6d9b64 2097
8289c6fa 2098 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
759001c5 2099 /* print DCT coefficients */
8289c6fa 2100 int i,j;
c4fb3b03
MN
2101 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2102 for(i=0; i<6; i++){
2103 for(j=0; j<64; j++){
759001c5 2104 av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
c4fb3b03
MN
2105 }
2106 av_log(s->avctx, AV_LOG_DEBUG, "\n");
2107 }
8289c6fa
WH
2108 }
2109
759001c5 2110 s->current_picture.qscale_table[mb_xy] = s->qscale;
79e7b305 2111
de6d9b64
FB
2112 /* update DC predictors for P macroblocks */
2113 if (!s->mb_intra) {
bd7c626a 2114 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
0f440e02 2115 if(s->mbintra_table[mb_xy])
7f2fe444 2116 ff_clean_intra_table_entries(s);
de6d9b64 2117 } else {
7f2fe444
MN
2118 s->last_dc[0] =
2119 s->last_dc[1] =
de6d9b64
FB
2120 s->last_dc[2] = 128 << s->intra_dc_precision;
2121 }
2122 }
bd7c626a 2123 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
0f440e02 2124 s->mbintra_table[mb_xy]=1;
bff6ecaa 2125
975a1447 2126 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
0c1a9eda 2127 uint8_t *dest_y, *dest_cb, *dest_cr;
0f440e02 2128 int dct_linesize, dct_offset;
b3184779
MN
2129 op_pixels_func (*op_pix)[4];
2130 qpel_mc_func (*op_qpix)[16];
f6774f90 2131 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
2132 const int uvlinesize = s->current_picture.f->linesize[1];
2bcbd984
MR
2133 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
2134 const int block_size = 8;
3bb4e23a 2135
1e491e29 2136 /* avoid copy if macroblock skipped in last frame too */
1e491e29
MN
2137 /* skip only during decoding as we might trash the buffers during encoding a bit */
2138 if(!s->encoding){
0c1a9eda 2139 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1e491e29 2140
160d679c
MM
2141 if (s->mb_skipped) {
2142 s->mb_skipped= 0;
975a1447 2143 assert(s->pict_type!=AV_PICTURE_TYPE_I);
8400b126 2144 *mbskip_ptr = 1;
759001c5 2145 } else if(!s->current_picture.reference) {
8400b126 2146 *mbskip_ptr = 1;
f943e138 2147 } else{
3bb4e23a
FB
2148 *mbskip_ptr = 0; /* not skipped */
2149 }
3994623d 2150 }
115329f1 2151
ffdff4d7 2152 dct_linesize = linesize << s->interlaced_dct;
3dc99a18 2153 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
115329f1 2154
b68ab260
MN
2155 if(readable){
2156 dest_y= s->dest[0];
2157 dest_cb= s->dest[1];
2158 dest_cr= s->dest[2];
2159 }else{
9c3d33d6 2160 dest_y = s->b_scratchpad;
ae35f5e1 2161 dest_cb= s->b_scratchpad+16*linesize;
ffdff4d7 2162 dest_cr= s->b_scratchpad+32*linesize;
b68ab260 2163 }
178fcca8 2164
de6d9b64
FB
2165 if (!s->mb_intra) {
2166 /* motion handling */
dfb706da 2167 /* decoding or more than one mb_type (MC was already done otherwise) */
7d1c3fc1 2168 if(!s->encoding){
6a9c8594 2169
27237d52 2170 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
6a9c8594 2171 if (s->mv_dir & MV_DIR_FORWARD) {
759001c5 2172 ff_thread_await_progress(&s->last_picture_ptr->tf,
47c0ac96
DB
2173 ff_MPV_lowest_referenced_row(s, 0),
2174 0);
6a9c8594
AS
2175 }
2176 if (s->mv_dir & MV_DIR_BACKWARD) {
759001c5 2177 ff_thread_await_progress(&s->next_picture_ptr->tf,
47c0ac96
DB
2178 ff_MPV_lowest_referenced_row(s, 1),
2179 0);
6a9c8594
AS
2180 }
2181 }
2182
2bcbd984
MR
2183 op_qpix= s->me.qpel_put;
2184 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
f4fed5a2 2185 op_pix = s->hdsp.put_pixels_tab;
178fcca8 2186 }else{
f4fed5a2 2187 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2bcbd984
MR
2188 }
2189 if (s->mv_dir & MV_DIR_FORWARD) {
f6774f90 2190 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
f4fed5a2 2191 op_pix = s->hdsp.avg_pixels_tab;
2bcbd984
MR
2192 op_qpix= s->me.qpel_avg;
2193 }
2194 if (s->mv_dir & MV_DIR_BACKWARD) {
f6774f90 2195 ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
9dbcbd92 2196 }
de6d9b64
FB
2197 }
2198
0f440e02 2199 /* skip dequant / idct if we are really late ;) */
8c3eba7c 2200 if(s->avctx->skip_idct){
975a1447
SS
2201 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
2202 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
8c3eba7c
MN
2203 || s->avctx->skip_idct >= AVDISCARD_ALL)
2204 goto skip_idct;
2205 }
0f440e02 2206
de6d9b64 2207 /* add dct residue */
36ef5369
AK
2208 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
2209 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
178fcca8
MN
2210 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2211 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2212 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2213 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
0f440e02 2214
49fb20cb 2215 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2d974017
BC
2216 if (s->chroma_y_shift){
2217 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2218 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2219 }else{
2220 dct_linesize >>= 1;
2221 dct_offset >>=1;
2222 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2223 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2224 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2225 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2226 }
b50eef3a 2227 }
36ef5369 2228 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
178fcca8
MN
2229 add_dct(s, block[0], 0, dest_y , dct_linesize);
2230 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
2231 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
2232 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
de6d9b64 2233
49fb20cb 2234 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
ffdff4d7
IK
2235 if(s->chroma_y_shift){//Chroma420
2236 add_dct(s, block[4], 4, dest_cb, uvlinesize);
2237 add_dct(s, block[5], 5, dest_cr, uvlinesize);
2238 }else{
2239 //chroma422
2240 dct_linesize = uvlinesize << s->interlaced_dct;
3dc99a18 2241 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
ffdff4d7
IK
2242
2243 add_dct(s, block[4], 4, dest_cb, dct_linesize);
2244 add_dct(s, block[5], 5, dest_cr, dct_linesize);
2245 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
2246 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
2247 if(!s->chroma_x_shift){//Chroma444
2248 add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
2249 add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
2250 add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
2251 add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
2252 }
2253 }
2254 }//fi gray
2255 }
d702a2e6 2256 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
1457ab52 2257 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
0f440e02 2258 }
de6d9b64
FB
2259 } else {
2260 /* dct only in intra block */
36ef5369 2261 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
178fcca8
MN
2262 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2263 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2264 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2265 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
a0201736 2266
49fb20cb 2267 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2d974017
BC
2268 if(s->chroma_y_shift){
2269 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2270 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2271 }else{
2272 dct_offset >>=1;
2273 dct_linesize >>=1;
2274 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2275 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2276 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
3ada94ba 2277 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
477ab036
MN
2278 }
2279 }
2280 }else{
3ada94ba
BF
2281 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
2282 s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
2283 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
2284 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
fbb89806 2285
49fb20cb 2286 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
3ada94ba
BF
2287 if(s->chroma_y_shift){
2288 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
2289 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
2290 }else{
fbb89806 2291
3ada94ba 2292 dct_linesize = uvlinesize << s->interlaced_dct;
3dc99a18 2293 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
9981dfc6 2294
3ada94ba
BF
2295 s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
2296 s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
2297 s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
2298 s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
2299 if(!s->chroma_x_shift){//Chroma444
2300 s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
2301 s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
2302 s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
2303 s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
2304 }
2305 }
2306 }//gray
9981dfc6 2307 }
477ab036 2308 }
3ada94ba
BF
2309skip_idct:
2310 if(!readable){
f4fed5a2
RB
2311 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
2312 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
2313 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
34f60ee6 2314 }
477ab036 2315 }
477ab036
MN
2316}
2317
88bd7fdc 2318void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
b250f9c6 2319#if !CONFIG_SMALL
bd7c626a 2320 if(s->out_format == FMT_MPEG1) {
2bcbd984 2321 MPV_decode_mb_internal(s, block, 1);
bd7c626a
KC
2322 } else
2323#endif
2bcbd984 2324 MPV_decode_mb_internal(s, block, 0);
77ea0d4b
MN
2325}
2326
1d0feb5d
AK
2327void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
2328{
f6774f90 2329 ff_draw_horiz_band(s->avctx, s->current_picture.f,
2330 s->last_picture.f, y, h, s->picture_structure,
54b2ce74 2331 s->first_field, s->low_delay);
1d0feb5d
AK
2332}
2333
3ada94ba 2334void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
f6774f90 2335 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
2336 const int uvlinesize = s->current_picture.f->linesize[1];
2bcbd984 2337 const int mb_size= 4;
77ea0d4b 2338
3ada94ba
BF
2339 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
2340 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
2341 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
2342 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
2343 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
2344 s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
2345 //block_index is not used by mpeg2, so it is not affected by chroma_format
115329f1 2346
f6774f90 2347 s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
2348 s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
2349 s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
115329f1 2350
975a1447 2351 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
3ada94ba 2352 {
078cdecf 2353 if(s->picture_structure==PICT_FRAME){
3ada94ba
BF
2354 s->dest[0] += s->mb_y * linesize << mb_size;
2355 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2356 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
078cdecf
MN
2357 }else{
2358 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
2359 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2360 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2361 assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
2362 }
77ea0d4b 2363 }
77ea0d4b 2364}
115329f1 2365
0b016eb9
DB
2366/**
2367 * Permute an 8x8 block.
2368 * @param block the block which will be permuted according to the given permutation vector
2369 * @param permutation the permutation vector
2370 * @param last the last non zero coefficient in scantable order, used to speed the permutation up
2371 * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
2372 * (inverse) permutated to scantable order!
2373 */
2374void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
2375{
2376 int i;
2377 int16_t temp[64];
2378
2379 if(last<=0) return;
2380 //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
2381
2382 for(i=0; i<=last; i++){
2383 const int j= scantable[i];
2384 temp[j]= block[j];
2385 block[j]=0;
2386 }
2387
2388 for(i=0; i<=last; i++){
2389 const int j= scantable[i];
2390 const int perm_j= permutation[j];
2391 block[perm_j]= temp[j];
2392 }
2393}
2394
3ada94ba
BF
2395void ff_mpeg_flush(AVCodecContext *avctx){
2396 int i;
2397 MpegEncContext *s = avctx->priv_data;
77ea0d4b 2398
3ada94ba
BF
2399 if(s==NULL || s->picture==NULL)
2400 return;
77ea0d4b 2401
759001c5
AK
2402 for (i = 0; i < MAX_PICTURE_COUNT; i++)
2403 ff_mpeg_unref_picture(s, &s->picture[i]);
3ada94ba 2404 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
115329f1 2405
feec9349
HL
2406 ff_mpeg_unref_picture(s, &s->current_picture);
2407 ff_mpeg_unref_picture(s, &s->last_picture);
2408 ff_mpeg_unref_picture(s, &s->next_picture);
2409
3ada94ba 2410 s->mb_x= s->mb_y= 0;
7801d21d 2411
3ada94ba
BF
2412 s->parse_context.state= -1;
2413 s->parse_context.frame_start_found= 0;
2414 s->parse_context.overread= 0;
2415 s->parse_context.overread_index= 0;
2416 s->parse_context.index= 0;
2417 s->parse_context.last_index= 0;
2418 s->bitstream_buffer_size=0;
2419 s->pp_time=0;
de6d9b64
FB
2420}
2421
b776e3d1
AJ
2422/**
2423 * set qscale and update qscale dependent variables.
2424 */
2425void ff_set_qscale(MpegEncContext * s, int qscale)
2426{
2427 if (qscale < 1)
2428 qscale = 1;
2429 else if (qscale > 31)
2430 qscale = 31;
2431
2432 s->qscale = qscale;
2433 s->chroma_qscale= s->chroma_qscale_table[qscale];
2434
2435 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
2436 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
2437}
6a9c8594 2438
efd29844 2439void ff_MPV_report_decode_progress(MpegEncContext *s)
6a9c8594 2440{
54974c62 2441 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
759001c5 2442 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
6a9c8594 2443}
54974c62 2444
d88738e4 2445#if CONFIG_ERROR_RESILIENCE
d66e305b
VG
2446void ff_mpeg_set_erpic(ERPicture *dst, Picture *src)
2447{
2448 int i;
2449
2450 if (!src)
2451 return;
2452
f6774f90 2453 dst->f = src->f;
d66e305b
VG
2454 dst->tf = &src->tf;
2455
2456 for (i = 0; i < 2; i++) {
2457 dst->motion_val[i] = src->motion_val[i];
2458 dst->ref_index[i] = src->ref_index[i];
2459 }
2460
2461 dst->mb_type = src->mb_type;
2462 dst->field_picture = src->field_picture;
2463}
2464
54974c62
AK
2465void ff_mpeg_er_frame_start(MpegEncContext *s)
2466{
2467 ERContext *er = &s->er;
2468
d66e305b
VG
2469 ff_mpeg_set_erpic(&er->cur_pic, s->current_picture_ptr);
2470 ff_mpeg_set_erpic(&er->next_pic, s->next_picture_ptr);
2471 ff_mpeg_set_erpic(&er->last_pic, s->last_picture_ptr);
54974c62
AK
2472
2473 er->pp_time = s->pp_time;
2474 er->pb_time = s->pb_time;
2475 er->quarter_sample = s->quarter_sample;
2476 er->partitioned_frame = s->partitioned_frame;
2477
2478 ff_er_frame_start(er);
2479}
d88738e4 2480#endif /* CONFIG_ERROR_RESILIENCE */