d3d9/tests: Add a software vertexprocessing buffer discard test.
[wine] / dlls / avifil32 / tmpfile.c
1 /*
2  * Copyright 2003 Michael Günnewig
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "winerror.h"
26 #include "vfw.h"
27
28 #include "avifile_private.h"
29 #include "extrachunk.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
34
35 /***********************************************************************/
36
37 typedef struct _ITmpFileImpl {
38   IAVIFile     IAVIFile_iface;
39   LONG         ref;
40
41   AVIFILEINFOW  fInfo;
42   PAVISTREAM   *ppStreams;
43 } ITmpFileImpl;
44
45 static inline ITmpFileImpl *impl_from_IAVIFile(IAVIFile *iface)
46 {
47   return CONTAINING_RECORD(iface, ITmpFileImpl, IAVIFile_iface);
48 }
49
50 static HRESULT WINAPI ITmpFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
51                                                 LPVOID *obj)
52 {
53   ITmpFileImpl *This = impl_from_IAVIFile(iface);
54
55   TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
56
57   if (IsEqualGUID(&IID_IUnknown, refiid) ||
58       IsEqualGUID(&IID_IAVIFile, refiid)) {
59     *obj = iface;
60     IAVIFile_AddRef(iface);
61
62     return S_OK;
63   }
64
65   return OLE_E_ENUM_NOMORE;
66 }
67
68 static ULONG   WINAPI ITmpFile_fnAddRef(IAVIFile *iface)
69 {
70   ITmpFileImpl *This = impl_from_IAVIFile(iface);
71   ULONG ref = InterlockedIncrement(&This->ref);
72
73   TRACE("(%p) -> %d\n", iface, ref);
74
75   return ref;
76 }
77
78 static ULONG   WINAPI ITmpFile_fnRelease(IAVIFile *iface)
79 {
80   ITmpFileImpl *This = impl_from_IAVIFile(iface);
81   ULONG ref = InterlockedDecrement(&This->ref);
82
83   TRACE("(%p) -> %d\n", iface, ref);
84
85   if (!ref) {
86     unsigned int i;
87
88     for (i = 0; i < This->fInfo.dwStreams; i++) {
89       if (This->ppStreams[i] != NULL) {
90         AVIStreamRelease(This->ppStreams[i]);
91
92         This->ppStreams[i] = NULL;
93       }
94     }
95
96     HeapFree(GetProcessHeap(), 0, This);
97     return 0;
98   }
99
100   return ref;
101 }
102
103 static HRESULT WINAPI ITmpFile_fnInfo(IAVIFile *iface,
104                                       AVIFILEINFOW *afi, LONG size)
105 {
106   ITmpFileImpl *This = impl_from_IAVIFile(iface);
107
108   TRACE("(%p,%p,%d)\n",iface,afi,size);
109
110   if (afi == NULL)
111     return AVIERR_BADPARAM;
112   if (size < 0)
113     return AVIERR_BADSIZE;
114
115   memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
116
117   if ((DWORD)size < sizeof(This->fInfo))
118     return AVIERR_BUFFERTOOSMALL;
119   return AVIERR_OK;
120 }
121
122 static HRESULT WINAPI ITmpFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
123                                            DWORD fccType, LONG lParam)
124 {
125   ITmpFileImpl *This = impl_from_IAVIFile(iface);
126
127   ULONG nStream = (ULONG)-1;
128
129   TRACE("(%p,%p,0x%08X,%d)\n", iface, avis, fccType, lParam);
130
131   if (avis == NULL || lParam < 0)
132     return AVIERR_BADPARAM;
133
134   if (fccType != streamtypeANY) {
135     /* search the number of the specified stream */
136     ULONG i;
137
138     for (i = 0; i < This->fInfo.dwStreams; i++) {
139       AVISTREAMINFOW sInfo;
140       HRESULT        hr;
141
142       hr = AVIStreamInfoW(This->ppStreams[i], &sInfo, sizeof(sInfo));
143       if (FAILED(hr))
144         return hr;
145
146       if (sInfo.fccType == fccType) {
147         if (lParam == 0) {
148           nStream = i;
149           break;
150         } else
151           lParam--;
152       }
153     }
154   } else
155     nStream = lParam;
156
157   /* Does the requested stream exist ? */
158   if (nStream < This->fInfo.dwStreams && This->ppStreams[nStream] != NULL) {
159     *avis = This->ppStreams[nStream];
160     AVIStreamAddRef(*avis);
161
162     return AVIERR_OK;
163   }
164
165   /* Sorry, but the specified stream doesn't exist */
166   return AVIERR_NODATA;
167 }
168
169 static HRESULT WINAPI ITmpFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
170                                               AVISTREAMINFOW *asi)
171 {
172   TRACE("(%p,%p,%p)\n",iface,avis,asi);
173
174   return AVIERR_UNSUPPORTED;
175 }
176
177 static HRESULT WINAPI ITmpFile_fnWriteData(IAVIFile *iface, DWORD ckid,
178                                            LPVOID lpData, LONG size)
179 {
180   TRACE("(%p,0x%08X,%p,%d)\n", iface, ckid, lpData, size);
181
182   return AVIERR_UNSUPPORTED;
183 }
184
185 static HRESULT WINAPI ITmpFile_fnReadData(IAVIFile *iface, DWORD ckid,
186                                           LPVOID lpData, LONG *size)
187 {
188   TRACE("(%p,0x%08X,%p,%p)\n", iface, ckid, lpData, size);
189
190   return AVIERR_UNSUPPORTED;
191 }
192
193 static HRESULT WINAPI ITmpFile_fnEndRecord(IAVIFile *iface)
194 {
195   TRACE("(%p)\n",iface);
196
197   return AVIERR_OK;
198 }
199
200 static HRESULT WINAPI ITmpFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
201                                               LONG lParam)
202 {
203   TRACE("(%p,0x%08X,%d)\n", iface, fccType, lParam);
204
205   return AVIERR_UNSUPPORTED;
206 }
207
208 static const struct IAVIFileVtbl itmpft = {
209   ITmpFile_fnQueryInterface,
210   ITmpFile_fnAddRef,
211   ITmpFile_fnRelease,
212   ITmpFile_fnInfo,
213   ITmpFile_fnGetStream,
214   ITmpFile_fnCreateStream,
215   ITmpFile_fnWriteData,
216   ITmpFile_fnReadData,
217   ITmpFile_fnEndRecord,
218   ITmpFile_fnDeleteStream
219 };
220
221 PAVIFILE AVIFILE_CreateAVITempFile(int nStreams, const PAVISTREAM *ppStreams)
222 {
223   ITmpFileImpl *tmpFile;
224   int           i;
225
226   tmpFile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ITmpFileImpl));
227   if (tmpFile == NULL)
228     return NULL;
229
230   tmpFile->IAVIFile_iface.lpVtbl = &itmpft;
231   tmpFile->ref    = 1;
232   memset(&tmpFile->fInfo, 0, sizeof(tmpFile->fInfo));
233
234   tmpFile->fInfo.dwStreams = nStreams;
235   tmpFile->ppStreams = HeapAlloc(GetProcessHeap(), 0, nStreams * sizeof(PAVISTREAM));
236   if (tmpFile->ppStreams == NULL) {
237     HeapFree(GetProcessHeap(), 0, tmpFile);
238     return NULL;
239   }
240
241   for (i = 0; i < nStreams; i++) {
242     AVISTREAMINFOW sInfo;
243
244     tmpFile->ppStreams[i] = ppStreams[i];
245
246     AVIStreamAddRef(ppStreams[i]);
247     AVIStreamInfoW(ppStreams[i], &sInfo, sizeof(sInfo));
248     if (i == 0) {
249       tmpFile->fInfo.dwScale = sInfo.dwScale;
250       tmpFile->fInfo.dwRate  = sInfo.dwRate;
251       if (!sInfo.dwScale || !sInfo.dwRate) {
252         tmpFile->fInfo.dwScale = 1;
253         tmpFile->fInfo.dwRate  = 100;
254       }
255     }
256
257     if (tmpFile->fInfo.dwSuggestedBufferSize < sInfo.dwSuggestedBufferSize)
258       tmpFile->fInfo.dwSuggestedBufferSize = sInfo.dwSuggestedBufferSize;
259
260     {
261       DWORD tmp;
262
263       tmp = MulDiv(AVIStreamSampleToTime(ppStreams[i], sInfo.dwLength),
264                    tmpFile->fInfo.dwScale, tmpFile->fInfo.dwRate * 1000);
265       if (tmpFile->fInfo.dwLength < tmp)
266         tmpFile->fInfo.dwLength = tmp;
267
268       tmp = sInfo.rcFrame.right - sInfo.rcFrame.left;
269       if (tmpFile->fInfo.dwWidth < tmp)
270         tmpFile->fInfo.dwWidth = tmp;
271       tmp = sInfo.rcFrame.bottom - sInfo.rcFrame.top;
272       if (tmpFile->fInfo.dwHeight < tmp)
273         tmpFile->fInfo.dwHeight = tmp;
274     }
275   }
276
277   return (PAVIFILE)tmpFile;
278 }