Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libSBRenc / include / sbr_encoder.h
1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
6 All rights reserved.
7
8 1. INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28
29 2. COPYRIGHT LICENSE
30
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52
53 3. NO PATENT LICENSE
54
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 /*************************** Fraunhofer IIS ***********************
85
86 Author(s):
87 Description: SBR encoder top level processing prototype
88
89 ******************************************************************************/
90
91 #ifndef __SBR_ENCODER_H
92 #define __SBR_ENCODER_H
93
94 #include "common_fix.h"
95 #include "FDK_audio.h"
96
97 #include "FDK_bitstream.h"
98
99 /* core coder helpers */
100 #define MAX_TRANS_FAC 8
101 #define MAX_CODEC_FRAME_RATIO 2
102 #define MAX_PAYLOAD_SIZE 256
103
104 typedef enum codecType
105 {
106 CODEC_AAC=0,
107 CODEC_AACLD=1,
108 CODEC_UNSPECIFIED=99
109 } CODEC_TYPE;
110
111
112 typedef struct
113 {
114 INT bitRate;
115 INT nChannels;
116 INT sampleFreq;
117 INT transFac;
118 INT standardBitrate;
119 } CODEC_PARAM;
120
121 typedef enum
122 {
123 SBR_MONO,
124 SBR_LEFT_RIGHT,
125 SBR_COUPLING,
126 SBR_SWITCH_LRC
127 } SBR_STEREO_MODE;
128
129 /* bitstream syntax flags */
130 enum
131 {
132 SBR_SYNTAX_LOW_DELAY = 0x0001,
133 SBR_SYNTAX_SCALABLE = 0x0002,
134 SBR_SYNTAX_CRC = 0x0004,
135 SBR_SYNTAX_DRM_CRC = 0x0008
136 };
137
138 typedef struct
139 {
140 CODEC_TYPE coreCoder; /*!< LC or ELD */
141 UINT bitrateFrom; /*!< inclusive */
142 UINT bitrateTo; /*!< exclusive */
143
144 UINT sampleRate; /*!< */
145 UCHAR numChannels; /*!< */
146
147 UCHAR startFreq; /*!< bs_start_freq */
148 UCHAR startFreqSpeech; /*!< bs_start_freq for speech config flag */
149 UCHAR stopFreq; /*!< bs_stop_freq */
150 UCHAR stopFreqSpeech; /*!< bs_stop_freq for speech config flag */
151
152 UCHAR numNoiseBands; /*!< */
153 UCHAR noiseFloorOffset; /*!< */
154 SCHAR noiseMaxLevel; /*!< */
155 SBR_STEREO_MODE stereoMode; /*!< */
156 UCHAR freqScale; /*!< */
157 } sbrTuningTable_t;
158
159 typedef struct sbrConfiguration
160 {
161 /*
162 core coder dependent configurations
163 */
164 CODEC_PARAM codecSettings; /*!< Core coder settings. To be set from core coder. */
165 INT SendHeaderDataTime; /*!< SBR header send update frequency in ms. */
166 INT useWaveCoding; /*!< Flag: usage of wavecoding tool. */
167 INT crcSbr; /*!< Flag: usage of SBR-CRC. */
168 INT dynBwSupported; /*!< Flag: support for dynamic bandwidth in this combination. */
169 INT parametricCoding; /*!< Flag: usage of parametric coding tool. */
170 INT downSampleFactor; /*!< Sampling rate relation between the SBR and the core encoder. */
171 int freq_res_fixfix[3]; /*!< Frequency resolution of envelopes in frame class FIXFIX
172 0=1 Env; 1=2 Env; 2=4 Env; */
173 /*
174 core coder dependent tuning parameters
175 */
176 INT tran_thr; /*!< SBR transient detector threshold (* 100). */
177 INT noiseFloorOffset; /*!< Noise floor offset. */
178 UINT useSpeechConfig; /*!< Flag: adapt tuning parameters according to speech. */
179
180
181
182 /*
183 core coder independent configurations
184 */
185 INT sbrFrameSize; /*!< SBR frame size in samples. Will be calculated from core coder settings. */
186 INT sbr_data_extra; /*!< Flag usage of data extra. */
187 INT amp_res; /*!< Amplitude resolution. */
188 INT ana_max_level; /*!< Noise insertion maximum level. */
189 INT tran_fc; /*!< Transient detector start frequency. */
190 INT tran_det_mode; /*!< Transient detector mode. */
191 INT spread; /*!< Flag: usage of SBR spread. */
192 INT stat; /*!< Flag: usage of static framing. */
193 INT e; /*!< Number of envelopes when static framing is chosen. */
194 SBR_STEREO_MODE stereoMode; /*!< SBR stereo mode. */
195 INT deltaTAcrossFrames; /*!< Flag: allow time-delta coding. */
196 FIXP_DBL dF_edge_1stEnv; /*!< Extra fraction delta-F coding is allowed to be more expensive. */
197 FIXP_DBL dF_edge_incr; /*!< Increment dF_edge_1stEnv this much if dT-coding was used this frame. */
198 INT sbr_invf_mode; /*!< Inverse filtering mode. */
199 INT sbr_xpos_mode; /*!< Transposer mode. */
200 INT sbr_xpos_ctrl; /*!< Transposer control. */
201 INT sbr_xpos_level; /*!< Transposer 3rd order level. */
202 INT startFreq; /*!< The start frequency table index. */
203 INT stopFreq; /*!< The stop frequency table index. */
204 INT useSaPan; /*!< Flag: usage of SAPAN stereo. */
205 INT dynBwEnabled; /*!< Flag: usage of dynamic bandwidth. */
206 INT bParametricStereo; /*!< Flag: usage of parametric stereo coding tool. */
207
208 /*
209 header_extra1 configuration
210 */
211 UCHAR freqScale; /*!< Frequency grouping. */
212 INT alterScale; /*!< Scale resolution. */
213 INT sbr_noise_bands; /*!< Number of noise bands. */
214
215
216 /*
217 header_extra2 configuration
218 */
219 INT sbr_limiter_bands; /*!< Number of limiter bands. */
220 INT sbr_limiter_gains; /*!< Gain of limiter. */
221 INT sbr_interpol_freq; /*!< Flag: use interpolation in freq. direction. */
222 INT sbr_smoothing_length; /*!< Flag: choose length 4 or 0 (=on, off). */
223 UCHAR init_amp_res_FF;
224 } sbrConfiguration, *sbrConfigurationPtr ;
225
226 typedef struct SBR_CONFIG_DATA
227 {
228 UINT sbrSyntaxFlags; /**< SBR syntax flags derived from AOT. */
229 INT nChannels; /**< Number of channels. */
230
231 INT nSfb[2]; /**< Number of SBR scalefactor bands for LO_RES and HI_RES (?) */
232 INT num_Master; /**< Number of elements in v_k_master. */
233 INT sampleFreq; /**< SBR sampling frequency. */
234 INT frameSize;
235 INT xOverFreq; /**< The SBR start frequency. */
236 INT dynXOverFreq; /**< Used crossover frequency when dynamic bandwidth is enabled. */
237 INT noQmfBands; /**< Number of QMF frequency bands. */
238 INT noQmfSlots; /**< Number of QMF slots. */
239
240 UCHAR *freqBandTable[2]; /**< Frequency table for low and hires, only MAX_FREQ_COEFFS/2 +1 coeefs actually needed for lowres. */
241 UCHAR *v_k_master; /**< Master BandTable where freqBandTable is derived from. */
242
243
244 SBR_STEREO_MODE stereoMode;
245 INT noEnvChannels; /**< Number of envelope channels. */
246
247 INT useWaveCoding; /**< Flag indicates whether to use wave coding at all. */
248 INT useParametricCoding; /**< Flag indicates whether to use para coding at all. */
249 INT xposCtrlSwitch; /**< Flag indicates whether to switch xpos ctrl on the fly. */
250 INT switchTransposers; /**< Flag indicates whether to switch xpos on the fly . */
251 UCHAR initAmpResFF;
252 } SBR_CONFIG_DATA, *HANDLE_SBR_CONFIG_DATA;
253
254 typedef struct {
255 MP4_ELEMENT_ID elType;
256 INT bitRate;
257 int instanceTag;
258 UCHAR fParametricStereo;
259 UCHAR nChannelsInEl;
260 UCHAR ChannelIndex[2];
261 } SBR_ELEMENT_INFO;
262
263 #ifdef __cplusplus
264 extern "C" {
265 #endif
266
267 typedef struct SBR_ENCODER *HANDLE_SBR_ENCODER;
268
269 /**
270 * \brief Get the max required input buffer size including delay balancing space
271 * for N audio channels.
272 * \param noChannels Number of audio channels.
273 * \return Max required input buffer size in bytes.
274 */
275 INT sbrEncoder_GetInBufferSize(int noChannels);
276
277 INT sbrEncoder_Open(
278 HANDLE_SBR_ENCODER *phSbrEncoder,
279 INT nElements,
280 INT nChannels,
281 INT supportPS
282 );
283
284 /**
285 * \brief Get closest working bitrate to specified desired
286 * bitrate for a single SBR element.
287 * \param bitRate The desired target bit rate
288 * \param numChannels The amount of audio channels
289 * \param coreSampleRate The sample rate of the core coder
290 * \param aot The current Audio Object Type
291 * \return Closest working bit rate to bitRate value
292 */
293 UINT sbrEncoder_LimitBitRate(UINT bitRate, UINT numChannels, UINT coreSampleRate, AUDIO_OBJECT_TYPE aot);
294
295
296 /**
297 * \brief Check whether downsampled SBR single rate is possible
298 * with given audio object type.
299 * \param aot The Audio object type.
300 * \return 0 when downsampled SBR is not possible,
301 * 1 when downsampled SBR is possible.
302 */
303 UINT sbrEncoder_IsSingleRatePossible(AUDIO_OBJECT_TYPE aot);
304
305 /**
306 * \brief Initialize SBR Encoder instance.
307 * \param phSbrEncoder Pointer to a SBR Encoder instance.
308 * \param elInfo Structure that describes the element/channel arrangement.
309 * \param noElements Amount of elements described in elInfo.
310 * \param inputBuffer Pointer to the encoder audio buffer
311 * \param bandwidth Returns the core audio encoder bandwidth (output)
312 * \param bufferOffset Returns the offset for the audio input data in order to do delay balancing.
313 * \param numChannels Input: Encoder input channels. output: core encoder channels.
314 * \param sampleRate Input: Encoder samplerate. output core encoder samplerate.
315 * \param downSampleFactor Input: Relation between SBR and core coder sampling rate;
316 * \param frameLength Input: Encoder frameLength. output core encoder frameLength.
317 * \param aot Input: Desired AOT. output AOT to be used after parameter checking.
318 * \param delay Input: core encoder delay. Output: total delay because of SBR.
319 * \param transformFactor The core encoder transform factor (blockswitching).
320 * \param headerPeriod Repetition rate of the SBR header:
321 * - (-1) means intern configuration.
322 * - (1-10) corresponds to header repetition rate in frames.
323 * \return 0 on success, and non-zero if failed.
324 */
325 INT sbrEncoder_Init(
326 HANDLE_SBR_ENCODER hSbrEncoder,
327 SBR_ELEMENT_INFO elInfo[(8)],
328 int noElements,
329 INT_PCM *inputBuffer,
330 INT *coreBandwidth,
331 INT *inputBufferOffset,
332 INT *numChannels,
333 INT *sampleRate,
334 UINT *downSampleFactor,
335 INT *frameLength,
336 AUDIO_OBJECT_TYPE aot,
337 int *delay,
338 int transformFactor,
339 const int headerPeriod,
340 ULONG statesInitFlag
341 );
342
343 /**
344 * \brief Do delay line buffers housekeeping. To be called after each encoded audio frame.
345 * \param hEnvEnc SBR Encoder handle.
346 * \param timeBuffer Pointer to the encoder audio buffer.
347 * \return 0 on success, and non-zero if failed.
348 */
349 INT sbrEncoder_UpdateBuffers(HANDLE_SBR_ENCODER hEnvEnc,
350 INT_PCM *timeBuffer
351 );
352
353 /**
354 * \brief Close SBR encoder instance.
355 * \param phEbrEncoder Handle of SBR encoder instance to be closed.
356 * \return void
357 */
358 void sbrEncoder_Close(HANDLE_SBR_ENCODER *phEbrEncoder);
359
360 /**
361 * \brief Encode SBR data of one complete audio frame.
362 * \param hEnvEncoder Handle of SBR encoder instance.
363 * \param samples Time samples, always interleaved.
364 * \param timeInStride Channel stride factor of samples buffer.
365 * \param sbrDataBits Size of SBR payload in bits.
366 * \param sbrData SBR payload.
367 * \return 0 on success, and non-zero if failed.
368 */
369 INT sbrEncoder_EncodeFrame(HANDLE_SBR_ENCODER hEnvEncoder,
370 INT_PCM *samples,
371 UINT timeInStride,
372 UINT sbrDataBits[(8)],
373 UCHAR sbrData[(8)][MAX_PAYLOAD_SIZE]
374 );
375
376 /**
377 * \brief Write SBR headers of one SBR element.
378 * \param sbrEncoder Handle of the SBR encoder instance.
379 * \param hBs Handle of bit stream handle to write SBR header to.
380 * \param element_index Index of the SBR element which header should be written.
381 * \param fSendHeaders Flag indicating that the SBR encoder should send more headers in the SBR payload or not.
382 * \return void
383 */
384 void sbrEncoder_GetHeader(HANDLE_SBR_ENCODER sbrEncoder,
385 HANDLE_FDK_BITSTREAM hBs,
386 INT element_index,
387 int fSendHeaders);
388
389 /**
390 * \brief SBR encoder bitrate estimation.
391 * \param hSbrEncoder SBR encoder handle.
392 * \return Estimated bitrate.
393 */
394 INT sbrEncoder_GetEstimateBitrate(HANDLE_SBR_ENCODER hSbrEncoder);
395
396
397 /**
398 * \brief Delay between input data and downsampled output data.
399 * \param hSbrEncoder SBR encoder handle.
400 * \return Delay.
401 */
402 INT sbrEncoder_GetInputDataDelay(HANDLE_SBR_ENCODER hSbrEncoder);
403
404 /**
405 * \brief Get decoder library version info.
406 * \param info Pointer to an allocated LIB_INFO struct, where library info is written to.
407 * \return 0 on sucess.
408 */
409 INT sbrEncoder_GetLibInfo(LIB_INFO *info);
410
411 void sbrPrintRAM(void);
412
413 void sbrPrintROM(void);
414
415 #ifdef __cplusplus
416 }
417 #endif
418
419 #endif /* ifndef __SBR_MAIN_H */