Mercurial > libavcodec.hg
annotate h263dec.c @ 311:ac677a84d5df libavcodec
wmv1 header parser (unfinished)
author | michaelni |
---|---|
date | Sat, 06 Apr 2002 22:29:37 +0000 |
parents | 072c4ff05e62 |
children | 8aa87f1dfc52 |
rev | line source |
---|---|
0 | 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; | |
60 | 31 int i; |
32 | |
67 | 33 s->avctx = avctx; |
0 | 34 s->out_format = FMT_H263; |
35 | |
36 s->width = avctx->width; | |
37 s->height = avctx->height; | |
38 | |
39 /* select sub codec */ | |
40 switch(avctx->codec->id) { | |
41 case CODEC_ID_H263: | |
154
f914f710b8d0
- Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents:
144
diff
changeset
|
42 s->gob_number = 0; |
f914f710b8d0
- Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents:
144
diff
changeset
|
43 s->first_gob_line = 0; |
0 | 44 break; |
67 | 45 case CODEC_ID_MPEG4: |
0 | 46 s->time_increment_bits = 4; /* default value for broken headers */ |
47 s->h263_pred = 1; | |
262 | 48 s->has_b_frames = 1; |
0 | 49 break; |
307 | 50 case CODEC_ID_MSMPEG4V1: |
0 | 51 s->h263_msmpeg4 = 1; |
52 s->h263_pred = 1; | |
307 | 53 s->msmpeg4_version=1; |
54 break; | |
55 case CODEC_ID_MSMPEG4V2: | |
56 s->h263_msmpeg4 = 1; | |
57 s->h263_pred = 1; | |
58 s->msmpeg4_version=2; | |
59 break; | |
60 case CODEC_ID_MSMPEG4V3: | |
61 s->h263_msmpeg4 = 1; | |
62 s->h263_pred = 1; | |
63 s->msmpeg4_version=3; | |
0 | 64 break; |
311 | 65 case CODEC_ID_WMV1: |
66 s->h263_msmpeg4 = 1; | |
67 s->h263_pred = 1; | |
68 s->msmpeg4_version=4; | |
69 break; | |
0 | 70 case CODEC_ID_H263I: |
71 s->h263_intel = 1; | |
72 break; | |
73 default: | |
74 return -1; | |
75 } | |
76 | |
77 /* for h263, we allocate the images after having read the header */ | |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
78 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4) |
144 | 79 if (MPV_common_init(s) < 0) |
80 return -1; | |
0 | 81 |
60 | 82 /* XXX: suppress this matrix init, only needed because using mpeg1 |
83 dequantize in mmx case */ | |
84 for(i=0;i<64;i++) | |
85 s->non_intra_matrix[i] = default_non_intra_matrix[i]; | |
86 | |
0 | 87 if (s->h263_msmpeg4) |
88 msmpeg4_decode_init_vlc(s); | |
89 else | |
90 h263_decode_init_vlc(s); | |
91 | |
92 return 0; | |
93 } | |
94 | |
95 static int h263_decode_end(AVCodecContext *avctx) | |
96 { | |
97 MpegEncContext *s = avctx->priv_data; | |
98 | |
99 MPV_common_end(s); | |
100 return 0; | |
101 } | |
102 | |
103 static int h263_decode_frame(AVCodecContext *avctx, | |
104 void *data, int *data_size, | |
105 UINT8 *buf, int buf_size) | |
106 { | |
107 MpegEncContext *s = avctx->priv_data; | |
108 int ret; | |
109 AVPicture *pict = data; | |
110 | |
111 #ifdef DEBUG | |
112 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size); | |
113 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); | |
114 #endif | |
144 | 115 |
0 | 116 /* no supplementary picture */ |
117 if (buf_size == 0) { | |
118 *data_size = 0; | |
119 return 0; | |
120 } | |
121 | |
122 init_get_bits(&s->gb, buf, buf_size); | |
123 | |
124 /* let's go :-) */ | |
125 if (s->h263_msmpeg4) { | |
126 ret = msmpeg4_decode_picture_header(s); | |
127 } else if (s->h263_pred) { | |
128 ret = mpeg4_decode_picture_header(s); | |
129 } else if (s->h263_intel) { | |
130 ret = intel_h263_decode_picture_header(s); | |
131 } else { | |
132 ret = h263_decode_picture_header(s); | |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
133 } |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
134 |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
135 /* After H263 & mpeg4 header decode we have the height, width,*/ |
160 | 136 /* and other parameters. So then we could init the picture */ |
137 /* FIXME: By the way H263 decoder is evolving it should have */ | |
138 /* an H263EncContext */ | |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
139 if (!s->context_initialized) { |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
140 avctx->width = s->width; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
141 avctx->height = s->height; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
274
diff
changeset
|
142 avctx->aspect_ratio_info= s->aspect_ratio_info; |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
143 if (MPV_common_init(s) < 0) |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
144 return -1; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
145 } else if (s->width != avctx->width || s->height != avctx->height) { |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
146 /* H.263 could change picture size any time */ |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
147 MPV_common_end(s); |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
148 if (MPV_common_init(s) < 0) |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
149 return -1; |
0 | 150 } |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
151 |
0 | 152 if (ret < 0) |
153 return -1; | |
154 | |
155 MPV_frame_start(s); | |
156 | |
157 #ifdef DEBUG | |
158 printf("qscale=%d\n", s->qscale); | |
159 #endif | |
160 | |
161 /* decode each macroblock */ | |
266 | 162 s->block_wrap[0]= |
163 s->block_wrap[1]= | |
164 s->block_wrap[2]= | |
165 s->block_wrap[3]= s->mb_width*2 + 2; | |
166 s->block_wrap[4]= | |
167 s->block_wrap[5]= s->mb_width + 2; | |
0 | 168 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) { |
162 | 169 /* Check for GOB headers on H.263 */ |
170 /* FIXME: In the future H.263+ will have intra prediction */ | |
171 /* and we are gonna need another way to detect MPEG4 */ | |
172 if (s->mb_y && !s->h263_pred) { | |
173 s->first_gob_line = h263_decode_gob_header(s); | |
174 } | |
296 | 175 |
266 | 176 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1; |
177 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1); | |
178 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1; | |
179 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2); | |
180 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
181 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); | |
0 | 182 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) { |
266 | 183 s->block_index[0]+=2; |
184 s->block_index[1]+=2; | |
185 s->block_index[2]+=2; | |
186 s->block_index[3]+=2; | |
187 s->block_index[4]++; | |
188 s->block_index[5]++; | |
0 | 189 #ifdef DEBUG |
190 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); | |
191 #endif | |
144 | 192 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x); |
0 | 193 /* DCT & quantize */ |
194 if (s->h263_msmpeg4) { | |
195 msmpeg4_dc_scale(s); | |
196 } else if (s->h263_pred) { | |
197 h263_dc_scale(s); | |
198 } else { | |
199 /* default quantization values */ | |
200 s->y_dc_scale = 8; | |
201 s->c_dc_scale = 8; | |
202 } | |
296 | 203 clear_blocks(s->block[0]); |
204 | |
0 | 205 s->mv_dir = MV_DIR_FORWARD; |
206 s->mv_type = MV_TYPE_16X16; | |
207 if (s->h263_msmpeg4) { | |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
208 if (msmpeg4_decode_mb(s, s->block) < 0) { |
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
209 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); |
0 | 210 return -1; |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
211 } |
0 | 212 } else { |
161 | 213 if (h263_decode_mb(s, s->block) < 0) { |
214 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); | |
0 | 215 return -1; |
161 | 216 } |
0 | 217 } |
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
0
diff
changeset
|
218 MPV_decode_mb(s, s->block); |
0 | 219 } |
67 | 220 if (avctx->draw_horiz_band) { |
221 UINT8 *src_ptr[3]; | |
222 int y, h, offset; | |
223 y = s->mb_y * 16; | |
224 h = s->height - y; | |
225 if (h > 16) | |
226 h = 16; | |
227 offset = y * s->linesize; | |
308 | 228 if(s->pict_type==B_TYPE || (!s->has_b_frames)){ |
229 src_ptr[0] = s->current_picture[0] + offset; | |
230 src_ptr[1] = s->current_picture[1] + (offset >> 2); | |
231 src_ptr[2] = s->current_picture[2] + (offset >> 2); | |
232 } else { | |
233 src_ptr[0] = s->last_picture[0] + offset; | |
234 src_ptr[1] = s->last_picture[1] + (offset >> 2); | |
235 src_ptr[2] = s->last_picture[2] + (offset >> 2); | |
236 } | |
67 | 237 avctx->draw_horiz_band(avctx, src_ptr, s->linesize, |
238 y, s->width, h); | |
239 } | |
0 | 240 } |
208 | 241 |
311 | 242 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE) |
208 | 243 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1; |
0 | 244 |
245 MPV_frame_end(s); | |
246 | |
273
34f40a0fc840
msmpeg4 bugfix (wrong frame displayed if some frames are skipped)
michaelni
parents:
266
diff
changeset
|
247 if(s->pict_type==B_TYPE || (!s->has_b_frames)){ |
262 | 248 pict->data[0] = s->current_picture[0]; |
249 pict->data[1] = s->current_picture[1]; | |
250 pict->data[2] = s->current_picture[2]; | |
251 } else { | |
252 pict->data[0] = s->last_picture[0]; | |
253 pict->data[1] = s->last_picture[1]; | |
254 pict->data[2] = s->last_picture[2]; | |
255 } | |
0 | 256 pict->linesize[0] = s->linesize; |
257 pict->linesize[1] = s->linesize / 2; | |
258 pict->linesize[2] = s->linesize / 2; | |
259 | |
260 avctx->quality = s->qscale; | |
231 | 261 |
262 /* Return the Picture timestamp as the frame number */ | |
263 /* we substract 1 because it is added on utils.c */ | |
264 avctx->frame_number = s->picture_number - 1; | |
265 | |
0 | 266 *data_size = sizeof(AVPicture); |
267 return buf_size; | |
268 } | |
269 | |
67 | 270 AVCodec mpeg4_decoder = { |
271 "mpeg4", | |
0 | 272 CODEC_TYPE_VIDEO, |
67 | 273 CODEC_ID_MPEG4, |
0 | 274 sizeof(MpegEncContext), |
275 h263_decode_init, | |
276 NULL, | |
277 h263_decode_end, | |
278 h263_decode_frame, | |
67 | 279 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 280 }; |
281 | |
282 AVCodec h263_decoder = { | |
283 "h263", | |
284 CODEC_TYPE_VIDEO, | |
285 CODEC_ID_H263, | |
286 sizeof(MpegEncContext), | |
287 h263_decode_init, | |
288 NULL, | |
289 h263_decode_end, | |
290 h263_decode_frame, | |
67 | 291 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 292 }; |
293 | |
307 | 294 AVCodec msmpeg4v1_decoder = { |
295 "msmpeg4v1", | |
296 CODEC_TYPE_VIDEO, | |
297 CODEC_ID_MSMPEG4V1, | |
298 sizeof(MpegEncContext), | |
299 h263_decode_init, | |
300 NULL, | |
301 h263_decode_end, | |
302 h263_decode_frame, | |
303 CODEC_CAP_DRAW_HORIZ_BAND, | |
304 }; | |
305 | |
306 AVCodec msmpeg4v2_decoder = { | |
307 "msmpeg4v2", | |
308 CODEC_TYPE_VIDEO, | |
309 CODEC_ID_MSMPEG4V2, | |
310 sizeof(MpegEncContext), | |
311 h263_decode_init, | |
312 NULL, | |
313 h263_decode_end, | |
314 h263_decode_frame, | |
315 CODEC_CAP_DRAW_HORIZ_BAND, | |
316 }; | |
317 | |
318 AVCodec msmpeg4v3_decoder = { | |
0 | 319 "msmpeg4", |
320 CODEC_TYPE_VIDEO, | |
307 | 321 CODEC_ID_MSMPEG4V3, |
0 | 322 sizeof(MpegEncContext), |
323 h263_decode_init, | |
324 NULL, | |
325 h263_decode_end, | |
326 h263_decode_frame, | |
67 | 327 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 328 }; |
329 | |
311 | 330 AVCodec wmv1_decoder = { |
331 "wmv1", | |
332 CODEC_TYPE_VIDEO, | |
333 CODEC_ID_WMV1, | |
334 sizeof(MpegEncContext), | |
335 h263_decode_init, | |
336 NULL, | |
337 h263_decode_end, | |
338 h263_decode_frame, | |
339 CODEC_CAP_DRAW_HORIZ_BAND, | |
340 }; | |
341 | |
0 | 342 AVCodec h263i_decoder = { |
343 "h263i", | |
344 CODEC_TYPE_VIDEO, | |
345 CODEC_ID_H263I, | |
346 sizeof(MpegEncContext), | |
347 h263_decode_init, | |
348 NULL, | |
349 h263_decode_end, | |
350 h263_decode_frame, | |
67 | 351 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 352 }; |
353 |