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
);
143 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
145 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
150 TRACE("(%p,%d)\n", iface
, lPos
);
152 /* We don't want negative start values! -- marks invalid buffer content */
157 if (This
->pStream
== NULL
)
159 if (This
->lpInFormat
== NULL
)
162 /* Could stream have changed? */
163 if (! This
->bFixedStream
) {
164 AVISTREAMINFOW sInfo
;
166 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
168 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
169 This
->dwEditCount
= sInfo
.dwEditCount
;
170 This
->lCurrentFrame
= -1;
173 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
174 /* stream has changed */
175 if (This
->lpOutFormat
!= NULL
) {
178 bi
= *This
->lpOutFormat
;
179 AVIFILE_CloseCompressor(This
);
181 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
182 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
185 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
190 if (lPos
!= This
->lCurrentFrame
) {
191 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
194 return NULL
; /* frame doesn't exist */
195 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
196 lNext
= This
->lCurrentFrame
+ 1;
198 for (; lNext
<= lPos
; lNext
++) {
199 /* new format for this frame? */
200 if (This
->bFormatChanges
) {
201 IAVIStream_ReadFormat(This
->pStream
, lNext
,
202 This
->lpInFormat
, &This
->cbInFormat
);
203 if (This
->lpOutFormat
!= NULL
) {
204 if (This
->lpOutFormat
->biBitCount
<= 8)
205 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
210 /* read input frame */
211 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
212 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
213 /* not enough memory for input buffer? */
215 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
216 return NULL
; /* bad thing, but bad things will happen */
217 if (readBytes
<= 0) {
218 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
222 /* IAVIStream::Read failed because of other reasons not buffersize? */
223 if (This
->cbInBuffer
>= readBytes
)
225 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
226 This
->lpInFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpInFormat
, This
->cbInBuffer
);
227 if (This
->lpInFormat
== NULL
)
228 return NULL
; /* out of memory */
229 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
232 if (readSamples
!= 1) {
233 ERR(": no frames read\n");
236 if (readBytes
!= 0) {
237 This
->lpInFormat
->biSizeImage
= readBytes
;
239 /* nothing to decompress? */
240 if (This
->hic
== NULL
) {
241 This
->lCurrentFrame
= lPos
;
242 return This
->lpInFormat
;
246 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
247 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
248 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
251 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
252 This
->lpOutFormat
, This
->lpOutBuffer
);
255 } /* for (lNext < lPos) */
256 } /* if (This->lCurrentFrame != lPos) */
258 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
261 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
262 LONG lEnd
, LONG lRate
)
264 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
266 TRACE("(%p,%d,%d,%d)\n", iface
, lStart
, lEnd
, lRate
);
268 This
->bFixedStream
= TRUE
;
270 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
273 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
275 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
277 TRACE("(%p)\n", iface
);
279 This
->bFixedStream
= FALSE
;
284 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
285 LPBITMAPINFOHEADER lpbiWanted
,
286 LPVOID lpBits
, INT x
, INT y
,
289 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
291 AVISTREAMINFOW sInfo
;
292 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
293 BOOL bBestDisplay
= FALSE
;
295 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
298 if (This
->pStream
== NULL
)
301 if (lpbiWanted
== (LPBITMAPINFOHEADER
)AVIGETFRAMEF_BESTDISPLAYFMT
) {
306 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
307 if (sInfo
.fccType
!= streamtypeVIDEO
)
308 return AVIERR_UNSUPPORTED
;
310 This
->bFormatChanges
=
311 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
312 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
313 This
->dwEditCount
= sInfo
.dwEditCount
;
314 This
->lCurrentFrame
= -1;
316 /* get input format from stream */
317 if (This
->lpInFormat
== NULL
) {
320 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
321 if (This
->cbInBuffer
== 0)
322 This
->cbInBuffer
= 1024;
324 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
325 NULL
, &This
->cbInFormat
);
327 This
->lpInFormat
= HeapAlloc(GetProcessHeap(), 0, This
->cbInFormat
+ This
->cbInBuffer
);
328 if (This
->lpInFormat
== NULL
) {
329 AVIFILE_CloseCompressor(This
);
330 return AVIERR_MEMORY
;
333 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
335 AVIFILE_CloseCompressor(This
);
339 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
342 /* check input format */
343 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
344 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
345 if (This
->lpInFormat
->biSizeImage
== 0 &&
346 This
->lpInFormat
->biCompression
== BI_RGB
) {
347 This
->lpInFormat
->biSizeImage
=
348 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
351 /* only to pass through? */
352 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
354 (lpbi
->biCompression
== BI_RGB
&&
355 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
356 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
357 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
358 This
->lpOutFormat
= This
->lpInFormat
;
359 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
364 /* need memory for output format? */
365 if (This
->lpOutFormat
== NULL
) {
367 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
368 if (This
->lpOutFormat
== NULL
) {
369 AVIFILE_CloseCompressor(This
);
370 return AVIERR_MEMORY
;
374 /* need handle to video compressor */
375 if (This
->hic
== NULL
) {
378 if (This
->lpInFormat
->biCompression
== BI_RGB
)
379 fccHandler
= comptypeDIB
;
380 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
381 fccHandler
= mmioFOURCC('R','L','E',' ');
383 fccHandler
= sInfo
.fccHandler
;
386 if (lpbi
->biWidth
== 0)
387 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
388 if (lpbi
->biHeight
== 0)
389 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
392 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
393 if (This
->hic
== NULL
) {
394 AVIFILE_CloseCompressor(This
);
395 return AVIERR_NOCOMPRESSOR
;
399 /* output format given? */
401 /* check the given output format ... */
402 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
403 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
405 /* ... and remember it */
406 memcpy(This
->lpOutFormat
, lpbi
,
407 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
408 if (lpbi
->biBitCount
<= 8)
409 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
414 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
415 This
->lpOutFormat
, 0, dx
, dy
);
416 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
417 This
->lpOutFormat
) < 0) {
418 AVIFILE_CloseCompressor(This
);
419 return AVIERR_NOCOMPRESSOR
;
422 /* check output format */
423 if (This
->lpOutFormat
->biClrUsed
== 0 &&
424 This
->lpOutFormat
->biBitCount
<= 8)
425 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
426 if (This
->lpOutFormat
->biSizeImage
== 0 &&
427 This
->lpOutFormat
->biCompression
== BI_RGB
) {
428 This
->lpOutFormat
->biSizeImage
=
429 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
432 if (lpBits
== NULL
) {
433 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
435 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
436 This
->lpOutFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpOutFormat
, size
);
437 if (This
->lpOutFormat
== NULL
) {
438 AVIFILE_CloseCompressor(This
);
439 return AVIERR_MEMORY
;
441 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
443 This
->lpOutBuffer
= lpBits
;
445 /* for user size was irrelevant */
447 dx
= This
->lpOutFormat
->biWidth
;
449 dy
= This
->lpOutFormat
->biHeight
;
451 /* need to resize? */
452 if (x
!= 0 || y
!= 0) {
453 if (dy
== This
->lpOutFormat
->biHeight
&&
454 dx
== This
->lpOutFormat
->biWidth
)
455 This
->bResize
= FALSE
;
457 This
->bResize
= TRUE
;
466 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
467 0,This
->lpInFormat
->biWidth
,
468 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
469 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
471 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
472 This
->lpOutFormat
) == ICERR_OK
)
475 AVIFILE_CloseCompressor(This
);
477 return AVIERR_COMPRESSOR
;
481 static const struct IGetFrameVtbl igetframeVtbl
= {
482 IGetFrame_fnQueryInterface
,
485 IGetFrame_fnGetFrame
,
488 IGetFrame_fnSetFormat
491 PGETFRAME
AVIFILE_CreateGetFrame(PAVISTREAM pStream
)
495 /* check parameter */
499 pg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IGetFrameImpl
));
501 pg
->IGetFrame_iface
.lpVtbl
= &igetframeVtbl
;
503 pg
->lCurrentFrame
= -1;
504 pg
->pStream
= pStream
;
505 IAVIStream_AddRef(pStream
);
508 return (PGETFRAME
)pg
;
511 /***********************************************************************/