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.

855 lines
26KB

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