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.

424 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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "dsputil.h"
  23. #include "avcodec.h"
  24. #include "mpegvideo.h"
  25. //#define DEBUG
  26. //#define PRINT_FRAME_TIME
  27. #ifdef PRINT_FRAME_TIME
  28. static inline long long rdtsc()
  29. {
  30. long long l;
  31. asm volatile( "rdtsc\n\t"
  32. : "=A" (l)
  33. );
  34. // printf("%d\n", int(l/1000));
  35. return l;
  36. }
  37. #endif
  38. static int h263_decode_init(AVCodecContext *avctx)
  39. {
  40. MpegEncContext *s = avctx->priv_data;
  41. s->avctx = avctx;
  42. s->out_format = FMT_H263;
  43. s->width = avctx->width;
  44. s->height = avctx->height;
  45. /* select sub codec */
  46. switch(avctx->codec->id) {
  47. case CODEC_ID_H263:
  48. s->gob_number = 0;
  49. s->first_gob_line = 0;
  50. break;
  51. case CODEC_ID_MPEG4:
  52. s->time_increment_bits = 4; /* default value for broken headers */
  53. s->h263_pred = 1;
  54. s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
  55. break;
  56. case CODEC_ID_MSMPEG4V1:
  57. s->h263_msmpeg4 = 1;
  58. s->h263_pred = 1;
  59. s->msmpeg4_version=1;
  60. break;
  61. case CODEC_ID_MSMPEG4V2:
  62. s->h263_msmpeg4 = 1;
  63. s->h263_pred = 1;
  64. s->msmpeg4_version=2;
  65. break;
  66. case CODEC_ID_MSMPEG4V3:
  67. s->h263_msmpeg4 = 1;
  68. s->h263_pred = 1;
  69. s->msmpeg4_version=3;
  70. break;
  71. case CODEC_ID_WMV1:
  72. s->h263_msmpeg4 = 1;
  73. s->h263_pred = 1;
  74. s->msmpeg4_version=4;
  75. break;
  76. case CODEC_ID_H263I:
  77. s->h263_intel = 1;
  78. break;
  79. default:
  80. return -1;
  81. }
  82. s->codec_id= avctx->codec->id;
  83. avctx->mbskip_table= s->mbskip_table;
  84. /* for h263, we allocate the images after having read the header */
  85. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  86. if (MPV_common_init(s) < 0)
  87. return -1;
  88. if (s->h263_msmpeg4)
  89. msmpeg4_decode_init_vlc(s);
  90. else
  91. h263_decode_init_vlc(s);
  92. return 0;
  93. }
  94. static int h263_decode_end(AVCodecContext *avctx)
  95. {
  96. MpegEncContext *s = avctx->priv_data;
  97. MPV_common_end(s);
  98. return 0;
  99. }
  100. static int h263_decode_frame(AVCodecContext *avctx,
  101. void *data, int *data_size,
  102. UINT8 *buf, int buf_size)
  103. {
  104. MpegEncContext *s = avctx->priv_data;
  105. int ret;
  106. AVPicture *pict = data;
  107. #ifdef PRINT_FRAME_TIME
  108. uint64_t time= rdtsc();
  109. #endif
  110. #ifdef DEBUG
  111. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  112. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  113. #endif
  114. s->hurry_up= avctx->hurry_up;
  115. /* no supplementary picture */
  116. if (buf_size == 0) {
  117. *data_size = 0;
  118. return 0;
  119. }
  120. if(s->bitstream_buffer_size){ //divx 5.01+ frame reorder
  121. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
  122. s->bitstream_buffer_size=0;
  123. }else
  124. init_get_bits(&s->gb, buf, buf_size);
  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. if (ret < 0)
  154. return -1;
  155. /* skip b frames if we dont have reference frames */
  156. if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
  157. /* skip b frames if we are in a hurry */
  158. if(s->hurry_up && s->pict_type==B_TYPE) return 0;
  159. MPV_frame_start(s);
  160. #ifdef DEBUG
  161. printf("qscale=%d\n", s->qscale);
  162. #endif
  163. /* decode each macroblock */
  164. s->block_wrap[0]=
  165. s->block_wrap[1]=
  166. s->block_wrap[2]=
  167. s->block_wrap[3]= s->mb_width*2 + 2;
  168. s->block_wrap[4]=
  169. s->block_wrap[5]= s->mb_width + 2;
  170. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  171. /* Check for GOB headers on H.263 */
  172. /* FIXME: In the future H.263+ will have intra prediction */
  173. /* and we are gonna need another way to detect MPEG4 */
  174. if (s->mb_y && !s->h263_pred) {
  175. s->first_gob_line = h263_decode_gob_header(s);
  176. }
  177. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  178. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  179. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  180. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  181. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  182. 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);
  183. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  184. s->block_index[0]+=2;
  185. s->block_index[1]+=2;
  186. s->block_index[2]+=2;
  187. s->block_index[3]+=2;
  188. s->block_index[4]++;
  189. s->block_index[5]++;
  190. #ifdef DEBUG
  191. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  192. #endif
  193. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  194. /* DCT & quantize */
  195. if (s->h263_pred && s->msmpeg4_version!=2) {
  196. h263_dc_scale(s);
  197. } else {
  198. /* default quantization values */
  199. s->y_dc_scale = 8;
  200. s->c_dc_scale = 8;
  201. }
  202. clear_blocks(s->block[0]);
  203. s->mv_dir = MV_DIR_FORWARD;
  204. s->mv_type = MV_TYPE_16X16;
  205. if (s->h263_msmpeg4) {
  206. if (msmpeg4_decode_mb(s, s->block) < 0) {
  207. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  208. return -1;
  209. }
  210. } else {
  211. if (h263_decode_mb(s, s->block) < 0) {
  212. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  213. return -1;
  214. }
  215. }
  216. MPV_decode_mb(s, s->block);
  217. }
  218. if ( avctx->draw_horiz_band
  219. && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
  220. UINT8 *src_ptr[3];
  221. int y, h, offset;
  222. y = s->mb_y * 16;
  223. h = s->height - y;
  224. if (h > 16)
  225. h = 16;
  226. offset = y * s->linesize;
  227. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  228. src_ptr[0] = s->current_picture[0] + offset;
  229. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  230. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  231. } else {
  232. src_ptr[0] = s->last_picture[0] + offset;
  233. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  234. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  235. }
  236. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  237. y, s->width, h);
  238. }
  239. }
  240. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  241. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  242. /* divx 5.01+ bistream reorder stuff */
  243. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){
  244. int current_pos= get_bits_count(&s->gb)/8;
  245. if( buf_size - current_pos > 5
  246. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  247. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  248. s->bitstream_buffer_size= buf_size - current_pos;
  249. }
  250. }
  251. MPV_frame_end(s);
  252. #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
  253. {
  254. int mb_y;
  255. s->has_b_frames=1;
  256. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  257. int mb_x;
  258. int y= mb_y*16;
  259. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  260. int x= mb_x*16;
  261. uint8_t *ptr= s->last_picture[0];
  262. int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
  263. int mx= (s->motion_val[xy][0]>>1) + x;
  264. int my= (s->motion_val[xy][1]>>1) + y;
  265. int i;
  266. int max;
  267. if(mx<0) mx=0;
  268. if(my<0) my=0;
  269. if(mx>=s->width) mx= s->width -1;
  270. if(my>=s->height) my= s->height-1;
  271. max= ABS(mx-x);
  272. if(ABS(my-y) > max) max= ABS(my-y);
  273. /* the ugliest linedrawing routine ... */
  274. for(i=0; i<max; i++){
  275. int x1= x + (mx-x)*i/max;
  276. int y1= y + (my-y)*i/max;
  277. ptr[y1*s->linesize + x1]+=100;
  278. }
  279. s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
  280. }
  281. }
  282. }
  283. #endif
  284. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  285. pict->data[0] = s->current_picture[0];
  286. pict->data[1] = s->current_picture[1];
  287. pict->data[2] = s->current_picture[2];
  288. } else {
  289. pict->data[0] = s->last_picture[0];
  290. pict->data[1] = s->last_picture[1];
  291. pict->data[2] = s->last_picture[2];
  292. }
  293. pict->linesize[0] = s->linesize;
  294. pict->linesize[1] = s->linesize / 2;
  295. pict->linesize[2] = s->linesize / 2;
  296. avctx->quality = s->qscale;
  297. /* Return the Picture timestamp as the frame number */
  298. /* we substract 1 because it is added on utils.c */
  299. avctx->frame_number = s->picture_number - 1;
  300. /* dont output the last pic after seeking
  301. note we allready added +1 for the current pix in MPV_frame_end(s) */
  302. if(s->num_available_buffers>=2 || (!s->has_b_frames))
  303. *data_size = sizeof(AVPicture);
  304. #ifdef PRINT_FRAME_TIME
  305. printf("%Ld\n", rdtsc()-time);
  306. #endif
  307. return buf_size;
  308. }
  309. AVCodec mpeg4_decoder = {
  310. "mpeg4",
  311. CODEC_TYPE_VIDEO,
  312. CODEC_ID_MPEG4,
  313. sizeof(MpegEncContext),
  314. h263_decode_init,
  315. NULL,
  316. h263_decode_end,
  317. h263_decode_frame,
  318. CODEC_CAP_DRAW_HORIZ_BAND,
  319. };
  320. AVCodec h263_decoder = {
  321. "h263",
  322. CODEC_TYPE_VIDEO,
  323. CODEC_ID_H263,
  324. sizeof(MpegEncContext),
  325. h263_decode_init,
  326. NULL,
  327. h263_decode_end,
  328. h263_decode_frame,
  329. CODEC_CAP_DRAW_HORIZ_BAND,
  330. };
  331. AVCodec msmpeg4v1_decoder = {
  332. "msmpeg4v1",
  333. CODEC_TYPE_VIDEO,
  334. CODEC_ID_MSMPEG4V1,
  335. sizeof(MpegEncContext),
  336. h263_decode_init,
  337. NULL,
  338. h263_decode_end,
  339. h263_decode_frame,
  340. CODEC_CAP_DRAW_HORIZ_BAND,
  341. };
  342. AVCodec msmpeg4v2_decoder = {
  343. "msmpeg4v2",
  344. CODEC_TYPE_VIDEO,
  345. CODEC_ID_MSMPEG4V2,
  346. sizeof(MpegEncContext),
  347. h263_decode_init,
  348. NULL,
  349. h263_decode_end,
  350. h263_decode_frame,
  351. CODEC_CAP_DRAW_HORIZ_BAND,
  352. };
  353. AVCodec msmpeg4v3_decoder = {
  354. "msmpeg4",
  355. CODEC_TYPE_VIDEO,
  356. CODEC_ID_MSMPEG4V3,
  357. sizeof(MpegEncContext),
  358. h263_decode_init,
  359. NULL,
  360. h263_decode_end,
  361. h263_decode_frame,
  362. CODEC_CAP_DRAW_HORIZ_BAND,
  363. };
  364. AVCodec wmv1_decoder = {
  365. "wmv1",
  366. CODEC_TYPE_VIDEO,
  367. CODEC_ID_WMV1,
  368. sizeof(MpegEncContext),
  369. h263_decode_init,
  370. NULL,
  371. h263_decode_end,
  372. h263_decode_frame,
  373. CODEC_CAP_DRAW_HORIZ_BAND,
  374. };
  375. AVCodec h263i_decoder = {
  376. "h263i",
  377. CODEC_TYPE_VIDEO,
  378. CODEC_ID_H263I,
  379. sizeof(MpegEncContext),
  380. h263_decode_init,
  381. NULL,
  382. h263_decode_end,
  383. h263_decode_frame,
  384. CODEC_CAP_DRAW_HORIZ_BAND,
  385. };