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.

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