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 LPCContext {
00055 int lpc_order;
00056 int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
00057 int lpc_quant;
00058 } LPCContext;
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 LPCContext lpc[MAX_CHANNELS];
00073 DSPContext dspctx;
00074 AVCodecContext *avctx;
00075 } AlacEncodeContext;
00076
00077
00078 static void init_sample_buffers(AlacEncodeContext *s, int16_t *input_samples)
00079 {
00080 int ch, i;
00081
00082 for(ch=0;ch<s->avctx->channels;ch++) {
00083 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->dspctx, 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, 1,
00149 ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1);
00150
00151 s->lpc[ch].lpc_order = opt_order;
00152 s->lpc[ch].lpc_quant = shift[opt_order-1];
00153 memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
00154 }
00155 }
00156
00157 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
00158 {
00159 int i, best;
00160 int32_t lt, rt;
00161 uint64_t sum[4];
00162 uint64_t score[4];
00163
00164
00165 sum[0] = sum[1] = sum[2] = sum[3] = 0;
00166 for(i=2; i<n; i++) {
00167 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
00168 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
00169 sum[2] += FFABS((lt + rt) >> 1);
00170 sum[3] += FFABS(lt - rt);
00171 sum[0] += FFABS(lt);
00172 sum[1] += FFABS(rt);
00173 }
00174
00175
00176 score[0] = sum[0] + sum[1];
00177 score[1] = sum[0] + sum[3];
00178 score[2] = sum[1] + sum[3];
00179 score[3] = sum[2] + sum[3];
00180
00181
00182 best = 0;
00183 for(i=1; i<4; i++) {
00184 if(score[i] < score[best]) {
00185 best = i;
00186 }
00187 }
00188 return best;
00189 }
00190
00191 static void alac_stereo_decorrelation(AlacEncodeContext *s)
00192 {
00193 int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
00194 int i, mode, n = s->avctx->frame_size;
00195 int32_t tmp;
00196
00197 mode = estimate_stereo_mode(left, right, n);
00198
00199 switch(mode)
00200 {
00201 case ALAC_CHMODE_LEFT_RIGHT:
00202 s->interlacing_leftweight = 0;
00203 s->interlacing_shift = 0;
00204 break;
00205
00206 case ALAC_CHMODE_LEFT_SIDE:
00207 for(i=0; i<n; i++) {
00208 right[i] = left[i] - right[i];
00209 }
00210 s->interlacing_leftweight = 1;
00211 s->interlacing_shift = 0;
00212 break;
00213
00214 case ALAC_CHMODE_RIGHT_SIDE:
00215 for(i=0; i<n; i++) {
00216 tmp = right[i];
00217 right[i] = left[i] - right[i];
00218 left[i] = tmp + (right[i] >> 31);
00219 }
00220 s->interlacing_leftweight = 1;
00221 s->interlacing_shift = 31;
00222 break;
00223
00224 default:
00225 for(i=0; i<n; i++) {
00226 tmp = left[i];
00227 left[i] = (tmp + right[i]) >> 1;
00228 right[i] = tmp - right[i];
00229 }
00230 s->interlacing_leftweight = 1;
00231 s->interlacing_shift = 1;
00232 break;
00233 }
00234 }
00235
00236 static void alac_linear_predictor(AlacEncodeContext *s, int ch)
00237 {
00238 int i;
00239 LPCContext lpc = s->lpc[ch];
00240
00241 if(lpc.lpc_order == 31) {
00242 s->predictor_buf[0] = s->sample_buf[ch][0];
00243
00244 for(i=1; i<s->avctx->frame_size; i++)
00245 s->predictor_buf[i] = s->sample_buf[ch][i] - s->sample_buf[ch][i-1];
00246
00247 return;
00248 }
00249
00250
00251
00252 if(lpc.lpc_order > 0) {
00253 int32_t *samples = s->sample_buf[ch];
00254 int32_t *residual = s->predictor_buf;
00255
00256
00257 residual[0] = samples[0];
00258 for(i=1;i<=lpc.lpc_order;i++)
00259 residual[i] = samples[i] - samples[i-1];
00260
00261
00262 for(i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
00263 int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
00264
00265 for (j = 0; j < lpc.lpc_order; j++) {
00266 sum += (samples[lpc.lpc_order-j] - samples[0]) *
00267 lpc.lpc_coeff[j];
00268 }
00269
00270 sum >>= lpc.lpc_quant;
00271 sum += samples[0];
00272 residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
00273 s->write_sample_size);
00274 res_val = residual[i];
00275
00276 if(res_val) {
00277 int index = lpc.lpc_order - 1;
00278 int neg = (res_val < 0);
00279
00280 while(index >= 0 && (neg ? (res_val < 0):(res_val > 0))) {
00281 int val = samples[0] - samples[lpc.lpc_order - index];
00282 int sign = (val ? FFSIGN(val) : 0);
00283
00284 if(neg)
00285 sign*=-1;
00286
00287 lpc.lpc_coeff[index] -= sign;
00288 val *= sign;
00289 res_val -= ((val >> lpc.lpc_quant) *
00290 (lpc.lpc_order - index));
00291 index--;
00292 }
00293 }
00294 samples++;
00295 }
00296 }
00297 }
00298
00299 static void alac_entropy_coder(AlacEncodeContext *s)
00300 {
00301 unsigned int history = s->rc.initial_history;
00302 int sign_modifier = 0, i, k;
00303 int32_t *samples = s->predictor_buf;
00304
00305 for(i=0;i < s->avctx->frame_size;) {
00306 int x;
00307
00308 k = av_log2((history >> 9) + 3);
00309
00310 x = -2*(*samples)-1;
00311 x ^= (x>>31);
00312
00313 samples++;
00314 i++;
00315
00316 encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
00317
00318 history += x * s->rc.history_mult
00319 - ((history * s->rc.history_mult) >> 9);
00320
00321 sign_modifier = 0;
00322 if(x > 0xFFFF)
00323 history = 0xFFFF;
00324
00325 if((history < 128) && (i < s->avctx->frame_size)) {
00326 unsigned int block_size = 0;
00327
00328 k = 7 - av_log2(history) + ((history + 16) >> 6);
00329
00330 while((*samples == 0) && (i < s->avctx->frame_size)) {
00331 samples++;
00332 i++;
00333 block_size++;
00334 }
00335 encode_scalar(s, block_size, k, 16);
00336
00337 sign_modifier = (block_size <= 0xFFFF);
00338
00339 history = 0;
00340 }
00341
00342 }
00343 }
00344
00345 static void write_compressed_frame(AlacEncodeContext *s)
00346 {
00347 int i, j;
00348
00349 if(s->avctx->channels == 2)
00350 alac_stereo_decorrelation(s);
00351 put_bits(&s->pbctx, 8, s->interlacing_shift);
00352 put_bits(&s->pbctx, 8, s->interlacing_leftweight);
00353
00354 for(i=0;i<s->avctx->channels;i++) {
00355
00356 calc_predictor_params(s, i);
00357
00358 put_bits(&s->pbctx, 4, 0);
00359 put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant);
00360
00361 put_bits(&s->pbctx, 3, s->rc.rice_modifier);
00362 put_bits(&s->pbctx, 5, s->lpc[i].lpc_order);
00363
00364 for(j=0;j<s->lpc[i].lpc_order;j++) {
00365 put_sbits(&s->pbctx, 16, s->lpc[i].lpc_coeff[j]);
00366 }
00367 }
00368
00369
00370
00371 for(i=0;i<s->avctx->channels;i++) {
00372 alac_linear_predictor(s, i);
00373 alac_entropy_coder(s);
00374 }
00375 }
00376
00377 static av_cold int alac_encode_init(AVCodecContext *avctx)
00378 {
00379 AlacEncodeContext *s = avctx->priv_data;
00380 uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1);
00381
00382 avctx->frame_size = DEFAULT_FRAME_SIZE;
00383 avctx->bits_per_coded_sample = DEFAULT_SAMPLE_SIZE;
00384
00385 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
00386 av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
00387 return -1;
00388 }
00389
00390
00391 if(avctx->compression_level == FF_COMPRESSION_DEFAULT)
00392 s->compression_level = 2;
00393 else
00394 s->compression_level = av_clip(avctx->compression_level, 0, 2);
00395
00396
00397 s->rc.history_mult = 40;
00398 s->rc.initial_history = 10;
00399 s->rc.k_modifier = 14;
00400 s->rc.rice_modifier = 4;
00401
00402 s->max_coded_frame_size = 8 + (avctx->frame_size*avctx->channels*avctx->bits_per_coded_sample>>3);
00403
00404 s->write_sample_size = avctx->bits_per_coded_sample + avctx->channels - 1;
00405
00406 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
00407 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
00408 AV_WB32(alac_extradata+12, avctx->frame_size);
00409 AV_WB8 (alac_extradata+17, avctx->bits_per_coded_sample);
00410 AV_WB8 (alac_extradata+21, avctx->channels);
00411 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
00412 AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_coded_sample);
00413 AV_WB32(alac_extradata+32, avctx->sample_rate);
00414
00415
00416 if(s->compression_level > 0) {
00417 AV_WB8(alac_extradata+18, s->rc.history_mult);
00418 AV_WB8(alac_extradata+19, s->rc.initial_history);
00419 AV_WB8(alac_extradata+20, s->rc.k_modifier);
00420 }
00421
00422 s->min_prediction_order = DEFAULT_MIN_PRED_ORDER;
00423 if(avctx->min_prediction_order >= 0) {
00424 if(avctx->min_prediction_order < MIN_LPC_ORDER ||
00425 avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
00426 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order);
00427 return -1;
00428 }
00429
00430 s->min_prediction_order = avctx->min_prediction_order;
00431 }
00432
00433 s->max_prediction_order = DEFAULT_MAX_PRED_ORDER;
00434 if(avctx->max_prediction_order >= 0) {
00435 if(avctx->max_prediction_order < MIN_LPC_ORDER ||
00436 avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
00437 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order);
00438 return -1;
00439 }
00440
00441 s->max_prediction_order = avctx->max_prediction_order;
00442 }
00443
00444 if(s->max_prediction_order < s->min_prediction_order) {
00445 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
00446 s->min_prediction_order, s->max_prediction_order);
00447 return -1;
00448 }
00449
00450 avctx->extradata = alac_extradata;
00451 avctx->extradata_size = ALAC_EXTRADATA_SIZE;
00452
00453 avctx->coded_frame = avcodec_alloc_frame();
00454 avctx->coded_frame->key_frame = 1;
00455
00456 s->avctx = avctx;
00457 dsputil_init(&s->dspctx, avctx);
00458
00459 return 0;
00460 }
00461
00462 static int alac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
00463 int buf_size, void *data)
00464 {
00465 AlacEncodeContext *s = avctx->priv_data;
00466 PutBitContext *pb = &s->pbctx;
00467 int i, out_bytes, verbatim_flag = 0;
00468
00469 if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
00470 av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n");
00471 return -1;
00472 }
00473
00474 if(buf_size < 2*s->max_coded_frame_size) {
00475 av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n");
00476 return -1;
00477 }
00478
00479 verbatim:
00480 init_put_bits(pb, frame, buf_size);
00481
00482 if((s->compression_level == 0) || verbatim_flag) {
00483
00484 int16_t *samples = data;
00485 write_frame_header(s, 1);
00486 for(i=0; i<avctx->frame_size*avctx->channels; i++) {
00487 put_sbits(pb, 16, *samples++);
00488 }
00489 } else {
00490 init_sample_buffers(s, data);
00491 write_frame_header(s, 0);
00492 write_compressed_frame(s);
00493 }
00494
00495 put_bits(pb, 3, 7);
00496 flush_put_bits(pb);
00497 out_bytes = put_bits_count(pb) >> 3;
00498
00499 if(out_bytes > s->max_coded_frame_size) {
00500
00501 if(verbatim_flag || (s->compression_level == 0)) {
00502
00503 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
00504 return -1;
00505 }
00506 verbatim_flag = 1;
00507 goto verbatim;
00508 }
00509
00510 return out_bytes;
00511 }
00512
00513 static av_cold int alac_encode_close(AVCodecContext *avctx)
00514 {
00515 av_freep(&avctx->extradata);
00516 avctx->extradata_size = 0;
00517 av_freep(&avctx->coded_frame);
00518 return 0;
00519 }
00520
00521 AVCodec alac_encoder = {
00522 "alac",
00523 AVMEDIA_TYPE_AUDIO,
00524 CODEC_ID_ALAC,
00525 sizeof(AlacEncodeContext),
00526 alac_encode_init,
00527 alac_encode_frame,
00528 alac_encode_close,
00529 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
00530 .sample_fmts = (const enum SampleFormat[]){ SAMPLE_FMT_S16, SAMPLE_FMT_NONE},
00531 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
00532 };