diff --git a/MP42/MP42AACImporter.mm b/MP42/MP42AACImporter.mm index 5722186..7ea36d0 100644 --- a/MP42/MP42AACImporter.mm +++ b/MP42/MP42AACImporter.mm @@ -801,7 +801,7 @@ static bool GetFirstHeader(FILE* inFile) break; } - u_int8_t* pConfig = NULL; + u_int8_t *pConfig = NULL; u_int32_t configLength = 0; MP4AV_AacGetConfiguration( @@ -811,8 +811,9 @@ static bool GetFirstHeader(FILE* inFile) samplesPerSecond, channelConfig); - [newTrack setChannels:channelConfig]; - [newTrack setDataLength:[[[[NSFileManager defaultManager] attributesOfItemAtPath:self.fileURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]]; + newTrack.timescale = samplesPerSecond; + newTrack.channels = channelConfig; + newTrack.dataLength = [[[NSFileManager.defaultManager attributesOfItemAtPath:self.fileURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]; aacInfo = [[NSMutableData alloc] init]; [aacInfo appendBytes:pConfig length:configLength]; @@ -824,16 +825,6 @@ static bool GetFirstHeader(FILE* inFile) return self; } -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - return samplesPerSecond; -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(0, 0); -} - - (NSData *)magicCookieForTrack:(MP42Track *)track { return aacInfo; diff --git a/MP42/MP42AC3Importer.m b/MP42/MP42AC3Importer.m index ac9847a..8b6d028 100644 --- a/MP42/MP42AC3Importer.m +++ b/MP42/MP42AC3Importer.m @@ -314,6 +314,7 @@ static bool GetFirstHeader(FILE* inFile) lfeon = (firstHeader[6] >> lfe_offset) & 0x1; samplesPerSecond = MP4AV_Ac3GetSamplingRate(firstHeader); + newTrack.timescale = samplesPerSecond; readAC3Config(acmod, lfeon, &channels, &channelLayoutTag); @@ -328,7 +329,7 @@ static bool GetFirstHeader(FILE* inFile) [ac3Info appendBytes:&lfeon length:sizeof(uint64_t)]; [ac3Info appendBytes:&frmsizecod length:sizeof(uint64_t)]; - [newTrack setDataLength:[[[[NSFileManager defaultManager] attributesOfItemAtPath:self.fileURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]]; + [newTrack setDataLength:[[[NSFileManager.defaultManager attributesOfItemAtPath:self.fileURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]]; [self addTrack:newTrack]; } @@ -336,16 +337,6 @@ static bool GetFirstHeader(FILE* inFile) return self; } -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - return samplesPerSecond; -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(0, 0); -} - - (NSData *)magicCookieForTrack:(MP42Track *)track { return ac3Info; diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index 06e4be9..dbf34d6 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -54,10 +54,6 @@ MP42_OBJC_DIRECT_MEMBERS @"ac3", @"eac3", @"ec3", @"webvtt", @"vtt", @"caf", @"aif", @"aiff", @"aifc", @"wav", @"flac"]; } -- (BOOL)supportsPreciseTimestamps { - return YES; -} - - (FourCharCode)formatForTrack:(AVAssetTrack *)track { FourCharCode result = 0; CMFormatDescriptionRef formatDescription = (__bridge CMFormatDescriptionRef)track.formatDescriptions.firstObject; @@ -337,6 +333,7 @@ MP42_OBJC_DIRECT_MEMBERS [self fourCCoverrideForAtmos:newTrack]; newTrack.trackId = track.trackID; newTrack.URL = self.fileURL; + newTrack.timescale = [self timescaleForTrack:track]; // Use the global duration if track duration is not available. CMTimeRange timeRange = track.timeRange; @@ -731,31 +728,21 @@ MP42_OBJC_DIRECT_MEMBERS } } -#pragma mark - Overrides - -- (UInt32)timescaleForTrack:(MP42Track *)track { - AVAssetTrack *assetTrack = [_localAsset trackWithTrackID:track.sourceId]; - +- (UInt32)timescaleForTrack:(AVAssetTrack *)track { // Prefer the asbd sample rate, naturalTimeScale might not be // the right one if we are reading for .ts - if ([assetTrack.mediaType isEqualToString:AVMediaTypeAudio] && assetTrack.naturalTimeScale == 90000) { - CMFormatDescriptionRef formatDescription = (__bridge CMFormatDescriptionRef)assetTrack.formatDescriptions.firstObject; + if ([track.mediaType isEqualToString:AVMediaTypeAudio] && track.naturalTimeScale == 90000) { + CMFormatDescriptionRef formatDescription = (__bridge CMFormatDescriptionRef)track.formatDescriptions.firstObject; if (formatDescription) { const AudioStreamBasicDescription *asbd = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription); return asbd->mSampleRate; } } - return assetTrack.naturalTimeScale; + return track.naturalTimeScale; } -- (NSSize)sizeForTrack:(MP42VideoTrack *)track { - if ([track isKindOfClass:[MP42VideoTrack class]]) { - return NSMakeSize(track.width, track.height); - } else { - return NSMakeSize(0, 0); - } -} +#pragma mark - Overrides - (NSData *)magicCookieForTrack:(MP42Track *)track { @@ -1005,6 +992,10 @@ MP42_OBJC_DIRECT_MEMBERS return result; } +- (BOOL)supportsPreciseTimestamps { + return YES; +} + - (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track { return YES; @@ -1036,14 +1027,13 @@ MP42_OBJC_DIRECT_MEMBERS if ([assetReader canAddOutput:assetReaderOutput]) { [assetReader addOutput:assetReaderOutput]; - } - else { + } else { NSLog(@"Unable to add the output to assetReader!"); } AVFDemuxHelper *demuxHelper = [[AVFDemuxHelper alloc] init]; demuxHelper->sourceID = track.sourceId; - demuxHelper->timescale = [self timescaleForTrack:track]; + demuxHelper->timescale = track.timescale; demuxHelper->format = track.format; demuxHelper->assetReaderOutput = assetReaderOutput; demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init]; @@ -1361,7 +1351,7 @@ MP42_OBJC_DIRECT_MEMBERS return nil; } -- (BOOL)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)fileHandle { +- (void)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)fileHandle { uint32_t timescale = MP4GetTimeScale(fileHandle); MP4Duration trackDuration = 0; @@ -1399,8 +1389,6 @@ MP42_OBJC_DIRECT_MEMBERS if (trackDuration) { MP4SetTrackIntegerProperty(fileHandle, trackId, "tkhd.duration", trackDuration); } - - return YES; } - (NSString *)description diff --git a/MP42/MP42AudioConverter.m b/MP42/MP42AudioConverter.m index b44ae81..24d8e8f 100644 --- a/MP42/MP42AudioConverter.m +++ b/MP42/MP42AudioConverter.m @@ -102,7 +102,7 @@ MP42_OBJC_DIRECT_MEMBERS { AudioStreamBasicDescription asbd; bzero(&asbd, sizeof(AudioStreamBasicDescription)); - asbd.mSampleRate = [track.importer timescaleForTrack:track];; + asbd.mSampleRate = track.timescale; asbd.mChannelsPerFrame = track.channels; if (track.format == kMP42AudioCodecType_LinearPCM) { diff --git a/MP42/MP42CCImporter.m b/MP42/MP42CCImporter.m index 7e9c44b..f8f044c 100644 --- a/MP42/MP42CCImporter.m +++ b/MP42/MP42CCImporter.m @@ -30,6 +30,7 @@ MP42_OBJC_DIRECT_MEMBERS newTrack.format = kMP42ClosedCaptionCodecType_CEA608; newTrack.URL = self.fileURL; + newTrack.timescale = 30000; [self addTrack:newTrack]; } @@ -37,16 +38,6 @@ MP42_OBJC_DIRECT_MEMBERS return self; } -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - return 30000; -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(track.trackWidth, track.trackHeight); -} - - (nullable NSData *)magicCookieForTrack:(MP42Track *)track { return nil; diff --git a/MP42/MP42FileImporter.h b/MP42/MP42FileImporter.h index 6c2c178..87697af 100644 --- a/MP42/MP42FileImporter.h +++ b/MP42/MP42FileImporter.h @@ -19,7 +19,6 @@ NS_ASSUME_NONNULL_BEGIN @class MP42Metadata; @class MP42Track; - @interface MP42FileImporter : NSObject + (NSArray *)supportedFileFormats; @@ -33,15 +32,15 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Override -- (UInt32)timescaleForTrack:(MP42Track *)track; -- (NSSize)sizeForTrack:(MP42VideoTrack *)track; - (nullable NSData *)magicCookieForTrack:(MP42Track *)track; - (AudioStreamBasicDescription)audioDescriptionForTrack:(MP42AudioTrack *)track; -- (BOOL)cleanUp:(MP42Track *)track fileHandle:(MP42FileHandle)fileHandle; - (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track; - (BOOL)supportsPreciseTimestamps; + +- (void)setup; - (void)demux; +- (void)cleanUp:(MP42Track *)track fileHandle:(MP42FileHandle)fileHandle; #pragma mark - Private diff --git a/MP42/MP42FileImporter.m b/MP42/MP42FileImporter.m index 8404598..de28b81 100644 --- a/MP42/MP42FileImporter.m +++ b/MP42/MP42FileImporter.m @@ -230,20 +230,6 @@ static NSArray *_supportedFileFormats; #pragma mark - Override -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)] - userInfo:nil]; -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)] - userInfo:nil]; -} - - (nullable NSData *)magicCookieForTrack:(MP42Track *)track { @throw [NSException exceptionWithName:NSInternalInconsistencyException @@ -258,6 +244,8 @@ static NSArray *_supportedFileFormats; userInfo:nil]; } +- (void)setup {} + - (void)demux { @throw [NSException exceptionWithName:NSInternalInconsistencyException @@ -275,9 +263,6 @@ static NSArray *_supportedFileFormats; return NO; } -- (BOOL)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)fileHandle -{ - return YES; -} +- (void)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)fileHandle {} @end diff --git a/MP42/MP42H264Importer.mm b/MP42/MP42H264Importer.mm index 91f27e6..ad1c66a 100644 --- a/MP42/MP42H264Importer.mm +++ b/MP42/MP42H264Importer.mm @@ -206,7 +206,6 @@ static uint8_t exp_golomb_bits[256] = { static const char *ProgName = "Subler"; static int Verbosity = 0; -#include "mp4v2.h" #include #include /* isdigit, isprint, isspace */ #include @@ -1415,7 +1414,7 @@ MP42_OBJC_DIRECT_MEMBERS newTrack.origLevel = newTrack.newLevel = level; } - [newTrack setDataLength:[[[[NSFileManager defaultManager] attributesOfItemAtPath:self.fileURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]]; + newTrack.dataLength = [[[NSFileManager.defaultManager attributesOfItemAtPath:self.fileURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]; [self addTrack:newTrack]; } @@ -1423,8 +1422,9 @@ MP42_OBJC_DIRECT_MEMBERS return self; } -- (UInt32)timescaleForTrack:(MP42Track *)track +- (void)setup { + MP42VideoTrack *track = self.tracks.firstObject; framerate_t *framerate; for (framerate = (framerate_t *)framerates; framerate->code; framerate++) { @@ -1436,12 +1436,7 @@ MP42_OBJC_DIRECT_MEMBERS timescale = framerate->timescale; mp4FrameDuration = framerate->duration; - return timescale; -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(track.trackWidth, track.trackHeight); + track.timescale = timescale; } - (NSData *)magicCookieForTrack:(MP42Track *)track @@ -1639,7 +1634,7 @@ MP42_OBJC_DIRECT_MEMBERS } } -- (BOOL)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)fileHandle +- (void)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)fileHandle { MP4TrackId trackId = track.trackId; @@ -1661,8 +1656,6 @@ MP42_OBJC_DIRECT_MEMBERS } DpbClean(&h264_dpb); - - return YES; } - (NSString *)description diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index 929bbe5..872a367 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -26,7 +26,7 @@ MP42_OBJC_DIRECT_MEMBERS @interface MatroskaDemuxHelper : NSObject { - @public +@public MP42TrackId sourceID; TrackInfo *trackInfo; @@ -56,93 +56,6 @@ MP42_OBJC_DIRECT_MEMBERS @end -static int readData(struct StdIoStream *ioStream, uint64_t pos, uint8_t **data, size_t dataSize) -{ - if (fseeko(ioStream->fp, pos, SEEK_SET)) { -#ifdef DEBUG - fprintf(stderr,"fseeko(): %s\n", strerror(errno)); -#endif - return 0; - } - - size_t rd = fread(*data, 1, dataSize, ioStream->fp); - - if (rd != dataSize) { - if (rd == 0) { - if (feof(ioStream->fp)) { -#ifdef DEBUG - fprintf(stderr,"Unexpected EOF while reading frame\n"); -#endif - } - else { -#ifdef DEBUG - fprintf(stderr,"Error reading frame: %s\n",strerror(errno)); -#endif - } - } else { -#ifdef DEBUG - fprintf(stderr,"Short read while reading frame\n"); -#endif - } - - return 0; - } - - return 1; -} - -static int copyMkvPacket(TrackInfo *trackInfo, char **Frame, uint32_t *FrameSize) -{ - uint8_t *packet = NULL; - uint32_t iSize = *FrameSize; - - if (trackInfo->CompMethodPrivateSize != 0) { - packet = malloc(iSize + trackInfo->CompMethodPrivateSize); - memcpy(packet, trackInfo->CompMethodPrivate, trackInfo->CompMethodPrivateSize); - } - else { - packet = malloc(iSize); - } - - if (packet == NULL) { - fprintf(stderr,"Out of memory\n"); - return 0; - } - - memcpy(packet + trackInfo->CompMethodPrivateSize, *Frame, iSize); - iSize += trackInfo->CompMethodPrivateSize; - - if (trackInfo->CompEnabled) { - switch (trackInfo->CompMethod) { - case COMP_ZLIB: - if (!DecompressZlib(&packet, &iSize)) { - free(packet); - return 0; - } - break; - - case COMP_BZIP: - if (!DecompressBzlib(&packet, &iSize)) { - free(packet); - return 0; - } - break; - - // Not Implemented yet - case COMP_LZO1X: - break; - - default: - break; - } - } - - *Frame = (char *)packet; - *FrameSize = iSize; - - return 1; -} - MP42_OBJC_DIRECT_MEMBERS @implementation MP42MkvImporter { @@ -158,10 +71,6 @@ MP42_OBJC_DIRECT_MEMBERS return @[@"mkv", @"mka", @"mks"]; } -- (BOOL)supportsPreciseTimestamps { - return YES; -} - - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError * __autoreleasing *)outError { if ((self = [super initWithURL:fileURL])) { @@ -298,6 +207,7 @@ MP42_OBJC_DIRECT_MEMBERS SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile); UInt64 scaledDuration = (UInt64)segInfo->Duration / SCALE_FACTOR * trackTimecodeScale; + newTrack.timescale = timescale(mkvTrack); newTrack.duration = scaledDuration; if (scaledDuration > _fileDuration) { @@ -350,6 +260,13 @@ MP42_OBJC_DIRECT_MEMBERS return self; } +- (void)dealloc +{ + closeMatroskaFile(_matroskaFile, _ioStream); +} + +#pragma mark - Metadata + - (void)addMetadataItem:(id)value identifier:(NSString *)identifier metadata:(MP42Metadata *)metadata { MP42MetadataItem *item = [MP42MetadataItem metadataItemWithIdentifier:identifier @@ -617,17 +534,6 @@ static uint32_t timescale(TrackInfo *trackInfo) return 1000; } -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, track.sourceId); - return timescale(trackInfo); -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(track.width, track.height); -} - - (NSData *)magicCookieForTrack:(MP42Track *)track { TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, track.sourceId); @@ -836,6 +742,10 @@ static uint32_t timescale(TrackInfo *trackInfo) return result; } +- (BOOL)supportsPreciseTimestamps { + return YES; +} + - (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track { TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, track.sourceId); @@ -851,7 +761,7 @@ static uint32_t timescale(TrackInfo *trackInfo) NSArray *inputTracks = self.inputTracks; NSUInteger tracksNumber = inputTracks.count; - MatroskaDemuxHelper * helpers[self.tracks.count]; + MatroskaDemuxHelper *helpers[self.tracks.count]; for (NSUInteger index = 0; index < tracksNumber; index += 1) { MP42Track *track = inputTracks[index]; @@ -859,7 +769,7 @@ static uint32_t timescale(TrackInfo *trackInfo) MatroskaDemuxHelper *demuxHelper = [[MatroskaDemuxHelper alloc] init]; demuxHelper->sourceID = track.sourceId; demuxHelper->trackInfo = mkv_GetTrackInfo(_matroskaFile, track.sourceId); - demuxHelper->timescale = timescale(demuxHelper->trackInfo); + demuxHelper->timescale = track.timescale; helpers[track.sourceId] = demuxHelper; [_helpers addObject:demuxHelper]; @@ -895,18 +805,19 @@ static uint32_t timescale(TrackInfo *trackInfo) MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; sample->data = Frame; sample->size = FrameSize; + sample->timescale = demuxHelper->timescale; sample->duration = MP4_INVALID_DURATION; - sample->offset = 0; sample->decodeTimestamp = StartTime; - sample->flags |= MP42SampleBufferFlagIsSync; + sample->flags = MP42SampleBufferFlagIsSync; sample->trackId = demuxHelper->sourceID; #define VARIABLE_AUDIO_RATE 1 #ifdef VARIABLE_AUDIO_RATE + double scaledStartTime = sample->decodeTimestamp * (double)mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000000000.f; + if (demuxHelper->previousSample) { - double convertedDuration = sample->decodeTimestamp * (double)mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000000000.f; - uint64_t sampleDuration = convertedDuration - demuxHelper->currentTime; + uint64_t sampleDuration = scaledStartTime - demuxHelper->currentTime; // MKV timestamps are a bit random, try to round them // to make the sample table in the mp4 smaller. @@ -931,7 +842,7 @@ static uint32_t timescale(TrackInfo *trackInfo) demuxHelper->currentTime += sampleDuration; } else { - demuxHelper->currentTime = sample->decodeTimestamp * (double)mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000000000.f; + demuxHelper->currentTime = scaledStartTime; } demuxHelper->previousSample = sample; @@ -951,9 +862,8 @@ static uint32_t timescale(TrackInfo *trackInfo) sample->size = FrameSize; sample->timescale = demuxHelper->timescale; sample->duration = EndTime / SCALE_FACTOR - StartTime / SCALE_FACTOR; - sample->offset = 0; sample->decodeTimestamp = StartTime / SCALE_FACTOR; - sample->flags |= MP42SampleBufferFlagIsSync; + sample->flags = MP42SampleBufferFlagIsSync; sample->trackId = demuxHelper->sourceID; demuxHelper->samplesWritten++; @@ -961,13 +871,11 @@ static uint32_t timescale(TrackInfo *trackInfo) } else { MP42SampleBuffer *nextSample = [[MP42SampleBuffer alloc] init]; - nextSample->timescale = demuxHelper->timescale; - nextSample->duration = 0; - nextSample->offset = 0; - nextSample->decodeTimestamp = StartTime; nextSample->data = Frame; nextSample->size = FrameSize; - nextSample->flags |= MP42SampleBufferFlagIsSync; + nextSample->timescale = demuxHelper->timescale; + nextSample->decodeTimestamp = StartTime; + nextSample->flags = MP42SampleBufferFlagIsSync; nextSample->trackId = demuxHelper->sourceID; // PGS are usually stored with just the start time, and blank samples to fill the gaps @@ -978,7 +886,7 @@ static uint32_t timescale(TrackInfo *trackInfo) demuxHelper->previousSample->duration = StartTime / SCALE_FACTOR; demuxHelper->previousSample->offset = 0; demuxHelper->previousSample->decodeTimestamp = StartTime; - demuxHelper->previousSample->flags |= MP42SampleBufferFlagIsSync; + demuxHelper->previousSample->flags = MP42SampleBufferFlagIsSync; demuxHelper->previousSample->trackId = demuxHelper->sourceID; } else { if (nextSample->decodeTimestamp < demuxHelper->previousSample->decodeTimestamp) { @@ -1000,11 +908,11 @@ static uint32_t timescale(TrackInfo *trackInfo) // VobSub seems to have an end duration, and no blank samples, so create a new one each time to fill the gaps if (StartTime > demuxHelper->currentTime) { MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; + sample->data = calloc(1, 2); + sample->size = 2; sample->timescale = demuxHelper->timescale; sample->duration = (StartTime - demuxHelper->currentTime) / SCALE_FACTOR; - sample->size = 2; - sample->data = calloc(1, 2); - sample->flags |= MP42SampleBufferFlagIsSync; + sample->flags = MP42SampleBufferFlagIsSync; sample->trackId = demuxHelper->sourceID; [self enqueue:sample]; @@ -1121,11 +1029,9 @@ static uint32_t timescale(TrackInfo *trackInfo) demuxHelper->currentTime += duration; - currentSample->timescale = demuxHelper->timescale; currentSample->duration = duration / 10000.0f; currentSample->offset = offset; - // save the minimum offset, used later to keep the all the offset values positive if (currentSample->offset < demuxHelper->minDisplayOffset) { demuxHelper->minDisplayOffset = currentSample->offset; @@ -1147,7 +1053,6 @@ static uint32_t timescale(TrackInfo *trackInfo) if (demuxHelper->previousSample) { if (trackInfo->Type == TT_SUB) { - demuxHelper->previousSample->timescale = demuxHelper->timescale; demuxHelper->previousSample->duration = 100; } @@ -1171,7 +1076,7 @@ static uint32_t timescale(TrackInfo *trackInfo) return nil; } -- (BOOL)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)fileHandle +- (void)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)fileHandle { MatroskaDemuxHelper *demuxHelper = [self helperWithTrackID:track.sourceId]; MP4TrackId trackId = track.trackId; @@ -1192,6 +1097,7 @@ static uint32_t timescale(TrackInfo *trackInfo) // Add an empty edit list if the first pts is not 0. + // FIXME if (demuxHelper->startTime > 0) { MP4Duration emptyDuration = MP4ConvertFromTrackDuration(fileHandle, trackId, demuxHelper->startTime / 10000.0f, @@ -1219,8 +1125,6 @@ static uint32_t timescale(TrackInfo *trackInfo) editDuration, 0); } - - return YES; } - (NSString *)description @@ -1228,9 +1132,97 @@ static uint32_t timescale(TrackInfo *trackInfo) return @"Matroska demuxer"; } -- (void)dealloc +static int readData(struct StdIoStream *ioStream, uint64_t pos, uint8_t **data, size_t dataSize) { - closeMatroskaFile(_matroskaFile, _ioStream); + if (fseeko(ioStream->fp, pos, SEEK_SET)) { +#ifdef DEBUG + fprintf(stderr,"fseeko(): %s\n", strerror(errno)); +#endif + return 0; + } + + size_t rd = fread(*data, 1, dataSize, ioStream->fp); + + if (rd != dataSize) { + if (rd == 0) { + if (feof(ioStream->fp)) { +#ifdef DEBUG + fprintf(stderr,"Unexpected EOF while reading frame\n"); +#endif + } + else { +#ifdef DEBUG + fprintf(stderr,"Error reading frame: %s\n",strerror(errno)); +#endif + } + } else { +#ifdef DEBUG + fprintf(stderr,"Short read while reading frame\n"); +#endif + } + + return 0; + } + + return 1; } +static int copyMkvPacket(TrackInfo *trackInfo, char **Frame, uint32_t *FrameSize) +{ + uint8_t *packet = NULL; + uint32_t iSize = *FrameSize; + + if (trackInfo->CompMethodPrivateSize || trackInfo->CompEnabled) { + if (trackInfo->CompMethodPrivateSize != 0) { + packet = malloc(iSize + trackInfo->CompMethodPrivateSize); + memcpy(packet, trackInfo->CompMethodPrivate, trackInfo->CompMethodPrivateSize); + } else { + packet = malloc(iSize); + } + + if (packet == NULL) { + fprintf(stderr,"Out of memory\n"); + free(*Frame); + return 0; + } + + memcpy(packet + trackInfo->CompMethodPrivateSize, *Frame, iSize); + iSize += trackInfo->CompMethodPrivateSize; + + free(*Frame); + + if (trackInfo->CompEnabled) { + switch (trackInfo->CompMethod) { + case COMP_ZLIB: + if (!DecompressZlib(&packet, &iSize)) { + free(packet); + return 0; + } + break; + + case COMP_BZIP: + if (!DecompressBzlib(&packet, &iSize)) { + free(packet); + return 0; + } + break; + + // Not Implemented yet + case COMP_LZO1X: + break; + + default: + break; + } + } + + *Frame = (char *)packet; + *FrameSize = iSize; + return 1; + } else { + return 1; + } +} + + @end diff --git a/MP42/MP42Mp4Importer.m b/MP42/MP42Mp4Importer.m index af1c395..03e6633 100644 --- a/MP42/MP42Mp4Importer.m +++ b/MP42/MP42Mp4Importer.m @@ -52,16 +52,6 @@ typedef struct MP4DemuxHelper { return self; } -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - return MP4GetTrackTimeScale(_fileHandle, track.sourceId); -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(track.width, track.height); -} - - (NSData *)magicCookieForTrack:(MP42Track *)track { if (!_fileHandle) { @@ -391,7 +381,7 @@ typedef struct MP4DemuxHelper { } } -- (BOOL)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)dstFileHandle +- (void)cleanUp:(MP42Track *)track fileHandle:(MP4FileHandle)dstFileHandle { MP4TrackId srcTrackId = track.sourceId; MP4TrackId dstTrackId = track.trackId; @@ -423,8 +413,6 @@ typedef struct MP4DemuxHelper { MP4AddTrackEdit(dstFileHandle, dstTrackId, MP4_INVALID_EDIT_ID, firstFrameOffset, editDuration, 0); } - - return YES; } - (NSString *)description diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index f911e24..37caaf1 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -83,7 +83,11 @@ MP42_OBJC_DIRECT_MEMBERS - (BOOL)setup:(NSError * __autoreleasing *)outError { - NSMutableArray *unsupportedTracks = [[NSMutableArray alloc] init];; + NSMutableArray *unsupportedTracks = [[NSMutableArray alloc] init]; + + for (MP42FileImporter *importer in self.fileImporters) { + [importer setup]; + } for (MP42Track *track in _activeTracks) { @@ -91,13 +95,11 @@ MP42_OBJC_DIRECT_MEMBERS FourCharCode format = track.format; MP4TrackId dstTrackId = 0; NSData *magicCookie = nil; - uint32_t timeScale = 0; + uint32_t timeScale = track.timescale; if (importer) { magicCookie = [importer magicCookieForTrack:track]; - timeScale = [importer timescaleForTrack:track]; - } - else { + } else { [unsupportedTracks addObject:track]; continue; } @@ -161,12 +163,10 @@ MP42_OBJC_DIRECT_MEMBERS continue; } - NSSize size = [importer sizeForTrack:(MP42VideoTrack *)track]; - uint8_t *avcCAtom = (uint8_t *)magicCookie.bytes; dstTrackId = MP4AddH264VideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, - size.width, size.height, + ((MP42VideoTrack *)track).width, ((MP42VideoTrack *)track).height, avcCAtom[1], // AVCProfileIndication avcCAtom[2], // profile_compat avcCAtom[3], // AVCLevelIndication @@ -222,9 +222,10 @@ MP42_OBJC_DIRECT_MEMBERS // Check whether we can use hvc1 or hev1 fourcc. bool completeness = 0; if (magicCookie.length && magicCookie.length < UINT32_MAX && !analyze_HEVC(magicCookie.bytes, (uint32_t)magicCookie.length, &completeness)) { - NSSize size = [importer sizeForTrack:(MP42VideoTrack *)track]; - dstTrackId = MP4AddH265VideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, size.width, size.height, completeness); + dstTrackId = MP4AddH265VideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, + ((MP42VideoTrack *)track).width, ((MP42VideoTrack *)track).height, + completeness); if (dstTrackId) { if (completeness) { @@ -449,7 +450,7 @@ MP42_OBJC_DIRECT_MEMBERS // WebVTT else if ([track isMemberOfClass:[MP42SubtitleTrack class]] && format ==kMP42SubtitleCodecType_WebVTT) { - NSSize videoSize = [importer sizeForTrack:(MP42SubtitleTrack *)track]; + NSSize videoSize = NSMakeSize(((MP42VideoTrack *)track).width, ((MP42VideoTrack *)track).height); for (id workingTrack in _activeTracks) if ([workingTrack isMemberOfClass:[MP42VideoTrack class]]) { @@ -477,7 +478,7 @@ MP42_OBJC_DIRECT_MEMBERS // Closed Caption text track else if ([track isMemberOfClass:[MP42ClosedCaptionTrack class]]) { - NSSize videoSize = [importer sizeForTrack:(MP42ClosedCaptionTrack *)track]; + NSSize videoSize = NSMakeSize(((MP42VideoTrack *)track).width, ((MP42VideoTrack *)track).height); for (id workingTrack in _activeTracks) if ([workingTrack isMemberOfClass:[MP42VideoTrack class]]) { diff --git a/MP42/MP42SSAImporter.m b/MP42/MP42SSAImporter.m index 18ddc33..af8ac4f 100644 --- a/MP42/MP42SSAImporter.m +++ b/MP42/MP42SSAImporter.m @@ -41,6 +41,7 @@ MP42_OBJC_DIRECT_MEMBERS track.format = kMP42SubtitleCodecType_3GText; track.URL = self.fileURL; + track.timescale = 1000; track.alternateGroup = 2; track.language = getFilenameLanguage((__bridge CFStringRef)self.fileURL.path); @@ -84,16 +85,6 @@ MP42_OBJC_DIRECT_MEMBERS return nil; } -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - return 1000; -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(track.trackWidth, track.trackHeight); -} - - (void)demux { @autoreleasepool { diff --git a/MP42/MP42SrtImporter.m b/MP42/MP42SrtImporter.m index 6103fab..b52ba55 100644 --- a/MP42/MP42SrtImporter.m +++ b/MP42/MP42SrtImporter.m @@ -35,6 +35,7 @@ MP42_OBJC_DIRECT_MEMBERS track.format = kMP42SubtitleCodecType_3GText; track.URL = self.fileURL; + track.timescale = 1000; track.alternateGroup = 2; track.language = getFilenameLanguage((__bridge CFStringRef)self.fileURL.path); @@ -93,16 +94,6 @@ MP42_OBJC_DIRECT_MEMBERS return nil; } -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - return 1000; -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(track.trackWidth, track.trackHeight); -} - - (void)demux { @autoreleasepool { diff --git a/MP42/MP42Track+Private.h b/MP42/MP42Track+Private.h index d6d8630..25c6928 100644 --- a/MP42/MP42Track+Private.h +++ b/MP42/MP42Track+Private.h @@ -26,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readwrite) MP42CodecType format; @property(nonatomic, readwrite) MP42MediaType mediaType; +@property(nonatomic, readwrite) uint32_t timescale; @property(nonatomic, readwrite) MP42Duration duration; @property(nonatomic, readwrite) uint32_t bitrate; @property(nonatomic, readwrite) uint64_t dataLength; diff --git a/MP42/MP42Track.m b/MP42/MP42Track.m index c19b048..a389f79 100644 --- a/MP42/MP42Track.m +++ b/MP42/MP42Track.m @@ -37,6 +37,7 @@ typedef struct muxer_helper { @property(nonatomic, readwrite) MP42CodecType format; @property(nonatomic, readwrite) MP42MediaType mediaType; +@property(nonatomic, readwrite) uint32_t timescale; @property(nonatomic, readwrite) MP42Duration duration; @property(nonatomic, readwrite) uint64_t dataLength; @property(nonatomic, readwrite) uint32_t bitrate; diff --git a/MP42/MP42VobSubImporter.m b/MP42/MP42VobSubImporter.m index c06aaf0..7e69e56 100644 --- a/MP42/MP42VobSubImporter.m +++ b/MP42/MP42VobSubImporter.m @@ -239,6 +239,7 @@ MP42_OBJC_DIRECT_MEMBERS newTrack.alternateGroup = 2; newTrack.trackId = count++; newTrack.language = [MP42Languages.defaultManager extendedTagForISO_639_1:track->language]; + newTrack.timescale = 1000; newTrack.duration = track->duration; [self addTrack:newTrack]; @@ -256,16 +257,6 @@ MP42_OBJC_DIRECT_MEMBERS return self; } -- (UInt32)timescaleForTrack:(MP42Track *)track -{ - return 1000; -} - -- (NSSize)sizeForTrack:(MP42VideoTrack *)track -{ - return NSMakeSize(track.trackWidth, track.trackHeight); -} - - (NSData *)magicCookieForTrack:(MP42Track *)track { SBVobSubTrack *vobTrack = [_VobSubTracks objectAtIndex:track.sourceId];