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.

800 lines
23KB

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