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.

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