1 /* 2 * Copyright 2002 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 17 */ 18 19 #define COBJMACROS 20 #include <assert.h> 21 #include <stdarg.h> 22 23 #include "windef.h" 24 #include "winbase.h" 25 #include "wingdi.h" 26 #include "winuser.h" 27 #include "winnls.h" 28 #include "winerror.h" 29 #include "mmsystem.h" 30 #include "vfw.h" 31 #include "msacm.h" 32 33 #include "avifile_private.h" 34 #include "extrachunk.h" 35 36 #include "wine/unicode.h" 37 #include "wine/debug.h" 38 39 WINE_DEFAULT_DEBUG_CHANNEL(avifile); 40 41 /***********************************************************************/ 42 43 #define formtypeWAVE mmioFOURCC('W','A','V','E') 44 #define ckidWAVEFORMAT mmioFOURCC('f','m','t',' ') 45 #define ckidWAVEFACT mmioFOURCC('f','a','c','t') 46 #define ckidWAVEDATA mmioFOURCC('d','a','t','a') 47 48 /***********************************************************************/ 49 50 #define ENDIAN_SWAPWORD(x) ((((x) >> 8) & 0xFF) | (((x) & 0xFF) << 8)) 51 #define ENDIAN_SWAPDWORD(x) (ENDIAN_SWAPWORD((x >> 16) & 0xFFFF) | \ 52 ENDIAN_SWAPWORD(x & 0xFFFF) << 16) 53 54 #ifdef WORDS_BIGENDIAN 55 #define BE2H_WORD(x) (x) 56 #define BE2H_DWORD(x) (x) 57 #define LE2H_WORD(x) ENDIAN_SWAPWORD(x) 58 #define LE2H_DWORD(x) ENDIAN_SWAPDWORD(x) 59 #else 60 #define BE2H_WORD(x) ENDIAN_SWAPWORD(x) 61 #define BE2H_DWORD(x) ENDIAN_SWAPDWORD(x) 62 #define LE2H_WORD(x) (x) 63 #define LE2H_DWORD(x) (x) 64 #endif 65 66 typedef struct { 67 FOURCC fccType; 68 DWORD offset; 69 DWORD size; 70 INT encoding; 71 DWORD sampleRate; 72 DWORD channels; 73 } SUNAUDIOHEADER; 74 75 #define AU_ENCODING_ULAW_8 1 76 #define AU_ENCODING_PCM_8 2 77 #define AU_ENCODING_PCM_16 3 78 #define AU_ENCODING_PCM_24 4 79 #define AU_ENCODING_PCM_32 5 80 #define AU_ENCODING_FLOAT 6 81 #define AU_ENCODING_DOUBLE 7 82 #define AU_ENCODING_ADPCM_G721_32 23 83 #define AU_ENCODING_ADPCM_G722 24 84 #define AU_ENCODING_ADPCM_G723_24 25 85 #define AU_ENCODING_ADPCM_G723_5 26 86 #define AU_ENCODING_ALAW_8 27 87 88 /***********************************************************************/ 89 90 typedef struct _IAVIFileImpl { 91 IUnknown IUnknown_inner; 92 IAVIFile IAVIFile_iface; 93 IPersistFile IPersistFile_iface; 94 IAVIStream IAVIStream_iface; 95 IUnknown *outer_unk; 96 LONG ref; 97 /* IAVIFile, IAVIStream stuff... */ 98 AVIFILEINFOW fInfo; 99 AVISTREAMINFOW sInfo; 100 101 LPWAVEFORMATEX lpFormat; 102 LONG cbFormat; 103 104 MMCKINFO ckData; 105 106 EXTRACHUNKS extra; 107 108 /* IPersistFile stuff ... */ 109 HMMIO hmmio; 110 LPWSTR szFileName; 111 UINT uMode; 112 BOOL fDirty; 113 } IAVIFileImpl; 114 115 /***********************************************************************/ 116 117 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This); 118 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This); 119 static HRESULT AVIFILE_SaveFile(const IAVIFileImpl *This); 120 121 static inline IAVIFileImpl *impl_from_IUnknown(IUnknown *iface) 122 { 123 return CONTAINING_RECORD(iface, IAVIFileImpl, IUnknown_inner); 124 } 125 126 static HRESULT WINAPI IUnknown_fnQueryInterface(IUnknown *iface, REFIID riid, void **ret_iface) 127 { 128 IAVIFileImpl *This = impl_from_IUnknown(iface); 129 130 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ret_iface); 131 132 if (IsEqualGUID(&IID_IUnknown, riid)) 133 *ret_iface = &This->IUnknown_inner; 134 else if (IsEqualGUID(&IID_IAVIFile, riid)) 135 *ret_iface = &This->IAVIFile_iface; 136 else if (IsEqualGUID(&IID_IAVIStream, riid)) 137 *ret_iface = &This->IAVIStream_iface; 138 else if (IsEqualGUID(&IID_IPersistFile, riid)) 139 *ret_iface = &This->IPersistFile_iface; 140 else { 141 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ret_iface); 142 *ret_iface = NULL; 143 return E_NOINTERFACE; 144 } 145 146 /* Violation of the COM aggregation ref counting rule */ 147 IUnknown_AddRef(&This->IUnknown_inner); 148 return S_OK; 149 } 150 151 static ULONG WINAPI IUnknown_fnAddRef(IUnknown *iface) 152 { 153 IAVIFileImpl *This = impl_from_IUnknown(iface); 154 ULONG ref = InterlockedIncrement(&This->ref); 155 156 TRACE("(%p) ref=%d\n", This, ref); 157 158 return ref; 159 } 160 161 static ULONG WINAPI IUnknown_fnRelease(IUnknown *iface) 162 { 163 IAVIFileImpl *This = impl_from_IUnknown(iface); 164 ULONG ref = InterlockedDecrement(&This->ref); 165 166 TRACE("(%p) ref=%d\n", This, ref); 167 168 if (!ref) { 169 /* need to write headers to file */ 170 if (This->fDirty) 171 AVIFILE_SaveFile(This); 172 173 HeapFree(GetProcessHeap(), 0, This->lpFormat); 174 This->lpFormat = NULL; 175 This->cbFormat = 0; 176 HeapFree(GetProcessHeap(), 0, This->extra.lp); 177 This->extra.lp = NULL; 178 This->extra.cb = 0; 179 HeapFree(GetProcessHeap(), 0, This->szFileName); 180 This->szFileName = NULL; 181 if (This->hmmio) { 182 mmioClose(This->hmmio, 0); 183 This->hmmio = NULL; 184 } 185 HeapFree(GetProcessHeap(), 0, This); 186 } 187 188 return ref; 189 } 190 191 static const IUnknownVtbl unk_vtbl = 192 { 193 IUnknown_fnQueryInterface, 194 IUnknown_fnAddRef, 195 IUnknown_fnRelease 196 }; 197 198 static inline IAVIFileImpl *impl_from_IAVIFile(IAVIFile *iface) 199 { 200 return CONTAINING_RECORD(iface, IAVIFileImpl, IAVIFile_iface); 201 } 202 203 static HRESULT WINAPI IAVIFile_fnQueryInterface(IAVIFile *iface, REFIID riid, void **ret_iface) 204 { 205 IAVIFileImpl *This = impl_from_IAVIFile(iface); 206 207 return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface); 208 } 209 210 static ULONG WINAPI IAVIFile_fnAddRef(IAVIFile *iface) 211 { 212 IAVIFileImpl *This = impl_from_IAVIFile(iface); 213 214 return IUnknown_AddRef(This->outer_unk); 215 } 216 217 static ULONG WINAPI IAVIFile_fnRelease(IAVIFile *iface) 218 { 219 IAVIFileImpl *This = impl_from_IAVIFile(iface); 220 221 return IUnknown_Release(This->outer_unk); 222 } 223 224 static HRESULT WINAPI IAVIFile_fnInfo(IAVIFile *iface, AVIFILEINFOW *afi, LONG size) 225 { 226 IAVIFileImpl *This = impl_from_IAVIFile(iface); 227 228 TRACE("(%p,%p,%d)\n",iface,afi,size); 229 230 if (afi == NULL) 231 return AVIERR_BADPARAM; 232 if (size < 0) 233 return AVIERR_BADSIZE; 234 235 /* update file info */ 236 This->fInfo.dwFlags = 0; 237 This->fInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE; 238 if (This->lpFormat != NULL) { 239 assert(This->sInfo.dwScale != 0); 240 241 This->fInfo.dwStreams = 1; 242 This->fInfo.dwScale = This->sInfo.dwScale; 243 This->fInfo.dwRate = This->sInfo.dwRate; 244 This->fInfo.dwLength = This->sInfo.dwLength; 245 This->fInfo.dwSuggestedBufferSize = This->ckData.cksize; 246 This->fInfo.dwMaxBytesPerSec = 247 MulDiv(This->sInfo.dwSampleSize,This->sInfo.dwRate,This->sInfo.dwScale); 248 } 249 250 memcpy(afi, &This->fInfo, min((DWORD)size, sizeof(This->fInfo))); 251 252 if ((DWORD)size < sizeof(This->fInfo)) 253 return AVIERR_BUFFERTOOSMALL; 254 return AVIERR_OK; 255 } 256 257 static HRESULT WINAPI IAVIFile_fnGetStream(IAVIFile *iface, IAVIStream **avis, DWORD fccType, 258 LONG lParam) 259 { 260 IAVIFileImpl *This = impl_from_IAVIFile(iface); 261 262 TRACE("(%p,%p,0x%08X,%d)\n", iface, avis, fccType, lParam); 263 264 /* check parameter */ 265 if (avis == NULL) 266 return AVIERR_BADPARAM; 267 268 *avis = NULL; 269 270 /* Does our stream exists? */ 271 if (lParam != 0 || This->fInfo.dwStreams == 0) 272 return AVIERR_NODATA; 273 if (fccType != 0 && fccType != streamtypeAUDIO) 274 return AVIERR_NODATA; 275 276 *avis = &This->IAVIStream_iface; 277 IAVIStream_AddRef(*avis); 278 279 return AVIERR_OK; 280 } 281 282 static HRESULT WINAPI IAVIFile_fnCreateStream(IAVIFile *iface, IAVIStream **avis, 283 AVISTREAMINFOW *asi) 284 { 285 IAVIFileImpl *This = impl_from_IAVIFile(iface); 286 287 TRACE("(%p,%p,%p)\n", iface, avis, asi); 288 289 /* check parameters */ 290 if (avis == NULL || asi == NULL) 291 return AVIERR_BADPARAM; 292 293 *avis = NULL; 294 295 /* We only support one audio stream */ 296 if (This->fInfo.dwStreams != 0 || This->lpFormat != NULL) 297 return AVIERR_UNSUPPORTED; 298 if (asi->fccType != streamtypeAUDIO) 299 return AVIERR_UNSUPPORTED; 300 301 /* Does the user have write permission? */ 302 if ((This->uMode & MMIO_RWMODE) == 0) 303 return AVIERR_READONLY; 304 305 This->cbFormat = 0; 306 This->lpFormat = NULL; 307 308 memcpy(&This->sInfo, asi, sizeof(This->sInfo)); 309 310 /* make sure streaminfo if okay for us */ 311 This->sInfo.fccHandler = 0; 312 This->sInfo.dwFlags = 0; 313 This->sInfo.dwCaps = AVIFILECAPS_CANREAD|AVIFILECAPS_CANWRITE; 314 This->sInfo.dwStart = 0; 315 This->sInfo.dwInitialFrames = 0; 316 This->sInfo.dwFormatChangeCount = 0; 317 SetRectEmpty(&This->sInfo.rcFrame); 318 319 This->fInfo.dwStreams = 1; 320 This->fInfo.dwScale = This->sInfo.dwScale; 321 This->fInfo.dwRate = This->sInfo.dwRate; 322 This->fInfo.dwLength = This->sInfo.dwLength; 323 324 This->ckData.dwDataOffset = 0; 325 This->ckData.cksize = 0; 326 327 *avis = &This->IAVIStream_iface; 328 IAVIStream_AddRef(*avis); 329 330 return AVIERR_OK; 331 } 332 333 static HRESULT WINAPI IAVIFile_fnWriteData(IAVIFile *iface, DWORD ckid, void *lpData, LONG size) 334 { 335 IAVIFileImpl *This = impl_from_IAVIFile(iface); 336 337 TRACE("(%p,0x%08X,%p,%d)\n", iface, ckid, lpData, size); 338 339 /* check parameters */ 340 if (lpData == NULL) 341 return AVIERR_BADPARAM; 342 if (size < 0) 343 return AVIERR_BADSIZE; 344 345 /* Do we have write permission? */ 346 if ((This->uMode & MMIO_RWMODE) == 0) 347 return AVIERR_READONLY; 348 349 This->fDirty = TRUE; 350 351 return WriteExtraChunk(&This->extra, ckid, lpData, size); 352 } 353 354 static HRESULT WINAPI IAVIFile_fnReadData(IAVIFile *iface, DWORD ckid, void *lpData, LONG *size) 355 { 356 IAVIFileImpl *This = impl_from_IAVIFile(iface); 357 358 TRACE("(%p,0x%08X,%p,%p)\n", iface, ckid, lpData, size); 359 360 return ReadExtraChunk(&This->extra, ckid, lpData, size); 361 } 362 363 static HRESULT WINAPI IAVIFile_fnEndRecord(IAVIFile *iface) 364 { 365 TRACE("(%p)\n",iface); 366 367 /* This is only needed for interleaved files. 368 * We have only one stream, which can't be interleaved. 369 */ 370 return AVIERR_OK; 371 } 372 373 static HRESULT WINAPI IAVIFile_fnDeleteStream(IAVIFile *iface, DWORD fccType, LONG lParam) 374 { 375 IAVIFileImpl *This = impl_from_IAVIFile(iface); 376 377 TRACE("(%p,0x%08X,%d)\n", iface, fccType, lParam); 378 379 /* check parameter */ 380 if (lParam < 0) 381 return AVIERR_BADPARAM; 382 383 /* Do we have our audio stream? */ 384 if (lParam != 0 || This->fInfo.dwStreams == 0 || 385 (fccType != 0 && fccType != streamtypeAUDIO)) 386 return AVIERR_NODATA; 387 388 /* Have user write permissions? */ 389 if ((This->uMode & MMIO_RWMODE) == 0) 390 return AVIERR_READONLY; 391 392 HeapFree(GetProcessHeap(), 0, This->lpFormat); 393 This->lpFormat = NULL; 394 This->cbFormat = 0; 395 396 /* update infos */ 397 This->ckData.dwDataOffset = 0; 398 This->ckData.cksize = 0; 399 400 This->sInfo.dwScale = 0; 401 This->sInfo.dwRate = 0; 402 This->sInfo.dwLength = 0; 403 This->sInfo.dwSuggestedBufferSize = 0; 404 405 This->fInfo.dwStreams = 0; 406 This->fInfo.dwEditCount++; 407 408 This->fDirty = TRUE; 409 410 return AVIERR_OK; 411 } 412 413 static const struct IAVIFileVtbl iwavft = { 414 IAVIFile_fnQueryInterface, 415 IAVIFile_fnAddRef, 416 IAVIFile_fnRelease, 417 IAVIFile_fnInfo, 418 IAVIFile_fnGetStream, 419 IAVIFile_fnCreateStream, 420 IAVIFile_fnWriteData, 421 IAVIFile_fnReadData, 422 IAVIFile_fnEndRecord, 423 IAVIFile_fnDeleteStream 424 }; 425 426 /***********************************************************************/ 427 428 static inline IAVIFileImpl *impl_from_IPersistFile(IPersistFile *iface) 429 { 430 return CONTAINING_RECORD(iface, IAVIFileImpl, IPersistFile_iface); 431 } 432 433 static HRESULT WINAPI IPersistFile_fnQueryInterface(IPersistFile *iface, REFIID riid, 434 void **ret_iface) 435 { 436 IAVIFileImpl *This = impl_from_IPersistFile(iface); 437 438 return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface); 439 } 440 441 static ULONG WINAPI IPersistFile_fnAddRef(IPersistFile *iface) 442 { 443 IAVIFileImpl *This = impl_from_IPersistFile(iface); 444 445 return IUnknown_AddRef(This->outer_unk); 446 } 447 448 static ULONG WINAPI IPersistFile_fnRelease(IPersistFile *iface) 449 { 450 IAVIFileImpl *This = impl_from_IPersistFile(iface); 451 452 return IUnknown_Release(This->outer_unk); 453 } 454 455 static HRESULT WINAPI IPersistFile_fnGetClassID(IPersistFile *iface, 456 LPCLSID pClassID) 457 { 458 TRACE("(%p,%p)\n", iface, pClassID); 459 460 if (pClassID == NULL) 461 return AVIERR_BADPARAM; 462 463 *pClassID = CLSID_WAVFile; 464 465 return AVIERR_OK; 466 } 467 468 static HRESULT WINAPI IPersistFile_fnIsDirty(IPersistFile *iface) 469 { 470 IAVIFileImpl *This = impl_from_IPersistFile(iface); 471 472 TRACE("(%p)\n", iface); 473 474 return (This->fDirty ? S_OK : S_FALSE); 475 } 476 477 static HRESULT WINAPI IPersistFile_fnLoad(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode) 478 { 479 IAVIFileImpl *This = impl_from_IPersistFile(iface); 480 WCHAR wszStreamFmt[50]; 481 INT len; 482 483 TRACE("(%p,%s,0x%08X)\n", iface, debugstr_w(pszFileName), dwMode); 484 485 /* check parameter */ 486 if (pszFileName == NULL) 487 return AVIERR_BADPARAM; 488 489 if (This->hmmio != NULL) 490 return AVIERR_ERROR; /* No reuse of this object for another file! */ 491 492 /* remember mode and name */ 493 This->uMode = dwMode; 494 495 len = lstrlenW(pszFileName) + 1; 496 This->szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); 497 if (This->szFileName == NULL) 498 return AVIERR_MEMORY; 499 lstrcpyW(This->szFileName, pszFileName); 500 501 /* try to open the file */ 502 This->hmmio = mmioOpenW(This->szFileName, NULL, MMIO_ALLOCBUF | dwMode); 503 if (This->hmmio == NULL) { 504 /* mmioOpenW not in native DLLs of Win9x -- try mmioOpenA */ 505 LPSTR szFileName; 506 len = WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1, 507 NULL, 0, NULL, NULL); 508 szFileName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(CHAR)); 509 if (szFileName == NULL) 510 return AVIERR_MEMORY; 511 512 WideCharToMultiByte(CP_ACP, 0, This->szFileName, -1, szFileName, 513 len, NULL, NULL); 514 515 This->hmmio = mmioOpenA(szFileName, NULL, MMIO_ALLOCBUF | dwMode); 516 HeapFree(GetProcessHeap(), 0, szFileName); 517 if (This->hmmio == NULL) 518 return AVIERR_FILEOPEN; 519 } 520 521 memset(& This->fInfo, 0, sizeof(This->fInfo)); 522 memset(& This->sInfo, 0, sizeof(This->sInfo)); 523 524 LoadStringW(AVIFILE_hModule, IDS_WAVEFILETYPE, This->fInfo.szFileType, 525 sizeof(This->fInfo.szFileType)/sizeof(This->fInfo.szFileType[0])); 526 if (LoadStringW(AVIFILE_hModule, IDS_WAVESTREAMFORMAT, 527 wszStreamFmt, sizeof(wszStreamFmt)/sizeof(wszStreamFmt[0])) > 0) { 528 wsprintfW(This->sInfo.szName, wszStreamFmt, 529 AVIFILE_BasenameW(This->szFileName)); 530 } 531 532 /* should we create a new file? */ 533 if (dwMode & OF_CREATE) { 534 /* nothing more to do */ 535 return AVIERR_OK; 536 } else 537 return AVIFILE_LoadFile(This); 538 } 539 540 static HRESULT WINAPI IPersistFile_fnSave(IPersistFile *iface, 541 LPCOLESTR pszFileName,BOOL fRemember) 542 { 543 TRACE("(%p,%s,%d)\n", iface, debugstr_w(pszFileName), fRemember); 544 545 /* We write directly to disk, so nothing to do. */ 546 547 return AVIERR_OK; 548 } 549 550 static HRESULT WINAPI IPersistFile_fnSaveCompleted(IPersistFile *iface, 551 LPCOLESTR pszFileName) 552 { 553 TRACE("(%p,%s)\n", iface, debugstr_w(pszFileName)); 554 555 /* We write directly to disk, so nothing to do. */ 556 557 return AVIERR_OK; 558 } 559 560 static HRESULT WINAPI IPersistFile_fnGetCurFile(IPersistFile *iface, LPOLESTR *ppszFileName) 561 { 562 IAVIFileImpl *This = impl_from_IPersistFile(iface); 563 564 TRACE("(%p,%p)\n", iface, ppszFileName); 565 566 if (ppszFileName == NULL) 567 return AVIERR_BADPARAM; 568 569 *ppszFileName = NULL; 570 571 if (This->szFileName) { 572 int len = lstrlenW(This->szFileName) + 1; 573 574 *ppszFileName = CoTaskMemAlloc(len * sizeof(WCHAR)); 575 if (*ppszFileName == NULL) 576 return AVIERR_MEMORY; 577 578 strcpyW(*ppszFileName, This->szFileName); 579 } 580 581 return AVIERR_OK; 582 } 583 584 static const struct IPersistFileVtbl iwavpft = { 585 IPersistFile_fnQueryInterface, 586 IPersistFile_fnAddRef, 587 IPersistFile_fnRelease, 588 IPersistFile_fnGetClassID, 589 IPersistFile_fnIsDirty, 590 IPersistFile_fnLoad, 591 IPersistFile_fnSave, 592 IPersistFile_fnSaveCompleted, 593 IPersistFile_fnGetCurFile 594 }; 595 596 /***********************************************************************/ 597 598 static inline IAVIFileImpl *impl_from_IAVIStream(IAVIStream *iface) 599 { 600 return CONTAINING_RECORD(iface, IAVIFileImpl, IAVIStream_iface); 601 } 602 603 static HRESULT WINAPI IAVIStream_fnQueryInterface(IAVIStream *iface, REFIID riid, void **ret_iface) 604 { 605 IAVIFileImpl *This = impl_from_IAVIStream(iface); 606 607 return IUnknown_QueryInterface(This->outer_unk, riid, ret_iface); 608 } 609 610 static ULONG WINAPI IAVIStream_fnAddRef(IAVIStream *iface) 611 { 612 IAVIFileImpl *This = impl_from_IAVIStream(iface); 613 614 return IUnknown_AddRef(This->outer_unk); 615 } 616 617 static ULONG WINAPI IAVIStream_fnRelease(IAVIStream* iface) 618 { 619 IAVIFileImpl *This = impl_from_IAVIStream(iface); 620 621 return IUnknown_Release(This->outer_unk); 622 } 623 624 static HRESULT WINAPI IAVIStream_fnCreate(IAVIStream *iface, LPARAM lParam1, 625 LPARAM lParam2) 626 { 627 TRACE("(%p,0x%08lX,0x%08lX)\n", iface, lParam1, lParam2); 628 629 /* This IAVIStream interface needs an WAVFile */ 630 return AVIERR_UNSUPPORTED; 631 } 632 633 static HRESULT WINAPI IAVIStream_fnInfo(IAVIStream *iface, AVISTREAMINFOW *psi, LONG size) 634 { 635 IAVIFileImpl *This = impl_from_IAVIStream(iface); 636 637 TRACE("(%p,%p,%d)\n", iface, psi, size); 638 639 if (psi == NULL) 640 return AVIERR_BADPARAM; 641 if (size < 0) 642 return AVIERR_BADSIZE; 643 644 memcpy(psi, &This->sInfo, min((DWORD)size, sizeof(This->sInfo))); 645 646 if ((DWORD)size < sizeof(This->sInfo)) 647 return AVIERR_BUFFERTOOSMALL; 648 return AVIERR_OK; 649 } 650 651 static LONG WINAPI IAVIStream_fnFindSample(IAVIStream *iface, LONG pos, LONG flags) 652 { 653 IAVIFileImpl *This = impl_from_IAVIStream(iface); 654 655 TRACE("(%p,%d,0x%08X)\n",iface,pos,flags); 656 657 /* Do we have data? */ 658 if (This->lpFormat == NULL) 659 return -1; 660 661 /* We don't have an index */ 662 if (flags & FIND_INDEX) 663 return -1; 664 665 if (flags & FIND_FROM_START) { 666 pos = This->sInfo.dwStart; 667 flags &= ~(FIND_FROM_START|FIND_PREV); 668 flags |= FIND_NEXT; 669 } 670 671 if (flags & FIND_FORMAT) { 672 if ((flags & FIND_NEXT) && pos > 0) 673 pos = -1; 674 else 675 pos = 0; 676 } 677 678 if ((flags & FIND_RET) == FIND_LENGTH || 679 (flags & FIND_RET) == FIND_SIZE) 680 return This->sInfo.dwSampleSize; 681 if ((flags & FIND_RET) == FIND_OFFSET) 682 return This->ckData.dwDataOffset + pos * This->sInfo.dwSampleSize; 683 684 return pos; 685 } 686 687 static HRESULT WINAPI IAVIStream_fnReadFormat(IAVIStream *iface, LONG pos, void *format, 688 LONG *formatsize) 689 { 690 IAVIFileImpl *This = impl_from_IAVIStream(iface); 691 692 TRACE("(%p,%d,%p,%p)\n", iface, pos, format, formatsize); 693 694 if (formatsize == NULL) 695 return AVIERR_BADPARAM; 696 697 /* only interested in needed buffersize? */ 698 if (format == NULL || *formatsize <= 0) { 699 *formatsize = This->cbFormat; 700 701 return AVIERR_OK; 702 } 703 704 /* copy initial format (only as much as will fit) */ 705 memcpy(format, This->lpFormat, min(*formatsize, This->cbFormat)); 706 if (*formatsize < This->cbFormat) { 707 *formatsize = This->cbFormat; 708 return AVIERR_BUFFERTOOSMALL; 709 } 710 711 *formatsize = This->cbFormat; 712 return AVIERR_OK; 713 } 714 715 static HRESULT WINAPI IAVIStream_fnSetFormat(IAVIStream *iface, LONG pos, void *format, 716 LONG formatsize) 717 { 718 IAVIFileImpl *This = impl_from_IAVIStream(iface); 719 720 TRACE("(%p,%d,%p,%d)\n", iface, pos, format, formatsize); 721 722 /* check parameters */ 723 if (format == NULL || formatsize <= sizeof(PCMWAVEFORMAT)) 724 return AVIERR_BADPARAM; 725 726 /* We can only do this to an empty wave file, but ignore call 727 * if still same format */ 728 if (This->lpFormat != NULL) { 729 if (formatsize != This->cbFormat || 730 memcmp(format, This->lpFormat, formatsize) != 0) 731 return AVIERR_UNSUPPORTED; 732 733 return AVIERR_OK; 734 } 735 736 /* only support start at position 0 */ 737 if (pos != 0) 738 return AVIERR_UNSUPPORTED; 739 740 /* Do we have write permission? */ 741 if ((This->uMode & MMIO_RWMODE) == 0) 742 return AVIERR_READONLY; 743 744 /* get memory for format and copy it */ 745 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, formatsize); 746 if (This->lpFormat == NULL) 747 return AVIERR_MEMORY; 748 749 This->cbFormat = formatsize; 750 memcpy(This->lpFormat, format, formatsize); 751 752 /* update info's about 'data' chunk */ 753 This->ckData.dwDataOffset = formatsize + 7 * sizeof(DWORD); 754 This->ckData.cksize = 0; 755 756 /* for non-pcm format we need also a 'fact' chunk */ 757 if (This->lpFormat->wFormatTag != WAVE_FORMAT_PCM) 758 This->ckData.dwDataOffset += 3 * sizeof(DWORD); 759 760 /* update stream and file info */ 761 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign; 762 This->sInfo.dwScale = This->lpFormat->nBlockAlign; 763 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec; 764 This->sInfo.dwLength = 0; 765 This->sInfo.dwSuggestedBufferSize = 0; 766 767 return AVIERR_OK; 768 } 769 770 static HRESULT WINAPI IAVIStream_fnRead(IAVIStream *iface, LONG start, LONG samples, void *buffer, 771 LONG buffersize, LONG *bytesread, LONG *samplesread) 772 { 773 IAVIFileImpl *This = impl_from_IAVIStream(iface); 774 775 TRACE("(%p,%d,%d,%p,%d,%p,%p)\n", iface, start, samples, buffer, 776 buffersize, bytesread, samplesread); 777 778 /* clear return parameters if given */ 779 if (bytesread != NULL) 780 *bytesread = 0; 781 if (samplesread != NULL) 782 *samplesread = 0; 783 784 /* positions without data */ 785 if (start < 0 || (DWORD)start > This->sInfo.dwLength) 786 return AVIERR_OK; 787 788 /* check samples */ 789 if (samples < 0) 790 samples = 0; 791 if (buffersize > 0) { 792 if (samples > 0) 793 samples = min((DWORD)samples, buffersize / This->sInfo.dwSampleSize); 794 else 795 samples = buffersize / This->sInfo.dwSampleSize; 796 } 797 798 /* limit to end of stream */ 799 if ((DWORD)(start + samples) > This->sInfo.dwLength) 800 samples = This->sInfo.dwLength - start; 801 802 /* request only the sizes? */ 803 if (buffer == NULL || buffersize <= 0) { 804 /* then I need at least one parameter for it */ 805 if (bytesread == NULL && samplesread == NULL) 806 return AVIERR_BADPARAM; 807 808 if (bytesread != NULL) 809 *bytesread = samples * This->sInfo.dwSampleSize; 810 if (samplesread != NULL) 811 *samplesread = samples; 812 813 return AVIERR_OK; 814 } 815 816 /* nothing to read? */ 817 if (samples == 0) 818 return AVIERR_OK; 819 820 /* Can I read at least one sample? */ 821 if ((DWORD)buffersize < This->sInfo.dwSampleSize) 822 return AVIERR_BUFFERTOOSMALL; 823 824 buffersize = samples * This->sInfo.dwSampleSize; 825 826 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset 827 + start * This->sInfo.dwSampleSize, SEEK_SET) == -1) 828 return AVIERR_FILEREAD; 829 if (mmioRead(This->hmmio, buffer, buffersize) != buffersize) 830 return AVIERR_FILEREAD; 831 832 /* fill out return parameters if given */ 833 if (bytesread != NULL) 834 *bytesread = buffersize; 835 if (samplesread != NULL) 836 *samplesread = samples; 837 838 return AVIERR_OK; 839 } 840 841 static HRESULT WINAPI IAVIStream_fnWrite(IAVIStream *iface, LONG start, LONG samples, void *buffer, 842 LONG buffersize, DWORD flags, LONG *sampwritten, LONG *byteswritten) 843 { 844 IAVIFileImpl *This = impl_from_IAVIStream(iface); 845 846 TRACE("(%p,%d,%d,%p,%d,0x%08X,%p,%p)\n", iface, start, samples, 847 buffer, buffersize, flags, sampwritten, byteswritten); 848 849 /* clear return parameters if given */ 850 if (sampwritten != NULL) 851 *sampwritten = 0; 852 if (byteswritten != NULL) 853 *byteswritten = 0; 854 855 /* check parameters */ 856 if (buffer == NULL && (buffersize > 0 || samples > 0)) 857 return AVIERR_BADPARAM; 858 859 /* Do we have write permission? */ 860 if ((This->uMode & MMIO_RWMODE) == 0) 861 return AVIERR_READONLY; 862 863 /* < 0 means "append" */ 864 if (start < 0) 865 start = This->sInfo.dwStart + This->sInfo.dwLength; 866 867 /* check buffersize -- must multiple of samplesize */ 868 if (buffersize & ~(This->sInfo.dwSampleSize - 1)) 869 return AVIERR_BADSIZE; 870 871 /* do we have anything to write? */ 872 if (buffer != NULL && buffersize > 0) { 873 This->fDirty = TRUE; 874 875 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset + 876 start * This->sInfo.dwSampleSize, SEEK_SET) == -1) 877 return AVIERR_FILEWRITE; 878 if (mmioWrite(This->hmmio, buffer, buffersize) != buffersize) 879 return AVIERR_FILEWRITE; 880 881 This->sInfo.dwLength = max(This->sInfo.dwLength, (DWORD)start + samples); 882 This->ckData.cksize = max(This->ckData.cksize, 883 start * This->sInfo.dwSampleSize + buffersize); 884 885 /* fill out return parameters if given */ 886 if (sampwritten != NULL) 887 *sampwritten = samples; 888 if (byteswritten != NULL) 889 *byteswritten = buffersize; 890 } 891 892 return AVIERR_OK; 893 } 894 895 static HRESULT WINAPI IAVIStream_fnDelete(IAVIStream *iface, LONG start, LONG samples) 896 { 897 IAVIFileImpl *This = impl_from_IAVIStream(iface); 898 899 TRACE("(%p,%d,%d)\n", iface, start, samples); 900 901 /* check parameters */ 902 if (start < 0 || samples < 0) 903 return AVIERR_BADPARAM; 904 905 /* Delete before start of stream? */ 906 if ((DWORD)(start + samples) < This->sInfo.dwStart) 907 return AVIERR_OK; 908 909 /* Delete after end of stream? */ 910 if ((DWORD)start > This->sInfo.dwLength) 911 return AVIERR_OK; 912 913 /* For the rest we need write permissions */ 914 if ((This->uMode & MMIO_RWMODE) == 0) 915 return AVIERR_READONLY; 916 917 if ((DWORD)(start + samples) >= This->sInfo.dwLength) { 918 /* deletion at end */ 919 samples = This->sInfo.dwLength - start; 920 This->sInfo.dwLength -= samples; 921 This->ckData.cksize -= samples * This->sInfo.dwSampleSize; 922 } else if ((DWORD)start <= This->sInfo.dwStart) { 923 /* deletion at start */ 924 samples = This->sInfo.dwStart - start; 925 start = This->sInfo.dwStart; 926 This->ckData.dwDataOffset += samples * This->sInfo.dwSampleSize; 927 This->ckData.cksize -= samples * This->sInfo.dwSampleSize; 928 } else { 929 /* deletion inside stream -- needs playlist and cue's */ 930 FIXME(": deletion inside of stream not supported!\n"); 931 932 return AVIERR_UNSUPPORTED; 933 } 934 935 This->fDirty = TRUE; 936 937 return AVIERR_OK; 938 } 939 940 static HRESULT WINAPI IAVIStream_fnReadData(IAVIStream *iface, DWORD fcc, void *lp, LONG *lpread) 941 { 942 IAVIFileImpl *This = impl_from_IAVIStream(iface); 943 944 return IAVIFile_ReadData(&This->IAVIFile_iface, fcc, lp, lpread); 945 } 946 947 static HRESULT WINAPI IAVIStream_fnWriteData(IAVIStream *iface, DWORD fcc, void *lp, LONG size) 948 { 949 IAVIFileImpl *This = impl_from_IAVIStream(iface); 950 951 return IAVIFile_WriteData(&This->IAVIFile_iface, fcc, lp, size); 952 } 953 954 static HRESULT WINAPI IAVIStream_fnSetInfo(IAVIStream *iface, 955 LPAVISTREAMINFOW info, LONG infolen) 956 { 957 FIXME("(%p,%p,%d): stub\n", iface, info, infolen); 958 959 return E_FAIL; 960 } 961 962 static const struct IAVIStreamVtbl iwavst = { 963 IAVIStream_fnQueryInterface, 964 IAVIStream_fnAddRef, 965 IAVIStream_fnRelease, 966 IAVIStream_fnCreate, 967 IAVIStream_fnInfo, 968 IAVIStream_fnFindSample, 969 IAVIStream_fnReadFormat, 970 IAVIStream_fnSetFormat, 971 IAVIStream_fnRead, 972 IAVIStream_fnWrite, 973 IAVIStream_fnDelete, 974 IAVIStream_fnReadData, 975 IAVIStream_fnWriteData, 976 IAVIStream_fnSetInfo 977 }; 978 979 HRESULT AVIFILE_CreateWAVFile(IUnknown *outer_unk, REFIID riid, void **ret_iface) 980 { 981 IAVIFileImpl *pfile; 982 HRESULT hr; 983 984 *ret_iface = NULL; 985 986 pfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pfile)); 987 if (!pfile) 988 return AVIERR_MEMORY; 989 990 pfile->IUnknown_inner.lpVtbl = &unk_vtbl; 991 pfile->IAVIFile_iface.lpVtbl = &iwavft; 992 pfile->IPersistFile_iface.lpVtbl = &iwavpft; 993 pfile->IAVIStream_iface.lpVtbl = &iwavst; 994 pfile->ref = 1; 995 if (outer_unk) 996 pfile->outer_unk = outer_unk; 997 else 998 pfile->outer_unk = &pfile->IUnknown_inner; 999 1000 hr = IUnknown_QueryInterface(&pfile->IUnknown_inner, riid, ret_iface); 1001 IUnknown_Release(&pfile->IUnknown_inner); 1002 1003 return hr; 1004 } 1005 1006 /***********************************************************************/ 1007 1008 static HRESULT AVIFILE_LoadFile(IAVIFileImpl *This) 1009 { 1010 MMCKINFO ckRIFF; 1011 MMCKINFO ck; 1012 1013 This->sInfo.dwLength = 0; /* just to be sure */ 1014 This->fDirty = FALSE; 1015 1016 /* search for RIFF chunk */ 1017 ckRIFF.fccType = 0; /* find any */ 1018 if (mmioDescend(This->hmmio, &ckRIFF, NULL, MMIO_FINDRIFF) != S_OK) { 1019 return AVIFILE_LoadSunFile(This); 1020 } 1021 1022 if (ckRIFF.fccType != formtypeWAVE) 1023 return AVIERR_BADFORMAT; 1024 1025 /* search WAVE format chunk */ 1026 ck.ckid = ckidWAVEFORMAT; 1027 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck, 1028 &ckRIFF, MMIO_FINDCHUNK) != S_OK) 1029 return AVIERR_FILEREAD; 1030 1031 /* get memory for format and read it */ 1032 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, ck.cksize); 1033 if (This->lpFormat == NULL) 1034 return AVIERR_FILEREAD; 1035 This->cbFormat = ck.cksize; 1036 1037 if (mmioRead(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize) 1038 return AVIERR_FILEREAD; 1039 if (mmioAscend(This->hmmio, &ck, 0) != S_OK) 1040 return AVIERR_FILEREAD; 1041 1042 /* Non-pcm formats have a fact chunk. 1043 * We don't need it, so simply add it to the extra chunks. 1044 */ 1045 1046 /* find the big data chunk */ 1047 This->ckData.ckid = ckidWAVEDATA; 1048 if (FindChunkAndKeepExtras(&This->extra, This->hmmio, &This->ckData, 1049 &ckRIFF, MMIO_FINDCHUNK) != S_OK) 1050 return AVIERR_FILEREAD; 1051 1052 memset(&This->sInfo, 0, sizeof(This->sInfo)); 1053 This->sInfo.fccType = streamtypeAUDIO; 1054 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec; 1055 This->sInfo.dwSampleSize = 1056 This->sInfo.dwScale = This->lpFormat->nBlockAlign; 1057 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign; 1058 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize; 1059 1060 This->fInfo.dwStreams = 1; 1061 1062 if (mmioAscend(This->hmmio, &This->ckData, 0) != S_OK) { 1063 /* seems to be truncated */ 1064 WARN(": file seems to be truncated!\n"); 1065 This->ckData.cksize = mmioSeek(This->hmmio, 0, SEEK_END) - 1066 This->ckData.dwDataOffset; 1067 This->sInfo.dwLength = This->ckData.cksize / This->lpFormat->nBlockAlign; 1068 This->sInfo.dwSuggestedBufferSize = This->ckData.cksize; 1069 } 1070 1071 /* ignore errors */ 1072 FindChunkAndKeepExtras(&This->extra, This->hmmio, &ck, &ckRIFF, 0); 1073 1074 return AVIERR_OK; 1075 } 1076 1077 static HRESULT AVIFILE_LoadSunFile(IAVIFileImpl *This) 1078 { 1079 SUNAUDIOHEADER auhdr; 1080 1081 mmioSeek(This->hmmio, 0, SEEK_SET); 1082 if (mmioRead(This->hmmio, (HPSTR)&auhdr, sizeof(auhdr)) != sizeof(auhdr)) 1083 return AVIERR_FILEREAD; 1084 1085 if (auhdr.fccType == 0x0064732E) { 1086 /* header in little endian */ 1087 This->ckData.dwDataOffset = LE2H_DWORD(auhdr.offset); 1088 This->ckData.cksize = LE2H_DWORD(auhdr.size); 1089 1090 auhdr.encoding = LE2H_DWORD(auhdr.encoding); 1091 auhdr.sampleRate = LE2H_DWORD(auhdr.sampleRate); 1092 auhdr.channels = LE2H_DWORD(auhdr.channels); 1093 } else if (auhdr.fccType == mmioFOURCC('.','s','n','d')) { 1094 /* header in big endian */ 1095 This->ckData.dwDataOffset = BE2H_DWORD(auhdr.offset); 1096 This->ckData.cksize = BE2H_DWORD(auhdr.size); 1097 1098 auhdr.encoding = BE2H_DWORD(auhdr.encoding); 1099 auhdr.sampleRate = BE2H_DWORD(auhdr.sampleRate); 1100 auhdr.channels = BE2H_DWORD(auhdr.channels); 1101 } else 1102 return AVIERR_FILEREAD; 1103 1104 if (auhdr.channels < 1) 1105 return AVIERR_BADFORMAT; 1106 1107 /* get size of header */ 1108 switch(auhdr.encoding) { 1109 case AU_ENCODING_ADPCM_G721_32: 1110 This->cbFormat = sizeof(G721_ADPCMWAVEFORMAT); break; 1111 case AU_ENCODING_ADPCM_G723_24: 1112 This->cbFormat = sizeof(G723_ADPCMWAVEFORMAT); break; 1113 case AU_ENCODING_ADPCM_G722: 1114 case AU_ENCODING_ADPCM_G723_5: 1115 WARN("unsupported Sun audio format %d\n", auhdr.encoding); 1116 return AVIERR_UNSUPPORTED; /* FIXME */ 1117 default: 1118 This->cbFormat = sizeof(WAVEFORMATEX); break; 1119 }; 1120 1121 This->lpFormat = HeapAlloc(GetProcessHeap(), 0, This->cbFormat); 1122 if (This->lpFormat == NULL) 1123 return AVIERR_MEMORY; 1124 1125 This->lpFormat->nChannels = auhdr.channels; 1126 This->lpFormat->nSamplesPerSec = auhdr.sampleRate; 1127 switch(auhdr.encoding) { 1128 case AU_ENCODING_ULAW_8: 1129 This->lpFormat->wFormatTag = WAVE_FORMAT_MULAW; 1130 This->lpFormat->wBitsPerSample = 8; 1131 break; 1132 case AU_ENCODING_PCM_8: 1133 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM; 1134 This->lpFormat->wBitsPerSample = 8; 1135 break; 1136 case AU_ENCODING_PCM_16: 1137 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM; 1138 This->lpFormat->wBitsPerSample = 16; 1139 break; 1140 case AU_ENCODING_PCM_24: 1141 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM; 1142 This->lpFormat->wBitsPerSample = 24; 1143 break; 1144 case AU_ENCODING_PCM_32: 1145 This->lpFormat->wFormatTag = WAVE_FORMAT_PCM; 1146 This->lpFormat->wBitsPerSample = 32; 1147 break; 1148 case AU_ENCODING_ALAW_8: 1149 This->lpFormat->wFormatTag = WAVE_FORMAT_ALAW; 1150 This->lpFormat->wBitsPerSample = 8; 1151 break; 1152 case AU_ENCODING_ADPCM_G721_32: 1153 This->lpFormat->wFormatTag = WAVE_FORMAT_G721_ADPCM; 1154 This->lpFormat->wBitsPerSample = (3*5*8); 1155 This->lpFormat->nBlockAlign = 15*15*8; 1156 This->lpFormat->cbSize = sizeof(WORD); 1157 ((LPG721_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0; 1158 break; 1159 case AU_ENCODING_ADPCM_G723_24: 1160 This->lpFormat->wFormatTag = WAVE_FORMAT_G723_ADPCM; 1161 This->lpFormat->wBitsPerSample = (3*5*8); 1162 This->lpFormat->nBlockAlign = 15*15*8; 1163 This->lpFormat->cbSize = 2*sizeof(WORD); 1164 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->cbExtraSize = 0; 1165 ((LPG723_ADPCMWAVEFORMAT)This->lpFormat)->nAuxBlockSize = 0; 1166 break; 1167 default: 1168 WARN("unsupported Sun audio format %d\n", auhdr.encoding); 1169 return AVIERR_UNSUPPORTED; 1170 }; 1171 1172 This->lpFormat->nBlockAlign = 1173 (This->lpFormat->nChannels * This->lpFormat->wBitsPerSample) / 8; 1174 if (This->lpFormat->nBlockAlign == 0 && This->lpFormat->wBitsPerSample < 8) 1175 This->lpFormat->nBlockAlign++; 1176 This->lpFormat->nAvgBytesPerSec = 1177 This->lpFormat->nBlockAlign * This->lpFormat->nSamplesPerSec; 1178 1179 This->fDirty = FALSE; 1180 1181 This->sInfo.fccType = streamtypeAUDIO; 1182 This->sInfo.fccHandler = 0; 1183 This->sInfo.dwFlags = 0; 1184 This->sInfo.wPriority = 0; 1185 This->sInfo.wLanguage = 0; 1186 This->sInfo.dwInitialFrames = 0; 1187 This->sInfo.dwScale = This->lpFormat->nBlockAlign; 1188 This->sInfo.dwRate = This->lpFormat->nAvgBytesPerSec; 1189 This->sInfo.dwStart = 0; 1190 This->sInfo.dwLength = 1191 This->ckData.cksize / This->lpFormat->nBlockAlign; 1192 This->sInfo.dwSuggestedBufferSize = This->sInfo.dwLength; 1193 This->sInfo.dwSampleSize = This->lpFormat->nBlockAlign; 1194 1195 This->fInfo.dwStreams = 1; 1196 This->fInfo.dwScale = 1; 1197 This->fInfo.dwRate = This->lpFormat->nSamplesPerSec; 1198 This->fInfo.dwLength = 1199 MulDiv(This->ckData.cksize, This->lpFormat->nSamplesPerSec, 1200 This->lpFormat->nAvgBytesPerSec); 1201 1202 return AVIERR_OK; 1203 } 1204 1205 static HRESULT AVIFILE_SaveFile(const IAVIFileImpl *This) 1206 { 1207 MMCKINFO ckRIFF; 1208 MMCKINFO ck; 1209 1210 mmioSeek(This->hmmio, 0, SEEK_SET); 1211 1212 /* create the RIFF chunk with formtype WAVE */ 1213 ckRIFF.fccType = formtypeWAVE; 1214 ckRIFF.cksize = 0; 1215 if (mmioCreateChunk(This->hmmio, &ckRIFF, MMIO_CREATERIFF) != S_OK) 1216 return AVIERR_FILEWRITE; 1217 1218 /* the next chunk is the format */ 1219 ck.ckid = ckidWAVEFORMAT; 1220 ck.cksize = This->cbFormat; 1221 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK) 1222 return AVIERR_FILEWRITE; 1223 if (This->lpFormat != NULL && This->cbFormat > 0) { 1224 if (mmioWrite(This->hmmio, (HPSTR)This->lpFormat, ck.cksize) != ck.cksize) 1225 return AVIERR_FILEWRITE; 1226 } 1227 if (mmioAscend(This->hmmio, &ck, 0) != S_OK) 1228 return AVIERR_FILEWRITE; 1229 1230 /* fact chunk is needed for non-pcm waveforms */ 1231 if (This->lpFormat != NULL && This->cbFormat > sizeof(PCMWAVEFORMAT) && 1232 This->lpFormat->wFormatTag != WAVE_FORMAT_PCM) { 1233 WAVEFORMATEX wfx; 1234 DWORD dwFactLength; 1235 HACMSTREAM has; 1236 1237 /* try to open an appropriate audio codec to figure out 1238 * data for fact-chunk */ 1239 wfx.wFormatTag = WAVE_FORMAT_PCM; 1240 if (acmFormatSuggest(NULL, This->lpFormat, &wfx, 1241 sizeof(wfx), ACM_FORMATSUGGESTF_WFORMATTAG)) { 1242 acmStreamOpen(&has, NULL, This->lpFormat, &wfx, NULL, 1243 0, 0, ACM_STREAMOPENF_NONREALTIME); 1244 acmStreamSize(has, This->ckData.cksize, &dwFactLength, 1245 ACM_STREAMSIZEF_SOURCE); 1246 dwFactLength /= wfx.nBlockAlign; 1247 acmStreamClose(has, 0); 1248 1249 /* create the fact chunk */ 1250 ck.ckid = ckidWAVEFACT; 1251 ck.cksize = sizeof(dwFactLength); 1252 1253 /* test for enough space before data chunk */ 1254 if (mmioSeek(This->hmmio, 0, SEEK_CUR) > This->ckData.dwDataOffset 1255 - ck.cksize - 4 * sizeof(DWORD)) 1256 return AVIERR_FILEWRITE; 1257 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK) 1258 return AVIERR_FILEWRITE; 1259 if (mmioWrite(This->hmmio, (HPSTR)&dwFactLength, ck.cksize) != ck.cksize) 1260 return AVIERR_FILEWRITE; 1261 if (mmioAscend(This->hmmio, &ck, 0) != S_OK) 1262 return AVIERR_FILEWRITE; 1263 } else 1264 ERR(": fact chunk is needed for non-pcm files -- currently no codec found, so skipped!\n"); 1265 } 1266 1267 /* if there was extra stuff, we need to fill it with JUNK */ 1268 if (mmioSeek(This->hmmio, 0, SEEK_CUR) + 2 * sizeof(DWORD) < This->ckData.dwDataOffset) { 1269 ck.ckid = ckidAVIPADDING; 1270 ck.cksize = 0; 1271 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK) 1272 return AVIERR_FILEWRITE; 1273 1274 if (mmioSeek(This->hmmio, This->ckData.dwDataOffset 1275 - 2 * sizeof(DWORD), SEEK_SET) == -1) 1276 return AVIERR_FILEWRITE; 1277 if (mmioAscend(This->hmmio, &ck, 0) != S_OK) 1278 return AVIERR_FILEWRITE; 1279 } 1280 1281 /* create the data chunk */ 1282 ck.ckid = ckidWAVEDATA; 1283 ck.cksize = This->ckData.cksize; 1284 if (mmioCreateChunk(This->hmmio, &ck, 0) != S_OK) 1285 return AVIERR_FILEWRITE; 1286 if (mmioSeek(This->hmmio, This->ckData.cksize, SEEK_CUR) == -1) 1287 return AVIERR_FILEWRITE; 1288 if (mmioAscend(This->hmmio, &ck, 0) != S_OK) 1289 return AVIERR_FILEWRITE; 1290 1291 /* some optional extra chunks? */ 1292 if (This->extra.lp != NULL && This->extra.cb > 0) { 1293 /* chunk headers are already in structure */ 1294 if (mmioWrite(This->hmmio, This->extra.lp, This->extra.cb) != This->extra.cb) 1295 return AVIERR_FILEWRITE; 1296 } 1297 1298 /* close RIFF chunk */ 1299 if (mmioAscend(This->hmmio, &ckRIFF, 0) != S_OK) 1300 return AVIERR_FILEWRITE; 1301 if (mmioFlush(This->hmmio, 0) != S_OK) 1302 return AVIERR_FILEWRITE; 1303 1304 return AVIERR_OK; 1305 } 1306