1 // AudioDecoderSimple.cpp: Audio decoding using "simple" internal decoders.
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "AudioDecoderSimple.h"
23 #include "AudioResampler.h"
24 #include "BitsReader.h"
25 #include "SoundInfo.h"
26 #include "MediaParser.h" // for AudioInfo definition..
27 #include "GnashNumeric.h" // for clamp
31 #include <boost/scoped_array.hpp>
32 #include <algorithm> // for std::swap
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 /// ADPCM decoder utilities
43 /// Algo from http://www.circuitcellar.com/pastissues/articles/richey110/text.htm
45 /// Here's another reference: http://www.geocities.com/SiliconValley/8682/aud3.txt
46 /// Original IMA spec doesn't seem to be on the web :(
48 /// TODO: move in it's own file
54 // Data from Alexis' SWF reference
55 static int _index_update_table_2bits
[2];
56 static int _index_update_table_3bits
[4];
57 static int _index_update_table_4bits
[8];
58 static int _index_update_table_5bits
[16];
60 static int* s_index_update_tables
[4];
62 // Data from Jansen. http://homepages.cwi.nl/~jack/
63 // Check out his Dutch retro punk songs, heh heh :)
64 static const int STEPSIZE_CT
= 89;
65 static int s_stepsize
[STEPSIZE_CT
];
68 static void doSample(int n_bits
, int& sample
, int& stepsize_index
, int raw_code
)
70 assert(raw_code
>= 0 && raw_code
< (1 << n_bits
));
72 static const int HI_BIT
= (1 << (n_bits
- 1));
73 int* index_update_table
= s_index_update_tables
[n_bits
- 2];
77 int code_mag
= raw_code
& (HI_BIT
- 1);
78 bool code_sign_bit
= (raw_code
& HI_BIT
) ? 1 : 0;
79 int mag
= (code_mag
<< 1) + 1; /* shift in LSB (they do this so that pos & neg zero are different)*/
81 int stepsize
= s_stepsize
[stepsize_index
];
83 /* Compute the new sample. It's the predicted value */
84 /* (i.e. the previous value), plus a delta. The delta */
85 /* comes from the code times the stepsize. going for */
86 /* something like: delta = stepsize * (code * 2 + 1) >> code_bits */
87 int delta
= (stepsize
* mag
) >> (n_bits
- 1);
88 if (code_sign_bit
) delta
= -delta
;
91 sample
= clamp
<int>(sample
, -32768, 32767);
93 /* Update our stepsize index. Use a lookup table. */
94 stepsize_index
+= index_update_table
[code_mag
];
95 stepsize_index
= clamp
<int>(stepsize_index
, 0, STEPSIZE_CT
- 1);
98 /* Uncompress 4096 mono samples of ADPCM. */
99 static boost::uint32_t doMonoBlock(boost::int16_t** out_data
, int n_bits
, BitsReader
& in
, int sample
, int stepsize_index
)
101 /* First sample doesn't need to be decompressed. */
102 boost::uint32_t sample_count
= 1;
103 *(*out_data
)++ = (boost::int16_t) sample
;
105 while (sample_count
< 4096 && in
.gotBits(n_bits
))
107 int raw_code
= in
.read_uint(n_bits
);
108 doSample(n_bits
, sample
, stepsize_index
, raw_code
); /* sample & stepsize_index are in/out params */
109 *(*out_data
)++ = (boost::int16_t) sample
;
117 /* Uncompress 4096 stereo sample pairs of ADPCM. */
118 static int doStereoBlock(
119 boost::int16_t** out_data
, // in/out param
123 int left_stepsize_index
,
125 int right_stepsize_index
128 /* First samples don't need to be decompressed. */
129 boost::uint32_t sample_count
= 2;
130 *(*out_data
)++ = (boost::int16_t) left_sample
;
131 *(*out_data
)++ = (boost::int16_t) right_sample
;
133 unsigned bitsNeeded
= n_bits
*2;
134 while (sample_count
< 4096 && in
.gotBits(bitsNeeded
))
136 int left_raw_code
= in
.read_uint(n_bits
);
137 doSample(n_bits
, left_sample
, left_stepsize_index
, left_raw_code
);
138 *(*out_data
)++ = (boost::int16_t) left_sample
;
140 int right_raw_code
= in
.read_uint(n_bits
);
141 doSample(n_bits
, right_sample
, right_stepsize_index
, right_raw_code
);
142 *(*out_data
)++ = (boost::int16_t) right_sample
;
151 // Utility function: uncompress ADPCM data from in BitReader to
152 // out_data[]. Returns the output samplecount.
153 static boost::uint32_t adpcm_expand(
154 unsigned char* &data
,
160 if ( ! in
.gotBits(2) )
162 IF_VERBOSE_MALFORMED_SWF(
163 log_swferror(_("corrupted ADPCM header"));
167 unsigned int n_bits
= in
.read_uint(2) + 2; // 2 to 5 bits
169 // The compression ratio is 4:1, so this should be enough...
170 boost::int16_t* out_data
= new boost::int16_t[insize
* 5];
171 data
= reinterpret_cast<unsigned char *>(out_data
);
173 boost::uint32_t sample_count
= 0;
175 while ( in
.gotBits(22) )
177 // Read initial sample & index values.
179 int sample
= in
.read_sint(16);
181 int stepsize_index
= in
.read_uint(6);
182 assert(STEPSIZE_CT
>= (1 << 6)); // ensure we don't need to clamp.
189 } else if (n_bits
== 2) {
190 sample_count
+= doMonoBlock(&out_data
, 2, in
, sample
, stepsize_index
);
191 } else if (n_bits
== 3) {
192 sample_count
+= doMonoBlock(&out_data
, 3, in
, sample
, stepsize_index
);
193 } else if (n_bits
== 4) {
194 sample_count
+= doMonoBlock(&out_data
, 4, in
, sample
, stepsize_index
);
195 } else if (n_bits
== 5) {
196 sample_count
+= doMonoBlock(&out_data
, 5, in
, sample
, stepsize_index
);
203 // Got values for left channel; now get initial sample
204 // & index for right channel.
205 int right_sample
= in
.read_sint(16);
207 int right_stepsize_index
= in
.read_uint(6);
208 assert(STEPSIZE_CT
>= (1 << 6)); // ensure we don't need to clamp.
212 } else if (n_bits
== 2) {
213 sample_count
+= doStereoBlock(&out_data
, 2, in
, sample
, stepsize_index
, right_sample
, right_stepsize_index
);
214 } else if (n_bits
== 3) {
215 sample_count
+= doStereoBlock(&out_data
, 3, in
, sample
, stepsize_index
, right_sample
, right_stepsize_index
);
216 } else if (n_bits
== 4) {
217 sample_count
+= doStereoBlock(&out_data
, 4, in
, sample
, stepsize_index
, right_sample
, right_stepsize_index
);
218 } else if (n_bits
== 5) {
219 sample_count
+= doStereoBlock(&out_data
, 5, in
, sample
, stepsize_index
, right_sample
, right_stepsize_index
);
230 int ADPCMDecoder::_index_update_table_2bits
[2] = { -1, 2 };
231 int ADPCMDecoder::_index_update_table_3bits
[4] = { -1, -1, 2, 4 };
232 int ADPCMDecoder::_index_update_table_4bits
[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
233 int ADPCMDecoder::_index_update_table_5bits
[16] = { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 };
235 int* ADPCMDecoder::s_index_update_tables
[4] = {
236 ADPCMDecoder::_index_update_table_2bits
,
237 ADPCMDecoder::_index_update_table_3bits
,
238 ADPCMDecoder::_index_update_table_4bits
,
239 ADPCMDecoder::_index_update_table_5bits
242 int ADPCMDecoder::s_stepsize
[STEPSIZE_CT
] = {
243 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
244 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
245 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
246 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
247 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
248 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
249 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
250 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
251 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
254 // ----------------------------------------------------------------------------
255 // END OF ADPCMDecoder class
256 // ----------------------------------------------------------------------------
259 // Unsigned 8-bit expansion (128 is silence)
261 // u8_expand allocates the memory for its "data" pointer.
265 u8_expand(unsigned char * &data
,
266 const unsigned char* input
,
267 boost::uint32_t input_size
) // This is also the number of u8bit samples
269 boost::int16_t *out_data
= new boost::int16_t[input_size
];
271 // Convert 8-bit to 16
272 const boost::uint8_t *inp
= input
;
273 boost::int16_t *outp
= out_data
;
274 for (unsigned int i
= input_size
; i
>0; i
--) {
275 *outp
++ = ((boost::int16_t)(*inp
++) - 128) * 256;
278 data
= (unsigned char *)out_data
;
282 AudioDecoderSimple::AudioDecoderSimple(const AudioInfo
& info
)
291 log_debug(_("AudioDecoderSimple: initialized flash codec %s (%d)"),
292 (int)_codec
, _codec
);
295 AudioDecoderSimple::AudioDecoderSimple(const SoundInfo
& info
)
304 log_debug(_("AudioDecoderSimple: initialized flash codec %s (%d)"),
305 (int)_codec
, _codec
);
309 AudioDecoderSimple::~AudioDecoderSimple()
314 AudioDecoderSimple::setup(const SoundInfo
& info
)
316 _codec
= info
.getFormat();
319 case AUDIO_CODEC_ADPCM
:
320 case AUDIO_CODEC_RAW
:
321 case AUDIO_CODEC_UNCOMPRESSED
:
322 _sampleRate
= info
.getSampleRate();
323 _sampleCount
= info
.getSampleCount();
324 _stereo
= info
.isStereo();
325 _is16bit
= info
.is16bit();
329 boost::format err
= boost::format(
330 _("AudioDecoderSimple: unsupported flash codec %d (%s)"))
331 % (int)_codec
% _codec
;
332 throw MediaException(err
.str());
337 AudioDecoderSimple::setup(const AudioInfo
& info
)
339 if (info
.type
!= CODEC_TYPE_FLASH
) {
340 boost::format err
= boost::format(
341 _("AudioDecoderSimple: unable to intepret custom audio codec id %s"))
343 throw MediaException(err
.str());
346 _codec
= static_cast<audioCodecType
>(info
.codec
);
349 case AUDIO_CODEC_ADPCM
:
350 case AUDIO_CODEC_RAW
:
351 case AUDIO_CODEC_UNCOMPRESSED
:
352 _sampleRate
= info
.sampleRate
;
353 _stereo
= info
.stereo
;
354 _is16bit
= (info
.sampleSize
==2); // check this!
355 if ( info
.sampleSize
> 2 ) // troubles...
356 log_unimpl("Sample size > 2 in %s sound!", _codec
);
360 boost::format err
= boost::format(
361 _("AudioDecoderSimple: unsupported flash codec %d (%s)"))
362 % (int)_codec
% _codec
;
363 throw MediaException(err
.str());
368 AudioDecoderSimple::decode(const boost::uint8_t* input
, boost::uint32_t inputSize
,
369 boost::uint32_t& outputSize
, boost::uint32_t& decodedBytes
)
372 unsigned char* decodedData
= NULL
;
373 boost::uint32_t outsize
= 0;
376 case AUDIO_CODEC_ADPCM
:
378 //boost::uint32_t sample_count = inputSize * ( _stereo ? 1 : 2 ); //(_sampleCount == 0 ? inputSize / ( _stereo ? 4 : 2 ) : _sampleCount);
379 BitsReader
br(input
, inputSize
);
380 boost::uint32_t sample_count
= ADPCMDecoder::adpcm_expand(decodedData
, br
, inputSize
, _stereo
);
381 outsize
= sample_count
* (_stereo
? 4 : 2);
384 case AUDIO_CODEC_RAW
:
386 // FORMAT_RAW 16-bit is exactly what we want!
387 decodedData
= new unsigned char[inputSize
];
388 memcpy(decodedData
, input
, inputSize
);
391 // Convert 8-bit signed to 16-bit range
392 // Allocate as many shorts as there are samples
393 u8_expand(decodedData
, input
, inputSize
);
394 outsize
= inputSize
* (_stereo
? 4 : 2);
397 case AUDIO_CODEC_UNCOMPRESSED
:
398 // 8- or 16-bit mono or stereo little-endian audio
399 // Convert to 16-bit host-endian.
402 // Convert 8-bit signed to 16-bit range
403 // Allocate as many shorts as there are 8-bit samples
404 u8_expand(decodedData
, input
, inputSize
);
405 outsize
= inputSize
* (_stereo
? 4 : 2);
408 // Allocate a destination buffer
409 // Read 16-bit data into buffer
410 decodedData
= new unsigned char[inputSize
];
413 // Convert 16-bit little-endian data to host-endian.
415 // Runtime detection of host endianness costs almost
416 // nothing and is less of a continual maintenance headache
417 // than compile-time detection.
427 default: // Impossible
428 log_error(_("Host endianness not detected in AudioDecoderSimple"));
429 // Just carry on anyway...
430 case 0x01: // Little-endian host: sample is already native.
431 // If the input data is the output data, then we probably
432 // can't move the data faster than memcpy.
433 memcpy((char *)decodedData
, input
, inputSize
);
435 case 0x00: // Big-endian host
436 // Swap sample bytes to get big-endian format.
437 assert((inputSize
& 1) == 0);
439 // Cast the buffers to help the compiler understand that we
440 // swapping 16-bit words. This should produce a single-instruction
441 // swap for each 16-bit word.
442 const boost::uint16_t* input16
= reinterpret_cast<const boost::uint16_t*>(input
);
443 boost::uint16_t* decodedData16
= reinterpret_cast<boost::uint16_t*>(decodedData
);
444 unsigned inputSize16
= inputSize
/ sizeof(boost::uint16_t);
445 for ( unsigned i
= 0; i
< inputSize16
; i
++ )
447 boost::uint16_t sample
= input16
[i
];
448 decodedData16
[i
] = ((sample
<< 8) & 0xFF00) | ((sample
>> 8) & 0x00FF);
456 // ???, this should only decode ADPCM, RAW and UNCOMPRESSED
459 boost::uint8_t* tmp_raw_buffer
= decodedData
;
460 boost::uint32_t tmp_raw_buffer_size
= 0;
462 // If we need to convert samplerate or/and from mono to stereo...
463 if (outsize
> 0 && (_sampleRate
!= 44100 || !_stereo
)) {
465 boost::int16_t* adjusted_data
= 0;
466 int adjusted_size
= 0;
467 int sample_count
= outsize
/ (_stereo
? 4 : 2); // samples are of size 2
469 // Convert to needed samplerate - this converter only support standard flash samplerates
470 AudioResampler::convert_raw_data(&adjusted_data
, &adjusted_size
,
472 sample_count
, 2, // input sample size is 2 !
473 _sampleRate
, _stereo
,
474 44100, true /* stereo */);
476 // Hopefully this wont happen
477 if (!adjusted_data
) {
478 log_error(_("Error in sound sample conversion"));
479 delete[] tmp_raw_buffer
;
485 // Move the new data to the sound-struct
486 delete[] tmp_raw_buffer
;
487 tmp_raw_buffer
= reinterpret_cast<boost::uint8_t*>(adjusted_data
);
488 tmp_raw_buffer_size
= adjusted_size
;
491 tmp_raw_buffer_size
= outsize
;
494 outputSize
= tmp_raw_buffer_size
;
496 decodedBytes
= inputSize
;
497 return tmp_raw_buffer
;
500 } // gnash.media namespace