File: k3boggvorbisdecoder.cpp

package info (click to toggle)
k3b 25.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,840 kB
  • sloc: cpp: 99,202; xml: 372; sh: 84; makefile: 10
file content (237 lines) | stat: -rw-r--r-- 6,304 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "k3boggvorbisdecoder.h"
#include "k3bplugin_i18n.h"

#include <config-k3b.h>

#include <QDebug>
#include <QFile>
#include <QStringList>

#include <stdio.h>
#include <stdlib.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>

K_PLUGIN_CLASS_WITH_JSON(K3bOggVorbisDecoderFactory, "k3boggvorbisdecoder.json")

class K3bOggVorbisDecoder::Private
{
public:
    Private()
        : vInfo(0),
          vComment(0),
          isOpen(false) {
    }

    OggVorbis_File oggVorbisFile;
    vorbis_info* vInfo;
    vorbis_comment* vComment;
    bool isOpen;
};


K3bOggVorbisDecoder::K3bOggVorbisDecoder( QObject* parent )
    : K3b::AudioDecoder( parent )
{
    d = new Private();
}


K3bOggVorbisDecoder::~K3bOggVorbisDecoder()
{
    delete d;
}


bool K3bOggVorbisDecoder::openOggVorbisFile()
{
    if( !d->isOpen ) {
        FILE* file = fopen( QFile::encodeName(filename()), "r" );
        if( !file ) {
            qDebug() << "(K3bOggVorbisDecoder) Could not open file " << filename();
            return false;
        }
        else if( ov_open( file, &d->oggVorbisFile, 0, 0 ) ) {
            qDebug() << "(K3bOggVorbisDecoder) " << filename()
                     << " seems not to to be an ogg vorbis file." << Qt::endl;
            fclose( file );
            return false;
        }
    }

    d->isOpen = true;
    return true;
}


bool K3bOggVorbisDecoder::analyseFileInternal( K3b::Msf& frames, int& samplerate, int& ch )
{
    cleanup();

    if( openOggVorbisFile() ) {
        // check length of track
        double seconds = ov_time_total( &d->oggVorbisFile, -1 );
        if( seconds == OV_EINVAL ) {
            qDebug() << "(K3bOggVorbisDecoder) Could not determine length of file " << filename();
            cleanup();
            return false;
        }
        else {

            d->vInfo = ov_info( &d->oggVorbisFile, -1 /* current bitstream */ );
            d->vComment = ov_comment( &d->oggVorbisFile, -1 );

            // add meta tags
            for( int i = 0; i < d->vComment->comments; ++i ) {
                QString comment = QString::fromUtf8( d->vComment->user_comments[i] );
                QStringList values = comment.split( '=' );
                if( values.count() > 1 ) {
                    if( values[0].toLower() == "title" )
                        addMetaInfo( META_TITLE, values[1] );
                    else if( values[0].toLower() == "artist" )
                        addMetaInfo( META_ARTIST, values[1] );
                    else if( values[0].toLower() == "description" )
                        addMetaInfo( META_COMMENT, values[1] );
                }
            }


            // add technical infos
            addTechnicalInfo( i18n("Version"), QString::number(d->vInfo->version) );
            addTechnicalInfo( i18n("Channels"), QString::number(d->vInfo->channels) );
            addTechnicalInfo( i18n("Sampling Rate"), i18n("%1 Hz",d->vInfo->rate) );
            if( d->vInfo->bitrate_upper > 0 )
                addTechnicalInfo( i18n("Bitrate Upper"), i18n( "%1 bps" ,d->vInfo->bitrate_upper) );
            if( d->vInfo->bitrate_nominal > 0 )
                addTechnicalInfo( i18n("Bitrate Nominal"), i18n( "%1 bps" ,d->vInfo->bitrate_nominal) );
            if( d->vInfo->bitrate_lower > 0 )
                addTechnicalInfo( i18n("Bitrate Lower"), i18n( "%1 bps",d->vInfo->bitrate_lower) );

            frames = K3b::Msf::fromSeconds(seconds);
            samplerate = d->vInfo->rate;
            ch = d->vInfo->channels;

            cleanup();

            return true;
        }
    }
    else
        return false;
}


bool K3bOggVorbisDecoder::initDecoderInternal()
{
    cleanup();
    return openOggVorbisFile();
}


int K3bOggVorbisDecoder::decodeInternal( char* data, int maxLen )
{
    int bitStream = 0;
    long bytesRead = ov_read( &d->oggVorbisFile,
                              data,
                              maxLen,  // max length to be read
                              1,                   // big endian
                              2,                   // word size: 16-bit samples
                              1,                   // signed
                              &bitStream );        // current bitstream

    if( bitStream != 0 ) {
        qDebug() << "(K3bOggVorbisDecoder) bitstream != 0. Multiple bitstreams not supported.";
        return -1;
    }

    else if( bytesRead == OV_HOLE ) {
        qDebug() << "(K3bOggVorbisDecoder) OV_HOLE";
        // recursive new try
        return decodeInternal( data, maxLen );
    }

    else if( bytesRead < 0 ) {
        qDebug() << "(K3bOggVorbisDecoder) Error: " << bytesRead;
        return -1;
    }

    else if( bytesRead == 0 ) {
        qDebug() << "(K3bOggVorbisDecoder) successfully finished decoding.";
        return 0;
    }

    else {
        return bytesRead;
    }
}


void K3bOggVorbisDecoder::cleanup()
{
    if( d->isOpen )
        ov_clear( &d->oggVorbisFile );
    d->isOpen = false;
    d->vComment = 0;
    d->vInfo = 0;
}


bool K3bOggVorbisDecoder::seekInternal( const K3b::Msf& pos )
{
    return ( ov_pcm_seek( &d->oggVorbisFile, pos.pcmSamples() ) == 0 );
}


QString K3bOggVorbisDecoder::fileType() const
{
    return i18n("Ogg-Vorbis");
}


K3bOggVorbisDecoderFactory::K3bOggVorbisDecoderFactory( QObject* parent, const QVariantList& )
    : K3b::AudioDecoderFactory( parent )
{
}


K3bOggVorbisDecoderFactory::~K3bOggVorbisDecoderFactory()
{
}


K3b::AudioDecoder* K3bOggVorbisDecoderFactory::createDecoder( QObject* parent ) const
{
    return new K3bOggVorbisDecoder( parent );
}


bool K3bOggVorbisDecoderFactory::canDecode( const QUrl& url )
{
    FILE* file = fopen( QFile::encodeName(url.toLocalFile()), "r" );
    if( !file ) {
        qDebug() << "(K3bOggVorbisDecoder) Could not open file " << url.toLocalFile();
        return false;
    }

    OggVorbis_File of;

    if( ov_open( file, &of, 0, 0 ) ) {
        fclose( file );
        qDebug() << "(K3bOggVorbisDecoder) not an Ogg-Vorbis file: " << url.toLocalFile();
        return false;
    }

    ov_clear( &of );

    return true;
}


#include "k3boggvorbisdecoder.moc"

#include "moc_k3boggvorbisdecoder.cpp"