avidec.c 13.5 KB
Newer Older
1 2 3
/*
 * AVI Decompressor (VFW decompressors wrapper)
 *
4
 * Copyright 2004-2005 Christian Costa
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

#include "config.h"

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

#include "uuids.h"
#include "amvideo.h"
#include "windef.h"
#include "winbase.h"
#include "dshow.h"
#include "strmif.h"
#include "vfwmsgs.h"
#include "vfw.h"
34
#include "dvdmedia.h"
35 36 37 38 39 40

#include <assert.h>

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

41
WINE_DEFAULT_DEBUG_CHANNEL(quartz);
42 43 44

typedef struct AVIDecImpl
{
45 46 47
    TransformFilter tf;
    IUnknown *seekthru_unk;

48
    HIC hvid;
Christian Costa's avatar
Christian Costa committed
49 50
    BITMAPINFOHEADER* pBihIn;
    BITMAPINFOHEADER* pBihOut;
51
    REFERENCE_TIME late;
52 53
} AVIDecImpl;

54 55 56
static const IBaseFilterVtbl AVIDec_Vtbl;

static HRESULT WINAPI AVIDec_StartStreaming(TransformFilter* pTransformFilter)
Christian Costa's avatar
Christian Costa committed
57 58 59 60 61
{
    AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
    DWORD result;

    TRACE("(%p)->()\n", This);
62
    This->late = -1;
Christian Costa's avatar
Christian Costa committed
63 64 65 66

    result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
    if (result != ICERR_OK)
    {
67
        ERR("Cannot start processing (%d)\n", result);
Christian Costa's avatar
Christian Costa committed
68 69 70 71 72
	return E_FAIL;
    }
    return S_OK;
}

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
static HRESULT WINAPI AVIDec_EndFlush(TransformFilter *pTransformFilter) {
    AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
    This->late = -1;
    return S_OK;
}

static HRESULT WINAPI AVIDec_NotifyDrop(TransformFilter *pTransformFilter, IBaseFilter *sender, Quality qm) {
    AVIDecImpl *This = (AVIDecImpl*)pTransformFilter;

    EnterCriticalSection(&This->tf.filter.csFilter);
    if (qm.Late > 0)
        This->late = qm.Late + qm.TimeStamp;
    else
        This->late = -1;
    LeaveCriticalSection(&This->tf.filter.csFilter);
    return S_OK;
}

static int AVIDec_DropSample(AVIDecImpl *This, REFERENCE_TIME tStart) {
    if (This->late < 0)
        return 0;

    if (tStart < This->late) {
        TRACE("Dropping sample\n");
        return 1;
    }
    This->late = -1;
    return 0;
}

103
static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
104
{
105
    AVIDecImpl* This = (AVIDecImpl *)tf;
106 107 108
    AM_MEDIA_TYPE amt;
    HRESULT hr;
    DWORD res;
109
    IMediaSample* pOutSample = NULL;
110 111
    DWORD cbDstStream;
    LPBYTE pbDstStream;
112 113
    DWORD cbSrcStream;
    LPBYTE pbSrcStream;
114
    LONGLONG tStart, tStop;
115
    DWORD flags = 0;
116

117
    EnterCriticalSection(&This->tf.filter.csFilter);
118 119 120 121
    hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
    if (FAILED(hr))
    {
        ERR("Cannot get pointer to sample data (%x)\n", hr);
122
        goto error;
123 124 125 126
    }

    cbSrcStream = IMediaSample_GetActualDataLength(pSample);

127
    TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
128

129
    hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
130
    if (FAILED(hr)) {
131 132
        ERR("Unable to retrieve media type\n");
        goto error;
133 134
    }

Christian Costa's avatar
Christian Costa committed
135
    /* Update input size to match sample size */
136
    This->pBihIn->biSizeImage = cbSrcStream;
137

138
    hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
139
    if (FAILED(hr)) {
140 141
        ERR("Unable to get delivery buffer (%x)\n", hr);
        goto error;
142
    }
143

144
    hr = IMediaSample_SetActualDataLength(pOutSample, 0);
145 146
    assert(hr == S_OK);

147
    hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
148
    if (FAILED(hr)) {
149
	ERR("Unable to get pointer to buffer (%x)\n", hr);
150 151
	goto error;
    }
152
    cbDstStream = IMediaSample_GetSize(pOutSample);
Christian Costa's avatar
Christian Costa committed
153
    if (cbDstStream < This->pBihOut->biSizeImage) {
154
        ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
155 156
        hr = E_FAIL;
        goto error;
157 158
    }

159 160 161 162 163 164 165 166 167
    if (IMediaSample_IsPreroll(pSample) == S_OK)
        flags |= ICDECOMPRESS_PREROLL;
    if (IMediaSample_IsSyncPoint(pSample) != S_OK)
        flags |= ICDECOMPRESS_NOTKEYFRAME;
    if (IMediaSample_GetTime(pSample, &tStart, NULL) == S_OK &&
        AVIDec_DropSample(This, tStart))
        flags |= ICDECOMPRESS_HURRYUP;

    res = ICDecompress(This->hvid, flags, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
168
    if (res != ICERR_OK)
169
        ERR("Error occurred during the decompression (%x)\n", res);
Christian Costa's avatar
Christian Costa committed
170

171 172 173 174
    /* Drop sample if its intended to be dropped */
    if (flags & ICDECOMPRESS_HURRYUP)
        goto error;

175
    IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
176 177 178 179 180 181 182 183 184 185

    IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
    IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
    IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));

    if (IMediaSample_GetTime(pSample, &tStart, &tStop) == S_OK)
        IMediaSample_SetTime(pOutSample, &tStart, &tStop);
    else
        IMediaSample_SetTime(pOutSample, NULL, NULL);

186 187 188 189 190
    if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
        IMediaSample_SetMediaTime(pOutSample, &tStart, &tStop);
    else
        IMediaSample_SetMediaTime(pOutSample, NULL, NULL);

191
    LeaveCriticalSection(&This->tf.filter.csFilter);
192
    hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
193
    if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
194
        ERR("Error sending sample (%x)\n", hr);
195 196
    IMediaSample_Release(pOutSample);
    return hr;
197 198

error:
199 200
    if (pOutSample)
        IMediaSample_Release(pOutSample);
201

202
    LeaveCriticalSection(&This->tf.filter.csFilter);
203 204 205
    return hr;
}

206
static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
Christian Costa's avatar
Christian Costa committed
207 208 209 210 211 212
{
    AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
    DWORD result;

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

213 214 215
    if (!This->hvid)
        return S_OK;

Christian Costa's avatar
Christian Costa committed
216 217 218
    result = ICDecompressEnd(This->hvid);
    if (result != ICERR_OK)
    {
219
        ERR("Cannot stop processing (%d)\n", result);
220
        return E_FAIL;
Christian Costa's avatar
Christian Costa committed
221 222 223 224
    }
    return S_OK;
}

225
static HRESULT WINAPI AVIDec_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
226
{
227
    AVIDecImpl* This = (AVIDecImpl*)tf;
228
    HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
229 230

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

232 233 234
    if (dir != PINDIR_INPUT)
        return S_OK;

235
    /* Check root (GUID w/o FOURCC) */
236
    if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
237
        (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
238
    {
239 240 241 242 243 244 245 246 247 248
        VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
        VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
        BITMAPINFOHEADER *bmi;

        if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
            bmi = &format1->bmiHeader;
        else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
            bmi = &format2->bmiHeader;
        else
            goto failed;
249
        TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
250 251

        This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
Christian Costa's avatar
Christian Costa committed
252
        if (This->hvid)
Christian Costa's avatar
Christian Costa committed
253
        {
254
            AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
Christian Costa's avatar
Christian Costa committed
255 256
            const CLSID* outsubtype;
            DWORD bih_size;
257
            DWORD output_depth = bmi->biBitCount;
Christian Costa's avatar
Christian Costa committed
258
            DWORD result;
259
            FreeMediaType(outpmt);
Christian Costa's avatar
Christian Costa committed
260

261
            switch(bmi->biBitCount)
262
            {
Christian Costa's avatar
Christian Costa committed
263
                case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
264 265 266 267
                case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
                case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
                case 8:  outsubtype = &MEDIASUBTYPE_RGB8; break;
                default:
268
                    WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
Christian Costa's avatar
Christian Costa committed
269 270 271
                    outsubtype = &MEDIASUBTYPE_RGB32;
                    output_depth = 32;
                    break;
272
            }
Christian Costa's avatar
Christian Costa committed
273 274

            /* Copy bitmap header from media type to 1 for input and 1 for output */
275
            bih_size = bmi->biSize + bmi->biClrUsed * 4;
276
            This->pBihIn = CoTaskMemAlloc(bih_size);
277
            if (!This->pBihIn)
Christian Costa's avatar
Christian Costa committed
278
            {
Christian Costa's avatar
Christian Costa committed
279 280
                hr = E_OUTOFMEMORY;
                goto failed;
Christian Costa's avatar
Christian Costa committed
281
            }
282
            This->pBihOut = CoTaskMemAlloc(bih_size);
283
            if (!This->pBihOut)
Christian Costa's avatar
Christian Costa committed
284
            {
Christian Costa's avatar
Christian Costa committed
285 286
                hr = E_OUTOFMEMORY;
                goto failed;
Christian Costa's avatar
Christian Costa committed
287
            }
288 289
            memcpy(This->pBihIn, bmi, bih_size);
            memcpy(This->pBihOut, bmi, bih_size);
Christian Costa's avatar
Christian Costa committed
290 291

            /* Update output format as non compressed bitmap */
292
            This->pBihOut->biCompression = 0;
Christian Costa's avatar
Christian Costa committed
293
            This->pBihOut->biBitCount = output_depth;
294
            This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
295
            TRACE("Size: %u\n", This->pBihIn->biSize);
Christian Costa's avatar
Christian Costa committed
296 297 298
            result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
            if (result != ICERR_OK)
            {
299
                ERR("Unable to found a suitable output format (%d)\n", result);
Christian Costa's avatar
Christian Costa committed
300 301 302 303 304 305
                goto failed;
            }

            /* Update output media type */
            CopyMediaType(outpmt, pmt);
            outpmt->subtype = *outsubtype;
306 307 308 309 310 311 312

            if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
                memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
            else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
                memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
            else
                assert(0);
Christian Costa's avatar
Christian Costa committed
313

Christian Costa's avatar
Christian Costa committed
314
            TRACE("Connection accepted\n");
315
            return S_OK;
Christian Costa's avatar
Christian Costa committed
316
        }
317 318 319
        TRACE("Unable to find a suitable VFW decompressor\n");
    }

Christian Costa's avatar
Christian Costa committed
320
failed:
321

322
    TRACE("Connection refused\n");
Christian Costa's avatar
Christian Costa committed
323
    return hr;
324 325
}

326
static HRESULT WINAPI AVIDec_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
327
{
328 329 330 331 332 333 334 335 336 337
    AVIDecImpl* This = (AVIDecImpl*)tf;

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

    return S_OK;
}

static HRESULT WINAPI AVIDec_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
{
    AVIDecImpl *This = (AVIDecImpl *)tf;
338 339

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

341 342 343 344 345 346 347 348 349 350 351 352 353
    if (dir == PINDIR_INPUT)
    {
        if (This->hvid)
            ICClose(This->hvid);
        if (This->pBihIn)
            CoTaskMemFree(This->pBihIn);
        if (This->pBihOut)
            CoTaskMemFree(This->pBihOut);

        This->hvid = NULL;
        This->pBihIn = NULL;
        This->pBihOut = NULL;
    }
354

355
    return S_OK;
356 357
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
static HRESULT WINAPI AVIDec_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
{
    AVIDecImpl *pAVI = (AVIDecImpl*)tf;
    ALLOCATOR_PROPERTIES actual;

    if (!ppropInputRequest->cbAlign)
        ppropInputRequest->cbAlign = 1;

    if (ppropInputRequest->cbBuffer < pAVI->pBihOut->biSizeImage)
            ppropInputRequest->cbBuffer = pAVI->pBihOut->biSizeImage;

    if (!ppropInputRequest->cBuffers)
        ppropInputRequest->cBuffers = 1;

    return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
}

375
static const TransformFilterFuncTable AVIDec_FuncsTable = {
376
    AVIDec_DecideBufferSize,
377 378 379 380 381 382 383
    AVIDec_StartStreaming,
    AVIDec_Receive,
    AVIDec_StopStreaming,
    NULL,
    AVIDec_SetMediaType,
    AVIDec_CompleteConnect,
    AVIDec_BreakConnect,
384
    NULL,
385
    NULL,
386
    AVIDec_EndFlush,
387
    NULL,
388
    AVIDec_NotifyDrop
Christian Costa's avatar
Christian Costa committed
389 390
};

391 392 393
HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
{
    HRESULT hr;
394
    AVIDecImpl * This;
395 396 397 398 399 400 401

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

    *ppv = NULL;

    if (pUnkOuter)
        return CLASS_E_NOAGGREGATION;
402

403 404 405 406 407 408 409 410 411 412 413 414
    hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);

    if (FAILED(hr))
        return hr;
    else
    {
        ISeekingPassThru *passthru;
        hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
        IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
        ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
        ISeekingPassThru_Release(passthru);
    }
415

416 417
    This->hvid = NULL;
    This->pBihIn = NULL;
Christian Costa's avatar
Christian Costa committed
418
    This->pBihOut = NULL;
419

420 421 422 423
    *ppv = This;

    return hr;
}
424

425 426 427 428 429
HRESULT WINAPI AVIDec_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
{
    HRESULT hr;
    AVIDecImpl *This = (AVIDecImpl *)iface;
    TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
430

431 432 433 434
    if (IsEqualIID(riid, &IID_IMediaSeeking))
        return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);

    hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
435 436 437

    return hr;
}
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

static const IBaseFilterVtbl AVIDec_Vtbl =
{
    AVIDec_QueryInterface,
    BaseFilterImpl_AddRef,
    TransformFilterImpl_Release,
    BaseFilterImpl_GetClassID,
    TransformFilterImpl_Stop,
    TransformFilterImpl_Pause,
    TransformFilterImpl_Run,
    BaseFilterImpl_GetState,
    BaseFilterImpl_SetSyncSource,
    BaseFilterImpl_GetSyncSource,
    BaseFilterImpl_EnumPins,
    TransformFilterImpl_FindPin,
    BaseFilterImpl_QueryFilterInfo,
    BaseFilterImpl_JoinFilterGraph,
    BaseFilterImpl_QueryVendorInfo
};