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"
|
2012-02-11 10:04:02 +01:00
|
|
|
#include "vorbis_comments.h"
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "_ogg_common.h"
|
2009-11-10 19:01:38 +01:00
|
|
|
#include "audio_check.h"
|
2009-02-25 16:44:06 +01:00
|
|
|
#include "uri.h"
|
2012-02-11 19:12:02 +01:00
|
|
|
#include "tag_handler.h"
|
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-04-03 01:07:52 +02:00
|
|
|
#define OGG_CHUNK_SIZE 4096
|
2009-02-17 08:51:34 +01:00
|
|
|
|
2009-01-05 12:40:57 +01:00
|
|
|
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
2004-03-09 22:42:08 +01:00
|
|
|
#define OGG_DECODE_USE_BIGENDIAN 1
|
|
|
|
#else
|
|
|
|
#define OGG_DECODE_USE_BIGENDIAN 0
|
|
|
|
#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
|
|
|
{
|
2010-05-30 23:57:58 +02:00
|
|
|
struct vorbis_input_stream *vis = data;
|
2008-03-26 11:37:36 +01:00
|
|
|
size_t ret;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2010-05-30 23:57:58 +02:00
|
|
|
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
|
|
|
{
|
2010-05-30 23:57:58 +02:00
|
|
|
struct vorbis_input_stream *vis = 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
|
|
|
{
|
2010-05-30 23:57:58 +02:00
|
|
|
const struct vorbis_input_stream *vis = 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 = {
|
|
|
|
.read_func = ogg_read_cb,
|
|
|
|
.seek_func = ogg_seek_cb,
|
|
|
|
.close_func = ogg_close_cb,
|
|
|
|
.tell_func = ogg_tell_cb,
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
vis->seekable = input_stream->seekable &&
|
|
|
|
(input_stream->uri == NULL ||
|
|
|
|
!uri_has_scheme(input_stream->uri));
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *tag;
|
2004-06-01 13:18:25 +02:00
|
|
|
|
2009-01-14 23:09:02 +01:00
|
|
|
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
|
|
|
|
2008-11-02 17:02:28 +01:00
|
|
|
decoder_tag(decoder, is, tag);
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_free(tag);
|
2004-06-01 13:18:25 +02:00
|
|
|
}
|
|
|
|
|
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-02-24 00:41:20 +01:00
|
|
|
OggVorbis_File vf;
|
2010-05-30 23:57:58 +02:00
|
|
|
struct vorbis_input_stream vis;
|
2008-09-07 19:19:55 +02:00
|
|
|
struct audio_format audio_format;
|
2009-11-11 21:34:55 +01:00
|
|
|
float total_time;
|
2004-06-01 12:37:13 +02:00
|
|
|
int current_section;
|
2004-06-05 07:03:00 +02:00
|
|
|
int prev_section = -1;
|
2004-06-01 12:37:13 +02:00
|
|
|
long ret;
|
|
|
|
char chunk[OGG_CHUNK_SIZE];
|
|
|
|
long bitRate = 0;
|
|
|
|
long test;
|
2009-11-11 21:34:55 +01:00
|
|
|
const vorbis_info *vi;
|
2008-11-11 21:21:54 +01:00
|
|
|
enum decoder_command cmd = DECODE_COMMAND_NONE;
|
2004-05-20 05:44:33 +02:00
|
|
|
|
2009-01-14 23:09:02 +01:00
|
|
|
if (ogg_stream_type_detect(input_stream) != VORBIS)
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2008-11-10 15:07:01 +01:00
|
|
|
|
|
|
|
/* rewind the stream, because ogg_stream_type_detect() has
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2009-11-11 21:34:55 +01:00
|
|
|
vi = ov_info(&vf, -1);
|
|
|
|
if (vi == NULL) {
|
|
|
|
g_warning("ov_info() has failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
if (!audio_format_init_checked(&audio_format, vi->rate,
|
|
|
|
SAMPLE_FORMAT_S16,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_time = ov_time_total(&vf, -1);
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
ret = ov_read(&vf, chunk, sizeof(chunk),
|
2006-07-20 18:02:40 +02:00
|
|
|
OGG_DECODE_USE_BIGENDIAN, 2, 1, ¤t_section);
|
2008-11-20 12:45:17 +01:00
|
|
|
if (ret == OV_HOLE) /* bad packet */
|
|
|
|
ret = 0;
|
|
|
|
else if (ret <= 0)
|
|
|
|
/* 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
|
|
|
char **comments;
|
2008-11-11 16:19:00 +01:00
|
|
|
|
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
|
|
|
|
2004-06-05 18:01:44 +02:00
|
|
|
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
|
|
|
|
2008-11-11 21:15:01 +01:00
|
|
|
if ((test = ov_bitrate_instant(&vf)) > 0)
|
|
|
|
bitRate = test / 1000;
|
2004-06-01 12:37:13 +02:00
|
|
|
|
2009-01-14 23:09:02 +01:00
|
|
|
cmd = decoder_data(decoder, input_stream,
|
2008-11-11 21:21:54 +01:00
|
|
|
chunk, ret,
|
2010-01-03 22:44:23 +01:00
|
|
|
bitRate);
|
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 = {
|
2009-02-16 19:31:06 +01:00
|
|
|
.name = "vorbis",
|
2009-01-14 23:09:02 +01:00
|
|
|
.stream_decode = vorbis_stream_decode,
|
2012-02-11 19:12:02 +01:00
|
|
|
.scan_stream = vorbis_scan_stream,
|
2009-01-14 23:09:02 +01:00
|
|
|
.suffixes = vorbis_suffixes,
|
|
|
|
.mime_types = vorbis_mime_types
|
2004-05-31 03:21:17 +02:00
|
|
|
};
|