mpegvideo: Fix undefined negative shifts in ff_init_block_index
[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"
8b5007a3 40#include "mpeg_er.h"
e0c16e4e 41#include "mpegutils.h"
de6d9b64 42#include "mpegvideo.h"
378a0008 43#include "mpegvideodata.h"
d9c9259f 44#include "mjpegenc.h"
15025553 45#include "msmpeg4.h"
368f5035 46#include "qpeldsp.h"
4440bd0d 47#include "xvmc_internal.h"
6a9c8594 48#include "thread.h"
2f15846a 49#include "wmv2.h"
e96682e6 50#include <limits.h>
de6d9b64 51
a4d0c6e0
AK
52static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
53 int16_t *block, int n, int qscale)
54{
55 int i, level, nCoeffs;
56 const uint16_t *quant_matrix;
57
58 nCoeffs= s->block_last_index[n];
59
60 if (n < 4)
61 block[0] = block[0] * s->y_dc_scale;
62 else
63 block[0] = block[0] * s->c_dc_scale;
64 /* XXX: only mpeg1 */
65 quant_matrix = s->intra_matrix;
66 for(i=1;i<=nCoeffs;i++) {
67 int j= s->intra_scantable.permutated[i];
68 level = block[j];
69 if (level) {
70 if (level < 0) {
71 level = -level;
72 level = (int)(level * qscale * quant_matrix[j]) >> 3;
73 level = (level - 1) | 1;
74 level = -level;
75 } else {
76 level = (int)(level * qscale * quant_matrix[j]) >> 3;
77 level = (level - 1) | 1;
78 }
79 block[j] = level;
80 }
81 }
82}
83
84static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
85 int16_t *block, int n, int qscale)
86{
87 int i, level, nCoeffs;
88 const uint16_t *quant_matrix;
89
90 nCoeffs= s->block_last_index[n];
91
92 quant_matrix = s->inter_matrix;
93 for(i=0; i<=nCoeffs; i++) {
94 int j= s->intra_scantable.permutated[i];
95 level = block[j];
96 if (level) {
97 if (level < 0) {
98 level = -level;
99 level = (((level << 1) + 1) * qscale *
100 ((int) (quant_matrix[j]))) >> 4;
101 level = (level - 1) | 1;
102 level = -level;
103 } else {
104 level = (((level << 1) + 1) * qscale *
105 ((int) (quant_matrix[j]))) >> 4;
106 level = (level - 1) | 1;
107 }
108 block[j] = level;
109 }
110 }
111}
112
113static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
114 int16_t *block, int n, int qscale)
115{
116 int i, level, nCoeffs;
117 const uint16_t *quant_matrix;
118
119 if(s->alternate_scan) nCoeffs= 63;
120 else nCoeffs= s->block_last_index[n];
121
122 if (n < 4)
123 block[0] = block[0] * s->y_dc_scale;
124 else
125 block[0] = block[0] * s->c_dc_scale;
126 quant_matrix = s->intra_matrix;
127 for(i=1;i<=nCoeffs;i++) {
128 int j= s->intra_scantable.permutated[i];
129 level = block[j];
130 if (level) {
131 if (level < 0) {
132 level = -level;
133 level = (int)(level * qscale * quant_matrix[j]) >> 3;
134 level = -level;
135 } else {
136 level = (int)(level * qscale * quant_matrix[j]) >> 3;
137 }
138 block[j] = level;
139 }
140 }
141}
142
143static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
144 int16_t *block, int n, int qscale)
145{
146 int i, level, nCoeffs;
147 const uint16_t *quant_matrix;
148 int sum=-1;
149
150 if(s->alternate_scan) nCoeffs= 63;
151 else nCoeffs= s->block_last_index[n];
152
153 if (n < 4)
154 block[0] = block[0] * s->y_dc_scale;
155 else
156 block[0] = block[0] * s->c_dc_scale;
157 quant_matrix = s->intra_matrix;
158 for(i=1;i<=nCoeffs;i++) {
159 int j= s->intra_scantable.permutated[i];
160 level = block[j];
161 if (level) {
162 if (level < 0) {
163 level = -level;
164 level = (int)(level * qscale * quant_matrix[j]) >> 3;
165 level = -level;
166 } else {
167 level = (int)(level * qscale * quant_matrix[j]) >> 3;
168 }
169 block[j] = level;
170 sum+=level;
171 }
172 }
173 block[63]^=sum&1;
174}
175
176static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
177 int16_t *block, int n, int qscale)
178{
179 int i, level, nCoeffs;
180 const uint16_t *quant_matrix;
181 int sum=-1;
182
183 if(s->alternate_scan) nCoeffs= 63;
184 else nCoeffs= s->block_last_index[n];
185
186 quant_matrix = s->inter_matrix;
187 for(i=0; i<=nCoeffs; i++) {
188 int j= s->intra_scantable.permutated[i];
189 level = block[j];
190 if (level) {
191 if (level < 0) {
192 level = -level;
193 level = (((level << 1) + 1) * qscale *
194 ((int) (quant_matrix[j]))) >> 4;
195 level = -level;
196 } else {
197 level = (((level << 1) + 1) * qscale *
198 ((int) (quant_matrix[j]))) >> 4;
199 }
200 block[j] = level;
201 sum+=level;
202 }
203 }
204 block[63]^=sum&1;
205}
206
207static void dct_unquantize_h263_intra_c(MpegEncContext *s,
208 int16_t *block, int n, int qscale)
209{
210 int i, level, qmul, qadd;
211 int nCoeffs;
212
213 assert(s->block_last_index[n]>=0);
214
215 qmul = qscale << 1;
216
217 if (!s->h263_aic) {
218 if (n < 4)
219 block[0] = block[0] * s->y_dc_scale;
220 else
221 block[0] = block[0] * s->c_dc_scale;
222 qadd = (qscale - 1) | 1;
223 }else{
224 qadd = 0;
225 }
226 if(s->ac_pred)
227 nCoeffs=63;
228 else
229 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
230
231 for(i=1; i<=nCoeffs; i++) {
232 level = block[i];
233 if (level) {
234 if (level < 0) {
235 level = level * qmul - qadd;
236 } else {
237 level = level * qmul + qadd;
238 }
239 block[i] = level;
240 }
241 }
242}
243
244static void dct_unquantize_h263_inter_c(MpegEncContext *s,
245 int16_t *block, int n, int qscale)
246{
247 int i, level, qmul, qadd;
248 int nCoeffs;
249
250 assert(s->block_last_index[n]>=0);
251
252 qadd = (qscale - 1) | 1;
253 qmul = qscale << 1;
254
255 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
256
257 for(i=0; i<=nCoeffs; i++) {
258 level = block[i];
259 if (level) {
260 if (level < 0) {
261 level = level * qmul - qadd;
262 } else {
263 level = level * qmul + qadd;
264 }
265 block[i] = level;
266 }
267 }
268}
269
defdfc9a 270/* init common dct for both encoder and decoder */
998c9f15 271static av_cold int dct_init(MpegEncContext *s)
de6d9b64 272{
e74433a8 273 ff_blockdsp_init(&s->bdsp, s->avctx);
f4fed5a2 274 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
fab9df63 275 ff_mpegvideodsp_init(&s->mdsp);
1f4ea4e0 276 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
c3027b4d 277
d50635cd
MN
278 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
279 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
280 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
281 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
282 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
7c6eb0a1 283 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
e27b6e62 284 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
d50635cd 285 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
b0368839 286
16e66522 287 if (HAVE_INTRINSICS_NEON)
835f798c 288 ff_mpv_common_init_neon(s);
16e66522 289
70dd8892 290 if (ARCH_ARM)
835f798c 291 ff_mpv_common_init_arm(s);
70dd8892 292 if (ARCH_PPC)
835f798c 293 ff_mpv_common_init_ppc(s);
70dd8892 294 if (ARCH_X86)
835f798c 295 ff_mpv_common_init_x86(s);
676e200c 296
998c9f15
JS
297 return 0;
298}
299
300av_cold void ff_mpv_idct_init(MpegEncContext *s)
301{
302 ff_idctdsp_init(&s->idsp, s->avctx);
303
2ad1516a 304 /* load & permutate scantables
363114e8
KT
305 * note: only wmv uses different ones
306 */
307 if (s->alternate_scan) {
e3fcb143
DB
308 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
309 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
363114e8 310 } else {
e3fcb143
DB
311 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
312 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
bb198e19 313 }
e3fcb143
DB
314 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
315 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
defdfc9a
AB
316}
317
9bb11be0
VG
318static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
319{
320 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0,
321 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
322 s->mb_stride, s->mb_height, s->b8_stride,
323 &s->linesize, &s->uvlinesize);
324}
325
12b54a1f 326static int init_duplicate_context(MpegEncContext *s)
363114e8 327{
2cbd734a
MR
328 int y_size = s->b8_stride * (2 * s->mb_height + 1);
329 int c_size = s->mb_stride * (s->mb_height + 1);
330 int yc_size = y_size + 2 * c_size;
9c3d33d6
MN
331 int i;
332
da0c8664 333 s->sc.edge_emu_buffer =
f1d8763a
JG
334 s->me.scratchpad =
335 s->me.temp =
da0c8664
VG
336 s->sc.rd_scratchpad =
337 s->sc.b_scratchpad =
338 s->sc.obmc_scratchpad = NULL;
363114e8 339
9c3d33d6 340 if (s->encoding) {
363114e8
KT
341 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
342 ME_MAP_SIZE * sizeof(uint32_t), fail)
343 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
344 ME_MAP_SIZE * sizeof(uint32_t), fail)
1482aff2 345 if (s->noise_reduction) {
363114e8
KT
346 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
347 2 * 64 * sizeof(int), fail)
9c3d33d6 348 }
115329f1 349 }
88bd7fdc 350 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
363114e8 351 s->block = s->blocks[0];
9c3d33d6 352
363114e8 353 for (i = 0; i < 12; i++) {
21effaa4 354 s->pblocks[i] = &s->block[i];
9c3d33d6 355 }
419e3404
VG
356 if (s->avctx->codec_tag == AV_RL32("VCR2")) {
357 // exchange uv
358 int16_t (*tmp)[64];
359 tmp = s->pblocks[4];
360 s->pblocks[4] = s->pblocks[5];
361 s->pblocks[5] = tmp;
362 }
2cbd734a 363
79042a6e
MR
364 if (s->out_format == FMT_H263) {
365 /* ac values */
363114e8
KT
366 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
367 yc_size * sizeof(int16_t) * 16, fail);
2cbd734a
MR
368 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
369 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
370 s->ac_val[2] = s->ac_val[1] + c_size;
371 }
372
9c3d33d6
MN
373 return 0;
374fail:
835f798c 375 return -1; // free() through ff_mpv_common_end()
9c3d33d6
MN
376}
377
363114e8
KT
378static void free_duplicate_context(MpegEncContext *s)
379{
f929ab05 380 if (!s)
363114e8 381 return;
9c3d33d6 382
da0c8664 383 av_freep(&s->sc.edge_emu_buffer);
9c3d33d6 384 av_freep(&s->me.scratchpad);
363114e8 385 s->me.temp =
da0c8664
VG
386 s->sc.rd_scratchpad =
387 s->sc.b_scratchpad =
388 s->sc.obmc_scratchpad = NULL;
115329f1 389
9c3d33d6
MN
390 av_freep(&s->dct_error_sum);
391 av_freep(&s->me.map);
392 av_freep(&s->me.score_map);
393 av_freep(&s->blocks);
2cbd734a 394 av_freep(&s->ac_val_base);
363114e8 395 s->block = NULL;
9c3d33d6
MN
396}
397
363114e8
KT
398static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
399{
400#define COPY(a) bak->a = src->a
da0c8664 401 COPY(sc.edge_emu_buffer);
9c3d33d6 402 COPY(me.scratchpad);
a6f2c0d6 403 COPY(me.temp);
da0c8664
VG
404 COPY(sc.rd_scratchpad);
405 COPY(sc.b_scratchpad);
406 COPY(sc.obmc_scratchpad);
9c3d33d6
MN
407 COPY(me.map);
408 COPY(me.score_map);
409 COPY(blocks);
410 COPY(block);
411 COPY(start_mb_y);
412 COPY(end_mb_y);
413 COPY(me.map_generation);
414 COPY(pb);
415 COPY(dct_error_sum);
da16b204
MN
416 COPY(dct_count[0]);
417 COPY(dct_count[1]);
2cbd734a
MR
418 COPY(ac_val_base);
419 COPY(ac_val[0]);
420 COPY(ac_val[1]);
421 COPY(ac_val[2]);
9c3d33d6
MN
422#undef COPY
423}
424
f1d8763a 425int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
363114e8 426{
9c3d33d6 427 MpegEncContext bak;
f1d8763a 428 int i, ret;
363114e8
KT
429 // FIXME copy only needed parts
430 // START_TIMER
9c3d33d6
MN
431 backup_duplicate_context(&bak, dst);
432 memcpy(dst, src, sizeof(MpegEncContext));
433 backup_duplicate_context(dst, &bak);
363114e8 434 for (i = 0; i < 12; i++) {
21effaa4 435 dst->pblocks[i] = &dst->block[i];
c62c07d3 436 }
419e3404
VG
437 if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
438 // exchange uv
439 int16_t (*tmp)[64];
440 tmp = dst->pblocks[4];
441 dst->pblocks[4] = dst->pblocks[5];
442 dst->pblocks[5] = tmp;
443 }
da0c8664 444 if (!dst->sc.edge_emu_buffer &&
f8716a14
VG
445 (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
446 &dst->sc, dst->linesize)) < 0) {
f1d8763a
JG
447 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
448 "scratch buffers.\n");
449 return ret;
450 }
363114e8
KT
451 // STOP_TIMER("update_duplicate_context")
452 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
f1d8763a 453 return 0;
9c3d33d6
MN
454}
455
363114e8
KT
456int ff_mpeg_update_thread_context(AVCodecContext *dst,
457 const AVCodecContext *src)
6a9c8594 458{
759001c5 459 int i, ret;
6a9c8594
AS
460 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
461
363114e8
KT
462 if (dst == src || !s1->context_initialized)
463 return 0;
6a9c8594 464
363114e8
KT
465 // FIXME can parameters change on I-frames?
466 // in that case dst may need a reinit
467 if (!s->context_initialized) {
898e9a24 468 int err;
6a9c8594
AS
469 memcpy(s, s1, sizeof(MpegEncContext));
470
471 s->avctx = dst;
6a9c8594
AS
472 s->bitstream_buffer = NULL;
473 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
474
998c9f15 475 ff_mpv_idct_init(s);
898e9a24
VG
476 if ((err = ff_mpv_common_init(s)) < 0)
477 return err;
6a9c8594
AS
478 }
479
8701f4f8
JG
480 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
481 int err;
482 s->context_reinit = 0;
483 s->height = s1->height;
484 s->width = s1->width;
835f798c 485 if ((err = ff_mpv_common_frame_size_change(s)) < 0)
8701f4f8
JG
486 return err;
487 }
488
6a9c8594
AS
489 s->avctx->coded_height = s1->avctx->coded_height;
490 s->avctx->coded_width = s1->avctx->coded_width;
491 s->avctx->width = s1->avctx->width;
492 s->avctx->height = s1->avctx->height;
493
494 s->coded_picture_number = s1->coded_picture_number;
495 s->picture_number = s1->picture_number;
6a9c8594 496
759001c5 497 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
d5280455 498 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
f6774f90 499 if (s1->picture[i].f->buf[0] &&
a3f4c930 500 (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
759001c5
AK
501 return ret;
502 }
503
504#define UPDATE_PICTURE(pic)\
505do {\
d5280455 506 ff_mpeg_unref_picture(s->avctx, &s->pic);\
f6774f90 507 if (s1->pic.f->buf[0])\
a3f4c930 508 ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
759001c5 509 else\
7061bf09 510 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
759001c5
AK
511 if (ret < 0)\
512 return ret;\
513} while (0)
514
515 UPDATE_PICTURE(current_picture);
516 UPDATE_PICTURE(last_picture);
517 UPDATE_PICTURE(next_picture);
1481e198 518
d75190aa
VG
519#define REBASE_PICTURE(pic, new_ctx, old_ctx) \
520 ((pic && pic >= old_ctx->picture && \
521 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
522 &new_ctx->picture[pic - old_ctx->picture] : NULL)
523
4d9ec050
KT
524 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
525 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
526 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
6a9c8594 527
4d9ec050 528 // Error/bug resilience
6a9c8594
AS
529 s->next_p_frame_damaged = s1->next_p_frame_damaged;
530 s->workaround_bugs = s1->workaround_bugs;
531
4d9ec050 532 // MPEG4 timing info
e62a43f6 533 memcpy(&s->last_time_base, &s1->last_time_base,
ee8af2dd 534 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
e62a43f6 535 (char *) &s1->last_time_base);
4d9ec050
KT
536
537 // B-frame info
538 s->max_b_frames = s1->max_b_frames;
539 s->low_delay = s1->low_delay;
ba0c8981 540 s->droppable = s1->droppable;
4d9ec050
KT
541
542 // DivX handling (doesn't work)
543 s->divx_packed = s1->divx_packed;
544
545 if (s1->bitstream_buffer) {
546 if (s1->bitstream_buffer_size +
059a9348 547 AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
4d9ec050
KT
548 av_fast_malloc(&s->bitstream_buffer,
549 &s->allocated_bitstream_buffer_size,
550 s1->allocated_bitstream_buffer_size);
551 s->bitstream_buffer_size = s1->bitstream_buffer_size;
552 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
553 s1->bitstream_buffer_size);
554 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
059a9348 555 AV_INPUT_BUFFER_PADDING_SIZE);
6a9c8594
AS
556 }
557
f1d8763a 558 // linesize dependend scratch buffer allocation
da0c8664 559 if (!s->sc.edge_emu_buffer)
f1d8763a 560 if (s1->linesize) {
f8716a14
VG
561 if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
562 &s->sc, s1->linesize) < 0) {
f1d8763a
JG
563 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
564 "scratch buffers.\n");
565 return AVERROR(ENOMEM);
566 }
567 } else {
568 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
569 "be allocated due to unknown size.\n");
570 return AVERROR_BUG;
571 }
572
4d9ec050
KT
573 // MPEG2/interlacing info
574 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
575 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
6a9c8594 576
4d9ec050
KT
577 if (!s1->first_field) {
578 s->last_pict_type = s1->pict_type;
579 if (s1->current_picture_ptr)
f6774f90 580 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
6a9c8594
AS
581 }
582
583 return 0;
584}
585
3edcacde 586/**
58c42af7 587 * Set the given MpegEncContext to common defaults
4d9ec050 588 * (same for encoding and decoding).
58c42af7 589 * The changed fields will not depend upon the
4d9ec050 590 * prior state of the MpegEncContext.
3edcacde 591 */
835f798c 592void ff_mpv_common_defaults(MpegEncContext *s)
4d9ec050
KT
593{
594 s->y_dc_scale_table =
595 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
596 s->chroma_qscale_table = ff_default_chroma_qscale_table;
597 s->progressive_frame = 1;
598 s->progressive_sequence = 1;
599 s->picture_structure = PICT_FRAME;
600
601 s->coded_picture_number = 0;
602 s->picture_number = 0;
7976241a 603
4d9ec050
KT
604 s->f_code = 1;
605 s->b_code = 1;
6a9c8594 606
881a5e04 607 s->slice_context_count = 1;
3edcacde
MN
608}
609
610/**
58c42af7 611 * Set the given MpegEncContext to defaults for decoding.
4d9ec050
KT
612 * the changed fields will not depend upon
613 * the prior state of the MpegEncContext.
3edcacde 614 */
835f798c 615void ff_mpv_decode_defaults(MpegEncContext *s)
4d9ec050 616{
835f798c 617 ff_mpv_common_defaults(s);
3edcacde
MN
618}
619
620/**
1b3439b3
JG
621 * Initialize and allocates MpegEncContext fields dependent on the resolution.
622 */
623static int init_context_frame(MpegEncContext *s)
624{
625 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
626
627 s->mb_width = (s->width + 15) / 16;
628 s->mb_stride = s->mb_width + 1;
629 s->b8_stride = s->mb_width * 2 + 1;
1b3439b3
JG
630 mb_array_size = s->mb_height * s->mb_stride;
631 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
632
633 /* set default edge pos, will be overriden
634 * in decode_header if needed */
635 s->h_edge_pos = s->mb_width * 16;
636 s->v_edge_pos = s->mb_height * 16;
637
638 s->mb_num = s->mb_width * s->mb_height;
639
640 s->block_wrap[0] =
641 s->block_wrap[1] =
642 s->block_wrap[2] =
643 s->block_wrap[3] = s->b8_stride;
644 s->block_wrap[4] =
645 s->block_wrap[5] = s->mb_stride;
646
647 y_size = s->b8_stride * (2 * s->mb_height + 1);
648 c_size = s->mb_stride * (s->mb_height + 1);
649 yc_size = y_size + 2 * c_size;
650
651 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
652 fail); // error ressilience code looks cleaner with this
653 for (y = 0; y < s->mb_height; y++)
654 for (x = 0; x < s->mb_width; x++)
655 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
656
657 s->mb_index2xy[s->mb_height * s->mb_width] =
658 (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
659
660 if (s->encoding) {
661 /* Allocate MV tables */
662 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
663 mv_table_size * 2 * sizeof(int16_t), fail);
664 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
665 mv_table_size * 2 * sizeof(int16_t), fail);
666 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
667 mv_table_size * 2 * sizeof(int16_t), fail);
668 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
669 mv_table_size * 2 * sizeof(int16_t), fail);
670 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
671 mv_table_size * 2 * sizeof(int16_t), fail);
672 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
673 mv_table_size * 2 * sizeof(int16_t), fail);
674 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
675 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
676 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
677 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
678 s->mb_stride + 1;
679 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
680 s->mb_stride + 1;
681 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
682
683 /* Allocate MB type table */
684 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
685 sizeof(uint16_t), fail); // needed for encoding
686
687 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
688 sizeof(int), fail);
689
690 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
691 mb_array_size * sizeof(float), fail);
692 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
693 mb_array_size * sizeof(float), fail);
694
695 }
696
1b3439b3 697 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
7c6eb0a1 698 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
1b3439b3
JG
699 /* interlaced direct mode decoding tables */
700 for (i = 0; i < 2; i++) {
701 int j, k;
702 for (j = 0; j < 2; j++) {
703 for (k = 0; k < 2; k++) {
704 FF_ALLOCZ_OR_GOTO(s->avctx,
705 s->b_field_mv_table_base[i][j][k],
706 mv_table_size * 2 * sizeof(int16_t),
707 fail);
708 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
709 s->mb_stride + 1;
710 }
711 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
712 mb_array_size * 2 * sizeof(uint8_t), fail);
713 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
714 mv_table_size * 2 * sizeof(int16_t), fail);
715 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
716 + s->mb_stride + 1;
717 }
718 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
719 mb_array_size * 2 * sizeof(uint8_t), fail);
720 }
721 }
722 if (s->out_format == FMT_H263) {
723 /* cbp values */
724 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
725 s->coded_block = s->coded_block_base + s->b8_stride + 1;
726
727 /* cbp, ac_pred, pred_dir */
728 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
729 mb_array_size * sizeof(uint8_t), fail);
730 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
731 mb_array_size * sizeof(uint8_t), fail);
732 }
733
734 if (s->h263_pred || s->h263_plus || !s->encoding) {
735 /* dc values */
736 // MN: we need these for error resilience of intra-frames
737 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
738 yc_size * sizeof(int16_t), fail);
739 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
740 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
741 s->dc_val[2] = s->dc_val[1] + c_size;
742 for (i = 0; i < yc_size; i++)
743 s->dc_val_base[i] = 1024;
744 }
745
746 /* which mb is a intra block */
747 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
748 memset(s->mbintra_table, 1, mb_array_size);
749
750 /* init macroblock skip table */
751 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
752 // Note the + 1 is for a quicker mpeg4 slice_end detection
753
8b5007a3 754 return ff_mpeg_er_init(s);
1b3439b3
JG
755fail:
756 return AVERROR(ENOMEM);
757}
758
759/**
3edcacde
MN
760 * init common structure for both encoder and decoder.
761 * this assumes that some variables like width/height are already set
762 */
835f798c 763av_cold int ff_mpv_common_init(MpegEncContext *s)
defdfc9a 764{
7e76fc52 765 int i;
881a5e04
JG
766 int nb_slices = (HAVE_THREADS &&
767 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
768 s->avctx->thread_count : 1;
769
770 if (s->encoding && s->avctx->slices)
771 nb_slices = s->avctx->slices;
defdfc9a 772
36ef5369 773 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
0127b861 774 s->mb_height = (s->height + 31) / 32 * 2;
19cac8e3 775 else
1b661802 776 s->mb_height = (s->height + 15) / 16;
fdb52bcc 777
716d413c 778 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
4d9ec050 779 av_log(s->avctx, AV_LOG_ERROR,
716d413c 780 "decoding to AV_PIX_FMT_NONE is not supported.\n");
9cfc1b3a
IK
781 return -1;
782 }
783
881a5e04
JG
784 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
785 int max_slices;
786 if (s->mb_height)
787 max_slices = FFMIN(MAX_THREADS, s->mb_height);
788 else
789 max_slices = MAX_THREADS;
790 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
791 " reducing to %d\n", nb_slices, max_slices);
792 nb_slices = max_slices;
000a9c02
MN
793 }
794
4d9ec050
KT
795 if ((s->width || s->height) &&
796 av_image_check_size(s->width, s->height, 0, s->avctx))
0ecca7a4
MN
797 return -1;
798
998c9f15 799 dct_init(s);
eb4b3dd3 800
5f24fe82
MS
801 /* set chroma shifts */
802 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
803 &s->chroma_x_shift,
804 &s->chroma_y_shift);
eb4b3dd3 805
5f24fe82
MS
806 /* convert fourcc to upper case */
807 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
115329f1 808
4d9ec050 809 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
759001c5
AK
810 MAX_PICTURE_COUNT * sizeof(Picture), fail);
811 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
f6774f90 812 s->picture[i].f = av_frame_alloc();
813 if (!s->picture[i].f)
814 goto fail;
747069e2 815 }
759001c5
AK
816 memset(&s->next_picture, 0, sizeof(s->next_picture));
817 memset(&s->last_picture, 0, sizeof(s->last_picture));
818 memset(&s->current_picture, 0, sizeof(s->current_picture));
f6774f90 819 memset(&s->new_picture, 0, sizeof(s->new_picture));
820 s->next_picture.f = av_frame_alloc();
821 if (!s->next_picture.f)
822 goto fail;
823 s->last_picture.f = av_frame_alloc();
824 if (!s->last_picture.f)
825 goto fail;
826 s->current_picture.f = av_frame_alloc();
827 if (!s->current_picture.f)
828 goto fail;
829 s->new_picture.f = av_frame_alloc();
830 if (!s->new_picture.f)
831 goto fail;
b465449e 832
fb22c237 833 if (s->width && s->height) {
7e76fc52 834 if (init_context_frame(s))
1b3439b3 835 goto fail;
4d9ec050
KT
836
837 s->parse_context.state = -1;
fb22c237 838 }
d7425f59 839
de6d9b64 840 s->context_initialized = 1;
4d9ec050 841 s->thread_context[0] = s;
9c3d33d6 842
fb22c237 843 if (s->width && s->height) {
881a5e04
JG
844 if (nb_slices > 1) {
845 for (i = 1; i < nb_slices; i++) {
4d9ec050
KT
846 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
847 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
848 }
9c3d33d6 849
881a5e04 850 for (i = 0; i < nb_slices; i++) {
12b54a1f 851 if (init_duplicate_context(s->thread_context[i]) < 0)
4d9ec050
KT
852 goto fail;
853 s->thread_context[i]->start_mb_y =
881a5e04 854 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
4d9ec050 855 s->thread_context[i]->end_mb_y =
881a5e04 856 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
4d9ec050
KT
857 }
858 } else {
12b54a1f 859 if (init_duplicate_context(s) < 0)
d969e93a 860 goto fail;
4d9ec050
KT
861 s->start_mb_y = 0;
862 s->end_mb_y = s->mb_height;
d969e93a 863 }
881a5e04 864 s->slice_context_count = nb_slices;
fb22c237
RB
865 }
866
de6d9b64
FB
867 return 0;
868 fail:
835f798c 869 ff_mpv_common_end(s);
de6d9b64
FB
870 return -1;
871}
872
1b3439b3
JG
873/**
874 * Frees and resets MpegEncContext fields depending on the resolution.
875 * Is used during resolution changes to avoid a full reinitialization of the
876 * codec.
877 */
7a38987f 878static void free_context_frame(MpegEncContext *s)
de6d9b64 879{
bb198e19 880 int i, j, k;
de6d9b64 881
6000abfa 882 av_freep(&s->mb_type);
7bc9090a
MN
883 av_freep(&s->p_mv_table_base);
884 av_freep(&s->b_forw_mv_table_base);
885 av_freep(&s->b_back_mv_table_base);
886 av_freep(&s->b_bidir_forw_mv_table_base);
887 av_freep(&s->b_bidir_back_mv_table_base);
888 av_freep(&s->b_direct_mv_table_base);
4d9ec050
KT
889 s->p_mv_table = NULL;
890 s->b_forw_mv_table = NULL;
891 s->b_back_mv_table = NULL;
892 s->b_bidir_forw_mv_table = NULL;
893 s->b_bidir_back_mv_table = NULL;
894 s->b_direct_mv_table = NULL;
895 for (i = 0; i < 2; i++) {
896 for (j = 0; j < 2; j++) {
897 for (k = 0; k < 2; k++) {
bb198e19 898 av_freep(&s->b_field_mv_table_base[i][j][k]);
4d9ec050 899 s->b_field_mv_table[i][j][k] = NULL;
bb198e19
MN
900 }
901 av_freep(&s->b_field_select_table[i][j]);
902 av_freep(&s->p_field_mv_table_base[i][j]);
4d9ec050 903 s->p_field_mv_table[i][j] = NULL;
bb198e19
MN
904 }
905 av_freep(&s->p_field_select_table[i]);
906 }
115329f1 907
137c8468 908 av_freep(&s->dc_val_base);
137c8468 909 av_freep(&s->coded_block_base);
6000abfa 910 av_freep(&s->mbintra_table);
7f2fe444
MN
911 av_freep(&s->cbp_table);
912 av_freep(&s->pred_dir_table);
115329f1 913
6000abfa 914 av_freep(&s->mbskip_table);
0ecca7a4 915
54974c62
AK
916 av_freep(&s->er.error_status_table);
917 av_freep(&s->er.er_temp_buffer);
7bc9090a 918 av_freep(&s->mb_index2xy);
158c7f05 919 av_freep(&s->lambda_table);
1b3439b3
JG
920 av_freep(&s->cplx_tab);
921 av_freep(&s->bits_tab);
922
923 s->linesize = s->uvlinesize = 0;
1b3439b3
JG
924}
925
835f798c 926int ff_mpv_common_frame_size_change(MpegEncContext *s)
435c0b87
JG
927{
928 int i, err = 0;
929
930 if (s->slice_context_count > 1) {
931 for (i = 0; i < s->slice_context_count; i++) {
932 free_duplicate_context(s->thread_context[i]);
933 }
934 for (i = 1; i < s->slice_context_count; i++) {
935 av_freep(&s->thread_context[i]);
936 }
937 } else
938 free_duplicate_context(s);
939
7a38987f 940 free_context_frame(s);
435c0b87
JG
941
942 if (s->picture)
759001c5 943 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
435c0b87
JG
944 s->picture[i].needs_realloc = 1;
945 }
946
947 s->last_picture_ptr =
948 s->next_picture_ptr =
949 s->current_picture_ptr = NULL;
950
951 // init
952 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
953 s->mb_height = (s->height + 31) / 32 * 2;
19cac8e3 954 else
435c0b87
JG
955 s->mb_height = (s->height + 15) / 16;
956
957 if ((s->width || s->height) &&
894545cb
MN
958 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
959 goto fail;
435c0b87
JG
960
961 if ((err = init_context_frame(s)))
962 goto fail;
963
964 s->thread_context[0] = s;
965
966 if (s->width && s->height) {
967 int nb_slices = s->slice_context_count;
968 if (nb_slices > 1) {
969 for (i = 1; i < nb_slices; i++) {
970 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
971 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
972 }
973
974 for (i = 0; i < nb_slices; i++) {
894545cb 975 if ((err = init_duplicate_context(s->thread_context[i])) < 0)
435c0b87
JG
976 goto fail;
977 s->thread_context[i]->start_mb_y =
978 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
979 s->thread_context[i]->end_mb_y =
980 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
981 }
982 } else {
12b54a1f 983 if (init_duplicate_context(s) < 0)
435c0b87
JG
984 goto fail;
985 s->start_mb_y = 0;
986 s->end_mb_y = s->mb_height;
987 }
988 s->slice_context_count = nb_slices;
989 }
990
991 return 0;
992 fail:
835f798c 993 ff_mpv_common_end(s);
435c0b87
JG
994 return err;
995}
996
1b3439b3 997/* init common structure for both encoder and decoder */
835f798c 998void ff_mpv_common_end(MpegEncContext *s)
1b3439b3
JG
999{
1000 int i;
1001
1002 if (s->slice_context_count > 1) {
1003 for (i = 0; i < s->slice_context_count; i++) {
1004 free_duplicate_context(s->thread_context[i]);
1005 }
1006 for (i = 1; i < s->slice_context_count; i++) {
1007 av_freep(&s->thread_context[i]);
1008 }
1009 s->slice_context_count = 1;
1010 } else free_duplicate_context(s);
1011
1012 av_freep(&s->parse_context.buffer);
1013 s->parse_context.buffer_size = 0;
1014
1015 av_freep(&s->bitstream_buffer);
1016 s->allocated_bitstream_buffer_size = 0;
1017
759001c5
AK
1018 if (s->picture) {
1019 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
0b0a7a75 1020 ff_free_picture_tables(&s->picture[i]);
d5280455 1021 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
f6774f90 1022 av_frame_free(&s->picture[i].f);
9b4b6e09 1023 }
de6d9b64 1024 }
b465449e 1025 av_freep(&s->picture);
0b0a7a75 1026 ff_free_picture_tables(&s->last_picture);
d5280455 1027 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
f6774f90 1028 av_frame_free(&s->last_picture.f);
0b0a7a75 1029 ff_free_picture_tables(&s->current_picture);
d5280455 1030 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
f6774f90 1031 av_frame_free(&s->current_picture.f);
0b0a7a75 1032 ff_free_picture_tables(&s->next_picture);
d5280455 1033 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
f6774f90 1034 av_frame_free(&s->next_picture.f);
1035 ff_free_picture_tables(&s->new_picture);
d5280455 1036 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
f6774f90 1037 av_frame_free(&s->new_picture.f);
32c7589b
JG
1038
1039 free_context_frame(s);
1040
4d9ec050
KT
1041 s->context_initialized = 0;
1042 s->last_picture_ptr =
1043 s->next_picture_ptr =
1044 s->current_picture_ptr = NULL;
1045 s->linesize = s->uvlinesize = 0;
de6d9b64
FB
1046}
1047
5f194811 1048/**
aec25b1c
AK
1049 * generic function called after decoding
1050 * the header and before a frame is decoded.
5f194811 1051 */
835f798c 1052int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
de6d9b64 1053{
759001c5 1054 int i, ret;
357ec71f 1055 Picture *pic;
160d679c 1056 s->mb_skipped = 0;
0da71265 1057
c65dfac4 1058 /* mark & release old frames */
ee870491
AK
1059 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1060 s->last_picture_ptr != s->next_picture_ptr &&
f6774f90 1061 s->last_picture_ptr->f->buf[0]) {
d5280455 1062 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
ee870491 1063 }
c65dfac4 1064
ee870491
AK
1065 /* release forgotten pictures */
1066 /* if (mpeg124/h263) */
aec25b1c
AK
1067 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1068 if (&s->picture[i] != s->last_picture_ptr &&
1069 &s->picture[i] != s->next_picture_ptr &&
1070 s->picture[i].reference && !s->picture[i].needs_realloc) {
d5280455 1071 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
d6db1c9c 1072 }
ee870491 1073 }
d52b4abe 1074
d5280455 1075 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
4b796681 1076
c6aa0554
VG
1077 /* release non reference frames */
1078 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1079 if (!s->picture[i].reference)
1080 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1081 }
e20c4069 1082
f929ab05 1083 if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
aec25b1c
AK
1084 // we already have a unused image
1085 // (maybe it was set before reading the header)
1086 pic = s->current_picture_ptr;
1087 } else {
4e17946f 1088 i = ff_find_unused_picture(s->avctx, s->picture, 0);
aec25b1c
AK
1089 if (i < 0) {
1090 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1091 return i;
5f194811 1092 }
aec25b1c
AK
1093 pic = &s->picture[i];
1094 }
5f194811 1095
aec25b1c
AK
1096 pic->reference = 0;
1097 if (!s->droppable) {
1098 if (s->pict_type != AV_PICTURE_TYPE_B)
1099 pic->reference = 3;
1100 }
b536d0aa 1101
f6774f90 1102 pic->f->coded_picture_number = s->coded_picture_number++;
115329f1 1103
9bb11be0 1104 if (alloc_picture(s, pic, 0) < 0)
aec25b1c 1105 return -1;
93a21abd 1106
aec25b1c
AK
1107 s->current_picture_ptr = pic;
1108 // FIXME use only the vars from current_pic
f6774f90 1109 s->current_picture_ptr->f->top_field_first = s->top_field_first;
aec25b1c
AK
1110 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1111 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1112 if (s->picture_structure != PICT_FRAME)
f6774f90 1113 s->current_picture_ptr->f->top_field_first =
aec25b1c 1114 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1e491e29 1115 }
f6774f90 1116 s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
aec25b1c
AK
1117 !s->progressive_sequence;
1118 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
b7adc711 1119
f6774f90 1120 s->current_picture_ptr->f->pict_type = s->pict_type;
7c6eb0a1 1121 // if (s->avctx->flags && AV_CODEC_FLAG_QSCALE)
c65dfac4 1122 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
f6774f90 1123 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
9f2e61b6 1124
a3f4c930 1125 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
759001c5
AK
1126 s->current_picture_ptr)) < 0)
1127 return ret;
115329f1 1128
19cac8e3 1129 if (s->pict_type != AV_PICTURE_TYPE_B) {
c65dfac4 1130 s->last_picture_ptr = s->next_picture_ptr;
ba0c8981 1131 if (!s->droppable)
c65dfac4 1132 s->next_picture_ptr = s->current_picture_ptr;
de6d9b64 1133 }
6a85dfc8 1134 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1218777f 1135 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
f6774f90 1136 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
1137 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
1138 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
ba0c8981 1139 s->pict_type, s->droppable);
c65dfac4 1140
f929ab05 1141 if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
ee870491
AK
1142 (s->pict_type != AV_PICTURE_TYPE_I ||
1143 s->picture_structure != PICT_FRAME)) {
1144 int h_chroma_shift, v_chroma_shift;
1145 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1146 &h_chroma_shift, &v_chroma_shift);
1147 if (s->pict_type != AV_PICTURE_TYPE_I)
1148 av_log(avctx, AV_LOG_ERROR,
1149 "warning: first frame is no keyframe\n");
1150 else if (s->picture_structure != PICT_FRAME)
1151 av_log(avctx, AV_LOG_INFO,
1152 "allocate dummy last picture for field based first keyframe\n");
1153
1154 /* Allocate a dummy frame */
4e17946f 1155 i = ff_find_unused_picture(s->avctx, s->picture, 0);
ee870491
AK
1156 if (i < 0) {
1157 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1158 return i;
d52b4abe 1159 }
ee870491 1160 s->last_picture_ptr = &s->picture[i];
feded990
AK
1161
1162 s->last_picture_ptr->reference = 3;
f6774f90 1163 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
feded990 1164
9bb11be0 1165 if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
ee870491
AK
1166 s->last_picture_ptr = NULL;
1167 return -1;
d52b4abe 1168 }
d52b4abe 1169
f6774f90 1170 memset(s->last_picture_ptr->f->data[0], 0,
1171 avctx->height * s->last_picture_ptr->f->linesize[0]);
1172 memset(s->last_picture_ptr->f->data[1], 0x80,
ee870491 1173 (avctx->height >> v_chroma_shift) *
f6774f90 1174 s->last_picture_ptr->f->linesize[1]);
1175 memset(s->last_picture_ptr->f->data[2], 0x80,
ee870491 1176 (avctx->height >> v_chroma_shift) *
f6774f90 1177 s->last_picture_ptr->f->linesize[2]);
ee870491
AK
1178
1179 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1180 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1181 }
f929ab05 1182 if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
ee870491
AK
1183 s->pict_type == AV_PICTURE_TYPE_B) {
1184 /* Allocate a dummy frame */
4e17946f 1185 i = ff_find_unused_picture(s->avctx, s->picture, 0);
ee870491
AK
1186 if (i < 0) {
1187 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1188 return i;
759001c5 1189 }
ee870491 1190 s->next_picture_ptr = &s->picture[i];
feded990
AK
1191
1192 s->next_picture_ptr->reference = 3;
f6774f90 1193 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
feded990 1194
9bb11be0 1195 if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
ee870491
AK
1196 s->next_picture_ptr = NULL;
1197 return -1;
759001c5 1198 }
ee870491
AK
1199 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1200 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1201 }
1202
1203 if (s->last_picture_ptr) {
d5280455 1204 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
f6774f90 1205 if (s->last_picture_ptr->f->buf[0] &&
a3f4c930 1206 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
ee870491
AK
1207 s->last_picture_ptr)) < 0)
1208 return ret;
1209 }
1210 if (s->next_picture_ptr) {
d5280455 1211 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
f6774f90 1212 if (s->next_picture_ptr->f->buf[0] &&
a3f4c930 1213 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
ee870491
AK
1214 s->next_picture_ptr)) < 0)
1215 return ret;
1216 }
115329f1 1217
72072bf9 1218 if (s->pict_type != AV_PICTURE_TYPE_I &&
f6774f90 1219 !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
72072bf9
LB
1220 av_log(s, AV_LOG_ERROR,
1221 "Non-reference picture received and no reference available\n");
1222 return AVERROR_INVALIDDATA;
1223 }
3ab77000 1224
19cac8e3 1225 if (s->picture_structure!= PICT_FRAME) {
b536d0aa 1226 int i;
c65dfac4
KT
1227 for (i = 0; i < 4; i++) {
1228 if (s->picture_structure == PICT_BOTTOM_FIELD) {
f6774f90 1229 s->current_picture.f->data[i] +=
1230 s->current_picture.f->linesize[i];
115329f1 1231 }
f6774f90 1232 s->current_picture.f->linesize[i] *= 2;
1233 s->last_picture.f->linesize[i] *= 2;
1234 s->next_picture.f->linesize[i] *= 2;
b536d0aa
MN
1235 }
1236 }
115329f1 1237
c65dfac4
KT
1238 /* set dequantizer, we can't do it during init as
1239 * it might change for mpeg4 and we can't do it in the header
1240 * decode as init is not called for mpeg4 there yet */
36ef5369 1241 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
d50635cd
MN
1242 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1243 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
c65dfac4 1244 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
d50635cd
MN
1245 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1246 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
c65dfac4 1247 } else {
d50635cd
MN
1248 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1249 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1250 }
d6db1c9c 1251
19e30a58
DB
1252#if FF_API_XVMC
1253FF_DISABLE_DEPRECATION_WARNINGS
c65dfac4 1254 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
a002da79 1255 return ff_xvmc_field_start(s, avctx);
19e30a58
DB
1256FF_ENABLE_DEPRECATION_WARNINGS
1257#endif /* FF_API_XVMC */
83344066 1258
d6db1c9c 1259 return 0;
de6d9b64 1260}
21af69f7 1261
381a7225 1262/* called after a frame has been decoded. */
835f798c 1263void ff_mpv_frame_end(MpegEncContext *s)
de6d9b64 1264{
19e30a58
DB
1265#if FF_API_XVMC
1266FF_DISABLE_DEPRECATION_WARNINGS
6a9c8594 1267 /* redraw edges for the frame if decoding didn't complete */
c65dfac4
KT
1268 // just to make sure that all data is rendered.
1269 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
a002da79 1270 ff_xvmc_field_end(s);
19e30a58
DB
1271 } else
1272FF_ENABLE_DEPRECATION_WARNINGS
1273#endif /* FF_API_XVMC */
6a9c8594 1274
5975626d 1275 emms_c();
115329f1 1276
19cac8e3 1277 if (s->current_picture.reference)
759001c5 1278 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
de6d9b64
FB
1279}
1280
7bc9090a 1281/**
c65dfac4 1282 * Print debugging info for the given picture.
7bc9090a 1283 */
759001c5 1284void ff_print_debug_info(MpegEncContext *s, Picture *p)
c65dfac4 1285{
759001c5
AK
1286 AVFrame *pict;
1287 if (s->avctx->hwaccel || !p || !p->mb_type)
c65dfac4 1288 return;
f6774f90 1289 pict = p->f;
7bc9090a 1290
c65dfac4 1291 if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
7bc9090a 1292 int x,y;
115329f1 1293
0c9bbaec
WH
1294 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1295 switch (pict->pict_type) {
c65dfac4
KT
1296 case AV_PICTURE_TYPE_I:
1297 av_log(s->avctx,AV_LOG_DEBUG,"I\n");
1298 break;
1299 case AV_PICTURE_TYPE_P:
1300 av_log(s->avctx,AV_LOG_DEBUG,"P\n");
1301 break;
1302 case AV_PICTURE_TYPE_B:
1303 av_log(s->avctx,AV_LOG_DEBUG,"B\n");
1304 break;
1305 case AV_PICTURE_TYPE_S:
1306 av_log(s->avctx,AV_LOG_DEBUG,"S\n");
1307 break;
1308 case AV_PICTURE_TYPE_SI:
1309 av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
1310 break;
1311 case AV_PICTURE_TYPE_SP:
1312 av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
1313 break;
0c9bbaec 1314 }
c65dfac4
KT
1315 for (y = 0; y < s->mb_height; y++) {
1316 for (x = 0; x < s->mb_width; x++) {
1317 if (s->avctx->debug & FF_DEBUG_SKIP) {
1318 int count = s->mbskip_table[x + y * s->mb_stride];
1319 if (count > 9)
1320 count = 9;
9b879566 1321 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
7bc9090a 1322 }
c65dfac4
KT
1323 if (s->avctx->debug & FF_DEBUG_QP) {
1324 av_log(s->avctx, AV_LOG_DEBUG, "%2d",
759001c5 1325 p->qscale_table[x + y * s->mb_stride]);
7bc9090a 1326 }
c65dfac4 1327 if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
759001c5 1328 int mb_type = p->mb_type[x + y * s->mb_stride];
c65dfac4
KT
1329 // Type & MV direction
1330 if (IS_PCM(mb_type))
9b879566 1331 av_log(s->avctx, AV_LOG_DEBUG, "P");
c65dfac4 1332 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
9b879566 1333 av_log(s->avctx, AV_LOG_DEBUG, "A");
c65dfac4 1334 else if (IS_INTRA4x4(mb_type))
9b879566 1335 av_log(s->avctx, AV_LOG_DEBUG, "i");
c65dfac4 1336 else if (IS_INTRA16x16(mb_type))
9b879566 1337 av_log(s->avctx, AV_LOG_DEBUG, "I");
c65dfac4 1338 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
9b879566 1339 av_log(s->avctx, AV_LOG_DEBUG, "d");
c65dfac4 1340 else if (IS_DIRECT(mb_type))
9b879566 1341 av_log(s->avctx, AV_LOG_DEBUG, "D");
c65dfac4 1342 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
9b879566 1343 av_log(s->avctx, AV_LOG_DEBUG, "g");
c65dfac4 1344 else if (IS_GMC(mb_type))
9b879566 1345 av_log(s->avctx, AV_LOG_DEBUG, "G");
c65dfac4 1346 else if (IS_SKIP(mb_type))
9b879566 1347 av_log(s->avctx, AV_LOG_DEBUG, "S");
c65dfac4 1348 else if (!USES_LIST(mb_type, 1))
9b879566 1349 av_log(s->avctx, AV_LOG_DEBUG, ">");
c65dfac4 1350 else if (!USES_LIST(mb_type, 0))
9b879566 1351 av_log(s->avctx, AV_LOG_DEBUG, "<");
c65dfac4 1352 else {
7bc9090a 1353 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
9b879566 1354 av_log(s->avctx, AV_LOG_DEBUG, "X");
7bc9090a 1355 }
115329f1 1356
c65dfac4
KT
1357 // segmentation
1358 if (IS_8X8(mb_type))
9b879566 1359 av_log(s->avctx, AV_LOG_DEBUG, "+");
c65dfac4 1360 else if (IS_16X8(mb_type))
9b879566 1361 av_log(s->avctx, AV_LOG_DEBUG, "-");
c65dfac4 1362 else if (IS_8X16(mb_type))
30344a83 1363 av_log(s->avctx, AV_LOG_DEBUG, "|");
c65dfac4 1364 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
9b879566 1365 av_log(s->avctx, AV_LOG_DEBUG, " ");
7bc9090a 1366 else
9b879566 1367 av_log(s->avctx, AV_LOG_DEBUG, "?");
115329f1
DB
1368
1369
c65dfac4 1370 if (IS_INTERLACED(mb_type))
9b879566 1371 av_log(s->avctx, AV_LOG_DEBUG, "=");
7bc9090a 1372 else
9b879566 1373 av_log(s->avctx, AV_LOG_DEBUG, " ");
7bc9090a 1374 }
7bc9090a 1375 }
9b879566 1376 av_log(s->avctx, AV_LOG_DEBUG, "\n");
7bc9090a
MN
1377 }
1378 }
1379}
1380
6a9c8594
AS
1381/**
1382 * find the lowest MB row referenced in the MVs
1383 */
c0e6b8ca 1384static int lowest_referenced_row(MpegEncContext *s, int dir)
6a9c8594
AS
1385{
1386 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1387 int my, off, i, mvs;
1388
6bcdfe48
MN
1389 if (s->picture_structure != PICT_FRAME || s->mcsel)
1390 goto unhandled;
6a9c8594
AS
1391
1392 switch (s->mv_type) {
1393 case MV_TYPE_16X16:
1394 mvs = 1;
1395 break;
1396 case MV_TYPE_16X8:
1397 mvs = 2;
1398 break;
1399 case MV_TYPE_8X8:
1400 mvs = 4;
1401 break;
1402 default:
1403 goto unhandled;
1404 }
1405
1406 for (i = 0; i < mvs; i++) {
1407 my = s->mv[dir][i][1]<<qpel_shift;
1408 my_max = FFMAX(my_max, my);
1409 my_min = FFMIN(my_min, my);
1410 }
1411
1412 off = (FFMAX(-my_min, my_max) + 63) >> 6;
1413
1414 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
1415unhandled:
1416 return s->mb_height-1;
1417}
1418
3ada94ba
BF
1419/* put block[] to dest[] */
1420static inline void put_dct(MpegEncContext *s,
88bd7fdc 1421 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
178fcca8 1422{
3ada94ba 1423 s->dct_unquantize_intra(s, block, i, qscale);
e3fcb143 1424 s->idsp.idct_put(dest, line_size, block);
3ada94ba 1425}
da9c9637 1426
3ada94ba
BF
1427/* add block[] to dest[] */
1428static inline void add_dct(MpegEncContext *s,
88bd7fdc 1429 int16_t *block, int i, uint8_t *dest, int line_size)
3ada94ba
BF
1430{
1431 if (s->block_last_index[i] >= 0) {
e3fcb143 1432 s->idsp.idct_add(dest, line_size, block);
3ada94ba
BF
1433 }
1434}
2417652e 1435
115329f1 1436static inline void add_dequant_dct(MpegEncContext *s,
88bd7fdc 1437 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
0f440e02 1438{
de6d9b64 1439 if (s->block_last_index[i] >= 0) {
d50635cd 1440 s->dct_unquantize_inter(s, block, i, qscale);
9dbcbd92 1441
e3fcb143 1442 s->idsp.idct_add(dest, line_size, block);
de6d9b64
FB
1443 }
1444}
1445
7f2fe444 1446/**
58c42af7 1447 * Clean dc, ac, coded_block for the current non-intra MB.
7f2fe444
MN
1448 */
1449void ff_clean_intra_table_entries(MpegEncContext *s)
1450{
137c8468 1451 int wrap = s->b8_stride;
7f2fe444 1452 int xy = s->block_index[0];
115329f1
DB
1453
1454 s->dc_val[0][xy ] =
1455 s->dc_val[0][xy + 1 ] =
7f2fe444
MN
1456 s->dc_val[0][xy + wrap] =
1457 s->dc_val[0][xy + 1 + wrap] = 1024;
1458 /* ac pred */
0c1a9eda
ZK
1459 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
1460 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
7f2fe444
MN
1461 if (s->msmpeg4_version>=3) {
1462 s->coded_block[xy ] =
1463 s->coded_block[xy + 1 ] =
1464 s->coded_block[xy + wrap] =
1465 s->coded_block[xy + 1 + wrap] = 0;
1466 }
1467 /* chroma */
137c8468
MN
1468 wrap = s->mb_stride;
1469 xy = s->mb_x + s->mb_y * wrap;
7f2fe444
MN
1470 s->dc_val[1][xy] =
1471 s->dc_val[2][xy] = 1024;
1472 /* ac pred */
0c1a9eda
ZK
1473 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1474 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
115329f1 1475
137c8468 1476 s->mbintra_table[xy]= 0;
7f2fe444
MN
1477}
1478
de6d9b64
FB
1479/* generic function called after a macroblock has been parsed by the
1480 decoder or after it has been encoded by the encoder.
1481
1482 Important variables used:
1483 s->mb_intra : true if intra macroblock
1484 s->mv_dir : motion vector direction
1485 s->mv_type : motion vector type
1486 s->mv : motion vector
1487 s->interlaced_dct : true if interlaced dct used (mpeg2)
1488 */
54816a3e 1489static av_always_inline
835f798c 1490void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2bcbd984 1491 int is_mpeg12)
de6d9b64 1492{
7bc9090a 1493 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
19e30a58
DB
1494
1495#if FF_API_XVMC
1496FF_DISABLE_DEPRECATION_WARNINGS
83344066 1497 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
78f9a878 1498 ff_xvmc_decode_mb(s);//xvmc uses pblocks
2e7b4c84
IK
1499 return;
1500 }
19e30a58
DB
1501FF_ENABLE_DEPRECATION_WARNINGS
1502#endif /* FF_API_XVMC */
de6d9b64 1503
8289c6fa 1504 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
759001c5 1505 /* print DCT coefficients */
8289c6fa 1506 int i,j;
c4fb3b03
MN
1507 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1508 for(i=0; i<6; i++){
1509 for(j=0; j<64; j++){
e3fcb143
DB
1510 av_log(s->avctx, AV_LOG_DEBUG, "%5d",
1511 block[i][s->idsp.idct_permutation[j]]);
c4fb3b03
MN
1512 }
1513 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1514 }
8289c6fa
WH
1515 }
1516
759001c5 1517 s->current_picture.qscale_table[mb_xy] = s->qscale;
79e7b305 1518
de6d9b64
FB
1519 /* update DC predictors for P macroblocks */
1520 if (!s->mb_intra) {
bd7c626a 1521 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
0f440e02 1522 if(s->mbintra_table[mb_xy])
7f2fe444 1523 ff_clean_intra_table_entries(s);
de6d9b64 1524 } else {
7f2fe444
MN
1525 s->last_dc[0] =
1526 s->last_dc[1] =
de6d9b64
FB
1527 s->last_dc[2] = 128 << s->intra_dc_precision;
1528 }
1529 }
bd7c626a 1530 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
0f440e02 1531 s->mbintra_table[mb_xy]=1;
bff6ecaa 1532
7c6eb0a1 1533 if ((s->avctx->flags & AV_CODEC_FLAG_PSNR) ||
848e86f7
VG
1534 !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
1535 s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
0c1a9eda 1536 uint8_t *dest_y, *dest_cb, *dest_cr;
0f440e02 1537 int dct_linesize, dct_offset;
b3184779
MN
1538 op_pixels_func (*op_pix)[4];
1539 qpel_mc_func (*op_qpix)[16];
f6774f90 1540 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1541 const int uvlinesize = s->current_picture.f->linesize[1];
2bcbd984
MR
1542 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
1543 const int block_size = 8;
3bb4e23a 1544
1e491e29 1545 /* avoid copy if macroblock skipped in last frame too */
1e491e29
MN
1546 /* skip only during decoding as we might trash the buffers during encoding a bit */
1547 if(!s->encoding){
0c1a9eda 1548 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1e491e29 1549
160d679c
MM
1550 if (s->mb_skipped) {
1551 s->mb_skipped= 0;
975a1447 1552 assert(s->pict_type!=AV_PICTURE_TYPE_I);
8400b126 1553 *mbskip_ptr = 1;
759001c5 1554 } else if(!s->current_picture.reference) {
8400b126 1555 *mbskip_ptr = 1;
f943e138 1556 } else{
3bb4e23a
FB
1557 *mbskip_ptr = 0; /* not skipped */
1558 }
3994623d 1559 }
115329f1 1560
ffdff4d7 1561 dct_linesize = linesize << s->interlaced_dct;
3dc99a18 1562 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
115329f1 1563
b68ab260
MN
1564 if(readable){
1565 dest_y= s->dest[0];
1566 dest_cb= s->dest[1];
1567 dest_cr= s->dest[2];
1568 }else{
da0c8664
VG
1569 dest_y = s->sc.b_scratchpad;
1570 dest_cb= s->sc.b_scratchpad+16*linesize;
1571 dest_cr= s->sc.b_scratchpad+32*linesize;
b68ab260 1572 }
178fcca8 1573
de6d9b64
FB
1574 if (!s->mb_intra) {
1575 /* motion handling */
dfb706da 1576 /* decoding or more than one mb_type (MC was already done otherwise) */
7d1c3fc1 1577 if(!s->encoding){
6a9c8594 1578
27237d52 1579 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
6a9c8594 1580 if (s->mv_dir & MV_DIR_FORWARD) {
759001c5 1581 ff_thread_await_progress(&s->last_picture_ptr->tf,
c0e6b8ca 1582 lowest_referenced_row(s, 0),
47c0ac96 1583 0);
6a9c8594
AS
1584 }
1585 if (s->mv_dir & MV_DIR_BACKWARD) {
759001c5 1586 ff_thread_await_progress(&s->next_picture_ptr->tf,
c0e6b8ca 1587 lowest_referenced_row(s, 1),
47c0ac96 1588 0);
6a9c8594
AS
1589 }
1590 }
1591
2bcbd984
MR
1592 op_qpix= s->me.qpel_put;
1593 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
f4fed5a2 1594 op_pix = s->hdsp.put_pixels_tab;
178fcca8 1595 }else{
f4fed5a2 1596 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2bcbd984
MR
1597 }
1598 if (s->mv_dir & MV_DIR_FORWARD) {
835f798c 1599 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
f4fed5a2 1600 op_pix = s->hdsp.avg_pixels_tab;
2bcbd984
MR
1601 op_qpix= s->me.qpel_avg;
1602 }
1603 if (s->mv_dir & MV_DIR_BACKWARD) {
835f798c 1604 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
9dbcbd92 1605 }
de6d9b64
FB
1606 }
1607
0f440e02 1608 /* skip dequant / idct if we are really late ;) */
8c3eba7c 1609 if(s->avctx->skip_idct){
975a1447
SS
1610 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
1611 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
8c3eba7c
MN
1612 || s->avctx->skip_idct >= AVDISCARD_ALL)
1613 goto skip_idct;
1614 }
0f440e02 1615
de6d9b64 1616 /* add dct residue */
36ef5369
AK
1617 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
1618 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
178fcca8
MN
1619 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1620 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1621 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1622 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
0f440e02 1623
7c6eb0a1 1624 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2d974017
BC
1625 if (s->chroma_y_shift){
1626 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1627 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1628 }else{
1629 dct_linesize >>= 1;
1630 dct_offset >>=1;
1631 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1632 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1633 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1634 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1635 }
b50eef3a 1636 }
36ef5369 1637 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
178fcca8
MN
1638 add_dct(s, block[0], 0, dest_y , dct_linesize);
1639 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
1640 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
1641 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
de6d9b64 1642
7c6eb0a1 1643 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
ffdff4d7
IK
1644 if(s->chroma_y_shift){//Chroma420
1645 add_dct(s, block[4], 4, dest_cb, uvlinesize);
1646 add_dct(s, block[5], 5, dest_cr, uvlinesize);
1647 }else{
1648 //chroma422
1649 dct_linesize = uvlinesize << s->interlaced_dct;
3dc99a18 1650 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
ffdff4d7
IK
1651
1652 add_dct(s, block[4], 4, dest_cb, dct_linesize);
1653 add_dct(s, block[5], 5, dest_cr, dct_linesize);
1654 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
1655 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
1656 if(!s->chroma_x_shift){//Chroma444
1657 add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
1658 add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
1659 add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
1660 add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
1661 }
1662 }
1663 }//fi gray
1664 }
d702a2e6 1665 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
1457ab52 1666 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
0f440e02 1667 }
de6d9b64
FB
1668 } else {
1669 /* dct only in intra block */
36ef5369 1670 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
178fcca8
MN
1671 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1672 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1673 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1674 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
a0201736 1675
7c6eb0a1 1676 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2d974017
BC
1677 if(s->chroma_y_shift){
1678 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1679 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1680 }else{
1681 dct_offset >>=1;
1682 dct_linesize >>=1;
1683 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1684 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1685 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
3ada94ba 1686 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
477ab036
MN
1687 }
1688 }
1689 }else{
e3fcb143
DB
1690 s->idsp.idct_put(dest_y, dct_linesize, block[0]);
1691 s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
1692 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
1693 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
fbb89806 1694
7c6eb0a1 1695 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
3ada94ba 1696 if(s->chroma_y_shift){
e3fcb143
DB
1697 s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
1698 s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
3ada94ba 1699 }else{
fbb89806 1700
3ada94ba 1701 dct_linesize = uvlinesize << s->interlaced_dct;
3dc99a18 1702 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
9981dfc6 1703
e3fcb143
DB
1704 s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
1705 s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
1706 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
1707 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
3ada94ba 1708 if(!s->chroma_x_shift){//Chroma444
e3fcb143
DB
1709 s->idsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
1710 s->idsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
1711 s->idsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
1712 s->idsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
3ada94ba
BF
1713 }
1714 }
1715 }//gray
9981dfc6 1716 }
477ab036 1717 }
3ada94ba
BF
1718skip_idct:
1719 if(!readable){
f4fed5a2
RB
1720 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
1721 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
1722 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
34f60ee6 1723 }
477ab036 1724 }
477ab036
MN
1725}
1726
835f798c
DB
1727void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
1728{
b250f9c6 1729#if !CONFIG_SMALL
bd7c626a 1730 if(s->out_format == FMT_MPEG1) {
835f798c 1731 mpv_decode_mb_internal(s, block, 1);
bd7c626a
KC
1732 } else
1733#endif
835f798c 1734 mpv_decode_mb_internal(s, block, 0);
77ea0d4b
MN
1735}
1736
1d0feb5d
AK
1737void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
1738{
f6774f90 1739 ff_draw_horiz_band(s->avctx, s->current_picture.f,
1740 s->last_picture.f, y, h, s->picture_structure,
54b2ce74 1741 s->first_field, s->low_delay);
1d0feb5d
AK
1742}
1743
3ada94ba 1744void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
f6774f90 1745 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1746 const int uvlinesize = s->current_picture.f->linesize[1];
2bcbd984 1747 const int mb_size= 4;
77ea0d4b 1748
3ada94ba
BF
1749 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
1750 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
1751 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
1752 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
1753 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
1754 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;
1755 //block_index is not used by mpeg2, so it is not affected by chroma_format
115329f1 1756
7d4a1ff3
LB
1757 s->dest[0] = s->current_picture.f->data[0] + (s->mb_x - 1) * (1 << mb_size);
1758 s->dest[1] = s->current_picture.f->data[1] + (s->mb_x - 1) * (1 << (mb_size - s->chroma_x_shift));
1759 s->dest[2] = s->current_picture.f->data[2] + (s->mb_x - 1) * (1 << (mb_size - s->chroma_x_shift));
115329f1 1760
975a1447 1761 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
3ada94ba 1762 {
078cdecf 1763 if(s->picture_structure==PICT_FRAME){
3ada94ba
BF
1764 s->dest[0] += s->mb_y * linesize << mb_size;
1765 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
1766 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
078cdecf
MN
1767 }else{
1768 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
1769 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
1770 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
1771 assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
1772 }
77ea0d4b 1773 }
77ea0d4b 1774}
115329f1 1775
3ada94ba
BF
1776void ff_mpeg_flush(AVCodecContext *avctx){
1777 int i;
1778 MpegEncContext *s = avctx->priv_data;
77ea0d4b 1779
f929ab05 1780 if (!s || !s->picture)
3ada94ba 1781 return;
77ea0d4b 1782
759001c5 1783 for (i = 0; i < MAX_PICTURE_COUNT; i++)
d5280455 1784 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
3ada94ba 1785 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
115329f1 1786
d5280455
VG
1787 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1788 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1789 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
feec9349 1790
3ada94ba 1791 s->mb_x= s->mb_y= 0;
7801d21d 1792
3ada94ba
BF
1793 s->parse_context.state= -1;
1794 s->parse_context.frame_start_found= 0;
1795 s->parse_context.overread= 0;
1796 s->parse_context.overread_index= 0;
1797 s->parse_context.index= 0;
1798 s->parse_context.last_index= 0;
1799 s->bitstream_buffer_size=0;
1800 s->pp_time=0;
de6d9b64
FB
1801}
1802
b776e3d1
AJ
1803/**
1804 * set qscale and update qscale dependent variables.
1805 */
1806void ff_set_qscale(MpegEncContext * s, int qscale)
1807{
1808 if (qscale < 1)
1809 qscale = 1;
1810 else if (qscale > 31)
1811 qscale = 31;
1812
1813 s->qscale = qscale;
1814 s->chroma_qscale= s->chroma_qscale_table[qscale];
1815
1816 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
1817 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
1818}
6a9c8594 1819
835f798c 1820void ff_mpv_report_decode_progress(MpegEncContext *s)
6a9c8594 1821{
54974c62 1822 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
759001c5 1823 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
6a9c8594 1824}