7571
|
1 /*
|
|
2 * AAC encoder
|
|
3 * Copyright (C) 2008 Konstantin Shishkov
|
|
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 /**
|
|
23 * @file aacenc.c
|
|
24 * AAC encoder
|
|
25 */
|
|
26
|
|
27 /***********************************
|
|
28 * TODOs:
|
|
29 * psy model selection with some option
|
7584
|
30 * add sane pulse detection
|
7571
|
31 ***********************************/
|
|
32
|
|
33 #include "avcodec.h"
|
|
34 #include "bitstream.h"
|
|
35 #include "dsputil.h"
|
|
36 #include "mpeg4audio.h"
|
|
37
|
|
38 #include "aacpsy.h"
|
|
39 #include "aac.h"
|
|
40 #include "aactab.h"
|
|
41
|
|
42 static const uint8_t swb_size_1024_96[] = {
|
|
43 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
|
|
44 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
|
|
45 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
|
|
46 };
|
|
47
|
|
48 static const uint8_t swb_size_1024_64[] = {
|
|
49 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
|
|
50 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
|
|
51 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
|
|
52 };
|
|
53
|
|
54 static const uint8_t swb_size_1024_48[] = {
|
|
55 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
|
|
56 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
|
|
57 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
|
|
58 96
|
|
59 };
|
|
60
|
|
61 static const uint8_t swb_size_1024_32[] = {
|
|
62 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
|
|
63 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
|
|
64 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
|
|
65 };
|
|
66
|
|
67 static const uint8_t swb_size_1024_24[] = {
|
|
68 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
69 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
|
|
70 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
|
|
71 };
|
|
72
|
|
73 static const uint8_t swb_size_1024_16[] = {
|
|
74 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
|
75 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
|
|
76 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
|
|
77 };
|
|
78
|
|
79 static const uint8_t swb_size_1024_8[] = {
|
|
80 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
|
|
81 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
|
|
82 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
|
|
83 };
|
|
84
|
|
85 static const uint8_t *swb_size_1024[] = {
|
|
86 swb_size_1024_96, swb_size_1024_96, swb_size_1024_64,
|
|
87 swb_size_1024_48, swb_size_1024_48, swb_size_1024_32,
|
|
88 swb_size_1024_24, swb_size_1024_24, swb_size_1024_16,
|
|
89 swb_size_1024_16, swb_size_1024_16, swb_size_1024_8
|
|
90 };
|
|
91
|
|
92 static const uint8_t swb_size_128_96[] = {
|
|
93 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
|
|
94 };
|
|
95
|
|
96 static const uint8_t swb_size_128_48[] = {
|
|
97 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
|
|
98 };
|
|
99
|
|
100 static const uint8_t swb_size_128_24[] = {
|
|
101 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
|
|
102 };
|
|
103
|
|
104 static const uint8_t swb_size_128_16[] = {
|
|
105 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
|
|
106 };
|
|
107
|
|
108 static const uint8_t swb_size_128_8[] = {
|
|
109 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
|
|
110 };
|
|
111
|
|
112 static const uint8_t *swb_size_128[] = {
|
|
113 /* the last entry on the following row is swb_size_128_64 but is a
|
|
114 duplicate of swb_size_128_96 */
|
|
115 swb_size_128_96, swb_size_128_96, swb_size_128_96,
|
|
116 swb_size_128_48, swb_size_128_48, swb_size_128_48,
|
|
117 swb_size_128_24, swb_size_128_24, swb_size_128_16,
|
|
118 swb_size_128_16, swb_size_128_16, swb_size_128_8
|
|
119 };
|
|
120
|
|
121 /** default channel configurations */
|
|
122 static const uint8_t aac_chan_configs[6][5] = {
|
7585
|
123 {1, TYPE_SCE}, // 1 channel - single channel element
|
|
124 {1, TYPE_CPE}, // 2 channels - channel pair
|
|
125 {2, TYPE_SCE, TYPE_CPE}, // 3 channels - center + stereo
|
|
126 {3, TYPE_SCE, TYPE_CPE, TYPE_SCE}, // 4 channels - front center + stereo + back center
|
|
127 {3, TYPE_SCE, TYPE_CPE, TYPE_CPE}, // 5 channels - front center + stereo + back stereo
|
|
128 {4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE}, // 6 channels - front center + stereo + back stereo + LFE
|
7571
|
129 };
|
|
130
|
|
131 /**
|
7584
|
132 * AAC encoder context
|
|
133 */
|
|
134 typedef struct {
|
|
135 PutBitContext pb;
|
|
136 MDCTContext mdct1024; ///< long (1024 samples) frame transform context
|
|
137 MDCTContext mdct128; ///< short (128 samples) frame transform context
|
|
138 DSPContext dsp;
|
|
139 } AACEncContext;
|
|
140
|
|
141 /**
|
7571
|
142 * Make AAC audio config object.
|
|
143 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
|
|
144 */
|
|
145 static void put_audio_specific_config(AVCodecContext *avctx)
|
|
146 {
|
|
147 PutBitContext pb;
|
|
148 AACEncContext *s = avctx->priv_data;
|
|
149
|
|
150 init_put_bits(&pb, avctx->extradata, avctx->extradata_size*8);
|
|
151 put_bits(&pb, 5, 2); //object type - AAC-LC
|
|
152 put_bits(&pb, 4, s->samplerate_index); //sample rate index
|
|
153 put_bits(&pb, 4, avctx->channels);
|
|
154 //GASpecificConfig
|
|
155 put_bits(&pb, 1, 0); //frame length - 1024 samples
|
|
156 put_bits(&pb, 1, 0); //does not depend on core coder
|
|
157 put_bits(&pb, 1, 0); //is not extension
|
|
158 flush_put_bits(&pb);
|
|
159 }
|
|
160
|
|
161 static av_cold int aac_encode_init(AVCodecContext *avctx)
|
|
162 {
|
|
163 AACEncContext *s = avctx->priv_data;
|
|
164 int i;
|
|
165
|
|
166 avctx->frame_size = 1024;
|
|
167
|
|
168 for(i = 0; i < 16; i++)
|
|
169 if(avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
|
|
170 break;
|
|
171 if(i == 16){
|
|
172 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", avctx->sample_rate);
|
|
173 return -1;
|
|
174 }
|
|
175 if(avctx->channels > 6){
|
|
176 av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", avctx->channels);
|
|
177 return -1;
|
|
178 }
|
|
179 s->samplerate_index = i;
|
|
180 s->swb_sizes1024 = swb_size_1024[i];
|
|
181 s->swb_num1024 = ff_aac_num_swb_1024[i];
|
|
182 s->swb_sizes128 = swb_size_128[i];
|
|
183 s->swb_num128 = ff_aac_num_swb_128[i];
|
|
184
|
|
185 dsputil_init(&s->dsp, avctx);
|
|
186 ff_mdct_init(&s->mdct1024, 11, 0);
|
|
187 ff_mdct_init(&s->mdct128, 8, 0);
|
7584
|
188 // window init
|
|
189 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
|
|
190 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
|
|
191 ff_sine_window_init(ff_sine_1024, 1024);
|
|
192 ff_sine_window_init(ff_sine_128, 128);
|
7571
|
193
|
|
194 s->samples = av_malloc(2 * 1024 * avctx->channels * sizeof(s->samples[0]));
|
|
195 s->cpe = av_mallocz(sizeof(ChannelElement) * aac_chan_configs[avctx->channels-1][0]);
|
|
196 if(ff_aac_psy_init(&s->psy, avctx, AAC_PSY_3GPP, aac_chan_configs[avctx->channels-1][0], 0, s->swb_sizes1024, s->swb_num1024, s->swb_sizes128, s->swb_num128) < 0){
|
|
197 av_log(avctx, AV_LOG_ERROR, "Cannot initialize selected model.\n");
|
|
198 return -1;
|
|
199 }
|
|
200 avctx->extradata = av_malloc(2);
|
|
201 avctx->extradata_size = 2;
|
|
202 put_audio_specific_config(avctx);
|
|
203 return 0;
|
|
204 }
|
|
205
|
|
206 /**
|
|
207 * Encode ics_info element.
|
|
208 * @see Table 4.6 (syntax of ics_info)
|
|
209 */
|
|
210 static void put_ics_info(AVCodecContext *avctx, IndividualChannelStream *info)
|
|
211 {
|
|
212 AACEncContext *s = avctx->priv_data;
|
|
213 int i;
|
|
214
|
|
215 put_bits(&s->pb, 1, 0); // ics_reserved bit
|
|
216 put_bits(&s->pb, 2, info->window_sequence[0]);
|
|
217 put_bits(&s->pb, 1, info->use_kb_window[0]);
|
|
218 if(info->window_sequence[0] != EIGHT_SHORT_SEQUENCE){
|
|
219 put_bits(&s->pb, 6, info->max_sfb);
|
|
220 put_bits(&s->pb, 1, 0); // no prediction
|
|
221 }else{
|
|
222 put_bits(&s->pb, 4, info->max_sfb);
|
|
223 for(i = 1; i < info->num_windows; i++)
|
|
224 put_bits(&s->pb, 1, info->group_len[i]);
|
|
225 }
|
|
226 }
|
|
227
|
|
228 /**
|
7584
|
229 * Encode pulse data.
|
|
230 */
|
|
231 static void encode_pulses(AVCodecContext *avctx, AACEncContext *s, Pulse *pulse, int channel)
|
|
232 {
|
|
233 int i;
|
|
234
|
|
235 put_bits(&s->pb, 1, !!pulse->num_pulse);
|
|
236 if(!pulse->num_pulse) return;
|
|
237
|
|
238 put_bits(&s->pb, 2, pulse->num_pulse - 1);
|
|
239 put_bits(&s->pb, 6, pulse->start);
|
|
240 for(i = 0; i < pulse->num_pulse; i++){
|
7585
|
241 put_bits(&s->pb, 5, pulse->pos[i]);
|
7584
|
242 put_bits(&s->pb, 4, pulse->amp[i]);
|
|
243 }
|
|
244 }
|
|
245
|
|
246 /**
|
|
247 * Encode spectral coefficients processed by psychoacoustic model.
|
|
248 */
|
|
249 static void encode_spectral_coeffs(AVCodecContext *avctx, AACEncContext *s, ChannelElement *cpe, int channel)
|
|
250 {
|
|
251 int start, i, w, w2, wg;
|
|
252
|
|
253 w = 0;
|
|
254 for(wg = 0; wg < cpe->ch[channel].ics.num_window_groups; wg++){
|
|
255 start = 0;
|
|
256 for(i = 0; i < cpe->ch[channel].ics.max_sfb; i++){
|
|
257 if(cpe->ch[channel].zeroes[w][i]){
|
|
258 start += cpe->ch[channel].ics.swb_sizes[i];
|
|
259 continue;
|
|
260 }
|
|
261 for(w2 = w; w2 < w + cpe->ch[channel].ics.group_len[wg]; w2++){
|
|
262 encode_band_coeffs(s, cpe, channel, start + w2*128, cpe->ch[channel].ics.swb_sizes[i], cpe->ch[channel].band_type[w][i]);
|
|
263 }
|
|
264 start += cpe->ch[channel].ics.swb_sizes[i];
|
|
265 }
|
|
266 w += cpe->ch[channel].ics.group_len[wg];
|
|
267 }
|
|
268 }
|
|
269
|
|
270 /**
|
7571
|
271 * Write some auxiliary information about the created AAC file.
|
|
272 */
|
|
273 static void put_bitstream_info(AVCodecContext *avctx, AACEncContext *s, const char *name)
|
|
274 {
|
|
275 int i, namelen, padbits;
|
|
276
|
|
277 namelen = strlen(name) + 2;
|
7585
|
278 put_bits(&s->pb, 3, TYPE_FIL);
|
7571
|
279 put_bits(&s->pb, 4, FFMIN(namelen, 15));
|
|
280 if(namelen >= 15)
|
|
281 put_bits(&s->pb, 8, namelen - 16);
|
|
282 put_bits(&s->pb, 4, 0); //extension type - filler
|
|
283 padbits = 8 - (put_bits_count(&s->pb) & 7);
|
|
284 align_put_bits(&s->pb);
|
|
285 for(i = 0; i < namelen - 2; i++)
|
|
286 put_bits(&s->pb, 8, name[i]);
|
|
287 put_bits(&s->pb, 12 - padbits, 0);
|
|
288 }
|
|
289
|
|
290 static av_cold int aac_encode_end(AVCodecContext *avctx)
|
|
291 {
|
|
292 AACEncContext *s = avctx->priv_data;
|
|
293
|
|
294 ff_mdct_end(&s->mdct1024);
|
|
295 ff_mdct_end(&s->mdct128);
|
|
296 ff_aac_psy_end(&s->psy);
|
|
297 av_freep(&s->samples);
|
|
298 av_freep(&s->cpe);
|
|
299 return 0;
|
|
300 }
|
|
301
|
|
302 AVCodec aac_encoder = {
|
|
303 "aac",
|
|
304 CODEC_TYPE_AUDIO,
|
|
305 CODEC_ID_AAC,
|
|
306 sizeof(AACEncContext),
|
|
307 aac_encode_init,
|
|
308 aac_encode_frame,
|
|
309 aac_encode_end,
|
|
310 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
|
|
311 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
|
|
312 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
|
|
313 };
|