ntdll: Store memory views in a binary tree instead of a list.
[wine.git] / dlls / strmbase / mediatype.c
blob91c0ddc60adc5cd711580766a689c7d120bf5e04
1 /*
2 * Implementation of MedaType utility functions
4 * Copyright 2003 Robert Shearman
5 * Copyright 2010 Aric Stewart, CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
24 #include "dshow.h"
26 #include "wine/strmbase.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
31 HRESULT WINAPI CopyMediaType(AM_MEDIA_TYPE *dest, const AM_MEDIA_TYPE *src)
33 *dest = *src;
34 if (src->pbFormat)
36 dest->pbFormat = CoTaskMemAlloc(src->cbFormat);
37 if (!dest->pbFormat)
38 return E_OUTOFMEMORY;
39 memcpy(dest->pbFormat, src->pbFormat, src->cbFormat);
41 if (dest->pUnk)
42 IUnknown_AddRef(dest->pUnk);
43 return S_OK;
46 void WINAPI FreeMediaType(AM_MEDIA_TYPE * pMediaType)
48 if (pMediaType->pbFormat)
50 CoTaskMemFree(pMediaType->pbFormat);
51 pMediaType->pbFormat = NULL;
53 if (pMediaType->pUnk)
55 IUnknown_Release(pMediaType->pUnk);
56 pMediaType->pUnk = NULL;
60 AM_MEDIA_TYPE * WINAPI CreateMediaType(AM_MEDIA_TYPE const * pSrc)
62 AM_MEDIA_TYPE * pDest;
64 pDest = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
65 if (!pDest)
66 return NULL;
68 if (FAILED(CopyMediaType(pDest, pSrc)))
70 CoTaskMemFree(pDest);
71 return NULL;
74 return pDest;
77 void WINAPI DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
79 FreeMediaType(pMediaType);
80 CoTaskMemFree(pMediaType);
83 typedef struct tagENUMEDIADETAILS
85 ULONG cMediaTypes;
86 AM_MEDIA_TYPE * pMediaTypes;
87 } ENUMMEDIADETAILS;
89 typedef struct IEnumMediaTypesImpl
91 IEnumMediaTypes IEnumMediaTypes_iface;
92 LONG refCount;
93 BasePin *basePin;
94 BasePin_GetMediaType enumMediaFunction;
95 BasePin_GetMediaTypeVersion mediaVersionFunction;
96 LONG currentVersion;
97 ENUMMEDIADETAILS enumMediaDetails;
98 ULONG uIndex;
99 } IEnumMediaTypesImpl;
101 static inline IEnumMediaTypesImpl *impl_from_IEnumMediaTypes(IEnumMediaTypes *iface)
103 return CONTAINING_RECORD(iface, IEnumMediaTypesImpl, IEnumMediaTypes_iface);
106 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl;
108 HRESULT WINAPI EnumMediaTypes_Construct(BasePin *basePin, BasePin_GetMediaType enumFunc, BasePin_GetMediaTypeVersion versionFunc, IEnumMediaTypes ** ppEnum)
110 ULONG i;
111 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
112 AM_MEDIA_TYPE amt;
114 *ppEnum = NULL;
116 if (!pEnumMediaTypes)
117 return E_OUTOFMEMORY;
119 pEnumMediaTypes->IEnumMediaTypes_iface.lpVtbl = &IEnumMediaTypesImpl_Vtbl;
120 pEnumMediaTypes->refCount = 1;
121 pEnumMediaTypes->uIndex = 0;
122 pEnumMediaTypes->enumMediaFunction = enumFunc;
123 pEnumMediaTypes->mediaVersionFunction = versionFunc;
124 IPin_AddRef(&basePin->IPin_iface);
125 pEnumMediaTypes->basePin = basePin;
127 i = 0;
128 while (enumFunc(basePin, i, &amt) == S_OK)
130 FreeMediaType(&amt);
131 i++;
134 pEnumMediaTypes->enumMediaDetails.cMediaTypes = i;
135 pEnumMediaTypes->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
136 memset(pEnumMediaTypes->enumMediaDetails.pMediaTypes, 0, sizeof(AM_MEDIA_TYPE) * i);
137 for (i = 0; i < pEnumMediaTypes->enumMediaDetails.cMediaTypes; i++)
139 HRESULT hr;
141 if (FAILED(hr = enumFunc(basePin, i, &pEnumMediaTypes->enumMediaDetails.pMediaTypes[i])))
143 IEnumMediaTypes_Release(&pEnumMediaTypes->IEnumMediaTypes_iface);
144 return hr;
147 *ppEnum = &pEnumMediaTypes->IEnumMediaTypes_iface;
148 pEnumMediaTypes->currentVersion = versionFunc(basePin);
149 return S_OK;
152 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface, REFIID riid, void ** ret_iface)
154 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ret_iface);
156 if (IsEqualIID(riid, &IID_IUnknown) ||
157 IsEqualIID(riid, &IID_IEnumMediaTypes))
159 IEnumMediaTypes_AddRef(iface);
160 *ret_iface = iface;
161 return S_OK;
164 *ret_iface = NULL;
166 WARN("No interface for %s\n", debugstr_guid(riid));
168 return E_NOINTERFACE;
171 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
173 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
174 ULONG ref = InterlockedIncrement(&This->refCount);
176 TRACE("(%p)->(): new ref = %u\n", iface, ref);
178 return ref;
181 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
183 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
184 ULONG ref = InterlockedDecrement(&This->refCount);
186 TRACE("(%p)->(): new ref = %u\n", iface, ref);
188 if (!ref)
190 ULONG i;
191 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
192 FreeMediaType(&This->enumMediaDetails.pMediaTypes[i]);
193 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
194 IPin_Release(&This->basePin->IPin_iface);
195 CoTaskMemFree(This);
197 return ref;
200 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface, ULONG cMediaTypes, AM_MEDIA_TYPE ** ppMediaTypes, ULONG * pcFetched)
202 ULONG cFetched;
203 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
205 TRACE("(%p)->(%u, %p, %p)\n", iface, cMediaTypes, ppMediaTypes, pcFetched);
207 cFetched = min(This->enumMediaDetails.cMediaTypes, This->uIndex + cMediaTypes) - This->uIndex;
209 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
210 return VFW_E_ENUM_OUT_OF_SYNC;
212 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
214 if (cFetched > 0)
216 ULONG i;
217 for (i = 0; i < cFetched; i++)
218 if (!(ppMediaTypes[i] = CreateMediaType(&This->enumMediaDetails.pMediaTypes[This->uIndex + i])))
220 while (i--)
221 DeleteMediaType(ppMediaTypes[i]);
222 *pcFetched = 0;
223 return E_OUTOFMEMORY;
227 if ((cMediaTypes != 1) || pcFetched)
228 *pcFetched = cFetched;
230 This->uIndex += cFetched;
232 if (cFetched != cMediaTypes)
233 return S_FALSE;
234 return S_OK;
237 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface, ULONG cMediaTypes)
239 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
241 TRACE("(%p)->(%u)\n", iface, cMediaTypes);
243 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
244 return VFW_E_ENUM_OUT_OF_SYNC;
246 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
248 This->uIndex += cMediaTypes;
249 return S_OK;
251 return S_FALSE;
254 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
256 ULONG i;
257 AM_MEDIA_TYPE amt;
258 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
260 TRACE("(%p)->()\n", iface);
262 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
263 FreeMediaType(&This->enumMediaDetails.pMediaTypes[i]);
264 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
266 i = 0;
267 while (This->enumMediaFunction(This->basePin, i, &amt) == S_OK) i++;
269 This->enumMediaDetails.cMediaTypes = i;
270 This->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
271 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
273 This->enumMediaFunction(This->basePin, i,&amt);
274 if (FAILED(CopyMediaType(&This->enumMediaDetails.pMediaTypes[i], &amt)))
276 while (i--)
277 FreeMediaType(&This->enumMediaDetails.pMediaTypes[i]);
278 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
279 return E_OUTOFMEMORY;
283 This->currentVersion = This->mediaVersionFunction(This->basePin);
284 This->uIndex = 0;
286 return S_OK;
289 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface, IEnumMediaTypes ** ppEnum)
291 HRESULT hr;
292 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
294 TRACE("(%p)->(%p)\n", iface, ppEnum);
296 hr = EnumMediaTypes_Construct(This->basePin, This->enumMediaFunction, This->mediaVersionFunction, ppEnum);
297 if (FAILED(hr))
298 return hr;
299 return IEnumMediaTypes_Skip(*ppEnum, This->uIndex);
302 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
304 IEnumMediaTypesImpl_QueryInterface,
305 IEnumMediaTypesImpl_AddRef,
306 IEnumMediaTypesImpl_Release,
307 IEnumMediaTypesImpl_Next,
308 IEnumMediaTypesImpl_Skip,
309 IEnumMediaTypesImpl_Reset,
310 IEnumMediaTypesImpl_Clone