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 void 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 *bd->raw_samples = 0;
00561 *bd->const_block = get_bits1(gb);
00562 bd->js_blocks = get_bits1(gb);
00563
00564
00565 skip_bits(gb, 5);
00566
00567 if (*bd->const_block) {
00568 unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
00569 *bd->raw_samples = get_sbits_long(gb, const_val_bits);
00570 }
00571
00572
00573 *bd->const_block = 1;
00574 }
00575
00576
00579 static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00580 {
00581 int smp = bd->block_length - 1;
00582 int32_t val = *bd->raw_samples;
00583 int32_t *dst = bd->raw_samples + 1;
00584
00585
00586 for (; smp; smp--)
00587 *dst++ = val;
00588 }
00589
00590
00593 static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00594 {
00595 ALSSpecificConfig *sconf = &ctx->sconf;
00596 AVCodecContext *avctx = ctx->avctx;
00597 GetBitContext *gb = &ctx->gb;
00598 unsigned int k;
00599 unsigned int s[8];
00600 unsigned int sx[8];
00601 unsigned int sub_blocks, log2_sub_blocks, sb_length;
00602 unsigned int start = 0;
00603 unsigned int opt_order;
00604 int sb;
00605 int32_t *quant_cof = bd->quant_cof;
00606 int32_t *current_res;
00607
00608
00609
00610 *bd->const_block = 0;
00611
00612 *bd->opt_order = 1;
00613 bd->js_blocks = get_bits1(gb);
00614
00615 opt_order = *bd->opt_order;
00616
00617
00618 if (!sconf->bgmc && !sconf->sb_part) {
00619 log2_sub_blocks = 0;
00620 } else {
00621 if (sconf->bgmc && sconf->sb_part)
00622 log2_sub_blocks = get_bits(gb, 2);
00623 else
00624 log2_sub_blocks = 2 * get_bits1(gb);
00625 }
00626
00627 sub_blocks = 1 << log2_sub_blocks;
00628
00629
00630
00631 if (bd->block_length & (sub_blocks - 1)) {
00632 av_log(avctx, AV_LOG_WARNING,
00633 "Block length is not evenly divisible by the number of subblocks.\n");
00634 return -1;
00635 }
00636
00637 sb_length = bd->block_length >> log2_sub_blocks;
00638
00639 if (sconf->bgmc) {
00640 s[0] = get_bits(gb, 8 + (sconf->resolution > 1));
00641 for (k = 1; k < sub_blocks; k++)
00642 s[k] = s[k - 1] + decode_rice(gb, 2);
00643
00644 for (k = 0; k < sub_blocks; k++) {
00645 sx[k] = s[k] & 0x0F;
00646 s [k] >>= 4;
00647 }
00648 } else {
00649 s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
00650 for (k = 1; k < sub_blocks; k++)
00651 s[k] = s[k - 1] + decode_rice(gb, 0);
00652 }
00653
00654 if (get_bits1(gb))
00655 *bd->shift_lsbs = get_bits(gb, 4) + 1;
00656
00657 *bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || *bd->shift_lsbs;
00658
00659
00660 if (!sconf->rlslms) {
00661 if (sconf->adapt_order) {
00662 int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1,
00663 2, sconf->max_order + 1));
00664 *bd->opt_order = get_bits(gb, opt_order_length);
00665 } else {
00666 *bd->opt_order = sconf->max_order;
00667 }
00668
00669 opt_order = *bd->opt_order;
00670
00671 if (opt_order) {
00672 int add_base;
00673
00674 if (sconf->coef_table == 3) {
00675 add_base = 0x7F;
00676
00677
00678 quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
00679
00680
00681 if (opt_order > 1)
00682 quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
00683
00684
00685 for (k = 2; k < opt_order; k++)
00686 quant_cof[k] = get_bits(gb, 7);
00687 } else {
00688 int k_max;
00689 add_base = 1;
00690
00691
00692 k_max = FFMIN(opt_order, 20);
00693 for (k = 0; k < k_max; k++) {
00694 int rice_param = parcor_rice_table[sconf->coef_table][k][1];
00695 int offset = parcor_rice_table[sconf->coef_table][k][0];
00696 quant_cof[k] = decode_rice(gb, rice_param) + offset;
00697 }
00698
00699
00700 k_max = FFMIN(opt_order, 127);
00701 for (; k < k_max; k++)
00702 quant_cof[k] = decode_rice(gb, 2) + (k & 1);
00703
00704
00705 for (; k < opt_order; k++)
00706 quant_cof[k] = decode_rice(gb, 1);
00707
00708 quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
00709
00710 if (opt_order > 1)
00711 quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
00712 }
00713
00714 for (k = 2; k < opt_order; k++)
00715 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
00716 }
00717 }
00718
00719
00720 if (sconf->long_term_prediction) {
00721 *bd->use_ltp = get_bits1(gb);
00722
00723 if (*bd->use_ltp) {
00724 int r, c;
00725
00726 bd->ltp_gain[0] = decode_rice(gb, 1) << 3;
00727 bd->ltp_gain[1] = decode_rice(gb, 2) << 3;
00728
00729 r = get_unary(gb, 0, 4);
00730 c = get_bits(gb, 2);
00731 bd->ltp_gain[2] = ltp_gain_values[r][c];
00732
00733 bd->ltp_gain[3] = decode_rice(gb, 2) << 3;
00734 bd->ltp_gain[4] = decode_rice(gb, 1) << 3;
00735
00736 *bd->ltp_lag = get_bits(gb, ctx->ltp_lag_length);
00737 *bd->ltp_lag += FFMAX(4, opt_order + 1);
00738 }
00739 }
00740
00741
00742 if (bd->ra_block) {
00743 if (opt_order)
00744 bd->raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
00745 if (opt_order > 1)
00746 bd->raw_samples[1] = decode_rice(gb, FFMIN(s[0] + 3, ctx->s_max));
00747 if (opt_order > 2)
00748 bd->raw_samples[2] = decode_rice(gb, FFMIN(s[0] + 1, ctx->s_max));
00749
00750 start = FFMIN(opt_order, 3);
00751 }
00752
00753
00754 if (sconf->bgmc) {
00755 int delta[8];
00756 unsigned int k [8];
00757 unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5);
00758 unsigned int i = start;
00759
00760
00761 unsigned int high;
00762 unsigned int low;
00763 unsigned int value;
00764
00765 ff_bgmc_decode_init(gb, &high, &low, &value);
00766
00767 current_res = bd->raw_samples + start;
00768
00769 for (sb = 0; sb < sub_blocks; sb++, i = 0) {
00770 k [sb] = s[sb] > b ? s[sb] - b : 0;
00771 delta[sb] = 5 - s[sb] + k[sb];
00772
00773 ff_bgmc_decode(gb, sb_length, current_res,
00774 delta[sb], sx[sb], &high, &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status);
00775
00776 current_res += sb_length;
00777 }
00778
00779 ff_bgmc_decode_end(gb);
00780
00781
00782
00783 i = start;
00784 current_res = bd->raw_samples + start;
00785
00786 for (sb = 0; sb < sub_blocks; sb++, i = 0) {
00787 unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]];
00788 unsigned int cur_k = k[sb];
00789 unsigned int cur_s = s[sb];
00790
00791 for (; i < sb_length; i++) {
00792 int32_t res = *current_res;
00793
00794 if (res == cur_tail_code) {
00795 unsigned int max_msb = (2 + (sx[sb] > 2) + (sx[sb] > 10))
00796 << (5 - delta[sb]);
00797
00798 res = decode_rice(gb, cur_s);
00799
00800 if (res >= 0) {
00801 res += (max_msb ) << cur_k;
00802 } else {
00803 res -= (max_msb - 1) << cur_k;
00804 }
00805 } else {
00806 if (res > cur_tail_code)
00807 res--;
00808
00809 if (res & 1)
00810 res = -res;
00811
00812 res >>= 1;
00813
00814 if (cur_k) {
00815 res <<= cur_k;
00816 res |= get_bits_long(gb, cur_k);
00817 }
00818 }
00819
00820 *current_res++ = res;
00821 }
00822 }
00823 } else {
00824 current_res = bd->raw_samples + start;
00825
00826 for (sb = 0; sb < sub_blocks; sb++, start = 0)
00827 for (; start < sb_length; start++)
00828 *current_res++ = decode_rice(gb, s[sb]);
00829 }
00830
00831 if (!sconf->mc_coding || ctx->js_switch)
00832 align_get_bits(gb);
00833
00834 return 0;
00835 }
00836
00837
00840 static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00841 {
00842 ALSSpecificConfig *sconf = &ctx->sconf;
00843 unsigned int block_length = bd->block_length;
00844 unsigned int smp = 0;
00845 unsigned int k;
00846 int opt_order = *bd->opt_order;
00847 int sb;
00848 int64_t y;
00849 int32_t *quant_cof = bd->quant_cof;
00850 int32_t *lpc_cof = bd->lpc_cof;
00851 int32_t *raw_samples = bd->raw_samples;
00852 int32_t *raw_samples_end = bd->raw_samples + bd->block_length;
00853 int32_t *lpc_cof_reversed = ctx->lpc_cof_reversed_buffer;
00854
00855
00856 if (*bd->use_ltp) {
00857 int ltp_smp;
00858
00859 for (ltp_smp = FFMAX(*bd->ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
00860 int center = ltp_smp - *bd->ltp_lag;
00861 int begin = FFMAX(0, center - 2);
00862 int end = center + 3;
00863 int tab = 5 - (end - begin);
00864 int base;
00865
00866 y = 1 << 6;
00867
00868 for (base = begin; base < end; base++, tab++)
00869 y += MUL64(bd->ltp_gain[tab], raw_samples[base]);
00870
00871 raw_samples[ltp_smp] += y >> 7;
00872 }
00873 }
00874
00875
00876 if (bd->ra_block) {
00877 for (smp = 0; smp < opt_order; smp++) {
00878 y = 1 << 19;
00879
00880 for (sb = 0; sb < smp; sb++)
00881 y += MUL64(lpc_cof[sb], raw_samples[-(sb + 1)]);
00882
00883 *raw_samples++ -= y >> 20;
00884 parcor_to_lpc(smp, quant_cof, lpc_cof);
00885 }
00886 } else {
00887 for (k = 0; k < opt_order; k++)
00888 parcor_to_lpc(k, quant_cof, lpc_cof);
00889
00890
00891 if (*bd->store_prev_samples)
00892 memcpy(bd->prev_raw_samples, raw_samples - sconf->max_order,
00893 sizeof(*bd->prev_raw_samples) * sconf->max_order);
00894
00895
00896 if (bd->js_blocks && bd->raw_other) {
00897 int32_t *left, *right;
00898
00899 if (bd->raw_other > raw_samples) {
00900 left = raw_samples;
00901 right = bd->raw_other;
00902 } else {
00903 left = bd->raw_other;
00904 right = raw_samples;
00905 }
00906
00907 for (sb = -1; sb >= -sconf->max_order; sb--)
00908 raw_samples[sb] = right[sb] - left[sb];
00909 }
00910
00911
00912 if (*bd->shift_lsbs)
00913 for (sb = -1; sb >= -sconf->max_order; sb--)
00914 raw_samples[sb] >>= *bd->shift_lsbs;
00915 }
00916
00917
00918 lpc_cof = lpc_cof + opt_order;
00919
00920 for (sb = 0; sb < opt_order; sb++)
00921 lpc_cof_reversed[sb] = lpc_cof[-(sb + 1)];
00922
00923
00924 raw_samples = bd->raw_samples + smp;
00925 lpc_cof = lpc_cof_reversed + opt_order;
00926
00927 for (; raw_samples < raw_samples_end; raw_samples++) {
00928 y = 1 << 19;
00929
00930 for (sb = -opt_order; sb < 0; sb++)
00931 y += MUL64(lpc_cof[sb], raw_samples[sb]);
00932
00933 *raw_samples -= y >> 20;
00934 }
00935
00936 raw_samples = bd->raw_samples;
00937
00938
00939 if (*bd->store_prev_samples)
00940 memcpy(raw_samples - sconf->max_order, bd->prev_raw_samples,
00941 sizeof(*raw_samples) * sconf->max_order);
00942
00943 return 0;
00944 }
00945
00946
00949 static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
00950 {
00951 GetBitContext *gb = &ctx->gb;
00952
00953 *bd->shift_lsbs = 0;
00954
00955 if (get_bits1(gb)) {
00956 if (read_var_block_data(ctx, bd))
00957 return -1;
00958 } else {
00959 read_const_block_data(ctx, bd);
00960 }
00961
00962 return 0;
00963 }
00964
00965
00968 static int decode_block(ALSDecContext *ctx, ALSBlockData *bd)
00969 {
00970 unsigned int smp;
00971
00972
00973 if (*bd->const_block)
00974 decode_const_block_data(ctx, bd);
00975 else if (decode_var_block_data(ctx, bd))
00976 return -1;
00977
00978
00979
00980 if (*bd->shift_lsbs)
00981 for (smp = 0; smp < bd->block_length; smp++)
00982 bd->raw_samples[smp] <<= *bd->shift_lsbs;
00983
00984 return 0;
00985 }
00986
00987
00990 static int read_decode_block(ALSDecContext *ctx, ALSBlockData *bd)
00991 {
00992 int ret;
00993
00994 ret = read_block(ctx, bd);
00995
00996 if (ret)
00997 return ret;
00998
00999 ret = decode_block(ctx, bd);
01000
01001 return ret;
01002 }
01003
01004
01008 static void zero_remaining(unsigned int b, unsigned int b_max,
01009 const unsigned int *div_blocks, int32_t *buf)
01010 {
01011 unsigned int count = 0;
01012
01013 for (; b < b_max; b++)
01014 count += div_blocks[b];
01015
01016 if (count)
01017 memset(buf, 0, sizeof(*buf) * count);
01018 }
01019
01020
01023 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
01024 unsigned int c, const unsigned int *div_blocks,
01025 unsigned int *js_blocks)
01026 {
01027 unsigned int b;
01028 ALSBlockData bd;
01029
01030 memset(&bd, 0, sizeof(ALSBlockData));
01031
01032 bd.ra_block = ra_frame;
01033 bd.const_block = ctx->const_block;
01034 bd.shift_lsbs = ctx->shift_lsbs;
01035 bd.opt_order = ctx->opt_order;
01036 bd.store_prev_samples = ctx->store_prev_samples;
01037 bd.use_ltp = ctx->use_ltp;
01038 bd.ltp_lag = ctx->ltp_lag;
01039 bd.ltp_gain = ctx->ltp_gain[0];
01040 bd.quant_cof = ctx->quant_cof[0];
01041 bd.lpc_cof = ctx->lpc_cof[0];
01042 bd.prev_raw_samples = ctx->prev_raw_samples;
01043 bd.raw_samples = ctx->raw_samples[c];
01044
01045
01046 for (b = 0; b < ctx->num_blocks; b++) {
01047 bd.block_length = div_blocks[b];
01048
01049 if (read_decode_block(ctx, &bd)) {
01050
01051 zero_remaining(b, ctx->num_blocks, div_blocks, bd.raw_samples);
01052 return -1;
01053 }
01054 bd.raw_samples += div_blocks[b];
01055 bd.ra_block = 0;
01056 }
01057
01058 return 0;
01059 }
01060
01061
01064 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
01065 unsigned int c, const unsigned int *div_blocks,
01066 unsigned int *js_blocks)
01067 {
01068 ALSSpecificConfig *sconf = &ctx->sconf;
01069 unsigned int offset = 0;
01070 unsigned int b;
01071 ALSBlockData bd[2];
01072
01073 memset(bd, 0, 2 * sizeof(ALSBlockData));
01074
01075 bd[0].ra_block = ra_frame;
01076 bd[0].const_block = ctx->const_block;
01077 bd[0].shift_lsbs = ctx->shift_lsbs;
01078 bd[0].opt_order = ctx->opt_order;
01079 bd[0].store_prev_samples = ctx->store_prev_samples;
01080 bd[0].use_ltp = ctx->use_ltp;
01081 bd[0].ltp_lag = ctx->ltp_lag;
01082 bd[0].ltp_gain = ctx->ltp_gain[0];
01083 bd[0].quant_cof = ctx->quant_cof[0];
01084 bd[0].lpc_cof = ctx->lpc_cof[0];
01085 bd[0].prev_raw_samples = ctx->prev_raw_samples;
01086 bd[0].js_blocks = *js_blocks;
01087
01088 bd[1].ra_block = ra_frame;
01089 bd[1].const_block = ctx->const_block;
01090 bd[1].shift_lsbs = ctx->shift_lsbs;
01091 bd[1].opt_order = ctx->opt_order;
01092 bd[1].store_prev_samples = ctx->store_prev_samples;
01093 bd[1].use_ltp = ctx->use_ltp;
01094 bd[1].ltp_lag = ctx->ltp_lag;
01095 bd[1].ltp_gain = ctx->ltp_gain[0];
01096 bd[1].quant_cof = ctx->quant_cof[0];
01097 bd[1].lpc_cof = ctx->lpc_cof[0];
01098 bd[1].prev_raw_samples = ctx->prev_raw_samples;
01099 bd[1].js_blocks = *(js_blocks + 1);
01100
01101
01102 for (b = 0; b < ctx->num_blocks; b++) {
01103 unsigned int s;
01104
01105 bd[0].block_length = div_blocks[b];
01106 bd[1].block_length = div_blocks[b];
01107
01108 bd[0].raw_samples = ctx->raw_samples[c ] + offset;
01109 bd[1].raw_samples = ctx->raw_samples[c + 1] + offset;
01110
01111 bd[0].raw_other = bd[1].raw_samples;
01112 bd[1].raw_other = bd[0].raw_samples;
01113
01114 if(read_decode_block(ctx, &bd[0]) || read_decode_block(ctx, &bd[1])) {
01115
01116 zero_remaining(b, ctx->num_blocks, div_blocks, bd[0].raw_samples);
01117 zero_remaining(b, ctx->num_blocks, div_blocks, bd[1].raw_samples);
01118 return -1;
01119 }
01120
01121
01122 if (bd[0].js_blocks) {
01123 if (bd[1].js_blocks)
01124 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
01125
01126 for (s = 0; s < div_blocks[b]; s++)
01127 bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s];
01128 } else if (bd[1].js_blocks) {
01129 for (s = 0; s < div_blocks[b]; s++)
01130 bd[1].raw_samples[s] = bd[1].raw_samples[s] + bd[0].raw_samples[s];
01131 }
01132
01133 offset += div_blocks[b];
01134 bd[0].ra_block = 0;
01135 bd[1].ra_block = 0;
01136 }
01137
01138
01139
01140 memmove(ctx->raw_samples[c] - sconf->max_order,
01141 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01142 sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01143
01144 return 0;
01145 }
01146
01147
01150 static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
01151 {
01152 GetBitContext *gb = &ctx->gb;
01153 ALSChannelData *current = cd;
01154 unsigned int channels = ctx->avctx->channels;
01155 int entries = 0;
01156
01157 while (entries < channels && !(current->stop_flag = get_bits1(gb))) {
01158 current->master_channel = get_bits_long(gb, av_ceil_log2(channels));
01159
01160 if (current->master_channel >= channels) {
01161 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n");
01162 return -1;
01163 }
01164
01165 if (current->master_channel != c) {
01166 current->time_diff_flag = get_bits1(gb);
01167 current->weighting[0] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01168 current->weighting[1] = mcc_weightings[av_clip(decode_rice(gb, 2) + 14, 0, 32)];
01169 current->weighting[2] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01170
01171 if (current->time_diff_flag) {
01172 current->weighting[3] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01173 current->weighting[4] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01174 current->weighting[5] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01175
01176 current->time_diff_sign = get_bits1(gb);
01177 current->time_diff_index = get_bits(gb, ctx->ltp_lag_length - 3) + 3;
01178 }
01179 }
01180
01181 current++;
01182 entries++;
01183 }
01184
01185 if (entries == channels) {
01186 av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n");
01187 return -1;
01188 }
01189
01190 align_get_bits(gb);
01191 return 0;
01192 }
01193
01194
01197 static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
01198 ALSChannelData **cd, int *reverted,
01199 unsigned int offset, int c)
01200 {
01201 ALSChannelData *ch = cd[c];
01202 unsigned int dep = 0;
01203 unsigned int channels = ctx->avctx->channels;
01204
01205 if (reverted[c])
01206 return 0;
01207
01208 reverted[c] = 1;
01209
01210 while (dep < channels && !ch[dep].stop_flag) {
01211 revert_channel_correlation(ctx, bd, cd, reverted, offset,
01212 ch[dep].master_channel);
01213
01214 dep++;
01215 }
01216
01217 if (dep == channels) {
01218 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n");
01219 return -1;
01220 }
01221
01222 bd->const_block = ctx->const_block + c;
01223 bd->shift_lsbs = ctx->shift_lsbs + c;
01224 bd->opt_order = ctx->opt_order + c;
01225 bd->store_prev_samples = ctx->store_prev_samples + c;
01226 bd->use_ltp = ctx->use_ltp + c;
01227 bd->ltp_lag = ctx->ltp_lag + c;
01228 bd->ltp_gain = ctx->ltp_gain[c];
01229 bd->lpc_cof = ctx->lpc_cof[c];
01230 bd->quant_cof = ctx->quant_cof[c];
01231 bd->raw_samples = ctx->raw_samples[c] + offset;
01232
01233 dep = 0;
01234 while (!ch[dep].stop_flag) {
01235 unsigned int smp;
01236 unsigned int begin = 1;
01237 unsigned int end = bd->block_length - 1;
01238 int64_t y;
01239 int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;
01240
01241 if (ch[dep].time_diff_flag) {
01242 int t = ch[dep].time_diff_index;
01243
01244 if (ch[dep].time_diff_sign) {
01245 t = -t;
01246 begin -= t;
01247 } else {
01248 end -= t;
01249 }
01250
01251 for (smp = begin; smp < end; smp++) {
01252 y = (1 << 6) +
01253 MUL64(ch[dep].weighting[0], master[smp - 1 ]) +
01254 MUL64(ch[dep].weighting[1], master[smp ]) +
01255 MUL64(ch[dep].weighting[2], master[smp + 1 ]) +
01256 MUL64(ch[dep].weighting[3], master[smp - 1 + t]) +
01257 MUL64(ch[dep].weighting[4], master[smp + t]) +
01258 MUL64(ch[dep].weighting[5], master[smp + 1 + t]);
01259
01260 bd->raw_samples[smp] += y >> 7;
01261 }
01262 } else {
01263 for (smp = begin; smp < end; smp++) {
01264 y = (1 << 6) +
01265 MUL64(ch[dep].weighting[0], master[smp - 1]) +
01266 MUL64(ch[dep].weighting[1], master[smp ]) +
01267 MUL64(ch[dep].weighting[2], master[smp + 1]);
01268
01269 bd->raw_samples[smp] += y >> 7;
01270 }
01271 }
01272
01273 dep++;
01274 }
01275
01276 return 0;
01277 }
01278
01279
01282 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
01283 {
01284 ALSSpecificConfig *sconf = &ctx->sconf;
01285 AVCodecContext *avctx = ctx->avctx;
01286 GetBitContext *gb = &ctx->gb;
01287 unsigned int div_blocks[32];
01288 unsigned int c;
01289 unsigned int js_blocks[2];
01290
01291 uint32_t bs_info = 0;
01292
01293
01294 if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
01295 skip_bits_long(gb, 32);
01296
01297 if (sconf->mc_coding && sconf->joint_stereo) {
01298 ctx->js_switch = get_bits1(gb);
01299 align_get_bits(gb);
01300 }
01301
01302 if (!sconf->mc_coding || ctx->js_switch) {
01303 int independent_bs = !sconf->joint_stereo;
01304
01305 for (c = 0; c < avctx->channels; c++) {
01306 js_blocks[0] = 0;
01307 js_blocks[1] = 0;
01308
01309 get_block_sizes(ctx, div_blocks, &bs_info);
01310
01311
01312
01313 if (sconf->joint_stereo && sconf->block_switching)
01314 if (bs_info >> 31)
01315 independent_bs = 2;
01316
01317
01318 if (c == avctx->channels - 1)
01319 independent_bs = 1;
01320
01321 if (independent_bs) {
01322 if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))
01323 return -1;
01324
01325 independent_bs--;
01326 } else {
01327 if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))
01328 return -1;
01329
01330 c++;
01331 }
01332
01333
01334 memmove(ctx->raw_samples[c] - sconf->max_order,
01335 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01336 sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01337 }
01338 } else {
01339 ALSBlockData bd;
01340 int b;
01341 int *reverted_channels = ctx->reverted_channels;
01342 unsigned int offset = 0;
01343
01344 for (c = 0; c < avctx->channels; c++)
01345 if (ctx->chan_data[c] < ctx->chan_data_buffer) {
01346 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");
01347 return -1;
01348 }
01349
01350 memset(&bd, 0, sizeof(ALSBlockData));
01351 memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);
01352
01353 bd.ra_block = ra_frame;
01354 bd.prev_raw_samples = ctx->prev_raw_samples;
01355
01356 get_block_sizes(ctx, div_blocks, &bs_info);
01357
01358 for (b = 0; b < ctx->num_blocks; b++) {
01359 bd.block_length = div_blocks[b];
01360
01361 for (c = 0; c < avctx->channels; c++) {
01362 bd.const_block = ctx->const_block + c;
01363 bd.shift_lsbs = ctx->shift_lsbs + c;
01364 bd.opt_order = ctx->opt_order + c;
01365 bd.store_prev_samples = ctx->store_prev_samples + c;
01366 bd.use_ltp = ctx->use_ltp + c;
01367 bd.ltp_lag = ctx->ltp_lag + c;
01368 bd.ltp_gain = ctx->ltp_gain[c];
01369 bd.lpc_cof = ctx->lpc_cof[c];
01370 bd.quant_cof = ctx->quant_cof[c];
01371 bd.raw_samples = ctx->raw_samples[c] + offset;
01372 bd.raw_other = NULL;
01373
01374 read_block(ctx, &bd);
01375 if (read_channel_data(ctx, ctx->chan_data[c], c))
01376 return -1;
01377 }
01378
01379 for (c = 0; c < avctx->channels; c++)
01380 if (revert_channel_correlation(ctx, &bd, ctx->chan_data,
01381 reverted_channels, offset, c))
01382 return -1;
01383
01384 for (c = 0; c < avctx->channels; c++) {
01385 bd.const_block = ctx->const_block + c;
01386 bd.shift_lsbs = ctx->shift_lsbs + c;
01387 bd.opt_order = ctx->opt_order + c;
01388 bd.store_prev_samples = ctx->store_prev_samples + c;
01389 bd.use_ltp = ctx->use_ltp + c;
01390 bd.ltp_lag = ctx->ltp_lag + c;
01391 bd.ltp_gain = ctx->ltp_gain[c];
01392 bd.lpc_cof = ctx->lpc_cof[c];
01393 bd.quant_cof = ctx->quant_cof[c];
01394 bd.raw_samples = ctx->raw_samples[c] + offset;
01395 decode_block(ctx, &bd);
01396 }
01397
01398 memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));
01399 offset += div_blocks[b];
01400 bd.ra_block = 0;
01401 }
01402
01403
01404 for (c = 0; c < avctx->channels; c++)
01405 memmove(ctx->raw_samples[c] - sconf->max_order,
01406 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01407 sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01408 }
01409
01410
01411
01412 return 0;
01413 }
01414
01415
01418 static int decode_frame(AVCodecContext *avctx,
01419 void *data, int *data_size,
01420 AVPacket *avpkt)
01421 {
01422 ALSDecContext *ctx = avctx->priv_data;
01423 ALSSpecificConfig *sconf = &ctx->sconf;
01424 const uint8_t *buffer = avpkt->data;
01425 int buffer_size = avpkt->size;
01426 int invalid_frame, size;
01427 unsigned int c, sample, ra_frame, bytes_read, shift;
01428
01429 init_get_bits(&ctx->gb, buffer, buffer_size * 8);
01430
01431
01432
01433
01434
01435 ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
01436
01437
01438 if (sconf->samples != 0xFFFFFFFF)
01439 ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
01440 sconf->frame_length);
01441 else
01442 ctx->cur_frame_length = sconf->frame_length;
01443
01444
01445 if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
01446 av_log(ctx->avctx, AV_LOG_WARNING,
01447 "Reading frame data failed. Skipping RA unit.\n");
01448
01449 ctx->frame_id++;
01450
01451
01452 size = ctx->cur_frame_length * avctx->channels *
01453 av_get_bytes_per_sample(avctx->sample_fmt);
01454
01455 if (size > *data_size) {
01456 av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n");
01457 return -1;
01458 }
01459
01460 *data_size = size;
01461
01462
01463 #define INTERLEAVE_OUTPUT(bps) \
01464 { \
01465 int##bps##_t *dest = (int##bps##_t*) data; \
01466 shift = bps - ctx->avctx->bits_per_raw_sample; \
01467 for (sample = 0; sample < ctx->cur_frame_length; sample++) \
01468 for (c = 0; c < avctx->channels; c++) \
01469 *dest++ = ctx->raw_samples[c][sample] << shift; \
01470 }
01471
01472 if (ctx->avctx->bits_per_raw_sample <= 16) {
01473 INTERLEAVE_OUTPUT(16)
01474 } else {
01475 INTERLEAVE_OUTPUT(32)
01476 }
01477
01478
01479 if (sconf->crc_enabled && avctx->error_recognition >= FF_ER_CAREFUL) {
01480 int swap = HAVE_BIGENDIAN != sconf->msb_first;
01481
01482 if (ctx->avctx->bits_per_raw_sample == 24) {
01483 int32_t *src = data;
01484
01485 for (sample = 0;
01486 sample < ctx->cur_frame_length * avctx->channels;
01487 sample++) {
01488 int32_t v;
01489
01490 if (swap)
01491 v = av_bswap32(src[sample]);
01492 else
01493 v = src[sample];
01494 if (!HAVE_BIGENDIAN)
01495 v >>= 8;
01496
01497 ctx->crc = av_crc(ctx->crc_table, ctx->crc, (uint8_t*)(&v), 3);
01498 }
01499 } else {
01500 uint8_t *crc_source;
01501
01502 if (swap) {
01503 if (ctx->avctx->bits_per_raw_sample <= 16) {
01504 int16_t *src = (int16_t*) data;
01505 int16_t *dest = (int16_t*) ctx->crc_buffer;
01506 for (sample = 0;
01507 sample < ctx->cur_frame_length * avctx->channels;
01508 sample++)
01509 *dest++ = av_bswap16(src[sample]);
01510 } else {
01511 ctx->dsp.bswap_buf((uint32_t*)ctx->crc_buffer, data,
01512 ctx->cur_frame_length * avctx->channels);
01513 }
01514 crc_source = ctx->crc_buffer;
01515 } else {
01516 crc_source = data;
01517 }
01518
01519 ctx->crc = av_crc(ctx->crc_table, ctx->crc, crc_source, size);
01520 }
01521
01522
01523
01524 if (ctx->cur_frame_length != sconf->frame_length &&
01525 ctx->crc_org != ctx->crc) {
01526 av_log(avctx, AV_LOG_ERROR, "CRC error.\n");
01527 }
01528 }
01529
01530
01531 bytes_read = invalid_frame ? buffer_size :
01532 (get_bits_count(&ctx->gb) + 7) >> 3;
01533
01534 return bytes_read;
01535 }
01536
01537
01540 static av_cold int decode_end(AVCodecContext *avctx)
01541 {
01542 ALSDecContext *ctx = avctx->priv_data;
01543
01544 av_freep(&ctx->sconf.chan_pos);
01545
01546 ff_bgmc_end(&ctx->bgmc_lut, &ctx->bgmc_lut_status);
01547
01548 av_freep(&ctx->const_block);
01549 av_freep(&ctx->shift_lsbs);
01550 av_freep(&ctx->opt_order);
01551 av_freep(&ctx->store_prev_samples);
01552 av_freep(&ctx->use_ltp);
01553 av_freep(&ctx->ltp_lag);
01554 av_freep(&ctx->ltp_gain);
01555 av_freep(&ctx->ltp_gain_buffer);
01556 av_freep(&ctx->quant_cof);
01557 av_freep(&ctx->lpc_cof);
01558 av_freep(&ctx->quant_cof_buffer);
01559 av_freep(&ctx->lpc_cof_buffer);
01560 av_freep(&ctx->lpc_cof_reversed_buffer);
01561 av_freep(&ctx->prev_raw_samples);
01562 av_freep(&ctx->raw_samples);
01563 av_freep(&ctx->raw_buffer);
01564 av_freep(&ctx->chan_data);
01565 av_freep(&ctx->chan_data_buffer);
01566 av_freep(&ctx->reverted_channels);
01567 av_freep(&ctx->crc_buffer);
01568
01569 return 0;
01570 }
01571
01572
01575 static av_cold int decode_init(AVCodecContext *avctx)
01576 {
01577 unsigned int c;
01578 unsigned int channel_size;
01579 int num_buffers;
01580 ALSDecContext *ctx = avctx->priv_data;
01581 ALSSpecificConfig *sconf = &ctx->sconf;
01582 ctx->avctx = avctx;
01583
01584 if (!avctx->extradata) {
01585 av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
01586 return -1;
01587 }
01588
01589 if (read_specific_config(ctx)) {
01590 av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
01591 decode_end(avctx);
01592 return -1;
01593 }
01594
01595 if (check_specific_config(ctx)) {
01596 decode_end(avctx);
01597 return -1;
01598 }
01599
01600 if (sconf->bgmc)
01601 ff_bgmc_init(avctx, &ctx->bgmc_lut, &ctx->bgmc_lut_status);
01602
01603 if (sconf->floating) {
01604 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01605 avctx->bits_per_raw_sample = 32;
01606 } else {
01607 avctx->sample_fmt = sconf->resolution > 1
01608 ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
01609 avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
01610 }
01611
01612
01613
01614
01615 ctx->s_max = sconf->resolution > 1 ? 31 : 15;
01616
01617
01618 ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) +
01619 (avctx->sample_rate >= 192000);
01620
01621
01622 num_buffers = sconf->mc_coding ? avctx->channels : 1;
01623
01624 ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * num_buffers);
01625 ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * num_buffers);
01626 ctx->quant_cof_buffer = av_malloc(sizeof(*ctx->quant_cof_buffer) *
01627 num_buffers * sconf->max_order);
01628 ctx->lpc_cof_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
01629 num_buffers * sconf->max_order);
01630 ctx->lpc_cof_reversed_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
01631 sconf->max_order);
01632
01633 if (!ctx->quant_cof || !ctx->lpc_cof ||
01634 !ctx->quant_cof_buffer || !ctx->lpc_cof_buffer ||
01635 !ctx->lpc_cof_reversed_buffer) {
01636 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01637 return AVERROR(ENOMEM);
01638 }
01639
01640
01641 for (c = 0; c < num_buffers; c++) {
01642 ctx->quant_cof[c] = ctx->quant_cof_buffer + c * sconf->max_order;
01643 ctx->lpc_cof[c] = ctx->lpc_cof_buffer + c * sconf->max_order;
01644 }
01645
01646
01647 ctx->const_block = av_malloc (sizeof(*ctx->const_block) * num_buffers);
01648 ctx->shift_lsbs = av_malloc (sizeof(*ctx->shift_lsbs) * num_buffers);
01649 ctx->opt_order = av_malloc (sizeof(*ctx->opt_order) * num_buffers);
01650 ctx->store_prev_samples = av_malloc(sizeof(*ctx->store_prev_samples) * num_buffers);
01651 ctx->use_ltp = av_mallocz(sizeof(*ctx->use_ltp) * num_buffers);
01652 ctx->ltp_lag = av_malloc (sizeof(*ctx->ltp_lag) * num_buffers);
01653 ctx->ltp_gain = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers);
01654 ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) *
01655 num_buffers * 5);
01656
01657 if (!ctx->const_block || !ctx->shift_lsbs ||
01658 !ctx->opt_order || !ctx->store_prev_samples ||
01659 !ctx->use_ltp || !ctx->ltp_lag ||
01660 !ctx->ltp_gain || !ctx->ltp_gain_buffer) {
01661 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01662 decode_end(avctx);
01663 return AVERROR(ENOMEM);
01664 }
01665
01666 for (c = 0; c < num_buffers; c++)
01667 ctx->ltp_gain[c] = ctx->ltp_gain_buffer + c * 5;
01668
01669
01670 if (sconf->mc_coding) {
01671 ctx->chan_data_buffer = av_malloc(sizeof(*ctx->chan_data_buffer) *
01672 num_buffers * num_buffers);
01673 ctx->chan_data = av_malloc(sizeof(*ctx->chan_data) *
01674 num_buffers);
01675 ctx->reverted_channels = av_malloc(sizeof(*ctx->reverted_channels) *
01676 num_buffers);
01677
01678 if (!ctx->chan_data_buffer || !ctx->chan_data || !ctx->reverted_channels) {
01679 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01680 decode_end(avctx);
01681 return AVERROR(ENOMEM);
01682 }
01683
01684 for (c = 0; c < num_buffers; c++)
01685 ctx->chan_data[c] = ctx->chan_data_buffer + c * num_buffers;
01686 } else {
01687 ctx->chan_data = NULL;
01688 ctx->chan_data_buffer = NULL;
01689 ctx->reverted_channels = NULL;
01690 }
01691
01692 avctx->frame_size = sconf->frame_length;
01693 channel_size = sconf->frame_length + sconf->max_order;
01694
01695 ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
01696 ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);
01697 ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels);
01698
01699
01700 if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
01701 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01702 decode_end(avctx);
01703 return AVERROR(ENOMEM);
01704 }
01705
01706
01707 ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
01708 for (c = 1; c < avctx->channels; c++)
01709 ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
01710
01711
01712 if (HAVE_BIGENDIAN != sconf->msb_first && sconf->crc_enabled &&
01713 avctx->error_recognition >= FF_ER_CAREFUL) {
01714 ctx->crc_buffer = av_malloc(sizeof(*ctx->crc_buffer) *
01715 ctx->cur_frame_length *
01716 avctx->channels *
01717 av_get_bytes_per_sample(avctx->sample_fmt));
01718 if (!ctx->crc_buffer) {
01719 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01720 decode_end(avctx);
01721 return AVERROR(ENOMEM);
01722 }
01723 }
01724
01725 dsputil_init(&ctx->dsp, avctx);
01726
01727 return 0;
01728 }
01729
01730
01733 static av_cold void flush(AVCodecContext *avctx)
01734 {
01735 ALSDecContext *ctx = avctx->priv_data;
01736
01737 ctx->frame_id = 0;
01738 }
01739
01740
01741 AVCodec ff_als_decoder = {
01742 "als",
01743 AVMEDIA_TYPE_AUDIO,
01744 CODEC_ID_MP4ALS,
01745 sizeof(ALSDecContext),
01746 decode_init,
01747 NULL,
01748 decode_end,
01749 decode_frame,
01750 .flush = flush,
01751 .capabilities = CODEC_CAP_SUBFRAMES,
01752 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
01753 };
01754