commit FS#10424 and FS#10425
[kugel-rb.git] / apps / codecs / libpcm / dialogic_oki_adpcm.c
blobe12930057b95ba01152df77434be735924cd850c
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 "adpcm_seek.h"
26 * Dialogic OKI ADPCM
28 * References
29 * [1] Dialogic Corporation, Dialogic ADPCM Algorithm, 1988
30 * [2] MultimediaWiki, Dialogic IMA ADPCM, URL:http://wiki.multimedia.cx/index.php?title=Dialogic_IMA_ADPCM
31 * [3] sox source code, src/adpcms.c
32 * [4] Tetsuya Isaki, NetBSD:/sys/dev/audio.c, http://www.tri-tree.gr.jp/~isaki/NetBSD/src/sys/dev/ic/msm6258.c.html
35 static const uint16_t step_table[] ICONST_ATTR = {
36 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
37 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209,
38 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
39 876, 963, 1060, 1166, 1282, 1411, 1552,
42 static const int index_table[] ICONST_ATTR = {
43 -1, -1, -1, -1, 2, 4, 6, 8
46 static struct adpcm_data cur_data;
48 static struct pcm_format *fmt;
50 static bool set_format(struct pcm_format *format)
52 uint32_t max_chunk_count;
54 fmt = format;
56 if (fmt->bitspersample != 4)
58 DEBUGF("CODEC_ERROR: dialogic oki adpcm must be 4 bitspersample: %d\n",
59 fmt->bitspersample);
60 return false;
63 if (fmt->channels != 1)
65 DEBUGF("CODEC_ERROR: dialogic oki adpcm must be monaural\n");
66 return false;
69 /* blockalign = 2 samples */
70 fmt->blockalign = 1;
71 fmt->samplesperblock = 2;
73 /* chunksize = about 1/32[sec] data */
74 fmt->chunksize = ci->id3->frequency >> 6;
76 max_chunk_count = (uint64_t)ci->id3->length * ci->id3->frequency
77 / (2000LL * fmt->chunksize);
79 /* initialize seek table */
80 init_seek_table(max_chunk_count);
81 /* add first data */
82 add_adpcm_data(&cur_data);
84 return true;
87 static int16_t create_pcmdata(uint8_t nibble)
89 int16_t delta;
90 int16_t index = cur_data.step[0];
91 int16_t step = step_table[index];
93 delta = (step >> 3);
94 if (nibble & 4) delta += step;
95 if (nibble & 2) delta += (step >> 1);
96 if (nibble & 1) delta += (step >> 2);
98 if (nibble & 0x08)
99 cur_data.pcmdata[0] -= delta;
100 else
101 cur_data.pcmdata[0] += delta;
103 CLIP(cur_data.pcmdata[0], -2048, 2047);
105 index += index_table[nibble & 0x07];
106 CLIP(index, 0, 48);
107 cur_data.step[0] = index;
109 return cur_data.pcmdata[0];
112 static int decode(const uint8_t *inbuf, size_t inbufsize,
113 int32_t *outbuf, int *outbufcount)
115 size_t nsamples = 0;
117 while (inbufsize)
119 *outbuf++ = create_pcmdata(*inbuf >> 4) << 17;
120 *outbuf++ = create_pcmdata(*inbuf ) << 17;
121 nsamples += 2;
123 inbuf++;
124 inbufsize--;
127 *outbufcount = nsamples;
128 add_adpcm_data(&cur_data);
130 return CODEC_OK;
133 static int decode_for_seek(const uint8_t *inbuf, size_t inbufsize)
135 while (inbufsize)
137 create_pcmdata(*inbuf >> 4);
138 create_pcmdata(*inbuf );
140 inbuf++;
141 inbufsize--;
144 add_adpcm_data(&cur_data);
146 return CODEC_OK;
149 static struct pcm_pos *get_seek_pos(long seek_time,
150 uint8_t *(*read_buffer)(size_t *realsize))
152 static struct pcm_pos newpos;
153 uint32_t seek_count = 0;
154 uint32_t new_count;
156 if (seek_time > 0)
157 seek_count = (uint64_t)seek_time * ci->id3->frequency
158 / (2000LL * fmt->chunksize);
160 new_count = seek(seek_count, &cur_data, read_buffer, &decode_for_seek);
161 newpos.pos = new_count * fmt->chunksize;
162 newpos.samples = (newpos.pos / fmt->blockalign) * fmt->samplesperblock;
163 return &newpos;
166 static const struct pcm_codec codec = {
167 set_format,
168 get_seek_pos,
169 decode,
172 const struct pcm_codec *get_dialogic_oki_adpcm_codec(void)
175 * initialize first pcm data, step index
176 * because the dialogic oki adpcm is always monaural,
177 * pcmdata[1], step[1] do not use.
179 cur_data.pcmdata[0] = 0;
180 cur_data.step[0] = 0;
182 return &codec;