Files
MP42Foundation/MP42/MP42EditListsReconstructor.m
T

205 lines
6.0 KiB
Objective-C

//
// MP42EditListsConstructor.m
// MP42Foundation
//
// Created by Damiano Galassi on 29/06/14.
// Copyright (c) 2014 Damiano Galassi. All rights reserved.
//
#import "MP42EditListsReconstructor.h"
#import "MP42MediaFormat.h"
#import "MP42Heap.h"
@implementation MP42EditListsReconstructor
{
@private
MP42Heap<MP42SampleBuffer *> *_priorityQueue;
uint64_t _currentMediaTime;
CMTimeScale _timescale;
CMTimeRange *_edits;
uint64_t _editsCount;
uint64_t _editsSize;
BOOL _editOpen;
BOOL _emptyEditOpen;
}
- (instancetype)init
{
self = [super init];
if (self) {
_priorityQueue = [[MP42Heap alloc] initWithCapacity:32 comparator:^NSComparisonResult(MP42SampleBuffer * obj1, MP42SampleBuffer * obj2) {
return obj2->presentationTimestamp - obj1->presentationTimestamp;
}];
}
return self;
}
- (void)dealloc
{
free(_edits);
}
- (void)addSample:(MP42SampleBuffer *)sample
{
if (sample->attachments) {
// Flush the current queue, because pts time is going to be reset
CFBooleanRef resetDecoderBeforeDecoding = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding);
if (resetDecoderBeforeDecoding && CFBooleanGetValue(resetDecoderBeforeDecoding) == 1) {
[self flush];
}
// Flush the current queue, because an empty edit is coming
CFBooleanRef emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia);
if (emptyMedia && CFBooleanGetValue(emptyMedia) == 1) {
[self flush];
}
}
[_priorityQueue insert:sample];
if ([_priorityQueue isFull]) {
MP42SampleBuffer *extractedSample = [_priorityQueue extract];
[self analyzeSample:extractedSample];
}
}
- (void)flush
{
while (!_priorityQueue.isEmpty) {
MP42SampleBuffer *extractedSample = [_priorityQueue extract];
[self analyzeSample:extractedSample];
}
if (_editOpen == YES && _emptyEditOpen == NO) {
CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale);
[self endEditListAtTime:editEnd empty:NO];
}
}
- (void)done
{
[self flush];
}
- (void)analyzeSample:(MP42SampleBuffer *)sample {
#ifdef AVF_DEBUG
NSLog(@"T: %llu, D: %lld, P: %lld, PO: %lld O: %lld", _currentMediaTime, sample->decodeTimestamp, sample->presentationTimestamp, sample->presentationOutputTimestamp, sample->offset);
#endif
if (_timescale == 0) {
_timescale = sample->timescale;
// Re-align things if the first sample pts is not 0
if (sample->presentationTimestamp != 0) {
_currentMediaTime += sample->presentationTimestamp;
}
}
CFDictionaryRef trimStart = NULL, trimEnd = NULL, emptyMedia = NULL;
if (sample->attachments) {
trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart);
trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd);
emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia);
}
// Check if we need to add an empty edit list.
if (emptyMedia) {
if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO];
}
CMTime editStart = CMTimeMake(sample->presentationOutputTimestamp, _timescale);
[self startEditListAtTime:editStart];
_emptyEditOpen = YES;
}
BOOL shouldStartNewEdit = trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == NO && _editOpen == NO);
uint64_t trimmedDuration = sample->duration;
if (shouldStartNewEdit) {
// Close the current edit list
if (_emptyEditOpen == YES) {
_emptyEditOpen = NO;
CMTime editEnd = CMTimeMake(sample->presentationOutputTimestamp, _timescale);
[self endEditListAtTime:editEnd empty:YES];
}
if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO];
}
// Calculate the new edit start
CMTime editStart = CMTimeMake(_currentMediaTime, _timescale);
if (trimStart) {
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default);
editStart.value += trimStartTime.value;
trimmedDuration -= trimStartTime.value;
}
[self startEditListAtTime:editStart];
}
_currentMediaTime += sample->duration;
BOOL shouldEndEdit = trimEnd || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == YES && _editOpen == YES);
if (shouldEndEdit) {
CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale);
if (trimEnd) {
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default);
editEnd.value -= trimEndTime.value;
trimmedDuration -= trimEndTime.value;
}
[self endEditListAtTime:editEnd empty:NO];
}
}
/**
* Starts a new edit
*/
- (void)startEditListAtTime:(CMTime)time {
NSAssert(!_editOpen, @"Trying to open an edit list when one is already open.");
if (_editsSize <= _editsCount) {
_editsSize += 20;
_edits = (CMTimeRange *) realloc(_edits, sizeof(CMTimeRange) * _editsSize);
}
_edits[_editsCount] = CMTimeRangeMake(time, kCMTimeInvalid);
_editOpen = YES;
}
/**
* Closes a open edit
*/
- (void)endEditListAtTime:(CMTime)time empty:(BOOL)type {
NSAssert(_editOpen, @"Trying to close an edit list when there isn't a open one");
time.value -= _edits[_editsCount].start.value;
_edits[_editsCount].duration = time;
if (type) {
_edits[_editsCount].start.value = -1;
}
else {
}
if (_edits[_editsCount].duration.value > 0) {
_editsCount++;
}
_editOpen = NO;
}
@end