00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00049 #include "avcodec.h"
00050 #include "get_bits.h"
00051 #include "bytestream.h"
00052 #include "unary.h"
00053 #include "mathops.h"
00054
00055 #define ALAC_EXTRADATA_SIZE 36
00056 #define MAX_CHANNELS 2
00057
00058 typedef struct {
00059
00060 AVCodecContext *avctx;
00061 AVFrame frame;
00062 GetBitContext gb;
00063
00064 int numchannels;
00065
00066
00067 int32_t *predicterror_buffer[MAX_CHANNELS];
00068
00069 int32_t *outputsamples_buffer[MAX_CHANNELS];
00070
00071 int32_t *extra_bits_buffer[MAX_CHANNELS];
00072
00073
00074 uint32_t setinfo_max_samples_per_frame;
00075 uint8_t setinfo_sample_size;
00076 uint8_t setinfo_rice_historymult;
00077 uint8_t setinfo_rice_initialhistory;
00078 uint8_t setinfo_rice_kmodifier;
00079
00080
00081 int extra_bits;
00082 } ALACContext;
00083
00084 static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){
00085
00086 int x = get_unary_0_9(gb);
00087
00088 if (x > 8) {
00089
00090 x = get_bits(gb, readsamplesize);
00091 } else {
00092 if (k >= limit)
00093 k = limit;
00094
00095 if (k != 1) {
00096 int extrabits = show_bits(gb, k);
00097
00098
00099 x = (x << k) - x;
00100
00101 if (extrabits > 1) {
00102 x += extrabits - 1;
00103 skip_bits(gb, k);
00104 } else
00105 skip_bits(gb, k - 1);
00106 }
00107 }
00108 return x;
00109 }
00110
00111 static void bastardized_rice_decompress(ALACContext *alac,
00112 int32_t *output_buffer,
00113 int output_size,
00114 int readsamplesize,
00115 int rice_initialhistory,
00116 int rice_kmodifier,
00117 int rice_historymult,
00118 int rice_kmodifier_mask
00119 )
00120 {
00121 int output_count;
00122 unsigned int history = rice_initialhistory;
00123 int sign_modifier = 0;
00124
00125 for (output_count = 0; output_count < output_size; output_count++) {
00126 int32_t x;
00127 int32_t x_modified;
00128 int32_t final_val;
00129
00130
00131 int k;
00132
00133
00134 k = av_log2((history >> 9) + 3);
00135 x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize);
00136
00137 x_modified = sign_modifier + x;
00138 final_val = (x_modified + 1) / 2;
00139 if (x_modified & 1) final_val *= -1;
00140
00141 output_buffer[output_count] = final_val;
00142
00143 sign_modifier = 0;
00144
00145
00146 history += x_modified * rice_historymult
00147 - ((history * rice_historymult) >> 9);
00148
00149 if (x_modified > 0xffff)
00150 history = 0xffff;
00151
00152
00153 if ((history < 128) && (output_count+1 < output_size)) {
00154 int k;
00155 unsigned int block_size;
00156
00157 sign_modifier = 1;
00158
00159 k = 7 - av_log2(history) + ((history + 16) >> 6 );
00160
00161 block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16);
00162
00163 if (block_size > 0) {
00164 if(block_size >= output_size - output_count){
00165 av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count);
00166 block_size= output_size - output_count - 1;
00167 }
00168 memset(&output_buffer[output_count+1], 0, block_size * 4);
00169 output_count += block_size;
00170 }
00171
00172 if (block_size > 0xffff)
00173 sign_modifier = 0;
00174
00175 history = 0;
00176 }
00177 }
00178 }
00179
00180 static inline int sign_only(int v)
00181 {
00182 return v ? FFSIGN(v) : 0;
00183 }
00184
00185 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
00186 int32_t *buffer_out,
00187 int output_size,
00188 int readsamplesize,
00189 int16_t *predictor_coef_table,
00190 int predictor_coef_num,
00191 int predictor_quantitization)
00192 {
00193 int i;
00194
00195
00196 *buffer_out = *error_buffer;
00197
00198 if (!predictor_coef_num) {
00199 if (output_size <= 1)
00200 return;
00201
00202 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
00203 return;
00204 }
00205
00206 if (predictor_coef_num == 0x1f) {
00207
00208
00209
00210 if (output_size <= 1)
00211 return;
00212 for (i = 0; i < output_size - 1; i++) {
00213 int32_t prev_value;
00214 int32_t error_value;
00215
00216 prev_value = buffer_out[i];
00217 error_value = error_buffer[i+1];
00218 buffer_out[i+1] =
00219 sign_extend((prev_value + error_value), readsamplesize);
00220 }
00221 return;
00222 }
00223
00224
00225 if (predictor_coef_num > 0)
00226 for (i = 0; i < predictor_coef_num; i++) {
00227 int32_t val;
00228
00229 val = buffer_out[i] + error_buffer[i+1];
00230 val = sign_extend(val, readsamplesize);
00231 buffer_out[i+1] = val;
00232 }
00233
00234
00235
00236
00237
00238
00239 if (predictor_coef_num > 0) {
00240 for (i = predictor_coef_num + 1; i < output_size; i++) {
00241 int j;
00242 int sum = 0;
00243 int outval;
00244 int error_val = error_buffer[i];
00245
00246 for (j = 0; j < predictor_coef_num; j++) {
00247 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
00248 predictor_coef_table[j];
00249 }
00250
00251 outval = (1 << (predictor_quantitization-1)) + sum;
00252 outval = outval >> predictor_quantitization;
00253 outval = outval + buffer_out[0] + error_val;
00254 outval = sign_extend(outval, readsamplesize);
00255
00256 buffer_out[predictor_coef_num+1] = outval;
00257
00258 if (error_val > 0) {
00259 int predictor_num = predictor_coef_num - 1;
00260
00261 while (predictor_num >= 0 && error_val > 0) {
00262 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
00263 int sign = sign_only(val);
00264
00265 predictor_coef_table[predictor_num] -= sign;
00266
00267 val *= sign;
00268
00269 error_val -= ((val >> predictor_quantitization) *
00270 (predictor_coef_num - predictor_num));
00271
00272 predictor_num--;
00273 }
00274 } else if (error_val < 0) {
00275 int predictor_num = predictor_coef_num - 1;
00276
00277 while (predictor_num >= 0 && error_val < 0) {
00278 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
00279 int sign = - sign_only(val);
00280
00281 predictor_coef_table[predictor_num] -= sign;
00282
00283 val *= sign;
00284
00285 error_val -= ((val >> predictor_quantitization) *
00286 (predictor_coef_num - predictor_num));
00287
00288 predictor_num--;
00289 }
00290 }
00291
00292 buffer_out++;
00293 }
00294 }
00295 }
00296
00297 static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS],
00298 int numsamples, uint8_t interlacing_shift,
00299 uint8_t interlacing_leftweight)
00300 {
00301 int i;
00302
00303 for (i = 0; i < numsamples; i++) {
00304 int32_t a, b;
00305
00306 a = buffer[0][i];
00307 b = buffer[1][i];
00308
00309 a -= (b * interlacing_leftweight) >> interlacing_shift;
00310 b += a;
00311
00312 buffer[0][i] = b;
00313 buffer[1][i] = a;
00314 }
00315 }
00316
00317 static void append_extra_bits(int32_t *buffer[MAX_CHANNELS],
00318 int32_t *extra_bits_buffer[MAX_CHANNELS],
00319 int extra_bits, int numchannels, int numsamples)
00320 {
00321 int i, ch;
00322
00323 for (ch = 0; ch < numchannels; ch++)
00324 for (i = 0; i < numsamples; i++)
00325 buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
00326 }
00327
00328 static void interleave_stereo_16(int32_t *buffer[MAX_CHANNELS],
00329 int16_t *buffer_out, int numsamples)
00330 {
00331 int i;
00332
00333 for (i = 0; i < numsamples; i++) {
00334 *buffer_out++ = buffer[0][i];
00335 *buffer_out++ = buffer[1][i];
00336 }
00337 }
00338
00339 static void interleave_stereo_24(int32_t *buffer[MAX_CHANNELS],
00340 int32_t *buffer_out, int numsamples)
00341 {
00342 int i;
00343
00344 for (i = 0; i < numsamples; i++) {
00345 *buffer_out++ = buffer[0][i] << 8;
00346 *buffer_out++ = buffer[1][i] << 8;
00347 }
00348 }
00349
00350 static int alac_decode_frame(AVCodecContext *avctx, void *data,
00351 int *got_frame_ptr, AVPacket *avpkt)
00352 {
00353 const uint8_t *inbuffer = avpkt->data;
00354 int input_buffer_size = avpkt->size;
00355 ALACContext *alac = avctx->priv_data;
00356
00357 int channels;
00358 unsigned int outputsamples;
00359 int hassize;
00360 unsigned int readsamplesize;
00361 int isnotcompressed;
00362 uint8_t interlacing_shift;
00363 uint8_t interlacing_leftweight;
00364 int i, ch, ret;
00365
00366 init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
00367
00368 channels = get_bits(&alac->gb, 3) + 1;
00369 if (channels != avctx->channels) {
00370 av_log(avctx, AV_LOG_ERROR, "frame header channel count mismatch\n");
00371 return AVERROR_INVALIDDATA;
00372 }
00373
00374
00375
00376
00377 skip_bits(&alac->gb, 4);
00378
00379 skip_bits(&alac->gb, 12);
00380
00381
00382 hassize = get_bits1(&alac->gb);
00383
00384 alac->extra_bits = get_bits(&alac->gb, 2) << 3;
00385
00386
00387 isnotcompressed = get_bits1(&alac->gb);
00388
00389 if (hassize) {
00390
00391 outputsamples = get_bits_long(&alac->gb, 32);
00392 if(outputsamples > alac->setinfo_max_samples_per_frame){
00393 av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", outputsamples, alac->setinfo_max_samples_per_frame);
00394 return -1;
00395 }
00396 } else
00397 outputsamples = alac->setinfo_max_samples_per_frame;
00398
00399
00400 if (outputsamples > INT32_MAX) {
00401 av_log(avctx, AV_LOG_ERROR, "unsupported block size: %u\n", outputsamples);
00402 return AVERROR_INVALIDDATA;
00403 }
00404 alac->frame.nb_samples = outputsamples;
00405 if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) {
00406 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00407 return ret;
00408 }
00409
00410 readsamplesize = alac->setinfo_sample_size - alac->extra_bits + channels - 1;
00411 if (readsamplesize > MIN_CACHE_BITS) {
00412 av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
00413 return -1;
00414 }
00415
00416 if (!isnotcompressed) {
00417
00418 int16_t predictor_coef_table[MAX_CHANNELS][32];
00419 int predictor_coef_num[MAX_CHANNELS];
00420 int prediction_type[MAX_CHANNELS];
00421 int prediction_quantitization[MAX_CHANNELS];
00422 int ricemodifier[MAX_CHANNELS];
00423
00424 interlacing_shift = get_bits(&alac->gb, 8);
00425 interlacing_leftweight = get_bits(&alac->gb, 8);
00426
00427 for (ch = 0; ch < channels; ch++) {
00428 prediction_type[ch] = get_bits(&alac->gb, 4);
00429 prediction_quantitization[ch] = get_bits(&alac->gb, 4);
00430
00431 ricemodifier[ch] = get_bits(&alac->gb, 3);
00432 predictor_coef_num[ch] = get_bits(&alac->gb, 5);
00433
00434
00435 for (i = 0; i < predictor_coef_num[ch]; i++)
00436 predictor_coef_table[ch][i] = (int16_t)get_bits(&alac->gb, 16);
00437 }
00438
00439 if (alac->extra_bits) {
00440 for (i = 0; i < outputsamples; i++) {
00441 for (ch = 0; ch < channels; ch++)
00442 alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
00443 }
00444 }
00445 for (ch = 0; ch < channels; ch++) {
00446 bastardized_rice_decompress(alac,
00447 alac->predicterror_buffer[ch],
00448 outputsamples,
00449 readsamplesize,
00450 alac->setinfo_rice_initialhistory,
00451 alac->setinfo_rice_kmodifier,
00452 ricemodifier[ch] * alac->setinfo_rice_historymult / 4,
00453 (1 << alac->setinfo_rice_kmodifier) - 1);
00454
00455
00456 if (prediction_type[ch] == 15) {
00457
00458
00459
00460
00461
00462
00463
00464 predictor_decompress_fir_adapt(alac->predicterror_buffer[ch],
00465 alac->predicterror_buffer[ch],
00466 outputsamples, readsamplesize,
00467 NULL, 31, 0);
00468 } else if (prediction_type[ch] > 0) {
00469 av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
00470 prediction_type[ch]);
00471 }
00472 predictor_decompress_fir_adapt(alac->predicterror_buffer[ch],
00473 alac->outputsamples_buffer[ch],
00474 outputsamples, readsamplesize,
00475 predictor_coef_table[ch],
00476 predictor_coef_num[ch],
00477 prediction_quantitization[ch]);
00478 }
00479 } else {
00480
00481 for (i = 0; i < outputsamples; i++) {
00482 for (ch = 0; ch < channels; ch++) {
00483 alac->outputsamples_buffer[ch][i] = get_sbits_long(&alac->gb,
00484 alac->setinfo_sample_size);
00485 }
00486 }
00487 alac->extra_bits = 0;
00488 interlacing_shift = 0;
00489 interlacing_leftweight = 0;
00490 }
00491 if (get_bits(&alac->gb, 3) != 7)
00492 av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n");
00493
00494 if (channels == 2 && interlacing_leftweight) {
00495 decorrelate_stereo(alac->outputsamples_buffer, outputsamples,
00496 interlacing_shift, interlacing_leftweight);
00497 }
00498
00499 if (alac->extra_bits) {
00500 append_extra_bits(alac->outputsamples_buffer, alac->extra_bits_buffer,
00501 alac->extra_bits, alac->numchannels, outputsamples);
00502 }
00503
00504 switch(alac->setinfo_sample_size) {
00505 case 16:
00506 if (channels == 2) {
00507 interleave_stereo_16(alac->outputsamples_buffer,
00508 (int16_t *)alac->frame.data[0], outputsamples);
00509 } else {
00510 int16_t *outbuffer = (int16_t *)alac->frame.data[0];
00511 for (i = 0; i < outputsamples; i++) {
00512 outbuffer[i] = alac->outputsamples_buffer[0][i];
00513 }
00514 }
00515 break;
00516 case 24:
00517 if (channels == 2) {
00518 interleave_stereo_24(alac->outputsamples_buffer,
00519 (int32_t *)alac->frame.data[0], outputsamples);
00520 } else {
00521 int32_t *outbuffer = (int32_t *)alac->frame.data[0];
00522 for (i = 0; i < outputsamples; i++)
00523 outbuffer[i] = alac->outputsamples_buffer[0][i] << 8;
00524 }
00525 break;
00526 }
00527
00528 if (input_buffer_size * 8 - get_bits_count(&alac->gb) > 8)
00529 av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", input_buffer_size * 8 - get_bits_count(&alac->gb));
00530
00531 *got_frame_ptr = 1;
00532 *(AVFrame *)data = alac->frame;
00533
00534 return input_buffer_size;
00535 }
00536
00537 static av_cold int alac_decode_close(AVCodecContext *avctx)
00538 {
00539 ALACContext *alac = avctx->priv_data;
00540
00541 int ch;
00542 for (ch = 0; ch < alac->numchannels; ch++) {
00543 av_freep(&alac->predicterror_buffer[ch]);
00544 av_freep(&alac->outputsamples_buffer[ch]);
00545 av_freep(&alac->extra_bits_buffer[ch]);
00546 }
00547
00548 return 0;
00549 }
00550
00551 static int allocate_buffers(ALACContext *alac)
00552 {
00553 int ch;
00554 for (ch = 0; ch < alac->numchannels; ch++) {
00555 int buf_size = alac->setinfo_max_samples_per_frame * sizeof(int32_t);
00556
00557 FF_ALLOC_OR_GOTO(alac->avctx, alac->predicterror_buffer[ch],
00558 buf_size, buf_alloc_fail);
00559
00560 FF_ALLOC_OR_GOTO(alac->avctx, alac->outputsamples_buffer[ch],
00561 buf_size, buf_alloc_fail);
00562
00563 FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
00564 buf_size, buf_alloc_fail);
00565 }
00566 return 0;
00567 buf_alloc_fail:
00568 alac_decode_close(alac->avctx);
00569 return AVERROR(ENOMEM);
00570 }
00571
00572 static int alac_set_info(ALACContext *alac)
00573 {
00574 const unsigned char *ptr = alac->avctx->extradata;
00575
00576 ptr += 4;
00577 ptr += 4;
00578 ptr += 4;
00579
00580 if(AV_RB32(ptr) >= UINT_MAX/4){
00581 av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
00582 return -1;
00583 }
00584
00585
00586 alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
00587 ptr++;
00588 alac->setinfo_sample_size = *ptr++;
00589 alac->setinfo_rice_historymult = *ptr++;
00590 alac->setinfo_rice_initialhistory = *ptr++;
00591 alac->setinfo_rice_kmodifier = *ptr++;
00592 alac->numchannels = *ptr++;
00593 bytestream_get_be16(&ptr);
00594 bytestream_get_be32(&ptr);
00595 bytestream_get_be32(&ptr);
00596 bytestream_get_be32(&ptr);
00597
00598 return 0;
00599 }
00600
00601 static av_cold int alac_decode_init(AVCodecContext * avctx)
00602 {
00603 int ret;
00604 ALACContext *alac = avctx->priv_data;
00605 alac->avctx = avctx;
00606
00607
00608 if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
00609 av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
00610 ALAC_EXTRADATA_SIZE);
00611 return -1;
00612 }
00613 if (alac_set_info(alac)) {
00614 av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
00615 return -1;
00616 }
00617
00618 switch (alac->setinfo_sample_size) {
00619 case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00620 break;
00621 case 24: avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00622 break;
00623 default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n",
00624 alac->setinfo_sample_size);
00625 return AVERROR_PATCHWELCOME;
00626 }
00627
00628 if (alac->numchannels < 1) {
00629 av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
00630 alac->numchannels = avctx->channels;
00631 } else {
00632 if (alac->numchannels > MAX_CHANNELS)
00633 alac->numchannels = avctx->channels;
00634 else
00635 avctx->channels = alac->numchannels;
00636 }
00637 if (avctx->channels > MAX_CHANNELS) {
00638 av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n",
00639 avctx->channels);
00640 return AVERROR_PATCHWELCOME;
00641 }
00642
00643 if ((ret = allocate_buffers(alac)) < 0) {
00644 av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
00645 return ret;
00646 }
00647
00648 avcodec_get_frame_defaults(&alac->frame);
00649 avctx->coded_frame = &alac->frame;
00650
00651 return 0;
00652 }
00653
00654 AVCodec ff_alac_decoder = {
00655 .name = "alac",
00656 .type = AVMEDIA_TYPE_AUDIO,
00657 .id = CODEC_ID_ALAC,
00658 .priv_data_size = sizeof(ALACContext),
00659 .init = alac_decode_init,
00660 .close = alac_decode_close,
00661 .decode = alac_decode_frame,
00662 .capabilities = CODEC_CAP_DR1,
00663 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
00664 };