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.

375 lines
11KB

  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. static int h263_decode_init(AVCodecContext *avctx)
  27. {
  28. MpegEncContext *s = avctx->priv_data;
  29. s->avctx = avctx;
  30. s->out_format = FMT_H263;
  31. s->width = avctx->width;
  32. s->height = avctx->height;
  33. /* select sub codec */
  34. switch(avctx->codec->id) {
  35. case CODEC_ID_H263:
  36. s->gob_number = 0;
  37. s->first_gob_line = 0;
  38. break;
  39. case CODEC_ID_MPEG4:
  40. s->time_increment_bits = 4; /* default value for broken headers */
  41. s->h263_pred = 1;
  42. s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
  43. break;
  44. case CODEC_ID_MSMPEG4V1:
  45. s->h263_msmpeg4 = 1;
  46. s->h263_pred = 1;
  47. s->msmpeg4_version=1;
  48. break;
  49. case CODEC_ID_MSMPEG4V2:
  50. s->h263_msmpeg4 = 1;
  51. s->h263_pred = 1;
  52. s->msmpeg4_version=2;
  53. break;
  54. case CODEC_ID_MSMPEG4V3:
  55. s->h263_msmpeg4 = 1;
  56. s->h263_pred = 1;
  57. s->msmpeg4_version=3;
  58. break;
  59. case CODEC_ID_WMV1:
  60. s->h263_msmpeg4 = 1;
  61. s->h263_pred = 1;
  62. s->msmpeg4_version=4;
  63. break;
  64. case CODEC_ID_H263I:
  65. s->h263_intel = 1;
  66. break;
  67. default:
  68. return -1;
  69. }
  70. s->codec_id= avctx->codec->id;
  71. avctx->mbskip_table= s->mbskip_table;
  72. /* for h263, we allocate the images after having read the header */
  73. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  74. if (MPV_common_init(s) < 0)
  75. return -1;
  76. if (s->h263_msmpeg4)
  77. msmpeg4_decode_init_vlc(s);
  78. else
  79. h263_decode_init_vlc(s);
  80. return 0;
  81. }
  82. static int h263_decode_end(AVCodecContext *avctx)
  83. {
  84. MpegEncContext *s = avctx->priv_data;
  85. MPV_common_end(s);
  86. return 0;
  87. }
  88. static int h263_decode_frame(AVCodecContext *avctx,
  89. void *data, int *data_size,
  90. UINT8 *buf, int buf_size)
  91. {
  92. MpegEncContext *s = avctx->priv_data;
  93. int ret;
  94. AVPicture *pict = data;
  95. #ifdef DEBUG
  96. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  97. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  98. #endif
  99. s->hurry_up= avctx->hurry_up;
  100. /* no supplementary picture */
  101. if (buf_size == 0) {
  102. *data_size = 0;
  103. return 0;
  104. }
  105. if(s->bitstream_buffer_size) //divx 5.01+ frame reorder
  106. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
  107. else
  108. init_get_bits(&s->gb, buf, buf_size);
  109. /* let's go :-) */
  110. if (s->h263_msmpeg4) {
  111. ret = msmpeg4_decode_picture_header(s);
  112. } else if (s->h263_pred) {
  113. ret = mpeg4_decode_picture_header(s);
  114. s->has_b_frames= !s->low_delay;
  115. } else if (s->h263_intel) {
  116. ret = intel_h263_decode_picture_header(s);
  117. } else {
  118. ret = h263_decode_picture_header(s);
  119. }
  120. /* After H263 & mpeg4 header decode we have the height, width,*/
  121. /* and other parameters. So then we could init the picture */
  122. /* FIXME: By the way H263 decoder is evolving it should have */
  123. /* an H263EncContext */
  124. if (!s->context_initialized) {
  125. avctx->width = s->width;
  126. avctx->height = s->height;
  127. avctx->aspect_ratio_info= s->aspect_ratio_info;
  128. if (MPV_common_init(s) < 0)
  129. return -1;
  130. } else if (s->width != avctx->width || s->height != avctx->height) {
  131. /* H.263 could change picture size any time */
  132. MPV_common_end(s);
  133. if (MPV_common_init(s) < 0)
  134. return -1;
  135. }
  136. if(ret==FRAME_SKIPED) return 0;
  137. if (ret < 0)
  138. return -1;
  139. /* skip b frames if we dont have reference frames */
  140. if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
  141. /* skip b frames if we are in a hurry */
  142. if(s->hurry_up && s->pict_type==B_TYPE) return 0;
  143. MPV_frame_start(s);
  144. #ifdef DEBUG
  145. printf("qscale=%d\n", s->qscale);
  146. #endif
  147. /* decode each macroblock */
  148. s->block_wrap[0]=
  149. s->block_wrap[1]=
  150. s->block_wrap[2]=
  151. s->block_wrap[3]= s->mb_width*2 + 2;
  152. s->block_wrap[4]=
  153. s->block_wrap[5]= s->mb_width + 2;
  154. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  155. /* Check for GOB headers on H.263 */
  156. /* FIXME: In the future H.263+ will have intra prediction */
  157. /* and we are gonna need another way to detect MPEG4 */
  158. if (s->mb_y && !s->h263_pred) {
  159. s->first_gob_line = h263_decode_gob_header(s);
  160. }
  161. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  162. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  163. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  164. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  165. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  166. 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);
  167. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  168. s->block_index[0]+=2;
  169. s->block_index[1]+=2;
  170. s->block_index[2]+=2;
  171. s->block_index[3]+=2;
  172. s->block_index[4]++;
  173. s->block_index[5]++;
  174. #ifdef DEBUG
  175. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  176. #endif
  177. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  178. /* DCT & quantize */
  179. if (s->h263_pred && s->msmpeg4_version!=2) {
  180. h263_dc_scale(s);
  181. } else {
  182. /* default quantization values */
  183. s->y_dc_scale = 8;
  184. s->c_dc_scale = 8;
  185. }
  186. clear_blocks(s->block[0]);
  187. s->mv_dir = MV_DIR_FORWARD;
  188. s->mv_type = MV_TYPE_16X16;
  189. if (s->h263_msmpeg4) {
  190. if (msmpeg4_decode_mb(s, s->block) < 0) {
  191. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  192. return -1;
  193. }
  194. } else {
  195. if (h263_decode_mb(s, s->block) < 0) {
  196. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  197. return -1;
  198. }
  199. }
  200. MPV_decode_mb(s, s->block);
  201. }
  202. if ( avctx->draw_horiz_band
  203. && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
  204. UINT8 *src_ptr[3];
  205. int y, h, offset;
  206. y = s->mb_y * 16;
  207. h = s->height - y;
  208. if (h > 16)
  209. h = 16;
  210. offset = y * s->linesize;
  211. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  212. src_ptr[0] = s->current_picture[0] + offset;
  213. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  214. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  215. } else {
  216. src_ptr[0] = s->last_picture[0] + offset;
  217. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  218. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  219. }
  220. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  221. y, s->width, h);
  222. }
  223. }
  224. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  225. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  226. /* divx 5.01+ bistream reorder stuff */
  227. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){
  228. int current_pos= get_bits_count(&s->gb)/8;
  229. if( buf_size - current_pos > 5
  230. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  231. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  232. s->bitstream_buffer_size= buf_size - current_pos;
  233. }
  234. }else
  235. s->bitstream_buffer_size=0;
  236. MPV_frame_end(s);
  237. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  238. pict->data[0] = s->current_picture[0];
  239. pict->data[1] = s->current_picture[1];
  240. pict->data[2] = s->current_picture[2];
  241. } else {
  242. pict->data[0] = s->last_picture[0];
  243. pict->data[1] = s->last_picture[1];
  244. pict->data[2] = s->last_picture[2];
  245. }
  246. pict->linesize[0] = s->linesize;
  247. pict->linesize[1] = s->linesize / 2;
  248. pict->linesize[2] = s->linesize / 2;
  249. avctx->quality = s->qscale;
  250. /* Return the Picture timestamp as the frame number */
  251. /* we substract 1 because it is added on utils.c */
  252. avctx->frame_number = s->picture_number - 1;
  253. /* dont output the last pic after seeking
  254. note we allready added +1 for the current pix in MPV_frame_end(s) */
  255. if(s->num_available_buffers>=2 || (!s->has_b_frames))
  256. *data_size = sizeof(AVPicture);
  257. return buf_size;
  258. }
  259. AVCodec mpeg4_decoder = {
  260. "mpeg4",
  261. CODEC_TYPE_VIDEO,
  262. CODEC_ID_MPEG4,
  263. sizeof(MpegEncContext),
  264. h263_decode_init,
  265. NULL,
  266. h263_decode_end,
  267. h263_decode_frame,
  268. CODEC_CAP_DRAW_HORIZ_BAND,
  269. };
  270. AVCodec h263_decoder = {
  271. "h263",
  272. CODEC_TYPE_VIDEO,
  273. CODEC_ID_H263,
  274. sizeof(MpegEncContext),
  275. h263_decode_init,
  276. NULL,
  277. h263_decode_end,
  278. h263_decode_frame,
  279. CODEC_CAP_DRAW_HORIZ_BAND,
  280. };
  281. AVCodec msmpeg4v1_decoder = {
  282. "msmpeg4v1",
  283. CODEC_TYPE_VIDEO,
  284. CODEC_ID_MSMPEG4V1,
  285. sizeof(MpegEncContext),
  286. h263_decode_init,
  287. NULL,
  288. h263_decode_end,
  289. h263_decode_frame,
  290. CODEC_CAP_DRAW_HORIZ_BAND,
  291. };
  292. AVCodec msmpeg4v2_decoder = {
  293. "msmpeg4v2",
  294. CODEC_TYPE_VIDEO,
  295. CODEC_ID_MSMPEG4V2,
  296. sizeof(MpegEncContext),
  297. h263_decode_init,
  298. NULL,
  299. h263_decode_end,
  300. h263_decode_frame,
  301. CODEC_CAP_DRAW_HORIZ_BAND,
  302. };
  303. AVCodec msmpeg4v3_decoder = {
  304. "msmpeg4",
  305. CODEC_TYPE_VIDEO,
  306. CODEC_ID_MSMPEG4V3,
  307. sizeof(MpegEncContext),
  308. h263_decode_init,
  309. NULL,
  310. h263_decode_end,
  311. h263_decode_frame,
  312. CODEC_CAP_DRAW_HORIZ_BAND,
  313. };
  314. AVCodec wmv1_decoder = {
  315. "wmv1",
  316. CODEC_TYPE_VIDEO,
  317. CODEC_ID_WMV1,
  318. sizeof(MpegEncContext),
  319. h263_decode_init,
  320. NULL,
  321. h263_decode_end,
  322. h263_decode_frame,
  323. CODEC_CAP_DRAW_HORIZ_BAND,
  324. };
  325. AVCodec h263i_decoder = {
  326. "h263i",
  327. CODEC_TYPE_VIDEO,
  328. CODEC_ID_H263I,
  329. sizeof(MpegEncContext),
  330. h263_decode_init,
  331. NULL,
  332. h263_decode_end,
  333. h263_decode_frame,
  334. CODEC_CAP_DRAW_HORIZ_BAND,
  335. };