parser.c 24.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Parser (Base for parsers and splitters)
 *
 * Copyright 2003 Robert Shearman
 * Copyright 2004-2005 Christian Costa
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 */

#include "quartz_private.h"
#include "pin.h"

#include "vfwmsgs.h"
#include "amvideo.h"

#include "wine/unicode.h"
#include "wine/debug.h"

#include <math.h>
#include <assert.h>

#include "parser.h"

WINE_DEFAULT_DEBUG_CHANNEL(quartz);

static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
39 40 41
static const IMediaSeekingVtbl Parser_Seeking_Vtbl;
static const IPinVtbl Parser_OutputPin_Vtbl;
static const IPinVtbl Parser_InputPin_Vtbl;
42

43 44 45
static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface);
static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface);
static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface);
46
static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest);
47
static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt);
48 49
static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc);
static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This);
50

51
static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
52
{
53
    return CONTAINING_RECORD(iface, ParserImpl, sourceSeeking.IMediaSeeking_iface);
54 55
}

56 57 58 59 60 61 62 63 64 65
static inline ParserImpl *impl_from_IBaseFilter( IBaseFilter *iface )
{
    return CONTAINING_RECORD(iface, ParserImpl, filter.IBaseFilter_iface);
}

static inline ParserImpl *impl_from_BaseFilter( BaseFilter *iface )
{
    return CONTAINING_RECORD(iface, ParserImpl, filter);
}

66
/* FIXME: WRONG */
67
static IPin* WINAPI Parser_GetPin(BaseFilter *iface, int pos)
68
{
69
    ParserImpl *This = impl_from_BaseFilter(iface);
70 71 72 73 74 75 76 77 78 79 80

    TRACE("Asking for pos %x\n", pos);

    /* Input pin also has a pin, hence the > and not >= */
    if (pos > This->cStreams || pos < 0)
        return NULL;

    IPin_AddRef(This->ppPins[pos]);
    return This->ppPins[pos];
}

81
static LONG WINAPI Parser_GetPinCount(BaseFilter *iface)
82
{
83
    ParserImpl *This = impl_from_BaseFilter(iface);
84 85 86

    return This->cStreams;
}
87

88 89 90 91 92
static const BaseFilterFuncTable BaseFuncTable = {
    Parser_GetPin,
    Parser_GetPinCount
};

93
HRESULT Parser_Create(ParserImpl* pParser, const IBaseFilterVtbl *Parser_Vtbl, const CLSID* pClsid, PFN_PROCESS_SAMPLE fnProcessSample, PFN_QUERY_ACCEPT fnQueryAccept, PFN_PRE_CONNECT fnPreConnect, PFN_CLEANUP fnCleanup, PFN_DISCONNECT fnDisconnect, REQUESTPROC fnRequest, STOPPROCESSPROC fnDone, SourceSeeking_ChangeStop stop, SourceSeeking_ChangeStart start, SourceSeeking_ChangeRate rate)
94 95 96 97 98
{
    HRESULT hr;
    PIN_INFO piInput;

    /* pTransformFilter is already allocated */
99
    BaseFilter_Init(&pParser->filter, Parser_Vtbl, pClsid, (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter"), &BaseFuncTable);
100

101
    pParser->fnDisconnect = fnDisconnect;
102

103 104 105 106 107
    pParser->cStreams = 0;
    pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));

    /* construct input pin */
    piInput.dir = PINDIR_INPUT;
108
    piInput.pFilter = &pParser->filter.IBaseFilter_iface;
109
    lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
110

111 112
    if (!start)
        start = Parser_ChangeStart;
113 114 115 116 117 118 119

    if (!stop)
        stop = Parser_ChangeStop;

    if (!rate)
        rate = Parser_ChangeRate;

120
    SourceSeeking_Init(&pParser->sourceSeeking, &Parser_Seeking_Vtbl, stop, start, rate,  &pParser->filter.csFilter);
121

122
    hr = PullPin_Construct(&Parser_InputPin_Vtbl, &piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, fnCleanup, fnRequest, fnDone, &pParser->filter.csFilter, (IPin **)&pParser->pInputPin);
123 124 125

    if (SUCCEEDED(hr))
    {
126
        pParser->ppPins[0] = &pParser->pInputPin->pin.IPin_iface;
127 128 129 130 131
        pParser->pInputPin->fnPreConnect = fnPreConnect;
    }
    else
    {
        CoTaskMemFree(pParser->ppPins);
132
        BaseFilterImpl_Release(&pParser->filter.IBaseFilter_iface);
133 134 135 136 137 138
        CoTaskMemFree(pParser);
    }

    return hr;
}

139
HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
140
{
141
    ParserImpl *This = impl_from_IBaseFilter(iface);
142 143 144 145
    TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);

    *ppv = NULL;

146 147 148 149
    if ( IsEqualIID(riid, &IID_IUnknown)
      || IsEqualIID(riid, &IID_IPersist)
      || IsEqualIID(riid, &IID_IMediaFilter)
      || IsEqualIID(riid, &IID_IBaseFilter) )
150
        *ppv = This;
151 152 153 154 155 156 157

    if (*ppv)
    {
        IUnknown_AddRef((IUnknown *)(*ppv));
        return S_OK;
    }

158
    if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
159
        FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
160 161 162 163

    return E_NOINTERFACE;
}

164
ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
165
{
166
    return BaseFilterImpl_AddRef(iface);
167 168
}

169
void Parser_Destroy(ParserImpl *This)
170
{
171
    IPin *connected = NULL;
172 173
    ULONG pinref;

174
    assert(!This->filter.refCount);
175
    PullPin_WaitForStateChange(This->pInputPin, INFINITE);
176

177
    /* Don't need to clean up output pins, freeing input pin will do that */
178
    IPin_ConnectedTo(&This->pInputPin->pin.IPin_iface, &connected);
179
    if (connected)
180
    {
181
        assert(IPin_Disconnect(connected) == S_OK);
182
        IPin_Release(connected);
183
        assert(IPin_Disconnect(&This->pInputPin->pin.IPin_iface) == S_OK);
184
    }
185
    pinref = IPin_Release(&This->pInputPin->pin.IPin_iface);
186 187 188 189 190 191 192
    if (pinref)
    {
        /* Valgrind could find this, if I kill it here */
        ERR("pinref should be null, is %u, destroying anyway\n", pinref);
        assert((LONG)pinref > 0);

        while (pinref)
193
            pinref = IPin_Release(&This->pInputPin->pin.IPin_iface);
194 195
    }

196
    CoTaskMemFree(This->ppPins);
197

198 199 200
    TRACE("Destroying parser\n");
    CoTaskMemFree(This);
}
201

202 203
ULONG WINAPI Parser_Release(IBaseFilter * iface)
{
204
    ParserImpl *This = impl_from_IBaseFilter(iface);
205
    ULONG refCount = BaseFilterImpl_Release(iface);
206

207 208 209 210 211 212
    TRACE("(%p)->() Release from %d\n", This, refCount + 1);

    if (!refCount)
        Parser_Destroy(This);

    return refCount;
213 214 215 216
}

/** IPersist methods **/

217
HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
218
{
219
    ParserImpl *This = impl_from_IBaseFilter(iface);
220

221 222
    TRACE("(%p)\n", pClsid);

223
    *pClsid = This->filter.clsid;
224 225 226 227 228 229

    return S_OK;
}

/** IMediaFilter methods **/

230
HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
231
{
232
    ParserImpl *This = impl_from_IBaseFilter(iface);
233
    PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
234
    ULONG i;
235 236 237

    TRACE("()\n");

238
    EnterCriticalSection(&pin->thread_lock);
239 240

    IAsyncReader_BeginFlush(This->pInputPin->pReader);
241
    EnterCriticalSection(&This->filter.csFilter);
242

243
    if (This->filter.state == State_Stopped)
244
    {
245
        LeaveCriticalSection(&This->filter.csFilter);
246
        IAsyncReader_EndFlush(This->pInputPin->pReader);
247 248
        LeaveCriticalSection(&pin->thread_lock);
        return S_OK;
249
    }
250

251
    This->filter.state = State_Stopped;
252 253 254

    for (i = 1; i < (This->cStreams + 1); i++)
    {
255
        BaseOutputPinImpl_Inactive((BaseOutputPin *)This->ppPins[i]);
256 257
    }

258
    LeaveCriticalSection(&This->filter.csFilter);
259 260 261

    PullPin_PauseProcessing(This->pInputPin);
    PullPin_WaitForStateChange(This->pInputPin, INFINITE);
262
    IAsyncReader_EndFlush(This->pInputPin->pReader);
263

264
    LeaveCriticalSection(&pin->thread_lock);
265
    return S_OK;
266 267
}

268
HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
269 270
{
    HRESULT hr = S_OK;
271
    ParserImpl *This = impl_from_IBaseFilter(iface);
272
    PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
273

274 275
    TRACE("()\n");

276
    EnterCriticalSection(&pin->thread_lock);
277
    EnterCriticalSection(&This->filter.csFilter);
278

279
    if (This->filter.state == State_Paused)
280
    {
281
        LeaveCriticalSection(&This->filter.csFilter);
282
        LeaveCriticalSection(&pin->thread_lock);
283
        return S_OK;
284 285
    }

286
    if (This->filter.state == State_Stopped)
287
    {
288
        LeaveCriticalSection(&This->filter.csFilter);
289
        hr = IBaseFilter_Run(iface, -1);
290
        EnterCriticalSection(&This->filter.csFilter);
291 292
    }

293
    if (SUCCEEDED(hr))
294
        This->filter.state = State_Paused;
295

296
    LeaveCriticalSection(&This->filter.csFilter);
297
    LeaveCriticalSection(&pin->thread_lock);
298

299 300 301
    return hr;
}

302
HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
303 304
{
    HRESULT hr = S_OK;
305
    ParserImpl *This = impl_from_IBaseFilter(iface);
306
    PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
307

308
    ULONG i;
309 310 311

    TRACE("(%s)\n", wine_dbgstr_longlong(tStart));

312
    EnterCriticalSection(&pin->thread_lock);
313
    EnterCriticalSection(&This->filter.csFilter);
314
    {
315 316
        HRESULT hr_any = VFW_E_NOT_CONNECTED;

317
        This->filter.rtStreamStart = tStart;
318
        if (This->filter.state == State_Running || This->filter.state == State_Paused)
319
        {
320 321
            This->filter.state = State_Running;
            LeaveCriticalSection(&This->filter.csFilter);
322
            LeaveCriticalSection(&pin->thread_lock);
323 324 325
            return S_OK;
        }

326
        for (i = 1; i < (This->cStreams + 1); i++)
327
        {
328
            hr = BaseOutputPinImpl_Active((BaseOutputPin *)This->ppPins[i]);
329 330
            if (SUCCEEDED(hr))
                hr_any = hr;
331
        }
332

333
        hr = hr_any;
334
        if (SUCCEEDED(hr))
335
        {
336
            LeaveCriticalSection(&This->filter.csFilter);
337
            hr = PullPin_StartProcessing(This->pInputPin);
338
            EnterCriticalSection(&This->filter.csFilter);
339
        }
340 341

        if (SUCCEEDED(hr))
342
            This->filter.state = State_Running;
343
    }
344
    LeaveCriticalSection(&This->filter.csFilter);
345
    LeaveCriticalSection(&pin->thread_lock);
346 347 348 349

    return hr;
}

350
HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
351
{
352
    ParserImpl *This = impl_from_IBaseFilter(iface);
353
    PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
354
    HRESULT hr = S_OK;
355

356
    TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
357

358
    EnterCriticalSection(&pin->thread_lock);
359
    EnterCriticalSection(&This->filter.csFilter);
360
    {
361
        *pState = This->filter.state;
362
    }
363
    LeaveCriticalSection(&This->filter.csFilter);
364 365

    if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
366 367
        hr = VFW_S_STATE_INTERMEDIATE;
    LeaveCriticalSection(&pin->thread_lock);
368

369
    return hr;
370 371
}

372
HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
373
{
374
    ParserImpl *This = impl_from_IBaseFilter(iface);
375
    PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
376 377 378

    TRACE("(%p)\n", pClock);

379
    EnterCriticalSection(&pin->thread_lock);
380
    BaseFilterImpl_SetSyncSource(iface,pClock);
381
    LeaveCriticalSection(&pin->thread_lock);
382 383 384 385

    return S_OK;
}

386
HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
387
{
388
    return BaseFilterImpl_GetSyncSource(iface, ppClock);
389 390 391 392
}

/** IBaseFilter implementation **/

393
HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
394
{
395
    return BaseFilterImpl_EnumPins(iface,ppEnum);
396 397
}

398
HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
399 400 401 402 403 404 405 406
{
    FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);

    /* FIXME: critical section */

    return E_NOTIMPL;
}

407
HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
408
{
409
    return BaseFilterImpl_QueryFilterInfo(iface, pInfo);
410 411
}

412
HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
413
{
414
    return BaseFilterImpl_JoinFilterGraph(iface, pGraph, pName);
415 416
}

417
HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
418
{
419
    return BaseFilterImpl_QueryVendorInfo(iface, pVendorInfo);
420 421
}

422 423
static const  BasePinFuncTable output_BaseFuncTable = {
    NULL,
424 425 426
    BaseOutputPinImpl_AttemptConnection,
    BasePinImpl_GetMediaTypeVersion,
    Parser_OutputPin_GetMediaType
427 428 429
};

static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
430 431 432
    Parser_OutputPin_DecideBufferSize,
    Parser_OutputPin_DecideAllocator,
    Parser_OutputPin_BreakConnect
433 434
};

435
HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
436 437 438 439 440 441
{
    IPin ** ppOldPins;
    HRESULT hr;

    ppOldPins = This->ppPins;

442
    This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
443 444
    memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));

445
    hr = BaseOutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, &output_BaseFuncTable, &output_BaseOutputFuncTable, &This->filter.csFilter, This->ppPins + (This->cStreams + 1));
446 447 448

    if (SUCCEEDED(hr))
    {
449
        IPin *pPin = This->ppPins[This->cStreams + 1];
450
        Parser_OutputPin *pin = unsafe_impl_Parser_OutputPin_from_IPin(pPin);
451 452 453 454
        pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
        CopyMediaType(pin->pmt, amt);
        pin->dwSamplesProcessed = 0;

455
        pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
456
        pin->allocProps = *props;
457
        This->cStreams++;
458
        BaseFilterImpl_IncrementPinVersion(&This->filter);
459
        CoTaskMemFree(ppOldPins);
460 461 462
    }
    else
    {
463
        CoTaskMemFree(This->ppPins);
464
        This->ppPins = ppOldPins;
465
        ERR("Failed with error %x\n", hr);
466 467 468 469 470 471 472 473
    }

    return hr;
}

static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
{
    /* NOTE: should be in critical section when calling this function */
474
    HRESULT hr;
475 476 477
    ULONG i;
    IPin ** ppOldPins = This->ppPins;

478 479
    TRACE("(%p)\n", This);

480
    /* reduce the pin array down to 1 (just our input pin) */
481
    This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
482 483 484 485
    memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);

    for (i = 0; i < This->cStreams; i++)
    {
486
        hr = ((BaseOutputPin *)ppOldPins[i + 1])->pFuncsTable->pfnBreakConnect((BaseOutputPin *)ppOldPins[i + 1]);
487
        TRACE("Disconnect: %08x\n", hr);
488 489 490
        IPin_Release(ppOldPins[i + 1]);
    }

491
    BaseFilterImpl_IncrementPinVersion(&This->filter);
492
    This->cStreams = 0;
493
    CoTaskMemFree(ppOldPins);
494 495 496 497

    return S_OK;
}

498
static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface)
499
{
500
    FIXME("(%p) filter hasn't implemented start position change!\n", iface);
501 502 503
    return S_OK;
}

504
static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface)
505
{
506
    FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
507 508 509
    return S_OK;
}

510
static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface)
511
{
512
    FIXME("(%p) filter hasn't implemented rate change!\n", iface);
513 514 515 516 517 518
    return S_OK;
}


static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
{
519
    ParserImpl *This = impl_from_IMediaSeeking(iface);
520 521 522 523 524 525

    return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
}

static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
{
526
    ParserImpl *This = impl_from_IMediaSeeking(iface);
527 528 529 530 531 532

    return IUnknown_AddRef((IUnknown *)This);
}

static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
{
533
    ParserImpl *This = impl_from_IMediaSeeking(iface);
534 535 536 537 538 539 540 541 542

    return IUnknown_Release((IUnknown *)This);
}

static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
{
    Parser_Seeking_QueryInterface,
    Parser_Seeking_AddRef,
    Parser_Seeking_Release,
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    SourceSeekingImpl_GetCapabilities,
    SourceSeekingImpl_CheckCapabilities,
    SourceSeekingImpl_IsFormatSupported,
    SourceSeekingImpl_QueryPreferredFormat,
    SourceSeekingImpl_GetTimeFormat,
    SourceSeekingImpl_IsUsingTimeFormat,
    SourceSeekingImpl_SetTimeFormat,
    SourceSeekingImpl_GetDuration,
    SourceSeekingImpl_GetStopPosition,
    SourceSeekingImpl_GetCurrentPosition,
    SourceSeekingImpl_ConvertTimeFormat,
    SourceSeekingImpl_SetPositions,
    SourceSeekingImpl_GetPositions,
    SourceSeekingImpl_GetAvailable,
    SourceSeekingImpl_SetRate,
    SourceSeekingImpl_GetRate,
    SourceSeekingImpl_GetPreroll
560 561
};

562
static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
563
{
564
    Parser_OutputPin *This = (Parser_OutputPin*)iface;
565 566 567 568 569 570 571 572 573 574 575 576 577 578
    ALLOCATOR_PROPERTIES actual;

    if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
        FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
    if (ppropInputRequest->cbPrefix)
        FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
    if (ppropInputRequest->cbBuffer)
        FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
    if (ppropInputRequest->cBuffers)
        FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);

    return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
}

579
static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
580
{
581
    Parser_OutputPin *This = (Parser_OutputPin*)iface;
582 583 584 585 586 587 588 589
    if (iPosition < 0)
        return E_INVALIDARG;
    if (iPosition > 0)
        return VFW_S_NO_MORE_ITEMS;
    CopyMediaType(pmt, This->pmt);
    return S_OK;
}

590 591
static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
{
592
    Parser_OutputPin *This = (Parser_OutputPin*)iface;
593 594
    HRESULT hr;

595
    *pAlloc = NULL;
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

    if (This->alloc)
        hr = IMemInputPin_NotifyAllocator(pPin, This->alloc, This->readonly);
    else
        hr = VFW_E_NO_ALLOCATOR;

    return hr;
}

static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This)
{
    HRESULT hr;

    TRACE("(%p)->()\n", This);

    EnterCriticalSection(This->pin.pCritSec);
    if (!This->pin.pConnectedTo || !This->pMemInputPin)
        hr = VFW_E_NOT_CONNECTED;
    else
    {
        hr = IPin_Disconnect(This->pin.pConnectedTo);
617
        IPin_Disconnect(&This->pin.IPin_iface);
618 619 620 621 622 623 624
    }
    LeaveCriticalSection(This->pin.pCritSec);

    return hr;
}


625
static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
626
{
627
    Parser_OutputPin *This = unsafe_impl_Parser_OutputPin_from_IPin(iface);
628 629 630 631 632 633

    TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);

    *ppv = NULL;

    if (IsEqualIID(riid, &IID_IUnknown))
634
        *ppv = iface;
635
    else if (IsEqualIID(riid, &IID_IPin))
636
        *ppv = iface;
637
    /* The Parser filter does not support querying IMediaSeeking, return it directly */
638
    else if (IsEqualIID(riid, &IID_IMediaSeeking))
639
        *ppv = &impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->sourceSeeking;
640 641 642 643 644 645 646 647 648 649 650 651 652 653

    if (*ppv)
    {
        IUnknown_AddRef((IUnknown *)(*ppv));
        return S_OK;
    }

    FIXME("No interface for %s!\n", qzdebugstr_guid(riid));

    return E_NOINTERFACE;
}

static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
{
654
    Parser_OutputPin *This = unsafe_impl_Parser_OutputPin_from_IPin(iface);
655 656
    ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
    
657
    TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
658

659 660
    if (!refCount)
    {
661
        FreeMediaType(This->pmt);
662
        CoTaskMemFree(This->pmt);
663
        FreeMediaType(&This->pin.pin.mtCurrent);
664 665 666 667 668 669
        CoTaskMemFree(This);
        return 0;
    }
    return refCount;
}

670 671
static HRESULT WINAPI Parser_OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
{
672
    Parser_OutputPin *This = unsafe_impl_Parser_OutputPin_from_IPin(iface);
673
    ParserImpl *parser = impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter);
674 675 676

    /* Set the allocator to our input pin's */
    EnterCriticalSection(This->pin.pin.pCritSec);
677
    This->alloc = parser->pInputPin->pAlloc;
678 679
    LeaveCriticalSection(This->pin.pin.pCritSec);

680
    return BaseOutputPinImpl_Connect(iface, pReceivePin, pmt);
681 682
}

683
static HRESULT WINAPI Parser_OutputPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE * pmt)
684
{
685
    Parser_OutputPin *This = unsafe_impl_Parser_OutputPin_from_IPin(iface);
686 687 688 689 690 691 692 693 694 695

    TRACE("()\n");
    dump_AM_MEDIA_TYPE(pmt);

    return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
}

static const IPinVtbl Parser_OutputPin_Vtbl = 
{
    Parser_OutputPin_QueryInterface,
696
    BasePinImpl_AddRef,
697
    Parser_OutputPin_Release,
698
    Parser_OutputPin_Connect,
699 700
    BaseOutputPinImpl_ReceiveConnection,
    BaseOutputPinImpl_Disconnect,
701 702 703 704 705 706
    BasePinImpl_ConnectedTo,
    BasePinImpl_ConnectionMediaType,
    BasePinImpl_QueryPinInfo,
    BasePinImpl_QueryDirection,
    BasePinImpl_QueryId,
    Parser_OutputPin_QueryAccept,
707
    BasePinImpl_EnumMediaTypes,
708
    BasePinImpl_QueryInternalConnections,
709 710 711
    BaseOutputPinImpl_EndOfStream,
    BaseOutputPinImpl_BeginFlush,
    BaseOutputPinImpl_EndFlush,
712
    BasePinImpl_NewSegment
713 714
};

715 716
static HRESULT WINAPI Parser_PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
{
717
    PullPin *This = impl_PullPin_from_IPin(iface);
718 719 720 721 722 723 724 725 726 727 728

    TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);

    *ppv = NULL;

    /*
     * It is important to capture the request for the IMediaSeeking interface before it is passed
     * on to PullPin_QueryInterface, this is necessary since the Parser filter does not support
     * querying IMediaSeeking
     */
    if (IsEqualIID(riid, &IID_IMediaSeeking))
729
        *ppv = &impl_from_IBaseFilter(This->pin.pinInfo.pFilter)->sourceSeeking;
730 731 732 733 734 735 736 737 738 739

    if (*ppv)
    {
        IUnknown_AddRef((IUnknown *)(*ppv));
        return S_OK;
    }

    return PullPin_QueryInterface(iface, riid, ppv);
}

740
static HRESULT WINAPI Parser_PullPin_Disconnect(IPin * iface)
741 742
{
    HRESULT hr;
743
    PullPin *This = impl_PullPin_from_IPin(iface);
744 745 746

    TRACE("()\n");

747 748
    EnterCriticalSection(&This->thread_lock);
    EnterCriticalSection(This->pin.pCritSec);
749
    {
750
        if (This->pin.pConnectedTo)
751 752
        {
            FILTER_STATE state;
753
            ParserImpl *Parser = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
754

755 756 757
            LeaveCriticalSection(This->pin.pCritSec);
            hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
            EnterCriticalSection(This->pin.pCritSec);
758

759
            if (SUCCEEDED(hr) && (state == State_Stopped) && SUCCEEDED(Parser->fnDisconnect(Parser)))
760
            {
761 762 763
                LeaveCriticalSection(This->pin.pCritSec);
                PullPin_Disconnect(iface);
                EnterCriticalSection(This->pin.pCritSec);
764
                hr = Parser_RemoveOutputPins(impl_from_IBaseFilter(This->pin.pinInfo.pFilter));
765 766 767 768 769 770 771
            }
            else
                hr = VFW_E_NOT_STOPPED;
        }
        else
            hr = S_FALSE;
    }
772 773 774
    LeaveCriticalSection(This->pin.pCritSec);
    LeaveCriticalSection(&This->thread_lock);

775 776 777
    return hr;
}

778
static HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
779 780 781 782 783 784 785 786
{
    HRESULT hr;

    TRACE("()\n");

    hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
    if (FAILED(hr))
    {
787
        BasePin *This = (BasePin *)iface;
788 789

        EnterCriticalSection(This->pCritSec);
790
        Parser_RemoveOutputPins(impl_from_IBaseFilter(This->pinInfo.pFilter));
791 792 793 794 795 796
        LeaveCriticalSection(This->pCritSec);
    }

    return hr;
}

797 798 799 800 801 802 803 804 805
static HRESULT WINAPI Parser_PullPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
{
    BasePin *This = (BasePin *)iface;

    TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);

    return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
}

806 807
static const IPinVtbl Parser_InputPin_Vtbl =
{
808
    Parser_PullPin_QueryInterface,
809
    BasePinImpl_AddRef,
810
    PullPin_Release,
811
    BaseInputPinImpl_Connect,
812
    Parser_PullPin_ReceiveConnection,
813
    Parser_PullPin_Disconnect,
814 815 816 817 818 819
    BasePinImpl_ConnectedTo,
    BasePinImpl_ConnectionMediaType,
    BasePinImpl_QueryPinInfo,
    BasePinImpl_QueryDirection,
    BasePinImpl_QueryId,
    PullPin_QueryAccept,
820
    Parser_PullPin_EnumMediaTypes,
821
    BasePinImpl_QueryInternalConnections,
822 823 824 825 826
    PullPin_EndOfStream,
    PullPin_BeginFlush,
    PullPin_EndFlush,
    PullPin_NewSegment
};