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.

242 lines
6.5KB

  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. break;
  38. case CODEC_ID_MPEG4:
  39. s->time_increment_bits = 4; /* default value for broken headers */
  40. s->h263_pred = 1;
  41. break;
  42. case CODEC_ID_MSMPEG4:
  43. s->h263_msmpeg4 = 1;
  44. s->h263_pred = 1;
  45. break;
  46. case CODEC_ID_H263I:
  47. s->h263_intel = 1;
  48. break;
  49. default:
  50. return -1;
  51. }
  52. /* for h263, we allocate the images after having read the header */
  53. if (avctx->codec->id != CODEC_ID_H263)
  54. if (MPV_common_init(s) < 0)
  55. return -1;
  56. /* XXX: suppress this matrix init, only needed because using mpeg1
  57. dequantize in mmx case */
  58. for(i=0;i<64;i++)
  59. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  60. if (s->h263_msmpeg4)
  61. msmpeg4_decode_init_vlc(s);
  62. else
  63. h263_decode_init_vlc(s);
  64. return 0;
  65. }
  66. static int h263_decode_end(AVCodecContext *avctx)
  67. {
  68. MpegEncContext *s = avctx->priv_data;
  69. MPV_common_end(s);
  70. return 0;
  71. }
  72. static int h263_decode_frame(AVCodecContext *avctx,
  73. void *data, int *data_size,
  74. UINT8 *buf, int buf_size)
  75. {
  76. MpegEncContext *s = avctx->priv_data;
  77. int ret;
  78. AVPicture *pict = data;
  79. #ifdef DEBUG
  80. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  81. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  82. #endif
  83. /* no supplementary picture */
  84. if (buf_size == 0) {
  85. *data_size = 0;
  86. return 0;
  87. }
  88. init_get_bits(&s->gb, buf, buf_size);
  89. /* let's go :-) */
  90. if (s->h263_msmpeg4) {
  91. ret = msmpeg4_decode_picture_header(s);
  92. } else if (s->h263_pred) {
  93. ret = mpeg4_decode_picture_header(s);
  94. } else if (s->h263_intel) {
  95. ret = intel_h263_decode_picture_header(s);
  96. } else {
  97. ret = h263_decode_picture_header(s);
  98. /* After H263 header decode we have the height, width, */
  99. /* and other parameters. So then we could init the picture */
  100. if (s->width != avctx->width || s->height != avctx->height) {
  101. avctx->width = s->width;
  102. avctx->height = s->height;
  103. /* FIXME: By the way H263 decoder is evolving it should have */
  104. /* an H263EncContext */
  105. if (MPV_common_init(s) < 0)
  106. return -1;
  107. }
  108. }
  109. if (ret < 0)
  110. return -1;
  111. MPV_frame_start(s);
  112. #ifdef DEBUG
  113. printf("qscale=%d\n", s->qscale);
  114. #endif
  115. /* decode each macroblock */
  116. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  117. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  118. #ifdef DEBUG
  119. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  120. #endif
  121. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  122. /* DCT & quantize */
  123. if (s->h263_msmpeg4) {
  124. msmpeg4_dc_scale(s);
  125. } else if (s->h263_pred) {
  126. h263_dc_scale(s);
  127. } else {
  128. /* default quantization values */
  129. s->y_dc_scale = 8;
  130. s->c_dc_scale = 8;
  131. }
  132. memset(s->block, 0, sizeof(s->block));
  133. s->mv_dir = MV_DIR_FORWARD;
  134. s->mv_type = MV_TYPE_16X16;
  135. if (s->h263_msmpeg4) {
  136. if (msmpeg4_decode_mb(s, s->block) < 0)
  137. return -1;
  138. } else {
  139. if (h263_decode_mb(s, s->block) < 0)
  140. return -1;
  141. }
  142. MPV_decode_mb(s, s->block);
  143. }
  144. if (avctx->draw_horiz_band) {
  145. UINT8 *src_ptr[3];
  146. int y, h, offset;
  147. y = s->mb_y * 16;
  148. h = s->height - y;
  149. if (h > 16)
  150. h = 16;
  151. offset = y * s->linesize;
  152. src_ptr[0] = s->current_picture[0] + offset;
  153. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  154. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  155. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  156. y, s->width, h);
  157. }
  158. }
  159. MPV_frame_end(s);
  160. pict->data[0] = s->current_picture[0];
  161. pict->data[1] = s->current_picture[1];
  162. pict->data[2] = s->current_picture[2];
  163. pict->linesize[0] = s->linesize;
  164. pict->linesize[1] = s->linesize / 2;
  165. pict->linesize[2] = s->linesize / 2;
  166. avctx->quality = s->qscale;
  167. *data_size = sizeof(AVPicture);
  168. return buf_size;
  169. }
  170. AVCodec mpeg4_decoder = {
  171. "mpeg4",
  172. CODEC_TYPE_VIDEO,
  173. CODEC_ID_MPEG4,
  174. sizeof(MpegEncContext),
  175. h263_decode_init,
  176. NULL,
  177. h263_decode_end,
  178. h263_decode_frame,
  179. CODEC_CAP_DRAW_HORIZ_BAND,
  180. };
  181. AVCodec h263_decoder = {
  182. "h263",
  183. CODEC_TYPE_VIDEO,
  184. CODEC_ID_H263,
  185. sizeof(MpegEncContext),
  186. h263_decode_init,
  187. NULL,
  188. h263_decode_end,
  189. h263_decode_frame,
  190. CODEC_CAP_DRAW_HORIZ_BAND,
  191. };
  192. AVCodec msmpeg4_decoder = {
  193. "msmpeg4",
  194. CODEC_TYPE_VIDEO,
  195. CODEC_ID_MSMPEG4,
  196. sizeof(MpegEncContext),
  197. h263_decode_init,
  198. NULL,
  199. h263_decode_end,
  200. h263_decode_frame,
  201. CODEC_CAP_DRAW_HORIZ_BAND,
  202. };
  203. AVCodec h263i_decoder = {
  204. "h263i",
  205. CODEC_TYPE_VIDEO,
  206. CODEC_ID_H263I,
  207. sizeof(MpegEncContext),
  208. h263_decode_init,
  209. NULL,
  210. h263_decode_end,
  211. h263_decode_frame,
  212. CODEC_CAP_DRAW_HORIZ_BAND,
  213. };