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
00033
00034 #include "stdafx.h"
00035 #include "VorbisEncodeInputPin.h"
00036
00037 VorbisEncodeInputPin::VorbisEncodeInputPin(AbstractTransformFilter* inParentFilter, CCritSec* inFilterLock, AbstractTransformOutputPin* inOutputPin, vector<CMediaType*> inAcceptableMediaTypes)
00038 : AbstractTransformInputPin(inParentFilter, inFilterLock, inOutputPin, NAME("VorbisEncodeInputPin"), L"PCM In", inAcceptableMediaTypes)
00039 , mFishSound(NULL)
00040 , mWaveFormat(NULL)
00041 , mUptoFrame(0)
00042 , mVorbisQuality(0.6f)
00043 {
00044 #ifdef OGGCODECS_LOGGING
00045 debugLog.open("G:\\logs\\vorbisenc.logs", ios_base::out);
00046 #endif
00047 }
00048
00049 VorbisEncodeInputPin::~VorbisEncodeInputPin(void)
00050 {
00051 debugLog.close();
00052 DestroyCodec();
00053 }
00054
00055
00056
00057 HRESULT VorbisEncodeInputPin::TransformData(unsigned char* inBuf, long inNumBytes)
00058 {
00059
00060 float* locFloatBuf = new float[inNumBytes/2];
00061 short locTempShort = 0;
00062 float locTempFloat = 0;
00063
00064 for (int i = 0; i < inNumBytes; i += 2) {
00065 locTempShort = *((short*)(inBuf + i));
00066 locTempFloat = (float)locTempShort;
00067 locTempFloat /= 32767.0;
00068 locFloatBuf[i/2] = locTempFloat;;
00069 }
00070
00071 long locErr = fish_sound_encode(mFishSound, (float**)locFloatBuf, inNumBytes/(mFishInfo.channels*2));
00072 delete[] locFloatBuf;
00073
00074 if (locErr < 0) {
00075
00076 } else {
00077
00078 }
00079 return locErr;
00080 }
00081 bool VorbisEncodeInputPin::ConstructCodec() {
00082 mFishInfo.channels = mWaveFormat->nChannels;
00083 mFishInfo.format = FISH_SOUND_VORBIS;
00084 mFishInfo.samplerate = mWaveFormat->nSamplesPerSec;
00085
00086 mFishInfo.format = FISH_SOUND_VORBIS;
00087
00088
00089 debugLog<<"Setting quality with fs command to "<<mVorbisQuality<<endl;
00090
00091
00092 int I_AM_NOT_NULL = 1;
00093
00094 fish_sound_command((FishSound*)I_AM_NOT_NULL, FISH_SOUND_VORBIS_SET_QUALITY, &mVorbisQuality, sizeof(float));
00095
00096 mFishSound = fish_sound_new (FISH_SOUND_ENCODE, &mFishInfo);
00097
00098
00099 ((VorbisEncodeFilter*)mParentFilter)->mVorbisFormatBlock.numChannels = (unsigned char)mWaveFormat->nChannels;
00100 ((VorbisEncodeFilter*)mParentFilter)->mVorbisFormatBlock.samplesPerSec = mWaveFormat->nSamplesPerSec;
00101
00102
00103 int i = 1;
00104
00105 fish_sound_command(mFishSound, FISH_SOUND_SET_INTERLEAVE, &i, sizeof(int));
00106
00107
00108
00109 fish_sound_set_encoded_callback (mFishSound, VorbisEncodeInputPin::VorbisEncoded, this);
00110
00111 return true;
00112
00113 }
00114 void VorbisEncodeInputPin::DestroyCodec()
00115 {
00116 fish_sound_delete(mFishSound);
00117 mFishSound = NULL;
00118 }
00119
00120
00121
00122 int VorbisEncodeInputPin::VorbisEncoded (FishSound* inFishSound, unsigned char* inPacketData, long inNumBytes, void* inThisPointer)
00123 {
00124
00125 VorbisEncodeInputPin* locThis = reinterpret_cast<VorbisEncodeInputPin*> (inThisPointer);
00126 VorbisEncodeFilter* locFilter = reinterpret_cast<VorbisEncodeFilter*>(locThis->m_pFilter);
00127
00128
00129
00130
00131 LONGLONG locFrameStart = locThis->mUptoFrame;
00132 LONGLONG locFrameEnd = locThis->mUptoFrame
00133 = fish_sound_get_frameno(locThis->mFishSound);
00134
00135 IMediaSample* locSample;
00136 HRESULT locHR = locThis->mOutputPin->GetDeliveryBuffer(&locSample, &locFrameStart, &locFrameEnd, NULL);
00137
00138 if (FAILED(locHR)) {
00139
00140 return locHR;
00141 }
00142
00143 BYTE* locBuffer = NULL;
00144
00145
00146
00147 locSample->GetPointer(&locBuffer);
00148
00149
00150
00151 if (locSample->GetSize() >= inNumBytes) {
00152
00153 memcpy((void*)locBuffer, (const void*)inPacketData, inNumBytes);
00154
00155
00156 locThis->SetSampleParams(locSample, inNumBytes, &locFrameStart, &locFrameEnd);
00157
00158 {
00159 CAutoLock locLock(locThis->m_pLock);
00160
00161
00162
00163 HRESULT locHR = ((VorbisEncodeOutputPin*)(locThis->mOutputPin))->mDataQueue->Receive(locSample);
00164 if (locHR != S_OK) {
00165
00166 } else {
00167 }
00168 }
00169
00170 return 0;
00171 } else {
00172 throw 0;
00173 }
00174 }
00175
00176
00177 HRESULT VorbisEncodeInputPin::SetMediaType(const CMediaType* inMediaType) {
00178 if ( (inMediaType->subtype == MEDIASUBTYPE_PCM) &&
00179 (inMediaType->formattype == FORMAT_WaveFormatEx)) {
00180
00181 mWaveFormat = (WAVEFORMATEX*)inMediaType->pbFormat;
00182
00183 } else {
00184
00185 throw 0;
00186 }
00187
00188
00189 ConstructCodec();
00190
00191 return CBaseInputPin::SetMediaType(inMediaType);
00192
00193
00194 }