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