Mercurial > libavcodec.hg
annotate alsdec.c @ 10530:d428e57f14c6 libavcodec
Add long-term prediction to the ALS decoder.
| author | thilo.borgmann |
|---|---|
| date | Sat, 14 Nov 2009 06:29:19 +0000 |
| parents | e5d7b184a5b0 |
| children | 142645a57180 |
| rev | line source |
|---|---|
| 10522 | 1 /* |
| 2 * MPEG-4 ALS decoder | |
| 3 * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com> | |
| 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/alsdec.c | |
| 24 * MPEG-4 ALS decoder | |
| 25 * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com> | |
| 26 */ | |
| 27 | |
| 28 | |
| 29 //#define DEBUG | |
| 30 | |
| 31 | |
| 32 #include "avcodec.h" | |
| 33 #include "get_bits.h" | |
| 34 #include "unary.h" | |
| 35 #include "mpeg4audio.h" | |
| 36 #include "bytestream.h" | |
| 37 | |
| 38 #include "als_data.h" | |
| 39 | |
| 40 enum RA_Flag { | |
| 41 RA_FLAG_NONE, | |
| 42 RA_FLAG_FRAMES, | |
| 43 RA_FLAG_HEADER | |
| 44 }; | |
| 45 | |
| 46 | |
| 47 typedef struct { | |
| 48 uint32_t samples; ///< number of samples, 0xFFFFFFFF if unknown | |
| 49 int resolution; ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit | |
| 50 int floating; ///< 1 = IEEE 32-bit floating-point, 0 = integer | |
| 51 int frame_length; ///< frame length for each frame (last frame may differ) | |
| 52 int ra_distance; ///< distance between RA frames (in frames, 0...255) | |
| 53 enum RA_Flag ra_flag; ///< indicates where the size of ra units is stored | |
| 54 int adapt_order; ///< adaptive order: 1 = on, 0 = off | |
| 55 int coef_table; ///< table index of Rice code parameters | |
| 56 int long_term_prediction; ///< long term prediction (LTP): 1 = on, 0 = off | |
| 57 int max_order; ///< maximum prediction order (0..1023) | |
| 58 int block_switching; ///< number of block switching levels | |
| 59 int bgmc; ///< "Block Gilbert-Moore Code": 1 = on, 0 = off (Rice coding only) | |
| 60 int sb_part; ///< sub-block partition | |
| 61 int joint_stereo; ///< joint stereo: 1 = on, 0 = off | |
| 62 int mc_coding; ///< extended inter-channel coding (multi channel coding): 1 = on, 0 = off | |
| 63 int chan_config; ///< indicates that a chan_config_info field is present | |
| 64 int chan_sort; ///< channel rearrangement: 1 = on, 0 = off | |
| 65 int rlslms; ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off | |
| 66 int chan_config_info; ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented. | |
| 67 int *chan_pos; ///< original channel positions | |
| 68 uint32_t header_size; ///< header size of original audio file in bytes, provided for debugging | |
| 69 uint32_t trailer_size; ///< trailer size of original audio file in bytes, provided for debugging | |
| 70 } ALSSpecificConfig; | |
| 71 | |
| 72 | |
| 73 typedef struct { | |
| 74 AVCodecContext *avctx; | |
| 75 ALSSpecificConfig sconf; | |
| 76 GetBitContext gb; | |
| 77 unsigned int cur_frame_length; ///< length of the current frame to decode | |
| 78 unsigned int frame_id; ///< the frame ID / number of the current frame | |
| 79 unsigned int js_switch; ///< if true, joint-stereo decoding is enforced | |
| 80 unsigned int num_blocks; ///< number of blocks used in the current frame | |
|
10530
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
81 int ltp_lag_length; ///< number of bits used for ltp lag value |
| 10522 | 82 int32_t *quant_cof; ///< quantized parcor coefficients |
| 83 int32_t *lpc_cof; ///< coefficients of the direct form prediction filter | |
| 84 int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block | |
| 85 int32_t **raw_samples; ///< decoded raw samples for each channel | |
| 86 int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples | |
| 87 } ALSDecContext; | |
| 88 | |
| 89 | |
| 90 static av_cold void dprint_specific_config(ALSDecContext *ctx) | |
| 91 { | |
| 92 #ifdef DEBUG | |
| 93 AVCodecContext *avctx = ctx->avctx; | |
| 94 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 95 | |
| 96 dprintf(avctx, "resolution = %i\n", sconf->resolution); | |
| 97 dprintf(avctx, "floating = %i\n", sconf->floating); | |
| 98 dprintf(avctx, "frame_length = %i\n", sconf->frame_length); | |
| 99 dprintf(avctx, "ra_distance = %i\n", sconf->ra_distance); | |
| 100 dprintf(avctx, "ra_flag = %i\n", sconf->ra_flag); | |
| 101 dprintf(avctx, "adapt_order = %i\n", sconf->adapt_order); | |
| 102 dprintf(avctx, "coef_table = %i\n", sconf->coef_table); | |
| 103 dprintf(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction); | |
| 104 dprintf(avctx, "max_order = %i\n", sconf->max_order); | |
| 105 dprintf(avctx, "block_switching = %i\n", sconf->block_switching); | |
| 106 dprintf(avctx, "bgmc = %i\n", sconf->bgmc); | |
| 107 dprintf(avctx, "sb_part = %i\n", sconf->sb_part); | |
| 108 dprintf(avctx, "joint_stereo = %i\n", sconf->joint_stereo); | |
| 109 dprintf(avctx, "mc_coding = %i\n", sconf->mc_coding); | |
| 110 dprintf(avctx, "chan_config = %i\n", sconf->chan_config); | |
| 111 dprintf(avctx, "chan_sort = %i\n", sconf->chan_sort); | |
| 112 dprintf(avctx, "RLSLMS = %i\n", sconf->rlslms); | |
| 113 dprintf(avctx, "chan_config_info = %i\n", sconf->chan_config_info); | |
| 114 dprintf(avctx, "header_size = %i\n", sconf->header_size); | |
| 115 dprintf(avctx, "trailer_size = %i\n", sconf->trailer_size); | |
| 116 #endif | |
| 117 } | |
| 118 | |
| 119 | |
| 120 /** Reads an ALSSpecificConfig from a buffer into the output struct. | |
| 121 */ | |
| 122 static av_cold int read_specific_config(ALSDecContext *ctx) | |
| 123 { | |
| 124 GetBitContext gb; | |
| 125 uint64_t ht_size; | |
| 126 int i, config_offset, crc_enabled; | |
| 127 MPEG4AudioConfig m4ac; | |
| 128 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 129 AVCodecContext *avctx = ctx->avctx; | |
| 130 uint32_t als_id; | |
| 131 | |
| 132 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8); | |
| 133 | |
| 134 config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata, | |
| 135 avctx->extradata_size); | |
| 136 | |
| 137 if (config_offset < 0) | |
| 138 return -1; | |
| 139 | |
| 140 skip_bits_long(&gb, config_offset); | |
| 141 | |
| 142 if (get_bits_left(&gb) < (30 << 3)) | |
| 143 return -1; | |
| 144 | |
| 145 // read the fixed items | |
| 146 als_id = get_bits_long(&gb, 32); | |
| 147 avctx->sample_rate = m4ac.sample_rate; | |
| 148 skip_bits_long(&gb, 32); // sample rate already known | |
| 149 sconf->samples = get_bits_long(&gb, 32); | |
| 150 avctx->channels = m4ac.channels; | |
| 151 skip_bits(&gb, 16); // number of channels already knwon | |
| 152 skip_bits(&gb, 3); // skip file_type | |
| 153 sconf->resolution = get_bits(&gb, 3); | |
| 154 sconf->floating = get_bits1(&gb); | |
| 155 skip_bits1(&gb); // skip msb_first | |
| 156 sconf->frame_length = get_bits(&gb, 16) + 1; | |
| 157 sconf->ra_distance = get_bits(&gb, 8); | |
| 158 sconf->ra_flag = get_bits(&gb, 2); | |
| 159 sconf->adapt_order = get_bits1(&gb); | |
| 160 sconf->coef_table = get_bits(&gb, 2); | |
| 161 sconf->long_term_prediction = get_bits1(&gb); | |
| 162 sconf->max_order = get_bits(&gb, 10); | |
| 163 sconf->block_switching = get_bits(&gb, 2); | |
| 164 sconf->bgmc = get_bits1(&gb); | |
| 165 sconf->sb_part = get_bits1(&gb); | |
| 166 sconf->joint_stereo = get_bits1(&gb); | |
| 167 sconf->mc_coding = get_bits1(&gb); | |
| 168 sconf->chan_config = get_bits1(&gb); | |
| 169 sconf->chan_sort = get_bits1(&gb); | |
| 170 crc_enabled = get_bits1(&gb); | |
| 171 sconf->rlslms = get_bits1(&gb); | |
| 172 skip_bits(&gb, 5); // skip 5 reserved bits | |
| 173 skip_bits1(&gb); // skip aux_data_enabled | |
| 174 | |
| 175 | |
| 176 // check for ALSSpecificConfig struct | |
| 177 if (als_id != MKBETAG('A','L','S','\0')) | |
| 178 return -1; | |
| 179 | |
| 180 ctx->cur_frame_length = sconf->frame_length; | |
| 181 | |
| 182 // allocate quantized parcor coefficient buffer | |
| 183 if (!(ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * sconf->max_order)) || | |
| 184 !(ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * sconf->max_order))) { | |
| 185 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n"); | |
| 186 return AVERROR(ENOMEM); | |
| 187 } | |
| 188 | |
| 189 // read channel config | |
| 190 if (sconf->chan_config) | |
| 191 sconf->chan_config_info = get_bits(&gb, 16); | |
| 192 // TODO: use this to set avctx->channel_layout | |
| 193 | |
| 194 | |
| 195 // read channel sorting | |
| 196 if (sconf->chan_sort && avctx->channels > 1) { | |
| 197 int chan_pos_bits = av_ceil_log2(avctx->channels); | |
| 198 int bits_needed = avctx->channels * chan_pos_bits + 7; | |
| 199 if (get_bits_left(&gb) < bits_needed) | |
| 200 return -1; | |
| 201 | |
| 202 if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos)))) | |
| 203 return AVERROR(ENOMEM); | |
| 204 | |
| 205 for (i = 0; i < avctx->channels; i++) | |
| 206 sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits); | |
| 207 | |
| 208 align_get_bits(&gb); | |
| 209 // TODO: use this to actually do channel sorting | |
| 210 } else { | |
| 211 sconf->chan_sort = 0; | |
| 212 } | |
| 213 | |
| 214 | |
| 215 // read fixed header and trailer sizes, | |
| 216 // if size = 0xFFFFFFFF then there is no data field! | |
| 217 if (get_bits_left(&gb) < 64) | |
| 218 return -1; | |
| 219 | |
| 220 sconf->header_size = get_bits_long(&gb, 32); | |
| 221 sconf->trailer_size = get_bits_long(&gb, 32); | |
| 222 if (sconf->header_size == 0xFFFFFFFF) | |
| 223 sconf->header_size = 0; | |
| 224 if (sconf->trailer_size == 0xFFFFFFFF) | |
| 225 sconf->trailer_size = 0; | |
| 226 | |
| 227 ht_size = ((int64_t)(sconf->header_size) + (int64_t)(sconf->trailer_size)) << 3; | |
| 228 | |
| 229 | |
| 230 // skip the header and trailer data | |
| 231 if (get_bits_left(&gb) < ht_size) | |
| 232 return -1; | |
| 233 | |
| 234 if (ht_size > INT32_MAX) | |
| 235 return -1; | |
| 236 | |
| 237 skip_bits_long(&gb, ht_size); | |
| 238 | |
| 239 | |
| 240 // skip the crc data | |
| 241 if (crc_enabled) { | |
| 242 if (get_bits_left(&gb) < 32) | |
| 243 return -1; | |
| 244 | |
| 245 skip_bits_long(&gb, 32); | |
| 246 } | |
| 247 | |
| 248 | |
| 249 // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data) | |
| 250 | |
| 251 dprint_specific_config(ctx); | |
| 252 | |
| 253 return 0; | |
| 254 } | |
| 255 | |
| 256 | |
| 257 /** Checks the ALSSpecificConfig for unsupported features. | |
| 258 */ | |
| 259 static int check_specific_config(ALSDecContext *ctx) | |
| 260 { | |
| 261 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 262 int error = 0; | |
| 263 | |
| 264 // report unsupported feature and set error value | |
| 265 #define MISSING_ERR(cond, str, errval) \ | |
| 266 { \ | |
| 267 if (cond) { \ | |
| 268 av_log_missing_feature(ctx->avctx, str, 0); \ | |
| 269 error = errval; \ | |
| 270 } \ | |
| 271 } | |
| 272 | |
| 273 MISSING_ERR(sconf->floating, "Floating point decoding", -1); | |
| 274 MISSING_ERR(sconf->bgmc, "BGMC entropy decoding", -1); | |
| 275 MISSING_ERR(sconf->mc_coding, "Multi-channel correlation", -1); | |
| 276 MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1); | |
| 277 MISSING_ERR(sconf->chan_sort, "Channel sorting", 0); | |
| 278 | |
| 279 return error; | |
| 280 } | |
| 281 | |
| 282 | |
| 283 /** Parses the bs_info field to extract the block partitioning used in | |
| 284 * block switching mode, refer to ISO/IEC 14496-3, section 11.6.2. | |
| 285 */ | |
| 286 static void parse_bs_info(const uint32_t bs_info, unsigned int n, | |
| 287 unsigned int div, unsigned int **div_blocks, | |
| 288 unsigned int *num_blocks) | |
| 289 { | |
| 290 if (n < 31 && ((bs_info << n) & 0x40000000)) { | |
| 291 // if the level is valid and the investigated bit n is set | |
| 292 // then recursively check both children at bits (2n+1) and (2n+2) | |
| 293 n *= 2; | |
| 294 div += 1; | |
| 295 parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks); | |
| 296 parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks); | |
| 297 } else { | |
| 298 // else the bit is not set or the last level has been reached | |
| 299 // (bit implicitly not set) | |
| 300 **div_blocks = div; | |
| 301 (*div_blocks)++; | |
| 302 (*num_blocks)++; | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 | |
| 307 /** Reads and decodes a Rice codeword. | |
| 308 */ | |
| 309 static int32_t decode_rice(GetBitContext *gb, unsigned int k) | |
| 310 { | |
| 311 int max = gb->size_in_bits - get_bits_count(gb) - k; | |
| 312 int q = get_unary(gb, 0, max); | |
| 313 int r = k ? get_bits1(gb) : !(q & 1); | |
| 314 | |
| 315 if (k > 1) { | |
| 316 q <<= (k - 1); | |
| 317 q += get_bits_long(gb, k - 1); | |
| 318 } else if (!k) { | |
| 319 q >>= 1; | |
| 320 } | |
| 321 return r ? q : ~q; | |
| 322 } | |
| 323 | |
| 324 | |
| 325 /** Converts PARCOR coefficient k to direct filter coefficient. | |
| 326 */ | |
| 327 static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof) | |
| 328 { | |
| 329 int i, j; | |
| 330 | |
| 331 for (i = 0, j = k - 1; i < j; i++, j--) { | |
| 332 int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20); | |
| 333 cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20); | |
| 334 cof[i] += tmp1; | |
| 335 } | |
| 336 if (i == j) | |
| 337 cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20); | |
| 338 | |
| 339 cof[k] = par[k]; | |
| 340 } | |
| 341 | |
| 342 | |
| 343 /** Reads block switching field if necessary and sets actual block sizes. | |
| 344 * Also assures that the block sizes of the last frame correspond to the | |
| 345 * actual number of samples. | |
| 346 */ | |
| 347 static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks, | |
| 348 uint32_t *bs_info) | |
| 349 { | |
| 350 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 351 GetBitContext *gb = &ctx->gb; | |
| 352 unsigned int *ptr_div_blocks = div_blocks; | |
| 353 unsigned int b; | |
| 354 | |
| 355 if (sconf->block_switching) { | |
| 356 unsigned int bs_info_len = 1 << (sconf->block_switching + 2); | |
| 357 *bs_info = get_bits_long(gb, bs_info_len); | |
| 358 *bs_info <<= (32 - bs_info_len); | |
| 359 } | |
| 360 | |
| 361 ctx->num_blocks = 0; | |
| 362 parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks); | |
| 363 | |
| 364 // The last frame may have an overdetermined block structure given in | |
| 365 // the bitstream. In that case the defined block structure would need | |
| 366 // more samples than available to be consistent. | |
| 367 // The block structure is actually used but the block sizes are adapted | |
| 368 // to fit the actual number of available samples. | |
| 369 // Example: 5 samples, 2nd level block sizes: 2 2 2 2. | |
| 370 // This results in the actual block sizes: 2 2 1 0. | |
| 371 // This is not specified in 14496-3 but actually done by the reference | |
| 372 // codec RM22 revision 2. | |
| 373 // This appears to happen in case of an odd number of samples in the last | |
| 374 // frame which is actually not allowed by the block length switching part | |
| 375 // of 14496-3. | |
| 376 // The ALS conformance files feature an odd number of samples in the last | |
| 377 // frame. | |
| 378 | |
| 379 for (b = 0; b < ctx->num_blocks; b++) | |
| 380 div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b]; | |
| 381 | |
| 382 if (ctx->cur_frame_length != ctx->sconf.frame_length) { | |
| 383 unsigned int remaining = ctx->cur_frame_length; | |
| 384 | |
| 385 for (b = 0; b < ctx->num_blocks; b++) { | |
| 386 if (remaining < div_blocks[b]) { | |
| 387 div_blocks[b] = remaining; | |
| 388 ctx->num_blocks = b + 1; | |
| 389 break; | |
| 390 } | |
| 391 | |
| 392 remaining -= div_blocks[b]; | |
| 393 } | |
| 394 } | |
| 395 } | |
| 396 | |
| 397 | |
| 398 /** Reads the block data for a constant block | |
| 399 */ | |
| 400 static void read_const_block(ALSDecContext *ctx, int32_t *raw_samples, | |
| 401 unsigned int block_length, unsigned int *js_blocks) | |
| 402 { | |
| 403 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 404 AVCodecContext *avctx = ctx->avctx; | |
| 405 GetBitContext *gb = &ctx->gb; | |
| 406 int32_t const_val = 0; | |
| 407 unsigned int const_block, k; | |
| 408 | |
| 409 const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence) | |
| 410 *js_blocks = get_bits1(gb); | |
| 411 | |
| 412 // skip 5 reserved bits | |
| 413 skip_bits(gb, 5); | |
| 414 | |
| 415 if (const_block) { | |
| 416 unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample; | |
| 417 const_val = get_sbits_long(gb, const_val_bits); | |
| 418 } | |
| 419 | |
| 420 // write raw samples into buffer | |
| 421 for (k = 0; k < block_length; k++) | |
| 422 raw_samples[k] = const_val; | |
| 423 } | |
| 424 | |
| 425 | |
| 426 /** Reads the block data for a non-constant block | |
| 427 */ | |
| 428 static int read_var_block(ALSDecContext *ctx, unsigned int ra_block, | |
| 429 int32_t *raw_samples, unsigned int block_length, | |
| 430 unsigned int *js_blocks, int32_t *raw_other, | |
| 431 unsigned int *shift_lsbs) | |
| 432 { | |
| 433 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 434 AVCodecContext *avctx = ctx->avctx; | |
| 435 GetBitContext *gb = &ctx->gb; | |
| 436 unsigned int k; | |
| 437 unsigned int s[8]; | |
| 438 unsigned int sub_blocks, log2_sub_blocks, sb_length; | |
| 439 unsigned int opt_order = 1; | |
| 440 int32_t *quant_cof = ctx->quant_cof; | |
| 441 int32_t *lpc_cof = ctx->lpc_cof; | |
| 442 unsigned int start = 0; | |
| 443 int smp = 0; | |
| 444 int sb, store_prev_samples; | |
| 445 int64_t y; | |
|
10530
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
446 int use_ltp = 0; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
447 int ltp_lag = 0; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
448 int ltp_gain[5]; |
| 10522 | 449 |
| 450 *js_blocks = get_bits1(gb); | |
| 451 | |
| 452 // determine the number of subblocks for entropy decoding | |
| 453 if (!sconf->bgmc && !sconf->sb_part) { | |
| 454 log2_sub_blocks = 0; | |
| 455 } else { | |
| 456 if (sconf->bgmc && sconf->sb_part) | |
| 457 log2_sub_blocks = get_bits(gb, 2); | |
| 458 else | |
| 459 log2_sub_blocks = 2 * get_bits1(gb); | |
| 460 } | |
| 461 | |
| 462 sub_blocks = 1 << log2_sub_blocks; | |
| 463 | |
| 464 // do not continue in case of a damaged stream since | |
| 465 // block_length must be evenly divisible by sub_blocks | |
| 466 if (block_length & (sub_blocks - 1)) { | |
| 467 av_log(avctx, AV_LOG_WARNING, | |
| 468 "Block length is not evenly divisible by the number of subblocks.\n"); | |
| 469 return -1; | |
| 470 } | |
| 471 | |
| 472 sb_length = block_length >> log2_sub_blocks; | |
| 473 | |
| 474 | |
| 475 if (sconf->bgmc) { | |
| 476 // TODO: BGMC mode | |
| 477 } else { | |
| 478 s[0] = get_bits(gb, 4 + (sconf->resolution > 1)); | |
| 479 for (k = 1; k < sub_blocks; k++) | |
| 480 s[k] = s[k - 1] + decode_rice(gb, 0); | |
| 481 } | |
| 482 | |
| 483 if (get_bits1(gb)) | |
| 484 *shift_lsbs = get_bits(gb, 4) + 1; | |
| 485 | |
| 486 store_prev_samples = (*js_blocks && raw_other) || *shift_lsbs; | |
| 487 | |
| 488 | |
| 489 if (!sconf->rlslms) { | |
| 490 if (sconf->adapt_order) { | |
| 491 int opt_order_length = av_ceil_log2(av_clip((block_length >> 3) - 1, | |
| 492 2, sconf->max_order + 1)); | |
| 493 opt_order = get_bits(gb, opt_order_length); | |
| 494 } else { | |
| 495 opt_order = sconf->max_order; | |
| 496 } | |
| 497 | |
| 498 if (opt_order) { | |
| 499 int add_base; | |
| 500 | |
| 501 if (sconf->coef_table == 3) { | |
| 502 add_base = 0x7F; | |
| 503 | |
| 504 // read coefficient 0 | |
| 505 quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)]; | |
| 506 | |
| 507 // read coefficient 1 | |
| 508 if (opt_order > 1) | |
| 509 quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)]; | |
| 510 | |
| 511 // read coefficients 2 to opt_order | |
| 512 for (k = 2; k < opt_order; k++) | |
| 513 quant_cof[k] = get_bits(gb, 7); | |
| 514 } else { | |
| 515 int k_max; | |
| 516 add_base = 1; | |
| 517 | |
| 518 // read coefficient 0 to 19 | |
| 519 k_max = FFMIN(opt_order, 20); | |
| 520 for (k = 0; k < k_max; k++) { | |
| 521 int rice_param = parcor_rice_table[sconf->coef_table][k][1]; | |
| 522 int offset = parcor_rice_table[sconf->coef_table][k][0]; | |
| 523 quant_cof[k] = decode_rice(gb, rice_param) + offset; | |
| 524 } | |
| 525 | |
| 526 // read coefficients 20 to 126 | |
| 527 k_max = FFMIN(opt_order, 127); | |
| 528 for (; k < k_max; k++) | |
| 529 quant_cof[k] = decode_rice(gb, 2) + (k & 1); | |
| 530 | |
| 531 // read coefficients 127 to opt_order | |
| 532 for (; k < opt_order; k++) | |
| 533 quant_cof[k] = decode_rice(gb, 1); | |
| 534 | |
| 535 quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64]; | |
| 536 | |
| 537 if (opt_order > 1) | |
| 538 quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64]; | |
| 539 } | |
| 540 | |
| 541 for (k = 2; k < opt_order; k++) | |
| 542 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13); | |
| 543 } | |
| 544 } | |
| 545 | |
|
10530
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
546 // read LTP gain and lag values |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
547 if (sconf->long_term_prediction) { |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
548 use_ltp = get_bits1(gb); |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
549 |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
550 if (use_ltp) { |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
551 ltp_gain[0] = decode_rice(gb, 1) << 3; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
552 ltp_gain[1] = decode_rice(gb, 2) << 3; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
553 |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
554 ltp_gain[2] = ltp_gain_values[get_unary(gb, 0, 4)][get_bits(gb, 2)]; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
555 |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
556 ltp_gain[3] = decode_rice(gb, 2) << 3; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
557 ltp_gain[4] = decode_rice(gb, 1) << 3; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
558 |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
559 ltp_lag = get_bits(gb, ctx->ltp_lag_length); |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
560 ltp_lag += FFMAX(4, opt_order + 1); |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
561 } |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
562 } |
| 10522 | 563 |
| 564 // read first value and residuals in case of a random access block | |
| 565 if (ra_block) { | |
| 566 if (opt_order) | |
| 567 raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4); | |
| 568 if (opt_order > 1) | |
| 569 raw_samples[1] = decode_rice(gb, s[0] + 3); | |
| 570 if (opt_order > 2) | |
| 571 raw_samples[2] = decode_rice(gb, s[0] + 1); | |
| 572 | |
| 573 start = FFMIN(opt_order, 3); | |
| 574 } | |
| 575 | |
| 576 // read all residuals | |
| 577 if (sconf->bgmc) { | |
| 578 // TODO: BGMC mode | |
| 579 } else { | |
| 580 int32_t *current_res = raw_samples + start; | |
| 581 | |
| 582 for (sb = 0; sb < sub_blocks; sb++, start = 0) | |
| 583 for (; start < sb_length; start++) | |
| 584 *current_res++ = decode_rice(gb, s[sb]); | |
| 585 } | |
| 586 | |
|
10530
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
587 // reverse long-term prediction |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
588 if (use_ltp) { |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
589 int ltp_smp; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
590 |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
591 for (ltp_smp = FFMAX(ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) { |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
592 int center = ltp_smp - ltp_lag; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
593 int begin = FFMAX(0, center - 2); |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
594 int end = center + 3; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
595 int tab = 5 - (end - begin); |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
596 int base; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
597 |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
598 y = 1 << 6; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
599 |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
600 for (base = begin; base < end; base++, tab++) |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
601 y += MUL64(ltp_gain[tab], raw_samples[base]); |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
602 |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
603 raw_samples[ltp_smp] += y >> 7; |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
604 } |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
605 } |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
606 |
| 10522 | 607 // reconstruct all samples from residuals |
| 608 if (ra_block) { | |
| 609 for (smp = 0; smp < opt_order; smp++) { | |
| 610 y = 1 << 19; | |
| 611 | |
| 612 for (sb = 0; sb < smp; sb++) | |
| 613 y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]); | |
| 614 | |
| 615 raw_samples[smp] -= y >> 20; | |
| 616 parcor_to_lpc(smp, quant_cof, lpc_cof); | |
| 617 } | |
| 618 } else { | |
| 619 for (k = 0; k < opt_order; k++) | |
| 620 parcor_to_lpc(k, quant_cof, lpc_cof); | |
| 621 | |
| 622 // store previous samples in case that they have to be altered | |
| 623 if (store_prev_samples) | |
| 624 memcpy(ctx->prev_raw_samples, raw_samples - sconf->max_order, | |
| 625 sizeof(*ctx->prev_raw_samples) * sconf->max_order); | |
| 626 | |
| 627 // reconstruct difference signal for prediction (joint-stereo) | |
| 628 if (*js_blocks && raw_other) { | |
| 629 int32_t *left, *right; | |
| 630 | |
| 631 if (raw_other > raw_samples) { // D = R - L | |
| 632 left = raw_samples; | |
| 633 right = raw_other; | |
| 634 } else { // D = R - L | |
| 635 left = raw_other; | |
| 636 right = raw_samples; | |
| 637 } | |
| 638 | |
| 639 for (sb = -1; sb >= -sconf->max_order; sb--) | |
| 640 raw_samples[sb] = right[sb] - left[sb]; | |
| 641 } | |
| 642 | |
| 643 // reconstruct shifted signal | |
| 644 if (*shift_lsbs) | |
| 645 for (sb = -1; sb >= -sconf->max_order; sb--) | |
| 646 raw_samples[sb] >>= *shift_lsbs; | |
| 647 } | |
| 648 | |
| 649 // reconstruct raw samples | |
| 650 for (; smp < block_length; smp++) { | |
| 651 y = 1 << 19; | |
| 652 | |
| 653 for (sb = 0; sb < opt_order; sb++) | |
| 654 y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]); | |
| 655 | |
| 656 raw_samples[smp] -= y >> 20; | |
| 657 } | |
| 658 | |
| 659 // restore previous samples in case that they have been altered | |
| 660 if (store_prev_samples) | |
| 661 memcpy(raw_samples - sconf->max_order, ctx->prev_raw_samples, | |
| 662 sizeof(*raw_samples) * sconf->max_order); | |
| 663 | |
| 664 return 0; | |
| 665 } | |
| 666 | |
| 667 | |
| 668 /** Reads the block data. | |
| 669 */ | |
| 670 static int read_block_data(ALSDecContext *ctx, unsigned int ra_block, | |
| 671 int32_t *raw_samples, unsigned int block_length, | |
| 672 unsigned int *js_blocks, int32_t *raw_other) | |
| 673 { | |
| 674 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 675 GetBitContext *gb = &ctx->gb; | |
| 676 unsigned int shift_lsbs = 0; | |
| 677 unsigned int k; | |
| 678 | |
| 679 // read block type flag and read the samples accordingly | |
| 680 if (get_bits1(gb)) { | |
| 681 if (read_var_block(ctx, ra_block, raw_samples, block_length, js_blocks, | |
| 682 raw_other, &shift_lsbs)) | |
| 683 return -1; | |
| 684 } else { | |
| 685 read_const_block(ctx, raw_samples, block_length, js_blocks); | |
| 686 } | |
| 687 | |
| 688 // TODO: read RLSLMS extension data | |
| 689 | |
| 690 if (!sconf->mc_coding || ctx->js_switch) | |
| 691 align_get_bits(gb); | |
| 692 | |
| 693 if (shift_lsbs) | |
| 694 for (k = 0; k < block_length; k++) | |
| 695 raw_samples[k] <<= shift_lsbs; | |
| 696 | |
| 697 return 0; | |
| 698 } | |
| 699 | |
| 700 | |
| 701 /** Computes the number of samples left to decode for the current frame and | |
| 702 * sets these samples to zero. | |
| 703 */ | |
| 704 static void zero_remaining(unsigned int b, unsigned int b_max, | |
| 705 const unsigned int *div_blocks, int32_t *buf) | |
| 706 { | |
| 707 unsigned int count = 0; | |
| 708 | |
| 709 while (b < b_max) | |
| 710 count += div_blocks[b]; | |
| 711 | |
| 10523 | 712 if (count) |
| 10524 | 713 memset(buf, 0, sizeof(*buf) * count); |
| 10522 | 714 } |
| 715 | |
| 716 | |
| 717 /** Decodes blocks independently. | |
| 718 */ | |
| 719 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame, | |
| 720 unsigned int c, const unsigned int *div_blocks, | |
| 721 unsigned int *js_blocks) | |
| 722 { | |
| 723 int32_t *raw_sample; | |
| 724 unsigned int b; | |
| 725 raw_sample = ctx->raw_samples[c]; | |
| 726 | |
| 727 for (b = 0; b < ctx->num_blocks; b++) { | |
| 728 if (read_block_data(ctx, ra_frame, raw_sample, | |
| 729 div_blocks[b], &js_blocks[0], NULL)) { | |
| 730 // damaged block, write zero for the rest of the frame | |
| 731 zero_remaining(b, ctx->num_blocks, div_blocks, raw_sample); | |
| 732 return -1; | |
| 733 } | |
| 734 raw_sample += div_blocks[b]; | |
| 735 ra_frame = 0; | |
| 736 } | |
| 737 | |
| 738 return 0; | |
| 739 } | |
| 740 | |
| 741 | |
| 742 /** Decodes blocks dependently. | |
| 743 */ | |
| 744 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame, | |
| 745 unsigned int c, const unsigned int *div_blocks, | |
| 746 unsigned int *js_blocks) | |
| 747 { | |
| 748 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 749 unsigned int offset = 0; | |
| 750 int32_t *raw_samples_R; | |
| 751 int32_t *raw_samples_L; | |
| 752 unsigned int b; | |
| 753 | |
| 754 // decode all blocks | |
| 755 for (b = 0; b < ctx->num_blocks; b++) { | |
| 756 unsigned int s; | |
| 757 raw_samples_L = ctx->raw_samples[c ] + offset; | |
| 758 raw_samples_R = ctx->raw_samples[c + 1] + offset; | |
| 759 if (read_block_data(ctx, ra_frame, raw_samples_L, div_blocks[b], | |
| 760 &js_blocks[0], raw_samples_R) || | |
| 761 read_block_data(ctx, ra_frame, raw_samples_R, div_blocks[b], | |
| 762 &js_blocks[1], raw_samples_L)) { | |
| 763 // damaged block, write zero for the rest of the frame | |
| 764 zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_L); | |
| 765 zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_R); | |
| 766 return -1; | |
| 767 } | |
| 768 | |
| 769 // reconstruct joint-stereo blocks | |
| 770 if (js_blocks[0]) { | |
| 771 if (js_blocks[1]) | |
| 772 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n"); | |
| 773 | |
| 774 for (s = 0; s < div_blocks[b]; s++) | |
| 775 raw_samples_L[s] = raw_samples_R[s] - raw_samples_L[s]; | |
| 776 } else if (js_blocks[1]) { | |
| 777 for (s = 0; s < div_blocks[b]; s++) | |
| 778 raw_samples_R[s] = raw_samples_R[s] + raw_samples_L[s]; | |
| 779 } | |
| 780 | |
| 781 offset += div_blocks[b]; | |
| 782 ra_frame = 0; | |
| 783 } | |
| 784 | |
| 785 // store carryover raw samples, | |
| 786 // the others channel raw samples are stored by the calling function. | |
| 787 memmove(ctx->raw_samples[c] - sconf->max_order, | |
| 788 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length, | |
| 789 sizeof(*ctx->raw_samples[c]) * sconf->max_order); | |
| 790 | |
| 791 return 0; | |
| 792 } | |
| 793 | |
| 794 | |
| 795 /** Reads the frame data. | |
| 796 */ | |
| 797 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame) | |
| 798 { | |
| 799 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 800 AVCodecContext *avctx = ctx->avctx; | |
| 801 GetBitContext *gb = &ctx->gb; | |
| 802 unsigned int div_blocks[32]; ///< block sizes. | |
| 803 unsigned int c; | |
| 804 unsigned int js_blocks[2]; | |
| 805 | |
| 806 uint32_t bs_info = 0; | |
| 807 | |
| 808 // skip the size of the ra unit if present in the frame | |
| 809 if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame) | |
| 810 skip_bits_long(gb, 32); | |
| 811 | |
| 812 if (sconf->mc_coding && sconf->joint_stereo) { | |
| 813 ctx->js_switch = get_bits1(gb); | |
| 814 align_get_bits(gb); | |
| 815 } | |
| 816 | |
| 817 if (!sconf->mc_coding || ctx->js_switch) { | |
| 818 int independent_bs = !sconf->joint_stereo; | |
| 819 | |
| 820 for (c = 0; c < avctx->channels; c++) { | |
| 821 js_blocks[0] = 0; | |
| 822 js_blocks[1] = 0; | |
| 823 | |
| 824 get_block_sizes(ctx, div_blocks, &bs_info); | |
| 825 | |
| 826 // if joint_stereo and block_switching is set, independent decoding | |
| 827 // is signaled via the first bit of bs_info | |
| 828 if (sconf->joint_stereo && sconf->block_switching) | |
| 829 if (bs_info >> 31) | |
| 830 independent_bs = 2; | |
| 831 | |
| 832 // if this is the last channel, it has to be decoded independently | |
| 833 if (c == avctx->channels - 1) | |
| 834 independent_bs = 1; | |
| 835 | |
| 836 if (independent_bs) { | |
| 837 if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks)) | |
| 838 return -1; | |
| 839 | |
| 840 independent_bs--; | |
| 841 } else { | |
| 842 if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks)) | |
| 843 return -1; | |
| 844 | |
| 845 c++; | |
| 846 } | |
| 847 | |
| 848 // store carryover raw samples | |
| 849 memmove(ctx->raw_samples[c] - sconf->max_order, | |
| 850 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length, | |
| 851 sizeof(*ctx->raw_samples[c]) * sconf->max_order); | |
| 852 } | |
| 853 } else { // multi-channel coding | |
| 854 get_block_sizes(ctx, div_blocks, &bs_info); | |
| 855 | |
| 856 // TODO: multi channel coding might use a temporary buffer instead as | |
| 857 // the actual channel is not known when read_block-data is called | |
| 858 if (decode_blocks_ind(ctx, ra_frame, 0, div_blocks, js_blocks)) | |
| 859 return -1; | |
| 860 // TODO: read_channel_data | |
| 861 } | |
| 862 | |
| 863 // TODO: read_diff_float_data | |
| 864 | |
| 865 return 0; | |
| 866 } | |
| 867 | |
| 868 | |
| 869 /** Decodes an ALS frame. | |
| 870 */ | |
| 871 static int decode_frame(AVCodecContext *avctx, | |
| 872 void *data, int *data_size, | |
| 873 AVPacket *avpkt) | |
| 874 { | |
| 875 ALSDecContext *ctx = avctx->priv_data; | |
| 876 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 877 const uint8_t *buffer = avpkt->data; | |
| 878 int buffer_size = avpkt->size; | |
| 879 int invalid_frame, size; | |
| 880 unsigned int c, sample, ra_frame, bytes_read, shift; | |
| 881 | |
| 882 init_get_bits(&ctx->gb, buffer, buffer_size * 8); | |
| 883 | |
| 884 // In the case that the distance between random access frames is set to zero | |
| 885 // (sconf->ra_distance == 0) no frame is treated as a random access frame. | |
| 886 // For the first frame, if prediction is used, all samples used from the | |
| 887 // previous frame are assumed to be zero. | |
| 888 ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance); | |
| 889 | |
| 890 // the last frame to decode might have a different length | |
| 891 if (sconf->samples != 0xFFFFFFFF) | |
| 892 ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length, | |
| 893 sconf->frame_length); | |
| 894 else | |
| 895 ctx->cur_frame_length = sconf->frame_length; | |
| 896 | |
| 897 // decode the frame data | |
| 898 if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0)) | |
| 899 av_log(ctx->avctx, AV_LOG_WARNING, | |
| 900 "Reading frame data failed. Skipping RA unit.\n"); | |
| 901 | |
| 902 ctx->frame_id++; | |
| 903 | |
| 904 // check for size of decoded data | |
| 905 size = ctx->cur_frame_length * avctx->channels * | |
| 906 (av_get_bits_per_sample_format(avctx->sample_fmt) >> 3); | |
| 907 | |
| 908 if (size > *data_size) { | |
| 909 av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n"); | |
| 910 return -1; | |
| 911 } | |
| 912 | |
| 913 *data_size = size; | |
| 914 | |
| 915 // transform decoded frame into output format | |
| 916 #define INTERLEAVE_OUTPUT(bps) \ | |
| 917 { \ | |
| 918 int##bps##_t *dest = (int##bps##_t*) data; \ | |
| 919 shift = bps - ctx->avctx->bits_per_raw_sample; \ | |
| 920 for (sample = 0; sample < ctx->cur_frame_length; sample++) \ | |
| 921 for (c = 0; c < avctx->channels; c++) \ | |
| 922 *dest++ = ctx->raw_samples[c][sample] << shift; \ | |
| 923 } | |
| 924 | |
| 925 if (ctx->avctx->bits_per_raw_sample <= 16) { | |
| 926 INTERLEAVE_OUTPUT(16) | |
| 927 } else { | |
| 928 INTERLEAVE_OUTPUT(32) | |
| 929 } | |
| 930 | |
| 931 bytes_read = invalid_frame ? buffer_size : | |
| 932 (get_bits_count(&ctx->gb) + 7) >> 3; | |
| 933 | |
| 934 return bytes_read; | |
| 935 } | |
| 936 | |
| 937 | |
| 938 /** Uninitializes the ALS decoder. | |
| 939 */ | |
| 940 static av_cold int decode_end(AVCodecContext *avctx) | |
| 941 { | |
| 942 ALSDecContext *ctx = avctx->priv_data; | |
| 943 | |
| 944 av_freep(&ctx->sconf.chan_pos); | |
| 945 | |
| 946 av_freep(&ctx->quant_cof); | |
| 947 av_freep(&ctx->lpc_cof); | |
| 948 av_freep(&ctx->prev_raw_samples); | |
| 949 av_freep(&ctx->raw_samples); | |
| 950 av_freep(&ctx->raw_buffer); | |
| 951 | |
| 952 return 0; | |
| 953 } | |
| 954 | |
| 955 | |
| 956 /** Initializes the ALS decoder. | |
| 957 */ | |
| 958 static av_cold int decode_init(AVCodecContext *avctx) | |
| 959 { | |
| 960 unsigned int c; | |
| 961 unsigned int channel_size; | |
| 962 ALSDecContext *ctx = avctx->priv_data; | |
| 963 ALSSpecificConfig *sconf = &ctx->sconf; | |
| 964 ctx->avctx = avctx; | |
| 965 | |
| 966 if (!avctx->extradata) { | |
| 967 av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n"); | |
| 968 return -1; | |
| 969 } | |
| 970 | |
| 971 if (read_specific_config(ctx)) { | |
| 972 av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n"); | |
| 973 decode_end(avctx); | |
| 974 return -1; | |
| 975 } | |
| 976 | |
| 977 if (check_specific_config(ctx)) { | |
| 978 decode_end(avctx); | |
| 979 return -1; | |
| 980 } | |
| 981 | |
| 982 if (sconf->floating) { | |
| 983 avctx->sample_fmt = SAMPLE_FMT_FLT; | |
| 984 avctx->bits_per_raw_sample = 32; | |
| 985 } else { | |
| 986 avctx->sample_fmt = sconf->resolution > 1 | |
| 987 ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16; | |
| 988 avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8; | |
| 989 } | |
| 990 | |
|
10530
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
991 // set lag value for long-term prediction |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
992 ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) + |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
993 (avctx->sample_rate >= 192000); |
|
d428e57f14c6
Add long-term prediction to the ALS decoder.
thilo.borgmann
parents:
10524
diff
changeset
|
994 |
| 10522 | 995 avctx->frame_size = sconf->frame_length; |
| 996 channel_size = sconf->frame_length + sconf->max_order; | |
| 997 | |
| 998 ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order); | |
| 999 ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size); | |
| 1000 ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels); | |
| 1001 | |
| 1002 // allocate previous raw sample buffer | |
| 1003 if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) { | |
| 1004 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n"); | |
| 1005 decode_end(avctx); | |
| 1006 return AVERROR(ENOMEM); | |
| 1007 } | |
| 1008 | |
| 1009 // assign raw samples buffers | |
| 1010 ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order; | |
| 1011 for (c = 1; c < avctx->channels; c++) | |
| 1012 ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size; | |
| 1013 | |
| 1014 return 0; | |
| 1015 } | |
| 1016 | |
| 1017 | |
| 1018 /** Flushes (resets) the frame ID after seeking. | |
| 1019 */ | |
| 1020 static av_cold void flush(AVCodecContext *avctx) | |
| 1021 { | |
| 1022 ALSDecContext *ctx = avctx->priv_data; | |
| 1023 | |
| 1024 ctx->frame_id = 0; | |
| 1025 } | |
| 1026 | |
| 1027 | |
| 1028 AVCodec als_decoder = { | |
| 1029 "als", | |
| 1030 CODEC_TYPE_AUDIO, | |
| 1031 CODEC_ID_MP4ALS, | |
| 1032 sizeof(ALSDecContext), | |
| 1033 decode_init, | |
| 1034 NULL, | |
| 1035 decode_end, | |
| 1036 decode_frame, | |
| 1037 .flush = flush, | |
| 1038 .capabilities = CODEC_CAP_SUBFRAMES, | |
| 1039 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"), | |
| 1040 }; | |
| 1041 |
