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.

596 lines
18KB

  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. //#define DEBUG
  23. //#define PRINT_FRAME_TIME
  24. #ifdef PRINT_FRAME_TIME
  25. static inline long long rdtsc()
  26. {
  27. long long l;
  28. asm volatile( "rdtsc\n\t"
  29. : "=A" (l)
  30. );
  31. // printf("%d\n", int(l/1000));
  32. return l;
  33. }
  34. #endif
  35. static int h263_decode_init(AVCodecContext *avctx)
  36. {
  37. MpegEncContext *s = avctx->priv_data;
  38. s->avctx = avctx;
  39. s->out_format = FMT_H263;
  40. s->width = avctx->width;
  41. s->height = avctx->height;
  42. s->workaround_bugs= avctx->workaround_bugs;
  43. /* select sub codec */
  44. switch(avctx->codec->id) {
  45. case CODEC_ID_H263:
  46. s->gob_number = 0;
  47. s->first_slice_line = 0;
  48. break;
  49. case CODEC_ID_MPEG4:
  50. s->time_increment_bits = 4; /* default value for broken headers */
  51. s->h263_pred = 1;
  52. s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
  53. break;
  54. case CODEC_ID_MSMPEG4V1:
  55. s->h263_msmpeg4 = 1;
  56. s->h263_pred = 1;
  57. s->msmpeg4_version=1;
  58. break;
  59. case CODEC_ID_MSMPEG4V2:
  60. s->h263_msmpeg4 = 1;
  61. s->h263_pred = 1;
  62. s->msmpeg4_version=2;
  63. break;
  64. case CODEC_ID_MSMPEG4V3:
  65. s->h263_msmpeg4 = 1;
  66. s->h263_pred = 1;
  67. s->msmpeg4_version=3;
  68. break;
  69. case CODEC_ID_WMV1:
  70. s->h263_msmpeg4 = 1;
  71. s->h263_pred = 1;
  72. s->msmpeg4_version=4;
  73. break;
  74. case CODEC_ID_WMV2:
  75. s->h263_msmpeg4 = 1;
  76. s->h263_pred = 1;
  77. s->msmpeg4_version=5;
  78. break;
  79. case CODEC_ID_H263I:
  80. s->h263_intel = 1;
  81. break;
  82. default:
  83. return -1;
  84. }
  85. s->codec_id= avctx->codec->id;
  86. /* for h263, we allocate the images after having read the header */
  87. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  88. if (MPV_common_init(s) < 0)
  89. return -1;
  90. if (s->h263_msmpeg4)
  91. ff_msmpeg4_decode_init(s);
  92. else
  93. h263_decode_init_vlc(s);
  94. return 0;
  95. }
  96. static int h263_decode_end(AVCodecContext *avctx)
  97. {
  98. MpegEncContext *s = avctx->priv_data;
  99. MPV_common_end(s);
  100. return 0;
  101. }
  102. /**
  103. * retunrs the number of bytes consumed for building the current frame
  104. */
  105. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  106. int pos= (get_bits_count(&s->gb)+7)>>3;
  107. if(s->divx_version>=500){
  108. //we would have to scan through the whole buf to handle the weird reordering ...
  109. return buf_size;
  110. }else{
  111. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  112. if(pos+10>buf_size) pos=buf_size; // oops ;)
  113. return pos;
  114. }
  115. }
  116. static int h263_decode_frame(AVCodecContext *avctx,
  117. void *data, int *data_size,
  118. UINT8 *buf, int buf_size)
  119. {
  120. MpegEncContext *s = avctx->priv_data;
  121. int ret;
  122. AVPicture *pict = data;
  123. #ifdef PRINT_FRAME_TIME
  124. uint64_t time= rdtsc();
  125. #endif
  126. #ifdef DEBUG
  127. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  128. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  129. #endif
  130. s->hurry_up= avctx->hurry_up;
  131. s->error_resilience= avctx->error_resilience;
  132. s->workaround_bugs= avctx->workaround_bugs;
  133. if(s->avctx->fourcc == ff_get_fourcc("XVIX") && s->workaround_bugs==0)
  134. s->workaround_bugs=2;
  135. s->flags= avctx->flags;
  136. *data_size = 0;
  137. /* no supplementary picture */
  138. if (buf_size == 0) {
  139. return 0;
  140. }
  141. if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
  142. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
  143. }else
  144. init_get_bits(&s->gb, buf, buf_size);
  145. s->bitstream_buffer_size=0;
  146. if (!s->context_initialized) {
  147. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  148. return -1;
  149. }
  150. /* let's go :-) */
  151. if (s->h263_msmpeg4) {
  152. ret = msmpeg4_decode_picture_header(s);
  153. } else if (s->h263_pred) {
  154. ret = mpeg4_decode_picture_header(s);
  155. s->has_b_frames= !s->low_delay;
  156. } else if (s->h263_intel) {
  157. ret = intel_h263_decode_picture_header(s);
  158. } else {
  159. ret = h263_decode_picture_header(s);
  160. }
  161. avctx->has_b_frames= s->has_b_frames;
  162. #if 0 // dump bits per frame / qp / complexity
  163. {
  164. static FILE *f=NULL;
  165. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  166. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  167. }
  168. #endif
  169. /* After H263 & mpeg4 header decode we have the height, width,*/
  170. /* and other parameters. So then we could init the picture */
  171. /* FIXME: By the way H263 decoder is evolving it should have */
  172. /* an H263EncContext */
  173. if ( s->width != avctx->width || s->height != avctx->height
  174. || avctx->aspect_ratio_info != s->aspect_ratio_info
  175. || avctx->aspected_width != s->aspected_width
  176. || avctx->aspected_height != s->aspected_height) {
  177. /* H.263 could change picture size any time */
  178. MPV_common_end(s);
  179. s->context_initialized=0;
  180. }
  181. if (!s->context_initialized) {
  182. avctx->width = s->width;
  183. avctx->height = s->height;
  184. avctx->aspect_ratio_info= s->aspect_ratio_info;
  185. if (s->aspect_ratio_info == FF_ASPECT_EXTENDED)
  186. {
  187. avctx->aspected_width = s->aspected_width;
  188. avctx->aspected_height = s->aspected_height;
  189. }
  190. if (MPV_common_init(s) < 0)
  191. return -1;
  192. }
  193. if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
  194. /* skip if the header was thrashed */
  195. if (ret < 0){
  196. fprintf(stderr, "header damaged\n");
  197. return -1;
  198. }
  199. /* skip b frames if we dont have reference frames */
  200. if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  201. /* skip b frames if we are in a hurry */
  202. if(s->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  203. if(s->next_p_frame_damaged){
  204. if(s->pict_type==B_TYPE)
  205. return get_consumed_bytes(s, buf_size);
  206. else
  207. s->next_p_frame_damaged=0;
  208. }
  209. MPV_frame_start(s, avctx);
  210. #ifdef DEBUG
  211. printf("qscale=%d\n", s->qscale);
  212. #endif
  213. /* init resync/ error resilience specific variables */
  214. s->next_resync_qscale= s->qscale;
  215. s->next_resync_gb= s->gb;
  216. if(s->resync_marker) s->mb_num_left= 0;
  217. else s->mb_num_left= s->mb_num;
  218. /* decode each macroblock */
  219. s->block_wrap[0]=
  220. s->block_wrap[1]=
  221. s->block_wrap[2]=
  222. s->block_wrap[3]= s->mb_width*2 + 2;
  223. s->block_wrap[4]=
  224. s->block_wrap[5]= s->mb_width + 2;
  225. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  226. /* Check for GOB headers on H.263 */
  227. /* FIXME: In the future H.263+ will have intra prediction */
  228. /* and we are gonna need another way to detect MPEG4 */
  229. if (s->mb_y && !s->h263_pred) {
  230. s->first_slice_line = h263_decode_gob_header(s);
  231. }
  232. if(s->msmpeg4_version==1){
  233. s->last_dc[0]=
  234. s->last_dc[1]=
  235. s->last_dc[2]= 128;
  236. }
  237. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  238. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  239. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  240. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  241. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  242. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  243. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  244. s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  245. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  246. s->block_index[0]+=2;
  247. s->block_index[1]+=2;
  248. s->block_index[2]+=2;
  249. s->block_index[3]+=2;
  250. s->block_index[4]++;
  251. s->block_index[5]++;
  252. #ifdef DEBUG
  253. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  254. #endif
  255. if(s->resync_marker){
  256. if(s->mb_num_left<=0){
  257. /* except the first block */
  258. if(s->mb_x!=0 || s->mb_y!=0){
  259. /* did we miss the next resync marker without noticing an error yet */
  260. if(((get_bits_count(&s->gb)+8)&(~7)) != s->next_resync_pos && s->decoding_error==0){
  261. fprintf(stderr, "slice end missmatch x:%d y:%d %d %d\n",
  262. s->mb_x, s->mb_y, get_bits_count(&s->gb), s->next_resync_pos);
  263. ff_conceal_past_errors(s, 1);
  264. }
  265. }
  266. s->qscale= s->next_resync_qscale;
  267. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  268. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  269. s->gb= s->next_resync_gb;
  270. s->resync_mb_x= s->mb_x; //we know that the marker is here cuz mb_num_left was the distance to it
  271. s->resync_mb_y= s->mb_y;
  272. s->first_slice_line=1;
  273. if(s->codec_id==CODEC_ID_MPEG4){
  274. ff_mpeg4_clean_buffers(s);
  275. ff_mpeg4_resync(s);
  276. }
  277. }
  278. if( s->resync_mb_x==s->mb_x
  279. && s->resync_mb_y==s->mb_y && s->decoding_error!=0){
  280. fprintf(stderr, "resynced at %d %d\n", s->mb_x, s->mb_y);
  281. s->decoding_error= 0;
  282. }
  283. }
  284. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  285. /* DCT & quantize */
  286. if(s->decoding_error!=DECODING_DESYNC){
  287. int last_error= s->decoding_error;
  288. clear_blocks(s->block[0]);
  289. s->mv_dir = MV_DIR_FORWARD;
  290. s->mv_type = MV_TYPE_16X16;
  291. if (s->h263_msmpeg4) {
  292. if (msmpeg4_decode_mb(s, s->block) < 0) {
  293. fprintf(stderr,"Error at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  294. s->decoding_error=DECODING_DESYNC;
  295. }
  296. } else {
  297. if (h263_decode_mb(s, s->block) < 0) {
  298. fprintf(stderr,"Error at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  299. s->decoding_error=DECODING_DESYNC;
  300. }
  301. }
  302. if(s->decoding_error!=last_error){
  303. ff_conceal_past_errors(s, 0);
  304. }
  305. }
  306. /* conceal errors */
  307. if( s->decoding_error==DECODING_DESYNC
  308. || (s->decoding_error==DECODING_ACDC_LOST && s->mb_intra)){
  309. s->mv_dir = MV_DIR_FORWARD;
  310. s->mv_type = MV_TYPE_16X16;
  311. s->mb_skiped=0;
  312. s->mb_intra=0;
  313. s->mv[0][0][0]=0; //FIXME this is not optimal
  314. s->mv[0][0][1]=0;
  315. clear_blocks(s->block[0]);
  316. }else if(s->decoding_error && !s->mb_intra){
  317. clear_blocks(s->block[0]);
  318. }
  319. //FIXME remove AC for intra
  320. MPV_decode_mb(s, s->block);
  321. s->mb_num_left--;
  322. }
  323. if ( avctx->draw_horiz_band
  324. && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
  325. UINT8 *src_ptr[3];
  326. int y, h, offset;
  327. y = s->mb_y * 16;
  328. h = s->height - y;
  329. if (h > 16)
  330. h = 16;
  331. if(s->pict_type==B_TYPE)
  332. offset = 0;
  333. else
  334. offset = y * s->linesize;
  335. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  336. src_ptr[0] = s->current_picture[0] + offset;
  337. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  338. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  339. } else {
  340. src_ptr[0] = s->last_picture[0] + offset;
  341. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  342. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  343. }
  344. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  345. y, s->width, h);
  346. }
  347. }
  348. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  349. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  350. /* divx 5.01+ bistream reorder stuff */
  351. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_version>=500){
  352. int current_pos= get_bits_count(&s->gb)>>3;
  353. if( buf_size - current_pos > 5
  354. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  355. int i;
  356. int startcode_found=0;
  357. for(i=current_pos; i<buf_size-3; i++){
  358. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  359. startcode_found=1;
  360. break;
  361. }
  362. }
  363. if(startcode_found){
  364. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  365. s->bitstream_buffer_size= buf_size - current_pos;
  366. }
  367. }
  368. }
  369. if(s->bitstream_buffer_size==0 && s->error_resilience>0){
  370. int left= s->gb.size*8 - get_bits_count(&s->gb);
  371. int max_extra=8;
  372. if(s->codec_id==CODEC_ID_MPEG4) max_extra+=32;
  373. if(left>max_extra){
  374. fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
  375. if(s->decoding_error==0)
  376. ff_conceal_past_errors(s, 1);
  377. }
  378. if(left<0){
  379. fprintf(stderr, "overreading %d bits\n", -left);
  380. if(s->decoding_error==0)
  381. ff_conceal_past_errors(s, 1);
  382. }
  383. }
  384. MPV_frame_end(s);
  385. #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
  386. {
  387. int mb_y;
  388. s->has_b_frames=1;
  389. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  390. int mb_x;
  391. int y= mb_y*16 + 8;
  392. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  393. int x= mb_x*16 + 8;
  394. uint8_t *ptr= s->last_picture[0];
  395. int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
  396. int mx= (s->motion_val[xy][0]>>1) + x;
  397. int my= (s->motion_val[xy][1]>>1) + y;
  398. int i;
  399. int max;
  400. if(mx<0) mx=0;
  401. if(my<0) my=0;
  402. if(mx>=s->width) mx= s->width -1;
  403. if(my>=s->height) my= s->height-1;
  404. max= ABS(mx-x);
  405. if(ABS(my-y) > max) max= ABS(my-y);
  406. /* the ugliest linedrawing routine ... */
  407. for(i=0; i<max; i++){
  408. int x1= x + (mx-x)*i/max;
  409. int y1= y + (my-y)*i/max;
  410. ptr[y1*s->linesize + x1]+=100;
  411. }
  412. ptr[y*s->linesize + x]+=100;
  413. s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
  414. }
  415. }
  416. }
  417. #endif
  418. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  419. pict->data[0] = s->current_picture[0];
  420. pict->data[1] = s->current_picture[1];
  421. pict->data[2] = s->current_picture[2];
  422. } else {
  423. pict->data[0] = s->last_picture[0];
  424. pict->data[1] = s->last_picture[1];
  425. pict->data[2] = s->last_picture[2];
  426. }
  427. pict->linesize[0] = s->linesize;
  428. pict->linesize[1] = s->uvlinesize;
  429. pict->linesize[2] = s->uvlinesize;
  430. avctx->quality = s->qscale;
  431. /* Return the Picture timestamp as the frame number */
  432. /* we substract 1 because it is added on utils.c */
  433. avctx->frame_number = s->picture_number - 1;
  434. /* dont output the last pic after seeking
  435. note we allready added +1 for the current pix in MPV_frame_end(s) */
  436. if(s->num_available_buffers>=2 || (!s->has_b_frames))
  437. *data_size = sizeof(AVPicture);
  438. #ifdef PRINT_FRAME_TIME
  439. printf("%Ld\n", rdtsc()-time);
  440. #endif
  441. return get_consumed_bytes(s, buf_size);
  442. }
  443. AVCodec mpeg4_decoder = {
  444. "mpeg4",
  445. CODEC_TYPE_VIDEO,
  446. CODEC_ID_MPEG4,
  447. sizeof(MpegEncContext),
  448. h263_decode_init,
  449. NULL,
  450. h263_decode_end,
  451. h263_decode_frame,
  452. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  453. };
  454. AVCodec h263_decoder = {
  455. "h263",
  456. CODEC_TYPE_VIDEO,
  457. CODEC_ID_H263,
  458. sizeof(MpegEncContext),
  459. h263_decode_init,
  460. NULL,
  461. h263_decode_end,
  462. h263_decode_frame,
  463. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  464. };
  465. AVCodec msmpeg4v1_decoder = {
  466. "msmpeg4v1",
  467. CODEC_TYPE_VIDEO,
  468. CODEC_ID_MSMPEG4V1,
  469. sizeof(MpegEncContext),
  470. h263_decode_init,
  471. NULL,
  472. h263_decode_end,
  473. h263_decode_frame,
  474. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  475. };
  476. AVCodec msmpeg4v2_decoder = {
  477. "msmpeg4v2",
  478. CODEC_TYPE_VIDEO,
  479. CODEC_ID_MSMPEG4V2,
  480. sizeof(MpegEncContext),
  481. h263_decode_init,
  482. NULL,
  483. h263_decode_end,
  484. h263_decode_frame,
  485. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  486. };
  487. AVCodec msmpeg4v3_decoder = {
  488. "msmpeg4",
  489. CODEC_TYPE_VIDEO,
  490. CODEC_ID_MSMPEG4V3,
  491. sizeof(MpegEncContext),
  492. h263_decode_init,
  493. NULL,
  494. h263_decode_end,
  495. h263_decode_frame,
  496. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  497. };
  498. AVCodec wmv1_decoder = {
  499. "wmv1",
  500. CODEC_TYPE_VIDEO,
  501. CODEC_ID_WMV1,
  502. sizeof(MpegEncContext),
  503. h263_decode_init,
  504. NULL,
  505. h263_decode_end,
  506. h263_decode_frame,
  507. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  508. };
  509. AVCodec wmv2_decoder = {
  510. "wmv2",
  511. CODEC_TYPE_VIDEO,
  512. CODEC_ID_WMV2,
  513. sizeof(MpegEncContext),
  514. h263_decode_init,
  515. NULL,
  516. h263_decode_end,
  517. h263_decode_frame,
  518. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  519. };
  520. AVCodec h263i_decoder = {
  521. "h263i",
  522. CODEC_TYPE_VIDEO,
  523. CODEC_ID_H263I,
  524. sizeof(MpegEncContext),
  525. h263_decode_init,
  526. NULL,
  527. h263_decode_end,
  528. h263_decode_frame,
  529. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  530. };