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