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