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.

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