Forward the functions DllRegisterServer and DllUnregisterServer of
[wine/multimedia.git] / dlls / avifil32 / getframe.c
blob874d78fda5ad0265e649095d9405c9c1f3a9cfed
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 static const 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 const IGetFrameVtbl *lpVtbl;
70 LONG 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;
158 ULONG ref = InterlockedIncrement(&This->ref);
160 TRACE("(%p)\n", iface);
162 return ref;
165 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface)
167 IGetFrameImpl *This = (IGetFrameImpl *)iface;
168 ULONG ref = InterlockedDecrement(&This->ref);
170 TRACE("(%p)\n", iface);
172 if (!ref) {
173 AVIFILE_CloseCompressor(This);
174 if (This->pStream != NULL) {
175 IAVIStream_Release(This->pStream);
176 This->pStream = NULL;
179 LocalFree((HLOCAL)iface);
180 return 0;
183 return ref;
186 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos)
188 IGetFrameImpl *This = (IGetFrameImpl *)iface;
190 LONG readBytes;
191 LONG readSamples;
193 TRACE("(%p,%ld)\n", iface, lPos);
195 /* We don't want negative start values! -- marks invalid buffer content */
196 if (lPos < 0)
197 return NULL;
199 /* check state */
200 if (This->pStream == NULL)
201 return NULL;
202 if (This->lpInFormat == NULL)
203 return NULL;
205 /* Could stream have changed? */
206 if (! This->bFixedStream) {
207 AVISTREAMINFOW sInfo;
209 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
211 if (sInfo.dwEditCount != This->dwEditCount) {
212 This->dwEditCount = sInfo.dwEditCount;
213 This->lCurrentFrame = -1;
216 if (sInfo.dwFormatChangeCount != This->dwFormatChangeCount) {
217 /* stream has changed */
218 if (This->lpOutFormat != NULL) {
219 BITMAPINFOHEADER bi;
221 memcpy(&bi, This->lpOutFormat, sizeof(bi));
222 AVIFILE_CloseCompressor(This);
224 if (FAILED(IGetFrame_SetFormat(iface, &bi, NULL, 0, 0, -1, -1))) {
225 if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
226 return NULL;
228 } else if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
229 return NULL;
233 if (lPos != This->lCurrentFrame) {
234 LONG lNext = IAVIStream_FindSample(This->pStream,lPos,FIND_KEY|FIND_PREV);
236 if (lNext == -1)
237 return NULL; /* frame doesn't exist */
238 if (lNext <= This->lCurrentFrame && This->lCurrentFrame < lPos)
239 lNext = This->lCurrentFrame + 1;
241 for (; lNext <= lPos; lNext++) {
242 /* new format for this frame? */
243 if (This->bFormatChanges) {
244 IAVIStream_ReadFormat(This->pStream, lNext,
245 This->lpInFormat, &This->cbInFormat);
246 if (This->lpOutFormat != NULL) {
247 if (This->lpOutFormat->biBitCount <= 8)
248 ICDecompressGetPalette(This->hic, This->lpInFormat,
249 This->lpOutFormat);
253 /* read input frame */
254 while (FAILED(AVIStreamRead(This->pStream, lNext, 1, This->lpInBuffer,
255 This->cbInBuffer, &readBytes, &readSamples))) {
256 /* not enough memory for input buffer? */
257 readBytes = 0;
258 if (FAILED(AVIStreamSampleSize(This->pStream, lNext, &readBytes)))
259 return NULL; /* bad thing, but bad things will happen */
260 if (readBytes <= 0) {
261 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
262 return NULL;
265 /* IAVIStream::Read failed because of other reasons not buffersize? */
266 if (This->cbInBuffer >= readBytes)
267 break;
268 This->cbInBuffer = This->cbInFormat + readBytes;
269 This->lpInFormat = GlobalReAllocPtr(This->lpInFormat, This->cbInBuffer, 0);
270 if (This->lpInFormat == NULL)
271 return NULL; /* out of memory */
272 This->lpInBuffer = (BYTE*)This->lpInFormat + This->cbInFormat;
275 if (readSamples != 1) {
276 ERR(": no frames read\n");
277 return NULL;
279 if (readBytes != 0) {
280 This->lpInFormat->biSizeImage = readBytes;
282 /* nothing to decompress? */
283 if (This->hic == NULL) {
284 This->lCurrentFrame = lPos;
285 return This->lpInFormat;
288 if (This->bResize) {
289 ICDecompressEx(This->hic,0,This->lpInFormat,This->lpInBuffer,0,0,
290 This->lpInFormat->biWidth,This->lpInFormat->biHeight,
291 This->lpOutFormat,This->lpOutBuffer,This->x,This->y,
292 This->dx,This->dy);
293 } else {
294 ICDecompress(This->hic, 0, This->lpInFormat, This->lpInBuffer,
295 This->lpOutFormat, This->lpOutBuffer);
298 } /* for (lNext < lPos) */
299 } /* if (This->lCurrentFrame != lPos) */
301 return (This->hic == NULL ? This->lpInFormat : This->lpOutFormat);
304 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
305 LONG lEnd, LONG lRate)
307 IGetFrameImpl *This = (IGetFrameImpl *)iface;
309 TRACE("(%p,%ld,%ld,%ld)\n", iface, lStart, lEnd, lRate);
311 This->bFixedStream = TRUE;
313 return (IGetFrame_GetFrame(iface, lStart) ? AVIERR_OK : AVIERR_ERROR);
316 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface)
318 IGetFrameImpl *This = (IGetFrameImpl *)iface;
320 TRACE("(%p)\n", iface);
322 This->bFixedStream = FALSE;
324 return AVIERR_OK;
327 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
328 LPBITMAPINFOHEADER lpbiWanted,
329 LPVOID lpBits, INT x, INT y,
330 INT dx, INT dy)
332 IGetFrameImpl *This = (IGetFrameImpl *)iface;
334 AVISTREAMINFOW sInfo;
335 LPBITMAPINFOHEADER lpbi = lpbiWanted;
336 BOOL bBestDisplay = FALSE;
338 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface, lpbiWanted, lpBits,
339 x, y, dx, dy);
341 if (This->pStream == NULL)
342 return AVIERR_ERROR;
344 if (lpbiWanted == (LPBITMAPINFOHEADER)AVIGETFRAMEF_BESTDISPLAYFMT) {
345 lpbi = NULL;
346 bBestDisplay = TRUE;
349 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
350 if (sInfo.fccType != streamtypeVIDEO)
351 return AVIERR_UNSUPPORTED;
353 This->bFormatChanges =
354 (sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES ? TRUE : FALSE );
355 This->dwFormatChangeCount = sInfo.dwFormatChangeCount;
356 This->dwEditCount = sInfo.dwEditCount;
357 This->lCurrentFrame = -1;
359 /* get input format from stream */
360 if (This->lpInFormat == NULL) {
361 HRESULT hr;
363 This->cbInBuffer = (LONG)sInfo.dwSuggestedBufferSize;
364 if (This->cbInBuffer == 0)
365 This->cbInBuffer = 1024;
367 IAVIStream_ReadFormat(This->pStream, sInfo.dwStart,
368 NULL, &This->cbInFormat);
370 This->lpInFormat =
371 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, This->cbInFormat + This->cbInBuffer);
372 if (This->lpInFormat == NULL) {
373 AVIFILE_CloseCompressor(This);
374 return AVIERR_MEMORY;
377 hr = IAVIStream_ReadFormat(This->pStream, sInfo.dwStart, This->lpInFormat, &This->cbInFormat);
378 if (FAILED(hr)) {
379 AVIFILE_CloseCompressor(This);
380 return hr;
383 This->lpInBuffer = ((LPBYTE)This->lpInFormat) + This->cbInFormat;
386 /* check input format */
387 if (This->lpInFormat->biClrUsed == 0 && This->lpInFormat->biBitCount <= 8)
388 This->lpInFormat->biClrUsed = 1u << This->lpInFormat->biBitCount;
389 if (This->lpInFormat->biSizeImage == 0 &&
390 This->lpInFormat->biCompression == BI_RGB) {
391 This->lpInFormat->biSizeImage =
392 DIBWIDTHBYTES(*This->lpInFormat) * This->lpInFormat->biHeight;
395 /* only to pass through? */
396 if (This->lpInFormat->biCompression == BI_RGB && lpBits == NULL) {
397 if (lpbi == NULL ||
398 (lpbi->biCompression == BI_RGB &&
399 lpbi->biWidth == This->lpInFormat->biWidth &&
400 lpbi->biHeight == This->lpInFormat->biHeight &&
401 lpbi->biBitCount == This->lpInFormat->biBitCount)) {
402 This->lpOutFormat = This->lpInFormat;
403 This->lpOutBuffer = DIBPTR(This->lpInFormat);
404 return AVIERR_OK;
408 /* need memory for output format? */
409 if (This->lpOutFormat == NULL) {
410 This->lpOutFormat =
411 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, sizeof(BITMAPINFOHEADER)
412 + 256 * sizeof(RGBQUAD));
413 if (This->lpOutFormat == NULL) {
414 AVIFILE_CloseCompressor(This);
415 return AVIERR_MEMORY;
419 /* need handle to video compressor */
420 if (This->hic == NULL) {
421 FOURCC fccHandler;
423 if (This->lpInFormat->biCompression == BI_RGB)
424 fccHandler = comptypeDIB;
425 else if (This->lpInFormat->biCompression == BI_RLE8)
426 fccHandler = mmioFOURCC('R','L','E',' ');
427 else
428 fccHandler = sInfo.fccHandler;
430 if (lpbi != NULL) {
431 if (lpbi->biWidth == 0)
432 lpbi->biWidth = This->lpInFormat->biWidth;
433 if (lpbi->biHeight == 0)
434 lpbi->biHeight = This->lpInFormat->biHeight;
437 This->hic = ICLocate(ICTYPE_VIDEO, fccHandler, This->lpInFormat, lpbi, ICMODE_DECOMPRESS);
438 if (This->hic == NULL) {
439 AVIFILE_CloseCompressor(This);
440 return AVIERR_NOCOMPRESSOR;
444 /* output format given? */
445 if (lpbi != NULL) {
446 /* check the given output format ... */
447 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
448 lpbi->biClrUsed = 1u << lpbi->biBitCount;
450 /* ... and remember it */
451 memcpy(This->lpOutFormat, lpbi,
452 lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD));
453 if (lpbi->biBitCount <= 8)
454 ICDecompressGetPalette(This->hic, This->lpInFormat, This->lpOutFormat);
456 return AVIERR_OK;
457 } else {
458 if (bBestDisplay) {
459 ICGetDisplayFormat(This->hic, This->lpInFormat,
460 This->lpOutFormat, 0, dx, dy);
461 } else if (ICDecompressGetFormat(This->hic, This->lpInFormat,
462 This->lpOutFormat) < 0) {
463 AVIFILE_CloseCompressor(This);
464 return AVIERR_NOCOMPRESSOR;
467 /* check output format */
468 if (This->lpOutFormat->biClrUsed == 0 &&
469 This->lpOutFormat->biBitCount <= 8)
470 This->lpOutFormat->biClrUsed = 1u << This->lpOutFormat->biBitCount;
471 if (This->lpOutFormat->biSizeImage == 0 &&
472 This->lpOutFormat->biCompression == BI_RGB) {
473 This->lpOutFormat->biSizeImage =
474 DIBWIDTHBYTES(*This->lpOutFormat) * This->lpOutFormat->biHeight;
477 if (lpBits == NULL) {
478 register DWORD size = This->lpOutFormat->biClrUsed * sizeof(RGBQUAD);
480 size += This->lpOutFormat->biSize + This->lpOutFormat->biSizeImage;
481 This->lpOutFormat =
482 (LPBITMAPINFOHEADER)GlobalReAllocPtr(This->lpOutFormat, size, GMEM_MOVEABLE);
483 if (This->lpOutFormat == NULL) {
484 AVIFILE_CloseCompressor(This);
485 return AVIERR_MEMORY;
487 This->lpOutBuffer = DIBPTR(This->lpOutFormat);
488 } else
489 This->lpOutBuffer = lpBits;
491 /* for user size was irrelevant */
492 if (dx == -1)
493 dx = This->lpOutFormat->biWidth;
494 if (dy == -1)
495 dy = This->lpOutFormat->biHeight;
497 /* need to resize? */
498 if (x != 0 || y != 0) {
499 if (dy == This->lpOutFormat->biHeight &&
500 dx == This->lpOutFormat->biWidth)
501 This->bResize = FALSE;
502 else
503 This->bResize = TRUE;
506 if (This->bResize) {
507 This->x = x;
508 This->y = y;
509 This->dx = dx;
510 This->dy = dy;
512 if (ICDecompressExBegin(This->hic,0,This->lpInFormat,This->lpInBuffer,0,
513 0,This->lpInFormat->biWidth,
514 This->lpInFormat->biHeight,This->lpOutFormat,
515 This->lpOutBuffer, x, y, dx, dy) == ICERR_OK)
516 return AVIERR_OK;
517 } else if (ICDecompressBegin(This->hic, This->lpInFormat,
518 This->lpOutFormat) == ICERR_OK)
519 return AVIERR_OK;
521 AVIFILE_CloseCompressor(This);
523 return AVIERR_COMPRESSOR;
527 /***********************************************************************/