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