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 BaseMemAllocator
*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 BaseMemAllocator
*This
= (BaseMemAllocator
*)iface
;
143 TRACE("(%p)->() AddRef from %ld\n", iface
, This
->ref
);
145 return InterlockedIncrement(&This
->ref
);
148 static ULONG WINAPI
BaseMemAllocator_Release(IMemAllocator
* iface
)
150 BaseMemAllocator
*This
= (BaseMemAllocator
*)iface
;
152 TRACE("(%p)->() Release from %ld\n", iface
, This
->ref
);
154 if (!InterlockedDecrement(&This
->ref
))
156 CloseHandle(This
->hSemWaiting
);
157 if (This
->bCommitted
)
159 HeapFree(GetProcessHeap(), 0, This
->pProps
);
166 static HRESULT WINAPI
BaseMemAllocator_SetProperties(IMemAllocator
* iface
, ALLOCATOR_PROPERTIES
*pRequest
, ALLOCATOR_PROPERTIES
*pActual
)
168 BaseMemAllocator
*This
= (BaseMemAllocator
*)iface
;
171 TRACE("(%p, %p)\n", pRequest
, pActual
);
173 EnterCriticalSection(&This
->csState
);
175 if (!list_empty(&This
->used_list
))
176 hr
= VFW_E_BUFFERS_OUTSTANDING
;
177 else if (This
->bCommitted
)
178 hr
= VFW_E_ALREADY_COMMITTED
;
182 This
->pProps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->pProps
));
188 memcpy(This
->pProps
, pRequest
, sizeof(*This
->pProps
));
190 memcpy(pActual
, pRequest
, sizeof(*pActual
));
196 LeaveCriticalSection(&This
->csState
);
201 static HRESULT WINAPI
BaseMemAllocator_GetProperties(IMemAllocator
* iface
, ALLOCATOR_PROPERTIES
*pProps
)
203 BaseMemAllocator
*This
= (BaseMemAllocator
*)iface
;
206 TRACE("(%p)\n", pProps
);
208 EnterCriticalSection(&This
->csState
);
210 /* NOTE: this is different from the native version.
211 * It would silently succeed if the properties had
212 * not been set, but would fail further on down the
213 * line with some obscure error like having an
214 * invalid alignment. Whether or not our version
215 * will cause any problems remains to be seen */
217 hr
= VFW_E_SIZENOTSET
;
219 memcpy(pProps
, This
->pProps
, sizeof(*pProps
));
221 LeaveCriticalSection(&This
->csState
);
226 static HRESULT WINAPI
BaseMemAllocator_Commit(IMemAllocator
* iface
)
228 BaseMemAllocator
*This
= (BaseMemAllocator
*)iface
;
233 EnterCriticalSection(&This
->csState
);
236 hr
= VFW_E_SIZENOTSET
;
237 else if (This
->bCommitted
)
238 hr
= VFW_E_ALREADY_COMMITTED
;
239 else if (This
->bDecommitQueued
)
241 This
->bDecommitQueued
= FALSE
;
246 if (!(This
->hSemWaiting
= CreateSemaphoreW(NULL
, This
->pProps
->cBuffers
, This
->pProps
->cBuffers
, NULL
)))
248 ERR("Couldn't create semaphore (error was %ld)\n", GetLastError());
249 hr
= HRESULT_FROM_WIN32(GetLastError());
253 hr
= This
->fnAlloc(iface
);
255 This
->bCommitted
= TRUE
;
257 ERR("fnAlloc failed with error 0x%lx\n", hr
);
261 LeaveCriticalSection(&This
->csState
);
266 static HRESULT WINAPI
BaseMemAllocator_Decommit(IMemAllocator
* iface
)
268 BaseMemAllocator
*This
= (BaseMemAllocator
*)iface
;
273 EnterCriticalSection(&This
->csState
);
275 if (!This
->bCommitted
)
276 hr
= VFW_E_NOT_COMMITTED
;
279 if (!list_empty(&This
->used_list
))
281 This
->bDecommitQueued
= TRUE
;
282 /* notify ALL waiting threads that they cannot be allocated a buffer any more */
283 ReleaseSemaphore(This
->hSemWaiting
, This
->lWaiting
, NULL
);
289 assert(This
->lWaiting
== 0);
291 This
->bCommitted
= FALSE
;
292 CloseHandle(This
->hSemWaiting
);
293 This
->hSemWaiting
= NULL
;
295 hr
= This
->fnFree(iface
);
297 ERR("fnFree failed with error 0x%lx\n", hr
);
301 LeaveCriticalSection(&This
->csState
);
306 static HRESULT WINAPI
BaseMemAllocator_GetBuffer(IMemAllocator
* iface
, IMediaSample
** pSample
, REFERENCE_TIME
*pStartTime
, REFERENCE_TIME
*pEndTime
, DWORD dwFlags
)
308 BaseMemAllocator
*This
= (BaseMemAllocator
*)iface
;
311 /* NOTE: The pStartTime and pEndTime parameters are not applied to the sample.
312 * The allocator might use these values to determine which buffer it retrieves */
314 TRACE("(%p, %p, %p, %lx)\n", pSample
, pStartTime
, pEndTime
, dwFlags
);
318 if (!This
->bCommitted
)
319 return VFW_E_NOT_COMMITTED
;
322 if (WaitForSingleObject(This
->hSemWaiting
, (dwFlags
& AM_GBF_NOWAIT
) ? 0 : INFINITE
) != WAIT_OBJECT_0
)
325 return VFW_E_TIMEOUT
;
329 EnterCriticalSection(&This
->csState
);
331 if (!This
->bCommitted
)
332 hr
= VFW_E_NOT_COMMITTED
;
333 else if (This
->bDecommitQueued
)
337 struct list
* free
= list_head(&This
->free_list
);
339 list_add_head(&This
->used_list
, free
);
341 *pSample
= (IMediaSample
*)LIST_ENTRY(free
, StdMediaSample2
, listentry
);
343 assert(((StdMediaSample2
*)*pSample
)->ref
== 0);
345 IMediaSample_AddRef(*pSample
);
348 LeaveCriticalSection(&This
->csState
);
353 static HRESULT WINAPI
BaseMemAllocator_ReleaseBuffer(IMemAllocator
* iface
, IMediaSample
* pSample
)
355 BaseMemAllocator
*This
= (BaseMemAllocator
*)iface
;
356 StdMediaSample2
* pStdSample
= (StdMediaSample2
*)pSample
;
359 TRACE("(%p)\n", pSample
);
361 /* FIXME: make sure that sample is currently on the used list */
363 /* FIXME: we should probably check the ref count on the sample before freeing
364 * it to make sure that it is not still in use */
365 EnterCriticalSection(&This
->csState
);
367 if (!This
->bCommitted
)
368 ERR("Releasing a buffer when the allocator is not committed?!?\n");
370 /* remove from used_list */
371 list_remove(&pStdSample
->listentry
);
373 list_add_head(&This
->free_list
, &pStdSample
->listentry
);
375 if (list_empty(&This
->used_list
) && This
->bDecommitQueued
&& This
->bCommitted
)
379 assert(This
->lWaiting
== 0);
381 This
->bCommitted
= FALSE
;
382 This
->bDecommitQueued
= FALSE
;
384 CloseHandle(This
->hSemWaiting
);
385 This
->hSemWaiting
= NULL
;
387 if (FAILED(hrfree
= This
->fnFree(iface
)))
388 ERR("fnFree failed with error 0x%lx\n", hrfree
);
391 LeaveCriticalSection(&This
->csState
);
393 /* notify a waiting thread that there is now a free buffer */
394 if (!ReleaseSemaphore(This
->hSemWaiting
, 1, NULL
))
396 ERR("ReleaseSemaphore failed with error %ld\n", GetLastError());
397 hr
= HRESULT_FROM_WIN32(GetLastError());
403 static const IMemAllocatorVtbl BaseMemAllocator_VTable
=
405 BaseMemAllocator_QueryInterface
,
406 BaseMemAllocator_AddRef
,
407 BaseMemAllocator_Release
,
408 BaseMemAllocator_SetProperties
,
409 BaseMemAllocator_GetProperties
,
410 BaseMemAllocator_Commit
,
411 BaseMemAllocator_Decommit
,
412 BaseMemAllocator_GetBuffer
,
413 BaseMemAllocator_ReleaseBuffer
416 static HRESULT
StdMediaSample2_Construct(BYTE
* pbBuffer
, LONG cbBuffer
, IMemAllocator
* pParent
, StdMediaSample2
** ppSample
)
418 assert(pbBuffer
&& pParent
&& (cbBuffer
> 0));
420 if (!(*ppSample
= CoTaskMemAlloc(sizeof(StdMediaSample2
))))
421 return E_OUTOFMEMORY
;
423 (*ppSample
)->lpvtbl
= &StdMediaSample2_VTable
;
424 (*ppSample
)->ref
= 0;
425 ZeroMemory(&(*ppSample
)->props
, sizeof((*ppSample
)->props
));
427 /* NOTE: no need to AddRef as the parent is guaranteed to be around
428 * at least as long as us and we don't want to create circular
429 * dependencies on the ref count */
430 (*ppSample
)->pParent
= pParent
;
431 (*ppSample
)->props
.cbData
= sizeof(AM_SAMPLE2_PROPERTIES
);
432 (*ppSample
)->props
.cbBuffer
= (*ppSample
)->props
.lActual
= cbBuffer
;
433 (*ppSample
)->props
.pbBuffer
= pbBuffer
;
434 (*ppSample
)->tMediaStart
= INVALID_MEDIA_TIME
;
435 (*ppSample
)->tMediaEnd
= 0;
440 static void StdMediaSample2_Delete(StdMediaSample2
* This
)
442 /* NOTE: does not remove itself from the list it belongs to */
446 static HRESULT WINAPI
StdMediaSample2_QueryInterface(IMediaSample2
* iface
, REFIID riid
, LPVOID
* ppv
)
448 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
449 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
453 if (IsEqualIID(riid
, &IID_IUnknown
))
455 else if (IsEqualIID(riid
, &IID_IMediaSample
))
457 else if (IsEqualIID(riid
, &IID_IMediaSample2
))
462 IUnknown_AddRef((IUnknown
*)(*ppv
));
466 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
468 return E_NOINTERFACE
;
471 static ULONG WINAPI
StdMediaSample2_AddRef(IMediaSample2
* iface
)
473 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
475 TRACE("(%p)->() AddRef from %ld\n", iface
, This
->ref
);
477 return InterlockedIncrement(&This
->ref
);
480 static ULONG WINAPI
StdMediaSample2_Release(IMediaSample2
* iface
)
482 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
484 TRACE("(%p)->() Release from %ld\n", iface
, This
->ref
);
486 if (!InterlockedDecrement(&This
->ref
))
488 IMemAllocator_ReleaseBuffer(This
->pParent
, (IMediaSample
*)iface
);
494 static HRESULT WINAPI
StdMediaSample2_GetPointer(IMediaSample2
* iface
, BYTE
** ppBuffer
)
496 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
498 TRACE("(%p)\n", ppBuffer
);
500 *ppBuffer
= This
->props
.pbBuffer
;
505 static long WINAPI
StdMediaSample2_GetSize(IMediaSample2
* iface
)
507 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
509 TRACE("StdMediaSample2_GetSize()\n");
511 return This
->props
.cbBuffer
;
514 static HRESULT WINAPI
StdMediaSample2_GetTime(IMediaSample2
* iface
, REFERENCE_TIME
* pStart
, REFERENCE_TIME
* pEnd
)
517 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
519 TRACE("(%p, %p)\n", pStart
, pEnd
);
521 if (!(This
->props
.dwSampleFlags
& AM_SAMPLE_TIMEVALID
))
522 hr
= VFW_E_SAMPLE_TIME_NOT_SET
;
523 else if (!(This
->props
.dwSampleFlags
& AM_SAMPLE_STOPVALID
))
525 *pStart
= This
->props
.tStart
;
526 *pEnd
= This
->props
.tStart
+ 1;
528 hr
= VFW_S_NO_STOP_TIME
;
532 *pStart
= This
->props
.tStart
;
533 *pEnd
= This
->props
.tStop
;
541 static HRESULT WINAPI
StdMediaSample2_SetTime(IMediaSample2
* iface
, REFERENCE_TIME
* pStart
, REFERENCE_TIME
* pEnd
)
543 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
545 TRACE("(%p, %p)\n", pStart
, pEnd
);
549 This
->props
.tStart
= *pStart
;
550 This
->props
.dwSampleFlags
|= AM_SAMPLE_TIMEVALID
;
553 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_TIMEVALID
;
557 This
->props
.tStop
= *pEnd
;
558 This
->props
.dwSampleFlags
|= AM_SAMPLE_STOPVALID
;
561 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_STOPVALID
;
566 static HRESULT WINAPI
StdMediaSample2_IsSyncPoint(IMediaSample2
* iface
)
568 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
572 return (This
->props
.dwSampleFlags
& AM_SAMPLE_SPLICEPOINT
) ? S_OK
: S_FALSE
;
575 static HRESULT WINAPI
StdMediaSample2_SetSyncPoint(IMediaSample2
* iface
, BOOL bIsSyncPoint
)
577 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
579 TRACE("(%s)\n", bIsSyncPoint
? "TRUE" : "FALSE");
581 This
->props
.dwSampleFlags
= (This
->props
.dwSampleFlags
& ~AM_SAMPLE_SPLICEPOINT
) | bIsSyncPoint
? AM_SAMPLE_SPLICEPOINT
: 0;
586 static HRESULT WINAPI
StdMediaSample2_IsPreroll(IMediaSample2
* iface
)
588 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
592 return (This
->props
.dwSampleFlags
& AM_SAMPLE_PREROLL
) ? S_OK
: S_FALSE
;
595 static HRESULT WINAPI
StdMediaSample2_SetPreroll(IMediaSample2
* iface
, BOOL bIsPreroll
)
597 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
599 TRACE("(%s)\n", bIsPreroll
? "TRUE" : "FALSE");
601 This
->props
.dwSampleFlags
= (This
->props
.dwSampleFlags
& ~AM_SAMPLE_PREROLL
) | bIsPreroll
? AM_SAMPLE_PREROLL
: 0;
606 static LONG WINAPI
StdMediaSample2_GetActualDataLength(IMediaSample2
* iface
)
608 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
612 return This
->props
.lActual
;
615 static HRESULT WINAPI
StdMediaSample2_SetActualDataLength(IMediaSample2
* iface
, LONG len
)
617 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
619 TRACE("(%ld)\n", len
);
621 if ((len
> This
->props
.cbBuffer
) || (len
< 0))
622 return VFW_E_BUFFER_OVERFLOW
;
625 This
->props
.lActual
= len
;
630 static HRESULT WINAPI
StdMediaSample2_GetMediaType(IMediaSample2
* iface
, AM_MEDIA_TYPE
** ppMediaType
)
632 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
634 TRACE("(%p)\n", ppMediaType
);
636 if (!This
->props
.pMediaType
) {
637 /* Make sure we return a NULL pointer (required by native Quartz dll) */
643 if (!(*ppMediaType
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
))))
644 return E_OUTOFMEMORY
;
646 return CopyMediaType(*ppMediaType
, This
->props
.pMediaType
);
649 static HRESULT WINAPI
StdMediaSample2_SetMediaType(IMediaSample2
* iface
, AM_MEDIA_TYPE
* pMediaType
)
651 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
653 TRACE("(%p)\n", pMediaType
);
655 if (This
->props
.pMediaType
)
656 DeleteMediaType(This
->props
.pMediaType
);
657 else if (!(This
->props
.pMediaType
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
))))
658 return E_OUTOFMEMORY
;
660 return CopyMediaType(This
->props
.pMediaType
, pMediaType
);
663 static HRESULT WINAPI
StdMediaSample2_IsDiscontinuity(IMediaSample2
* iface
)
665 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
669 return (This
->props
.dwSampleFlags
& AM_SAMPLE_DATADISCONTINUITY
) ? S_OK
: S_FALSE
;
672 static HRESULT WINAPI
StdMediaSample2_SetDiscontinuity(IMediaSample2
* iface
, BOOL bIsDiscontinuity
)
674 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
676 TRACE("(%s)\n", bIsDiscontinuity
? "TRUE" : "FALSE");
678 This
->props
.dwSampleFlags
= (This
->props
.dwSampleFlags
& ~AM_SAMPLE_DATADISCONTINUITY
) | bIsDiscontinuity
? AM_SAMPLE_DATADISCONTINUITY
: 0;
683 static HRESULT WINAPI
StdMediaSample2_GetMediaTime(IMediaSample2
* iface
, LONGLONG
* pStart
, LONGLONG
* pEnd
)
685 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
687 TRACE("(%p, %p)\n", pStart
, pEnd
);
689 if (This
->tMediaStart
== INVALID_MEDIA_TIME
)
690 return VFW_E_MEDIA_TIME_NOT_SET
;
692 *pStart
= This
->tMediaStart
;
693 *pEnd
= This
->tMediaEnd
;
698 static HRESULT WINAPI
StdMediaSample2_SetMediaTime(IMediaSample2
* iface
, LONGLONG
* pStart
, LONGLONG
* pEnd
)
700 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
702 TRACE("(%p, %p)\n", pStart
, pEnd
);
705 This
->tMediaStart
= *pStart
;
707 This
->tMediaStart
= INVALID_MEDIA_TIME
;
710 This
->tMediaEnd
= *pEnd
;
717 static HRESULT WINAPI
StdMediaSample2_GetProperties(IMediaSample2
* iface
, DWORD cbProperties
, BYTE
* pbProperties
)
719 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
721 TRACE("(%ld, %p)\n", cbProperties
, pbProperties
);
723 memcpy(pbProperties
, &This
->props
, min(cbProperties
, sizeof(This
->props
)));
728 static HRESULT WINAPI
StdMediaSample2_SetProperties(IMediaSample2
* iface
, DWORD cbProperties
, const BYTE
* pbProperties
)
730 StdMediaSample2
*This
= (StdMediaSample2
*)iface
;
732 TRACE("(%ld, %p)\n", cbProperties
, pbProperties
);
734 /* NOTE: pbBuffer and cbBuffer are read-only */
735 memcpy(&This
->props
, pbProperties
, min(cbProperties
, AM_SAMPLE2_PROP_SIZE_WRITABLE
));
740 static const IMediaSample2Vtbl StdMediaSample2_VTable
=
742 StdMediaSample2_QueryInterface
,
743 StdMediaSample2_AddRef
,
744 StdMediaSample2_Release
,
745 StdMediaSample2_GetPointer
,
746 StdMediaSample2_GetSize
,
747 StdMediaSample2_GetTime
,
748 StdMediaSample2_SetTime
,
749 StdMediaSample2_IsSyncPoint
,
750 StdMediaSample2_SetSyncPoint
,
751 StdMediaSample2_IsPreroll
,
752 StdMediaSample2_SetPreroll
,
753 StdMediaSample2_GetActualDataLength
,
754 StdMediaSample2_SetActualDataLength
,
755 StdMediaSample2_GetMediaType
,
756 StdMediaSample2_SetMediaType
,
757 StdMediaSample2_IsDiscontinuity
,
758 StdMediaSample2_SetDiscontinuity
,
759 StdMediaSample2_GetMediaTime
,
760 StdMediaSample2_SetMediaTime
,
761 StdMediaSample2_GetProperties
,
762 StdMediaSample2_SetProperties
765 typedef struct StdMemAllocator
767 BaseMemAllocator base
;
771 static HRESULT
StdMemAllocator_Alloc(IMemAllocator
* iface
)
773 StdMemAllocator
*This
= (StdMemAllocator
*)iface
;
774 StdMediaSample2
* pSample
= NULL
;
778 assert(list_empty(&This
->base
.free_list
));
780 /* check alignment */
783 /* we do not allow a courser alignment than the OS page size */
784 if ((si
.dwPageSize
% This
->base
.pProps
->cbAlign
) != 0)
785 return VFW_E_BADALIGN
;
787 /* FIXME: each sample has to have its buffer start on the right alignment.
788 * We don't do this at the moment */
790 /* allocate memory */
791 This
->pMemory
= VirtualAlloc(NULL
, (This
->base
.pProps
->cbBuffer
+ This
->base
.pProps
->cbPrefix
) * This
->base
.pProps
->cBuffers
, MEM_COMMIT
, PAGE_READWRITE
);
793 for (i
= This
->base
.pProps
->cBuffers
- 1; i
>= 0; i
--)
795 /* pbBuffer does not start at the base address, it starts at base + cbPrefix */
796 BYTE
* pbBuffer
= (BYTE
*)This
->pMemory
+ i
* (This
->base
.pProps
->cbBuffer
+ This
->base
.pProps
->cbPrefix
) + This
->base
.pProps
->cbPrefix
;
798 StdMediaSample2_Construct(pbBuffer
, This
->base
.pProps
->cbBuffer
, iface
, &pSample
);
800 list_add_head(&This
->base
.free_list
, &pSample
->listentry
);
806 static HRESULT
StdMemAllocator_Free(IMemAllocator
* iface
)
808 StdMemAllocator
*This
= (StdMemAllocator
*)iface
;
809 struct list
* cursor
;
811 assert(list_empty(&This
->base
.used_list
));
813 while ((cursor
= list_head(&This
->base
.free_list
)) != NULL
)
816 StdMediaSample2_Delete(LIST_ENTRY(cursor
, StdMediaSample2
, listentry
));
820 if (!VirtualFree(This
->pMemory
, 0, MEM_RELEASE
))
822 ERR("Couldn't free memory. Error: %ld\n", GetLastError());
823 return HRESULT_FROM_WIN32(GetLastError());
829 HRESULT
StdMemAllocator_create(LPUNKNOWN lpUnkOuter
, LPVOID
* ppv
)
831 StdMemAllocator
* pMemAlloc
;
837 return CLASS_E_NOAGGREGATION
;
839 if (!(pMemAlloc
= CoTaskMemAlloc(sizeof(*pMemAlloc
))))
840 return E_OUTOFMEMORY
;
842 pMemAlloc
->pMemory
= NULL
;
844 if (SUCCEEDED(hr
= BaseMemAllocator_Init(StdMemAllocator_Alloc
, StdMemAllocator_Free
, &pMemAlloc
->base
)))
845 *ppv
= (LPVOID
)pMemAlloc
;
847 CoTaskMemFree(pMemAlloc
);