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