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