Update matroska parser.

This commit is contained in:
Damiano Galassi committed 2020-09-21 13:09:46 +02:00
1 parent 7e14b78afa
commit 8235bfab97
3 files changed
+1840 -1308

No files matched your search

+118 -176
View File
@@ -23,33 +23,19 @@
#define SCALE_FACTOR 1000000.f #define SCALE_FACTOR 1000000.f
MP42_OBJC_DIRECT_MEMBERS
@interface MatroskaSample : NSObject {
@public
unsigned long long startTime;
unsigned long long endTime;
unsigned long long filePos;
unsigned int frameSize;
unsigned int frameFlags;
}
@end
@implementation MatroskaSample
@end
MP42_OBJC_DIRECT_MEMBERS MP42_OBJC_DIRECT_MEMBERS
@interface MatroskaDemuxHelper : NSObject { @interface MatroskaDemuxHelper : NSObject {
@public @public
MP42TrackId sourceID; MP42TrackId sourceID;
TrackInfo *trackInfo; TrackInfo *trackInfo;
NSMutableArray<MatroskaSample *> *queue; NSMutableArray<MP42SampleBuffer *> *queue;
uint32_t timescale; uint32_t timescale;
uint64_t currentTime; uint64_t currentTime;
uint64_t startTime; uint64_t startTime;
int64_t minDisplayOffset; int64_t minDisplayOffset;
uint32_t buffer, samplesWritten, bufferFlush; uint32_t buffer, samplesWritten, bufferFlush;
MP42SampleBuffer *previousSample; MP42SampleBuffer *previousSample;
} }
@@ -59,7 +45,8 @@ MP42_OBJC_DIRECT_MEMBERS
- (instancetype)init - (instancetype)init
{ {
if ((self = [super init])) { self = [super init];
if (self) {
queue = [[NSMutableArray alloc] init]; queue = [[NSMutableArray alloc] init];
startTime = INT64_MAX; startTime = INT64_MAX;
} }
@@ -103,18 +90,11 @@ static int readData(struct StdIoStream *ioStream, uint64_t pos, uint8_t **data,
return 1; return 1;
} }
static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t FilePos, uint8_t** frame, uint32_t *FrameSize) static int copyMkvPacket(TrackInfo *trackInfo, char **Frame, uint32_t *FrameSize)
{ {
uint8_t *packet = NULL; uint8_t *packet = NULL;
uint32_t iSize = *FrameSize; uint32_t iSize = *FrameSize;
if (fseeko(ioStream->fp, FilePos, SEEK_SET)) {
#ifdef DEBUG
fprintf(stderr,"fseeko(): %s\n", strerror(errno));
#endif
return 0;
}
if (trackInfo->CompMethodPrivateSize != 0) { if (trackInfo->CompMethodPrivateSize != 0) {
packet = malloc(iSize + trackInfo->CompMethodPrivateSize); packet = malloc(iSize + trackInfo->CompMethodPrivateSize);
memcpy(packet, trackInfo->CompMethodPrivate, trackInfo->CompMethodPrivateSize); memcpy(packet, trackInfo->CompMethodPrivate, trackInfo->CompMethodPrivateSize);
@@ -128,29 +108,7 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui
return 0; return 0;
} }
size_t rd = fread(packet + trackInfo->CompMethodPrivateSize, 1, iSize, ioStream->fp); memcpy(packet + trackInfo->CompMethodPrivateSize, *Frame, iSize);
if (rd != iSize) {
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
}
free(packet);
return 0;
}
iSize += trackInfo->CompMethodPrivateSize; iSize += trackInfo->CompMethodPrivateSize;
if (trackInfo->CompEnabled) { if (trackInfo->CompEnabled) {
@@ -178,7 +136,7 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui
} }
} }
*frame = packet; *Frame = (char *)packet;
*FrameSize = iSize; *FrameSize = iSize;
return 1; return 1;
@@ -481,15 +439,17 @@ MP42_OBJC_DIRECT_MEMBERS
- (NSArray<NSNumber *> *)approximatedTrackDataLength - (NSArray<NSNumber *> *)approximatedTrackDataLength
{ {
SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile); SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile);
NSInteger trackCount = mkv_GetNumTracks(_matroskaFile); unsigned int trackCount = mkv_GetNumTracks(_matroskaFile);
uint64_t trackSizes[trackCount]; uint64_t trackSizes[trackCount];
uint64_t trackTimestamp[trackCount]; uint64_t trackTimestamp[trackCount];
uint64_t StartTime, EndTime, FilePos; uint64_t StartTime, EndTime, FilePos;
uint32_t Track, FrameSize, FrameFlags; uint32_t Track, FrameSize, FrameFlags;
int64_t FrameDiscard;
char *frame;
if (trackCount) { if (trackCount) {
for (int i = 0; i < trackCount; i++) { for (unsigned int i = 0; i < trackCount; i++) {
trackSizes[i] = 0; trackSizes[i] = 0;
trackTimestamp[i] = 0; trackTimestamp[i] = 0;
} }
@@ -497,17 +457,16 @@ MP42_OBJC_DIRECT_MEMBERS
StartTime = 0; StartTime = 0;
int i = 0; int i = 0;
while (StartTime < (segInfo->Duration / 64)) { while (StartTime < (segInfo->Duration / 64)) {
if (!mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags)) { if (!mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &frame, &FrameFlags, &FrameDiscard)) {
trackSizes[Track] += FrameSize; trackSizes[Track] += FrameSize;
trackTimestamp[Track] = StartTime; trackTimestamp[Track] = StartTime;
i++; i++;
} } else {
else {
break; break;
} }
} }
for (int j = 0; i < trackCount; i++) { for (unsigned int j = 0; i < trackCount; i++) {
if (trackTimestamp[j] > 0) { if (trackTimestamp[j] > 0) {
trackSizes[j] = trackSizes[j] * (segInfo->Duration / trackTimestamp[j]); trackSizes[j] = trackSizes[j] * (segInfo->Duration / trackTimestamp[j]);
} }
@@ -517,7 +476,7 @@ MP42_OBJC_DIRECT_MEMBERS
} }
NSMutableArray<NSNumber *> *sizes = [NSMutableArray array]; NSMutableArray<NSNumber *> *sizes = [NSMutableArray array];
for (int i = 0; i < trackCount; i++) { for (unsigned int i = 0; i < trackCount; i++) {
[sizes addObject:@(trackSizes[i])]; [sizes addObject:@(trackSizes[i])];
} }
@@ -551,6 +510,7 @@ static const struct {
{ "V_VP8", kMP42VideoCodecType_VP8 }, { "V_VP8", kMP42VideoCodecType_VP8 },
{ "V_VP9", kMP42VideoCodecType_VP9 }, { "V_VP9", kMP42VideoCodecType_VP9 },
{ "V_PRORES", kMP42VideoCodecType_AppleProRes422 }, { "V_PRORES", kMP42VideoCodecType_AppleProRes422 },
{ "V_AV1", kMP42VideoCodecType_AV1 },
{ "A_AAC", kMP42AudioCodecType_MPEG4AAC }, { "A_AAC", kMP42AudioCodecType_MPEG4AAC },
{ "A_AAC/MPEG2/LC", kMP42AudioCodecType_MPEG4AAC }, { "A_AAC/MPEG2/LC", kMP42AudioCodecType_MPEG4AAC },
@@ -572,7 +532,6 @@ static const struct {
{ "A_PCM/FLOAT/IEEE", kMP42AudioCodecType_LinearPCM }, { "A_PCM/FLOAT/IEEE", kMP42AudioCodecType_LinearPCM },
{ "A_ALAC", kMP42AudioCodecType_AppleLossless }, { "A_ALAC", kMP42AudioCodecType_AppleLossless },
{ "S_TEXT/UTF8", kMP42SubtitleCodecType_Text }, { "S_TEXT/UTF8", kMP42SubtitleCodecType_Text },
{ "S_TEXT/WEBVTT", kMP42SubtitleCodecType_WebVTT }, { "S_TEXT/WEBVTT", kMP42SubtitleCodecType_WebVTT },
{ "S_TEXT/ASS", kMP42SubtitleCodecType_SSA }, { "S_TEXT/ASS", kMP42SubtitleCodecType_SSA },
@@ -614,13 +573,15 @@ static NSString * TrackNameToString(TrackInfo *track)
{ {
uint64_t StartTime, EndTime, FilePos; uint64_t StartTime, EndTime, FilePos;
uint32_t Track, FrameSize, FrameFlags; uint32_t Track, FrameSize, FrameFlags;
int64_t FrameDiscard;
char *frame;
// mask other tracks because we don't need them // mask other tracks because we don't need them
unsigned long TrackMask = ~0; unsigned long TrackMask = ~0;
TrackMask &= ~(1l << Id); TrackMask &= ~(1l << Id);
mkv_SetTrackMask(_matroskaFile, TrackMask); mkv_SetTrackMask(_matroskaFile, TrackMask);
mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags); mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &frame, &FrameFlags, &FrameDiscard);
mkv_Seek(_matroskaFile, 0, 0); mkv_Seek(_matroskaFile, 0, 0);
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, Id); TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, Id);
@@ -683,35 +644,35 @@ static uint32_t timescale(TrackInfo *trackInfo)
else if (!strcmp(trackInfo->CodecID, "A_AC3") || !strcmp(trackInfo->CodecID, "A_EAC3")) { else if (!strcmp(trackInfo->CodecID, "A_AC3") || !strcmp(trackInfo->CodecID, "A_EAC3")) {
mkv_SetTrackMask(_matroskaFile, ~(1l << track.sourceId)); mkv_SetTrackMask(_matroskaFile, ~(1l << track.sourceId));
uint64_t StartTime, EndTime, FilePos; uint64_t StartTime, EndTime, FilePos;
uint32_t rt, FrameSize, FrameFlags; uint32_t rt, FrameSize, FrameFlags;
uint8_t *frame = NULL; int64_t FrameDiscard;
NSData *magicCookie = nil; char *Frame = NULL;
NSData *magicCookie = nil;
if (!strcmp(trackInfo->CodecID, "A_AC3")) { if (!strcmp(trackInfo->CodecID, "A_AC3")) {
// read first header to create track // read first header to create track
int firstFrame = mkv_ReadFrame(_matroskaFile, 0, &rt, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags); int firstFrame = mkv_ReadFrame(_matroskaFile, 0, &rt, &StartTime, &EndTime, &FilePos, &FrameSize, &Frame, &FrameFlags, &FrameDiscard);
if (firstFrame != 0) { if (firstFrame != 0) {
return nil; return nil;
} }
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) { if (copyMkvPacket(trackInfo, &Frame, &FrameSize)) {
// parse AC3 header // parse AC3 header
// collect all the necessary meta information // collect all the necessary meta information
uint64_t fscod, frmsizecod, bsid, bsmod, acmod, lfeon; uint64_t fscod, frmsizecod, bsid, bsmod, acmod, lfeon;
uint32_t lfe_offset = 4; uint32_t lfe_offset = 4;
fscod = (*(frame+4) >> 6) & 0x3; fscod = (*(Frame+4) >> 6) & 0x3;
frmsizecod = (*(frame+4) & 0x3f) >> 1; frmsizecod = (*(Frame+4) & 0x3f) >> 1;
bsid = (*(frame+5) >> 3) & 0x1f; bsid = (*(Frame+5) >> 3) & 0x1f;
bsmod = (*(frame+5) & 0xf); bsmod = (*(Frame+5) & 0xf);
acmod = (*(frame+6) >> 5) & 0x7; acmod = (*(Frame+6) >> 5) & 0x7;
if (acmod == 2) { if (acmod == 2) {
lfe_offset -= 2; lfe_offset -= 2;
} } else {
else {
if ((acmod & 1) && acmod != 1) { if ((acmod & 1) && acmod != 1) {
lfe_offset -= 2; lfe_offset -= 2;
} }
@@ -719,7 +680,7 @@ static uint32_t timescale(TrackInfo *trackInfo)
lfe_offset -= 2; lfe_offset -= 2;
} }
} }
lfeon = (*(frame+6) >> lfe_offset) & 0x1; lfeon = (*(Frame+6) >> lfe_offset) & 0x1;
NSMutableData *mutableCookie = [[NSMutableData alloc] init]; NSMutableData *mutableCookie = [[NSMutableData alloc] init];
[mutableCookie appendBytes:&fscod length:sizeof(uint64_t)]; [mutableCookie appendBytes:&fscod length:sizeof(uint64_t)];
@@ -731,7 +692,7 @@ static uint32_t timescale(TrackInfo *trackInfo)
magicCookie = mutableCookie; magicCookie = mutableCookie;
free(frame); free(Frame);
} }
mkv_Seek(_matroskaFile, 0, 0); mkv_Seek(_matroskaFile, 0, 0);
@@ -744,14 +705,14 @@ static uint32_t timescale(TrackInfo *trackInfo)
struct eac3_info *context = NULL; struct eac3_info *context = NULL;
SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile); SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile);
while (!mkv_ReadFrame(_matroskaFile, 0, &rt, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) && StartTime < (segInfo->Duration / 64)) while (!mkv_ReadFrame(_matroskaFile, 0, &rt, &StartTime, &EndTime, &FilePos, &FrameSize, &Frame, &FrameFlags, &FrameDiscard) && StartTime < (segInfo->Duration / 64))
{ {
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) { if (copyMkvPacket(trackInfo, &Frame, &FrameSize)) {
#ifdef DEBUG_PARSER #ifdef DEBUG_PARSER
printf("\n\nMP42MkvImporter.magicCookieForTrack 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); printf("\n\nMP42MkvImporter.magicCookieForTrack 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 #endif
analyze_EAC3((void *)&context, frame, FrameSize); analyze_EAC3((void *)&context, (uint8_t *)Frame, FrameSize);
free(frame); free(Frame);
} }
} }
@@ -796,18 +757,16 @@ static uint32_t timescale(TrackInfo *trackInfo)
if (!strcmp(trackInfo->CodecID, "A_EAC3")) { if (!strcmp(trackInfo->CodecID, "A_EAC3")) {
mkv_SetTrackMask(_matroskaFile, ~(1l << track.sourceId)); mkv_SetTrackMask(_matroskaFile, ~(1l << track.sourceId));
uint64_t StartTime, EndTime, FilePos; uint64_t StartTime, EndTime, FilePos;
uint32_t rt, FrameSize, FrameFlags; uint32_t rt, FrameSize, FrameFlags;
uint8_t *frame = NULL; int64_t FrameDiscard;
char *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, &frame, &FrameFlags, &FrameDiscard) && mkvpktnum++ < 5) {
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize))
{
#ifdef DEBUG_PARSER #ifdef DEBUG_PARSER
printf("\nMP42MkvImporter.streamExtensionTypeForAudioTrack 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); printf("\nMP42MkvImporter.streamExtensionTypeForAudioTrack 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 #endif
analyze_EAC3((void *)&context, frame, FrameSize); //NB! This routine allocates buffer for context, because we're passing NULL as input! analyze_EAC3((void *)&context, (uint8_t *)frame, FrameSize); //NB! This routine allocates buffer for context, because we're passing NULL as input!
}
} }
} }
@@ -911,13 +870,15 @@ static uint32_t timescale(TrackInfo *trackInfo)
// mask other tracks because we don't need them // mask other tracks because we don't need them
mkv_SetTrackMask(_matroskaFile, TrackMask); mkv_SetTrackMask(_matroskaFile, TrackMask);
uint64_t StartTime, EndTime, FilePos; uint64_t StartTime, EndTime, FilePos;
uint32_t Track, FrameSize, FrameFlags; uint32_t Track, FrameSize, FrameFlags;
uint8_t *frame = NULL; int64_t FrameDiscard;
MatroskaSample *frameSample = nil, *currentSample = nil; char *Frame = NULL;
int64_t duration, next_duration;
while (!mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) && !self.isCancelled) { MP42SampleBuffer *frameSample = nil, *currentSample = nil;
int64_t duration, next_duration;
while (!mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &Frame, &FrameFlags, &FrameDiscard) && !self.isCancelled) {
self.progress = (StartTime / _fileDuration / 10000); self.progress = (StartTime / _fileDuration / 10000);
MatroskaDemuxHelper *demuxHelper = helpers[Track]; MatroskaDemuxHelper *demuxHelper = helpers[Track];
@@ -928,12 +889,11 @@ static uint32_t timescale(TrackInfo *trackInfo)
} }
if (trackInfo->Type == TT_AUDIO) { if (trackInfo->Type == TT_AUDIO) {
demuxHelper->samplesWritten++;
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) { if (copyMkvPacket(trackInfo, &Frame, &FrameSize)) {
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
sample->data = frame; sample->data = Frame;
sample->size = FrameSize; sample->size = FrameSize;
sample->duration = MP4_INVALID_DURATION; sample->duration = MP4_INVALID_DURATION;
sample->offset = 0; sample->offset = 0;
@@ -978,15 +938,16 @@ static uint32_t timescale(TrackInfo *trackInfo)
#else #else
[self enqueue:sample]; [self enqueue:sample];
#endif #endif
demuxHelper->samplesWritten++;
} }
} }
if (trackInfo->Type == TT_SUB) { if (trackInfo->Type == TT_SUB) {
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) { if (copyMkvPacket(trackInfo, &Frame, &FrameSize)) {
if (strcmp(trackInfo->CodecID, "S_VOBSUB") && strcmp(trackInfo->CodecID, "S_HDMV/PGS")) { if (strcmp(trackInfo->CodecID, "S_VOBSUB") && strcmp(trackInfo->CodecID, "S_HDMV/PGS")) {
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
sample->data = frame; sample->data = Frame;
sample->size = FrameSize; sample->size = FrameSize;
sample->timescale = demuxHelper->timescale; sample->timescale = demuxHelper->timescale;
sample->duration = EndTime / SCALE_FACTOR - StartTime / SCALE_FACTOR; sample->duration = EndTime / SCALE_FACTOR - StartTime / SCALE_FACTOR;
@@ -995,16 +956,16 @@ static uint32_t timescale(TrackInfo *trackInfo)
sample->flags |= MP42SampleBufferFlagIsSync; sample->flags |= MP42SampleBufferFlagIsSync;
sample->trackId = demuxHelper->sourceID; sample->trackId = demuxHelper->sourceID;
demuxHelper->samplesWritten++;
[self enqueue:sample]; [self enqueue:sample];
demuxHelper->samplesWritten++;
} else { } else {
MP42SampleBuffer *nextSample = [[MP42SampleBuffer alloc] init]; MP42SampleBuffer *nextSample = [[MP42SampleBuffer alloc] init];
nextSample->timescale = demuxHelper->timescale; nextSample->timescale = demuxHelper->timescale;
nextSample->duration = 0; nextSample->duration = 0;
nextSample->offset = 0; nextSample->offset = 0;
nextSample->decodeTimestamp = StartTime; nextSample->decodeTimestamp = StartTime;
nextSample->data = frame; nextSample->data = Frame;
nextSample->size = FrameSize; nextSample->size = FrameSize;
nextSample->flags |= MP42SampleBufferFlagIsSync; nextSample->flags |= MP42SampleBufferFlagIsSync;
nextSample->trackId = demuxHelper->sourceID; nextSample->trackId = demuxHelper->sourceID;
@@ -1030,10 +991,11 @@ static uint32_t timescale(TrackInfo *trackInfo)
demuxHelper->previousSample->duration = (nextSample->decodeTimestamp - demuxHelper->previousSample->decodeTimestamp) / SCALE_FACTOR; demuxHelper->previousSample->duration = (nextSample->decodeTimestamp - demuxHelper->previousSample->decodeTimestamp) / SCALE_FACTOR;
} }
demuxHelper->samplesWritten++;
[self enqueue:demuxHelper->previousSample]; [self enqueue:demuxHelper->previousSample];
demuxHelper->previousSample = nextSample; demuxHelper->previousSample = nextSample;
demuxHelper->samplesWritten++;
} else if (!strcmp(trackInfo->CodecID, "S_VOBSUB")) { } else if (!strcmp(trackInfo->CodecID, "S_VOBSUB")) {
// VobSub seems to have an end duration, and no blank samples, so create a new one each time to fill the gaps // 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) { if (StartTime > demuxHelper->currentTime) {
@@ -1060,28 +1022,32 @@ static uint32_t timescale(TrackInfo *trackInfo)
else if (trackInfo->Type == TT_VIDEO) { else if (trackInfo->Type == TT_VIDEO) {
copyMkvPacket(trackInfo, &Frame, &FrameSize);
// read frames from file // read frames from file
frameSample = [[MatroskaSample alloc] init]; frameSample = [[MP42SampleBuffer alloc] init];
frameSample->startTime = StartTime; frameSample->data = Frame;
frameSample->endTime = EndTime; frameSample->size = FrameSize;
frameSample->filePos = FilePos; frameSample->timescale = demuxHelper->timescale;
frameSample->frameSize = FrameSize; frameSample->decodeTimestamp = StartTime;
frameSample->frameFlags = FrameFlags; frameSample->presentationOutputTimestamp = EndTime;
frameSample->flags = (FrameFlags & FRAME_KF) ? MP42SampleBufferFlagIsSync : 0;
frameSample->trackId = demuxHelper->sourceID;
[demuxHelper->queue addObject:frameSample]; [demuxHelper->queue addObject:frameSample];
if ([demuxHelper->queue count] < bufferSize) { if (demuxHelper->queue.count < bufferSize) {
continue; continue;
} else { } else {
currentSample = [demuxHelper->queue objectAtIndex:demuxHelper->buffer]; currentSample = [demuxHelper->queue objectAtIndex:demuxHelper->buffer];
// matroska stores only the start and end time, so we need to recreate // Matroska stores only the start and end time in decode order, so we need to recreate
// the frame duration and the offset from the start time, the end time is useless // the frame duration and the offset from the start time, the end time is useless
// duration calculation // duration calculation
duration = demuxHelper->queue.lastObject->startTime - currentSample->startTime; duration = demuxHelper->queue.lastObject->decodeTimestamp - currentSample->decodeTimestamp;
for (MatroskaSample *sample in demuxHelper->queue) { for (MP42SampleBuffer *sample in demuxHelper->queue) {
if (sample != currentSample && (sample->startTime >= currentSample->startTime)) { if (sample != currentSample && (sample->decodeTimestamp >= currentSample->decodeTimestamp)) {
if ((next_duration = (sample->startTime - currentSample->startTime)) < duration) { if ((next_duration = (sample->decodeTimestamp - currentSample->decodeTimestamp)) < duration) {
duration = next_duration; duration = next_duration;
} }
} }
@@ -1092,40 +1058,27 @@ static uint32_t timescale(TrackInfo *trackInfo)
} }
// offset calculation // offset calculation
int64_t offset = currentSample->startTime / 10000.0f - demuxHelper->currentTime / 10000.0f; int64_t offset = currentSample->decodeTimestamp / 10000.0f - demuxHelper->currentTime / 10000.0f;
demuxHelper->currentTime += duration; demuxHelper->currentTime += duration;
if (readMkvPacket(_ioStream, trackInfo, currentSample->filePos, &frame, &currentSample->frameSize)) { currentSample->duration = duration / 10000.0f;
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; currentSample->offset = offset;
sample->data = frame;
sample->size = currentSample->frameSize;
sample->timescale = demuxHelper->timescale;
sample->duration = duration / 10000.0f;
sample->offset = offset;
sample->decodeTimestamp = StartTime;
sample->flags |= (currentSample->frameFlags & FRAME_KF) ? MP42SampleBufferFlagIsSync : 0;
sample->trackId = demuxHelper->sourceID;
demuxHelper->samplesWritten++; // save the minimum offset, used later to keep all the offset values positive
if (currentSample->offset < demuxHelper->minDisplayOffset) {
// save the minimum offset, used later to keep all the offset values positive demuxHelper->minDisplayOffset = currentSample->offset;
if (sample->offset < demuxHelper->minDisplayOffset) {
demuxHelper->minDisplayOffset = sample->offset;
}
if (demuxHelper->buffer >= bufferSize) {
[demuxHelper->queue removeObjectAtIndex:0];
}
if (demuxHelper->buffer < bufferSize) {
demuxHelper->buffer++;
}
[self enqueue:sample];
} }
else {
continue; if (demuxHelper->buffer >= bufferSize) {
[demuxHelper->queue removeObjectAtIndex:0];
} }
if (demuxHelper->buffer < bufferSize) {
demuxHelper->buffer++;
}
demuxHelper->samplesWritten++;
[self enqueue:currentSample];
} }
} }
} }
@@ -1138,14 +1091,14 @@ static uint32_t timescale(TrackInfo *trackInfo)
while (demuxHelper->queue.count) { while (demuxHelper->queue.count) {
if (demuxHelper->bufferFlush == 1) { if (demuxHelper->bufferFlush == 1) {
// add a last sample to get the duration for the last frame // add a last sample to get the duration for the last frame
MatroskaSample *lastSample = [demuxHelper->queue lastObject]; MP42SampleBuffer *lastSample = demuxHelper->queue.lastObject;
for (MatroskaSample *sample in demuxHelper->queue) { for (MP42SampleBuffer *sample in demuxHelper->queue) {
if (sample->startTime > lastSample->startTime) { if (sample->decodeTimestamp > lastSample->decodeTimestamp) {
lastSample = sample; lastSample = sample;
} }
} }
frameSample = [[MatroskaSample alloc] init]; frameSample = [[MP42SampleBuffer alloc] init];
frameSample->startTime = lastSample->endTime; frameSample->decodeTimestamp = lastSample->presentationOutputTimestamp;
[demuxHelper->queue addObject:frameSample]; [demuxHelper->queue addObject:frameSample];
} }
currentSample = [demuxHelper->queue objectAtIndex:demuxHelper->buffer]; currentSample = [demuxHelper->queue objectAtIndex:demuxHelper->buffer];
@@ -1153,51 +1106,40 @@ static uint32_t timescale(TrackInfo *trackInfo)
// matroska stores only the start and end time, so we need to recreate // matroska stores only the start and end time, so we need to recreate
// the frame duration and the offset from the start time, the end time is useless // the frame duration and the offset from the start time, the end time is useless
// duration calculation // duration calculation
duration = demuxHelper->queue.lastObject->startTime - currentSample->startTime; duration = demuxHelper->queue.lastObject->decodeTimestamp - currentSample->decodeTimestamp;
for (MatroskaSample *sample in demuxHelper->queue) { for (MP42SampleBuffer *sample in demuxHelper->queue) {
if (sample != currentSample && (sample->startTime >= currentSample->startTime)) { if (sample != currentSample && (sample->decodeTimestamp >= currentSample->decodeTimestamp)) {
if ((next_duration = (sample->startTime - currentSample->startTime)) < duration) { if ((next_duration = (sample->decodeTimestamp - currentSample->decodeTimestamp)) < duration) {
duration = next_duration; duration = next_duration;
} }
} }
} }
// offset calculation // offset calculation
int64_t offset = currentSample->startTime / 10000.0f - demuxHelper->currentTime / 10000.0f; int64_t offset = currentSample->decodeTimestamp / 10000.0f - demuxHelper->currentTime / 10000.0f;
demuxHelper->currentTime += duration; demuxHelper->currentTime += duration;
if (readMkvPacket(_ioStream, trackInfo, currentSample->filePos, &frame, &currentSample->frameSize)) { currentSample->timescale = demuxHelper->timescale;
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; currentSample->duration = duration / 10000.0f;
sample->data = frame; currentSample->offset = offset;
sample->size = currentSample->frameSize;
sample->timescale = demuxHelper->timescale;
sample->duration = duration / 10000.0f;
sample->offset = offset;
sample->decodeTimestamp = StartTime;
sample->flags |= (currentSample->frameFlags & FRAME_KF) ? MP42SampleBufferFlagIsSync : 0;
sample->trackId = demuxHelper->sourceID;
demuxHelper->samplesWritten++;
// save the minimum offset, used later to keep the all the offset values positive // save the minimum offset, used later to keep the all the offset values positive
if (sample->offset < demuxHelper->minDisplayOffset) { if (currentSample->offset < demuxHelper->minDisplayOffset) {
demuxHelper->minDisplayOffset = sample->offset; demuxHelper->minDisplayOffset = currentSample->offset;
}
if (demuxHelper->buffer >= bufferSize) {
[demuxHelper->queue removeObjectAtIndex:0];
}
[self enqueue:sample];
demuxHelper->bufferFlush++;
if (demuxHelper->bufferFlush >= bufferSize - 1) {
break;
}
} }
else {
if (demuxHelper->buffer >= bufferSize) {
[demuxHelper->queue removeObjectAtIndex:0];
}
demuxHelper->samplesWritten++;
[self enqueue:currentSample];
demuxHelper->bufferFlush++;
if (demuxHelper->bufferFlush >= bufferSize - 1) {
break; break;
} }
} }
+1521 -977
View File
File diff suppressed because it is too large. Load diff
+201 -155
View File
@@ -24,13 +24,13 @@
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: MatroskaParser.h,v 1.22 2008/04/29 21:03:09 mike Exp $ * $Id: MatroskaParser.h,v 1.22 2008/04/29 21:03:09 mike Exp $
* *
*/ */
#ifndef MATROSKA_PARSER_H #ifndef MATROSKA_PARSER_H
#define MATROSKA_PARSER_H #define MATROSKA_PARSER_H
/* Random notes: /* Random notes:
* *
@@ -42,7 +42,7 @@
*/ */
#ifdef MPDLLBUILD #ifdef MPDLLBUILD
#define X __declspec(dllexport) #define X __declspec(dllexport)
#else #else
#ifdef MPDLL #ifdef MPDLL
#define X __declspec(dllimport) #define X __declspec(dllimport)
@@ -53,7 +53,7 @@
#endif #endif
#define MATROSKA_COMPRESSION_SUPPORT #define MATROSKA_COMPRESSION_SUPPORT
#define MATROSKA_INTEGER_ONLY //#define MATROSKA_INTEGER_ONLY
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -61,7 +61,7 @@ extern "C" {
/* 64-bit integers */ /* 64-bit integers */
#ifdef _WIN32_WCE #ifdef _WIN32_WCE
typedef signed __int64 longlong; typedef signed __int64 longlong;
typedef unsigned __int64 ulonglong; typedef unsigned __int64 ulonglong;
#else #else
typedef signed long long longlong; typedef signed long long longlong;
@@ -71,16 +71,16 @@ typedef unsigned long long ulonglong;
/* MKFLOATing point */ /* MKFLOATing point */
#ifdef MATROSKA_INTEGER_ONLY #ifdef MATROSKA_INTEGER_ONLY
typedef struct { typedef struct {
longlong v; longlong v;
} MKFLOAT; } MKFLOAT;
#else #else
typedef double MKFLOAT; typedef double MKFLOAT;
#endif #endif
/* generic I/O */ /* generic I/O */
struct InputStream { struct InputStream {
/* read bytes from stream */ /* read bytes from stream */
int (*read)(struct InputStream *cc,ulonglong pos,void *buffer,int count); int (*read)(struct InputStream *cc,ulonglong pos,void *buffer,int count);
/* scan for a four byte signature, bytes must be nonzero */ /* scan for a four byte signature, bytes must be nonzero */
longlong (*scan)(struct InputStream *cc,ulonglong start,unsigned signature); longlong (*scan)(struct InputStream *cc,ulonglong start,unsigned signature);
/* get cache size, this is used to cap readahead */ /* get cache size, this is used to cap readahead */
@@ -88,11 +88,11 @@ struct InputStream {
/* fetch last error message */ /* fetch last error message */
const char *(*geterror)(struct InputStream *cc); const char *(*geterror)(struct InputStream *cc);
/* memory allocation */ /* memory allocation */
void *(*memalloc)(struct InputStream *cc,size_t size); void *(*memalloc)(struct InputStream *cc,size_t size);
void *(*memrealloc)(struct InputStream *cc,void *mem,size_t newsize); void *(*memrealloc)(struct InputStream *cc,void *mem,size_t newsize);
void (*memfree)(struct InputStream *cc,void *mem); void (*memfree)(struct InputStream *cc,void *mem);
/* zero return causes parser to abort open */ /* zero return causes parser to abort open */
int (*progress)(struct InputStream *cc,ulonglong cur,ulonglong max); int (*progress)(struct InputStream *cc,ulonglong cur,ulonglong max);
/* get file size, optional, can be NULL or return -1 if filesize is unknown */ /* get file size, optional, can be NULL or return -1 if filesize is unknown */
longlong (*getfilesize)(struct InputStream *cc); longlong (*getfilesize)(struct InputStream *cc);
}; };
@@ -104,34 +104,36 @@ struct MatroskaFile; /* opaque */
typedef struct MatroskaFile MatroskaFile; typedef struct MatroskaFile MatroskaFile;
#define COMP_ZLIB 0 #define COMP_ZLIB 0
#define COMP_BZIP 1 #define COMP_BZIP 1
#define COMP_LZO1X 2 #define COMP_LZO1X 2
#define COMP_PREPEND 3 #define COMP_PREPEND 3
#define TT_VIDEO 1 #define TT_VIDEO 1
#define TT_AUDIO 2 #define TT_AUDIO 2
#define TT_SUB 17 #define TT_SUB 17
struct TrackInfo { struct TrackInfo {
unsigned char Number; unsigned char Number;
unsigned char Type; unsigned char Type;
unsigned char TrackOverlay; unsigned char TrackOverlay;
ulonglong UID; ulonglong UID;
ulonglong MinCache; ulonglong MinCache;
ulonglong MaxCache; ulonglong MaxCache;
ulonglong DefaultDuration; ulonglong DefaultDuration;
MKFLOAT TimecodeScale; ulonglong CodecDelay;
void *CodecPrivate; ulonglong SeekPreRoll;
unsigned CodecPrivateSize; MKFLOAT TimecodeScale;
unsigned int CodecDelay; void *CodecPrivate;
unsigned CompMethod; unsigned CodecPrivateSize;
void *CompMethodPrivate; unsigned CompMethod;
unsigned CompMethodPrivateSize; void *CompMethodPrivate;
unsigned MaxBlockAdditionID; unsigned CompMethodPrivateSize;
unsigned MaxBlockAdditionID;
unsigned int Enabled:1; unsigned int Enabled:1;
unsigned int Default:1; unsigned int Default:1;
unsigned int Forced:1;
unsigned int Lacing:1; unsigned int Lacing:1;
unsigned int DecodeAll:1; unsigned int DecodeAll:1;
unsigned int CompEnabled:1; unsigned int CompEnabled:1;
@@ -147,129 +149,162 @@ struct TrackInfo {
unsigned int DisplayHeight; unsigned int DisplayHeight;
unsigned int CropL, CropT, CropR, CropB; unsigned int CropL, CropT, CropR, CropB;
unsigned int ColourSpace; unsigned int ColourSpace;
MKFLOAT GammaValue; MKFLOAT GammaValue;
struct { struct {
unsigned int MatrixCoefficients; unsigned int MatrixCoefficients;
unsigned int BitsPerChannel; unsigned int BitsPerChannel;
unsigned int TransferCharacteristics; unsigned int ChromaSubsamplingHorz;
unsigned int Primaries; unsigned int ChromaSubsamplingVert;
unsigned int CbSubsamplingHorz;
unsigned int CbSubsamplingVert;
unsigned int ChromaSitingHorz;
unsigned int ChromaSitingVert;
unsigned int Range;
unsigned int TransferCharacteristics;
unsigned int Primaries;
unsigned int MaxCLL;
unsigned int MaxFALL;
struct {
float PrimaryRChromaticityX;
float PrimaryRChromaticityY;
float PrimaryGChromaticityX;
float PrimaryGChromaticityY;
float PrimaryBChromaticityX;
float PrimaryBChromaticityY;
float WhitePointChromaticityX;
float WhitePointChromaticityY;
float LuminanceMax;
float LuminanceMin;
} MasteringMetadata;
} Colour; } Colour;
unsigned int Interlaced:1; unsigned int Interlaced:1;
} Video; } Video;
struct { struct {
MKFLOAT SamplingFreq; MKFLOAT SamplingFreq;
MKFLOAT OutputSamplingFreq; MKFLOAT OutputSamplingFreq;
unsigned char Channels; unsigned char Channels;
unsigned char BitDepth; unsigned char BitDepth;
} Audio; } Audio;
} AV; } AV;
/* various strings */ /* various strings */
char *Name; char *Name;
char Language[4]; char Language[4];
char *CodecID; char *CodecID;
unsigned int NeedKeyframes;
}; };
typedef struct TrackInfo TrackInfo; typedef struct TrackInfo TrackInfo;
struct SegmentInfo { struct SegmentInfo {
char UID[16]; char UID[16];
char PrevUID[16]; char PrevUID[16];
char NextUID[16]; char NextUID[16];
char *Filename; char *Filename;
char *PrevFilename; char *PrevFilename;
char *NextFilename; char *NextFilename;
char *Title; char *Title;
char *MuxingApp; char *MuxingApp;
char *WritingApp; char *WritingApp;
ulonglong TimecodeScale; ulonglong TimecodeScale;
ulonglong Duration; ulonglong Duration;
longlong DateUTC; longlong DateUTC;
char DateUTCValid; char DateUTCValid;
}; };
typedef struct SegmentInfo SegmentInfo; typedef struct SegmentInfo SegmentInfo;
struct Attachment { struct Attachment {
ulonglong Position; ulonglong Position;
ulonglong Length; ulonglong Length;
ulonglong UID; ulonglong UID;
char *Name; char *Name;
char *Description; char *Description;
char *MimeType; char *MimeType;
}; };
typedef struct Attachment Attachment; typedef struct Attachment Attachment;
struct ChapterDisplay { struct ChapterDisplay {
char *String; char *String;
char Language[4]; char Language[4];
char Country[4]; char Country[4];
}; };
struct ChapterCommand { struct ChapterCommand {
unsigned Time; unsigned Time;
unsigned CommandLength; unsigned CommandLength;
void *Command; void *Command;
}; };
struct ChapterProcess { struct ChapterProcess {
unsigned CodecID; unsigned CodecID;
unsigned CodecPrivateLength; unsigned CodecPrivateLength;
void *CodecPrivate; void *CodecPrivate;
unsigned nCommands,nCommandsSize; unsigned nCommands,nCommandsSize;
struct ChapterCommand *Commands; struct ChapterCommand *Commands;
}; };
struct Chapter { struct Chapter {
ulonglong UID; ulonglong UID;
ulonglong Start; ulonglong Start;
ulonglong End; ulonglong End;
unsigned nTracks,nTracksSize; unsigned nTracks,nTracksSize;
ulonglong *Tracks; ulonglong *Tracks;
unsigned nDisplay,nDisplaySize; unsigned nDisplay,nDisplaySize;
struct ChapterDisplay *Display; struct ChapterDisplay *Display;
unsigned nChildren,nChildrenSize; unsigned nChildren,nChildrenSize;
struct Chapter *Children; struct Chapter *Children;
unsigned nProcess,nProcessSize; unsigned nProcess,nProcessSize;
struct ChapterProcess *Process; struct ChapterProcess *Process;
char SegmentUID[16]; char SegmentUID[16];
unsigned int Hidden:1; unsigned int Hidden:1;
unsigned int Enabled:1; unsigned int Enabled:1;
// Editions // Editions
unsigned int Default:1; unsigned int Default:1;
unsigned int Ordered:1; unsigned int Ordered:1;
}; };
typedef struct Chapter Chapter; typedef struct Chapter Chapter;
#define TARGET_TRACK 0 struct Cue {
#define TARGET_CHAPTER 1 ulonglong Time;
#define TARGET_ATTACHMENT 2 ulonglong Duration;
#define TARGET_EDITION 3 ulonglong Position;
ulonglong RelativePosition;
ulonglong Block;
unsigned char Track;
};
typedef struct Cue Cue;
#define TARGET_TRACK 0
#define TARGET_CHAPTER 1
#define TARGET_ATTACHMENT 2
#define TARGET_EDITION 3
struct Target { struct Target {
ulonglong UID; ulonglong UID;
unsigned Type; unsigned Type;
}; };
struct SimpleTag { struct SimpleTag {
char *Name; char *Name;
char *Value; char *Value;
char Language[4]; char Language[4];
unsigned Default:1; unsigned Default:1;
}; };
struct Tag { struct Tag {
unsigned nTargets,nTargetsSize; unsigned nTargets,nTargetsSize;
struct Target *Targets; struct Target *Targets;
unsigned nSimpleTags,nSimpleTagsSize; unsigned nSimpleTags,nSimpleTagsSize;
struct SimpleTag *SimpleTags; struct SimpleTag *SimpleTags;
}; };
@@ -279,21 +314,26 @@ typedef struct Tag Tag;
* io pointer is recorded inside MatroskaFile * io pointer is recorded inside MatroskaFile
*/ */
X MatroskaFile *mkv_Open(/* in */ InputStream *io, X MatroskaFile *mkv_Open(/* in */ InputStream *io,
/* out */ char *err_msg, /* out */ char *err_msg,
/* in */ unsigned msgsize); /* in */ unsigned msgsize);
#define MKVF_AVOID_SEEKS 1 /* use sequential reading only */ #define MKVF_AVOID_SEEKS 1 /* use sequential reading only */
X MatroskaFile *mkv_OpenEx(/* in */ InputStream *io, X MatroskaFile *mkv_OpenEx(/* in */ InputStream *io,
/* in */ ulonglong base, /* in */ ulonglong base,
/* in */ unsigned flags, /* in */ unsigned flags,
/* out */ char *err_msg, /* out */ char *err_msg,
/* in */ unsigned msgsize); /* in */ unsigned msgsize);
/* Open the file and only parse enough information to find the segment uid */
X MatroskaFile *mkv_OpenSparse(/* in */ InputStream *io,
/* out */ char *err_msg,
/* in */ unsigned msgsize);
/* Close and deallocate mf /* Close and deallocate mf
* NULL pointer is ok and is simply ignored * NULL pointer is ok and is simply ignored
*/ */
X void mkv_Close(/* in */ MatroskaFile *mf); X void mkv_Close(/* in */ MatroskaFile *mf);
/* Fetch the error message of the last failed operation */ /* Fetch the error message of the last failed operation */
X const char *mkv_GetLastError(/* in */ MatroskaFile *mf); X const char *mkv_GetLastError(/* in */ MatroskaFile *mf);
@@ -306,15 +346,17 @@ X unsigned int mkv_GetNumTracks(/* in */ MatroskaFile *mf);
X TrackInfo *mkv_GetTrackInfo(/* in */ MatroskaFile *mf,/* in */ unsigned track); X TrackInfo *mkv_GetTrackInfo(/* in */ MatroskaFile *mf,/* in */ unsigned track);
/* chapters, tags and attachments */ /* chapters, tags and attachments */
X void mkv_GetAttachments(/* in */ MatroskaFile *mf, X void mkv_GetAttachments(/* in */ MatroskaFile *mf,
/* out */ Attachment **at, /* out */ Attachment **at,
/* out */ unsigned *count); /* out */ unsigned *count);
X void mkv_GetChapters(/* in */ MatroskaFile *mf, X void mkv_GetChapters(/* in */ MatroskaFile *mf,
/* out */ Chapter **ch, /* out */ Chapter **ch,
/* out */ unsigned *count); /* out */ unsigned *count);
X void mkv_GetTags(/* in */ MatroskaFile *mf, X void mkv_GetTags(/* in */ MatroskaFile *mf,
/* out */ Tag **tag, /* out */ Tag **tag,
/* out */ unsigned *count); /* out */ unsigned *count);
X void mkv_GetCues(MatroskaFile *mf, Cue **cue, unsigned *count);
X ulonglong mkv_GetSegmentTop(MatroskaFile *mf); X ulonglong mkv_GetSegmentTop(MatroskaFile *mf);
@@ -323,51 +365,55 @@ X ulonglong mkv_GetSegmentTop(MatroskaFile *mf);
* all tracks are set to return EOF * all tracks are set to return EOF
* on next read * on next read
*/ */
#define MKVF_SEEK_TO_PREV_KEYFRAME 1 #define MKVF_SEEK_TO_PREV_KEYFRAME 1
#define MKVF_SEEK_TO_PREV_KEYFRAME_STRICT 2 #define MKVF_SEEK_TO_PREV_KEYFRAME_STRICT 2
X void mkv_Seek(/* in */ MatroskaFile *mf, X void mkv_Seek(/* in */ MatroskaFile *mf,
/* in */ ulonglong timecode /* in ns */, /* in */ ulonglong timecode /* in ns */,
/* in */ unsigned flags); /* in */ unsigned flags);
X void mkv_SkipToKeyframe(MatroskaFile *mf); X void mkv_Seek_CueAware(MatroskaFile *mf, ulonglong timecode, unsigned flags, unsigned fuzzy);
X void mkv_SkipToKeyframe(MatroskaFile *mf);
X ulonglong mkv_GetLowestQTimecode(MatroskaFile *mf); X ulonglong mkv_GetLowestQTimecode(MatroskaFile *mf);
X int mkv_TruncFloat(MKFLOAT f); X int mkv_TruncFloat(MKFLOAT f);
/************************************************************************* /*************************************************************************
* reading data, pull model * reading data, pull model
*/ */
/* frame flags */ /* frame flags */
#define FRAME_UNKNOWN_START 0x00000001 #define FRAME_UNKNOWN_START 0x00000001
#define FRAME_UNKNOWN_END 0x00000002 #define FRAME_UNKNOWN_END 0x00000002
#define FRAME_KF 0x00000004 #define FRAME_KF 0x00000004
#define FRAME_GAP 0x00800000 #define FRAME_GAP 0x00800000
#define FRAME_STREAM_MASK 0xff000000 #define FRAME_STREAM_MASK 0xff000000
#define FRAME_STREAM_SHIFT 24 #define FRAME_STREAM_SHIFT 24
/* This sets the masking flags for the parser, /* This sets the masking flags for the parser,
* masked tracks [with 1s in their bit positions] * masked tracks [with 1s in their bit positions]
* will be ignored when reading file data. * will be ignored when reading file data.
* This call discards all parsed and queued frames * This call discards all parsed and queued frames
*/ */
X void mkv_SetTrackMask(/* in */ MatroskaFile *mf,/* in */ unsigned long mask); X void mkv_SetTrackMask(/* in */ MatroskaFile *mf,/* in */ ulonglong mask);
/* Read one frame from the queue. /* Read one frame from the queue.
* mask specifies what tracks to ignore. * mask specifies what tracks to ignore.
* Returns -1 if there are no more frames in the specified * Returns -1 if there are no more frames in the specified
* set of tracks, 0 on success * set of tracks, 0 on success
*/ */
X int mkv_ReadFrame(/* in */ MatroskaFile *mf, X int mkv_ReadFrame(/* in */ MatroskaFile *mf,
/* in */ unsigned int mask, /* in */ ulonglong mask,
/* out */ unsigned int *track, /* out */ unsigned int *track,
/* out */ ulonglong *StartTime /* in ns */, /* out */ ulonglong *StartTime /* in ns */,
/* out */ ulonglong *EndTime /* in ns */, /* out */ ulonglong *EndTime /* in ns */,
/* out */ ulonglong *FilePos /* in bytes from start of file */, /* out */ ulonglong *FilePos /* in bytes from start of file */,
/* out */ unsigned int *FrameSize /* in bytes */, /* out */ unsigned int *FrameSize /* in bytes */,
/* out */ unsigned int *FrameFlags); /* out */ char **FrameData,
/* out */ unsigned int *FrameFlags,
/* out */ longlong *FrameDiscard);
#ifdef MATROSKA_COMPRESSION_SUPPORT #ifdef MATROSKA_COMPRESSION_SUPPORT
/* Compressed streams support */ /* Compressed streams support */
@@ -375,24 +421,24 @@ struct CompressedStream;
typedef struct CompressedStream CompressedStream; typedef struct CompressedStream CompressedStream;
X CompressedStream *cs_Create(/* in */ MatroskaFile *mf, X CompressedStream *cs_Create(/* in */ MatroskaFile *mf,
/* in */ unsigned tracknum, /* in */ unsigned tracknum,
/* out */ char *errormsg, /* out */ char *errormsg,
/* in */ unsigned msgsize); /* in */ unsigned msgsize);
X void cs_Destroy(/* in */ CompressedStream *cs); X void cs_Destroy(/* in */ CompressedStream *cs);
/* advance to the next frame in matroska stream, you need to pass values returned /* advance to the next frame in matroska stream, you need to pass values returned
* by mkv_ReadFrame */ * by mkv_ReadFrame */
X void cs_NextFrame(/* in */ CompressedStream *cs, X void cs_NextFrame(/* in */ CompressedStream *cs,
/* in */ ulonglong pos, /* in */ unsigned size,
/* in */ unsigned size); /* in */ char *frame_data);
/* read and decode more data from current frame, return number of bytes decoded, /* read and decode more data from current frame, return number of bytes decoded,
* 0 on end of frame, or -1 on error */ * 0 on end of frame, or -1 on error */
X int cs_ReadData(CompressedStream *cs,char *buffer,unsigned bufsize); X int cs_ReadData(CompressedStream *cs,char *buffer,unsigned bufsize);
/* return error message for the last error */ /* return error message for the last error */
X const char *cs_GetLastError(CompressedStream *cs); X const char *cs_GetLastError(CompressedStream *cs);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus