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 "AacEncoder.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 static const char fileid[] = "$Id$";
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 bool
00066 AacEncoder :: 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 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
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
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
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
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
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
00189
00190
00191
00192
00193
00194