1 // AudioDecoderSimple.cpp: Audio decoding using "simple" internal decoders.
2 //
3 //   Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 //   Free Software Foundation, Inc.
5 //
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.
10 //
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.
15 //
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
19 //
20 
21 
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
28 
29 #include "log.h"
30 
31 #include <algorithm> // for std::swap
32 
33 namespace gnash {
34 namespace media {
35 
36 // ----------------------------------------------------------------------------
37 // ADPCMDecoder class
38 // ----------------------------------------------------------------------------
39 
40 /// ADPCM decoder utilities
41 //
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 {
50 
51 private:
52 
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];
58 
59 	static int* s_index_update_tables[4];
60 
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];
65 
66 
doSample(int n_bits,int & sample,int & stepsize_index,int raw_code)67 	static void doSample(int n_bits, int& sample, int& stepsize_index, int raw_code)
68 	{
69 		assert(raw_code >= 0 && raw_code < (1 << n_bits));
70 
71 		static const int	HI_BIT = (1 << (n_bits - 1));
72 		int*	index_update_table = s_index_update_tables[n_bits - 2];
73 
74 		/* Core of ADPCM. */
75 
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)*/
79 
80 		int	stepsize = s_stepsize[stepsize_index];
81 
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;
88 
89 		sample += delta;
90 		sample = clamp<int>(sample, -32768, 32767);
91 
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);
95 	}
96 
97 	/* Uncompress 4096 mono samples of ADPCM. */
doMonoBlock(std::int16_t ** out_data,int n_bits,BitsReader & in,int sample,int stepsize_index)98 	static std::uint32_t doMonoBlock(std::int16_t** out_data, int n_bits, BitsReader& in, int sample, int stepsize_index)
99 	{
100 		/* First sample doesn't need to be decompressed. */
101 		std::uint32_t sample_count = 1;
102 		*(*out_data)++ = (std::int16_t) sample;
103 
104 		while (sample_count < 4096 && in.gotBits(n_bits))
105 		{
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)++ = (std::int16_t) sample;
109 
110 			sample_count++;
111 		}
112 		return sample_count;
113 	}
114 
115 
116 	/* Uncompress 4096 stereo sample pairs of ADPCM. */
doStereoBlock(std::int16_t ** out_data,int n_bits,BitsReader & in,int left_sample,int left_stepsize_index,int right_sample,int right_stepsize_index)117 	static int doStereoBlock(
118 			std::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
125 			)
126 	{
127 		/* First samples don't need to be decompressed. */
128 		std::uint32_t sample_count = 2;
129 		*(*out_data)++ = (std::int16_t) left_sample;
130 		*(*out_data)++ = (std::int16_t) right_sample;
131 
132 		unsigned bitsNeeded = n_bits*2;
133 		while (sample_count < 4096 && in.gotBits(bitsNeeded))
134 		{
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)++ = (std::int16_t) left_sample;
138 
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)++ = (std::int16_t) right_sample;
142 
143 			sample_count++;
144 		}
145 		return sample_count;
146 	}
147 
148 public:
149 
150 	// Utility function: uncompress ADPCM data from in BitReader to
151 	// out_data[]. Returns the output samplecount.
adpcm_expand(unsigned char * & data,BitsReader & in,unsigned int insize,bool stereo)152 	static std::uint32_t adpcm_expand(
153 		unsigned char* &data,
154 		BitsReader& in,
155 		unsigned int insize,
156 		bool stereo)
157 	{
158 		// Read header.
159 		if ( ! in.gotBits(2) )
160 		{
161 			IF_VERBOSE_MALFORMED_SWF(
162 			log_swferror(_("corrupted ADPCM header"));
163 			);
164 			return 0;
165 		}
166 		unsigned int n_bits = in.read_uint(2) + 2; // 2 to 5 bits
167 
168 		// The compression ratio is 4:1, so this should be enough...
169 		std::int16_t* out_data = new std::int16_t[insize * 5];
170 		data = reinterpret_cast<unsigned char *>(out_data);
171 
172 		std::uint32_t sample_count = 0;
173 
174 		while ( in.gotBits(22) )
175 		{
176 			// Read initial sample & index values.
177 
178 			int	sample = in.read_sint(16);
179 
180 			int	stepsize_index = in.read_uint(6);
181 			assert(STEPSIZE_CT >= (1 << 6));	// ensure we don't need to clamp.
182 
183 			if (stereo == false)
184 			{
185 
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);
196 				}
197 			}
198 			else
199 			{
200 				// Stereo.
201 
202 				// Got values for left channel; now get initial sample
203 				// & index for right channel.
204 				int	right_sample = in.read_sint(16);
205 
206 				int	right_stepsize_index = in.read_uint(6);
207 				assert(STEPSIZE_CT >= (1 << 6));	// ensure we don't need to clamp.
208 
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);
219 				}
220 
221 			}
222 		}
223 		return sample_count;
224 
225 	}
226 
227 };
228 
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 };
233 
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
239 };
240 
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
251 };
252 
253 // ----------------------------------------------------------------------------
254 // END OF ADPCMDecoder class
255 // ----------------------------------------------------------------------------
256 
257 //
258 // Unsigned 8-bit expansion (128 is silence)
259 //
260 // u8_expand allocates the memory for its "data" pointer.
261 //
262 
263 static void
u8_expand(unsigned char * & data,const unsigned char * input,std::uint32_t input_size)264 u8_expand(unsigned char * &data,
265 	const unsigned char* input,
266 	std::uint32_t input_size) // This is also the number of u8bit samples
267 {
268 	std::int16_t	*out_data = new std::int16_t[input_size];
269 
270 	// Convert 8-bit to 16
271 	const std::uint8_t *inp = input;
272 	std::int16_t *outp = out_data;
273 	for (unsigned int i = input_size; i>0; i--) {
274 		*outp++ = ((std::int16_t)(*inp++) - 128) * 256;
275 	}
276 
277 	data = (unsigned char *)out_data;
278 }
279 
280 
AudioDecoderSimple(const AudioInfo & info)281 AudioDecoderSimple::AudioDecoderSimple(const AudioInfo& info)
282 	:
283 	_sampleRate(0),
284 	_sampleCount(0),
285 	_stereo(false),
286 	_is16bit(true)
287 {
288     setup(info);
289 
290   	log_debug(_("AudioDecoderSimple: initialized flash codec %s (%d)"),
291 		(int)_codec, _codec);
292 }
293 
AudioDecoderSimple(const SoundInfo & info)294 AudioDecoderSimple::AudioDecoderSimple(const SoundInfo& info)
295 	:
296 	_sampleRate(0),
297 	_sampleCount(0),
298 	_stereo(false),
299 	_is16bit(true)
300 {
301     setup(info);
302 
303   	log_debug(_("AudioDecoderSimple: initialized flash codec %s (%d)"),
304 		(int)_codec, _codec);
305 }
306 
307 
~AudioDecoderSimple()308 AudioDecoderSimple::~AudioDecoderSimple()
309 {
310 }
311 
312 void
setup(const SoundInfo & info)313 AudioDecoderSimple::setup(const SoundInfo& info)
314 {
315 	_codec = info.getFormat();
316     switch (_codec)
317     {
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;
326 
327         default:
328             boost::format err = boost::format(
329                 _("AudioDecoderSimple: unsupported flash codec %d (%s)"))
330                 % (int)_codec % _codec;
331             throw MediaException(err.str());
332 	}
333 }
334 
335 void
setup(const AudioInfo & info)336 AudioDecoderSimple::setup(const AudioInfo& info)
337 {
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());
343     }
344 
345     _codec = static_cast<audioCodecType>(info.codec);
346     switch (_codec)
347     {
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;
357 
358         default:
359             boost::format err = boost::format(
360                 _("AudioDecoderSimple: unsupported flash codec %d (%s)"))
361                 % (int)_codec % _codec;
362             throw MediaException(err.str());
363 	}
364 }
365 
366 std::uint8_t*
decode(const std::uint8_t * input,std::uint32_t inputSize,std::uint32_t & outputSize,std::uint32_t & decodedBytes)367 AudioDecoderSimple::decode(const std::uint8_t* input, std::uint32_t inputSize,
368         std::uint32_t& outputSize, std::uint32_t& decodedBytes)
369 {
370 
371 	unsigned char* decodedData = nullptr;
372 	std::uint32_t outsize = 0;
373 
374     switch (_codec) {
375 	case AUDIO_CODEC_ADPCM:
376 		{
377 		//std::uint32_t sample_count = inputSize * ( _stereo ? 1 : 2 ); //(_sampleCount == 0 ? inputSize / ( _stereo ? 4 : 2 ) : _sampleCount);
378 		BitsReader br(input, inputSize);
379 		std::uint32_t sample_count = ADPCMDecoder::adpcm_expand(decodedData, br, inputSize, _stereo);
380 		outsize = sample_count * (_stereo ? 4 : 2);
381 		}
382 		break;
383 	case AUDIO_CODEC_RAW:
384 		if (_is16bit) {
385 			// FORMAT_RAW 16-bit is exactly what we want!
386 			decodedData = new unsigned char[inputSize];
387 			memcpy(decodedData, input, inputSize);
388 			outsize = inputSize;
389 		} else {
390 			// Convert 8-bit unsigned to 16-bit signed range
391 			// Allocate as many shorts as there are samples
392 			u8_expand(decodedData, input, inputSize);
393 			outsize = inputSize * 2;
394 		}
395 		break;
396 	case AUDIO_CODEC_UNCOMPRESSED:
397 		// 8- or 16-bit mono or stereo little-endian audio
398 		// Convert to 16-bit host-endian.
399 		if (!_is16bit)
400 		{
401 			// Convert 8-bit unsigned to 16-bit signed range
402 			// Allocate as many shorts as there are 8-bit samples
403 			u8_expand(decodedData, input, inputSize);
404 			outsize = inputSize * 2;
405 
406 		} else {
407 			// Allocate a destination buffer
408 			// Read 16-bit data into buffer
409 			decodedData = new unsigned char[inputSize];
410 			outsize = inputSize;
411 
412 			// Convert 16-bit little-endian data to host-endian.
413 
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 				std::uint16_t s;
419 				struct {
420 					std::uint8_t c0;
421 					std::uint8_t c1;
422 				} c;
423 			} u = { 0x0001 };
424 
425 			switch (u.c.c0) {
426 				default:	// Impossible
427 					log_error(_("Host endianness not detected in AudioDecoderSimple"));
428 					// Just carry on anyway...
429 				case 0x01:	// Little-endian host: sample is already native.
430 					// If the input data is the output data, then we probably
431 					// can't move the data faster than memcpy.
432 					memcpy((char *)decodedData, input, inputSize);
433 					break;
434 				case 0x00:  // Big-endian host
435 					// Swap sample bytes to get big-endian format.
436 					assert((inputSize & 1) == 0);
437 
438 					// Cast the buffers to help the compiler understand that we
439 					// swapping 16-bit words. This should produce a single-instruction
440 					// swap for each 16-bit word.
441 					const std::uint16_t* input16 = reinterpret_cast<const std::uint16_t*>(input);
442 					std::uint16_t* decodedData16 = reinterpret_cast<std::uint16_t*>(decodedData);
443 					unsigned inputSize16 = inputSize / sizeof(std::uint16_t);
444 					for ( unsigned i = 0; i < inputSize16; i++ )
445 					{
446 						std::uint16_t sample = input16[i];
447 						decodedData16[i] = ((sample << 8) & 0xFF00) | ((sample >> 8) & 0x00FF);
448 					}
449 					break;
450 			}
451 		}
452 		break;
453 		default:
454 		break;
455 		// ???, this should only decode ADPCM, RAW and UNCOMPRESSED
456 	}
457 
458 	std::uint8_t* tmp_raw_buffer = decodedData;
459 	std::uint32_t tmp_raw_buffer_size = 0;
460 
461 	// If we need to convert samplerate or/and from mono to stereo...
462 	if (outsize > 0 && (_sampleRate != 44100 || !_stereo)) {
463 
464 		std::int16_t* adjusted_data = nullptr;
465 		int	adjusted_size = 0;
466 		int sample_count = outsize / (_stereo ? 4 : 2); // samples are of size 2
467 
468 		// Convert to needed samplerate - this converter only support standard flash samplerates
469 		AudioResampler::convert_raw_data(&adjusted_data, &adjusted_size,
470                 tmp_raw_buffer,
471                 sample_count, 2,  // input sample size is 2 !
472 				_sampleRate, _stereo,
473 				44100,  true /* stereo */);
474 
475 		// Hopefully this wont happen
476 		if (!adjusted_data) {
477 			log_error(_("Error in sound sample conversion"));
478 			delete[] tmp_raw_buffer;
479 			outputSize = 0;
480 			decodedBytes = 0;
481 			return nullptr;
482 		}
483 
484 		// Move the new data to the sound-struct
485 		delete[] tmp_raw_buffer;
486 		tmp_raw_buffer = reinterpret_cast<std::uint8_t*>(adjusted_data);
487 		tmp_raw_buffer_size = adjusted_size;
488 
489 	} else {
490 		tmp_raw_buffer_size = outsize;
491 	}
492 
493 	outputSize = tmp_raw_buffer_size;
494 
495 	decodedBytes = inputSize;
496 	return tmp_raw_buffer;
497 }
498 
499 } // gnash.media namespace
500 } // gnash namespace
501