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