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 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
41 REFIID refiid
, LPVOID
*obj
);
42 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
);
43 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
);
44 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
);
45 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
46 LONG lEnd
, LONG lRate
);
47 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
);
48 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
49 LPBITMAPINFOHEADER lpbi
,
50 LPVOID lpBits
, INT x
, INT y
,
53 static const struct IGetFrameVtbl igetframeVtbl
= {
54 IGetFrame_fnQueryInterface
,
63 typedef struct _IGetFrameImpl
{
65 const IGetFrameVtbl
*lpVtbl
;
74 LPBITMAPINFOHEADER lpInFormat
;
78 LPBITMAPINFOHEADER lpOutFormat
;
89 DWORD dwFormatChangeCount
;
93 /***********************************************************************/
95 static void AVIFILE_CloseCompressor(IGetFrameImpl
*This
)
97 if (This
->lpInFormat
!= This
->lpOutFormat
) {
98 HeapFree(GetProcessHeap(), 0, This
->lpOutFormat
);
99 This
->lpOutFormat
= NULL
;
101 HeapFree(GetProcessHeap(), 0, This
->lpInFormat
);
102 This
->lpInFormat
= NULL
;
103 if (This
->hic
!= NULL
) {
105 ICDecompressExEnd(This
->hic
);
107 ICDecompressEnd(This
->hic
);
113 PGETFRAME
AVIFILE_CreateGetFrame(PAVISTREAM pStream
)
117 /* check parameter */
121 pg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IGetFrameImpl
));
123 pg
->lpVtbl
= &igetframeVtbl
;
125 pg
->lCurrentFrame
= -1;
126 pg
->pStream
= pStream
;
127 IAVIStream_AddRef(pStream
);
130 return (PGETFRAME
)pg
;
133 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
134 REFIID refiid
, LPVOID
*obj
)
136 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
138 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
140 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
141 IsEqualGUID(&IID_IGetFrame
, refiid
)) {
143 IGetFrame_AddRef(iface
);
147 return OLE_E_ENUM_NOMORE
;
150 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
)
152 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
153 ULONG ref
= InterlockedIncrement(&This
->ref
);
155 TRACE("(%p)\n", iface
);
160 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
162 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
163 ULONG ref
= InterlockedDecrement(&This
->ref
);
165 TRACE("(%p)\n", iface
);
168 AVIFILE_CloseCompressor(This
);
169 if (This
->pStream
!= NULL
) {
170 IAVIStream_Release(This
->pStream
);
171 This
->pStream
= NULL
;
174 HeapFree(GetProcessHeap(), 0, iface
);
181 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
183 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
188 TRACE("(%p,%d)\n", iface
, lPos
);
190 /* We don't want negative start values! -- marks invalid buffer content */
195 if (This
->pStream
== NULL
)
197 if (This
->lpInFormat
== NULL
)
200 /* Could stream have changed? */
201 if (! This
->bFixedStream
) {
202 AVISTREAMINFOW sInfo
;
204 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
206 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
207 This
->dwEditCount
= sInfo
.dwEditCount
;
208 This
->lCurrentFrame
= -1;
211 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
212 /* stream has changed */
213 if (This
->lpOutFormat
!= NULL
) {
216 bi
= *This
->lpOutFormat
;
217 AVIFILE_CloseCompressor(This
);
219 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
220 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
223 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
228 if (lPos
!= This
->lCurrentFrame
) {
229 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
232 return NULL
; /* frame doesn't exist */
233 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
234 lNext
= This
->lCurrentFrame
+ 1;
236 for (; lNext
<= lPos
; lNext
++) {
237 /* new format for this frame? */
238 if (This
->bFormatChanges
) {
239 IAVIStream_ReadFormat(This
->pStream
, lNext
,
240 This
->lpInFormat
, &This
->cbInFormat
);
241 if (This
->lpOutFormat
!= NULL
) {
242 if (This
->lpOutFormat
->biBitCount
<= 8)
243 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
248 /* read input frame */
249 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
250 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
251 /* not enough memory for input buffer? */
253 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
254 return NULL
; /* bad thing, but bad things will happen */
255 if (readBytes
<= 0) {
256 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
260 /* IAVIStream::Read failed because of other reasons not buffersize? */
261 if (This
->cbInBuffer
>= readBytes
)
263 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
264 This
->lpInFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpInFormat
, This
->cbInBuffer
);
265 if (This
->lpInFormat
== NULL
)
266 return NULL
; /* out of memory */
267 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
270 if (readSamples
!= 1) {
271 ERR(": no frames read\n");
274 if (readBytes
!= 0) {
275 This
->lpInFormat
->biSizeImage
= readBytes
;
277 /* nothing to decompress? */
278 if (This
->hic
== NULL
) {
279 This
->lCurrentFrame
= lPos
;
280 return This
->lpInFormat
;
284 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
285 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
286 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
289 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
290 This
->lpOutFormat
, This
->lpOutBuffer
);
293 } /* for (lNext < lPos) */
294 } /* if (This->lCurrentFrame != lPos) */
296 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
299 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
300 LONG lEnd
, LONG lRate
)
302 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
304 TRACE("(%p,%d,%d,%d)\n", iface
, lStart
, lEnd
, lRate
);
306 This
->bFixedStream
= TRUE
;
308 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
311 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
313 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
315 TRACE("(%p)\n", iface
);
317 This
->bFixedStream
= FALSE
;
322 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
323 LPBITMAPINFOHEADER lpbiWanted
,
324 LPVOID lpBits
, INT x
, INT y
,
327 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
329 AVISTREAMINFOW sInfo
;
330 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
331 BOOL bBestDisplay
= FALSE
;
333 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
336 if (This
->pStream
== NULL
)
339 if (lpbiWanted
== (LPBITMAPINFOHEADER
)AVIGETFRAMEF_BESTDISPLAYFMT
) {
344 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
345 if (sInfo
.fccType
!= streamtypeVIDEO
)
346 return AVIERR_UNSUPPORTED
;
348 This
->bFormatChanges
=
349 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
350 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
351 This
->dwEditCount
= sInfo
.dwEditCount
;
352 This
->lCurrentFrame
= -1;
354 /* get input format from stream */
355 if (This
->lpInFormat
== NULL
) {
358 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
359 if (This
->cbInBuffer
== 0)
360 This
->cbInBuffer
= 1024;
362 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
363 NULL
, &This
->cbInFormat
);
365 This
->lpInFormat
= HeapAlloc(GetProcessHeap(), 0, This
->cbInFormat
+ This
->cbInBuffer
);
366 if (This
->lpInFormat
== NULL
) {
367 AVIFILE_CloseCompressor(This
);
368 return AVIERR_MEMORY
;
371 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
373 AVIFILE_CloseCompressor(This
);
377 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
380 /* check input format */
381 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
382 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
383 if (This
->lpInFormat
->biSizeImage
== 0 &&
384 This
->lpInFormat
->biCompression
== BI_RGB
) {
385 This
->lpInFormat
->biSizeImage
=
386 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
389 /* only to pass through? */
390 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
392 (lpbi
->biCompression
== BI_RGB
&&
393 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
394 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
395 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
396 This
->lpOutFormat
= This
->lpInFormat
;
397 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
402 /* need memory for output format? */
403 if (This
->lpOutFormat
== NULL
) {
405 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
406 if (This
->lpOutFormat
== NULL
) {
407 AVIFILE_CloseCompressor(This
);
408 return AVIERR_MEMORY
;
412 /* need handle to video compressor */
413 if (This
->hic
== NULL
) {
416 if (This
->lpInFormat
->biCompression
== BI_RGB
)
417 fccHandler
= comptypeDIB
;
418 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
419 fccHandler
= mmioFOURCC('R','L','E',' ');
421 fccHandler
= sInfo
.fccHandler
;
424 if (lpbi
->biWidth
== 0)
425 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
426 if (lpbi
->biHeight
== 0)
427 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
430 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
431 if (This
->hic
== NULL
) {
432 AVIFILE_CloseCompressor(This
);
433 return AVIERR_NOCOMPRESSOR
;
437 /* output format given? */
439 /* check the given output format ... */
440 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
441 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
443 /* ... and remember it */
444 memcpy(This
->lpOutFormat
, lpbi
,
445 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
446 if (lpbi
->biBitCount
<= 8)
447 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
452 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
453 This
->lpOutFormat
, 0, dx
, dy
);
454 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
455 This
->lpOutFormat
) < 0) {
456 AVIFILE_CloseCompressor(This
);
457 return AVIERR_NOCOMPRESSOR
;
460 /* check output format */
461 if (This
->lpOutFormat
->biClrUsed
== 0 &&
462 This
->lpOutFormat
->biBitCount
<= 8)
463 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
464 if (This
->lpOutFormat
->biSizeImage
== 0 &&
465 This
->lpOutFormat
->biCompression
== BI_RGB
) {
466 This
->lpOutFormat
->biSizeImage
=
467 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
470 if (lpBits
== NULL
) {
471 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
473 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
474 This
->lpOutFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpOutFormat
, size
);
475 if (This
->lpOutFormat
== NULL
) {
476 AVIFILE_CloseCompressor(This
);
477 return AVIERR_MEMORY
;
479 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
481 This
->lpOutBuffer
= lpBits
;
483 /* for user size was irrelevant */
485 dx
= This
->lpOutFormat
->biWidth
;
487 dy
= This
->lpOutFormat
->biHeight
;
489 /* need to resize? */
490 if (x
!= 0 || y
!= 0) {
491 if (dy
== This
->lpOutFormat
->biHeight
&&
492 dx
== This
->lpOutFormat
->biWidth
)
493 This
->bResize
= FALSE
;
495 This
->bResize
= TRUE
;
504 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
505 0,This
->lpInFormat
->biWidth
,
506 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
507 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
509 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
510 This
->lpOutFormat
) == ICERR_OK
)
513 AVIFILE_CloseCompressor(This
);
515 return AVIERR_COMPRESSOR
;
519 /***********************************************************************/