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.

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