Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

FaacEncoder.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 
00003    Copyright (c) 2005 Tyrell Corporation. All rights reserved.
00004 
00005    Tyrell DarkIce
00006 
00007    File     : FaacEncoder.cpp
00008    Version  : $Revision: 1.2 $
00009    Author   : $Author: darkeye $
00010    Location : $Source: /cvsroot/darkice/darkice/src/FaacEncoder.cpp,v $
00011    
00012    Copyright notice:
00013 
00014     This program is free software; you can redistribute it and/or
00015     modify it under the terms of the GNU General Public License  
00016     as published by the Free Software Foundation; either version 2
00017     of the License, or (at your option) any later version.
00018    
00019     This program is distributed in the hope that it will be useful,
00020     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00022     GNU General Public License for more details.
00023    
00024     You should have received a copy of the GNU General Public License
00025     along with this program; if not, write to the Free Software
00026     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ------------------------------------------------------------------------------*/
00029 
00030 /* ============================================================ include files */
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035 
00036 // compile the whole file only if faac support configured in
00037 #ifdef HAVE_FAAC_LIB
00038 
00039 
00040 
00041 #include "Exception.h"
00042 #include "Util.h"
00043 #include "FaacEncoder.h"
00044 
00045 
00046 /* ===================================================  local data structures */
00047 
00048 
00049 /* ================================================  local constants & macros */
00050 
00051 /*------------------------------------------------------------------------------
00052  *  File identity
00053  *----------------------------------------------------------------------------*/
00054 static const char fileid[] = "$Id: FaacEncoder.cpp,v 1.2 2005/04/16 22:19:20 darkeye Exp $";
00055 
00056 
00057 /* ===============================================  local function prototypes */
00058 
00059 
00060 /* =============================================================  module code */
00061 
00062 /*------------------------------------------------------------------------------
00063  *  Open an encoding session
00064  *----------------------------------------------------------------------------*/
00065 bool
00066 FaacEncoder :: open ( void )
00067                                                             throw ( Exception )
00068 {
00069     if ( isOpen() ) {
00070         close();
00071     }
00072 
00073     // open the underlying sink
00074     if ( !sink->open() ) {
00075         throw Exception( __FILE__, __LINE__,
00076                          "faac lib opening underlying sink error");
00077     }
00078 
00079     char      * faacVersion;
00080     char      * faacCopyright;
00081     faacEncGetVersion(&faacVersion, &faacCopyright);
00082     reportEvent(1, "Using faac codec version", faacVersion);
00083 
00084     encoderHandle = faacEncOpen(getInSampleRate(),
00085                                 getInChannel(),
00086                                 &inputSamples,
00087                                 &maxOutputBytes);
00088 
00089     faacEncConfiguration      * faacConfig;
00090 
00091     faacConfig = faacEncGetCurrentConfiguration(encoderHandle);
00092 
00093     faacConfig->aacObjectType = MAIN;
00094     faacConfig->mpegVersion   = MPEG2;
00095     faacConfig->useTns        = 1;
00096     faacConfig->shortctl      = SHORTCTL_NORMAL;
00097     faacConfig->useLfe        = 0;
00098     faacConfig->allowMidside  = 1;
00099     faacConfig->bitRate       = getOutBitrate() * 1000 / getOutChannel();
00100     faacConfig->bandWidth     = lowpass;
00101     faacConfig->quantqual     = (unsigned long) (getOutQuality() * 1000.0);
00102     faacConfig->outputFormat  = 1;
00103     faacConfig->inputFormat   = FAAC_INPUT_16BIT;
00104 
00105     if (!faacEncSetConfiguration(encoderHandle, faacConfig)) {
00106         throw Exception(__FILE__, __LINE__,
00107                         "error configuring faac library");
00108     }
00109 
00110     faacOpen = true;
00111 
00112     return true;
00113 }
00114 
00115 
00116 /*------------------------------------------------------------------------------
00117  *  Write data to the encoder
00118  *----------------------------------------------------------------------------*/
00119 unsigned int
00120 FaacEncoder :: write (  const void    * buf,
00121                         unsigned int    len )           throw ( Exception )
00122 {
00123     if ( !isOpen() || len == 0 ) {
00124         return 0;
00125     }
00126 
00127     unsigned int    channels         = getInChannel();
00128     unsigned int    bitsPerSample    = getInBitsPerSample();
00129     unsigned int    sampleSize       = (bitsPerSample / 8) * channels;
00130     unsigned char * b                = (unsigned char*) buf;
00131     unsigned int    processed        = len - (len % sampleSize);
00132     unsigned int    nSamples         = processed / sampleSize;
00133     unsigned char * faacBuf          = new unsigned char[maxOutputBytes];
00134     int             samples          = (int) nSamples * channels;
00135     int             processedSamples = 0;
00136 
00137     while (processedSamples < samples) {
00138         int     outputBytes;
00139         int     inSamples = samples - processedSamples < (int) inputSamples
00140                           ? samples - processedSamples
00141                           : inputSamples;
00142 
00143         outputBytes = faacEncEncode(encoderHandle,
00144                                    (int32_t*) (b + processedSamples/sampleSize),
00145                                     inSamples,
00146                                     faacBuf,
00147                                     maxOutputBytes);
00148         sink->write(faacBuf, outputBytes);
00149 
00150         processedSamples += inSamples;
00151     }
00152 
00153     delete[] faacBuf;
00154 
00155     return processedSamples;
00156 }
00157 
00158 
00159 /*------------------------------------------------------------------------------
00160  *  Flush the data from the encoder
00161  *----------------------------------------------------------------------------*/
00162 void
00163 FaacEncoder :: flush ( void )
00164                                                             throw ( Exception )
00165 {
00166     if ( !isOpen() ) {
00167         return;
00168     }
00169 
00170     sink->flush();
00171 }
00172 
00173 
00174 /*------------------------------------------------------------------------------
00175  *  Close the encoding session
00176  *----------------------------------------------------------------------------*/
00177 void
00178 FaacEncoder :: close ( void )                           throw ( Exception )
00179 {
00180     if ( isOpen() ) {
00181         flush();
00182         faacEncClose(encoderHandle);
00183         faacOpen = false;
00184 
00185         sink->close();
00186     }
00187 }
00188 
00189 
00190 #endif // HAVE_FAAC_LIB
00191 
00192 
00193 /*------------------------------------------------------------------------------
00194  
00195   $Source: /cvsroot/darkice/darkice/src/FaacEncoder.cpp,v $
00196 
00197   $Log: FaacEncoder.cpp,v $
00198   Revision 1.2  2005/04/16 22:19:20  darkeye
00199   changed remaining typos
00200 
00201   Revision 1.1  2005/04/16 21:57:34  darkeye
00202   added AAC support through the faac codec, http://www.audiocoding.com/
00203 
00204 
00205   
00206 ------------------------------------------------------------------------------*/
00207 

Generated on Fri May 19 15:36:48 2006 for DarkIce by  doxygen 1.4.4