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
31 #include "avifile_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
38 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
39 (lp)->biClrUsed * sizeof(RGBQUAD))
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,
57 static const struct IGetFrameVtbl igetframeVtbl = {
58 IGetFrame_fnQueryInterface,
67 typedef struct _IGetFrameImpl {
69 const IGetFrameVtbl *lpVtbl;
78 LPBITMAPINFOHEADER lpInFormat;
82 LPBITMAPINFOHEADER lpOutFormat;
93 DWORD dwFormatChangeCount;
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) {
111 ICDecompressExEnd(This->hic);
113 ICDecompressEnd(This->hic);
119 PGETFRAME AVIFILE_CreateGetFrame(PAVISTREAM pStream)
123 /* check parameter */
127 pg = (IGetFrameImpl*)LocalAlloc(LPTR, sizeof(IGetFrameImpl));
129 pg->lpVtbl = &igetframeVtbl;
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)) {
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);
165 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface)
167 IGetFrameImpl *This = (IGetFrameImpl *)iface;
168 ULONG ref = InterlockedDecrement(&This->ref);
170 TRACE("(%p)\n", iface);
173 AVIFILE_CloseCompressor(This);
174 if (This->pStream != NULL) {
175 IAVIStream_Release(This->pStream);
176 This->pStream = NULL;
179 LocalFree((HLOCAL)iface);
186 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos)
188 IGetFrameImpl *This = (IGetFrameImpl *)iface;
193 TRACE("(%p,%ld)\n", iface, lPos);
195 /* We don't want negative start values! -- marks invalid buffer content */
200 if (This->pStream == NULL)
202 if (This->lpInFormat == 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) {
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)))
228 } else if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
233 if (lPos != This->lCurrentFrame) {
234 LONG lNext = IAVIStream_FindSample(This->pStream,lPos,FIND_KEY|FIND_PREV);
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,
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? */
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");
265 /* IAVIStream::Read failed because of other reasons not buffersize? */
266 if (This->cbInBuffer >= readBytes)
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");
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;
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,
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;
327 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
328 LPBITMAPINFOHEADER lpbiWanted,
329 LPVOID lpBits, INT x, INT y,
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,
341 if (This->pStream == NULL)
344 if ((LONG)lpbiWanted == AVIGETFRAMEF_BESTDISPLAYFMT) {
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) {
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);
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);
379 AVIFILE_CloseCompressor(This);
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) {
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);
408 /* need memory for output format? */
409 if (This->lpOutFormat == NULL) {
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) {
423 if (This->lpInFormat->biCompression == BI_RGB)
424 fccHandler = comptypeDIB;
425 else if (This->lpInFormat->biCompression == BI_RLE8)
426 fccHandler = mmioFOURCC('R','L','E',' ');
428 fccHandler = sInfo.fccHandler;
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? */
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);
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;
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);
489 This->lpOutBuffer = lpBits;
491 /* for user size was irrelevant */
493 dx = This->lpOutFormat->biWidth;
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;
503 This->bResize = TRUE;
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)
517 } else if (ICDecompressBegin(This->hic, This->lpInFormat,
518 This->lpOutFormat) == ICERR_OK)
521 AVIFILE_CloseCompressor(This);
523 return AVIERR_COMPRESSOR;
527 /***********************************************************************/