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
29 #include "quartz_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
34 typedef struct StdMediaSample2
36 IMediaSample2 IMediaSample2_iface
;
38 AM_SAMPLE2_PROPERTIES props
;
39 IMemAllocator
* pParent
;
40 struct list listentry
;
45 typedef struct BaseMemAllocator
47 IMemAllocator IMemAllocator_iface
;
50 ALLOCATOR_PROPERTIES props
;
51 HRESULT (* fnAlloc
) (IMemAllocator
*);
52 HRESULT (* fnFree
)(IMemAllocator
*);
53 HRESULT (* fnVerify
)(IMemAllocator
*, ALLOCATOR_PROPERTIES
*);
54 HRESULT (* fnBufferPrepare
)(IMemAllocator
*, StdMediaSample2
*, DWORD flags
);
55 HRESULT (* fnBufferReleased
)(IMemAllocator
*, StdMediaSample2
*);
56 void (* fnDestroyed
)(IMemAllocator
*);
61 struct list free_list
;
62 struct list used_list
;
63 CRITICAL_SECTION
*pCritSect
;
66 static inline BaseMemAllocator
*impl_from_IMemAllocator(IMemAllocator
*iface
)
68 return CONTAINING_RECORD(iface
, BaseMemAllocator
, IMemAllocator_iface
);
71 static const IMemAllocatorVtbl BaseMemAllocator_VTable
;
72 static const IMediaSample2Vtbl StdMediaSample2_VTable
;
73 static inline StdMediaSample2
*unsafe_impl_from_IMediaSample(IMediaSample
* iface
);
75 #define AM_SAMPLE2_PROP_SIZE_WRITABLE FIELD_OFFSET(AM_SAMPLE2_PROPERTIES, pbBuffer)
77 #define INVALID_MEDIA_TIME (((ULONGLONG)0x7fffffff << 32) | 0xffffffff)
79 static HRESULT
BaseMemAllocator_Init(HRESULT (* fnAlloc
)(IMemAllocator
*),
80 HRESULT (* fnFree
)(IMemAllocator
*),
81 HRESULT (* fnVerify
)(IMemAllocator
*, ALLOCATOR_PROPERTIES
*),
82 HRESULT (* fnBufferPrepare
)(IMemAllocator
*, StdMediaSample2
*, DWORD
),
83 HRESULT (* fnBufferReleased
)(IMemAllocator
*, StdMediaSample2
*),
84 void (* fnDestroyed
)(IMemAllocator
*),
85 CRITICAL_SECTION
*pCritSect
,
86 BaseMemAllocator
* pMemAlloc
)
88 assert(fnAlloc
&& fnFree
&& fnDestroyed
);
90 pMemAlloc
->IMemAllocator_iface
.lpVtbl
= &BaseMemAllocator_VTable
;
93 ZeroMemory(&pMemAlloc
->props
, sizeof(pMemAlloc
->props
));
94 list_init(&pMemAlloc
->free_list
);
95 list_init(&pMemAlloc
->used_list
);
96 pMemAlloc
->fnAlloc
= fnAlloc
;
97 pMemAlloc
->fnFree
= fnFree
;
98 pMemAlloc
->fnVerify
= fnVerify
;
99 pMemAlloc
->fnBufferPrepare
= fnBufferPrepare
;
100 pMemAlloc
->fnBufferReleased
= fnBufferReleased
;
101 pMemAlloc
->fnDestroyed
= fnDestroyed
;
102 pMemAlloc
->bDecommitQueued
= FALSE
;
103 pMemAlloc
->bCommitted
= FALSE
;
104 pMemAlloc
->hSemWaiting
= NULL
;
105 pMemAlloc
->lWaiting
= 0;
106 pMemAlloc
->pCritSect
= pCritSect
;
111 static HRESULT WINAPI
BaseMemAllocator_QueryInterface(IMemAllocator
* iface
, REFIID riid
, LPVOID
* ppv
)
113 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
114 TRACE("(%p)->(%s, %p)\n", This
, qzdebugstr_guid(riid
), ppv
);
118 if (IsEqualIID(riid
, &IID_IUnknown
))
119 *ppv
= &This
->IMemAllocator_iface
;
120 else if (IsEqualIID(riid
, &IID_IMemAllocator
))
121 *ppv
= &This
->IMemAllocator_iface
;
125 IUnknown_AddRef((IUnknown
*)(*ppv
));
129 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
131 return E_NOINTERFACE
;
134 static ULONG WINAPI
BaseMemAllocator_AddRef(IMemAllocator
* iface
)
136 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
137 ULONG ref
= InterlockedIncrement(&This
->ref
);
139 TRACE("(%p)->() AddRef from %d\n", iface
, ref
- 1);
144 static ULONG WINAPI
BaseMemAllocator_Release(IMemAllocator
* iface
)
146 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
147 ULONG ref
= InterlockedDecrement(&This
->ref
);
149 TRACE("(%p)->() Release from %d\n", iface
, ref
+ 1);
153 CloseHandle(This
->hSemWaiting
);
154 if (This
->bCommitted
)
157 This
->fnDestroyed(iface
);
163 static HRESULT WINAPI
BaseMemAllocator_SetProperties(IMemAllocator
* iface
, ALLOCATOR_PROPERTIES
*pRequest
, ALLOCATOR_PROPERTIES
*pActual
)
165 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
168 TRACE("(%p)->(%p, %p)\n", This
, pRequest
, pActual
);
170 EnterCriticalSection(This
->pCritSect
);
172 if (!list_empty(&This
->used_list
))
173 hr
= VFW_E_BUFFERS_OUTSTANDING
;
174 else if (This
->bCommitted
)
175 hr
= VFW_E_ALREADY_COMMITTED
;
176 else if (pRequest
->cbAlign
== 0)
181 hr
= This
->fnVerify(iface
, pRequest
);
186 This
->props
= *pRequest
;
188 *pActual
= This
->props
;
191 LeaveCriticalSection(This
->pCritSect
);
196 static HRESULT WINAPI
BaseMemAllocator_GetProperties(IMemAllocator
* iface
, ALLOCATOR_PROPERTIES
*pProps
)
198 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
201 TRACE("(%p)->(%p)\n", This
, pProps
);
203 EnterCriticalSection(This
->pCritSect
);
205 memcpy(pProps
, &This
->props
, sizeof(*pProps
));
207 LeaveCriticalSection(This
->pCritSect
);
212 static HRESULT WINAPI
BaseMemAllocator_Commit(IMemAllocator
* iface
)
214 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
217 TRACE("(%p)->()\n", This
);
219 EnterCriticalSection(This
->pCritSect
);
221 if (!This
->props
.cbAlign
)
223 else if (!This
->props
.cbBuffer
)
224 hr
= VFW_E_SIZENOTSET
;
225 else if (!This
->props
.cBuffers
)
226 hr
= VFW_E_BUFFER_NOTSET
;
227 else if (This
->bDecommitQueued
&& This
->bCommitted
)
229 This
->bDecommitQueued
= FALSE
;
232 else if (This
->bCommitted
)
236 if (!(This
->hSemWaiting
= CreateSemaphoreW(NULL
, This
->props
.cBuffers
, This
->props
.cBuffers
, NULL
)))
238 ERR("Couldn't create semaphore (error was %u)\n", GetLastError());
239 hr
= HRESULT_FROM_WIN32(GetLastError());
243 hr
= This
->fnAlloc(iface
);
245 This
->bCommitted
= TRUE
;
247 ERR("fnAlloc failed with error 0x%x\n", hr
);
251 LeaveCriticalSection(This
->pCritSect
);
256 static HRESULT WINAPI
BaseMemAllocator_Decommit(IMemAllocator
* iface
)
258 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
261 TRACE("(%p)->()\n", This
);
263 EnterCriticalSection(This
->pCritSect
);
265 if (!This
->bCommitted
)
269 if (!list_empty(&This
->used_list
))
271 This
->bDecommitQueued
= TRUE
;
272 /* notify ALL waiting threads that they cannot be allocated a buffer any more */
273 ReleaseSemaphore(This
->hSemWaiting
, This
->lWaiting
, NULL
);
279 if (This
->lWaiting
!= 0)
280 ERR("Waiting: %d\n", This
->lWaiting
);
282 This
->bCommitted
= FALSE
;
283 CloseHandle(This
->hSemWaiting
);
284 This
->hSemWaiting
= NULL
;
286 hr
= This
->fnFree(iface
);
288 ERR("fnFree failed with error 0x%x\n", hr
);
292 LeaveCriticalSection(This
->pCritSect
);
297 static HRESULT WINAPI
BaseMemAllocator_GetBuffer(IMemAllocator
* iface
, IMediaSample
** pSample
, REFERENCE_TIME
*pStartTime
, REFERENCE_TIME
*pEndTime
, DWORD dwFlags
)
299 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
302 /* NOTE: The pStartTime and pEndTime parameters are not applied to the sample.
303 * The allocator might use these values to determine which buffer it retrieves */
305 TRACE("(%p)->(%p, %p, %p, %x)\n", This
, pSample
, pStartTime
, pEndTime
, dwFlags
);
309 EnterCriticalSection(This
->pCritSect
);
310 if (!This
->bCommitted
|| This
->bDecommitQueued
)
312 WARN("Not committed\n");
313 hr
= VFW_E_NOT_COMMITTED
;
317 LeaveCriticalSection(This
->pCritSect
);
321 if (WaitForSingleObject(This
->hSemWaiting
, (dwFlags
& AM_GBF_NOWAIT
) ? 0 : INFINITE
) != WAIT_OBJECT_0
)
323 EnterCriticalSection(This
->pCritSect
);
325 LeaveCriticalSection(This
->pCritSect
);
327 return VFW_E_TIMEOUT
;
330 EnterCriticalSection(This
->pCritSect
);
333 if (!This
->bCommitted
)
334 hr
= VFW_E_NOT_COMMITTED
;
335 else if (This
->bDecommitQueued
)
340 struct list
* free
= list_head(&This
->free_list
);
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
);
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
);
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
)
383 if (This
->lWaiting
!= 0)
384 ERR("Waiting: %d\n", This
->lWaiting
);
386 This
->bCommitted
= FALSE
;
387 This
->bDecommitQueued
= FALSE
;
389 CloseHandle(This
->hSemWaiting
);
390 This
->hSemWaiting
= NULL
;
392 if (FAILED(hrfree
= This
->fnFree(iface
)))
393 ERR("fnFree failed with error 0x%x\n", hrfree
);
396 LeaveCriticalSection(This
->pCritSect
);
398 /* notify a waiting thread that there is now a free buffer */
399 if (This
->hSemWaiting
&& !ReleaseSemaphore(This
->hSemWaiting
, 1, NULL
))
401 ERR("ReleaseSemaphore failed with error %u\n", GetLastError());
402 hr
= HRESULT_FROM_WIN32(GetLastError());
408 static const IMemAllocatorVtbl BaseMemAllocator_VTable
=
410 BaseMemAllocator_QueryInterface
,
411 BaseMemAllocator_AddRef
,
412 BaseMemAllocator_Release
,
413 BaseMemAllocator_SetProperties
,
414 BaseMemAllocator_GetProperties
,
415 BaseMemAllocator_Commit
,
416 BaseMemAllocator_Decommit
,
417 BaseMemAllocator_GetBuffer
,
418 BaseMemAllocator_ReleaseBuffer
421 static HRESULT
StdMediaSample2_Construct(BYTE
* pbBuffer
, LONG cbBuffer
, IMemAllocator
* pParent
, StdMediaSample2
** ppSample
)
423 assert(pbBuffer
&& pParent
&& (cbBuffer
> 0));
425 if (!(*ppSample
= CoTaskMemAlloc(sizeof(StdMediaSample2
))))
426 return E_OUTOFMEMORY
;
428 (*ppSample
)->IMediaSample2_iface
.lpVtbl
= &StdMediaSample2_VTable
;
429 (*ppSample
)->ref
= 0;
430 ZeroMemory(&(*ppSample
)->props
, sizeof((*ppSample
)->props
));
432 /* NOTE: no need to AddRef as the parent is guaranteed to be around
433 * at least as long as us and we don't want to create circular
434 * dependencies on the ref count */
435 (*ppSample
)->pParent
= pParent
;
436 (*ppSample
)->props
.cbData
= sizeof(AM_SAMPLE2_PROPERTIES
);
437 (*ppSample
)->props
.cbBuffer
= (*ppSample
)->props
.lActual
= cbBuffer
;
438 (*ppSample
)->props
.pbBuffer
= pbBuffer
;
439 (*ppSample
)->tMediaStart
= INVALID_MEDIA_TIME
;
440 (*ppSample
)->tMediaEnd
= 0;
445 static void StdMediaSample2_Delete(StdMediaSample2
* This
)
447 /* NOTE: does not remove itself from the list it belongs to */
451 static inline StdMediaSample2
*impl_from_IMediaSample2(IMediaSample2
* iface
)
453 return CONTAINING_RECORD(iface
, StdMediaSample2
, IMediaSample2_iface
);
456 static HRESULT WINAPI
StdMediaSample2_QueryInterface(IMediaSample2
* iface
, REFIID riid
, void ** ppv
)
458 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
462 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMediaSample
) ||
463 IsEqualIID(riid
, &IID_IMediaSample2
))
466 IMediaSample2_AddRef(iface
);
470 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
471 return E_NOINTERFACE
;
474 static ULONG WINAPI
StdMediaSample2_AddRef(IMediaSample2
* iface
)
476 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
477 ULONG ref
= InterlockedIncrement(&This
->ref
);
479 TRACE("(%p)->(): new ref = %d\n", This
, ref
);
484 static ULONG WINAPI
StdMediaSample2_Release(IMediaSample2
* iface
)
486 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
487 ULONG ref
= InterlockedDecrement(&This
->ref
);
489 TRACE("(%p)->(): new ref = %d\n", This
, ref
);
494 IMemAllocator_ReleaseBuffer(This
->pParent
, (IMediaSample
*)iface
);
496 StdMediaSample2_Delete(This
);
501 static HRESULT WINAPI
StdMediaSample2_GetPointer(IMediaSample2
* iface
, BYTE
** ppBuffer
)
503 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
505 TRACE("(%p)->(%p)\n", iface
, ppBuffer
);
507 *ppBuffer
= This
->props
.pbBuffer
;
511 ERR("Requested an unlocked surface and trying to lock regardless\n");
518 static LONG WINAPI
StdMediaSample2_GetSize(IMediaSample2
* iface
)
520 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
522 TRACE("StdMediaSample2_GetSize()\n");
524 return This
->props
.cbBuffer
;
527 static HRESULT WINAPI
StdMediaSample2_GetTime(IMediaSample2
* iface
, REFERENCE_TIME
* pStart
, REFERENCE_TIME
* pEnd
)
529 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
532 TRACE("(%p)->(%p, %p)\n", iface
, pStart
, pEnd
);
534 if (!(This
->props
.dwSampleFlags
& AM_SAMPLE_TIMEVALID
))
535 hr
= VFW_E_SAMPLE_TIME_NOT_SET
;
536 else if (!(This
->props
.dwSampleFlags
& AM_SAMPLE_STOPVALID
))
538 *pStart
= This
->props
.tStart
;
539 *pEnd
= This
->props
.tStart
+ 1;
541 hr
= VFW_S_NO_STOP_TIME
;
545 *pStart
= This
->props
.tStart
;
546 *pEnd
= This
->props
.tStop
;
554 static HRESULT WINAPI
StdMediaSample2_SetTime(IMediaSample2
* iface
, REFERENCE_TIME
* pStart
, REFERENCE_TIME
* pEnd
)
556 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
558 TRACE("(%p)->(%p, %p)\n", iface
, pStart
, pEnd
);
562 This
->props
.tStart
= *pStart
;
563 This
->props
.dwSampleFlags
|= AM_SAMPLE_TIMEVALID
;
566 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_TIMEVALID
;
570 This
->props
.tStop
= *pEnd
;
571 This
->props
.dwSampleFlags
|= AM_SAMPLE_STOPVALID
;
574 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_STOPVALID
;
579 static HRESULT WINAPI
StdMediaSample2_IsSyncPoint(IMediaSample2
* iface
)
581 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
583 TRACE("(%p)->()\n", iface
);
585 return (This
->props
.dwSampleFlags
& AM_SAMPLE_SPLICEPOINT
) ? S_OK
: S_FALSE
;
588 static HRESULT WINAPI
StdMediaSample2_SetSyncPoint(IMediaSample2
* iface
, BOOL bIsSyncPoint
)
590 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
592 TRACE("(%p)->(%s)\n", iface
, bIsSyncPoint
? "TRUE" : "FALSE");
595 This
->props
.dwSampleFlags
|= AM_SAMPLE_SPLICEPOINT
;
597 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_SPLICEPOINT
;
602 static HRESULT WINAPI
StdMediaSample2_IsPreroll(IMediaSample2
* iface
)
604 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
606 TRACE("(%p)->()\n", iface
);
608 return (This
->props
.dwSampleFlags
& AM_SAMPLE_PREROLL
) ? S_OK
: S_FALSE
;
611 static HRESULT WINAPI
StdMediaSample2_SetPreroll(IMediaSample2
* iface
, BOOL bIsPreroll
)
613 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
615 TRACE("(%p)->(%s)\n", iface
, bIsPreroll
? "TRUE" : "FALSE");
618 This
->props
.dwSampleFlags
|= AM_SAMPLE_PREROLL
;
620 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_PREROLL
;
625 static LONG WINAPI
StdMediaSample2_GetActualDataLength(IMediaSample2
* iface
)
627 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
629 TRACE("(%p)->()\n", iface
);
631 return This
->props
.lActual
;
634 static HRESULT WINAPI
StdMediaSample2_SetActualDataLength(IMediaSample2
* iface
, LONG len
)
636 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
638 TRACE("(%p)->(%d)\n", iface
, len
);
640 if ((len
> This
->props
.cbBuffer
) || (len
< 0))
642 WARN("Tried to set length to %d, while max is %d\n", len
, This
->props
.cbBuffer
);
643 return VFW_E_BUFFER_OVERFLOW
;
647 This
->props
.lActual
= len
;
652 static HRESULT WINAPI
StdMediaSample2_GetMediaType(IMediaSample2
* iface
, AM_MEDIA_TYPE
** ppMediaType
)
654 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
656 TRACE("(%p)->(%p)\n", iface
, ppMediaType
);
658 if (!This
->props
.pMediaType
) {
659 /* Make sure we return a NULL pointer (required by native Quartz dll) */
665 if (!(*ppMediaType
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
))))
666 return E_OUTOFMEMORY
;
668 return CopyMediaType(*ppMediaType
, This
->props
.pMediaType
);
671 static HRESULT WINAPI
StdMediaSample2_SetMediaType(IMediaSample2
* iface
, AM_MEDIA_TYPE
* pMediaType
)
673 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
675 TRACE("(%p)->(%p)\n", iface
, pMediaType
);
677 if (This
->props
.pMediaType
)
679 FreeMediaType(This
->props
.pMediaType
);
680 This
->props
.pMediaType
= NULL
;
684 if (!(This
->props
.pMediaType
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
))))
685 return E_OUTOFMEMORY
;
687 return CopyMediaType(This
->props
.pMediaType
, pMediaType
);
690 static HRESULT WINAPI
StdMediaSample2_IsDiscontinuity(IMediaSample2
* iface
)
692 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
694 TRACE("(%p)->()\n", iface
);
696 return (This
->props
.dwSampleFlags
& AM_SAMPLE_DATADISCONTINUITY
) ? S_OK
: S_FALSE
;
699 static HRESULT WINAPI
StdMediaSample2_SetDiscontinuity(IMediaSample2
* iface
, BOOL bIsDiscontinuity
)
701 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
703 TRACE("(%p)->(%s)\n", iface
, bIsDiscontinuity
? "TRUE" : "FALSE");
705 if (bIsDiscontinuity
)
706 This
->props
.dwSampleFlags
|= AM_SAMPLE_DATADISCONTINUITY
;
708 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_DATADISCONTINUITY
;
713 static HRESULT WINAPI
StdMediaSample2_GetMediaTime(IMediaSample2
* iface
, LONGLONG
* pStart
, LONGLONG
* pEnd
)
715 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
717 TRACE("(%p)->(%p, %p)\n", iface
, pStart
, pEnd
);
719 if (This
->tMediaStart
== INVALID_MEDIA_TIME
)
720 return VFW_E_MEDIA_TIME_NOT_SET
;
722 *pStart
= This
->tMediaStart
;
723 *pEnd
= This
->tMediaEnd
;
728 static HRESULT WINAPI
StdMediaSample2_SetMediaTime(IMediaSample2
* iface
, LONGLONG
* pStart
, LONGLONG
* pEnd
)
730 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
732 TRACE("(%p)->(%p, %p)\n", iface
, pStart
, pEnd
);
735 This
->tMediaStart
= *pStart
;
737 This
->tMediaStart
= INVALID_MEDIA_TIME
;
740 This
->tMediaEnd
= *pEnd
;
747 static HRESULT WINAPI
StdMediaSample2_GetProperties(IMediaSample2
* iface
, DWORD cbProperties
, BYTE
* pbProperties
)
749 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
751 TRACE("(%p)->(%d, %p)\n", iface
, cbProperties
, pbProperties
);
753 memcpy(pbProperties
, &This
->props
, min(cbProperties
, sizeof(This
->props
)));
758 static HRESULT WINAPI
StdMediaSample2_SetProperties(IMediaSample2
* iface
, DWORD cbProperties
, const BYTE
* pbProperties
)
760 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
762 TRACE("(%p)->(%d, %p)\n", iface
, cbProperties
, pbProperties
);
764 /* NOTE: pbBuffer and cbBuffer are read-only */
765 memcpy(&This
->props
, pbProperties
, min(cbProperties
, AM_SAMPLE2_PROP_SIZE_WRITABLE
));
770 static const IMediaSample2Vtbl StdMediaSample2_VTable
=
772 StdMediaSample2_QueryInterface
,
773 StdMediaSample2_AddRef
,
774 StdMediaSample2_Release
,
775 StdMediaSample2_GetPointer
,
776 StdMediaSample2_GetSize
,
777 StdMediaSample2_GetTime
,
778 StdMediaSample2_SetTime
,
779 StdMediaSample2_IsSyncPoint
,
780 StdMediaSample2_SetSyncPoint
,
781 StdMediaSample2_IsPreroll
,
782 StdMediaSample2_SetPreroll
,
783 StdMediaSample2_GetActualDataLength
,
784 StdMediaSample2_SetActualDataLength
,
785 StdMediaSample2_GetMediaType
,
786 StdMediaSample2_SetMediaType
,
787 StdMediaSample2_IsDiscontinuity
,
788 StdMediaSample2_SetDiscontinuity
,
789 StdMediaSample2_GetMediaTime
,
790 StdMediaSample2_SetMediaTime
,
791 StdMediaSample2_GetProperties
,
792 StdMediaSample2_SetProperties
795 static inline StdMediaSample2
*unsafe_impl_from_IMediaSample(IMediaSample
* iface
)
797 IMediaSample2
*iface2
= (IMediaSample2
*)iface
;
801 assert(iface2
->lpVtbl
== &StdMediaSample2_VTable
);
802 return impl_from_IMediaSample2(iface2
);
805 typedef struct StdMemAllocator
807 BaseMemAllocator base
;
808 CRITICAL_SECTION csState
;
812 static inline StdMemAllocator
*StdMemAllocator_from_IMemAllocator(IMemAllocator
* iface
)
814 return CONTAINING_RECORD(iface
, StdMemAllocator
, base
.IMemAllocator_iface
);
817 static HRESULT
StdMemAllocator_Alloc(IMemAllocator
* iface
)
819 StdMemAllocator
*This
= StdMemAllocator_from_IMemAllocator(iface
);
820 StdMediaSample2
* pSample
= NULL
;
824 assert(list_empty(&This
->base
.free_list
));
826 /* check alignment */
829 /* we do not allow a courser alignment than the OS page size */
830 if ((si
.dwPageSize
% This
->base
.props
.cbAlign
) != 0)
831 return VFW_E_BADALIGN
;
833 /* FIXME: each sample has to have its buffer start on the right alignment.
834 * We don't do this at the moment */
836 /* allocate memory */
837 This
->pMemory
= VirtualAlloc(NULL
, (This
->base
.props
.cbBuffer
+ This
->base
.props
.cbPrefix
) * This
->base
.props
.cBuffers
, MEM_COMMIT
, PAGE_READWRITE
);
840 return E_OUTOFMEMORY
;
842 for (i
= This
->base
.props
.cBuffers
- 1; i
>= 0; i
--)
844 /* pbBuffer does not start at the base address, it starts at base + cbPrefix */
845 BYTE
* pbBuffer
= (BYTE
*)This
->pMemory
+ i
* (This
->base
.props
.cbBuffer
+ This
->base
.props
.cbPrefix
) + This
->base
.props
.cbPrefix
;
847 StdMediaSample2_Construct(pbBuffer
, This
->base
.props
.cbBuffer
, iface
, &pSample
);
849 list_add_head(&This
->base
.free_list
, &pSample
->listentry
);
855 static HRESULT
StdMemAllocator_Free(IMemAllocator
* iface
)
857 StdMemAllocator
*This
= StdMemAllocator_from_IMemAllocator(iface
);
858 struct list
* cursor
;
860 if (!list_empty(&This
->base
.used_list
))
862 WARN("Freeing allocator with outstanding samples!\n");
863 while ((cursor
= list_head(&This
->base
.used_list
)) != NULL
)
865 StdMediaSample2
*pSample
;
867 pSample
= LIST_ENTRY(cursor
, StdMediaSample2
, listentry
);
868 pSample
->pParent
= NULL
;
872 while ((cursor
= list_head(&This
->base
.free_list
)) != NULL
)
875 StdMediaSample2_Delete(LIST_ENTRY(cursor
, StdMediaSample2
, listentry
));
879 if (!VirtualFree(This
->pMemory
, 0, MEM_RELEASE
))
881 ERR("Couldn't free memory. Error: %u\n", GetLastError());
882 return HRESULT_FROM_WIN32(GetLastError());
888 static void StdMemAllocator_Destroy(IMemAllocator
*iface
)
890 StdMemAllocator
*This
= StdMemAllocator_from_IMemAllocator(iface
);
892 This
->csState
.DebugInfo
->Spare
[0] = 0;
893 DeleteCriticalSection(&This
->csState
);
898 HRESULT
StdMemAllocator_create(LPUNKNOWN lpUnkOuter
, LPVOID
* ppv
)
900 StdMemAllocator
* pMemAlloc
;
906 return CLASS_E_NOAGGREGATION
;
908 if (!(pMemAlloc
= CoTaskMemAlloc(sizeof(*pMemAlloc
))))
909 return E_OUTOFMEMORY
;
911 InitializeCriticalSection(&pMemAlloc
->csState
);
912 pMemAlloc
->csState
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": StdMemAllocator.csState");
914 pMemAlloc
->pMemory
= NULL
;
916 if (SUCCEEDED(hr
= BaseMemAllocator_Init(StdMemAllocator_Alloc
, StdMemAllocator_Free
, NULL
, NULL
, NULL
, StdMemAllocator_Destroy
, &pMemAlloc
->csState
, &pMemAlloc
->base
)))
919 CoTaskMemFree(pMemAlloc
);