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