2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
84 /************************** MPEG-4 Transport Decoder ************************
86 Author(s): Manuel Jander
87 Description: MPEG Transport decoder
89 ******************************************************************************/
91 #include "tpdec_lib.h"
99 #include "tpdec_adts.h"
101 #include "tpdec_adif.h"
103 #include "tpdec_latm.h"
107 #define MODULE_NAME "transportDec"
117 } transportdec_parser_t
;
121 TRANSPORT_TYPE transportFmt
; /*!< MPEG4 transportDec type. */
123 CSTpCallBacks callbacks
; /*!< Struct holding callback and its data */
125 FDK_BITSTREAM bitStream
[2]; /* Bitstream reader */
126 UCHAR
*bsBuffer
; /* Internal bitstreamd data buffer (unallocated in case of TT_MP4_RAWPACKETS) */
128 transportdec_parser_t parser
; /* Format specific parser structs. */
130 CSAudioSpecificConfig asc
[(1*2)]; /* Audio specific config from the last config found. */
131 UINT globalFramePos
; /* Global transport frame reference bit position. */
132 UINT accessUnitAnchor
[2]; /* Current access unit start bit position. */
133 INT auLength
[2]; /* Length of current access unit. */
134 INT numberOfRawDataBlocks
; /* Current number of raw data blocks contained remaining from the current transport frame. */
135 UINT avgBitRate
; /* Average bit rate used for frame loss estimation. */
136 UINT lastValidBufferFullness
; /* Last valid buffer fullness value for frame loss estimation */
137 INT remainder
; /* Reminder in division during lost access unit estimation. */
138 INT missingAccessUnits
; /* Estimated missing access units. */
139 UINT burstPeriod
; /* Data burst period in mili seconds. */
140 UINT holdOffFrames
; /* Amount of frames that were already hold off due to buffer fullness condition not being met. */
141 UINT flags
; /* Flags. */
144 /* Flag bitmasks for "flags" member of struct TRANSPORTDEC */
145 #define TPDEC_SYNCOK 1
146 #define TPDEC_MINIMIZE_DELAY 2
147 #define TPDEC_IGNORE_BUFFERFULLNESS 4
148 #define TPDEC_EARLY_CONFIG 8
149 #define TPDEC_LOST_FRAMES_PENDING 16
150 #define TPDEC_CONFIG_FOUND 32
152 C_ALLOC_MEM(Ram_TransportDecoder
, TRANSPORTDEC
, 1)
153 C_ALLOC_MEM(Ram_TransportDecoderBuffer
, UCHAR
, TRANSPORTDEC_INBUF_SIZE
)
158 HANDLE_TRANSPORTDEC
transportDec_Open( const TRANSPORT_TYPE transportFmt
, const UINT flags
)
160 HANDLE_TRANSPORTDEC hInput
;
162 hInput
= GetRam_TransportDecoder(0);
163 if ( hInput
== NULL
) {
167 /* Init transportDec struct. */
168 hInput
->transportFmt
= transportFmt
;
170 switch (transportFmt
) {
176 if (flags
& TP_FLAG_MPEG4
)
177 hInput
->parser
.adts
.decoderCanDoMpeg4
= 1;
179 hInput
->parser
.adts
.decoderCanDoMpeg4
= 0;
180 adtsRead_CrcInit(&hInput
->parser
.adts
);
181 hInput
->parser
.adts
.BufferFullnesStartFlag
= 1;
182 hInput
->numberOfRawDataBlocks
= 0;
186 case TT_MP4_LATM_MCP0
:
187 case TT_MP4_LATM_MCP1
:
193 FreeRam_TransportDecoder(&hInput
);
198 if (hInput
!= NULL
) {
199 /* Create bitstream */
200 if ( TT_IS_PACKET(transportFmt
) ) {
201 hInput
->bsBuffer
= NULL
;
203 hInput
->bsBuffer
= GetRam_TransportDecoderBuffer(0);
204 if (hInput
->bsBuffer
== NULL
) {
205 transportDec_Close( &hInput
);
208 FDKinitBitStream(&hInput
->bitStream
[0], hInput
->bsBuffer
, TRANSPORTDEC_INBUF_SIZE
, 0, BS_READER
);
211 hInput
->burstPeriod
= 0;
217 TRANSPORTDEC_ERROR
transportDec_OutOfBandConfig(HANDLE_TRANSPORTDEC hTp
, UCHAR
*conf
, const UINT length
, UINT layer
)
219 TRANSPORTDEC_ERROR err
= TRANSPORTDEC_OK
;
222 HANDLE_FDK_BITSTREAM hBs
= &bs
;
224 FDKinitBitStream(hBs
, conf
, 0x10000000, length
<<3, BS_READER
);
226 int fConfigFound
= 0;
228 /* config transport decoder */
229 switch (hTp
->transportFmt
) {
230 case TT_MP4_LATM_MCP0
:
231 case TT_MP4_LATM_MCP1
:
235 return TRANSPORTDEC_INVALID_PARAMETER
;
237 CLatmDemux
*pLatmDemux
= &hTp
->parser
.latm
;
238 err
= CLatmDemux_ReadStreamMuxConfig(hBs
, pLatmDemux
, &hTp
->callbacks
, hTp
->asc
, &fConfigFound
);
239 if (err
!= TRANSPORTDEC_OK
) {
246 err
= AudioSpecificConfig_Parse(&hTp
->asc
[layer
], hBs
, 1, &hTp
->callbacks
);
247 if (err
== TRANSPORTDEC_OK
) {
250 errC
= hTp
->callbacks
.cbUpdateConfig(hTp
->callbacks
.cbUpdateConfigData
, &hTp
->asc
[layer
]);
252 err
= TRANSPORTDEC_PARSE_ERROR
;
258 if (err
== TRANSPORTDEC_OK
&& fConfigFound
) {
259 hTp
->flags
|= TPDEC_CONFIG_FOUND
;
265 int transportDec_RegisterAscCallback( HANDLE_TRANSPORTDEC hTpDec
, const cbUpdateConfig_t cbUpdateConfig
, void* user_data
)
267 if (hTpDec
== NULL
) {
270 hTpDec
->callbacks
.cbUpdateConfig
= cbUpdateConfig
;
271 hTpDec
->callbacks
.cbUpdateConfigData
= user_data
;
275 int transportDec_RegisterSscCallback( HANDLE_TRANSPORTDEC hTpDec
, const cbSsc_t cbSsc
, void* user_data
)
277 if (hTpDec
== NULL
) {
280 hTpDec
->callbacks
.cbSsc
= cbSsc
;
281 hTpDec
->callbacks
.cbSscData
= user_data
;
285 int transportDec_RegisterSbrCallback( HANDLE_TRANSPORTDEC hTpDec
, const cbSbr_t cbSbr
, void* user_data
)
287 if (hTpDec
== NULL
) {
290 hTpDec
->callbacks
.cbSbr
= cbSbr
;
291 hTpDec
->callbacks
.cbSbrData
= user_data
;
295 TRANSPORTDEC_ERROR
transportDec_FillData(
296 const HANDLE_TRANSPORTDEC hTp
,
298 const UINT bufferSize
,
302 HANDLE_FDK_BITSTREAM hBs
;
306 return TRANSPORTDEC_INVALID_PARAMETER
;
309 if (*pBytesValid
== 0) {
311 return TRANSPORTDEC_OK
;
314 /* set bitbuffer shortcut */
315 hBs
= &hTp
->bitStream
[layer
];
317 if ( TT_IS_PACKET(hTp
->transportFmt
) ) {
318 if (hTp
->numberOfRawDataBlocks
== 0) {
319 /* For packet based transport, pass input buffer to bitbuffer without copying the data.
320 Unfortunately we do not know the actual buffer size. And the FDK bit buffer implementation
321 needs a number 2^x. So we assume the maximum of 48 channels with 6144 bits per channel
322 and round it up to the next power of 2 => 65536 bytes */
323 FDKinitBitStream(hBs
, pBuffer
, 0x10000, (*pBytesValid
)<<3, BS_READER
);
327 /* ... else feed bitbuffer with new stream data (append). */
328 if (hTp
->numberOfRawDataBlocks
<= 0) {
329 FDKfeedBuffer (hBs
, pBuffer
, bufferSize
, pBytesValid
) ;
333 return TRANSPORTDEC_OK
;
336 HANDLE_FDK_BITSTREAM
transportDec_GetBitstream( const HANDLE_TRANSPORTDEC hTp
, const UINT layer
)
338 return &hTp
->bitStream
[layer
];
341 TRANSPORT_TYPE
transportDec_GetFormat( const HANDLE_TRANSPORTDEC hTp
)
343 return hTp
->transportFmt
;
346 INT
transportDec_GetBufferFullness( const HANDLE_TRANSPORTDEC hTp
)
348 INT bufferFullness
= -1;
350 switch (hTp
->transportFmt
) {
352 if (hTp
->parser
.adts
.bs
.adts_fullness
!= 0x7ff) {
353 bufferFullness
= hTp
->parser
.adts
.bs
.frame_length
*8 + hTp
->parser
.adts
.bs
.adts_fullness
* 32 * getNumberOfEffectiveChannels(hTp
->parser
.adts
.bs
.channel_config
);
357 case TT_MP4_LATM_MCP0
:
358 case TT_MP4_LATM_MCP1
:
359 if (hTp
->parser
.latm
.m_linfo
[0][0].m_bufferFullness
!= 0xff) {
360 bufferFullness
= hTp
->parser
.latm
.m_linfo
[0][0].m_bufferFullness
;
367 return bufferFullness
;
371 * \brief adjust bit stream position and the end of an access unit.
372 * \param hTp transport decoder handle.
373 * \return error code.
376 TRANSPORTDEC_ERROR
transportDec_AdjustEndOfAccessUnit(HANDLE_TRANSPORTDEC hTp
)
378 HANDLE_FDK_BITSTREAM hBs
= &hTp
->bitStream
[0];
379 TRANSPORTDEC_ERROR err
= TRANSPORTDEC_OK
;
381 switch (hTp
->transportFmt
) {
383 case TT_MP4_LATM_MCP0
:
384 case TT_MP4_LATM_MCP1
:
385 if ( hTp
->numberOfRawDataBlocks
== 0 )
387 /* Do byte align at the end of AudioMuxElement. */
388 FDKbyteAlign(hBs
, hTp
->globalFramePos
);
390 /* Check global frame length */
391 if (hTp
->transportFmt
== TT_MP4_LOAS
&& hTp
->parser
.latm
.m_audioMuxLengthBytes
> 0)
395 loasOffset
= (hTp
->parser
.latm
.m_audioMuxLengthBytes
*8 + FDKgetValidBits(hBs
)) - hTp
->globalFramePos
;
396 if (loasOffset
!= 0) {
397 FDKpushBiDirectional(hBs
, loasOffset
);
398 /* For ELD and other payloads there is an unknown amount of padding, so ignore unread bits, but
399 throw an error only if too many bits where read. */
400 if (loasOffset
< 0) {
401 err
= TRANSPORTDEC_PARSE_ERROR
;
409 if (hTp
->parser
.adts
.bs
.protection_absent
== 0)
413 /* Calculate offset to end of AU */
414 offset
= hTp
->parser
.adts
.rawDataBlockDist
[hTp
->parser
.adts
.bs
.num_raw_blocks
-hTp
->numberOfRawDataBlocks
]<<3;
415 /* CAUTION: The PCE (if available) is declared to be a part of the header! */
416 offset
-= hTp
->accessUnitAnchor
[0] - FDKgetValidBits(hBs
) + 16 + hTp
->parser
.adts
.bs
.num_pce_bits
;
417 FDKpushBiDirectional(hBs
, offset
);
419 if (hTp
->parser
.adts
.bs
.num_raw_blocks
> 0 && hTp
->parser
.adts
.bs
.protection_absent
== 0) {
420 /* Note this CRC read currently happens twice because of transportDec_CrcCheck() */
421 hTp
->parser
.adts
.crcReadValue
= FDKreadBits(hBs
, 16);
423 if ( hTp
->numberOfRawDataBlocks
== 0 )
425 /* Check global frame length */
426 if (hTp
->parser
.adts
.bs
.protection_absent
== 0)
430 offset
= (hTp
->parser
.adts
.bs
.frame_length
*8 - ADTS_SYNCLENGTH
+ FDKgetValidBits(hBs
)) - hTp
->globalFramePos
;
432 FDKpushBiDirectional(hBs
, offset
);
447 * \brief Determine additional buffer fullness contraint due to burst data reception.
448 * The parameter TPDEC_PARAM_BURSTPERIOD must have been set as a precondition.
449 * \param hTp transport decoder handle.
450 * \param bufferFullness the buffer fullness value of the first frame to be decoded.
451 * \param bitsAvail the amount of available bits at the end of the first frame to be decoded.
455 TRANSPORTDEC_ERROR
additionalHoldOffNeeded(
456 HANDLE_TRANSPORTDEC hTp
,
461 INT checkLengthBits
, avgBitsPerFrame
;
462 INT maxAU
; /* maximum number of frames per Master Frame */
463 INT samplesPerFrame
= hTp
->asc
->m_samplesPerFrame
;
464 INT samplingFrequency
= (INT
)hTp
->asc
->m_samplingFrequency
;
466 if ( (hTp
->avgBitRate
== 0) || (hTp
->burstPeriod
== 0) ) {
467 return TRANSPORTDEC_OK
;
469 if ( (samplesPerFrame
== 0 ) || (samplingFrequency
== 0) ) {
470 return TRANSPORTDEC_NOT_ENOUGH_BITS
;
473 /* One Master Frame is sent every hTp->burstPeriod ms */
474 maxAU
= hTp
->burstPeriod
* samplingFrequency
+ (samplesPerFrame
*1000 - 1);
475 maxAU
= maxAU
/ (samplesPerFrame
*1000);
476 /* Subtract number of frames which were already held off. */
477 maxAU
-= hTp
->holdOffFrames
;
479 avgBitsPerFrame
= hTp
->avgBitRate
* samplesPerFrame
+ (samplingFrequency
-1);
480 avgBitsPerFrame
= avgBitsPerFrame
/ samplingFrequency
;
482 /* Consider worst case of bufferFullness quantization. */
483 switch (hTp
->transportFmt
) {
487 case TT_MP4_LATM_MCP0
:
488 case TT_MP4_LATM_MCP1
:
489 bufferFullness
+= 31;
495 checkLengthBits
= bufferFullness
+ (maxAU
-1)*avgBitsPerFrame
;
497 /* Check if buffer is big enough to fullfill buffer fullness condition */
498 if ( (checkLengthBits
/*+headerBits*/) > ((TRANSPORTDEC_INBUF_SIZE
<<3)-7) ) {
499 return TRANSPORTDEC_SYNC_ERROR
;
502 if ( bitsAvail
< checkLengthBits
) {
503 return TRANSPORTDEC_NOT_ENOUGH_BITS
;
506 return TRANSPORTDEC_OK
;
510 static TRANSPORTDEC_ERROR
transportDec_readHeader(
511 HANDLE_TRANSPORTDEC hTp
,
512 HANDLE_FDK_BITSTREAM hBs
,
514 int ignoreBufferFullness
,
515 int *pRawDataBlockLength
,
516 int *pfTraverseMoreFrames
,
517 int *pSyncLayerFrameBits
,
522 TRANSPORTDEC_ERROR err
= TRANSPORTDEC_OK
;
523 int rawDataBlockLength
= *pRawDataBlockLength
;
524 int fTraverseMoreFrames
= (pfTraverseMoreFrames
!= NULL
) ? *pfTraverseMoreFrames
: 0;
525 int syncLayerFrameBits
= (pSyncLayerFrameBits
!= NULL
) ? *pSyncLayerFrameBits
: 0;
526 int fConfigFound
= (pfConfigFound
!= NULL
) ? *pfConfigFound
: 0;
529 startPos
= FDKgetValidBits(hBs
);
531 switch (hTp
->transportFmt
) {
533 if (hTp
->numberOfRawDataBlocks
<= 0)
537 hTp
->globalFramePos
= FDKgetValidBits(hBs
);
539 /* Parse ADTS header */
540 err
= adtsRead_DecodeHeader( &hTp
->parser
.adts
, &hTp
->asc
[0], hBs
, ignoreBufferFullness
);
541 if (err
!= TRANSPORTDEC_OK
) {
542 if (err
!= TRANSPORTDEC_NOT_ENOUGH_BITS
) {
543 err
= TRANSPORTDEC_SYNC_ERROR
;
546 errC
= hTp
->callbacks
.cbUpdateConfig(hTp
->callbacks
.cbUpdateConfigData
, &hTp
->asc
[0]);
548 if (errC
== TRANSPORTDEC_NEED_TO_RESTART
) {
549 err
= TRANSPORTDEC_NEED_TO_RESTART
;
552 err
= TRANSPORTDEC_SYNC_ERROR
;
556 hTp
->numberOfRawDataBlocks
= hTp
->parser
.adts
.bs
.num_raw_blocks
+1;
561 /* Reset CRC because the next bits are the beginning of a raw_data_block() */
562 FDKcrcReset(&hTp
->parser
.adts
.crcInfo
);
563 hTp
->parser
.adts
.bs
.num_pce_bits
= 0;
565 if (err
== TRANSPORTDEC_OK
) {
566 hTp
->numberOfRawDataBlocks
--;
567 rawDataBlockLength
= adtsRead_GetRawDataBlockLength(&hTp
->parser
.adts
, (hTp
->parser
.adts
.bs
.num_raw_blocks
-hTp
->numberOfRawDataBlocks
));
568 if (rawDataBlockLength
<= 0) {
569 /* No further frame traversal possible. */
570 fTraverseMoreFrames
= 0;
572 syncLayerFrameBits
= (hTp
->parser
.adts
.bs
.frame_length
<<3) - (startPos
- FDKgetValidBits(hBs
)) - syncLength
;
573 if (syncLayerFrameBits
<= 0) {
574 err
= TRANSPORTDEC_SYNC_ERROR
;
577 hTp
->numberOfRawDataBlocks
= 0;
581 if (hTp
->numberOfRawDataBlocks
<= 0)
583 syncLayerFrameBits
= FDKreadBits(hBs
, 13);
584 hTp
->parser
.latm
.m_audioMuxLengthBytes
= syncLayerFrameBits
;
585 syncLayerFrameBits
<<= 3;
587 case TT_MP4_LATM_MCP1
:
588 case TT_MP4_LATM_MCP0
:
589 if (hTp
->numberOfRawDataBlocks
<= 0)
591 hTp
->globalFramePos
= FDKgetValidBits(hBs
);
593 err
= CLatmDemux_Read(
600 ignoreBufferFullness
);
602 if (err
!= TRANSPORTDEC_OK
) {
603 if (err
!= TRANSPORTDEC_NOT_ENOUGH_BITS
) {
604 err
= TRANSPORTDEC_SYNC_ERROR
;
607 hTp
->numberOfRawDataBlocks
= CLatmDemux_GetNrOfSubFrames(&hTp
->parser
.latm
);
608 if (hTp
->transportFmt
== TT_MP4_LOAS
) {
609 syncLayerFrameBits
-= startPos
- FDKgetValidBits(hBs
) - (13);
613 err
= CLatmDemux_ReadPayloadLengthInfo(hBs
, &hTp
->parser
.latm
);
614 if (err
!= TRANSPORTDEC_OK
) {
615 err
= TRANSPORTDEC_SYNC_ERROR
;
618 if (err
== TRANSPORTDEC_OK
) {
619 rawDataBlockLength
= CLatmDemux_GetFrameLengthInBits(&hTp
->parser
.latm
);
620 hTp
->numberOfRawDataBlocks
--;
622 hTp
->numberOfRawDataBlocks
= 0;
627 syncLayerFrameBits
= 0;
634 *pRawDataBlockLength
= rawDataBlockLength
;
636 if (pHeaderBits
!= NULL
) {
637 *pHeaderBits
+= startPos
- (INT
)FDKgetValidBits(hBs
);
639 if (pfConfigFound
!= NULL
) {
640 *pfConfigFound
= fConfigFound
;
643 if (pfTraverseMoreFrames
!= NULL
) {
644 *pfTraverseMoreFrames
= fTraverseMoreFrames
;
646 if (pSyncLayerFrameBits
!= NULL
) {
647 *pSyncLayerFrameBits
= syncLayerFrameBits
;
649 if (pfConfigFound
!= NULL
) {
650 *pfConfigFound
= fConfigFound
;
656 /* How many bits to advance for synchronization search. */
657 #define TPDEC_SYNCSKIP 8
660 TRANSPORTDEC_ERROR
synchronization(
661 HANDLE_TRANSPORTDEC hTp
,
665 TRANSPORTDEC_ERROR err
= TRANSPORTDEC_OK
, errFirstFrame
= TRANSPORTDEC_OK
;
666 HANDLE_FDK_BITSTREAM hBs
= &hTp
->bitStream
[0];
668 INT syncLayerFrameBits
= 0; /* Length of sync layer frame (i.e. LOAS) */
669 INT rawDataBlockLength
= 0, rawDataBlockLengthPrevious
;
671 INT headerBits
= 0, headerBitsFirstFrame
= 0, headerBitsPrevious
;
672 INT numFramesTraversed
= 0, fTraverseMoreFrames
, fConfigFound
= (hTp
->flags
& TPDEC_CONFIG_FOUND
), startPosFirstFrame
= -1;
673 INT numRawDataBlocksFirstFrame
= 0, numRawDataBlocksPrevious
, globalFramePosFirstFrame
= 0, rawDataBlockLengthFirstFrame
= 0;
674 INT ignoreBufferFullness
= hTp
->flags
& (TPDEC_LOST_FRAMES_PENDING
|TPDEC_IGNORE_BUFFERFULLNESS
|TPDEC_SYNCOK
);
676 /* Synch parameters */
677 INT syncLength
; /* Length of sync word in bits */
678 UINT syncWord
; /* Sync word to be found */
679 UINT syncMask
; /* Mask for sync word (for adding one bit, so comprising one bit less) */
680 C_ALLOC_SCRATCH_START(contextFirstFrame
, transportdec_parser_t
, 1);
682 totalBits
= (INT
)FDKgetValidBits(hBs
);
684 if (totalBits
<= 0) {
685 err
= TRANSPORTDEC_NOT_ENOUGH_BITS
;
689 fTraverseMoreFrames
= (hTp
->flags
& (TPDEC_MINIMIZE_DELAY
|TPDEC_EARLY_CONFIG
)) && ! (hTp
->flags
& TPDEC_SYNCOK
);
691 /* Set transport specific sync parameters */
692 switch (hTp
->transportFmt
) {
694 syncWord
= ADTS_SYNCWORD
;
695 syncLength
= ADTS_SYNCLENGTH
;
707 syncMask
= (1<<syncLength
)-1;
710 INT bitsAvail
= 0; /* Bits available in bitstream buffer */
711 INT checkLengthBits
; /* Helper to check remaining bits and buffer boundaries */
712 UINT synch
; /* Current sync word read from bitstream */
714 headerBitsPrevious
= headerBits
;
716 bitsAvail
= (INT
)FDKgetValidBits(hBs
);
718 if (hTp
->numberOfRawDataBlocks
== 0) {
719 /* search synchword */
721 FDK_ASSERT( (bitsAvail
% TPDEC_SYNCSKIP
) == 0);
723 if ((bitsAvail
-syncLength
) < TPDEC_SYNCSKIP
) {
724 err
= TRANSPORTDEC_NOT_ENOUGH_BITS
;
728 synch
= FDKreadBits(hBs
, syncLength
);
730 if ( !(hTp
->flags
& TPDEC_SYNCOK
) ) {
731 for (; (bitsAvail
-syncLength
) >= TPDEC_SYNCSKIP
; bitsAvail
-=TPDEC_SYNCSKIP
) {
732 if (synch
== syncWord
) {
735 synch
= ((synch
<< TPDEC_SYNCSKIP
) & syncMask
) | FDKreadBits(hBs
, TPDEC_SYNCSKIP
);
738 if (synch
!= syncWord
) {
739 /* No correct syncword found. */
740 err
= TRANSPORTDEC_SYNC_ERROR
;
742 err
= TRANSPORTDEC_OK
;
744 headerBits
= syncLength
;
750 /* Save previous raw data block data */
751 rawDataBlockLengthPrevious
= rawDataBlockLength
;
752 numRawDataBlocksPrevious
= hTp
->numberOfRawDataBlocks
;
754 /* Parse transport header (raw data block granularity) */
756 if (err
== TRANSPORTDEC_OK
)
758 err
= transportDec_readHeader(
762 ignoreBufferFullness
,
764 &fTraverseMoreFrames
,
771 bitsAvail
-= headerBits
;
773 checkLengthBits
= syncLayerFrameBits
;
775 /* Check if the whole frame would fit the bitstream buffer */
776 if (err
== TRANSPORTDEC_OK
) {
777 if ( (checkLengthBits
+headerBits
) > ((TRANSPORTDEC_INBUF_SIZE
<<3)-7) ) {
778 /* We assume that the size of the transport bit buffer has been
779 chosen to meet all system requirements, thus this condition
780 is considered a synchronisation error. */
781 err
= TRANSPORTDEC_SYNC_ERROR
;
783 if ( bitsAvail
< checkLengthBits
) {
784 err
= TRANSPORTDEC_NOT_ENOUGH_BITS
;
789 if (err
== TRANSPORTDEC_NOT_ENOUGH_BITS
) {
794 if (err
== TRANSPORTDEC_SYNC_ERROR
) {
797 /* Enforce re-sync of transport headers. */
798 hTp
->numberOfRawDataBlocks
= 0;
800 /* Ensure that the bit amount lands at a multiple of TPDEC_SYNCSKIP */
801 bits
= (bitsAvail
+ headerBits
) % TPDEC_SYNCSKIP
;
802 /* Rewind - TPDEC_SYNCSKIP, in order to look for a synch one bit ahead next time. */
803 FDKpushBiDirectional(hBs
, -(headerBits
- TPDEC_SYNCSKIP
) + bits
);
804 bitsAvail
+= headerBits
- TPDEC_SYNCSKIP
- bits
;
808 /* Frame traversal */
809 if ( fTraverseMoreFrames
)
811 /* Save parser context for early config discovery "rewind all frames" */
812 if ( (hTp
->flags
& TPDEC_EARLY_CONFIG
) && !(hTp
->flags
& TPDEC_MINIMIZE_DELAY
))
814 /* ignore buffer fullness if just traversing additional frames for ECD */
815 ignoreBufferFullness
= 1;
817 /* Save context in order to return later */
818 if ( err
== TRANSPORTDEC_OK
&& startPosFirstFrame
== -1 ) {
819 startPosFirstFrame
= FDKgetValidBits(hBs
);
820 numRawDataBlocksFirstFrame
= hTp
->numberOfRawDataBlocks
;
821 globalFramePosFirstFrame
= hTp
->globalFramePos
;
822 rawDataBlockLengthFirstFrame
= rawDataBlockLength
;
823 headerBitsFirstFrame
= headerBits
;
825 FDKmemcpy(contextFirstFrame
, &hTp
->parser
, sizeof(transportdec_parser_t
));
828 /* Break when config was found or it is not possible anymore to find a config */
829 if (startPosFirstFrame
!= -1 && (fConfigFound
|| err
!= TRANSPORTDEC_OK
))
831 /* In case of ECD and sync error, do not rewind anywhere. */
832 if (err
== TRANSPORTDEC_SYNC_ERROR
)
834 startPosFirstFrame
= -1;
836 numFramesTraversed
= 0;
842 if (err
== TRANSPORTDEC_OK
) {
843 FDKpushFor(hBs
, rawDataBlockLength
);
844 bitsAvail
-= rawDataBlockLength
;
845 numFramesTraversed
++;
846 /* Ignore error here itentionally. */
847 transportDec_AdjustEndOfAccessUnit(hTp
);
850 } while ( fTraverseMoreFrames
|| (err
== TRANSPORTDEC_SYNC_ERROR
&& !(hTp
->flags
& TPDEC_SYNCOK
)));
852 /* Restore context in case of ECD frame traversal */
853 if ( startPosFirstFrame
!= -1 && (fConfigFound
|| err
!= TRANSPORTDEC_OK
) ) {
854 FDKpushBiDirectional(hBs
, FDKgetValidBits(hBs
) - startPosFirstFrame
);
855 FDKmemcpy(&hTp
->parser
, contextFirstFrame
, sizeof(transportdec_parser_t
));
856 hTp
->numberOfRawDataBlocks
= numRawDataBlocksFirstFrame
;
857 hTp
->globalFramePos
= globalFramePosFirstFrame
;
858 rawDataBlockLength
= rawDataBlockLengthFirstFrame
;
859 headerBits
= headerBitsFirstFrame
;
861 numFramesTraversed
= 0;
864 /* Additional burst data mode buffer fullness check. */
865 if ( !(hTp
->flags
& (TPDEC_LOST_FRAMES_PENDING
|TPDEC_IGNORE_BUFFERFULLNESS
|TPDEC_SYNCOK
)) && err
== TRANSPORTDEC_OK
) {
866 err
= additionalHoldOffNeeded(hTp
, transportDec_GetBufferFullness(hTp
), FDKgetValidBits(hBs
) - syncLayerFrameBits
);
867 if (err
== TRANSPORTDEC_NOT_ENOUGH_BITS
) {
868 hTp
->holdOffFrames
++;
872 /* Rewind for retry because of not enough bits */
873 if (err
== TRANSPORTDEC_NOT_ENOUGH_BITS
) {
874 FDKpushBack(hBs
, headerBits
);
878 /* reset hold off frame counter */
879 hTp
->holdOffFrames
= 0;
882 /* Return to last good frame in case of frame traversal but not ECD. */
883 if (numFramesTraversed
> 0) {
884 FDKpushBack(hBs
, rawDataBlockLengthPrevious
);
885 if (err
!= TRANSPORTDEC_OK
) {
886 hTp
->numberOfRawDataBlocks
= numRawDataBlocksPrevious
;
887 headerBits
= headerBitsPrevious
;
889 err
= TRANSPORTDEC_OK
;
893 hTp
->auLength
[0] = rawDataBlockLength
;
895 /* Detect pointless TRANSPORTDEC_NOT_ENOUGH_BITS error case, were the bit buffer is already full,
896 or no new burst packet fits. Recover by advancing the bit buffer. */
897 if ( (TRANSPORTDEC_NOT_ENOUGH_BITS
== err
) && (FDKgetValidBits(hBs
) >= ((TRANSPORTDEC_INBUF_SIZE
*8 - ((hTp
->avgBitRate
*hTp
->burstPeriod
)/1000)) - 7)) )
899 FDKpushFor(hBs
, TPDEC_SYNCSKIP
);
900 err
= TRANSPORTDEC_SYNC_ERROR
;
903 if (err
== TRANSPORTDEC_OK
) {
904 hTp
->flags
|= TPDEC_SYNCOK
;
908 hTp
->flags
|= TPDEC_CONFIG_FOUND
;
911 if (pHeaderBits
!= NULL
) {
912 *pHeaderBits
= headerBits
;
915 if (err
== TRANSPORTDEC_SYNC_ERROR
) {
916 hTp
->flags
&= ~TPDEC_SYNCOK
;
919 C_ALLOC_SCRATCH_END(contextFirstFrame
, transportdec_parser_t
, 1);
925 * \brief Synchronize to stream and estimate the amount of missing access units due
926 * to a current synchronization error in case of constant average bit rate.
929 TRANSPORTDEC_ERROR
transportDec_readStream ( HANDLE_TRANSPORTDEC hTp
, const UINT layer
)
932 TRANSPORTDEC_ERROR error
= TRANSPORTDEC_OK
;
933 HANDLE_FDK_BITSTREAM hBs
= &hTp
->bitStream
[layer
];
936 INT bitDistance
, bfDelta
;
938 /* Obtain distance to next synch word */
939 bitDistance
= FDKgetValidBits(hBs
);
940 error
= synchronization(hTp
, &headerBits
);
941 bitDistance
-= FDKgetValidBits(hBs
);
944 FDK_ASSERT(bitDistance
>= 0);
946 if (error
== TRANSPORTDEC_SYNC_ERROR
|| (hTp
->flags
& TPDEC_LOST_FRAMES_PENDING
))
948 /* Check if estimating lost access units is feasible. */
949 if (hTp
->avgBitRate
> 0 && hTp
->asc
[0].m_samplesPerFrame
> 0 && hTp
->asc
[0].m_samplingFrequency
> 0)
951 if (error
== TRANSPORTDEC_OK
)
955 aj
= transportDec_GetBufferFullness(hTp
);
961 /* sync was ok: last of a series of bad access units. */
962 hTp
->flags
&= ~TPDEC_LOST_FRAMES_PENDING
;
963 /* Add up bitDistance until end of the current frame. Later we substract
964 this frame from the grand total, since this current successfully synchronized
965 frame should not be skipped of course; but it must be accounted into the
966 bufferfulness math. */
967 bitDistance
+= hTp
->auLength
[0];
969 if ( !(hTp
->flags
& TPDEC_LOST_FRAMES_PENDING
) ) {
970 /* sync not ok: one of many bad access units. */
971 hTp
->flags
|= TPDEC_LOST_FRAMES_PENDING
;
972 bfDelta
= - (INT
)hTp
->lastValidBufferFullness
;
981 /* Obtain estimate of number of lost frames */
982 num
= hTp
->asc
[0].m_samplingFrequency
* (bfDelta
+ bitDistance
) + hTp
->remainder
;
983 denom
= hTp
->avgBitRate
* hTp
->asc
[0].m_samplesPerFrame
;
986 hTp
->remainder
= num
% denom
;
988 hTp
->remainder
= num
;
991 if (error
== TRANSPORTDEC_OK
)
993 /* Final adjustment of remainder, taken -1 into account because current
994 frame should not be skipped, thus substract -1 or do nothing instead
995 of +1-1 accordingly. */
996 if ( (denom
- hTp
->remainder
) >= hTp
->remainder
) {
1001 /* There was one frame too much concealed, so unfortunately we will have to skip one good frame. */
1002 transportDec_EndAccessUnit(hTp
);
1003 error
= synchronization(hTp
, &headerBits
);
1006 FDKprintf("ERROR: Bufferfullness accounting failed. remainder=%d, nAU=%d\n", hTp
->remainder
, nAU
);
1010 /* Enforce last missed frames to be concealed. */
1012 FDKpushBack(hBs
, headerBits
);
1019 /* Be sure that lost frames are handled correctly. This is necessary due to some
1020 sync error sequences where later it turns out that there is not enough data, but
1021 the bits upto the sync word are discarded, thus causing a value of nAU > 0 */
1023 error
= TRANSPORTDEC_SYNC_ERROR
;
1026 hTp
->missingAccessUnits
= nAU
;
1031 /* returns error code */
1032 TRANSPORTDEC_ERROR
transportDec_ReadAccessUnit( const HANDLE_TRANSPORTDEC hTp
, const UINT layer
)
1034 TRANSPORTDEC_ERROR err
= TRANSPORTDEC_OK
;
1035 HANDLE_FDK_BITSTREAM hBs
;
1038 return TRANSPORTDEC_INVALID_PARAMETER
;
1041 hBs
= &hTp
->bitStream
[layer
];
1043 if ((INT
)FDKgetValidBits(hBs
) <= 0) {
1044 err
= TRANSPORTDEC_NOT_ENOUGH_BITS
;
1047 switch (hTp
->transportFmt
) {
1050 /* Read header if not already done */
1051 if (!(hTp
->flags
& TPDEC_CONFIG_FOUND
))
1053 CProgramConfig
*pce
;
1055 AudioSpecificConfig_Init(&hTp
->asc
[0]);
1056 pce
= &hTp
->asc
[0].m_progrConfigElement
;
1057 err
= adifRead_DecodeHeader(&hTp
->parser
.adif
, pce
, hBs
);
1061 /* Map adif header to ASC */
1062 hTp
->asc
[0].m_aot
= (AUDIO_OBJECT_TYPE
)(pce
->Profile
+ 1);
1063 hTp
->asc
[0].m_samplingFrequencyIndex
= pce
->SamplingFrequencyIndex
;
1064 hTp
->asc
[0].m_samplingFrequency
= SamplingRateTable
[pce
->SamplingFrequencyIndex
];
1065 hTp
->asc
[0].m_channelConfiguration
= 0;
1066 hTp
->asc
[0].m_samplesPerFrame
= 1024;
1067 hTp
->avgBitRate
= hTp
->parser
.adif
.BitRate
;
1069 /* Call callback to decoder. */
1073 errC
= hTp
->callbacks
.cbUpdateConfig(hTp
->callbacks
.cbUpdateConfigData
, &hTp
->asc
[0]);
1075 hTp
->flags
|= TPDEC_CONFIG_FOUND
;
1077 err
= TRANSPORTDEC_PARSE_ERROR
;
1082 hTp
->auLength
[layer
] = -1; /* Access Unit data length is unknown. */
1086 /* One Access Unit was filled into buffer.
1087 So get the length out of the buffer. */
1088 hTp
->auLength
[layer
] = FDKgetValidBits(hBs
);
1089 hTp
->flags
|= TPDEC_SYNCOK
;
1092 case TT_MP4_LATM_MCP0
:
1093 case TT_MP4_LATM_MCP1
:
1095 int fConfigFound
= hTp
->flags
& TPDEC_CONFIG_FOUND
;
1096 err
= transportDec_readHeader(hTp
, hBs
, 0, 1, &hTp
->auLength
[layer
], NULL
, NULL
, &fConfigFound
, NULL
);
1098 hTp
->flags
|= TPDEC_CONFIG_FOUND
;
1106 err
= transportDec_readStream(hTp
, layer
);
1110 err
= TRANSPORTDEC_UNSUPPORTED_FORMAT
;
1114 if (err
== TRANSPORTDEC_OK
) {
1115 hTp
->accessUnitAnchor
[layer
] = FDKgetValidBits(hBs
);
1117 hTp
->accessUnitAnchor
[layer
] = 0;
1124 INT
transportDec_GetAuBitsRemaining( const HANDLE_TRANSPORTDEC hTp
, const UINT layer
)
1128 if (hTp
->accessUnitAnchor
[layer
] > 0 && hTp
->auLength
[layer
] > 0) {
1129 bits
= hTp
->auLength
[layer
] - (hTp
->accessUnitAnchor
[layer
] - FDKgetValidBits(&hTp
->bitStream
[layer
]));
1131 bits
= FDKgetValidBits(&hTp
->bitStream
[layer
]);
1137 INT
transportDec_GetAuBitsTotal( const HANDLE_TRANSPORTDEC hTp
, const UINT layer
)
1139 return hTp
->auLength
[layer
];
1142 TRANSPORTDEC_ERROR
transportDec_GetMissingAccessUnitCount ( INT
*pNAccessUnits
, HANDLE_TRANSPORTDEC hTp
)
1144 *pNAccessUnits
= hTp
->missingAccessUnits
;
1146 return TRANSPORTDEC_OK
;
1149 /* Inform the transportDec layer that reading of access unit has finished. */
1150 TRANSPORTDEC_ERROR
transportDec_EndAccessUnit(HANDLE_TRANSPORTDEC hTp
)
1152 TRANSPORTDEC_ERROR err
= TRANSPORTDEC_OK
;
1155 err
= transportDec_AdjustEndOfAccessUnit(hTp
);
1157 switch (hTp
->transportFmt
) {
1165 TRANSPORTDEC_ERROR
transportDec_SetParam ( const HANDLE_TRANSPORTDEC hTp
,
1166 const TPDEC_PARAM param
,
1169 TRANSPORTDEC_ERROR error
= TRANSPORTDEC_OK
;
1172 case TPDEC_PARAM_MINIMIZE_DELAY
:
1174 hTp
->flags
|= TPDEC_MINIMIZE_DELAY
;
1176 hTp
->flags
&= ~TPDEC_MINIMIZE_DELAY
;
1179 case TPDEC_PARAM_EARLY_CONFIG
:
1181 hTp
->flags
|= TPDEC_EARLY_CONFIG
;
1183 hTp
->flags
&= ~TPDEC_EARLY_CONFIG
;
1186 case TPDEC_PARAM_IGNORE_BUFFERFULLNESS
:
1188 hTp
->flags
|= TPDEC_IGNORE_BUFFERFULLNESS
;
1190 hTp
->flags
&= ~TPDEC_IGNORE_BUFFERFULLNESS
;
1193 case TPDEC_PARAM_SET_BITRATE
:
1194 hTp
->avgBitRate
= value
;
1196 case TPDEC_PARAM_BURST_PERIOD
:
1197 hTp
->burstPeriod
= value
;
1199 case TPDEC_PARAM_RESET
:
1203 for (i
=0; i
<(1*2); i
++) {
1204 FDKresetBitbuffer(&hTp
->bitStream
[i
]);
1205 hTp
->auLength
[i
] = 0;
1206 hTp
->accessUnitAnchor
[i
] = 0;
1208 hTp
->flags
&= ~(TPDEC_SYNCOK
|TPDEC_LOST_FRAMES_PENDING
);
1209 if (hTp
->transportFmt
!= TT_MP4_ADIF
) {
1210 hTp
->flags
&= ~TPDEC_CONFIG_FOUND
;
1213 hTp
->avgBitRate
= 0;
1214 hTp
->missingAccessUnits
= 0;
1215 hTp
->numberOfRawDataBlocks
= 0;
1216 hTp
->globalFramePos
= 0;
1217 hTp
->holdOffFrames
= 0;
1225 UINT
transportDec_GetNrOfSubFrames(HANDLE_TRANSPORTDEC hTp
)
1227 UINT nSubFrames
= 0;
1232 if (hTp
->transportFmt
==TT_MP4_LATM_MCP1
|| hTp
->transportFmt
==TT_MP4_LATM_MCP0
|| hTp
->transportFmt
==TT_MP4_LOAS
)
1233 nSubFrames
= CLatmDemux_GetNrOfSubFrames(&hTp
->parser
.latm
);
1234 else if (hTp
->transportFmt
==TT_MP4_ADTS
)
1235 nSubFrames
= hTp
->parser
.adts
.bs
.num_raw_blocks
;
1240 void transportDec_Close(HANDLE_TRANSPORTDEC
*phTp
)
1244 if (*phTp
!= NULL
) {
1245 if ( ! TT_IS_PACKET((*phTp
)->transportFmt
) ) {
1246 FreeRam_TransportDecoderBuffer(&(*phTp
)->bsBuffer
);
1248 if (*phTp
!= NULL
) {
1249 FreeRam_TransportDecoder(phTp
);
1255 TRANSPORTDEC_ERROR
transportDec_GetLibInfo( LIB_INFO
*info
)
1260 return TRANSPORTDEC_UNKOWN_ERROR
;
1263 /* search for next free tab */
1264 for (i
= 0; i
< FDK_MODULE_LAST
; i
++) {
1265 if (info
[i
].module_id
== FDK_NONE
) break;
1267 if (i
== FDK_MODULE_LAST
) return TRANSPORTDEC_UNKOWN_ERROR
;
1270 info
->module_id
= FDK_TPDEC
;
1271 info
->build_date
= __DATE__
;
1272 info
->build_time
= __TIME__
;
1273 info
->title
= TP_LIB_TITLE
;
1274 info
->version
= LIB_VERSION(TP_LIB_VL0
, TP_LIB_VL1
, TP_LIB_VL2
);
1275 LIB_VERSION_STRING(info
);
1284 return TRANSPORTDEC_OK
; /* FDKERR_NOERROR; */
1288 int transportDec_CrcStartReg(HANDLE_TRANSPORTDEC pTp
, INT mBits
)
1290 switch (pTp
->transportFmt
) {
1292 return adtsRead_CrcStartReg(&pTp
->parser
.adts
, &pTp
->bitStream
[0], mBits
);
1298 void transportDec_CrcEndReg(HANDLE_TRANSPORTDEC pTp
, INT reg
)
1300 switch (pTp
->transportFmt
) {
1302 adtsRead_CrcEndReg(&pTp
->parser
.adts
, &pTp
->bitStream
[0], reg
);
1309 TRANSPORTDEC_ERROR
transportDec_CrcCheck(HANDLE_TRANSPORTDEC pTp
)
1311 switch (pTp
->transportFmt
) {
1313 if ( (pTp
->parser
.adts
.bs
.num_raw_blocks
> 0) && (pTp
->parser
.adts
.bs
.protection_absent
== 0) )
1315 HANDLE_FDK_BITSTREAM hBs
= &pTp
->bitStream
[0];
1317 transportDec_AdjustEndOfAccessUnit(pTp
);
1319 return adtsRead_CrcCheck(&pTp
->parser
.adts
);
1321 return TRANSPORTDEC_OK
;