liveMedia/AMRAudioFileSource.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2008 Live Networks, Inc.  All rights reserved.
00018 // A source object for AMR audio files (as defined in RFC 3267, section 5)
00019 // Implementation
00020 
00021 #include "AMRAudioFileSource.hh"
00022 #include "InputFile.hh"
00023 #include "GroupsockHelper.hh"
00024 
00026 
00027 AMRAudioFileSource*
00028 AMRAudioFileSource::createNew(UsageEnvironment& env, char const* fileName) {
00029   FILE* fid = NULL;
00030   Boolean magicNumberOK = True;
00031   do {
00032 
00033     fid = OpenInputFile(env, fileName);
00034     if (fid == NULL) break;
00035 
00036     // Now, having opened the input file, read the first few bytes, to
00037     // check the required 'magic number':
00038     magicNumberOK = False; // until we learn otherwise
00039     Boolean isWideband = False; // by default
00040     unsigned numChannels = 1; // by default
00041     char buf[100];
00042     // Start with the first 6 bytes (the first 5 of which must be "#!AMR"):
00043     if (fread(buf, 1, 6, fid) < 6) break;
00044     if (strncmp(buf, "#!AMR", 5) != 0) break; // bad magic #
00045     unsigned bytesRead = 6;
00046     
00047     // The next bytes must be "\n", "-WB\n", "_MC1.0\n", or "-WB_MC1.0\n"
00048     if (buf[5] == '-') {
00049       // The next bytes must be "WB\n" or "WB_MC1.0\n"
00050       if (fread(&buf[bytesRead], 1, 3, fid) < 3) break;
00051       if (strncmp(&buf[bytesRead], "WB", 2) != 0) break; // bad magic #
00052       isWideband = True;
00053       bytesRead += 3;
00054     }
00055     if (buf[bytesRead-1] == '_') {
00056       // The next bytes must be "MC1.0\n"
00057       if (fread(&buf[bytesRead], 1, 6, fid) < 6) break;
00058       if (strncmp(&buf[bytesRead], "MC1.0\n", 6) != 0) break; // bad magic #
00059       bytesRead += 6;
00060 
00061       // The next 4 bytes contain the number of channels:
00062       char channelDesc[4];
00063       if (fread(channelDesc, 1, 4, fid) < 4) break;
00064       numChannels = channelDesc[3]&0xF;
00065     } else if (buf[bytesRead-1] != '\n') {
00066       break; // bad magic #
00067     }
00068 
00069     // If we get here, the magic number was OK:
00070     magicNumberOK = True;
00071 
00072 #ifdef DEBUG
00073     fprintf(stderr, "isWideband: %d, numChannels: %d\n",
00074             isWideband, numChannels);
00075 #endif
00076     return new AMRAudioFileSource(env, fid, isWideband, numChannels);
00077   } while (0);
00078 
00079   // An error occurred:
00080   CloseInputFile(fid);
00081   if (!magicNumberOK) {
00082     env.setResultMsg("Bad (or nonexistent) AMR file header");
00083   }
00084   return NULL;
00085 }
00086 
00087 AMRAudioFileSource
00088 ::AMRAudioFileSource(UsageEnvironment& env, FILE* fid,
00089                      Boolean isWideband, unsigned numChannels)
00090   : AMRAudioSource(env, isWideband, numChannels),
00091     fFid(fid) {
00092 }
00093 
00094 AMRAudioFileSource::~AMRAudioFileSource() {
00095   CloseInputFile(fFid);
00096 }
00097 
00098 // The mapping from the "FT" field to frame size.
00099 // Values of 65535 are invalid.
00100 #define FT_INVALID 65535
00101 static unsigned short frameSize[16] = {
00102   12, 13, 15, 17,
00103   19, 20, 26, 31,
00104   5, FT_INVALID, FT_INVALID, FT_INVALID,
00105   FT_INVALID, FT_INVALID, FT_INVALID, 0
00106 };
00107 static unsigned short frameSizeWideband[16] = {
00108   17, 23, 32, 36,
00109   40, 46, 50, 58,
00110   60, 5, FT_INVALID, FT_INVALID,
00111   FT_INVALID, FT_INVALID, 0, 0
00112 };
00113 
00114 // Note: We should change the following to use asynchronous file reading, #####
00115 // as we now do with ByteStreamFileSource. #####
00116 void AMRAudioFileSource::doGetNextFrame() {
00117   if (feof(fFid) || ferror(fFid)) {
00118     handleClosure(this);
00119     return;
00120   }
00121 
00122   // Begin by reading the 1-byte frame header (and checking it for validity)
00123   while (1) {
00124     if (fread(&fLastFrameHeader, 1, 1, fFid) < 1) {
00125       handleClosure(this);
00126       return;
00127     }
00128     if ((fLastFrameHeader&0x83) != 0) {
00129 #ifdef DEBUG
00130       fprintf(stderr, "Invalid frame header 0x%02x (padding bits (0x83) are not zero)\n", fLastFrameHeader);
00131 #endif
00132     } else {
00133       unsigned char ft = (fLastFrameHeader&0x78)>>3;
00134       fFrameSize = fIsWideband ? frameSizeWideband[ft] : frameSize[ft];
00135       if (fFrameSize == FT_INVALID) {
00136 #ifdef DEBUG
00137         fprintf(stderr, "Invalid FT field %d (from frame header 0x%02x)\n",
00138                 ft, fLastFrameHeader);
00139 #endif
00140       } else {
00141         // The frame header is OK
00142 #ifdef DEBUG
00143         fprintf(stderr, "Valid frame header 0x%02x -> ft %d -> frame size %d\n", fLastFrameHeader, ft, fFrameSize);
00144 #endif
00145         break;
00146       }
00147     }
00148   } 
00149       
00150   // Next, read the frame-block into the buffer provided:
00151   fFrameSize *= fNumChannels; // because multiple channels make up a frame-block
00152   if (fFrameSize > fMaxSize) {
00153     fNumTruncatedBytes = fFrameSize - fMaxSize;
00154     fFrameSize = fMaxSize;
00155   }
00156   fFrameSize = fread(fTo, 1, fFrameSize, fFid);
00157 
00158   // Set the 'presentation time':
00159   if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
00160     // This is the first frame, so use the current time:
00161     gettimeofday(&fPresentationTime, NULL);
00162   } else {
00163     // Increment by the play time of the previous frame (20 ms)
00164     unsigned uSeconds   = fPresentationTime.tv_usec + 20000;
00165     fPresentationTime.tv_sec += uSeconds/1000000;
00166     fPresentationTime.tv_usec = uSeconds%1000000;
00167   }
00168 
00169   fDurationInMicroseconds = 20000; // each frame is 20 ms
00170 
00171   // Switch to another task, and inform the reader that he has data:
00172   nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
00173                                 (TaskFunc*)FramedSource::afterGetting, this);
00174  }

Generated on Tue Jul 22 06:39:05 2008 for live by  doxygen 1.5.2