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