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 "SpeexDecodeInputPin.h"
00034
00035
00036 SpeexDecodeInputPin::SpeexDecodeInputPin(AbstractTransformFilter* inFilter, CCritSec* inFilterLock, AbstractTransformOutputPin* inOutputPin, vector<CMediaType*> inAcceptableMediaTypes)
00037 : AbstractTransformInputPin(inFilter, inFilterLock, inOutputPin, NAME("SpeexDecodeInputPin"), L"Speex In", inAcceptableMediaTypes)
00038 , mFishSound(NULL)
00039
00040 , mNumChannels(0)
00041 , mFrameSize(0)
00042 , mSampleRate(0)
00043 , mUptoFrame(0)
00044
00045 , mBegun(false)
00046 {
00047 ConstructCodec();
00048 }
00049
00050 bool SpeexDecodeInputPin::ConstructCodec() {
00051 mFishSound = fish_sound_new (FISH_SOUND_DECODE, &mFishInfo);
00052
00053 int i = 1;
00054
00055 fish_sound_command(mFishSound, FISH_SOUND_SET_INTERLEAVE, &i, sizeof(int));
00056
00057 fish_sound_set_decoded_callback (mFishSound, SpeexDecodeInputPin::SpeexDecoded, this);
00058
00059 return true;
00060 }
00061 void SpeexDecodeInputPin::DestroyCodec() {
00062 fish_sound_delete(mFishSound);
00063 mFishSound = NULL;
00064 }
00065 SpeexDecodeInputPin::~SpeexDecodeInputPin(void)
00066 {
00067 DestroyCodec();
00068 }
00069
00070 STDMETHODIMP SpeexDecodeInputPin::NonDelegatingQueryInterface(REFIID riid, void **ppv)
00071 {
00072 if (riid == IID_IMediaSeeking) {
00073 *ppv = (IMediaSeeking*)this;
00074 ((IUnknown*)*ppv)->AddRef();
00075 return NOERROR;
00076 }
00077
00078 return CBaseInputPin::NonDelegatingQueryInterface(riid, ppv);
00079 }
00080 STDMETHODIMP SpeexDecodeInputPin::NewSegment(REFERENCE_TIME inStartTime, REFERENCE_TIME inStopTime, double inRate)
00081 {
00082 CAutoLock locLock(mStreamLock);
00083
00084 mUptoFrame = 0;
00085 return AbstractTransformInputPin::NewSegment(inStartTime, inStopTime, inRate);
00086
00087 }
00088
00089 int SpeexDecodeInputPin::SpeexDecoded (FishSound* inFishSound, float** inPCM, long inFrames, void* inThisPointer)
00090 {
00091
00092
00093
00094
00095 SpeexDecodeInputPin* locThis = reinterpret_cast<SpeexDecodeInputPin*> (inThisPointer);
00096 SpeexDecodeFilter* locFilter = reinterpret_cast<SpeexDecodeFilter*>(locThis->m_pFilter);
00097
00098
00099 if (! locThis->mBegun) {
00100
00101 fish_sound_command (locThis->mFishSound, FISH_SOUND_GET_INFO, &(locThis->mFishInfo), sizeof (FishSoundInfo));
00102 locThis->mBegun = true;
00103
00104 locThis->mNumChannels = locThis->mFishInfo.channels;
00105 locThis->mFrameSize = locThis->mNumChannels * SIZE_16_BITS;
00106 locThis->mSampleRate = locThis->mFishInfo.samplerate;
00107
00108 }
00109
00110
00111 unsigned long locActualSize = inFrames * locThis->mFrameSize;
00112 unsigned long locTotalFrameCount = inFrames * locThis->mNumChannels;
00113
00114 REFERENCE_TIME locFrameStart = (((__int64)(locThis->mUptoFrame * UNITS)) / locThis->mSampleRate);
00115
00116 locThis->mUptoFrame += inFrames;
00117
00118
00119
00120 REFERENCE_TIME locFrameEnd = (((__int64)(locThis->mUptoFrame * UNITS)) / locThis->mSampleRate);
00121
00122
00123 IMediaSample* locSample;
00124 HRESULT locHR = locThis->mOutputPin->GetDeliveryBuffer(&locSample, &locFrameStart, &locFrameEnd, NULL);
00125
00126 if (locHR != S_OK) {
00127 return locHR;
00128 }
00129
00130
00131 BYTE* locBuffer = NULL;
00132 signed short* locShortBuffer = NULL;
00133
00134
00135 locSample->GetPointer(&locBuffer);
00136 locShortBuffer = (short *) locBuffer;
00137
00138 signed short tempInt = 0;
00139 float tempFloat = 0;
00140
00141
00142
00143 if (locSample->GetSize() >= locActualSize) {
00144
00145 float SINT_MAX_AS_FLOAT = 32767.0f;
00146 for (unsigned long i = 0; i < locTotalFrameCount; i++) {
00147
00148 if (((float*)inPCM)[i] <= -1.0f) {
00149 tempInt = SINT_MIN;
00150 } else if (((float*)inPCM)[i] >= 1.0f) {
00151 tempInt = SINT_MAX;
00152 } else {
00153
00154 tempFloat = ((( (float*) inPCM )[i]) * SINT_MAX_AS_FLOAT);
00155
00156 tempInt = (signed short)(tempFloat);
00157
00158 }
00159
00160 *locShortBuffer = tempInt;
00161 locShortBuffer++;
00162 }
00163
00164
00165 locThis->SetSampleParams(locSample, locActualSize, &locFrameStart, &locFrameEnd);
00166
00167 {
00168 CAutoLock locLock(locThis->m_pLock);
00169 HRESULT locHR = ((SpeexDecodeOutputPin*)(locThis->mOutputPin))->mDataQueue->Receive(locSample);
00170 if (locHR != S_OK) {
00171 return locHR;
00172 }
00173 }
00174
00175
00176 return 0;
00177 } else {
00178 throw 0;
00179 }
00180
00181 }
00182
00183
00184
00185 HRESULT SpeexDecodeInputPin::TransformData(BYTE* inBuf, long inNumBytes)
00186 {
00187 long locErr = fish_sound_decode(mFishSound, inBuf, inNumBytes);
00188 if (locErr == 0) {
00189 return S_OK;
00190 } else {
00191 return S_FALSE;
00192 }
00193 }
00194
00195
00196
00197 HRESULT SpeexDecodeInputPin::SetMediaType(const CMediaType* inMediaType) {
00198
00199
00200
00201 if (inMediaType->subtype == MEDIASUBTYPE_Speex) {
00202 ((SpeexDecodeFilter*)mParentFilter)->setSpeexFormat((sSpeexFormatBlock*)inMediaType->pbFormat);
00203
00204 } else {
00205 throw 0;
00206 }
00207 return CBaseInputPin::SetMediaType(inMediaType);
00208 }
00209