include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / quartz / memallocator.c
blobca786ab00463c7449c170e9c55e1294b0a186c81
1 /*
2 * Memory Allocator and Media Sample Implementation
4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * 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
21 #include <assert.h>
22 #include <limits.h>
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "vfwmsgs.h"
29 #include "quartz_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
34 typedef struct StdMediaSample2
36 IMediaSample2 IMediaSample2_iface;
37 LONG ref;
38 AM_SAMPLE2_PROPERTIES props;
39 IMemAllocator * pParent;
40 struct list listentry;
41 LONGLONG tMediaStart;
42 LONGLONG tMediaEnd;
43 BOOL media_time_valid;
44 } StdMediaSample2;
46 typedef struct BaseMemAllocator
48 IMemAllocator IMemAllocator_iface;
50 LONG ref;
51 ALLOCATOR_PROPERTIES props;
52 HRESULT (* fnAlloc) (IMemAllocator *);
53 HRESULT (* fnFree)(IMemAllocator *);
54 HRESULT (* fnVerify)(IMemAllocator *, ALLOCATOR_PROPERTIES *);
55 HRESULT (* fnBufferPrepare)(IMemAllocator *, StdMediaSample2 *, DWORD flags);
56 HRESULT (* fnBufferReleased)(IMemAllocator *, StdMediaSample2 *);
57 void (* fnDestroyed)(IMemAllocator *);
58 HANDLE hSemWaiting;
59 BOOL bDecommitQueued;
60 BOOL bCommitted;
61 LONG lWaiting;
62 struct list free_list;
63 struct list used_list;
64 CRITICAL_SECTION *pCritSect;
65 } BaseMemAllocator;
67 static inline BaseMemAllocator *impl_from_IMemAllocator(IMemAllocator *iface)
69 return CONTAINING_RECORD(iface, BaseMemAllocator, IMemAllocator_iface);
72 static const IMemAllocatorVtbl BaseMemAllocator_VTable;
73 static const IMediaSample2Vtbl StdMediaSample2_VTable;
74 static inline StdMediaSample2 *unsafe_impl_from_IMediaSample(IMediaSample * iface);
76 #define AM_SAMPLE2_PROP_SIZE_WRITABLE FIELD_OFFSET(AM_SAMPLE2_PROPERTIES, pbBuffer)
78 static HRESULT BaseMemAllocator_Init(HRESULT (* fnAlloc)(IMemAllocator *),
79 HRESULT (* fnFree)(IMemAllocator *),
80 HRESULT (* fnVerify)(IMemAllocator *, ALLOCATOR_PROPERTIES *),
81 HRESULT (* fnBufferPrepare)(IMemAllocator *, StdMediaSample2 *, DWORD),
82 HRESULT (* fnBufferReleased)(IMemAllocator *, StdMediaSample2 *),
83 void (* fnDestroyed)(IMemAllocator *),
84 CRITICAL_SECTION *pCritSect,
85 BaseMemAllocator * pMemAlloc)
87 assert(fnAlloc && fnFree && fnDestroyed);
89 pMemAlloc->IMemAllocator_iface.lpVtbl = &BaseMemAllocator_VTable;
91 pMemAlloc->ref = 1;
92 ZeroMemory(&pMemAlloc->props, sizeof(pMemAlloc->props));
93 list_init(&pMemAlloc->free_list);
94 list_init(&pMemAlloc->used_list);
95 pMemAlloc->fnAlloc = fnAlloc;
96 pMemAlloc->fnFree = fnFree;
97 pMemAlloc->fnVerify = fnVerify;
98 pMemAlloc->fnBufferPrepare = fnBufferPrepare;
99 pMemAlloc->fnBufferReleased = fnBufferReleased;
100 pMemAlloc->fnDestroyed = fnDestroyed;
101 pMemAlloc->bDecommitQueued = FALSE;
102 pMemAlloc->bCommitted = FALSE;
103 pMemAlloc->hSemWaiting = NULL;
104 pMemAlloc->lWaiting = 0;
105 pMemAlloc->pCritSect = pCritSect;
107 return S_OK;
110 static HRESULT WINAPI BaseMemAllocator_QueryInterface(IMemAllocator * iface, REFIID riid, LPVOID * ppv)
112 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
113 TRACE("(%p)->(%s, %p)\n", This, qzdebugstr_guid(riid), ppv);
115 *ppv = NULL;
117 if (IsEqualIID(riid, &IID_IUnknown))
118 *ppv = &This->IMemAllocator_iface;
119 else if (IsEqualIID(riid, &IID_IMemAllocator))
120 *ppv = &This->IMemAllocator_iface;
122 if (*ppv)
124 IUnknown_AddRef((IUnknown *)(*ppv));
125 return S_OK;
128 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
130 return E_NOINTERFACE;
133 static ULONG WINAPI BaseMemAllocator_AddRef(IMemAllocator * iface)
135 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
136 ULONG ref = InterlockedIncrement(&This->ref);
138 TRACE("%p increasing refcount to %lu.\n", This, ref);
140 return ref;
143 static ULONG WINAPI BaseMemAllocator_Release(IMemAllocator * iface)
145 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
146 ULONG ref = InterlockedDecrement(&This->ref);
148 TRACE("%p decreasing refcount to %lu.\n", This, ref);
150 if (!ref)
152 CloseHandle(This->hSemWaiting);
153 if (This->bCommitted)
154 This->fnFree(iface);
156 This->fnDestroyed(iface);
157 return 0;
159 return ref;
162 static HRESULT WINAPI BaseMemAllocator_SetProperties(IMemAllocator * iface, ALLOCATOR_PROPERTIES *pRequest, ALLOCATOR_PROPERTIES *pActual)
164 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
165 HRESULT hr;
167 TRACE("(%p)->(%p, %p)\n", This, pRequest, pActual);
169 TRACE("Requested %ld buffers, size %ld, alignment %ld, prefix %ld.\n",
170 pRequest->cBuffers, pRequest->cbBuffer, pRequest->cbAlign, pRequest->cbPrefix);
172 EnterCriticalSection(This->pCritSect);
174 if (!list_empty(&This->used_list))
175 hr = VFW_E_BUFFERS_OUTSTANDING;
176 else if (This->bCommitted)
177 hr = VFW_E_ALREADY_COMMITTED;
178 else if (pRequest->cbAlign == 0)
179 hr = VFW_E_BADALIGN;
180 else
182 if (This->fnVerify)
183 hr = This->fnVerify(iface, pRequest);
184 else
185 hr = S_OK;
187 if (SUCCEEDED(hr))
188 This->props = *pRequest;
190 *pActual = This->props;
193 LeaveCriticalSection(This->pCritSect);
195 return hr;
198 static HRESULT WINAPI BaseMemAllocator_GetProperties(IMemAllocator * iface, ALLOCATOR_PROPERTIES *pProps)
200 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
202 TRACE("(%p)->(%p)\n", This, pProps);
204 EnterCriticalSection(This->pCritSect);
206 memcpy(pProps, &This->props, sizeof(*pProps));
208 LeaveCriticalSection(This->pCritSect);
210 return S_OK;
213 static HRESULT WINAPI BaseMemAllocator_Commit(IMemAllocator * iface)
215 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
216 HRESULT hr;
218 TRACE("(%p)->()\n", This);
220 EnterCriticalSection(This->pCritSect);
222 if (!This->props.cbAlign)
223 hr = VFW_E_BADALIGN;
224 else if (!This->props.cbBuffer)
225 hr = VFW_E_SIZENOTSET;
226 else if (!This->props.cBuffers)
227 hr = VFW_E_BUFFER_NOTSET;
228 else if (This->bDecommitQueued && This->bCommitted)
230 This->bDecommitQueued = FALSE;
231 hr = S_OK;
233 else if (This->bCommitted)
234 hr = S_OK;
235 else
237 if (!(This->hSemWaiting = CreateSemaphoreW(NULL, This->props.cBuffers, This->props.cBuffers, NULL)))
239 ERR("Failed to create semaphore, error %lu.\n", GetLastError());
240 hr = HRESULT_FROM_WIN32(GetLastError());
242 else
244 hr = This->fnAlloc(iface);
245 if (SUCCEEDED(hr))
246 This->bCommitted = TRUE;
247 else
248 ERR("Failed to allocate, hr %#lx.\n", hr);
252 LeaveCriticalSection(This->pCritSect);
254 return hr;
257 static HRESULT WINAPI BaseMemAllocator_Decommit(IMemAllocator * iface)
259 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
260 HRESULT hr;
262 TRACE("(%p)->()\n", This);
264 EnterCriticalSection(This->pCritSect);
266 if (!This->bCommitted)
267 hr = S_OK;
268 else
270 if (!list_empty(&This->used_list))
272 This->bDecommitQueued = TRUE;
273 /* notify ALL waiting threads that they cannot be allocated a buffer any more */
274 ReleaseSemaphore(This->hSemWaiting, This->lWaiting, NULL);
276 hr = S_OK;
278 else
280 if (This->lWaiting != 0)
281 ERR("Waiting: %ld\n", This->lWaiting);
283 This->bCommitted = FALSE;
284 CloseHandle(This->hSemWaiting);
285 This->hSemWaiting = NULL;
287 hr = This->fnFree(iface);
291 LeaveCriticalSection(This->pCritSect);
293 return hr;
296 static HRESULT WINAPI BaseMemAllocator_GetBuffer(IMemAllocator * iface, IMediaSample ** pSample, REFERENCE_TIME *pStartTime, REFERENCE_TIME *pEndTime, DWORD dwFlags)
298 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
299 HRESULT hr = S_OK;
301 /* NOTE: The pStartTime and pEndTime parameters are not applied to the sample.
302 * The allocator might use these values to determine which buffer it retrieves */
304 TRACE("allocator %p, sample %p, start_time %p, end_time %p, flags %#lx.\n",
305 This, pSample, pStartTime, pEndTime, dwFlags);
307 *pSample = NULL;
309 EnterCriticalSection(This->pCritSect);
310 if (!This->bCommitted || This->bDecommitQueued)
312 WARN("Not committed\n");
313 hr = VFW_E_NOT_COMMITTED;
315 else
316 ++This->lWaiting;
317 LeaveCriticalSection(This->pCritSect);
318 if (FAILED(hr))
319 return hr;
321 if (WaitForSingleObject(This->hSemWaiting, (dwFlags & AM_GBF_NOWAIT) ? 0 : INFINITE) != WAIT_OBJECT_0)
323 EnterCriticalSection(This->pCritSect);
324 --This->lWaiting;
325 LeaveCriticalSection(This->pCritSect);
326 WARN("Timed out\n");
327 return VFW_E_TIMEOUT;
330 EnterCriticalSection(This->pCritSect);
332 --This->lWaiting;
333 if (!This->bCommitted)
334 hr = VFW_E_NOT_COMMITTED;
335 else if (This->bDecommitQueued)
336 hr = VFW_E_TIMEOUT;
337 else
339 StdMediaSample2 *ms;
340 struct list * free = list_head(&This->free_list);
341 list_remove(free);
342 list_add_head(&This->used_list, free);
344 ms = LIST_ENTRY(free, StdMediaSample2, listentry);
345 assert(ms->ref == 0);
346 *pSample = (IMediaSample *)&ms->IMediaSample2_iface;
347 IMediaSample_AddRef(*pSample);
350 LeaveCriticalSection(This->pCritSect);
352 if (hr != S_OK)
353 WARN("Returning hr %#lx.\n", hr);
354 return hr;
357 static HRESULT WINAPI BaseMemAllocator_ReleaseBuffer(IMemAllocator * iface, IMediaSample * pSample)
359 BaseMemAllocator *This = impl_from_IMemAllocator(iface);
360 StdMediaSample2 * pStdSample = unsafe_impl_from_IMediaSample(pSample);
361 HRESULT hr = S_OK;
363 TRACE("(%p)->(%p)\n", This, pSample);
365 /* FIXME: make sure that sample is currently on the used list */
367 /* FIXME: we should probably check the ref count on the sample before freeing
368 * it to make sure that it is not still in use */
369 EnterCriticalSection(This->pCritSect);
371 if (!This->bCommitted)
372 ERR("Releasing a buffer when the allocator is not committed?!?\n");
374 /* remove from used_list */
375 list_remove(&pStdSample->listentry);
377 list_add_head(&This->free_list, &pStdSample->listentry);
379 if (list_empty(&This->used_list) && This->bDecommitQueued && This->bCommitted)
381 if (This->lWaiting != 0)
382 ERR("Waiting: %ld\n", This->lWaiting);
384 This->bCommitted = FALSE;
385 This->bDecommitQueued = FALSE;
387 CloseHandle(This->hSemWaiting);
388 This->hSemWaiting = NULL;
390 This->fnFree(iface);
393 LeaveCriticalSection(This->pCritSect);
395 /* notify a waiting thread that there is now a free buffer */
396 if (This->hSemWaiting && !ReleaseSemaphore(This->hSemWaiting, 1, NULL))
398 ERR("Failed to release semaphore, error %lu.\n", GetLastError());
399 hr = HRESULT_FROM_WIN32(GetLastError());
402 return hr;
405 static const IMemAllocatorVtbl BaseMemAllocator_VTable =
407 BaseMemAllocator_QueryInterface,
408 BaseMemAllocator_AddRef,
409 BaseMemAllocator_Release,
410 BaseMemAllocator_SetProperties,
411 BaseMemAllocator_GetProperties,
412 BaseMemAllocator_Commit,
413 BaseMemAllocator_Decommit,
414 BaseMemAllocator_GetBuffer,
415 BaseMemAllocator_ReleaseBuffer
418 static HRESULT StdMediaSample2_Construct(BYTE * pbBuffer, LONG cbBuffer, IMemAllocator * pParent, StdMediaSample2 ** ppSample)
420 assert(pbBuffer && pParent && (cbBuffer > 0));
422 if (!(*ppSample = CoTaskMemAlloc(sizeof(StdMediaSample2))))
423 return E_OUTOFMEMORY;
425 (*ppSample)->IMediaSample2_iface.lpVtbl = &StdMediaSample2_VTable;
426 (*ppSample)->ref = 0;
427 ZeroMemory(&(*ppSample)->props, sizeof((*ppSample)->props));
429 /* NOTE: no need to AddRef as the parent is guaranteed to be around
430 * at least as long as us and we don't want to create circular
431 * dependencies on the ref count */
432 (*ppSample)->pParent = pParent;
433 (*ppSample)->props.cbData = sizeof(AM_SAMPLE2_PROPERTIES);
434 (*ppSample)->props.cbBuffer = (*ppSample)->props.lActual = cbBuffer;
435 (*ppSample)->props.pbBuffer = pbBuffer;
436 (*ppSample)->media_time_valid = FALSE;
438 return S_OK;
441 static void StdMediaSample2_Delete(StdMediaSample2 * This)
443 if (This->props.pMediaType)
444 DeleteMediaType(This->props.pMediaType);
446 /* NOTE: does not remove itself from the list it belongs to */
447 CoTaskMemFree(This);
450 static inline StdMediaSample2 *impl_from_IMediaSample2(IMediaSample2 * iface)
452 return CONTAINING_RECORD(iface, StdMediaSample2, IMediaSample2_iface);
455 static HRESULT WINAPI StdMediaSample2_QueryInterface(IMediaSample2 * iface, REFIID riid, void ** ppv)
457 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
459 *ppv = NULL;
461 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMediaSample) ||
462 IsEqualIID(riid, &IID_IMediaSample2))
464 *ppv = iface;
465 IMediaSample2_AddRef(iface);
466 return S_OK;
469 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
470 return E_NOINTERFACE;
473 static ULONG WINAPI StdMediaSample2_AddRef(IMediaSample2 * iface)
475 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
476 ULONG ref = InterlockedIncrement(&This->ref);
478 TRACE("%p increasing refcount to %lu.\n", This, ref);
480 return ref;
483 static ULONG WINAPI StdMediaSample2_Release(IMediaSample2 * iface)
485 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
486 ULONG ref = InterlockedDecrement(&This->ref);
488 TRACE("%p decreasing refcount to %lu.\n", This, ref);
490 if (!ref)
492 if (This->props.pMediaType)
493 DeleteMediaType(This->props.pMediaType);
494 This->props.pMediaType = NULL;
495 This->props.dwSampleFlags = 0;
496 This->media_time_valid = FALSE;
498 if (This->pParent)
499 IMemAllocator_ReleaseBuffer(This->pParent, (IMediaSample *)iface);
500 else
501 StdMediaSample2_Delete(This);
503 return ref;
506 static HRESULT WINAPI StdMediaSample2_GetPointer(IMediaSample2 * iface, BYTE ** ppBuffer)
508 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
510 TRACE("(%p)->(%p)\n", iface, ppBuffer);
512 *ppBuffer = This->props.pbBuffer;
514 if (!*ppBuffer)
516 ERR("Requested an unlocked surface and trying to lock regardless\n");
517 return E_FAIL;
520 return S_OK;
523 static LONG WINAPI StdMediaSample2_GetSize(IMediaSample2 * iface)
525 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
527 TRACE("StdMediaSample2_GetSize()\n");
529 return This->props.cbBuffer;
532 static HRESULT WINAPI StdMediaSample2_GetTime(IMediaSample2 * iface, REFERENCE_TIME * pStart, REFERENCE_TIME * pEnd)
534 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
535 HRESULT hr;
537 TRACE("(%p)->(%p, %p)\n", iface, pStart, pEnd);
539 if (!(This->props.dwSampleFlags & AM_SAMPLE_TIMEVALID))
540 hr = VFW_E_SAMPLE_TIME_NOT_SET;
541 else if (!(This->props.dwSampleFlags & AM_SAMPLE_STOPVALID))
543 *pStart = This->props.tStart;
544 *pEnd = This->props.tStart + 1;
546 hr = VFW_S_NO_STOP_TIME;
548 else
550 *pStart = This->props.tStart;
551 *pEnd = This->props.tStop;
553 hr = S_OK;
556 return hr;
559 static HRESULT WINAPI StdMediaSample2_SetTime(IMediaSample2 *iface, REFERENCE_TIME *start, REFERENCE_TIME *end)
561 StdMediaSample2 *sample = impl_from_IMediaSample2(iface);
563 TRACE("sample %p, start %s, end %s.\n", sample, start ? debugstr_time(*start) : "(null)",
564 end ? debugstr_time(*end) : "(null)");
566 if (start)
568 sample->props.tStart = *start;
569 sample->props.dwSampleFlags |= AM_SAMPLE_TIMEVALID;
571 if (end)
573 sample->props.tStop = *end;
574 sample->props.dwSampleFlags |= AM_SAMPLE_STOPVALID;
576 else
577 sample->props.dwSampleFlags &= ~AM_SAMPLE_STOPVALID;
579 else
580 sample->props.dwSampleFlags &= ~(AM_SAMPLE_TIMEVALID | AM_SAMPLE_STOPVALID);
582 return S_OK;
585 static HRESULT WINAPI StdMediaSample2_IsSyncPoint(IMediaSample2 * iface)
587 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
589 TRACE("(%p)->()\n", iface);
591 return (This->props.dwSampleFlags & AM_SAMPLE_SPLICEPOINT) ? S_OK : S_FALSE;
594 static HRESULT WINAPI StdMediaSample2_SetSyncPoint(IMediaSample2 * iface, BOOL bIsSyncPoint)
596 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
598 TRACE("(%p)->(%s)\n", iface, bIsSyncPoint ? "TRUE" : "FALSE");
600 if (bIsSyncPoint)
601 This->props.dwSampleFlags |= AM_SAMPLE_SPLICEPOINT;
602 else
603 This->props.dwSampleFlags &= ~AM_SAMPLE_SPLICEPOINT;
605 return S_OK;
608 static HRESULT WINAPI StdMediaSample2_IsPreroll(IMediaSample2 * iface)
610 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
612 TRACE("(%p)->()\n", iface);
614 return (This->props.dwSampleFlags & AM_SAMPLE_PREROLL) ? S_OK : S_FALSE;
617 static HRESULT WINAPI StdMediaSample2_SetPreroll(IMediaSample2 * iface, BOOL bIsPreroll)
619 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
621 TRACE("(%p)->(%s)\n", iface, bIsPreroll ? "TRUE" : "FALSE");
623 if (bIsPreroll)
624 This->props.dwSampleFlags |= AM_SAMPLE_PREROLL;
625 else
626 This->props.dwSampleFlags &= ~AM_SAMPLE_PREROLL;
628 return S_OK;
631 static LONG WINAPI StdMediaSample2_GetActualDataLength(IMediaSample2 * iface)
633 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
635 TRACE("(%p)->()\n", iface);
637 return This->props.lActual;
640 static HRESULT WINAPI StdMediaSample2_SetActualDataLength(IMediaSample2 * iface, LONG len)
642 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
644 TRACE("sample %p, len %ld.\n", This, len);
646 if ((len > This->props.cbBuffer) || (len < 0))
648 ERR("Length %ld exceeds maximum %ld.\n", len, This->props.cbBuffer);
649 return VFW_E_BUFFER_OVERFLOW;
651 else
653 This->props.lActual = len;
654 return S_OK;
658 static HRESULT WINAPI StdMediaSample2_GetMediaType(IMediaSample2 * iface, AM_MEDIA_TYPE ** ppMediaType)
660 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
662 TRACE("(%p)->(%p)\n", iface, ppMediaType);
664 if (!This->props.pMediaType) {
665 /* Make sure we return a NULL pointer (required by native Quartz dll) */
666 if (ppMediaType)
667 *ppMediaType = NULL;
668 return S_FALSE;
671 if (!(*ppMediaType = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE))))
672 return E_OUTOFMEMORY;
674 return CopyMediaType(*ppMediaType, This->props.pMediaType);
677 static HRESULT WINAPI StdMediaSample2_SetMediaType(IMediaSample2 * iface, AM_MEDIA_TYPE * pMediaType)
679 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
681 TRACE("(%p)->(%p)\n", iface, pMediaType);
683 if (This->props.pMediaType)
685 DeleteMediaType(This->props.pMediaType);
686 This->props.pMediaType = NULL;
689 if (!pMediaType)
691 This->props.dwSampleFlags &= ~AM_SAMPLE_TYPECHANGED;
692 return S_OK;
695 This->props.dwSampleFlags |= AM_SAMPLE_TYPECHANGED;
697 if (!(This->props.pMediaType = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE))))
698 return E_OUTOFMEMORY;
700 return CopyMediaType(This->props.pMediaType, pMediaType);
703 static HRESULT WINAPI StdMediaSample2_IsDiscontinuity(IMediaSample2 * iface)
705 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
707 TRACE("(%p)->()\n", iface);
709 return (This->props.dwSampleFlags & AM_SAMPLE_DATADISCONTINUITY) ? S_OK : S_FALSE;
712 static HRESULT WINAPI StdMediaSample2_SetDiscontinuity(IMediaSample2 * iface, BOOL bIsDiscontinuity)
714 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
716 TRACE("(%p)->(%s)\n", iface, bIsDiscontinuity ? "TRUE" : "FALSE");
718 if (bIsDiscontinuity)
719 This->props.dwSampleFlags |= AM_SAMPLE_DATADISCONTINUITY;
720 else
721 This->props.dwSampleFlags &= ~AM_SAMPLE_DATADISCONTINUITY;
723 return S_OK;
726 static HRESULT WINAPI StdMediaSample2_GetMediaTime(IMediaSample2 * iface, LONGLONG * pStart, LONGLONG * pEnd)
728 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
730 TRACE("(%p)->(%p, %p)\n", iface, pStart, pEnd);
732 if (!This->media_time_valid)
733 return VFW_E_MEDIA_TIME_NOT_SET;
735 *pStart = This->tMediaStart;
736 *pEnd = This->tMediaEnd;
738 return S_OK;
741 static HRESULT WINAPI StdMediaSample2_SetMediaTime(IMediaSample2 *iface, LONGLONG *start, LONGLONG *end)
743 StdMediaSample2 *sample = impl_from_IMediaSample2(iface);
745 TRACE("sample %p, start %s, end %s.\n", sample, start ? debugstr_time(*start) : "(null)",
746 end ? debugstr_time(*end) : "(null)");
748 if (start)
750 if (!end) return E_POINTER;
751 sample->tMediaStart = *start;
752 sample->tMediaEnd = *end;
753 sample->media_time_valid = TRUE;
755 else
756 sample->media_time_valid = FALSE;
758 return S_OK;
761 static HRESULT WINAPI StdMediaSample2_GetProperties(IMediaSample2 * iface, DWORD cbProperties, BYTE * pbProperties)
763 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
765 TRACE("sample %p, size %lu, properties %p.\n", This, cbProperties, pbProperties);
767 memcpy(pbProperties, &This->props, min(cbProperties, sizeof(This->props)));
769 return S_OK;
772 static HRESULT WINAPI StdMediaSample2_SetProperties(IMediaSample2 * iface, DWORD cbProperties, const BYTE * pbProperties)
774 StdMediaSample2 *This = impl_from_IMediaSample2(iface);
776 TRACE("sample %p, size %lu, properties %p.\n", This, cbProperties, pbProperties);
778 /* NOTE: pbBuffer and cbBuffer are read-only */
779 memcpy(&This->props, pbProperties, min(cbProperties, AM_SAMPLE2_PROP_SIZE_WRITABLE));
781 return S_OK;
784 static const IMediaSample2Vtbl StdMediaSample2_VTable =
786 StdMediaSample2_QueryInterface,
787 StdMediaSample2_AddRef,
788 StdMediaSample2_Release,
789 StdMediaSample2_GetPointer,
790 StdMediaSample2_GetSize,
791 StdMediaSample2_GetTime,
792 StdMediaSample2_SetTime,
793 StdMediaSample2_IsSyncPoint,
794 StdMediaSample2_SetSyncPoint,
795 StdMediaSample2_IsPreroll,
796 StdMediaSample2_SetPreroll,
797 StdMediaSample2_GetActualDataLength,
798 StdMediaSample2_SetActualDataLength,
799 StdMediaSample2_GetMediaType,
800 StdMediaSample2_SetMediaType,
801 StdMediaSample2_IsDiscontinuity,
802 StdMediaSample2_SetDiscontinuity,
803 StdMediaSample2_GetMediaTime,
804 StdMediaSample2_SetMediaTime,
805 StdMediaSample2_GetProperties,
806 StdMediaSample2_SetProperties
809 static inline StdMediaSample2 *unsafe_impl_from_IMediaSample(IMediaSample * iface)
811 IMediaSample2 *iface2 = (IMediaSample2 *)iface;
813 if (!iface)
814 return NULL;
815 assert(iface2->lpVtbl == &StdMediaSample2_VTable);
816 return impl_from_IMediaSample2(iface2);
819 typedef struct StdMemAllocator
821 BaseMemAllocator base;
822 CRITICAL_SECTION csState;
823 LPVOID pMemory;
824 } StdMemAllocator;
826 static inline StdMemAllocator *StdMemAllocator_from_IMemAllocator(IMemAllocator * iface)
828 return CONTAINING_RECORD(iface, StdMemAllocator, base.IMemAllocator_iface);
831 static HRESULT StdMemAllocator_Alloc(IMemAllocator * iface)
833 StdMemAllocator *This = StdMemAllocator_from_IMemAllocator(iface);
834 StdMediaSample2 * pSample = NULL;
835 SYSTEM_INFO si;
836 LONG i;
838 assert(list_empty(&This->base.free_list));
840 /* check alignment */
841 GetSystemInfo(&si);
843 /* we do not allow a courser alignment than the OS page size */
844 if ((si.dwPageSize % This->base.props.cbAlign) != 0)
845 return VFW_E_BADALIGN;
847 /* FIXME: each sample has to have its buffer start on the right alignment.
848 * We don't do this at the moment */
850 /* allocate memory */
851 This->pMemory = VirtualAlloc(NULL, (This->base.props.cbBuffer + This->base.props.cbPrefix) * This->base.props.cBuffers, MEM_COMMIT, PAGE_READWRITE);
853 if (!This->pMemory)
854 return E_OUTOFMEMORY;
856 for (i = This->base.props.cBuffers - 1; i >= 0; i--)
858 /* pbBuffer does not start at the base address, it starts at base + cbPrefix */
859 BYTE * pbBuffer = (BYTE *)This->pMemory + i * (This->base.props.cbBuffer + This->base.props.cbPrefix) + This->base.props.cbPrefix;
861 StdMediaSample2_Construct(pbBuffer, This->base.props.cbBuffer, iface, &pSample);
863 list_add_head(&This->base.free_list, &pSample->listentry);
866 return S_OK;
869 static HRESULT StdMemAllocator_Free(IMemAllocator * iface)
871 StdMemAllocator *This = StdMemAllocator_from_IMemAllocator(iface);
872 struct list * cursor;
874 if (!list_empty(&This->base.used_list))
876 WARN("Freeing allocator with outstanding samples!\n");
877 while ((cursor = list_head(&This->base.used_list)) != NULL)
879 StdMediaSample2 *pSample;
880 list_remove(cursor);
881 pSample = LIST_ENTRY(cursor, StdMediaSample2, listentry);
882 pSample->pParent = NULL;
886 while ((cursor = list_head(&This->base.free_list)) != NULL)
888 list_remove(cursor);
889 StdMediaSample2_Delete(LIST_ENTRY(cursor, StdMediaSample2, listentry));
892 /* free memory */
893 if (!VirtualFree(This->pMemory, 0, MEM_RELEASE))
895 ERR("Failed to free memory, error %lu.\n", GetLastError());
896 return HRESULT_FROM_WIN32(GetLastError());
899 return S_OK;
902 static void StdMemAllocator_Destroy(IMemAllocator *iface)
904 StdMemAllocator *This = StdMemAllocator_from_IMemAllocator(iface);
906 This->csState.DebugInfo->Spare[0] = 0;
907 DeleteCriticalSection(&This->csState);
909 CoTaskMemFree(This);
912 HRESULT mem_allocator_create(IUnknown *lpUnkOuter, IUnknown **out)
914 StdMemAllocator * pMemAlloc;
915 HRESULT hr;
917 if (lpUnkOuter)
918 return CLASS_E_NOAGGREGATION;
920 if (!(pMemAlloc = CoTaskMemAlloc(sizeof(*pMemAlloc))))
921 return E_OUTOFMEMORY;
923 InitializeCriticalSectionEx(&pMemAlloc->csState, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO);
924 pMemAlloc->csState.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": StdMemAllocator.csState");
926 pMemAlloc->pMemory = NULL;
928 if (SUCCEEDED(hr = BaseMemAllocator_Init(StdMemAllocator_Alloc, StdMemAllocator_Free, NULL, NULL, NULL, StdMemAllocator_Destroy, &pMemAlloc->csState, &pMemAlloc->base)))
929 *out = (IUnknown *)&pMemAlloc->base.IMemAllocator_iface;
930 else
931 CoTaskMemFree(pMemAlloc);
933 return hr;