From cd35f787e3a2b38d92eeba06a6d15e14b7b78a5e Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sat, 28 Oct 2017 08:38:50 +0200 Subject: [PATCH] Read the WAVEFORMATEX info in mkv. --- MP42/MP42FormatUtilites.h | 33 ++++++++++ MP42/MP42FormatUtilites.mm | 130 +++++++++++++++++++++++++++++++++++++ MP42/MP42MkvImporter.m | 54 +++++++++++++-- 3 files changed, 213 insertions(+), 4 deletions(-) diff --git a/MP42/MP42FormatUtilites.h b/MP42/MP42FormatUtilites.h index d122578..d0fae02 100644 --- a/MP42/MP42FormatUtilites.h +++ b/MP42/MP42FormatUtilites.h @@ -24,6 +24,39 @@ extern "C" { CFDataRef createCookie_EAC3(void *context); void free_EAC3_context(void *context); + typedef struct { + uint16_t wFormatTag; + uint16_t nChannels; + uint32_t nSamplesPerSec; + uint32_t nAvgBytesPerSec; + uint16_t nBlockAlign; + uint16_t wBitsPerSample; + uint16_t cbSize; + } waveformatex_t; + + typedef struct { + uint32_t Data1; + uint16_t Data2; + uint16_t Data3; + uint8_t Data4[8]; + } waveformatex_guid_t; + + typedef struct { + waveformatex_t Format; + union { + uint16_t wValidBitsPerSample; + uint16_t wSamplesPerBlock; + uint16_t wReserved; + } Samples; + uint32_t dwChannelMask; + waveformatex_guid_t SubFormat; + } waveformatextensible_t; + + FourCharCode readWaveFormat(waveformatextensible_t *ex); + UInt32 readWaveChannelLayout(waveformatextensible_t *ex); + + int analyze_WAVEFORMATEX(const uint8_t *cookie, uint32_t cookieLen, waveformatextensible_t *ex); + typedef struct MPEG4AudioConfig { int object_type; int sampling_index; diff --git a/MP42/MP42FormatUtilites.mm b/MP42/MP42FormatUtilites.mm index dc8a660..768d0b8 100644 --- a/MP42/MP42FormatUtilites.mm +++ b/MP42/MP42FormatUtilites.mm @@ -8,6 +8,7 @@ #include "MP42FormatUtilites.h" #include "mbs.h" +#include "MP42MediaFormat.h" #import @@ -1012,6 +1013,135 @@ void free_EAC3_context(void *context) free(context); } +#pragma mark - WAVEFORMATEX + +#define WAVE_FORMAT_PCM 0x0001 +#define WAVE_FORMAT_IEEE_FLOAT 0x0003 +#define WAVE_FORMAT_ALAW 0x0006 +#define WAVE_FORMAT_MULAW 0x0007 +#define WAVE_FORMAT_EXTENSIBLE 0xFFFE + +#define SPEAKER_FRONT_LEFT 0x1 +#define SPEAKER_FRONT_RIGHT 0x2 +#define SPEAKER_FRONT_CENTER 0x4 +#define SPEAKER_LOW_FREQUENCY 0x8 +#define SPEAKER_BACK_LEFT 0x10 +#define SPEAKER_BACK_RIGHT 0x20 +#define SPEAKER_FRONT_LEFT_OF_CENTER 0x40 +#define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80 +#define SPEAKER_BACK_CENTER 0x100 +#define SPEAKER_SIDE_LEFT 0x200 +#define SPEAKER_SIDE_RIGHT 0x400 +#define SPEAKER_TOP_CENTER 0x800 +#define SPEAKER_TOP_FRONT_LEFT 0x1000 +#define SPEAKER_TOP_FRONT_CENTER 0x2000 +#define SPEAKER_TOP_FRONT_RIGHT 0x4000 +#define SPEAKER_TOP_BACK_LEFT 0x8000 +#define SPEAKER_TOP_BACK_CENTER 0x10000 +#define SPEAKER_TOP_BACK_RIGHT 0x20000 + +#define KSAUDIO_SPEAKER_DIRECTOUT 0 //(no speakers) +#define KSAUDIO_SPEAKER_MONO (SPEAKER_FRONT_CENTER) +#define KSAUDIO_SPEAKER_STEREO (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT) +#define KSAUDIO_SPEAKER_QUAD (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT) +#define KSAUDIO_SPEAKER_SURROUND (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_BACK_CENTER) +#define KSAUDIO_SPEAKER_5POINT1 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT) +#define KSAUDIO_SPEAKER_5POINT1_SURROUND (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT) +#define KSAUDIO_SPEAKER_7POINT1 (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_FRONT_LEFT_OF_CENTER | SPEAKER_FRONT_RIGHT_OF_CENTER) +#define KSAUDIO_SPEAKER_7POINT1_SURROUND (SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT) + +FourCharCode readWaveFormat(waveformatextensible_t *ex) { + uint32_t format = 0; + + if (ex->Format.wFormatTag == 0xFFFE) { + format = ex->SubFormat.Data1; + } else { + format = ex->Format.wFormatTag; + } + + switch (format) { + case WAVE_FORMAT_PCM: + case WAVE_FORMAT_IEEE_FLOAT: + return kMP42AudioCodecType_LinearPCM; + + case WAVE_FORMAT_ALAW: + return kMP42AudioCodecType_ALaw; + + case WAVE_FORMAT_MULAW: + return kMP42AudioCodecType_ULaw; + + default: + return kMP42MediaType_Unknown; + } +} + +UInt32 readWaveChannelLayout(waveformatextensible_t *ex) +{ + if (ex->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) { + switch (ex->dwChannelMask) { + case KSAUDIO_SPEAKER_DIRECTOUT: + return 0; + case KSAUDIO_SPEAKER_MONO: + return kAudioChannelLayoutTag_Mono; + case KSAUDIO_SPEAKER_STEREO: + return kAudioChannelLayoutTag_Stereo; + case KSAUDIO_SPEAKER_QUAD: + return kAudioChannelLayoutTag_Quadraphonic; + case KSAUDIO_SPEAKER_SURROUND: + return kAudioChannelLayoutTag_MPEG_4_0_A; + case KSAUDIO_SPEAKER_5POINT1: + return kAudioChannelLayoutTag_MPEG_5_1_A; + case KSAUDIO_SPEAKER_5POINT1_SURROUND: + return kAudioChannelLayoutTag_MPEG_5_1_A; + case KSAUDIO_SPEAKER_7POINT1: + return kAudioChannelLayoutTag_MPEG_7_1_A; + case KSAUDIO_SPEAKER_7POINT1_SURROUND: + return kAudioChannelLayoutTag_MPEG_7_1_A; + } + } + return 0; +} + +int analyze_WAVEFORMATEX(const uint8_t *cookie, uint32_t cookieLen, waveformatextensible_t *ex) +{ + CMemoryBitstream b; + b.SetBytes((u_int8_t *)cookie, cookieLen); + bzero(ex, sizeof(waveformatextensible_t)); + + try { + ex->Format.wFormatTag = EndianU16_BtoN(b.GetBits(16)); + ex->Format.nChannels = EndianU16_BtoN(b.GetBits(16)); + ex->Format.nSamplesPerSec = EndianU32_BtoN(b.GetBits(32)); + ex->Format.nAvgBytesPerSec = EndianU32_BtoN(b.GetBits(32)); + ex->Format.nBlockAlign = EndianU16_BtoN(b.GetBits(16)); + ex->Format.wBitsPerSample = EndianU16_BtoN(b.GetBits(16)); + ex->Format.cbSize = EndianU16_BtoN(b.GetBits(16)); + + if (ex->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) { + ex->Samples.wValidBitsPerSample = EndianU16_BtoN(b.GetBits(16)); + ex->dwChannelMask = EndianU32_BtoN(b.GetBits(32)); + + ex->SubFormat.Data1 = EndianU32_BtoN(b.GetBits(32)); + ex->SubFormat.Data2 = EndianU16_BtoN(b.GetBits(16)); + ex->SubFormat.Data3 = EndianU16_BtoN(b.GetBits(16)); + + ex->SubFormat.Data4[0] = b.GetBits(8); + ex->SubFormat.Data4[1] = b.GetBits(8); + ex->SubFormat.Data4[2] = b.GetBits(8); + ex->SubFormat.Data4[3] = b.GetBits(8); + ex->SubFormat.Data4[4] = b.GetBits(8); + ex->SubFormat.Data4[5] = b.GetBits(8); + ex->SubFormat.Data4[6] = b.GetBits(8); + ex->SubFormat.Data4[7] = b.GetBits(8); + } + } + catch (int e) { + return 1; + } + + return 0; +} + #pragma mark - MPEG 4 Audio #define ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0])) diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index e82e3ae..163cd14 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -284,7 +284,7 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui else if (mkvTrack->Type == TT_AUDIO) { MP42AudioTrack *audioTrack = [[MP42AudioTrack alloc] init]; audioTrack.channels = mkvTrack->AV.Audio.Channels; - audioTrack.channelLayoutTag = getDefaultChannelLayout(mkvTrack->AV.Audio.Channels); + audioTrack.channelLayoutTag = channelLayout(mkvTrack); audioTrack.alternateGroup = 1; for (MP42Track *track in self.tracks) { @@ -518,6 +518,16 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui return sizes; } +static UInt32 channelLayout(TrackInfo *trackInfo) { + if (!strcmp(trackInfo->CodecID, "A_MS/ACM")) { + waveformatextensible_t ex; + if (!analyze_WAVEFORMATEX(trackInfo->CodecPrivate, trackInfo->CodecPrivateSize, &ex)) { + return readWaveChannelLayout(&ex); + } + } + return getDefaultChannelLayout(trackInfo->AV.Audio.Channels); +} + // List of codec IDs we know about and that map to fourccs static const struct { const char *codecID; @@ -569,11 +579,19 @@ static const struct { static FourCharCode CodecIDToFourCC(TrackInfo *track) { - for (int i = 0; kCodecIDMap[i].fourCC != kMP42MediaType_Unknown; i++) { - if (!strcmp(track->CodecID, kCodecIDMap[i].codecID)) { - return kCodecIDMap[i].fourCC; + if (!strcmp(track->CodecID, "A_MS/ACM")) { + waveformatextensible_t ex; + if (!analyze_WAVEFORMATEX(track->CodecPrivate, track->CodecPrivateSize, &ex)) { + return readWaveFormat(&ex); + } + } else { + for (int i = 0; kCodecIDMap[i].fourCC != kMP42MediaType_Unknown; i++) { + if (!strcmp(track->CodecID, kCodecIDMap[i].codecID)) { + return kCodecIDMap[i].fourCC; + } } } + return kMP42MediaType_Unknown; } @@ -774,6 +792,34 @@ static NSString * TrackNameToString(TrackInfo *track) result.mFormatID = kAudioFormatLinearPCM; result.mFormatFlags += kAudioFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked; } + else if (!strcmp(trackInfo->CodecID, "A_MS/ACM")) { + waveformatextensible_t ex; + + if (!analyze_WAVEFORMATEX(trackInfo->CodecPrivate, trackInfo->CodecPrivateSize, &ex)) { + + if (ex.Format.wFormatTag == 0xFFFE) { + if (ex.SubFormat.Data1 == 0x0001) { + result.mFormatID = kAudioFormatLinearPCM; + result.mFormatFlags += kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; + } else if (ex.SubFormat.Data1 == 0x0003) { + result.mFormatID = kAudioFormatLinearPCM; + result.mFormatFlags += kAudioFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked; + } + result.mSampleRate = ex.Format.nSamplesPerSec; + result.mBitsPerChannel = ex.Samples.wSamplesPerBlock; + } else { + if (ex.SubFormat.Data1 == 0x0001) { + result.mFormatID = kAudioFormatLinearPCM; + result.mFormatFlags += kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; + } else if (ex.SubFormat.Data1 == 0x0003) { + result.mFormatID = kAudioFormatLinearPCM; + result.mFormatFlags += kAudioFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked; + } + result.mSampleRate = ex.Format.nSamplesPerSec; + result.mBitsPerChannel = ex.Samples.wSamplesPerBlock; + } + } + } return result; }