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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define COM_NO_WINDOWS_H
33 #include "avifile_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
40 #define DIBPTR(lp) ((LPBYTE)(lp) + (lp)->biSize + \
41 (lp)->biClrUsed * sizeof(RGBQUAD))
44 /***********************************************************************/
46 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
47 REFIID refiid
, LPVOID
*obj
);
48 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
);
49 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
);
50 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
);
51 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
52 LONG lEnd
, LONG lRate
);
53 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
);
54 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
55 LPBITMAPINFOHEADER lpbi
,
56 LPVOID lpBits
, INT x
, INT y
,
59 static const struct IGetFrameVtbl igetframeVtbl
= {
60 IGetFrame_fnQueryInterface
,
69 typedef struct _IGetFrameImpl
{
71 const IGetFrameVtbl
*lpVtbl
;
80 LPBITMAPINFOHEADER lpInFormat
;
84 LPBITMAPINFOHEADER lpOutFormat
;
95 DWORD dwFormatChangeCount
;
99 /***********************************************************************/
101 static void AVIFILE_CloseCompressor(IGetFrameImpl
*This
)
103 if (This
->lpOutFormat
!= NULL
&& This
->lpInFormat
!= This
->lpOutFormat
) {
104 GlobalFreePtr(This
->lpOutFormat
);
105 This
->lpOutFormat
= NULL
;
107 if (This
->lpInFormat
!= NULL
) {
108 GlobalFreePtr(This
->lpInFormat
);
109 This
->lpInFormat
= NULL
;
111 if (This
->hic
!= NULL
) {
113 ICDecompressExEnd(This
->hic
);
115 ICDecompressEnd(This
->hic
);
121 PGETFRAME
AVIFILE_CreateGetFrame(PAVISTREAM pStream
)
125 /* check parameter */
129 pg
= (IGetFrameImpl
*)LocalAlloc(LPTR
, sizeof(IGetFrameImpl
));
131 pg
->lpVtbl
= &igetframeVtbl
;
133 pg
->lCurrentFrame
= -1;
134 pg
->pStream
= pStream
;
135 IAVIStream_AddRef(pStream
);
138 return (PGETFRAME
)pg
;
141 static HRESULT WINAPI
IGetFrame_fnQueryInterface(IGetFrame
*iface
,
142 REFIID refiid
, LPVOID
*obj
)
144 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
146 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
148 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
149 IsEqualGUID(&IID_IGetFrame
, refiid
)) {
154 return OLE_E_ENUM_NOMORE
;
157 static ULONG WINAPI
IGetFrame_fnAddRef(IGetFrame
*iface
)
159 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
160 ULONG ref
= InterlockedIncrement(&This
->ref
);
162 TRACE("(%p)\n", iface
);
167 static ULONG WINAPI
IGetFrame_fnRelease(IGetFrame
*iface
)
169 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
170 ULONG ref
= InterlockedDecrement(&This
->ref
);
172 TRACE("(%p)\n", iface
);
175 AVIFILE_CloseCompressor(This
);
176 if (This
->pStream
!= NULL
) {
177 IAVIStream_Release(This
->pStream
);
178 This
->pStream
= NULL
;
181 LocalFree((HLOCAL
)iface
);
188 static LPVOID WINAPI
IGetFrame_fnGetFrame(IGetFrame
*iface
, LONG lPos
)
190 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
195 TRACE("(%p,%ld)\n", iface
, lPos
);
197 /* We don't want negative start values! -- marks invalid buffer content */
202 if (This
->pStream
== NULL
)
204 if (This
->lpInFormat
== NULL
)
207 /* Could stream have changed? */
208 if (! This
->bFixedStream
) {
209 AVISTREAMINFOW sInfo
;
211 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
213 if (sInfo
.dwEditCount
!= This
->dwEditCount
) {
214 This
->dwEditCount
= sInfo
.dwEditCount
;
215 This
->lCurrentFrame
= -1;
218 if (sInfo
.dwFormatChangeCount
!= This
->dwFormatChangeCount
) {
219 /* stream has changed */
220 if (This
->lpOutFormat
!= NULL
) {
223 memcpy(&bi
, This
->lpOutFormat
, sizeof(bi
));
224 AVIFILE_CloseCompressor(This
);
226 if (FAILED(IGetFrame_SetFormat(iface
, &bi
, NULL
, 0, 0, -1, -1))) {
227 if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
230 } else if (FAILED(IGetFrame_SetFormat(iface
, NULL
, NULL
, 0, 0, -1, -1)))
235 if (lPos
!= This
->lCurrentFrame
) {
236 LONG lNext
= IAVIStream_FindSample(This
->pStream
,lPos
,FIND_KEY
|FIND_PREV
);
239 return NULL
; /* frame doesn't exist */
240 if (lNext
<= This
->lCurrentFrame
&& This
->lCurrentFrame
< lPos
)
241 lNext
= This
->lCurrentFrame
+ 1;
243 for (; lNext
<= lPos
; lNext
++) {
244 /* new format for this frame? */
245 if (This
->bFormatChanges
) {
246 IAVIStream_ReadFormat(This
->pStream
, lNext
,
247 This
->lpInFormat
, &This
->cbInFormat
);
248 if (This
->lpOutFormat
!= NULL
) {
249 if (This
->lpOutFormat
->biBitCount
<= 8)
250 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
,
255 /* read input frame */
256 while (FAILED(AVIStreamRead(This
->pStream
, lNext
, 1, This
->lpInBuffer
,
257 This
->cbInBuffer
, &readBytes
, &readSamples
))) {
258 /* not enough memory for input buffer? */
260 if (FAILED(AVIStreamSampleSize(This
->pStream
, lNext
, &readBytes
)))
261 return NULL
; /* bad thing, but bad things will happen */
262 if (readBytes
<= 0) {
263 ERR(": IAVIStream::REad doesn't return needed bytes!\n");
267 /* IAVIStream::Read failed because of other reasons not buffersize? */
268 if (This
->cbInBuffer
>= readBytes
)
270 This
->cbInBuffer
= This
->cbInFormat
+ readBytes
;
271 This
->lpInFormat
= GlobalReAllocPtr(This
->lpInFormat
, This
->cbInBuffer
, 0);
272 if (This
->lpInFormat
== NULL
)
273 return NULL
; /* out of memory */
274 This
->lpInBuffer
= (BYTE
*)This
->lpInFormat
+ This
->cbInFormat
;
277 if (readSamples
!= 1) {
278 ERR(": no frames read\n");
281 if (readBytes
!= 0) {
282 This
->lpInFormat
->biSizeImage
= readBytes
;
284 /* nothing to decompress? */
285 if (This
->hic
== NULL
) {
286 This
->lCurrentFrame
= lPos
;
287 return This
->lpInFormat
;
291 ICDecompressEx(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,0,
292 This
->lpInFormat
->biWidth
,This
->lpInFormat
->biHeight
,
293 This
->lpOutFormat
,This
->lpOutBuffer
,This
->x
,This
->y
,
296 ICDecompress(This
->hic
, 0, This
->lpInFormat
, This
->lpInBuffer
,
297 This
->lpOutFormat
, This
->lpOutBuffer
);
300 } /* for (lNext < lPos) */
301 } /* if (This->lCurrentFrame != lPos) */
303 return (This
->hic
== NULL
? This
->lpInFormat
: This
->lpOutFormat
);
306 static HRESULT WINAPI
IGetFrame_fnBegin(IGetFrame
*iface
, LONG lStart
,
307 LONG lEnd
, LONG lRate
)
309 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
311 TRACE("(%p,%ld,%ld,%ld)\n", iface
, lStart
, lEnd
, lRate
);
313 This
->bFixedStream
= TRUE
;
315 return (IGetFrame_GetFrame(iface
, lStart
) ? AVIERR_OK
: AVIERR_ERROR
);
318 static HRESULT WINAPI
IGetFrame_fnEnd(IGetFrame
*iface
)
320 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
322 TRACE("(%p)\n", iface
);
324 This
->bFixedStream
= FALSE
;
329 static HRESULT WINAPI
IGetFrame_fnSetFormat(IGetFrame
*iface
,
330 LPBITMAPINFOHEADER lpbiWanted
,
331 LPVOID lpBits
, INT x
, INT y
,
334 IGetFrameImpl
*This
= (IGetFrameImpl
*)iface
;
336 AVISTREAMINFOW sInfo
;
337 LPBITMAPINFOHEADER lpbi
= lpbiWanted
;
338 BOOL bBestDisplay
= FALSE
;
340 TRACE("(%p,%p,%p,%d,%d,%d,%d)\n", iface
, lpbiWanted
, lpBits
,
343 if (This
->pStream
== NULL
)
346 if (lpbiWanted
== (LPBITMAPINFOHEADER
)AVIGETFRAMEF_BESTDISPLAYFMT
) {
351 IAVIStream_Info(This
->pStream
, &sInfo
, sizeof(sInfo
));
352 if (sInfo
.fccType
!= streamtypeVIDEO
)
353 return AVIERR_UNSUPPORTED
;
355 This
->bFormatChanges
=
356 (sInfo
.dwFlags
& AVISTREAMINFO_FORMATCHANGES
? TRUE
: FALSE
);
357 This
->dwFormatChangeCount
= sInfo
.dwFormatChangeCount
;
358 This
->dwEditCount
= sInfo
.dwEditCount
;
359 This
->lCurrentFrame
= -1;
361 /* get input format from stream */
362 if (This
->lpInFormat
== NULL
) {
365 This
->cbInBuffer
= (LONG
)sInfo
.dwSuggestedBufferSize
;
366 if (This
->cbInBuffer
== 0)
367 This
->cbInBuffer
= 1024;
369 IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
,
370 NULL
, &This
->cbInFormat
);
373 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, This
->cbInFormat
+ This
->cbInBuffer
);
374 if (This
->lpInFormat
== NULL
) {
375 AVIFILE_CloseCompressor(This
);
376 return AVIERR_MEMORY
;
379 hr
= IAVIStream_ReadFormat(This
->pStream
, sInfo
.dwStart
, This
->lpInFormat
, &This
->cbInFormat
);
381 AVIFILE_CloseCompressor(This
);
385 This
->lpInBuffer
= ((LPBYTE
)This
->lpInFormat
) + This
->cbInFormat
;
388 /* check input format */
389 if (This
->lpInFormat
->biClrUsed
== 0 && This
->lpInFormat
->biBitCount
<= 8)
390 This
->lpInFormat
->biClrUsed
= 1u << This
->lpInFormat
->biBitCount
;
391 if (This
->lpInFormat
->biSizeImage
== 0 &&
392 This
->lpInFormat
->biCompression
== BI_RGB
) {
393 This
->lpInFormat
->biSizeImage
=
394 DIBWIDTHBYTES(*This
->lpInFormat
) * This
->lpInFormat
->biHeight
;
397 /* only to pass through? */
398 if (This
->lpInFormat
->biCompression
== BI_RGB
&& lpBits
== NULL
) {
400 (lpbi
->biCompression
== BI_RGB
&&
401 lpbi
->biWidth
== This
->lpInFormat
->biWidth
&&
402 lpbi
->biHeight
== This
->lpInFormat
->biHeight
&&
403 lpbi
->biBitCount
== This
->lpInFormat
->biBitCount
)) {
404 This
->lpOutFormat
= This
->lpInFormat
;
405 This
->lpOutBuffer
= DIBPTR(This
->lpInFormat
);
410 /* need memory for output format? */
411 if (This
->lpOutFormat
== NULL
) {
413 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, sizeof(BITMAPINFOHEADER
)
414 + 256 * sizeof(RGBQUAD
));
415 if (This
->lpOutFormat
== NULL
) {
416 AVIFILE_CloseCompressor(This
);
417 return AVIERR_MEMORY
;
421 /* need handle to video compressor */
422 if (This
->hic
== NULL
) {
425 if (This
->lpInFormat
->biCompression
== BI_RGB
)
426 fccHandler
= comptypeDIB
;
427 else if (This
->lpInFormat
->biCompression
== BI_RLE8
)
428 fccHandler
= mmioFOURCC('R','L','E',' ');
430 fccHandler
= sInfo
.fccHandler
;
433 if (lpbi
->biWidth
== 0)
434 lpbi
->biWidth
= This
->lpInFormat
->biWidth
;
435 if (lpbi
->biHeight
== 0)
436 lpbi
->biHeight
= This
->lpInFormat
->biHeight
;
439 This
->hic
= ICLocate(ICTYPE_VIDEO
, fccHandler
, This
->lpInFormat
, lpbi
, ICMODE_DECOMPRESS
);
440 if (This
->hic
== NULL
) {
441 AVIFILE_CloseCompressor(This
);
442 return AVIERR_NOCOMPRESSOR
;
446 /* output format given? */
448 /* check the given output format ... */
449 if (lpbi
->biClrUsed
== 0 && lpbi
->biBitCount
<= 8)
450 lpbi
->biClrUsed
= 1u << lpbi
->biBitCount
;
452 /* ... and remember it */
453 memcpy(This
->lpOutFormat
, lpbi
,
454 lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
));
455 if (lpbi
->biBitCount
<= 8)
456 ICDecompressGetPalette(This
->hic
, This
->lpInFormat
, This
->lpOutFormat
);
461 ICGetDisplayFormat(This
->hic
, This
->lpInFormat
,
462 This
->lpOutFormat
, 0, dx
, dy
);
463 } else if (ICDecompressGetFormat(This
->hic
, This
->lpInFormat
,
464 This
->lpOutFormat
) < 0) {
465 AVIFILE_CloseCompressor(This
);
466 return AVIERR_NOCOMPRESSOR
;
469 /* check output format */
470 if (This
->lpOutFormat
->biClrUsed
== 0 &&
471 This
->lpOutFormat
->biBitCount
<= 8)
472 This
->lpOutFormat
->biClrUsed
= 1u << This
->lpOutFormat
->biBitCount
;
473 if (This
->lpOutFormat
->biSizeImage
== 0 &&
474 This
->lpOutFormat
->biCompression
== BI_RGB
) {
475 This
->lpOutFormat
->biSizeImage
=
476 DIBWIDTHBYTES(*This
->lpOutFormat
) * This
->lpOutFormat
->biHeight
;
479 if (lpBits
== NULL
) {
480 register DWORD size
= This
->lpOutFormat
->biClrUsed
* sizeof(RGBQUAD
);
482 size
+= This
->lpOutFormat
->biSize
+ This
->lpOutFormat
->biSizeImage
;
484 (LPBITMAPINFOHEADER
)GlobalReAllocPtr(This
->lpOutFormat
, size
, GMEM_MOVEABLE
);
485 if (This
->lpOutFormat
== NULL
) {
486 AVIFILE_CloseCompressor(This
);
487 return AVIERR_MEMORY
;
489 This
->lpOutBuffer
= DIBPTR(This
->lpOutFormat
);
491 This
->lpOutBuffer
= lpBits
;
493 /* for user size was irrelevant */
495 dx
= This
->lpOutFormat
->biWidth
;
497 dy
= This
->lpOutFormat
->biHeight
;
499 /* need to resize? */
500 if (x
!= 0 || y
!= 0) {
501 if (dy
== This
->lpOutFormat
->biHeight
&&
502 dx
== This
->lpOutFormat
->biWidth
)
503 This
->bResize
= FALSE
;
505 This
->bResize
= TRUE
;
514 if (ICDecompressExBegin(This
->hic
,0,This
->lpInFormat
,This
->lpInBuffer
,0,
515 0,This
->lpInFormat
->biWidth
,
516 This
->lpInFormat
->biHeight
,This
->lpOutFormat
,
517 This
->lpOutBuffer
, x
, y
, dx
, dy
) == ICERR_OK
)
519 } else if (ICDecompressBegin(This
->hic
, This
->lpInFormat
,
520 This
->lpOutFormat
) == ICERR_OK
)
523 AVIFILE_CloseCompressor(This
);
525 return AVIERR_COMPRESSOR
;
529 /***********************************************************************/