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.

315 lines
9.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->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. s->has_b_frames = 1;
  44. break;
  45. case CODEC_ID_MSMPEG4:
  46. s->h263_msmpeg4 = 1;
  47. s->h263_pred = 1;
  48. break;
  49. case CODEC_ID_H263I:
  50. s->h263_intel = 1;
  51. break;
  52. default:
  53. return -1;
  54. }
  55. /* for h263, we allocate the images after having read the header */
  56. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  57. if (MPV_common_init(s) < 0)
  58. return -1;
  59. /* XXX: suppress this matrix init, only needed because using mpeg1
  60. dequantize in mmx case */
  61. for(i=0;i<64;i++)
  62. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  63. if (s->h263_msmpeg4)
  64. msmpeg4_decode_init_vlc(s);
  65. else
  66. h263_decode_init_vlc(s);
  67. return 0;
  68. }
  69. static int h263_decode_end(AVCodecContext *avctx)
  70. {
  71. MpegEncContext *s = avctx->priv_data;
  72. MPV_common_end(s);
  73. return 0;
  74. }
  75. static int h263_decode_frame(AVCodecContext *avctx,
  76. void *data, int *data_size,
  77. UINT8 *buf, int buf_size)
  78. {
  79. MpegEncContext *s = avctx->priv_data;
  80. int ret;
  81. AVPicture *pict = data;
  82. #ifdef DEBUG
  83. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  84. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  85. #endif
  86. /* no supplementary picture */
  87. if (buf_size == 0) {
  88. *data_size = 0;
  89. return 0;
  90. }
  91. init_get_bits(&s->gb, buf, buf_size);
  92. /* let's go :-) */
  93. if (s->h263_msmpeg4) {
  94. ret = msmpeg4_decode_picture_header(s);
  95. } else if (s->h263_pred) {
  96. ret = mpeg4_decode_picture_header(s);
  97. } else if (s->h263_intel) {
  98. ret = intel_h263_decode_picture_header(s);
  99. } else {
  100. ret = h263_decode_picture_header(s);
  101. }
  102. /* After H263 & mpeg4 header decode we have the height, width,*/
  103. /* and other parameters. So then we could init the picture */
  104. /* FIXME: By the way H263 decoder is evolving it should have */
  105. /* an H263EncContext */
  106. if (!s->context_initialized) {
  107. avctx->width = s->width;
  108. avctx->height = s->height;
  109. avctx->aspect_ratio_info= s->aspect_ratio_info;
  110. if (MPV_common_init(s) < 0)
  111. return -1;
  112. } else if (s->width != avctx->width || s->height != avctx->height) {
  113. /* H.263 could change picture size any time */
  114. MPV_common_end(s);
  115. if (MPV_common_init(s) < 0)
  116. return -1;
  117. }
  118. if (ret < 0)
  119. return -1;
  120. MPV_frame_start(s);
  121. #ifdef DEBUG
  122. printf("qscale=%d\n", s->qscale);
  123. #endif
  124. /* decode each macroblock */
  125. s->block_wrap[0]=
  126. s->block_wrap[1]=
  127. s->block_wrap[2]=
  128. s->block_wrap[3]= s->mb_width*2 + 2;
  129. s->block_wrap[4]=
  130. s->block_wrap[5]= s->mb_width + 2;
  131. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  132. /* Check for GOB headers on H.263 */
  133. /* FIXME: In the future H.263+ will have intra prediction */
  134. /* and we are gonna need another way to detect MPEG4 */
  135. if (s->mb_y && !s->h263_pred) {
  136. s->first_gob_line = h263_decode_gob_header(s);
  137. }
  138. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  139. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  140. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  141. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  142. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  143. s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  144. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  145. s->block_index[0]+=2;
  146. s->block_index[1]+=2;
  147. s->block_index[2]+=2;
  148. s->block_index[3]+=2;
  149. s->block_index[4]++;
  150. s->block_index[5]++;
  151. #ifdef DEBUG
  152. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  153. #endif
  154. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  155. /* DCT & quantize */
  156. if (s->h263_msmpeg4) {
  157. msmpeg4_dc_scale(s);
  158. } else if (s->h263_pred) {
  159. h263_dc_scale(s);
  160. } else {
  161. /* default quantization values */
  162. s->y_dc_scale = 8;
  163. s->c_dc_scale = 8;
  164. }
  165. #ifdef HAVE_MMX
  166. if (mm_flags & MM_MMX) {
  167. asm volatile(
  168. "pxor %%mm7, %%mm7 \n\t"
  169. "movl $-128*6, %%eax \n\t"
  170. "1: \n\t"
  171. "movq %%mm7, (%0, %%eax) \n\t"
  172. "movq %%mm7, 8(%0, %%eax) \n\t"
  173. "movq %%mm7, 16(%0, %%eax) \n\t"
  174. "movq %%mm7, 24(%0, %%eax) \n\t"
  175. "addl $32, %%eax \n\t"
  176. " js 1b \n\t"
  177. : : "r" (((int)s->block)+128*6)
  178. : "%eax"
  179. );
  180. }else{
  181. memset(s->block, 0, sizeof(s->block));
  182. }
  183. #else
  184. memset(s->block, 0, sizeof(s->block));
  185. #endif
  186. s->mv_dir = MV_DIR_FORWARD;
  187. s->mv_type = MV_TYPE_16X16;
  188. if (s->h263_msmpeg4) {
  189. if (msmpeg4_decode_mb(s, s->block) < 0) {
  190. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  191. return -1;
  192. }
  193. } else {
  194. if (h263_decode_mb(s, s->block) < 0) {
  195. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  196. return -1;
  197. }
  198. }
  199. MPV_decode_mb(s, s->block);
  200. }
  201. if (avctx->draw_horiz_band) {
  202. UINT8 *src_ptr[3];
  203. int y, h, offset;
  204. y = s->mb_y * 16;
  205. h = s->height - y;
  206. if (h > 16)
  207. h = 16;
  208. offset = y * s->linesize;
  209. src_ptr[0] = s->current_picture[0] + offset;
  210. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  211. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  212. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  213. y, s->width, h);
  214. }
  215. }
  216. if (s->h263_msmpeg4 && s->pict_type==I_TYPE)
  217. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  218. MPV_frame_end(s);
  219. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  220. pict->data[0] = s->current_picture[0];
  221. pict->data[1] = s->current_picture[1];
  222. pict->data[2] = s->current_picture[2];
  223. } else {
  224. pict->data[0] = s->last_picture[0];
  225. pict->data[1] = s->last_picture[1];
  226. pict->data[2] = s->last_picture[2];
  227. }
  228. pict->linesize[0] = s->linesize;
  229. pict->linesize[1] = s->linesize / 2;
  230. pict->linesize[2] = s->linesize / 2;
  231. avctx->quality = s->qscale;
  232. /* Return the Picture timestamp as the frame number */
  233. /* we substract 1 because it is added on utils.c */
  234. avctx->frame_number = s->picture_number - 1;
  235. *data_size = sizeof(AVPicture);
  236. return buf_size;
  237. }
  238. AVCodec mpeg4_decoder = {
  239. "mpeg4",
  240. CODEC_TYPE_VIDEO,
  241. CODEC_ID_MPEG4,
  242. sizeof(MpegEncContext),
  243. h263_decode_init,
  244. NULL,
  245. h263_decode_end,
  246. h263_decode_frame,
  247. CODEC_CAP_DRAW_HORIZ_BAND,
  248. };
  249. AVCodec h263_decoder = {
  250. "h263",
  251. CODEC_TYPE_VIDEO,
  252. CODEC_ID_H263,
  253. sizeof(MpegEncContext),
  254. h263_decode_init,
  255. NULL,
  256. h263_decode_end,
  257. h263_decode_frame,
  258. CODEC_CAP_DRAW_HORIZ_BAND,
  259. };
  260. AVCodec msmpeg4_decoder = {
  261. "msmpeg4",
  262. CODEC_TYPE_VIDEO,
  263. CODEC_ID_MSMPEG4,
  264. sizeof(MpegEncContext),
  265. h263_decode_init,
  266. NULL,
  267. h263_decode_end,
  268. h263_decode_frame,
  269. CODEC_CAP_DRAW_HORIZ_BAND,
  270. };
  271. AVCodec h263i_decoder = {
  272. "h263i",
  273. CODEC_TYPE_VIDEO,
  274. CODEC_ID_H263I,
  275. sizeof(MpegEncContext),
  276. h263_decode_init,
  277. NULL,
  278. h263_decode_end,
  279. h263_decode_frame,
  280. CODEC_CAP_DRAW_HORIZ_BAND,
  281. };