00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "put_bits.h"
00024 #include "dsputil.h"
00025 #include "internal.h"
00026 #include "lpc.h"
00027 #include "mathops.h"
00028
00029 #define DEFAULT_FRAME_SIZE 4096
00030 #define DEFAULT_SAMPLE_SIZE 16
00031 #define MAX_CHANNELS 8
00032 #define ALAC_EXTRADATA_SIZE 36
00033 #define ALAC_FRAME_HEADER_SIZE 55
00034 #define ALAC_FRAME_FOOTER_SIZE 3
00035
00036 #define ALAC_ESCAPE_CODE 0x1FF
00037 #define ALAC_MAX_LPC_ORDER 30
00038 #define DEFAULT_MAX_PRED_ORDER 6
00039 #define DEFAULT_MIN_PRED_ORDER 4
00040 #define ALAC_MAX_LPC_PRECISION 9
00041 #define ALAC_MAX_LPC_SHIFT 9
00042
00043 #define ALAC_CHMODE_LEFT_RIGHT 0
00044 #define ALAC_CHMODE_LEFT_SIDE 1
00045 #define ALAC_CHMODE_RIGHT_SIDE 2
00046 #define ALAC_CHMODE_MID_SIDE 3
00047
00048 typedef struct RiceContext {
00049 int history_mult;
00050 int initial_history;
00051 int k_modifier;
00052 int rice_modifier;
00053 } RiceContext;
00054
00055 typedef struct AlacLPCContext {
00056 int lpc_order;
00057 int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
00058 int lpc_quant;
00059 } AlacLPCContext;
00060
00061 typedef struct AlacEncodeContext {
00062 int frame_size;
00063 int verbatim;
00064 int compression_level;
00065 int min_prediction_order;
00066 int max_prediction_order;
00067 int max_coded_frame_size;
00068 int write_sample_size;
00069 int32_t sample_buf[MAX_CHANNELS][DEFAULT_FRAME_SIZE];
00070 int32_t predictor_buf[DEFAULT_FRAME_SIZE];
00071 int interlacing_shift;
00072 int interlacing_leftweight;
00073 PutBitContext pbctx;
00074 RiceContext rc;
00075 AlacLPCContext lpc[MAX_CHANNELS];
00076 LPCContext lpc_ctx;
00077 AVCodecContext *avctx;
00078 } AlacEncodeContext;
00079
00080
00081 static void init_sample_buffers(AlacEncodeContext *s, int16_t **input_samples)
00082 {
00083 int ch, i;
00084
00085 for (ch = 0; ch < s->avctx->channels; ch++) {
00086 int32_t *bptr = s->sample_buf[ch];
00087 const int16_t *sptr = input_samples[ch];
00088 for (i = 0; i < s->frame_size; i++)
00089 bptr[i] = sptr[i];
00090 }
00091 }
00092
00093 static void encode_scalar(AlacEncodeContext *s, int x,
00094 int k, int write_sample_size)
00095 {
00096 int divisor, q, r;
00097
00098 k = FFMIN(k, s->rc.k_modifier);
00099 divisor = (1<<k) - 1;
00100 q = x / divisor;
00101 r = x % divisor;
00102
00103 if (q > 8) {
00104
00105 put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
00106 put_bits(&s->pbctx, write_sample_size, x);
00107 } else {
00108 if (q)
00109 put_bits(&s->pbctx, q, (1<<q) - 1);
00110 put_bits(&s->pbctx, 1, 0);
00111
00112 if (k != 1) {
00113 if (r > 0)
00114 put_bits(&s->pbctx, k, r+1);
00115 else
00116 put_bits(&s->pbctx, k-1, 0);
00117 }
00118 }
00119 }
00120
00121 static void write_frame_header(AlacEncodeContext *s)
00122 {
00123 int encode_fs = 0;
00124
00125 if (s->frame_size < DEFAULT_FRAME_SIZE)
00126 encode_fs = 1;
00127
00128 put_bits(&s->pbctx, 3, s->avctx->channels-1);
00129 put_bits(&s->pbctx, 16, 0);
00130 put_bits(&s->pbctx, 1, encode_fs);
00131 put_bits(&s->pbctx, 2, 0);
00132 put_bits(&s->pbctx, 1, s->verbatim);
00133 if (encode_fs)
00134 put_bits32(&s->pbctx, s->frame_size);
00135 }
00136
00137 static void calc_predictor_params(AlacEncodeContext *s, int ch)
00138 {
00139 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
00140 int shift[MAX_LPC_ORDER];
00141 int opt_order;
00142
00143 if (s->compression_level == 1) {
00144 s->lpc[ch].lpc_order = 6;
00145 s->lpc[ch].lpc_quant = 6;
00146 s->lpc[ch].lpc_coeff[0] = 160;
00147 s->lpc[ch].lpc_coeff[1] = -190;
00148 s->lpc[ch].lpc_coeff[2] = 170;
00149 s->lpc[ch].lpc_coeff[3] = -130;
00150 s->lpc[ch].lpc_coeff[4] = 80;
00151 s->lpc[ch].lpc_coeff[5] = -25;
00152 } else {
00153 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
00154 s->frame_size,
00155 s->min_prediction_order,
00156 s->max_prediction_order,
00157 ALAC_MAX_LPC_PRECISION, coefs, shift,
00158 FF_LPC_TYPE_LEVINSON, 0,
00159 ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1);
00160
00161 s->lpc[ch].lpc_order = opt_order;
00162 s->lpc[ch].lpc_quant = shift[opt_order-1];
00163 memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
00164 }
00165 }
00166
00167 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
00168 {
00169 int i, best;
00170 int32_t lt, rt;
00171 uint64_t sum[4];
00172 uint64_t score[4];
00173
00174
00175 sum[0] = sum[1] = sum[2] = sum[3] = 0;
00176 for (i = 2; i < n; i++) {
00177 lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
00178 rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
00179 sum[2] += FFABS((lt + rt) >> 1);
00180 sum[3] += FFABS(lt - rt);
00181 sum[0] += FFABS(lt);
00182 sum[1] += FFABS(rt);
00183 }
00184
00185
00186 score[0] = sum[0] + sum[1];
00187 score[1] = sum[0] + sum[3];
00188 score[2] = sum[1] + sum[3];
00189 score[3] = sum[2] + sum[3];
00190
00191
00192 best = 0;
00193 for (i = 1; i < 4; i++) {
00194 if (score[i] < score[best])
00195 best = i;
00196 }
00197 return best;
00198 }
00199
00200 static void alac_stereo_decorrelation(AlacEncodeContext *s)
00201 {
00202 int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
00203 int i, mode, n = s->frame_size;
00204 int32_t tmp;
00205
00206 mode = estimate_stereo_mode(left, right, n);
00207
00208 switch (mode) {
00209 case ALAC_CHMODE_LEFT_RIGHT:
00210 s->interlacing_leftweight = 0;
00211 s->interlacing_shift = 0;
00212 break;
00213 case ALAC_CHMODE_LEFT_SIDE:
00214 for (i = 0; i < n; i++)
00215 right[i] = left[i] - right[i];
00216 s->interlacing_leftweight = 1;
00217 s->interlacing_shift = 0;
00218 break;
00219 case ALAC_CHMODE_RIGHT_SIDE:
00220 for (i = 0; i < n; i++) {
00221 tmp = right[i];
00222 right[i] = left[i] - right[i];
00223 left[i] = tmp + (right[i] >> 31);
00224 }
00225 s->interlacing_leftweight = 1;
00226 s->interlacing_shift = 31;
00227 break;
00228 default:
00229 for (i = 0; i < n; i++) {
00230 tmp = left[i];
00231 left[i] = (tmp + right[i]) >> 1;
00232 right[i] = tmp - right[i];
00233 }
00234 s->interlacing_leftweight = 1;
00235 s->interlacing_shift = 1;
00236 break;
00237 }
00238 }
00239
00240 static void alac_linear_predictor(AlacEncodeContext *s, int ch)
00241 {
00242 int i;
00243 AlacLPCContext lpc = s->lpc[ch];
00244
00245 if (lpc.lpc_order == 31) {
00246 s->predictor_buf[0] = s->sample_buf[ch][0];
00247
00248 for (i = 1; i < s->frame_size; i++) {
00249 s->predictor_buf[i] = s->sample_buf[ch][i ] -
00250 s->sample_buf[ch][i - 1];
00251 }
00252
00253 return;
00254 }
00255
00256
00257
00258 if (lpc.lpc_order > 0) {
00259 int32_t *samples = s->sample_buf[ch];
00260 int32_t *residual = s->predictor_buf;
00261
00262
00263 residual[0] = samples[0];
00264 for (i = 1; i <= lpc.lpc_order; i++)
00265 residual[i] = samples[i] - samples[i-1];
00266
00267
00268 for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
00269 int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
00270
00271 for (j = 0; j < lpc.lpc_order; j++) {
00272 sum += (samples[lpc.lpc_order-j] - samples[0]) *
00273 lpc.lpc_coeff[j];
00274 }
00275
00276 sum >>= lpc.lpc_quant;
00277 sum += samples[0];
00278 residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
00279 s->write_sample_size);
00280 res_val = residual[i];
00281
00282 if (res_val) {
00283 int index = lpc.lpc_order - 1;
00284 int neg = (res_val < 0);
00285
00286 while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
00287 int val = samples[0] - samples[lpc.lpc_order - index];
00288 int sign = (val ? FFSIGN(val) : 0);
00289
00290 if (neg)
00291 sign *= -1;
00292
00293 lpc.lpc_coeff[index] -= sign;
00294 val *= sign;
00295 res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
00296 index--;
00297 }
00298 }
00299 samples++;
00300 }
00301 }
00302 }
00303
00304 static void alac_entropy_coder(AlacEncodeContext *s)
00305 {
00306 unsigned int history = s->rc.initial_history;
00307 int sign_modifier = 0, i, k;
00308 int32_t *samples = s->predictor_buf;
00309
00310 for (i = 0; i < s->frame_size;) {
00311 int x;
00312
00313 k = av_log2((history >> 9) + 3);
00314
00315 x = -2 * (*samples) -1;
00316 x ^= x >> 31;
00317
00318 samples++;
00319 i++;
00320
00321 encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
00322
00323 history += x * s->rc.history_mult -
00324 ((history * s->rc.history_mult) >> 9);
00325
00326 sign_modifier = 0;
00327 if (x > 0xFFFF)
00328 history = 0xFFFF;
00329
00330 if (history < 128 && i < s->frame_size) {
00331 unsigned int block_size = 0;
00332
00333 k = 7 - av_log2(history) + ((history + 16) >> 6);
00334
00335 while (*samples == 0 && i < s->frame_size) {
00336 samples++;
00337 i++;
00338 block_size++;
00339 }
00340 encode_scalar(s, block_size, k, 16);
00341 sign_modifier = (block_size <= 0xFFFF);
00342 history = 0;
00343 }
00344
00345 }
00346 }
00347
00348 static int write_frame(AlacEncodeContext *s, AVPacket *avpkt, int16_t **samples)
00349 {
00350 int i, j;
00351 int prediction_type = 0;
00352 PutBitContext *pb = &s->pbctx;
00353
00354 init_put_bits(pb, avpkt->data, avpkt->size);
00355
00356 if (s->verbatim) {
00357 write_frame_header(s);
00358
00359 for (i = 0; i < s->frame_size; i++)
00360 for (j = 0; j < s->avctx->channels; j++)
00361 put_sbits(pb, 16, samples[j][i]);
00362 } else {
00363 init_sample_buffers(s, samples);
00364 write_frame_header(s);
00365
00366 if (s->avctx->channels == 2)
00367 alac_stereo_decorrelation(s);
00368 put_bits(pb, 8, s->interlacing_shift);
00369 put_bits(pb, 8, s->interlacing_leftweight);
00370
00371 for (i = 0; i < s->avctx->channels; i++) {
00372 calc_predictor_params(s, i);
00373
00374 put_bits(pb, 4, prediction_type);
00375 put_bits(pb, 4, s->lpc[i].lpc_quant);
00376
00377 put_bits(pb, 3, s->rc.rice_modifier);
00378 put_bits(pb, 5, s->lpc[i].lpc_order);
00379
00380 for (j = 0; j < s->lpc[i].lpc_order; j++)
00381 put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
00382 }
00383
00384
00385
00386 for (i = 0; i < s->avctx->channels; i++) {
00387 alac_linear_predictor(s, i);
00388
00389
00390 if (prediction_type == 15) {
00391
00392 for (j = s->frame_size - 1; j > 0; j--)
00393 s->predictor_buf[j] -= s->predictor_buf[j - 1];
00394 }
00395
00396 alac_entropy_coder(s);
00397 }
00398 }
00399 put_bits(pb, 3, 7);
00400 flush_put_bits(pb);
00401 return put_bits_count(pb) >> 3;
00402 }
00403
00404 static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
00405 {
00406 int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
00407 return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
00408 }
00409
00410 static av_cold int alac_encode_close(AVCodecContext *avctx)
00411 {
00412 AlacEncodeContext *s = avctx->priv_data;
00413 ff_lpc_end(&s->lpc_ctx);
00414 av_freep(&avctx->extradata);
00415 avctx->extradata_size = 0;
00416 av_freep(&avctx->coded_frame);
00417 return 0;
00418 }
00419
00420 static av_cold int alac_encode_init(AVCodecContext *avctx)
00421 {
00422 AlacEncodeContext *s = avctx->priv_data;
00423 int ret;
00424 uint8_t *alac_extradata;
00425
00426 avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
00427
00428
00429
00430
00431 if (avctx->channels > 2) {
00432 av_log(avctx, AV_LOG_ERROR, "only mono or stereo input is currently supported\n");
00433 return AVERROR_PATCHWELCOME;
00434 }
00435
00436
00437 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
00438 s->compression_level = 2;
00439 else
00440 s->compression_level = av_clip(avctx->compression_level, 0, 2);
00441
00442
00443 s->rc.history_mult = 40;
00444 s->rc.initial_history = 10;
00445 s->rc.k_modifier = 14;
00446 s->rc.rice_modifier = 4;
00447
00448 s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
00449 avctx->channels,
00450 DEFAULT_SAMPLE_SIZE);
00451
00452
00453 s->write_sample_size = DEFAULT_SAMPLE_SIZE + avctx->channels - 1;
00454
00455 avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
00456 if (!avctx->extradata) {
00457 ret = AVERROR(ENOMEM);
00458 goto error;
00459 }
00460 avctx->extradata_size = ALAC_EXTRADATA_SIZE;
00461
00462 alac_extradata = avctx->extradata;
00463 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
00464 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
00465 AV_WB32(alac_extradata+12, avctx->frame_size);
00466 AV_WB8 (alac_extradata+17, DEFAULT_SAMPLE_SIZE);
00467 AV_WB8 (alac_extradata+21, avctx->channels);
00468 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
00469 AV_WB32(alac_extradata+28,
00470 avctx->sample_rate * avctx->channels * DEFAULT_SAMPLE_SIZE);
00471 AV_WB32(alac_extradata+32, avctx->sample_rate);
00472
00473
00474 if (s->compression_level > 0) {
00475 AV_WB8(alac_extradata+18, s->rc.history_mult);
00476 AV_WB8(alac_extradata+19, s->rc.initial_history);
00477 AV_WB8(alac_extradata+20, s->rc.k_modifier);
00478 }
00479
00480 s->min_prediction_order = DEFAULT_MIN_PRED_ORDER;
00481 if (avctx->min_prediction_order >= 0) {
00482 if (avctx->min_prediction_order < MIN_LPC_ORDER ||
00483 avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
00484 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
00485 avctx->min_prediction_order);
00486 ret = AVERROR(EINVAL);
00487 goto error;
00488 }
00489
00490 s->min_prediction_order = avctx->min_prediction_order;
00491 }
00492
00493 s->max_prediction_order = DEFAULT_MAX_PRED_ORDER;
00494 if (avctx->max_prediction_order >= 0) {
00495 if (avctx->max_prediction_order < MIN_LPC_ORDER ||
00496 avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
00497 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
00498 avctx->max_prediction_order);
00499 ret = AVERROR(EINVAL);
00500 goto error;
00501 }
00502
00503 s->max_prediction_order = avctx->max_prediction_order;
00504 }
00505
00506 if (s->max_prediction_order < s->min_prediction_order) {
00507 av_log(avctx, AV_LOG_ERROR,
00508 "invalid prediction orders: min=%d max=%d\n",
00509 s->min_prediction_order, s->max_prediction_order);
00510 ret = AVERROR(EINVAL);
00511 goto error;
00512 }
00513
00514 avctx->coded_frame = avcodec_alloc_frame();
00515 if (!avctx->coded_frame) {
00516 ret = AVERROR(ENOMEM);
00517 goto error;
00518 }
00519
00520 s->avctx = avctx;
00521
00522 if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
00523 s->max_prediction_order,
00524 FF_LPC_TYPE_LEVINSON)) < 0) {
00525 goto error;
00526 }
00527
00528 return 0;
00529 error:
00530 alac_encode_close(avctx);
00531 return ret;
00532 }
00533
00534 static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00535 const AVFrame *frame, int *got_packet_ptr)
00536 {
00537 AlacEncodeContext *s = avctx->priv_data;
00538 int out_bytes, max_frame_size, ret;
00539 int16_t **samples = (int16_t **)frame->extended_data;
00540
00541 s->frame_size = frame->nb_samples;
00542
00543 if (frame->nb_samples < DEFAULT_FRAME_SIZE)
00544 max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
00545 DEFAULT_SAMPLE_SIZE);
00546 else
00547 max_frame_size = s->max_coded_frame_size;
00548
00549 if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * max_frame_size)))
00550 return ret;
00551
00552
00553 s->verbatim = !s->compression_level;
00554
00555 out_bytes = write_frame(s, avpkt, samples);
00556
00557 if (out_bytes > max_frame_size) {
00558
00559 s->verbatim = 1;
00560 out_bytes = write_frame(s, avpkt, samples);
00561 }
00562
00563 avpkt->size = out_bytes;
00564 *got_packet_ptr = 1;
00565 return 0;
00566 }
00567
00568 AVCodec ff_alac_encoder = {
00569 .name = "alac",
00570 .type = AVMEDIA_TYPE_AUDIO,
00571 .id = AV_CODEC_ID_ALAC,
00572 .priv_data_size = sizeof(AlacEncodeContext),
00573 .init = alac_encode_init,
00574 .encode2 = alac_encode_frame,
00575 .close = alac_encode_close,
00576 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
00577 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
00578 AV_SAMPLE_FMT_NONE },
00579 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
00580 };