Fixes a crash in the eac3 header parser, add some preliminary work to read atmos in AVFoundation.
This commit is contained in:
5 files changed
+92
-25
No files matched your search
+70
-5
@@ -253,6 +253,7 @@
|
||||
|
||||
// Audio type, check the channel layout and channels number
|
||||
MP42AudioTrack *audioTrack = [[MP42AudioTrack alloc] init];
|
||||
UInt32 objectsCount = 0;
|
||||
|
||||
if (formatDescription) {
|
||||
size_t layoutSize = 0;
|
||||
@@ -264,13 +265,17 @@
|
||||
}
|
||||
else {
|
||||
// Guess the layout.
|
||||
const AudioStreamBasicDescription *asbd = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription);
|
||||
const AudioStreamBasicDescription *asbd = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription);
|
||||
audioTrack.channels = asbd->mChannelsPerFrame;
|
||||
audioTrack.channelLayoutTag = getDefaultChannelLayout(asbd->mChannelsPerFrame);
|
||||
}
|
||||
FourCharCode audioFormat = CMFormatDescriptionGetMediaSubType(formatDescription);
|
||||
if (audioFormat == kAudioFormatAC3 || audioFormat == kAudioFormatEnhancedAC3) {
|
||||
objectsCount = [self numObjectsForAudioTrack:audioTrack];
|
||||
}
|
||||
}
|
||||
audioTrack.embeddedAudioFormat = objectsCount > 0 ? kMP42EmbeddedAudioCodecType_Atmos : 0;
|
||||
newTrack = audioTrack;
|
||||
|
||||
}
|
||||
else if ([track.mediaType isEqualToString:AVMediaTypeSubtitle]) {
|
||||
|
||||
@@ -779,8 +784,19 @@
|
||||
cookieBuffer += 8;
|
||||
cookieSizeOut = cookieSizeOut - 8;
|
||||
|
||||
return [NSData dataWithBytes:cookieBuffer length:cookieSizeOut];
|
||||
}
|
||||
NSMutableData *ac3Info = [NSMutableData dataWithBytes:cookieBuffer length:cookieSizeOut];
|
||||
MP42AudioTrack *audiotrack = (MP42AudioTrack *)track;
|
||||
|
||||
if (audiotrack.embeddedAudioFormat == kMP42EmbeddedAudioCodecType_Atmos)
|
||||
{
|
||||
// uint8_t atmosversion = 1;
|
||||
// uint8_t numobjects = (uint8_t)(audiotrack.objects & 0xFF);
|
||||
// [ac3Info appendBytes:&atmosversion length:sizeof(uint8_t)];
|
||||
// [ac3Info appendBytes:&numobjects length:sizeof(uint8_t)];
|
||||
}
|
||||
|
||||
return ac3Info;
|
||||
}
|
||||
|
||||
else if (code == kAudioFormatAC3 ||
|
||||
code == 'ms \0') {
|
||||
@@ -841,7 +857,17 @@
|
||||
[ac3Info appendBytes:&lfeon length:sizeof(uint64_t)];
|
||||
[ac3Info appendBytes:&bit_rate_code length:sizeof(uint64_t)];
|
||||
|
||||
return ac3Info;
|
||||
// MP42AudioTrack *audiotrack = (MP42AudioTrack *)track;
|
||||
//
|
||||
// if (audiotrack.embeddedAudioFormat == kMP42EmbeddedAudioCodecType_Atmos)
|
||||
// {
|
||||
// uint8_t atmosversion = 1;
|
||||
// uint8_t numobjects = (uint8_t)(audiotrack.objects & 0xFF);
|
||||
// [ac3Info appendBytes:&atmosversion length:sizeof(uint8_t)];
|
||||
// [ac3Info appendBytes:&numobjects length:sizeof(uint8_t)];
|
||||
// }
|
||||
|
||||
return ac3Info;
|
||||
|
||||
} else if (cookieSizeOut) {
|
||||
return [NSData dataWithBytes:cookieBuffer length:cookieSizeOut];
|
||||
@@ -1273,4 +1299,43 @@
|
||||
return @"AVFoundation demuxer";
|
||||
}
|
||||
|
||||
- (UInt32)numObjectsForAudioTrack:(MP42AudioTrack *)track
|
||||
{
|
||||
AudioFileID audioFile;
|
||||
uint8_t *syncframe;
|
||||
UInt32 ioNumPackets = 1;
|
||||
UInt32 ioNumBytes = 2 * 4096; //E-AC-3 max syncframe is 4096 bytes. If embedded as substream inside AC-3 frame, tandem will occupy 2560+4096 bytes
|
||||
AudioStreamPacketDescription outPacketDescriptions;
|
||||
SInt64 inStartingPacket = 0;
|
||||
UInt32 objectsCount = 0;
|
||||
EAC3Info *eac3Info = NULL;
|
||||
|
||||
syncframe = (uint8_t *)malloc(ioNumBytes);
|
||||
if (syncframe) {
|
||||
memset(syncframe, 0, ioNumBytes);
|
||||
OSStatus result = AudioFileOpenURL((__bridge CFURLRef _Nonnull)(self.fileURL), kAudioFileReadPermission, 0, &audioFile); //kAudioFileAC3Type
|
||||
if (result == noErr) {
|
||||
while(inStartingPacket < 10 && result == noErr) {
|
||||
result = AudioFileReadPacketData(audioFile, false, &ioNumBytes, &outPacketDescriptions, inStartingPacket, &ioNumPackets, syncframe);
|
||||
if(result == noErr) {
|
||||
#ifdef DEBUG_PARSER
|
||||
printf("\n\nMP42AVFimporter analyzing %s packet number %lld with size 0x%X (%u) bits\n", [self.fileURL.resourceSpecifier UTF8String], inStartingPacket, outPacketDescriptions.mDataByteSize, outPacketDescriptions.mDataByteSize);
|
||||
#endif
|
||||
analyze_EAC3((void *)&eac3Info, syncframe, ioNumBytes); //NB! This routine allocates buffer for eac3Info, because we're passing NULL as input!
|
||||
}
|
||||
inStartingPacket++;
|
||||
}
|
||||
AudioFileClose(audioFile);
|
||||
}
|
||||
free(syncframe);
|
||||
}
|
||||
|
||||
if (eac3Info) {
|
||||
objectsCount = get_num_objects_EAC3(eac3Info);
|
||||
free(eac3Info);
|
||||
}
|
||||
|
||||
return objectsCount;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -73,7 +73,7 @@ extern "C" {
|
||||
int frame_length_short;
|
||||
} MPEG4AudioConfig;
|
||||
|
||||
struct eac3_info {
|
||||
typedef struct eac3_info {
|
||||
uint8_t *frame;
|
||||
uint32_t size;
|
||||
|
||||
@@ -107,7 +107,7 @@ extern "C" {
|
||||
uint16_t chan_loc;
|
||||
/* if there is no dependent substream, then one bit reserved instead */
|
||||
} substream[1]; /* TODO: support 8 independent substreams */
|
||||
};
|
||||
} EAC3Info;
|
||||
|
||||
int analyze_ESDS(MPEG4AudioConfig *c, const uint8_t *cookie, uint32_t cookieLen);
|
||||
|
||||
|
||||
@@ -803,16 +803,21 @@ static int ac3_parse_header(CMemoryBitstream &b, AC3HeaderInfo **phdr)
|
||||
hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
|
||||
}
|
||||
//location in stream - bsi().dialnorm(5)
|
||||
analyze_eac3_atmos(b, hdr);
|
||||
try {
|
||||
analyze_eac3_atmos(b, hdr);
|
||||
} catch (int e) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void analyze_eac3_atmos(CMemoryBitstream &b, AC3HeaderInfo *phdr)
|
||||
{
|
||||
if(phdr->bitstream_id != 16)
|
||||
if (phdr->bitstream_id != 16) {
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
uint32_t savedbitpos = b.GetBitPosition(); //calling routines assume bit pos in b after this fn returns!
|
||||
parse_eac3_bsi(b, phdr); //resets bit_pos to frame start + 2
|
||||
//parse_eac3_audfrm(b, phdr); //starts from bit_pos where bsi left off
|
||||
@@ -978,7 +983,7 @@ concatenate:
|
||||
void parse_eac3_bsi(CMemoryBitstream &b, AC3HeaderInfo *hdr)
|
||||
{
|
||||
#ifdef DEBUG_PARSER
|
||||
printf("***parse_eac3_bsi start bit pos: %d remaining bits: %d\n", gbc.GetBitPosition(), gbc.GetRemainingBits());
|
||||
printf("***parse_eac3_bsi start bit pos: %d remaining bits: %d\n", b.GetBitPosition(), b.GetRemainingBits());
|
||||
#endif
|
||||
b.SetBitPosition(16); //skip syncword
|
||||
b.SkipBits(2+3+11+2+2+3+1); //strmtyp,substreamid,frmsiz,fscod,numblkscod,acmod,lfeon
|
||||
@@ -1134,7 +1139,7 @@ void parse_eac3_bsi(CMemoryBitstream &b, AC3HeaderInfo *hdr)
|
||||
b.SkipBytes(addbsil); //addbsi
|
||||
}
|
||||
#ifdef DEBUG_PARSER
|
||||
printf("***parse_eac3_bsi end bit pos: %d remaining bits: %d\n", gbc.GetBitPosition(), gbc.GetRemainingBits());
|
||||
printf("***parse_eac3_bsi end bit pos: %d remaining bits: %d\n", b.GetBitPosition(), b.GetRemainingBits());
|
||||
#endif
|
||||
} /* end of bsi */
|
||||
|
||||
@@ -1884,7 +1889,6 @@ int analyze_ESDS(MPEG4AudioConfig *c, const uint8_t *cookie, uint32_t cookieLen)
|
||||
|
||||
} catch (int e) {
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
return specific_config_bitindex;
|
||||
|
||||
+9
-11
@@ -776,38 +776,36 @@ static NSString * TrackNameToString(TrackInfo *track)
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (uint8_t)numObjectsForAudioTrack:(MP42Track *)track mkvTrack:(TrackInfo *)trackInfo
|
||||
- (UInt32)numObjectsForAudioTrack:(MP42AudioTrack *)track mkvTrack:(TrackInfo *)trackInfo
|
||||
{
|
||||
//TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, track.trackId);
|
||||
struct eac3_info *context = NULL;
|
||||
uint8_t mkvpktnum = 0;
|
||||
uint8_t num_objects = 0;
|
||||
UInt32 num_objects = 0;
|
||||
|
||||
if (!strcmp(trackInfo->CodecID, "A_EAC3"))
|
||||
{
|
||||
if (!strcmp(trackInfo->CodecID, "A_EAC3")) {
|
||||
mkv_SetTrackMask(_matroskaFile, ~(1l << track.sourceId));
|
||||
|
||||
uint64_t StartTime, EndTime, FilePos;
|
||||
uint32_t rt, FrameSize, FrameFlags;
|
||||
uint8_t *frame = NULL;
|
||||
|
||||
while (!mkv_ReadFrame(_matroskaFile, 0, &rt, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) && mkvpktnum++ < 5)
|
||||
{
|
||||
while (!mkv_ReadFrame(_matroskaFile, 0, &rt, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) && mkvpktnum++ < 5) {
|
||||
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize))
|
||||
{
|
||||
#ifdef DEBUG_PARSER
|
||||
printf("\n\nAnalyzing MKV EAC3 packet number %d at filepos 0x%llX (%llu) with size 0x%X (%u) bits\n", mkvpktnum++, FilePos, FilePos, FrameSize * 8, FrameSize * 8);
|
||||
printf("\nMP42MkvImporter.numObjectsForAudioTrack analyzing MKV EAC3 packet number %d at filepos 0x%llX (%llu) with size 0x%X (%u) bits\n", mkvpktnum++, FilePos, FilePos, FrameSize * 8, FrameSize * 8);
|
||||
#endif
|
||||
analyze_EAC3((void *)&context, frame, FrameSize);
|
||||
analyze_EAC3((void *)&context, frame, FrameSize); //NB! This routine allocates buffer for context, because we're passing NULL as input!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (context) {
|
||||
num_objects = get_num_objects_EAC3(context);
|
||||
num_objects = get_num_objects_EAC3(context);
|
||||
free(context);
|
||||
}
|
||||
|
||||
return num_objects;
|
||||
return num_objects;
|
||||
}
|
||||
|
||||
- (AudioStreamBasicDescription)audioDescriptionForTrack:(MP42AudioTrack *)track
|
||||
|
||||
+1
-1
@@ -319,7 +319,7 @@
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// EAC-3 audio tack
|
||||
// EAC-3 audio track
|
||||
else if ([track isMemberOfClass:[MP42AudioTrack class]] && format == kMP42AudioCodecType_EnhancedAC3) {
|
||||
dstTrackId = MP4AddEAC3AudioTrack(_fileHandle, timeScale, magicCookie.bytes, magicCookie.length);
|
||||
|
||||
|
||||
Reference in new issue
Block a user