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.

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