ntdll: The closing brace of a guid string is at index 37.
[wine] / dlls / avifil32 / editstream.c
1 /*
2  * Copyright 2003 Michael Günnewig
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #define COM_NO_WINDOWS_H
20 #include <assert.h>
21 #include <stdarg.h>
22
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 "mmsystem.h"
30 #include "vfw.h"
31
32 #include "avifile_private.h"
33 #include "extrachunk.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
38
39 /***********************************************************************/
40
41 /* internal interface to get access to table of stream in an editable stream */
42
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 */
47 } EditStreamTable;
48
49 #define INTERFACE IEditStreamInternal
50 DECLARE_INTERFACE_(IEditStreamInternal,IUnknown)
51 {
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;
58 };
59 #undef INTERFACE
60
61 #define EditStreamEnd(This,streamNr) ((This)->pStreams[streamNr].dwStart + \
62                                       (This)->pStreams[streamNr].dwLength)
63
64 /***********************************************************************/
65
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,
77                                              PAVISTREAM*ppResult);
78 static HRESULT WINAPI IAVIEditStream_fnSetInfo(IAVIEditStream*iface,
79                                                LPAVISTREAMINFOW asi,LONG size);
80
81 static const struct IAVIEditStreamVtbl ieditstream = {
82   IAVIEditStream_fnQueryInterface,
83   IAVIEditStream_fnAddRef,
84   IAVIEditStream_fnRelease,
85   IAVIEditStream_fnCut,
86   IAVIEditStream_fnCopy,
87   IAVIEditStream_fnPaste,
88   IAVIEditStream_fnClone,
89   IAVIEditStream_fnSetInfo
90 };
91
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,
98                                                   LONG flags);
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,
104                                             LONG*samplesread);
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);
115
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
131 };
132
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);
137
138 static const struct IEditStreamInternalVtbl ieditstreaminternal = {
139   IEditStreamInternal_fnQueryInterface,
140   IEditStreamInternal_fnAddRef,
141   IEditStreamInternal_fnRelease,
142   IEditStreamInternal_fnGetEditStreamImpl
143 };
144
145 typedef struct _IAVIEditStreamImpl IAVIEditStreamImpl;
146
147 typedef struct _IEditAVIStreamImpl {
148   /* IUnknown stuff */
149   const IAVIStreamVtbl *lpVtbl;
150
151   /* IAVIStream stuff */
152   IAVIEditStreamImpl *pae;
153 } IEditAVIStreamImpl;
154
155 typedef struct _IEditStreamInternalImpl {
156   /* IUnknown stuff */
157   const IEditStreamInternalVtbl *lpVtbl;
158
159   /* IEditStreamInternal stuff */
160   IAVIEditStreamImpl *pae;
161 } IEditStreamInternalImpl;
162
163 struct _IAVIEditStreamImpl {
164   /* IUnknown stuff */
165   const IAVIEditStreamVtbl *lpVtbl;
166   LONG  ref;
167
168   /* IAVIEditStream stuff */
169   IEditAVIStreamImpl      iAVIStream;
170   IEditStreamInternalImpl iEditStreamInternal;
171
172   AVISTREAMINFOW       sInfo;
173
174   EditStreamTable     *pStreams;
175   DWORD                nStreams;   /* current fill level of pStreams table */
176   DWORD                nTableSize; /* size of pStreams table */
177
178   BOOL                 bDecompress;
179   PAVISTREAM           pCurStream;
180   PGETFRAME            pg;         /* IGetFrame for pCurStream */
181   LPBITMAPINFOHEADER   lpFrame;    /* frame of pCurStream */
182 };
183
184 /***********************************************************************/
185
186 PAVIEDITSTREAM AVIFILE_CreateEditStream(PAVISTREAM pstream)
187 {
188   IAVIEditStreamImpl *pedit = NULL;
189
190   pedit = HeapAlloc(GetProcessHeap(), 0, sizeof(IAVIEditStreamImpl));
191   if (pedit == NULL)
192     return NULL;
193
194   pedit->lpVtbl            = &ieditstream;
195   pedit->iAVIStream.lpVtbl = &ieditstast;
196   pedit->iAVIStream.pae    = pedit;
197   pedit->iEditStreamInternal.lpVtbl = &ieditstreaminternal;
198   pedit->iEditStreamInternal.pae    = pedit;
199   pedit->ref = 1;
200
201   IAVIStream_Create((PAVISTREAM)&pedit->iAVIStream,(LPARAM)pstream,0);
202
203   return (PAVIEDITSTREAM)pedit;
204 }
205
206 static HRESULT AVIFILE_FindStreamInTable(IAVIEditStreamImpl* const This,
207                                          DWORD pos,PAVISTREAM *ppStream,
208                                          DWORD* streamPos,
209                                          DWORD* streamNr,BOOL bFindSample)
210 {
211   DWORD n;
212
213   TRACE("(%p,%lu,%p,%p,%p,%d)\n",This,pos,ppStream,streamPos,
214         streamNr,bFindSample);
215
216   if (pos < This->sInfo.dwStart)
217     return AVIERR_BADPARAM;
218
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)
225         *streamNr = n;
226
227       return AVIERR_OK;
228     }
229     pos -= This->pStreams[n].dwLength;
230   }
231   if (pos == 0 && bFindSample) {
232     *ppStream  = This->pStreams[--n].pStream;
233     *streamPos = EditStreamEnd(This, n);
234     if (streamNr != NULL)
235       *streamNr = n;
236
237     TRACE(" -- pos=0 && b=1 -> (%p,%lu,%lu)\n",*ppStream, *streamPos, n);
238     return AVIERR_OK;
239   } else {
240     *ppStream = NULL;
241     *streamPos = 0;
242     if (streamNr != NULL)
243       *streamNr = 0;
244
245     TRACE(" -> ERROR (NULL,0,0)\n");
246     return AVIERR_BADPARAM;
247   }
248 }
249
250 static LPVOID AVIFILE_ReadFrame(IAVIEditStreamImpl* const This,
251                                 PAVISTREAM pstream, LONG pos)
252 {
253   PGETFRAME pg;
254
255   TRACE("(%p,%p,%ld)\n",This,pstream,pos);
256
257   if (pstream == NULL)
258     return NULL;
259
260   /* if stream changes make sure that only palette changes */
261   if (This->pCurStream != pstream) {
262     pg = AVIStreamGetFrameOpen(pstream, NULL);
263     if (pg == NULL)
264       return 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");
269         return NULL;
270       }
271       AVIStreamGetFrameClose(This->pg);
272     }
273     This->pg         = pg;
274     This->pCurStream = pstream;
275   }
276
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;
281
282   return This->lpFrame;
283 }
284
285 static HRESULT AVIFILE_RemoveStream(IAVIEditStreamImpl* const This, DWORD nr)
286 {
287   assert(This != NULL);
288   assert(nr < This->nStreams);
289
290   /* remove part nr */
291   IAVIStream_Release(This->pStreams[nr].pStream);
292   This->nStreams--;
293   if (This->nStreams - nr > 0) {
294     memmove(This->pStreams + nr, This->pStreams + nr + 1,
295             (This->nStreams - nr) * sizeof(EditStreamTable));
296   }
297   This->pStreams[This->nStreams].pStream  = NULL;
298   This->pStreams[This->nStreams].dwStart  = 0;
299   This->pStreams[This->nStreams].dwLength = 0;
300
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);
307     }
308   }
309
310   return AVIERR_OK;
311 }
312
313 static BOOL AVIFILE_FormatsEqual(PAVISTREAM avi1, PAVISTREAM avi2)
314 {
315   LPVOID fmt1 = NULL, fmt2 = NULL;
316   LONG size1, size2, start1, start2;
317   BOOL status = FALSE;
318
319   assert(avi1 != NULL && avi2 != NULL);
320
321   /* get stream starts and check format sizes */
322   start1 = AVIStreamStart(avi1);
323   start2 = AVIStreamStart(avi2);
324   if (FAILED(AVIStreamFormatSize(avi1, start1, &size1)))
325     return FALSE;
326   if (FAILED(AVIStreamFormatSize(avi2, start2, &size2)))
327     return FALSE;
328   if (size1 != size2)
329     return FALSE;
330
331   /* sizes match, now get formats and compare them */
332   fmt1 = HeapAlloc(GetProcessHeap(), 0, size1);
333   if (fmt1 == NULL)
334     return FALSE;
335   if (SUCCEEDED(AVIStreamReadFormat(avi1, start1, fmt1, &size1))) {
336     fmt2 = HeapAlloc(GetProcessHeap(), 0, size1);
337     if (fmt2 != NULL) {
338       if (SUCCEEDED(AVIStreamReadFormat(avi2, start2, fmt2, &size1)))
339         status = (memcmp(fmt1, fmt2, size1) == 0);
340     }
341   }
342
343   if (fmt2 != NULL)
344     HeapFree(GetProcessHeap(), 0, fmt2);
345   HeapFree(GetProcessHeap(), 0, fmt1);
346
347   return status;
348 }
349
350 /***********************************************************************/
351
352 static HRESULT WINAPI IAVIEditStream_fnQueryInterface(IAVIEditStream*iface,REFIID refiid,LPVOID *obj)
353 {
354   IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
355
356   TRACE("(%p,%s,%p)\n", This, debugstr_guid(refiid), obj);
357
358   if (IsEqualGUID(&IID_IUnknown, refiid) ||
359       IsEqualGUID(&IID_IAVIEditStream, refiid)) {
360     *obj = iface;
361     IAVIEditStream_AddRef(iface);
362
363     return S_OK;
364   } else if (IsEqualGUID(&IID_IAVIStream, refiid)) {
365     *obj = &This->iAVIStream;
366     IAVIEditStream_AddRef(iface);
367
368     return S_OK;
369   } else if (IsEqualGUID(&IID_IEditStreamInternal, refiid)) {
370     *obj = &This->iEditStreamInternal;
371     IAVIEditStream_AddRef(iface);
372
373     return S_OK;
374   }
375
376   return OLE_E_ENUM_NOMORE;
377 }
378
379 static ULONG   WINAPI IAVIEditStream_fnAddRef(IAVIEditStream*iface)
380 {
381   IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
382   ULONG ref = InterlockedIncrement(&This->ref);
383
384   TRACE("(%p) -> %ld\n", iface, ref);
385
386   return ref;
387 }
388
389 static ULONG   WINAPI IAVIEditStream_fnRelease(IAVIEditStream*iface)
390 {
391   IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
392   DWORD i;
393   ULONG ref = InterlockedDecrement(&This->ref);
394
395   TRACE("(%p) -> %ld\n", iface, ref);
396
397   if (!ref) {
398     /* release memory */
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);
405       }
406       HeapFree(GetProcessHeap(), 0, This->pStreams);
407     }
408
409     HeapFree(GetProcessHeap(), 0, This);
410     return 0;
411   }
412   return ref;
413 }
414
415 static HRESULT WINAPI IAVIEditStream_fnCut(IAVIEditStream*iface,LONG*plStart,
416                                            LONG*plLength,PAVISTREAM*ppResult)
417 {
418   IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
419   PAVISTREAM stream;
420   DWORD      start, len, streamPos, streamNr;
421   HRESULT    hr;
422
423   TRACE("(%p,%p,%p,%p)\n",iface,plStart,plLength,ppResult);
424
425   if (ppResult != NULL)
426     *ppResult = NULL;
427   if (plStart == NULL || plLength == NULL || *plStart < 0)
428     return AVIERR_BADPARAM;
429
430   /* if asked for cutted part copy it before deleting */
431   if (ppResult != NULL) {
432     hr = IAVIEditStream_Copy(iface, plStart, plLength, ppResult);
433     if (FAILED(hr))
434       return hr;
435   }
436
437   start = *plStart;
438   len   = *plLength;
439
440   /* now delete the requested part */
441   while (len > 0) {
442     hr = AVIFILE_FindStreamInTable(This, start, &stream,
443                                    &streamPos, &streamNr, FALSE);
444     if (FAILED(hr))
445       return hr;
446     if (This->pStreams[streamNr].dwStart == streamPos) {
447       /* deleting from start of part */
448       if (len < This->pStreams[streamNr].dwLength) {
449         start += len;
450         This->pStreams[streamNr].dwStart  += len;
451         This->pStreams[streamNr].dwLength -= len;
452         This->sInfo.dwLength -= len;
453         len = 0;
454
455         /* we must return decompressed data now */
456         This->bDecompress = TRUE;
457       } else {
458         /* deleting hole part */
459         len -= This->pStreams[streamNr].dwLength;
460         AVIFILE_RemoveStream(This,streamNr);
461       }
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;
466       len                  -= count;
467       This->pStreams[streamNr].dwLength =
468         streamPos - This->pStreams[streamNr].dwStart;
469     } else {
470       /* splitting */
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;
477       }
478       memmove(This->pStreams + streamNr + 1, This->pStreams + streamNr,
479               (This->nStreams - streamNr) * sizeof(EditStreamTable));
480       This->nStreams++;
481
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;
486
487       This->pStreams[streamNr].dwLength =
488         streamPos - This->pStreams[streamNr].dwStart;
489       This->sInfo.dwLength -= len;
490       len = 0;
491     }
492   }
493
494   This->sInfo.dwEditCount++;
495
496   return AVIERR_OK;
497 }
498
499 static HRESULT WINAPI IAVIEditStream_fnCopy(IAVIEditStream*iface,LONG*plStart,
500                                             LONG*plLength,PAVISTREAM*ppResult)
501 {
502   IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
503   IAVIEditStreamImpl* pEdit;
504   HRESULT hr;
505   LONG start = 0;
506
507   TRACE("(%p,%p,%p,%p)\n",iface,plStart,plLength,ppResult);
508
509   if (ppResult == NULL)
510     return AVIERR_BADPARAM;
511   *ppResult = NULL;
512   if (plStart == NULL || plLength == NULL || *plStart < 0 || *plLength < 0)
513     return AVIERR_BADPARAM;
514
515   /* check bounds */
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;
521     if (*plLength < 0)
522       return AVIERR_BADPARAM;
523   }
524   if (*(LPDWORD)plStart + *(LPDWORD)plLength > This->sInfo.dwStart + This->sInfo.dwLength)
525     *(LPDWORD)plLength = This->sInfo.dwStart + This->sInfo.dwLength -
526       *(LPDWORD)plStart;
527
528   pEdit = (IAVIEditStreamImpl*)AVIFILE_CreateEditStream(NULL);
529   if (pEdit == NULL)
530     return AVIERR_MEMORY;
531
532   hr = IAVIEditStream_Paste((PAVIEDITSTREAM)pEdit,&start,plLength,
533                             (PAVISTREAM)&This->iAVIStream,*plStart,
534                             *plStart + *plLength);
535   *plStart = start;
536   if (FAILED(hr))
537     IAVIEditStream_Release((PAVIEDITSTREAM)pEdit);
538   else
539     *ppResult = (PAVISTREAM)&pEdit->iAVIStream;
540
541   return hr;
542 }
543
544 static HRESULT WINAPI IAVIEditStream_fnPaste(IAVIEditStream*iface,LONG*plStart,
545                                              LONG*plLength,PAVISTREAM pSource,
546                                              LONG lStart,LONG lLength)
547 {
548   IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
549   AVISTREAMINFOW      srcInfo;
550   IEditStreamInternal*pInternal = NULL;
551   IAVIEditStreamImpl *pEdit = NULL;
552   PAVISTREAM          pStream;
553   DWORD               startPos, endPos, streamNr, nStreams;
554   ULONG               n;
555
556   TRACE("(%p,%p,%p,%p,%ld,%ld)\n",iface,plStart,plLength,
557         pSource,lStart,lLength);
558
559   if (pSource == NULL)
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))))
566     return AVIERR_ERROR;
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;
574   }
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;
583
584   /* streamtype specific tests */
585   if (srcInfo.fccType == streamtypeVIDEO) {
586     LONG size;
587
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;
597   } else {
598     /* FIXME: streamtypeMIDI and streamtypeTEXT */
599     return AVIERR_UNSUPPORTED;
600   }
601
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);
607   }
608
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
617        */
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);
625
626         /* Check if we could convert the source streams to the disired format... */
627         if (pEdit != NULL) {
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;
636           }
637         } else if (AVIFILE_ReadFrame(This, pSource, lStart) == NULL)
638           return AVIERR_BADFORMAT;
639
640         This->bDecompress      = TRUE;
641         This->sInfo.fccHandler = 0;
642       }
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? */
646
647   /* Make sure we have enough memory for parts */
648   if (pEdit != NULL) {
649     DWORD nLastStream;
650
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)
656       nLastStream++;
657
658     nStreams = nLastStream - streamNr;
659   } else 
660     nStreams = 1;
661   if (This->nStreams + nStreams + 1 > This->nTableSize) {
662     n = This->nStreams + nStreams + 33;
663
664     This->pStreams = HeapReAlloc(GetProcessHeap(), 0, This->pStreams, n * sizeof(EditStreamTable));
665     if (This->pStreams == NULL)
666       return AVIERR_MEMORY;
667     This->nTableSize = n;
668   }
669
670   if (plLength != NULL)
671     *plLength = lLength;
672
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));
682
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);
689       streamNr++;
690     } else {
691       /* insert before stream at streamNr */
692       memmove(This->pStreams + streamNr + nStreams, This->pStreams + streamNr,
693               (This->nStreams + nStreams - streamNr) * sizeof(EditStreamTable));
694     }
695   } else /* append the streams */
696     streamNr = This->nStreams;
697
698   if (pEdit != NULL) {
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);
703
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;
710     }
711     if (endPos < EditStreamEnd(This, streamNr + nStreams))
712       This->pStreams[streamNr + nStreams].dwLength =
713         endPos - This->pStreams[streamNr + nStreams].dwStart;
714   } else {
715     /* a simple stream */
716     This->pStreams[streamNr].pStream  = pSource;
717     This->pStreams[streamNr].dwStart  = lStart;
718     This->pStreams[streamNr].dwLength = lLength;
719   }
720
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++;
727     }
728   }
729   This->sInfo.dwEditCount++;
730   This->sInfo.dwLength += lLength;
731   This->nStreams += nStreams;
732
733   return AVIERR_OK;
734 }
735
736 static HRESULT WINAPI IAVIEditStream_fnClone(IAVIEditStream*iface,
737                                              PAVISTREAM*ppResult)
738 {
739   IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
740   IAVIEditStreamImpl* pEdit;
741   DWORD i;
742
743   TRACE("(%p,%p)\n",iface,ppResult);
744
745   if (ppResult == NULL)
746     return AVIERR_BADPARAM;
747   *ppResult = NULL;
748
749   pEdit = (IAVIEditStreamImpl*)AVIFILE_CreateEditStream(NULL);
750   if (pEdit == 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;
758   }
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);
766   }
767
768   *ppResult = (PAVISTREAM)&pEdit->iAVIStream;
769
770   return AVIERR_OK;
771 }
772
773 static HRESULT WINAPI IAVIEditStream_fnSetInfo(IAVIEditStream*iface,
774                                                LPAVISTREAMINFOW asi,LONG size)
775 {
776   IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
777
778   TRACE("(%p,%p,%ld)\n",iface,asi,size);
779
780   /* check parameters */
781   if (asi == NULL)
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)
787     return AVIERR_ERROR;
788
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++;
801
802   return AVIERR_OK;
803 }
804
805 static HRESULT WINAPI IEditAVIStream_fnQueryInterface(IAVIStream*iface,
806                                                       REFIID refiid,LPVOID*obj)
807 {
808   IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
809
810   assert(This->pae != NULL);
811
812   return IAVIEditStream_QueryInterface((IAVIEditStream*)This->pae,refiid,obj);
813 }
814
815 static ULONG   WINAPI IEditAVIStream_fnAddRef(IAVIStream*iface)
816 {
817   IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
818
819   assert(This->pae != NULL);
820
821   return IAVIEditStream_AddRef((IAVIEditStream*)This->pae);
822 }
823
824 static ULONG   WINAPI IEditAVIStream_fnRelease(IAVIStream*iface)
825 {
826   IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
827
828   assert(This->pae != NULL);
829
830   return IAVIEditStream_Release((IAVIEditStream*)This->pae);
831 }
832
833 static HRESULT WINAPI IEditAVIStream_fnCreate(IAVIStream*iface,
834                                               LPARAM lParam1,LPARAM lParam2)
835 {
836   IAVIEditStreamImpl *This = ((IEditAVIStreamImpl*)iface)->pae;
837
838   if (lParam2 != 0)
839     return AVIERR_ERROR;
840
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;
846   }
847
848   if (lParam1 != 0) {
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;
854     This->nStreams = 1;
855   }
856   return AVIERR_OK;
857 }
858
859 static HRESULT WINAPI IEditAVIStream_fnInfo(IAVIStream*iface,
860                                             AVISTREAMINFOW *psi,LONG size)
861 {
862   IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
863
864   TRACE("(%p,%p,%ld)\n",iface,psi,size);
865
866   assert(This->pae != NULL);
867
868   if (psi == NULL)
869     return AVIERR_BADPARAM;
870   if (size < 0)
871     return AVIERR_BADSIZE;
872
873   if (This->pae->bDecompress)
874     This->pae->sInfo.fccHandler = 0;
875
876   memcpy(psi, &This->pae->sInfo, min((DWORD)size, sizeof(This->pae->sInfo)));
877
878   if ((DWORD)size < sizeof(This->pae->sInfo))
879     return AVIERR_BUFFERTOOSMALL;
880   return AVIERR_OK;
881 }
882
883 static LONG    WINAPI IEditAVIStream_fnFindSample(IAVIStream*iface,LONG pos,
884                                                   LONG flags)
885 {
886   IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
887   PAVISTREAM stream;
888   DWORD      streamPos, streamNr;
889
890   TRACE("(%p,%ld,0x%08lX)\n",iface,pos,flags);
891
892   if (flags & FIND_FROM_START)
893     pos = (LONG)This->sInfo.dwStart;
894
895   /* outside of stream? */
896   if (pos < (LONG)This->sInfo.dwStart ||
897       (LONG)This->sInfo.dwStart + (LONG)This->sInfo.dwLength <= pos)
898     return -1;
899
900   /* map our position to a stream and position in it */
901   if (AVIFILE_FindStreamInTable(This, pos, &stream, &streamPos,
902                                 &streamNr, TRUE))
903     return -1; /* doesn't exist */
904
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);
909
910     /* FIXME: map positions back to us */
911     return IAVIStream_FindSample(stream, streamPos, flags);
912   } else {
913     /* assume change of format every frame */
914     return pos;
915   }
916 }
917
918 static HRESULT WINAPI IEditAVIStream_fnReadFormat(IAVIStream*iface,LONG pos,
919                                                   LPVOID format,LONG*fmtsize)
920 {
921   IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
922   LPBITMAPINFOHEADER  lp;
923   PAVISTREAM          stream;
924   DWORD               n;
925   HRESULT             hr;
926
927   TRACE("(%p,%ld,%p,%p)\n",iface,pos,format,fmtsize);
928
929   if (fmtsize == NULL || pos < This->sInfo.dwStart ||
930       This->sInfo.dwStart + This->sInfo.dwLength <= pos)
931     return AVIERR_BADPARAM;
932
933   /* find stream corresponding to position */
934   hr = AVIFILE_FindStreamInTable(This, pos, &stream, &n, NULL, FALSE);
935   if (FAILED(hr))
936     return hr;
937
938   if (! This->bDecompress)
939     return IAVIStream_ReadFormat(stream, n, format, fmtsize);
940
941   lp = (LPBITMAPINFOHEADER)AVIFILE_ReadFrame(This, stream, n);
942   if (lp == NULL)
943     return AVIERR_ERROR;
944   if (lp->biBitCount <= 8) {
945     n  = (lp->biClrUsed > 0 ? lp->biClrUsed : 1 << lp->biBitCount);
946     n *= sizeof(RGBQUAD);
947   } else
948     n = 0;
949   n += lp->biSize;
950   
951   memcpy(format, lp, min((LONG)n, *fmtsize));
952   hr = ((LONG)n > *fmtsize ? AVIERR_BUFFERTOOSMALL : AVIERR_OK);
953   *fmtsize = n;
954
955   return hr;
956 }
957
958 static HRESULT WINAPI IEditAVIStream_fnSetFormat(IAVIStream*iface,LONG pos,
959                                                  LPVOID format,LONG formatsize)
960 {
961   TRACE("(%p,%ld,%p,%ld)\n",iface,pos,format,formatsize);
962
963   return AVIERR_UNSUPPORTED;
964 }
965
966 static HRESULT WINAPI IEditAVIStream_fnRead(IAVIStream*iface,LONG start,
967                                             LONG samples,LPVOID buffer,
968                                             LONG buffersize,LONG*bytesread,
969                                             LONG*samplesread)
970 {
971   IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
972   PAVISTREAM stream;
973   DWORD   streamPos, streamNr;
974   LONG    readBytes, readSamples, count;
975   HRESULT hr;
976
977   TRACE("(%p,%ld,%ld,%p,%ld,%p,%p) -- 0x%08lX\n",iface,start,samples,
978         buffer,buffersize,bytesread,samplesread,This->sInfo.fccType);
979
980   /* check parameters */
981   if (bytesread != NULL)
982     *bytesread = 0;
983   if (samplesread != NULL)
984     *samplesread = 0;
985   if (buffersize < 0)
986     return AVIERR_BADSIZE;
987   if ((DWORD)start < This->sInfo.dwStart ||
988       This->sInfo.dwStart + This->sInfo.dwLength < (DWORD)start)
989     return AVIERR_BADPARAM;
990
991   if (! This->bDecompress) {
992     /* audio like data -- sample-based */
993     do {
994       if (samples == 0)
995         return AVIERR_OK; /* nothing at all or already done */
996
997       if (FAILED(AVIFILE_FindStreamInTable(This, start, &stream,
998                                            &streamPos, &streamNr, FALSE)))
999         return AVIERR_ERROR;
1000
1001       /* limit to end of the stream */
1002       count = samples;
1003       if (streamPos + count > EditStreamEnd(This, streamNr))
1004         count = EditStreamEnd(This, streamNr) - streamPos;
1005
1006       hr = IAVIStream_Read(stream, streamPos, count, buffer, buffersize,
1007                            &readBytes, &readSamples);
1008       if (FAILED(hr))
1009         return hr;
1010       if (readBytes == 0 && readSamples == 0 && count != 0)
1011         return AVIERR_FILEREAD; /* for bad stream implementations */
1012
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;
1020       }
1021       start   += count;
1022       samples -= count;
1023     } while (This->sInfo.dwStart + This->sInfo.dwLength > start);
1024   } else {
1025     /* video like data -- frame-based */
1026     LPBITMAPINFOHEADER lp;
1027
1028     if (samples == 0)
1029       return AVIERR_OK;
1030
1031     if (FAILED(AVIFILE_FindStreamInTable(This, start, &stream,
1032                                          &streamPos, &streamNr, FALSE)))
1033       return AVIERR_ERROR;
1034
1035     lp = AVIFILE_ReadFrame(This, stream, streamPos);
1036     if (lp == NULL)
1037       return AVIERR_ERROR;
1038
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);
1044       } else
1045         count = 0;
1046       count += lp->biSize;
1047
1048       if (buffersize < lp->biSizeImage)
1049         return AVIERR_BUFFERTOOSMALL;
1050       memcpy(buffer, (LPBYTE)lp + count, lp->biSizeImage);
1051     }
1052
1053     if (bytesread != NULL)
1054       *bytesread = lp->biSizeImage;
1055     if (samplesread != NULL)
1056       *samplesread = 1;
1057   }
1058
1059   return AVIERR_OK;
1060 }
1061
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)
1066 {
1067   TRACE("(%p,%ld,%ld,%p,%ld,0x%08lX,%p,%p)\n",iface,start,samples,buffer,
1068         buffersize,flags,sampwritten,byteswritten);
1069
1070   /* be sure return parameters have correct values */
1071   if (sampwritten != NULL)
1072     *sampwritten = 0;
1073   if (byteswritten != NULL)
1074     *byteswritten = 0;
1075
1076   return AVIERR_UNSUPPORTED;
1077 }
1078
1079 static HRESULT WINAPI IEditAVIStream_fnDelete(IAVIStream*iface,LONG start,
1080                                               LONG samples)
1081 {
1082   IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
1083
1084   TRACE("(%p,%ld,%ld)\n",iface,start,samples);
1085
1086   return IAVIEditStream_Cut((IAVIEditStream*)This->pae,&start,&samples,NULL);
1087 }
1088
1089 static HRESULT WINAPI IEditAVIStream_fnReadData(IAVIStream*iface,DWORD fcc,
1090                                                 LPVOID lp,LONG *lpread)
1091 {
1092   IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
1093   DWORD n;
1094
1095   TRACE("(%p,0x%08lX,%p,%p)\n",iface,fcc,lp,lpread);
1096
1097   /* check parameters */
1098   if (lp == NULL || lpread == NULL)
1099     return AVIERR_BADPARAM;
1100
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);
1104
1105     if (SUCCEEDED(hr))
1106       return hr;
1107   }
1108
1109   *lpread = 0;
1110   return AVIERR_NODATA;
1111 }
1112
1113 static HRESULT WINAPI IEditAVIStream_fnWriteData(IAVIStream*iface,DWORD fcc,
1114                                                  LPVOID lp,LONG size)
1115 {
1116   TRACE("(%p,0x%08lX,%p,%ld)\n",iface,fcc,lp,size);
1117
1118   return AVIERR_UNSUPPORTED;
1119 }
1120
1121 static HRESULT WINAPI IEditAVIStream_fnSetInfo(IAVIStream*iface,
1122                                                AVISTREAMINFOW*info,LONG len)
1123 {
1124   IEditAVIStreamImpl *This = (IEditAVIStreamImpl *)iface;
1125
1126   TRACE("(%p,%p,%ld)\n",iface,info,len);
1127
1128   return IAVIEditStream_SetInfo((IAVIEditStream*)This->pae,info,len);
1129 }
1130
1131 static HRESULT WINAPI IEditStreamInternal_fnQueryInterface(IEditStreamInternal*iface,REFIID refiid,LPVOID*obj)
1132 {
1133   IEditStreamInternalImpl *This = (IEditStreamInternalImpl *)iface;
1134
1135   assert(This->pae != NULL);
1136
1137   return IAVIEditStream_QueryInterface((IAVIEditStream*)This->pae, refiid, obj);
1138 }
1139
1140 static ULONG   WINAPI IEditStreamInternal_fnAddRef(IEditStreamInternal*iface)
1141 {
1142   IEditStreamInternalImpl *This = (IEditStreamInternalImpl *)iface;
1143
1144   assert(This->pae != NULL);
1145
1146   return IAVIEditStream_AddRef((IAVIEditStream*)This->pae);
1147 }
1148
1149 static ULONG   WINAPI IEditStreamInternal_fnRelease(IEditStreamInternal*iface)
1150 {
1151   IEditStreamInternalImpl *This = (IEditStreamInternalImpl *)iface;
1152
1153   assert(This->pae != NULL);
1154
1155   return IAVIEditStream_Release((IAVIEditStream*)This->pae);
1156 }
1157
1158 static HRESULT  WINAPI IEditStreamInternal_fnGetEditStreamImpl(IEditStreamInternal*iface,LPVOID*ppimpl)
1159 {
1160   IEditStreamInternalImpl *This = (IEditStreamInternalImpl *)iface;
1161
1162   TRACE("(%p,%p) -> %p\n", iface, ppimpl, This->pae);
1163
1164   assert(This->pae != NULL);
1165   assert(ppimpl != NULL);
1166
1167   *ppimpl = This->pae;
1168   return AVIERR_OK;
1169 }