sample.c 50.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2020 Nikolay Sivov
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS

#include "evr.h"
22 23
#include "mfapi.h"
#include "mferror.h"
24 25
#include "d3d9.h"
#include "dxva2api.h"
26 27

#include "wine/debug.h"
28
#include "wine/list.h"
29 30 31

WINE_DEFAULT_DEBUG_CHANNEL(evr);

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static const char *debugstr_time(LONGLONG time)
{
    ULONGLONG abstime = time >= 0 ? time : -time;
    unsigned int i = 0, j = 0;
    char buffer[23], rev[23];

    while (abstime || i <= 8)
    {
        buffer[i++] = '0' + (abstime % 10);
        abstime /= 10;
        if (i == 7) buffer[i++] = '.';
    }
    if (time < 0) buffer[i++] = '-';

    while (i--) rev[j++] = buffer[i];
    while (rev[j-1] == '0' && rev[j-2] != '.') --j;
    rev[j] = 0;

    return wine_dbg_sprintf("%s", rev);
}

53 54 55 56 57 58 59 60 61 62
struct surface_buffer
{
    IMFMediaBuffer IMFMediaBuffer_iface;
    IMFGetService IMFGetService_iface;
    LONG refcount;

    IUnknown *surface;
    ULONG length;
};

63 64 65 66 67 68 69
enum sample_prop_flags
{
    SAMPLE_PROP_HAS_DURATION      = 1 << 0,
    SAMPLE_PROP_HAS_TIMESTAMP     = 1 << 1,
    SAMPLE_PROP_HAS_DESIRED_PROPS = 1 << 2,
};

70 71 72 73 74 75 76 77 78 79 80
struct video_sample
{
    IMFSample IMFSample_iface;
    IMFTrackedSample IMFTrackedSample_iface;
    IMFDesiredSample IMFDesiredSample_iface;
    LONG refcount;

    IMFSample *sample;

    IMFAsyncResult *tracked_result;
    LONG tracked_refcount;
81

82 83 84
    LONGLONG timestamp;
    LONGLONG duration;
    LONGLONG desired_timestamp;
85
    LONGLONG desired_duration;
86 87
    unsigned int flags;
    CRITICAL_SECTION cs;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
};

static struct video_sample *impl_from_IMFSample(IMFSample *iface)
{
    return CONTAINING_RECORD(iface, struct video_sample, IMFSample_iface);
}

static struct video_sample *impl_from_IMFTrackedSample(IMFTrackedSample *iface)
{
    return CONTAINING_RECORD(iface, struct video_sample, IMFTrackedSample_iface);
}

static struct video_sample *impl_from_IMFDesiredSample(IMFDesiredSample *iface)
{
    return CONTAINING_RECORD(iface, struct video_sample, IMFDesiredSample_iface);
}

105 106 107 108 109 110 111 112 113 114
static struct surface_buffer *impl_from_IMFMediaBuffer(IMFMediaBuffer *iface)
{
    return CONTAINING_RECORD(iface, struct surface_buffer, IMFMediaBuffer_iface);
}

static struct surface_buffer *impl_from_IMFGetService(IMFGetService *iface)
{
    return CONTAINING_RECORD(iface, struct surface_buffer, IMFGetService_iface);
}

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
struct tracked_async_result
{
    MFASYNCRESULT result;
    LONG refcount;
    IUnknown *object;
    IUnknown *state;
};

static struct tracked_async_result *impl_from_IMFAsyncResult(IMFAsyncResult *iface)
{
    return CONTAINING_RECORD(iface, struct tracked_async_result, result.AsyncResult);
}

static HRESULT WINAPI tracked_async_result_QueryInterface(IMFAsyncResult *iface, REFIID riid, void **obj)
{
    TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);

    if (IsEqualIID(riid, &IID_IMFAsyncResult) ||
            IsEqualIID(riid, &IID_IUnknown))
    {
        *obj = iface;
        IMFAsyncResult_AddRef(iface);
        return S_OK;
    }

    *obj = NULL;
    WARN("Unsupported interface %s.\n", debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI tracked_async_result_AddRef(IMFAsyncResult *iface)
{
    struct tracked_async_result *result = impl_from_IMFAsyncResult(iface);
    ULONG refcount = InterlockedIncrement(&result->refcount);

150
    TRACE("%p, refcount %lu.\n", iface, refcount);
151 152 153 154 155 156 157 158 159

    return refcount;
}

static ULONG WINAPI tracked_async_result_Release(IMFAsyncResult *iface)
{
    struct tracked_async_result *result = impl_from_IMFAsyncResult(iface);
    ULONG refcount = InterlockedDecrement(&result->refcount);

160
    TRACE("%p, refcount %lu.\n", iface, refcount);
161 162 163 164 165 166 167 168 169

    if (!refcount)
    {
        if (result->result.pCallback)
            IMFAsyncCallback_Release(result->result.pCallback);
        if (result->object)
            IUnknown_Release(result->object);
        if (result->state)
            IUnknown_Release(result->state);
170
        free(result);
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
    }

    return refcount;
}

static HRESULT WINAPI tracked_async_result_GetState(IMFAsyncResult *iface, IUnknown **state)
{
    struct tracked_async_result *result = impl_from_IMFAsyncResult(iface);

    TRACE("%p, %p.\n", iface, state);

    if (!result->state)
        return E_POINTER;

    *state = result->state;
    IUnknown_AddRef(*state);

    return S_OK;
}

static HRESULT WINAPI tracked_async_result_GetStatus(IMFAsyncResult *iface)
{
    struct tracked_async_result *result = impl_from_IMFAsyncResult(iface);

    TRACE("%p.\n", iface);

    return result->result.hrStatusResult;
}

static HRESULT WINAPI tracked_async_result_SetStatus(IMFAsyncResult *iface, HRESULT status)
{
    struct tracked_async_result *result = impl_from_IMFAsyncResult(iface);

204
    TRACE("%p, %#lx.\n", iface, status);
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

    result->result.hrStatusResult = status;

    return S_OK;
}

static HRESULT WINAPI tracked_async_result_GetObject(IMFAsyncResult *iface, IUnknown **object)
{
    struct tracked_async_result *result = impl_from_IMFAsyncResult(iface);

    TRACE("%p, %p.\n", iface, object);

    if (!result->object)
        return E_POINTER;

    *object = result->object;
    IUnknown_AddRef(*object);

    return S_OK;
}

static IUnknown * WINAPI tracked_async_result_GetStateNoAddRef(IMFAsyncResult *iface)
{
    struct tracked_async_result *result = impl_from_IMFAsyncResult(iface);

    TRACE("%p.\n", iface);

    return result->state;
}

static const IMFAsyncResultVtbl tracked_async_result_vtbl =
{
    tracked_async_result_QueryInterface,
    tracked_async_result_AddRef,
    tracked_async_result_Release,
    tracked_async_result_GetState,
    tracked_async_result_GetStatus,
    tracked_async_result_SetStatus,
    tracked_async_result_GetObject,
    tracked_async_result_GetStateNoAddRef,
};

static HRESULT create_async_result(IUnknown *object, IMFAsyncCallback *callback,
        IUnknown *state, IMFAsyncResult **out)
{
    struct tracked_async_result *result;

252
    result = calloc(1, sizeof(*result));
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
    if (!result)
        return E_OUTOFMEMORY;

    result->result.AsyncResult.lpVtbl = &tracked_async_result_vtbl;
    result->refcount = 1;
    result->object = object;
    if (result->object)
        IUnknown_AddRef(result->object);
    result->result.pCallback = callback;
    if (result->result.pCallback)
        IMFAsyncCallback_AddRef(result->result.pCallback);
    result->state = state;
    if (result->state)
        IUnknown_AddRef(result->state);

    *out = &result->result.AsyncResult;

    return S_OK;
}

struct tracking_thread
{
    HANDLE hthread;
    DWORD tid;
    LONG refcount;
};
static struct tracking_thread tracking_thread;

static CRITICAL_SECTION tracking_thread_cs = { NULL, -1, 0, 0, 0, 0 };

enum tracking_thread_message
{
    TRACKM_STOP = WM_USER,
    TRACKM_INVOKE = WM_USER + 1,
};

static DWORD CALLBACK tracking_thread_proc(void *arg)
{
    HANDLE ready_event = arg;
    BOOL stop_thread = FALSE;
    IMFAsyncResult *result;
    MFASYNCRESULT *data;
    MSG msg;

    PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);

    SetEvent(ready_event);

    while (!stop_thread)
    {
        MsgWaitForMultipleObjects(0, NULL, FALSE, INFINITE, QS_POSTMESSAGE);

        while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
        {
            switch (msg.message)
            {
                case TRACKM_INVOKE:
                    result = (IMFAsyncResult *)msg.lParam;
                    data = (MFASYNCRESULT *)result;
                    if (data->pCallback)
                        IMFAsyncCallback_Invoke(data->pCallback, result);
                    IMFAsyncResult_Release(result);
                    break;

                case TRACKM_STOP:
                    stop_thread = TRUE;
                    break;

                default:
                    ;
            }
        }
    }

    TRACE("Tracking thread exiting.\n");

    return 0;
}

static void video_sample_create_tracking_thread(void)
{
    EnterCriticalSection(&tracking_thread_cs);

    if (++tracking_thread.refcount == 1)
    {
        HANDLE ready_event;

        ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);

        if (!(tracking_thread.hthread = CreateThread(NULL, 0, tracking_thread_proc,
                ready_event, 0, &tracking_thread.tid)))
        {
            WARN("Failed to create sample tracking thread.\n");
            CloseHandle(ready_event);
347
            LeaveCriticalSection(&tracking_thread_cs);
348 349 350 351 352 353
            return;
        }

        WaitForSingleObject(ready_event, INFINITE);
        CloseHandle(ready_event);

354
        TRACE("Create tracking thread %#lx.\n", tracking_thread.tid);
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    }

    LeaveCriticalSection(&tracking_thread_cs);
}

static void video_sample_stop_tracking_thread(void)
{
    EnterCriticalSection(&tracking_thread_cs);

    if (!--tracking_thread.refcount)
    {
        PostThreadMessageW(tracking_thread.tid, TRACKM_STOP, 0, 0);
        CloseHandle(tracking_thread.hthread);
        memset(&tracking_thread, 0, sizeof(tracking_thread));
    }

    LeaveCriticalSection(&tracking_thread_cs);
}

static void video_sample_tracking_thread_invoke(IMFAsyncResult *result)
{
    if (!tracking_thread.tid)
    {
        WARN("Sample tracking thread is not initialized.\n");
        return;
    }

    IMFAsyncResult_AddRef(result);
    PostThreadMessageW(tracking_thread.tid, TRACKM_INVOKE, 0, (LPARAM)result);
}

386 387 388 389
struct sample_allocator
{
    IMFVideoSampleAllocator IMFVideoSampleAllocator_iface;
    IMFVideoSampleAllocatorCallback IMFVideoSampleAllocatorCallback_iface;
390
    IMFAsyncCallback tracking_callback;
391
    LONG refcount;
392 393

    IMFVideoSampleAllocatorNotify *callback;
394
    IDirect3DDeviceManager9 *device_manager;
395 396 397
    unsigned int free_sample_count;
    struct list free_samples;
    struct list used_samples;
398
    CRITICAL_SECTION cs;
399 400
};

401 402 403 404 405 406
struct queued_sample
{
    struct list entry;
    IMFSample *sample;
};

407 408 409 410 411 412 413 414 415 416
static struct sample_allocator *impl_from_IMFVideoSampleAllocator(IMFVideoSampleAllocator *iface)
{
    return CONTAINING_RECORD(iface, struct sample_allocator, IMFVideoSampleAllocator_iface);
}

static struct sample_allocator *impl_from_IMFVideoSampleAllocatorCallback(IMFVideoSampleAllocatorCallback *iface)
{
    return CONTAINING_RECORD(iface, struct sample_allocator, IMFVideoSampleAllocatorCallback_iface);
}

417 418 419 420 421
static struct sample_allocator *impl_from_IMFAsyncCallback(IMFAsyncCallback *iface)
{
    return CONTAINING_RECORD(iface, struct sample_allocator, tracking_callback);
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
static HRESULT WINAPI sample_allocator_QueryInterface(IMFVideoSampleAllocator *iface, REFIID riid, void **obj)
{
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocator(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);

    if (IsEqualIID(riid, &IID_IMFVideoSampleAllocator) ||
            IsEqualIID(riid, &IID_IUnknown))
    {
        *obj = &allocator->IMFVideoSampleAllocator_iface;
    }
    else if (IsEqualIID(riid, &IID_IMFVideoSampleAllocatorCallback))
    {
        *obj = &allocator->IMFVideoSampleAllocatorCallback_iface;
    }
    else
    {
        WARN("Unsupported interface %s.\n", debugstr_guid(riid));
        *obj = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown *)*obj);
    return S_OK;
}

static ULONG WINAPI sample_allocator_AddRef(IMFVideoSampleAllocator *iface)
{
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocator(iface);
    ULONG refcount = InterlockedIncrement(&allocator->refcount);

453
    TRACE("%p, refcount %lu.\n", iface, refcount);
454 455 456 457

    return refcount;
}

458 459 460 461 462 463 464 465
static void sample_allocator_release_samples(struct sample_allocator *allocator)
{
    struct queued_sample *iter, *iter2;

    LIST_FOR_EACH_ENTRY_SAFE(iter, iter2, &allocator->free_samples, struct queued_sample, entry)
    {
        list_remove(&iter->entry);
        IMFSample_Release(iter->sample);
466
        free(iter);
467 468 469 470 471
    }

    LIST_FOR_EACH_ENTRY_SAFE(iter, iter2, &allocator->used_samples, struct queued_sample, entry)
    {
        list_remove(&iter->entry);
472
        free(iter);
473 474 475
    }
}

476 477 478 479 480
static ULONG WINAPI sample_allocator_Release(IMFVideoSampleAllocator *iface)
{
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocator(iface);
    ULONG refcount = InterlockedDecrement(&allocator->refcount);

481
    TRACE("%p, refcount %lu.\n", iface, refcount);
482 483 484

    if (!refcount)
    {
485 486
        if (allocator->callback)
            IMFVideoSampleAllocatorNotify_Release(allocator->callback);
487 488
        if (allocator->device_manager)
            IDirect3DDeviceManager9_Release(allocator->device_manager);
489
        sample_allocator_release_samples(allocator);
490
        DeleteCriticalSection(&allocator->cs);
491
        free(allocator);
492 493 494 495 496 497 498 499
    }

    return refcount;
}

static HRESULT WINAPI sample_allocator_SetDirectXManager(IMFVideoSampleAllocator *iface,
        IUnknown *manager)
{
500 501 502
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocator(iface);
    IDirect3DDeviceManager9 *device_manager = NULL;
    HRESULT hr;
503

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    TRACE("%p, %p.\n", iface, manager);

    if (manager && FAILED(hr = IUnknown_QueryInterface(manager, &IID_IDirect3DDeviceManager9,
            (void **)&device_manager)))
    {
        return hr;
    }

    EnterCriticalSection(&allocator->cs);

    if (allocator->device_manager)
        IDirect3DDeviceManager9_Release(allocator->device_manager);
    allocator->device_manager = device_manager;

    LeaveCriticalSection(&allocator->cs);

    return S_OK;
521 522 523 524
}

static HRESULT WINAPI sample_allocator_UninitializeSampleAllocator(IMFVideoSampleAllocator *iface)
{
525
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocator(iface);
526

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    TRACE("%p.\n", iface);

    EnterCriticalSection(&allocator->cs);

    sample_allocator_release_samples(allocator);
    allocator->free_sample_count = 0;

    LeaveCriticalSection(&allocator->cs);

    return S_OK;
}

static HRESULT sample_allocator_create_samples(struct sample_allocator *allocator, unsigned int sample_count,
        IMFMediaType *media_type)
{
    IDirectXVideoProcessorService *service = NULL;
    unsigned int i, width, height;
    IDirect3DSurface9 *surface;
    HANDLE hdevice = NULL;
    GUID major, subtype;
    UINT64 frame_size;
    IMFSample *sample;
    D3DFORMAT format;
    HRESULT hr;

    if (FAILED(IMFMediaType_GetMajorType(media_type, &major)))
        return MF_E_INVALIDMEDIATYPE;

    if (!IsEqualGUID(&major, &MFMediaType_Video))
        return MF_E_INVALIDMEDIATYPE;

    if (FAILED(IMFMediaType_GetUINT64(media_type, &MF_MT_FRAME_SIZE, &frame_size)))
        return MF_E_INVALIDMEDIATYPE;

    if (FAILED(IMFMediaType_GetGUID(media_type, &MF_MT_SUBTYPE, &subtype)))
        return MF_E_INVALIDMEDIATYPE;

    format = subtype.Data1;
    height = frame_size;
    width = frame_size >> 32;

    if (allocator->device_manager)
    {
        if (SUCCEEDED(hr = IDirect3DDeviceManager9_OpenDeviceHandle(allocator->device_manager, &hdevice)))
        {
            hr = IDirect3DDeviceManager9_GetVideoService(allocator->device_manager, hdevice,
                    &IID_IDirectXVideoProcessorService, (void **)&service);
        }

        if (FAILED(hr))
        {
578
            WARN("Failed to get processor service, %#lx.\n", hr);
579 580 581 582 583 584 585 586
            return hr;
        }
    }

    sample_allocator_release_samples(allocator);

    for (i = 0; i < sample_count; ++i)
    {
587
        struct queued_sample *queued_sample;
588
        IMFMediaBuffer *buffer;
589

590
        if (SUCCEEDED(hr = MFCreateVideoSampleFromSurface(NULL, &sample)))
591
        {
592
            if (service)
593
            {
594 595 596 597 598 599 600 601 602 603
                if (SUCCEEDED(hr = IDirectXVideoProcessorService_CreateSurface(service, width, height,
                        0, format, D3DPOOL_DEFAULT, 0, DXVA2_VideoProcessorRenderTarget, &surface, NULL)))
                {
                    hr = MFCreateDXSurfaceBuffer(&IID_IDirect3DSurface9, (IUnknown *)surface, FALSE, &buffer);
                    IDirect3DSurface9_Release(surface);
                }
            }
            else
            {
                hr = MFCreate2DMediaBuffer(width, height, format, FALSE, &buffer);
604 605
            }

606
            if (SUCCEEDED(hr))
607
            {
608 609
                hr = IMFSample_AddBuffer(sample, buffer);
                IMFMediaBuffer_Release(buffer);
610 611 612 613 614 615 616 617 618 619
            }
        }

        if (FAILED(hr))
        {
            WARN("Unable to allocate %u samples.\n", sample_count);
            sample_allocator_release_samples(allocator);
            break;
        }

620
        queued_sample = malloc(sizeof(*queued_sample));
621 622 623 624 625 626 627 628 629 630 631 632
        queued_sample->sample = sample;
        list_add_tail(&allocator->free_samples, &queued_sample->entry);
        allocator->free_sample_count++;
    }

    if (service)
        IDirectXVideoProcessorService_Release(service);

    if (allocator->device_manager)
        IDirect3DDeviceManager9_CloseDeviceHandle(allocator->device_manager, hdevice);

    return hr;
633 634 635 636 637
}

static HRESULT WINAPI sample_allocator_InitializeSampleAllocator(IMFVideoSampleAllocator *iface,
        DWORD sample_count, IMFMediaType *media_type)
{
638 639
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocator(iface);
    HRESULT hr;
640

641
    TRACE("%p, %lu, %p.\n", iface, sample_count, media_type);
642 643 644 645 646 647 648 649 650 651 652

    if (!sample_count)
        sample_count = 1;

    EnterCriticalSection(&allocator->cs);

    hr = sample_allocator_create_samples(allocator, sample_count, media_type);

    LeaveCriticalSection(&allocator->cs);

    return hr;
653 654
}

655
static HRESULT WINAPI sample_allocator_AllocateSample(IMFVideoSampleAllocator *iface, IMFSample **out)
656
{
657 658 659 660
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocator(iface);
    IMFTrackedSample *tracked_sample;
    IMFSample *sample;
    HRESULT hr;
661

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    TRACE("%p, %p.\n", iface, out);

    EnterCriticalSection(&allocator->cs);

    if (list_empty(&allocator->free_samples) && list_empty(&allocator->used_samples))
        hr = MF_E_NOT_INITIALIZED;
    else if (list_empty(&allocator->free_samples))
        hr = MF_E_SAMPLEALLOCATOR_EMPTY;
    else
    {
        struct list *head = list_head(&allocator->free_samples);

        sample = LIST_ENTRY(head, struct queued_sample, entry)->sample;

        if (SUCCEEDED(hr = IMFSample_QueryInterface(sample, &IID_IMFTrackedSample, (void **)&tracked_sample)))
        {
            hr = IMFTrackedSample_SetAllocator(tracked_sample, &allocator->tracking_callback, NULL);
            IMFTrackedSample_Release(tracked_sample);
        }

        if (SUCCEEDED(hr))
        {
            list_remove(head);
            list_add_tail(&allocator->used_samples, head);
            allocator->free_sample_count--;

688 689 690
            /* Reference counter is not increased when sample is returned, so next release could trigger
               tracking condition. This is balanced by incremented reference counter when sample is returned
               back to the free list. */
691 692 693 694 695 696 697
            *out = sample;
        }
    }

    LeaveCriticalSection(&allocator->cs);

    return hr;
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
}

static const IMFVideoSampleAllocatorVtbl sample_allocator_vtbl =
{
    sample_allocator_QueryInterface,
    sample_allocator_AddRef,
    sample_allocator_Release,
    sample_allocator_SetDirectXManager,
    sample_allocator_UninitializeSampleAllocator,
    sample_allocator_InitializeSampleAllocator,
    sample_allocator_AllocateSample,
};

static HRESULT WINAPI sample_allocator_callback_QueryInterface(IMFVideoSampleAllocatorCallback *iface,
        REFIID riid, void **obj)
{
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocatorCallback(iface);
    return IMFVideoSampleAllocator_QueryInterface(&allocator->IMFVideoSampleAllocator_iface, riid, obj);
}

static ULONG WINAPI sample_allocator_callback_AddRef(IMFVideoSampleAllocatorCallback *iface)
{
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocatorCallback(iface);
    return IMFVideoSampleAllocator_AddRef(&allocator->IMFVideoSampleAllocator_iface);
}

static ULONG WINAPI sample_allocator_callback_Release(IMFVideoSampleAllocatorCallback *iface)
{
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocatorCallback(iface);
    return IMFVideoSampleAllocator_Release(&allocator->IMFVideoSampleAllocator_iface);
}

static HRESULT WINAPI sample_allocator_callback_SetCallback(IMFVideoSampleAllocatorCallback *iface,
        IMFVideoSampleAllocatorNotify *callback)
{
733
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocatorCallback(iface);
734

735 736 737 738 739 740 741 742 743 744 745
    TRACE("%p, %p.\n", iface, callback);

    EnterCriticalSection(&allocator->cs);
    if (allocator->callback)
        IMFVideoSampleAllocatorNotify_Release(allocator->callback);
    allocator->callback = callback;
    if (allocator->callback)
        IMFVideoSampleAllocatorNotify_AddRef(allocator->callback);
    LeaveCriticalSection(&allocator->cs);

    return S_OK;
746 747 748 749 750
}

static HRESULT WINAPI sample_allocator_callback_GetFreeSampleCount(IMFVideoSampleAllocatorCallback *iface,
        LONG *count)
{
751
    struct sample_allocator *allocator = impl_from_IMFVideoSampleAllocatorCallback(iface);
752

753 754 755 756
    TRACE("%p, %p.\n", iface, count);

    EnterCriticalSection(&allocator->cs);
    if (count)
757
        *count = allocator->free_sample_count;
758 759 760
    LeaveCriticalSection(&allocator->cs);

    return S_OK;
761 762 763 764 765 766 767 768 769 770 771
}

static const IMFVideoSampleAllocatorCallbackVtbl sample_allocator_callback_vtbl =
{
    sample_allocator_callback_QueryInterface,
    sample_allocator_callback_AddRef,
    sample_allocator_callback_Release,
    sample_allocator_callback_SetCallback,
    sample_allocator_callback_GetFreeSampleCount,
};

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
static HRESULT WINAPI sample_allocator_tracking_callback_QueryInterface(IMFAsyncCallback *iface,
        REFIID riid, void **obj)
{
    if (IsEqualIID(riid, &IID_IMFAsyncCallback) ||
            IsEqualIID(riid, &IID_IUnknown))
    {
        *obj = iface;
        IMFAsyncCallback_AddRef(iface);
        return S_OK;
    }

    WARN("Unsupported interface %s.\n", debugstr_guid(riid));
    *obj = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI sample_allocator_tracking_callback_AddRef(IMFAsyncCallback *iface)
{
    struct sample_allocator *allocator = impl_from_IMFAsyncCallback(iface);
    return IMFVideoSampleAllocator_AddRef(&allocator->IMFVideoSampleAllocator_iface);
}

static ULONG WINAPI sample_allocator_tracking_callback_Release(IMFAsyncCallback *iface)
{
    struct sample_allocator *allocator = impl_from_IMFAsyncCallback(iface);
    return IMFVideoSampleAllocator_Release(&allocator->IMFVideoSampleAllocator_iface);
}

static HRESULT WINAPI sample_allocator_tracking_callback_GetParameters(IMFAsyncCallback *iface,
        DWORD *flags, DWORD *queue)
{
    return E_NOTIMPL;
}

static HRESULT WINAPI sample_allocator_tracking_callback_Invoke(IMFAsyncCallback *iface, IMFAsyncResult *result)
{
    struct sample_allocator *allocator = impl_from_IMFAsyncCallback(iface);
    struct queued_sample *iter;
810 811 812
    IUnknown *object = NULL;
    IMFSample *sample = NULL;
    HRESULT hr;
813

814 815
    if (FAILED(IMFAsyncResult_GetObject(result, &object)))
        return E_UNEXPECTED;
816

817 818 819 820 821 822
    hr = IUnknown_QueryInterface(object, &IID_IMFSample, (void **)&sample);
    IUnknown_Release(object);
    if (FAILED(hr))
        return E_UNEXPECTED;

    EnterCriticalSection(&allocator->cs);
823 824 825

    LIST_FOR_EACH_ENTRY(iter, &allocator->used_samples, struct queued_sample, entry)
    {
826
        if (sample == iter->sample)
827 828 829
        {
            list_remove(&iter->entry);
            list_add_tail(&allocator->free_samples, &iter->entry);
830
            IMFSample_AddRef(iter->sample);
831 832 833 834 835
            allocator->free_sample_count++;
            break;
        }
    }

836
    IMFSample_Release(sample);
837

838 839 840
    if (allocator->callback)
        IMFVideoSampleAllocatorNotify_NotifyRelease(allocator->callback);

841 842 843 844 845 846 847 848 849 850 851 852 853 854
    LeaveCriticalSection(&allocator->cs);

    return S_OK;
}

static const IMFAsyncCallbackVtbl sample_allocator_tracking_callback_vtbl =
{
    sample_allocator_tracking_callback_QueryInterface,
    sample_allocator_tracking_callback_AddRef,
    sample_allocator_tracking_callback_Release,
    sample_allocator_tracking_callback_GetParameters,
    sample_allocator_tracking_callback_Invoke,
};

855 856 857 858 859 860 861
HRESULT WINAPI MFCreateVideoSampleAllocator(REFIID riid, void **obj)
{
    struct sample_allocator *object;
    HRESULT hr;

    TRACE("%s, %p.\n", debugstr_guid(riid), obj);

862
    if (!(object = calloc(1, sizeof(*object))))
863 864 865 866
        return E_OUTOFMEMORY;

    object->IMFVideoSampleAllocator_iface.lpVtbl = &sample_allocator_vtbl;
    object->IMFVideoSampleAllocatorCallback_iface.lpVtbl = &sample_allocator_callback_vtbl;
867
    object->tracking_callback.lpVtbl = &sample_allocator_tracking_callback_vtbl;
868
    object->refcount = 1;
869 870
    list_init(&object->used_samples);
    list_init(&object->free_samples);
871
    InitializeCriticalSection(&object->cs);
872 873 874 875 876 877

    hr = IMFVideoSampleAllocator_QueryInterface(&object->IMFVideoSampleAllocator_iface, riid, obj);
    IMFVideoSampleAllocator_Release(&object->IMFVideoSampleAllocator_iface);

    return hr;
}
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914

static HRESULT WINAPI video_sample_QueryInterface(IMFSample *iface, REFIID riid, void **out)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), out);

    if (IsEqualIID(riid, &IID_IMFSample) ||
            IsEqualIID(riid, &IID_IMFAttributes) ||
            IsEqualIID(riid, &IID_IUnknown))
    {
        *out = &sample->IMFSample_iface;
    }
    else if (IsEqualIID(riid, &IID_IMFTrackedSample))
    {
        *out = &sample->IMFTrackedSample_iface;
    }
    else if (IsEqualIID(riid, &IID_IMFDesiredSample))
    {
        *out = &sample->IMFDesiredSample_iface;
    }
    else
    {
        WARN("Unsupported %s.\n", debugstr_guid(riid));
        *out = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown *)*out);
    return S_OK;
}

static ULONG WINAPI video_sample_AddRef(IMFSample *iface)
{
    struct video_sample *sample = impl_from_IMFSample(iface);
    ULONG refcount = InterlockedIncrement(&sample->refcount);

915
    TRACE("%p, refcount %lu.\n", iface, refcount);
916 917 918 919 920 921 922

    return refcount;
}

static ULONG WINAPI video_sample_Release(IMFSample *iface)
{
    struct video_sample *sample = impl_from_IMFSample(iface);
923 924
    ULONG refcount = InterlockedDecrement(&sample->refcount);
    IMFAsyncResult *tracked_result = NULL;
925

926
    EnterCriticalSection(&sample->cs);
927
    if (sample->tracked_result && sample->tracked_refcount == refcount)
928
    {
929 930
        tracked_result = sample->tracked_result;
        video_sample_tracking_thread_invoke(tracked_result);
931 932 933
        sample->tracked_result = NULL;
        sample->tracked_refcount = 0;
    }
934
    LeaveCriticalSection(&sample->cs);
935

936 937
    if (tracked_result)
        IMFAsyncResult_Release(tracked_result);
938

939
    TRACE("%p, refcount %lu.\n", iface, refcount);
940 941 942

    if (!refcount)
    {
943
        video_sample_stop_tracking_thread();
944 945
        if (sample->sample)
            IMFSample_Release(sample->sample);
946
        DeleteCriticalSection(&sample->cs);
947
        free(sample);
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
    }

    return refcount;
}

static HRESULT WINAPI video_sample_GetItem(IMFSample *iface, REFGUID key, PROPVARIANT *value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);

    return IMFSample_GetItem(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_GetItemType(IMFSample *iface, REFGUID key, MF_ATTRIBUTE_TYPE *type)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), type);

    return IMFSample_GetItemType(sample->sample, key, type);
}

static HRESULT WINAPI video_sample_CompareItem(IMFSample *iface, REFGUID key, REFPROPVARIANT value, BOOL *result)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, result);

    return IMFSample_CompareItem(sample->sample, key, value, result);
}

static HRESULT WINAPI video_sample_Compare(IMFSample *iface, IMFAttributes *theirs, MF_ATTRIBUTES_MATCH_TYPE type,
        BOOL *result)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %p, %d, %p.\n", iface, theirs, type, result);

    return IMFSample_Compare(sample->sample, theirs, type, result);
}

static HRESULT WINAPI video_sample_GetUINT32(IMFSample *iface, REFGUID key, UINT32 *value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);

    return IMFSample_GetUINT32(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_GetUINT64(IMFSample *iface, REFGUID key, UINT64 *value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);

    return IMFSample_GetUINT64(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_GetDouble(IMFSample *iface, REFGUID key, double *value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);

    return IMFSample_GetDouble(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_GetGUID(IMFSample *iface, REFGUID key, GUID *value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);

    return IMFSample_GetGUID(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_GetStringLength(IMFSample *iface, REFGUID key, UINT32 *length)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), length);

    return IMFSample_GetStringLength(sample->sample, key, length);
}

static HRESULT WINAPI video_sample_GetString(IMFSample *iface, REFGUID key, WCHAR *value, UINT32 size, UINT32 *length)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p, %u, %p.\n", iface, debugstr_guid(key), value, size, length);

    return IMFSample_GetString(sample->sample, key, value, size, length);
}

static HRESULT WINAPI video_sample_GetAllocatedString(IMFSample *iface, REFGUID key, WCHAR **value, UINT32 *length)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), value, length);

    return IMFSample_GetAllocatedString(sample->sample, key, value, length);
}

static HRESULT WINAPI video_sample_GetBlobSize(IMFSample *iface, REFGUID key, UINT32 *size)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), size);

    return IMFSample_GetBlobSize(sample->sample, key, size);
}

static HRESULT WINAPI video_sample_GetBlob(IMFSample *iface, REFGUID key, UINT8 *buf, UINT32 bufsize, UINT32 *blobsize)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p, %u, %p.\n", iface, debugstr_guid(key), buf, bufsize, blobsize);

    return IMFSample_GetBlob(sample->sample, key, buf, bufsize, blobsize);
}

static HRESULT WINAPI video_sample_GetAllocatedBlob(IMFSample *iface, REFGUID key, UINT8 **buf, UINT32 *size)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p, %p.\n", iface, debugstr_guid(key), buf, size);

    return IMFSample_GetAllocatedBlob(sample->sample, key, buf, size);
}

static HRESULT WINAPI video_sample_GetUnknown(IMFSample *iface, REFGUID key, REFIID riid, void **out)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(key), debugstr_guid(riid), out);

    return IMFSample_GetUnknown(sample->sample, key, riid, out);
}

static HRESULT WINAPI video_sample_SetItem(IMFSample *iface, REFGUID key, REFPROPVARIANT value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), value);

    return IMFSample_SetItem(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_DeleteItem(IMFSample *iface, REFGUID key)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s.\n", iface, debugstr_guid(key));

    return IMFSample_DeleteItem(sample->sample, key);
}

static HRESULT WINAPI video_sample_DeleteAllItems(IMFSample *iface)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p.\n", iface);

    return IMFSample_DeleteAllItems(sample->sample);
}

static HRESULT WINAPI video_sample_SetUINT32(IMFSample *iface, REFGUID key, UINT32 value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %u.\n", iface, debugstr_guid(key), value);

    return IMFSample_SetUINT32(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_SetUINT64(IMFSample *iface, REFGUID key, UINT64 value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), wine_dbgstr_longlong(value));

    return IMFSample_SetUINT64(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_SetDouble(IMFSample *iface, REFGUID key, double value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %f.\n", iface, debugstr_guid(key), value);

    return IMFSample_SetDouble(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_SetGUID(IMFSample *iface, REFGUID key, REFGUID value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_guid(value));

    return IMFSample_SetGUID(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_SetString(IMFSample *iface, REFGUID key, const WCHAR *value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %s.\n", iface, debugstr_guid(key), debugstr_w(value));

    return IMFSample_SetString(sample->sample, key, value);
}

static HRESULT WINAPI video_sample_SetBlob(IMFSample *iface, REFGUID key, const UINT8 *buf, UINT32 size)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p, %u.\n", iface, debugstr_guid(key), buf, size);

    return IMFSample_SetBlob(sample->sample, key, buf, size);
}

static HRESULT WINAPI video_sample_SetUnknown(IMFSample *iface, REFGUID key, IUnknown *unknown)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(key), unknown);

    return IMFSample_SetUnknown(sample->sample, key, unknown);
}

static HRESULT WINAPI video_sample_LockStore(IMFSample *iface)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p.\n", iface);

    return IMFSample_LockStore(sample->sample);
}

static HRESULT WINAPI video_sample_UnlockStore(IMFSample *iface)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p.\n", iface);

    return IMFSample_UnlockStore(sample->sample);
}

static HRESULT WINAPI video_sample_GetCount(IMFSample *iface, UINT32 *count)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %p.\n", iface, count);

    return IMFSample_GetCount(sample->sample, count);
}

static HRESULT WINAPI video_sample_GetItemByIndex(IMFSample *iface, UINT32 index, GUID *key, PROPVARIANT *value)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %u, %p, %p.\n", iface, index, key, value);

    return IMFSample_GetItemByIndex(sample->sample, index, key, value);
}

static HRESULT WINAPI video_sample_CopyAllItems(IMFSample *iface, IMFAttributes *dest)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %p.\n", iface, dest);

    return IMFSample_CopyAllItems(sample->sample, dest);
}

static HRESULT WINAPI video_sample_GetSampleFlags(IMFSample *iface, DWORD *flags)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %p.\n", iface, flags);

    return IMFSample_GetSampleFlags(sample->sample, flags);
}

static HRESULT WINAPI video_sample_SetSampleFlags(IMFSample *iface, DWORD flags)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

1237
    TRACE("%p, %#lx.\n", iface, flags);
1238 1239 1240 1241 1242 1243 1244

    return IMFSample_SetSampleFlags(sample->sample, flags);
}

static HRESULT WINAPI video_sample_GetSampleTime(IMFSample *iface, LONGLONG *timestamp)
{
    struct video_sample *sample = impl_from_IMFSample(iface);
1245
    HRESULT hr = S_OK;
1246 1247 1248

    TRACE("%p, %p.\n", iface, timestamp);

1249 1250 1251 1252 1253 1254 1255 1256
    EnterCriticalSection(&sample->cs);
    if (sample->flags & SAMPLE_PROP_HAS_TIMESTAMP)
        *timestamp = sample->timestamp;
    else
        hr = MF_E_NO_SAMPLE_TIMESTAMP;
    LeaveCriticalSection(&sample->cs);

    return hr;
1257 1258 1259 1260 1261 1262 1263 1264
}

static HRESULT WINAPI video_sample_SetSampleTime(IMFSample *iface, LONGLONG timestamp)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s.\n", iface, debugstr_time(timestamp));

1265 1266 1267 1268 1269 1270
    EnterCriticalSection(&sample->cs);
    sample->timestamp = timestamp;
    sample->flags |= SAMPLE_PROP_HAS_TIMESTAMP;
    LeaveCriticalSection(&sample->cs);

    return S_OK;
1271 1272 1273 1274 1275
}

static HRESULT WINAPI video_sample_GetSampleDuration(IMFSample *iface, LONGLONG *duration)
{
    struct video_sample *sample = impl_from_IMFSample(iface);
1276
    HRESULT hr = S_OK;
1277 1278 1279

    TRACE("%p, %p.\n", iface, duration);

1280 1281 1282 1283 1284 1285 1286 1287
    EnterCriticalSection(&sample->cs);
    if (sample->flags & SAMPLE_PROP_HAS_DURATION)
        *duration = sample->duration;
    else
        hr = MF_E_NO_SAMPLE_DURATION;
    LeaveCriticalSection(&sample->cs);

    return hr;
1288 1289 1290 1291 1292 1293 1294 1295
}

static HRESULT WINAPI video_sample_SetSampleDuration(IMFSample *iface, LONGLONG duration)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %s.\n", iface, debugstr_time(duration));

1296 1297 1298 1299 1300 1301
    EnterCriticalSection(&sample->cs);
    sample->duration = duration;
    sample->flags |= SAMPLE_PROP_HAS_DURATION;
    LeaveCriticalSection(&sample->cs);

    return S_OK;
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
}

static HRESULT WINAPI video_sample_GetBufferCount(IMFSample *iface, DWORD *count)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %p.\n", iface, count);

    return IMFSample_GetBufferCount(sample->sample, count);
}

static HRESULT WINAPI video_sample_GetBufferByIndex(IMFSample *iface, DWORD index, IMFMediaBuffer **buffer)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

1317
    TRACE("%p, %lu, %p.\n", iface, index, buffer);
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341

    return IMFSample_GetBufferByIndex(sample->sample, index, buffer);
}

static HRESULT WINAPI video_sample_ConvertToContiguousBuffer(IMFSample *iface, IMFMediaBuffer **buffer)
{
    TRACE("%p, %p.\n", iface, buffer);

    return E_NOTIMPL;
}

static HRESULT WINAPI video_sample_AddBuffer(IMFSample *iface, IMFMediaBuffer *buffer)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p, %p.\n", iface, buffer);

    return IMFSample_AddBuffer(sample->sample, buffer);
}

static HRESULT WINAPI video_sample_RemoveBufferByIndex(IMFSample *iface, DWORD index)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

1342
    TRACE("%p, %lu.\n", iface, index);
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

    return IMFSample_RemoveBufferByIndex(sample->sample, index);
}

static HRESULT WINAPI video_sample_RemoveAllBuffers(IMFSample *iface)
{
    struct video_sample *sample = impl_from_IMFSample(iface);

    TRACE("%p.\n", iface);

    return IMFSample_RemoveAllBuffers(sample->sample);
}

static HRESULT WINAPI video_sample_GetTotalLength(IMFSample *iface, DWORD *total_length)
{
    TRACE("%p, %p.\n", iface, total_length);

    *total_length = 0;

    return S_OK;
}

static HRESULT WINAPI video_sample_CopyToBuffer(IMFSample *iface, IMFMediaBuffer *buffer)
{
    TRACE("%p, %p.\n", iface, buffer);

    return E_NOTIMPL;
}

static const IMFSampleVtbl video_sample_vtbl =
{
    video_sample_QueryInterface,
    video_sample_AddRef,
    video_sample_Release,
    video_sample_GetItem,
    video_sample_GetItemType,
    video_sample_CompareItem,
    video_sample_Compare,
    video_sample_GetUINT32,
    video_sample_GetUINT64,
    video_sample_GetDouble,
    video_sample_GetGUID,
    video_sample_GetStringLength,
    video_sample_GetString,
    video_sample_GetAllocatedString,
    video_sample_GetBlobSize,
    video_sample_GetBlob,
    video_sample_GetAllocatedBlob,
    video_sample_GetUnknown,
    video_sample_SetItem,
    video_sample_DeleteItem,
    video_sample_DeleteAllItems,
    video_sample_SetUINT32,
    video_sample_SetUINT64,
    video_sample_SetDouble,
    video_sample_SetGUID,
    video_sample_SetString,
    video_sample_SetBlob,
    video_sample_SetUnknown,
    video_sample_LockStore,
    video_sample_UnlockStore,
    video_sample_GetCount,
    video_sample_GetItemByIndex,
    video_sample_CopyAllItems,
    video_sample_GetSampleFlags,
    video_sample_SetSampleFlags,
    video_sample_GetSampleTime,
    video_sample_SetSampleTime,
    video_sample_GetSampleDuration,
    video_sample_SetSampleDuration,
    video_sample_GetBufferCount,
    video_sample_GetBufferByIndex,
    video_sample_ConvertToContiguousBuffer,
    video_sample_AddBuffer,
    video_sample_RemoveBufferByIndex,
    video_sample_RemoveAllBuffers,
    video_sample_GetTotalLength,
    video_sample_CopyToBuffer,
};

static HRESULT WINAPI tracked_video_sample_QueryInterface(IMFTrackedSample *iface, REFIID riid, void **obj)
{
    struct video_sample *sample = impl_from_IMFTrackedSample(iface);
    return IMFSample_QueryInterface(&sample->IMFSample_iface, riid, obj);
}

static ULONG WINAPI tracked_video_sample_AddRef(IMFTrackedSample *iface)
{
    struct video_sample *sample = impl_from_IMFTrackedSample(iface);
    return IMFSample_AddRef(&sample->IMFSample_iface);
}

static ULONG WINAPI tracked_video_sample_Release(IMFTrackedSample *iface)
{
    struct video_sample *sample = impl_from_IMFTrackedSample(iface);
    return IMFSample_Release(&sample->IMFSample_iface);
}

static HRESULT WINAPI tracked_video_sample_SetAllocator(IMFTrackedSample *iface,
        IMFAsyncCallback *sample_allocator, IUnknown *state)
{
    struct video_sample *sample = impl_from_IMFTrackedSample(iface);
    HRESULT hr = S_OK;

    TRACE("%p, %p, %p.\n", iface, sample_allocator, state);

1449
    EnterCriticalSection(&sample->cs);
1450 1451 1452 1453
    if (sample->tracked_result)
        hr = MF_E_NOTACCEPTING;
    else
    {
1454
        if (SUCCEEDED(hr = create_async_result((IUnknown *)iface, sample_allocator, state, &sample->tracked_result)))
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
        {
            /* Account for additional refcount brought by 'state' object. This threshold is used
               on Release() to invoke tracker callback.  */
            sample->tracked_refcount = 1;
            if (state == (IUnknown *)&sample->IMFTrackedSample_iface ||
                    state == (IUnknown *)&sample->IMFSample_iface)
            {
                ++sample->tracked_refcount;
            }
        }
    }
1466
    LeaveCriticalSection(&sample->cs);
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499

    return hr;
}

static const IMFTrackedSampleVtbl tracked_video_sample_vtbl =
{
    tracked_video_sample_QueryInterface,
    tracked_video_sample_AddRef,
    tracked_video_sample_Release,
    tracked_video_sample_SetAllocator,
};

static HRESULT WINAPI desired_video_sample_QueryInterface(IMFDesiredSample *iface, REFIID riid, void **obj)
{
    struct video_sample *sample = impl_from_IMFDesiredSample(iface);
    return IMFSample_QueryInterface(&sample->IMFSample_iface, riid, obj);
}

static ULONG WINAPI desired_video_sample_AddRef(IMFDesiredSample *iface)
{
    struct video_sample *sample = impl_from_IMFDesiredSample(iface);
    return IMFSample_AddRef(&sample->IMFSample_iface);
}

static ULONG WINAPI desired_video_sample_Release(IMFDesiredSample *iface)
{
    struct video_sample *sample = impl_from_IMFDesiredSample(iface);
    return IMFSample_Release(&sample->IMFSample_iface);
}

static HRESULT WINAPI desired_video_sample_GetDesiredSampleTimeAndDuration(IMFDesiredSample *iface,
        LONGLONG *sample_time, LONGLONG *sample_duration)
{
1500 1501
    struct video_sample *sample = impl_from_IMFDesiredSample(iface);
    HRESULT hr = S_OK;
1502

1503 1504 1505 1506 1507
    TRACE("%p, %p, %p.\n", iface, sample_time, sample_duration);

    if (!sample_time || !sample_duration)
        return E_POINTER;

1508 1509
    EnterCriticalSection(&sample->cs);
    if (sample->flags & SAMPLE_PROP_HAS_DESIRED_PROPS)
1510
    {
1511
        *sample_time = sample->desired_timestamp;
1512 1513 1514 1515
        *sample_duration = sample->desired_duration;
    }
    else
        hr = MF_E_NOT_AVAILABLE;
1516
    LeaveCriticalSection(&sample->cs);
1517 1518

    return hr;
1519 1520 1521 1522 1523
}

static void WINAPI desired_video_sample_SetDesiredSampleTimeAndDuration(IMFDesiredSample *iface,
        LONGLONG sample_time, LONGLONG sample_duration)
{
1524 1525 1526 1527
    struct video_sample *sample = impl_from_IMFDesiredSample(iface);

    TRACE("%p, %s, %s.\n", iface, debugstr_time(sample_time), debugstr_time(sample_duration));

1528 1529 1530
    EnterCriticalSection(&sample->cs);
    sample->flags |= SAMPLE_PROP_HAS_DESIRED_PROPS;
    sample->desired_timestamp = sample_time;
1531
    sample->desired_duration = sample_duration;
1532
    LeaveCriticalSection(&sample->cs);
1533 1534 1535 1536
}

static void WINAPI desired_video_sample_Clear(IMFDesiredSample *iface)
{
1537 1538 1539 1540
    struct video_sample *sample = impl_from_IMFDesiredSample(iface);

    TRACE("%p.\n", iface);

1541 1542 1543 1544 1545
    EnterCriticalSection(&sample->cs);
    sample->flags = 0;
    IMFSample_SetSampleFlags(sample->sample, 0);
    IMFSample_DeleteAllItems(sample->sample);
    LeaveCriticalSection(&sample->cs);
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
}

static const IMFDesiredSampleVtbl desired_video_sample_vtbl =
{
    desired_video_sample_QueryInterface,
    desired_video_sample_AddRef,
    desired_video_sample_Release,
    desired_video_sample_GetDesiredSampleTimeAndDuration,
    desired_video_sample_SetDesiredSampleTimeAndDuration,
    desired_video_sample_Clear,
};

1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
static HRESULT WINAPI surface_buffer_QueryInterface(IMFMediaBuffer *iface, REFIID riid, void **obj)
{
    struct surface_buffer *buffer = impl_from_IMFMediaBuffer(iface);

    TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), obj);

    if (IsEqualIID(riid, &IID_IMFMediaBuffer) || IsEqualIID(riid, &IID_IUnknown))
    {
        *obj = &buffer->IMFMediaBuffer_iface;
    }
    else if (IsEqualIID(riid, &IID_IMFGetService))
    {
        *obj = &buffer->IMFGetService_iface;
    }
    else
    {
        WARN("Unsupported interface %s.\n", debugstr_guid(riid));
        *obj = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown *)*obj);
    return S_OK;
}

static ULONG WINAPI surface_buffer_AddRef(IMFMediaBuffer *iface)
{
    struct surface_buffer *buffer = impl_from_IMFMediaBuffer(iface);
    ULONG refcount = InterlockedIncrement(&buffer->refcount);

1588
    TRACE("%p, refcount %lu.\n", iface, refcount);
1589 1590 1591 1592 1593 1594 1595 1596 1597

    return refcount;
}

static ULONG WINAPI surface_buffer_Release(IMFMediaBuffer *iface)
{
    struct surface_buffer *buffer = impl_from_IMFMediaBuffer(iface);
    ULONG refcount = InterlockedDecrement(&buffer->refcount);

1598
    TRACE("%p, refcount %lu.\n", iface, refcount);
1599 1600 1601 1602

    if (!refcount)
    {
        IUnknown_Release(buffer->surface);
1603
        free(buffer);
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
    }

    return refcount;
}

static HRESULT WINAPI surface_buffer_Lock(IMFMediaBuffer *iface, BYTE **data, DWORD *maxlength, DWORD *length)
{
    TRACE("%p, %p, %p, %p.\n", iface, data, maxlength, length);

    return E_NOTIMPL;
}

static HRESULT WINAPI surface_buffer_Unlock(IMFMediaBuffer *iface)
{
    TRACE("%p.\n", iface);

    return E_NOTIMPL;
}

static HRESULT WINAPI surface_buffer_GetCurrentLength(IMFMediaBuffer *iface, DWORD *length)
{
    struct surface_buffer *buffer = impl_from_IMFMediaBuffer(iface);

    TRACE("%p.\n", iface);

    *length = buffer->length;

    return S_OK;
}

static HRESULT WINAPI surface_buffer_SetCurrentLength(IMFMediaBuffer *iface, DWORD length)
{
    struct surface_buffer *buffer = impl_from_IMFMediaBuffer(iface);

    TRACE("%p.\n", iface);

    buffer->length = length;

    return S_OK;
}

static HRESULT WINAPI surface_buffer_GetMaxLength(IMFMediaBuffer *iface, DWORD *length)
{
    TRACE("%p.\n", iface);

    return E_NOTIMPL;
}

static const IMFMediaBufferVtbl surface_buffer_vtbl =
{
    surface_buffer_QueryInterface,
    surface_buffer_AddRef,
    surface_buffer_Release,
    surface_buffer_Lock,
    surface_buffer_Unlock,
    surface_buffer_GetCurrentLength,
    surface_buffer_SetCurrentLength,
    surface_buffer_GetMaxLength,
};

static HRESULT WINAPI surface_buffer_gs_QueryInterface(IMFGetService *iface, REFIID riid, void **obj)
{
    struct surface_buffer *buffer = impl_from_IMFGetService(iface);
    return IMFMediaBuffer_QueryInterface(&buffer->IMFMediaBuffer_iface, riid, obj);
}

static ULONG WINAPI surface_buffer_gs_AddRef(IMFGetService *iface)
{
    struct surface_buffer *buffer = impl_from_IMFGetService(iface);
    return IMFMediaBuffer_AddRef(&buffer->IMFMediaBuffer_iface);
}

static ULONG WINAPI surface_buffer_gs_Release(IMFGetService *iface)
{
    struct surface_buffer *buffer = impl_from_IMFGetService(iface);
    return IMFMediaBuffer_Release(&buffer->IMFMediaBuffer_iface);
}

static HRESULT WINAPI surface_buffer_gs_GetService(IMFGetService *iface, REFGUID service, REFIID riid, void **obj)
{
1684
    struct surface_buffer *buffer = impl_from_IMFGetService(iface);
1685

1686 1687 1688 1689 1690 1691
    TRACE("%p, %s, %s, %p.\n", iface, debugstr_guid(service), debugstr_guid(riid), obj);

    if (IsEqualGUID(service, &MR_BUFFER_SERVICE))
        return IUnknown_QueryInterface(buffer->surface, riid, obj);

    return E_NOINTERFACE;
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
}

static const IMFGetServiceVtbl surface_buffer_gs_vtbl =
{
    surface_buffer_gs_QueryInterface,
    surface_buffer_gs_AddRef,
    surface_buffer_gs_Release,
    surface_buffer_gs_GetService,
};

static HRESULT create_surface_buffer(IUnknown *surface, IMFMediaBuffer **buffer)
{
    struct surface_buffer *object;

1706
    if (!(object = calloc(1, sizeof(*object))))
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
        return E_OUTOFMEMORY;

    object->IMFMediaBuffer_iface.lpVtbl = &surface_buffer_vtbl;
    object->IMFGetService_iface.lpVtbl = &surface_buffer_gs_vtbl;
    object->refcount = 1;
    object->surface = surface;
    IUnknown_AddRef(object->surface);

    *buffer = &object->IMFMediaBuffer_iface;

    return S_OK;
}

1720 1721 1722
HRESULT WINAPI MFCreateVideoSampleFromSurface(IUnknown *surface, IMFSample **sample)
{
    struct video_sample *object;
1723
    IMFMediaBuffer *buffer = NULL;
1724 1725 1726 1727
    HRESULT hr;

    TRACE("%p, %p.\n", surface, sample);

1728
    if (!(object = calloc(1, sizeof(*object))))
1729 1730 1731 1732 1733 1734
        return E_OUTOFMEMORY;

    object->IMFSample_iface.lpVtbl = &video_sample_vtbl;
    object->IMFTrackedSample_iface.lpVtbl = &tracked_video_sample_vtbl;
    object->IMFDesiredSample_iface.lpVtbl = &desired_video_sample_vtbl;
    object->refcount = 1;
1735
    InitializeCriticalSection(&object->cs);
1736 1737 1738

    if (FAILED(hr = MFCreateSample(&object->sample)))
    {
1739
        free(object);
1740 1741 1742
        return hr;
    }

1743 1744 1745 1746 1747 1748 1749
    if (surface && FAILED(hr = create_surface_buffer(surface, &buffer)))
    {
        IMFSample_Release(&object->IMFSample_iface);
        return hr;
    }

    if (buffer)
1750
    {
1751
        IMFSample_AddBuffer(object->sample, buffer);
1752 1753
        IMFMediaBuffer_Release(buffer);
    }
1754

1755 1756
    video_sample_create_tracking_thread();

1757 1758 1759 1760
    *sample = &object->IMFSample_iface;

    return S_OK;
}