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.

331 lines
9.3KB

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