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