libpcm: decoded pcm depth corrects.
[kugel-rb.git] / apps / codecs / vox.c
blob290fae08004ad3b2cf2415cd23e91b47a2f35ca0
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 = CODEC_OK;
50 uint32_t decodedsamples;
51 size_t n;
52 int bufcount;
53 int endofstream;
54 uint8_t *voxbuf;
55 off_t firstblockposn; /* position of the first block in file */
56 const struct pcm_codec *codec;
57 int offset = 0;
59 /* Generic codec initialisation */
60 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH);
62 next_track:
63 if (codec_init()) {
64 DEBUGF("codec_init() error\n");
65 status = CODEC_ERROR;
66 goto exit;
69 while (!*ci->taginfo_ready && !ci->stop_codec)
70 ci->sleep(1);
72 codec_set_replaygain(ci->id3);
74 ci->memset(&format, 0, sizeof(struct pcm_format));
76 /* set format */
77 format.channels = 1;
78 format.bitspersample = 4;
79 format.numbytes = ci->id3->filesize;
80 format.blockalign = 1;
82 /* advance to first WAVE chunk */
83 ci->advance_buffer(offset);
85 firstblockposn = offset;
87 decodedsamples = 0;
88 bytesdone = 0;
91 * get codec
92 * supports dialogic oki adpcm only
94 codec = get_dialogic_oki_adpcm_codec();
95 if (!codec)
97 DEBUGF("CODEC_ERROR: dialogic oki adpcm codec does not load.\n");
98 status = CODEC_ERROR;
99 goto done;
102 if (!codec->set_format(&format))
104 status = CODEC_ERROR;
105 goto done;
108 if (format.numbytes == 0) {
109 DEBUGF("CODEC_ERROR: data size is 0\n");
110 status = CODEC_ERROR;
111 goto done;
114 /* check chunksize */
115 if (format.chunksize * 2 > PCM_SAMPLE_SIZE)
116 format.chunksize = PCM_SAMPLE_SIZE / 2;
117 if (format.chunksize == 0)
119 DEBUGF("CODEC_ERROR: chunksize is 0\n");
120 status = CODEC_ERROR;
121 goto done;
124 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
125 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
127 /* The main decoder loop */
128 endofstream = 0;
130 while (!endofstream) {
131 ci->yield();
132 if (ci->stop_codec || ci->new_track) {
133 break;
136 if (ci->seek_time) {
137 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, &read_buffer);
139 decodedsamples = newpos->samples;
140 if (newpos->pos > format.numbytes)
141 break;
142 if (ci->seek_buffer(firstblockposn + newpos->pos))
144 bytesdone = newpos->pos;
146 ci->seek_complete();
149 voxbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
150 if (n == 0)
151 break; /* End of stream */
152 if (bytesdone + n > format.numbytes) {
153 n = format.numbytes - bytesdone;
154 endofstream = 1;
157 status = codec->decode(voxbuf, n, samples, &bufcount);
158 if (status == CODEC_ERROR)
160 DEBUGF("codec error\n");
161 goto done;
164 ci->pcmbuf_insert(samples, NULL, bufcount);
165 ci->advance_buffer(n);
166 bytesdone += n;
167 decodedsamples += bufcount;
169 if (bytesdone >= format.numbytes)
170 endofstream = 1;
171 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
173 status = CODEC_OK;
175 done:
176 if (ci->request_next_track())
177 goto next_track;
179 exit:
180 return status;