aboutsummaryrefslogtreecommitdiffstats
path: root/src/decoder/vorbis_decoder_plugin.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/decoder/vorbis_decoder_plugin.c (renamed from src/decoder/vorbis_plugin.c)191
1 files changed, 92 insertions, 99 deletions
diff --git a/src/decoder/vorbis_plugin.c b/src/decoder/vorbis_decoder_plugin.c
index 0ff898647..25e91c142 100644
--- a/src/decoder/vorbis_plugin.c
+++ b/src/decoder/vorbis_decoder_plugin.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2009 The Music Player Daemon Project
+ * Copyright (C) 2003-2010 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@@ -17,13 +17,13 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-/* TODO 'ogg' should probably be replaced with 'oggvorbis' in all instances */
-
-#include "_ogg_common.h"
#include "config.h"
+#include "_ogg_common.h"
+#include "audio_check.h"
#include "uri.h"
#ifndef HAVE_TREMOR
+#define OV_EXCLUDE_STATIC_CALLBACKS
#include <vorbis/vorbisfile.h>
#else
#include <tremor/ivorbisfile.h>
@@ -55,17 +55,17 @@
#define OGG_DECODE_USE_BIGENDIAN 0
#endif
-typedef struct _OggCallbackData {
+struct vorbis_decoder_data {
struct decoder *decoder;
struct input_stream *input_stream;
bool seekable;
-} OggCallbackData;
+};
static size_t ogg_read_cb(void *ptr, size_t size, size_t nmemb, void *vdata)
{
+ struct vorbis_decoder_data *data = (struct vorbis_decoder_data *)vdata;
size_t ret;
- OggCallbackData *data = (OggCallbackData *) vdata;
ret = decoder_read(data->decoder, data->input_stream, ptr, size * nmemb);
@@ -76,11 +76,11 @@ static size_t ogg_read_cb(void *ptr, size_t size, size_t nmemb, void *vdata)
static int ogg_seek_cb(void *vdata, ogg_int64_t offset, int whence)
{
- const OggCallbackData *data = (const OggCallbackData *) vdata;
+ struct vorbis_decoder_data *data = (struct vorbis_decoder_data *)vdata;
return data->seekable &&
decoder_get_command(data->decoder) != DECODE_COMMAND_STOP &&
- input_stream_seek(data->input_stream, offset, whence)
+ input_stream_seek(data->input_stream, offset, whence, NULL)
? 0 : -1;
}
@@ -92,12 +92,37 @@ static int ogg_close_cb(G_GNUC_UNUSED void *vdata)
static long ogg_tell_cb(void *vdata)
{
- const OggCallbackData *data = (const OggCallbackData *) vdata;
+ const struct vorbis_decoder_data *data =
+ (const struct vorbis_decoder_data *)vdata;
return (long)data->input_stream->offset;
}
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";
+ }
+}
+
+static const char *
vorbis_comment_value(const char *comment, const char *needle)
{
size_t len = strlen(needle);
@@ -109,14 +134,13 @@ vorbis_comment_value(const char *comment, const char *needle)
return NULL;
}
-static struct replay_gain_info *
-vorbis_comments_to_replay_gain(char **comments)
+static bool
+vorbis_comments_to_replay_gain(struct replay_gain_info *rgi, char **comments)
{
- struct replay_gain_info *rgi;
const char *temp;
bool found = false;
- rgi = replay_gain_info_new();
+ replay_gain_info_init(rgi);
while (*comments) {
if ((temp =
@@ -140,12 +164,7 @@ vorbis_comments_to_replay_gain(char **comments)
comments++;
}
- if (!found) {
- replay_gain_info_free(rgi);
- rgi = NULL;
- }
-
- return rgi;
+ return found;
}
static const char *VORBIS_COMMENT_TRACK_KEY = "tracknumber";
@@ -176,11 +195,11 @@ vorbis_parse_comment(struct tag *tag, const char *comment)
assert(tag != NULL);
if (vorbis_copy_comment(tag, comment, VORBIS_COMMENT_TRACK_KEY,
- TAG_ITEM_TRACK) ||
+ TAG_TRACK) ||
vorbis_copy_comment(tag, comment, VORBIS_COMMENT_DISC_KEY,
- TAG_ITEM_DISC) ||
+ TAG_DISC) ||
vorbis_copy_comment(tag, comment, "album artist",
- TAG_ITEM_ALBUM_ARTIST))
+ TAG_ALBUM_ARTIST))
return;
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
@@ -220,19 +239,10 @@ vorbis_send_comments(struct decoder *decoder, struct input_stream *is,
}
static bool
-oggvorbis_seekable(struct decoder *decoder)
+oggvorbis_seekable(const struct input_stream *is)
{
- char *uri;
- bool seekable;
-
- uri = decoder_get_uri(decoder);
- /* disable seeking on remote streams, because libvorbis seeks
- around like crazy, and due to being very expensive, this
- delays song playback my 10 or 20 seconds */
- seekable = !uri_has_scheme(uri);
- g_free(uri);
-
- return seekable;
+ return is->seekable &&
+ (is->uri == NULL || !uri_has_scheme(is->uri));
}
/* public */
@@ -240,19 +250,19 @@ static void
vorbis_stream_decode(struct decoder *decoder,
struct input_stream *input_stream)
{
+ GError *error = NULL;
OggVorbis_File vf;
ov_callbacks callbacks;
- OggCallbackData data;
+ struct vorbis_decoder_data data;
struct audio_format audio_format;
+ float total_time;
int current_section;
int prev_section = -1;
long ret;
char chunk[OGG_CHUNK_SIZE];
long bitRate = 0;
long test;
- struct replay_gain_info *replay_gain_info = NULL;
- char **comments;
- bool initialized = false;
+ const vorbis_info *vi;
enum decoder_command cmd = DECODE_COMMAND_NONE;
if (ogg_stream_type_detect(input_stream) != VORBIS)
@@ -260,47 +270,44 @@ vorbis_stream_decode(struct decoder *decoder,
/* rewind the stream, because ogg_stream_type_detect() has
moved it */
- input_stream_seek(input_stream, 0, SEEK_SET);
+ input_stream_seek(input_stream, 0, SEEK_SET, NULL);
data.decoder = decoder;
data.input_stream = input_stream;
- data.seekable = input_stream->seekable && oggvorbis_seekable(decoder);
+ data.seekable = oggvorbis_seekable(input_stream);
callbacks.read_func = ogg_read_cb;
callbacks.seek_func = ogg_seek_cb;
callbacks.close_func = ogg_close_cb;
callbacks.tell_func = ogg_tell_cb;
if ((ret = ov_open_callbacks(&data, &vf, NULL, 0, callbacks)) < 0) {
- const char *error;
-
if (decoder_get_command(decoder) != DECODE_COMMAND_NONE)
return;
- switch (ret) {
- case OV_EREAD:
- error = "read error";
- break;
- case OV_ENOTVORBIS:
- error = "not vorbis stream";
- break;
- case OV_EVERSION:
- error = "vorbis version mismatch";
- break;
- case OV_EBADHEADER:
- error = "invalid vorbis header";
- break;
- case OV_EFAULT:
- error = "internal logic error";
- break;
- default:
- error = "unknown error";
- break;
- }
+ g_warning("Error decoding Ogg Vorbis stream: %s",
+ vorbis_strerror(ret));
+ return;
+ }
+
+ vi = ov_info(&vf, -1);
+ if (vi == NULL) {
+ g_warning("ov_info() has failed");
+ return;
+ }
- g_warning("Error decoding Ogg Vorbis stream: %s", error);
+ if (!audio_format_init_checked(&audio_format, vi->rate,
+ SAMPLE_FORMAT_S16,
+ vi->channels, &error)) {
+ g_warning("%s", error->message);
+ g_error_free(error);
return;
}
- audio_format.bits = 16;
+
+ total_time = ov_time_total(&vf, -1);
+ if (total_time < 0)
+ total_time = 0;
+
+ decoder_initialized(decoder, &audio_format, data.seekable, total_time);
do {
if (cmd == DECODE_COMMAND_SEEK) {
@@ -320,54 +327,40 @@ vorbis_stream_decode(struct decoder *decoder,
break;
if (current_section != prev_section) {
- /*printf("new song!\n"); */
- vorbis_info *vi = ov_info(&vf, -1);
- struct replay_gain_info *new_rgi;
-
- audio_format.channels = vi->channels;
- audio_format.sample_rate = vi->rate;
-
- if (!audio_format_valid(&audio_format)) {
- g_warning("Invalid audio format: %u:%u:%u\n",
- audio_format.sample_rate,
- audio_format.bits,
- audio_format.channels);
+ char **comments;
+
+ vi = ov_info(&vf, -1);
+ if (vi == NULL) {
+ g_warning("ov_info() has failed");
break;
}
- if (!initialized) {
- float total_time = ov_time_total(&vf, -1);
- if (total_time < 0)
- total_time = 0;
- decoder_initialized(decoder, &audio_format,
- data.seekable,
- total_time);
- initialized = true;
+ if (vi->rate != (long)audio_format.sample_rate ||
+ vi->channels != (int)audio_format.channels) {
+ /* we don't support audio format
+ change yet */
+ g_warning("audio format change, stopping here");
+ break;
}
+
comments = ov_comment(&vf, -1)->user_comments;
vorbis_send_comments(decoder, input_stream, comments);
- new_rgi = vorbis_comments_to_replay_gain(comments);
- if (new_rgi != NULL) {
- if (replay_gain_info != NULL)
- replay_gain_info_free(replay_gain_info);
- replay_gain_info = new_rgi;
- }
- }
- prev_section = current_section;
+ struct replay_gain_info rgi;
+ if (vorbis_comments_to_replay_gain(&rgi, comments))
+ decoder_replay_gain(decoder, &rgi);
+
+ prev_section = current_section;
+ }
if ((test = ov_bitrate_instant(&vf)) > 0)
bitRate = test / 1000;
cmd = decoder_data(decoder, input_stream,
chunk, ret,
- ov_pcm_tell(&vf) / audio_format.sample_rate,
- bitRate, replay_gain_info);
+ bitRate);
} while (cmd != DECODE_COMMAND_STOP);
- if (replay_gain_info)
- replay_gain_info_free(replay_gain_info);
-
ov_clear(&vf);
}
@@ -378,7 +371,7 @@ vorbis_tag_dup(const char *file)
FILE *fp;
OggVorbis_File vf;
- fp = fopen(file, "r");
+ fp = fopen(file, "rb");
if (!fp) {
return NULL;
}