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