Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libMpegTPDec / include / tpdec_lib.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 /************************** MPEG-4 Transport Decoder ***********************
85
86 Author(s): Manuel Jander
87 Description: MPEG Transport decoder
88
89 ******************************************************************************/
90
91 #ifndef __TPDEC_LIB_H__
92 #define __TPDEC_LIB_H__
93
94 #include "tp_data.h"
95
96 #include "FDK_bitstream.h"
97
98 #define TRANSPORTDEC_INBUF_SIZE ( 8192 ) /*!< Size is in bytes.
99 Set the transport input buffer size carefully and
100 assure that it fulfills the requirements of the
101 supported transport format(s). */
102
103 typedef enum {
104 TRANSPORTDEC_OK = 0, /*!< All fine. */
105
106 /* Synchronization errors. Wait for new input data and try again. */
107 tpdec_sync_error_start = 0x100,
108 TRANSPORTDEC_NOT_ENOUGH_BITS, /*!< Out of bits. Provide more bits and try again. */
109 TRANSPORTDEC_SYNC_ERROR, /*!< No sync was found or sync got lost. Keep trying. */
110 tpdec_sync_error_end,
111
112 /* Decode errors. Mostly caused due to bit errors. */
113 tpdec_decode_error_start = 0x400,
114 TRANSPORTDEC_PARSE_ERROR, /*!< Bitstream data showed inconsistencies (wrong syntax). */
115 TRANSPORTDEC_UNSUPPORTED_FORMAT, /*!< Unsupported format or feature found in the bitstream data. */
116 TRANSPORTDEC_CRC_ERROR, /*!< CRC error encountered in bitstream data. */
117 tpdec_decode_error_end,
118
119 /* Fatal errors. Stop immediately on one of these errors! */
120 tpdec_fatal_error_start = 0x200,
121 TRANSPORTDEC_UNKOWN_ERROR, /*!< An unknown error occured. */
122 TRANSPORTDEC_INVALID_PARAMETER, /*!< An invalid parameter was passed to a function. */
123 TRANSPORTDEC_NEED_TO_RESTART, /*!< The decoder needs to be restarted, since the requiered
124 configuration change cannot be performed. */
125 tpdec_fatal_error_end
126
127 } TRANSPORTDEC_ERROR;
128
129
130 /** Macro to identify decode errors. */
131 #define TPDEC_IS_DECODE_ERROR(err) ( ((err>=tpdec_decode_error_start) && (err<=tpdec_decode_error_end)) ? 1 : 0)
132 /** Macro to identify fatal errors. */
133 #define TPDEC_IS_FATAL_ERROR(err) ( ((err>=tpdec_fatal_error_start) && (err<=tpdec_fatal_error_end)) ? 1 : 0)
134
135
136 /**
137 * \brief Parameter identifiers for transportDec_SetParam()
138 */
139 typedef enum {
140 TPDEC_PARAM_MINIMIZE_DELAY = 1, /** Delay minimization strategy. 0: none, 1: discard as many frames as possible. */
141 TPDEC_PARAM_EARLY_CONFIG, /** Enable early config discovery. */
142 TPDEC_PARAM_IGNORE_BUFFERFULLNESS, /** Ignore buffer fullness. */
143 TPDEC_PARAM_SET_BITRATE, /** Set average bit rate for bit stream interruption frame misses estimation. */
144 TPDEC_PARAM_RESET, /** Reset transport decoder instance status. */
145 TPDEC_PARAM_BURST_PERIOD /** Set data reception burst period in mili seconds. */
146 } TPDEC_PARAM;
147
148 /* ISO/IEC 14496-3 4.4.1.1 Table 4.2 Program config element */
149 #define PC_FSB_CHANNELS_MAX 16 /* Front/Side/Back channels */
150 #define PC_LFE_CHANNELS_MAX 4
151 #define PC_ASSOCDATA_MAX 8
152 #define PC_CCEL_MAX 16 /* CC elements */
153 #define PC_COMMENTLENGTH 256
154
155
156 /*!
157 \brief Reset Program Config Element.
158 \param pPce Program Config Element structure.
159 \return void
160 */
161 void CProgramConfig_Reset ( CProgramConfig *pPce );
162
163 /*!
164 \brief Initialize Program Config Element.
165 \param pPce Program Config Element structure.
166 \return void
167 */
168 void CProgramConfig_Init ( CProgramConfig *pPce );
169
170 /*!
171 \brief Inquire state of present Program Config Element structure.
172 \param pPce Program Config Element structure.
173 \return 1 if the PCE structure is filled correct,
174 0 if no valid PCE present.
175 */
176 int CProgramConfig_IsValid ( const CProgramConfig *pPce );
177
178 #ifdef TP_PCE_ENABLE
179 /*!
180 \brief Read Program Config Element.
181 \param pPce Program Config Element structure.
182 \param bs Bitstream buffer to read from.
183 \param alignAnchor Align bitstream to alignAnchor bits after all read operations.
184 \return void
185 */
186 void CProgramConfig_Read ( CProgramConfig *pPce,
187 HANDLE_FDK_BITSTREAM bs,
188 UINT alignAnchor );
189
190 /*!
191 \brief Compare two Program Config Elements.
192 \param pPce1 Pointer to first Program Config Element structure.
193 \param pPce2 Pointer to second Program Config Element structure.
194 \return -1 if PCEs are completely different,
195 0 if PCEs are completely equal,
196 1 if PCEs are different but have the same channel config,
197 2 if PCEs have different channel config but same number of channels.
198 */
199 int CProgramConfig_Compare ( const CProgramConfig * const pPce1,
200 const CProgramConfig * const pPce2 );
201
202 /*!
203 \brief Get a Program Config Element that matches the predefined MPEG-4 channel configurations 1-14.
204 \param pPce Program Config Element structure.
205 \param channelConfig MPEG-4 channel configuration.
206 \return void
207 */
208 void CProgramConfig_GetDefault ( CProgramConfig *pPce,
209 const UINT channelConfig );
210 #endif /* TP_PCE_ENABLE */
211
212 /**
213 * \brief Lookup and verify a given element. The decoder calls this
214 * method with every new element ID found in the bitstream.
215 *
216 * \param pPce A valid Program config structure.
217 * \param tag Tag of the current element to be looked up.
218 * \param channelIdx The current channel count of the decoder parser.
219 * \param chMapping Array to store the canonical channel mapping indexes.
220 * \param chType Array to store the audio channel type.
221 * \param chIndex Array to store the individual audio channel type index.
222 * \param elMapping Pointer where the canonical element index is stored.
223 * \param elType The element id of the current element to be looked up.
224 *
225 * \return Non-zero if the element belongs to the current program, zero
226 * if it does not.
227 */
228 int CProgramConfig_LookupElement(
229 CProgramConfig *pPce,
230 UINT channelConfig,
231 const UINT tag,
232 const UINT channelIdx,
233 UCHAR chMapping[],
234 AUDIO_CHANNEL_TYPE chType[],
235 UCHAR chIndex[],
236 UCHAR *elMapping,
237 MP4_ELEMENT_ID elList[],
238 MP4_ELEMENT_ID elType
239 );
240
241 /**
242 * \brief Get table of elements in canonical order.
243 * \param pPce A valid program config structure.
244 * \param table An array where the element IDs are stored.
245 * \return Total element count including all SCE, CPE and LFE.
246 */
247 int CProgramConfig_GetElementTable( const CProgramConfig *pPce,
248 MP4_ELEMENT_ID table[],
249 const INT elListSize );
250
251 /**
252 * \brief Initialize a given AudioSpecificConfig structure.
253 * \param pAsc A pointer to an allocated CSAudioSpecificConfig struct.
254 * \return void
255 */
256 void AudioSpecificConfig_Init(CSAudioSpecificConfig *pAsc);
257
258 /**
259 * \brief Parse a AudioSpecificConfig from a given bitstream handle.
260 *
261 * \param pAsc A pointer to an allocated CSAudioSpecificConfig struct.
262 * \param hBs Bitstream handle.
263 * \param fExplicitBackwardCompatible Do explicit backward compatibility parsing if set (flag).
264 * \param cb pointer to structure holding callback information
265 *
266 * \return Total element count including all SCE, CPE and LFE.
267 */
268 TRANSPORTDEC_ERROR AudioSpecificConfig_Parse(
269 CSAudioSpecificConfig *pAsc,
270 HANDLE_FDK_BITSTREAM hBs,
271 int fExplicitBackwardCompatible,
272 CSTpCallBacks *cb
273 );
274
275 /* CELP stuff */
276 enum {
277 MPE = 0,
278 RPE = 1,
279 fs8KHz = 0,
280 fs16KHz = 1
281 };
282
283 /* Defintion of flags that can be passed to transportDecOpen() */
284 #define TP_FLAG_MPEG4 1
285
286 /* Capability flags */
287 #define CAPF_TPDEC_ADIF 0x00001000 /**< Flag indicating support for ADIF transport format. */
288 #define CAPF_TPDEC_ADTS 0x00002000 /**< Flag indicating support for ADTS transport format. */
289 #define CAPF_TPDEC_LOAS 0x00004000 /**< Flag indicating support for LOAS transport format. */
290 #define CAPF_TPDEC_LATM 0x00008000 /**< Flag indicating support for LATM transport format. */
291 #define CAPF_TPDEC_RAWPACKETS 0x00010000 /**< Flag indicating support for raw packets transport format. */
292
293 typedef struct TRANSPORTDEC *HANDLE_TRANSPORTDEC;
294
295
296 /**
297 * \brief Configure Transport Decoder via a binary coded AudioSpecificConfig or StreamMuxConfig.
298 * The previously requested configuration callback will be called as well. The buffer conf
299 * must containt a SMC in case of LOAS/LATM transport format, and an ASC elseways.
300 *
301 * \param hTp Handle of a transport decoder.
302 * \param conf UCHAR buffer of the binary coded config (ASC or SMC).
303 * \param length The length in bytes of the conf buffer.
304 *
305 * \return Error code.
306 */
307 TRANSPORTDEC_ERROR transportDec_OutOfBandConfig( const HANDLE_TRANSPORTDEC hTp,
308 UCHAR *conf,
309 const UINT length,
310 const UINT layer );
311
312 /**
313 * \brief Open Transport medium for reading.
314 *
315 * \param transportDecFmt Format of the transport decoder medium to be accessed.
316 * \param flags Transport decoder flags. Currently only TP_FLAG_MPEG4, which signals a
317 * MPEG4 capable decoder (relevant for ADTS only).
318 *
319 * \return A pointer to a valid and allocated HANDLE_TRANSPORTDEC or a null pointer on failure.
320 */
321 HANDLE_TRANSPORTDEC transportDec_Open( TRANSPORT_TYPE transportDecFmt,
322 const UINT flags );
323
324 /**
325 * \brief Register configuration change callback.
326 * \param hTp Handle of transport decoder.
327 * \param cbUpdateConfig Pointer to a callback function to handle audio config changes.
328 * \param user_data void pointer for user data passed to the callback as first parameter.
329 * \return 0 on success.
330 */
331 int transportDec_RegisterAscCallback (
332 HANDLE_TRANSPORTDEC hTp,
333 const cbUpdateConfig_t cbUpdateConfig,
334 void* user_data );
335
336 /**
337 * \brief Register SSC parser callback.
338 * \param hTp Handle of transport decoder.
339 * \param cbUpdateConfig Pointer to a callback function to handle SSC parsing.
340 * \param user_data void pointer for user data passed to the callback as first parameter.
341 * \return 0 on success.
342 */
343 int transportDec_RegisterSscCallback (
344 HANDLE_TRANSPORTDEC hTp,
345 const cbSsc_t cbSscParse,
346 void* user_data );
347
348 /**
349 * \brief Register SBR header parser callback.
350 * \param hTp Handle of transport decoder.
351 * \param cbUpdateConfig Pointer to a callback function to handle SBR header parsing.
352 * \param user_data void pointer for user data passed to the callback as first parameter.
353 * \return 0 on success.
354 */
355 int transportDec_RegisterSbrCallback( HANDLE_TRANSPORTDEC hTpDec, const cbSbr_t cbSbr, void* user_data);
356
357 /**
358 * \brief Fill internal input buffer with bitstream data from the external input buffer.
359 * The function only copies such data as long as the decoder-internal input buffer is not full.
360 * So it grabs whatever it can from pBuffer and returns information (bytesValid) so that at a
361 * subsequent call of %transportDec_FillData(), the right position in pBuffer can be determined to
362 * grab the next data.
363 *
364 * \param hTp Handle of transportDec.
365 * \param pBuffer Pointer to external input buffer.
366 * \param bufferSize Size of external input buffer. This argument is required because decoder-internally
367 * we need the information to calculate the offset to pBuffer, where the next
368 * available data is, which is then fed into the decoder-internal buffer (as much
369 * as possible). Our example framework implementation fills the buffer at pBuffer
370 * again, once it contains no available valid bytes anymore (meaning bytesValid equal 0).
371 * \param bytesValid Number of bitstream bytes in the external bitstream buffer that have not yet been
372 * copied into the decoder's internal bitstream buffer by calling this function.
373 * The value is updated according to the amount of newly copied bytes.
374 * \param layer The layer the bitstream belongs to.
375 * \return Error code.
376 */
377 TRANSPORTDEC_ERROR transportDec_FillData(
378 const HANDLE_TRANSPORTDEC hTp,
379 UCHAR *pBuffer,
380 const UINT bufferSize,
381 UINT *pBytesValid,
382 const INT layer );
383
384 /**
385 * \brief Get transportDec bitstream handle.
386 * \param hTp Pointer to a transport decoder handle.
387 * \return HANDLE_FDK_BITSTREAM bitstream handle.
388 */
389 HANDLE_FDK_BITSTREAM transportDec_GetBitstream ( const HANDLE_TRANSPORTDEC hTp, const UINT layer );
390
391 /**
392 * \brief Get transport format.
393 * \param hTp Pointer to a transport decoder handle.
394 * \return The transport format.
395 */
396 TRANSPORT_TYPE transportDec_GetFormat ( const HANDLE_TRANSPORTDEC hTp );
397
398 /**
399 * \brief Get the current buffer fullness value.
400 *
401 * \param hTp Handle of a transport decoder.
402 *
403 * \return Buffer fullness
404 */
405 INT transportDec_GetBufferFullness( const HANDLE_TRANSPORTDEC hTp );
406
407 /**
408 * \brief Close and deallocate transportDec.
409 * \param phTp Pointer to a previously allocated transport decoder handle.
410 * \return void
411 */
412 void transportDec_Close ( HANDLE_TRANSPORTDEC *phTp );
413
414 /**
415 * \brief Read one access unit from the transportDec medium.
416 * \param hTp Handle of transportDec.
417 * \param length On return, this value is overwritten with the actual access unit length in bits.
418 * Set to -1 if length is unknown.
419 * \return Error code.
420 */
421 TRANSPORTDEC_ERROR transportDec_ReadAccessUnit ( const HANDLE_TRANSPORTDEC hTp, const UINT layer );
422
423 /**
424 * \brief Get the remaining amount of bits of the current access unit. The result
425 * can be below zero, meaning that too many bits have been read.
426 * \param hTp Handle of transportDec.
427 * \return amount of remaining bits.
428 */
429 INT transportDec_GetAuBitsRemaining( const HANDLE_TRANSPORTDEC hTp, const UINT layer );
430
431 /**
432 * \brief Get the total amount of bits of the current access unit.
433 * \param hTp Handle of transportDec.
434 * \return amount of total bits.
435 */
436 INT transportDec_GetAuBitsTotal( const HANDLE_TRANSPORTDEC hTp, const UINT layer );
437
438 /**
439 * \brief This function is required to be called when the decoder has finished parsing
440 * one Access Unit for bitstream housekeeping.
441 * \param hTp Transport Handle.
442 * \return Error code.
443 */
444 TRANSPORTDEC_ERROR transportDec_EndAccessUnit ( const HANDLE_TRANSPORTDEC hTp );
445
446 /**
447 * \brief Obtain the amount of missing access units if applicable in case of
448 * a bit stream synchronization error. Each time transportDec_ReadAccessUnit()
449 * returns TRANSPORTDEC_SYNC_ERROR this function can be called to retrieve an estimate
450 * of the amount of missing access units. This works only in case of constant average
451 * bit rate (has to be known) and if the parameter TPDEC_PARAM_SET_BITRATE has been set
452 * accordingly.
453 * \param hTp Transport Handle.
454 * \param pNAccessUnits pointer to a memory location where the estimated lost frame count will be stored into.
455 * \return Error code.
456 */
457 TRANSPORTDEC_ERROR transportDec_GetMissingAccessUnitCount ( INT *pNAccessUnits, HANDLE_TRANSPORTDEC hTp );
458
459
460 /**
461 * \brief Set a given setting.
462 * \param hTp Transport Handle.
463 * \param param Identifier of the parameter to be changed.
464 * \param value Value for the parameter to be changed.
465 * \return Error code.
466 */
467 TRANSPORTDEC_ERROR transportDec_SetParam ( const HANDLE_TRANSPORTDEC hTp,
468 const TPDEC_PARAM param,
469 const INT value );
470
471 /**
472 * \brief Get number of subframes (for LATM or ADTS)
473 * \param hTp Transport Handle.
474 * \return Number of ADTS/LATM subframes (return 1 for all other transport types).
475 */
476 UINT transportDec_GetNrOfSubFrames(HANDLE_TRANSPORTDEC hTp);
477
478
479 /**
480 * \brief Get info structure of transport decoder library.
481 * \param info A pointer to an allocated LIB_INFO struct.
482 * \return Error code.
483 */
484 TRANSPORTDEC_ERROR transportDec_GetLibInfo( LIB_INFO *info );
485
486 /* ADTS CRC support */
487
488 /**
489 * \brief Set current bitstream position as start of a new data region.
490 * \param hTp Transport handle.
491 * \param mBits Size in bits of the data region. Set to 0 if it should not be of a fixed size.
492 * \return Data region ID, which should be used when calling transportDec_CrcEndReg().
493 */
494 int transportDec_CrcStartReg ( const HANDLE_TRANSPORTDEC hTp,
495 const INT mBits );
496
497 /**
498 * \brief Set end of data region.
499 * \param hTp Transport handle.
500 * \param reg Data region ID, opbtained from transportDec_CrcStartReg().
501 * \return void
502 */
503 void transportDec_CrcEndReg ( const HANDLE_TRANSPORTDEC hTp,
504 const INT reg );
505
506 /**
507 * \brief Calculate ADTS crc and check if it is correct. The ADTS checksum is held internally.
508 * \param hTp Transport handle.
509 * \return Return TRANSPORTDEC_OK if the CRC is ok, or error if CRC is not correct.
510 */
511 TRANSPORTDEC_ERROR transportDec_CrcCheck ( const HANDLE_TRANSPORTDEC hTp );
512
513
514 #endif /* #ifndef __TPDEC_LIB_H__ */