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 #include "avifile_private.h"
22 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
23 (lp)->biClrUsed * sizeof(RGBQUAD))
26 /***********************************************************************/
28 typedef struct _IGetFrameImpl
{
30 IGetFrame IGetFrame_iface
;
39 LPBITMAPINFOHEADER lpInFormat
;
43 LPBITMAPINFOHEADER lpOutFormat
;
54 DWORD dwFormatChangeCount
;
58 /***********************************************************************/
60 static inline IGetFrameImpl
*impl_from_IGetFrame(IGetFrame
*iface
)
62 return CONTAINING_RECORD(iface
, IGetFrameImpl
, IGetFrame_iface
);
65 static void AVIFILE_CloseCompressor(IGetFrameImpl
*This
)
67 if (This
->lpInFormat
!= This
->lpOutFormat
) {
68 HeapFree(GetProcessHeap(), 0, This
->lpOutFormat
);
69 This
->lpOutFormat
= NULL
;
71 HeapFree(GetProcessHeap(), 0, This
->lpInFormat
);
72 This
->lpInFormat
= NULL
;
73 if (This
->hic
!= NULL
) {
75 ICDecompressExEnd(This
->hic
);
77 ICDecompressEnd(This
->hic
);
83 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
84 REFIID refiid
, LPVOID
*obj
)
86 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
88 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
90 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
91 IsEqualGUID(&IID_IGetFrame
, refiid
)) {
93 IGetFrame_AddRef(iface
);
97 return OLE_E_ENUM_NOMORE
;
100 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
)
102 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
103 ULONG ref
= InterlockedIncrement(&This
->ref
);
105 TRACE("(%p)\n", iface
);
110 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
112 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
113 ULONG ref
= InterlockedDecrement(&This
->ref
);
115 TRACE("(%p)\n", iface
);
118 AVIFILE_CloseCompressor(This
);
119 if (This
->pStream
!= NULL
) {
120 IAVIStream_Release(This
->pStream
);
121 This
->pStream
= NULL
;
124 HeapFree(GetProcessHeap(), 0, iface
);
131 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
133 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
138 TRACE("(%p,%d)\n", iface
, lPos
);
140 /* We don't want negative start values! -- marks invalid buffer content */
145 if (This
->pStream
== NULL
)
147 if (This
->lpInFormat
== NULL
)
150 /* Could stream have changed? */
151 if (! This
->bFixedStream
) {
152 AVISTREAMINFOW sInfo
;
154 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
156 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
157 This
->dwEditCount
= sInfo
.dwEditCount
;
158 This
->lCurrentFrame
= -1;
161 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
162 /* stream has changed */
163 if (This
->lpOutFormat
!= NULL
) {
166 bi
= *This
->lpOutFormat
;
167 AVIFILE_CloseCompressor(This
);
169 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
170 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
173 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
178 if (lPos
!= This
->lCurrentFrame
) {
179 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
182 return NULL
; /* frame doesn't exist */
183 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
184 lNext
= This
->lCurrentFrame
+ 1;
186 for (; lNext
<= lPos
; lNext
++) {
187 /* new format for this frame? */
188 if (This
->bFormatChanges
) {
189 IAVIStream_ReadFormat(This
->pStream
, lNext
,
190 This
->lpInFormat
, &This
->cbInFormat
);
191 if (This
->lpOutFormat
!= NULL
) {
192 if (This
->lpOutFormat
->biBitCount
<= 8)
193 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
198 /* read input frame */
199 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
200 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
201 /* not enough memory for input buffer? */
203 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
204 return NULL
; /* bad thing, but bad things will happen */
205 if (readBytes
<= 0) {
206 ERR(": IAVIStream::Read doesn't return needed bytes!\n");
210 /* IAVIStream::Read failed because of other reasons not buffersize? */
211 if (This
->cbInBuffer
>= readBytes
)
213 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
214 This
->lpInFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpInFormat
, This
->cbInBuffer
);
215 if (This
->lpInFormat
== NULL
)
216 return NULL
; /* out of memory */
217 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
220 if (readSamples
!= 1) {
221 ERR(": no frames read\n");
224 if (readBytes
!= 0) {
225 This
->lpInFormat
->biSizeImage
= readBytes
;
227 /* nothing to decompress? */
228 if (This
->hic
== NULL
) {
229 This
->lCurrentFrame
= lPos
;
230 return This
->lpInFormat
;
234 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
235 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
236 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
239 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
240 This
->lpOutFormat
, This
->lpOutBuffer
);
243 } /* for (lNext < lPos) */
244 } /* if (This->lCurrentFrame != lPos) */
246 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
249 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
250 LONG lEnd
, LONG lRate
)
252 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
254 TRACE("(%p,%d,%d,%d)\n", iface
, lStart
, lEnd
, lRate
);
256 This
->bFixedStream
= TRUE
;
258 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
261 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
263 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
265 TRACE("(%p)\n", iface
);
267 This
->bFixedStream
= FALSE
;
272 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
273 LPBITMAPINFOHEADER lpbiWanted
,
274 LPVOID lpBits
, INT x
, INT y
,
277 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
279 AVISTREAMINFOW sInfo
;
280 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
281 BOOL bBestDisplay
= FALSE
;
283 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
286 if (This
->pStream
== NULL
)
289 if (lpbiWanted
== (LPBITMAPINFOHEADER
)AVIGETFRAMEF_BESTDISPLAYFMT
) {
294 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
295 if (sInfo
.fccType
!= streamtypeVIDEO
)
296 return AVIERR_UNSUPPORTED
;
298 This
->bFormatChanges
= (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
) != 0;
299 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
300 This
->dwEditCount
= sInfo
.dwEditCount
;
301 This
->lCurrentFrame
= -1;
303 /* get input format from stream */
304 if (This
->lpInFormat
== NULL
) {
307 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
308 if (This
->cbInBuffer
== 0)
309 This
->cbInBuffer
= 1024;
311 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
312 NULL
, &This
->cbInFormat
);
314 This
->lpInFormat
= HeapAlloc(GetProcessHeap(), 0, This
->cbInFormat
+ This
->cbInBuffer
);
315 if (This
->lpInFormat
== NULL
) {
316 AVIFILE_CloseCompressor(This
);
317 return AVIERR_MEMORY
;
320 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
322 AVIFILE_CloseCompressor(This
);
326 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
329 /* check input format */
330 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
331 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
332 if (This
->lpInFormat
->biSizeImage
== 0 &&
333 This
->lpInFormat
->biCompression
== BI_RGB
) {
334 This
->lpInFormat
->biSizeImage
=
335 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
338 /* only to pass through? */
339 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
341 (lpbi
->biCompression
== BI_RGB
&&
342 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
343 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
344 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
345 This
->lpOutFormat
= This
->lpInFormat
;
346 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
351 /* need memory for output format? */
352 if (This
->lpOutFormat
== NULL
) {
354 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
355 if (This
->lpOutFormat
== NULL
) {
356 AVIFILE_CloseCompressor(This
);
357 return AVIERR_MEMORY
;
361 /* need handle to video compressor */
362 if (This
->hic
== NULL
) {
365 if (This
->lpInFormat
->biCompression
== BI_RGB
)
366 fccHandler
= comptypeDIB
;
367 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
368 fccHandler
= mmioFOURCC('R','L','E',' ');
370 fccHandler
= sInfo
.fccHandler
;
373 if (lpbi
->biWidth
== 0)
374 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
375 if (lpbi
->biHeight
== 0)
376 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
379 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
380 if (This
->hic
== NULL
) {
381 AVIFILE_CloseCompressor(This
);
382 return AVIERR_NOCOMPRESSOR
;
386 /* output format given? */
388 /* check the given output format ... */
389 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
390 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
392 /* ... and remember it */
393 memcpy(This
->lpOutFormat
, lpbi
,
394 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
395 if (lpbi
->biBitCount
<= 8)
396 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
399 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
400 This
->lpOutFormat
, 0, dx
, dy
);
401 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
402 This
->lpOutFormat
) < 0) {
403 AVIFILE_CloseCompressor(This
);
404 return AVIERR_NOCOMPRESSOR
;
408 /* check output format */
409 if (This
->lpOutFormat
->biClrUsed
== 0 &&
410 This
->lpOutFormat
->biBitCount
<= 8)
411 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
412 if (This
->lpOutFormat
->biSizeImage
== 0 &&
413 This
->lpOutFormat
->biCompression
== BI_RGB
) {
414 This
->lpOutFormat
->biSizeImage
=
415 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
418 if (lpBits
== NULL
) {
419 DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
421 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
422 This
->lpOutFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpOutFormat
, size
);
423 if (This
->lpOutFormat
== NULL
) {
424 AVIFILE_CloseCompressor(This
);
425 return AVIERR_MEMORY
;
427 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
429 This
->lpOutBuffer
= lpBits
;
431 /* for user size was irrelevant */
433 dx
= This
->lpOutFormat
->biWidth
;
435 dy
= This
->lpOutFormat
->biHeight
;
437 /* need to resize? */
438 if (x
!= 0 || y
!= 0) {
439 if (dy
== This
->lpOutFormat
->biHeight
&&
440 dx
== This
->lpOutFormat
->biWidth
)
441 This
->bResize
= FALSE
;
443 This
->bResize
= TRUE
;
452 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
453 0,This
->lpInFormat
->biWidth
,
454 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
455 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
457 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
458 This
->lpOutFormat
) == ICERR_OK
)
461 AVIFILE_CloseCompressor(This
);
463 return AVIERR_COMPRESSOR
;
466 static const struct IGetFrameVtbl igetframeVtbl
= {
467 IGetFrame_fnQueryInterface
,
470 IGetFrame_fnGetFrame
,
473 IGetFrame_fnSetFormat
476 PGETFRAME
AVIFILE_CreateGetFrame(PAVISTREAM pStream
)
480 /* check parameter */
484 pg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IGetFrameImpl
));
486 pg
->IGetFrame_iface
.lpVtbl
= &igetframeVtbl
;
488 pg
->lCurrentFrame
= -1;
489 pg
->pStream
= pStream
;
490 IAVIStream_AddRef(pStream
);
493 return (PGETFRAME
)pg
;
496 /***********************************************************************/