00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029
00030
00031
00032 #include "avcodec.h"
00033 #include "get_bits.h"
00034 #include "unary.h"
00035 #include "mpeg4audio.h"
00036 #include "bytestream.h"
00037 #include "bgmc.h"
00038 #include "dsputil.h"
00039 #include "libavutil/samplefmt.h"
00040 #include "libavutil/crc.h"
00041
00042 #include <stdint.h>
00043
00048 static const int8_t parcor_rice_table[3][20][2] = {
00049 { {-52, 4}, {-29, 5}, {-31, 4}, { 19, 4}, {-16, 4},
00050 { 12, 3}, { -7, 3}, { 9, 3}, { -5, 3}, { 6, 3},
00051 { -4, 3}, { 3, 3}, { -3, 2}, { 3, 2}, { -2, 2},
00052 { 3, 2}, { -1, 2}, { 2, 2}, { -1, 2}, { 2, 2} },
00053 { {-58, 3}, {-42, 4}, {-46, 4}, { 37, 5}, {-36, 4},
00054 { 29, 4}, {-29, 4}, { 25, 4}, {-23, 4}, { 20, 4},
00055 {-17, 4}, { 16, 4}, {-12, 4}, { 12, 3}, {-10, 4},
00056 { 7, 3}, { -4, 4}, { 3, 3}, { -1, 3}, { 1, 3} },
00057 { {-59, 3}, {-45, 5}, {-50, 4}, { 38, 4}, {-39, 4},
00058 { 32, 4}, {-30, 4}, { 25, 3}, {-23, 3}, { 20, 3},
00059 {-20, 3}, { 16, 3}, {-13, 3}, { 10, 3}, { -7, 3},
00060 { 3, 3}, { 0, 3}, { -1, 3}, { 2, 3}, { -1, 2} }
00061 };
00062
00063
00069 static const int16_t parcor_scaled_values[] = {
00070 -1048544 / 32, -1048288 / 32, -1047776 / 32, -1047008 / 32,
00071 -1045984 / 32, -1044704 / 32, -1043168 / 32, -1041376 / 32,
00072 -1039328 / 32, -1037024 / 32, -1034464 / 32, -1031648 / 32,
00073 -1028576 / 32, -1025248 / 32, -1021664 / 32, -1017824 / 32,
00074 -1013728 / 32, -1009376 / 32, -1004768 / 32, -999904 / 32,
00075 -994784 / 32, -989408 / 32, -983776 / 32, -977888 / 32,
00076 -971744 / 32, -965344 / 32, -958688 / 32, -951776 / 32,
00077 -944608 / 32, -937184 / 32, -929504 / 32, -921568 / 32,
00078 -913376 / 32, -904928 / 32, -896224 / 32, -887264 / 32,
00079 -878048 / 32, -868576 / 32, -858848 / 32, -848864 / 32,
00080 -838624 / 32, -828128 / 32, -817376 / 32, -806368 / 32,
00081 -795104 / 32, -783584 / 32, -771808 / 32, -759776 / 32,
00082 -747488 / 32, -734944 / 32, -722144 / 32, -709088 / 32,
00083 -695776 / 32, -682208 / 32, -668384 / 32, -654304 / 32,
00084 -639968 / 32, -625376 / 32, -610528 / 32, -595424 / 32,
00085 -580064 / 32, -564448 / 32, -548576 / 32, -532448 / 32,
00086 -516064 / 32, -499424 / 32, -482528 / 32, -465376 / 32,
00087 -447968 / 32, -430304 / 32, -412384 / 32, -394208 / 32,
00088 -375776 / 32, -357088 / 32, -338144 / 32, -318944 / 32,
00089 -299488 / 32, -279776 / 32, -259808 / 32, -239584 / 32,
00090 -219104 / 32, -198368 / 32, -177376 / 32, -156128 / 32,
00091 -134624 / 32, -112864 / 32, -90848 / 32, -68576 / 32,
00092 -46048 / 32, -23264 / 32, -224 / 32, 23072 / 32,
00093 46624 / 32, 70432 / 32, 94496 / 32, 118816 / 32,
00094 143392 / 32, 168224 / 32, 193312 / 32, 218656 / 32,
00095 244256 / 32, 270112 / 32, 296224 / 32, 322592 / 32,
00096 349216 / 32, 376096 / 32, 403232 / 32, 430624 / 32,
00097 458272 / 32, 486176 / 32, 514336 / 32, 542752 / 32,
00098 571424 / 32, 600352 / 32, 629536 / 32, 658976 / 32,
00099 688672 / 32, 718624 / 32, 748832 / 32, 779296 / 32,
00100 810016 / 32, 840992 / 32, 872224 / 32, 903712 / 32,
00101 935456 / 32, 967456 / 32, 999712 / 32, 1032224 / 32
00102 };
00103
00104
00108 static const uint8_t ltp_gain_values [4][4] = {
00109 { 0, 8, 16, 24},
00110 {32, 40, 48, 56},
00111 {64, 70, 76, 82},
00112 {88, 92, 96, 100}
00113 };
00114
00115
00119 static const int16_t mcc_weightings[] = {
00120 204, 192, 179, 166, 153, 140, 128, 115,
00121 102, 89, 76, 64, 51, 38, 25, 12,
00122 0, -12, -25, -38, -51, -64, -76, -89,
00123 -102, -115, -128, -140, -153, -166, -179, -192
00124 };
00125
00126
00129 static const uint8_t tail_code[16][6] = {
00130 { 74, 44, 25, 13, 7, 3},
00131 { 68, 42, 24, 13, 7, 3},
00132 { 58, 39, 23, 13, 7, 3},
00133 {126, 70, 37, 19, 10, 5},
00134 {132, 70, 37, 20, 10, 5},
00135 {124, 70, 38, 20, 10, 5},
00136 {120, 69, 37, 20, 11, 5},
00137 {116, 67, 37, 20, 11, 5},
00138 {108, 66, 36, 20, 10, 5},
00139 {102, 62, 36, 20, 10, 5},
00140 { 88, 58, 34, 19, 10, 5},
00141 {162, 89, 49, 25, 13, 7},
00142 {156, 87, 49, 26, 14, 7},
00143 {150, 86, 47, 26, 14, 7},
00144 {142, 84, 47, 26, 14, 7},
00145 {131, 79, 46, 26, 14, 7}
00146 };
00147
00148
00149 enum RA_Flag {
00150 RA_FLAG_NONE,
00151 RA_FLAG_FRAMES,
00152 RA_FLAG_HEADER
00153 };
00154
00155
00156 typedef struct {
00157 uint32_t samples;
00158 int resolution;
00159 int floating;
00160 int msb_first;
00161 int frame_length;
00162 int ra_distance;
00163 enum RA_Flag ra_flag;
00164 int adapt_order;
00165 int coef_table;
00166 int long_term_prediction;
00167 int max_order;
00168 int block_switching;
00169 int bgmc;
00170 int sb_part;
00171 int joint_stereo;
00172 int mc_coding;
00173 int chan_config;
00174 int chan_sort;
00175 int rlslms;
00176 int chan_config_info;
00177 int *chan_pos;
00178 int crc_enabled;
00179 } ALSSpecificConfig;
00180
00181
00182 typedef struct {
00183 int stop_flag;
00184 int master_channel;
00185 int time_diff_flag;
00186 int time_diff_sign;
00187 int time_diff_index;
00188 int weighting[6];
00189 } ALSChannelData;
00190
00191
00192 typedef struct {
00193 AVCodecContext *avctx;
00194 ALSSpecificConfig sconf;
00195 GetBitContext gb;
00196 DSPContext dsp;
00197 const AVCRC *crc_table;
00198 uint32_t crc_org;
00199 uint32_t crc;
00200 unsigned int cur_frame_length;
00201 unsigned int frame_id;
00202 unsigned int js_switch;
00203 unsigned int num_blocks;
00204 unsigned int s_max;
00205 uint8_t *bgmc_lut;
00206 int *bgmc_lut_status;
00207 int ltp_lag_length;
00208 int *const_block;
00209 unsigned int *shift_lsbs;
00210 unsigned int *opt_order;
00211 int *store_prev_samples;
00212 int *use_ltp;
00213 int *ltp_lag;
00214 int **ltp_gain;
00215 int *ltp_gain_buffer;
00216 int32_t **quant_cof;
00217 int32_t *quant_cof_buffer;
00218 int32_t **lpc_cof;
00219 int32_t *lpc_cof_buffer;
00220 int32_t *lpc_cof_reversed_buffer;
00221 ALSChannelData **chan_data;
00222 ALSChannelData *chan_data_buffer;
00223 int *reverted_channels;
00224 int32_t *prev_raw_samples;
00225 int32_t **raw_samples;
00226 int32_t *raw_buffer;
00227 uint8_t *crc_buffer;
00228 } ALSDecContext;
00229
00230
00231 typedef struct {
00232 unsigned int block_length;
00233 unsigned int ra_block;
00234 int *const_block;
00235 int js_blocks;
00236 unsigned int *shift_lsbs;
00237 unsigned int *opt_order;
00238 int *store_prev_samples;
00239 int *use_ltp;
00240 int *ltp_lag;
00241 int *ltp_gain;
00242 int32_t *quant_cof;
00243 int32_t *lpc_cof;
00244 int32_t *raw_samples;
00245 int32_t *prev_raw_samples;
00246 int32_t *raw_other;
00247 } ALSBlockData;
00248
00249
00250 static av_cold void dprint_specific_config(ALSDecContext *ctx)
00251 {
00252 #ifdef DEBUG
00253 AVCodecContext *avctx = ctx->avctx;
00254 ALSSpecificConfig *sconf = &ctx->sconf;
00255
00256 av_dlog(avctx, "resolution = %i\n", sconf->resolution);
00257 av_dlog(avctx, "floating = %i\n", sconf->floating);
00258 av_dlog(avctx, "frame_length = %i\n", sconf->frame_length);
00259 av_dlog(avctx, "ra_distance = %i\n", sconf->ra_distance);
00260 av_dlog(avctx, "ra_flag = %i\n", sconf->ra_flag);
00261 av_dlog(avctx, "adapt_order = %i\n", sconf->adapt_order);
00262 av_dlog(avctx, "coef_table = %i\n", sconf->coef_table);
00263 av_dlog(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
00264 av_dlog(avctx, "max_order = %i\n", sconf->max_order);
00265 av_dlog(avctx, "block_switching = %i\n", sconf->block_switching);
00266 av_dlog(avctx, "bgmc = %i\n", sconf->bgmc);
00267 av_dlog(avctx, "sb_part = %i\n", sconf->sb_part);
00268 av_dlog(avctx, "joint_stereo = %i\n", sconf->joint_stereo);
00269 av_dlog(avctx, "mc_coding = %i\n", sconf->mc_coding);
00270 av_dlog(avctx, "chan_config = %i\n", sconf->chan_config);
00271 av_dlog(avctx, "chan_sort = %i\n", sconf->chan_sort);
00272 av_dlog(avctx, "RLSLMS = %i\n", sconf->rlslms);
00273 av_dlog(avctx, "chan_config_info = %i\n", sconf->chan_config_info);
00274 #endif
00275 }
00276
00277
00280 static av_cold int read_specific_config(ALSDecContext *ctx)
00281 {
00282 GetBitContext gb;
00283 uint64_t ht_size;
00284 int i, config_offset;
00285 MPEG4AudioConfig m4ac;
00286 ALSSpecificConfig *sconf = &ctx->sconf;
00287 AVCodecContext *avctx = ctx->avctx;
00288 uint32_t als_id, header_size, trailer_size;
00289
00290 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
00291
00292 config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata,
00293 avctx->extradata_size);
00294
00295 if (config_offset < 0)
00296 return -1;
00297
00298 skip_bits_long(&gb, config_offset);
00299
00300 if (get_bits_left(&gb) < (30 << 3))
00301 return -1;
00302
00303
00304 als_id = get_bits_long(&gb, 32);
00305 avctx->sample_rate = m4ac.sample_rate;
00306 skip_bits_long(&gb, 32);
00307 sconf->samples = get_bits_long(&gb, 32);
00308 avctx->channels = m4ac.channels;
00309 skip_bits(&gb, 16);
00310 skip_bits(&gb, 3);
00311 sconf->resolution = get_bits(&gb, 3);
00312 sconf->floating = get_bits1(&gb);
00313 sconf->msb_first = get_bits1(&gb);
00314 sconf->frame_length = get_bits(&gb, 16) + 1;
00315 sconf->ra_distance = get_bits(&gb, 8);
00316 sconf->ra_flag = get_bits(&gb, 2);
00317 sconf->adapt_order = get_bits1(&gb);
00318 sconf->coef_table = get_bits(&gb, 2);
00319 sconf->long_term_prediction = get_bits1(&gb);
00320 sconf->max_order = get_bits(&gb, 10);
00321 sconf->block_switching = get_bits(&gb, 2);
00322 sconf->bgmc = get_bits1(&gb);
00323 sconf->sb_part = get_bits1(&gb);
00324 sconf->joint_stereo = get_bits1(&gb);
00325 sconf->mc_coding = get_bits1(&gb);
00326 sconf->chan_config = get_bits1(&gb);
00327 sconf->chan_sort = get_bits1(&gb);
00328 sconf->crc_enabled = get_bits1(&gb);
00329 sconf->rlslms = get_bits1(&gb);
00330 skip_bits(&gb, 5);
00331 skip_bits1(&gb);
00332
00333
00334
00335 if (als_id != MKBETAG('A','L','S','\0'))
00336 return -1;
00337
00338 ctx->cur_frame_length = sconf->frame_length;
00339
00340
00341 if (sconf->chan_config)
00342 sconf->chan_config_info = get_bits(&gb, 16);
00343
00344
00345
00346
00347 if (sconf->chan_sort && avctx->channels > 1) {
00348 int chan_pos_bits = av_ceil_log2(avctx->channels);
00349 int bits_needed = avctx->channels * chan_pos_bits + 7;
00350 if (get_bits_left(&gb) < bits_needed)
00351 return -1;
00352
00353 if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
00354 return AVERROR(ENOMEM);
00355
00356 for (i = 0; i < avctx->channels; i++)
00357 sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
00358
00359 align_get_bits(&gb);
00360
00361 } else {
00362 sconf->chan_sort = 0;
00363 }
00364
00365
00366
00367
00368 if (get_bits_left(&gb) < 64)
00369 return -1;
00370
00371 header_size = get_bits_long(&gb, 32);
00372 trailer_size = get_bits_long(&gb, 32);
00373 if (header_size == 0xFFFFFFFF)
00374 header_size = 0;
00375 if (trailer_size == 0xFFFFFFFF)
00376 trailer_size = 0;
00377
00378 ht_size = ((int64_t)(header_size) + (int64_t)(trailer_size)) << 3;
00379
00380
00381
00382 if (get_bits_left(&gb) < ht_size)
00383 return -1;
00384
00385 if (ht_size > INT32_MAX)
00386 return -1;
00387
00388 skip_bits_long(&gb, ht_size);
00389
00390
00391
00392 if (sconf->crc_enabled) {
00393 if (get_bits_left(&gb) < 32)
00394 return -1;
00395
00396 if (avctx->error_recognition >= FF_ER_CAREFUL) {
00397 ctx->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
00398 ctx->crc = 0xFFFFFFFF;
00399 ctx->crc_org = ~get_bits_long(&gb, 32);
00400 } else
00401 skip_bits_long(&gb, 32);
00402 }
00403
00404
00405
00406
00407 dprint_specific_config(ctx);
00408
00409 return 0;
00410 }
00411
00412
00415 static int check_specific_config(ALSDecContext *ctx)
00416 {
00417 ALSSpecificConfig *sconf = &ctx->sconf;
00418 int error = 0;
00419
00420
00421 #define MISSING_ERR(cond, str, errval) \
00422 { \
00423 if (cond) { \
00424 av_log_missing_feature(ctx->avctx, str, 0); \
00425 error = errval; \
00426 } \
00427 }
00428
00429 MISSING_ERR(sconf->floating, "Floating point decoding", -1);
00430 MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1);
00431 MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
00432
00433 return error;
00434 }
00435
00436
00440 static void parse_bs_info(const uint32_t bs_info, unsigned int n,
00441 unsigned int div, unsigned int **div_blocks,
00442 unsigned int *num_blocks)
00443 {
00444 if (n < 31 && ((bs_info << n) & 0x40000000)) {
00445
00446
00447 n *= 2;
00448 div += 1;
00449 parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
00450 parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
00451 } else {
00452
00453
00454 **div_blocks = div;
00455 (*div_blocks)++;
00456 (*num_blocks)++;
00457 }
00458 }
00459
00460
00463 static int32_t decode_rice(GetBitContext *gb, unsigned int k)
00464 {
00465 int max = get_bits_left(gb) - k;
00466 int q = get_unary(gb, 0, max);
00467 int r = k ? get_bits1(gb) : !(q & 1);
00468
00469 if (k > 1) {
00470 q <<= (k - 1);
00471 q += get_bits_long(gb, k - 1);
00472 } else if (!k) {
00473 q >>= 1;
00474 }
00475 return r ? q : ~q;
00476 }
00477
00478
00481 static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
00482 {
00483 int i, j;
00484
00485 for (i = 0, j = k - 1; i < j; i++, j--) {
00486 int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
00487 cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
00488 cof[i] += tmp1;
00489 }
00490 if (i == j)
00491 cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
00492
00493 cof[k] = par[k];
00494 }
00495
00496
00501 static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
00502 uint32_t *bs_info)
00503 {
00504 ALSSpecificConfig *sconf = &ctx->sconf;
00505 GetBitContext *gb = &ctx->gb;
00506 unsigned int *ptr_div_blocks = div_blocks;
00507 unsigned int b;
00508
00509 if (sconf->block_switching) {
00510 unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
00511 *bs_info = get_bits_long(gb, bs_info_len);
00512 *bs_info <<= (32 - bs_info_len);
00513 }
00514
00515 ctx->num_blocks = 0;
00516 parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533 for (b = 0; b < ctx->num_blocks; b++)
00534 div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
00535
00536 if (ctx->cur_frame_length != ctx->sconf.frame_length) {
00537 unsigned int remaining = ctx->cur_frame_length;
00538
00539 for (b = 0; b < ctx->num_blocks; b++) {
00540 if (remaining <= div_blocks[b]) {
00541 div_blocks[b] = remaining;
00542 ctx->num_blocks = b + 1;
00543 break;
00544 }
00545
00546 remaining -= div_blocks[b];
00547 }
00548 }
00549 }
00550
00551
00554 static int read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00555 {
00556 ALSSpecificConfig *sconf = &ctx->sconf;
00557 AVCodecContext *avctx = ctx->avctx;
00558 GetBitContext *gb = &ctx->gb;
00559
00560 if (bd->block_length <= 0)
00561 return -1;
00562
00563 *bd->raw_samples = 0;
00564 *bd->const_block = get_bits1(gb);
00565 bd->js_blocks = get_bits1(gb);
00566
00567
00568 skip_bits(gb, 5);
00569
00570 if (*bd->const_block) {
00571 unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
00572 *bd->raw_samples = get_sbits_long(gb, const_val_bits);
00573 }
00574
00575
00576 *bd->const_block = 1;
00577
00578 return 0;
00579 }
00580
00581
00584 static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00585 {
00586 int smp = bd->block_length - 1;
00587 int32_t val = *bd->raw_samples;
00588 int32_t *dst = bd->raw_samples + 1;
00589
00590
00591 for (; smp; smp--)
00592 *dst++ = val;
00593 }
00594
00595
00598 static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00599 {
00600 ALSSpecificConfig *sconf = &ctx->sconf;
00601 AVCodecContext *avctx = ctx->avctx;
00602 GetBitContext *gb = &ctx->gb;
00603 unsigned int k;
00604 unsigned int s[8];
00605 unsigned int sx[8];
00606 unsigned int sub_blocks, log2_sub_blocks, sb_length;
00607 unsigned int start = 0;
00608 unsigned int opt_order;
00609 int sb;
00610 int32_t *quant_cof = bd->quant_cof;
00611 int32_t *current_res;
00612
00613
00614
00615 *bd->const_block = 0;
00616
00617 *bd->opt_order = 1;
00618 bd->js_blocks = get_bits1(gb);
00619
00620 opt_order = *bd->opt_order;
00621
00622
00623 if (!sconf->bgmc && !sconf->sb_part) {
00624 log2_sub_blocks = 0;
00625 } else {
00626 if (sconf->bgmc && sconf->sb_part)
00627 log2_sub_blocks = get_bits(gb, 2);
00628 else
00629 log2_sub_blocks = 2 * get_bits1(gb);
00630 }
00631
00632 sub_blocks = 1 << log2_sub_blocks;
00633
00634
00635
00636 if (bd->block_length & (sub_blocks - 1)) {
00637 av_log(avctx, AV_LOG_WARNING,
00638 "Block length is not evenly divisible by the number of subblocks.\n");
00639 return -1;
00640 }
00641
00642 sb_length = bd->block_length >> log2_sub_blocks;
00643
00644 if (sconf->bgmc) {
00645 s[0] = get_bits(gb, 8 + (sconf->resolution > 1));
00646 for (k = 1; k < sub_blocks; k++)
00647 s[k] = s[k - 1] + decode_rice(gb, 2);
00648
00649 for (k = 0; k < sub_blocks; k++) {
00650 sx[k] = s[k] & 0x0F;
00651 s [k] >>= 4;
00652 }
00653 } else {
00654 s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
00655 for (k = 1; k < sub_blocks; k++)
00656 s[k] = s[k - 1] + decode_rice(gb, 0);
00657 }
00658 for (k = 1; k < sub_blocks; k++)
00659 if (s[k] > 32) {
00660 av_log(avctx, AV_LOG_ERROR, "k invalid for rice code.\n");
00661 return AVERROR_INVALIDDATA;
00662 }
00663
00664 if (get_bits1(gb))
00665 *bd->shift_lsbs = get_bits(gb, 4) + 1;
00666
00667 *bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || *bd->shift_lsbs;
00668
00669
00670 if (!sconf->rlslms) {
00671 if (sconf->adapt_order) {
00672 int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1,
00673 2, sconf->max_order + 1));
00674 *bd->opt_order = get_bits(gb, opt_order_length);
00675 if (*bd->opt_order > sconf->max_order) {
00676 *bd->opt_order = sconf->max_order;
00677 av_log(avctx, AV_LOG_ERROR, "Predictor order too large!\n");
00678 return AVERROR_INVALIDDATA;
00679 }
00680 } else {
00681 *bd->opt_order = sconf->max_order;
00682 }
00683
00684 opt_order = *bd->opt_order;
00685
00686 if (opt_order) {
00687 int add_base;
00688
00689 if (sconf->coef_table == 3) {
00690 add_base = 0x7F;
00691
00692
00693 quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
00694
00695
00696 if (opt_order > 1)
00697 quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
00698
00699
00700 for (k = 2; k < opt_order; k++)
00701 quant_cof[k] = get_bits(gb, 7);
00702 } else {
00703 int k_max;
00704 add_base = 1;
00705
00706
00707 k_max = FFMIN(opt_order, 20);
00708 for (k = 0; k < k_max; k++) {
00709 int rice_param = parcor_rice_table[sconf->coef_table][k][1];
00710 int offset = parcor_rice_table[sconf->coef_table][k][0];
00711 quant_cof[k] = decode_rice(gb, rice_param) + offset;
00712 if (quant_cof[k] < -64 || quant_cof[k] > 63) {
00713 av_log(avctx, AV_LOG_ERROR, "quant_cof %d is out of range\n", quant_cof[k]);
00714 return AVERROR_INVALIDDATA;
00715 }
00716 }
00717
00718
00719 k_max = FFMIN(opt_order, 127);
00720 for (; k < k_max; k++)
00721 quant_cof[k] = decode_rice(gb, 2) + (k & 1);
00722
00723
00724 for (; k < opt_order; k++)
00725 quant_cof[k] = decode_rice(gb, 1);
00726
00727 quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
00728
00729 if (opt_order > 1)
00730 quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
00731 }
00732
00733 for (k = 2; k < opt_order; k++)
00734 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
00735 }
00736 }
00737
00738
00739 if (sconf->long_term_prediction) {
00740 *bd->use_ltp = get_bits1(gb);
00741
00742 if (*bd->use_ltp) {
00743 int r, c;
00744
00745 bd->ltp_gain[0] = decode_rice(gb, 1) << 3;
00746 bd->ltp_gain[1] = decode_rice(gb, 2) << 3;
00747
00748 r = get_unary(gb, 0, 3);
00749 c = get_bits(gb, 2);
00750 bd->ltp_gain[2] = ltp_gain_values[r][c];
00751
00752 bd->ltp_gain[3] = decode_rice(gb, 2) << 3;
00753 bd->ltp_gain[4] = decode_rice(gb, 1) << 3;
00754
00755 *bd->ltp_lag = get_bits(gb, ctx->ltp_lag_length);
00756 *bd->ltp_lag += FFMAX(4, opt_order + 1);
00757 }
00758 }
00759
00760
00761 if (bd->ra_block) {
00762 if (opt_order)
00763 bd->raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
00764 if (opt_order > 1)
00765 bd->raw_samples[1] = decode_rice(gb, FFMIN(s[0] + 3, ctx->s_max));
00766 if (opt_order > 2)
00767 bd->raw_samples[2] = decode_rice(gb, FFMIN(s[0] + 1, ctx->s_max));
00768
00769 start = FFMIN(opt_order, 3);
00770 }
00771
00772
00773 if (sconf->bgmc) {
00774 int delta[8];
00775 unsigned int k [8];
00776 unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5);
00777
00778
00779 unsigned int high;
00780 unsigned int low;
00781 unsigned int value;
00782
00783 ff_bgmc_decode_init(gb, &high, &low, &value);
00784
00785 current_res = bd->raw_samples + start;
00786
00787 for (sb = 0; sb < sub_blocks; sb++) {
00788 unsigned int sb_len = sb_length - (sb ? 0 : start);
00789
00790 k [sb] = s[sb] > b ? s[sb] - b : 0;
00791 delta[sb] = 5 - s[sb] + k[sb];
00792
00793 ff_bgmc_decode(gb, sb_len, current_res,
00794 delta[sb], sx[sb], &high, &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status);
00795
00796 current_res += sb_len;
00797 }
00798
00799 ff_bgmc_decode_end(gb);
00800
00801
00802
00803 current_res = bd->raw_samples + start;
00804
00805 for (sb = 0; sb < sub_blocks; sb++, start = 0) {
00806 unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]];
00807 unsigned int cur_k = k[sb];
00808 unsigned int cur_s = s[sb];
00809
00810 for (; start < sb_length; start++) {
00811 int32_t res = *current_res;
00812
00813 if (res == cur_tail_code) {
00814 unsigned int max_msb = (2 + (sx[sb] > 2) + (sx[sb] > 10))
00815 << (5 - delta[sb]);
00816
00817 res = decode_rice(gb, cur_s);
00818
00819 if (res >= 0) {
00820 res += (max_msb ) << cur_k;
00821 } else {
00822 res -= (max_msb - 1) << cur_k;
00823 }
00824 } else {
00825 if (res > cur_tail_code)
00826 res--;
00827
00828 if (res & 1)
00829 res = -res;
00830
00831 res >>= 1;
00832
00833 if (cur_k) {
00834 res <<= cur_k;
00835 res |= get_bits_long(gb, cur_k);
00836 }
00837 }
00838
00839 *current_res++ = res;
00840 }
00841 }
00842 } else {
00843 current_res = bd->raw_samples + start;
00844
00845 for (sb = 0; sb < sub_blocks; sb++, start = 0)
00846 for (; start < sb_length; start++)
00847 *current_res++ = decode_rice(gb, s[sb]);
00848 }
00849
00850 if (!sconf->mc_coding || ctx->js_switch)
00851 align_get_bits(gb);
00852
00853 return 0;
00854 }
00855
00856
00859 static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00860 {
00861 ALSSpecificConfig *sconf = &ctx->sconf;
00862 unsigned int block_length = bd->block_length;
00863 unsigned int smp = 0;
00864 unsigned int k;
00865 int opt_order = *bd->opt_order;
00866 int sb;
00867 int64_t y;
00868 int32_t *quant_cof = bd->quant_cof;
00869 int32_t *lpc_cof = bd->lpc_cof;
00870 int32_t *raw_samples = bd->raw_samples;
00871 int32_t *raw_samples_end = bd->raw_samples + bd->block_length;
00872 int32_t *lpc_cof_reversed = ctx->lpc_cof_reversed_buffer;
00873
00874
00875 if (*bd->use_ltp) {
00876 int ltp_smp;
00877
00878 for (ltp_smp = FFMAX(*bd->ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
00879 int center = ltp_smp - *bd->ltp_lag;
00880 int begin = FFMAX(0, center - 2);
00881 int end = center + 3;
00882 int tab = 5 - (end - begin);
00883 int base;
00884
00885 y = 1 << 6;
00886
00887 for (base = begin; base < end; base++, tab++)
00888 y += MUL64(bd->ltp_gain[tab], raw_samples[base]);
00889
00890 raw_samples[ltp_smp] += y >> 7;
00891 }
00892 }
00893
00894
00895 if (bd->ra_block) {
00896 for (smp = 0; smp < opt_order; smp++) {
00897 y = 1 << 19;
00898
00899 for (sb = 0; sb < smp; sb++)
00900 y += MUL64(lpc_cof[sb], raw_samples[-(sb + 1)]);
00901
00902 *raw_samples++ -= y >> 20;
00903 parcor_to_lpc(smp, quant_cof, lpc_cof);
00904 }
00905 } else {
00906 for (k = 0; k < opt_order; k++)
00907 parcor_to_lpc(k, quant_cof, lpc_cof);
00908
00909
00910 if (*bd->store_prev_samples)
00911 memcpy(bd->prev_raw_samples, raw_samples - sconf->max_order,
00912 sizeof(*bd->prev_raw_samples) * sconf->max_order);
00913
00914
00915 if (bd->js_blocks && bd->raw_other) {
00916 int32_t *left, *right;
00917
00918 if (bd->raw_other > raw_samples) {
00919 left = raw_samples;
00920 right = bd->raw_other;
00921 } else {
00922 left = bd->raw_other;
00923 right = raw_samples;
00924 }
00925
00926 for (sb = -1; sb >= -sconf->max_order; sb--)
00927 raw_samples[sb] = right[sb] - left[sb];
00928 }
00929
00930
00931 if (*bd->shift_lsbs)
00932 for (sb = -1; sb >= -sconf->max_order; sb--)
00933 raw_samples[sb] >>= *bd->shift_lsbs;
00934 }
00935
00936
00937 lpc_cof = lpc_cof + opt_order;
00938
00939 for (sb = 0; sb < opt_order; sb++)
00940 lpc_cof_reversed[sb] = lpc_cof[-(sb + 1)];
00941
00942
00943 raw_samples = bd->raw_samples + smp;
00944 lpc_cof = lpc_cof_reversed + opt_order;
00945
00946 for (; raw_samples < raw_samples_end; raw_samples++) {
00947 y = 1 << 19;
00948
00949 for (sb = -opt_order; sb < 0; sb++)
00950 y += MUL64(lpc_cof[sb], raw_samples[sb]);
00951
00952 *raw_samples -= y >> 20;
00953 }
00954
00955 raw_samples = bd->raw_samples;
00956
00957
00958 if (*bd->store_prev_samples)
00959 memcpy(raw_samples - sconf->max_order, bd->prev_raw_samples,
00960 sizeof(*raw_samples) * sconf->max_order);
00961
00962 return 0;
00963 }
00964
00965
00968 static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
00969 {
00970 GetBitContext *gb = &ctx->gb;
00971
00972 *bd->shift_lsbs = 0;
00973
00974 if (get_bits1(gb)) {
00975 if (read_var_block_data(ctx, bd))
00976 return -1;
00977 } else {
00978 if (read_const_block_data(ctx, bd) < 0)
00979 return -1;
00980 }
00981
00982 return 0;
00983 }
00984
00985
00988 static int decode_block(ALSDecContext *ctx, ALSBlockData *bd)
00989 {
00990 unsigned int smp;
00991
00992
00993 if (*bd->const_block)
00994 decode_const_block_data(ctx, bd);
00995 else if (decode_var_block_data(ctx, bd))
00996 return -1;
00997
00998
00999
01000 if (*bd->shift_lsbs)
01001 for (smp = 0; smp < bd->block_length; smp++)
01002 bd->raw_samples[smp] <<= *bd->shift_lsbs;
01003
01004 return 0;
01005 }
01006
01007
01010 static int read_decode_block(ALSDecContext *ctx, ALSBlockData *bd)
01011 {
01012 int ret;
01013
01014 ret = read_block(ctx, bd);
01015
01016 if (ret)
01017 return ret;
01018
01019 ret = decode_block(ctx, bd);
01020
01021 return ret;
01022 }
01023
01024
01028 static void zero_remaining(unsigned int b, unsigned int b_max,
01029 const unsigned int *div_blocks, int32_t *buf)
01030 {
01031 unsigned int count = 0;
01032
01033 for (; b < b_max; b++)
01034 count += div_blocks[b];
01035
01036 if (count)
01037 memset(buf, 0, sizeof(*buf) * count);
01038 }
01039
01040
01043 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
01044 unsigned int c, const unsigned int *div_blocks,
01045 unsigned int *js_blocks)
01046 {
01047 unsigned int b;
01048 ALSBlockData bd;
01049
01050 memset(&bd, 0, sizeof(ALSBlockData));
01051
01052 bd.ra_block = ra_frame;
01053 bd.const_block = ctx->const_block;
01054 bd.shift_lsbs = ctx->shift_lsbs;
01055 bd.opt_order = ctx->opt_order;
01056 bd.store_prev_samples = ctx->store_prev_samples;
01057 bd.use_ltp = ctx->use_ltp;
01058 bd.ltp_lag = ctx->ltp_lag;
01059 bd.ltp_gain = ctx->ltp_gain[0];
01060 bd.quant_cof = ctx->quant_cof[0];
01061 bd.lpc_cof = ctx->lpc_cof[0];
01062 bd.prev_raw_samples = ctx->prev_raw_samples;
01063 bd.raw_samples = ctx->raw_samples[c];
01064
01065
01066 for (b = 0; b < ctx->num_blocks; b++) {
01067 bd.block_length = div_blocks[b];
01068
01069 if (read_decode_block(ctx, &bd)) {
01070
01071 zero_remaining(b, ctx->num_blocks, div_blocks, bd.raw_samples);
01072 return -1;
01073 }
01074 bd.raw_samples += div_blocks[b];
01075 bd.ra_block = 0;
01076 }
01077
01078 return 0;
01079 }
01080
01081
01084 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
01085 unsigned int c, const unsigned int *div_blocks,
01086 unsigned int *js_blocks)
01087 {
01088 ALSSpecificConfig *sconf = &ctx->sconf;
01089 unsigned int offset = 0;
01090 unsigned int b;
01091 ALSBlockData bd[2];
01092
01093 memset(bd, 0, 2 * sizeof(ALSBlockData));
01094
01095 bd[0].ra_block = ra_frame;
01096 bd[0].const_block = ctx->const_block;
01097 bd[0].shift_lsbs = ctx->shift_lsbs;
01098 bd[0].opt_order = ctx->opt_order;
01099 bd[0].store_prev_samples = ctx->store_prev_samples;
01100 bd[0].use_ltp = ctx->use_ltp;
01101 bd[0].ltp_lag = ctx->ltp_lag;
01102 bd[0].ltp_gain = ctx->ltp_gain[0];
01103 bd[0].quant_cof = ctx->quant_cof[0];
01104 bd[0].lpc_cof = ctx->lpc_cof[0];
01105 bd[0].prev_raw_samples = ctx->prev_raw_samples;
01106 bd[0].js_blocks = *js_blocks;
01107
01108 bd[1].ra_block = ra_frame;
01109 bd[1].const_block = ctx->const_block;
01110 bd[1].shift_lsbs = ctx->shift_lsbs;
01111 bd[1].opt_order = ctx->opt_order;
01112 bd[1].store_prev_samples = ctx->store_prev_samples;
01113 bd[1].use_ltp = ctx->use_ltp;
01114 bd[1].ltp_lag = ctx->ltp_lag;
01115 bd[1].ltp_gain = ctx->ltp_gain[0];
01116 bd[1].quant_cof = ctx->quant_cof[0];
01117 bd[1].lpc_cof = ctx->lpc_cof[0];
01118 bd[1].prev_raw_samples = ctx->prev_raw_samples;
01119 bd[1].js_blocks = *(js_blocks + 1);
01120
01121
01122 for (b = 0; b < ctx->num_blocks; b++) {
01123 unsigned int s;
01124
01125 bd[0].block_length = div_blocks[b];
01126 bd[1].block_length = div_blocks[b];
01127
01128 bd[0].raw_samples = ctx->raw_samples[c ] + offset;
01129 bd[1].raw_samples = ctx->raw_samples[c + 1] + offset;
01130
01131 bd[0].raw_other = bd[1].raw_samples;
01132 bd[1].raw_other = bd[0].raw_samples;
01133
01134 if(read_decode_block(ctx, &bd[0]) || read_decode_block(ctx, &bd[1])) {
01135
01136 zero_remaining(b, ctx->num_blocks, div_blocks, bd[0].raw_samples);
01137 zero_remaining(b, ctx->num_blocks, div_blocks, bd[1].raw_samples);
01138 return -1;
01139 }
01140
01141
01142 if (bd[0].js_blocks) {
01143 if (bd[1].js_blocks)
01144 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
01145
01146 for (s = 0; s < div_blocks[b]; s++)
01147 bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s];
01148 } else if (bd[1].js_blocks) {
01149 for (s = 0; s < div_blocks[b]; s++)
01150 bd[1].raw_samples[s] = bd[1].raw_samples[s] + bd[0].raw_samples[s];
01151 }
01152
01153 offset += div_blocks[b];
01154 bd[0].ra_block = 0;
01155 bd[1].ra_block = 0;
01156 }
01157
01158
01159
01160 memmove(ctx->raw_samples[c] - sconf->max_order,
01161 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01162 sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01163
01164 return 0;
01165 }
01166
01167
01170 static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
01171 {
01172 GetBitContext *gb = &ctx->gb;
01173 ALSChannelData *current = cd;
01174 unsigned int channels = ctx->avctx->channels;
01175 int entries = 0;
01176
01177 while (entries < channels && !(current->stop_flag = get_bits1(gb))) {
01178 current->master_channel = get_bits_long(gb, av_ceil_log2(channels));
01179
01180 if (current->master_channel >= channels) {
01181 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n");
01182 return -1;
01183 }
01184
01185 if (current->master_channel != c) {
01186 current->time_diff_flag = get_bits1(gb);
01187 current->weighting[0] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01188 current->weighting[1] = mcc_weightings[av_clip(decode_rice(gb, 2) + 14, 0, 32)];
01189 current->weighting[2] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01190
01191 if (current->time_diff_flag) {
01192 current->weighting[3] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01193 current->weighting[4] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01194 current->weighting[5] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01195
01196 current->time_diff_sign = get_bits1(gb);
01197 current->time_diff_index = get_bits(gb, ctx->ltp_lag_length - 3) + 3;
01198 }
01199 }
01200
01201 current++;
01202 entries++;
01203 }
01204
01205 if (entries == channels) {
01206 av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n");
01207 return -1;
01208 }
01209
01210 align_get_bits(gb);
01211 return 0;
01212 }
01213
01214
01217 static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
01218 ALSChannelData **cd, int *reverted,
01219 unsigned int offset, int c)
01220 {
01221 ALSChannelData *ch = cd[c];
01222 unsigned int dep = 0;
01223 unsigned int channels = ctx->avctx->channels;
01224
01225 if (reverted[c])
01226 return 0;
01227
01228 reverted[c] = 1;
01229
01230 while (dep < channels && !ch[dep].stop_flag) {
01231 revert_channel_correlation(ctx, bd, cd, reverted, offset,
01232 ch[dep].master_channel);
01233
01234 dep++;
01235 }
01236
01237 if (dep == channels) {
01238 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n");
01239 return -1;
01240 }
01241
01242 bd->const_block = ctx->const_block + c;
01243 bd->shift_lsbs = ctx->shift_lsbs + c;
01244 bd->opt_order = ctx->opt_order + c;
01245 bd->store_prev_samples = ctx->store_prev_samples + c;
01246 bd->use_ltp = ctx->use_ltp + c;
01247 bd->ltp_lag = ctx->ltp_lag + c;
01248 bd->ltp_gain = ctx->ltp_gain[c];
01249 bd->lpc_cof = ctx->lpc_cof[c];
01250 bd->quant_cof = ctx->quant_cof[c];
01251 bd->raw_samples = ctx->raw_samples[c] + offset;
01252
01253 dep = 0;
01254 while (!ch[dep].stop_flag) {
01255 unsigned int smp;
01256 unsigned int begin = 1;
01257 unsigned int end = bd->block_length - 1;
01258 int64_t y;
01259 int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;
01260
01261 if (ch[dep].time_diff_flag) {
01262 int t = ch[dep].time_diff_index;
01263
01264 if (ch[dep].time_diff_sign) {
01265 t = -t;
01266 begin -= t;
01267 } else {
01268 end -= t;
01269 }
01270
01271 for (smp = begin; smp < end; smp++) {
01272 y = (1 << 6) +
01273 MUL64(ch[dep].weighting[0], master[smp - 1 ]) +
01274 MUL64(ch[dep].weighting[1], master[smp ]) +
01275 MUL64(ch[dep].weighting[2], master[smp + 1 ]) +
01276 MUL64(ch[dep].weighting[3], master[smp - 1 + t]) +
01277 MUL64(ch[dep].weighting[4], master[smp + t]) +
01278 MUL64(ch[dep].weighting[5], master[smp + 1 + t]);
01279
01280 bd->raw_samples[smp] += y >> 7;
01281 }
01282 } else {
01283 for (smp = begin; smp < end; smp++) {
01284 y = (1 << 6) +
01285 MUL64(ch[dep].weighting[0], master[smp - 1]) +
01286 MUL64(ch[dep].weighting[1], master[smp ]) +
01287 MUL64(ch[dep].weighting[2], master[smp + 1]);
01288
01289 bd->raw_samples[smp] += y >> 7;
01290 }
01291 }
01292
01293 dep++;
01294 }
01295
01296 return 0;
01297 }
01298
01299
01302 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
01303 {
01304 ALSSpecificConfig *sconf = &ctx->sconf;
01305 AVCodecContext *avctx = ctx->avctx;
01306 GetBitContext *gb = &ctx->gb;
01307 unsigned int div_blocks[32];
01308 unsigned int c;
01309 unsigned int js_blocks[2];
01310
01311 uint32_t bs_info = 0;
01312
01313
01314 if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
01315 skip_bits_long(gb, 32);
01316
01317 if (sconf->mc_coding && sconf->joint_stereo) {
01318 ctx->js_switch = get_bits1(gb);
01319 align_get_bits(gb);
01320 }
01321
01322 if (!sconf->mc_coding || ctx->js_switch) {
01323 int independent_bs = !sconf->joint_stereo;
01324
01325 for (c = 0; c < avctx->channels; c++) {
01326 js_blocks[0] = 0;
01327 js_blocks[1] = 0;
01328
01329 get_block_sizes(ctx, div_blocks, &bs_info);
01330
01331
01332
01333 if (sconf->joint_stereo && sconf->block_switching)
01334 if (bs_info >> 31)
01335 independent_bs = 2;
01336
01337
01338 if (c == avctx->channels - 1)
01339 independent_bs = 1;
01340
01341 if (independent_bs) {
01342 if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))
01343 return -1;
01344
01345 independent_bs--;
01346 } else {
01347 if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))
01348 return -1;
01349
01350 c++;
01351 }
01352
01353
01354 memmove(ctx->raw_samples[c] - sconf->max_order,
01355 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01356 sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01357 }
01358 } else {
01359 ALSBlockData bd;
01360 int b;
01361 int *reverted_channels = ctx->reverted_channels;
01362 unsigned int offset = 0;
01363
01364 for (c = 0; c < avctx->channels; c++)
01365 if (ctx->chan_data[c] < ctx->chan_data_buffer) {
01366 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");
01367 return -1;
01368 }
01369
01370 memset(&bd, 0, sizeof(ALSBlockData));
01371 memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);
01372
01373 bd.ra_block = ra_frame;
01374 bd.prev_raw_samples = ctx->prev_raw_samples;
01375
01376 get_block_sizes(ctx, div_blocks, &bs_info);
01377
01378 for (b = 0; b < ctx->num_blocks; b++) {
01379 bd.block_length = div_blocks[b];
01380
01381 for (c = 0; c < avctx->channels; c++) {
01382 bd.const_block = ctx->const_block + c;
01383 bd.shift_lsbs = ctx->shift_lsbs + c;
01384 bd.opt_order = ctx->opt_order + c;
01385 bd.store_prev_samples = ctx->store_prev_samples + c;
01386 bd.use_ltp = ctx->use_ltp + c;
01387 bd.ltp_lag = ctx->ltp_lag + c;
01388 bd.ltp_gain = ctx->ltp_gain[c];
01389 bd.lpc_cof = ctx->lpc_cof[c];
01390 bd.quant_cof = ctx->quant_cof[c];
01391 bd.raw_samples = ctx->raw_samples[c] + offset;
01392 bd.raw_other = NULL;
01393
01394 read_block(ctx, &bd);
01395 if (read_channel_data(ctx, ctx->chan_data[c], c))
01396 return -1;
01397 }
01398
01399 for (c = 0; c < avctx->channels; c++)
01400 if (revert_channel_correlation(ctx, &bd, ctx->chan_data,
01401 reverted_channels, offset, c))
01402 return -1;
01403
01404 for (c = 0; c < avctx->channels; c++) {
01405 bd.const_block = ctx->const_block + c;
01406 bd.shift_lsbs = ctx->shift_lsbs + c;
01407 bd.opt_order = ctx->opt_order + c;
01408 bd.store_prev_samples = ctx->store_prev_samples + c;
01409 bd.use_ltp = ctx->use_ltp + c;
01410 bd.ltp_lag = ctx->ltp_lag + c;
01411 bd.ltp_gain = ctx->ltp_gain[c];
01412 bd.lpc_cof = ctx->lpc_cof[c];
01413 bd.quant_cof = ctx->quant_cof[c];
01414 bd.raw_samples = ctx->raw_samples[c] + offset;
01415 decode_block(ctx, &bd);
01416 }
01417
01418 memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));
01419 offset += div_blocks[b];
01420 bd.ra_block = 0;
01421 }
01422
01423
01424 for (c = 0; c < avctx->channels; c++)
01425 memmove(ctx->raw_samples[c] - sconf->max_order,
01426 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01427 sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01428 }
01429
01430
01431
01432 return 0;
01433 }
01434
01435
01438 static int decode_frame(AVCodecContext *avctx,
01439 void *data, int *data_size,
01440 AVPacket *avpkt)
01441 {
01442 ALSDecContext *ctx = avctx->priv_data;
01443 ALSSpecificConfig *sconf = &ctx->sconf;
01444 const uint8_t *buffer = avpkt->data;
01445 int buffer_size = avpkt->size;
01446 int invalid_frame, size;
01447 unsigned int c, sample, ra_frame, bytes_read, shift;
01448
01449 init_get_bits(&ctx->gb, buffer, buffer_size * 8);
01450
01451
01452
01453
01454
01455 ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
01456
01457
01458 if (sconf->samples != 0xFFFFFFFF)
01459 ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
01460 sconf->frame_length);
01461 else
01462 ctx->cur_frame_length = sconf->frame_length;
01463
01464
01465 if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
01466 av_log(ctx->avctx, AV_LOG_WARNING,
01467 "Reading frame data failed. Skipping RA unit.\n");
01468
01469 ctx->frame_id++;
01470
01471
01472 size = ctx->cur_frame_length * avctx->channels *
01473 av_get_bytes_per_sample(avctx->sample_fmt);
01474
01475 if (size > *data_size) {
01476 av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n");
01477 return -1;
01478 }
01479
01480 *data_size = size;
01481
01482
01483 #define INTERLEAVE_OUTPUT(bps) \
01484 { \
01485 int##bps##_t *dest = (int##bps##_t*) data; \
01486 shift = bps - ctx->avctx->bits_per_raw_sample; \
01487 for (sample = 0; sample < ctx->cur_frame_length; sample++) \
01488 for (c = 0; c < avctx->channels; c++) \
01489 *dest++ = ctx->raw_samples[c][sample] << shift; \
01490 }
01491
01492 if (ctx->avctx->bits_per_raw_sample <= 16) {
01493 INTERLEAVE_OUTPUT(16)
01494 } else {
01495 INTERLEAVE_OUTPUT(32)
01496 }
01497
01498
01499 if (sconf->crc_enabled && avctx->error_recognition >= FF_ER_CAREFUL) {
01500 int swap = HAVE_BIGENDIAN != sconf->msb_first;
01501
01502 if (ctx->avctx->bits_per_raw_sample == 24) {
01503 int32_t *src = data;
01504
01505 for (sample = 0;
01506 sample < ctx->cur_frame_length * avctx->channels;
01507 sample++) {
01508 int32_t v;
01509
01510 if (swap)
01511 v = av_bswap32(src[sample]);
01512 else
01513 v = src[sample];
01514 if (!HAVE_BIGENDIAN)
01515 v >>= 8;
01516
01517 ctx->crc = av_crc(ctx->crc_table, ctx->crc, (uint8_t*)(&v), 3);
01518 }
01519 } else {
01520 uint8_t *crc_source;
01521
01522 if (swap) {
01523 if (ctx->avctx->bits_per_raw_sample <= 16) {
01524 int16_t *src = (int16_t*) data;
01525 int16_t *dest = (int16_t*) ctx->crc_buffer;
01526 for (sample = 0;
01527 sample < ctx->cur_frame_length * avctx->channels;
01528 sample++)
01529 *dest++ = av_bswap16(src[sample]);
01530 } else {
01531 ctx->dsp.bswap_buf((uint32_t*)ctx->crc_buffer, data,
01532 ctx->cur_frame_length * avctx->channels);
01533 }
01534 crc_source = ctx->crc_buffer;
01535 } else {
01536 crc_source = data;
01537 }
01538
01539 ctx->crc = av_crc(ctx->crc_table, ctx->crc, crc_source, size);
01540 }
01541
01542
01543
01544 if (ctx->cur_frame_length != sconf->frame_length &&
01545 ctx->crc_org != ctx->crc) {
01546 av_log(avctx, AV_LOG_ERROR, "CRC error.\n");
01547 }
01548 }
01549
01550
01551 bytes_read = invalid_frame ? buffer_size :
01552 (get_bits_count(&ctx->gb) + 7) >> 3;
01553
01554 return bytes_read;
01555 }
01556
01557
01560 static av_cold int decode_end(AVCodecContext *avctx)
01561 {
01562 ALSDecContext *ctx = avctx->priv_data;
01563
01564 av_freep(&ctx->sconf.chan_pos);
01565
01566 ff_bgmc_end(&ctx->bgmc_lut, &ctx->bgmc_lut_status);
01567
01568 av_freep(&ctx->const_block);
01569 av_freep(&ctx->shift_lsbs);
01570 av_freep(&ctx->opt_order);
01571 av_freep(&ctx->store_prev_samples);
01572 av_freep(&ctx->use_ltp);
01573 av_freep(&ctx->ltp_lag);
01574 av_freep(&ctx->ltp_gain);
01575 av_freep(&ctx->ltp_gain_buffer);
01576 av_freep(&ctx->quant_cof);
01577 av_freep(&ctx->lpc_cof);
01578 av_freep(&ctx->quant_cof_buffer);
01579 av_freep(&ctx->lpc_cof_buffer);
01580 av_freep(&ctx->lpc_cof_reversed_buffer);
01581 av_freep(&ctx->prev_raw_samples);
01582 av_freep(&ctx->raw_samples);
01583 av_freep(&ctx->raw_buffer);
01584 av_freep(&ctx->chan_data);
01585 av_freep(&ctx->chan_data_buffer);
01586 av_freep(&ctx->reverted_channels);
01587 av_freep(&ctx->crc_buffer);
01588
01589 return 0;
01590 }
01591
01592
01595 static av_cold int decode_init(AVCodecContext *avctx)
01596 {
01597 unsigned int c;
01598 unsigned int channel_size;
01599 int num_buffers;
01600 ALSDecContext *ctx = avctx->priv_data;
01601 ALSSpecificConfig *sconf = &ctx->sconf;
01602 ctx->avctx = avctx;
01603
01604 if (!avctx->extradata) {
01605 av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
01606 return -1;
01607 }
01608
01609 if (read_specific_config(ctx)) {
01610 av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
01611 decode_end(avctx);
01612 return -1;
01613 }
01614
01615 if (check_specific_config(ctx)) {
01616 decode_end(avctx);
01617 return -1;
01618 }
01619
01620 if (sconf->bgmc)
01621 ff_bgmc_init(avctx, &ctx->bgmc_lut, &ctx->bgmc_lut_status);
01622
01623 if (sconf->floating) {
01624 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01625 avctx->bits_per_raw_sample = 32;
01626 } else {
01627 avctx->sample_fmt = sconf->resolution > 1
01628 ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
01629 avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
01630 }
01631
01632
01633
01634
01635 ctx->s_max = sconf->resolution > 1 ? 31 : 15;
01636
01637
01638 ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) +
01639 (avctx->sample_rate >= 192000);
01640
01641
01642 num_buffers = sconf->mc_coding ? avctx->channels : 1;
01643
01644 ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * num_buffers);
01645 ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * num_buffers);
01646 ctx->quant_cof_buffer = av_malloc(sizeof(*ctx->quant_cof_buffer) *
01647 num_buffers * sconf->max_order);
01648 ctx->lpc_cof_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
01649 num_buffers * sconf->max_order);
01650 ctx->lpc_cof_reversed_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
01651 sconf->max_order);
01652
01653 if (!ctx->quant_cof || !ctx->lpc_cof ||
01654 !ctx->quant_cof_buffer || !ctx->lpc_cof_buffer ||
01655 !ctx->lpc_cof_reversed_buffer) {
01656 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01657 return AVERROR(ENOMEM);
01658 }
01659
01660
01661 for (c = 0; c < num_buffers; c++) {
01662 ctx->quant_cof[c] = ctx->quant_cof_buffer + c * sconf->max_order;
01663 ctx->lpc_cof[c] = ctx->lpc_cof_buffer + c * sconf->max_order;
01664 }
01665
01666
01667 ctx->const_block = av_malloc (sizeof(*ctx->const_block) * num_buffers);
01668 ctx->shift_lsbs = av_malloc (sizeof(*ctx->shift_lsbs) * num_buffers);
01669 ctx->opt_order = av_malloc (sizeof(*ctx->opt_order) * num_buffers);
01670 ctx->store_prev_samples = av_malloc(sizeof(*ctx->store_prev_samples) * num_buffers);
01671 ctx->use_ltp = av_mallocz(sizeof(*ctx->use_ltp) * num_buffers);
01672 ctx->ltp_lag = av_malloc (sizeof(*ctx->ltp_lag) * num_buffers);
01673 ctx->ltp_gain = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers);
01674 ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) *
01675 num_buffers * 5);
01676
01677 if (!ctx->const_block || !ctx->shift_lsbs ||
01678 !ctx->opt_order || !ctx->store_prev_samples ||
01679 !ctx->use_ltp || !ctx->ltp_lag ||
01680 !ctx->ltp_gain || !ctx->ltp_gain_buffer) {
01681 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01682 decode_end(avctx);
01683 return AVERROR(ENOMEM);
01684 }
01685
01686 for (c = 0; c < num_buffers; c++)
01687 ctx->ltp_gain[c] = ctx->ltp_gain_buffer + c * 5;
01688
01689
01690 if (sconf->mc_coding) {
01691 ctx->chan_data_buffer = av_malloc(sizeof(*ctx->chan_data_buffer) *
01692 num_buffers * num_buffers);
01693 ctx->chan_data = av_malloc(sizeof(*ctx->chan_data) *
01694 num_buffers);
01695 ctx->reverted_channels = av_malloc(sizeof(*ctx->reverted_channels) *
01696 num_buffers);
01697
01698 if (!ctx->chan_data_buffer || !ctx->chan_data || !ctx->reverted_channels) {
01699 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01700 decode_end(avctx);
01701 return AVERROR(ENOMEM);
01702 }
01703
01704 for (c = 0; c < num_buffers; c++)
01705 ctx->chan_data[c] = ctx->chan_data_buffer + c * num_buffers;
01706 } else {
01707 ctx->chan_data = NULL;
01708 ctx->chan_data_buffer = NULL;
01709 ctx->reverted_channels = NULL;
01710 }
01711
01712 avctx->frame_size = sconf->frame_length;
01713 channel_size = sconf->frame_length + sconf->max_order;
01714
01715 ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
01716 ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);
01717 ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels);
01718
01719
01720 if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
01721 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01722 decode_end(avctx);
01723 return AVERROR(ENOMEM);
01724 }
01725
01726
01727 ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
01728 for (c = 1; c < avctx->channels; c++)
01729 ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
01730
01731
01732 if (HAVE_BIGENDIAN != sconf->msb_first && sconf->crc_enabled &&
01733 avctx->error_recognition >= FF_ER_CAREFUL) {
01734 ctx->crc_buffer = av_malloc(sizeof(*ctx->crc_buffer) *
01735 ctx->cur_frame_length *
01736 avctx->channels *
01737 av_get_bytes_per_sample(avctx->sample_fmt));
01738 if (!ctx->crc_buffer) {
01739 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01740 decode_end(avctx);
01741 return AVERROR(ENOMEM);
01742 }
01743 }
01744
01745 dsputil_init(&ctx->dsp, avctx);
01746
01747 return 0;
01748 }
01749
01750
01753 static av_cold void flush(AVCodecContext *avctx)
01754 {
01755 ALSDecContext *ctx = avctx->priv_data;
01756
01757 ctx->frame_id = 0;
01758 }
01759
01760
01761 AVCodec ff_als_decoder = {
01762 "als",
01763 AVMEDIA_TYPE_AUDIO,
01764 CODEC_ID_MP4ALS,
01765 sizeof(ALSDecContext),
01766 decode_init,
01767 NULL,
01768 decode_end,
01769 decode_frame,
01770 .flush = flush,
01771 .capabilities = CODEC_CAP_SUBFRAMES,
01772 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
01773 };
01774