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.

370 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; //default, might be overriden in the vol header during header parsing
  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. s->has_b_frames= !s->low_delay;
  117. } else if (s->h263_intel) {
  118. ret = intel_h263_decode_picture_header(s);
  119. } else {
  120. ret = h263_decode_picture_header(s);
  121. }
  122. if(ret==FRAME_SKIPED) return 0;
  123. /* After H263 & mpeg4 header decode we have the height, width,*/
  124. /* and other parameters. So then we could init the picture */
  125. /* FIXME: By the way H263 decoder is evolving it should have */
  126. /* an H263EncContext */
  127. if (!s->context_initialized) {
  128. avctx->width = s->width;
  129. avctx->height = s->height;
  130. avctx->aspect_ratio_info= s->aspect_ratio_info;
  131. if (MPV_common_init(s) < 0)
  132. return -1;
  133. } else if (s->width != avctx->width || s->height != avctx->height) {
  134. /* H.263 could change picture size any time */
  135. MPV_common_end(s);
  136. if (MPV_common_init(s) < 0)
  137. return -1;
  138. }
  139. if (ret < 0)
  140. return -1;
  141. MPV_frame_start(s);
  142. #ifdef DEBUG
  143. printf("qscale=%d\n", s->qscale);
  144. #endif
  145. /* decode each macroblock */
  146. s->block_wrap[0]=
  147. s->block_wrap[1]=
  148. s->block_wrap[2]=
  149. s->block_wrap[3]= s->mb_width*2 + 2;
  150. s->block_wrap[4]=
  151. s->block_wrap[5]= s->mb_width + 2;
  152. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  153. /* Check for GOB headers on H.263 */
  154. /* FIXME: In the future H.263+ will have intra prediction */
  155. /* and we are gonna need another way to detect MPEG4 */
  156. if (s->mb_y && !s->h263_pred) {
  157. s->first_gob_line = h263_decode_gob_header(s);
  158. }
  159. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  160. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  161. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  162. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  163. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  164. 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);
  165. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  166. s->block_index[0]+=2;
  167. s->block_index[1]+=2;
  168. s->block_index[2]+=2;
  169. s->block_index[3]+=2;
  170. s->block_index[4]++;
  171. s->block_index[5]++;
  172. #ifdef DEBUG
  173. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  174. #endif
  175. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  176. /* DCT & quantize */
  177. if (s->h263_msmpeg4) {
  178. msmpeg4_dc_scale(s);
  179. } else if (s->h263_pred) {
  180. h263_dc_scale(s);
  181. } else {
  182. /* default quantization values */
  183. s->y_dc_scale = 8;
  184. s->c_dc_scale = 8;
  185. }
  186. clear_blocks(s->block[0]);
  187. s->mv_dir = MV_DIR_FORWARD;
  188. s->mv_type = MV_TYPE_16X16;
  189. if (s->h263_msmpeg4) {
  190. if (msmpeg4_decode_mb(s, s->block) < 0) {
  191. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  192. return -1;
  193. }
  194. } else {
  195. if (h263_decode_mb(s, s->block) < 0) {
  196. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  197. return -1;
  198. }
  199. }
  200. MPV_decode_mb(s, s->block);
  201. }
  202. if (avctx->draw_horiz_band) {
  203. UINT8 *src_ptr[3];
  204. int y, h, offset;
  205. y = s->mb_y * 16;
  206. h = s->height - y;
  207. if (h > 16)
  208. h = 16;
  209. offset = y * s->linesize;
  210. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  211. src_ptr[0] = s->current_picture[0] + offset;
  212. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  213. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  214. } else {
  215. src_ptr[0] = s->last_picture[0] + offset;
  216. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  217. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  218. }
  219. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  220. y, s->width, h);
  221. }
  222. }
  223. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  224. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  225. /* divx 5.01+ bistream reorder stuff */
  226. if(s->h263_pred && s->bitstream_buffer_size==0){
  227. int current_pos= get_bits_count(&s->gb)/8;
  228. if( buf_size - current_pos > 5
  229. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  230. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  231. s->bitstream_buffer_size= buf_size - current_pos;
  232. }
  233. }else
  234. s->bitstream_buffer_size=0;
  235. MPV_frame_end(s);
  236. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  237. pict->data[0] = s->current_picture[0];
  238. pict->data[1] = s->current_picture[1];
  239. pict->data[2] = s->current_picture[2];
  240. } else {
  241. pict->data[0] = s->last_picture[0];
  242. pict->data[1] = s->last_picture[1];
  243. pict->data[2] = s->last_picture[2];
  244. }
  245. pict->linesize[0] = s->linesize;
  246. pict->linesize[1] = s->linesize / 2;
  247. pict->linesize[2] = s->linesize / 2;
  248. avctx->quality = s->qscale;
  249. /* Return the Picture timestamp as the frame number */
  250. /* we substract 1 because it is added on utils.c */
  251. avctx->frame_number = s->picture_number - 1;
  252. *data_size = sizeof(AVPicture);
  253. return buf_size;
  254. }
  255. AVCodec mpeg4_decoder = {
  256. "mpeg4",
  257. CODEC_TYPE_VIDEO,
  258. CODEC_ID_MPEG4,
  259. sizeof(MpegEncContext),
  260. h263_decode_init,
  261. NULL,
  262. h263_decode_end,
  263. h263_decode_frame,
  264. CODEC_CAP_DRAW_HORIZ_BAND,
  265. };
  266. AVCodec h263_decoder = {
  267. "h263",
  268. CODEC_TYPE_VIDEO,
  269. CODEC_ID_H263,
  270. sizeof(MpegEncContext),
  271. h263_decode_init,
  272. NULL,
  273. h263_decode_end,
  274. h263_decode_frame,
  275. CODEC_CAP_DRAW_HORIZ_BAND,
  276. };
  277. AVCodec msmpeg4v1_decoder = {
  278. "msmpeg4v1",
  279. CODEC_TYPE_VIDEO,
  280. CODEC_ID_MSMPEG4V1,
  281. sizeof(MpegEncContext),
  282. h263_decode_init,
  283. NULL,
  284. h263_decode_end,
  285. h263_decode_frame,
  286. CODEC_CAP_DRAW_HORIZ_BAND,
  287. };
  288. AVCodec msmpeg4v2_decoder = {
  289. "msmpeg4v2",
  290. CODEC_TYPE_VIDEO,
  291. CODEC_ID_MSMPEG4V2,
  292. sizeof(MpegEncContext),
  293. h263_decode_init,
  294. NULL,
  295. h263_decode_end,
  296. h263_decode_frame,
  297. CODEC_CAP_DRAW_HORIZ_BAND,
  298. };
  299. AVCodec msmpeg4v3_decoder = {
  300. "msmpeg4",
  301. CODEC_TYPE_VIDEO,
  302. CODEC_ID_MSMPEG4V3,
  303. sizeof(MpegEncContext),
  304. h263_decode_init,
  305. NULL,
  306. h263_decode_end,
  307. h263_decode_frame,
  308. CODEC_CAP_DRAW_HORIZ_BAND,
  309. };
  310. AVCodec wmv1_decoder = {
  311. "wmv1",
  312. CODEC_TYPE_VIDEO,
  313. CODEC_ID_WMV1,
  314. sizeof(MpegEncContext),
  315. h263_decode_init,
  316. NULL,
  317. h263_decode_end,
  318. h263_decode_frame,
  319. CODEC_CAP_DRAW_HORIZ_BAND,
  320. };
  321. AVCodec h263i_decoder = {
  322. "h263i",
  323. CODEC_TYPE_VIDEO,
  324. CODEC_ID_H263I,
  325. sizeof(MpegEncContext),
  326. h263_decode_init,
  327. NULL,
  328. h263_decode_end,
  329. h263_decode_frame,
  330. CODEC_CAP_DRAW_HORIZ_BAND,
  331. };