CSL  5.2
SoundFileMP3.cpp
Go to the documentation of this file.
1 //
2 // SoundFileMP3.cpp -- MP3File implementation
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 
6 #include "SoundFileMP3.h"
7 #include "math.h"
8 
9 #ifdef USE_libMAD
10 
11 using namespace csl;
12 
13 #define DEFAULT_MP3_RATE 44100
14 
15 // The following utility routine performs rounding to MP3_SAMP_TYPE
16 
17 #ifdef CSL_MP3_AS_FLOATS
18 
19 static double samp_scale = 0.01 / (double) (1L << MAD_F_FRACBITS);
20 
21 static inline float scale_MP3(mad_fixed_t sample) {
22  double fsample = sample;
23  return (float) (fsample / samp_scale);
24 }
25 
26 #define MP3_SAMP_TYPE float
27 #define MP3_WRITE sf_writef_float
28 #define MP3_DEPTH 32
29 
30 #else // CSL_MP3_AS_INTS
31 
32 #define MP3_SAMP_TYPE short
33 
34 // static double samp_scale = (1L << MAD_F_FRACBITS) + 0.5;
35 //
36 //static inline int scale_MP3(mad_fixed_t sample) {
37 // double fsample = SampleBuffer samp_scale;
38 // return (int) fsample;
39 //}
40 
41 static inline MP3_SAMP_TYPE scale_MP3(mad_fixed_t sample) {
42  sample += (1L << (MAD_F_FRACBITS - 16)); // round
43  if (sample >= MAD_F_ONE) // clip
44  sample = MAD_F_ONE - 1;
45  else if (sample < -MAD_F_ONE)
46  sample = -MAD_F_ONE;
47  return (MP3_SAMP_TYPE) (sample >> (MAD_F_FRACBITS + 1 - 16)); // quantize
48 }
49 
50 #define MP3_WRITE sf_writef_short
51 #define MP3_DEPTH 16
52 
53 #endif
54 
55 #define scale_MP3F(v) (((float) scale_MP3(v)) / 32768.0f)
56 
57 //
58 // DecodedFile
59 //
60 
61 // constructor
62 
63 DecodedFile::DecodedFile(string tpath, int start, int stop, float maxDurInSecs)
64  : LSoundFile(tpath, start, stop, false, maxDurInSecs) {
65  // no-op
66 }
67 
68 // Desctructor
69 
71  if ( ! mBuffers.empty())
72  mBuffers.clear(); // erase old values
73 }
74 
75 // DecodedFile::checkBufferStack - make certain the buffer can hold num_samples frames
76 
77 bool DecodedFile::checkBufferStack(unsigned num_samples) {
78  if (mBuffers.empty()) {
80  } else {
81  Buffer * bf = mBuffers.back(); // get buffer from vector in data struct
82  if ((bf->mNumFrames + num_samples) > bf->mNumAlloc) { // if it's full
84  }
85  }
86  return true;
87 }
88 
89 // Add a new buffer to the buffer vector
90 
91 bool DecodedFile::addBuffer(unsigned nnumChannels, unsigned nnumFrames) {
92 //#ifdef FMAK_ANALYZER
93 // unsigned totS = this->totalSize();
94 // unsigned maxSongLength = gConfig->iValue(kMaxSongLength) * mFrameRate;
95 //
96 // if (totS > maxSongLength) {
97 // // numFrames = maxSongLength;
98 // return false;
99 // // LMS used to throw a runtime error, which is a rather useless response.
100 // // throw RunTimeError("Sound file too long");
101 // }
102 //#endif
103  Buffer * bf2 = new Buffer(nnumChannels, nnumFrames);
104  bf2->allocateBuffers();
105  bf2->mNumFrames = 0;
106  mBuffers.push_back(bf2); // push new buffer onto vector
107  return true;
108 }
109 
110 /// store samples into the receiver, adding another buffer if necessary
111 
112 bool DecodedFile::writeBuffer (float * L_buffer, float * R_buffer, unsigned num_samples) {
113  sample * outL, * outR;
114  bool retVal = true;
115 
116  if (!this->checkBufferStack(num_samples))
117  return false;
118  Buffer * bf = mBuffers.back(); // get buffer from vector
119 
120  outL = bf->buffer(0) + bf->mNumFrames; // get prts for copy loop
121  unsigned nsamples = num_samples;
122  if (mMaxDurInSecs > 0.0) { // check for overflow of mMaxDurInSecs
123  unsigned tot = this->totalSize();
124  unsigned maxFrames = (unsigned) (mMaxDurInSecs * mFrameRate);
125  if ((tot + nsamples) >= maxFrames) {
126  retVal = false;
127  nsamples = maxFrames - tot;
128  }
129  }
130  bf->mNumFrames += nsamples; // increment buf ctr
131  memcpy(L_buffer, outL, (nsamples * sizeof(sample))); // do memcpy
132  if (mNumChannels == 2) {
133  outR = bf->buffer(1) + bf->mNumFrames;
134  memcpy(R_buffer, outL, (nsamples * sizeof(sample))); // other memcpy
135  }
136  return retVal;
137 }
138 
139 bool DecodedFile::writeBuffer (int * L_buffer, int * R_buffer, unsigned num_samples) {
140  sample * outL, * outR;
141  int * left_ch, * right_ch;
142  bool retVal = true;
143 
144  if (!this->checkBufferStack(num_samples))
145  return false;
146  Buffer * bf = mBuffers.back(); // get buffer from vector
147  outL = bf->buffer(0) + bf->mNumFrames; // get prts for copy loop
148  left_ch = L_buffer;
149  if (mNumChannels == 2) {
150  outR = bf->buffer(1) + bf->mNumFrames;
151  right_ch = R_buffer;
152  }
153  unsigned nsamples = num_samples;
154  if (mMaxDurInSecs > 0.0) { // check for overflow of mMaxSize
155  unsigned tot = this->totalSize();
156  unsigned maxFrames = (unsigned) (mMaxDurInSecs * mFrameRate);
157  if ((tot + nsamples) >= maxFrames) {
158  retVal = false;
159  // We set a check for an absurd situation when badly encoded MP3 files change sample rates half way through the file, changing maxFrames.
160  nsamples = (tot > maxFrames) ? 0 : maxFrames - tot;
161  }
162  }
163  bf->mNumFrames += nsamples; // increment buf ctr
164  while (nsamples--) { // float copy loop
165  *outL++ = scale_MP3F(*left_ch++);
166  if (mNumChannels == 2)
167  *outR++ = scale_MP3F(*right_ch++);
168  }
169  return retVal;
170 }
171 
172 bool DecodedFile::writeBuffer (float * stereo_buffer, unsigned num_samples) {
173  sample * outL, * outR, * inP;
174  bool retVal = true;
175 
176  if (!this->checkBufferStack(num_samples))
177  return false;
178  Buffer * bf = mBuffers.back(); // get buffer from vector
179  outL = bf->buffer(0) + bf->mNumFrames; // get prts for copy loop
180  if (mNumChannels == 2)
181  outR = bf->buffer(1) + bf->mNumFrames;
182  unsigned nsamples = num_samples / mNumChannels;
183  if (mMaxDurInSecs > 0.0) { // check for overflow of mMaxSize
184  unsigned tot = this->totalSize();
185  unsigned maxFrames = (unsigned) (mMaxDurInSecs * mFrameRate);
186  if ((tot + nsamples) >= maxFrames) {
187  retVal = false;
188  nsamples = maxFrames - tot;
189  }
190  }
191  bf->mNumFrames += nsamples; // increment buf ctr
192  inP = stereo_buffer;
193  while (nsamples--) { // float copy loop
194  *outL++ = *inP++;
195  if (mNumChannels == 2)
196  *outR++ = *inP++;
197  }
198  return retVal;
199 }
200 
201 // calc total size
202 
204  unsigned totS = 0;
205  for (BufferVector::iterator it = mBuffers.begin(); it != mBuffers.end(); it++)
206  totS += (*it)->mNumFrames;
207  return totS;
208 }
209 
210 /// merge (concatenate) the buffers into 1 after loading
211 
213  if (mBuffers.empty()) {
214 // logMsg(kLogError, "DecodedFile::mergeBuffers - empty buffer vector");
215  mIsValid = false;
216  return;
217  }
218  Buffer * bf = mBuffers.front();
219  if (bf->mNumFrames <= 0) {
220 // logMsg(kLogError, "DecodedFile::mergeBuffers - empty buffer");
221  mIsValid = false;
222  return;
223  }
224  if (mBuffers.size() > 1) { // if > 1 buffer
225  unsigned totS = this->totalSize();
226  bf = new Buffer(2, totS); // create large buffer
227  bf->allocateBuffers();
228  unsigned offset = 0; // copy loop
229  for (BufferVector::iterator it = mBuffers.begin(); it != mBuffers.end(); it++) {
230  Buffer * b2 = *it;
231  bf->copySamplesFromTo(*b2, offset); // write sample to concat buffer
232  offset += b2->mNumFrames;
233  }
234  }
236  mNumFrames = bf->mNumFrames;
240  mWavetable.copySamplesFrom(*bf); // copy data over to mWavetable
241  mSFInfo = 0;
242  mSndfile = 0;
243  mStart = 0;
244  mStop = mNumFrames;
245  mBytesPerSample = 4;
246  mIsValid = true;
247  mBase = 0;
248  if (mBuffers.size() > 1)
249  delete bf;
250 // mBuffers.clear(); // erase old values
251  for (BufferVector::iterator it = mBuffers.begin(); it != mBuffers.end(); it++)
252  delete *it;
253 }
254 
255 void DecodedFile::openForWrite(SoundFileFormat tformat, unsigned nchannels, unsigned rate, unsigned bitDepth)
256  throw (CException) {
257  throw IOError("Decoded files cannot be written");
258 }
259 
261  throw IOError("Decoded files cannot be written");
262 }
263 
264 // copy next buffer from cache
265 
267 
268 }
269 
270 //
271 //////////////////// MP3File implementation ///////////////////////////////////////////////////////
272 //
273 
274 // simple buffer vector struct with the MP3 file ptr
275 
276 struct vbuffer {
277  unsigned char const * start;
278  unsigned long length;
281 };
282 
283 // temp buffer & file for writing output
284 
285 #ifdef WRITE_MP3_TO_FILE
286 static MP3_SAMP_TYPE * xlate_buffer = NULL;
287 static SNDFILE * sfOutput = NULL;
288 #endif
289 
290 void close_file(int fildes) {
291  close(fildes);
292 }
293 
294 // This is the input callback. The purpose of this callback is to (re)fill
295 // the stream buffer which is to be decoded.
296 
297 static enum mad_flow m3_input(void * data, struct mad_stream * stream) {
298  struct vbuffer * buffer = (vbuffer *) data;
299  if ( ! buffer->length)
300  return MAD_FLOW_STOP;
301  mad_stream_buffer(stream, buffer->start, buffer->length);
302  buffer->length = 0;
303  return MAD_FLOW_CONTINUE;
304 }
305 
306 // This is the output callback function; it scales and writes the decoder output to an AIFF file.
307 
308 static enum mad_flow m3_output(void * data, struct mad_header const * header, struct mad_pcm * pcm) {
309  unsigned int nchannels, nsamples;
310  mad_fixed_t const *left_ch, *right_ch;
311  bool continueDecoding = true;
312  // pcm->samplerate contains the sampling frequency
313  nchannels = pcm->channels;
314  nsamples = pcm->length; // normally 1152
315  left_ch = pcm->samples[0];
316  right_ch = pcm->samples[1];
317 
318 #ifdef WRITE_MP3_TO_FILE
319  MP3_SAMP_TYPE * out_ptr = xlate_buffer;
320  while (nsamples--) {
321  *out_ptr++ = scale_MP3(*left_ch++);
322  if (nchannels == 2)
323  *out_ptr++ = scale_MP3(*right_ch++);
324  }
325  MP3_WRITE(sfOutput, xlate_buffer, pcm->length); // libsndfile sf_writef_short function
326 
327 #else // else write to buffer
328 
329  struct vbuffer * vbf = (vbuffer *) data;
330  MP3File * mp3File = vbf->mp3File;
331  if (mp3File->numChannels() != nchannels)
332  mp3File->setNumChannels(nchannels);
333 
334  continueDecoding = mp3File->writeBuffer((int *) left_ch, (int *) right_ch, nsamples);
335 #endif
336 
337  return continueDecoding ? MAD_FLOW_CONTINUE : MAD_FLOW_STOP;
338 }
339 
340 // This is the error callback function. It is called whenever a decoding
341 // error occurs. The error is indicated by stream->error; the list of
342 // possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
343 // header file.
344 
345 static enum mad_flow m3_error(void *data, struct mad_stream *stream, struct mad_frame *frame) {
346 // struct vbuffer * buffer = (vbuffer *) data;
347 // fprintf(stderr, "\tMP3 decoding error 0x%04x (%s) at byte offset %u\n",
348 // stream->error, mad_stream_errorstr(stream),
349 // stream->this_frame - buffer->start);
350  return MAD_FLOW_CONTINUE;
351 }
352 
353 // Header handler
354 
355 static enum mad_flow m3_header(void *data, struct mad_header const *header) {
356 // fprintf(stderr, "SR: %d\n", header->samplerate);
357  struct vbuffer * vbf = (vbuffer *) data;
358  MP3File * mp3File = vbf->mp3File;
359  mp3File->mMP3Rate = header->samplerate;
360  mp3File->setFrameRate(header->samplerate);
361  return MAD_FLOW_CONTINUE;
362 }
363 
364 // This is the function that performs all the decoding.
365 // It instantiates a decoder object and configures it with the input,
366 // output, and error callback functions above. A single call to
367 // mad_decoder_run() continues until a callback function returns
368 // MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
369 // signal an error).
370 //
371 // Note that this assumes:
372 // a stereo input file
373 // a file name with a "/" and a ".mp3"
374 
376  int inFile;
377  struct stat stat;
378 // fprintf(stderr, "\tMP3File::decodeMP3() \"%s\"n", mPath.c_str());
379  inFile = open(mPath.c_str(), O_RDONLY); // open & test in file
380  if (fstat(inFile, &stat) == -1 || stat.st_size == 0)
381 // throw IOError("Sound file MP3 open error");
382  return -1;
383  // memory-map input file -- ToDo: this is UNIX-specific
384  void * mapped_data = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, inFile, 0);
385 
386 #ifdef WRITE_MP3_TO_FILE
387  mTempPath = new char[CSL_LINE_LEN]; // create temp file name
388  if (mTempDir)
389  MP3_TEMP_NAME(mPath.c_str(), mTempPath, mTempDir)
390  else
391  AIFF_TEMP_NAME(mPath.c_str(), mTempPath)
392  // create output sound file
393  mTempFile = new LSoundFile(mTempPath, -1, -1, false);
394  mTempFile->openForWrite(kSoundFileFormatAIFF, 2, CGestalt::frameRate(), MP3_DEPTH);
395 #endif
396 
397  struct vbuffer vbf; // now set up libMAD decoder
398  struct mad_decoder decoder;
399  int result = 0;
400  vbf.start = (unsigned char const *) mapped_data;
401  vbf.length = stat.st_size;
402  vbf.mp3File = this;
403  // create first buffer
404  Buffer * bf = new Buffer(2, CGestalt::sndFileFrames());
405 
406  bf->allocateBuffers();
407  bf->mNumFrames = 0;
408  mBuffers.push_back(bf); // store buffer
409  vbf.outBufs = & mBuffers;
410 #ifdef WRITE_MP3_TO_FILE // set up the statics
411  sfOutput = mTempFile->sndFile();
412  xlate_buffer = new MP3_SAMP_TYPE[CGestalt::maxBufferFrames()];
413 #endif
414 
415  mad_decoder_init(&decoder, &vbf, // configure input, output, and error functions
416  m3_input,
417  m3_header, /* header */
418  0 /* filter */,
419  m3_output, /* output function does the work */
420  m3_error,
421  0 /* message */);
422 
423  // start decoding
424  result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
425 
426  mad_decoder_finish(&decoder); // release the decoder
427 #ifdef CSL_DEBUG
428  logMsg("MP3 decoder returned %d; temp file %s", result, tempPath);
429 #endif
430  munmap(mapped_data, stat.st_size);
431  close_file(inFile);
432 #ifdef WRITE_MP3_TO_FILE
433  mTempFile->close();
434  delete mTempFile;
435 #else // handle MP3 buffer vector
436  this->mergeBuffers();
437 #endif
438  return result;
439 }
440 
441 /// MP3File constructors
442 
443 MP3File::MP3File(string tpath, int start, int stop)
444  : DecodedFile(tpath, start, stop, 0) {
445  mMode = kSoundFileClosed;
446  mMP3Rate = DEFAULT_MP3_RATE; // default rate
447 // fprintf(stderr, "\tMP3File::c'tor \"%s\" \n", path.c_str());
448  openForRead(); // read and cache whole file
449 }
450 
451 MP3File::MP3File(float maxDurInSecs, string tpath)
452  : DecodedFile(tpath, -1, -1, maxDurInSecs) {
453  mMode = kSoundFileClosed;
454  mMP3Rate = DEFAULT_MP3_RATE; // default rate
455 // fprintf(stderr, "\tMP3File::c'tor \"%s\" \n", path.c_str());
456  openForRead(); // read and cache whole file
457 }
458 
460 
461 /// openForRead decodes MP3 file into a temp file and plugs that into the receiver
462 
464 // fprintf(stderr, "\tMP3File::openForRead() \"%s\"n", mTempPath);
465 // mTempDir = NULL;
466  if (mMode == kSoundFileRead)
467  return;
469 
470  if(!this->readTags()) { // read the ID3 tags
471 // logMsg(kLogError, "No tags in MP3 sound file");
472 // return;
473  }
474  int ret = this->decodeMP3(); // decode the MP3 using libMAD
475  if (ret < 0) {
476  mIsValid = false;
477  return;
478  }
479  mFrameRate = mMP3Rate; // ToDo: this can be a problem if the sample rate changes during the file.
480 
481 // if (mFrameRate != DEFAULT_MP3_RATE) {
482 //// if ((mMP3Rate != 48000) && (mMP3Rate != 32000)) {
483 // if ((mMP3Rate < 20000) || (mMP3Rate > 50000)) {
484 // logMsg(kLogError, "Unsupported MP3 file sample rate: %d", mMP3Rate);
485 // throw IOError("Unsupported MP3 file sample rate");
486 // }
487 //#ifdef CSL_USE_SRConv
488 // this->convertRate(mTempPath, mMP3Rate, DEFAULT_MP3_RATE);
489 //#endif
490 // }
491 #ifdef WRITE_MP3_TO_FILE
492  mTempFile = new LSoundFile(mTempPath);
493  mTempFile->openForRead();
494  mSFInfo = mTempFile->sfInfo();
495  mSndfile = mTempFile->sndFile();
496  mMode = mTempFile->mode();
497  mStart = mTempFile->startFrame();
498  mStop = mTempFile->stopFrame();
499  mIsValid = mTempFile->isValid();
500  mNumChannels = mTempFile->numChannels();
501  mNumFrames = mTempFile->duration();
502  mWavetable.copyFrom(mTempFile->mWavetable);
503 #endif
504 }
505 
506 #endif // USE_libMAD
507 
508 #ifdef USE_libFAAD // Support AAC/MP4 file reading using libFAAD
509 
510 //
511 //////////////////// AACFile implementation /////////////////////////////////////////////////////////////////////
512 //
513 
514 static int fill_buffer(aac_buffer *b) {
515  int bread;
516  if (b->bytes_consumed > 0) {
517  if (b->bytes_into_buffer) {
518  memmove((void*)b->buffer, (void*)(b->buffer + b->bytes_consumed),
519  b->bytes_into_buffer*sizeof(unsigned char));
520  }
521  if (!b->at_eof) {
522  bread = fread((void*)(b->buffer + b->bytes_into_buffer), 1,
523  b->bytes_consumed, b->infile);
524 
525  if (bread != b->bytes_consumed)
526  b->at_eof = 1;
527 
528  b->bytes_into_buffer += bread;
529  }
530  b->bytes_consumed = 0;
531  if (b->bytes_into_buffer > 3) {
532  if (memcmp(b->buffer, "TAG", 3) == 0)
533  b->bytes_into_buffer = 0;
534  }
535  if (b->bytes_into_buffer > 11) {
536  if (memcmp(b->buffer, "LYRICSBEGIN", 11) == 0)
537  b->bytes_into_buffer = 0;
538  }
539  if (b->bytes_into_buffer > 8) {
540  if (memcmp(b->buffer, "APETAGEX", 8) == 0)
541  b->bytes_into_buffer = 0;
542  }
543  }
544  return 1;
545 }
546 
547 static void advance_buffer(aac_buffer *b, int bytes) {
548  b->file_offset += bytes;
549  b->bytes_consumed = bytes;
550  b->bytes_into_buffer -= bytes;
551  if (b->bytes_into_buffer < 0)
552  b->bytes_into_buffer = 0;
553 }
554 
555 static int adts_sample_rates[] =
556 {96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000,7350,0,0,0};
557 
558 static int adts_parse(aac_buffer *b, int *bitrate, float *length) {
559  int frames, frame_length;
560  int t_framelength = 0;
561  int samplerate;
562  float frames_per_sec, bytes_per_frame;
563 
564  /* Read all frames to ensure correct time and bitrate */
565  for (frames = 0; /* */; frames++) {
566  fill_buffer(b);
567  if (b->bytes_into_buffer > 7) {
568  /* check syncword */
569  if (!((b->buffer[0] == 0xFF)&&((b->buffer[1] & 0xF6) == 0xF0)))
570  break;
571  if (frames == 0)
572  samplerate = adts_sample_rates[(b->buffer[2]&0x3c)>>2];
573  frame_length = ((((unsigned int)b->buffer[3] & 0x3)) << 11)
574  | (((unsigned int)b->buffer[4]) << 3) | (b->buffer[5] >> 5);
575  t_framelength += frame_length;
576  if (frame_length > b->bytes_into_buffer)
577  break;
578 
579  advance_buffer(b, frame_length);
580  } else {
581  break;
582  }
583  }
584  frames_per_sec = (float)samplerate/1024.0f;
585  if (frames != 0)
586  bytes_per_frame = (float)t_framelength/(float)(frames*1000);
587  else
588  bytes_per_frame = 0;
589  *bitrate = (int)(8. * bytes_per_frame * frames_per_sec + 0.5);
590  if (frames_per_sec != 0)
591  *length = (float)frames/frames_per_sec;
592  else
593  *length = 1;
594  return 1;
595 }
596 
597 uint32_t read_callback(void *user_data, void *buffer, uint32_t length) {
598  return fread(buffer, 1, length, (FILE*)user_data);
599 }
600 
601 uint32_t seek_callback(void *user_data, uint64_t position) {
602  return fseek((FILE*)user_data, position, SEEK_SET);
603 }
604 
605 ///* MicroSoft channel definitions */
606 //#define SPEAKER_FRONT_LEFT 0x1
607 //#define SPEAKER_FRONT_RIGHT 0x2
608 //#define SPEAKER_FRONT_CENTER 0x4
609 //#define SPEAKER_LOW_FREQUENCY 0x8
610 //#define SPEAKER_BACK_LEFT 0x10
611 //#define SPEAKER_BACK_RIGHT 0x20
612 //#define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
613 //#define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
614 //#define SPEAKER_BACK_CENTER 0x100
615 //#define SPEAKER_SIDE_LEFT 0x200
616 //#define SPEAKER_SIDE_RIGHT 0x400
617 //#define SPEAKER_TOP_CENTER 0x800
618 //#define SPEAKER_TOP_FRONT_LEFT 0x1000
619 //#define SPEAKER_TOP_FRONT_CENTER 0x2000
620 //#define SPEAKER_TOP_FRONT_RIGHT 0x4000
621 //#define SPEAKER_TOP_BACK_LEFT 0x8000
622 //#define SPEAKER_TOP_BACK_CENTER 0x10000
623 //#define SPEAKER_TOP_BACK_RIGHT 0x20000
624 //#define SPEAKER_RESERVED 0x80000000
625 //
626 //static long aacChannelConfig2wavexChannelMask(NeAACDecFrameInfo *hInfo) {
627 // if (hInfo->channels == 6 && hInfo->num_lfe_channels) {
628 // return SPEAKER_FRONT_LEFT + SPEAKER_FRONT_RIGHT +
629 // SPEAKER_FRONT_CENTER + SPEAKER_LOW_FREQUENCY +
630 // SPEAKER_BACK_LEFT + SPEAKER_BACK_RIGHT;
631 // } else {
632 // return 0;
633 // }
634 //}
635 //
636 //static const char * position2string(int position) {
637 // switch (position) {
638 // case FRONT_CHANNEL_CENTER: return "Center front";
639 // case FRONT_CHANNEL_LEFT: return "Left front";
640 // case FRONT_CHANNEL_RIGHT: return "Right front";
641 // case SIDE_CHANNEL_LEFT: return "Left side";
642 // case SIDE_CHANNEL_RIGHT: return "Right side";
643 // case BACK_CHANNEL_LEFT: return "Left back";
644 // case BACK_CHANNEL_RIGHT: return "Right back";
645 // case BACK_CHANNEL_CENTER: return "Center back";
646 // case LFE_CHANNEL: return "LFE";
647 // case UNKNOWN_CHANNEL: return "Unknown";
648 // default: return "";
649 // }
650 // return "";
651 //}
652 //
653 //static void print_channel_info(NeAACDecFrameInfo *frameInfo) {
654 // /* print some channel info */
655 // int i;
656 // long channelMask = aacChannelConfig2wavexChannelMask(frameInfo);
657 //
658 // logMsg(" ---------------------");
659 // if (frameInfo->num_lfe_channels > 0) {
660 // logMsg(" | Config: %2d.%d Ch |", frameInfo->channels-frameInfo->num_lfe_channels, frameInfo->num_lfe_channels);
661 // } else {
662 // logMsg(" | Config: %2d Ch |", frameInfo->channels);
663 // }
664 // if (channelMask)
665 // logMsg(" WARNING: channels are reordered according to");
666 // else
667 // logMsg("");
668 // logMsg(" ---------------------");
669 // if (channelMask)
670 // logMsg(" MS defaults defined in WAVE_FORMAT_EXTENSIBLE");
671 // else
672 // logMsg("");
673 // logMsg(" | Ch | Position |");
674 // logMsg(" ---------------------");
675 // for (i = 0; i < frameInfo->channels; i++) {
676 // logMsg(" | %.2d | %-14s |", i, position2string((int)frameInfo->channel_position[i]));
677 // }
678 // logMsg(" ---------------------");
679 // logMsg("");
680 //}
681 
682 //static int FindAdtsSRIndex(int sr) {
683 // int i;
684 // for (i = 0; i < 16; i++) {
685 // if (sr == adts_sample_rates[i])
686 // return i;
687 // }
688 // return 16 - 1;
689 //}
690 
691 /* find AAC track */
692 
693 static int GetAACTrack(mp4ff_t *infile) {
694  int i, rc;
695  int numTracks = mp4ff_total_tracks(infile);
696 
697  for (i = 0; i < numTracks; i++) {
698  unsigned char * buff = NULL;
699  unsigned int buff_size = 0;
700  mp4AudioSpecificConfig mp4ASC;
701  mp4ff_get_decoder_config(infile, i, &buff, &buff_size);
702  if (buff) {
703  rc = NeAACDecAudioSpecificConfig(buff, buff_size, &mp4ASC);
704  free(buff);
705  if (rc < 0)
706  continue;
707  return i;
708  }
709  }
710  return -1; /* can't decode this */
711 }
712 
713 static const unsigned long srates[] =
714 { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000 };
715 
716 /* print some mp4 file info */
717 
718 void MP4File::printMP4(mp4ff_t * infile, int track, unsigned *song_length) {
719  logMsg("%s file info:", mPath.c_str());
720  char *tag = NULL, *item = NULL;
721  int k, j;
722  const char *ot[6] = { "NULL", "MAIN AAC", "LC AAC", "SSR AAC", "LTP AAC", "HE AAC" };
723  long samples = mp4ff_num_samples(infile, track);
724  float f = 1024.0;
725  float seconds;
726  if (mp4ASC.sbr_present_flag == 1)
727  f = f * 2.0;
728  seconds = (float)samples * (float)(f-1.0) / (float)mp4ASC.samplingFrequency;
729  *song_length = (unsigned) samples * (unsigned) (f - 1);
730  logMsg("%s\t%.3f secs, %d ch, %d Hz", ot[(mp4ASC.objectTypeIndex > 5) ? 0 : mp4ASC.objectTypeIndex],
731  seconds, mp4ASC.channelsConfiguration, mp4ASC.samplingFrequency);
732 #define PRINT_MP4_METADATA
733 #ifdef PRINT_MP4_METADATA
734  j = mp4ff_meta_get_num_items(infile);
735  for (k = 0; k < j; k++) {
736  if (mp4ff_meta_get_by_index(infile, k, &item, &tag)) {
737  if (item != NULL && tag != NULL) {
738  logMsg("\t\t%s: %s", item, tag);
739  free(item); item = NULL;
740  free(tag); tag = NULL;
741  }
742  }
743  }
744  if (j > 0) logMsg("");
745 #endif
746 }
747 
748 ///////////////////////////////////////////////////////////////////////////////////////
749 
750 MP4File::MP4File(string tpath, int start, int stop)
751  : DecodedFile(tpath, start, stop, 0) {
752  mMode = kSoundFileClosed;
753  openForRead(); // read and cache whole file
754 }
755 
756 MP4File::MP4File(float maxDurInSecs, string tpath)
757  : DecodedFile(tpath, -1, -1, maxDurInSecs) {
758  mMode = kSoundFileClosed;
759  openForRead(); // read and cache whole file
760 }
761 
762 MP4File::~MP4File() {
763  // structs are auto-free'd
764 }
765 
766 // open file and get stats
767 
768 void MP4File::openForRead() throw (CException) {
769  unsigned char header[8];
770  int mp4file = 0;
771  Status result;
772  if (mMode == kSoundFileRead)
773  return;
774  mMode = kSoundFileRead;
775 
776 // unsigned long cap = NeAACDecGetCapabilities();
777 // if (cap & FIXED_POINT_CAP)
778 // logMsg("Fixed point version");
779 // else
780 // logMsg("Floating point version");
781 
782  /* check for mp4 file */
783  FILE * hMP4File = fopen(mPath.c_str(), "rb");
784  if (!hMP4File) {
785  logMsg("Error opening file (fopen): %s", mPath.c_str());
786  return;
787  }
788  fread(header, 1, 8, hMP4File);
789  rewind(hMP4File);
790  // logMsg("Header: %.4s - %.4s", &header[0], &header[4]);
791  // if (header[4] == 'f' && header[5] == 't' && header[6] == 'y' && header[7] == 'p')
792  if (memcmp(&header[4], "ftyp", 4) == 0)
793  mp4file = 1;
794 
795  if (mp4file) // now call the right decoder method
796  result = this->decodeMP4file(hMP4File);
797  else
798  result = this->decodeAACfile(hMP4File);
799 
800  if (result != kOk) {
801 // logMsg("Error opening file (decode): %s", mPath.c_str());
802 // return;
803  }
804  fclose(hMP4File);
805  this->mergeBuffers();
806 
807  if (mIsValid) {
808  this->readTags(); // read the ID3 tags
809  }
810 }
811 
812 // decode
813 
814 Status MP4File::decodeAACfile(FILE * inFile) throw (CException){
815  float length;
816  int bread, fileread;
817  int header_type;
818  int bitrate;
819  int tagsize;
820  unsigned long samplerate;
821  unsigned char tchannels;
822  void *sample_buffer;
823 // unsigned char *adtsData;
824 // int adtsDataSize;
825 
826  memset(&aacBuff, 0, sizeof(aac_buffer));
827  aacBuff.infile = inFile;
828  fseek(aacBuff.infile, 0, SEEK_END);
829  fileread = ftell(aacBuff.infile);
830  fseek(aacBuff.infile, 0, SEEK_SET);
831 
832  if ( ! (aacBuff.buffer = (unsigned char *) malloc(FAAD_MIN_STREAMSIZE * MAX_CHANNELS))) {
833  logMsg("Memory allocation error");
834  return kErr;
835  }
836  memset(aacBuff.buffer, 0, FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
837 
838  bread = fread(aacBuff.buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, aacBuff.infile);
839  aacBuff.bytes_into_buffer = bread;
840  aacBuff.bytes_consumed = 0;
841  aacBuff.file_offset = 0;
842 
843  if (bread != FAAD_MIN_STREAMSIZE*MAX_CHANNELS)
844  aacBuff.at_eof = 1;
845 
846  tagsize = 0;
847  if (!memcmp(aacBuff.buffer, "ID3", 3)) {
848  /* high bit is not used */
849  tagsize = (aacBuff.buffer[6] << 21) | (aacBuff.buffer[7] << 14) |
850  (aacBuff.buffer[8] << 7) | (aacBuff.buffer[9] << 0);
851 
852  tagsize += 10;
853  advance_buffer(&aacBuff, tagsize);
854  fill_buffer(&aacBuff);
855  }
856  hDecoder = NeAACDecOpen();
857 
858  /* Set the default object type and samplerate */
859  /* This is useful for RAW AAC files */
860  config = NeAACDecGetCurrentConfiguration(hDecoder);
861  config->defObjectType = LC;
862  config->outputFormat = 4;
863  config->downMatrix = 1;
864  config->useOldADTSFormat = 0;
865 // config->dontUpSampleImplicitSBR = 1;
866  NeAACDecSetConfiguration(hDecoder, config);
867 
868  /* get AAC infos for printing */
869  header_type = 0;
870  if ((aacBuff.buffer[0] == 0xFF) && ((aacBuff.buffer[1] & 0xF6) == 0xF0)) {
871  adts_parse(&aacBuff, &bitrate, &length);
872  fseek(aacBuff.infile, tagsize, SEEK_SET);
873 
874  bread = fread(aacBuff.buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, aacBuff.infile);
875  if (bread != FAAD_MIN_STREAMSIZE*MAX_CHANNELS)
876  aacBuff.at_eof = 1;
877  else
878  aacBuff.at_eof = 0;
879  aacBuff.bytes_into_buffer = bread;
880  aacBuff.bytes_consumed = 0;
881  aacBuff.file_offset = tagsize;
882  header_type = 1;
883  } else if (memcmp(aacBuff.buffer, "ADIF", 4) == 0) {
884  int skip_size = (aacBuff.buffer[4] & 0x80) ? 9 : 0;
885  bitrate = ((unsigned int)(aacBuff.buffer[4 + skip_size] & 0x0F)<<19) |
886  ((unsigned int)aacBuff.buffer[5 + skip_size]<<11) |
887  ((unsigned int)aacBuff.buffer[6 + skip_size]<<3) |
888  ((unsigned int)aacBuff.buffer[7 + skip_size] & 0xE0);
889 
890  length = (float) fileread;
891  if (length != 0) {
892  length = ((float)length*8.0f)/((float)bitrate) + 0.5f;
893  }
894  bitrate = (int)((float)bitrate/1000.0f + 0.5f);
895  header_type = 2;
896  }
897 
898  fill_buffer(&aacBuff);
899  if ((bread = NeAACDecInit(hDecoder, aacBuff.buffer,
900  aacBuff.bytes_into_buffer, &samplerate, &tchannels)) < 0) {
901  /* If some error initializing occured, skip the file */
902  logMsg("Error initializing decoder library.");
903  if (aacBuff.buffer)
904  free(aacBuff.buffer);
905  NeAACDecClose(hDecoder);
906  fclose(aacBuff.infile);
907  return kErr;
908  }
909  advance_buffer(&aacBuff, bread);
910  fill_buffer(&aacBuff);
911 
912  /* print AAC file info */
913  logMsg("%s file info:", mPath.c_str());
914  switch (header_type) {
915  case 0:
916  logMsg("RAW");
917  break;
918  case 1:
919  logMsg("ADTS, %.3f sec, %d kbps, %d Hz", length, bitrate, samplerate);
920  break;
921  case 2:
922  logMsg("ADIF, %.3f sec, %d kbps, %d Hz", length, bitrate, samplerate);
923  break;
924  }
925 
926  do { // main read loop
927  sample_buffer = NeAACDecDecode(hDecoder, &frameInfo, aacBuff.buffer, aacBuff.bytes_into_buffer);
928 
929  /* update buffer indices */
930  advance_buffer(&aacBuff, frameInfo.bytesconsumed);
931 
932  if (frameInfo.error > 0) {
933  logMsg("Error: %s",
934  NeAACDecGetErrorMessage(frameInfo.error));
935  }
936  if ( ! writeBuffer((float *) sample_buffer, frameInfo.samples) )
937  break;
938  /* fill buffer */
939  fill_buffer(&aacBuff);
940  if (aacBuff.bytes_into_buffer == 0)
941  sample_buffer = NULL; /* to make sure it stops now */
942 
943  } while (sample_buffer != NULL); // end of loop
944 
945  NeAACDecClose(hDecoder);
946  if (aacBuff.buffer)
947  free(aacBuff.buffer);
948  return (frameInfo.error ? kErr : kOk);
949 }
950 
951 // read and decode an MP4 file
952 
953 Status MP4File::decodeMP4file(FILE * inFile) throw (CException) {
954  int track;
955  void *sample_buffer;
956  long sampleId, numSamples;
957  mp4ff_t *mp4File;
958 // FILE *adtsFile;
959 // unsigned char *adtsData;
960 // int adtsDataSize;
961  unsigned char *tbuffer = NULL;
962  unsigned int buffer_size;
963  unsigned long samplerate;
964  unsigned char tchannels;
965  /* for gapless decoding */
966  unsigned int useAacLength = 1;
967  unsigned int initial = 1;
968  unsigned int framesize;
969  unsigned long timescale;
970 
971  /* initialise the callback structure */
972  mp4ff_callback_t * mp4cb = (mp4ff_callback_t *) malloc(sizeof(mp4ff_callback_t));
973  mp4cb->read = read_callback;
974  mp4cb->seek = seek_callback;
975  mp4cb->user_data = inFile;
976 
977  hDecoder = NeAACDecOpen();
978 
979  /* Set configuration */
980  config = NeAACDecGetCurrentConfiguration(hDecoder);
981  config->outputFormat = 4;
982  config->downMatrix = 1;
983  // config->dontUpSampleImplicitSBR = 1;
984  NeAACDecSetConfiguration(hDecoder, config);
985 
986  mp4File = mp4ff_open_read(mp4cb);
987 
988  if ((track = GetAACTrack(mp4File)) < 0) {
989  logMsg("Warning: Unable to find correct AAC sound track in the MP4 file.");
990  NeAACDecClose(hDecoder);
991  mp4ff_close(mp4File);
992  free(mp4cb);
993  fclose(inFile);
994  return kErr;
995  }
996  tbuffer = NULL;
997  buffer_size = 0;
998  mp4ff_get_decoder_config(mp4File, track, &tbuffer, &buffer_size);
999 
1000  if (NeAACDecInit2(hDecoder, tbuffer, buffer_size, &samplerate, &tchannels) < 0) {
1001  /* If some error initializing occured, skip the file */
1002  logMsg("Error initializing decoder library.");
1003  NeAACDecClose(hDecoder);
1004  mp4ff_close(mp4File);
1005  free(mp4cb);
1006  fclose(inFile);
1007  return kErr;
1008  }
1009  timescale = mp4ff_time_scale(mp4File, track);
1010  framesize = 1024;
1011  useAacLength = 0;
1012  mFrameRate = samplerate;
1013  mNumChannels = tchannels;
1014  if (tbuffer) {
1015  if (NeAACDecAudioSpecificConfig(tbuffer, buffer_size, &mp4ASC) >= 0) {
1016  if (mp4ASC.frameLengthFlag == 1)
1017  framesize = 960;
1018  if (mp4ASC.sbr_present_flag == 1)
1019  framesize *= 2;
1020  }
1021  free(tbuffer);
1022  }
1023  numSamples = mp4ff_num_samples(mp4File, track);
1024 
1025  unsigned songLength;
1026 // if (gConfig->Verbose())
1027 // this->printMP4(mp4File, track, &songLength);
1028 
1029  this->addBuffer(mNumChannels, (songLength + mFrameRate));
1030  // IO loop
1031  for (sampleId = 0; sampleId < numSamples; sampleId++) {
1032  int rc;
1033  long dur;
1034  unsigned int sample_count;
1035  unsigned int delay = 0;
1036  tbuffer = NULL;
1037  buffer_size = 0;
1038  /* get acces unit from MP4 file */
1039  dur = mp4ff_get_sample_duration(mp4File, track, sampleId);
1040  rc = mp4ff_read_sample(mp4File, track, sampleId, &tbuffer, &buffer_size);
1041  if (rc == 0) {
1042  logMsg("Reading from MP4 file failed.");
1043  NeAACDecClose(hDecoder);
1044  mp4ff_close(mp4File);
1045  free(mp4cb);
1046  fclose(inFile);
1047  return kErr;
1048  } // decode a block here, returns the sample buffer
1049  sample_buffer = NeAACDecDecode(hDecoder, &frameInfo, tbuffer, buffer_size);
1050  if (tbuffer) free(tbuffer);
1051 
1052  if (sampleId == 0) dur = 0;
1053 
1054  if (useAacLength || (timescale != samplerate)) {
1055  sample_count = frameInfo.samples;
1056  } else {
1057  sample_count = (unsigned int)(dur * frameInfo.channels);
1058  if (sample_count > frameInfo.samples)
1059  sample_count = frameInfo.samples;
1060 
1061  if (!useAacLength && !initial && (sampleId < numSamples/2) && (sample_count != frameInfo.samples)) {
1062  logMsg("MP4 seems to have incorrect frame duration, using values from AAC data.");
1063  useAacLength = 1;
1064  sample_count = frameInfo.samples;
1065  }
1066  }
1067  if (initial && (sample_count < framesize*frameInfo.channels) && (frameInfo.samples > sample_count))
1068  delay = frameInfo.samples - sample_count;
1069 // if ((sampleId == 0) && gConfig->Verbose() && !frameInfo.error) { // print some channel info
1070 // print_channel_info(&frameInfo);
1071 // }
1072  if ((frameInfo.error == 0) && (sample_count > 0))
1073  if ( ! writeBuffer((float *) sample_buffer, sample_count))
1074  break;
1075 
1076  if (frameInfo.error > 0) {
1077  logMsg("Warning: %s", NeAACDecGetErrorMessage(frameInfo.error));
1078  }
1079  }
1080  NeAACDecClose(hDecoder);
1081  mp4ff_close(mp4File);
1082  free(mp4cb);
1083  return (frameInfo.error ? kErr : kOk);
1084 }
1085 
1086 #endif