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