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
30 #include "avifile_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
37 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
38 (lp)->biClrUsed * sizeof(RGBQUAD))
41 /***********************************************************************/
43 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
44 REFIID refiid
, LPVOID
*obj
);
45 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
);
46 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
);
47 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
);
48 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
49 LONG lEnd
, LONG lRate
);
50 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
);
51 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
52 LPBITMAPINFOHEADER lpbi
,
53 LPVOID lpBits
, INT x
, INT y
,
56 struct ICOM_VTABLE(IGetFrame
) igetframeVtbl
= {
57 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
58 IGetFrame_fnQueryInterface
,
67 typedef struct _IGetFrameImpl
{
69 ICOM_VFIELD(IGetFrame
);
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 ICOM_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 ICOM_THIS(IGetFrameImpl
,iface
);
159 TRACE("(%p)\n", iface
);
161 return ++(This
->ref
);
164 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
166 ICOM_THIS(IGetFrameImpl
,iface
);
168 TRACE("(%p)\n", iface
);
170 if (!--(This
->ref
)) {
171 AVIFILE_CloseCompressor(This
);
172 if (This
->pStream
!= NULL
) {
173 AVIStreamRelease(This
->pStream
);
174 This
->pStream
= NULL
;
177 LocalFree((HLOCAL
)iface
);
184 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
186 ICOM_THIS(IGetFrameImpl
,iface
);
191 TRACE("(%p,%ld)\n", iface
, lPos
);
194 if (This
->pStream
== NULL
)
196 if (This
->lpInFormat
== NULL
)
199 /* Could stream have changed? */
200 if (! This
->bFixedStream
) {
201 AVISTREAMINFOW sInfo
;
203 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
205 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
206 This
->dwEditCount
= sInfo
.dwEditCount
;
207 This
->lCurrentFrame
= -1;
210 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
211 /* stream has changed */
212 if (This
->lpOutFormat
!= NULL
) {
215 memcpy(&bi
, This
->lpOutFormat
, sizeof(bi
));
216 AVIFILE_CloseCompressor(This
);
218 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
219 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
222 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
227 if (lPos
!= This
->lCurrentFrame
) {
228 LONG lNext
= AVIStreamFindSample(This
->pStream
, lPos
, FIND_KEY
|FIND_PREV
);
232 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
233 lNext
= This
->lCurrentFrame
+ 1;
235 for (; lNext
<= lPos
; lNext
++) {
236 /* new format for this frame? */
237 if (This
->bFormatChanges
) {
238 AVIStreamReadFormat(This
->pStream
, lNext
, This
->lpInFormat
, &This
->cbInFormat
);
239 if (This
->lpOutFormat
!= NULL
) {
240 if (This
->lpOutFormat
->biBitCount
<= 8)
241 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
246 /* read input frame */
247 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
248 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
249 /* not enough memory for input buffer? */
251 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
253 if (This
->cbInBuffer
>= readBytes
)
255 This
->lpInFormat
= GlobalReAllocPtr(This
->lpInFormat
, This
->cbInFormat
+ readBytes
, 0);
256 if (This
->lpInFormat
== NULL
)
258 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
261 if (readSamples
!= 1)
263 if (readBytes
!= 0) {
264 This
->lpInFormat
->biSizeImage
= readBytes
;
266 /* nothing to decompress? */
267 if (This
->hic
== NULL
) {
268 This
->lCurrentFrame
= lPos
;
269 return This
->lpInFormat
;
273 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
274 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
275 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
278 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
279 This
->lpOutFormat
, This
->lpOutBuffer
);
282 } /* for (lNext < lPos) */
283 } /* if (This->lCurrentFrame != lPos) */
285 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
288 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
289 LONG lEnd
, LONG lRate
)
291 ICOM_THIS(IGetFrameImpl
,iface
);
293 TRACE("(%p,%ld,%ld,%ld)\n", iface
, lStart
, lEnd
, lRate
);
295 This
->bFixedStream
= TRUE
;
297 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
300 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
302 ICOM_THIS(IGetFrameImpl
,iface
);
304 TRACE("(%p)\n", iface
);
306 This
->bFixedStream
= FALSE
;
311 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
312 LPBITMAPINFOHEADER lpbiWanted
,
313 LPVOID lpBits
, INT x
, INT y
,
316 ICOM_THIS(IGetFrameImpl
,iface
);
318 AVISTREAMINFOW sInfo
;
319 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
320 BOOL bBestDisplay
= FALSE
;
322 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
325 if (This
->pStream
== NULL
)
328 if ((LONG
)lpbiWanted
== AVIGETFRAMEF_BESTDISPLAYFMT
) {
333 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
334 if (sInfo
.fccType
!= streamtypeVIDEO
)
335 return AVIERR_UNSUPPORTED
;
337 This
->bFormatChanges
=
338 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
339 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
340 This
->dwEditCount
= sInfo
.dwEditCount
;
341 This
->lCurrentFrame
= -1;
343 /* get input format from stream */
344 if (This
->lpInFormat
== NULL
) {
347 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
348 if (This
->cbInBuffer
== 0)
349 This
->cbInBuffer
= 1024;
351 AVIStreamFormatSize(This
->pStream
, sInfo
.dwStart
, &This
->cbInFormat
);
354 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, This
->cbInFormat
+ This
->cbInBuffer
);
355 if (This
->lpInFormat
== NULL
) {
356 AVIFILE_CloseCompressor(This
);
357 return AVIERR_MEMORY
;
360 hr
= AVIStreamReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
362 AVIFILE_CloseCompressor(This
);
366 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
369 /* check input format */
370 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
371 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
372 if (This
->lpInFormat
->biSizeImage
== 0 &&
373 This
->lpInFormat
->biCompression
== BI_RGB
) {
374 This
->lpInFormat
->biSizeImage
=
375 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
378 /* only to pass through? */
379 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
381 (lpbi
->biCompression
== BI_RGB
&&
382 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
383 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
384 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
385 This
->lpOutFormat
= This
->lpInFormat
;
386 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
391 /* need memory for output format? */
392 if (This
->lpOutFormat
== NULL
) {
394 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, sizeof(BITMAPINFOHEADER
)
395 + 256 * sizeof(RGBQUAD
));
396 if (This
->lpOutFormat
== NULL
) {
397 AVIFILE_CloseCompressor(This
);
398 return AVIERR_MEMORY
;
402 /* need handle to video compressor */
403 if (This
->hic
== NULL
) {
406 if (This
->lpInFormat
->biCompression
== BI_RGB
)
407 fccHandler
= comptypeDIB
;
408 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
409 fccHandler
= mmioFOURCC('R','L','E',' ');
411 fccHandler
= sInfo
.fccHandler
;
414 if (lpbi
->biWidth
== 0)
415 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
416 if (lpbi
->biHeight
== 0)
417 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
420 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
421 if (This
->hic
== NULL
) {
422 AVIFILE_CloseCompressor(This
);
423 return AVIERR_NOCOMPRESSOR
;
427 /* output format given? */
429 /* check the given output format ... */
430 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
431 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
433 /* ... and remember it */
434 memcpy(This
->lpOutFormat
, lpbi
,
435 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
436 if (lpbi
->biBitCount
<= 8)
437 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
442 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
443 This
->lpOutFormat
, 0, dx
, dy
);
444 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
445 This
->lpOutFormat
) < 0) {
446 AVIFILE_CloseCompressor(This
);
447 return AVIERR_NOCOMPRESSOR
;
450 /* check output format */
451 if (This
->lpOutFormat
->biClrUsed
== 0 &&
452 This
->lpOutFormat
->biBitCount
<= 8)
453 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
454 if (This
->lpOutFormat
->biSizeImage
== 0 &&
455 This
->lpOutFormat
->biCompression
== BI_RGB
) {
456 This
->lpOutFormat
->biSizeImage
=
457 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
460 if (lpBits
== NULL
) {
461 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
463 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
465 (LPBITMAPINFOHEADER
)GlobalReAllocPtr(This
->lpOutFormat
, size
, GMEM_MOVEABLE
);
466 if (This
->lpOutFormat
== NULL
) {
467 AVIFILE_CloseCompressor(This
);
468 return AVIERR_MEMORY
;
470 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
472 This
->lpOutBuffer
= lpBits
;
474 /* for user size was irrelevant */
476 dx
= This
->lpOutFormat
->biWidth
;
478 dy
= This
->lpOutFormat
->biHeight
;
480 /* need to resize? */
481 if (x
!= 0 || y
!= 0) {
482 if (dy
== This
->lpOutFormat
->biHeight
&&
483 dx
== This
->lpOutFormat
->biWidth
)
484 This
->bResize
= FALSE
;
486 This
->bResize
= TRUE
;
495 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
496 0,This
->lpInFormat
->biWidth
,
497 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
498 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
500 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
501 This
->lpOutFormat
) == ICERR_OK
)
504 AVIFILE_CloseCompressor(This
);
506 return AVIERR_COMPRESSOR
;
510 /***********************************************************************/