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(uint32_t seek_val
, int seek_mode
,
85 uint8_t *(*read_buffer
)(size_t *realsize
))
87 static struct pcm_pos newpos
;
88 uint32_t chunkbits
= blockbits
;
89 uint32_t seekblocks
= (seek_mode
== PCM_SEEK_TIME
)?
90 ((uint64_t)seek_val
* ci
->id3
->frequency
)
91 / (1000LL * fmt
->samplesperblock
) :
92 ((seek_val
<< 3) - 2) / blockbits
;
93 uint32_t seekbits
= seekblocks
* blockbits
+ 2;
97 newpos
.pos
= seekbits
>> 3;
98 newpos
.samples
= seekblocks
* fmt
->samplesperblock
;
108 lastbytebits
= seekbits
& 0x07;
109 if (lastbytebits
!= 0)
110 chunkbits
-= (8 - lastbytebits
);
113 /* calculates next read bytes */
114 fmt
->chunksize
= (chunkbits
>> 3) + (((chunkbits
& 0x07) > 0)?1:0)
115 + ((lastbytebits
> 0)?1:0);
121 static uint8_t get_data(const uint8_t **buf
, int bit
)
124 uint8_t mask
= (1 << bit
) - 1;
126 if (validity_bits
>= bit
)
128 validity_bits
-= bit
;
129 return (**buf
>> validity_bits
) & mask
;
132 if (validity_bits
> 0)
133 res
= **buf
<< (bit
- validity_bits
);
135 validity_bits
+= 8 - bit
;
136 res
= (res
| (*(++(*buf
)) >> validity_bits
)) & mask
;
140 static int decode(const uint8_t *inbuf
, size_t inbufsize
,
141 int32_t *outbuf
, int *outbufcount
)
145 int count
= ((size_t)fmt
->chunksize
== inbufsize
) ? fmt
->samplesperblock
:
146 GET_SAMPLE_COUNT(inbufsize
);
147 int32_t init_pcmdata
[2];
148 int8_t init_index
[2];
149 static uint8_t lastbyte
= 0;
152 *outbufcount
= count
;
154 /* read block header */
155 ch
= fmt
->channels
- 1;
158 adpcm_code_size
= get_data(&inbuf
, 2) + 2;
159 if (fmt
->bitspersample
!= adpcm_code_size
)
161 DEBUGF("CODEC_ERROR: swf adpcm different adpcm code size=%d != %d\n",
162 adpcm_code_size
, fmt
->bitspersample
);
165 init_pcmdata
[0] = (get_data(&inbuf
, 8) << 8) | get_data(&inbuf
, 8);
172 if (after_seek
&& lastbytebits
> 0)
177 if (lastbytebits
> 0)
178 init_pcmdata
[0] = ((lastbyte
<< (8 + lastbytebits
)) |
179 (get_data(&inbuf
, 8) << lastbytebits
) |
180 get_data(&inbuf
, lastbytebits
)) & 65535;
182 init_pcmdata
[0] = (get_data(&inbuf
, 8) << 8) | get_data(&inbuf
, 8);
186 init_index
[0] = get_data(&inbuf
, 6);
187 if (init_pcmdata
[0] > 32767)
188 init_pcmdata
[0] -= 65536;
192 init_pcmdata
[1] = (get_data(&inbuf
, 8) << 8) | get_data(&inbuf
, 8);
193 init_index
[1] = get_data(&inbuf
, 6);
194 if (init_pcmdata
[1] > 32767)
195 init_pcmdata
[1] -= 65536;
198 *outbuf
++ = init_pcmdata
[0] << IMA_ADPCM_INC_DEPTH
;
200 *outbuf
++ = init_pcmdata
[1] << IMA_ADPCM_INC_DEPTH
;
202 set_decode_parameters(fmt
->channels
, init_pcmdata
, init_index
);
204 /* read block data */
207 *outbuf
++ = create_pcmdata(0, get_data(&inbuf
, fmt
->bitspersample
))
208 << IMA_ADPCM_INC_DEPTH
;
210 *outbuf
++ = create_pcmdata(ch
, get_data(&inbuf
, fmt
->bitspersample
))
211 << IMA_ADPCM_INC_DEPTH
;
215 lastbytebits
= (8 - validity_bits
) & 0x07;
217 /* calculates next read bytes */
218 fmt
->chunksize
= (blockbits
- validity_bits
+ 7) >> 3;
223 static const struct pcm_codec codec
= {
229 const struct pcm_codec
*get_swf_adpcm_codec(void)