75
75
} AlacEncodeContext;
78
static void init_sample_buffers(AlacEncodeContext *s, const int16_t *input_samples)
78
static void init_sample_buffers(AlacEncodeContext *s,
79
const int16_t *input_samples)
82
for(ch=0;ch<s->avctx->channels;ch++) {
83
for (ch = 0; ch < s->avctx->channels; ch++) {
83
84
const int16_t *sptr = input_samples + ch;
84
for(i=0;i<s->avctx->frame_size;i++) {
85
for (i = 0; i < s->avctx->frame_size; i++) {
85
86
s->sample_buf[ch][i] = *sptr;
86
87
sptr += s->avctx->channels;
91
static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
92
static void encode_scalar(AlacEncodeContext *s, int x,
93
int k, int write_sample_size)
101
103
// write escape code and sample value directly
102
104
put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
103
105
put_bits(&s->pbctx, write_sample_size, x);
106
108
put_bits(&s->pbctx, q, (1<<q) - 1);
107
109
put_bits(&s->pbctx, 1, 0);
111
113
put_bits(&s->pbctx, k, r+1);
113
115
put_bits(&s->pbctx, k-1, 0);
165
167
/* calculate sum of 2nd order residual for each channel */
166
168
sum[0] = sum[1] = sum[2] = sum[3] = 0;
169
for (i = 2; i < n; i++) {
168
170
lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
169
171
rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
170
172
sum[2] += FFABS((lt + rt) >> 1);
240
242
AlacLPCContext lpc = s->lpc[ch];
242
if(lpc.lpc_order == 31) {
244
if (lpc.lpc_order == 31) {
243
245
s->predictor_buf[0] = s->sample_buf[ch][0];
245
for(i=1; i<s->avctx->frame_size; i++)
247
for (i = 1; i < s->avctx->frame_size; i++)
246
248
s->predictor_buf[i] = s->sample_buf[ch][i] - s->sample_buf[ch][i-1];
251
253
// generalised linear predictor
253
if(lpc.lpc_order > 0) {
255
if (lpc.lpc_order > 0) {
254
256
int32_t *samples = s->sample_buf[ch];
255
257
int32_t *residual = s->predictor_buf;
257
259
// generate warm-up samples
258
260
residual[0] = samples[0];
259
for(i=1;i<=lpc.lpc_order;i++)
261
for (i = 1; i <= lpc.lpc_order; i++)
260
262
residual[i] = samples[i] - samples[i-1];
262
264
// perform lpc on remaining samples
263
for(i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
265
for (i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
264
266
int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
266
268
for (j = 0; j < lpc.lpc_order; j++) {
303
305
int sign_modifier = 0, i, k;
304
306
int32_t *samples = s->predictor_buf;
306
for(i=0;i < s->avctx->frame_size;) {
308
for (i = 0; i < s->avctx->frame_size;) {
309
311
k = av_log2((history >> 9) + 3);
320
322
- ((history * s->rc.history_mult) >> 9);
322
324
sign_modifier = 0;
324
326
history = 0xFFFF;
326
if((history < 128) && (i < s->avctx->frame_size)) {
328
if (history < 128 && i < s->avctx->frame_size) {
327
329
unsigned int block_size = 0;
329
331
k = 7 - av_log2(history) + ((history + 16) >> 6);
331
while((*samples == 0) && (i < s->avctx->frame_size)) {
333
while (*samples == 0 && i < s->avctx->frame_size) {
346
348
static void write_compressed_frame(AlacEncodeContext *s)
351
int prediction_type = 0;
350
if(s->avctx->channels == 2)
353
if (s->avctx->channels == 2)
351
354
alac_stereo_decorrelation(s);
352
355
put_bits(&s->pbctx, 8, s->interlacing_shift);
353
356
put_bits(&s->pbctx, 8, s->interlacing_leftweight);
355
for(i=0;i<s->avctx->channels;i++) {
358
for (i = 0; i < s->avctx->channels; i++) {
357
360
calc_predictor_params(s, i);
359
put_bits(&s->pbctx, 4, 0); // prediction type : currently only type 0 has been RE'd
362
put_bits(&s->pbctx, 4, prediction_type);
360
363
put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant);
362
365
put_bits(&s->pbctx, 3, s->rc.rice_modifier);
363
366
put_bits(&s->pbctx, 5, s->lpc[i].lpc_order);
364
367
// predictor coeff. table
365
for(j=0;j<s->lpc[i].lpc_order;j++) {
368
for (j = 0; j < s->lpc[i].lpc_order; j++) {
366
369
put_sbits(&s->pbctx, 16, s->lpc[i].lpc_coeff[j]);
370
373
// apply lpc and entropy coding to audio samples
372
for(i=0;i<s->avctx->channels;i++) {
375
for (i = 0; i < s->avctx->channels; i++) {
373
376
alac_linear_predictor(s, i);
378
// TODO: determine when this will actually help. for now it's not used.
379
if (prediction_type == 15) {
380
// 2nd pass 1st order filter
381
for (j = s->avctx->frame_size - 1; j > 0; j--)
382
s->predictor_buf[j] -= s->predictor_buf[j - 1];
374
385
alac_entropy_coder(s);
384
395
avctx->frame_size = DEFAULT_FRAME_SIZE;
385
396
avctx->bits_per_coded_sample = DEFAULT_SAMPLE_SIZE;
387
if(avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
398
if (avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
388
399
av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
403
/* TODO: Correctly implement multi-channel ALAC.
404
It is similar to multi-channel AAC, in that it has a series of
405
single-channel (SCE), channel-pair (CPE), and LFE elements. */
406
if (avctx->channels > 2) {
407
av_log(avctx, AV_LOG_ERROR, "only mono or stereo input is currently supported\n");
408
return AVERROR_PATCHWELCOME;
392
411
// Set default compression level
393
if(avctx->compression_level == FF_COMPRESSION_DEFAULT)
412
if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
394
413
s->compression_level = 2;
396
415
s->compression_level = av_clip(avctx->compression_level, 0, 2);
411
430
AV_WB8 (alac_extradata+17, avctx->bits_per_coded_sample);
412
431
AV_WB8 (alac_extradata+21, avctx->channels);
413
432
AV_WB32(alac_extradata+24, s->max_coded_frame_size);
414
AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_coded_sample); // average bitrate
433
AV_WB32(alac_extradata+28,
434
avctx->sample_rate * avctx->channels * avctx->bits_per_coded_sample); // average bitrate
415
435
AV_WB32(alac_extradata+32, avctx->sample_rate);
417
437
// Set relevant extradata fields
418
if(s->compression_level > 0) {
438
if (s->compression_level > 0) {
419
439
AV_WB8(alac_extradata+18, s->rc.history_mult);
420
440
AV_WB8(alac_extradata+19, s->rc.initial_history);
421
441
AV_WB8(alac_extradata+20, s->rc.k_modifier);
424
444
s->min_prediction_order = DEFAULT_MIN_PRED_ORDER;
425
if(avctx->min_prediction_order >= 0) {
426
if(avctx->min_prediction_order < MIN_LPC_ORDER ||
445
if (avctx->min_prediction_order >= 0) {
446
if (avctx->min_prediction_order < MIN_LPC_ORDER ||
427
447
avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
428
av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order);
448
av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
449
avctx->min_prediction_order);
435
456
s->max_prediction_order = DEFAULT_MAX_PRED_ORDER;
436
if(avctx->max_prediction_order >= 0) {
437
if(avctx->max_prediction_order < MIN_LPC_ORDER ||
438
avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
439
av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order);
457
if (avctx->max_prediction_order >= 0) {
458
if (avctx->max_prediction_order < MIN_LPC_ORDER ||
459
avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
460
av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
461
avctx->max_prediction_order);
443
465
s->max_prediction_order = avctx->max_prediction_order;
446
if(s->max_prediction_order < s->min_prediction_order) {
447
av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
468
if (s->max_prediction_order < s->min_prediction_order) {
469
av_log(avctx, AV_LOG_ERROR,
470
"invalid prediction orders: min=%d max=%d\n",
448
471
s->min_prediction_order, s->max_prediction_order);
469
492
PutBitContext *pb = &s->pbctx;
470
493
int i, out_bytes, verbatim_flag = 0;
472
if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
495
if (avctx->frame_size > DEFAULT_FRAME_SIZE) {
473
496
av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n");
477
if(buf_size < 2*s->max_coded_frame_size) {
500
if (buf_size < 2 * s->max_coded_frame_size) {
478
501
av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n");
483
506
init_put_bits(pb, frame, buf_size);
485
if((s->compression_level == 0) || verbatim_flag) {
508
if (s->compression_level == 0 || verbatim_flag) {
487
510
const int16_t *samples = data;
488
511
write_frame_header(s, 1);
489
for(i=0; i<avctx->frame_size*avctx->channels; i++) {
512
for (i = 0; i < avctx->frame_size * avctx->channels; i++) {
490
513
put_sbits(pb, 16, *samples++);
499
522
flush_put_bits(pb);
500
523
out_bytes = put_bits_count(pb) >> 3;
502
if(out_bytes > s->max_coded_frame_size) {
525
if (out_bytes > s->max_coded_frame_size) {
503
526
/* frame too large. use verbatim mode */
504
if(verbatim_flag || (s->compression_level == 0)) {
527
if (verbatim_flag || s->compression_level == 0) {
505
528
/* still too large. must be an error. */
506
529
av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
526
549
AVCodec ff_alac_encoder = {
530
sizeof(AlacEncodeContext),
551
.type = AVMEDIA_TYPE_AUDIO,
553
.priv_data_size = sizeof(AlacEncodeContext),
554
.init = alac_encode_init,
555
.encode = alac_encode_frame,
556
.close = alac_encode_close,
534
557
.capabilities = CODEC_CAP_SMALL_LAST_FRAME,
535
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
558
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
559
AV_SAMPLE_FMT_NONE },
536
560
.long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),