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