summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libpcm/qt_ima_adpcm.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libpcm/qt_ima_adpcm.c')
-rw-r--r--lib/rbcodec/codecs/libpcm/qt_ima_adpcm.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libpcm/qt_ima_adpcm.c b/lib/rbcodec/codecs/libpcm/qt_ima_adpcm.c
new file mode 100644
index 0000000000..d7b3360eb3
--- /dev/null
+++ b/lib/rbcodec/codecs/libpcm/qt_ima_adpcm.c
@@ -0,0 +1,138 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Yoshihisa Uchida
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "codeclib.h"
22#include "ima_adpcm_common.h"
23#include "support_formats.h"
24
25/*
26 * Apple QuickTime IMA ADPCM
27 *
28 * References
29 * [1] Multimedia Wiki, Apple QuickTime IMA ADPCM
30 * URL:http://wiki.multimedia.cx/index.php?title=Apple_QuickTime_IMA_ADPCM
31 * [2] Apple Inc., Technical Note TN1081 Understanding the Differences Between
32 * Apple and Windows IMA-ADPCM Compressed Sound Files, 1996
33 * [3] ffmpeg source code, libavcodec/adpcm.c
34 */
35
36static struct pcm_format *fmt;
37
38static bool set_format(struct pcm_format *format)
39{
40 fmt = format;
41
42 if (fmt->bitspersample != 4)
43 {
44 DEBUGF("CODEC_ERROR: quicktime ima adpcm must be 4 bitspersample: %d\n",
45 fmt->bitspersample);
46 return false;
47 }
48
49 fmt->blockalign = 34 * fmt->channels;
50 fmt->samplesperblock = 64;
51
52 /* chunksize = about 1/50[s] data */
53 fmt->chunksize = (ci->id3->frequency / (50 * fmt->samplesperblock))
54 * fmt->blockalign;
55
56 init_ima_adpcm_decoder(4, NULL);
57 return true;
58}
59
60static struct pcm_pos *get_seek_pos(uint32_t seek_val, int seek_mode,
61 uint8_t *(*read_buffer)(size_t *realsize))
62{
63 static struct pcm_pos newpos;
64 uint32_t newblock = (seek_mode == PCM_SEEK_TIME) ?
65 ((uint64_t)seek_val * ci->id3->frequency / 1000LL)
66 / fmt->samplesperblock :
67 seek_val / fmt->blockalign;
68
69 (void)read_buffer;
70 newpos.pos = newblock * fmt->blockalign;
71 newpos.samples = newblock * fmt->samplesperblock;
72 return &newpos;
73}
74
75static int decode(const uint8_t *inbuf, size_t inbufsize,
76 int32_t *outbuf, int *outbufcount)
77{
78 int ch;
79 size_t nsamples = 0;
80 int block_size;
81 int32_t *pcmbuf;
82 int32_t init_pcmdata;
83 int8_t init_index;
84
85 while (inbufsize > 0)
86 {
87 for (ch = 0; ch < fmt->channels; ch++)
88 {
89 /* read block header */
90 init_pcmdata = (inbuf[0] << 8)|(inbuf[1] & 0x80);
91 if (init_pcmdata > 32767)
92 init_pcmdata -= 65536;
93
94 init_index = inbuf[1] & 0x7f;
95 if (init_index > 88)
96 {
97 DEBUGF("CODEC_ERROR: quicktime ima adpcm illegal step index=%d > 88\n",
98 init_index);
99 return CODEC_ERROR;
100 }
101
102 inbuf += 2;
103 inbufsize -= 2;
104
105 set_decode_parameters(1, &init_pcmdata, &init_index);
106
107 /* read block data */
108 pcmbuf = outbuf + ch;
109 for (block_size = 32; block_size > 0 && inbufsize > 0; block_size--, inbufsize--)
110 {
111 *pcmbuf = create_pcmdata_size4(ch, *inbuf ) << IMA_ADPCM_INC_DEPTH;
112 pcmbuf += fmt->channels;
113 *pcmbuf = create_pcmdata_size4(ch, *inbuf >> 4) << IMA_ADPCM_INC_DEPTH;
114 pcmbuf += fmt->channels;
115 nsamples += 2;
116 inbuf++;
117 }
118 }
119 outbuf += 64 * fmt->channels;
120 }
121
122 if (fmt->channels == 2)
123 nsamples >>= 1;
124 *outbufcount = nsamples;
125
126 return CODEC_OK;
127}
128
129static const struct pcm_codec codec = {
130 set_format,
131 get_seek_pos,
132 decode,
133 };
134
135const struct pcm_codec *get_qt_ima_adpcm_codec(void)
136{
137 return &codec;
138}