Include program counter on action limit notification log
[gnash.git] / libmedia / AudioDecoderSimple.cpp
blob16e109a54c2df80d24df613bff60ac4599964c72
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::int16_t *out_data = new boost::int16_t[input_size];
270 // Convert 8-bit to 16
271 const boost::uint8_t *inp = input;
272 boost::int16_t *outp = out_data;
273 for (unsigned int i = input_size; i>0; i--) {
274 *outp++ = ((boost::int16_t)(*inp++) - 128) * 256;
277 data = (unsigned char *)out_data;
281 AudioDecoderSimple::AudioDecoderSimple(const AudioInfo& info)
283 _sampleRate(0),
284 _sampleCount(0),
285 _stereo(false),
286 _is16bit(true)
288 setup(info);
290 log_debug(_("AudioDecoderSimple: initialized flash codec %s (%d)"),
291 (int)_codec, _codec);
294 AudioDecoderSimple::AudioDecoderSimple(const SoundInfo& info)
296 _sampleRate(0),
297 _sampleCount(0),
298 _stereo(false),
299 _is16bit(true)
301 setup(info);
303 log_debug(_("AudioDecoderSimple: initialized flash codec %s (%d)"),
304 (int)_codec, _codec);
308 AudioDecoderSimple::~AudioDecoderSimple()
312 void
313 AudioDecoderSimple::setup(const SoundInfo& info)
315 _codec = info.getFormat();
316 switch (_codec)
318 case AUDIO_CODEC_ADPCM:
319 case AUDIO_CODEC_RAW:
320 case AUDIO_CODEC_UNCOMPRESSED:
321 _sampleRate = info.getSampleRate();
322 _sampleCount = info.getSampleCount();
323 _stereo = info.isStereo();
324 _is16bit = info.is16bit();
325 break;
327 default:
328 boost::format err = boost::format(
329 _("AudioDecoderSimple: unsupported flash codec %d (%s)"))
330 % (int)_codec % _codec;
331 throw MediaException(err.str());
335 void
336 AudioDecoderSimple::setup(const AudioInfo& info)
338 if (info.type != CODEC_TYPE_FLASH) {
339 boost::format err = boost::format(
340 _("AudioDecoderSimple: unable to intepret custom audio codec id %s"))
341 % info.codec;
342 throw MediaException(err.str());
345 _codec = static_cast<audioCodecType>(info.codec);
346 switch (_codec)
348 case AUDIO_CODEC_ADPCM:
349 case AUDIO_CODEC_RAW:
350 case AUDIO_CODEC_UNCOMPRESSED:
351 _sampleRate = info.sampleRate;
352 _stereo = info.stereo;
353 _is16bit = (info.sampleSize==2); // check this!
354 if ( info.sampleSize > 2 ) // troubles...
355 log_unimpl("Sample size > 2 in %s sound!", _codec);
356 break;
358 default:
359 boost::format err = boost::format(
360 _("AudioDecoderSimple: unsupported flash codec %d (%s)"))
361 % (int)_codec % _codec;
362 throw MediaException(err.str());
366 boost::uint8_t*
367 AudioDecoderSimple::decode(const boost::uint8_t* input, boost::uint32_t inputSize,
368 boost::uint32_t& outputSize, boost::uint32_t& decodedBytes,
369 bool /*parse*/)
372 unsigned char* decodedData = NULL;
373 int outsize = 0;
375 switch (_codec) {
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);
383 break;
384 case AUDIO_CODEC_RAW:
385 if (_is16bit) {
386 // FORMAT_RAW 16-bit is exactly what we want!
387 decodedData = new unsigned char[inputSize];
388 memcpy(decodedData, input, inputSize);
389 outsize = inputSize;
390 } else {
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);
396 break;
397 case AUDIO_CODEC_UNCOMPRESSED:
398 // 8- or 16-bit mono or stereo little-endian audio
399 // Convert to 16-bit host-endian.
400 if (!_is16bit)
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);
407 } else {
408 // Read 16-bit data into buffer
409 decodedData = new unsigned char[inputSize];
410 memcpy((char *)decodedData, input, inputSize);
412 // Convert 16-bit little-endian data to host-endian.
414 // Runtime detection of host endianness costs almost
415 // nothing and is less of a continual maintenance headache
416 // than compile-time detection.
417 union u {
418 boost::uint16_t s;
419 struct {
420 boost::uint8_t c0;
421 boost::uint8_t c1;
422 } c;
423 } u = { 0x0001 };
425 switch (u.c.c0) {
426 case 0x01: // Little-endian host: sample is already native.
427 break;
428 case 0x00: // Big-endian host
429 // Swap sample bytes to get big-endian format.
430 assert((inputSize & 1) == 0);
431 for (unsigned i = 0; i < inputSize; i+=2)
433 std::swap(decodedData[i], decodedData[i+1]);
435 break;
436 default: // Impossible
437 log_error(_("Host endianness not detected in AudioDecoderSimple"));
438 // Just carry on anyway...
441 break;
442 default:
443 break;
444 // ???, this should only decode ADPCM, RAW and UNCOMPRESSED
447 boost::uint8_t* tmp_raw_buffer = decodedData;
448 boost::uint32_t tmp_raw_buffer_size = 0;
450 // If we need to convert samplerate or/and from mono to stereo...
451 if (outsize > 0 && (_sampleRate != 44100 || !_stereo)) {
453 boost::int16_t* adjusted_data = 0;
454 int adjusted_size = 0;
455 int sample_count = outsize / (_stereo ? 4 : 2); // samples are of size 2
457 // Convert to needed samplerate - this converter only support standard flash samplerates
458 AudioResampler::convert_raw_data(&adjusted_data, &adjusted_size,
459 tmp_raw_buffer,
460 sample_count, 2, // input sample size is 2 !
461 _sampleRate, _stereo,
462 44100, true /* stereo */);
464 // Hopefully this wont happen
465 if (!adjusted_data) {
466 log_error(_("Error in sound sample conversion"));
467 delete[] tmp_raw_buffer;
468 outputSize = 0;
469 decodedBytes = 0;
470 return NULL;
473 // Move the new data to the sound-struct
474 delete[] tmp_raw_buffer;
475 tmp_raw_buffer = reinterpret_cast<boost::uint8_t*>(adjusted_data);
476 tmp_raw_buffer_size = adjusted_size;
478 } else {
479 tmp_raw_buffer_size = outsize;
482 outputSize = tmp_raw_buffer_size;
484 decodedBytes = inputSize;
485 return tmp_raw_buffer;
488 } // gnash.media namespace
489 } // gnash namespace