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.

891 lines
27KB

  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_version>=500){
  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. s->last_resync_gb= s->gb;
  131. s->first_slice_line= 1;
  132. s->resync_mb_x= s->mb_x;
  133. s->resync_mb_y= s->mb_y;
  134. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  135. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  136. if(s->partitioned_frame){
  137. const int qscale= s->qscale;
  138. if(s->codec_id==CODEC_ID_MPEG4){
  139. if(ff_mpeg4_decode_partitions(s) < 0)
  140. return -1;
  141. }
  142. /* restore variables which where modified */
  143. s->first_slice_line=1;
  144. s->mb_x= s->resync_mb_x;
  145. s->mb_y= s->resync_mb_y;
  146. s->qscale= qscale;
  147. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  148. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  149. }
  150. for(; s->mb_y < s->mb_height; s->mb_y++) {
  151. /* per-row end of slice checks */
  152. if(s->msmpeg4_version){
  153. if(s->resync_mb_y + s->slice_height == s->mb_y){
  154. const int xy= s->mb_x + s->mb_y*s->mb_width;
  155. s->error_status_table[xy-1]|= 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_width;
  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. s->error_status_table[xy]|= AC_END;
  184. if(!s->partitioned_frame)
  185. s->error_status_table[xy]|= MV_END|DC_END;
  186. s->padding_bug_score--;
  187. if(++s->mb_x >= s->mb_width){
  188. s->mb_x=0;
  189. ff_draw_horiz_band(s, s->mb_y*16, 16);
  190. s->mb_y++;
  191. }
  192. return 0;
  193. }else if(ret==SLICE_NOEND){
  194. fprintf(stderr,"Slice mismatch at MB: %d\n", xy);
  195. return -1;
  196. }
  197. fprintf(stderr,"Error at MB: %d\n", xy);
  198. s->error_status_table[xy]|= AC_ERROR;
  199. if(!s->partitioned_frame)
  200. s->error_status_table[xy]|= DC_ERROR|MV_ERROR;
  201. return -1;
  202. }
  203. }
  204. ff_draw_horiz_band(s, s->mb_y*16, 16);
  205. s->mb_x= 0;
  206. }
  207. assert(s->mb_x==0 && s->mb_y==s->mb_height);
  208. /* try to detect the padding bug */
  209. if( s->codec_id==CODEC_ID_MPEG4
  210. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  211. && s->gb.size_in_bits - get_bits_count(&s->gb) >=0
  212. && s->gb.size_in_bits - get_bits_count(&s->gb) < 48
  213. // && !s->resync_marker
  214. && !s->data_partitioning){
  215. const int bits_count= get_bits_count(&s->gb);
  216. const int bits_left = s->gb.size_in_bits - bits_count;
  217. if(bits_left==0){
  218. s->padding_bug_score+=16;
  219. }else if(bits_left>8){
  220. s->padding_bug_score++;
  221. } else if(bits_left != 1){
  222. int v= show_bits(&s->gb, 8);
  223. v|= 0x7F >> (7-(bits_count&7));
  224. if(v==0x7F)
  225. s->padding_bug_score--;
  226. else
  227. s->padding_bug_score++;
  228. }
  229. }
  230. // handle formats which dont have unique end markers
  231. if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
  232. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  233. int max_extra=7;
  234. /* no markers in M$ crap */
  235. if(s->msmpeg4_version && s->pict_type==I_TYPE)
  236. max_extra+= 17;
  237. /* buggy padding but the frame should still end approximately at the bitstream end */
  238. if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3)
  239. max_extra+= 48;
  240. else if((s->workaround_bugs&FF_BUG_NO_PADDING))
  241. max_extra+= 256*256*256*64;
  242. if(left>max_extra){
  243. fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
  244. }
  245. else if(left<0){
  246. fprintf(stderr, "overreading %d bits\n", -left);
  247. }else
  248. s->error_status_table[s->mb_num-1]|= AC_END|MV_END|DC_END;
  249. return 0;
  250. }
  251. fprintf(stderr, "slice end not reached but screenspace end (%d left %06X)\n",
  252. s->gb.size_in_bits - get_bits_count(&s->gb),
  253. show_bits(&s->gb, 24));
  254. return -1;
  255. }
  256. /**
  257. * finds the end of the current frame in the bitstream.
  258. * @return the position of the first byte of the next frame, or -1
  259. */
  260. static int mpeg4_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  261. ParseContext *pc= &s->parse_context;
  262. int vop_found, i;
  263. uint32_t state;
  264. vop_found= pc->frame_start_found;
  265. state= pc->state;
  266. i=0;
  267. if(!vop_found){
  268. for(i=0; i<buf_size; i++){
  269. state= (state<<8) | buf[i];
  270. if(state == 0x1B6){
  271. i++;
  272. vop_found=1;
  273. break;
  274. }
  275. }
  276. }
  277. for(; i<buf_size; i++){
  278. state= (state<<8) | buf[i];
  279. if((state&0xFFFFFF00) == 0x100){
  280. pc->frame_start_found=0;
  281. pc->state=-1;
  282. return i-3;
  283. }
  284. }
  285. pc->frame_start_found= vop_found;
  286. pc->state= state;
  287. return -1;
  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)
  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 0
  457. if(s->divx_version==500)
  458. s->padding_bug_score= 256*256*256*64;
  459. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  460. * lets hope this at least works
  461. */
  462. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  463. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  464. s->workaround_bugs|= FF_BUG_NO_PADDING;
  465. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  466. s->workaround_bugs|= FF_BUG_NO_PADDING;
  467. #endif
  468. }
  469. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  470. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  471. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  472. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  473. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  474. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  475. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  476. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  477. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  478. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  479. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  480. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  481. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  482. }
  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->aspected_height)
  495. new_aspect= s->aspected_width*s->width / (float)(s->height*s->aspected_height);
  496. else
  497. new_aspect=0;
  498. if ( s->width != avctx->width || s->height != avctx->height
  499. || ABS(new_aspect - avctx->aspect_ratio) > 0.001) {
  500. /* H.263 could change picture size any time */
  501. MPV_common_end(s);
  502. s->context_initialized=0;
  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.data[0]==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. if(s->error_resilience)
  539. memset(s->error_status_table, MV_ERROR|AC_ERROR|DC_ERROR|VP_START|AC_END|DC_END|MV_END, s->mb_num*sizeof(uint8_t));
  540. /* decode each macroblock */
  541. s->block_wrap[0]=
  542. s->block_wrap[1]=
  543. s->block_wrap[2]=
  544. s->block_wrap[3]= s->mb_width*2 + 2;
  545. s->block_wrap[4]=
  546. s->block_wrap[5]= s->mb_width + 2;
  547. s->mb_x=0;
  548. s->mb_y=0;
  549. decode_slice(s);
  550. s->error_status_table[0]|= VP_START;
  551. while(s->mb_y<s->mb_height && s->gb.size_in_bits - get_bits_count(&s->gb)>16){
  552. if(s->msmpeg4_version){
  553. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0)
  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. s->error_status_table[s->resync_mb_x + s->resync_mb_y*s->mb_width]|= VP_START;
  563. }
  564. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  565. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  566. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  567. }
  568. /* divx 5.01+ bistream reorder stuff */
  569. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_version>=500){
  570. int current_pos= get_bits_count(&s->gb)>>3;
  571. if( buf_size - current_pos > 5
  572. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  573. int i;
  574. int startcode_found=0;
  575. for(i=current_pos; i<buf_size-3; i++){
  576. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  577. startcode_found=1;
  578. break;
  579. }
  580. }
  581. if(startcode_found){
  582. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  583. s->bitstream_buffer_size= buf_size - current_pos;
  584. }
  585. }
  586. }
  587. if(s->error_resilience){
  588. int error=0, num_end_markers=0;
  589. for(i=0; i<s->mb_num; i++){
  590. int status= s->error_status_table[i];
  591. #if 0
  592. if(i%s->mb_width == 0) printf("\n");
  593. printf("%2X ", status);
  594. #endif
  595. if(status==0) continue;
  596. if(status&(DC_ERROR|AC_ERROR|MV_ERROR))
  597. error=1;
  598. if(status&VP_START){
  599. if(num_end_markers)
  600. error=1;
  601. num_end_markers=3;
  602. }
  603. if(status&AC_END)
  604. num_end_markers--;
  605. if(status&DC_END)
  606. num_end_markers--;
  607. if(status&MV_END)
  608. num_end_markers--;
  609. }
  610. if(num_end_markers || error){
  611. fprintf(stderr, "concealing errors\n");
  612. ff_error_resilience(s);
  613. }
  614. }
  615. MPV_frame_end(s);
  616. if((avctx->debug&FF_DEBUG_VIS_MV) && s->last_picture.data[0]){
  617. const int shift= 1 + s->quarter_sample;
  618. int mb_y;
  619. uint8_t *ptr= s->last_picture.data[0];
  620. s->low_delay=0; //needed to see the vectors without trashing the buffers
  621. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  622. int mb_x;
  623. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  624. const int mb_index= mb_x + mb_y*s->mb_width;
  625. if(s->co_located_type_table[mb_index] == MV_TYPE_8X8){
  626. int i;
  627. for(i=0; i<4; i++){
  628. int sx= mb_x*16 + 4 + 8*(i&1);
  629. int sy= mb_y*16 + 4 + 8*(i>>1);
  630. int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
  631. int mx= (s->motion_val[xy][0]>>shift) + sx;
  632. int my= (s->motion_val[xy][1]>>shift) + sy;
  633. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  634. }
  635. }else{
  636. int sx= mb_x*16 + 8;
  637. int sy= mb_y*16 + 8;
  638. int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
  639. int mx= (s->motion_val[xy][0]>>shift) + sx;
  640. int my= (s->motion_val[xy][1]>>shift) + sy;
  641. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  642. }
  643. s->mbskip_table[mb_index]=0;
  644. }
  645. }
  646. }
  647. if(s->pict_type==B_TYPE || s->low_delay){
  648. *pict= *(AVFrame*)&s->current_picture;
  649. } else {
  650. *pict= *(AVFrame*)&s->last_picture;
  651. }
  652. if(avctx->debug&FF_DEBUG_QP){
  653. int8_t *qtab= pict->qscale_table;
  654. int x,y;
  655. for(y=0; y<s->mb_height; y++){
  656. for(x=0; x<s->mb_width; x++){
  657. printf("%2d ", qtab[x + y*s->mb_width]);
  658. }
  659. printf("\n");
  660. }
  661. printf("\n");
  662. }
  663. /* Return the Picture timestamp as the frame number */
  664. /* we substract 1 because it is added on utils.c */
  665. avctx->frame_number = s->picture_number - 1;
  666. /* dont output the last pic after seeking */
  667. if(s->last_picture.data[0] || s->low_delay)
  668. *data_size = sizeof(AVFrame);
  669. #ifdef PRINT_FRAME_TIME
  670. printf("%Ld\n", rdtsc()-time);
  671. #endif
  672. return get_consumed_bytes(s, buf_size);
  673. }
  674. static const AVOption mpeg4_decoptions[] =
  675. {
  676. AVOPTION_SUB(avoptions_workaround_bug),
  677. AVOPTION_END()
  678. };
  679. AVCodec mpeg4_decoder = {
  680. "mpeg4",
  681. CODEC_TYPE_VIDEO,
  682. CODEC_ID_MPEG4,
  683. sizeof(MpegEncContext),
  684. ff_h263_decode_init,
  685. NULL,
  686. ff_h263_decode_end,
  687. ff_h263_decode_frame,
  688. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  689. .options = mpeg4_decoptions,
  690. };
  691. AVCodec h263_decoder = {
  692. "h263",
  693. CODEC_TYPE_VIDEO,
  694. CODEC_ID_H263,
  695. sizeof(MpegEncContext),
  696. ff_h263_decode_init,
  697. NULL,
  698. ff_h263_decode_end,
  699. ff_h263_decode_frame,
  700. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  701. };
  702. AVCodec msmpeg4v1_decoder = {
  703. "msmpeg4v1",
  704. CODEC_TYPE_VIDEO,
  705. CODEC_ID_MSMPEG4V1,
  706. sizeof(MpegEncContext),
  707. ff_h263_decode_init,
  708. NULL,
  709. ff_h263_decode_end,
  710. ff_h263_decode_frame,
  711. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  712. mpeg4_decoptions,
  713. };
  714. AVCodec msmpeg4v2_decoder = {
  715. "msmpeg4v2",
  716. CODEC_TYPE_VIDEO,
  717. CODEC_ID_MSMPEG4V2,
  718. sizeof(MpegEncContext),
  719. ff_h263_decode_init,
  720. NULL,
  721. ff_h263_decode_end,
  722. ff_h263_decode_frame,
  723. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  724. mpeg4_decoptions,
  725. };
  726. AVCodec msmpeg4v3_decoder = {
  727. "msmpeg4",
  728. CODEC_TYPE_VIDEO,
  729. CODEC_ID_MSMPEG4V3,
  730. sizeof(MpegEncContext),
  731. ff_h263_decode_init,
  732. NULL,
  733. ff_h263_decode_end,
  734. ff_h263_decode_frame,
  735. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  736. .options = mpeg4_decoptions,
  737. };
  738. AVCodec wmv1_decoder = {
  739. "wmv1",
  740. CODEC_TYPE_VIDEO,
  741. CODEC_ID_WMV1,
  742. sizeof(MpegEncContext),
  743. ff_h263_decode_init,
  744. NULL,
  745. ff_h263_decode_end,
  746. ff_h263_decode_frame,
  747. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  748. mpeg4_decoptions,
  749. };
  750. AVCodec h263i_decoder = {
  751. "h263i",
  752. CODEC_TYPE_VIDEO,
  753. CODEC_ID_H263I,
  754. sizeof(MpegEncContext),
  755. ff_h263_decode_init,
  756. NULL,
  757. ff_h263_decode_end,
  758. ff_h263_decode_frame,
  759. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  760. mpeg4_decoptions,
  761. };