1 /*
2    Copyright 2009 Last.fm Ltd.
3    Copyright 2009 John Stamp <jstamp@users.sourceforge.net>
4 
5    This file is part of liblastfm.
6 
7    liblastfm is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    liblastfm is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with liblastfm.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <iostream>
22 #include <fstream>
23 #include <limits>
24 #include <climits>
25 #include <cstdlib>
26 #include <sstream>
27 #include <cassert>
28 #include <stdexcept>
29 #include "MadSource.h"
30 
31 #undef max // was definded in mad
32 
33 using namespace std;
34 
35 
36 // -----------------------------------------------------------
37 
MadSource()38 MadSource::MadSource()
39           : m_pMP3_Buffer ( new unsigned char[m_MP3_BufferSize+MAD_BUFFER_GUARD] )
40 {}
41 
42 // -----------------------------------------------------------
43 
~MadSource()44 MadSource::~MadSource()
45 {
46    if ( m_inputFile.isOpen() )
47    {
48       m_inputFile.close();
49       mad_synth_finish(&m_mad_synth);
50       mad_frame_finish(&m_mad_frame);
51       mad_stream_finish(&m_mad_stream);
52    }
53    if (m_pMP3_Buffer) delete[] m_pMP3_Buffer;
54 }
55 
56 // ---------------------------------------------------------------------
57 
f2s(mad_fixed_t f)58 inline short f2s(mad_fixed_t f)
59 {
60    /* A fixed point number is formed of the following bit pattern:
61    *
62    * SWWWFFFFFFFFFFFFFFFFFFFFFFFFFFFF
63    * MSB                          LSB
64    * S ==> Sign (0 is positive, 1 is negative)
65    * W ==> Whole part bits
66    * F ==> Fractional part bits
67    *
68    * This pattern contains MAD_F_FRACBITS fractional bits, one
69    * should alway use this macro when working on the bits of a fixed
70    * point number. It is not guaranteed to be constant over the
71    * different platforms supported by libmad.
72    *
73    * The signed short value is formed, after clipping, by the least
74    * significant whole part bit, followed by the 15 most significant
75    * fractional part bits. Warning: this is a quick and dirty way to
76    * compute the 16-bit number, madplay includes much better
77    * algorithms.
78    */
79 
80    /* Clipping */
81    if(f >= MAD_F_ONE)
82       return(SHRT_MAX);
83    if(f <= -MAD_F_ONE)
84       return(-SHRT_MAX);
85 
86    /* Conversion. */
87    f = f >> (MAD_F_FRACBITS-15);
88    return (signed short)f;
89 }
90 
91 // ---------------------------------------------------------------------
92 
MadErrorString(const mad_error & error)93 string MadSource::MadErrorString(const mad_error& error)
94 {
95    switch(error)
96    {
97       /* Generic unrecoverable errors. */
98    case MAD_ERROR_BUFLEN:
99       return("input buffer too small (or EOF)");
100    case MAD_ERROR_BUFPTR:
101       return("invalid (null) buffer pointer");
102    case MAD_ERROR_NOMEM:
103       return("not enough memory");
104 
105       /* Frame header related unrecoverable errors. */
106    case MAD_ERROR_LOSTSYNC:
107       return("lost synchronization");
108    case MAD_ERROR_BADLAYER:
109       return("reserved header layer value");
110    case MAD_ERROR_BADBITRATE:
111       return("forbidden bitrate value");
112    case MAD_ERROR_BADSAMPLERATE:
113       return("reserved sample frequency value");
114    case MAD_ERROR_BADEMPHASIS:
115       return("reserved emphasis value");
116 
117       /* Recoverable errors */
118    case MAD_ERROR_BADCRC:
119       return("CRC check failed");
120    case MAD_ERROR_BADBITALLOC:
121       return("forbidden bit allocation value");
122    case MAD_ERROR_BADSCALEFACTOR:
123       return("bad scalefactor index");
124    case MAD_ERROR_BADFRAMELEN:
125       return("bad frame length");
126    case MAD_ERROR_BADBIGVALUES:
127       return("bad big_values count");
128    case MAD_ERROR_BADBLOCKTYPE:
129       return("reserved block_type");
130    case MAD_ERROR_BADSCFSI:
131       return("bad scalefactor selection info");
132    case MAD_ERROR_BADDATAPTR:
133       return("bad main_data_begin pointer");
134    case MAD_ERROR_BADPART3LEN:
135       return("bad audio data length");
136    case MAD_ERROR_BADHUFFTABLE:
137       return("bad Huffman table select");
138    case MAD_ERROR_BADHUFFDATA:
139       return("Huffman data overrun");
140    case MAD_ERROR_BADSTEREO:
141       return("incompatible block_type for JS");
142 
143       /* Unknown error. This switch may be out of sync with libmad's
144       * defined error codes.
145       */
146    default:
147       return("Unknown error code");
148    }
149 }
150 
151 
152 // -----------------------------------------------------------------------------
153 
isRecoverable(const mad_error & error,bool log)154 bool MadSource::isRecoverable(const mad_error& error, bool log)
155 {
156    if (MAD_RECOVERABLE (error))
157    {
158       /* Do not print a message if the error is a loss of
159       * synchronization and this loss is due to the end of
160       * stream guard bytes. (See the comments marked {3}
161       * supra for more informations about guard bytes.)
162       */
163       if (error != MAD_ERROR_LOSTSYNC   /*|| mad_stream.this_frame != pGuard */ && log)
164       {
165          cerr << "Recoverable frame level error: "
166               << MadErrorString(error) << endl;
167       }
168 
169       return true;
170    }
171    else
172    {
173       if (error == MAD_ERROR_BUFLEN)
174          return true;
175       else
176       {
177          stringstream ss;
178 
179          ss << "Unrecoverable frame level error: "
180             << MadErrorString (error) << endl;
181          throw ss.str();
182       }
183    }
184 
185    return false;
186 }
187 
188 // -----------------------------------------------------------
189 
init(const QString & fileName)190 void MadSource::init(const QString& fileName)
191 {
192    m_inputFile.setFileName( m_fileName = fileName );
193    bool fine = m_inputFile.open( QIODevice::ReadOnly );
194 
195    if ( !fine )
196    {
197       throw std::runtime_error ("Cannot load mp3 file!");
198    }
199 
200    mad_stream_init(&m_mad_stream);
201    mad_frame_init (&m_mad_frame);
202    mad_synth_init (&m_mad_synth);
203    mad_timer_reset(&m_mad_timer);
204 
205    m_pcmpos = m_mad_synth.pcm.length;
206 }
207 
208 // -----------------------------------------------------------------------------
209 
210 /*QString MadSource::getMbid()
211 {
212     char out[MBID_BUFFER_SIZE];
213     int const r = getMP3_MBID( QFile::encodeName( m_fileName ), out );
214     if (r == 0)
215         return QString::fromLatin1( out );
216     return QString();
217 }*/
218 
getInfo(int & lengthSecs,int & samplerate,int & bitrate,int & nchannels)219 void MadSource::getInfo(int& lengthSecs, int& samplerate, int& bitrate, int& nchannels )
220 {
221    // get the header plus some other stuff..
222    QFile inputFile(m_fileName);
223    bool fine = inputFile.open( QIODevice::ReadOnly );
224 
225    if ( !fine )
226    {
227       throw std::runtime_error ("ERROR: Cannot load file for getInfo!");
228       return;
229    }
230 
231    unsigned char* pMP3_Buffer  = new unsigned char[m_MP3_BufferSize+MAD_BUFFER_GUARD];
232 
233    mad_stream   madStream;
234    mad_header  madHeader;
235    mad_timer_t  madTimer;
236 
237    mad_stream_init(&madStream);
238    mad_timer_reset(&madTimer);
239 
240    double avgSamplerate = 0;
241    double avgBitrate = 0;
242    double avgNChannels = 0;
243    int nFrames = 0;
244 
245    while ( fetchData( inputFile, pMP3_Buffer, m_MP3_BufferSize, madStream) )
246    {
247       if ( mad_header_decode(&madHeader, &madStream) != 0 )
248       {
249          if ( isRecoverable(madStream.error) )
250             continue;
251          else
252             break;
253       }
254 
255       mad_timer_add(&madTimer, madHeader.duration);
256 
257       avgSamplerate += madHeader.samplerate;
258       avgBitrate += madHeader.bitrate;
259 
260       if ( madHeader.mode == MAD_MODE_SINGLE_CHANNEL )
261          ++avgNChannels;
262       else
263          avgNChannels += 2;
264 
265       ++nFrames;
266    }
267 
268    inputFile.close();
269    mad_stream_finish(&madStream);
270    mad_header_finish(&madHeader);
271    delete[] pMP3_Buffer;
272 
273 
274    lengthSecs = static_cast<int>(madTimer.seconds);
275    samplerate = static_cast<int>( (avgSamplerate/nFrames) + 0.5 );
276    bitrate = static_cast<int>( (avgBitrate/nFrames) + 0.5 );
277    nchannels = static_cast<int>( (avgNChannels/nFrames) + 0.5 );
278 }
279 
280 // -----------------------------------------------------------
281 
282 
fetchData(QFile & mp3File,unsigned char * pMP3_Buffer,const int MP3_BufferSize,mad_stream & madStream)283 bool MadSource::fetchData( QFile& mp3File,
284                             unsigned char* pMP3_Buffer,
285                             const int MP3_BufferSize,
286                             mad_stream& madStream )
287 {
288    unsigned char *pReadStart = NULL;
289    unsigned char *pGuard = NULL;
290 
291    if ( madStream.buffer == NULL ||
292         madStream.error == MAD_ERROR_BUFLEN )
293    {
294 
295       size_t readSize;
296       size_t remaining;
297 
298       /* {2} libmad may not consume all bytes of the input
299       * buffer. If the last frame in the buffer is not wholly
300       * contained by it, then that frame's start is pointed by
301       * the next_frame member of the Stream structure. This
302       * common situation occurs when mad_frame_decode() fails,
303       * sets the stream error code to MAD_ERROR_BUFLEN, and
304       * sets the next_frame pointer to a non NULL value. (See
305       * also the comment marked {4} bellow.)
306       *
307       * When this occurs, the remaining unused bytes must be
308       * put back at the beginning of the buffer and taken in
309       * account before refilling the buffer. This means that
310       * the input buffer must be large enough to hold a whole
311       * frame at the highest observable bit-rate (currently 448
312       * kb/s). XXX=XXX Is 2016 bytes the size of the largest
313       * frame? (448000*(1152/32000))/8
314       */
315       if (madStream.next_frame != NULL)
316       {
317          remaining = madStream.bufend - madStream.next_frame;
318          memmove (pMP3_Buffer, madStream.next_frame, remaining);
319 
320          pReadStart = pMP3_Buffer + remaining;
321          readSize = MP3_BufferSize - remaining;
322       }
323       else
324       {
325          readSize = MP3_BufferSize;
326          pReadStart = pMP3_Buffer;
327          remaining = 0;
328       }
329 
330       readSize = mp3File.read( reinterpret_cast<char*>(pReadStart), readSize );
331 
332       // nothing else to read!
333       if (readSize <= 0)
334          return false;
335 
336       if ( mp3File.atEnd() )
337       {
338          pGuard = pReadStart + readSize;
339 
340          memset (pGuard, 0, MAD_BUFFER_GUARD);
341          readSize += MAD_BUFFER_GUARD;
342       }
343 
344       // Pipe the new buffer content to libmad's stream decoder facility.
345       mad_stream_buffer( &madStream, pMP3_Buffer,
346                          static_cast<unsigned int>(readSize + remaining));
347 
348       madStream.error = MAD_ERROR_NONE;
349    }
350 
351    return true;
352 }
353 
354 // -----------------------------------------------------------------------------
355 
skipSilence(double silenceThreshold)356 void MadSource::skipSilence(double silenceThreshold /* = 0.0001 */)
357 {
358    mad_frame  madFrame;
359    mad_synth    madSynth;
360 
361    mad_frame_init(&madFrame);
362    mad_synth_init (&madSynth);
363 
364    silenceThreshold *= static_cast<double>( numeric_limits<short>::max() );
365 
366    for (;;)
367    {
368       if ( !fetchData( m_inputFile, m_pMP3_Buffer, m_MP3_BufferSize, m_mad_stream) )
369          break;
370 
371       if ( mad_frame_decode(&madFrame, &m_mad_stream) != 0 )
372       {
373          if ( isRecoverable(m_mad_stream.error) )
374             continue;
375          else
376             break;
377       }
378 
379       mad_synth_frame (&madSynth, &madFrame);
380 
381       double sum = 0;
382 
383       switch (madSynth.pcm.channels)
384       {
385       case 1:
386          for (size_t j = 0; j < madSynth.pcm.length; ++j)
387             sum += abs(f2s(madSynth.pcm.samples[0][j]));
388          break;
389       case 2:
390          for (size_t j = 0; j < madSynth.pcm.length; ++j)
391             sum += abs(f2s(
392                      (madSynth.pcm.samples[0][j] >> 1)
393                    + (madSynth.pcm.samples[1][j] >> 1)));
394          break;
395       }
396 
397       if ( (sum >= silenceThreshold * madSynth.pcm.length) )
398          break;
399    }
400 
401    mad_frame_finish(&madFrame);
402 }
403 
404 // -----------------------------------------------------------------------------
405 
skip(const int mSecs)406 void MadSource::skip(const int mSecs)
407 {
408    if ( mSecs <= 0 )
409       return;
410 
411    mad_header  madHeader;
412    mad_header_init(&madHeader);
413 
414    for (;;)
415    {
416       if (!fetchData( m_inputFile, m_pMP3_Buffer, m_MP3_BufferSize, m_mad_stream))
417          break;
418 
419       if ( mad_header_decode(&madHeader, &m_mad_stream) != 0 )
420       {
421          if ( isRecoverable(m_mad_stream.error) )
422             continue;
423          else
424             break;
425       }
426 
427       mad_timer_add(&m_mad_timer, madHeader.duration);
428 
429       if ( mad_timer_count(m_mad_timer, MAD_UNITS_MILLISECONDS) >= mSecs )
430          break;
431    }
432 
433    mad_header_finish(&madHeader);
434 }
435 
436 // -----------------------------------------------------------
437 
updateBuffer(signed short * pBuffer,size_t bufferSize)438 int MadSource::updateBuffer(signed short* pBuffer, size_t bufferSize)
439 {
440    size_t nwrit = 0; //number of samples written to the output buffer
441 
442    for (;;)
443    {
444       // get a (valid) frame
445       // m_pcmpos == 0 could mean two things
446       // - we have completely decoded a frame, but the output buffer is still
447       //   not full (it would make more sense for pcmpos == pcm.length(), but
448       //   the loop assigns pcmpos = 0 at the end and does it this way!
449       // - we are starting a stream
450       if ( m_pcmpos == m_mad_synth.pcm.length )
451       {
452          if ( !fetchData( m_inputFile, m_pMP3_Buffer, m_MP3_BufferSize, m_mad_stream) )
453          {
454             break; // nothing else to read
455          }
456 
457          // decode the frame
458          if (mad_frame_decode (&m_mad_frame, &m_mad_stream))
459          {
460             if ( isRecoverable(m_mad_stream.error) )
461                continue;
462             else
463                break;
464          } // if (mad_frame_decode (&madFrame, &madStream))
465 
466          mad_timer_add (&m_mad_timer, m_mad_frame.header.duration);
467          mad_synth_frame (&m_mad_synth, &m_mad_frame);
468 
469          m_pcmpos = 0;
470       }
471 
472       size_t samples_for_mp3 = m_mad_synth.pcm.length - m_pcmpos;
473       size_t samples_for_buf = bufferSize - nwrit;
474       signed short* pBufferIt = pBuffer + nwrit;
475       size_t i = 0, j = 0;
476 
477       switch( m_mad_synth.pcm.channels )
478       {
479       case 1:
480          {
481             size_t samples_to_use = min (samples_for_mp3, samples_for_buf);
482             for (i = 0; i < samples_to_use; ++i )
483                pBufferIt[i] = f2s( m_mad_synth.pcm.samples[0][i+m_pcmpos] );
484          }
485          j = i;
486          break;
487 
488       case 2:
489          for (; i < samples_for_mp3 && j < samples_for_buf ; ++i, j+=2 )
490          {
491             pBufferIt[j]   = f2s( m_mad_synth.pcm.samples[0][i+m_pcmpos] );
492             pBufferIt[j+1] = f2s( m_mad_synth.pcm.samples[1][i+m_pcmpos] );
493          }
494          break;
495 
496       default:
497          cerr << "wtf kind of mp3 has " << m_mad_synth.pcm.channels << " channels??\n";
498          break;
499       }
500 
501       m_pcmpos += i;
502       nwrit += j;
503 
504       assert( nwrit <= bufferSize );
505 
506       if (nwrit == bufferSize)
507          return static_cast<int>(nwrit);
508    }
509 
510    return static_cast<int>(nwrit);
511 }
512 
513 // -----------------------------------------------------------------------------
514 
515