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