summaryrefslogtreecommitdiffstats
path: root/apps/codecs/vorbis.c
blob: 1900364a898c4f21af2e26fcd7df5769d4d3091b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Bjrn Stenberg
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "codeclib.h"
#include "Tremor/ivorbisfile.h"
#include "Tremor/ogg.h"

CODEC_HEADER

static struct codec_api *rb;

/* Some standard functions and variables needed by Tremor */

size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
{
    (void)datasource;
    return rb->read_filebuf(ptr, nmemb*size);
}

int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence)
{
    (void)datasource;
    (void)offset;
    (void)whence;
    return -1;
}

int seek_handler(void *datasource, ogg_int64_t offset, int whence)
{
    (void)datasource;

    if (whence == SEEK_CUR) {
        offset += rb->curpos;
    } else if (whence == SEEK_END) {
        offset += rb->filesize;
    }

    if (rb->seek_buffer(offset)) {
        return 0;
    }

    return -1;
}

int close_handler(void *datasource)
{
    (void)datasource;
    return 0;
}

long tell_handler(void *datasource)
{
    (void)datasource;
    return rb->curpos;
}

/* This sets the DSP parameters based on the current logical bitstream
 * (sampling rate, number of channels, etc).  It also tries to guess
 * reasonable buffer parameters based on the current quality setting.
 */
bool vorbis_set_codec_parameters(OggVorbis_File *vf)
{
    vorbis_info *vi;

    vi = ov_info(vf, -1);

    if (vi == NULL) {
        //rb->splash(HZ*2, true, "Vorbis Error");
        return false;
    }

    rb->configure(DSP_SET_FREQUENCY, (int *)rb->id3->frequency);
    codec_set_replaygain(rb->id3);

    if (vi->channels == 2) {
          rb->configure(DSP_SET_STEREO_MODE, (int *)STEREO_NONINTERLEAVED);
    } else if (vi->channels == 1) {
          rb->configure(DSP_SET_STEREO_MODE, (int *)STEREO_MONO);
    }

    return true;
}

#ifdef USE_IRAM
extern char iramcopy[];
extern char iramstart[];
extern char iramend[];
extern char iedata[];
extern char iend[];
#endif

/* this is the codec entry point */
enum codec_status codec_start(struct codec_api *api)
{
    ov_callbacks callbacks;
    OggVorbis_File vf;
    ogg_int32_t **pcm;

    int error;
    long n;
    int current_section;
    int previous_section = -1;
    int eof;
    ogg_int64_t vf_offsets[2];
    ogg_int64_t vf_dataoffsets;
    ogg_uint32_t vf_serialnos;
    ogg_int64_t vf_pcmlengths[2];

    rb = api;

    #ifdef USE_IRAM
    rb->memcpy(iramstart, iramcopy, iramend - iramstart);
    rb->memset(iedata, 0, iend - iedata);
    #endif

    rb->configure(DSP_DITHER, (bool *)false);
    rb->configure(DSP_SET_SAMPLE_DEPTH, (long *)24);
    rb->configure(DSP_SET_CLIP_MAX, (long *)((1 << 24) - 1));
    rb->configure(DSP_SET_CLIP_MIN, (long *)-((1 << 24) - 1));
    /* Note: These are sane defaults for these values.  Perhaps
     * they should be set differently based on quality setting
     */

    /* The chunk size below is magic.  If set any lower, resume
     * doesn't work properly (ov_raw_seek() does the wrong thing).
     */
    rb->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (long *)(1024*256));

/* We need to flush reserver memory every track load. */
next_track:
    if (codec_init(rb)) {
        error = CODEC_ERROR;
        goto exit;
    }

    while (!*rb->taginfo_ready && !rb->stop_codec)
        rb->sleep(1);

    /* Create a decoder instance */
    callbacks.read_func = read_handler;
    callbacks.seek_func = initial_seek_handler;
    callbacks.tell_func = tell_handler;
    callbacks.close_func = close_handler;

    /* Open a non-seekable stream */
    error = ov_open_callbacks(rb, &vf, NULL, 0, callbacks);
    
    /* If the non-seekable open was successful, we need to supply the missing
     * data to make it seekable.  This is a hack, but it's reasonable since we
     * don't want to run the whole file through the buffer before we start
     * playing.  Using Tremor's seekable open routine would cause us to do
     * this, so we pretend not to be seekable at first.  Then we fill in the
     * missing fields of vf with 1) information in rb->id3, and 2) info
     * obtained by Tremor in the above ov_open call.
     *
     * Note that this assumes there is only ONE logical Vorbis bitstream in our
     * physical Ogg bitstream.  This is verified in metadata.c, well before we
     * get here.
     */
    if (!error) {
         vf.offsets = vf_offsets;
         vf.dataoffsets = &vf_dataoffsets;
         vf.serialnos = &vf_serialnos;
         vf.pcmlengths = vf_pcmlengths;

         vf.offsets[0] = 0;
         vf.offsets[1] = rb->id3->filesize;
         vf.dataoffsets[0] = vf.offset;
         vf.pcmlengths[0] = 0;
         vf.pcmlengths[1] = rb->id3->samples;
         vf.serialnos[0] = vf.current_serialno;
         vf.callbacks.seek_func = seek_handler;
         vf.seekable = 1;
         vf.end = rb->id3->filesize;
         vf.ready_state = OPENED;
         vf.links = 1;
    } else {
         //rb->logf("ov_open: %d", error);
         error = CODEC_ERROR;
         goto done;
    }

    if (rb->id3->offset) {
        rb->advance_buffer(rb->id3->offset);
        ov_raw_seek(&vf, rb->id3->offset);
        rb->set_elapsed(ov_time_tell(&vf));
        rb->set_offset(ov_raw_tell(&vf));
    }

    eof = 0;
    while (!eof) {
        rb->yield();
        if (rb->stop_codec || rb->new_track)
            break;

        if (rb->seek_time) {
            if (ov_time_seek(&vf, rb->seek_time - 1)) {
                //rb->logf("ov_time_seek failed");
            }
            rb->seek_complete();
        }

        /* Read host-endian signed 24-bit PCM samples */
        n = ov_read_fixed(&vf, &pcm, 1024, &current_section);

        /* Change DSP and buffer settings for this bitstream */
        if (current_section != previous_section) {
            if (!vorbis_set_codec_parameters(&vf)) {
                error = CODEC_ERROR;
                goto done;
            } else {
                previous_section = current_section;
            }
        }

        if (n == 0) {
            eof = 1;
        } else if (n < 0) {
            DEBUGF("Error decoding frame\n");
        } else {
            while (!rb->pcmbuf_insert_split(pcm[0], pcm[1], 
                   n*sizeof(ogg_int32_t))) {
                rb->sleep(1);
            }
            rb->set_offset(ov_raw_tell(&vf));
            rb->set_elapsed(ov_time_tell(&vf));
        }
    }
    error = CODEC_OK;
    
done:
    if (rb->request_next_track()) {
        /* Clean things up for the next track */
        vf.dataoffsets = NULL;
        vf.offsets = NULL;
        vf.serialnos = NULL;
        vf.pcmlengths = NULL;
        ov_clear(&vf);
        previous_section = -1;
        goto next_track;
    }
        
exit:
    return error;
}