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 IAVIStream_Release(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
);
194 /* We don't want negative start values! -- marks invalid buffer content */
199 if (This
->pStream
== NULL
)
201 if (This
->lpInFormat
== NULL
)
204 /* Could stream have changed? */
205 if (! This
->bFixedStream
) {
206 AVISTREAMINFOW sInfo
;
208 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
210 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
211 This
->dwEditCount
= sInfo
.dwEditCount
;
212 This
->lCurrentFrame
= -1;
215 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
216 /* stream has changed */
217 if (This
->lpOutFormat
!= NULL
) {
220 memcpy(&bi
, This
->lpOutFormat
, sizeof(bi
));
221 AVIFILE_CloseCompressor(This
);
223 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
224 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
227 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
232 if (lPos
!= This
->lCurrentFrame
) {
233 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
236 return NULL
; /* frame doesn't exist */
237 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
238 lNext
= This
->lCurrentFrame
+ 1;
240 for (; lNext
<= lPos
; lNext
++) {
241 /* new format for this frame? */
242 if (This
->bFormatChanges
) {
243 IAVIStream_ReadFormat(This
->pStream
, lNext
,
244 This
->lpInFormat
, &This
->cbInFormat
);
245 if (This
->lpOutFormat
!= NULL
) {
246 if (This
->lpOutFormat
->biBitCount
<= 8)
247 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
252 /* read input frame */
253 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
254 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
255 /* not enough memory for input buffer? */
257 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
258 return NULL
; /* bad thing, but bad things will happen */
259 if (readBytes
<= 0) {
260 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
264 /* IAVIStream::Read failed because of other reasons not buffersize? */
265 if (This
->cbInBuffer
>= readBytes
)
267 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
268 This
->lpInFormat
= GlobalReAllocPtr(This
->lpInFormat
, This
->cbInBuffer
, 0);
269 if (This
->lpInFormat
== NULL
)
270 return NULL
; /* out of memory */
271 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
274 if (readSamples
!= 1) {
275 ERR(": no frames read\n");
278 if (readBytes
!= 0) {
279 This
->lpInFormat
->biSizeImage
= readBytes
;
281 /* nothing to decompress? */
282 if (This
->hic
== NULL
) {
283 This
->lCurrentFrame
= lPos
;
284 return This
->lpInFormat
;
288 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
289 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
290 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
293 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
294 This
->lpOutFormat
, This
->lpOutBuffer
);
297 } /* for (lNext < lPos) */
298 } /* if (This->lCurrentFrame != lPos) */
300 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
303 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
304 LONG lEnd
, LONG lRate
)
306 ICOM_THIS(IGetFrameImpl
,iface
);
308 TRACE("(%p,%ld,%ld,%ld)\n", iface
, lStart
, lEnd
, lRate
);
310 This
->bFixedStream
= TRUE
;
312 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
315 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
317 ICOM_THIS(IGetFrameImpl
,iface
);
319 TRACE("(%p)\n", iface
);
321 This
->bFixedStream
= FALSE
;
326 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
327 LPBITMAPINFOHEADER lpbiWanted
,
328 LPVOID lpBits
, INT x
, INT y
,
331 ICOM_THIS(IGetFrameImpl
,iface
);
333 AVISTREAMINFOW sInfo
;
334 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
335 BOOL bBestDisplay
= FALSE
;
337 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
340 if (This
->pStream
== NULL
)
343 if ((LONG
)lpbiWanted
== AVIGETFRAMEF_BESTDISPLAYFMT
) {
348 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
349 if (sInfo
.fccType
!= streamtypeVIDEO
)
350 return AVIERR_UNSUPPORTED
;
352 This
->bFormatChanges
=
353 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
354 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
355 This
->dwEditCount
= sInfo
.dwEditCount
;
356 This
->lCurrentFrame
= -1;
358 /* get input format from stream */
359 if (This
->lpInFormat
== NULL
) {
362 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
363 if (This
->cbInBuffer
== 0)
364 This
->cbInBuffer
= 1024;
366 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
367 NULL
, &This
->cbInFormat
);
370 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, This
->cbInFormat
+ This
->cbInBuffer
);
371 if (This
->lpInFormat
== NULL
) {
372 AVIFILE_CloseCompressor(This
);
373 return AVIERR_MEMORY
;
376 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
378 AVIFILE_CloseCompressor(This
);
382 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
385 /* check input format */
386 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
387 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
388 if (This
->lpInFormat
->biSizeImage
== 0 &&
389 This
->lpInFormat
->biCompression
== BI_RGB
) {
390 This
->lpInFormat
->biSizeImage
=
391 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
394 /* only to pass through? */
395 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
397 (lpbi
->biCompression
== BI_RGB
&&
398 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
399 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
400 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
401 This
->lpOutFormat
= This
->lpInFormat
;
402 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
407 /* need memory for output format? */
408 if (This
->lpOutFormat
== NULL
) {
410 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, sizeof(BITMAPINFOHEADER
)
411 + 256 * sizeof(RGBQUAD
));
412 if (This
->lpOutFormat
== NULL
) {
413 AVIFILE_CloseCompressor(This
);
414 return AVIERR_MEMORY
;
418 /* need handle to video compressor */
419 if (This
->hic
== NULL
) {
422 if (This
->lpInFormat
->biCompression
== BI_RGB
)
423 fccHandler
= comptypeDIB
;
424 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
425 fccHandler
= mmioFOURCC('R','L','E',' ');
427 fccHandler
= sInfo
.fccHandler
;
430 if (lpbi
->biWidth
== 0)
431 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
432 if (lpbi
->biHeight
== 0)
433 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
436 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
437 if (This
->hic
== NULL
) {
438 AVIFILE_CloseCompressor(This
);
439 return AVIERR_NOCOMPRESSOR
;
443 /* output format given? */
445 /* check the given output format ... */
446 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
447 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
449 /* ... and remember it */
450 memcpy(This
->lpOutFormat
, lpbi
,
451 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
452 if (lpbi
->biBitCount
<= 8)
453 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
458 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
459 This
->lpOutFormat
, 0, dx
, dy
);
460 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
461 This
->lpOutFormat
) < 0) {
462 AVIFILE_CloseCompressor(This
);
463 return AVIERR_NOCOMPRESSOR
;
466 /* check output format */
467 if (This
->lpOutFormat
->biClrUsed
== 0 &&
468 This
->lpOutFormat
->biBitCount
<= 8)
469 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
470 if (This
->lpOutFormat
->biSizeImage
== 0 &&
471 This
->lpOutFormat
->biCompression
== BI_RGB
) {
472 This
->lpOutFormat
->biSizeImage
=
473 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
476 if (lpBits
== NULL
) {
477 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
479 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
481 (LPBITMAPINFOHEADER
)GlobalReAllocPtr(This
->lpOutFormat
, size
, GMEM_MOVEABLE
);
482 if (This
->lpOutFormat
== NULL
) {
483 AVIFILE_CloseCompressor(This
);
484 return AVIERR_MEMORY
;
486 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
488 This
->lpOutBuffer
= lpBits
;
490 /* for user size was irrelevant */
492 dx
= This
->lpOutFormat
->biWidth
;
494 dy
= This
->lpOutFormat
->biHeight
;
496 /* need to resize? */
497 if (x
!= 0 || y
!= 0) {
498 if (dy
== This
->lpOutFormat
->biHeight
&&
499 dx
== This
->lpOutFormat
->biWidth
)
500 This
->bResize
= FALSE
;
502 This
->bResize
= TRUE
;
511 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
512 0,This
->lpInFormat
->biWidth
,
513 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
514 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
516 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
517 This
->lpOutFormat
) == ICERR_OK
)
520 AVIFILE_CloseCompressor(This
);
522 return AVIERR_COMPRESSOR
;
526 /***********************************************************************/