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
|
|
|
|
*
|
2007-06-25 14:13:45 +02:00
|
|
|
* WavPack support added by Laszlo Ashin <kodest@gmail.com>
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inputPlugin.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_WAVPACK
|
|
|
|
|
|
|
|
#include "../utils.h"
|
|
|
|
#include "../audio.h"
|
|
|
|
#include "../log.h"
|
|
|
|
#include "../pcm_utils.h"
|
|
|
|
#include "../playerData.h"
|
|
|
|
#include "../outputBuffer.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "../os_compat.h"
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
#include <wavpack/wavpack.h>
|
|
|
|
|
|
|
|
#define ERRORLEN 80
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
int type;
|
|
|
|
} tagtypes[] = {
|
|
|
|
{ "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 },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function has been borrowed from the tiny player found on
|
|
|
|
* wavpack.com. Modifications were required because mpd only handles
|
|
|
|
* max 16 bit samples.
|
|
|
|
*/
|
|
|
|
static void format_samples_int(int Bps, void *buffer, uint32_t samcnt)
|
|
|
|
{
|
|
|
|
int32_t temp;
|
|
|
|
uchar *dst = (uchar *)buffer;
|
|
|
|
int32_t *src = (int32_t *)buffer;
|
|
|
|
|
|
|
|
switch (Bps) {
|
|
|
|
case 1:
|
|
|
|
while (samcnt--)
|
|
|
|
*dst++ = *src++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
while (samcnt--) {
|
2007-10-03 18:11:01 +02:00
|
|
|
temp = *src++;
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
*dst++ = (uchar)(temp >> 8);
|
|
|
|
*dst++ = (uchar)(temp);
|
|
|
|
#else
|
|
|
|
*dst++ = (uchar)(temp);
|
2007-06-24 22:40:04 +02:00
|
|
|
*dst++ = (uchar)(temp >> 8);
|
2007-10-03 18:11:01 +02:00
|
|
|
#endif
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
/* downscale to 16 bits */
|
|
|
|
while (samcnt--) {
|
|
|
|
temp = *src++;
|
2007-10-03 18:11:01 +02:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
*dst++ = (uchar)(temp >> 16);
|
|
|
|
*dst++ = (uchar)(temp >> 8);
|
|
|
|
#else
|
2007-06-24 22:40:04 +02:00
|
|
|
*dst++ = (uchar)(temp >> 8);
|
|
|
|
*dst++ = (uchar)(temp >> 16);
|
2007-10-03 18:11:01 +02:00
|
|
|
#endif
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
/* downscale to 16 bits */
|
|
|
|
while (samcnt--) {
|
|
|
|
temp = *src++;
|
2007-10-03 18:11:01 +02:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
*dst++ = (uchar)(temp >> 24);
|
|
|
|
*dst++ = (uchar)(temp >> 16);
|
|
|
|
|
|
|
|
#else
|
2007-06-24 22:40:04 +02:00
|
|
|
*dst++ = (uchar)(temp >> 16);
|
|
|
|
*dst++ = (uchar)(temp >> 24);
|
2007-10-03 18:11:01 +02:00
|
|
|
#endif
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function converts floating point sample data to 16 bit integer.
|
|
|
|
*/
|
|
|
|
static void format_samples_float(int Bps, void *buffer, uint32_t samcnt)
|
|
|
|
{
|
|
|
|
int16_t *dst = (int16_t *)buffer;
|
|
|
|
float *src = (float *)buffer;
|
|
|
|
|
|
|
|
while (samcnt--) {
|
|
|
|
*dst++ = (int16_t)(*src++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This does the main decoding thing.
|
|
|
|
* Requires an already opened WavpackContext.
|
|
|
|
*/
|
|
|
|
static void wavpack_decode(OutputBuffer *cb, DecoderControl *dc,
|
2007-06-25 15:37:21 +02:00
|
|
|
WavpackContext *wpc, int canseek,
|
|
|
|
ReplayGainInfo *replayGainInfo)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
void (*format_samples)(int Bps, void *buffer, uint32_t samcnt);
|
|
|
|
char chunk[CHUNK_SIZE];
|
2008-04-12 06:16:32 +02:00
|
|
|
float file_time;
|
2007-06-24 22:40:04 +02:00
|
|
|
int samplesreq, samplesgot;
|
|
|
|
int allsamples;
|
|
|
|
int position, outsamplesize;
|
|
|
|
int Bps;
|
|
|
|
|
|
|
|
dc->audioFormat.sampleRate = WavpackGetSampleRate(wpc);
|
|
|
|
dc->audioFormat.channels = WavpackGetReducedChannels(wpc);
|
|
|
|
dc->audioFormat.bits = WavpackGetBitsPerSample(wpc);
|
|
|
|
|
|
|
|
if (dc->audioFormat.bits > 16)
|
|
|
|
dc->audioFormat.bits = 16;
|
|
|
|
|
|
|
|
if ((WavpackGetMode(wpc) & MODE_FLOAT) == MODE_FLOAT)
|
|
|
|
format_samples = format_samples_float;
|
|
|
|
else
|
|
|
|
format_samples = format_samples_int;
|
2008-03-26 11:38:30 +01:00
|
|
|
/*
|
|
|
|
if ((WavpackGetMode(wpc) & MODE_WVC) == MODE_WVC)
|
|
|
|
ERROR("decoding WITH wvc!!!\n");
|
|
|
|
else
|
|
|
|
ERROR("decoding without wvc\n");
|
|
|
|
*/
|
2007-06-24 22:40:04 +02:00
|
|
|
allsamples = WavpackGetNumSamples(wpc);
|
|
|
|
Bps = WavpackGetBytesPerSample(wpc);
|
|
|
|
|
|
|
|
outsamplesize = Bps;
|
|
|
|
if (outsamplesize > 2)
|
|
|
|
outsamplesize = 2;
|
|
|
|
outsamplesize *= dc->audioFormat.channels;
|
|
|
|
|
|
|
|
samplesreq = sizeof(chunk) / (4 * dc->audioFormat.channels);
|
|
|
|
|
|
|
|
getOutputAudioFormat(&(dc->audioFormat), &(cb->audioFormat));
|
|
|
|
|
|
|
|
dc->totalTime = (float)allsamples / dc->audioFormat.sampleRate;
|
|
|
|
dc->state = DECODE_STATE_DECODE;
|
2008-03-26 11:38:30 +01:00
|
|
|
dc->seekable = canseek;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
position = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (dc->seek) {
|
|
|
|
if (canseek) {
|
|
|
|
int where;
|
|
|
|
|
|
|
|
clearOutputBuffer(cb);
|
|
|
|
|
|
|
|
where = dc->seekWhere *
|
|
|
|
dc->audioFormat.sampleRate;
|
|
|
|
if (WavpackSeekSample(wpc, where))
|
|
|
|
position = where;
|
|
|
|
else
|
|
|
|
dc->seekError = 1;
|
|
|
|
} else {
|
|
|
|
dc->seekError = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dc->seek = 0;
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
decoder_wakeup_player();
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dc->stop)
|
|
|
|
break;
|
|
|
|
|
|
|
|
samplesgot = WavpackUnpackSamples(wpc,
|
|
|
|
(int32_t *)chunk, samplesreq);
|
|
|
|
if (samplesgot > 0) {
|
|
|
|
int bitrate = (int)(WavpackGetInstantBitrate(wpc) /
|
|
|
|
1000 + 0.5);
|
|
|
|
position += samplesgot;
|
2008-04-12 06:16:32 +02:00
|
|
|
file_time = (float)position /
|
|
|
|
dc->audioFormat.sampleRate;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
format_samples(Bps, chunk,
|
|
|
|
samplesgot * dc->audioFormat.channels);
|
|
|
|
|
|
|
|
sendDataToOutputBuffer(cb, NULL, dc, 0, chunk,
|
|
|
|
samplesgot * outsamplesize,
|
2008-04-12 06:16:32 +02:00
|
|
|
file_time, bitrate,
|
|
|
|
replayGainInfo);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
} while (samplesgot == samplesreq);
|
|
|
|
|
|
|
|
flushOutputBuffer(cb);
|
|
|
|
}
|
|
|
|
|
2007-06-25 15:37:21 +02:00
|
|
|
static char *wavpack_tag(WavpackContext *wpc, char *key)
|
|
|
|
{
|
|
|
|
char *value = NULL;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
size = WavpackGetTagItem(wpc, key, NULL, 0);
|
|
|
|
if (size > 0) {
|
|
|
|
size++;
|
|
|
|
value = xmalloc(size);
|
|
|
|
if (!value)
|
|
|
|
return NULL;
|
|
|
|
WavpackGetTagItem(wpc, key, value, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ReplayGainInfo *wavpack_replaygain(WavpackContext *wpc)
|
|
|
|
{
|
|
|
|
ReplayGainInfo *replayGainInfo;
|
|
|
|
int found = 0;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
replayGainInfo = newReplayGainInfo();
|
|
|
|
|
|
|
|
value = wavpack_tag(wpc, "replaygain_track_gain");
|
|
|
|
if (value) {
|
|
|
|
replayGainInfo->trackGain = atof(value);
|
|
|
|
free(value);
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = wavpack_tag(wpc, "replaygain_album_gain");
|
|
|
|
if (value) {
|
|
|
|
replayGainInfo->albumGain = atof(value);
|
|
|
|
free(value);
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = wavpack_tag(wpc, "replaygain_track_peak");
|
|
|
|
if (value) {
|
|
|
|
replayGainInfo->trackPeak = atof(value);
|
|
|
|
free(value);
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = wavpack_tag(wpc, "replaygain_album_peak");
|
|
|
|
if (value) {
|
|
|
|
replayGainInfo->albumPeak = atof(value);
|
|
|
|
free(value);
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
return replayGainInfo;
|
|
|
|
|
|
|
|
freeReplayGainInfo(replayGainInfo);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
/*
|
|
|
|
* Reads metainfo from the specified file.
|
|
|
|
*/
|
|
|
|
static MpdTag *wavpack_tagdup(char *fname)
|
|
|
|
{
|
|
|
|
WavpackContext *wpc;
|
|
|
|
MpdTag *tag;
|
|
|
|
char error[ERRORLEN];
|
|
|
|
char *s;
|
|
|
|
int ssize;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
wpc = WavpackOpenFileInput(fname, error, OPEN_TAGS, 0);
|
|
|
|
if (wpc == NULL) {
|
|
|
|
ERROR("failed to open WavPack file \"%s\": %s\n", fname, error);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = newMpdTag();
|
|
|
|
if (tag == NULL) {
|
|
|
|
ERROR("failed to newMpdTag()\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag->time =
|
|
|
|
(float)WavpackGetNumSamples(wpc) / WavpackGetSampleRate(wpc);
|
|
|
|
|
|
|
|
ssize = 0;
|
|
|
|
s = NULL;
|
|
|
|
|
|
|
|
for (i = 0; tagtypes[i].name != NULL; ++i) {
|
|
|
|
j = WavpackGetTagItem(wpc, tagtypes[i].name, NULL, 0);
|
|
|
|
if (j > 0) {
|
|
|
|
++j;
|
|
|
|
|
|
|
|
if (s == NULL) {
|
|
|
|
s = xmalloc(j);
|
|
|
|
if (s == NULL) break;
|
|
|
|
ssize = j;
|
|
|
|
} else if (j > ssize) {
|
|
|
|
char *t = (char *)xrealloc(s, j);
|
|
|
|
if (t == NULL) break;
|
|
|
|
ssize = j;
|
|
|
|
s = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
WavpackGetTagItem(wpc, tagtypes[i].name, s, j);
|
|
|
|
addItemToMpdTag(tag, tagtypes[i].type, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s != NULL)
|
|
|
|
free(s);
|
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mpd InputStream <=> WavpackStreamReader wrapper callbacks
|
|
|
|
*/
|
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
/* This struct is needed for per-stream last_byte storage. */
|
|
|
|
typedef struct {
|
|
|
|
InputStream *is;
|
|
|
|
/* Needed for push_back_byte() */
|
|
|
|
int last_byte;
|
|
|
|
} InputStreamPlus;
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
static int32_t read_bytes(void *id, void *data, int32_t bcount)
|
|
|
|
{
|
2008-03-26 11:38:30 +01:00
|
|
|
InputStreamPlus *isp = (InputStreamPlus *)id;
|
2007-06-24 22:40:04 +02:00
|
|
|
uint8_t *buf = (uint8_t *)data;
|
|
|
|
int32_t i = 0;
|
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
if (isp->last_byte != EOF) {
|
|
|
|
*buf++ = isp->last_byte;
|
|
|
|
isp->last_byte = EOF;
|
2007-06-24 22:40:04 +02:00
|
|
|
--bcount;
|
|
|
|
++i;
|
|
|
|
}
|
2008-03-26 11:38:30 +01:00
|
|
|
return i + readFromInputStream(isp->is, buf, 1, bcount);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t get_pos(void *id)
|
|
|
|
{
|
2008-03-26 11:38:30 +01:00
|
|
|
return ((InputStreamPlus *)id)->is->offset;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int set_pos_abs(void *id, uint32_t pos)
|
|
|
|
{
|
2008-03-26 11:38:30 +01:00
|
|
|
return seekInputStream(((InputStreamPlus *)id)->is, pos, SEEK_SET);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int set_pos_rel(void *id, int32_t delta, int mode)
|
|
|
|
{
|
2008-03-26 11:38:30 +01:00
|
|
|
return seekInputStream(((InputStreamPlus *)id)->is, delta, mode);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int push_back_byte(void *id, int c)
|
|
|
|
{
|
2008-03-26 11:38:30 +01:00
|
|
|
((InputStreamPlus *)id)->last_byte = c;
|
2007-06-24 22:40:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t get_length(void *id)
|
|
|
|
{
|
2008-03-26 11:38:30 +01:00
|
|
|
return ((InputStreamPlus *)id)->is->size;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int can_seek(void *id)
|
|
|
|
{
|
2008-03-26 11:38:30 +01:00
|
|
|
return ((InputStreamPlus *)id)->is->seekable;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static WavpackStreamReader mpd_is_reader = {
|
|
|
|
.read_bytes = read_bytes,
|
|
|
|
.get_pos = get_pos,
|
|
|
|
.set_pos_abs = set_pos_abs,
|
|
|
|
.set_pos_rel = set_pos_rel,
|
|
|
|
.push_back_byte = push_back_byte,
|
|
|
|
.get_length = get_length,
|
|
|
|
.can_seek = can_seek,
|
|
|
|
.write_bytes = NULL /* no need to write edited tags */
|
|
|
|
};
|
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
static void
|
|
|
|
initInputStreamPlus(InputStreamPlus *isp, InputStream *is)
|
|
|
|
{
|
|
|
|
isp->is = is;
|
|
|
|
isp->last_byte = EOF;
|
|
|
|
}
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
/*
|
|
|
|
* Tries to decode the specified stream, and gives true if managed to do it.
|
|
|
|
*/
|
|
|
|
static unsigned int wavpack_trydecode(InputStream *is)
|
|
|
|
{
|
|
|
|
char error[ERRORLEN];
|
|
|
|
WavpackContext *wpc;
|
2008-03-26 11:38:30 +01:00
|
|
|
InputStreamPlus isp;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
initInputStreamPlus(&isp, is);
|
|
|
|
wpc = WavpackOpenFileInputEx(&mpd_is_reader, &isp, NULL, error,
|
2007-06-24 22:40:04 +02:00
|
|
|
OPEN_STREAMING, 0);
|
|
|
|
if (wpc == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
|
|
|
/* Seek it back in order to play from the first byte. */
|
|
|
|
seekInputStream(is, 0, SEEK_SET);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decodes a stream.
|
|
|
|
*/
|
|
|
|
static int wavpack_streamdecode(OutputBuffer *cb, DecoderControl *dc,
|
|
|
|
InputStream *is)
|
|
|
|
{
|
|
|
|
char error[ERRORLEN];
|
|
|
|
WavpackContext *wpc;
|
2008-03-26 11:38:30 +01:00
|
|
|
InputStream is_wvc;
|
|
|
|
int open_flags = OPEN_2CH_MAX | OPEN_NORMALIZE /*| OPEN_STREAMING*/;
|
|
|
|
char *wvc_url = NULL;
|
|
|
|
int err;
|
|
|
|
InputStreamPlus isp, isp_wvc;
|
|
|
|
int canseek;
|
|
|
|
|
|
|
|
/* Try to find wvc */
|
|
|
|
do {
|
2008-04-12 06:15:16 +02:00
|
|
|
char tmp[MPD_PATH_MAX];
|
|
|
|
const char *utf8url;
|
2008-03-26 11:38:30 +01:00
|
|
|
size_t len;
|
|
|
|
err = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As we use dc->utf8url, this function will be bad for
|
|
|
|
* single files. utf8url is not absolute file path :/
|
|
|
|
*/
|
2008-04-12 06:15:16 +02:00
|
|
|
utf8url = get_song_url(tmp, getPlayerData()->playerControl.current_song);
|
|
|
|
if (utf8url == NULL) {
|
2008-03-26 11:38:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-04-12 06:15:16 +02:00
|
|
|
len = strlen(utf8url);
|
2008-03-26 11:38:30 +01:00
|
|
|
if (!len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-26 11:38:34 +01:00
|
|
|
wvc_url = (char *)xmalloc(len + 2); /* +2: 'c' and EOS */
|
2008-03-26 11:38:30 +01:00
|
|
|
if (wvc_url == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:15:16 +02:00
|
|
|
memcpy(wvc_url, utf8url, len);
|
2008-03-26 11:38:30 +01:00
|
|
|
wvc_url[len] = 'c';
|
|
|
|
wvc_url[len + 1] = '\0';
|
|
|
|
|
|
|
|
if (openInputStream(&is_wvc, wvc_url)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And we try to buffer in order to get know
|
|
|
|
* about a possible 404 error.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
if (inputStreamAtEOF(&is_wvc)) {
|
|
|
|
/*
|
|
|
|
* EOF is reached even without
|
|
|
|
* a single byte is read...
|
|
|
|
* So, this is not good :/
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bufferInputStream(&is_wvc) >= 0) {
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dc->stop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save some CPU */
|
|
|
|
my_usleep(1000);
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
closeInputStream(&is_wvc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
open_flags |= OPEN_WVC;
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
canseek = can_seek(&isp);
|
|
|
|
if (wvc_url != NULL) {
|
|
|
|
if (err) {
|
|
|
|
free(wvc_url);
|
|
|
|
wvc_url = NULL;
|
|
|
|
} else {
|
|
|
|
initInputStreamPlus(&isp_wvc, &is_wvc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initInputStreamPlus(&isp, is);
|
|
|
|
wpc = WavpackOpenFileInputEx(&mpd_is_reader, &isp, &isp_wvc, error,
|
|
|
|
open_flags, 15);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
if (wpc == NULL) {
|
|
|
|
ERROR("failed to open WavPack stream: %s\n", error);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
wavpack_decode(cb, dc, wpc, canseek, NULL);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
2008-03-26 11:38:30 +01:00
|
|
|
if (wvc_url != NULL) {
|
|
|
|
closeInputStream(&is_wvc);
|
|
|
|
free(wvc_url);
|
|
|
|
}
|
|
|
|
closeInputStream(is);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-26 11:38:30 +01:00
|
|
|
* Decodes a file.
|
2007-06-24 22:40:04 +02:00
|
|
|
*/
|
|
|
|
static int wavpack_filedecode(OutputBuffer *cb, DecoderControl *dc, char *fname)
|
|
|
|
{
|
|
|
|
char error[ERRORLEN];
|
|
|
|
WavpackContext *wpc;
|
2007-06-25 15:37:21 +02:00
|
|
|
ReplayGainInfo *replayGainInfo;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
wpc = WavpackOpenFileInput(fname, error,
|
2007-06-25 16:24:30 +02:00
|
|
|
OPEN_TAGS | OPEN_WVC |
|
|
|
|
OPEN_2CH_MAX | OPEN_NORMALIZE, 15);
|
2007-06-24 22:40:04 +02:00
|
|
|
if (wpc == NULL) {
|
|
|
|
ERROR("failed to open WavPack file \"%s\": %s\n", fname, error);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-06-25 16:24:30 +02:00
|
|
|
replayGainInfo = wavpack_replaygain(wpc);
|
2007-06-25 15:37:21 +02:00
|
|
|
|
|
|
|
wavpack_decode(cb, dc, wpc, 1, replayGainInfo);
|
|
|
|
|
|
|
|
if (replayGainInfo)
|
|
|
|
freeReplayGainInfo(replayGainInfo);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-26 11:38:30 +01:00
|
|
|
static char const *wavpackSuffixes[] = { "wv", NULL };
|
|
|
|
static char const *wavpackMimeTypes[] = { "audio/x-wavpack", NULL };
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
InputPlugin wavpackPlugin = {
|
|
|
|
"wavpack",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
wavpack_trydecode,
|
|
|
|
wavpack_streamdecode,
|
2008-03-26 11:38:30 +01:00
|
|
|
wavpack_filedecode,
|
2007-06-24 22:40:04 +02:00
|
|
|
wavpack_tagdup,
|
|
|
|
INPUT_PLUGIN_STREAM_FILE | INPUT_PLUGIN_STREAM_URL,
|
|
|
|
wavpackSuffixes,
|
|
|
|
wavpackMimeTypes
|
|
|
|
};
|
|
|
|
|
|
|
|
#else /* !HAVE_WAVPACK */
|
|
|
|
|
|
|
|
InputPlugin wavpackPlugin;
|
|
|
|
|
|
|
|
#endif /* !HAVE_WAVPACK */
|