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.

421 lines
12KB

  1. /*
  2. * H263 decoder
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. /* select sub codec */
  43. switch(avctx->codec->id) {
  44. case CODEC_ID_H263:
  45. s->gob_number = 0;
  46. s->first_gob_line = 0;
  47. break;
  48. case CODEC_ID_MPEG4:
  49. s->time_increment_bits = 4; /* default value for broken headers */
  50. s->h263_pred = 1;
  51. s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
  52. break;
  53. case CODEC_ID_MSMPEG4V1:
  54. s->h263_msmpeg4 = 1;
  55. s->h263_pred = 1;
  56. s->msmpeg4_version=1;
  57. break;
  58. case CODEC_ID_MSMPEG4V2:
  59. s->h263_msmpeg4 = 1;
  60. s->h263_pred = 1;
  61. s->msmpeg4_version=2;
  62. break;
  63. case CODEC_ID_MSMPEG4V3:
  64. s->h263_msmpeg4 = 1;
  65. s->h263_pred = 1;
  66. s->msmpeg4_version=3;
  67. break;
  68. case CODEC_ID_WMV1:
  69. s->h263_msmpeg4 = 1;
  70. s->h263_pred = 1;
  71. s->msmpeg4_version=4;
  72. break;
  73. case CODEC_ID_H263I:
  74. s->h263_intel = 1;
  75. break;
  76. default:
  77. return -1;
  78. }
  79. s->codec_id= avctx->codec->id;
  80. avctx->mbskip_table= s->mbskip_table;
  81. /* for h263, we allocate the images after having read the header */
  82. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  83. if (MPV_common_init(s) < 0)
  84. return -1;
  85. if (s->h263_msmpeg4)
  86. msmpeg4_decode_init_vlc(s);
  87. else
  88. h263_decode_init_vlc(s);
  89. return 0;
  90. }
  91. static int h263_decode_end(AVCodecContext *avctx)
  92. {
  93. MpegEncContext *s = avctx->priv_data;
  94. MPV_common_end(s);
  95. return 0;
  96. }
  97. static int h263_decode_frame(AVCodecContext *avctx,
  98. void *data, int *data_size,
  99. UINT8 *buf, int buf_size)
  100. {
  101. MpegEncContext *s = avctx->priv_data;
  102. int ret;
  103. AVPicture *pict = data;
  104. #ifdef PRINT_FRAME_TIME
  105. uint64_t time= rdtsc();
  106. #endif
  107. #ifdef DEBUG
  108. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  109. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  110. #endif
  111. s->hurry_up= avctx->hurry_up;
  112. /* no supplementary picture */
  113. if (buf_size == 0) {
  114. *data_size = 0;
  115. return 0;
  116. }
  117. if(s->bitstream_buffer_size){ //divx 5.01+ frame reorder
  118. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
  119. s->bitstream_buffer_size=0;
  120. }else
  121. init_get_bits(&s->gb, buf, buf_size);
  122. /* let's go :-) */
  123. if (s->h263_msmpeg4) {
  124. ret = msmpeg4_decode_picture_header(s);
  125. } else if (s->h263_pred) {
  126. ret = mpeg4_decode_picture_header(s);
  127. s->has_b_frames= !s->low_delay;
  128. } else if (s->h263_intel) {
  129. ret = intel_h263_decode_picture_header(s);
  130. } else {
  131. ret = h263_decode_picture_header(s);
  132. }
  133. /* After H263 & mpeg4 header decode we have the height, width,*/
  134. /* and other parameters. So then we could init the picture */
  135. /* FIXME: By the way H263 decoder is evolving it should have */
  136. /* an H263EncContext */
  137. if (!s->context_initialized) {
  138. avctx->width = s->width;
  139. avctx->height = s->height;
  140. avctx->aspect_ratio_info= s->aspect_ratio_info;
  141. if (MPV_common_init(s) < 0)
  142. return -1;
  143. } else if (s->width != avctx->width || s->height != avctx->height) {
  144. /* H.263 could change picture size any time */
  145. MPV_common_end(s);
  146. if (MPV_common_init(s) < 0)
  147. return -1;
  148. }
  149. if(ret==FRAME_SKIPED) return 0;
  150. if (ret < 0)
  151. return -1;
  152. /* skip b frames if we dont have reference frames */
  153. if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
  154. /* skip b frames if we are in a hurry */
  155. if(s->hurry_up && s->pict_type==B_TYPE) return 0;
  156. MPV_frame_start(s);
  157. #ifdef DEBUG
  158. printf("qscale=%d\n", s->qscale);
  159. #endif
  160. /* decode each macroblock */
  161. s->block_wrap[0]=
  162. s->block_wrap[1]=
  163. s->block_wrap[2]=
  164. s->block_wrap[3]= s->mb_width*2 + 2;
  165. s->block_wrap[4]=
  166. s->block_wrap[5]= s->mb_width + 2;
  167. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  168. /* Check for GOB headers on H.263 */
  169. /* FIXME: In the future H.263+ will have intra prediction */
  170. /* and we are gonna need another way to detect MPEG4 */
  171. if (s->mb_y && !s->h263_pred) {
  172. s->first_gob_line = h263_decode_gob_header(s);
  173. }
  174. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  175. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  176. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  177. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  178. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  179. 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);
  180. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  181. s->block_index[0]+=2;
  182. s->block_index[1]+=2;
  183. s->block_index[2]+=2;
  184. s->block_index[3]+=2;
  185. s->block_index[4]++;
  186. s->block_index[5]++;
  187. #ifdef DEBUG
  188. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  189. #endif
  190. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  191. /* DCT & quantize */
  192. if (s->h263_pred && s->msmpeg4_version!=2) {
  193. h263_dc_scale(s);
  194. } else {
  195. /* default quantization values */
  196. s->y_dc_scale = 8;
  197. s->c_dc_scale = 8;
  198. }
  199. clear_blocks(s->block[0]);
  200. s->mv_dir = MV_DIR_FORWARD;
  201. s->mv_type = MV_TYPE_16X16;
  202. if (s->h263_msmpeg4) {
  203. if (msmpeg4_decode_mb(s, s->block) < 0) {
  204. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  205. return -1;
  206. }
  207. } else {
  208. if (h263_decode_mb(s, s->block) < 0) {
  209. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  210. return -1;
  211. }
  212. }
  213. MPV_decode_mb(s, s->block);
  214. }
  215. if ( avctx->draw_horiz_band
  216. && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
  217. UINT8 *src_ptr[3];
  218. int y, h, offset;
  219. y = s->mb_y * 16;
  220. h = s->height - y;
  221. if (h > 16)
  222. h = 16;
  223. offset = y * s->linesize;
  224. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  225. src_ptr[0] = s->current_picture[0] + offset;
  226. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  227. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  228. } else {
  229. src_ptr[0] = s->last_picture[0] + offset;
  230. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  231. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  232. }
  233. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  234. y, s->width, h);
  235. }
  236. }
  237. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  238. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  239. /* divx 5.01+ bistream reorder stuff */
  240. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){
  241. int current_pos= get_bits_count(&s->gb)/8;
  242. if( buf_size - current_pos > 5
  243. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  244. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  245. s->bitstream_buffer_size= buf_size - current_pos;
  246. }
  247. }
  248. MPV_frame_end(s);
  249. #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
  250. {
  251. int mb_y;
  252. s->has_b_frames=1;
  253. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  254. int mb_x;
  255. int y= mb_y*16;
  256. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  257. int x= mb_x*16;
  258. uint8_t *ptr= s->last_picture[0];
  259. int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
  260. int mx= (s->motion_val[xy][0]>>1) + x;
  261. int my= (s->motion_val[xy][1]>>1) + y;
  262. int i;
  263. int max;
  264. if(mx<0) mx=0;
  265. if(my<0) my=0;
  266. if(mx>=s->width) mx= s->width -1;
  267. if(my>=s->height) my= s->height-1;
  268. max= ABS(mx-x);
  269. if(ABS(my-y) > max) max= ABS(my-y);
  270. /* the ugliest linedrawing routine ... */
  271. for(i=0; i<max; i++){
  272. int x1= x + (mx-x)*i/max;
  273. int y1= y + (my-y)*i/max;
  274. ptr[y1*s->linesize + x1]+=100;
  275. }
  276. s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
  277. }
  278. }
  279. }
  280. #endif
  281. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  282. pict->data[0] = s->current_picture[0];
  283. pict->data[1] = s->current_picture[1];
  284. pict->data[2] = s->current_picture[2];
  285. } else {
  286. pict->data[0] = s->last_picture[0];
  287. pict->data[1] = s->last_picture[1];
  288. pict->data[2] = s->last_picture[2];
  289. }
  290. pict->linesize[0] = s->linesize;
  291. pict->linesize[1] = s->linesize / 2;
  292. pict->linesize[2] = s->linesize / 2;
  293. avctx->quality = s->qscale;
  294. /* Return the Picture timestamp as the frame number */
  295. /* we substract 1 because it is added on utils.c */
  296. avctx->frame_number = s->picture_number - 1;
  297. /* dont output the last pic after seeking
  298. note we allready added +1 for the current pix in MPV_frame_end(s) */
  299. if(s->num_available_buffers>=2 || (!s->has_b_frames))
  300. *data_size = sizeof(AVPicture);
  301. #ifdef PRINT_FRAME_TIME
  302. printf("%Ld\n", rdtsc()-time);
  303. #endif
  304. return buf_size;
  305. }
  306. AVCodec mpeg4_decoder = {
  307. "mpeg4",
  308. CODEC_TYPE_VIDEO,
  309. CODEC_ID_MPEG4,
  310. sizeof(MpegEncContext),
  311. h263_decode_init,
  312. NULL,
  313. h263_decode_end,
  314. h263_decode_frame,
  315. CODEC_CAP_DRAW_HORIZ_BAND,
  316. };
  317. AVCodec h263_decoder = {
  318. "h263",
  319. CODEC_TYPE_VIDEO,
  320. CODEC_ID_H263,
  321. sizeof(MpegEncContext),
  322. h263_decode_init,
  323. NULL,
  324. h263_decode_end,
  325. h263_decode_frame,
  326. CODEC_CAP_DRAW_HORIZ_BAND,
  327. };
  328. AVCodec msmpeg4v1_decoder = {
  329. "msmpeg4v1",
  330. CODEC_TYPE_VIDEO,
  331. CODEC_ID_MSMPEG4V1,
  332. sizeof(MpegEncContext),
  333. h263_decode_init,
  334. NULL,
  335. h263_decode_end,
  336. h263_decode_frame,
  337. CODEC_CAP_DRAW_HORIZ_BAND,
  338. };
  339. AVCodec msmpeg4v2_decoder = {
  340. "msmpeg4v2",
  341. CODEC_TYPE_VIDEO,
  342. CODEC_ID_MSMPEG4V2,
  343. sizeof(MpegEncContext),
  344. h263_decode_init,
  345. NULL,
  346. h263_decode_end,
  347. h263_decode_frame,
  348. CODEC_CAP_DRAW_HORIZ_BAND,
  349. };
  350. AVCodec msmpeg4v3_decoder = {
  351. "msmpeg4",
  352. CODEC_TYPE_VIDEO,
  353. CODEC_ID_MSMPEG4V3,
  354. sizeof(MpegEncContext),
  355. h263_decode_init,
  356. NULL,
  357. h263_decode_end,
  358. h263_decode_frame,
  359. CODEC_CAP_DRAW_HORIZ_BAND,
  360. };
  361. AVCodec wmv1_decoder = {
  362. "wmv1",
  363. CODEC_TYPE_VIDEO,
  364. CODEC_ID_WMV1,
  365. sizeof(MpegEncContext),
  366. h263_decode_init,
  367. NULL,
  368. h263_decode_end,
  369. h263_decode_frame,
  370. CODEC_CAP_DRAW_HORIZ_BAND,
  371. };
  372. AVCodec h263i_decoder = {
  373. "h263i",
  374. CODEC_TYPE_VIDEO,
  375. CODEC_ID_H263I,
  376. sizeof(MpegEncContext),
  377. h263_decode_init,
  378. NULL,
  379. h263_decode_end,
  380. h263_decode_frame,
  381. CODEC_CAP_DRAW_HORIZ_BAND,
  382. };