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.

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