Mercurial > libavcodec.hg
annotate indeo5.c @ 11124:85a1b00a2413 libavcodec
Use int8_t instead of char, the signedness of char can differ between systems.
| author | reimar |
|---|---|
| date | Fri, 12 Feb 2010 18:18:35 +0000 |
| parents | 1d4aeef800d4 |
| children | 384b6a615a92 |
| rev | line source |
|---|---|
| 11107 | 1 /* |
| 2 * Indeo Video Interactive v5 compatible decoder | |
| 3 * Copyright (c) 2009 Maxim Poliakovski | |
| 4 * | |
| 5 * This file is part of FFmpeg. | |
| 6 * | |
| 7 * FFmpeg is free software; you can redistribute it and/or | |
| 8 * modify it under the terms of the GNU Lesser General Public | |
| 9 * License as published by the Free Software Foundation; either | |
| 10 * version 2.1 of the License, or (at your option) any later version. | |
| 11 * | |
| 12 * FFmpeg is distributed in the hope that it will be useful, | |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 * Lesser General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU Lesser General Public | |
| 18 * License along with FFmpeg; if not, write to the Free Software | |
| 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 20 */ | |
| 21 | |
| 22 /** | |
| 23 * @file libavcodec/indeo5.c | |
| 24 * Indeo Video Interactive version 5 decoder | |
| 25 * | |
| 26 * Indeo5 data is usually transported within .avi or .mov files. | |
| 27 * Known FOURCCs: 'IV50' | |
| 28 */ | |
| 29 | |
| 30 #define ALT_BITSTREAM_READER_LE | |
| 31 #include "avcodec.h" | |
| 32 #include "get_bits.h" | |
| 33 #include "ivi_dsp.h" | |
| 34 #include "ivi_common.h" | |
| 35 #include "indeo5data.h" | |
| 36 | |
| 37 /** | |
| 38 * Indeo5 frame types. | |
| 39 */ | |
| 40 enum { | |
| 41 FRAMETYPE_INTRA = 0, | |
| 42 FRAMETYPE_INTER = 1, ///< non-droppable P-frame | |
| 43 FRAMETYPE_INTER_SCAL = 2, ///< droppable P-frame used in the scalability mode | |
| 44 FRAMETYPE_INTER_NOREF = 3, ///< droppable P-frame | |
| 45 FRAMETYPE_NULL = 4 ///< empty frame with no data | |
| 46 }; | |
| 47 | |
| 48 #define IVI5_PIC_SIZE_ESC 15 | |
| 49 | |
| 50 #define IVI5_IS_PROTECTED 0x20 | |
| 51 | |
| 52 typedef struct { | |
| 53 GetBitContext gb; | |
| 54 AVFrame frame; | |
| 55 RVMapDesc rvmap_tabs[9]; ///< local corrected copy of the static rvmap tables | |
| 56 IVIPlaneDesc planes[3]; ///< color planes | |
| 57 const uint8_t *frame_data; ///< input frame data pointer | |
| 58 int buf_switch; ///< used to switch between three buffers | |
| 59 int dst_buf; | |
| 60 int ref_buf; | |
| 61 uint32_t frame_size; ///< frame size in bytes | |
| 62 int frame_type; | |
| 63 int prev_frame_type; ///< frame type of the previous frame | |
| 64 int frame_num; | |
| 65 uint32_t pic_hdr_size; ///< picture header size in bytes | |
| 66 uint8_t frame_flags; | |
| 67 uint16_t checksum; ///< frame checksum | |
| 68 | |
| 69 int16_t mb_huff_sel; ///< MB huffman table selector | |
| 70 IVIHuffDesc mb_huff_desc; ///< MB table descriptor associated with the selector above | |
| 71 VLC *mb_vlc; ///< ptr to the vlc table for decoding macroblock data | |
| 72 VLC mb_vlc_cust; ///< custom macroblock vlc table | |
| 73 | |
| 74 uint16_t gop_hdr_size; | |
| 75 uint8_t gop_flags; | |
| 76 int is_scalable; | |
| 77 uint32_t lock_word; | |
| 78 IVIPicConfig pic_conf; | |
| 79 } IVI5DecContext; | |
| 80 | |
| 81 //! static vlc tables (initialized at startup) | |
| 82 static VLC mb_vlc_tabs [8]; | |
| 83 static VLC blk_vlc_tabs[8]; | |
| 84 | |
| 85 | |
| 86 /** | |
| 87 * Decodes Indeo5 GOP (Group of pictures) header. | |
| 88 * This header is present in key frames only. | |
| 89 * It defines parameters for all frames in a GOP. | |
| 90 * | |
| 91 * @param ctx [in,out] ptr to the decoder context | |
| 92 * @param avctx [in] ptr to the AVCodecContext | |
| 93 * @return result code: 0 = OK, -1 = error | |
| 94 */ | |
| 95 static int decode_gop_header(IVI5DecContext *ctx, AVCodecContext *avctx) | |
| 96 { | |
| 97 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, blk_size_changed = 0; | |
| 98 IVIBandDesc *band, *band1, *band2; | |
| 99 IVIPicConfig pic_conf; | |
| 100 | |
| 101 ctx->gop_flags = get_bits(&ctx->gb, 8); | |
| 102 | |
| 103 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0; | |
| 104 | |
| 105 if (ctx->gop_flags & IVI5_IS_PROTECTED) | |
| 106 ctx->lock_word = get_bits_long(&ctx->gb, 32); | |
| 107 | |
| 108 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0; | |
| 109 if (tile_size > 256) { | |
| 110 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size); | |
| 111 return -1; | |
| 112 } | |
| 113 | |
| 114 /* decode number of wavelet bands */ | |
| 115 /* num_levels * 3 + 1 */ | |
| 116 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1; | |
| 117 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1; | |
| 118 ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1; | |
| 119 if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) { | |
| 120 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n", | |
| 121 pic_conf.luma_bands, pic_conf.chroma_bands); | |
| 122 return -1; | |
| 123 } | |
| 124 | |
| 125 pic_size_indx = get_bits(&ctx->gb, 4); | |
| 126 if (pic_size_indx == IVI5_PIC_SIZE_ESC) { | |
| 127 pic_conf.pic_height = get_bits(&ctx->gb, 13); | |
| 128 pic_conf.pic_width = get_bits(&ctx->gb, 13); | |
| 129 } else { | |
| 130 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2; | |
| 131 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2; | |
| 132 } | |
| 133 | |
| 134 if (ctx->gop_flags & 2) { | |
| 135 av_log(avctx, AV_LOG_ERROR, "YV12 picture format not supported!\n"); | |
| 136 return -1; | |
| 137 } | |
| 138 | |
| 139 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2; | |
| 140 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2; | |
| 141 | |
| 142 if (!tile_size) { | |
| 143 pic_conf.tile_height = pic_conf.pic_height; | |
| 144 pic_conf.tile_width = pic_conf.pic_width; | |
| 145 } else { | |
| 146 pic_conf.tile_height = pic_conf.tile_width = tile_size; | |
| 147 } | |
| 148 | |
| 149 /* check if picture layout was changed and reallocate buffers */ | |
| 150 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) { | |
| 151 result = ff_ivi_init_planes(ctx->planes, &pic_conf); | |
| 152 if (result) { | |
| 153 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n"); | |
| 154 return -1; | |
| 155 } | |
| 156 ctx->pic_conf = pic_conf; | |
| 157 blk_size_changed = 1; /* force reallocation of the internal structures */ | |
| 158 } | |
| 159 | |
| 160 for (p = 0; p <= 1; p++) { | |
| 161 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) { | |
| 162 band = &ctx->planes[p].bands[i]; | |
| 163 | |
| 164 band->is_halfpel = get_bits1(&ctx->gb); | |
| 165 | |
| 166 mb_size = get_bits1(&ctx->gb); | |
| 167 blk_size = 8 >> get_bits1(&ctx->gb); | |
| 168 mb_size = blk_size << !mb_size; | |
| 169 | |
| 170 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size; | |
| 171 if (blk_size_changed) { | |
| 172 band->mb_size = mb_size; | |
| 173 band->blk_size = blk_size; | |
| 174 } | |
| 175 | |
| 176 if (get_bits1(&ctx->gb)) { | |
| 177 av_log(avctx, AV_LOG_ERROR, "Extended transform info encountered!\n"); | |
| 178 return -1; | |
| 179 } | |
| 180 | |
| 181 /* select transform function and scan pattern according to plane and band number */ | |
| 182 switch ((p << 2) + i) { | |
| 183 case 0: | |
| 184 band->inv_transform = ff_ivi_inverse_slant_8x8; | |
| 185 band->dc_transform = ff_ivi_dc_slant_2d; | |
| 186 band->scan = ivi5_scans8x8[0]; | |
| 187 break; | |
| 188 | |
| 189 case 1: | |
| 190 band->inv_transform = ff_ivi_row_slant8; | |
| 191 band->dc_transform = ff_ivi_dc_row_slant; | |
| 192 band->scan = ivi5_scans8x8[1]; | |
| 193 break; | |
| 194 | |
| 195 case 2: | |
| 196 band->inv_transform = ff_ivi_col_slant8; | |
| 197 band->dc_transform = ff_ivi_dc_col_slant; | |
| 198 band->scan = ivi5_scans8x8[2]; | |
| 199 break; | |
| 200 | |
| 201 case 3: | |
| 202 band->inv_transform = ff_ivi_put_pixels_8x8; | |
| 203 band->dc_transform = ff_ivi_put_dc_pixel_8x8; | |
| 204 band->scan = ivi5_scans8x8[2]; | |
| 205 break; | |
| 206 | |
| 207 case 4: | |
| 208 band->inv_transform = ff_ivi_inverse_slant_4x4; | |
| 209 band->dc_transform = ff_ivi_dc_slant_2d; | |
| 210 band->scan = ivi5_scan4x4; | |
| 211 break; | |
| 212 } | |
| 213 | |
| 214 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 || | |
| 215 band->inv_transform == ff_ivi_inverse_slant_4x4; | |
| 216 | |
| 217 /* select dequant matrix according to plane and band number */ | |
| 218 if (!p) { | |
| 219 band->quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0; | |
| 220 } else { | |
| 221 band->quant_mat = 5; | |
| 222 } | |
| 223 | |
| 224 if (get_bits(&ctx->gb, 2)) { | |
| 225 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n"); | |
| 226 return -1; | |
| 227 } | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 /* copy chroma parameters into the 2nd chroma plane */ | |
| 232 for (i = 0; i < pic_conf.chroma_bands; i++) { | |
| 233 band1 = &ctx->planes[1].bands[i]; | |
| 234 band2 = &ctx->planes[2].bands[i]; | |
| 235 | |
| 236 band2->width = band1->width; | |
| 237 band2->height = band1->height; | |
| 238 band2->mb_size = band1->mb_size; | |
| 239 band2->blk_size = band1->blk_size; | |
| 240 band2->is_halfpel = band1->is_halfpel; | |
| 241 band2->quant_mat = band1->quant_mat; | |
| 242 band2->scan = band1->scan; | |
| 243 band2->inv_transform = band1->inv_transform; | |
| 244 band2->dc_transform = band1->dc_transform; | |
| 245 band2->is_2d_trans = band1->is_2d_trans; | |
| 246 } | |
| 247 | |
| 248 /* reallocate internal structures if needed */ | |
| 249 if (blk_size_changed) { | |
| 250 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width, | |
| 251 pic_conf.tile_height); | |
| 252 if (result) { | |
| 253 av_log(avctx, AV_LOG_ERROR, | |
| 254 "Couldn't reallocate internal structures!\n"); | |
| 255 return -1; | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 if (ctx->gop_flags & 8) { | |
| 260 if (get_bits(&ctx->gb, 3)) { | |
| 261 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n"); | |
| 262 return -1; | |
| 263 } | |
| 264 | |
| 265 if (get_bits1(&ctx->gb)) | |
| 266 skip_bits_long(&ctx->gb, 24); /* skip transparency fill color */ | |
| 267 } | |
| 268 | |
| 269 align_get_bits(&ctx->gb); | |
| 270 | |
| 271 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */ | |
| 272 | |
| 273 /* skip GOP extension if any */ | |
| 274 if (get_bits1(&ctx->gb)) { | |
| 275 do { | |
| 276 i = get_bits(&ctx->gb, 16); | |
| 277 } while (i & 0x8000); | |
| 278 } | |
| 279 | |
| 280 align_get_bits(&ctx->gb); | |
| 281 | |
| 282 return 0; | |
| 283 } | |
| 284 | |
| 285 | |
| 286 /** | |
| 287 * Skips a header extension. | |
| 288 * | |
| 289 * @param gb [in,out] the GetBit context | |
| 290 */ | |
| 291 static inline void skip_hdr_extension(GetBitContext *gb) | |
| 292 { | |
| 293 int i, len; | |
| 294 | |
| 295 do { | |
| 296 len = get_bits(gb, 8); | |
| 297 for (i = 0; i < len; i++) skip_bits(gb, 8); | |
| 298 } while(len); | |
| 299 } | |
| 300 | |
| 301 | |
| 302 /** | |
| 303 * Decodes Indeo5 picture header. | |
| 304 * | |
| 305 * @param ctx [in,out] ptr to the decoder context | |
| 306 * @param avctx [in] ptr to the AVCodecContext | |
| 307 * @return result code: 0 = OK, -1 = error | |
| 308 */ | |
| 309 static int decode_pic_hdr(IVI5DecContext *ctx, AVCodecContext *avctx) | |
| 310 { | |
| 311 int result; | |
| 312 IVIHuffDesc new_huff; | |
| 313 | |
| 314 if (get_bits(&ctx->gb, 5) != 0x1F) { | |
| 315 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n"); | |
| 316 return -1; | |
| 317 } | |
| 318 | |
| 319 ctx->prev_frame_type = ctx->frame_type; | |
| 320 ctx->frame_type = get_bits(&ctx->gb, 3); | |
| 321 if (ctx->frame_type >= 5) { | |
| 322 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type); | |
| 323 return -1; | |
| 324 } | |
| 325 | |
| 326 ctx->frame_num = get_bits(&ctx->gb, 8); | |
| 327 | |
| 328 if (ctx->frame_type == FRAMETYPE_INTRA) { | |
| 329 if (decode_gop_header(ctx, avctx)) | |
| 330 return -1; | |
| 331 } | |
| 332 | |
| 333 if (ctx->frame_type != FRAMETYPE_NULL) { | |
| 334 ctx->frame_flags = get_bits(&ctx->gb, 8); | |
| 335 | |
| 336 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits_long(&ctx->gb, 24) : 0; | |
| 337 | |
| 338 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0; | |
| 339 | |
| 340 /* skip unknown extension if any */ | |
| 341 if (ctx->frame_flags & 0x20) | |
| 342 skip_hdr_extension(&ctx->gb); /* XXX: untested */ | |
| 343 | |
| 344 /* decode macroblock huffman codebook */ | |
| 345 if (ctx->frame_flags & 0x40) { | |
| 346 ctx->mb_huff_sel = ff_ivi_dec_huff_desc(&ctx->gb, &new_huff); | |
| 347 if (ctx->mb_huff_sel != 7) { | |
| 348 ctx->mb_vlc = &mb_vlc_tabs[ctx->mb_huff_sel]; | |
| 349 } else { | |
| 350 if (ff_ivi_huff_desc_cmp(&new_huff, &ctx->mb_huff_desc)) { | |
| 351 ff_ivi_huff_desc_copy(&ctx->mb_huff_desc, &new_huff); | |
| 352 | |
| 353 if (ctx->mb_vlc_cust.table) | |
| 354 free_vlc(&ctx->mb_vlc_cust); | |
| 355 result = ff_ivi_create_huff_from_desc(&ctx->mb_huff_desc, | |
| 356 &ctx->mb_vlc_cust, 0); | |
| 357 if (result) { | |
| 358 av_log(avctx, AV_LOG_ERROR, "Error while initializing custom macroblock vlc table!\n"); | |
| 359 return -1; | |
| 360 } | |
| 361 } | |
| 362 ctx->mb_vlc = &ctx->mb_vlc_cust; | |
| 363 } | |
| 364 } else { | |
| 365 ctx->mb_vlc = &mb_vlc_tabs[7]; /* select the default macroblock huffman table */ | |
| 366 } | |
| 367 | |
| 368 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */ | |
| 369 } | |
| 370 | |
| 371 align_get_bits(&ctx->gb); | |
| 372 | |
| 373 return 0; | |
| 374 } | |
| 375 | |
| 376 | |
| 377 /** | |
| 378 * Decodes Indeo5 band header. | |
| 379 * | |
| 380 * @param ctx [in,out] ptr to the decoder context | |
| 381 * @param band [in,out] ptr to the band descriptor | |
| 382 * @param avctx [in] ptr to the AVCodecContext | |
| 383 * @return result code: 0 = OK, -1 = error | |
| 384 */ | |
| 385 static int decode_band_hdr(IVI5DecContext *ctx, IVIBandDesc *band, | |
| 386 AVCodecContext *avctx) | |
| 387 { | |
| 388 int i, result; | |
| 389 uint8_t band_flags; | |
| 390 IVIHuffDesc new_huff; | |
| 391 | |
| 392 band_flags = get_bits(&ctx->gb, 8); | |
| 393 | |
| 394 if (band_flags & 1) { | |
| 395 band->is_empty = 1; | |
| 396 return 0; | |
| 397 } | |
| 398 | |
| 399 band->data_size = (ctx->frame_flags & 0x80) ? get_bits_long(&ctx->gb, 24) : 0; | |
| 400 | |
| 401 band->inherit_mv = band_flags & 2; | |
| 402 band->inherit_qdelta = band_flags & 8; | |
| 403 band->qdelta_present = band_flags & 4; | |
| 404 if (!band->qdelta_present) band->inherit_qdelta = 1; | |
| 405 | |
| 406 /* decode rvmap probability corrections if any */ | |
| 407 band->num_corr = 0; /* there are no corrections */ | |
| 408 if (band_flags & 0x10) { | |
| 409 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */ | |
| 410 if (band->num_corr > 61) { | |
| 411 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n", | |
| 412 band->num_corr); | |
| 413 return -1; | |
| 414 } | |
| 415 | |
| 416 /* read correction pairs */ | |
| 417 for (i = 0; i < band->num_corr * 2; i++) | |
| 418 band->corr[i] = get_bits(&ctx->gb, 8); | |
| 419 } | |
| 420 | |
| 421 /* select appropriate rvmap table for this band */ | |
| 422 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8; | |
| 423 | |
| 424 /* decode block huffman codebook */ | |
| 425 if (band_flags & 0x80) { | |
| 426 band->huff_sel = ff_ivi_dec_huff_desc(&ctx->gb, &new_huff); | |
| 427 if (band->huff_sel != 7) { | |
| 428 band->blk_vlc = &blk_vlc_tabs[band->huff_sel]; | |
| 429 } else { | |
| 430 if (ff_ivi_huff_desc_cmp(&new_huff, &band->huff_desc)) { | |
| 431 ff_ivi_huff_desc_copy(&band->huff_desc, &new_huff); | |
| 432 | |
| 433 if (band->blk_vlc_cust.table) | |
| 434 free_vlc(&band->blk_vlc_cust); | |
| 435 result = ff_ivi_create_huff_from_desc(&band->huff_desc, | |
| 436 &band->blk_vlc_cust, 0); | |
| 437 if (result) { | |
| 438 av_log(avctx, AV_LOG_ERROR, "Error while initializing custom block vlc table!\n"); | |
| 439 return -1; | |
| 440 } | |
| 441 } | |
| 442 band->blk_vlc = &band->blk_vlc_cust; | |
| 443 } | |
| 444 } else { | |
| 445 band->blk_vlc = &blk_vlc_tabs[7]; /* select the default macroblock huffman table */ | |
| 446 } | |
| 447 | |
| 448 band->checksum_present = get_bits1(&ctx->gb); | |
| 449 if (band->checksum_present) | |
| 450 band->checksum = get_bits(&ctx->gb, 16); | |
| 451 | |
| 452 band->glob_quant = get_bits(&ctx->gb, 5); | |
| 453 | |
| 454 /* skip unknown extension if any */ | |
| 455 if (band_flags & 0x20) { /* XXX: untested */ | |
| 456 align_get_bits(&ctx->gb); | |
| 457 skip_hdr_extension(&ctx->gb); | |
| 458 } | |
| 459 | |
| 460 align_get_bits(&ctx->gb); | |
| 461 | |
| 462 return 0; | |
| 463 } | |
| 464 | |
| 465 | |
| 466 /** | |
| 467 * Decodes info (block type, cbp, quant delta, motion vector) | |
| 468 * for all macroblocks in the current tile. | |
| 469 * | |
| 470 * @param ctx [in,out] ptr to the decoder context | |
| 471 * @param band [in,out] ptr to the band descriptor | |
| 472 * @param tile [in,out] ptr to the tile descriptor | |
| 473 * @param avctx [in] ptr to the AVCodecContext | |
| 474 * @return result code: 0 = OK, -1 = error | |
| 475 */ | |
| 476 static int decode_mb_info(IVI5DecContext *ctx, IVIBandDesc *band, | |
| 477 IVITile *tile, AVCodecContext *avctx) | |
| 478 { | |
| 479 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset, | |
| 480 mv_scale, blks_per_mb; | |
| 481 IVIMbInfo *mb, *ref_mb; | |
| 482 int row_offset = band->mb_size * band->pitch; | |
| 483 | |
| 484 mb = tile->mbs; | |
| 485 ref_mb = tile->ref_mbs; | |
| 486 offs = tile->ypos * band->pitch + tile->xpos; | |
| 487 | |
| 488 /* scale factor for motion vectors */ | |
| 489 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3); | |
| 490 mv_x = mv_y = 0; | |
| 491 | |
| 492 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) { | |
| 493 mb_offset = offs; | |
| 494 | |
| 495 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) { | |
| 496 mb->xpos = x; | |
| 497 mb->ypos = y; | |
| 498 mb->buf_offs = mb_offset; | |
| 499 | |
| 500 if (get_bits1(&ctx->gb)) { | |
| 501 if (ctx->frame_type == FRAMETYPE_INTRA) { | |
| 502 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n"); | |
| 503 return -1; | |
| 504 } | |
| 505 mb->type = 1; /* empty macroblocks are always INTER */ | |
| 506 mb->cbp = 0; /* all blocks are empty */ | |
| 507 | |
| 508 mb->q_delta = 0; | |
| 509 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) { | |
| 510 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc->table, | |
| 511 IVI_VLC_BITS, 1); | |
| 512 mb->q_delta = IVI_TOSIGNED(mb->q_delta); | |
| 513 } | |
| 514 | |
| 515 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */ | |
| 516 if (band->inherit_mv){ | |
| 517 /* motion vector inheritance */ | |
| 518 if (mv_scale) { | |
| 519 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); | |
| 520 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); | |
| 521 } else { | |
| 522 mb->mv_x = ref_mb->mv_x; | |
| 523 mb->mv_y = ref_mb->mv_y; | |
| 524 } | |
| 525 } | |
| 526 } else { | |
| 527 if (band->inherit_mv) { | |
| 528 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */ | |
| 529 } else if (ctx->frame_type == FRAMETYPE_INTRA) { | |
| 530 mb->type = 0; /* mb_type is always INTRA for intra-frames */ | |
| 531 } else { | |
| 532 mb->type = get_bits1(&ctx->gb); | |
| 533 } | |
| 534 | |
| 535 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1; | |
| 536 mb->cbp = get_bits(&ctx->gb, blks_per_mb); | |
| 537 | |
| 538 mb->q_delta = 0; | |
| 539 if (band->qdelta_present) { | |
| 540 if (band->inherit_qdelta) { | |
| 541 if (ref_mb) mb->q_delta = ref_mb->q_delta; | |
| 542 } else if (mb->cbp || (!band->plane && !band->band_num && | |
| 543 (ctx->frame_flags & 8))) { | |
| 544 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc->table, | |
| 545 IVI_VLC_BITS, 1); | |
| 546 mb->q_delta = IVI_TOSIGNED(mb->q_delta); | |
| 547 } | |
| 548 } | |
| 549 | |
| 550 if (!mb->type) { | |
| 551 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */ | |
| 552 } else { | |
| 553 if (band->inherit_mv){ | |
| 554 /* motion vector inheritance */ | |
| 555 if (mv_scale) { | |
| 556 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale); | |
| 557 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale); | |
| 558 } else { | |
| 559 mb->mv_x = ref_mb->mv_x; | |
| 560 mb->mv_y = ref_mb->mv_y; | |
| 561 } | |
| 562 } else { | |
| 563 /* decode motion vector deltas */ | |
| 564 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc->table, | |
| 565 IVI_VLC_BITS, 1); | |
| 566 mv_y += IVI_TOSIGNED(mv_delta); | |
| 567 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc->table, | |
| 568 IVI_VLC_BITS, 1); | |
| 569 mv_x += IVI_TOSIGNED(mv_delta); | |
| 570 mb->mv_x = mv_x; | |
| 571 mb->mv_y = mv_y; | |
| 572 } | |
| 573 } | |
| 574 } | |
| 575 | |
| 576 mb++; | |
| 577 if (ref_mb) | |
| 578 ref_mb++; | |
| 579 mb_offset += band->mb_size; | |
| 580 } | |
| 581 | |
| 582 offs += row_offset; | |
| 583 } | |
| 584 | |
| 585 align_get_bits(&ctx->gb); | |
| 586 | |
| 587 return 0; | |
| 588 } | |
| 589 | |
| 590 | |
| 591 /** | |
| 592 * Decodes an Indeo5 band. | |
| 593 * | |
| 594 * @param ctx [in,out] ptr to the decoder context | |
| 595 * @param band [in,out] ptr to the band descriptor | |
| 596 * @param avctx [in] ptr to the AVCodecContext | |
| 597 * @return result code: 0 = OK, -1 = error | |
| 598 */ | |
| 599 static int decode_band(IVI5DecContext *ctx, int plane_num, | |
| 600 IVIBandDesc *band, AVCodecContext *avctx) | |
| 601 { | |
| 602 int result, i, t, idx1, idx2; | |
| 603 IVITile *tile; | |
| 604 | |
| 605 band->buf = band->bufs[ctx->dst_buf]; | |
| 606 band->ref_buf = band->bufs[ctx->ref_buf]; | |
| 607 band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3); | |
| 608 | |
| 609 result = decode_band_hdr(ctx, band, avctx); | |
| 610 if (result) { | |
| 611 av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n", | |
| 612 result); | |
| 613 return -1; | |
| 614 } | |
| 615 | |
| 616 if (band->is_empty) { | |
| 617 av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n"); | |
| 618 return -1; | |
| 619 } | |
| 620 | |
| 621 band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel]; | |
| 622 | |
| 623 /* apply corrections to the selected rvmap table if present */ | |
| 624 for (i = 0; i < band->num_corr; i++) { | |
| 625 idx1 = band->corr[i*2]; | |
| 626 idx2 = band->corr[i*2+1]; | |
| 627 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); | |
| 628 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); | |
| 629 } | |
| 630 | |
| 631 for (t = 0; t < band->num_tiles; t++) { | |
| 632 tile = &band->tiles[t]; | |
| 633 | |
| 634 tile->is_empty = get_bits1(&ctx->gb); | |
| 635 if (tile->is_empty) { | |
| 636 ff_ivi_process_empty_tile(avctx, band, tile, | |
| 637 (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3)); | |
| 638 align_get_bits(&ctx->gb); | |
| 639 } else { | |
| 640 tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb); | |
| 641 | |
| 642 result = decode_mb_info(ctx, band, tile, avctx); | |
| 643 if (result < 0) | |
| 644 break; | |
| 645 | |
| 646 if (band->blk_size == 8) { | |
| 647 band->intra_base = &ivi5_base_quant_8x8_intra[band->quant_mat][0]; | |
| 648 band->inter_base = &ivi5_base_quant_8x8_inter[band->quant_mat][0]; | |
| 649 band->intra_scale = &ivi5_scale_quant_8x8_intra[band->quant_mat][0]; | |
| 650 band->inter_scale = &ivi5_scale_quant_8x8_inter[band->quant_mat][0]; | |
| 651 } else { | |
| 652 band->intra_base = ivi5_base_quant_4x4_intra; | |
| 653 band->inter_base = ivi5_base_quant_4x4_inter; | |
| 654 band->intra_scale = ivi5_scale_quant_4x4_intra; | |
| 655 band->inter_scale = ivi5_scale_quant_4x4_inter; | |
| 656 } | |
| 657 | |
| 658 result = ff_ivi_decode_blocks(&ctx->gb, band, tile); | |
| 659 if (result < 0) { | |
| 660 av_log(avctx, AV_LOG_ERROR, "Corrupted blocks data encountered!\n"); | |
| 661 break; | |
| 662 } | |
| 663 } | |
| 664 } | |
| 665 | |
| 666 /* restore the selected rvmap table by applying its corrections in reverse order */ | |
| 667 for (i = band->num_corr-1; i >= 0; i--) { | |
| 668 idx1 = band->corr[i*2]; | |
| 669 idx2 = band->corr[i*2+1]; | |
| 670 FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]); | |
| 671 FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]); | |
| 672 } | |
| 673 | |
|
11120
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
674 #if IVI_DEBUG |
|
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
675 if (band->checksum_present) { |
|
11121
1d4aeef800d4
Move 'chksum' declaration to the only block where that variable is used
kostya
parents:
11120
diff
changeset
|
676 uint16_t chksum = ivi_calc_band_checksum(band); |
| 11107 | 677 if (chksum != band->checksum) { |
| 678 av_log(avctx, AV_LOG_ERROR, | |
| 679 "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n", | |
| 680 band->plane, band->band_num, band->checksum, chksum); | |
| 681 } | |
| 682 } | |
|
11120
b622564e2d60
Move band checksum verifying into preprocessor condition, so compiler won't
kostya
parents:
11107
diff
changeset
|
683 #endif |
| 11107 | 684 |
| 685 return result; | |
| 686 } | |
| 687 | |
| 688 | |
| 689 /** | |
| 690 * Switches buffers. | |
| 691 * | |
| 692 * @param ctx [in,out] ptr to the decoder context | |
| 693 * @param avctx [in] ptr to the AVCodecContext | |
| 694 */ | |
| 695 static void switch_buffers(IVI5DecContext *ctx, AVCodecContext *avctx) | |
| 696 { | |
| 697 switch (ctx->frame_type) { | |
| 698 case FRAMETYPE_INTRA: | |
| 699 ctx->buf_switch = 0; | |
| 700 ctx->dst_buf = 0; | |
| 701 ctx->ref_buf = 0; | |
| 702 break; | |
| 703 case FRAMETYPE_INTER: | |
| 704 ctx->buf_switch &= 1; | |
| 705 /* swap buffers only if there were no droppable frames */ | |
| 706 if (ctx->prev_frame_type != FRAMETYPE_INTER_NOREF && | |
| 707 ctx->prev_frame_type != FRAMETYPE_INTER_SCAL) | |
| 708 ctx->buf_switch ^= 1; | |
| 709 ctx->dst_buf = ctx->buf_switch; | |
| 710 ctx->ref_buf = ctx->buf_switch ^ 1; | |
| 711 break; | |
| 712 case FRAMETYPE_INTER_SCAL: | |
| 713 if (ctx->prev_frame_type == FRAMETYPE_INTER_NOREF) | |
| 714 break; | |
| 715 if (ctx->prev_frame_type != FRAMETYPE_INTER_SCAL) { | |
| 716 ctx->buf_switch ^= 1; | |
| 717 ctx->dst_buf = ctx->buf_switch; | |
| 718 ctx->ref_buf = ctx->buf_switch ^ 1; | |
| 719 } else { | |
| 720 ctx->buf_switch ^= 2; | |
| 721 ctx->dst_buf = 2; | |
| 722 ctx->ref_buf = ctx->buf_switch & 1; | |
| 723 if (!(ctx->buf_switch & 2)) | |
| 724 FFSWAP(int, ctx->dst_buf, ctx->ref_buf); | |
| 725 } | |
| 726 break; | |
| 727 case FRAMETYPE_INTER_NOREF: | |
| 728 if (ctx->prev_frame_type == FRAMETYPE_INTER_SCAL) { | |
| 729 ctx->buf_switch ^= 2; | |
| 730 ctx->dst_buf = 2; | |
| 731 ctx->ref_buf = ctx->buf_switch & 1; | |
| 732 if (!(ctx->buf_switch & 2)) | |
| 733 FFSWAP(int, ctx->dst_buf, ctx->ref_buf); | |
| 734 } else { | |
| 735 ctx->buf_switch ^= 1; | |
| 736 ctx->dst_buf = ctx->buf_switch & 1; | |
| 737 ctx->ref_buf = (ctx->buf_switch & 1) ^ 1; | |
| 738 } | |
| 739 break; | |
| 740 case FRAMETYPE_NULL: | |
| 741 return; | |
| 742 default: | |
| 743 av_log(avctx, AV_LOG_ERROR, "unsupported frame type: %d\n", ctx->frame_type); | |
| 744 } | |
| 745 } | |
| 746 | |
| 747 | |
| 748 /** | |
| 749 * Initializes Indeo5 decoder. | |
| 750 */ | |
| 751 static av_cold int decode_init(AVCodecContext *avctx) | |
| 752 { | |
| 753 IVI5DecContext *ctx = avctx->priv_data; | |
| 754 int i, result; | |
| 755 | |
| 756 /* initialize static vlc tables for macroblock/block signals */ | |
| 757 for (i = 0; i < 8; i++) { | |
| 758 ff_ivi_create_huff_from_desc(&ff_ivi_mb_huff_desc[i], &mb_vlc_tabs[i], 1); | |
| 759 ff_ivi_create_huff_from_desc(&ff_ivi_blk_huff_desc[i], &blk_vlc_tabs[i], 1); | |
| 760 } | |
| 761 | |
| 762 /* copy rvmap tables in our context so we can apply changes to them */ | |
| 763 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs)); | |
| 764 | |
| 765 /* set the initial picture layout according to the basic profile: | |
| 766 there is only one band per plane (no scalability), only one tile (no local decoding) | |
| 767 and picture format = YVU9 */ | |
| 768 ctx->pic_conf.pic_width = avctx->width; | |
| 769 ctx->pic_conf.pic_height = avctx->height; | |
| 770 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2; | |
| 771 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2; | |
| 772 ctx->pic_conf.tile_width = avctx->width; | |
| 773 ctx->pic_conf.tile_height = avctx->height; | |
| 774 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1; | |
| 775 | |
| 776 result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf); | |
| 777 if (result) { | |
| 778 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n"); | |
| 779 return -1; | |
| 780 } | |
| 781 | |
| 782 avctx->pix_fmt = PIX_FMT_YUV410P; | |
| 783 | |
| 784 return 0; | |
| 785 } | |
| 786 | |
| 787 | |
| 788 /** | |
| 789 * main decoder function | |
| 790 */ | |
| 791 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, | |
| 792 AVPacket *avpkt) | |
| 793 { | |
| 794 IVI5DecContext *ctx = avctx->priv_data; | |
| 795 const uint8_t *buf = avpkt->data; | |
| 796 int buf_size = avpkt->size; | |
| 797 int result, p, b; | |
| 798 | |
| 799 init_get_bits(&ctx->gb, buf, buf_size * 8); | |
| 800 ctx->frame_data = buf; | |
| 801 ctx->frame_size = buf_size; | |
| 802 | |
| 803 result = decode_pic_hdr(ctx, avctx); | |
| 804 if (result) { | |
| 805 av_log(avctx, AV_LOG_ERROR, | |
| 806 "Error while decoding picture header: %d\n", result); | |
| 807 return -1; | |
| 808 } | |
| 809 | |
| 810 if (ctx->gop_flags & IVI5_IS_PROTECTED) { | |
| 811 av_log(avctx, AV_LOG_ERROR, "Password-protected clip!\n"); | |
| 812 return -1; | |
| 813 } | |
| 814 | |
| 815 switch_buffers(ctx, avctx); | |
| 816 | |
| 817 //START_TIMER; | |
| 818 | |
| 819 if (ctx->frame_type == FRAMETYPE_NULL) { | |
| 820 ctx->frame_type = ctx->prev_frame_type; | |
| 821 } else { | |
| 822 for (p = 0; p < 3; p++) { | |
| 823 for (b = 0; b < ctx->planes[p].num_bands; b++) { | |
| 824 result = decode_band(ctx, p, &ctx->planes[p].bands[b], avctx); | |
| 825 if (result) { | |
| 826 av_log(avctx, AV_LOG_ERROR, | |
| 827 "Error while decoding band: %d, plane: %d\n", b, p); | |
| 828 return -1; | |
| 829 } | |
| 830 } | |
| 831 } | |
| 832 } | |
| 833 | |
| 834 //STOP_TIMER("decode_planes"); | |
| 835 | |
| 836 if (ctx->frame.data[0]) | |
| 837 avctx->release_buffer(avctx, &ctx->frame); | |
| 838 | |
| 839 ctx->frame.reference = 0; | |
| 840 if (avctx->get_buffer(avctx, &ctx->frame) < 0) { | |
| 841 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
| 842 return -1; | |
| 843 } | |
| 844 | |
| 845 if (ctx->is_scalable) { | |
| 846 ff_ivi_recompose53 (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0], 4); | |
| 847 } else { | |
| 848 ff_ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]); | |
| 849 } | |
| 850 | |
| 851 ff_ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]); | |
| 852 ff_ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]); | |
| 853 | |
| 854 *data_size = sizeof(AVFrame); | |
| 855 *(AVFrame*)data = ctx->frame; | |
| 856 | |
| 857 return buf_size; | |
| 858 } | |
| 859 | |
| 860 | |
| 861 /** | |
| 862 * Closes Indeo5 decoder and cleans up its context. | |
| 863 */ | |
| 864 static av_cold int decode_close(AVCodecContext *avctx) | |
| 865 { | |
| 866 IVI5DecContext *ctx = avctx->priv_data; | |
| 867 | |
| 868 ff_ivi_free_buffers(&ctx->planes[0]); | |
| 869 | |
| 870 if (ctx->frame.data[0]) | |
| 871 avctx->release_buffer(avctx, &ctx->frame); | |
| 872 | |
| 873 return 0; | |
| 874 } | |
| 875 | |
| 876 | |
| 877 AVCodec indeo5_decoder = { | |
| 878 .name = "indeo5", | |
| 879 .type = CODEC_TYPE_VIDEO, | |
| 880 .id = CODEC_ID_INDEO5, | |
| 881 .priv_data_size = sizeof(IVI5DecContext), | |
| 882 .init = decode_init, | |
| 883 .close = decode_close, | |
| 884 .decode = decode_frame, | |
| 885 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo Video Interactive 5"), | |
| 886 }; |
