Mediana.Documents.Spreadsheets
0.1.52
dotnet add package Mediana.Documents.Spreadsheets --version 0.1.52
NuGet\Install-Package Mediana.Documents.Spreadsheets -Version 0.1.52
<PackageReference Include="Mediana.Documents.Spreadsheets" Version="0.1.52" />
<PackageVersion Include="Mediana.Documents.Spreadsheets" Version="0.1.52" />
<PackageReference Include="Mediana.Documents.Spreadsheets" />
paket add Mediana.Documents.Spreadsheets --version 0.1.52
#r "nuget: Mediana.Documents.Spreadsheets, 0.1.52"
#:package Mediana.Documents.Spreadsheets@0.1.52
#addin nuget:?package=Mediana.Documents.Spreadsheets&version=0.1.52
#tool nuget:?package=Mediana.Documents.Spreadsheets&version=0.1.52
Mediana
Mediana is an MIT-licensed multimedia library under development for .NET 11. Its media implementation is written in C#, with no FFmpeg, external media processes, native media libraries, or third-party runtime packages.
The current implementation is experimental. It reads metadata and compressed packets from ordinary and fragmented MP4/MOV/M4A files containing AVC, HEVC, AV1, MPEG-4 Visual, AAC, MP3 or AC-3/E-AC-3 tracks. The MP4 reader also exposes movie-level iTunes/indexed metadata and original encoded cover art. The H.264 source module connects CAVLC and CABAC I/P/B decoding to public video sessions and returns cropped YUV frames. The HEVC module decodes Main/Main10 I/P/B pictures to native eight/ten-bit YUV, including indexed seek in MP4 and Matroska. The sample can extract a supported video frame, convert SDR color to sRGB, correct the sample aspect ratio and right-angle orientation, and save a PNG preview. An experimental MPEG-TS source module packages AVC/AAC/MP3 access units without decoding. The WAVE and PCM source modules read integer/float audio through public audio sessions. The AAC source module decodes AAC-LC configurations 1–7 and mono/stereo HE-AAC from MP4/M4A and standalone ADTS. SBR and parametric stereo support single-rate and dual-rate synthesis. It encodes contiguous mono/stereo PCM as experimental AAC-LC access units. AC-3/E-AC-3 decoding includes dependent channel composition and indexed raw/MP4 input. Independent source modules also decode HEIC/HEIF still images, JPEG 2000 and JBIG2, and rasterize SVG and TGS/Lottie 2D animation. Their output connects to Mediana frames, SDR color conversion and PNG encoding. These modules compile inside Mediana and do not depend on Heic.NET or Render.NET packages. An experimental PDF module reads page geometry, renders supported vector/text/image content and exposes decoded image XObjects. See PDF support and limits. Word documents can be paginated and rendered directly to PNG from the library or CLI. See Word rendering and public API. PowerPoint slides render directly through Drawing. Independent XLS and XLSX readers expose typed rows and cached formula results. PDF, DOCX, PPTX and spreadsheets share a lazy Unicode text API with structural paths and cancellation. See document reading, rendering and text APIs. Word previews include Fluent color emoji; library callers can enable the optional resource module through the same public API. STL, OBJ and 3MF models can be loaded and rendered to PNG with configurable camera, material, shadows and ambient occlusion. See Mesh APIs and previews. Experimental fixed-QP H.264 I/P encoding and YUV downscaling are available. H.264 encoding offers Baseline CAVLC and Main CABAC entropy modes. Experimental AAC-LC audio encoding to MPEG-TS selects long/short windows for transients; perceptual allocation remains open. HLS variants can copy or encode audio.
Indexed audiovisual operations, codec coverage and measured costs describe the common library pipeline and current verification. The complete HLS codec cycles include independent playback, out-of-order requests, two-minute audio synchronization and bounded cold reads.
Components
| Project | Current responsibility |
|---|---|
Mediana.Models.Mesh |
STL/OBJ/3MF geometry, assembly selection and managed 3D previews (API) |
Mediana.Documents.OpenXml |
Bounded OPC packages, relationships and content-type document/model preview routing |
Mediana.Documents |
Lazy Unicode text blocks, structural paths, part selection and output budgets |
Mediana.Documents.Spreadsheets |
Typed sheets, rows, cells, date systems and cached formula contracts |
Mediana.Documents.Xlsx |
Sequential SpreadsheetML reading through bounded OPC streams |
Mediana.Documents.Xls |
Independent CFB/BIFF8 workbook reader with typed cells and cached formula results |
Mediana.Documents.Presentation |
PresentationML slide reading, direct Drawing rasterization and text extraction (API) |
Mediana.Documents.Word |
DOCX styles, pagination and direct page rasterization (API) |
Mediana.Documents.Pdf |
PDF page geometry, managed parsing/rendering and decoded image extraction |
Mediana.Drawing |
Managed 2D paths, shared scenes/effects, font outlines, clipping, compositing and RGBA surfaces |
Mediana.Drawing.Emoji.Fluent |
Optional compressed Microsoft Fluent Flat emoji and reusable inline graphics (API) |
Mediana.Images.Apk |
Android application icons from compiled APK manifests and drawable resources |
Mediana.Images.Heif |
HEIC item graphs, intra HEVC, grids, transforms and alpha |
Mediana.Codecs.Jpeg2000 |
JP2/J2K integer components with native precision and optional RGBA conversion |
Mediana.Codecs.Jpeg |
Sequential/progressive Huffman JPEG, grayscale/RGB pixels, restart intervals and explicit source interpretation (API) |
Mediana.Codecs.Jbig2 |
Packed monochrome pages, globals and optional RGBA expansion |
Mediana.Images.Svg |
SVG paths, styles, text, masks, filters and embedded PNG |
Mediana.Images.Tgs |
TGS/Lottie 2D keyframes and lazy timed raster frames |
Mediana.Formats |
Shared codec configuration syntax, elementary framing and access-unit conversion |
Mediana.Core |
Rational time, stream metadata, packets, video/PCM frames, buffer ownership and allocation budgets |
Mediana.IO |
Sequential and random access byte sources, stream ownership and probe replay |
Mediana.Containers |
Reader/writer factory contracts and explicit writer finalization |
Mediana.Containers.Matroska |
Indexed MKV/MKA/WebM reading and packet mapping for AVC, HEVC, AV1, VP8/VP9, AAC, MP3, AC-3, Opus, Vorbis and FLAC |
Mediana.Containers.Mp4 |
Ordinary and fragmented MP4/MOV/M4A metadata, original cover art and AVC/HEVC/AV1/MPEG-4 Visual/AAC/MP3/AC-3/E-AC-3 packet reading |
Mediana.Containers.Avi |
Indexed RIFF/AVI and OpenDML DV25/FFV1/MJPEG/MPEG-4 Visual/PCM input (API) |
Mediana.Containers.Wave |
Sequential RIFF/WAVE integer/float PCM, G.711 A-law and μ-law, IMA ADPCM and Microsoft ADPCM reading |
Mediana.Containers.Aiff |
Sequential mono/stereo AIFF and AIFF-C PCM and Apple IMA4 reading |
Mediana.Containers.Flac |
Native FLAC STREAMINFO, Vorbis comments, original pictures and compressed chunks |
Mediana.Containers.Mp3 |
MPEG Layer III frames, ID3 text and original pictures, Xing/Info and coded timing |
Mediana.Containers.Adts |
Streaming/indexed ADTS transport reading, CRC, multiple AAC blocks and exact core sample clocks |
Mediana.Containers.Ac3 |
Indexed raw AC-3/E-AC-3 frame sets, exact sample clocks and opening channel metadata |
Mediana.Containers.Ogg |
Ogg Theora/Opus/Vorbis packets, comments, granule clocks and indexed seek (API) |
Mediana.Codecs.Opus |
Managed SILK/CELT/hybrid PCM, multistream, gain and seek priming (API) |
Mediana.Codecs.Vorbis |
Managed Vorbis I PCM, all floors/residues, multichannel coupling and indexed seek (API) |
Mediana.Codecs.Mp3 |
MPEG-1/2/2.5 Layer III to float32 PCM, all standard rates, mono/stereo and joint stereo |
Mediana.Containers.MpegTs |
MPEG-TS and MPEG-PS input, plus a single-program AVC/AAC/MP3 transport-stream writer |
Mediana.Containers.Asf |
ASF/WMV audio and video media objects with native header metadata |
Mediana.Codecs |
Separate input/output codec protocol and video/audio decoder/encoder factories |
Mediana.Codecs.Pcm |
Integer/float PCM conversion and WAVE G.711 and ADPCM decoding |
Mediana.Codecs.Flac |
Lossless FLAC PCM, predictors/Rice, stereo decorrelation and frame CRC |
Mediana.Codecs.Aac |
AAC-LC through 7.1 wide, mono/stereo SBR, parametric stereo and experimental mono/stereo AAC-LC encoding (HE-AAC) |
Mediana.Codecs.Alac |
Managed Apple Lossless decoding, M4A metadata and indexed seeking; details |
Mediana.Codecs.Dts |
Managed DTS Core decoding, MKV metadata and indexed seeking; details |
Mediana.Codecs.ProRes |
Six ProRes profiles, MOV decoding and full alpha precision; details |
Mediana.Codecs.Dv |
Managed PAL and NTSC DV25 video decode and AVI seeking (API) |
Mediana.Codecs.Ffv1 |
Managed planar 8-bit FFV1 versions 0, 1 and 3 with indexed AVI seeking (API) |
Mediana.Codecs.Ac3 |
Managed AC-3/E-AC-3 transforms, coupling, spectral extension, transient processing and dependent channels (API) |
Mediana.Codecs.Av1 |
Managed eight-bit Main intra/compound inter reconstruction, loop filters and indexed MP4/Matroska input (coverage) |
Mediana.Codecs.Mpeg4Visual |
Managed MPEG-4 Part 2 I/P/B video, half/quarter-pixel motion and indexed AVI/MP4 seeking (API) |
Mediana.Codecs.Vp8 |
Managed VP8 video, planar references and indexed WebM seeking (API) |
Mediana.Codecs.Theora |
Managed Theora video, native planar sampling and indexed Ogg seeking (API) |
Mediana.Codecs.Vp9 |
Managed VP9 profiles 0–3, native planar precision, scaled references and indexed WebM seeking (API) |
Mediana.Codecs.Hevc |
Managed Main/Main10 I/P/B decoding, native ten-bit frames and indexed seeking (API) |
Mediana.Codecs.H264 |
Experimental AVC CAVLC/CABAC I/P/B decoding and fixed-QP Baseline I/P encoding |
Mediana.Codecs.Png |
Static PNG decoding to RGBA32 and sRGB encoding to owned buffers or bounded asynchronous streams (API) |
Mediana.Codecs.Bmp |
BMP palettes, bitfields and RLE; ICO/CUR PNG/DIB entries with alpha (API) |
Mediana.Codecs.Gif |
GIF LZW, interlace, transparency, disposal and timed animation (API) |
Mediana.Codecs.Tiff |
TIFF/BigTIFF strips, tiles, pages and compression; TIFF-based RAW metadata (API) |
Mediana.Codecs.Webp |
VP8/VP8L decoding/encoding, alpha, ICC/EXIF/XMP and WebP animation (API) |
Mediana.Processing |
SDR conversion to sRGB, preview geometry, PCM conversion, channel matrices, sinc resampling and pull-driven waveform RMS/peak |
Mediana.Streaming.Hls |
MP4 stream-copy HLS, streaming AVC variants, continuous AAC/MP3 audio playlists and measured transport output |
Mediana.Execution |
Pull-driven decoding, shared A/V demux, encoding/remux operations, delayed output and cancellation |
Mediana |
Explicit input/output registration, container detection and individual or combined A/V sessions |
Mediana.Cli |
Command-line media inspection, conversions, previews and exports |
Mediana.Sample |
Minimal generated-frame example using the public API |
Core does not reference the other components. Containers do not reference codecs. Codec modules do not open files or invoke external processes; complete-image adapters can write to caller-owned streams. The public facade composes these contracts. See module boundaries, stream contracts and reusable operations.
The format implementation catalog maps target containers, codecs, images, documents and 3D formats, with independent progress for reading, decoding, rendering, encoding and writing. Its operation checklist separately tracks samples, correctness, CPU, memory and Native AOT.
The H.264 decoder reconstructs progressive 8-bit 4:2:0 CAVLC I/P/B and CABAC I/P/B pictures with default scaling, including motion compensation, reference picture management, intra prediction, inverse transforms, PCM blocks and deblocking. Each packet contains a complete picture, with one or more slices in increasing macroblock order. Three further CAVLC/CABAC multislice sequences match all 54 independent reference pictures, including slice starts inside macroblock rows, per-slice quantizers, temporal/spatial B prediction and filtering across slice edges. Two complete Baseline/Main CAVLC sequences and one cropped IDR picture match the independent filtered YUV references sample for sample (nineteen pictures total). The Main sequence includes explicit prediction weights and reference-list changes. Two High CAVLC sequences with 4x4/8x8/16x16 intra prediction, inter 8x8 transforms, motion, weighted prediction and nonzero deblocking offsets match thirty further independent YUV pictures internally and through public sessions. Three High CABAC sequences with the same transform sizes match forty-two further independent YUV pictures, including all three CABAC initialization selections. Two High CAVLC I/P/B sequences add 36 complete independent YUV pictures, including spatial/temporal direct prediction, both reference lists, implicit prediction weights, reference B pictures and deblocking. Public sessions preserve presentation order; selected B-picture sRGB PNGs match independent pixels within one channel level. All 85 CAVLC pictures, six Main CABAC intra pictures and 96 additional CABAC I/P pictures match through MediaEngine. Five CABAC sequences add 98 complete pictures with B prediction and all initialization selections. Timing, cropping and sample aspect ratio are preserved. Preview generation requires the supported CAVLC/CABAC I/P/B subset and known color and sample aspect ratio; it does not cover general H.264 files.
Images and 2D animation
Mediana.Images.Apk.ApkIconReader.ReadAsync(apkStream, size, cancellationToken) returns an owned square RGBA32 frame, or null when the application omits android:icon. Dispose the returned frame after use. The reader supports PNG, WebP, JPEG and adaptive icons with color, bitmap or solid-painted vector layers. It renders the central adaptive viewport without a device-specific launcher mask. The requested side is 1–4096 pixels. Non-seekable input is buffered up to 256 MiB.
The image modules use their own document/component APIs instead of inventing
video tracks for HEIF items, JBIG2 pages or vector scenes. Raster results expose
ToVideoFrame for the existing processing and PNG APIs. HEIC and JPEG 2000 retain
native color information; conversion rejects ICC profiles instead of labelling
them sRGB. The common preview pipeline provides explicit PQ/HLG tone mapping. JPEG 2000 native component
precision remains available separately from its optional RGBA8 preview.
dotnet run --project tools/Mediana.Cli -c Release -- image heic photo.heic preview.png --aspect-width 1 --aspect-height 1
dotnet run --project tools/Mediana.Cli -c Release -- image jp2 scan.jp2 preview.png --aspect-width 1 --aspect-height 1
dotnet run --project tools/Mediana.Cli -c Release -- image jbig2 scan.jb2 preview.png --aspect-width 1 --aspect-height 1
dotnet run --project tools/Mediana.Cli -c Release -- image svg artwork.svg preview.png --width 512
dotnet run --project tools/Mediana.Cli -c Release -- image tgs animation.tgs preview.png --time 0.5 --width 512
Raster examples select the primary HEIC item or first JBIG2 page. The final two
arguments explicitly declare pixel aspect ratio; 1 1 means square pixels.
The APIs also expose item selection, all JBIG2 pages and native JPEG 2000 samples.
SVG/TGS width preserves the viewport proportions; TGS time is seconds from its
in-point. Existing output files use the shared confirmation and --overwrite
policy. Input, output and workspace have separate budgets; the output allocator is not a whole-process
memory limit.
Supported subsets and limits: HEIC,
JPEG 2000, JBIG2,
SVG, TGS/Lottie, PNG, JPEG.
TGS/Lottie expression code is not executed. CLI previews use stored property
values and omit Puppet deformation, with warnings; library callers enable these
policies explicitly through TgsReaderOptions.
PDF page rendering supports the subset described below. 3D scene/model import,
rendering and export are future targets in the
format catalog.
Download and run
Each publishing run attaches standalone Native AOT command-line executables to the GitHub release. Download the executable for your operating system and processor. No .NET SDK, .NET runtime or FFmpeg installation is needed.
| Download | Platform |
|---|---|
Mediana-<version>-linux-x64 |
Linux x64 (glibc), built on Ubuntu 24.04 |
Mediana-<version>-win-x64.exe |
Windows x64 |
Mediana-<version>-osx-arm64 |
macOS on Apple Silicon, built on macOS 15 |
Rename the downloaded file to mediana (mediana.exe on Windows).
On Linux/macOS, make it executable with chmod +x mediana.
Optional archives include the same executable, this README and the MIT license:
| Archive | Platform | Executable |
|---|---|---|
Mediana-<version>-linux-x64.tar.gz |
Linux x64 (glibc), built on Ubuntu 24.04 | mediana |
Mediana-<version>-win-x64.zip |
Windows x64 | mediana.exe |
Mediana-<version>-osx-arm64.tar.gz |
macOS on Apple Silicon, built on macOS 15 | mediana |
Each executable and archive has a matching .sha256 checksum file. For Linux,
use Ubuntu 24.04 or a compatible newer glibc distribution; the executable does not
target Alpine.
From the download or extraction directory on Linux/macOS:
./mediana --help
./mediana -i audio.mp3
./mediana probe artwork.svg
./mediana cover audio.mp3 cover.png
./mediana -i audio.mp3 out.wav
./mediana -i video.mp4 audio.mp3
./mediana -i photo.heic out.png
./mediana -i photo.heic out.webp
./mediana -i artwork.svg out.webp --image-compression Lossy --quality 80
./mediana -i sticker.tgs animation.webp --animate
./mediana -i artwork.svg out.png
./mediana -i report.pdf page.png --page 1
./mediana -i input.mp4 frame.png --time-ms 250
./mediana -i input.mp4 output.ts
./mediana -i input.mp4 encoded.ts --video-codec H264 --video-only --qp 26
./mediana -i input.mp4 small.ts --video-codec H264 --video-only --width 640 --height 360
./mediana -i input.mp3 audio.ts --audio-codec Aac --audio-bit-rate 128000
./mediana -i input.m4a stereo.ts --audio-codec Aac --audio-channels 2
./mediana probe mp4 input.mp4
./mediana hls create input.mp4 output-hls --audio-stream 1
mediana -i input inspects media when no output path is supplied, using the same
operation as mediana probe input. Detection uses file contents. Containers report
streams and declared metadata; PNG, JPEG, SVG, HEIF, JPEG 2000, JBIG2, PDF, DOCX and TGS/Lottie report
image, first-page or animation-canvas dimensions without rendering. PDF and DOCX dimensions
are points (1/72 inch); animation metadata includes duration and frame rate. Image
inspection reads bounded input and does not decode pixel data. Conversion options
require an output path.
CLI inspection, audio/video conversion and HLS commands use shared resource settings without fixed megabyte or megapixel budgets. Individual buffers must fit the runtime's managed-array length, and processing requires enough available memory. Library applications can configure input, pixel and allocation budgets for their own workloads.
mediana cover input output --index N extracts original picture bytes from MP3,
FLAC or MP4/M4A metadata. Detection uses input contents, and indices follow
container order; no preference for the front or largest cover is applied. The
output path does not select an image encoder. External picture references are
reported without fetching or writing them as images. cover mp3, cover flac
and cover mp4 use the same operation and require their named container.
mediana cover select input output searches attachments and picture tags by
registered image signatures, preserving the selected image's original encoding.
--first Picture gives picture tags priority. Names and MIME declarations do not
select the image. The container image API exposes the
same explicit ordering, bounded prefix reads and owned payloads to applications.
On Windows, use ./mediana.exe in PowerShell with the same arguments.
The MP4 probe lists stream indices; use the appropriate video/audio indices in
the HLS, preview and audio commands. Omit --audio-stream for hls create without
audio; use explicit --video-only for hls encode without audio. Output parent
directories must already exist. Interactive file commands ask before replacing an
existing output, with No as the default. --overwrite permits file replacement;
-y / --yes answers Yes to all confirmation prompts. Redirected input requires
one of these options to replace a file. File output is staged beside the destination
and published only after successful processing; cancellation or failure preserves
the previous file. HLS output directories must be new.
mediana -i audio.mp3 out.wav --overwrite
mediana -y -i audio.mp3 out.wav
TS stream copy accepts supported MP4/MOV AVC/AAC/MP3 tracks and native MPEG-1/2
Layer III files. MP4-to-HLS copies the supported AVC/AAC/MP3 tracks without re-encoding. PCM export
supports WAVE/AIFF PCM, FLAC, MP3 and the supported AAC-LC subset in MP4/M4A. It writes raw,
native-endian interleaved samples without a WAVE header and prints the sample
format, channel layout and sample rate. Video previews require the supported
H.264 subset described above. --video-codec H264 selects fixed-QP I/P video
encoding to TS. Inputs without audio need no audio-selection option. If audio is
present, this command requires --video-only to explicitly omit it; combined video
encoding and audio output is available through hls encode. --audio-codec Aac
selects audio-only AAC-LC encoding to TS; see audio encoding contracts and limits.
The command-line arguments match the source examples below: replace
dotnet run --project tools/Mediana.Cli -c Release -- with the executable.
The main command is mediana -i input output. Input content determines its
format; the output extension selects WAVE PCM16 (.wav or .wave), MP3 (.mp3), PNG (.png), WebP (.webp) or
MPEG-TS (.ts, stream copy by default). WAVE conversion preserves sample rate and channel
order and performs no resampling or dithering. RIFF output must fit its 32-bit
size fields; RF64 is not supported. Named multichannel audio must follow WAVE
speaker order. Still-image output is one image, a video frame at or after --time-ms
(default 0), an animation rendered at that time, or a document --page (default 1). Vector and animation
images use intrinsic dimensions; PDF and DOCX pages render at up to 96 DPI. The existing format
subsets described below also apply to these commands. WebP defaults to lossless;
--image-compression Lossy --quality 80 selects lossy color with exact alpha.
--animate writes a WebP sequence from TGS or video using shared frame timing.
See WebP encoding. Unsupported output formats
fail explicitly.
Every invocation first prints the product version and copyright in stderr.
Commands have typed options and generated help. Use mediana --help to list
commands and, for example, mediana audio decode --help for that command's options.
Add --log-level Debug after the command to include diagnostic details, or
--log-level None to disable diagnostics. Logs go to stderr; command results go
to stdout. Colors are automatic in an interactive terminal; --no-color or the
NO_COLOR environment variable disables them.
WAVE, AAC and H.264 conversion, WebP animation and hls encode report processed media time,
speed relative to playback and output size immediately, at one-second intervals,
and on completion. HLS counts input video duration once across all variants and
reports their combined transport size. When
EncodedStreamInfo.CodedDuration is known, a shared media-time estimator adds
eta=hh:mm:ss after a short warmup using smoothed recent processing speed.
The total comes from container metadata, including codec delay and padding; it
does not apply presentation edits. MP3 Xing/Info, WAVE, AIFF and FLAC with a known
sample count provide this metadata. Unknown totals, insufficient progress, stalls
and completion omit the ETA field. No extra file scan or bitrate estimate is used.
If an input frame has no known duration or its time base changes, subsequent
reports show output size without inventing processed time, speed or ETA.
Ctrl+C cancels processing and removes incomplete output. Existing destinations are never replaced. Exit codes are 0 for success, 1 for an unexpected failure, 2 for invalid arguments, 3 for an unsupported operation or format feature, 4 for invalid media data, 5 for file access errors, and 130 for cancellation.
Reusable client operations are described in Media operations.
Build and run
Install a .NET 11 SDK. The repository accepts newer .NET 11 feature bands.
Library packages require .NET 11. Native CLI downloads include their runtime. On x64, both require an x86-64-v2 processor, including SSE4.2 and POPCNT. See the .NET 11 migration and performance results.
dotnet tool restore
PACKAGE_VERSION=$(dotnet gitversion --show-variable SemVer)
dotnet restore Mediana.slnx
dotnet build Mediana.slnx -c Release --no-restore -p:Version="$PACKAGE_VERSION"
dotnet test Mediana.slnx -c Release --no-build --no-restore
dotnet run --project tests/Mediana.NativeTests -c Release --no-build --no-restore
dotnet format Mediana.slnx --verify-no-changes --no-restore
dotnet run --project samples/Mediana.Sample -c Release --no-build -- --self-test
dotnet run --project tools/Mediana.Cli -c Release --no-build -- probe mp4 tests/Mediana.Tests/Data/Mp4/avc-aac.mp4
dotnet run --project tools/Mediana.Cli -c Release --no-build -- cover mp4 tests/Mediana.Tests/Data/Metadata/covers.m4a artifacts/cover.png --index 0
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio decode tests/Mediana.Tests/Data/Wave/s24-surround.wav artifacts/surround.pcm --stream 0
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio resample tests/Mediana.Tests/Data/Wave/s16-stereo.wav artifacts/mono-400.s16le --stream 0 --sample-rate 400 --gains 0.5 0.5
dotnet run --project tools/Mediana.Cli -c Release --no-build -- video preview tests/Mediana.Tests/Data/Mp4/avc-weighted.mp4 artifacts/preview.png --stream 0 --time-ms 250 --width 100 --height 100
dotnet run --project tools/Mediana.Cli -c Release --no-build -- video preview tests/Mediana.Tests/Data/Mp4/avc-weighted-rotated.mp4 artifacts/preview-rotated.png --stream 0 --time-ms 0 --width 100 --height 100
dotnet run --project tools/Mediana.Cli -c Release --no-build -- video preview tests/Mediana.Tests/Data/Mp4/avc-cabac-intra.mp4 artifacts/preview-cabac.png --stream 0 --time-ms 450 --width 100 --height 100
dotnet run --project tools/Mediana.Cli -c Release --no-build -- video preview tests/Mediana.Tests/Data/Mp4/avc-cabac-p2.mp4 artifacts/preview-cabac-p.png --stream 0 --time-ms 450 --width 100 --height 100
dotnet run --project tools/Mediana.Cli -c Release --no-build -- video preview tests/Mediana.Tests/Data/Mp4/avc-cavlc-8x8-weighted.mp4 artifacts/preview-high-cavlc.png --stream 0 --time-ms 450 --width 100 --height 100
dotnet run --project tools/Mediana.Cli -c Release --no-build -- video preview tests/Mediana.Tests/Data/Mp4/avc-cabac-8x8-2.mp4 artifacts/preview-high-cabac.png --stream 0 --time-ms 450 --width 100 --height 100
The self-test decodes a generated 2×2 RGBA packet through the codec protocol, resizes it to 1×1, checks the red output pixel and exact time conversion, then verifies that all leased buffer bytes have been released. It does not open a video file.
The preview command takes input, an output path, video stream index, target
--time-ms in the media timeline, --width and --height. In this example,
it selects the first frame at or after 250 ms (300 ms) and writes a 100×49 sRGB PNG
with square pixels. The output directory must exist. Existing outputs follow the CLI overwrite policy;
a request beyond the last frame fails without creating an image.
The rotated fixture writes 49×100 after applying its display matrix and SAR.
The CABAC intra fixture selects 600 ms for a request at 450 ms and writes 100×49.
The CABAC P fixture selects 500 ms for the same request and writes 100×49.
The High CAVLC and CABAC 8x8 fixtures select 500 ms and write 100×50.
Publish and execute it with Native AOT on Linux x64:
dotnet publish tools/Mediana.Cli -c Release -r linux-x64 -m:1 -p:IlcSingleThreaded=true -p:Version="$PACKAGE_VERSION" -o artifacts/native/linux-x64
./artifacts/native/linux-x64/Mediana.Cli --help
./artifacts/native/linux-x64/Mediana.Cli probe mp4 tests/Mediana.Tests/Data/Mp4/avc-aac.mp4
./artifacts/native/linux-x64/Mediana.Cli audio decode tests/Mediana.Tests/Data/Wave/s16-stereo.wav artifacts/audio-native.pcm --stream 0
./artifacts/native/linux-x64/Mediana.Cli video preview tests/Mediana.Tests/Data/Mp4/avc-weighted.mp4 artifacts/preview-native.png --stream 0 --time-ms 250 --width 100 --height 100
dotnet publish tests/Mediana.NativeTests -c Release -r linux-x64 -m:1 -p:IlcSingleThreaded=true -p:Version="$PACKAGE_VERSION" -o artifacts/native-tests/linux-x64
./artifacts/native-tests/linux-x64/Mediana.NativeTests
Native AOT requires the platform build toolchain. The resulting executable targets one OS/architecture and still uses standard OS runtime facilities. See the .NET Native AOT prerequisites.
Mediana.NativeTests executes internal H.264 CAVLC/CABAC I/P/B reconstruction and public MediaEngine video sessions. Each path compares all visible YUV samples in 85 CAVLC pictures with the checked-in references. Public sessions also compare six CABAC intra pictures and 96 CABAC I/P pictures and verify timing, cropping, sample aspect ratio and allocation release. Its fixture loading and comparison code is shared with the unit tests. Indexed video seeking is checked against six independent YUV sequences, including backward targets, EOF restart and retained frames. The native checks also compare SDR conversion with an independent sRGB reference and decode a square-pixel PNG preview to verify every sample, CRC, color tag and pixel aspect ratio, including the rotated MP4 fixture. Internal CABAC decision, bypass and termination arithmetic is checked against a separate arbitrary-precision interval writer that shares the normative probability constants. These arithmetic components and checks are adapted from the MIT-licensed Heic.NET, with H.264 context initialization and bit reading. Internal checks also compare 227 I-slice contexts at every 8-bit quantizer with a separate initialization reference and read the beginning of a real CABAC IDR. P-slice initialization is checked for 252 contexts, all 52 quantizers and all three initialization selections; the first real non-skipped P partition matches an independent macroblock trace. The shared residual reader is checked against exact intervals with I and all P initialization selections. Internal P-slice syntax checks cover skip flags, all 30 permitted macroblock types, the four sub-macroblock types, reference indices and signed motion differences, including unary/Exp-Golomb boundaries and invalid precision/reference limits. Real I/P slice headers match an independent header trace. Internal CABAC reconstruction also matches seven independent filtered YUV pictures, including mixed 4x4/16x16 intra prediction, cropping and nonzero deblocking offsets. The public decoder reconstructs supported CABAC I/P/B pictures, including weighted prediction, multiple references and per-partition motion differences. Four complete CABAC I/P sequences match 54 independent YUV pictures with all three cabac_init_idc selections, mixed intra/inter prediction and cropped storage. Eight-bit CAVLC and CABAC support 8x8 intra prediction/transforms; all nine directional prediction modes match explicit independent vectors. Three High CABAC sequences match 42 additional complete I/P pictures with actual intra/inter 8x8 use. The 34 progressive 8x8 contexts match all 7072 normative initial states; residual intervals exercise every scan position, signs and large magnitudes. CAVLC B reconstruction covers all macroblock and sub-macroblock types, spatial and temporal direct prediction, both reference lists and deblocking. Two real sequences verify all 36 reordered pictures and selected B-picture PNG previews. Five CABAC sequences verify another 98 complete pictures, including all three initialization selections, actual B-picture 8x8 transforms, spatial/temporal direct, reference B pictures, public presentation timing and selected B-picture PNG previews. B-specific contexts match all 2496 normative initial states; exact intervals exercise all 49 macroblock types, 13 sub-macroblock types and independent L0/L1 reference-index and motion-difference state. Broader slice support remains open. Unsupported or damaged pictures fault the session and release pending output; reset or disposal is required. Unused bits in the arithmetic terminal byte are discarded; following zero words are checked. This decoder is not a complete bitstream conformance validator. Seven complete WAVE sequences also match independent PCM references through public audio sessions with a 16 KiB media-buffer budget, including unsigned/signed integers, both float precisions and six-channel speaker order. Returned audio frames retain their storage after session disposal. CI is configured to build and run both native executables on Linux, Windows and macOS.
Reading WAVE audio
Reference the Mediana.Containers.Wave and Mediana.Codecs.Pcm source projects:
MediaEngine engine = new MediaEngineBuilder()
.AddContainer(new WaveReaderFactory())
.AddAudioDecoder(new PcmDecoderFactory())
.AddAudioDecoder(new WaveCompressedAudioCodec(WaveCompressedAudioEncoding.ALaw))
.AddAudioDecoder(new WaveCompressedAudioCodec(WaveCompressedAudioEncoding.MuLaw))
.AddAudioDecoder(new WaveCompressedAudioCodec(WaveCompressedAudioEncoding.ImaAdpcm))
.AddAudioDecoder(new WaveCompressedAudioCodec(WaveCompressedAudioEncoding.MicrosoftAdpcm))
.Build();
await using RandomAccessStreamByteSource source = new(File.OpenRead(path));
await using AudioSession session = await engine.OpenAudioAsync(source, 0);
using AudioFrame? frame = await session.ReadFrameAsync();
The namespaces are Mediana, Mediana.Codecs.Pcm, Mediana.Containers.Wave, Mediana.Core.Audio, Mediana.Formats.Audio and Mediana.IO. Sequential byte sources are also supported. Sessions borrow the source, own decoder/reader state, enforce cancellation and configured resource limits, and return independently owned PCM frames. Frame PTS and duration use exact sample counts. Random-access opening preserves the source cursor.
The reader supports one little-endian RIFF/WAVE data chunk, preceded by its
fmt configuration. Integer PCM accepts 8/16/24/32-bit storage; IEEE float accepts
32/64-bit storage. The shared Mediana.Formats.Audio.PcmConfiguration describes standard WAVEFORMATEX
or WAVEFORMATEXTENSIBLE bytes, including valid precision and explicit channel order.
The decoder returns interleaved native-endian samples. Signed 24-bit input expands
to left-aligned signed 32-bit output; float bit patterns retain their precision.
Channel masks preserve the declared speaker order; unassigned channels remain discrete.
G.711 A-law and μ-law, 3-/4-bit IMA ADPCM, and 4-bit mono/stereo Microsoft ADPCM
produce interleaved native-endian PCM16.
An ADPCM fact chunk between fmt and data supplies the exact decoded length, including
when the final block is padded. Register WaveCompressedAudioCodec through
AddStreamMetadataReader to probe sample rate and channel order without decoding.
Other metadata chunks are skipped within byte/chunk limits. Trailing chunks and padding
are checked when the stream is drained. RF64, RIFX, nested audio lists and
tag/artwork extraction are unsupported.
The audio decode command takes input, a raw PCM output path and --stream;
it reports the sample format, rate and channels needed to interpret its output.
It does not write a WAVE header. The output directory must exist; replacement of
an existing file requires confirmation or explicit --overwrite / -y. See the WAVE fixtures
for independent references and format definitions.
Reading AIFF audio
Mediana.Containers.Aiff reads one SSND chunk from AIFF or AIFF-C, with exact
sample counts and timestamps. Add AiffReaderFactory to the container registry
and AiffPcmDecoderFactory or AiffIma4Codec from Mediana.Codecs.Pcm to the audio decoder registry.
The decoder shares PCM conversion with the WAVE path. Its pcm.aiff configuration
contains the four-byte FORM type followed by the complete COMM payload.
Supported input is mono/stereo signed integer AIFF with 1–32 bits of precision,
AIFF-C NONE, 16-bit sowt/twos, and big-endian fl32/fl64. Decoded integer
storage is native-endian and left-aligned; signed 8-bit input becomes unsigned
8-bit PCM. A positive integral sample rate must fit AudioFormat.
AIFF-C ima4 uses one 34-byte packet per channel for 64 interleaved PCM16
sample frames. The reader expands the COMM packet count into decoded samples.
Register AiffIma4Codec as a stream metadata reader to inspect it without decoding.
The reader borrows the source, streams bounded packets and honors SSND offsets
and trailing block fill. COMM and the AIFF-C version chunk must precede SSND;
other chunk orders requiring a backwards read are unsupported. Unknown metadata
chunks are skipped within byte/count limits. Artwork and fractional sample rates
require additional handling. Audio channel layouts are mono and stereo.
Trailing chunks and padding are checked when the stream is drained.
The CLI detects AIFF by content for WAVE conversion, raw PCM, resampling and waveform data:
mediana -i recording.aiff output.wav
mediana audio waveform recording.aiff waveform.json --gains 0.5 0.5
AIFF references cover complete integer and float sequences through the public session, including sample timing, independent frame ownership and a 16 KiB media-buffer budget.
Decoding AAC-LC
Reference Mediana.Containers.Mp4 and the Mediana.Codecs.Aac source project:
MediaEngine engine = new MediaEngineBuilder()
.AddContainer(new Mp4ReaderFactory())
.AddAudioDecoder(new AacDecoderFactory())
.Build();
await using RandomAccessStreamByteSource source = new(File.OpenRead(path));
await using AudioSession session = await engine.OpenAudioAsync(source, audioStreamIndex);
using AudioFrame? frame = await session.ReadFrameAsync();
The decoder accepts AAC-LC with indexed sample rates from 8000 to 96000 Hz,
standard channel configurations 1–7 and one raw_data_block per packet.
It returns 1024 native-endian interleaved float32 samples per channel per frame.
Mono is front-center; stereo is front-left, front-right. Packet PTS is
preserved, and frame duration is exactly 1024 samples. The decoder preserves
encoder delay and final padding. The final PCM count can therefore exceed the
original recording length. Explicit sample selection is available after decoding.
Multichannel PCM uses speaker-bit order, with the coded elements mapped to their
declared speakers. Configuration 7 is 7.1 wide: five front speakers, two back
speakers and LFE. Each channel retains its own overlap/window state; stereo tools
apply independently to each pair. Element types, counts and order must match the
configuration, with unique instance tags per type that remain stable until reset.
All eleven spectral codebooks, escape values, scalefactors, pulse data, long/short
sine/KBD windows, TNS, perceptual noise substitution, M/S and intensity stereo
are implemented. Mono/stereo SBR and mono-core parametric stereo reconstruct
high-frequency and stereo output through persistent filterbanks. Single-rate
output contains 1024 samples per channel; dual-rate output contains 2048.
AacConfiguration retains core parameters while AudioFrame.Format describes
the actual output. See HE-AAC signaling, recovery and verification.
Other spatial extensions, dynamic-range extensions,
coupling/program-configuration elements, 960-sample
frames and 7350 Hz are unsupported. Standalone ADTS input uses
Mediana.Containers.Adts; see ADTS reading.
ADIF input is not implemented.
Unsupported tools and malformed input fail explicitly; failure or cancellation
requires reset or disposal. Reset clears overlap and noise state, so continuous
output requires decoding the preceding packets. The default access-unit byte
limit is 8192 and can be configured in the factory.
Three complete real M4A sequences run through the same managed and Linux Native
AOT checks with a 128 KiB media-buffer budget. Two PNS-disabled sequences match
independent float PCM within 2e-6 for every sample (observed maximum error
8.94e-8). The PNS-enabled sequence uses channel/block and total-energy checks
because random sequences differ between decoders. Tests also verify all four
window sequences, both shapes, TNS, joint stereo, every PTS, retained-frame
ownership, cancellation, reset and partial allocation failure. Numeric tables and
equations follow ISO/IEC 13818-7:2004 and ISO/IEC 14496-3:2009; see the
AAC references for provenance and limits.
Five complete multichannel references cover configurations 3–7, including distinct
speaker signals, LFE, back-center, 7.1 wide and independent pair masks/windows.
Every Float32 sample matches independent decoding within 2e-6; timestamps,
channel labels, EOF and retained-frame ownership are checked. See the
multichannel references.
A separate stereo reference covers every window-sequence and sine/KBD shape
transition, plus independent/common window histories, against FAAD2. The complete
original 5.1 film track also matches FAAD2 on all 171,786,240 scalar samples;
full-source accuracy and CPU/memory results include the observed
FFmpeg differences.
Two complete sequences also pass through explicit mono mixing, 400 Hz sinc resampling, PCM16 quantization and streaming RMS/peak in managed and Linux Native AOT execution. Every resulting PCM16 sample differs by at most one from processing the independently decoded float reference, with matching sample counts, PTS, durations and final partial level windows. This isolates the effect of AAC decoding. A separate comparison with Cotton's waveform renderer is described below.
The same audio decode and audio resample commands accept supported M4A
inputs. Select the audio stream index explicitly:
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio decode tests/Mediana.Tests/Data/Aac/lc-stereo-44.m4a artifacts/aac.f32le --stream 0
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio resample tests/Mediana.Tests/Data/Aac/lc-stereo-44.m4a artifacts/aac-mono-400.s16le --stream 0 --sample-rate 400 --gains 0.5 0.5
Processing PCM
The Mediana.Processing.Audio namespace provides independent processing steps.
AudioTrimmer skips a caller-specified number of decoded samples and optionally
selects an exact sample count. It pulls owned frames, retains complete blocks
without copying their PCM and copies partial boundary blocks into its allocator.
Every returned frame has an independent lease; complete blocks remain charged
to their original allocator. It keeps no recording-sized history. Interleaved and
planar PCM retain their format, channel order and exact sample bytes. A null count
reads through EOF; a finite requested range must exist completely. A zero count
returns EOF without reading. Original timestamps remain continuous unless the
caller supplies an explicit output origin. It stops reading at the selected end;
the source remains borrowed and must still be disposed by its owner.
AudioTrimmer.CreateTimeWindow(readFrame, start, end, allocator) selects sample
instants in a half-open presentation-time interval. It uses exact rational clocks,
keeps the original sample grid and timestamps, and selects the available PCM
without adding silence. Apply container edits before selecting movie time. Known,
continuous input timestamps are required; seeking and codec restart state belong
to the input session. See audio/video intervals for
a complete H.264/AAC-to-MPEG-TS example.
AudioFrame.RetainWithTimestamp(timestamp) retains immutable PCM with an explicit
known or unknown timestamp. Sample count, duration and the original frame remain
unchanged. Audio trimming uses this operation when a complete block needs a new
output origin. See the complete audio-window comparison.
Mp4AudioSampleRange.FromEditList(track, decodedSampleRate) translates one
explicit unit-rate media edit, optionally preceded by an empty edit, into a PCM
range and the first sample's movie presentation time. It selects sample instants
in the half-open edit interval using exact rational arithmetic. Leading empty
edits shift the returned PTS without generating silent PCM. Missing or nonlinear
edits and unsupported exact clocks fail explicitly; codec delay and roll groups
are not inferred. Range indices count from media time zero, so decode preceding
packets from that point before applying the range; arbitrary seeking needs
separate decoder preroll handling. MP4/MOV AAC-LC sessions provide
AudioSession.SeekAsync; after seeking, select time
windows on the mapped clock instead of reapplying a media-zero sample index.
GetPresentationEnd(decodedSampleRate) returns the first excluded sample instant
on that movie grid after the exactly selected sample count, including when the
declared edit ends between samples. The audiovisual interval sample composes indexed audio
restart, exact edit selection and the requested time window.
Mp4AudioSampleRange range = Mp4AudioSampleRange.FromEditList(track, decodedSampleRate);
await using AudioTrimmer trimmer = new(session.ReadFrameAsync,
range.StartSample, range.SampleCount, allocator, range.PresentationTime);
using AudioFrame? frame = await trimmer.ReadFrameAsync();
Pass null instead of range.SampleCount to remove only the leading samples.
These policies differ: on the stereo AAC reference, ordinary FFmpeg decoding
returns 16384 samples after removing priming, while the exact edit selects 16317.
Both complete results are checked against independent decoding in managed and
Linux Native AOT execution. The exact edit also feeds the complete mono 400 Hz
PCM16 and RMS pipeline; its samples and levels match processing of the independently
decoded and cropped reference, including both edges. Edit-list time is distinct from AAC frame duration;
see Apple's explicit encoder-delay representation.
Mp4TrackInfo.RecoveryGroups exposes declared roll/prol recovery requirements.
GetDistance(sampleIndex) returns the signed distance for a zero-based encoded
sample index, including mapping runs and defaults. These counts are encoded
packets, distinct from PCM trimming and encoder delay. See
MP4 recovery groups.
AudioChannelMixer takes an explicit output-by-input coefficient matrix and channel
labels. It preserves finite headroom in interleaved Float64 or Float32 output, without implicit
speaker mapping, gain normalization or clipping. AudioSampleConverter changes
native-endian sample representation and planar/interleaved storage. Integer output
saturates and rounds to nearest even without dithering; processing rejects NaN,
infinity and values that exceed finite target float storage. Both operations borrow
the input frame and return an independently owned frame with preserved timing.
AudioDownmix.CreateMono and CreateStereo construct explicit normalized matrices
for named front/center/surround speakers, with LFE omitted. A common attenuation
preserves stereo balance and bounds unit input peaks. Float32 output feeds an
encoder directly. See channel policy, API and complete-source verification.
AudioLevelAccumulator emits fixed-size mono RMS/peak windows with sample counts,
PTS and duration. Windows span input frame boundaries; Complete emits the final
partial window. A scaled energy sum supports finite floating-point headroom without
squaring overflow or underflow. Frames are borrowed, samples are not retained, and
levels are passed by value to a synchronous callback. Emitted windows are a prefix
if processing or the callback fails. Display normalization, tapering and waveform
rendering belong to the application.
AudioWaveformReader combines an explicit mono mix, resampling, optional sample
quantization and level accumulation behind ReadLevelAsync. Each call returns
one AudioLevel, or null at EOF. AudioWaveformOptions requires the target rate
and window size; its sample format defaults to float64, preserving finite
headroom. Selecting Signed16 applies saturation and PCM16 quantization before
computing RMS/peak. The final partial window is returned with its exact count,
timestamp and duration. Returned levels contain no frame leases.
await using AudioWaveformReader waveform = new(
session.ReadFrameAsync, new double[] { 0.5, 0.5 },
new AudioWaveformOptions(400, 40)
{
SampleFormat = AudioSampleFormat.Signed16,
Resampling = new AudioResampleOptions
{
CutoffRatio = 0.97,
BoundaryMode = AudioBoundaryMode.Reflect
}
}, allocator);
while (await waveform.ReadLevelAsync(cancellationToken) is AudioLevel level)
{
ConsumeLevel(level);
}
The reader also accepts an existing mono AudioChannelMixer, including
AudioDownmix.CreateMono(inputFormat). It applies that matrix directly before
resampling; a stereo-output mixer is rejected. The
complete file example registers WAV, MP3, FLAC and MP4/AAC
and writes streaming JSON without retaining the complete PCM recording:
dotnet run --project samples/Mediana.Sample -c Release -- --waveform input.flac > waveform.json
The audio preview example opens a container once and writes either an embedded PNG/JPEG cover or streaming waveform JSON:
dotnet run --project samples/Mediana.Sample -c Release -- --audio-preview input.mp3 cover.png waveform.json
The reader owns callback frames and filter storage, borrows the source and uses
the caller's media-buffer allocator. It keeps bounded processing history and one
pending level, independent of recording length. EOF, failure, cancellation and
disposal release owned storage. Failure or cancellation makes the reader terminal;
concurrent reads/disposal fail explicitly. It infers neither channel mapping nor
codec priming. To select a decoded range, pass AudioTrimmer.ReadFrameAsync as the
callback. Display shaping remains in the application. Managed and Linux Native
AOT checks cover independent WAVE/speech references and raw/exact-edited AAC.
AudioResampler pulls owned frames from an asynchronous callback and returns
interleaved float64. It uses a Blackman-windowed sinc low-pass filter before rate
conversion, a polyphase coefficient bank, bounded sample history and exact rational
positions. Cutoff, filter support, output block size and coefficient-byte limits are
configurable. The caller supplies an allocation budget. The input format and PTS
must remain continuous; unknown timestamps remain unknown. Filter lookahead delays
availability, while output timestamps retain the original timeline. BoundaryMode
defaults to Zero; Reflect mirrors sample positions around the first and last
sample instants without repeating endpoints. A single-sample recording repeats
that sample. Both modes use the same bounded history. Output contains
ceil(input samples * output rate / input rate) samples. Equal rates preserve the
samples without filtering.
The rate-conversion basis is described in
Windowed Sinc Interpolation.
The resampler owns frames returned by the callback and releases its history on drain, failure or disposal. It borrows the callback's source; returned output frames survive resampler disposal. Concurrent operations, discontinuities and unsupported exact clock/filter dimensions fail explicitly. Managed and Linux Native AOT checks cover tone amplitude, independent channels, exact counts/timestamps and output ownership; managed checks also cover stop-band rejection, packet boundary invariance, early disposal, cancellation, resource limits and invalid input.
The audio resample command takes input, an output path, --stream,
--sample-rate and space-separated --gains in source-channel order. It mixes to mono,
resamples and streams signed 16-bit little-endian PCM with no header. For stereo,
--gains 0.5 0.5 selects an average; multichannel gains must be supplied explicitly. This
provides the PCM shape needed for a 400 Hz waveform. The complete mono mix →
resample → PCM16 → RMS path is checked on a real WAVE input and analytic tone energy
in managed and Linux Native AOT execution. AAC-LC supplies compressed input
for the same processing steps. Other compressed audio, cover art in other
containers and Cotton integration remain open.
An explicit CutoffRatio = 0.97, BoundaryMode = Reflect profile is checked
against independent mono 400 Hz PCM16 references for silence, endpoint impulses,
a synthetic musical signal, recorded speech and a fractional-duration recording.
Every sample, timestamp and RMS/peak window is checked in managed and Linux
Native AOT execution; the corpus documentation
states the numeric bounds and speech attribution. General defaults remain 0.94
and zero extension.
A local comparison passed these five WAVE inputs and three AAC-LC M4A inputs through Cotton's existing 150-pixel waveform renderer. For AAC it removed only the explicit edit's leading samples, preserving final decoded padding to match the existing application policy. Mediana's output had the same PCM length on all eight inputs. The maximum sample difference was 34 PCM16 steps; alpha-silhouette intersection over union ranged from 99.64% to 100%. The comparison supplied Mediana PCM through a 400 Hz WAVE wrapper and independently verified byte identity through the renderer's existing PCM decode step. Lossy WebP pixels differ; these short inputs establish neither bit-identical rendering nor general media equivalence. Application integration and the policy for the final remainder of samples beyond Cotton's equal bar windows remain open.
The sample exports waveform data directly from supported WAVE, AAC-LC or native FLAC sources:
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio waveform tests/Mediana.Tests/Data/Waveform/speech.wav artifacts/speech-waveform.json --stream 0 --sample-rate 400 --samples-per-window 40 --gains 1
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio waveform tests/Mediana.Tests/Data/Aac/lc-stereo-44.m4a artifacts/aac-waveform.json --stream 0 --sample-rate 400 --samples-per-window 16 --gains 0.5 0.5
Arguments are input, a new JSON output, stream index, target rate, samples per window and gains in source-channel order. The sample explicitly selects PCM16, cutoff 0.97 and reflected boundaries. JSON includes RMS/peak, counts and rational timestamps/durations; periodic flushes bound writer buffering. Output is published only after successful completion. Existing destinations are preserved and failed processing removes the temporary output. Like the PCM-export samples, it retains complete decoded AAC frames, including priming/padding; application edit selection is available through the library API. The same command runs with the Native AOT sample executable.
Managed and Native AOT JSON outputs match on recorded speech and stereo AAC.
A local Linux x64 Native AOT run processed a generated three-minute 48 kHz stereo
WAVE into 72000 mono samples and 1800 level windows in 2.01 seconds, with an
observed peak RSS of 13056 KiB. All level counts/times matched an independent PCM
reference; maximum RMS error was 5.10e-6 and peak error one PCM16 step. This is
one resource observation, not a controlled throughput comparison.
Reading MP3 metadata, frames and pictures
The Mediana.Containers.Mp3 module reads MPEG-1/2/2.5 Layer III
files from a sequential source, including short reads and unknown source length.
It reads one leading ID3v2.3/v2.4 tag, text and original APIC picture bytes,
Xing/Info declarations and a final ID3v1 tag. The sample exposes:
dotnet run --project tools/Mediana.Cli -c Release -- probe mp3 input.mp3
dotnet run --project tools/Mediana.Cli -c Release -- cover mp3 input.mp3 cover.png --index 0
Mp3Reader.OpenAsync borrows the input. ReadPacketAsync returns one complete
Layer III frame with its own lease and continuous coded-sample PTS/DTS/duration;
Xing/Info is excluded from audio packets. Bitrate and padding may vary within a
stream. Frames may depend on previous main data through the bit reservoir, so
IsKeyFrame is unspecified. Coded timing includes codec delay and padding;
CodedDuration is declared by Xing/Info or available after a complete read.
The declared frame count is checked at EOF. ByteCount remains descriptive.
TextFrames preserves frame IDs, TXXX descriptions and values; v2.4 supports
multiple values. Pictures describes APIC bytes, and ReadPicture returns a
lease that survives reader disposal. Linked-picture URLs are returned as bytes
and never retrieved. TrailingTag becomes available after EOF and is kept
separate from ID3v2 text. Without a Xing/Info frame count, the metadata probe
counts frames on known-length random-access sources. It reads frame headers
without decoding audio or moving the packet cursor.
Metadata/frame sizes and counts are bounded. Unsynchronisation, grouping, v2.4 data-length indicators and plain extended headers are handled. Compressed or encrypted ID3 frames, extended CRC/update/restriction fields, ID3v2.2, VBRI, free-format frame discovery, junk resynchronization, stream format changes, seeking and container-level MPEG CRC verification are not supported. The decoder below verifies MPEG CRCs and enables PCM and waveform export. Explicit ID3 iTunSMPB presentation selection is available through the shared prepared audio source. Managed Layer III encoding and MP3 file output use the same registered reader/decoder pipeline; MPEG-TS supports complete MP3 frames.
The MP3 reference corpus verifies every packet's bytes and coded timing, text, picture contents, trailing tags and ownership against independent evidence for MPEG-1/2/2.5, CBR/VBR and both ID3 versions. The speech fixture has separate CC BY 4.0 attribution.
Decoding MP3 audio
Reference the Mediana.Containers.Mp3 and Mediana.Codecs.Mp3 source projects:
MediaEngine engine = new MediaEngineBuilder()
.AddContainer(new Mp3ReaderFactory())
.AddAudioDecoder(new Mp3DecoderFactory())
.Build();
await using AudioSession session = await engine.OpenAudioAsync(source, 0);
The decoder restores MPEG-1/2/2.5 Layer III at all nine standard sample rates, mono, stereo, dual channel, intensity stereo and mid/side stereo. It handles the bit reservoir, all Huffman tables, long/start/short/stop and mixed windows, alias reduction, overlap-add and polyphase synthesis. Output is native-endian, interleaved float32 PCM, with 1152 samples per MPEG-1 frame or 576 for MPEG-2/2.5. Timestamps follow the input packets. Coded encoder delay and end padding remain; LAME gapless trimming is not applied.
Mp3DecoderFactory(maxFrameBytes) bounds each complete encoded frame (4096 bytes
by default). Configuration contains the first four-byte header. Protected fields
are CRC-checked. The allocator accounts for reservoir, spectrum, synthesis history
and output storage. Retained output frames survive reset/disposal. Failure or
cancellation requires reset or disposal; reset clears all decoding history.
Seeking into reservoir-dependent frames requires decoding the preceding data.
Free-format input, format changes, de-emphasis and encoding are unsupported.
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio decode input.mp3 output.f32le --stream 0
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio waveform input.mp3 waveform.json --stream 0 --sample-rate 400 --samples-per-window 10 --gains 0.5 0.5
The PCM corpus compares complete real files and constructed all-rate stereo/window vectors with independent sample references in managed execution and Native AOT. It documents the distinct reference treatment of 8 kHz mixed windows and illegal low-rate intensity positions.
Reading native FLAC metadata and pictures
The source Mediana.Containers.Flac module reads native files beginning with
fLaC. STREAMINFO supplies rate, channels, precision, block/frame bounds, total
samples and the stored decoded-audio MD5. A zero total sample count has unknown
duration. Mediana.Formats.Audio.FlacConfiguration preserves these declarations without decoding PCM
or verifying the MD5.
MediaBufferAllocator allocator = new(32 * 1024 * 1024);
await using StreamByteSource source = new(File.OpenRead(path));
await using FlacReader reader = await FlacReader.OpenAsync(source, allocator);
IReadOnlyList<FlacComment> comments = reader.Comments;
IReadOnlyList<FlacPictureInfo> pictures = reader.Pictures;
using MediaBuffer picture = reader.ReadPicture(0);
await output.WriteAsync(picture.Memory);
The namespaces are Mediana.Containers.Flac, Mediana.Core.Memory and
Mediana.IO. The reader consumes the source from its current position and
borrows it through disposal. Comment spelling, UTF-8 values, repeated names and
file order remain available; compare comment names with OrdinalIgnoreCase.
Text decoding is strict. Original picture bytes have independent leases that
survive reader disposal and account for the complete picture metadata block.
Picture dimensions and media types are declarations, not image validation.
Media type --> exposes URI bytes without retrieving the resource.
ReadPacketAsync streams bounded compressed byte chunks in file order. They
have arbitrary boundaries and no frame timestamps/durations or key-frame flag;
a FLAC codec must recover actual frames and sample timing. The reader validates
metadata structure, block uniqueness, padding and seek-table entry ordering.
Application blocks and cuesheets are skipped; they are not interpreted. Limits
bound metadata bytes/blocks, individual text values, comments, pictures and packet
size. Media bytes use the supplied allocator. Cancellation/failure is terminal
and releases owned storage; concurrent operations/disposal fail explicitly.
Native metadata, exact compressed bytes and original PNG/JPEG pictures match independent references on stereo musical and recorded-speech files, with 127- and 16384-byte chunk limits; Linux Native AOT executes the same verifier with 127-byte chunks and a 128 KiB media-buffer budget. Parser checks include one-byte source reads, Unicode, repeated comments, linked pictures, retained leases, malformed data, configured limits, cancellation and concurrency. Source attribution and exact commands are in the FLAC reference documentation. The original implementation follows RFC 9639.
dotnet run --project tools/Mediana.Cli -c Release --no-build -- probe flac tests/Mediana.Tests/Data/Flac/stereo-covers.flac
dotnet run --project tools/Mediana.Cli -c Release --no-build -- cover flac tests/Mediana.Tests/Data/Flac/stereo-covers.flac artifacts/flac-cover.png --index 0
The probe reads metadata only. Cover extraction publishes original embedded bytes
after successful processing; replacement requires confirmation or explicit
--overwrite / -y. URI pictures are
rejected by that sample. Decoded-audio MD5
verification, seeking, encoding, custom channel masks,
reserved metadata block types and Ogg/MP4 FLAC mapping remain unsupported.
The managed and Linux Native AOT samples extract the original reference PNG/JPEG
bytes exactly and release all retained media buffers.
Decoding native FLAC audio
Reference the Mediana.Containers.Flac and Mediana.Codecs.Flac source projects:
MediaEngine engine = new MediaEngineBuilder()
.AddContainer(new FlacReaderFactory())
.AddAudioDecoder(new FlacDecoderFactory())
.Build();
await using AudioSession session = await engine.OpenAudioAsync(source, 0);
The decoder accepts contiguous encoded chunks with arbitrary boundaries, starting
at the first audio frame. It restores constant/verbatim subframes, fixed predictors
of orders 0–4, LPC orders 1–32, partitioned Rice4/Rice5 and escaped residuals,
wasted bits, independent channels and all three stereo decorrelation modes.
It uses wide integer arithmetic for predictions and the 33-bit stereo side channel.
PCM is native-endian, interleaved Signed32, with the original 4–32-bit precision
left-aligned. The 1–8 default channel positions follow RFC 9639 and use the same
speaker names as the WAVE/AAC modules. Explicit masks matching the default layout
are accepted; other masks are unsupported.
Frame headers, reserved/forbidden syntax, CRC8/CRC16, zero padding, reconstructed sample ranges and continuous frame/sample numbering are checked before output. Frame timestamps start at sample zero. Fixed/variable blocking and final short blocks are supported; when drained, known STREAMINFO sample totals must match. Changing rate/channel count/precision mid-stream is unsupported. STREAMINFO MD5 verification, seeking and encoding remain open.
FlacDecoderFactory(maxFrameBytes) bounds individual encoded frames (default
4 MiB); nonzero STREAMINFO frame limits also apply. Input packets retain their original leases. Encoded storage, prediction
workspace and returned PCM use the supplied media allocator. Calls must not
overlap. Failure/cancellation requires reset or disposal. Reset starts again at
frame zero; returned frames retain independent leases.
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio decode tests/Mediana.Tests/Data/Flac/stereo-covers.flac artifacts/flac.s32le --stream 0
dotnet run --project tools/Mediana.Cli -c Release --no-build -- audio waveform tests/Mediana.Tests/Data/Flac/mono-speech.flac artifacts/flac-waveform.json --stream 0 --sample-rate 400 --samples-per-window 37 --gains 1
The complete PCM from musical and recorded-speech 16-bit inputs, stereo and 5.1 24-bit files, and a 32-bit mid-side vector matches independent decoded bytes and sample counts. One-byte encoded packets are checked. All 400 Hz PCM16 waveform RMS/peak windows and timestamps for music/speech match their original WAVE sources exactly. The shared decoder checks use a 512 KiB media-buffer budget and verify retained frame ownership and full release. See the FLAC references for provenance and commands.
Reading MP4/MOV
Install the optional Mediana.Containers.Mp4 package to read supported files directly:
MediaBufferAllocator allocator = new(64 * 1024 * 1024);
await using RandomAccessStreamByteSource source = new(File.OpenRead(path));
await using Mp4Reader reader = await Mp4Reader.OpenAsync(source, allocator);
IReadOnlyList<Mp4TrackInfo> tracks = reader.Tracks;
using MediaPacket? packet = await reader.ReadPacketAsync();
The namespaces are Mediana.Containers.Mp4, Mediana.Core, Mediana.Core.Memory and Mediana.IO. Mp4ReaderFactory connects the same reader to MediaEngineBuilder. H264DecoderFactory connects the source H.264 module to decoded video sessions.
Movie-level meta full boxes expose Metadata and CoverArt. The reader supports
iTunes mdir item lists in movie user data and indexed mdta keys in a movie
metadata atom. Values retain their namespace, name, declared data type and raw locale;
repeated/localized values remain separate. Text decodes UTF-8/UTF-16BE strictly,
and is null for other types. ReadMetadataValue(index) returns the original bytes
for binary fields such as track/disc declarations and unsupported data types.
Freeform iTunes keys retain their explicit namespace and name. Text bytes and value
counts have additional configured limits within the existing movie/box budgets.
Mp4CoverArtInfo info = reader.CoverArt[0];
using MediaBuffer cover = reader.ReadCoverArt(0);
await output.WriteAsync(cover.Memory);
Cover declarations include iTunes covr and indexed com.apple.quicktime.artwork.
JPEG/PNG/BMP declarations expose a content type; unsupported declarations retain a
null content type and accessible original bytes. The reader does not decode or
validate the image codec. Cover bytes are separate from timed audio/video tracks.
An extracted lease survives reader disposal and retains the full backing movie
metadata allocation. Reading metadata/cover values does not move packet or source
cursors. The cover and cover mp4 commands take input, an image output path and
--index; they preserve the original encoding and bytes. Existing files follow
the shared confirmation and --overwrite policy.
The caller chooses an output suffix matching the declared image type.
Movie metadata names are not normalized to application fields. Track/media-level metadata, locale-list resolution, legacy QuickTime user-data text, headerless QuickTime metadata atoms and other handlers/indexed-key namespaces remain unsupported. Malformed structures, invalid text and unsupported metadata handlers fail explicitly. See the metadata fixtures for exact byte, Unicode and packet comparisons in managed and Linux Native AOT checks.
The reader requires random access and a known source length. It scans box headers, loads the bounded movie metadata and reads individual samples on demand. It does not load the media data box while opening. It supports movie metadata at either end of the file, sample/chunk mapping, decode time runs, signed/unsigned composition offsets, sync samples and 32/64-bit chunk offsets. Packets are interleaved by decode time; each track retains its sample order.
GetSampleInfo(streamIndex, sampleIndex) resolves the absolute source offset,
encoded size, PTS/DTS, duration and container sync indication without reading media
bytes or allocating a per-sample index. ReadPacketAtAsync(streamIndex, sampleIndex)
reads just that encoded sample and returns an owned packet. Both indices are
zero-based; sample order is decode order. These operations preserve every sequential
cursor, also after end of stream, and use the selected timestamp mode. Lookup scans
the compact chunk/timing runs and the sample sizes inside the containing chunk;
it does not read preceding payloads. The mapping follows the
QuickTime sample lookup.
Successive sample lookups advance a separate metadata cursor so a sequential
metadata scan stays linear even when many samples share one chunk.
Reader operations must be sequential. A packet read by index does not initialize
or reset a decoder, and a sync declaration alone does not verify codec-level
segment independence.
AVC streams expose the original avcC configuration and length-prefixed NAL packets.
MP3 mp4a entries with ES object type 0x69/0x6B obtain their configuration
from a bounded four-byte read of the first Layer III frame, without moving packet
cursors. Each sample contains one complete MP3 frame. Container declarations
remain available separately from the parsed frame configuration. Empty MP3 tracks,
Layer I/II and free-format audio are unsupported. The existing MP3 decoder can
decode these streams through the public audio session; edit-list trimming remains
an explicit application operation.
AAC streams expose the AudioSpecificConfig and raw access units. Container-declared
audio channels and sample rate are labelled explicitly: codec configuration may
describe different values, which require codec-level interpretation.
Movie duration, track presentation duration, original media duration and edit lists
remain separate. By default, packets use original media timestamps. Select
Mp4ReadOptions.TimestampMode = Mp4TimestampMode.LinearMovie to translate one
unit-rate media edit, optionally preceded by one empty edit, into movie time.
Packet PTS/DTS and durations use an exact rational time base, and tracks are merged
by their translated DTS. reader.Streams describes that packet time base;
reader.Tracks preserves original declarations and edits. Unsupported edit shapes
or an unrepresentable exact time base fail explicitly in this mode. The translation
follows the QuickTime edit-list mapping.
Both modes retain every compressed sample, including negative decoder priming, audio padding and samples outside the edit interval. LinearMovie translates time; it does not trim media, implement repeated segments or apply non-unit playback rates. The reader does not apply rotation/display transforms. A sync-sample flag alone does not establish HLS segment independence.
Mp4Reader implements the optional IIndexedContainerReader contract. Packet
counts, timestamp-based sync candidates and packet reads by index preserve the
reader's sequential cursors. Candidate lookup uses the sample tables, including
composition offsets and the selected edit-list mapping, without reading encoded
media. A candidate is a container declaration; the decoder must validate that it
can restart there. Sample lookup follows the
QuickTime seeking procedure.
Mp4Reader.DisplayMatrix and Mp4TrackInfo.DisplayMatrix preserve the movie and track header matrices. A video stream's EncodedStreamInfo.DisplayMatrix composes the track transform followed by the movie transform. VideoSession.Stream exposes this selected stream metadata; decoding leaves the raster unchanged. Matrix interpretation follows the QuickTime matrix definition.
Fragmented files, encrypted/external data, compressed QuickTime headers, multiple sample descriptions and other sample/handler types fail explicitly. The current sample entries include avc1/avc3, hvc1/hev1, av01, mp4v, ac-3/ec-3 and mp4a with AAC object type 2, 5 or 29, or MPEG-1/2 Layer III under ES object type indication 0x6B/0x69. Supported QuickTime audio entry versions preserve their version-specific declarations. Empty samples, sample sizes above 2 GiB and time scales above Int32.MaxValue are unsupported. Metadata, codec configuration, boxes, tracks, samples and edits have configurable limits through Mp4ReadOptions.
Movie storage, header scratch space and returned packet arrays share the supplied allocator budget. Immutable stream configuration snapshots and managed object overhead are outside that byte-array accounting; their size/count has separate limits. Disposing the reader releases its movie storage; retained packets remain valid.
Decoding H.264
Reference Mediana.Codecs.H264 and Mediana.Containers.Mp4. Register both modules explicitly:
MediaEngine engine = new MediaEngineBuilder()
.AddContainer(new Mp4ReaderFactory())
.AddVideoDecoder(new H264DecoderFactory())
.Build();
await using RandomAccessStreamByteSource source = new(File.OpenRead(path));
await using VideoSession session = await engine.OpenVideoAsync(source, streamIndex: 0);
MediaTimestamp target = new(1, new TimeBase(1, 1));
await session.SeekAsync(target);
using VideoFrame? frame = await session.ReadFrameAtOrAfterAsync(target);
The namespaces are Mediana, Mediana.Codecs.H264, Mediana.Containers.Mp4, Mediana.Core.Timing, Mediana.Core.Video and Mediana.IO. The example selects the first frame at or after one second in the original media timeline. It restarts at a validated IDR access point and decodes forward from there, including reordered pictures. Null means the stream has ended.
H264RandomAccessInspector examines AVC packets without reconstructing pictures.
IsIdr(packet) ignores container key-frame flags and validates access-unit boundaries
and all supported IDR slice headers, including their parameter references and picture
identity. It requires a progressive 8-bit 4:2:0 IDR picture whose first slice starts at
macroblock zero and whose remaining slices proceed in increasing order. It does not
validate residual data or prove complete macroblock coverage. Full non-IDR slice
headers, including B pictures, are not parsed by this
inspection operation. Packet inspection uses the original configuration and any
parameter sets carried within that packet, without retaining state between calls.
The decoder accepts AVC length-prefixed packets with one complete
picture and an initial avcC configuration. Supported pictures are progressive
8-bit YUV 4:2:0 CAVLC or CABAC I/P/B with default scaling, including 4x4/8x8
prediction and transforms.
Decoding begins with an IDR; sequence changes through in-band SPS/PPS also
require an IDR. Each output carries
its own format, color information, sample aspect ratio, presentation timestamp and
duration. Cropping is represented by visible plane offsets and row widths over
immutable coded YUV storage; callers use GetRow or the declared plane layouts.
Finished picture pixels are shared with independently owned output frames without
a second pixel allocation. Rotation, edit lists and display aspect
transforms are not applied. Missing packet timestamps remain absent; unspecified
sample aspect ratio and CICP color codes remain unspecified.
See decoded H.264 storage for ownership and resource checks. Identical direct-prediction motion is processed in larger regions; see H.264 direct prediction for exact-output checks and complete HD measurements.
MP4/MOV colr descriptions are retained separately from codec labels.
VideoFormat.GetColorInterpretation() resolves source precedence; default sRGB
conversion, previews and H.264 encoding use it. nclx declares range, while
QuickTime nclc preserves the codec range. Probe displays container CICP codes or
ICC profile declarations alongside original codec metadata. ICC transforms and
HDR tone mapping are processing operations with explicit source interpretation. See
source color contracts.
AVC stream copy to MPEG-TS/HLS writes source CICP colors into configuration and
known in-band SPS parameters, preserving compressed pictures and other header
syntax. ICC-only or unrepresentable descriptions fail before transport output.
Picture order types 0, 1 and 2, bounded delayed output, IDR/MMCO resets, input completion, cancellation and buffer ownership have executable checks. SEI message boundaries are validated; payload semantics are not interpreted. Slices have independent prediction and entropy state; deblocking follows complete picture reconstruction, honouring each slice's controls and reference identities. Missing or overlapping macroblock ranges and conflicting picture identities or reference marking fail explicitly. Arbitrary slice order, slice groups, redundant slices, interlacing, custom scaling, higher bit depths and inferred references for frame-number gaps are unsupported. H264DecoderOptions bounds picture pixels, configuration bytes, parameter-set bytes, NAL count and reference operations. Returned frames remain valid after reset or disposal until their owners release them.
Complete HD verification and CPU/memory measurements cover a full original 720p movie, every decoded picture, MPEG-TS copying and VOD HLS.
SrgbConverter.Apply from Mediana.Processing converts supported YUV420P8 or straight
RGBA32 to straight sRGB RGBA32. It supports full/limited YCbCr range, five conventional
YCbCr matrices, all six progressive chroma locations and common D65 primaries.
Conversion uses linear-light primary transforms and an ideal BT.1886 reference
display for BT.709/SMPTE 240M signals; RGB excursions and the target sRGB gamut are
clipped. Alpha, time and sample aspect ratio are preserved. Default conversion rejects
HDR and unknown color interpretation. Explicit HdrToneMappingOptions enable PQ/HLG
conversion with Hable tone mapping; see HDR previews. An overload accepts an explicit source
color interpretation; values are not guessed from the frame size.
The first weighted reference picture matches independent sRGB RGBA within one 8-bit
code per channel. The numerical definitions follow ITU H.264 Annex E,
BT.1886 and
sRGB in CSS Color 4.
Creating a PNG preview
The sample produces a preview fitted within 150×150 pixels from a container image or the first video stream, opening the container and building its index once:
dotnet run --project samples/Mediana.Sample -c Release -- --preview tests/Mediana.Tests/Data/FrameRate/preview-hd.mp4 preview.png
It selects the midpoint of a declared duration longer than one second, otherwise
time zero. Target calculation uses integer clock ticks; SeekAsync restarts from a
validated access point and ReadFrameAtOrAfterAsync discards earlier pictures.
The selected frame remains owned by the caller after the decoding session closes.
Unknown duration selects time zero. The sample searches registered PNG/JPEG attachments
before picture tags, using signatures rather than names or MIME types. When no image
is recognized, it transfers the opened container to video decoding. Recognized image
decoding errors are returned without trying another image or a video frame.
SdrPreviewColorPolicy explicitly supplies missing SDR color labels and the sample
reports assumptions; declared color interpretation retains precedence. A known sample
aspect ratio and an orthogonal display transform remain required. A missing frame
fails explicitly. Target selection uses the declared duration without replacing it
with indexed coded length. Ownership and complete preview checks
cover both paths.
MP4 LinearMovie maps original media timestamps through the supported edit list;
it does not clamp the first remaining PTS to zero or clip encoded samples to the
edit interval. For an edit beginning between picture boundaries, a selected
picture can therefore start after time zero. The preview references
check this distinction separately from the selected picture's pixels.
With a supported decoded frame and an allocation budget:
VideoDisplayMatrix displayMatrix = session.Stream.DisplayMatrix
?? throw new NotSupportedException("A display interpretation is required.");
using VideoFrame preview = SrgbPreview.Fit(frame, displayMatrix, maximumWidth: 320, maximumHeight: 180, allocator);
using MediaBuffer png = PngEncoder.Encode(preview, allocator);
await output.WriteAsync(png.Memory, cancellationToken);
The additional namespaces are Mediana.Processing, Mediana.Codecs.Png and
Mediana.Core.Memory. Reference Mediana.Codecs.Png. The encoder returns owned PNG bytes and does not open files or streams.
SrgbPreview.Fit combines SDR conversion, aspect correction, orthogonal orientation and nearest-neighbor fitting. Its output matches SrgbConverter.Apply followed by DisplayResize.Fit; it converts only selected source pixels and allocates one preview frame instead of an intermediate full-resolution RGB frame. It requires known color and SAR, preserves time and straight alpha, and produces square-pixel sRGB RGBA32. The sample uses this combined path. Separate color conversion and geometry operations remain available. The explicit-source-color overload accepts an application interpretation while preserving the original frame metadata.
The CLI preview commands use an explicit compatibility policy for missing SDR
color labels and log the chosen values once per configured source format. Explicit decoder labels take precedence. Missing primaries or matrix
first use the declared matrix or primaries when available; otherwise SD storage
up to 1024x576 uses BT.601 (SMPTE 170M at up to 480 lines, BT.470BG above 480),
and larger storage uses BT.709. Missing SDR transfer uses BT.709; straight RGB
uses sRGB defaults. Missing planar-YUV chroma location uses left siting with a
warning. These are application interpretations of missing labels. Declared range,
chroma location and sample aspect ratio retain their decoder values. Library
conversion and video preparation remain strict unless a caller selects a policy;
VideoPreparationOptions.ColorResolver uses the same mechanism for file and HLS
processing. Their CLI policy resolves missing YUV chroma siting while preserving
native colorimetry, including HDR labels. RGB-to-YUV conversion uses the SDR color
interpretation. Explicit SourceColorInterpretation takes precedence over a resolver.
CLI H.264 output explicitly selects YUV420P8 and warns once when source precision
is reduced. The library preserves source precision unless the caller selects a target format.
PQ/HLG previews still require tone mapping.
DisplayResize.Fit uses storage width times SAR and the storage height as the unscaled display size, fits within the requested box without enlargement, rounds to the nearest pixel and produces square pixels. It uses nearest-neighbor sampling, preserves color and time, and requires known SAR or an explicit caller interpretation. NearestNeighborResize.Apply instead resizes storage and preserves the original SAR. The display-matrix overload supports the eight exact right-angle rotations and reflections. It applies aspect correction before orientation and fits using the final axes in a single sampling pass. Canvas translation is normalized to the image's tight bounding rectangle; canvas padding is not added. Scaling, shear, perspective and approximate rotations are rejected. The overload without a matrix fits the stored raster without orientation. Neither operation maps edit lists.
Yuv420AreaResizer filters progressive, even-sized YUV420P8 video directly in its
three planes. Create selects exact smaller dimensions; CreateFit fits storage
into an even-sized box without enlargement. It adjusts known SAR to preserve exact
display aspect, and preserves unknown SAR, color, PTS and duration. Explicit chroma
location is required. All six positions follow H.264 Figure E-1; unspecified or
invalid positions fail rather than receiving an assumed interpretation.
using Yuv420AreaResizer resizer = Yuv420AreaResizer.CreateFit(frame.Format, 640, 360, allocator);
using VideoFrame smaller = resizer.Apply(frame, cancellationToken);
Reference Mediana.Processing. Reuse one plan for frames with the same dimensions, pixel format, SAR and color; calls and disposal must not overlap. The plan stores allocator-budgeted integer sample ranges and creates one owned output buffer per frame. Input row strides may include padding. Output survives plan/input disposal; cancellation and allocation failure leave the plan reusable. The area filter integrates stored sample values with repeated edges and one final integer rounding. It does not perform linear-light filtering, orientation, color conversion or upsampling. Six chroma-position cases and 30 complete decoded pictures match exact rational references.
PngEncoder accepts straight RGBA32 sRGB with known SAR. It writes non-interlaced 8-bit RGBA, an sRGB rendering intent and pHYs pixel dimensions, using BCL zlib compression and unfiltered scanlines. Alpha is preserved. PNG framing and CRCs follow the PNG specification. Encoding reserves a bounded output array; the returned buffer exposes its actual encoded length while the allocator counts the complete backing capacity. BCL compression workspace is outside that budget. Adaptive encoding filters, other input pixel layouts and other encoding color spaces are not implemented. Static PNG decoding supports standard color/depth combinations, transparency and Adam7, with explicit color metadata and bounded scanline storage; see the PNG API.
The generated previews from the first and fourth weighted reference pictures were read by an independent PNG decoder and compared with independent SDR-to-sRGB output. All samples differed by at most one 8-bit code. The fourth picture was selected at 300 ms for a request at 250 ms. Both previews preserve the source display ratio within integer-dimension rounding. The rotated MP4 also produces a 49×100 image whose orientation, color and aspect match independent inspection and decoded reference samples. Movie-to-media time mapping and broader display transforms remain open; timestamps used here refer to the original media timeline. The CABAC intra fixture also produces a 100×49 PNG from the picture at 600 ms for a request at 450 ms, with all samples within one code of an independent sRGB reference. All three CABAC P initialization fixtures select 500 ms for the same request and produce 100×49 PNGs matching independent sRGB references within one code per channel. The High CAVLC and CABAC 8x8 fixtures also select 500 ms and produce square-pixel 100×50 PNGs within one code of their independent sRGB references. Unit tests and Native AOT execute the same preview checks.
Encoding H.264
H264Encoder accepts progressive, even-sized 8-bit planar YUV 4:2:0 and
writes Constrained Baseline AVC with CAVLC or Main profile AVC with CABAC.
IDR groups use a media-time interval. P pictures can copy or predict from the
preceding reconstruction. With CABAC they can also use Intra16 prediction. Defaults are
QP26, a two-second IDR interval and a 16-pixel motion-search range. An explicit
zero interval selects all-IDR encoding. Input timing, SAR and source color are
preserved; output packet ownership and reconstructed storage are bounded.
Optional rate control adapts slice QP within a media-time packet budget.
Private reconstruction storage is reused between pictures and rate-control
attempts, and released on input completion, reset, failure or disposal. Retained
output packets remain independent. See the measured
reconstruction storage comparison.
mediana -i input.mp4 output.ts --video-codec H264 --video-only --qp 26
mediana -i input.mp4 output.ts --video-codec H264 --video-only --video-bit-rate 1500000
Optional --width/--height bounds apply YUV area filtering before encoding;
HLS variants share video decoding and align IDRs with the
requested segment duration. See H.264 contracts, algorithms and verification
for the public API, complete independent I/P and adaptive-rate corpora, original
all-IDR corpus and performance measurements. Full HRD conformance remains open.
Reading MPEG-TS, MPEG-PS and ASF
Register MpegTsReaderFactory and MpegPsReaderFactory from
Mediana.Containers.MpegTs, and AsfReaderFactory from
Mediana.Containers.Asf with MediaEngineBuilder.AddContainer.
The factories recognize content on known-length random-access sources.
The MPEG readers expose elementary payload bytes in PES order with available PTS and DTS. PES boundaries can split encoded pictures or audio frames. The ASF reader rejoins split audio/video media objects and reports timestamps relative to the declared preroll. Reading packets uses their declared framing; codec decoding depends on the registered decoder's input contract.
Writing an MPEG-TS program
Reference the Mediana.Containers.MpegTs source project and configure the output with one to sixteen AVC/AAC/MP3 streams. The module depends on container/core contracts and does not require a video or audio decoder.
await using MpegTsWriter writer = MpegTsWriter.Create(output, reader.Streams, allocator);
while (await reader.ReadPacketAsync(cancellationToken) is MediaPacket packet)
{
using (packet)
{
await writer.WritePacketAsync(packet, cancellationToken);
}
}
await writer.CompleteAsync(cancellationToken);
The namespaces are Mediana.Containers.MpegTs and Mediana.Core. Input packets contain one complete access unit with explicit PTS and DTS. Decode timestamps must increase within each track and must not decrease across calls. The writer rescales to the 90 kHz clock with nearest-even rounding and adds the same configurable offset to all timestamps; the default is one second. Timestamps must leave a 100 ms lead for the program clock. Gaps above the configured limit (default sixty seconds) require a new writer.
AVC requires a length-prefixed configuration and an initial IDR access unit on each video track. The writer converts NAL lengths to Annex B delimiters, writes access-unit delimiters and repeats configured SPS/PPS before IDR pictures. Changing parameter sets, data partitioning and AVC extensions are unsupported. B pictures can be copied as compressed data. AAC requires AAC-LC, indexed sample rates, channel configurations 1 through 7 and 1024-sample frames; each raw access unit is wrapped in one ADTS frame. HE-AAC, program-config elements and other audio codecs are unsupported.
The output carries PAT/PMT, separate PES timestamps, continuity counters, adaptation stuffing and PCRs at intervals no greater than 40 ms. Writes work on a non-seekable Stream, borrow its lifetime and use the supplied allocator for transport/PES scratch storage and framed access units. CompleteAsync flushes without closing the destination. Any failed operation faults the writer and releases its buffers; unfinished or failed output must be discarded.
For a program with one audio track or exactly one AVC track with optional audio,
BeginSegmentAsync(nextOutput) flushes the previous borrowed stream and continues
in a different writable stream. The next packet must belong to the program's
clock track: the sole audio track, or the AVC track with an IDR access unit.
PAT/PMT begin the new file;
all PID counters, PCR state and timestamp offsets continue across the boundary.
No stream is closed. A pending empty segment cannot be completed or skipped.
Callers select boundaries and keep audio packets in global decode order.
CaptureSegmentState() records the current configuration, limits, clocks and
counters without I/O. After disposing the preceding writer, use
MpegTsWriter.ResumeSegment(output, state, allocator) to write the next segment
from that state. Its first packet follows the same clock-track and AVC IDR rules.
Snapshots are immutable,
retain no I/O resources or media buffers and can be reused for requests in any
order. Given the same packets, resumed segments match continuous output byte for
byte. A snapshot does not select packet ranges or prove decoder independence.
Audio decoding can depend on preceding packets; see
audio transport segment contracts and verification.
The writer uses the caller's timeline. The MP4 reader defaults to original media timestamps; select LinearMovie for the supported edit-list translation before stream copy. Audio priming/discard metadata and container display matrices are not represented in TS. This operation does not yet define HLS segment boundaries, seek or playlists.
MediaEngine.DecodeImageAsync(source, allocator, request) decodes one complete
registered image or document page into the same owned VideoFrame used by video
processing. Register HeicImageDecoder, Jpeg2000ImageDecoder,
Jbig2ImageDecoder, SvgImageDecoder, TgsImageDecoder and PdfImageDecoder
explicitly as needed; no format modules are loaded by discovery.
MediaEngine engine = new MediaEngineBuilder()
.AddImageDecoder(new Jpeg2000ImageDecoder(SampleAspectRatio.Square))
.AddImageDecoder(new HeicImageDecoder(SampleAspectRatio.Square))
.AddImageDecoder(new Jbig2ImageDecoder(SampleAspectRatio.Square))
.AddImageDecoder(new SvgImageDecoder())
.AddImageDecoder(new TgsImageDecoder())
.AddImageDecoder(new PdfImageDecoder())
.Build();
MediaBufferAllocator allocator = new(64 * 1024 * 1024);
await using RandomAccessStreamByteSource source = new(File.OpenRead("input.jp2"));
using VideoFrame frame = await engine.DecodeImageAsync(source, allocator);
Raster decoder registration requires an explicit pixel-aspect interpretation. HEIF primary-image transforms are already applied; ICC conversion remains unsupported. JP2 uses its supported container color space; raw J2K requires a known source color space supplied to the decoder. Native component APIs retain their higher precision. The generic result does not infer unknown color metadata or convert every input to sRGB. Existing processing operations consume this frame.
ImageDecodeRequest.PageIndex is zero-based; JBIG2 selects by document page order,
not the page association identifier. Nonzero page/time selection is rejected when
the decoder does not declare that capability. Recognized malformed input fails in
the selected decoder; ambiguous recognition is rejected without trying another
implementation. ImageProbeLength and RecognizeImage(prefix) expose the same
registry for bounded routing; recognition does not validate the complete input.
The CLI uses these registrations for PNG and WebP image output, including
renamed files. A recognized image failure propagates from that decoder.
SVG XML bytes retain their BOM and encoding declaration; external entities and
implicit external resources remain disabled. TGS/Lottie uses elapsed time from
its in-point, returns the requested timestamp and the selected fractional-frame
duration at TimeSpan resolution. PDF uses zero-based PageIndex, defaults to
96 DPI and keeps its page transforms and rendering limits. CLI --page remains
one-based and --time-ms selects animation time. A container stream selector is
not applicable to images or pages. Format-specific scene/document APIs remain
available for callers that need them.
The caller owns the source and returned frame. Random access starts at source
origin while preserving its sequential cursor; sequential probing is replayed and
consumes input even on failure. ImageOpenOptions bounds encoded bytes and probe
size. The supplied allocator counts probe, encoded input, growth buffers and frame
leases. Unknown-length inputs start with up to 8 KiB of encoded capacity and grow
within the byte limit; capacity slack stays counted. Codec workspace and owned
document/component arrays have their module-specific limits. The decoded frame
retains no source or input-buffer lease and survives source disposal. Shared
registries and allocators support independent concurrent requests.
The registered raster path matches independent references for 42 HEIC, 98 JBIG2 and 24 JP2 inputs, including transformed/alpha HEIC, 16-bit component conversion and a two-page JBIG2 selection check. These checks run in managed .NET and Linux Native AOT. The registered vector/document path also checks 352 SVG filter references, 33 supported TGS frames and independent PDF render references, plus second-page selection. These checks cover the supported decoder subsets and retain the existing reference tolerances; unsupported features still fail.
MediaEngine.ProbeAsync(source) returns container identity, nullable declared
duration and an immutable snapshot of encoded streams through the same registered
readers as decoding. It needs no decoder registrations, does not iterate packets,
and releases reader buffers before returning. Random access preserves the source
cursor; sequential input consumes its opening headers. The source remains owned
by the caller. Existing probe and buffer limits apply.
mediana probe input.bin
Content selects the reader, including for renamed files. WAVE/AIFF frame counts, FLAC STREAMINFO, MP3 Xing/Info frame counts and MP4 movie headers supply declared duration in its original time base. For a seekable MP3 without a frame count, probe uses the complete chain of frame headers. Other missing durations stay unknown. MP3 duration includes coded delay and padding, and MP4 movie duration need not equal the last presented timestamp. Headers and decoder configuration are inspected; this is not a complete file validation, a packet index export or a cover-art API. The existing format-specific probe commands provide their additional diagnostics.
MediaInfo.Metadata provides a common immutable tree of namespaced, typed
properties for containers, images and documents. Child scopes retain pages,
components, encoding configurations and Exif directories. New native properties
do not require new fields in the shared model. The CLI prints this tree through
one formatter and reports encoded size when the source supplies it. HEIF exposes
brands, coded configurations, grids, transformations and color declarations;
JPEG and PNG use the same Exif reader. PDF exposes each page's dimensions and
rotation, SVG its root attributes, and TGS/Lottie its composition timing.
ContainerInfo.Tags exposes immutable header text without changing native keys.
ID3v2 frames use namespace id3v2; FLAC Vorbis comments use vorbis; MP4 text
items keep their native mdir, mdta or freeform namespace. Repeated names,
value groups, file order, original spelling, custom descriptions and opaque MP4
locale IDs are preserved. Application naming, case matching and normalization
remain with the consumer. null means the reader does not expose header text
(currently WAVE/AIFF); an empty list means none was found in the inspected
supported structures. MP3 trailing ID3v1, binary MP4 track/disc values, cover bytes
and unimplemented tag structures are outside this text view. Format-specific APIs
retain their additional metadata and binary access.
The generic CLI probe prints these tags as quoted, escaped text; each value group is a JSON array. Embedded newlines and terminal controls cannot create extra output lines. Reading tags reuses the opening parse and does not scan audio or video packets. Container metadata byte/count limits still apply. Native MP3/FLAC/MP4 readers build this common snapshot once, at the first metadata access; plain packet copying and decoding do not project unused text tags. Delayed access still uses opening header declarations, including after EOF and reader disposal, and retains no media-buffer lease.
Register IStreamMetadataReader modules with
MediaEngineBuilder.AddStreamMetadataReader to inspect codec configuration during
ProbeAsync. The immutable MediaInfo.StreamMetadata dictionary uses actual
stream indices and contains typed AudioStreamMetadata and VideoStreamMetadata.
Audio metadata reports sample rate, channel count, nullable sample precision and
nullable explicit channel order. Encoded
Streams retain their original configuration. There is at most one metadata
reader per codec identifier; missing entries mean no reader was registered or
its configuration is unsupported. Malformed configuration throws, and media-kind
mismatches are rejected. Readers perform no I/O, create no decoder and must support
concurrent independent probes.
PcmStreamMetadataReader, AiffPcmStreamMetadataReader,
FlacStreamMetadataReader, Mp3StreamMetadataReader and
AacStreamMetadataReader reuse the codec configuration parsers. PCM reports
valid sample precision separately from storage width and retains explicit channel
order. FLAC reports STREAMINFO precision without interpreting channel-layout
comment overrides. Lossy MP3/AAC precision and unestablished channel order remain
unknown. AAC-LC indexed multichannel headers can be inspected independently of
decoder support for the sample rate. Unsupported AAC profiles or PCM representations
remain unavailable; opening configuration is not proof of whole-file consistency.
The generic CLI probe registers these readers and prints known audio parameters.
Average VBR bit rate and stream-level tags are not yet exposed through this
metadata view. Audio header checks cover 30 real WAVE, AIFF, FLAC,
MP3 and AAC inputs, including MP4 audio tracks and AAC 5.1.
These checks pass in managed .NET and Linux Native AOT.
H264StreamMetadataReader reads all SPS descriptions in the opening AVC
configuration, with bounded configuration/parameter-set bytes and no decoded
surface allocation. It exposes each SPS as H264SequenceMetadata through
VideoStreamMetadata.Configurations: visible cropped sample dimensions, chroma
sampling, luma/chroma precision, declared sample aspect ratio and colorimetry.
These dimensions do not apply pixel aspect ratio or container rotation. Native
SPS identifiers, profile/constraints/level, coded dimensions, crop offsets,
frame/field flags, raw color codes and nullable VUI clock declarations remain
available. Clock declarations are not an average frame rate. Missing syntax
uses H.264 inference rules; unknown color codes remain available in the native
properties even when the common color enum is unspecified.
No opening SPS is implicitly treated as active. IsProgressive is true when
frame_mbs_only_flag establishes progressive coding; false syntax permits both
frames and fields, so the common scan property stays unknown. A common chroma
location is assigned only for progressive 4:2:0 with matching top/bottom field
locations. Initial configurations do not describe every later in-band change.
The reader validates AVC configuration bounds and PPS identifiers/references,
without parsing complete PPS reconstruction syntax. Unsupported SPS profiles
return no metadata; malformed supported syntax throws.
The generic CLI probe prints all opening video descriptions through this reader.
Common CodecProfileInfo descriptions expose H.264 profile/level names per SPS
and AAC-LC profiles without decoder creation. Numeric H.264 declarations remain
available. See codec profile metadata for constraint
variants, level 1b and unknown-level behavior.
Metadata availability is independent of decoding support: independent header
checks cover eleven real AVC inputs, including 10-bit, 4:2:2, 4:4:4, interlaced,
8K, cropped and rotated files. They pass with a 32 KiB media-buffer budget in
managed .NET and Linux Native AOT without decoder registration. These checks do
not expand the decoder's supported pixel formats, resolution or field coding.
The CLI uses the same registered container probes as audio/video decoding.
MediaEngine.OpenContainerAsync opens a ContainerSession of encoded packets
without a decoder; the session borrows the source and owns its reader, replay
buffer and read budget. Adding a container registers its reader factory; the
command does not choose readers by filename or try parsers in sequence.
TS stream copy accepts AVC/AAC/MP3 tracks from supported MP4/MOV files and native
MPEG-1/2 Layer III files, including renamed inputs. It preserves encoded access
units, applies LinearMovie timestamps to MP4 and adds the writer's one-second
offset. The root command copies all tracks by default; --stream selects one:
dotnet run --project tools/Mediana.Cli -c Release -- \
remux input.mp4 output.ts
mediana -i input.mp3 output.ts
mediana -i input.mp4 audio.ts --stream 1
The sample requires an initial IDR on each video track and timestamps that leave
the required program-clock lead after the offset. It preserves priming, padding
and samples outside the edit interval, so it does not perform sample-accurate
trimming. Replacing an existing output requires confirmation or explicit
--overwrite / -y. A failed conversion removes its temporary file and preserves
the previous destination. The same command works with
the published Native AOT sample executable.
Tests remux three real MP4/MOV inputs, including AVC with reordered timestamps, AAC and a High CABAC video-only stream. They compare every compressed slice and AAC access unit, shifted PTS/DTS in both timeline modes, program tables, PCR cadence, counters and buffer release in managed execution and Native AOT. Exact byte vectors also cover framing, CRC, timestamp markers and packet-boundary stuffing. The transport syntax follows ITU-T H.222.0.
Planning AVC video windows for HLS
Reference the Mediana.Streaming.Hls source project and open the MP4 reader in
LinearMovie mode. Mp4AvcSegmentPlan.CreateAsync(reader, videoStreamIndex, options)
uses metadata for the complete video timeline and reads payloads only for the
first sample and container sync candidates. Candidate IDR headers are inspected;
ordinary I pictures or false sync flags do not create boundaries. The first
sample must be an IDR. Each subsequent boundary is the first eligible IDR at or
after TargetDuration from the current segment start, rounded up to a video tick.
Each immutable entry records a zero-based first sample/count, presentation start, video duration and an exclusive decode window. Decode order remains distinct from presentation order. The final duration follows the last video presentation end. Nonpositive sample durations, decreasing DTS, leading pictures or presentation overlap at a chosen IDR boundary fail explicitly. MaximumSegments bounds the plan; the reader allocator bounds candidate packet storage. Planning and random boundary reads preserve sequential reader positions.
dotnet run --project tools/Mediana.Cli -c Release -- \
hls plan input.mp4 --video-stream 0 --segment-ms 2250
The 7.2-second closed-GOP AVC/AAC fixture produces video windows starting at 0, 3 and 6 seconds with durations 3, 3 and 1.2 seconds. Managed and Native AOT checks verify exact rational times, sample windows and reverse boundary reads. Transport tests also split the fixture into three files with one writer, retaining all 36 AVC and 339 AAC packets. Every file and their concatenation pass packet, timestamp, table, clock and continuity checks in managed execution and Native AOT. The video plan does not include audio or transport state. The session below binds them to one source. Neither API declares INDEPENDENT-SEGMENTS. Segment requirements follow RFC 8216.
Serving MP4 as HLS without transcoding
The experimental Mediana.Streaming.Hls source module provides Mp4HlsSession for
one supported AVC video track and an optional AAC-LC or MPEG-1/2 Layer III track. Open it on an immutable
IRandomAccessByteSource, selecting stream indices explicitly:
await using Mp4HlsSession session = await Mp4HlsSession.OpenAsync(source, allocator,
videoStreamIndex: 0, audioStreamIndex: 1, cancellationToken: cancellationToken);
string playlist = session.CreateVodPlaylist(number => $"segment-{number}.ts");
await session.WriteSegmentAsync(2, output, cancellationToken);
The session owns its MP4 reader and borrows the source. Opening inspects candidate IDR headers and scans the selected compressed packets once, building audio sample ranges and immutable transport snapshots. Media payloads and encoded segments are not retained. MaximumSegments bounds index entries; MP4 and transport options bound metadata, configuration and packet sizes. Unselected tracks are not copied.
Requests may arrive in any order and repeat. Each reads only its indexed sample ranges, preserving continuous PID counters, clocks and PTS/DTS across the sequence. Video wins equal decode-time ties. Audio packets before the next video's decode start belong to the preceding segment. With B pictures this can place audio before the first video presentation timestamp in a file. AAC requires PTS equal to DTS, positive sample durations and increasing decode timestamps.
CreateVodPlaylist returns LF-delimited text for UTF-8 encoding, version 3, a zero media sequence, VOD and ENDLIST. EXTINF follows actual video boundaries on the 90 kHz transport clock, including any declared audio tail in the last segment; TARGETDURATION is the ceiling of the largest interval. Segment URIs are supplied by the application and must be bounded, well-formed URI lines. No variant metadata or independent-audio claim is inferred.
One session permits one I/O operation at a time. The caller keeps the source unchanged and alive, manages concurrent sessions and owns every destination. WriteSegmentAsync flushes without closing it. On failure or cancellation discard the partial destination; the same segment can be retried using the immutable index. Disposal releases reader metadata. Segment metadata and playlist formatting remain available afterward.
This copies compressed streams. It does not encode, resize, normalize display orientation, recover damaged pictures or trim MP4 edit windows. AAC priming and padding are preserved; MP4 discard metadata and a shortened last AAC sample duration are not represented in ADTS. Audio-independent decoding, additional codecs and multiple renditions remain outside this path. Existing AVC inspection and MPEG-TS configuration limits apply.
MP3 frames are copied byte-for-byte with MPEG-1/2 transport stream types
0x03/0x04. Each packet must contain one complete frame with stable sample rate,
version and channel count; VBR and frame padding are preserved. MPEG-2.5 transport
is unsupported. Audio reservoir dependencies and synthesis history remain intact,
so starting a segment alone can lose leading audio; the playlist does not declare
EXT-X-INDEPENDENT-SEGMENTS or add preroll. This path supplies continuous stream
copy and does not promise sample-exact independent audio starts.
MP3-in-MP4 references cover independent packet hashes/timestamps, both MPEG versions, TS program tables and repeatable HLS segments in managed and Linux Native AOT execution.
Managed and Native AOT checks cover the real 36-picture/339-AAC fixture, exact sample windows, all compressed packets, playlists, transport continuity and repeated requests in order 2, 0, 1, 2. Error tests cover short range reads, cancellation, partial output, flush failures, invalid requests and source ownership.
The sample exports the indexed session into a new directory:
dotnet run --project tools/Mediana.Cli -c Release -- \
hls create input.mp4 output-hls --video-stream 0 --audio-stream 1 --segment-ms 6000
Omit --audio-stream for video-only output. The directory's parent
must exist. All files are prepared in a temporary sibling directory, then moved
into place together; an existing destination is preserved and failures remove the
temporary output. The same command works with the Native AOT sample executable.
An independent decoder reads the resulting playlist with all 36 video and 339 AAC
frame hashes matching the source. Each TS file also decodes its 15/15/6 video frames
independently. Managed and Native AOT exports produce identical playlist and segment
bytes; existing-output and failed-export checks preserve destination ownership.
Encoding HLS video variants
mediana hls encode input.mp4 output-hls --video-only --segment-ms 6000 --qp 26 --widths 1280 640
mediana hls encode input.mp4 output-av --audio-stream 1 --segment-ms 6000 --qp 26 --widths 1280 640
mediana hls encode input.mp4 output-aac --audio-stream 1 --audio-codec Aac --audio-bit-rate 128000 --widths 1280 640
mediana hls encode input.mp4 output-stereo --audio-stream 1 --audio-codec Aac --audio-channels 2 --widths 1280 640
mediana hls encode input.mp4 output-separate --audio-stream 1 --audio-codec Aac --separate-audio --widths 1280 640
The command decodes supported MP4/MOV AVC video once and produces one smaller
YUV420P8/H.264 sequence per width. Width bounds round down to even storage sizes
without enlargement; bounds that produce identical dimensions fail. --widths
uses space-separated integers and must be the final option. Audio omission is
explicit through --video-only; alternatively, --audio-stream copies one selected
AAC-LC or MPEG-1/2 Layer III track to every variant. Adding --audio-codec Aac
decodes that track and uses the experimental AAC encoder instead. Its optional
--audio-bit-rate ceiling defaults to 128000 raw bits/second for every variant;
alternatively, --audio-bit-rates 96000,128000,192000 assigns one raw ceiling per
width in the same order. The two bitrate options are mutually exclusive. Equal
settings share an encoder; distinct settings encode the same decoded PCM through
independent AAC states. Rate and channel order are preserved unless an explicit
downmix is selected. The command requires an identity display
transform.
--separate-audio writes audio-only playlists and video-only variants. Equal audio
settings share one continuous audio rendition, referenced by each matching video
variant. Distinct AAC bitrates have separate audio groups. Completed media
playlists use the same measured target duration and corresponding bitrate window.
Each variant contains IDR-aligned TS segments and a VOD
media playlist. The master playlist records measured peak/average transport bitrates
and the video/audio codec identifiers. Only video-only output declares independent
segments. Copy mode preserves original audio bytes and linear-movie timing.
AAC encoding applies the exact input MP4 sample edit, preserves its movie origin
and writes each selected AAC sequence with its own priming and final tail.
Per-variant audio verification covers the shared input,
exact packet budgets, complete decoding and master playlist measurements.
Complete 720p/360p HLS verification records the full
original film, CPU/RSS comparison with FFmpeg and every output picture and segment.
Segment timelines must match across variants. Output
is prepared in one temporary directory and published together after completion;
errors or cancellation remove it. The library also exposes AvcHlsWriter for
streaming encoded packets without a source-container dependency.
Peak bandwidth normally follows the contiguous-segment window in
RFC 8216 section 4.1.
For a presentation shorter than half TARGETDURATION, that window is undefined.
The CLI estimates BANDWIDTH from the largest complete-segment bitrate and logs
a warning; average bandwidth still uses the whole presentation. Library clients
can select this behavior through HlsBandwidthOptions.PeakMode on
HlsWriteOptions or HlsRenditionOptions. Their default requires an eligible RFC window.
The current mode uses I/P video with IDRs aligned to segment targets. Optional per-variant video packet rates use adaptive QP; otherwise QP is fixed. Full HRD conformance and perceptual AAC quality remain open. See HLS encoding contracts and checks.
Preparing continuous audio HLS
The library's AudioHlsWriter partitions one AAC-LC or MPEG-1/2 Layer III packet
sequence into TS files and a VOD media playlist. Applications can copy audio or
keep one AAC encoder running across all segments. Boundaries follow a fixed grid
from the first coded PTS; final segment duration includes the complete coded tail.
HlsWriteOptions supplies the target duration and output limits. Destination
ownership and transport continuation use the same component as AvcHlsWriter.
Independent decoding of complete playlists matches continuous reference PCM byte
for byte, including a 12-second stereo AAC preparation from 5.1 input.
CreateRendition supplies codec, channel and measured bitrate metadata for
HlsMasterPlaylist audio groups. PlaylistTargetDurationSeconds selects a common
advertised target for audio and video and their bandwidth measurements. A real
prepared presentation passes complete
master-playlist decoding with video requests in order 1, 0, 1. Chrome/hls.js
completes playback and buffered seeks; exact VOD presentation boundaries and
smooth rendering remain open.
See audio HLS contracts and checks.
See audio group contracts and checks.
Connecting modules
Applications supply container and decoder factories; none are registered implicitly.
Factories declare stable implementation identifiers, and decoder factories also
declare the codec identifier and validate a specific stream configuration.
Factories must have immutable descriptors and support independent session creation.
Decoder factories may implement IRandomAccessDecoderFactory to provide restart
inspection; indexed containers implement IIndexedContainerReader independently.
Duplicate implementation identifiers are rejected when registering. If two factories match the same input/configuration, opening fails explicitly. There is no arbitrary selection based on registration order.
With application-provided containerFactory and decoderFactory:
MediaEngine engine = new MediaEngineBuilder()
.AddContainer(containerFactory)
.AddVideoDecoder(decoderFactory)
.Build();
await using StreamByteSource source = new(File.OpenRead(path));
await using VideoSession session = await engine.OpenVideoAsync(
source,
streamIndex: 0,
options: new MediaOpenOptions { BufferBudgetBytes = 64 * 1024 * 1024 },
cancellationToken: cancellationToken);
using VideoFrame? frame = await session.ReadFrameAtOrAfterAsync(
new MediaTimestamp(5, new TimeBase(1, 1)),
cancellationToken);
The example requires namespaces Mediana, Mediana.IO, Mediana.Core.Timing
and Mediana.Core.Video. The caller chooses the actual video stream index.
The timestamp is in the stream's presentation timeline, not a normalized movie timeline.
ReadFrameAtOrAfterAsync scans remaining decoded frames and returns the first
presentation timestamp at or after the target. It does not seek, return the nearest
frame, infer missing timestamps or replace an out-of-range request with an earlier
frame. End of stream returns null. A frame without a timestamp causes a clear error.
VideoSession.CanSeek reports whether the container supplies indexed packets and
the decoder supplies restart inspection. SeekAsync(target) restarts at a
codec-validated candidate at or before the target, or at the first packet when
the target precedes it. Follow it with ReadFrameAtOrAfterAsync(target) to discard
preroll. Seeking can move backward and restart after EOF; retained frames remain
valid. The MP4/MOV and H.264 combination supports this operation. A container
sync flag alone cannot authorize decoder restart. Unsupported seeking fails
explicitly. Cancellation or seek failure makes the session terminal; dispose it.
Packet and frame budgets remain cumulative across seeks.
Opening borrows the caller's source for the session lifetime. Sequential sources are consumed even if opening fails. Random access sources open at their origin and preserve the caller's sequential cursor. A session owns its reader, decoder and input cursor or probe replay state. The source is disposed separately by its caller. Session operations and disposal must not overlap. Frames already returned remain valid after the session is disposed.
Random access sources
RandomAccessStreamByteSource requires a readable, seekable Stream. Its ReadAtAsync and ReadExactlyAtAsync operations use offsets relative to the stream's initial position and preserve its sequential cursor, including after a failed read. Reads at or beyond the captured length return EOF; an exact read throws if the requested range is truncated. The underlying stream must remain exclusively owned by the source while it is in use. Reads and disposal must not overlap.
Random access to bytes alone does not provide video seeking. The selected container and decoder must also support indexing and restart inspection.
RandomAccessCursorByteSource provides an independent sequential cursor over a random access source, while preserving range reads and the source's own cursor.
Buffer ownership
MediaBufferAllocator limits the total byte-array capacity currently held by its
leases. Allocation fails immediately if the requested storage exceeds that budget.
Buffers are zero-initialized. There is currently no retained buffer pool.
MediaBufferAllocator allocator = new(1024);
using WritableMediaBuffer writable = allocator.Allocate(16);
writable.Memory.Span.Fill(255);
using MediaBuffer pixels = writable.Publish();
VideoFormat format = new(2, 2, VideoPixelFormat.Rgba32);
using VideoFrame frame = new(format, pixels, format.CreateTightLayout());
using VideoFrame retained = frame.Retain();
The relevant namespaces are Mediana.Core.Memory and Mediana.Core.Video.
Publishing transfers ownership and invalidates the writable owner. Frames, packets
and audio frames retain their own read-only lease. Retain shares storage without
copying; every lease must be disposed independently. Slice returns an independent
lease over a logical range; the allocator still counts the complete backing array.
Writable owners are single-consumer objects. Borrowed Memory or Span views must
not be used after publication/disposal or concurrently with disposal. A retained
read-only lease permits independent ownership, not mutation. The API cannot revoke
a view previously obtained by a caller.
The budget covers allocations made through that allocator. It does not cap process RSS, object metadata, stream buffers, third-party module allocations, or arrays awaiting garbage collection. Custom modules must use the supplied allocator for their media storage to participate in its accounting.
Time, frames and execution
TimeBasestores positive rational seconds per tick. Timestamps use signed 64-bit values and checked 128-bit intermediate arithmetic. Rescaling requires an explicit rounding rule. Unknown timestamps are nullable; default time bases are invalid.- Packets distinguish presentation and decode timestamps. Decoders must produce frames in presentation order. Constant frame rate is not assumed.
- Video supports custom byte-addressable plane descriptions, positive strides, and explicit color metadata. RGBA32 and planar YUV420P8 layouts are predefined. Resize currently accepts the predefined RGBA32 layout only and preserves color metadata without performing color conversion.
- Audio storage supports explicit channel order, native-endian PCM samples, planar/interleaved layouts and exact sample-count duration. WAVE PCM decoding, explicit channel matrices, storage conversion and streaming sinc resampling are available, including limited AAC-LC decoding. Broader compressed audio, inferred delay/padding correction and playback remain open. Explicit sample trimming and selection from supported MP4 edit lists are available.
- A codec can accept several packets before output, or produce several frames from one packet. Completing input drains delayed output; resetting discards it. Input is borrowed for a call, retained input needs a separate lease, and output belongs to its caller.
- Execution is sequential and demand-driven. It does not prefetch unbounded queues or launch worker pools. One codec instance is not called concurrently.
- Cancellation is cooperative. Cancellation or decode failure makes the session terminal and releases decoder state; dispose the session to release reader state.
MaxPacketReadscounts all container reads, including seek candidate inspection, skipped streams and EOF checks.MaxFrameReadscounts session reads, including EOF checks and frames consumed while selecting a timestamp. Both are optional positive work budgets; null, the default, permits complete media of any duration. Memory and format validation limits remain active. Work budgets are not timeouts.
No source or module is selected through reflection or runtime assembly discovery.
Libraries enable AOT/trimming analysis, and compiler/analyzer warnings fail the build.
Repository builds default to OptimizationPreference=Speed for Native AOT.
Applications consuming NuGet packages select this property in their own publish
project. It changes compiler optimization preferences and preserves the default
processor instruction-set requirement. See the complete native stage comparison.
Validation and current boundaries
The automated tests exercise signed time conversion, overflow, shared-buffer ownership, concurrent releases, allocation limits, video strides and subsampling, PCM layouts, non-seekable short reads, probe replay, registration conflicts, unsupported configurations, delayed and multiple codec outputs, EOF, cancellation during I/O, invalid codec states and cleanup. Real MP4/MOV fixtures compare every packet's data hash, size, timestamps, duration and sync flag with independent reference results. Corrupted tables, truncated input and resource limits are checked.
The Native AOT sample has been built and executed locally on Linux x64. The workflow defines build, formatting, tests and native publication/execution on Linux, Windows and macOS. Other platform results depend on those CI runs. The native sample covers the generated-frame pipeline, reading all packets from the two original MP4/MOV fixtures, an MP4-to-PNG preview, WAVE/AAC/FLAC PCM export and resampling. The separate native tests also verify internal and public decoding of 85 CAVLC reference pictures and six CABAC intra pictures, 96 additional CABAC I/P pictures and 98 CABAC I/P/B pictures, SDR conversion, decoded PNG pixels, complete WAVE/AAC/FLAC references, FLAC waveform data and audio resampling. Each future format also requires an executable AOT integration test.
Current limitations include broader compressed codec support, other container formats, seeking for additional audio codecs, more general movie timeline mapping, arbitrary changes within a coded sequence, broader display transforms, broader color conversion, broader compressed audio, artwork in other containers, Cotton provider integration, representative HD encoding efficiency and HRD conformance, perceptual AAC quality, other muxers, scheduling, further CPU optimization and playback. The contracts are experimental and may change before 1.0.
Production format scope and performance targets remain open. Measurements and independent comparisons cover selected inputs and do not establish general performance or quality parity across media formats.
Packages
dotnet pack Mediana.slnx -c Release --no-build --no-restore -p:Version="$PACKAGE_VERSION" -o artifacts/packages
The library projects depend at runtime only on .NET and other Mediana projects.
Build analyzers and test tools are development dependencies.
Release packaging includes all 28 library projects in the component table.
Every module has its own package; application and test projects are excluded.
Installing Mediana brings in the facade's contract and execution dependencies.
Concrete containers, codecs, image/document adapters, drawing, processing and HLS
remain optional packages. All packages use the same computed release version.
The release checks restore the complete media test application from the newly built packages, reject unresolved or project-based dependencies, publish it with Native AOT and execute its reference tests. This checks package resources and transitive dependencies as well as compilation. Packages are published only by the release workflow described below.
Publishing
GitVersion computes the version from Git history and release tags using GitVersion.yml. All library packages use the same computed version for both compilation and packing. Ordinary releases advance the patch version; minor and major changes require an explicit versioning decision.
Pushes to main and pull requests run build, formatting, tests and Native AOT checks on Linux x64, Windows x64 and macOS ARM64. Successful builds upload standalone executables, optional archives and SHA-256 checksums as workflow artifacts.
After all checks pass, pushes to main and manual workflow runs from main publish the same validated packages to NuGet.org and GitHub Packages, and symbols to NuGet.org. Pull requests only validate. The GitHub release is created after both registries succeed. Its downloads contain the tested native executables, archives and checksums; NuGet packages are available from the registries.
The release action uses the push's final commit message as its description. The message should summarize the milestone's changes and relevant validation. No separate release-description generator or manually maintained changelog is required.
Authentication uses NuGet Trusted Publishing.
Set the NUGET_USER repository variable to the NuGet profile username. Configure
the matching NuGet policy for repository bvdcode/Mediana, workflow file
publish-release.yml, environment nuget, and package patterns Mediana and Mediana.*
with permission to push new packages and versions.
GitHub Packages uses the workflow's GITHUB_TOKEN with packages: write and the
feed https://nuget.pkg.github.com/bvdcode/index.json. Each package is linked to
this repository through its RepositoryUrl. GitHub package visibility must be
checked after the first publication; newly created packages default to private.
Re-running a partially completed publication skips existing package versions and GitHub releases. Package IDs become assigned after successful publication.
License
MIT.
Apply source license headers with dotnet run --file UpdateLicense.cs -- ..
Validate them without editing files with
dotnet run --file UpdateLicense.cs -- . --check; the build workflow runs this check.
The updater skips generated files and build-output directories.
PDF previews
dotnet run --project tools/Mediana.Cli -c Release -- pdf render report.pdf page.png --page 1 --width 1200
dotnet run --project tools/Mediana.Cli -c Release -- pdf image report.pdf image.png --page 1 --index 0
Pages start at one; decoded image indices start at zero. Output files must be new. Page previews use 96 DPI, reduce proportionally to the requested maximum width and apply a white background. Image extraction returns RGBA pixels, including masks, without the image's placement on the page. The supported PDF subset, limits, font behavior and known rendering gaps are described in docs/pdf.md.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Mediana.Documents (>= 0.1.52)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Mediana.Documents.Spreadsheets:
| Package | Downloads |
|---|---|
|
Mediana.Documents.Xls
Managed multimedia building blocks for .NET. |
|
|
Mediana.Documents.Xlsx
Managed multimedia building blocks for .NET. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.52 | 0 | 11/28/2026 |
| 0.1.51 | 12 | 11/27/2026 |
| 0.1.50 | 43 | 11/27/2026 |
| 0.1.49 | 48 | 11/27/2026 |
| 0.1.48 | 58 | 11/27/2026 |
| 0.1.47 | 52 | 11/27/2026 |
| 0.1.46 | 61 | 11/26/2026 |
| 0.1.45 | 81 | 11/26/2026 |
| 0.1.44 | 76 | 11/25/2026 |
| 0.1.43 | 80 | 11/25/2026 |
| 0.1.42 | 79 | 11/25/2026 |
| 0.1.41 | 74 | 11/25/2026 |
| 0.1.40 | 81 | 11/25/2026 |
| 0.1.39 | 79 | 11/25/2026 |
| 0.1.38 | 92 | 11/25/2026 |
| 0.1.37 | 102 | 11/23/2026 |
| 0.1.36 | 103 | 11/22/2026 |
| 0.1.35 | 106 | 11/22/2026 |
| 0.1.34 | 109 | 11/22/2026 |
| 0.1.33 | 110 | 11/21/2026 |
Changes and supported modules: https://github.com/bvdcode/Mediana/releases/tag/v0.1.52