dinput: Use BOOL type where appropriate.
[wine.git] / dlls / strmbase / mediatype.c
blobd100cc9e8c99fe307ad9b80f00deb929051624bb
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 if (!pEnumMediaTypes)
116 *ppEnum = NULL;
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) i++;
130 pEnumMediaTypes->enumMediaDetails.cMediaTypes = i;
131 pEnumMediaTypes->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
132 for (i = 0; i < pEnumMediaTypes->enumMediaDetails.cMediaTypes; i++)
134 enumFunc(basePin,i,&amt);
135 if (FAILED(CopyMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i], &amt)))
137 while (i--)
138 FreeMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i]);
139 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes);
140 return E_OUTOFMEMORY;
143 *ppEnum = &pEnumMediaTypes->IEnumMediaTypes_iface;
144 pEnumMediaTypes->currentVersion = versionFunc(basePin);
145 return S_OK;
148 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface, REFIID riid, void ** ret_iface)
150 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ret_iface);
152 if (IsEqualIID(riid, &IID_IUnknown) ||
153 IsEqualIID(riid, &IID_IEnumMediaTypes))
155 IEnumMediaTypes_AddRef(iface);
156 *ret_iface = iface;
157 return S_OK;
160 *ret_iface = NULL;
162 WARN("No interface for %s\n", debugstr_guid(riid));
164 return E_NOINTERFACE;
167 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
169 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
170 ULONG ref = InterlockedIncrement(&This->refCount);
172 TRACE("(%p)->(): new ref = %u\n", iface, ref);
174 return ref;
177 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
179 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
180 ULONG ref = InterlockedDecrement(&This->refCount);
182 TRACE("(%p)->(): new ref = %u\n", iface, ref);
184 if (!ref)
186 ULONG i;
187 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
188 FreeMediaType(&This->enumMediaDetails.pMediaTypes[i]);
189 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
190 IPin_Release(&This->basePin->IPin_iface);
191 CoTaskMemFree(This);
193 return ref;
196 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface, ULONG cMediaTypes, AM_MEDIA_TYPE ** ppMediaTypes, ULONG * pcFetched)
198 ULONG cFetched;
199 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
201 TRACE("(%p)->(%u, %p, %p)\n", iface, cMediaTypes, ppMediaTypes, pcFetched);
203 cFetched = min(This->enumMediaDetails.cMediaTypes, This->uIndex + cMediaTypes) - This->uIndex;
205 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
206 return VFW_E_ENUM_OUT_OF_SYNC;
208 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
210 if (cFetched > 0)
212 ULONG i;
213 for (i = 0; i < cFetched; i++)
214 if (!(ppMediaTypes[i] = CreateMediaType(&This->enumMediaDetails.pMediaTypes[This->uIndex + i])))
216 while (i--)
217 DeleteMediaType(ppMediaTypes[i]);
218 *pcFetched = 0;
219 return E_OUTOFMEMORY;
223 if ((cMediaTypes != 1) || pcFetched)
224 *pcFetched = cFetched;
226 This->uIndex += cFetched;
228 if (cFetched != cMediaTypes)
229 return S_FALSE;
230 return S_OK;
233 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface, ULONG cMediaTypes)
235 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
237 TRACE("(%p)->(%u)\n", iface, cMediaTypes);
239 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
240 return VFW_E_ENUM_OUT_OF_SYNC;
242 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
244 This->uIndex += cMediaTypes;
245 return S_OK;
247 return S_FALSE;
250 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
252 ULONG i;
253 AM_MEDIA_TYPE amt;
254 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
256 TRACE("(%p)->()\n", iface);
258 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
259 FreeMediaType(&This->enumMediaDetails.pMediaTypes[i]);
260 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
262 i = 0;
263 while (This->enumMediaFunction(This->basePin, i, &amt) == S_OK) i++;
265 This->enumMediaDetails.cMediaTypes = i;
266 This->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
267 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
269 This->enumMediaFunction(This->basePin, i,&amt);
270 if (FAILED(CopyMediaType(&This->enumMediaDetails.pMediaTypes[i], &amt)))
272 while (i--)
273 FreeMediaType(&This->enumMediaDetails.pMediaTypes[i]);
274 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
275 return E_OUTOFMEMORY;
279 This->currentVersion = This->mediaVersionFunction(This->basePin);
280 This->uIndex = 0;
282 return S_OK;
285 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface, IEnumMediaTypes ** ppEnum)
287 HRESULT hr;
288 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
290 TRACE("(%p)->(%p)\n", iface, ppEnum);
292 hr = EnumMediaTypes_Construct(This->basePin, This->enumMediaFunction, This->mediaVersionFunction, ppEnum);
293 if (FAILED(hr))
294 return hr;
295 return IEnumMediaTypes_Skip(*ppEnum, This->uIndex);
298 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
300 IEnumMediaTypesImpl_QueryInterface,
301 IEnumMediaTypesImpl_AddRef,
302 IEnumMediaTypesImpl_Release,
303 IEnumMediaTypesImpl_Next,
304 IEnumMediaTypesImpl_Skip,
305 IEnumMediaTypesImpl_Reset,
306 IEnumMediaTypesImpl_Clone