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