Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BufferFilter.cpp « BufferFilter « transform « filters « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2fe954fd8ece2c790c1e64717f2afe5b9f5e5897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * (C) 2003-2006 Gabest
 * (C) 2006-2013 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include <algorithm>
#include "BufferFilter.h"
#include "../../../DSUtil/DSUtil.h"

#ifdef STANDALONE_FILTER

const AMOVIESETUP_MEDIATYPE sudPinTypesIn[] = {
    {&MEDIATYPE_NULL, &MEDIASUBTYPE_NULL},
};

const AMOVIESETUP_MEDIATYPE sudPinTypesOut[] = {
    {&MEDIATYPE_NULL, &MEDIASUBTYPE_NULL},
};

const AMOVIESETUP_PIN sudpPins[] = {
    {L"Input", FALSE, FALSE, FALSE, FALSE, &CLSID_NULL, nullptr, _countof(sudPinTypesIn), sudPinTypesIn},
    {L"Output", FALSE, TRUE, FALSE, FALSE, &CLSID_NULL, nullptr, _countof(sudPinTypesOut), sudPinTypesOut}
};

const AMOVIESETUP_FILTER sudFilter[] = {
    {&__uuidof(CBufferFilter), BufferFilterName, MERIT_DO_NOT_USE, _countof(sudpPins), sudpPins, CLSID_LegacyAmFilterCategory}
};

CFactoryTemplate g_Templates[] = {
    {sudFilter[0].strName, sudFilter[0].clsID, CreateInstance<CBufferFilter>, nullptr, &sudFilter[0]}
};

int g_cTemplates = _countof(g_Templates);

STDAPI DllRegisterServer()
{
    return AMovieDllRegisterServer2(TRUE);
}

STDAPI DllUnregisterServer()
{
    return AMovieDllRegisterServer2(FALSE);
}

#include "../../FilterApp.h"

CFilterApp theApp;

#endif

//
// CBufferFilter
//

CBufferFilter::CBufferFilter(LPUNKNOWN lpunk, HRESULT* phr)
    : CTransformFilter(NAME("CBufferFilter"), lpunk, __uuidof(this))
    , m_nSamplesToBuffer(2)
{
    HRESULT hr = S_OK;

    do {
        m_pInput = DEBUG_NEW CTransformInputPin(NAME("Transform input pin"), this, &hr, L"In");
        if (!m_pInput) {
            hr = E_OUTOFMEMORY;
        }
        if (FAILED(hr)) {
            break;
        }

        m_pOutput = DEBUG_NEW CBufferFilterOutputPin(this, &hr);
        if (!m_pOutput) {
            hr = E_OUTOFMEMORY;
        }
        if (FAILED(hr)) {
            delete m_pInput;
            m_pInput = nullptr;
            break;
        }
    } while (false);

    if (phr) {
        *phr = hr;
    }
}

CBufferFilter::~CBufferFilter()
{
}

STDMETHODIMP CBufferFilter::NonDelegatingQueryInterface(REFIID riid, void** ppv)
{
    return
        QI(IBufferFilter)
        __super::NonDelegatingQueryInterface(riid, ppv);
}

// IBufferFilter

STDMETHODIMP CBufferFilter::SetBuffers(int nBuffers)
{
    if (!m_pOutput) {
        return E_FAIL;
    }

    if (m_pOutput->IsConnected()) { // TODO: allow "on-the-fly" changes
        return VFW_E_ALREADY_CONNECTED;
    }

    m_nSamplesToBuffer = nBuffers;

    return S_OK;
}

STDMETHODIMP_(int) CBufferFilter::GetBuffers()
{
    return m_nSamplesToBuffer;
}

STDMETHODIMP_(int) CBufferFilter::GetFreeBuffers()
{
    CBufferFilterOutputPin* pPin = static_cast<CBufferFilterOutputPin*>(m_pOutput);
    return (pPin && pPin->m_pOutputQueue ? (m_nSamplesToBuffer - pPin->m_pOutputQueue->GetQueueCount()) : 0);
}

STDMETHODIMP CBufferFilter::SetPriority(DWORD dwPriority)
{
    CBufferFilterOutputPin* pPin = static_cast<CBufferFilterOutputPin*>(m_pOutput);
    return (pPin && pPin->m_pOutputQueue ? (pPin->m_pOutputQueue->SetPriority(dwPriority) ? S_OK : E_FAIL) : E_UNEXPECTED);
}

//

HRESULT CBufferFilter::Receive(IMediaSample* pSample)
{
    ASSERT(pSample);
    CheckPointer(pSample, E_POINTER);
    ASSERT(m_pOutput);
    CheckPointer(m_pOutput, E_POINTER);

    /*  Check for other streams and pass them on */
    AM_SAMPLE2_PROPERTIES* const pProps = m_pInput->SampleProps();
    if (pProps->dwStreamId != AM_STREAM_MEDIA) {
        return m_pOutput->Deliver(pSample);
    }

    HRESULT hr;
    IMediaSample* pOutSample;

    // Set up the output sample
    hr = InitializeOutputSample(pSample, &pOutSample);

    if (FAILED(hr)) {
        return hr;
    }

    // Start timing the transform (if PERF is defined)
    MSR_START(m_idTransform);

    // have the derived class transform the data

    hr = Transform(pSample, pOutSample);

    // Stop the clock and log it (if PERF is defined)
    MSR_STOP(m_idTransform);

    if (FAILED(hr)) {
        DbgLog((LOG_TRACE, 1, _T("Error from transform")));
    } else {
        // the Transform() function can return S_FALSE to indicate that the
        // sample should not be delivered; we only deliver the sample if it's
        // really S_OK (same as NOERROR, of course.)
        if (hr == NOERROR) {
            hr = m_pOutput->Deliver(pOutSample);
            m_bSampleSkipped = FALSE;   // last thing no longer dropped
        } else {
            // S_FALSE returned from Transform is a PRIVATE agreement
            // We should return NOERROR from Receive() in this cause because returning S_FALSE
            // from Receive() means that this is the end of the stream and no more data should
            // be sent.
            if (S_FALSE == hr) {

                //  Release the sample before calling notify to avoid
                //  deadlocks if the sample holds a lock on the system
                //  such as DirectDraw buffers do
                pOutSample->Release();
                m_bSampleSkipped = TRUE;
                if (!m_bQualityChanged) {
                    NotifyEvent(EC_QUALITY_CHANGE, 0, 0);
                    m_bQualityChanged = TRUE;
                }
                return NOERROR;
            }
        }
    }

    // release the output buffer. If the connected pin still needs it,
    // it will have addrefed it itself.
    pOutSample->Release();

    return hr;
}

HRESULT CBufferFilter::Transform(IMediaSample* pIn, IMediaSample* pOut)
{
    BYTE* pDataIn  = nullptr;
    BYTE* pDataOut = nullptr;

    long len  = pIn->GetActualDataLength();
    long size = pOut->GetSize();

    if (FAILED(pIn->GetPointer(&pDataIn)) || !pDataIn || FAILED(pOut->GetPointer(&pDataOut)) || !pDataOut
            || len > size || len <= 0) {
        return S_FALSE;
    }

    memcpy(pDataOut, pDataIn, std::min(len, size));

    pOut->SetActualDataLength(std::min(len, size));

    return S_OK;
}

HRESULT CBufferFilter::CheckInputType(const CMediaType* mtIn)
{
    return S_OK;
}

HRESULT CBufferFilter::CheckTransform(const CMediaType* mtIn, const CMediaType* mtOut)
{
    return mtIn->MatchesPartial(mtOut) ? S_OK : VFW_E_TYPE_NOT_ACCEPTED;
}

HRESULT CBufferFilter::DecideBufferSize(IMemAllocator* pAllocator, ALLOCATOR_PROPERTIES* pProperties)
{
    if (m_pInput->IsConnected() == FALSE) {
        return E_UNEXPECTED;
    }

    CComPtr<IMemAllocator> pAllocatorIn;
    m_pInput->GetAllocator(&pAllocatorIn);
    if (!pAllocatorIn) {
        return E_UNEXPECTED;
    }

    pAllocatorIn->GetProperties(pProperties);

    pProperties->cBuffers = std::max<long>(m_nSamplesToBuffer, pProperties->cBuffers);

    HRESULT hr;
    ALLOCATOR_PROPERTIES Actual;
    if (FAILED(hr = pAllocator->SetProperties(pProperties, &Actual))) {
        return hr;
    }

    return (pProperties->cBuffers > Actual.cBuffers || pProperties->cbBuffer > Actual.cbBuffer
            ? E_FAIL
            : NOERROR);
}

HRESULT CBufferFilter::GetMediaType(int iPosition, CMediaType* pMediaType)
{
    if (m_pInput->IsConnected() == FALSE) {
        return E_UNEXPECTED;
    }

    // TODO: offer all input types from upstream and allow reconnection at least in stopped state
    if (iPosition < 0) {
        return E_INVALIDARG;
    }
    if (iPosition > 0) {
        return VFW_S_NO_MORE_ITEMS;
    }

    CopyMediaType(pMediaType, &m_pInput->CurrentMediaType());

    return S_OK;
}

HRESULT CBufferFilter::StopStreaming()
{
    CBufferFilterOutputPin* pPin = static_cast<CBufferFilterOutputPin*>(m_pOutput);
    if (m_pInput && pPin && pPin->m_pOutputQueue) {
        while (!m_pInput->IsFlushing() && pPin->m_pOutputQueue->GetQueueCount() > 0) {
            Sleep(50);
        }
    }

    return __super::StopStreaming();
}

//
// CBufferFilterOutputPin
//

CBufferFilterOutputPin::CBufferFilterOutputPin(CTransformFilter* pFilter, HRESULT* phr)
    : CTransformOutputPin(NAME("CBufferFilterOutputPin"), pFilter, phr, L"Out")
{
}

HRESULT CBufferFilterOutputPin::Active()
{
    CAutoLock lock_it(m_pLock);

    if (m_Connected && !m_pOutputQueue) {
        HRESULT hr = NOERROR;

        m_pOutputQueue.Attach(DEBUG_NEW CBufferFilterOutputQueue(m_Connected, &hr));
        if (!m_pOutputQueue) {
            hr = E_OUTOFMEMORY;
        }

        if (FAILED(hr)) {
            m_pOutputQueue.Free();
            return hr;
        }
    }

    return __super::Active();
}

HRESULT CBufferFilterOutputPin::Inactive()
{
    CAutoLock lock_it(m_pLock);
    m_pOutputQueue.Free();
    return __super::Inactive();
}

HRESULT CBufferFilterOutputPin::Deliver(IMediaSample* pMediaSample)
{
    if (!m_pOutputQueue) {
        return NOERROR;
    }
    pMediaSample->AddRef();
    return m_pOutputQueue->Receive(pMediaSample);
}

HRESULT CBufferFilterOutputPin::DeliverEndOfStream()
{
    CallQueue(EOS());
}

HRESULT CBufferFilterOutputPin::DeliverBeginFlush()
{
    CallQueue(BeginFlush());
}

HRESULT CBufferFilterOutputPin::DeliverEndFlush()
{
    CallQueue(EndFlush());
}

HRESULT CBufferFilterOutputPin::DeliverNewSegment(REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
{
    CallQueue(NewSegment(tStart, tStop, dRate));
}