From 0528f7cad856a2b1347e41944167b0583fc4a3d9 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 25 Jan 2025 10:26:46 +0200 Subject: [PATCH] feat(YouTube): Add patch `Disable HDR video` (#4347) --- .../youtube/patches/DisableHdrPatch.java | 15 ++++ .../extension/youtube/settings/Settings.java | 1 + patches/api/patches.api | 4 + .../layout/seekbar/SeekbarColorPatch.kt | 17 +++-- .../youtube/video/hdr/DisableHdrPatch.kt | 74 +++++++++++++++++++ .../patches/youtube/video/hdr/Fingerprints.kt | 12 +++ .../resources/addresources/values/strings.xml | 5 ++ 7 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/DisableHdrPatch.java create mode 100644 patches/src/main/kotlin/app/revanced/patches/youtube/video/hdr/DisableHdrPatch.kt create mode 100644 patches/src/main/kotlin/app/revanced/patches/youtube/video/hdr/Fingerprints.kt diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/DisableHdrPatch.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/DisableHdrPatch.java new file mode 100644 index 0000000000..174e8a47ef --- /dev/null +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/DisableHdrPatch.java @@ -0,0 +1,15 @@ +package app.revanced.extension.youtube.patches; + +import app.revanced.extension.youtube.settings.Settings; + +@SuppressWarnings("unused") +public class DisableHdrPatch { + + /** + * Injection point. + */ + public static boolean disableHDRVideo() { + return !Settings.DISABLE_HDR_VIDEO.get(); + } +} + diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java index 59b803d985..6977d59e3b 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java @@ -47,6 +47,7 @@ public class Settings extends BaseSettings { // Video + public static final BooleanSetting DISABLE_HDR_VIDEO = new BooleanSetting("revanced_disable_hdr_video", FALSE); public static final BooleanSetting RESTORE_OLD_VIDEO_QUALITY_MENU = new BooleanSetting("revanced_restore_old_video_quality_menu", TRUE); public static final BooleanSetting REMEMBER_VIDEO_QUALITY_LAST_SELECTED = new BooleanSetting("revanced_remember_video_quality_last_selected", FALSE); public static final IntegerSetting VIDEO_QUALITY_DEFAULT_WIFI = new IntegerSetting("revanced_video_quality_default_wifi", -2); diff --git a/patches/api/patches.api b/patches/api/patches.api index 7287742784..ce8c2f0a9c 100644 --- a/patches/api/patches.api +++ b/patches/api/patches.api @@ -1414,6 +1414,10 @@ public final class app/revanced/patches/youtube/video/audio/ForceOriginalAudioPa public static final fun getForceOriginalAudioPatch ()Lapp/revanced/patcher/patch/BytecodePatch; } +public final class app/revanced/patches/youtube/video/hdr/DisableHdrPatchKt { + public static final fun getDisableHdrPatch ()Lapp/revanced/patcher/patch/BytecodePatch; +} + public final class app/revanced/patches/youtube/video/information/VideoInformationPatchKt { public static final fun getVideoInformationPatch ()Lapp/revanced/patcher/patch/BytecodePatch; public static final fun userSelectedPlaybackSpeedHook (Ljava/lang/String;Ljava/lang/String;)V diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt index 93e8ff19ca..19a2b5397f 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/layout/seekbar/SeekbarColorPatch.kt @@ -68,14 +68,6 @@ private val seekbarColorResourcePatch = resourcePatch { "color", "inline_time_bar_played_not_highlighted_color", ] - ytYoutubeMagentaColorId = resourceMappings[ - "color", - "yt_youtube_magenta", - ] - ytStaticBrandRedId = resourceMappings[ - "attr", - "ytStaticBrandRed", - ] // Modify the resume playback drawable and replace the progress bar with a custom drawable. document("res/drawable/resume_playback_progressbar_drawable.xml").use { document -> @@ -97,6 +89,15 @@ private val seekbarColorResourcePatch = resourcePatch { return@execute } + ytYoutubeMagentaColorId = resourceMappings[ + "color", + "yt_youtube_magenta", + ] + ytStaticBrandRedId = resourceMappings[ + "attr", + "ytStaticBrandRed", + ] + // Add attribute and styles for splash screen custom color. // Using a style is the only way to selectively change just the seekbar fill color. // diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/video/hdr/DisableHdrPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/video/hdr/DisableHdrPatch.kt new file mode 100644 index 0000000000..8e1eb7c96a --- /dev/null +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/video/hdr/DisableHdrPatch.kt @@ -0,0 +1,74 @@ +package app.revanced.patches.youtube.video.hdr + +import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels +import app.revanced.patcher.patch.bytecodePatch +import app.revanced.patches.all.misc.resources.addResources +import app.revanced.patches.all.misc.resources.addResourcesPatch +import app.revanced.patches.shared.misc.settings.preference.SwitchPreference +import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch +import app.revanced.patches.youtube.misc.settings.PreferenceScreen +import app.revanced.patches.youtube.misc.settings.settingsPatch +import app.revanced.util.getReference +import app.revanced.util.indexOfFirstInstructionOrThrow +import com.android.tools.smali.dexlib2.iface.reference.MethodReference + +private const val EXTENSION_CLASS_DESCRIPTOR = + "Lapp/revanced/extension/youtube/patches/DisableHdrPatch;" + +@Suppress("unused") +val disableHdrPatch = bytecodePatch( + name = "Disable HDR video", + description = "Adds an option to disable video HDR.", +) { + dependsOn( + sharedExtensionPatch, + settingsPatch, + addResourcesPatch, + ) + + compatibleWith( + "com.google.android.youtube"( + "18.38.44", + "18.49.37", + "19.16.39", + "19.25.37", + "19.34.42", + "19.43.41", + "19.45.38", + "19.46.42", + "19.47.53", + ), + ) + + execute { + addResources("youtube", "video.hdr.disableHdrPatch") + + PreferenceScreen.VIDEO.addPreferences( + SwitchPreference("revanced_disable_hdr_video") + ) + + hdrCapabilityFingerprint.let { + it.originalMethod.apply { + val stringIndex = it.stringMatches!!.first().index + val navigateIndex = indexOfFirstInstructionOrThrow(stringIndex) { + val reference = getReference() + reference?.parameterTypes == listOf("I", "Landroid/view/Display;") && + reference.returnType == "Z" + } + + // Modify the HDR lookup method (Method is in the same class as the fingerprint). + navigate(this).to(navigateIndex).stop().addInstructionsWithLabels( + 0, + """ + invoke-static {}, $EXTENSION_CLASS_DESCRIPTOR->disableHDRVideo()Z + move-result v0 + if-nez v0, :useHdr + return v0 + :useHdr + nop + """ + ) + } + } + } +} diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/video/hdr/Fingerprints.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/video/hdr/Fingerprints.kt new file mode 100644 index 0000000000..fe8d2dce3c --- /dev/null +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/video/hdr/Fingerprints.kt @@ -0,0 +1,12 @@ +package app.revanced.patches.youtube.video.hdr + +import app.revanced.patcher.fingerprint +import com.android.tools.smali.dexlib2.AccessFlags + +internal val hdrCapabilityFingerprint = fingerprint { + accessFlags(AccessFlags.PUBLIC, AccessFlags.FINAL) + strings( + "av1_profile_main_10_hdr_10_plus_supported", + "video/av01" + ) +} \ No newline at end of file diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index 7e7a6f2dba..ed53a0d5a1 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -1367,6 +1367,11 @@ Enabling this can unlock higher video qualities" Default playback speed Changed default speed to: %s + + Disable HDR video + HDR video is disabled + HDR video is enabled + Restore old video quality menu Old video quality menu is shown