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.

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