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

AacEncoder.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     : AacEncoder.cpp
00008    Version  : $Revision$
00009    Author   : $Author$
00010    Location : $Source$
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 lame support configured in
00037 #ifdef HAVE_FAAC_LIB
00038 
00039 
00040 
00041 #include "Exception.h"
00042 #include "Util.h"
00043 #include "AacEncoder.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$";
00055 
00056 
00057 /* ===============================================  local function prototypes */
00058 
00059 
00060 /* =============================================================  module code */
00061 
00062 /*------------------------------------------------------------------------------
00063  *  Open an encoding session
00064  *----------------------------------------------------------------------------*/
00065 bool
00066 AacEncoder :: 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     reportEvent(1, "Using faac codec\n", getFaacVersion());
00080 
00081     encoderHandle = faacEncOpen(getInSampleRate(),
00082                                 getInChannel(),
00083                                 &inputSamples,
00084                                 &maxOutputBytes);
00085 
00086     faacEncConfiguration      * faacConfig;
00087 
00088     faacConfig = faacEncGetCurrentConfiguration(encoderHandle);
00089 
00090     faacConfig->aacObjectType = MAIN;
00091     faacConfig->mpegVersion   = MPEG2;
00092     faacConfig->useTns        = 1;
00093     faacConfig->shortctl      = SHORTCTL_NORMAL;
00094     faacConfig->useLfe        = 0;
00095     faacConfig->allowMidside  = 1;
00096     faacConfig->bitRate       = getOutBitrate() / getOutChannel();
00097 //    faacConfig->bandWidth     = getOutBitrate();
00098     faacConfig->quantqual     = 0;
00099     faacConfig->outputFormat  = 1;
00100     faacConfig->inputFormat   = FAAC_INPUT_16BIT;
00101 
00102     if (!faacEncSetConfiguration(encoderHandle, faacConfig)) {
00103         throw Exception(__FILE__, __LINE__,
00104                         "error configuring faac library");
00105     }
00106 
00107     faacOpen = true;
00108 
00109     return true;
00110 }
00111 
00112 
00113 /*------------------------------------------------------------------------------
00114  *  Write data to the encoder
00115  *----------------------------------------------------------------------------*/
00116 unsigned int
00117 AacEncoder :: write (   const void    * buf,
00118                         unsigned int    len )           throw ( Exception )
00119 {
00120     if ( !isOpen() || len == 0 ) {
00121         return 0;
00122     }
00123 
00124     // hard-coded 16 bit input
00125     uint16_t          * inBuf   = (uint16_t*) buf;
00126     unsigned char     * faacBuf = new unsigned char[maxOutputBytes];
00127     int                 samples          = (int) len;
00128     int                 processedSamples = 0;
00129 
00130     while (processedSamples < samples) {
00131         int     outputBytes;
00132         int     inSamples = processedSamples - samples < inputSamples
00133                           ? processedSamples - samples
00134                           : inputSamples;
00135 
00136         reportEvent(1, "before encode", samples, inSamples, processedSamples);
00137         outputBytes = faacEncEncode(encoderHandle,
00138                                     (int32_t*) (inBuf + processedSamples),
00139                                     inSamples,
00140                                     faacBuf,
00141                                     maxOutputBytes);
00142         reportEvent(1, "after encode", outputBytes, inputSamples);
00143         sink->write(faacBuf, outputBytes);
00144 
00145         processedSamples += inSamples;
00146     }
00147 
00148     return processedSamples;
00149 }
00150 
00151 
00152 /*------------------------------------------------------------------------------
00153  *  Flush the data from the encoder
00154  *----------------------------------------------------------------------------*/
00155 void
00156 AacEncoder :: flush ( void )
00157                                                             throw ( Exception )
00158 {
00159     if ( !isOpen() ) {
00160         return;
00161     }
00162 
00163     sink->flush();
00164 }
00165 
00166 
00167 /*------------------------------------------------------------------------------
00168  *  Close the encoding session
00169  *----------------------------------------------------------------------------*/
00170 void
00171 AacEncoder :: close ( void )                    throw ( Exception )
00172 {
00173     if ( isOpen() ) {
00174         flush();
00175         faacEncClose(encoderHandle);
00176         faacOpen = false;
00177 
00178         sink->close();
00179     }
00180 }
00181 
00182 
00183 #endif // HAVE_FAAC_LIB
00184 
00185 
00186 /*------------------------------------------------------------------------------
00187  
00188   $Source$
00189 
00190   $Log$
00191 
00192   
00193 ------------------------------------------------------------------------------*/
00194 

Generated on Sat Apr 16 22:06:16 2005 for DarkIce by  doxygen 1.4.1