Add VDPAU hardware accelerated decoding for MPEG-4 ASP which can be used
[ffmpeg.git] / libavcodec / h263dec.c
1 /*
2  * H.263 decoder
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file libavcodec/h263dec.c
25  * H.263 decoder.
26  */
27
28 #include "internal.h"
29 #include "avcodec.h"
30 #include "dsputil.h"
31 #include "mpegvideo.h"
32 #include "h263_parser.h"
33 #include "mpeg4video_parser.h"
34 #include "msmpeg4.h"
35 #include "vdpau_internal.h"
36
37 //#define DEBUG
38 //#define PRINT_FRAME_TIME
39
40 av_cold int ff_h263_decode_init(AVCodecContext *avctx)
41 {
42     MpegEncContext *s = avctx->priv_data;
43
44     s->avctx = avctx;
45     s->out_format = FMT_H263;
46
47     s->width  = avctx->coded_width;
48     s->height = avctx->coded_height;
49     s->workaround_bugs= avctx->workaround_bugs;
50
51     // set defaults
52     MPV_decode_defaults(s);
53     s->quant_precision=5;
54     s->decode_mb= ff_h263_decode_mb;
55     s->low_delay= 1;
56     avctx->pix_fmt= avctx->get_format(avctx, avctx->codec->pix_fmts);
57     s->unrestricted_mv= 1;
58
59     /* select sub codec */
60     switch(avctx->codec->id) {
61     case CODEC_ID_H263:
62         s->unrestricted_mv= 0;
63         avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
64         break;
65     case CODEC_ID_MPEG4:
66         s->decode_mb= ff_mpeg4_decode_mb;
67         s->time_increment_bits = 4; /* default value for broken headers */
68         s->h263_pred = 1;
69         s->low_delay = 0; //default, might be overriden in the vol header during header parsing
70         avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
71         break;
72     case CODEC_ID_MSMPEG4V1:
73         s->h263_msmpeg4 = 1;
74         s->h263_pred = 1;
75         s->msmpeg4_version=1;
76         break;
77     case CODEC_ID_MSMPEG4V2:
78         s->h263_msmpeg4 = 1;
79         s->h263_pred = 1;
80         s->msmpeg4_version=2;
81         break;
82     case CODEC_ID_MSMPEG4V3:
83         s->h263_msmpeg4 = 1;
84         s->h263_pred = 1;
85         s->msmpeg4_version=3;
86         break;
87     case CODEC_ID_WMV1:
88         s->h263_msmpeg4 = 1;
89         s->h263_pred = 1;
90         s->msmpeg4_version=4;
91         break;
92     case CODEC_ID_WMV2:
93         s->h263_msmpeg4 = 1;
94         s->h263_pred = 1;
95         s->msmpeg4_version=5;
96         break;
97     case CODEC_ID_VC1:
98     case CODEC_ID_WMV3:
99         s->h263_msmpeg4 = 1;
100         s->h263_pred = 1;
101         s->msmpeg4_version=6;
102         avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
103         break;
104     case CODEC_ID_H263I:
105         break;
106     case CODEC_ID_FLV1:
107         s->h263_flv = 1;
108         break;
109     default:
110         return -1;
111     }
112     s->codec_id= avctx->codec->id;
113     avctx->hwaccel= ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
114
115     /* for h263, we allocate the images after having read the header */
116     if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
117         if (MPV_common_init(s) < 0)
118             return -1;
119
120     if (CONFIG_MSMPEG4_DECODER && s->h263_msmpeg4)
121         ff_msmpeg4_decode_init(s);
122     else
123         h263_decode_init_vlc(s);
124
125     return 0;
126 }
127
128 av_cold int ff_h263_decode_end(AVCodecContext *avctx)
129 {
130     MpegEncContext *s = avctx->priv_data;
131
132     MPV_common_end(s);
133     return 0;
134 }
135
136 /**
137  * returns the number of bytes consumed for building the current frame
138  */
139 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
140     int pos= (get_bits_count(&s->gb)+7)>>3;
141
142     if(s->divx_packed || s->avctx->hwaccel){
143         //we would have to scan through the whole buf to handle the weird reordering ...
144         return buf_size;
145     }else if(s->flags&CODEC_FLAG_TRUNCATED){
146         pos -= s->parse_context.last_index;
147         if(pos<0) pos=0; // padding is not really read so this might be -1
148         return pos;
149     }else{
150         if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
151         if(pos+10>buf_size) pos=buf_size; // oops ;)
152
153         return pos;
154     }
155 }
156
157 static int decode_slice(MpegEncContext *s){
158     const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
159     const int mb_size= 16>>s->avctx->lowres;
160     s->last_resync_gb= s->gb;
161     s->first_slice_line= 1;
162
163     s->resync_mb_x= s->mb_x;
164     s->resync_mb_y= s->mb_y;
165
166     ff_set_qscale(s, s->qscale);
167
168     if (s->avctx->hwaccel) {
169         const uint8_t *start= s->gb.buffer + get_bits_count(&s->gb)/8;
170         const uint8_t *end  = ff_h263_find_resync_marker(start + 1, s->gb.buffer_end);
171         skip_bits_long(&s->gb, 8*(end - start));
172         return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
173     }
174
175     if(s->partitioned_frame){
176         const int qscale= s->qscale;
177
178         if(s->codec_id==CODEC_ID_MPEG4){
179             if(ff_mpeg4_decode_partitions(s) < 0)
180                 return -1;
181         }
182
183         /* restore variables which were modified */
184         s->first_slice_line=1;
185         s->mb_x= s->resync_mb_x;
186         s->mb_y= s->resync_mb_y;
187         ff_set_qscale(s, qscale);
188     }
189
190     for(; s->mb_y < s->mb_height; s->mb_y++) {
191         /* per-row end of slice checks */
192         if(s->msmpeg4_version){
193             if(s->resync_mb_y + s->slice_height == s->mb_y){
194                 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
195
196                 return 0;
197             }
198         }
199
200         if(s->msmpeg4_version==1){
201             s->last_dc[0]=
202             s->last_dc[1]=
203             s->last_dc[2]= 128;
204         }
205
206         ff_init_block_index(s);
207         for(; s->mb_x < s->mb_width; s->mb_x++) {
208             int ret;
209
210             ff_update_block_index(s);
211
212             if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
213                 s->first_slice_line=0;
214             }
215
216             /* DCT & quantize */
217
218             s->mv_dir = MV_DIR_FORWARD;
219             s->mv_type = MV_TYPE_16X16;
220 //            s->mb_skipped = 0;
221 //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
222             ret= s->decode_mb(s, s->block);
223
224             if (s->pict_type!=FF_B_TYPE)
225                 ff_h263_update_motion_val(s);
226
227             if(ret<0){
228                 const int xy= s->mb_x + s->mb_y*s->mb_stride;
229                 if(ret==SLICE_END){
230                     MPV_decode_mb(s, s->block);
231                     if(s->loop_filter)
232                         ff_h263_loop_filter(s);
233
234 //printf("%d %d %d %06X\n", s->mb_x, s->mb_y, s->gb.size*8 - get_bits_count(&s->gb), show_bits(&s->gb, 24));
235                     ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
236
237                     s->padding_bug_score--;
238
239                     if(++s->mb_x >= s->mb_width){
240                         s->mb_x=0;
241                         ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
242                         s->mb_y++;
243                     }
244                     return 0;
245                 }else if(ret==SLICE_NOEND){
246                     av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
247                     ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x+1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
248                     return -1;
249                 }
250                 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
251                 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
252
253                 return -1;
254             }
255
256             MPV_decode_mb(s, s->block);
257             if(s->loop_filter)
258                 ff_h263_loop_filter(s);
259         }
260
261         ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
262
263         s->mb_x= 0;
264     }
265
266     assert(s->mb_x==0 && s->mb_y==s->mb_height);
267
268     /* try to detect the padding bug */
269     if(      s->codec_id==CODEC_ID_MPEG4
270        &&   (s->workaround_bugs&FF_BUG_AUTODETECT)
271        &&    s->gb.size_in_bits - get_bits_count(&s->gb) >=0
272        &&    s->gb.size_in_bits - get_bits_count(&s->gb) < 48
273 //       &&   !s->resync_marker
274        &&   !s->data_partitioning){
275
276         const int bits_count= get_bits_count(&s->gb);
277         const int bits_left = s->gb.size_in_bits - bits_count;
278
279         if(bits_left==0){
280             s->padding_bug_score+=16;
281         } else if(bits_left != 1){
282             int v= show_bits(&s->gb, 8);
283             v|= 0x7F >> (7-(bits_count&7));
284
285             if(v==0x7F && bits_left<=8)
286                 s->padding_bug_score--;
287             else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16)
288                 s->padding_bug_score+= 4;
289             else
290                 s->padding_bug_score++;
291         }
292     }
293
294     if(s->workaround_bugs&FF_BUG_AUTODETECT){
295         if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version || !s->resync_marker)*/)
296             s->workaround_bugs |=  FF_BUG_NO_PADDING;
297         else
298             s->workaround_bugs &= ~FF_BUG_NO_PADDING;
299     }
300
301     // handle formats which don't have unique end markers
302     if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
303         int left= s->gb.size_in_bits - get_bits_count(&s->gb);
304         int max_extra=7;
305
306         /* no markers in M$ crap */
307         if(s->msmpeg4_version && s->pict_type==FF_I_TYPE)
308             max_extra+= 17;
309
310         /* buggy padding but the frame should still end approximately at the bitstream end */
311         if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_recognition>=3)
312             max_extra+= 48;
313         else if((s->workaround_bugs&FF_BUG_NO_PADDING))
314             max_extra+= 256*256*256*64;
315
316         if(left>max_extra){
317             av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
318         }
319         else if(left<0){
320             av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
321         }else
322             ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
323
324         return 0;
325     }
326
327     av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
328             s->gb.size_in_bits - get_bits_count(&s->gb),
329             show_bits(&s->gb, 24), s->padding_bug_score);
330
331     ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
332
333     return -1;
334 }
335
336 int ff_h263_decode_frame(AVCodecContext *avctx,
337                              void *data, int *data_size,
338                              AVPacket *avpkt)
339 {
340     const uint8_t *buf = avpkt->data;
341     int buf_size = avpkt->size;
342     MpegEncContext *s = avctx->priv_data;
343     int ret;
344     AVFrame *pict = data;
345
346 #ifdef PRINT_FRAME_TIME
347 uint64_t time= rdtsc();
348 #endif
349     s->flags= avctx->flags;
350     s->flags2= avctx->flags2;
351
352     /* no supplementary picture */
353     if (buf_size == 0) {
354         /* special case for last picture */
355         if (s->low_delay==0 && s->next_picture_ptr) {
356             *pict= *(AVFrame*)s->next_picture_ptr;
357             s->next_picture_ptr= NULL;
358
359             *data_size = sizeof(AVFrame);
360         }
361
362         return 0;
363     }
364
365     if(s->flags&CODEC_FLAG_TRUNCATED){
366         int next;
367
368         if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4){
369             next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
370         }else if(CONFIG_H263_DECODER && s->codec_id==CODEC_ID_H263){
371             next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
372         }else{
373             av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
374             return -1;
375         }
376
377         if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
378             return buf_size;
379     }
380
381
382 retry:
383
384     if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
385         init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
386     }else
387         init_get_bits(&s->gb, buf, buf_size*8);
388     s->bitstream_buffer_size=0;
389
390     if (!s->context_initialized) {
391         if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
392             return -1;
393     }
394
395     /* We need to set current_picture_ptr before reading the header,
396      * otherwise we cannot store anyting in there */
397     if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
398         int i= ff_find_unused_picture(s, 0);
399         s->current_picture_ptr= &s->picture[i];
400     }
401
402     /* let's go :-) */
403     if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5) {
404         ret= ff_wmv2_decode_picture_header(s);
405     } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
406         ret = msmpeg4_decode_picture_header(s);
407     } else if (s->h263_pred) {
408         if(s->avctx->extradata_size && s->picture_number==0){
409             GetBitContext gb;
410
411             init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
412             ret = ff_mpeg4_decode_picture_header(s, &gb);
413         }
414         ret = ff_mpeg4_decode_picture_header(s, &s->gb);
415     } else if (s->codec_id == CODEC_ID_H263I) {
416         ret = intel_h263_decode_picture_header(s);
417     } else if (s->h263_flv) {
418         ret = flv_h263_decode_picture_header(s);
419     } else {
420         ret = h263_decode_picture_header(s);
421     }
422
423     if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
424
425     /* skip if the header was thrashed */
426     if (ret < 0){
427         av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
428         return -1;
429     }
430
431     avctx->has_b_frames= !s->low_delay;
432
433     if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
434         if(s->stream_codec_tag == AV_RL32("XVID") ||
435            s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") ||
436            s->codec_tag == AV_RL32("RMP4"))
437             s->xvid_build= -1;
438 #if 0
439         if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
440            && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
441             s->xvid_build= -1;
442 #endif
443     }
444
445     if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
446         if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
447             s->divx_version= 400; //divx 4
448     }
449
450     if(s->xvid_build && s->divx_version){
451         s->divx_version=
452         s->divx_build= 0;
453     }
454
455     if(s->workaround_bugs&FF_BUG_AUTODETECT){
456         if(s->codec_tag == AV_RL32("XVIX"))
457             s->workaround_bugs|= FF_BUG_XVID_ILACE;
458
459         if(s->codec_tag == AV_RL32("UMP4")){
460             s->workaround_bugs|= FF_BUG_UMP4;
461         }
462
463         if(s->divx_version>=500 && s->divx_build<1814){
464             s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
465         }
466
467         if(s->divx_version>502 && s->divx_build<1814){
468             s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
469         }
470
471         if(s->xvid_build && s->xvid_build<=3)
472             s->padding_bug_score= 256*256*256*64;
473
474         if(s->xvid_build && s->xvid_build<=1)
475             s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
476
477         if(s->xvid_build && s->xvid_build<=12)
478             s->workaround_bugs|= FF_BUG_EDGE;
479
480         if(s->xvid_build && s->xvid_build<=32)
481             s->workaround_bugs|= FF_BUG_DC_CLIP;
482
483 #define SET_QPEL_FUNC(postfix1, postfix2) \
484     s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
485     s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
486     s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
487
488         if(s->lavc_build && s->lavc_build<4653)
489             s->workaround_bugs|= FF_BUG_STD_QPEL;
490
491         if(s->lavc_build && s->lavc_build<4655)
492             s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
493
494         if(s->lavc_build && s->lavc_build<4670){
495             s->workaround_bugs|= FF_BUG_EDGE;
496         }
497
498         if(s->lavc_build && s->lavc_build<=4712)
499             s->workaround_bugs|= FF_BUG_DC_CLIP;
500
501         if(s->divx_version)
502             s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
503 //printf("padding_bug_score: %d\n", s->padding_bug_score);
504         if(s->divx_version==501 && s->divx_build==20020416)
505             s->padding_bug_score= 256*256*256*64;
506
507         if(s->divx_version && s->divx_version<500){
508             s->workaround_bugs|= FF_BUG_EDGE;
509         }
510
511         if(s->divx_version)
512             s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
513 #if 0
514         if(s->divx_version==500)
515             s->padding_bug_score= 256*256*256*64;
516
517         /* very ugly XVID padding bug detection FIXME/XXX solve this differently
518          * Let us hope this at least works.
519          */
520         if(   s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
521            && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
522             s->workaround_bugs|= FF_BUG_NO_PADDING;
523
524         if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
525             s->workaround_bugs|= FF_BUG_NO_PADDING;
526 #endif
527     }
528
529     if(s->workaround_bugs& FF_BUG_STD_QPEL){
530         SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
531         SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
532         SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
533         SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
534         SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
535         SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
536
537         SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
538         SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
539         SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
540         SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
541         SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
542         SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
543     }
544
545     if(avctx->debug & FF_DEBUG_BUGS)
546         av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
547                s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
548                s->divx_packed ? "p" : "");
549
550 #if 0 // dump bits per frame / qp / complexity
551 {
552     static FILE *f=NULL;
553     if(!f) f=fopen("rate_qp_cplx.txt", "w");
554     fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
555 }
556 #endif
557
558 #if HAVE_MMX
559     if(s->codec_id == CODEC_ID_MPEG4 && s->xvid_build && avctx->idct_algo == FF_IDCT_AUTO && (mm_flags & FF_MM_MMX)){
560         avctx->idct_algo= FF_IDCT_XVIDMMX;
561         avctx->coded_width= 0; // force reinit
562 //        dsputil_init(&s->dsp, avctx);
563         s->picture_number=0;
564     }
565 #endif
566
567         /* After H263 & mpeg4 header decode we have the height, width,*/
568         /* and other parameters. So then we could init the picture   */
569         /* FIXME: By the way H263 decoder is evolving it should have */
570         /* an H263EncContext                                         */
571
572     if (   s->width  != avctx->coded_width
573         || s->height != avctx->coded_height) {
574         /* H.263 could change picture size any time */
575         ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
576         s->parse_context.buffer=0;
577         MPV_common_end(s);
578         s->parse_context= pc;
579     }
580     if (!s->context_initialized) {
581         avcodec_set_dimensions(avctx, s->width, s->height);
582
583         goto retry;
584     }
585
586     if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P || s->codec_id == CODEC_ID_H263I))
587         s->gob_index = ff_h263_get_gob_height(s);
588
589     // for hurry_up==5
590     s->current_picture.pict_type= s->pict_type;
591     s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
592
593     /* skip B-frames if we don't have reference frames */
594     if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size);
595     /* skip b frames if we are in a hurry */
596     if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return get_consumed_bytes(s, buf_size);
597     if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
598        || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
599        ||  avctx->skip_frame >= AVDISCARD_ALL)
600         return get_consumed_bytes(s, buf_size);
601     /* skip everything if we are in a hurry>=5 */
602     if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
603
604     if(s->next_p_frame_damaged){
605         if(s->pict_type==FF_B_TYPE)
606             return get_consumed_bytes(s, buf_size);
607         else
608             s->next_p_frame_damaged=0;
609     }
610
611     if((s->avctx->flags2 & CODEC_FLAG2_FAST) && s->pict_type==FF_B_TYPE){
612         s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
613         s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
614     }else if((!s->no_rounding) || s->pict_type==FF_B_TYPE){
615         s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
616         s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
617     }else{
618         s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
619         s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
620     }
621
622     if(MPV_frame_start(s, avctx) < 0)
623         return -1;
624
625     if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
626         ff_vdpau_mpeg4_decode_picture(s, buf, buf_size);
627         goto frame_end;
628     }
629
630     if (avctx->hwaccel) {
631         if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
632             return -1;
633     }
634
635     ff_er_frame_start(s);
636
637     //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
638     //which is not available before MPV_frame_start()
639     if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){
640         ret = ff_wmv2_decode_secondary_picture_header(s);
641         if(ret<0) return ret;
642         if(ret==1) goto intrax8_decoded;
643     }
644
645     /* decode each macroblock */
646     s->mb_x=0;
647     s->mb_y=0;
648
649     decode_slice(s);
650     while(s->mb_y<s->mb_height){
651         if(s->msmpeg4_version){
652             if(s->slice_height==0 || s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
653                 break;
654         }else{
655             if(ff_h263_resync(s)<0)
656                 break;
657         }
658
659         if(s->msmpeg4_version<4 && s->h263_pred)
660             ff_mpeg4_clean_buffers(s);
661
662         decode_slice(s);
663     }
664
665     if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==FF_I_TYPE)
666         if(!CONFIG_MSMPEG4_DECODER || msmpeg4_decode_ext_header(s, buf_size) < 0){
667             s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
668         }
669
670     /* divx 5.01+ bistream reorder stuff */
671     if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
672         int current_pos= get_bits_count(&s->gb)>>3;
673         int startcode_found=0;
674
675         if(buf_size - current_pos > 5){
676             int i;
677             for(i=current_pos; i<buf_size-3; i++){
678                 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
679                     startcode_found=1;
680                     break;
681                 }
682             }
683         }
684         if(s->gb.buffer == s->bitstream_buffer && buf_size>20){ //xvid style
685             startcode_found=1;
686             current_pos=0;
687         }
688
689         if(startcode_found){
690             av_fast_malloc(
691                 &s->bitstream_buffer,
692                 &s->allocated_bitstream_buffer_size,
693                 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
694             if (!s->bitstream_buffer)
695                 return AVERROR(ENOMEM);
696             memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
697             s->bitstream_buffer_size= buf_size - current_pos;
698         }
699     }
700
701 intrax8_decoded:
702     ff_er_frame_end(s);
703
704 frame_end:
705     if (avctx->hwaccel) {
706         if (avctx->hwaccel->end_frame(avctx) < 0)
707             return -1;
708     }
709
710     MPV_frame_end(s);
711
712 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
713 assert(s->current_picture.pict_type == s->pict_type);
714     if (s->pict_type == FF_B_TYPE || s->low_delay) {
715         *pict= *(AVFrame*)s->current_picture_ptr;
716     } else if (s->last_picture_ptr != NULL) {
717         *pict= *(AVFrame*)s->last_picture_ptr;
718     }
719
720     if(s->last_picture_ptr || s->low_delay){
721         *data_size = sizeof(AVFrame);
722         ff_print_debug_info(s, pict);
723     }
724
725 #ifdef PRINT_FRAME_TIME
726 av_log(avctx, AV_LOG_DEBUG, "%"PRId64"\n", rdtsc()-time);
727 #endif
728
729     return get_consumed_bytes(s, buf_size);
730 }
731
732 AVCodec mpeg4_decoder = {
733     "mpeg4",
734     CODEC_TYPE_VIDEO,
735     CODEC_ID_MPEG4,
736     sizeof(MpegEncContext),
737     ff_h263_decode_init,
738     NULL,
739     ff_h263_decode_end,
740     ff_h263_decode_frame,
741     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
742     .flush= ff_mpeg_flush,
743     .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
744     .pix_fmts= ff_hwaccel_pixfmt_list_420,
745 };
746
747 AVCodec h263_decoder = {
748     "h263",
749     CODEC_TYPE_VIDEO,
750     CODEC_ID_H263,
751     sizeof(MpegEncContext),
752     ff_h263_decode_init,
753     NULL,
754     ff_h263_decode_end,
755     ff_h263_decode_frame,
756     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
757     .flush= ff_mpeg_flush,
758     .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
759     .pix_fmts= ff_hwaccel_pixfmt_list_420,
760 };
761
762 AVCodec msmpeg4v1_decoder = {
763     "msmpeg4v1",
764     CODEC_TYPE_VIDEO,
765     CODEC_ID_MSMPEG4V1,
766     sizeof(MpegEncContext),
767     ff_h263_decode_init,
768     NULL,
769     ff_h263_decode_end,
770     ff_h263_decode_frame,
771     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
772     .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
773     .pix_fmts= ff_pixfmt_list_420,
774 };
775
776 AVCodec msmpeg4v2_decoder = {
777     "msmpeg4v2",
778     CODEC_TYPE_VIDEO,
779     CODEC_ID_MSMPEG4V2,
780     sizeof(MpegEncContext),
781     ff_h263_decode_init,
782     NULL,
783     ff_h263_decode_end,
784     ff_h263_decode_frame,
785     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
786     .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
787     .pix_fmts= ff_pixfmt_list_420,
788 };
789
790 AVCodec msmpeg4v3_decoder = {
791     "msmpeg4",
792     CODEC_TYPE_VIDEO,
793     CODEC_ID_MSMPEG4V3,
794     sizeof(MpegEncContext),
795     ff_h263_decode_init,
796     NULL,
797     ff_h263_decode_end,
798     ff_h263_decode_frame,
799     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
800     .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
801     .pix_fmts= ff_pixfmt_list_420,
802 };
803
804 AVCodec wmv1_decoder = {
805     "wmv1",
806     CODEC_TYPE_VIDEO,
807     CODEC_ID_WMV1,
808     sizeof(MpegEncContext),
809     ff_h263_decode_init,
810     NULL,
811     ff_h263_decode_end,
812     ff_h263_decode_frame,
813     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
814     .long_name= NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
815     .pix_fmts= ff_pixfmt_list_420,
816 };
817
818 AVCodec h263i_decoder = {
819     "h263i",
820     CODEC_TYPE_VIDEO,
821     CODEC_ID_H263I,
822     sizeof(MpegEncContext),
823     ff_h263_decode_init,
824     NULL,
825     ff_h263_decode_end,
826     ff_h263_decode_frame,
827     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
828     .long_name = NULL_IF_CONFIG_SMALL("Intel H.263"),
829     .pix_fmts= ff_pixfmt_list_420,
830 };
831
832 AVCodec flv_decoder = {
833     "flv",
834     CODEC_TYPE_VIDEO,
835     CODEC_ID_FLV1,
836     sizeof(MpegEncContext),
837     ff_h263_decode_init,
838     NULL,
839     ff_h263_decode_end,
840     ff_h263_decode_frame,
841     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
842     .long_name= NULL_IF_CONFIG_SMALL("Flash Video (FLV) / Sorenson Spark / Sorenson H.263"),
843     .pix_fmts= ff_pixfmt_list_420,
844 };
845
846 #if CONFIG_MPEG4_VDPAU_DECODER
847 AVCodec mpeg4_vdpau_decoder = {
848     "mpeg4_vdpau",
849     CODEC_TYPE_VIDEO,
850     CODEC_ID_MPEG4,
851     sizeof(MpegEncContext),
852     ff_h263_decode_init,
853     NULL,
854     ff_h263_decode_end,
855     ff_h263_decode_frame,
856     CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
857     .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
858     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_VDPAU_MPEG4, PIX_FMT_NONE},
859 };
860 #endif