Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ dependencies {
implementation(libs.androidx.media3.ui)
implementation(libs.androidx.media3.ui.compose)
implementation(libs.ass.media)
implementation(libs.exoplayerhdrutils)

implementation(libs.coil.core)
implementation(libs.coil.compose)
Expand Down
3 changes: 3 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
-keep class androidx.media3.ui.SubtitlePainter {
private final float outlineWidth;
}

# Keep external/native functions
-keep class com.suyashbelekar.exoplayerhdrutils.libdovi.LibDovi { *; }
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class AppPreferencesSerializer
disableAudioOffload = ExperimentalPreference.DisableAudioOffload.defaultValue
preferDolbyVisionOverHdr10Plus =
ExperimentalPreference.PreferDolbyVisionOverHdr10Plus.defaultValue
convertDolbyVisionProfile7 =
ExperimentalPreference.ConvertDolbyVisionProfile7.defaultValue
}.build()
}.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ object ExperimentalPreference {
summary = R.string.prefer_dovi_over_hdr10_plus_summary,
)

val ConvertDolbyVisionProfile7 =
AppSwitchPreference<AppPreferences>(
title = R.string.convert_dovi_profile_7,
defaultValue = false,
getter = { it.experimentalPreferences.convertDolbyVisionProfile7 },
setter = { prefs, value ->
prefs.updateExperimentalPreferences { convertDolbyVisionProfile7 = value }
},
summary = R.string.convert_dovi_profile_7_summary,
)

val PreferAc3ForSurround =
AppSwitchPreference<AppPreferences>(
title = R.string.prefer_ac3_for_surround,
Expand Down Expand Up @@ -93,6 +104,7 @@ val experimentalPreferences =
listOf(
ExperimentalPreference.VideoTunneling,
ExperimentalPreference.PreferDolbyVisionOverHdr10Plus,
ExperimentalPreference.ConvertDolbyVisionProfile7,
ExperimentalPreference.PreferAc3ForSurround,
ExperimentalPreference.DisableAudioOffload,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import android.content.Context
import com.github.damontecres.wholphin.preferences.AppPreferences
import com.github.damontecres.wholphin.preferences.AssPlaybackMode
import com.github.damontecres.wholphin.preferences.ExperimentalPreferences
import com.github.damontecres.wholphin.preferences.HdrFormat
import com.github.damontecres.wholphin.preferences.HdrOverrideMode
import com.github.damontecres.wholphin.preferences.PlaybackOverrides
import com.github.damontecres.wholphin.preferences.enabled
import com.github.damontecres.wholphin.preferences.getHdrRangeTypesFor
import com.github.damontecres.wholphin.util.DolbyVisionProfile7Conversion
import com.github.damontecres.wholphin.util.WholphinDispatchers
import com.github.damontecres.wholphin.util.profile.MediaCodecCapabilitiesTest
import com.github.damontecres.wholphin.util.profile.createDeviceProfile
Expand Down Expand Up @@ -66,7 +68,17 @@ class DeviceProfileService
decodeAv1 = prefs.overrides.decodeAv1,
preferAc3ForSurround = appPrefs.experimentalPreferences.enabled { preferAc3Surround },
forceEnabledHdr =
appPrefs.getHdrRangeTypesFor(HdrOverrideMode.HDR_OVERRIDE_ENABLE, newConfig.jellyfinTenEleven),
buildSet {
appPrefs
.getHdrRangeTypesFor(HdrOverrideMode.HDR_OVERRIDE_ENABLE, newConfig.jellyfinTenEleven)
.let(::addAll)
// A profile 7 track the player converts to 8.1 is one the server need not transcode
if (DolbyVisionProfile7Conversion.isWanted(newConfig.experimental, mediaCodecCapabilitiesTest)) {
HdrFormat.DOVI_PROFILE_7
.rangeTypeNames(newConfig.jellyfinTenEleven)
.let(::addAll)
}
},
forceDisabledHdr =
appPrefs.getHdrRangeTypesFor(HdrOverrideMode.HDR_OVERRIDE_DISABLE, newConfig.jellyfinTenEleven),
jellyfinTenEleven = newConfig.jellyfinTenEleven,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import androidx.media3.exoplayer.video.MediaCodecVideoRenderer
import androidx.media3.exoplayer.video.VideoRendererEventListener
import androidx.media3.extractor.DefaultExtractorsFactory
import androidx.media3.extractor.ExtractorsFactory
import androidx.media3.session.MediaSession
import com.github.damontecres.wholphin.mpv.MpvPlayer
import com.github.damontecres.wholphin.preferences.AppPreferences
Expand All @@ -32,7 +33,11 @@ import com.github.damontecres.wholphin.preferences.MediaExtensionStatus
import com.github.damontecres.wholphin.preferences.PlayerBackend
import com.github.damontecres.wholphin.preferences.get
import com.github.damontecres.wholphin.services.hilt.AuthOkHttpClient
import com.github.damontecres.wholphin.util.BitstreamFilter
import com.github.damontecres.wholphin.util.BitstreamFilteringCodecAdapterFactory
import com.github.damontecres.wholphin.util.DolbyVisionProfile7Conversion
import com.github.damontecres.wholphin.util.DolbyVisionProfile7ExtractorsFactory
import com.github.damontecres.wholphin.util.DolbyVisionProfile7Filter
import com.github.damontecres.wholphin.util.Hdr10PlusMaskingFilter
import com.github.damontecres.wholphin.util.WholphinDispatchers
import dagger.hilt.android.qualifiers.ApplicationContext
Expand All @@ -57,6 +62,7 @@ class PlayerFactory
constructor(
@param:ApplicationContext private val context: Context,
@param:AuthOkHttpClient private val authOkHttpClient: OkHttpClient,
private val deviceProfileService: DeviceProfileService,
) {
@Volatile
var currentPlayer: Player? = null
Expand Down Expand Up @@ -94,10 +100,16 @@ class PlayerFactory
val preferDolbyVision =
appPreferences.experimentalPreferences
.get { preferDolbyVisionOverHdr10Plus } ?: false
val convertDolbyVisionProfile7 =
DolbyVisionProfile7Conversion.isWanted(
appPreferences.experimentalPreferences,
deviceProfileService.mediaCodecCapabilitiesTest,
)
Timber.v(
"extensions=%s, assPlaybackMode=%s",
"extensions=%s, assPlaybackMode=%s, convertDolbyVisionProfile7=%s",
extensions,
prefs.overrides.assPlaybackMode,
convertDolbyVisionProfile7,
)
val rendererMode =
when (extensions) {
Expand All @@ -108,9 +120,18 @@ class PlayerFactory
}
val dataSourceFactory = DefaultDataSource.Factory(context)
val extractorsFactory = createExtractorsFactory()

// Applied outermost so that a profile 7 track is advertised as 8.1 whichever
// extractor, media3's own or libass's Matroska replacement, produced it
fun ExtractorsFactory.forDolbyVision(): ExtractorsFactory =
if (convertDolbyVisionProfile7) DolbyVisionProfile7ExtractorsFactory(this) else this
var renderersFactory: RenderersFactory =
WholphinRenderersFactory(context, decodeAv1, preferDolbyVision)
.setEnableDecoderFallback(true)
WholphinRenderersFactory(
context,
decodeAv1,
preferDolbyVision,
convertDolbyVisionProfile7,
).setEnableDecoderFallback(true)
.setExtensionRendererMode(rendererMode)

val mediaSourceFactory =
Expand All @@ -126,15 +147,16 @@ class PlayerFactory
renderersFactory = AssRenderersFactory(assHandler, renderersFactory)
DefaultMediaSourceFactory(
dataSourceFactory,
extractorsFactory.withAssMkvSupport(
assSubtitleParserFactory,
assHandler,
),
extractorsFactory
.withAssMkvSupport(
assSubtitleParserFactory,
assHandler,
).forDolbyVision(),
).setSubtitleParserFactory(assSubtitleParserFactory)
} else {
DefaultMediaSourceFactory(
dataSourceFactory,
extractorsFactory,
extractorsFactory.forDolbyVision(),
)
}
val disableAudioOffload =
Expand Down Expand Up @@ -268,6 +290,7 @@ class WholphinRenderersFactory(
context: Context,
private val av1Enabled: Boolean,
private val preferDolbyVisionOverHdr10Plus: Boolean = false,
private val convertDolbyVisionProfile7: Boolean = false,
) : DefaultRenderersFactory(context) {
@OptIn(ExperimentalApi::class)
override fun buildVideoRenderers(
Expand All @@ -280,12 +303,16 @@ class WholphinRenderersFactory(
allowedVideoJoiningTimeMs: Long,
out: ArrayList<Renderer>,
) {
// The conversion goes first: it may shorten the access unit, and the masking then only
// has to walk what is left of it
val bitstreamFilters =
buildList<BitstreamFilter> {
if (convertDolbyVisionProfile7) add(DolbyVisionProfile7Filter())
if (preferDolbyVisionOverHdr10Plus) add(Hdr10PlusMaskingFilter())
}
val videoCodecAdapterFactory =
if (preferDolbyVisionOverHdr10Plus) {
BitstreamFilteringCodecAdapterFactory(
codecAdapterFactory,
listOf(Hdr10PlusMaskingFilter()),
)
if (bitstreamFilters.isNotEmpty()) {
BitstreamFilteringCodecAdapterFactory(codecAdapterFactory, bitstreamFilters)
} else {
codecAdapterFactory
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@file:OptIn(UnstableApi::class)

package com.github.damontecres.wholphin.util

import androidx.annotation.OptIn
import androidx.media3.common.Format
import androidx.media3.common.MimeTypes
import androidx.media3.common.util.UnstableApi
import com.github.damontecres.wholphin.preferences.ExperimentalPreferences
import com.github.damontecres.wholphin.preferences.enabled
import com.github.damontecres.wholphin.util.profile.MediaCodecCapabilitiesTest

/**
* The one place which decides whether Dolby Vision profile 7 is converted to profile 8.1 on this
* device, and how a converted track is described.
*
* Profile 7 is HDR10 compatible HEVC plus two extra NAL unit types: 62 carries the RPU metadata and
* 63 the enhancement layer. A decoder which only lists profile 8 takes such a stream but does not
* switch the display into Dolby Vision for it, so the picture comes out as the HDR10 base layer.
* Rewriting the RPU into its profile 8.1 form and dropping the enhancement layer produces the
* single layer stream that decoder does switch for. Nothing is re-encoded.
*
* The conversion has two halves which have to agree:
* - [DolbyVisionProfile7Filter] rewrites the access units on their way into the decoder;
* - [DolbyVisionProfile7ExtractorsFactory] rewrites the codec string of the track, because
* media3 picks the decoder by `Format.codecs` and a decoder which does not list profile 7 would
* otherwise never be chosen for it.
*
* Both are switched on by [isWanted], as is direct play of profile 7 in the device profile.
*/
object DolbyVisionProfile7Conversion {
/**
* True when both the user has enabled the setting and this device is one it helps: its Dolby
* Vision decoder lists profile 8 but not profile 7. A device which decodes profile 7 itself
* is left alone, since it can use the enhancement layer the conversion would throw away, and
* a device without a profile 8 decoder has nothing to handle the converted stream
*/
fun isWanted(
experimental: ExperimentalPreferences,
mediaTest: MediaCodecCapabilitiesTest,
): Boolean =
experimental.enabled { convertDolbyVisionProfile7 } &&
!mediaTest.supportsHevcDolbyVisionEL() &&
mediaTest.supportsHevcDolbyVisionProfile8()

/** Whether [format] is a Dolby Vision profile 7 video track, in either sample entry form. */
fun isProfile7(format: Format): Boolean {
val codecs = format.codecs ?: return false
return MimeTypes.VIDEO_DOLBY_VISION == format.sampleMimeType &&
(codecs.startsWith(CODECS_PREFIX_DVHE_07) || codecs.startsWith(CODECS_PREFIX_DVH1_07))
}

/**
* The [format] as the converted stream should be advertised: `dvhe.07.06` becomes
* `dvhe.08.06`, the level and the sample entry prefix stay. Encrypted tracks are left as they
* are: their samples reach the decoder through the secure path the filter does not touch, so
* advertising them as converted would be a lie.
*/
fun advertiseAsProfile8(format: Format): Format =
if (isProfile7(format) && format.drmInitData == null) {
format.buildUpon().setCodecs(toProfile8(format.codecs!!)).build()
} else {
format
}

/** `dvhe.07.06` → `dvhe.08.06`. The caller checks [isProfile7] first. */
fun toProfile8(codecs: String): String = codecs.substring(0, 5) + "08" + codecs.substring(7)

private const val CODECS_PREFIX_DVHE_07 = "dvhe.07"
private const val CODECS_PREFIX_DVH1_07 = "dvh1.07"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@file:OptIn(markerClass = [UnstableApi::class])

package com.github.damontecres.wholphin.util

import android.net.Uri
import androidx.annotation.OptIn
import androidx.media3.common.C
import androidx.media3.common.Format
import androidx.media3.common.util.UnstableApi
import androidx.media3.extractor.Extractor
import androidx.media3.extractor.ExtractorOutput
import androidx.media3.extractor.ExtractorsFactory
import androidx.media3.extractor.ForwardingExtractor
import androidx.media3.extractor.ForwardingExtractorOutput
import androidx.media3.extractor.ForwardingExtractorsFactory
import androidx.media3.extractor.ForwardingTrackOutput
import androidx.media3.extractor.TrackOutput
import timber.log.Timber

/**
* Advertises Dolby Vision profile 7 video tracks as profile 8.1, which is what
* [DolbyVisionProfile7Filter] turns their samples into before the decoder sees them.
*
* media3 chooses the decoder by `Format.codecs`, and a decoder which lists profile 8 but not
* profile 7 answers "not supported" to `dvhe.07`; with decoder fallback on, playback then lands on
* whichever decoder comes first in the list, with a warning that the format exceeds its
* capabilities. Rewriting the codec string at the extractor routes the track to the profile 8
* decoder, and it also sets `MediaFormat.KEY_PROFILE` right, which media3 derives from the same
* string.
*
* Only the codec string changes. Sample data is not touched here: the two convenience overloads of
* [TrackOutput.sampleData] go straight to the delegate, which is exactly what is wanted.
*/
class DolbyVisionProfile7ExtractorsFactory(
delegate: ExtractorsFactory,
) : ForwardingExtractorsFactory(delegate) {
override fun createExtractors(): Array<Extractor> = wrap(super.createExtractors())

override fun createExtractors(
uri: Uri,
responseHeaders: Map<String, List<String>>,
): Array<Extractor> = wrap(super.createExtractors(uri, responseHeaders))

private fun wrap(extractors: Array<Extractor>): Array<Extractor> =
Array(extractors.size) { Profile8AdvertisingExtractor(extractors[it]) }
}

private class Profile8AdvertisingExtractor(
delegate: Extractor,
) : ForwardingExtractor(delegate) {
override fun init(output: ExtractorOutput) {
super.init(Profile8AdvertisingExtractorOutput(output))
}
}

private class Profile8AdvertisingExtractorOutput(
output: ExtractorOutput,
) : ForwardingExtractorOutput(output) {
override fun track(
id: Int,
type: Int,
): TrackOutput {
val trackOutput = super.track(id, type)
return if (type == C.TRACK_TYPE_VIDEO) Profile8AdvertisingTrackOutput(trackOutput) else trackOutput
}
}

/** Rewrites the codec string of every profile 7 format it is handed; everything else passes unchanged. */
internal class Profile8AdvertisingTrackOutput(
delegate: TrackOutput,
) : ForwardingTrackOutput(delegate) {
override fun format(format: Format) {
val advertised = DolbyVisionProfile7Conversion.advertiseAsProfile8(format)
if (advertised !== format) {
Timber.i("Advertising Dolby Vision track %s as %s for conversion", format.codecs, advertised.codecs)
}
super.format(advertised)
}
}
Loading
Loading