Initial revision
[libav.git] / libavcodec / h263dec.c
CommitLineData
de6d9b64
FB
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
26//#define DEBUG
27
28static int h263_decode_init(AVCodecContext *avctx)
29{
30 MpegEncContext *s = avctx->priv_data;
31
32 s->out_format = FMT_H263;
33
34 s->width = avctx->width;
35 s->height = avctx->height;
36
37 /* select sub codec */
38 switch(avctx->codec->id) {
39 case CODEC_ID_H263:
40 break;
41 case CODEC_ID_OPENDIVX:
42 s->time_increment_bits = 4; /* default value for broken headers */
43 s->h263_pred = 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
56 /* for h263, we allocate the images after having read the header */
57 if (MPV_common_init(s) < 0)
58 return -1;
59
60 if (s->h263_msmpeg4)
61 msmpeg4_decode_init_vlc(s);
62 else
63 h263_decode_init_vlc(s);
64
65 return 0;
66}
67
68static int h263_decode_end(AVCodecContext *avctx)
69{
70 MpegEncContext *s = avctx->priv_data;
71
72 MPV_common_end(s);
73 return 0;
74}
75
76static int h263_decode_frame(AVCodecContext *avctx,
77 void *data, int *data_size,
78 UINT8 *buf, int buf_size)
79{
80 MpegEncContext *s = avctx->priv_data;
81 int ret;
82 DCTELEM block[6][64];
83 AVPicture *pict = data;
84
85#ifdef DEBUG
86 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
87 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
88#endif
89
90 /* no supplementary picture */
91 if (buf_size == 0) {
92 *data_size = 0;
93 return 0;
94 }
95
96 init_get_bits(&s->gb, buf, buf_size);
97
98 /* let's go :-) */
99 if (s->h263_msmpeg4) {
100 ret = msmpeg4_decode_picture_header(s);
101 } else if (s->h263_pred) {
102 ret = mpeg4_decode_picture_header(s);
103 } else if (s->h263_intel) {
104 ret = intel_h263_decode_picture_header(s);
105 } else {
106 ret = h263_decode_picture_header(s);
107 }
108 if (ret < 0)
109 return -1;
110
111 MPV_frame_start(s);
112
113#ifdef DEBUG
114 printf("qscale=%d\n", s->qscale);
115#endif
116
117 /* decode each macroblock */
118 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
119 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
120#ifdef DEBUG
121 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
122#endif
123 /* DCT & quantize */
124 if (s->h263_msmpeg4) {
125 msmpeg4_dc_scale(s);
126 } else if (s->h263_pred) {
127 h263_dc_scale(s);
128 } else {
129 /* default quantization values */
130 s->y_dc_scale = 8;
131 s->c_dc_scale = 8;
132 }
133
134 memset(block, 0, sizeof(block));
135 s->mv_dir = MV_DIR_FORWARD;
136 s->mv_type = MV_TYPE_16X16;
137 if (s->h263_msmpeg4) {
138 if (msmpeg4_decode_mb(s, block) < 0)
139 return -1;
140 } else {
141 if (h263_decode_mb(s, block) < 0)
142 return -1;
143 }
144 MPV_decode_mb(s, block);
145 }
146 }
147
148 MPV_frame_end(s);
149
150 pict->data[0] = s->current_picture[0];
151 pict->data[1] = s->current_picture[1];
152 pict->data[2] = s->current_picture[2];
153 pict->linesize[0] = s->linesize;
154 pict->linesize[1] = s->linesize / 2;
155 pict->linesize[2] = s->linesize / 2;
156
157 avctx->quality = s->qscale;
158 *data_size = sizeof(AVPicture);
159 return buf_size;
160}
161
162AVCodec opendivx_decoder = {
163 "opendivx",
164 CODEC_TYPE_VIDEO,
165 CODEC_ID_OPENDIVX,
166 sizeof(MpegEncContext),
167 h263_decode_init,
168 NULL,
169 h263_decode_end,
170 h263_decode_frame,
171};
172
173AVCodec h263_decoder = {
174 "h263",
175 CODEC_TYPE_VIDEO,
176 CODEC_ID_H263,
177 sizeof(MpegEncContext),
178 h263_decode_init,
179 NULL,
180 h263_decode_end,
181 h263_decode_frame,
182};
183
184AVCodec msmpeg4_decoder = {
185 "msmpeg4",
186 CODEC_TYPE_VIDEO,
187 CODEC_ID_MSMPEG4,
188 sizeof(MpegEncContext),
189 h263_decode_init,
190 NULL,
191 h263_decode_end,
192 h263_decode_frame,
193};
194
195AVCodec h263i_decoder = {
196 "h263i",
197 CODEC_TYPE_VIDEO,
198 CODEC_ID_H263I,
199 sizeof(MpegEncContext),
200 h263_decode_init,
201 NULL,
202 h263_decode_end,
203 h263_decode_frame,
204};
205