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.

427 lines
13KB

  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. 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_gob_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. /* no supplementary picture */
  114. if (buf_size == 0) {
  115. *data_size = 0;
  116. return 0;
  117. }
  118. if(s->bitstream_buffer_size){ //divx 5.01+ frame reorder
  119. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
  120. s->bitstream_buffer_size=0;
  121. }else
  122. init_get_bits(&s->gb, buf, buf_size);
  123. /* let's go :-) */
  124. if (s->h263_msmpeg4) {
  125. ret = msmpeg4_decode_picture_header(s);
  126. } else if (s->h263_pred) {
  127. ret = mpeg4_decode_picture_header(s);
  128. s->has_b_frames= !s->low_delay;
  129. } else if (s->h263_intel) {
  130. ret = intel_h263_decode_picture_header(s);
  131. } else {
  132. ret = h263_decode_picture_header(s);
  133. }
  134. /* After H263 & mpeg4 header decode we have the height, width,*/
  135. /* and other parameters. So then we could init the picture */
  136. /* FIXME: By the way H263 decoder is evolving it should have */
  137. /* an H263EncContext */
  138. if (!s->context_initialized) {
  139. avctx->width = s->width;
  140. avctx->height = s->height;
  141. avctx->aspect_ratio_info= s->aspect_ratio_info;
  142. if (MPV_common_init(s) < 0)
  143. return -1;
  144. } else if (s->width != avctx->width || s->height != avctx->height) {
  145. /* H.263 could change picture size any time */
  146. MPV_common_end(s);
  147. if (MPV_common_init(s) < 0)
  148. return -1;
  149. }
  150. if(ret==FRAME_SKIPED) return 0;
  151. if (ret < 0)
  152. return -1;
  153. /* skip b frames if we dont have reference frames */
  154. if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
  155. /* skip b frames if we are in a hurry */
  156. if(s->hurry_up && s->pict_type==B_TYPE) return 0;
  157. MPV_frame_start(s);
  158. #ifdef DEBUG
  159. printf("qscale=%d\n", s->qscale);
  160. #endif
  161. /* decode each macroblock */
  162. s->block_wrap[0]=
  163. s->block_wrap[1]=
  164. s->block_wrap[2]=
  165. s->block_wrap[3]= s->mb_width*2 + 2;
  166. s->block_wrap[4]=
  167. s->block_wrap[5]= s->mb_width + 2;
  168. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  169. /* Check for GOB headers on H.263 */
  170. /* FIXME: In the future H.263+ will have intra prediction */
  171. /* and we are gonna need another way to detect MPEG4 */
  172. if (s->mb_y && !s->h263_pred) {
  173. s->first_gob_line = h263_decode_gob_header(s);
  174. }
  175. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  176. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  177. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  178. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  179. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  180. 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);
  181. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  182. s->block_index[0]+=2;
  183. s->block_index[1]+=2;
  184. s->block_index[2]+=2;
  185. s->block_index[3]+=2;
  186. s->block_index[4]++;
  187. s->block_index[5]++;
  188. #ifdef DEBUG
  189. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  190. #endif
  191. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  192. /* DCT & quantize */
  193. if (s->h263_pred && s->msmpeg4_version!=2) {
  194. /* old ffmpeg encoded msmpeg4v3 workaround */
  195. if(s->workaround_bugs==1 && s->msmpeg4_version==3)
  196. ff_old_msmpeg4_dc_scale(s);
  197. else
  198. h263_dc_scale(s);
  199. } else {
  200. /* default quantization values */
  201. s->y_dc_scale = 8;
  202. s->c_dc_scale = 8;
  203. }
  204. clear_blocks(s->block[0]);
  205. s->mv_dir = MV_DIR_FORWARD;
  206. s->mv_type = MV_TYPE_16X16;
  207. if (s->h263_msmpeg4) {
  208. if (msmpeg4_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. } else {
  213. if (h263_decode_mb(s, s->block) < 0) {
  214. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  215. return -1;
  216. }
  217. }
  218. MPV_decode_mb(s, s->block);
  219. }
  220. if ( avctx->draw_horiz_band
  221. && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
  222. UINT8 *src_ptr[3];
  223. int y, h, offset;
  224. y = s->mb_y * 16;
  225. h = s->height - y;
  226. if (h > 16)
  227. h = 16;
  228. offset = y * s->linesize;
  229. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  230. src_ptr[0] = s->current_picture[0] + offset;
  231. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  232. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  233. } else {
  234. src_ptr[0] = s->last_picture[0] + offset;
  235. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  236. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  237. }
  238. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  239. y, s->width, h);
  240. }
  241. }
  242. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  243. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  244. /* divx 5.01+ bistream reorder stuff */
  245. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){
  246. int current_pos= get_bits_count(&s->gb)/8;
  247. if( buf_size - current_pos > 5
  248. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  249. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  250. s->bitstream_buffer_size= buf_size - current_pos;
  251. }
  252. }
  253. MPV_frame_end(s);
  254. #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
  255. {
  256. int mb_y;
  257. s->has_b_frames=1;
  258. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  259. int mb_x;
  260. int y= mb_y*16 + 8;
  261. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  262. int x= mb_x*16 + 8;
  263. uint8_t *ptr= s->last_picture[0];
  264. int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
  265. int mx= (s->motion_val[xy][0]>>1) + x;
  266. int my= (s->motion_val[xy][1]>>1) + y;
  267. int i;
  268. int max;
  269. if(mx<0) mx=0;
  270. if(my<0) my=0;
  271. if(mx>=s->width) mx= s->width -1;
  272. if(my>=s->height) my= s->height-1;
  273. max= ABS(mx-x);
  274. if(ABS(my-y) > max) max= ABS(my-y);
  275. /* the ugliest linedrawing routine ... */
  276. for(i=0; i<max; i++){
  277. int x1= x + (mx-x)*i/max;
  278. int y1= y + (my-y)*i/max;
  279. ptr[y1*s->linesize + x1]+=100;
  280. }
  281. ptr[y*s->linesize + x]+=100;
  282. s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
  283. }
  284. }
  285. }
  286. #endif
  287. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  288. pict->data[0] = s->current_picture[0];
  289. pict->data[1] = s->current_picture[1];
  290. pict->data[2] = s->current_picture[2];
  291. } else {
  292. pict->data[0] = s->last_picture[0];
  293. pict->data[1] = s->last_picture[1];
  294. pict->data[2] = s->last_picture[2];
  295. }
  296. pict->linesize[0] = s->linesize;
  297. pict->linesize[1] = s->linesize / 2;
  298. pict->linesize[2] = s->linesize / 2;
  299. avctx->quality = s->qscale;
  300. /* Return the Picture timestamp as the frame number */
  301. /* we substract 1 because it is added on utils.c */
  302. avctx->frame_number = s->picture_number - 1;
  303. /* dont output the last pic after seeking
  304. note we allready added +1 for the current pix in MPV_frame_end(s) */
  305. if(s->num_available_buffers>=2 || (!s->has_b_frames))
  306. *data_size = sizeof(AVPicture);
  307. #ifdef PRINT_FRAME_TIME
  308. printf("%Ld\n", rdtsc()-time);
  309. #endif
  310. return buf_size;
  311. }
  312. AVCodec mpeg4_decoder = {
  313. "mpeg4",
  314. CODEC_TYPE_VIDEO,
  315. CODEC_ID_MPEG4,
  316. sizeof(MpegEncContext),
  317. h263_decode_init,
  318. NULL,
  319. h263_decode_end,
  320. h263_decode_frame,
  321. CODEC_CAP_DRAW_HORIZ_BAND,
  322. };
  323. AVCodec h263_decoder = {
  324. "h263",
  325. CODEC_TYPE_VIDEO,
  326. CODEC_ID_H263,
  327. sizeof(MpegEncContext),
  328. h263_decode_init,
  329. NULL,
  330. h263_decode_end,
  331. h263_decode_frame,
  332. CODEC_CAP_DRAW_HORIZ_BAND,
  333. };
  334. AVCodec msmpeg4v1_decoder = {
  335. "msmpeg4v1",
  336. CODEC_TYPE_VIDEO,
  337. CODEC_ID_MSMPEG4V1,
  338. sizeof(MpegEncContext),
  339. h263_decode_init,
  340. NULL,
  341. h263_decode_end,
  342. h263_decode_frame,
  343. CODEC_CAP_DRAW_HORIZ_BAND,
  344. };
  345. AVCodec msmpeg4v2_decoder = {
  346. "msmpeg4v2",
  347. CODEC_TYPE_VIDEO,
  348. CODEC_ID_MSMPEG4V2,
  349. sizeof(MpegEncContext),
  350. h263_decode_init,
  351. NULL,
  352. h263_decode_end,
  353. h263_decode_frame,
  354. CODEC_CAP_DRAW_HORIZ_BAND,
  355. };
  356. AVCodec msmpeg4v3_decoder = {
  357. "msmpeg4",
  358. CODEC_TYPE_VIDEO,
  359. CODEC_ID_MSMPEG4V3,
  360. sizeof(MpegEncContext),
  361. h263_decode_init,
  362. NULL,
  363. h263_decode_end,
  364. h263_decode_frame,
  365. CODEC_CAP_DRAW_HORIZ_BAND,
  366. };
  367. AVCodec wmv1_decoder = {
  368. "wmv1",
  369. CODEC_TYPE_VIDEO,
  370. CODEC_ID_WMV1,
  371. sizeof(MpegEncContext),
  372. h263_decode_init,
  373. NULL,
  374. h263_decode_end,
  375. h263_decode_frame,
  376. CODEC_CAP_DRAW_HORIZ_BAND,
  377. };
  378. AVCodec h263i_decoder = {
  379. "h263i",
  380. CODEC_TYPE_VIDEO,
  381. CODEC_ID_H263I,
  382. sizeof(MpegEncContext),
  383. h263_decode_init,
  384. NULL,
  385. h263_decode_end,
  386. h263_decode_frame,
  387. CODEC_CAP_DRAW_HORIZ_BAND,
  388. };