Mercurial > mplayer.hg
annotate libmpcodecs/ad_imaadpcm.c @ 27118:5798fb7fceb6
Simplify some imaadpcm macros
author | reimar |
---|---|
date | Sun, 29 Jun 2008 08:42:53 +0000 |
parents | 95495e636ba8 |
children | 7c07e13eeae3 |
rev | line source |
---|---|
5408 | 1 /* |
2 IMA ADPCM Decoder for MPlayer | |
3 by Mike Melanson | |
4 | |
5 This file is in charge of decoding all of the various IMA ADPCM data | |
6 formats that various entities have created. Details about the data | |
7 formats can be found here: | |
8 http://www.pcisys.net/~melanson/codecs/ | |
9 | |
10 So far, this file handles these formats: | |
11 'ima4': IMA ADPCM found in QT files | |
12 0x11: IMA ADPCM found in MS AVI/ASF/WAV files | |
13 0x61: DK4 ADPCM found in certain AVI files on Sega Saturn CD-ROMs; | |
14 note that this is a 'rogue' format number in that it was | |
15 never officially registered with Microsoft | |
8103 | 16 0x1100736d: IMA ADPCM coded like in MS AVI/ASF/WAV found in QT files |
5408 | 17 */ |
18 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
19 #include <stdio.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
20 #include <stdlib.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
21 #include <unistd.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
22 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
23 #include "config.h" |
21372 | 24 #include "libavutil/common.h" |
21507
fa99b3d31d13
Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents:
21372
diff
changeset
|
25 #include "mpbswap.h" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
26 #include "ad_internal.h" |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
27 |
5408 | 28 #define MS_IMA_ADPCM_PREAMBLE_SIZE 4 |
29 | |
30 #define QT_IMA_ADPCM_PREAMBLE_SIZE 2 | |
31 #define QT_IMA_ADPCM_BLOCK_SIZE 0x22 | |
32 #define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64 | |
33 | |
34 #define BE_16(x) (be2me_16(*(unsigned short *)(x))) | |
35 #define BE_32(x) (be2me_32(*(unsigned int *)(x))) | |
36 #define LE_16(x) (le2me_16(*(unsigned short *)(x))) | |
37 #define LE_32(x) (le2me_32(*(unsigned int *)(x))) | |
38 | |
39 // pertinent tables for IMA ADPCM | |
27116 | 40 static const int16_t adpcm_step[89] = |
5408 | 41 { |
42 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, | |
43 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, | |
44 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, | |
45 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, | |
46 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, | |
47 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, | |
48 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, | |
49 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | |
50 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 | |
51 }; | |
52 | |
27116 | 53 static const int8_t adpcm_index[16] = |
5408 | 54 { |
55 -1, -1, -1, -1, 2, 4, 6, 8, | |
56 -1, -1, -1, -1, 2, 4, 6, 8 | |
57 }; | |
58 | |
59 // useful macros | |
60 // clamp a number between 0 and 88 | |
27118 | 61 #define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88); |
5408 | 62 // clamp a number within a signed 16-bit range |
27118 | 63 #define CLAMP_S16(x) x = av_clip_int16(x); |
5408 | 64 // clamp a number above 16 |
65 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; | |
66 // sign extend a 16-bit value | |
27118 | 67 #define SE_16BIT(x) x = (int16_t)x; |
5408 | 68 // sign extend a 4-bit value |
69 #define SE_4BIT(x) if (x & 0x8) x -= 0x10; | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
70 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
71 static ad_info_t info = |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
72 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
73 "IMA ADPCM audio decoder", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
74 "imaadpcm", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
75 "Nick Kurshev", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
76 "Mike Melanson", |
5408 | 77 "" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
78 }; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
79 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
80 LIBAD_EXTERN(imaadpcm) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
81 |
5408 | 82 static int preinit(sh_audio_t *sh_audio) |
83 { | |
84 // not exactly sure what this field is for | |
85 sh_audio->audio_out_minsize = 8192; | |
86 | |
87 // if format is "ima4", assume the audio is coming from a QT file which | |
88 // indicates constant block size, whereas an AVI/ASF/WAV file will fill | |
89 // in this field with 0x11 | |
8103 | 90 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x61) || |
91 (sh_audio->format == 0x1100736d)) | |
5408 | 92 { |
93 sh_audio->ds->ss_div = (sh_audio->wf->nBlockAlign - | |
94 (MS_IMA_ADPCM_PREAMBLE_SIZE * sh_audio->wf->nChannels)) * 2; | |
95 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign; | |
96 } | |
97 else | |
98 { | |
99 sh_audio->ds->ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK; | |
100 sh_audio->ds->ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels; | |
101 } | |
5458 | 102 sh_audio->audio_in_minsize=sh_audio->ds->ss_mul; |
5408 | 103 return 1; |
104 } | |
105 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
106 static int init(sh_audio_t *sh_audio) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
107 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
108 /* IMA-ADPCM 4:1 audio codec:*/ |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
109 sh_audio->channels=sh_audio->wf->nChannels; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
110 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
111 /* decodes 34 byte -> 64 short*/ |
5408 | 112 sh_audio->i_bps = |
113 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div; | |
13427
9d0b052c4f74
setting samplesize to 2 in decoders where neccessary.
reimar
parents:
10808
diff
changeset
|
114 sh_audio->samplesize=2; |
5408 | 115 |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
116 return 1; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
117 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
118 |
5408 | 119 static void uninit(sh_audio_t *sh_audio) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
120 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
121 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
122 |
5481 | 123 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
124 { |
5481 | 125 if(cmd==ADCTRL_SKIP_FRAME){ |
126 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul); | |
127 return CONTROL_TRUE; | |
128 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
129 return CONTROL_UNKNOWN; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
130 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
131 |
5408 | 132 static void decode_nibbles(unsigned short *output, |
133 int output_size, int channels, | |
27117 | 134 int predictor[2], int index[2]) |
5408 | 135 { |
136 int step[2]; | |
137 int diff; | |
138 int i; | |
139 int sign; | |
140 int delta; | |
141 int channel_number = 0; | |
142 | |
27117 | 143 step[0] = adpcm_step[index[0]]; |
144 step[1] = adpcm_step[index[1]]; | |
5408 | 145 |
146 for (i = 0; i < output_size; i++) | |
147 { | |
148 delta = output[i]; | |
149 | |
150 index[channel_number] += adpcm_index[delta]; | |
151 CLAMP_0_TO_88(index[channel_number]); | |
152 | |
153 sign = delta & 8; | |
154 delta = delta & 7; | |
155 | |
156 diff = step[channel_number] >> 3; | |
157 if (delta & 4) diff += step[channel_number]; | |
158 if (delta & 2) diff += step[channel_number] >> 1; | |
159 if (delta & 1) diff += step[channel_number] >> 2; | |
160 | |
161 if (sign) | |
162 predictor[channel_number] -= diff; | |
163 else | |
164 predictor[channel_number] += diff; | |
165 | |
166 CLAMP_S16(predictor[channel_number]); | |
167 output[i] = predictor[channel_number]; | |
168 step[channel_number] = adpcm_step[index[channel_number]]; | |
169 | |
170 // toggle channel | |
171 channel_number ^= channels - 1; | |
172 | |
173 } | |
174 } | |
175 | |
176 static int qt_ima_adpcm_decode_block(unsigned short *output, | |
27112 | 177 unsigned char *input, int channels, int block_size) |
5408 | 178 { |
27114 | 179 int initial_predictor[2]; |
180 int initial_index[2]; | |
5408 | 181 int i; |
182 | |
27113
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
183 if (channels > 1) channels = 2; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
184 if (block_size < channels * QT_IMA_ADPCM_BLOCK_SIZE) |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
185 return -1; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
186 |
27114 | 187 for (i = 0; i < channels; i++) { |
188 initial_index[i] = initial_predictor[i] = BE_16(&input[i * QT_IMA_ADPCM_BLOCK_SIZE]); | |
5408 | 189 |
190 // mask, sign-extend, and clamp the predictor portion | |
27114 | 191 initial_predictor[i] &= 0xFF80; |
192 SE_16BIT(initial_predictor[i]); | |
193 CLAMP_S16(initial_predictor[i]); | |
5408 | 194 |
195 // mask and clamp the index portion | |
27114 | 196 initial_index[i] &= 0x7F; |
197 CLAMP_0_TO_88(initial_index[i]); | |
5408 | 198 } |
199 | |
200 // break apart all of the nibbles in the block | |
201 if (channels == 1) | |
202 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) | |
203 { | |
204 output[i * 2 + 0] = input[2 + i] & 0x0F; | |
205 output[i * 2 + 1] = input[2 + i] >> 4; | |
206 } | |
207 else | |
15786 | 208 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) |
5408 | 209 { |
210 output[i * 4 + 0] = input[2 + i] & 0x0F; | |
211 output[i * 4 + 1] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] & 0x0F; | |
212 output[i * 4 + 2] = input[2 + i] >> 4; | |
213 output[i * 4 + 3] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] >> 4; | |
214 } | |
215 | |
216 decode_nibbles(output, | |
217 QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels, | |
27117 | 218 initial_predictor, initial_index); |
5408 | 219 |
220 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels; | |
221 } | |
222 | |
223 static int ms_ima_adpcm_decode_block(unsigned short *output, | |
224 unsigned char *input, int channels, int block_size) | |
225 { | |
27114 | 226 int predictor[2]; |
227 int index[2]; | |
5408 | 228 int i; |
229 int channel_counter; | |
230 int channel_index; | |
231 int channel_index_l; | |
232 int channel_index_r; | |
233 | |
27113
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
234 if (channels > 1) channels = 2; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
235 if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels) |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
236 return -1; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
237 |
27114 | 238 for (i = 0; i < channels; i++) { |
239 predictor[i] = LE_16(&input[i * 4]); | |
240 SE_16BIT(predictor[i]); | |
241 index[i] = input[i * 4 + 2]; | |
5408 | 242 } |
243 | |
244 if (channels == 1) | |
245 for (i = 0; | |
10808
9883dfced49c
100l: you have 2 nibbles per byte, don't divide byte count by 2
rtognimp
parents:
8103
diff
changeset
|
246 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) |
5408 | 247 { |
248 output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F; | |
249 output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4; | |
250 } | |
251 else | |
252 { | |
253 // encoded as 8 nibbles (4 bytes) per channel; switch channel every | |
254 // 4th byte | |
255 channel_counter = 0; | |
256 channel_index_l = 0; | |
257 channel_index_r = 1; | |
258 channel_index = channel_index_l; | |
259 for (i = 0; | |
260 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) | |
261 { | |
262 output[channel_index + 0] = | |
263 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F; | |
264 output[channel_index + 2] = | |
265 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4; | |
266 channel_index += 4; | |
267 channel_counter++; | |
268 if (channel_counter == 4) | |
269 { | |
270 channel_index_l = channel_index; | |
271 channel_index = channel_index_r; | |
272 } | |
273 else if (channel_counter == 8) | |
274 { | |
275 channel_index_r = channel_index; | |
276 channel_index = channel_index_l; | |
277 channel_counter = 0; | |
278 } | |
279 } | |
280 } | |
281 | |
282 decode_nibbles(output, | |
283 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2, | |
284 channels, | |
27117 | 285 predictor, index); |
5408 | 286 |
287 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2; | |
288 } | |
289 | |
290 static int dk4_ima_adpcm_decode_block(unsigned short *output, | |
291 unsigned char *input, int channels, int block_size) | |
292 { | |
293 int i; | |
294 int output_ptr; | |
27114 | 295 int predictor[2]; |
296 int index[2]; | |
5408 | 297 |
27113
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
298 if (channels > 1) channels = 2; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
299 if (block_size < MS_IMA_ADPCM_PREAMBLE_SIZE * channels) |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
300 return -1; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
301 |
27114 | 302 for (i = 0; i < channels; i++) { |
303 // the first predictor value goes straight to the output | |
304 predictor[i] = output[i] = LE_16(&input[i * 4]); | |
305 SE_16BIT(predictor[i]); | |
306 index[i] = input[i * 4 + 2]; | |
5408 | 307 } |
308 | |
309 output_ptr = channels; | |
310 for (i = MS_IMA_ADPCM_PREAMBLE_SIZE * channels; i < block_size; i++) | |
311 { | |
312 output[output_ptr++] = input[i] >> 4; | |
313 output[output_ptr++] = input[i] & 0x0F; | |
314 } | |
315 | |
316 decode_nibbles(&output[channels], | |
317 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels, | |
318 channels, | |
27117 | 319 predictor, index); |
5408 | 320 |
321 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels; | |
322 } | |
323 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
324 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
325 { |
27112 | 326 int res = -1; |
327 int (*decode_func)(unsigned short *output, unsigned char *input, int channels, int block_size) = qt_ima_adpcm_decode_block; | |
5408 | 328 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, |
329 sh_audio->ds->ss_mul) != | |
330 sh_audio->ds->ss_mul) | |
331 return -1; | |
332 | |
8103 | 333 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x1100736d)) |
27112 | 334 decode_func = ms_ima_adpcm_decode_block; |
5408 | 335 else if (sh_audio->format == 0x61) |
27112 | 336 decode_func = dk4_ima_adpcm_decode_block; |
337 | |
338 res = decode_func((unsigned short*)buf, sh_audio->a_in_buffer, | |
339 sh_audio->wf->nChannels, sh_audio->ds->ss_mul); | |
340 if (res < 0) return res; | |
341 else return 2 * res; | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
342 } |