1 /**
2 \file ADM_ad_ima_adpcm.cpp
3 \brief Audio decoders built ulaw from mplayer or ffmpeg (??, can't remember)
4 \author mean (c) 2009
5
6 */
7 /*
8 IMA ADPCM Decoder for MPlayer
9 by Mike Melanson
10
11 This file is in charge of decoding all of the various IMA ADPCM data
12 formats that various entities have created. Details about the data
13 formats can be found here:
14 http://www.pcisys.net/~melanson/codecs/
15
16 So far, this file handles these formats:
17 'ima4': IMA ADPCM found in QT files
18 0x11: IMA ADPCM found in MS AVI/ASF/WAV files
19 0x61: DK4 ADPCM found in certain AVI files on Sega Saturn CD-ROMs;
20 note that this is a 'rogue' format number in that it was
21 never officially registered with Microsoft
22 0x1100736d: IMA ADPCM coded like in MS AVI/ASF/WAV found in QT files
23 */
24
25 /***************************************************************************
26 * *
27 * This program is free software; you can redistribute it and/or modify *
28 * it under the terms of the GNU General Public License as published by *
29 * the Free Software Foundation; either version 2 of the License, or *
30 * (at your option) any later version. *
31 * *
32 ***************************************************************************/
33 #include "ADM_default.h"
34 #include <math.h>
35
36 #include "libavutil/bswap.h"
37 #include "ADM_ad_plugin.h"
38
39 /**
40 \class ADM_AudiocodecImaAdpcm
41 \brief
42 */
43
44
45 #define IMA_BUFFER 4096*8
46 #define SCRATCH_PAD_SIZE (100*1000*2)
47 class ADM_AudiocodecImaAdpcm : public ADM_Audiocodec
48 {
49 protected:
50 uint32_t _inStock,_me,_channels;
51 int ss_div,ss_mul; // ???
52 void *_contextVoid;
53 uint8_t _buffer[ IMA_BUFFER];
54 uint32_t _head,_tail;
55 uint8_t scratchPad[SCRATCH_PAD_SIZE];
56
57 public:
58 ADM_AudiocodecImaAdpcm(uint32_t fourcc, WAVHeader *info, uint32_t l, uint8_t *d);
59 virtual ~ADM_AudiocodecImaAdpcm();
resetAfterSeek(void)60 virtual bool resetAfterSeek(void) {_head=_tail=0;return 1;}
61 virtual uint8_t run(uint8_t *inptr, uint32_t nbIn, float *outptr, uint32_t *nbOut);
isCompressed(void)62 virtual uint8_t isCompressed(void) {return 1;}
63 };
64 // Supported formats + declare our plugin
65 //*******************************************************
66
67 static ad_supportedFormat Formats[]={
68 {WAV_IMAADPCM,AD_MEDIUM_QUAL},
69
70 };
71 DECLARE_AUDIO_DECODER(ADM_AudiocodecImaAdpcm, // Class
72 0,0,1, // Major, minor,patch
73 Formats, // Supported formats
74 "IMA ADPCM decoder plugin for avidemux (c) Mean\n"); // Desc
75 //********************************************************
76
77
78 #define MS_IMA_ADPCM_PREAMBLE_SIZE 4
79
80 #define QT_IMA_ADPCM_PREAMBLE_SIZE 2
81 #define QT_IMA_ADPCM_BLOCK_SIZE 0x22
82 #define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64
83
84 #define BE_16(x) (be2me16(*(unsigned short *)(x)))
85 #define BE_32(x) (be2me32(*(unsigned int *)(x)))
86 #define LE_16(x) (av_le2ne16(*(unsigned short *)(x)))
87 #define LE_32(x) (av_le2ne32(*(unsigned int *)(x)))
88
89 int ms_ima_adpcm_decode_block(unsigned short *output,
90 unsigned char *input, int channels, int block_size);
91
92 // pertinent tables for IMA ADPCM
93 static int adpcm_step[89] =
94 {
95 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
96 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
97 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
98 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
99 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
100 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
101 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
102 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
103 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
104 };
105
106 static int adpcm_index[16] =
107 {
108 -1, -1, -1, -1, 2, 4, 6, 8,
109 -1, -1, -1, -1, 2, 4, 6, 8
110 };
111
112 // useful macros
113 // clamp a number between 0 and 88
114 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88;
115 // clamp a number within a signed 16-bit range
116 #define CLAMP_S16(x) if (x < -32768) x = -32768; \
117 else if (x > 32767) x = 32767;
118 // clamp a number above 16
119 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
120 // sign extend a 16-bit value
121 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000;
122 // sign extend a 4-bit value
123 #define SE_4BIT(x) if (x & 0x8) x -= 0x10;
124
125 // static ad_info_t info =
126 // {
127 // "IMA ADPCM audio decoder",
128 // "imaadpcm",
129 // "Nick Kurshev",
130 // "Mike Melanson",
131 // ""
132 // };
133
ADM_AudiocodecImaAdpcm(uint32_t fourcc,WAVHeader * info,uint32_t l,uint8_t * d)134 ADM_AudiocodecImaAdpcm::ADM_AudiocodecImaAdpcm( uint32_t fourcc, WAVHeader *info, uint32_t l, uint8_t *d)
135 : ADM_Audiocodec(fourcc,*info)
136 {
137 _me=info->encoding;
138 _channels=info->channels;
139
140 if ((info->encoding == WAV_IMAADPCM) || (info->encoding == 0x61)
141 //|| (info->encoding == (uint16_t)0x1100736d)
142 )
143 {
144 ss_div = info->blockalign -
145 (MS_IMA_ADPCM_PREAMBLE_SIZE * info->channels) * 2;
146 ss_mul = info->blockalign;
147 }
148 else
149 {
150 ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK;
151 ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * info->channels;
152 }
153 //sh_audio->audio_in_minsize=sss_mul;
154 _tail=_head=0;
155 printf("Block size: %d\n",ss_mul);
156 }
~ADM_AudiocodecImaAdpcm()157 ADM_AudiocodecImaAdpcm::~ADM_AudiocodecImaAdpcm()
158 {
159
160 }
161
run(uint8_t * inptr,uint32_t nbIn,float * outptr,uint32_t * nbOut)162 uint8_t ADM_AudiocodecImaAdpcm::run(uint8_t *inptr, uint32_t nbIn, float *outptr, uint32_t *nbOut)
163 {
164 int produced=0,one;
165 uint8_t *start;
166 int16_t *run16;
167 // Add to buffer
168
169 ADM_assert((_tail+nbIn)<IMA_BUFFER);
170 memcpy(&(_buffer[_tail]),inptr,nbIn);
171 _tail+=nbIn;
172 *nbOut=0;
173
174 if((_tail-_head)<ss_mul)
175 return 0;
176
177
178 if (_me== WAV_IMAADPCM)// || (sh_audio->format == 0x1100736d))
179 {
180 while((_tail-_head)>=ss_mul)
181 {
182 start=(uint8_t *)&(_buffer[_head]);
183 one= ms_ima_adpcm_decode_block(
184 (unsigned short *)scratchPad,start,_channels , ss_mul);
185 _head+=ss_mul;
186 produced+=one;
187 run16=(int16_t *)scratchPad;
188 for(int i=0;i<one;i++)
189 {
190 *outptr++=((float)run16[i])/32767.;
191 }
192 }
193 if(_tail>IMA_BUFFER/2 && _head)
194 {
195 memmove(_buffer,&_buffer[_head],_tail-_head);
196 _tail-=_head;
197 _head=0;
198 }
199 *nbOut=produced;
200 return 1;
201 }
202 #if 0
203 else if (_me == 0x61)
204 {
205 return 2 * dk4_ima_adpcm_decode_block(
206 (unsigned short*)outptr, ptr, _channels, ss_mul);
207 }
208 else
209 {
210 return 2 * qt_ima_adpcm_decode_block(
211 (unsigned short*)outptr, ptr, _channels);
212 }
213 #endif
214 return 0;
215 }
216
217
decode_nibbles(unsigned short * output,int output_size,int channels,int predictor_l,int index_l,int predictor_r,int index_r)218 static void decode_nibbles(unsigned short *output,
219 int output_size, int channels,
220 int predictor_l, int index_l,
221 int predictor_r, int index_r)
222 {
223 int step[2];
224 int predictor[2];
225 int index[2];
226 int diff;
227 int i;
228 int sign;
229 int delta;
230 int channel_number = 0;
231
232 step[0] = adpcm_step[index_l];
233 step[1] = adpcm_step[index_r];
234 predictor[0] = predictor_l;
235 predictor[1] = predictor_r;
236 index[0] = index_l;
237 index[1] = index_r;
238
239 for (i = 0; i < output_size; i++)
240 {
241 delta = output[i];
242
243 index[channel_number] += adpcm_index[delta];
244 CLAMP_0_TO_88(index[channel_number]);
245
246 sign = delta & 8;
247 delta = delta & 7;
248
249 diff = step[channel_number] >> 3;
250 if (delta & 4) diff += step[channel_number];
251 if (delta & 2) diff += step[channel_number] >> 1;
252 if (delta & 1) diff += step[channel_number] >> 2;
253
254 if (sign)
255 predictor[channel_number] -= diff;
256 else
257 predictor[channel_number] += diff;
258
259 CLAMP_S16(predictor[channel_number]);
260 output[i] = predictor[channel_number];
261 step[channel_number] = adpcm_step[index[channel_number]];
262
263 // toggle channel
264 channel_number ^= channels - 1;
265
266 }
267 }
268
ms_ima_adpcm_decode_block(unsigned short * output,unsigned char * input,int channels,int block_size)269 int ms_ima_adpcm_decode_block(unsigned short *output,
270 unsigned char *input, int channels, int block_size)
271 {
272 int predictor_l = 0;
273 int predictor_r = 0;
274 int index_l = 0;
275 int index_r = 0;
276 int i;
277 int channel_counter;
278 int channel_index;
279 int channel_index_l;
280 int channel_index_r;
281
282 predictor_l = LE_16(&input[0]);
283 SE_16BIT(predictor_l);
284 index_l = input[2];
285 if (channels == 2)
286 {
287 predictor_r = LE_16(&input[4]);
288 SE_16BIT(predictor_r);
289 index_r = input[6];
290 }
291
292 if (channels == 1)
293 for (i = 0;
294 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++)
295 {
296 output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F;
297 output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4;
298 }
299 else
300 {
301 // encoded as 8 nibbles (4 bytes) per channel; switch channel every
302 // 4th byte
303 channel_counter = 0;
304 channel_index_l = 0;
305 channel_index_r = 1;
306 channel_index = channel_index_l;
307 for (i = 0;
308 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++)
309 {
310 output[channel_index + 0] =
311 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F;
312 output[channel_index + 2] =
313 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4;
314 channel_index += 4;
315 channel_counter++;
316 if (channel_counter == 4)
317 {
318 channel_index_l = channel_index;
319 channel_index = channel_index_r;
320 }
321 else if (channel_counter == 8)
322 {
323 channel_index_r = channel_index;
324 channel_index = channel_index_l;
325 channel_counter = 0;
326 }
327 }
328 }
329
330 decode_nibbles(output,
331 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2,
332 channels,
333 predictor_l, index_l,
334 predictor_r, index_r);
335
336 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2;
337 }
338
339
340 // EOF
341