1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
22 #include "ima_adpcm_common.h"
23 #include "support_formats.h"
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
= {
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
)
56 if (fmt
->bitspersample
< 2 || fmt
->bitspersample
> 5)
58 DEBUGF("CODEC_ERROR: swf adpcm must be 2, 3, 4 or 5 bitspersample: %d\n",
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
);
79 init_ima_adpcm_decoder(fmt
->bitspersample
, NULL
);
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;
94 newpos
.pos
= seekbits
>> 3;
95 newpos
.samples
= (((uint64_t)seek_time
* ci
->id3
->frequency
)
96 / (1000LL * fmt
->samplesperblock
))
97 * fmt
->samplesperblock
;
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);
120 static uint8_t get_data(const uint8_t **buf
, int bit
)
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
;
139 static int decode(const uint8_t *inbuf
, size_t inbufsize
,
140 int32_t *outbuf
, int *outbufcount
)
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;
151 *outbufcount
= count
;
153 /* read block header */
154 ch
= fmt
->channels
- 1;
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
);
164 init_pcmdata
[0] = (get_data(&inbuf
, 8) << 8) | get_data(&inbuf
, 8);
171 if (after_seek
&& lastbytebits
> 0)
176 if (lastbytebits
> 0)
177 init_pcmdata
[0] = ((lastbyte
<< (8 + lastbytebits
)) |
178 (get_data(&inbuf
, 8) << lastbytebits
) |
179 get_data(&inbuf
, lastbytebits
)) & 65535;
181 init_pcmdata
[0] = (get_data(&inbuf
, 8) << 8) | get_data(&inbuf
, 8);
185 init_index
[0] = get_data(&inbuf
, 6);
186 if (init_pcmdata
[0] > 32767)
187 init_pcmdata
[0] -= 65536;
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
;
199 *outbuf
++ = init_pcmdata
[1] << IMA_ADPCM_INC_DEPTH
;
201 set_decode_parameters(fmt
->channels
, init_pcmdata
, init_index
);
203 /* read block data */
206 *outbuf
++ = create_pcmdata(0, get_data(&inbuf
, fmt
->bitspersample
))
207 << IMA_ADPCM_INC_DEPTH
;
209 *outbuf
++ = create_pcmdata(ch
, get_data(&inbuf
, fmt
->bitspersample
))
210 << IMA_ADPCM_INC_DEPTH
;
214 lastbytebits
= (8 - validity_bits
) & 0x07;
216 /* calculates next read bytes */
217 fmt
->chunksize
= (blockbits
- validity_bits
+ 7) >> 3;
222 static const struct pcm_codec codec
= {
228 const struct pcm_codec
*get_swf_adpcm_codec(void)