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.

287 lines
7.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. 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. /* FIXME: By the way H263 decoder is evolving it should have */
  103. /* an H263EncContext */
  104. if (!s->context_initialized) {
  105. avctx->width = s->width;
  106. avctx->height = s->height;
  107. if (MPV_common_init(s) < 0)
  108. return -1;
  109. } else if (s->width != avctx->width || s->height != avctx->height) {
  110. /* H.263 could change picture size any time */
  111. MPV_common_end(s);
  112. if (MPV_common_init(s) < 0)
  113. return -1;
  114. }
  115. }
  116. if (ret < 0)
  117. return -1;
  118. MPV_frame_start(s);
  119. #ifdef DEBUG
  120. printf("qscale=%d\n", s->qscale);
  121. #endif
  122. /* decode each macroblock */
  123. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  124. /* Check for GOB headers on H.263 */
  125. /* FIXME: In the future H.263+ will have intra prediction */
  126. /* and we are gonna need another way to detect MPEG4 */
  127. if (s->mb_y && !s->h263_pred) {
  128. s->first_gob_line = h263_decode_gob_header(s);
  129. }
  130. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  131. #ifdef DEBUG
  132. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  133. #endif
  134. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  135. /* DCT & quantize */
  136. if (s->h263_msmpeg4) {
  137. msmpeg4_dc_scale(s);
  138. } else if (s->h263_pred) {
  139. h263_dc_scale(s);
  140. } else {
  141. /* default quantization values */
  142. s->y_dc_scale = 8;
  143. s->c_dc_scale = 8;
  144. }
  145. #ifdef HAVE_MMX
  146. if (mm_flags & MM_MMX) {
  147. asm volatile(
  148. "pxor %%mm7, %%mm7 \n\t"
  149. "movl $-128*6, %%eax \n\t"
  150. "1: \n\t"
  151. "movq %%mm7, (%0, %%eax) \n\t"
  152. "movq %%mm7, 8(%0, %%eax) \n\t"
  153. "movq %%mm7, 16(%0, %%eax) \n\t"
  154. "movq %%mm7, 24(%0, %%eax) \n\t"
  155. "addl $32, %%eax \n\t"
  156. " js 1b \n\t"
  157. : : "r" (((int)s->block)+128*6)
  158. : "%eax"
  159. );
  160. }else{
  161. memset(s->block, 0, sizeof(s->block));
  162. }
  163. #else
  164. memset(s->block, 0, sizeof(s->block));
  165. #endif
  166. s->mv_dir = MV_DIR_FORWARD;
  167. s->mv_type = MV_TYPE_16X16;
  168. if (s->h263_msmpeg4) {
  169. if (msmpeg4_decode_mb(s, s->block) < 0) {
  170. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  171. return -1;
  172. }
  173. } else {
  174. if (h263_decode_mb(s, s->block) < 0) {
  175. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  176. return -1;
  177. }
  178. }
  179. MPV_decode_mb(s, s->block);
  180. }
  181. if (avctx->draw_horiz_band) {
  182. UINT8 *src_ptr[3];
  183. int y, h, offset;
  184. y = s->mb_y * 16;
  185. h = s->height - y;
  186. if (h > 16)
  187. h = 16;
  188. offset = y * s->linesize;
  189. src_ptr[0] = s->current_picture[0] + offset;
  190. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  191. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  192. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  193. y, s->width, h);
  194. }
  195. }
  196. if (s->h263_msmpeg4)
  197. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  198. MPV_frame_end(s);
  199. pict->data[0] = s->current_picture[0];
  200. pict->data[1] = s->current_picture[1];
  201. pict->data[2] = s->current_picture[2];
  202. pict->linesize[0] = s->linesize;
  203. pict->linesize[1] = s->linesize / 2;
  204. pict->linesize[2] = s->linesize / 2;
  205. avctx->quality = s->qscale;
  206. /* Return the Picture timestamp as the frame number */
  207. /* we substract 1 because it is added on utils.c */
  208. avctx->frame_number = s->picture_number - 1;
  209. *data_size = sizeof(AVPicture);
  210. return buf_size;
  211. }
  212. AVCodec mpeg4_decoder = {
  213. "mpeg4",
  214. CODEC_TYPE_VIDEO,
  215. CODEC_ID_MPEG4,
  216. sizeof(MpegEncContext),
  217. h263_decode_init,
  218. NULL,
  219. h263_decode_end,
  220. h263_decode_frame,
  221. CODEC_CAP_DRAW_HORIZ_BAND,
  222. };
  223. AVCodec h263_decoder = {
  224. "h263",
  225. CODEC_TYPE_VIDEO,
  226. CODEC_ID_H263,
  227. sizeof(MpegEncContext),
  228. h263_decode_init,
  229. NULL,
  230. h263_decode_end,
  231. h263_decode_frame,
  232. CODEC_CAP_DRAW_HORIZ_BAND,
  233. };
  234. AVCodec msmpeg4_decoder = {
  235. "msmpeg4",
  236. CODEC_TYPE_VIDEO,
  237. CODEC_ID_MSMPEG4,
  238. sizeof(MpegEncContext),
  239. h263_decode_init,
  240. NULL,
  241. h263_decode_end,
  242. h263_decode_frame,
  243. CODEC_CAP_DRAW_HORIZ_BAND,
  244. };
  245. AVCodec h263i_decoder = {
  246. "h263i",
  247. CODEC_TYPE_VIDEO,
  248. CODEC_ID_H263I,
  249. sizeof(MpegEncContext),
  250. h263_decode_init,
  251. NULL,
  252. h263_decode_end,
  253. h263_decode_frame,
  254. CODEC_CAP_DRAW_HORIZ_BAND,
  255. };