waveparser.c 13.5 KB
Newer Older
Christian Costa's avatar
Christian Costa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * WAVE Parser Filter
 *
 * Copyright 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Christian Costa's avatar
Christian Costa committed
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
 */

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

#include "uuids.h"
#include "aviriff.h"
#include "vfwmsgs.h"
#include "mmsystem.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 wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};

typedef struct WAVEParserImpl
{
    ParserImpl Parser;
    LONGLONG StartOfFile; /* in media time */
    LONGLONG EndOfFile;
46 47
    DWORD nAvgBytesPerSec;
    DWORD nBlockAlign;
Christian Costa's avatar
Christian Costa committed
48 49
} WAVEParserImpl;

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

55 56 57 58 59
static inline WAVEParserImpl *impl_from_IBaseFilter( IBaseFilter *iface )
{
    return CONTAINING_RECORD(iface, WAVEParserImpl, Parser.filter.IBaseFilter_iface);
}

60 61 62 63
static LONGLONG bytepos_to_duration(WAVEParserImpl *This, LONGLONG bytepos)
{
    LONGLONG duration = BYTES_FROM_MEDIATIME(bytepos - This->StartOfFile);
    duration *= 10000000;
64
    duration /= This->nAvgBytesPerSec;
65 66 67 68 69 70 71 72

    return duration;
}

static LONGLONG duration_to_bytepos(WAVEParserImpl *This, LONGLONG duration)
{
    LONGLONG bytepos;

73
    bytepos = This->nAvgBytesPerSec;
74 75
    bytepos *= duration;
    bytepos /= 10000000;
76
    bytepos -= bytepos % This->nBlockAlign;
77 78 79 80 81
    bytepos += BYTES_FROM_MEDIATIME(This->StartOfFile);

    return MEDIATIME_FROM_BYTES(bytepos);
}

82
static HRESULT WAVEParser_Sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
Christian Costa's avatar
Christian Costa committed
83
{
84
    WAVEParserImpl *This = iface;
Christian Costa's avatar
Christian Costa committed
85
    LPBYTE pbSrcStream = NULL;
86
    ULONG cbSrcStream = 0;
Christian Costa's avatar
Christian Costa committed
87 88
    REFERENCE_TIME tStart, tStop;
    HRESULT hr;
89 90 91
    IMediaSample *newsample = NULL;
    Parser_OutputPin *pOutputPin;
    PullPin *pin = This->Parser.pInputPin;
Christian Costa's avatar
Christian Costa committed
92

93
    IMediaSample_GetPointer(pSample, &pbSrcStream);
Christian Costa's avatar
Christian Costa committed
94 95 96 97
    hr = IMediaSample_GetTime(pSample, &tStart, &tStop);

    cbSrcStream = IMediaSample_GetActualDataLength(pSample);

98
    /* Flush occurring */
99 100 101 102 103
    if (cbSrcStream == 0)
    {
        TRACE(".. Why do I need you?\n");
        return S_OK;
    }
Christian Costa's avatar
Christian Costa committed
104

105
    pOutputPin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
Christian Costa's avatar
Christian Costa committed
106

107 108
    if (SUCCEEDED(hr))
        hr = IMemAllocator_GetBuffer(pin->pAlloc, &newsample, NULL, NULL, 0);
Christian Costa's avatar
Christian Costa committed
109

110
    if (SUCCEEDED(hr))
Christian Costa's avatar
Christian Costa committed
111
    {
112 113 114
        LONGLONG rtSampleStart = pin->rtNext;
        /* Add 4 for the next header, which should hopefully work */
        LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(newsample));
Christian Costa's avatar
Christian Costa committed
115

116 117
        if (rtSampleStop > pin->rtStop)
            rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
Christian Costa's avatar
Christian Costa committed
118

119
        IMediaSample_SetTime(newsample, &rtSampleStart, &rtSampleStop);
Christian Costa's avatar
Christian Costa committed
120

121 122
        pin->rtCurrent = pin->rtNext;
        pin->rtNext = rtSampleStop;
Christian Costa's avatar
Christian Costa committed
123

124 125 126
        IMediaSample_SetPreroll(newsample, FALSE);
        IMediaSample_SetDiscontinuity(newsample, FALSE);
        IMediaSample_SetSyncPoint(newsample, TRUE);
Christian Costa's avatar
Christian Costa committed
127

128 129
        hr = IAsyncReader_Request(pin->pReader, newsample, 0);
    }
130

131 132 133
    if (SUCCEEDED(hr))
    {
        REFERENCE_TIME tAviStart, tAviStop;
Christian Costa's avatar
Christian Costa committed
134

135 136
        IMediaSample_SetSyncPoint(pSample, TRUE);
        pOutputPin->dwSamplesProcessed++;
137

138 139
        tAviStart = bytepos_to_duration(This, tStart);
        tAviStop = bytepos_to_duration(This, tStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample)));
Christian Costa's avatar
Christian Costa committed
140

141
        IMediaSample_SetTime(pSample, &tAviStart, &tAviStop);
Christian Costa's avatar
Christian Costa committed
142

143
        hr = BaseOutputPinImpl_Deliver(&pOutputPin->pin, pSample);
144
        if (hr != S_OK && hr != S_FALSE && hr != VFW_E_WRONG_STATE)
145
            ERR("Error sending sample (%x)\n", hr);
146 147 148
        else if (hr != S_OK)
            /* Unset progression if denied! */
            This->Parser.pInputPin->rtCurrent = tStart;
Christian Costa's avatar
Christian Costa committed
149 150
    }

151
    if (tStop >= This->EndOfFile || (bytepos_to_duration(This, tStop) >= This->Parser.sourceSeeking.llStop) || hr == VFW_E_NOT_CONNECTED)
152
    {
153
        unsigned int i;
154 155 156 157 158 159 160 161

        TRACE("End of file reached\n");

        for (i = 0; i < This->Parser.cStreams; i++)
        {
            IPin* ppin;
            HRESULT hr;

162
            TRACE("Send End Of Stream to output pin %u\n", i);
163 164 165 166 167 168 169 170 171

            hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
            if (SUCCEEDED(hr))
            {
                hr = IPin_EndOfStream(ppin);
                IPin_Release(ppin);
            }
            if (FAILED(hr))
            {
172
                ERR("%x\n", hr);
173 174 175 176 177 178 179 180
                break;
            }
        }

        /* Force the pullpin thread to stop */
        hr = S_FALSE;
    }

Christian Costa's avatar
Christian Costa committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194
    return hr;
}

static HRESULT WAVEParser_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
{
    if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
	return S_FALSE;
    if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_WAVE))
        return S_OK;
    if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AU) || IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AIFF))
	FIXME("AU and AIFF files not supported yet!\n");
    return S_FALSE;
}

195
static HRESULT WINAPI WAVEParserImpl_seek(IMediaSeeking *iface)
196
{
197
    WAVEParserImpl *This = impl_from_IMediaSeeking(iface);
198 199 200 201
    PullPin *pPin = This->Parser.pInputPin;
    IPin *victim = NULL;
    LONGLONG newpos, curpos, endpos, bytepos;

202
    newpos = This->Parser.sourceSeeking.llCurrent;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    curpos = bytepos_to_duration(This, pPin->rtCurrent);
    endpos = bytepos_to_duration(This, This->EndOfFile);
    bytepos = duration_to_bytepos(This, newpos);

    if (newpos > endpos)
    {
        WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(endpos>>32), (DWORD)endpos);
        return E_INVALIDARG;
    }

    if (curpos/1000000 == newpos/1000000)
    {
        TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(curpos>>32), (DWORD)curpos);
        return S_OK;
    }

    TRACE("Moving sound to %08u bytes!\n", (DWORD)BYTES_FROM_MEDIATIME(bytepos));

    EnterCriticalSection(&pPin->thread_lock);
222
    IPin_BeginFlush(&pPin->pin.IPin_iface);
223 224

    /* Make sure this is done while stopped, BeginFlush takes care of this */
225
    EnterCriticalSection(&This->Parser.filter.csFilter);
226 227 228 229 230 231 232 233
    IPin_ConnectedTo(This->Parser.ppPins[1], &victim);
    if (victim)
    {
        IPin_NewSegment(victim, newpos, endpos, pPin->dRate);
        IPin_Release(victim);
    }

    pPin->rtStart = pPin->rtCurrent = bytepos;
234
    unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1])->dwSamplesProcessed = 0;
235
    LeaveCriticalSection(&This->Parser.filter.csFilter);
236 237

    TRACE("Done flushing\n");
238
    IPin_EndFlush(&pPin->pin.IPin_iface);
239 240 241 242 243
    LeaveCriticalSection(&pPin->thread_lock);

    return S_OK;
}

244
static HRESULT WAVEParser_InputPin_PreConnect(IPin * iface, IPin * pConnectPin, ALLOCATOR_PROPERTIES *props)
Christian Costa's avatar
Christian Costa committed
245
{
246
    PullPin *This = impl_PullPin_from_IPin(iface);
Christian Costa's avatar
Christian Costa committed
247 248 249 250 251 252
    HRESULT hr;
    RIFFLIST list;
    RIFFCHUNK chunk;
    LONGLONG pos = 0; /* in bytes */
    PIN_INFO piOutput;
    AM_MEDIA_TYPE amt;
253
    WAVEParserImpl * pWAVEParser = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
Christian Costa's avatar
Christian Costa committed
254 255

    piOutput.dir = PINDIR_OUTPUT;
256
    piOutput.pFilter = &pWAVEParser->Parser.filter.IBaseFilter_iface;
257
    lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
Christian Costa's avatar
Christian Costa committed
258 259 260 261
    
    hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
    pos += sizeof(list);

262
   if (list.fcc != FOURCC_RIFF)
Christian Costa's avatar
Christian Costa committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    {
        ERR("Input stream not a RIFF file\n");
        return E_FAIL;
    }
    if (list.cb > 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
    {
        ERR("Input stream violates RIFF spec\n");
        return E_FAIL;
    }
    if (list.fccListType != mmioFOURCC('W','A','V','E'))
    {
        ERR("Input stream not an WAVE RIFF file\n");
        return E_FAIL;
    }

    hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
    pos += sizeof(chunk);
    if (chunk.fcc != mmioFOURCC('f','m','t',' '))
    {
        ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
        return E_FAIL;
    }

286 287
    amt.majortype = MEDIATYPE_Audio;
    amt.formattype = FORMAT_WaveFormatEx;
Christian Costa's avatar
Christian Costa committed
288 289 290
    amt.cbFormat = chunk.cb;
    amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
    amt.pUnk = NULL;
291
    IAsyncReader_SyncRead(This->pReader, pos, amt.cbFormat, amt.pbFormat);
292
    amt.subtype = MEDIATYPE_Audio;
Christian Costa's avatar
Christian Costa committed
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
    amt.subtype.Data1 = ((WAVEFORMATEX*)amt.pbFormat)->wFormatTag;

    pos += chunk.cb;
    hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
    if (chunk.fcc == mmioFOURCC('f','a','c','t'))
    {
        FIXME("'fact' chunk not supported yet\n");
	pos += sizeof(chunk) + chunk.cb;
	hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
    }
    if (chunk.fcc != mmioFOURCC('d','a','t','a'))
    {
        ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
        return E_FAIL;
    }

    if (hr == S_OK)
    {
        pWAVEParser->StartOfFile = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFCHUNK));
        pWAVEParser->EndOfFile = MEDIATIME_FROM_BYTES(pos + chunk.cb + sizeof(RIFFCHUNK));
    }

    if (hr != S_OK)
        return E_FAIL;

318 319 320
    props->cbAlign = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
    props->cbPrefix = 0;
    props->cbBuffer = 4096;
321
    props->cBuffers = 3;
322 323
    pWAVEParser->nBlockAlign = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
    pWAVEParser->nAvgBytesPerSec = ((WAVEFORMATEX*)amt.pbFormat)->nAvgBytesPerSec;
324
    hr = Parser_AddPin(&(pWAVEParser->Parser), &piOutput, props, &amt);
325 326
    CoTaskMemFree(amt.pbFormat);

327 328 329
    pWAVEParser->Parser.sourceSeeking.llCurrent = 0;
    pWAVEParser->Parser.sourceSeeking.llStop = pWAVEParser->Parser.sourceSeeking.llDuration = bytepos_to_duration(pWAVEParser, pWAVEParser->EndOfFile);
    TRACE("Duration: %u seconds\n", (DWORD)(pWAVEParser->Parser.sourceSeeking.llDuration / (LONGLONG)10000000));
330 331 332 333

    This->rtStop = pWAVEParser->EndOfFile;
    This->rtStart = pWAVEParser->StartOfFile;

Christian Costa's avatar
Christian Costa committed
334 335 336 337 338
    TRACE("WAVE File ok\n");

    return hr;
}

339 340
static HRESULT WAVEParser_Cleanup(LPVOID iface)
{
341
    WAVEParserImpl *This = iface;
342 343 344 345 346 347

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

    return S_OK;
}

348 349
static HRESULT WAVEParser_first_request(LPVOID iface)
{
350
    WAVEParserImpl *This = iface;
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    PullPin *pin = This->Parser.pInputPin;
    HRESULT hr;
    IMediaSample *sample;

    if (pin->rtCurrent >= pin->rtStop)
    {
        /* Last sample has already been queued, request nothing more */
        TRACE("Done!\n");
        return S_OK;
    }

    hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);

    pin->rtNext = pin->rtCurrent;
    if (SUCCEEDED(hr))
    {
        LONGLONG rtSampleStart = pin->rtNext;
        /* Add 4 for the next header, which should hopefully work */
        LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(sample));
370
        Parser_OutputPin *outpin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
371 372 373 374

        if (rtSampleStop > pin->rtStop)
            rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));

375
        IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
376 377 378 379 380 381 382 383 384 385 386 387 388

        pin->rtCurrent = pin->rtNext;
        pin->rtNext = rtSampleStop;

        IMediaSample_SetPreroll(sample, FALSE);
        if (!outpin->dwSamplesProcessed++)
            IMediaSample_SetDiscontinuity(sample, TRUE);
        else
            IMediaSample_SetDiscontinuity(sample, FALSE);

        hr = IAsyncReader_Request(pin->pReader, sample, 0);
    }
    if (FAILED(hr))
389
        ERR("Horsemen of the apocalypse came to bring error 0x%08x %p\n", hr, sample);
390 391 392 393

    return hr;
}

394 395 396 397 398 399
static HRESULT WAVEParser_disconnect(LPVOID iface)
{
    /* TODO: Find and plug memory leaks */
    return S_OK;
}

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
static const IBaseFilterVtbl WAVEParser_Vtbl =
{
    Parser_QueryInterface,
    Parser_AddRef,
    Parser_Release,
    Parser_GetClassID,
    Parser_Stop,
    Parser_Pause,
    Parser_Run,
    Parser_GetState,
    Parser_SetSyncSource,
    Parser_GetSyncSource,
    Parser_EnumPins,
    Parser_FindPin,
    Parser_QueryFilterInfo,
    Parser_JoinFilterGraph,
    Parser_QueryVendorInfo
};

Christian Costa's avatar
Christian Costa committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
HRESULT WAVEParser_create(IUnknown * pUnkOuter, LPVOID * ppv)
{
    HRESULT hr;
    WAVEParserImpl * This;

    TRACE("(%p, %p)\n", pUnkOuter, ppv);

    *ppv = NULL;

    if (pUnkOuter)
        return CLASS_E_NOAGGREGATION;

    /* Note: This memory is managed by the transform filter once created */
    This = CoTaskMemAlloc(sizeof(WAVEParserImpl));

434
    hr = Parser_Create(&(This->Parser), &WAVEParser_Vtbl, &CLSID_WAVEParser, WAVEParser_Sample, WAVEParser_QueryAccept, WAVEParser_InputPin_PreConnect, WAVEParser_Cleanup, WAVEParser_disconnect, WAVEParser_first_request, NULL, NULL, WAVEParserImpl_seek, NULL);
Christian Costa's avatar
Christian Costa committed
435 436 437 438

    if (FAILED(hr))
        return hr;

439
    *ppv = &This->Parser.filter.IBaseFilter_iface;
Christian Costa's avatar
Christian Costa committed
440 441 442

    return hr;
}