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 struct ICOM_VTABLE(IGetFrame) igetframeVtbl = {
58 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
59 IGetFrame_fnQueryInterface,
68 typedef struct _IGetFrameImpl {
70 ICOM_VFIELD(IGetFrame);
79 LPBITMAPINFOHEADER lpInFormat;
83 LPBITMAPINFOHEADER lpOutFormat;
94 DWORD dwFormatChangeCount;
98 /***********************************************************************/
100 static void AVIFILE_CloseCompressor(IGetFrameImpl *This)
102 if (This->lpOutFormat != NULL && This->lpInFormat != This->lpOutFormat) {
103 GlobalFreePtr(This->lpOutFormat);
104 This->lpOutFormat = NULL;
106 if (This->lpInFormat != NULL) {
107 GlobalFreePtr(This->lpInFormat);
108 This->lpInFormat = NULL;
110 if (This->hic != NULL) {
112 ICDecompressExEnd(This->hic);
114 ICDecompressEnd(This->hic);
120 PGETFRAME AVIFILE_CreateGetFrame(PAVISTREAM pStream)
124 /* check parameter */
128 pg = (IGetFrameImpl*)LocalAlloc(LPTR, sizeof(IGetFrameImpl));
130 pg->lpVtbl = &igetframeVtbl;
132 pg->lCurrentFrame = -1;
133 pg->pStream = pStream;
134 IAVIStream_AddRef(pStream);
137 return (PGETFRAME)pg;
140 static HRESULT WINAPI IGetFrame_fnQueryInterface(IGetFrame *iface,
141 REFIID refiid, LPVOID *obj)
143 ICOM_THIS(IGetFrameImpl,iface);
145 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
147 if (IsEqualGUID(&IID_IUnknown, refiid) ||
148 IsEqualGUID(&IID_IGetFrame, refiid)) {
153 return OLE_E_ENUM_NOMORE;
156 static ULONG WINAPI IGetFrame_fnAddRef(IGetFrame *iface)
158 ICOM_THIS(IGetFrameImpl,iface);
160 TRACE("(%p)\n", iface);
162 return ++(This->ref);
165 static ULONG WINAPI IGetFrame_fnRelease(IGetFrame *iface)
167 ICOM_THIS(IGetFrameImpl,iface);
169 TRACE("(%p)\n", iface);
171 if (!--(This->ref)) {
172 AVIFILE_CloseCompressor(This);
173 if (This->pStream != NULL) {
174 AVIStreamRelease(This->pStream);
175 This->pStream = NULL;
178 LocalFree((HLOCAL)iface);
185 static LPVOID WINAPI IGetFrame_fnGetFrame(IGetFrame *iface, LONG lPos)
187 ICOM_THIS(IGetFrameImpl,iface);
192 TRACE("(%p,%ld)\n", iface, lPos);
195 if (This->pStream == NULL)
197 if (This->lpInFormat == 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) {
216 memcpy(&bi, This->lpOutFormat, sizeof(bi));
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)))
223 } else if (FAILED(IGetFrame_SetFormat(iface, NULL, NULL, 0, 0, -1, -1)))
228 if (lPos != This->lCurrentFrame) {
229 LONG lNext = AVIStreamFindSample(This->pStream, lPos, FIND_KEY|FIND_PREV);
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 AVIStreamReadFormat(This->pStream, lNext, This->lpInFormat, &This->cbInFormat);
240 if (This->lpOutFormat != NULL) {
241 if (This->lpOutFormat->biBitCount <= 8)
242 ICDecompressGetPalette(This->hic, This->lpInFormat,
247 /* read input frame */
248 while (FAILED(AVIStreamRead(This->pStream, lNext, 1, This->lpInBuffer,
249 This->cbInBuffer, &readBytes, &readSamples))) {
250 /* not enough memory for input buffer? */
252 if (FAILED(AVIStreamSampleSize(This->pStream, lNext, &readBytes)))
254 if (This->cbInBuffer >= readBytes)
256 This->lpInFormat = GlobalReAllocPtr(This->lpInFormat, This->cbInFormat + readBytes, 0);
257 if (This->lpInFormat == NULL)
259 This->lpInBuffer = (BYTE*)This->lpInFormat + This->cbInFormat;
262 if (readSamples != 1)
264 if (readBytes != 0) {
265 This->lpInFormat->biSizeImage = readBytes;
267 /* nothing to decompress? */
268 if (This->hic == NULL) {
269 This->lCurrentFrame = lPos;
270 return This->lpInFormat;
274 ICDecompressEx(This->hic,0,This->lpInFormat,This->lpInBuffer,0,0,
275 This->lpInFormat->biWidth,This->lpInFormat->biHeight,
276 This->lpOutFormat,This->lpOutBuffer,This->x,This->y,
279 ICDecompress(This->hic, 0, This->lpInFormat, This->lpInBuffer,
280 This->lpOutFormat, This->lpOutBuffer);
283 } /* for (lNext < lPos) */
284 } /* if (This->lCurrentFrame != lPos) */
286 return (This->hic == NULL ? This->lpInFormat : This->lpOutFormat);
289 static HRESULT WINAPI IGetFrame_fnBegin(IGetFrame *iface, LONG lStart,
290 LONG lEnd, LONG lRate)
292 ICOM_THIS(IGetFrameImpl,iface);
294 TRACE("(%p,%ld,%ld,%ld)\n", iface, lStart, lEnd, lRate);
296 This->bFixedStream = TRUE;
298 return (IGetFrame_GetFrame(iface, lStart) ? AVIERR_OK : AVIERR_ERROR);
301 static HRESULT WINAPI IGetFrame_fnEnd(IGetFrame *iface)
303 ICOM_THIS(IGetFrameImpl,iface);
305 TRACE("(%p)\n", iface);
307 This->bFixedStream = FALSE;
312 static HRESULT WINAPI IGetFrame_fnSetFormat(IGetFrame *iface,
313 LPBITMAPINFOHEADER lpbiWanted,
314 LPVOID lpBits, INT x, INT y,
317 ICOM_THIS(IGetFrameImpl,iface);
319 AVISTREAMINFOW sInfo;
320 LPBITMAPINFOHEADER lpbi = lpbiWanted;
321 BOOL bBestDisplay = FALSE;
323 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface, lpbiWanted, lpBits,
326 if (This->pStream == NULL)
329 if ((LONG)lpbiWanted == AVIGETFRAMEF_BESTDISPLAYFMT) {
334 IAVIStream_Info(This->pStream, &sInfo, sizeof(sInfo));
335 if (sInfo.fccType != streamtypeVIDEO)
336 return AVIERR_UNSUPPORTED;
338 This->bFormatChanges =
339 (sInfo.dwFlags & AVISTREAMINFO_FORMATCHANGES ? TRUE : FALSE );
340 This->dwFormatChangeCount = sInfo.dwFormatChangeCount;
341 This->dwEditCount = sInfo.dwEditCount;
342 This->lCurrentFrame = -1;
344 /* get input format from stream */
345 if (This->lpInFormat == NULL) {
348 This->cbInBuffer = (LONG)sInfo.dwSuggestedBufferSize;
349 if (This->cbInBuffer == 0)
350 This->cbInBuffer = 1024;
352 AVIStreamFormatSize(This->pStream, sInfo.dwStart, &This->cbInFormat);
355 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, This->cbInFormat + This->cbInBuffer);
356 if (This->lpInFormat == NULL) {
357 AVIFILE_CloseCompressor(This);
358 return AVIERR_MEMORY;
361 hr = AVIStreamReadFormat(This->pStream, sInfo.dwStart, This->lpInFormat, &This->cbInFormat);
363 AVIFILE_CloseCompressor(This);
367 This->lpInBuffer = ((LPBYTE)This->lpInFormat) + This->cbInFormat;
370 /* check input format */
371 if (This->lpInFormat->biClrUsed == 0 && This->lpInFormat->biBitCount <= 8)
372 This->lpInFormat->biClrUsed = 1u << This->lpInFormat->biBitCount;
373 if (This->lpInFormat->biSizeImage == 0 &&
374 This->lpInFormat->biCompression == BI_RGB) {
375 This->lpInFormat->biSizeImage =
376 DIBWIDTHBYTES(*This->lpInFormat) * This->lpInFormat->biHeight;
379 /* only to pass through? */
380 if (This->lpInFormat->biCompression == BI_RGB && lpBits == NULL) {
382 (lpbi->biCompression == BI_RGB &&
383 lpbi->biWidth == This->lpInFormat->biWidth &&
384 lpbi->biHeight == This->lpInFormat->biHeight &&
385 lpbi->biBitCount == This->lpInFormat->biBitCount)) {
386 This->lpOutFormat = This->lpInFormat;
387 This->lpOutBuffer = DIBPTR(This->lpInFormat);
392 /* need memory for output format? */
393 if (This->lpOutFormat == NULL) {
395 (LPBITMAPINFOHEADER)GlobalAllocPtr(GHND, sizeof(BITMAPINFOHEADER)
396 + 256 * sizeof(RGBQUAD));
397 if (This->lpOutFormat == NULL) {
398 AVIFILE_CloseCompressor(This);
399 return AVIERR_MEMORY;
403 /* need handle to video compressor */
404 if (This->hic == NULL) {
407 if (This->lpInFormat->biCompression == BI_RGB)
408 fccHandler = comptypeDIB;
409 else if (This->lpInFormat->biCompression == BI_RLE8)
410 fccHandler = mmioFOURCC('R','L','E',' ');
412 fccHandler = sInfo.fccHandler;
415 if (lpbi->biWidth == 0)
416 lpbi->biWidth = This->lpInFormat->biWidth;
417 if (lpbi->biHeight == 0)
418 lpbi->biHeight = This->lpInFormat->biHeight;
421 This->hic = ICLocate(ICTYPE_VIDEO, fccHandler, This->lpInFormat, lpbi, ICMODE_DECOMPRESS);
422 if (This->hic == NULL) {
423 AVIFILE_CloseCompressor(This);
424 return AVIERR_NOCOMPRESSOR;
428 /* output format given? */
430 /* check the given output format ... */
431 if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
432 lpbi->biClrUsed = 1u << lpbi->biBitCount;
434 /* ... and remember it */
435 memcpy(This->lpOutFormat, lpbi,
436 lpbi->biSize + lpbi->biClrUsed * sizeof(RGBQUAD));
437 if (lpbi->biBitCount <= 8)
438 ICDecompressGetPalette(This->hic, This->lpInFormat, This->lpOutFormat);
443 ICGetDisplayFormat(This->hic, This->lpInFormat,
444 This->lpOutFormat, 0, dx, dy);
445 } else if (ICDecompressGetFormat(This->hic, This->lpInFormat,
446 This->lpOutFormat) < 0) {
447 AVIFILE_CloseCompressor(This);
448 return AVIERR_NOCOMPRESSOR;
451 /* check output format */
452 if (This->lpOutFormat->biClrUsed == 0 &&
453 This->lpOutFormat->biBitCount <= 8)
454 This->lpOutFormat->biClrUsed = 1u << This->lpOutFormat->biBitCount;
455 if (This->lpOutFormat->biSizeImage == 0 &&
456 This->lpOutFormat->biCompression == BI_RGB) {
457 This->lpOutFormat->biSizeImage =
458 DIBWIDTHBYTES(*This->lpOutFormat) * This->lpOutFormat->biHeight;
461 if (lpBits == NULL) {
462 register DWORD size = This->lpOutFormat->biClrUsed * sizeof(RGBQUAD);
464 size += This->lpOutFormat->biSize + This->lpOutFormat->biSizeImage;
466 (LPBITMAPINFOHEADER)GlobalReAllocPtr(This->lpOutFormat, size, GMEM_MOVEABLE);
467 if (This->lpOutFormat == NULL) {
468 AVIFILE_CloseCompressor(This);
469 return AVIERR_MEMORY;
471 This->lpOutBuffer = DIBPTR(This->lpOutFormat);
473 This->lpOutBuffer = lpBits;
475 /* for user size was irrelevant */
477 dx = This->lpOutFormat->biWidth;
479 dy = This->lpOutFormat->biHeight;
481 /* need to resize? */
482 if (x != 0 || y != 0) {
483 if (dy == This->lpOutFormat->biHeight &&
484 dx == This->lpOutFormat->biWidth)
485 This->bResize = FALSE;
487 This->bResize = TRUE;
496 if (ICDecompressExBegin(This->hic,0,This->lpInFormat,This->lpInBuffer,0,
497 0,This->lpInFormat->biWidth,
498 This->lpInFormat->biHeight,This->lpOutFormat,
499 This->lpOutBuffer, x, y, dx, dy) == ICERR_OK)
501 } else if (ICDecompressBegin(This->hic, This->lpInFormat,
502 This->lpOutFormat) == ICERR_OK)
505 AVIFILE_CloseCompressor(This);
507 return AVIERR_COMPRESSOR;
511 /***********************************************************************/