// Format negotiation HRESULT CTimeStretchFilter::NegotiateFormat(const WAVEFORMATEXTENSIBLE* pwfx, int nApplyChangesDepth, ChannelOrder* pChOrder) { if (!pwfx) return VFW_E_TYPE_NOT_ACCEPTED; #ifdef INTEGER_SAMPLES // only accept 16bit int if (pwfx->Format.wBitsPerSample != 16 || pwfx->SubFormat != KSDATAFORMAT_SUBTYPE_PCM) return VFW_E_TYPE_NOT_ACCEPTED; #else // only accept 32bit float if (pwfx->Format.wBitsPerSample != 32 || pwfx->SubFormat != KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) return VFW_E_TYPE_NOT_ACCEPTED; #endif if (FormatsEqual(pwfx, m_pInputFormat)) { *pChOrder = m_chOrder; return S_OK; } bool bApplyChanges = (nApplyChangesDepth != 0); if (nApplyChangesDepth != INFINITE && nApplyChangesDepth > 0) nApplyChangesDepth--; HRESULT hr = m_pNextSink->NegotiateFormat(pwfx, nApplyChangesDepth, pChOrder); if (FAILED(hr)) return hr; hr = VFW_E_CANNOT_CONNECT; if (!pwfx) return SetFormat(NULL); if (bApplyChanges) { LogWaveFormat(pwfx, "TS - applying "); AM_MEDIA_TYPE tmp; HRESULT result = CreateAudioMediaType((WAVEFORMATEX*)pwfx, &tmp, true); if (SUCCEEDED(result)) { if (m_pMediaType) DeleteMediaType(m_pMediaType); m_pMediaType = CreateMediaType(&tmp); } SetInputFormat(pwfx); SetOutputFormat(pwfx); SetFormat(pwfx); } else LogWaveFormat(pwfx, "TS - "); m_chOrder = *pChOrder; return S_OK; }
// ask the server for a good decoder for the arguments // GETS THE CODEC for in_format->type + in_format->u.x.encoding status_t BMediaDecoder::SetTo(const media_format *in_format, const void *info, size_t info_size) { fNeedsInit = false; fInitStatus = B_NO_INIT; delete fDecoder; DecoderPlugin * plugin = GetDecoderPlugin(in_format); if (plugin == NULL) { return fInitStatus = B_ERROR; } Decoder * decoder = plugin->NewDecoder(0); if (decoder == NULL) { return fInitStatus = B_ERROR; } fDecoder = decoder; // fDecoderID = mci->sub_id; if ((fInitStatus = AttachToDecoder()) != B_OK) { return fInitStatus; } fInitStatus = SetInputFormat(in_format,info,info_size); return fInitStatus; }
HRESULT CChannelMixer::NegotiateFormat(const WAVEFORMATEXTENSIBLE* pwfx, int nApplyChangesDepth, ChannelOrder* pChOrder) { if (!pwfx) return VFW_E_TYPE_NOT_ACCEPTED; if (FormatsEqual(pwfx, m_pInputFormat)) { *pChOrder = m_chOrder; return S_OK; } if (!m_pNextSink) return VFW_E_TYPE_NOT_ACCEPTED; bool bApplyChanges = (nApplyChangesDepth != 0); if (nApplyChangesDepth != INFINITE && nApplyChangesDepth > 0) nApplyChangesDepth--; if (pwfx->SubFormat != KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) return VFW_E_TYPE_NOT_ACCEPTED; HRESULT hr = S_OK; bool expandToStereo = pwfx->Format.nChannels == 1 && m_pSettings->m_bExpandMonoToStereo; if (!m_pSettings->m_bForceChannelMixing && !expandToStereo) { // try the format directly hr = m_pNextSink->NegotiateFormat(pwfx, nApplyChangesDepth, pChOrder); if (SUCCEEDED(hr)) { if (bApplyChanges) { SetInputFormat(pwfx); SetOutputFormat(pwfx); m_bPassThrough = false; hr = SetupConversion(*pChOrder); } m_chOrder = *pChOrder; return hr; } } WAVEFORMATEXTENSIBLE* pOutWfx; CopyWaveFormatEx(&pOutWfx, pwfx); if (!expandToStereo || m_pSettings->m_bForceChannelMixing) { pOutWfx->dwChannelMask = m_pSettings->m_lSpeakerConfig; pOutWfx->Format.nChannels = m_pSettings->m_lSpeakerCount; } else // Expand mono to stereo { pOutWfx->dwChannelMask = KSAUDIO_SPEAKER_STEREO; pOutWfx->Format.nChannels = 2; } pOutWfx->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT; pOutWfx->Format.nBlockAlign = pOutWfx->Format.wBitsPerSample / 8 * pOutWfx->Format.nChannels; pOutWfx->Format.nAvgBytesPerSec = pOutWfx->Format.nBlockAlign * pOutWfx->Format.nSamplesPerSec; hr = m_pNextSink->NegotiateFormat(pOutWfx, nApplyChangesDepth, pChOrder); m_chOrder = *pChOrder; if (FAILED(hr)) { SAFE_DELETE_WAVEFORMATEX(pOutWfx); return hr; } if (bApplyChanges) { LogWaveFormat(pwfx, "MIX - applying "); m_bPassThrough = false; SetInputFormat(pwfx); SetOutputFormat(pOutWfx, true); hr = SetupConversion(*pChOrder); } else { LogWaveFormat(pwfx, "MIX - "); SAFE_DELETE_WAVEFORMATEX(pOutWfx); } return hr; }
HRESULT CSampleRateConverter::NegotiateFormat(const WAVEFORMATEXTENSIBLE* pwfx, int nApplyChangesDepth, ChannelOrder* pChOrder) { if (!pwfx) return VFW_E_TYPE_NOT_ACCEPTED; if (FormatsEqual(pwfx, m_pInputFormat)) { *pChOrder = m_chOrder; return S_OK; } if (!m_pNextSink) return VFW_E_TYPE_NOT_ACCEPTED; bool bApplyChanges = (nApplyChangesDepth != 0); if (nApplyChangesDepth != INFINITE && nApplyChangesDepth > 0) nApplyChangesDepth--; // Try passthrough HRESULT hr = m_pNextSink->NegotiateFormat(pwfx, nApplyChangesDepth, pChOrder); if (SUCCEEDED(hr)) { if (bApplyChanges) { m_bPassThrough = true; SetInputFormat(pwfx); SetOutputFormat(pwfx); } m_chOrder = *pChOrder; return hr; } if (pwfx->SubFormat != KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) return VFW_E_TYPE_NOT_ACCEPTED; WAVEFORMATEXTENSIBLE* pOutWfx; CopyWaveFormatEx(&pOutWfx, pwfx); pOutWfx->Format.nSamplesPerSec = 0; hr = VFW_E_TYPE_NOT_ACCEPTED; const unsigned int sampleRateCount = sizeof(gAllowedSampleRates) / sizeof(int); unsigned int startPoint = 0; // TODO test duplicate sample rates first // Search for the input sample rate in sample rate array bool foundSampleRate = false; for (unsigned int i = 0; i < sampleRateCount && !foundSampleRate; i++) { if (gAllowedSampleRates[i] == pwfx->Format.nSamplesPerSec) { startPoint = ++i; // select closest sample rate in ascending order foundSampleRate = true; } } if (!foundSampleRate) Log("CSampleRateConverter::NegotiateFormat - sample rate (%d) not found in the source array", pwfx->Format.nSamplesPerSec); unsigned int sampleRatesTested = 0; for (int i = startPoint; FAILED(hr) && pOutWfx->Format.nSamplesPerSec == 0 && sampleRatesTested < sampleRateCount; i++) { if (pOutWfx->Format.nSamplesPerSec == pwfx->Format.nSamplesPerSec) { sampleRatesTested++; continue; // skip if same as source } pOutWfx->Format.nSamplesPerSec = gAllowedSampleRates[i]; pOutWfx->Format.nAvgBytesPerSec = gAllowedSampleRates[i] * pOutWfx->Format.nBlockAlign; hr = m_pNextSink->NegotiateFormat(pOutWfx, nApplyChangesDepth, pChOrder); sampleRatesTested++; if (FAILED(hr)) pOutWfx->Format.nSamplesPerSec = 0; // Search from the lower end if (i == sampleRateCount - 1) i = 0; } if (FAILED(hr)) { SAFE_DELETE_WAVEFORMATEX(pOutWfx); return hr; } if (bApplyChanges) { LogWaveFormat(pwfx, "SRC - applying "); m_bPassThrough = false; SetInputFormat(pwfx); SetOutputFormat(pOutWfx, true); hr = SetupConversion(); // TODO: do something meaningfull if SetupConversion fails //if (FAILED(hr)) } else { LogWaveFormat(pwfx, "SRC - "); SAFE_DELETE_WAVEFORMATEX(pOutWfx); } m_chOrder = *pChOrder; return S_OK; }
// Format negotiation HRESULT CWASAPIRenderFilter::NegotiateFormat(const WAVEFORMATEXTENSIBLE* pwfx, int nApplyChangesDepth, ChannelOrder* pChOrder) { if (!pwfx) return VFW_E_TYPE_NOT_ACCEPTED; if (FormatsEqual(pwfx, m_pInputFormat)) { *pChOrder = m_chOrder; return S_OK; } bool bApplyChanges = nApplyChangesDepth != 0; bool bitDepthForced = (m_pSettings->m_nForceBitDepth != 0 && m_pSettings->m_nForceBitDepth != pwfx->Format.wBitsPerSample); bool sampleRateForced = (m_pSettings->m_nForceSamplingRate != 0 && m_pSettings->m_nForceSamplingRate != pwfx->Format.nSamplesPerSec); if ((bitDepthForced || sampleRateForced) && pwfx->SubFormat == KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL || pwfx->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT && bitDepthForced) return VFW_E_TYPE_NOT_ACCEPTED; if (((bitDepthForced && m_pSettings->m_nForceBitDepth != pwfx->Format.wBitsPerSample) || (sampleRateForced && m_pSettings->m_nForceSamplingRate != pwfx->Format.nSamplesPerSec))) return VFW_E_TYPE_NOT_ACCEPTED; CAutoLock lock(&m_csResources); HRESULT hr = CreateAudioClient(); if (FAILED(hr)) { Log("CWASAPIRenderFilter::NegotiateFormat Error, audio client not initialized: (0x%08x)", hr); return VFW_E_CANNOT_CONNECT; } WAVEFORMATEXTENSIBLE* pwfxAccepted = NULL; hr = IsFormatSupported(pwfx, &pwfxAccepted); if (FAILED(hr)) { SAFE_DELETE_WAVEFORMATEX(pwfxAccepted); return hr; } if (bApplyChanges) { LogWaveFormat(pwfx, "REN - applying "); // Stop and discard audio client StopAudioClient(); SAFE_RELEASE(m_pRenderClient); SAFE_RELEASE(m_pAudioClock); SAFE_RELEASE(m_pAudioClient); // We must use incoming format so the WAVEFORMATEXTENSIBLE to WAVEFORMATEXT difference // that some audio drivers require is not causing an infonite loop of format changes SetInputFormat(pwfx); // Reinitialize audio client hr = CreateAudioClient(true); } else LogWaveFormat(pwfx, "Input format "); m_chOrder = *pChOrder = DS_ORDER; SAFE_DELETE_WAVEFORMATEX(pwfxAccepted); return hr; }