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