Add missing #include in libpcm codecs
[kugel-rb.git] / apps / codecs / libpcm / yamaha_adpcm.c
blob073a1dc703c4a940437ad99f5838958025da50ed
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"
24 #include "support_formats.h"
27 * YAMAHA ADPCM
29 * References
30 * [1] YAMAHA, YAMAHA ADPCM ACM Driver Version 1.0.0.0, 2005
31 * [2] BlendWorks, YM2608 ADPCM,
32 * http://web.archive.org/web/20050208190547/www.memb.jp/~dearna/ma/ym2608/adpcm.html
33 * [3] Naoyuki Sawa, ADPCM no shikumi #1,
34 * http://www.piece-me.org/piece-lab/adpcm/adpcm1.html
35 * [4] ffmpeg source code, libavcodec/adpcm.c
38 /* ADPCM data block layout
40 * when the block header exists. (for example, encoding by YAMAHA ADPCM ACM Driver)
41 * blockAlign = (frequency / 60 + 4) * channels.
43 * block
44 * <Mono> (channels = 1)
45 * int16_t first value (Little endian)
46 * uint16_t first predictor (Little endian)
47 * uint8_t ADPCM data (1st data: 0-3 bit, 2nd data: 4-7 bit)
48 * ....
50 * <Stereo> (channels = 2)
51 * int16_t Left channel first value (Little endian)
52 * uint16_t Left channel first predictor (Little endian)
53 * int16_t Right channel first value (Little endian)
54 * uint16_t Right channel first predictor (Little endian)
55 * uint8_t ADPCM data (Left channel: 0-3 bit, Right channel: 4-7 bit)
56 * ....
58 * when the block header does not exist. (for example, encoding by ffmpeg)
59 * blockAlign = 8000
61 * block
62 * <Mono> (channels = 1)
63 * uint8_t ADPCM data (1st data: 0-3 bit, 2nd data: 4-7 bit)
64 * ....
66 * <Stereo> (channels = 2)
67 * uint8_t ADPCM data (Left channel: 0-3 bit, Right channel: 4-7 bit)
68 * ....
71 static const int32_t amplification_table[] ICONST_ATTR = {
72 230, 230, 230, 230, 307, 409, 512, 614, 230, 230, 230, 230, 307, 409, 512, 614
75 static bool has_block_header = false;
77 static struct adpcm_data cur_data;
78 static int blocksperchunk;
80 static struct pcm_format *fmt;
82 static bool set_format(struct pcm_format *format)
84 fmt = format;
86 if (fmt->bitspersample != 4)
88 DEBUGF("CODEC_ERROR: yamaha adpcm must be 4 bitspersample: %d\n",
89 fmt->bitspersample);
90 return false;
93 /* check exists block header */
94 if (fmt->blockalign == ((ci->id3->frequency / 60) + 4) * fmt->channels)
96 has_block_header = true;
98 /* chunksize = about 1/30 [sec] data */
99 fmt->chunksize = fmt->blockalign;
100 blocksperchunk = 1;
102 else
104 uint32_t max_chunk_count;
106 has_block_header = false;
108 /* blockalign = 2 * channels samples */
109 fmt->blockalign = fmt->channels;
110 fmt->samplesperblock = 2;
112 /* chunksize = about 1/32[sec] data */
113 blocksperchunk = ci->id3->frequency >> 6;
114 fmt->chunksize = blocksperchunk * fmt->blockalign;
116 max_chunk_count = (uint64_t)ci->id3->length * ci->id3->frequency
117 / (2000LL * fmt->chunksize / fmt->channels);
119 /* initialize seek table */
120 init_seek_table(max_chunk_count);
121 /* add first data */
122 add_adpcm_data(&cur_data);
125 return true;
128 static int16_t create_pcmdata(int ch, uint8_t nibble)
130 int32_t tmp_pcmdata = cur_data.pcmdata[ch];
131 int32_t step = cur_data.step[ch];
132 int32_t delta = step >> 3;
134 if (nibble & 4) delta += step;
135 if (nibble & 2) delta += (step >> 1);
136 if (nibble & 1) delta += (step >> 2);
138 if (nibble & 0x08)
139 tmp_pcmdata -= delta;
140 else
141 tmp_pcmdata += delta;
143 CLIP(tmp_pcmdata, -32768, 32767);
144 cur_data.pcmdata[ch] = tmp_pcmdata;
146 step = (step * amplification_table[nibble & 0x07]) >> 8;
147 CLIP(step, 127, 24576);
148 cur_data.step[ch] = step;
150 return cur_data.pcmdata[ch];
153 static int decode(const uint8_t *inbuf, size_t inbufsize,
154 int32_t *outbuf, int *outbufcount)
156 int ch;
157 size_t nsamples = 0;
159 /* read block header */
160 if (has_block_header)
162 for (ch = 0; ch < fmt->channels; ch++)
164 cur_data.pcmdata[ch] = inbuf[0] | (SE(inbuf[1]) << 8);
165 cur_data.step[ch] = inbuf[2] | (inbuf[3] << 8);
167 inbuf += 4;
168 inbufsize -= 4;
172 /* read block data */
173 ch = fmt->channels - 1;
174 while (inbufsize)
176 *outbuf++ = create_pcmdata(0, *inbuf ) << 13;
177 *outbuf++ = create_pcmdata(ch, *inbuf >> 4) << 13;
178 nsamples += 2;
180 inbuf++;
181 inbufsize--;
184 if (fmt->channels == 2)
185 nsamples >>= 1;
186 *outbufcount = nsamples;
188 if (!has_block_header)
189 add_adpcm_data(&cur_data);
191 return CODEC_OK;
194 static int decode_for_seek(const uint8_t *inbuf, size_t inbufsize)
196 int ch = fmt->channels - 1;
198 while (inbufsize)
200 create_pcmdata(0, *inbuf );
201 create_pcmdata(ch, *inbuf >> 4);
203 inbuf++;
204 inbufsize--;
207 add_adpcm_data(&cur_data);
209 return CODEC_OK;
212 static struct pcm_pos *get_seek_pos(long seek_time,
213 uint8_t *(*read_buffer)(size_t *realsize))
215 static struct pcm_pos newpos;
216 uint32_t new_count= 0;
218 if (seek_time > 0)
219 new_count = ((uint64_t)seek_time * ci->id3->frequency
220 / (1000LL * fmt->samplesperblock)) / blocksperchunk;
222 if (!has_block_header)
224 new_count = seek(new_count, &cur_data, read_buffer, &decode_for_seek);
226 newpos.pos = new_count * fmt->chunksize;
227 newpos.samples = new_count * blocksperchunk * fmt->samplesperblock;
228 return &newpos;
231 static struct pcm_codec codec = {
232 set_format,
233 get_seek_pos,
234 decode,
237 const struct pcm_codec *get_yamaha_adpcm_codec(void)
239 /* initialize first step, pcm data */
240 cur_data.pcmdata[0] = 0;
241 cur_data.pcmdata[1] = 0;
242 cur_data.step[0] = 127;
243 cur_data.step[1] = 127;
245 return &codec;