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"
26 #include "libavutil/mem_internal.h"
32 #include "audio_frame_queue.h"
37 typedef enum AACCoder {
45 typedef struct AACEncOptions {
58 typedef struct AACCoefficientsEncoder {
59 void (*search_for_quantizers)(AVCodecContext *avctx, struct AACEncContext *s,
60 SingleChannelElement *sce, const float lambda);
61 void (*encode_window_bands_info)(struct AACEncContext *s, SingleChannelElement *sce,
62 int win, int group_len, const float lambda);
63 void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
64 int scale_idx, int cb, const float lambda, int rtz);
65 void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
66 void (*encode_ltp_info)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
67 void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
68 void (*adjust_common_pred)(struct AACEncContext *s, ChannelElement *cpe);
69 void (*adjust_common_ltp)(struct AACEncContext *s, ChannelElement *cpe);
70 void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
71 void (*apply_tns_filt)(struct AACEncContext *s, SingleChannelElement *sce);
72 void (*update_ltp)(struct AACEncContext *s, SingleChannelElement *sce);
73 void (*ltp_insert_new_frame)(struct AACEncContext *s);
74 void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
75 void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
76 void (*mark_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
77 void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
78 void (*search_for_ltp)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
79 void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
80 void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
81 void (*search_for_pred)(struct AACEncContext *s, SingleChannelElement *sce);
82 } AACCoefficientsEncoder;
84 extern const AACCoefficientsEncoder ff_aac_coders[];
86 typedef struct AACQuantizeBandCostCacheEntry {
93 } AACQuantizeBandCostCacheEntry;
95 typedef struct AACPCEInfo {
97 int num_ele[4]; ///< front, side, back, lfe
98 int pairing[3][8]; ///< front, side, back
99 int index[4][8]; ///< front, side, back, lfe
100 uint8_t config_map[16]; ///< configs the encoder's channel specific settings
101 uint8_t reorder_map[16]; ///< maps channels from lavc to aac order
105 * List of PCE (Program Configuration Element) for the channel layouts listed
106 * in channel_layout.h
108 * For those wishing in the future to add other layouts:
110 * - num_ele: number of elements in each group of front, side, back, lfe channels
111 * (an element is of type SCE (single channel), CPE (channel pair) for
112 * the first 3 groups; and is LFE for LFE group).
114 * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
116 * - index: there are three independent indices for SCE, CPE and LFE;
117 * they are incremented irrespective of the group to which the element belongs;
118 * they are not reset when going from one group to another
120 * Example: for 7.0 channel layout,
121 * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group)
122 * .index = { { 0, 0 }, { 1 }, { 2 }, },
123 * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs)
125 * The index order impacts the channel ordering. But is otherwise arbitrary
126 * (the sequence could have been 2, 0, 1 instead of 0, 1, 2).
128 * Spec allows for discontinuous indices, e.g. if one has a total of two SCE,
129 * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder
130 * which at this time requires that indices fully cover some range starting
131 * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15).
133 * - config_map: total number of elements and their types. Beware, the way the
134 * types are ordered impacts the final channel ordering.
136 * - reorder_map: reorders the channels.
139 static const AACPCEInfo aac_pce_configs[] = {
141 .layout = AV_CH_LAYOUT_MONO,
142 .num_ele = { 1, 0, 0, 0 },
143 .pairing = { { 0 }, },
145 .config_map = { 1, TYPE_SCE, },
146 .reorder_map = { 0 },
149 .layout = AV_CH_LAYOUT_STEREO,
150 .num_ele = { 1, 0, 0, 0 },
151 .pairing = { { 1 }, },
153 .config_map = { 1, TYPE_CPE, },
154 .reorder_map = { 0, 1 },
157 .layout = AV_CH_LAYOUT_2POINT1,
158 .num_ele = { 1, 0, 0, 1 },
159 .pairing = { { 1 }, },
160 .index = { { 0 },{ 0 },{ 0 },{ 0 } },
161 .config_map = { 2, TYPE_CPE, TYPE_LFE },
162 .reorder_map = { 0, 1, 2 },
165 .layout = AV_CH_LAYOUT_2_1,
166 .num_ele = { 1, 0, 1, 0 },
167 .pairing = { { 1 },{ 0 },{ 0 } },
168 .index = { { 0 },{ 0 },{ 0 }, },
169 .config_map = { 2, TYPE_CPE, TYPE_SCE },
170 .reorder_map = { 0, 1, 2 },
173 .layout = AV_CH_LAYOUT_SURROUND,
174 .num_ele = { 2, 0, 0, 0 },
175 .pairing = { { 1, 0 }, },
176 .index = { { 0, 0 }, },
177 .config_map = { 2, TYPE_CPE, TYPE_SCE, },
178 .reorder_map = { 0, 1, 2 },
181 .layout = AV_CH_LAYOUT_3POINT1,
182 .num_ele = { 2, 0, 0, 1 },
183 .pairing = { { 1, 0 }, },
184 .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
185 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_LFE },
186 .reorder_map = { 0, 1, 2, 3 },
189 .layout = AV_CH_LAYOUT_4POINT0,
190 .num_ele = { 2, 0, 1, 0 },
191 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
192 .index = { { 0, 0 }, { 0 }, { 1 } },
193 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_SCE },
194 .reorder_map = { 0, 1, 2, 3 },
197 .layout = AV_CH_LAYOUT_4POINT1,
198 .num_ele = { 2, 1, 1, 0 },
199 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
200 .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } },
201 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_SCE },
202 .reorder_map = { 0, 1, 2, 3, 4 },
205 .layout = AV_CH_LAYOUT_2_2,
206 .num_ele = { 1, 1, 0, 0 },
207 .pairing = { { 1 }, { 1 }, },
208 .index = { { 0 }, { 1 }, },
209 .config_map = { 2, TYPE_CPE, TYPE_CPE },
210 .reorder_map = { 0, 1, 2, 3 },
213 .layout = AV_CH_LAYOUT_QUAD,
214 .num_ele = { 1, 0, 1, 0 },
215 .pairing = { { 1 }, { 0 }, { 1 }, },
216 .index = { { 0 }, { 0 }, { 1 } },
217 .config_map = { 2, TYPE_CPE, TYPE_CPE },
218 .reorder_map = { 0, 1, 2, 3 },
221 .layout = AV_CH_LAYOUT_5POINT0,
222 .num_ele = { 2, 1, 0, 0 },
223 .pairing = { { 1, 0 }, { 1 }, },
224 .index = { { 0, 0 }, { 1 } },
225 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
226 .reorder_map = { 0, 1, 2, 3, 4 },
229 .layout = AV_CH_LAYOUT_5POINT1,
230 .num_ele = { 2, 1, 1, 0 },
231 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
232 .index = { { 0, 0 }, { 1 }, { 1 } },
233 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
234 .reorder_map = { 0, 1, 2, 3, 4, 5 },
237 .layout = AV_CH_LAYOUT_5POINT0_BACK,
238 .num_ele = { 2, 0, 1, 0 },
239 .pairing = { { 1, 0 }, { 0 }, { 1 } },
240 .index = { { 0, 0 }, { 0 }, { 1 } },
241 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
242 .reorder_map = { 0, 1, 2, 3, 4 },
245 .layout = AV_CH_LAYOUT_5POINT1_BACK,
246 .num_ele = { 2, 1, 1, 0 },
247 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
248 .index = { { 0, 0 }, { 1 }, { 1 } },
249 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
250 .reorder_map = { 0, 1, 2, 3, 4, 5 },
253 .layout = AV_CH_LAYOUT_6POINT0,
254 .num_ele = { 2, 1, 1, 0 },
255 .pairing = { { 1, 0 }, { 1 }, { 0 }, },
256 .index = { { 0, 0 }, { 1 }, { 1 } },
257 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
258 .reorder_map = { 0, 1, 2, 3, 4, 5 },
261 .layout = AV_CH_LAYOUT_6POINT0_FRONT,
262 .num_ele = { 2, 1, 0, 0 },
263 .pairing = { { 1, 1 }, { 1 } },
264 .index = { { 1, 0 }, { 2 }, },
265 .config_map = { 3, TYPE_CPE, TYPE_CPE, TYPE_CPE, },
266 .reorder_map = { 0, 1, 2, 3, 4, 5 },
269 .layout = AV_CH_LAYOUT_HEXAGONAL,
270 .num_ele = { 2, 0, 2, 0 },
271 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
272 .index = { { 0, 0 },{ 0 },{ 1, 1 } },
273 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, },
274 .reorder_map = { 0, 1, 2, 3, 4, 5 },
277 .layout = AV_CH_LAYOUT_6POINT1,
278 .num_ele = { 2, 1, 2, 0 },
279 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
280 .index = { { 0, 0 },{ 1 },{ 1, 2 } },
281 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
282 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
285 .layout = AV_CH_LAYOUT_6POINT1_BACK,
286 .num_ele = { 2, 1, 2, 0 },
287 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
288 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
289 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
290 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
293 .layout = AV_CH_LAYOUT_6POINT1_FRONT,
294 .num_ele = { 2, 1, 2, 0 },
295 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
296 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
297 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
298 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
301 .layout = AV_CH_LAYOUT_7POINT0,
302 .num_ele = { 2, 1, 1, 0 },
303 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
304 .index = { { 0, 0 }, { 1 }, { 2 }, },
305 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
306 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
309 .layout = AV_CH_LAYOUT_7POINT0_FRONT,
310 .num_ele = { 2, 1, 1, 0 },
311 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
312 .index = { { 0, 0 }, { 1 }, { 2 }, },
313 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
314 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
317 .layout = AV_CH_LAYOUT_7POINT1,
318 .num_ele = { 2, 1, 2, 0 },
319 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
320 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
321 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
322 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
325 .layout = AV_CH_LAYOUT_7POINT1_WIDE,
326 .num_ele = { 2, 1, 2, 0 },
327 .pairing = { { 1, 0 }, { 0 },{ 1, 1 }, },
328 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
329 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
330 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
333 .layout = AV_CH_LAYOUT_7POINT1_WIDE_BACK,
334 .num_ele = { 2, 1, 2, 0 },
335 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
336 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
337 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
338 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
341 .layout = AV_CH_LAYOUT_OCTAGONAL,
342 .num_ele = { 2, 1, 2, 0 },
343 .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, },
344 .index = { { 0, 0 }, { 1 }, { 2, 1 } },
345 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
346 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
348 { /* Meant for order 2/mixed ambisonics */
349 .layout = AV_CH_LAYOUT_OCTAGONAL | AV_CH_TOP_CENTER,
350 .num_ele = { 2, 2, 2, 0 },
351 .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, },
352 .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } },
353 .config_map = { 6, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
354 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
356 { /* Meant for order 2/mixed ambisonics */
357 .layout = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_BACK_CENTER |
358 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_TOP_CENTER,
359 .num_ele = { 2, 2, 2, 0 },
360 .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, },
361 .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } },
362 .config_map = { 6, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
363 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
366 .layout = AV_CH_LAYOUT_HEXADECAGONAL,
367 .num_ele = { 4, 2, 4, 0 },
368 .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, },
369 .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } },
370 .config_map = { 10, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
371 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
376 * AAC encoder context
378 typedef struct AACEncContext {
380 AACEncOptions options; ///< encoding options
382 FFTContext mdct1024; ///< long (1024 samples) frame transform context
383 FFTContext mdct128; ///< short (128 samples) frame transform context
384 AVFloatDSPContext *fdsp;
385 AACPCEInfo pce; ///< PCE data, if needed
386 float *planar_samples[16]; ///< saved preprocessed input
388 int profile; ///< copied from avctx
389 int needs_pce; ///< flag for non-standard layout
390 LPCContext lpc; ///< used by TNS
391 int samplerate_index; ///< MPEG-4 samplerate index
392 int channels; ///< channel count
393 const uint8_t *reorder_map; ///< lavc to aac reorder map
394 const uint8_t *chan_map; ///< channel configuration map
396 ChannelElement *cpe; ///< channel elements
398 struct FFPsyPreprocessContext* psypp;
399 const AACCoefficientsEncoder *coder;
400 int cur_channel; ///< current channel for coder context
403 int last_frame_pb_count; ///< number of bits for the previous frame
404 float lambda_sum; ///< sum(lambda), for Qvg reporting
405 int lambda_count; ///< count(lambda), for Qvg reporting
406 enum RawDataBlockType cur_type; ///< channel group type cur_channel belongs to
409 DECLARE_ALIGNED(16, int, qcoefs)[96]; ///< quantized coefficients
410 DECLARE_ALIGNED(32, float, scoefs)[1024]; ///< scaled coefficients
412 uint16_t quantize_band_cost_cache_generation;
413 AACQuantizeBandCostCacheEntry quantize_band_cost_cache[256][128]; ///< memoization area for quantize_band_cost
415 void (*abs_pow34)(float *out, const float *in, const int size);
416 void (*quant_bands)(int *out, const float *in, const float *scaled,
417 int size, int is_signed, int maxval, const float Q34,
418 const float rounding);
425 void ff_aac_dsp_init_x86(AACEncContext *s);
426 void ff_aac_coder_init_mips(AACEncContext *c);
427 void ff_quantize_band_cost_cache_init(struct AACEncContext *s);
430 #endif /* AVCODEC_AACENC_H */