Implement ldap_search* functions.
[wine/multimedia.git] / dlls / quartz / avidec.c
blobec60b6f24cde530ae8de10e8287b3c0a4441f6cd
1 /*
2 * AVI Decompressor (VFW decompressors wrapper)
4 * Copyright 2004-2005 Christian Costa
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include "quartz_private.h"
24 #include "control_private.h"
25 #include "pin.h"
27 #include "uuids.h"
28 #include "aviriff.h"
29 #include "mmreg.h"
30 #include "vfwmsgs.h"
31 #include "amvideo.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "dshow.h"
35 #include "strmif.h"
36 #include "vfwmsgs.h"
37 #include "evcode.h"
38 #include "vfw.h"
40 #include <assert.h>
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
45 #include "transform.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
49 static HRESULT AVIDec_Cleanup(TransformFilterImpl* pTransformFilter);
51 typedef struct AVIDecImpl
53 TransformFilterImpl tf;
54 HIC hvid;
55 BITMAPINFOHEADER* pBihIn;
56 BITMAPINFOHEADER* pBihOut;
57 } AVIDecImpl;
59 static HRESULT AVIDec_ProcessBegin(TransformFilterImpl* pTransformFilter)
61 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
62 DWORD result;
64 TRACE("(%p)->()\n", This);
66 result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
67 if (result != ICERR_OK)
69 ERR("Cannot start processing (%ld)\n", result);
70 return E_FAIL;
72 return S_OK;
75 static HRESULT AVIDec_ProcessSampleData(TransformFilterImpl* pTransformFilter, LPBYTE data, DWORD size)
77 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
78 VIDEOINFOHEADER* format;
79 AM_MEDIA_TYPE amt;
80 HRESULT hr;
81 DWORD res;
82 IMediaSample* pSample = NULL;
83 DWORD cbDstStream;
84 LPBYTE pbDstStream;
86 TRACE("(%p)->(%p,%ld)\n", This, data, size);
88 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
89 if (FAILED(hr)) {
90 ERR("Unable to retrieve media type\n");
91 goto error;
93 format = (VIDEOINFOHEADER*)amt.pbFormat;
95 /* Update input size to match sample size */
96 This->pBihIn->biSizeImage = size;
98 hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pSample, NULL, NULL, 0);
99 if (FAILED(hr)) {
100 ERR("Unable to get delivery buffer (%lx)\n", hr);
101 goto error;
104 hr = IMediaSample_SetActualDataLength(pSample, 0);
105 assert(hr == S_OK);
107 hr = IMediaSample_GetPointer(pSample, &pbDstStream);
108 if (FAILED(hr)) {
109 ERR("Unable to get pointer to buffer (%lx)\n", hr);
110 goto error;
112 cbDstStream = IMediaSample_GetSize(pSample);
113 if (cbDstStream < This->pBihOut->biSizeImage) {
114 ERR("Sample size is too small %ld < %ld\n", cbDstStream, This->pBihOut->biSizeImage);
115 hr = E_FAIL;
116 goto error;
119 res = ICDecompress(This->hvid, 0, This->pBihIn, data, This->pBihOut, pbDstStream);
120 if (res != ICERR_OK)
121 ERR("Error occurred during the decompression (%lx)\n", res);
123 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pSample);
124 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
125 ERR("Error sending sample (%lx)\n", hr);
126 goto error;
129 error:
130 if (pSample)
131 IMediaSample_Release(pSample);
133 return hr;
136 static HRESULT AVIDec_ProcessEnd(TransformFilterImpl* pTransformFilter)
138 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
139 DWORD result;
141 TRACE("(%p)->()\n", This);
143 result = ICDecompressEnd(This->hvid);
144 if (result != ICERR_OK)
146 ERR("Cannot stop processing (%ld)\n", result);
147 return E_FAIL;
149 return S_OK;
152 static HRESULT AVIDec_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
154 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
155 HRESULT hr = S_FALSE;
157 TRACE("(%p)->(%p)\n", This, pmt);
159 AVIDec_Cleanup(pTransformFilter);
161 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
162 (!memcmp(((char*)&pmt->subtype)+4, ((char*)&MEDIATYPE_Video)+4, sizeof(GUID)-4)) && /* Check root (GUID w/o FOURCC) */
163 (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo)))
165 VIDEOINFOHEADER* format = (VIDEOINFOHEADER*)pmt->pbFormat;
167 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, &format->bmiHeader, NULL, ICMODE_DECOMPRESS);
168 if (This->hvid)
170 AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
171 const CLSID* outsubtype;
172 DWORD bih_size;
173 DWORD output_depth = format->bmiHeader.biBitCount;
174 DWORD result;
176 switch(format->bmiHeader.biBitCount)
178 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
179 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
180 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
181 case 8: outsubtype = &MEDIASUBTYPE_RGB8; break;
182 default:
183 TRACE("Non standard input depth %d, forced ouptut depth to 32\n", format->bmiHeader.biBitCount);
184 outsubtype = &MEDIASUBTYPE_RGB32;
185 output_depth = 32;
186 break;
189 /* Copy bitmap header from media type to 1 for input and 1 for output */
190 bih_size = format->bmiHeader.biSize + format->bmiHeader.biClrUsed * 4;
191 This->pBihIn = (BITMAPINFOHEADER*)CoTaskMemAlloc(bih_size);
192 if (!This->pBihIn)
194 hr = E_OUTOFMEMORY;
195 goto failed;
197 This->pBihOut = (BITMAPINFOHEADER*)CoTaskMemAlloc(bih_size);
198 if (!This->pBihOut)
200 hr = E_OUTOFMEMORY;
201 goto failed;
203 memcpy(This->pBihIn, &format->bmiHeader, bih_size);
204 memcpy(This->pBihOut, &format->bmiHeader, bih_size);
206 /* Update output format as non compressed bitmap */
207 This->pBihOut->biCompression = 0;
208 This->pBihOut->biBitCount = output_depth;
209 This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
211 result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
212 if (result != ICERR_OK)
214 TRACE("Unable to found a suitable output format (%ld)\n", result);
215 goto failed;
218 /* Update output media type */
219 CopyMediaType(outpmt, pmt);
220 outpmt->subtype = *outsubtype;
221 memcpy(&(((VIDEOINFOHEADER*)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
223 /* Update buffer size of media samples in output */
224 ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pBihOut->biSizeImage;
226 TRACE("Connection accepted\n");
227 return S_OK;
229 TRACE("Unable to find a suitable VFW decompressor\n");
232 failed:
233 AVIDec_Cleanup(pTransformFilter);
235 TRACE("Connection refused\n");
236 return hr;
239 static HRESULT AVIDec_Cleanup(TransformFilterImpl* pTransformFilter)
241 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
243 TRACE("(%p)->()\n", This);
245 if (This->hvid)
246 ICClose(This->hvid);
247 if (This->pBihIn)
248 CoTaskMemFree(This->pBihIn);
249 if (This->pBihOut)
250 CoTaskMemFree(This->pBihOut);
252 This->hvid = NULL;
253 This->pBihIn = NULL;
254 This->pBihOut = NULL;
256 return S_OK;
259 TransformFuncsTable AVIDec_FuncsTable = {
260 AVIDec_ProcessBegin,
261 AVIDec_ProcessSampleData,
262 AVIDec_ProcessEnd,
263 AVIDec_ConnectInput,
264 AVIDec_Cleanup
267 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
269 HRESULT hr;
270 AVIDecImpl * This;
272 TRACE("(%p, %p)\n", pUnkOuter, ppv);
274 *ppv = NULL;
276 if (pUnkOuter)
277 return CLASS_E_NOAGGREGATION;
279 /* Note: This memory is managed by the transform filter once created */
280 This = CoTaskMemAlloc(sizeof(AVIDecImpl));
282 This->hvid = NULL;
283 This->pBihIn = NULL;
284 This->pBihOut = NULL;
286 hr = TransformFilter_Create(&(This->tf), &CLSID_AVIDec, &AVIDec_FuncsTable);
288 if (FAILED(hr))
289 return hr;
291 *ppv = (LPVOID)This;
293 return hr;