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