wine.inf: Avoid creating empty registry values.
[wine/hacks.git] / dlls / avifil32 / getframe.c
blobdb05f853920b75fcb8e1200e56b573b33f8044cc
1 /*
2 * Copyright 2002-2003 Michael Günnewig
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.
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.
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "vfw.h"
27 #include "avifile_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
33 #ifndef DIBPTR
34 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
35 (lp)->biClrUsed * sizeof(RGBQUAD))
36 #endif
38 /***********************************************************************/
40 static HRESULT WINAPI IGetFrame_fnQueryInterface(IGetFrame *iface,
41 REFIID refiid, LPVOID *obj);
42 static ULONG WINAPI IGetFrame_fnAddRef(IGetFrame *iface);
43 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface);
44 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos);
45 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
46 LONG lEnd, LONG lRate);
47 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface);
48 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
49 LPBITMAPINFOHEADER lpbi,
50 LPVOID lpBits, INT x, INT y,
51 INT dx, INT dy);
53 static const struct IGetFrameVtbl igetframeVtbl = {
54 IGetFrame_fnQueryInterface,
55 IGetFrame_fnAddRef,
56 IGetFrame_fnRelease,
57 IGetFrame_fnGetFrame,
58 IGetFrame_fnBegin,
59 IGetFrame_fnEnd,
60 IGetFrame_fnSetFormat
63 typedef struct _IGetFrameImpl {
64 /* IUnknown stuff */
65 const IGetFrameVtbl *lpVtbl;
66 LONG ref;
68 /* IGetFrame stuff */
69 BOOL bFixedStream;
70 PAVISTREAM pStream;
72 LPVOID lpInBuffer;
73 LONG cbInBuffer;
74 LPBITMAPINFOHEADER lpInFormat;
75 LONG cbInFormat;
77 LONG lCurrentFrame;
78 LPBITMAPINFOHEADER lpOutFormat;
79 LPVOID lpOutBuffer;
81 HIC hic;
82 BOOL bResize;
83 DWORD x;
84 DWORD y;
85 DWORD dx;
86 DWORD dy;
88 BOOL bFormatChanges;
89 DWORD dwFormatChangeCount;
90 DWORD dwEditCount;
91 } IGetFrameImpl;
93 /***********************************************************************/
95 static void AVIFILE_CloseCompressor(IGetFrameImpl *This)
97 if (This->lpInFormat != This->lpOutFormat) {
98 HeapFree(GetProcessHeap(), 0, This->lpOutFormat);
99 This->lpOutFormat = NULL;
101 HeapFree(GetProcessHeap(), 0, This->lpInFormat);
102 This->lpInFormat = NULL;
103 if (This->hic != NULL) {
104 if (This->bResize)
105 ICDecompressExEnd(This->hic);
106 else
107 ICDecompressEnd(This->hic);
108 ICClose(This->hic);
109 This->hic = NULL;
113 PGETFRAME AVIFILE_CreateGetFrame(PAVISTREAM pStream)
115 IGetFrameImpl *pg;
117 /* check parameter */
118 if (pStream == NULL)
119 return NULL;
121 pg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IGetFrameImpl));
122 if (pg != NULL) {
123 pg->lpVtbl = &igetframeVtbl;
124 pg->ref = 1;
125 pg->lCurrentFrame = -1;
126 pg->pStream = pStream;
127 IAVIStream_AddRef(pStream);
130 return (PGETFRAME)pg;
133 static HRESULT WINAPI IGetFrame_fnQueryInterface(IGetFrame *iface,
134 REFIID refiid, LPVOID *obj)
136 IGetFrameImpl *This = (IGetFrameImpl *)iface;
138 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
140 if (IsEqualGUID(&IID_IUnknown, refiid) ||
141 IsEqualGUID(&IID_IGetFrame, refiid)) {
142 *obj = iface;
143 IGetFrame_AddRef(iface);
144 return S_OK;
147 return OLE_E_ENUM_NOMORE;
150 static ULONG WINAPI IGetFrame_fnAddRef(IGetFrame *iface)
152 IGetFrameImpl *This = (IGetFrameImpl *)iface;
153 ULONG ref = InterlockedIncrement(&This->ref);
155 TRACE("(%p)\n", iface);
157 return ref;
160 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface)
162 IGetFrameImpl *This = (IGetFrameImpl *)iface;
163 ULONG ref = InterlockedDecrement(&This->ref);
165 TRACE("(%p)\n", iface);
167 if (!ref) {
168 AVIFILE_CloseCompressor(This);
169 if (This->pStream != NULL) {
170 IAVIStream_Release(This->pStream);
171 This->pStream = NULL;
174 HeapFree(GetProcessHeap(), 0, iface);
175 return 0;
178 return ref;
181 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos)
183 IGetFrameImpl *This = (IGetFrameImpl *)iface;
185 LONG readBytes;
186 LONG readSamples;
188 TRACE("(%p,%d)\n", iface, lPos);
190 /* We don't want negative start values! -- marks invalid buffer content */
191 if (lPos < 0)
192 return NULL;
194 /* check state */
195 if (This->pStream == NULL)
196 return NULL;
197 if (This->lpInFormat == NULL)
198 return NULL;
200 /* Could stream have changed? */
201 if (! This->bFixedStream) {
202 AVISTREAMINFOW sInfo;
204 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
206 if (sInfo.dwEditCount != This->dwEditCount) {
207 This->dwEditCount = sInfo.dwEditCount;
208 This->lCurrentFrame = -1;
211 if (sInfo.dwFormatChangeCount != This->dwFormatChangeCount) {
212 /* stream has changed */
213 if (This->lpOutFormat != NULL) {
214 BITMAPINFOHEADER bi;
216 bi = *This->lpOutFormat;
217 AVIFILE_CloseCompressor(This);
219 if (FAILED(IGetFrame_SetFormat(iface, &bi, NULL, 0, 0, -1, -1))) {
220 if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
221 return NULL;
223 } else if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
224 return NULL;
228 if (lPos != This->lCurrentFrame) {
229 LONG lNext = IAVIStream_FindSample(This->pStream,lPos,FIND_KEY|FIND_PREV);
231 if (lNext == -1)
232 return NULL; /* frame doesn't exist */
233 if (lNext <= This->lCurrentFrame && This->lCurrentFrame < lPos)
234 lNext = This->lCurrentFrame + 1;
236 for (; lNext <= lPos; lNext++) {
237 /* new format for this frame? */
238 if (This->bFormatChanges) {
239 IAVIStream_ReadFormat(This->pStream, lNext,
240 This->lpInFormat, &This->cbInFormat);
241 if (This->lpOutFormat != NULL) {
242 if (This->lpOutFormat->biBitCount <= 8)
243 ICDecompressGetPalette(This->hic, This->lpInFormat,
244 This->lpOutFormat);
248 /* read input frame */
249 while (FAILED(AVIStreamRead(This->pStream, lNext, 1, This->lpInBuffer,
250 This->cbInBuffer, &readBytes, &readSamples))) {
251 /* not enough memory for input buffer? */
252 readBytes = 0;
253 if (FAILED(AVIStreamSampleSize(This->pStream, lNext, &readBytes)))
254 return NULL; /* bad thing, but bad things will happen */
255 if (readBytes <= 0) {
256 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
257 return NULL;
260 /* IAVIStream::Read failed because of other reasons not buffersize? */
261 if (This->cbInBuffer >= readBytes)
262 break;
263 This->cbInBuffer = This->cbInFormat + readBytes;
264 This->lpInFormat = HeapReAlloc(GetProcessHeap(), 0, This->lpInFormat, This->cbInBuffer);
265 if (This->lpInFormat == NULL)
266 return NULL; /* out of memory */
267 This->lpInBuffer = (BYTE*)This->lpInFormat + This->cbInFormat;
270 if (readSamples != 1) {
271 ERR(": no frames read\n");
272 return NULL;
274 if (readBytes != 0) {
275 This->lpInFormat->biSizeImage = readBytes;
277 /* nothing to decompress? */
278 if (This->hic == NULL) {
279 This->lCurrentFrame = lPos;
280 return This->lpInFormat;
283 if (This->bResize) {
284 ICDecompressEx(This->hic,0,This->lpInFormat,This->lpInBuffer,0,0,
285 This->lpInFormat->biWidth,This->lpInFormat->biHeight,
286 This->lpOutFormat,This->lpOutBuffer,This->x,This->y,
287 This->dx,This->dy);
288 } else {
289 ICDecompress(This->hic, 0, This->lpInFormat, This->lpInBuffer,
290 This->lpOutFormat, This->lpOutBuffer);
293 } /* for (lNext < lPos) */
294 } /* if (This->lCurrentFrame != lPos) */
296 return (This->hic == NULL ? This->lpInFormat : This->lpOutFormat);
299 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
300 LONG lEnd, LONG lRate)
302 IGetFrameImpl *This = (IGetFrameImpl *)iface;
304 TRACE("(%p,%d,%d,%d)\n", iface, lStart, lEnd, lRate);
306 This->bFixedStream = TRUE;
308 return (IGetFrame_GetFrame(iface, lStart) ? AVIERR_OK : AVIERR_ERROR);
311 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface)
313 IGetFrameImpl *This = (IGetFrameImpl *)iface;
315 TRACE("(%p)\n", iface);
317 This->bFixedStream = FALSE;
319 return AVIERR_OK;
322 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
323 LPBITMAPINFOHEADER lpbiWanted,
324 LPVOID lpBits, INT x, INT y,
325 INT dx, INT dy)
327 IGetFrameImpl *This = (IGetFrameImpl *)iface;
329 AVISTREAMINFOW sInfo;
330 LPBITMAPINFOHEADER lpbi = lpbiWanted;
331 BOOL bBestDisplay = FALSE;
333 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface, lpbiWanted, lpBits,
334 x, y, dx, dy);
336 if (This->pStream == NULL)
337 return AVIERR_ERROR;
339 if (lpbiWanted == (LPBITMAPINFOHEADER)AVIGETFRAMEF_BESTDISPLAYFMT) {
340 lpbi = NULL;
341 bBestDisplay = TRUE;
344 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
345 if (sInfo.fccType != streamtypeVIDEO)
346 return AVIERR_UNSUPPORTED;
348 This->bFormatChanges =
349 (sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES ? TRUE : FALSE );
350 This->dwFormatChangeCount = sInfo.dwFormatChangeCount;
351 This->dwEditCount = sInfo.dwEditCount;
352 This->lCurrentFrame = -1;
354 /* get input format from stream */
355 if (This->lpInFormat == NULL) {
356 HRESULT hr;
358 This->cbInBuffer = (LONG)sInfo.dwSuggestedBufferSize;
359 if (This->cbInBuffer == 0)
360 This->cbInBuffer = 1024;
362 IAVIStream_ReadFormat(This->pStream, sInfo.dwStart,
363 NULL, &This->cbInFormat);
365 This->lpInFormat = HeapAlloc(GetProcessHeap(), 0, This->cbInFormat + This->cbInBuffer);
366 if (This->lpInFormat == NULL) {
367 AVIFILE_CloseCompressor(This);
368 return AVIERR_MEMORY;
371 hr = IAVIStream_ReadFormat(This->pStream, sInfo.dwStart, This->lpInFormat, &This->cbInFormat);
372 if (FAILED(hr)) {
373 AVIFILE_CloseCompressor(This);
374 return hr;
377 This->lpInBuffer = ((LPBYTE)This->lpInFormat) + This->cbInFormat;
380 /* check input format */
381 if (This->lpInFormat->biClrUsed == 0 && This->lpInFormat->biBitCount <= 8)
382 This->lpInFormat->biClrUsed = 1u << This->lpInFormat->biBitCount;
383 if (This->lpInFormat->biSizeImage == 0 &&
384 This->lpInFormat->biCompression == BI_RGB) {
385 This->lpInFormat->biSizeImage =
386 DIBWIDTHBYTES(*This->lpInFormat) * This->lpInFormat->biHeight;
389 /* only to pass through? */
390 if (This->lpInFormat->biCompression == BI_RGB && lpBits == NULL) {
391 if (lpbi == NULL ||
392 (lpbi->biCompression == BI_RGB &&
393 lpbi->biWidth == This->lpInFormat->biWidth &&
394 lpbi->biHeight == This->lpInFormat->biHeight &&
395 lpbi->biBitCount == This->lpInFormat->biBitCount)) {
396 This->lpOutFormat = This->lpInFormat;
397 This->lpOutBuffer = DIBPTR(This->lpInFormat);
398 return AVIERR_OK;
402 /* need memory for output format? */
403 if (This->lpOutFormat == NULL) {
404 This->lpOutFormat =
405 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
406 if (This->lpOutFormat == NULL) {
407 AVIFILE_CloseCompressor(This);
408 return AVIERR_MEMORY;
412 /* need handle to video compressor */
413 if (This->hic == NULL) {
414 FOURCC fccHandler;
416 if (This->lpInFormat->biCompression == BI_RGB)
417 fccHandler = comptypeDIB;
418 else if (This->lpInFormat->biCompression == BI_RLE8)
419 fccHandler = mmioFOURCC('R','L','E',' ');
420 else
421 fccHandler = sInfo.fccHandler;
423 if (lpbi != NULL) {
424 if (lpbi->biWidth == 0)
425 lpbi->biWidth = This->lpInFormat->biWidth;
426 if (lpbi->biHeight == 0)
427 lpbi->biHeight = This->lpInFormat->biHeight;
430 This->hic = ICLocate(ICTYPE_VIDEO, fccHandler, This->lpInFormat, lpbi, ICMODE_DECOMPRESS);
431 if (This->hic == NULL) {
432 AVIFILE_CloseCompressor(This);
433 return AVIERR_NOCOMPRESSOR;
437 /* output format given? */
438 if (lpbi != NULL) {
439 /* check the given output format ... */
440 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
441 lpbi->biClrUsed = 1u << lpbi->biBitCount;
443 /* ... and remember it */
444 memcpy(This->lpOutFormat, lpbi,
445 lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD));
446 if (lpbi->biBitCount <= 8)
447 ICDecompressGetPalette(This->hic, This->lpInFormat, This->lpOutFormat);
449 return AVIERR_OK;
450 } else {
451 if (bBestDisplay) {
452 ICGetDisplayFormat(This->hic, This->lpInFormat,
453 This->lpOutFormat, 0, dx, dy);
454 } else if (ICDecompressGetFormat(This->hic, This->lpInFormat,
455 This->lpOutFormat) < 0) {
456 AVIFILE_CloseCompressor(This);
457 return AVIERR_NOCOMPRESSOR;
460 /* check output format */
461 if (This->lpOutFormat->biClrUsed == 0 &&
462 This->lpOutFormat->biBitCount <= 8)
463 This->lpOutFormat->biClrUsed = 1u << This->lpOutFormat->biBitCount;
464 if (This->lpOutFormat->biSizeImage == 0 &&
465 This->lpOutFormat->biCompression == BI_RGB) {
466 This->lpOutFormat->biSizeImage =
467 DIBWIDTHBYTES(*This->lpOutFormat) * This->lpOutFormat->biHeight;
470 if (lpBits == NULL) {
471 register DWORD size = This->lpOutFormat->biClrUsed * sizeof(RGBQUAD);
473 size += This->lpOutFormat->biSize + This->lpOutFormat->biSizeImage;
474 This->lpOutFormat = HeapReAlloc(GetProcessHeap(), 0, This->lpOutFormat, size);
475 if (This->lpOutFormat == NULL) {
476 AVIFILE_CloseCompressor(This);
477 return AVIERR_MEMORY;
479 This->lpOutBuffer = DIBPTR(This->lpOutFormat);
480 } else
481 This->lpOutBuffer = lpBits;
483 /* for user size was irrelevant */
484 if (dx == -1)
485 dx = This->lpOutFormat->biWidth;
486 if (dy == -1)
487 dy = This->lpOutFormat->biHeight;
489 /* need to resize? */
490 if (x != 0 || y != 0) {
491 if (dy == This->lpOutFormat->biHeight &&
492 dx == This->lpOutFormat->biWidth)
493 This->bResize = FALSE;
494 else
495 This->bResize = TRUE;
498 if (This->bResize) {
499 This->x = x;
500 This->y = y;
501 This->dx = dx;
502 This->dy = dy;
504 if (ICDecompressExBegin(This->hic,0,This->lpInFormat,This->lpInBuffer,0,
505 0,This->lpInFormat->biWidth,
506 This->lpInFormat->biHeight,This->lpOutFormat,
507 This->lpOutBuffer, x, y, dx, dy) == ICERR_OK)
508 return AVIERR_OK;
509 } else if (ICDecompressBegin(This->hic, This->lpInFormat,
510 This->lpOutFormat) == ICERR_OK)
511 return AVIERR_OK;
513 AVIFILE_CloseCompressor(This);
515 return AVIERR_COMPRESSOR;
519 /***********************************************************************/