8bbd3a75805212bc5537d50ba61468f63bc6abf9
[libav.git] / libavcodec / h263dec.c
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
28 static 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
68 static int h263_decode_end(AVCodecContext *avctx)
69 {
70 MpegEncContext *s = avctx->priv_data;
71
72 MPV_common_end(s);
73 return 0;
74 }
75
76 static 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 AVPicture *pict = data;
83
84 #ifdef DEBUG
85 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
86 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
87 #endif
88
89 /* no supplementary picture */
90 if (buf_size == 0) {
91 *data_size = 0;
92 return 0;
93 }
94
95 init_get_bits(&s->gb, buf, buf_size);
96
97 /* let's go :-) */
98 if (s->h263_msmpeg4) {
99 ret = msmpeg4_decode_picture_header(s);
100 } else if (s->h263_pred) {
101 ret = mpeg4_decode_picture_header(s);
102 } else if (s->h263_intel) {
103 ret = intel_h263_decode_picture_header(s);
104 } else {
105 ret = h263_decode_picture_header(s);
106 }
107 if (ret < 0)
108 return -1;
109
110 MPV_frame_start(s);
111
112 #ifdef DEBUG
113 printf("qscale=%d\n", s->qscale);
114 #endif
115
116 /* decode each macroblock */
117 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
118 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
119 #ifdef DEBUG
120 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
121 #endif
122 /* DCT & quantize */
123 if (s->h263_msmpeg4) {
124 msmpeg4_dc_scale(s);
125 } else if (s->h263_pred) {
126 h263_dc_scale(s);
127 } else {
128 /* default quantization values */
129 s->y_dc_scale = 8;
130 s->c_dc_scale = 8;
131 }
132
133 memset(s->block, 0, sizeof(s->block));
134 s->mv_dir = MV_DIR_FORWARD;
135 s->mv_type = MV_TYPE_16X16;
136 if (s->h263_msmpeg4) {
137 if (msmpeg4_decode_mb(s, s->block) < 0)
138 return -1;
139 } else {
140 if (h263_decode_mb(s, s->block) < 0)
141 return -1;
142 }
143 MPV_decode_mb(s, s->block);
144 }
145 }
146
147 MPV_frame_end(s);
148
149 pict->data[0] = s->current_picture[0];
150 pict->data[1] = s->current_picture[1];
151 pict->data[2] = s->current_picture[2];
152 pict->linesize[0] = s->linesize;
153 pict->linesize[1] = s->linesize / 2;
154 pict->linesize[2] = s->linesize / 2;
155
156 avctx->quality = s->qscale;
157 *data_size = sizeof(AVPicture);
158 return buf_size;
159 }
160
161 AVCodec opendivx_decoder = {
162 "opendivx",
163 CODEC_TYPE_VIDEO,
164 CODEC_ID_OPENDIVX,
165 sizeof(MpegEncContext),
166 h263_decode_init,
167 NULL,
168 h263_decode_end,
169 h263_decode_frame,
170 };
171
172 AVCodec h263_decoder = {
173 "h263",
174 CODEC_TYPE_VIDEO,
175 CODEC_ID_H263,
176 sizeof(MpegEncContext),
177 h263_decode_init,
178 NULL,
179 h263_decode_end,
180 h263_decode_frame,
181 };
182
183 AVCodec msmpeg4_decoder = {
184 "msmpeg4",
185 CODEC_TYPE_VIDEO,
186 CODEC_ID_MSMPEG4,
187 sizeof(MpegEncContext),
188 h263_decode_init,
189 NULL,
190 h263_decode_end,
191 h263_decode_frame,
192 };
193
194 AVCodec h263i_decoder = {
195 "h263i",
196 CODEC_TYPE_VIDEO,
197 CODEC_ID_H263I,
198 sizeof(MpegEncContext),
199 h263_decode_init,
200 NULL,
201 h263_decode_end,
202 h263_decode_frame,
203 };
204