Skip to content

Windows Media movies: ASF/WMV files, the WMV decoder and resampler transforms, decode-ahead (Sabbat of the Witch, Senren Banka) - #631

Open
wenze-allen wants to merge 9 commits into
willfaust:mainfrom
wenze-allen:video-cg-sabbat-senren
Open

wenze-allen wants to merge 9 commits into
willfaust:mainfrom
wenze-allen:video-cg-sabbat-senren

Conversation

@wenze-allen

Copy link
Copy Markdown

In short. Nine commits on main (fbad430). They make two 32-bit visual novels' movies play through winegstreamer's unix side on iOS:

  • Sabbat of the Witch's video CGs (Windows Media, read through the WM reader) were tested on a device.
  • Senren Banka's movie (Media Foundation into the EVR) has not been tested on a device.

This depends on willfaust/wine#78. The wine pin moves to its head, 34765056b9e, so merge that one first. The MPEG-1/2 work I have for other games is not in this PR.

Tested on a device: Sabbat of the Witch

iPad Air M3, confirmed by me on IPA bef1e5d, built from my fork. The video CGs play. The path is KiriKiri 2's krmovie.dll → WM sync reader with compressed samples → WMV/WMA decoder DMOs through the DMO wrapper → VMR9.

That build had the first six commits here, through the resampler. The unix sources at 3dab915 are the tested ones, except for comments and upstream's 64-bit parser gate (wgp_create64), which a 32-bit game does not reach. Its wine has the same tree as b0d33cbd85a in willfaust/wine#78. The rest of that build was my fork, including its DXMT pin (1f617cd, not 3d62913). This branch itself has not been built into an IPA.

Not tested on a device: Senren Banka

The path is krkrz's Media Foundation session over this PR's ASF source → resampler to the SAR → decode-ahead → EVR mixer/presenter on DXMT, with the DXVA2 render-target and mfplat discard-copy fixes in willfaust/wine#78. It is verified only by the host checks below and by the device logs that diagnosed each fault:

  • a white window with the renderer's topology nodes dropped;
  • the audio branch lost to a refused resampler;
  • audio running short behind each decoded picture;
  • a black mixer output;
  • the copier's GPU readback, [d9-drain] ... avg-us=9300.

The RGB32 default and the resampler were in the tested build. Decode-ahead and the dxva2/mfplat pin have not run in any build on a device.

Details

  1. wg_parser: hold a reader back instead of dropping another stream's packets. When one stream's reader fell behind, the other kept demuxing for it until the queue dropped packets. Past a soft limit, the demuxing reader now waits, without the parser lock, until that queue drains. A stream not read for 5 s is queued without waiting.
  2. wg_parser: play Windows Media (ASF/WMV) files. FFmpeg now builds the asf demuxer and the LGPL wmv1,wmv2,wmv3,vc1,msmpeg4v1-v3 decoders, and the parser decodes Windows Media video in software. Shipping the WMV9/VC-1 decoders is your call: they are LGPL like the rest of the configuration, but VC-1 has a patent pool. That is the same question as for H.264/HEVC/AAC, which stay with Apple's decoders.
  3. RGB32 is the default native video format. The EVR's sample allocator wants NV12 surfaces, which DXMT's d3d9 cannot create, and the session then silently lost the renderer's node. media_source.c still offers the YUV types after RGB32. The cost is a CPU conversion from NV12. MADEIRA_WG_VIDEO_FORMAT=nv12 restores the old default. The wine pin gets the mf/session commit.
  4. A WMV decoder transform for the WMVideo Decoder DMO/MFT (WMV1/2/3, WMVA, WVC1), following wg_transform.c's protocol. Transforms are now kinds in one table (transform_kinds[]). MADEIRA_WG_WMV_TRANSFORM=0 refuses video transforms again.
  5. Compressed WMV/WMA samples for the WM reader. A compressed-output parser hands out the demuxer's packets as they are. MADEIRA_WG_COMPRESSED=0 refuses this again. The wine pin gets the wm_reader commit.
  6. The audio resampler transform on libswresample. It takes integer and float PCM, 1 to 8 channels, any rate. MADEIRA_WG_RESAMPLER=0 refuses it again.
  7. tests: build the shared host FFmpeg under a lock. Two checks configuring into one build directory broke a cold parallel run.
  8. Decode video ahead on a thread of its own. Wine's media source asks for every stream on one work-queue thread. A 1080p WMV picture (about 20 ms) held up the audio behind it. Each decoded video stream now keeps up to three pictures ready. MADEIRA_WG_VIDEO_AHEAD=0 decodes on demand again.
  9. The wine pin moves to winegstreamer, mf, dxva2 and mfplat: the WM reader's compressed samples, a session that keeps its renderer, DXVA2 render targets, a discard copy wine#78's head (dxva2 render targets, mfplat discard copy), with tests/host/check-mf-video-render.py and the docs.

64-bit processes: upstream's policy stays as it is. They get the transform table by default and their parser only with MADEIRA_WG_64BIT=1. Because the table now holds every transform kind, a 64-bit process gets the WMV decoder and the resampler as well as WMA. MADEIRA_WG_64BIT=0 still gives it the stub table. Neither has been tried in a 64-bit game.

docs/MEDIA.md, THIRD-PARTY-NOTICES.md (FFmpeg components) and the settings catalog (four new Media switches) are updated. Fork-only parts (CI workflows, submodule URLs, build overlays) are left out.

Overlaps with open PRs: #588 conflicts in winegstreamer_unixlib_ios.c. Both change the default native video format (its MADEIRA_MF_CPU_VIDEO=1 picks ARGB32) and the wgp_create64 comment. #463, #608, #614 and #616 touch the same files but already conflict with main. #594 merges cleanly.

Validation

python3 tests/host/run-all.py -j 6 on Linux, compared with the same run on main (fbad430):

checks PASS NOTOOL FAIL FLAKY
main 289 271 4 14 0
this branch 293 274 4 14 1
  • The four new checks pass: check-wg-parser-asf, check-wmv-transform, check-wg-resampler and check-mf-video-render.
  • The 14 FAILs are the same on both. All of them need macOS tools: Swift, Objective-C frameworks, plutil, Xcode.
  • check-fastsync is the one FLAKY: it failed beside the others and passed alone. Its exit 85 means the wrong-order control of the semaphore race model did not stall under load. This PR does not touch that model or the header it builds against. It passed alone 3 of 3 times on this branch, and twice under load from the media checks.

The media checks:

  • check-wg-parser: with back-pressure, a slow video reader gets 90 of 90 pictures; without it, 16 of 90. With decode-ahead, a video request takes 0.0 ms on average instead of 21.0 ms. ThreadSanitizer is clean.
  • check-wg-parser-asf runs under ASan/UBSan and TSan, bit-exact against an independent libavcodec decode and demux.
  • check-wmv-transform and check-wg-resampler run under ASan/UBSan. check-wma-decoder passes.
  • check-mf-video-render passes against DXMT 3d62913.
  • The five media checks, run at once on an empty FFmpeg cache, pass with one build per prefix.
  • gen-config-catalog.py --check is current at every commit.

Not run: the Swift/macOS checks, an IPA build of this branch, and a device run of it.

Written with Claude Code. Signed off (DCO).

🤖 Generated with Claude Code

wenze-allen and others added 9 commits November 22, 2026 05:34
…ckets

quartz and Media Foundation read each stream on its own thread; a packet
demuxed for another stream is queued for it. When one stream's reader
fell behind (a video decoded slower than real time) the other reader
kept demuxing for it, and past 16,384 packets or 64 MB the queue dropped
packets, which breaks every picture up to the next keyframe.

Once a queue passes a soft limit (1,024 packets or 16 MB) while another
thread reads that stream, the reader that demuxes for it now waits in
mav_wait_drain, without the parser lock, until the queue has drained to
three quarters of the limit. A stream not read for 5 s is marked unread
and queued without waiting (dropped past the hard limits, as before)
until it is read again. One thread reading both streams (the
earliest-buffer mode) never waits for itself, and a seek, a disable or
disconnect wakes a waiting reader; disconnect frees nothing until the
waiters have left.

tests/host/check-wg-parser.py builds the core again with small limits
(soft 8 packets, hard 16, 400 ms) and runs those cases on its synthetic
MP4; the same slow-video case built without back-pressure drops packets
and loses pictures (18 of 90 here).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
A 32-bit visual novel's cutscenes go through the Windows Media Format
reader (wm_reader.c), a wg_parser user. The parser knew MP3, WAV and MP4
only, so the reader failed to connect (0x80040265): a white screen with
no sound.

FFmpeg now builds the asf demuxer and the LGPL wmv1/wmv2/wmv3/vc1/
msmpeg4v1-v3 decoders; no platform decoder on iOS takes these, so the
parser decodes Windows Media video in software (mav_lavc_video, one
video backend per stream), WMA like MPEG audio, and writes every format
the WM reader offers, its bottom-up BGR default first (also UYVY, YVYU,
RGB565 and RGB555). The glue describes ASF streams by their WMV / WMA
codec formats.

This enables WMV9/VC-1 decoders in the app's FFmpeg. They are LGPL, as
the rest of the configuration, but VC-1 has a patent pool: whether to
ship them is the maintainers' call, as for H.264/HEVC/AAC, which stay
with Apple's decoders.

tests/host/check-wg-parser-asf.py synthesises WMV7, WMV8 and MS-MPEG4 v3
files with WMA2 audio and checks every picture bit-exact against an
independent libavcodec decode, the WM reader's way of reading, every
format after a keyframe seek, a WMV3 stream of garbage, and the
back-pressure cases on the software decoder. WMV9/VC-1 streams
themselves are not covered: FFmpeg cannot encode them. On a device the
demuxer has run only under the compressed-sample path later in this
series (Sabbat of the Witch, which plays); the parser's own decoding of
Windows Media video is tested on Linux only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
…faces DXMT has

Senren Banka's CG movie (32-bit; MP4, H.264 1080p + AAC, played by krkrz's
MFPlayer through a media session with the EVR and the SAR) left a white video
window and the story never went on. The parser connected both streams, and
nothing ever asked it for a sample.

The media session connects a source to its renderer with the source's current
type only, and that is the native format the unix side reports: NV12. The EVR
accepts it (dxva2's processor service lists NV12 without asking the device),
and the session then asks the EVR's sample allocator for two NV12 Direct3D 9
surfaces for the sample copier it puts in front of the EVR. DXMT's d3d9 maps no
YUV format to Metal, so CreateOffscreenPlainSurface fails and the allocator
does too. session_append_node then fails the EVR's output node, and
session_collect_nodes stops there: the EVR node and every node after it are
missing (the audio branch included). The EVR still gets the clock, and with no
rate set the session skips preroll. It reports MESessionStarted with no output
node to request samples for. The audio renderer never gets the clock, and the
game waits for a movie that never ends. The device log fits every step: the
parser connected; topology_loader_Load and dxva2's FIXMEs appear; the EVR's
sample tracking thread and the presenter's streaming thread (only
MFVP_MESSAGE_BEGINSTREAMING, i.e. the clock start, creates it) were created;
the SAR's audio client was created but never started (no timer thread); the
media threads used no CPU; the read thread sat in wgp_get_next_read_offset;
and the parser logged no error.

RGB32 is now the default native format. DXMT has RGB32 surfaces, so the
allocator and the copier work on ordinary surfaces. (The mixer's stretch into
its window-sized target still needs a render target; the DXVA2 change later in
this series makes one.) media_source.c still lists NV12,
YV12, YUY2, I420 and IYUV after the native type, so a source reader that asks
for NV12 still gets it (and one that asks for RGB32 no longer needs a
converter this port does not have). The cost is converting each picture from
NV12 to BGRx on the CPU. MADEIRA_WG_VIDEO_FORMAT=nv12 restores the old
default.

Each stream now logs one line when its first buffer is handed out, so a
device log tells a stream nobody asked for from one that fails to decode.

tests/host/check-wg-parser.py: asserts the default and the new lines; passes.

Wine pin: willfaust/wine `mf/session: Keep an output node whose sample allocator
cannot be initialized`, so a renderer whose allocator does fail keeps its output
node (and its branch) instead of silently losing everything after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
Wine's WMVideo Decoder DMO and MFT (CLSID_CWMVDecMediaObject, wmv_decoder_create() in
dlls/winegstreamer/video_decoder.c) is a wg_transform, and this port refused every video
transform. KiriKiri's movie plugin decodes the WM reader's compressed WMV samples with that DMO
(Sabbat of the Witch's video CGs).

The transform decodes WMV1, WMV2, WMV3, WMVA and WVC1 with libavcodec (the parser's
mav_lavc_video) and writes each picture with the parser's converter (mav_convert_picture now
takes the output description instead of a stream): NV12, YV12, I420/IYUV, YUY2, UYVY, YVYU,
RGB32, ARGB32, ABGR32, RGB24, RGB565 and RGB555, top-down or bottom-up, in upstream's layout or
an MF 2D buffer's stride, with apertures honoured. It follows wg_transform.c: push queues up to
input_queue_length + 1 frames, read decodes until one picture comes out, each picture keeps the
timestamps of the sample it was coded in, and drain, flush, get_status and the output type work
as there. The DMO's construction probe (WMV3 without a sequence header) creates a transform
that drops its input, as GStreamer would.

Transforms are now kinds in one table (transform_kinds[]): create goes to the first kind that
claims the input type, and every other entry finds the kind by the magic its object starts
with. The WMA decoder's functions are renamed for it (wma_create, wma_push_data, ...), and the
unix entries are wg_transform_*. MADEIRA_WG_WMV_TRANSFORM=0 refuses video transforms again.

tests/host/check-wmv-transform.py: WMV1 and WMV2 bit-exact against libavcodec, all 13 output
formats both ways up at both layouts, apertures, timestamps, the queue limit, drain, flush,
output type changes, a buffer too small, the DMO wrapper's loop, two threads, refusals, the
WMV3/WVC1/WMVA plumbing, the wow64 thunks, WMA through the same dispatch, and the switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
KiriKiri's movie plugin (krmovie.dll) reads a WMV through the WM sync reader as compressed
samples of both streams and decodes them with the WMV and WMA decoder DMOs (Sabbat of the
Witch's video CGs). The reader then opens a compressed-output parser, which this port refused
for every ASF; Wine's reader freed its streams on that failure and the next call crashed.

- wg_parser core: a compressed-output parser exposes an ASF's streams as the decoding one does
  (the reader asserts the counts match). Its WMA, WMV1/2/3, WMVA and WVC1 streams start in
  compressed mode and hand out the demuxer's packets as they are: the file's bytes, keyframe
  flags as `delta`, timestamps. After a seek the video starts at a keyframe (at most 300 delta
  frames are dropped waiting for one). A stream enabled with a raw format in the same parser is
  decoded as before; MS-MPEG4 has no wg_format and always is.
- glue: get_current_format reports the codec format for such a stream, stream_enable takes
  WG_MAJOR_TYPE_VIDEO_WMV / WG_MAJOR_TYPE_AUDIO_WMA. MADEIRA_WG_COMPRESSED=0 refuses compressed
  output for an ASF again.
- wine pin: willfaust/wine `winegstreamer: Read compressed samples per stream; keep the reader's
  streams when a reinit fails`: the reader chooses compressed or decoded per stream, keeps its streams when
  a reinit fails and goes back to the previous parser, and fails instead of asserting when the
  stream counts differ.
- tests/host/check-wg-parser-asf.py: every packet of WMV7, WMV8, a WMV3 stream and WMA2 against
  an independent demux of the same file; compressed video beside decoded audio; a seek; back to
  decoded pictures, bit-exact; MS-MPEG4 refused compressed; the switch; source checks on the
  reader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
Media Foundation's topology loader puts winegstreamer's resampler (CLSID_CResamplerMediaObject,
wg_resampler) between a source's PCM and the audio renderer, which takes only the device mix
format. The wg_transform behind it was refused here ("input format tag 0x1 is not a WMA family
stream"), resampler_create failed, and Senren Banka's WMV movie (48 kHz S16 from its WMA
stream) lost the audio branch of its topology: no presentation clock, and the video renderer
never presented.

Raw audio in is now a third transform kind, the first row of transform_kinds (the WMA decoder
claims all other audio), converted with libswresample: integer PCM of 8, 16, 24 or 32 bits and
float of 32 or 64, WAVEFORMATEX or WAVEFORMATEXTENSIBLE, any rate, 1 to 8 channels, in and out.
Timestamps run from the input's, less what the resampler still holds, and are computed from a
frame count so rounding never adds up. Push, read, back-pressure, drain, flush and the output
type follow the WMA decoder's protocol. MADEIRA_WG_RESAMPLER=0 refuses it again.

tests/host/check-wg-resampler.py drives it under ASan/UBSan through the unix entries: Senren's
case to 48 and 44.1 kHz float (frequency, level, frame count, timestamps), all 36 sample-format
pairs (identity bit-exact), channel up/downmix, check_audio_transform_support()'s probe,
back-pressure, flush, a new output type, drain, the refusals and the switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
check-wma-decoder.py and check-wg-resampler.py share one host FFmpeg build (the same configure
flags), and so do check-wg-parser-asf.py and check-wmv-transform.py. Run at the same time on a
cold cache (a parallel runner, or CI), both configured into one build directory:
ffbuild/config.mak broke ("extraneous 'endif'"), and both checks failed.

tests/host/host_ffmpeg.py builds it once for all five media checks: under a lock on the prefix,
from a clean build directory, and a check waits for a build in progress instead of linking
against a half-installed prefix. The cache paths do not change. All five, run at once on an
empty cache, now pass with one build per prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
Wine's media source asks for every stream's samples on one work-queue thread (a standard work
queue has one thread), and so does the WM reader reading the earliest buffer. The parser decoded
on the requesting thread, under its lock: each picture of Senren Banka's 1080p WMV (software
decode and RGB conversion, about 20 ms) held up the audio request behind it, and the audio
renderer ran short for 6 to 17% of each 10 s ("client wrote 8.33 s of audio in 10.01 s").

GStreamer decodes in streaming threads of its own. Here each decoded video stream gets a worker
at its first request, which keeps up to three pictures ready in a ring of slots; a request pops
one. The worker demuxes under the parser lock like any reader, and decodes and converts without
it (mav_heavy_begin), so a request for another stream is answered meanwhile. A seek, a new
format or compressed output waits for the picture the worker is on, and it starts no new one
until they are done (mav_ahead_quiesce). Disconnect joins the workers and waits for requests
waiting on a picture before anything is freed. The worker does not wait on back-pressure for a
stream that the thread it reads for would drain. MADEIRA_WG_VIDEO_AHEAD=0 decodes on demand
again.

tests/host/check-wg-parser.py: with a stub decoder that takes 20 ms a picture and one thread
asking for both streams at 30 fps, a video request takes 0.0 ms instead of 21, every picture
comes in order, the audio has no gaps, a seek lands exactly, and a disconnect mid-decode is
clean; the threaded cases also run under ThreadSanitizer (the harness's read-thread flag is
atomic now). tests/host/check-wg-parser-asf.py: every case runs with the worker, and again under
ThreadSanitizer; a new back-pressure case (one thread that read the audio, then only the video)
stalls 447 ms without the last rule above.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
…enren Banka's black movie)

Senren Banka's WMV movie (krkrz's MFPlayer, Media Foundation) played its audio over a black
video window, its audio running short every few seconds. Two of Wine's choices on the way to
the EVR worked only on wined3d, which is lenient where native d3d9 and DXMT are not:

- dxva2 (`1f0f5075e94`): CreateSurface() made every surface an offscreen plain surface. The EVR
  mixer's targets are the size of the video window (1920x1080 into 2360x1328), and DXMT, as
  native, does not StretchRect into a surface that is not a render target, so the mixer left only
  its black background. DXVA2_VideoProcessorRenderTarget surfaces are now lockable render
  targets, and a scaled blit is filtered.
- mfplat (`34765056b9e`): the sample copier's ContiguousCopyFrom() locked the EVR's surface without
  flags, so DXMT read it back and waited for the GPU first, 9 to 12 ms per frame
  ("[d9-drain] ... avg-us=9300"), on the session thread that also feeds the audio. It now locks
  with D3DLOCK_DISCARD; a plain write-only lock keeps the contents, as native does.

The wine pin moves to the head of willfaust/wine#78, `34765056b9e`, which has these two commits.
tests/host/check-mf-video-render.py checks the path from source: DXMT's StretchRect rule and discard lock, the two changes, the allocators
that ask for these surfaces, and the one-thread work queue that makes decode-ahead necessary.
docs/MEDIA.md says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: allen <wenze532865208@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant