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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class AssHandler(
/** The available ASS tracks in the current media. */
private val availableTracks = mutableMapOf<String, AssTrack>()

/** Fonts encountered before any ASS track was created. Flushed in [createTrack]. */
private val pendingFonts = mutableListOf<Pair<String, ByteArray>>()

/** The size of the video track. */
var videoSize = Size.ZERO
private set
Expand Down Expand Up @@ -114,6 +117,7 @@ class AssHandler(
render = null
track = null
availableTracks.clear()
pendingFonts.clear()
videoSize = Size.ZERO
videoTime = -1
videoFrameIndex = 0
Expand Down Expand Up @@ -216,6 +220,19 @@ class AssHandler(
return availableTracks.isNotEmpty()
}

/**
* Adds a font to the ASS library. If no tracks have been created yet, the font is buffered
* and will be added when the first track is created via [createTrack].
*/
@Synchronized
fun addFont(name: String, data: ByteArray) {
if (hasTracks()) {
ass.addFont(name, data)
} else {
pendingFonts.add(name to data)
}
}

/**
* Creates a new ASS track from the given format and saves it in the [availableTracks].
* The renderer and libass are also created if needed.
Expand All @@ -228,6 +245,14 @@ class AssHandler(
// Ensure the renderer is created before creating tracks.
createRenderIfNeeded()

// Flush any fonts that were buffered before the first track was created.
if (pendingFonts.isNotEmpty()) {
for ((name, data) in pendingFonts) {
ass.addFont(name, data)
}
pendingFonts.clear()
}

val track = ass.createTrack()
if (format.initializationData.size > 0) {
val header = AssHeaderParser.parse(format, renderType != AssRenderType.CUES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ class AssMatroskaExtractor(
val attachmentName = requireNotNull(currentAttachmentName)
val attachmentMime = requireNotNull(currentAttachmentMime)

// Only add fonts if an ASS track was detected to support lazy initialization
if (assHandler.hasTracks() && attachmentMime in fontMimeTypes) {
if (attachmentMime in fontMimeTypes) {
val data = ByteArray(contentSize)
input.readFully(data, 0, contentSize)
assHandler.ass.addFont(attachmentName, data)
assHandler.addFont(attachmentName, data)
} else {
input.skipFully(contentSize)
}
Expand Down