Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / avifil32 / getframe.c
blobb911c35cab4441660b89a99e29016971e960568b
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define COM_NO_WINDOWS_H
20 #include <assert.h>
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "windowsx.h"
27 #include "wingdi.h"
28 #include "vfw.h"
30 #include "avifile_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
36 #ifndef DIBPTR
37 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
38 (lp)->biClrUsed * sizeof(RGBQUAD))
39 #endif
41 /***********************************************************************/
43 static HRESULT WINAPI IGetFrame_fnQueryInterface(IGetFrame *iface,
44 REFIID refiid, LPVOID *obj);
45 static ULONG WINAPI IGetFrame_fnAddRef(IGetFrame *iface);
46 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface);
47 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos);
48 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
49 LONG lEnd, LONG lRate);
50 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface);
51 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
52 LPBITMAPINFOHEADER lpbi,
53 LPVOID lpBits, INT x, INT y,
54 INT dx, INT dy);
56 struct ICOM_VTABLE(IGetFrame) igetframeVtbl = {
57 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
58 IGetFrame_fnQueryInterface,
59 IGetFrame_fnAddRef,
60 IGetFrame_fnRelease,
61 IGetFrame_fnGetFrame,
62 IGetFrame_fnBegin,
63 IGetFrame_fnEnd,
64 IGetFrame_fnSetFormat
67 typedef struct _IGetFrameImpl {
68 /* IUnknown stuff */
69 ICOM_VFIELD(IGetFrame);
70 DWORD ref;
72 /* IGetFrame stuff */
73 BOOL bFixedStream;
74 PAVISTREAM pStream;
76 LPVOID lpInBuffer;
77 LONG cbInBuffer;
78 LPBITMAPINFOHEADER lpInFormat;
79 LONG cbInFormat;
81 LONG lCurrentFrame;
82 LPBITMAPINFOHEADER lpOutFormat;
83 LPVOID lpOutBuffer;
85 HIC hic;
86 BOOL bResize;
87 DWORD x;
88 DWORD y;
89 DWORD dx;
90 DWORD dy;
92 BOOL bFormatChanges;
93 DWORD dwFormatChangeCount;
94 DWORD dwEditCount;
95 } IGetFrameImpl;
97 /***********************************************************************/
99 static void AVIFILE_CloseCompressor(IGetFrameImpl *This)
101 if (This->lpOutFormat != NULL && This->lpInFormat != This->lpOutFormat) {
102 GlobalFreePtr(This->lpOutFormat);
103 This->lpOutFormat = NULL;
105 if (This->lpInFormat != NULL) {
106 GlobalFreePtr(This->lpInFormat);
107 This->lpInFormat = NULL;
109 if (This->hic != NULL) {
110 if (This->bResize)
111 ICDecompressExEnd(This->hic);
112 else
113 ICDecompressEnd(This->hic);
114 ICClose(This->hic);
115 This->hic = NULL;
119 PGETFRAME AVIFILE_CreateGetFrame(PAVISTREAM pStream)
121 IGetFrameImpl *pg;
123 /* check parameter */
124 if (pStream == NULL)
125 return NULL;
127 pg = (IGetFrameImpl*)LocalAlloc(LPTR, sizeof(IGetFrameImpl));
128 if (pg != NULL) {
129 pg->lpVtbl = &igetframeVtbl;
130 pg->ref = 1;
131 pg->lCurrentFrame = -1;
132 pg->pStream = pStream;
133 IAVIStream_AddRef(pStream);
136 return (PGETFRAME)pg;
139 static HRESULT WINAPI IGetFrame_fnQueryInterface(IGetFrame *iface,
140 REFIID refiid, LPVOID *obj)
142 ICOM_THIS(IGetFrameImpl,iface);
144 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
146 if (IsEqualGUID(&IID_IUnknown, refiid) ||
147 IsEqualGUID(&IID_IGetFrame, refiid)) {
148 *obj = iface;
149 return S_OK;
152 return OLE_E_ENUM_NOMORE;
155 static ULONG WINAPI IGetFrame_fnAddRef(IGetFrame *iface)
157 ICOM_THIS(IGetFrameImpl,iface);
159 TRACE("(%p)\n", iface);
161 return ++(This->ref);
164 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface)
166 ICOM_THIS(IGetFrameImpl,iface);
168 TRACE("(%p)\n", iface);
170 if (!--(This->ref)) {
171 AVIFILE_CloseCompressor(This);
172 if (This->pStream != NULL) {
173 AVIStreamRelease(This->pStream);
174 This->pStream = NULL;
177 LocalFree((HLOCAL)iface);
178 return 0;
181 return This->ref;
184 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos)
186 ICOM_THIS(IGetFrameImpl,iface);
188 LONG readBytes;
189 LONG readSamples;
191 TRACE("(%p,%ld)\n", iface, lPos);
193 /* check state */
194 if (This->pStream == NULL)
195 return NULL;
196 if (This->lpInFormat == NULL)
197 return NULL;
199 /* Could stream have changed? */
200 if (! This->bFixedStream) {
201 AVISTREAMINFOW sInfo;
203 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
205 if (sInfo.dwEditCount != This->dwEditCount) {
206 This->dwEditCount = sInfo.dwEditCount;
207 This->lCurrentFrame = -1;
210 if (sInfo.dwFormatChangeCount != This->dwFormatChangeCount) {
211 /* stream has changed */
212 if (This->lpOutFormat != NULL) {
213 BITMAPINFOHEADER bi;
215 memcpy(&bi, This->lpOutFormat, sizeof(bi));
216 AVIFILE_CloseCompressor(This);
218 if (FAILED(IGetFrame_SetFormat(iface, &bi, NULL, 0, 0, -1, -1))) {
219 if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
220 return NULL;
222 } else if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
223 return NULL;
227 if (lPos != This->lCurrentFrame) {
228 LONG lNext = AVIStreamFindSample(This->pStream, lPos, FIND_KEY|FIND_PREV);
230 if (lNext == -1)
231 return NULL;
232 if (lNext <= This->lCurrentFrame && This->lCurrentFrame < lPos)
233 lNext = This->lCurrentFrame + 1;
235 for (; lNext <= lPos; lNext++) {
236 /* new format for this frame? */
237 if (This->bFormatChanges) {
238 AVIStreamReadFormat(This->pStream, lNext, This->lpInFormat, &This->cbInFormat);
239 if (This->lpOutFormat != NULL) {
240 if (This->lpOutFormat->biBitCount <= 8)
241 ICDecompressGetPalette(This->hic, This->lpInFormat,
242 This->lpOutFormat);
246 /* read input frame */
247 while (FAILED(AVIStreamRead(This->pStream, lNext, 1, This->lpInBuffer,
248 This->cbInBuffer, &readBytes, &readSamples))) {
249 /* not enough memory for input buffer? */
250 readBytes = 0;
251 if (FAILED(AVIStreamSampleSize(This->pStream, lNext, &readBytes)))
252 return NULL;
253 if (This->cbInBuffer >= readBytes)
254 break;
255 This->lpInFormat = GlobalReAllocPtr(This->lpInFormat, This->cbInFormat + readBytes, 0);
256 if (This->lpInFormat == NULL)
257 return NULL;
258 This->lpInBuffer = (BYTE*)This->lpInFormat + This->cbInFormat;
261 if (readSamples != 1)
262 return NULL;
263 if (readBytes != 0) {
264 This->lpInFormat->biSizeImage = readBytes;
266 /* nothing to decompress? */
267 if (This->hic == NULL) {
268 This->lCurrentFrame = lPos;
269 return This->lpInFormat;
272 if (This->bResize) {
273 ICDecompressEx(This->hic,0,This->lpInFormat,This->lpInBuffer,0,0,
274 This->lpInFormat->biWidth,This->lpInFormat->biHeight,
275 This->lpOutFormat,This->lpOutBuffer,This->x,This->y,
276 This->dx,This->dy);
277 } else {
278 ICDecompress(This->hic, 0, This->lpInFormat, This->lpInBuffer,
279 This->lpOutFormat, This->lpOutBuffer);
282 } /* for (lNext < lPos) */
283 } /* if (This->lCurrentFrame != lPos) */
285 return (This->hic == NULL ? This->lpInFormat : This->lpOutFormat);
288 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
289 LONG lEnd, LONG lRate)
291 ICOM_THIS(IGetFrameImpl,iface);
293 TRACE("(%p,%ld,%ld,%ld)\n", iface, lStart, lEnd, lRate);
295 This->bFixedStream = TRUE;
297 return (IGetFrame_GetFrame(iface, lStart) ? AVIERR_OK : AVIERR_ERROR);
300 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface)
302 ICOM_THIS(IGetFrameImpl,iface);
304 TRACE("(%p)\n", iface);
306 This->bFixedStream = FALSE;
308 return AVIERR_OK;
311 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
312 LPBITMAPINFOHEADER lpbiWanted,
313 LPVOID lpBits, INT x, INT y,
314 INT dx, INT dy)
316 ICOM_THIS(IGetFrameImpl,iface);
318 AVISTREAMINFOW sInfo;
319 LPBITMAPINFOHEADER lpbi = lpbiWanted;
320 BOOL bBestDisplay = FALSE;
322 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface, lpbiWanted, lpBits,
323 x, y, dx, dy);
325 if (This->pStream == NULL)
326 return AVIERR_ERROR;
328 if ((LONG)lpbiWanted == AVIGETFRAMEF_BESTDISPLAYFMT) {
329 lpbi = NULL;
330 bBestDisplay = TRUE;
333 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
334 if (sInfo.fccType != streamtypeVIDEO)
335 return AVIERR_UNSUPPORTED;
337 This->bFormatChanges =
338 (sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES ? TRUE : FALSE );
339 This->dwFormatChangeCount = sInfo.dwFormatChangeCount;
340 This->dwEditCount = sInfo.dwEditCount;
341 This->lCurrentFrame = -1;
343 /* get input format from stream */
344 if (This->lpInFormat == NULL) {
345 HRESULT hr;
347 This->cbInBuffer = (LONG)sInfo.dwSuggestedBufferSize;
348 if (This->cbInBuffer == 0)
349 This->cbInBuffer = 1024;
351 AVIStreamFormatSize(This->pStream, sInfo.dwStart, &This->cbInFormat);
353 This->lpInFormat =
354 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, This->cbInFormat + This->cbInBuffer);
355 if (This->lpInFormat == NULL) {
356 AVIFILE_CloseCompressor(This);
357 return AVIERR_MEMORY;
360 hr = AVIStreamReadFormat(This->pStream, sInfo.dwStart, This->lpInFormat, &This->cbInFormat);
361 if (FAILED(hr)) {
362 AVIFILE_CloseCompressor(This);
363 return hr;
366 This->lpInBuffer = ((LPBYTE)This->lpInFormat) + This->cbInFormat;
369 /* check input format */
370 if (This->lpInFormat->biClrUsed == 0 && This->lpInFormat->biBitCount <= 8)
371 This->lpInFormat->biClrUsed = 1u << This->lpInFormat->biBitCount;
372 if (This->lpInFormat->biSizeImage == 0 &&
373 This->lpInFormat->biCompression == BI_RGB) {
374 This->lpInFormat->biSizeImage =
375 DIBWIDTHBYTES(*This->lpInFormat) * This->lpInFormat->biHeight;
378 /* only to pass through? */
379 if (This->lpInFormat->biCompression == BI_RGB && lpBits == NULL) {
380 if (lpbi == NULL ||
381 (lpbi->biCompression == BI_RGB &&
382 lpbi->biWidth == This->lpInFormat->biWidth &&
383 lpbi->biHeight == This->lpInFormat->biHeight &&
384 lpbi->biBitCount == This->lpInFormat->biBitCount)) {
385 This->lpOutFormat = This->lpInFormat;
386 This->lpOutBuffer = DIBPTR(This->lpInFormat);
387 return AVIERR_OK;
391 /* need memory for output format? */
392 if (This->lpOutFormat == NULL) {
393 This->lpOutFormat =
394 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, sizeof(BITMAPINFOHEADER)
395 + 256 * sizeof(RGBQUAD));
396 if (This->lpOutFormat == NULL) {
397 AVIFILE_CloseCompressor(This);
398 return AVIERR_MEMORY;
402 /* need handle to video compressor */
403 if (This->hic == NULL) {
404 FOURCC fccHandler;
406 if (This->lpInFormat->biCompression == BI_RGB)
407 fccHandler = comptypeDIB;
408 else if (This->lpInFormat->biCompression == BI_RLE8)
409 fccHandler = mmioFOURCC('R','L','E',' ');
410 else
411 fccHandler = sInfo.fccHandler;
413 if (lpbi != NULL) {
414 if (lpbi->biWidth == 0)
415 lpbi->biWidth = This->lpInFormat->biWidth;
416 if (lpbi->biHeight == 0)
417 lpbi->biHeight = This->lpInFormat->biHeight;
420 This->hic = ICLocate(ICTYPE_VIDEO, fccHandler, This->lpInFormat, lpbi, ICMODE_DECOMPRESS);
421 if (This->hic == NULL) {
422 AVIFILE_CloseCompressor(This);
423 return AVIERR_NOCOMPRESSOR;
427 /* output format given? */
428 if (lpbi != NULL) {
429 /* check the given output format ... */
430 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
431 lpbi->biClrUsed = 1u << lpbi->biBitCount;
433 /* ... and remember it */
434 memcpy(This->lpOutFormat, lpbi,
435 lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD));
436 if (lpbi->biBitCount <= 8)
437 ICDecompressGetPalette(This->hic, This->lpInFormat, This->lpOutFormat);
439 return AVIERR_OK;
440 } else {
441 if (bBestDisplay) {
442 ICGetDisplayFormat(This->hic, This->lpInFormat,
443 This->lpOutFormat, 0, dx, dy);
444 } else if (ICDecompressGetFormat(This->hic, This->lpInFormat,
445 This->lpOutFormat) < 0) {
446 AVIFILE_CloseCompressor(This);
447 return AVIERR_NOCOMPRESSOR;
450 /* check output format */
451 if (This->lpOutFormat->biClrUsed == 0 &&
452 This->lpOutFormat->biBitCount <= 8)
453 This->lpOutFormat->biClrUsed = 1u << This->lpOutFormat->biBitCount;
454 if (This->lpOutFormat->biSizeImage == 0 &&
455 This->lpOutFormat->biCompression == BI_RGB) {
456 This->lpOutFormat->biSizeImage =
457 DIBWIDTHBYTES(*This->lpOutFormat) * This->lpOutFormat->biHeight;
460 if (lpBits == NULL) {
461 register DWORD size = This->lpOutFormat->biClrUsed * sizeof(RGBQUAD);
463 size += This->lpOutFormat->biSize + This->lpOutFormat->biSizeImage;
464 This->lpOutFormat =
465 (LPBITMAPINFOHEADER)GlobalReAllocPtr(This->lpOutFormat, size, GMEM_MOVEABLE);
466 if (This->lpOutFormat == NULL) {
467 AVIFILE_CloseCompressor(This);
468 return AVIERR_MEMORY;
470 This->lpOutBuffer = DIBPTR(This->lpOutFormat);
471 } else
472 This->lpOutBuffer = lpBits;
474 /* for user size was irrelevant */
475 if (dx == -1)
476 dx = This->lpOutFormat->biWidth;
477 if (dy == -1)
478 dy = This->lpOutFormat->biHeight;
480 /* need to resize? */
481 if (x != 0 || y != 0) {
482 if (dy == This->lpOutFormat->biHeight &&
483 dx == This->lpOutFormat->biWidth)
484 This->bResize = FALSE;
485 else
486 This->bResize = TRUE;
489 if (This->bResize) {
490 This->x = x;
491 This->y = y;
492 This->dx = dx;
493 This->dy = dy;
495 if (ICDecompressExBegin(This->hic,0,This->lpInFormat,This->lpInBuffer,0,
496 0,This->lpInFormat->biWidth,
497 This->lpInFormat->biHeight,This->lpOutFormat,
498 This->lpOutBuffer, x, y, dx, dy) == ICERR_OK)
499 return AVIERR_OK;
500 } else if (ICDecompressBegin(This->hic, This->lpInFormat,
501 This->lpOutFormat) == ICERR_OK)
502 return AVIERR_OK;
504 AVIFILE_CloseCompressor(This);
506 return AVIERR_COMPRESSOR;
510 /***********************************************************************/