-
Notifications
You must be signed in to change notification settings - Fork 0
fix(player): prevent subtitle overlap and restore zlib subtitle samples #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ModerRAS
wants to merge
3
commits into
master
Choose a base branch
from
fix/subtitle-bilingual-overlap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
player-core/src/main/kotlin/com/miruplay/tv/player/ZlibSubtitleExtractor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package com.miruplay.tv.player | ||
|
|
||
| import androidx.media3.common.Format | ||
| import androidx.media3.common.util.Consumer | ||
| import androidx.media3.common.util.UnstableApi | ||
| import androidx.media3.extractor.DefaultExtractorsFactory | ||
| import androidx.media3.extractor.Extractor | ||
| import androidx.media3.extractor.ExtractorsFactory | ||
| import androidx.media3.extractor.text.DefaultSubtitleParserFactory | ||
| import androidx.media3.extractor.text.SubtitleParser | ||
| import java.io.ByteArrayOutputStream | ||
| import java.util.zip.DataFormatException | ||
| import java.util.zip.Inflater | ||
|
|
||
| /** | ||
| * Media3 handles Matroska header stripping but not zlib-compressed subtitle samples. | ||
| * This factory keeps the stock extractors and inflates text samples before parsing. | ||
| */ | ||
| @UnstableApi | ||
| internal class ZlibSubtitleExtractorsFactory( | ||
| private val delegate: DefaultExtractorsFactory = DefaultExtractorsFactory() | ||
| .setSubtitleParserFactory(ZlibSubtitleParserFactory(DefaultSubtitleParserFactory())) | ||
| .experimentalSetTextTrackTranscodingEnabled(true), | ||
| ) : ExtractorsFactory { | ||
|
|
||
| override fun createExtractors(): Array<Extractor> = delegate.createExtractors() | ||
|
|
||
| override fun experimentalSetTextTrackTranscodingEnabled(enabled: Boolean): ExtractorsFactory { | ||
| delegate.experimentalSetTextTrackTranscodingEnabled(enabled) | ||
| return this | ||
| } | ||
|
|
||
| override fun setSubtitleParserFactory(factory: SubtitleParser.Factory): ExtractorsFactory { | ||
| delegate.setSubtitleParserFactory(ZlibSubtitleParserFactory(factory)) | ||
| return this | ||
| } | ||
| } | ||
|
|
||
| @UnstableApi | ||
| private class ZlibSubtitleParserFactory( | ||
| private val delegate: SubtitleParser.Factory, | ||
| ) : SubtitleParser.Factory { | ||
|
|
||
| override fun supportsFormat(format: Format): Boolean = delegate.supportsFormat(format) | ||
|
|
||
| override fun getCueReplacementBehavior(format: Format): Int = | ||
| delegate.getCueReplacementBehavior(format) | ||
|
|
||
| override fun create(format: Format): SubtitleParser = | ||
| ZlibSubtitleParser(delegate.create(format)) | ||
| } | ||
|
|
||
| @UnstableApi | ||
| private class ZlibSubtitleParser( | ||
| private val delegate: SubtitleParser, | ||
| ) : SubtitleParser { | ||
|
|
||
| override fun parse( | ||
| data: ByteArray, | ||
| offset: Int, | ||
| length: Int, | ||
| outputOptions: SubtitleParser.OutputOptions, | ||
| output: Consumer<androidx.media3.extractor.text.CuesWithTiming>, | ||
| ) { | ||
| val sample = data.copyOfRange(offset, offset + length) | ||
| val inflated = inflateSubtitleSampleIfNeeded(sample) | ||
| delegate.parse(inflated, 0, inflated.size, outputOptions, output) | ||
| } | ||
|
|
||
| override fun getCueReplacementBehavior(): Int = delegate.getCueReplacementBehavior() | ||
|
|
||
| override fun reset() { | ||
| delegate.reset() | ||
| } | ||
| } | ||
|
|
||
| internal fun inflateSubtitleSampleIfNeeded(input: ByteArray): ByteArray { | ||
| if (input.size < 2) return input | ||
|
|
||
| for (start in 0..input.lastIndex - 1) { | ||
| if (!isZlibHeader(input, start)) continue | ||
| val inflated = inflateFrom(input, start) ?: continue | ||
| val candidate = input.copyOfRange(0, start) + inflated | ||
| if (looksLikeSubtitleText(candidate)) return candidate | ||
| } | ||
| return input | ||
| } | ||
|
|
||
| private fun isZlibHeader(input: ByteArray, index: Int): Boolean { | ||
| val compressionMethod = input[index].toInt() and 0x0F | ||
| val flags = input[index + 1].toInt() and 0xFF | ||
| val header = ((input[index].toInt() and 0xFF) shl 8) or flags | ||
| return compressionMethod == 8 && header % 31 == 0 | ||
| } | ||
|
|
||
| private fun inflateFrom(input: ByteArray, start: Int): ByteArray? { | ||
| val inflater = Inflater() | ||
| return try { | ||
| inflater.setInput(input, start, input.size - start) | ||
| val output = ByteArrayOutputStream(maxOf(32, (input.size - start) * 2)) | ||
| val buffer = ByteArray(8 * 1024) | ||
| while (!inflater.finished()) { | ||
| val count = inflater.inflate(buffer) | ||
| if (count > 0) { | ||
| output.write(buffer, 0, count) | ||
| } else if (inflater.needsDictionary() || inflater.needsInput()) { | ||
| return null | ||
| } else { | ||
| return null | ||
| } | ||
| } | ||
| output.toByteArray() | ||
| } catch (_: DataFormatException) { | ||
| null | ||
| } finally { | ||
| inflater.end() | ||
| } | ||
| } | ||
|
|
||
| private fun looksLikeSubtitleText(bytes: ByteArray): Boolean { | ||
| if (bytes.isEmpty()) return false | ||
| val text = bytes.toString(Charsets.UTF_8) | ||
| if (text.indexOf('\uFFFD') >= 0) return false | ||
| return text.contains("Dialogue:") || | ||
| text.contains("--> ") || | ||
| text.contains("WEBVTT") || | ||
| text.contains("<tt") | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
player-core/src/test/kotlin/com/miruplay/tv/player/ZlibSubtitleSampleTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package com.miruplay.tv.player | ||
|
|
||
| import java.util.zip.Deflater | ||
| import org.junit.Assert.assertArrayEquals | ||
| import org.junit.Test | ||
|
|
||
| class ZlibSubtitleSampleTest { | ||
|
|
||
| @Test | ||
| fun `zlib compressed subtitle sample is inflated`() { | ||
| val ass = "Dialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,Hello\n".toByteArray() | ||
|
|
||
| val result = inflateSubtitleSampleIfNeeded(deflate(ass)) | ||
|
|
||
| assertArrayEquals(ass, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `matroska dialogue prefix is preserved while zlib payload is inflated`() { | ||
| val prefix = "Dialogue: 0:00:00:00,0:00:02:16," | ||
| val payload = "Default,,0,0,0,,Hello\n".toByteArray() | ||
| val sample = prefix.toByteArray() + deflate(payload) | ||
|
|
||
| assertArrayEquals( | ||
| prefix.toByteArray() + payload, | ||
| inflateSubtitleSampleIfNeeded(sample), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `plain subtitle sample is returned unchanged`() { | ||
| val ass = "Dialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,Hello\n".toByteArray() | ||
|
|
||
| assertArrayEquals(ass, inflateSubtitleSampleIfNeeded(ass)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `invalid compressed-looking sample is returned unchanged`() { | ||
| val bytes = byteArrayOf(0x78.toByte(), 0x9c.toByte(), 0x01, 0x02, 0x03) | ||
|
|
||
| assertArrayEquals(bytes, inflateSubtitleSampleIfNeeded(bytes)) | ||
| } | ||
|
|
||
| private fun deflate(input: ByteArray): ByteArray { | ||
| val deflater = Deflater() | ||
| deflater.setInput(input) | ||
| deflater.finish() | ||
| val output = ByteArray(input.size + 64) | ||
| val length = deflater.deflate(output) | ||
| deflater.end() | ||
| return output.copyOf(length) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
player-mpv-android/src/main/kotlin/is/xyz/mpv/MpvSubtitleLayout.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package `is`.xyz.mpv | ||
|
|
||
| /** mpv subtitle options intentionally left empty so libass keeps authored ASS layout. */ | ||
| internal val mpvSubtitleLayoutNormalisationOptions: List<Pair<String, String>> = emptyList() |
15 changes: 15 additions & 0 deletions
15
player-mpv-android/src/test/kotlin/is/xyz/mpv/MpvSubtitleLayoutTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package `is`.xyz.mpv | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| class MpvSubtitleLayoutTest { | ||
|
|
||
| @Test | ||
| fun `subtitle layout leaves ass positioning to libass`() { | ||
| assertEquals( | ||
| emptyList<Pair<String, String>>(), | ||
| mpvSubtitleLayoutNormalisationOptions, | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Cap the decompressed size to prevent a zlib-bomb OOM.
inflateFromhas no limit on the decompressed output size.ByteArrayOutputStreamkeeps growing on everyinflater.inflatecall until the stream reportsfinished(). A crafted subtitle sample with a small compressed size but a huge decompressed size (a zip bomb) can exhaust memory and crash playback.looksLikeSubtitleTextthen decodes the unbounded candidate as UTF-8, compounding the cost. Since subtitle samples can originate from untrusted or remote media, bound the inflate loop.🛡️ Proposed fix to cap decompressed subtitle size
📝 Committable suggestion
🤖 Prompt for AI Agents