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.

796 lines
24KB

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