CLAM-Development  1.4.0
MpegBitstream.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "MpegBitstream.hxx"
23 #include "Assert.hxx"
24 #include <cstring>
25 #include <iostream>
26 #include <sstream>
27 #include <iomanip>
28 
29 namespace CLAM
30 {
31 
32 namespace AudioCodecs
33 {
34  // MRJ: This number was extracted from Underbit's own
35  // mp3 player code ( mad-0.14.2b2, player.c:86 ). As they
36  // say: 40000 bytes is worth 2.5 secs of audio on a stream
37  // with 128 kpbs, 1s with 320 kbps
38  const int MpegBitstream::mInputBufferSize = 5*8192;
39 
40  // Taken from alsaplayer
41  static std::string MadError(enum mad_error error)
42  {
43  switch (error) {
44  case MAD_ERROR_BUFLEN:
45  case MAD_ERROR_BUFPTR:
46  /* these errors are handled specially and/or should not occur */
47  break;
48 
49  case MAD_ERROR_NOMEM: return ("not enough memory");
50  case MAD_ERROR_LOSTSYNC: return ("lost synchronization");
51  case MAD_ERROR_BADLAYER: return ("reserved header layer value");
52  case MAD_ERROR_BADBITRATE: return ("forbidden bitrate value");
53  case MAD_ERROR_BADSAMPLERATE: return ("reserved sample frequency value");
54  case MAD_ERROR_BADEMPHASIS: return ("reserved emphasis value");
55  case MAD_ERROR_BADCRC: return ("CRC check failed");
56  case MAD_ERROR_BADBITALLOC: return ("forbidden bit allocation value");
57  case MAD_ERROR_BADSCALEFACTOR: return ("bad scalefactor index");
58  case MAD_ERROR_BADFRAMELEN: return ("bad frame length");
59  case MAD_ERROR_BADBIGVALUES: return ("bad big_values count");
60  case MAD_ERROR_BADBLOCKTYPE: return ("reserved block_type");
61  case MAD_ERROR_BADSCFSI: return ("bad scalefactor selection info");
62  case MAD_ERROR_BADDATAPTR: return ("bad main_data_begin pointer");
63  case MAD_ERROR_BADPART3LEN: return ("bad audio data length");
64  case MAD_ERROR_BADHUFFTABLE: return ("bad Huffman table select");
65  case MAD_ERROR_BADHUFFDATA: return ("Huffman data overrun");
66  case MAD_ERROR_BADSTEREO: return ("incompatible block_type for JS");
67  default:;
68  }
69  std::ostringstream os;
70  os << "error 0x" << std::setbase(16) << std::setfill('4') << std::setw(4) << error;
71  return os.str();
72  }
73 
75  : mpFile(NULL)
76  {
77  mInputBuffer = new unsigned char[mInputBufferSize];
78  }
79 
81  {
82  if ( mInputBuffer )
83  delete [] mInputBuffer;
84  }
85 
86  void MpegBitstream::Init( FILE* fp )
87  {
88  mpFile = fp;
89  Init();
90  }
91 
93  {
94  mad_stream_init( &mBitstream );
95  mad_frame_init( &mCurrentFrame );
96  mad_synth_init( &mMpegSynth );
97  mad_timer_reset( &mStreamTimer );
98  mFatalError = false;
99  }
100 
102  {
103  mad_synth_finish( &mMpegSynth );
104  mad_frame_finish( &mCurrentFrame );
105  mad_stream_finish( &mBitstream );
106 
107  return (TTime)mad_timer_count( mStreamTimer, MAD_UNITS_MILLISECONDS );
108  }
109 
111  {
112  if ( feof( mpFile ) ) // no more frames
113  return true;
114  return false;
115  }
116 
118  {
119  return mFatalError || ferror(mpFile)!=0;
120  }
121 
123  {
124  bool firstFrameAfterSeek = mBitstream.buffer == NULL;
125  bool lastDecodeNeededMoreData = mBitstream.error == MAD_ERROR_BUFLEN;
126  if ( not firstFrameAfterSeek and not lastDecodeNeededMoreData) return true;
127 
128  TSize remaining = 0;
129  if (not firstFrameAfterSeek)
130  {
131  remaining = mBitstream.bufend - mBitstream.next_frame;
132  memmove( mInputBuffer, mBitstream.next_frame, remaining );
133  }
134  unsigned char * readStart = mInputBuffer + remaining;
135  TSize readSize = mInputBufferSize - remaining;
136  mBufferFileOffset = ftell(mpFile) - remaining;
137  TSize actuallyRead = fread( readStart, sizeof(unsigned char), readSize, mpFile );
138 
139  if ( actuallyRead == 0 ) return false; // Eof or ferror
140  // Less bytes than expected were read, add buffer guard
141  if ( actuallyRead < readSize )
142  {
143  CLAM_ASSERT( readStart + actuallyRead + MAD_BUFFER_GUARD <= mInputBuffer + mInputBufferSize,
144  "Whoops! no room left for buffer guard bytes" );
145  // Add mad buffer guard
146  memset(readStart+actuallyRead, 0, MAD_BUFFER_GUARD);
147  actuallyRead += MAD_BUFFER_GUARD;
148  }
149 
150  mad_stream_buffer( &mBitstream, mInputBuffer, actuallyRead+remaining );
151  mBitstream.error = MAD_ERROR_NONE; // DGG: Why that?
152  return true;
153  }
154 
158  {
159  while( not ferror(mpFile) )
160  {
161  if (not EnsureEnoughBufferData()) return false;
162 
163  int error = mad_frame_decode(&mCurrentFrame, &mBitstream);
164  if (error != -1)
165  {
166  // frame was decoded right
167  // we add this frame duration to the bitstream internal timer
168  mad_timer_add( &mStreamTimer, mCurrentFrame.header.duration );
169  return true;
170  }
171 
172  if ( MAD_RECOVERABLE( mBitstream.error ) )
173  {
174 // std::cerr << "MP3 recoverable error ignored: " << MadError(mBitstream.error) << std::endl;
175  continue; // Recoverable error, ignore
176  }
177  if ( mBitstream.error == MAD_ERROR_BUFLEN ) continue; // Not enough data, take more
178  // Some fatal error happened!
179  mFatalError = true;
180  std::cerr << "MP3 fatal error: " << MadError(mBitstream.error) << std::endl;
181  return false;
182  }
183  return false; // file error
184  }
185 
187  {
188  unsigned long frameOffset = mBitstream.this_frame - mInputBuffer;
189  return mBufferFileOffset + frameOffset;
190  }
191 
193  {
194  mad_synth_frame( &mMpegSynth, &mCurrentFrame );
195  }
196 
197  struct mad_frame& MpegBitstream::CurrentFrame()
198  {
199  return mCurrentFrame;
200  }
201 
203  {
204  return mMpegSynth;
205  }
206 
207 }
208 
209 }
210