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