Add missing #include in libpcm codecs
[kugel-rb.git] / apps / codecs / libpcm / qt_ima_adpcm.c
blobe99a91796db99aaa7d819e46ef29a8e7d58c9adb
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 "ima_adpcm_common.h"
24 #include "support_formats.h"
27 * Apple QuickTime IMA ADPCM
29 * References
30 * [1] Multimedia Wiki, Apple QuickTime IMA ADPCM
31 * URL:http://wiki.multimedia.cx/index.php?title=Apple_QuickTime_IMA_ADPCM
32 * [2] Apple Inc., Technical Note TN1081 Understanding the Differences Between
33 * Apple and Windows IMA-ADPCM Compressed Sound Files, 1996
34 * [3] ffmpeg source code, libavcodec/adpcm.c
37 static struct pcm_format *fmt;
39 static bool set_format(struct pcm_format *format)
41 fmt = format;
43 if (fmt->bitspersample != 4)
45 DEBUGF("CODEC_ERROR: quicktime ima adpcm must be 4 bitspersample: %d\n",
46 fmt->bitspersample);
47 return false;
50 fmt->blockalign = 34 * fmt->channels;
51 fmt->samplesperblock = 64;
53 /* chunksize = about 1/50[s] data */
54 fmt->chunksize = (ci->id3->frequency / (50 * fmt->samplesperblock))
55 * fmt->blockalign;
57 init_ima_adpcm_decoder(4, NULL);
58 return true;
61 static struct pcm_pos *get_seek_pos(long seek_time,
62 uint8_t *(*read_buffer)(size_t *realsize))
64 static struct pcm_pos newpos;
65 uint32_t newblock = ((uint64_t)seek_time * ci->id3->frequency)
66 / (1000LL * fmt->samplesperblock);
68 (void)read_buffer;
69 newpos.pos = newblock * fmt->blockalign;
70 newpos.samples = newblock * fmt->samplesperblock;
71 return &newpos;
74 static int decode(const uint8_t *inbuf, size_t inbufsize,
75 int32_t *outbuf, int *outbufcount)
77 int ch;
78 size_t nsamples = 0;
79 int block_size;
80 int32_t *pcmbuf;
81 int32_t init_pcmdata;
82 int8_t init_index;
84 while (inbufsize > 0)
86 for (ch = 0; ch < fmt->channels; ch++)
88 /* read block header */
89 init_pcmdata = (inbuf[0] << 8)|(inbuf[1] & 0x80);
90 if (init_pcmdata > 32767)
91 init_pcmdata -= 65536;
93 init_index = inbuf[1] & 0x7f;
94 if (init_index > 88)
96 DEBUGF("CODEC_ERROR: quicktime ima adpcm illegal step index=%d > 88\n",
97 init_index);
98 return CODEC_ERROR;
101 inbuf += 2;
102 inbufsize -= 2;
104 set_decode_parameters(1, &init_pcmdata, &init_index);
106 /* read block data */
107 pcmbuf = outbuf + ch;
108 for (block_size = 32; block_size > 0 && inbufsize > 0; block_size--, inbufsize--)
110 *pcmbuf = create_pcmdata_size4(ch, *inbuf ) << 13;
111 pcmbuf += fmt->channels;
112 *pcmbuf = create_pcmdata_size4(ch, *inbuf >> 4) << 13;
113 pcmbuf += fmt->channels;
114 nsamples += 2;
115 inbuf++;
118 outbuf += 64 * fmt->channels;
121 if (fmt->channels == 2)
122 nsamples >>= 1;
123 *outbufcount = nsamples;
125 return CODEC_OK;
128 static const struct pcm_codec codec = {
129 set_format,
130 get_seek_pos,
131 decode,
134 const struct pcm_codec *get_qt_ima_adpcm_codec(void)
136 return &codec;