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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "avifile_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
34 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
35 (lp)->biClrUsed * sizeof(RGBQUAD))
38 /***********************************************************************/
40 typedef struct _IGetFrameImpl
{
42 IGetFrame IGetFrame_iface
;
51 LPBITMAPINFOHEADER lpInFormat
;
55 LPBITMAPINFOHEADER lpOutFormat
;
66 DWORD dwFormatChangeCount
;
70 /***********************************************************************/
72 static inline IGetFrameImpl
*impl_from_IGetFrame(IGetFrame
*iface
)
74 return CONTAINING_RECORD(iface
, IGetFrameImpl
, IGetFrame_iface
);
77 static void AVIFILE_CloseCompressor(IGetFrameImpl
*This
)
79 if (This
->lpInFormat
!= This
->lpOutFormat
) {
80 HeapFree(GetProcessHeap(), 0, This
->lpOutFormat
);
81 This
->lpOutFormat
= NULL
;
83 HeapFree(GetProcessHeap(), 0, This
->lpInFormat
);
84 This
->lpInFormat
= NULL
;
85 if (This
->hic
!= NULL
) {
87 ICDecompressExEnd(This
->hic
);
89 ICDecompressEnd(This
->hic
);
95 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
96 REFIID refiid
, LPVOID
*obj
)
98 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
100 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
102 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
103 IsEqualGUID(&IID_IGetFrame
, refiid
)) {
105 IGetFrame_AddRef(iface
);
109 return OLE_E_ENUM_NOMORE
;
112 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
)
114 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
115 ULONG ref
= InterlockedIncrement(&This
->ref
);
117 TRACE("(%p)\n", iface
);
122 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
124 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
125 ULONG ref
= InterlockedDecrement(&This
->ref
);
127 TRACE("(%p)\n", iface
);
130 AVIFILE_CloseCompressor(This
);
131 if (This
->pStream
!= NULL
) {
132 IAVIStream_Release(This
->pStream
);
133 This
->pStream
= NULL
;
136 HeapFree(GetProcessHeap(), 0, iface
);
142 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
144 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
149 TRACE("(%p,%d)\n", iface
, lPos
);
151 /* We don't want negative start values! -- marks invalid buffer content */
156 if (This
->pStream
== NULL
)
158 if (This
->lpInFormat
== NULL
)
161 /* Could stream have changed? */
162 if (! This
->bFixedStream
) {
163 AVISTREAMINFOW sInfo
;
165 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
167 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
168 This
->dwEditCount
= sInfo
.dwEditCount
;
169 This
->lCurrentFrame
= -1;
172 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
173 /* stream has changed */
174 if (This
->lpOutFormat
!= NULL
) {
177 bi
= *This
->lpOutFormat
;
178 AVIFILE_CloseCompressor(This
);
180 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
181 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
184 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
189 if (lPos
!= This
->lCurrentFrame
) {
190 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
193 return NULL
; /* frame doesn't exist */
194 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
195 lNext
= This
->lCurrentFrame
+ 1;
197 for (; lNext
<= lPos
; lNext
++) {
198 /* new format for this frame? */
199 if (This
->bFormatChanges
) {
200 IAVIStream_ReadFormat(This
->pStream
, lNext
,
201 This
->lpInFormat
, &This
->cbInFormat
);
202 if (This
->lpOutFormat
!= NULL
) {
203 if (This
->lpOutFormat
->biBitCount
<= 8)
204 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
209 /* read input frame */
210 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
211 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
212 /* not enough memory for input buffer? */
214 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
215 return NULL
; /* bad thing, but bad things will happen */
216 if (readBytes
<= 0) {
217 ERR(": IAVIStream::Read doesn't return needed bytes!\n");
221 /* IAVIStream::Read failed because of other reasons not buffersize? */
222 if (This
->cbInBuffer
>= readBytes
)
224 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
225 This
->lpInFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpInFormat
, This
->cbInBuffer
);
226 if (This
->lpInFormat
== NULL
)
227 return NULL
; /* out of memory */
228 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
231 if (readSamples
!= 1) {
232 ERR(": no frames read\n");
235 if (readBytes
!= 0) {
236 This
->lpInFormat
->biSizeImage
= readBytes
;
238 /* nothing to decompress? */
239 if (This
->hic
== NULL
) {
240 This
->lCurrentFrame
= lPos
;
241 return This
->lpInFormat
;
245 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
246 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
247 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
250 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
251 This
->lpOutFormat
, This
->lpOutBuffer
);
254 } /* for (lNext < lPos) */
255 } /* if (This->lCurrentFrame != lPos) */
257 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
260 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
261 LONG lEnd
, LONG lRate
)
263 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
265 TRACE("(%p,%d,%d,%d)\n", iface
, lStart
, lEnd
, lRate
);
267 This
->bFixedStream
= TRUE
;
269 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
272 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
274 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
276 TRACE("(%p)\n", iface
);
278 This
->bFixedStream
= FALSE
;
283 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
284 LPBITMAPINFOHEADER lpbiWanted
,
285 LPVOID lpBits
, INT x
, INT y
,
288 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
290 AVISTREAMINFOW sInfo
;
291 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
292 BOOL bBestDisplay
= FALSE
;
294 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
297 if (This
->pStream
== NULL
)
300 if (lpbiWanted
== (LPBITMAPINFOHEADER
)AVIGETFRAMEF_BESTDISPLAYFMT
) {
305 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
306 if (sInfo
.fccType
!= streamtypeVIDEO
)
307 return AVIERR_UNSUPPORTED
;
309 This
->bFormatChanges
= (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
) != 0;
310 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
311 This
->dwEditCount
= sInfo
.dwEditCount
;
312 This
->lCurrentFrame
= -1;
314 /* get input format from stream */
315 if (This
->lpInFormat
== NULL
) {
318 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
319 if (This
->cbInBuffer
== 0)
320 This
->cbInBuffer
= 1024;
322 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
323 NULL
, &This
->cbInFormat
);
325 This
->lpInFormat
= HeapAlloc(GetProcessHeap(), 0, This
->cbInFormat
+ This
->cbInBuffer
);
326 if (This
->lpInFormat
== NULL
) {
327 AVIFILE_CloseCompressor(This
);
328 return AVIERR_MEMORY
;
331 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
333 AVIFILE_CloseCompressor(This
);
337 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
340 /* check input format */
341 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
342 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
343 if (This
->lpInFormat
->biSizeImage
== 0 &&
344 This
->lpInFormat
->biCompression
== BI_RGB
) {
345 This
->lpInFormat
->biSizeImage
=
346 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
349 /* only to pass through? */
350 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
352 (lpbi
->biCompression
== BI_RGB
&&
353 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
354 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
355 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
356 This
->lpOutFormat
= This
->lpInFormat
;
357 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
362 /* need memory for output format? */
363 if (This
->lpOutFormat
== NULL
) {
365 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
366 if (This
->lpOutFormat
== NULL
) {
367 AVIFILE_CloseCompressor(This
);
368 return AVIERR_MEMORY
;
372 /* need handle to video compressor */
373 if (This
->hic
== NULL
) {
376 if (This
->lpInFormat
->biCompression
== BI_RGB
)
377 fccHandler
= comptypeDIB
;
378 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
379 fccHandler
= mmioFOURCC('R','L','E',' ');
381 fccHandler
= sInfo
.fccHandler
;
384 if (lpbi
->biWidth
== 0)
385 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
386 if (lpbi
->biHeight
== 0)
387 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
390 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
391 if (This
->hic
== NULL
) {
392 AVIFILE_CloseCompressor(This
);
393 return AVIERR_NOCOMPRESSOR
;
397 /* output format given? */
399 /* check the given output format ... */
400 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
401 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
403 /* ... and remember it */
404 memcpy(This
->lpOutFormat
, lpbi
,
405 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
406 if (lpbi
->biBitCount
<= 8)
407 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
410 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
411 This
->lpOutFormat
, 0, dx
, dy
);
412 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
413 This
->lpOutFormat
) < 0) {
414 AVIFILE_CloseCompressor(This
);
415 return AVIERR_NOCOMPRESSOR
;
419 /* check output format */
420 if (This
->lpOutFormat
->biClrUsed
== 0 &&
421 This
->lpOutFormat
->biBitCount
<= 8)
422 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
423 if (This
->lpOutFormat
->biSizeImage
== 0 &&
424 This
->lpOutFormat
->biCompression
== BI_RGB
) {
425 This
->lpOutFormat
->biSizeImage
=
426 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
429 if (lpBits
== NULL
) {
430 DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
432 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
433 This
->lpOutFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpOutFormat
, size
);
434 if (This
->lpOutFormat
== NULL
) {
435 AVIFILE_CloseCompressor(This
);
436 return AVIERR_MEMORY
;
438 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
440 This
->lpOutBuffer
= lpBits
;
442 /* for user size was irrelevant */
444 dx
= This
->lpOutFormat
->biWidth
;
446 dy
= This
->lpOutFormat
->biHeight
;
448 /* need to resize? */
449 if (x
!= 0 || y
!= 0) {
450 if (dy
== This
->lpOutFormat
->biHeight
&&
451 dx
== This
->lpOutFormat
->biWidth
)
452 This
->bResize
= FALSE
;
454 This
->bResize
= TRUE
;
463 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
464 0,This
->lpInFormat
->biWidth
,
465 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
466 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
468 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
469 This
->lpOutFormat
) == ICERR_OK
)
472 AVIFILE_CloseCompressor(This
);
474 return AVIERR_COMPRESSOR
;
477 static const struct IGetFrameVtbl igetframeVtbl
= {
478 IGetFrame_fnQueryInterface
,
481 IGetFrame_fnGetFrame
,
484 IGetFrame_fnSetFormat
487 PGETFRAME
AVIFILE_CreateGetFrame(PAVISTREAM pStream
)
491 /* check parameter */
495 pg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IGetFrameImpl
));
497 pg
->IGetFrame_iface
.lpVtbl
= &igetframeVtbl
;
499 pg
->lCurrentFrame
= -1;
500 pg
->pStream
= pStream
;
501 IAVIStream_AddRef(pStream
);
504 return &pg
->IGetFrame_iface
;
507 /***********************************************************************/