1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3 
4 © Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6 
7  1.    INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12 
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18 
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28 
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33 
34 2.    COPYRIGHT LICENSE
35 
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39 
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42 
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48 
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51 
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54 
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60 
61 3.    NO PATENT LICENSE
62 
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67 
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70 
71 4.    DISCLAIMER
72 
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83 
84 5.    CONTACT INFORMATION
85 
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90 
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94 
95 /******************* MPEG transport format encoder library *********************
96 
97    Author(s):   Manuel Jander
98 
99    Description: MPEG Transport encode
100 
101 *******************************************************************************/
102 
103 #ifndef TPENC_LIB_H
104 #define TPENC_LIB_H
105 
106 #include "tp_data.h"
107 #include "FDK_bitstream.h"
108 
109 #define TRANSPORTENC_INBUF_SIZE 8192
110 
111 typedef enum {
112   TRANSPORTENC_OK = 0,             /*!< All fine.             */
113   TRANSPORTENC_NO_MEM,             /*!< Out of memory.             */
114   TRANSPORTENC_UNKOWN_ERROR = 1,   /*!< Unknown error (embarrasing).   */
115   TRANSPORTENC_INVALID_PARAMETER,  /*!< An invalid parameter was passed to a
116                                       function .          */
117   TRANSPORTENC_UNSUPPORTED_FORMAT, /*!< Unsupported transport format. */
118   TRANSPORTENC_NOT_ENOUGH_BITS,    /*!< Out of bits. Provide more bits and try
119                                       again.            */
120 
121   TRANSPORTENC_INVALID_CONFIG, /*!< Error in configuration.               */
122   TRANSPORTENC_LATM_INVALID_NR_OF_SUBFRAMES, /*!< LATM: number of subframes out
123                                                 of range.                  */
124   TRANSPORTENC_LOAS_NOT_AVAILABLE, /*!< LOAS format not supported.           */
125   TRANSPORTENC_INVALID_LATM_ALIGNMENT, /*!< AudioMuxElement length not aligned
126                                           to 1 byte.            */
127 
128   TRANSPORTENC_INVALID_TRANSMISSION_FRAME_LENGTH, /*!< Invalid transmission
129                                                      frame length (< 0). */
130   TRANSPORTENC_INVALID_CELP_FRAME_LENGTH, /*!< Invalid CELP frame length found
131                                              (>= 62).                 */
132   TRANSPORTENC_INVALID_FRAME_BITS, /*!< Frame bits is not 40 and not 80. */
133   TRANSPORTENC_INVALID_AOT,        /*!< Unknown AOT found.        */
134   TRANSPORTENC_INVALID_AU_LENGTH   /*!< Invalid Access Unit length (not
135                                       byte-aligned).           */
136 
137 } TRANSPORTENC_ERROR;
138 
139 typedef struct TRANSPORTENC *HANDLE_TRANSPORTENC;
140 
141 /**
142  * \brief             Determine a reasonable channel configuration on the basis
143  * of channel_mode.
144  * \param noChannels  Number of audio channels.
145  * \return            CHANNEL_MODE value that matches the given amount of audio
146  * channels.
147  */
148 CHANNEL_MODE transportEnc_GetChannelMode(int noChannels);
149 
150 /**
151  * \brief                Register SBR heaqder writer callback.
152  * \param hTp            Handle of transport decoder.
153  * \param cbUpdateConfig Pointer to a callback function to handle SBR header
154  * writing.
155  * \param user_data      void pointer for user data passed to the callback as
156  * first parameter.
157  * \return               0 on success.
158  */
159 int transportEnc_RegisterSbrCallback(HANDLE_TRANSPORTENC hTpEnc,
160                                      const cbSbr_t cbSbr, void *user_data);
161 
162 /**
163  * \brief                Register USAC SC writer callback.
164  * \param hTp            Handle of transport decoder.
165  * \param cbUpdateConfig Pointer to a callback function to handle USAC
166  * SCwriting.
167  * \param user_data      void pointer for user data passed to the callback as
168  * first parameter.
169  * \return               0 on success.
170  */
171 int transportEnc_RegisterUsacCallback(HANDLE_TRANSPORTENC hTpEnc,
172                                       const cbUsac_t cbUsac, void *user_data);
173 
174 /**
175  * \brief                Register SSC writer callback.
176  * \param hTp            Handle of transport decoder.
177  * \param cbUpdateConfig Pointer to a callback function to handle SSC writing.
178  * \param user_data      void pointer for user data passed to the callback as
179  * first parameter.
180  * \return               0 on success.
181  */
182 int transportEnc_RegisterSscCallback(HANDLE_TRANSPORTENC hTpEnc,
183                                      const cbSsc_t cbSsc, void *user_data);
184 
185 /**
186  * \brief         Write ASC from given parameters.
187  * \param asc     A HANDLE_FDK_BITSTREAM where the ASC is written to.
188  * \param config  Structure containing the codec configuration settings.
189  * \param cb callback information structure.
190  * \return        0 on success.
191  */
192 int transportEnc_writeASC(HANDLE_FDK_BITSTREAM asc, CODER_CONFIG *config,
193                           CSTpCallBacks *cb);
194 
195 /* Defintion of flags that can be passed to transportEnc_Open() */
196 #define TP_FLAG_MPEG4 1     /** MPEG4 (instead of MPEG2) */
197 #define TP_FLAG_LATM_AMV 2  /** LATM AudioMuxVersion  */
198 #define TP_FLAG_LATM_AMVA 4 /** LATM AudioMuxVersionA */
199 
200 /**
201  * \brief               Allocate transport encoder.
202  * \param phTpEnc       Pointer to transport encoder handle.
203  * \return              Error code.
204  */
205 TRANSPORTENC_ERROR transportEnc_Open(HANDLE_TRANSPORTENC *phTpEnc);
206 
207 /**
208  * \brief               Init transport encoder.
209  * \param bsBuffer      Pointer to transport encoder.
210  * \param bsBuffer      Pointer to bitstream buffer.
211  * \param bsBufferSize  Size in bytes of bsBuffer.
212  * \param transportFmt  Format of the transport to be written.
213  * \param config        Pointer to a valid CODER_CONFIG struct.
214  * \param flags         Transport encoder flags.
215  * \return              Error code.
216  */
217 TRANSPORTENC_ERROR transportEnc_Init(HANDLE_TRANSPORTENC hTpEnc,
218                                      UCHAR *bsBuffer, INT bsBufferSize,
219                                      TRANSPORT_TYPE transportFmt,
220                                      CODER_CONFIG *config, UINT flags);
221 
222 /**
223  * \brief               Write additional bits in transport encoder.
224  * \param config        Pointer to a valid CODER_CONFIG struct.
225  * \param nBits         Number of additional bits.
226  * \return              Error code.
227  */
228 TRANSPORTENC_ERROR transportEnc_AddOtherDataBits(HANDLE_TRANSPORTENC hTpEnc,
229                                                  const int nBits);
230 
231 /**
232  * \brief      Get transport encoder bitstream.
233  * \param hTp  Pointer to a transport encoder handle.
234  * \return     The handle to the requested FDK bitstream.
235  */
236 HANDLE_FDK_BITSTREAM transportEnc_GetBitstream(HANDLE_TRANSPORTENC hTp);
237 
238 /**
239  * \brief         Get amount of bits required by the transport headers.
240  * \param hTp     Handle of transport encoder.
241  * \param auBits  Amount of payload bits required for the current subframe.
242  * \return        Error code.
243  */
244 INT transportEnc_GetStaticBits(HANDLE_TRANSPORTENC hTp, int auBits);
245 
246 /**
247  * \brief       Close transport encoder. This function assures that all
248  * allocated memory is freed.
249  * \param phTp  Pointer to a previously allocated transport encoder handle.
250  */
251 void transportEnc_Close(HANDLE_TRANSPORTENC *phTp);
252 
253 /**
254  * \brief                       Write one access unit.
255  * \param hTp                   Handle of transport encoder.
256  * \param total_bits            Amount of total access unit bits.
257  * \param bufferFullness        Value of current buffer fullness in bits.
258  * \param noConsideredChannels  Number of bitrate wise considered channels (all
259  * minus LFE channels).
260  * \return                      Error code.
261  */
262 TRANSPORTENC_ERROR transportEnc_WriteAccessUnit(HANDLE_TRANSPORTENC hTp,
263                                                 INT total_bits,
264                                                 int bufferFullness,
265                                                 int noConsideredChannels);
266 
267 /**
268  * \brief        Inform the transportEnc layer that writing of access unit has
269  * finished. This function is required to be called when the encoder has
270  * finished writing one Access one Access Unit for bitstream
271  * housekeeping.
272  * \param hTp    Transport handle.
273  * \param pBits  Pointer to an int, where the current amount of frame bits is
274  * passed and where the current amount of subframe bits is returned.
275  *
276  * OR:  This integer is modified by the amount of extra bit alignment that may
277  * occurr.
278  *
279  * \return       Error code.
280  */
281 TRANSPORTENC_ERROR transportEnc_EndAccessUnit(HANDLE_TRANSPORTENC hTp,
282                                               int *pBits);
283 
284 /*
285  * \brief         Get a payload frame.
286  * \param hTpEnc  Transport encoder handle.
287  * \param nBytes  Pointer to an int to hold the frame size in bytes. Returns
288  * zero if currently there is no complete frame for output (number of sub frames
289  * > 1).
290  * \return        Error code.
291  */
292 TRANSPORTENC_ERROR transportEnc_GetFrame(HANDLE_TRANSPORTENC hTpEnc,
293                                          int *nbytes);
294 
295 /* ADTS CRC support */
296 
297 /**
298  * \brief         Set current bitstream position as start of a new data region.
299  * \param hTpEnc  Transport encoder handle.
300  * \param mBits   Size in bits of the data region. Set to 0 if it should not be
301  * of a fixed size.
302  * \return        Data region ID, which should be used when calling
303  * transportEnc_CrcEndReg().
304  */
305 int transportEnc_CrcStartReg(HANDLE_TRANSPORTENC hTpEnc, int mBits);
306 
307 /**
308  * \brief         Set end of data region.
309  * \param hTpEnc  Transport encoder handle.
310  * \param reg     Data region ID, opbtained from transportEnc_CrcStartReg().
311  * \return        void
312  */
313 void transportEnc_CrcEndReg(HANDLE_TRANSPORTENC hTpEnc, int reg);
314 
315 /**
316  * \brief             Get AudioSpecificConfig or StreamMuxConfig from transport
317  * encoder handle and write it to dataBuffer.
318  * \param hTpEnc      Transport encoder handle.
319  * \param cc          Pointer to the current and valid configuration contained
320  * in a CODER_CONFIG struct.
321  * \param dataBuffer  Bitbuffer holding binary configuration.
322  * \param confType    Pointer to an UINT where the configuration type is
323  * returned (0:ASC, 1:SMC).
324  * \return            Error code.
325  */
326 TRANSPORTENC_ERROR transportEnc_GetConf(HANDLE_TRANSPORTENC hTpEnc,
327                                         CODER_CONFIG *cc,
328                                         FDK_BITSTREAM *dataBuffer,
329                                         UINT *confType);
330 
331 /**
332  * \brief       Get information (version among other things) of the transport
333  * encoder library.
334  * \param info  Pointer to an allocated LIB_INFO struct.
335  * \return      Error code.
336  */
337 TRANSPORTENC_ERROR transportEnc_GetLibInfo(LIB_INFO *info);
338 
339 #endif /* #ifndef TPENC_LIB_H */
340