2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
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.
|
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.
|
2007-06-24 22:40:04 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-10 18:18:14 +01:00
|
|
|
#include "WavpackDecoderPlugin.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2013-09-04 23:46:20 +02:00
|
|
|
#include "tag/ApeTag.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-10-15 22:04:17 +02:00
|
|
|
#include "util/Macros.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
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>
|
2009-01-03 14:53:42 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
#define ERRORLEN 80
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain wavpack_domain("wavpack");
|
|
|
|
|
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
|
|
|
{
|
2013-01-10 18:18:14 +01:00
|
|
|
int32_t *src = (int32_t *)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: {
|
2013-01-10 18:18:14 +01:00
|
|
|
int8_t *dst = (int8_t *)buffer;
|
2008-11-08 13:11:10 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-01-10 18:18:14 +01:00
|
|
|
static_assert(sizeof(*dst) <= sizeof(*src), "Wrong size");
|
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: {
|
2013-01-10 18:18:14 +01:00
|
|
|
uint16_t *dst = (uint16_t *)buffer;
|
|
|
|
static_assert(sizeof(*dst) <= sizeof(*src), "Wrong size");
|
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
|
|
|
}
|
2009-11-11 21:49:00 +01:00
|
|
|
|
2008-11-04 17:08:57 +01:00
|
|
|
case 3:
|
2009-11-11 21:49:00 +01:00
|
|
|
case 4:
|
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
|
|
|
}
|
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
|
2013-08-04 23:48:01 +02:00
|
|
|
format_samples_float(gcc_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
|
|
|
{
|
2013-01-10 18:18:14 +01:00
|
|
|
float *p = (float *)buffer;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-14 15:21:44 +01:00
|
|
|
while (count--) {
|
2011-10-08 15:01:01 +02:00
|
|
|
*p /= (1 << 23);
|
|
|
|
++p;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
/**
|
|
|
|
* Choose a MPD sample format from libwavpacks' number of bits.
|
|
|
|
*/
|
2013-08-03 21:00:50 +02:00
|
|
|
static SampleFormat
|
2009-11-10 17:11:34 +01:00
|
|
|
wavpack_bits_to_sample_format(bool is_float, int bytes_per_sample)
|
|
|
|
{
|
|
|
|
if (is_float)
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::FLOAT;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
switch (bytes_per_sample) {
|
|
|
|
case 1:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S8;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
case 2:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S16;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
case 3:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S24_P32;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
case 4:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::S32;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
default:
|
2013-08-03 21:00:50 +02:00
|
|
|
return SampleFormat::UNDEFINED;
|
2009-11-10 17:11:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2013-10-21 21:12:37 +02:00
|
|
|
wavpack_decode(Decoder &decoder, WavpackContext *wpc, bool can_seek)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2013-10-23 23:20:55 +02:00
|
|
|
bool is_float = (WavpackGetMode(wpc) & MODE_FLOAT) != 0;
|
|
|
|
SampleFormat sample_format =
|
2009-11-10 17:11:34 +01:00
|
|
|
wavpack_bits_to_sample_format(is_float,
|
|
|
|
WavpackGetBytesPerSample(wpc));
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-10-23 23:20:55 +02:00
|
|
|
AudioFormat audio_format;
|
2013-08-03 21:00:50 +02:00
|
|
|
if (!audio_format_init_checked(audio_format,
|
2009-11-10 17:11:34 +01:00
|
|
|
WavpackGetSampleRate(wpc),
|
|
|
|
sample_format,
|
2013-08-10 18:02:44 +02:00
|
|
|
WavpackGetNumChannels(wpc), error)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2008-11-21 20:27:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:20:55 +02:00
|
|
|
const format_samples_t format_samples = is_float
|
|
|
|
? format_samples_float
|
|
|
|
: format_samples_int;
|
2008-11-12 21:25:08 +01:00
|
|
|
|
2013-10-23 23:20:55 +02:00
|
|
|
const float total_time = float(WavpackGetNumSamples(wpc))
|
|
|
|
/ audio_format.sample_rate;
|
|
|
|
|
|
|
|
const int bytes_per_sample = WavpackGetBytesPerSample(wpc);
|
|
|
|
const int output_sample_size = audio_format.GetFrameSize();
|
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 */
|
2011-07-19 22:47:12 +02:00
|
|
|
int32_t chunk[1024];
|
2013-10-15 22:04:17 +02:00
|
|
|
const uint32_t samples_requested = ARRAY_SIZE(chunk) /
|
2011-07-19 22:47:12 +02:00
|
|
|
audio_format.channels;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
decoder_initialized(decoder, audio_format, can_seek, total_time);
|
2008-08-26 08:27:04 +02:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd = decoder_get_command(decoder);
|
|
|
|
while (cmd != DecoderCommand::STOP) {
|
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2008-11-14 15:21:44 +01:00
|
|
|
if (can_seek) {
|
2009-12-26 00:35:05 +01:00
|
|
|
unsigned where = decoder_seek_where(decoder) *
|
|
|
|
audio_format.sample_rate;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-08-26 08:27:07 +02:00
|
|
|
if (WavpackSeekSample(wpc, where)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-19 22:47:12 +02:00
|
|
|
uint32_t samples_got = WavpackUnpackSamples(wpc, chunk,
|
|
|
|
samples_requested);
|
2011-07-19 22:32:48 +02:00
|
|
|
if (samples_got == 0)
|
2007-06-24 22:40:04 +02:00
|
|
|
break;
|
|
|
|
|
2011-07-19 22:32:48 +02:00
|
|
|
int bitrate = (int)(WavpackGetInstantBitrate(wpc) / 1000 +
|
|
|
|
0.5);
|
|
|
|
format_samples(bytes_per_sample, chunk,
|
|
|
|
samples_got * audio_format.channels);
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
cmd = decoder_data(decoder, nullptr, chunk,
|
2011-07-20 12:30:56 +02:00
|
|
|
samples_got * output_sample_size,
|
|
|
|
bitrate);
|
2011-07-19 22:32:48 +02:00
|
|
|
}
|
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];
|
2013-10-23 23:20:55 +02:00
|
|
|
if (WavpackGetTagItem(wpc, key, buffer, sizeof(buffer)) <= 0)
|
2008-11-11 16:21:09 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
static bool
|
2013-10-25 19:09:22 +02:00
|
|
|
wavpack_replaygain(ReplayGainInfo &rgi,
|
2010-02-14 20:36:31 +01:00
|
|
|
WavpackContext *wpc)
|
2007-06-25 15:37:21 +02:00
|
|
|
{
|
2013-10-25 19:05:49 +02:00
|
|
|
rgi.Clear();
|
2013-10-25 19:09:22 +02:00
|
|
|
|
2013-10-25 19:05:49 +02:00
|
|
|
bool found = false;
|
2013-10-25 19:09:22 +02:00
|
|
|
found |= wavpack_tag_float(wpc, "replaygain_track_gain",
|
|
|
|
&rgi.tuples[REPLAY_GAIN_TRACK].gain);
|
|
|
|
found |= wavpack_tag_float(wpc, "replaygain_track_peak",
|
|
|
|
&rgi.tuples[REPLAY_GAIN_TRACK].peak);
|
|
|
|
found |= wavpack_tag_float(wpc, "replaygain_album_gain",
|
|
|
|
&rgi.tuples[REPLAY_GAIN_ALBUM].gain);
|
|
|
|
found |= wavpack_tag_float(wpc, "replaygain_album_peak",
|
|
|
|
&rgi.tuples[REPLAY_GAIN_ALBUM].peak);
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
return found;
|
2007-06-25 15:37:21 +02:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:36:59 +01:00
|
|
|
static void
|
|
|
|
wavpack_scan_tag_item(WavpackContext *wpc, const char *name,
|
2013-10-20 13:32:59 +02:00
|
|
|
TagType type,
|
2012-02-11 19:36:59 +01:00
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
int len = WavpackGetTagItem(wpc, name, buffer, sizeof(buffer));
|
|
|
|
if (len <= 0 || (unsigned)len >= sizeof(buffer))
|
|
|
|
return;
|
|
|
|
|
|
|
|
tag_handler_invoke_tag(handler, handler_ctx, type, buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:24:51 +01:00
|
|
|
static void
|
|
|
|
wavpack_scan_pair(WavpackContext *wpc, const char *name,
|
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
|
|
|
{
|
2012-02-12 16:10:15 +01:00
|
|
|
char buffer[8192];
|
2012-02-11 19:24:51 +01:00
|
|
|
int len = WavpackGetTagItem(wpc, name, buffer, sizeof(buffer));
|
|
|
|
if (len <= 0 || (unsigned)len >= sizeof(buffer))
|
|
|
|
return;
|
|
|
|
|
|
|
|
tag_handler_invoke_pair(handler, handler_ctx, name, buffer);
|
|
|
|
}
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
/*
|
|
|
|
* Reads metainfo from the specified file.
|
|
|
|
*/
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
wavpack_scan_file(const char *fname,
|
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
char error[ERRORLEN];
|
2013-10-23 23:20:55 +02:00
|
|
|
WavpackContext *wpc = WavpackOpenFileInput(fname, error, OPEN_TAGS, 0);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (wpc == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(wavpack_domain,
|
|
|
|
"failed to open WavPack file \"%s\": %s",
|
|
|
|
fname, error);
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx,
|
|
|
|
WavpackGetNumSamples(wpc) /
|
|
|
|
WavpackGetSampleRate(wpc));
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2012-09-25 09:37:16 +02:00
|
|
|
/* the WavPack format implies APEv2 tags, which means we can
|
|
|
|
reuse the mapping from tag_ape.c */
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
|
|
|
|
const char *name = tag_item_names[i];
|
2013-10-19 18:19:03 +02:00
|
|
|
if (name != nullptr)
|
2013-10-20 13:32:59 +02:00
|
|
|
wavpack_scan_tag_item(wpc, name, (TagType)i,
|
2012-09-25 09:37:16 +02:00
|
|
|
handler, handler_ctx);
|
|
|
|
}
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
for (const struct tag_table *i = ape_tags; i->name != nullptr; ++i)
|
2012-02-11 19:36:59 +01:00
|
|
|
wavpack_scan_tag_item(wpc, i->name, i->type,
|
|
|
|
handler, handler_ctx);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (handler->pair != nullptr) {
|
2012-02-11 19:24:51 +01:00
|
|
|
char name[64];
|
|
|
|
|
|
|
|
for (int i = 0, n = WavpackGetNumTagItems(wpc);
|
|
|
|
i < n; ++i) {
|
|
|
|
int len = WavpackGetTagItemIndexed(wpc, i, name,
|
|
|
|
sizeof(name));
|
|
|
|
if (len <= 0 || (unsigned)len >= sizeof(name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
wavpack_scan_pair(wpc, name, handler, handler_ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
WavpackCloseFile(wpc);
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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 {
|
2013-10-21 21:12:37 +02:00
|
|
|
Decoder *decoder;
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream *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);
|
2013-01-10 18:18:14 +01:00
|
|
|
return (struct wavpack_input *)id;
|
2008-11-08 13:09:07 +01:00
|
|
|
}
|
|
|
|
|
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(
|
2013-10-23 22:08:59 +02:00
|
|
|
wpin(id)->decoder, *wpin(id)->is, buf, bcount
|
2008-11-14 15:21:44 +01:00
|
|
|
);
|
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
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
return wpin(id)->is->LockSeek(pos, SEEK_SET, IgnoreError()) ? 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
|
|
|
{
|
2013-09-05 00:06:31 +02:00
|
|
|
return wpin(id)->is->LockSeek(delta, mode, IgnoreError()) ? 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 = {
|
2013-01-10 18:18:14 +01:00
|
|
|
wavpack_input_read_bytes,
|
|
|
|
wavpack_input_get_pos,
|
|
|
|
wavpack_input_set_pos_abs,
|
|
|
|
wavpack_input_set_pos_rel,
|
|
|
|
wavpack_input_push_back_byte,
|
|
|
|
wavpack_input_get_length,
|
|
|
|
wavpack_input_can_seek,
|
|
|
|
nullptr /* no need to write edited tags */
|
2007-06-24 22:40:04 +02:00
|
|
|
};
|
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
static void
|
2013-10-21 21:12:37 +02:00
|
|
|
wavpack_input_init(struct wavpack_input *isp, Decoder &decoder,
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream &is)
|
2008-03-26 11:38:30 +01:00
|
|
|
{
|
2013-10-21 21:12:37 +02:00
|
|
|
isp->decoder = &decoder;
|
2013-10-23 22:08:59 +02:00
|
|
|
isp->is = &is;
|
2008-03-26 11:38:30 +01:00
|
|
|
isp->last_byte = EOF;
|
|
|
|
}
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
static InputStream *
|
2013-10-21 21:12:37 +02:00
|
|
|
wavpack_open_wvc(Decoder &decoder, const char *uri,
|
2013-01-27 17:20:50 +01:00
|
|
|
Mutex &mutex, Cond &cond,
|
2010-01-20 12:08:44 +01:00
|
|
|
struct wavpack_input *wpi)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
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 :/
|
|
|
|
*/
|
2013-10-19 18:19:03 +02:00
|
|
|
if (uri == nullptr)
|
2013-01-10 18:18:14 +01:00
|
|
|
return nullptr;
|
2009-01-04 19:09:34 +01:00
|
|
|
|
2013-10-23 23:20:55 +02:00
|
|
|
char *wvc_url = g_strconcat(uri, "c", nullptr);
|
2013-08-10 18:02:44 +02:00
|
|
|
|
2013-10-23 23:20:55 +02:00
|
|
|
InputStream *is_wvc = InputStream::Open(wvc_url, mutex, cond,
|
|
|
|
IgnoreError());
|
2008-11-04 17:10:10 +01:00
|
|
|
g_free(wvc_url);
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (is_wvc == nullptr)
|
|
|
|
return nullptr;
|
2008-08-26 08:27:14 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* And we try to buffer in order to get know
|
|
|
|
* about a possible 404 error.
|
|
|
|
*/
|
2013-10-23 23:20:55 +02:00
|
|
|
char first_byte;
|
|
|
|
size_t nbytes = decoder_read(decoder, *is_wvc,
|
|
|
|
&first_byte, sizeof(first_byte));
|
2008-11-06 06:48:33 +01:00
|
|
|
if (nbytes == 0) {
|
2013-09-05 00:06:31 +02:00
|
|
|
is_wvc->Close();
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
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 */
|
2013-10-23 22:08:59 +02:00
|
|
|
wavpack_input_init(wpi, decoder, *is_wvc);
|
2008-11-04 17:10:03 +01:00
|
|
|
wpi->last_byte = first_byte;
|
2009-12-30 23:27:37 +01:00
|
|
|
return is_wvc;
|
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
|
2013-10-23 22:08:59 +02:00
|
|
|
wavpack_streamdecode(Decoder &decoder, InputStream &is)
|
2008-08-26 08:27:14 +02:00
|
|
|
{
|
2009-11-11 23:03:20 +01:00
|
|
|
int open_flags = OPEN_NORMALIZE;
|
2013-10-23 22:08:59 +02:00
|
|
|
bool can_seek = is.seekable;
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2013-10-23 23:20:55 +02:00
|
|
|
wavpack_input isp_wvc;
|
|
|
|
InputStream *is_wvc = wavpack_open_wvc(decoder, is.uri.c_str(),
|
|
|
|
is.mutex, is.cond,
|
|
|
|
&isp_wvc);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (is_wvc != nullptr) {
|
2008-03-26 11:38:30 +01:00
|
|
|
open_flags |= OPEN_WVC;
|
2009-12-30 23:27:37 +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;
|
|
|
|
}
|
|
|
|
|
2013-10-23 23:20:55 +02:00
|
|
|
wavpack_input isp;
|
2008-11-04 17:08:59 +01:00
|
|
|
wavpack_input_init(&isp, decoder, is);
|
2013-10-23 23:20:55 +02:00
|
|
|
|
|
|
|
char error[ERRORLEN];
|
|
|
|
WavpackContext *wpc =
|
|
|
|
WavpackOpenFileInputEx(&mpd_is_reader, &isp,
|
|
|
|
open_flags & OPEN_WVC
|
|
|
|
? &isp_wvc : nullptr,
|
|
|
|
error, open_flags, 23);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
if (wpc == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(wavpack_domain,
|
|
|
|
"failed to open WavPack stream: %s", error);
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2010-01-03 22:44:23 +01:00
|
|
|
wavpack_decode(decoder, wpc, can_seek);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
2008-11-08 13:08:21 +01:00
|
|
|
if (open_flags & OPEN_WVC) {
|
2013-09-05 00:06:31 +02:00
|
|
|
is_wvc->Close();
|
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
|
2013-10-21 21:12:37 +02:00
|
|
|
wavpack_filedecode(Decoder &decoder, const char *fname)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
char error[ERRORLEN];
|
2013-10-23 23:20:55 +02:00
|
|
|
WavpackContext *wpc = WavpackOpenFileInput(fname, error,
|
|
|
|
OPEN_TAGS | OPEN_WVC | OPEN_NORMALIZE,
|
|
|
|
23);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (wpc == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(wavpack_domain,
|
|
|
|
"failed to open WavPack file \"%s\": %s",
|
|
|
|
fname, error);
|
2008-11-11 17:13:44 +01:00
|
|
|
return;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2013-10-25 19:09:22 +02:00
|
|
|
ReplayGainInfo rgi;
|
|
|
|
if (wavpack_replaygain(rgi, wpc))
|
|
|
|
decoder_replay_gain(decoder, &rgi);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2010-01-03 22:44:23 +01:00
|
|
|
wavpack_decode(decoder, wpc, true);
|
|
|
|
|
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",
|
2013-10-19 18:19:03 +02:00
|
|
|
nullptr
|
2008-11-14 15:21:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static char const *const wavpack_mime_types[] = {
|
|
|
|
"audio/x-wavpack",
|
2013-10-19 18:19:03 +02:00
|
|
|
nullptr
|
2008-11-14 15:21:44 +01:00
|
|
|
};
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin wavpack_decoder_plugin = {
|
2013-01-10 18:18:14 +01:00
|
|
|
"wavpack",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
wavpack_streamdecode,
|
|
|
|
wavpack_filedecode,
|
|
|
|
wavpack_scan_file,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
wavpack_suffixes,
|
|
|
|
wavpack_mime_types
|
2007-06-24 22:40:04 +02:00
|
|
|
};
|