Mercurial > libavcodec.hg
annotate h263dec.c @ 418:b2fb2081dab5 libavcodec
* somewhat more advanced test suit for dsp code - for now i386 only
cd test ; make dsptest ; ./dsptest
author | kabi |
---|---|
date | Thu, 23 May 2002 13:34:22 +0000 |
parents | 5c8b3a717929 |
children | 718a22dc121f |
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 */ | |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
384
diff
changeset
|
19 #include "avcodec.h" |
0 | 20 #include "dsputil.h" |
21 #include "mpegvideo.h" | |
22 | |
23 //#define DEBUG | |
384 | 24 //#define PRINT_FRAME_TIME |
25 #ifdef PRINT_FRAME_TIME | |
26 static inline long long rdtsc() | |
27 { | |
28 long long l; | |
29 asm volatile( "rdtsc\n\t" | |
30 : "=A" (l) | |
31 ); | |
32 // printf("%d\n", int(l/1000)); | |
33 return l; | |
34 } | |
35 #endif | |
0 | 36 |
37 static int h263_decode_init(AVCodecContext *avctx) | |
38 { | |
39 MpegEncContext *s = avctx->priv_data; | |
60 | 40 |
67 | 41 s->avctx = avctx; |
0 | 42 s->out_format = FMT_H263; |
43 | |
44 s->width = avctx->width; | |
45 s->height = avctx->height; | |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
46 s->workaround_bugs= avctx->workaround_bugs; |
0 | 47 |
48 /* select sub codec */ | |
49 switch(avctx->codec->id) { | |
50 case CODEC_ID_H263: | |
154
f914f710b8d0
- Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents:
144
diff
changeset
|
51 s->gob_number = 0; |
f914f710b8d0
- Fixed a bug on H.263 MV prediction for MB on GOBs limits.
pulento
parents:
144
diff
changeset
|
52 s->first_gob_line = 0; |
0 | 53 break; |
67 | 54 case CODEC_ID_MPEG4: |
0 | 55 s->time_increment_bits = 4; /* default value for broken headers */ |
56 s->h263_pred = 1; | |
336 | 57 s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing |
0 | 58 break; |
307 | 59 case CODEC_ID_MSMPEG4V1: |
0 | 60 s->h263_msmpeg4 = 1; |
61 s->h263_pred = 1; | |
307 | 62 s->msmpeg4_version=1; |
63 break; | |
64 case CODEC_ID_MSMPEG4V2: | |
65 s->h263_msmpeg4 = 1; | |
66 s->h263_pred = 1; | |
67 s->msmpeg4_version=2; | |
68 break; | |
69 case CODEC_ID_MSMPEG4V3: | |
70 s->h263_msmpeg4 = 1; | |
71 s->h263_pred = 1; | |
72 s->msmpeg4_version=3; | |
0 | 73 break; |
311 | 74 case CODEC_ID_WMV1: |
75 s->h263_msmpeg4 = 1; | |
76 s->h263_pred = 1; | |
77 s->msmpeg4_version=4; | |
78 break; | |
0 | 79 case CODEC_ID_H263I: |
80 s->h263_intel = 1; | |
81 break; | |
82 default: | |
83 return -1; | |
84 } | |
344 | 85 s->codec_id= avctx->codec->id; |
345 | 86 avctx->mbskip_table= s->mbskip_table; |
344 | 87 |
0 | 88 /* 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
|
89 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4) |
144 | 90 if (MPV_common_init(s) < 0) |
91 return -1; | |
0 | 92 |
93 if (s->h263_msmpeg4) | |
94 msmpeg4_decode_init_vlc(s); | |
95 else | |
96 h263_decode_init_vlc(s); | |
97 | |
98 return 0; | |
99 } | |
100 | |
101 static int h263_decode_end(AVCodecContext *avctx) | |
102 { | |
103 MpegEncContext *s = avctx->priv_data; | |
104 | |
105 MPV_common_end(s); | |
106 return 0; | |
107 } | |
108 | |
109 static int h263_decode_frame(AVCodecContext *avctx, | |
110 void *data, int *data_size, | |
111 UINT8 *buf, int buf_size) | |
112 { | |
113 MpegEncContext *s = avctx->priv_data; | |
114 int ret; | |
115 AVPicture *pict = data; | |
384 | 116 #ifdef PRINT_FRAME_TIME |
117 uint64_t time= rdtsc(); | |
118 #endif | |
0 | 119 #ifdef DEBUG |
120 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size); | |
121 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); | |
122 #endif | |
345 | 123 |
124 s->hurry_up= avctx->hurry_up; | |
144 | 125 |
0 | 126 /* no supplementary picture */ |
127 if (buf_size == 0) { | |
128 *data_size = 0; | |
129 return 0; | |
130 } | |
131 | |
353 | 132 if(s->bitstream_buffer_size){ //divx 5.01+ frame reorder |
333 | 133 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size); |
353 | 134 s->bitstream_buffer_size=0; |
135 }else | |
333 | 136 init_get_bits(&s->gb, buf, buf_size); |
0 | 137 |
138 /* let's go :-) */ | |
139 if (s->h263_msmpeg4) { | |
140 ret = msmpeg4_decode_picture_header(s); | |
141 } else if (s->h263_pred) { | |
142 ret = mpeg4_decode_picture_header(s); | |
336 | 143 s->has_b_frames= !s->low_delay; |
0 | 144 } else if (s->h263_intel) { |
145 ret = intel_h263_decode_picture_header(s); | |
146 } else { | |
147 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
|
148 } |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
149 |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
150 /* After H263 & mpeg4 header decode we have the height, width,*/ |
160 | 151 /* and other parameters. So then we could init the picture */ |
152 /* FIXME: By the way H263 decoder is evolving it should have */ | |
153 /* an H263EncContext */ | |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
154 if (!s->context_initialized) { |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
155 avctx->width = s->width; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
156 avctx->height = s->height; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
274
diff
changeset
|
157 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
|
158 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
|
159 return -1; |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
160 } 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
|
161 /* 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
|
162 MPV_common_end(s); |
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
163 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
|
164 return -1; |
0 | 165 } |
274
d0c186bcf075
use the width & height from the mpeg4 header ... in the case that its complete
michaelni
parents:
273
diff
changeset
|
166 |
341 | 167 if(ret==FRAME_SKIPED) return 0; |
0 | 168 if (ret < 0) |
169 return -1; | |
341 | 170 /* skip b frames if we dont have reference frames */ |
171 if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0; | |
345 | 172 /* skip b frames if we are in a hurry */ |
173 if(s->hurry_up && s->pict_type==B_TYPE) return 0; | |
341 | 174 |
0 | 175 MPV_frame_start(s); |
176 | |
177 #ifdef DEBUG | |
178 printf("qscale=%d\n", s->qscale); | |
179 #endif | |
180 | |
181 /* decode each macroblock */ | |
266 | 182 s->block_wrap[0]= |
183 s->block_wrap[1]= | |
184 s->block_wrap[2]= | |
185 s->block_wrap[3]= s->mb_width*2 + 2; | |
186 s->block_wrap[4]= | |
187 s->block_wrap[5]= s->mb_width + 2; | |
0 | 188 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) { |
162 | 189 /* Check for GOB headers on H.263 */ |
190 /* FIXME: In the future H.263+ will have intra prediction */ | |
191 /* and we are gonna need another way to detect MPEG4 */ | |
192 if (s->mb_y && !s->h263_pred) { | |
193 s->first_gob_line = h263_decode_gob_header(s); | |
194 } | |
296 | 195 |
266 | 196 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1; |
197 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1); | |
198 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1; | |
199 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2); | |
200 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
201 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 | 202 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) { |
266 | 203 s->block_index[0]+=2; |
204 s->block_index[1]+=2; | |
205 s->block_index[2]+=2; | |
206 s->block_index[3]+=2; | |
207 s->block_index[4]++; | |
208 s->block_index[5]++; | |
0 | 209 #ifdef DEBUG |
210 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y); | |
211 #endif | |
144 | 212 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x); |
0 | 213 /* DCT & quantize */ |
347 | 214 if (s->h263_pred && s->msmpeg4_version!=2) { |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
215 /* old ffmpeg encoded msmpeg4v3 workaround */ |
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
216 if(s->workaround_bugs==1 && s->msmpeg4_version==3) |
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
217 ff_old_msmpeg4_dc_scale(s); |
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
218 else |
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
219 h263_dc_scale(s); |
0 | 220 } else { |
221 /* default quantization values */ | |
222 s->y_dc_scale = 8; | |
223 s->c_dc_scale = 8; | |
224 } | |
296 | 225 clear_blocks(s->block[0]); |
226 | |
0 | 227 s->mv_dir = MV_DIR_FORWARD; |
228 s->mv_type = MV_TYPE_16X16; | |
229 if (s->h263_msmpeg4) { | |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
230 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
|
231 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); |
0 | 232 return -1; |
242
d1a9663b973b
* continue after error in msmpeg4_decode_mb - helps for some movie samples
kabi
parents:
231
diff
changeset
|
233 } |
0 | 234 } else { |
161 | 235 if (h263_decode_mb(s, s->block) < 0) { |
236 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x); | |
0 | 237 return -1; |
161 | 238 } |
0 | 239 } |
12
4d50c7d89e0f
use block[] in structure to have it aligned on 8 bytes for mmx optimizations
glantau
parents:
0
diff
changeset
|
240 MPV_decode_mb(s, s->block); |
0 | 241 } |
341 | 242 if ( avctx->draw_horiz_band |
243 && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) { | |
67 | 244 UINT8 *src_ptr[3]; |
245 int y, h, offset; | |
246 y = s->mb_y * 16; | |
247 h = s->height - y; | |
248 if (h > 16) | |
249 h = 16; | |
250 offset = y * s->linesize; | |
308 | 251 if(s->pict_type==B_TYPE || (!s->has_b_frames)){ |
252 src_ptr[0] = s->current_picture[0] + offset; | |
253 src_ptr[1] = s->current_picture[1] + (offset >> 2); | |
254 src_ptr[2] = s->current_picture[2] + (offset >> 2); | |
255 } else { | |
256 src_ptr[0] = s->last_picture[0] + offset; | |
257 src_ptr[1] = s->last_picture[1] + (offset >> 2); | |
258 src_ptr[2] = s->last_picture[2] + (offset >> 2); | |
259 } | |
67 | 260 avctx->draw_horiz_band(avctx, src_ptr, s->linesize, |
261 y, s->width, h); | |
262 } | |
0 | 263 } |
208 | 264 |
311 | 265 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE) |
208 | 266 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1; |
333 | 267 |
268 /* divx 5.01+ bistream reorder stuff */ | |
344 | 269 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){ |
333 | 270 int current_pos= get_bits_count(&s->gb)/8; |
271 if( buf_size - current_pos > 5 | |
272 && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){ | |
273 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos); | |
274 s->bitstream_buffer_size= buf_size - current_pos; | |
275 } | |
353 | 276 } |
333 | 277 |
0 | 278 MPV_frame_end(s); |
361 | 279 #if 0 //dirty show MVs, we should export the MV tables and write a filter to show them |
280 { | |
281 int mb_y; | |
282 s->has_b_frames=1; | |
283 for(mb_y=0; mb_y<s->mb_height; mb_y++){ | |
284 int mb_x; | |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
285 int y= mb_y*16 + 8; |
361 | 286 for(mb_x=0; mb_x<s->mb_width; mb_x++){ |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
287 int x= mb_x*16 + 8; |
361 | 288 uint8_t *ptr= s->last_picture[0]; |
289 int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2); | |
290 int mx= (s->motion_val[xy][0]>>1) + x; | |
291 int my= (s->motion_val[xy][1]>>1) + y; | |
292 int i; | |
293 int max; | |
294 | |
295 if(mx<0) mx=0; | |
296 if(my<0) my=0; | |
297 if(mx>=s->width) mx= s->width -1; | |
298 if(my>=s->height) my= s->height-1; | |
299 max= ABS(mx-x); | |
300 if(ABS(my-y) > max) max= ABS(my-y); | |
301 /* the ugliest linedrawing routine ... */ | |
302 for(i=0; i<max; i++){ | |
303 int x1= x + (mx-x)*i/max; | |
304 int y1= y + (my-y)*i/max; | |
305 ptr[y1*s->linesize + x1]+=100; | |
306 } | |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
307 ptr[y*s->linesize + x]+=100; |
361 | 308 s->mbskip_table[mb_x + mb_y*s->mb_width]=0; |
309 } | |
310 } | |
311 | |
312 } | |
313 #endif | |
273
34f40a0fc840
msmpeg4 bugfix (wrong frame displayed if some frames are skipped)
michaelni
parents:
266
diff
changeset
|
314 if(s->pict_type==B_TYPE || (!s->has_b_frames)){ |
262 | 315 pict->data[0] = s->current_picture[0]; |
316 pict->data[1] = s->current_picture[1]; | |
317 pict->data[2] = s->current_picture[2]; | |
318 } else { | |
319 pict->data[0] = s->last_picture[0]; | |
320 pict->data[1] = s->last_picture[1]; | |
321 pict->data[2] = s->last_picture[2]; | |
322 } | |
0 | 323 pict->linesize[0] = s->linesize; |
324 pict->linesize[1] = s->linesize / 2; | |
325 pict->linesize[2] = s->linesize / 2; | |
326 | |
327 avctx->quality = s->qscale; | |
231 | 328 |
329 /* Return the Picture timestamp as the frame number */ | |
330 /* we substract 1 because it is added on utils.c */ | |
331 avctx->frame_number = s->picture_number - 1; | |
332 | |
341 | 333 /* dont output the last pic after seeking |
334 note we allready added +1 for the current pix in MPV_frame_end(s) */ | |
335 if(s->num_available_buffers>=2 || (!s->has_b_frames)) | |
336 *data_size = sizeof(AVPicture); | |
384 | 337 #ifdef PRINT_FRAME_TIME |
338 printf("%Ld\n", rdtsc()-time); | |
339 #endif | |
0 | 340 return buf_size; |
341 } | |
342 | |
67 | 343 AVCodec mpeg4_decoder = { |
344 "mpeg4", | |
0 | 345 CODEC_TYPE_VIDEO, |
67 | 346 CODEC_ID_MPEG4, |
0 | 347 sizeof(MpegEncContext), |
348 h263_decode_init, | |
349 NULL, | |
350 h263_decode_end, | |
351 h263_decode_frame, | |
67 | 352 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 353 }; |
354 | |
355 AVCodec h263_decoder = { | |
356 "h263", | |
357 CODEC_TYPE_VIDEO, | |
358 CODEC_ID_H263, | |
359 sizeof(MpegEncContext), | |
360 h263_decode_init, | |
361 NULL, | |
362 h263_decode_end, | |
363 h263_decode_frame, | |
67 | 364 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 365 }; |
366 | |
307 | 367 AVCodec msmpeg4v1_decoder = { |
368 "msmpeg4v1", | |
369 CODEC_TYPE_VIDEO, | |
370 CODEC_ID_MSMPEG4V1, | |
371 sizeof(MpegEncContext), | |
372 h263_decode_init, | |
373 NULL, | |
374 h263_decode_end, | |
375 h263_decode_frame, | |
376 CODEC_CAP_DRAW_HORIZ_BAND, | |
377 }; | |
378 | |
379 AVCodec msmpeg4v2_decoder = { | |
380 "msmpeg4v2", | |
381 CODEC_TYPE_VIDEO, | |
382 CODEC_ID_MSMPEG4V2, | |
383 sizeof(MpegEncContext), | |
384 h263_decode_init, | |
385 NULL, | |
386 h263_decode_end, | |
387 h263_decode_frame, | |
388 CODEC_CAP_DRAW_HORIZ_BAND, | |
389 }; | |
390 | |
391 AVCodec msmpeg4v3_decoder = { | |
0 | 392 "msmpeg4", |
393 CODEC_TYPE_VIDEO, | |
307 | 394 CODEC_ID_MSMPEG4V3, |
0 | 395 sizeof(MpegEncContext), |
396 h263_decode_init, | |
397 NULL, | |
398 h263_decode_end, | |
399 h263_decode_frame, | |
67 | 400 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 401 }; |
402 | |
311 | 403 AVCodec wmv1_decoder = { |
404 "wmv1", | |
405 CODEC_TYPE_VIDEO, | |
406 CODEC_ID_WMV1, | |
407 sizeof(MpegEncContext), | |
408 h263_decode_init, | |
409 NULL, | |
410 h263_decode_end, | |
411 h263_decode_frame, | |
412 CODEC_CAP_DRAW_HORIZ_BAND, | |
413 }; | |
414 | |
0 | 415 AVCodec h263i_decoder = { |
416 "h263i", | |
417 CODEC_TYPE_VIDEO, | |
418 CODEC_ID_H263I, | |
419 sizeof(MpegEncContext), | |
420 h263_decode_init, | |
421 NULL, | |
422 h263_decode_end, | |
423 h263_decode_frame, | |
67 | 424 CODEC_CAP_DRAW_HORIZ_BAND, |
0 | 425 }; |
426 |