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