Skip to content

fix(android): keep bitrate-targeted exports at the requested bitrate - #16

Merged
thangdevalone merged 2 commits into
thangdevalone:mainfrom
FrikkieSnyman:fix/android-cbr-tonemap
Nov 19, 2026
Merged

thangdevalone merged 2 commits into
thangdevalone:mainfrom
FrikkieSnyman:fix/android-cbr-tonemap

Conversation

@FrikkieSnyman

Copy link
Copy Markdown
Contributor

What

On Android, exports with a target bitrate (compressVideo, and the edit path that passes 4 Mbps) now:

  • encode in CBR (BITRATE_MODE_CBR) instead of the default VBR;
  • tone-map HDR input to SDR (Composition.HDR_MODE_TONE_MAP_HDR_TO_SDR_USING_OPEN_GL, API 29+) instead of keeping HDR.

Exports without a target bitrate are unchanged. export() now starts a one-item Composition to set the HDR mode.

Why

compressVideo can produce a file many times larger than targetSizeInMB. Two separate causes:

  1. HDR input switches the encoder to HEVC. setVideoMimeType(MimeTypes.VIDEO_H264) can't carry HDR, so Media3 keeps HDR by moving to a 10-bit HEVC encoder. On a Pixel 7 Pro, c2.exynos.hevc.encoder ignores the requested bitrate: a 10 s 4K60 HLG clip comes out at ~49 Mbps whether 4.2 Mbps or 1 Mbps is requested.
  2. VBR lets Android raise the bitrate. In VBR mode, Android asks encoders that support it for a minimum quality (VQApply: minquality: codec claims to implement minquality=1 in logcat), which overrides low bitrate requests. Even after tone-mapping to H.264, the same 4K60 clip doesn't go below ~5.6 Mbps. CBR is outside that behaviour.

Testing

A standalone app built against Media3 1.10.1 ran this export() setup unchanged: H.264 requested, Presentation to 1292×726, 10 s clipped, audio removed. Only the bitrate mode and HDR mode varied. Device: Pixel 7 Pro, Android 17. Clips are synthetic and noisy, a worst case for encoders. Each cell shows the average video bitrate in Mbps for requests of 4.2 / 1.0 Mbps.

Source VBR, keep HDR (before) CBR, keep HDR VBR, tone-map CBR, tone-map (after)
4K60 HEVC HLG 49 / 49 4.2 / 7.6 5.6 / 5.6 4.1 / 1.75
1080p60 HEVC HLG 24 / 24 4.2 / 3.3 4.2 / 2.4 4.1 / 1.3
4K30 H.264 SDR 4.2 / 1.8 4.1 / 0.96 4.2 / 1.8 4.1 / 0.96

Tone-mapped output is H.264 High, 8-bit, BT.709, and the colours look right. The changed statements compile against Media3 1.10.1, and the APIs used are also in 1.5.1, the version android/build.gradle declares. This ships in Relive as a patch on 2.4.3.

🤖 Generated with Claude Code

FrikkieSnyman and others added 2 commits November 19, 2026 09:36
Encode in CBR and tone-map HDR input to SDR when a target bitrate is set.
In VBR, Android asks encoders for a minimum quality that overrides the
bitrate, and HDR input kept as HDR switches the requested H.264 to a
10-bit HEVC encoder that can ignore the bitrate altogether.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keeps PR thangdevalone#16's behaviour (CBR + HDR-to-SDR tone-mapping for bitrate-targeted
exports) on devices that support it, and stops it from failing exports on
devices that don't:

- Tone-mapping is tried with OpenGL (API 29+), then in the decoder (API 31+),
  then the pre-thangdevalone#16 behaviour (keep HDR, VBR) when the error is one a different
  HDR/bitrate mode can avoid.
- CBR is requested only when the encoder DefaultEncoderFactory would pick
  supports it, since Media3 drops every encoder without the requested mode.
- Whether HDR is tone-mapped is an explicit runTransform flag instead of being
  inferred from targetBitrate; compressVideo and processVideo set it, so their
  behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thangdevalone
thangdevalone merged commit 2d855c3 into thangdevalone:main Nov 19, 2026
3 checks passed
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.

2 participants