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