Removed some uses of the non-standard ICOM_THIS macro.
[wine/multimedia.git] / dlls / avifil32 / getframe.c
blob05681bf9a54c108f81e34af8d8b41efa2f1ac9c8
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 "winuser.h"
29 #include "vfw.h"
31 #include "avifile_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
37 #ifndef DIBPTR
38 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
39 (lp)->biClrUsed * sizeof(RGBQUAD))
40 #endif
42 /***********************************************************************/
44 static HRESULT WINAPI IGetFrame_fnQueryInterface(IGetFrame *iface,
45 REFIID refiid, LPVOID *obj);
46 static ULONG WINAPI IGetFrame_fnAddRef(IGetFrame *iface);
47 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface);
48 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos);
49 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
50 LONG lEnd, LONG lRate);
51 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface);
52 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
53 LPBITMAPINFOHEADER lpbi,
54 LPVOID lpBits, INT x, INT y,
55 INT dx, INT dy);
57 struct IGetFrameVtbl igetframeVtbl = {
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 IGetFrameVtbl *lpVtbl;
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 IGetFrameImpl *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 IGetFrameImpl *This = (IGetFrameImpl *)iface;
159 TRACE("(%p)\n", iface);
161 return ++(This->ref);
164 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface)
166 IGetFrameImpl *This = (IGetFrameImpl *)iface;
168 TRACE("(%p)\n", iface);
170 if (!--(This->ref)) {
171 AVIFILE_CloseCompressor(This);
172 if (This->pStream != NULL) {
173 IAVIStream_Release(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 IGetFrameImpl *This = (IGetFrameImpl *)iface;
188 LONG readBytes;
189 LONG readSamples;
191 TRACE("(%p,%ld)\n", iface, lPos);
193 /* We don't want negative start values! -- marks invalid buffer content */
194 if (lPos < 0)
195 return NULL;
197 /* check state */
198 if (This->pStream == NULL)
199 return NULL;
200 if (This->lpInFormat == NULL)
201 return NULL;
203 /* Could stream have changed? */
204 if (! This->bFixedStream) {
205 AVISTREAMINFOW sInfo;
207 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
209 if (sInfo.dwEditCount != This->dwEditCount) {
210 This->dwEditCount = sInfo.dwEditCount;
211 This->lCurrentFrame = -1;
214 if (sInfo.dwFormatChangeCount != This->dwFormatChangeCount) {
215 /* stream has changed */
216 if (This->lpOutFormat != NULL) {
217 BITMAPINFOHEADER bi;
219 memcpy(&bi, This->lpOutFormat, sizeof(bi));
220 AVIFILE_CloseCompressor(This);
222 if (FAILED(IGetFrame_SetFormat(iface, &bi, NULL, 0, 0, -1, -1))) {
223 if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
224 return NULL;
226 } else if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
227 return NULL;
231 if (lPos != This->lCurrentFrame) {
232 LONG lNext = IAVIStream_FindSample(This->pStream,lPos,FIND_KEY|FIND_PREV);
234 if (lNext == -1)
235 return NULL; /* frame doesn't exist */
236 if (lNext <= This->lCurrentFrame && This->lCurrentFrame < lPos)
237 lNext = This->lCurrentFrame + 1;
239 for (; lNext <= lPos; lNext++) {
240 /* new format for this frame? */
241 if (This->bFormatChanges) {
242 IAVIStream_ReadFormat(This->pStream, lNext,
243 This->lpInFormat, &This->cbInFormat);
244 if (This->lpOutFormat != NULL) {
245 if (This->lpOutFormat->biBitCount <= 8)
246 ICDecompressGetPalette(This->hic, This->lpInFormat,
247 This->lpOutFormat);
251 /* read input frame */
252 while (FAILED(AVIStreamRead(This->pStream, lNext, 1, This->lpInBuffer,
253 This->cbInBuffer, &readBytes, &readSamples))) {
254 /* not enough memory for input buffer? */
255 readBytes = 0;
256 if (FAILED(AVIStreamSampleSize(This->pStream, lNext, &readBytes)))
257 return NULL; /* bad thing, but bad things will happen */
258 if (readBytes <= 0) {
259 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
260 return NULL;
263 /* IAVIStream::Read failed because of other reasons not buffersize? */
264 if (This->cbInBuffer >= readBytes)
265 break;
266 This->cbInBuffer = This->cbInFormat + readBytes;
267 This->lpInFormat = GlobalReAllocPtr(This->lpInFormat, This->cbInBuffer, 0);
268 if (This->lpInFormat == NULL)
269 return NULL; /* out of memory */
270 This->lpInBuffer = (BYTE*)This->lpInFormat + This->cbInFormat;
273 if (readSamples != 1) {
274 ERR(": no frames read\n");
275 return NULL;
277 if (readBytes != 0) {
278 This->lpInFormat->biSizeImage = readBytes;
280 /* nothing to decompress? */
281 if (This->hic == NULL) {
282 This->lCurrentFrame = lPos;
283 return This->lpInFormat;
286 if (This->bResize) {
287 ICDecompressEx(This->hic,0,This->lpInFormat,This->lpInBuffer,0,0,
288 This->lpInFormat->biWidth,This->lpInFormat->biHeight,
289 This->lpOutFormat,This->lpOutBuffer,This->x,This->y,
290 This->dx,This->dy);
291 } else {
292 ICDecompress(This->hic, 0, This->lpInFormat, This->lpInBuffer,
293 This->lpOutFormat, This->lpOutBuffer);
296 } /* for (lNext < lPos) */
297 } /* if (This->lCurrentFrame != lPos) */
299 return (This->hic == NULL ? This->lpInFormat : This->lpOutFormat);
302 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
303 LONG lEnd, LONG lRate)
305 IGetFrameImpl *This = (IGetFrameImpl *)iface;
307 TRACE("(%p,%ld,%ld,%ld)\n", iface, lStart, lEnd, lRate);
309 This->bFixedStream = TRUE;
311 return (IGetFrame_GetFrame(iface, lStart) ? AVIERR_OK : AVIERR_ERROR);
314 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface)
316 IGetFrameImpl *This = (IGetFrameImpl *)iface;
318 TRACE("(%p)\n", iface);
320 This->bFixedStream = FALSE;
322 return AVIERR_OK;
325 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
326 LPBITMAPINFOHEADER lpbiWanted,
327 LPVOID lpBits, INT x, INT y,
328 INT dx, INT dy)
330 IGetFrameImpl *This = (IGetFrameImpl *)iface;
332 AVISTREAMINFOW sInfo;
333 LPBITMAPINFOHEADER lpbi = lpbiWanted;
334 BOOL bBestDisplay = FALSE;
336 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface, lpbiWanted, lpBits,
337 x, y, dx, dy);
339 if (This->pStream == NULL)
340 return AVIERR_ERROR;
342 if ((LONG)lpbiWanted == AVIGETFRAMEF_BESTDISPLAYFMT) {
343 lpbi = NULL;
344 bBestDisplay = TRUE;
347 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
348 if (sInfo.fccType != streamtypeVIDEO)
349 return AVIERR_UNSUPPORTED;
351 This->bFormatChanges =
352 (sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES ? TRUE : FALSE );
353 This->dwFormatChangeCount = sInfo.dwFormatChangeCount;
354 This->dwEditCount = sInfo.dwEditCount;
355 This->lCurrentFrame = -1;
357 /* get input format from stream */
358 if (This->lpInFormat == NULL) {
359 HRESULT hr;
361 This->cbInBuffer = (LONG)sInfo.dwSuggestedBufferSize;
362 if (This->cbInBuffer == 0)
363 This->cbInBuffer = 1024;
365 IAVIStream_ReadFormat(This->pStream, sInfo.dwStart,
366 NULL, &This->cbInFormat);
368 This->lpInFormat =
369 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, This->cbInFormat + This->cbInBuffer);
370 if (This->lpInFormat == NULL) {
371 AVIFILE_CloseCompressor(This);
372 return AVIERR_MEMORY;
375 hr = IAVIStream_ReadFormat(This->pStream, sInfo.dwStart, This->lpInFormat, &This->cbInFormat);
376 if (FAILED(hr)) {
377 AVIFILE_CloseCompressor(This);
378 return hr;
381 This->lpInBuffer = ((LPBYTE)This->lpInFormat) + This->cbInFormat;
384 /* check input format */
385 if (This->lpInFormat->biClrUsed == 0 && This->lpInFormat->biBitCount <= 8)
386 This->lpInFormat->biClrUsed = 1u << This->lpInFormat->biBitCount;
387 if (This->lpInFormat->biSizeImage == 0 &&
388 This->lpInFormat->biCompression == BI_RGB) {
389 This->lpInFormat->biSizeImage =
390 DIBWIDTHBYTES(*This->lpInFormat) * This->lpInFormat->biHeight;
393 /* only to pass through? */
394 if (This->lpInFormat->biCompression == BI_RGB && lpBits == NULL) {
395 if (lpbi == NULL ||
396 (lpbi->biCompression == BI_RGB &&
397 lpbi->biWidth == This->lpInFormat->biWidth &&
398 lpbi->biHeight == This->lpInFormat->biHeight &&
399 lpbi->biBitCount == This->lpInFormat->biBitCount)) {
400 This->lpOutFormat = This->lpInFormat;
401 This->lpOutBuffer = DIBPTR(This->lpInFormat);
402 return AVIERR_OK;
406 /* need memory for output format? */
407 if (This->lpOutFormat == NULL) {
408 This->lpOutFormat =
409 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, sizeof(BITMAPINFOHEADER)
410 + 256 * sizeof(RGBQUAD));
411 if (This->lpOutFormat == NULL) {
412 AVIFILE_CloseCompressor(This);
413 return AVIERR_MEMORY;
417 /* need handle to video compressor */
418 if (This->hic == NULL) {
419 FOURCC fccHandler;
421 if (This->lpInFormat->biCompression == BI_RGB)
422 fccHandler = comptypeDIB;
423 else if (This->lpInFormat->biCompression == BI_RLE8)
424 fccHandler = mmioFOURCC('R','L','E',' ');
425 else
426 fccHandler = sInfo.fccHandler;
428 if (lpbi != NULL) {
429 if (lpbi->biWidth == 0)
430 lpbi->biWidth = This->lpInFormat->biWidth;
431 if (lpbi->biHeight == 0)
432 lpbi->biHeight = This->lpInFormat->biHeight;
435 This->hic = ICLocate(ICTYPE_VIDEO, fccHandler, This->lpInFormat, lpbi, ICMODE_DECOMPRESS);
436 if (This->hic == NULL) {
437 AVIFILE_CloseCompressor(This);
438 return AVIERR_NOCOMPRESSOR;
442 /* output format given? */
443 if (lpbi != NULL) {
444 /* check the given output format ... */
445 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
446 lpbi->biClrUsed = 1u << lpbi->biBitCount;
448 /* ... and remember it */
449 memcpy(This->lpOutFormat, lpbi,
450 lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD));
451 if (lpbi->biBitCount <= 8)
452 ICDecompressGetPalette(This->hic, This->lpInFormat, This->lpOutFormat);
454 return AVIERR_OK;
455 } else {
456 if (bBestDisplay) {
457 ICGetDisplayFormat(This->hic, This->lpInFormat,
458 This->lpOutFormat, 0, dx, dy);
459 } else if (ICDecompressGetFormat(This->hic, This->lpInFormat,
460 This->lpOutFormat) < 0) {
461 AVIFILE_CloseCompressor(This);
462 return AVIERR_NOCOMPRESSOR;
465 /* check output format */
466 if (This->lpOutFormat->biClrUsed == 0 &&
467 This->lpOutFormat->biBitCount <= 8)
468 This->lpOutFormat->biClrUsed = 1u << This->lpOutFormat->biBitCount;
469 if (This->lpOutFormat->biSizeImage == 0 &&
470 This->lpOutFormat->biCompression == BI_RGB) {
471 This->lpOutFormat->biSizeImage =
472 DIBWIDTHBYTES(*This->lpOutFormat) * This->lpOutFormat->biHeight;
475 if (lpBits == NULL) {
476 register DWORD size = This->lpOutFormat->biClrUsed * sizeof(RGBQUAD);
478 size += This->lpOutFormat->biSize + This->lpOutFormat->biSizeImage;
479 This->lpOutFormat =
480 (LPBITMAPINFOHEADER)GlobalReAllocPtr(This->lpOutFormat, size, GMEM_MOVEABLE);
481 if (This->lpOutFormat == NULL) {
482 AVIFILE_CloseCompressor(This);
483 return AVIERR_MEMORY;
485 This->lpOutBuffer = DIBPTR(This->lpOutFormat);
486 } else
487 This->lpOutBuffer = lpBits;
489 /* for user size was irrelevant */
490 if (dx == -1)
491 dx = This->lpOutFormat->biWidth;
492 if (dy == -1)
493 dy = This->lpOutFormat->biHeight;
495 /* need to resize? */
496 if (x != 0 || y != 0) {
497 if (dy == This->lpOutFormat->biHeight &&
498 dx == This->lpOutFormat->biWidth)
499 This->bResize = FALSE;
500 else
501 This->bResize = TRUE;
504 if (This->bResize) {
505 This->x = x;
506 This->y = y;
507 This->dx = dx;
508 This->dy = dy;
510 if (ICDecompressExBegin(This->hic,0,This->lpInFormat,This->lpInBuffer,0,
511 0,This->lpInFormat->biWidth,
512 This->lpInFormat->biHeight,This->lpOutFormat,
513 This->lpOutBuffer, x, y, dx, dy) == ICERR_OK)
514 return AVIERR_OK;
515 } else if (ICDecompressBegin(This->hic, This->lpInFormat,
516 This->lpOutFormat) == ICERR_OK)
517 return AVIERR_OK;
519 AVIFILE_CloseCompressor(This);
521 return AVIERR_COMPRESSOR;
525 /***********************************************************************/