Import ok'd parts of ALAC encoder from GSoC repo.
[libav.git] / libavcodec / alacenc.c
1 /**
2 * ALAC audio encoder
3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "avcodec.h"
23 #include "bitstream.h"
24 #include "dsputil.h"
25 #include "lpc.h"
26
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
33
34 #define ALAC_ESCAPE_CODE 0x1FF
35 #define ALAC_MAX_LPC_ORDER 30
36
37 int interlacing_shift;
38 int interlacing_leftweight;
39 PutBitContext pbctx;
40 DSPContext dspctx;
41 AVCodecContext *avctx;
42 } AlacEncodeContext;
43
44
45 static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
46 {
47 int divisor, q, r;
48
49 k = FFMIN(k, s->rc.k_modifier);
50 divisor = (1<<k) - 1;
51 q = x / divisor;
52 r = x % divisor;
53
54 if(q > 8) {
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);
58 } else {
59 if(q)
60 put_bits(&s->pbctx, q, (1<<q) - 1);
61 put_bits(&s->pbctx, 1, 0);
62
63 if(k != 1) {
64 if(r > 0)
65 put_bits(&s->pbctx, k, r+1);
66 else
67 put_bits(&s->pbctx, k-1, 0);
68 }
69 }
70 }
71
72 static void write_frame_header(AlacEncodeContext *s, int is_verbatim)
73 {
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
80 }
81
82 static void write_compressed_frame(AlacEncodeContext *s)
83 {
84 int i, j;
85
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);
90
91 for(i=0;i<s->channels;i++) {
92
93 calc_predictor_params(s, i);
94
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);
97
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]);
103 }
104 }
105
106 // apply lpc and entropy coding to audio samples
107
108 for(i=0;i<s->channels;i++) {
109 alac_linear_predictor(s, i);
110 alac_entropy_coder(s);
111 }
112 }
113
114 static av_cold int alac_encode_init(AVCodecContext *avctx)
115 {
116 AlacEncodeContext *s = avctx->priv_data;
117 uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1);
118
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;
123
124 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
125 av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
126 return -1;
127 }
128
129 // Set default compression level
130 if(avctx->compression_level == FF_COMPRESSION_DEFAULT)
131 s->compression_level = 1;
132 else
133 s->compression_level = av_clip(avctx->compression_level, 0, 1);
134
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;
140
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;
143
144 s->write_sample_size = avctx->bits_per_sample + s->channels - 1; // FIXME: consider wasted_bytes
145
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);
154
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);
160 }
161
162 avctx->extradata = alac_extradata;
163 avctx->extradata_size = ALAC_EXTRADATA_SIZE;
164
165 avctx->coded_frame = avcodec_alloc_frame();
166 avctx->coded_frame->key_frame = 1;
167
168 s->avctx = avctx;
169 dsputil_init(&s->dspctx, avctx);
170
171 allocate_sample_buffers(s);
172
173 return 0;
174 }
175
176 static av_cold int alac_encode_close(AVCodecContext *avctx)
177 {
178 AlacEncodeContext *s = avctx->priv_data;
179
180 av_freep(&avctx->extradata);
181 avctx->extradata_size = 0;
182 av_freep(&avctx->coded_frame);
183 free_sample_buffers(s);
184 return 0;
185 }
186
187 AVCodec alac_encoder = {
188 "alac",
189 CODEC_TYPE_AUDIO,
190 CODEC_ID_ALAC,
191 sizeof(AlacEncodeContext),
192 alac_encode_init,
193 alac_encode_frame,
194 alac_encode_close,
195 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
196 .long_name = "ALAC (Apple Lossless Audio Codec)",
197 };