You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

843 lines
25KB

  1. /*
  2. * H.263 decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file h263dec.c
  21. * H.263 decoder.
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. //#define DEBUG
  27. //#define PRINT_FRAME_TIME
  28. #ifdef PRINT_FRAME_TIME
  29. static inline long long rdtsc()
  30. {
  31. long long l;
  32. asm volatile( "rdtsc\n\t"
  33. : "=A" (l)
  34. );
  35. // printf("%d\n", int(l/1000));
  36. return l;
  37. }
  38. #endif
  39. int ff_h263_decode_init(AVCodecContext *avctx)
  40. {
  41. MpegEncContext *s = avctx->priv_data;
  42. s->avctx = avctx;
  43. s->out_format = FMT_H263;
  44. s->width = avctx->width;
  45. s->height = avctx->height;
  46. s->workaround_bugs= avctx->workaround_bugs;
  47. // set defaults
  48. s->quant_precision=5;
  49. s->progressive_sequence=1;
  50. s->decode_mb= ff_h263_decode_mb;
  51. s->low_delay= 1;
  52. avctx->pix_fmt= PIX_FMT_YUV420P;
  53. /* select sub codec */
  54. switch(avctx->codec->id) {
  55. case CODEC_ID_H263:
  56. s->gob_number = 0;
  57. break;
  58. case CODEC_ID_MPEG4:
  59. s->time_increment_bits = 4; /* default value for broken headers */
  60. s->h263_pred = 1;
  61. s->low_delay = 0; //default, might be overriden in the vol header during header parsing
  62. break;
  63. case CODEC_ID_MSMPEG4V1:
  64. s->h263_msmpeg4 = 1;
  65. s->h263_pred = 1;
  66. s->msmpeg4_version=1;
  67. break;
  68. case CODEC_ID_MSMPEG4V2:
  69. s->h263_msmpeg4 = 1;
  70. s->h263_pred = 1;
  71. s->msmpeg4_version=2;
  72. break;
  73. case CODEC_ID_MSMPEG4V3:
  74. s->h263_msmpeg4 = 1;
  75. s->h263_pred = 1;
  76. s->msmpeg4_version=3;
  77. break;
  78. case CODEC_ID_WMV1:
  79. s->h263_msmpeg4 = 1;
  80. s->h263_pred = 1;
  81. s->msmpeg4_version=4;
  82. break;
  83. case CODEC_ID_WMV2:
  84. s->h263_msmpeg4 = 1;
  85. s->h263_pred = 1;
  86. s->msmpeg4_version=5;
  87. break;
  88. case CODEC_ID_H263I:
  89. s->h263_intel = 1;
  90. break;
  91. case CODEC_ID_FLV1:
  92. s->h263_flv = 1;
  93. break;
  94. default:
  95. return -1;
  96. }
  97. s->codec_id= avctx->codec->id;
  98. /* for h263, we allocate the images after having read the header */
  99. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  100. if (MPV_common_init(s) < 0)
  101. return -1;
  102. if (s->h263_msmpeg4)
  103. ff_msmpeg4_decode_init(s);
  104. else
  105. h263_decode_init_vlc(s);
  106. return 0;
  107. }
  108. int ff_h263_decode_end(AVCodecContext *avctx)
  109. {
  110. MpegEncContext *s = avctx->priv_data;
  111. MPV_common_end(s);
  112. return 0;
  113. }
  114. /**
  115. * retunrs the number of bytes consumed for building the current frame
  116. */
  117. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  118. int pos= (get_bits_count(&s->gb)+7)>>3;
  119. if(s->divx_packed){
  120. //we would have to scan through the whole buf to handle the weird reordering ...
  121. return buf_size;
  122. }else if(s->flags&CODEC_FLAG_TRUNCATED){
  123. pos -= s->parse_context.last_index;
  124. if(pos<0) pos=0; // padding is not really read so this might be -1
  125. return pos;
  126. }else{
  127. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  128. if(pos+10>buf_size) pos=buf_size; // oops ;)
  129. return pos;
  130. }
  131. }
  132. static int decode_slice(MpegEncContext *s){
  133. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  134. s->last_resync_gb= s->gb;
  135. s->first_slice_line= 1;
  136. s->resync_mb_x= s->mb_x;
  137. s->resync_mb_y= s->mb_y;
  138. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  139. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  140. if(s->partitioned_frame){
  141. const int qscale= s->qscale;
  142. if(s->codec_id==CODEC_ID_MPEG4){
  143. if(ff_mpeg4_decode_partitions(s) < 0)
  144. return -1;
  145. }
  146. /* restore variables which were modified */
  147. s->first_slice_line=1;
  148. s->mb_x= s->resync_mb_x;
  149. s->mb_y= s->resync_mb_y;
  150. s->qscale= qscale;
  151. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  152. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  153. }
  154. for(; s->mb_y < s->mb_height; s->mb_y++) {
  155. /* per-row end of slice checks */
  156. if(s->msmpeg4_version){
  157. if(s->resync_mb_y + s->slice_height == s->mb_y){
  158. 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);
  159. return 0;
  160. }
  161. }
  162. if(s->msmpeg4_version==1){
  163. s->last_dc[0]=
  164. s->last_dc[1]=
  165. s->last_dc[2]= 128;
  166. }
  167. ff_init_block_index(s);
  168. for(; s->mb_x < s->mb_width; s->mb_x++) {
  169. int ret;
  170. ff_update_block_index(s);
  171. if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
  172. s->first_slice_line=0;
  173. }
  174. /* DCT & quantize */
  175. s->dsp.clear_blocks(s->block[0]);
  176. s->mv_dir = MV_DIR_FORWARD;
  177. s->mv_type = MV_TYPE_16X16;
  178. // s->mb_skiped = 0;
  179. //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
  180. ret= s->decode_mb(s, s->block);
  181. if (s->pict_type!=B_TYPE)
  182. ff_h263_update_motion_val(s);
  183. if(ret<0){
  184. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  185. if(ret==SLICE_END){
  186. MPV_decode_mb(s, s->block);
  187. //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));
  188. 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);
  189. s->padding_bug_score--;
  190. if(++s->mb_x >= s->mb_width){
  191. s->mb_x=0;
  192. ff_draw_horiz_band(s, s->mb_y*16, 16);
  193. s->mb_y++;
  194. }
  195. return 0;
  196. }else if(ret==SLICE_NOEND){
  197. fprintf(stderr,"Slice mismatch at MB: %d\n", xy);
  198. 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);
  199. return -1;
  200. }
  201. fprintf(stderr,"Error at MB: %d\n", xy);
  202. 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);
  203. return -1;
  204. }
  205. MPV_decode_mb(s, s->block);
  206. }
  207. ff_draw_horiz_band(s, s->mb_y*16, 16);
  208. s->mb_x= 0;
  209. }
  210. assert(s->mb_x==0 && s->mb_y==s->mb_height);
  211. /* try to detect the padding bug */
  212. if( s->codec_id==CODEC_ID_MPEG4
  213. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  214. && s->gb.size_in_bits - get_bits_count(&s->gb) >=0
  215. && s->gb.size_in_bits - get_bits_count(&s->gb) < 48
  216. // && !s->resync_marker
  217. && !s->data_partitioning){
  218. const int bits_count= get_bits_count(&s->gb);
  219. const int bits_left = s->gb.size_in_bits - bits_count;
  220. if(bits_left==0){
  221. s->padding_bug_score+=16;
  222. }else if(bits_left>8){
  223. s->padding_bug_score++;
  224. } else if(bits_left != 1){
  225. int v= show_bits(&s->gb, 8);
  226. v|= 0x7F >> (7-(bits_count&7));
  227. if(v==0x7F)
  228. s->padding_bug_score--;
  229. else
  230. s->padding_bug_score++;
  231. }
  232. }
  233. // handle formats which dont have unique end markers
  234. if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
  235. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  236. int max_extra=7;
  237. /* no markers in M$ crap */
  238. if(s->msmpeg4_version && s->pict_type==I_TYPE)
  239. max_extra+= 17;
  240. /* buggy padding but the frame should still end approximately at the bitstream end */
  241. if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3)
  242. max_extra+= 48;
  243. else if((s->workaround_bugs&FF_BUG_NO_PADDING))
  244. max_extra+= 256*256*256*64;
  245. if(left>max_extra){
  246. fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
  247. }
  248. else if(left<0){
  249. fprintf(stderr, "overreading %d bits\n", -left);
  250. }else
  251. 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);
  252. return 0;
  253. }
  254. fprintf(stderr, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
  255. s->gb.size_in_bits - get_bits_count(&s->gb),
  256. show_bits(&s->gb, 24), s->padding_bug_score);
  257. 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);
  258. return -1;
  259. }
  260. /**
  261. * finds the end of the current frame in the bitstream.
  262. * @return the position of the first byte of the next frame, or -1
  263. */
  264. static int mpeg4_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  265. ParseContext *pc= &s->parse_context;
  266. int vop_found, i;
  267. uint32_t state;
  268. vop_found= pc->frame_start_found;
  269. state= pc->state;
  270. i=0;
  271. if(!vop_found){
  272. for(i=0; i<buf_size; i++){
  273. state= (state<<8) | buf[i];
  274. if(state == 0x1B6){
  275. i++;
  276. vop_found=1;
  277. break;
  278. }
  279. }
  280. }
  281. if(vop_found){
  282. for(; i<buf_size; i++){
  283. state= (state<<8) | buf[i];
  284. if((state&0xFFFFFF00) == 0x100){
  285. pc->frame_start_found=0;
  286. pc->state=-1;
  287. return i-3;
  288. }
  289. }
  290. }
  291. pc->frame_start_found= vop_found;
  292. pc->state= state;
  293. return END_NOT_FOUND;
  294. }
  295. static int h263_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  296. ParseContext *pc= &s->parse_context;
  297. int vop_found, i;
  298. uint32_t state;
  299. vop_found= pc->frame_start_found;
  300. state= pc->state;
  301. i=0;
  302. if(!vop_found){
  303. for(i=0; i<buf_size; i++){
  304. state= (state<<8) | buf[i];
  305. if(state>>(32-22) == 0x20){
  306. i++;
  307. vop_found=1;
  308. break;
  309. }
  310. }
  311. }
  312. if(vop_found){
  313. for(; i<buf_size; i++){
  314. state= (state<<8) | buf[i];
  315. if(state>>(32-22) == 0x20){
  316. pc->frame_start_found=0;
  317. pc->state=-1;
  318. return i-3;
  319. }
  320. }
  321. }
  322. pc->frame_start_found= vop_found;
  323. pc->state= state;
  324. return END_NOT_FOUND;
  325. }
  326. int ff_h263_decode_frame(AVCodecContext *avctx,
  327. void *data, int *data_size,
  328. uint8_t *buf, int buf_size)
  329. {
  330. MpegEncContext *s = avctx->priv_data;
  331. int ret;
  332. AVFrame *pict = data;
  333. float new_aspect;
  334. #ifdef PRINT_FRAME_TIME
  335. uint64_t time= rdtsc();
  336. #endif
  337. #ifdef DEBUG
  338. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  339. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  340. #endif
  341. s->flags= avctx->flags;
  342. *data_size = 0;
  343. /* no supplementary picture */
  344. if (buf_size == 0) {
  345. /* special case for last picture */
  346. if (s->low_delay==0 && s->next_picture_ptr) {
  347. *pict= *(AVFrame*)s->next_picture_ptr;
  348. s->next_picture_ptr= NULL;
  349. *data_size = sizeof(AVFrame);
  350. }
  351. return 0;
  352. }
  353. if(s->flags&CODEC_FLAG_TRUNCATED){
  354. int next;
  355. if(s->codec_id==CODEC_ID_MPEG4){
  356. next= mpeg4_find_frame_end(s, buf, buf_size);
  357. }else if(s->codec_id==CODEC_ID_H263){
  358. next= h263_find_frame_end(s, buf, buf_size);
  359. }else{
  360. fprintf(stderr, "this codec doesnt support truncated bitstreams\n");
  361. return -1;
  362. }
  363. if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
  364. return buf_size;
  365. }
  366. retry:
  367. if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
  368. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
  369. }else
  370. init_get_bits(&s->gb, buf, buf_size*8);
  371. s->bitstream_buffer_size=0;
  372. if (!s->context_initialized) {
  373. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  374. return -1;
  375. }
  376. /* let's go :-) */
  377. if (s->msmpeg4_version==5) {
  378. ret= ff_wmv2_decode_picture_header(s);
  379. } else if (s->msmpeg4_version) {
  380. ret = msmpeg4_decode_picture_header(s);
  381. } else if (s->h263_pred) {
  382. if(s->avctx->extradata_size && s->picture_number==0){
  383. GetBitContext gb;
  384. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  385. ret = ff_mpeg4_decode_picture_header(s, &gb);
  386. }
  387. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  388. if(s->flags& CODEC_FLAG_LOW_DELAY)
  389. s->low_delay=1;
  390. } else if (s->h263_intel) {
  391. ret = intel_h263_decode_picture_header(s);
  392. } else if (s->h263_flv) {
  393. ret = flv_h263_decode_picture_header(s);
  394. } else {
  395. ret = h263_decode_picture_header(s);
  396. }
  397. if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
  398. /* skip if the header was thrashed */
  399. if (ret < 0){
  400. fprintf(stderr, "header damaged\n");
  401. return -1;
  402. }
  403. avctx->has_b_frames= !s->low_delay;
  404. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  405. if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") ||
  406. s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  407. s->xvid_build= -1;
  408. #if 0
  409. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  410. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  411. s->xvid_build= -1;
  412. #endif
  413. }
  414. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  415. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  416. s->divx_version= 400; //divx 4
  417. }
  418. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  419. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  420. if(s->padding_bug_score > -2 && !s->data_partitioning && (s->divx_version || !s->resync_marker))
  421. s->workaround_bugs |= FF_BUG_NO_PADDING;
  422. if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  423. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  424. if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
  425. s->workaround_bugs|= FF_BUG_UMP4;
  426. s->workaround_bugs|= FF_BUG_AC_VLC;
  427. }
  428. if(s->divx_version>=500){
  429. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  430. }
  431. if(s->divx_version>502){
  432. s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
  433. }
  434. if(s->xvid_build && s->xvid_build<=3)
  435. s->padding_bug_score= 256*256*256*64;
  436. if(s->xvid_build && s->xvid_build<=1)
  437. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  438. if(s->xvid_build && s->xvid_build<=12)
  439. s->workaround_bugs|= FF_BUG_EDGE;
  440. #define SET_QPEL_FUNC(postfix1, postfix2) \
  441. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  442. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  443. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  444. if(s->lavc_build && s->lavc_build<4653)
  445. s->workaround_bugs|= FF_BUG_STD_QPEL;
  446. if(s->lavc_build && s->lavc_build<4655)
  447. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  448. if(s->lavc_build && s->lavc_build<4618){
  449. s->workaround_bugs|= FF_BUG_EDGE;
  450. }
  451. if(s->divx_version)
  452. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  453. //printf("padding_bug_score: %d\n", s->padding_bug_score);
  454. if(s->divx_version==501 && s->divx_build==20020416)
  455. s->padding_bug_score= 256*256*256*64;
  456. if(s->divx_version && s->divx_version<500){
  457. s->workaround_bugs|= FF_BUG_EDGE;
  458. }
  459. #if 0
  460. if(s->divx_version==500)
  461. s->padding_bug_score= 256*256*256*64;
  462. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  463. * lets hope this at least works
  464. */
  465. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  466. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  467. s->workaround_bugs|= FF_BUG_NO_PADDING;
  468. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  469. s->workaround_bugs|= FF_BUG_NO_PADDING;
  470. #endif
  471. }
  472. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  473. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  474. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  475. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  476. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  477. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  478. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  479. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  480. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  481. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  482. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  483. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  484. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  485. }
  486. if(avctx->debug & FF_DEBUG_BUGS)
  487. printf("bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  488. s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
  489. s->divx_packed ? "p" : "");
  490. #if 0 // dump bits per frame / qp / complexity
  491. {
  492. static FILE *f=NULL;
  493. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  494. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  495. }
  496. #endif
  497. /* After H263 & mpeg4 header decode we have the height, width,*/
  498. /* and other parameters. So then we could init the picture */
  499. /* FIXME: By the way H263 decoder is evolving it should have */
  500. /* an H263EncContext */
  501. if(s->aspected_height)
  502. new_aspect= s->aspected_width*s->width / (float)(s->height*s->aspected_height);
  503. else
  504. new_aspect=0;
  505. if ( s->width != avctx->width || s->height != avctx->height
  506. || ABS(new_aspect - avctx->aspect_ratio) > 0.001) {
  507. /* H.263 could change picture size any time */
  508. MPV_common_end(s);
  509. }
  510. if (!s->context_initialized) {
  511. avctx->width = s->width;
  512. avctx->height = s->height;
  513. avctx->aspect_ratio= new_aspect;
  514. goto retry;
  515. }
  516. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  517. s->gob_index = ff_h263_get_gob_height(s);
  518. // for hurry_up==5
  519. s->current_picture.pict_type= s->pict_type;
  520. s->current_picture.key_frame= s->pict_type == I_TYPE;
  521. /* skip b frames if we dont have reference frames */
  522. if(s->last_picture_ptr==NULL && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  523. /* skip b frames if we are in a hurry */
  524. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  525. /* skip everything if we are in a hurry>=5 */
  526. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  527. if(s->next_p_frame_damaged){
  528. if(s->pict_type==B_TYPE)
  529. return get_consumed_bytes(s, buf_size);
  530. else
  531. s->next_p_frame_damaged=0;
  532. }
  533. if(MPV_frame_start(s, avctx) < 0)
  534. return -1;
  535. #ifdef DEBUG
  536. printf("qscale=%d\n", s->qscale);
  537. #endif
  538. ff_er_frame_start(s);
  539. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  540. //which isnt available before MPV_frame_start()
  541. if (s->msmpeg4_version==5){
  542. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  543. return -1;
  544. }
  545. /* decode each macroblock */
  546. s->mb_x=0;
  547. s->mb_y=0;
  548. decode_slice(s);
  549. while(s->mb_y<s->mb_height){
  550. if(s->msmpeg4_version){
  551. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  552. break;
  553. }else{
  554. if(ff_h263_resync(s)<0)
  555. break;
  556. }
  557. if(s->msmpeg4_version<4 && s->h263_pred)
  558. ff_mpeg4_clean_buffers(s);
  559. decode_slice(s);
  560. }
  561. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  562. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  563. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  564. }
  565. /* divx 5.01+ bistream reorder stuff */
  566. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  567. int current_pos= get_bits_count(&s->gb)>>3;
  568. if( buf_size - current_pos > 5
  569. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  570. int i;
  571. int startcode_found=0;
  572. for(i=current_pos; i<buf_size-3; i++){
  573. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  574. startcode_found=1;
  575. break;
  576. }
  577. }
  578. if(startcode_found){
  579. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  580. s->bitstream_buffer_size= buf_size - current_pos;
  581. }
  582. }
  583. }
  584. ff_er_frame_end(s);
  585. MPV_frame_end(s);
  586. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  587. assert(s->current_picture.pict_type == s->pict_type);
  588. if(s->pict_type==B_TYPE || s->low_delay){
  589. *pict= *(AVFrame*)&s->current_picture;
  590. ff_print_debug_info(s, s->current_picture_ptr);
  591. } else {
  592. *pict= *(AVFrame*)&s->last_picture;
  593. ff_print_debug_info(s, s->last_picture_ptr);
  594. }
  595. /* Return the Picture timestamp as the frame number */
  596. /* we substract 1 because it is added on utils.c */
  597. avctx->frame_number = s->picture_number - 1;
  598. /* dont output the last pic after seeking */
  599. if(s->last_picture_ptr || s->low_delay)
  600. *data_size = sizeof(AVFrame);
  601. #ifdef PRINT_FRAME_TIME
  602. printf("%Ld\n", rdtsc()-time);
  603. #endif
  604. return get_consumed_bytes(s, buf_size);
  605. }
  606. static const AVOption mpeg4_decoptions[] =
  607. {
  608. AVOPTION_SUB(avoptions_workaround_bug),
  609. AVOPTION_END()
  610. };
  611. AVCodec mpeg4_decoder = {
  612. "mpeg4",
  613. CODEC_TYPE_VIDEO,
  614. CODEC_ID_MPEG4,
  615. sizeof(MpegEncContext),
  616. ff_h263_decode_init,
  617. NULL,
  618. ff_h263_decode_end,
  619. ff_h263_decode_frame,
  620. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  621. .options = mpeg4_decoptions,
  622. .flush= ff_mpeg_flush,
  623. };
  624. AVCodec h263_decoder = {
  625. "h263",
  626. CODEC_TYPE_VIDEO,
  627. CODEC_ID_H263,
  628. sizeof(MpegEncContext),
  629. ff_h263_decode_init,
  630. NULL,
  631. ff_h263_decode_end,
  632. ff_h263_decode_frame,
  633. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  634. .flush= ff_mpeg_flush,
  635. };
  636. AVCodec msmpeg4v1_decoder = {
  637. "msmpeg4v1",
  638. CODEC_TYPE_VIDEO,
  639. CODEC_ID_MSMPEG4V1,
  640. sizeof(MpegEncContext),
  641. ff_h263_decode_init,
  642. NULL,
  643. ff_h263_decode_end,
  644. ff_h263_decode_frame,
  645. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  646. mpeg4_decoptions,
  647. };
  648. AVCodec msmpeg4v2_decoder = {
  649. "msmpeg4v2",
  650. CODEC_TYPE_VIDEO,
  651. CODEC_ID_MSMPEG4V2,
  652. sizeof(MpegEncContext),
  653. ff_h263_decode_init,
  654. NULL,
  655. ff_h263_decode_end,
  656. ff_h263_decode_frame,
  657. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  658. mpeg4_decoptions,
  659. };
  660. AVCodec msmpeg4v3_decoder = {
  661. "msmpeg4",
  662. CODEC_TYPE_VIDEO,
  663. CODEC_ID_MSMPEG4V3,
  664. sizeof(MpegEncContext),
  665. ff_h263_decode_init,
  666. NULL,
  667. ff_h263_decode_end,
  668. ff_h263_decode_frame,
  669. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  670. .options = mpeg4_decoptions,
  671. };
  672. AVCodec wmv1_decoder = {
  673. "wmv1",
  674. CODEC_TYPE_VIDEO,
  675. CODEC_ID_WMV1,
  676. sizeof(MpegEncContext),
  677. ff_h263_decode_init,
  678. NULL,
  679. ff_h263_decode_end,
  680. ff_h263_decode_frame,
  681. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  682. mpeg4_decoptions,
  683. };
  684. AVCodec h263i_decoder = {
  685. "h263i",
  686. CODEC_TYPE_VIDEO,
  687. CODEC_ID_H263I,
  688. sizeof(MpegEncContext),
  689. ff_h263_decode_init,
  690. NULL,
  691. ff_h263_decode_end,
  692. ff_h263_decode_frame,
  693. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  694. mpeg4_decoptions,
  695. };
  696. AVCodec flv_decoder = {
  697. "flv",
  698. CODEC_TYPE_VIDEO,
  699. CODEC_ID_FLV1,
  700. sizeof(MpegEncContext),
  701. ff_h263_decode_init,
  702. NULL,
  703. ff_h263_decode_end,
  704. ff_h263_decode_frame,
  705. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
  706. };