summaryrefslogtreecommitdiff
path: root/src/wavpack/wavpack.cc
blob: 4e35df571198d0dd1755029ffa7f6e40586c1164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdlib.h>
#include <string.h>

#include <wavpack/wavpack.h>

#define WANT_VFS_STDIO_COMPAT
#include <audacious/audtag.h>
#include <libaudcore/runtime.h>
#include <libaudcore/i18n.h>
#include <libaudcore/plugin.h>
#include <libaudcore/audstrings.h>

#define BUFFER_SIZE 256 /* read buffer size, in samples / frames */
#define SAMPLE_SIZE(a) (a == 8 ? sizeof(uint8_t) : (a == 16 ? sizeof(uint16_t) : sizeof(uint32_t)))
#define SAMPLE_FMT(a) (a == 8 ? FMT_S8 : (a == 16 ? FMT_S16_NE : (a == 24 ? FMT_S24_NE : FMT_S32_NE)))

class WavpackPlugin : public InputPlugin
{
public:
    static const char about[];
    static const char * const exts[];

    static constexpr PluginInfo info = {
        N_("WavPack Decoder"),
        PACKAGE,
        about
    };

    static constexpr auto iinfo = InputInfo (FlagWritesTag)
        .with_exts (exts);

    constexpr WavpackPlugin() : InputPlugin (info, iinfo) {}

    bool is_our_file (const char * filename, VFSFile & file)
        { return false; }

    Tuple read_tuple (const char * filename, VFSFile & file);
    bool write_tuple (const char * filename, VFSFile & file, const Tuple & tuple);
    bool play (const char * filename, VFSFile & file);
};

EXPORT WavpackPlugin aud_plugin_instance;

/* Audacious VFS wrappers for Wavpack stream reading
 */

static int32_t
wv_read_bytes(void *id, void *data, int32_t bcount)
{
    return ((VFSFile *) id)->fread (data, 1, bcount);
}

static uint32_t
wv_get_pos(void *id)
{
    return aud::clamp (((VFSFile *) id)->ftell (), (int64_t) 0, (int64_t) 0xffffffff);
}

static int
wv_set_pos_abs(void *id, uint32_t pos)
{
    return ((VFSFile *) id)->fseek (pos, VFS_SEEK_SET);
}

static int
wv_set_pos_rel(void *id, int32_t delta, int mode)
{
    return ((VFSFile *) id)->fseek (delta, to_vfs_seek_type(mode));
}

static int
wv_push_back_byte(void *id, int c)
{
    return (((VFSFile *) id)->fseek (-1, VFS_SEEK_CUR) == 0) ? c : -1;
}

static uint32_t
wv_get_length(void *id)
{
    return aud::clamp (((VFSFile *) id)->fsize (), (int64_t) 0, (int64_t) 0xffffffff);
}

static int wv_can_seek(void *id)
{
    return (((VFSFile *) id)->fsize () >= 0);
}

static int32_t wv_write_bytes(void *id, void *data, int32_t bcount)
{
    return ((VFSFile *) id)->fwrite (data, 1, bcount);
}

WavpackStreamReader wv_readers = {
    wv_read_bytes,
    wv_get_pos,
    wv_set_pos_abs,
    wv_set_pos_rel,
    wv_push_back_byte,
    wv_get_length,
    wv_can_seek,
    wv_write_bytes
};

static bool wv_attach (const char * filename, VFSFile & wv_input,
 VFSFile & wvc_input, WavpackContext * * ctx, char * error, int flags)
{
    if (flags & OPEN_WVC)
    {
        StringBuf corrFilename = str_concat ({filename, "c"});
        if (VFSFile::test_file (corrFilename, VFS_IS_REGULAR))
            wvc_input = VFSFile (corrFilename, "r");
    }

    * ctx = WavpackOpenFileInputEx (& wv_readers, & wv_input, & wvc_input, error, flags, 0);
    return (* ctx != nullptr);
}

static void wv_deattach (WavpackContext * ctx)
{
    WavpackCloseFile(ctx);
}

bool WavpackPlugin::play (const char * filename, VFSFile & file)
{
    int sample_rate, num_channels, bits_per_sample;
    unsigned num_samples;
    WavpackContext *ctx = nullptr;
    VFSFile wvc_input;

    if (! wv_attach (filename, file, wvc_input, & ctx, nullptr, OPEN_TAGS | OPEN_WVC))
    {
        AUDERR ("Error opening Wavpack file '%s'.", filename);
        return false;
    }

    sample_rate = WavpackGetSampleRate(ctx);
    num_channels = WavpackGetNumChannels(ctx);
    bits_per_sample = WavpackGetBitsPerSample(ctx);
    num_samples = WavpackGetNumSamples(ctx);

    set_stream_bitrate(WavpackGetAverageBitrate(ctx, num_channels));
    open_audio(SAMPLE_FMT(bits_per_sample), sample_rate, num_channels);

    Index<int32_t> input;
    input.resize (BUFFER_SIZE * num_channels);

    Index<char> output;
    output.resize (BUFFER_SIZE * num_channels * SAMPLE_SIZE (bits_per_sample));

    while (! check_stop ())
    {
        int seek_value = check_seek ();
        if (seek_value >= 0)
            WavpackSeekSample (ctx, (int64_t) seek_value * sample_rate / 1000);

        /* Decode audio data */
        unsigned samples_left = num_samples - WavpackGetSampleIndex(ctx);

        if (samples_left == 0)
            break;

        int ret = WavpackUnpackSamples (ctx, input.begin (), BUFFER_SIZE);

        if (ret < 0)
        {
            AUDERR ("Error decoding file.\n");
            break;
        }
        else
        {
            /* Perform audio data conversion and output */
            int32_t * rp = input.begin ();
            int8_t * wp = (int8_t *) output.begin ();
            int16_t * wp2 = (int16_t *) output.begin ();
            int32_t * wp4 = (int32_t *) output.begin ();

            if (bits_per_sample == 8)
            {
                for (int i = 0; i < ret * num_channels; i++, wp++, rp++)
                    *wp = *rp & 0xff;
            }
            else if (bits_per_sample == 16)
            {
                for (int i = 0; i < ret * num_channels; i++, wp2++, rp++)
                    *wp2 = *rp & 0xffff;
            }
            else if (bits_per_sample == 24 || bits_per_sample == 32)
            {
                for (int i = 0; i < ret * num_channels; i++, wp4++, rp++)
                    *wp4 = *rp;
            }

            write_audio (output.begin (),
             ret * num_channels * SAMPLE_SIZE (bits_per_sample));
        }
    }

    wv_deattach (ctx);
    return true;
}

static StringBuf
wv_get_quality(WavpackContext *ctx)
{
    int mode = WavpackGetMode(ctx);
    const char *quality;

    if (mode & MODE_LOSSLESS)
        quality = _("lossless");
    else if (mode & MODE_HYBRID)
        quality = _("lossy (hybrid)");
    else
        quality = _("lossy");

    return str_concat ({quality, (mode & MODE_WVC) ? " (wvc corrected)" : "",
     (mode & MODE_DNS) ? " (dynamic noise shaped)" : ""});
}

Tuple WavpackPlugin::read_tuple (const char * filename, VFSFile & file)
{
    WavpackContext *ctx;
    Tuple tuple;
    char error[1024];

    ctx = WavpackOpenFileInputEx(&wv_readers, &file, nullptr, error, OPEN_TAGS, 0);

    if (ctx == nullptr)
        return tuple;

    AUDDBG("starting probe of %s\n", file.filename ());

    tuple.set_filename (filename);

    tuple.set_int (Tuple::Length,
        ((uint64_t) WavpackGetNumSamples(ctx) * 1000) / (uint64_t) WavpackGetSampleRate(ctx));
    tuple.set_str (Tuple::Codec, "WavPack");

    tuple.set_str (Tuple::Quality, wv_get_quality (ctx));

    WavpackCloseFile(ctx);

    if (! file.fseek (0, VFS_SEEK_SET))
        audtag::read_tag (file, & tuple, nullptr);

    AUDDBG("returning tuple for file %s\n", file.filename ());
    return tuple;
}

bool WavpackPlugin::write_tuple (const char * filename, VFSFile & handle, const Tuple & tuple)
{
    return audtag::tuple_write(tuple, handle, audtag::TagType::APE);
}

const char WavpackPlugin::about[] =
 N_("Copyright 2006 William Pitcock <nenolod@nenolod.net>\n\n"
    "Some of the plugin code was by Miles Egan.");

const char * const WavpackPlugin::exts[] = { "wv", nullptr };