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.

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