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.

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