MP3FileReader.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifdef HAVE_MAD
17 
18 #include "MP3FileReader.h"
19 #include "base/ProgressReporter.h"
20 #include "base/Profiler.h"
21 
22 #include "system/System.h"
23 
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 
28 #include <iostream>
29 
30 #include <cstdlib>
31 
32 #ifdef HAVE_ID3TAG
33 #include <id3tag.h>
34 #endif
35 
36 #ifdef _WIN32
37 #include <io.h>
38 #include <fcntl.h>
39 #else
40 #include <fcntl.h>
41 #include <unistd.h>
42 #endif
43 
44 #include <QFileInfo>
45 
46 #include <QTextCodec>
47 
48 using std::string;
49 
51 
53  CacheMode mode, GaplessMode gaplessMode,
54  sv_samplerate_t targetRate,
55  bool normalised,
56  ProgressReporter *reporter) :
57  CodedAudioFileReader(mode, targetRate, normalised),
58  m_source(source),
59  m_path(source.getLocalFilename()),
60  m_gaplessMode(gaplessMode),
61  m_decodeErrorShown(false),
62  m_decodeThread(nullptr)
63 {
64  SVDEBUG << "MP3FileReader: local path: \"" << m_path
65  << "\", decode mode: " << decodeMode << " ("
66  << (decodeMode == DecodeAtOnce ? "DecodeAtOnce" : "DecodeThreaded")
67  << ")" << endl;
68 
69  m_channelCount = 0;
70  m_fileRate = 0;
71  m_fileSize = 0;
72  m_bitrateNum = 0;
73  m_bitrateDenom = 0;
74  m_cancelled = false;
75  m_mp3FrameCount = 0;
76  m_completion = 0;
77  m_done = false;
78  m_reporter = reporter;
79 
82  }
83 
84  m_fileSize = 0;
85 
86  m_fileBuffer = nullptr;
87  m_fileBufferSize = 0;
88 
89  m_sampleBuffer = nullptr;
91 
92  QFile qfile(m_path);
93  if (!qfile.open(QIODevice::ReadOnly)) {
94  m_error = QString("Failed to open file %1 for reading.").arg(m_path);
95  SVDEBUG << "MP3FileReader: " << m_error << endl;
96  return;
97  }
98 
99  m_fileSize = qfile.size();
100 
101  try {
102  // We need a mysterious MAD_BUFFER_GUARD (== 8) zero bytes at
103  // end of input, to ensure libmad decodes the last frame
104  // correctly. Otherwise the decoded audio is truncated.
105  SVDEBUG << "file size = " << m_fileSize << ", buffer guard = " << MAD_BUFFER_GUARD << endl;
106  m_fileBufferSize = m_fileSize + MAD_BUFFER_GUARD;
107  m_fileBuffer = new unsigned char[m_fileBufferSize];
108  memset(m_fileBuffer + m_fileSize, 0, MAD_BUFFER_GUARD);
109  } catch (...) {
110  m_error = QString("Out of memory");
111  SVDEBUG << "MP3FileReader: " << m_error << endl;
112  return;
113  }
114 
115  auto amountRead = qfile.read(reinterpret_cast<char *>(m_fileBuffer),
116  m_fileSize);
117 
118  if (amountRead < m_fileSize) {
119  SVCERR << QString("MP3FileReader::MP3FileReader: Warning: reached EOF after only %1 of %2 bytes")
120  .arg(amountRead).arg(m_fileSize) << endl;
121  memset(m_fileBuffer + amountRead, 0, m_fileSize - amountRead);
122  m_fileSize = amountRead;
123  }
124 
125  loadTags(qfile.handle());
126 
127  qfile.close();
128 
129  if (decodeMode == DecodeAtOnce) {
130 
131  if (m_reporter) {
132  connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
134  (tr("Decoding %1...").arg(QFileInfo(m_path).fileName()));
135  }
136 
138  m_error = QString("Failed to decode file %1.").arg(m_path);
139  }
140 
141  if (m_sampleBuffer) {
142  for (int c = 0; c < m_channelCount; ++c) {
143  delete[] m_sampleBuffer[c];
144  }
145  delete[] m_sampleBuffer;
146  m_sampleBuffer = nullptr;
147  }
148 
149  delete[] m_fileBuffer;
150  m_fileBuffer = nullptr;
151 
153  endSerialised();
154 
155  } else {
156 
157  if (m_reporter) m_reporter->setProgress(100);
158 
159  m_decodeThread = new DecodeThread(this);
161 
162  while ((m_channelCount == 0 || m_fileRate == 0 || m_sampleRate == 0)
163  && !m_done) {
164  usleep(10);
165  }
166 
167  SVDEBUG << "MP3FileReader: decoding startup complete, file rate = " << m_fileRate << endl;
168  }
169 
170  if (m_error != "") {
171  SVDEBUG << "MP3FileReader::MP3FileReader(\"" << m_path << "\"): ERROR: " << m_error << endl;
172  }
173 }
174 
176 {
177  Profiler profiler("MP3FileReader::~MP3FileReader");
178 
179  if (m_decodeThread) {
180  m_cancelled = true;
181  m_decodeThread->wait();
182  delete m_decodeThread;
183  }
184 }
185 
186 void
188 {
189  m_cancelled = true;
190 }
191 
192 void
194 {
195  m_title = "";
196 
197 #ifdef HAVE_ID3TAG
198 
199 #ifdef _WIN32
200  int id3fd = _dup(fd);
201 #else
202  int id3fd = dup(fd);
203 #endif
204 
205  id3_file *file = id3_file_fdopen(id3fd, ID3_FILE_MODE_READONLY);
206  if (!file) return;
207 
208  // We can do this a lot more elegantly, but we'll leave that for
209  // when we implement support for more than just the one tag!
210 
211  id3_tag *tag = id3_file_tag(file);
212  if (!tag) {
213  SVDEBUG << "MP3FileReader::loadTags: No ID3 tag found" << endl;
214  id3_file_close(file); // also closes our dup'd fd
215  return;
216  }
217 
218  m_title = loadTag(tag, "TIT2"); // work title
219  if (m_title == "") m_title = loadTag(tag, "TIT1");
220  if (m_title == "") SVDEBUG << "MP3FileReader::loadTags: No title found" << endl;
221 
222  m_maker = loadTag(tag, "TPE1"); // "lead artist"
223  if (m_maker == "") m_maker = loadTag(tag, "TPE2");
224  if (m_maker == "") SVDEBUG << "MP3FileReader::loadTags: No artist/maker found" << endl;
225 
226  for (unsigned int i = 0; i < tag->nframes; ++i) {
227  if (tag->frames[i]) {
228  QString value = loadTag(tag, tag->frames[i]->id);
229  if (value != "") {
230  m_tags[tag->frames[i]->id] = value;
231  }
232  }
233  }
234 
235  id3_file_close(file); // also closes our dup'd fd
236 
237 #else
238  SVDEBUG << "MP3FileReader::loadTags: ID3 tag support not compiled in" << endl;
239 #endif
240 }
241 
242 QString
243 MP3FileReader::loadTag(void *vtag, const char *name)
244 {
245 #ifdef HAVE_ID3TAG
246  id3_tag *tag = (id3_tag *)vtag;
247 
248  id3_frame *frame = id3_tag_findframe(tag, name, 0);
249  if (!frame) {
250  SVDEBUG << "MP3FileReader::loadTag: No \"" << name << "\" frame found in ID3 tag" << endl;
251  return "";
252  }
253 
254  if (frame->nfields < 2) {
255  cerr << "MP3FileReader::loadTag: WARNING: Not enough fields (" << frame->nfields << ") for \"" << name << "\" in ID3 tag" << endl;
256  return "";
257  }
258 
259  unsigned int nstrings = id3_field_getnstrings(&frame->fields[1]);
260  if (nstrings == 0) {
261  SVDEBUG << "MP3FileReader::loadTag: No strings for \"" << name << "\" in ID3 tag" << endl;
262  return "";
263  }
264 
265  id3_ucs4_t const *ustr = id3_field_getstrings(&frame->fields[1], 0);
266  if (!ustr) {
267  SVDEBUG << "MP3FileReader::loadTag: Invalid or absent data for \"" << name << "\" in ID3 tag" << endl;
268  return "";
269  }
270 
271  id3_utf8_t *u8str = id3_ucs4_utf8duplicate(ustr);
272  if (!u8str) {
273  SVDEBUG << "MP3FileReader::loadTag: ERROR: Internal error: Failed to convert UCS4 to UTF8 in ID3 tag" << endl;
274  return "";
275  }
276 
277  QString rv = QString::fromUtf8((const char *)u8str);
278  free(u8str);
279 
280  SVDEBUG << "MP3FileReader::loadTag: Tag \"" << name << "\" -> \""
281  << rv << "\"" << endl;
282 
283  return rv;
284 
285 #else
286  return "";
287 #endif
288 }
289 
290 void
292 {
293  if (!m_reader->decode(m_reader->m_fileBuffer, m_reader->m_fileBufferSize)) {
294  m_reader->m_error = QString("Failed to decode file %1.").arg(m_reader->m_path);
295  }
296 
297  delete[] m_reader->m_fileBuffer;
298  m_reader->m_fileBuffer = nullptr;
299 
300  if (m_reader->m_sampleBuffer) {
301  for (int c = 0; c < m_reader->m_channelCount; ++c) {
302  delete[] m_reader->m_sampleBuffer[c];
303  }
304  delete[] m_reader->m_sampleBuffer;
305  m_reader->m_sampleBuffer = nullptr;
306  }
307 
308  if (m_reader->isDecodeCacheInitialised()) {
309  m_reader->finishDecodeCache();
310  }
311 
312  m_reader->m_done = true;
313  m_reader->m_completion = 100;
314 
315  m_reader->endSerialised();
316 }
317 
318 bool
320 {
321  DecoderData data;
322  struct mad_decoder decoder;
323 
324  data.start = (unsigned char const *)mm;
325  data.length = sz;
326  data.finished = false;
327  data.reader = this;
328 
329  mad_decoder_init(&decoder, // decoder to initialise
330  &data, // our own data block for callbacks
331  input_callback, // provides (entire) input to mad
332  nullptr, // checks header
333  filter_callback, // filters frame before decoding
334  output_callback, // receives decoded output
335  error_callback, // handles decode errors
336  nullptr); // "message_func"
337 
338  mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
339  mad_decoder_finish(&decoder);
340 
341  SVDEBUG << "MP3FileReader: Decoding complete, decoded " << m_mp3FrameCount
342  << " mp3 frames" << endl;
343 
344  m_done = true;
345  return true;
346 }
347 
348 enum mad_flow
349 MP3FileReader::input_callback(void *dp, struct mad_stream *stream)
350 {
351  DecoderData *data = (DecoderData *)dp;
352 
353  if (!data->length) {
354  data->finished = true;
355  return MAD_FLOW_STOP;
356  }
357 
358  unsigned char const *start = data->start;
359  sv_frame_t length = data->length;
360 
361 #ifdef HAVE_ID3TAG
362  while (length > ID3_TAG_QUERYSIZE) {
363  ssize_t taglen = id3_tag_query(start, ID3_TAG_QUERYSIZE);
364  if (taglen <= 0) {
365  break;
366  }
367  SVDEBUG << "MP3FileReader: ID3 tag length to skip: " << taglen << endl;
368  start += taglen;
369  length -= taglen;
370  }
371 #endif
372 
373  mad_stream_buffer(stream, start, length);
374  data->length = 0;
375 
376  return MAD_FLOW_CONTINUE;
377 }
378 
379 enum mad_flow
381  struct mad_stream const *stream,
382  struct mad_frame *frame)
383 {
384  DecoderData *data = (DecoderData *)dp;
385  return data->reader->filter(stream, frame);
386 }
387 
388 static string toMagic(unsigned long fourcc)
389 {
390  string magic("....");
391  for (int i = 0; i < 4; ++i) {
392  magic[3-i] = char((fourcc >> (8*i)) & 0xff);
393  }
394  return magic;
395 }
396 
397 enum mad_flow
398 MP3FileReader::filter(struct mad_stream const *stream,
399  struct mad_frame *)
400 {
401  if (m_mp3FrameCount > 0) {
402  // only handle info frame if it appears as first mp3 frame
403  return MAD_FLOW_CONTINUE;
404  }
405 
407  // Our non-gapless mode does not even filter out the Xing/LAME
408  // frame. That's because the main reason non-gapless mode
409  // exists is for backward compatibility with MP3FileReader
410  // behaviour before the gapless support was added, so we even
411  // need to keep the spurious 1152 samples resulting from
412  // feeding Xing/LAME frame to the decoder as otherwise we'd
413  // have different output from before.
414  SVDEBUG << "MP3FileReader: Not gapless mode, not checking Xing/LAME frame"
415  << endl;
416  return MAD_FLOW_CONTINUE;
417  }
418 
419  struct mad_bitptr ptr = stream->anc_ptr;
420  string magic = toMagic(mad_bit_read(&ptr, 32));
421 
422  if (magic == "Xing" || magic == "Info") {
423 
424  SVDEBUG << "MP3FileReader: Found Xing/LAME metadata frame (magic = \""
425  << magic << "\")" << endl;
426 
427  // All we want at this point is the LAME encoder delay and
428  // padding values. We expect to see the Xing/Info magic (which
429  // we've already read), then 116 bytes of Xing data, then LAME
430  // magic, 5 byte version string, 12 bytes of LAME data that we
431  // aren't currently interested in, then the delays encoded as
432  // two 12-bit numbers into three bytes.
433  //
434  // (See gabriel.mp3-tech.org/mp3infotag.html)
435 
436  for (int skip = 0; skip < 116; ++skip) {
437  (void)mad_bit_read(&ptr, 8);
438  }
439 
440  magic = toMagic(mad_bit_read(&ptr, 32));
441 
442  if (magic == "LAME") {
443 
444  SVDEBUG << "MP3FileReader: Found LAME-specific metadata" << endl;
445 
446  for (int skip = 0; skip < 5 + 12; ++skip) {
447  (void)mad_bit_read(&ptr, 8);
448  }
449 
450  auto delay = mad_bit_read(&ptr, 12);
451  auto padding = mad_bit_read(&ptr, 12);
452 
453  sv_frame_t delayToDrop = DEFAULT_DECODER_DELAY + delay;
454  sv_frame_t paddingToDrop = padding - DEFAULT_DECODER_DELAY;
455  if (paddingToDrop < 0) paddingToDrop = 0;
456 
457  SVDEBUG << "MP3FileReader: LAME encoder delay = " << delay
458  << ", padding = " << padding << endl;
459 
460  SVDEBUG << "MP3FileReader: Will be trimming " << delayToDrop
461  << " samples from start and " << paddingToDrop
462  << " from end" << endl;
463 
464  CodedAudioFileReader::setFramesToTrim(delayToDrop, paddingToDrop);
465 
466  } else {
467  SVDEBUG << "MP3FileReader: Xing frame has no LAME metadata" << endl;
468  }
469 
470  return MAD_FLOW_IGNORE;
471 
472  } else {
473  return MAD_FLOW_CONTINUE;
474  }
475 }
476 
477 enum mad_flow
479  struct mad_header const *header,
480  struct mad_pcm *pcm)
481 {
482  DecoderData *data = (DecoderData *)dp;
483  return data->reader->accept(header, pcm);
484 }
485 
486 enum mad_flow
487 MP3FileReader::accept(struct mad_header const *header,
488  struct mad_pcm *pcm)
489 {
490  int channels = pcm->channels;
491  int frames = pcm->length;
492 
493  if (header) {
494  m_bitrateNum = m_bitrateNum + double(header->bitrate);
495  m_bitrateDenom ++;
496  }
497 
498  if (frames < 1) return MAD_FLOW_CONTINUE;
499 
500  if (m_channelCount == 0) {
501 
502  m_fileRate = pcm->samplerate;
503  m_channelCount = channels;
504 
505  SVDEBUG << "MP3FileReader::accept: file rate = " << pcm->samplerate
506  << ", channel count = " << channels << ", about to init "
507  << "decode cache" << endl;
508 
510 
512 // SVDEBUG << "MP3FileReader::accept: channel count " << m_channelCount << ", file rate " << m_fileRate << ", about to start serialised section" << endl;
513  startSerialised("MP3FileReader::Decode", &m_cancelled);
514  if (m_cancelled) {
515  return MAD_FLOW_STOP;
516  }
517  }
518  }
519 
520  if (m_bitrateDenom > 0) {
521  double bitrate = m_bitrateNum / m_bitrateDenom;
522  double duration = double(m_fileSize * 8) / bitrate;
523  double elapsed = double(m_frameCount) / m_sampleRate;
524  double percent = 100;
525  if (duration > 0.0) percent = ((elapsed * 100.0) / duration);
526  int p = int(percent);
527  if (p < 1) p = 1;
528  if (p > 99) p = 99;
529  if (m_completion != p && m_reporter) {
530  m_completion = p;
532  }
533  }
534 
535  if (m_cancelled) {
536  SVDEBUG << "MP3FileReader: Decoding cancelled" << endl;
537  return MAD_FLOW_STOP;
538  }
539 
540  if (!isDecodeCacheInitialised()) {
541  SVDEBUG << "MP3FileReader::accept: fallback case: file rate = " << pcm->samplerate
542  << ", channel count = " << channels << ", about to init "
543  << "decode cache" << endl;
545  }
546 
547  if (m_sampleBufferSize < size_t(frames)) {
548  if (!m_sampleBuffer) {
549  m_sampleBuffer = new float *[channels];
550  for (int c = 0; c < channels; ++c) {
551  m_sampleBuffer[c] = nullptr;
552  }
553  }
554  for (int c = 0; c < channels; ++c) {
555  delete[] m_sampleBuffer[c];
556  m_sampleBuffer[c] = new float[frames];
557  }
558  m_sampleBufferSize = frames;
559  }
560 
561  int activeChannels = int(sizeof(pcm->samples) / sizeof(pcm->samples[0]));
562 
563  for (int ch = 0; ch < channels; ++ch) {
564 
565  for (int i = 0; i < frames; ++i) {
566 
567  mad_fixed_t sample = 0;
568  if (ch < activeChannels) {
569  sample = pcm->samples[ch][i];
570  }
571  float fsample = float(sample) / float(MAD_F_ONE);
572 
573  m_sampleBuffer[ch][i] = fsample;
574  }
575  }
576 
578 
579  ++m_mp3FrameCount;
580 
581  return MAD_FLOW_CONTINUE;
582 }
583 
584 enum mad_flow
586  struct mad_stream *stream,
587  struct mad_frame *)
588 {
589  DecoderData *data = (DecoderData *)dp;
590 
591  sv_frame_t ix = stream->this_frame - data->start;
592 
593  if (stream->error == MAD_ERROR_LOSTSYNC &&
594  (data->finished || ix >= data->length)) {
595  // We are at end of file, losing sync is expected behaviour,
596  // don't report it
597  return MAD_FLOW_CONTINUE;
598  }
599 
600  if (!data->reader->m_decodeErrorShown) {
601  char buffer[256];
602  snprintf(buffer, 255,
603  "MP3 decoding error 0x%04x (%s) at byte offset %lld",
604  stream->error, mad_stream_errorstr(stream), (long long int)ix);
605  SVCERR << "Warning: in file \"" << data->reader->m_path << "\": "
606  << buffer << " (continuing; will not report any further decode errors for this file)" << endl;
607  data->reader->m_decodeErrorShown = true;
608  }
609 
610  return MAD_FLOW_CONTINUE;
611 }
612 
613 void
614 MP3FileReader::getSupportedExtensions(std::set<QString> &extensions)
615 {
616  extensions.insert("mp3");
617 }
618 
619 bool
621 {
622  std::set<QString> extensions;
623  getSupportedExtensions(extensions);
624  return (extensions.find(extension.toLower()) != extensions.end());
625 }
626 
627 bool
629 {
630  return (type == "audio/mpeg");
631 }
632 
633 bool
635 {
636  return (supportsExtension(source.getExtension()) ||
638 }
639 
640 
641 #endif
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
unsigned char const * start
QString loadTag(void *vtag, const char *name)
enum mad_flow filter(struct mad_stream const *, struct mad_frame *)
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
static void getSupportedExtensions(std::set< QString > &extensions)
bool m_decodeErrorShown
GaplessMode
How the MP3FileReader should handle leading and trailing gaps.
Definition: MP3FileReader.h:41
static sv_frame_t DEFAULT_DECODER_DELAY
void start()
Definition: Thread.cpp:34
float ** m_sampleBuffer
DecodeThread * m_decodeThread
unsigned char * m_fileBuffer
Do not trim any samples.
size_t m_sampleBufferSize
GaplessMode m_gaplessMode
enum mad_flow accept(struct mad_header const *, struct mad_pcm *)
static enum mad_flow filter_callback(void *, struct mad_stream const *, struct mad_frame *)
void startSerialised(QString id, const std::atomic< bool > *cancelled)
static string toMagic(unsigned long fourcc)
MP3FileReader(FileSource source, DecodeMode decodeMode, CacheMode cacheMode, GaplessMode gaplessMode, sv_samplerate_t targetRate=0, bool normalised=false, ProgressReporter *reporter=0)
Trim unwanted samples from the start and end of the decoded audio.
bool decode(void *mm, sv_frame_t sz)
FileSource is a class used to refer to the contents of a file that may be either local or at a remote...
Definition: FileSource.h:59
void addSamplesToDecodeCache(float **samples, sv_frame_t nframes)
QString getContentType() const
Return the MIME content type of this file, if known.
Definition: FileSource.cpp:634
void loadTags(int fd)
double m_bitrateNum
static enum mad_flow output_callback(void *, struct mad_header const *, struct mad_pcm *)
virtual void setProgress(int percentage)=0
static bool supports(FileSource &source)
size_t m_fileBufferSize
#define SVDEBUG
Definition: Debug.h:106
sv_frame_t m_fileSize
ProgressReporter * m_reporter
void setFramesToTrim(sv_frame_t fromStart, sv_frame_t fromEnd)
#define SVCERR
Definition: Debug.h:109
sv_frame_t m_frameCount
virtual void setMessage(QString text)=0
QString getExtension() const
Return the file extension for this file, if any.
Definition: FileSource.cpp:640
static enum mad_flow error_callback(void *, struct mad_stream *, struct mad_frame *)
std::atomic< bool > m_cancelled
sv_samplerate_t m_sampleRate
bool isDecodeCacheInitialised() const
static bool supportsExtension(QString ext)
virtual ~MP3FileReader()
static bool supportsContentType(QString type)
static enum mad_flow input_callback(void *, struct mad_stream *)
Profile point instance class.
Definition: Profiler.h:93