Skip to content

Commit

Permalink
MKA : Properly detect Opus and Vorbis audio formats instead of OGG
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeugma440 committed Sep 7, 2024
1 parent 3a61e48 commit b633f61
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ATL.unit-test/IO/AudioData/AudioData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void Audio_CAF()
public void Audio_MKA()
{
testGenericAudio("MKA/mka.mka", 3422, 128, -1, 44100, false, CF_LOSSY, STEREO, "Matroska / MPEG", 459, 69016);
testGenericAudio("MKA/no_info_duration.webm", 7159, 16, 0, 48000, false, CF_LOSSY, MONO, "Matroska / OGG", 167, 112605);
testGenericAudio("MKA/no_info_duration.webm", 7159, 16, 0, 48000, false, CF_LOSSY, MONO, "Matroska / Opus", 167, 112605);
}
#pragma warning restore S2699 // Tests should include assertions
}
Expand Down
56 changes: 37 additions & 19 deletions ATL/AudioData/IO/MKA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal partial class MKA : MetaDataIO, IAudioDataIO
private const int TRACKTYPE_AUDIO = 2;

// Mapping between MKV format ID and ATL format IDs
private static readonly Dictionary<string, int> codecsMapping = new Dictionary<string, int>
private static readonly Dictionary<string, int> codecsMappingCodes = new Dictionary<string, int>
{
{ "A_MPEG/L3", AudioDataIOFactory.CID_MP3 },
{ "A_MPEG/L2", AudioDataIOFactory.CID_MP3 },
Expand All @@ -135,7 +135,7 @@ internal partial class MKA : MetaDataIO, IAudioDataIO
{ "A_DTS", AudioDataIOFactory.CID_DTS },
{ "A_DTS/EXPRESS", AudioDataIOFactory.CID_DTS },
{ "A_DTS/LOSSLESS", AudioDataIOFactory.CID_DTS },
{ "A_VORBIS", AudioDataIOFactory.CID_OGG }, // TODO display Vorbis instead of Ogg
{ "A_VORBIS", AudioDataIOFactory.CID_OGG },
{ "A_FLAC", AudioDataIOFactory.CID_FLAC },
// No support for RealMedia
// No support for MS ACM
Expand All @@ -152,9 +152,12 @@ internal partial class MKA : MetaDataIO, IAudioDataIO
{ "A_TTA1", AudioDataIOFactory.CID_TTA },
{ "A_WAVPACK4", AudioDataIOFactory.CID_WAVPACK },
// No support for ATRAC1
{ "A_OPUS", AudioDataIOFactory.CID_OGG } // TODO display Opus instead of Ogg
{ "A_OPUS", AudioDataIOFactory.CID_OGG }
};

// Mapping between MKV format ID and ATL formats
private static readonly Dictionary<string, Format> codecsMapping = new Dictionary<string, Format>();

// Mapping between MKV tag names and ATL frame codes
private static readonly Dictionary<string, Field> frameMapping = new Dictionary<string, Field>()
{
Expand Down Expand Up @@ -266,6 +269,36 @@ public override string EncodeDate(DateTime date)

// ---------- CONSTRUCTORS & INITIALIZERS

static MKA()
{
var formats = AudioDataIOFactory.GetInstance().getFormats();
foreach (var cmc in codecsMappingCodes)
{
var format = formats.Where(f => f.ID == cmc.Value).ToList();
if (format.Count > 0)
{
var formatFinal = format[0];
formatFinal = cmc.Key switch
{
"A_OPUS" => new Format(formatFinal) { Name = "Opus", ShortName = "Opus" },
"A_VORBIS" => new Format(formatFinal) { Name = "Vorbis", ShortName = "Vorbis" },
_ => formatFinal
};
codecsMapping.Add(cmc.Key, formatFinal);
}
}
}

/// <summary>
/// Constructor
/// </summary>
public MKA(string filePath, Format format)
{
FileName = filePath;
containerAudioFormat = format;
resetData();
}

protected void resetData()
{
SampleRate = 0;
Expand All @@ -285,16 +318,6 @@ protected void resetData()
seekHeads.Clear();
}

/// <summary>
/// Constructor
/// </summary>
public MKA(string filePath, Format format)
{
FileName = filePath;
containerAudioFormat = format;
resetData();
}

public static bool IsValidHeader(byte[] data) => EBML_MAGIC_NUMBER == StreamUtils.DecodeBEUInt32(data);


Expand Down Expand Up @@ -366,12 +389,7 @@ private bool readPhysicalData(EBMLReader reader)

var codecId = "";
if (reader.seekElement(0x86)) codecId = reader.readString().ToUpper(); // CodecID
if (codecsMapping.TryGetValue(codecId, out var value))
{
var formats = AudioDataIOFactory.GetInstance().getFormats();
var format = formats.Where(f => f.ID == value).ToList();
if (format.Count > 0) containeeAudioFormat = format[0];
}
if (codecsMapping.TryGetValue(codecId, out var value)) containeeAudioFormat = value;

reader.seek(trackEntryOffset);
if (reader.seekElement(0xE1)) audioOffset = reader.Position; // Audio
Expand Down

0 comments on commit b633f61

Please sign in to comment.