3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bitstream.h"
27 #define DEFAULT_FRAME_SIZE 4096
28 #define DEFAULT_SAMPLE_SIZE 16
29 #define MAX_CHANNELS 8
30 #define ALAC_EXTRADATA_SIZE 36
31 #define ALAC_FRAME_HEADER_SIZE 55
32 #define ALAC_FRAME_FOOTER_SIZE 3
34 #define ALAC_ESCAPE_CODE 0x1FF
35 #define ALAC_MAX_LPC_ORDER 30
37 int interlacing_shift;
38 int interlacing_leftweight;
41 AVCodecContext *avctx;
45 static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
49 k = FFMIN(k, s->rc.k_modifier);
55 // write escape code and sample value directly
56 put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
57 put_bits(&s->pbctx, write_sample_size, x);
60 put_bits(&s->pbctx, q, (1<<q) - 1);
61 put_bits(&s->pbctx, 1, 0);
65 put_bits(&s->pbctx, k, r+1);
67 put_bits(&s->pbctx, k-1, 0);
72 static void write_frame_header(AlacEncodeContext *s, int is_verbatim)
74 put_bits(&s->pbctx, 3, s->channels-1); // No. of channels -1
75 put_bits(&s->pbctx, 16, 0); // Seems to be zero
76 put_bits(&s->pbctx, 1, 1); // Sample count is in the header
77 put_bits(&s->pbctx, 2, 0); // FIXME: Wasted bytes field
78 put_bits(&s->pbctx, 1, is_verbatim); // Audio block is verbatim
79 put_bits(&s->pbctx, 32, s->avctx->frame_size); // No. of samples in the frame
82 static void write_compressed_frame(AlacEncodeContext *s)
86 /* only simple mid/side decorrelation supported as of now */
87 alac_stereo_decorrelation(s);
88 put_bits(&s->pbctx, 8, s->interlacing_shift);
89 put_bits(&s->pbctx, 8, s->interlacing_leftweight);
91 for(i=0;i<s->channels;i++) {
93 calc_predictor_params(s, i);
95 put_bits(&s->pbctx, 4, 0); // prediction type : currently only type 0 has been RE'd
96 put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant);
98 put_bits(&s->pbctx, 3, s->rc.rice_modifier);
99 put_bits(&s->pbctx, 5, s->lpc[i].lpc_order);
100 // predictor coeff. table
101 for(j=0;j<s->lpc[i].lpc_order;j++) {
102 put_sbits(&s->pbctx, 16, s->lpc[i].lpc_coeff[j]);
106 // apply lpc and entropy coding to audio samples
108 for(i=0;i<s->channels;i++) {
109 alac_linear_predictor(s, i);
110 alac_entropy_coder(s);
114 static av_cold int alac_encode_init(AVCodecContext *avctx)
116 AlacEncodeContext *s = avctx->priv_data;
117 uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1);
119 avctx->frame_size = DEFAULT_FRAME_SIZE;
120 avctx->bits_per_sample = DEFAULT_SAMPLE_SIZE;
121 s->channels = avctx->channels;
122 s->samplerate = avctx->sample_rate;
124 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
125 av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
129 // Set default compression level
130 if(avctx->compression_level == FF_COMPRESSION_DEFAULT)
131 s->compression_level = 1;
133 s->compression_level = av_clip(avctx->compression_level, 0, 1);
135 // Initialize default Rice parameters
136 s->rc.history_mult = 40;
137 s->rc.initial_history = 10;
138 s->rc.k_modifier = 14;
139 s->rc.rice_modifier = 4;
141 s->max_coded_frame_size = (ALAC_FRAME_HEADER_SIZE + ALAC_FRAME_FOOTER_SIZE +
142 avctx->frame_size*s->channels*avctx->bits_per_sample)>>3;
144 s->write_sample_size = avctx->bits_per_sample + s->channels - 1; // FIXME: consider wasted_bytes
146 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
147 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
148 AV_WB32(alac_extradata+12, avctx->frame_size);
149 AV_WB8 (alac_extradata+17, avctx->bits_per_sample);
150 AV_WB8 (alac_extradata+21, s->channels);
151 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
152 AV_WB32(alac_extradata+28, s->samplerate*s->channels*avctx->bits_per_sample); // average bitrate
153 AV_WB32(alac_extradata+32, s->samplerate);
155 // Set relevant extradata fields
156 if(s->compression_level > 0) {
157 AV_WB8(alac_extradata+18, s->rc.history_mult);
158 AV_WB8(alac_extradata+19, s->rc.initial_history);
159 AV_WB8(alac_extradata+20, s->rc.k_modifier);
162 avctx->extradata = alac_extradata;
163 avctx->extradata_size = ALAC_EXTRADATA_SIZE;
165 avctx->coded_frame = avcodec_alloc_frame();
166 avctx->coded_frame->key_frame = 1;
169 dsputil_init(&s->dspctx, avctx);
171 allocate_sample_buffers(s);
176 static av_cold int alac_encode_close(AVCodecContext *avctx)
178 AlacEncodeContext *s = avctx->priv_data;
180 av_freep(&avctx->extradata);
181 avctx->extradata_size = 0;
182 av_freep(&avctx->coded_frame);
183 free_sample_buffers(s);
187 AVCodec alac_encoder = {
191 sizeof(AlacEncodeContext),
195 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
196 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),