[PATCH 2/2] dmime: Reimplement the track form parser

Michael Stefaniuc mstefani at winehq.org
Wed Jul 11 16:44:36 CDT 2018


Signed-off-by: Michael Stefaniuc <mstefani at winehq.org>
---
 dlls/dmime/segment.c | 239 +++++++++++++--------------------------------------
 1 file changed, 60 insertions(+), 179 deletions(-)

diff --git a/dlls/dmime/segment.c b/dlls/dmime/segment.c
index 48fd6438e1..dc460690f6 100644
--- a/dlls/dmime/segment.c
+++ b/dlls/dmime/segment.c
@@ -22,7 +22,6 @@
 #include "dmobject.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
-WINE_DECLARE_DEBUG_CHANNEL(dmfile);
 
 /*****************************************************************************
  * IDirectMusicSegmentImpl implementation
@@ -622,195 +621,77 @@ static const IDirectMusicObjectVtbl dmobject_vtbl = {
 };
 
 /* IDirectMusicSegment8Impl IPersistStream part: */
-static HRESULT load_track(IDirectMusicSegment8Impl *This, IStream *pClonedStream,
-        IDirectMusicTrack **ppTrack, DMUS_IO_TRACK_HEADER *pTrack_hdr)
-{
-  HRESULT hr = E_FAIL;
-  IPersistStream* pPersistStream = NULL;
-  
-  hr = CoCreateInstance (&pTrack_hdr->guidClassID, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicTrack, (LPVOID*) ppTrack);
-  if (FAILED(hr)) {
-    ERR(": could not create object\n");
-    return hr;
-  }
-  /* acquire PersistStream interface */
-  hr = IDirectMusicTrack_QueryInterface (*ppTrack, &IID_IPersistStream, (LPVOID*) &pPersistStream);
-  if (FAILED(hr)) {
-    ERR(": could not acquire IPersistStream\n");
-    return hr;
-  }
-  /* load */
-  hr = IPersistStream_Load (pPersistStream, pClonedStream);
-  if (FAILED(hr)) {
-    ERR(": failed to load object\n");
-    return hr;
-  }
-  
-  /* release all loading-related stuff */
-  IPersistStream_Release (pPersistStream);
-
-  hr = IDirectMusicSegment8_InsertTrack(&This->IDirectMusicSegment8_iface, *ppTrack,
-                                        pTrack_hdr->dwGroup); /* at dsPosition */
-  if (FAILED(hr)) {
-    ERR(": could not insert track\n");
-    return hr;
-  }
-
-  return S_OK;
-}
-
-static HRESULT parse_track_form(IDirectMusicSegment8Impl *This, DWORD size, IStream *pStm)
+static HRESULT parse_track_form(IDirectMusicSegment8Impl *This, IStream *stream,
+        const struct chunk_entry *riff)
 {
-  HRESULT hr = E_FAIL;
-  DMUS_PRIVATE_CHUNK Chunk;
-  DWORD StreamSize, StreamCount, ListSize[3];
-  LARGE_INTEGER liMove; /* used when skipping chunks */
-
-  DMUS_IO_TRACK_HEADER        track_hdr;
-  DMUS_IO_TRACK_EXTRAS_HEADER track_xhdr;
-  IDirectMusicTrack*          pTrack = NULL;
-
-  StreamSize = size - sizeof(FOURCC);
-  StreamCount = 0;
+    struct chunk_entry chunk = {.parent = riff};
+    IDirectMusicTrack *track = NULL;
+    IPersistStream *ps = NULL;
+    IStream *clone;
+    DMUS_IO_TRACK_HEADER thdr;
+    HRESULT hr;
 
-  do {
-    IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
-    StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
-    TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
-    
-    switch (Chunk.fccID) {
-    case DMUS_FOURCC_TRACK_CHUNK: {
-      TRACE_(dmfile)(": track chunk\n");
-      IStream_Read (pStm, &track_hdr, sizeof(DMUS_IO_TRACK_HEADER), NULL);
-      TRACE_(dmfile)(" - class: %s\n", debugstr_guid (&track_hdr.guidClassID));
-      TRACE_(dmfile)(" - dwGroup: %d\n", track_hdr.dwGroup);
-      TRACE_(dmfile)(" - ckid: %s\n", debugstr_fourcc (track_hdr.ckid));
-      TRACE_(dmfile)(" - fccType: %s\n", debugstr_fourcc (track_hdr.fccType));
-      break;
-    }
-    case DMUS_FOURCC_TRACK_EXTRAS_CHUNK: {
-      TRACE_(dmfile)(": track extras chunk\n");
-      IStream_Read (pStm, &track_xhdr, sizeof(DMUS_IO_TRACK_EXTRAS_HEADER), NULL);
-      break;
-    }
+    TRACE("Parsing track form in %p: %s\n", stream, debugstr_chunk(riff));
 
-    case DMUS_FOURCC_COMMANDTRACK_CHUNK: {
-      TRACE_(dmfile)(": COMMANDTRACK track\n");
-      liMove.QuadPart = Chunk.dwSize;
-      IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
-      break;
+    /* First chunk must be the track header */
+    if (FAILED(hr = stream_get_chunk(stream, &chunk)))
+        return hr;
+    if (chunk.id != DMUS_FOURCC_TRACK_CHUNK)
+        return DMUS_E_TRACK_HDR_NOT_FIRST_CK;
+    if (FAILED(hr = stream_chunk_get_data(stream, &chunk, &thdr, sizeof(thdr))))
+        return hr;
+    TRACE("Found DMUS_IO_TRACK_HEADER\n");
+    TRACE("\tclass: %s\n", debugstr_guid (&thdr.guidClassID));
+    TRACE("\tdwGroup: %d\n", thdr.dwGroup);
+    TRACE("\tckid: %s\n", debugstr_fourcc (thdr.ckid));
+    TRACE("\tfccType: %s\n", debugstr_fourcc (thdr.fccType));
+
+    if (!!thdr.ckid == !!thdr.fccType) {
+        WARN("One and only one of the ckid (%s) and fccType (%s) need to be set\n",
+                debugstr_fourcc(thdr.ckid), debugstr_fourcc(thdr.fccType));
+        return DMUS_E_INVALID_TRACK_HDR;
     }
 
-    case FOURCC_LIST: {
-      IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
-      TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
-      ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
-      if (Chunk.fccID == track_hdr.fccType && 0 == track_hdr.ckid) {
-	LPSTREAM pClonedStream = NULL;
-
-	TRACE_(dmfile)(": TRACK list\n");
-
-	IStream_Clone (pStm, &pClonedStream);
-
-	liMove.QuadPart = 0;
-	liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
-	IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
-
-        hr = load_track(This, pClonedStream, &pTrack, &track_hdr);
-	if (FAILED(hr)) {
-	  ERR(": could not load track\n");
-	  return hr;
-	}
-	IStream_Release (pClonedStream);
-	
-	IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
-
-	liMove.QuadPart = ListSize[0];
-	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
-	
-      } else {
-	TRACE_(dmfile)(": unknown (skipping)\n");
-	liMove.QuadPart = Chunk.dwSize;
-	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
-      }
-      break;
-    }
+    /* Optional chunks */
+    while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
+        if ((thdr.ckid && chunk.id == thdr.ckid) ||
+                (!thdr.ckid && (chunk.id == FOURCC_LIST || chunk.id == FOURCC_RIFF) &&
+                 chunk.type == thdr.fccType))
+            break;
 
-    case FOURCC_RIFF: {
-      IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
-      TRACE_(dmfile)(": RIFF chunk of type %s\n", debugstr_fourcc(Chunk.fccID));
-
-      ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
-
-      if (Chunk.fccID == track_hdr.fccType && 0 == track_hdr.ckid) {	
-	LPSTREAM pClonedStream = NULL;
-
-	TRACE_(dmfile)(": TRACK RIFF\n");
-
-	IStream_Clone (pStm, &pClonedStream);
-	
-	liMove.QuadPart = 0;
-	liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
-	IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
-
-        hr = load_track(This, pClonedStream, &pTrack, &track_hdr);
-	if (FAILED(hr)) {
-	  ERR(": could not load track\n");
-	  return hr;
-	}
-	IStream_Release (pClonedStream);
-	
-	IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
-
-	/** now safe move the cursor */
-	liMove.QuadPart = ListSize[0];
-	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
-	 
-      } else {
-	TRACE_(dmfile)(": unknown RIFF fmt (skipping)\n");
-	liMove.QuadPart = ListSize[0];
-	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
-      }
-      break;
+        if (chunk.id == DMUS_FOURCC_TRACK_EXTRAS_CHUNK)
+            FIXME("DMUS_IO_TRACK_EXTRAS_HEADER chunk not handled\n");
     }
+    if (hr != S_OK)
+        return hr == S_FALSE ? DMUS_E_TRACK_NOT_FOUND : hr;
 
-    default: {
-      if (0 == track_hdr.fccType && Chunk.fccID == track_hdr.ckid) {
-	LPSTREAM pClonedStream = NULL;
-
-	TRACE_(dmfile)(": TRACK solo\n");
-
-	IStream_Clone (pStm, &pClonedStream);
-	
-	liMove.QuadPart = 0;
-	liMove.QuadPart -= (sizeof(FOURCC) + sizeof(DWORD));
-	IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
+    /* Some DirectMusicTrack implementation expect the stream to start with their data chunk */
+    if (FAILED(hr = IStream_Clone(stream, &clone)))
+        return hr;
+    stream_reset_chunk_start(clone, &chunk);
 
-        hr = load_track(This, pClonedStream, &pTrack, &track_hdr);
-	if (FAILED(hr)) {
-	  ERR(": could not load track\n");
-	  return hr;
-	}
-	IStream_Release (pClonedStream);
-	
-	IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
+    /* Load the track */
+    hr = CoCreateInstance(&thdr.guidClassID, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicTrack,
+            (void **)&track);
+    if (FAILED(hr))
+        goto done;
+    hr = IDirectMusicTrack_QueryInterface(track, &IID_IPersistStream, (void **)&ps);
+    if (FAILED(hr))
+        goto done;
+    hr = IPersistStream_Load(ps, clone);
+    if (FAILED(hr))
+        goto done;
 
-	liMove.QuadPart = Chunk.dwSize;
-	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
+    hr = IDirectMusicSegment8_InsertTrack(&This->IDirectMusicSegment8_iface, track, thdr.dwGroup);
 
-	break;
-      }
+done:
+    if (ps)
+        IPersistStream_Release(ps);
+    if (track)
+        IDirectMusicTrack_Release(track);
+    IStream_Release(clone);
 
-      TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
-      liMove.QuadPart = Chunk.dwSize;
-      IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
-      break;						
-    }
-    }
-    TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
-  } while (StreamCount < StreamSize);  
-
-  return S_OK;
+    return hr;
 }
 
 static HRESULT parse_track_list(IDirectMusicSegment8Impl *This, IStream *stream,
@@ -823,7 +704,7 @@ static HRESULT parse_track_list(IDirectMusicSegment8Impl *This, IStream *stream,
 
     while ((hr = stream_next_chunk(stream, &chunk)) == S_OK)
         if (chunk.id == FOURCC_RIFF && chunk.type == DMUS_FOURCC_TRACK_FORM)
-            hr = parse_track_form(This, chunk.size, stream);
+            hr = parse_track_form(This, stream, &chunk);
 
     return SUCCEEDED(hr) ? S_OK : hr;
 }
-- 
2.14.4




More information about the wine-devel mailing list