2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-01-08 21:37:02 +01:00
|
|
|
#include "config.h"
|
2013-01-07 22:04:58 +01:00
|
|
|
#include "VorbisDecoderPlugin.h"
|
|
|
|
#include "VorbisComments.hxx"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-01-07 22:12:09 +01:00
|
|
|
#include "OggCodec.hxx"
|
2013-04-08 23:30:21 +02:00
|
|
|
#include "util/UriUtil.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2013-07-29 07:32:36 +02:00
|
|
|
#include "TagHandler.hxx"
|
2005-08-25 10:07:28 +02:00
|
|
|
|
|
|
|
#ifndef HAVE_TREMOR
|
2009-10-18 17:09:32 +02:00
|
|
|
#define OV_EXCLUDE_STATIC_CALLBACKS
|
2004-02-24 00:41:20 +01:00
|
|
|
#include <vorbis/vorbisfile.h>
|
2005-08-25 10:07:28 +02:00
|
|
|
#else
|
|
|
|
#include <tremor/ivorbisfile.h>
|
|
|
|
/* Macros to make Tremor's API look like libogg. Tremor always
|
|
|
|
returns host-byte-order 16-bit signed data, and uses integer
|
2010-05-31 22:11:59 +02:00
|
|
|
milliseconds where libogg uses double seconds.
|
2005-08-25 10:07:28 +02:00
|
|
|
*/
|
|
|
|
#define ov_read(VF, BUFFER, LENGTH, BIGENDIANP, WORD, SGNED, BITSTREAM) \
|
|
|
|
ov_read(VF, BUFFER, LENGTH, BITSTREAM)
|
|
|
|
#define ov_time_total(VF, I) ((double)ov_time_total(VF, I)/1000)
|
|
|
|
#define ov_time_tell(VF) ((double)ov_time_tell(VF)/1000)
|
|
|
|
#define ov_time_seek_page(VF, S) (ov_time_seek_page(VF, (S)*1000))
|
2006-07-20 20:53:56 +02:00
|
|
|
#endif /* HAVE_TREMOR */
|
2005-08-25 10:07:28 +02:00
|
|
|
|
2008-11-11 21:33:01 +01:00
|
|
|
#include <glib.h>
|
2009-01-14 23:09:31 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2008-11-11 21:33:01 +01:00
|
|
|
#include <errno.h>
|
2008-11-24 10:33:08 +01:00
|
|
|
#include <unistd.h>
|
2008-11-11 21:33:01 +01:00
|
|
|
|
2009-02-17 08:51:34 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "vorbis"
|
|
|
|
|
2009-01-05 12:40:57 +01:00
|
|
|
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
2012-09-25 21:23:13 +02:00
|
|
|
#define VORBIS_BIG_ENDIAN true
|
2004-03-09 22:42:08 +01:00
|
|
|
#else
|
2012-09-25 21:23:13 +02:00
|
|
|
#define VORBIS_BIG_ENDIAN false
|
2004-03-09 22:42:08 +01:00
|
|
|
#endif
|
|
|
|
|
2010-05-30 23:57:58 +02:00
|
|
|
struct vorbis_input_stream {
|
2008-08-26 08:27:07 +02:00
|
|
|
struct decoder *decoder;
|
2009-01-17 11:43:13 +01:00
|
|
|
|
2009-01-14 23:09:02 +01:00
|
|
|
struct input_stream *input_stream;
|
2009-01-17 11:43:13 +01:00
|
|
|
bool seekable;
|
2009-11-11 21:04:32 +01:00
|
|
|
};
|
2004-05-20 05:44:33 +02:00
|
|
|
|
2010-05-30 23:57:58 +02:00
|
|
|
static size_t ogg_read_cb(void *ptr, size_t size, size_t nmemb, void *data)
|
2004-05-04 21:49:29 +02:00
|
|
|
{
|
2013-01-07 22:04:58 +01:00
|
|
|
struct vorbis_input_stream *vis = (struct vorbis_input_stream *)data;
|
2012-09-25 21:11:38 +02:00
|
|
|
size_t ret = decoder_read(vis->decoder, vis->input_stream,
|
|
|
|
ptr, size * nmemb);
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
errno = 0;
|
2004-05-04 21:49:29 +02:00
|
|
|
|
2008-08-26 08:27:14 +02:00
|
|
|
return ret / size;
|
2004-05-04 21:49:29 +02:00
|
|
|
}
|
|
|
|
|
2010-05-30 23:57:58 +02:00
|
|
|
static int ogg_seek_cb(void *data, ogg_int64_t offset, int whence)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-07 22:04:58 +01:00
|
|
|
struct vorbis_input_stream *vis = (struct vorbis_input_stream *)data;
|
2009-01-17 11:43:13 +01:00
|
|
|
|
2010-05-30 23:57:58 +02:00
|
|
|
return vis->seekable &&
|
2010-05-31 22:11:59 +02:00
|
|
|
(!vis->decoder || decoder_get_command(vis->decoder) != DECODE_COMMAND_STOP) &&
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_lock_seek(vis->input_stream, offset, whence, NULL)
|
2009-01-17 11:43:13 +01:00
|
|
|
? 0 : -1;
|
2004-05-04 21:49:29 +02:00
|
|
|
}
|
|
|
|
|
2008-01-01 11:09:56 +01:00
|
|
|
/* TODO: check Ogg libraries API and see if we can just not have this func */
|
2010-05-30 23:57:58 +02:00
|
|
|
static int ogg_close_cb(G_GNUC_UNUSED void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-01-01 11:09:56 +01:00
|
|
|
return 0;
|
2004-05-04 21:49:29 +02:00
|
|
|
}
|
|
|
|
|
2010-05-30 23:57:58 +02:00
|
|
|
static long ogg_tell_cb(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-07 22:04:58 +01:00
|
|
|
struct vorbis_input_stream *vis = (struct vorbis_input_stream *)data;
|
2004-05-20 05:44:33 +02:00
|
|
|
|
2010-05-30 23:57:58 +02:00
|
|
|
return (long)vis->input_stream->offset;
|
2004-05-04 21:49:29 +02:00
|
|
|
}
|
|
|
|
|
2010-05-30 23:52:05 +02:00
|
|
|
static const ov_callbacks vorbis_is_callbacks = {
|
2013-01-07 22:04:58 +01:00
|
|
|
ogg_read_cb,
|
|
|
|
ogg_seek_cb,
|
|
|
|
ogg_close_cb,
|
|
|
|
ogg_tell_cb,
|
2010-05-30 23:52:05 +02:00
|
|
|
};
|
|
|
|
|
2009-11-11 21:12:10 +01:00
|
|
|
static const char *
|
|
|
|
vorbis_strerror(int code)
|
|
|
|
{
|
|
|
|
switch (code) {
|
|
|
|
case OV_EREAD:
|
|
|
|
return "read error";
|
|
|
|
|
|
|
|
case OV_ENOTVORBIS:
|
|
|
|
return "not vorbis stream";
|
|
|
|
|
|
|
|
case OV_EVERSION:
|
|
|
|
return "vorbis version mismatch";
|
|
|
|
|
|
|
|
case OV_EBADHEADER:
|
|
|
|
return "invalid vorbis header";
|
|
|
|
|
|
|
|
case OV_EFAULT:
|
|
|
|
return "internal logic error";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-30 23:50:17 +02:00
|
|
|
static bool
|
|
|
|
vorbis_is_open(struct vorbis_input_stream *vis, OggVorbis_File *vf,
|
|
|
|
struct decoder *decoder, struct input_stream *input_stream)
|
|
|
|
{
|
|
|
|
vis->decoder = decoder;
|
|
|
|
vis->input_stream = input_stream;
|
2013-01-07 23:23:58 +01:00
|
|
|
vis->seekable = input_stream_cheap_seeking(input_stream);
|
2010-05-30 23:50:17 +02:00
|
|
|
|
|
|
|
int ret = ov_open_callbacks(vis, vf, NULL, 0, vorbis_is_callbacks);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (decoder == NULL ||
|
|
|
|
decoder_get_command(decoder) == DECODE_COMMAND_NONE)
|
|
|
|
g_warning("Failed to open Ogg Vorbis stream: %s",
|
|
|
|
vorbis_strerror(ret));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-14 23:09:02 +01:00
|
|
|
static void
|
|
|
|
vorbis_send_comments(struct decoder *decoder, struct input_stream *is,
|
|
|
|
char **comments)
|
2004-06-01 13:18:25 +02:00
|
|
|
{
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag = vorbis_comments_to_tag(comments);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!tag)
|
|
|
|
return;
|
2004-06-01 13:18:25 +02:00
|
|
|
|
2013-07-31 00:34:22 +02:00
|
|
|
decoder_tag(decoder, is, std::move(*tag));
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
2004-06-01 13:18:25 +02:00
|
|
|
}
|
|
|
|
|
2012-09-25 21:08:32 +02:00
|
|
|
#ifndef HAVE_TREMOR
|
|
|
|
static void
|
|
|
|
vorbis_interleave(float *dest, const float *const*src,
|
|
|
|
unsigned nframes, unsigned channels)
|
|
|
|
{
|
|
|
|
for (const float *const*src_end = src + channels;
|
|
|
|
src != src_end; ++src, ++dest) {
|
|
|
|
float *d = dest;
|
|
|
|
for (const float *s = *src, *s_end = s + nframes;
|
|
|
|
s != s_end; ++s, d += channels)
|
|
|
|
*d = *s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-16 07:52:46 +01:00
|
|
|
/* public */
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2009-01-14 23:09:02 +01:00
|
|
|
vorbis_stream_decode(struct decoder *decoder,
|
|
|
|
struct input_stream *input_stream)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2009-11-10 19:01:38 +01:00
|
|
|
GError *error = NULL;
|
2004-05-20 05:44:33 +02:00
|
|
|
|
2012-09-04 13:05:12 +02:00
|
|
|
if (ogg_codec_detect(decoder, input_stream) != OGG_CODEC_VORBIS)
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2008-11-10 15:07:01 +01:00
|
|
|
|
2012-09-04 13:05:12 +02:00
|
|
|
/* rewind the stream, because ogg_codec_detect() has
|
2008-11-10 15:07:01 +01:00
|
|
|
moved it */
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_lock_seek(input_stream, 0, SEEK_SET, NULL);
|
2008-11-10 15:07:01 +01:00
|
|
|
|
2012-09-25 21:11:38 +02:00
|
|
|
struct vorbis_input_stream vis;
|
|
|
|
OggVorbis_File vf;
|
2010-05-30 23:50:17 +02:00
|
|
|
if (!vorbis_is_open(&vis, &vf, decoder, input_stream))
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2004-06-01 12:37:13 +02:00
|
|
|
|
2012-09-25 21:11:38 +02:00
|
|
|
const vorbis_info *vi = ov_info(&vf, -1);
|
2009-11-11 21:34:55 +01:00
|
|
|
if (vi == NULL) {
|
|
|
|
g_warning("ov_info() has failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-25 21:11:38 +02:00
|
|
|
struct audio_format audio_format;
|
2009-11-10 17:11:34 +01:00
|
|
|
if (!audio_format_init_checked(&audio_format, vi->rate,
|
2012-09-25 21:08:32 +02:00
|
|
|
#ifdef HAVE_TREMOR
|
2009-11-10 17:11:34 +01:00
|
|
|
SAMPLE_FORMAT_S16,
|
2012-09-25 21:08:32 +02:00
|
|
|
#else
|
|
|
|
SAMPLE_FORMAT_FLOAT,
|
|
|
|
#endif
|
2009-11-10 19:01:38 +01:00
|
|
|
vi->channels, &error)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
2009-11-11 21:34:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-25 21:11:38 +02:00
|
|
|
float total_time = ov_time_total(&vf, -1);
|
2009-11-11 21:34:55 +01:00
|
|
|
if (total_time < 0)
|
|
|
|
total_time = 0;
|
|
|
|
|
2010-05-30 23:57:58 +02:00
|
|
|
decoder_initialized(decoder, &audio_format, vis.seekable, total_time);
|
2009-11-11 21:34:55 +01:00
|
|
|
|
2012-09-25 21:20:01 +02:00
|
|
|
enum decoder_command cmd = decoder_get_command(decoder);
|
2012-09-25 21:08:32 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_TREMOR
|
2012-09-25 21:23:13 +02:00
|
|
|
char buffer[4096];
|
2012-09-25 21:08:32 +02:00
|
|
|
#else
|
|
|
|
float buffer[2048];
|
|
|
|
const int frames_per_buffer =
|
|
|
|
G_N_ELEMENTS(buffer) / audio_format.channels;
|
|
|
|
const unsigned frame_size = sizeof(buffer[0]) * audio_format.channels;
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 21:11:38 +02:00
|
|
|
int prev_section = -1;
|
2012-09-25 21:23:13 +02:00
|
|
|
unsigned kbit_rate = 0;
|
2012-09-25 21:11:38 +02:00
|
|
|
|
2008-11-11 21:21:54 +01:00
|
|
|
do {
|
|
|
|
if (cmd == DECODE_COMMAND_SEEK) {
|
2008-08-26 08:27:07 +02:00
|
|
|
double seek_where = decoder_seek_where(decoder);
|
|
|
|
if (0 == ov_time_seek_page(&vf, seek_where)) {
|
|
|
|
decoder_command_finished(decoder);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_seek_error(decoder);
|
2004-06-01 12:37:13 +02:00
|
|
|
}
|
2008-11-11 21:15:01 +01:00
|
|
|
|
2012-09-25 21:11:38 +02:00
|
|
|
int current_section;
|
2012-09-25 21:08:32 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_TREMOR
|
2012-09-25 21:23:13 +02:00
|
|
|
long nbytes = ov_read(&vf, buffer, sizeof(buffer),
|
|
|
|
VORBIS_BIG_ENDIAN, 2, 1,
|
|
|
|
¤t_section);
|
2012-09-25 21:08:32 +02:00
|
|
|
#else
|
|
|
|
float **per_channel;
|
|
|
|
long nframes = ov_read_float(&vf, &per_channel,
|
|
|
|
frames_per_buffer,
|
|
|
|
¤t_section);
|
|
|
|
long nbytes = nframes;
|
|
|
|
if (nframes > 0) {
|
|
|
|
vorbis_interleave(buffer,
|
|
|
|
(const float*const*)per_channel,
|
|
|
|
nframes, audio_format.channels);
|
|
|
|
nbytes *= frame_size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 21:23:13 +02:00
|
|
|
if (nbytes == OV_HOLE) /* bad packet */
|
|
|
|
nbytes = 0;
|
|
|
|
else if (nbytes <= 0)
|
2008-11-20 12:45:17 +01:00
|
|
|
/* break on EOF or other error */
|
|
|
|
break;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (current_section != prev_section) {
|
2009-11-11 21:34:55 +01:00
|
|
|
vi = ov_info(&vf, -1);
|
|
|
|
if (vi == NULL) {
|
|
|
|
g_warning("ov_info() has failed");
|
2008-11-21 20:27:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-12 09:16:34 +01:00
|
|
|
if (vi->rate != (long)audio_format.sample_rate ||
|
|
|
|
vi->channels != (int)audio_format.channels) {
|
2009-11-11 21:34:55 +01:00
|
|
|
/* we don't support audio format
|
|
|
|
change yet */
|
|
|
|
g_warning("audio format change, stopping here");
|
|
|
|
break;
|
2004-06-06 18:42:14 +02:00
|
|
|
}
|
2009-11-11 21:34:55 +01:00
|
|
|
|
2012-09-25 21:11:38 +02:00
|
|
|
char **comments = ov_comment(&vf, -1)->user_comments;
|
2009-01-14 23:09:02 +01:00
|
|
|
vorbis_send_comments(decoder, input_stream, comments);
|
2010-02-14 20:36:31 +01:00
|
|
|
|
|
|
|
struct replay_gain_info rgi;
|
|
|
|
if (vorbis_comments_to_replay_gain(&rgi, comments))
|
|
|
|
decoder_replay_gain(decoder, &rgi);
|
2004-06-05 07:03:00 +02:00
|
|
|
|
2009-11-11 21:34:55 +01:00
|
|
|
prev_section = current_section;
|
|
|
|
}
|
2004-06-05 07:03:00 +02:00
|
|
|
|
2012-09-25 21:11:38 +02:00
|
|
|
long test = ov_bitrate_instant(&vf);
|
|
|
|
if (test > 0)
|
2012-09-25 21:23:13 +02:00
|
|
|
kbit_rate = test / 1000;
|
2004-06-01 12:37:13 +02:00
|
|
|
|
2009-01-14 23:09:02 +01:00
|
|
|
cmd = decoder_data(decoder, input_stream,
|
2012-09-25 21:23:13 +02:00
|
|
|
buffer, nbytes,
|
|
|
|
kbit_rate);
|
2008-11-11 21:21:54 +01:00
|
|
|
} while (cmd != DECODE_COMMAND_STOP);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-06-01 12:37:13 +02:00
|
|
|
ov_clear(&vf);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
vorbis_scan_stream(struct input_stream *is,
|
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2010-05-31 00:02:57 +02:00
|
|
|
struct vorbis_input_stream vis;
|
2004-05-31 03:21:17 +02:00
|
|
|
OggVorbis_File vf;
|
|
|
|
|
2010-05-31 00:02:57 +02:00
|
|
|
if (!vorbis_is_open(&vis, &vf, NULL, is))
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx,
|
|
|
|
(int)(ov_time_total(&vf, -1) + 0.5));
|
2004-05-31 05:02:17 +02:00
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
vorbis_comments_scan(ov_comment(&vf, -1)->user_comments,
|
|
|
|
handler, handler_ctx);
|
2004-05-31 03:21:17 +02:00
|
|
|
|
|
|
|
ov_clear(&vf);
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2004-05-31 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2009-01-14 23:09:02 +01:00
|
|
|
static const char *const vorbis_suffixes[] = {
|
|
|
|
"ogg", "oga", NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const vorbis_mime_types[] = {
|
2008-11-01 14:55:23 +01:00
|
|
|
"application/ogg",
|
|
|
|
"application/x-ogg",
|
2009-12-29 22:33:46 +01:00
|
|
|
"audio/ogg",
|
|
|
|
"audio/vorbis",
|
|
|
|
"audio/vorbis+ogg",
|
|
|
|
"audio/x-ogg",
|
|
|
|
"audio/x-vorbis",
|
|
|
|
"audio/x-vorbis+ogg",
|
2008-11-01 14:55:23 +01:00
|
|
|
NULL
|
|
|
|
};
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2009-01-14 23:09:02 +01:00
|
|
|
const struct decoder_plugin vorbis_decoder_plugin = {
|
2013-01-07 22:04:58 +01:00
|
|
|
"vorbis",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
vorbis_stream_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
vorbis_scan_stream,
|
|
|
|
nullptr,
|
|
|
|
vorbis_suffixes,
|
|
|
|
vorbis_mime_types
|
2004-05-31 03:21:17 +02:00
|
|
|
};
|