commit FS#10424 and FS#10425
[kugel-rb.git] / apps / codecs / libpcm / yamaha_adpcm.c
blob6b3daa8f8319592fc3b33aafdf805ab43d427319
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 * YAMAHA ADPCM
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
37 /* ADPCM data block layout
39 * when the block header exists. (for example, encoding by YAMAHA ADPCM ACM Driver)
40 * blockAlign = (frequency / 60 + 4) * channels.
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 * ....
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 * ....
57 * when the block header does not exist. (for example, encoding by ffmpeg)
58 * blockAlign = 8000
60 * block
61 * <Mono> (channels = 1)
62 * uint8_t ADPCM data (1st data: 0-3 bit, 2nd data: 4-7 bit)
63 * ....
65 * <Stereo> (channels = 2)
66 * uint8_t ADPCM data (Left channel: 0-3 bit, Right channel: 4-7 bit)
67 * ....
70 static const int32_t amplification_table[] ICONST_ATTR = {
71 230, 230, 230, 230, 307, 409, 512, 614, 230, 230, 230, 230, 307, 409, 512, 614
74 static bool has_block_header = false;
76 static struct adpcm_data cur_data;
77 static int blocksperchunk;
79 static struct pcm_format *fmt;
81 static bool set_format(struct pcm_format *format)
83 fmt = format;
85 if (fmt->bitspersample != 4)
87 DEBUGF("CODEC_ERROR: yamaha adpcm must be 4 bitspersample: %d\n",
88 fmt->bitspersample);
89 return false;
92 /* check exists block header */
93 if (fmt->blockalign == ((ci->id3->frequency / 60) + 4) * fmt->channels)
95 has_block_header = true;
97 /* chunksize = about 1/30 [sec] data */
98 fmt->chunksize = fmt->blockalign;
99 blocksperchunk = 1;
101 else
103 uint32_t max_chunk_count;
105 has_block_header = false;
107 /* blockalign = 2 * channels samples */
108 fmt->blockalign = fmt->channels;
109 fmt->samplesperblock = 2;
111 /* chunksize = about 1/32[sec] data */
112 blocksperchunk = ci->id3->frequency >> 6;
113 fmt->chunksize = blocksperchunk * fmt->blockalign;
115 max_chunk_count = (uint64_t)ci->id3->length * ci->id3->frequency
116 / (2000LL * fmt->chunksize / fmt->channels);
118 /* initialize seek table */
119 init_seek_table(max_chunk_count);
120 /* add first data */
121 add_adpcm_data(&cur_data);
124 return true;
127 static int16_t create_pcmdata(int ch, uint8_t nibble)
129 int32_t tmp_pcmdata = cur_data.pcmdata[ch];
130 int32_t step = cur_data.step[ch];
131 int32_t delta = step >> 3;
133 if (nibble & 4) delta += step;
134 if (nibble & 2) delta += (step >> 1);
135 if (nibble & 1) delta += (step >> 2);
137 if (nibble & 0x08)
138 tmp_pcmdata -= delta;
139 else
140 tmp_pcmdata += delta;
142 CLIP(tmp_pcmdata, -32768, 32767);
143 cur_data.pcmdata[ch] = tmp_pcmdata;
145 step = (step * amplification_table[nibble & 0x07]) >> 8;
146 CLIP(step, 127, 24576);
147 cur_data.step[ch] = step;
149 return cur_data.pcmdata[ch];
152 static int decode(const uint8_t *inbuf, size_t inbufsize,
153 int32_t *outbuf, int *outbufcount)
155 int ch;
156 size_t nsamples = 0;
158 /* read block header */
159 if (has_block_header)
161 for (ch = 0; ch < fmt->channels; ch++)
163 cur_data.pcmdata[ch] = inbuf[0] | (SE(inbuf[1]) << 8);
164 cur_data.step[ch] = inbuf[2] | (inbuf[3] << 8);
166 inbuf += 4;
167 inbufsize -= 4;
171 /* read block data */
172 ch = fmt->channels - 1;
173 while (inbufsize)
175 *outbuf++ = create_pcmdata(0, *inbuf ) << 13;
176 *outbuf++ = create_pcmdata(ch, *inbuf >> 4) << 13;
177 nsamples += 2;
179 inbuf++;
180 inbufsize--;
183 if (fmt->channels == 2)
184 nsamples >>= 1;
185 *outbufcount = nsamples;
187 if (!has_block_header)
188 add_adpcm_data(&cur_data);
190 return CODEC_OK;
193 static int decode_for_seek(const uint8_t *inbuf, size_t inbufsize)
195 int ch = fmt->channels - 1;
197 while (inbufsize)
199 create_pcmdata(0, *inbuf );
200 create_pcmdata(ch, *inbuf >> 4);
202 inbuf++;
203 inbufsize--;
206 add_adpcm_data(&cur_data);
208 return CODEC_OK;
211 static struct pcm_pos *get_seek_pos(long seek_time,
212 uint8_t *(*read_buffer)(size_t *realsize))
214 static struct pcm_pos newpos;
215 uint32_t new_count= 0;
217 if (seek_time > 0)
218 new_count = ((uint64_t)seek_time * ci->id3->frequency
219 / (1000LL * fmt->samplesperblock)) / blocksperchunk;
221 if (!has_block_header)
223 new_count = seek(new_count, &cur_data, read_buffer, &decode_for_seek);
225 newpos.pos = new_count * fmt->chunksize;
226 newpos.samples = new_count * blocksperchunk * fmt->samplesperblock;
227 return &newpos;
230 static struct pcm_codec codec = {
231 set_format,
232 get_seek_pos,
233 decode,
236 const struct pcm_codec *get_yamaha_adpcm_codec(void)
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;
244 return &codec;