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.

871 lines
26KB

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