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.

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