wined3d: Make sure volumes have memory allocated.
[wine] / dlls / quartz / avidec.c
CommitLineData
f096fa3a
CC
1/*
2 * AVI Decompressor (VFW decompressors wrapper)
3 *
b0241781 4 * Copyright 2004-2005 Christian Costa
f096fa3a
CC
5 *
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.
10 *
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.
15 *
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
360a3f91 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
f096fa3a
CC
19 */
20
21#include "config.h"
22
23#include "quartz_private.h"
24#include "control_private.h"
25#include "pin.h"
26
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"
f096fa3a
CC
39
40#include <assert.h>
41
42#include "wine/unicode.h"
43#include "wine/debug.h"
44
b0241781 45#include "transform.h"
f096fa3a 46
b0241781 47WINE_DEFAULT_DEBUG_CHANNEL(quartz);
f096fa3a 48
56c701df
CC
49static HRESULT AVIDec_Cleanup(TransformFilterImpl* pTransformFilter);
50
f096fa3a
CC
51typedef struct AVIDecImpl
52{
b0241781 53 TransformFilterImpl tf;
f096fa3a 54 HIC hvid;
161a6b4d
CC
55 BITMAPINFOHEADER* pBihIn;
56 BITMAPINFOHEADER* pBihOut;
f096fa3a
CC
57} AVIDecImpl;
58
56c701df
CC
59static HRESULT AVIDec_ProcessBegin(TransformFilterImpl* pTransformFilter)
60{
61 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
62 DWORD result;
63
64 TRACE("(%p)->()\n", This);
65
66 result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
67 if (result != ICERR_OK)
68 {
cfbb859f 69 ERR("Cannot start processing (%d)\n", result);
56c701df
CC
70 return E_FAIL;
71 }
72 return S_OK;
73}
74
75static HRESULT AVIDec_ProcessSampleData(TransformFilterImpl* pTransformFilter, LPBYTE data, DWORD size)
f096fa3a 76{
72213b18 77 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
f096fa3a
CC
78 VIDEOINFOHEADER* format;
79 AM_MEDIA_TYPE amt;
f096fa3a
CC
80 HRESULT hr;
81 DWORD res;
82 IMediaSample* pSample = NULL;
83 DWORD cbDstStream;
84 LPBYTE pbDstStream;
85
cfbb859f 86 TRACE("(%p)->(%p,%d)\n", This, data, size);
b0241781
CC
87
88 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
f096fa3a 89 if (FAILED(hr)) {
dff5004b 90 ERR("Unable to retrieve media type\n");
f096fa3a
CC
91 goto error;
92 }
93 format = (VIDEOINFOHEADER*)amt.pbFormat;
94
161a6b4d
CC
95 /* Update input size to match sample size */
96 This->pBihIn->biSizeImage = size;
f096fa3a 97
b0241781 98 hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pSample, NULL, NULL, 0);
f096fa3a 99 if (FAILED(hr)) {
cfbb859f 100 ERR("Unable to get delivery buffer (%x)\n", hr);
f096fa3a
CC
101 goto error;
102 }
b0241781 103
f096fa3a
CC
104 hr = IMediaSample_SetActualDataLength(pSample, 0);
105 assert(hr == S_OK);
106
107 hr = IMediaSample_GetPointer(pSample, &pbDstStream);
108 if (FAILED(hr)) {
cfbb859f 109 ERR("Unable to get pointer to buffer (%x)\n", hr);
f096fa3a
CC
110 goto error;
111 }
112 cbDstStream = IMediaSample_GetSize(pSample);
161a6b4d 113 if (cbDstStream < This->pBihOut->biSizeImage) {
cfbb859f 114 ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
f096fa3a
CC
115 hr = E_FAIL;
116 goto error;
117 }
118
161a6b4d 119 res = ICDecompress(This->hvid, 0, This->pBihIn, data, This->pBihOut, pbDstStream);
f096fa3a 120 if (res != ICERR_OK)
cfbb859f 121 ERR("Error occurred during the decompression (%x)\n", res);
161a6b4d 122
b0241781 123 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pSample);
f096fa3a 124 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
cfbb859f 125 ERR("Error sending sample (%x)\n", hr);
f096fa3a
CC
126 goto error;
127 }
128
f096fa3a 129error:
f096fa3a
CC
130 if (pSample)
131 IMediaSample_Release(pSample);
132
133 return hr;
134}
135
56c701df
CC
136static HRESULT AVIDec_ProcessEnd(TransformFilterImpl* pTransformFilter)
137{
138 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
139 DWORD result;
140
141 TRACE("(%p)->()\n", This);
142
143 result = ICDecompressEnd(This->hvid);
144 if (result != ICERR_OK)
145 {
cfbb859f 146 ERR("Cannot stop processing (%d)\n", result);
56c701df
CC
147 return E_FAIL;
148 }
149 return S_OK;
150}
151
72213b18 152static HRESULT AVIDec_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
f096fa3a 153{
72213b18 154 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
56c701df 155 HRESULT hr = S_FALSE;
72213b18
CC
156
157 TRACE("(%p)->(%p)\n", This, pmt);
f096fa3a 158
56c701df 159 AVIDec_Cleanup(pTransformFilter);
04423c4f
AT
160
161 /* Check root (GUID w/o FOURCC) */
f096fa3a 162 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
04423c4f 163 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)) &&
161a6b4d 164 (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo)))
f096fa3a 165 {
f096fa3a 166 VIDEOINFOHEADER* format = (VIDEOINFOHEADER*)pmt->pbFormat;
b0241781 167
56c701df
CC
168 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, &format->bmiHeader, NULL, ICMODE_DECOMPRESS);
169 if (This->hvid)
161a6b4d 170 {
72213b18 171 AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
161a6b4d
CC
172 const CLSID* outsubtype;
173 DWORD bih_size;
56c701df
CC
174 DWORD output_depth = format->bmiHeader.biBitCount;
175 DWORD result;
161a6b4d
CC
176
177 switch(format->bmiHeader.biBitCount)
f096fa3a 178 {
161a6b4d 179 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
f096fa3a
CC
180 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
181 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
182 case 8: outsubtype = &MEDIASUBTYPE_RGB8; break;
183 default:
56c701df
CC
184 TRACE("Non standard input depth %d, forced ouptut depth to 32\n", format->bmiHeader.biBitCount);
185 outsubtype = &MEDIASUBTYPE_RGB32;
186 output_depth = 32;
187 break;
f096fa3a 188 }
161a6b4d
CC
189
190 /* Copy bitmap header from media type to 1 for input and 1 for output */
161a6b4d 191 bih_size = format->bmiHeader.biSize + format->bmiHeader.biClrUsed * 4;
72213b18
CC
192 This->pBihIn = (BITMAPINFOHEADER*)CoTaskMemAlloc(bih_size);
193 if (!This->pBihIn)
161a6b4d 194 {
56c701df
CC
195 hr = E_OUTOFMEMORY;
196 goto failed;
161a6b4d 197 }
72213b18
CC
198 This->pBihOut = (BITMAPINFOHEADER*)CoTaskMemAlloc(bih_size);
199 if (!This->pBihOut)
161a6b4d 200 {
56c701df
CC
201 hr = E_OUTOFMEMORY;
202 goto failed;
161a6b4d 203 }
72213b18
CC
204 memcpy(This->pBihIn, &format->bmiHeader, bih_size);
205 memcpy(This->pBihOut, &format->bmiHeader, bih_size);
161a6b4d
CC
206
207 /* Update output format as non compressed bitmap */
72213b18 208 This->pBihOut->biCompression = 0;
56c701df 209 This->pBihOut->biBitCount = output_depth;
72213b18 210 This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
161a6b4d 211
56c701df
CC
212 result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
213 if (result != ICERR_OK)
214 {
cfbb859f 215 TRACE("Unable to found a suitable output format (%d)\n", result);
56c701df
CC
216 goto failed;
217 }
218
219 /* Update output media type */
220 CopyMediaType(outpmt, pmt);
221 outpmt->subtype = *outsubtype;
09b49669 222 memcpy(&(((VIDEOINFOHEADER*)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
56c701df 223
45f111a2 224 /* Update buffer size of media samples in output */
72213b18 225 ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pBihOut->biSizeImage;
b0241781 226
161a6b4d 227 TRACE("Connection accepted\n");
f096fa3a 228 return S_OK;
161a6b4d 229 }
f096fa3a
CC
230 TRACE("Unable to find a suitable VFW decompressor\n");
231 }
f096fa3a 232
56c701df
CC
233failed:
234 AVIDec_Cleanup(pTransformFilter);
235
b0241781 236 TRACE("Connection refused\n");
56c701df 237 return hr;
f096fa3a
CC
238}
239
72213b18 240static HRESULT AVIDec_Cleanup(TransformFilterImpl* pTransformFilter)
f096fa3a 241{
72213b18
CC
242 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
243
244 TRACE("(%p)->()\n", This);
245
246 if (This->hvid)
247 ICClose(This->hvid);
56c701df 248 if (This->pBihIn)
72213b18 249 CoTaskMemFree(This->pBihIn);
56c701df 250 if (This->pBihOut)
72213b18 251 CoTaskMemFree(This->pBihOut);
f096fa3a 252
72213b18
CC
253 This->hvid = NULL;
254 This->pBihIn = NULL;
56c701df 255 This->pBihOut = NULL;
f096fa3a 256
b0241781 257 return S_OK;
f096fa3a
CC
258}
259
c78621ed 260static const TransformFuncsTable AVIDec_FuncsTable = {
56c701df
CC
261 AVIDec_ProcessBegin,
262 AVIDec_ProcessSampleData,
263 AVIDec_ProcessEnd,
264 AVIDec_ConnectInput,
265 AVIDec_Cleanup
266};
267
f096fa3a
CC
268HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
269{
270 HRESULT hr;
72213b18 271 AVIDecImpl * This;
f096fa3a
CC
272
273 TRACE("(%p, %p)\n", pUnkOuter, ppv);
274
275 *ppv = NULL;
276
277 if (pUnkOuter)
278 return CLASS_E_NOAGGREGATION;
b0241781
CC
279
280 /* Note: This memory is managed by the transform filter once created */
72213b18 281 This = CoTaskMemAlloc(sizeof(AVIDecImpl));
f096fa3a 282
72213b18
CC
283 This->hvid = NULL;
284 This->pBihIn = NULL;
56c701df 285 This->pBihOut = NULL;
f096fa3a 286
56c701df 287 hr = TransformFilter_Create(&(This->tf), &CLSID_AVIDec, &AVIDec_FuncsTable);
f096fa3a 288
b0241781
CC
289 if (FAILED(hr))
290 return hr;
f096fa3a 291
72213b18 292 *ppv = (LPVOID)This;
f096fa3a 293
f096fa3a
CC
294 return hr;
295}