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 DECLARE_INTERFACE_(IEditStreamInternal
, IUnknown
) { IEditStreamInternal_METHODS
};
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 IAVIEditStreamVtbl ieditstream
= {
79 IAVIEditStream_fnQueryInterface
,
80 IAVIEditStream_fnAddRef
,
81 IAVIEditStream_fnRelease
,
83 IAVIEditStream_fnCopy
,
84 IAVIEditStream_fnPaste
,
85 IAVIEditStream_fnClone
,
86 IAVIEditStream_fnSetInfo
89 static HRESULT WINAPI
IEditAVIStream_fnQueryInterface(IAVIStream
*iface
,REFIID refiid
,LPVOID
*obj
);
90 static ULONG WINAPI
IEditAVIStream_fnAddRef(IAVIStream
*iface
);
91 static ULONG WINAPI
IEditAVIStream_fnRelease(IAVIStream
*iface
);
92 static HRESULT WINAPI
IEditAVIStream_fnCreate(IAVIStream
*iface
,LPARAM lParam1
,LPARAM lParam2
);
93 static HRESULT WINAPI
IEditAVIStream_fnInfo(IAVIStream
*iface
,AVISTREAMINFOW
*psi
,LONG size
);
94 static LONG WINAPI
IEditAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,
96 static HRESULT WINAPI
IEditAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG
*formatsize
);
97 static HRESULT WINAPI
IEditAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,LPVOID format
,LONG formatsize
);
98 static HRESULT WINAPI
IEditAVIStream_fnRead(IAVIStream
*iface
,LONG start
,
99 LONG samples
,LPVOID buffer
,
100 LONG buffersize
,LONG
*bytesread
,
102 static HRESULT WINAPI
IEditAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,
103 LONG samples
,LPVOID buffer
,
104 LONG buffersize
,DWORD flags
,
105 LONG
*sampwritten
,LONG
*byteswritten
);
106 static HRESULT WINAPI
IEditAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,LONG samples
);
107 static HRESULT WINAPI
IEditAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,
108 LPVOID lp
,LONG
*lpread
);
109 static HRESULT WINAPI
IEditAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,
110 LPVOID lp
,LONG size
);
111 static HRESULT WINAPI
IEditAVIStream_fnSetInfo(IAVIStream
*iface
,AVISTREAMINFOW
*info
,LONG infolen
);
113 struct IAVIStreamVtbl ieditstast
= {
114 IEditAVIStream_fnQueryInterface
,
115 IEditAVIStream_fnAddRef
,
116 IEditAVIStream_fnRelease
,
117 IEditAVIStream_fnCreate
,
118 IEditAVIStream_fnInfo
,
119 IEditAVIStream_fnFindSample
,
120 IEditAVIStream_fnReadFormat
,
121 IEditAVIStream_fnSetFormat
,
122 IEditAVIStream_fnRead
,
123 IEditAVIStream_fnWrite
,
124 IEditAVIStream_fnDelete
,
125 IEditAVIStream_fnReadData
,
126 IEditAVIStream_fnWriteData
,
127 IEditAVIStream_fnSetInfo
130 static HRESULT WINAPI
IEditStreamInternal_fnQueryInterface(IEditStreamInternal
*iface
,REFIID refiid
,LPVOID
*obj
);
131 static ULONG WINAPI
IEditStreamInternal_fnAddRef(IEditStreamInternal
*iface
);
132 static ULONG WINAPI
IEditStreamInternal_fnRelease(IEditStreamInternal
*iface
);
133 static HRESULT WINAPI
IEditStreamInternal_fnGetEditStreamImpl(IEditStreamInternal
*iface
,LPVOID
*ppimpl
);
135 struct IEditStreamInternalVtbl ieditstreaminternal
= {
136 IEditStreamInternal_fnQueryInterface
,
137 IEditStreamInternal_fnAddRef
,
138 IEditStreamInternal_fnRelease
,
139 IEditStreamInternal_fnGetEditStreamImpl
142 typedef struct _IAVIEditStreamImpl IAVIEditStreamImpl
;
144 typedef struct _IEditAVIStreamImpl
{
146 IAVIStreamVtbl
*lpVtbl
;
148 /* IAVIStream stuff */
149 IAVIEditStreamImpl
*pae
;
150 } IEditAVIStreamImpl
;
152 typedef struct _IEditStreamInternalImpl
{
154 IEditStreamInternalVtbl
*lpVtbl
;
156 /* IEditStreamInternal stuff */
157 IAVIEditStreamImpl
*pae
;
158 } IEditStreamInternalImpl
;
160 struct _IAVIEditStreamImpl
{
162 IAVIEditStreamVtbl
*lpVtbl
;
165 /* IAVIEditStream stuff */
166 IEditAVIStreamImpl iAVIStream
;
167 IEditStreamInternalImpl iEditStreamInternal
;
169 AVISTREAMINFOW sInfo
;
171 EditStreamTable
*pStreams
;
172 DWORD nStreams
; /* current fill level of pStreams table */
173 DWORD nTableSize
; /* size of pStreams table */
176 PAVISTREAM pCurStream
;
177 PGETFRAME pg
; /* IGetFrame for pCurStream */
178 LPBITMAPINFOHEADER lpFrame
; /* frame of pCurStream */
181 /***********************************************************************/
183 PAVIEDITSTREAM
AVIFILE_CreateEditStream(PAVISTREAM pstream
)
185 IAVIEditStreamImpl
*pedit
= NULL
;
187 pedit
= (IAVIEditStreamImpl
*)LocalAlloc(LPTR
,sizeof(IAVIEditStreamImpl
));
191 pedit
->lpVtbl
= &ieditstream
;
192 pedit
->iAVIStream
.lpVtbl
= &ieditstast
;
193 pedit
->iAVIStream
.pae
= pedit
;
194 pedit
->iEditStreamInternal
.lpVtbl
= &ieditstreaminternal
;
195 pedit
->iEditStreamInternal
.pae
= pedit
;
198 IAVIStream_Create((PAVISTREAM
)&pedit
->iAVIStream
,(LPARAM
)pstream
,0);
200 return (PAVIEDITSTREAM
)pedit
;
203 static HRESULT
AVIFILE_FindStreamInTable(IAVIEditStreamImpl
* const This
,
204 DWORD pos
,PAVISTREAM
*ppStream
,
206 DWORD
* streamNr
,BOOL bFindSample
)
210 TRACE("(%p,%lu,%p,%p,%p,%d)\n",This
,pos
,ppStream
,streamPos
,
211 streamNr
,bFindSample
);
213 if (pos
< This
->sInfo
.dwStart
)
214 return AVIERR_BADPARAM
;
216 pos
-= This
->sInfo
.dwStart
;
217 for (n
= 0; n
< This
->nStreams
; n
++) {
218 if (pos
< This
->pStreams
[n
].dwLength
) {
219 *ppStream
= This
->pStreams
[n
].pStream
;
220 *streamPos
= This
->pStreams
[n
].dwStart
+ pos
;
221 if (streamNr
!= NULL
)
226 pos
-= This
->pStreams
[n
].dwLength
;
228 if (pos
== 0 && bFindSample
) {
229 *ppStream
= This
->pStreams
[--n
].pStream
;
230 *streamPos
= EditStreamEnd(This
, n
);
231 if (streamNr
!= NULL
)
234 TRACE(" -- pos=0 && b=1 -> (%p,%lu,%lu)\n",*ppStream
, *streamPos
, n
);
239 if (streamNr
!= NULL
)
242 TRACE(" -> ERROR (NULL,0,0)\n");
243 return AVIERR_BADPARAM
;
247 static LPVOID
AVIFILE_ReadFrame(IAVIEditStreamImpl
* const This
,
248 PAVISTREAM pstream
, LONG pos
)
252 TRACE("(%p,%p,%ld)\n",This
,pstream
,pos
);
257 /* if stream changes make sure that only palette changes */
258 if (This
->pCurStream
!= pstream
) {
259 pg
= AVIStreamGetFrameOpen(pstream
, NULL
);
262 if (This
->pg
!= NULL
) {
263 if (IGetFrame_SetFormat(pg
, This
->lpFrame
, NULL
, 0, 0, -1, -1)) {
264 AVIStreamGetFrameClose(pg
);
265 ERR(": IGetFrame_SetFormat failed\n");
268 AVIStreamGetFrameClose(This
->pg
);
271 This
->pCurStream
= pstream
;
274 /* now get the decompressed frame */
275 This
->lpFrame
= AVIStreamGetFrame(This
->pg
, pos
);
276 if (This
->lpFrame
!= NULL
)
277 This
->sInfo
.dwSuggestedBufferSize
= This
->lpFrame
->biSizeImage
;
279 return This
->lpFrame
;
282 static HRESULT
AVIFILE_RemoveStream(IAVIEditStreamImpl
* const This
, DWORD nr
)
284 assert(This
!= NULL
);
285 assert(nr
< This
->nStreams
);
288 IAVIStream_Release(This
->pStreams
[nr
].pStream
);
290 if (This
->nStreams
- nr
> 0) {
291 memmove(This
->pStreams
+ nr
, This
->pStreams
+ nr
+ 1,
292 (This
->nStreams
- nr
) * sizeof(EditStreamTable
));
294 This
->pStreams
[This
->nStreams
].pStream
= NULL
;
295 This
->pStreams
[This
->nStreams
].dwStart
= 0;
296 This
->pStreams
[This
->nStreams
].dwLength
= 0;
298 /* try to merge the part before the deleted one and the one after it */
299 if (0 < nr
&& 0 < This
->nStreams
&&
300 This
->pStreams
[nr
- 1].pStream
== This
->pStreams
[nr
].pStream
) {
301 if (EditStreamEnd(This
, nr
- 1) == This
->pStreams
[nr
].dwStart
) {
302 This
->pStreams
[nr
- 1].dwLength
+= This
->pStreams
[nr
].dwLength
;
303 return AVIFILE_RemoveStream(This
, nr
);
310 static BOOL
AVIFILE_FormatsEqual(PAVISTREAM avi1
, PAVISTREAM avi2
)
312 LPVOID fmt1
= NULL
, fmt2
= NULL
;
313 LONG size1
, size2
, start1
, start2
;
316 assert(avi1
!= NULL
&& avi2
!= NULL
);
318 /* get stream starts and check format sizes */
319 start1
= AVIStreamStart(avi1
);
320 start2
= AVIStreamStart(avi2
);
321 if (FAILED(AVIStreamFormatSize(avi1
, start1
, &size1
)))
323 if (FAILED(AVIStreamFormatSize(avi2
, start2
, &size2
)))
328 /* sizes match, now get formats and compare them */
329 fmt1
= GlobalAllocPtr(GHND
, size1
);
332 if (SUCCEEDED(AVIStreamReadFormat(avi1
, start1
, fmt1
, &size1
))) {
333 fmt2
= GlobalAllocPtr(GHND
, size1
);
335 if (SUCCEEDED(AVIStreamReadFormat(avi2
, start2
, fmt2
, &size1
)))
336 status
= (memcmp(fmt1
, fmt2
, size1
) == 0);
347 /***********************************************************************/
349 static HRESULT WINAPI
IAVIEditStream_fnQueryInterface(IAVIEditStream
*iface
,REFIID refiid
,LPVOID
*obj
)
351 ICOM_THIS(IAVIEditStreamImpl
,iface
);
353 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
355 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
356 IsEqualGUID(&IID_IAVIEditStream
, refiid
)) {
358 IAVIEditStream_AddRef(iface
);
361 } else if (IsEqualGUID(&IID_IAVIStream
, refiid
)) {
362 *obj
= &This
->iAVIStream
;
363 IAVIEditStream_AddRef(iface
);
366 } else if (IsEqualGUID(&IID_IEditStreamInternal
, refiid
)) {
367 *obj
= &This
->iEditStreamInternal
;
368 IAVIEditStream_AddRef(iface
);
373 return OLE_E_ENUM_NOMORE
;
376 static ULONG WINAPI
IAVIEditStream_fnAddRef(IAVIEditStream
*iface
)
378 ICOM_THIS(IAVIEditStreamImpl
,iface
);
380 TRACE("(%p) -> %ld\n", iface
, This
->ref
+ 1);
381 return ++(This
->ref
);
384 static ULONG WINAPI
IAVIEditStream_fnRelease(IAVIEditStream
*iface
)
386 ICOM_THIS(IAVIEditStreamImpl
,iface
);
389 TRACE("(%p) -> %ld\n", iface
, This
->ref
- 1);
391 if (!--(This
->ref
)) {
392 /* releaase memory */
393 if (This
->pg
!= NULL
)
394 AVIStreamGetFrameClose(This
->pg
);
395 if (This
->pStreams
!= NULL
) {
396 for (i
= 0; i
< This
->nStreams
; i
++) {
397 if (This
->pStreams
[i
].pStream
!= NULL
)
398 IAVIStream_Release(This
->pStreams
[i
].pStream
);
400 GlobalFreePtr(This
->pStreams
);
403 LocalFree((HLOCAL
)This
);
409 static HRESULT WINAPI
IAVIEditStream_fnCut(IAVIEditStream
*iface
,LONG
*plStart
,
410 LONG
*plLength
,PAVISTREAM
*ppResult
)
412 ICOM_THIS(IAVIEditStreamImpl
,iface
);
414 DWORD start
, len
, streamPos
, streamNr
;
417 TRACE("(%p,%p,%p,%p)\n",iface
,plStart
,plLength
,ppResult
);
419 if (ppResult
!= NULL
)
421 if (plStart
== NULL
|| plLength
== NULL
|| *plStart
< 0)
422 return AVIERR_BADPARAM
;
424 /* if asked for cutted part copy it before deleting */
425 if (ppResult
!= NULL
) {
426 hr
= IAVIEditStream_Copy(iface
, plStart
, plLength
, ppResult
);
434 /* now delete the requested part */
436 hr
= AVIFILE_FindStreamInTable(This
, start
, &stream
,
437 &streamPos
, &streamNr
, FALSE
);
440 if (This
->pStreams
[streamNr
].dwStart
== streamPos
) {
441 /* deleting from start of part */
442 if (len
< This
->pStreams
[streamNr
].dwLength
) {
444 This
->pStreams
[streamNr
].dwStart
+= len
;
445 This
->pStreams
[streamNr
].dwLength
-= len
;
446 This
->sInfo
.dwLength
-= len
;
449 /* we must return decompressed data now */
450 This
->bDecompress
= TRUE
;
452 /* deleting hole part */
453 len
-= This
->pStreams
[streamNr
].dwLength
;
454 AVIFILE_RemoveStream(This
,streamNr
);
456 } else if (EditStreamEnd(This
, streamNr
) <= streamPos
+ len
) {
457 /* deleting at end of a part */
458 DWORD count
= EditStreamEnd(This
, streamNr
) - streamPos
;
459 This
->sInfo
.dwLength
-= count
;
461 This
->pStreams
[streamNr
].dwLength
=
462 streamPos
- This
->pStreams
[streamNr
].dwStart
;
465 if (This
->nStreams
+ 1 >= This
->nTableSize
) {
467 GlobalReAllocPtr(This
->pStreams
, (This
->nTableSize
+ 32) * sizeof(EditStreamTable
), GMEM_SHARE
|GHND
);
468 if (This
->pStreams
== NULL
)
469 return AVIERR_MEMORY
;
470 This
->nTableSize
+= 32;
472 memmove(This
->pStreams
+ streamNr
+ 1, This
->pStreams
+ streamNr
,
473 (This
->nStreams
- streamNr
) * sizeof(EditStreamTable
));
476 IAVIStream_AddRef(This
->pStreams
[streamNr
+ 1].pStream
);
477 This
->pStreams
[streamNr
+ 1].dwStart
= streamPos
+ len
;
478 This
->pStreams
[streamNr
+ 1].dwLength
=
479 EditStreamEnd(This
, streamNr
) - This
->pStreams
[streamNr
+ 1].dwStart
;
481 This
->pStreams
[streamNr
].dwLength
=
482 streamPos
- This
->pStreams
[streamNr
].dwStart
;
483 This
->sInfo
.dwLength
-= len
;
488 This
->sInfo
.dwEditCount
++;
493 static HRESULT WINAPI
IAVIEditStream_fnCopy(IAVIEditStream
*iface
,LONG
*plStart
,
494 LONG
*plLength
,PAVISTREAM
*ppResult
)
496 ICOM_THIS(IAVIEditStreamImpl
,iface
);
497 IAVIEditStreamImpl
* pEdit
;
501 TRACE("(%p,%p,%p,%p)\n",iface
,plStart
,plLength
,ppResult
);
503 if (ppResult
== NULL
)
504 return AVIERR_BADPARAM
;
506 if (plStart
== NULL
|| plLength
== NULL
|| *plStart
< 0 || *plLength
< 0)
507 return AVIERR_BADPARAM
;
510 if (*(LPDWORD
)plLength
> This
->sInfo
.dwLength
)
511 *(LPDWORD
)plLength
= This
->sInfo
.dwLength
;
512 if (*(LPDWORD
)plStart
< This
->sInfo
.dwStart
) {
513 *(LPDWORD
)plLength
-= This
->sInfo
.dwStart
- *(LPDWORD
)plStart
;
514 *(LPDWORD
)plStart
= This
->sInfo
.dwStart
;
516 return AVIERR_BADPARAM
;
518 if (*(LPDWORD
)plStart
+ *(LPDWORD
)plLength
> This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
)
519 *(LPDWORD
)plLength
= This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
-
522 pEdit
= (IAVIEditStreamImpl
*)AVIFILE_CreateEditStream(NULL
);
524 return AVIERR_MEMORY
;
526 hr
= IAVIEditStream_Paste((PAVIEDITSTREAM
)pEdit
,&start
,plLength
,
527 (PAVISTREAM
)&This
->iAVIStream
,*plStart
,
528 *plStart
+ *plLength
);
531 IAVIEditStream_Release((PAVIEDITSTREAM
)pEdit
);
533 *ppResult
= (PAVISTREAM
)&pEdit
->iAVIStream
;
538 static HRESULT WINAPI
IAVIEditStream_fnPaste(IAVIEditStream
*iface
,LONG
*plStart
,
539 LONG
*plLength
,PAVISTREAM pSource
,
540 LONG lStart
,LONG lLength
)
542 ICOM_THIS(IAVIEditStreamImpl
,iface
);
543 AVISTREAMINFOW srcInfo
;
544 IEditStreamInternal
*pInternal
= NULL
;
545 IAVIEditStreamImpl
*pEdit
= NULL
;
547 DWORD startPos
, endPos
, streamNr
, n
, nStreams
;
549 TRACE("(%p,%p,%p,%p,%ld,%ld)\n",iface
,plStart
,plLength
,
550 pSource
,lStart
,lLength
);
553 return AVIERR_BADHANDLE
;
554 if (plStart
== NULL
|| *plStart
< 0)
555 return AVIERR_BADPARAM
;
556 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< *plStart
)
557 return AVIERR_BADPARAM
; /* Can't paste with holes */
558 if (FAILED(IAVIStream_Info(pSource
, &srcInfo
, sizeof(srcInfo
))))
560 if (lStart
< srcInfo
.dwStart
|| lStart
>= srcInfo
.dwStart
+ srcInfo
.dwLength
)
561 return AVIERR_BADPARAM
;
562 if (This
->sInfo
.fccType
== 0) {
563 /* This stream is empty */
564 IAVIStream_Info(pSource
, &This
->sInfo
, sizeof(This
->sInfo
));
565 This
->sInfo
.dwStart
= *plStart
;
566 This
->sInfo
.dwLength
= 0;
568 if (This
->sInfo
.fccType
!= srcInfo
.fccType
)
569 return AVIERR_UNSUPPORTED
; /* different stream types */
570 if (lLength
== -1) /* Copy the hole stream */
571 lLength
= srcInfo
.dwLength
;
572 if (lStart
+ lLength
> srcInfo
.dwStart
+ srcInfo
.dwLength
)
573 lLength
= srcInfo
.dwStart
+ srcInfo
.dwLength
- lStart
;
574 if (lLength
+ *plStart
>= 0x80000000)
575 return AVIERR_MEMORY
;
577 /* streamtype specific tests */
578 if (srcInfo
.fccType
== streamtypeVIDEO
) {
581 size
= srcInfo
.rcFrame
.right
- srcInfo
.rcFrame
.left
;
582 if (size
!= This
->sInfo
.rcFrame
.right
- This
->sInfo
.rcFrame
.left
)
583 return AVIERR_UNSUPPORTED
; /* FIXME: Can't GetFrame convert it? */
584 size
= srcInfo
.rcFrame
.bottom
- srcInfo
.rcFrame
.top
;
585 if (size
!= This
->sInfo
.rcFrame
.bottom
- This
->sInfo
.rcFrame
.top
)
586 return AVIERR_UNSUPPORTED
; /* FIXME: Can't GetFrame convert it? */
587 } else if (srcInfo
.fccType
== streamtypeAUDIO
) {
588 if (! AVIFILE_FormatsEqual((PAVISTREAM
)&This
->iAVIStream
, pSource
))
589 return AVIERR_UNSUPPORTED
;
591 /* FIXME: streamtypeMIDI and streamtypeTEXT */
592 return AVIERR_UNSUPPORTED
;
595 /* try to get an IEditStreamInternal interface */
596 if (SUCCEEDED(IAVIStream_QueryInterface(pSource
, &IID_IEditStreamInternal
,
597 (LPVOID
*)&pInternal
))) {
598 pInternal
->lpVtbl
->GetEditStreamImpl(pInternal
, (LPVOID
*)&pEdit
);
599 pInternal
->lpVtbl
->Release(pInternal
);
602 /* for video must check for change of format */
603 if (This
->sInfo
.fccType
== streamtypeVIDEO
) {
604 if (! This
->bDecompress
) {
605 /* Need to decompress if any of the following conditions matches:
606 * - pSource is an editable stream which decompresses
607 * - the nearest keyframe of pSource isn't lStart
608 * - the nearest keyframe of this stream isn't *plStart
609 * - the format of pSource doesn't match this one
611 if ((pEdit
!= NULL
&& pEdit
->bDecompress
) ||
612 AVIStreamNearestKeyFrame(pSource
, lStart
) != lStart
||
613 AVIStreamNearestKeyFrame((PAVISTREAM
)&This
->iAVIStream
, *plStart
) != *plStart
||
614 (This
->nStreams
> 0 && !AVIFILE_FormatsEqual((PAVISTREAM
)&This
->iAVIStream
, pSource
))) {
615 /* Use first stream part to get format to convert everything to */
616 AVIFILE_ReadFrame(This
, This
->pStreams
[0].pStream
,
617 This
->pStreams
[0].dwStart
);
619 /* Check if we could convert the source streams to the disired format... */
621 if (FAILED(AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
,
622 &startPos
, &streamNr
, TRUE
)))
623 return AVIERR_INTERNAL
;
624 for (n
= lStart
; n
< lStart
+ lLength
; streamNr
++) {
625 if (AVIFILE_ReadFrame(This
, pEdit
->pStreams
[streamNr
].pStream
, startPos
) == NULL
)
626 return AVIERR_BADFORMAT
;
627 startPos
= pEdit
->pStreams
[streamNr
].dwStart
;
628 n
+= pEdit
->pStreams
[streamNr
].dwLength
;
630 } else if (AVIFILE_ReadFrame(This
, pSource
, lStart
) == NULL
)
631 return AVIERR_BADFORMAT
;
633 This
->bDecompress
= TRUE
;
634 This
->sInfo
.fccHandler
= 0;
636 } else if (AVIFILE_ReadFrame(This
, pSource
, lStart
) == NULL
)
637 return AVIERR_BADFORMAT
; /* Can't convert source to own format */
638 } /* FIXME: something special for the other formats? */
640 /* Make sure we have enough memory for parts */
644 AVIFILE_FindStreamInTable(pEdit
, lStart
+ lLength
, &pStream
,
645 &endPos
, &nLastStream
, TRUE
);
646 AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
,
647 &startPos
, &streamNr
, FALSE
);
648 if (nLastStream
== streamNr
)
651 nStreams
= nLastStream
- streamNr
;
654 if (This
->nStreams
+ nStreams
+ 1 > This
->nTableSize
) {
655 n
= This
->nStreams
+ nStreams
+ 33;
658 GlobalReAllocPtr(This
->pStreams
, n
* sizeof(EditStreamTable
), GMEM_SHARE
|GHND
);
659 if (This
->pStreams
== NULL
)
660 return AVIERR_MEMORY
;
661 This
->nTableSize
= n
;
664 if (plLength
!= NULL
)
667 /* now do the real work */
668 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
> *plStart
) {
669 AVIFILE_FindStreamInTable(This
, *plStart
, &pStream
,
670 &startPos
, &streamNr
, FALSE
);
671 if (startPos
!= This
->pStreams
[streamNr
].dwStart
) {
672 /* split stream streamNr at startPos */
673 memmove(This
->pStreams
+ streamNr
+ nStreams
+ 1,
674 This
->pStreams
+ streamNr
,
675 (This
->nStreams
+ nStreams
- streamNr
+ 1) * sizeof(EditStreamTable
));
677 This
->pStreams
[streamNr
+ 2].dwLength
=
678 EditStreamEnd(This
, streamNr
+ 2) - startPos
;
679 This
->pStreams
[streamNr
+ 2].dwStart
= startPos
;
680 This
->pStreams
[streamNr
].dwLength
=
681 startPos
- This
->pStreams
[streamNr
].dwStart
;
682 IAVIStream_AddRef(This
->pStreams
[streamNr
].pStream
);
685 /* insert before stream at streamNr */
686 memmove(This
->pStreams
+ streamNr
+ nStreams
, This
->pStreams
+ streamNr
,
687 (This
->nStreams
+ nStreams
- streamNr
) * sizeof(EditStreamTable
));
689 } else /* append the streams */
690 streamNr
= This
->nStreams
;
693 /* insert the parts of the editable stream instead of itself */
694 AVIFILE_FindStreamInTable(pEdit
, lStart
+ lLength
, &pStream
,
695 &endPos
, NULL
, FALSE
);
696 AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
, &startPos
, &n
, FALSE
);
698 memcpy(This
->pStreams
+ streamNr
, pEdit
->pStreams
+ n
,
699 nStreams
* sizeof(EditStreamTable
));
700 if (This
->pStreams
[streamNr
].dwStart
< startPos
) {
701 This
->pStreams
[streamNr
].dwLength
=
702 EditStreamEnd(This
, streamNr
) - startPos
;
703 This
->pStreams
[streamNr
].dwStart
= startPos
;
705 if (endPos
< EditStreamEnd(This
, streamNr
+ nStreams
))
706 This
->pStreams
[streamNr
+ nStreams
].dwLength
=
707 endPos
- This
->pStreams
[streamNr
+ nStreams
].dwStart
;
709 /* a simple stream */
710 This
->pStreams
[streamNr
].pStream
= pSource
;
711 This
->pStreams
[streamNr
].dwStart
= lStart
;
712 This
->pStreams
[streamNr
].dwLength
= lLength
;
715 for (n
= 0; n
< nStreams
; n
++) {
716 IAVIStream_AddRef(This
->pStreams
[streamNr
+ n
].pStream
);
717 if (0 < streamNr
+ n
&&
718 This
->pStreams
[streamNr
+ n
- 1].pStream
!= This
->pStreams
[streamNr
+ n
].pStream
) {
719 This
->sInfo
.dwFlags
|= AVISTREAMINFO_FORMATCHANGES
;
720 This
->sInfo
.dwFormatChangeCount
++;
723 This
->sInfo
.dwEditCount
++;
724 This
->sInfo
.dwLength
+= lLength
;
725 This
->nStreams
+= nStreams
;
730 static HRESULT WINAPI
IAVIEditStream_fnClone(IAVIEditStream
*iface
,
733 ICOM_THIS(IAVIEditStreamImpl
,iface
);
734 IAVIEditStreamImpl
* pEdit
;
737 TRACE("(%p,%p)\n",iface
,ppResult
);
739 if (ppResult
== NULL
)
740 return AVIERR_BADPARAM
;
743 pEdit
= (IAVIEditStreamImpl
*)AVIFILE_CreateEditStream(NULL
);
745 return AVIERR_MEMORY
;
746 if (This
->nStreams
> pEdit
->nTableSize
) {
747 pEdit
->pStreams
= GlobalReAllocPtr(pEdit
->pStreams
, This
->nStreams
* sizeof(EditStreamTable
),GMEM_SHARE
|GHND
);
748 if (pEdit
->pStreams
== NULL
)
749 return AVIERR_MEMORY
;
750 pEdit
->nTableSize
= This
->nStreams
;
752 pEdit
->nStreams
= This
->nStreams
;
753 memcpy(pEdit
->pStreams
, This
->pStreams
,
754 This
->nStreams
* sizeof(EditStreamTable
));
755 memcpy(&pEdit
->sInfo
,&This
->sInfo
,sizeof(This
->sInfo
));
756 for (i
= 0; i
< This
->nStreams
; i
++) {
757 if (pEdit
->pStreams
[i
].pStream
!= NULL
)
758 IAVIStream_AddRef(pEdit
->pStreams
[i
].pStream
);
761 *ppResult
= (PAVISTREAM
)&pEdit
->iAVIStream
;
766 static HRESULT WINAPI
IAVIEditStream_fnSetInfo(IAVIEditStream
*iface
,
767 LPAVISTREAMINFOW asi
,LONG size
)
769 ICOM_THIS(IAVIEditStreamImpl
,iface
);
771 TRACE("(%p,%p,%ld)\n",iface
,asi
,size
);
773 /* check parameters */
775 return AVIERR_BADPARAM
;
776 if (size
!= sizeof(AVISTREAMINFOW
))
777 return AVIERR_BADSIZE
;
778 if (asi
->dwScale
== 0 || asi
->dwRate
== 0 || (LONG
)asi
->dwQuality
< -1 ||
779 asi
->dwQuality
> ICQUALITY_HIGH
)
782 This
->sInfo
.wLanguage
= asi
->wLanguage
;
783 This
->sInfo
.wPriority
= asi
->wPriority
;
784 This
->sInfo
.dwStart
= asi
->dwStart
;
785 if (asi
->dwRate
!= 0)
786 This
->sInfo
.dwRate
= asi
->dwRate
;
787 if (asi
->dwScale
!= 0)
788 This
->sInfo
.dwScale
= asi
->dwScale
;
789 if (asi
->dwQuality
<= ICQUALITY_HIGH
)
790 This
->sInfo
.dwQuality
= ICQUALITY_HIGH
;
791 CopyRect(&This
->sInfo
.rcFrame
, &asi
->rcFrame
);
792 memcpy(&This
->sInfo
.szName
, &asi
->szName
, sizeof(asi
->szName
));
793 This
->sInfo
.dwEditCount
++;
798 static HRESULT WINAPI
IEditAVIStream_fnQueryInterface(IAVIStream
*iface
,
799 REFIID refiid
,LPVOID
*obj
)
801 ICOM_THIS(IEditAVIStreamImpl
,iface
);
803 assert(This
->pae
!= NULL
);
805 return IAVIEditStream_QueryInterface((IAVIEditStream
*)This
->pae
,refiid
,obj
);
808 static ULONG WINAPI
IEditAVIStream_fnAddRef(IAVIStream
*iface
)
810 ICOM_THIS(IEditAVIStreamImpl
,iface
);
812 assert(This
->pae
!= NULL
);
814 return IAVIEditStream_AddRef((IAVIEditStream
*)This
->pae
);
817 static ULONG WINAPI
IEditAVIStream_fnRelease(IAVIStream
*iface
)
819 ICOM_THIS(IEditAVIStreamImpl
,iface
);
821 assert(This
->pae
!= NULL
);
823 return IAVIEditStream_Release((IAVIEditStream
*)This
->pae
);
826 static HRESULT WINAPI
IEditAVIStream_fnCreate(IAVIStream
*iface
,
827 LPARAM lParam1
,LPARAM lParam2
)
829 IAVIEditStreamImpl
*This
= ((IEditAVIStreamImpl
*)iface
)->pae
;
834 if (This
->pStreams
== NULL
) {
836 GlobalAllocPtr(GMEM_SHARE
|GHND
, 256 * sizeof(EditStreamTable
));
837 if (This
->pStreams
== NULL
)
838 return AVIERR_MEMORY
;
839 This
->nTableSize
= 256;
843 IAVIStream_Info((PAVISTREAM
)lParam1
, &This
->sInfo
, sizeof(This
->sInfo
));
844 IAVIStream_AddRef((PAVISTREAM
)lParam1
);
845 This
->pStreams
[0].pStream
= (PAVISTREAM
)lParam1
;
846 This
->pStreams
[0].dwStart
= This
->sInfo
.dwStart
;
847 This
->pStreams
[0].dwLength
= This
->sInfo
.dwLength
;
853 static HRESULT WINAPI
IEditAVIStream_fnInfo(IAVIStream
*iface
,
854 AVISTREAMINFOW
*psi
,LONG size
)
856 ICOM_THIS(IEditAVIStreamImpl
,iface
);
858 TRACE("(%p,%p,%ld)\n",iface
,psi
,size
);
860 assert(This
->pae
!= NULL
);
863 return AVIERR_BADPARAM
;
865 return AVIERR_BADSIZE
;
867 if (This
->pae
->bDecompress
)
868 This
->pae
->sInfo
.fccHandler
= 0;
870 memcpy(psi
, &This
->pae
->sInfo
, min((DWORD
)size
, sizeof(This
->pae
->sInfo
)));
872 if ((DWORD
)size
< sizeof(This
->pae
->sInfo
))
873 return AVIERR_BUFFERTOOSMALL
;
877 static LONG WINAPI
IEditAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,
880 IAVIEditStreamImpl
* const This
= ((IEditAVIStreamImpl
* const)iface
)->pae
;
882 DWORD streamPos
, streamNr
;
884 TRACE("(%p,%ld,0x%08lX)\n",iface
,pos
,flags
);
886 if (flags
& FIND_FROM_START
)
887 pos
= (LONG
)This
->sInfo
.dwStart
;
889 /* outside of stream? */
890 if (pos
< (LONG
)This
->sInfo
.dwStart
||
891 (LONG
)This
->sInfo
.dwStart
+ (LONG
)This
->sInfo
.dwLength
<= pos
)
894 /* map our position to a stream and position in it */
895 if (AVIFILE_FindStreamInTable(This
, pos
, &stream
, &streamPos
,
897 return -1; /* doesn't exist */
899 if (This
->bDecompress
) {
900 /* only one stream -- format changes only at start */
901 if (flags
& FIND_FORMAT
)
902 return (flags
& FIND_NEXT
? -1 : 0);
904 /* FIXME: map positions back to us */
905 return IAVIStream_FindSample(stream
, streamPos
, flags
);
907 /* assume change of format every frame */
912 static HRESULT WINAPI
IEditAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,
913 LPVOID format
,LONG
*fmtsize
)
915 IAVIEditStreamImpl
* const This
= ((IEditAVIStreamImpl
* const)iface
)->pae
;
916 LPBITMAPINFOHEADER lp
;
921 TRACE("(%p,%ld,%p,%p)\n",iface
,pos
,format
,fmtsize
);
923 if (fmtsize
== NULL
|| pos
< This
->sInfo
.dwStart
||
924 This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
<= pos
)
925 return AVIERR_BADPARAM
;
927 /* find stream corresponding to position */
928 hr
= AVIFILE_FindStreamInTable(This
, pos
, &stream
, &n
, NULL
, FALSE
);
932 if (! This
->bDecompress
)
933 return IAVIStream_ReadFormat(stream
, n
, format
, fmtsize
);
935 lp
= (LPBITMAPINFOHEADER
)AVIFILE_ReadFrame(This
, stream
, n
);
938 if (lp
->biBitCount
<= 8) {
939 n
= (lp
->biClrUsed
> 0 ? lp
->biClrUsed
: 1 << lp
->biBitCount
);
940 n
*= sizeof(RGBQUAD
);
945 memcpy(format
, lp
, min((LONG
)n
, *fmtsize
));
946 hr
= ((LONG
)n
> *fmtsize
? AVIERR_BUFFERTOOSMALL
: AVIERR_OK
);
952 static HRESULT WINAPI
IEditAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,
953 LPVOID format
,LONG formatsize
)
955 TRACE("(%p,%ld,%p,%ld)\n",iface
,pos
,format
,formatsize
);
957 return AVIERR_UNSUPPORTED
;
960 static HRESULT WINAPI
IEditAVIStream_fnRead(IAVIStream
*iface
,LONG start
,
961 LONG samples
,LPVOID buffer
,
962 LONG buffersize
,LONG
*bytesread
,
965 IAVIEditStreamImpl
* const This
= ((IEditAVIStreamImpl
* const)iface
)->pae
;
967 DWORD streamPos
, streamNr
;
968 LONG readBytes
, readSamples
, count
;
971 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p) -- 0x%08lX\n",iface
,start
,samples
,
972 buffer
,buffersize
,bytesread
,samplesread
,This
->sInfo
.fccType
);
974 /* check parameters */
975 if (bytesread
!= NULL
)
977 if (samplesread
!= NULL
)
980 return AVIERR_BADSIZE
;
981 if ((DWORD
)start
< This
->sInfo
.dwStart
||
982 This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< (DWORD
)start
)
983 return AVIERR_BADPARAM
;
985 if (! This
->bDecompress
) {
986 /* audio like data -- sample-based */
989 return AVIERR_OK
; /* nothing at all or already done */
991 if (FAILED(AVIFILE_FindStreamInTable(This
, start
, &stream
,
992 &streamPos
, &streamNr
, FALSE
)))
995 /* limit to end of the stream */
997 if (streamPos
+ count
> EditStreamEnd(This
, streamNr
))
998 count
= EditStreamEnd(This
, streamNr
) - streamPos
;
1000 hr
= IAVIStream_Read(stream
, streamPos
, count
, buffer
, buffersize
,
1001 &readBytes
, &readSamples
);
1004 if (readBytes
== 0 && readSamples
== 0 && count
!= 0)
1005 return AVIERR_FILEREAD
; /* for bad stream implementations */
1007 if (samplesread
!= NULL
)
1008 *samplesread
+= readSamples
;
1009 if (bytesread
!= NULL
)
1010 *bytesread
+= readBytes
;
1011 if (buffer
!= NULL
) {
1012 buffer
= ((LPBYTE
)buffer
)+readBytes
;
1013 buffersize
-= readBytes
;
1017 } while (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
> start
);
1019 /* video like data -- frame-based */
1020 LPBITMAPINFOHEADER lp
;
1025 if (FAILED(AVIFILE_FindStreamInTable(This
, start
, &stream
,
1026 &streamPos
, &streamNr
, FALSE
)))
1027 return AVIERR_ERROR
;
1029 lp
= AVIFILE_ReadFrame(This
, stream
, streamPos
);
1031 return AVIERR_ERROR
;
1033 if (buffer
!= NULL
) {
1034 /* need size of format to skip */
1035 if (lp
->biBitCount
<= 8) {
1036 count
= lp
->biClrUsed
> 0 ? lp
->biClrUsed
: 1 << lp
->biBitCount
;
1037 count
*= sizeof(RGBQUAD
);
1040 count
+= lp
->biSize
;
1042 if (buffersize
< lp
->biSizeImage
)
1043 return AVIERR_BUFFERTOOSMALL
;
1044 memcpy(buffer
, (LPBYTE
)lp
+ count
, lp
->biSizeImage
);
1047 if (bytesread
!= NULL
)
1048 *bytesread
= lp
->biSizeImage
;
1049 if (samplesread
!= NULL
)
1056 static HRESULT WINAPI
IEditAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,
1057 LONG samples
,LPVOID buffer
,
1058 LONG buffersize
,DWORD flags
,
1059 LONG
*sampwritten
,LONG
*byteswritten
)
1061 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n",iface
,start
,samples
,buffer
,
1062 buffersize
,flags
,sampwritten
,byteswritten
);
1064 /* be sure return parameters have correct values */
1065 if (sampwritten
!= NULL
)
1067 if (byteswritten
!= NULL
)
1070 return AVIERR_UNSUPPORTED
;
1073 static HRESULT WINAPI
IEditAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,
1076 ICOM_THIS(IEditAVIStreamImpl
,iface
);
1078 TRACE("(%p,%ld,%ld)\n",iface
,start
,samples
);
1080 return IAVIEditStream_Cut((IAVIEditStream
*)This
->pae
,&start
,&samples
,NULL
);
1083 static HRESULT WINAPI
IEditAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,
1084 LPVOID lp
,LONG
*lpread
)
1086 IAVIEditStreamImpl
* const This
= ((IEditAVIStreamImpl
* const)iface
)->pae
;
1089 TRACE("(%p,0x%08lX,%p,%p)\n",iface
,fcc
,lp
,lpread
);
1091 /* check parameters */
1092 if (lp
== NULL
|| lpread
== NULL
)
1093 return AVIERR_BADPARAM
;
1095 /* simply ask every stream and return the first block found */
1096 for (n
= 0; n
< This
->nStreams
; n
++) {
1097 HRESULT hr
= IAVIStream_ReadData(This
->pStreams
[n
].pStream
,fcc
,lp
,lpread
);
1104 return AVIERR_NODATA
;
1107 static HRESULT WINAPI
IEditAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,
1108 LPVOID lp
,LONG size
)
1110 TRACE("(%p,0x%08lX,%p,%ld)\n",iface
,fcc
,lp
,size
);
1112 return AVIERR_UNSUPPORTED
;
1115 static HRESULT WINAPI
IEditAVIStream_fnSetInfo(IAVIStream
*iface
,
1116 AVISTREAMINFOW
*info
,LONG len
)
1118 ICOM_THIS(IEditAVIStreamImpl
,iface
);
1120 TRACE("(%p,%p,%ld)\n",iface
,info
,len
);
1122 return IAVIEditStream_SetInfo((IAVIEditStream
*)This
->pae
,info
,len
);
1125 static HRESULT WINAPI
IEditStreamInternal_fnQueryInterface(IEditStreamInternal
*iface
,REFIID refiid
,LPVOID
*obj
)
1127 ICOM_THIS(IEditStreamInternalImpl
,iface
);
1129 assert(This
->pae
!= NULL
);
1131 return IAVIEditStream_QueryInterface((IAVIEditStream
*)This
->pae
, refiid
, obj
);
1134 static ULONG WINAPI
IEditStreamInternal_fnAddRef(IEditStreamInternal
*iface
)
1136 ICOM_THIS(IEditStreamInternalImpl
,iface
);
1138 assert(This
->pae
!= NULL
);
1140 return IAVIEditStream_AddRef((IAVIEditStream
*)This
->pae
);
1143 static ULONG WINAPI
IEditStreamInternal_fnRelease(IEditStreamInternal
*iface
)
1145 ICOM_THIS(IEditStreamInternalImpl
,iface
);
1147 assert(This
->pae
!= NULL
);
1149 return IAVIEditStream_Release((IAVIEditStream
*)This
->pae
);
1152 static HRESULT WINAPI
IEditStreamInternal_fnGetEditStreamImpl(IEditStreamInternal
*iface
,LPVOID
*ppimpl
)
1154 ICOM_THIS(IEditStreamInternalImpl
,iface
);
1156 TRACE("(%p,%p) -> %p\n", iface
, ppimpl
, This
->pae
);
1158 assert(This
->pae
!= NULL
);
1159 assert(ppimpl
!= NULL
);
1161 *ppimpl
= This
->pae
;