Skip to content

实现 Dolby Vision RPU 映射并完善解码回退 - #2

Closed
1824239290 wants to merge 16 commits into
mainfrom
agent/dolby-vision-mapping
Closed

1824239290 wants to merge 16 commits into
mainfrom
agent/dolby-vision-mapping

Conversation

@1824239290

@1824239290 1824239290 commented Sep 3, 2026 •

Copy link
Copy Markdown
Owner

概要

为 Erika 增加基于 FFmpeg RPU 元数据的 Dolby Vision 映射路径,按 libplacebo 的处理顺序完成曲线重塑、矩阵变换、PQ 线性化和 HDR 输出,并在无法安全映射时回退到普通 base layer。

主要改动

  • 读取并校验 FFmpeg 的 Dolby Vision 配置和逐帧 RPU side data。
  • 支持 polynomial/MMR 曲线、矩阵、偏移、PQ 峰值和 HDR mastering luminance。
  • 统一 WGSL、Metal、HLSL 的 DOVI 条件,避免重复 LMS 变换。
  • 修正 P010/NV12 的 offset、采样比例和 RGB 预转换路径。
  • Dolby Vision Profile 5 在 VideoToolbox、D3D11VA、MediaCodec、AvCodec 请求下回退软件解码。
  • 拒绝 FEL/residual、非平凡 NLQ、非法曲线参数和奇异矩阵,避免输出静默失真。
  • 增加 D3D11 软件 NV12/P010 上传路径、单元测试和 Dolby Vision 文档。

解码与兼容性策略

  • Profile 5:强制软件解码,以保留 RPU。
  • Profile 8:保留硬件解码;若 RPU 不可用则使用兼容的 base layer。
  • 当前不合成 FEL/residual enhancement layer;遇到不支持的 RPU 结构时回退普通 base layer。

验证

  • GitHub Actions run 33741707420 全部通过:Ubuntu、macOS arm64/x64、Windows 双架构、iOS、tvOS。
  • Rustfmt、许可证策略、Cargo patch 校验、核心测试、C API 测试和 Clippy 均通过。
  • 使用最新 macOS arm64 artifact 在 XXPlayer 中完成 ErikaKit ABI 测试(14 项通过)、OcPlayer-macOS XCTest(101 项通过)和 arm64 App 编译。

测试边界

自动化测试覆盖元数据解析、映射数学、解码回退、ABI 和宿主编译;本 PR 尚未在 CI 中执行真实 Dolby Vision 样片的人工播放验收。

jumusu and others added 16 commits September 3, 2026 02:52
Implements Dolby Vision profile 5/8 rendering for software-decoded HEVC,
following mpv's approach (libplacebo pl_shader_dovi_reshape):

- erika_ffmpeg_sys: expose libavutil/dovi_meta.h to bindgen.
- ffmpeg.rs: convert the decoder's per-frame AV_FRAME_DATA_DOVI_METADATA
  into shader-ready floats (pivots normalized by bl_bit_depth, curve
  coefficients by 2^-coef_log2_denom, RPU ycc_to_rgb/rgb_to_lms matrices).
- pipeline.rs: SourceColorState carries per-frame DoviSourceMetadata
  (peak from source_max_pq, primaries forced to BT.2020); DoviUniforms
  packs the reshaping curves into vec4-aligned uniforms, premultiplies
  the HPE LMS->RGB composite and folds the 1024/1023 signal-offset
  correction; render graph gains a DoviReshape pass.
- Shaders (WGSL/Metal/HLSL): per-component pivot/polynomial/MMR
  reshaping of the raw full-range base-layer signal, RPU matrix +
  offsets to PQ-encoded IPT, then LMS->RGB after PQ linearization;
  range expansion and the YCbCr matrix are bypassed for RPU frames.
- VideoFrameDescriptor carries dovi_metadata for hardware-frame paths.

FEL (profile 7 residual) is not composed; profiles 4/7 render the base
layer reshaping only, matching current mpv without a bound enhancement
layer. No C ABI changes.
The MMR extraction loop dereferenced the enumerate index instead of the
mutable coefficient slot (E0614 across all CI targets), and several
manually formatted blocks deviated from rustfmt. Nested call formatting
was also made deterministic where line width sat near the 100-column
limit.
WGSL if-expressions are not portable across validators; use a var plus a
plain if statement for the packed-luma sample.
- metal layout test expects the appended dovi uniform block (size 3104,
  dovi at offset 144)
- dovi LMS composite golden values recomputed from the RPU default
  matrix expressed in exact 1/16384 fractions
- remaining rustfmt deviations (assert wrapping, let-row split, blank
  line, copy_nonoverlapping join) applied verbatim from CI output
libplacebo's pl_map_avdovi_metadata unconditionally sets BT.2020/PQ from
the RPU because P5/P8 VUI tags are unreliable. Erika's dovi() builder
forced primaries and peak but left the transfer untouched, so streams
with an unspecified trc decoded the reshaped PQ signal through the sRGB
branch. Also sync reference_white_nits to the PQ reference white so the
linearization scale stays consistent.
Produces a runnable macOS binary from the fork CI so the Dolby Vision
color pipeline can be exercised against real content without a local
Rust toolchain.
Hardware decoders (VideoToolbox/D3D11VA/MediaCodec) cannot convey the
Dolby Vision RPU, so profile 5 streams rendered with the wrong colors
under the default hardware path. Read the container's DOVI configuration
record (AV_PKT_DATA_DOVI_CONF) at decoder-open time and switch to the
software decoder for profile 5, matching mpv's behavior; the decoder
event carries the requested backend plus a fallback reason. Profile 8
stays on hardware since its base layer is HDR10-compatible.

Also raise the wgpu_decode_png example's deployment target to 13.0 so
the VideoToolbox hwcontext symbols resolve at link time, and add
dolby_vision_profile() tests (fixture: None; ERIKA_DV_SAMPLE: Some(5)).
av_stream_get_side_data no longer exists in ffmpeg 8.x; the container's
DOVI configuration record lives in AVStream.coded_side_data and is read
through av_packet_side_data_get.
The example binary never calls the VideoToolbox hwcontext path that
references the version-check helper, so resolve it lazily like the
release dylib does instead of failing the executable link.
-Wl,-U only defers resolution and dyld then fails at launch; the
__isPlatformVersionAtLeast helper from FFmpeg's VideoToolbox hwcontext
must be statically provided by the compiler runtime instead.
- Add comprehensive top-level documentation (docs/dolby-vision.md)
  - Complete pipeline architecture explanation
  - Profile 5 vs 8 comparison and decode strategy
  - Reshaping algorithm details (polynomial and MMR)
  - Color transform flow and testing instructions

- Enhance inline documentation
  - Add field-level comments to DoviSourceMetadata
  - Clarify signal offset scaling formula (1024/1023 ratio)
  - Document uniform buffer size (~3KB, 20% of mobile limits)

- Expand test coverage
  - Add Profile 8 detection test (ERIKA_DV_PROFILE_8_SAMPLE)
  - Add 3 unit tests for dolby_vision_decode_fallback logic
  - Verify Profile 5 forces software decode
  - Verify Profile 8 stays on hardware decode

- Improve CI maintainability
  - Add comment explaining libclang_rt.osx linker workaround
  - Reference commits acae10f and 3498b8e for context

- Add validation comments for bit depth and coefficient ranges

These changes enhance documentation, testing, and maintainability
without modifying the core Dolby Vision mapping algorithm.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The tests were incorrectly expecting DecoderBackend::AvCodec instead of
DecoderBackend::Software. DecoderConfig::software() returns Software variant.

- dolby_vision_profile_5_falls_back_to_software_decode: expect Software
- dolby_vision_software_decode_stays_software: expect Software

This fixes the CI failure on macOS arm64.
@1824239290 1824239290 changed the title feat: dolby vision RPU mapping implementation 实现 Dolby Vision RPU 映射并完善解码回退 Sep 3, 2026
@1824239290

Copy link
Copy Markdown
Owner Author

该改动已转为向上游 AimesSoft/Erika 提交 PR AimesSoft#130,后续请以上游 PR 为准。

@1824239290 1824239290 closed this Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant