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.

376 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. s->codec_id= avctx->codec->id;
  72. avctx->mbskip_table= s->mbskip_table;
  73. /* for h263, we allocate the images after having read the header */
  74. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  75. if (MPV_common_init(s) < 0)
  76. return -1;
  77. if (s->h263_msmpeg4)
  78. msmpeg4_decode_init_vlc(s);
  79. else
  80. h263_decode_init_vlc(s);
  81. return 0;
  82. }
  83. static int h263_decode_end(AVCodecContext *avctx)
  84. {
  85. MpegEncContext *s = avctx->priv_data;
  86. MPV_common_end(s);
  87. return 0;
  88. }
  89. static int h263_decode_frame(AVCodecContext *avctx,
  90. void *data, int *data_size,
  91. UINT8 *buf, int buf_size)
  92. {
  93. MpegEncContext *s = avctx->priv_data;
  94. int ret;
  95. AVPicture *pict = data;
  96. #ifdef DEBUG
  97. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  98. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  99. #endif
  100. s->hurry_up= avctx->hurry_up;
  101. /* no supplementary picture */
  102. if (buf_size == 0) {
  103. *data_size = 0;
  104. return 0;
  105. }
  106. if(s->bitstream_buffer_size) //divx 5.01+ frame reorder
  107. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
  108. else
  109. init_get_bits(&s->gb, buf, buf_size);
  110. /* let's go :-) */
  111. if (s->h263_msmpeg4) {
  112. ret = msmpeg4_decode_picture_header(s);
  113. } else if (s->h263_pred) {
  114. ret = mpeg4_decode_picture_header(s);
  115. s->has_b_frames= !s->low_delay;
  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. /* After H263 & mpeg4 header decode we have the height, width,*/
  122. /* and other parameters. So then we could init the picture */
  123. /* FIXME: By the way H263 decoder is evolving it should have */
  124. /* an H263EncContext */
  125. if (!s->context_initialized) {
  126. avctx->width = s->width;
  127. avctx->height = s->height;
  128. avctx->aspect_ratio_info= s->aspect_ratio_info;
  129. if (MPV_common_init(s) < 0)
  130. return -1;
  131. } else if (s->width != avctx->width || s->height != avctx->height) {
  132. /* H.263 could change picture size any time */
  133. MPV_common_end(s);
  134. if (MPV_common_init(s) < 0)
  135. return -1;
  136. }
  137. if(ret==FRAME_SKIPED) return 0;
  138. if (ret < 0)
  139. return -1;
  140. /* skip b frames if we dont have reference frames */
  141. if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
  142. /* skip b frames if we are in a hurry */
  143. if(s->hurry_up && s->pict_type==B_TYPE) return 0;
  144. MPV_frame_start(s);
  145. #ifdef DEBUG
  146. printf("qscale=%d\n", s->qscale);
  147. #endif
  148. /* decode each macroblock */
  149. s->block_wrap[0]=
  150. s->block_wrap[1]=
  151. s->block_wrap[2]=
  152. s->block_wrap[3]= s->mb_width*2 + 2;
  153. s->block_wrap[4]=
  154. s->block_wrap[5]= s->mb_width + 2;
  155. for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  156. /* Check for GOB headers on H.263 */
  157. /* FIXME: In the future H.263+ will have intra prediction */
  158. /* and we are gonna need another way to detect MPEG4 */
  159. if (s->mb_y && !s->h263_pred) {
  160. s->first_gob_line = h263_decode_gob_header(s);
  161. }
  162. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
  163. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
  164. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
  165. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
  166. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  167. 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);
  168. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  169. s->block_index[0]+=2;
  170. s->block_index[1]+=2;
  171. s->block_index[2]+=2;
  172. s->block_index[3]+=2;
  173. s->block_index[4]++;
  174. s->block_index[5]++;
  175. #ifdef DEBUG
  176. printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  177. #endif
  178. //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
  179. /* DCT & quantize */
  180. if (s->h263_pred && s->msmpeg4_version!=2) {
  181. h263_dc_scale(s);
  182. } else {
  183. /* default quantization values */
  184. s->y_dc_scale = 8;
  185. s->c_dc_scale = 8;
  186. }
  187. clear_blocks(s->block[0]);
  188. s->mv_dir = MV_DIR_FORWARD;
  189. s->mv_type = MV_TYPE_16X16;
  190. if (s->h263_msmpeg4) {
  191. if (msmpeg4_decode_mb(s, s->block) < 0) {
  192. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  193. return -1;
  194. }
  195. } else {
  196. if (h263_decode_mb(s, s->block) < 0) {
  197. fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
  198. return -1;
  199. }
  200. }
  201. MPV_decode_mb(s, s->block);
  202. }
  203. if ( avctx->draw_horiz_band
  204. && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
  205. UINT8 *src_ptr[3];
  206. int y, h, offset;
  207. y = s->mb_y * 16;
  208. h = s->height - y;
  209. if (h > 16)
  210. h = 16;
  211. offset = y * s->linesize;
  212. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  213. src_ptr[0] = s->current_picture[0] + offset;
  214. src_ptr[1] = s->current_picture[1] + (offset >> 2);
  215. src_ptr[2] = s->current_picture[2] + (offset >> 2);
  216. } else {
  217. src_ptr[0] = s->last_picture[0] + offset;
  218. src_ptr[1] = s->last_picture[1] + (offset >> 2);
  219. src_ptr[2] = s->last_picture[2] + (offset >> 2);
  220. }
  221. avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
  222. y, s->width, h);
  223. }
  224. }
  225. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  226. if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
  227. /* divx 5.01+ bistream reorder stuff */
  228. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){
  229. int current_pos= get_bits_count(&s->gb)/8;
  230. if( buf_size - current_pos > 5
  231. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  232. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  233. s->bitstream_buffer_size= buf_size - current_pos;
  234. }
  235. }else
  236. s->bitstream_buffer_size=0;
  237. MPV_frame_end(s);
  238. if(s->pict_type==B_TYPE || (!s->has_b_frames)){
  239. pict->data[0] = s->current_picture[0];
  240. pict->data[1] = s->current_picture[1];
  241. pict->data[2] = s->current_picture[2];
  242. } else {
  243. pict->data[0] = s->last_picture[0];
  244. pict->data[1] = s->last_picture[1];
  245. pict->data[2] = s->last_picture[2];
  246. }
  247. pict->linesize[0] = s->linesize;
  248. pict->linesize[1] = s->linesize / 2;
  249. pict->linesize[2] = s->linesize / 2;
  250. avctx->quality = s->qscale;
  251. /* Return the Picture timestamp as the frame number */
  252. /* we substract 1 because it is added on utils.c */
  253. avctx->frame_number = s->picture_number - 1;
  254. /* dont output the last pic after seeking
  255. note we allready added +1 for the current pix in MPV_frame_end(s) */
  256. if(s->num_available_buffers>=2 || (!s->has_b_frames))
  257. *data_size = sizeof(AVPicture);
  258. return buf_size;
  259. }
  260. AVCodec mpeg4_decoder = {
  261. "mpeg4",
  262. CODEC_TYPE_VIDEO,
  263. CODEC_ID_MPEG4,
  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 h263_decoder = {
  272. "h263",
  273. CODEC_TYPE_VIDEO,
  274. CODEC_ID_H263,
  275. sizeof(MpegEncContext),
  276. h263_decode_init,
  277. NULL,
  278. h263_decode_end,
  279. h263_decode_frame,
  280. CODEC_CAP_DRAW_HORIZ_BAND,
  281. };
  282. AVCodec msmpeg4v1_decoder = {
  283. "msmpeg4v1",
  284. CODEC_TYPE_VIDEO,
  285. CODEC_ID_MSMPEG4V1,
  286. sizeof(MpegEncContext),
  287. h263_decode_init,
  288. NULL,
  289. h263_decode_end,
  290. h263_decode_frame,
  291. CODEC_CAP_DRAW_HORIZ_BAND,
  292. };
  293. AVCodec msmpeg4v2_decoder = {
  294. "msmpeg4v2",
  295. CODEC_TYPE_VIDEO,
  296. CODEC_ID_MSMPEG4V2,
  297. sizeof(MpegEncContext),
  298. h263_decode_init,
  299. NULL,
  300. h263_decode_end,
  301. h263_decode_frame,
  302. CODEC_CAP_DRAW_HORIZ_BAND,
  303. };
  304. AVCodec msmpeg4v3_decoder = {
  305. "msmpeg4",
  306. CODEC_TYPE_VIDEO,
  307. CODEC_ID_MSMPEG4V3,
  308. sizeof(MpegEncContext),
  309. h263_decode_init,
  310. NULL,
  311. h263_decode_end,
  312. h263_decode_frame,
  313. CODEC_CAP_DRAW_HORIZ_BAND,
  314. };
  315. AVCodec wmv1_decoder = {
  316. "wmv1",
  317. CODEC_TYPE_VIDEO,
  318. CODEC_ID_WMV1,
  319. sizeof(MpegEncContext),
  320. h263_decode_init,
  321. NULL,
  322. h263_decode_end,
  323. h263_decode_frame,
  324. CODEC_CAP_DRAW_HORIZ_BAND,
  325. };
  326. AVCodec h263i_decoder = {
  327. "h263i",
  328. CODEC_TYPE_VIDEO,
  329. CODEC_ID_H263I,
  330. sizeof(MpegEncContext),
  331. h263_decode_init,
  332. NULL,
  333. h263_decode_end,
  334. h263_decode_frame,
  335. CODEC_CAP_DRAW_HORIZ_BAND,
  336. };