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.

206 lines
4.9KB

  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. DCTELEM block[6][64];
  72. AVPicture *pict = data;
  73. #ifdef DEBUG
  74. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  75. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  76. #endif
  77. /* no supplementary picture */
  78. if (buf_size == 0) {
  79. *data_size = 0;
  80. return 0;
  81. }
  82. init_get_bits(&s->gb, buf, buf_size);
  83. /* let's go :-) */
  84. if (s->h263_msmpeg4) {
  85. ret = msmpeg4_decode_picture_header(s);
  86. } else if (s->h263_pred) {
  87. ret = mpeg4_decode_picture_header(s);
  88. } else if (s->h263_intel) {
  89. ret = intel_h263_decode_picture_header(s);
  90. } else {
  91. ret = h263_decode_picture_header(s);
  92. }
  93. if (ret < 0)
  94. return -1;
  95. MPV_frame_start(s);
  96. #ifdef DEBUG
  97. printf("qscale=%d\n", s->qscale);
  98. #endif
  99. /* decode each macroblock */
  100. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  101. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  102. #ifdef DEBUG
  103. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  104. #endif
  105. /* DCT & quantize */
  106. if (s->h263_msmpeg4) {
  107. msmpeg4_dc_scale(s);
  108. } else if (s->h263_pred) {
  109. h263_dc_scale(s);
  110. } else {
  111. /* default quantization values */
  112. s->y_dc_scale = 8;
  113. s->c_dc_scale = 8;
  114. }
  115. memset(block, 0, sizeof(block));
  116. s->mv_dir = MV_DIR_FORWARD;
  117. s->mv_type = MV_TYPE_16X16;
  118. if (s->h263_msmpeg4) {
  119. if (msmpeg4_decode_mb(s, block) < 0)
  120. return -1;
  121. } else {
  122. if (h263_decode_mb(s, block) < 0)
  123. return -1;
  124. }
  125. MPV_decode_mb(s, block);
  126. }
  127. }
  128. MPV_frame_end(s);
  129. pict->data[0] = s->current_picture[0];
  130. pict->data[1] = s->current_picture[1];
  131. pict->data[2] = s->current_picture[2];
  132. pict->linesize[0] = s->linesize;
  133. pict->linesize[1] = s->linesize / 2;
  134. pict->linesize[2] = s->linesize / 2;
  135. avctx->quality = s->qscale;
  136. *data_size = sizeof(AVPicture);
  137. return buf_size;
  138. }
  139. AVCodec opendivx_decoder = {
  140. "opendivx",
  141. CODEC_TYPE_VIDEO,
  142. CODEC_ID_OPENDIVX,
  143. sizeof(MpegEncContext),
  144. h263_decode_init,
  145. NULL,
  146. h263_decode_end,
  147. h263_decode_frame,
  148. };
  149. AVCodec h263_decoder = {
  150. "h263",
  151. CODEC_TYPE_VIDEO,
  152. CODEC_ID_H263,
  153. sizeof(MpegEncContext),
  154. h263_decode_init,
  155. NULL,
  156. h263_decode_end,
  157. h263_decode_frame,
  158. };
  159. AVCodec msmpeg4_decoder = {
  160. "msmpeg4",
  161. CODEC_TYPE_VIDEO,
  162. CODEC_ID_MSMPEG4,
  163. sizeof(MpegEncContext),
  164. h263_decode_init,
  165. NULL,
  166. h263_decode_end,
  167. h263_decode_frame,
  168. };
  169. AVCodec h263i_decoder = {
  170. "h263i",
  171. CODEC_TYPE_VIDEO,
  172. CODEC_ID_H263I,
  173. sizeof(MpegEncContext),
  174. h263_decode_init,
  175. NULL,
  176. h263_decode_end,
  177. h263_decode_frame,
  178. };