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