Added Video Renderer (based on Direct Draw).
[wine] / dlls / quartz / memallocator.c
1 /*
2  * Memory Allocator and Media Sample Implementation
3  *
4  * Copyright 2003 Robert Shearman
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <limits.h>
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "vfwmsgs.h"
28
29 #include "quartz_private.h"
30 #include "wine/list.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
34
35 void dump_AM_SAMPLE2_PROPERTIES(AM_SAMPLE2_PROPERTIES * pProps)
36 {
37     if (!pProps)
38     {
39         TRACE("AM_SAMPLE2_PROPERTIES: (null)\n");
40         return;
41     }
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);
52 }
53
54 typedef struct BaseMemAllocator
55 {
56     const IMemAllocatorVtbl * lpVtbl;
57
58     ULONG ref;
59     ALLOCATOR_PROPERTIES * pProps;
60     CRITICAL_SECTION csState;
61     HRESULT (* fnAlloc) (IMemAllocator *);
62     HRESULT (* fnFree)(IMemAllocator *);
63     HANDLE hSemWaiting;
64     BOOL bDecommitQueued;
65     BOOL bCommitted;
66     LONG lWaiting;
67     struct list free_list;
68     struct list used_list;
69 } BaseMemAllocator;
70
71 typedef struct StdMediaSample2
72 {
73     const IMediaSample2Vtbl * lpvtbl;
74
75     ULONG ref;
76     AM_SAMPLE2_PROPERTIES props;
77     IMemAllocator * pParent;
78     struct list listentry;
79     LONGLONG tMediaStart;
80     LONGLONG tMediaEnd;
81 } StdMediaSample2;
82
83 static const struct IMemAllocatorVtbl BaseMemAllocator_VTable;
84 static const struct IMediaSample2Vtbl StdMediaSample2_VTable;
85
86 #define AM_SAMPLE2_PROP_SIZE_WRITABLE (unsigned int)(&((AM_SAMPLE2_PROPERTIES *)0)->pbBuffer)
87
88 #ifdef _I64_MAX
89 #define INVALID_MEDIA_TIME _I64_MAX
90 #else
91 #define INVALID_MEDIA_TIME ((long long)(~0ull >> 1))
92 #endif
93
94 static HRESULT BaseMemAllocator_Init(HRESULT (* fnAlloc)(IMemAllocator *), HRESULT (* fnFree)(IMemAllocator *), BaseMemAllocator * pMemAlloc)
95 {
96     assert(fnAlloc && fnFree);
97
98     pMemAlloc->lpVtbl = &BaseMemAllocator_VTable;
99
100     pMemAlloc->ref = 1;
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;
110
111     InitializeCriticalSection(&pMemAlloc->csState);
112
113     return S_OK;
114 }
115
116 static HRESULT WINAPI BaseMemAllocator_QueryInterface(IMemAllocator * iface, REFIID riid, LPVOID * ppv)
117 {
118     ICOM_THIS(BaseMemAllocator, iface);
119     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
120
121     *ppv = NULL;
122
123     if (IsEqualIID(riid, &IID_IUnknown))
124         *ppv = (LPVOID)This;
125     else if (IsEqualIID(riid, &IID_IMemAllocator))
126         *ppv = (LPVOID)This;
127
128     if (*ppv)
129     {
130         IUnknown_AddRef((IUnknown *)(*ppv));
131         return S_OK;
132     }
133
134     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
135
136     return E_NOINTERFACE;
137 }
138
139 static ULONG WINAPI BaseMemAllocator_AddRef(IMemAllocator * iface)
140 {
141     ICOM_THIS(BaseMemAllocator, iface);
142
143     TRACE("()\n");
144
145     return InterlockedIncrement(&This->ref);
146 }
147
148 static ULONG WINAPI BaseMemAllocator_Release(IMemAllocator * iface)
149 {
150     ICOM_THIS(BaseMemAllocator, iface);
151
152     TRACE("()\n");
153
154     if (!InterlockedDecrement(&This->ref))
155     {
156         CloseHandle(This->hSemWaiting);
157         if (This->bCommitted)
158             This->fnFree(iface);
159         if (This->pProps)
160             HeapFree(GetProcessHeap(), 0, This->pProps);
161         CoTaskMemFree(This);
162         return 0;
163     }
164     return This->ref;
165 }
166
167 static HRESULT WINAPI BaseMemAllocator_SetProperties(IMemAllocator * iface, ALLOCATOR_PROPERTIES *pRequest, ALLOCATOR_PROPERTIES *pActual)
168 {
169     ICOM_THIS(BaseMemAllocator, iface);
170     HRESULT hr;
171
172     TRACE("(%p, %p)\n", pRequest, pActual);
173
174     EnterCriticalSection(&This->csState);
175     {
176         if (!list_empty(&This->used_list))
177             hr = VFW_E_BUFFERS_OUTSTANDING;
178         else if (This->bCommitted)
179             hr = VFW_E_ALREADY_COMMITTED;
180         else
181         {
182             if (!This->pProps)
183                 This->pProps = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->pProps));
184
185             if (!This->pProps)
186                 hr = E_OUTOFMEMORY;
187             else
188             {
189                 memcpy(This->pProps, pRequest, sizeof(*This->pProps));
190                     
191                 memcpy(pActual, pRequest, sizeof(*pActual));
192
193                 hr = S_OK;
194             }
195         }
196     }
197     LeaveCriticalSection(&This->csState);
198
199     return hr;
200 }
201
202 static HRESULT WINAPI BaseMemAllocator_GetProperties(IMemAllocator * iface, ALLOCATOR_PROPERTIES *pProps)
203 {
204     ICOM_THIS(BaseMemAllocator, iface);
205     HRESULT hr = S_OK;
206
207     TRACE("(%p)\n", pProps);
208
209     EnterCriticalSection(&This->csState);
210     {
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 */
217         if (!This->pProps)
218             hr = VFW_E_SIZENOTSET;
219         else
220             memcpy(pProps, This->pProps, sizeof(*pProps));
221     }
222     LeaveCriticalSection(&This->csState);
223
224     return hr;
225 }
226
227 static HRESULT WINAPI BaseMemAllocator_Commit(IMemAllocator * iface)
228 {
229     ICOM_THIS(BaseMemAllocator, iface);
230     HRESULT hr;
231
232     TRACE("()\n");
233
234     EnterCriticalSection(&This->csState);
235     {
236         if (!This->pProps)
237             hr = VFW_E_SIZENOTSET;
238         else if (This->bCommitted)
239             hr = VFW_E_ALREADY_COMMITTED;
240         else if (This->bDecommitQueued)
241         {
242             This->bDecommitQueued = FALSE;
243             hr = S_OK;
244         }
245         else
246         {
247             if (!(This->hSemWaiting = CreateSemaphoreW(NULL, This->pProps->cBuffers, This->pProps->cBuffers, NULL)))
248             {
249                 ERR("Couldn't create semaphore (error was %ld)\n", GetLastError());
250                 hr = HRESULT_FROM_WIN32(GetLastError());
251             }
252             else
253             {
254                 hr = This->fnAlloc(iface);
255                 if (SUCCEEDED(hr))
256                     This->bCommitted = TRUE;
257                 else
258                     ERR("fnAlloc failed with error 0x%lx\n", hr);
259             }
260         }
261     }
262     LeaveCriticalSection(&This->csState);
263
264     return hr;
265 }
266
267 static HRESULT WINAPI BaseMemAllocator_Decommit(IMemAllocator * iface)
268 {
269     ICOM_THIS(BaseMemAllocator, iface);
270     HRESULT hr;
271
272     TRACE("()\n");
273
274     EnterCriticalSection(&This->csState);
275     {
276         if (!This->bCommitted)
277             hr = VFW_E_NOT_COMMITTED;
278         else
279         {
280             if (!list_empty(&This->used_list))
281             {
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);
285                 
286                 hr = S_OK;
287             }
288             else
289             {
290                 assert(This->lWaiting == 0);
291
292                 This->bCommitted = FALSE;
293                 CloseHandle(This->hSemWaiting);
294                 This->hSemWaiting = NULL;
295
296                 hr = This->fnFree(iface);
297                 if (FAILED(hr))
298                     ERR("fnFree failed with error 0x%lx\n", hr);
299             }
300         }
301     }
302     LeaveCriticalSection(&This->csState);
303
304     return hr;
305 }
306
307 static HRESULT WINAPI BaseMemAllocator_GetBuffer(IMemAllocator * iface, IMediaSample ** pSample, REFERENCE_TIME *pStartTime, REFERENCE_TIME *pEndTime, DWORD dwFlags)
308 {
309     ICOM_THIS(BaseMemAllocator, iface);
310     HRESULT hr = S_OK;
311
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 */
314     
315     TRACE("(%p, %p, %p, %lx)\n", pSample, pStartTime, pEndTime, dwFlags);
316
317     *pSample = NULL;
318
319     if (!This->bCommitted)
320         return VFW_E_NOT_COMMITTED;
321
322     This->lWaiting++;
323     if (WaitForSingleObject(This->hSemWaiting, (dwFlags & AM_GBF_NOWAIT) ? 0 : INFINITE) != WAIT_OBJECT_0)
324     {
325         This->lWaiting--;
326         return VFW_E_TIMEOUT;
327     }
328     This->lWaiting--;
329
330     EnterCriticalSection(&This->csState);
331     {
332         if (!This->bCommitted)
333             hr = VFW_E_NOT_COMMITTED;
334         else if (This->bDecommitQueued)
335             hr = VFW_E_TIMEOUT;
336         else
337         {
338             struct list * free = list_head(&This->free_list);
339             list_remove(free);
340             list_add_head(&This->used_list, free);
341
342             *pSample = (IMediaSample *)LIST_ENTRY(free, StdMediaSample2, listentry);
343
344             assert(((StdMediaSample2 *)*pSample)->ref == 0);
345
346             IMediaSample_AddRef(*pSample);
347         }
348     }
349     LeaveCriticalSection(&This->csState);
350
351     return hr;
352 }
353
354 static HRESULT WINAPI BaseMemAllocator_ReleaseBuffer(IMemAllocator * iface, IMediaSample * pSample)
355 {
356     ICOM_THIS(BaseMemAllocator, iface);
357     StdMediaSample2 * pStdSample = (StdMediaSample2 *)pSample;
358     HRESULT hr = S_OK;
359     
360     TRACE("(%p)\n", pSample);
361
362     /* FIXME: make sure that sample is currently on the used list */
363
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);
367     {
368         if (!This->bCommitted)
369             ERR("Releasing a buffer when the allocator is not committed?!?\n");
370
371                 /* remove from used_list */
372         list_remove(&pStdSample->listentry);
373
374         list_add_head(&This->free_list, &pStdSample->listentry);
375
376         if (list_empty(&This->used_list) && This->bDecommitQueued && This->bCommitted)
377         {
378             HRESULT hrfree;
379
380             assert(This->lWaiting == 0);
381
382             This->bCommitted = FALSE;
383             This->bDecommitQueued = FALSE;
384
385             CloseHandle(This->hSemWaiting);
386             This->hSemWaiting = NULL;
387             
388             if (FAILED(hrfree = This->fnFree(iface)))
389                 ERR("fnFree failed with error 0x%lx\n", hrfree);
390         }
391     }
392     LeaveCriticalSection(&This->csState);
393
394     /* notify a waiting thread that there is now a free buffer */
395     if (!ReleaseSemaphore(This->hSemWaiting, 1, NULL))
396     {
397         ERR("ReleaseSemaphore failed with error %ld\n", GetLastError());
398         hr = HRESULT_FROM_WIN32(GetLastError());
399     }
400
401     return hr;
402 }
403
404 static const IMemAllocatorVtbl BaseMemAllocator_VTable = 
405 {
406     BaseMemAllocator_QueryInterface,
407     BaseMemAllocator_AddRef,
408     BaseMemAllocator_Release,
409     BaseMemAllocator_SetProperties,
410     BaseMemAllocator_GetProperties,
411     BaseMemAllocator_Commit,
412     BaseMemAllocator_Decommit,
413     BaseMemAllocator_GetBuffer,
414     BaseMemAllocator_ReleaseBuffer
415 };
416
417 static HRESULT StdMediaSample2_Construct(BYTE * pbBuffer, LONG cbBuffer, IMemAllocator * pParent, StdMediaSample2 ** ppSample)
418 {
419     assert(pbBuffer && pParent && (cbBuffer > 0));
420
421     if (!(*ppSample = CoTaskMemAlloc(sizeof(StdMediaSample2))))
422         return E_OUTOFMEMORY;
423
424     (*ppSample)->lpvtbl = &StdMediaSample2_VTable;
425     (*ppSample)->ref = 0;
426     ZeroMemory(&(*ppSample)->props, sizeof((*ppSample)->props));
427
428     /* NOTE: no need to AddRef as the parent is guaranteed to be around
429      * at least as long as us and we don't want to create circular
430      * dependencies on the ref count */
431     (*ppSample)->pParent = pParent;
432     (*ppSample)->props.cbData = sizeof(AM_SAMPLE2_PROPERTIES);
433     (*ppSample)->props.cbBuffer = (*ppSample)->props.lActual = cbBuffer;
434     (*ppSample)->props.pbBuffer = pbBuffer;
435     (*ppSample)->tMediaStart = INVALID_MEDIA_TIME;
436     (*ppSample)->tMediaEnd = 0;
437
438     return S_OK;
439 }
440
441 static void StdMediaSample2_Delete(StdMediaSample2 * This)
442 {
443     /* NOTE: does not remove itself from the list it belongs to */
444     CoTaskMemFree(This);
445 }
446
447 static HRESULT WINAPI StdMediaSample2_QueryInterface(IMediaSample2 * iface, REFIID riid, LPVOID * ppv)
448 {
449     ICOM_THIS(StdMediaSample2, iface);
450     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
451
452     *ppv = NULL;
453
454     if (IsEqualIID(riid, &IID_IUnknown))
455         *ppv = (LPVOID)This;
456     else if (IsEqualIID(riid, &IID_IMediaSample))
457         *ppv = (LPVOID)This;
458     else if (IsEqualIID(riid, &IID_IMediaSample2))
459         *ppv = (LPVOID)This;
460
461     if (*ppv)
462     {
463         IUnknown_AddRef((IUnknown *)(*ppv));
464         return S_OK;
465     }
466
467     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
468
469     return E_NOINTERFACE;
470 }
471
472 static ULONG WINAPI StdMediaSample2_AddRef(IMediaSample2 * iface)
473 {
474     ICOM_THIS(StdMediaSample2, iface);
475
476     TRACE("(%p)->() AddRef from %ld\n", iface, This->ref);
477
478     return InterlockedIncrement(&This->ref);
479 }
480
481 static ULONG WINAPI StdMediaSample2_Release(IMediaSample2 * iface)
482 {
483     ICOM_THIS(StdMediaSample2, iface);
484
485     TRACE("(%p)->() Release from %ld\n", iface, This->ref);
486
487     if (!InterlockedDecrement(&This->ref))
488     {
489         IMemAllocator_ReleaseBuffer(This->pParent, (IMediaSample *)iface);
490         return 0;
491     }
492     return This->ref;
493 }
494
495 static HRESULT WINAPI StdMediaSample2_GetPointer(IMediaSample2 * iface, BYTE ** ppBuffer)
496 {
497     ICOM_THIS(StdMediaSample2, iface);
498
499     TRACE("(%p)\n", ppBuffer);
500
501     *ppBuffer = This->props.pbBuffer;
502
503     return S_OK;
504 }
505
506 static long WINAPI StdMediaSample2_GetSize(IMediaSample2 * iface)
507 {
508     ICOM_THIS(StdMediaSample2, iface);
509
510     TRACE("StdMediaSample2_GetSize()\n");
511
512     return This->props.cbBuffer;
513 }
514
515 static HRESULT WINAPI StdMediaSample2_GetTime(IMediaSample2 * iface, REFERENCE_TIME * pStart, REFERENCE_TIME * pEnd)
516 {
517     HRESULT hr;
518     ICOM_THIS(StdMediaSample2, iface);
519
520     TRACE("(%p, %p)\n", pStart, pEnd);
521
522     if (!(This->props.dwSampleFlags & AM_SAMPLE_TIMEVALID))
523         hr = VFW_E_SAMPLE_TIME_NOT_SET;
524     else if (!(This->props.dwSampleFlags & AM_SAMPLE_STOPVALID))
525     {
526         *pStart = This->props.tStart;
527         *pEnd = This->props.tStart + 1;
528         
529         hr = VFW_S_NO_STOP_TIME;
530     }
531     else
532     {
533         *pStart = This->props.tStart;
534         *pEnd = This->props.tStop;
535
536         hr = S_OK;
537     }
538
539     return S_OK;
540 }
541
542 static HRESULT WINAPI StdMediaSample2_SetTime(IMediaSample2 * iface, REFERENCE_TIME * pStart, REFERENCE_TIME * pEnd)
543 {
544     ICOM_THIS(StdMediaSample2, iface);
545
546     TRACE("(%p, %p)\n", pStart, pEnd);
547
548     if (pStart)
549     {
550         This->props.tStart = *pStart;
551         This->props.dwSampleFlags |= AM_SAMPLE_TIMEVALID;
552     }
553     else
554         This->props.dwSampleFlags &= ~AM_SAMPLE_TIMEVALID;
555
556     if (pEnd)
557     {
558         This->props.tStop = *pEnd;
559         This->props.dwSampleFlags |= AM_SAMPLE_STOPVALID;
560     }
561     else
562         This->props.dwSampleFlags &= ~AM_SAMPLE_STOPVALID;
563
564     return S_OK;
565 }
566
567 static HRESULT WINAPI StdMediaSample2_IsSyncPoint(IMediaSample2 * iface)
568 {
569     ICOM_THIS(StdMediaSample2, iface);
570
571     TRACE("()\n");
572
573     return (This->props.dwSampleFlags & AM_SAMPLE_SPLICEPOINT) ? S_OK : S_FALSE;
574 }
575
576 static HRESULT WINAPI StdMediaSample2_SetSyncPoint(IMediaSample2 * iface, BOOL bIsSyncPoint)
577 {
578     ICOM_THIS(StdMediaSample2, iface);
579
580     TRACE("(%s)\n", bIsSyncPoint ? "TRUE" : "FALSE");
581
582     This->props.dwSampleFlags = (This->props.dwSampleFlags & ~AM_SAMPLE_SPLICEPOINT) | bIsSyncPoint ? AM_SAMPLE_SPLICEPOINT : 0;
583
584     return S_OK;
585 }
586
587 static HRESULT WINAPI StdMediaSample2_IsPreroll(IMediaSample2 * iface)
588 {
589     ICOM_THIS(StdMediaSample2, iface);
590
591     TRACE("()\n");
592
593     return (This->props.dwSampleFlags & AM_SAMPLE_PREROLL) ? S_OK : S_FALSE;
594 }
595
596 static HRESULT WINAPI StdMediaSample2_SetPreroll(IMediaSample2 * iface, BOOL bIsPreroll)
597 {
598     ICOM_THIS(StdMediaSample2, iface);
599
600     TRACE("(%s)\n", bIsPreroll ? "TRUE" : "FALSE");
601
602     This->props.dwSampleFlags = (This->props.dwSampleFlags & ~AM_SAMPLE_PREROLL) | bIsPreroll ? AM_SAMPLE_PREROLL : 0;
603
604     return S_OK;
605 }
606
607 static LONG WINAPI StdMediaSample2_GetActualDataLength(IMediaSample2 * iface)
608 {
609     ICOM_THIS(StdMediaSample2, iface);
610
611     TRACE("()\n");
612
613     return This->props.lActual;
614 }
615
616 static HRESULT WINAPI StdMediaSample2_SetActualDataLength(IMediaSample2 * iface, LONG len)
617 {
618     ICOM_THIS(StdMediaSample2, iface);
619
620     TRACE("(%ld)\n", len);
621
622     if ((len > This->props.cbBuffer) || (len < 0))
623         return VFW_E_BUFFER_OVERFLOW;
624     else
625     {
626         This->props.lActual = len;
627         return S_OK;
628     }
629 }
630
631 static HRESULT WINAPI StdMediaSample2_GetMediaType(IMediaSample2 * iface, AM_MEDIA_TYPE ** ppMediaType)
632 {
633     ICOM_THIS(StdMediaSample2, iface);
634
635     TRACE("(%p)\n", ppMediaType);
636
637     if (!This->props.pMediaType)
638         return S_FALSE;
639
640     if (!(*ppMediaType = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE))))
641         return E_OUTOFMEMORY;
642
643     return CopyMediaType(*ppMediaType, This->props.pMediaType);
644 }
645
646 static HRESULT WINAPI StdMediaSample2_SetMediaType(IMediaSample2 * iface, AM_MEDIA_TYPE * pMediaType)
647 {
648     ICOM_THIS(StdMediaSample2, iface);
649
650     TRACE("(%p)\n", pMediaType);
651
652     if (This->props.pMediaType)
653         DeleteMediaType(This->props.pMediaType);
654     else if (!(This->props.pMediaType = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE))))
655         return E_OUTOFMEMORY;
656
657     return CopyMediaType(This->props.pMediaType, pMediaType);
658 }
659
660 static HRESULT WINAPI StdMediaSample2_IsDiscontinuity(IMediaSample2 * iface)
661 {
662     ICOM_THIS(StdMediaSample2, iface);
663
664     TRACE("()\n");
665
666     return (This->props.dwSampleFlags & AM_SAMPLE_DATADISCONTINUITY) ? S_OK : S_FALSE;
667 }
668
669 static HRESULT WINAPI StdMediaSample2_SetDiscontinuity(IMediaSample2 * iface, BOOL bIsDiscontinuity)
670 {
671     ICOM_THIS(StdMediaSample2, iface);
672
673     TRACE("(%s)\n", bIsDiscontinuity ? "TRUE" : "FALSE");
674
675     This->props.dwSampleFlags = (This->props.dwSampleFlags & ~AM_SAMPLE_DATADISCONTINUITY) | bIsDiscontinuity ? AM_SAMPLE_DATADISCONTINUITY : 0;
676
677     return S_OK;
678 }
679
680 static HRESULT WINAPI StdMediaSample2_GetMediaTime(IMediaSample2 * iface, LONGLONG * pStart, LONGLONG * pEnd)
681 {
682     ICOM_THIS(StdMediaSample2, iface);
683
684     TRACE("(%p, %p)\n", pStart, pEnd);
685
686     if (This->tMediaStart == INVALID_MEDIA_TIME)
687         return VFW_E_MEDIA_TIME_NOT_SET;
688
689     *pStart = This->tMediaStart;
690     *pEnd = This->tMediaEnd;
691
692     return E_NOTIMPL;
693 }
694
695 static HRESULT WINAPI StdMediaSample2_SetMediaTime(IMediaSample2 * iface, LONGLONG * pStart, LONGLONG * pEnd)
696 {
697     ICOM_THIS(StdMediaSample2, iface);
698
699     TRACE("(%p, %p)\n", pStart, pEnd);
700
701     if (pStart)
702         This->tMediaStart = *pStart;
703     else
704         This->tMediaStart = INVALID_MEDIA_TIME;
705
706     if (pEnd)
707         This->tMediaEnd = *pEnd;
708     else
709         This->tMediaEnd = 0;
710
711     return S_OK;
712 }
713
714 static HRESULT WINAPI StdMediaSample2_GetProperties(IMediaSample2 * iface, DWORD cbProperties, BYTE * pbProperties)
715 {
716     ICOM_THIS(StdMediaSample2, iface);
717
718     TRACE("(%ld, %p)\n", cbProperties, pbProperties);
719
720     memcpy(pbProperties, &This->props, min(cbProperties, sizeof(This->props)));
721
722     return S_OK;
723 }
724
725 static HRESULT WINAPI StdMediaSample2_SetProperties(IMediaSample2 * iface, DWORD cbProperties, const BYTE * pbProperties)
726 {
727     ICOM_THIS(StdMediaSample2, iface);
728
729     TRACE("(%ld, %p)\n", cbProperties, pbProperties);
730
731     /* NOTE: pbBuffer and cbBuffer are read-only */
732     memcpy(&This->props, pbProperties, min(cbProperties, AM_SAMPLE2_PROP_SIZE_WRITABLE));
733
734     return S_OK;
735 }
736
737 static const IMediaSample2Vtbl StdMediaSample2_VTable = 
738 {
739     StdMediaSample2_QueryInterface,
740     StdMediaSample2_AddRef,
741     StdMediaSample2_Release,
742     StdMediaSample2_GetPointer,
743     StdMediaSample2_GetSize,
744     StdMediaSample2_GetTime,
745     StdMediaSample2_SetTime,
746     StdMediaSample2_IsSyncPoint,
747     StdMediaSample2_SetSyncPoint,
748     StdMediaSample2_IsPreroll,
749     StdMediaSample2_SetPreroll,
750     StdMediaSample2_GetActualDataLength,
751     StdMediaSample2_SetActualDataLength,
752     StdMediaSample2_GetMediaType,
753     StdMediaSample2_SetMediaType,
754     StdMediaSample2_IsDiscontinuity,
755     StdMediaSample2_SetDiscontinuity,
756     StdMediaSample2_GetMediaTime,
757     StdMediaSample2_SetMediaTime,
758     StdMediaSample2_GetProperties,
759     StdMediaSample2_SetProperties
760 };
761
762 typedef struct StdMemAllocator
763 {
764     BaseMemAllocator base;
765     LPVOID pMemory;
766 } StdMemAllocator;
767
768 static HRESULT StdMemAllocator_Alloc(IMemAllocator * iface)
769 {
770     ICOM_THIS(StdMemAllocator, iface);
771     StdMediaSample2 * pSample = NULL;
772     SYSTEM_INFO si;
773     long i;
774
775     assert(list_empty(&This->base.free_list));
776
777     /* check alignment */
778     GetSystemInfo(&si);
779
780     /* we do not allow a courser alignment than the OS page size */
781     if ((si.dwPageSize % This->base.pProps->cbAlign) != 0)
782         return VFW_E_BADALIGN;
783
784     /* FIXME: each sample has to have its buffer start on the right alignment.
785      * We don't do this at the moment */
786
787     /* allocate memory */
788     This->pMemory = VirtualAlloc(NULL, (This->base.pProps->cbBuffer + This->base.pProps->cbPrefix) * This->base.pProps->cBuffers, MEM_COMMIT, PAGE_READWRITE);
789
790     for (i = This->base.pProps->cBuffers - 1; i >= 0; i--)
791     {
792         /* pbBuffer does not start at the base address, it starts at base + cbPrefix */
793         BYTE * pbBuffer = (BYTE *)This->pMemory + i * (This->base.pProps->cbBuffer + This->base.pProps->cbPrefix) + This->base.pProps->cbPrefix;
794         
795         StdMediaSample2_Construct(pbBuffer, This->base.pProps->cbBuffer, iface, &pSample);
796
797         list_add_head(&This->base.free_list, &pSample->listentry);
798     }
799
800     return S_OK;
801 }
802
803 static HRESULT StdMemAllocator_Free(IMemAllocator * iface)
804 {
805     ICOM_THIS(StdMemAllocator, iface);
806     struct list * cursor;
807
808     assert(list_empty(&This->base.used_list));
809
810     while ((cursor = list_head(&This->base.free_list)) != NULL)
811     {
812         list_remove(cursor);
813         StdMediaSample2_Delete(LIST_ENTRY(cursor, StdMediaSample2, listentry));
814     }
815     
816     /* free memory */
817     if (!VirtualFree(This->pMemory, 0, MEM_RELEASE))
818     {
819         ERR("Couldn't free memory. Error: %ld\n", GetLastError());
820         return HRESULT_FROM_WIN32(GetLastError());
821     }
822
823     return S_OK;
824 }
825
826 HRESULT StdMemAllocator_create(LPUNKNOWN lpUnkOuter, LPVOID * ppv)
827 {
828     StdMemAllocator * pMemAlloc;
829     HRESULT hr;
830
831     *ppv = NULL;
832     
833     if (lpUnkOuter)
834         return CLASS_E_NOAGGREGATION;
835
836     if (!(pMemAlloc = CoTaskMemAlloc(sizeof(*pMemAlloc))))
837         return E_OUTOFMEMORY;
838
839     pMemAlloc->pMemory = NULL;
840
841     if (SUCCEEDED(hr = BaseMemAllocator_Init(StdMemAllocator_Alloc, StdMemAllocator_Free, &pMemAlloc->base)))
842         *ppv = (LPVOID)pMemAlloc;
843     else
844         CoTaskMemFree(pMemAlloc);
845
846     return hr;
847 }