From bbd22ca8bb0f8aa030830136ae92fbfb9edd5585 Mon Sep 17 00:00:00 2001 From: Single Date: Mon, 4 Nov 2019 12:38:00 +0800 Subject: [PATCH] SGVideoDecoder: Add outputFromKeyFrame. --- .../Classes/Core/SGDecoder/SGVideoDecoder.h | 5 +++ .../Classes/Core/SGDecoder/SGVideoDecoder.m | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.h b/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.h index 82a01f21..1b068927 100644 --- a/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.h +++ b/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.h @@ -10,4 +10,9 @@ @interface SGVideoDecoder : NSObject +/** + * + */ +@property (nonatomic) BOOL outputFromKeyFrame; + @end diff --git a/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m b/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m index 3c6c21e8..3bea5c1c 100644 --- a/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m +++ b/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m @@ -16,6 +16,7 @@ @interface SGVideoDecoder () { struct { + BOOL needsKeyFrame; BOOL needsAlignment; NSUInteger outputCount; } _flags; @@ -31,6 +32,14 @@ @implementation SGVideoDecoder @synthesize options = _options; +- (instancetype)init +{ + if (self = [super init]) { + self->_outputFromKeyFrame = YES; + } + return self; +} + - (void)dealloc { [self destroy]; @@ -50,6 +59,7 @@ - (void)setup - (void)destroy { self->_flags.outputCount = 0; + self->_flags.needsKeyFrame = YES; self->_flags.needsAlignment = YES; [self->_codecContext close]; self->_codecContext = nil; @@ -62,6 +72,7 @@ - (void)destroy - (void)flush { self->_flags.outputCount = 0; + self->_flags.needsKeyFrame = YES; self->_flags.needsAlignment = YES; [self->_codecContext flush]; [self->_lastFrame unlock]; @@ -123,6 +134,7 @@ - (void)flush SGCodecDescriptor *cd = self->_codecDescriptor; NSArray *objs = [self->_codecContext decode:packet]; objs = [self processFrames:objs done:!packet]; + objs = [self clipKeyFrames:objs]; objs = [self clipFrames:objs timeRange:cd.timeRange]; return objs; } @@ -138,6 +150,26 @@ - (void)flush return ret; } +- (NSArray<__kindof SGFrame *> *)clipKeyFrames:(NSArray<__kindof SGFrame *> *)frames +{ + if (self->_outputFromKeyFrame == NO || + self->_flags.needsKeyFrame == NO) { + return frames; + } + NSMutableArray *ret = [NSMutableArray array]; + for (SGFrame *obj in frames) { + if (self->_flags.needsKeyFrame == NO) { + [ret addObject:obj]; + } else if (obj.core->key_frame) { + [ret addObject:obj]; + self->_flags.needsKeyFrame = NO; + } else { + [obj unlock]; + } + } + return ret; +} + - (NSArray<__kindof SGFrame *> *)clipFrames:(NSArray<__kindof SGFrame *> *)frames timeRange:(CMTimeRange)timeRange { if (frames.count <= 0) {