Skip to content

feat: support for input with mix of mono and multitrack audiostreams #33

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -6,7 +6,7 @@ package se.svt.oss.encore.model.mediafile

enum class AudioLayout {
NONE,
INVALID,
MIXED_MONO_MULTI,
MONO_STREAMS,
MULTI_TRACK,
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ fun MediaContainer.audioLayout() = when {
audioStreams.size == 1 -> AudioLayout.MULTI_TRACK
audioStreams.all { it.channels == 1 } -> AudioLayout.MONO_STREAMS
audioStreams.first().channels > 1 -> AudioLayout.MULTI_TRACK
else -> AudioLayout.INVALID
else -> AudioLayout.MIXED_MONO_MULTI
}

fun MediaContainer.channelCount() = if (audioLayout() == AudioLayout.MULTI_TRACK) {
audioStreams.first().channels
} else {
audioStreams.size
fun MediaContainer.channelCount() = when (audioLayout()) {
AudioLayout.MULTI_TRACK -> audioStreams.first().channels
AudioLayout.MONO_STREAMS -> audioStreams.size
// Return number of mono tracks before first multitrack stream,
// effectively ignoring all other streams
AudioLayout.MIXED_MONO_MULTI -> audioStreams.indexOfFirst { it.channels > 1 }
AudioLayout.NONE -> 0
}

fun AudioIn.channelLayout(defaultChannelLayouts: Map<Int, ChannelLayout>): ChannelLayout = when (analyzedAudio.audioLayout()) {
AudioLayout.NONE, AudioLayout.INVALID -> null
AudioLayout.MONO_STREAMS -> if (analyzedAudio.channelCount() == channelLayout?.channels?.size) {
AudioLayout.NONE -> null
AudioLayout.MONO_STREAMS, AudioLayout.MIXED_MONO_MULTI -> if (analyzedAudio.channelCount() == channelLayout?.channels?.size) {
channelLayout
} else {
defaultChannelLayouts[analyzedAudio.channelCount()]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ data class AudioEncode(
val audioIn = job.inputs.audioInput(inputLabel)
?: return logOrThrow("Can not generate $outputName! No audio input with label '$inputLabel'.")
val analyzed = audioIn.analyzedAudio
if (analyzed.audioLayout() == AudioLayout.INVALID) {
throw RuntimeException("Audio layout of audio input '$inputLabel' is not supported!")
}
if (analyzed.audioLayout() == AudioLayout.NONE) {
return logOrThrow("Can not generate $outputName! No audio streams in input!")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,16 @@ class CommandBuilder(
return filters + input.videoFilters
}

private fun globalAudioFilters(input: AudioIn, analyzed: MediaContainer): List<String> = if (analyzed.audioLayout() == AudioLayout.MONO_STREAMS) {
val channelLayout = input.channelLayout(encodingProperties.defaultChannelLayouts)
val map = channelLayout.channels.withIndex().joinToString("|") { "${it.index}.0-${it.value}" }
listOf("join=inputs=${channelLayout.channels.size}:channel_layout=${channelLayout.layoutName}:map=$map")
} else {
emptyList()
} + input.audioFilters
private fun globalAudioFilters(input: AudioIn, analyzed: MediaContainer): List<String> =
if (analyzed.audioLayout() == AudioLayout.MONO_STREAMS ||
analyzed.audioLayout() == AudioLayout.MIXED_MONO_MULTI
) {
val channelLayout = input.channelLayout(encodingProperties.defaultChannelLayouts)
val map = channelLayout.channels.withIndex().joinToString("|") { "${it.index}.0-${it.value}" }
listOf("join=inputs=${channelLayout.channels.size}:channel_layout=${channelLayout.layoutName}:map=$map")
} else {
emptyList()
} + input.audioFilters

private fun firstPassParams(output: Output): List<String> {
if (output.video == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import se.svt.oss.mediaanalyzer.file.MediaFile
internal class MediaFileExtensionsTest {
private val noAudio = defaultVideoFile.copy(audioStreams = emptyList())

private val invalidAudio = defaultVideoFile.copy(
private val mixedMonoMultiAudio = defaultVideoFile.copy(
audioStreams = defaultVideoFile.audioStreams.mapIndexed { index, audioStream ->
if (index == 0) audioStream else audioStream.copy(channels = 2)
},
Expand Down Expand Up @@ -53,9 +53,9 @@ internal class MediaFileExtensionsTest {
}

@Test
@DisplayName("Audio layout is invalid if first stream has one channel and the rest has two channels or more")
fun testAudioLayoutInvalid() {
assertThat(invalidAudio.audioLayout()).isEqualTo(AudioLayout.INVALID)
@DisplayName("Audio layout is MIXED_MONO_MULTI if first stream has one channel and the rest has two channels or more")
fun testAudioLayoutMonotracksFollowedByMultitrack() {
assertThat(mixedMonoMultiAudio.audioLayout()).isEqualTo(AudioLayout.MIXED_MONO_MULTI)
}

@Test
Expand All @@ -72,6 +72,12 @@ internal class MediaFileExtensionsTest {
assertThat(defaultVideoFile.channelCount()).isEqualTo(8)
}

@Test
@DisplayName("Channel count for mono stream followed by multitrack streams is equal to number of leading mono streams")
fun testChannelCountMixedMonoMultiStreams() {
assertThat(mixedMonoMultiAudio.channelCount()).isEqualTo(1)
}

@Test
@DisplayName("Channel count for no streams is 0")
fun testChannelCountNoSteams() {
Expand Down Expand Up @@ -149,13 +155,6 @@ internal class MediaFileExtensionsTest {
.hasMessage("Could not determine channel layout for audio input 'main'!")
}

@Test
@DisplayName("Channel layout throws when invalid audio")
fun channelLayoutInvalidAudio() {
assertThatThrownBy { audioInput(invalidAudio).channelLayout(defaultChannelLayouts) }
.hasMessage("Could not determine channel layout for audio input 'main'!")
}

@Test
@DisplayName("Channel layout is set on input and channel count correct")
fun channelLayoutMonoStreamsSetByParam() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ class AudioEncodeTest {
.hasMessageContaining("No audio streams in input")
}

@Test
fun `not supported soundtype throws exception`() {
val job = job(getAudioStream(1), getAudioStream(2))
assertThatThrownBy {
audioEncode.getOutput(
job,
EncodingProperties(audioMixPresets = mapOf("default" to AudioMixPreset(fallbackToAuto = false))),
)
}.isInstanceOf(RuntimeException::class.java)
.hasMessage("Audio layout of audio input 'main' is not supported!")
}

@Test
fun `valid output`() {
val output = audioEncode.getOutput(
Expand Down