2 * Copyright 2002 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
34 #include "avifile_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
40 #define MAX_FRAMESIZE (16 * 1024 * 1024)
41 #define MAX_FRAMESIZE_DIFF 512
43 /***********************************************************************/
45 static HRESULT WINAPI
ICMStream_fnQueryInterface(IAVIStream
*iface
,REFIID refiid
,LPVOID
*obj
);
46 static ULONG WINAPI
ICMStream_fnAddRef(IAVIStream
*iface
);
47 static ULONG WINAPI
ICMStream_fnRelease(IAVIStream
* iface
);
48 static HRESULT WINAPI
ICMStream_fnCreate(IAVIStream
*iface
,LPARAM lParam1
,LPARAM lParam2
);
49 static HRESULT WINAPI
ICMStream_fnInfo(IAVIStream
*iface
,AVISTREAMINFOW
*psi
,LONG size
);
50 static LONG WINAPI
ICMStream_fnFindSample(IAVIStream
*iface
,LONG pos
,LONG flags
);
51 static HRESULT WINAPI
ICMStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG
*formatsize
);
52 static HRESULT WINAPI
ICMStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG formatsize
);
53 static HRESULT WINAPI
ICMStream_fnRead(IAVIStream
*iface
,LONG start
,LONG samples
,LPVOID buffer
,LONG buffersize
,LONG
*bytesread
,LONG
*samplesread
);
54 static HRESULT WINAPI
ICMStream_fnWrite(IAVIStream
*iface
,LONG start
,LONG samples
,LPVOID buffer
,LONG buffersize
,DWORD flags
,LONG
*sampwritten
,LONG
*byteswritten
);
55 static HRESULT WINAPI
ICMStream_fnDelete(IAVIStream
*iface
,LONG start
,LONG samples
);
56 static HRESULT WINAPI
ICMStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,LPVOID lp
,LONG
*lpread
);
57 static HRESULT WINAPI
ICMStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,LPVOID lp
,LONG size
);
58 static HRESULT WINAPI
ICMStream_fnSetInfo(IAVIStream
*iface
,AVISTREAMINFOW
*info
,LONG infolen
);
60 struct IAVIStreamVtbl iicmst
= {
61 ICMStream_fnQueryInterface
,
66 ICMStream_fnFindSample
,
67 ICMStream_fnReadFormat
,
68 ICMStream_fnSetFormat
,
73 ICMStream_fnWriteData
,
77 typedef struct _IAVIStreamImpl
{
79 IAVIStreamVtbl
*lpVtbl
;
82 /* IAVIStream stuff */
94 DWORD dwBytesPerFrame
;
97 LPBITMAPINFOHEADER lpbiCur
; /* current frame */
99 LPBITMAPINFOHEADER lpbiPrev
; /* previous frame */
102 LPBITMAPINFOHEADER lpbiOutput
; /* output format of codec */
104 LPBITMAPINFOHEADER lpbiInput
; /* input format for codec */
108 /***********************************************************************/
110 static HRESULT
AVIFILE_EncodeFrame(IAVIStreamImpl
*This
,
111 LPBITMAPINFOHEADER lpbi
, LPVOID lpBits
);
112 static HRESULT
AVIFILE_OpenGetFrame(IAVIStreamImpl
*This
);
114 inline void AVIFILE_Reset(IAVIStreamImpl
*This
)
118 This
->dwLastQuality
= ICQUALITY_HIGH
;
119 This
->dwUnusedBytes
= 0;
122 HRESULT
AVIFILE_CreateICMStream(REFIID riid
, LPVOID
*ppv
)
124 IAVIStreamImpl
*pstream
;
127 assert(riid
!= NULL
&& ppv
!= NULL
);
131 pstream
= (IAVIStreamImpl
*)LocalAlloc(LPTR
, sizeof(IAVIStreamImpl
));
133 return AVIERR_MEMORY
;
135 pstream
->lpVtbl
= &iicmst
;
136 AVIFILE_Reset(pstream
);
138 hr
= IUnknown_QueryInterface((IUnknown
*)pstream
, riid
, ppv
);
140 LocalFree((HLOCAL
)pstream
);
145 static HRESULT WINAPI
ICMStream_fnQueryInterface(IAVIStream
*iface
,
146 REFIID refiid
, LPVOID
*obj
)
148 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
150 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(refiid
), obj
);
152 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
153 IsEqualGUID(&IID_IAVIStream
, refiid
)) {
155 IAVIStream_AddRef(iface
);
160 return OLE_E_ENUM_NOMORE
;
163 static ULONG WINAPI
ICMStream_fnAddRef(IAVIStream
*iface
)
165 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
167 TRACE("(%p) -> %ld\n", iface
, This
->ref
+ 1);
169 /* also add reference to the nested stream */
170 if (This
->pStream
!= NULL
)
171 IAVIStream_AddRef(This
->pStream
);
173 return ++(This
->ref
);
176 static ULONG WINAPI
ICMStream_fnRelease(IAVIStream
* iface
)
178 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
180 TRACE("(%p) -> %ld\n", iface
, This
->ref
- 1);
182 if (This
->ref
== 0) {
184 if (This
->pg
!= NULL
) {
185 AVIStreamGetFrameClose(This
->pg
);
188 if (This
->pStream
!= NULL
) {
189 IAVIStream_Release(This
->pStream
);
190 This
->pStream
= NULL
;
192 if (This
->hic
!= NULL
) {
193 if (This
->lpbiPrev
!= NULL
) {
194 ICDecompressEnd(This
->hic
);
195 GlobalFreePtr(This
->lpbiPrev
);
196 This
->lpbiPrev
= NULL
;
199 ICCompressEnd(This
->hic
);
202 if (This
->lpbiCur
!= NULL
) {
203 GlobalFreePtr(This
->lpbiCur
);
204 This
->lpbiCur
= NULL
;
207 if (This
->lpbiOutput
!= NULL
) {
208 GlobalFreePtr(This
->lpbiOutput
);
209 This
->lpbiOutput
= NULL
;
212 if (This
->lpbiInput
!= NULL
) {
213 GlobalFreePtr(This
->lpbiInput
);
214 This
->lpbiInput
= NULL
;
218 LocalFree((HLOCAL
)This
);
223 /* also release reference to the nested stream */
224 if (This
->pStream
!= NULL
)
225 IAVIStream_Release(This
->pStream
);
230 /* lParam1: PAVISTREAM
231 * lParam2: LPAVICOMPRESSOPTIONS
233 static HRESULT WINAPI
ICMStream_fnCreate(IAVIStream
*iface
, LPARAM lParam1
,
236 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
239 ICCOMPRESSFRAMES icFrames
;
240 LPAVICOMPRESSOPTIONS pco
= (LPAVICOMPRESSOPTIONS
)lParam2
;
242 TRACE("(%p,0x%08lX,0x%08lX)\n", iface
, lParam1
, lParam2
);
244 /* check parameter */
245 if ((LPVOID
)lParam1
== NULL
)
246 return AVIERR_BADPARAM
;
248 /* get infos from stream */
249 IAVIStream_Info((PAVISTREAM
)lParam1
, &This
->sInfo
, sizeof(This
->sInfo
));
250 if (This
->sInfo
.fccType
!= streamtypeVIDEO
)
251 return AVIERR_ERROR
; /* error in registry or AVIMakeCompressedStream */
253 /* add reference to the stream */
254 This
->pStream
= (PAVISTREAM
)lParam1
;
255 IAVIStream_AddRef(This
->pStream
);
259 if (pco
!= NULL
&& pco
->fccHandler
!= comptypeDIB
) {
260 /* we should compress */
261 This
->sInfo
.fccHandler
= pco
->fccHandler
;
263 This
->hic
= ICOpen(ICTYPE_VIDEO
, pco
->fccHandler
, ICMODE_COMPRESS
);
264 if (This
->hic
== NULL
)
265 return AVIERR_NOCOMPRESSOR
;
267 /* restore saved state of codec */
268 if (pco
->cbParms
> 0 && pco
->lpParms
!= NULL
) {
269 ICSetState(This
->hic
, pco
->lpParms
, pco
->cbParms
);
272 /* set quality -- resolve default quality */
273 This
->sInfo
.dwQuality
= pco
->dwQuality
;
274 if (pco
->dwQuality
== ICQUALITY_DEFAULT
)
275 This
->sInfo
.dwQuality
= ICGetDefaultQuality(This
->hic
);
277 /* get capabilities of codec */
278 ICGetInfo(This
->hic
, &icinfo
, sizeof(icinfo
));
279 This
->dwICMFlags
= icinfo
.dwFlags
;
282 if ((pco
->dwFlags
& AVICOMPRESSF_KEYFRAMES
) &&
283 (icinfo
.dwFlags
& (VIDCF_TEMPORAL
|VIDCF_FASTTEMPORALC
))) {
284 This
->lKeyFrameEvery
= pco
->dwKeyFrameEvery
;
286 This
->lKeyFrameEvery
= 1;
289 if ((pco
->dwFlags
& AVICOMPRESSF_DATARATE
)) {
290 /* Do we have a chance to reduce size to desired one? */
291 if ((icinfo
.dwFlags
& (VIDCF_CRUNCH
|VIDCF_QUALITY
)) == 0)
292 return AVIERR_NOCOMPRESSOR
;
294 assert(This
->sInfo
.dwRate
!= 0);
296 This
->dwBytesPerFrame
= MulDiv(pco
->dwBytesPerSecond
,
297 This
->sInfo
.dwScale
, This
->sInfo
.dwRate
);
299 pco
->dwBytesPerSecond
= 0;
300 This
->dwBytesPerFrame
= 0;
303 if (icinfo
.dwFlags
& VIDCF_COMPRESSFRAMES
) {
304 memset(&icFrames
, 0, sizeof(icFrames
));
305 icFrames
.lpbiOutput
= This
->lpbiOutput
;
306 icFrames
.lpbiInput
= This
->lpbiInput
;
307 icFrames
.lFrameCount
= This
->sInfo
.dwLength
;
308 icFrames
.lQuality
= This
->sInfo
.dwQuality
;
309 icFrames
.lDataRate
= pco
->dwBytesPerSecond
;
310 icFrames
.lKeyRate
= This
->lKeyFrameEvery
;
311 icFrames
.dwRate
= This
->sInfo
.dwRate
;
312 icFrames
.dwScale
= This
->sInfo
.dwScale
;
313 ICSendMessage(This
->hic
, ICM_COMPRESS_FRAMES_INFO
,
314 (LPARAM
)&icFrames
, (LPARAM
)sizeof(icFrames
));
317 This
->sInfo
.fccHandler
= comptypeDIB
;
322 static HRESULT WINAPI
ICMStream_fnInfo(IAVIStream
*iface
,LPAVISTREAMINFOW psi
,
325 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
327 TRACE("(%p,%p,%ld)\n", iface
, psi
, size
);
330 return AVIERR_BADPARAM
;
332 return AVIERR_BADSIZE
;
334 memcpy(psi
, &This
->sInfo
, min((DWORD
)size
, sizeof(This
->sInfo
)));
336 if ((DWORD
)size
< sizeof(This
->sInfo
))
337 return AVIERR_BUFFERTOOSMALL
;
341 static LONG WINAPI
ICMStream_fnFindSample(IAVIStream
*iface
, LONG pos
,
344 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
346 TRACE("(%p,%ld,0x%08lX)\n",iface
,pos
,flags
);
348 if (flags
& FIND_FROM_START
) {
349 pos
= This
->sInfo
.dwStart
;
350 flags
&= ~(FIND_FROM_START
|FIND_PREV
);
354 if (flags
& FIND_RET
)
355 WARN(": FIND_RET flags will be ignored!\n");
357 if (flags
& FIND_KEY
) {
358 if (This
->hic
== NULL
)
359 return pos
; /* we decompress so every frame is a keyframe */
361 if (flags
& FIND_PREV
) {
362 /* need to read old or new frames? */
363 if (This
->lLastKey
<= pos
|| pos
< This
->lCurrent
)
364 IAVIStream_Read(iface
, pos
, 1, NULL
, 0, NULL
, NULL
);
366 return This
->lLastKey
;
368 } else if (flags
& FIND_ANY
) {
369 return pos
; /* We really don't know, reread is to expensive, so guess. */
370 } else if (flags
& FIND_FORMAT
) {
371 if (flags
& FIND_PREV
)
378 static HRESULT WINAPI
ICMStream_fnReadFormat(IAVIStream
*iface
, LONG pos
,
379 LPVOID format
, LONG
*formatsize
)
381 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
383 LPBITMAPINFOHEADER lpbi
;
386 TRACE("(%p,%ld,%p,%p)\n", iface
, pos
, format
, formatsize
);
388 if (formatsize
== NULL
)
389 return AVIERR_BADPARAM
;
391 if (This
->pg
== NULL
) {
392 hr
= AVIFILE_OpenGetFrame(This
);
398 lpbi
= (LPBITMAPINFOHEADER
)AVIStreamGetFrame(This
->pg
, pos
);
400 return AVIERR_MEMORY
;
402 if (This
->hic
== NULL
) {
403 LONG size
= lpbi
->biSize
+ lpbi
->biClrUsed
* sizeof(RGBQUAD
);
406 if (This
->sInfo
.dwSuggestedBufferSize
< lpbi
->biSizeImage
)
407 This
->sInfo
.dwSuggestedBufferSize
= lpbi
->biSizeImage
;
409 This
->cbOutput
= size
;
410 if (format
!= NULL
) {
411 if (This
->lpbiOutput
!= NULL
)
412 memcpy(format
, This
->lpbiOutput
, min(*formatsize
, This
->cbOutput
));
414 memcpy(format
, lpbi
, min(*formatsize
, size
));
417 } else if (format
!= NULL
)
418 memcpy(format
, This
->lpbiOutput
, min(*formatsize
, This
->cbOutput
));
420 if (*formatsize
< This
->cbOutput
)
421 hr
= AVIERR_BUFFERTOOSMALL
;
425 *formatsize
= This
->cbOutput
;
429 static HRESULT WINAPI
ICMStream_fnSetFormat(IAVIStream
*iface
, LONG pos
,
430 LPVOID format
, LONG formatsize
)
432 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
434 TRACE("(%p,%ld,%p,%ld)\n", iface
, pos
, format
, formatsize
);
436 /* check parameters */
437 if (format
== NULL
|| formatsize
<= 0)
438 return AVIERR_BADPARAM
;
440 /* We can only accept RGB data for writing */
441 if (((LPBITMAPINFOHEADER
)format
)->biCompression
!= BI_RGB
) {
442 WARN(": need RGB data as input\n");
443 return AVIERR_UNSUPPORTED
;
446 /* Input format already known?
447 * Changing of palette is supported, but be quiet if it's the same */
448 if (This
->lpbiInput
!= NULL
) {
449 if (This
->cbInput
!= formatsize
)
450 return AVIERR_UNSUPPORTED
;
452 if (memcmp(format
, This
->lpbiInput
, formatsize
) == 0)
456 /* Does the nested stream support writing? */
457 if ((This
->sInfo
.dwCaps
& AVIFILECAPS_CANWRITE
) == 0)
458 return AVIERR_READONLY
;
460 /* check if frame is already written */
461 if (This
->sInfo
.dwLength
+ This
->sInfo
.dwStart
> pos
)
462 return AVIERR_UNSUPPORTED
;
464 /* check if we should compress */
465 if (This
->sInfo
.fccHandler
== 0 ||
466 This
->sInfo
.fccHandler
== mmioFOURCC('N','O','N','E'))
467 This
->sInfo
.fccHandler
= comptypeDIB
;
469 /* only pass through? */
470 if (This
->sInfo
.fccHandler
== comptypeDIB
)
471 return IAVIStream_SetFormat(This
->pStream
, pos
, format
, formatsize
);
473 /* initial format setting? */
474 if (This
->lpbiInput
== NULL
) {
477 assert(This
->hic
!= NULL
);
479 /* get memory for input format */
480 This
->lpbiInput
= (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, formatsize
);
481 if (This
->lpbiInput
== NULL
)
482 return AVIERR_MEMORY
;
483 This
->cbInput
= formatsize
;
484 memcpy(This
->lpbiInput
, format
, formatsize
);
486 /* get output format */
487 size
= ICCompressGetFormatSize(This
->hic
, This
->lpbiInput
);
488 if (size
< sizeof(BITMAPINFOHEADER
))
489 return AVIERR_COMPRESSOR
;
490 This
->lpbiOutput
= (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, size
);
491 if (This
->lpbiOutput
== NULL
)
492 return AVIERR_MEMORY
;
493 This
->cbOutput
= size
;
494 if (ICCompressGetFormat(This
->hic
,This
->lpbiInput
,This
->lpbiOutput
) < S_OK
)
495 return AVIERR_COMPRESSOR
;
497 /* update AVISTREAMINFO structure */
498 This
->sInfo
.rcFrame
.right
=
499 This
->sInfo
.rcFrame
.left
+ This
->lpbiOutput
->biWidth
;
500 This
->sInfo
.rcFrame
.bottom
=
501 This
->sInfo
.rcFrame
.top
+ This
->lpbiOutput
->biHeight
;
503 /* prepare codec for compression */
504 if (ICCompressBegin(This
->hic
, This
->lpbiInput
, This
->lpbiOutput
) != S_OK
)
505 return AVIERR_COMPRESSOR
;
507 /* allocate memory for compressed frame */
508 size
= ICCompressGetSize(This
->hic
, This
->lpbiInput
, This
->lpbiOutput
);
510 (LPBITMAPINFOHEADER
)GlobalAllocPtr(GMEM_MOVEABLE
, This
->cbOutput
+ size
);
511 if (This
->lpbiCur
== NULL
)
512 return AVIERR_MEMORY
;
513 memcpy(This
->lpbiCur
, This
->lpbiOutput
, This
->cbOutput
);
514 This
->lpCur
= DIBPTR(This
->lpbiCur
);
516 /* allocate memory for last frame if needed */
517 if (This
->lKeyFrameEvery
!= 1 &&
518 (This
->dwICMFlags
& VIDCF_FASTTEMPORALC
) == 0) {
519 size
= ICDecompressGetFormatSize(This
->hic
, This
->lpbiOutput
);
520 This
->lpbiPrev
= (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, size
);
521 if (This
->lpbiPrev
== NULL
)
522 return AVIERR_MEMORY
;
523 if (ICDecompressGetFormat(This
->hic
, This
->lpbiOutput
, This
->lpbiPrev
) < S_OK
)
524 return AVIERR_COMPRESSOR
;
526 if (This
->lpbiPrev
->biSizeImage
== 0) {
527 This
->lpbiPrev
->biSizeImage
=
528 DIBWIDTHBYTES(*This
->lpbiPrev
) * This
->lpbiPrev
->biHeight
;
531 /* get memory for format and picture */
532 size
+= This
->lpbiPrev
->biSizeImage
;
534 (LPBITMAPINFOHEADER
)GlobalReAllocPtr(This
->lpbiPrev
,size
,GMEM_MOVEABLE
);
535 if (This
->lpbiPrev
== NULL
)
536 return AVIERR_MEMORY
;
537 This
->lpPrev
= DIBPTR(This
->lpbiPrev
);
539 /* prepare codec also for decompression */
540 if (ICDecompressBegin(This
->hic
,This
->lpbiOutput
,This
->lpbiPrev
) != S_OK
)
541 return AVIERR_COMPRESSOR
;
544 /* format change -- check that's only the palette */
545 LPBITMAPINFOHEADER lpbi
= (LPBITMAPINFOHEADER
)format
;
547 if (lpbi
->biSize
!= This
->lpbiInput
->biSize
||
548 lpbi
->biWidth
!= This
->lpbiInput
->biWidth
||
549 lpbi
->biHeight
!= This
->lpbiInput
->biHeight
||
550 lpbi
->biBitCount
!= This
->lpbiInput
->biBitCount
||
551 lpbi
->biPlanes
!= This
->lpbiInput
->biPlanes
||
552 lpbi
->biCompression
!= This
->lpbiInput
->biCompression
||
553 lpbi
->biClrUsed
!= This
->lpbiInput
->biClrUsed
)
554 return AVIERR_UNSUPPORTED
;
556 /* get new output format */
557 if (ICCompressGetFormat(This
->hic
, lpbi
, This
->lpbiOutput
) < S_OK
)
558 return AVIERR_BADFORMAT
;
560 /* restart compression */
561 ICCompressEnd(This
->hic
);
562 if (ICCompressBegin(This
->hic
, lpbi
, This
->lpbiOutput
) != S_OK
)
563 return AVIERR_COMPRESSOR
;
565 /* check if we need to restart decompresion also */
566 if (This
->lKeyFrameEvery
!= 1 &&
567 (This
->dwICMFlags
& VIDCF_FASTTEMPORALC
) == 0) {
568 ICDecompressEnd(This
->hic
);
569 if (ICDecompressGetFormat(This
->hic
,This
->lpbiOutput
,This
->lpbiPrev
) < S_OK
)
570 return AVIERR_COMPRESSOR
;
571 if (ICDecompressBegin(This
->hic
,This
->lpbiOutput
,This
->lpbiPrev
) != S_OK
)
572 return AVIERR_COMPRESSOR
;
576 /* tell nested stream the new format */
577 return IAVIStream_SetFormat(This
->pStream
, pos
,
578 This
->lpbiOutput
, This
->cbOutput
);
581 static HRESULT WINAPI
ICMStream_fnRead(IAVIStream
*iface
, LONG start
,
582 LONG samples
, LPVOID buffer
,
583 LONG buffersize
, LPLONG bytesread
,
586 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
588 LPBITMAPINFOHEADER lpbi
;
590 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p)\n", iface
, start
, samples
, buffer
,
591 buffersize
, bytesread
, samplesread
);
593 /* clear return parameters if given */
594 if (bytesread
!= NULL
)
596 if (samplesread
!= NULL
)
602 /* check parameters */
603 if (samples
!= 1 && (bytesread
== NULL
&& samplesread
== NULL
))
604 return AVIERR_BADPARAM
;
605 if (samples
== -1) /* read as much as we could */
608 if (This
->pg
== NULL
) {
609 HRESULT hr
= AVIFILE_OpenGetFrame(This
);
615 /* compress or decompress? */
616 if (This
->hic
== NULL
) {
618 lpbi
= (LPBITMAPINFOHEADER
)AVIStreamGetFrame(This
->pg
, start
);
620 return AVIERR_MEMORY
;
622 if (buffer
!= NULL
&& buffersize
> 0) {
623 /* check buffersize */
624 if (buffersize
< lpbi
->biSizeImage
)
625 return AVIERR_BUFFERTOOSMALL
;
627 memcpy(buffer
, DIBPTR(lpbi
), lpbi
->biSizeImage
);
630 /* fill out return parameters if given */
631 if (bytesread
!= NULL
)
632 *bytesread
= lpbi
->biSizeImage
;
635 if (This
->lCurrent
> start
)
638 while (start
> This
->lCurrent
) {
641 lpbi
= (LPBITMAPINFOHEADER
)AVIStreamGetFrame(This
->pg
, ++This
->lCurrent
);
644 return AVIERR_MEMORY
;
647 hr
= AVIFILE_EncodeFrame(This
, lpbi
, DIBPTR(lpbi
));
654 if (buffer
!= NULL
&& buffersize
> 0) {
655 /* check buffersize */
656 if (This
->lpbiCur
->biSizeImage
> buffersize
)
657 return AVIERR_BUFFERTOOSMALL
;
659 memcpy(buffer
, This
->lpCur
, This
->lpbiCur
->biSizeImage
);
662 /* fill out return parameters if given */
663 if (bytesread
!= NULL
)
664 *bytesread
= This
->lpbiCur
->biSizeImage
;
667 /* fill out return parameters if given */
668 if (samplesread
!= NULL
)
674 static HRESULT WINAPI
ICMStream_fnWrite(IAVIStream
*iface
, LONG start
,
675 LONG samples
, LPVOID buffer
,
676 LONG buffersize
, DWORD flags
,
680 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
684 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n", iface
, start
, samples
,
685 buffer
, buffersize
, flags
, sampwritten
, byteswritten
);
687 /* clear return parameters if given */
688 if (sampwritten
!= NULL
)
690 if (byteswritten
!= NULL
)
693 /* check parameters */
694 if (buffer
== NULL
&& (buffersize
> 0 || samples
> 0))
695 return AVIERR_BADPARAM
;
697 if (This
->sInfo
.fccHandler
== comptypeDIB
) {
698 /* only pass through */
699 flags
|= AVIIF_KEYFRAME
;
701 return IAVIStream_Write(This
->pStream
, start
, samples
, buffer
, buffersize
,
702 flags
, sampwritten
, byteswritten
);
704 /* compress data before writing to pStream */
705 if (samples
!= 1 && (sampwritten
== NULL
&& byteswritten
== NULL
))
706 return AVIERR_UNSUPPORTED
;
708 This
->lCurrent
= start
;
709 hr
= AVIFILE_EncodeFrame(This
, This
->lpbiInput
, buffer
);
713 if (This
->lLastKey
== start
)
714 flags
|= AVIIF_KEYFRAME
;
716 return IAVIStream_Write(This
->pStream
, start
, samples
, This
->lpCur
,
717 This
->lpbiCur
->biSizeImage
, flags
, byteswritten
,
722 static HRESULT WINAPI
ICMStream_fnDelete(IAVIStream
*iface
, LONG start
,
725 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
727 TRACE("(%p,%ld,%ld)\n", iface
, start
, samples
);
729 return IAVIStream_Delete(This
->pStream
, start
, samples
);
732 static HRESULT WINAPI
ICMStream_fnReadData(IAVIStream
*iface
, DWORD fcc
,
733 LPVOID lp
, LPLONG lpread
)
735 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
737 TRACE("(%p,0x%08lX,%p,%p)\n", iface
, fcc
, lp
, lpread
);
739 assert(This
->pStream
!= NULL
);
741 return IAVIStream_ReadData(This
->pStream
, fcc
, lp
, lpread
);
744 static HRESULT WINAPI
ICMStream_fnWriteData(IAVIStream
*iface
, DWORD fcc
,
745 LPVOID lp
, LONG size
)
747 IAVIStreamImpl
*This
= (IAVIStreamImpl
*)iface
;
749 TRACE("(%p,0x%08lx,%p,%ld)\n", iface
, fcc
, lp
, size
);
751 assert(This
->pStream
!= NULL
);
753 return IAVIStream_WriteData(This
->pStream
, fcc
, lp
, size
);
756 static HRESULT WINAPI
ICMStream_fnSetInfo(IAVIStream
*iface
,
757 LPAVISTREAMINFOW info
, LONG infolen
)
759 FIXME("(%p,%p,%ld): stub\n", iface
, info
, infolen
);
764 /***********************************************************************/
766 static HRESULT
AVIFILE_EncodeFrame(IAVIStreamImpl
*This
,
767 LPBITMAPINFOHEADER lpbi
, LPVOID lpBits
)
769 DWORD dwMinQual
, dwMaxQual
, dwCurQual
;
773 BOOL bDecreasedQual
= FALSE
;
777 /* make lKeyFrameEvery and at start a keyframe */
778 if ((This
->lKeyFrameEvery
!= 0 &&
779 (This
->lCurrent
- This
->lLastKey
) >= This
->lKeyFrameEvery
) ||
780 This
->lCurrent
== This
->sInfo
.dwStart
) {
781 idxFlags
= AVIIF_KEYFRAME
;
782 icmFlags
= ICCOMPRESS_KEYFRAME
;
785 if (This
->lKeyFrameEvery
!= 0) {
786 if (This
->lCurrent
== This
->sInfo
.dwStart
) {
787 if (idxFlags
& AVIIF_KEYFRAME
) {
788 /* for keyframes allow to consume all unused bytes */
789 dwRequest
= This
->dwBytesPerFrame
+ This
->dwUnusedBytes
;
790 This
->dwUnusedBytes
= 0;
792 /* for non-keyframes only allow something of the unused bytes to be consumed */
796 if (This
->dwBytesPerFrame
>= This
->dwUnusedBytes
)
797 tmp1
= This
->dwBytesPerFrame
/ This
->lKeyFrameEvery
;
798 tmp2
= (This
->dwUnusedBytes
+ tmp1
) / This
->lKeyFrameEvery
;
800 dwRequest
= This
->dwBytesPerFrame
- tmp1
+ tmp2
;
801 This
->dwUnusedBytes
-= tmp2
;
804 dwRequest
= MAX_FRAMESIZE
;
806 /* only one keyframe at start desired */
807 if (This
->lCurrent
== This
->sInfo
.dwStart
) {
808 dwRequest
= This
->dwBytesPerFrame
+ This
->dwUnusedBytes
;
809 This
->dwUnusedBytes
= 0;
811 dwRequest
= MAX_FRAMESIZE
;
814 /* must we check for framesize to gain requested
815 * datarate or could we trust codec? */
816 doSizeCheck
= (dwRequest
!= 0 && ((This
->dwICMFlags
& (VIDCF_CRUNCH
|VIDCF_QUALITY
)) == 0));
818 dwMaxQual
= dwCurQual
= This
->sInfo
.dwQuality
;
819 dwMinQual
= ICQUALITY_LOW
;
822 if ((icmFlags
& ICCOMPRESS_KEYFRAME
) == 0 &&
823 (This
->dwICMFlags
& VIDCF_FASTTEMPORALC
) == 0)
830 hr
= ICCompress(This
->hic
,icmFlags
,This
->lpbiCur
,This
->lpCur
,lpbi
,lpBits
,
831 &idxCkid
, &idxFlags
, This
->lCurrent
, dwRequest
, dwCurQual
,
832 noPrev
? NULL
:This
->lpbiPrev
, noPrev
? NULL
:This
->lpPrev
);
833 if (hr
== ICERR_NEWPALETTE
) {
834 FIXME(": codec has changed palette -- unhandled!\n");
835 } else if (hr
!= ICERR_OK
)
836 return AVIERR_COMPRESSOR
;
838 /* need to check for framesize */
842 if (dwRequest
>= This
->lpbiCur
->biSizeImage
) {
843 /* frame is smaller -- try to maximize quality */
844 if (dwMaxQual
- dwCurQual
> 10) {
845 DWORD tmp
= dwRequest
/ 8;
847 if (tmp
< MAX_FRAMESIZE_DIFF
)
848 tmp
= MAX_FRAMESIZE_DIFF
;
850 if (tmp
< dwRequest
- This
->lpbiCur
->biSizeImage
&& bDecreasedQual
) {
852 dwCurQual
= (dwMinQual
+ dwMaxQual
) / 2;
858 } else if (dwMaxQual
- dwMinQual
<= 1) {
861 dwMaxQual
= dwCurQual
;
863 if (bDecreasedQual
|| dwCurQual
== This
->dwLastQuality
)
864 dwCurQual
= (dwMinQual
+ dwMaxQual
) / 2;
866 FIXME(": no new quality computed min=%lu cur=%lu max=%lu last=%lu\n",
867 dwMinQual
, dwCurQual
, dwMaxQual
, This
->dwLastQuality
);
869 bDecreasedQual
= TRUE
;
873 /* remember some values */
874 This
->dwLastQuality
= dwCurQual
;
875 This
->dwUnusedBytes
= dwRequest
- This
->lpbiCur
->biSizeImage
;
876 if (icmFlags
& ICCOMPRESS_KEYFRAME
)
877 This
->lLastKey
= This
->lCurrent
;
879 /* Does we manage previous frame? */
880 if (This
->lpPrev
!= NULL
&& This
->lKeyFrameEvery
!= 1)
881 ICDecompress(This
->hic
, 0, This
->lpbiCur
, This
->lpCur
,
882 This
->lpbiPrev
, This
->lpPrev
);
887 static HRESULT
AVIFILE_OpenGetFrame(IAVIStreamImpl
*This
)
889 LPBITMAPINFOHEADER lpbi
;
893 assert(This
!= NULL
);
894 assert(This
->pStream
!= NULL
);
895 assert(This
->pg
== NULL
);
897 This
->pg
= AVIStreamGetFrameOpen(This
->pStream
, NULL
);
898 if (This
->pg
== NULL
)
901 /* When we only decompress this is enough */
902 if (This
->sInfo
.fccHandler
== comptypeDIB
)
905 assert(This
->hic
!= NULL
);
906 assert(This
->lpbiOutput
== NULL
);
908 /* get input format */
909 lpbi
= (LPBITMAPINFOHEADER
)AVIStreamGetFrame(This
->pg
, This
->sInfo
.dwStart
);
911 return AVIERR_MEMORY
;
913 /* get memory for output format */
914 size
= ICCompressGetFormatSize(This
->hic
, lpbi
);
915 if ((LONG
)size
< (LONG
)sizeof(BITMAPINFOHEADER
))
916 return AVIERR_COMPRESSOR
;
917 This
->lpbiOutput
= (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, size
);
918 if (This
->lpbiOutput
== NULL
)
919 return AVIERR_MEMORY
;
920 This
->cbOutput
= size
;
922 if (ICCompressGetFormat(This
->hic
, lpbi
, This
->lpbiOutput
) < S_OK
)
923 return AVIERR_BADFORMAT
;
925 /* update AVISTREAMINFO structure */
926 This
->sInfo
.rcFrame
.right
=
927 This
->sInfo
.rcFrame
.left
+ This
->lpbiOutput
->biWidth
;
928 This
->sInfo
.rcFrame
.bottom
=
929 This
->sInfo
.rcFrame
.top
+ This
->lpbiOutput
->biHeight
;
930 This
->sInfo
.dwSuggestedBufferSize
=
931 ICCompressGetSize(This
->hic
, lpbi
, This
->lpbiOutput
);
933 /* prepare codec for compression */
934 if (ICCompressBegin(This
->hic
, lpbi
, This
->lpbiOutput
) != S_OK
)
935 return AVIERR_COMPRESSOR
;
937 /* allocate memory for current frame */
938 size
+= This
->sInfo
.dwSuggestedBufferSize
;
939 This
->lpbiCur
= (LPBITMAPINFOHEADER
)GlobalAllocPtr(GMEM_MOVEABLE
, size
);
940 if (This
->lpbiCur
== NULL
)
941 return AVIERR_MEMORY
;
942 memcpy(This
->lpbiCur
, This
->lpbiOutput
, This
->cbOutput
);
943 This
->lpCur
= DIBPTR(This
->lpbiCur
);
945 /* allocate memory for last frame if needed */
946 if (This
->lKeyFrameEvery
!= 1 &&
947 (This
->dwICMFlags
& VIDCF_FASTTEMPORALC
) == 0) {
948 size
= ICDecompressGetFormatSize(This
->hic
, This
->lpbiOutput
);
949 This
->lpbiPrev
= (LPBITMAPINFOHEADER
)GlobalAllocPtr(GHND
, size
);
950 if (This
->lpbiPrev
== NULL
)
951 return AVIERR_MEMORY
;
952 if (ICDecompressGetFormat(This
->hic
, This
->lpbiOutput
, This
->lpbiPrev
) < S_OK
)
953 return AVIERR_COMPRESSOR
;
955 if (This
->lpbiPrev
->biSizeImage
== 0) {
956 This
->lpbiPrev
->biSizeImage
=
957 DIBWIDTHBYTES(*This
->lpbiPrev
) * This
->lpbiPrev
->biHeight
;
960 /* get memory for format and picture */
961 size
+= This
->lpbiPrev
->biSizeImage
;
963 (LPBITMAPINFOHEADER
)GlobalReAllocPtr(This
->lpbiPrev
,size
,GMEM_MOVEABLE
);
964 if (This
->lpbiPrev
== NULL
)
965 return AVIERR_MEMORY
;
966 This
->lpPrev
= DIBPTR(This
->lpbiPrev
);
968 /* prepare codec also for decompression */
969 if (ICDecompressBegin(This
->hic
,This
->lpbiOutput
,This
->lpbiPrev
) != S_OK
)
970 return AVIERR_COMPRESSOR
;