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
)) {
146 return OLE_E_ENUM_NOMORE
;
149 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
)
151 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
152 ULONG ref
= InterlockedIncrement(&This
->ref
);
154 TRACE("(%p)\n", iface
);
159 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
161 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
162 ULONG ref
= InterlockedDecrement(&This
->ref
);
164 TRACE("(%p)\n", iface
);
167 AVIFILE_CloseCompressor(This
);
168 if (This
->pStream
!= NULL
) {
169 IAVIStream_Release(This
->pStream
);
170 This
->pStream
= NULL
;
173 HeapFree(GetProcessHeap(), 0, iface
);
180 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
182 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
187 TRACE("(%p,%d)\n", iface
, lPos
);
189 /* We don't want negative start values! -- marks invalid buffer content */
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 bi
= *This
->lpOutFormat
;
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
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
231 return NULL
; /* frame doesn't exist */
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 IAVIStream_ReadFormat(This
->pStream
, lNext
,
239 This
->lpInFormat
, &This
->cbInFormat
);
240 if (This
->lpOutFormat
!= NULL
) {
241 if (This
->lpOutFormat
->biBitCount
<= 8)
242 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
247 /* read input frame */
248 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
249 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
250 /* not enough memory for input buffer? */
252 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
253 return NULL
; /* bad thing, but bad things will happen */
254 if (readBytes
<= 0) {
255 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
259 /* IAVIStream::Read failed because of other reasons not buffersize? */
260 if (This
->cbInBuffer
>= readBytes
)
262 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
263 This
->lpInFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpInFormat
, This
->cbInBuffer
);
264 if (This
->lpInFormat
== NULL
)
265 return NULL
; /* out of memory */
266 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
269 if (readSamples
!= 1) {
270 ERR(": no frames read\n");
273 if (readBytes
!= 0) {
274 This
->lpInFormat
->biSizeImage
= readBytes
;
276 /* nothing to decompress? */
277 if (This
->hic
== NULL
) {
278 This
->lCurrentFrame
= lPos
;
279 return This
->lpInFormat
;
283 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
284 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
285 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
288 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
289 This
->lpOutFormat
, This
->lpOutBuffer
);
292 } /* for (lNext < lPos) */
293 } /* if (This->lCurrentFrame != lPos) */
295 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
298 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
299 LONG lEnd
, LONG lRate
)
301 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
303 TRACE("(%p,%d,%d,%d)\n", iface
, lStart
, lEnd
, lRate
);
305 This
->bFixedStream
= TRUE
;
307 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
310 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
312 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
314 TRACE("(%p)\n", iface
);
316 This
->bFixedStream
= FALSE
;
321 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
322 LPBITMAPINFOHEADER lpbiWanted
,
323 LPVOID lpBits
, INT x
, INT y
,
326 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
328 AVISTREAMINFOW sInfo
;
329 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
330 BOOL bBestDisplay
= FALSE
;
332 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
335 if (This
->pStream
== NULL
)
338 if (lpbiWanted
== (LPBITMAPINFOHEADER
)AVIGETFRAMEF_BESTDISPLAYFMT
) {
343 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
344 if (sInfo
.fccType
!= streamtypeVIDEO
)
345 return AVIERR_UNSUPPORTED
;
347 This
->bFormatChanges
=
348 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
349 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
350 This
->dwEditCount
= sInfo
.dwEditCount
;
351 This
->lCurrentFrame
= -1;
353 /* get input format from stream */
354 if (This
->lpInFormat
== NULL
) {
357 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
358 if (This
->cbInBuffer
== 0)
359 This
->cbInBuffer
= 1024;
361 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
362 NULL
, &This
->cbInFormat
);
364 This
->lpInFormat
= HeapAlloc(GetProcessHeap(), 0, This
->cbInFormat
+ This
->cbInBuffer
);
365 if (This
->lpInFormat
== NULL
) {
366 AVIFILE_CloseCompressor(This
);
367 return AVIERR_MEMORY
;
370 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
372 AVIFILE_CloseCompressor(This
);
376 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
379 /* check input format */
380 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
381 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
382 if (This
->lpInFormat
->biSizeImage
== 0 &&
383 This
->lpInFormat
->biCompression
== BI_RGB
) {
384 This
->lpInFormat
->biSizeImage
=
385 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
388 /* only to pass through? */
389 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
391 (lpbi
->biCompression
== BI_RGB
&&
392 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
393 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
394 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
395 This
->lpOutFormat
= This
->lpInFormat
;
396 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
401 /* need memory for output format? */
402 if (This
->lpOutFormat
== NULL
) {
404 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
405 if (This
->lpOutFormat
== NULL
) {
406 AVIFILE_CloseCompressor(This
);
407 return AVIERR_MEMORY
;
411 /* need handle to video compressor */
412 if (This
->hic
== NULL
) {
415 if (This
->lpInFormat
->biCompression
== BI_RGB
)
416 fccHandler
= comptypeDIB
;
417 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
418 fccHandler
= mmioFOURCC('R','L','E',' ');
420 fccHandler
= sInfo
.fccHandler
;
423 if (lpbi
->biWidth
== 0)
424 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
425 if (lpbi
->biHeight
== 0)
426 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
429 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
430 if (This
->hic
== NULL
) {
431 AVIFILE_CloseCompressor(This
);
432 return AVIERR_NOCOMPRESSOR
;
436 /* output format given? */
438 /* check the given output format ... */
439 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
440 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
442 /* ... and remember it */
443 memcpy(This
->lpOutFormat
, lpbi
,
444 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
445 if (lpbi
->biBitCount
<= 8)
446 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
451 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
452 This
->lpOutFormat
, 0, dx
, dy
);
453 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
454 This
->lpOutFormat
) < 0) {
455 AVIFILE_CloseCompressor(This
);
456 return AVIERR_NOCOMPRESSOR
;
459 /* check output format */
460 if (This
->lpOutFormat
->biClrUsed
== 0 &&
461 This
->lpOutFormat
->biBitCount
<= 8)
462 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
463 if (This
->lpOutFormat
->biSizeImage
== 0 &&
464 This
->lpOutFormat
->biCompression
== BI_RGB
) {
465 This
->lpOutFormat
->biSizeImage
=
466 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
469 if (lpBits
== NULL
) {
470 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
472 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
473 This
->lpOutFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpOutFormat
, size
);
474 if (This
->lpOutFormat
== NULL
) {
475 AVIFILE_CloseCompressor(This
);
476 return AVIERR_MEMORY
;
478 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
480 This
->lpOutBuffer
= lpBits
;
482 /* for user size was irrelevant */
484 dx
= This
->lpOutFormat
->biWidth
;
486 dy
= This
->lpOutFormat
->biHeight
;
488 /* need to resize? */
489 if (x
!= 0 || y
!= 0) {
490 if (dy
== This
->lpOutFormat
->biHeight
&&
491 dx
== This
->lpOutFormat
->biWidth
)
492 This
->bResize
= FALSE
;
494 This
->bResize
= TRUE
;
503 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
504 0,This
->lpInFormat
->biWidth
,
505 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
506 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
508 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
509 This
->lpOutFormat
) == ICERR_OK
)
512 AVIFILE_CloseCompressor(This
);
514 return AVIERR_COMPRESSOR
;
518 /***********************************************************************/