Libav 0.7.1
|
00001 /* 00002 * H.263 decoder 00003 * Copyright (c) 2001 Fabrice Bellard 00004 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00028 #include "libavutil/cpu.h" 00029 #include "internal.h" 00030 #include "avcodec.h" 00031 #include "dsputil.h" 00032 #include "mpegvideo.h" 00033 #include "h263.h" 00034 #include "h263_parser.h" 00035 #include "mpeg4video_parser.h" 00036 #include "msmpeg4.h" 00037 #include "vdpau_internal.h" 00038 #include "thread.h" 00039 #include "flv.h" 00040 #include "mpeg4video.h" 00041 00042 //#define DEBUG 00043 //#define PRINT_FRAME_TIME 00044 00045 av_cold int ff_h263_decode_init(AVCodecContext *avctx) 00046 { 00047 MpegEncContext *s = avctx->priv_data; 00048 00049 s->avctx = avctx; 00050 s->out_format = FMT_H263; 00051 00052 s->width = avctx->coded_width; 00053 s->height = avctx->coded_height; 00054 s->workaround_bugs= avctx->workaround_bugs; 00055 00056 // set defaults 00057 MPV_decode_defaults(s); 00058 s->quant_precision=5; 00059 s->decode_mb= ff_h263_decode_mb; 00060 s->low_delay= 1; 00061 avctx->pix_fmt= avctx->get_format(avctx, avctx->codec->pix_fmts); 00062 s->unrestricted_mv= 1; 00063 00064 /* select sub codec */ 00065 switch(avctx->codec->id) { 00066 case CODEC_ID_H263: 00067 s->unrestricted_mv= 0; 00068 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; 00069 break; 00070 case CODEC_ID_MPEG4: 00071 break; 00072 case CODEC_ID_MSMPEG4V1: 00073 s->h263_pred = 1; 00074 s->msmpeg4_version=1; 00075 break; 00076 case CODEC_ID_MSMPEG4V2: 00077 s->h263_pred = 1; 00078 s->msmpeg4_version=2; 00079 break; 00080 case CODEC_ID_MSMPEG4V3: 00081 s->h263_pred = 1; 00082 s->msmpeg4_version=3; 00083 break; 00084 case CODEC_ID_WMV1: 00085 s->h263_pred = 1; 00086 s->msmpeg4_version=4; 00087 break; 00088 case CODEC_ID_WMV2: 00089 s->h263_pred = 1; 00090 s->msmpeg4_version=5; 00091 break; 00092 case CODEC_ID_VC1: 00093 case CODEC_ID_WMV3: 00094 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 /* for h263, we allocate the images after having read the header */ 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 //we would have to scan through the whole buf to handle the weird reordering ... 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; // padding is not really read so this might be -1 00139 return pos; 00140 }else{ 00141 if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...) 00142 if(pos+10>buf_size) pos=buf_size; // oops ;) 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 /* restore variables which were modified */ 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 /* per-row end of slice checks */ 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 /* DCT & quantize */ 00208 00209 s->mv_dir = MV_DIR_FORWARD; 00210 s->mv_type = MV_TYPE_16X16; 00211 // s->mb_skipped = 0; 00212 //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24)); 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 //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)); 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 /* try to detect the padding bug */ 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 // && !s->resync_marker 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 /*&& (s->divx_version>=0 || !s->resync_marker)*/) 00296 s->workaround_bugs |= FF_BUG_NO_PADDING; 00297 else 00298 s->workaround_bugs &= ~FF_BUG_NO_PADDING; 00299 } 00300 00301 // handle formats which don't have unique end markers 00302 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly 00303 int left= get_bits_left(&s->gb); 00304 int max_extra=7; 00305 00306 /* no markers in M$ crap */ 00307 if(s->msmpeg4_version && s->pict_type==AV_PICTURE_TYPE_I) 00308 max_extra+= 17; 00309 00310 /* buggy padding but the frame should still end approximately at the bitstream end */ 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 /* no supplementary picture */ 00353 if (buf_size == 0) { 00354 /* special case for last picture */ 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 00384 if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder 00385 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8); 00386 }else 00387 init_get_bits(&s->gb, buf, buf_size*8); 00388 s->bitstream_buffer_size=0; 00389 00390 if (!s->context_initialized) { 00391 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix 00392 return -1; 00393 } 00394 00395 /* We need to set current_picture_ptr before reading the header, 00396 * otherwise we cannot store anyting in there */ 00397 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){ 00398 int i= ff_find_unused_picture(s, 0); 00399 s->current_picture_ptr= &s->picture[i]; 00400 } 00401 00402 /* let's go :-) */ 00403 if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5) { 00404 ret= ff_wmv2_decode_picture_header(s); 00405 } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) { 00406 ret = msmpeg4_decode_picture_header(s); 00407 } else if (CONFIG_MPEG4_DECODER && s->h263_pred) { 00408 if(s->avctx->extradata_size && s->picture_number==0){ 00409 GetBitContext gb; 00410 00411 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8); 00412 ret = ff_mpeg4_decode_picture_header(s, &gb); 00413 } 00414 ret = ff_mpeg4_decode_picture_header(s, &s->gb); 00415 } else if (CONFIG_H263I_DECODER && s->codec_id == CODEC_ID_H263I) { 00416 ret = ff_intel_h263_decode_picture_header(s); 00417 } else if (CONFIG_FLV_DECODER && s->h263_flv) { 00418 ret = ff_flv_decode_picture_header(s); 00419 } else { 00420 ret = h263_decode_picture_header(s); 00421 } 00422 00423 if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size); 00424 00425 /* skip if the header was thrashed */ 00426 if (ret < 0){ 00427 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n"); 00428 return -1; 00429 } 00430 00431 avctx->has_b_frames= !s->low_delay; 00432 00433 if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){ 00434 if(s->stream_codec_tag == AV_RL32("XVID") || 00435 s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") || 00436 s->codec_tag == AV_RL32("RMP4") || 00437 s->codec_tag == AV_RL32("SIPP") 00438 ) 00439 s->xvid_build= 0; 00440 #if 0 00441 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1 00442 && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc 00443 s->xvid_build= 0; 00444 #endif 00445 } 00446 00447 if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){ 00448 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0) 00449 s->divx_version= 400; //divx 4 00450 } 00451 00452 if(s->xvid_build>=0 && s->divx_version>=0){ 00453 s->divx_version= 00454 s->divx_build= -1; 00455 } 00456 00457 if(s->workaround_bugs&FF_BUG_AUTODETECT){ 00458 if(s->codec_tag == AV_RL32("XVIX")) 00459 s->workaround_bugs|= FF_BUG_XVID_ILACE; 00460 00461 if(s->codec_tag == AV_RL32("UMP4")){ 00462 s->workaround_bugs|= FF_BUG_UMP4; 00463 } 00464 00465 if(s->divx_version>=500 && s->divx_build<1814){ 00466 s->workaround_bugs|= FF_BUG_QPEL_CHROMA; 00467 } 00468 00469 if(s->divx_version>502 && s->divx_build<1814){ 00470 s->workaround_bugs|= FF_BUG_QPEL_CHROMA2; 00471 } 00472 00473 if(s->xvid_build<=3U) 00474 s->padding_bug_score= 256*256*256*64; 00475 00476 if(s->xvid_build<=1U) 00477 s->workaround_bugs|= FF_BUG_QPEL_CHROMA; 00478 00479 if(s->xvid_build<=12U) 00480 s->workaround_bugs|= FF_BUG_EDGE; 00481 00482 if(s->xvid_build<=32U) 00483 s->workaround_bugs|= FF_BUG_DC_CLIP; 00484 00485 #define SET_QPEL_FUNC(postfix1, postfix2) \ 00486 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\ 00487 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\ 00488 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2; 00489 00490 if(s->lavc_build<4653U) 00491 s->workaround_bugs|= FF_BUG_STD_QPEL; 00492 00493 if(s->lavc_build<4655U) 00494 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE; 00495 00496 if(s->lavc_build<4670U){ 00497 s->workaround_bugs|= FF_BUG_EDGE; 00498 } 00499 00500 if(s->lavc_build<=4712U) 00501 s->workaround_bugs|= FF_BUG_DC_CLIP; 00502 00503 if(s->divx_version>=0) 00504 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE; 00505 //printf("padding_bug_score: %d\n", s->padding_bug_score); 00506 if(s->divx_version==501 && s->divx_build==20020416) 00507 s->padding_bug_score= 256*256*256*64; 00508 00509 if(s->divx_version<500U){ 00510 s->workaround_bugs|= FF_BUG_EDGE; 00511 } 00512 00513 if(s->divx_version>=0) 00514 s->workaround_bugs|= FF_BUG_HPEL_CHROMA; 00515 #if 0 00516 if(s->divx_version==500) 00517 s->padding_bug_score= 256*256*256*64; 00518 00519 /* very ugly XVID padding bug detection FIXME/XXX solve this differently 00520 * Let us hope this at least works. 00521 */ 00522 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==-1 00523 && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0) 00524 s->workaround_bugs|= FF_BUG_NO_PADDING; 00525 00526 if(s->lavc_build<4609U) //FIXME not sure about the version num but a 4609 file seems ok 00527 s->workaround_bugs|= FF_BUG_NO_PADDING; 00528 #endif 00529 } 00530 00531 if(s->workaround_bugs& FF_BUG_STD_QPEL){ 00532 SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c) 00533 SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c) 00534 SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c) 00535 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c) 00536 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c) 00537 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c) 00538 00539 SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c) 00540 SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c) 00541 SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c) 00542 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c) 00543 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c) 00544 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c) 00545 } 00546 00547 if(avctx->debug & FF_DEBUG_BUGS) 00548 av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n", 00549 s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build, 00550 s->divx_packed ? "p" : ""); 00551 00552 #if HAVE_MMX 00553 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)) { 00554 avctx->idct_algo= FF_IDCT_XVIDMMX; 00555 avctx->coded_width= 0; // force reinit 00556 // dsputil_init(&s->dsp, avctx); 00557 s->picture_number=0; 00558 } 00559 #endif 00560 00561 /* After H263 & mpeg4 header decode we have the height, width,*/ 00562 /* and other parameters. So then we could init the picture */ 00563 /* FIXME: By the way H263 decoder is evolving it should have */ 00564 /* an H263EncContext */ 00565 00566 if ( s->width != avctx->coded_width 00567 || s->height != avctx->coded_height) { 00568 /* H.263 could change picture size any time */ 00569 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat 00570 s->parse_context.buffer=0; 00571 MPV_common_end(s); 00572 s->parse_context= pc; 00573 } 00574 if (!s->context_initialized) { 00575 avcodec_set_dimensions(avctx, s->width, s->height); 00576 00577 goto retry; 00578 } 00579 00580 if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P || s->codec_id == CODEC_ID_H263I)) 00581 s->gob_index = ff_h263_get_gob_height(s); 00582 00583 // for skipping the frame 00584 s->current_picture.pict_type= s->pict_type; 00585 s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I; 00586 00587 /* skip B-frames if we don't have reference frames */ 00588 if(s->last_picture_ptr==NULL && (s->pict_type==AV_PICTURE_TYPE_B || s->dropable)) return get_consumed_bytes(s, buf_size); 00589 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B) 00590 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I) 00591 || avctx->skip_frame >= AVDISCARD_ALL) 00592 return get_consumed_bytes(s, buf_size); 00593 00594 if(s->next_p_frame_damaged){ 00595 if(s->pict_type==AV_PICTURE_TYPE_B) 00596 return get_consumed_bytes(s, buf_size); 00597 else 00598 s->next_p_frame_damaged=0; 00599 } 00600 00601 if((s->avctx->flags2 & CODEC_FLAG2_FAST) && s->pict_type==AV_PICTURE_TYPE_B){ 00602 s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab; 00603 s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab; 00604 }else if((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){ 00605 s->me.qpel_put= s->dsp.put_qpel_pixels_tab; 00606 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab; 00607 }else{ 00608 s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab; 00609 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab; 00610 } 00611 00612 if(MPV_frame_start(s, avctx) < 0) 00613 return -1; 00614 00615 if (!s->divx_packed) ff_thread_finish_setup(avctx); 00616 00617 if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) { 00618 ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer); 00619 goto frame_end; 00620 } 00621 00622 if (avctx->hwaccel) { 00623 if (avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer) < 0) 00624 return -1; 00625 } 00626 00627 ff_er_frame_start(s); 00628 00629 //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type 00630 //which is not available before MPV_frame_start() 00631 if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){ 00632 ret = ff_wmv2_decode_secondary_picture_header(s); 00633 if(ret<0) return ret; 00634 if(ret==1) goto intrax8_decoded; 00635 } 00636 00637 /* decode each macroblock */ 00638 s->mb_x=0; 00639 s->mb_y=0; 00640 00641 decode_slice(s); 00642 while(s->mb_y<s->mb_height){ 00643 if(s->msmpeg4_version){ 00644 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) 00645 break; 00646 }else{ 00647 int prev_x=s->mb_x, prev_y=s->mb_y; 00648 if(ff_h263_resync(s)<0) 00649 break; 00650 if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x) 00651 s->error_occurred = 1; 00652 } 00653 00654 if(s->msmpeg4_version<4 && s->h263_pred) 00655 ff_mpeg4_clean_buffers(s); 00656 00657 decode_slice(s); 00658 } 00659 00660 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type==AV_PICTURE_TYPE_I) 00661 if(!CONFIG_MSMPEG4_DECODER || msmpeg4_decode_ext_header(s, buf_size) < 0){ 00662 s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR; 00663 } 00664 00665 assert(s->bitstream_buffer_size==0); 00666 frame_end: 00667 /* divx 5.01+ bistream reorder stuff */ 00668 if(s->codec_id==CODEC_ID_MPEG4 && s->divx_packed){ 00669 int current_pos= get_bits_count(&s->gb)>>3; 00670 int startcode_found=0; 00671 00672 if(buf_size - current_pos > 5){ 00673 int i; 00674 for(i=current_pos; i<buf_size-3; i++){ 00675 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){ 00676 startcode_found=1; 00677 break; 00678 } 00679 } 00680 } 00681 if(s->gb.buffer == s->bitstream_buffer && buf_size>7 && s->xvid_build>=0){ //xvid style 00682 startcode_found=1; 00683 current_pos=0; 00684 } 00685 00686 if(startcode_found){ 00687 av_fast_malloc( 00688 &s->bitstream_buffer, 00689 &s->allocated_bitstream_buffer_size, 00690 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE); 00691 if (!s->bitstream_buffer) 00692 return AVERROR(ENOMEM); 00693 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos); 00694 s->bitstream_buffer_size= buf_size - current_pos; 00695 } 00696 } 00697 00698 intrax8_decoded: 00699 ff_er_frame_end(s); 00700 00701 if (avctx->hwaccel) { 00702 if (avctx->hwaccel->end_frame(avctx) < 0) 00703 return -1; 00704 } 00705 00706 MPV_frame_end(s); 00707 00708 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type); 00709 assert(s->current_picture.pict_type == s->pict_type); 00710 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) { 00711 *pict= *(AVFrame*)s->current_picture_ptr; 00712 } else if (s->last_picture_ptr != NULL) { 00713 *pict= *(AVFrame*)s->last_picture_ptr; 00714 } 00715 00716 if(s->last_picture_ptr || s->low_delay){ 00717 *data_size = sizeof(AVFrame); 00718 ff_print_debug_info(s, pict); 00719 } 00720 00721 #ifdef PRINT_FRAME_TIME 00722 av_log(avctx, AV_LOG_DEBUG, "%"PRId64"\n", rdtsc()-time); 00723 #endif 00724 00725 return get_consumed_bytes(s, buf_size); 00726 } 00727 00728 AVCodec ff_h263_decoder = { 00729 "h263", 00730 AVMEDIA_TYPE_VIDEO, 00731 CODEC_ID_H263, 00732 sizeof(MpegEncContext), 00733 ff_h263_decode_init, 00734 NULL, 00735 ff_h263_decode_end, 00736 ff_h263_decode_frame, 00737 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, 00738 .flush= ff_mpeg_flush, 00739 .max_lowres= 3, 00740 .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"), 00741 .pix_fmts= ff_hwaccel_pixfmt_list_420, 00742 };