mshtml: Added IHTMLObjectElement::get_vspace implementation.
[wine/multimedia.git] / dlls / strmbase / mediatype.c
blobb9255c30596606e862c240d92eb5da94150803a7
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 * pDest, const AM_MEDIA_TYPE *pSrc)
33 *pDest = *pSrc;
34 if (!pSrc->pbFormat) return S_OK;
35 if (!(pDest->pbFormat = CoTaskMemAlloc(pSrc->cbFormat)))
36 return E_OUTOFMEMORY;
37 memcpy(pDest->pbFormat, pSrc->pbFormat, pSrc->cbFormat);
38 if (pDest->pUnk)
39 IUnknown_AddRef(pDest->pUnk);
40 return S_OK;
43 void WINAPI FreeMediaType(AM_MEDIA_TYPE * pMediaType)
45 if (pMediaType->pbFormat)
47 CoTaskMemFree(pMediaType->pbFormat);
48 pMediaType->pbFormat = NULL;
50 if (pMediaType->pUnk)
52 IUnknown_Release(pMediaType->pUnk);
53 pMediaType->pUnk = NULL;
57 AM_MEDIA_TYPE * WINAPI CreateMediaType(AM_MEDIA_TYPE const * pSrc)
59 AM_MEDIA_TYPE * pDest;
61 pDest = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
62 if (!pDest)
63 return NULL;
65 if (FAILED(CopyMediaType(pDest, pSrc)))
67 CoTaskMemFree(pDest);
68 return NULL;
71 return pDest;
74 void WINAPI DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
76 FreeMediaType(pMediaType);
77 CoTaskMemFree(pMediaType);
80 typedef struct tagENUMEDIADETAILS
82 ULONG cMediaTypes;
83 AM_MEDIA_TYPE * pMediaTypes;
84 } ENUMMEDIADETAILS;
86 typedef struct IEnumMediaTypesImpl
88 const IEnumMediaTypesVtbl * lpVtbl;
89 LONG refCount;
90 BasePin *basePin;
91 BasePin_GetMediaType enumMediaFunction;
92 BasePin_GetMediaTypeVersion mediaVersionFunction;
93 LONG currentVersion;
94 ENUMMEDIADETAILS enumMediaDetails;
95 ULONG uIndex;
96 } IEnumMediaTypesImpl;
98 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl;
100 HRESULT WINAPI EnumMediaTypes_Construct(BasePin *basePin, BasePin_GetMediaType enumFunc, BasePin_GetMediaTypeVersion versionFunc, IEnumMediaTypes ** ppEnum)
102 ULONG i;
103 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
104 AM_MEDIA_TYPE amt;
106 if (!pEnumMediaTypes)
108 *ppEnum = NULL;
109 return E_OUTOFMEMORY;
111 pEnumMediaTypes->lpVtbl = &IEnumMediaTypesImpl_Vtbl;
112 pEnumMediaTypes->refCount = 1;
113 pEnumMediaTypes->uIndex = 0;
114 pEnumMediaTypes->enumMediaFunction = enumFunc;
115 pEnumMediaTypes->mediaVersionFunction = versionFunc;
116 IPin_AddRef((IPin*)basePin);
117 pEnumMediaTypes->basePin = basePin;
119 i = 0;
120 while (enumFunc(basePin,i,&amt) == S_OK) i++;
122 pEnumMediaTypes->enumMediaDetails.cMediaTypes = i;
123 pEnumMediaTypes->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
124 for (i = 0; i < pEnumMediaTypes->enumMediaDetails.cMediaTypes; i++)
126 enumFunc(basePin,i,&amt);
127 if (FAILED(CopyMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i], &amt)))
129 while (i--)
130 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes[i].pbFormat);
131 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes);
132 return E_OUTOFMEMORY;
135 *ppEnum = (IEnumMediaTypes *)(&pEnumMediaTypes->lpVtbl);
136 pEnumMediaTypes->currentVersion = versionFunc(basePin);
137 return S_OK;
140 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface, REFIID riid, LPVOID * ppv)
142 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
144 *ppv = NULL;
146 if (IsEqualIID(riid, &IID_IUnknown))
147 *ppv = iface;
148 else if (IsEqualIID(riid, &IID_IEnumMediaTypes))
149 *ppv = iface;
151 if (*ppv)
153 IUnknown_AddRef((IUnknown *)(*ppv));
154 return S_OK;
157 FIXME("No interface for %s!\n", debugstr_guid(riid));
159 return E_NOINTERFACE;
162 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
164 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
165 ULONG refCount = InterlockedIncrement(&This->refCount);
167 TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
169 return refCount;
172 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
174 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
175 ULONG refCount = InterlockedDecrement(&This->refCount);
177 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
179 if (!refCount)
181 ULONG i;
182 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
183 if (This->enumMediaDetails.pMediaTypes[i].pbFormat)
184 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
185 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
186 IPin_Release((IPin*)This->basePin);
187 CoTaskMemFree(This);
189 return refCount;
192 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface, ULONG cMediaTypes, AM_MEDIA_TYPE ** ppMediaTypes, ULONG * pcFetched)
194 ULONG cFetched;
195 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
197 cFetched = min(This->enumMediaDetails.cMediaTypes, This->uIndex + cMediaTypes) - This->uIndex;
199 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
200 return VFW_E_ENUM_OUT_OF_SYNC;
202 TRACE("(%u, %p, %p)\n", cMediaTypes, ppMediaTypes, pcFetched);
203 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
205 if (cFetched > 0)
207 ULONG i;
208 for (i = 0; i < cFetched; i++)
209 if (!(ppMediaTypes[i] = CreateMediaType(&This->enumMediaDetails.pMediaTypes[This->uIndex + i])))
211 while (i--)
212 DeleteMediaType(ppMediaTypes[i]);
213 *pcFetched = 0;
214 return E_OUTOFMEMORY;
218 if ((cMediaTypes != 1) || pcFetched)
219 *pcFetched = cFetched;
221 This->uIndex += cFetched;
223 if (cFetched != cMediaTypes)
224 return S_FALSE;
225 return S_OK;
228 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface, ULONG cMediaTypes)
230 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
232 TRACE("(%u)\n", cMediaTypes);
233 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
234 return VFW_E_ENUM_OUT_OF_SYNC;
236 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
238 This->uIndex += cMediaTypes;
239 return S_OK;
241 return S_FALSE;
244 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
246 ULONG i;
247 AM_MEDIA_TYPE amt;
248 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
250 TRACE("()\n");
252 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
253 if (This->enumMediaDetails.pMediaTypes[i].pbFormat)
254 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
255 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
257 i = 0;
258 while (This->enumMediaFunction(This->basePin, i,&amt) == S_OK) i++;
260 This->enumMediaDetails.cMediaTypes = i;
261 This->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
262 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
264 This->enumMediaFunction(This->basePin, i,&amt);
265 if (FAILED(CopyMediaType(&This->enumMediaDetails.pMediaTypes[i], &amt)))
267 while (i--)
268 CoTaskMemFree(This->enumMediaDetails.pMediaTypes[i].pbFormat);
269 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
270 return E_OUTOFMEMORY;
274 This->currentVersion = This->mediaVersionFunction(This->basePin);
275 This->uIndex = 0;
277 return S_OK;
280 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface, IEnumMediaTypes ** ppEnum)
282 HRESULT hr;
283 IEnumMediaTypesImpl *This = (IEnumMediaTypesImpl *)iface;
285 TRACE("(%p)\n", ppEnum);
287 hr = EnumMediaTypes_Construct(This->basePin, This->enumMediaFunction, This->mediaVersionFunction, ppEnum);
288 if (FAILED(hr))
289 return hr;
290 return IEnumMediaTypes_Skip(*ppEnum, This->uIndex);
293 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
295 IEnumMediaTypesImpl_QueryInterface,
296 IEnumMediaTypesImpl_AddRef,
297 IEnumMediaTypesImpl_Release,
298 IEnumMediaTypesImpl_Next,
299 IEnumMediaTypesImpl_Skip,
300 IEnumMediaTypesImpl_Reset,
301 IEnumMediaTypesImpl_Clone