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