]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo.c
Merge commit 'ab3fdaa7131e2fd8e33006daea25a282724c01f0'
[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 FFmpeg.
9  *
10  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "h264chroma.h"
38 #include "internal.h"
39 #include "mathops.h"
40 #include "mpegutils.h"
41 #include "mpegvideo.h"
42 #include "mjpegenc.h"
43 #include "msmpeg4.h"
44 #include "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     block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
117     /* XXX: only mpeg1 */
118     quant_matrix = s->intra_matrix;
119     for(i=1;i<=nCoeffs;i++) {
120         int j= s->intra_scantable.permutated[i];
121         level = block[j];
122         if (level) {
123             if (level < 0) {
124                 level = -level;
125                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
126                 level = (level - 1) | 1;
127                 level = -level;
128             } else {
129                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
130                 level = (level - 1) | 1;
131             }
132             block[j] = level;
133         }
134     }
135 }
136
137 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
138                                    int16_t *block, int n, int qscale)
139 {
140     int i, level, nCoeffs;
141     const uint16_t *quant_matrix;
142
143     nCoeffs= s->block_last_index[n];
144
145     quant_matrix = s->inter_matrix;
146     for(i=0; i<=nCoeffs; i++) {
147         int j= s->intra_scantable.permutated[i];
148         level = block[j];
149         if (level) {
150             if (level < 0) {
151                 level = -level;
152                 level = (((level << 1) + 1) * qscale *
153                          ((int) (quant_matrix[j]))) >> 4;
154                 level = (level - 1) | 1;
155                 level = -level;
156             } else {
157                 level = (((level << 1) + 1) * qscale *
158                          ((int) (quant_matrix[j]))) >> 4;
159                 level = (level - 1) | 1;
160             }
161             block[j] = level;
162         }
163     }
164 }
165
166 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
167                                    int16_t *block, int n, int qscale)
168 {
169     int i, level, nCoeffs;
170     const uint16_t *quant_matrix;
171
172     if(s->alternate_scan) nCoeffs= 63;
173     else nCoeffs= s->block_last_index[n];
174
175     block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
176     quant_matrix = s->intra_matrix;
177     for(i=1;i<=nCoeffs;i++) {
178         int j= s->intra_scantable.permutated[i];
179         level = block[j];
180         if (level) {
181             if (level < 0) {
182                 level = -level;
183                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
184                 level = -level;
185             } else {
186                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
187             }
188             block[j] = level;
189         }
190     }
191 }
192
193 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
194                                    int16_t *block, int n, int qscale)
195 {
196     int i, level, nCoeffs;
197     const uint16_t *quant_matrix;
198     int sum=-1;
199
200     if(s->alternate_scan) nCoeffs= 63;
201     else nCoeffs= s->block_last_index[n];
202
203     block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
204     sum += block[0];
205     quant_matrix = s->intra_matrix;
206     for(i=1;i<=nCoeffs;i++) {
207         int j= s->intra_scantable.permutated[i];
208         level = block[j];
209         if (level) {
210             if (level < 0) {
211                 level = -level;
212                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
213                 level = -level;
214             } else {
215                 level = (int)(level * qscale * quant_matrix[j]) >> 3;
216             }
217             block[j] = level;
218             sum+=level;
219         }
220     }
221     block[63]^=sum&1;
222 }
223
224 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
225                                    int16_t *block, int n, int qscale)
226 {
227     int i, level, nCoeffs;
228     const uint16_t *quant_matrix;
229     int sum=-1;
230
231     if(s->alternate_scan) nCoeffs= 63;
232     else nCoeffs= s->block_last_index[n];
233
234     quant_matrix = s->inter_matrix;
235     for(i=0; i<=nCoeffs; i++) {
236         int j= s->intra_scantable.permutated[i];
237         level = block[j];
238         if (level) {
239             if (level < 0) {
240                 level = -level;
241                 level = (((level << 1) + 1) * qscale *
242                          ((int) (quant_matrix[j]))) >> 4;
243                 level = -level;
244             } else {
245                 level = (((level << 1) + 1) * qscale *
246                          ((int) (quant_matrix[j]))) >> 4;
247             }
248             block[j] = level;
249             sum+=level;
250         }
251     }
252     block[63]^=sum&1;
253 }
254
255 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
256                                   int16_t *block, int n, int qscale)
257 {
258     int i, level, qmul, qadd;
259     int nCoeffs;
260
261     av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
262
263     qmul = qscale << 1;
264
265     if (!s->h263_aic) {
266         block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
267         qadd = (qscale - 1) | 1;
268     }else{
269         qadd = 0;
270     }
271     if(s->ac_pred)
272         nCoeffs=63;
273     else
274         nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
275
276     for(i=1; i<=nCoeffs; i++) {
277         level = block[i];
278         if (level) {
279             if (level < 0) {
280                 level = level * qmul - qadd;
281             } else {
282                 level = level * qmul + qadd;
283             }
284             block[i] = level;
285         }
286     }
287 }
288
289 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
290                                   int16_t *block, int n, int qscale)
291 {
292     int i, level, qmul, qadd;
293     int nCoeffs;
294
295     av_assert2(s->block_last_index[n]>=0);
296
297     qadd = (qscale - 1) | 1;
298     qmul = qscale << 1;
299
300     nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
301
302     for(i=0; i<=nCoeffs; i++) {
303         level = block[i];
304         if (level) {
305             if (level < 0) {
306                 level = level * qmul - qadd;
307             } else {
308                 level = level * qmul + qadd;
309             }
310             block[i] = level;
311         }
312     }
313 }
314
315 static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
316                               int (*mv)[2][4][2],
317                               int mb_x, int mb_y, int mb_intra, int mb_skipped)
318 {
319     MpegEncContext *s = opaque;
320
321     s->mv_dir     = mv_dir;
322     s->mv_type    = mv_type;
323     s->mb_intra   = mb_intra;
324     s->mb_skipped = mb_skipped;
325     s->mb_x       = mb_x;
326     s->mb_y       = mb_y;
327     memcpy(s->mv, mv, sizeof(*mv));
328
329     ff_init_block_index(s);
330     ff_update_block_index(s);
331
332     s->dsp.clear_blocks(s->block[0]);
333
334     s->dest[0] = s->current_picture.f->data[0] + (s->mb_y *  16                       * s->linesize)   + s->mb_x *  16;
335     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);
336     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);
337
338     if (ref)
339         av_log(s->avctx, AV_LOG_DEBUG, "Interlaced error concealment is not fully implemented\n");
340     ff_MPV_decode_mb(s, s->block);
341 }
342
343 static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
344 {
345     while(h--)
346         memset(dst + h*linesize, 128, 16);
347 }
348
349 static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
350 {
351     while(h--)
352         memset(dst + h*linesize, 128, 8);
353 }
354
355 /* init common dct for both encoder and decoder */
356 av_cold int ff_dct_common_init(MpegEncContext *s)
357 {
358     ff_dsputil_init(&s->dsp, s->avctx);
359     ff_h264chroma_init(&s->h264chroma, 8); //for lowres
360     ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
361     ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
362
363     if (s->avctx->debug & FF_DEBUG_NOMC) {
364         int i;
365         for (i=0; i<4; i++) {
366             s->hdsp.avg_pixels_tab[0][i] = gray16;
367             s->hdsp.put_pixels_tab[0][i] = gray16;
368             s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
369
370             s->hdsp.avg_pixels_tab[1][i] = gray8;
371             s->hdsp.put_pixels_tab[1][i] = gray8;
372             s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
373         }
374     }
375
376     s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
377     s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
378     s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
379     s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
380     s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
381     if (s->flags & CODEC_FLAG_BITEXACT)
382         s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
383     s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
384
385     if (ARCH_ALPHA)
386         ff_MPV_common_init_axp(s);
387     if (ARCH_ARM)
388         ff_MPV_common_init_arm(s);
389     if (ARCH_PPC)
390         ff_MPV_common_init_ppc(s);
391     if (ARCH_X86)
392         ff_MPV_common_init_x86(s);
393
394     /* load & permutate scantables
395      * note: only wmv uses different ones
396      */
397     if (s->alternate_scan) {
398         ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_alternate_vertical_scan);
399         ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_alternate_vertical_scan);
400     } else {
401         ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , ff_zigzag_direct);
402         ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , ff_zigzag_direct);
403     }
404     ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
405     ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
406
407     return 0;
408 }
409
410 static int frame_size_alloc(MpegEncContext *s, int linesize)
411 {
412     int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
413
414     // edge emu needs blocksize + filter length - 1
415     // (= 17x17 for  halfpel / 21x21 for  h264)
416     // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
417     // at uvlinesize. It supports only YUV420 so 24x24 is enough
418     // linesize * interlaced * MBsize
419     FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
420                       fail);
421
422     FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
423                       fail)
424     s->me.temp         = s->me.scratchpad;
425     s->rd_scratchpad   = s->me.scratchpad;
426     s->b_scratchpad    = s->me.scratchpad;
427     s->obmc_scratchpad = s->me.scratchpad + 16;
428
429     return 0;
430 fail:
431     av_freep(&s->edge_emu_buffer);
432     return AVERROR(ENOMEM);
433 }
434
435 /**
436  * Allocate a frame buffer
437  */
438 static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
439 {
440     int edges_needed = av_codec_is_encoder(s->avctx->codec);
441     int r, ret;
442
443     pic->tf.f = pic->f;
444     if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
445         s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
446         s->codec_id != AV_CODEC_ID_MSS2) {
447         if (edges_needed) {
448             pic->f->width  = s->avctx->width  + 2 * EDGE_WIDTH;
449             pic->f->height = s->avctx->height + 2 * EDGE_WIDTH;
450         }
451
452         r = ff_thread_get_buffer(s->avctx, &pic->tf,
453                                  pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
454     } else {
455         pic->f->width  = s->avctx->width;
456         pic->f->height = s->avctx->height;
457         pic->f->format = s->avctx->pix_fmt;
458         r = avcodec_default_get_buffer2(s->avctx, pic->f, 0);
459     }
460
461     if (r < 0 || !pic->f->buf[0]) {
462         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
463                r, pic->f->data[0]);
464         return -1;
465     }
466
467     if (edges_needed) {
468         int i;
469         for (i = 0; pic->f->data[i]; i++) {
470             int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
471                          pic->f->linesize[i] +
472                          (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
473             pic->f->data[i] += offset;
474         }
475         pic->f->width  = s->avctx->width;
476         pic->f->height = s->avctx->height;
477     }
478
479     if (s->avctx->hwaccel) {
480         assert(!pic->hwaccel_picture_private);
481         if (s->avctx->hwaccel->frame_priv_data_size) {
482             pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->frame_priv_data_size);
483             if (!pic->hwaccel_priv_buf) {
484                 av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
485                 return -1;
486             }
487             pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
488         }
489     }
490
491     if (s->linesize && (s->linesize   != pic->f->linesize[0] ||
492                         s->uvlinesize != pic->f->linesize[1])) {
493         av_log(s->avctx, AV_LOG_ERROR,
494                "get_buffer() failed (stride changed)\n");
495         ff_mpeg_unref_picture(s, pic);
496         return -1;
497     }
498
499     if (pic->f->linesize[1] != pic->f->linesize[2]) {
500         av_log(s->avctx, AV_LOG_ERROR,
501                "get_buffer() failed (uv stride mismatch)\n");
502         ff_mpeg_unref_picture(s, pic);
503         return -1;
504     }
505
506     if (!s->edge_emu_buffer &&
507         (ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) {
508         av_log(s->avctx, AV_LOG_ERROR,
509                "get_buffer() failed to allocate context scratch buffers.\n");
510         ff_mpeg_unref_picture(s, pic);
511         return ret;
512     }
513
514     return 0;
515 }
516
517 void ff_free_picture_tables(Picture *pic)
518 {
519     int i;
520
521     pic->alloc_mb_width  =
522     pic->alloc_mb_height = 0;
523
524     av_buffer_unref(&pic->mb_var_buf);
525     av_buffer_unref(&pic->mc_mb_var_buf);
526     av_buffer_unref(&pic->mb_mean_buf);
527     av_buffer_unref(&pic->mbskip_table_buf);
528     av_buffer_unref(&pic->qscale_table_buf);
529     av_buffer_unref(&pic->mb_type_buf);
530
531     for (i = 0; i < 2; i++) {
532         av_buffer_unref(&pic->motion_val_buf[i]);
533         av_buffer_unref(&pic->ref_index_buf[i]);
534     }
535 }
536
537 static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
538 {
539     const int big_mb_num    = s->mb_stride * (s->mb_height + 1) + 1;
540     const int mb_array_size = s->mb_stride * s->mb_height;
541     const int b8_array_size = s->b8_stride * s->mb_height * 2;
542     int i;
543
544
545     pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
546     pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
547     pic->mb_type_buf      = av_buffer_allocz((big_mb_num + s->mb_stride) *
548                                              sizeof(uint32_t));
549     if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
550         return AVERROR(ENOMEM);
551
552     if (s->encoding) {
553         pic->mb_var_buf    = av_buffer_allocz(mb_array_size * sizeof(int16_t));
554         pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
555         pic->mb_mean_buf   = av_buffer_allocz(mb_array_size);
556         if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
557             return AVERROR(ENOMEM);
558     }
559
560     if (s->out_format == FMT_H263 || s->encoding || s->avctx->debug_mv) {
561         int mv_size        = 2 * (b8_array_size + 4) * sizeof(int16_t);
562         int ref_index_size = 4 * mb_array_size;
563
564         for (i = 0; mv_size && i < 2; i++) {
565             pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
566             pic->ref_index_buf[i]  = av_buffer_allocz(ref_index_size);
567             if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
568                 return AVERROR(ENOMEM);
569         }
570     }
571
572     pic->alloc_mb_width  = s->mb_width;
573     pic->alloc_mb_height = s->mb_height;
574
575     return 0;
576 }
577
578 static int make_tables_writable(Picture *pic)
579 {
580     int ret, i;
581 #define MAKE_WRITABLE(table) \
582 do {\
583     if (pic->table &&\
584        (ret = av_buffer_make_writable(&pic->table)) < 0)\
585     return ret;\
586 } while (0)
587
588     MAKE_WRITABLE(mb_var_buf);
589     MAKE_WRITABLE(mc_mb_var_buf);
590     MAKE_WRITABLE(mb_mean_buf);
591     MAKE_WRITABLE(mbskip_table_buf);
592     MAKE_WRITABLE(qscale_table_buf);
593     MAKE_WRITABLE(mb_type_buf);
594
595     for (i = 0; i < 2; i++) {
596         MAKE_WRITABLE(motion_val_buf[i]);
597         MAKE_WRITABLE(ref_index_buf[i]);
598     }
599
600     return 0;
601 }
602
603 /**
604  * Allocate a Picture.
605  * The pixels are allocated/set by calling get_buffer() if shared = 0
606  */
607 int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
608 {
609     int i, ret;
610
611     if (pic->qscale_table_buf)
612         if (   pic->alloc_mb_width  != s->mb_width
613             || pic->alloc_mb_height != s->mb_height)
614             ff_free_picture_tables(pic);
615
616     if (shared) {
617         av_assert0(pic->f->data[0]);
618         pic->shared = 1;
619     } else {
620         av_assert0(!pic->f->buf[0]);
621
622         if (alloc_frame_buffer(s, pic) < 0)
623             return -1;
624
625         s->linesize   = pic->f->linesize[0];
626         s->uvlinesize = pic->f->linesize[1];
627     }
628
629     if (!pic->qscale_table_buf)
630         ret = alloc_picture_tables(s, pic);
631     else
632         ret = make_tables_writable(pic);
633     if (ret < 0)
634         goto fail;
635
636     if (s->encoding) {
637         pic->mb_var    = (uint16_t*)pic->mb_var_buf->data;
638         pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
639         pic->mb_mean   = pic->mb_mean_buf->data;
640     }
641
642     pic->mbskip_table = pic->mbskip_table_buf->data;
643     pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
644     pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
645
646     if (pic->motion_val_buf[0]) {
647         for (i = 0; i < 2; i++) {
648             pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
649             pic->ref_index[i]  = pic->ref_index_buf[i]->data;
650         }
651     }
652
653     return 0;
654 fail:
655     av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
656     ff_mpeg_unref_picture(s, pic);
657     ff_free_picture_tables(pic);
658     return AVERROR(ENOMEM);
659 }
660
661 /**
662  * Deallocate a picture.
663  */
664 void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
665 {
666     int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
667
668     pic->tf.f = pic->f;
669     /* WM Image / Screen codecs allocate internal buffers with different
670      * dimensions / colorspaces; ignore user-defined callbacks for these. */
671     if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
672         s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
673         s->codec_id != AV_CODEC_ID_MSS2)
674         ff_thread_release_buffer(s->avctx, &pic->tf);
675     else if (pic->f)
676         av_frame_unref(pic->f);
677
678     av_buffer_unref(&pic->hwaccel_priv_buf);
679
680     if (pic->needs_realloc)
681         ff_free_picture_tables(pic);
682
683     memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
684 }
685
686 static int update_picture_tables(Picture *dst, Picture *src)
687 {
688      int i;
689
690 #define UPDATE_TABLE(table)\
691 do {\
692     if (src->table &&\
693         (!dst->table || dst->table->buffer != src->table->buffer)) {\
694         av_buffer_unref(&dst->table);\
695         dst->table = av_buffer_ref(src->table);\
696         if (!dst->table) {\
697             ff_free_picture_tables(dst);\
698             return AVERROR(ENOMEM);\
699         }\
700     }\
701 } while (0)
702
703     UPDATE_TABLE(mb_var_buf);
704     UPDATE_TABLE(mc_mb_var_buf);
705     UPDATE_TABLE(mb_mean_buf);
706     UPDATE_TABLE(mbskip_table_buf);
707     UPDATE_TABLE(qscale_table_buf);
708     UPDATE_TABLE(mb_type_buf);
709     for (i = 0; i < 2; i++) {
710         UPDATE_TABLE(motion_val_buf[i]);
711         UPDATE_TABLE(ref_index_buf[i]);
712     }
713
714     dst->mb_var        = src->mb_var;
715     dst->mc_mb_var     = src->mc_mb_var;
716     dst->mb_mean       = src->mb_mean;
717     dst->mbskip_table  = src->mbskip_table;
718     dst->qscale_table  = src->qscale_table;
719     dst->mb_type       = src->mb_type;
720     for (i = 0; i < 2; i++) {
721         dst->motion_val[i] = src->motion_val[i];
722         dst->ref_index[i]  = src->ref_index[i];
723     }
724
725     dst->alloc_mb_width  = src->alloc_mb_width;
726     dst->alloc_mb_height = src->alloc_mb_height;
727
728     return 0;
729 }
730
731 int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
732 {
733     int ret;
734
735     av_assert0(!dst->f->buf[0]);
736     av_assert0(src->f->buf[0]);
737
738     src->tf.f = src->f;
739     dst->tf.f = dst->f;
740     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
741     if (ret < 0)
742         goto fail;
743
744     ret = update_picture_tables(dst, src);
745     if (ret < 0)
746         goto fail;
747
748     if (src->hwaccel_picture_private) {
749         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
750         if (!dst->hwaccel_priv_buf)
751             goto fail;
752         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
753     }
754
755     dst->field_picture           = src->field_picture;
756     dst->mb_var_sum              = src->mb_var_sum;
757     dst->mc_mb_var_sum           = src->mc_mb_var_sum;
758     dst->b_frame_score           = src->b_frame_score;
759     dst->needs_realloc           = src->needs_realloc;
760     dst->reference               = src->reference;
761     dst->shared                  = src->shared;
762
763     return 0;
764 fail:
765     ff_mpeg_unref_picture(s, dst);
766     return ret;
767 }
768
769 static void exchange_uv(MpegEncContext *s)
770 {
771     int16_t (*tmp)[64];
772
773     tmp           = s->pblocks[4];
774     s->pblocks[4] = s->pblocks[5];
775     s->pblocks[5] = tmp;
776 }
777
778 static int init_duplicate_context(MpegEncContext *s)
779 {
780     int y_size = s->b8_stride * (2 * s->mb_height + 1);
781     int c_size = s->mb_stride * (s->mb_height + 1);
782     int yc_size = y_size + 2 * c_size;
783     int i;
784
785     if (s->mb_height & 1)
786         yc_size += 2*s->b8_stride + 2*s->mb_stride;
787
788     s->edge_emu_buffer =
789     s->me.scratchpad   =
790     s->me.temp         =
791     s->rd_scratchpad   =
792     s->b_scratchpad    =
793     s->obmc_scratchpad = NULL;
794
795     if (s->encoding) {
796         FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
797                           ME_MAP_SIZE * sizeof(uint32_t), fail)
798         FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
799                           ME_MAP_SIZE * sizeof(uint32_t), fail)
800         if (s->avctx->noise_reduction) {
801             FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
802                               2 * 64 * sizeof(int), fail)
803         }
804     }
805     FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
806     s->block = s->blocks[0];
807
808     for (i = 0; i < 12; i++) {
809         s->pblocks[i] = &s->block[i];
810     }
811     if (s->avctx->codec_tag == AV_RL32("VCR2"))
812         exchange_uv(s);
813
814     if (s->out_format == FMT_H263) {
815         /* ac values */
816         FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
817                           yc_size * sizeof(int16_t) * 16, fail);
818         s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
819         s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
820         s->ac_val[2] = s->ac_val[1] + c_size;
821     }
822
823     return 0;
824 fail:
825     return -1; // free() through ff_MPV_common_end()
826 }
827
828 static void free_duplicate_context(MpegEncContext *s)
829 {
830     if (s == NULL)
831         return;
832
833     av_freep(&s->edge_emu_buffer);
834     av_freep(&s->me.scratchpad);
835     s->me.temp =
836     s->rd_scratchpad =
837     s->b_scratchpad =
838     s->obmc_scratchpad = NULL;
839
840     av_freep(&s->dct_error_sum);
841     av_freep(&s->me.map);
842     av_freep(&s->me.score_map);
843     av_freep(&s->blocks);
844     av_freep(&s->ac_val_base);
845     s->block = NULL;
846 }
847
848 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
849 {
850 #define COPY(a) bak->a = src->a
851     COPY(edge_emu_buffer);
852     COPY(me.scratchpad);
853     COPY(me.temp);
854     COPY(rd_scratchpad);
855     COPY(b_scratchpad);
856     COPY(obmc_scratchpad);
857     COPY(me.map);
858     COPY(me.score_map);
859     COPY(blocks);
860     COPY(block);
861     COPY(start_mb_y);
862     COPY(end_mb_y);
863     COPY(me.map_generation);
864     COPY(pb);
865     COPY(dct_error_sum);
866     COPY(dct_count[0]);
867     COPY(dct_count[1]);
868     COPY(ac_val_base);
869     COPY(ac_val[0]);
870     COPY(ac_val[1]);
871     COPY(ac_val[2]);
872 #undef COPY
873 }
874
875 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
876 {
877     MpegEncContext bak;
878     int i, ret;
879     // FIXME copy only needed parts
880     // START_TIMER
881     backup_duplicate_context(&bak, dst);
882     memcpy(dst, src, sizeof(MpegEncContext));
883     backup_duplicate_context(dst, &bak);
884     for (i = 0; i < 12; i++) {
885         dst->pblocks[i] = &dst->block[i];
886     }
887     if (dst->avctx->codec_tag == AV_RL32("VCR2"))
888         exchange_uv(dst);
889     if (!dst->edge_emu_buffer &&
890         (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
891         av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
892                "scratch buffers.\n");
893         return ret;
894     }
895     // STOP_TIMER("update_duplicate_context")
896     // about 10k cycles / 0.01 sec for  1000frames on 1ghz with 2 threads
897     return 0;
898 }
899
900 int ff_mpeg_update_thread_context(AVCodecContext *dst,
901                                   const AVCodecContext *src)
902 {
903     int i, ret;
904     MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
905
906     if (dst == src)
907         return 0;
908
909     av_assert0(s != s1);
910
911     // FIXME can parameters change on I-frames?
912     // in that case dst may need a reinit
913     if (!s->context_initialized) {
914         memcpy(s, s1, sizeof(MpegEncContext));
915
916         s->avctx                 = dst;
917         s->bitstream_buffer      = NULL;
918         s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
919
920         if (s1->context_initialized){
921 //             s->picture_range_start  += MAX_PICTURE_COUNT;
922 //             s->picture_range_end    += MAX_PICTURE_COUNT;
923             if((ret = ff_MPV_common_init(s)) < 0){
924                 memset(s, 0, sizeof(MpegEncContext));
925                 s->avctx = dst;
926                 return ret;
927             }
928         }
929     }
930
931     if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
932         s->context_reinit = 0;
933         s->height = s1->height;
934         s->width  = s1->width;
935         if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
936             return ret;
937     }
938
939     s->avctx->coded_height  = s1->avctx->coded_height;
940     s->avctx->coded_width   = s1->avctx->coded_width;
941     s->avctx->width         = s1->avctx->width;
942     s->avctx->height        = s1->avctx->height;
943
944     s->coded_picture_number = s1->coded_picture_number;
945     s->picture_number       = s1->picture_number;
946
947     av_assert0(!s->picture || s->picture != s1->picture);
948     if(s->picture)
949     for (i = 0; i < MAX_PICTURE_COUNT; i++) {
950         ff_mpeg_unref_picture(s, &s->picture[i]);
951         if (s1->picture[i].f->buf[0] &&
952             (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
953             return ret;
954     }
955
956 #define UPDATE_PICTURE(pic)\
957 do {\
958     ff_mpeg_unref_picture(s, &s->pic);\
959     if (s1->pic.f && s1->pic.f->buf[0])\
960         ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
961     else\
962         ret = update_picture_tables(&s->pic, &s1->pic);\
963     if (ret < 0)\
964         return ret;\
965 } while (0)
966
967     UPDATE_PICTURE(current_picture);
968     UPDATE_PICTURE(last_picture);
969     UPDATE_PICTURE(next_picture);
970
971     s->last_picture_ptr    = REBASE_PICTURE(s1->last_picture_ptr,    s, s1);
972     s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
973     s->next_picture_ptr    = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
974
975     // Error/bug resilience
976     s->next_p_frame_damaged = s1->next_p_frame_damaged;
977     s->workaround_bugs      = s1->workaround_bugs;
978     s->padding_bug_score    = s1->padding_bug_score;
979
980     // MPEG4 timing info
981     memcpy(&s->last_time_base, &s1->last_time_base,
982            (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
983            (char *) &s1->last_time_base);
984
985     // B-frame info
986     s->max_b_frames = s1->max_b_frames;
987     s->low_delay    = s1->low_delay;
988     s->droppable    = s1->droppable;
989
990     // DivX handling (doesn't work)
991     s->divx_packed  = s1->divx_packed;
992
993     if (s1->bitstream_buffer) {
994         if (s1->bitstream_buffer_size +
995             FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
996             av_fast_malloc(&s->bitstream_buffer,
997                            &s->allocated_bitstream_buffer_size,
998                            s1->allocated_bitstream_buffer_size);
999             s->bitstream_buffer_size = s1->bitstream_buffer_size;
1000         memcpy(s->bitstream_buffer, s1->bitstream_buffer,
1001                s1->bitstream_buffer_size);
1002         memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
1003                FF_INPUT_BUFFER_PADDING_SIZE);
1004     }
1005
1006     // linesize dependend scratch buffer allocation
1007     if (!s->edge_emu_buffer)
1008         if (s1->linesize) {
1009             if (frame_size_alloc(s, s1->linesize) < 0) {
1010                 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
1011                        "scratch buffers.\n");
1012                 return AVERROR(ENOMEM);
1013             }
1014         } else {
1015             av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
1016                    "be allocated due to unknown size.\n");
1017         }
1018
1019     // MPEG2/interlacing info
1020     memcpy(&s->progressive_sequence, &s1->progressive_sequence,
1021            (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
1022
1023     if (!s1->first_field) {
1024         s->last_pict_type = s1->pict_type;
1025         if (s1->current_picture_ptr)
1026             s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
1027     }
1028
1029     return 0;
1030 }
1031
1032 /**
1033  * Set the given MpegEncContext to common defaults
1034  * (same for encoding and decoding).
1035  * The changed fields will not depend upon the
1036  * prior state of the MpegEncContext.
1037  */
1038 void ff_MPV_common_defaults(MpegEncContext *s)
1039 {
1040     s->y_dc_scale_table      =
1041     s->c_dc_scale_table      = ff_mpeg1_dc_scale_table;
1042     s->chroma_qscale_table   = ff_default_chroma_qscale_table;
1043     s->progressive_frame     = 1;
1044     s->progressive_sequence  = 1;
1045     s->picture_structure     = PICT_FRAME;
1046
1047     s->coded_picture_number  = 0;
1048     s->picture_number        = 0;
1049
1050     s->f_code                = 1;
1051     s->b_code                = 1;
1052
1053     s->slice_context_count   = 1;
1054 }
1055
1056 /**
1057  * Set the given MpegEncContext to defaults for decoding.
1058  * the changed fields will not depend upon
1059  * the prior state of the MpegEncContext.
1060  */
1061 void ff_MPV_decode_defaults(MpegEncContext *s)
1062 {
1063     ff_MPV_common_defaults(s);
1064 }
1065
1066 static int init_er(MpegEncContext *s)
1067 {
1068     ERContext *er = &s->er;
1069     int mb_array_size = s->mb_height * s->mb_stride;
1070     int i;
1071
1072     er->avctx       = s->avctx;
1073     er->dsp         = &s->dsp;
1074
1075     er->mb_index2xy = s->mb_index2xy;
1076     er->mb_num      = s->mb_num;
1077     er->mb_width    = s->mb_width;
1078     er->mb_height   = s->mb_height;
1079     er->mb_stride   = s->mb_stride;
1080     er->b8_stride   = s->b8_stride;
1081
1082     er->er_temp_buffer     = av_malloc(s->mb_height * s->mb_stride);
1083     er->error_status_table = av_mallocz(mb_array_size);
1084     if (!er->er_temp_buffer || !er->error_status_table)
1085         goto fail;
1086
1087     er->mbskip_table  = s->mbskip_table;
1088     er->mbintra_table = s->mbintra_table;
1089
1090     for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
1091         er->dc_val[i] = s->dc_val[i];
1092
1093     er->decode_mb = mpeg_er_decode_mb;
1094     er->opaque    = s;
1095
1096     return 0;
1097 fail:
1098     av_freep(&er->er_temp_buffer);
1099     av_freep(&er->error_status_table);
1100     return AVERROR(ENOMEM);
1101 }
1102
1103 /**
1104  * Initialize and allocates MpegEncContext fields dependent on the resolution.
1105  */
1106 static int init_context_frame(MpegEncContext *s)
1107 {
1108     int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
1109
1110     s->mb_width   = (s->width + 15) / 16;
1111     s->mb_stride  = s->mb_width + 1;
1112     s->b8_stride  = s->mb_width * 2 + 1;
1113     mb_array_size = s->mb_height * s->mb_stride;
1114     mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
1115
1116     /* set default edge pos, will be overridden
1117      * in decode_header if needed */
1118     s->h_edge_pos = s->mb_width * 16;
1119     s->v_edge_pos = s->mb_height * 16;
1120
1121     s->mb_num     = s->mb_width * s->mb_height;
1122
1123     s->block_wrap[0] =
1124     s->block_wrap[1] =
1125     s->block_wrap[2] =
1126     s->block_wrap[3] = s->b8_stride;
1127     s->block_wrap[4] =
1128     s->block_wrap[5] = s->mb_stride;
1129
1130     y_size  = s->b8_stride * (2 * s->mb_height + 1);
1131     c_size  = s->mb_stride * (s->mb_height + 1);
1132     yc_size = y_size + 2   * c_size;
1133
1134     if (s->mb_height & 1)
1135         yc_size += 2*s->b8_stride + 2*s->mb_stride;
1136
1137     FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
1138     for (y = 0; y < s->mb_height; y++)
1139         for (x = 0; x < s->mb_width; x++)
1140             s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
1141
1142     s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
1143
1144     if (s->encoding) {
1145         /* Allocate MV tables */
1146         FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,                 mv_table_size * 2 * sizeof(int16_t), fail)
1147         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), fail)
1148         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), fail)
1149         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), fail)
1150         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), fail)
1151         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,          mv_table_size * 2 * sizeof(int16_t), fail)
1152         s->p_mv_table            = s->p_mv_table_base + s->mb_stride + 1;
1153         s->b_forw_mv_table       = s->b_forw_mv_table_base + s->mb_stride + 1;
1154         s->b_back_mv_table       = s->b_back_mv_table_base + s->mb_stride + 1;
1155         s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
1156         s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
1157         s->b_direct_mv_table     = s->b_direct_mv_table_base + s->mb_stride + 1;
1158
1159         /* Allocate MB type table */
1160         FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
1161
1162         FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
1163
1164         FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
1165                          mb_array_size * sizeof(float), fail);
1166         FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
1167                          mb_array_size * sizeof(float), fail);
1168
1169     }
1170
1171     if (s->codec_id == AV_CODEC_ID_MPEG4 ||
1172         (s->flags & CODEC_FLAG_INTERLACED_ME)) {
1173         /* interlaced direct mode decoding tables */
1174         for (i = 0; i < 2; i++) {
1175             int j, k;
1176             for (j = 0; j < 2; j++) {
1177                 for (k = 0; k < 2; k++) {
1178                     FF_ALLOCZ_OR_GOTO(s->avctx,
1179                                       s->b_field_mv_table_base[i][j][k],
1180                                       mv_table_size * 2 * sizeof(int16_t),
1181                                       fail);
1182                     s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
1183                                                    s->mb_stride + 1;
1184                 }
1185                 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
1186                 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
1187                 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
1188             }
1189             FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
1190         }
1191     }
1192     if (s->out_format == FMT_H263) {
1193         /* cbp values */
1194         FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
1195         s->coded_block = s->coded_block_base + s->b8_stride + 1;
1196
1197         /* cbp, ac_pred, pred_dir */
1198         FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table     , mb_array_size * sizeof(uint8_t), fail);
1199         FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
1200     }
1201
1202     if (s->h263_pred || s->h263_plus || !s->encoding) {
1203         /* dc values */
1204         // MN: we need these for  error resilience of intra-frames
1205         FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
1206         s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
1207         s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
1208         s->dc_val[2] = s->dc_val[1] + c_size;
1209         for (i = 0; i < yc_size; i++)
1210             s->dc_val_base[i] = 1024;
1211     }
1212
1213     /* which mb is a intra block */
1214     FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
1215     memset(s->mbintra_table, 1, mb_array_size);
1216
1217     /* init macroblock skip table */
1218     FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
1219     // Note the + 1 is for  a quicker mpeg4 slice_end detection
1220
1221     return init_er(s);
1222 fail:
1223     return AVERROR(ENOMEM);
1224 }
1225
1226 /**
1227  * init common structure for both encoder and decoder.
1228  * this assumes that some variables like width/height are already set
1229  */
1230 av_cold int ff_MPV_common_init(MpegEncContext *s)
1231 {
1232     int i;
1233     int nb_slices = (HAVE_THREADS &&
1234                      s->avctx->active_thread_type & FF_THREAD_SLICE) ?
1235                     s->avctx->thread_count : 1;
1236
1237     if (s->encoding && s->avctx->slices)
1238         nb_slices = s->avctx->slices;
1239
1240     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1241         s->mb_height = (s->height + 31) / 32 * 2;
1242     else
1243         s->mb_height = (s->height + 15) / 16;
1244
1245     if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
1246         av_log(s->avctx, AV_LOG_ERROR,
1247                "decoding to AV_PIX_FMT_NONE is not supported.\n");
1248         return -1;
1249     }
1250
1251     if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
1252         int max_slices;
1253         if (s->mb_height)
1254             max_slices = FFMIN(MAX_THREADS, s->mb_height);
1255         else
1256             max_slices = MAX_THREADS;
1257         av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
1258                " reducing to %d\n", nb_slices, max_slices);
1259         nb_slices = max_slices;
1260     }
1261
1262     if ((s->width || s->height) &&
1263         av_image_check_size(s->width, s->height, 0, s->avctx))
1264         return -1;
1265
1266     ff_dct_common_init(s);
1267
1268     s->flags  = s->avctx->flags;
1269     s->flags2 = s->avctx->flags2;
1270
1271     /* set chroma shifts */
1272     avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
1273                                   &s->chroma_x_shift,
1274                                   &s->chroma_y_shift);
1275
1276     /* convert fourcc to upper case */
1277     s->codec_tag          = avpriv_toupper4(s->avctx->codec_tag);
1278
1279     s->stream_codec_tag   = avpriv_toupper4(s->avctx->stream_codec_tag);
1280
1281     FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
1282                       MAX_PICTURE_COUNT * sizeof(Picture), fail);
1283     for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1284         s->picture[i].f = av_frame_alloc();
1285         if (!s->picture[i].f)
1286             goto fail;
1287     }
1288     memset(&s->next_picture, 0, sizeof(s->next_picture));
1289     memset(&s->last_picture, 0, sizeof(s->last_picture));
1290     memset(&s->current_picture, 0, sizeof(s->current_picture));
1291     memset(&s->new_picture, 0, sizeof(s->new_picture));
1292     s->next_picture.f = av_frame_alloc();
1293     if (!s->next_picture.f)
1294         goto fail;
1295     s->last_picture.f = av_frame_alloc();
1296     if (!s->last_picture.f)
1297         goto fail;
1298     s->current_picture.f = av_frame_alloc();
1299     if (!s->current_picture.f)
1300         goto fail;
1301     s->new_picture.f = av_frame_alloc();
1302     if (!s->new_picture.f)
1303         goto fail;
1304
1305         if (init_context_frame(s))
1306             goto fail;
1307
1308         s->parse_context.state = -1;
1309
1310         s->context_initialized = 1;
1311         s->thread_context[0]   = s;
1312
1313 //     if (s->width && s->height) {
1314         if (nb_slices > 1) {
1315             for (i = 1; i < nb_slices; i++) {
1316                 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1317                 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1318             }
1319
1320             for (i = 0; i < nb_slices; i++) {
1321                 if (init_duplicate_context(s->thread_context[i]) < 0)
1322                     goto fail;
1323                     s->thread_context[i]->start_mb_y =
1324                         (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1325                     s->thread_context[i]->end_mb_y   =
1326                         (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1327             }
1328         } else {
1329             if (init_duplicate_context(s) < 0)
1330                 goto fail;
1331             s->start_mb_y = 0;
1332             s->end_mb_y   = s->mb_height;
1333         }
1334         s->slice_context_count = nb_slices;
1335 //     }
1336
1337     return 0;
1338  fail:
1339     ff_MPV_common_end(s);
1340     return -1;
1341 }
1342
1343 /**
1344  * Frees and resets MpegEncContext fields depending on the resolution.
1345  * Is used during resolution changes to avoid a full reinitialization of the
1346  * codec.
1347  */
1348 static int free_context_frame(MpegEncContext *s)
1349 {
1350     int i, j, k;
1351
1352     av_freep(&s->mb_type);
1353     av_freep(&s->p_mv_table_base);
1354     av_freep(&s->b_forw_mv_table_base);
1355     av_freep(&s->b_back_mv_table_base);
1356     av_freep(&s->b_bidir_forw_mv_table_base);
1357     av_freep(&s->b_bidir_back_mv_table_base);
1358     av_freep(&s->b_direct_mv_table_base);
1359     s->p_mv_table            = NULL;
1360     s->b_forw_mv_table       = NULL;
1361     s->b_back_mv_table       = NULL;
1362     s->b_bidir_forw_mv_table = NULL;
1363     s->b_bidir_back_mv_table = NULL;
1364     s->b_direct_mv_table     = NULL;
1365     for (i = 0; i < 2; i++) {
1366         for (j = 0; j < 2; j++) {
1367             for (k = 0; k < 2; k++) {
1368                 av_freep(&s->b_field_mv_table_base[i][j][k]);
1369                 s->b_field_mv_table[i][j][k] = NULL;
1370             }
1371             av_freep(&s->b_field_select_table[i][j]);
1372             av_freep(&s->p_field_mv_table_base[i][j]);
1373             s->p_field_mv_table[i][j] = NULL;
1374         }
1375         av_freep(&s->p_field_select_table[i]);
1376     }
1377
1378     av_freep(&s->dc_val_base);
1379     av_freep(&s->coded_block_base);
1380     av_freep(&s->mbintra_table);
1381     av_freep(&s->cbp_table);
1382     av_freep(&s->pred_dir_table);
1383
1384     av_freep(&s->mbskip_table);
1385
1386     av_freep(&s->er.error_status_table);
1387     av_freep(&s->er.er_temp_buffer);
1388     av_freep(&s->mb_index2xy);
1389     av_freep(&s->lambda_table);
1390
1391     av_freep(&s->cplx_tab);
1392     av_freep(&s->bits_tab);
1393
1394     s->linesize = s->uvlinesize = 0;
1395
1396     return 0;
1397 }
1398
1399 int ff_MPV_common_frame_size_change(MpegEncContext *s)
1400 {
1401     int i, err = 0;
1402
1403     if (s->slice_context_count > 1) {
1404         for (i = 0; i < s->slice_context_count; i++) {
1405             free_duplicate_context(s->thread_context[i]);
1406         }
1407         for (i = 1; i < s->slice_context_count; i++) {
1408             av_freep(&s->thread_context[i]);
1409         }
1410     } else
1411         free_duplicate_context(s);
1412
1413     if ((err = free_context_frame(s)) < 0)
1414         return err;
1415
1416     if (s->picture)
1417         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1418                 s->picture[i].needs_realloc = 1;
1419         }
1420
1421     s->last_picture_ptr         =
1422     s->next_picture_ptr         =
1423     s->current_picture_ptr      = NULL;
1424
1425     // init
1426     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1427         s->mb_height = (s->height + 31) / 32 * 2;
1428     else
1429         s->mb_height = (s->height + 15) / 16;
1430
1431     if ((s->width || s->height) &&
1432         av_image_check_size(s->width, s->height, 0, s->avctx))
1433         return AVERROR_INVALIDDATA;
1434
1435     if ((err = init_context_frame(s)))
1436         goto fail;
1437
1438     s->thread_context[0]   = s;
1439
1440     if (s->width && s->height) {
1441         int nb_slices = s->slice_context_count;
1442         if (nb_slices > 1) {
1443             for (i = 1; i < nb_slices; i++) {
1444                 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1445                 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1446             }
1447
1448             for (i = 0; i < nb_slices; i++) {
1449                 if (init_duplicate_context(s->thread_context[i]) < 0)
1450                     goto fail;
1451                     s->thread_context[i]->start_mb_y =
1452                         (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1453                     s->thread_context[i]->end_mb_y   =
1454                         (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1455             }
1456         } else {
1457             err = init_duplicate_context(s);
1458             if (err < 0)
1459                 goto fail;
1460             s->start_mb_y = 0;
1461             s->end_mb_y   = s->mb_height;
1462         }
1463         s->slice_context_count = nb_slices;
1464     }
1465
1466     return 0;
1467  fail:
1468     ff_MPV_common_end(s);
1469     return err;
1470 }
1471
1472 /* init common structure for both encoder and decoder */
1473 void ff_MPV_common_end(MpegEncContext *s)
1474 {
1475     int i;
1476
1477     if (s->slice_context_count > 1) {
1478         for (i = 0; i < s->slice_context_count; i++) {
1479             free_duplicate_context(s->thread_context[i]);
1480         }
1481         for (i = 1; i < s->slice_context_count; i++) {
1482             av_freep(&s->thread_context[i]);
1483         }
1484         s->slice_context_count = 1;
1485     } else free_duplicate_context(s);
1486
1487     av_freep(&s->parse_context.buffer);
1488     s->parse_context.buffer_size = 0;
1489
1490     av_freep(&s->bitstream_buffer);
1491     s->allocated_bitstream_buffer_size = 0;
1492
1493     if (s->picture) {
1494         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1495             ff_free_picture_tables(&s->picture[i]);
1496             ff_mpeg_unref_picture(s, &s->picture[i]);
1497             av_frame_free(&s->picture[i].f);
1498         }
1499     }
1500     av_freep(&s->picture);
1501     ff_free_picture_tables(&s->last_picture);
1502     ff_mpeg_unref_picture(s, &s->last_picture);
1503     av_frame_free(&s->last_picture.f);
1504     ff_free_picture_tables(&s->current_picture);
1505     ff_mpeg_unref_picture(s, &s->current_picture);
1506     av_frame_free(&s->current_picture.f);
1507     ff_free_picture_tables(&s->next_picture);
1508     ff_mpeg_unref_picture(s, &s->next_picture);
1509     av_frame_free(&s->next_picture.f);
1510     ff_free_picture_tables(&s->new_picture);
1511     ff_mpeg_unref_picture(s, &s->new_picture);
1512     av_frame_free(&s->new_picture.f);
1513
1514     free_context_frame(s);
1515
1516     s->context_initialized      = 0;
1517     s->last_picture_ptr         =
1518     s->next_picture_ptr         =
1519     s->current_picture_ptr      = NULL;
1520     s->linesize = s->uvlinesize = 0;
1521 }
1522
1523 av_cold void ff_init_rl(RLTable *rl,
1524                         uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1525 {
1526     int8_t  max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
1527     uint8_t index_run[MAX_RUN + 1];
1528     int last, run, level, start, end, i;
1529
1530     /* If table is static, we can quit if rl->max_level[0] is not NULL */
1531     if (static_store && rl->max_level[0])
1532         return;
1533
1534     /* compute max_level[], max_run[] and index_run[] */
1535     for (last = 0; last < 2; last++) {
1536         if (last == 0) {
1537             start = 0;
1538             end = rl->last;
1539         } else {
1540             start = rl->last;
1541             end = rl->n;
1542         }
1543
1544         memset(max_level, 0, MAX_RUN + 1);
1545         memset(max_run, 0, MAX_LEVEL + 1);
1546         memset(index_run, rl->n, MAX_RUN + 1);
1547         for (i = start; i < end; i++) {
1548             run   = rl->table_run[i];
1549             level = rl->table_level[i];
1550             if (index_run[run] == rl->n)
1551                 index_run[run] = i;
1552             if (level > max_level[run])
1553                 max_level[run] = level;
1554             if (run > max_run[level])
1555                 max_run[level] = run;
1556         }
1557         if (static_store)
1558             rl->max_level[last] = static_store[last];
1559         else
1560             rl->max_level[last] = av_malloc(MAX_RUN + 1);
1561         memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1562         if (static_store)
1563             rl->max_run[last]   = static_store[last] + MAX_RUN + 1;
1564         else
1565             rl->max_run[last]   = av_malloc(MAX_LEVEL + 1);
1566         memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1567         if (static_store)
1568             rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
1569         else
1570             rl->index_run[last] = av_malloc(MAX_RUN + 1);
1571         memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1572     }
1573 }
1574
1575 av_cold void ff_init_vlc_rl(RLTable *rl)
1576 {
1577     int i, q;
1578
1579     for (q = 0; q < 32; q++) {
1580         int qmul = q * 2;
1581         int qadd = (q - 1) | 1;
1582
1583         if (q == 0) {
1584             qmul = 1;
1585             qadd = 0;
1586         }
1587         for (i = 0; i < rl->vlc.table_size; i++) {
1588             int code = rl->vlc.table[i][0];
1589             int len  = rl->vlc.table[i][1];
1590             int level, run;
1591
1592             if (len == 0) { // illegal code
1593                 run   = 66;
1594                 level = MAX_LEVEL;
1595             } else if (len < 0) { // more bits needed
1596                 run   = 0;
1597                 level = code;
1598             } else {
1599                 if (code == rl->n) { // esc
1600                     run   = 66;
1601                     level =  0;
1602                 } else {
1603                     run   = rl->table_run[code] + 1;
1604                     level = rl->table_level[code] * qmul + qadd;
1605                     if (code >= rl->last) run += 192;
1606                 }
1607             }
1608             rl->rl_vlc[q][i].len   = len;
1609             rl->rl_vlc[q][i].level = level;
1610             rl->rl_vlc[q][i].run   = run;
1611         }
1612     }
1613 }
1614
1615 static void release_unused_pictures(MpegEncContext *s)
1616 {
1617     int i;
1618
1619     /* release non reference frames */
1620     for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1621         if (!s->picture[i].reference)
1622             ff_mpeg_unref_picture(s, &s->picture[i]);
1623     }
1624 }
1625
1626 static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
1627 {
1628     if (pic == s->last_picture_ptr)
1629         return 0;
1630     if (pic->f->buf[0] == NULL)
1631         return 1;
1632     if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
1633         return 1;
1634     return 0;
1635 }
1636
1637 static int find_unused_picture(MpegEncContext *s, int shared)
1638 {
1639     int i;
1640
1641     if (shared) {
1642         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1643             if (s->picture[i].f->buf[0] == NULL && &s->picture[i] != s->last_picture_ptr)
1644                 return i;
1645         }
1646     } else {
1647         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1648             if (pic_is_unused(s, &s->picture[i]))
1649                 return i;
1650         }
1651     }
1652
1653     av_log(s->avctx, AV_LOG_FATAL,
1654            "Internal error, picture buffer overflow\n");
1655     /* We could return -1, but the codec would crash trying to draw into a
1656      * non-existing frame anyway. This is safer than waiting for a random crash.
1657      * Also the return of this is never useful, an encoder must only allocate
1658      * as much as allowed in the specification. This has no relationship to how
1659      * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
1660      * enough for such valid streams).
1661      * Plus, a decoder has to check stream validity and remove frames if too
1662      * many reference frames are around. Waiting for "OOM" is not correct at
1663      * all. Similarly, missing reference frames have to be replaced by
1664      * interpolated/MC frames, anything else is a bug in the codec ...
1665      */
1666     abort();
1667     return -1;
1668 }
1669
1670 int ff_find_unused_picture(MpegEncContext *s, int shared)
1671 {
1672     int ret = find_unused_picture(s, shared);
1673
1674     if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
1675         if (s->picture[ret].needs_realloc) {
1676             s->picture[ret].needs_realloc = 0;
1677             ff_free_picture_tables(&s->picture[ret]);
1678             ff_mpeg_unref_picture(s, &s->picture[ret]);
1679         }
1680     }
1681     return ret;
1682 }
1683
1684 static void gray_frame(AVFrame *frame)
1685 {
1686     int i, h_chroma_shift, v_chroma_shift;
1687
1688     av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
1689
1690     for(i=0; i<frame->height; i++)
1691         memset(frame->data[0] + frame->linesize[0]*i, 0x80, frame->width);
1692     for(i=0; i<FF_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
1693         memset(frame->data[1] + frame->linesize[1]*i,
1694                0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
1695         memset(frame->data[2] + frame->linesize[2]*i,
1696                0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
1697     }
1698 }
1699
1700 /**
1701  * generic function called after decoding
1702  * the header and before a frame is decoded.
1703  */
1704 int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1705 {
1706     int i, ret;
1707     Picture *pic;
1708     s->mb_skipped = 0;
1709
1710     if (!ff_thread_can_start_frame(avctx)) {
1711         av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
1712         return -1;
1713     }
1714
1715     /* mark & release old frames */
1716     if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1717         s->last_picture_ptr != s->next_picture_ptr &&
1718         s->last_picture_ptr->f->buf[0]) {
1719         ff_mpeg_unref_picture(s, s->last_picture_ptr);
1720     }
1721
1722     /* release forgotten pictures */
1723     /* if (mpeg124/h263) */
1724     for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1725         if (&s->picture[i] != s->last_picture_ptr &&
1726             &s->picture[i] != s->next_picture_ptr &&
1727             s->picture[i].reference && !s->picture[i].needs_realloc) {
1728             if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1729                 av_log(avctx, AV_LOG_ERROR,
1730                        "releasing zombie picture\n");
1731             ff_mpeg_unref_picture(s, &s->picture[i]);
1732         }
1733     }
1734
1735     ff_mpeg_unref_picture(s, &s->current_picture);
1736
1737     release_unused_pictures(s);
1738
1739     if (s->current_picture_ptr &&
1740         s->current_picture_ptr->f->buf[0] == NULL) {
1741         // we already have a unused image
1742         // (maybe it was set before reading the header)
1743         pic = s->current_picture_ptr;
1744     } else {
1745         i   = ff_find_unused_picture(s, 0);
1746         if (i < 0) {
1747             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1748             return i;
1749         }
1750         pic = &s->picture[i];
1751     }
1752
1753     pic->reference = 0;
1754     if (!s->droppable) {
1755         if (s->pict_type != AV_PICTURE_TYPE_B)
1756             pic->reference = 3;
1757     }
1758
1759     pic->f->coded_picture_number = s->coded_picture_number++;
1760
1761     if (ff_alloc_picture(s, pic, 0) < 0)
1762         return -1;
1763
1764     s->current_picture_ptr = pic;
1765     // FIXME use only the vars from current_pic
1766     s->current_picture_ptr->f->top_field_first = s->top_field_first;
1767     if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1768         s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1769         if (s->picture_structure != PICT_FRAME)
1770             s->current_picture_ptr->f->top_field_first =
1771                 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1772     }
1773     s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
1774                                                  !s->progressive_sequence;
1775     s->current_picture_ptr->field_picture      =  s->picture_structure != PICT_FRAME;
1776
1777     s->current_picture_ptr->f->pict_type = s->pict_type;
1778     // if (s->flags && CODEC_FLAG_QSCALE)
1779     //     s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1780     s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1781
1782     if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1783                                    s->current_picture_ptr)) < 0)
1784         return ret;
1785
1786     if (s->pict_type != AV_PICTURE_TYPE_B) {
1787         s->last_picture_ptr = s->next_picture_ptr;
1788         if (!s->droppable)
1789             s->next_picture_ptr = s->current_picture_ptr;
1790     }
1791     av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1792             s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1793             s->last_picture_ptr    ? s->last_picture_ptr->f->data[0]    : NULL,
1794             s->next_picture_ptr    ? s->next_picture_ptr->f->data[0]    : NULL,
1795             s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
1796             s->pict_type, s->droppable);
1797
1798     if ((s->last_picture_ptr == NULL ||
1799          s->last_picture_ptr->f->buf[0] == NULL) &&
1800         (s->pict_type != AV_PICTURE_TYPE_I ||
1801          s->picture_structure != PICT_FRAME)) {
1802         int h_chroma_shift, v_chroma_shift;
1803         av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1804                                          &h_chroma_shift, &v_chroma_shift);
1805         if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
1806             av_log(avctx, AV_LOG_DEBUG,
1807                    "allocating dummy last picture for B frame\n");
1808         else if (s->pict_type != AV_PICTURE_TYPE_I)
1809             av_log(avctx, AV_LOG_ERROR,
1810                    "warning: first frame is no keyframe\n");
1811         else if (s->picture_structure != PICT_FRAME)
1812             av_log(avctx, AV_LOG_DEBUG,
1813                    "allocate dummy last picture for field based first keyframe\n");
1814
1815         /* Allocate a dummy frame */
1816         i = ff_find_unused_picture(s, 0);
1817         if (i < 0) {
1818             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1819             return i;
1820         }
1821         s->last_picture_ptr = &s->picture[i];
1822
1823         s->last_picture_ptr->reference   = 3;
1824         s->last_picture_ptr->f->key_frame = 0;
1825         s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
1826
1827         if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1828             s->last_picture_ptr = NULL;
1829             return -1;
1830         }
1831
1832         if (!avctx->hwaccel && !(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)) {
1833             for(i=0; i<avctx->height; i++)
1834                 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
1835                        0x80, avctx->width);
1836             for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
1837                 memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
1838                        0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
1839                 memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
1840                        0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
1841             }
1842
1843             if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
1844                 for(i=0; i<avctx->height; i++)
1845                 memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
1846             }
1847         }
1848
1849         ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1850         ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1851     }
1852     if ((s->next_picture_ptr == NULL ||
1853          s->next_picture_ptr->f->buf[0] == NULL) &&
1854         s->pict_type == AV_PICTURE_TYPE_B) {
1855         /* Allocate a dummy frame */
1856         i = ff_find_unused_picture(s, 0);
1857         if (i < 0) {
1858             av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1859             return i;
1860         }
1861         s->next_picture_ptr = &s->picture[i];
1862
1863         s->next_picture_ptr->reference   = 3;
1864         s->next_picture_ptr->f->key_frame = 0;
1865         s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
1866
1867         if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1868             s->next_picture_ptr = NULL;
1869             return -1;
1870         }
1871         ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1872         ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1873     }
1874
1875 #if 0 // BUFREF-FIXME
1876     memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
1877     memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
1878 #endif
1879     if (s->last_picture_ptr) {
1880         ff_mpeg_unref_picture(s, &s->last_picture);
1881         if (s->last_picture_ptr->f->buf[0] &&
1882             (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1883                                        s->last_picture_ptr)) < 0)
1884             return ret;
1885     }
1886     if (s->next_picture_ptr) {
1887         ff_mpeg_unref_picture(s, &s->next_picture);
1888         if (s->next_picture_ptr->f->buf[0] &&
1889             (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1890                                        s->next_picture_ptr)) < 0)
1891             return ret;
1892     }
1893
1894     av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
1895                                                  s->last_picture_ptr->f->buf[0]));
1896
1897     if (s->picture_structure!= PICT_FRAME) {
1898         int i;
1899         for (i = 0; i < 4; i++) {
1900             if (s->picture_structure == PICT_BOTTOM_FIELD) {
1901                 s->current_picture.f->data[i] +=
1902                     s->current_picture.f->linesize[i];
1903             }
1904             s->current_picture.f->linesize[i] *= 2;
1905             s->last_picture.f->linesize[i]    *= 2;
1906             s->next_picture.f->linesize[i]    *= 2;
1907         }
1908     }
1909
1910     s->err_recognition = avctx->err_recognition;
1911
1912     /* set dequantizer, we can't do it during init as
1913      * it might change for mpeg4 and we can't do it in the header
1914      * decode as init is not called for mpeg4 there yet */
1915     if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1916         s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1917         s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1918     } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1919         s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1920         s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1921     } else {
1922         s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1923         s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1924     }
1925
1926     if (s->avctx->debug & FF_DEBUG_NOMC) {
1927         gray_frame(s->current_picture_ptr->f);
1928     }
1929
1930     return 0;
1931 }
1932
1933 /* called after a frame has been decoded. */
1934 void ff_MPV_frame_end(MpegEncContext *s)
1935 {
1936     emms_c();
1937
1938     if (s->current_picture.reference)
1939         ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1940 }
1941
1942 /**
1943  * Draw a line from (ex, ey) -> (sx, sy).
1944  * @param w width of the image
1945  * @param h height of the image
1946  * @param stride stride/linesize of the image
1947  * @param color color of the arrow
1948  */
1949 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
1950                       int w, int h, int stride, int color)
1951 {
1952     int x, y, fr, f;
1953
1954     sx = av_clip(sx, 0, w - 1);
1955     sy = av_clip(sy, 0, h - 1);
1956     ex = av_clip(ex, 0, w - 1);
1957     ey = av_clip(ey, 0, h - 1);
1958
1959     buf[sy * stride + sx] += color;
1960
1961     if (FFABS(ex - sx) > FFABS(ey - sy)) {
1962         if (sx > ex) {
1963             FFSWAP(int, sx, ex);
1964             FFSWAP(int, sy, ey);
1965         }
1966         buf += sx + sy * stride;
1967         ex  -= sx;
1968         f    = ((ey - sy) << 16) / ex;
1969         for (x = 0; x <= ex; x++) {
1970             y  = (x * f) >> 16;
1971             fr = (x * f) & 0xFFFF;
1972             buf[y * stride + x]       += (color * (0x10000 - fr)) >> 16;
1973             if(fr) buf[(y + 1) * stride + x] += (color *            fr ) >> 16;
1974         }
1975     } else {
1976         if (sy > ey) {
1977             FFSWAP(int, sx, ex);
1978             FFSWAP(int, sy, ey);
1979         }
1980         buf += sx + sy * stride;
1981         ey  -= sy;
1982         if (ey)
1983             f = ((ex - sx) << 16) / ey;
1984         else
1985             f = 0;
1986         for(y= 0; y <= ey; y++){
1987             x  = (y*f) >> 16;
1988             fr = (y*f) & 0xFFFF;
1989             buf[y * stride + x]     += (color * (0x10000 - fr)) >> 16;
1990             if(fr) buf[y * stride + x + 1] += (color *            fr ) >> 16;
1991         }
1992     }
1993 }
1994
1995 /**
1996  * Draw an arrow from (ex, ey) -> (sx, sy).
1997  * @param w width of the image
1998  * @param h height of the image
1999  * @param stride stride/linesize of the image
2000  * @param color color of the arrow
2001  */
2002 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
2003                        int ey, int w, int h, int stride, int color)
2004 {
2005     int dx,dy;
2006
2007     sx = av_clip(sx, -100, w + 100);
2008     sy = av_clip(sy, -100, h + 100);
2009     ex = av_clip(ex, -100, w + 100);
2010     ey = av_clip(ey, -100, h + 100);
2011
2012     dx = ex - sx;
2013     dy = ey - sy;
2014
2015     if (dx * dx + dy * dy > 3 * 3) {
2016         int rx =  dx + dy;
2017         int ry = -dx + dy;
2018         int length = ff_sqrt((rx * rx + ry * ry) << 8);
2019
2020         // FIXME subpixel accuracy
2021         rx = ROUNDED_DIV(rx * 3 << 4, length);
2022         ry = ROUNDED_DIV(ry * 3 << 4, length);
2023
2024         draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
2025         draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
2026     }
2027     draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
2028 }
2029
2030 /**
2031  * Print debugging info for the given picture.
2032  */
2033 void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
2034                          uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
2035                          int *low_delay,
2036                          int mb_width, int mb_height, int mb_stride, int quarter_sample)
2037 {
2038     if (avctx->hwaccel || !mbtype_table
2039         || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
2040         return;
2041
2042
2043     if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
2044         int x,y;
2045
2046         av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
2047                av_get_picture_type_char(pict->pict_type));
2048         for (y = 0; y < mb_height; y++) {
2049             for (x = 0; x < mb_width; x++) {
2050                 if (avctx->debug & FF_DEBUG_SKIP) {
2051                     int count = mbskip_table[x + y * mb_stride];
2052                     if (count > 9)
2053                         count = 9;
2054                     av_log(avctx, AV_LOG_DEBUG, "%1d", count);
2055                 }
2056                 if (avctx->debug & FF_DEBUG_QP) {
2057                     av_log(avctx, AV_LOG_DEBUG, "%2d",
2058                            qscale_table[x + y * mb_stride]);
2059                 }
2060                 if (avctx->debug & FF_DEBUG_MB_TYPE) {
2061                     int mb_type = mbtype_table[x + y * mb_stride];
2062                     // Type & MV direction
2063                     if (IS_PCM(mb_type))
2064                         av_log(avctx, AV_LOG_DEBUG, "P");
2065                     else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
2066                         av_log(avctx, AV_LOG_DEBUG, "A");
2067                     else if (IS_INTRA4x4(mb_type))
2068                         av_log(avctx, AV_LOG_DEBUG, "i");
2069                     else if (IS_INTRA16x16(mb_type))
2070                         av_log(avctx, AV_LOG_DEBUG, "I");
2071                     else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
2072                         av_log(avctx, AV_LOG_DEBUG, "d");
2073                     else if (IS_DIRECT(mb_type))
2074                         av_log(avctx, AV_LOG_DEBUG, "D");
2075                     else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
2076                         av_log(avctx, AV_LOG_DEBUG, "g");
2077                     else if (IS_GMC(mb_type))
2078                         av_log(avctx, AV_LOG_DEBUG, "G");
2079                     else if (IS_SKIP(mb_type))
2080                         av_log(avctx, AV_LOG_DEBUG, "S");
2081                     else if (!USES_LIST(mb_type, 1))
2082                         av_log(avctx, AV_LOG_DEBUG, ">");
2083                     else if (!USES_LIST(mb_type, 0))
2084                         av_log(avctx, AV_LOG_DEBUG, "<");
2085                     else {
2086                         av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2087                         av_log(avctx, AV_LOG_DEBUG, "X");
2088                     }
2089
2090                     // segmentation
2091                     if (IS_8X8(mb_type))
2092                         av_log(avctx, AV_LOG_DEBUG, "+");
2093                     else if (IS_16X8(mb_type))
2094                         av_log(avctx, AV_LOG_DEBUG, "-");
2095                     else if (IS_8X16(mb_type))
2096                         av_log(avctx, AV_LOG_DEBUG, "|");
2097                     else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
2098                         av_log(avctx, AV_LOG_DEBUG, " ");
2099                     else
2100                         av_log(avctx, AV_LOG_DEBUG, "?");
2101
2102
2103                     if (IS_INTERLACED(mb_type))
2104                         av_log(avctx, AV_LOG_DEBUG, "=");
2105                     else
2106                         av_log(avctx, AV_LOG_DEBUG, " ");
2107                 }
2108             }
2109             av_log(avctx, AV_LOG_DEBUG, "\n");
2110         }
2111     }
2112
2113     if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
2114         (avctx->debug_mv)) {
2115         const int shift = 1 + quarter_sample;
2116         int mb_y;
2117         uint8_t *ptr;
2118         int i;
2119         int h_chroma_shift, v_chroma_shift, block_height;
2120         const int width          = avctx->width;
2121         const int height         = avctx->height;
2122         const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
2123         const int mv_stride      = (mb_width << mv_sample_log2) +
2124                                    (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
2125
2126         *low_delay = 0; // needed to see the vectors without trashing the buffers
2127
2128         avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
2129
2130         av_frame_make_writable(pict);
2131
2132         pict->opaque = NULL;
2133         ptr          = pict->data[0];
2134         block_height = 16 >> v_chroma_shift;
2135
2136         for (mb_y = 0; mb_y < mb_height; mb_y++) {
2137             int mb_x;
2138             for (mb_x = 0; mb_x < mb_width; mb_x++) {
2139                 const int mb_index = mb_x + mb_y * mb_stride;
2140                 if ((avctx->debug_mv) && motion_val[0]) {
2141                     int type;
2142                     for (type = 0; type < 3; type++) {
2143                         int direction = 0;
2144                         switch (type) {
2145                         case 0:
2146                             if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
2147                                 (pict->pict_type!= AV_PICTURE_TYPE_P))
2148                                 continue;
2149                             direction = 0;
2150                             break;
2151                         case 1:
2152                             if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
2153                                 (pict->pict_type!= AV_PICTURE_TYPE_B))
2154                                 continue;
2155                             direction = 0;
2156                             break;
2157                         case 2:
2158                             if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
2159                                 (pict->pict_type!= AV_PICTURE_TYPE_B))
2160                                 continue;
2161                             direction = 1;
2162                             break;
2163                         }
2164                         if (!USES_LIST(mbtype_table[mb_index], direction))
2165                             continue;
2166
2167                         if (IS_8X8(mbtype_table[mb_index])) {
2168                             int i;
2169                             for (i = 0; i < 4; i++) {
2170                                 int sx = mb_x * 16 + 4 + 8 * (i & 1);
2171                                 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
2172                                 int xy = (mb_x * 2 + (i & 1) +
2173                                           (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2174                                 int mx = (motion_val[direction][xy][0] >> shift) + sx;
2175                                 int my = (motion_val[direction][xy][1] >> shift) + sy;
2176                                 draw_arrow(ptr, sx, sy, mx, my, width,
2177                                            height, pict->linesize[0], 100);
2178                             }
2179                         } else if (IS_16X8(mbtype_table[mb_index])) {
2180                             int i;
2181                             for (i = 0; i < 2; i++) {
2182                                 int sx = mb_x * 16 + 8;
2183                                 int sy = mb_y * 16 + 4 + 8 * i;
2184                                 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
2185                                 int mx = (motion_val[direction][xy][0] >> shift);
2186                                 int my = (motion_val[direction][xy][1] >> shift);
2187
2188                                 if (IS_INTERLACED(mbtype_table[mb_index]))
2189                                     my *= 2;
2190
2191                             draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2192                                        height, pict->linesize[0], 100);
2193                             }
2194                         } else if (IS_8X16(mbtype_table[mb_index])) {
2195                             int i;
2196                             for (i = 0; i < 2; i++) {
2197                                 int sx = mb_x * 16 + 4 + 8 * i;
2198                                 int sy = mb_y * 16 + 8;
2199                                 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
2200                                 int mx = motion_val[direction][xy][0] >> shift;
2201                                 int my = motion_val[direction][xy][1] >> shift;
2202
2203                                 if (IS_INTERLACED(mbtype_table[mb_index]))
2204                                     my *= 2;
2205
2206                                 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2207                                            height, pict->linesize[0], 100);
2208                             }
2209                         } else {
2210                               int sx= mb_x * 16 + 8;
2211                               int sy= mb_y * 16 + 8;
2212                               int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
2213                               int mx= (motion_val[direction][xy][0]>>shift) + sx;
2214                               int my= (motion_val[direction][xy][1]>>shift) + sy;
2215                               draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
2216                         }
2217                     }
2218                 }
2219                 if ((avctx->debug & FF_DEBUG_VIS_QP)) {
2220                     uint64_t c = (qscale_table[mb_index] * 128 / 31) *
2221                                  0x0101010101010101ULL;
2222                     int y;
2223                     for (y = 0; y < block_height; y++) {
2224                         *(uint64_t *)(pict->data[1] + 8 * mb_x +
2225                                       (block_height * mb_y + y) *
2226                                       pict->linesize[1]) = c;
2227                         *(uint64_t *)(pict->data[2] + 8 * mb_x +
2228                                       (block_height * mb_y + y) *
2229                                       pict->linesize[2]) = c;
2230                     }
2231                 }
2232                 if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
2233                     motion_val[0]) {
2234                     int mb_type = mbtype_table[mb_index];
2235                     uint64_t u,v;
2236                     int y;
2237 #define COLOR(theta, r) \
2238     u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
2239     v = (int)(128 + r * sin(theta * 3.141592 / 180));
2240
2241
2242                     u = v = 128;
2243                     if (IS_PCM(mb_type)) {
2244                         COLOR(120, 48)
2245                     } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
2246                                IS_INTRA16x16(mb_type)) {
2247                         COLOR(30, 48)
2248                     } else if (IS_INTRA4x4(mb_type)) {
2249                         COLOR(90, 48)
2250                     } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
2251                         // COLOR(120, 48)
2252                     } else if (IS_DIRECT(mb_type)) {
2253                         COLOR(150, 48)
2254                     } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
2255                         COLOR(170, 48)
2256                     } else if (IS_GMC(mb_type)) {
2257                         COLOR(190, 48)
2258                     } else if (IS_SKIP(mb_type)) {
2259                         // COLOR(180, 48)
2260                     } else if (!USES_LIST(mb_type, 1)) {
2261                         COLOR(240, 48)
2262                     } else if (!USES_LIST(mb_type, 0)) {
2263                         COLOR(0, 48)
2264                     } else {
2265                         av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2266                         COLOR(300,48)
2267                     }
2268
2269                     u *= 0x0101010101010101ULL;
2270                     v *= 0x0101010101010101ULL;
2271                     for (y = 0; y < block_height; y++) {
2272                         *(uint64_t *)(pict->data[1] + 8 * mb_x +
2273                                       (block_height * mb_y + y) * pict->linesize[1]) = u;
2274                         *(uint64_t *)(pict->data[2] + 8 * mb_x +
2275                                       (block_height * mb_y + y) * pict->linesize[2]) = v;
2276                     }
2277
2278                     // segmentation
2279                     if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
2280                         *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
2281                                       (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2282                         *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
2283                                       (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2284                     }
2285                     if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
2286                         for (y = 0; y < 16; y++)
2287                             pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
2288                                           pict->linesize[0]] ^= 0x80;
2289                     }
2290                     if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
2291                         int dm = 1 << (mv_sample_log2 - 2);
2292                         for (i = 0; i < 4; i++) {
2293                             int sx = mb_x * 16 + 8 * (i & 1);
2294                             int sy = mb_y * 16 + 8 * (i >> 1);
2295                             int xy = (mb_x * 2 + (i & 1) +
2296                                      (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2297                             // FIXME bidir
2298                             int32_t *mv = (int32_t *) &motion_val[0][xy];
2299                             if (mv[0] != mv[dm] ||
2300                                 mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
2301                                 for (y = 0; y < 8; y++)
2302                                     pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
2303                             if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
2304                                 *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
2305                                               pict->linesize[0]) ^= 0x8080808080808080ULL;
2306                         }
2307                     }
2308
2309                     if (IS_INTERLACED(mb_type) &&
2310                         avctx->codec->id == AV_CODEC_ID_H264) {
2311                         // hmm
2312                     }
2313                 }
2314                 mbskip_table[mb_index] = 0;
2315             }
2316         }
2317     }
2318 }
2319
2320 void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
2321 {
2322     ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
2323                          p->qscale_table, p->motion_val, &s->low_delay,
2324                          s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
2325 }
2326
2327 int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
2328 {
2329     AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
2330     int offset = 2*s->mb_stride + 1;
2331     if(!ref)
2332         return AVERROR(ENOMEM);
2333     av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
2334     ref->size -= offset;
2335     ref->data += offset;
2336     return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
2337 }
2338
2339 static inline int hpel_motion_lowres(MpegEncContext *s,
2340                                      uint8_t *dest, uint8_t *src,
2341                                      int field_based, int field_select,
2342                                      int src_x, int src_y,
2343                                      int width, int height, ptrdiff_t stride,
2344                                      int h_edge_pos, int v_edge_pos,
2345                                      int w, int h, h264_chroma_mc_func *pix_op,
2346                                      int motion_x, int motion_y)
2347 {
2348     const int lowres   = s->avctx->lowres;
2349     const int op_index = FFMIN(lowres, 3);
2350     const int s_mask   = (2 << lowres) - 1;
2351     int emu = 0;
2352     int sx, sy;
2353
2354     if (s->quarter_sample) {
2355         motion_x /= 2;
2356         motion_y /= 2;
2357     }
2358
2359     sx = motion_x & s_mask;
2360     sy = motion_y & s_mask;
2361     src_x += motion_x >> lowres + 1;
2362     src_y += motion_y >> lowres + 1;
2363
2364     src   += src_y * stride + src_x;
2365
2366     if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w,                 0) ||
2367         (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2368         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
2369                                  s->linesize, s->linesize,
2370                                  w + 1, (h + 1) << field_based,
2371                                  src_x, src_y   << field_based,
2372                                  h_edge_pos, v_edge_pos);
2373         src = s->edge_emu_buffer;
2374         emu = 1;
2375     }
2376
2377     sx = (sx << 2) >> lowres;
2378     sy = (sy << 2) >> lowres;
2379     if (field_select)
2380         src += s->linesize;
2381     pix_op[op_index](dest, src, stride, h, sx, sy);
2382     return emu;
2383 }
2384
2385 /* apply one mpeg motion vector to the three components */
2386 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
2387                                                 uint8_t *dest_y,
2388                                                 uint8_t *dest_cb,
2389                                                 uint8_t *dest_cr,
2390                                                 int field_based,
2391                                                 int bottom_field,
2392                                                 int field_select,
2393                                                 uint8_t **ref_picture,
2394                                                 h264_chroma_mc_func *pix_op,
2395                                                 int motion_x, int motion_y,
2396                                                 int h, int mb_y)
2397 {
2398     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2399     int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
2400     ptrdiff_t uvlinesize, linesize;
2401     const int lowres     = s->avctx->lowres;
2402     const int op_index   = FFMIN(lowres-1+s->chroma_x_shift, 3);
2403     const int block_s    = 8>>lowres;
2404     const int s_mask     = (2 << lowres) - 1;
2405     const int h_edge_pos = s->h_edge_pos >> lowres;
2406     const int v_edge_pos = s->v_edge_pos >> lowres;
2407     linesize   = s->current_picture.f->linesize[0] << field_based;
2408     uvlinesize = s->current_picture.f->linesize[1] << field_based;
2409
2410     // FIXME obviously not perfect but qpel will not work in lowres anyway
2411     if (s->quarter_sample) {
2412         motion_x /= 2;
2413         motion_y /= 2;
2414     }
2415
2416     if(field_based){
2417         motion_y += (bottom_field - field_select)*((1 << lowres)-1);
2418     }
2419
2420     sx = motion_x & s_mask;
2421     sy = motion_y & s_mask;
2422     src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
2423     src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
2424
2425     if (s->out_format == FMT_H263) {
2426         uvsx    = ((motion_x >> 1) & s_mask) | (sx & 1);
2427         uvsy    = ((motion_y >> 1) & s_mask) | (sy & 1);
2428         uvsrc_x = src_x >> 1;
2429         uvsrc_y = src_y >> 1;
2430     } else if (s->out_format == FMT_H261) {
2431         // even chroma mv's are full pel in H261
2432         mx      = motion_x / 4;
2433         my      = motion_y / 4;
2434         uvsx    = (2 * mx) & s_mask;
2435         uvsy    = (2 * my) & s_mask;
2436         uvsrc_x = s->mb_x * block_s + (mx >> lowres);
2437         uvsrc_y =    mb_y * block_s + (my >> lowres);
2438     } else {
2439         if(s->chroma_y_shift){
2440             mx      = motion_x / 2;
2441             my      = motion_y / 2;
2442             uvsx    = mx & s_mask;
2443             uvsy    = my & s_mask;
2444             uvsrc_x = s->mb_x * block_s                 + (mx >> lowres + 1);
2445             uvsrc_y =   (mb_y * block_s >> field_based) + (my >> lowres + 1);
2446         } else {
2447             if(s->chroma_x_shift){
2448             //Chroma422
2449                 mx = motion_x / 2;
2450                 uvsx = mx & s_mask;
2451                 uvsy = motion_y & s_mask;
2452                 uvsrc_y = src_y;
2453                 uvsrc_x = s->mb_x*block_s               + (mx >> (lowres+1));
2454             } else {
2455             //Chroma444
2456                 uvsx = motion_x & s_mask;
2457                 uvsy = motion_y & s_mask;
2458                 uvsrc_x = src_x;
2459                 uvsrc_y = src_y;
2460             }
2461         }
2462     }
2463
2464     ptr_y  = ref_picture[0] + src_y   * linesize   + src_x;
2465     ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
2466     ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
2467
2468     if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s,       0) || uvsrc_y<0 ||
2469         (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2470         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
2471                                  linesize >> field_based, linesize >> field_based,
2472                                  17, 17 + field_based,
2473                                 src_x, src_y << field_based, h_edge_pos,
2474                                 v_edge_pos);
2475         ptr_y = s->edge_emu_buffer;
2476         if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2477             uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
2478             s->vdsp.emulated_edge_mc(uvbuf,  ptr_cb,
2479                                      uvlinesize >> field_based, uvlinesize >> field_based,
2480                                      9, 9 + field_based,
2481                                     uvsrc_x, uvsrc_y << field_based,
2482                                     h_edge_pos >> 1, v_edge_pos >> 1);
2483             s->vdsp.emulated_edge_mc(uvbuf + 16,  ptr_cr,
2484                                      uvlinesize >> field_based,uvlinesize >> field_based,
2485                                      9, 9 + field_based,
2486                                     uvsrc_x, uvsrc_y << field_based,
2487                                     h_edge_pos >> 1, v_edge_pos >> 1);
2488             ptr_cb = uvbuf;
2489             ptr_cr = uvbuf + 16;
2490         }
2491     }
2492
2493     // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
2494     if (bottom_field) {
2495         dest_y  += s->linesize;
2496         dest_cb += s->uvlinesize;
2497         dest_cr += s->uvlinesize;
2498     }
2499
2500     if (field_select) {
2501         ptr_y   += s->linesize;
2502         ptr_cb  += s->uvlinesize;
2503         ptr_cr  += s->uvlinesize;
2504     }
2505
2506     sx = (sx << 2) >> lowres;
2507     sy = (sy << 2) >> lowres;
2508     pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
2509
2510     if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
2511         int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
2512         uvsx = (uvsx << 2) >> lowres;
2513         uvsy = (uvsy << 2) >> lowres;
2514         if (hc) {
2515             pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
2516             pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
2517         }
2518     }
2519     // FIXME h261 lowres loop filter
2520 }
2521
2522 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
2523                                             uint8_t *dest_cb, uint8_t *dest_cr,
2524                                             uint8_t **ref_picture,
2525                                             h264_chroma_mc_func * pix_op,
2526                                             int mx, int my)
2527 {
2528     const int lowres     = s->avctx->lowres;
2529     const int op_index   = FFMIN(lowres, 3);
2530     const int block_s    = 8 >> lowres;
2531     const int s_mask     = (2 << lowres) - 1;
2532     const int h_edge_pos = s->h_edge_pos >> lowres + 1;
2533     const int v_edge_pos = s->v_edge_pos >> lowres + 1;
2534     int emu = 0, src_x, src_y, sx, sy;
2535     ptrdiff_t offset;
2536     uint8_t *ptr;
2537
2538     if (s->quarter_sample) {
2539         mx /= 2;
2540         my /= 2;
2541     }
2542
2543     /* In case of 8X8, we construct a single chroma motion vector
2544        with a special rounding */
2545     mx = ff_h263_round_chroma(mx);
2546     my = ff_h263_round_chroma(my);
2547
2548     sx = mx & s_mask;
2549     sy = my & s_mask;
2550     src_x = s->mb_x * block_s + (mx >> lowres + 1);
2551     src_y = s->mb_y * block_s + (my >> lowres + 1);
2552
2553     offset = src_y * s->uvlinesize + src_x;
2554     ptr = ref_picture[1] + offset;
2555     if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
2556         (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2557         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
2558                                  s->uvlinesize, s->uvlinesize,
2559                                  9, 9,
2560                                  src_x, src_y, h_edge_pos, v_edge_pos);
2561         ptr = s->edge_emu_buffer;
2562         emu = 1;
2563     }
2564     sx = (sx << 2) >> lowres;
2565     sy = (sy << 2) >> lowres;
2566     pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
2567
2568     ptr = ref_picture[2] + offset;
2569     if (emu) {
2570         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
2571                                  s->uvlinesize, s->uvlinesize,
2572                                  9, 9,
2573                                  src_x, src_y, h_edge_pos, v_edge_pos);
2574         ptr = s->edge_emu_buffer;
2575     }
2576     pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
2577 }
2578
2579 /**
2580  * motion compensation of a single macroblock
2581  * @param s context
2582  * @param dest_y luma destination pointer
2583  * @param dest_cb chroma cb/u destination pointer
2584  * @param dest_cr chroma cr/v destination pointer
2585  * @param dir direction (0->forward, 1->backward)
2586  * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2587  * @param pix_op halfpel motion compensation function (average or put normally)
2588  * the motion vectors are taken from s->mv and the MV type from s->mv_type
2589  */
2590 static inline void MPV_motion_lowres(MpegEncContext *s,
2591                                      uint8_t *dest_y, uint8_t *dest_cb,
2592                                      uint8_t *dest_cr,
2593                                      int dir, uint8_t **ref_picture,
2594                                      h264_chroma_mc_func *pix_op)
2595 {
2596     int mx, my;
2597     int mb_x, mb_y, i;
2598     const int lowres  = s->avctx->lowres;
2599     const int block_s = 8 >>lowres;
2600
2601     mb_x = s->mb_x;
2602     mb_y = s->mb_y;
2603
2604     switch (s->mv_type) {
2605     case MV_TYPE_16X16:
2606         mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2607                            0, 0, 0,
2608                            ref_picture, pix_op,
2609                            s->mv[dir][0][0], s->mv[dir][0][1],
2610                            2 * block_s, mb_y);
2611         break;
2612     case MV_TYPE_8X8:
2613         mx = 0;
2614         my = 0;
2615         for (i = 0; i < 4; i++) {
2616             hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
2617                                s->linesize) * block_s,
2618                                ref_picture[0], 0, 0,
2619                                (2 * mb_x + (i & 1)) * block_s,
2620                                (2 * mb_y + (i >> 1)) * block_s,
2621                                s->width, s->height, s->linesize,
2622                                s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
2623                                block_s, block_s, pix_op,
2624                                s->mv[dir][i][0], s->mv[dir][i][1]);
2625
2626             mx += s->mv[dir][i][0];
2627             my += s->mv[dir][i][1];
2628         }
2629
2630         if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
2631             chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
2632                                      pix_op, mx, my);
2633         break;
2634     case MV_TYPE_FIELD:
2635         if (s->picture_structure == PICT_FRAME) {
2636             /* top field */
2637             mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2638                                1, 0, s->field_select[dir][0],
2639                                ref_picture, pix_op,
2640                                s->mv[dir][0][0], s->mv[dir][0][1],
2641                                block_s, mb_y);
2642             /* bottom field */
2643             mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2644                                1, 1, s->field_select[dir][1],
2645                                ref_picture, pix_op,
2646                                s->mv[dir][1][0], s->mv[dir][1][1],
2647                                block_s, mb_y);
2648         } else {
2649             if (s->picture_structure != s->field_select[dir][0] + 1 &&
2650                 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
2651                 ref_picture = s->current_picture_ptr->f->data;
2652
2653             }
2654             mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2655                                0, 0, s->field_select[dir][0],
2656                                ref_picture, pix_op,
2657                                s->mv[dir][0][0],
2658                                s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
2659             }
2660         break;
2661     case MV_TYPE_16X8:
2662         for (i = 0; i < 2; i++) {
2663             uint8_t **ref2picture;
2664
2665             if (s->picture_structure == s->field_select[dir][i] + 1 ||
2666                 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
2667                 ref2picture = ref_picture;
2668             } else {
2669                 ref2picture = s->current_picture_ptr->f->data;
2670             }
2671
2672             mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2673                                0, 0, s->field_select[dir][i],
2674                                ref2picture, pix_op,
2675                                s->mv[dir][i][0], s->mv[dir][i][1] +
2676                                2 * block_s * i, block_s, mb_y >> 1);
2677
2678             dest_y  +=  2 * block_s *  s->linesize;
2679             dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2680             dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2681         }
2682         break;
2683     case MV_TYPE_DMV:
2684         if (s->picture_structure == PICT_FRAME) {
2685             for (i = 0; i < 2; i++) {
2686                 int j;
2687                 for (j = 0; j < 2; j++) {
2688                     mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2689                                        1, j, j ^ i,
2690                                        ref_picture, pix_op,
2691                                        s->mv[dir][2 * i + j][0],
2692                                        s->mv[dir][2 * i + j][1],
2693                                        block_s, mb_y);
2694                 }
2695                 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2696             }
2697         } else {
2698             for (i = 0; i < 2; i++) {
2699                 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2700                                    0, 0, s->picture_structure != i + 1,
2701                                    ref_picture, pix_op,
2702                                    s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
2703                                    2 * block_s, mb_y >> 1);
2704
2705                 // after put we make avg of the same block
2706                 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
2707
2708                 // opposite parity is always in the same
2709                 // frame if this is second field
2710                 if (!s->first_field) {
2711                     ref_picture = s->current_picture_ptr->f->data;
2712                 }
2713             }
2714         }
2715         break;
2716     default:
2717         av_assert2(0);
2718     }
2719 }
2720
2721 /**
2722  * find the lowest MB row referenced in the MVs
2723  */
2724 int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2725 {
2726     int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2727     int my, off, i, mvs;
2728
2729     if (s->picture_structure != PICT_FRAME || s->mcsel)
2730         goto unhandled;
2731
2732     switch (s->mv_type) {
2733         case MV_TYPE_16X16:
2734             mvs = 1;
2735             break;
2736         case MV_TYPE_16X8:
2737             mvs = 2;
2738             break;
2739         case MV_TYPE_8X8:
2740             mvs = 4;
2741             break;
2742         default:
2743             goto unhandled;
2744     }
2745
2746     for (i = 0; i < mvs; i++) {
2747         my = s->mv[dir][i][1]<<qpel_shift;
2748         my_max = FFMAX(my_max, my);
2749         my_min = FFMIN(my_min, my);
2750     }
2751
2752     off = (FFMAX(-my_min, my_max) + 63) >> 6;
2753
2754     return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
2755 unhandled:
2756     return s->mb_height-1;
2757 }
2758
2759 /* put block[] to dest[] */
2760 static inline void put_dct(MpegEncContext *s,
2761                            int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2762 {
2763     s->dct_unquantize_intra(s, block, i, qscale);
2764     s->dsp.idct_put (dest, line_size, block);
2765 }
2766
2767 /* add block[] to dest[] */
2768 static inline void add_dct(MpegEncContext *s,
2769                            int16_t *block, int i, uint8_t *dest, int line_size)
2770 {
2771     if (s->block_last_index[i] >= 0) {
2772         s->dsp.idct_add (dest, line_size, block);
2773     }
2774 }
2775
2776 static inline void add_dequant_dct(MpegEncContext *s,
2777                            int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2778 {
2779     if (s->block_last_index[i] >= 0) {
2780         s->dct_unquantize_inter(s, block, i, qscale);
2781
2782         s->dsp.idct_add (dest, line_size, block);
2783     }
2784 }
2785
2786 /**
2787  * Clean dc, ac, coded_block for the current non-intra MB.
2788  */
2789 void ff_clean_intra_table_entries(MpegEncContext *s)
2790 {
2791     int wrap = s->b8_stride;
2792     int xy = s->block_index[0];
2793
2794     s->dc_val[0][xy           ] =
2795     s->dc_val[0][xy + 1       ] =
2796     s->dc_val[0][xy     + wrap] =
2797     s->dc_val[0][xy + 1 + wrap] = 1024;
2798     /* ac pred */
2799     memset(s->ac_val[0][xy       ], 0, 32 * sizeof(int16_t));
2800     memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2801     if (s->msmpeg4_version>=3) {
2802         s->coded_block[xy           ] =
2803         s->coded_block[xy + 1       ] =
2804         s->coded_block[xy     + wrap] =
2805         s->coded_block[xy + 1 + wrap] = 0;
2806     }
2807     /* chroma */
2808     wrap = s->mb_stride;
2809     xy = s->mb_x + s->mb_y * wrap;
2810     s->dc_val[1][xy] =
2811     s->dc_val[2][xy] = 1024;
2812     /* ac pred */
2813     memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2814     memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2815
2816     s->mbintra_table[xy]= 0;
2817 }
2818
2819 /* generic function called after a macroblock has been parsed by the
2820    decoder or after it has been encoded by the encoder.
2821
2822    Important variables used:
2823    s->mb_intra : true if intra macroblock
2824    s->mv_dir   : motion vector direction
2825    s->mv_type  : motion vector type
2826    s->mv       : motion vector
2827    s->interlaced_dct : true if interlaced dct used (mpeg2)
2828  */
2829 static av_always_inline
2830 void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
2831                             int lowres_flag, int is_mpeg12)
2832 {
2833     const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2834
2835     if (CONFIG_XVMC &&
2836         s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
2837         s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
2838         return;
2839     }
2840
2841     if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2842        /* print DCT coefficients */
2843        int i,j;
2844        av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2845        for(i=0; i<6; i++){
2846            for(j=0; j<64; j++){
2847                av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
2848            }
2849            av_log(s->avctx, AV_LOG_DEBUG, "\n");
2850        }
2851     }
2852
2853     s->current_picture.qscale_table[mb_xy] = s->qscale;
2854
2855     /* update DC predictors for P macroblocks */
2856     if (!s->mb_intra) {
2857         if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2858             if(s->mbintra_table[mb_xy])
2859                 ff_clean_intra_table_entries(s);
2860         } else {
2861             s->last_dc[0] =
2862             s->last_dc[1] =
2863             s->last_dc[2] = 128 << s->intra_dc_precision;
2864         }
2865     }
2866     else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2867         s->mbintra_table[mb_xy]=1;
2868
2869     if (   (s->flags&CODEC_FLAG_PSNR)
2870         || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor
2871         || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
2872         uint8_t *dest_y, *dest_cb, *dest_cr;
2873         int dct_linesize, dct_offset;
2874         op_pixels_func (*op_pix)[4];
2875         qpel_mc_func (*op_qpix)[16];
2876         const int linesize   = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
2877         const int uvlinesize = s->current_picture.f->linesize[1];
2878         const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
2879         const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
2880
2881         /* avoid copy if macroblock skipped in last frame too */
2882         /* skip only during decoding as we might trash the buffers during encoding a bit */
2883         if(!s->encoding){
2884             uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2885
2886             if (s->mb_skipped) {
2887                 s->mb_skipped= 0;
2888                 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
2889                 *mbskip_ptr = 1;
2890             } else if(!s->current_picture.reference) {
2891                 *mbskip_ptr = 1;
2892             } else{
2893                 *mbskip_ptr = 0; /* not skipped */
2894             }
2895         }
2896
2897         dct_linesize = linesize << s->interlaced_dct;
2898         dct_offset   = s->interlaced_dct ? linesize : linesize * block_size;
2899
2900         if(readable){
2901             dest_y=  s->dest[0];
2902             dest_cb= s->dest[1];
2903             dest_cr= s->dest[2];
2904         }else{
2905             dest_y = s->b_scratchpad;
2906             dest_cb= s->b_scratchpad+16*linesize;
2907             dest_cr= s->b_scratchpad+32*linesize;
2908         }
2909
2910         if (!s->mb_intra) {
2911             /* motion handling */
2912             /* decoding or more than one mb_type (MC was already done otherwise) */
2913             if(!s->encoding){
2914
2915                 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2916                     if (s->mv_dir & MV_DIR_FORWARD) {
2917                         ff_thread_await_progress(&s->last_picture_ptr->tf,
2918                                                  ff_MPV_lowest_referenced_row(s, 0),
2919                                                  0);
2920                     }
2921                     if (s->mv_dir & MV_DIR_BACKWARD) {
2922                         ff_thread_await_progress(&s->next_picture_ptr->tf,
2923                                                  ff_MPV_lowest_referenced_row(s, 1),
2924                                                  0);
2925                     }
2926                 }
2927
2928                 if(lowres_flag){
2929                     h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
2930
2931                     if (s->mv_dir & MV_DIR_FORWARD) {
2932                         MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
2933                         op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
2934                     }
2935                     if (s->mv_dir & MV_DIR_BACKWARD) {
2936                         MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
2937                     }
2938                 }else{
2939                     op_qpix = s->me.qpel_put;
2940                     if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2941                         op_pix = s->hdsp.put_pixels_tab;
2942                     }else{
2943                         op_pix = s->hdsp.put_no_rnd_pixels_tab;
2944                     }
2945                     if (s->mv_dir & MV_DIR_FORWARD) {
2946                         ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
2947                         op_pix = s->hdsp.avg_pixels_tab;
2948                         op_qpix= s->me.qpel_avg;
2949                     }
2950                     if (s->mv_dir & MV_DIR_BACKWARD) {
2951                         ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
2952                     }
2953                 }
2954             }
2955
2956             /* skip dequant / idct if we are really late ;) */
2957             if(s->avctx->skip_idct){
2958                 if(  (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
2959                    ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
2960                    || s->avctx->skip_idct >= AVDISCARD_ALL)
2961                     goto skip_idct;
2962             }
2963
2964             /* add dct residue */
2965             if(s->encoding || !(   s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
2966                                 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2967                 add_dequant_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);
2968                 add_dequant_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);
2969                 add_dequant_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);
2970                 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2971
2972                 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2973                     if (s->chroma_y_shift){
2974                         add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2975                         add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2976                     }else{
2977                         dct_linesize >>= 1;
2978                         dct_offset >>=1;
2979                         add_dequant_dct(s, block[4], 4, dest_cb,              dct_linesize, s->chroma_qscale);
2980                         add_dequant_dct(s, block[5], 5, dest_cr,              dct_linesize, s->chroma_qscale);
2981                         add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2982                         add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2983                     }
2984                 }
2985             } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2986                 add_dct(s, block[0], 0, dest_y                          , dct_linesize);
2987                 add_dct(s, block[1], 1, dest_y              + block_size, dct_linesize);
2988                 add_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize);
2989                 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
2990
2991                 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2992                     if(s->chroma_y_shift){//Chroma420
2993                         add_dct(s, block[4], 4, dest_cb, uvlinesize);
2994                         add_dct(s, block[5], 5, dest_cr, uvlinesize);
2995                     }else{
2996                         //chroma422
2997                         dct_linesize = uvlinesize << s->interlaced_dct;
2998                         dct_offset   = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
2999
3000                         add_dct(s, block[4], 4, dest_cb, dct_linesize);
3001                         add_dct(s, block[5], 5, dest_cr, dct_linesize);
3002                         add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
3003                         add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
3004                         if(!s->chroma_x_shift){//Chroma444
3005                             add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
3006                             add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
3007                             add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
3008                             add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
3009                         }
3010                     }
3011                 }//fi gray
3012             }
3013             else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
3014                 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
3015             }
3016         } else {
3017             /* dct only in intra block */
3018             if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
3019                 put_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);
3020                 put_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);
3021                 put_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);
3022                 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
3023
3024                 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
3025                     if(s->chroma_y_shift){
3026                         put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
3027                         put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
3028                     }else{
3029                         dct_offset >>=1;
3030                         dct_linesize >>=1;
3031                         put_dct(s, block[4], 4, dest_cb,              dct_linesize, s->chroma_qscale);
3032                         put_dct(s, block[5], 5, dest_cr,              dct_linesize, s->chroma_qscale);
3033                         put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
3034                         put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
3035                     }
3036                 }
3037             }else{
3038                 s->dsp.idct_put(dest_y                          , dct_linesize, block[0]);
3039                 s->dsp.idct_put(dest_y              + block_size, dct_linesize, block[1]);
3040                 s->dsp.idct_put(dest_y + dct_offset             , dct_linesize, block[2]);
3041                 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
3042
3043                 if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
3044                     if(s->chroma_y_shift){
3045                         s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
3046                         s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
3047                     }else{
3048
3049                         dct_linesize = uvlinesize << s->interlaced_dct;
3050                         dct_offset   = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
3051
3052                         s->dsp.idct_put(dest_cb,              dct_linesize, block[4]);
3053                         s->dsp.idct_put(dest_cr,              dct_linesize, block[5]);
3054                         s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
3055                         s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
3056                         if(!s->chroma_x_shift){//Chroma444
3057                             s->dsp.idct_put(dest_cb + block_size,              dct_linesize, block[8]);
3058                             s->dsp.idct_put(dest_cr + block_size,              dct_linesize, block[9]);
3059                             s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
3060                             s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
3061                         }
3062                     }
3063                 }//gray
3064             }
3065         }
3066 skip_idct:
3067         if(!readable){
3068             s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y ,   linesize,16);
3069             s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
3070             s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
3071         }
3072     }
3073 }
3074
3075 void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
3076 #if !CONFIG_SMALL
3077     if(s->out_format == FMT_MPEG1) {
3078         if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
3079         else                 MPV_decode_mb_internal(s, block, 0, 1);
3080     } else
3081 #endif
3082     if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
3083     else                  MPV_decode_mb_internal(s, block, 0, 0);
3084 }
3085
3086 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
3087 {
3088     ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
3089                        s->last_picture_ptr ? s->last_picture_ptr->f : NULL, y, h, s->picture_structure,
3090                        s->first_field, s->low_delay);
3091 }
3092
3093 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
3094     const int linesize   = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
3095     const int uvlinesize = s->current_picture.f->linesize[1];
3096     const int mb_size= 4 - s->avctx->lowres;
3097
3098     s->block_index[0]= s->b8_stride*(s->mb_y*2    ) - 2 + s->mb_x*2;
3099     s->block_index[1]= s->b8_stride*(s->mb_y*2    ) - 1 + s->mb_x*2;
3100     s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
3101     s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
3102     s->block_index[4]= s->mb_stride*(s->mb_y + 1)                + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
3103     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;
3104     //block_index is not used by mpeg2, so it is not affected by chroma_format
3105
3106     s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) <<  mb_size);
3107     s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3108     s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
3109
3110     if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
3111     {
3112         if(s->picture_structure==PICT_FRAME){
3113         s->dest[0] += s->mb_y *   linesize << mb_size;
3114         s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3115         s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3116         }else{
3117             s->dest[0] += (s->mb_y>>1) *   linesize << mb_size;
3118             s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3119             s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3120             av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
3121         }
3122     }
3123 }
3124
3125 /**
3126  * Permute an 8x8 block.
3127  * @param block the block which will be permuted according to the given permutation vector
3128  * @param permutation the permutation vector
3129  * @param last the last non zero coefficient in scantable order, used to speed the permutation up
3130  * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
3131  *                  (inverse) permutated to scantable order!
3132  */
3133 void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
3134 {
3135     int i;
3136     int16_t temp[64];
3137
3138     if(last<=0) return;
3139     //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
3140
3141     for(i=0; i<=last; i++){
3142         const int j= scantable[i];
3143         temp[j]= block[j];
3144         block[j]=0;
3145     }
3146
3147     for(i=0; i<=last; i++){
3148         const int j= scantable[i];
3149         const int perm_j= permutation[j];
3150         block[perm_j]= temp[j];
3151     }
3152 }
3153
3154 void ff_mpeg_flush(AVCodecContext *avctx){
3155     int i;
3156     MpegEncContext *s = avctx->priv_data;
3157
3158     if(s==NULL || s->picture==NULL)
3159         return;
3160
3161     for (i = 0; i < MAX_PICTURE_COUNT; i++)
3162         ff_mpeg_unref_picture(s, &s->picture[i]);
3163     s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
3164
3165     ff_mpeg_unref_picture(s, &s->current_picture);
3166     ff_mpeg_unref_picture(s, &s->last_picture);
3167     ff_mpeg_unref_picture(s, &s->next_picture);
3168
3169     s->mb_x= s->mb_y= 0;
3170     s->closed_gop= 0;
3171
3172     s->parse_context.state= -1;
3173     s->parse_context.frame_start_found= 0;
3174     s->parse_context.overread= 0;
3175     s->parse_context.overread_index= 0;
3176     s->parse_context.index= 0;
3177     s->parse_context.last_index= 0;
3178     s->bitstream_buffer_size=0;
3179     s->pp_time=0;
3180 }
3181
3182 /**
3183  * set qscale and update qscale dependent variables.
3184  */
3185 void ff_set_qscale(MpegEncContext * s, int qscale)
3186 {
3187     if (qscale < 1)
3188         qscale = 1;
3189     else if (qscale > 31)
3190         qscale = 31;
3191
3192     s->qscale = qscale;
3193     s->chroma_qscale= s->chroma_qscale_table[qscale];
3194
3195     s->y_dc_scale= s->y_dc_scale_table[ qscale ];
3196     s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
3197 }
3198
3199 void ff_MPV_report_decode_progress(MpegEncContext *s)
3200 {
3201     if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
3202         ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
3203 }
3204
3205 #if CONFIG_ERROR_RESILIENCE
3206 void ff_mpeg_set_erpic(ERPicture *dst, Picture *src)
3207 {
3208     int i;
3209
3210     memset(dst, 0, sizeof(*dst));
3211     if (!src)
3212         return;
3213
3214     dst->f = src->f;
3215     dst->tf = &src->tf;
3216
3217     for (i = 0; i < 2; i++) {
3218         dst->motion_val[i] = src->motion_val[i];
3219         dst->ref_index[i] = src->ref_index[i];
3220     }
3221
3222     dst->mb_type = src->mb_type;
3223     dst->field_picture = src->field_picture;
3224 }
3225
3226 void ff_mpeg_er_frame_start(MpegEncContext *s)
3227 {
3228     ERContext *er = &s->er;
3229
3230     ff_mpeg_set_erpic(&er->cur_pic, s->current_picture_ptr);
3231     ff_mpeg_set_erpic(&er->next_pic, s->next_picture_ptr);
3232     ff_mpeg_set_erpic(&er->last_pic, s->last_picture_ptr);
3233
3234     er->pp_time           = s->pp_time;
3235     er->pb_time           = s->pb_time;
3236     er->quarter_sample    = s->quarter_sample;
3237     er->partitioned_frame = s->partitioned_frame;
3238
3239     ff_er_frame_start(er);
3240 }
3241 #endif /* CONFIG_ERROR_RESILIENCE */