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"
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
#include <wavpack/wavpack.h>
|
2008-11-04 17:10:10 +01:00
|
|
|
#include <glib.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
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function has been borrowed from the tiny player found on
|
|
|
|
* wavpack.com. Modifications were required because mpd only handles
|
|
|
|
* max 16 bit samples.
|
|
|
|
*/
|
2008-11-04 17:08:59 +01:00
|
|
|
static void
|
|
|
|
format_samples_int(int bytes_per_sample, void *buffer, uint32_t samcnt)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
int32_t temp;
|
|
|
|
uchar *dst = (uchar *)buffer;
|
|
|
|
int32_t *src = (int32_t *)buffer;
|
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
switch (bytes_per_sample) {
|
2008-11-04 17:08:57 +01:00
|
|
|
case 1:
|
|
|
|
while (samcnt--)
|
|
|
|
*dst++ = *src++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
while (samcnt--) {
|
|
|
|
temp = *src++;
|
2007-10-03 18:11:01 +02:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2008-11-04 17:08:57 +01:00
|
|
|
*dst++ = (uchar)(temp >> 8);
|
|
|
|
*dst++ = (uchar)(temp);
|
2007-10-03 18:11:01 +02:00
|
|
|
#else
|
2008-11-04 17:08:57 +01:00
|
|
|
*dst++ = (uchar)(temp);
|
|
|
|
*dst++ = (uchar)(temp >> 8);
|
2007-10-03 18:11:01 +02:00
|
|
|
#endif
|
2008-11-04 17:08:57 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
/* downscale to 16 bits */
|
|
|
|
while (samcnt--) {
|
|
|
|
temp = *src++;
|
2007-10-03 18:11:01 +02:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2008-11-04 17:08:57 +01:00
|
|
|
*dst++ = (uchar)(temp >> 16);
|
|
|
|
*dst++ = (uchar)(temp >> 8);
|
2007-10-03 18:11:01 +02:00
|
|
|
#else
|
2008-11-04 17:08:57 +01:00
|
|
|
*dst++ = (uchar)(temp >> 8);
|
|
|
|
*dst++ = (uchar)(temp >> 16);
|
2007-10-03 18:11:01 +02:00
|
|
|
#endif
|
2008-11-04 17:08:57 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
/* downscale to 16 bits */
|
|
|
|
while (samcnt--) {
|
|
|
|
temp = *src++;
|
2007-10-03 18:11:01 +02:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2008-11-04 17:08:57 +01:00
|
|
|
*dst++ = (uchar)(temp >> 24);
|
|
|
|
*dst++ = (uchar)(temp >> 16);
|
2007-10-03 18:11:01 +02:00
|
|
|
|
|
|
|
#else
|
2008-11-04 17:08:57 +01:00
|
|
|
*dst++ = (uchar)(temp >> 16);
|
|
|
|
*dst++ = (uchar)(temp >> 24);
|
2007-10-03 18:11:01 +02:00
|
|
|
#endif
|
2008-11-04 17:08:57 +01:00
|
|
|
}
|
|
|
|
break;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function converts floating point sample data to 16 bit integer.
|
|
|
|
*/
|
2008-11-08 13:07:09 +01:00
|
|
|
static void
|
|
|
|
format_samples_float(mpd_unused int bytes_per_sample, void *buffer,
|
|
|
|
uint32_t samcnt)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2008-11-08 13:07:09 +01:00
|
|
|
static void
|
|
|
|
wavpack_decode(struct decoder * decoder, WavpackContext *wpc, bool canseek,
|
|
|
|
ReplayGainInfo *replayGainInfo)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
2008-09-07 19:19:55 +02:00
|
|
|
struct audio_format audio_format;
|
2008-11-04 17:08:59 +01:00
|
|
|
void (*format_samples)(int bytes_per_sample,
|
|
|
|
void *buffer, uint32_t samcnt);
|
2007-06-24 22:40:04 +02:00
|
|
|
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;
|
2008-11-04 17:08:59 +01:00
|
|
|
int bytes_per_sample;
|
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-08-26 08:27:05 +02:00
|
|
|
if (audio_format.bits > 16)
|
|
|
|
audio_format.bits = 16;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
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);
|
2008-11-04 17:08:59 +01:00
|
|
|
bytes_per_sample = WavpackGetBytesPerSample(wpc);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
outsamplesize = bytes_per_sample;
|
2007-06-24 22:40:04 +02:00
|
|
|
if (outsamplesize > 2)
|
|
|
|
outsamplesize = 2;
|
2008-08-26 08:27:05 +02:00
|
|
|
outsamplesize *= audio_format.channels;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-08-26 08:27:05 +02:00
|
|
|
samplesreq = sizeof(chunk) / (4 * audio_format.channels);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-06 06:48:33 +01:00
|
|
|
decoder_initialized(decoder, &audio_format, canseek,
|
2008-10-10 14:40:54 +02:00
|
|
|
(float)allsamples / audio_format.sample_rate);
|
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) {
|
2007-06-24 22:40:04 +02:00
|
|
|
if (canseek) {
|
|
|
|
int where;
|
|
|
|
|
2008-08-26 08:27:07 +02:00
|
|
|
where = decoder_seek_where(decoder) *
|
2008-10-10 14:40:54 +02:00
|
|
|
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);
|
|
|
|
} else
|
|
|
|
decoder_seek_error(decoder);
|
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-08-26 08:27:07 +02:00
|
|
|
if (decoder_get_command(decoder) == DECODE_COMMAND_STOP)
|
2007-06-24 22:40:04 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
samplesgot = WavpackUnpackSamples(wpc,
|
|
|
|
(int32_t *)chunk, samplesreq);
|
|
|
|
if (samplesgot > 0) {
|
|
|
|
int bitrate = (int)(WavpackGetInstantBitrate(wpc) /
|
|
|
|
1000 + 0.5);
|
|
|
|
position += samplesgot;
|
2008-10-10 14:40:54 +02:00
|
|
|
file_time = (float)position / audio_format.sample_rate;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
format_samples(bytes_per_sample, chunk,
|
2008-08-26 08:27:05 +02:00
|
|
|
samplesgot * audio_format.channels);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-02 17:01:51 +01:00
|
|
|
decoder_data(decoder, NULL, chunk,
|
2008-08-26 08:27:05 +02:00
|
|
|
samplesgot * outsamplesize,
|
|
|
|
file_time, bitrate,
|
|
|
|
replayGainInfo);
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
} while (samplesgot == samplesreq);
|
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static char *
|
|
|
|
wavpack_tag(WavpackContext *wpc, char *key)
|
2007-06-25 15:37:21 +02:00
|
|
|
{
|
|
|
|
char *value = NULL;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
size = WavpackGetTagItem(wpc, key, NULL, 0);
|
|
|
|
if (size > 0) {
|
|
|
|
size++;
|
2008-11-04 17:10:10 +01:00
|
|
|
value = g_malloc(size);
|
2007-06-25 15:37:21 +02:00
|
|
|
WavpackGetTagItem(wpc, key, value, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2008-11-08 13:07:09 +01:00
|
|
|
static ReplayGainInfo *
|
|
|
|
wavpack_replaygain(WavpackContext *wpc)
|
2007-06-25 15:37:21 +02:00
|
|
|
{
|
2008-09-07 19:14:39 +02:00
|
|
|
static char replaygain_track_gain[] = "replaygain_track_gain";
|
|
|
|
static char replaygain_album_gain[] = "replaygain_album_gain";
|
|
|
|
static char replaygain_track_peak[] = "replaygain_track_peak";
|
|
|
|
static char replaygain_album_peak[] = "replaygain_album_peak";
|
2008-11-04 17:08:59 +01:00
|
|
|
ReplayGainInfo *replay_gain_info;
|
2008-10-30 08:38:54 +01:00
|
|
|
bool found = false;
|
2007-06-25 15:37:21 +02:00
|
|
|
char *value;
|
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
replay_gain_info = newReplayGainInfo();
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-09-07 19:14:39 +02:00
|
|
|
value = wavpack_tag(wpc, replaygain_track_gain);
|
2007-06-25 15:37:21 +02:00
|
|
|
if (value) {
|
2008-11-04 17:08:59 +01:00
|
|
|
replay_gain_info->trackGain = atof(value);
|
2007-06-25 15:37:21 +02:00
|
|
|
free(value);
|
2008-10-30 08:38:54 +01:00
|
|
|
found = true;
|
2007-06-25 15:37:21 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 19:14:39 +02:00
|
|
|
value = wavpack_tag(wpc, replaygain_album_gain);
|
2007-06-25 15:37:21 +02:00
|
|
|
if (value) {
|
2008-11-04 17:08:59 +01:00
|
|
|
replay_gain_info->albumGain = atof(value);
|
2007-06-25 15:37:21 +02:00
|
|
|
free(value);
|
2008-10-30 08:38:54 +01:00
|
|
|
found = true;
|
2007-06-25 15:37:21 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 19:14:39 +02:00
|
|
|
value = wavpack_tag(wpc, replaygain_track_peak);
|
2007-06-25 15:37:21 +02:00
|
|
|
if (value) {
|
2008-11-04 17:08:59 +01:00
|
|
|
replay_gain_info->trackPeak = atof(value);
|
2007-06-25 15:37:21 +02:00
|
|
|
free(value);
|
2008-10-30 08:38:54 +01:00
|
|
|
found = true;
|
2007-06-25 15:37:21 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 19:14:39 +02:00
|
|
|
value = wavpack_tag(wpc, replaygain_album_peak);
|
2007-06-25 15:37:21 +02:00
|
|
|
if (value) {
|
2008-11-04 17:08:59 +01:00
|
|
|
replay_gain_info->albumPeak = atof(value);
|
2007-06-25 15:37:21 +02:00
|
|
|
free(value);
|
2008-10-30 08:38:54 +01:00
|
|
|
found = true;
|
2007-06-25 15:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (found)
|
2008-11-04 17:08:59 +01:00
|
|
|
return replay_gain_info;
|
2007-06-25 15:37:21 +02:00
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
freeReplayGainInfo(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;
|
|
|
|
int ssize;
|
2008-11-04 17:10:17 +01:00
|
|
|
int j;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
wpc = WavpackOpenFileInput(fname, error, OPEN_TAGS, 0);
|
|
|
|
if (wpc == NULL) {
|
2008-11-04 17:10:10 +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();
|
2007-06-24 22:40:04 +02:00
|
|
|
tag->time =
|
|
|
|
(float)WavpackGetNumSamples(wpc) / WavpackGetSampleRate(wpc);
|
|
|
|
|
|
|
|
ssize = 0;
|
|
|
|
s = NULL;
|
|
|
|
|
2008-11-04 17:10:17 +01:00
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(tagtypes); ++i) {
|
2007-06-24 22:40:04 +02:00
|
|
|
j = WavpackGetTagItem(wpc, tagtypes[i].name, NULL, 0);
|
|
|
|
if (j > 0) {
|
|
|
|
++j;
|
|
|
|
|
|
|
|
if (s == NULL) {
|
2008-11-04 17:10:10 +01:00
|
|
|
s = g_malloc(j);
|
2007-06-24 22:40:04 +02:00
|
|
|
ssize = j;
|
|
|
|
} else if (j > ssize) {
|
2008-11-04 17:10:10 +01:00
|
|
|
char *t = (char *)g_realloc(s, j);
|
2007-06-24 22:40:04 +02:00
|
|
|
ssize = j;
|
|
|
|
s = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
WavpackGetTagItem(wpc, tagtypes[i].name, s, j);
|
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: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
|
|
|
{
|
2008-11-04 17:08:59 +01:00
|
|
|
struct wavpack_input *isp = (struct wavpack_input *)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-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) {
|
|
|
|
size_t nbytes = decoder_read(isp->decoder, isp->is,
|
|
|
|
buf, bcount);
|
|
|
|
if (nbytes == 0)
|
|
|
|
/* EOF, error or a decoder command */
|
|
|
|
break;
|
|
|
|
|
|
|
|
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-04 17:08:59 +01:00
|
|
|
return ((struct wavpack_input *)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-04 17:08:59 +01:00
|
|
|
return input_stream_seek(((struct wavpack_input *)id)->is, pos, SEEK_SET)
|
2008-10-26 20:56:46 +01:00
|
|
|
? 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-04 17:08:59 +01:00
|
|
|
return input_stream_seek(((struct wavpack_input *)id)->is, delta, mode)
|
2008-10-26 20:56:46 +01:00
|
|
|
? 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-04 17:08:59 +01:00
|
|
|
((struct wavpack_input *)id)->last_byte = c;
|
2007-06-24 22:40:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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-04 17:08:59 +01:00
|
|
|
return ((struct wavpack_input *)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-04 17:08:59 +01:00
|
|
|
return ((struct wavpack_input *)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;
|
|
|
|
}
|
|
|
|
|
2007-06-24 22:40:04 +02:00
|
|
|
/*
|
|
|
|
* Tries to decode the specified stream, and gives true if managed to do it.
|
|
|
|
*/
|
2008-11-08 13:07:09 +01:00
|
|
|
static bool
|
|
|
|
wavpack_trydecode(struct input_stream *is)
|
2007-06-24 22:40:04 +02:00
|
|
|
{
|
|
|
|
char error[ERRORLEN];
|
|
|
|
WavpackContext *wpc;
|
2008-11-04 17:08:59 +01:00
|
|
|
struct wavpack_input isp;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
wavpack_input_init(&isp, NULL, is);
|
2008-03-26 11:38:30 +01:00
|
|
|
wpc = WavpackOpenFileInputEx(&mpd_is_reader, &isp, NULL, error,
|
2007-06-24 22:40:04 +02:00
|
|
|
OPEN_STREAMING, 0);
|
|
|
|
if (wpc == NULL)
|
2008-10-08 11:03:39 +02:00
|
|
|
return false;
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
|
|
|
|
2008-10-08 11:03:39 +02:00
|
|
|
return true;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (utf8url == NULL)
|
2008-11-04 17:10:01 +01:00
|
|
|
return false;
|
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-10-26 20:56:46 +01:00
|
|
|
if (!ret)
|
2008-11-04 17:10:01 +01:00
|
|
|
return false;
|
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-04 17:10:03 +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-10-30 08:38:54 +01:00
|
|
|
static bool
|
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-08-26 08:27:14 +02:00
|
|
|
int open_flags = OPEN_2CH_MAX | OPEN_NORMALIZE /*| OPEN_STREAMING*/;
|
2008-11-04 17:08:59 +01:00
|
|
|
struct wavpack_input isp, isp_wvc;
|
2008-11-06 06:48:33 +01:00
|
|
|
bool canseek = 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-06 06:48:33 +01:00
|
|
|
canseek &= is_wvc.seekable;
|
|
|
|
}
|
2008-03-26 11:38:30 +01:00
|
|
|
|
2008-11-04 17:08:59 +01:00
|
|
|
wavpack_input_init(&isp, decoder, is);
|
2008-03-26 11:38:30 +01:00
|
|
|
wpc = WavpackOpenFileInputEx(&mpd_is_reader, &isp, &isp_wvc, error,
|
|
|
|
open_flags, 15);
|
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-10-30 08:38:54 +01:00
|
|
|
return false;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-06 06:48:33 +01:00
|
|
|
wavpack_decode(decoder, wpc, canseek, NULL);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
2008-08-26 08:27:14 +02:00
|
|
|
if (open_flags & OPEN_WVC)
|
2008-10-26 20:34:47 +01:00
|
|
|
input_stream_close(&is_wvc);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
2008-10-30 08:38:54 +01:00
|
|
|
return true;
|
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-10-30 08:38:54 +01:00
|
|
|
static bool
|
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-04 17:08:59 +01:00
|
|
|
ReplayGainInfo *replay_gain_info;
|
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) {
|
2008-11-04 17:10:10 +01:00
|
|
|
g_warning("failed to open WavPack file \"%s\": %s\n",
|
|
|
|
fname, error);
|
2008-10-30 08:38:54 +01:00
|
|
|
return false;
|
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-04 17:08:59 +01:00
|
|
|
if (replay_gain_info)
|
|
|
|
freeReplayGainInfo(replay_gain_info);
|
2007-06-24 22:40:04 +02:00
|
|
|
|
|
|
|
WavpackCloseFile(wpc);
|
|
|
|
|
2008-10-30 08:38:54 +01:00
|
|
|
return true;
|
2007-06-24 22:40:04 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 17:08:59 +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",
|
|
|
|
.try_decode = wavpack_trydecode,
|
|
|
|
.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
|
|
|
};
|