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.

354 lines
10.0KB

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