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.

230 lines
5.8KB

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