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