Mercurial > mplayer.hg
annotate libmpcodecs/ad_imaadpcm.c @ 27188:8b7f0838d2ec
Remove redundant dependencies for .rc files.
author | diego |
---|---|
date | Sun, 06 Jul 2008 13:28:45 +0000 |
parents | f8e189f9e119 |
children | 3d94f70003e9 |
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 | |
27121 | 53 static const int8_t adpcm_index[8] = |
5408 | 54 { |
55 -1, -1, -1, -1, 2, 4, 6, 8, | |
56 }; | |
57 | |
58 // useful macros | |
59 // clamp a number between 0 and 88 | |
27118 | 60 #define CLAMP_0_TO_88(x) x = av_clip(x, 0, 88); |
5408 | 61 // clamp a number within a signed 16-bit range |
27118 | 62 #define CLAMP_S16(x) x = av_clip_int16(x); |
5408 | 63 // clamp a number above 16 |
64 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; | |
65 // sign extend a 4-bit value | |
66 #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
|
67 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
68 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
|
69 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
70 "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
|
71 "imaadpcm", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
72 "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
|
73 "Mike Melanson", |
5408 | 74 "" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
75 }; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
76 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
77 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
|
78 |
5408 | 79 static int preinit(sh_audio_t *sh_audio) |
80 { | |
81 // not exactly sure what this field is for | |
82 sh_audio->audio_out_minsize = 8192; | |
83 | |
84 // if format is "ima4", assume the audio is coming from a QT file which | |
85 // indicates constant block size, whereas an AVI/ASF/WAV file will fill | |
86 // in this field with 0x11 | |
8103 | 87 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x61) || |
88 (sh_audio->format == 0x1100736d)) | |
5408 | 89 { |
90 sh_audio->ds->ss_div = (sh_audio->wf->nBlockAlign - | |
91 (MS_IMA_ADPCM_PREAMBLE_SIZE * sh_audio->wf->nChannels)) * 2; | |
92 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign; | |
93 } | |
94 else | |
95 { | |
96 sh_audio->ds->ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK; | |
97 sh_audio->ds->ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels; | |
98 } | |
5458 | 99 sh_audio->audio_in_minsize=sh_audio->ds->ss_mul; |
5408 | 100 return 1; |
101 } | |
102 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
103 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
|
104 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
105 /* 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
|
106 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
|
107 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
|
108 /* decodes 34 byte -> 64 short*/ |
5408 | 109 sh_audio->i_bps = |
110 (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
|
111 sh_audio->samplesize=2; |
5408 | 112 |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
113 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
|
114 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
115 |
5408 | 116 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
|
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 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
119 |
5481 | 120 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
|
121 { |
5481 | 122 if(cmd==ADCTRL_SKIP_FRAME){ |
123 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul); | |
124 return CONTROL_TRUE; | |
125 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
126 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
|
127 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
128 |
5408 | 129 static void decode_nibbles(unsigned short *output, |
130 int output_size, int channels, | |
27117 | 131 int predictor[2], int index[2]) |
5408 | 132 { |
133 int step[2]; | |
134 int i; | |
135 int sign; | |
136 int delta; | |
137 int channel_number = 0; | |
138 | |
27117 | 139 step[0] = adpcm_step[index[0]]; |
140 step[1] = adpcm_step[index[1]]; | |
5408 | 141 |
142 for (i = 0; i < output_size; i++) | |
143 { | |
144 delta = output[i]; | |
27121 | 145 sign = delta & 8; |
146 delta = delta & 7; | |
5408 | 147 |
148 index[channel_number] += adpcm_index[delta]; | |
149 CLAMP_0_TO_88(index[channel_number]); | |
150 | |
27120 | 151 delta = 2 * delta + 1; |
152 if (sign) delta = -delta; | |
5408 | 153 |
27120 | 154 predictor[channel_number] += (delta * step[channel_number]) >> 3; |
5408 | 155 |
156 CLAMP_S16(predictor[channel_number]); | |
157 output[i] = predictor[channel_number]; | |
158 step[channel_number] = adpcm_step[index[channel_number]]; | |
159 | |
160 // toggle channel | |
161 channel_number ^= channels - 1; | |
162 | |
163 } | |
164 } | |
165 | |
166 static int qt_ima_adpcm_decode_block(unsigned short *output, | |
27112 | 167 unsigned char *input, int channels, int block_size) |
5408 | 168 { |
27114 | 169 int initial_predictor[2]; |
170 int initial_index[2]; | |
5408 | 171 int i; |
172 | |
27113
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
173 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
|
174 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
|
175 return -1; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
176 |
27114 | 177 for (i = 0; i < channels; i++) { |
27119 | 178 initial_index[i] = initial_predictor[i] = (int16_t)BE_16(&input[i * QT_IMA_ADPCM_BLOCK_SIZE]); |
5408 | 179 |
180 // mask, sign-extend, and clamp the predictor portion | |
27119 | 181 initial_predictor[i] &= ~0x7F; |
27114 | 182 CLAMP_S16(initial_predictor[i]); |
5408 | 183 |
184 // mask and clamp the index portion | |
27114 | 185 initial_index[i] &= 0x7F; |
186 CLAMP_0_TO_88(initial_index[i]); | |
5408 | 187 } |
188 | |
189 // break apart all of the nibbles in the block | |
190 if (channels == 1) | |
191 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) | |
192 { | |
193 output[i * 2 + 0] = input[2 + i] & 0x0F; | |
194 output[i * 2 + 1] = input[2 + i] >> 4; | |
195 } | |
196 else | |
15786 | 197 for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) |
5408 | 198 { |
199 output[i * 4 + 0] = input[2 + i] & 0x0F; | |
200 output[i * 4 + 1] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] & 0x0F; | |
201 output[i * 4 + 2] = input[2 + i] >> 4; | |
202 output[i * 4 + 3] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] >> 4; | |
203 } | |
204 | |
205 decode_nibbles(output, | |
206 QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels, | |
27117 | 207 initial_predictor, initial_index); |
5408 | 208 |
209 return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels; | |
210 } | |
211 | |
212 static int ms_ima_adpcm_decode_block(unsigned short *output, | |
213 unsigned char *input, int channels, int block_size) | |
214 { | |
27114 | 215 int predictor[2]; |
216 int index[2]; | |
5408 | 217 int i; |
218 int channel_counter; | |
219 int channel_index; | |
220 int channel_index_l; | |
221 int channel_index_r; | |
222 | |
27113
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
223 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
|
224 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
|
225 return -1; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
226 |
27114 | 227 for (i = 0; i < channels; i++) { |
27119 | 228 predictor[i] = (int16_t)LE_16(&input[i * 4]); |
27114 | 229 index[i] = input[i * 4 + 2]; |
5408 | 230 } |
231 | |
232 if (channels == 1) | |
233 for (i = 0; | |
10808
9883dfced49c
100l: you have 2 nibbles per byte, don't divide byte count by 2
rtognimp
parents:
8103
diff
changeset
|
234 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) |
5408 | 235 { |
236 output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F; | |
237 output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4; | |
238 } | |
239 else | |
240 { | |
241 // encoded as 8 nibbles (4 bytes) per channel; switch channel every | |
242 // 4th byte | |
243 channel_counter = 0; | |
244 channel_index_l = 0; | |
245 channel_index_r = 1; | |
246 channel_index = channel_index_l; | |
247 for (i = 0; | |
248 i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) | |
249 { | |
250 output[channel_index + 0] = | |
251 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F; | |
252 output[channel_index + 2] = | |
253 input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4; | |
254 channel_index += 4; | |
255 channel_counter++; | |
256 if (channel_counter == 4) | |
257 { | |
258 channel_index_l = channel_index; | |
259 channel_index = channel_index_r; | |
260 } | |
261 else if (channel_counter == 8) | |
262 { | |
263 channel_index_r = channel_index; | |
264 channel_index = channel_index_l; | |
265 channel_counter = 0; | |
266 } | |
267 } | |
268 } | |
269 | |
270 decode_nibbles(output, | |
271 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2, | |
272 channels, | |
27117 | 273 predictor, index); |
5408 | 274 |
275 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2; | |
276 } | |
277 | |
278 static int dk4_ima_adpcm_decode_block(unsigned short *output, | |
279 unsigned char *input, int channels, int block_size) | |
280 { | |
281 int i; | |
282 int output_ptr; | |
27114 | 283 int predictor[2]; |
284 int index[2]; | |
5408 | 285 |
27113
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
286 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
|
287 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
|
288 return -1; |
680301b96149
Add a few size checks to IMA decoder. The code is still a mess though,
reimar
parents:
27112
diff
changeset
|
289 |
27114 | 290 for (i = 0; i < channels; i++) { |
291 // the first predictor value goes straight to the output | |
27119 | 292 predictor[i] = output[i] = (int16_t)LE_16(&input[i * 4]); |
27114 | 293 index[i] = input[i * 4 + 2]; |
5408 | 294 } |
295 | |
296 output_ptr = channels; | |
297 for (i = MS_IMA_ADPCM_PREAMBLE_SIZE * channels; i < block_size; i++) | |
298 { | |
299 output[output_ptr++] = input[i] >> 4; | |
300 output[output_ptr++] = input[i] & 0x0F; | |
301 } | |
302 | |
303 decode_nibbles(&output[channels], | |
304 (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels, | |
305 channels, | |
27117 | 306 predictor, index); |
5408 | 307 |
308 return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2 - channels; | |
309 } | |
310 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
311 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
|
312 { |
27112 | 313 int res = -1; |
314 int (*decode_func)(unsigned short *output, unsigned char *input, int channels, int block_size) = qt_ima_adpcm_decode_block; | |
5408 | 315 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, |
316 sh_audio->ds->ss_mul) != | |
317 sh_audio->ds->ss_mul) | |
318 return -1; | |
319 | |
8103 | 320 if ((sh_audio->format == 0x11) || (sh_audio->format == 0x1100736d)) |
27112 | 321 decode_func = ms_ima_adpcm_decode_block; |
5408 | 322 else if (sh_audio->format == 0x61) |
27112 | 323 decode_func = dk4_ima_adpcm_decode_block; |
324 | |
325 res = decode_func((unsigned short*)buf, sh_audio->a_in_buffer, | |
326 sh_audio->wf->nChannels, sh_audio->ds->ss_mul); | |
327 if (res < 0) return res; | |
328 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
|
329 } |