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.

818 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->lavc_build && s->lavc_build<4618){
  430. s->workaround_bugs|= FF_BUG_EDGE;
  431. }
  432. if(s->divx_version)
  433. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  434. //printf("padding_bug_score: %d\n", s->padding_bug_score);
  435. if(s->divx_version==501 && s->divx_build==20020416)
  436. s->padding_bug_score= 256*256*256*64;
  437. if(s->divx_version && s->divx_version<500){
  438. s->workaround_bugs|= FF_BUG_EDGE;
  439. }
  440. #if 0
  441. if(s->divx_version==500)
  442. s->padding_bug_score= 256*256*256*64;
  443. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  444. * lets hope this at least works
  445. */
  446. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  447. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  448. s->workaround_bugs|= FF_BUG_NO_PADDING;
  449. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  450. s->workaround_bugs|= FF_BUG_NO_PADDING;
  451. #endif
  452. }
  453. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  454. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  455. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  456. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  457. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  458. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  459. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  460. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  461. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  462. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  463. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  464. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  465. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  466. }
  467. #if 0 // dump bits per frame / qp / complexity
  468. {
  469. static FILE *f=NULL;
  470. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  471. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  472. }
  473. #endif
  474. /* After H263 & mpeg4 header decode we have the height, width,*/
  475. /* and other parameters. So then we could init the picture */
  476. /* FIXME: By the way H263 decoder is evolving it should have */
  477. /* an H263EncContext */
  478. if(s->aspected_height)
  479. new_aspect= s->aspected_width*s->width / (float)(s->height*s->aspected_height);
  480. else
  481. new_aspect=0;
  482. if ( s->width != avctx->width || s->height != avctx->height
  483. || ABS(new_aspect - avctx->aspect_ratio) > 0.001) {
  484. /* H.263 could change picture size any time */
  485. MPV_common_end(s);
  486. }
  487. if (!s->context_initialized) {
  488. avctx->width = s->width;
  489. avctx->height = s->height;
  490. avctx->aspect_ratio= new_aspect;
  491. goto retry;
  492. }
  493. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  494. s->gob_index = ff_h263_get_gob_height(s);
  495. if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
  496. /* skip if the header was thrashed */
  497. if (ret < 0){
  498. fprintf(stderr, "header damaged\n");
  499. return -1;
  500. }
  501. // for hurry_up==5
  502. s->current_picture.pict_type= s->pict_type;
  503. s->current_picture.key_frame= s->pict_type == I_TYPE;
  504. /* skip b frames if we dont have reference frames */
  505. if(s->last_picture_ptr==NULL && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  506. /* skip b frames if we are in a hurry */
  507. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  508. /* skip everything if we are in a hurry>=5 */
  509. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  510. if(s->next_p_frame_damaged){
  511. if(s->pict_type==B_TYPE)
  512. return get_consumed_bytes(s, buf_size);
  513. else
  514. s->next_p_frame_damaged=0;
  515. }
  516. if(MPV_frame_start(s, avctx) < 0)
  517. return -1;
  518. #ifdef DEBUG
  519. printf("qscale=%d\n", s->qscale);
  520. #endif
  521. ff_er_frame_start(s);
  522. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  523. //which isnt available before MPV_frame_start()
  524. if (s->msmpeg4_version==5){
  525. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  526. return -1;
  527. }
  528. /* decode each macroblock */
  529. s->mb_x=0;
  530. s->mb_y=0;
  531. decode_slice(s);
  532. while(s->mb_y<s->mb_height){
  533. if(s->msmpeg4_version){
  534. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  535. break;
  536. }else{
  537. if(ff_h263_resync(s)<0)
  538. break;
  539. }
  540. if(s->msmpeg4_version<4 && s->h263_pred)
  541. ff_mpeg4_clean_buffers(s);
  542. decode_slice(s);
  543. }
  544. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  545. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  546. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  547. }
  548. /* divx 5.01+ bistream reorder stuff */
  549. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  550. int current_pos= get_bits_count(&s->gb)>>3;
  551. if( buf_size - current_pos > 5
  552. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  553. int i;
  554. int startcode_found=0;
  555. for(i=current_pos; i<buf_size-3; i++){
  556. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  557. startcode_found=1;
  558. break;
  559. }
  560. }
  561. if(startcode_found){
  562. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  563. s->bitstream_buffer_size= buf_size - current_pos;
  564. }
  565. }
  566. }
  567. ff_er_frame_end(s);
  568. MPV_frame_end(s);
  569. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  570. assert(s->current_picture.pict_type == s->pict_type);
  571. if(s->pict_type==B_TYPE || s->low_delay){
  572. *pict= *(AVFrame*)&s->current_picture;
  573. ff_print_debug_info(s, s->current_picture_ptr);
  574. } else {
  575. *pict= *(AVFrame*)&s->last_picture;
  576. ff_print_debug_info(s, s->last_picture_ptr);
  577. }
  578. /* Return the Picture timestamp as the frame number */
  579. /* we substract 1 because it is added on utils.c */
  580. avctx->frame_number = s->picture_number - 1;
  581. /* dont output the last pic after seeking */
  582. if(s->last_picture_ptr || s->low_delay)
  583. *data_size = sizeof(AVFrame);
  584. #ifdef PRINT_FRAME_TIME
  585. printf("%Ld\n", rdtsc()-time);
  586. #endif
  587. return get_consumed_bytes(s, buf_size);
  588. }
  589. static const AVOption mpeg4_decoptions[] =
  590. {
  591. AVOPTION_SUB(avoptions_workaround_bug),
  592. AVOPTION_END()
  593. };
  594. AVCodec mpeg4_decoder = {
  595. "mpeg4",
  596. CODEC_TYPE_VIDEO,
  597. CODEC_ID_MPEG4,
  598. sizeof(MpegEncContext),
  599. ff_h263_decode_init,
  600. NULL,
  601. ff_h263_decode_end,
  602. ff_h263_decode_frame,
  603. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  604. .options = mpeg4_decoptions,
  605. .flush= ff_mpeg_flush,
  606. };
  607. AVCodec h263_decoder = {
  608. "h263",
  609. CODEC_TYPE_VIDEO,
  610. CODEC_ID_H263,
  611. sizeof(MpegEncContext),
  612. ff_h263_decode_init,
  613. NULL,
  614. ff_h263_decode_end,
  615. ff_h263_decode_frame,
  616. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  617. .flush= ff_mpeg_flush,
  618. };
  619. AVCodec msmpeg4v1_decoder = {
  620. "msmpeg4v1",
  621. CODEC_TYPE_VIDEO,
  622. CODEC_ID_MSMPEG4V1,
  623. sizeof(MpegEncContext),
  624. ff_h263_decode_init,
  625. NULL,
  626. ff_h263_decode_end,
  627. ff_h263_decode_frame,
  628. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  629. mpeg4_decoptions,
  630. };
  631. AVCodec msmpeg4v2_decoder = {
  632. "msmpeg4v2",
  633. CODEC_TYPE_VIDEO,
  634. CODEC_ID_MSMPEG4V2,
  635. sizeof(MpegEncContext),
  636. ff_h263_decode_init,
  637. NULL,
  638. ff_h263_decode_end,
  639. ff_h263_decode_frame,
  640. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  641. mpeg4_decoptions,
  642. };
  643. AVCodec msmpeg4v3_decoder = {
  644. "msmpeg4",
  645. CODEC_TYPE_VIDEO,
  646. CODEC_ID_MSMPEG4V3,
  647. sizeof(MpegEncContext),
  648. ff_h263_decode_init,
  649. NULL,
  650. ff_h263_decode_end,
  651. ff_h263_decode_frame,
  652. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  653. .options = mpeg4_decoptions,
  654. };
  655. AVCodec wmv1_decoder = {
  656. "wmv1",
  657. CODEC_TYPE_VIDEO,
  658. CODEC_ID_WMV1,
  659. sizeof(MpegEncContext),
  660. ff_h263_decode_init,
  661. NULL,
  662. ff_h263_decode_end,
  663. ff_h263_decode_frame,
  664. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  665. mpeg4_decoptions,
  666. };
  667. AVCodec h263i_decoder = {
  668. "h263i",
  669. CODEC_TYPE_VIDEO,
  670. CODEC_ID_H263I,
  671. sizeof(MpegEncContext),
  672. ff_h263_decode_init,
  673. NULL,
  674. ff_h263_decode_end,
  675. ff_h263_decode_frame,
  676. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  677. mpeg4_decoptions,
  678. };
  679. AVCodec flv_decoder = {
  680. "flv",
  681. CODEC_TYPE_VIDEO,
  682. CODEC_ID_FLV1,
  683. sizeof(MpegEncContext),
  684. ff_h263_decode_init,
  685. NULL,
  686. ff_h263_decode_end,
  687. ff_h263_decode_frame,
  688. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
  689. };