ima adpcm/swf adpcm: corrects the problem the noise occurs after the play ends.
[kugel-rb.git] / apps / codecs / libpcm / swf_adpcm.c
blobebc4328c590c8dbb9ecdcca846d0eed4224a0980
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 "ima_adpcm_common.h"
23 #include "support_formats.h"
26 * Adobe SWF ADPCM
28 * References
29 * [1] Adobe, SWF File Format Specification Version 10, 2008
30 * [2] Jack Jansen, adpcm.c in adpcm.zip
31 * [3] ffmpeg source code, libavcodec/adpcm.c
34 /* step index table when bitspersample is 3.
35 * (when bitspersample is 2, 4, 5, step index table uses the table
36 * which is defined ima_adpcm_common.c.)
38 static const int index_table[4] ICONST_ATTR = {
39 -1, -1, 2, 4,
42 static int validity_bits = 8;
43 static bool first_block = true;
44 static int blockbits = 0;
45 static int lastbytebits = 0;
46 static bool after_seek = false;
48 static struct pcm_format *fmt;
50 #define GET_SAMPLE_COUNT(s) ((((s) << 3) / fmt->channels - 22) / fmt->bitspersample + 1)
52 static bool set_format(struct pcm_format *format)
54 fmt = format;
56 if (fmt->bitspersample < 2 || fmt->bitspersample > 5)
58 DEBUGF("CODEC_ERROR: swf adpcm must be 2, 3, 4 or 5 bitspersample: %d\n",
59 fmt->bitspersample);
60 return false;
63 if (fmt->samplesperblock == 0)
64 fmt->samplesperblock = (((fmt->blockalign << 3) - 2) / fmt->channels - 22)
65 / fmt->bitspersample + 1;
67 blockbits = ((fmt->samplesperblock - 1) * fmt->bitspersample + 22) * fmt->channels;
70 * chunksize = about 93 [ms] data (frequency:44.1kHz, 4096 [sample/block])
71 * chunksize changes depending upon the position of block.
73 fmt->chunksize = (blockbits + 9) >> 3;
75 /* initialize for ima adpcm common functions */
76 if (fmt->bitspersample == 3)
77 init_ima_adpcm_decoder(fmt->bitspersample, index_table);
78 else
79 init_ima_adpcm_decoder(fmt->bitspersample, NULL);
81 return true;
84 static struct pcm_pos *get_seek_pos(long seek_time,
85 uint8_t *(*read_buffer)(size_t *realsize))
87 static struct pcm_pos newpos;
88 uint32_t chunkbits = blockbits;
89 uint32_t seekbits = (((uint64_t)seek_time * ci->id3->frequency)
90 / (1000LL * fmt->samplesperblock)) * blockbits + 2;
92 (void)read_buffer;
94 newpos.pos = seekbits >> 3;
95 newpos.samples = (((uint64_t)seek_time * ci->id3->frequency)
96 / (1000LL * fmt->samplesperblock))
97 * fmt->samplesperblock;
99 if (newpos.pos == 0)
101 first_block = true;
102 lastbytebits = 0;
104 else
106 first_block = false;
107 lastbytebits = seekbits & 0x07;
108 if (lastbytebits != 0)
109 chunkbits -= (8 - lastbytebits);
112 /* calculates next read bytes */
113 fmt->chunksize = (chunkbits >> 3) + (((chunkbits & 0x07) > 0)?1:0)
114 + ((lastbytebits > 0)?1:0);
116 after_seek = true;
117 return &newpos;
120 static uint8_t get_data(const uint8_t **buf, int bit)
122 uint8_t res = 0;
123 uint8_t mask = (1 << bit) - 1;
125 if (validity_bits >= bit)
127 validity_bits -= bit;
128 return (**buf >> validity_bits) & mask;
131 if (validity_bits > 0)
132 res = **buf << (bit - validity_bits);
134 validity_bits += 8 - bit;
135 res = (res | (*(++(*buf)) >> validity_bits)) & mask;
136 return res;
139 static int decode(const uint8_t *inbuf, size_t inbufsize,
140 int32_t *outbuf, int *outbufcount)
142 int ch;
143 int adpcm_code_size;
144 int count = ((size_t)fmt->chunksize == inbufsize) ? fmt->samplesperblock :
145 GET_SAMPLE_COUNT(inbufsize);
146 int32_t init_pcmdata[2];
147 int8_t init_index[2];
148 static uint8_t lastbyte = 0;
150 validity_bits = 8;
151 *outbufcount = count;
153 /* read block header */
154 ch = fmt->channels - 1;
155 if (first_block)
157 adpcm_code_size = get_data(&inbuf, 2) + 2;
158 if (fmt->bitspersample != adpcm_code_size)
160 DEBUGF("CODEC_ERROR: swf adpcm different adpcm code size=%d != %d\n",
161 adpcm_code_size, fmt->bitspersample);
162 return CODEC_ERROR;
164 init_pcmdata[0] = (get_data(&inbuf, 8) << 8) | get_data(&inbuf, 8);
166 lastbytebits = 0;
167 first_block = false;
169 else
171 if (after_seek && lastbytebits > 0)
173 lastbyte = *inbuf++;
174 after_seek = false;
176 if (lastbytebits > 0)
177 init_pcmdata[0] = ((lastbyte << (8 + lastbytebits)) |
178 (get_data(&inbuf, 8) << lastbytebits) |
179 get_data(&inbuf, lastbytebits)) & 65535;
180 else
181 init_pcmdata[0] = (get_data(&inbuf, 8) << 8) | get_data(&inbuf, 8);
183 after_seek = false;
185 init_index[0] = get_data(&inbuf, 6);
186 if (init_pcmdata[0] > 32767)
187 init_pcmdata[0] -= 65536;
189 if (ch > 0)
191 init_pcmdata[1] = (get_data(&inbuf, 8) << 8) | get_data(&inbuf, 8);
192 init_index[1] = get_data(&inbuf, 6);
193 if (init_pcmdata[1] > 32767)
194 init_pcmdata[1] -= 65536;
197 *outbuf++ = init_pcmdata[0] << IMA_ADPCM_INC_DEPTH;
198 if (ch > 0)
199 *outbuf++ = init_pcmdata[1] << IMA_ADPCM_INC_DEPTH;
201 set_decode_parameters(fmt->channels, init_pcmdata, init_index);
203 /* read block data */
204 while (--count > 0)
206 *outbuf++ = create_pcmdata(0, get_data(&inbuf, fmt->bitspersample))
207 << IMA_ADPCM_INC_DEPTH;
208 if (ch > 0)
209 *outbuf++ = create_pcmdata(ch, get_data(&inbuf, fmt->bitspersample))
210 << IMA_ADPCM_INC_DEPTH;
213 lastbyte = *inbuf;
214 lastbytebits = (8 - validity_bits) & 0x07;
216 /* calculates next read bytes */
217 fmt->chunksize = (blockbits - validity_bits + 7) >> 3;
219 return CODEC_OK;
222 static const struct pcm_codec codec = {
223 set_format,
224 get_seek_pos,
225 decode,
228 const struct pcm_codec *get_swf_adpcm_codec(void)
230 first_block = true;
231 lastbytebits = 0;
232 after_seek = false;
234 return &codec;