Backslash ${prefix} for kde3 too...
[gnash.git] / libmedia / AudioDecoderSimple.cpp
blobb88d9d792491ecfba60ffb87141d8da107aedeaa
1 // AudioDecoderSimple.cpp: Audio decoding using "simple" internal decoders.
2 //
3 // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "AudioDecoderSimple.h"
22 #include "AudioResampler.h"
23 #include "BitsReader.h"
24 #include "SoundInfo.h"
25 #include "MediaParser.h" // for AudioInfo definition..
26 #include "GnashNumeric.h" // for clamp
28 #include "log.h"
30 #include <boost/scoped_array.hpp>
31 #include <algorithm> // for std::swap
33 namespace gnash {
34 namespace media {
36 // ----------------------------------------------------------------------------
37 // ADPCMDecoder class
38 // ----------------------------------------------------------------------------
40 /// ADPCM decoder utilities
42 /// Algo from http://www.circuitcellar.com/pastissues/articles/richey110/text.htm
43 /// And also Jansen.
44 /// Here's another reference: http://www.geocities.com/SiliconValley/8682/aud3.txt
45 /// Original IMA spec doesn't seem to be on the web :(
46 ///
47 /// TODO: move in it's own file
48 ///
49 class ADPCMDecoder {
51 private:
53 // Data from Alexis' SWF reference
54 static int _index_update_table_2bits[2];
55 static int _index_update_table_3bits[4];
56 static int _index_update_table_4bits[8];
57 static int _index_update_table_5bits[16];
59 static int* s_index_update_tables[4];
61 // Data from Jansen. http://homepages.cwi.nl/~jack/
62 // Check out his Dutch retro punk songs, heh heh :)
63 static const int STEPSIZE_CT = 89;
64 static int s_stepsize[STEPSIZE_CT];
67 static void doSample(int n_bits, int& sample, int& stepsize_index, int raw_code)
69 assert(raw_code >= 0 && raw_code < (1 << n_bits));
71 static const int HI_BIT = (1 << (n_bits - 1));
72 int* index_update_table = s_index_update_tables[n_bits - 2];
74 /* Core of ADPCM. */
76 int code_mag = raw_code & (HI_BIT - 1);
77 bool code_sign_bit = (raw_code & HI_BIT) ? 1 : 0;
78 int mag = (code_mag << 1) + 1; /* shift in LSB (they do this so that pos & neg zero are different)*/
80 int stepsize = s_stepsize[stepsize_index];
82 /* Compute the new sample. It's the predicted value */
83 /* (i.e. the previous value), plus a delta. The delta */
84 /* comes from the code times the stepsize. going for */
85 /* something like: delta = stepsize * (code * 2 + 1) >> code_bits */
86 int delta = (stepsize * mag) >> (n_bits - 1);
87 if (code_sign_bit) delta = -delta;
89 sample += delta;
90 sample = clamp<int>(sample, -32768, 32767);
92 /* Update our stepsize index. Use a lookup table. */
93 stepsize_index += index_update_table[code_mag];
94 stepsize_index = clamp<int>(stepsize_index, 0, STEPSIZE_CT - 1);
97 /* Uncompress 4096 mono samples of ADPCM. */
98 static boost::uint32_t doMonoBlock(boost::int16_t** out_data, int n_bits, BitsReader& in, int sample, int stepsize_index)
100 /* First sample doesn't need to be decompressed. */
101 boost::uint32_t sample_count = 1;
102 *(*out_data)++ = (boost::int16_t) sample;
104 while (sample_count < 4096 && in.gotBits(n_bits))
106 int raw_code = in.read_uint(n_bits);
107 doSample(n_bits, sample, stepsize_index, raw_code); /* sample & stepsize_index are in/out params */
108 *(*out_data)++ = (boost::int16_t) sample;
110 sample_count++;
112 return sample_count;
116 /* Uncompress 4096 stereo sample pairs of ADPCM. */
117 static int doStereoBlock(
118 boost::int16_t** out_data, // in/out param
119 int n_bits,
120 BitsReader& in,
121 int left_sample,
122 int left_stepsize_index,
123 int right_sample,
124 int right_stepsize_index
127 /* First samples don't need to be decompressed. */
128 boost::uint32_t sample_count = 2;
129 *(*out_data)++ = (boost::int16_t) left_sample;
130 *(*out_data)++ = (boost::int16_t) right_sample;
132 unsigned bitsNeeded = n_bits*2;
133 while (sample_count < 4096 && in.gotBits(bitsNeeded))
135 int left_raw_code = in.read_uint(n_bits);
136 doSample(n_bits, left_sample, left_stepsize_index, left_raw_code);
137 *(*out_data)++ = (boost::int16_t) left_sample;
139 int right_raw_code = in.read_uint(n_bits);
140 doSample(n_bits, right_sample, right_stepsize_index, right_raw_code);
141 *(*out_data)++ = (boost::int16_t) right_sample;
143 sample_count++;
145 return sample_count;
148 public:
150 // Utility function: uncompress ADPCM data from in BitReader to
151 // out_data[]. Returns the output samplecount.
152 static boost::uint32_t adpcm_expand(
153 unsigned char* &data,
154 BitsReader& in,
155 unsigned int insize,
156 bool stereo)
158 // Read header.
159 if ( ! in.gotBits(2) )
161 IF_VERBOSE_MALFORMED_SWF(
162 log_swferror(_("corrupted ADPCM header"));
164 return 0;
166 unsigned int n_bits = in.read_uint(2) + 2; // 2 to 5 bits
168 // The compression ratio is 4:1, so this should be enough...
169 boost::int16_t* out_data = new boost::int16_t[insize * 5];
170 data = reinterpret_cast<unsigned char *>(out_data);
172 boost::uint32_t sample_count = 0;
174 while ( in.gotBits(22) )
176 // Read initial sample & index values.
178 int sample = in.read_sint(16);
180 int stepsize_index = in.read_uint(6);
181 assert(STEPSIZE_CT >= (1 << 6)); // ensure we don't need to clamp.
183 if (stereo == false)
186 if (n_bits == 0) {
187 abort();
188 } else if (n_bits == 2) {
189 sample_count += doMonoBlock(&out_data, 2, in, sample, stepsize_index);
190 } else if (n_bits == 3) {
191 sample_count += doMonoBlock(&out_data, 3, in, sample, stepsize_index);
192 } else if (n_bits == 4) {
193 sample_count += doMonoBlock(&out_data, 4, in, sample, stepsize_index);
194 } else if (n_bits == 5) {
195 sample_count += doMonoBlock(&out_data, 5, in, sample, stepsize_index);
198 else
200 // Stereo.
202 // Got values for left channel; now get initial sample
203 // & index for right channel.
204 int right_sample = in.read_sint(16);
206 int right_stepsize_index = in.read_uint(6);
207 assert(STEPSIZE_CT >= (1 << 6)); // ensure we don't need to clamp.
209 if (n_bits == 0) {
210 abort();
211 } else if (n_bits == 2) {
212 sample_count += doStereoBlock(&out_data, 2, in, sample, stepsize_index, right_sample, right_stepsize_index);
213 } else if (n_bits == 3) {
214 sample_count += doStereoBlock(&out_data, 3, in, sample, stepsize_index, right_sample, right_stepsize_index);
215 } else if (n_bits == 4) {
216 sample_count += doStereoBlock(&out_data, 4, in, sample, stepsize_index, right_sample, right_stepsize_index);
217 } else if (n_bits == 5) {
218 sample_count += doStereoBlock(&out_data, 5, in, sample, stepsize_index, right_sample, right_stepsize_index);
223 return sample_count;
229 int ADPCMDecoder::_index_update_table_2bits[2] = { -1, 2 };
230 int ADPCMDecoder::_index_update_table_3bits[4] = { -1, -1, 2, 4 };
231 int ADPCMDecoder::_index_update_table_4bits[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
232 int ADPCMDecoder::_index_update_table_5bits[16] = { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 };
234 int* ADPCMDecoder::s_index_update_tables[4] = {
235 ADPCMDecoder::_index_update_table_2bits,
236 ADPCMDecoder::_index_update_table_3bits,
237 ADPCMDecoder::_index_update_table_4bits,
238 ADPCMDecoder::_index_update_table_5bits
241 int ADPCMDecoder::s_stepsize[STEPSIZE_CT] = {
242 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
243 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
244 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
245 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
246 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
247 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
248 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
249 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
250 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
253 // ----------------------------------------------------------------------------
254 // END OF ADPCMDecoder class
255 // ----------------------------------------------------------------------------
258 // Unsigned 8-bit expansion (128 is silence)
260 // u8_expand allocates the memory for its "data" pointer.
263 static void
264 u8_expand(unsigned char * &data,
265 const unsigned char* input,
266 boost::uint32_t input_size) // This is also the number of u8bit samples
268 boost::scoped_array<boost::uint8_t> in_data ( new boost::uint8_t[input_size] );
269 boost::int16_t *out_data = new boost::int16_t[input_size];
271 memcpy((char *)in_data.get(), input, input_size);
273 // Convert 8-bit to 16
274 boost::uint8_t *inp = in_data.get();
275 boost::int16_t *outp = out_data;
276 for (unsigned int i = input_size; i>0; i--) {
277 *outp++ = ((boost::int16_t)(*inp++) - 128) * 256;
280 data = (unsigned char *)out_data;
284 AudioDecoderSimple::AudioDecoderSimple(const AudioInfo& info)
286 _sampleRate(0),
287 _sampleCount(0),
288 _stereo(false),
289 _is16bit(true)
291 setup(info);
293 log_debug(_("AudioDecoderSimple: initialized flash codec %s (%d)"),
294 (int)_codec, _codec);
297 AudioDecoderSimple::AudioDecoderSimple(const SoundInfo& info)
299 _sampleRate(0),
300 _sampleCount(0),
301 _stereo(false),
302 _is16bit(true)
304 setup(info);
306 log_debug(_("AudioDecoderSimple: initialized flash codec %s (%d)"),
307 (int)_codec, _codec);
311 AudioDecoderSimple::~AudioDecoderSimple()
315 void
316 AudioDecoderSimple::setup(const SoundInfo& info)
318 _codec = info.getFormat();
319 switch (_codec)
321 case AUDIO_CODEC_ADPCM:
322 case AUDIO_CODEC_RAW:
323 case AUDIO_CODEC_UNCOMPRESSED:
324 _sampleRate = info.getSampleRate();
325 _sampleCount = info.getSampleCount();
326 _stereo = info.isStereo();
327 _is16bit = info.is16bit();
328 break;
330 default:
331 boost::format err = boost::format(
332 _("AudioDecoderSimple: unsupported flash codec %d (%s)"))
333 % (int)_codec % _codec;
334 throw MediaException(err.str());
338 void
339 AudioDecoderSimple::setup(const AudioInfo& info)
341 if (info.type != CODEC_TYPE_FLASH) {
342 boost::format err = boost::format(
343 _("AudioDecoderSimple: unable to intepret custom audio codec id %s"))
344 % info.codec;
345 throw MediaException(err.str());
348 _codec = static_cast<audioCodecType>(info.codec);
349 switch (_codec)
351 case AUDIO_CODEC_ADPCM:
352 case AUDIO_CODEC_RAW:
353 case AUDIO_CODEC_UNCOMPRESSED:
354 _sampleRate = info.sampleRate;
355 _stereo = info.stereo;
356 _is16bit = (info.sampleSize==2); // check this!
357 if ( info.sampleSize > 2 ) // troubles...
358 log_unimpl("Sample size > 2 in %s sound!", _codec);
359 break;
361 default:
362 boost::format err = boost::format(
363 _("AudioDecoderSimple: unsupported flash codec %d (%s)"))
364 % (int)_codec % _codec;
365 throw MediaException(err.str());
369 boost::uint8_t*
370 AudioDecoderSimple::decode(const boost::uint8_t* input, boost::uint32_t inputSize,
371 boost::uint32_t& outputSize, boost::uint32_t& decodedBytes,
372 bool /*parse*/)
375 unsigned char* decodedData = NULL;
376 int outsize = 0;
378 switch (_codec) {
379 case AUDIO_CODEC_ADPCM:
381 //boost::uint32_t sample_count = inputSize * ( _stereo ? 1 : 2 ); //(_sampleCount == 0 ? inputSize / ( _stereo ? 4 : 2 ) : _sampleCount);
382 BitsReader br(input, inputSize);
383 boost::uint32_t sample_count = ADPCMDecoder::adpcm_expand(decodedData, br, inputSize, _stereo);
384 outsize = sample_count * (_stereo ? 4 : 2);
386 break;
387 case AUDIO_CODEC_RAW:
388 if (_is16bit) {
389 // FORMAT_RAW 16-bit is exactly what we want!
390 decodedData = new unsigned char[inputSize];
391 memcpy(decodedData, input, inputSize);
392 outsize = inputSize;
393 } else {
394 // Convert 8-bit signed to 16-bit range
395 // Allocate as many shorts as there are samples
396 u8_expand(decodedData, input, inputSize);
397 outsize = inputSize * (_stereo ? 4 : 2);
399 break;
400 case AUDIO_CODEC_UNCOMPRESSED:
401 // 8- or 16-bit mono or stereo little-endian audio
402 // Convert to 16-bit host-endian.
403 if (!_is16bit)
405 // Convert 8-bit signed to 16-bit range
406 // Allocate as many shorts as there are 8-bit samples
407 u8_expand(decodedData, input, inputSize);
408 outsize = inputSize * (_stereo ? 4 : 2);
410 } else {
411 // Read 16-bit data into buffer
412 decodedData = new unsigned char[inputSize];
413 memcpy((char *)decodedData, input, inputSize);
415 // Convert 16-bit little-endian data to host-endian.
417 // Runtime detection of host endianness costs almost
418 // nothing and is less of a continual maintenance headache
419 // than compile-time detection.
420 union u {
421 boost::uint16_t s;
422 struct {
423 boost::uint8_t c0;
424 boost::uint8_t c1;
425 } c;
426 } u = { 0x0001 };
428 switch (u.c.c0) {
429 case 0x01: // Little-endian host: sample is already native.
430 break;
431 case 0x00: // Big-endian host
432 // Swap sample bytes to get big-endian format.
433 assert((inputSize & 1) == 0);
434 for (unsigned i = 0; i < inputSize; i+=2)
436 std::swap(decodedData[i], decodedData[i+1]);
438 break;
439 default: // Impossible
440 log_error(_("Host endianness not detected in AudioDecoderSimple"));
441 // Just carry on anyway...
444 break;
445 default:
446 break;
447 // ???, this should only decode ADPCM, RAW and UNCOMPRESSED
450 boost::uint8_t* tmp_raw_buffer = decodedData;
451 boost::uint32_t tmp_raw_buffer_size = 0;
453 // If we need to convert samplerate or/and from mono to stereo...
454 if (outsize > 0 && (_sampleRate != 44100 || !_stereo)) {
456 boost::int16_t* adjusted_data = 0;
457 int adjusted_size = 0;
458 int sample_count = outsize / (_stereo ? 4 : 2); // samples are of size 2
460 // Convert to needed samplerate - this converter only support standard flash samplerates
461 AudioResampler::convert_raw_data(&adjusted_data, &adjusted_size,
462 tmp_raw_buffer,
463 sample_count, 2, // input sample size is 2 !
464 _sampleRate, _stereo,
465 44100, true /* stereo */);
467 // Hopefully this wont happen
468 if (!adjusted_data) {
469 log_error(_("Error in sound sample conversion"));
470 delete[] tmp_raw_buffer;
471 outputSize = 0;
472 decodedBytes = 0;
473 return NULL;
476 // Move the new data to the sound-struct
477 delete[] tmp_raw_buffer;
478 tmp_raw_buffer = reinterpret_cast<boost::uint8_t*>(adjusted_data);
479 tmp_raw_buffer_size = adjusted_size;
481 } else {
482 tmp_raw_buffer_size = outsize;
485 outputSize = tmp_raw_buffer_size;
487 decodedBytes = inputSize;
488 return tmp_raw_buffer;
491 } // gnash.media namespace
492 } // gnash namespace