File: OggDecoder.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (285 lines) | stat: -rw-r--r-- 9,648 bytes parent folder | download
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
/*************************************************************************
        OggDecoder.cpp  -  decoder for Ogg/Vorbis data
                             -------------------
    begin                : Tue Sep 10 2002
    copyright            : (C) 2002 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <math.h>
#include <stdlib.h>
#include <new>

#include <QDate>
#include <QIODevice>
#include <QLatin1Char>

#include <KLocalizedString>

#include "libkwave/Compression.h"
#include "libkwave/MessageBox.h"
#include "libkwave/MultiWriter.h"
#include "libkwave/Sample.h"
#include "libkwave/SampleArray.h"
#include "libkwave/String.h"
#include "libkwave/Utils.h"
#include "libkwave/Writer.h"

#include "OggCodecPlugin.h"
#include "OggDecoder.h"
#include "OggSubDecoder.h"
#include "OpusDecoder.h"
#include "VorbisDecoder.h"

//***************************************************************************
Kwave::OggDecoder::OggDecoder()
    :Kwave::Decoder(), m_sub_decoder(nullptr), m_source(nullptr)
{
#ifdef HAVE_OGG_OPUS
    REGISTER_OGG_OPUS_MIME_TYPES
    REGISTER_COMPRESSION_TYPE_OGG_OPUS
#endif /* HAVE_OGG_OPUS */

#ifdef HAVE_OGG_VORBIS
    REGISTER_OGG_VORBIS_MIME_TYPES
    REGISTER_COMPRESSION_TYPE_OGG_VORBIS
#endif /* HAVE_OGG_VORBIS */

    /* Ogg audio, as per RFC5334 */
    addMimeType("audio/ogg",         i18n("Ogg audio"),        "*.oga");
    addMimeType("application/ogg",   i18n("Ogg audio"),        "*.ogx");

}

//***************************************************************************
Kwave::OggDecoder::~OggDecoder()
{
    if (m_source) close();
}

//***************************************************************************
Kwave::Decoder *Kwave::OggDecoder::instance()
{
    return new(std::nothrow) Kwave::OggDecoder();
}

//***************************************************************************
int Kwave::OggDecoder::parseHeader(QWidget *widget)
{
    // grab some data at the head of the stream.  We want the first page
    // (which is guaranteed to be small and only contain the Vorbis
    // stream initial header) We need the first page to get the stream
    // serialno.

    // submit a 4k block to libvorbis' Ogg layer
    char *buffer = ogg_sync_buffer(&m_oy, 4096);
    Q_ASSERT(buffer);
    if (!buffer) return -1;

    long int bytes = static_cast<long int>(m_source->read(buffer, 4096));
    if ((bytes <= 0) && (!m_source->pos())) {
        Kwave::MessageBox::error(widget, i18n(
            "Ogg bitstream has zero-length."));
        return -1;
    }
    ogg_sync_wrote(&m_oy, bytes);

    // Get the first page.
    if (ogg_sync_pageout(&m_oy, &m_og) != 1) {
        // have we simply run out of data?  If so, we're done.
        if (bytes < 4096) return 0;

        // error case. seems not be Vorbis data
        Kwave::MessageBox::error(widget, i18n(
             "Input does not appear to be an Ogg bitstream."));
        return -1;
    }

    // Get the serial number and set up the rest of decode.
    // serialno first; use it to set up a logical stream
    ogg_stream_init(&m_os, ogg_page_serialno(&m_og));

    // get the first packet
    if (ogg_stream_pagein(&m_os, &m_og) < 0) {
        // error; stream version mismatch perhaps
        Kwave::MessageBox::error(widget, i18n(
            "Error reading first page of the Ogg bitstream data."));
        return -1;
    }

    if ((ogg_stream_packetout(&m_os, &m_op) != 1) || (m_op.bytes < 8)) {
        // no page? must not be vorbis
        Kwave::MessageBox::error(widget, i18n(
            "Error reading initial header packet."));
        return -1;
    }

    // get rid of the previous sub decoder
    if (m_sub_decoder) {
        delete m_sub_decoder;
        m_sub_decoder = nullptr;
    }

    Kwave::FileInfo info(metaData());

    // ---------------------------------
    // auto-detect the sub decoder
#ifdef HAVE_OGG_OPUS
    if (memcmp(m_op.packet, "OpusHead", 8) == 0) {
        qDebug("    OggDecoder: detected Opus codec");
        m_sub_decoder = new(std::nothrow)
            Kwave::OpusDecoder(m_source, m_oy, m_os, m_og, m_op);
        info.set(Kwave::INF_MIMETYPE, _("audio/opus"));
    }
#endif /* HAVE_OGG_OPUS */
#ifdef HAVE_OGG_VORBIS
    if (memcmp(m_op.packet + 1, "vorbis", 6) == 0) {
        qDebug("    OggDecoder: detected Vorbis codec");
        m_sub_decoder = new(std::nothrow)
            Kwave::VorbisDecoder(m_source, m_oy, m_os, m_og, m_op);
        info.set(Kwave::INF_MIMETYPE, _("audio/x-vorbis+ogg"));
    }
#endif /* HAVE_OGG_VORBIS */

    if (!m_sub_decoder) {
        qDebug("--- dump of the first 8 bytes of the packet: ---");
        for (unsigned int i = 0; i < 8; i++)
            qDebug("%2u: 0x%02X - '%c'", i, m_op.packet[i], m_op.packet[i]);

        Kwave::MessageBox::error(widget, i18n(
            "Error: Codec not supported"));
        return -1;
    }

    info.setLength(0);         // use streaming
    info.setBits(SAMPLE_BITS); // use Kwave's internal resolution
    if (m_sub_decoder->open(widget, info) < 0)
        return -1;

    metaData().replace(Kwave::MetaDataList(info));
    return 1;
}

//***************************************************************************
bool Kwave::OggDecoder::open(QWidget *widget, QIODevice &src)
{
    metaData().clear();
    Q_ASSERT(!m_source);
    if (m_source) qWarning("OggDecoder::open(), already open !");

    // try to open the source
    if (!src.open(QIODevice::ReadOnly)) {
        qWarning("failed to open source !");
        return false;
    }

    // take over the source
    m_source = &src;

    /********** Decode setup ************/
    qDebug("--- OggDecoder::open() ---");
    ogg_sync_init(&m_oy); // Now we can read pages

    // read the header the first time
    if (parseHeader(widget) < 0)
        return false;

    return true;
}

//***************************************************************************
bool Kwave::OggDecoder::decode(QWidget *widget, Kwave::MultiWriter &dst)
{
    int eos = 0;

    Q_ASSERT(m_source);
    Q_ASSERT(m_sub_decoder);
    if (!m_source || !m_sub_decoder) return false;

    // we repeat if the bitstream is chained
    while (!dst.isCanceled()) {
        // The rest is just a straight decode loop until end of stream
        while (!eos) {
            while (!eos) {
                int result = ogg_sync_pageout(&m_oy, &m_og);
                if (result == 0) break; // need more data
                if (result < 0) {
                    // missing or corrupt data at this page position
                    Kwave::MessageBox::error(widget, i18n(
                        "Corrupt or missing data in bitstream. Continuing."
                        ));
                } else {
                    // can safely ignore errors at this point
                    ogg_stream_pagein(&m_os, &m_og);
                    while (1) {
                        result = ogg_stream_packetout(&m_os, &m_op);

                        if (result == 0) break; // need more data
                        if (result < 0) {
                            // missing or corrupt data at this page position
                            // no reason to complain; already complained above
                        } else {
                            result = m_sub_decoder->decode(dst);
                            if (result < 0)
                                break;

                            // signal the current position
                            emit sourceProcessed(m_source->pos());
                        }
                    }
                    if (ogg_page_eos(&m_og) || dst.isCanceled()) eos = 1;
                }
            }

            if (!eos) {
                char *buffer = ogg_sync_buffer(&m_oy, 4096);
                int bytes = Kwave::toInt(m_source->read(buffer, 4096));
                ogg_sync_wrote(&m_oy, bytes);
                if (!bytes) eos = 1;
            }
        }

        // clean up this logical bitstream; before exit we see if we're
        // followed by another [chained]
        ogg_stream_clear(&m_os);
        m_sub_decoder->reset();

        // parse the next header, maybe we parse a stream or chain...
        if (eos || (parseHeader(widget) < 1)) break;
    }

    // OK, clean up the framer
    ogg_sync_clear(&m_oy);

    // signal the current position
    emit sourceProcessed(m_source->pos());

    Kwave::FileInfo info(metaData());
    m_sub_decoder->close(info);
    metaData().replace(Kwave::MetaDataList(info));

    // return with a valid Signal, even if the user pressed cancel !
    return true;
}

//***************************************************************************
void Kwave::OggDecoder::close()
{
    m_source = nullptr;
    delete m_sub_decoder;
    m_sub_decoder = nullptr;
}

//***************************************************************************
//***************************************************************************