ntoskrnl: Implemented IoDeleteSymbolicLink.
[wine/multimedia.git] / dlls / mciavi32 / mmoutput.c
blobf7118ff6ca325636ad25ad18952d879fe4d462e6
1 /*
2 * Digital video MCI Wine Driver
4 * Copyright 1999, 2000 Eric POUECH
5 * Copyright 2003 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "private_mciavi.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(mciavi);
27 static BOOL MCIAVI_GetInfoAudio(WINE_MCIAVI* wma, const MMCKINFO* mmckList, MMCKINFO *mmckStream)
29 MMCKINFO mmckInfo;
31 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(wma->ash_audio.fccType)),
32 HIBYTE(LOWORD(wma->ash_audio.fccType)),
33 LOBYTE(HIWORD(wma->ash_audio.fccType)),
34 HIBYTE(HIWORD(wma->ash_audio.fccType)));
35 if (wma->ash_audio.fccHandler) /* not all streams specify a handler */
36 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(wma->ash_audio.fccHandler)),
37 HIBYTE(LOWORD(wma->ash_audio.fccHandler)),
38 LOBYTE(HIWORD(wma->ash_audio.fccHandler)),
39 HIBYTE(HIWORD(wma->ash_audio.fccHandler)));
40 else
41 TRACE("ash.fccHandler=0, no handler specified\n");
42 TRACE("ash.dwFlags=%d\n", wma->ash_audio.dwFlags);
43 TRACE("ash.wPriority=%d\n", wma->ash_audio.wPriority);
44 TRACE("ash.wLanguage=%d\n", wma->ash_audio.wLanguage);
45 TRACE("ash.dwInitialFrames=%d\n", wma->ash_audio.dwInitialFrames);
46 TRACE("ash.dwScale=%d\n", wma->ash_audio.dwScale);
47 TRACE("ash.dwRate=%d\n", wma->ash_audio.dwRate);
48 TRACE("ash.dwStart=%d\n", wma->ash_audio.dwStart);
49 TRACE("ash.dwLength=%d\n", wma->ash_audio.dwLength);
50 TRACE("ash.dwSuggestedBufferSize=%d\n", wma->ash_audio.dwSuggestedBufferSize);
51 TRACE("ash.dwQuality=%d\n", wma->ash_audio.dwQuality);
52 TRACE("ash.dwSampleSize=%d\n", wma->ash_audio.dwSampleSize);
53 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", wma->ash_audio.rcFrame.top, wma->ash_audio.rcFrame.left,
54 wma->ash_audio.rcFrame.bottom, wma->ash_audio.rcFrame.right);
56 /* rewind to the start of the stream */
57 mmioAscend(wma->hFile, mmckStream, 0);
59 mmckInfo.ckid = ckidSTREAMFORMAT;
60 if (mmioDescend(wma->hFile, &mmckInfo, mmckList, MMIO_FINDCHUNK) != 0) {
61 WARN("Can't find 'strf' chunk\n");
62 return FALSE;
64 if (mmckInfo.cksize < sizeof(WAVEFORMAT)) {
65 WARN("Size of strf chunk (%d) < audio format struct\n", mmckInfo.cksize);
66 return FALSE;
68 wma->lpWaveFormat = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
69 if (!wma->lpWaveFormat) {
70 WARN("Can't alloc WaveFormat\n");
71 return FALSE;
74 mmioRead(wma->hFile, (LPSTR)wma->lpWaveFormat, mmckInfo.cksize);
76 TRACE("waveFormat.wFormatTag=%d\n", wma->lpWaveFormat->wFormatTag);
77 TRACE("waveFormat.nChannels=%d\n", wma->lpWaveFormat->nChannels);
78 TRACE("waveFormat.nSamplesPerSec=%d\n", wma->lpWaveFormat->nSamplesPerSec);
79 TRACE("waveFormat.nAvgBytesPerSec=%d\n", wma->lpWaveFormat->nAvgBytesPerSec);
80 TRACE("waveFormat.nBlockAlign=%d\n", wma->lpWaveFormat->nBlockAlign);
81 TRACE("waveFormat.wBitsPerSample=%d\n", wma->lpWaveFormat->wBitsPerSample);
82 if (mmckInfo.cksize >= sizeof(WAVEFORMATEX))
83 TRACE("waveFormat.cbSize=%d\n", wma->lpWaveFormat->cbSize);
85 return TRUE;
88 static BOOL MCIAVI_GetInfoVideo(WINE_MCIAVI* wma, const MMCKINFO* mmckList, MMCKINFO* mmckStream)
90 MMCKINFO mmckInfo;
92 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(wma->ash_video.fccType)),
93 HIBYTE(LOWORD(wma->ash_video.fccType)),
94 LOBYTE(HIWORD(wma->ash_video.fccType)),
95 HIBYTE(HIWORD(wma->ash_video.fccType)));
96 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(wma->ash_video.fccHandler)),
97 HIBYTE(LOWORD(wma->ash_video.fccHandler)),
98 LOBYTE(HIWORD(wma->ash_video.fccHandler)),
99 HIBYTE(HIWORD(wma->ash_video.fccHandler)));
100 TRACE("ash.dwFlags=%d\n", wma->ash_video.dwFlags);
101 TRACE("ash.wPriority=%d\n", wma->ash_video.wPriority);
102 TRACE("ash.wLanguage=%d\n", wma->ash_video.wLanguage);
103 TRACE("ash.dwInitialFrames=%d\n", wma->ash_video.dwInitialFrames);
104 TRACE("ash.dwScale=%d\n", wma->ash_video.dwScale);
105 TRACE("ash.dwRate=%d\n", wma->ash_video.dwRate);
106 TRACE("ash.dwStart=%d\n", wma->ash_video.dwStart);
107 TRACE("ash.dwLength=%d\n", wma->ash_video.dwLength);
108 TRACE("ash.dwSuggestedBufferSize=%d\n", wma->ash_video.dwSuggestedBufferSize);
109 TRACE("ash.dwQuality=%d\n", wma->ash_video.dwQuality);
110 TRACE("ash.dwSampleSize=%d\n", wma->ash_video.dwSampleSize);
111 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", wma->ash_video.rcFrame.top, wma->ash_video.rcFrame.left,
112 wma->ash_video.rcFrame.bottom, wma->ash_video.rcFrame.right);
114 /* rewind to the start of the stream */
115 mmioAscend(wma->hFile, mmckStream, 0);
117 mmckInfo.ckid = ckidSTREAMFORMAT;
118 if (mmioDescend(wma->hFile, &mmckInfo, mmckList, MMIO_FINDCHUNK) != 0) {
119 WARN("Can't find 'strf' chunk\n");
120 return FALSE;
123 wma->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
124 if (!wma->inbih) {
125 WARN("Can't alloc input BIH\n");
126 return FALSE;
129 mmioRead(wma->hFile, (LPSTR)wma->inbih, mmckInfo.cksize);
131 TRACE("bih.biSize=%d\n", wma->inbih->biSize);
132 TRACE("bih.biWidth=%d\n", wma->inbih->biWidth);
133 TRACE("bih.biHeight=%d\n", wma->inbih->biHeight);
134 TRACE("bih.biPlanes=%d\n", wma->inbih->biPlanes);
135 TRACE("bih.biBitCount=%d\n", wma->inbih->biBitCount);
136 TRACE("bih.biCompression=%x\n", wma->inbih->biCompression);
137 TRACE("bih.biSizeImage=%d\n", wma->inbih->biSizeImage);
138 TRACE("bih.biXPelsPerMeter=%d\n", wma->inbih->biXPelsPerMeter);
139 TRACE("bih.biYPelsPerMeter=%d\n", wma->inbih->biYPelsPerMeter);
140 TRACE("bih.biClrUsed=%d\n", wma->inbih->biClrUsed);
141 TRACE("bih.biClrImportant=%d\n", wma->inbih->biClrImportant);
143 wma->source.left = 0;
144 wma->source.top = 0;
145 wma->source.right = wma->inbih->biWidth;
146 wma->source.bottom = wma->inbih->biHeight;
148 wma->dest = wma->source;
150 return TRUE;
153 struct AviListBuild {
154 DWORD numVideoFrames;
155 DWORD numAudioAllocated;
156 DWORD numAudioBlocks;
157 DWORD inVideoSize;
158 DWORD inAudioSize;
161 static BOOL MCIAVI_AddFrame(WINE_MCIAVI* wma, LPMMCKINFO mmck,
162 struct AviListBuild* alb)
164 const BYTE *p;
165 DWORD stream_n;
166 DWORD twocc;
168 if (mmck->ckid == ckidAVIPADDING) return TRUE;
170 p = (const BYTE *)&mmck->ckid;
172 if (!isxdigit(p[0]) || !isxdigit(p[1]))
174 WARN("wrongly encoded stream #\n");
175 return FALSE;
178 stream_n = (p[0] <= '9') ? (p[0] - '0') : (tolower(p[0]) - 'a' + 10);
179 stream_n <<= 4;
180 stream_n |= (p[1] <= '9') ? (p[1] - '0') : (tolower(p[1]) - 'a' + 10);
182 TRACE("ckid %4.4s (stream #%d)\n", (LPSTR)&mmck->ckid, stream_n);
184 /* Some (rare?) AVI files have video streams name XXYY where XX = stream number and YY = TWOCC
185 * of the last 2 characters of the biCompression member of the BITMAPINFOHEADER structure.
186 * Ex: fccHandler = IV32 & biCompression = IV32 => stream name = XX32
187 * fccHandler = MSVC & biCompression = CRAM => stream name = XXAM
188 * Another possibility is that these TWOCC are simply ignored.
189 * Default to cktypeDIBcompressed when this case happens.
191 twocc = TWOCCFromFOURCC(mmck->ckid);
192 if (twocc == TWOCCFromFOURCC(wma->inbih->biCompression))
193 twocc = cktypeDIBcompressed;
195 switch (twocc) {
196 case cktypeDIBbits:
197 case cktypeDIBcompressed:
198 case cktypePALchange:
199 if (stream_n != wma->video_stream_n)
201 TRACE("data belongs to another video stream #%d\n", stream_n);
202 return FALSE;
205 TRACE("Adding video frame[%d]: %d bytes\n",
206 alb->numVideoFrames, mmck->cksize);
208 if (alb->numVideoFrames < wma->dwPlayableVideoFrames) {
209 wma->lpVideoIndex[alb->numVideoFrames].dwOffset = mmck->dwDataOffset;
210 wma->lpVideoIndex[alb->numVideoFrames].dwSize = mmck->cksize;
211 if (alb->inVideoSize < mmck->cksize)
212 alb->inVideoSize = mmck->cksize;
213 alb->numVideoFrames++;
214 } else {
215 WARN("Too many video frames\n");
217 break;
218 case cktypeWAVEbytes:
219 if (stream_n != wma->audio_stream_n)
221 TRACE("data belongs to another audio stream #%d\n", stream_n);
222 return FALSE;
225 TRACE("Adding audio frame[%d]: %d bytes\n",
226 alb->numAudioBlocks, mmck->cksize);
227 if (wma->lpWaveFormat) {
228 if (alb->numAudioBlocks >= alb->numAudioAllocated) {
229 alb->numAudioAllocated += 32;
230 if (!wma->lpAudioIndex)
231 wma->lpAudioIndex = HeapAlloc(GetProcessHeap(), 0,
232 alb->numAudioAllocated * sizeof(struct MMIOPos));
233 else
234 wma->lpAudioIndex = HeapReAlloc(GetProcessHeap(), 0, wma->lpAudioIndex,
235 alb->numAudioAllocated * sizeof(struct MMIOPos));
236 if (!wma->lpAudioIndex) return FALSE;
238 wma->lpAudioIndex[alb->numAudioBlocks].dwOffset = mmck->dwDataOffset;
239 wma->lpAudioIndex[alb->numAudioBlocks].dwSize = mmck->cksize;
240 if (alb->inAudioSize < mmck->cksize)
241 alb->inAudioSize = mmck->cksize;
242 alb->numAudioBlocks++;
243 } else {
244 WARN("Wave chunk without wave format... discarding\n");
246 break;
247 default:
248 WARN("Unknown frame type %4.4s\n", (LPSTR)&mmck->ckid);
249 break;
251 return TRUE;
254 BOOL MCIAVI_GetInfo(WINE_MCIAVI* wma)
256 MMCKINFO ckMainRIFF;
257 MMCKINFO mmckHead;
258 MMCKINFO mmckList;
259 MMCKINFO mmckInfo;
260 AVIStreamHeader strh;
261 struct AviListBuild alb;
262 DWORD stream_n;
264 if (mmioDescend(wma->hFile, &ckMainRIFF, NULL, 0) != 0) {
265 WARN("Can't find 'RIFF' chunk\n");
266 return FALSE;
269 if ((ckMainRIFF.ckid != FOURCC_RIFF) || (ckMainRIFF.fccType != formtypeAVI)) {
270 WARN("Can't find 'AVI ' chunk\n");
271 return FALSE;
274 mmckHead.fccType = listtypeAVIHEADER;
275 if (mmioDescend(wma->hFile, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
276 WARN("Can't find 'hdrl' list\n");
277 return FALSE;
280 mmckInfo.ckid = ckidAVIMAINHDR;
281 if (mmioDescend(wma->hFile, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
282 WARN("Can't find 'avih' chunk\n");
283 return FALSE;
286 mmioRead(wma->hFile, (LPSTR)&wma->mah, sizeof(wma->mah));
288 TRACE("mah.dwMicroSecPerFrame=%d\n", wma->mah.dwMicroSecPerFrame);
289 TRACE("mah.dwMaxBytesPerSec=%d\n", wma->mah.dwMaxBytesPerSec);
290 TRACE("mah.dwPaddingGranularity=%d\n", wma->mah.dwPaddingGranularity);
291 TRACE("mah.dwFlags=%d\n", wma->mah.dwFlags);
292 TRACE("mah.dwTotalFrames=%d\n", wma->mah.dwTotalFrames);
293 TRACE("mah.dwInitialFrames=%d\n", wma->mah.dwInitialFrames);
294 TRACE("mah.dwStreams=%d\n", wma->mah.dwStreams);
295 TRACE("mah.dwSuggestedBufferSize=%d\n", wma->mah.dwSuggestedBufferSize);
296 TRACE("mah.dwWidth=%d\n", wma->mah.dwWidth);
297 TRACE("mah.dwHeight=%d\n", wma->mah.dwHeight);
299 mmioAscend(wma->hFile, &mmckInfo, 0);
301 TRACE("Start of streams\n");
302 wma->video_stream_n = 0;
303 wma->audio_stream_n = 0;
305 for (stream_n = 0; stream_n < wma->mah.dwStreams; stream_n++)
307 MMCKINFO mmckStream;
309 mmckList.fccType = listtypeSTREAMHEADER;
310 if (mmioDescend(wma->hFile, &mmckList, &mmckHead, MMIO_FINDLIST) != 0)
311 break;
313 mmckStream.ckid = ckidSTREAMHEADER;
314 if (mmioDescend(wma->hFile, &mmckStream, &mmckList, MMIO_FINDCHUNK) != 0)
316 WARN("Can't find 'strh' chunk\n");
317 continue;
320 mmioRead(wma->hFile, (LPSTR)&strh, sizeof(strh));
322 TRACE("Stream #%d fccType %4.4s\n", stream_n, (LPSTR)&strh.fccType);
324 if (strh.fccType == streamtypeVIDEO)
326 TRACE("found video stream\n");
327 if (wma->inbih)
328 WARN("ignoring another video stream\n");
329 else
331 wma->ash_audio = strh;
333 if (!MCIAVI_GetInfoVideo(wma, &mmckList, &mmckStream))
334 return FALSE;
335 wma->video_stream_n = stream_n;
338 else if (strh.fccType == streamtypeAUDIO)
340 TRACE("found audio stream\n");
341 if (wma->lpWaveFormat)
342 WARN("ignoring another audio stream\n");
343 else
345 wma->ash_video = strh;
347 if (!MCIAVI_GetInfoAudio(wma, &mmckList, &mmckStream))
348 return FALSE;
349 wma->audio_stream_n = stream_n;
352 else
353 TRACE("Unsupported stream type %4.4s\n", (LPSTR)&strh.fccType);
355 mmioAscend(wma->hFile, &mmckList, 0);
358 TRACE("End of streams\n");
360 mmioAscend(wma->hFile, &mmckHead, 0);
362 /* no need to read optional JUNK chunk */
364 mmckList.fccType = listtypeAVIMOVIE;
365 if (mmioDescend(wma->hFile, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
366 WARN("Can't find 'movi' list\n");
367 return FALSE;
370 wma->dwPlayableVideoFrames = wma->mah.dwTotalFrames;
371 wma->lpVideoIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
372 wma->dwPlayableVideoFrames * sizeof(struct MMIOPos));
373 if (!wma->lpVideoIndex) {
374 WARN("Can't alloc video index array\n");
375 return FALSE;
377 wma->dwPlayableAudioBlocks = 0;
378 wma->lpAudioIndex = NULL;
380 alb.numAudioBlocks = alb.numVideoFrames = 0;
381 alb.inVideoSize = alb.inAudioSize = 0;
382 alb.numAudioAllocated = 0;
384 while (mmioDescend(wma->hFile, &mmckInfo, &mmckList, 0) == 0) {
385 if (mmckInfo.fccType == listtypeAVIRECORD) {
386 MMCKINFO tmp;
388 while (mmioDescend(wma->hFile, &tmp, &mmckInfo, 0) == 0) {
389 MCIAVI_AddFrame(wma, &tmp, &alb);
390 mmioAscend(wma->hFile, &tmp, 0);
392 } else {
393 MCIAVI_AddFrame(wma, &mmckInfo, &alb);
396 mmioAscend(wma->hFile, &mmckInfo, 0);
398 if (alb.numVideoFrames != wma->dwPlayableVideoFrames) {
399 WARN("Found %d video frames (/%d), reducing playable frames\n",
400 alb.numVideoFrames, wma->dwPlayableVideoFrames);
401 wma->dwPlayableVideoFrames = alb.numVideoFrames;
403 wma->dwPlayableAudioBlocks = alb.numAudioBlocks;
405 if (alb.inVideoSize > wma->ash_video.dwSuggestedBufferSize) {
406 WARN("inVideoSize=%d suggestedSize=%d\n", alb.inVideoSize, wma->ash_video.dwSuggestedBufferSize);
407 wma->ash_video.dwSuggestedBufferSize = alb.inVideoSize;
409 if (alb.inAudioSize > wma->ash_audio.dwSuggestedBufferSize) {
410 WARN("inAudioSize=%d suggestedSize=%d\n", alb.inAudioSize, wma->ash_audio.dwSuggestedBufferSize);
411 wma->ash_audio.dwSuggestedBufferSize = alb.inAudioSize;
414 wma->indata = HeapAlloc(GetProcessHeap(), 0, wma->ash_video.dwSuggestedBufferSize);
415 if (!wma->indata) {
416 WARN("Can't alloc input buffer\n");
417 return FALSE;
420 return TRUE;
423 BOOL MCIAVI_OpenVideo(WINE_MCIAVI* wma)
425 HDC hDC;
426 DWORD outSize;
427 FOURCC fcc = wma->ash_video.fccHandler;
429 TRACE("fcc %4.4s\n", (LPSTR)&fcc);
431 wma->dwCachedFrame = -1;
433 /* get the right handle */
434 if (fcc == mmioFOURCC('C','R','A','M')) fcc = mmioFOURCC('M','S','V','C');
436 /* try to get a decompressor for that type */
437 wma->hic = ICLocate(ICTYPE_VIDEO, fcc, wma->inbih, NULL, ICMODE_DECOMPRESS);
438 if (!wma->hic) {
439 /* check for builtin DIB compressions */
440 fcc = wma->inbih->biCompression;
441 if ((fcc == mmioFOURCC('D','I','B',' ')) ||
442 (fcc == mmioFOURCC('R','L','E',' ')) ||
443 (fcc == BI_RGB) || (fcc == BI_RLE8) ||
444 (fcc == BI_RLE4) || (fcc == BI_BITFIELDS))
445 goto paint_frame;
447 WARN("Can't locate codec for the file\n");
448 return FALSE;
451 outSize = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
453 wma->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
454 if (!wma->outbih) {
455 WARN("Can't alloc output BIH\n");
456 return FALSE;
458 if (!ICGetDisplayFormat(wma->hic, wma->inbih, wma->outbih, 0, 0, 0)) {
459 WARN("Can't open decompressor\n");
460 return FALSE;
463 TRACE("bih.biSize=%d\n", wma->outbih->biSize);
464 TRACE("bih.biWidth=%d\n", wma->outbih->biWidth);
465 TRACE("bih.biHeight=%d\n", wma->outbih->biHeight);
466 TRACE("bih.biPlanes=%d\n", wma->outbih->biPlanes);
467 TRACE("bih.biBitCount=%d\n", wma->outbih->biBitCount);
468 TRACE("bih.biCompression=%x\n", wma->outbih->biCompression);
469 TRACE("bih.biSizeImage=%d\n", wma->outbih->biSizeImage);
470 TRACE("bih.biXPelsPerMeter=%d\n", wma->outbih->biXPelsPerMeter);
471 TRACE("bih.biYPelsPerMeter=%d\n", wma->outbih->biYPelsPerMeter);
472 TRACE("bih.biClrUsed=%d\n", wma->outbih->biClrUsed);
473 TRACE("bih.biClrImportant=%d\n", wma->outbih->biClrImportant);
475 wma->outdata = HeapAlloc(GetProcessHeap(), 0, wma->outbih->biSizeImage);
476 if (!wma->outdata) {
477 WARN("Can't alloc output buffer\n");
478 return FALSE;
481 if (ICSendMessage(wma->hic, ICM_DECOMPRESS_BEGIN,
482 (DWORD_PTR)wma->inbih, (DWORD_PTR)wma->outbih) != ICERR_OK) {
483 WARN("Can't begin decompression\n");
484 return FALSE;
487 paint_frame:
488 hDC = wma->hWndPaint ? GetDC(wma->hWndPaint) : 0;
489 if (hDC)
491 MCIAVI_PaintFrame(wma, hDC);
492 ReleaseDC(wma->hWndPaint, hDC);
494 return TRUE;
497 static void CALLBACK MCIAVI_waveCallback(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
498 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
500 WINE_MCIAVI *wma = MCIAVI_mciGetOpenDev(dwInstance);
502 if (!wma) return;
504 EnterCriticalSection(&wma->cs);
506 switch (uMsg) {
507 case WOM_OPEN:
508 case WOM_CLOSE:
509 break;
510 case WOM_DONE:
511 InterlockedIncrement(&wma->dwEventCount);
512 TRACE("Returning waveHdr=%lx\n", dwParam1);
513 SetEvent(wma->hEvent);
514 break;
515 default:
516 ERR("Unknown uMsg=%d\n", uMsg);
519 LeaveCriticalSection(&wma->cs);
522 DWORD MCIAVI_OpenAudio(WINE_MCIAVI* wma, unsigned* nHdr, LPWAVEHDR* pWaveHdr)
524 DWORD dwRet;
525 LPWAVEHDR waveHdr;
526 unsigned i;
528 dwRet = waveOutOpen((HWAVEOUT *)&wma->hWave, WAVE_MAPPER, wma->lpWaveFormat,
529 (DWORD_PTR)MCIAVI_waveCallback, wma->wDevID, CALLBACK_FUNCTION);
530 if (dwRet != 0) {
531 TRACE("Can't open low level audio device %d\n", dwRet);
532 dwRet = MCIERR_DEVICE_OPEN;
533 wma->hWave = 0;
534 goto cleanUp;
537 /* FIXME: should set up a heuristic to compute the number of wave headers
538 * to be used...
540 *nHdr = 7;
541 waveHdr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
542 *nHdr * (sizeof(WAVEHDR) + wma->ash_audio.dwSuggestedBufferSize));
543 if (!waveHdr) {
544 TRACE("Can't alloc wave headers\n");
545 dwRet = MCIERR_DEVICE_OPEN;
546 goto cleanUp;
549 for (i = 0; i < *nHdr; i++) {
550 /* other fields are zero:ed on allocation */
551 waveHdr[i].lpData = (char*)waveHdr +
552 *nHdr * sizeof(WAVEHDR) + i * wma->ash_audio.dwSuggestedBufferSize;
553 waveHdr[i].dwBufferLength = wma->ash_audio.dwSuggestedBufferSize;
554 if (waveOutPrepareHeader(wma->hWave, &waveHdr[i], sizeof(WAVEHDR))) {
555 dwRet = MCIERR_INTERNAL;
556 goto cleanUp;
560 if (wma->dwCurrVideoFrame != 0 && wma->lpWaveFormat) {
561 FIXME("Should recompute dwCurrAudioBlock, except unsynchronized sound & video\n");
563 wma->dwCurrAudioBlock = 0;
565 wma->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
566 wma->dwEventCount = *nHdr - 1;
567 *pWaveHdr = waveHdr;
568 cleanUp:
569 return dwRet;
572 void MCIAVI_PlayAudioBlocks(WINE_MCIAVI* wma, unsigned nHdr, LPWAVEHDR waveHdr)
574 if (!wma->lpAudioIndex)
575 return;
576 TRACE("%d (ec=%u)\n", wma->lpAudioIndex[wma->dwCurrAudioBlock].dwOffset, wma->dwEventCount);
578 /* push as many blocks as possible => audio gets priority */
579 while (wma->dwStatus != MCI_MODE_STOP && wma->dwStatus != MCI_MODE_NOT_READY &&
580 wma->dwCurrAudioBlock < wma->dwPlayableAudioBlocks) {
581 unsigned whidx = wma->dwCurrAudioBlock % nHdr;
583 ResetEvent(wma->hEvent);
584 if (InterlockedDecrement(&wma->dwEventCount) < 0 ||
585 !wma->lpAudioIndex[wma->dwCurrAudioBlock].dwOffset)
587 InterlockedIncrement(&wma->dwEventCount);
588 break;
591 mmioSeek(wma->hFile, wma->lpAudioIndex[wma->dwCurrAudioBlock].dwOffset, SEEK_SET);
592 mmioRead(wma->hFile, waveHdr[whidx].lpData, wma->lpAudioIndex[wma->dwCurrAudioBlock].dwSize);
594 waveHdr[whidx].dwFlags &= ~WHDR_DONE;
595 waveHdr[whidx].dwBufferLength = wma->lpAudioIndex[wma->dwCurrAudioBlock].dwSize;
596 waveOutWrite(wma->hWave, &waveHdr[whidx], sizeof(WAVEHDR));
597 wma->dwCurrAudioBlock++;
601 LRESULT MCIAVI_PaintFrame(WINE_MCIAVI* wma, HDC hDC)
603 void* pBitmapData;
604 LPBITMAPINFO pBitmapInfo;
606 if (!hDC || !wma->inbih)
607 return TRUE;
609 TRACE("Painting frame %u (cached %u)\n", wma->dwCurrVideoFrame, wma->dwCachedFrame);
611 if (wma->dwCurrVideoFrame != wma->dwCachedFrame)
613 if (!wma->lpVideoIndex[wma->dwCurrVideoFrame].dwOffset)
614 return FALSE;
616 if (wma->lpVideoIndex[wma->dwCurrVideoFrame].dwSize)
618 mmioSeek(wma->hFile, wma->lpVideoIndex[wma->dwCurrVideoFrame].dwOffset, SEEK_SET);
619 mmioRead(wma->hFile, wma->indata, wma->lpVideoIndex[wma->dwCurrVideoFrame].dwSize);
621 wma->inbih->biSizeImage = wma->lpVideoIndex[wma->dwCurrVideoFrame].dwSize;
623 if (wma->hic && ICDecompress(wma->hic, 0, wma->inbih, wma->indata,
624 wma->outbih, wma->outdata) != ICERR_OK)
626 WARN("Decompression error\n");
627 return FALSE;
631 wma->dwCachedFrame = wma->dwCurrVideoFrame;
634 if (wma->hic) {
635 pBitmapData = wma->outdata;
636 pBitmapInfo = (LPBITMAPINFO)wma->outbih;
637 } else {
638 pBitmapData = wma->indata;
639 pBitmapInfo = (LPBITMAPINFO)wma->inbih;
642 StretchDIBits(hDC,
643 wma->dest.left, wma->dest.top,
644 wma->dest.right - wma->dest.left, wma->dest.bottom - wma->dest.top,
645 wma->source.left, wma->source.top,
646 wma->source.right - wma->source.left, wma->source.bottom - wma->source.top,
647 pBitmapData, pBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
649 return TRUE;