summaryrefslogtreecommitdiff
path: root/src/SFML/Audio/SoundFileReaderFlac.cpp
blob: 646b0312fe3fd12f8f6081ac2636b06fee828bea (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/SoundFileReaderFlac.hpp>
#include <SFML/System/InputStream.hpp>
#include <SFML/System/Err.hpp>
#include <cassert>


namespace
{
    FLAC__StreamDecoderReadStatus streamRead(const FLAC__StreamDecoder*, FLAC__byte buffer[], std::size_t* bytes, void* clientData)
    {
        sf::priv::SoundFileReaderFlac::ClientData* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

        sf::Int64 count = data->stream->read(buffer, *bytes);
        if (count > 0)
        {
            *bytes = static_cast<std::size_t>(count);
            return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
        }
        else if (count == 0)
        {
            return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
        }
        else
        {
            return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
        }
    }

    FLAC__StreamDecoderSeekStatus streamSeek(const FLAC__StreamDecoder*, FLAC__uint64 absoluteByteOffset, void* clientData)
    {
        sf::priv::SoundFileReaderFlac::ClientData* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

        sf::Int64 position = data->stream->seek(absoluteByteOffset);
        if (position >= 0)
            return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
        else
            return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
    }

    FLAC__StreamDecoderTellStatus streamTell(const FLAC__StreamDecoder*, FLAC__uint64* absoluteByteOffset, void* clientData)
    {
        sf::priv::SoundFileReaderFlac::ClientData* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

        sf::Int64 position = data->stream->tell();
        if (position >= 0)
        {
            *absoluteByteOffset = position;
            return FLAC__STREAM_DECODER_TELL_STATUS_OK;
        }
        else
        {
            return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
        }
    }

    FLAC__StreamDecoderLengthStatus streamLength(const FLAC__StreamDecoder*, FLAC__uint64* streamLength, void* clientData)
    {
        sf::priv::SoundFileReaderFlac::ClientData* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

        sf::Int64 count = data->stream->getSize();
        if (count >= 0)
        {
            *streamLength = count;
            return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
        }
        else
        {
            return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
        }
    }

    FLAC__bool streamEof(const FLAC__StreamDecoder*, void* clientData)
    {
        sf::priv::SoundFileReaderFlac::ClientData* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

        return data->stream->tell() == data->stream->getSize();
    }

    FLAC__StreamDecoderWriteStatus streamWrite(const FLAC__StreamDecoder*, const FLAC__Frame* frame, const FLAC__int32* const buffer[], void* clientData)
    {
        sf::priv::SoundFileReaderFlac::ClientData* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

        // If there's no output buffer, it means that we are seeking
        if (!data->buffer)
            return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;

        // Reserve memory if we're going to use the leftovers buffer
        unsigned int frameSamples = frame->header.blocksize * frame->header.channels;
        if (data->remaining < frameSamples)
            data->leftovers.reserve(static_cast<std::size_t>(frameSamples - data->remaining));

        // Decode the samples
        for (unsigned i = 0; i < frame->header.blocksize; ++i)
        {
            for (unsigned int j = 0; j < frame->header.channels; ++j)
            {
                // Decode the current sample
                sf::Int16 sample = 0;
                switch (frame->header.bits_per_sample)
                {
                    case 8:
                        sample = buffer[j][i] << 8;
                        break;
                    case 16:
                        sample = buffer[j][i];
                        break;
                    case 24:
                        sample = buffer[j][i] >> 8;
                        break;
                    case 32:
                        sample = buffer[j][i] >> 16;
                        break;
                    default:
                        assert(false);
                        break;
                }

                if (data->remaining > 0)
                {
                    // There's room in the output buffer, copy the sample there
                    *data->buffer++ = sample;
                    data->remaining--;
                }
                else
                {
                    // We have decoded all the requested samples, put the sample in a temporary buffer until next call
                    data->leftovers.push_back(sample);
                }
            }
        }

        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
    }

    void streamMetadata(const FLAC__StreamDecoder*, const FLAC__StreamMetadata* meta, void* clientData)
    {
        sf::priv::SoundFileReaderFlac::ClientData* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);

        if (meta->type == FLAC__METADATA_TYPE_STREAMINFO)
        {
            data->info.sampleCount = meta->data.stream_info.total_samples * meta->data.stream_info.channels;
            data->info.sampleRate = meta->data.stream_info.sample_rate;
            data->info.channelCount = meta->data.stream_info.channels;
        }
    }

    void streamError(const FLAC__StreamDecoder*, FLAC__StreamDecoderErrorStatus, void* clientData)
    {
        sf::priv::SoundFileReaderFlac::ClientData* data = static_cast<sf::priv::SoundFileReaderFlac::ClientData*>(clientData);
        data->error = true;
    }
}

namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
bool SoundFileReaderFlac::check(InputStream& stream)
{
    // Create a decoder
    FLAC__StreamDecoder* decoder = FLAC__stream_decoder_new();
    if (!decoder)
        return false;

    // Initialize the decoder with our callbacks
    ClientData data;
    data.stream = &stream;
    data.error = false;
    FLAC__stream_decoder_init_stream(decoder, &streamRead, &streamSeek, &streamTell, &streamLength, &streamEof, &streamWrite, NULL, &streamError, &data);

    // Read the header
    bool valid = FLAC__stream_decoder_process_until_end_of_metadata(decoder) != 0;

    // Destroy the decoder
    FLAC__stream_decoder_finish(decoder);
    FLAC__stream_decoder_delete(decoder);

    return valid && !data.error;
}


////////////////////////////////////////////////////////////
SoundFileReaderFlac::SoundFileReaderFlac() :
m_decoder(NULL)
{
}


////////////////////////////////////////////////////////////
SoundFileReaderFlac::~SoundFileReaderFlac()
{
    close();
}


////////////////////////////////////////////////////////////
bool SoundFileReaderFlac::open(InputStream& stream, Info& info)
{
    // Create the decoder
    m_decoder = FLAC__stream_decoder_new();
    if (!m_decoder)
    {
        err() << "Failed to open FLAC file (failed to allocate the decoder)" << std::endl;
        return false;
    }

    // Initialize the decoder with our callbacks
    m_clientData.stream = &stream;
    FLAC__stream_decoder_init_stream(m_decoder, &streamRead, &streamSeek, &streamTell, &streamLength, &streamEof, &streamWrite, &streamMetadata, &streamError, &m_clientData);

    // Read the header
    if (!FLAC__stream_decoder_process_until_end_of_metadata(m_decoder))
    {
        close();
        err() << "Failed to open FLAC file (failed to read metadata)" << std::endl;
        return false;
    }

    // Retrieve the sound properties
    info = m_clientData.info; // was filled in the "metadata" callback

    return true;
}


////////////////////////////////////////////////////////////
void SoundFileReaderFlac::seek(Uint64 sampleOffset)
{
    assert(m_decoder);

    // Reset the callback data (the "write" callback will be called)
    m_clientData.buffer = NULL;
    m_clientData.remaining = 0;
    m_clientData.leftovers.clear();

    FLAC__stream_decoder_seek_absolute(m_decoder, sampleOffset);
}


////////////////////////////////////////////////////////////
Uint64 SoundFileReaderFlac::read(Int16* samples, Uint64 maxCount)
{
    assert(m_decoder);

    // If there are leftovers from previous call, use it first
    Uint64 left = m_clientData.leftovers.size();
    if (left > 0)
    {
        if (left > maxCount)
        {
            // There are more leftovers than needed
            std::copy(m_clientData.leftovers.begin(), m_clientData.leftovers.end(), samples);
            std::vector<Int16> leftovers(m_clientData.leftovers.begin() + maxCount, m_clientData.leftovers.end());
            m_clientData.leftovers.swap(leftovers);
            return maxCount;
        }
        else
        {
            // We can use all the leftovers and decode new frames
            std::copy(m_clientData.leftovers.begin(), m_clientData.leftovers.end(), samples);
        }
    }

    // Reset the data that will be used in the callback
    m_clientData.buffer = samples + left;
    m_clientData.remaining = maxCount - left;
    m_clientData.leftovers.clear();

    // Decode frames one by one until we reach the requested sample count, the end of file or an error
    while (m_clientData.remaining > 0)
    {
        // Everything happens in the "write" callback
        // This will break on any fatal error (does not include EOF)
        if (!FLAC__stream_decoder_process_single(m_decoder))
            break;

        // Break on EOF
        if (FLAC__stream_decoder_get_state(m_decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
            break;
    }

    return maxCount - m_clientData.remaining;
}


////////////////////////////////////////////////////////////
void SoundFileReaderFlac::close()
{
    if (m_decoder)
    {
        FLAC__stream_decoder_finish(m_decoder);
        FLAC__stream_decoder_delete(m_decoder);
        m_decoder = NULL;
    }
}

} // namespace priv

} // namespace sf