Mercurial > libavcodec.hg
annotate h263dec.c @ 345:e05b357a398a libavcodec
export mbskip_table for direct rendering
add hurry_up support
author | michaelni |
---|---|
date | Sat, 27 Apr 2002 13:12:57 +0000 |
parents | 9f6071a87e17 |
children | 3c5266cda02d |
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; | |
336 | 48 s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing |
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 } | |
344 | 76 s->codec_id= avctx->codec->id; |
345 | 77 avctx->mbskip_table= s->mbskip_table; |
344 | 78 |
0 | 79 /* 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
|
80 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4) |
144 | 81 if (MPV_common_init(s) < 0) |
82 return -1; | |
0 | 83 |
84 if (s->h263_msmpeg4) | |
85 msmpeg4_decode_init_vlc(s); | |
86 else | |
87 h263_decode_init_vlc(s); | |
88 | |
89 return 0; | |
90 } | |
91 | |
92 static int h263_decode_end(AVCodecContext *avctx) | |
93 { | |
94 MpegEncContext *s = avctx->priv_data; | |
95 | |
96 MPV_common_end(s); | |
97 return 0; | |
98 } | |
99 | |
100 static int h263_decode_frame(AVCodecContext *avctx, | |
101 void *data, int *data_size, | |
102 UINT8 *buf, int buf_size) | |
103 { | |
104 MpegEncContext *s = avctx->priv_data; | |
105 int ret; | |
106 AVPicture *pict = data; | |
107 | |
108 #ifdef DEBUG | |
109 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size); | |
110 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); | |
111 #endif | |
345 | 112 |
113 s->hurry_up= avctx->hurry_up; | |
144 | 114 |
0 | 115 /* no supplementary picture */ |
116 if (buf_size == 0) { | |
117 *data_size = 0; | |
118 return 0; | |
119 } | |
120 | |
333 | 121 if(s->bitstream_buffer_size) //divx 5.01+ frame reorder |
122 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size); | |
123 else | |
124 init_get_bits(&s->gb, buf, buf_size); | |
0 | 125 |
126 /* let's go :-) */ | |
127 if (s->h263_msmpeg4) { | |
128 ret = msmpeg4_decode_picture_header(s); | |
129 } else if (s->h263_pred) { | |
130 ret = mpeg4_decode_picture_header(s); | |
336 | 131 s->has_b_frames= !s->low_delay; |
0 | 132 } else if (s->h263_intel) { |
133 ret = intel_h263_decode_picture_header(s); | |
134 } else { | |
135 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
|
136 } |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
137 |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
138 /* After H263 & mpeg4 header decode we have the height, width,*/ |
160 | 139 /* and other parameters. So then we could init the picture */ |
140 /* FIXME: By the way H263 decoder is evolving it should have */ | |
141 /* an H263EncContext */ | |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
142 if (!s->context_initialized) { |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
143 avctx->width = s->width; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
144 avctx->height = s->height; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
274
diff
changeset
|
145 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
|
146 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
|
147 return -1; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
148 } 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
|
149 /* 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
|
150 MPV_common_end(s); |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
151 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
|
152 return -1; |
0 | 153 } |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
154 |
341 | 155 if(ret==FRAME_SKIPED) return 0; |
0 | 156 if (ret < 0) |
157 return -1; | |
341 | 158 /* skip b frames if we dont have reference frames */ |
159 if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0; | |
345 | 160 /* skip b frames if we are in a hurry */ |
161 if(s->hurry_up && s->pict_type==B_TYPE) return 0; | |
341 | 162 |
0 | 163 MPV_frame_start(s); |
164 | |
165 #ifdef DEBUG | |
166 printf("qscale=%d\n", s->qscale); | |
167 #endif | |
168 | |
169 /* decode each macroblock */ | |
266 | 170 s->block_wrap[0]= |
171 s->block_wrap[1]= | |
172 s->block_wrap[2]= | |
173 s->block_wrap[3]= s->mb_width*2 + 2; | |
174 s->block_wrap[4]= | |
175 s->block_wrap[5]= s->mb_width + 2; | |
0 | 176 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) { |
162 | 177 /* Check for GOB headers on H.263 */ |
178 /* FIXME: In the future H.263+ will have intra prediction */ | |
179 /* and we are gonna need another way to detect MPEG4 */ | |
180 if (s->mb_y && !s->h263_pred) { | |
181 s->first_gob_line = h263_decode_gob_header(s); | |
182 } | |
296 | 183 |
266 | 184 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1; |
185 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1); | |
186 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1; | |
187 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2); | |
188 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
189 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 | 190 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) { |
266 | 191 s->block_index[0]+=2; |
192 s->block_index[1]+=2; | |
193 s->block_index[2]+=2; | |
194 s->block_index[3]+=2; | |
195 s->block_index[4]++; | |
196 s->block_index[5]++; | |
0 | 197 #ifdef DEBUG |
198 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); | |
199 #endif | |
144 | 200 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x); |
0 | 201 /* DCT & quantize */ |
202 if (s->h263_msmpeg4) { | |
203 msmpeg4_dc_scale(s); | |
204 } else if (s->h263_pred) { | |
205 h263_dc_scale(s); | |
206 } else { | |
207 /* default quantization values */ | |
208 s->y_dc_scale = 8; | |
209 s->c_dc_scale = 8; | |
210 } | |
296 | 211 clear_blocks(s->block[0]); |
212 | |
0 | 213 s->mv_dir = MV_DIR_FORWARD; |
214 s->mv_type = MV_TYPE_16X16; | |
215 if (s->h263_msmpeg4) { | |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
216 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
|
217 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); |
0 | 218 return -1; |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
219 } |
0 | 220 } else { |
161 | 221 if (h263_decode_mb(s, s->block) < 0) { |
222 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); | |
0 | 223 return -1; |
161 | 224 } |
0 | 225 } |
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
0
diff
changeset
|
226 MPV_decode_mb(s, s->block); |
0 | 227 } |
341 | 228 if ( avctx->draw_horiz_band |
229 && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) { | |
67 | 230 UINT8 *src_ptr[3]; |
231 int y, h, offset; | |
232 y = s->mb_y * 16; | |
233 h = s->height - y; | |
234 if (h > 16) | |
235 h = 16; | |
236 offset = y * s->linesize; | |
308 | 237 if(s->pict_type==B_TYPE || (!s->has_b_frames)){ |
238 src_ptr[0] = s->current_picture[0] + offset; | |
239 src_ptr[1] = s->current_picture[1] + (offset >> 2); | |
240 src_ptr[2] = s->current_picture[2] + (offset >> 2); | |
241 } else { | |
242 src_ptr[0] = s->last_picture[0] + offset; | |
243 src_ptr[1] = s->last_picture[1] + (offset >> 2); | |
244 src_ptr[2] = s->last_picture[2] + (offset >> 2); | |
245 } | |
67 | 246 avctx->draw_horiz_band(avctx, src_ptr, s->linesize, |
247 y, s->width, h); | |
248 } | |
0 | 249 } |
208 | 250 |
311 | 251 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE) |
208 | 252 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1; |
333 | 253 |
254 /* divx 5.01+ bistream reorder stuff */ | |
344 | 255 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){ |
333 | 256 int current_pos= get_bits_count(&s->gb)/8; |
257 if( buf_size - current_pos > 5 | |
258 && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){ | |
259 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos); | |
260 s->bitstream_buffer_size= buf_size - current_pos; | |
261 } | |
262 }else | |
263 s->bitstream_buffer_size=0; | |
264 | |
0 | 265 MPV_frame_end(s); |
266 | |
273
34f40a0fc840
msmpeg4 bugfix (wrong frame displayed if some frames are skipped)
michaelni
parents:
266
diff
changeset
|
267 if(s->pict_type==B_TYPE || (!s->has_b_frames)){ |
262 | 268 pict->data[0] = s->current_picture[0]; |
269 pict->data[1] = s->current_picture[1]; | |
270 pict->data[2] = s->current_picture[2]; | |
271 } else { | |
272 pict->data[0] = s->last_picture[0]; | |
273 pict->data[1] = s->last_picture[1]; | |
274 pict->data[2] = s->last_picture[2]; | |
275 } | |
0 | 276 pict->linesize[0] = s->linesize; |
277 pict->linesize[1] = s->linesize / 2; | |
278 pict->linesize[2] = s->linesize / 2; | |
279 | |
280 avctx->quality = s->qscale; | |
231 | 281 |
282 /* Return the Picture timestamp as the frame number */ | |
283 /* we substract 1 because it is added on utils.c */ | |
284 avctx->frame_number = s->picture_number - 1; | |
285 | |
341 | 286 /* dont output the last pic after seeking |
287 note we allready added +1 for the current pix in MPV_frame_end(s) */ | |
288 if(s->num_available_buffers>=2 || (!s->has_b_frames)) | |
289 *data_size = sizeof(AVPicture); | |
290 | |
0 | 291 return buf_size; |
292 } | |
293 | |
67 | 294 AVCodec mpeg4_decoder = { |
295 "mpeg4", | |
0 | 296 CODEC_TYPE_VIDEO, |
67 | 297 CODEC_ID_MPEG4, |
0 | 298 sizeof(MpegEncContext), |
299 h263_decode_init, | |
300 NULL, | |
301 h263_decode_end, | |
302 h263_decode_frame, | |
67 | 303 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 304 }; |
305 | |
306 AVCodec h263_decoder = { | |
307 "h263", | |
308 CODEC_TYPE_VIDEO, | |
309 CODEC_ID_H263, | |
310 sizeof(MpegEncContext), | |
311 h263_decode_init, | |
312 NULL, | |
313 h263_decode_end, | |
314 h263_decode_frame, | |
67 | 315 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 316 }; |
317 | |
307 | 318 AVCodec msmpeg4v1_decoder = { |
319 "msmpeg4v1", | |
320 CODEC_TYPE_VIDEO, | |
321 CODEC_ID_MSMPEG4V1, | |
322 sizeof(MpegEncContext), | |
323 h263_decode_init, | |
324 NULL, | |
325 h263_decode_end, | |
326 h263_decode_frame, | |
327 CODEC_CAP_DRAW_HORIZ_BAND, | |
328 }; | |
329 | |
330 AVCodec msmpeg4v2_decoder = { | |
331 "msmpeg4v2", | |
332 CODEC_TYPE_VIDEO, | |
333 CODEC_ID_MSMPEG4V2, | |
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 | |
342 AVCodec msmpeg4v3_decoder = { | |
0 | 343 "msmpeg4", |
344 CODEC_TYPE_VIDEO, | |
307 | 345 CODEC_ID_MSMPEG4V3, |
0 | 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 | |
311 | 354 AVCodec wmv1_decoder = { |
355 "wmv1", | |
356 CODEC_TYPE_VIDEO, | |
357 CODEC_ID_WMV1, | |
358 sizeof(MpegEncContext), | |
359 h263_decode_init, | |
360 NULL, | |
361 h263_decode_end, | |
362 h263_decode_frame, | |
363 CODEC_CAP_DRAW_HORIZ_BAND, | |
364 }; | |
365 | |
0 | 366 AVCodec h263i_decoder = { |
367 "h263i", | |
368 CODEC_TYPE_VIDEO, | |
369 CODEC_ID_H263I, | |
370 sizeof(MpegEncContext), | |
371 h263_decode_init, | |
372 NULL, | |
373 h263_decode_end, | |
374 h263_decode_frame, | |
67 | 375 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 376 }; |
377 |