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.

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