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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "quartz_private.h"
30 #include "wine/list.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
35 void dump_AM_SAMPLE2_PROPERTIES(AM_SAMPLE2_PROPERTIES
* pProps
)
39 TRACE("AM_SAMPLE2_PROPERTIES: (null)\n");
42 TRACE("\tcbData: %ld\n", pProps
->cbData
);
43 TRACE("\tdwTypeSpecificFlags: 0x%8lx\n", pProps
->dwTypeSpecificFlags
);
44 TRACE("\tdwSampleFlags: 0x%8lx\n", pProps
->dwSampleFlags
);
45 TRACE("\tlActual: %ld\n", pProps
->lActual
);
46 TRACE("\ttStart: %lx%08lx%s\n", (LONG
)(pProps
->tStart
>> 32), (LONG
)pProps
->tStart
, pProps
->dwSampleFlags
& AM_SAMPLE_TIMEVALID
? "" : " (not valid)");
47 TRACE("\ttStop: %lx%08lx%s\n", (LONG
)(pProps
->tStop
>> 32), (LONG
)pProps
->tStop
, pProps
->dwSampleFlags
& AM_SAMPLE_STOPVALID
? "" : " (not valid)");
48 TRACE("\tdwStreamId: 0x%lx\n", pProps
->dwStreamId
);
49 TRACE("\tpMediaType: %p\n", pProps
->pMediaType
);
50 TRACE("\tpbBuffer: %p\n", pProps
->pbBuffer
);
51 TRACE("\tcbBuffer: %ld\n", pProps
->cbBuffer
);
54 typedef struct BaseMemAllocator
56 const IMemAllocatorVtbl
* lpVtbl
;
59 ALLOCATOR_PROPERTIES
* pProps
;
60 CRITICAL_SECTION csState
;
61 HRESULT (* fnAlloc
) (IMemAllocator
*);
62 HRESULT (* fnFree
)(IMemAllocator
*);
67 struct list free_list
;
68 struct list used_list
;
71 typedef struct StdMediaSample2
73 const IMediaSample2Vtbl
* lpvtbl
;
76 AM_SAMPLE2_PROPERTIES props
;
77 IMemAllocator
* pParent
;
78 struct list listentry
;
83 static const struct IMemAllocatorVtbl BaseMemAllocator_VTable
;
84 static const struct IMediaSample2Vtbl StdMediaSample2_VTable
;
86 #define AM_SAMPLE2_PROP_SIZE_WRITABLE (unsigned int)(&((AM_SAMPLE2_PROPERTIES *)0)->pbBuffer)
89 #define INVALID_MEDIA_TIME _I64_MAX
91 #define INVALID_MEDIA_TIME ((long long)(~0ull >> 1))
94 static HRESULT
BaseMemAllocator_Init(HRESULT (* fnAlloc
)(IMemAllocator
*), HRESULT (* fnFree
)(IMemAllocator
*), BaseMemAllocator
* pMemAlloc
)
96 assert(fnAlloc
&& fnFree
);
98 pMemAlloc
->lpVtbl
= &BaseMemAllocator_VTable
;
101 pMemAlloc
->pProps
= NULL
;
102 list_init(&pMemAlloc
->free_list
);
103 list_init(&pMemAlloc
->used_list
);
104 pMemAlloc
->fnAlloc
= fnAlloc
;
105 pMemAlloc
->fnFree
= fnFree
;
106 pMemAlloc
->bDecommitQueued
= FALSE
;
107 pMemAlloc
->bCommitted
= FALSE
;
108 pMemAlloc
->hSemWaiting
= NULL
;
109 pMemAlloc
->lWaiting
= 0;
111 InitializeCriticalSection(&pMemAlloc
->csState
);
116 static HRESULT WINAPI
BaseMemAllocator_QueryInterface(IMemAllocator
* iface
, REFIID riid
, LPVOID
* ppv
)
118 ICOM_THIS(BaseMemAllocator
, iface
);
119 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
123 if (IsEqualIID(riid
, &IID_IUnknown
))
125 else if (IsEqualIID(riid
, &IID_IMemAllocator
))
130 IUnknown_AddRef((IUnknown
*)(*ppv
));
134 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
136 return E_NOINTERFACE
;
139 static ULONG WINAPI
BaseMemAllocator_AddRef(IMemAllocator
* iface
)
141 ICOM_THIS(BaseMemAllocator
, iface
);
145 return InterlockedIncrement(&This
->ref
);
148 static ULONG WINAPI
BaseMemAllocator_Release(IMemAllocator
* iface
)
150 ICOM_THIS(BaseMemAllocator
, iface
);
154 if (!InterlockedDecrement(&This
->ref
))
156 CloseHandle(This
->hSemWaiting
);
157 if (This
->bCommitted
)
160 HeapFree(GetProcessHeap(), 0, This
->pProps
);
167 static HRESULT WINAPI
BaseMemAllocator_SetProperties(IMemAllocator
* iface
, ALLOCATOR_PROPERTIES
*pRequest
, ALLOCATOR_PROPERTIES
*pActual
)
169 ICOM_THIS(BaseMemAllocator
, iface
);
172 TRACE("(%p, %p)\n", pRequest
, pActual
);
174 EnterCriticalSection(&This
->csState
);
176 if (!list_empty(&This
->used_list
))
177 hr
= VFW_E_BUFFERS_OUTSTANDING
;
178 else if (This
->bCommitted
)
179 hr
= VFW_E_ALREADY_COMMITTED
;
183 This
->pProps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->pProps
));
189 memcpy(This
->pProps
, pRequest
, sizeof(*This
->pProps
));
191 memcpy(pActual
, pRequest
, sizeof(*pActual
));
197 LeaveCriticalSection(&This
->csState
);
202 static HRESULT WINAPI
BaseMemAllocator_GetProperties(IMemAllocator
* iface
, ALLOCATOR_PROPERTIES
*pProps
)
204 ICOM_THIS(BaseMemAllocator
, iface
);
207 TRACE("(%p)\n", pProps
);
209 EnterCriticalSection(&This
->csState
);
211 /* NOTE: this is different from the native version.
212 * It would silently succeed if the properties had
213 * not been set, but would fail further on down the
214 * line with some obscure error like having an
215 * invalid alignment. Whether or not our version
216 * will cause any problems remains to be seen */
218 hr
= VFW_E_SIZENOTSET
;
220 memcpy(pProps
, This
->pProps
, sizeof(*pProps
));
222 LeaveCriticalSection(&This
->csState
);
227 static HRESULT WINAPI
BaseMemAllocator_Commit(IMemAllocator
* iface
)
229 ICOM_THIS(BaseMemAllocator
, iface
);
234 EnterCriticalSection(&This
->csState
);
237 hr
= VFW_E_SIZENOTSET
;
238 else if (This
->bCommitted
)
239 hr
= VFW_E_ALREADY_COMMITTED
;
240 else if (This
->bDecommitQueued
)
242 This
->bDecommitQueued
= FALSE
;
247 if (!(This
->hSemWaiting
= CreateSemaphoreW(NULL
, This
->pProps
->cBuffers
, This
->pProps
->cBuffers
, NULL
)))
249 ERR("Couldn't create semaphore (error was %ld)\n", GetLastError());
250 hr
= HRESULT_FROM_WIN32(GetLastError());
254 hr
= This
->fnAlloc(iface
);
256 This
->bCommitted
= TRUE
;
258 ERR("fnAlloc failed with error 0x%lx\n", hr
);
262 LeaveCriticalSection(&This
->csState
);
267 static HRESULT WINAPI
BaseMemAllocator_Decommit(IMemAllocator
* iface
)
269 ICOM_THIS(BaseMemAllocator
, iface
);
274 EnterCriticalSection(&This
->csState
);
276 if (!This
->bCommitted
)
277 hr
= VFW_E_NOT_COMMITTED
;
280 if (!list_empty(&This
->used_list
))
282 This
->bDecommitQueued
= TRUE
;
283 /* notify ALL waiting threads that they cannot be allocated a buffer any more */
284 ReleaseSemaphore(This
->hSemWaiting
, This
->lWaiting
, NULL
);
290 assert(This
->lWaiting
== 0);
292 This
->bCommitted
= FALSE
;
293 CloseHandle(This
->hSemWaiting
);
294 This
->hSemWaiting
= NULL
;
296 hr
= This
->fnFree(iface
);
298 ERR("fnFree failed with error 0x%lx\n", hr
);
302 LeaveCriticalSection(&This
->csState
);
307 static HRESULT WINAPI
BaseMemAllocator_GetBuffer(IMemAllocator
* iface
, IMediaSample
** pSample
, REFERENCE_TIME
*pStartTime
, REFERENCE_TIME
*pEndTime
, DWORD dwFlags
)
309 ICOM_THIS(BaseMemAllocator
, iface
);
312 /* NOTE: The pStartTime and pEndTime parameters are not applied to the sample.
313 * The allocator might use these values to determine which buffer it retrieves */
315 TRACE("(%p, %p, %p, %lx)\n", pSample
, pStartTime
, pEndTime
, dwFlags
);
319 if (!This
->bCommitted
)
320 return VFW_E_NOT_COMMITTED
;
323 if (WaitForSingleObject(This
->hSemWaiting
, (dwFlags
& AM_GBF_NOWAIT
) ? 0 : INFINITE
) != WAIT_OBJECT_0
)
326 return VFW_E_TIMEOUT
;
330 EnterCriticalSection(&This
->csState
);
332 if (!This
->bCommitted
)
333 hr
= VFW_E_NOT_COMMITTED
;
334 else if (This
->bDecommitQueued
)
338 struct list
* free
= list_head(&This
->free_list
);
340 list_add_head(&This
->used_list
, free
);
342 *pSample
= (IMediaSample
*)LIST_ENTRY(free
, StdMediaSample2
, listentry
);
344 assert(((StdMediaSample2
*)*pSample
)->ref
== 0);
346 IMediaSample_AddRef(*pSample
);
349 LeaveCriticalSection(&This
->csState
);
354 static HRESULT WINAPI
BaseMemAllocator_ReleaseBuffer(IMemAllocator
* iface
, IMediaSample
* pSample
)
356 ICOM_THIS(BaseMemAllocator
, iface
);
357 StdMediaSample2
* pStdSample
= (StdMediaSample2
*)pSample
;
360 TRACE("(%p)\n", pSample
);
362 /* FIXME: make sure that sample is currently on the used list */
364 /* FIXME: we should probably check the ref count on the sample before freeing
365 * it to make sure that it is not still in use */
366 EnterCriticalSection(&This
->csState
);
368 if (!This
->bCommitted
)
369 ERR("Releasing a buffer when the allocator is not committed?!?\n");
371 /* remove from used_list */
372 list_remove(&pStdSample
->listentry
);
374 list_add_head(&This
->free_list
, &pStdSample
->listentry
);
376 if (list_empty(&This
->used_list
) && This
->bDecommitQueued
&& This
->bCommitted
)
380 assert(This
->lWaiting
== 0);
382 This
->bCommitted
= FALSE
;
383 This
->bDecommitQueued
= FALSE
;
385 CloseHandle(This
->hSemWaiting
);
386 This
->hSemWaiting
= NULL
;
388 if (FAILED(hrfree
= This
->fnFree(iface
)))
389 ERR("fnFree failed with error 0x%lx\n", hrfree
);
392 LeaveCriticalSection(&This
->csState
);
394 /* notify a waiting thread that there is now a free buffer */
395 if (!ReleaseSemaphore(This
->hSemWaiting
, 1, NULL
))
397 ERR("ReleaseSemaphore failed with error %ld\n", GetLastError());
398 hr
= HRESULT_FROM_WIN32(GetLastError());
404 static const IMemAllocatorVtbl BaseMemAllocator_VTable
=
406 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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
)->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
)->tMediaStart
= INVALID_MEDIA_TIME
;
437 (*ppSample
)->tMediaEnd
= 0;
442 static void StdMediaSample2_Delete(StdMediaSample2
* This
)
444 /* NOTE: does not remove itself from the list it belongs to */
448 static HRESULT WINAPI
StdMediaSample2_QueryInterface(IMediaSample2
* iface
, REFIID riid
, LPVOID
* ppv
)
450 ICOM_THIS(StdMediaSample2
, iface
);
451 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
455 if (IsEqualIID(riid
, &IID_IUnknown
))
457 else if (IsEqualIID(riid
, &IID_IMediaSample
))
459 else if (IsEqualIID(riid
, &IID_IMediaSample2
))
464 IUnknown_AddRef((IUnknown
*)(*ppv
));
468 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
470 return E_NOINTERFACE
;
473 static ULONG WINAPI
StdMediaSample2_AddRef(IMediaSample2
* iface
)
475 ICOM_THIS(StdMediaSample2
, iface
);
479 return InterlockedIncrement(&This
->ref
);
482 static ULONG WINAPI
StdMediaSample2_Release(IMediaSample2
* iface
)
484 ICOM_THIS(StdMediaSample2
, iface
);
488 if (!InterlockedDecrement(&This
->ref
))
490 IMemAllocator_ReleaseBuffer(This
->pParent
, (IMediaSample
*)iface
);
496 static HRESULT WINAPI
StdMediaSample2_GetPointer(IMediaSample2
* iface
, BYTE
** ppBuffer
)
498 ICOM_THIS(StdMediaSample2
, iface
);
500 TRACE("(%p)\n", ppBuffer
);
502 *ppBuffer
= This
->props
.pbBuffer
;
507 static long WINAPI
StdMediaSample2_GetSize(IMediaSample2
* iface
)
509 ICOM_THIS(StdMediaSample2
, iface
);
511 TRACE("StdMediaSample2_GetSize()\n");
513 return This
->props
.cbBuffer
;
516 static HRESULT WINAPI
StdMediaSample2_GetTime(IMediaSample2
* iface
, REFERENCE_TIME
* pStart
, REFERENCE_TIME
* pEnd
)
519 ICOM_THIS(StdMediaSample2
, iface
);
521 TRACE("(%p, %p)\n", pStart
, pEnd
);
523 if (!(This
->props
.dwSampleFlags
& AM_SAMPLE_TIMEVALID
))
524 hr
= VFW_E_SAMPLE_TIME_NOT_SET
;
525 else if (!(This
->props
.dwSampleFlags
& AM_SAMPLE_STOPVALID
))
527 *pStart
= This
->props
.tStart
;
528 *pEnd
= This
->props
.tStart
+ 1;
530 hr
= VFW_S_NO_STOP_TIME
;
534 *pStart
= This
->props
.tStart
;
535 *pEnd
= This
->props
.tStop
;
543 static HRESULT WINAPI
StdMediaSample2_SetTime(IMediaSample2
* iface
, REFERENCE_TIME
* pStart
, REFERENCE_TIME
* pEnd
)
545 ICOM_THIS(StdMediaSample2
, iface
);
547 TRACE("(%p, %p)\n", pStart
, pEnd
);
551 This
->props
.tStart
= *pStart
;
552 This
->props
.dwSampleFlags
|= AM_SAMPLE_TIMEVALID
;
555 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_TIMEVALID
;
559 This
->props
.tStop
= *pEnd
;
560 This
->props
.dwSampleFlags
|= AM_SAMPLE_STOPVALID
;
563 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_STOPVALID
;
568 static HRESULT WINAPI
StdMediaSample2_IsSyncPoint(IMediaSample2
* iface
)
570 ICOM_THIS(StdMediaSample2
, iface
);
574 return (This
->props
.dwSampleFlags
& AM_SAMPLE_SPLICEPOINT
) ? S_OK
: S_FALSE
;
577 static HRESULT WINAPI
StdMediaSample2_SetSyncPoint(IMediaSample2
* iface
, BOOL bIsSyncPoint
)
579 ICOM_THIS(StdMediaSample2
, iface
);
581 TRACE("(%s)\n", bIsSyncPoint
? "TRUE" : "FALSE");
583 This
->props
.dwSampleFlags
= (This
->props
.dwSampleFlags
& ~AM_SAMPLE_SPLICEPOINT
) | bIsSyncPoint
? AM_SAMPLE_SPLICEPOINT
: 0;
588 static HRESULT WINAPI
StdMediaSample2_IsPreroll(IMediaSample2
* iface
)
590 ICOM_THIS(StdMediaSample2
, iface
);
594 return (This
->props
.dwSampleFlags
& AM_SAMPLE_PREROLL
) ? S_OK
: S_FALSE
;
597 static HRESULT WINAPI
StdMediaSample2_SetPreroll(IMediaSample2
* iface
, BOOL bIsPreroll
)
599 ICOM_THIS(StdMediaSample2
, iface
);
601 TRACE("(%s)\n", bIsPreroll
? "TRUE" : "FALSE");
603 This
->props
.dwSampleFlags
= (This
->props
.dwSampleFlags
& ~AM_SAMPLE_PREROLL
) | bIsPreroll
? AM_SAMPLE_PREROLL
: 0;
608 static LONG WINAPI
StdMediaSample2_GetActualDataLength(IMediaSample2
* iface
)
610 ICOM_THIS(StdMediaSample2
, iface
);
614 return This
->props
.lActual
;
617 static HRESULT WINAPI
StdMediaSample2_SetActualDataLength(IMediaSample2
* iface
, LONG len
)
619 ICOM_THIS(StdMediaSample2
, iface
);
621 TRACE("(%ld)\n", len
);
623 if ((len
> This
->props
.cbBuffer
) || (len
< 0))
624 return VFW_E_BUFFER_OVERFLOW
;
627 This
->props
.lActual
= len
;
632 static HRESULT WINAPI
StdMediaSample2_GetMediaType(IMediaSample2
* iface
, AM_MEDIA_TYPE
** ppMediaType
)
634 ICOM_THIS(StdMediaSample2
, iface
);
636 TRACE("(%p)\n", ppMediaType
);
638 if (!This
->props
.pMediaType
)
641 if (!(*ppMediaType
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
))))
642 return E_OUTOFMEMORY
;
644 return CopyMediaType(*ppMediaType
, This
->props
.pMediaType
);
647 static HRESULT WINAPI
StdMediaSample2_SetMediaType(IMediaSample2
* iface
, AM_MEDIA_TYPE
* pMediaType
)
649 ICOM_THIS(StdMediaSample2
, iface
);
651 TRACE("(%p)\n", pMediaType
);
653 if (This
->props
.pMediaType
)
654 DeleteMediaType(This
->props
.pMediaType
);
655 else if (!(This
->props
.pMediaType
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
))))
656 return E_OUTOFMEMORY
;
658 return CopyMediaType(This
->props
.pMediaType
, pMediaType
);
661 static HRESULT WINAPI
StdMediaSample2_IsDiscontinuity(IMediaSample2
* iface
)
663 ICOM_THIS(StdMediaSample2
, iface
);
667 return (This
->props
.dwSampleFlags
& AM_SAMPLE_DATADISCONTINUITY
) ? S_OK
: S_FALSE
;
670 static HRESULT WINAPI
StdMediaSample2_SetDiscontinuity(IMediaSample2
* iface
, BOOL bIsDiscontinuity
)
672 ICOM_THIS(StdMediaSample2
, iface
);
674 TRACE("(%s)\n", bIsDiscontinuity
? "TRUE" : "FALSE");
676 This
->props
.dwSampleFlags
= (This
->props
.dwSampleFlags
& ~AM_SAMPLE_DATADISCONTINUITY
) | bIsDiscontinuity
? AM_SAMPLE_DATADISCONTINUITY
: 0;
681 static HRESULT WINAPI
StdMediaSample2_GetMediaTime(IMediaSample2
* iface
, LONGLONG
* pStart
, LONGLONG
* pEnd
)
683 ICOM_THIS(StdMediaSample2
, iface
);
685 TRACE("(%p, %p)\n", pStart
, pEnd
);
687 if (This
->tMediaStart
== INVALID_MEDIA_TIME
)
688 return VFW_E_MEDIA_TIME_NOT_SET
;
690 *pStart
= This
->tMediaStart
;
691 *pEnd
= This
->tMediaEnd
;
696 static HRESULT WINAPI
StdMediaSample2_SetMediaTime(IMediaSample2
* iface
, LONGLONG
* pStart
, LONGLONG
* pEnd
)
698 ICOM_THIS(StdMediaSample2
, iface
);
700 TRACE("(%p, %p)\n", pStart
, pEnd
);
703 This
->tMediaStart
= *pStart
;
705 This
->tMediaStart
= INVALID_MEDIA_TIME
;
708 This
->tMediaEnd
= *pEnd
;
715 static HRESULT WINAPI
StdMediaSample2_GetProperties(IMediaSample2
* iface
, DWORD cbProperties
, BYTE
* pbProperties
)
717 ICOM_THIS(StdMediaSample2
, iface
);
719 TRACE("(%ld, %p)\n", cbProperties
, pbProperties
);
721 memcpy(pbProperties
, &This
->props
, min(cbProperties
, sizeof(This
->props
)));
726 static HRESULT WINAPI
StdMediaSample2_SetProperties(IMediaSample2
* iface
, DWORD cbProperties
, const BYTE
* pbProperties
)
728 ICOM_THIS(StdMediaSample2
, iface
);
730 TRACE("(%ld, %p)\n", cbProperties
, pbProperties
);
732 /* NOTE: pbBuffer and cbBuffer are read-only */
733 memcpy(&This
->props
, pbProperties
, min(cbProperties
, AM_SAMPLE2_PROP_SIZE_WRITABLE
));
738 static const IMediaSample2Vtbl StdMediaSample2_VTable
=
740 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
741 StdMediaSample2_QueryInterface
,
742 StdMediaSample2_AddRef
,
743 StdMediaSample2_Release
,
744 StdMediaSample2_GetPointer
,
745 StdMediaSample2_GetSize
,
746 StdMediaSample2_GetTime
,
747 StdMediaSample2_SetTime
,
748 StdMediaSample2_IsSyncPoint
,
749 StdMediaSample2_SetSyncPoint
,
750 StdMediaSample2_IsPreroll
,
751 StdMediaSample2_SetPreroll
,
752 StdMediaSample2_GetActualDataLength
,
753 StdMediaSample2_SetActualDataLength
,
754 StdMediaSample2_GetMediaType
,
755 StdMediaSample2_SetMediaType
,
756 StdMediaSample2_IsDiscontinuity
,
757 StdMediaSample2_SetDiscontinuity
,
758 StdMediaSample2_GetMediaTime
,
759 StdMediaSample2_SetMediaTime
,
760 StdMediaSample2_GetProperties
,
761 StdMediaSample2_SetProperties
764 typedef struct StdMemAllocator
766 BaseMemAllocator base
;
770 static HRESULT
StdMemAllocator_Alloc(IMemAllocator
* iface
)
772 ICOM_THIS(StdMemAllocator
, iface
);
773 StdMediaSample2
* pSample
= NULL
;
777 assert(list_empty(&This
->base
.free_list
));
779 /* check alignment */
782 /* we do not allow a courser alignment than the OS page size */
783 if ((si
.dwPageSize
% This
->base
.pProps
->cbAlign
) != 0)
784 return VFW_E_BADALIGN
;
786 /* FIXME: each sample has to have its buffer start on the right alignment.
787 * We don't do this at the moment */
789 /* allocate memory */
790 This
->pMemory
= VirtualAlloc(NULL
, (This
->base
.pProps
->cbBuffer
+ This
->base
.pProps
->cbPrefix
) * This
->base
.pProps
->cBuffers
, MEM_COMMIT
, PAGE_READWRITE
);
792 for (i
= This
->base
.pProps
->cBuffers
- 1; i
>= 0; i
--)
794 /* pbBuffer does not start at the base address, it starts at base + cbPrefix */
795 BYTE
* pbBuffer
= (BYTE
*)This
->pMemory
+ i
* (This
->base
.pProps
->cbBuffer
+ This
->base
.pProps
->cbPrefix
) + This
->base
.pProps
->cbPrefix
;
797 StdMediaSample2_Construct(pbBuffer
, This
->base
.pProps
->cbBuffer
, iface
, &pSample
);
799 list_add_head(&This
->base
.free_list
, &pSample
->listentry
);
805 static HRESULT
StdMemAllocator_Free(IMemAllocator
* iface
)
807 ICOM_THIS(StdMemAllocator
, iface
);
808 struct list
* cursor
;
810 assert(list_empty(&This
->base
.used_list
));
812 while ((cursor
= list_head(&This
->base
.free_list
)) != NULL
)
815 StdMediaSample2_Delete(LIST_ENTRY(cursor
, StdMediaSample2
, listentry
));
819 if (!VirtualFree(This
->pMemory
, 0, MEM_RELEASE
))
821 ERR("Couldn't free memory. Error: %ld\n", GetLastError());
822 return HRESULT_FROM_WIN32(GetLastError());
828 HRESULT
StdMemAllocator_create(LPUNKNOWN lpUnkOuter
, LPVOID
* ppv
)
830 StdMemAllocator
* pMemAlloc
;
836 return CLASS_E_NOAGGREGATION
;
838 if (!(pMemAlloc
= CoTaskMemAlloc(sizeof(*pMemAlloc
))))
839 return E_OUTOFMEMORY
;
841 pMemAlloc
->pMemory
= NULL
;
843 if (SUCCEEDED(hr
= BaseMemAllocator_Init(StdMemAllocator_Alloc
, StdMemAllocator_Free
, &pMemAlloc
->base
)))
844 *ppv
= (LPVOID
)pMemAlloc
;
846 CoTaskMemFree(pMemAlloc
);