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.

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