summaryrefslogtreecommitdiffstats
path: root/plugins/decoder/ogg/k3boggvorbisdecoder.cpp
blob: 717c1a2a8d42c45a17917416664866318df0b0cc (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
/* 
 *
 * $Id$
 * Copyright (C) 2003 Sebastian Trueg <[email protected]>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <[email protected]>
 *
 * 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.
 * See the file "COPYING" for the exact licensing terms.
 */

#include <config.h>

#include "k3boggvorbisdecoder.h"

#include <k3bpluginfactory.h>

#include <tqfile.h>
#include <tqstringlist.h>

#include <kurl.h>
#include <kdebug.h>
#include <klocale.h>

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


K_EXPORT_COMPONENT_FACTORY( libk3boggvorbisdecoder, K3bPluginFactory<K3bOggVorbisDecoderFactory>( "libk3boggvorbisdecoder" ) )


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

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


K3bOggVorbisDecoder::K3bOggVorbisDecoder( TQObject* parent, const char* name )
  : K3bAudioDecoder( parent, name )
{
  d = new Private();
}


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


bool K3bOggVorbisDecoder::openOggVorbisFile()
{
  if( !d->isOpen ) {
    FILE* file = fopen( TQFile::encodeName(filename()), "r" );
    if( !file ) {
      kdDebug() << "(K3bOggVorbisDecoder) Could not open file " << filename() << endl;
      return false;
    }
    else if( ov_open( file, &d->oggVorbisFile, 0, 0 ) ) {
      kdDebug() << "(K3bOggVorbisDecoder) " << filename() 
		<< " seems not to to be an ogg vorbis file." << 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 ) {
      kdDebug() << "(K3bOggVorbisDecoder) Could not determine length of file " << filename() << endl;
      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 ) {
	TQString comment = TQString::fromUtf8( d->vComment->user_comments[i] );
	TQStringList values = TQStringList::split( "=", comment );
	if( values.count() > 1 ) {
	  if( values[0].lower() == "title" )
	    addMetaInfo( META_TITLE, values[1] );
	  else if( values[0].lower() == "artist" )
	    addMetaInfo( META_ARTIST, values[1] );
	  else if( values[0].lower() == "description" )
	    addMetaInfo( META_COMMENT, values[1] );
	}
      }


      // add technical infos
      addTechnicalInfo( i18n("Version"), TQString::number(d->vInfo->version) );
      addTechnicalInfo( i18n("Channels"), TQString::number(d->vInfo->channels) );
      addTechnicalInfo( i18n("Sampling Rate"), i18n("%1 Hz").arg(d->vInfo->rate) );
      if( d->vInfo->bitrate_upper > 0 )
	addTechnicalInfo( i18n("Bitrate Upper"), i18n( "%1 bps" ).arg(d->vInfo->bitrate_upper) );
      if( d->vInfo->bitrate_nominal > 0 )
	addTechnicalInfo( i18n("Bitrate Nominal"), i18n( "%1 bps" ).arg(d->vInfo->bitrate_nominal) );
      if( d->vInfo->bitrate_lower > 0 )
	addTechnicalInfo( i18n("Bitrate Lower"), i18n( "%1 bps" ).arg(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 ) {
    kdDebug() << "(K3bOggVorbisDecoder) bitstream != 0. Multible bitstreams not supported." << endl;
    return -1;
  }
  
  else if( bytesRead == OV_HOLE ) {
    kdDebug() << "(K3bOggVorbisDecoder) OV_HOLE" << endl;
    // recursive new try
    return decodeInternal( data, maxLen );
  }

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

  else if( bytesRead == 0 ) {
    kdDebug() << "(K3bOggVorbisDecoder) successfully finished decoding." << endl;
    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 );
}


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


K3bOggVorbisDecoderFactory::K3bOggVorbisDecoderFactory( TQObject* parent, const char* name )
  : K3bAudioDecoderFactory( parent, name )
{
}


K3bOggVorbisDecoderFactory::~K3bOggVorbisDecoderFactory()
{
}


K3bAudioDecoder* K3bOggVorbisDecoderFactory::createDecoder( TQObject* parent, 
							    const char* name ) const
{
  return new K3bOggVorbisDecoder( parent, name );
}


bool K3bOggVorbisDecoderFactory::canDecode( const KURL& url )
{
  FILE* file = fopen( TQFile::encodeName(url.path()), "r" );
  if( !file ) {
    kdDebug() << "(K3bOggVorbisDecoder) Could not open file " << url.path() << endl;
    return false;
  }

  OggVorbis_File of;

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

  ov_clear( &of );

  return true;
}


#include "k3boggvorbisdecoder.moc"