2 * Copyright 2002 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
26 #include "avifile_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
33 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
34 (lp)->biClrUsed * sizeof(RGBQUAD))
37 /***********************************************************************/
39 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
40 REFIID refiid
, LPVOID
*obj
);
41 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
);
42 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
);
43 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
);
44 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
45 LONG lEnd
, LONG lRate
);
46 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
);
47 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
48 LPBITMAPINFOHEADER lpbi
,
49 LPVOID lpBits
, INT x
, INT y
,
52 struct ICOM_VTABLE(IGetFrame
) igetframeVtbl
= {
53 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
54 IGetFrame_fnQueryInterface
,
63 typedef struct _IGetFrameImpl
{
65 ICOM_VFIELD(IGetFrame
);
74 LPBITMAPINFOHEADER lpInFormat
;
78 LPBITMAPINFOHEADER lpOutFormat
;
89 DWORD dwFormatChangeCount
;
93 /***********************************************************************/
95 static void AVIFILE_CloseCompressor(IGetFrameImpl
*This
)
97 if (This
->lpOutFormat
!= NULL
&& This
->lpInFormat
!= This
->lpOutFormat
) {
98 GlobalFreePtr(This
->lpOutFormat
);
99 This
->lpOutFormat
= NULL
;
101 if (This
->lpInFormat
!= NULL
) {
102 GlobalFreePtr(This
->lpInFormat
);
103 This
->lpInFormat
= NULL
;
105 if (This
->hic
!= (HIC
)NULL
) {
107 ICDecompressExEnd(This
->hic
);
109 ICDecompressEnd(This
->hic
);
111 This
->hic
= (HIC
)NULL
;
115 PGETFRAME
AVIFILE_CreateGetFrame(PAVISTREAM pStream
)
119 /* check parameter */
123 pg
= (IGetFrameImpl
*)LocalAlloc(LPTR
, sizeof(IGetFrameImpl
));
125 ICOM_VTBL(pg
) = &igetframeVtbl
;
127 pg
->lCurrentFrame
= -1;
128 pg
->pStream
= pStream
;
129 IAVIStream_AddRef(pStream
);
132 return (PGETFRAME
)pg
;
135 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
136 REFIID refiid
, LPVOID
*obj
)
138 ICOM_THIS(IGetFrameImpl
,iface
);
140 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
142 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
143 IsEqualGUID(&IID_IGetFrame
, refiid
)) {
148 return OLE_E_ENUM_NOMORE
;
151 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
)
153 ICOM_THIS(IGetFrameImpl
,iface
);
155 TRACE("(%p)\n", iface
);
157 return ++(This
->ref
);
160 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
162 ICOM_THIS(IGetFrameImpl
,iface
);
164 TRACE("(%p)\n", iface
);
166 if (!--(This
->ref
)) {
167 AVIFILE_CloseCompressor(This
);
168 if (This
->pStream
!= NULL
) {
169 AVIStreamRelease(This
->pStream
);
170 This
->pStream
= NULL
;
173 LocalFree((HLOCAL
)iface
);
180 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
182 ICOM_THIS(IGetFrameImpl
,iface
);
187 TRACE("(%p,%ld)\n", iface
, lPos
);
190 if (This
->pStream
== NULL
)
192 if (This
->lpInFormat
== NULL
)
195 /* Could stream have changed? */
196 if (! This
->bFixedStream
) {
197 AVISTREAMINFOW sInfo
;
199 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
201 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
202 This
->dwEditCount
= sInfo
.dwEditCount
;
203 This
->lCurrentFrame
= -1;
206 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
207 /* stream has changed */
208 if (This
->lpOutFormat
!= NULL
) {
211 memcpy(&bi
, This
->lpOutFormat
, sizeof(bi
));
212 AVIFILE_CloseCompressor(This
);
214 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
215 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
218 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
223 if (lPos
!= This
->lCurrentFrame
) {
224 LONG lNext
= AVIStreamFindSample(This
->pStream
, lPos
, FIND_KEY
|FIND_PREV
);
228 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
231 for (; lNext
< lPos
; lNext
++) {
232 /* new format for this frame? */
233 if (This
->bFormatChanges
) {
234 AVIStreamReadFormat(This
->pStream
, lNext
, This
->lpInFormat
, &This
->cbInFormat
);
235 if (This
->lpOutFormat
!= NULL
) {
236 if (This
->lpOutFormat
->biBitCount
<= 8)
237 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
242 /* read input frame */
243 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
244 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
245 /* not enough memory for input buffer? */
247 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
249 if (This
->cbInBuffer
>= readBytes
)
251 This
->lpInFormat
= GlobalReAllocPtr(This
->lpInFormat
, This
->cbInFormat
+ readBytes
, 0);
252 if (This
->lpInFormat
== NULL
)
254 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
257 if (readSamples
!= 1)
259 if (readBytes
!= 0) {
260 This
->lpInFormat
->biSizeImage
= readBytes
;
262 /* nothing to decompress? */
263 if (This
->hic
== (HIC
)NULL
) {
264 This
->lCurrentFrame
= lPos
;
265 return This
->lpInFormat
;
269 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
270 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
271 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
274 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
275 This
->lpOutFormat
, This
->lpOutBuffer
);
278 } /* for (lNext < lPos) */
279 } /* if (This->lCurrentFrame != lPos) */
281 return (This
->hic
== (HIC
)NULL
? This
->lpInFormat
: This
->lpOutFormat
);
284 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
285 LONG lEnd
, LONG lRate
)
287 ICOM_THIS(IGetFrameImpl
,iface
);
289 TRACE("(%p,%ld,%ld,%ld)\n", iface
, lStart
, lEnd
, lRate
);
291 This
->bFixedStream
= TRUE
;
293 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
296 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
298 ICOM_THIS(IGetFrameImpl
,iface
);
300 TRACE("(%p)\n", iface
);
302 This
->bFixedStream
= FALSE
;
307 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
308 LPBITMAPINFOHEADER lpbiWanted
,
309 LPVOID lpBits
, INT x
, INT y
,
312 ICOM_THIS(IGetFrameImpl
,iface
);
314 AVISTREAMINFOW sInfo
;
315 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
316 BOOL bBestDisplay
= FALSE
;
318 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
321 if (This
->pStream
== NULL
)
324 if ((LONG
)lpbiWanted
== AVIGETFRAMEF_BESTDISPLAYFMT
) {
329 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
330 if (sInfo
.fccType
!= streamtypeVIDEO
)
331 return AVIERR_UNSUPPORTED
;
333 This
->bFormatChanges
=
334 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
335 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
336 This
->dwEditCount
= sInfo
.dwEditCount
;
337 This
->lCurrentFrame
= -1;
339 /* get input format from stream */
340 if (This
->lpInFormat
== NULL
) {
343 This
->cbInBuffer
= sInfo
.dwSuggestedBufferSize
;
344 if (This
->cbInBuffer
== 0)
345 This
->cbInBuffer
= 1024;
347 AVIStreamFormatSize(This
->pStream
, sInfo
.dwStart
, &This
->cbInFormat
);
350 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, This
->cbInFormat
+ This
->cbInBuffer
);
351 if (This
->lpInFormat
== NULL
) {
352 AVIFILE_CloseCompressor(This
);
353 return AVIERR_MEMORY
;
356 hr
= AVIStreamReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
358 AVIFILE_CloseCompressor(This
);
362 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
365 /* check input format */
366 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
367 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
368 if (This
->lpInFormat
->biSizeImage
== 0 &&
369 This
->lpInFormat
->biCompression
== BI_RGB
) {
370 This
->lpInFormat
->biSizeImage
=
371 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
374 /* only to pass through? */
375 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
377 (lpbi
->biCompression
== BI_RGB
&&
378 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
379 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
380 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
381 This
->lpOutFormat
= This
->lpInFormat
;
382 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
387 /* need memory for output format? */
388 if (This
->lpOutFormat
== NULL
) {
390 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, sizeof(BITMAPINFOHEADER
)
391 + 256 * sizeof(RGBQUAD
));
392 if (This
->lpOutFormat
== NULL
) {
393 AVIFILE_CloseCompressor(This
);
394 return AVIERR_MEMORY
;
398 /* need handle to video compressor */
399 if (This
->hic
== (HIC
)NULL
) {
402 if (This
->lpInFormat
->biCompression
== BI_RGB
)
403 fccHandler
= comptypeDIB
;
404 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
405 fccHandler
= mmioFOURCC('R','L','E',' ');
407 fccHandler
= sInfo
.fccHandler
;
410 if (lpbi
->biWidth
== 0)
411 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
412 if (lpbi
->biHeight
== 0)
413 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
416 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
417 if (This
->hic
== (HIC
)NULL
) {
418 AVIFILE_CloseCompressor(This
);
419 return AVIERR_NOCOMPRESSOR
;
423 /* output format given? */
425 /* check the given output format ... */
426 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
427 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
429 /* ... and remember it */
430 memcpy(This
->lpOutFormat
, lpbi
,
431 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
432 if (lpbi
->biBitCount
<= 8)
433 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
438 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
439 This
->lpOutFormat
, 0, dx
, dy
);
440 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
441 This
->lpOutFormat
) < 0) {
442 AVIFILE_CloseCompressor(This
);
443 return AVIERR_NOCOMPRESSOR
;
446 /* check output format */
447 if (This
->lpOutFormat
->biClrUsed
== 0 &&
448 This
->lpOutFormat
->biBitCount
<= 8)
449 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
450 if (This
->lpOutFormat
->biSizeImage
== 0 &&
451 This
->lpOutFormat
->biCompression
== BI_RGB
) {
452 This
->lpOutFormat
->biSizeImage
=
453 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
456 if (lpBits
== NULL
) {
457 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
459 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
461 (LPBITMAPINFOHEADER
)GlobalReAllocPtr(This
->lpOutFormat
, size
, GMEM_MOVEABLE
);
462 if (This
->lpOutFormat
== NULL
) {
463 AVIFILE_CloseCompressor(This
);
464 return AVIERR_MEMORY
;
466 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
468 This
->lpOutBuffer
= lpBits
;
470 /* for user size was irrelevant */
472 dx
= This
->lpOutFormat
->biWidth
;
474 dy
= This
->lpOutFormat
->biHeight
;
476 /* need to resize? */
477 if (x
!= 0 || y
!= 0) {
478 if (dy
== This
->lpOutFormat
->biHeight
&&
479 dx
== This
->lpOutFormat
->biWidth
)
480 This
->bResize
= FALSE
;
482 This
->bResize
= TRUE
;
491 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
492 0,This
->lpInFormat
->biWidth
,
493 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
494 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
496 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
497 This
->lpOutFormat
) == ICERR_OK
)
500 AVIFILE_CloseCompressor(This
);
502 return AVIERR_COMPRESSOR
;
506 /***********************************************************************/