summaryrefslogtreecommitdiffstats
path: root/plugins/decoder/flac/k3bflacdecoder.cpp
blob: 495e3d9686912ebdc9fbe1c225b5b5d0666a49ce (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* 
 * FLAC decoder module for K3b.
 * Based on the Ogg Vorbis module for same.
 * Copyright (C) 1998-2007 Sebastian Trueg <[email protected]>
 * Copyright (C) 2003-2004 John Steele Scott <[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 "k3bflacdecoder.h"

#include <k3bpluginfactory.h>

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

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

#include <string.h>
#include <math.h>
#include <FLAC++/metadata.h>
#include <FLAC++/decoder.h>

#ifdef HAVE_TAGLIB
#include <taglib/tag.h>
#include <taglib/flacfile.h>
#endif

#if !defined FLACPP_API_VERSION_CURRENT || FLACPP_API_VERSION_CURRENT < 6
#define LEGACY_FLAC
#else
#undef LEGACY_FLAC
#endif

K_EXPORT_COMPONENT_FACTORY( libk3bflacdecoder, K3bPluginFactory<K3bFLACDecoderFactory>( "libk3bflacdecoder" ) )


class K3bFLACDecoder::Private
#ifdef LEGACY_FLAC
  : public FLAC::Decoder::SeekableStream
#else
  : public FLAC::Decoder::Stream
#endif
{
public:
  void open(TQFile* f) {
    file = f;
    file->open(IO_ReadOnly);
    
    TQT_TQIODEVICE(internalBuffer)->flush();
    
    set_metadata_respond(FLAC__METADATA_TYPE_STREAMINFO);
    set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT);

    init();
    process_until_end_of_metadata();
  }

  void cleanup() {
    file->close();
    finish();
    delete comments;
    comments = 0;
  }

  Private(TQFile* f)
#ifdef LEGACY_FLAC
    : FLAC::Decoder::SeekableStream(),
#else
    : FLAC::Decoder::Stream(),
#endif
      comments(0) {
    internalBuffer = new TQBuffer();
    internalBuffer->open(IO_ReadWrite);

    open(f);
  }
  

  ~Private() {
    cleanup();
    delete internalBuffer;
  }
  
  bool seekToFrame(int frame);

  TQFile* file;
  TQBuffer* internalBuffer;
  FLAC::Metadata::VorbisComment* comments;
  unsigned rate;
  unsigned channels;
  unsigned bitsPerSample;
  unsigned maxFramesize;
  unsigned maxBlocksize;
  unsigned minFramesize;
  unsigned minBlocksize;
  FLAC__uint64 samples;
  
protected:
#ifdef LEGACY_FLAC
  virtual FLAC__SeekableStreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes);
  virtual FLAC__SeekableStreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
  virtual FLAC__SeekableStreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
  virtual FLAC__SeekableStreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length);
#else
  virtual FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
  virtual FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
  virtual FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
  virtual FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length);
#endif
  virtual bool eof_callback();
  virtual void error_callback(FLAC__StreamDecoderErrorStatus){};
  virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata);
  virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
};

bool K3bFLACDecoder::Private::seekToFrame(int frame) {
  FLAC__uint64 sample = frame * rate / 75;
  return seek_absolute(sample);
}

bool K3bFLACDecoder::Private::eof_callback() {
  return file->atEnd();
}

#ifdef LEGACY_FLAC
FLAC__SeekableStreamDecoderReadStatus K3bFLACDecoder::Private::read_callback(FLAC__byte buffer[],                                                                             unsigned *bytes) {
  long retval =  file->readBlock((char *)buffer, (*bytes));
  if(-1 == retval) {
    return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
  } else {
    (*bytes) = retval;
    return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
  }
}
#else
FLAC__StreamDecoderReadStatus K3bFLACDecoder::Private::read_callback(FLAC__byte buffer[],                                                                             size_t *bytes) {
  long retval =  file->readBlock((char *)buffer, (*bytes));
  if(-1 == retval) {
    return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  } else {
    (*bytes) = retval;
    return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  }
}
#endif

#ifdef LEGACY_FLAC
FLAC__SeekableStreamDecoderSeekStatus 
K3bFLACDecoder::Private::seek_callback(FLAC__uint64 absolute_byte_offset) {
  if(!file->at(absolute_byte_offset))
    return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
  else
    return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
}
#else
FLAC__StreamDecoderSeekStatus 
K3bFLACDecoder::Private::seek_callback(FLAC__uint64 absolute_byte_offset) {
  if(file->at(absolute_byte_offset) == FALSE)
    return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
  else
    return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
#endif

#ifdef LEGACY_FLAC
FLAC__SeekableStreamDecoderTellStatus 
K3bFLACDecoder::Private::tell_callback(FLAC__uint64 *absolute_byte_offset) {
  (*absolute_byte_offset) = file->at();
  return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
}
#else
FLAC__StreamDecoderTellStatus 
K3bFLACDecoder::Private::tell_callback(FLAC__uint64 *absolute_byte_offset) {
  (*absolute_byte_offset) = file->at();
  return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
#endif

#ifdef LEGACY_FLAC
FLAC__SeekableStreamDecoderLengthStatus 
K3bFLACDecoder::Private::length_callback(FLAC__uint64 *stream_length) {
  (*stream_length) = file->size();
  return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
}
#else
FLAC__StreamDecoderLengthStatus 
K3bFLACDecoder::Private::length_callback(FLAC__uint64 *stream_length) {
  (*stream_length) = file->size();
  return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
#endif


void K3bFLACDecoder::Private::metadata_callback(const FLAC__StreamMetadata *metadata) {
  switch (metadata->type) {
  case FLAC__METADATA_TYPE_STREAMINFO:
    channels = metadata->data.stream_info.channels;
    rate = metadata->data.stream_info.sample_rate;
    bitsPerSample = metadata->data.stream_info.bits_per_sample;
    samples = metadata->data.stream_info.total_samples;
    maxFramesize = metadata->data.stream_info.max_framesize;
    minFramesize = metadata->data.stream_info.min_framesize;
    maxBlocksize = metadata->data.stream_info.max_blocksize;
    minBlocksize = metadata->data.stream_info.min_blocksize;
    break;
  case FLAC__METADATA_TYPE_VORBIS_COMMENT:
    comments = new FLAC::Metadata::VorbisComment((FLAC__StreamMetadata *)metadata, true);
    break;
  default:
    break;
  }
}

FLAC__StreamDecoderWriteStatus K3bFLACDecoder::Private::write_callback(const FLAC__Frame *frame, const FLAC__int32 * const buffer[]) {
  unsigned i, j;
  // Note that in canDecode we made sure that the input is 1-16 bit stereo or mono.
  unsigned samples = frame->header.blocksize;

  for(i=0; i < samples; i++) {
   // in FLAC channel 0 is left, 1 is right
   for(j=0; j < this->channels; j++) {
    FLAC__int32 value = (buffer[j][i])<<(16 - frame->header.bits_per_sample);
    internalBuffer->putch(value >> 8); // msb
    internalBuffer->putch(value & 0xFF); // lsb
   }
  }

  // Rewind the buffer so the decode method will take data from the beginning.
  internalBuffer->at(0);
  return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

K3bFLACDecoder::K3bFLACDecoder( TQObject* tqparent, const char* name )
  : K3bAudioDecoder( tqparent, name )
{
  d = 0;
}


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

void K3bFLACDecoder::cleanup()
{
  if (d) {
    d->cleanup();
    d->open(new TQFile(filename()));
  }
  else
    d = new Private(new TQFile(filename()));
}

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

  frames = (unsigned long)ceil((d->samples * 75.0)/d->rate);
  samplerate = d->rate;
  ch = d->channels;

  // add meta info
  if( d->comments != 0 ) {
    kdDebug() << "(K3bFLACDecoder) unpacking Vorbis tags" << endl;
    for( unsigned int i = 0; i < d->comments->get_num_comments(); ++i ) {
      TQString key = TQString::fromUtf8( d->comments->get_comment(i).get_field_name(),
                                       d->comments->get_comment(i).get_field_name_length() );
      TQString value = TQString::fromUtf8( d->comments->get_comment(i).get_field_value(),
                                         d->comments->get_comment(i).get_field_value_length() );

      if( key.upper() == "TITLE" )
        addMetaInfo( META_TITLE, value );
      else if( key.upper() == "ARTIST" )
        addMetaInfo( META_ARTIST, value );
      else if( key.upper() == "DESCRIPTION" )
        addMetaInfo( META_COMMENT, value );
    }
  }
#ifdef HAVE_TAGLIB
  if ((d->comments == 0) || (d->comments->get_num_comments() == 0)) {
    // no Vorbis comments, check for ID3 tags
    kdDebug() << "(K3bFLACDecoder) using taglib to read tag" << endl;
    TagLib::FLAC::File f( TQFile::encodeName(filename()) );
    if( f.isOpen() ) {
      addMetaInfo( META_TITLE, TStringToQString( f.tag()->title() ) );
      addMetaInfo( META_ARTIST, TStringToQString( f.tag()->artist() ) );
      addMetaInfo( META_COMMENT, TStringToQString( f.tag()->comment() ) );
    }
  }
#endif

  return true;
}


bool K3bFLACDecoder::initDecoderInternal()
{
  cleanup();

  return true;
}


int K3bFLACDecoder::decodeInternal( char* _data, int maxLen )
{
  int bytesToCopy;
  int bytesCopied;
  int bytesAvailable;

#ifdef LEGACY_FLAC
  if(d->internalBuffer->size() == 0) {
    // want more data
    switch(d->get_state()) {
    case FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM:
      d->finish();
      break;
    case FLAC__SEEKABLE_STREAM_DECODER_OK:
      if(! d->process_single())
        return -1;
      break;
    default:
      return -1;
    }
  }
#else
  if(d->internalBuffer->size() == 0) {
    // want more data
    if(d->get_state() == FLAC__STREAM_DECODER_END_OF_STREAM)
      d->finish();
    else if(d->get_state() < FLAC__STREAM_DECODER_END_OF_STREAM) {
      if(! d->process_single())
        return -1;
    }
    else
      return -1;
  }
#endif
  
  bytesAvailable = d->internalBuffer->size() - d->internalBuffer->at();
  bytesToCopy = TQMIN(maxLen, bytesAvailable);
  bytesCopied = (int)d->internalBuffer->readBlock(_data, bytesToCopy);

  if(bytesCopied == bytesAvailable) {
    // reset the buffer
    d->internalBuffer->close();
    d->internalBuffer->open(IO_ReadWrite|IO_Truncate);
  }

  return bytesCopied;
}


bool K3bFLACDecoder::seekInternal( const K3b::Msf& pos )
{
  return d->seekToFrame(pos.totalFrames());
}


TQString K3bFLACDecoder::fileType() const
{
  return i18n("FLAC");
}


TQStringList K3bFLACDecoder::supportedTechnicalInfos() const
{
  return TQStringList::split( ";", 
                             i18n("Channels") + ";" +
                             i18n("Sampling Rate") + ";" +
                             i18n("Sample Size") );
}


TQString K3bFLACDecoder::technicalInfo( const TQString& info ) const
{
  if( d->comments != 0 ) {
    if( info == i18n("Vendor") )
#ifdef FLAC_NEWER_THAN_1_1_1
      return TQString::fromUtf8((char*)d->comments->get_vendor_string());
#else
      return TQString::fromUtf8(d->comments->get_vendor_string().get_field());
#endif
    else if( info == i18n("Channels") )
      return TQString::number(d->channels);
    else if( info == i18n("Sampling Rate") )
      return i18n("%1 Hz").tqarg(d->rate);
    else if( info == i18n("Sample Size") )
      return i18n("%1 bits").tqarg(d->bitsPerSample);
  }

  return TQString();
}



K3bFLACDecoderFactory::K3bFLACDecoderFactory( TQObject* tqparent, const char* name )
  : K3bAudioDecoderFactory( tqparent, name )
{
}


K3bFLACDecoderFactory::~K3bFLACDecoderFactory()
{
}


K3bAudioDecoder* K3bFLACDecoderFactory::createDecoder( TQObject* tqparent, 
						 const char* name ) const
{
  return new K3bFLACDecoder( tqparent, name );
}


bool K3bFLACDecoderFactory::canDecode( const KURL& url )
{
  // buffer large enough to read an ID3 tag header
  char buf[10];

  // Note: since file is created on the stack it will be closed automatically
  // by its destructor when this method (i.e. canDecode) returns.
  TQFile file(url.path());

  if(!file.open(IO_ReadOnly)) {
    kdDebug() << "(K3bFLACDecoder) Could not open file " << url.path() << endl;
    return false;
  }

  // look for a fLaC magic number or ID3 tag header
  if(10 != file.readBlock(buf, 10)) {
    kdDebug() << "(K3bFLACDecorder) File " << url.path()
              << " is too small to be a FLAC file" << endl;
    return false;
  }

  if(0 == memcmp(buf, "ID3", 3)) {
    // Found ID3 tag, try and seek past it.
    kdDebug() << "(K3bFLACDecorder) File " << url.path() << ": found ID3 tag" << endl;

    // See www.id3.org for details of the header, note that the size field
    // unpacks to 7-bit bytes, then the +10 is for the header itself.
    int pos;
    pos = ((buf[6]<<21)|(buf[7]<<14)|(buf[8]<<7)|buf[9]) + 10;

    kdDebug() << "(K3bFLACDecoder) " << url.path() << ": seeking to " 
              << pos << endl;
    if(!file.at(pos)) {
      kdDebug() << "(K3bFLACDecoder) " << url.path() << ": couldn't seek to " 
                << pos << endl;
      return false;
    }else{
      // seek was okay, try and read magic number into buf
      if(4 != file.readBlock(buf, 4)) {
        kdDebug() << "(K3bFLACDecorder) File " << url.path()
                  << " has ID3 tag but naught else!" << endl;
        return false;
      }
    }
  }

  if(memcmp(buf, "fLaC", 4) != 0) {
    kdDebug() << "(K3bFLACDecoder) " << url.path() << ": not a FLAC file" << endl;
    return false;
  }

  FLAC::Metadata::StreamInfo info = FLAC::Metadata::StreamInfo();
  FLAC::Metadata::get_streaminfo(url.path().ascii(), info);

  if((info.get_channels() <= 2) &&
     (info.get_bits_per_sample() <= 16)) {
    return true;
  } else {
    kdDebug() << "(K3bFLACDecoder) " << url.path() << ": wrong format:" << endl
              << "                channels:    " 
              << TQString::number(info.get_channels()) << endl
              << "                samplerate:  "
              << TQString::number(info.get_sample_rate()) << endl
              << "                bits/sample: "
              << TQString::number(info.get_bits_per_sample()) << endl;
    return false;
  }
}

#include "k3bflacdecoder.moc"