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.

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