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

Generated on Wed Apr 11 2012 07:31:33 for FFmpeg by  doxygen 1.7.1