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