summaryrefslogtreecommitdiffstats
path: root/src/decoders/qalflacaudiodecoder.cpp
blob: 8e94a5773de3f8c9a0b0d0bb3273dc8e166802f7 (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
/******************************************************************************
 * This file is part of the QtOpenAL project
 * Copyright (c) 2011 Laszlo Papp <lpapp@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "qalflacaudiodecoder.h"

#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QUrl>
#include <QtCore/QDebug>

#include <FLAC/stream_decoder.h>

class QALFlacAudioDecoder::Private
{
    public:
        Private()
            : flacStreamDecoder(0)
        {
        }

        ~Private()
        {
        }

        static FLAC__StreamDecoderReadStatus readCallback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
        static FLAC__StreamDecoderSeekStatus seekCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
        static FLAC__StreamDecoderTellStatus tellCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
        static FLAC__StreamDecoderLengthStatus lengthCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
        static FLAC__bool eofCallback(const FLAC__StreamDecoder *decoder, void *client_data);
        static FLAC__StreamDecoderWriteStatus writeCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data);
        static void metadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
        static void errorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);

        QFile file;

        QByteArray encodedData;
        FLAC__StreamDecoder *flacStreamDecoder;
};

FLAC__StreamDecoderReadStatus
QALFlacAudioDecoder::Private::readCallback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
    Q_UNUSED(decoder)

    if (*bytes <= 0)
        return FLAC__STREAM_DECODER_READ_STATUS_ABORT;

    int retval;
    if ((retval = reinterpret_cast<QALFlacAudioDecoder::Private*>(client_data)->file.read(reinterpret_cast<char*>(buffer), *bytes)) == -1) {
        qWarning() << Q_FUNC_INFO << "Failed to read the data from the file";
        return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
    }

    if (retval != 0)
        return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;

    return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
}

FLAC__StreamDecoderSeekStatus
QALFlacAudioDecoder::Private::seekCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
{
    Q_UNUSED(decoder)

    if (reinterpret_cast<QALFlacAudioDecoder::Private*>(client_data)->file.seek(absolute_byte_offset) == false) {
         qWarning() << Q_FUNC_INFO << "Failed to seek in the file";
         return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
    }

    return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}

FLAC__StreamDecoderTellStatus
QALFlacAudioDecoder::Private::tellCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
{
    Q_UNUSED(decoder)

    *absolute_path_offset = reinterpret_cast<QALFlacAudioDecoder::Private*>(client_data)->file.pos();

    return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}

FLAC__StreamDecoderLengthStatus
QALFlacAudioDecoder::Private::lengthCallback(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
{
    Q_UNUSED(decoder)

    *stream_length = reinterpret_cast<QALFlacAudioDecoder::Private*>(client_data)->file.size();

    return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}

QALFlacAudioDecoder::QALFlacAudioDecoder()
    : d(new Private)
{
}

QALFlacAudioDecoder::~QALFlacAudioDecoder()
{
}

bool
QALFlacAudioDecoder::open(const QFile &file)
{
    return open(file.fileName());
}

bool
QALFlacAudioDecoder::open(const QUrl &fileUrl)
{
    return open(fileUrl.toLocalFile());
}

bool
QALFlacAudioDecoder::open(const QString &fileName)
{
    d->file.setFileName(fileName);

    d->flacStreamDecoder = *FLAC__stream_decoder_new();
    if (d->flacStreamDecoder == 0) {
        qWarning() << Q_FUNC_INFO << "Could not allocate enough memory for the flac stream decoder handle";
        return false;
    }

    if (FLAC__stream_decoder_init_stream(d->flacStreamDecoder, &d->readCallback, &d->seekCallback,
                                         d->tellCallback, d->lengthCallback, d->eofCallback, d->writeCallback,
                                         d->metadataCallback, d->errorCallback, d) == FLAC__STREAM_DECODER_INIT_STATUS_OK)
    {   
        if(InitFlac())
        {   
            // all ok
            return;
        }   

        FLAC__stream_decoder_finish(flacFile);
    }   

    if ((d->sndFile = sf_open_virtual(&sfVirtualIO, SFM_READ, &sfInfo, d)) == 0) {
        qWarning() << Q_FUNC_INFO << "Failed to open the file" << fileName.toUtf8().constData() << "for decoding:" << sf_strerror(d->sndFile);
        return false;
    }

    d->sfInfo = sfInfo;

    return true;
}

qint64
QALFlacAudioDecoder::pos()
{
    int position;
    if ((position = sf_seek(d->sndFile, 0, SEEK_CUR)) == -1) {
        qWarning() << Q_FUNC_INFO << "Failed to tell the current position:" << sf_strerror(d->sndFile);
    }

    return position;
}

bool
QALFlacAudioDecoder::seek(qint64 pos)
{
    if (FLAC__stream_decoder_seek_absolute(d->flacStreamDecoder, pos) == false) {
        qWarning() << Q_FUNC_INFO << "Failed to seek in the file";
        return false;
    }

    return true;
}

bool
QALFlacAudioDecoder::close()
{
    FLAC__stream_decoder_delete(d->flacStreamDecoder);
    d->flacStreamDecoder = 0;

    return true;
}

void
QALFlacAudioDecoder::setEncodedData(const QByteArray &encodedData)
{
    d->encodedData = encodedData;
}

int
QALFlacAudioDecoder::channels() const
{
    return FLAC__stream_decoder_get_channels(d->flacStreamDecoder);
}

int
QALFlacAudioDecoder::sampleRate() const
{
    return FLAC__stream_decoder_get_sample_rate(d->flacStreamDecoder);
}

int
QALFlacAudioDecoder::sampleSize() const
{
    return FLAC__stream_decoder_get_bits_per_sample(d->flacStreamDecoder);
}

QByteArray
QALFlacAudioDecoder::decode(qint64 maxlen)
{
    QByteArray result;
    result.reserve(maxlen);

    char *decodedData = result.data();

    if (maxlen != decode(decodedData, maxlen))
        qWarning() << Q_FUNC_INFO << "Could not to decode all the data:" << maxlen;

    return result;
}

qint64
QALFlacAudioDecoder::decode(char *decodedData, qint64 maxlen)
{
    if (FLAC__stream_decoder_get_state(d->flacStreamDecoder) == FLAC__STREAM_DECODER_SEEK_ERROR
        && FLAC__stream_decoder_reset(d->flacStreamDecoder) == false)
        {
            qWarning() << Q_FUNC_INFO << "Failed to allocate memory while resetting before decoding";;
            return false;
        }
    }

    return sf_readf_short(d->sndFile, reinterpret_cast<short*>(decodedData), maxlen);
}