summaryrefslogtreecommitdiffstats
path: root/loader/dshow/allocator.c
blob: ce30e615d22ce4d1fa889aa801ceb21dd6d31645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "allocator.h"
#include "com.h"
#include "wine/winerror.h"
#include <stdio.h>

//#undef Debug
//#define Debug

using namespace std;

class AllocatorKeeper
{
public:
    AllocatorKeeper()
    {
	RegisterComClass(&CLSID_MemoryAllocator, MemAllocator::CreateAllocator);
    }
    ~AllocatorKeeper()
    {
	UnregisterComClass(&CLSID_MemoryAllocator, MemAllocator::CreateAllocator);
    }
};
static AllocatorKeeper keeper;


GUID MemAllocator::interfaces[]=
{
    IID_IUnknown,
    IID_IMemAllocator,
};

IMPLEMENT_IUNKNOWN(MemAllocator)

long MemAllocator::CreateAllocator(GUID* clsid, GUID* iid, void** ppv)
{
    if(!ppv)return -1;
    *ppv=0;
    if(memcmp(clsid, &CLSID_MemoryAllocator, sizeof(GUID)))
	return -1;

    IMemAllocator* p=new MemAllocator;
    int result=p->vt->QueryInterface((IUnknown*)p, iid, ppv);
    p->vt->Release((IUnknown*)p);
    return result;
}


static HRESULT STDCALL MemAllocator_SetProperties(IMemAllocator * This,
						  /* [in] */ ALLOCATOR_PROPERTIES *pRequest,
						  /* [out] */ ALLOCATOR_PROPERTIES *pActual)
{
    Debug printf("MemAllocator_SetProperties() called\n");
    if (!pRequest || !pActual)
	return E_INVALIDARG;
    if (pRequest->cBuffers<=0 || pRequest->cbBuffer<=0)
	return E_FAIL;
    MemAllocator* me = (MemAllocator*)This;
    if (me->used_list.size() || me->free_list.size())
	return E_FAIL;
    me->props = *pRequest;
    *pActual = *pRequest;
    return 0;
}

static HRESULT STDCALL MemAllocator_GetProperties(IMemAllocator * This,
						  /* [out] */ ALLOCATOR_PROPERTIES *pProps)
{
    Debug printf("MemAllocator_GetProperties(%p) called\n", This);
    if (!pProps)
	return E_INVALIDARG;
    if (((MemAllocator*)This)->props.cbBuffer<0)
	return E_FAIL;
    *pProps=((MemAllocator*)This)->props;
    return 0;
}

static HRESULT STDCALL MemAllocator_Commit(IMemAllocator * This)
{
    Debug printf("MemAllocator_Commit(%p) called\n", This);
    MemAllocator* me=(MemAllocator*)This;
    if (((MemAllocator*)This)->props.cbBuffer < 0)
	return E_FAIL;
    if (me->used_list.size() || me->free_list.size())
	return E_INVALIDARG;
    for(int i = 0; i<me->props.cBuffers; i++)
	me->free_list.push_back(new CMediaSample(me, me->props.cbBuffer));

    return 0;
}

static HRESULT STDCALL MemAllocator_Decommit(IMemAllocator * This)
{
    Debug printf("MemAllocator_Decommit(%p) called\n", This);
    MemAllocator* me=(MemAllocator*)This;
    list<CMediaSample*>::iterator it;
    for(it=me->free_list.begin(); it!=me->free_list.end(); it++)
	delete *it;
    for(it=me->used_list.begin(); it!=me->used_list.end(); it++)
	delete *it;
    me->free_list.clear();
    me->used_list.clear();
    return 0;
}

static HRESULT STDCALL MemAllocator_GetBuffer(IMemAllocator * This,
					      /* [out] */ IMediaSample **ppBuffer,
					      /* [in] */ REFERENCE_TIME *pStartTime,
					      /* [in] */ REFERENCE_TIME *pEndTime,
					      /* [in] */ DWORD dwFlags)
{
    Debug printf("MemAllocator_GetBuffer(%p) called\n", This);
    MemAllocator* me = (MemAllocator*)This;
    if (me->free_list.size() == 0)
    {
	Debug printf("No samples available\n");
	return E_FAIL;//should block here if no samples are available
    }
    list<CMediaSample*>::iterator it = me->free_list.begin();
    me->used_list.push_back(*it);
    *ppBuffer = *it;
    (*ppBuffer)->vt->AddRef((IUnknown*)*ppBuffer);
    if (me->new_pointer)
    {
	if(me->modified_sample)
	    me->modified_sample->ResetPointer();
	(*it)->SetPointer(me->new_pointer);
	me->modified_sample=*it;
	me->new_pointer = 0;
    }
    me->free_list.remove(*it);
    return 0;
}

static HRESULT STDCALL MemAllocator_ReleaseBuffer(IMemAllocator * This,
						  /* [in] */ IMediaSample *pBuffer)
{
    Debug printf("MemAllocator_ReleaseBuffer(%p) called\n", This);
    MemAllocator* me = (MemAllocator*)This;
    list<CMediaSample*>::iterator it;
    for (it = me->used_list.begin(); it != me->used_list.end(); it++)
	if (*it == pBuffer)
	{
	    me->used_list.erase(it);
	    me->free_list.push_back((CMediaSample*)pBuffer);
	    return 0;
	}
    Debug printf("Releasing unknown buffer\n");
    return E_FAIL;
}

MemAllocator::MemAllocator()
{
    Debug printf("MemAllocator::MemAllocator() called\n");
    vt = new IMemAllocator_vt;
    vt->QueryInterface = QueryInterface;
    vt->AddRef = AddRef;
    vt->Release = Release;
    vt->SetProperties = MemAllocator_SetProperties;
    vt->GetProperties = MemAllocator_GetProperties;
    vt->Commit = MemAllocator_Commit;
    vt->Decommit = MemAllocator_Decommit;
    vt->GetBuffer = MemAllocator_GetBuffer;
    vt->ReleaseBuffer = MemAllocator_ReleaseBuffer;

    refcount = 1;
    props.cBuffers = 1;
    props.cbBuffer = 65536; /* :/ */
    props.cbAlign = props.cbPrefix = 0;

    new_pointer=0;
    modified_sample=0;
}

MemAllocator::~MemAllocator()
{
    Debug printf("MemAllocator::~MemAllocator() called\n");
    delete vt;
}