3 * Copyright (C) 2008 Konstantin Shishkov
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
22 #ifndef AVCODEC_AACENC_H
23 #define AVCODEC_AACENC_H
25 #include "libavutil/float_dsp.h"
30 #include "audio_frame_queue.h"
35 typedef enum AACCoder {
43 typedef struct AACEncOptions {
56 typedef struct AACCoefficientsEncoder {
57 void (*search_for_quantizers)(AVCodecContext *avctx, struct AACEncContext *s,
58 SingleChannelElement *sce, const float lambda);
59 void (*encode_window_bands_info)(struct AACEncContext *s, SingleChannelElement *sce,
60 int win, int group_len, const float lambda);
61 void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
62 int scale_idx, int cb, const float lambda, int rtz);
63 void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
64 void (*encode_ltp_info)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
65 void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
66 void (*adjust_common_pred)(struct AACEncContext *s, ChannelElement *cpe);
67 void (*adjust_common_ltp)(struct AACEncContext *s, ChannelElement *cpe);
68 void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
69 void (*apply_tns_filt)(struct AACEncContext *s, SingleChannelElement *sce);
70 void (*update_ltp)(struct AACEncContext *s, SingleChannelElement *sce);
71 void (*ltp_insert_new_frame)(struct AACEncContext *s);
72 void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
73 void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
74 void (*mark_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
75 void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
76 void (*search_for_ltp)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
77 void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
78 void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
79 void (*search_for_pred)(struct AACEncContext *s, SingleChannelElement *sce);
80 } AACCoefficientsEncoder;
82 extern const AACCoefficientsEncoder ff_aac_coders[];
84 typedef struct AACQuantizeBandCostCacheEntry {
91 } AACQuantizeBandCostCacheEntry;
93 typedef struct AACPCEInfo {
95 int num_ele[4]; ///< front, side, back, lfe
96 int pairing[3][8]; ///< front, side, back
97 int index[4][8]; ///< front, side, back, lfe
98 uint8_t config_map[16]; ///< configs the encoder's channel specific settings
99 uint8_t reorder_map[16]; ///< maps channels from lavc to aac order
103 * List of PCE (Program Configuration Element) for the channel layouts listed
104 * in channel_layout.h
106 * For those wishing in the future to add other layouts:
108 * - num_ele: number of elements in each group of front, side, back, lfe channels
109 * (an element is of type SCE (single channel), CPE (channel pair) for
110 * the first 3 groups; and is LFE for LFE group).
112 * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
114 * - index: there are three independent indices for SCE, CPE and LFE;
115 * they are incremented irrespective of the group to which the element belongs;
116 * they are not reset when going from one group to another
118 * Example: for 7.0 channel layout,
119 * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group)
120 * .index = { { 0, 0 }, { 1 }, { 2 }, },
121 * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs)
123 * The index order impacts the channel ordering. But is otherwise arbitrary
124 * (the sequence could have been 2, 0, 1 instead of 0, 1, 2).
126 * Spec allows for discontinuous indices, e.g. if one has a total of two SCE,
127 * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder
128 * which at this time requires that indices fully cover some range starting
129 * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15).
131 * - config_map: total number of elements and their types. Beware, the way the
132 * types are ordered impacts the final channel ordering.
134 * - reorder_map: reorders the channels.
137 static const AACPCEInfo aac_pce_configs[] = {
139 .layout = AV_CH_LAYOUT_MONO,
140 .num_ele = { 1, 0, 0, 0 },
141 .pairing = { { 0 }, },
143 .config_map = { 1, TYPE_SCE, },
144 .reorder_map = { 0 },
147 .layout = AV_CH_LAYOUT_STEREO,
148 .num_ele = { 1, 0, 0, 0 },
149 .pairing = { { 1 }, },
151 .config_map = { 1, TYPE_CPE, },
152 .reorder_map = { 0, 1 },
155 .layout = AV_CH_LAYOUT_2POINT1,
156 .num_ele = { 1, 0, 0, 1 },
157 .pairing = { { 1 }, },
158 .index = { { 0 },{ 0 },{ 0 },{ 0 } },
159 .config_map = { 2, TYPE_CPE, TYPE_LFE },
160 .reorder_map = { 0, 1, 2 },
163 .layout = AV_CH_LAYOUT_2_1,
164 .num_ele = { 1, 0, 1, 0 },
165 .pairing = { { 1 },{ 0 },{ 0 } },
166 .index = { { 0 },{ 0 },{ 0 }, },
167 .config_map = { 2, TYPE_CPE, TYPE_SCE },
168 .reorder_map = { 0, 1, 2 },
171 .layout = AV_CH_LAYOUT_SURROUND,
172 .num_ele = { 2, 0, 0, 0 },
173 .pairing = { { 1, 0 }, },
174 .index = { { 0, 0 }, },
175 .config_map = { 2, TYPE_CPE, TYPE_SCE, },
176 .reorder_map = { 0, 1, 2 },
179 .layout = AV_CH_LAYOUT_3POINT1,
180 .num_ele = { 2, 0, 0, 1 },
181 .pairing = { { 1, 0 }, },
182 .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
183 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_LFE },
184 .reorder_map = { 0, 1, 2, 3 },
187 .layout = AV_CH_LAYOUT_4POINT0,
188 .num_ele = { 2, 0, 1, 0 },
189 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
190 .index = { { 0, 0 }, { 0 }, { 1 } },
191 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_SCE },
192 .reorder_map = { 0, 1, 2, 3 },
195 .layout = AV_CH_LAYOUT_4POINT1,
196 .num_ele = { 2, 1, 1, 0 },
197 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
198 .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } },
199 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_SCE },
200 .reorder_map = { 0, 1, 2, 3, 4 },
203 .layout = AV_CH_LAYOUT_2_2,
204 .num_ele = { 1, 1, 0, 0 },
205 .pairing = { { 1 }, { 1 }, },
206 .index = { { 0 }, { 1 }, },
207 .config_map = { 2, TYPE_CPE, TYPE_CPE },
208 .reorder_map = { 0, 1, 2, 3 },
211 .layout = AV_CH_LAYOUT_QUAD,
212 .num_ele = { 1, 0, 1, 0 },
213 .pairing = { { 1 }, { 0 }, { 1 }, },
214 .index = { { 0 }, { 0 }, { 1 } },
215 .config_map = { 2, TYPE_CPE, TYPE_CPE },
216 .reorder_map = { 0, 1, 2, 3 },
219 .layout = AV_CH_LAYOUT_5POINT0,
220 .num_ele = { 2, 1, 0, 0 },
221 .pairing = { { 1, 0 }, { 1 }, },
222 .index = { { 0, 0 }, { 1 } },
223 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
224 .reorder_map = { 0, 1, 2, 3, 4 },
227 .layout = AV_CH_LAYOUT_5POINT1,
228 .num_ele = { 2, 1, 1, 0 },
229 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
230 .index = { { 0, 0 }, { 1 }, { 1 } },
231 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
232 .reorder_map = { 0, 1, 2, 3, 4, 5 },
235 .layout = AV_CH_LAYOUT_5POINT0_BACK,
236 .num_ele = { 2, 0, 1, 0 },
237 .pairing = { { 1, 0 }, { 0 }, { 1 } },
238 .index = { { 0, 0 }, { 0 }, { 1 } },
239 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
240 .reorder_map = { 0, 1, 2, 3, 4 },
243 .layout = AV_CH_LAYOUT_5POINT1_BACK,
244 .num_ele = { 2, 1, 1, 0 },
245 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
246 .index = { { 0, 0 }, { 1 }, { 1 } },
247 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
248 .reorder_map = { 0, 1, 2, 3, 4, 5 },
251 .layout = AV_CH_LAYOUT_6POINT0,
252 .num_ele = { 2, 1, 1, 0 },
253 .pairing = { { 1, 0 }, { 1 }, { 0 }, },
254 .index = { { 0, 0 }, { 1 }, { 1 } },
255 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
256 .reorder_map = { 0, 1, 2, 3, 4, 5 },
259 .layout = AV_CH_LAYOUT_6POINT0_FRONT,
260 .num_ele = { 2, 1, 0, 0 },
261 .pairing = { { 1, 1 }, { 1 } },
262 .index = { { 1, 0 }, { 2 }, },
263 .config_map = { 3, TYPE_CPE, TYPE_CPE, TYPE_CPE, },
264 .reorder_map = { 0, 1, 2, 3, 4, 5 },
267 .layout = AV_CH_LAYOUT_HEXAGONAL,
268 .num_ele = { 2, 0, 2, 0 },
269 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
270 .index = { { 0, 0 },{ 0 },{ 1, 1 } },
271 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, },
272 .reorder_map = { 0, 1, 2, 3, 4, 5 },
275 .layout = AV_CH_LAYOUT_6POINT1,
276 .num_ele = { 2, 1, 2, 0 },
277 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
278 .index = { { 0, 0 },{ 1 },{ 1, 2 } },
279 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
280 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
283 .layout = AV_CH_LAYOUT_6POINT1_BACK,
284 .num_ele = { 2, 1, 2, 0 },
285 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
286 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
287 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
288 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
291 .layout = AV_CH_LAYOUT_6POINT1_FRONT,
292 .num_ele = { 2, 1, 2, 0 },
293 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
294 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
295 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
296 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
299 .layout = AV_CH_LAYOUT_7POINT0,
300 .num_ele = { 2, 1, 1, 0 },
301 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
302 .index = { { 0, 0 }, { 1 }, { 2 }, },
303 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
304 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
307 .layout = AV_CH_LAYOUT_7POINT0_FRONT,
308 .num_ele = { 2, 1, 1, 0 },
309 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
310 .index = { { 0, 0 }, { 1 }, { 2 }, },
311 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
312 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
315 .layout = AV_CH_LAYOUT_7POINT1,
316 .num_ele = { 2, 1, 2, 0 },
317 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
318 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
319 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
320 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
323 .layout = AV_CH_LAYOUT_7POINT1_WIDE,
324 .num_ele = { 2, 1, 2, 0 },
325 .pairing = { { 1, 0 }, { 0 },{ 1, 1 }, },
326 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
327 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
328 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
331 .layout = AV_CH_LAYOUT_7POINT1_WIDE_BACK,
332 .num_ele = { 2, 1, 2, 0 },
333 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
334 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
335 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
336 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
339 .layout = AV_CH_LAYOUT_OCTAGONAL,
340 .num_ele = { 2, 1, 2, 0 },
341 .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, },
342 .index = { { 0, 0 }, { 1 }, { 2, 1 } },
343 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
344 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
346 { /* Meant for order 2/mixed ambisonics */
347 .layout = AV_CH_LAYOUT_OCTAGONAL | AV_CH_TOP_CENTER,
348 .num_ele = { 2, 2, 2, 0 },
349 .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, },
350 .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } },
351 .config_map = { 6, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
352 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
354 { /* Meant for order 2/mixed ambisonics */
355 .layout = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_BACK_CENTER |
356 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_TOP_CENTER,
357 .num_ele = { 2, 2, 2, 0 },
358 .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, },
359 .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } },
360 .config_map = { 6, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
361 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
364 .layout = AV_CH_LAYOUT_HEXADECAGONAL,
365 .num_ele = { 4, 2, 4, 0 },
366 .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, },
367 .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } },
368 .config_map = { 10, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
369 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
374 * AAC encoder context
376 typedef struct AACEncContext {
378 AACEncOptions options; ///< encoding options
380 FFTContext mdct1024; ///< long (1024 samples) frame transform context
381 FFTContext mdct128; ///< short (128 samples) frame transform context
382 AVFloatDSPContext *fdsp;
383 AACPCEInfo pce; ///< PCE data, if needed
384 float *planar_samples[16]; ///< saved preprocessed input
386 int profile; ///< copied from avctx
387 int needs_pce; ///< flag for non-standard layout
388 LPCContext lpc; ///< used by TNS
389 int samplerate_index; ///< MPEG-4 samplerate index
390 int channels; ///< channel count
391 const uint8_t *reorder_map; ///< lavc to aac reorder map
392 const uint8_t *chan_map; ///< channel configuration map
394 ChannelElement *cpe; ///< channel elements
396 struct FFPsyPreprocessContext* psypp;
397 const AACCoefficientsEncoder *coder;
398 int cur_channel; ///< current channel for coder context
401 int last_frame_pb_count; ///< number of bits for the previous frame
402 float lambda_sum; ///< sum(lambda), for Qvg reporting
403 int lambda_count; ///< count(lambda), for Qvg reporting
404 enum RawDataBlockType cur_type; ///< channel group type cur_channel belongs to
407 DECLARE_ALIGNED(16, int, qcoefs)[96]; ///< quantized coefficients
408 DECLARE_ALIGNED(32, float, scoefs)[1024]; ///< scaled coefficients
410 uint16_t quantize_band_cost_cache_generation;
411 AACQuantizeBandCostCacheEntry quantize_band_cost_cache[256][128]; ///< memoization area for quantize_band_cost
413 void (*abs_pow34)(float *out, const float *in, const int size);
414 void (*quant_bands)(int *out, const float *in, const float *scaled,
415 int size, int is_signed, int maxval, const float Q34,
416 const float rounding);
423 void ff_aac_dsp_init_x86(AACEncContext *s);
424 void ff_aac_coder_init_mips(AACEncContext *c);
425 void ff_quantize_band_cost_cache_init(struct AACEncContext *s);
428 #endif /* AVCODEC_AACENC_H */