summaryrefslogtreecommitdiff
path: root/plugins/vorbis/vorbis.c
blob: 32d99660f33697b78047dccd68b48ea3f39e42c9 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009  Alexey Yakovenko

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
#include "../../deadbeef.h"

#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))

//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)

static DB_decoder_t plugin;
static DB_functions_t *deadbeef;

static DB_FILE *file;
static OggVorbis_File vorbis_file;
static vorbis_info *vi;
static int cur_bit_stream;
static int startsample;
static int endsample;
static int currentsample;

static void
cvorbis_free (void);

static size_t
cvorbis_fread (void *ptr, size_t size, size_t nmemb, void *datasource) {
    return deadbeef->fread (ptr, size, nmemb, datasource);
}

static int
cvorbis_fseek (void *datasource, ogg_int64_t offset, int whence) {
    DB_FILE *f = (DB_FILE *)datasource;
    return deadbeef->fseek (datasource, offset, whence);
}

static int
cvorbis_fclose (void *datasource) {
    deadbeef->fclose (datasource);
    return 0;
}

static long
cvorbis_ftell (void *datasource) {
    return deadbeef->ftell (datasource);
}

static int
cvorbis_init (DB_playItem_t *it) {
    file = NULL;
    vi = NULL;
    cur_bit_stream = -1;

    file = deadbeef->fopen (it->fname);
    if (!file) {
        return -1;
    }
    ov_callbacks ovcb = {
        .read_func = cvorbis_fread,
        .seek_func = cvorbis_fseek,
        .close_func = cvorbis_fclose,
        .tell_func = cvorbis_ftell
    };

    memset (&plugin.info, 0, sizeof (plugin.info));

    ov_open_callbacks (file, &vorbis_file, NULL, 0, ovcb);
    vi = ov_info (&vorbis_file, -1);
    if (!vi) { // not a vorbis stream
        cvorbis_free ();
        return -1;
    }
    plugin.info.bps = 16;
    //plugin.info.dataSize = ov_pcm_total (&vorbis_file, -1) * vi->channels * 2;
    plugin.info.channels = vi->channels;
    plugin.info.samplerate = vi->rate;
    plugin.info.readpos = 0;
    currentsample = 0;
    if (it->endsample > 0) {
        startsample = it->startsample;
        endsample = it->endsample;
        plugin.seek_sample (0);
    }
    else {
        startsample = 0;
        endsample = ov_pcm_total (&vorbis_file, -1)-1;
    }
    return 0;
}

static void
cvorbis_free (void) {
    if (file) {
        ov_clear (&vorbis_file);
        //fclose (file); //-- ov_clear closes it
        file = NULL;
        vi = NULL;
    }
}

static int
cvorbis_read (char *bytes, int size) {
    if (currentsample + size / (2 * plugin.info.channels) > endsample) {
        size = (endsample - currentsample + 1) * 2 * plugin.info.channels;
        trace ("size truncated to %d bytes, cursample=%d, endsample=%d, totalsamples=%d\n", size, currentsample, endsample, ov_pcm_total (&vorbis_file, -1));
        if (size <= 0) {
            return 0;
        }
    }
    int initsize = size;
    for (;;)
    {
        // read ogg
        int endianess = 0;
#if WORDS_BIGENDIAN
        endianess = 1;
#endif
        long ret=ov_read (&vorbis_file, bytes, size, endianess, 2, 1, &cur_bit_stream);
        if (ret <= 0)
        {
            // error or eof
            break;
        }
        else if (ret < size)
        {
            currentsample += ret / (vi->channels * 2);
            size -= ret;
            bytes += ret;
        }
        else {
            currentsample += ret / (vi->channels * 2);
            size = 0;
            break;
        }
    }
    plugin.info.readpos = (float)(ov_pcm_tell(&vorbis_file)-startsample)/vi->rate;
    return initsize - size;
}

static int
cvorbis_seek_sample (int sample) {
    if (!file) {
        return -1;
    }
    sample += startsample;
    int res = ov_pcm_seek (&vorbis_file, sample);
    if (res != 0 && res != OV_ENOSEEK)
        return -1;
    int tell = ov_pcm_tell (&vorbis_file);
    if (tell != sample) {
        fprintf (stderr, "oggvorbis: failed to do sample-accurate seek (%d->%d)\n", sample, tell);
    }
    currentsample = sample;
    plugin.info.readpos = (float)(ov_pcm_tell(&vorbis_file) - startsample)/vi->rate;
    return 0;
}

static int
cvorbis_seek (float time) {
    return cvorbis_seek_sample (time * vi->rate);
}

static DB_playItem_t *
cvorbis_insert (DB_playItem_t *after, const char *fname) {
    // check for validity
    DB_FILE *fp = deadbeef->fopen (fname);
    if (!fp) {
        fprintf (stderr, "vorbis: failed to fopen %s\n", fname);
        return NULL;
    }
    ov_callbacks ovcb = {
        .read_func = cvorbis_fread,
        .seek_func = cvorbis_fseek,
        .close_func = cvorbis_fclose,
        .tell_func = cvorbis_ftell
    };
    OggVorbis_File vorbis_file;
    vorbis_info *vi;
    ov_open_callbacks (fp, &vorbis_file, NULL, 0, ovcb);
    vi = ov_info (&vorbis_file, -1);
    if (!vi) { // not a vorbis stream
        fprintf (stderr, "vorbis: failed to ov_open %s\n", fname);
        return NULL;
    }
    float duration = ov_time_total (&vorbis_file, -1);
    int totalsamples = ov_pcm_total (&vorbis_file, -1);
    DB_playItem_t *cue_after = deadbeef->pl_insert_cue (after, fname, &plugin, "OggVorbis", totalsamples, vi->rate);
    if (cue_after) {
        ov_clear (&vorbis_file);
        return cue_after;
    }

    DB_playItem_t *it = deadbeef->pl_item_alloc ();
    it->decoder = &plugin;
    it->fname = strdup (fname);
    it->filetype = "OggVorbis";
    it->duration = duration;

    // metainfo
    int title_added = 0;
    vorbis_comment *vc = ov_comment (&vorbis_file, -1);
    if (vc) {
        deadbeef->pl_add_meta (it, "vendor", vc->vendor);
        for (int i = 0; i < vc->comments; i++) {
            if (!strncasecmp (vc->user_comments[i], "artist=", 7)) {
                deadbeef->pl_add_meta (it, "artist", vc->user_comments[i] + 7);
            }
            else if (!strncasecmp (vc->user_comments[i], "album=", 6)) {
                deadbeef->pl_add_meta (it, "album", vc->user_comments[i] + 6);
            }
            else if (!strncasecmp (vc->user_comments[i], "title=", 6)) {
                deadbeef->pl_add_meta (it, "title", vc->user_comments[i] + 6);
                title_added = 1;
            }
            else if (!strncasecmp (vc->user_comments[i], "date=", 5)) {
                deadbeef->pl_add_meta (it, "date", vc->user_comments[i] + 5);
            }
            else if (!strncasecmp (vc->user_comments[i], "replaygain_album_gain=", 22)) {
                it->replaygain_album_gain = atof (vc->user_comments[i] + 22);
            }
            else if (!strncasecmp (vc->user_comments[i], "replaygain_album_peak=", 22)) {
                it->replaygain_album_peak = atof (vc->user_comments[i] + 22);
            }
            else if (!strncasecmp (vc->user_comments[i], "replaygain_track_gain=", 22)) {
                it->replaygain_track_gain = atof (vc->user_comments[i] + 22);
            }
            else if (!strncasecmp (vc->user_comments[i], "replaygain_track_peak=", 22)) {
                it->replaygain_track_peak = atof (vc->user_comments[i] + 22);
            }
        }
    }
    if (!title_added) {
        deadbeef->pl_add_meta (it, "title", NULL);
    }
    ov_clear (&vorbis_file);
    after = deadbeef->pl_insert_item (after, it);
    return after;
}

static const char * exts[] = { "ogg", NULL };
static const char *filetypes[] = { "OggVorbis", NULL };

// define plugin interface
static DB_decoder_t plugin = {
    DB_PLUGIN_SET_API_VERSION
    .plugin.version_major = 0,
    .plugin.version_minor = 1,
    .plugin.type = DB_PLUGIN_DECODER,
    .plugin.name = "OggVorbis decoder",
    .plugin.author = "Alexey Yakovenko",
    .plugin.email = "waker@users.sourceforge.net",
    .plugin.website = "http://deadbeef.sf.net",
    .init = cvorbis_init,
    .free = cvorbis_free,
    .read_int16 = cvorbis_read,
    // vorbisfile can't output float32
//    .read_float32 = cvorbis_read_float32,
    .seek = cvorbis_seek,
    .seek_sample = cvorbis_seek_sample,
    .insert = cvorbis_insert,
    .exts = exts,
    .id = "stdogg",
    .filetypes = filetypes
};

DB_plugin_t *
vorbis_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}