Skip to content

Commit

Permalink
feat: debanding and yuv420p
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahmehiz committed Jun 4, 2024
1 parent df75611 commit 10ecf37
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 26 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/live/mehiz/mpvkt/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import live.mehiz.mpvkt.ui.theme.MpvKtTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MpvKtTheme {
Navigator(HomeScreen) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package live.mehiz.mpvkt.preferences

import live.mehiz.mpvkt.preferences.preference.PreferenceStore
import live.mehiz.mpvkt.preferences.preference.getEnum
import live.mehiz.mpvkt.ui.player.Debanding

class DecoderPreferences(preferenceStore: PreferenceStore) {
val tryHWDecoding = preferenceStore.getBoolean("try_hw_dec", true)
val gpuNext = preferenceStore.getBoolean("gpu_next", false)
val debanding = preferenceStore.getEnum("debanding", Debanding.None)
val useYUV420P = preferenceStore.getBoolean("use_yuv420p", true)
}
50 changes: 35 additions & 15 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,24 @@ class PlayerActivity : AppCompatActivity() {
private val binding by lazy { PlayerLayoutBinding.inflate(this.layoutInflater) }
val player by lazy { binding.player }
val windowInsetsController by lazy { WindowCompat.getInsetsController(window, window.decorView) }
val audioManager by lazy { getSystemService(Context.AUDIO_SERVICE) as AudioManager }
private val playerPreferences by inject<PlayerPreferences>()
private val decoderPreferences by inject<DecoderPreferences>()
val audioManager by lazy { getSystemService(Context.AUDIO_SERVICE) as AudioManager }

override fun onCreate(savedInstanceState: Bundle?) {
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
super.onCreate(savedInstanceState)
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
setContentView(binding.root)
player.initialize(
applicationContext.filesDir.path,
applicationContext.cacheDir.path,
"v",
if(decoderPreferences.gpuNext.get()) "gpu-next" else "gpu"
)
MPVLib.setPropertyString(
"hwdec",
if (decoderPreferences.tryHWDecoding.get()) "auto-copy" else "no"
)
player.addObserver(PlayerObserver(this))
Utils.copyAssets(this)

setupMPV()
val uri = parsePathFromIntent(intent)
val videoUri = if (uri?.startsWith("content://") == true) {
openContentFd(Uri.parse(uri))
} else {
uri
}
player.playFile(videoUri!!)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
setOrientation()
val controls = PlayerControls(viewModel)

Expand All @@ -76,6 +66,36 @@ class PlayerActivity : AppCompatActivity() {
viewModel.pauseUnpause()
}

private fun setupMPV() {

Utils.copyAssets(this)

player.initialize(
applicationContext.filesDir.path,
applicationContext.cacheDir.path,
"v",
if(decoderPreferences.gpuNext.get()) "gpu-next" else "gpu"
)

MPVLib.setPropertyString(
"hwdec",
if (decoderPreferences.tryHWDecoding.get()) "auto-copy" else "no"
)
when(decoderPreferences.debanding.get()) {
Debanding.None -> {}
Debanding.CPU -> MPVLib.setPropertyString("vf", "gradfun=radius=12")
Debanding.GPU -> MPVLib.setPropertyString("deband", "yes")
}
if(decoderPreferences.useYUV420P.get()) MPVLib.setPropertyString("vf", "format=yuv420p")

MPVLib.command(arrayOf("script-binding", "stats/display-stats-toggle"))
MPVLib.command(
arrayOf("script-binding", "stats/display-page-1"),
)

player.addObserver(PlayerObserver(this))
}

private fun parsePathFromIntent(intent: Intent): String? {
val filepath: String? = when (intent.action) {
Intent.ACTION_VIEW -> intent.data?.let { resolveUri(it) }
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerEnums.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ enum class Decoder(val title: String, val value: String) {
fun getDecoderFromValue(value: String): Decoder {
return Decoder.entries.first { it.value == value }
}

enum class Debanding {
None,
CPU,
GPU,
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import live.mehiz.mpvkt.R
import live.mehiz.mpvkt.preferences.DecoderPreferences
import live.mehiz.mpvkt.preferences.preference.collectAsState
import live.mehiz.mpvkt.ui.player.Debanding
import me.zhanghai.compose.preference.ListPreference
import me.zhanghai.compose.preference.ProvidePreferenceLocals
import me.zhanghai.compose.preference.listPreference
import me.zhanghai.compose.preference.switchPreference
import org.koin.compose.koinInject

Expand All @@ -46,8 +52,8 @@ object DecoderPreferencesScreen : Screen {
ProvidePreferenceLocals {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(padding),
.fillMaxSize()
.padding(padding),
) {
switchPreference(
preferences.tryHWDecoding.key(),
Expand All @@ -58,7 +64,24 @@ object DecoderPreferencesScreen : Screen {
preferences.gpuNext.key(),
defaultValue = preferences.gpuNext.defaultValue(),
title = { Text(stringResource(R.string.pref_decoder_gpu_next_title)) },
summary = { Text(stringResource(R.string.pref_decoder_gpu_next_subtitle)) },
summary = { Text(stringResource(R.string.pref_decoder_gpu_next_summary)) },
)
item {
val debanding by preferences.debanding.collectAsState()
ListPreference(
debanding,
onValueChange = { preferences.debanding.set(it) },
title = { Text(stringResource(R.string.pref_decoder_debanding_title)) },
values = Debanding.entries,
valueToText = { AnnotatedString(it.name) },
summary = { Text(debanding.name) },
)
}
switchPreference(
preferences.useYUV420P.key(),
defaultValue = preferences.useYUV420P.defaultValue(),
title = { Text(stringResource(R.string.pref_decoder_yuv420p_title)) },
summary = { Text(stringResource(R.string.pref_decoder_yuv420p_summary)) }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ object PlayerPreferencesScreen : Screen {
preferences.showChaptersButton.key(),
defaultValue = preferences.showChaptersButton.defaultValue(),
title = { Text(stringResource(R.string.pref_player_controls_show_chapters_button)) },
summary = { Text(stringResource(R.string.pref_player_controls_show_chapters_subtitle)) },
summary = { Text(stringResource(R.string.pref_player_controls_show_chapters_summary)) },
)
switchPreference(
preferences.currentChaptersIndicator.key(),
defaultValue = preferences.currentChaptersIndicator.defaultValue(),
title = { Text(stringResource(R.string.pref_player_controls_show_chapter_indicator)) },
summary = { Text(stringResource(R.string.pref_player_controls_show_chapters_subtitle)) },
summary = { Text(stringResource(R.string.pref_player_controls_show_chapters_summary)) },
)
}
}
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<string name="pref_appearance_darkmode_light">Light</string>
<string name="pref_appearance_darkmode_dark">Dark</string>
<string name="pref_appearance_material_you_title">Material You</string>
<string name="pref_appearance_material_you_summary">Enable Material You dynamic colors</string>
<string name="pref_appearance_material_you_summary_disabled">Only available for Android 12 and newer</string>
<string name="pref_appearance_material_you_summary">Enable Material You dynamic colors.</string>
<string name="pref_appearance_material_you_summary_disabled">Only available for Android 12 and newer.</string>

<string name="pref_player">Player</string>
<string name="pref_player_orientation">Orientation</string>
Expand All @@ -32,18 +32,21 @@
<string name="pref_player_controls">Controls</string>
<string name="pref_player_controls_show_chapter_indicator">Show current chapter indicator</string>
<string name="pref_player_controls_show_chapters_button">Show chapters select button</string>
<string name="pref_player_controls_show_chapters_subtitle">Only appears if the video has chapters</string>
<string name="pref_player_controls_show_chapters_summary">Only appears if the video has chapters.</string>

<string name="pref_decoder">Decoder</string>
<string name="pref_decoder_try_hw_dec_title">Try hardware decoding</string>
<string name="pref_decoder_gpu_next_title">Use gpu-next</string>
<string name="pref_decoder_gpu_next_subtitle">A new rendering backend.</string>
<string name="pref_decoder_gpu_next_summary">A new rendering backend.</string>
<string name="pref_decoder_debanding_title">Debanding</string>
<string name="pref_decoder_yuv420p_title">Use YUV420P pixel format</string>
<string name="pref_decoder_yuv420p_summary">May fix black screens on some video codecs, can also improve performance at the cost of quality.</string>

<string name="player_sheets_add_ext_sub">Add external subtitles</string>
<string name="player_sheets_add_ext_audio">Add external audio tracks</string>
<string name="player_sheets_tracks_off">Off</string>
<string name="player_sheets_track_title_w_lang">#%d: %s (%s)</string>
<string name="player_sheets_track_title_wo_lang">#%d: %s</string>
<string name="player_sheets_track_lang_wo_title">#%d: %s</string>
<string name="player_sheets_subtitles_footer_secondary_sid_no_styles">Secondary subtitles will have no ASS/SSA Styling</string>
<string name="player_sheets_subtitles_footer_secondary_sid_no_styles">Secondary subtitles will have no ASS/SSA Styling.</string>
</resources>

0 comments on commit 10ecf37

Please sign in to comment.