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 WIN32_NO_STATUS
21 #define COM_NO_WINDOWS_H
28 //#include "winuser.h"
31 #include "avifile_private.h"
33 #include <wine/debug.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
38 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
39 (lp)->biClrUsed * sizeof(RGBQUAD))
42 /***********************************************************************/
44 typedef struct _IGetFrameImpl
{
46 IGetFrame IGetFrame_iface
;
55 LPBITMAPINFOHEADER lpInFormat
;
59 LPBITMAPINFOHEADER lpOutFormat
;
70 DWORD dwFormatChangeCount
;
74 /***********************************************************************/
76 static inline IGetFrameImpl
*impl_from_IGetFrame(IGetFrame
*iface
)
78 return CONTAINING_RECORD(iface
, IGetFrameImpl
, IGetFrame_iface
);
81 static void AVIFILE_CloseCompressor(IGetFrameImpl
*This
)
83 if (This
->lpInFormat
!= This
->lpOutFormat
) {
84 HeapFree(GetProcessHeap(), 0, This
->lpOutFormat
);
85 This
->lpOutFormat
= NULL
;
87 HeapFree(GetProcessHeap(), 0, This
->lpInFormat
);
88 This
->lpInFormat
= NULL
;
89 if (This
->hic
!= NULL
) {
91 ICDecompressExEnd(This
->hic
);
93 ICDecompressEnd(This
->hic
);
99 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
100 REFIID refiid
, LPVOID
*obj
)
102 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
104 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
106 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
107 IsEqualGUID(&IID_IGetFrame
, refiid
)) {
109 IGetFrame_AddRef(iface
);
113 return OLE_E_ENUM_NOMORE
;
116 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
)
118 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
119 ULONG ref
= InterlockedIncrement(&This
->ref
);
121 TRACE("(%p)\n", iface
);
126 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
128 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
129 ULONG ref
= InterlockedDecrement(&This
->ref
);
131 TRACE("(%p)\n", iface
);
134 AVIFILE_CloseCompressor(This
);
135 if (This
->pStream
!= NULL
) {
136 IAVIStream_Release(This
->pStream
);
137 This
->pStream
= NULL
;
140 HeapFree(GetProcessHeap(), 0, iface
);
147 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
149 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
154 TRACE("(%p,%d)\n", iface
, lPos
);
156 /* We don't want negative start values! -- marks invalid buffer content */
161 if (This
->pStream
== NULL
)
163 if (This
->lpInFormat
== NULL
)
166 /* Could stream have changed? */
167 if (! This
->bFixedStream
) {
168 AVISTREAMINFOW sInfo
;
170 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
172 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
173 This
->dwEditCount
= sInfo
.dwEditCount
;
174 This
->lCurrentFrame
= -1;
177 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
178 /* stream has changed */
179 if (This
->lpOutFormat
!= NULL
) {
182 bi
= *This
->lpOutFormat
;
183 AVIFILE_CloseCompressor(This
);
185 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
186 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
189 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
194 if (lPos
!= This
->lCurrentFrame
) {
195 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
198 return NULL
; /* frame doesn't exist */
199 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
200 lNext
= This
->lCurrentFrame
+ 1;
202 for (; lNext
<= lPos
; lNext
++) {
203 /* new format for this frame? */
204 if (This
->bFormatChanges
) {
205 IAVIStream_ReadFormat(This
->pStream
, lNext
,
206 This
->lpInFormat
, &This
->cbInFormat
);
207 if (This
->lpOutFormat
!= NULL
) {
208 if (This
->lpOutFormat
->biBitCount
<= 8)
209 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
214 /* read input frame */
215 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
216 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
217 /* not enough memory for input buffer? */
219 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
220 return NULL
; /* bad thing, but bad things will happen */
221 if (readBytes
<= 0) {
222 ERR(": IAVIStream::Read doesn't return needed bytes!\n");
226 /* IAVIStream::Read failed because of other reasons not buffersize? */
227 if (This
->cbInBuffer
>= readBytes
)
229 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
230 This
->lpInFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpInFormat
, This
->cbInBuffer
);
231 if (This
->lpInFormat
== NULL
)
232 return NULL
; /* out of memory */
233 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
236 if (readSamples
!= 1) {
237 ERR(": no frames read\n");
240 if (readBytes
!= 0) {
241 This
->lpInFormat
->biSizeImage
= readBytes
;
243 /* nothing to decompress? */
244 if (This
->hic
== NULL
) {
245 This
->lCurrentFrame
= lPos
;
246 return This
->lpInFormat
;
250 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
251 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
252 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
255 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
256 This
->lpOutFormat
, This
->lpOutBuffer
);
259 } /* for (lNext < lPos) */
260 } /* if (This->lCurrentFrame != lPos) */
262 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
265 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
266 LONG lEnd
, LONG lRate
)
268 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
270 TRACE("(%p,%d,%d,%d)\n", iface
, lStart
, lEnd
, lRate
);
272 This
->bFixedStream
= TRUE
;
274 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
277 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
279 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
281 TRACE("(%p)\n", iface
);
283 This
->bFixedStream
= FALSE
;
288 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
289 LPBITMAPINFOHEADER lpbiWanted
,
290 LPVOID lpBits
, INT x
, INT y
,
293 IGetFrameImpl
*This
= impl_from_IGetFrame(iface
);
295 AVISTREAMINFOW sInfo
;
296 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
297 BOOL bBestDisplay
= FALSE
;
299 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
302 if (This
->pStream
== NULL
)
305 if (lpbiWanted
== (LPBITMAPINFOHEADER
)AVIGETFRAMEF_BESTDISPLAYFMT
) {
310 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
311 if (sInfo
.fccType
!= streamtypeVIDEO
)
312 return AVIERR_UNSUPPORTED
;
314 This
->bFormatChanges
=
315 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
316 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
317 This
->dwEditCount
= sInfo
.dwEditCount
;
318 This
->lCurrentFrame
= -1;
320 /* get input format from stream */
321 if (This
->lpInFormat
== NULL
) {
324 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
325 if (This
->cbInBuffer
== 0)
326 This
->cbInBuffer
= 1024;
328 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
329 NULL
, &This
->cbInFormat
);
331 This
->lpInFormat
= HeapAlloc(GetProcessHeap(), 0, This
->cbInFormat
+ This
->cbInBuffer
);
332 if (This
->lpInFormat
== NULL
) {
333 AVIFILE_CloseCompressor(This
);
334 return AVIERR_MEMORY
;
337 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
339 AVIFILE_CloseCompressor(This
);
343 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
346 /* check input format */
347 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
348 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
349 if (This
->lpInFormat
->biSizeImage
== 0 &&
350 This
->lpInFormat
->biCompression
== BI_RGB
) {
351 This
->lpInFormat
->biSizeImage
=
352 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
355 /* only to pass through? */
356 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
358 (lpbi
->biCompression
== BI_RGB
&&
359 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
360 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
361 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
362 This
->lpOutFormat
= This
->lpInFormat
;
363 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
368 /* need memory for output format? */
369 if (This
->lpOutFormat
== NULL
) {
371 HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
372 if (This
->lpOutFormat
== NULL
) {
373 AVIFILE_CloseCompressor(This
);
374 return AVIERR_MEMORY
;
378 /* need handle to video compressor */
379 if (This
->hic
== NULL
) {
382 if (This
->lpInFormat
->biCompression
== BI_RGB
)
383 fccHandler
= comptypeDIB
;
384 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
385 fccHandler
= mmioFOURCC('R','L','E',' ');
387 fccHandler
= sInfo
.fccHandler
;
390 if (lpbi
->biWidth
== 0)
391 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
392 if (lpbi
->biHeight
== 0)
393 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
396 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
397 if (This
->hic
== NULL
) {
398 AVIFILE_CloseCompressor(This
);
399 return AVIERR_NOCOMPRESSOR
;
403 /* output format given? */
405 /* check the given output format ... */
406 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
407 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
409 /* ... and remember it */
410 memcpy(This
->lpOutFormat
, lpbi
,
411 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
412 if (lpbi
->biBitCount
<= 8)
413 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
418 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
419 This
->lpOutFormat
, 0, dx
, dy
);
420 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
421 This
->lpOutFormat
) < 0) {
422 AVIFILE_CloseCompressor(This
);
423 return AVIERR_NOCOMPRESSOR
;
426 /* check output format */
427 if (This
->lpOutFormat
->biClrUsed
== 0 &&
428 This
->lpOutFormat
->biBitCount
<= 8)
429 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
430 if (This
->lpOutFormat
->biSizeImage
== 0 &&
431 This
->lpOutFormat
->biCompression
== BI_RGB
) {
432 This
->lpOutFormat
->biSizeImage
=
433 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
436 if (lpBits
== NULL
) {
437 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
439 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
440 This
->lpOutFormat
= HeapReAlloc(GetProcessHeap(), 0, This
->lpOutFormat
, size
);
441 if (This
->lpOutFormat
== NULL
) {
442 AVIFILE_CloseCompressor(This
);
443 return AVIERR_MEMORY
;
445 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
447 This
->lpOutBuffer
= lpBits
;
449 /* for user size was irrelevant */
451 dx
= This
->lpOutFormat
->biWidth
;
453 dy
= This
->lpOutFormat
->biHeight
;
455 /* need to resize? */
456 if (x
!= 0 || y
!= 0) {
457 if (dy
== This
->lpOutFormat
->biHeight
&&
458 dx
== This
->lpOutFormat
->biWidth
)
459 This
->bResize
= FALSE
;
461 This
->bResize
= TRUE
;
470 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
471 0,This
->lpInFormat
->biWidth
,
472 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
473 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
475 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
476 This
->lpOutFormat
) == ICERR_OK
)
479 AVIFILE_CloseCompressor(This
);
481 return AVIERR_COMPRESSOR
;
485 static const struct IGetFrameVtbl igetframeVtbl
= {
486 IGetFrame_fnQueryInterface
,
489 IGetFrame_fnGetFrame
,
492 IGetFrame_fnSetFormat
495 PGETFRAME
AVIFILE_CreateGetFrame(PAVISTREAM pStream
)
499 /* check parameter */
503 pg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IGetFrameImpl
));
505 pg
->IGetFrame_iface
.lpVtbl
= &igetframeVtbl
;
507 pg
->lCurrentFrame
= -1;
508 pg
->pStream
= pStream
;
509 IAVIStream_AddRef(pStream
);
512 return (PGETFRAME
)pg
;
515 /***********************************************************************/