d100cc9e8c99fe307ad9b80f00deb929051624bb
[reactos.git] / reactos / lib / 3rdparty / strmbase / mediatype.c
1 /*
2 * Implementation of MedaType utility functions
3 *
4 * Copyright 2003 Robert Shearman
5 * Copyright 2010 Aric Stewart, CodeWeavers
6 *
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.
11 *
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.
16 *
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
20 */
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #include "dshow.h"
25
26 #include "wine/strmbase.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
30
31 HRESULT WINAPI CopyMediaType(AM_MEDIA_TYPE *dest, const AM_MEDIA_TYPE *src)
32 {
33 *dest = *src;
34 if (src->pbFormat)
35 {
36 dest->pbFormat = CoTaskMemAlloc(src->cbFormat);
37 if (!dest->pbFormat)
38 return E_OUTOFMEMORY;
39 memcpy(dest->pbFormat, src->pbFormat, src->cbFormat);
40 }
41 if (dest->pUnk)
42 IUnknown_AddRef(dest->pUnk);
43 return S_OK;
44 }
45
46 void WINAPI FreeMediaType(AM_MEDIA_TYPE * pMediaType)
47 {
48 if (pMediaType->pbFormat)
49 {
50 CoTaskMemFree(pMediaType->pbFormat);
51 pMediaType->pbFormat = NULL;
52 }
53 if (pMediaType->pUnk)
54 {
55 IUnknown_Release(pMediaType->pUnk);
56 pMediaType->pUnk = NULL;
57 }
58 }
59
60 AM_MEDIA_TYPE * WINAPI CreateMediaType(AM_MEDIA_TYPE const * pSrc)
61 {
62 AM_MEDIA_TYPE * pDest;
63
64 pDest = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
65 if (!pDest)
66 return NULL;
67
68 if (FAILED(CopyMediaType(pDest, pSrc)))
69 {
70 CoTaskMemFree(pDest);
71 return NULL;
72 }
73
74 return pDest;
75 }
76
77 void WINAPI DeleteMediaType(AM_MEDIA_TYPE * pMediaType)
78 {
79 FreeMediaType(pMediaType);
80 CoTaskMemFree(pMediaType);
81 }
82
83 typedef struct tagENUMEDIADETAILS
84 {
85 ULONG cMediaTypes;
86 AM_MEDIA_TYPE * pMediaTypes;
87 } ENUMMEDIADETAILS;
88
89 typedef struct IEnumMediaTypesImpl
90 {
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;
100
101 static inline IEnumMediaTypesImpl *impl_from_IEnumMediaTypes(IEnumMediaTypes *iface)
102 {
103 return CONTAINING_RECORD(iface, IEnumMediaTypesImpl, IEnumMediaTypes_iface);
104 }
105
106 static const struct IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl;
107
108 HRESULT WINAPI EnumMediaTypes_Construct(BasePin *basePin, BasePin_GetMediaType enumFunc, BasePin_GetMediaTypeVersion versionFunc, IEnumMediaTypes ** ppEnum)
109 {
110 ULONG i;
111 IEnumMediaTypesImpl * pEnumMediaTypes = CoTaskMemAlloc(sizeof(IEnumMediaTypesImpl));
112 AM_MEDIA_TYPE amt;
113
114 if (!pEnumMediaTypes)
115 {
116 *ppEnum = NULL;
117 return E_OUTOFMEMORY;
118 }
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;
126
127 i = 0;
128 while (enumFunc(basePin, i, &amt) == S_OK) i++;
129
130 pEnumMediaTypes->enumMediaDetails.cMediaTypes = i;
131 pEnumMediaTypes->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
132 for (i = 0; i < pEnumMediaTypes->enumMediaDetails.cMediaTypes; i++)
133 {
134 enumFunc(basePin,i,&amt);
135 if (FAILED(CopyMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i], &amt)))
136 {
137 while (i--)
138 FreeMediaType(&pEnumMediaTypes->enumMediaDetails.pMediaTypes[i]);
139 CoTaskMemFree(pEnumMediaTypes->enumMediaDetails.pMediaTypes);
140 return E_OUTOFMEMORY;
141 }
142 }
143 *ppEnum = &pEnumMediaTypes->IEnumMediaTypes_iface;
144 pEnumMediaTypes->currentVersion = versionFunc(basePin);
145 return S_OK;
146 }
147
148 static HRESULT WINAPI IEnumMediaTypesImpl_QueryInterface(IEnumMediaTypes * iface, REFIID riid, void ** ret_iface)
149 {
150 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ret_iface);
151
152 if (IsEqualIID(riid, &IID_IUnknown) ||
153 IsEqualIID(riid, &IID_IEnumMediaTypes))
154 {
155 IEnumMediaTypes_AddRef(iface);
156 *ret_iface = iface;
157 return S_OK;
158 }
159
160 *ret_iface = NULL;
161
162 WARN("No interface for %s\n", debugstr_guid(riid));
163
164 return E_NOINTERFACE;
165 }
166
167 static ULONG WINAPI IEnumMediaTypesImpl_AddRef(IEnumMediaTypes * iface)
168 {
169 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
170 ULONG ref = InterlockedIncrement(&This->refCount);
171
172 TRACE("(%p)->(): new ref = %u\n", iface, ref);
173
174 return ref;
175 }
176
177 static ULONG WINAPI IEnumMediaTypesImpl_Release(IEnumMediaTypes * iface)
178 {
179 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
180 ULONG ref = InterlockedDecrement(&This->refCount);
181
182 TRACE("(%p)->(): new ref = %u\n", iface, ref);
183
184 if (!ref)
185 {
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);
192 }
193 return ref;
194 }
195
196 static HRESULT WINAPI IEnumMediaTypesImpl_Next(IEnumMediaTypes * iface, ULONG cMediaTypes, AM_MEDIA_TYPE ** ppMediaTypes, ULONG * pcFetched)
197 {
198 ULONG cFetched;
199 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
200
201 TRACE("(%p)->(%u, %p, %p)\n", iface, cMediaTypes, ppMediaTypes, pcFetched);
202
203 cFetched = min(This->enumMediaDetails.cMediaTypes, This->uIndex + cMediaTypes) - This->uIndex;
204
205 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
206 return VFW_E_ENUM_OUT_OF_SYNC;
207
208 TRACE("Next uIndex: %u, cFetched: %u\n", This->uIndex, cFetched);
209
210 if (cFetched > 0)
211 {
212 ULONG i;
213 for (i = 0; i < cFetched; i++)
214 if (!(ppMediaTypes[i] = CreateMediaType(&This->enumMediaDetails.pMediaTypes[This->uIndex + i])))
215 {
216 while (i--)
217 DeleteMediaType(ppMediaTypes[i]);
218 *pcFetched = 0;
219 return E_OUTOFMEMORY;
220 }
221 }
222
223 if ((cMediaTypes != 1) || pcFetched)
224 *pcFetched = cFetched;
225
226 This->uIndex += cFetched;
227
228 if (cFetched != cMediaTypes)
229 return S_FALSE;
230 return S_OK;
231 }
232
233 static HRESULT WINAPI IEnumMediaTypesImpl_Skip(IEnumMediaTypes * iface, ULONG cMediaTypes)
234 {
235 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
236
237 TRACE("(%p)->(%u)\n", iface, cMediaTypes);
238
239 if (This->currentVersion != This->mediaVersionFunction(This->basePin))
240 return VFW_E_ENUM_OUT_OF_SYNC;
241
242 if (This->uIndex + cMediaTypes < This->enumMediaDetails.cMediaTypes)
243 {
244 This->uIndex += cMediaTypes;
245 return S_OK;
246 }
247 return S_FALSE;
248 }
249
250 static HRESULT WINAPI IEnumMediaTypesImpl_Reset(IEnumMediaTypes * iface)
251 {
252 ULONG i;
253 AM_MEDIA_TYPE amt;
254 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
255
256 TRACE("(%p)->()\n", iface);
257
258 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
259 FreeMediaType(&This->enumMediaDetails.pMediaTypes[i]);
260 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
261
262 i = 0;
263 while (This->enumMediaFunction(This->basePin, i, &amt) == S_OK) i++;
264
265 This->enumMediaDetails.cMediaTypes = i;
266 This->enumMediaDetails.pMediaTypes = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * i);
267 for (i = 0; i < This->enumMediaDetails.cMediaTypes; i++)
268 {
269 This->enumMediaFunction(This->basePin, i,&amt);
270 if (FAILED(CopyMediaType(&This->enumMediaDetails.pMediaTypes[i], &amt)))
271 {
272 while (i--)
273 FreeMediaType(&This->enumMediaDetails.pMediaTypes[i]);
274 CoTaskMemFree(This->enumMediaDetails.pMediaTypes);
275 return E_OUTOFMEMORY;
276 }
277 }
278
279 This->currentVersion = This->mediaVersionFunction(This->basePin);
280 This->uIndex = 0;
281
282 return S_OK;
283 }
284
285 static HRESULT WINAPI IEnumMediaTypesImpl_Clone(IEnumMediaTypes * iface, IEnumMediaTypes ** ppEnum)
286 {
287 HRESULT hr;
288 IEnumMediaTypesImpl *This = impl_from_IEnumMediaTypes(iface);
289
290 TRACE("(%p)->(%p)\n", iface, ppEnum);
291
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);
296 }
297
298 static const IEnumMediaTypesVtbl IEnumMediaTypesImpl_Vtbl =
299 {
300 IEnumMediaTypesImpl_QueryInterface,
301 IEnumMediaTypesImpl_AddRef,
302 IEnumMediaTypesImpl_Release,
303 IEnumMediaTypesImpl_Next,
304 IEnumMediaTypesImpl_Skip,
305 IEnumMediaTypesImpl_Reset,
306 IEnumMediaTypesImpl_Clone
307 };