2 * Copyright 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"
34 #include "extrachunk.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
40 /***********************************************************************/
42 /* internal interface to get access to table of stream in an editable stream */
43 typedef struct IEditStreamInternal IEditStreamInternal
;
45 typedef struct _EditStreamTable
{
46 PAVISTREAM pStream
; /* stream which contains the data */
47 DWORD dwStart
; /* where starts the part which is also our */
48 DWORD dwLength
; /* how many is also in this stream */
51 #define INTERFACE IEditStreamInternal
52 #define IEditStreamInternal_METHODS \
54 STDMETHOD(GetEditStreamImpl)(THIS_ LPVOID*) PURE;
55 ICOM_DEFINE(IEditStreamInternal
, IUnknown
)
58 #define EditStreamEnd(This,streamNr) ((This)->pStreams[streamNr].dwStart + \
59 (This)->pStreams[streamNr].dwLength)
61 /***********************************************************************/
63 static HRESULT WINAPI
IAVIEditStream_fnQueryInterface(IAVIEditStream
*iface
,REFIID refiid
,LPVOID
*obj
);
64 static ULONG WINAPI
IAVIEditStream_fnAddRef(IAVIEditStream
*iface
);
65 static ULONG WINAPI
IAVIEditStream_fnRelease(IAVIEditStream
*iface
);
66 static HRESULT WINAPI
IAVIEditStream_fnCut(IAVIEditStream
*iface
,LONG
*plStart
,
67 LONG
*plLength
,PAVISTREAM
*ppResult
);
68 static HRESULT WINAPI
IAVIEditStream_fnCopy(IAVIEditStream
*iface
,LONG
*plStart
,
69 LONG
*plLength
,PAVISTREAM
*ppResult
);
70 static HRESULT WINAPI
IAVIEditStream_fnPaste(IAVIEditStream
*iface
,LONG
*plStart
,
71 LONG
*plLength
,PAVISTREAM pSource
,
72 LONG lStart
,LONG lEnd
);
73 static HRESULT WINAPI
IAVIEditStream_fnClone(IAVIEditStream
*iface
,
75 static HRESULT WINAPI
IAVIEditStream_fnSetInfo(IAVIEditStream
*iface
,
76 LPAVISTREAMINFOW asi
,LONG size
);
78 struct ICOM_VTABLE(IAVIEditStream
) ieditstream
= {
79 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
80 IAVIEditStream_fnQueryInterface
,
81 IAVIEditStream_fnAddRef
,
82 IAVIEditStream_fnRelease
,
84 IAVIEditStream_fnCopy
,
85 IAVIEditStream_fnPaste
,
86 IAVIEditStream_fnClone
,
87 IAVIEditStream_fnSetInfo
90 static HRESULT WINAPI
IEditAVIStream_fnQueryInterface(IAVIStream
*iface
,REFIID refiid
,LPVOID
*obj
);
91 static ULONG WINAPI
IEditAVIStream_fnAddRef(IAVIStream
*iface
);
92 static ULONG WINAPI
IEditAVIStream_fnRelease(IAVIStream
*iface
);
93 static HRESULT WINAPI
IEditAVIStream_fnCreate(IAVIStream
*iface
,LPARAM lParam1
,LPARAM lParam2
);
94 static HRESULT WINAPI
IEditAVIStream_fnInfo(IAVIStream
*iface
,AVISTREAMINFOW
*psi
,LONG size
);
95 static LONG WINAPI
IEditAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,
97 static HRESULT WINAPI
IEditAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG
*formatsize
);
98 static HRESULT WINAPI
IEditAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG formatsize
);
99 static HRESULT WINAPI
IEditAVIStream_fnRead(IAVIStream
*iface
,LONG start
,
100 LONG samples
,LPVOID buffer
,
101 LONG buffersize
,LONG
*bytesread
,
103 static HRESULT WINAPI
IEditAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,
104 LONG samples
,LPVOID buffer
,
105 LONG buffersize
,DWORD flags
,
106 LONG
*sampwritten
,LONG
*byteswritten
);
107 static HRESULT WINAPI
IEditAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,LONG samples
);
108 static HRESULT WINAPI
IEditAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,
109 LPVOID lp
,LONG
*lpread
);
110 static HRESULT WINAPI
IEditAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,
111 LPVOID lp
,LONG size
);
112 static HRESULT WINAPI
IEditAVIStream_fnSetInfo(IAVIStream
*iface
,AVISTREAMINFOW
*info
,LONG infolen
);
114 struct ICOM_VTABLE(IAVIStream
) ieditstast
= {
115 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
116 IEditAVIStream_fnQueryInterface
,
117 IEditAVIStream_fnAddRef
,
118 IEditAVIStream_fnRelease
,
119 IEditAVIStream_fnCreate
,
120 IEditAVIStream_fnInfo
,
121 IEditAVIStream_fnFindSample
,
122 IEditAVIStream_fnReadFormat
,
123 IEditAVIStream_fnSetFormat
,
124 IEditAVIStream_fnRead
,
125 IEditAVIStream_fnWrite
,
126 IEditAVIStream_fnDelete
,
127 IEditAVIStream_fnReadData
,
128 IEditAVIStream_fnWriteData
,
129 IEditAVIStream_fnSetInfo
132 static HRESULT WINAPI
IEditStreamInternal_fnQueryInterface(IEditStreamInternal
*iface
,REFIID refiid
,LPVOID
*obj
);
133 static ULONG WINAPI
IEditStreamInternal_fnAddRef(IEditStreamInternal
*iface
);
134 static ULONG WINAPI
IEditStreamInternal_fnRelease(IEditStreamInternal
*iface
);
135 static HRESULT WINAPI
IEditStreamInternal_fnGetEditStreamImpl(IEditStreamInternal
*iface
,LPVOID
*ppimpl
);
137 struct ICOM_VTABLE(IEditStreamInternal
) ieditstreaminternal
= {
138 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
139 IEditStreamInternal_fnQueryInterface
,
140 IEditStreamInternal_fnAddRef
,
141 IEditStreamInternal_fnRelease
,
142 IEditStreamInternal_fnGetEditStreamImpl
145 typedef struct _IAVIEditStreamImpl IAVIEditStreamImpl
;
147 typedef struct _IEditAVIStreamImpl
{
149 ICOM_VFIELD(IAVIStream
);
151 /* IAVIStream stuff */
152 IAVIEditStreamImpl
*pae
;
153 } IEditAVIStreamImpl
;
155 typedef struct _IEditStreamInternalImpl
{
157 ICOM_VFIELD(IEditStreamInternal
);
159 /* IEditStreamInternal stuff */
160 IAVIEditStreamImpl
*pae
;
161 } IEditStreamInternalImpl
;
163 struct _IAVIEditStreamImpl
{
165 ICOM_VFIELD(IAVIEditStream
);
168 /* IAVIEditStream stuff */
169 IEditAVIStreamImpl iAVIStream
;
170 IEditStreamInternalImpl iEditStreamInternal
;
172 AVISTREAMINFOW sInfo
;
174 EditStreamTable
*pStreams
;
175 DWORD nStreams
; /* current fill level of pStreams table */
176 DWORD nTableSize
; /* size of pStreams table */
179 PAVISTREAM pCurStream
;
180 PGETFRAME pg
; /* IGetFrame for pCurStream */
181 LPBITMAPINFOHEADER lpFrame
; /* frame of pCurStream */
184 /***********************************************************************/
186 PAVIEDITSTREAM
AVIFILE_CreateEditStream(PAVISTREAM pstream
)
188 IAVIEditStreamImpl
*pedit
= NULL
;
190 pedit
= (IAVIEditStreamImpl
*)LocalAlloc(LPTR
,sizeof(IAVIEditStreamImpl
));
194 pedit
->lpVtbl
= &ieditstream
;
195 pedit
->iAVIStream
.lpVtbl
= &ieditstast
;
196 pedit
->iAVIStream
.pae
= pedit
;
197 pedit
->iEditStreamInternal
.lpVtbl
= &ieditstreaminternal
;
198 pedit
->iEditStreamInternal
.pae
= pedit
;
201 IAVIStream_Create((PAVISTREAM
)&pedit
->iAVIStream
,(LPARAM
)pstream
,0);
203 return (PAVIEDITSTREAM
)pedit
;
206 static HRESULT
AVIFILE_FindStreamInTable(IAVIEditStreamImpl
* const This
,
207 DWORD pos
,PAVISTREAM
*ppStream
,
209 DWORD
* streamNr
,BOOL bFindSample
)
213 TRACE("(%p,%lu,%p,%p,%p,%d)\n",This
,pos
,ppStream
,streamPos
,
214 streamNr
,bFindSample
);
216 if (pos
< This
->sInfo
.dwStart
)
217 return AVIERR_BADPARAM
;
219 pos
-= This
->sInfo
.dwStart
;
220 for (n
= 0; n
< This
->nStreams
; n
++) {
221 if (pos
< This
->pStreams
[n
].dwLength
) {
222 *ppStream
= This
->pStreams
[n
].pStream
;
223 *streamPos
= This
->pStreams
[n
].dwStart
+ pos
;
224 if (streamNr
!= NULL
)
229 pos
-= This
->pStreams
[n
].dwLength
;
231 if (pos
== 0 && bFindSample
) {
232 *ppStream
= This
->pStreams
[--n
].pStream
;
233 *streamPos
= EditStreamEnd(This
, n
);
234 if (streamNr
!= NULL
)
237 TRACE(" -- pos=0 && b=1 -> (%p,%lu,%lu)\n",*ppStream
, *streamPos
, n
);
242 if (streamNr
!= NULL
)
245 TRACE(" -> ERROR (NULL,0,0)\n");
246 return AVIERR_BADPARAM
;
250 static LPVOID
AVIFILE_ReadFrame(IAVIEditStreamImpl
* const This
,
251 PAVISTREAM pstream
, LONG pos
)
255 TRACE("(%p,%p,%ld)\n",This
,pstream
,pos
);
260 /* if stream changes make sure that only palette changes */
261 if (This
->pCurStream
!= pstream
) {
262 pg
= AVIStreamGetFrameOpen(pstream
, NULL
);
265 if (This
->pg
!= NULL
) {
266 if (IGetFrame_SetFormat(pg
, This
->lpFrame
, NULL
, 0, 0, -1, -1)) {
267 AVIStreamGetFrameClose(pg
);
268 ERR(": IGetFrame_SetFormat failed\n");
271 AVIStreamGetFrameClose(This
->pg
);
274 This
->pCurStream
= pstream
;
277 /* now get the decompressed frame */
278 This
->lpFrame
= AVIStreamGetFrame(This
->pg
, pos
);
279 if (This
->lpFrame
!= NULL
)
280 This
->sInfo
.dwSuggestedBufferSize
= This
->lpFrame
->biSizeImage
;
282 return This
->lpFrame
;
285 static HRESULT
AVIFILE_RemoveStream(IAVIEditStreamImpl
* const This
, DWORD nr
)
287 assert(This
!= NULL
);
288 assert(nr
< This
->nStreams
);
291 IAVIStream_Release(This
->pStreams
[nr
].pStream
);
293 if (This
->nStreams
- nr
> 0) {
294 memmove(This
->pStreams
+ nr
, This
->pStreams
+ nr
+ 1,
295 (This
->nStreams
- nr
) * sizeof(EditStreamTable
));
297 This
->pStreams
[This
->nStreams
].pStream
= NULL
;
298 This
->pStreams
[This
->nStreams
].dwStart
= 0;
299 This
->pStreams
[This
->nStreams
].dwLength
= 0;
301 /* try to merge the part before the deleted one and the one after it */
302 if (0 < nr
&& 0 < This
->nStreams
&&
303 This
->pStreams
[nr
- 1].pStream
== This
->pStreams
[nr
].pStream
) {
304 if (EditStreamEnd(This
, nr
- 1) == This
->pStreams
[nr
].dwStart
) {
305 This
->pStreams
[nr
- 1].dwLength
+= This
->pStreams
[nr
].dwLength
;
306 return AVIFILE_RemoveStream(This
, nr
);
313 static BOOL
AVIFILE_FormatsEqual(PAVISTREAM avi1
, PAVISTREAM avi2
)
315 LPVOID fmt1
= NULL
, fmt2
= NULL
;
316 LONG size1
, size2
, start1
, start2
;
319 assert(avi1
!= NULL
&& avi2
!= NULL
);
321 /* get stream starts and check format sizes */
322 start1
= AVIStreamStart(avi1
);
323 start2
= AVIStreamStart(avi2
);
324 if (FAILED(AVIStreamFormatSize(avi1
, start1
, &size1
)))
326 if (FAILED(AVIStreamFormatSize(avi2
, start2
, &size2
)))
331 /* sizes match, now get formats and compare them */
332 fmt1
= GlobalAllocPtr(GHND
, size1
);
335 if (SUCCEEDED(AVIStreamReadFormat(avi1
, start1
, fmt1
, &size1
))) {
336 fmt2
= GlobalAllocPtr(GHND
, size1
);
338 if (SUCCEEDED(AVIStreamReadFormat(avi2
, start2
, fmt2
, &size1
)))
339 status
= (memcmp(fmt1
, fmt2
, size1
) == 0);
350 /***********************************************************************/
352 static HRESULT WINAPI
IAVIEditStream_fnQueryInterface(IAVIEditStream
*iface
,REFIID refiid
,LPVOID
*obj
)
354 ICOM_THIS(IAVIEditStreamImpl
,iface
);
356 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
358 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
359 IsEqualGUID(&IID_IAVIEditStream
, refiid
)) {
361 IAVIEditStream_AddRef(iface
);
364 } else if (IsEqualGUID(&IID_IAVIStream
, refiid
)) {
365 *obj
= &This
->iAVIStream
;
366 IAVIEditStream_AddRef(iface
);
369 } else if (IsEqualGUID(&IID_IEditStreamInternal
, refiid
)) {
370 *obj
= &This
->iEditStreamInternal
;
371 IAVIEditStream_AddRef(iface
);
376 return OLE_E_ENUM_NOMORE
;
379 static ULONG WINAPI
IAVIEditStream_fnAddRef(IAVIEditStream
*iface
)
381 ICOM_THIS(IAVIEditStreamImpl
,iface
);
383 TRACE("(%p) -> %ld\n", iface
, This
->ref
+ 1);
384 return ++(This
->ref
);
387 static ULONG WINAPI
IAVIEditStream_fnRelease(IAVIEditStream
*iface
)
389 ICOM_THIS(IAVIEditStreamImpl
,iface
);
392 TRACE("(%p) -> %ld\n", iface
, This
->ref
- 1);
394 if (!--(This
->ref
)) {
395 /* releaase memory */
396 if (This
->pg
!= NULL
)
397 AVIStreamGetFrameClose(This
->pg
);
398 if (This
->pStreams
!= NULL
) {
399 for (i
= 0; i
< This
->nStreams
; i
++) {
400 if (This
->pStreams
[i
].pStream
!= NULL
)
401 IAVIStream_Release(This
->pStreams
[i
].pStream
);
403 GlobalFreePtr(This
->pStreams
);
406 LocalFree((HLOCAL
)This
);
412 static HRESULT WINAPI
IAVIEditStream_fnCut(IAVIEditStream
*iface
,LONG
*plStart
,
413 LONG
*plLength
,PAVISTREAM
*ppResult
)
415 ICOM_THIS(IAVIEditStreamImpl
,iface
);
417 DWORD start
, len
, streamPos
, streamNr
;
420 TRACE("(%p,%p,%p,%p)\n",iface
,plStart
,plLength
,ppResult
);
422 if (ppResult
!= NULL
)
424 if (plStart
== NULL
|| plLength
== NULL
|| *plStart
< 0)
425 return AVIERR_BADPARAM
;
427 /* if asked for cutted part copy it before deleting */
428 if (ppResult
!= NULL
) {
429 hr
= IAVIEditStream_Copy(iface
, plStart
, plLength
, ppResult
);
437 /* now delete the requested part */
439 hr
= AVIFILE_FindStreamInTable(This
, start
, &stream
,
440 &streamPos
, &streamNr
, FALSE
);
443 if (This
->pStreams
[streamNr
].dwStart
== streamPos
) {
444 /* deleting from start of part */
445 if (len
< This
->pStreams
[streamNr
].dwLength
) {
447 This
->pStreams
[streamNr
].dwStart
+= len
;
448 This
->pStreams
[streamNr
].dwLength
-= len
;
449 This
->sInfo
.dwLength
-= len
;
452 /* we must return decompressed data now */
453 This
->bDecompress
= TRUE
;
455 /* deleting hole part */
456 len
-= This
->pStreams
[streamNr
].dwLength
;
457 AVIFILE_RemoveStream(This
,streamNr
);
459 } else if (EditStreamEnd(This
, streamNr
) <= streamPos
+ len
) {
460 /* deleting at end of a part */
461 DWORD count
= EditStreamEnd(This
, streamNr
) - streamPos
;
462 This
->sInfo
.dwLength
-= count
;
464 This
->pStreams
[streamNr
].dwLength
=
465 streamPos
- This
->pStreams
[streamNr
].dwStart
;
468 if (This
->nStreams
+ 1 >= This
->nTableSize
) {
470 GlobalReAllocPtr(This
->pStreams
, (This
->nTableSize
+ 32) * sizeof(EditStreamTable
), GMEM_SHARE
|GHND
);
471 if (This
->pStreams
== NULL
)
472 return AVIERR_MEMORY
;
473 This
->nTableSize
+= 32;
475 memmove(This
->pStreams
+ streamNr
+ 1, This
->pStreams
+ streamNr
,
476 (This
->nStreams
- streamNr
) * sizeof(EditStreamTable
));
479 IAVIStream_AddRef(This
->pStreams
[streamNr
+ 1].pStream
);
480 This
->pStreams
[streamNr
+ 1].dwStart
= streamPos
+ len
;
481 This
->pStreams
[streamNr
+ 1].dwLength
=
482 EditStreamEnd(This
, streamNr
) - This
->pStreams
[streamNr
+ 1].dwStart
;
484 This
->pStreams
[streamNr
].dwLength
=
485 streamPos
- This
->pStreams
[streamNr
].dwStart
;
486 This
->sInfo
.dwLength
-= len
;
491 This
->sInfo
.dwEditCount
++;
496 static HRESULT WINAPI
IAVIEditStream_fnCopy(IAVIEditStream
*iface
,LONG
*plStart
,
497 LONG
*plLength
,PAVISTREAM
*ppResult
)
499 ICOM_THIS(IAVIEditStreamImpl
,iface
);
500 IAVIEditStreamImpl
* pEdit
;
504 TRACE("(%p,%p,%p,%p)\n",iface
,plStart
,plLength
,ppResult
);
506 if (ppResult
== NULL
)
507 return AVIERR_BADPARAM
;
509 if (plStart
== NULL
|| plLength
== NULL
|| *plStart
< 0 || *plLength
< 0)
510 return AVIERR_BADPARAM
;
513 if (*(LPDWORD
)plLength
> This
->sInfo
.dwLength
)
514 *(LPDWORD
)plLength
= This
->sInfo
.dwLength
;
515 if (*(LPDWORD
)plStart
< This
->sInfo
.dwStart
) {
516 *(LPDWORD
)plLength
-= This
->sInfo
.dwStart
- *(LPDWORD
)plStart
;
517 *(LPDWORD
)plStart
= This
->sInfo
.dwStart
;
519 return AVIERR_BADPARAM
;
521 if (*(LPDWORD
)plStart
+ *(LPDWORD
)plLength
> This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
)
522 *(LPDWORD
)plLength
= This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
-
525 pEdit
= (IAVIEditStreamImpl
*)AVIFILE_CreateEditStream(NULL
);
527 return AVIERR_MEMORY
;
529 hr
= IAVIEditStream_Paste((PAVIEDITSTREAM
)pEdit
,&start
,plLength
,
530 (PAVISTREAM
)&This
->iAVIStream
,*plStart
,
531 *plStart
+ *plLength
);
534 IAVIEditStream_Release((PAVIEDITSTREAM
)pEdit
);
536 *ppResult
= (PAVISTREAM
)&pEdit
->iAVIStream
;
541 static HRESULT WINAPI
IAVIEditStream_fnPaste(IAVIEditStream
*iface
,LONG
*plStart
,
542 LONG
*plLength
,PAVISTREAM pSource
,
543 LONG lStart
,LONG lLength
)
545 ICOM_THIS(IAVIEditStreamImpl
,iface
);
546 AVISTREAMINFOW srcInfo
;
547 IEditStreamInternal
*pInternal
= NULL
;
548 IAVIEditStreamImpl
*pEdit
= NULL
;
550 DWORD startPos
, endPos
, streamNr
, n
, nStreams
;
552 TRACE("(%p,%p,%p,%p,%ld,%ld)\n",iface
,plStart
,plLength
,
553 pSource
,lStart
,lLength
);
556 return AVIERR_BADHANDLE
;
557 if (plStart
== NULL
|| *plStart
< 0)
558 return AVIERR_BADPARAM
;
559 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< *plStart
)
560 return AVIERR_BADPARAM
; /* Can't paste with holes */
561 if (FAILED(IAVIStream_Info(pSource
, &srcInfo
, sizeof(srcInfo
))))
563 if (lStart
< srcInfo
.dwStart
|| lStart
>= srcInfo
.dwStart
+ srcInfo
.dwLength
)
564 return AVIERR_BADPARAM
;
565 if (This
->sInfo
.fccType
== 0) {
566 /* This stream is empty */
567 IAVIStream_Info(pSource
, &This
->sInfo
, sizeof(This
->sInfo
));
568 This
->sInfo
.dwStart
= *plStart
;
569 This
->sInfo
.dwLength
= 0;
571 if (This
->sInfo
.fccType
!= srcInfo
.fccType
)
572 return AVIERR_UNSUPPORTED
; /* different stream types */
573 if (lLength
== -1) /* Copy the hole stream */
574 lLength
= srcInfo
.dwLength
;
575 if (lStart
+ lLength
> srcInfo
.dwStart
+ srcInfo
.dwLength
)
576 lLength
= srcInfo
.dwStart
+ srcInfo
.dwLength
- lStart
;
577 if (lLength
+ *plStart
>= 0x80000000)
578 return AVIERR_MEMORY
;
580 /* streamtype specific tests */
581 if (srcInfo
.fccType
== streamtypeVIDEO
) {
584 size
= srcInfo
.rcFrame
.right
- srcInfo
.rcFrame
.left
;
585 if (size
!= This
->sInfo
.rcFrame
.right
- This
->sInfo
.rcFrame
.left
)
586 return AVIERR_UNSUPPORTED
; /* FIXME: Can't GetFrame convert it? */
587 size
= srcInfo
.rcFrame
.bottom
- srcInfo
.rcFrame
.top
;
588 if (size
!= This
->sInfo
.rcFrame
.bottom
- This
->sInfo
.rcFrame
.top
)
589 return AVIERR_UNSUPPORTED
; /* FIXME: Can't GetFrame convert it? */
590 } else if (srcInfo
.fccType
== streamtypeAUDIO
) {
591 if (! AVIFILE_FormatsEqual((PAVISTREAM
)&This
->iAVIStream
, pSource
))
592 return AVIERR_UNSUPPORTED
;
594 /* FIXME: streamtypeMIDI and streamtypeTEXT */
595 return AVIERR_UNSUPPORTED
;
598 /* try to get an IEditStreamInternal interface */
599 if (SUCCEEDED(IAVIStream_QueryInterface(pSource
, &IID_IEditStreamInternal
,
600 (LPVOID
*)&pInternal
))) {
601 pInternal
->lpVtbl
->GetEditStreamImpl(pInternal
, (LPVOID
*)&pEdit
);
602 pInternal
->lpVtbl
->Release(pInternal
);
605 /* for video must check for change of format */
606 if (This
->sInfo
.fccType
== streamtypeVIDEO
) {
607 if (! This
->bDecompress
) {
608 /* Need to decompress if any of the following conditions matches:
609 * - pSource is an editable stream which decompresses
610 * - the nearest keyframe of pSource isn't lStart
611 * - the nearest keyframe of this stream isn't *plStart
612 * - the format of pSource doesn't match this one
614 if ((pEdit
!= NULL
&& pEdit
->bDecompress
) ||
615 AVIStreamNearestKeyFrame(pSource
, lStart
) != lStart
||
616 AVIStreamNearestKeyFrame((PAVISTREAM
)&This
->iAVIStream
, *plStart
) != *plStart
||
617 (This
->nStreams
> 0 && !AVIFILE_FormatsEqual((PAVISTREAM
)&This
->iAVIStream
, pSource
))) {
618 /* Use first stream part to get format to convert everything to */
619 AVIFILE_ReadFrame(This
, This
->pStreams
[0].pStream
,
620 This
->pStreams
[0].dwStart
);
622 /* Check if we could convert the source streams to the disired format... */
624 if (FAILED(AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
,
625 &startPos
, &streamNr
, TRUE
)))
626 return AVIERR_INTERNAL
;
627 for (n
= lStart
; n
< lStart
+ lLength
; streamNr
++) {
628 if (AVIFILE_ReadFrame(This
, pEdit
->pStreams
[streamNr
].pStream
, startPos
) == NULL
)
629 return AVIERR_BADFORMAT
;
630 startPos
= pEdit
->pStreams
[streamNr
].dwStart
;
631 n
+= pEdit
->pStreams
[streamNr
].dwLength
;
633 } else if (AVIFILE_ReadFrame(This
, pSource
, lStart
) == NULL
)
634 return AVIERR_BADFORMAT
;
636 This
->bDecompress
= TRUE
;
637 This
->sInfo
.fccHandler
= 0;
639 } else if (AVIFILE_ReadFrame(This
, pSource
, lStart
) == NULL
)
640 return AVIERR_BADFORMAT
; /* Can't convert source to own format */
641 } /* FIXME: something special for the other formats? */
643 /* Make sure we have enough memory for parts */
647 AVIFILE_FindStreamInTable(pEdit
, lStart
+ lLength
, &pStream
,
648 &endPos
, &nLastStream
, TRUE
);
649 AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
,
650 &startPos
, &streamNr
, FALSE
);
651 if (nLastStream
== streamNr
)
654 nStreams
= nLastStream
- streamNr
;
657 if (This
->nStreams
+ nStreams
+ 1 > This
->nTableSize
) {
658 n
= This
->nStreams
+ nStreams
+ 33;
661 GlobalReAllocPtr(This
->pStreams
, n
* sizeof(EditStreamTable
), GMEM_SHARE
|GHND
);
662 if (This
->pStreams
== NULL
)
663 return AVIERR_MEMORY
;
664 This
->nTableSize
= n
;
667 if (plLength
!= NULL
)
670 /* now do the real work */
671 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
> *plStart
) {
672 AVIFILE_FindStreamInTable(This
, *plStart
, &pStream
,
673 &startPos
, &streamNr
, FALSE
);
674 if (startPos
!= This
->pStreams
[streamNr
].dwStart
) {
675 /* split stream streamNr at startPos */
676 memmove(This
->pStreams
+ streamNr
+ nStreams
+ 1,
677 This
->pStreams
+ streamNr
,
678 (This
->nStreams
+ nStreams
- streamNr
+ 1) * sizeof(EditStreamTable
));
680 This
->pStreams
[streamNr
+ 2].dwLength
=
681 EditStreamEnd(This
, streamNr
+ 2) - startPos
;
682 This
->pStreams
[streamNr
+ 2].dwStart
= startPos
;
683 This
->pStreams
[streamNr
].dwLength
=
684 startPos
- This
->pStreams
[streamNr
].dwStart
;
685 IAVIStream_AddRef(This
->pStreams
[streamNr
].pStream
);
688 /* insert before stream at streamNr */
689 memmove(This
->pStreams
+ streamNr
+ nStreams
, This
->pStreams
+ streamNr
,
690 (This
->nStreams
+ nStreams
- streamNr
) * sizeof(EditStreamTable
));
692 } else /* append the streams */
693 streamNr
= This
->nStreams
;
696 /* insert the parts of the editable stream instead of itself */
697 AVIFILE_FindStreamInTable(pEdit
, lStart
+ lLength
, &pStream
,
698 &endPos
, NULL
, FALSE
);
699 AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
, &startPos
, &n
, FALSE
);
701 memcpy(This
->pStreams
+ streamNr
, pEdit
->pStreams
+ n
,
702 nStreams
* sizeof(EditStreamTable
));
703 if (This
->pStreams
[streamNr
].dwStart
< startPos
) {
704 This
->pStreams
[streamNr
].dwLength
=
705 EditStreamEnd(This
, streamNr
) - startPos
;
706 This
->pStreams
[streamNr
].dwStart
= startPos
;
708 if (endPos
< EditStreamEnd(This
, streamNr
+ nStreams
))
709 This
->pStreams
[streamNr
+ nStreams
].dwLength
=
710 endPos
- This
->pStreams
[streamNr
+ nStreams
].dwStart
;
712 /* a simple stream */
713 This
->pStreams
[streamNr
].pStream
= pSource
;
714 This
->pStreams
[streamNr
].dwStart
= lStart
;
715 This
->pStreams
[streamNr
].dwLength
= lLength
;
718 for (n
= 0; n
< nStreams
; n
++) {
719 IAVIStream_AddRef(This
->pStreams
[streamNr
+ n
].pStream
);
720 if (0 < streamNr
+ n
&&
721 This
->pStreams
[streamNr
+ n
- 1].pStream
!= This
->pStreams
[streamNr
+ n
].pStream
) {
722 This
->sInfo
.dwFlags
|= AVISTREAMINFO_FORMATCHANGES
;
723 This
->sInfo
.dwFormatChangeCount
++;
726 This
->sInfo
.dwEditCount
++;
727 This
->sInfo
.dwLength
+= lLength
;
728 This
->nStreams
+= nStreams
;
733 static HRESULT WINAPI
IAVIEditStream_fnClone(IAVIEditStream
*iface
,
736 ICOM_THIS(IAVIEditStreamImpl
,iface
);
737 IAVIEditStreamImpl
* pEdit
;
740 TRACE("(%p,%p)\n",iface
,ppResult
);
742 if (ppResult
== NULL
)
743 return AVIERR_BADPARAM
;
746 pEdit
= (IAVIEditStreamImpl
*)AVIFILE_CreateEditStream(NULL
);
748 return AVIERR_MEMORY
;
749 if (This
->nStreams
> pEdit
->nTableSize
) {
750 pEdit
->pStreams
= GlobalReAllocPtr(pEdit
->pStreams
, This
->nStreams
* sizeof(EditStreamTable
),GMEM_SHARE
|GHND
);
751 if (pEdit
->pStreams
== NULL
)
752 return AVIERR_MEMORY
;
753 pEdit
->nTableSize
= This
->nStreams
;
755 pEdit
->nStreams
= This
->nStreams
;
756 memcpy(pEdit
->pStreams
, This
->pStreams
,
757 This
->nStreams
* sizeof(EditStreamTable
));
758 memcpy(&pEdit
->sInfo
,&This
->sInfo
,sizeof(This
->sInfo
));
759 for (i
= 0; i
< This
->nStreams
; i
++) {
760 if (pEdit
->pStreams
[i
].pStream
!= NULL
)
761 IAVIStream_AddRef(pEdit
->pStreams
[i
].pStream
);
764 *ppResult
= (PAVISTREAM
)&pEdit
->iAVIStream
;
769 static HRESULT WINAPI
IAVIEditStream_fnSetInfo(IAVIEditStream
*iface
,
770 LPAVISTREAMINFOW asi
,LONG size
)
772 ICOM_THIS(IAVIEditStreamImpl
,iface
);
774 TRACE("(%p,%p,%ld)\n",iface
,asi
,size
);
776 /* check parameters */
778 return AVIERR_BADPARAM
;
779 if (size
!= sizeof(AVISTREAMINFOW
))
780 return AVIERR_BADSIZE
;
781 if (asi
->dwScale
== 0 || asi
->dwRate
== 0 || (LONG
)asi
->dwQuality
< -1 ||
782 asi
->dwQuality
> ICQUALITY_HIGH
)
785 This
->sInfo
.wLanguage
= asi
->wLanguage
;
786 This
->sInfo
.wPriority
= asi
->wPriority
;
787 This
->sInfo
.dwStart
= asi
->dwStart
;
788 if (asi
->dwRate
!= 0)
789 This
->sInfo
.dwRate
= asi
->dwRate
;
790 if (asi
->dwScale
!= 0)
791 This
->sInfo
.dwScale
= asi
->dwScale
;
792 if (asi
->dwQuality
<= ICQUALITY_HIGH
)
793 This
->sInfo
.dwQuality
= ICQUALITY_HIGH
;
794 CopyRect(&This
->sInfo
.rcFrame
, &asi
->rcFrame
);
795 memcpy(&This
->sInfo
.szName
, &asi
->szName
, sizeof(asi
->szName
));
796 This
->sInfo
.dwEditCount
++;
801 static HRESULT WINAPI
IEditAVIStream_fnQueryInterface(IAVIStream
*iface
,
802 REFIID refiid
,LPVOID
*obj
)
804 ICOM_THIS(IEditAVIStreamImpl
,iface
);
806 assert(This
->pae
!= NULL
);
808 return IAVIEditStream_QueryInterface((IAVIEditStream
*)This
->pae
,refiid
,obj
);
811 static ULONG WINAPI
IEditAVIStream_fnAddRef(IAVIStream
*iface
)
813 ICOM_THIS(IEditAVIStreamImpl
,iface
);
815 assert(This
->pae
!= NULL
);
817 return IAVIEditStream_AddRef((IAVIEditStream
*)This
->pae
);
820 static ULONG WINAPI
IEditAVIStream_fnRelease(IAVIStream
*iface
)
822 ICOM_THIS(IEditAVIStreamImpl
,iface
);
824 assert(This
->pae
!= NULL
);
826 return IAVIEditStream_Release((IAVIEditStream
*)This
->pae
);
829 static HRESULT WINAPI
IEditAVIStream_fnCreate(IAVIStream
*iface
,
830 LPARAM lParam1
,LPARAM lParam2
)
832 IAVIEditStreamImpl
*This
= ((IEditAVIStreamImpl
*)iface
)->pae
;
837 if (This
->pStreams
== NULL
) {
839 GlobalAllocPtr(GMEM_SHARE
|GHND
, 256 * sizeof(EditStreamTable
));
840 if (This
->pStreams
== NULL
)
841 return AVIERR_MEMORY
;
842 This
->nTableSize
= 256;
846 IAVIStream_Info((PAVISTREAM
)lParam1
, &This
->sInfo
, sizeof(This
->sInfo
));
847 IAVIStream_AddRef((PAVISTREAM
)lParam1
);
848 This
->pStreams
[0].pStream
= (PAVISTREAM
)lParam1
;
849 This
->pStreams
[0].dwStart
= This
->sInfo
.dwStart
;
850 This
->pStreams
[0].dwLength
= This
->sInfo
.dwLength
;
856 static HRESULT WINAPI
IEditAVIStream_fnInfo(IAVIStream
*iface
,
857 AVISTREAMINFOW
*psi
,LONG size
)
859 ICOM_THIS(IEditAVIStreamImpl
,iface
);
861 TRACE("(%p,%p,%ld)\n",iface
,psi
,size
);
863 assert(This
->pae
!= NULL
);
866 return AVIERR_BADPARAM
;
868 return AVIERR_BADSIZE
;
870 if (This
->pae
->bDecompress
)
871 This
->pae
->sInfo
.fccHandler
= 0;
873 memcpy(psi
, &This
->pae
->sInfo
, min((DWORD
)size
, sizeof(This
->pae
->sInfo
)));
875 if ((DWORD
)size
< sizeof(This
->pae
->sInfo
))
876 return AVIERR_BUFFERTOOSMALL
;
880 static LONG WINAPI
IEditAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,
883 IAVIEditStreamImpl
* const This
= ((IEditAVIStreamImpl
* const)iface
)->pae
;
885 DWORD streamPos
, streamNr
;
887 TRACE("(%p,%ld,0x%08lX)\n",iface
,pos
,flags
);
889 if (flags
& FIND_FROM_START
)
890 pos
= (LONG
)This
->sInfo
.dwStart
;
892 /* outside of stream? */
893 if (pos
< (LONG
)This
->sInfo
.dwStart
||
894 (LONG
)This
->sInfo
.dwStart
+ (LONG
)This
->sInfo
.dwLength
<= pos
)
897 /* map our position to a stream and position in it */
898 if (AVIFILE_FindStreamInTable(This
, pos
, &stream
, &streamPos
,
900 return -1; /* doesn't exist */
902 if (This
->bDecompress
) {
903 /* only one stream -- format changes only at start */
904 if (flags
& FIND_FORMAT
)
905 return (flags
& FIND_NEXT
? -1 : 0);
907 /* FIXME: map positions back to us */
908 return IAVIStream_FindSample(stream
, streamPos
, flags
);
910 /* assume change of format every frame */
915 static HRESULT WINAPI
IEditAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,
916 LPVOID format
,LONG
*fmtsize
)
918 IAVIEditStreamImpl
* const This
= ((IEditAVIStreamImpl
* const)iface
)->pae
;
919 LPBITMAPINFOHEADER lp
;
924 TRACE("(%p,%ld,%p,%p)\n",iface
,pos
,format
,fmtsize
);
926 if (fmtsize
== NULL
|| pos
< This
->sInfo
.dwStart
||
927 This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
<= pos
)
928 return AVIERR_BADPARAM
;
930 /* find stream corresponding to position */
931 hr
= AVIFILE_FindStreamInTable(This
, pos
, &stream
, &n
, NULL
, FALSE
);
935 if (! This
->bDecompress
)
936 return IAVIStream_ReadFormat(stream
, n
, format
, fmtsize
);
938 lp
= (LPBITMAPINFOHEADER
)AVIFILE_ReadFrame(This
, stream
, n
);
941 if (lp
->biBitCount
<= 8) {
942 n
= (lp
->biClrUsed
> 0 ? lp
->biClrUsed
: 1 << lp
->biBitCount
);
943 n
*= sizeof(RGBQUAD
);
948 memcpy(format
, lp
, min((LONG
)n
, *fmtsize
));
949 hr
= ((LONG
)n
> *fmtsize
? AVIERR_BUFFERTOOSMALL
: AVIERR_OK
);
955 static HRESULT WINAPI
IEditAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,
956 LPVOID format
,LONG formatsize
)
958 TRACE("(%p,%ld,%p,%ld)\n",iface
,pos
,format
,formatsize
);
960 return AVIERR_UNSUPPORTED
;
963 static HRESULT WINAPI
IEditAVIStream_fnRead(IAVIStream
*iface
,LONG start
,
964 LONG samples
,LPVOID buffer
,
965 LONG buffersize
,LONG
*bytesread
,
968 IAVIEditStreamImpl
* const This
= ((IEditAVIStreamImpl
* const)iface
)->pae
;
970 DWORD streamPos
, streamNr
;
971 LONG readBytes
, readSamples
, count
;
974 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p) -- 0x%08lX\n",iface
,start
,samples
,
975 buffer
,buffersize
,bytesread
,samplesread
,This
->sInfo
.fccType
);
977 /* check parameters */
978 if (bytesread
!= NULL
)
980 if (samplesread
!= NULL
)
983 return AVIERR_BADSIZE
;
984 if ((DWORD
)start
< This
->sInfo
.dwStart
||
985 This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< (DWORD
)start
)
986 return AVIERR_BADPARAM
;
988 if (! This
->bDecompress
) {
989 /* audio like data -- sample-based */
992 return AVIERR_OK
; /* nothing at all or already done */
994 if (FAILED(AVIFILE_FindStreamInTable(This
, start
, &stream
,
995 &streamPos
, &streamNr
, FALSE
)))
998 /* limit to end of the stream */
1000 if (streamPos
+ count
> EditStreamEnd(This
, streamNr
))
1001 count
= EditStreamEnd(This
, streamNr
) - streamPos
;
1003 hr
= IAVIStream_Read(stream
, streamPos
, count
, buffer
, buffersize
,
1004 &readBytes
, &readSamples
);
1007 if (readBytes
== 0 && readSamples
== 0 && count
!= 0)
1008 return AVIERR_FILEREAD
; /* for bad stream implementations */
1010 if (samplesread
!= NULL
)
1011 *samplesread
+= readSamples
;
1012 if (bytesread
!= NULL
)
1013 *bytesread
+= readBytes
;
1014 if (buffer
!= NULL
) {
1015 (LPBYTE
)buffer
+= readBytes
;
1016 buffersize
-= readBytes
;
1020 } while (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
> start
);
1022 /* video like data -- frame-based */
1023 LPBITMAPINFOHEADER lp
;
1028 if (FAILED(AVIFILE_FindStreamInTable(This
, start
, &stream
,
1029 &streamPos
, &streamNr
, FALSE
)))
1030 return AVIERR_ERROR
;
1032 lp
= AVIFILE_ReadFrame(This
, stream
, streamPos
);
1034 return AVIERR_ERROR
;
1036 if (buffer
!= NULL
) {
1037 /* need size of format to skip */
1038 if (lp
->biBitCount
<= 8) {
1039 count
= lp
->biClrUsed
> 0 ? lp
->biClrUsed
: 1 << lp
->biBitCount
;
1040 count
*= sizeof(RGBQUAD
);
1043 count
+= lp
->biSize
;
1045 if (buffersize
< lp
->biSizeImage
)
1046 return AVIERR_BUFFERTOOSMALL
;
1047 memcpy(buffer
, (LPBYTE
)lp
+ count
, lp
->biSizeImage
);
1050 if (bytesread
!= NULL
)
1051 *bytesread
= lp
->biSizeImage
;
1052 if (samplesread
!= NULL
)
1059 static HRESULT WINAPI
IEditAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,
1060 LONG samples
,LPVOID buffer
,
1061 LONG buffersize
,DWORD flags
,
1062 LONG
*sampwritten
,LONG
*byteswritten
)
1064 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n",iface
,start
,samples
,buffer
,
1065 buffersize
,flags
,sampwritten
,byteswritten
);
1067 /* be sure return parameters have correct values */
1068 if (sampwritten
!= NULL
)
1070 if (byteswritten
!= NULL
)
1073 return AVIERR_UNSUPPORTED
;
1076 static HRESULT WINAPI
IEditAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,
1079 ICOM_THIS(IEditAVIStreamImpl
,iface
);
1081 TRACE("(%p,%ld,%ld)\n",iface
,start
,samples
);
1083 return IAVIEditStream_Cut((IAVIEditStream
*)This
->pae
,&start
,&samples
,NULL
);
1086 static HRESULT WINAPI
IEditAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,
1087 LPVOID lp
,LONG
*lpread
)
1089 IAVIEditStreamImpl
* const This
= ((IEditAVIStreamImpl
* const)iface
)->pae
;
1092 TRACE("(%p,0x%08lX,%p,%p)\n",iface
,fcc
,lp
,lpread
);
1094 /* check parameters */
1095 if (lp
== NULL
|| lpread
== NULL
)
1096 return AVIERR_BADPARAM
;
1098 /* simply ask every stream and return the first block found */
1099 for (n
= 0; n
< This
->nStreams
; n
++) {
1100 HRESULT hr
= IAVIStream_ReadData(This
->pStreams
[n
].pStream
,fcc
,lp
,lpread
);
1107 return AVIERR_NODATA
;
1110 static HRESULT WINAPI
IEditAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,
1111 LPVOID lp
,LONG size
)
1113 TRACE("(%p,0x%08lX,%p,%ld)\n",iface
,fcc
,lp
,size
);
1115 return AVIERR_UNSUPPORTED
;
1118 static HRESULT WINAPI
IEditAVIStream_fnSetInfo(IAVIStream
*iface
,
1119 AVISTREAMINFOW
*info
,LONG len
)
1121 ICOM_THIS(IEditAVIStreamImpl
,iface
);
1123 TRACE("(%p,%p,%ld)\n",iface
,info
,len
);
1125 return IAVIEditStream_SetInfo((IAVIEditStream
*)This
->pae
,info
,len
);
1128 static HRESULT WINAPI
IEditStreamInternal_fnQueryInterface(IEditStreamInternal
*iface
,REFIID refiid
,LPVOID
*obj
)
1130 ICOM_THIS(IEditStreamInternalImpl
,iface
);
1132 assert(This
->pae
!= NULL
);
1134 return IAVIEditStream_QueryInterface((IAVIEditStream
*)This
->pae
, refiid
, obj
);
1137 static ULONG WINAPI
IEditStreamInternal_fnAddRef(IEditStreamInternal
*iface
)
1139 ICOM_THIS(IEditStreamInternalImpl
,iface
);
1141 assert(This
->pae
!= NULL
);
1143 return IAVIEditStream_AddRef((IAVIEditStream
*)This
->pae
);
1146 static ULONG WINAPI
IEditStreamInternal_fnRelease(IEditStreamInternal
*iface
)
1148 ICOM_THIS(IEditStreamInternalImpl
,iface
);
1150 assert(This
->pae
!= NULL
);
1152 return IAVIEditStream_Release((IAVIEditStream
*)This
->pae
);
1155 static HRESULT WINAPI
IEditStreamInternal_fnGetEditStreamImpl(IEditStreamInternal
*iface
,LPVOID
*ppimpl
)
1157 ICOM_THIS(IEditStreamInternalImpl
,iface
);
1159 TRACE("(%p,%p) -> %p\n", iface
, ppimpl
, This
->pae
);
1161 assert(This
->pae
!= NULL
);
1162 assert(ppimpl
!= NULL
);
1164 *ppimpl
= This
->pae
;