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
32 #include "avifile_private.h"
33 #include "extrachunk.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
39 /***********************************************************************/
41 /* internal interface to get access to table of stream in an editable stream */
43 typedef struct _EditStreamTable {
44 PAVISTREAM pStream; /* stream which contains the data */
45 DWORD dwStart; /* where starts the part which is also our */
46 DWORD dwLength; /* how many is also in this stream */
49 #define INTERFACE IEditStreamInternal
50 DECLARE_INTERFACE_(IEditStreamInternal,IUnknown)
52 /*** IUnknown methods ***/
53 STDMETHOD_(HRESULT,QueryInterface)(THIS_ REFIID riid, void** ppvObject) PURE;
54 STDMETHOD_(ULONG,AddRef)(THIS) PURE;
55 STDMETHOD_(ULONG,Release)(THIS) PURE;
56 /*** IEditStreamInternal methods ***/
57 STDMETHOD(GetEditStreamImpl)(THIS_ LPVOID*) PURE;
61 #define EditStreamEnd(This,streamNr) ((This)->pStreams[streamNr].dwStart + \
62 (This)->pStreams[streamNr].dwLength)
64 /***********************************************************************/
66 static HRESULT WINAPI IAVIEditStream_fnQueryInterface(IAVIEditStream*iface,REFIID refiid,LPVOID *obj);
67 static ULONG WINAPI IAVIEditStream_fnAddRef(IAVIEditStream*iface);
68 static ULONG WINAPI IAVIEditStream_fnRelease(IAVIEditStream*iface);
69 static HRESULT WINAPI IAVIEditStream_fnCut(IAVIEditStream*iface,LONG*plStart,
70 LONG*plLength,PAVISTREAM*ppResult);
71 static HRESULT WINAPI IAVIEditStream_fnCopy(IAVIEditStream*iface,LONG*plStart,
72 LONG*plLength,PAVISTREAM*ppResult);
73 static HRESULT WINAPI IAVIEditStream_fnPaste(IAVIEditStream*iface,LONG*plStart,
74 LONG*plLength,PAVISTREAM pSource,
75 LONG lStart,LONG lEnd);
76 static HRESULT WINAPI IAVIEditStream_fnClone(IAVIEditStream*iface,
78 static HRESULT WINAPI IAVIEditStream_fnSetInfo(IAVIEditStream*iface,
79 LPAVISTREAMINFOW asi,LONG size);
81 static const struct IAVIEditStreamVtbl ieditstream = {
82 IAVIEditStream_fnQueryInterface,
83 IAVIEditStream_fnAddRef,
84 IAVIEditStream_fnRelease,
86 IAVIEditStream_fnCopy,
87 IAVIEditStream_fnPaste,
88 IAVIEditStream_fnClone,
89 IAVIEditStream_fnSetInfo
92 static HRESULT WINAPI IEditAVIStream_fnQueryInterface(IAVIStream*iface,REFIID refiid,LPVOID*obj);
93 static ULONG WINAPI IEditAVIStream_fnAddRef(IAVIStream*iface);
94 static ULONG WINAPI IEditAVIStream_fnRelease(IAVIStream*iface);
95 static HRESULT WINAPI IEditAVIStream_fnCreate(IAVIStream*iface,LPARAM lParam1,LPARAM lParam2);
96 static HRESULT WINAPI IEditAVIStream_fnInfo(IAVIStream*iface,AVISTREAMINFOW *psi,LONG size);
97 static LONG WINAPI IEditAVIStream_fnFindSample(IAVIStream*iface,LONG pos,
99 static HRESULT WINAPI IEditAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG*formatsize);
100 static HRESULT WINAPI IEditAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,LPVOID format,LONG formatsize);
101 static HRESULT WINAPI IEditAVIStream_fnRead(IAVIStream*iface,LONG start,
102 LONG samples,LPVOID buffer,
103 LONG buffersize,LONG*bytesread,
105 static HRESULT WINAPI IEditAVIStream_fnWrite(IAVIStream*iface,LONG start,
106 LONG samples,LPVOID buffer,
107 LONG buffersize,DWORD flags,
108 LONG*sampwritten,LONG*byteswritten);
109 static HRESULT WINAPI IEditAVIStream_fnDelete(IAVIStream*iface,LONG start,LONG samples);
110 static HRESULT WINAPI IEditAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,
111 LPVOID lp,LONG *lpread);
112 static HRESULT WINAPI IEditAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,
113 LPVOID lp,LONG size);
114 static HRESULT WINAPI IEditAVIStream_fnSetInfo(IAVIStream*iface,AVISTREAMINFOW*info,LONG infolen);
116 static const struct IAVIStreamVtbl ieditstast = {
117 IEditAVIStream_fnQueryInterface,
118 IEditAVIStream_fnAddRef,
119 IEditAVIStream_fnRelease,
120 IEditAVIStream_fnCreate,
121 IEditAVIStream_fnInfo,
122 IEditAVIStream_fnFindSample,
123 IEditAVIStream_fnReadFormat,
124 IEditAVIStream_fnSetFormat,
125 IEditAVIStream_fnRead,
126 IEditAVIStream_fnWrite,
127 IEditAVIStream_fnDelete,
128 IEditAVIStream_fnReadData,
129 IEditAVIStream_fnWriteData,
130 IEditAVIStream_fnSetInfo
133 static HRESULT WINAPI IEditStreamInternal_fnQueryInterface(IEditStreamInternal*iface,REFIID refiid,LPVOID*obj);
134 static ULONG WINAPI IEditStreamInternal_fnAddRef(IEditStreamInternal*iface);
135 static ULONG WINAPI IEditStreamInternal_fnRelease(IEditStreamInternal*iface);
136 static HRESULT WINAPI IEditStreamInternal_fnGetEditStreamImpl(IEditStreamInternal*iface,LPVOID*ppimpl);
138 static const struct IEditStreamInternalVtbl ieditstreaminternal = {
139 IEditStreamInternal_fnQueryInterface,
140 IEditStreamInternal_fnAddRef,
141 IEditStreamInternal_fnRelease,
142 IEditStreamInternal_fnGetEditStreamImpl
145 typedef struct _IAVIEditStreamImpl IAVIEditStreamImpl;
147 typedef struct _IEditAVIStreamImpl {
149 const IAVIStreamVtbl *lpVtbl;
151 /* IAVIStream stuff */
152 IAVIEditStreamImpl *pae;
153 } IEditAVIStreamImpl;
155 typedef struct _IEditStreamInternalImpl {
157 const IEditStreamInternalVtbl *lpVtbl;
159 /* IEditStreamInternal stuff */
160 IAVIEditStreamImpl *pae;
161 } IEditStreamInternalImpl;
163 struct _IAVIEditStreamImpl {
165 const IAVIEditStreamVtbl *lpVtbl;
168 /* IAVIEditStream stuff */
169 IEditAVIStreamImpl iAVIStream;
170 IEditStreamInternalImpl iEditStreamInternal;
172 AVISTREAMINFOW sInfo;
174 EditStreamTable *pStreams;
175 DWORD nStreams; /* current fill level of pStreams table */
176 DWORD nTableSize; /* size of pStreams table */
179 PAVISTREAM pCurStream;
180 PGETFRAME pg; /* IGetFrame for pCurStream */
181 LPBITMAPINFOHEADER lpFrame; /* frame of pCurStream */
184 /***********************************************************************/
186 PAVIEDITSTREAM AVIFILE_CreateEditStream(PAVISTREAM pstream)
188 IAVIEditStreamImpl *pedit = NULL;
190 pedit = HeapAlloc(GetProcessHeap(), 0, sizeof(IAVIEditStreamImpl));
194 pedit->lpVtbl = &ieditstream;
195 pedit->iAVIStream.lpVtbl = &ieditstast;
196 pedit->iAVIStream.pae = pedit;
197 pedit->iEditStreamInternal.lpVtbl = &ieditstreaminternal;
198 pedit->iEditStreamInternal.pae = pedit;
201 IAVIStream_Create((PAVISTREAM)&pedit->iAVIStream,(LPARAM)pstream,0);
203 return (PAVIEDITSTREAM)pedit;
206 static HRESULT AVIFILE_FindStreamInTable(IAVIEditStreamImpl* const This,
207 DWORD pos,PAVISTREAM *ppStream,
209 DWORD* streamNr,BOOL bFindSample)
213 TRACE("(%p,%lu,%p,%p,%p,%d)\n",This,pos,ppStream,streamPos,
214 streamNr,bFindSample);
216 if (pos < This->sInfo.dwStart)
217 return AVIERR_BADPARAM;
219 pos -= This->sInfo.dwStart;
220 for (n = 0; n < This->nStreams; n++) {
221 if (pos < This->pStreams[n].dwLength) {
222 *ppStream = This->pStreams[n].pStream;
223 *streamPos = This->pStreams[n].dwStart + pos;
224 if (streamNr != NULL)
229 pos -= This->pStreams[n].dwLength;
231 if (pos == 0 && bFindSample) {
232 *ppStream = This->pStreams[--n].pStream;
233 *streamPos = EditStreamEnd(This, n);
234 if (streamNr != NULL)
237 TRACE(" -- pos=0 && b=1 -> (%p,%lu,%lu)\n",*ppStream, *streamPos, n);
242 if (streamNr != NULL)
245 TRACE(" -> ERROR (NULL,0,0)\n");
246 return AVIERR_BADPARAM;
250 static LPVOID AVIFILE_ReadFrame(IAVIEditStreamImpl* const This,
251 PAVISTREAM pstream, LONG pos)
255 TRACE("(%p,%p,%ld)\n",This,pstream,pos);
260 /* if stream changes make sure that only palette changes */
261 if (This->pCurStream != pstream) {
262 pg = AVIStreamGetFrameOpen(pstream, NULL);
265 if (This->pg != NULL) {
266 if (IGetFrame_SetFormat(pg, This->lpFrame, NULL, 0, 0, -1, -1)) {
267 AVIStreamGetFrameClose(pg);
268 ERR(": IGetFrame_SetFormat failed\n");
271 AVIStreamGetFrameClose(This->pg);
274 This->pCurStream = pstream;
277 /* now get the decompressed frame */
278 This->lpFrame = AVIStreamGetFrame(This->pg, pos);
279 if (This->lpFrame != NULL)
280 This->sInfo.dwSuggestedBufferSize = This->lpFrame->biSizeImage;
282 return This->lpFrame;
285 static HRESULT AVIFILE_RemoveStream(IAVIEditStreamImpl* const This, DWORD nr)
287 assert(This != NULL);
288 assert(nr < This->nStreams);
291 IAVIStream_Release(This->pStreams[nr].pStream);
293 if (This->nStreams - nr > 0) {
294 memmove(This->pStreams + nr, This->pStreams + nr + 1,
295 (This->nStreams - nr) * sizeof(EditStreamTable));
297 This->pStreams[This->nStreams].pStream = NULL;
298 This->pStreams[This->nStreams].dwStart = 0;
299 This->pStreams[This->nStreams].dwLength = 0;
301 /* try to merge the part before the deleted one and the one after it */
302 if (0 < nr && 0 < This->nStreams &&
303 This->pStreams[nr - 1].pStream == This->pStreams[nr].pStream) {
304 if (EditStreamEnd(This, nr - 1) == This->pStreams[nr].dwStart) {
305 This->pStreams[nr - 1].dwLength += This->pStreams[nr].dwLength;
306 return AVIFILE_RemoveStream(This, nr);
313 static BOOL AVIFILE_FormatsEqual(PAVISTREAM avi1, PAVISTREAM avi2)
315 LPVOID fmt1 = NULL, fmt2 = NULL;
316 LONG size1, size2, start1, start2;
319 assert(avi1 != NULL && avi2 != NULL);
321 /* get stream starts and check format sizes */
322 start1 = AVIStreamStart(avi1);
323 start2 = AVIStreamStart(avi2);
324 if (FAILED(AVIStreamFormatSize(avi1, start1, &size1)))
326 if (FAILED(AVIStreamFormatSize(avi2, start2, &size2)))
331 /* sizes match, now get formats and compare them */
332 fmt1 = HeapAlloc(GetProcessHeap(), 0, size1);
335 if (SUCCEEDED(AVIStreamReadFormat(avi1, start1, fmt1, &size1))) {
336 fmt2 = HeapAlloc(GetProcessHeap(), 0, size1);
338 if (SUCCEEDED(AVIStreamReadFormat(avi2, start2, fmt2, &size1)))
339 status = (memcmp(fmt1, fmt2, size1) == 0);
344 HeapFree(GetProcessHeap(), 0, fmt2);
345 HeapFree(GetProcessHeap(), 0, fmt1);
350 /***********************************************************************/
352 static HRESULT WINAPI IAVIEditStream_fnQueryInterface(IAVIEditStream*iface,REFIID refiid,LPVOID *obj)
354 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
356 TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
358 if (IsEqualGUID(&IID_IUnknown, refiid) ||
359 IsEqualGUID(&IID_IAVIEditStream, refiid)) {
361 IAVIEditStream_AddRef(iface);
364 } else if (IsEqualGUID(&IID_IAVIStream, refiid)) {
365 *obj = &This->iAVIStream;
366 IAVIEditStream_AddRef(iface);
369 } else if (IsEqualGUID(&IID_IEditStreamInternal, refiid)) {
370 *obj = &This->iEditStreamInternal;
371 IAVIEditStream_AddRef(iface);
376 return OLE_E_ENUM_NOMORE;
379 static ULONG WINAPI IAVIEditStream_fnAddRef(IAVIEditStream*iface)
381 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
382 ULONG ref = InterlockedIncrement(&This->ref);
384 TRACE("(%p) -> %ld\n", iface, ref);
389 static ULONG WINAPI IAVIEditStream_fnRelease(IAVIEditStream*iface)
391 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
393 ULONG ref = InterlockedDecrement(&This->ref);
395 TRACE("(%p) -> %ld\n", iface, ref);
399 if (This->pg != NULL)
400 AVIStreamGetFrameClose(This->pg);
401 if (This->pStreams != NULL) {
402 for (i = 0; i < This->nStreams; i++) {
403 if (This->pStreams[i].pStream != NULL)
404 IAVIStream_Release(This->pStreams[i].pStream);
406 HeapFree(GetProcessHeap(), 0, This->pStreams);
409 HeapFree(GetProcessHeap(), 0, This);
415 static HRESULT WINAPI IAVIEditStream_fnCut(IAVIEditStream*iface,LONG*plStart,
416 LONG*plLength,PAVISTREAM*ppResult)
418 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
420 DWORD start, len, streamPos, streamNr;
423 TRACE("(%p,%p,%p,%p)\n",iface,plStart,plLength,ppResult);
425 if (ppResult != NULL)
427 if (plStart == NULL || plLength == NULL || *plStart < 0)
428 return AVIERR_BADPARAM;
430 /* if asked for cutted part copy it before deleting */
431 if (ppResult != NULL) {
432 hr = IAVIEditStream_Copy(iface, plStart, plLength, ppResult);
440 /* now delete the requested part */
442 hr = AVIFILE_FindStreamInTable(This, start, &stream,
443 &streamPos, &streamNr, FALSE);
446 if (This->pStreams[streamNr].dwStart == streamPos) {
447 /* deleting from start of part */
448 if (len < This->pStreams[streamNr].dwLength) {
450 This->pStreams[streamNr].dwStart += len;
451 This->pStreams[streamNr].dwLength -= len;
452 This->sInfo.dwLength -= len;
455 /* we must return decompressed data now */
456 This->bDecompress = TRUE;
458 /* deleting hole part */
459 len -= This->pStreams[streamNr].dwLength;
460 AVIFILE_RemoveStream(This,streamNr);
462 } else if (EditStreamEnd(This, streamNr) <= streamPos + len) {
463 /* deleting at end of a part */
464 DWORD count = EditStreamEnd(This, streamNr) - streamPos;
465 This->sInfo.dwLength -= count;
467 This->pStreams[streamNr].dwLength =
468 streamPos - This->pStreams[streamNr].dwStart;
471 if (This->nStreams + 1 >= This->nTableSize) {
472 This->pStreams = HeapReAlloc(GetProcessHeap(), 0, This->pStreams,
473 (This->nTableSize + 32) * sizeof(EditStreamTable));
474 if (This->pStreams == NULL)
475 return AVIERR_MEMORY;
476 This->nTableSize += 32;
478 memmove(This->pStreams + streamNr + 1, This->pStreams + streamNr,
479 (This->nStreams - streamNr) * sizeof(EditStreamTable));
482 IAVIStream_AddRef(This->pStreams[streamNr + 1].pStream);
483 This->pStreams[streamNr + 1].dwStart = streamPos + len;
484 This->pStreams[streamNr + 1].dwLength =
485 EditStreamEnd(This, streamNr) - This->pStreams[streamNr + 1].dwStart;
487 This->pStreams[streamNr].dwLength =
488 streamPos - This->pStreams[streamNr].dwStart;
489 This->sInfo.dwLength -= len;
494 This->sInfo.dwEditCount++;
499 static HRESULT WINAPI IAVIEditStream_fnCopy(IAVIEditStream*iface,LONG*plStart,
500 LONG*plLength,PAVISTREAM*ppResult)
502 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
503 IAVIEditStreamImpl* pEdit;
507 TRACE("(%p,%p,%p,%p)\n",iface,plStart,plLength,ppResult);
509 if (ppResult == NULL)
510 return AVIERR_BADPARAM;
512 if (plStart == NULL || plLength == NULL || *plStart < 0 || *plLength < 0)
513 return AVIERR_BADPARAM;
516 if (*(LPDWORD)plLength > This->sInfo.dwLength)
517 *(LPDWORD)plLength = This->sInfo.dwLength;
518 if (*(LPDWORD)plStart < This->sInfo.dwStart) {
519 *(LPDWORD)plLength -= This->sInfo.dwStart - *(LPDWORD)plStart;
520 *(LPDWORD)plStart = This->sInfo.dwStart;
522 return AVIERR_BADPARAM;
524 if (*(LPDWORD)plStart + *(LPDWORD)plLength > This->sInfo.dwStart + This->sInfo.dwLength)
525 *(LPDWORD)plLength = This->sInfo.dwStart + This->sInfo.dwLength -
528 pEdit = (IAVIEditStreamImpl*)AVIFILE_CreateEditStream(NULL);
530 return AVIERR_MEMORY;
532 hr = IAVIEditStream_Paste((PAVIEDITSTREAM)pEdit,&start,plLength,
533 (PAVISTREAM)&This->iAVIStream,*plStart,
534 *plStart + *plLength);
537 IAVIEditStream_Release((PAVIEDITSTREAM)pEdit);
539 *ppResult = (PAVISTREAM)&pEdit->iAVIStream;
544 static HRESULT WINAPI IAVIEditStream_fnPaste(IAVIEditStream*iface,LONG*plStart,
545 LONG*plLength,PAVISTREAM pSource,
546 LONG lStart,LONG lLength)
548 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
549 AVISTREAMINFOW srcInfo;
550 IEditStreamInternal*pInternal = NULL;
551 IAVIEditStreamImpl *pEdit = NULL;
553 DWORD startPos, endPos, streamNr, nStreams;
556 TRACE("(%p,%p,%p,%p,%ld,%ld)\n",iface,plStart,plLength,
557 pSource,lStart,lLength);
560 return AVIERR_BADHANDLE;
561 if (plStart == NULL || *plStart < 0)
562 return AVIERR_BADPARAM;
563 if (This->sInfo.dwStart + This->sInfo.dwLength < *plStart)
564 return AVIERR_BADPARAM; /* Can't paste with holes */
565 if (FAILED(IAVIStream_Info(pSource, &srcInfo, sizeof(srcInfo))))
567 if (lStart < srcInfo.dwStart || lStart >= srcInfo.dwStart + srcInfo.dwLength)
568 return AVIERR_BADPARAM;
569 if (This->sInfo.fccType == 0) {
570 /* This stream is empty */
571 IAVIStream_Info(pSource, &This->sInfo, sizeof(This->sInfo));
572 This->sInfo.dwStart = *plStart;
573 This->sInfo.dwLength = 0;
575 if (This->sInfo.fccType != srcInfo.fccType)
576 return AVIERR_UNSUPPORTED; /* different stream types */
577 if (lLength == -1) /* Copy the hole stream */
578 lLength = srcInfo.dwLength;
579 if (lStart + lLength > srcInfo.dwStart + srcInfo.dwLength)
580 lLength = srcInfo.dwStart + srcInfo.dwLength - lStart;
581 if (lLength + *plStart >= 0x80000000)
582 return AVIERR_MEMORY;
584 /* streamtype specific tests */
585 if (srcInfo.fccType == streamtypeVIDEO) {
588 size = srcInfo.rcFrame.right - srcInfo.rcFrame.left;
589 if (size != This->sInfo.rcFrame.right - This->sInfo.rcFrame.left)
590 return AVIERR_UNSUPPORTED; /* FIXME: Can't GetFrame convert it? */
591 size = srcInfo.rcFrame.bottom - srcInfo.rcFrame.top;
592 if (size != This->sInfo.rcFrame.bottom - This->sInfo.rcFrame.top)
593 return AVIERR_UNSUPPORTED; /* FIXME: Can't GetFrame convert it? */
594 } else if (srcInfo.fccType == streamtypeAUDIO) {
595 if (! AVIFILE_FormatsEqual((PAVISTREAM)&This->iAVIStream, pSource))
596 return AVIERR_UNSUPPORTED;
598 /* FIXME: streamtypeMIDI and streamtypeTEXT */
599 return AVIERR_UNSUPPORTED;
602 /* try to get an IEditStreamInternal interface */
603 if (SUCCEEDED(IAVIStream_QueryInterface(pSource, &IID_IEditStreamInternal,
604 (LPVOID*)&pInternal))) {
605 pInternal->lpVtbl->GetEditStreamImpl(pInternal, (LPVOID*)&pEdit);
606 pInternal->lpVtbl->Release(pInternal);
609 /* for video must check for change of format */
610 if (This->sInfo.fccType == streamtypeVIDEO) {
611 if (! This->bDecompress) {
612 /* Need to decompress if any of the following conditions matches:
613 * - pSource is an editable stream which decompresses
614 * - the nearest keyframe of pSource isn't lStart
615 * - the nearest keyframe of this stream isn't *plStart
616 * - the format of pSource doesn't match this one
618 if ((pEdit != NULL && pEdit->bDecompress) ||
619 AVIStreamNearestKeyFrame(pSource, lStart) != lStart ||
620 AVIStreamNearestKeyFrame((PAVISTREAM)&This->iAVIStream, *plStart) != *plStart ||
621 (This->nStreams > 0 && !AVIFILE_FormatsEqual((PAVISTREAM)&This->iAVIStream, pSource))) {
622 /* Use first stream part to get format to convert everything to */
623 AVIFILE_ReadFrame(This, This->pStreams[0].pStream,
624 This->pStreams[0].dwStart);
626 /* Check if we could convert the source streams to the disired format... */
628 if (FAILED(AVIFILE_FindStreamInTable(pEdit, lStart, &pStream,
629 &startPos, &streamNr, TRUE)))
630 return AVIERR_INTERNAL;
631 for (n = lStart; n < lStart + lLength; streamNr++) {
632 if (AVIFILE_ReadFrame(This, pEdit->pStreams[streamNr].pStream, startPos) == NULL)
633 return AVIERR_BADFORMAT;
634 startPos = pEdit->pStreams[streamNr].dwStart;
635 n += pEdit->pStreams[streamNr].dwLength;
637 } else if (AVIFILE_ReadFrame(This, pSource, lStart) == NULL)
638 return AVIERR_BADFORMAT;
640 This->bDecompress = TRUE;
641 This->sInfo.fccHandler = 0;
643 } else if (AVIFILE_ReadFrame(This, pSource, lStart) == NULL)
644 return AVIERR_BADFORMAT; /* Can't convert source to own format */
645 } /* FIXME: something special for the other formats? */
647 /* Make sure we have enough memory for parts */
651 AVIFILE_FindStreamInTable(pEdit, lStart + lLength, &pStream,
652 &endPos, &nLastStream, TRUE);
653 AVIFILE_FindStreamInTable(pEdit, lStart, &pStream,
654 &startPos, &streamNr, FALSE);
655 if (nLastStream == streamNr)
658 nStreams = nLastStream - streamNr;
661 if (This->nStreams + nStreams + 1 > This->nTableSize) {
662 n = This->nStreams + nStreams + 33;
664 This->pStreams = HeapReAlloc(GetProcessHeap(), 0, This->pStreams, n * sizeof(EditStreamTable));
665 if (This->pStreams == NULL)
666 return AVIERR_MEMORY;
667 This->nTableSize = n;
670 if (plLength != NULL)
673 /* now do the real work */
674 if (This->sInfo.dwStart + This->sInfo.dwLength > *plStart) {
675 AVIFILE_FindStreamInTable(This, *plStart, &pStream,
676 &startPos, &streamNr, FALSE);
677 if (startPos != This->pStreams[streamNr].dwStart) {
678 /* split stream streamNr at startPos */
679 memmove(This->pStreams + streamNr + nStreams + 1,
680 This->pStreams + streamNr,
681 (This->nStreams + nStreams - streamNr + 1) * sizeof(EditStreamTable));
683 This->pStreams[streamNr + 2].dwLength =
684 EditStreamEnd(This, streamNr + 2) - startPos;
685 This->pStreams[streamNr + 2].dwStart = startPos;
686 This->pStreams[streamNr].dwLength =
687 startPos - This->pStreams[streamNr].dwStart;
688 IAVIStream_AddRef(This->pStreams[streamNr].pStream);
691 /* insert before stream at streamNr */
692 memmove(This->pStreams + streamNr + nStreams, This->pStreams + streamNr,
693 (This->nStreams + nStreams - streamNr) * sizeof(EditStreamTable));
695 } else /* append the streams */
696 streamNr = This->nStreams;
699 /* insert the parts of the editable stream instead of itself */
700 AVIFILE_FindStreamInTable(pEdit, lStart + lLength, &pStream,
701 &endPos, NULL, FALSE);
702 AVIFILE_FindStreamInTable(pEdit, lStart, &pStream, &startPos, &n, FALSE);
704 memcpy(This->pStreams + streamNr, pEdit->pStreams + n,
705 nStreams * sizeof(EditStreamTable));
706 if (This->pStreams[streamNr].dwStart < startPos) {
707 This->pStreams[streamNr].dwLength =
708 EditStreamEnd(This, streamNr) - startPos;
709 This->pStreams[streamNr].dwStart = startPos;
711 if (endPos < EditStreamEnd(This, streamNr + nStreams))
712 This->pStreams[streamNr + nStreams].dwLength =
713 endPos - This->pStreams[streamNr + nStreams].dwStart;
715 /* a simple stream */
716 This->pStreams[streamNr].pStream = pSource;
717 This->pStreams[streamNr].dwStart = lStart;
718 This->pStreams[streamNr].dwLength = lLength;
721 for (n = 0; n < nStreams; n++) {
722 IAVIStream_AddRef(This->pStreams[streamNr + n].pStream);
723 if (0 < streamNr + n &&
724 This->pStreams[streamNr + n - 1].pStream != This->pStreams[streamNr + n].pStream) {
725 This->sInfo.dwFlags |= AVISTREAMINFO_FORMATCHANGES;
726 This->sInfo.dwFormatChangeCount++;
729 This->sInfo.dwEditCount++;
730 This->sInfo.dwLength += lLength;
731 This->nStreams += nStreams;
736 static HRESULT WINAPI IAVIEditStream_fnClone(IAVIEditStream*iface,
739 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
740 IAVIEditStreamImpl* pEdit;
743 TRACE("(%p,%p)\n",iface,ppResult);
745 if (ppResult == NULL)
746 return AVIERR_BADPARAM;
749 pEdit = (IAVIEditStreamImpl*)AVIFILE_CreateEditStream(NULL);
751 return AVIERR_MEMORY;
752 if (This->nStreams > pEdit->nTableSize) {
753 pEdit->pStreams = HeapReAlloc(GetProcessHeap(), 0, pEdit->pStreams,
754 This->nStreams * sizeof(EditStreamTable));
755 if (pEdit->pStreams == NULL)
756 return AVIERR_MEMORY;
757 pEdit->nTableSize = This->nStreams;
759 pEdit->nStreams = This->nStreams;
760 memcpy(pEdit->pStreams, This->pStreams,
761 This->nStreams * sizeof(EditStreamTable));
762 memcpy(&pEdit->sInfo,&This->sInfo,sizeof(This->sInfo));
763 for (i = 0; i < This->nStreams; i++) {
764 if (pEdit->pStreams[i].pStream != NULL)
765 IAVIStream_AddRef(pEdit->pStreams[i].pStream);
768 *ppResult = (PAVISTREAM)&pEdit->iAVIStream;
773 static HRESULT WINAPI IAVIEditStream_fnSetInfo(IAVIEditStream*iface,
774 LPAVISTREAMINFOW asi,LONG size)
776 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
778 TRACE("(%p,%p,%ld)\n",iface,asi,size);
780 /* check parameters */
782 return AVIERR_BADPARAM;
783 if (size != sizeof(AVISTREAMINFOW))
784 return AVIERR_BADSIZE;
785 if (asi->dwScale == 0 || asi->dwRate == 0 || (LONG)asi->dwQuality < -1 ||
786 asi->dwQuality > ICQUALITY_HIGH)
789 This->sInfo.wLanguage = asi->wLanguage;
790 This->sInfo.wPriority = asi->wPriority;
791 This->sInfo.dwStart = asi->dwStart;
792 if (asi->dwRate != 0)
793 This->sInfo.dwRate = asi->dwRate;
794 if (asi->dwScale != 0)
795 This->sInfo.dwScale = asi->dwScale;
796 if (asi->dwQuality <= ICQUALITY_HIGH)
797 This->sInfo.dwQuality = ICQUALITY_HIGH;
798 CopyRect(&This->sInfo.rcFrame, &asi->rcFrame);
799 memcpy(&This->sInfo.szName, &asi->szName, sizeof(asi->szName));
800 This->sInfo.dwEditCount++;
805 static HRESULT WINAPI IEditAVIStream_fnQueryInterface(IAVIStream*iface,
806 REFIID refiid,LPVOID*obj)
808 IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
810 assert(This->pae != NULL);
812 return IAVIEditStream_QueryInterface((IAVIEditStream*)This->pae,refiid,obj);
815 static ULONG WINAPI IEditAVIStream_fnAddRef(IAVIStream*iface)
817 IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
819 assert(This->pae != NULL);
821 return IAVIEditStream_AddRef((IAVIEditStream*)This->pae);
824 static ULONG WINAPI IEditAVIStream_fnRelease(IAVIStream*iface)
826 IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
828 assert(This->pae != NULL);
830 return IAVIEditStream_Release((IAVIEditStream*)This->pae);
833 static HRESULT WINAPI IEditAVIStream_fnCreate(IAVIStream*iface,
834 LPARAM lParam1,LPARAM lParam2)
836 IAVIEditStreamImpl *This = ((IEditAVIStreamImpl*)iface)->pae;
841 if (This->pStreams == NULL) {
842 This->pStreams = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(EditStreamTable));
843 if (This->pStreams == NULL)
844 return AVIERR_MEMORY;
845 This->nTableSize = 256;
849 IAVIStream_Info((PAVISTREAM)lParam1, &This->sInfo, sizeof(This->sInfo));
850 IAVIStream_AddRef((PAVISTREAM)lParam1);
851 This->pStreams[0].pStream = (PAVISTREAM)lParam1;
852 This->pStreams[0].dwStart = This->sInfo.dwStart;
853 This->pStreams[0].dwLength = This->sInfo.dwLength;
859 static HRESULT WINAPI IEditAVIStream_fnInfo(IAVIStream*iface,
860 AVISTREAMINFOW *psi,LONG size)
862 IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
864 TRACE("(%p,%p,%ld)\n",iface,psi,size);
866 assert(This->pae != NULL);
869 return AVIERR_BADPARAM;
871 return AVIERR_BADSIZE;
873 if (This->pae->bDecompress)
874 This->pae->sInfo.fccHandler = 0;
876 memcpy(psi, &This->pae->sInfo, min((DWORD)size, sizeof(This->pae->sInfo)));
878 if ((DWORD)size < sizeof(This->pae->sInfo))
879 return AVIERR_BUFFERTOOSMALL;
883 static LONG WINAPI IEditAVIStream_fnFindSample(IAVIStream*iface,LONG pos,
886 IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
888 DWORD streamPos, streamNr;
890 TRACE("(%p,%ld,0x%08lX)\n",iface,pos,flags);
892 if (flags & FIND_FROM_START)
893 pos = (LONG)This->sInfo.dwStart;
895 /* outside of stream? */
896 if (pos < (LONG)This->sInfo.dwStart ||
897 (LONG)This->sInfo.dwStart + (LONG)This->sInfo.dwLength <= pos)
900 /* map our position to a stream and position in it */
901 if (AVIFILE_FindStreamInTable(This, pos, &stream, &streamPos,
903 return -1; /* doesn't exist */
905 if (This->bDecompress) {
906 /* only one stream -- format changes only at start */
907 if (flags & FIND_FORMAT)
908 return (flags & FIND_NEXT ? -1 : 0);
910 /* FIXME: map positions back to us */
911 return IAVIStream_FindSample(stream, streamPos, flags);
913 /* assume change of format every frame */
918 static HRESULT WINAPI IEditAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,
919 LPVOID format,LONG*fmtsize)
921 IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
922 LPBITMAPINFOHEADER lp;
927 TRACE("(%p,%ld,%p,%p)\n",iface,pos,format,fmtsize);
929 if (fmtsize == NULL || pos < This->sInfo.dwStart ||
930 This->sInfo.dwStart + This->sInfo.dwLength <= pos)
931 return AVIERR_BADPARAM;
933 /* find stream corresponding to position */
934 hr = AVIFILE_FindStreamInTable(This, pos, &stream, &n, NULL, FALSE);
938 if (! This->bDecompress)
939 return IAVIStream_ReadFormat(stream, n, format, fmtsize);
941 lp = (LPBITMAPINFOHEADER)AVIFILE_ReadFrame(This, stream, n);
944 if (lp->biBitCount <= 8) {
945 n = (lp->biClrUsed > 0 ? lp->biClrUsed : 1 << lp->biBitCount);
946 n *= sizeof(RGBQUAD);
951 memcpy(format, lp, min((LONG)n, *fmtsize));
952 hr = ((LONG)n > *fmtsize ? AVIERR_BUFFERTOOSMALL : AVIERR_OK);
958 static HRESULT WINAPI IEditAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,
959 LPVOID format,LONG formatsize)
961 TRACE("(%p,%ld,%p,%ld)\n",iface,pos,format,formatsize);
963 return AVIERR_UNSUPPORTED;
966 static HRESULT WINAPI IEditAVIStream_fnRead(IAVIStream*iface,LONG start,
967 LONG samples,LPVOID buffer,
968 LONG buffersize,LONG*bytesread,
971 IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
973 DWORD streamPos, streamNr;
974 LONG readBytes, readSamples, count;
977 TRACE("(%p,%ld,%ld,%p,%ld,%p,%p) -- 0x%08lX\n",iface,start,samples,
978 buffer,buffersize,bytesread,samplesread,This->sInfo.fccType);
980 /* check parameters */
981 if (bytesread != NULL)
983 if (samplesread != NULL)
986 return AVIERR_BADSIZE;
987 if ((DWORD)start < This->sInfo.dwStart ||
988 This->sInfo.dwStart + This->sInfo.dwLength < (DWORD)start)
989 return AVIERR_BADPARAM;
991 if (! This->bDecompress) {
992 /* audio like data -- sample-based */
995 return AVIERR_OK; /* nothing at all or already done */
997 if (FAILED(AVIFILE_FindStreamInTable(This, start, &stream,
998 &streamPos, &streamNr, FALSE)))
1001 /* limit to end of the stream */
1003 if (streamPos + count > EditStreamEnd(This, streamNr))
1004 count = EditStreamEnd(This, streamNr) - streamPos;
1006 hr = IAVIStream_Read(stream, streamPos, count, buffer, buffersize,
1007 &readBytes, &readSamples);
1010 if (readBytes == 0 && readSamples == 0 && count != 0)
1011 return AVIERR_FILEREAD; /* for bad stream implementations */
1013 if (samplesread != NULL)
1014 *samplesread += readSamples;
1015 if (bytesread != NULL)
1016 *bytesread += readBytes;
1017 if (buffer != NULL) {
1018 buffer = ((LPBYTE)buffer)+readBytes;
1019 buffersize -= readBytes;
1023 } while (This->sInfo.dwStart + This->sInfo.dwLength > start);
1025 /* video like data -- frame-based */
1026 LPBITMAPINFOHEADER lp;
1031 if (FAILED(AVIFILE_FindStreamInTable(This, start, &stream,
1032 &streamPos, &streamNr, FALSE)))
1033 return AVIERR_ERROR;
1035 lp = AVIFILE_ReadFrame(This, stream, streamPos);
1037 return AVIERR_ERROR;
1039 if (buffer != NULL) {
1040 /* need size of format to skip */
1041 if (lp->biBitCount <= 8) {
1042 count = lp->biClrUsed > 0 ? lp->biClrUsed : 1 << lp->biBitCount;
1043 count *= sizeof(RGBQUAD);
1046 count += lp->biSize;
1048 if (buffersize < lp->biSizeImage)
1049 return AVIERR_BUFFERTOOSMALL;
1050 memcpy(buffer, (LPBYTE)lp + count, lp->biSizeImage);
1053 if (bytesread != NULL)
1054 *bytesread = lp->biSizeImage;
1055 if (samplesread != NULL)
1062 static HRESULT WINAPI IEditAVIStream_fnWrite(IAVIStream*iface,LONG start,
1063 LONG samples,LPVOID buffer,
1064 LONG buffersize,DWORD flags,
1065 LONG*sampwritten,LONG*byteswritten)
1067 TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n",iface,start,samples,buffer,
1068 buffersize,flags,sampwritten,byteswritten);
1070 /* be sure return parameters have correct values */
1071 if (sampwritten != NULL)
1073 if (byteswritten != NULL)
1076 return AVIERR_UNSUPPORTED;
1079 static HRESULT WINAPI IEditAVIStream_fnDelete(IAVIStream*iface,LONG start,
1082 IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
1084 TRACE("(%p,%ld,%ld)\n",iface,start,samples);
1086 return IAVIEditStream_Cut((IAVIEditStream*)This->pae,&start,&samples,NULL);
1089 static HRESULT WINAPI IEditAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,
1090 LPVOID lp,LONG *lpread)
1092 IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
1095 TRACE("(%p,0x%08lX,%p,%p)\n",iface,fcc,lp,lpread);
1097 /* check parameters */
1098 if (lp == NULL || lpread == NULL)
1099 return AVIERR_BADPARAM;
1101 /* simply ask every stream and return the first block found */
1102 for (n = 0; n < This->nStreams; n++) {
1103 HRESULT hr = IAVIStream_ReadData(This->pStreams[n].pStream,fcc,lp,lpread);
1110 return AVIERR_NODATA;
1113 static HRESULT WINAPI IEditAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,
1114 LPVOID lp,LONG size)
1116 TRACE("(%p,0x%08lX,%p,%ld)\n",iface,fcc,lp,size);
1118 return AVIERR_UNSUPPORTED;
1121 static HRESULT WINAPI IEditAVIStream_fnSetInfo(IAVIStream*iface,
1122 AVISTREAMINFOW*info,LONG len)
1124 IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
1126 TRACE("(%p,%p,%ld)\n",iface,info,len);
1128 return IAVIEditStream_SetInfo((IAVIEditStream*)This->pae,info,len);
1131 static HRESULT WINAPI IEditStreamInternal_fnQueryInterface(IEditStreamInternal*iface,REFIID refiid,LPVOID*obj)
1133 IEditStreamInternalImpl *This = (IEditStreamInternalImpl *)iface;
1135 assert(This->pae != NULL);
1137 return IAVIEditStream_QueryInterface((IAVIEditStream*)This->pae, refiid, obj);
1140 static ULONG WINAPI IEditStreamInternal_fnAddRef(IEditStreamInternal*iface)
1142 IEditStreamInternalImpl *This = (IEditStreamInternalImpl *)iface;
1144 assert(This->pae != NULL);
1146 return IAVIEditStream_AddRef((IAVIEditStream*)This->pae);
1149 static ULONG WINAPI IEditStreamInternal_fnRelease(IEditStreamInternal*iface)
1151 IEditStreamInternalImpl *This = (IEditStreamInternalImpl *)iface;
1153 assert(This->pae != NULL);
1155 return IAVIEditStream_Release((IAVIEditStream*)This->pae);
1158 static HRESULT WINAPI IEditStreamInternal_fnGetEditStreamImpl(IEditStreamInternal*iface,LPVOID*ppimpl)
1160 IEditStreamInternalImpl *This = (IEditStreamInternalImpl *)iface;
1162 TRACE("(%p,%p) -> %p\n", iface, ppimpl, This->pae);
1164 assert(This->pae != NULL);
1165 assert(ppimpl != NULL);
1167 *ppimpl = This->pae;