shell32: Add missing '\n' to Wine trace.
[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 <assert.h>
20 #include <stdarg.h>
21
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winuser.h"
26 #include "winnls.h"
27 #include "winerror.h"
28 #include "mmsystem.h"
29 #include "vfw.h"
30
31 #include "avifile_private.h"
32 #include "extrachunk.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
37
38 /***********************************************************************/
39
40 static HRESULT WINAPI ITmpFile_fnQueryInterface(IAVIFile* iface,REFIID refiid,LPVOID *obj);
41 static ULONG   WINAPI ITmpFile_fnAddRef(IAVIFile* iface);
42 static ULONG   WINAPI ITmpFile_fnRelease(IAVIFile* iface);
43 static HRESULT WINAPI ITmpFile_fnInfo(IAVIFile*iface,AVIFILEINFOW*afi,LONG size);
44 static HRESULT WINAPI ITmpFile_fnGetStream(IAVIFile*iface,PAVISTREAM*avis,DWORD fccType,LONG lParam);
45 static HRESULT WINAPI ITmpFile_fnCreateStream(IAVIFile*iface,PAVISTREAM*avis,AVISTREAMINFOW*asi);
46 static HRESULT WINAPI ITmpFile_fnWriteData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG size);
47 static HRESULT WINAPI ITmpFile_fnReadData(IAVIFile*iface,DWORD ckid,LPVOID lpData,LONG *size);
48 static HRESULT WINAPI ITmpFile_fnEndRecord(IAVIFile*iface);
49 static HRESULT WINAPI ITmpFile_fnDeleteStream(IAVIFile*iface,DWORD fccType,LONG lParam);
50
51 static const struct IAVIFileVtbl itmpft = {
52   ITmpFile_fnQueryInterface,
53   ITmpFile_fnAddRef,
54   ITmpFile_fnRelease,
55   ITmpFile_fnInfo,
56   ITmpFile_fnGetStream,
57   ITmpFile_fnCreateStream,
58   ITmpFile_fnWriteData,
59   ITmpFile_fnReadData,
60   ITmpFile_fnEndRecord,
61   ITmpFile_fnDeleteStream
62 };
63
64 typedef struct _ITmpFileImpl {
65   /* IUnknown stuff */
66   const IAVIFileVtbl *lpVtbl;
67   LONG         ref;
68
69   /* IAVIFile stuff */
70   AVIFILEINFOW  fInfo;
71   PAVISTREAM   *ppStreams;
72 } ITmpFileImpl;
73
74 PAVIFILE AVIFILE_CreateAVITempFile(int nStreams, PAVISTREAM *ppStreams) {
75   ITmpFileImpl *tmpFile;
76   int           i;
77
78   tmpFile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ITmpFileImpl));
79   if (tmpFile == NULL)
80     return NULL;
81
82   tmpFile->lpVtbl = &itmpft;
83   tmpFile->ref    = 1;
84   memset(&tmpFile->fInfo, 0, sizeof(tmpFile->fInfo));
85
86   tmpFile->fInfo.dwStreams = nStreams;
87   tmpFile->ppStreams = HeapAlloc(GetProcessHeap(), 0, nStreams * sizeof(PAVISTREAM));
88   if (tmpFile->ppStreams == NULL) {
89     HeapFree(GetProcessHeap(), 0, tmpFile);
90     return NULL;
91   }
92
93   for (i = 0; i < nStreams; i++) {
94     AVISTREAMINFOW sInfo;
95
96     tmpFile->ppStreams[i] = ppStreams[i];
97
98     AVIStreamAddRef(ppStreams[i]);
99     AVIStreamInfoW(ppStreams[i], &sInfo, sizeof(sInfo));
100     if (i == 0) {
101       tmpFile->fInfo.dwScale = sInfo.dwScale;
102       tmpFile->fInfo.dwRate  = sInfo.dwRate;
103       if (!sInfo.dwScale || !sInfo.dwRate) {
104         tmpFile->fInfo.dwScale = 1;
105         tmpFile->fInfo.dwRate  = 100;
106       }
107     }
108
109     if (tmpFile->fInfo.dwSuggestedBufferSize < sInfo.dwSuggestedBufferSize)
110       tmpFile->fInfo.dwSuggestedBufferSize = sInfo.dwSuggestedBufferSize;
111
112     {
113       register DWORD tmp;
114
115       tmp = MulDiv(AVIStreamSampleToTime(ppStreams[i], sInfo.dwLength), \
116                    tmpFile->fInfo.dwScale, tmpFile->fInfo.dwRate * 1000);
117       if (tmpFile->fInfo.dwLength < tmp)
118         tmpFile->fInfo.dwLength = tmp;
119
120       tmp = sInfo.rcFrame.right - sInfo.rcFrame.left;
121       if (tmpFile->fInfo.dwWidth < tmp)
122         tmpFile->fInfo.dwWidth = tmp;
123       tmp = sInfo.rcFrame.bottom - sInfo.rcFrame.top;
124       if (tmpFile->fInfo.dwHeight < tmp)
125         tmpFile->fInfo.dwHeight = tmp;
126     }
127   }
128
129   return (PAVIFILE)tmpFile;
130 }
131
132 static HRESULT WINAPI ITmpFile_fnQueryInterface(IAVIFile *iface, REFIID refiid,
133                                                 LPVOID *obj)
134 {
135   ITmpFileImpl *This = (ITmpFileImpl *)iface;
136
137   TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
138
139   if (IsEqualGUID(&IID_IUnknown, refiid) ||
140       IsEqualGUID(&IID_IAVIFile, refiid)) {
141     *obj = iface;
142     IAVIFile_AddRef(iface);
143
144     return S_OK;
145   }
146
147   return OLE_E_ENUM_NOMORE;
148 }
149
150 static ULONG   WINAPI ITmpFile_fnAddRef(IAVIFile *iface)
151 {
152   ITmpFileImpl *This = (ITmpFileImpl *)iface;
153   ULONG ref = InterlockedIncrement(&This->ref);
154
155   TRACE("(%p) -> %d\n", iface, ref);
156
157   return ref;
158 }
159
160 static ULONG   WINAPI ITmpFile_fnRelease(IAVIFile *iface)
161 {
162   ITmpFileImpl *This = (ITmpFileImpl *)iface;
163   ULONG ref = InterlockedDecrement(&This->ref);
164
165   TRACE("(%p) -> %d\n", iface, ref);
166
167   if (!ref) {
168     unsigned int i;
169
170     for (i = 0; i < This->fInfo.dwStreams; i++) {
171       if (This->ppStreams[i] != NULL) {
172         AVIStreamRelease(This->ppStreams[i]);
173
174         This->ppStreams[i] = NULL;
175       }
176     }
177
178     HeapFree(GetProcessHeap(), 0, This);
179     return 0;
180   }
181
182   return ref;
183 }
184
185 static HRESULT WINAPI ITmpFile_fnInfo(IAVIFile *iface,
186                                       AVIFILEINFOW *afi, LONG size)
187 {
188   ITmpFileImpl *This = (ITmpFileImpl *)iface;
189
190   TRACE("(%p,%p,%d)\n",iface,afi,size);
191
192   if (afi == NULL)
193     return AVIERR_BADPARAM;
194   if (size < 0)
195     return AVIERR_BADSIZE;
196
197   memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo)));
198
199   if ((DWORD)size < sizeof(This->fInfo))
200     return AVIERR_BUFFERTOOSMALL;
201   return AVIERR_OK;
202 }
203
204 static HRESULT WINAPI ITmpFile_fnGetStream(IAVIFile *iface, PAVISTREAM *avis,
205                                            DWORD fccType, LONG lParam)
206 {
207   ITmpFileImpl *This = (ITmpFileImpl *)iface;
208
209   ULONG nStream = (ULONG)-1;
210
211   TRACE("(%p,%p,0x%08X,%d)\n", iface, avis, fccType, lParam);
212
213   if (avis == NULL || lParam < 0)
214     return AVIERR_BADPARAM;
215
216   if (fccType != streamtypeANY) {
217     /* search the number of the specified stream */
218     ULONG i;
219
220     for (i = 0; i < This->fInfo.dwStreams; i++) {
221       AVISTREAMINFOW sInfo;
222       HRESULT        hr;
223
224       hr = AVIStreamInfoW(This->ppStreams[i], &sInfo, sizeof(sInfo));
225       if (FAILED(hr))
226         return hr;
227
228       if (sInfo.fccType == fccType) {
229         if (lParam == 0) {
230           nStream = i;
231           break;
232         } else
233           lParam--;
234       }
235     }
236   } else
237     nStream = lParam;
238
239   /* Does the requested stream exist ? */
240   if (nStream < This->fInfo.dwStreams && This->ppStreams[nStream] != NULL) {
241     *avis = This->ppStreams[nStream];
242     AVIStreamAddRef(*avis);
243
244     return AVIERR_OK;
245   }
246
247   /* Sorry, but the specified stream doesn't exist */
248   return AVIERR_NODATA;
249 }
250
251 static HRESULT WINAPI ITmpFile_fnCreateStream(IAVIFile *iface,PAVISTREAM *avis,
252                                               AVISTREAMINFOW *asi)
253 {
254   TRACE("(%p,%p,%p)\n",iface,avis,asi);
255
256   return AVIERR_UNSUPPORTED;
257 }
258
259 static HRESULT WINAPI ITmpFile_fnWriteData(IAVIFile *iface, DWORD ckid,
260                                            LPVOID lpData, LONG size)
261 {
262   TRACE("(%p,0x%08X,%p,%d)\n", iface, ckid, lpData, size);
263
264   return AVIERR_UNSUPPORTED;
265 }
266
267 static HRESULT WINAPI ITmpFile_fnReadData(IAVIFile *iface, DWORD ckid,
268                                           LPVOID lpData, LONG *size)
269 {
270   TRACE("(%p,0x%08X,%p,%p)\n", iface, ckid, lpData, size);
271
272   return AVIERR_UNSUPPORTED;
273 }
274
275 static HRESULT WINAPI ITmpFile_fnEndRecord(IAVIFile *iface)
276 {
277   TRACE("(%p)\n",iface);
278
279   return AVIERR_OK;
280 }
281
282 static HRESULT WINAPI ITmpFile_fnDeleteStream(IAVIFile *iface, DWORD fccType,
283                                               LONG lParam)
284 {
285   TRACE("(%p,0x%08X,%d)\n", iface, fccType, lParam);
286
287   return AVIERR_UNSUPPORTED;
288 }