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