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.

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