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.

838 lines
25KB

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