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