wined3d: Clean up per version shader limits code.
[wine/multimedia.git] / dlls / avifil32 / editstream.c
blobbd654a0aff1a494f002f6952ce68af3cf2922353
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 "mmsystem.h"
30 #include "vfw.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 */
47 } EditStreamTable;
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;
59 #undef INTERFACE
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,
77 PAVISTREAM*ppResult);
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,
85 IAVIEditStream_fnCut,
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,
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);
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 {
148 /* IUnknown stuff */
149 const IAVIStreamVtbl *lpVtbl;
151 /* IAVIStream stuff */
152 IAVIEditStreamImpl *pae;
153 } IEditAVIStreamImpl;
155 typedef struct _IEditStreamInternalImpl {
156 /* IUnknown stuff */
157 const IEditStreamInternalVtbl *lpVtbl;
159 /* IEditStreamInternal stuff */
160 IAVIEditStreamImpl *pae;
161 } IEditStreamInternalImpl;
163 struct _IAVIEditStreamImpl {
164 /* IUnknown stuff */
165 const IAVIEditStreamVtbl *lpVtbl;
166 LONG ref;
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 */
178 BOOL bDecompress;
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(), HEAP_ZERO_MEMORY, sizeof(IAVIEditStreamImpl));
191 if (pedit == NULL)
192 return NULL;
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;
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,
208 DWORD* streamPos,
209 DWORD* streamNr,BOOL bFindSample)
211 DWORD n;
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)
225 *streamNr = n;
227 return AVIERR_OK;
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)
235 *streamNr = n;
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;
245 TRACE(" -> ERROR (NULL,0,0)\n");
246 return AVIERR_BADPARAM;
250 static LPVOID AVIFILE_ReadFrame(IAVIEditStreamImpl* const This,
251 PAVISTREAM pstream, LONG pos)
253 PGETFRAME pg;
255 TRACE("(%p,%p,%ld)\n",This,pstream,pos);
257 if (pstream == NULL)
258 return NULL;
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;
271 AVIStreamGetFrameClose(This->pg);
273 This->pg = 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);
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));
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);
310 return AVIERR_OK;
313 static BOOL AVIFILE_FormatsEqual(PAVISTREAM avi1, PAVISTREAM avi2)
315 LPVOID fmt1 = NULL, fmt2 = NULL;
316 LONG size1, size2, start1, start2;
317 BOOL status = FALSE;
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)))
325 return FALSE;
326 if (FAILED(AVIStreamFormatSize(avi2, start2, &size2)))
327 return FALSE;
328 if (size1 != size2)
329 return FALSE;
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);
343 if (fmt2 != NULL)
344 HeapFree(GetProcessHeap(), 0, fmt2);
345 HeapFree(GetProcessHeap(), 0, fmt1);
347 return status;
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)) {
360 *obj = iface;
361 IAVIEditStream_AddRef(iface);
363 return S_OK;
364 } else if (IsEqualGUID(&IID_IAVIStream, refiid)) {
365 *obj = &This->iAVIStream;
366 IAVIEditStream_AddRef(iface);
368 return S_OK;
369 } else if (IsEqualGUID(&IID_IEditStreamInternal, refiid)) {
370 *obj = &This->iEditStreamInternal;
371 IAVIEditStream_AddRef(iface);
373 return S_OK;
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);
386 return ref;
389 static ULONG WINAPI IAVIEditStream_fnRelease(IAVIEditStream*iface)
391 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
392 DWORD i;
393 ULONG ref = InterlockedDecrement(&This->ref);
395 TRACE("(%p) -> %ld\n", iface, ref);
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);
406 HeapFree(GetProcessHeap(), 0, This->pStreams);
409 HeapFree(GetProcessHeap(), 0, This);
410 return 0;
412 return ref;
415 static HRESULT WINAPI IAVIEditStream_fnCut(IAVIEditStream*iface,LONG*plStart,
416 LONG*plLength,PAVISTREAM*ppResult)
418 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
419 PAVISTREAM stream;
420 DWORD start, len, streamPos, streamNr;
421 HRESULT hr;
423 TRACE("(%p,%p,%p,%p)\n",iface,plStart,plLength,ppResult);
425 if (ppResult != NULL)
426 *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);
433 if (FAILED(hr))
434 return hr;
437 start = *plStart;
438 len = *plLength;
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;
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);
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(), HEAP_ZERO_MEMORY, 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));
480 This->nStreams++;
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;
490 len = 0;
494 This->sInfo.dwEditCount++;
496 return AVIERR_OK;
499 static HRESULT WINAPI IAVIEditStream_fnCopy(IAVIEditStream*iface,LONG*plStart,
500 LONG*plLength,PAVISTREAM*ppResult)
502 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
503 IAVIEditStreamImpl* pEdit;
504 HRESULT hr;
505 LONG start = 0;
507 TRACE("(%p,%p,%p,%p)\n",iface,plStart,plLength,ppResult);
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;
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;
524 if (*(LPDWORD)plStart + *(LPDWORD)plLength > This->sInfo.dwStart + This->sInfo.dwLength)
525 *(LPDWORD)plLength = This->sInfo.dwStart + This->sInfo.dwLength -
526 *(LPDWORD)plStart;
528 pEdit = (IAVIEditStreamImpl*)AVIFILE_CreateEditStream(NULL);
529 if (pEdit == NULL)
530 return AVIERR_MEMORY;
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;
541 return hr;
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;
552 PAVISTREAM pStream;
553 DWORD startPos, endPos, streamNr, nStreams;
554 ULONG n;
556 TRACE("(%p,%p,%p,%p,%ld,%ld)\n",iface,plStart,plLength,
557 pSource,lStart,lLength);
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;
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) {
586 LONG size;
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;
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... */
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;
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 */
648 if (pEdit != NULL) {
649 DWORD nLastStream;
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++;
658 nStreams = nLastStream - streamNr;
659 } else
660 nStreams = 1;
661 if (This->nStreams + nStreams + 1 > This->nTableSize) {
662 n = This->nStreams + nStreams + 33;
664 This->pStreams = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->pStreams, n * sizeof(EditStreamTable));
665 if (This->pStreams == NULL)
666 return AVIERR_MEMORY;
667 This->nTableSize = n;
670 if (plLength != NULL)
671 *plLength = lLength;
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);
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));
695 } else /* append the streams */
696 streamNr = This->nStreams;
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);
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;
714 } else {
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;
733 return AVIERR_OK;
736 static HRESULT WINAPI IAVIEditStream_fnClone(IAVIEditStream*iface,
737 PAVISTREAM*ppResult)
739 IAVIEditStreamImpl *This = (IAVIEditStreamImpl *)iface;
740 IAVIEditStreamImpl* pEdit;
741 DWORD i;
743 TRACE("(%p,%p)\n",iface,ppResult);
745 if (ppResult == NULL)
746 return AVIERR_BADPARAM;
747 *ppResult = NULL;
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(), HEAP_ZERO_MEMORY, 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;
770 return AVIERR_OK;
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 */
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;
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++;
802 return AVIERR_OK;
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;
838 if (lParam2 != 0)
839 return AVIERR_ERROR;
841 if (This->pStreams == NULL) {
842 This->pStreams = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 256 * sizeof(EditStreamTable));
843 if (This->pStreams == NULL)
844 return AVIERR_MEMORY;
845 This->nTableSize = 256;
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;
856 return AVIERR_OK;
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);
868 if (psi == NULL)
869 return AVIERR_BADPARAM;
870 if (size < 0)
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;
880 return AVIERR_OK;
883 static LONG WINAPI IEditAVIStream_fnFindSample(IAVIStream*iface,LONG pos,
884 LONG flags)
886 IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
887 PAVISTREAM stream;
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)
898 return -1;
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 */
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);
912 } else {
913 /* assume change of format every frame */
914 return pos;
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;
923 PAVISTREAM stream;
924 DWORD n;
925 HRESULT hr;
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);
935 if (FAILED(hr))
936 return hr;
938 if (! This->bDecompress)
939 return IAVIStream_ReadFormat(stream, n, format, fmtsize);
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;
951 memcpy(format, lp, min((LONG)n, *fmtsize));
952 hr = ((LONG)n > *fmtsize ? AVIERR_BUFFERTOOSMALL : AVIERR_OK);
953 *fmtsize = n;
955 return hr;
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,
969 LONG*samplesread)
971 IAVIEditStreamImpl* const This = ((IEditAVIStreamImpl* const)iface)->pae;
972 PAVISTREAM stream;
973 DWORD streamPos, streamNr;
974 LONG readBytes, readSamples, count;
975 HRESULT hr;
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)
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;
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 */
997 if (FAILED(AVIFILE_FindStreamInTable(This, start, &stream,
998 &streamPos, &streamNr, FALSE)))
999 return AVIERR_ERROR;
1001 /* limit to end of the stream */
1002 count = samples;
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);
1008 if (FAILED(hr))
1009 return hr;
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;
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;
1028 if (samples == 0)
1029 return AVIERR_OK;
1031 if (FAILED(AVIFILE_FindStreamInTable(This, start, &stream,
1032 &streamPos, &streamNr, FALSE)))
1033 return AVIERR_ERROR;
1035 lp = AVIFILE_ReadFrame(This, stream, streamPos);
1036 if (lp == NULL)
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);
1044 } else
1045 count = 0;
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)
1056 *samplesread = 1;
1059 return AVIERR_OK;
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)
1072 *sampwritten = 0;
1073 if (byteswritten != NULL)
1074 *byteswritten = 0;
1076 return AVIERR_UNSUPPORTED;
1079 static HRESULT WINAPI IEditAVIStream_fnDelete(IAVIStream*iface,LONG start,
1080 LONG samples)
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;
1093 DWORD n;
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);
1105 if (SUCCEEDED(hr))
1106 return hr;
1109 *lpread = 0;
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;
1168 return AVIERR_OK;