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 int ret;
00051
00052 s->avctx = avctx;
00053 s->out_format = FMT_H263;
00054
00055 s->width = avctx->coded_width;
00056 s->height = avctx->coded_height;
00057 s->workaround_bugs= avctx->workaround_bugs;
00058
00059
00060 ff_MPV_decode_defaults(s);
00061 s->quant_precision=5;
00062 s->decode_mb= ff_h263_decode_mb;
00063 s->low_delay= 1;
00064 if (avctx->codec->id == AV_CODEC_ID_MSS2)
00065 avctx->pix_fmt = PIX_FMT_YUV420P;
00066 else
00067 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
00068 s->unrestricted_mv= 1;
00069
00070
00071 switch(avctx->codec->id) {
00072 case AV_CODEC_ID_H263:
00073 case AV_CODEC_ID_H263P:
00074 s->unrestricted_mv= 0;
00075 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
00076 break;
00077 case AV_CODEC_ID_MPEG4:
00078 break;
00079 case AV_CODEC_ID_MSMPEG4V1:
00080 s->h263_pred = 1;
00081 s->msmpeg4_version=1;
00082 break;
00083 case AV_CODEC_ID_MSMPEG4V2:
00084 s->h263_pred = 1;
00085 s->msmpeg4_version=2;
00086 break;
00087 case AV_CODEC_ID_MSMPEG4V3:
00088 s->h263_pred = 1;
00089 s->msmpeg4_version=3;
00090 break;
00091 case AV_CODEC_ID_WMV1:
00092 s->h263_pred = 1;
00093 s->msmpeg4_version=4;
00094 break;
00095 case AV_CODEC_ID_WMV2:
00096 s->h263_pred = 1;
00097 s->msmpeg4_version=5;
00098 break;
00099 case AV_CODEC_ID_VC1:
00100 case AV_CODEC_ID_WMV3:
00101 case AV_CODEC_ID_VC1IMAGE:
00102 case AV_CODEC_ID_WMV3IMAGE:
00103 case AV_CODEC_ID_MSS2:
00104 s->h263_pred = 1;
00105 s->msmpeg4_version=6;
00106 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
00107 break;
00108 case AV_CODEC_ID_H263I:
00109 break;
00110 case AV_CODEC_ID_FLV1:
00111 s->h263_flv = 1;
00112 break;
00113 default:
00114 return AVERROR(EINVAL);
00115 }
00116 s->codec_id= avctx->codec->id;
00117 avctx->hwaccel= ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
00118
00119
00120 if (avctx->codec->id != AV_CODEC_ID_H263 && avctx->codec->id != AV_CODEC_ID_H263P && avctx->codec->id != AV_CODEC_ID_MPEG4)
00121 if ((ret = ff_MPV_common_init(s)) < 0)
00122 return ret;
00123
00124 ff_h263_decode_init_vlc(s);
00125
00126 return 0;
00127 }
00128
00129 av_cold int ff_h263_decode_end(AVCodecContext *avctx)
00130 {
00131 MpegEncContext *s = avctx->priv_data;
00132
00133 ff_MPV_common_end(s);
00134 return 0;
00135 }
00136
00140 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
00141 int pos= (get_bits_count(&s->gb)+7)>>3;
00142
00143 if(s->divx_packed || s->avctx->hwaccel){
00144
00145 return buf_size;
00146 }else if(s->flags&CODEC_FLAG_TRUNCATED){
00147 pos -= s->parse_context.last_index;
00148 if(pos<0) pos=0;
00149 return pos;
00150 }else{
00151 if(pos==0) pos=1;
00152 if(pos+10>buf_size) pos=buf_size;
00153
00154 return pos;
00155 }
00156 }
00157
00158 static int decode_slice(MpegEncContext *s){
00159 const int part_mask= s->partitioned_frame ? (ER_AC_END|ER_AC_ERROR) : 0x7F;
00160 const int mb_size= 16>>s->avctx->lowres;
00161 int ret;
00162
00163 s->last_resync_gb= s->gb;
00164 s->first_slice_line= 1;
00165
00166 s->resync_mb_x= s->mb_x;
00167 s->resync_mb_y= s->mb_y;
00168
00169 ff_set_qscale(s, s->qscale);
00170
00171 if (s->avctx->hwaccel) {
00172 const uint8_t *start= s->gb.buffer + get_bits_count(&s->gb)/8;
00173 const uint8_t *end = ff_h263_find_resync_marker(start + 1, s->gb.buffer_end);
00174 skip_bits_long(&s->gb, 8*(end - start));
00175 return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
00176 }
00177
00178 if(s->partitioned_frame){
00179 const int qscale= s->qscale;
00180
00181 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4){
00182 if ((ret = ff_mpeg4_decode_partitions(s)) < 0)
00183 return ret;
00184 }
00185
00186
00187 s->first_slice_line=1;
00188 s->mb_x= s->resync_mb_x;
00189 s->mb_y= s->resync_mb_y;
00190 ff_set_qscale(s, qscale);
00191 }
00192
00193 for(; s->mb_y < s->mb_height; s->mb_y++) {
00194
00195 if(s->msmpeg4_version){
00196 if(s->resync_mb_y + s->slice_height == s->mb_y){
00197 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
00198
00199 return 0;
00200 }
00201 }
00202
00203 if(s->msmpeg4_version==1){
00204 s->last_dc[0]=
00205 s->last_dc[1]=
00206 s->last_dc[2]= 128;
00207 }
00208
00209 ff_init_block_index(s);
00210 for(; s->mb_x < s->mb_width; s->mb_x++) {
00211 int ret;
00212
00213 ff_update_block_index(s);
00214
00215 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
00216 s->first_slice_line=0;
00217 }
00218
00219
00220
00221 s->mv_dir = MV_DIR_FORWARD;
00222 s->mv_type = MV_TYPE_16X16;
00223
00224
00225 ret= s->decode_mb(s, s->block);
00226
00227 if (s->pict_type!=AV_PICTURE_TYPE_B)
00228 ff_h263_update_motion_val(s);
00229
00230 if(ret<0){
00231 const int xy= s->mb_x + s->mb_y*s->mb_stride;
00232 if(ret==SLICE_END){
00233 ff_MPV_decode_mb(s, s->block);
00234 if(s->loop_filter)
00235 ff_h263_loop_filter(s);
00236
00237
00238 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
00239
00240 s->padding_bug_score--;
00241
00242 if(++s->mb_x >= s->mb_width){
00243 s->mb_x=0;
00244 ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
00245 ff_MPV_report_decode_progress(s);
00246 s->mb_y++;
00247 }
00248 return 0;
00249 }else if(ret==SLICE_NOEND){
00250 av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
00251 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);
00252 return AVERROR_INVALIDDATA;
00253 }
00254 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
00255 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
00256
00257 return AVERROR_INVALIDDATA;
00258 }
00259
00260 ff_MPV_decode_mb(s, s->block);
00261 if(s->loop_filter)
00262 ff_h263_loop_filter(s);
00263 }
00264
00265 ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
00266 ff_MPV_report_decode_progress(s);
00267
00268 s->mb_x= 0;
00269 }
00270
00271 assert(s->mb_x==0 && s->mb_y==s->mb_height);
00272
00273 if(s->codec_id==AV_CODEC_ID_MPEG4
00274 && (s->workaround_bugs&FF_BUG_AUTODETECT)
00275 && get_bits_left(&s->gb) >= 48
00276 && show_bits(&s->gb, 24)==0x4010
00277 && !s->data_partitioning)
00278 s->padding_bug_score+=32;
00279
00280
00281 if( s->codec_id==AV_CODEC_ID_MPEG4
00282 && (s->workaround_bugs&FF_BUG_AUTODETECT)
00283 && get_bits_left(&s->gb) >=0
00284 && get_bits_left(&s->gb) < 137
00285
00286 && !s->data_partitioning){
00287
00288 const int bits_count= get_bits_count(&s->gb);
00289 const int bits_left = s->gb.size_in_bits - bits_count;
00290
00291 if(bits_left==0){
00292 s->padding_bug_score+=16;
00293 } else if(bits_left != 1){
00294 int v= show_bits(&s->gb, 8);
00295 v|= 0x7F >> (7-(bits_count&7));
00296
00297 if(v==0x7F && bits_left<=8)
00298 s->padding_bug_score--;
00299 else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16)
00300 s->padding_bug_score+= 4;
00301 else
00302 s->padding_bug_score++;
00303 }
00304 }
00305
00306 if(s->workaround_bugs&FF_BUG_AUTODETECT){
00307 if(s->padding_bug_score > -2 && !s->data_partitioning )
00308 s->workaround_bugs |= FF_BUG_NO_PADDING;
00309 else
00310 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
00311 }
00312
00313
00314 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){
00315 int left= get_bits_left(&s->gb);
00316 int max_extra=7;
00317
00318
00319 if(s->msmpeg4_version && s->pict_type==AV_PICTURE_TYPE_I)
00320 max_extra+= 17;
00321
00322
00323 if((s->workaround_bugs&FF_BUG_NO_PADDING) && (s->err_recognition&(AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
00324 max_extra+= 48;
00325 else if((s->workaround_bugs&FF_BUG_NO_PADDING))
00326 max_extra+= 256*256*256*64;
00327
00328 if(left>max_extra){
00329 av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
00330 }
00331 else if(left<0){
00332 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
00333 }else
00334 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
00335
00336 return 0;
00337 }
00338
00339 av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
00340 get_bits_left(&s->gb),
00341 show_bits(&s->gb, 24), s->padding_bug_score);
00342
00343 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
00344
00345 return AVERROR_INVALIDDATA;
00346 }
00347
00348 int ff_h263_decode_frame(AVCodecContext *avctx,
00349 void *data, int *data_size,
00350 AVPacket *avpkt)
00351 {
00352 const uint8_t *buf = avpkt->data;
00353 int buf_size = avpkt->size;
00354 MpegEncContext *s = avctx->priv_data;
00355 int ret;
00356 AVFrame *pict = data;
00357
00358 #ifdef PRINT_FRAME_TIME
00359 uint64_t time= rdtsc();
00360 #endif
00361 s->flags= avctx->flags;
00362 s->flags2= avctx->flags2;
00363
00364
00365 if (buf_size == 0) {
00366
00367 if (s->low_delay==0 && s->next_picture_ptr) {
00368 *pict = s->next_picture_ptr->f;
00369 s->next_picture_ptr= NULL;
00370
00371 *data_size = sizeof(AVFrame);
00372 }
00373
00374 return 0;
00375 }
00376
00377 if(s->flags&CODEC_FLAG_TRUNCATED){
00378 int next;
00379
00380 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4){
00381 next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
00382 }else if(CONFIG_H263_DECODER && s->codec_id==AV_CODEC_ID_H263){
00383 next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
00384 }else if(CONFIG_H263P_DECODER && s->codec_id==AV_CODEC_ID_H263P){
00385 next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
00386 }else{
00387 av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
00388 return AVERROR(EINVAL);
00389 }
00390
00391 if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
00392 return buf_size;
00393 }
00394
00395
00396 retry:
00397 if(s->divx_packed && s->bitstream_buffer_size){
00398 int i;
00399 for(i=0; i<buf_size-3; i++){
00400 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1){
00401 if(buf[i+3]==0xB0){
00402 av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
00403 s->bitstream_buffer_size=0;
00404 }
00405 break;
00406 }
00407 }
00408 }
00409
00410 if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){
00411 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
00412 }else
00413 init_get_bits(&s->gb, buf, buf_size*8);
00414 s->bitstream_buffer_size=0;
00415
00416 if (!s->context_initialized) {
00417 if ((ret = ff_MPV_common_init(s)) < 0)
00418 return ret;
00419 }
00420
00421
00422
00423 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
00424 int i= ff_find_unused_picture(s, 0);
00425 if (i < 0)
00426 return i;
00427 s->current_picture_ptr= &s->picture[i];
00428 }
00429
00430
00431 if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5) {
00432 ret= ff_wmv2_decode_picture_header(s);
00433 } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
00434 ret = ff_msmpeg4_decode_picture_header(s);
00435 } else if (CONFIG_MPEG4_DECODER && s->h263_pred) {
00436 if(s->avctx->extradata_size && s->picture_number==0){
00437 GetBitContext gb;
00438
00439 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
00440 ret = ff_mpeg4_decode_picture_header(s, &gb);
00441 }
00442 ret = ff_mpeg4_decode_picture_header(s, &s->gb);
00443 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
00444 ret = ff_intel_h263_decode_picture_header(s);
00445 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
00446 ret = ff_flv_decode_picture_header(s);
00447 } else {
00448 ret = ff_h263_decode_picture_header(s);
00449 }
00450
00451 if (ret < 0 || ret==FRAME_SKIPPED) {
00452 if ( s->width != avctx->coded_width
00453 || s->height != avctx->coded_height) {
00454 av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
00455 s->width = avctx->coded_width;
00456 s->height= avctx->coded_height;
00457 }
00458 }
00459 if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
00460
00461
00462 if (ret < 0){
00463 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
00464 return ret;
00465 }
00466
00467 avctx->has_b_frames= !s->low_delay;
00468
00469 if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
00470 if(s->stream_codec_tag == AV_RL32("XVID") ||
00471 s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") ||
00472 s->codec_tag == AV_RL32("RMP4") ||
00473 s->codec_tag == AV_RL32("SIPP")
00474 )
00475 s->xvid_build= 0;
00476 #if 0
00477 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
00478 && s->padding_bug_score > 0 && s->low_delay)
00479 s->xvid_build= 0;
00480 #endif
00481 }
00482
00483 if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
00484 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
00485 s->divx_version= 400;
00486 }
00487
00488 if(s->xvid_build>=0 && s->divx_version>=0){
00489 s->divx_version=
00490 s->divx_build= -1;
00491 }
00492
00493 if(s->workaround_bugs&FF_BUG_AUTODETECT){
00494 if(s->codec_tag == AV_RL32("XVIX"))
00495 s->workaround_bugs|= FF_BUG_XVID_ILACE;
00496
00497 if(s->codec_tag == AV_RL32("UMP4")){
00498 s->workaround_bugs|= FF_BUG_UMP4;
00499 }
00500
00501 if(s->divx_version>=500 && s->divx_build<1814){
00502 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
00503 }
00504
00505 if(s->divx_version>502 && s->divx_build<1814){
00506 s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
00507 }
00508
00509 if(s->xvid_build<=3U)
00510 s->padding_bug_score= 256*256*256*64;
00511
00512 if(s->xvid_build<=1U)
00513 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
00514
00515 if(s->xvid_build<=12U)
00516 s->workaround_bugs|= FF_BUG_EDGE;
00517
00518 if(s->xvid_build<=32U)
00519 s->workaround_bugs|= FF_BUG_DC_CLIP;
00520
00521 #define SET_QPEL_FUNC(postfix1, postfix2) \
00522 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
00523 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
00524 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
00525
00526 if(s->lavc_build<4653U)
00527 s->workaround_bugs|= FF_BUG_STD_QPEL;
00528
00529 if(s->lavc_build<4655U)
00530 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
00531
00532 if(s->lavc_build<4670U){
00533 s->workaround_bugs|= FF_BUG_EDGE;
00534 }
00535
00536 if(s->lavc_build<=4712U)
00537 s->workaround_bugs|= FF_BUG_DC_CLIP;
00538
00539 if(s->divx_version>=0)
00540 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
00541
00542 if(s->divx_version==501 && s->divx_build==20020416)
00543 s->padding_bug_score= 256*256*256*64;
00544
00545 if(s->divx_version<500U){
00546 s->workaround_bugs|= FF_BUG_EDGE;
00547 }
00548
00549 if(s->divx_version>=0)
00550 s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
00551 #if 0
00552 if(s->divx_version==500)
00553 s->padding_bug_score= 256*256*256*64;
00554
00555
00556
00557
00558 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==-1
00559 && s->codec_id==AV_CODEC_ID_MPEG4 && s->vo_type==0)
00560 s->workaround_bugs|= FF_BUG_NO_PADDING;
00561
00562 if(s->lavc_build<4609U)
00563 s->workaround_bugs|= FF_BUG_NO_PADDING;
00564 #endif
00565 }
00566
00567 if(s->workaround_bugs& FF_BUG_STD_QPEL){
00568 SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
00569 SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
00570 SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
00571 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
00572 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
00573 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
00574
00575 SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
00576 SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
00577 SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
00578 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
00579 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
00580 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
00581 }
00582
00583 if(avctx->debug & FF_DEBUG_BUGS)
00584 av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
00585 s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
00586 s->divx_packed ? "p" : "");
00587
00588 #if HAVE_MMX
00589 if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build>=0 && avctx->idct_algo == FF_IDCT_AUTO && (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
00590 avctx->idct_algo= FF_IDCT_XVIDMMX;
00591 ff_dct_common_init(s);
00592 goto retry;
00593 }
00594 #endif
00595
00596
00597
00598
00599
00600
00601 if ((!avctx->coded_width || !avctx->coded_height) && 0) {
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 avcodec_set_dimensions(avctx, s->width, s->height);
00608
00609 goto retry;
00610 }
00611
00612 if (s->width != avctx->coded_width ||
00613 s->height != avctx->coded_height ||
00614 s->context_reinit) {
00615
00616 s->context_reinit = 0;
00617
00618 avcodec_set_dimensions(avctx, s->width, s->height);
00619
00620 if ((ret = ff_MPV_common_frame_size_change(s)))
00621 return ret;
00622 }
00623
00624 if((s->codec_id==AV_CODEC_ID_H263 || s->codec_id==AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_H263I))
00625 s->gob_index = ff_h263_get_gob_height(s);
00626
00627
00628 s->current_picture.f.pict_type = s->pict_type;
00629 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
00630
00631
00632 if(s->last_picture_ptr==NULL && (s->pict_type==AV_PICTURE_TYPE_B || s->dropable)) return get_consumed_bytes(s, buf_size);
00633 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
00634 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
00635 || avctx->skip_frame >= AVDISCARD_ALL)
00636 return get_consumed_bytes(s, buf_size);
00637
00638 if(s->next_p_frame_damaged){
00639 if(s->pict_type==AV_PICTURE_TYPE_B)
00640 return get_consumed_bytes(s, buf_size);
00641 else
00642 s->next_p_frame_damaged=0;
00643 }
00644
00645 if((s->avctx->flags2 & CODEC_FLAG2_FAST) && s->pict_type==AV_PICTURE_TYPE_B){
00646 s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
00647 s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
00648 }else if((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
00649 s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
00650 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
00651 }else{
00652 s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
00653 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
00654 }
00655
00656 if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
00657 return ret;
00658
00659 if (!s->divx_packed) ff_thread_finish_setup(avctx);
00660
00661 if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
00662 ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
00663 goto frame_end;
00664 }
00665
00666 if (avctx->hwaccel) {
00667 if ((ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer)) < 0)
00668 return ret;
00669 }
00670
00671 ff_er_frame_start(s);
00672
00673
00674
00675 if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){
00676 ret = ff_wmv2_decode_secondary_picture_header(s);
00677 if(ret<0) return ret;
00678 if(ret==1) goto intrax8_decoded;
00679 }
00680
00681
00682 s->mb_x=0;
00683 s->mb_y=0;
00684
00685 ret = decode_slice(s);
00686 while(s->mb_y<s->mb_height){
00687 if(s->msmpeg4_version){
00688 if(s->slice_height==0 || s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_left(&s->gb)<0)
00689 break;
00690 }else{
00691 int prev_x=s->mb_x, prev_y=s->mb_y;
00692 if(ff_h263_resync(s)<0)
00693 break;
00694 if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
00695 s->error_occurred = 1;
00696 }
00697
00698 if(s->msmpeg4_version<4 && s->h263_pred)
00699 ff_mpeg4_clean_buffers(s);
00700
00701 if (decode_slice(s) < 0) ret = AVERROR_INVALIDDATA;
00702 }
00703
00704 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type==AV_PICTURE_TYPE_I)
00705 if(!CONFIG_MSMPEG4_DECODER || ff_msmpeg4_decode_ext_header(s, buf_size) < 0){
00706 s->error_status_table[s->mb_num-1]= ER_MB_ERROR;
00707 }
00708
00709 assert(s->bitstream_buffer_size==0);
00710 frame_end:
00711
00712 if(s->codec_id==AV_CODEC_ID_MPEG4 && s->divx_packed){
00713 int current_pos= s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb)>>3);
00714 int startcode_found=0;
00715
00716 if(buf_size - current_pos > 7){
00717 int i;
00718 for(i=current_pos; i<buf_size-4; i++){
00719 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
00720 startcode_found=!(buf[i+4]&0x40);
00721 break;
00722 }
00723 }
00724 }
00725
00726 if(startcode_found){
00727 av_fast_malloc(
00728 &s->bitstream_buffer,
00729 &s->allocated_bitstream_buffer_size,
00730 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
00731 if (!s->bitstream_buffer)
00732 return AVERROR(ENOMEM);
00733 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
00734 s->bitstream_buffer_size= buf_size - current_pos;
00735 }
00736 }
00737
00738 intrax8_decoded:
00739 ff_er_frame_end(s);
00740
00741 if (avctx->hwaccel) {
00742 if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
00743 return ret;
00744 }
00745
00746 ff_MPV_frame_end(s);
00747
00748 assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
00749 assert(s->current_picture.f.pict_type == s->pict_type);
00750 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
00751 *pict = s->current_picture_ptr->f;
00752 } else if (s->last_picture_ptr != NULL) {
00753 *pict = s->last_picture_ptr->f;
00754 }
00755
00756 if(s->last_picture_ptr || s->low_delay){
00757 *data_size = sizeof(AVFrame);
00758 ff_print_debug_info(s, pict);
00759 }
00760
00761 #ifdef PRINT_FRAME_TIME
00762 av_log(avctx, AV_LOG_DEBUG, "%"PRId64"\n", rdtsc()-time);
00763 #endif
00764
00765 return (ret && (avctx->err_recognition & AV_EF_EXPLODE))?ret:get_consumed_bytes(s, buf_size);
00766 }
00767
00768 AVCodec ff_h263_decoder = {
00769 .name = "h263",
00770 .type = AVMEDIA_TYPE_VIDEO,
00771 .id = AV_CODEC_ID_H263,
00772 .priv_data_size = sizeof(MpegEncContext),
00773 .init = ff_h263_decode_init,
00774 .close = ff_h263_decode_end,
00775 .decode = ff_h263_decode_frame,
00776 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
00777 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
00778 .flush = ff_mpeg_flush,
00779 .max_lowres = 3,
00780 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
00781 .pix_fmts = ff_hwaccel_pixfmt_list_420,
00782 };
00783
00784 AVCodec ff_h263p_decoder = {
00785 .name = "h263p",
00786 .type = AVMEDIA_TYPE_VIDEO,
00787 .id = AV_CODEC_ID_H263P,
00788 .priv_data_size = sizeof(MpegEncContext),
00789 .init = ff_h263_decode_init,
00790 .close = ff_h263_decode_end,
00791 .decode = ff_h263_decode_frame,
00792 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
00793 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
00794 .flush = ff_mpeg_flush,
00795 .max_lowres = 3,
00796 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
00797 .pix_fmts = ff_hwaccel_pixfmt_list_420,
00798 };