Get rid of the no longer used ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
[wine/dcerpc.git] / dlls / avifil32 / editstream.c
blobdc18f9c0adf924f03a31002aa10892b9e4b2e5a7
1 /*
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
20 #include <assert.h>
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "winnls.h"
28 #include "winerror.h"
29 #include "windowsx.h"
30 #include "mmsystem.h"
31 #include "vfw.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 */
49 } EditStreamTable;
51 #define INTERFACE IEditStreamInternal
52 #define IEditStreamInternal_METHODS \
53 IUnknown_METHODS \
54 STDMETHOD(GetEditStreamImpl)(THIS_ LPVOID*) PURE;
55 DECLARE_INTERFACE_(IEditStreamInternal, IUnknown) { IEditStreamInternal_METHODS };
56 #undef INTERFACE
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,
74 PAVISTREAM*ppResult);
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,
82 IAVIEditStream_fnCut,
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,
95 LONG flags);
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,
101 LONG*samplesread);
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 {
145 /* IUnknown stuff */
146 IAVIStreamVtbl *lpVtbl;
148 /* IAVIStream stuff */
149 IAVIEditStreamImpl *pae;
150 } IEditAVIStreamImpl;
152 typedef struct _IEditStreamInternalImpl {
153 /* IUnknown stuff */
154 IEditStreamInternalVtbl *lpVtbl;
156 /* IEditStreamInternal stuff */
157 IAVIEditStreamImpl *pae;
158 } IEditStreamInternalImpl;
160 struct _IAVIEditStreamImpl {
161 /* IUnknown stuff */
162 IAVIEditStreamVtbl *lpVtbl;
163 DWORD ref;
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 */
175 BOOL bDecompress;
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));
188 if (pedit == NULL)
189 return NULL;
191 pedit->lpVtbl = &ieditstream;
192 pedit->iAVIStream.lpVtbl = &ieditstast;
193 pedit->iAVIStream.pae = pedit;
194 pedit->iEditStreamInternal.lpVtbl = &ieditstreaminternal;
195 pedit->iEditStreamInternal.pae = pedit;
196 pedit->ref = 1;
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,
205 DWORD* streamPos,
206 DWORD* streamNr,BOOL bFindSample)
208 DWORD n;
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)
222 *streamNr = n;
224 return AVIERR_OK;
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)
232 *streamNr = n;
234 TRACE(" -- pos=0 && b=1 -> (%p,%lu,%lu)\n",*ppStream, *streamPos, n);
235 return AVIERR_OK;
236 } else {
237 *ppStream = NULL;
238 *streamPos = 0;
239 if (streamNr != NULL)
240 *streamNr = 0;
242 TRACE(" -> ERROR (NULL,0,0)\n");
243 return AVIERR_BADPARAM;
247 static LPVOID AVIFILE_ReadFrame(IAVIEditStreamImpl* const This,
248 PAVISTREAM pstream, LONG pos)
250 PGETFRAME pg;
252 TRACE("(%p,%p,%ld)\n",This,pstream,pos);
254 if (pstream == NULL)
255 return NULL;
257 /* if stream changes make sure that only palette changes */
258 if (This->pCurStream != pstream) {
259 pg = AVIStreamGetFrameOpen(pstream, NULL);
260 if (pg == NULL)
261 return 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");
266 return NULL;
268 AVIStreamGetFrameClose(This->pg);
270 This->pg = 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);
287 /* remove part nr */
288 IAVIStream_Release(This->pStreams[nr].pStream);
289 This->nStreams--;
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);
307 return AVIERR_OK;
310 static BOOL AVIFILE_FormatsEqual(PAVISTREAM avi1, PAVISTREAM avi2)
312 LPVOID fmt1 = NULL, fmt2 = NULL;
313 LONG size1, size2, start1, start2;
314 BOOL status = FALSE;
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)))
322 return FALSE;
323 if (FAILED(AVIStreamFormatSize(avi2, start2, &size2)))
324 return FALSE;
325 if (size1 != size2)
326 return FALSE;
328 /* sizes match, now get formats and compare them */
329 fmt1 = GlobalAllocPtr(GHND, size1);
330 if (fmt1 == NULL)
331 return FALSE;
332 if (SUCCEEDED(AVIStreamReadFormat(avi1, start1, fmt1, &size1))) {
333 fmt2 = GlobalAllocPtr(GHND, size1);
334 if (fmt2 != NULL) {
335 if (SUCCEEDED(AVIStreamReadFormat(avi2, start2, fmt2, &size1)))
336 status = (memcmp(fmt1, fmt2, size1) == 0);
340 if (fmt2 != NULL)
341 GlobalFreePtr(fmt2);
342 GlobalFreePtr(fmt1);
344 return status;
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)) {
357 *obj = iface;
358 IAVIEditStream_AddRef(iface);
360 return S_OK;
361 } else if (IsEqualGUID(&IID_IAVIStream, refiid)) {
362 *obj = &This->iAVIStream;
363 IAVIEditStream_AddRef(iface);
365 return S_OK;
366 } else if (IsEqualGUID(&IID_IEditStreamInternal, refiid)) {
367 *obj = &This->iEditStreamInternal;
368 IAVIEditStream_AddRef(iface);
370 return S_OK;
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);
387 DWORD i;
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);
404 return 0;
406 return This->ref;
409 static HRESULT WINAPI IAVIEditStream_fnCut(IAVIEditStream*iface,LONG*plStart,
410 LONG*plLength,PAVISTREAM*ppResult)
412 ICOM_THIS(IAVIEditStreamImpl,iface);
413 PAVISTREAM stream;
414 DWORD start, len, streamPos, streamNr;
415 HRESULT hr;
417 TRACE("(%p,%p,%p,%p)\n",iface,plStart,plLength,ppResult);
419 if (ppResult != NULL)
420 *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);
427 if (FAILED(hr))
428 return hr;
431 start = *plStart;
432 len = *plLength;
434 /* now delete the requested part */
435 while (len > 0) {
436 hr = AVIFILE_FindStreamInTable(This, start, &stream,
437 &streamPos, &streamNr, FALSE);
438 if (FAILED(hr))
439 return hr;
440 if (This->pStreams[streamNr].dwStart == streamPos) {
441 /* deleting from start of part */
442 if (len < This->pStreams[streamNr].dwLength) {
443 start += len;
444 This->pStreams[streamNr].dwStart += len;
445 This->pStreams[streamNr].dwLength -= len;
446 This->sInfo.dwLength -= len;
447 len = 0;
449 /* we must return decompressed data now */
450 This->bDecompress = TRUE;
451 } else {
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;
460 len -= count;
461 This->pStreams[streamNr].dwLength =
462 streamPos - This->pStreams[streamNr].dwStart;
463 } else {
464 /* splitting */
465 if (This->nStreams + 1 >= This->nTableSize) {
466 This->pStreams =
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));
474 This->nStreams++;
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;
484 len = 0;
488 This->sInfo.dwEditCount++;
490 return AVIERR_OK;
493 static HRESULT WINAPI IAVIEditStream_fnCopy(IAVIEditStream*iface,LONG*plStart,
494 LONG*plLength,PAVISTREAM*ppResult)
496 ICOM_THIS(IAVIEditStreamImpl,iface);
497 IAVIEditStreamImpl* pEdit;
498 HRESULT hr;
499 LONG start = 0;
501 TRACE("(%p,%p,%p,%p)\n",iface,plStart,plLength,ppResult);
503 if (ppResult == NULL)
504 return AVIERR_BADPARAM;
505 *ppResult = NULL;
506 if (plStart == NULL || plLength == NULL || *plStart < 0 || *plLength < 0)
507 return AVIERR_BADPARAM;
509 /* check bounds */
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;
515 if (*plLength < 0)
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 -
520 *(LPDWORD)plStart;
522 pEdit = (IAVIEditStreamImpl*)AVIFILE_CreateEditStream(NULL);
523 if (pEdit == NULL)
524 return AVIERR_MEMORY;
526 hr = IAVIEditStream_Paste((PAVIEDITSTREAM)pEdit,&start,plLength,
527 (PAVISTREAM)&This->iAVIStream,*plStart,
528 *plStart + *plLength);
529 *plStart = start;
530 if (FAILED(hr))
531 IAVIEditStream_Release((PAVIEDITSTREAM)pEdit);
532 else
533 *ppResult = (PAVISTREAM)&pEdit->iAVIStream;
535 return hr;
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;
546 PAVISTREAM pStream;
547 DWORD startPos, endPos, streamNr, n, nStreams;
549 TRACE("(%p,%p,%p,%p,%ld,%ld)\n",iface,plStart,plLength,
550 pSource,lStart,lLength);
552 if (pSource == NULL)
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))))
559 return AVIERR_ERROR;
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) {
579 DWORD size;
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;
590 } else {
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... */
620 if (pEdit != NULL) {
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 */
641 if (pEdit != NULL) {
642 DWORD nLastStream;
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)
649 nLastStream++;
651 nStreams = nLastStream - streamNr;
652 } else
653 nStreams = 1;
654 if (This->nStreams + nStreams + 1 > This->nTableSize) {
655 n = This->nStreams + nStreams + 33;
657 This->pStreams =
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)
665 *plLength = lLength;
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);
683 streamNr++;
684 } else {
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;
692 if (pEdit != NULL) {
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;
708 } else {
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;
727 return AVIERR_OK;
730 static HRESULT WINAPI IAVIEditStream_fnClone(IAVIEditStream*iface,
731 PAVISTREAM*ppResult)
733 ICOM_THIS(IAVIEditStreamImpl,iface);
734 IAVIEditStreamImpl* pEdit;
735 DWORD i;
737 TRACE("(%p,%p)\n",iface,ppResult);
739 if (ppResult == NULL)
740 return AVIERR_BADPARAM;
741 *ppResult = NULL;
743 pEdit = (IAVIEditStreamImpl*)AVIFILE_CreateEditStream(NULL);
744 if (pEdit == 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;
763 return AVIERR_OK;
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 */
774 if (asi == NULL)
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)
780 return AVIERR_ERROR;
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++;
795 return AVIERR_OK;
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;
831 if (lParam2 != 0)
832 return AVIERR_ERROR;
834 if (This->pStreams == NULL) {
835 This->pStreams =
836 GlobalAllocPtr(GMEM_SHARE|GHND, 256 * sizeof(EditStreamTable));
837 if (This->pStreams == NULL)
838 return AVIERR_MEMORY;
839 This->nTableSize = 256;
842 if (lParam1 != 0) {
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;
848 This->nStreams = 1;
850 return AVIERR_OK;
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);
862 if (psi == NULL)
863 return AVIERR_BADPARAM;
864 if (size < 0)
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;
874 return AVIERR_OK;
877 static LONG WINAPI IEditAVIStream_fnFindSample(IAVIStream*iface,LONG pos,
878 LONG flags)
880 IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
881 PAVISTREAM stream;
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)
892 return -1;
894 /* map our position to a stream and position in it */
895 if (AVIFILE_FindStreamInTable(This, pos, &stream, &streamPos,
896 &streamNr, TRUE))
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);
906 } else {
907 /* assume change of format every frame */
908 return pos;
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;
917 PAVISTREAM stream;
918 DWORD n;
919 HRESULT hr;
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);
929 if (FAILED(hr))
930 return hr;
932 if (! This->bDecompress)
933 return IAVIStream_ReadFormat(stream, n, format, fmtsize);
935 lp = (LPBITMAPINFOHEADER)AVIFILE_ReadFrame(This, stream, n);
936 if (lp == NULL)
937 return AVIERR_ERROR;
938 if (lp->biBitCount <= 8) {
939 n = (lp->biClrUsed > 0 ? lp->biClrUsed : 1 << lp->biBitCount);
940 n *= sizeof(RGBQUAD);
941 } else
942 n = 0;
943 n += lp->biSize;
945 memcpy(format, lp, min((LONG)n, *fmtsize));
946 hr = ((LONG)n > *fmtsize ? AVIERR_BUFFERTOOSMALL : AVIERR_OK);
947 *fmtsize = n;
949 return hr;
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,
963 LONG*samplesread)
965 IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
966 PAVISTREAM stream;
967 DWORD streamPos, streamNr;
968 LONG readBytes, readSamples, count;
969 HRESULT hr;
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)
976 *bytesread = 0;
977 if (samplesread != NULL)
978 *samplesread = 0;
979 if (buffersize < 0)
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 */
987 do {
988 if (samples == 0)
989 return AVIERR_OK; /* nothing at all or already done */
991 if (FAILED(AVIFILE_FindStreamInTable(This, start, &stream,
992 &streamPos, &streamNr, FALSE)))
993 return AVIERR_ERROR;
995 /* limit to end of the stream */
996 count = samples;
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);
1002 if (FAILED(hr))
1003 return hr;
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;
1015 start += count;
1016 samples -= count;
1017 } while (This->sInfo.dwStart + This->sInfo.dwLength > start);
1018 } else {
1019 /* video like data -- frame-based */
1020 LPBITMAPINFOHEADER lp;
1022 if (samples == 0)
1023 return AVIERR_OK;
1025 if (FAILED(AVIFILE_FindStreamInTable(This, start, &stream,
1026 &streamPos, &streamNr, FALSE)))
1027 return AVIERR_ERROR;
1029 lp = AVIFILE_ReadFrame(This, stream, streamPos);
1030 if (lp == NULL)
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);
1038 } else
1039 count = 0;
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)
1050 *samplesread = 1;
1053 return AVIERR_OK;
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)
1066 *sampwritten = 0;
1067 if (byteswritten != NULL)
1068 *byteswritten = 0;
1070 return AVIERR_UNSUPPORTED;
1073 static HRESULT WINAPI IEditAVIStream_fnDelete(IAVIStream*iface,LONG start,
1074 LONG samples)
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;
1087 DWORD n;
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);
1099 if (SUCCEEDED(hr))
1100 return hr;
1103 *lpread = 0;
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;
1162 return AVIERR_OK;