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.

556 lines
17KB

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