Another try at making the construction of new edit lists in the AVFoundation importer a bit better.

This commit is contained in:
Damiano Galassi committed 2014-06-29 14:30:47 +02:00
1 parent 64c95977ca
commit 78ccde9934
6 files changed
+229 -134

No files matched your search

+20 -132
View File
@@ -17,64 +17,27 @@
#import "MP42PrivateUtilities.h"
#import "MP42Track+Muxer.h"
#import "MP42EditListsReconstructor.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface AVFDemuxHelper : NSObject {
@public
CMTimeRange *edits;
uint64_t editsCount;
uint64_t editsSize;
BOOL editOpen;
int64_t currentTime;
int64_t delta;
AVAssetReaderOutput *assetReaderOutput;
}
- (void)startEditListAtTime:(CMTime)time;
- (void)endEditListAtTime:(CMTime)time empty:(BOOL)type;
- (BOOL)isEditListOpen;
MP42EditListsReconstructor *editsConstructor;
}
@end
@implementation AVFDemuxHelper
/**
* Starts a new edit
*/
- (void)startEditListAtTime:(CMTime)time {
if (editsSize <= editsCount) {
editsSize += 20;
edits = (CMTimeRange *) realloc(edits, sizeof(CMTimeRange) * editsSize);
}
edits[editsCount] = CMTimeRangeMake(time, kCMTimeInvalid);
editOpen = YES;
}
/**
* Closes a opened edit
*/
- (void)endEditListAtTime:(CMTime)time empty:(BOOL)type {
if (!editOpen)
return;
time.value -= edits[editsCount].start.value;
edits[editsCount].duration = time;
if (type)
edits[editsCount].start.value = -1;
if (edits[editsCount].duration.value > 0) {
editsCount++;
}
editOpen = NO;
}
/**
* Returns if there is an open edit list
*/
- (BOOL)isEditListOpen {
return editOpen;
- (void)dealloc {
[editsConstructor release];
[super dealloc];
}
@end
@@ -706,8 +669,6 @@
return nil;
}
#define SB_AVF_DEBUG
- (void)demux:(id)sender {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
@@ -734,6 +695,7 @@
track.muxer_helper->demuxer_context = [[AVFDemuxHelper alloc] init];
demuxHelper = track.muxer_helper->demuxer_context;
demuxHelper->assetReaderOutput = assetReaderOutput;
demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init];
totalDataLength += [track dataLength];
}
@@ -746,9 +708,6 @@
for (MP42Track *track in _inputTracks) {
demuxHelper = track.muxer_helper->demuxer_context;
AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput;
int32_t timescale = 0;
[demuxHelper startEditListAtTime:kCMTimeInvalid];
while (!_cancelled) {
CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
@@ -778,55 +737,7 @@
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_Default);
int64_t delta = presentationTimeStamp.value - currentOutputTimeStamp.value;
if (timescale == 0) {
timescale = duration.timescale;
}
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(NULL, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
CFDictionaryRef trimStart = NULL, trimEnd = NULL;
if (attachments) {
// Check if we have to trim the start or end of a sample
// If so it means we need to start/end an edit
if ((trimStart = CFDictionaryGetValue(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart))) {
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
trimStartTime = CMTimeConvertScale(trimStartTime, duration.timescale, kCMTimeRoundingMethod_Default);
CMTime editStart = CMTimeMake(demuxHelper->currentTime, duration.timescale);
editStart.value += trimStartTime.value;
currentOutputTimeStamp.value += -trimStartTime.value;
[demuxHelper startEditListAtTime:editStart];
}
if ((trimEnd = CFDictionaryGetValue(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd))) {
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
trimEndTime = CMTimeConvertScale(trimEndTime, duration.timescale, kCMTimeRoundingMethod_Default);
CMTime editEnd = CMTimeMake(demuxHelper->currentTime, duration.timescale);
editEnd.value += duration.value - trimEndTime.value;
[demuxHelper endEditListAtTime:editEnd empty:NO];
}
}
if (delta != demuxHelper->delta && currentOutputTimeStamp.value > demuxHelper->currentTime) {
if (![demuxHelper isEditListOpen]) {
CMTime editStart = CMTimeMake(demuxHelper->currentTime + delta, duration.timescale);
[demuxHelper startEditListAtTime:editStart];
CMTime editEnd = CMTimeMake(demuxHelper->currentTime - delta, duration.timescale);
[demuxHelper endEditListAtTime:editEnd empty:YES];
demuxHelper->currentTime += delta;
} else {
CMTime editEnd = CMTimeMake(demuxHelper->currentTime + currentOutputTimeStamp.value, duration.timescale);
[demuxHelper endEditListAtTime:editEnd empty:YES];
[demuxHelper startEditListAtTime:editEnd];
demuxHelper->currentTime += currentOutputTimeStamp.value;
}
demuxHelper->delta = delta;
}
#ifdef SB_AVF_DEBUG
NSLog(@"Delta: %lld", presentationTimeStamp.value - currentOutputTimeStamp.value);
@@ -840,10 +751,14 @@
sample->size = sampleSize;
sample->duration = duration.value;
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->timestamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer).value;
sample->presentationTimestamp = presentationTimeStamp.value;
sample->timestamp = currentOutputTimeStamp.value;
sample->timescale = duration.timescale;
sample->isSync = sync;
sample->trackId = track.sourceId;
sample->attachments = (void *)attachments;
[demuxHelper->editsConstructor addSample:sample];
[self enqueue:sample];
[sample release];
@@ -885,34 +800,6 @@
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
size_t bufferSize = CMBlockBufferGetDataLength(buffer);
// Check if we have to trim the start or end of a sample
// If so it means we need to start/end an edit
CFTypeRef trimStart = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_TrimDurationAtStart, kCMAttachmentMode_ShouldNotPropagate);
if (trimStart) {
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
trimStartTime = CMTimeConvertScale(trimStartTime, timingArrayOut[0].duration.timescale, kCMTimeRoundingMethod_Default);
CMTime editStart = CMTimeMake(demuxHelper->currentTime, timingArrayOut[0].duration.timescale);
editStart.value += trimStartTime.value;
[demuxHelper startEditListAtTime:editStart];
}
CFTypeRef trimEnd = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_TrimDurationAtEnd, kCMAttachmentMode_ShouldNotPropagate);
if (trimEnd) {
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
trimEndTime = CMTimeConvertScale(trimEndTime, timingArrayOut[0].duration.timescale, kCMTimeRoundingMethod_Default);
CMTime editEnd = CMTimeMake(demuxHelper->currentTime, timingArrayOut[0].duration.timescale);
editEnd.value += timingArrayOut[0].duration.value * samplesNum - trimEndTime.value;
[demuxHelper endEditListAtTime:editEnd empty:NO];
}
if (timescale == 0) {
timescale = timingArrayOut[0].duration.timescale;
}
int pos = 0;
for (int i = 0; i < samplesNum; i++) {
CMSampleTimingInfo sampleTimingInfo;
@@ -977,9 +864,12 @@
sample->duration = sampleTimingInfo.duration.value;
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->timestamp = sampleTimingInfo.presentationTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value;
sample->timescale = sampleTimingInfo.duration.timescale;
sample->isSync = sync;
sample->trackId = track.sourceId;
[demuxHelper->editsConstructor addSample:sample];
[self enqueue:sample];
[sample release];
@@ -1000,8 +890,8 @@
break;
}
}
// Closes the last edit
[demuxHelper endEditListAtTime:CMTimeMake(demuxHelper->currentTime, timescale) empty:NO];
[demuxHelper->editsConstructor done];
}
[assetReader release];
@@ -1028,8 +918,8 @@
AVFDemuxHelper *helper = inputTrack.muxer_helper->demuxer_context;
for (int i = 0; i < helper->editsCount; i++) {
CMTimeRange timeRange = helper->edits[i];
for (int i = 0; i < helper->editsConstructor.editsCount; i++) {
CMTimeRange timeRange = helper->editsConstructor.edits[i];
CMTime duration = CMTimeConvertScale(timeRange.duration, timescale, kCMTimeRoundingMethod_Default);
trackDuration += duration.value;
@@ -1042,8 +932,6 @@
if (trackDuration)
MP4SetTrackIntegerProperty(fileHandle, track.Id, "tkhd.duration", trackDuration);
free(helper->edits);
}
return YES;
}
+39
View File
@@ -0,0 +1,39 @@
//
// MP42EditListsConstructor.h
// MP42Foundation
//
// Created by Damiano Galassi on 29/06/14.
// Copyright (c) 2014 Damiano Galassi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import "MP42Heap.h"
#import "MP42Sample.h"
/**
* Analyzes the sample buffers of a track and tries to recreate an array of edits lists.
*/
@interface MP42EditListsReconstructor : NSObject {
@private
MP42Heap *_queue;
int64_t _currentTime;
int64_t _delta;
CMTimeScale _timescale;
int64_t _count;
CMTimeRange *edits;
uint64_t editsCount;
uint64_t editsSize;
BOOL editOpen;
}
- (void)addSample:(MP42SampleBuffer *)sample;
- (void)done;
@property (readonly, nonatomic) CMTimeRange *edits;
@property (readonly, nonatomic) uint64_t editsCount;
@end
+139
View File
@@ -0,0 +1,139 @@
//
// MP42EditListsConstructor.m
// MP42Foundation
//
// Created by Damiano Galassi on 29/06/14.
// Copyright (c) 2014 Damiano Galassi. All rights reserved.
//
#import "MP42EditListsReconstructor.h"
@implementation MP42EditListsReconstructor
@synthesize edits;
@synthesize editsCount;
- (instancetype)init {
self = [super init];
if (self) {
_queue = [[MP42Heap alloc] initWithCapacity:32 andComparator:^NSComparisonResult(id obj1, id obj2) {
return ((MP42SampleBuffer *)obj2)->presentationTimestamp - ((MP42SampleBuffer *)obj1)->presentationTimestamp;
}];
_count = 1;
}
return self;
}
- (void)addSample:(MP42SampleBuffer *)sample {
[sample retain];
[_queue insert:sample];
if (_timescale == 0) {
_timescale = sample->timescale;
[self startEditListAtTime:CMTimeMake(_currentTime, _timescale)];
}
if ([_queue isFull]) {
MP42SampleBuffer *extractedSample = [_queue extract];
[self analyzeSample:extractedSample];
[extractedSample release];
}
}
- (void)done {
while (![_queue isEmpty]) {
MP42SampleBuffer *extractedSample = [_queue extract];
[self analyzeSample:extractedSample];
[extractedSample release];
}
if ([self isEditListOpen]) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
}
}
- (void)analyzeSample:(MP42SampleBuffer *)sample {
if (sample->attachments) {
NSLog(@"Attachments found: %@", sample->attachments);
}
CFDictionaryRef trimStart = NULL, trimEnd = NULL;
if (sample->attachments) {
// Check if we have to trim the start or end of a sample
// If so it means we need to start/end an edit
if ((trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart))) {
if ([self isEditListOpen])
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default);
CMTime editStart = CMTimeMake(_currentTime, _timescale);
editStart.value += trimStartTime.value;
[self startEditListAtTime:editStart];
}
}
_currentTime += sample->duration;
_delta = sample->presentationTimestamp - sample->timestamp;
if (sample->attachments) {
if ((trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd))) {
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default);
CMTime editEnd = CMTimeMake(_currentTime - trimEndTime.value, _timescale);
[self endEditListAtTime:editEnd empty:NO];
}
}
NSLog(@"%llu, %llu, %llu, %llu", _count++, _delta, _currentTime, sample->timestamp);
}
/**
* Starts a new edit
*/
- (void)startEditListAtTime:(CMTime)time {
if (editsSize <= editsCount) {
editsSize += 20;
edits = (CMTimeRange *) realloc(edits, sizeof(CMTimeRange) * editsSize);
}
edits[editsCount] = CMTimeRangeMake(time, kCMTimeInvalid);
editOpen = YES;
}
/**
* Closes a opened edit
*/
- (void)endEditListAtTime:(CMTime)time empty:(BOOL)type {
if (!editOpen)
return;
time.value -= edits[editsCount].start.value;
edits[editsCount].duration = time;
if (type)
edits[editsCount].start.value = -1;
if (edits[editsCount].duration.value > 0) {
editsCount++;
}
editOpen = NO;
}
/**
* Returns if there is an open edit list
*/
- (BOOL)isEditListOpen {
return editOpen;
}
- (void)dealloc {
[_queue release];
[super dealloc];
}
@end
+1 -1
View File
@@ -767,7 +767,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
if (offset < minOffset)
minOffset = offset;
[demuxHelper->offsetsArray addObject:[NSNumber numberWithLongLong:offset]];
[demuxHelper->offsetsArray addObject:@(offset)];
demuxHelper->current_time += duration;
+8 -1
View File
@@ -14,13 +14,20 @@
@public
void *data;
uint64_t size;
uint64_t timescale;
uint64_t duration;
int64_t offset;
uint64_t presentationTimestamp;
uint64_t timestamp;
uint32_t trackId;
BOOL isSync;
BOOL isCompressed;
BOOL isForced;
void *attachments;
}
@end
+22
View File
@@ -192,6 +192,13 @@
A9B9C49F1823989C00416A4E /* libjpeg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A9B9C49C1823989C00416A4E /* libjpeg.a */; };
A9B9C4A01823989C00416A4E /* liblept.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A9B9C49D1823989C00416A4E /* liblept.a */; };
A9B9C4A11823989C00416A4E /* libtesseract.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A9B9C49E1823989C00416A4E /* libtesseract.a */; };
A9DFBDA1195FF5BA00410060 /* MP42Heap.h in Headers */ = {isa = PBXBuildFile; fileRef = A9DFBD9F195FF5BA00410060 /* MP42Heap.h */; };
A9DFBDA2195FF5BA00410060 /* MP42Heap.h in Headers */ = {isa = PBXBuildFile; fileRef = A9DFBD9F195FF5BA00410060 /* MP42Heap.h */; };
A9DFBDA3195FF5BA00410060 /* MP42Heap.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DFBDA0195FF5BA00410060 /* MP42Heap.m */; };
A9DFBDA4195FF5BA00410060 /* MP42Heap.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DFBDA0195FF5BA00410060 /* MP42Heap.m */; };
A9DFBDA5195FF5BA00410060 /* MP42Heap.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DFBDA0195FF5BA00410060 /* MP42Heap.m */; };
A9DFBDCB1960174500410060 /* MP42EditListsReconstructor.h in Headers */ = {isa = PBXBuildFile; fileRef = A9DFBDC91960174500410060 /* MP42EditListsReconstructor.h */; };
A9DFBDCC1960174500410060 /* MP42EditListsReconstructor.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DFBDCA1960174500410060 /* MP42EditListsReconstructor.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -380,6 +387,10 @@
A9B9C49C1823989C00416A4E /* libjpeg.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjpeg.a; path = Tesseract/lib/libjpeg.a; sourceTree = "<group>"; };
A9B9C49D1823989C00416A4E /* liblept.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = liblept.a; path = Tesseract/lib/liblept.a; sourceTree = "<group>"; };
A9B9C49E1823989C00416A4E /* libtesseract.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libtesseract.a; path = Tesseract/lib/libtesseract.a; sourceTree = "<group>"; };
A9DFBD9F195FF5BA00410060 /* MP42Heap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42Heap.h; sourceTree = "<group>"; };
A9DFBDA0195FF5BA00410060 /* MP42Heap.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42Heap.m; sourceTree = "<group>"; };
A9DFBDC91960174500410060 /* MP42EditListsReconstructor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42EditListsReconstructor.h; sourceTree = "<group>"; };
A9DFBDCA1960174500410060 /* MP42EditListsReconstructor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42EditListsReconstructor.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -603,6 +614,8 @@
A9B9C22D1823923100416A4E /* MP42ConverterProtocol.h */,
A9B9C22E1823923100416A4E /* MP42Fifo.h */,
A9B9C22F1823923100416A4E /* MP42Fifo.m */,
A9DFBD9F195FF5BA00410060 /* MP42Heap.h */,
A9DFBDA0195FF5BA00410060 /* MP42Heap.m */,
A9B9C2321823923100416A4E /* MP42FileImporter.h */,
A9B9C2331823923100416A4E /* MP42FileImporter.m */,
A9B9C4A518239BE900416A4E /* converters */,
@@ -629,6 +642,8 @@
A9B9C24B1823923100416A4E /* MP42SrtImporter.m */,
A9B9C2231823923100416A4E /* MP42AVFImporter.h */,
A9B9C2241823923100416A4E /* MP42AVFImporter.m */,
A9DFBDC91960174500410060 /* MP42EditListsReconstructor.h */,
A9DFBDCA1960174500410060 /* MP42EditListsReconstructor.m */,
A9B9C2271823923100416A4E /* MP42CCImporter.h */,
A9B9C2281823923100416A4E /* MP42CCImporter.m */,
A9B9C21B1823923100416A4E /* MP42AACImporter.h */,
@@ -696,6 +711,7 @@
buildActionMask = 2147483647;
files = (
A91711E7187D547D00459495 /* MP42PreviewGenerator.h in Headers */,
A9DFBDA2195FF5BA00410060 /* MP42Heap.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -704,6 +720,7 @@
buildActionMask = 2147483647;
files = (
A9B9C27B1823923200416A4E /* MP42File.h in Headers */,
A9DFBDCB1960174500410060 /* MP42EditListsReconstructor.h in Headers */,
A9B9C29B1823923200416A4E /* MP42Track.h in Headers */,
A9B9C29F1823923200416A4E /* MP42VideoTrack.h in Headers */,
A9B9C26C1823923200416A4E /* MP42AudioTrack.h in Headers */,
@@ -744,6 +761,7 @@
A9B9C2C81823962600416A4E /* UniversalDetector.h in Headers */,
A9B9C27F1823923200416A4E /* MP42H264Importer.h in Headers */,
A9B9C29D1823923200416A4E /* MP42PrivateUtilities.h in Headers */,
A9DFBDA1195FF5BA00410060 /* MP42Heap.h in Headers */,
A9B9C2661823923200416A4E /* MP42AACImporter.h in Headers */,
A9B9C2681823923200416A4E /* MP42AC3Importer.h in Headers */,
A989F1001823AB2E0070AD8C /* MP42Track+Muxer.h in Headers */,
@@ -940,6 +958,7 @@
A910B60918394EB20064028F /* MP42Mp4Importer.m in Sources */,
A910B60B18394EB20064028F /* MP42QTImporter.m in Sources */,
A910B60D18394EB20064028F /* MP42SrtImporter.m in Sources */,
A9DFBDA5195FF5BA00410060 /* MP42Heap.m in Sources */,
A910B60F18394EB20064028F /* MP42AVFImporter.m in Sources */,
A910B61118394EB20064028F /* MP42CCImporter.m in Sources */,
A910B61318394EB20064028F /* MP42AACImporter.mm in Sources */,
@@ -980,6 +999,7 @@
A9B9C2821823923200416A4E /* MP42HtmlParser.m in Sources */,
A9B9C2861823923200416A4E /* MP42MediaFormat.m in Sources */,
A9B9C2A41823923200416A4E /* MP42XMLReader.m in Sources */,
A9DFBDCC1960174500410060 /* MP42EditListsReconstructor.m in Sources */,
A989F1101823B3B40070AD8C /* MP42TextSample.m in Sources */,
A9B9C2751823923200416A4E /* MP42ChapterTrack.m in Sources */,
A9B9C2C51823957800416A4E /* MP42Languages.m in Sources */,
@@ -991,6 +1011,7 @@
A9B9C2881823923200416A4E /* MP42Metadata.m in Sources */,
A9B9C2A91823923200416A4E /* MatroskaParser.c in Sources */,
A9B9C2941823923200416A4E /* MP42Sample.m in Sources */,
A9DFBDA3195FF5BA00410060 /* MP42Heap.m in Sources */,
A9B9C28A1823923200416A4E /* MP42MkvImporter.m in Sources */,
A925AAB018379AF800BE84D4 /* MP42Utilities.m in Sources */,
A9B9C2731823923200416A4E /* MP42CCImporter.m in Sources */,
@@ -1006,6 +1027,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A9DFBDA4195FF5BA00410060 /* MP42Heap.m in Sources */,
A991714D195AAAEF005AD68E /* MP42Track+Muxer.m in Sources */,
A9B9C212182391DF00416A4E /* MP42Tests.m in Sources */,
);