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.

846 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. s->unrestricted_mv= 1;
  54. /* select sub codec */
  55. switch(avctx->codec->id) {
  56. case CODEC_ID_H263:
  57. s->gob_number = 0;
  58. break;
  59. case CODEC_ID_MPEG4:
  60. s->time_increment_bits = 4; /* default value for broken headers */
  61. s->h263_pred = 1;
  62. s->low_delay = 0; //default, might be overriden in the vol header during header parsing
  63. break;
  64. case CODEC_ID_MSMPEG4V1:
  65. s->h263_msmpeg4 = 1;
  66. s->h263_pred = 1;
  67. s->msmpeg4_version=1;
  68. break;
  69. case CODEC_ID_MSMPEG4V2:
  70. s->h263_msmpeg4 = 1;
  71. s->h263_pred = 1;
  72. s->msmpeg4_version=2;
  73. break;
  74. case CODEC_ID_MSMPEG4V3:
  75. s->h263_msmpeg4 = 1;
  76. s->h263_pred = 1;
  77. s->msmpeg4_version=3;
  78. break;
  79. case CODEC_ID_WMV1:
  80. s->h263_msmpeg4 = 1;
  81. s->h263_pred = 1;
  82. s->msmpeg4_version=4;
  83. break;
  84. case CODEC_ID_WMV2:
  85. s->h263_msmpeg4 = 1;
  86. s->h263_pred = 1;
  87. s->msmpeg4_version=5;
  88. break;
  89. case CODEC_ID_H263I:
  90. s->h263_intel = 1;
  91. break;
  92. case CODEC_ID_FLV1:
  93. s->h263_flv = 1;
  94. break;
  95. default:
  96. return -1;
  97. }
  98. s->codec_id= avctx->codec->id;
  99. /* for h263, we allocate the images after having read the header */
  100. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  101. if (MPV_common_init(s) < 0)
  102. return -1;
  103. if (s->h263_msmpeg4)
  104. ff_msmpeg4_decode_init(s);
  105. else
  106. h263_decode_init_vlc(s);
  107. return 0;
  108. }
  109. int ff_h263_decode_end(AVCodecContext *avctx)
  110. {
  111. MpegEncContext *s = avctx->priv_data;
  112. MPV_common_end(s);
  113. return 0;
  114. }
  115. /**
  116. * retunrs the number of bytes consumed for building the current frame
  117. */
  118. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  119. int pos= (get_bits_count(&s->gb)+7)>>3;
  120. if(s->divx_packed){
  121. //we would have to scan through the whole buf to handle the weird reordering ...
  122. return buf_size;
  123. }else if(s->flags&CODEC_FLAG_TRUNCATED){
  124. pos -= s->parse_context.last_index;
  125. if(pos<0) pos=0; // padding is not really read so this might be -1
  126. return pos;
  127. }else{
  128. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  129. if(pos+10>buf_size) pos=buf_size; // oops ;)
  130. return pos;
  131. }
  132. }
  133. static int decode_slice(MpegEncContext *s){
  134. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  135. s->last_resync_gb= s->gb;
  136. s->first_slice_line= 1;
  137. s->resync_mb_x= s->mb_x;
  138. s->resync_mb_y= s->mb_y;
  139. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  140. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  141. if(s->partitioned_frame){
  142. const int qscale= s->qscale;
  143. if(s->codec_id==CODEC_ID_MPEG4){
  144. if(ff_mpeg4_decode_partitions(s) < 0)
  145. return -1;
  146. }
  147. /* restore variables which were modified */
  148. s->first_slice_line=1;
  149. s->mb_x= s->resync_mb_x;
  150. s->mb_y= s->resync_mb_y;
  151. s->qscale= qscale;
  152. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  153. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  154. }
  155. for(; s->mb_y < s->mb_height; s->mb_y++) {
  156. /* per-row end of slice checks */
  157. if(s->msmpeg4_version){
  158. if(s->resync_mb_y + s->slice_height == s->mb_y){
  159. 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);
  160. return 0;
  161. }
  162. }
  163. if(s->msmpeg4_version==1){
  164. s->last_dc[0]=
  165. s->last_dc[1]=
  166. s->last_dc[2]= 128;
  167. }
  168. ff_init_block_index(s);
  169. for(; s->mb_x < s->mb_width; s->mb_x++) {
  170. int ret;
  171. ff_update_block_index(s);
  172. if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
  173. s->first_slice_line=0;
  174. }
  175. /* DCT & quantize */
  176. s->dsp.clear_blocks(s->block[0]);
  177. s->mv_dir = MV_DIR_FORWARD;
  178. s->mv_type = MV_TYPE_16X16;
  179. // s->mb_skiped = 0;
  180. //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
  181. ret= s->decode_mb(s, s->block);
  182. if (s->pict_type!=B_TYPE)
  183. ff_h263_update_motion_val(s);
  184. if(ret<0){
  185. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  186. if(ret==SLICE_END){
  187. MPV_decode_mb(s, s->block);
  188. //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));
  189. 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);
  190. s->padding_bug_score--;
  191. if(++s->mb_x >= s->mb_width){
  192. s->mb_x=0;
  193. ff_draw_horiz_band(s, s->mb_y*16, 16);
  194. s->mb_y++;
  195. }
  196. return 0;
  197. }else if(ret==SLICE_NOEND){
  198. fprintf(stderr,"Slice mismatch at MB: %d\n", xy);
  199. 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);
  200. return -1;
  201. }
  202. fprintf(stderr,"Error at MB: %d\n", xy);
  203. 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);
  204. return -1;
  205. }
  206. MPV_decode_mb(s, s->block);
  207. }
  208. ff_draw_horiz_band(s, s->mb_y*16, 16);
  209. s->mb_x= 0;
  210. }
  211. assert(s->mb_x==0 && s->mb_y==s->mb_height);
  212. /* try to detect the padding bug */
  213. if( s->codec_id==CODEC_ID_MPEG4
  214. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  215. && s->gb.size_in_bits - get_bits_count(&s->gb) >=0
  216. && s->gb.size_in_bits - get_bits_count(&s->gb) < 48
  217. // && !s->resync_marker
  218. && !s->data_partitioning){
  219. const int bits_count= get_bits_count(&s->gb);
  220. const int bits_left = s->gb.size_in_bits - bits_count;
  221. if(bits_left==0){
  222. s->padding_bug_score+=16;
  223. }else if(bits_left>8){
  224. s->padding_bug_score++;
  225. } else if(bits_left != 1){
  226. int v= show_bits(&s->gb, 8);
  227. v|= 0x7F >> (7-(bits_count&7));
  228. if(v==0x7F)
  229. s->padding_bug_score--;
  230. else
  231. s->padding_bug_score++;
  232. }
  233. }
  234. // handle formats which dont have unique end markers
  235. if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
  236. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  237. int max_extra=7;
  238. /* no markers in M$ crap */
  239. if(s->msmpeg4_version && s->pict_type==I_TYPE)
  240. max_extra+= 17;
  241. /* buggy padding but the frame should still end approximately at the bitstream end */
  242. if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3)
  243. max_extra+= 48;
  244. else if((s->workaround_bugs&FF_BUG_NO_PADDING))
  245. max_extra+= 256*256*256*64;
  246. if(left>max_extra){
  247. fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
  248. }
  249. else if(left<0){
  250. fprintf(stderr, "overreading %d bits\n", -left);
  251. }else
  252. 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);
  253. return 0;
  254. }
  255. fprintf(stderr, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
  256. s->gb.size_in_bits - get_bits_count(&s->gb),
  257. show_bits(&s->gb, 24), s->padding_bug_score);
  258. 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);
  259. return -1;
  260. }
  261. /**
  262. * finds the end of the current frame in the bitstream.
  263. * @return the position of the first byte of the next frame, or -1
  264. */
  265. static int mpeg4_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  266. ParseContext *pc= &s->parse_context;
  267. int vop_found, i;
  268. uint32_t state;
  269. vop_found= pc->frame_start_found;
  270. state= pc->state;
  271. i=0;
  272. if(!vop_found){
  273. for(i=0; i<buf_size; i++){
  274. state= (state<<8) | buf[i];
  275. if(state == 0x1B6){
  276. i++;
  277. vop_found=1;
  278. break;
  279. }
  280. }
  281. }
  282. if(vop_found){
  283. for(; i<buf_size; i++){
  284. state= (state<<8) | buf[i];
  285. if((state&0xFFFFFF00) == 0x100){
  286. pc->frame_start_found=0;
  287. pc->state=-1;
  288. return i-3;
  289. }
  290. }
  291. }
  292. pc->frame_start_found= vop_found;
  293. pc->state= state;
  294. return END_NOT_FOUND;
  295. }
  296. static int h263_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  297. ParseContext *pc= &s->parse_context;
  298. int vop_found, i;
  299. uint32_t state;
  300. vop_found= pc->frame_start_found;
  301. state= pc->state;
  302. i=0;
  303. if(!vop_found){
  304. for(i=0; i<buf_size; i++){
  305. state= (state<<8) | buf[i];
  306. if(state>>(32-22) == 0x20){
  307. i++;
  308. vop_found=1;
  309. break;
  310. }
  311. }
  312. }
  313. if(vop_found){
  314. for(; i<buf_size; i++){
  315. state= (state<<8) | buf[i];
  316. if(state>>(32-22) == 0x20){
  317. pc->frame_start_found=0;
  318. pc->state=-1;
  319. return i-3;
  320. }
  321. }
  322. }
  323. pc->frame_start_found= vop_found;
  324. pc->state= state;
  325. return END_NOT_FOUND;
  326. }
  327. int ff_h263_decode_frame(AVCodecContext *avctx,
  328. void *data, int *data_size,
  329. uint8_t *buf, int buf_size)
  330. {
  331. MpegEncContext *s = avctx->priv_data;
  332. int ret;
  333. AVFrame *pict = data;
  334. float new_aspect;
  335. #ifdef PRINT_FRAME_TIME
  336. uint64_t time= rdtsc();
  337. #endif
  338. #ifdef DEBUG
  339. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  340. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  341. #endif
  342. s->flags= avctx->flags;
  343. *data_size = 0;
  344. /* no supplementary picture */
  345. if (buf_size == 0) {
  346. /* special case for last picture */
  347. if (s->low_delay==0 && s->next_picture_ptr) {
  348. *pict= *(AVFrame*)s->next_picture_ptr;
  349. s->next_picture_ptr= NULL;
  350. *data_size = sizeof(AVFrame);
  351. }
  352. return 0;
  353. }
  354. if(s->flags&CODEC_FLAG_TRUNCATED){
  355. int next;
  356. if(s->codec_id==CODEC_ID_MPEG4){
  357. next= mpeg4_find_frame_end(s, buf, buf_size);
  358. }else if(s->codec_id==CODEC_ID_H263){
  359. next= h263_find_frame_end(s, buf, buf_size);
  360. }else{
  361. fprintf(stderr, "this codec doesnt support truncated bitstreams\n");
  362. return -1;
  363. }
  364. if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
  365. return buf_size;
  366. }
  367. retry:
  368. if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
  369. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
  370. }else
  371. init_get_bits(&s->gb, buf, buf_size*8);
  372. s->bitstream_buffer_size=0;
  373. if (!s->context_initialized) {
  374. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  375. return -1;
  376. }
  377. /* let's go :-) */
  378. if (s->msmpeg4_version==5) {
  379. ret= ff_wmv2_decode_picture_header(s);
  380. } else if (s->msmpeg4_version) {
  381. ret = msmpeg4_decode_picture_header(s);
  382. } else if (s->h263_pred) {
  383. if(s->avctx->extradata_size && s->picture_number==0){
  384. GetBitContext gb;
  385. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  386. ret = ff_mpeg4_decode_picture_header(s, &gb);
  387. }
  388. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  389. if(s->flags& CODEC_FLAG_LOW_DELAY)
  390. s->low_delay=1;
  391. } else if (s->h263_intel) {
  392. ret = intel_h263_decode_picture_header(s);
  393. } else if (s->h263_flv) {
  394. ret = flv_h263_decode_picture_header(s);
  395. } else {
  396. ret = h263_decode_picture_header(s);
  397. }
  398. if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
  399. /* skip if the header was thrashed */
  400. if (ret < 0){
  401. fprintf(stderr, "header damaged\n");
  402. return -1;
  403. }
  404. avctx->has_b_frames= !s->low_delay;
  405. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  406. if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") ||
  407. s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  408. s->xvid_build= -1;
  409. #if 0
  410. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  411. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  412. s->xvid_build= -1;
  413. #endif
  414. }
  415. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  416. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  417. s->divx_version= 400; //divx 4
  418. }
  419. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  420. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  421. if(s->padding_bug_score > -2 && !s->data_partitioning && (s->divx_version || !s->resync_marker))
  422. s->workaround_bugs |= FF_BUG_NO_PADDING;
  423. if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  424. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  425. if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
  426. s->workaround_bugs|= FF_BUG_UMP4;
  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<4670){
  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. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  509. s->parse_context.buffer=0;
  510. MPV_common_end(s);
  511. s->parse_context= pc;
  512. }
  513. if (!s->context_initialized) {
  514. avctx->width = s->width;
  515. avctx->height = s->height;
  516. avctx->aspect_ratio= new_aspect;
  517. goto retry;
  518. }
  519. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  520. s->gob_index = ff_h263_get_gob_height(s);
  521. // for hurry_up==5
  522. s->current_picture.pict_type= s->pict_type;
  523. s->current_picture.key_frame= s->pict_type == I_TYPE;
  524. /* skip b frames if we dont have reference frames */
  525. if(s->last_picture_ptr==NULL && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  526. /* skip b frames if we are in a hurry */
  527. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  528. /* skip everything if we are in a hurry>=5 */
  529. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  530. if(s->next_p_frame_damaged){
  531. if(s->pict_type==B_TYPE)
  532. return get_consumed_bytes(s, buf_size);
  533. else
  534. s->next_p_frame_damaged=0;
  535. }
  536. if(MPV_frame_start(s, avctx) < 0)
  537. return -1;
  538. #ifdef DEBUG
  539. printf("qscale=%d\n", s->qscale);
  540. #endif
  541. ff_er_frame_start(s);
  542. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  543. //which isnt available before MPV_frame_start()
  544. if (s->msmpeg4_version==5){
  545. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  546. return -1;
  547. }
  548. /* decode each macroblock */
  549. s->mb_x=0;
  550. s->mb_y=0;
  551. decode_slice(s);
  552. while(s->mb_y<s->mb_height){
  553. if(s->msmpeg4_version){
  554. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  555. break;
  556. }else{
  557. if(ff_h263_resync(s)<0)
  558. break;
  559. }
  560. if(s->msmpeg4_version<4 && s->h263_pred)
  561. ff_mpeg4_clean_buffers(s);
  562. decode_slice(s);
  563. }
  564. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  565. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  566. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  567. }
  568. /* divx 5.01+ bistream reorder stuff */
  569. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  570. int current_pos= get_bits_count(&s->gb)>>3;
  571. if( buf_size - current_pos > 5
  572. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  573. int i;
  574. int startcode_found=0;
  575. for(i=current_pos; i<buf_size-3; i++){
  576. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  577. startcode_found=1;
  578. break;
  579. }
  580. }
  581. if(startcode_found){
  582. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  583. s->bitstream_buffer_size= buf_size - current_pos;
  584. }
  585. }
  586. }
  587. ff_er_frame_end(s);
  588. MPV_frame_end(s);
  589. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  590. assert(s->current_picture.pict_type == s->pict_type);
  591. if(s->pict_type==B_TYPE || s->low_delay){
  592. *pict= *(AVFrame*)&s->current_picture;
  593. ff_print_debug_info(s, s->current_picture_ptr);
  594. } else {
  595. *pict= *(AVFrame*)&s->last_picture;
  596. ff_print_debug_info(s, s->last_picture_ptr);
  597. }
  598. /* Return the Picture timestamp as the frame number */
  599. /* we substract 1 because it is added on utils.c */
  600. avctx->frame_number = s->picture_number - 1;
  601. /* dont output the last pic after seeking */
  602. if(s->last_picture_ptr || s->low_delay)
  603. *data_size = sizeof(AVFrame);
  604. #ifdef PRINT_FRAME_TIME
  605. printf("%Ld\n", rdtsc()-time);
  606. #endif
  607. return get_consumed_bytes(s, buf_size);
  608. }
  609. static const AVOption mpeg4_decoptions[] =
  610. {
  611. AVOPTION_SUB(avoptions_workaround_bug),
  612. AVOPTION_END()
  613. };
  614. AVCodec mpeg4_decoder = {
  615. "mpeg4",
  616. CODEC_TYPE_VIDEO,
  617. CODEC_ID_MPEG4,
  618. sizeof(MpegEncContext),
  619. ff_h263_decode_init,
  620. NULL,
  621. ff_h263_decode_end,
  622. ff_h263_decode_frame,
  623. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  624. .options = mpeg4_decoptions,
  625. .flush= ff_mpeg_flush,
  626. };
  627. AVCodec h263_decoder = {
  628. "h263",
  629. CODEC_TYPE_VIDEO,
  630. CODEC_ID_H263,
  631. sizeof(MpegEncContext),
  632. ff_h263_decode_init,
  633. NULL,
  634. ff_h263_decode_end,
  635. ff_h263_decode_frame,
  636. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  637. .flush= ff_mpeg_flush,
  638. };
  639. AVCodec msmpeg4v1_decoder = {
  640. "msmpeg4v1",
  641. CODEC_TYPE_VIDEO,
  642. CODEC_ID_MSMPEG4V1,
  643. sizeof(MpegEncContext),
  644. ff_h263_decode_init,
  645. NULL,
  646. ff_h263_decode_end,
  647. ff_h263_decode_frame,
  648. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  649. mpeg4_decoptions,
  650. };
  651. AVCodec msmpeg4v2_decoder = {
  652. "msmpeg4v2",
  653. CODEC_TYPE_VIDEO,
  654. CODEC_ID_MSMPEG4V2,
  655. sizeof(MpegEncContext),
  656. ff_h263_decode_init,
  657. NULL,
  658. ff_h263_decode_end,
  659. ff_h263_decode_frame,
  660. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  661. mpeg4_decoptions,
  662. };
  663. AVCodec msmpeg4v3_decoder = {
  664. "msmpeg4",
  665. CODEC_TYPE_VIDEO,
  666. CODEC_ID_MSMPEG4V3,
  667. sizeof(MpegEncContext),
  668. ff_h263_decode_init,
  669. NULL,
  670. ff_h263_decode_end,
  671. ff_h263_decode_frame,
  672. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  673. .options = mpeg4_decoptions,
  674. };
  675. AVCodec wmv1_decoder = {
  676. "wmv1",
  677. CODEC_TYPE_VIDEO,
  678. CODEC_ID_WMV1,
  679. sizeof(MpegEncContext),
  680. ff_h263_decode_init,
  681. NULL,
  682. ff_h263_decode_end,
  683. ff_h263_decode_frame,
  684. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  685. mpeg4_decoptions,
  686. };
  687. AVCodec h263i_decoder = {
  688. "h263i",
  689. CODEC_TYPE_VIDEO,
  690. CODEC_ID_H263I,
  691. sizeof(MpegEncContext),
  692. ff_h263_decode_init,
  693. NULL,
  694. ff_h263_decode_end,
  695. ff_h263_decode_frame,
  696. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  697. mpeg4_decoptions,
  698. };
  699. AVCodec flv_decoder = {
  700. "flv",
  701. CODEC_TYPE_VIDEO,
  702. CODEC_ID_FLV1,
  703. sizeof(MpegEncContext),
  704. ff_h263_decode_init,
  705. NULL,
  706. ff_h263_decode_end,
  707. ff_h263_decode_frame,
  708. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
  709. };