• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/h263dec.c

Go to the documentation of this file.
00001 /*
00002  * H.263 decoder
00003  * Copyright (c) 2001 Fabrice Bellard
00004  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "internal.h"
00029 #include "avcodec.h"
00030 #include "dsputil.h"
00031 #include "mpegvideo.h"
00032 #include "h263.h"
00033 #include "h263_parser.h"
00034 #include "mpeg4video_parser.h"
00035 #include "msmpeg4.h"
00036 #include "vdpau_internal.h"
00037 #include "flv.h"
00038 #include "mpeg4video.h"
00039 
00040 //#define DEBUG
00041 //#define PRINT_FRAME_TIME
00042 
00043 av_cold int ff_h263_decode_init(AVCodecContext *avctx)
00044 {
00045     MpegEncContext *s = avctx->priv_data;
00046 
00047     s->avctx = avctx;
00048     s->out_format = FMT_H263;
00049 
00050     s->width  = avctx->coded_width;
00051     s->height = avctx->coded_height;
00052     s->workaround_bugs= avctx->workaround_bugs;
00053 
00054     // set defaults
00055     MPV_decode_defaults(s);
00056     s->quant_precision=5;
00057     s->decode_mb= ff_h263_decode_mb;
00058     s->low_delay= 1;
00059     avctx->pix_fmt= avctx->get_format(avctx, avctx->codec->pix_fmts);
00060     s->unrestricted_mv= 1;
00061 
00062     /* select sub codec */
00063     switch(avctx->codec->id) {
00064     case CODEC_ID_H263:
00065         s->unrestricted_mv= 0;
00066         avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
00067         break;
00068     case CODEC_ID_MPEG4:
00069         break;
00070     case CODEC_ID_MSMPEG4V1:
00071         s->h263_msmpeg4 = 1;
00072         s->h263_pred = 1;
00073         s->msmpeg4_version=1;
00074         break;
00075     case CODEC_ID_MSMPEG4V2:
00076         s->h263_msmpeg4 = 1;
00077         s->h263_pred = 1;
00078         s->msmpeg4_version=2;
00079         break;
00080     case CODEC_ID_MSMPEG4V3:
00081         s->h263_msmpeg4 = 1;
00082         s->h263_pred = 1;
00083         s->msmpeg4_version=3;
00084         break;
00085     case CODEC_ID_WMV1:
00086         s->h263_msmpeg4 = 1;
00087         s->h263_pred = 1;
00088         s->msmpeg4_version=4;
00089         break;
00090     case CODEC_ID_WMV2:
00091         s->h263_msmpeg4 = 1;
00092         s->h263_pred = 1;
00093         s->msmpeg4_version=5;
00094         break;
00095     case CODEC_ID_VC1:
00096     case CODEC_ID_WMV3:
00097         s->h263_msmpeg4 = 1;
00098         s->h263_pred = 1;
00099         s->msmpeg4_version=6;
00100         avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
00101         break;
00102     case CODEC_ID_H263I:
00103         break;
00104     case CODEC_ID_FLV1:
00105         s->h263_flv = 1;
00106         break;
00107     default:
00108         return -1;
00109     }
00110     s->codec_id= avctx->codec->id;
00111     avctx->hwaccel= ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
00112 
00113     /* for h263, we allocate the images after having read the header */
00114     if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
00115         if (MPV_common_init(s) < 0)
00116             return -1;
00117 
00118         h263_decode_init_vlc(s);
00119 
00120     return 0;
00121 }
00122 
00123 av_cold int ff_h263_decode_end(AVCodecContext *avctx)
00124 {
00125     MpegEncContext *s = avctx->priv_data;
00126 
00127     MPV_common_end(s);
00128     return 0;
00129 }
00130 
00134 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
00135     int pos= (get_bits_count(&s->gb)+7)>>3;
00136 
00137     if(s->divx_packed || s->avctx->hwaccel){
00138         //we would have to scan through the whole buf to handle the weird reordering ...
00139         return buf_size;
00140     }else if(s->flags&CODEC_FLAG_TRUNCATED){
00141         pos -= s->parse_context.last_index;
00142         if(pos<0) pos=0; // padding is not really read so this might be -1
00143         return pos;
00144     }else{
00145         if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
00146         if(pos+10>buf_size) pos=buf_size; // oops ;)
00147 
00148         return pos;
00149     }
00150 }
00151 
00152 static int decode_slice(MpegEncContext *s){
00153     const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
00154     const int mb_size= 16>>s->avctx->lowres;
00155     s->last_resync_gb= s->gb;
00156     s->first_slice_line= 1;
00157 
00158     s->resync_mb_x= s->mb_x;
00159     s->resync_mb_y= s->mb_y;
00160 
00161     ff_set_qscale(s, s->qscale);
00162 
00163     if (s->avctx->hwaccel) {
00164         const uint8_t *start= s->gb.buffer + get_bits_count(&s->gb)/8;
00165         const uint8_t *end  = ff_h263_find_resync_marker(start + 1, s->gb.buffer_end);
00166         skip_bits_long(&s->gb, 8*(end - start));
00167         return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
00168     }
00169 
00170     if(s->partitioned_frame){
00171         const int qscale= s->qscale;
00172 
00173         if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4){
00174             if(ff_mpeg4_decode_partitions(s) < 0)
00175                 return -1;
00176         }
00177 
00178         /* restore variables which were modified */
00179         s->first_slice_line=1;
00180         s->mb_x= s->resync_mb_x;
00181         s->mb_y= s->resync_mb_y;
00182         ff_set_qscale(s, qscale);
00183     }
00184 
00185     for(; s->mb_y < s->mb_height; s->mb_y++) {
00186         /* per-row end of slice checks */
00187         if(s->msmpeg4_version){
00188             if(s->resync_mb_y + s->slice_height == s->mb_y){
00189                 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);
00190 
00191                 return 0;
00192             }
00193         }
00194 
00195         if(s->msmpeg4_version==1){
00196             s->last_dc[0]=
00197             s->last_dc[1]=
00198             s->last_dc[2]= 128;
00199         }
00200 
00201         ff_init_block_index(s);
00202         for(; s->mb_x < s->mb_width; s->mb_x++) {
00203             int ret;
00204 
00205             ff_update_block_index(s);
00206 
00207             if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
00208                 s->first_slice_line=0;
00209             }
00210 
00211             /* DCT & quantize */
00212 
00213             s->mv_dir = MV_DIR_FORWARD;
00214             s->mv_type = MV_TYPE_16X16;
00215 //            s->mb_skipped = 0;
00216 //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
00217             ret= s->decode_mb(s, s->block);
00218 
00219             if (s->pict_type!=FF_B_TYPE)
00220                 ff_h263_update_motion_val(s);
00221 
00222             if(ret<0){
00223                 const int xy= s->mb_x + s->mb_y*s->mb_stride;
00224                 if(ret==SLICE_END){
00225                     MPV_decode_mb(s, s->block);
00226                     if(s->loop_filter)
00227                         ff_h263_loop_filter(s);
00228 
00229 //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));
00230                     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);
00231 
00232                     s->padding_bug_score--;
00233 
00234                     if(++s->mb_x >= s->mb_width){
00235                         s->mb_x=0;
00236                         ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
00237                         s->mb_y++;
00238                     }
00239                     return 0;
00240                 }else if(ret==SLICE_NOEND){
00241                     av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
00242                     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);
00243                     return -1;
00244                 }
00245                 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
00246                 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);
00247 
00248                 return -1;
00249             }
00250 
00251             MPV_decode_mb(s, s->block);
00252             if(s->loop_filter)
00253                 ff_h263_loop_filter(s);
00254         }
00255 
00256         ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
00257 
00258         s->mb_x= 0;
00259     }
00260 
00261     assert(s->mb_x==0 && s->mb_y==s->mb_height);
00262 
00263     /* try to detect the padding bug */
00264     if(      s->codec_id==CODEC_ID_MPEG4
00265        &&   (s->workaround_bugs&FF_BUG_AUTODETECT)
00266        &&    get_bits_left(&s->gb) >=0
00267        &&    get_bits_left(&s->gb) < 48
00268 //       &&   !s->resync_marker
00269        &&   !s->data_partitioning){
00270 
00271         const int bits_count= get_bits_count(&s->gb);
00272         const int bits_left = s->gb.size_in_bits - bits_count;
00273 
00274         if(bits_left==0){
00275             s->padding_bug_score+=16;
00276         } else if(bits_left != 1){
00277             int v= show_bits(&s->gb, 8);
00278             v|= 0x7F >> (7-(bits_count&7));
00279 
00280             if(v==0x7F && bits_left<=8)
00281                 s->padding_bug_score--;
00282             else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16)
00283                 s->padding_bug_score+= 4;
00284             else
00285                 s->padding_bug_score++;
00286         }
00287     }
00288 
00289     if(s->workaround_bugs&FF_BUG_AUTODETECT){
00290         if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version>=0 || !s->resync_marker)*/)
00291             s->workaround_bugs |=  FF_BUG_NO_PADDING;
00292         else
00293             s->workaround_bugs &= ~FF_BUG_NO_PADDING;
00294     }
00295 
00296     // handle formats which don't have unique end markers
00297     if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
00298         int left= get_bits_left(&s->gb);
00299         int max_extra=7;
00300 
00301         /* no markers in M$ crap */
00302         if(s->msmpeg4_version && s->pict_type==FF_I_TYPE)
00303             max_extra+= 17;
00304 
00305         /* buggy padding but the frame should still end approximately at the bitstream end */
00306         if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_recognition>=3)
00307             max_extra+= 48;
00308         else if((s->workaround_bugs&FF_BUG_NO_PADDING))
00309             max_extra+= 256*256*256*64;
00310 
00311         if(left>max_extra){
00312             av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
00313         }
00314         else if(left<0){
00315             av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
00316         }else
00317             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);
00318 
00319         return 0;
00320     }
00321 
00322     av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
00323             get_bits_left(&s->gb),
00324             show_bits(&s->gb, 24), s->padding_bug_score);
00325 
00326     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);
00327 
00328     return -1;
00329 }
00330 
00331 int ff_h263_decode_frame(AVCodecContext *avctx,
00332                              void *data, int *data_size,
00333                              AVPacket *avpkt)
00334 {
00335     const uint8_t *buf = avpkt->data;
00336     int buf_size = avpkt->size;
00337     MpegEncContext *s = avctx->priv_data;
00338     int ret;
00339     AVFrame *pict = data;
00340 
00341 #ifdef PRINT_FRAME_TIME
00342 uint64_t time= rdtsc();
00343 #endif
00344     s->flags= avctx->flags;
00345     s->flags2= avctx->flags2;
00346 
00347     /* no supplementary picture */
00348     if (buf_size == 0) {
00349         /* special case for last picture */
00350         if (s->low_delay==0 && s->next_picture_ptr) {
00351             *pict= *(AVFrame*)s->next_picture_ptr;
00352             s->next_picture_ptr= NULL;
00353 
00354             *data_size = sizeof(AVFrame);
00355         }
00356 
00357         return 0;
00358     }
00359 
00360     if(s->flags&CODEC_FLAG_TRUNCATED){
00361         int next;
00362 
00363         if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4){
00364             next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
00365         }else if(CONFIG_H263_DECODER && s->codec_id==CODEC_ID_H263){
00366             next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
00367         }else{
00368             av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
00369             return -1;
00370         }
00371 
00372         if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
00373             return buf_size;
00374     }
00375 
00376 
00377 retry:
00378 
00379     if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
00380         init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
00381     }else
00382         init_get_bits(&s->gb, buf, buf_size*8);
00383     s->bitstream_buffer_size=0;
00384 
00385     if (!s->context_initialized) {
00386         if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
00387             return -1;
00388     }
00389 
00390     /* We need to set current_picture_ptr before reading the header,
00391      * otherwise we cannot store anyting in there */
00392     if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
00393         int i= ff_find_unused_picture(s, 0);
00394         s->current_picture_ptr= &s->picture[i];
00395     }
00396 
00397     /* let's go :-) */
00398     if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5) {
00399         ret= ff_wmv2_decode_picture_header(s);
00400     } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
00401         ret = msmpeg4_decode_picture_header(s);
00402     } else if (CONFIG_MPEG4_DECODER && s->h263_pred) {
00403         if(s->avctx->extradata_size && s->picture_number==0){
00404             GetBitContext gb;
00405 
00406             init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
00407             ret = ff_mpeg4_decode_picture_header(s, &gb);
00408         }
00409         ret = ff_mpeg4_decode_picture_header(s, &s->gb);
00410     } else if (CONFIG_H263I_DECODER && s->codec_id == CODEC_ID_H263I) {
00411         ret = ff_intel_h263_decode_picture_header(s);
00412     } else if (CONFIG_FLV_DECODER && s->h263_flv) {
00413         ret = ff_flv_decode_picture_header(s);
00414     } else {
00415         ret = h263_decode_picture_header(s);
00416     }
00417 
00418     if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
00419 
00420     /* skip if the header was thrashed */
00421     if (ret < 0){
00422         av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
00423         return -1;
00424     }
00425 
00426     avctx->has_b_frames= !s->low_delay;
00427 
00428     if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
00429         if(s->stream_codec_tag == AV_RL32("XVID") ||
00430            s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") ||
00431            s->codec_tag == AV_RL32("RMP4") ||
00432            s->codec_tag == AV_RL32("SIPP")
00433            )
00434             s->xvid_build= 0;
00435 #if 0
00436         if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
00437            && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
00438             s->xvid_build= 0;
00439 #endif
00440     }
00441 
00442     if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
00443         if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
00444             s->divx_version= 400; //divx 4
00445     }
00446 
00447     if(s->xvid_build>=0 && s->divx_version>=0){
00448         s->divx_version=
00449         s->divx_build= -1;
00450     }
00451 
00452     if(s->workaround_bugs&FF_BUG_AUTODETECT){
00453         if(s->codec_tag == AV_RL32("XVIX"))
00454             s->workaround_bugs|= FF_BUG_XVID_ILACE;
00455 
00456         if(s->codec_tag == AV_RL32("UMP4")){
00457             s->workaround_bugs|= FF_BUG_UMP4;
00458         }
00459 
00460         if(s->divx_version>=500 && s->divx_build<1814){
00461             s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
00462         }
00463 
00464         if(s->divx_version>502 && s->divx_build<1814){
00465             s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
00466         }
00467 
00468         if(s->xvid_build<=3U)
00469             s->padding_bug_score= 256*256*256*64;
00470 
00471         if(s->xvid_build<=1U)
00472             s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
00473 
00474         if(s->xvid_build<=12U)
00475             s->workaround_bugs|= FF_BUG_EDGE;
00476 
00477         if(s->xvid_build<=32U)
00478             s->workaround_bugs|= FF_BUG_DC_CLIP;
00479 
00480 #define SET_QPEL_FUNC(postfix1, postfix2) \
00481     s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
00482     s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
00483     s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
00484 
00485         if(s->lavc_build<4653U)
00486             s->workaround_bugs|= FF_BUG_STD_QPEL;
00487 
00488         if(s->lavc_build<4655U)
00489             s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
00490 
00491         if(s->lavc_build<4670U){
00492             s->workaround_bugs|= FF_BUG_EDGE;
00493         }
00494 
00495         if(s->lavc_build<=4712U)
00496             s->workaround_bugs|= FF_BUG_DC_CLIP;
00497 
00498         if(s->divx_version>=0)
00499             s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
00500 //printf("padding_bug_score: %d\n", s->padding_bug_score);
00501         if(s->divx_version==501 && s->divx_build==20020416)
00502             s->padding_bug_score= 256*256*256*64;
00503 
00504         if(s->divx_version<500U){
00505             s->workaround_bugs|= FF_BUG_EDGE;
00506         }
00507 
00508         if(s->divx_version>=0)
00509             s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
00510 #if 0
00511         if(s->divx_version==500)
00512             s->padding_bug_score= 256*256*256*64;
00513 
00514         /* very ugly XVID padding bug detection FIXME/XXX solve this differently
00515          * Let us hope this at least works.
00516          */
00517         if(   s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==-1
00518            && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
00519             s->workaround_bugs|= FF_BUG_NO_PADDING;
00520 
00521         if(s->lavc_build<4609U) //FIXME not sure about the version num but a 4609 file seems ok
00522             s->workaround_bugs|= FF_BUG_NO_PADDING;
00523 #endif
00524     }
00525 
00526     if(s->workaround_bugs& FF_BUG_STD_QPEL){
00527         SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
00528         SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
00529         SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
00530         SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
00531         SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
00532         SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
00533 
00534         SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
00535         SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
00536         SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
00537         SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
00538         SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
00539         SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
00540     }
00541 
00542     if(avctx->debug & FF_DEBUG_BUGS)
00543         av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
00544                s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
00545                s->divx_packed ? "p" : "");
00546 
00547 #if 0 // dump bits per frame / qp / complexity
00548 {
00549     static FILE *f=NULL;
00550     if(!f) f=fopen("rate_qp_cplx.txt", "w");
00551     fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
00552 }
00553 #endif
00554 
00555 #if HAVE_MMX
00556     if(s->codec_id == CODEC_ID_MPEG4 && s->xvid_build>=0 && avctx->idct_algo == FF_IDCT_AUTO && (mm_flags & FF_MM_MMX)){
00557         avctx->idct_algo= FF_IDCT_XVIDMMX;
00558         avctx->coded_width= 0; // force reinit
00559 //        dsputil_init(&s->dsp, avctx);
00560         s->picture_number=0;
00561     }
00562 #endif
00563 
00564         /* After H263 & mpeg4 header decode we have the height, width,*/
00565         /* and other parameters. So then we could init the picture   */
00566         /* FIXME: By the way H263 decoder is evolving it should have */
00567         /* an H263EncContext                                         */
00568 
00569     if (   s->width  != avctx->coded_width
00570         || s->height != avctx->coded_height) {
00571         /* H.263 could change picture size any time */
00572         ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
00573         s->parse_context.buffer=0;
00574         MPV_common_end(s);
00575         s->parse_context= pc;
00576     }
00577     if (!s->context_initialized) {
00578         avcodec_set_dimensions(avctx, s->width, s->height);
00579 
00580         goto retry;
00581     }
00582 
00583     if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P || s->codec_id == CODEC_ID_H263I))
00584         s->gob_index = ff_h263_get_gob_height(s);
00585 
00586     // for hurry_up==5
00587     s->current_picture.pict_type= s->pict_type;
00588     s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
00589 
00590     /* skip B-frames if we don't have reference frames */
00591     if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size);
00592     /* skip b frames if we are in a hurry */
00593     if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return get_consumed_bytes(s, buf_size);
00594     if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
00595        || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
00596        ||  avctx->skip_frame >= AVDISCARD_ALL)
00597         return get_consumed_bytes(s, buf_size);
00598     /* skip everything if we are in a hurry>=5 */
00599     if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
00600 
00601     if(s->next_p_frame_damaged){
00602         if(s->pict_type==FF_B_TYPE)
00603             return get_consumed_bytes(s, buf_size);
00604         else
00605             s->next_p_frame_damaged=0;
00606     }
00607 
00608     if((s->avctx->flags2 & CODEC_FLAG2_FAST) && s->pict_type==FF_B_TYPE){
00609         s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
00610         s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
00611     }else if((!s->no_rounding) || s->pict_type==FF_B_TYPE){
00612         s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
00613         s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
00614     }else{
00615         s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
00616         s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
00617     }
00618 
00619     if(MPV_frame_start(s, avctx) < 0)
00620         return -1;
00621 
00622     if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
00623         ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
00624         goto frame_end;
00625     }
00626 
00627     if (avctx->hwaccel) {
00628         if (avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer) < 0)
00629             return -1;
00630     }
00631 
00632     ff_er_frame_start(s);
00633 
00634     //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
00635     //which is not available before MPV_frame_start()
00636     if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){
00637         ret = ff_wmv2_decode_secondary_picture_header(s);
00638         if(ret<0) return ret;
00639         if(ret==1) goto intrax8_decoded;
00640     }
00641 
00642     /* decode each macroblock */
00643     s->mb_x=0;
00644     s->mb_y=0;
00645 
00646     decode_slice(s);
00647     while(s->mb_y<s->mb_height){
00648         if(s->msmpeg4_version){
00649             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)
00650                 break;
00651         }else{
00652             if(ff_h263_resync(s)<0)
00653                 break;
00654         }
00655 
00656         if(s->msmpeg4_version<4 && s->h263_pred)
00657             ff_mpeg4_clean_buffers(s);
00658 
00659         decode_slice(s);
00660     }
00661 
00662     if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==FF_I_TYPE)
00663         if(!CONFIG_MSMPEG4_DECODER || msmpeg4_decode_ext_header(s, buf_size) < 0){
00664             s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
00665         }
00666 
00667     assert(s->bitstream_buffer_size==0);
00668 frame_end:
00669     /* divx 5.01+ bistream reorder stuff */
00670     if(s->codec_id==CODEC_ID_MPEG4 && s->divx_packed){
00671         int current_pos= get_bits_count(&s->gb)>>3;
00672         int startcode_found=0;
00673 
00674         if(buf_size - current_pos > 5){
00675             int i;
00676             for(i=current_pos; i<buf_size-3; i++){
00677                 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
00678                     startcode_found=1;
00679                     break;
00680                 }
00681             }
00682         }
00683         if(s->gb.buffer == s->bitstream_buffer && buf_size>7 && s->xvid_build>=0){ //xvid style
00684             startcode_found=1;
00685             current_pos=0;
00686         }
00687 
00688         if(startcode_found){
00689             av_fast_malloc(
00690                 &s->bitstream_buffer,
00691                 &s->allocated_bitstream_buffer_size,
00692                 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
00693             if (!s->bitstream_buffer)
00694                 return AVERROR(ENOMEM);
00695             memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
00696             s->bitstream_buffer_size= buf_size - current_pos;
00697         }
00698     }
00699 
00700 intrax8_decoded:
00701     ff_er_frame_end(s);
00702 
00703     if (avctx->hwaccel) {
00704         if (avctx->hwaccel->end_frame(avctx) < 0)
00705             return -1;
00706     }
00707 
00708     MPV_frame_end(s);
00709 
00710 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
00711 assert(s->current_picture.pict_type == s->pict_type);
00712     if (s->pict_type == FF_B_TYPE || s->low_delay) {
00713         *pict= *(AVFrame*)s->current_picture_ptr;
00714     } else if (s->last_picture_ptr != NULL) {
00715         *pict= *(AVFrame*)s->last_picture_ptr;
00716     }
00717 
00718     if(s->last_picture_ptr || s->low_delay){
00719         *data_size = sizeof(AVFrame);
00720         ff_print_debug_info(s, pict);
00721     }
00722 
00723 #ifdef PRINT_FRAME_TIME
00724 av_log(avctx, AV_LOG_DEBUG, "%"PRId64"\n", rdtsc()-time);
00725 #endif
00726 
00727     return get_consumed_bytes(s, buf_size);
00728 }
00729 
00730 AVCodec h263_decoder = {
00731     "h263",
00732     AVMEDIA_TYPE_VIDEO,
00733     CODEC_ID_H263,
00734     sizeof(MpegEncContext),
00735     ff_h263_decode_init,
00736     NULL,
00737     ff_h263_decode_end,
00738     ff_h263_decode_frame,
00739     CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
00740     .flush= ff_mpeg_flush,
00741     .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
00742     .pix_fmts= ff_hwaccel_pixfmt_list_420,
00743 };

Generated on Fri Sep 16 2011 17:17:37 for FFmpeg by  doxygen 1.7.1