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
30 #include "avifile_private.h"
31 #include "extrachunk.h"
33 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
38 /***********************************************************************/
40 /* internal interface to get access to table of stream in an editable stream */
42 DEFINE_AVIGUID(IID_IEditStreamInternal
, 0x0002000A,0,0);
44 typedef struct _EditStreamTable
{
45 PAVISTREAM pStream
; /* stream which contains the data */
46 DWORD dwStart
; /* where starts the part which is also our */
47 DWORD dwLength
; /* how many is also in this stream */
50 #define EditStreamEnd(This,streamNr) ((This)->pStreams[streamNr].dwStart + \
51 (This)->pStreams[streamNr].dwLength)
53 typedef struct _IAVIEditStreamImpl IAVIEditStreamImpl
;
55 struct _IAVIEditStreamImpl
{
56 IAVIEditStream IAVIEditStream_iface
;
57 IAVIStream IAVIStream_iface
;
63 EditStreamTable
*pStreams
;
64 DWORD nStreams
; /* current fill level of pStreams table */
65 DWORD nTableSize
; /* size of pStreams table */
68 PAVISTREAM pCurStream
;
69 PGETFRAME pg
; /* IGetFrame for pCurStream */
70 LPBITMAPINFOHEADER lpFrame
; /* frame of pCurStream */
73 static inline IAVIEditStreamImpl
*impl_from_IAVIEditStream(IAVIEditStream
*iface
)
75 return CONTAINING_RECORD(iface
, IAVIEditStreamImpl
, IAVIEditStream_iface
);
78 static inline IAVIEditStreamImpl
*impl_from_IAVIStream(IAVIStream
*iface
)
80 return CONTAINING_RECORD(iface
, IAVIEditStreamImpl
, IAVIStream_iface
);
83 /***********************************************************************/
85 static HRESULT
AVIFILE_FindStreamInTable(IAVIEditStreamImpl
* const This
,
86 DWORD pos
,PAVISTREAM
*ppStream
,
88 DWORD
* streamNr
,BOOL bFindSample
)
92 TRACE("(%p,%u,%p,%p,%p,%d)\n",This
,pos
,ppStream
,streamPos
,
93 streamNr
,bFindSample
);
95 if (pos
< This
->sInfo
.dwStart
)
96 return AVIERR_BADPARAM
;
98 pos
-= This
->sInfo
.dwStart
;
99 for (n
= 0; n
< This
->nStreams
; n
++) {
100 if (pos
< This
->pStreams
[n
].dwLength
) {
101 *ppStream
= This
->pStreams
[n
].pStream
;
102 *streamPos
= This
->pStreams
[n
].dwStart
+ pos
;
103 if (streamNr
!= NULL
)
108 pos
-= This
->pStreams
[n
].dwLength
;
110 if (pos
== 0 && bFindSample
) {
111 *ppStream
= This
->pStreams
[--n
].pStream
;
112 *streamPos
= EditStreamEnd(This
, n
);
113 if (streamNr
!= NULL
)
116 TRACE(" -- pos=0 && b=1 -> (%p,%u,%u)\n",*ppStream
, *streamPos
, n
);
121 if (streamNr
!= NULL
)
124 TRACE(" -> ERROR (NULL,0,0)\n");
125 return AVIERR_BADPARAM
;
129 static LPVOID
AVIFILE_ReadFrame(IAVIEditStreamImpl
* const This
,
130 PAVISTREAM pstream
, LONG pos
)
134 TRACE("(%p,%p,%d)\n",This
,pstream
,pos
);
139 /* if stream changes make sure that only palette changes */
140 if (This
->pCurStream
!= pstream
) {
141 pg
= AVIStreamGetFrameOpen(pstream
, NULL
);
144 if (This
->pg
!= NULL
) {
145 if (IGetFrame_SetFormat(pg
, This
->lpFrame
, NULL
, 0, 0, -1, -1) != S_OK
) {
146 AVIStreamGetFrameClose(pg
);
147 ERR(": IGetFrame_SetFormat failed\n");
150 AVIStreamGetFrameClose(This
->pg
);
153 This
->pCurStream
= pstream
;
156 /* now get the decompressed frame */
157 This
->lpFrame
= AVIStreamGetFrame(This
->pg
, pos
);
158 if (This
->lpFrame
!= NULL
)
159 This
->sInfo
.dwSuggestedBufferSize
= This
->lpFrame
->biSizeImage
;
161 return This
->lpFrame
;
164 static HRESULT
AVIFILE_RemoveStream(IAVIEditStreamImpl
* const This
, DWORD nr
)
166 assert(This
!= NULL
);
167 assert(nr
< This
->nStreams
);
170 IAVIStream_Release(This
->pStreams
[nr
].pStream
);
172 if (This
->nStreams
- nr
> 0) {
173 memmove(This
->pStreams
+ nr
, This
->pStreams
+ nr
+ 1,
174 (This
->nStreams
- nr
) * sizeof(EditStreamTable
));
176 This
->pStreams
[This
->nStreams
].pStream
= NULL
;
177 This
->pStreams
[This
->nStreams
].dwStart
= 0;
178 This
->pStreams
[This
->nStreams
].dwLength
= 0;
180 /* try to merge the part before the deleted one and the one after it */
181 if (0 < nr
&& 0 < This
->nStreams
&&
182 This
->pStreams
[nr
- 1].pStream
== This
->pStreams
[nr
].pStream
) {
183 if (EditStreamEnd(This
, nr
- 1) == This
->pStreams
[nr
].dwStart
) {
184 This
->pStreams
[nr
- 1].dwLength
+= This
->pStreams
[nr
].dwLength
;
185 return AVIFILE_RemoveStream(This
, nr
);
192 static BOOL
AVIFILE_FormatsEqual(PAVISTREAM avi1
, PAVISTREAM avi2
)
194 LPVOID fmt1
= NULL
, fmt2
= NULL
;
195 LONG size1
, size2
, start1
, start2
;
198 assert(avi1
!= NULL
&& avi2
!= NULL
);
200 /* get stream starts and check format sizes */
201 start1
= AVIStreamStart(avi1
);
202 start2
= AVIStreamStart(avi2
);
203 if (FAILED(AVIStreamFormatSize(avi1
, start1
, &size1
)))
205 if (FAILED(AVIStreamFormatSize(avi2
, start2
, &size2
)))
210 /* sizes match, now get formats and compare them */
211 fmt1
= HeapAlloc(GetProcessHeap(), 0, size1
);
214 if (SUCCEEDED(AVIStreamReadFormat(avi1
, start1
, fmt1
, &size1
))) {
215 fmt2
= HeapAlloc(GetProcessHeap(), 0, size1
);
217 if (SUCCEEDED(AVIStreamReadFormat(avi2
, start2
, fmt2
, &size1
)))
218 status
= (memcmp(fmt1
, fmt2
, size1
) == 0);
222 HeapFree(GetProcessHeap(), 0, fmt2
);
223 HeapFree(GetProcessHeap(), 0, fmt1
);
228 /***********************************************************************/
230 static HRESULT WINAPI
IAVIEditStream_fnQueryInterface(IAVIEditStream
*iface
,REFIID refiid
,LPVOID
*obj
)
232 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
234 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(refiid
), obj
);
236 if (IsEqualGUID(&IID_IUnknown
, refiid
) ||
237 IsEqualGUID(&IID_IAVIEditStream
, refiid
) ||
238 IsEqualGUID(&IID_IEditStreamInternal
, refiid
)) {
240 IAVIEditStream_AddRef(iface
);
243 } else if (IsEqualGUID(&IID_IAVIStream
, refiid
)) {
244 *obj
= &This
->IAVIStream_iface
;
245 IAVIEditStream_AddRef(iface
);
250 return E_NOINTERFACE
;
253 static ULONG WINAPI
IAVIEditStream_fnAddRef(IAVIEditStream
*iface
)
255 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
256 ULONG ref
= InterlockedIncrement(&This
->ref
);
258 TRACE("(%p) -> %d\n", iface
, ref
);
263 static ULONG WINAPI
IAVIEditStream_fnRelease(IAVIEditStream
*iface
)
265 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
267 ULONG ref
= InterlockedDecrement(&This
->ref
);
269 TRACE("(%p) -> %d\n", iface
, ref
);
273 if (This
->pg
!= NULL
)
274 AVIStreamGetFrameClose(This
->pg
);
275 if (This
->pStreams
!= NULL
) {
276 for (i
= 0; i
< This
->nStreams
; i
++) {
277 if (This
->pStreams
[i
].pStream
!= NULL
)
278 IAVIStream_Release(This
->pStreams
[i
].pStream
);
280 HeapFree(GetProcessHeap(), 0, This
->pStreams
);
283 HeapFree(GetProcessHeap(), 0, This
);
289 static HRESULT WINAPI
IAVIEditStream_fnCut(IAVIEditStream
*iface
,LONG
*plStart
,
290 LONG
*plLength
,PAVISTREAM
*ppResult
)
292 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
294 DWORD start
, len
, streamPos
, streamNr
;
297 TRACE("(%p,%p,%p,%p)\n",iface
,plStart
,plLength
,ppResult
);
299 if (ppResult
!= NULL
)
301 if (plStart
== NULL
|| plLength
== NULL
|| *plStart
< 0)
302 return AVIERR_BADPARAM
;
304 /* if asked for cut part copy it before deleting */
305 if (ppResult
!= NULL
) {
306 hr
= IAVIEditStream_Copy(iface
, plStart
, plLength
, ppResult
);
314 /* now delete the requested part */
316 hr
= AVIFILE_FindStreamInTable(This
, start
, &stream
,
317 &streamPos
, &streamNr
, FALSE
);
320 if (This
->pStreams
[streamNr
].dwStart
== streamPos
) {
321 /* deleting from start of part */
322 if (len
< This
->pStreams
[streamNr
].dwLength
) {
324 This
->pStreams
[streamNr
].dwStart
+= len
;
325 This
->pStreams
[streamNr
].dwLength
-= len
;
326 This
->sInfo
.dwLength
-= len
;
329 /* we must return decompressed data now */
330 This
->bDecompress
= TRUE
;
332 /* deleting hole part */
333 len
-= This
->pStreams
[streamNr
].dwLength
;
334 AVIFILE_RemoveStream(This
,streamNr
);
336 } else if (EditStreamEnd(This
, streamNr
) <= streamPos
+ len
) {
337 /* deleting at end of a part */
338 DWORD count
= EditStreamEnd(This
, streamNr
) - streamPos
;
339 This
->sInfo
.dwLength
-= count
;
341 This
->pStreams
[streamNr
].dwLength
=
342 streamPos
- This
->pStreams
[streamNr
].dwStart
;
345 if (This
->nStreams
+ 1 >= This
->nTableSize
) {
346 This
->pStreams
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->pStreams
,
347 (This
->nTableSize
+ 32) * sizeof(EditStreamTable
));
348 if (This
->pStreams
== NULL
)
349 return AVIERR_MEMORY
;
350 This
->nTableSize
+= 32;
352 memmove(This
->pStreams
+ streamNr
+ 1, This
->pStreams
+ streamNr
,
353 (This
->nStreams
- streamNr
) * sizeof(EditStreamTable
));
356 IAVIStream_AddRef(This
->pStreams
[streamNr
+ 1].pStream
);
357 This
->pStreams
[streamNr
+ 1].dwStart
= streamPos
+ len
;
358 This
->pStreams
[streamNr
+ 1].dwLength
=
359 EditStreamEnd(This
, streamNr
) - This
->pStreams
[streamNr
+ 1].dwStart
;
361 This
->pStreams
[streamNr
].dwLength
=
362 streamPos
- This
->pStreams
[streamNr
].dwStart
;
363 This
->sInfo
.dwLength
-= len
;
368 This
->sInfo
.dwEditCount
++;
373 static HRESULT WINAPI
IAVIEditStream_fnCopy(IAVIEditStream
*iface
,LONG
*plStart
,
374 LONG
*plLength
,PAVISTREAM
*ppResult
)
376 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
377 IAVIEditStreamImpl
* pEdit
;
381 TRACE("(%p,%p,%p,%p)\n",iface
,plStart
,plLength
,ppResult
);
383 if (ppResult
== NULL
)
384 return AVIERR_BADPARAM
;
386 if (plStart
== NULL
|| plLength
== NULL
|| *plStart
< 0 || *plLength
< 0)
387 return AVIERR_BADPARAM
;
390 if (*(LPDWORD
)plLength
> This
->sInfo
.dwLength
)
391 *(LPDWORD
)plLength
= This
->sInfo
.dwLength
;
392 if (*(LPDWORD
)plStart
< This
->sInfo
.dwStart
) {
393 *(LPDWORD
)plLength
-= This
->sInfo
.dwStart
- *(LPDWORD
)plStart
;
394 *(LPDWORD
)plStart
= This
->sInfo
.dwStart
;
396 return AVIERR_BADPARAM
;
398 if (*(LPDWORD
)plStart
+ *(LPDWORD
)plLength
> This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
)
399 *(LPDWORD
)plLength
= This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
-
402 pEdit
= (IAVIEditStreamImpl
*)AVIFILE_CreateEditStream(NULL
);
404 return AVIERR_MEMORY
;
406 hr
= IAVIEditStream_Paste((PAVIEDITSTREAM
)pEdit
, &start
, plLength
, &This
->IAVIStream_iface
,
407 *plStart
, *plStart
+ *plLength
);
410 IAVIEditStream_Release((PAVIEDITSTREAM
)pEdit
);
412 *ppResult
= &This
->IAVIStream_iface
;
417 static HRESULT WINAPI
IAVIEditStream_fnPaste(IAVIEditStream
*iface
,LONG
*plStart
,
418 LONG
*plLength
,PAVISTREAM pSource
,
419 LONG lStart
,LONG lLength
)
421 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
422 AVISTREAMINFOW srcInfo
;
423 IAVIEditStreamImpl
*pEdit
= NULL
;
425 DWORD startPos
, endPos
, streamNr
, nStreams
;
428 TRACE("(%p,%p,%p,%p,%d,%d)\n",iface
,plStart
,plLength
,
429 pSource
,lStart
,lLength
);
432 return AVIERR_BADHANDLE
;
433 if (plStart
== NULL
|| *plStart
< 0)
434 return AVIERR_BADPARAM
;
435 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< *plStart
)
436 return AVIERR_BADPARAM
; /* Can't paste with holes */
437 if (FAILED(IAVIStream_Info(pSource
, &srcInfo
, sizeof(srcInfo
))))
439 if (lStart
< srcInfo
.dwStart
|| lStart
>= srcInfo
.dwStart
+ srcInfo
.dwLength
)
440 return AVIERR_BADPARAM
;
441 if (This
->sInfo
.fccType
== 0) {
442 /* This stream is empty */
443 IAVIStream_Info(pSource
, &This
->sInfo
, sizeof(This
->sInfo
));
444 This
->sInfo
.dwStart
= *plStart
;
445 This
->sInfo
.dwLength
= 0;
447 if (This
->sInfo
.fccType
!= srcInfo
.fccType
)
448 return AVIERR_UNSUPPORTED
; /* different stream types */
449 if (lLength
== -1) /* Copy the hole stream */
450 lLength
= srcInfo
.dwLength
;
451 if (lStart
+ lLength
> srcInfo
.dwStart
+ srcInfo
.dwLength
)
452 lLength
= srcInfo
.dwStart
+ srcInfo
.dwLength
- lStart
;
453 if (lLength
+ *plStart
>= 0x80000000)
454 return AVIERR_MEMORY
;
456 /* streamtype specific tests */
457 if (srcInfo
.fccType
== streamtypeVIDEO
) {
460 size
= srcInfo
.rcFrame
.right
- srcInfo
.rcFrame
.left
;
461 if (size
!= This
->sInfo
.rcFrame
.right
- This
->sInfo
.rcFrame
.left
)
462 return AVIERR_UNSUPPORTED
; /* FIXME: Can't GetFrame convert it? */
463 size
= srcInfo
.rcFrame
.bottom
- srcInfo
.rcFrame
.top
;
464 if (size
!= This
->sInfo
.rcFrame
.bottom
- This
->sInfo
.rcFrame
.top
)
465 return AVIERR_UNSUPPORTED
; /* FIXME: Can't GetFrame convert it? */
466 } else if (srcInfo
.fccType
== streamtypeAUDIO
) {
467 if (!AVIFILE_FormatsEqual(&This
->IAVIStream_iface
, pSource
))
468 return AVIERR_UNSUPPORTED
;
470 /* FIXME: streamtypeMIDI and streamtypeTEXT */
471 return AVIERR_UNSUPPORTED
;
474 /* try to get an IEditStreamInternal interface */
475 if (SUCCEEDED(IAVIStream_QueryInterface(pSource
, &IID_IEditStreamInternal
, (LPVOID
*)&pEdit
)))
476 IAVIEditStream_Release(&pEdit
->IAVIEditStream_iface
); /* pSource holds a reference */
478 /* for video must check for change of format */
479 if (This
->sInfo
.fccType
== streamtypeVIDEO
) {
480 if (! This
->bDecompress
) {
481 /* Need to decompress if any of the following conditions matches:
482 * - pSource is an editable stream which decompresses
483 * - the nearest keyframe of pSource isn't lStart
484 * - the nearest keyframe of this stream isn't *plStart
485 * - the format of pSource doesn't match this one
487 if ((pEdit
!= NULL
&& pEdit
->bDecompress
) ||
488 AVIStreamNearestKeyFrame(pSource
, lStart
) != lStart
||
489 AVIStreamNearestKeyFrame(&This
->IAVIStream_iface
, *plStart
) != *plStart
||
490 (This
->nStreams
> 0 && !AVIFILE_FormatsEqual(&This
->IAVIStream_iface
, pSource
))) {
491 /* Use first stream part to get format to convert everything to */
492 AVIFILE_ReadFrame(This
, This
->pStreams
[0].pStream
,
493 This
->pStreams
[0].dwStart
);
495 /* Check if we could convert the source streams to the desired format... */
497 if (FAILED(AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
,
498 &startPos
, &streamNr
, TRUE
)))
499 return AVIERR_INTERNAL
;
500 for (n
= lStart
; n
< lStart
+ lLength
; streamNr
++) {
501 if (AVIFILE_ReadFrame(This
, pEdit
->pStreams
[streamNr
].pStream
, startPos
) == NULL
)
502 return AVIERR_BADFORMAT
;
503 startPos
= pEdit
->pStreams
[streamNr
].dwStart
;
504 n
+= pEdit
->pStreams
[streamNr
].dwLength
;
506 } else if (AVIFILE_ReadFrame(This
, pSource
, lStart
) == NULL
)
507 return AVIERR_BADFORMAT
;
509 This
->bDecompress
= TRUE
;
510 This
->sInfo
.fccHandler
= 0;
512 } else if (AVIFILE_ReadFrame(This
, pSource
, lStart
) == NULL
)
513 return AVIERR_BADFORMAT
; /* Can't convert source to own format */
514 } /* FIXME: something special for the other formats? */
516 /* Make sure we have enough memory for parts */
520 AVIFILE_FindStreamInTable(pEdit
, lStart
+ lLength
, &pStream
,
521 &endPos
, &nLastStream
, TRUE
);
522 AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
,
523 &startPos
, &streamNr
, FALSE
);
524 if (nLastStream
== streamNr
)
527 nStreams
= nLastStream
- streamNr
;
530 if (This
->nStreams
+ nStreams
+ 1 > This
->nTableSize
) {
531 n
= This
->nStreams
+ nStreams
+ 33;
533 This
->pStreams
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->pStreams
, n
* sizeof(EditStreamTable
));
534 if (This
->pStreams
== NULL
)
535 return AVIERR_MEMORY
;
536 This
->nTableSize
= n
;
539 if (plLength
!= NULL
)
542 /* now do the real work */
543 if (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
> *plStart
) {
544 AVIFILE_FindStreamInTable(This
, *plStart
, &pStream
,
545 &startPos
, &streamNr
, FALSE
);
546 if (startPos
!= This
->pStreams
[streamNr
].dwStart
) {
547 /* split stream streamNr at startPos */
548 memmove(This
->pStreams
+ streamNr
+ nStreams
+ 1,
549 This
->pStreams
+ streamNr
,
550 (This
->nStreams
+ nStreams
- streamNr
+ 1) * sizeof(EditStreamTable
));
552 This
->pStreams
[streamNr
+ 2].dwLength
=
553 EditStreamEnd(This
, streamNr
+ 2) - startPos
;
554 This
->pStreams
[streamNr
+ 2].dwStart
= startPos
;
555 This
->pStreams
[streamNr
].dwLength
=
556 startPos
- This
->pStreams
[streamNr
].dwStart
;
557 IAVIStream_AddRef(This
->pStreams
[streamNr
].pStream
);
560 /* insert before stream at streamNr */
561 memmove(This
->pStreams
+ streamNr
+ nStreams
, This
->pStreams
+ streamNr
,
562 (This
->nStreams
+ nStreams
- streamNr
) * sizeof(EditStreamTable
));
564 } else /* append the streams */
565 streamNr
= This
->nStreams
;
568 /* insert the parts of the editable stream instead of itself */
569 AVIFILE_FindStreamInTable(pEdit
, lStart
+ lLength
, &pStream
,
570 &endPos
, NULL
, FALSE
);
571 AVIFILE_FindStreamInTable(pEdit
, lStart
, &pStream
, &startPos
, &n
, FALSE
);
573 memcpy(This
->pStreams
+ streamNr
, pEdit
->pStreams
+ n
,
574 nStreams
* sizeof(EditStreamTable
));
575 if (This
->pStreams
[streamNr
].dwStart
< startPos
) {
576 This
->pStreams
[streamNr
].dwLength
=
577 EditStreamEnd(This
, streamNr
) - startPos
;
578 This
->pStreams
[streamNr
].dwStart
= startPos
;
580 if (endPos
< EditStreamEnd(This
, streamNr
+ nStreams
))
581 This
->pStreams
[streamNr
+ nStreams
].dwLength
=
582 endPos
- This
->pStreams
[streamNr
+ nStreams
].dwStart
;
584 /* a simple stream */
585 This
->pStreams
[streamNr
].pStream
= pSource
;
586 This
->pStreams
[streamNr
].dwStart
= lStart
;
587 This
->pStreams
[streamNr
].dwLength
= lLength
;
590 for (n
= 0; n
< nStreams
; n
++) {
591 IAVIStream_AddRef(This
->pStreams
[streamNr
+ n
].pStream
);
592 if (0 < streamNr
+ n
&&
593 This
->pStreams
[streamNr
+ n
- 1].pStream
!= This
->pStreams
[streamNr
+ n
].pStream
) {
594 This
->sInfo
.dwFlags
|= AVISTREAMINFO_FORMATCHANGES
;
595 This
->sInfo
.dwFormatChangeCount
++;
598 This
->sInfo
.dwEditCount
++;
599 This
->sInfo
.dwLength
+= lLength
;
600 This
->nStreams
+= nStreams
;
605 static HRESULT WINAPI
IAVIEditStream_fnClone(IAVIEditStream
*iface
,
608 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
609 IAVIEditStreamImpl
* pEdit
;
612 TRACE("(%p,%p)\n",iface
,ppResult
);
614 if (ppResult
== NULL
)
615 return AVIERR_BADPARAM
;
618 pEdit
= (IAVIEditStreamImpl
*)AVIFILE_CreateEditStream(NULL
);
620 return AVIERR_MEMORY
;
621 if (This
->nStreams
> pEdit
->nTableSize
) {
622 pEdit
->pStreams
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, pEdit
->pStreams
,
623 This
->nStreams
* sizeof(EditStreamTable
));
624 if (pEdit
->pStreams
== NULL
)
625 return AVIERR_MEMORY
;
626 pEdit
->nTableSize
= This
->nStreams
;
628 pEdit
->nStreams
= This
->nStreams
;
629 memcpy(pEdit
->pStreams
, This
->pStreams
,
630 This
->nStreams
* sizeof(EditStreamTable
));
631 memcpy(&pEdit
->sInfo
,&This
->sInfo
,sizeof(This
->sInfo
));
632 for (i
= 0; i
< This
->nStreams
; i
++) {
633 if (pEdit
->pStreams
[i
].pStream
!= NULL
)
634 IAVIStream_AddRef(pEdit
->pStreams
[i
].pStream
);
637 *ppResult
= &This
->IAVIStream_iface
;
642 static HRESULT WINAPI
IAVIEditStream_fnSetInfo(IAVIEditStream
*iface
,
643 LPAVISTREAMINFOW asi
,LONG size
)
645 IAVIEditStreamImpl
*This
= impl_from_IAVIEditStream(iface
);
647 TRACE("(%p,%p,%d)\n",iface
,asi
,size
);
649 /* check parameters */
650 if (size
>= 0 && size
< sizeof(AVISTREAMINFOW
))
651 return AVIERR_BADSIZE
;
653 This
->sInfo
.wLanguage
= asi
->wLanguage
;
654 This
->sInfo
.wPriority
= asi
->wPriority
;
655 This
->sInfo
.dwStart
= asi
->dwStart
;
656 This
->sInfo
.dwRate
= asi
->dwRate
;
657 This
->sInfo
.dwScale
= asi
->dwScale
;
658 This
->sInfo
.dwQuality
= asi
->dwQuality
;
659 CopyRect(&This
->sInfo
.rcFrame
, &asi
->rcFrame
);
660 memcpy(This
->sInfo
.szName
, asi
->szName
, sizeof(asi
->szName
));
661 This
->sInfo
.dwEditCount
++;
666 static const struct IAVIEditStreamVtbl ieditstream
= {
667 IAVIEditStream_fnQueryInterface
,
668 IAVIEditStream_fnAddRef
,
669 IAVIEditStream_fnRelease
,
670 IAVIEditStream_fnCut
,
671 IAVIEditStream_fnCopy
,
672 IAVIEditStream_fnPaste
,
673 IAVIEditStream_fnClone
,
674 IAVIEditStream_fnSetInfo
677 static HRESULT WINAPI
IEditAVIStream_fnQueryInterface(IAVIStream
*iface
,
678 REFIID refiid
,LPVOID
*obj
)
680 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
681 return IAVIEditStream_QueryInterface(&This
->IAVIEditStream_iface
,refiid
,obj
);
684 static ULONG WINAPI
IEditAVIStream_fnAddRef(IAVIStream
*iface
)
686 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
687 return IAVIEditStream_AddRef(&This
->IAVIEditStream_iface
);
690 static ULONG WINAPI
IEditAVIStream_fnRelease(IAVIStream
*iface
)
692 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
693 return IAVIEditStream_Release(&This
->IAVIEditStream_iface
);
696 static HRESULT WINAPI
IEditAVIStream_fnCreate(IAVIStream
*iface
,
697 LPARAM lParam1
,LPARAM lParam2
)
699 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
704 if (This
->pStreams
== NULL
) {
705 This
->pStreams
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 256 * sizeof(EditStreamTable
));
706 if (This
->pStreams
== NULL
)
707 return AVIERR_MEMORY
;
708 This
->nTableSize
= 256;
712 IAVIStream_Info((PAVISTREAM
)lParam1
, &This
->sInfo
, sizeof(This
->sInfo
));
713 IAVIStream_AddRef((PAVISTREAM
)lParam1
);
714 This
->pStreams
[0].pStream
= (PAVISTREAM
)lParam1
;
715 This
->pStreams
[0].dwStart
= This
->sInfo
.dwStart
;
716 This
->pStreams
[0].dwLength
= This
->sInfo
.dwLength
;
722 static HRESULT WINAPI
IEditAVIStream_fnInfo(IAVIStream
*iface
,
723 AVISTREAMINFOW
*psi
,LONG size
)
725 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
727 TRACE("(%p,%p,%d)\n",iface
,psi
,size
);
730 return AVIERR_BADPARAM
;
732 return AVIERR_BADSIZE
;
734 if (This
->bDecompress
)
735 This
->sInfo
.fccHandler
= 0;
737 memcpy(psi
, &This
->sInfo
, min((DWORD
)size
, sizeof(This
->sInfo
)));
739 if ((DWORD
)size
< sizeof(This
->sInfo
))
740 return AVIERR_BUFFERTOOSMALL
;
744 static LONG WINAPI
IEditAVIStream_fnFindSample(IAVIStream
*iface
,LONG pos
,
747 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
749 DWORD streamPos
, streamNr
;
751 TRACE("(%p,%d,0x%08X)\n",iface
,pos
,flags
);
753 if (flags
& FIND_FROM_START
)
754 pos
= (LONG
)This
->sInfo
.dwStart
;
756 /* outside of stream? */
757 if (pos
< (LONG
)This
->sInfo
.dwStart
||
758 (LONG
)This
->sInfo
.dwStart
+ (LONG
)This
->sInfo
.dwLength
<= pos
)
761 /* map our position to a stream and position in it */
762 if (AVIFILE_FindStreamInTable(This
, pos
, &stream
, &streamPos
,
763 &streamNr
, TRUE
) != S_OK
)
764 return -1; /* doesn't exist */
766 if (This
->bDecompress
) {
767 /* only one stream -- format changes only at start */
768 if (flags
& FIND_FORMAT
)
769 return (flags
& FIND_NEXT
? -1 : 0);
771 /* FIXME: map positions back to us */
772 return IAVIStream_FindSample(stream
, streamPos
, flags
);
774 /* assume change of format every frame */
779 static HRESULT WINAPI
IEditAVIStream_fnReadFormat(IAVIStream
*iface
,LONG pos
,
780 LPVOID format
,LONG
*fmtsize
)
782 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
783 LPBITMAPINFOHEADER lp
;
788 TRACE("(%p,%d,%p,%p)\n",iface
,pos
,format
,fmtsize
);
790 if (fmtsize
== NULL
|| pos
< This
->sInfo
.dwStart
||
791 This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
<= pos
)
792 return AVIERR_BADPARAM
;
794 /* find stream corresponding to position */
795 hr
= AVIFILE_FindStreamInTable(This
, pos
, &stream
, &n
, NULL
, FALSE
);
799 if (! This
->bDecompress
)
800 return IAVIStream_ReadFormat(stream
, n
, format
, fmtsize
);
802 lp
= AVIFILE_ReadFrame(This
, stream
, n
);
805 if (lp
->biBitCount
<= 8) {
806 n
= (lp
->biClrUsed
> 0 ? lp
->biClrUsed
: 1 << lp
->biBitCount
);
807 n
*= sizeof(RGBQUAD
);
812 memcpy(format
, lp
, min((LONG
)n
, *fmtsize
));
813 hr
= ((LONG
)n
> *fmtsize
? AVIERR_BUFFERTOOSMALL
: AVIERR_OK
);
819 static HRESULT WINAPI
IEditAVIStream_fnSetFormat(IAVIStream
*iface
,LONG pos
,
820 LPVOID format
,LONG formatsize
)
822 TRACE("(%p,%d,%p,%d)\n",iface
,pos
,format
,formatsize
);
824 return AVIERR_UNSUPPORTED
;
827 static HRESULT WINAPI
IEditAVIStream_fnRead(IAVIStream
*iface
,LONG start
,
828 LONG samples
,LPVOID buffer
,
829 LONG buffersize
,LONG
*bytesread
,
832 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
834 DWORD streamPos
, streamNr
;
835 LONG readBytes
, readSamples
, count
;
838 TRACE("(%p,%d,%d,%p,%d,%p,%p) -- 0x%08X\n",iface
,start
,samples
,
839 buffer
,buffersize
,bytesread
,samplesread
,This
->sInfo
.fccType
);
841 /* check parameters */
842 if (bytesread
!= NULL
)
844 if (samplesread
!= NULL
)
847 return AVIERR_BADSIZE
;
848 if ((DWORD
)start
< This
->sInfo
.dwStart
||
849 This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
< (DWORD
)start
)
850 return AVIERR_BADPARAM
;
852 if (! This
->bDecompress
) {
853 /* audio like data -- sample-based */
856 return AVIERR_OK
; /* nothing at all or already done */
858 if (FAILED(AVIFILE_FindStreamInTable(This
, start
, &stream
,
859 &streamPos
, &streamNr
, FALSE
)))
862 /* limit to end of the stream */
864 if (streamPos
+ count
> EditStreamEnd(This
, streamNr
))
865 count
= EditStreamEnd(This
, streamNr
) - streamPos
;
867 hr
= IAVIStream_Read(stream
, streamPos
, count
, buffer
, buffersize
,
868 &readBytes
, &readSamples
);
871 if (readBytes
== 0 && readSamples
== 0 && count
!= 0)
872 return AVIERR_FILEREAD
; /* for bad stream implementations */
874 if (samplesread
!= NULL
)
875 *samplesread
+= readSamples
;
876 if (bytesread
!= NULL
)
877 *bytesread
+= readBytes
;
878 if (buffer
!= NULL
) {
879 buffer
= ((LPBYTE
)buffer
)+readBytes
;
880 buffersize
-= readBytes
;
884 } while (This
->sInfo
.dwStart
+ This
->sInfo
.dwLength
> start
);
886 /* video like data -- frame-based */
887 LPBITMAPINFOHEADER lp
;
892 if (FAILED(AVIFILE_FindStreamInTable(This
, start
, &stream
,
893 &streamPos
, &streamNr
, FALSE
)))
896 lp
= AVIFILE_ReadFrame(This
, stream
, streamPos
);
900 if (buffer
!= NULL
) {
901 /* need size of format to skip */
902 if (lp
->biBitCount
<= 8) {
903 count
= lp
->biClrUsed
> 0 ? lp
->biClrUsed
: 1 << lp
->biBitCount
;
904 count
*= sizeof(RGBQUAD
);
909 if (buffersize
< lp
->biSizeImage
)
910 return AVIERR_BUFFERTOOSMALL
;
911 memcpy(buffer
, (LPBYTE
)lp
+ count
, lp
->biSizeImage
);
914 if (bytesread
!= NULL
)
915 *bytesread
= lp
->biSizeImage
;
916 if (samplesread
!= NULL
)
923 static HRESULT WINAPI
IEditAVIStream_fnWrite(IAVIStream
*iface
,LONG start
,
924 LONG samples
,LPVOID buffer
,
925 LONG buffersize
,DWORD flags
,
926 LONG
*sampwritten
,LONG
*byteswritten
)
928 TRACE("(%p,%d,%d,%p,%d,0x%08X,%p,%p)\n",iface
,start
,samples
,buffer
,
929 buffersize
,flags
,sampwritten
,byteswritten
);
931 /* be sure return parameters have correct values */
932 if (sampwritten
!= NULL
)
934 if (byteswritten
!= NULL
)
937 return AVIERR_UNSUPPORTED
;
940 static HRESULT WINAPI
IEditAVIStream_fnDelete(IAVIStream
*iface
,LONG start
,
943 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
945 TRACE("(%p,%d,%d)\n",iface
,start
,samples
);
947 return IAVIEditStream_Cut(&This
->IAVIEditStream_iface
,&start
,&samples
,NULL
);
950 static HRESULT WINAPI
IEditAVIStream_fnReadData(IAVIStream
*iface
,DWORD fcc
,
951 LPVOID lp
,LONG
*lpread
)
953 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
956 TRACE("(%p,0x%08X,%p,%p)\n",iface
,fcc
,lp
,lpread
);
958 /* check parameters */
959 if (lp
== NULL
|| lpread
== NULL
)
960 return AVIERR_BADPARAM
;
962 /* simply ask every stream and return the first block found */
963 for (n
= 0; n
< This
->nStreams
; n
++) {
964 HRESULT hr
= IAVIStream_ReadData(This
->pStreams
[n
].pStream
,fcc
,lp
,lpread
);
971 return AVIERR_NODATA
;
974 static HRESULT WINAPI
IEditAVIStream_fnWriteData(IAVIStream
*iface
,DWORD fcc
,
977 TRACE("(%p,0x%08X,%p,%d)\n",iface
,fcc
,lp
,size
);
979 return AVIERR_UNSUPPORTED
;
982 static HRESULT WINAPI
IEditAVIStream_fnSetInfo(IAVIStream
*iface
,
983 AVISTREAMINFOW
*info
,LONG len
)
985 IAVIEditStreamImpl
*This
= impl_from_IAVIStream( iface
);
987 TRACE("(%p,%p,%d)\n",iface
,info
,len
);
989 return IAVIEditStream_SetInfo(&This
->IAVIEditStream_iface
,info
,len
);
992 static const struct IAVIStreamVtbl ieditstast
= {
993 IEditAVIStream_fnQueryInterface
,
994 IEditAVIStream_fnAddRef
,
995 IEditAVIStream_fnRelease
,
996 IEditAVIStream_fnCreate
,
997 IEditAVIStream_fnInfo
,
998 IEditAVIStream_fnFindSample
,
999 IEditAVIStream_fnReadFormat
,
1000 IEditAVIStream_fnSetFormat
,
1001 IEditAVIStream_fnRead
,
1002 IEditAVIStream_fnWrite
,
1003 IEditAVIStream_fnDelete
,
1004 IEditAVIStream_fnReadData
,
1005 IEditAVIStream_fnWriteData
,
1006 IEditAVIStream_fnSetInfo
1009 PAVIEDITSTREAM
AVIFILE_CreateEditStream(PAVISTREAM pstream
)
1011 IAVIEditStreamImpl
*pedit
= NULL
;
1013 pedit
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAVIEditStreamImpl
));
1017 pedit
->IAVIEditStream_iface
.lpVtbl
= &ieditstream
;
1018 pedit
->IAVIStream_iface
.lpVtbl
= &ieditstast
;
1021 IAVIStream_Create(&pedit
->IAVIStream_iface
, (LPARAM
)pstream
, 0);
1023 return (PAVIEDITSTREAM
)pedit
;