From 18689a08977f6b0756788f63c9c6f6a7f7c76e81 Mon Sep 17 00:00:00 2001 From: allen Date: Fri, 6 Nov 2026 04:18:40 +0900 Subject: [PATCH 1/4] mf/session: Keep an output node whose sample allocator cannot be initialized. session_append_node() kept the HRESULT of IMFVideoSampleAllocator_InitializeSampleAllocator(), so an allocator that failed (the EVR's, when the device cannot create surfaces of the stream's format) failed the whole output node. session_collect_nodes() then stopped at that node, and the session ran with the renderer's node and every node after it missing. It still sent MESessionStarted, but no sample was ever requested, the remaining sinks never got the clock, and playback never ended. Release the allocator instead and keep the node. The upstream node then gets system memory samples, as it does for a renderer without an allocator. Co-Authored-By: Claude Opus 5.5 Signed-off-by: allen --- dlls/mf/session.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/dlls/mf/session.c b/dlls/mf/session.c index 28195f914b06..12f618b067ac 100644 --- a/dlls/mf/session.c +++ b/dlls/mf/session.c @@ -1905,12 +1905,21 @@ static HRESULT session_append_node(struct media_session *session, IMFTopologyNod if (FAILED(hr = IMFVideoSampleAllocator_InitializeSampleAllocator(topo_node->u.sink.allocator, 2, media_type))) { - WARN("Failed to initialize sample allocator for the stream, hr %#lx.\n", hr); + /* Keep the node and give the upstream node system memory samples instead. + * Failing here dropped this node and every node after it, and the session + * then started without ever requesting a sample. */ + ERR("Failed to initialize sample allocator for the stream, hr %#lx.\n", hr); + IMFVideoSampleAllocator_Release(topo_node->u.sink.allocator); + topo_node->u.sink.allocator = NULL; + hr = S_OK; + } + else + { + IMFVideoSampleAllocator_QueryInterface(topo_node->u.sink.allocator, + &IID_IMFVideoSampleAllocatorCallback, (void **)&topo_node->u.sink.allocator_cb); + IMFVideoSampleAllocatorCallback_SetCallback(topo_node->u.sink.allocator_cb, + &topo_node->u.sink.notify_cb); } - IMFVideoSampleAllocator_QueryInterface(topo_node->u.sink.allocator, - &IID_IMFVideoSampleAllocatorCallback, (void **)&topo_node->u.sink.allocator_cb); - IMFVideoSampleAllocatorCallback_SetCallback(topo_node->u.sink.allocator_cb, - &topo_node->u.sink.notify_cb); } IMFMediaType_Release(media_type); } From b0d33cbd85acf4339150507cac9bab137c6021d0 Mon Sep 17 00:00:00 2001 From: allen Date: Fri, 20 Nov 2026 23:20:34 +0800 Subject: [PATCH 2/4] winegstreamer: Read compressed samples per stream; keep the reader's streams when a reinit fails. SetReadStreamSamples is per stream, but reinit_stream() created the parser for compressed output from the last call's flag alone and enabled every stream with its compressed format then. It now creates a compressed-output parser when any stream reads compressed samples, and enables each stream with its own format: the compressed one, or the decoded one it has. When reinit_stream() failed (the parser refused to connect), it freed the reader's stream array but kept stream_count, and SetReadStreamSamples() returned S_OK anyway. The next call into the reader wrote through the NULL array: a KiriKiri movie plugin (krmovie.dll) that reads WMV as compressed samples crashed there, and the game stayed on a white screen. The streams belong to the open file, so a failed reinit now keeps them and drops only the handles into the destroyed parser. SetReadStreamSamples() goes back to the previous mode, rebuilds that parser and returns the failure. reinit_stream() tolerates being called with no parser, read thread or semaphore left, and returns an error rather than S_OK when it cannot wait for its read thread. free_streams() clears stream_count with the array. A parser that exposes a different number of streams with compressed output failed an assert, which aborts the process; it now fails the reinit, and the caller goes back to the previous parser. Co-Authored-By: Claude Opus 5.5 Signed-off-by: allen --- dlls/winegstreamer/wm_reader.c | 82 +++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/dlls/winegstreamer/wm_reader.c b/dlls/winegstreamer/wm_reader.c index 4f41f1a1ceb2..3e1d0e144261 100644 --- a/dlls/winegstreamer/wm_reader.c +++ b/dlls/winegstreamer/wm_reader.c @@ -1489,6 +1489,7 @@ static void free_streams(struct wm_reader *reader) free(reader->streams); reader->streams = NULL; + reader->stream_count = 0; } static HRESULT init_stream(struct wm_reader *reader) @@ -1600,28 +1601,51 @@ static HRESULT init_stream(struct wm_reader *reader) return hr; } -static HRESULT reinit_stream(struct wm_reader *reader, bool read_compressed) +/* A stream reads compressed samples or decoded ones on its own (SetReadStreamSamples is per stream): + * the parser is created for compressed output when any stream asks for it, and each stream is + * enabled with its own format, the compressed one or the decoded one. A parser that cannot do both + * fails to connect, and the reader keeps its streams (see the end). */ +static bool wm_reader_reads_compressed(struct wm_reader *reader) { + WORD i; + + for (i = 0; i < reader->stream_count; ++i) + if (reader->streams[i].read_compressed) + return true; + return false; +} + +static HRESULT reinit_stream(struct wm_reader *reader) +{ + bool read_compressed = wm_reader_reads_compressed(reader); wg_parser_t wg_parser; HRESULT hr; WORD i; - ReleaseSemaphore(reader->read_sem, 1, NULL); + /* After a failed reinit (below) there is no parser, read thread or semaphore to tear down. */ + if (reader->read_sem) + ReleaseSemaphore(reader->read_sem, 1, NULL); free_stream_buffers(reader); - wg_parser_disconnect(reader->wg_parser); + if (reader->wg_parser) + wg_parser_disconnect(reader->wg_parser); - EnterCriticalSection(&reader->shutdown_cs); - reader->read_thread_shutdown = true; - LeaveCriticalSection(&reader->shutdown_cs); - WaitForSingleObject(reader->read_thread, INFINITE); - CloseHandle(reader->read_thread); - CloseHandle(reader->read_sem); + if (reader->read_thread) + { + EnterCriticalSection(&reader->shutdown_cs); + reader->read_thread_shutdown = true; + LeaveCriticalSection(&reader->shutdown_cs); + WaitForSingleObject(reader->read_thread, INFINITE); + CloseHandle(reader->read_thread); + } + if (reader->read_sem) + CloseHandle(reader->read_sem); reader->read_thread = NULL; reader->read_sem = NULL; - wg_parser_destroy(reader->wg_parser); + if (reader->wg_parser) + wg_parser_destroy(reader->wg_parser); reader->wg_parser = 0; if (!(wg_parser = wg_parser_create(read_compressed))) @@ -1647,7 +1671,16 @@ static HRESULT reinit_stream(struct wm_reader *reader, bool read_compressed) goto out_shutdown_thread; } - assert(reader->stream_count == wg_parser_get_stream_count(reader->wg_parser)); + /* A parser that exposes the file's streams differently with compressed output fails here, + * and the caller goes back to the previous parser. */ + if (wg_parser_get_stream_count(reader->wg_parser) != reader->stream_count) + { + ERR("Parser has %u streams, expected %u.\n", + wg_parser_get_stream_count(reader->wg_parser), reader->stream_count); + wg_parser_disconnect(reader->wg_parser); + hr = E_FAIL; + goto out_shutdown_thread; + } for (i = 0; i < reader->stream_count; ++i) { @@ -1658,7 +1691,7 @@ static HRESULT reinit_stream(struct wm_reader *reader, bool read_compressed) stream->reader = reader; wg_parser_stream_get_current_format(stream->wg_stream, &format); if (stream->selection == WMT_ON) - wg_parser_stream_enable(stream->wg_stream, read_compressed ? &format : &stream->format); + wg_parser_stream_enable(stream->wg_stream, stream->read_compressed ? &format : &stream->format); } /* We probably discarded events because streams weren't enabled yet. @@ -1668,6 +1701,7 @@ static HRESULT reinit_stream(struct wm_reader *reader, bool read_compressed) if (WaitForSingleObject(reader->read_sem, INFINITE) != WAIT_OBJECT_0) { ERR("Failed to wait for read semaphore.\n"); + hr = E_FAIL; goto out_shutdown_thread; } @@ -1688,7 +1722,12 @@ static HRESULT reinit_stream(struct wm_reader *reader, bool read_compressed) reader->read_sem = NULL; } free_stream_buffers(reader); - free_streams(reader); + /* The streams belong to the open file, not to this parser: the application has their + * selection, output format and allocators. Freeing them here left stream_count streams in a + * NULL array, and the next call into the reader wrote through it (a KiriKiri movie plugin + * that asks for compressed samples). Only the handles into the destroyed parser go. */ + for (i = 0; i < reader->stream_count; ++i) + reader->streams[i].wg_stream = 0; wg_parser_destroy(reader->wg_parser); reader->wg_parser = 0; @@ -2573,6 +2612,8 @@ static HRESULT WINAPI reader_SetReadStreamSamples(IWMSyncReader2 *iface, WORD st { struct wm_reader *reader = impl_from_IWMSyncReader2(iface); struct wm_stream *stream; + bool old_compressed; + HRESULT hr; TRACE("reader %p, stream_index %u, compressed %d.\n", reader, stream_number, compressed); @@ -2584,11 +2625,22 @@ static HRESULT WINAPI reader_SetReadStreamSamples(IWMSyncReader2 *iface, WORD st return E_INVALIDARG; } + old_compressed = stream->read_compressed; stream->read_compressed = compressed; - reinit_stream(reader, compressed); + if (FAILED(hr = reinit_stream(reader))) + { + /* The parser could not serve the new mix of compressed and decoded streams. Go back to + * the one that worked, so the reader stays usable, and say so instead of returning S_OK + * over a reader without a parser. */ + ERR("Stream %u: compressed %d is not available, hr %#lx; keeping the previous mode.\n", + stream_number, compressed, hr); + stream->read_compressed = old_compressed; + if (FAILED(reinit_stream(reader))) + ERR("Failed to restore the previous parser.\n"); + } LeaveCriticalSection(&reader->cs); - return S_OK; + return hr; } static HRESULT WINAPI reader_SetStreamsSelected(IWMSyncReader2 *iface, From 1f0f5075e9468ba0de9919b16dc249b4e84aa85a Mon Sep 17 00:00:00 2001 From: allen Date: Sat, 21 Nov 2026 12:02:47 +0800 Subject: [PATCH 3/4] dxva2: Create video processor render targets as render targets, and filter a scaled blit. IDirectXVideoProcessorService::CreateSurface() made every surface an offscreen plain surface, whatever its DXVA2 type, and VideoProcessBlt() stretches the samples into the target with StretchRect(). Native d3d9 does not stretch into a destination that is not a render target, and a strict implementation refuses it. The EVR mixer's targets are the size of the video window, not of the video: with DXMT's d3d9, VideoProcessBlt() left only its background fill, and a Media Foundation movie played as a black window (Senren Banka, a 1920x1080 WMV in a 2360x1328 window). wined3d is lenient, so it did not show there. DXVA2_VideoProcessorRenderTarget surfaces are now lockable render targets: the sample copier still writes frames into them, and the presenter's GetCurrentImage() reads them back with GetRenderTargetData(), which wants a render target. Native reports no usage for them, so the test's usage check is todo_wine. A blit that scales now uses D3DTEXF_LINEAR, as a video processor filters; a 1:1 copy stays D3DTEXF_POINT. Co-Authored-By: Claude Opus 5.5 Signed-off-by: allen --- dlls/dxva2/main.c | 19 ++++++++++++++++--- dlls/dxva2/tests/dxva2.c | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/dlls/dxva2/main.c b/dlls/dxva2/main.c index c74c0116d227..37969ad9b34d 100644 --- a/dlls/dxva2/main.c +++ b/dlls/dxva2/main.c @@ -302,6 +302,7 @@ static HRESULT WINAPI video_processor_VideoProcessBlt(IDirectXVideoProcessor *if IDirect3DDevice9 *device; unsigned int i; RECT dst_rect; + BOOL scaled; HRESULT hr; TRACE("%p, %p, %p, %p, %u, %p.\n", iface, rt, params, samples, sample_count, complete_handle); @@ -327,8 +328,11 @@ static HRESULT WINAPI video_processor_VideoProcessBlt(IDirectXVideoProcessor *if if (!intersect_rect(&dst_rect, &dst_rect, &samples[i].DstRect)) continue; + /* A scaled picture is filtered, as a video processor does; a 1:1 copy stays exact. */ + scaled = samples[i].SrcRect.right - samples[i].SrcRect.left != dst_rect.right - dst_rect.left + || samples[i].SrcRect.bottom - samples[i].SrcRect.top != dst_rect.bottom - dst_rect.top; if (FAILED(hr = IDirect3DDevice9_StretchRect(device, samples[i].SrcSurface, &samples[i].SrcRect, - rt, &dst_rect, D3DTEXF_POINT))) + rt, &dst_rect, scaled ? D3DTEXF_LINEAR : D3DTEXF_POINT))) { WARN("Failed to copy sample %u, hr %#lx.\n", i, hr); } @@ -408,8 +412,17 @@ static HRESULT WINAPI device_manager_processor_service_CreateSurface(IDirectXVid for (i = 0; i < backbuffers + 1; ++i) { - if (FAILED(hr = IDirect3DDevice9_CreateOffscreenPlainSurface(manager->device, width, height, format, - pool, &surfaces[i], NULL))) + /* VideoProcessBlt() stretches the samples into the target with StretchRect(), which native + * d3d9 (and a strict implementation of it) refuses for a destination that is not a render + * target. The EVR mixer's targets are the size of the video window, so its output stayed + * black. The surfaces stay lockable: the sample copier writes frames into them. */ + if (dxvaType == DXVA2_VideoProcessorRenderTarget && pool == D3DPOOL_DEFAULT) + hr = IDirect3DDevice9_CreateRenderTarget(manager->device, width, height, format, + D3DMULTISAMPLE_NONE, 0, TRUE, &surfaces[i], NULL); + else + hr = IDirect3DDevice9_CreateOffscreenPlainSurface(manager->device, width, height, format, + pool, &surfaces[i], NULL); + if (FAILED(hr)) break; } diff --git a/dlls/dxva2/tests/dxva2.c b/dlls/dxva2/tests/dxva2.c index 96368602f4a5..485416527b79 100644 --- a/dlls/dxva2/tests/dxva2.c +++ b/dlls/dxva2/tests/dxva2.c @@ -70,7 +70,8 @@ static void test_surface_desc(IDirect3DSurface9 *surface) ok(desc.Format == D3DFMT_X8R8G8B8, "Unexpected format %d.\n", desc.Format); ok(desc.Type == D3DRTYPE_SURFACE, "Unexpected type %d.\n", desc.Type); - ok(!desc.Usage, "Unexpected usage %ld.\n", desc.Usage); + /* Wine creates a render target, which VideoProcessBlt() can stretch into. */ + todo_wine ok(!desc.Usage, "Unexpected usage %ld.\n", desc.Usage); ok(desc.Pool == D3DPOOL_DEFAULT, "Unexpected pool %d.\n", desc.Pool); ok(desc.MultiSampleType == D3DMULTISAMPLE_NONE, "Unexpected multisample type %d.\n", desc.MultiSampleType); ok(!desc.MultiSampleQuality, "Unexpected multisample quality %ld.\n", desc.MultiSampleQuality); From 34765056b9eec60a8a128c44b9bef8cf5a02a411 Mon Sep 17 00:00:00 2001 From: allen Date: Sat, 21 Nov 2026 12:02:48 +0800 Subject: [PATCH 4/4] mfplat: Do not read back a D3D9 surface that ContiguousCopyFrom() overwrites. ContiguousCopyFrom() on a D3D9 surface buffer locked the surface with no flags, so the surface was read back first, though every byte is then overwritten. The sample copier delivers each frame of a Media Foundation movie to the EVR this way. With DXMT's d3d9 the readback waits for the GPU: 9 to 12 ms per frame, 30 times a second, on the session's work queue thread, which also feeds the audio renderer; the audio ran short every few seconds (Senren Banka). The surface buffer now has its own ContiguousCopyFrom(), which locks with D3DLOCK_DISCARD. A plain write-only Lock2DSize() keeps the contents, as the tests show native does, and a buffer that is already locked takes the generic path. Co-Authored-By: Claude Opus 5.5 Signed-off-by: allen --- dlls/mfplat/buffer.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/dlls/mfplat/buffer.c b/dlls/mfplat/buffer.c index 6d59245b40ca..5435fcd379bd 100644 --- a/dlls/mfplat/buffer.c +++ b/dlls/mfplat/buffer.c @@ -832,6 +832,41 @@ static HRESULT WINAPI d3d9_surface_buffer_Lock2DSize(IMF2DBuffer2 *iface, MF2DBu return hr; } +/* The whole image is overwritten, so the surface is locked with D3DLOCK_DISCARD: a plain lock + * reads the surface back first, which waits for the GPU (once per frame the sample copier + * delivers to the EVR). A buffer that is already locked takes the generic path. */ +static HRESULT WINAPI d3d9_surface_buffer_ContiguousCopyFrom(IMF2DBuffer2 *iface, const BYTE *src_buffer, + DWORD src_length) +{ + struct buffer *buffer = impl_from_IMF2DBuffer2(iface); + D3DLOCKED_RECT rect; + HRESULT hr; + + TRACE("%p, %p, %lu.\n", iface, src_buffer, src_length); + + if (src_length < buffer->_2d.plane_size) + return E_INVALIDARG; + + EnterCriticalSection(&buffer->cs); + + if (buffer->_2d.linear_buffer || buffer->_2d.locks) + { + LeaveCriticalSection(&buffer->cs); + return memory_2d_buffer_ContiguousCopyFrom(iface, src_buffer, src_length); + } + + if (SUCCEEDED(hr = IDirect3DSurface9_LockRect(buffer->d3d9_surface.surface, &rect, NULL, D3DLOCK_DISCARD))) + { + copy_image(buffer, rect.pBits, rect.Pitch, src_buffer, buffer->_2d.width, + buffer->_2d.width, buffer->_2d.height); + IDirect3DSurface9_UnlockRect(buffer->d3d9_surface.surface); + } + + LeaveCriticalSection(&buffer->cs); + + return hr; +} + static const IMF2DBuffer2Vtbl d3d9_surface_buffer_vtbl = { memory_2d_buffer_QueryInterface, @@ -843,7 +878,7 @@ static const IMF2DBuffer2Vtbl d3d9_surface_buffer_vtbl = memory_2d_buffer_IsContiguousFormat, memory_2d_buffer_GetContiguousLength, memory_2d_buffer_ContiguousCopyTo, - memory_2d_buffer_ContiguousCopyFrom, + d3d9_surface_buffer_ContiguousCopyFrom, d3d9_surface_buffer_Lock2DSize, memory_2d_buffer_Copy2DTo, };