Skip to content

Commit

Permalink
SGVideoDecoder: Add outputFromKeyFrame.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Nov 4, 2019
1 parent 18e359b commit bbd22ca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@

@interface SGVideoDecoder : NSObject <SGDecodable>

/**
*
*/
@property (nonatomic) BOOL outputFromKeyFrame;

@end
32 changes: 32 additions & 0 deletions SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ @interface SGVideoDecoder ()

{
struct {
BOOL needsKeyFrame;
BOOL needsAlignment;
NSUInteger outputCount;
} _flags;
Expand All @@ -31,6 +32,14 @@ @implementation SGVideoDecoder

@synthesize options = _options;

- (instancetype)init
{
if (self = [super init]) {
self->_outputFromKeyFrame = YES;
}
return self;
}

- (void)dealloc
{
[self destroy];
Expand All @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit bbd22ca

Please sign in to comment.