00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00045
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
00059 ff_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
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
00114 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
00115 if (ff_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 ff_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
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;
00143 return pos;
00144 }else{
00145 if(pos==0) pos=1;
00146 if(pos+10>buf_size) pos=buf_size;
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
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
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
00212
00213 s->mv_dir = MV_DIR_FORWARD;
00214 s->mv_type = MV_TYPE_16X16;
00215
00216
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 ff_MPV_decode_mb(s, s->block);
00226 if(s->loop_filter)
00227 ff_h263_loop_filter(s);
00228
00229
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 ff_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 ff_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 ff_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
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
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 )
00300 s->workaround_bugs |= FF_BUG_NO_PADDING;
00301 else
00302 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
00303 }
00304
00305
00306 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){
00307 int left= get_bits_left(&s->gb);
00308 int max_extra=7;
00309
00310
00311 if(s->msmpeg4_version && s->pict_type==AV_PICTURE_TYPE_I)
00312 max_extra+= 17;
00313
00314
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
00357 if (buf_size == 0) {
00358
00359 if (s->low_delay==0 && s->next_picture_ptr) {
00360 *pict = s->next_picture_ptr->f;
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)){
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 (ff_MPV_common_init(s) < 0)
00408 return -1;
00409 }
00410
00411
00412
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
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 = ff_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 < 0 || ret==FRAME_SKIPPED) {
00442 if ( s->width != avctx->coded_width
00443 || s->height != avctx->coded_height) {
00444 av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
00445 s->width = avctx->coded_width;
00446 s->height= avctx->coded_height;
00447 }
00448 }
00449 if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
00450
00451
00452 if (ret < 0){
00453 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
00454 return -1;
00455 } else if ((s->width != avctx->coded_width ||
00456 s->height != avctx->coded_height ||
00457 (s->width + 15) >> 4 != s->mb_width ||
00458 (s->height + 15) >> 4 != s->mb_height) &&
00459 (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))) {
00460 av_log_missing_feature(s->avctx, "Width/height/bit depth/chroma idc changing with threads is", 0);
00461 s->width = avctx->coded_width;
00462 s->height= avctx->coded_height;
00463 return AVERROR_PATCHWELCOME;
00464 }
00465
00466 avctx->has_b_frames= !s->low_delay;
00467
00468 if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
00469 if(s->stream_codec_tag == AV_RL32("XVID") ||
00470 s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") ||
00471 s->codec_tag == AV_RL32("RMP4") ||
00472 s->codec_tag == AV_RL32("SIPP")
00473 )
00474 s->xvid_build= 0;
00475 #if 0
00476 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
00477 && s->padding_bug_score > 0 && s->low_delay)
00478 s->xvid_build= 0;
00479 #endif
00480 }
00481
00482 if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
00483 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
00484 s->divx_version= 400;
00485 }
00486
00487 if(s->xvid_build>=0 && s->divx_version>=0){
00488 s->divx_version=
00489 s->divx_build= -1;
00490 }
00491
00492 if(s->workaround_bugs&FF_BUG_AUTODETECT){
00493 if(s->codec_tag == AV_RL32("XVIX"))
00494 s->workaround_bugs|= FF_BUG_XVID_ILACE;
00495
00496 if(s->codec_tag == AV_RL32("UMP4")){
00497 s->workaround_bugs|= FF_BUG_UMP4;
00498 }
00499
00500 if(s->divx_version>=500 && s->divx_build<1814){
00501 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
00502 }
00503
00504 if(s->divx_version>502 && s->divx_build<1814){
00505 s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
00506 }
00507
00508 if(s->xvid_build<=3U)
00509 s->padding_bug_score= 256*256*256*64;
00510
00511 if(s->xvid_build<=1U)
00512 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
00513
00514 if(s->xvid_build<=12U)
00515 s->workaround_bugs|= FF_BUG_EDGE;
00516
00517 if(s->xvid_build<=32U)
00518 s->workaround_bugs|= FF_BUG_DC_CLIP;
00519
00520 #define SET_QPEL_FUNC(postfix1, postfix2) \
00521 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
00522 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
00523 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
00524
00525 if(s->lavc_build<4653U)
00526 s->workaround_bugs|= FF_BUG_STD_QPEL;
00527
00528 if(s->lavc_build<4655U)
00529 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
00530
00531 if(s->lavc_build<4670U){
00532 s->workaround_bugs|= FF_BUG_EDGE;
00533 }
00534
00535 if(s->lavc_build<=4712U)
00536 s->workaround_bugs|= FF_BUG_DC_CLIP;
00537
00538 if(s->divx_version>=0)
00539 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
00540
00541 if(s->divx_version==501 && s->divx_build==20020416)
00542 s->padding_bug_score= 256*256*256*64;
00543
00544 if(s->divx_version<500U){
00545 s->workaround_bugs|= FF_BUG_EDGE;
00546 }
00547
00548 if(s->divx_version>=0)
00549 s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
00550 #if 0
00551 if(s->divx_version==500)
00552 s->padding_bug_score= 256*256*256*64;
00553
00554
00555
00556
00557 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==-1
00558 && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
00559 s->workaround_bugs|= FF_BUG_NO_PADDING;
00560
00561 if(s->lavc_build<4609U)
00562 s->workaround_bugs|= FF_BUG_NO_PADDING;
00563 #endif
00564 }
00565
00566 if(s->workaround_bugs& FF_BUG_STD_QPEL){
00567 SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
00568 SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
00569 SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
00570 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
00571 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
00572 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
00573
00574 SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
00575 SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
00576 SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
00577 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
00578 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
00579 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
00580 }
00581
00582 if(avctx->debug & FF_DEBUG_BUGS)
00583 av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
00584 s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
00585 s->divx_packed ? "p" : "");
00586
00587 #if HAVE_MMX
00588 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)) {
00589 avctx->idct_algo= FF_IDCT_XVIDMMX;
00590 ff_dct_common_init(s);
00591 }
00592 #endif
00593
00594
00595
00596
00597
00598
00599 if ( s->width != avctx->coded_width
00600 || s->height != avctx->coded_height) {
00601
00602 ParseContext pc= s->parse_context;
00603
00604 s->parse_context.buffer=0;
00605 ff_MPV_common_end(s);
00606 s->parse_context= pc;
00607 }
00608 if (!s->context_initialized) {
00609 avcodec_set_dimensions(avctx, s->width, s->height);
00610
00611 goto retry;
00612 }
00613
00614 if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P || s->codec_id == CODEC_ID_H263I))
00615 s->gob_index = ff_h263_get_gob_height(s);
00616
00617
00618 s->current_picture.f.pict_type = s->pict_type;
00619 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
00620
00621
00622 if(s->last_picture_ptr==NULL && (s->pict_type==AV_PICTURE_TYPE_B || s->dropable)) return get_consumed_bytes(s, buf_size);
00623 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
00624 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
00625 || avctx->skip_frame >= AVDISCARD_ALL)
00626 return get_consumed_bytes(s, buf_size);
00627
00628 if(s->next_p_frame_damaged){
00629 if(s->pict_type==AV_PICTURE_TYPE_B)
00630 return get_consumed_bytes(s, buf_size);
00631 else
00632 s->next_p_frame_damaged=0;
00633 }
00634
00635 if((s->avctx->flags2 & CODEC_FLAG2_FAST) && s->pict_type==AV_PICTURE_TYPE_B){
00636 s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
00637 s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
00638 }else if((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
00639 s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
00640 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
00641 }else{
00642 s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
00643 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
00644 }
00645
00646 if(ff_MPV_frame_start(s, avctx) < 0)
00647 return -1;
00648
00649 if (!s->divx_packed) ff_thread_finish_setup(avctx);
00650
00651 if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
00652 ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
00653 goto frame_end;
00654 }
00655
00656 if (avctx->hwaccel) {
00657 if (avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer) < 0)
00658 return -1;
00659 }
00660
00661 ff_er_frame_start(s);
00662
00663
00664
00665 if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){
00666 ret = ff_wmv2_decode_secondary_picture_header(s);
00667 if(ret<0) return ret;
00668 if(ret==1) goto intrax8_decoded;
00669 }
00670
00671
00672 s->mb_x=0;
00673 s->mb_y=0;
00674
00675 ret = decode_slice(s);
00676 while(s->mb_y<s->mb_height){
00677 if(s->msmpeg4_version){
00678 if(s->slice_height==0 || s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_left(&s->gb)<0)
00679 break;
00680 }else{
00681 int prev_x=s->mb_x, prev_y=s->mb_y;
00682 if(ff_h263_resync(s)<0)
00683 break;
00684 if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
00685 s->error_occurred = 1;
00686 }
00687
00688 if(s->msmpeg4_version<4 && s->h263_pred)
00689 ff_mpeg4_clean_buffers(s);
00690
00691 if (decode_slice(s) < 0) ret = AVERROR_INVALIDDATA;
00692 }
00693
00694 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type==AV_PICTURE_TYPE_I)
00695 if(!CONFIG_MSMPEG4_DECODER || ff_msmpeg4_decode_ext_header(s, buf_size) < 0){
00696 s->error_status_table[s->mb_num-1]= ER_MB_ERROR;
00697 }
00698
00699 assert(s->bitstream_buffer_size==0);
00700 frame_end:
00701
00702 if(s->codec_id==CODEC_ID_MPEG4 && s->divx_packed){
00703 int current_pos= s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb)>>3);
00704 int startcode_found=0;
00705
00706 if(buf_size - current_pos > 7){
00707 int i;
00708 for(i=current_pos; i<buf_size-4; i++){
00709 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
00710 startcode_found=!(buf[i+4]&0x40);
00711 break;
00712 }
00713 }
00714 }
00715
00716 if(startcode_found){
00717 av_fast_malloc(
00718 &s->bitstream_buffer,
00719 &s->allocated_bitstream_buffer_size,
00720 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
00721 if (!s->bitstream_buffer)
00722 return AVERROR(ENOMEM);
00723 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
00724 s->bitstream_buffer_size= buf_size - current_pos;
00725 }
00726 }
00727
00728 intrax8_decoded:
00729 ff_er_frame_end(s);
00730
00731 if (avctx->hwaccel) {
00732 if (avctx->hwaccel->end_frame(avctx) < 0)
00733 return -1;
00734 }
00735
00736 ff_MPV_frame_end(s);
00737
00738 assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
00739 assert(s->current_picture.f.pict_type == s->pict_type);
00740 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
00741 *pict = s->current_picture_ptr->f;
00742 } else if (s->last_picture_ptr != NULL) {
00743 *pict = s->last_picture_ptr->f;
00744 }
00745
00746 if(s->last_picture_ptr || s->low_delay){
00747 *data_size = sizeof(AVFrame);
00748 ff_print_debug_info(s, pict);
00749 }
00750
00751 #ifdef PRINT_FRAME_TIME
00752 av_log(avctx, AV_LOG_DEBUG, "%"PRId64"\n", rdtsc()-time);
00753 #endif
00754
00755 return (ret && (avctx->err_recognition & AV_EF_EXPLODE))?ret:get_consumed_bytes(s, buf_size);
00756 }
00757
00758 AVCodec ff_h263_decoder = {
00759 .name = "h263",
00760 .type = AVMEDIA_TYPE_VIDEO,
00761 .id = CODEC_ID_H263,
00762 .priv_data_size = sizeof(MpegEncContext),
00763 .init = ff_h263_decode_init,
00764 .close = ff_h263_decode_end,
00765 .decode = ff_h263_decode_frame,
00766 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
00767 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
00768 .flush = ff_mpeg_flush,
00769 .max_lowres = 3,
00770 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
00771 .pix_fmts = ff_hwaccel_pixfmt_list_420,
00772 };