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.

853 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. for(; i<buf_size; i++){
  276. state= (state<<8) | buf[i];
  277. if((state&0xFFFFFF00) == 0x100){
  278. pc->frame_start_found=0;
  279. pc->state=-1;
  280. return i-3;
  281. }
  282. }
  283. pc->frame_start_found= vop_found;
  284. pc->state= state;
  285. return -1;
  286. }
  287. /**
  288. * draws an line from (ex, ey) -> (sx, sy).
  289. * @param w width of the image
  290. * @param h height of the image
  291. * @param stride stride/linesize of the image
  292. * @param color color of the arrow
  293. */
  294. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  295. int t, x, y, f;
  296. ex= clip(ex, 0, w-1);
  297. ey= clip(ey, 0, h-1);
  298. buf[sy*stride + sx]+= color;
  299. if(ABS(ex - sx) > ABS(ey - sy)){
  300. if(sx > ex){
  301. t=sx; sx=ex; ex=t;
  302. t=sy; sy=ey; ey=t;
  303. }
  304. buf+= sx + sy*stride;
  305. ex-= sx;
  306. f= ((ey-sy)<<16)/ex;
  307. for(x= 0; x <= ex; x++){
  308. y= ((x*f) + (1<<15))>>16;
  309. buf[y*stride + x]+= color;
  310. }
  311. }else{
  312. if(sy > ey){
  313. t=sx; sx=ex; ex=t;
  314. t=sy; sy=ey; ey=t;
  315. }
  316. buf+= sx + sy*stride;
  317. ey-= sy;
  318. if(ey) f= ((ex-sx)<<16)/ey;
  319. else f= 0;
  320. for(y= 0; y <= ey; y++){
  321. x= ((y*f) + (1<<15))>>16;
  322. buf[y*stride + x]+= color;
  323. }
  324. }
  325. }
  326. /**
  327. * draws an arrow from (ex, ey) -> (sx, sy).
  328. * @param w width of the image
  329. * @param h height of the image
  330. * @param stride stride/linesize of the image
  331. * @param color color of the arrow
  332. */
  333. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  334. int dx= ex - sx;
  335. int dy= ey - sy;
  336. if(dx*dx + dy*dy > 3*3){
  337. int rx= dx + dy;
  338. int ry= -dx + dy;
  339. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  340. //FIXME subpixel accuracy
  341. rx= ROUNDED_DIV(rx*3<<4, length);
  342. ry= ROUNDED_DIV(ry*3<<4, length);
  343. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  344. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  345. }
  346. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  347. }
  348. int ff_h263_decode_frame(AVCodecContext *avctx,
  349. void *data, int *data_size,
  350. uint8_t *buf, int buf_size)
  351. {
  352. MpegEncContext *s = avctx->priv_data;
  353. int ret,i;
  354. AVFrame *pict = data;
  355. float new_aspect;
  356. #ifdef PRINT_FRAME_TIME
  357. uint64_t time= rdtsc();
  358. #endif
  359. #ifdef DEBUG
  360. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  361. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  362. #endif
  363. s->flags= avctx->flags;
  364. *data_size = 0;
  365. /* no supplementary picture */
  366. if (buf_size == 0) {
  367. return 0;
  368. }
  369. if(s->flags&CODEC_FLAG_TRUNCATED){
  370. int next;
  371. if(s->codec_id==CODEC_ID_MPEG4){
  372. next= mpeg4_find_frame_end(s, buf, buf_size);
  373. }else{
  374. fprintf(stderr, "this codec doesnt support truncated bitstreams\n");
  375. return -1;
  376. }
  377. if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
  378. return buf_size;
  379. }
  380. retry:
  381. if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
  382. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
  383. }else
  384. init_get_bits(&s->gb, buf, buf_size*8);
  385. s->bitstream_buffer_size=0;
  386. if (!s->context_initialized) {
  387. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  388. return -1;
  389. }
  390. /* let's go :-) */
  391. if (s->msmpeg4_version==5) {
  392. ret= ff_wmv2_decode_picture_header(s);
  393. } else if (s->msmpeg4_version) {
  394. ret = msmpeg4_decode_picture_header(s);
  395. } else if (s->h263_pred) {
  396. if(s->avctx->extradata_size && s->picture_number==0){
  397. GetBitContext gb;
  398. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  399. ret = ff_mpeg4_decode_picture_header(s, &gb);
  400. }
  401. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  402. if(s->flags& CODEC_FLAG_LOW_DELAY)
  403. s->low_delay=1;
  404. } else if (s->h263_intel) {
  405. ret = intel_h263_decode_picture_header(s);
  406. } else {
  407. ret = h263_decode_picture_header(s);
  408. }
  409. avctx->has_b_frames= !s->low_delay;
  410. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  411. if(s->padding_bug_score > -2 && !s->data_partitioning && !s->resync_marker)
  412. s->workaround_bugs |= FF_BUG_NO_PADDING;
  413. else
  414. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  415. if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  416. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  417. #if 0
  418. if(s->avctx->codec_tag == ff_get_fourcc("MP4S"))
  419. s->workaround_bugs|= FF_BUG_AC_VLC;
  420. if(s->avctx->codec_tag == ff_get_fourcc("M4S2"))
  421. s->workaround_bugs|= FF_BUG_AC_VLC;
  422. #endif
  423. if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
  424. s->workaround_bugs|= FF_BUG_UMP4;
  425. s->workaround_bugs|= FF_BUG_AC_VLC;
  426. }
  427. if(s->divx_version){
  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->avctx->codec_tag == ff_get_fourcc("XVID") && s->xvid_build==0)
  434. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  435. if(s->avctx->codec_tag == ff_get_fourcc("XVID") && s->xvid_build==0)
  436. s->padding_bug_score= 256*256*256*64;
  437. if(s->xvid_build && s->xvid_build<=3)
  438. s->padding_bug_score= 256*256*256*64;
  439. if(s->xvid_build && s->xvid_build<=1)
  440. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  441. #define SET_QPEL_FUNC(postfix1, postfix2) \
  442. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  443. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  444. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  445. if(s->lavc_build && s->lavc_build<4653)
  446. s->workaround_bugs|= FF_BUG_STD_QPEL;
  447. if(s->lavc_build && s->lavc_build<4655)
  448. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  449. if(s->divx_version)
  450. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  451. //printf("padding_bug_score: %d\n", s->padding_bug_score);
  452. if(s->divx_version==501 && s->divx_build==20020416)
  453. s->padding_bug_score= 256*256*256*64;
  454. if(s->divx_version>=500){
  455. s->workaround_bugs|= FF_BUG_EDGE;
  456. }
  457. #if 0
  458. if(s->divx_version==500)
  459. s->padding_bug_score= 256*256*256*64;
  460. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  461. * lets hope this at least works
  462. */
  463. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  464. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  465. s->workaround_bugs|= FF_BUG_NO_PADDING;
  466. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  467. s->workaround_bugs|= FF_BUG_NO_PADDING;
  468. #endif
  469. }
  470. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  471. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  472. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  473. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  474. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  475. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  476. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  477. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  478. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  479. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  480. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  481. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  482. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  483. }
  484. #if 0 // dump bits per frame / qp / complexity
  485. {
  486. static FILE *f=NULL;
  487. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  488. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  489. }
  490. #endif
  491. /* After H263 & mpeg4 header decode we have the height, width,*/
  492. /* and other parameters. So then we could init the picture */
  493. /* FIXME: By the way H263 decoder is evolving it should have */
  494. /* an H263EncContext */
  495. if(s->aspected_height)
  496. new_aspect= s->aspected_width*s->width / (float)(s->height*s->aspected_height);
  497. else
  498. new_aspect=0;
  499. if ( s->width != avctx->width || s->height != avctx->height
  500. || ABS(new_aspect - avctx->aspect_ratio) > 0.001) {
  501. /* H.263 could change picture size any time */
  502. MPV_common_end(s);
  503. }
  504. if (!s->context_initialized) {
  505. avctx->width = s->width;
  506. avctx->height = s->height;
  507. avctx->aspect_ratio= new_aspect;
  508. goto retry;
  509. }
  510. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  511. s->gob_index = ff_h263_get_gob_height(s);
  512. if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
  513. /* skip if the header was thrashed */
  514. if (ret < 0){
  515. fprintf(stderr, "header damaged\n");
  516. return -1;
  517. }
  518. // for hurry_up==5
  519. s->current_picture.pict_type= s->pict_type;
  520. s->current_picture.key_frame= s->pict_type == I_TYPE;
  521. /* skip b frames if we dont have reference frames */
  522. if(s->last_picture_ptr==NULL && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  523. /* skip b frames if we are in a hurry */
  524. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  525. /* skip everything if we are in a hurry>=5 */
  526. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  527. if(s->next_p_frame_damaged){
  528. if(s->pict_type==B_TYPE)
  529. return get_consumed_bytes(s, buf_size);
  530. else
  531. s->next_p_frame_damaged=0;
  532. }
  533. if(MPV_frame_start(s, avctx) < 0)
  534. return -1;
  535. #ifdef DEBUG
  536. printf("qscale=%d\n", s->qscale);
  537. #endif
  538. ff_er_frame_start(s);
  539. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  540. //which isnt available before MPV_frame_start()
  541. if (s->msmpeg4_version==5){
  542. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  543. return -1;
  544. }
  545. /* decode each macroblock */
  546. s->mb_x=0;
  547. s->mb_y=0;
  548. decode_slice(s);
  549. while(s->mb_y<s->mb_height){
  550. if(s->msmpeg4_version){
  551. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  552. break;
  553. }else{
  554. if(ff_h263_resync(s)<0)
  555. break;
  556. }
  557. if(s->msmpeg4_version<4 && s->h263_pred)
  558. ff_mpeg4_clean_buffers(s);
  559. decode_slice(s);
  560. }
  561. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  562. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  563. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  564. }
  565. /* divx 5.01+ bistream reorder stuff */
  566. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  567. int current_pos= get_bits_count(&s->gb)>>3;
  568. if( buf_size - current_pos > 5
  569. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  570. int i;
  571. int startcode_found=0;
  572. for(i=current_pos; i<buf_size-3; i++){
  573. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  574. startcode_found=1;
  575. break;
  576. }
  577. }
  578. if(startcode_found){
  579. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  580. s->bitstream_buffer_size= buf_size - current_pos;
  581. }
  582. }
  583. }
  584. ff_er_frame_end(s);
  585. MPV_frame_end(s);
  586. if((avctx->debug&FF_DEBUG_VIS_MV) && s->last_picture_ptr){
  587. const int shift= 1 + s->quarter_sample;
  588. int mb_y;
  589. uint8_t *ptr= s->last_picture.data[0];
  590. s->low_delay=0; //needed to see the vectors without trashing the buffers
  591. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  592. int mb_x;
  593. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  594. const int mb_index= mb_x + mb_y*s->mb_stride;
  595. if(IS_8X8(s->current_picture.mb_type[mb_index])){
  596. int i;
  597. for(i=0; i<4; i++){
  598. int sx= mb_x*16 + 4 + 8*(i&1);
  599. int sy= mb_y*16 + 4 + 8*(i>>1);
  600. int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
  601. int mx= (s->motion_val[xy][0]>>shift) + sx;
  602. int my= (s->motion_val[xy][1]>>shift) + sy;
  603. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  604. }
  605. }else{
  606. int sx= mb_x*16 + 8;
  607. int sy= mb_y*16 + 8;
  608. int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
  609. int mx= (s->motion_val[xy][0]>>shift) + sx;
  610. int my= (s->motion_val[xy][1]>>shift) + sy;
  611. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  612. }
  613. s->mbskip_table[mb_index]=0;
  614. }
  615. }
  616. }
  617. if(s->pict_type==B_TYPE || s->low_delay){
  618. *pict= *(AVFrame*)&s->current_picture;
  619. ff_print_debug_info(s, s->current_picture_ptr);
  620. } else {
  621. *pict= *(AVFrame*)&s->last_picture;
  622. ff_print_debug_info(s, s->last_picture_ptr);
  623. }
  624. /* Return the Picture timestamp as the frame number */
  625. /* we substract 1 because it is added on utils.c */
  626. avctx->frame_number = s->picture_number - 1;
  627. /* dont output the last pic after seeking */
  628. if(s->last_picture_ptr || s->low_delay)
  629. *data_size = sizeof(AVFrame);
  630. #ifdef PRINT_FRAME_TIME
  631. printf("%Ld\n", rdtsc()-time);
  632. #endif
  633. return get_consumed_bytes(s, buf_size);
  634. }
  635. static const AVOption mpeg4_decoptions[] =
  636. {
  637. AVOPTION_SUB(avoptions_workaround_bug),
  638. AVOPTION_END()
  639. };
  640. AVCodec mpeg4_decoder = {
  641. "mpeg4",
  642. CODEC_TYPE_VIDEO,
  643. CODEC_ID_MPEG4,
  644. sizeof(MpegEncContext),
  645. ff_h263_decode_init,
  646. NULL,
  647. ff_h263_decode_end,
  648. ff_h263_decode_frame,
  649. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  650. .options = mpeg4_decoptions,
  651. };
  652. AVCodec h263_decoder = {
  653. "h263",
  654. CODEC_TYPE_VIDEO,
  655. CODEC_ID_H263,
  656. sizeof(MpegEncContext),
  657. ff_h263_decode_init,
  658. NULL,
  659. ff_h263_decode_end,
  660. ff_h263_decode_frame,
  661. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  662. };
  663. AVCodec msmpeg4v1_decoder = {
  664. "msmpeg4v1",
  665. CODEC_TYPE_VIDEO,
  666. CODEC_ID_MSMPEG4V1,
  667. sizeof(MpegEncContext),
  668. ff_h263_decode_init,
  669. NULL,
  670. ff_h263_decode_end,
  671. ff_h263_decode_frame,
  672. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  673. mpeg4_decoptions,
  674. };
  675. AVCodec msmpeg4v2_decoder = {
  676. "msmpeg4v2",
  677. CODEC_TYPE_VIDEO,
  678. CODEC_ID_MSMPEG4V2,
  679. sizeof(MpegEncContext),
  680. ff_h263_decode_init,
  681. NULL,
  682. ff_h263_decode_end,
  683. ff_h263_decode_frame,
  684. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  685. mpeg4_decoptions,
  686. };
  687. AVCodec msmpeg4v3_decoder = {
  688. "msmpeg4",
  689. CODEC_TYPE_VIDEO,
  690. CODEC_ID_MSMPEG4V3,
  691. sizeof(MpegEncContext),
  692. ff_h263_decode_init,
  693. NULL,
  694. ff_h263_decode_end,
  695. ff_h263_decode_frame,
  696. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  697. .options = mpeg4_decoptions,
  698. };
  699. AVCodec wmv1_decoder = {
  700. "wmv1",
  701. CODEC_TYPE_VIDEO,
  702. CODEC_ID_WMV1,
  703. sizeof(MpegEncContext),
  704. ff_h263_decode_init,
  705. NULL,
  706. ff_h263_decode_end,
  707. ff_h263_decode_frame,
  708. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  709. mpeg4_decoptions,
  710. };
  711. AVCodec h263i_decoder = {
  712. "h263i",
  713. CODEC_TYPE_VIDEO,
  714. CODEC_ID_H263I,
  715. sizeof(MpegEncContext),
  716. ff_h263_decode_init,
  717. NULL,
  718. ff_h263_decode_end,
  719. ff_h263_decode_frame,
  720. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  721. mpeg4_decoptions,
  722. };