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