comparison spandsp-0.0.6pre17/src/ima_adpcm.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * ima_adpcm.c - Conversion routines between linear 16 bit PCM data and
5 * IMA/DVI/Intel ADPCM format.
6 *
7 * Written by Steve Underwood <steveu@coppice.org>
8 *
9 * Copyright (C) 2001, 2004 Steve Underwood
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License version 2.1,
15 * as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * $Id: ima_adpcm.c,v 1.36 2009/04/11 18:11:19 steveu Exp $
27 */
28
29 /*! \file */
30
31 #if defined(HAVE_CONFIG_H)
32 #include "config.h"
33 #endif
34
35 #include <stdlib.h>
36 #include <inttypes.h>
37 #include <string.h>
38 #if defined(HAVE_TGMATH_H)
39 #include <tgmath.h>
40 #endif
41 #if defined(HAVE_MATH_H)
42 #include <math.h>
43 #endif
44 #include "floating_fudge.h"
45
46 #include "spandsp/telephony.h"
47 #include "spandsp/fast_convert.h"
48 #include "spandsp/saturated.h"
49 #include "spandsp/ima_adpcm.h"
50 #include "spandsp/private/ima_adpcm.h"
51
52 /*
53 * Intel/DVI ADPCM coder/decoder.
54 *
55 * The algorithm for this coder was taken from the IMA Compatability Project
56 * proceedings, Vol 2, Number 2; May 1992.
57 *
58 * The RTP payload specs. reference a variant of DVI, called VDVI. This attempts to
59 * further compress, in a variable bit rate manner, by expressing the 4 bit codes
60 * from the DVI codec as:
61 *
62 * 0 00
63 * 1 010
64 * 2 1100
65 * 3 11100
66 * 4 111100
67 * 5 1111100
68 * 6 11111100
69 * 7 11111110
70 * 8 10
71 * 9 011
72 * 10 1101
73 * 11 11101
74 * 12 111101
75 * 13 1111101
76 * 14 11111101
77 * 15 11111111
78 *
79 * Any left over bits in the last octet of an encoded burst are set to one.
80 */
81
82 /*
83 DVI4 uses an adaptive delta pulse code modulation (ADPCM) encoding
84 scheme that was specified by the Interactive Multimedia Association
85 (IMA) as the "IMA ADPCM wave type". However, the encoding defined
86 here as DVI4 differs in three respects from the IMA specification:
87
88 o The RTP DVI4 header contains the predicted value rather than the
89 first sample value contained the IMA ADPCM block header.
90
91 o IMA ADPCM blocks contain an odd number of samples, since the first
92 sample of a block is contained just in the header (uncompressed),
93 followed by an even number of compressed samples. DVI4 has an
94 even number of compressed samples only, using the `predict' word
95 from the header to decode the first sample.
96
97 o For DVI4, the 4-bit samples are packed with the first sample in
98 the four most significant bits and the second sample in the four
99 least significant bits. In the IMA ADPCM codec, the samples are
100 packed in the opposite order.
101
102 Each packet contains a single DVI block. This profile only defines
103 the 4-bit-per-sample version, while IMA also specified a 3-bit-per-
104 sample encoding.
105
106 The "header" word for each channel has the following structure:
107
108 int16 predict; // predicted value of first sample
109 // from the previous block (L16 format)
110 u_int8 index; // current index into stepsize table
111 u_int8 reserved; // set to zero by sender, ignored by receiver
112
113 Each octet following the header contains two 4-bit samples, thus the
114 number of samples per packet MUST be even because there is no means
115 to indicate a partially filled last octet.
116 */
117
118 /*! The number of ADPCM step sizes */
119 #define STEP_MAX 88
120
121 /* Intel ADPCM step variation table */
122 static const int step_size[STEP_MAX + 1] =
123 {
124 7, 8, 9, 10, 11, 12, 13, 14,
125 16, 17, 19, 21, 23, 25, 28, 31,
126 34, 37, 41, 45, 50, 55, 60, 66,
127 73, 80, 88, 97, 107, 118, 130, 143,
128 157, 173, 190, 209, 230, 253, 279, 307,
129 337, 371, 408, 449, 494, 544, 598, 658,
130 724, 796, 876, 963, 1060, 1166, 1282, 1411,
131 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
132 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
133 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
134 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
135 32767
136 };
137
138 static const int step_adjustment[8] =
139 {
140 -1, -1, -1, -1, 2, 4, 6, 8
141 };
142
143 static const struct
144 {
145 uint8_t code;
146 uint8_t bits;
147 } vdvi_encode[] =
148 {
149 {0x00, 2},
150 {0x02, 3},
151 {0x0C, 4},
152 {0x1C, 5},
153 {0x3C, 6},
154 {0x7C, 7},
155 {0xFC, 8},
156 {0xFE, 8},
157 {0x02, 2},
158 {0x03, 3},
159 {0x0D, 4},
160 {0x1D, 5},
161 {0x3D, 6},
162 {0x7D, 7},
163 {0xFD, 8},
164 {0xFF, 8}
165 };
166
167 static const struct
168 {
169 uint16_t code;
170 uint16_t mask;
171 uint8_t bits;
172 } vdvi_decode[] =
173 {
174 {0x0000, 0xC000, 2},
175 {0x4000, 0xE000, 3},
176 {0xC000, 0xF000, 4},
177 {0xE000, 0xF800, 5},
178 {0xF000, 0xFC00, 6},
179 {0xF800, 0xFE00, 7},
180 {0xFC00, 0xFF00, 8},
181 {0xFE00, 0xFF00, 8},
182 {0x8000, 0xC000, 2},
183 {0x6000, 0xE000, 3},
184 {0xD000, 0xF000, 4},
185 {0xE800, 0xF800, 5},
186 {0xF400, 0xFC00, 6},
187 {0xFA00, 0xFE00, 7},
188 {0xFD00, 0xFF00, 8},
189 {0xFF00, 0xFF00, 8}
190 };
191
192 static int16_t decode(ima_adpcm_state_t *s, uint8_t adpcm)
193 {
194 int e;
195 int ss;
196 int16_t linear;
197
198 /* e = (adpcm+0.5)*step/4 */
199 ss = step_size[s->step_index];
200 e = ss >> 3;
201 if (adpcm & 0x01)
202 e += (ss >> 2);
203 /*endif*/
204 if (adpcm & 0x02)
205 e += (ss >> 1);
206 /*endif*/
207 if (adpcm & 0x04)
208 e += ss;
209 /*endif*/
210 if (adpcm & 0x08)
211 e = -e;
212 /*endif*/
213 linear = saturate(s->last + e);
214 s->last = linear;
215 s->step_index += step_adjustment[adpcm & 0x07];
216 if (s->step_index < 0)
217 s->step_index = 0;
218 else if (s->step_index > STEP_MAX)
219 s->step_index = STEP_MAX;
220 /*endif*/
221 return linear;
222 }
223 /*- End of function --------------------------------------------------------*/
224
225 static uint8_t encode(ima_adpcm_state_t *s, int16_t linear)
226 {
227 int e;
228 int ss;
229 int adpcm;
230 int diff;
231 int initial_e;
232
233 ss = step_size[s->step_index];
234 initial_e =
235 e = linear - s->last;
236 diff = ss >> 3;
237 adpcm = (uint8_t) 0x00;
238 if (e < 0)
239 {
240 adpcm = (uint8_t) 0x08;
241 e = -e;
242 }
243 /*endif*/
244 if (e >= ss)
245 {
246 adpcm |= (uint8_t) 0x04;
247 e -= ss;
248 }
249 /*endif*/
250 ss >>= 1;
251 if (e >= ss)
252 {
253 adpcm |= (uint8_t) 0x02;
254 e -= ss;
255 }
256 /*endif*/
257 ss >>= 1;
258 if (e >= ss)
259 {
260 adpcm |= (uint8_t) 0x01;
261 e -= ss;
262 }
263 /*endif*/
264
265 if (initial_e < 0)
266 diff = -(diff - initial_e - e);
267 else
268 diff = diff + initial_e - e;
269 /*endif*/
270 s->last = saturate(diff + s->last);
271 s->step_index += step_adjustment[adpcm & 0x07];
272 if (s->step_index < 0)
273 s->step_index = 0;
274 else if (s->step_index > STEP_MAX)
275 s->step_index = STEP_MAX;
276 /*endif*/
277 return (uint8_t) adpcm;
278 }
279 /*- End of function --------------------------------------------------------*/
280
281 SPAN_DECLARE(ima_adpcm_state_t *) ima_adpcm_init(ima_adpcm_state_t *s,
282 int variant,
283 int chunk_size)
284 {
285 if (s == NULL)
286 {
287 if ((s = (ima_adpcm_state_t *) malloc(sizeof(*s))) == NULL)
288 return NULL;
289 }
290 /*endif*/
291 memset(s, 0, sizeof(*s));
292 s->variant = variant;
293 s->chunk_size = chunk_size;
294 return s;
295 }
296 /*- End of function --------------------------------------------------------*/
297
298 SPAN_DECLARE(int) ima_adpcm_release(ima_adpcm_state_t *s)
299 {
300 return 0;
301 }
302 /*- End of function --------------------------------------------------------*/
303
304 SPAN_DECLARE(int) ima_adpcm_free(ima_adpcm_state_t *s)
305 {
306 free(s);
307 return 0;
308 }
309 /*- End of function --------------------------------------------------------*/
310
311 SPAN_DECLARE(int) ima_adpcm_decode(ima_adpcm_state_t *s,
312 int16_t amp[],
313 const uint8_t ima_data[],
314 int ima_bytes)
315 {
316 int i;
317 int j;
318 int samples;
319 uint16_t code;
320
321 samples = 0;
322 switch (s->variant)
323 {
324 case IMA_ADPCM_IMA4:
325 i = 0;
326 if (s->chunk_size == 0)
327 {
328 amp[samples++] = (ima_data[1] << 8) | ima_data[0];
329 s->step_index = ima_data[2];
330 s->last = amp[0];
331 i = 4;
332 }
333 /*endif*/
334 for ( ; i < ima_bytes; i++)
335 {
336 amp[samples++] = decode(s, ima_data[i] & 0xF);
337 amp[samples++] = decode(s, (ima_data[i] >> 4) & 0xF);
338 }
339 /*endfor*/
340 break;
341 case IMA_ADPCM_DVI4:
342 i = 0;
343 if (s->chunk_size == 0)
344 {
345 s->last = (int16_t) ((ima_data[0] << 8) | ima_data[1]);
346 s->step_index = ima_data[2];
347 i = 4;
348 }
349 /*endif*/
350 for ( ; i < ima_bytes; i++)
351 {
352 amp[samples++] = decode(s, (ima_data[i] >> 4) & 0xF);
353 amp[samples++] = decode(s, ima_data[i] & 0xF);
354 }
355 /*endfor*/
356 break;
357 case IMA_ADPCM_VDVI:
358 i = 0;
359 if (s->chunk_size == 0)
360 {
361 s->last = (int16_t) ((ima_data[0] << 8) | ima_data[1]);
362 s->step_index = ima_data[2];
363 i = 4;
364 }
365 /*endif*/
366 code = 0;
367 s->bits = 0;
368 for (;;)
369 {
370 if (s->bits <= 8)
371 {
372 if (i >= ima_bytes)
373 break;
374 /*endif*/
375 code |= ((uint16_t) ima_data[i++] << (8 - s->bits));
376 s->bits += 8;
377 }
378 /*endif*/
379 for (j = 0; j < 8; j++)
380 {
381 if ((vdvi_decode[j].mask & code) == vdvi_decode[j].code)
382 break;
383 if ((vdvi_decode[j + 8].mask & code) == vdvi_decode[j + 8].code)
384 {
385 j += 8;
386 break;
387 }
388 /*endif*/
389 }
390 /*endfor*/
391 amp[samples++] = decode(s, (uint8_t) j);
392 code <<= vdvi_decode[j].bits;
393 s->bits -= vdvi_decode[j].bits;
394 }
395 /*endfor*/
396 /* Use up the remanents of the last octet */
397 while (s->bits > 0)
398 {
399 for (j = 0; j < 8; j++)
400 {
401 if ((vdvi_decode[j].mask & code) == vdvi_decode[j].code)
402 break;
403 /*endif*/
404 if ((vdvi_decode[j + 8].mask & code) == vdvi_decode[j + 8].code)
405 {
406 j += 8;
407 break;
408 }
409 /*endif*/
410 }
411 /*endfor*/
412 if (vdvi_decode[j].bits > s->bits)
413 break;
414 /*endif*/
415 amp[samples++] = decode(s, (uint8_t) j);
416 code <<= vdvi_decode[j].bits;
417 s->bits -= vdvi_decode[j].bits;
418 }
419 /*endwhile*/
420 break;
421 }
422 /*endswitch*/
423 return samples;
424 }
425 /*- End of function --------------------------------------------------------*/
426
427 SPAN_DECLARE(int) ima_adpcm_encode(ima_adpcm_state_t *s,
428 uint8_t ima_data[],
429 const int16_t amp[],
430 int len)
431 {
432 int i;
433 int bytes;
434 uint8_t code;
435
436 bytes = 0;
437 switch (s->variant)
438 {
439 case IMA_ADPCM_IMA4:
440 i = 0;
441 if (s->chunk_size == 0)
442 {
443 ima_data[bytes++] = (uint8_t) amp[0];
444 ima_data[bytes++] = (uint8_t) (amp[0] >> 8);
445 ima_data[bytes++] = (uint8_t) s->step_index;
446 ima_data[bytes++] = 0;
447 s->last = amp[0];
448 s->bits = 0;
449 i = 1;
450 }
451 /*endif*/
452 for ( ; i < len; i++)
453 {
454 s->ima_byte = (uint8_t) ((s->ima_byte >> 4) | (encode(s, amp[i]) << 4));
455 if ((s->bits++ & 1))
456 ima_data[bytes++] = (uint8_t) s->ima_byte;
457 /*endif*/
458 }
459 /*endfor*/
460 break;
461 case IMA_ADPCM_DVI4:
462 if (s->chunk_size == 0)
463 {
464 ima_data[bytes++] = (uint8_t) (s->last >> 8);
465 ima_data[bytes++] = (uint8_t) s->last;
466 ima_data[bytes++] = (uint8_t) s->step_index;
467 ima_data[bytes++] = 0;
468 }
469 /*endif*/
470 for (i = 0; i < len; i++)
471 {
472 s->ima_byte = (uint8_t) ((s->ima_byte << 4) | encode(s, amp[i]));
473 if ((s->bits++ & 1))
474 ima_data[bytes++] = (uint8_t) s->ima_byte;
475 /*endif*/
476 }
477 /*endfor*/
478 break;
479 case IMA_ADPCM_VDVI:
480 if (s->chunk_size == 0)
481 {
482 ima_data[bytes++] = (uint8_t) (s->last >> 8);
483 ima_data[bytes++] = (uint8_t) s->last;
484 ima_data[bytes++] = (uint8_t) s->step_index;
485 ima_data[bytes++] = 0;
486 }
487 /*endif*/
488 s->bits = 0;
489 for (i = 0; i < len; i++)
490 {
491 code = encode(s, amp[i]);
492 s->ima_byte = (s->ima_byte << vdvi_encode[code].bits) | vdvi_encode[code].code;
493 s->bits += vdvi_encode[code].bits;
494 if (s->bits >= 8)
495 {
496 s->bits -= 8;
497 ima_data[bytes++] = (uint8_t) (s->ima_byte >> s->bits);
498 }
499 /*endif*/
500 }
501 /*endfor*/
502 if (s->bits)
503 ima_data[bytes++] = (uint8_t) (((s->ima_byte << 8) | 0xFF) >> s->bits);
504 /*endif*/
505 break;
506 }
507 /*endswitch*/
508 return bytes;
509 }
510 /*- End of function --------------------------------------------------------*/
511 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.