• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "libavutil/mem_internal.h"
27 
28 #include "avcodec.h"
29 #include "put_bits.h"
30 
31 #include "aac.h"
32 #include "audio_frame_queue.h"
33 #include "psymodel.h"
34 
35 #include "lpc.h"
36 
37 typedef enum AACCoder {
38     AAC_CODER_ANMR = 0,
39     AAC_CODER_TWOLOOP,
40     AAC_CODER_FAST,
41 
42     AAC_CODER_NB,
43 }AACCoder;
44 
45 typedef struct AACEncOptions {
46     int coder;
47     int pns;
48     int tns;
49     int ltp;
50     int pce;
51     int pred;
52     int mid_side;
53     int intensity_stereo;
54 } AACEncOptions;
55 
56 struct AACEncContext;
57 
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;
83 
84 extern const AACCoefficientsEncoder ff_aac_coders[];
85 
86 typedef struct AACQuantizeBandCostCacheEntry {
87     float rd;
88     float energy;
89     int bits;
90     char cb;
91     char rtz;
92     uint16_t generation;
93 } AACQuantizeBandCostCacheEntry;
94 
95 typedef struct AACPCEInfo {
96     int64_t layout;
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
102 } AACPCEInfo;
103 
104 /**
105  * List of PCE (Program Configuration Element) for the channel layouts listed
106  * in channel_layout.h
107  *
108  * For those wishing in the future to add other layouts:
109  *
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).
113  *
114  * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
115  *
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
119  *
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)
124  *
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).
127  *
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).
132  *
133  * - config_map: total number of elements and their types. Beware, the way the
134  *               types are ordered impacts the final channel ordering.
135  *
136  * - reorder_map: reorders the channels.
137  *
138  */
139 static const AACPCEInfo aac_pce_configs[] = {
140     {
141         .layout = AV_CH_LAYOUT_MONO,
142         .num_ele = { 1, 0, 0, 0 },
143         .pairing = { { 0 }, },
144         .index = { { 0 }, },
145         .config_map = { 1, TYPE_SCE, },
146         .reorder_map = { 0 },
147     },
148     {
149         .layout = AV_CH_LAYOUT_STEREO,
150         .num_ele = { 1, 0, 0, 0 },
151         .pairing = { { 1 }, },
152         .index = { { 0 }, },
153         .config_map = { 1, TYPE_CPE, },
154         .reorder_map = { 0, 1 },
155     },
156     {
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 },
163     },
164     {
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 },
171     },
172     {
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 },
179     },
180     {
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 },
187     },
188     {
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 },
195     },
196     {
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 },
203     },
204     {
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 },
211     },
212     {
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 },
219     },
220     {
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 },
227     },
228     {
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 },
235     },
236     {
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 },
243     },
244     {
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 },
251     },
252     {
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 },
259     },
260     {
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 },
267     },
268     {
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 },
275     },
276     {
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 },
283     },
284     {
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 },
291     },
292     {
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 },
299     },
300     {
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 },
307     },
308     {
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 },
315     },
316     {
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 },
323     },
324     {
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 },
331     },
332     {
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 },
339     },
340     {
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 },
347     },
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 },
355     },
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 },
364     },
365     {
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 },
372     },
373 };
374 
375 /**
376  * AAC encoder context
377  */
378 typedef struct AACEncContext {
379     AVClass *av_class;
380     AACEncOptions options;                       ///< encoding options
381     PutBitContext pb;
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
387 
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
395 
396     ChannelElement *cpe;                         ///< channel elements
397     FFPsyContext psy;
398     struct FFPsyPreprocessContext* psypp;
399     const AACCoefficientsEncoder *coder;
400     int cur_channel;                             ///< current channel for coder context
401     int random_state;
402     float lambda;
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
407 
408     AudioFrameQueue afq;
409     DECLARE_ALIGNED(16, int,   qcoefs)[96];      ///< quantized coefficients
410     DECLARE_ALIGNED(32, float, scoefs)[1024];    ///< scaled coefficients
411 
412     uint16_t quantize_band_cost_cache_generation;
413     AACQuantizeBandCostCacheEntry quantize_band_cost_cache[256][128]; ///< memoization area for quantize_band_cost
414 
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);
419 
420     struct {
421         float *samples;
422     } buffer;
423 } AACEncContext;
424 
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);
428 
429 
430 #endif /* AVCODEC_AACENC_H */
431