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.

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