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