summaryrefslogtreecommitdiff
path: root/apps/codecs/libpcm/yamaha_adpcm.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libpcm/yamaha_adpcm.c')
-rw-r--r--apps/codecs/libpcm/yamaha_adpcm.c245
1 files changed, 245 insertions, 0 deletions
diff --git a/apps/codecs/libpcm/yamaha_adpcm.c b/apps/codecs/libpcm/yamaha_adpcm.c
new file mode 100644
index 0000000000..6b3daa8f83
--- /dev/null
+++ b/apps/codecs/libpcm/yamaha_adpcm.c
@@ -0,0 +1,245 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Yoshihisa Uchida
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "codeclib.h"
22#include "pcm_common.h"
23#include "adpcm_seek.h"
24
25/*
26 * YAMAHA ADPCM
27 *
28 * References
29 * [1] YAMAHA, YAMAHA ADPCM ACM Driver Version 1.0.0.0, 2005
30 * [2] BlendWorks, YM2608 ADPCM,
31 * http://web.archive.org/web/20050208190547/www.memb.jp/~dearna/ma/ym2608/adpcm.html
32 * [3] Naoyuki Sawa, ADPCM no shikumi #1,
33 * http://www.piece-me.org/piece-lab/adpcm/adpcm1.html
34 * [4] ffmpeg source code, libavcodec/adpcm.c
35 */
36
37/* ADPCM data block layout
38 *
39 * when the block header exists. (for example, encoding by YAMAHA ADPCM ACM Driver)
40 * blockAlign = (frequency / 60 + 4) * channels.
41 *
42 * block
43 * <Mono> (channels = 1)
44 * int16_t first value (Little endian)
45 * uint16_t first predictor (Little endian)
46 * uint8_t ADPCM data (1st data: 0-3 bit, 2nd data: 4-7 bit)
47 * ....
48 *
49 * <Stereo> (channels = 2)
50 * int16_t Left channel first value (Little endian)
51 * uint16_t Left channel first predictor (Little endian)
52 * int16_t Right channel first value (Little endian)
53 * uint16_t Right channel first predictor (Little endian)
54 * uint8_t ADPCM data (Left channel: 0-3 bit, Right channel: 4-7 bit)
55 * ....
56 *
57 * when the block header does not exist. (for example, encoding by ffmpeg)
58 * blockAlign = 8000
59 *
60 * block
61 * <Mono> (channels = 1)
62 * uint8_t ADPCM data (1st data: 0-3 bit, 2nd data: 4-7 bit)
63 * ....
64 *
65 * <Stereo> (channels = 2)
66 * uint8_t ADPCM data (Left channel: 0-3 bit, Right channel: 4-7 bit)
67 * ....
68 */
69
70static const int32_t amplification_table[] ICONST_ATTR = {
71 230, 230, 230, 230, 307, 409, 512, 614, 230, 230, 230, 230, 307, 409, 512, 614
72};
73
74static bool has_block_header = false;
75
76static struct adpcm_data cur_data;
77static int blocksperchunk;
78
79static struct pcm_format *fmt;
80
81static bool set_format(struct pcm_format *format)
82{
83 fmt = format;
84
85 if (fmt->bitspersample != 4)
86 {
87 DEBUGF("CODEC_ERROR: yamaha adpcm must be 4 bitspersample: %d\n",
88 fmt->bitspersample);
89 return false;
90 }
91
92 /* check exists block header */
93 if (fmt->blockalign == ((ci->id3->frequency / 60) + 4) * fmt->channels)
94 {
95 has_block_header = true;
96
97 /* chunksize = about 1/30 [sec] data */
98 fmt->chunksize = fmt->blockalign;
99 blocksperchunk = 1;
100 }
101 else
102 {
103 uint32_t max_chunk_count;
104
105 has_block_header = false;
106
107 /* blockalign = 2 * channels samples */
108 fmt->blockalign = fmt->channels;
109 fmt->samplesperblock = 2;
110
111 /* chunksize = about 1/32[sec] data */
112 blocksperchunk = ci->id3->frequency >> 6;
113 fmt->chunksize = blocksperchunk * fmt->blockalign;
114
115 max_chunk_count = (uint64_t)ci->id3->length * ci->id3->frequency
116 / (2000LL * fmt->chunksize / fmt->channels);
117
118 /* initialize seek table */
119 init_seek_table(max_chunk_count);
120 /* add first data */
121 add_adpcm_data(&cur_data);
122 }
123
124 return true;
125}
126
127static int16_t create_pcmdata(int ch, uint8_t nibble)
128{
129 int32_t tmp_pcmdata = cur_data.pcmdata[ch];
130 int32_t step = cur_data.step[ch];
131 int32_t delta = step >> 3;
132
133 if (nibble & 4) delta += step;
134 if (nibble & 2) delta += (step >> 1);
135 if (nibble & 1) delta += (step >> 2);
136
137 if (nibble & 0x08)
138 tmp_pcmdata -= delta;
139 else
140 tmp_pcmdata += delta;
141
142 CLIP(tmp_pcmdata, -32768, 32767);
143 cur_data.pcmdata[ch] = tmp_pcmdata;
144
145 step = (step * amplification_table[nibble & 0x07]) >> 8;
146 CLIP(step, 127, 24576);
147 cur_data.step[ch] = step;
148
149 return cur_data.pcmdata[ch];
150}
151
152static int decode(const uint8_t *inbuf, size_t inbufsize,
153 int32_t *outbuf, int *outbufcount)
154{
155 int ch;
156 size_t nsamples = 0;
157
158 /* read block header */
159 if (has_block_header)
160 {
161 for (ch = 0; ch < fmt->channels; ch++)
162 {
163 cur_data.pcmdata[ch] = inbuf[0] | (SE(inbuf[1]) << 8);
164 cur_data.step[ch] = inbuf[2] | (inbuf[3] << 8);
165
166 inbuf += 4;
167 inbufsize -= 4;
168 }
169 }
170
171 /* read block data */
172 ch = fmt->channels - 1;
173 while (inbufsize)
174 {
175 *outbuf++ = create_pcmdata(0, *inbuf ) << 13;
176 *outbuf++ = create_pcmdata(ch, *inbuf >> 4) << 13;
177 nsamples += 2;
178
179 inbuf++;
180 inbufsize--;
181 }
182
183 if (fmt->channels == 2)
184 nsamples >>= 1;
185 *outbufcount = nsamples;
186
187 if (!has_block_header)
188 add_adpcm_data(&cur_data);
189
190 return CODEC_OK;
191}
192
193static int decode_for_seek(const uint8_t *inbuf, size_t inbufsize)
194{
195 int ch = fmt->channels - 1;
196
197 while (inbufsize)
198 {
199 create_pcmdata(0, *inbuf );
200 create_pcmdata(ch, *inbuf >> 4);
201
202 inbuf++;
203 inbufsize--;
204 }
205
206 add_adpcm_data(&cur_data);
207
208 return CODEC_OK;
209}
210
211static struct pcm_pos *get_seek_pos(long seek_time,
212 uint8_t *(*read_buffer)(size_t *realsize))
213{
214 static struct pcm_pos newpos;
215 uint32_t new_count= 0;
216
217 if (seek_time > 0)
218 new_count = ((uint64_t)seek_time * ci->id3->frequency
219 / (1000LL * fmt->samplesperblock)) / blocksperchunk;
220
221 if (!has_block_header)
222 {
223 new_count = seek(new_count, &cur_data, read_buffer, &decode_for_seek);
224 }
225 newpos.pos = new_count * fmt->chunksize;
226 newpos.samples = new_count * blocksperchunk * fmt->samplesperblock;
227 return &newpos;
228}
229
230static struct pcm_codec codec = {
231 set_format,
232 get_seek_pos,
233 decode,
234 };
235
236const struct pcm_codec *get_yamaha_adpcm_codec(void)
237{
238 /* initialize first step, pcm data */
239 cur_data.pcmdata[0] = 0;
240 cur_data.pcmdata[1] = 0;
241 cur_data.step[0] = 127;
242 cur_data.step[1] = 127;
243
244 return &codec;
245}