00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035
00036
00037 #ifdef HAVE_FAAC_LIB
00038
00039
00040
00041 #include "Exception.h"
00042 #include "Util.h"
00043 #include "FaacEncoder.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 static const char fileid[] = "$Id: FaacEncoder.cpp,v 1.2 2005/04/16 22:19:20 darkeye Exp $";
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 bool
00066 FaacEncoder :: open ( void )
00067 throw ( Exception )
00068 {
00069 if ( isOpen() ) {
00070 close();
00071 }
00072
00073
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
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
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
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
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207