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.

369 lines
11KB

  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_MSMPEG4V1:
  46. s->h263_msmpeg4 = 1;
  47. s->h263_pred = 1;
  48. s->msmpeg4_version=1;
  49. break;
  50. case CODEC_ID_MSMPEG4V2:
  51. s->h263_msmpeg4 = 1;
  52. s->h263_pred = 1;
  53. s->msmpeg4_version=2;
  54. break;
  55. case CODEC_ID_MSMPEG4V3:
  56. s->h263_msmpeg4 = 1;
  57. s->h263_pred = 1;
  58. s->msmpeg4_version=3;
  59. break;
  60. case CODEC_ID_WMV1:
  61. s->h263_msmpeg4 = 1;
  62. s->h263_pred = 1;
  63. s->msmpeg4_version=4;
  64. break;
  65. case CODEC_ID_H263I:
  66. s->h263_intel = 1;
  67. break;
  68. default:
  69. return -1;
  70. }
  71. /* for h263, we allocate the images after having read the header */
  72. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  73. if (MPV_common_init(s) < 0)
  74. return -1;
  75. /* XXX: suppress this matrix init, only needed because using mpeg1
  76. dequantize in mmx case */
  77. for(i=0;i<64;i++)
  78. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  79. if (s->h263_msmpeg4)
  80. msmpeg4_decode_init_vlc(s);
  81. else
  82. h263_decode_init_vlc(s);
  83. return 0;
  84. }
  85. static int h263_decode_end(AVCodecContext *avctx)
  86. {
  87. MpegEncContext *s = avctx->priv_data;
  88. MPV_common_end(s);
  89. return 0;
  90. }
  91. static int h263_decode_frame(AVCodecContext *avctx,
  92. void *data, int *data_size,
  93. UINT8 *buf, int buf_size)
  94. {
  95. MpegEncContext *s = avctx->priv_data;
  96. int ret;
  97. AVPicture *pict = data;
  98. #ifdef DEBUG
  99. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  100. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  101. #endif
  102. /* no supplementary picture */
  103. if (buf_size == 0) {
  104. *data_size = 0;
  105. return 0;
  106. }
  107. if(s->bitstream_buffer_size) //divx 5.01+ frame reorder
  108. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
  109. else
  110. init_get_bits(&s->gb, buf, buf_size);
  111. /* let's go :-) */
  112. if (s->h263_msmpeg4) {
  113. ret = msmpeg4_decode_picture_header(s);
  114. } else if (s->h263_pred) {
  115. ret = mpeg4_decode_picture_header(s);
  116. } else if (s->h263_intel) {
  117. ret = intel_h263_decode_picture_header(s);
  118. } else {
  119. ret = h263_decode_picture_header(s);
  120. }
  121. if(ret==FRAME_SKIPED) return 0;
  122. /* After H263 & mpeg4 header decode we have the height, width,*/
  123. /* and other parameters. So then we could init the picture */
  124. /* FIXME: By the way H263 decoder is evolving it should have */
  125. /* an H263EncContext */
  126. if (!s->context_initialized) {
  127. avctx->width = s->width;
  128. avctx->height = s->height;
  129. avctx->aspect_ratio_info= s->aspect_ratio_info;
  130. if (MPV_common_init(s) < 0)
  131. return -1;
  132. } else if (s->width != avctx->width || s->height != avctx->height) {
  133. /* H.263 could change picture size any time */
  134. MPV_common_end(s);
  135. if (MPV_common_init(s) < 0)
  136. return -1;
  137. }
  138. if (ret < 0)
  139. return -1;
  140. MPV_frame_start(s);
  141. #ifdef DEBUG
  142. printf("qscale=%d\n", s->qscale);
  143. #endif
  144. /* decode each macroblock */
  145. s->block_wrap[0]=
  146. s->block_wrap[1]=
  147. s->block_wrap[2]=
  148. s->block_wrap[3]= s->mb_width*2 + 2;
  149. s->block_wrap[4]=
  150. s->block_wrap[5]= s->mb_width + 2;
  151. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  152. /* Check for GOB headers on H.263 */
  153. /* FIXME: In the future H.263+ will have intra prediction */
  154. /* and we are gonna need another way to detect MPEG4 */
  155. if (s->mb_y && !s->h263_pred) {
  156. s->first_gob_line = h263_decode_gob_header(s);
  157. }
  158. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  159. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  160. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  161. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  162. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  163. 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);
  164. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  165. s->block_index[0]+=2;
  166. s->block_index[1]+=2;
  167. s->block_index[2]+=2;
  168. s->block_index[3]+=2;
  169. s->block_index[4]++;
  170. s->block_index[5]++;
  171. #ifdef DEBUG
  172. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  173. #endif
  174. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  175. /* DCT & quantize */
  176. if (s->h263_msmpeg4) {
  177. msmpeg4_dc_scale(s);
  178. } else if (s->h263_pred) {
  179. h263_dc_scale(s);
  180. } else {
  181. /* default quantization values */
  182. s->y_dc_scale = 8;
  183. s->c_dc_scale = 8;
  184. }
  185. clear_blocks(s->block[0]);
  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. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  210. src_ptr[0] = s->current_picture[0] + offset;
  211. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  212. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  213. } else {
  214. src_ptr[0] = s->last_picture[0] + offset;
  215. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  216. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  217. }
  218. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  219. y, s->width, h);
  220. }
  221. }
  222. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  223. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  224. /* divx 5.01+ bistream reorder stuff */
  225. if(s->h263_pred && s->bitstream_buffer_size==0){
  226. int current_pos= get_bits_count(&s->gb)/8;
  227. if( buf_size - current_pos > 5
  228. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  229. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  230. s->bitstream_buffer_size= buf_size - current_pos;
  231. }
  232. }else
  233. s->bitstream_buffer_size=0;
  234. MPV_frame_end(s);
  235. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  236. pict->data[0] = s->current_picture[0];
  237. pict->data[1] = s->current_picture[1];
  238. pict->data[2] = s->current_picture[2];
  239. } else {
  240. pict->data[0] = s->last_picture[0];
  241. pict->data[1] = s->last_picture[1];
  242. pict->data[2] = s->last_picture[2];
  243. }
  244. pict->linesize[0] = s->linesize;
  245. pict->linesize[1] = s->linesize / 2;
  246. pict->linesize[2] = s->linesize / 2;
  247. avctx->quality = s->qscale;
  248. /* Return the Picture timestamp as the frame number */
  249. /* we substract 1 because it is added on utils.c */
  250. avctx->frame_number = s->picture_number - 1;
  251. *data_size = sizeof(AVPicture);
  252. return buf_size;
  253. }
  254. AVCodec mpeg4_decoder = {
  255. "mpeg4",
  256. CODEC_TYPE_VIDEO,
  257. CODEC_ID_MPEG4,
  258. sizeof(MpegEncContext),
  259. h263_decode_init,
  260. NULL,
  261. h263_decode_end,
  262. h263_decode_frame,
  263. CODEC_CAP_DRAW_HORIZ_BAND,
  264. };
  265. AVCodec h263_decoder = {
  266. "h263",
  267. CODEC_TYPE_VIDEO,
  268. CODEC_ID_H263,
  269. sizeof(MpegEncContext),
  270. h263_decode_init,
  271. NULL,
  272. h263_decode_end,
  273. h263_decode_frame,
  274. CODEC_CAP_DRAW_HORIZ_BAND,
  275. };
  276. AVCodec msmpeg4v1_decoder = {
  277. "msmpeg4v1",
  278. CODEC_TYPE_VIDEO,
  279. CODEC_ID_MSMPEG4V1,
  280. sizeof(MpegEncContext),
  281. h263_decode_init,
  282. NULL,
  283. h263_decode_end,
  284. h263_decode_frame,
  285. CODEC_CAP_DRAW_HORIZ_BAND,
  286. };
  287. AVCodec msmpeg4v2_decoder = {
  288. "msmpeg4v2",
  289. CODEC_TYPE_VIDEO,
  290. CODEC_ID_MSMPEG4V2,
  291. sizeof(MpegEncContext),
  292. h263_decode_init,
  293. NULL,
  294. h263_decode_end,
  295. h263_decode_frame,
  296. CODEC_CAP_DRAW_HORIZ_BAND,
  297. };
  298. AVCodec msmpeg4v3_decoder = {
  299. "msmpeg4",
  300. CODEC_TYPE_VIDEO,
  301. CODEC_ID_MSMPEG4V3,
  302. sizeof(MpegEncContext),
  303. h263_decode_init,
  304. NULL,
  305. h263_decode_end,
  306. h263_decode_frame,
  307. CODEC_CAP_DRAW_HORIZ_BAND,
  308. };
  309. AVCodec wmv1_decoder = {
  310. "wmv1",
  311. CODEC_TYPE_VIDEO,
  312. CODEC_ID_WMV1,
  313. sizeof(MpegEncContext),
  314. h263_decode_init,
  315. NULL,
  316. h263_decode_end,
  317. h263_decode_frame,
  318. CODEC_CAP_DRAW_HORIZ_BAND,
  319. };
  320. AVCodec h263i_decoder = {
  321. "h263i",
  322. CODEC_TYPE_VIDEO,
  323. CODEC_ID_H263I,
  324. sizeof(MpegEncContext),
  325. h263_decode_init,
  326. NULL,
  327. h263_decode_end,
  328. h263_decode_frame,
  329. CODEC_CAP_DRAW_HORIZ_BAND,
  330. };