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