/src/aac/libMpegTPEnc/src/tpenc_adts.cpp
Line | Count | Source (jump to first uncovered line) |
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): Alex Groeschel |
98 | | |
99 | | Description: ADTS Transport Headers support |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "tpenc_adts.h" |
104 | | |
105 | | #include "tpenc_lib.h" |
106 | | #include "tpenc_asc.h" |
107 | | |
108 | | int adtsWrite_CrcStartReg( |
109 | | HANDLE_ADTS pAdts, /*!< pointer to adts stucture */ |
110 | | HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */ |
111 | | int mBits /*!< number of bits in crc region */ |
112 | 0 | ) { |
113 | 0 | if (pAdts->protection_absent) { |
114 | 0 | return 0; |
115 | 0 | } |
116 | 0 | return (FDKcrcStartReg(&pAdts->crcInfo, hBs, mBits)); |
117 | 0 | } |
118 | | |
119 | | void adtsWrite_CrcEndReg( |
120 | | HANDLE_ADTS pAdts, /*!< pointer to adts crc info stucture */ |
121 | | HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */ |
122 | | int reg /*!< crc region */ |
123 | 0 | ) { |
124 | 0 | if (pAdts->protection_absent == 0) { |
125 | 0 | FDKcrcEndReg(&pAdts->crcInfo, hBs, reg); |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | 0 | int adtsWrite_GetHeaderBits(HANDLE_ADTS hAdts) { |
130 | 0 | int bits = 0; |
131 | |
|
132 | 0 | if (hAdts->currentBlock == 0) { |
133 | | /* Static and variable header bits */ |
134 | 0 | bits = 56; |
135 | 0 | if (!hAdts->protection_absent) { |
136 | | /* Add header/ single raw data block CRC bits */ |
137 | 0 | bits += 16; |
138 | 0 | if (hAdts->num_raw_blocks > 0) { |
139 | | /* Add bits of raw data block position markers */ |
140 | 0 | bits += (hAdts->num_raw_blocks) * 16; |
141 | 0 | } |
142 | 0 | } |
143 | 0 | } |
144 | 0 | if (!hAdts->protection_absent && hAdts->num_raw_blocks > 0) { |
145 | | /* Add raw data block CRC bits. Not really part of the header, put they |
146 | | * cause bit overhead to be accounted. */ |
147 | 0 | bits += 16; |
148 | 0 | } |
149 | |
|
150 | 0 | hAdts->headerBits = bits; |
151 | |
|
152 | 0 | return bits; |
153 | 0 | } |
154 | | |
155 | 0 | INT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config) { |
156 | | /* Sanity checks */ |
157 | 0 | if (config->nSubFrames < 1 || config->nSubFrames > 4 || |
158 | 0 | (int)config->aot > 4 || (int)config->aot < 1) { |
159 | 0 | return -1; |
160 | 0 | } |
161 | | |
162 | | /* fixed header */ |
163 | 0 | if (config->flags & CC_MPEG_ID) { |
164 | 0 | hAdts->mpeg_id = 0; /* MPEG 4 */ |
165 | 0 | } else { |
166 | 0 | hAdts->mpeg_id = 1; /* MPEG 2 */ |
167 | 0 | } |
168 | 0 | hAdts->layer = 0; |
169 | 0 | hAdts->protection_absent = !(config->flags & CC_PROTECTION); |
170 | 0 | hAdts->profile = ((int)config->aot) - 1; |
171 | 0 | hAdts->sample_freq_index = getSamplingRateIndex(config->samplingRate, 4); |
172 | 0 | hAdts->sample_freq = config->samplingRate; |
173 | 0 | hAdts->private_bit = 0; |
174 | 0 | hAdts->channel_mode = config->channelMode; |
175 | 0 | hAdts->original = 0; |
176 | 0 | hAdts->home = 0; |
177 | | /* variable header */ |
178 | 0 | hAdts->copyright_id = 0; |
179 | 0 | hAdts->copyright_start = 0; |
180 | |
|
181 | 0 | hAdts->num_raw_blocks = config->nSubFrames - 1; /* 0 means 1 raw data block */ |
182 | |
|
183 | 0 | hAdts->channel_config_zero = config->channelConfigZero; |
184 | |
|
185 | 0 | FDKcrcInit(&hAdts->crcInfo, 0x8005, 0xFFFF, 16); |
186 | |
|
187 | 0 | hAdts->currentBlock = 0; |
188 | |
|
189 | 0 | return 0; |
190 | 0 | } |
191 | | |
192 | | int adtsWrite_EncodeHeader(HANDLE_ADTS hAdts, HANDLE_FDK_BITSTREAM hBitStream, |
193 | 0 | int buffer_fullness, int frame_length) { |
194 | 0 | INT crcIndex = 0; |
195 | |
|
196 | 0 | hAdts->headerBits = adtsWrite_GetHeaderBits(hAdts); |
197 | |
|
198 | 0 | FDK_ASSERT(((frame_length + hAdts->headerBits) / 8) < 0x2000); /*13 bit*/ |
199 | 0 | FDK_ASSERT(buffer_fullness < 0x800); /* 11 bit */ |
200 | | |
201 | 0 | if (!hAdts->protection_absent) { |
202 | 0 | FDKcrcReset(&hAdts->crcInfo); |
203 | 0 | } |
204 | |
|
205 | 0 | if (hAdts->currentBlock == 0) { |
206 | 0 | FDKresetBitbuffer(hBitStream, BS_WRITER); |
207 | 0 | } |
208 | |
|
209 | 0 | hAdts->subFrameStartBit = FDKgetValidBits(hBitStream); |
210 | | |
211 | | /* Skip new header if this is raw data block 1..n */ |
212 | 0 | if (hAdts->currentBlock == 0) { |
213 | 0 | FDKresetBitbuffer(hBitStream, BS_WRITER); |
214 | |
|
215 | 0 | if (hAdts->num_raw_blocks == 0) { |
216 | 0 | crcIndex = adtsWrite_CrcStartReg(hAdts, hBitStream, 0); |
217 | 0 | } |
218 | | |
219 | | /* fixed header */ |
220 | 0 | FDKwriteBits(hBitStream, 0xFFF, 12); |
221 | 0 | FDKwriteBits(hBitStream, hAdts->mpeg_id, 1); |
222 | 0 | FDKwriteBits(hBitStream, hAdts->layer, 2); |
223 | 0 | FDKwriteBits(hBitStream, hAdts->protection_absent, 1); |
224 | 0 | FDKwriteBits(hBitStream, hAdts->profile, 2); |
225 | 0 | FDKwriteBits(hBitStream, hAdts->sample_freq_index, 4); |
226 | 0 | FDKwriteBits(hBitStream, hAdts->private_bit, 1); |
227 | 0 | FDKwriteBits( |
228 | 0 | hBitStream, |
229 | 0 | getChannelConfig(hAdts->channel_mode, hAdts->channel_config_zero), 3); |
230 | 0 | FDKwriteBits(hBitStream, hAdts->original, 1); |
231 | 0 | FDKwriteBits(hBitStream, hAdts->home, 1); |
232 | | /* variable header */ |
233 | 0 | FDKwriteBits(hBitStream, hAdts->copyright_id, 1); |
234 | 0 | FDKwriteBits(hBitStream, hAdts->copyright_start, 1); |
235 | 0 | FDKwriteBits(hBitStream, (frame_length + hAdts->headerBits) >> 3, 13); |
236 | 0 | FDKwriteBits(hBitStream, buffer_fullness, 11); |
237 | 0 | FDKwriteBits(hBitStream, hAdts->num_raw_blocks, 2); |
238 | |
|
239 | 0 | if (!hAdts->protection_absent) { |
240 | 0 | int i; |
241 | | |
242 | | /* End header CRC portion for single raw data block and write dummy zero |
243 | | * values for unknown fields. */ |
244 | 0 | if (hAdts->num_raw_blocks == 0) { |
245 | 0 | adtsWrite_CrcEndReg(hAdts, hBitStream, crcIndex); |
246 | 0 | } else { |
247 | 0 | for (i = 0; i < hAdts->num_raw_blocks; i++) { |
248 | 0 | FDKwriteBits(hBitStream, 0, 16); |
249 | 0 | } |
250 | 0 | } |
251 | 0 | FDKwriteBits(hBitStream, 0, 16); |
252 | 0 | } |
253 | 0 | } /* End of ADTS header */ |
254 | |
|
255 | 0 | return 0; |
256 | 0 | } |
257 | | |
258 | | void adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts, HANDLE_FDK_BITSTREAM hBs, |
259 | 0 | int *pBits) { |
260 | 0 | if (!hAdts->protection_absent) { |
261 | 0 | FDK_BITSTREAM bsWriter; |
262 | |
|
263 | 0 | FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, |
264 | 0 | BS_WRITER); |
265 | 0 | FDKpushFor(&bsWriter, 56); |
266 | |
|
267 | 0 | if (hAdts->num_raw_blocks == 0) { |
268 | 0 | FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16); |
269 | 0 | } else { |
270 | 0 | int distance; |
271 | | |
272 | | /* Write CRC of current raw data block */ |
273 | 0 | FDKwriteBits(hBs, FDKcrcGetCRC(&hAdts->crcInfo), 16); |
274 | | |
275 | | /* Write distance to current data block */ |
276 | 0 | if (hAdts->currentBlock < hAdts->num_raw_blocks) { |
277 | 0 | FDKpushFor(&bsWriter, hAdts->currentBlock * 16); |
278 | 0 | distance = |
279 | 0 | FDKgetValidBits(hBs) - (56 + (hAdts->num_raw_blocks) * 16 + 16); |
280 | 0 | FDKwriteBits(&bsWriter, distance >> 3, 16); |
281 | 0 | } |
282 | 0 | } |
283 | 0 | FDKsyncCache(&bsWriter); |
284 | 0 | } |
285 | | |
286 | | /* Write total frame lenth for multiple raw data blocks and header CRC */ |
287 | 0 | if (hAdts->num_raw_blocks > 0 && |
288 | 0 | hAdts->currentBlock == hAdts->num_raw_blocks) { |
289 | 0 | FDK_BITSTREAM bsWriter; |
290 | 0 | int crcIndex = 0; |
291 | |
|
292 | 0 | FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, |
293 | 0 | BS_WRITER); |
294 | |
|
295 | 0 | if (!hAdts->protection_absent) { |
296 | 0 | FDKcrcReset(&hAdts->crcInfo); |
297 | 0 | crcIndex = FDKcrcStartReg(&hAdts->crcInfo, &bsWriter, 0); |
298 | 0 | } |
299 | | /* Write total frame length */ |
300 | 0 | FDKpushFor(&bsWriter, 56 - 28 + 2); |
301 | 0 | FDKwriteBits(&bsWriter, FDKgetValidBits(hBs) >> 3, 13); |
302 | | |
303 | | /* Write header CRC */ |
304 | 0 | if (!hAdts->protection_absent) { |
305 | 0 | FDKpushFor(&bsWriter, 11 + 2 + (hAdts->num_raw_blocks) * 16); |
306 | 0 | FDKcrcEndReg(&hAdts->crcInfo, &bsWriter, crcIndex); |
307 | 0 | FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16); |
308 | 0 | } |
309 | 0 | FDKsyncCache(&bsWriter); |
310 | 0 | } |
311 | | |
312 | | /* Correct *pBits to reflect the amount of bits of the current subframe */ |
313 | 0 | *pBits -= hAdts->subFrameStartBit; |
314 | 0 | if (!hAdts->protection_absent && hAdts->num_raw_blocks > 0) { |
315 | | /* Fixup CRC bits, since they come after each raw data block */ |
316 | 0 | *pBits += 16; |
317 | 0 | } |
318 | 0 | hAdts->currentBlock++; |
319 | 0 | } |