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.

211 lines
5.1KB

  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->out_format = FMT_H263;
  31. s->width = avctx->width;
  32. s->height = avctx->height;
  33. /* select sub codec */
  34. switch(avctx->codec->id) {
  35. case CODEC_ID_H263:
  36. break;
  37. case CODEC_ID_OPENDIVX:
  38. s->time_increment_bits = 4; /* default value for broken headers */
  39. s->h263_pred = 1;
  40. break;
  41. case CODEC_ID_MSMPEG4:
  42. s->h263_msmpeg4 = 1;
  43. s->h263_pred = 1;
  44. break;
  45. case CODEC_ID_H263I:
  46. s->h263_intel = 1;
  47. break;
  48. default:
  49. return -1;
  50. }
  51. /* for h263, we allocate the images after having read the header */
  52. if (MPV_common_init(s) < 0)
  53. return -1;
  54. /* XXX: suppress this matrix init, only needed because using mpeg1
  55. dequantize in mmx case */
  56. for(i=0;i<64;i++)
  57. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  58. if (s->h263_msmpeg4)
  59. msmpeg4_decode_init_vlc(s);
  60. else
  61. h263_decode_init_vlc(s);
  62. return 0;
  63. }
  64. static int h263_decode_end(AVCodecContext *avctx)
  65. {
  66. MpegEncContext *s = avctx->priv_data;
  67. MPV_common_end(s);
  68. return 0;
  69. }
  70. static int h263_decode_frame(AVCodecContext *avctx,
  71. void *data, int *data_size,
  72. UINT8 *buf, int buf_size)
  73. {
  74. MpegEncContext *s = avctx->priv_data;
  75. int ret;
  76. AVPicture *pict = data;
  77. #ifdef DEBUG
  78. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  79. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  80. #endif
  81. /* no supplementary picture */
  82. if (buf_size == 0) {
  83. *data_size = 0;
  84. return 0;
  85. }
  86. init_get_bits(&s->gb, buf, buf_size);
  87. /* let's go :-) */
  88. if (s->h263_msmpeg4) {
  89. ret = msmpeg4_decode_picture_header(s);
  90. } else if (s->h263_pred) {
  91. ret = mpeg4_decode_picture_header(s);
  92. } else if (s->h263_intel) {
  93. ret = intel_h263_decode_picture_header(s);
  94. } else {
  95. ret = h263_decode_picture_header(s);
  96. }
  97. if (ret < 0)
  98. return -1;
  99. MPV_frame_start(s);
  100. #ifdef DEBUG
  101. printf("qscale=%d\n", s->qscale);
  102. #endif
  103. /* decode each macroblock */
  104. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  105. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  106. #ifdef DEBUG
  107. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  108. #endif
  109. /* DCT & quantize */
  110. if (s->h263_msmpeg4) {
  111. msmpeg4_dc_scale(s);
  112. } else if (s->h263_pred) {
  113. h263_dc_scale(s);
  114. } else {
  115. /* default quantization values */
  116. s->y_dc_scale = 8;
  117. s->c_dc_scale = 8;
  118. }
  119. memset(s->block, 0, sizeof(s->block));
  120. s->mv_dir = MV_DIR_FORWARD;
  121. s->mv_type = MV_TYPE_16X16;
  122. if (s->h263_msmpeg4) {
  123. if (msmpeg4_decode_mb(s, s->block) < 0)
  124. return -1;
  125. } else {
  126. if (h263_decode_mb(s, s->block) < 0)
  127. return -1;
  128. }
  129. MPV_decode_mb(s, s->block);
  130. }
  131. }
  132. MPV_frame_end(s);
  133. pict->data[0] = s->current_picture[0];
  134. pict->data[1] = s->current_picture[1];
  135. pict->data[2] = s->current_picture[2];
  136. pict->linesize[0] = s->linesize;
  137. pict->linesize[1] = s->linesize / 2;
  138. pict->linesize[2] = s->linesize / 2;
  139. avctx->quality = s->qscale;
  140. *data_size = sizeof(AVPicture);
  141. return buf_size;
  142. }
  143. AVCodec opendivx_decoder = {
  144. "opendivx",
  145. CODEC_TYPE_VIDEO,
  146. CODEC_ID_OPENDIVX,
  147. sizeof(MpegEncContext),
  148. h263_decode_init,
  149. NULL,
  150. h263_decode_end,
  151. h263_decode_frame,
  152. };
  153. AVCodec h263_decoder = {
  154. "h263",
  155. CODEC_TYPE_VIDEO,
  156. CODEC_ID_H263,
  157. sizeof(MpegEncContext),
  158. h263_decode_init,
  159. NULL,
  160. h263_decode_end,
  161. h263_decode_frame,
  162. };
  163. AVCodec msmpeg4_decoder = {
  164. "msmpeg4",
  165. CODEC_TYPE_VIDEO,
  166. CODEC_ID_MSMPEG4,
  167. sizeof(MpegEncContext),
  168. h263_decode_init,
  169. NULL,
  170. h263_decode_end,
  171. h263_decode_frame,
  172. };
  173. AVCodec h263i_decoder = {
  174. "h263i",
  175. CODEC_TYPE_VIDEO,
  176. CODEC_ID_H263I,
  177. sizeof(MpegEncContext),
  178. h263_decode_init,
  179. NULL,
  180. h263_decode_end,
  181. h263_decode_frame,
  182. };