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.

205 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. 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. MPV_frame_start(s);
  95. #ifdef DEBUG
  96. printf("qscale=%d\n", s->qscale);
  97. #endif
  98. /* decode each macroblock */
  99. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  100. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  101. #ifdef DEBUG
  102. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  103. #endif
  104. /* DCT & quantize */
  105. if (s->h263_msmpeg4) {
  106. msmpeg4_dc_scale(s);
  107. } else if (s->h263_pred) {
  108. h263_dc_scale(s);
  109. } else {
  110. /* default quantization values */
  111. s->y_dc_scale = 8;
  112. s->c_dc_scale = 8;
  113. }
  114. memset(s->block, 0, sizeof(s->block));
  115. s->mv_dir = MV_DIR_FORWARD;
  116. s->mv_type = MV_TYPE_16X16;
  117. if (s->h263_msmpeg4) {
  118. if (msmpeg4_decode_mb(s, s->block) < 0)
  119. return -1;
  120. } else {
  121. if (h263_decode_mb(s, s->block) < 0)
  122. return -1;
  123. }
  124. MPV_decode_mb(s, s->block);
  125. }
  126. }
  127. MPV_frame_end(s);
  128. pict->data[0] = s->current_picture[0];
  129. pict->data[1] = s->current_picture[1];
  130. pict->data[2] = s->current_picture[2];
  131. pict->linesize[0] = s->linesize;
  132. pict->linesize[1] = s->linesize / 2;
  133. pict->linesize[2] = s->linesize / 2;
  134. avctx->quality = s->qscale;
  135. *data_size = sizeof(AVPicture);
  136. return buf_size;
  137. }
  138. AVCodec opendivx_decoder = {
  139. "opendivx",
  140. CODEC_TYPE_VIDEO,
  141. CODEC_ID_OPENDIVX,
  142. sizeof(MpegEncContext),
  143. h263_decode_init,
  144. NULL,
  145. h263_decode_end,
  146. h263_decode_frame,
  147. };
  148. AVCodec h263_decoder = {
  149. "h263",
  150. CODEC_TYPE_VIDEO,
  151. CODEC_ID_H263,
  152. sizeof(MpegEncContext),
  153. h263_decode_init,
  154. NULL,
  155. h263_decode_end,
  156. h263_decode_frame,
  157. };
  158. AVCodec msmpeg4_decoder = {
  159. "msmpeg4",
  160. CODEC_TYPE_VIDEO,
  161. CODEC_ID_MSMPEG4,
  162. sizeof(MpegEncContext),
  163. h263_decode_init,
  164. NULL,
  165. h263_decode_end,
  166. h263_decode_frame,
  167. };
  168. AVCodec h263i_decoder = {
  169. "h263i",
  170. CODEC_TYPE_VIDEO,
  171. CODEC_ID_H263I,
  172. sizeof(MpegEncContext),
  173. h263_decode_init,
  174. NULL,
  175. h263_decode_end,
  176. h263_decode_frame,
  177. };