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