1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 � Copyright 1995 - 2012 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 /************************** Fraunhofer IIS FDK SysLib **********************
85
86 Author(s): Eric Allamanche
87 Description: a rudimentary wav file interface
88
89 ******************************************************************************/
90
91
92
93 #include "wav_file.h"
94 #include "genericStds.h"
95
96
97 static INT_PCM ulaw2pcm (UCHAR ulawbyte);
98
99 /*!
100 *
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.
105 *
106 */
WAV_InputOpen(HANDLE_WAV * pWav,const char * filename)107 INT WAV_InputOpen (HANDLE_WAV *pWav, const char *filename)
108 {
109 HANDLE_WAV wav = (HANDLE_WAV)FDKcalloc(1, sizeof(struct WAV));
110 INT offset;
111
112 if (wav == NULL) {
113 FDKprintfErr("WAV_InputOpen(): Unable to allocate WAV struct.\n");
114 goto error;
115 }
116
117 wav->fp = FDKfopen(filename, "rb");
118 if (wav->fp == NULL) {
119 FDKprintfErr("WAV_InputOpen(): Unable to open wav file. %s\n", filename);
120 goto error;
121 }
122
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" */
127 }
128 if (FDKstrncmp("RIFF", wav->header.riffType, 4)) {
129 FDKprintfErr("WAV_InputOpen(): RIFF descriptor not found.\n") ;
130 goto error;
131 }
132
133 /* Read RIFF size. Ignored. */
134 FDKfread_EL(&(wav->header.riffSize), 4, 1, wav->fp);
135
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" */
140 }
141 if (FDKstrncmp("WAVE", wav->header.waveType, 4)) {
142 FDKprintfErr("WAV_InputOpen(): WAVE chunk ID not found.\n") ;
143 goto error;
144 }
145
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" */
150 }
151 if (FDKstrncmp("fmt", wav->header.formatType, 3)) {
152 FDKprintfErr("WAV_InputOpen(): fmt chunk format not found.\n") ;
153 goto error;
154 }
155
156
157 FDKfread_EL(&wav->header.formatSize, 4, 1, wav->fp); /* should be 16 for PCM-format (uncompressed) */
158
159
160 /* read info */
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);
167
168 offset = wav->header.formatSize - 16;
169
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
175 };
176 USHORT extraFormatBytes, validBitsPerSample;
177 UINT channelMask;
178 UCHAR guid[16];
179 INT i;
180
181 /* read extra bytes */
182 FDKfread_EL(&(extraFormatBytes), 2, 1, wav->fp);
183 offset -= 2;
184
185 if (extraFormatBytes >= 22) {
186 FDKfread_EL(&(validBitsPerSample), 2, 1, wav->fp);
187 FDKfread_EL(&(channelMask), 4, 1, wav->fp);
188 FDKfread_EL(&(guid), 16, 1, wav->fp);
189
190 /* check for PCM GUID */
191 for (i = 0; i < 16; i++) if (guid[i] != guidPCM[i]) break;
192 if (i == 16) wav->header.compressionCode = 0x01;
193
194 offset -= 22;
195 }
196 }
197
198 /* Skip rest of fmt header if any. */
199 for (;offset > 0; offset--) {
200 FDKfread(&wav->header.formatSize, 1, 1, wav->fp);
201 }
202
203 do {
204 /* Read data chunk ID */
205 if (FDKfread(wav->header.dataType, 1, 4, wav->fp) != 4) {
206 FDKprintfErr("WAV_InputOpen(): Unable to read data chunk ID.\n");
207 FDKfree(wav);
208 goto error;
209 }
210
211 /* Read chunk length. */
212 FDKfread_EL(&offset, 4, 1, wav->fp);
213
214 /* Check for data chunk signature. */
215 if (FDKstrncmp("data", wav->header.dataType, 4) == 0) {
216 wav->header.dataSize = offset;
217 break;
218 }
219 /* Jump over non data chunk. */
220 for (;offset > 0; offset--) {
221 FDKfread(&(wav->header.dataSize), 1, 1, wav->fp);
222 }
223 } while (!FDKfeof(wav->fp));
224
225 /* return success */
226 *pWav = wav;
227 return 0;
228
229 /* Error path */
230 error:
231 if (wav->fp) {
232 FDKfclose(wav->fp);
233 wav->fp = NULL;
234 }
235
236 if (wav) {
237 FDKfree(wav);
238 }
239
240 *pWav = NULL;
241
242 return -1;
243 }
244
245 /*!
246 *
247 * \brief Read samples from a WAVEfile. The samples are automatically reorder to the native
248 * host endianess and scaled to full scale of the INT_PCM type, from whatever bps the WAVEfile
249 * had specified in its haader data.
250 *
251 * \wav HANDLE_WAV of the wav file.
252 * \buffer Pointer to store read data.
253 * \numSamples Desired number of samples to read.
254 * \nBits sample size in bits to be used for the buffer
255 *
256 * \return Number of samples actually read.
257 *
258 */
259
WAV_InputRead(HANDLE_WAV wav,void * buffer,UINT numSamples,int nBits)260 INT WAV_InputRead (HANDLE_WAV wav, void *buffer, UINT numSamples, int nBits)
261 {
262 UINT result = 0 ;
263 UINT i;
264 SCHAR *bptr = (SCHAR*)buffer;
265 LONG *lptr = (LONG*)buffer;
266 SHORT *sptr = (SHORT*)buffer;
267
268 switch (wav->header.compressionCode)
269 {
270 case 0x01: /* PCM uncompressed */
271 if (nBits == wav->header.bitsPerSample) {
272 result = FDKfread_EL(buffer, wav->header.bitsPerSample >> 3, numSamples, wav->fp) ;
273 } else {
274 result = 0;
275 for (i=0; i<numSamples; i++)
276 {
277 LONG tmp = 0;
278 result += FDKfread_EL(&tmp, wav->header.bitsPerSample >> 3, 1, wav->fp) ;
279
280 /* Move read bits to lower bits of LONG. */
281 if ( !IS_LITTLE_ENDIAN() && wav->header.bitsPerSample != 24 && wav->header.bitsPerSample < 32) {
282 tmp >>= (32-wav->header.bitsPerSample);
283 }
284
285 /* Full scale */
286 if (wav->header.bitsPerSample > nBits)
287 tmp >>= (wav->header.bitsPerSample-nBits);
288 else
289 tmp <<= (nBits-wav->header.bitsPerSample);
290
291 if (nBits == 8)
292 *bptr++ = (SCHAR) tmp;
293 if (nBits == 16)
294 *sptr++ = (SHORT) tmp;
295 if (nBits == 32)
296 *lptr++ = (LONG) tmp;
297 }
298 }
299 break;
300
301 case 0x07: /* u-Law compression */
302 for (i=0; i<numSamples; i++) {
303 result += FDKfread(&(bptr[i<<1]), 1, 1, wav->fp) ;
304 sptr[i] = ulaw2pcm(bptr[i<<1]) ;
305 }
306 break ;
307
308 default:
309 FDKprintf("WAV_InputRead(): unsupported data-compression!!") ;
310 break ;
311 }
312 return result ;
313 }
314
WAV_InputClose(HANDLE_WAV * pWav)315 void WAV_InputClose(HANDLE_WAV *pWav)
316 {
317 HANDLE_WAV wav = *pWav;
318
319 if (wav != NULL) {
320 if (wav->fp != NULL) {
321 FDKfclose(wav->fp);
322 wav->fp = NULL;
323 }
324 if (wav) {
325 FDKfree(wav);
326 }
327 }
328 *pWav = NULL;
329 }
330
331 /* conversion of u-law to linear coding */
ulaw2pcm(UCHAR ulawbyte)332 static INT_PCM ulaw2pcm (UCHAR ulawbyte)
333 {
334 static const INT exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 } ;
335 INT sign, exponent, mantissa, sample ;
336
337 ulawbyte = (UCHAR)~ulawbyte ;
338 sign = (ulawbyte & 0x80) ;
339 exponent = (ulawbyte >> 4) & 0x07 ;
340 mantissa = ulawbyte & 0x0F ;
341
342 sample = exp_lut[exponent] + (mantissa << (exponent + 3)) ;
343 if (sign != 0)
344 sample = -sample ;
345
346 return (INT_PCM)sample ;
347 }
348
349 /************** Writer ***********************/
350
LittleEndian32(UINT v)351 static UINT LittleEndian32(UINT v)
352 {
353 if (IS_LITTLE_ENDIAN())
354 return v ;
355 else
356 return (v & 0x000000FF) << 24 | (v & 0x0000FF00) << 8 | (v & 0x00FF0000) >> 8 | (v & 0xFF000000) >> 24;
357 }
358
LittleEndian16(SHORT v)359 static SHORT LittleEndian16(SHORT v)
360 {
361 if (IS_LITTLE_ENDIAN())
362 return v;
363 else
364 return (SHORT)(((v << 8) & 0xFF00) | ((v >> 8) & 0x00FF));
365 }
366
Unpack(USHORT v)367 static USHORT Unpack(USHORT v)
368 {
369 if (IS_LITTLE_ENDIAN())
370 return v;
371 else
372 return (SHORT)(((v << 8) & 0xFF00) | ((v >> 8) & 0x00FF));
373 }
374
375 /**
376 * WAV_OutputOpen
377 * \brief Open WAV output/writer handle
378 * \param pWav pointer to WAV handle to be returned
379 * \param sampleRate desired samplerate of the resulting WAV file
380 * \param numChannels desired number of audio channels of the resulting WAV file
381 * \param bitsPerSample desired number of bits per audio sample of the resulting WAV file
382 *
383 * \return value: 0: ok
384 * -1: error
385 */
WAV_OutputOpen(HANDLE_WAV * pWav,const char * outputFilename,INT sampleRate,INT numChannels,INT bitsPerSample)386 INT WAV_OutputOpen(HANDLE_WAV *pWav, const char *outputFilename, INT sampleRate, INT numChannels, INT bitsPerSample)
387 {
388 HANDLE_WAV wav = (HANDLE_WAV)FDKcalloc(1, sizeof(struct WAV));
389 UINT size = 0;
390
391 if (bitsPerSample != 16 && bitsPerSample != 24 && bitsPerSample != 32)
392 {
393 FDKprintfErr("WAV_OutputOpen(): Invalid argument (bitsPerSample).\n");
394 goto bail;
395 }
396
397 wav->fp = FDKfopen(outputFilename, "wb");
398 if (wav->fp == NULL)
399 {
400 FDKprintfErr("WAV_OutputOpen(): unable to create file %s\n", outputFilename);
401 goto bail;
402 }
403
404 FDKstrcpy(wav->header.riffType, "RIFF");
405 wav->header.riffSize = LittleEndian32(0x7fffffff); /* in case fseek() doesn't work later in WAV_OutputClose() */
406 FDKstrcpy(wav->header.waveType, "WAVE");
407
408 FDKstrcpy(wav->header.formatType, "fmt ");
409 wav->header.formatSize = LittleEndian32(16);
410
411 wav->header.compressionCode = LittleEndian16(0x01);
412 wav->header.bitsPerSample = LittleEndian16((SHORT)bitsPerSample);
413 wav->header.numChannels = LittleEndian16((SHORT)numChannels);
414 wav->header.blockAlign = LittleEndian16((SHORT)(numChannels * (bitsPerSample >> 3)));
415 wav->header.sampleRate = LittleEndian32(sampleRate);
416 wav->header.bytesPerSecond = LittleEndian32(sampleRate * wav->header.blockAlign);
417 FDKstrcpy(wav->header.dataType, "data");
418 wav->header.dataSize = LittleEndian32(0x7fffffff - 36);
419
420
421 size = sizeof(WAV_HEADER);
422 if (FDKfwrite(&wav->header, 1, size, wav->fp) != size)
423 {
424 FDKprintfErr("WAV_OutputOpen(): error writing to output file %s\n", outputFilename);
425 goto bail;
426 }
427
428
429 wav->header.dataSize = wav->header.riffSize = 0;
430
431 *pWav = wav;
432
433 return 0;
434
435 bail:
436 if (wav->fp)
437 FDKfclose(wav->fp);
438 if (wav)
439 FDKfree(wav);
440
441 pWav = NULL;
442
443 return -1;
444 }
445
446
447 /**
448 * WAV_OutputWrite
449 * \brief Write data to WAV file asociated to WAV handle
450 *
451 * \param wav handle of wave file
452 * \param sampleBuffer pointer to audio samples, right justified integer values
453 * \param nBufBits size in bits of each audio sample in sampleBuffer
454 * \param nSigBits amount of significant bits of each nBufBits in sampleBuffer
455 *
456 * \return value: 0: ok
457 * -1: error
458 */
WAV_OutputWrite(HANDLE_WAV wav,void * sampleBuffer,UINT numberOfSamples,int nBufBits,int nSigBits)459 INT WAV_OutputWrite(HANDLE_WAV wav, void *sampleBuffer, UINT numberOfSamples, int nBufBits, int nSigBits)
460 {
461 SCHAR *bptr = (SCHAR*)sampleBuffer;
462 SHORT *sptr = (SHORT*)sampleBuffer;
463 LONG *lptr = (LONG*)sampleBuffer;
464 LONG tmp;
465
466 int bps = Unpack(wav->header.bitsPerSample);
467 UINT i;
468
469 /* Pack samples if required */
470 if (bps == nBufBits && bps == nSigBits) {
471 if (FDKfwrite_EL(sampleBuffer, (bps>>3), numberOfSamples, wav->fp) != numberOfSamples)
472 {
473 FDKprintfErr("WAV_OutputWrite(): error: unable to write to file %d\n", wav->fp);
474 return -1;
475 }
476 } else {
477 for (i=0; i<numberOfSamples; i++)
478 {
479 int result;
480 int shift;
481
482 switch (nBufBits) {
483 case 8: tmp = *bptr++; break;
484 case 16: tmp = *sptr++; break;
485 case 32: tmp = *lptr++; break;
486 default: return -1;
487 }
488 /* Adapt sample size */
489 shift = (nBufBits-nSigBits)-(32-bps);
490
491 /* Correct alignment difference between 32 bit data buffer "tmp" and 24 bits to be written. */
492 if ( !IS_LITTLE_ENDIAN() && bps == 24) {
493 shift += 8;
494 }
495
496 if (shift < 0)
497 tmp >>= -shift;
498 else
499 tmp <<= shift;
500
501 /* Write sample */
502 result=FDKfwrite_EL(&tmp, bps>>3, 1, wav->fp);
503 if (result <= 0) {
504 FDKprintfErr("WAV_OutputWrite(): error: unable to write to file %d\n", wav->fp);
505 return -1;
506 }
507 }
508 }
509
510 wav->header.dataSize += (numberOfSamples * (bps>>3));
511 return 0;
512 }
513
514
515 /**
516 * WAV_OutputClose
517 * \brief Close WAV Output handle
518 * \param pWav pointer to WAV handle. *pWav is set to NULL.
519 */
WAV_OutputClose(HANDLE_WAV * pWav)520 void WAV_OutputClose(HANDLE_WAV *pWav)
521 {
522 HANDLE_WAV wav = *pWav;
523 UINT size = 0;
524
525 if ( wav == NULL ) {
526 return;
527 }
528
529 wav->header.dataSize = LittleEndian32(wav->header.dataSize);
530 wav->header.riffSize = LittleEndian32(wav->header.dataSize + 36);
531
532 if (wav->fp != NULL)
533 {
534 if (FDKfseek(wav->fp, 0, FDKSEEK_SET)) {
535 FDKprintf("WAV_OutputClose(): fseek() failed.\n");
536 }
537
538 size = sizeof(WAV_HEADER);
539 if (FDKfwrite(&wav->header.riffType, 1, size, wav->fp) != size)
540 {
541 FDKprintfErr("WAV_OutputClose(): unable to write header\n");
542 }
543
544 if (FDKfclose(wav->fp))
545 {
546 FDKprintfErr("WAV_OutputClose(): unable to close wav file\n");
547 }
548 wav->fp = NULL;
549 }
550
551 FDKfree(wav);
552 *pWav = NULL;
553 }
554
555