Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / codecs / libpcm / ima_adpcm_common.c
blob724cce31b099da2bce74ace7b6e5767717336456
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Yoshihisa Uchida
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "codeclib.h"
22 #include "pcm_common.h"
23 #include "ima_adpcm_common.h"
26 * Functions for IMA ADPCM and IMA ADPCM series format
28 * References
29 * [1] The IMA Digital Audio Focus and Technical Working Groups,
30 * Recommended Practices for Enhancing Digital Audio Compatibility
31 * in Multimedia Systems Revision 3.00, 1992
32 * [2] Microsoft Corporation, New Multimedia Data Types and Data Techniques,
33 * Revision:3.0, 1994
34 * [3] ffmpeg source code, libavcodec/adpcm.c
37 /* step table */
38 static const uint16_t step_table[89] ICONST_ATTR = {
39 7, 8, 9, 10, 11, 12, 13, 14,
40 16, 17, 19, 21, 23, 25, 28, 31,
41 34, 37, 41, 45, 50, 55, 60, 66,
42 73, 80, 88, 97, 107, 118, 130, 143,
43 157, 173, 190, 209, 230, 253, 279, 307,
44 337, 371, 408, 449, 494, 544, 598, 658,
45 724, 796, 876, 963, 1060, 1166, 1282, 1411,
46 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
47 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
48 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
49 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
50 32767
53 /* step index tables */
54 static const int index_tables[4][16] ICONST_ATTR = {
55 /* adpcm data size is 2 */
56 { -1, 2 },
57 /* adpcm data size is 3 */
58 { -1, -1, 1, 2 },
59 /* adpcm data size is 4 */
60 { -1, -1, -1, -1, 2, 4, 6, 8 },
61 /* adpcm data size is 5 */
62 { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 },
65 static int32_t pcmdata[2];
66 static int8_t indices[2];
68 static int adpcm_data_size;
69 static uint8_t step_mask;
70 static uint8_t step_sign_mask;
71 static int8_t step_shift;
72 static const int *use_index_table;
75 * Before first decoding, this function must be executed.
77 * params
78 * bit: adpcm data size (2 <= bit <= 5).
79 * index_table: step index table
80 * if index_table is null, then step index table
81 * is used index_tables[bit-2].
83 void init_ima_adpcm_decoder(int bit, const int *index_table)
85 adpcm_data_size = bit;
86 step_sign_mask = 1 << (adpcm_data_size - 1);
87 step_mask = step_sign_mask - 1;
88 step_shift = adpcm_data_size - 2;
89 if (index_table)
90 use_index_table = index_table;
91 else
92 use_index_table = index_tables[adpcm_data_size - 2];
96 * When starting decoding for each block, this function must be executed.
98 * params
99 * channels: channel count
100 * init_pcmdata: array of init pcmdata
101 * init_index: array of init step indexes
103 void set_decode_parameters(int channels, int32_t *init_pcmdata, int8_t *init_index)
105 int ch;
107 for (ch = 0; ch < channels; ch++)
109 pcmdata[ch] = init_pcmdata[ch];
110 indices[ch] = init_index[ch];
115 * convert ADPCM to PCM for any adpcm data size.
117 * If adpcm_data_size is 4, then you use create_pcmdata_size4()
118 * in place of this functon.
120 int16_t create_pcmdata(int ch, uint8_t nibble)
122 int check_bit = 1 << step_shift;
123 int32_t delta = 0;
124 int16_t step = step_table[indices[ch]];
126 do {
127 if (nibble & check_bit)
128 delta += step;
129 step >>= 1;
130 check_bit >>= 1;
131 } while (check_bit);
132 delta += step;
134 if (nibble & step_sign_mask)
135 pcmdata[ch] -= delta;
136 else
137 pcmdata[ch] += delta;
139 indices[ch] += use_index_table[nibble & step_mask];
140 CLIP(indices[ch], 0, 88);
142 CLIP(pcmdata[ch], -32768, 32767);
144 return (int16_t)pcmdata[ch];
148 * convert ADPCM to PCM when adpcm data size is 4.
150 int16_t create_pcmdata_size4(int ch, uint8_t nibble)
152 int32_t delta;
153 int16_t step = step_table[indices[ch]];
155 delta = (step >> 3);
156 if (nibble & 4) delta += step;
157 if (nibble & 2) delta += (step >> 1);
158 if (nibble & 1) delta += (step >> 2);
160 if (nibble & 0x08)
161 pcmdata[ch] -= delta;
162 else
163 pcmdata[ch] += delta;
165 indices[ch] += use_index_table[nibble & 0x07];
166 CLIP(indices[ch], 0, 88);
168 CLIP(pcmdata[ch], -32768, 32767);
170 return (int16_t)pcmdata[ch];