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
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 static const struct IGetFrameVtbl igetframeVtbl
= {
57 IGetFrame_fnQueryInterface
,
66 typedef struct _IGetFrameImpl
{
68 const IGetFrameVtbl
*lpVtbl
;
77 LPBITMAPINFOHEADER lpInFormat
;
81 LPBITMAPINFOHEADER lpOutFormat
;
92 DWORD dwFormatChangeCount
;
96 /***********************************************************************/
98 static void AVIFILE_CloseCompressor(IGetFrameImpl
*This
)
100 if (This
->lpInFormat
!= This
->lpOutFormat
) {
101 HeapFree(GetProcessHeap(), 0, This
->lpOutFormat
);
102 This
->lpOutFormat
= NULL
;
104 HeapFree(GetProcessHeap(), 0, This
->lpInFormat
);
105 This
->lpInFormat
= NULL
;
106 if (This
->hic
!= NULL
) {
108 ICDecompressExEnd(This
->hic
);
110 ICDecompressEnd(This
->hic
);
116 PGETFRAME
AVIFILE_CreateGetFrame(PAVISTREAM pStream
)
120 /* check parameter */
124 pg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IGetFrameImpl
));
126 pg
->lpVtbl
= &igetframeVtbl
;
128 pg
->lCurrentFrame
= -1;
129 pg
->pStream
= pStream
;
130 IAVIStream_AddRef(pStream
);
133 return (PGETFRAME
)pg
;
136 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
137 REFIID refiid
, LPVOID
*obj
)
139 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
141 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
143 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
144 IsEqualGUID(&IID_IGetFrame
, refiid
)) {
149 return OLE_E_ENUM_NOMORE
;
152 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
)
154 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
155 ULONG ref
= InterlockedIncrement(&This
->ref
);
157 TRACE("(%p)\n", iface
);
162 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
164 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
165 ULONG ref
= InterlockedDecrement(&This
->ref
);
167 TRACE("(%p)\n", iface
);
170 AVIFILE_CloseCompressor(This
);
171 if (This
->pStream
!= NULL
) {
172 IAVIStream_Release(This
->pStream
);
173 This
->pStream
= NULL
;
176 HeapFree(GetProcessHeap(), 0, iface
);
183 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
185 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
190 TRACE("(%p,%d)\n", iface
, lPos
);
192 /* We don't want negative start values! -- marks invalid buffer content */
197 if (This
->pStream
== NULL
)
199 if (This
->lpInFormat
== NULL
)
202 /* Could stream have changed? */
203 if (! This
->bFixedStream
) {
204 AVISTREAMINFOW sInfo
;
206 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
208 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
209 This
->dwEditCount
= sInfo
.dwEditCount
;
210 This
->lCurrentFrame
= -1;
213 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
214 /* stream has changed */
215 if (This
->lpOutFormat
!= NULL
) {
218 memcpy(&bi
, This
->lpOutFormat
, sizeof(bi
));
219 AVIFILE_CloseCompressor(This
);
221 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
222 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
225 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
230 if (lPos
!= This
->lCurrentFrame
) {
231 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
234 return NULL
; /* frame doesn't exist */
235 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
236 lNext
= This
->lCurrentFrame
+ 1;
238 for (; lNext
<= lPos
; lNext
++) {
239 /* new format for this frame? */
240 if (This
->bFormatChanges
) {
241 IAVIStream_ReadFormat(This
->pStream
, lNext
,
242 This
->lpInFormat
, &This
->cbInFormat
);
243 if (This
->lpOutFormat
!= NULL
) {
244 if (This
->lpOutFormat
->biBitCount
<= 8)
245 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
250 /* read input frame */
251 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
252 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
253 /* not enough memory for input buffer? */
255 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
256 return NULL
; /* bad thing, but bad things will happen */
257 if (readBytes
<= 0) {
258 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
262 /* IAVIStream::Read failed because of other reasons not buffersize? */
263 if (This
->cbInBuffer
>= readBytes
)
265 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
266 This
->lpInFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpInFormat
, This
->cbInBuffer
);
267 if (This
->lpInFormat
== NULL
)
268 return NULL
; /* out of memory */
269 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
272 if (readSamples
!= 1) {
273 ERR(": no frames read\n");
276 if (readBytes
!= 0) {
277 This
->lpInFormat
->biSizeImage
= readBytes
;
279 /* nothing to decompress? */
280 if (This
->hic
== NULL
) {
281 This
->lCurrentFrame
= lPos
;
282 return This
->lpInFormat
;
286 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
287 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
288 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
291 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
292 This
->lpOutFormat
, This
->lpOutBuffer
);
295 } /* for (lNext < lPos) */
296 } /* if (This->lCurrentFrame != lPos) */
298 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
301 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
302 LONG lEnd
, LONG lRate
)
304 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
306 TRACE("(%p,%d,%d,%d)\n", iface
, lStart
, lEnd
, lRate
);
308 This
->bFixedStream
= TRUE
;
310 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
313 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
315 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
317 TRACE("(%p)\n", iface
);
319 This
->bFixedStream
= FALSE
;
324 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
325 LPBITMAPINFOHEADER lpbiWanted
,
326 LPVOID lpBits
, INT x
, INT y
,
329 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
331 AVISTREAMINFOW sInfo
;
332 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
333 BOOL bBestDisplay
= FALSE
;
335 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
338 if (This
->pStream
== NULL
)
341 if (lpbiWanted
== (LPBITMAPINFOHEADER
)AVIGETFRAMEF_BESTDISPLAYFMT
) {
346 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
347 if (sInfo
.fccType
!= streamtypeVIDEO
)
348 return AVIERR_UNSUPPORTED
;
350 This
->bFormatChanges
=
351 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
352 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
353 This
->dwEditCount
= sInfo
.dwEditCount
;
354 This
->lCurrentFrame
= -1;
356 /* get input format from stream */
357 if (This
->lpInFormat
== NULL
) {
360 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
361 if (This
->cbInBuffer
== 0)
362 This
->cbInBuffer
= 1024;
364 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
365 NULL
, &This
->cbInFormat
);
367 This
->lpInFormat
= HeapAlloc(GetProcessHeap(), 0, This
->cbInFormat
+ This
->cbInBuffer
);
368 if (This
->lpInFormat
== NULL
) {
369 AVIFILE_CloseCompressor(This
);
370 return AVIERR_MEMORY
;
373 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
375 AVIFILE_CloseCompressor(This
);
379 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
382 /* check input format */
383 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
384 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
385 if (This
->lpInFormat
->biSizeImage
== 0 &&
386 This
->lpInFormat
->biCompression
== BI_RGB
) {
387 This
->lpInFormat
->biSizeImage
=
388 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
391 /* only to pass through? */
392 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
394 (lpbi
->biCompression
== BI_RGB
&&
395 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
396 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
397 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
398 This
->lpOutFormat
= This
->lpInFormat
;
399 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
404 /* need memory for output format? */
405 if (This
->lpOutFormat
== NULL
) {
407 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
408 if (This
->lpOutFormat
== NULL
) {
409 AVIFILE_CloseCompressor(This
);
410 return AVIERR_MEMORY
;
414 /* need handle to video compressor */
415 if (This
->hic
== NULL
) {
418 if (This
->lpInFormat
->biCompression
== BI_RGB
)
419 fccHandler
= comptypeDIB
;
420 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
421 fccHandler
= mmioFOURCC('R','L','E',' ');
423 fccHandler
= sInfo
.fccHandler
;
426 if (lpbi
->biWidth
== 0)
427 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
428 if (lpbi
->biHeight
== 0)
429 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
432 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
433 if (This
->hic
== NULL
) {
434 AVIFILE_CloseCompressor(This
);
435 return AVIERR_NOCOMPRESSOR
;
439 /* output format given? */
441 /* check the given output format ... */
442 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
443 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
445 /* ... and remember it */
446 memcpy(This
->lpOutFormat
, lpbi
,
447 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
448 if (lpbi
->biBitCount
<= 8)
449 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
454 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
455 This
->lpOutFormat
, 0, dx
, dy
);
456 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
457 This
->lpOutFormat
) < 0) {
458 AVIFILE_CloseCompressor(This
);
459 return AVIERR_NOCOMPRESSOR
;
462 /* check output format */
463 if (This
->lpOutFormat
->biClrUsed
== 0 &&
464 This
->lpOutFormat
->biBitCount
<= 8)
465 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
466 if (This
->lpOutFormat
->biSizeImage
== 0 &&
467 This
->lpOutFormat
->biCompression
== BI_RGB
) {
468 This
->lpOutFormat
->biSizeImage
=
469 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
472 if (lpBits
== NULL
) {
473 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
475 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
476 This
->lpOutFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpOutFormat
, size
);
477 if (This
->lpOutFormat
== NULL
) {
478 AVIFILE_CloseCompressor(This
);
479 return AVIERR_MEMORY
;
481 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
483 This
->lpOutBuffer
= lpBits
;
485 /* for user size was irrelevant */
487 dx
= This
->lpOutFormat
->biWidth
;
489 dy
= This
->lpOutFormat
->biHeight
;
491 /* need to resize? */
492 if (x
!= 0 || y
!= 0) {
493 if (dy
== This
->lpOutFormat
->biHeight
&&
494 dx
== This
->lpOutFormat
->biWidth
)
495 This
->bResize
= FALSE
;
497 This
->bResize
= TRUE
;
506 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
507 0,This
->lpInFormat
->biWidth
,
508 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
509 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
511 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
512 This
->lpOutFormat
) == ICERR_OK
)
515 AVIFILE_CloseCompressor(This
);
517 return AVIERR_COMPRESSOR
;
521 /***********************************************************************/