summaryrefslogtreecommitdiffstats
path: root/loader/dshow/allocator.c
blob: 4323e4a176a82f7c808fee40fb6cc303e9ea191c (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Modified for use with MPlayer, detailed changelog at
 * http://svn.mplayerhq.hu/mplayer/trunk/
 */

#include "config.h"
#include "allocator.h"
#include "com.h"
#include "wine/winerror.h"
#include <stdio.h>
#include <stdlib.h>

static int AllocatorKeeper = 0;

struct avm_list_t
{
    struct avm_list_t* next;
    struct avm_list_t* prev;
    void* member;
};

static inline int avm_list_size(avm_list_t* head)
{
    avm_list_t* it = head;
    int i = 0;
    if (it)
    {
	for (;;)
	{
            i++;
	    it = it->next;
	    if (it == head)
                break;
	}
    }
    return i;
}

static inline int avm_list_print(avm_list_t* head)
{
    avm_list_t* it = head;
    int i = 0;
    printf("Head: %p\n", head);
    if (it)
    {
	for (;;)
	{
	    i++;
	    printf("%d:  member: %p    next: %p  prev: %p\n",
		   i, it->member, it->next, it->prev);
	    it = it->next;
	    if (it == head)
                break;
	}
    }
    return i;
}

static inline avm_list_t* avm_list_add_head(avm_list_t* head, void* member)
{
    avm_list_t* n = (avm_list_t*) malloc(sizeof(avm_list_t));
    n->member = member;

    if (!head)
    {
	head = n;
        head->prev = head;
    }

    n->prev = head->prev;
    head->prev = n;
    n->next = head;

    return n;
}

static inline avm_list_t* avm_list_add_tail(avm_list_t* head, void* member)
{
    avm_list_t* n = avm_list_add_head(head, member);
    return (!head) ? n : head;
}

static inline avm_list_t* avm_list_del_head(avm_list_t* head)
{
    avm_list_t* n = 0;

    if (head)
    {
	if (head->next != head)
	{
	    n = head->next;
	    head->prev->next = head->next;
	    head->next->prev = head->prev;
	}
	free(head);
    }
    return n;
}

static inline avm_list_t* avm_list_find(avm_list_t* head, void* member)
{
    avm_list_t* it = head;
    if (it)
    {
	for (;;)
	{
	    if (it->member == member)
		return it;
	    it = it->next;
	    if (it == head)
                break;
	}
    }
    return NULL;
}

static long MemAllocator_CreateAllocator(GUID* clsid, const GUID* iid, void** ppv)
{
    IMemAllocator* p;
    int result;
    if (!ppv)
	return -1;
    *ppv = 0;
    if (memcmp(clsid, &CLSID_MemoryAllocator, sizeof(GUID)))
	return -1;

    p = (IMemAllocator*) MemAllocatorCreate();
    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)
{
    MemAllocator* me = (MemAllocator*)This;
    Debug printf("MemAllocator_SetProperties(%p) called\n", This);
    if (!pRequest || !pActual)
	return E_INVALIDARG;
    if (pRequest->cBuffers<=0 || pRequest->cbBuffer<=0)
	return E_FAIL;
    if (me->used_list != 0 || me->free_list != 0)
	return E_FAIL;

    *pActual = *pRequest;
    /*
       DirectShow DOCS ("Negotiating Allocators" chapter) says that allocator might not
       honor the requested properties. Thus, since WMSP audio codecs requests bufer with two 
       bytes length for unknown reason, we should correct requested value. Otherwise above
       codec don't want to load.
    */
    if (pActual->cbBuffer == 2)
        pActual->cbBuffer = 10240; //Enough for WMSP codec

    me->props = *pActual;

    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)
{
    MemAllocator* me = (MemAllocator*)This;
    int i;
    Debug printf("MemAllocator_Commit(%p) called\n", This);
    if (((MemAllocator*)This)->props.cbBuffer < 0)
	return E_FAIL;
    if (me->used_list || me->free_list)
	return E_INVALIDARG;
    for (i = 0; i < me->props.cBuffers; i++)
    {
	CMediaSample* sample = CMediaSampleCreate((IMemAllocator*)me,
						  me->props.cbBuffer);
	if (!sample)
            return E_OUTOFMEMORY;
	//printf("FREEEEEEEEEEEE ADDED %p\n", sample);
	me->free_list = avm_list_add_tail(me->free_list, sample);
	//avm_list_print(me->free_list);
    }

    //printf("Added mem %p: lsz: %d  %d  size: %d\n", me, avm_list_size(me->free_list), me->props.cBuffers, me->props.cbBuffer);
    return 0;
}

static HRESULT STDCALL MemAllocator_Decommit(IMemAllocator * This)
{
    MemAllocator* me=(MemAllocator*)This;
    Debug printf("MemAllocator_Decommit(%p) called\n", This);
    //printf("Deleted mem %p: %d  %d\n", me, me->free_list.size(), me->used_list.size());
    while (me->used_list)
    {
	me->free_list = avm_list_add_tail(me->free_list,
					  (CMediaSample*) me->used_list->member);
	me->used_list = avm_list_del_head(me->used_list);
    }

    while (me->free_list)
    {
        CMediaSample* sample = (CMediaSample*) me->free_list->member;
	//printf("****************** Decommiting FREE %p\n", sample);
	//sample->vt->Release((IUnknown*)sample);
	CMediaSample_Destroy((CMediaSample*)sample);
	me->free_list = avm_list_del_head(me->free_list);
    }

    return 0;
}

static HRESULT STDCALL MemAllocator_GetBuffer(IMemAllocator * This,
					      /* [out] */ IMediaSample **ppBuffer,
					      /* [in] */ REFERENCE_TIME *pStartTime,
					      /* [in] */ REFERENCE_TIME *pEndTime,
					      /* [in] */ DWORD dwFlags)
{
    MemAllocator* me = (MemAllocator*)This;
    CMediaSample* sample;
    Debug printf("MemAllocator_ReleaseBuffer(%p) called   %d  %d\n", This,
		 avm_list_size(me->used_list), avm_list_size(me->free_list));

    if (!me->free_list)
    {
	Debug printf("No samples available\n");
	return E_FAIL;//should block here if no samples are available
    }

    sample = (CMediaSample*) me->free_list->member;
    me->free_list = avm_list_del_head(me->free_list);
    me->used_list = avm_list_add_tail(me->used_list, sample);

    *ppBuffer = (IMediaSample*) sample;
    sample->vt->AddRef((IUnknown*) sample);
    if (me->new_pointer)
    {
	if (me->modified_sample)
	    me->modified_sample->ResetPointer(me->modified_sample);
	sample->SetPointer(sample, me->new_pointer);
	me->modified_sample = sample;
	me->new_pointer = 0;
    }
    return 0;
}

static HRESULT STDCALL MemAllocator_ReleaseBuffer(IMemAllocator* This,
						  /* [in] */ IMediaSample* pBuffer)
{
    avm_list_t* l;
    MemAllocator* me = (MemAllocator*)This;
    Debug printf("MemAllocator_ReleaseBuffer(%p) called   %d  %d\n", This,
		 avm_list_size(me->used_list), avm_list_size(me->free_list));

    l = avm_list_find(me->used_list, pBuffer);
    if (l)
    {
	CMediaSample* sample = (CMediaSample*) l->member;
	if (me->modified_sample == sample)
	{
	    me->modified_sample->ResetPointer(me->modified_sample);
	    me->modified_sample = 0;
	}
	me->used_list = avm_list_del_head(me->used_list);
	me->free_list = avm_list_add_head(me->free_list, sample);
	//printf("****************** RELEASED OK %p  %p\n", me->used_list, me->free_list);
	return 0;
    }
    Debug printf("MemAllocator_ReleaseBuffer(%p) releasing unknown buffer!!!! %p\n", This, pBuffer);
    return E_FAIL;
}


static void MemAllocator_SetPointer(MemAllocator* This, char* pointer)
{
    This->new_pointer = pointer;
}

static void MemAllocator_ResetPointer(MemAllocator* This)
{
    if (This->modified_sample)
    {
	This->modified_sample->ResetPointer(This->modified_sample);
	This->modified_sample = 0;
    }
}

static void MemAllocator_Destroy(MemAllocator* This)
{
    Debug printf("MemAllocator_Destroy(%p) called  (%d, %d)\n", This, This->refcount, AllocatorKeeper);
#ifdef WIN32_LOADER
    if (--AllocatorKeeper == 0)
	UnregisterComClass(&CLSID_MemoryAllocator, MemAllocator_CreateAllocator);
#endif
    free(This->vt);
    free(This);
}

IMPLEMENT_IUNKNOWN(MemAllocator)

MemAllocator* MemAllocatorCreate()
{
    MemAllocator* This = (MemAllocator*) malloc(sizeof(MemAllocator));

    if (!This)
        return NULL;

    Debug printf("MemAllocatorCreate() called -> %p\n", This);

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

    This->vt = (IMemAllocator_vt*) malloc(sizeof(IMemAllocator_vt));

    if (!This->vt)
    {
        free(This);
	return NULL;
    }

    This->vt->QueryInterface = MemAllocator_QueryInterface;
    This->vt->AddRef = MemAllocator_AddRef;
    This->vt->Release = MemAllocator_Release;
    This->vt->SetProperties = MemAllocator_SetProperties;
    This->vt->GetProperties = MemAllocator_GetProperties;
    This->vt->Commit = MemAllocator_Commit;
    This->vt->Decommit = MemAllocator_Decommit;
    This->vt->GetBuffer = MemAllocator_GetBuffer;
    This->vt->ReleaseBuffer = MemAllocator_ReleaseBuffer;

    This->SetPointer = MemAllocator_SetPointer;
    This->ResetPointer = MemAllocator_ResetPointer;

    This->modified_sample = 0;
    This->new_pointer = 0;
    This->used_list = 0;
    This->free_list = 0;

    This->interfaces[0]=IID_IUnknown;
    This->interfaces[1]=IID_IMemAllocator;

#ifdef WIN32_LOADER
    if (AllocatorKeeper++ == 0)
	RegisterComClass(&CLSID_MemoryAllocator, MemAllocator_CreateAllocator);
#endif

    return This;
}