2007-06-24 22:40:04 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
|
|
|
* This project's homepage is: http://www.musicpd.org
|
2008-11-04 17:08:57 +01:00
|
|
|
*
|
2007-06-25 14:13:45 +02:00
|
|
|
* WavPack support added by Laszlo Ashin <kodest@gmail.com>
|
2008-11-04 17:08:57 +01:00
|
|
|
*
|
2007-06-24 22:40:04 +02: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.
|
|
|
|
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2008-08-26 08:27:04 +02:00
|
|
|
#include "../decoder_api.h"
|
2008-04-12 06:19:26 +02:00
|
|
|
#include "../path.h"
|
2008-11-22 14:27:18 +01:00
|
|
|
#include "../utils.h"
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
#include <wavpack/wavpack.h>
|
2008-11-04 17:10:10 +01:00
|
|
|
#include <glib.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2008-11-24 10:33:08 +01:00
|
|
|
#include <unistd.h>
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-08-26 08:41:05 +02:00
|
|
|
/* pick 1020 since its devisible for 8,16,24, and 32-bit audio */
|
|
|
|
#define CHUNK_SIZE 1020
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
#define ERRORLEN 80
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
2008-11-04 17:10:13 +01:00
|
|
|
enum tag_type type;
|
2007-06-24 22:40:04 +02:00
|
|
|
} tagtypes[] = {
|
2008-11-04 17:08:57 +01:00
|
|
|
{ "artist", TAG_ITEM_ARTIST },
|
|
|
|
{ "album", TAG_ITEM_ALBUM },
|
|
|
|
{ "title", TAG_ITEM_TITLE },
|
|
|
|
{ "track", TAG_ITEM_TRACK },
|
|
|
|
{ "name", TAG_ITEM_NAME },
|
|
|
|
{ "genre", TAG_ITEM_GENRE },
|
|
|
|
{ "date", TAG_ITEM_DATE },
|
|
|
|
{ "composer", TAG_ITEM_COMPOSER },
|
|
|
|
{ "performer", TAG_ITEM_PERFORMER },
|
|
|
|
{ "comment", TAG_ITEM_COMMENT },
|
|
|
|
{ "disc", TAG_ITEM_DISC },
|
2007-06-24 22:40:04 +02:00
|
|
|
};
|
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
/** A pointer type for format converter function. */
|
|
|
|
typedef void (*format_samples_t)(
|
|
|
|
int bytes_per_sample,
|
|
|
|
void *buffer, uint32_t count
|
|
|
|
);
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
/*
|
|
|
|
* This function has been borrowed from the tiny player found on
|
|
|
|
* wavpack.com. Modifications were required because mpd only handles
|
2008-11-08 13:11:10 +01:00
|
|
|
* max 24-bit samples.
|
2007-06-24 22:40:04 +02:00
|
|
|
*/
|
2008-11-04 17:08:59 +01:00
|
|
|
static void
|
2008-11-14 15:21:44 +01:00
|
|
|
format_samples_int(int bytes_per_sample, void *buffer, uint32_t count)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-11-08 13:11:10 +01:00
|
|
|
int32_t *src = buffer;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
switch (bytes_per_sample) {
|
2008-11-08 13:11:10 +01:00
|
|
|
case 1: {
|
|
|
|
uchar *dst = buffer;
|
|
|
|
/*
|
|
|
|
* The asserts like the following one are because we do the
|
|
|
|
* formatting of samples within a single buffer. The size
|
|
|
|
* of the output samples never can be greater than the size
|
|
|
|
* of the input ones. Otherwise we would have an overflow.
|
|
|
|
*/
|
2008-11-22 14:27:18 +01:00
|
|
|
assert_static(sizeof(*dst) <= sizeof(*src));
|
2008-11-08 13:11:10 +01:00
|
|
|
|
|
|
|
/* pass through and align 8-bit samples */
|
2008-11-14 15:21:44 +01:00
|
|
|
while (count--) {
|
2008-11-04 17:08:57 +01:00
|
|
|
*dst++ = *src++;
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2008-11-04 17:08:57 +01:00
|
|
|
break;
|
2008-11-08 13:11:10 +01:00
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
uint16_t *dst = buffer;
|
2008-11-22 14:27:18 +01:00
|
|
|
assert_static(sizeof(*dst) <= sizeof(*src));
|
2008-11-08 13:11:10 +01:00
|
|
|
|
|
|
|
/* pass through and align 16-bit samples */
|
2008-11-14 15:21:44 +01:00
|
|
|
while (count--) {
|
2008-11-08 13:11:10 +01:00
|
|
|
*dst++ = *src++;
|
2008-11-04 17:08:57 +01:00
|
|
|
}
|
|
|
|
break;
|
2008-11-08 13:11:10 +01:00
|
|
|
}
|
2008-11-04 17:08:57 +01:00
|
|
|
case 3:
|
2008-11-08 13:11:10 +01:00
|
|
|
/* do nothing */
|
2008-11-04 17:08:57 +01:00
|
|
|
break;
|
2008-11-08 13:11:10 +01:00
|
|
|
case 4: {
|
|
|
|
uint32_t *dst = buffer;
|
2008-11-22 14:27:18 +01:00
|
|
|
assert_static(sizeof(*dst) <= sizeof(*src));
|
2008-11-08 13:11:10 +01:00
|
|
|
|
|
|
|
/* downsample to 24-bit */
|
2008-11-14 15:21:44 +01:00
|
|
|
while (count--) {
|
2008-11-08 13:11:10 +01:00
|
|
|
*dst++ = *src++ >> 8;
|
2008-11-04 17:08:57 +01:00
|
|
|
}
|
|
|
|
break;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
2008-11-08 13:11:10 +01:00
|
|
|
}
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-11-08 13:11:10 +01:00
|
|
|
* This function converts floating point sample data to 24-bit integer.
|
2007-06-24 22:40:04 +02:00
|
|
|
*/
|
2008-11-08 13:07:09 +01:00
|
|
|
static void
|
2009-01-01 18:09:24 +01:00
|
|
|
format_samples_float(G_GNUC_UNUSED int bytes_per_sample, void *buffer,
|
2008-11-14 15:21:44 +01:00
|
|
|
uint32_t count)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-11-08 13:11:10 +01:00
|
|
|
int32_t *dst = buffer;
|
|
|
|
float *src = buffer;
|
2008-11-22 14:27:18 +01:00
|
|
|
assert_static(sizeof(*dst) <= sizeof(*src));
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
while (count--) {
|
2008-11-08 13:11:10 +01:00
|
|
|
*dst++ = (int32_t)(*src++ + 0.5f);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This does the main decoding thing.
|
|
|
|
* Requires an already opened WavpackContext.
|
|
|
|
*/
|
2008-11-08 13:07:09 +01:00
|
|
|
static void
|
2008-11-14 15:21:44 +01:00
|
|
|
wavpack_decode(struct decoder *decoder, WavpackContext *wpc, bool can_seek,
|
|
|
|
struct replay_gain_info *replay_gain_info)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-09-07 19:19:55 +02:00
|
|
|
struct audio_format audio_format;
|
2008-11-14 15:21:44 +01:00
|
|
|
format_samples_t format_samples;
|
2007-06-24 22:40:04 +02:00
|
|
|
char chunk[CHUNK_SIZE];
|
2008-11-14 15:21:44 +01:00
|
|
|
int samples_requested, samples_got;
|
|
|
|
float total_time, current_time;
|
|
|
|
int bytes_per_sample, output_sample_size;
|
|
|
|
int position;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-10-10 14:40:54 +02:00
|
|
|
audio_format.sample_rate = WavpackGetSampleRate(wpc);
|
2008-08-26 08:27:05 +02:00
|
|
|
audio_format.channels = WavpackGetReducedChannels(wpc);
|
|
|
|
audio_format.bits = WavpackGetBitsPerSample(wpc);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-08 13:11:10 +01:00
|
|
|
/* round bitwidth to 8-bit units */
|
|
|
|
audio_format.bits = (audio_format.bits + 7) & (~7);
|
|
|
|
/* mpd handles max 24-bit samples */
|
|
|
|
if (audio_format.bits > 24) {
|
|
|
|
audio_format.bits = 24;
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-21 20:27:30 +01:00
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-08 13:08:21 +01:00
|
|
|
if ((WavpackGetMode(wpc) & MODE_FLOAT) == MODE_FLOAT) {
|
2007-06-24 22:40:04 +02:00
|
|
|
format_samples = format_samples_float;
|
2008-11-08 13:08:21 +01:00
|
|
|
} else {
|
2007-06-24 22:40:04 +02:00
|
|
|
format_samples = format_samples_int;
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2008-11-12 21:25:08 +01:00
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
total_time = WavpackGetNumSamples(wpc);
|
|
|
|
total_time /= audio_format.sample_rate;
|
2008-11-04 17:08:59 +01:00
|
|
|
bytes_per_sample = WavpackGetBytesPerSample(wpc);
|
2008-11-19 00:02:06 +01:00
|
|
|
output_sample_size = audio_format_frame_size(&audio_format);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-08 13:11:10 +01:00
|
|
|
/* wavpack gives us all kind of samples in a 32-bit space */
|
2008-11-14 15:21:44 +01:00
|
|
|
samples_requested = sizeof(chunk) / (4 * audio_format.channels);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
decoder_initialized(decoder, &audio_format, can_seek, total_time);
|
2008-08-26 08:27:04 +02:00
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
position = 0;
|
|
|
|
|
|
|
|
do {
|
2008-08-26 08:27:07 +02:00
|
|
|
if (decoder_get_command(decoder) == DECODE_COMMAND_SEEK) {
|
2008-11-14 15:21:44 +01:00
|
|
|
if (can_seek) {
|
2007-06-24 22:40:04 +02:00
|
|
|
int where;
|
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
where = decoder_seek_where(decoder);
|
|
|
|
where *= audio_format.sample_rate;
|
2008-08-26 08:27:07 +02:00
|
|
|
if (WavpackSeekSample(wpc, where)) {
|
2007-06-24 22:40:04 +02:00
|
|
|
position = where;
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_command_finished(decoder);
|
2008-11-08 13:08:21 +01:00
|
|
|
} else {
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_seek_error(decoder);
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2007-06-24 22:40:04 +02:00
|
|
|
} else {
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_seek_error(decoder);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-08 13:08:21 +01:00
|
|
|
if (decoder_get_command(decoder) == DECODE_COMMAND_STOP) {
|
2007-06-24 22:40:04 +02:00
|
|
|
break;
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
samples_got = WavpackUnpackSamples(
|
|
|
|
wpc, (int32_t *)chunk, samples_requested
|
|
|
|
);
|
|
|
|
if (samples_got > 0) {
|
2007-06-24 22:40:04 +02:00
|
|
|
int bitrate = (int)(WavpackGetInstantBitrate(wpc) /
|
|
|
|
1000 + 0.5);
|
2008-11-14 15:21:44 +01:00
|
|
|
position += samples_got;
|
|
|
|
current_time = position;
|
|
|
|
current_time /= audio_format.sample_rate;
|
|
|
|
|
|
|
|
format_samples(
|
|
|
|
bytes_per_sample, chunk,
|
|
|
|
samples_got * audio_format.channels
|
|
|
|
);
|
|
|
|
|
|
|
|
decoder_data(
|
|
|
|
decoder, NULL, chunk,
|
|
|
|
samples_got * output_sample_size,
|
|
|
|
current_time, bitrate,
|
|
|
|
replay_gain_info
|
|
|
|
);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
2008-11-14 15:23:13 +01:00
|
|
|
} while (samples_got > 0);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 16:21:09 +01:00
|
|
|
/**
|
|
|
|
* Locate and parse a floating point tag. Returns true if it was
|
|
|
|
* found.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
wavpack_tag_float(WavpackContext *wpc, const char *key, float *value_r)
|
2007-06-25 15:37:21 +02:00
|
|
|
{
|
2008-11-11 16:21:09 +01:00
|
|
|
char buffer[64];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = WavpackGetTagItem(wpc, key, buffer, sizeof(buffer));
|
|
|
|
if (ret <= 0)
|
|
|
|
return false;
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-11-11 16:21:09 +01:00
|
|
|
*value_r = atof(buffer);
|
|
|
|
return true;
|
2007-06-25 15:37:21 +02:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
static struct replay_gain_info *
|
2008-11-08 13:07:09 +01:00
|
|
|
wavpack_replaygain(WavpackContext *wpc)
|
2007-06-25 15:37:21 +02:00
|
|
|
{
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info *replay_gain_info;
|
2008-10-30 08:38:54 +01:00
|
|
|
bool found = false;
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_info = replay_gain_info_new();
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
found |= wavpack_tag_float(
|
|
|
|
wpc, "replaygain_track_gain",
|
|
|
|
&replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain
|
|
|
|
);
|
|
|
|
found |= wavpack_tag_float(
|
|
|
|
wpc, "replaygain_track_peak",
|
|
|
|
&replay_gain_info->tuples[REPLAY_GAIN_TRACK].peak
|
|
|
|
);
|
|
|
|
found |= wavpack_tag_float(
|
|
|
|
wpc, "replaygain_album_gain",
|
|
|
|
&replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain
|
|
|
|
);
|
|
|
|
found |= wavpack_tag_float(
|
|
|
|
wpc, "replaygain_album_peak",
|
|
|
|
&replay_gain_info->tuples[REPLAY_GAIN_ALBUM].peak
|
|
|
|
);
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-11-08 13:08:21 +01:00
|
|
|
if (found) {
|
2008-11-04 17:08:59 +01:00
|
|
|
return replay_gain_info;
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_info_free(replay_gain_info);
|
2007-06-25 15:37:21 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
/*
|
|
|
|
* Reads metainfo from the specified file.
|
|
|
|
*/
|
2008-11-08 13:07:09 +01:00
|
|
|
static struct tag *
|
|
|
|
wavpack_tagdup(const char *fname)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
WavpackContext *wpc;
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *tag;
|
2007-06-24 22:40:04 +02:00
|
|
|
char error[ERRORLEN];
|
|
|
|
char *s;
|
2008-11-14 15:21:44 +01:00
|
|
|
int size, allocated_size;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
wpc = WavpackOpenFileInput(fname, error, OPEN_TAGS, 0);
|
|
|
|
if (wpc == NULL) {
|
2008-11-14 15:21:44 +01:00
|
|
|
g_warning(
|
|
|
|
"failed to open WavPack file \"%s\": %s\n",
|
|
|
|
fname, error
|
|
|
|
);
|
2007-06-24 22:40:04 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
tag = tag_new();
|
2008-11-14 15:21:44 +01:00
|
|
|
tag->time = WavpackGetNumSamples(wpc);
|
|
|
|
tag->time /= WavpackGetSampleRate(wpc);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
allocated_size = 0;
|
2007-06-24 22:40:04 +02:00
|
|
|
s = NULL;
|
|
|
|
|
2008-11-04 17:10:17 +01:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(tagtypes); ++i) {
|
2008-11-14 15:21:44 +01:00
|
|
|
size = WavpackGetTagItem(wpc, tagtypes[i].name, NULL, 0);
|
|
|
|
if (size > 0) {
|
|
|
|
++size; /* EOS */
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
if (s == NULL) {
|
2008-11-14 15:21:44 +01:00
|
|
|
s = g_malloc(size);
|
|
|
|
allocated_size = size;
|
|
|
|
} else if (size > allocated_size) {
|
|
|
|
char *t = (char *)g_realloc(s, size);
|
|
|
|
allocated_size = size;
|
2007-06-24 22:40:04 +02:00
|
|
|
s = t;
|
|
|
|
}
|
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
WavpackGetTagItem(wpc, tagtypes[i].name, s, size);
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_add_item(tag, tagtypes[i].type, s);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 17:10:10 +01:00
|
|
|
g_free(s);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-10-26 19:54:57 +01:00
|
|
|
* mpd input_stream <=> WavpackStreamReader wrapper callbacks
|
2007-06-24 22:40:04 +02:00
|
|
|
*/
|
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
/* This struct is needed for per-stream last_byte storage. */
|
2008-11-04 17:08:59 +01:00
|
|
|
struct wavpack_input {
|
2008-08-26 08:27:14 +02:00
|
|
|
struct decoder *decoder;
|
2008-10-26 19:54:57 +01:00
|
|
|
struct input_stream *is;
|
2008-03-26 11:38:30 +01:00
|
|
|
/* Needed for push_back_byte() */
|
|
|
|
int last_byte;
|
2008-11-04 17:08:59 +01:00
|
|
|
};
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2008-11-08 13:09:07 +01:00
|
|
|
/**
|
|
|
|
* Little wrapper for struct wavpack_input to cast from void *.
|
|
|
|
*/
|
|
|
|
static struct wavpack_input *
|
|
|
|
wpin(void *id)
|
|
|
|
{
|
|
|
|
assert(id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static int32_t
|
2008-11-08 13:07:40 +01:00
|
|
|
wavpack_input_read_bytes(void *id, void *data, int32_t bcount)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
uint8_t *buf = (uint8_t *)data;
|
|
|
|
int32_t i = 0;
|
|
|
|
|
2008-11-08 13:09:07 +01:00
|
|
|
if (wpin(id)->last_byte != EOF) {
|
|
|
|
*buf++ = wpin(id)->last_byte;
|
|
|
|
wpin(id)->last_byte = EOF;
|
2007-06-24 22:40:04 +02:00
|
|
|
--bcount;
|
|
|
|
++i;
|
|
|
|
}
|
2008-11-05 07:24:57 +01:00
|
|
|
|
|
|
|
/* wavpack fails if we return a partial read, so we just wait
|
|
|
|
until the buffer is full */
|
|
|
|
while (bcount > 0) {
|
2008-11-14 15:21:44 +01:00
|
|
|
size_t nbytes = decoder_read(
|
|
|
|
wpin(id)->decoder, wpin(id)->is, buf, bcount
|
|
|
|
);
|
2008-11-08 13:08:21 +01:00
|
|
|
if (nbytes == 0) {
|
2008-11-05 07:24:57 +01:00
|
|
|
/* EOF, error or a decoder command */
|
|
|
|
break;
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2008-11-05 07:24:57 +01:00
|
|
|
|
|
|
|
i += nbytes;
|
|
|
|
bcount -= nbytes;
|
|
|
|
buf += nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static uint32_t
|
2008-11-08 13:07:40 +01:00
|
|
|
wavpack_input_get_pos(void *id)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-11-08 13:09:07 +01:00
|
|
|
return wpin(id)->is->offset;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static int
|
2008-11-08 13:07:40 +01:00
|
|
|
wavpack_input_set_pos_abs(void *id, uint32_t pos)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-11-08 13:09:07 +01:00
|
|
|
return input_stream_seek(wpin(id)->is, pos, SEEK_SET) ? 0 : -1;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static int
|
2008-11-08 13:07:40 +01:00
|
|
|
wavpack_input_set_pos_rel(void *id, int32_t delta, int mode)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-11-08 13:09:07 +01:00
|
|
|
return input_stream_seek(wpin(id)->is, delta, mode) ? 0 : -1;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static int
|
2008-11-08 13:07:40 +01:00
|
|
|
wavpack_input_push_back_byte(void *id, int c)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-11-08 13:10:15 +01:00
|
|
|
if (wpin(id)->last_byte == EOF) {
|
|
|
|
wpin(id)->last_byte = c;
|
|
|
|
return c;
|
|
|
|
} else {
|
|
|
|
return EOF;
|
|
|
|
}
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static uint32_t
|
2008-11-08 13:07:40 +01:00
|
|
|
wavpack_input_get_length(void *id)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-11-16 20:42:08 +01:00
|
|
|
if (wpin(id)->is->size < 0)
|
|
|
|
return 0;
|
|
|
|
|
2008-11-08 13:09:07 +01:00
|
|
|
return wpin(id)->is->size;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static int
|
2008-11-08 13:07:40 +01:00
|
|
|
wavpack_input_can_seek(void *id)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-11-08 13:09:07 +01:00
|
|
|
return wpin(id)->is->seekable;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static WavpackStreamReader mpd_is_reader = {
|
2008-11-08 13:07:40 +01:00
|
|
|
.read_bytes = wavpack_input_read_bytes,
|
|
|
|
.get_pos = wavpack_input_get_pos,
|
|
|
|
.set_pos_abs = wavpack_input_set_pos_abs,
|
|
|
|
.set_pos_rel = wavpack_input_set_pos_rel,
|
|
|
|
.push_back_byte = wavpack_input_push_back_byte,
|
|
|
|
.get_length = wavpack_input_get_length,
|
|
|
|
.can_seek = wavpack_input_can_seek,
|
2007-06-24 22:40:04 +02:00
|
|
|
.write_bytes = NULL /* no need to write edited tags */
|
|
|
|
};
|
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
static void
|
2008-11-04 17:08:59 +01:00
|
|
|
wavpack_input_init(struct wavpack_input *isp, struct decoder *decoder,
|
|
|
|
struct input_stream *is)
|
2008-03-26 11:38:30 +01:00
|
|
|
{
|
2008-08-26 08:27:14 +02:00
|
|
|
isp->decoder = decoder;
|
2008-03-26 11:38:30 +01:00
|
|
|
isp->is = is;
|
|
|
|
isp->last_byte = EOF;
|
|
|
|
}
|
|
|
|
|
2008-11-04 17:10:01 +01:00
|
|
|
static bool
|
2008-11-04 17:10:03 +01:00
|
|
|
wavpack_open_wvc(struct decoder *decoder, struct input_stream *is_wvc,
|
|
|
|
struct wavpack_input *wpi)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-08-26 08:27:14 +02:00
|
|
|
char tmp[MPD_PATH_MAX];
|
|
|
|
const char *utf8url;
|
2008-03-26 11:38:30 +01:00
|
|
|
char *wvc_url = NULL;
|
2008-10-26 20:56:46 +01:00
|
|
|
bool ret;
|
2008-11-04 17:10:03 +01:00
|
|
|
char first_byte;
|
|
|
|
size_t nbytes;
|
2008-08-26 08:27:14 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* As we use dc->utf8url, this function will be bad for
|
|
|
|
* single files. utf8url is not absolute file path :/
|
|
|
|
*/
|
|
|
|
utf8url = decoder_get_url(decoder, tmp);
|
2008-11-08 13:08:21 +01:00
|
|
|
if (utf8url == NULL) {
|
2008-11-04 17:10:01 +01:00
|
|
|
return false;
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2008-11-04 17:10:10 +01:00
|
|
|
wvc_url = g_strconcat(utf8url, "c", NULL);
|
2008-10-26 20:34:47 +01:00
|
|
|
ret = input_stream_open(is_wvc, wvc_url);
|
2008-11-04 17:10:10 +01:00
|
|
|
g_free(wvc_url);
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2008-11-08 13:08:21 +01:00
|
|
|
if (!ret) {
|
2008-11-04 17:10:01 +01:00
|
|
|
return false;
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2008-08-26 08:27:14 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* And we try to buffer in order to get know
|
|
|
|
* about a possible 404 error.
|
|
|
|
*/
|
2008-11-14 15:21:44 +01:00
|
|
|
nbytes = decoder_read(
|
|
|
|
decoder, is_wvc, &first_byte, sizeof(first_byte)
|
|
|
|
);
|
2008-11-06 06:48:33 +01:00
|
|
|
if (nbytes == 0) {
|
|
|
|
input_stream_close(is_wvc);
|
2008-11-04 17:10:03 +01:00
|
|
|
return false;
|
2008-11-06 06:48:33 +01:00
|
|
|
}
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2008-11-04 17:10:03 +01:00
|
|
|
/* push it back */
|
|
|
|
wavpack_input_init(wpi, decoder, is_wvc);
|
|
|
|
wpi->last_byte = first_byte;
|
|
|
|
return true;
|
2008-08-26 08:27:14 +02:00
|
|
|
}
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2008-08-26 08:27:14 +02:00
|
|
|
/*
|
|
|
|
* Decodes a stream.
|
|
|
|
*/
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2008-10-26 19:54:57 +01:00
|
|
|
wavpack_streamdecode(struct decoder * decoder, struct input_stream *is)
|
2008-08-26 08:27:14 +02:00
|
|
|
{
|
|
|
|
char error[ERRORLEN];
|
|
|
|
WavpackContext *wpc;
|
2008-10-26 19:54:57 +01:00
|
|
|
struct input_stream is_wvc;
|
2008-11-14 15:23:18 +01:00
|
|
|
int open_flags = OPEN_2CH_MAX | OPEN_NORMALIZE;
|
2008-11-04 17:08:59 +01:00
|
|
|
struct wavpack_input isp, isp_wvc;
|
2008-11-14 15:21:44 +01:00
|
|
|
bool can_seek = is->seekable;
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2008-11-06 06:48:33 +01:00
|
|
|
if (wavpack_open_wvc(decoder, &is_wvc, &isp_wvc)) {
|
2008-03-26 11:38:30 +01:00
|
|
|
open_flags |= OPEN_WVC;
|
2008-11-14 15:21:44 +01:00
|
|
|
can_seek &= is_wvc.seekable;
|
2008-11-06 06:48:33 +01:00
|
|
|
}
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2008-11-14 15:23:18 +01:00
|
|
|
if (!can_seek) {
|
|
|
|
open_flags |= OPEN_STREAMING;
|
|
|
|
}
|
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
wavpack_input_init(&isp, decoder, is);
|
2008-11-14 15:21:44 +01:00
|
|
|
wpc = WavpackOpenFileInputEx(
|
|
|
|
&mpd_is_reader, &isp, &isp_wvc, error, open_flags, 23
|
|
|
|
);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
if (wpc == NULL) {
|
2008-11-04 17:10:10 +01:00
|
|
|
g_warning("failed to open WavPack stream: %s\n", error);
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
wavpack_decode(decoder, wpc, can_seek, NULL);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
2008-11-08 13:08:21 +01:00
|
|
|
if (open_flags & OPEN_WVC) {
|
2008-10-26 20:34:47 +01:00
|
|
|
input_stream_close(&is_wvc);
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-26 11:38:30 +01:00
|
|
|
* Decodes a file.
|
2007-06-24 22:40:04 +02:00
|
|
|
*/
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2008-10-31 15:56:43 +01:00
|
|
|
wavpack_filedecode(struct decoder *decoder, const char *fname)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
char error[ERRORLEN];
|
|
|
|
WavpackContext *wpc;
|
2008-11-11 15:55:34 +01:00
|
|
|
struct replay_gain_info *replay_gain_info;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
wpc = WavpackOpenFileInput(
|
|
|
|
fname, error,
|
|
|
|
OPEN_TAGS | OPEN_WVC | OPEN_2CH_MAX | OPEN_NORMALIZE, 23
|
|
|
|
);
|
2007-06-24 22:40:04 +02:00
|
|
|
if (wpc == NULL) {
|
2008-11-14 15:21:44 +01:00
|
|
|
g_warning(
|
|
|
|
"failed to open WavPack file \"%s\": %s\n",
|
|
|
|
fname, error
|
|
|
|
);
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
replay_gain_info = wavpack_replaygain(wpc);
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-11-04 17:10:01 +01:00
|
|
|
wavpack_decode(decoder, wpc, true, replay_gain_info);
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-11-08 13:08:21 +01:00
|
|
|
if (replay_gain_info) {
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_info_free(replay_gain_info);
|
2008-11-08 13:08:21 +01:00
|
|
|
}
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
|
|
|
}
|
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
static char const *const wavpack_suffixes[] = {
|
|
|
|
"wv",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static char const *const wavpack_mime_types[] = {
|
|
|
|
"audio/x-wavpack",
|
|
|
|
NULL
|
|
|
|
};
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
const struct decoder_plugin wavpack_plugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "wavpack",
|
|
|
|
.stream_decode = wavpack_streamdecode,
|
|
|
|
.file_decode = wavpack_filedecode,
|
|
|
|
.tag_dup = wavpack_tagdup,
|
2008-11-04 17:08:59 +01:00
|
|
|
.suffixes = wavpack_suffixes,
|
|
|
|
.mime_types = wavpack_mime_types
|
2007-06-24 22:40:04 +02:00
|
|
|
};
|