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