avidec.c 13.2 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
    TransformFilter tf;

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

53 54
static const IBaseFilterVtbl AVIDec_Vtbl;

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

static inline AVIDecImpl *impl_from_TransformFilter( TransformFilter *iface )
{
    return CONTAINING_RECORD(iface, AVIDecImpl, tf.filter);
}

65
static HRESULT WINAPI AVIDec_StartStreaming(TransformFilter* pTransformFilter)
Christian Costa's avatar
Christian Costa committed
66
{
67
    AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
Christian Costa's avatar
Christian Costa committed
68 69 70
    DWORD result;

    TRACE("(%p)->()\n", This);
71
    This->late = -1;
Christian Costa's avatar
Christian Costa committed
72 73 74 75

    result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
    if (result != ICERR_OK)
    {
76
        ERR("Cannot start processing (%d)\n", result);
Christian Costa's avatar
Christian Costa committed
77 78 79 80 81
	return E_FAIL;
    }
    return S_OK;
}

82
static HRESULT WINAPI AVIDec_EndFlush(TransformFilter *pTransformFilter) {
83
    AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
84 85 86 87 88
    This->late = -1;
    return S_OK;
}

static HRESULT WINAPI AVIDec_NotifyDrop(TransformFilter *pTransformFilter, IBaseFilter *sender, Quality qm) {
89
    AVIDecImpl *This = impl_from_TransformFilter(pTransformFilter);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

    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;
}

112
static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
113
{
114
    AVIDecImpl* This = impl_from_TransformFilter(tf);
115 116 117
    AM_MEDIA_TYPE amt;
    HRESULT hr;
    DWORD res;
118
    IMediaSample* pOutSample = NULL;
119 120
    DWORD cbDstStream;
    LPBYTE pbDstStream;
121 122
    DWORD cbSrcStream;
    LPBYTE pbSrcStream;
123
    LONGLONG tStart, tStop;
124
    DWORD flags = 0;
125

126
    EnterCriticalSection(&This->tf.csReceive);
127 128 129 130
    hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
    if (FAILED(hr))
    {
        ERR("Cannot get pointer to sample data (%x)\n", hr);
131
        goto error;
132 133 134 135
    }

    cbSrcStream = IMediaSample_GetActualDataLength(pSample);

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

138
    hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
139
    if (FAILED(hr)) {
140 141
        ERR("Unable to retrieve media type\n");
        goto error;
142 143
    }

Christian Costa's avatar
Christian Costa committed
144
    /* Update input size to match sample size */
145
    This->pBihIn->biSizeImage = cbSrcStream;
146

147
    hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
148
    if (FAILED(hr)) {
149 150
        ERR("Unable to get delivery buffer (%x)\n", hr);
        goto error;
151
    }
152

153
    hr = IMediaSample_SetActualDataLength(pOutSample, 0);
154 155
    assert(hr == S_OK);

156
    hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
157
    if (FAILED(hr)) {
158
	ERR("Unable to get pointer to buffer (%x)\n", hr);
159 160
	goto error;
    }
161
    cbDstStream = IMediaSample_GetSize(pOutSample);
Christian Costa's avatar
Christian Costa committed
162
    if (cbDstStream < This->pBihOut->biSizeImage) {
163
        ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
164 165
        hr = E_FAIL;
        goto error;
166 167
    }

168 169 170 171
    if (IMediaSample_IsPreroll(pSample) == S_OK)
        flags |= ICDECOMPRESS_PREROLL;
    if (IMediaSample_IsSyncPoint(pSample) != S_OK)
        flags |= ICDECOMPRESS_NOTKEYFRAME;
172 173
    hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
    if (hr == S_OK && AVIDec_DropSample(This, tStart))
174 175 176
        flags |= ICDECOMPRESS_HURRYUP;

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

180
    /* Drop sample if its intended to be dropped */
181 182
    if (flags & ICDECOMPRESS_HURRYUP) {
        hr = S_OK;
183
        goto error;
184
    }
185

186
    IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
187 188 189 190 191

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

192
    if (hr == S_OK)
193
        IMediaSample_SetTime(pOutSample, &tStart, &tStop);
194 195
    else if (hr == VFW_S_NO_STOP_TIME)
        IMediaSample_SetTime(pOutSample, &tStart, NULL);
196 197 198
    else
        IMediaSample_SetTime(pOutSample, NULL, NULL);

199 200 201 202 203
    if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
        IMediaSample_SetMediaTime(pOutSample, &tStart, &tStop);
    else
        IMediaSample_SetMediaTime(pOutSample, NULL, NULL);

204
    LeaveCriticalSection(&This->tf.csReceive);
205
    hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
206
    EnterCriticalSection(&This->tf.csReceive);
207
    if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
208
        ERR("Error sending sample (%x)\n", hr);
209 210

error:
211 212
    if (pOutSample)
        IMediaSample_Release(pOutSample);
213

214
    LeaveCriticalSection(&This->tf.csReceive);
215 216 217
    return hr;
}

218
static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
Christian Costa's avatar
Christian Costa committed
219
{
220
    AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
Christian Costa's avatar
Christian Costa committed
221 222 223 224
    DWORD result;

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

225 226 227
    if (!This->hvid)
        return S_OK;

Christian Costa's avatar
Christian Costa committed
228 229 230
    result = ICDecompressEnd(This->hvid);
    if (result != ICERR_OK)
    {
231
        ERR("Cannot stop processing (%d)\n", result);
232
        return E_FAIL;
Christian Costa's avatar
Christian Costa committed
233 234 235 236
    }
    return S_OK;
}

237
static HRESULT WINAPI AVIDec_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
238
{
239
    AVIDecImpl* This = impl_from_TransformFilter(tf);
240
    HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
241 242

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

244 245 246
    if (dir != PINDIR_INPUT)
        return S_OK;

247
    /* Check root (GUID w/o FOURCC) */
248
    if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
249
        (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
250
    {
251 252 253 254 255 256 257 258 259 260
        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;
261
        TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
262 263

        This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
Christian Costa's avatar
Christian Costa committed
264
        if (This->hvid)
Christian Costa's avatar
Christian Costa committed
265
        {
266
            AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
Christian Costa's avatar
Christian Costa committed
267 268
            const CLSID* outsubtype;
            DWORD bih_size;
269
            DWORD output_depth = bmi->biBitCount;
Christian Costa's avatar
Christian Costa committed
270
            DWORD result;
271
            FreeMediaType(outpmt);
Christian Costa's avatar
Christian Costa committed
272

273
            switch(bmi->biBitCount)
274
            {
Christian Costa's avatar
Christian Costa committed
275
                case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
276 277 278 279
                case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
                case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
                case 8:  outsubtype = &MEDIASUBTYPE_RGB8; break;
                default:
280
                    WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
Christian Costa's avatar
Christian Costa committed
281 282 283
                    outsubtype = &MEDIASUBTYPE_RGB32;
                    output_depth = 32;
                    break;
284
            }
Christian Costa's avatar
Christian Costa committed
285 286

            /* Copy bitmap header from media type to 1 for input and 1 for output */
287
            bih_size = bmi->biSize + bmi->biClrUsed * 4;
288
            This->pBihIn = CoTaskMemAlloc(bih_size);
289
            if (!This->pBihIn)
Christian Costa's avatar
Christian Costa committed
290
            {
Christian Costa's avatar
Christian Costa committed
291 292
                hr = E_OUTOFMEMORY;
                goto failed;
Christian Costa's avatar
Christian Costa committed
293
            }
294
            This->pBihOut = CoTaskMemAlloc(bih_size);
295
            if (!This->pBihOut)
Christian Costa's avatar
Christian Costa committed
296
            {
Christian Costa's avatar
Christian Costa committed
297 298
                hr = E_OUTOFMEMORY;
                goto failed;
Christian Costa's avatar
Christian Costa committed
299
            }
300 301
            memcpy(This->pBihIn, bmi, bih_size);
            memcpy(This->pBihOut, bmi, bih_size);
Christian Costa's avatar
Christian Costa committed
302 303

            /* Update output format as non compressed bitmap */
304
            This->pBihOut->biCompression = 0;
Christian Costa's avatar
Christian Costa committed
305
            This->pBihOut->biBitCount = output_depth;
306
            This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
307
            TRACE("Size: %u\n", This->pBihIn->biSize);
Christian Costa's avatar
Christian Costa committed
308 309 310
            result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
            if (result != ICERR_OK)
            {
311
                ERR("Unable to found a suitable output format (%d)\n", result);
Christian Costa's avatar
Christian Costa committed
312 313 314 315 316 317
                goto failed;
            }

            /* Update output media type */
            CopyMediaType(outpmt, pmt);
            outpmt->subtype = *outsubtype;
318 319 320 321 322 323 324

            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
325

Christian Costa's avatar
Christian Costa committed
326
            TRACE("Connection accepted\n");
327
            return S_OK;
Christian Costa's avatar
Christian Costa committed
328
        }
329 330 331
        TRACE("Unable to find a suitable VFW decompressor\n");
    }

Christian Costa's avatar
Christian Costa committed
332
failed:
333

334
    TRACE("Connection refused\n");
Christian Costa's avatar
Christian Costa committed
335
    return hr;
336 337
}

338
static HRESULT WINAPI AVIDec_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
339
{
340
    AVIDecImpl* This = impl_from_TransformFilter(tf);
341 342 343 344 345 346 347 348

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

    return S_OK;
}

static HRESULT WINAPI AVIDec_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
{
349
    AVIDecImpl *This = impl_from_TransformFilter(tf);
350 351

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

353 354 355 356 357 358 359 360 361 362 363 364 365
    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;
    }
366

367
    return S_OK;
368 369
}

370 371
static HRESULT WINAPI AVIDec_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
{
372
    AVIDecImpl *pAVI = impl_from_TransformFilter(tf);
373 374 375 376 377 378 379 380 381 382 383 384 385 386
    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);
}

387
static const TransformFilterFuncTable AVIDec_FuncsTable = {
388
    AVIDec_DecideBufferSize,
389 390 391 392 393 394 395
    AVIDec_StartStreaming,
    AVIDec_Receive,
    AVIDec_StopStreaming,
    NULL,
    AVIDec_SetMediaType,
    AVIDec_CompleteConnect,
    AVIDec_BreakConnect,
396
    NULL,
397
    NULL,
398
    AVIDec_EndFlush,
399
    NULL,
400
    AVIDec_NotifyDrop
Christian Costa's avatar
Christian Costa committed
401 402
};

403 404 405
HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
{
    HRESULT hr;
406
    AVIDecImpl * This;
407 408 409 410 411 412 413

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

    *ppv = NULL;

    if (pUnkOuter)
        return CLASS_E_NOAGGREGATION;
414

415 416 417 418
    hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);

    if (FAILED(hr))
        return hr;
419

420 421
    This->hvid = NULL;
    This->pBihIn = NULL;
Christian Costa's avatar
Christian Costa committed
422
    This->pBihOut = NULL;
423

424 425 426 427
    *ppv = This;

    return hr;
}
428

429 430
static const IBaseFilterVtbl AVIDec_Vtbl =
{
431
    TransformFilterImpl_QueryInterface,
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    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
};