Calibrate iPod Classic battery gauge a bit better
[kugel-rb.git] / apps / codecs / vox.c
blobc7f39342c3d304097ec56acbd4d1bb7ec51944cc
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 ****************************************************************************/
22 #include "codeclib.h"
23 #include "codecs/libpcm/support_formats.h"
25 CODEC_HEADER
27 /* vox codec (Dialogic telephony file formats) */
29 #define PCM_SAMPLE_SIZE (2048)
31 static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
33 static struct pcm_format format;
34 static uint32_t bytesdone;
36 static uint8_t *read_buffer(size_t *realsize)
38 uint8_t *buffer = (uint8_t *)ci->request_buffer(realsize, format.chunksize);
39 if (bytesdone + (*realsize) > format.numbytes)
40 *realsize = format.numbytes - bytesdone;
41 bytesdone += *realsize;
42 ci->advance_buffer(*realsize);
43 return buffer;
46 /* this is the codec entry point */
47 enum codec_status codec_main(void)
49 int status;
50 uint32_t decodedsamples;
51 size_t n;
52 int bufcount;
53 int endofstream;
54 uint8_t *voxbuf;
55 off_t firstblockposn = 0; /* position of the first block in file */
56 const struct pcm_codec *codec;
58 /* Generic codec initialisation */
59 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
61 next_track:
62 status = CODEC_OK;
64 if (codec_init()) {
65 DEBUGF("codec_init() error\n");
66 status = CODEC_ERROR;
67 goto exit;
70 if (codec_wait_taginfo() != 0)
71 goto done;
73 codec_set_replaygain(ci->id3);
75 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
76 bytesdone = ci->id3->offset;
78 ci->memset(&format, 0, sizeof(struct pcm_format));
80 /* set format */
81 format.channels = 1;
82 format.bitspersample = 4;
83 format.numbytes = ci->id3->filesize;
84 format.blockalign = 1;
86 /* advance to first WAVE chunk */
87 firstblockposn = 0;
88 decodedsamples = 0;
89 ci->advance_buffer(firstblockposn);
92 * get codec
93 * supports dialogic oki adpcm only
95 codec = get_dialogic_oki_adpcm_codec();
96 if (!codec)
98 DEBUGF("CODEC_ERROR: dialogic oki adpcm codec does not load.\n");
99 status = CODEC_ERROR;
100 goto done;
103 if (!codec->set_format(&format))
105 status = CODEC_ERROR;
106 goto done;
109 if (format.numbytes == 0) {
110 DEBUGF("CODEC_ERROR: data size is 0\n");
111 status = CODEC_ERROR;
112 goto done;
115 /* check chunksize */
116 if (format.chunksize * 2 > PCM_SAMPLE_SIZE)
117 format.chunksize = PCM_SAMPLE_SIZE / 2;
118 if (format.chunksize == 0)
120 DEBUGF("CODEC_ERROR: chunksize is 0\n");
121 status = CODEC_ERROR;
122 goto done;
125 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
126 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
128 /* make sure we're at the correct offset */
129 if (bytesdone > (uint32_t) firstblockposn) {
130 /* Round down to previous block */
131 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
132 PCM_SEEK_POS, &read_buffer);
134 if (newpos->pos > format.numbytes)
135 goto done;
136 if (ci->seek_buffer(firstblockposn + newpos->pos))
138 bytesdone = newpos->pos;
139 decodedsamples = newpos->samples;
141 ci->seek_complete();
142 } else {
143 /* already where we need to be */
144 bytesdone = 0;
147 /* The main decoder loop */
148 endofstream = 0;
150 while (!endofstream) {
151 ci->yield();
152 if (ci->stop_codec || ci->new_track) {
153 break;
156 if (ci->seek_time) {
157 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME,
158 &read_buffer);
160 if (newpos->pos > format.numbytes)
161 break;
162 if (ci->seek_buffer(firstblockposn + newpos->pos))
164 bytesdone = newpos->pos;
165 decodedsamples = newpos->samples;
167 ci->seek_complete();
170 voxbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
171 if (n == 0)
172 break; /* End of stream */
173 if (bytesdone + n > format.numbytes) {
174 n = format.numbytes - bytesdone;
175 endofstream = 1;
178 status = codec->decode(voxbuf, n, samples, &bufcount);
179 if (status == CODEC_ERROR)
181 DEBUGF("codec error\n");
182 goto done;
185 ci->pcmbuf_insert(samples, NULL, bufcount);
186 ci->advance_buffer(n);
187 bytesdone += n;
188 decodedsamples += bufcount;
190 if (bytesdone >= format.numbytes)
191 endofstream = 1;
192 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
195 done:
196 if (ci->request_next_track())
197 goto next_track;
199 exit:
200 return status;