3 * Copyright (c) 2001 Fabrice Bellard.
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.
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.
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
21 #include "mpegvideo.h"
24 #define PRINT_QP(a, b) {}
26 #define PRINT_QP(a, b) printf(a, b)
30 //#define PRINT_FRAME_TIME
31 #ifdef PRINT_FRAME_TIME
32 static inline long long rdtsc()
35 asm volatile( "rdtsc\n\t"
38 // printf("%d\n", int(l/1000));
43 static int h263_decode_init(AVCodecContext *avctx)
45 MpegEncContext *s = avctx->priv_data;
48 s->out_format = FMT_H263;
50 s->width = avctx->width;
51 s->height = avctx->height;
52 s->workaround_bugs= avctx->workaround_bugs;
56 s->progressive_sequence=1;
57 s->decode_mb= ff_h263_decode_mb;
59 /* select sub codec */
60 switch(avctx->codec->id) {
65 s->time_increment_bits = 4; /* default value for broken headers */
67 s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
69 case CODEC_ID_MSMPEG4V1:
74 case CODEC_ID_MSMPEG4V2:
79 case CODEC_ID_MSMPEG4V3:
100 s->codec_id= avctx->codec->id;
102 /* for h263, we allocate the images after having read the header */
103 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
104 if (MPV_common_init(s) < 0)
108 ff_msmpeg4_decode_init(s);
110 h263_decode_init_vlc(s);
115 static int h263_decode_end(AVCodecContext *avctx)
117 MpegEncContext *s = avctx->priv_data;
124 * retunrs the number of bytes consumed for building the current frame
126 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
127 int pos= (get_bits_count(&s->gb)+7)>>3;
128 if(s->divx_version>=500){
129 //we would have to scan through the whole buf to handle the weird reordering ...
132 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
133 if(pos+10>buf_size) pos=buf_size; // oops ;)
139 static int decode_slice(MpegEncContext *s){
140 s->last_resync_gb= s->gb;
141 s->first_slice_line= 1;
143 s->resync_mb_x= s->mb_x;
144 s->resync_mb_y= s->mb_y;
146 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
147 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
149 if(s->partitioned_frame){
150 const int qscale= s->qscale;
152 if(s->codec_id==CODEC_ID_MPEG4){
153 if(ff_mpeg4_decode_partitions(s) < 0)
157 /* restore variables which where modified */
158 s->first_slice_line=1;
159 s->mb_x= s->resync_mb_x;
160 s->mb_y= s->resync_mb_y;
162 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
163 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
166 for(; s->mb_y < s->mb_height; s->mb_y++) {
167 /* per-row end of slice checks */
168 if(s->msmpeg4_version){
169 if(s->resync_mb_y + s->slice_height == s->mb_y){
170 const int xy= s->mb_x + s->mb_y*s->mb_width;
171 s->error_status_table[xy-1]|= AC_END|DC_END|MV_END;
176 if(s->msmpeg4_version==1){
182 ff_init_block_index(s);
183 for(; s->mb_x < s->mb_width; s->mb_x++) {
186 ff_update_block_index(s);
188 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
189 s->first_slice_line=0;
193 clear_blocks(s->block[0]);
195 s->mv_dir = MV_DIR_FORWARD;
196 s->mv_type = MV_TYPE_16X16;
197 //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
198 ret= s->decode_mb(s, s->block);
200 PRINT_QP("%2d", s->qscale);
201 MPV_decode_mb(s, s->block);
204 const int xy= s->mb_x + s->mb_y*s->mb_width;
206 //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));
207 s->error_status_table[xy]|= AC_END;
208 if(!s->partitioned_frame)
209 s->error_status_table[xy]|= MV_END|DC_END;
211 s->padding_bug_score--;
213 if(++s->mb_x >= s->mb_width){
215 ff_draw_horiz_band(s);
219 }else if(ret==SLICE_NOEND){
220 fprintf(stderr,"Slice mismatch at MB: %d\n", xy);
223 fprintf(stderr,"Error at MB: %d\n", xy);
224 s->error_status_table[xy]|= AC_ERROR;
225 if(!s->partitioned_frame)
226 s->error_status_table[xy]|= DC_ERROR|MV_ERROR;
232 ff_draw_horiz_band(s);
234 PRINT_QP("%s", "\n");
239 assert(s->mb_x==0 && s->mb_y==s->mb_height);
241 /* try to detect the padding bug */
242 if( s->codec_id==CODEC_ID_MPEG4
243 && (s->workaround_bugs&FF_BUG_AUTODETECT)
244 && s->gb.size*8 - get_bits_count(&s->gb) >=0
245 && s->gb.size*8 - get_bits_count(&s->gb) < 48
247 && !s->data_partitioning){
249 const int bits_count= get_bits_count(&s->gb);
250 const int bits_left = s->gb.size*8 - bits_count;
252 if(bits_left==0 || bits_left>8){
253 s->padding_bug_score++;
254 } else if(bits_left != 1){
255 int v= show_bits(&s->gb, 8);
256 v|= 0x7F >> (7-(bits_count&7));
259 s->padding_bug_score--;
261 s->padding_bug_score++;
264 if(s->padding_bug_score > -2)
265 s->workaround_bugs |= FF_BUG_NO_PADDING;
267 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
270 // handle formats which dont have unique end markers
271 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
272 int left= s->gb.size*8 - get_bits_count(&s->gb);
275 /* no markers in M$ crap */
276 if(s->msmpeg4_version && s->pict_type==I_TYPE)
279 /* buggy padding but the frame should still end approximately at the bitstream end */
280 if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3)
282 else if((s->workaround_bugs&FF_BUG_NO_PADDING))
283 max_extra+= 256*256*256*64;
286 fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
289 fprintf(stderr, "overreading %d bits\n", -left);
291 s->error_status_table[s->mb_num-1]|= AC_END|MV_END|DC_END;
296 fprintf(stderr, "slice end not reached but screenspace end (%d left %06X)\n",
297 s->gb.size*8 - get_bits_count(&s->gb),
298 show_bits(&s->gb, 24));
302 static int h263_decode_frame(AVCodecContext *avctx,
303 void *data, int *data_size,
304 UINT8 *buf, int buf_size)
306 MpegEncContext *s = avctx->priv_data;
308 AVPicture *pict = data;
309 #ifdef PRINT_FRAME_TIME
310 uint64_t time= rdtsc();
313 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
314 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
317 s->hurry_up= avctx->hurry_up;
318 s->error_resilience= avctx->error_resilience;
320 s->flags= avctx->flags;
324 /* no supplementary picture */
331 if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
332 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
334 init_get_bits(&s->gb, buf, buf_size);
335 s->bitstream_buffer_size=0;
337 if (!s->context_initialized) {
338 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
343 if (s->h263_msmpeg4) {
344 ret = msmpeg4_decode_picture_header(s);
345 } else if (s->h263_pred) {
346 if(s->avctx->extradata_size && s->picture_number==0){
349 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size);
350 ret = ff_mpeg4_decode_picture_header(s, &gb);
352 ret = ff_mpeg4_decode_picture_header(s, &s->gb);
354 if(s->flags& CODEC_FLAG_LOW_DELAY)
357 s->has_b_frames= !s->low_delay;
358 } else if (s->h263_intel) {
359 ret = intel_h263_decode_picture_header(s);
361 ret = h263_decode_picture_header(s);
363 avctx->has_b_frames= s->has_b_frames;
365 if(s->workaround_bugs&FF_BUG_AUTODETECT){
366 if(s->avctx->fourcc == ff_get_fourcc("XVIX"))
367 s->workaround_bugs|= FF_BUG_XVID_ILACE;
369 if(s->avctx->fourcc == ff_get_fourcc("MP4S"))
370 s->workaround_bugs|= FF_BUG_AC_VLC;
372 if(s->avctx->fourcc == ff_get_fourcc("M4S2"))
373 s->workaround_bugs|= FF_BUG_AC_VLC;
375 if(s->avctx->fourcc == ff_get_fourcc("UMP4")){
376 s->workaround_bugs|= FF_BUG_UMP4;
377 s->workaround_bugs|= FF_BUG_AC_VLC;
381 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
384 if(s->avctx->fourcc == ff_get_fourcc("XVID") && s->xvid_build==0)
385 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
387 if(s->xvid_build && s->xvid_build<=1)
388 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
390 //printf("padding_bug_score: %d\n", s->padding_bug_score);
392 if(s->divx_version==500)
393 s->workaround_bugs|= FF_BUG_NO_PADDING;
395 /* very ugly XVID padding bug detection FIXME/XXX solve this differently
396 * lets hope this at least works
398 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
399 && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
400 s->workaround_bugs|= FF_BUG_NO_PADDING;
402 if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
403 s->workaround_bugs|= FF_BUG_NO_PADDING;
408 #if 0 // dump bits per frame / qp / complexity
411 if(!f) f=fopen("rate_qp_cplx.txt", "w");
412 fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
416 /* After H263 & mpeg4 header decode we have the height, width,*/
417 /* and other parameters. So then we could init the picture */
418 /* FIXME: By the way H263 decoder is evolving it should have */
419 /* an H263EncContext */
420 if ( s->width != avctx->width || s->height != avctx->height
421 || avctx->aspect_ratio_info != s->aspect_ratio_info
422 || avctx->aspected_width != s->aspected_width
423 || avctx->aspected_height != s->aspected_height) {
424 /* H.263 could change picture size any time */
426 s->context_initialized=0;
428 if (!s->context_initialized) {
429 avctx->width = s->width;
430 avctx->height = s->height;
431 avctx->aspect_ratio_info= s->aspect_ratio_info;
432 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED)
434 avctx->aspected_width = s->aspected_width;
435 avctx->aspected_height = s->aspected_height;
441 if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
442 s->gob_index = ff_h263_get_gob_height(s);
444 if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
445 /* skip if the header was thrashed */
447 fprintf(stderr, "header damaged\n");
450 /* skip b frames if we dont have reference frames */
451 if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
452 /* skip b frames if we are in a hurry */
453 if(s->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
455 if(s->next_p_frame_damaged){
456 if(s->pict_type==B_TYPE)
457 return get_consumed_bytes(s, buf_size);
459 s->next_p_frame_damaged=0;
462 if(MPV_frame_start(s, avctx) < 0)
466 printf("qscale=%d\n", s->qscale);
469 if(s->error_resilience)
470 memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_num*sizeof(UINT8));
472 /* decode each macroblock */
476 s->block_wrap[3]= s->mb_width*2 + 2;
478 s->block_wrap[5]= s->mb_width + 2;
483 s->error_status_table[0]|= VP_START;
484 while(s->mb_y<s->mb_height && s->gb.size*8 - get_bits_count(&s->gb)>16){
485 if(s->msmpeg4_version){
486 if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0)
489 if(ff_h263_resync(s)<0)
493 if(s->msmpeg4_version!=4 && s->h263_pred)
494 ff_mpeg4_clean_buffers(s);
498 s->error_status_table[s->resync_mb_x + s->resync_mb_y*s->mb_width]|= VP_START;
501 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
502 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
504 /* divx 5.01+ bistream reorder stuff */
505 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_version>=500){
506 int current_pos= get_bits_count(&s->gb)>>3;
508 if( buf_size - current_pos > 5
509 && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
511 int startcode_found=0;
512 for(i=current_pos; i<buf_size-3; i++){
513 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
519 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
520 s->bitstream_buffer_size= buf_size - current_pos;
525 if(s->error_resilience){
526 int error=0, num_end_markers=0;
527 for(i=0; i<s->mb_num; i++){
528 int status= s->error_status_table[i];
530 if(i%s->mb_width == 0) printf("\n");
531 printf("%2X ", status);
533 if(status==0) continue;
535 if(status&(DC_ERROR|AC_ERROR|MV_ERROR))
549 if(num_end_markers || error){
550 fprintf(stderr, "concealing errors\n");
551 //printf("type:%d\n", s->pict_type);
552 ff_error_resilience(s);
557 #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
561 for(mb_y=0; mb_y<s->mb_height; mb_y++){
564 for(mb_x=0; mb_x<s->mb_width; mb_x++){
566 uint8_t *ptr= s->last_picture[0];
567 int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
568 int mx= (s->motion_val[xy][0]>>1) + x;
569 int my= (s->motion_val[xy][1]>>1) + y;
575 if(mx>=s->width) mx= s->width -1;
576 if(my>=s->height) my= s->height-1;
578 if(ABS(my-y) > max) max= ABS(my-y);
579 /* the ugliest linedrawing routine ... */
580 for(i=0; i<max; i++){
581 int x1= x + (mx-x)*i/max;
582 int y1= y + (my-y)*i/max;
583 ptr[y1*s->linesize + x1]+=100;
585 ptr[y*s->linesize + x]+=100;
586 s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
592 if(s->pict_type==B_TYPE || (!s->has_b_frames)){
593 pict->data[0] = s->current_picture[0];
594 pict->data[1] = s->current_picture[1];
595 pict->data[2] = s->current_picture[2];
597 pict->data[0] = s->last_picture[0];
598 pict->data[1] = s->last_picture[1];
599 pict->data[2] = s->last_picture[2];
601 pict->linesize[0] = s->linesize;
602 pict->linesize[1] = s->uvlinesize;
603 pict->linesize[2] = s->uvlinesize;
605 avctx->quality = s->qscale;
607 /* Return the Picture timestamp as the frame number */
608 /* we substract 1 because it is added on utils.c */
609 avctx->frame_number = s->picture_number - 1;
611 /* dont output the last pic after seeking
612 note we allready added +1 for the current pix in MPV_frame_end(s) */
613 if(s->num_available_buffers>=2 || (!s->has_b_frames))
614 *data_size = sizeof(AVPicture);
615 #ifdef PRINT_FRAME_TIME
616 printf("%Ld\n", rdtsc()-time);
618 return get_consumed_bytes(s, buf_size);
621 AVCodec mpeg4_decoder = {
625 sizeof(MpegEncContext),
630 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
633 AVCodec h263_decoder = {
637 sizeof(MpegEncContext),
642 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
645 AVCodec msmpeg4v1_decoder = {
649 sizeof(MpegEncContext),
654 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
657 AVCodec msmpeg4v2_decoder = {
661 sizeof(MpegEncContext),
666 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
669 AVCodec msmpeg4v3_decoder = {
673 sizeof(MpegEncContext),
678 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
681 AVCodec wmv1_decoder = {
685 sizeof(MpegEncContext),
690 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
693 AVCodec wmv2_decoder = {
697 sizeof(MpegEncContext),
702 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
705 AVCodec h263i_decoder = {
709 sizeof(MpegEncContext),
714 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,