]> git.sesse.net Git - ffmpeg/blob - libavcodec/h263dec.c
support buffers containing more than one frame
[ffmpeg] / libavcodec / h263dec.c
1 /*
2  * H263 decoder
3  * Copyright (c) 2001 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 #include "avcodec.h"
20 #include "dsputil.h"
21 #include "mpegvideo.h"
22
23 //#define DEBUG
24 //#define PRINT_FRAME_TIME
25 #ifdef PRINT_FRAME_TIME
26 static inline long long rdtsc()
27 {
28         long long l;
29         asm volatile(   "rdtsc\n\t"
30                 : "=A" (l)
31         );
32 //      printf("%d\n", int(l/1000));
33         return l;
34 }
35 #endif
36
37 static int h263_decode_init(AVCodecContext *avctx)
38 {
39     MpegEncContext *s = avctx->priv_data;
40
41     s->avctx = avctx;
42     s->out_format = FMT_H263;
43
44     s->width = avctx->width;
45     s->height = avctx->height;
46     s->workaround_bugs= avctx->workaround_bugs;
47
48     /* select sub codec */
49     switch(avctx->codec->id) {
50     case CODEC_ID_H263:
51         s->gob_number = 0;
52         s->first_slice_line = 0;
53         break;
54     case CODEC_ID_MPEG4:
55         s->time_increment_bits = 4; /* default value for broken headers */
56         s->h263_pred = 1;
57         s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
58         break;
59     case CODEC_ID_MSMPEG4V1:
60         s->h263_msmpeg4 = 1;
61         s->h263_pred = 1;
62         s->msmpeg4_version=1;
63         break;
64     case CODEC_ID_MSMPEG4V2:
65         s->h263_msmpeg4 = 1;
66         s->h263_pred = 1;
67         s->msmpeg4_version=2;
68         break;
69     case CODEC_ID_MSMPEG4V3:
70         s->h263_msmpeg4 = 1;
71         s->h263_pred = 1;
72         s->msmpeg4_version=3;
73         break;
74     case CODEC_ID_WMV1:
75         s->h263_msmpeg4 = 1;
76         s->h263_pred = 1;
77         s->msmpeg4_version=4;
78         break;
79     case CODEC_ID_WMV2:
80         s->h263_msmpeg4 = 1;
81         s->h263_pred = 1;
82         s->msmpeg4_version=5;
83         break;
84     case CODEC_ID_H263I:
85         s->h263_intel = 1;
86         break;
87     default:
88         return -1;
89     }
90     s->codec_id= avctx->codec->id;
91
92     /* for h263, we allocate the images after having read the header */
93     if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
94         if (MPV_common_init(s) < 0)
95             return -1;
96
97     if (s->h263_msmpeg4)
98         ff_msmpeg4_decode_init(s);
99     else
100         h263_decode_init_vlc(s);
101     
102     return 0;
103 }
104
105 static int h263_decode_end(AVCodecContext *avctx)
106 {
107     MpegEncContext *s = avctx->priv_data;
108
109     MPV_common_end(s);
110     return 0;
111 }
112
113 /**
114  * retunrs the number of bytes consumed for building the current frame
115  */
116 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
117     int pos= (get_bits_count(&s->gb)+7)>>3;
118
119     if(s->divx_version>=500){
120         //we would have to scan through the whole buf to handle the weird reordering ...
121         return buf_size; 
122     }else{
123         if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
124         if(pos+10>buf_size) pos=buf_size; // oops ;)
125
126         return pos;
127     }
128 }
129
130 static int h263_decode_frame(AVCodecContext *avctx, 
131                              void *data, int *data_size,
132                              UINT8 *buf, int buf_size)
133 {
134     MpegEncContext *s = avctx->priv_data;
135     int ret;
136     AVPicture *pict = data; 
137 #ifdef PRINT_FRAME_TIME
138 uint64_t time= rdtsc();
139 #endif
140 #ifdef DEBUG
141     printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
142     printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
143 #endif
144
145     s->hurry_up= avctx->hurry_up;
146     s->error_resilience= avctx->error_resilience;
147
148     s->workaround_bugs= avctx->workaround_bugs;
149     if(s->avctx->fourcc == ff_get_fourcc("XVIX") && s->workaround_bugs==0) 
150         s->workaround_bugs=2;
151
152     s->flags= avctx->flags;
153
154     *data_size = 0;
155    
156    /* no supplementary picture */
157     if (buf_size == 0) {
158         return 0;
159     }
160
161     if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
162         init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
163     }else
164         init_get_bits(&s->gb, buf, buf_size);
165     s->bitstream_buffer_size=0;
166
167     if (!s->context_initialized) {
168         if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
169             return -1;
170     }
171         
172     /* let's go :-) */
173     if (s->h263_msmpeg4) {
174         ret = msmpeg4_decode_picture_header(s);
175     } else if (s->h263_pred) {
176         ret = mpeg4_decode_picture_header(s);
177         s->has_b_frames= !s->low_delay;
178     } else if (s->h263_intel) {
179         ret = intel_h263_decode_picture_header(s);
180     } else {
181         ret = h263_decode_picture_header(s);
182     }
183     avctx->has_b_frames= s->has_b_frames;
184 #if 0 // dump bits per frame / qp / complexity
185 {
186     static FILE *f=NULL;
187     if(!f) f=fopen("rate_qp_cplx.txt", "w");
188     fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
189 }
190 #endif
191        
192         /* After H263 & mpeg4 header decode we have the height, width,*/
193         /* and other parameters. So then we could init the picture   */
194         /* FIXME: By the way H263 decoder is evolving it should have */
195         /* an H263EncContext                                         */
196     if (   s->width != avctx->width || s->height != avctx->height 
197         || avctx->aspect_ratio_info != s->aspect_ratio_info
198         || avctx->aspected_width != s->aspected_width
199         || avctx->aspected_height != s->aspected_height) {
200         /* H.263 could change picture size any time */
201         MPV_common_end(s);
202         s->context_initialized=0;
203     }
204     if (!s->context_initialized) {
205         avctx->width = s->width;
206         avctx->height = s->height;
207         avctx->aspect_ratio_info= s->aspect_ratio_info;
208         if (s->aspect_ratio_info == FF_ASPECT_EXTENDED)
209         {
210             avctx->aspected_width = s->aspected_width;
211             avctx->aspected_height = s->aspected_height;
212         }
213         if (MPV_common_init(s) < 0)
214             return -1;
215     }
216
217     if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
218     /* skip if the header was thrashed */
219     if (ret < 0){
220         fprintf(stderr, "header damaged\n");
221         return -1;
222     }
223     /* skip b frames if we dont have reference frames */
224     if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
225     /* skip b frames if we are in a hurry */
226     if(s->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
227     
228     if(s->next_p_frame_damaged){
229         if(s->pict_type==B_TYPE)
230             return get_consumed_bytes(s, buf_size);
231         else
232             s->next_p_frame_damaged=0;
233     }
234
235     MPV_frame_start(s, avctx);
236
237 #ifdef DEBUG
238     printf("qscale=%d\n", s->qscale);
239 #endif
240
241     /* init resync/ error resilience specific variables */
242     s->next_resync_qscale= s->qscale;
243     s->next_resync_gb= s->gb;
244     if(s->resync_marker) s->mb_num_left= 0;
245     else                 s->mb_num_left= s->mb_num;
246
247     /* decode each macroblock */
248     s->block_wrap[0]=
249     s->block_wrap[1]=
250     s->block_wrap[2]=
251     s->block_wrap[3]= s->mb_width*2 + 2;
252     s->block_wrap[4]=
253     s->block_wrap[5]= s->mb_width + 2;
254     for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
255         /* Check for GOB headers on H.263 */
256         /* FIXME: In the future H.263+ will have intra prediction */
257         /* and we are gonna need another way to detect MPEG4      */
258         if (s->mb_y && !s->h263_pred) {
259             s->first_slice_line = h263_decode_gob_header(s);
260         }
261         
262         if(s->msmpeg4_version==1){
263             s->last_dc[0]=
264             s->last_dc[1]=
265             s->last_dc[2]= 128;
266         }
267
268         s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
269         s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
270
271         s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
272         s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
273         s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
274         s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
275         s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
276         s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
277         for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
278             s->block_index[0]+=2;
279             s->block_index[1]+=2;
280             s->block_index[2]+=2;
281             s->block_index[3]+=2;
282             s->block_index[4]++;
283             s->block_index[5]++;
284 #ifdef DEBUG
285             printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
286 #endif
287
288             if(s->resync_marker){
289                 if(s->mb_num_left<=0){
290                     /* except the first block */
291                     if(s->mb_x!=0 || s->mb_y!=0){
292                         /* did we miss the next resync marker without noticing an error yet */
293                         if(((get_bits_count(&s->gb)+8)&(~7)) != s->next_resync_pos && s->decoding_error==0){
294                             fprintf(stderr, "slice end missmatch x:%d y:%d %d %d\n", 
295                                     s->mb_x, s->mb_y, get_bits_count(&s->gb), s->next_resync_pos);
296                             ff_conceal_past_errors(s, 1);
297                         }
298                     }
299                     s->qscale= s->next_resync_qscale;
300                     s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
301                     s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
302
303                     s->gb= s->next_resync_gb;
304                     s->resync_mb_x= s->mb_x; //we know that the marker is here cuz mb_num_left was the distance to it
305                     s->resync_mb_y= s->mb_y;
306                     s->first_slice_line=1;
307
308                     if(s->codec_id==CODEC_ID_MPEG4){
309                         ff_mpeg4_clean_buffers(s);
310                         ff_mpeg4_resync(s);
311                     }
312                 }
313
314                 if(   s->resync_mb_x==s->mb_x 
315                    && s->resync_mb_y==s->mb_y && s->decoding_error!=0){
316                     fprintf(stderr, "resynced at %d %d\n", s->mb_x, s->mb_y);
317                     s->decoding_error= 0;
318                 }
319             }
320
321             //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
322             /* DCT & quantize */
323             if(s->decoding_error!=DECODING_DESYNC){
324                 int last_error= s->decoding_error;
325                 clear_blocks(s->block[0]);
326             
327                 s->mv_dir = MV_DIR_FORWARD;
328                 s->mv_type = MV_TYPE_16X16;
329                 if (s->h263_msmpeg4) {
330                     if (msmpeg4_decode_mb(s, s->block) < 0) {
331                         fprintf(stderr,"Error at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
332                         s->decoding_error=DECODING_DESYNC;
333                     }
334                 } else {
335                     if (h263_decode_mb(s, s->block) < 0) {
336                         fprintf(stderr,"Error at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
337                         s->decoding_error=DECODING_DESYNC;
338                     }
339                 }
340
341                 if(s->decoding_error!=last_error){
342                     ff_conceal_past_errors(s, 0);
343                 }
344             }
345
346             /* conceal errors */
347             if(    s->decoding_error==DECODING_DESYNC
348                || (s->decoding_error==DECODING_ACDC_LOST && s->mb_intra)){
349                 s->mv_dir = MV_DIR_FORWARD;
350                 s->mv_type = MV_TYPE_16X16;
351                 s->mb_skiped=0;
352                 s->mb_intra=0;
353                 s->mv[0][0][0]=0; //FIXME this is not optimal 
354                 s->mv[0][0][1]=0;
355                 clear_blocks(s->block[0]);
356             }else if(s->decoding_error && !s->mb_intra){
357                 clear_blocks(s->block[0]);
358             }
359             //FIXME remove AC for intra
360                         
361             MPV_decode_mb(s, s->block);
362
363             s->mb_num_left--;            
364         }
365         if (    avctx->draw_horiz_band 
366             && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
367             UINT8 *src_ptr[3];
368             int y, h, offset;
369             y = s->mb_y * 16;
370             h = s->height - y;
371             if (h > 16)
372                 h = 16;
373
374             if(s->pict_type==B_TYPE)
375                 offset = 0;
376             else
377                 offset = y * s->linesize;
378
379             if(s->pict_type==B_TYPE || (!s->has_b_frames)){
380                 src_ptr[0] = s->current_picture[0] + offset;
381                 src_ptr[1] = s->current_picture[1] + (offset >> 2);
382                 src_ptr[2] = s->current_picture[2] + (offset >> 2);
383             } else {
384                 src_ptr[0] = s->last_picture[0] + offset;
385                 src_ptr[1] = s->last_picture[1] + (offset >> 2);
386                 src_ptr[2] = s->last_picture[2] + (offset >> 2);
387             }
388             avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
389                                    y, s->width, h);
390         }
391     }
392     
393     if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
394         if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
395     
396     /* divx 5.01+ bistream reorder stuff */
397     if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_version>=500){
398         int current_pos= get_bits_count(&s->gb)>>3;
399
400         if(   buf_size - current_pos > 5 
401            && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
402             int i;
403             int startcode_found=0;
404             for(i=current_pos; i<buf_size-3; i++){
405                 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
406                     startcode_found=1;
407                     break;
408                 }
409             }
410             if(startcode_found){
411                 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
412                 s->bitstream_buffer_size= buf_size - current_pos;
413             }
414         }
415     }
416
417     if(s->bitstream_buffer_size==0 && s->error_resilience>0){
418         int left= s->gb.size*8 - get_bits_count(&s->gb);
419         int max_extra=8;
420         
421         if(s->codec_id==CODEC_ID_MPEG4) max_extra+=32;
422
423         if(left>max_extra){
424             fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
425             if(s->decoding_error==0)
426                 ff_conceal_past_errors(s, 1);
427         }
428         if(left<0){
429             fprintf(stderr, "overreading %d bits\n", -left);
430             if(s->decoding_error==0)
431                 ff_conceal_past_errors(s, 1);
432         }
433     }
434   
435     MPV_frame_end(s);
436 #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
437 {
438   int mb_y;
439   s->has_b_frames=1;
440   for(mb_y=0; mb_y<s->mb_height; mb_y++){
441     int mb_x;
442     int y= mb_y*16 + 8;
443     for(mb_x=0; mb_x<s->mb_width; mb_x++){
444       int x= mb_x*16 + 8;
445       uint8_t *ptr= s->last_picture[0];
446       int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
447       int mx= (s->motion_val[xy][0]>>1) + x;
448       int my= (s->motion_val[xy][1]>>1) + y;
449       int i;
450       int max;
451
452       if(mx<0) mx=0;
453       if(my<0) my=0;
454       if(mx>=s->width)  mx= s->width -1;
455       if(my>=s->height) my= s->height-1;
456       max= ABS(mx-x);
457       if(ABS(my-y) > max) max= ABS(my-y);
458       /* the ugliest linedrawing routine ... */
459       for(i=0; i<max; i++){
460         int x1= x + (mx-x)*i/max;
461         int y1= y + (my-y)*i/max;
462         ptr[y1*s->linesize + x1]+=100;
463       }
464       ptr[y*s->linesize + x]+=100;
465       s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
466     }
467   }
468
469 }
470 #endif    
471     if(s->pict_type==B_TYPE || (!s->has_b_frames)){
472         pict->data[0] = s->current_picture[0];
473         pict->data[1] = s->current_picture[1];
474         pict->data[2] = s->current_picture[2];
475     } else {
476         pict->data[0] = s->last_picture[0];
477         pict->data[1] = s->last_picture[1];
478         pict->data[2] = s->last_picture[2];
479     }
480     pict->linesize[0] = s->linesize;
481     pict->linesize[1] = s->uvlinesize;
482     pict->linesize[2] = s->uvlinesize;
483
484     avctx->quality = s->qscale;
485
486     /* Return the Picture timestamp as the frame number */
487     /* we substract 1 because it is added on utils.c    */
488     avctx->frame_number = s->picture_number - 1;
489
490     /* dont output the last pic after seeking 
491        note we allready added +1 for the current pix in MPV_frame_end(s) */
492     if(s->num_available_buffers>=2 || (!s->has_b_frames))
493         *data_size = sizeof(AVPicture);
494 #ifdef PRINT_FRAME_TIME
495 printf("%Ld\n", rdtsc()-time);
496 #endif
497     return get_consumed_bytes(s, buf_size);
498 }
499
500 AVCodec mpeg4_decoder = {
501     "mpeg4",
502     CODEC_TYPE_VIDEO,
503     CODEC_ID_MPEG4,
504     sizeof(MpegEncContext),
505     h263_decode_init,
506     NULL,
507     h263_decode_end,
508     h263_decode_frame,
509     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
510 };
511
512 AVCodec h263_decoder = {
513     "h263",
514     CODEC_TYPE_VIDEO,
515     CODEC_ID_H263,
516     sizeof(MpegEncContext),
517     h263_decode_init,
518     NULL,
519     h263_decode_end,
520     h263_decode_frame,
521     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
522 };
523
524 AVCodec msmpeg4v1_decoder = {
525     "msmpeg4v1",
526     CODEC_TYPE_VIDEO,
527     CODEC_ID_MSMPEG4V1,
528     sizeof(MpegEncContext),
529     h263_decode_init,
530     NULL,
531     h263_decode_end,
532     h263_decode_frame,
533     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
534 };
535
536 AVCodec msmpeg4v2_decoder = {
537     "msmpeg4v2",
538     CODEC_TYPE_VIDEO,
539     CODEC_ID_MSMPEG4V2,
540     sizeof(MpegEncContext),
541     h263_decode_init,
542     NULL,
543     h263_decode_end,
544     h263_decode_frame,
545     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
546 };
547
548 AVCodec msmpeg4v3_decoder = {
549     "msmpeg4",
550     CODEC_TYPE_VIDEO,
551     CODEC_ID_MSMPEG4V3,
552     sizeof(MpegEncContext),
553     h263_decode_init,
554     NULL,
555     h263_decode_end,
556     h263_decode_frame,
557     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
558 };
559
560 AVCodec wmv1_decoder = {
561     "wmv1",
562     CODEC_TYPE_VIDEO,
563     CODEC_ID_WMV1,
564     sizeof(MpegEncContext),
565     h263_decode_init,
566     NULL,
567     h263_decode_end,
568     h263_decode_frame,
569     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
570 };
571
572 AVCodec wmv2_decoder = {
573     "wmv2",
574     CODEC_TYPE_VIDEO,
575     CODEC_ID_WMV2,
576     sizeof(MpegEncContext),
577     h263_decode_init,
578     NULL,
579     h263_decode_end,
580     h263_decode_frame,
581     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
582 };
583
584 AVCodec h263i_decoder = {
585     "h263i",
586     CODEC_TYPE_VIDEO,
587     CODEC_ID_H263I,
588     sizeof(MpegEncContext),
589     h263_decode_init,
590     NULL,
591     h263_decode_end,
592     h263_decode_frame,
593     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
594 };
595