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.

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