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 #ifndef AVCODEC_AACENC_H
23 #define AVCODEC_AACENC_H
24 
25 #include "libavutil/float_dsp.h"
26 #include "avcodec.h"
27 #include "put_bits.h"
28 
29 #include "aac.h"
30 #include "audio_frame_queue.h"
31 #include "psymodel.h"
32 
33 #include "lpc.h"
34 
35 typedef enum AACCoder {
36     AAC_CODER_ANMR = 0,
37     AAC_CODER_TWOLOOP,
38     AAC_CODER_FAST,
39 
40     AAC_CODER_NB,
41 }AACCoder;
42 
43 typedef struct AACEncOptions {
44     int coder;
45     int pns;
46     int tns;
47     int ltp;
48     int pce;
49     int pred;
50     int mid_side;
51     int intensity_stereo;
52 } AACEncOptions;
53 
54 struct AACEncContext;
55 
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;
81 
82 extern const AACCoefficientsEncoder ff_aac_coders[];
83 
84 typedef struct AACQuantizeBandCostCacheEntry {
85     float rd;
86     float energy;
87     int bits;
88     char cb;
89     char rtz;
90     uint16_t generation;
91 } AACQuantizeBandCostCacheEntry;
92 
93 typedef struct AACPCEInfo {
94     int64_t layout;
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
100 } AACPCEInfo;
101 
102 /**
103  * List of PCE (Program Configuration Element) for the channel layouts listed
104  * in channel_layout.h
105  *
106  * For those wishing in the future to add other layouts:
107  *
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).
111  *
112  * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
113  *
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
117  *
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)
122  *
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).
125  *
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).
130  *
131  * - config_map: total number of elements and their types. Beware, the way the
132  *               types are ordered impacts the final channel ordering.
133  *
134  * - reorder_map: reorders the channels.
135  *
136  */
137 static const AACPCEInfo aac_pce_configs[] = {
138     {
139         .layout = AV_CH_LAYOUT_MONO,
140         .num_ele = { 1, 0, 0, 0 },
141         .pairing = { { 0 }, },
142         .index = { { 0 }, },
143         .config_map = { 1, TYPE_SCE, },
144         .reorder_map = { 0 },
145     },
146     {
147         .layout = AV_CH_LAYOUT_STEREO,
148         .num_ele = { 1, 0, 0, 0 },
149         .pairing = { { 1 }, },
150         .index = { { 0 }, },
151         .config_map = { 1, TYPE_CPE, },
152         .reorder_map = { 0, 1 },
153     },
154     {
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 },
161     },
162     {
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 },
169     },
170     {
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 },
177     },
178     {
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 },
185     },
186     {
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 },
193     },
194     {
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 },
201     },
202     {
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 },
209     },
210     {
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 },
217     },
218     {
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 },
225     },
226     {
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 },
233     },
234     {
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 },
241     },
242     {
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 },
249     },
250     {
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 },
257     },
258     {
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 },
265     },
266     {
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 },
273     },
274     {
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 },
281     },
282     {
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 },
289     },
290     {
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 },
297     },
298     {
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 },
305     },
306     {
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 },
313     },
314     {
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 },
321     },
322     {
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 },
329     },
330     {
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 },
337     },
338     {
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 },
345     },
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 },
353     },
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 },
362     },
363     {
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 },
370     },
371 };
372 
373 /**
374  * AAC encoder context
375  */
376 typedef struct AACEncContext {
377     AVClass *av_class;
378     AACEncOptions options;                       ///< encoding options
379     PutBitContext pb;
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
385 
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
393 
394     ChannelElement *cpe;                         ///< channel elements
395     FFPsyContext psy;
396     struct FFPsyPreprocessContext* psypp;
397     const AACCoefficientsEncoder *coder;
398     int cur_channel;                             ///< current channel for coder context
399     int random_state;
400     float lambda;
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
405 
406     AudioFrameQueue afq;
407     DECLARE_ALIGNED(16, int,   qcoefs)[96];      ///< quantized coefficients
408     DECLARE_ALIGNED(32, float, scoefs)[1024];    ///< scaled coefficients
409 
410     uint16_t quantize_band_cost_cache_generation;
411     AACQuantizeBandCostCacheEntry quantize_band_cost_cache[256][128]; ///< memoization area for quantize_band_cost
412 
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);
417 
418     struct {
419         float *samples;
420     } buffer;
421 } AACEncContext;
422 
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);
426 
427 
428 #endif /* AVCODEC_AACENC_H */
429