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