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