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 /************************** Fraunhofer IIS FDK SysLib **********************
86 Author(s): Eric Allamanche
87 Description: a rudimentary wav file interface
89 ******************************************************************************/
94 #include "genericStds.h"
97 static INT_PCM
ulaw2pcm (UCHAR ulawbyte
);
101 * \brief Read header from a WAVEfile. Host endianess is handled accordingly.
102 * \wav->fp filepointer of type FILE*.
103 * \wavinfo SWavInfo struct where the decoded header info is stored into.
104 * \return 0 on success and non-zero on failure.
107 INT
WAV_InputOpen (HANDLE_WAV
*pWav
, const char *filename
)
109 HANDLE_WAV wav
= (HANDLE_WAV
)FDKcalloc(1, sizeof(struct WAV
));
113 FDKprintfErr("WAV_InputOpen(): Unable to allocate WAV struct.\n");
117 wav
->fp
= FDKfopen(filename
, "rb");
118 if (wav
->fp
== NULL
) {
119 FDKprintfErr("WAV_InputOpen(): Unable to open wav file. %s\n", filename
);
123 /* read RIFF-chunk */
124 if (FDKfread(&(wav
->header
.riffType
), 1, 4, wav
->fp
) != 4) {
125 FDKprintfErr("WAV_InputOpen(): couldn't read RIFF_ID\n");
126 goto error
; /* bad error "couldn't read RIFF_ID" */
128 if (FDKstrncmp("RIFF", wav
->header
.riffType
, 4)) {
129 FDKprintfErr("WAV_InputOpen(): RIFF descriptor not found.\n") ;
133 /* Read RIFF size. Ignored. */
134 FDKfread_EL(&(wav
->header
.riffSize
), 4, 1, wav
->fp
);
136 /* read WAVE-chunk */
137 if (FDKfread(&wav
->header
.waveType
, 1, 4, wav
->fp
) !=4) {
138 FDKprintfErr("WAV_InputOpen(): couldn't read format\n");
139 goto error
; /* bad error "couldn't read format" */
141 if (FDKstrncmp("WAVE", wav
->header
.waveType
, 4)) {
142 FDKprintfErr("WAV_InputOpen(): WAVE chunk ID not found.\n") ;
146 /* read format-chunk */
147 if (FDKfread(&(wav
->header
.formatType
), 1, 4, wav
->fp
) != 4) {
148 FDKprintfErr("WAV_InputOpen(): couldn't read format_ID\n");
149 goto error
; /* bad error "couldn't read format_ID" */
151 if (FDKstrncmp("fmt", wav
->header
.formatType
, 3)) {
152 FDKprintfErr("WAV_InputOpen(): fmt chunk format not found.\n") ;
157 FDKfread_EL(&wav
->header
.formatSize
, 4, 1, wav
->fp
); /* should be 16 for PCM-format (uncompressed) */
161 FDKfread_EL(&(wav
->header
.compressionCode
), 2, 1, wav
->fp
);
162 FDKfread_EL(&(wav
->header
.numChannels
), 2, 1, wav
->fp
);
163 FDKfread_EL(&(wav
->header
.sampleRate
), 4, 1, wav
->fp
);
164 FDKfread_EL(&(wav
->header
.bytesPerSecond
), 4, 1, wav
->fp
);
165 FDKfread_EL(&(wav
->header
.blockAlign
), 2, 1, wav
->fp
);
166 FDKfread_EL(&(wav
->header
.bitsPerSample
), 2, 1, wav
->fp
);
168 offset
= wav
->header
.formatSize
- 16;
170 /* Wave format extensible */
171 if (wav
->header
.compressionCode
== 0xFFFE) {
172 static const UCHAR guidPCM
[16] = {
173 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
174 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
176 USHORT extraFormatBytes
, validBitsPerSample
;
180 /* read extra bytes */
181 FDKfread_EL(&(extraFormatBytes
), 2, 1, wav
->fp
);
184 if (extraFormatBytes
>= 22) {
185 FDKfread_EL(&(validBitsPerSample
), 2, 1, wav
->fp
);
186 FDKfread_EL(&(wav
->channelMask
), 4, 1, wav
->fp
);
187 FDKfread_EL(&(guid
), 16, 1, wav
->fp
);
189 /* check for PCM GUID */
190 for (i
= 0; i
< 16; i
++) if (guid
[i
] != guidPCM
[i
]) break;
191 if (i
== 16) wav
->header
.compressionCode
= 0x01;
197 /* Skip rest of fmt header if any. */
198 for (;offset
> 0; offset
--) {
199 FDKfread(&wav
->header
.formatSize
, 1, 1, wav
->fp
);
203 /* Read data chunk ID */
204 if (FDKfread(wav
->header
.dataType
, 1, 4, wav
->fp
) != 4) {
205 FDKprintfErr("WAV_InputOpen(): Unable to read data chunk ID.\n");
210 /* Read chunk length. */
211 FDKfread_EL(&offset
, 4, 1, wav
->fp
);
213 /* Check for data chunk signature. */
214 if (FDKstrncmp("data", wav
->header
.dataType
, 4) == 0) {
215 wav
->header
.dataSize
= offset
;
218 /* Jump over non data chunk. */
219 for (;offset
> 0; offset
--) {
220 FDKfread(&(wav
->header
.dataSize
), 1, 1, wav
->fp
);
222 } while (!FDKfeof(wav
->fp
));
246 * \brief Read samples from a WAVEfile. The samples are automatically reorder to the native
247 * host endianess and scaled to full scale of the INT_PCM type, from whatever bps the WAVEfile
248 * had specified in its haader data.
250 * \wav HANDLE_WAV of the wav file.
251 * \buffer Pointer to store read data.
252 * \numSamples Desired number of samples to read.
253 * \nBits sample size in bits to be used for the buffer
255 * \return Number of samples actually read.
259 INT
WAV_InputRead (HANDLE_WAV wav
, void *buffer
, UINT numSamples
, int nBits
)
263 SCHAR
*bptr
= (SCHAR
*)buffer
;
264 LONG
*lptr
= (LONG
*)buffer
;
265 SHORT
*sptr
= (SHORT
*)buffer
;
267 switch (wav
->header
.compressionCode
)
269 case 0x01: /* PCM uncompressed */
270 if (nBits
== wav
->header
.bitsPerSample
) {
271 result
= FDKfread_EL(buffer
, wav
->header
.bitsPerSample
>> 3, numSamples
, wav
->fp
) ;
274 for (i
=0; i
<numSamples
; i
++)
277 result
+= FDKfread_EL(&tmp
, wav
->header
.bitsPerSample
>> 3, 1, wav
->fp
) ;
279 /* Move read bits to lower bits of LONG. */
280 if ( !IS_LITTLE_ENDIAN() && wav
->header
.bitsPerSample
!= 24 && wav
->header
.bitsPerSample
< 32) {
281 tmp
>>= (32-wav
->header
.bitsPerSample
);
285 if (wav
->header
.bitsPerSample
> nBits
)
286 tmp
>>= (wav
->header
.bitsPerSample
-nBits
);
288 tmp
<<= (nBits
-wav
->header
.bitsPerSample
);
291 *bptr
++ = (SCHAR
) tmp
;
293 *sptr
++ = (SHORT
) tmp
;
295 *lptr
++ = (LONG
) tmp
;
300 case 0x07: /* u-Law compression */
301 for (i
=0; i
<numSamples
; i
++) {
302 result
+= FDKfread(&(bptr
[i
<<1]), 1, 1, wav
->fp
) ;
303 sptr
[i
] = ulaw2pcm(bptr
[i
<<1]) ;
308 FDKprintf("WAV_InputRead(): unsupported data-compression!!") ;
314 void WAV_InputClose(HANDLE_WAV
*pWav
)
316 HANDLE_WAV wav
= *pWav
;
319 if (wav
->fp
!= NULL
) {
330 /* conversion of u-law to linear coding */
331 static INT_PCM
ulaw2pcm (UCHAR ulawbyte
)
333 static const INT exp_lut
[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 } ;
334 INT sign
, exponent
, mantissa
, sample
;
336 ulawbyte
= (UCHAR
)~ulawbyte
;
337 sign
= (ulawbyte
& 0x80) ;
338 exponent
= (ulawbyte
>> 4) & 0x07 ;
339 mantissa
= ulawbyte
& 0x0F ;
341 sample
= exp_lut
[exponent
] + (mantissa
<< (exponent
+ 3)) ;
345 return (INT_PCM
)sample
;
348 /************** Writer ***********************/
350 static UINT
LittleEndian32(UINT v
)
352 if (IS_LITTLE_ENDIAN())
355 return (v
& 0x000000FF) << 24 | (v
& 0x0000FF00) << 8 | (v
& 0x00FF0000) >> 8 | (v
& 0xFF000000) >> 24;
358 static SHORT
LittleEndian16(SHORT v
)
360 if (IS_LITTLE_ENDIAN())
363 return (SHORT
)(((v
<< 8) & 0xFF00) | ((v
>> 8) & 0x00FF));
366 static USHORT
Unpack(USHORT v
)
368 if (IS_LITTLE_ENDIAN())
371 return (SHORT
)(((v
<< 8) & 0xFF00) | ((v
>> 8) & 0x00FF));
376 * \brief Open WAV output/writer handle
377 * \param pWav pointer to WAV handle to be returned
378 * \param sampleRate desired samplerate of the resulting WAV file
379 * \param numChannels desired number of audio channels of the resulting WAV file
380 * \param bitsPerSample desired number of bits per audio sample of the resulting WAV file
382 * \return value: 0: ok
385 INT
WAV_OutputOpen(HANDLE_WAV
*pWav
, const char *outputFilename
, INT sampleRate
, INT numChannels
, INT bitsPerSample
)
387 HANDLE_WAV wav
= (HANDLE_WAV
)FDKcalloc(1, sizeof(struct WAV
));
391 FDKprintfErr("WAV_OutputOpen(): Unable to allocate WAV struct.\n");
395 if (bitsPerSample
!= 16 && bitsPerSample
!= 24 && bitsPerSample
!= 32)
397 FDKprintfErr("WAV_OutputOpen(): Invalid argument (bitsPerSample).\n");
401 wav
->fp
= FDKfopen(outputFilename
, "wb");
404 FDKprintfErr("WAV_OutputOpen(): unable to create file %s\n", outputFilename
);
408 FDKstrcpy(wav
->header
.riffType
, "RIFF");
409 wav
->header
.riffSize
= LittleEndian32(0x7fffffff); /* in case fseek() doesn't work later in WAV_OutputClose() */
410 FDKstrcpy(wav
->header
.waveType
, "WAVE");
412 FDKstrcpy(wav
->header
.formatType
, "fmt ");
413 wav
->header
.formatSize
= LittleEndian32(16);
415 wav
->header
.compressionCode
= LittleEndian16(0x01);
416 wav
->header
.bitsPerSample
= LittleEndian16((SHORT
)bitsPerSample
);
417 wav
->header
.numChannels
= LittleEndian16((SHORT
)numChannels
);
418 wav
->header
.blockAlign
= LittleEndian16((SHORT
)(numChannels
* (bitsPerSample
>> 3)));
419 wav
->header
.sampleRate
= LittleEndian32(sampleRate
);
420 wav
->header
.bytesPerSecond
= LittleEndian32(sampleRate
* wav
->header
.blockAlign
);
421 FDKstrcpy(wav
->header
.dataType
, "data");
422 wav
->header
.dataSize
= LittleEndian32(0x7fffffff - 36);
425 size
= sizeof(WAV_HEADER
);
426 if (FDKfwrite(&wav
->header
, 1, size
, wav
->fp
) != size
)
428 FDKprintfErr("WAV_OutputOpen(): error writing to output file %s\n", outputFilename
);
433 wav
->header
.dataSize
= wav
->header
.riffSize
= 0;
455 * \brief Write data to WAV file asociated to WAV handle
457 * \param wav handle of wave file
458 * \param sampleBuffer pointer to audio samples, right justified integer values
459 * \param nBufBits size in bits of each audio sample in sampleBuffer
460 * \param nSigBits amount of significant bits of each nBufBits in sampleBuffer
462 * \return value: 0: ok
465 INT
WAV_OutputWrite(HANDLE_WAV wav
, void *sampleBuffer
, UINT numberOfSamples
, int nBufBits
, int nSigBits
)
467 SCHAR
*bptr
= (SCHAR
*)sampleBuffer
;
468 SHORT
*sptr
= (SHORT
*)sampleBuffer
;
469 LONG
*lptr
= (LONG
*)sampleBuffer
;
472 int bps
= Unpack(wav
->header
.bitsPerSample
);
475 /* Pack samples if required */
476 if (bps
== nBufBits
&& bps
== nSigBits
) {
477 if (FDKfwrite_EL(sampleBuffer
, (bps
>>3), numberOfSamples
, wav
->fp
) != numberOfSamples
)
479 FDKprintfErr("WAV_OutputWrite(): error: unable to write to file %d\n", wav
->fp
);
483 for (i
=0; i
<numberOfSamples
; i
++)
489 case 8: tmp
= *bptr
++; break;
490 case 16: tmp
= *sptr
++; break;
491 case 32: tmp
= *lptr
++; break;
494 /* Adapt sample size */
495 shift
= (nBufBits
-nSigBits
)-(32-bps
);
497 /* Correct alignment difference between 32 bit data buffer "tmp" and 24 bits to be written. */
498 if ( !IS_LITTLE_ENDIAN() && bps
== 24) {
508 result
=FDKfwrite_EL(&tmp
, bps
>>3, 1, wav
->fp
);
510 FDKprintfErr("WAV_OutputWrite(): error: unable to write to file %d\n", wav
->fp
);
516 wav
->header
.dataSize
+= (numberOfSamples
* (bps
>>3));
523 * \brief Close WAV Output handle
524 * \param pWav pointer to WAV handle. *pWav is set to NULL.
526 void WAV_OutputClose(HANDLE_WAV
*pWav
)
528 HANDLE_WAV wav
= *pWav
;
535 wav
->header
.dataSize
= LittleEndian32(wav
->header
.dataSize
);
536 wav
->header
.riffSize
= LittleEndian32(wav
->header
.dataSize
+ 36);
540 if (FDKfseek(wav
->fp
, 0, FDKSEEK_SET
)) {
541 FDKprintf("WAV_OutputClose(): fseek() failed.\n");
544 size
= sizeof(WAV_HEADER
);
545 if (FDKfwrite(&wav
->header
.riffType
, 1, size
, wav
->fp
) != size
)
547 FDKprintfErr("WAV_OutputClose(): unable to write header\n");
550 if (FDKfclose(wav
->fp
))
552 FDKprintfErr("WAV_OutputClose(): unable to close wav file\n");