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.

834 lines
25KB

  1. /*
  2. * H.263 decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file h263dec.c
  21. * H.263 decoder.
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. //#define DEBUG
  27. //#define PRINT_FRAME_TIME
  28. int ff_h263_decode_init(AVCodecContext *avctx)
  29. {
  30. MpegEncContext *s = avctx->priv_data;
  31. s->avctx = avctx;
  32. s->out_format = FMT_H263;
  33. s->width = avctx->width;
  34. s->height = avctx->height;
  35. s->workaround_bugs= avctx->workaround_bugs;
  36. // set defaults
  37. s->quant_precision=5;
  38. s->progressive_sequence=1;
  39. s->decode_mb= ff_h263_decode_mb;
  40. s->low_delay= 1;
  41. avctx->pix_fmt= PIX_FMT_YUV420P;
  42. s->unrestricted_mv= 1;
  43. /* select sub codec */
  44. switch(avctx->codec->id) {
  45. case CODEC_ID_H263:
  46. s->gob_number = 0;
  47. break;
  48. case CODEC_ID_MPEG4:
  49. s->time_increment_bits = 4; /* default value for broken headers */
  50. s->h263_pred = 1;
  51. s->low_delay = 0; //default, might be overriden in the vol header during header parsing
  52. break;
  53. case CODEC_ID_MSMPEG4V1:
  54. s->h263_msmpeg4 = 1;
  55. s->h263_pred = 1;
  56. s->msmpeg4_version=1;
  57. break;
  58. case CODEC_ID_MSMPEG4V2:
  59. s->h263_msmpeg4 = 1;
  60. s->h263_pred = 1;
  61. s->msmpeg4_version=2;
  62. break;
  63. case CODEC_ID_MSMPEG4V3:
  64. s->h263_msmpeg4 = 1;
  65. s->h263_pred = 1;
  66. s->msmpeg4_version=3;
  67. break;
  68. case CODEC_ID_WMV1:
  69. s->h263_msmpeg4 = 1;
  70. s->h263_pred = 1;
  71. s->msmpeg4_version=4;
  72. break;
  73. case CODEC_ID_WMV2:
  74. s->h263_msmpeg4 = 1;
  75. s->h263_pred = 1;
  76. s->msmpeg4_version=5;
  77. break;
  78. case CODEC_ID_H263I:
  79. s->h263_intel = 1;
  80. break;
  81. case CODEC_ID_FLV1:
  82. s->h263_flv = 1;
  83. break;
  84. default:
  85. return -1;
  86. }
  87. s->codec_id= avctx->codec->id;
  88. /* for h263, we allocate the images after having read the header */
  89. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  90. if (MPV_common_init(s) < 0)
  91. return -1;
  92. if (s->h263_msmpeg4)
  93. ff_msmpeg4_decode_init(s);
  94. else
  95. h263_decode_init_vlc(s);
  96. return 0;
  97. }
  98. int ff_h263_decode_end(AVCodecContext *avctx)
  99. {
  100. MpegEncContext *s = avctx->priv_data;
  101. MPV_common_end(s);
  102. return 0;
  103. }
  104. /**
  105. * retunrs the number of bytes consumed for building the current frame
  106. */
  107. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  108. int pos= (get_bits_count(&s->gb)+7)>>3;
  109. if(s->divx_packed){
  110. //we would have to scan through the whole buf to handle the weird reordering ...
  111. return buf_size;
  112. }else if(s->flags&CODEC_FLAG_TRUNCATED){
  113. pos -= s->parse_context.last_index;
  114. if(pos<0) pos=0; // padding is not really read so this might be -1
  115. return pos;
  116. }else{
  117. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  118. if(pos+10>buf_size) pos=buf_size; // oops ;)
  119. return pos;
  120. }
  121. }
  122. static int decode_slice(MpegEncContext *s){
  123. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  124. s->last_resync_gb= s->gb;
  125. s->first_slice_line= 1;
  126. s->resync_mb_x= s->mb_x;
  127. s->resync_mb_y= s->mb_y;
  128. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  129. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  130. if(s->partitioned_frame){
  131. const int qscale= s->qscale;
  132. if(s->codec_id==CODEC_ID_MPEG4){
  133. if(ff_mpeg4_decode_partitions(s) < 0)
  134. return -1;
  135. }
  136. /* restore variables which were modified */
  137. s->first_slice_line=1;
  138. s->mb_x= s->resync_mb_x;
  139. s->mb_y= s->resync_mb_y;
  140. s->qscale= qscale;
  141. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  142. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  143. }
  144. for(; s->mb_y < s->mb_height; s->mb_y++) {
  145. /* per-row end of slice checks */
  146. if(s->msmpeg4_version){
  147. if(s->resync_mb_y + s->slice_height == s->mb_y){
  148. 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);
  149. return 0;
  150. }
  151. }
  152. if(s->msmpeg4_version==1){
  153. s->last_dc[0]=
  154. s->last_dc[1]=
  155. s->last_dc[2]= 128;
  156. }
  157. ff_init_block_index(s);
  158. for(; s->mb_x < s->mb_width; s->mb_x++) {
  159. int ret;
  160. ff_update_block_index(s);
  161. if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
  162. s->first_slice_line=0;
  163. }
  164. /* DCT & quantize */
  165. s->dsp.clear_blocks(s->block[0]);
  166. s->mv_dir = MV_DIR_FORWARD;
  167. s->mv_type = MV_TYPE_16X16;
  168. // s->mb_skiped = 0;
  169. //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
  170. ret= s->decode_mb(s, s->block);
  171. if (s->pict_type!=B_TYPE)
  172. ff_h263_update_motion_val(s);
  173. if(ret<0){
  174. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  175. if(ret==SLICE_END){
  176. MPV_decode_mb(s, s->block);
  177. //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));
  178. 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);
  179. s->padding_bug_score--;
  180. if(++s->mb_x >= s->mb_width){
  181. s->mb_x=0;
  182. ff_draw_horiz_band(s, s->mb_y*16, 16);
  183. s->mb_y++;
  184. }
  185. return 0;
  186. }else if(ret==SLICE_NOEND){
  187. fprintf(stderr,"Slice mismatch at MB: %d\n", xy);
  188. 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);
  189. return -1;
  190. }
  191. fprintf(stderr,"Error at MB: %d\n", xy);
  192. 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);
  193. return -1;
  194. }
  195. MPV_decode_mb(s, s->block);
  196. }
  197. ff_draw_horiz_band(s, s->mb_y*16, 16);
  198. s->mb_x= 0;
  199. }
  200. assert(s->mb_x==0 && s->mb_y==s->mb_height);
  201. /* try to detect the padding bug */
  202. if( s->codec_id==CODEC_ID_MPEG4
  203. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  204. && s->gb.size_in_bits - get_bits_count(&s->gb) >=0
  205. && s->gb.size_in_bits - get_bits_count(&s->gb) < 48
  206. // && !s->resync_marker
  207. && !s->data_partitioning){
  208. const int bits_count= get_bits_count(&s->gb);
  209. const int bits_left = s->gb.size_in_bits - bits_count;
  210. if(bits_left==0){
  211. s->padding_bug_score+=16;
  212. }else if(bits_left>8){
  213. s->padding_bug_score++;
  214. } else if(bits_left != 1){
  215. int v= show_bits(&s->gb, 8);
  216. v|= 0x7F >> (7-(bits_count&7));
  217. if(v==0x7F)
  218. s->padding_bug_score--;
  219. else
  220. s->padding_bug_score++;
  221. }
  222. }
  223. // handle formats which dont have unique end markers
  224. if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
  225. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  226. int max_extra=7;
  227. /* no markers in M$ crap */
  228. if(s->msmpeg4_version && s->pict_type==I_TYPE)
  229. max_extra+= 17;
  230. /* buggy padding but the frame should still end approximately at the bitstream end */
  231. if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3)
  232. max_extra+= 48;
  233. else if((s->workaround_bugs&FF_BUG_NO_PADDING))
  234. max_extra+= 256*256*256*64;
  235. if(left>max_extra){
  236. fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
  237. }
  238. else if(left<0){
  239. fprintf(stderr, "overreading %d bits\n", -left);
  240. }else
  241. 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);
  242. return 0;
  243. }
  244. fprintf(stderr, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
  245. s->gb.size_in_bits - get_bits_count(&s->gb),
  246. show_bits(&s->gb, 24), s->padding_bug_score);
  247. 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);
  248. return -1;
  249. }
  250. /**
  251. * finds the end of the current frame in the bitstream.
  252. * @return the position of the first byte of the next frame, or -1
  253. */
  254. static int mpeg4_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  255. ParseContext *pc= &s->parse_context;
  256. int vop_found, i;
  257. uint32_t state;
  258. vop_found= pc->frame_start_found;
  259. state= pc->state;
  260. i=0;
  261. if(!vop_found){
  262. for(i=0; i<buf_size; i++){
  263. state= (state<<8) | buf[i];
  264. if(state == 0x1B6){
  265. i++;
  266. vop_found=1;
  267. break;
  268. }
  269. }
  270. }
  271. if(vop_found){
  272. for(; i<buf_size; i++){
  273. state= (state<<8) | buf[i];
  274. if((state&0xFFFFFF00) == 0x100){
  275. pc->frame_start_found=0;
  276. pc->state=-1;
  277. return i-3;
  278. }
  279. }
  280. }
  281. pc->frame_start_found= vop_found;
  282. pc->state= state;
  283. return END_NOT_FOUND;
  284. }
  285. static int h263_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  286. ParseContext *pc= &s->parse_context;
  287. int vop_found, i;
  288. uint32_t state;
  289. vop_found= pc->frame_start_found;
  290. state= pc->state;
  291. i=0;
  292. if(!vop_found){
  293. for(i=0; i<buf_size; i++){
  294. state= (state<<8) | buf[i];
  295. if(state>>(32-22) == 0x20){
  296. i++;
  297. vop_found=1;
  298. break;
  299. }
  300. }
  301. }
  302. if(vop_found){
  303. for(; i<buf_size; i++){
  304. state= (state<<8) | buf[i];
  305. if(state>>(32-22) == 0x20){
  306. pc->frame_start_found=0;
  307. pc->state=-1;
  308. return i-3;
  309. }
  310. }
  311. }
  312. pc->frame_start_found= vop_found;
  313. pc->state= state;
  314. return END_NOT_FOUND;
  315. }
  316. int ff_h263_decode_frame(AVCodecContext *avctx,
  317. void *data, int *data_size,
  318. uint8_t *buf, int buf_size)
  319. {
  320. MpegEncContext *s = avctx->priv_data;
  321. int ret;
  322. AVFrame *pict = data;
  323. #ifdef PRINT_FRAME_TIME
  324. uint64_t time= rdtsc();
  325. #endif
  326. #ifdef DEBUG
  327. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  328. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  329. #endif
  330. s->flags= avctx->flags;
  331. *data_size = 0;
  332. /* no supplementary picture */
  333. if (buf_size == 0) {
  334. /* special case for last picture */
  335. if (s->low_delay==0 && s->next_picture_ptr) {
  336. *pict= *(AVFrame*)s->next_picture_ptr;
  337. s->next_picture_ptr= NULL;
  338. *data_size = sizeof(AVFrame);
  339. }
  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. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  366. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  367. int i= ff_find_unused_picture(s, 0);
  368. s->current_picture_ptr= &s->picture[i];
  369. }
  370. /* let's go :-) */
  371. if (s->msmpeg4_version==5) {
  372. ret= ff_wmv2_decode_picture_header(s);
  373. } else if (s->msmpeg4_version) {
  374. ret = msmpeg4_decode_picture_header(s);
  375. } else if (s->h263_pred) {
  376. if(s->avctx->extradata_size && s->picture_number==0){
  377. GetBitContext gb;
  378. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  379. ret = ff_mpeg4_decode_picture_header(s, &gb);
  380. }
  381. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  382. if(s->flags& CODEC_FLAG_LOW_DELAY)
  383. s->low_delay=1;
  384. } else if (s->h263_intel) {
  385. ret = intel_h263_decode_picture_header(s);
  386. } else if (s->h263_flv) {
  387. ret = flv_h263_decode_picture_header(s);
  388. } else {
  389. ret = h263_decode_picture_header(s);
  390. }
  391. if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
  392. /* skip if the header was thrashed */
  393. if (ret < 0){
  394. fprintf(stderr, "header damaged\n");
  395. return -1;
  396. }
  397. avctx->has_b_frames= !s->low_delay;
  398. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  399. if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") ||
  400. s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  401. s->xvid_build= -1;
  402. #if 0
  403. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  404. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  405. s->xvid_build= -1;
  406. #endif
  407. }
  408. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  409. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  410. s->divx_version= 400; //divx 4
  411. }
  412. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  413. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  414. if(s->padding_bug_score > -2 && !s->data_partitioning && (s->divx_version || !s->resync_marker))
  415. s->workaround_bugs |= FF_BUG_NO_PADDING;
  416. if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  417. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  418. if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
  419. s->workaround_bugs|= FF_BUG_UMP4;
  420. }
  421. if(s->divx_version>=500){
  422. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  423. }
  424. if(s->divx_version>502){
  425. s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
  426. }
  427. if(s->xvid_build && s->xvid_build<=3)
  428. s->padding_bug_score= 256*256*256*64;
  429. if(s->xvid_build && s->xvid_build<=1)
  430. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  431. if(s->xvid_build && s->xvid_build<=12)
  432. s->workaround_bugs|= FF_BUG_EDGE;
  433. #define SET_QPEL_FUNC(postfix1, postfix2) \
  434. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  435. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  436. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  437. if(s->lavc_build && s->lavc_build<4653)
  438. s->workaround_bugs|= FF_BUG_STD_QPEL;
  439. if(s->lavc_build && s->lavc_build<4655)
  440. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  441. if(s->lavc_build && s->lavc_build<4670){
  442. s->workaround_bugs|= FF_BUG_EDGE;
  443. }
  444. if(s->divx_version)
  445. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  446. //printf("padding_bug_score: %d\n", s->padding_bug_score);
  447. if(s->divx_version==501 && s->divx_build==20020416)
  448. s->padding_bug_score= 256*256*256*64;
  449. if(s->divx_version && s->divx_version<500){
  450. s->workaround_bugs|= FF_BUG_EDGE;
  451. }
  452. #if 0
  453. if(s->divx_version==500)
  454. s->padding_bug_score= 256*256*256*64;
  455. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  456. * lets hope this at least works
  457. */
  458. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  459. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  460. s->workaround_bugs|= FF_BUG_NO_PADDING;
  461. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  462. s->workaround_bugs|= FF_BUG_NO_PADDING;
  463. #endif
  464. }
  465. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  466. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  467. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  468. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  469. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  470. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  471. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  472. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  473. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  474. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  475. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  476. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  477. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  478. }
  479. if(avctx->debug & FF_DEBUG_BUGS)
  480. printf("bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  481. s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
  482. s->divx_packed ? "p" : "");
  483. #if 0 // dump bits per frame / qp / complexity
  484. {
  485. static FILE *f=NULL;
  486. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  487. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  488. }
  489. #endif
  490. /* After H263 & mpeg4 header decode we have the height, width,*/
  491. /* and other parameters. So then we could init the picture */
  492. /* FIXME: By the way H263 decoder is evolving it should have */
  493. /* an H263EncContext */
  494. if ( s->width != avctx->width || s->height != avctx->height) {
  495. /* H.263 could change picture size any time */
  496. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  497. s->parse_context.buffer=0;
  498. MPV_common_end(s);
  499. s->parse_context= pc;
  500. }
  501. if (!s->context_initialized) {
  502. avctx->width = s->width;
  503. avctx->height = s->height;
  504. goto retry;
  505. }
  506. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  507. s->gob_index = ff_h263_get_gob_height(s);
  508. // for hurry_up==5
  509. s->current_picture.pict_type= s->pict_type;
  510. s->current_picture.key_frame= s->pict_type == I_TYPE;
  511. /* skip b frames if we dont have reference frames */
  512. if(s->last_picture_ptr==NULL && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  513. /* skip b frames if we are in a hurry */
  514. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  515. /* skip everything if we are in a hurry>=5 */
  516. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  517. if(s->next_p_frame_damaged){
  518. if(s->pict_type==B_TYPE)
  519. return get_consumed_bytes(s, buf_size);
  520. else
  521. s->next_p_frame_damaged=0;
  522. }
  523. if(MPV_frame_start(s, avctx) < 0)
  524. return -1;
  525. #ifdef DEBUG
  526. printf("qscale=%d\n", s->qscale);
  527. #endif
  528. ff_er_frame_start(s);
  529. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  530. //which isnt available before MPV_frame_start()
  531. if (s->msmpeg4_version==5){
  532. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  533. return -1;
  534. }
  535. /* decode each macroblock */
  536. s->mb_x=0;
  537. s->mb_y=0;
  538. decode_slice(s);
  539. while(s->mb_y<s->mb_height){
  540. if(s->msmpeg4_version){
  541. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  542. break;
  543. }else{
  544. if(ff_h263_resync(s)<0)
  545. break;
  546. }
  547. if(s->msmpeg4_version<4 && s->h263_pred)
  548. ff_mpeg4_clean_buffers(s);
  549. decode_slice(s);
  550. }
  551. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  552. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  553. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  554. }
  555. /* divx 5.01+ bistream reorder stuff */
  556. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  557. int current_pos= get_bits_count(&s->gb)>>3;
  558. if( buf_size - current_pos > 5
  559. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  560. int i;
  561. int startcode_found=0;
  562. for(i=current_pos; i<buf_size-3; i++){
  563. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  564. startcode_found=1;
  565. break;
  566. }
  567. }
  568. if(startcode_found){
  569. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  570. s->bitstream_buffer_size= buf_size - current_pos;
  571. }
  572. }
  573. }
  574. ff_er_frame_end(s);
  575. MPV_frame_end(s);
  576. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  577. assert(s->current_picture.pict_type == s->pict_type);
  578. if(s->pict_type==B_TYPE || s->low_delay){
  579. *pict= *(AVFrame*)&s->current_picture;
  580. ff_print_debug_info(s, s->current_picture_ptr);
  581. } else {
  582. *pict= *(AVFrame*)&s->last_picture;
  583. ff_print_debug_info(s, s->last_picture_ptr);
  584. }
  585. /* Return the Picture timestamp as the frame number */
  586. /* we substract 1 because it is added on utils.c */
  587. avctx->frame_number = s->picture_number - 1;
  588. /* dont output the last pic after seeking */
  589. if(s->last_picture_ptr || s->low_delay)
  590. *data_size = sizeof(AVFrame);
  591. #ifdef PRINT_FRAME_TIME
  592. printf("%Ld\n", rdtsc()-time);
  593. #endif
  594. return get_consumed_bytes(s, buf_size);
  595. }
  596. static const AVOption mpeg4_decoptions[] =
  597. {
  598. AVOPTION_SUB(avoptions_workaround_bug),
  599. AVOPTION_END()
  600. };
  601. AVCodec mpeg4_decoder = {
  602. "mpeg4",
  603. CODEC_TYPE_VIDEO,
  604. CODEC_ID_MPEG4,
  605. sizeof(MpegEncContext),
  606. ff_h263_decode_init,
  607. NULL,
  608. ff_h263_decode_end,
  609. ff_h263_decode_frame,
  610. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  611. .options = mpeg4_decoptions,
  612. .flush= ff_mpeg_flush,
  613. };
  614. AVCodec h263_decoder = {
  615. "h263",
  616. CODEC_TYPE_VIDEO,
  617. CODEC_ID_H263,
  618. sizeof(MpegEncContext),
  619. ff_h263_decode_init,
  620. NULL,
  621. ff_h263_decode_end,
  622. ff_h263_decode_frame,
  623. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  624. .flush= ff_mpeg_flush,
  625. };
  626. AVCodec msmpeg4v1_decoder = {
  627. "msmpeg4v1",
  628. CODEC_TYPE_VIDEO,
  629. CODEC_ID_MSMPEG4V1,
  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 msmpeg4v2_decoder = {
  639. "msmpeg4v2",
  640. CODEC_TYPE_VIDEO,
  641. CODEC_ID_MSMPEG4V2,
  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. mpeg4_decoptions,
  649. };
  650. AVCodec msmpeg4v3_decoder = {
  651. "msmpeg4",
  652. CODEC_TYPE_VIDEO,
  653. CODEC_ID_MSMPEG4V3,
  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. .options = mpeg4_decoptions,
  661. };
  662. AVCodec wmv1_decoder = {
  663. "wmv1",
  664. CODEC_TYPE_VIDEO,
  665. CODEC_ID_WMV1,
  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 h263i_decoder = {
  675. "h263i",
  676. CODEC_TYPE_VIDEO,
  677. CODEC_ID_H263I,
  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. mpeg4_decoptions,
  685. };
  686. AVCodec flv_decoder = {
  687. "flv",
  688. CODEC_TYPE_VIDEO,
  689. CODEC_ID_FLV1,
  690. sizeof(MpegEncContext),
  691. ff_h263_decode_init,
  692. NULL,
  693. ff_h263_decode_end,
  694. ff_h263_decode_frame,
  695. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
  696. };