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 IGetFrameVtbl igetframeVtbl
= {
58 IGetFrame_fnQueryInterface
,
67 typedef struct _IGetFrameImpl
{
69 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
;
159 TRACE("(%p)\n", iface
);
161 return ++(This
->ref
);
164 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
166 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
168 TRACE("(%p)\n", iface
);
170 if (!--(This
->ref
)) {
171 AVIFILE_CloseCompressor(This
);
172 if (This
->pStream
!= NULL
) {
173 IAVIStream_Release(This
->pStream
);
174 This
->pStream
= NULL
;
177 LocalFree((HLOCAL
)iface
);
184 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
186 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
191 TRACE("(%p,%ld)\n", iface
, lPos
);
193 /* We don't want negative start values! -- marks invalid buffer content */
198 if (This
->pStream
== NULL
)
200 if (This
->lpInFormat
== NULL
)
203 /* Could stream have changed? */
204 if (! This
->bFixedStream
) {
205 AVISTREAMINFOW sInfo
;
207 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
209 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
210 This
->dwEditCount
= sInfo
.dwEditCount
;
211 This
->lCurrentFrame
= -1;
214 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
215 /* stream has changed */
216 if (This
->lpOutFormat
!= NULL
) {
219 memcpy(&bi
, This
->lpOutFormat
, sizeof(bi
));
220 AVIFILE_CloseCompressor(This
);
222 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
223 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
226 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
231 if (lPos
!= This
->lCurrentFrame
) {
232 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
235 return NULL
; /* frame doesn't exist */
236 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
237 lNext
= This
->lCurrentFrame
+ 1;
239 for (; lNext
<= lPos
; lNext
++) {
240 /* new format for this frame? */
241 if (This
->bFormatChanges
) {
242 IAVIStream_ReadFormat(This
->pStream
, lNext
,
243 This
->lpInFormat
, &This
->cbInFormat
);
244 if (This
->lpOutFormat
!= NULL
) {
245 if (This
->lpOutFormat
->biBitCount
<= 8)
246 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
251 /* read input frame */
252 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
253 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
254 /* not enough memory for input buffer? */
256 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
257 return NULL
; /* bad thing, but bad things will happen */
258 if (readBytes
<= 0) {
259 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
263 /* IAVIStream::Read failed because of other reasons not buffersize? */
264 if (This
->cbInBuffer
>= readBytes
)
266 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
267 This
->lpInFormat
= GlobalReAllocPtr(This
->lpInFormat
, This
->cbInBuffer
, 0);
268 if (This
->lpInFormat
== NULL
)
269 return NULL
; /* out of memory */
270 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
273 if (readSamples
!= 1) {
274 ERR(": no frames read\n");
277 if (readBytes
!= 0) {
278 This
->lpInFormat
->biSizeImage
= readBytes
;
280 /* nothing to decompress? */
281 if (This
->hic
== NULL
) {
282 This
->lCurrentFrame
= lPos
;
283 return This
->lpInFormat
;
287 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
288 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
289 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
292 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
293 This
->lpOutFormat
, This
->lpOutBuffer
);
296 } /* for (lNext < lPos) */
297 } /* if (This->lCurrentFrame != lPos) */
299 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
302 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
303 LONG lEnd
, LONG lRate
)
305 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
307 TRACE("(%p,%ld,%ld,%ld)\n", iface
, lStart
, lEnd
, lRate
);
309 This
->bFixedStream
= TRUE
;
311 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
314 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
316 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
318 TRACE("(%p)\n", iface
);
320 This
->bFixedStream
= FALSE
;
325 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
326 LPBITMAPINFOHEADER lpbiWanted
,
327 LPVOID lpBits
, INT x
, INT y
,
330 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
332 AVISTREAMINFOW sInfo
;
333 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
334 BOOL bBestDisplay
= FALSE
;
336 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
339 if (This
->pStream
== NULL
)
342 if ((LONG
)lpbiWanted
== AVIGETFRAMEF_BESTDISPLAYFMT
) {
347 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
348 if (sInfo
.fccType
!= streamtypeVIDEO
)
349 return AVIERR_UNSUPPORTED
;
351 This
->bFormatChanges
=
352 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
353 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
354 This
->dwEditCount
= sInfo
.dwEditCount
;
355 This
->lCurrentFrame
= -1;
357 /* get input format from stream */
358 if (This
->lpInFormat
== NULL
) {
361 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
362 if (This
->cbInBuffer
== 0)
363 This
->cbInBuffer
= 1024;
365 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
366 NULL
, &This
->cbInFormat
);
369 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, This
->cbInFormat
+ This
->cbInBuffer
);
370 if (This
->lpInFormat
== NULL
) {
371 AVIFILE_CloseCompressor(This
);
372 return AVIERR_MEMORY
;
375 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
377 AVIFILE_CloseCompressor(This
);
381 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
384 /* check input format */
385 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
386 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
387 if (This
->lpInFormat
->biSizeImage
== 0 &&
388 This
->lpInFormat
->biCompression
== BI_RGB
) {
389 This
->lpInFormat
->biSizeImage
=
390 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
393 /* only to pass through? */
394 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
396 (lpbi
->biCompression
== BI_RGB
&&
397 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
398 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
399 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
400 This
->lpOutFormat
= This
->lpInFormat
;
401 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
406 /* need memory for output format? */
407 if (This
->lpOutFormat
== NULL
) {
409 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, sizeof(BITMAPINFOHEADER
)
410 + 256 * sizeof(RGBQUAD
));
411 if (This
->lpOutFormat
== NULL
) {
412 AVIFILE_CloseCompressor(This
);
413 return AVIERR_MEMORY
;
417 /* need handle to video compressor */
418 if (This
->hic
== NULL
) {
421 if (This
->lpInFormat
->biCompression
== BI_RGB
)
422 fccHandler
= comptypeDIB
;
423 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
424 fccHandler
= mmioFOURCC('R','L','E',' ');
426 fccHandler
= sInfo
.fccHandler
;
429 if (lpbi
->biWidth
== 0)
430 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
431 if (lpbi
->biHeight
== 0)
432 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
435 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
436 if (This
->hic
== NULL
) {
437 AVIFILE_CloseCompressor(This
);
438 return AVIERR_NOCOMPRESSOR
;
442 /* output format given? */
444 /* check the given output format ... */
445 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
446 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
448 /* ... and remember it */
449 memcpy(This
->lpOutFormat
, lpbi
,
450 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
451 if (lpbi
->biBitCount
<= 8)
452 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
457 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
458 This
->lpOutFormat
, 0, dx
, dy
);
459 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
460 This
->lpOutFormat
) < 0) {
461 AVIFILE_CloseCompressor(This
);
462 return AVIERR_NOCOMPRESSOR
;
465 /* check output format */
466 if (This
->lpOutFormat
->biClrUsed
== 0 &&
467 This
->lpOutFormat
->biBitCount
<= 8)
468 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
469 if (This
->lpOutFormat
->biSizeImage
== 0 &&
470 This
->lpOutFormat
->biCompression
== BI_RGB
) {
471 This
->lpOutFormat
->biSizeImage
=
472 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
475 if (lpBits
== NULL
) {
476 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
478 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
480 (LPBITMAPINFOHEADER
)GlobalReAllocPtr(This
->lpOutFormat
, size
, GMEM_MOVEABLE
);
481 if (This
->lpOutFormat
== NULL
) {
482 AVIFILE_CloseCompressor(This
);
483 return AVIERR_MEMORY
;
485 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
487 This
->lpOutBuffer
= lpBits
;
489 /* for user size was irrelevant */
491 dx
= This
->lpOutFormat
->biWidth
;
493 dy
= This
->lpOutFormat
->biHeight
;
495 /* need to resize? */
496 if (x
!= 0 || y
!= 0) {
497 if (dy
== This
->lpOutFormat
->biHeight
&&
498 dx
== This
->lpOutFormat
->biWidth
)
499 This
->bResize
= FALSE
;
501 This
->bResize
= TRUE
;
510 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
511 0,This
->lpInFormat
->biWidth
,
512 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
513 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
515 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
516 This
->lpOutFormat
) == ICERR_OK
)
519 AVIFILE_CloseCompressor(This
);
521 return AVIERR_COMPRESSOR
;
525 /***********************************************************************/