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.

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