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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "avifile_private.h"
23 /***********************************************************************/
25 /* internal interface to get access to table of stream in an editable stream */
27 DEFINE_AVIGUID(IID_IEditStreamInternal
, 0x0002000A,0,0);
29 typedef struct _EditStreamTable
{
30 PAVISTREAM pStream
; /* stream which contains the data */
31 DWORD dwStart
; /* where starts the part which is also our */
32 DWORD dwLength
; /* how many is also in this stream */
35 #define EditStreamEnd(This,streamNr) ((This)->pStreams[streamNr].dwStart + \
36 (This)->pStreams[streamNr].dwLength)
38 typedef struct _IAVIEditStreamImpl IAVIEditStreamImpl
;
40 struct _IAVIEditStreamImpl
{
41 IAVIEditStream IAVIEditStream_iface
;
42 IAVIStream IAVIStream_iface
;
48 EditStreamTable
*pStreams
;
49 DWORD nStreams
; /* current fill level of pStreams table */
50 DWORD nTableSize
; /* size of pStreams table */
53 PAVISTREAM pCurStream
;
54 PGETFRAME pg
; /* IGetFrame for pCurStream */
55 LPBITMAPINFOHEADER lpFrame
; /* frame of pCurStream */
58 static inline IAVIEditStreamImpl
*impl_from_IAVIEditStream(IAVIEditStream
*iface
)
60 return CONTAINING_RECORD(iface
, IAVIEditStreamImpl
, IAVIEditStream_iface
);
63 static inline IAVIEditStreamImpl
*impl_from_IAVIStream(IAVIStream
*iface
)
65 return CONTAINING_RECORD(iface
, IAVIEditStreamImpl
, IAVIStream_iface
);
68 /***********************************************************************/
70 static HRESULT
AVIFILE_FindStreamInTable(IAVIEditStreamImpl
* const This
,
71 DWORD pos
,PAVISTREAM
*ppStream
,
73 DWORD
* streamNr
,BOOL bFindSample
)
77 TRACE("(%p,%u,%p,%p,%p,%d)\n",This
,pos
,ppStream
,streamPos
,
78 streamNr
,bFindSample
);
80 if (pos
< This
->sInfo
.dwStart
)
81 return AVIERR_BADPARAM
;
83 pos
-= This
->sInfo
.dwStart
;
84 for (n
= 0; n
< This
->nStreams
; n
++) {
85 if (pos
< This
->pStreams
[n
].dwLength
) {
86 *ppStream
= This
->pStreams
[n
].pStream
;
87 *streamPos
= This
->pStreams
[n
].dwStart
+ pos
;
93 pos
-= This
->pStreams
[n
].dwLength
;
95 if (pos
== 0 && bFindSample
) {
96 *ppStream
= This
->pStreams
[--n
].pStream
;
97 *streamPos
= EditStreamEnd(This
, n
);
101 TRACE(" -- pos=0 && b=1 -> (%p,%u,%u)\n",*ppStream
, *streamPos
, n
);
106 if (streamNr
!= NULL
)
109 TRACE(" -> ERROR (NULL,0,0)\n");
110 return AVIERR_BADPARAM
;
114 static LPVOID
AVIFILE_ReadFrame(IAVIEditStreamImpl
* const This
,
115 PAVISTREAM pstream
, LONG pos
)
119 TRACE("(%p,%p,%d)\n",This
,pstream
,pos
);
124 /* if stream changes make sure that only palette changes */
125 if (This
->pCurStream
!= pstream
) {
126 pg
= AVIStreamGetFrameOpen(pstream
, NULL
);
129 if (This
->pg
!= NULL
) {
130 if (IGetFrame_SetFormat(pg
, This
->lpFrame
, NULL
, 0, 0, -1, -1) != S_OK
) {
131 AVIStreamGetFrameClose(pg
);
132 ERR(": IGetFrame_SetFormat failed\n");
135 AVIStreamGetFrameClose(This
->pg
);
138 This
->pCurStream
= pstream
;
141 /* now get the decompressed frame */
142 This
->lpFrame
= AVIStreamGetFrame(This
->pg
, pos
);
143 if (This
->lpFrame
!= NULL
)
144 This
->sInfo
.dwSuggestedBufferSize
= This
->lpFrame
->biSizeImage
;
146 return This
->lpFrame
;
149 static HRESULT
AVIFILE_RemoveStream(IAVIEditStreamImpl
* const This
, DWORD nr
)
151 assert(This
!= NULL
);
152 assert(nr
< This
->nStreams
);
155 IAVIStream_Release(This
->pStreams
[nr
].pStream
);
157 if (This
->nStreams
- nr
> 0) {
158 memmove(This
->pStreams
+ nr
, This
->pStreams
+ nr
+ 1,
159 (This
->nStreams
- nr
) * sizeof(EditStreamTable
));
161 This
->pStreams
[This
->nStreams
].pStream
= NULL
;
162 This
->pStreams
[This
->nStreams
].dwStart
= 0;
163 This
->pStreams
[This
->nStreams
].dwLength
= 0;
165 /* try to merge the part before the deleted one and the one after it */
166 if (0 < nr
&& 0 < This
->nStreams
&&
167 This
->pStreams
[nr
- 1].pStream
== This
->pStreams
[nr
].pStream
) {
168 if (EditStreamEnd(This
, nr
- 1) == This
->pStreams
[nr
].dwStart
) {
169 This
->pStreams
[nr
- 1].dwLength
+= This
->pStreams
[nr
].dwLength
;
170 return AVIFILE_RemoveStream(This
, nr
);
177 static BOOL
AVIFILE_FormatsEqual(PAVISTREAM avi1
, PAVISTREAM avi2
)
179 LPVOID fmt1
= NULL
, fmt2
= NULL
;
180 LONG size1
, size2
, start1
, start2
;
183 assert(avi1
!= NULL
&& avi2
!= NULL
);
185 /* get stream starts and check format sizes */
186 start1
= AVIStreamStart(avi1
);
187 start2
= AVIStreamStart(avi2
);
188 if (FAILED(AVIStreamFormatSize(avi1
, start1
, &size1
)))
190 if (FAILED(AVIStreamFormatSize(avi2
, start2
, &size2
)))
195 /* sizes match, now get formats and compare them */
196 fmt1
= HeapAlloc(GetProcessHeap(), 0, size1
);
199 if (SUCCEEDED(AVIStreamReadFormat(avi1
, start1
, fmt1
, &size1
))) {
200 fmt2
= HeapAlloc(GetProcessHeap(), 0, size1
);
202 if (SUCCEEDED(AVIStreamReadFormat(avi2
, start2
, fmt2
, &size1
)))
203 status
= (memcmp(fmt1
, fmt2
, size1
) == 0);
207 HeapFree(GetProcessHeap(), 0, fmt2
);
208 HeapFree(GetProcessHeap(), 0, fmt1
);
213 /***********************************************************************/
215 static HRESULT WINAPI
IAVIEditStream_fnQueryInterface(IAVIEditStream
*iface
,REFIID refiid
,LPVOID
*obj
)
217 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
219 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
221 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
222 IsEqualGUID(&IID_IAVIEditStream
, refiid
) ||
223 IsEqualGUID(&IID_IEditStreamInternal
, refiid
)) {
225 IAVIEditStream_AddRef(iface
);
228 } else if (IsEqualGUID(&IID_IAVIStream
, refiid
)) {
229 *obj
= &This
->IAVIStream_iface
;
230 IAVIEditStream_AddRef(iface
);
235 return E_NOINTERFACE
;
238 static ULONG WINAPI
IAVIEditStream_fnAddRef(IAVIEditStream
*iface
)
240 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
241 ULONG ref
= InterlockedIncrement(&This
->ref
);
243 TRACE("(%p) -> %d\n", iface
, ref
);
248 static ULONG WINAPI
IAVIEditStream_fnRelease(IAVIEditStream
*iface
)
250 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
252 ULONG ref
= InterlockedDecrement(&This
->ref
);
254 TRACE("(%p) -> %d\n", iface
, ref
);
258 if (This
->pg
!= NULL
)
259 AVIStreamGetFrameClose(This
->pg
);
260 if (This
->pStreams
!= NULL
) {
261 for (i
= 0; i
< This
->nStreams
; i
++) {
262 if (This
->pStreams
[i
].pStream
!= NULL
)
263 IAVIStream_Release(This
->pStreams
[i
].pStream
);
265 HeapFree(GetProcessHeap(), 0, This
->pStreams
);
268 HeapFree(GetProcessHeap(), 0, This
);
274 static HRESULT WINAPI
IAVIEditStream_fnCut(IAVIEditStream
*iface
,LONG
*plStart
,
275 LONG
*plLength
,PAVISTREAM
*ppResult
)
277 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
279 DWORD start
, len
, streamPos
, streamNr
;
282 TRACE("(%p,%p,%p,%p)\n",iface
,plStart
,plLength
,ppResult
);
284 if (ppResult
!= NULL
)
286 if (plStart
== NULL
|| plLength
== NULL
|| *plStart
< 0)
287 return AVIERR_BADPARAM
;
289 /* if asked for cut part copy it before deleting */
290 if (ppResult
!= NULL
) {
291 hr
= IAVIEditStream_Copy(iface
, plStart
, plLength
, ppResult
);
299 /* now delete the requested part */
301 hr
= AVIFILE_FindStreamInTable(This
, start
, &stream
,
302 &streamPos
, &streamNr
, FALSE
);
305 if (This
->pStreams
[streamNr
].dwStart
== streamPos
) {
306 /* deleting from start of part */
307 if (len
< This
->pStreams
[streamNr
].dwLength
) {
309 This
->pStreams
[streamNr
].dwStart
+= len
;
310 This
->pStreams
[streamNr
].dwLength
-= len
;
311 This
->sInfo
.dwLength
-= len
;
314 /* we must return decompressed data now */
315 This
->bDecompress
= TRUE
;
317 /* deleting hole part */
318 len
-= This
->pStreams
[streamNr
].dwLength
;
319 AVIFILE_RemoveStream(This
,streamNr
);
321 } else if (EditStreamEnd(This
, streamNr
) <= streamPos
+ len
) {
322 /* deleting at end of a part */
323 DWORD count
= EditStreamEnd(This
, streamNr
) - streamPos
;
324 This
->sInfo
.dwLength
-= count
;
326 This
->pStreams
[streamNr
].dwLength
=
327 streamPos
- This
->pStreams
[streamNr
].dwStart
;
330 if (This
->nStreams
+ 1 >= This
->nTableSize
) {
331 This
->pStreams
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->pStreams
,
332 (This
->nTableSize
+ 32) * sizeof(EditStreamTable
));
333 if (This
->pStreams
== NULL
)
334 return AVIERR_MEMORY
;
335 This
->nTableSize
+= 32;
337 memmove(This
->pStreams
+ streamNr
+ 1, This
->pStreams
+ streamNr
,
338 (This
->nStreams
- streamNr
) * sizeof(EditStreamTable
));
341 IAVIStream_AddRef(This
->pStreams
[streamNr
+ 1].pStream
);
342 This
->pStreams
[streamNr
+ 1].dwStart
= streamPos
+ len
;
343 This
->pStreams
[streamNr
+ 1].dwLength
=
344 EditStreamEnd(This
, streamNr
) - This
->pStreams
[streamNr
+ 1].dwStart
;
346 This
->pStreams
[streamNr
].dwLength
=
347 streamPos
- This
->pStreams
[streamNr
].dwStart
;
348 This
->sInfo
.dwLength
-= len
;
353 This
->sInfo
.dwEditCount
++;
358 static HRESULT WINAPI
IAVIEditStream_fnCopy(IAVIEditStream
*iface
,LONG
*plStart
,
359 LONG
*plLength
,PAVISTREAM
*ppResult
)
361 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
362 IAVIEditStreamImpl
* pEdit
;
366 TRACE("(%p,%p,%p,%p)\n",iface
,plStart
,plLength
,ppResult
);
368 if (ppResult
== NULL
)
369 return AVIERR_BADPARAM
;
371 if (plStart
== NULL
|| plLength
== NULL
|| *plStart
< 0 || *plLength
< 0)
372 return AVIERR_BADPARAM
;
375 if (*(LPDWORD
)plLength
> This
->sInfo
.dwLength
)
376 *(LPDWORD
)plLength
= This
->sInfo
.dwLength
;
377 if (*(LPDWORD
)plStart
< This
->sInfo
.dwStart
) {
378 *(LPDWORD
)plLength
-= This
->sInfo
.dwStart
- *(LPDWORD
)plStart
;
379 *(LPDWORD
)plStart
= This
->sInfo
.dwStart
;
381 return AVIERR_BADPARAM
;
383 if (*(LPDWORD
)plStart
+ *(LPDWORD
)plLength
> This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
)
384 *(LPDWORD
)plLength
= This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
-
387 pEdit
= (IAVIEditStreamImpl
*)AVIFILE_CreateEditStream(NULL
);
389 return AVIERR_MEMORY
;
391 hr
= IAVIEditStream_Paste((PAVIEDITSTREAM
)pEdit
, &start
, plLength
, &This
->IAVIStream_iface
,
392 *plStart
, *plStart
+ *plLength
);
395 IAVIEditStream_Release((PAVIEDITSTREAM
)pEdit
);
397 *ppResult
= &This
->IAVIStream_iface
;
402 static HRESULT WINAPI
IAVIEditStream_fnPaste(IAVIEditStream
*iface
,LONG
*plStart
,
403 LONG
*plLength
,PAVISTREAM pSource
,
404 LONG lStart
,LONG lLength
)
406 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
407 AVISTREAMINFOW srcInfo
;
408 IAVIEditStreamImpl
*pEdit
= NULL
;
410 DWORD startPos
, endPos
, streamNr
, nStreams
;
413 TRACE("(%p,%p,%p,%p,%d,%d)\n",iface
,plStart
,plLength
,
414 pSource
,lStart
,lLength
);
417 return AVIERR_BADHANDLE
;
418 if (plStart
== NULL
|| *plStart
< 0)
419 return AVIERR_BADPARAM
;
420 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< *plStart
)
421 return AVIERR_BADPARAM
; /* Can't paste with holes */
422 if (FAILED(IAVIStream_Info(pSource
, &srcInfo
, sizeof(srcInfo
))))
424 if (lStart
< srcInfo
.dwStart
|| lStart
>= srcInfo
.dwStart
+ srcInfo
.dwLength
)
425 return AVIERR_BADPARAM
;
426 if (This
->sInfo
.fccType
== 0) {
427 /* This stream is empty */
428 IAVIStream_Info(pSource
, &This
->sInfo
, sizeof(This
->sInfo
));
429 This
->sInfo
.dwStart
= *plStart
;
430 This
->sInfo
.dwLength
= 0;
432 if (This
->sInfo
.fccType
!= srcInfo
.fccType
)
433 return AVIERR_UNSUPPORTED
; /* different stream types */
434 if (lLength
== -1) /* Copy the hole stream */
435 lLength
= srcInfo
.dwLength
;
436 if (lStart
+ lLength
> srcInfo
.dwStart
+ srcInfo
.dwLength
)
437 lLength
= srcInfo
.dwStart
+ srcInfo
.dwLength
- lStart
;
438 if (lLength
+ *plStart
>= 0x80000000)
439 return AVIERR_MEMORY
;
441 /* streamtype specific tests */
442 if (srcInfo
.fccType
== streamtypeVIDEO
) {
445 size
= srcInfo
.rcFrame
.right
- srcInfo
.rcFrame
.left
;
446 if (size
!= This
->sInfo
.rcFrame
.right
- This
->sInfo
.rcFrame
.left
)
447 return AVIERR_UNSUPPORTED
; /* FIXME: Can't GetFrame convert it? */
448 size
= srcInfo
.rcFrame
.bottom
- srcInfo
.rcFrame
.top
;
449 if (size
!= This
->sInfo
.rcFrame
.bottom
- This
->sInfo
.rcFrame
.top
)
450 return AVIERR_UNSUPPORTED
; /* FIXME: Can't GetFrame convert it? */
451 } else if (srcInfo
.fccType
== streamtypeAUDIO
) {
452 if (!AVIFILE_FormatsEqual(&This
->IAVIStream_iface
, pSource
))
453 return AVIERR_UNSUPPORTED
;
455 /* FIXME: streamtypeMIDI and streamtypeTEXT */
456 return AVIERR_UNSUPPORTED
;
459 /* try to get an IEditStreamInternal interface */
460 if (SUCCEEDED(IAVIStream_QueryInterface(pSource
, &IID_IEditStreamInternal
, (LPVOID
*)&pEdit
)))
461 IAVIEditStream_Release(&pEdit
->IAVIEditStream_iface
); /* pSource holds a reference */
463 /* for video must check for change of format */
464 if (This
->sInfo
.fccType
== streamtypeVIDEO
) {
465 if (! This
->bDecompress
) {
466 /* Need to decompress if any of the following conditions matches:
467 * - pSource is an editable stream which decompresses
468 * - the nearest keyframe of pSource isn't lStart
469 * - the nearest keyframe of this stream isn't *plStart
470 * - the format of pSource doesn't match this one
472 if ((pEdit
!= NULL
&& pEdit
->bDecompress
) ||
473 AVIStreamNearestKeyFrame(pSource
, lStart
) != lStart
||
474 AVIStreamNearestKeyFrame(&This
->IAVIStream_iface
, *plStart
) != *plStart
||
475 (This
->nStreams
> 0 && !AVIFILE_FormatsEqual(&This
->IAVIStream_iface
, pSource
))) {
476 /* Use first stream part to get format to convert everything to */
477 AVIFILE_ReadFrame(This
, This
->pStreams
[0].pStream
,
478 This
->pStreams
[0].dwStart
);
480 /* Check if we could convert the source streams to the desired format... */
482 if (FAILED(AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
,
483 &startPos
, &streamNr
, TRUE
)))
484 return AVIERR_INTERNAL
;
485 for (n
= lStart
; n
< lStart
+ lLength
; streamNr
++) {
486 if (AVIFILE_ReadFrame(This
, pEdit
->pStreams
[streamNr
].pStream
, startPos
) == NULL
)
487 return AVIERR_BADFORMAT
;
488 startPos
= pEdit
->pStreams
[streamNr
].dwStart
;
489 n
+= pEdit
->pStreams
[streamNr
].dwLength
;
491 } else if (AVIFILE_ReadFrame(This
, pSource
, lStart
) == NULL
)
492 return AVIERR_BADFORMAT
;
494 This
->bDecompress
= TRUE
;
495 This
->sInfo
.fccHandler
= 0;
497 } else if (AVIFILE_ReadFrame(This
, pSource
, lStart
) == NULL
)
498 return AVIERR_BADFORMAT
; /* Can't convert source to own format */
499 } /* FIXME: something special for the other formats? */
501 /* Make sure we have enough memory for parts */
505 AVIFILE_FindStreamInTable(pEdit
, lStart
+ lLength
, &pStream
,
506 &endPos
, &nLastStream
, TRUE
);
507 AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
,
508 &startPos
, &streamNr
, FALSE
);
509 if (nLastStream
== streamNr
)
512 nStreams
= nLastStream
- streamNr
;
515 if (This
->nStreams
+ nStreams
+ 1 > This
->nTableSize
) {
516 n
= This
->nStreams
+ nStreams
+ 33;
518 This
->pStreams
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->pStreams
, n
* sizeof(EditStreamTable
));
519 if (This
->pStreams
== NULL
)
520 return AVIERR_MEMORY
;
521 This
->nTableSize
= n
;
524 if (plLength
!= NULL
)
527 /* now do the real work */
528 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
> *plStart
) {
529 AVIFILE_FindStreamInTable(This
, *plStart
, &pStream
,
530 &startPos
, &streamNr
, FALSE
);
531 if (startPos
!= This
->pStreams
[streamNr
].dwStart
) {
532 /* split stream streamNr at startPos */
533 memmove(This
->pStreams
+ streamNr
+ nStreams
+ 1,
534 This
->pStreams
+ streamNr
,
535 (This
->nStreams
+ nStreams
- streamNr
+ 1) * sizeof(EditStreamTable
));
537 This
->pStreams
[streamNr
+ 2].dwLength
=
538 EditStreamEnd(This
, streamNr
+ 2) - startPos
;
539 This
->pStreams
[streamNr
+ 2].dwStart
= startPos
;
540 This
->pStreams
[streamNr
].dwLength
=
541 startPos
- This
->pStreams
[streamNr
].dwStart
;
542 IAVIStream_AddRef(This
->pStreams
[streamNr
].pStream
);
545 /* insert before stream at streamNr */
546 memmove(This
->pStreams
+ streamNr
+ nStreams
, This
->pStreams
+ streamNr
,
547 (This
->nStreams
+ nStreams
- streamNr
) * sizeof(EditStreamTable
));
549 } else /* append the streams */
550 streamNr
= This
->nStreams
;
553 /* insert the parts of the editable stream instead of itself */
554 AVIFILE_FindStreamInTable(pEdit
, lStart
+ lLength
, &pStream
,
555 &endPos
, NULL
, FALSE
);
556 AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
, &startPos
, &n
, FALSE
);
558 memcpy(This
->pStreams
+ streamNr
, pEdit
->pStreams
+ n
,
559 nStreams
* sizeof(EditStreamTable
));
560 if (This
->pStreams
[streamNr
].dwStart
< startPos
) {
561 This
->pStreams
[streamNr
].dwLength
=
562 EditStreamEnd(This
, streamNr
) - startPos
;
563 This
->pStreams
[streamNr
].dwStart
= startPos
;
565 if (endPos
< EditStreamEnd(This
, streamNr
+ nStreams
))
566 This
->pStreams
[streamNr
+ nStreams
].dwLength
=
567 endPos
- This
->pStreams
[streamNr
+ nStreams
].dwStart
;
569 /* a simple stream */
570 This
->pStreams
[streamNr
].pStream
= pSource
;
571 This
->pStreams
[streamNr
].dwStart
= lStart
;
572 This
->pStreams
[streamNr
].dwLength
= lLength
;
575 for (n
= 0; n
< nStreams
; n
++) {
576 IAVIStream_AddRef(This
->pStreams
[streamNr
+ n
].pStream
);
577 if (0 < streamNr
+ n
&&
578 This
->pStreams
[streamNr
+ n
- 1].pStream
!= This
->pStreams
[streamNr
+ n
].pStream
) {
579 This
->sInfo
.dwFlags
|= AVISTREAMINFO_FORMATCHANGES
;
580 This
->sInfo
.dwFormatChangeCount
++;
583 This
->sInfo
.dwEditCount
++;
584 This
->sInfo
.dwLength
+= lLength
;
585 This
->nStreams
+= nStreams
;
590 static HRESULT WINAPI
IAVIEditStream_fnClone(IAVIEditStream
*iface
,
593 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
594 IAVIEditStreamImpl
* pEdit
;
597 TRACE("(%p,%p)\n",iface
,ppResult
);
599 if (ppResult
== NULL
)
600 return AVIERR_BADPARAM
;
603 pEdit
= (IAVIEditStreamImpl
*)AVIFILE_CreateEditStream(NULL
);
605 return AVIERR_MEMORY
;
606 if (This
->nStreams
> pEdit
->nTableSize
) {
607 pEdit
->pStreams
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, pEdit
->pStreams
,
608 This
->nStreams
* sizeof(EditStreamTable
));
609 if (pEdit
->pStreams
== NULL
)
610 return AVIERR_MEMORY
;
611 pEdit
->nTableSize
= This
->nStreams
;
613 pEdit
->nStreams
= This
->nStreams
;
614 memcpy(pEdit
->pStreams
, This
->pStreams
,
615 This
->nStreams
* sizeof(EditStreamTable
));
616 memcpy(&pEdit
->sInfo
,&This
->sInfo
,sizeof(This
->sInfo
));
617 for (i
= 0; i
< This
->nStreams
; i
++) {
618 if (pEdit
->pStreams
[i
].pStream
!= NULL
)
619 IAVIStream_AddRef(pEdit
->pStreams
[i
].pStream
);
622 *ppResult
= &This
->IAVIStream_iface
;
627 static HRESULT WINAPI
IAVIEditStream_fnSetInfo(IAVIEditStream
*iface
,
628 LPAVISTREAMINFOW asi
,LONG size
)
630 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
632 TRACE("(%p,%p,%d)\n",iface
,asi
,size
);
634 /* check parameters */
635 if (size
>= 0 && size
< sizeof(AVISTREAMINFOW
))
636 return AVIERR_BADSIZE
;
638 This
->sInfo
.wLanguage
= asi
->wLanguage
;
639 This
->sInfo
.wPriority
= asi
->wPriority
;
640 This
->sInfo
.dwStart
= asi
->dwStart
;
641 This
->sInfo
.dwRate
= asi
->dwRate
;
642 This
->sInfo
.dwScale
= asi
->dwScale
;
643 This
->sInfo
.dwQuality
= asi
->dwQuality
;
644 CopyRect(&This
->sInfo
.rcFrame
, &asi
->rcFrame
);
645 memcpy(This
->sInfo
.szName
, asi
->szName
, sizeof(asi
->szName
));
646 This
->sInfo
.dwEditCount
++;
651 static const struct IAVIEditStreamVtbl ieditstream
= {
652 IAVIEditStream_fnQueryInterface
,
653 IAVIEditStream_fnAddRef
,
654 IAVIEditStream_fnRelease
,
655 IAVIEditStream_fnCut
,
656 IAVIEditStream_fnCopy
,
657 IAVIEditStream_fnPaste
,
658 IAVIEditStream_fnClone
,
659 IAVIEditStream_fnSetInfo
662 static HRESULT WINAPI
IEditAVIStream_fnQueryInterface(IAVIStream
*iface
,
663 REFIID refiid
,LPVOID
*obj
)
665 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
666 return IAVIEditStream_QueryInterface(&This
->IAVIEditStream_iface
,refiid
,obj
);
669 static ULONG WINAPI
IEditAVIStream_fnAddRef(IAVIStream
*iface
)
671 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
672 return IAVIEditStream_AddRef(&This
->IAVIEditStream_iface
);
675 static ULONG WINAPI
IEditAVIStream_fnRelease(IAVIStream
*iface
)
677 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
678 return IAVIEditStream_Release(&This
->IAVIEditStream_iface
);
681 static HRESULT WINAPI
IEditAVIStream_fnCreate(IAVIStream
*iface
,
682 LPARAM lParam1
,LPARAM lParam2
)
684 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
689 if (This
->pStreams
== NULL
) {
690 This
->pStreams
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 256 * sizeof(EditStreamTable
));
691 if (This
->pStreams
== NULL
)
692 return AVIERR_MEMORY
;
693 This
->nTableSize
= 256;
697 IAVIStream_Info((PAVISTREAM
)lParam1
, &This
->sInfo
, sizeof(This
->sInfo
));
698 IAVIStream_AddRef((PAVISTREAM
)lParam1
);
699 This
->pStreams
[0].pStream
= (PAVISTREAM
)lParam1
;
700 This
->pStreams
[0].dwStart
= This
->sInfo
.dwStart
;
701 This
->pStreams
[0].dwLength
= This
->sInfo
.dwLength
;
707 static HRESULT WINAPI
IEditAVIStream_fnInfo(IAVIStream
*iface
,
708 AVISTREAMINFOW
*psi
,LONG size
)
710 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
712 TRACE("(%p,%p,%d)\n",iface
,psi
,size
);
715 return AVIERR_BADPARAM
;
717 return AVIERR_BADSIZE
;
719 if (This
->bDecompress
)
720 This
->sInfo
.fccHandler
= 0;
722 memcpy(psi
, &This
->sInfo
, min((DWORD
)size
, sizeof(This
->sInfo
)));
724 if ((DWORD
)size
< sizeof(This
->sInfo
))
725 return AVIERR_BUFFERTOOSMALL
;
729 static LONG WINAPI
IEditAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,
732 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
734 DWORD streamPos
, streamNr
;
736 TRACE("(%p,%d,0x%08X)\n",iface
,pos
,flags
);
738 if (flags
& FIND_FROM_START
)
739 pos
= (LONG
)This
->sInfo
.dwStart
;
741 /* outside of stream? */
742 if (pos
< (LONG
)This
->sInfo
.dwStart
||
743 (LONG
)This
->sInfo
.dwStart
+ (LONG
)This
->sInfo
.dwLength
<= pos
)
746 /* map our position to a stream and position in it */
747 if (AVIFILE_FindStreamInTable(This
, pos
, &stream
, &streamPos
,
748 &streamNr
, TRUE
) != S_OK
)
749 return -1; /* doesn't exist */
751 if (This
->bDecompress
) {
752 /* only one stream -- format changes only at start */
753 if (flags
& FIND_FORMAT
)
754 return (flags
& FIND_NEXT
? -1 : 0);
756 /* FIXME: map positions back to us */
757 return IAVIStream_FindSample(stream
, streamPos
, flags
);
759 /* assume change of format every frame */
764 static HRESULT WINAPI
IEditAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,
765 LPVOID format
,LONG
*fmtsize
)
767 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
768 LPBITMAPINFOHEADER lp
;
773 TRACE("(%p,%d,%p,%p)\n",iface
,pos
,format
,fmtsize
);
775 if (fmtsize
== NULL
|| pos
< This
->sInfo
.dwStart
||
776 This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
<= pos
)
777 return AVIERR_BADPARAM
;
779 /* find stream corresponding to position */
780 hr
= AVIFILE_FindStreamInTable(This
, pos
, &stream
, &n
, NULL
, FALSE
);
784 if (! This
->bDecompress
)
785 return IAVIStream_ReadFormat(stream
, n
, format
, fmtsize
);
787 lp
= AVIFILE_ReadFrame(This
, stream
, n
);
790 if (lp
->biBitCount
<= 8) {
791 n
= (lp
->biClrUsed
> 0 ? lp
->biClrUsed
: 1 << lp
->biBitCount
);
792 n
*= sizeof(RGBQUAD
);
797 memcpy(format
, lp
, min((LONG
)n
, *fmtsize
));
798 hr
= ((LONG
)n
> *fmtsize
? AVIERR_BUFFERTOOSMALL
: AVIERR_OK
);
804 static HRESULT WINAPI
IEditAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,
805 LPVOID format
,LONG formatsize
)
807 TRACE("(%p,%d,%p,%d)\n",iface
,pos
,format
,formatsize
);
809 return AVIERR_UNSUPPORTED
;
812 static HRESULT WINAPI
IEditAVIStream_fnRead(IAVIStream
*iface
,LONG start
,
813 LONG samples
,LPVOID buffer
,
814 LONG buffersize
,LONG
*bytesread
,
817 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
819 DWORD streamPos
, streamNr
;
820 LONG readBytes
, readSamples
, count
;
823 TRACE("(%p,%d,%d,%p,%d,%p,%p) -- 0x%08X\n",iface
,start
,samples
,
824 buffer
,buffersize
,bytesread
,samplesread
,This
->sInfo
.fccType
);
826 /* check parameters */
827 if (bytesread
!= NULL
)
829 if (samplesread
!= NULL
)
832 return AVIERR_BADSIZE
;
833 if ((DWORD
)start
< This
->sInfo
.dwStart
||
834 This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< (DWORD
)start
)
835 return AVIERR_BADPARAM
;
837 if (! This
->bDecompress
) {
838 /* audio like data -- sample-based */
841 return AVIERR_OK
; /* nothing at all or already done */
843 if (FAILED(AVIFILE_FindStreamInTable(This
, start
, &stream
,
844 &streamPos
, &streamNr
, FALSE
)))
847 /* limit to end of the stream */
849 if (streamPos
+ count
> EditStreamEnd(This
, streamNr
))
850 count
= EditStreamEnd(This
, streamNr
) - streamPos
;
852 hr
= IAVIStream_Read(stream
, streamPos
, count
, buffer
, buffersize
,
853 &readBytes
, &readSamples
);
856 if (readBytes
== 0 && readSamples
== 0 && count
!= 0)
857 return AVIERR_FILEREAD
; /* for bad stream implementations */
859 if (samplesread
!= NULL
)
860 *samplesread
+= readSamples
;
861 if (bytesread
!= NULL
)
862 *bytesread
+= readBytes
;
863 if (buffer
!= NULL
) {
864 buffer
= ((LPBYTE
)buffer
)+readBytes
;
865 buffersize
-= readBytes
;
869 } while (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
> start
);
871 /* video like data -- frame-based */
872 LPBITMAPINFOHEADER lp
;
877 if (FAILED(AVIFILE_FindStreamInTable(This
, start
, &stream
,
878 &streamPos
, &streamNr
, FALSE
)))
881 lp
= AVIFILE_ReadFrame(This
, stream
, streamPos
);
885 if (buffer
!= NULL
) {
886 /* need size of format to skip */
887 if (lp
->biBitCount
<= 8) {
888 count
= lp
->biClrUsed
> 0 ? lp
->biClrUsed
: 1 << lp
->biBitCount
;
889 count
*= sizeof(RGBQUAD
);
894 if (buffersize
< lp
->biSizeImage
)
895 return AVIERR_BUFFERTOOSMALL
;
896 memcpy(buffer
, (LPBYTE
)lp
+ count
, lp
->biSizeImage
);
899 if (bytesread
!= NULL
)
900 *bytesread
= lp
->biSizeImage
;
901 if (samplesread
!= NULL
)
908 static HRESULT WINAPI
IEditAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,
909 LONG samples
,LPVOID buffer
,
910 LONG buffersize
,DWORD flags
,
911 LONG
*sampwritten
,LONG
*byteswritten
)
913 TRACE("(%p,%d,%d,%p,%d,0x%08X,%p,%p)\n",iface
,start
,samples
,buffer
,
914 buffersize
,flags
,sampwritten
,byteswritten
);
916 /* be sure return parameters have correct values */
917 if (sampwritten
!= NULL
)
919 if (byteswritten
!= NULL
)
922 return AVIERR_UNSUPPORTED
;
925 static HRESULT WINAPI
IEditAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,
928 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
930 TRACE("(%p,%d,%d)\n",iface
,start
,samples
);
932 return IAVIEditStream_Cut(&This
->IAVIEditStream_iface
,&start
,&samples
,NULL
);
935 static HRESULT WINAPI
IEditAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,
936 LPVOID lp
,LONG
*lpread
)
938 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
941 TRACE("(%p,0x%08X,%p,%p)\n",iface
,fcc
,lp
,lpread
);
943 /* check parameters */
944 if (lp
== NULL
|| lpread
== NULL
)
945 return AVIERR_BADPARAM
;
947 /* simply ask every stream and return the first block found */
948 for (n
= 0; n
< This
->nStreams
; n
++) {
949 HRESULT hr
= IAVIStream_ReadData(This
->pStreams
[n
].pStream
,fcc
,lp
,lpread
);
956 return AVIERR_NODATA
;
959 static HRESULT WINAPI
IEditAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,
962 TRACE("(%p,0x%08X,%p,%d)\n",iface
,fcc
,lp
,size
);
964 return AVIERR_UNSUPPORTED
;
967 static HRESULT WINAPI
IEditAVIStream_fnSetInfo(IAVIStream
*iface
,
968 AVISTREAMINFOW
*info
,LONG len
)
970 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
972 TRACE("(%p,%p,%d)\n",iface
,info
,len
);
974 return IAVIEditStream_SetInfo(&This
->IAVIEditStream_iface
,info
,len
);
977 static const struct IAVIStreamVtbl ieditstast
= {
978 IEditAVIStream_fnQueryInterface
,
979 IEditAVIStream_fnAddRef
,
980 IEditAVIStream_fnRelease
,
981 IEditAVIStream_fnCreate
,
982 IEditAVIStream_fnInfo
,
983 IEditAVIStream_fnFindSample
,
984 IEditAVIStream_fnReadFormat
,
985 IEditAVIStream_fnSetFormat
,
986 IEditAVIStream_fnRead
,
987 IEditAVIStream_fnWrite
,
988 IEditAVIStream_fnDelete
,
989 IEditAVIStream_fnReadData
,
990 IEditAVIStream_fnWriteData
,
991 IEditAVIStream_fnSetInfo
994 PAVIEDITSTREAM
AVIFILE_CreateEditStream(PAVISTREAM pstream
)
996 IAVIEditStreamImpl
*pedit
= NULL
;
998 pedit
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAVIEditStreamImpl
));
1002 pedit
->IAVIEditStream_iface
.lpVtbl
= &ieditstream
;
1003 pedit
->IAVIStream_iface
.lpVtbl
= &ieditstast
;
1006 IAVIStream_Create(&pedit
->IAVIStream_iface
, (LPARAM
)pstream
, 0);
1008 return (PAVIEDITSTREAM
)pedit
;