Skip to content

Commit

Permalink
SGProcessor: Preposition.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Nov 28, 2019
1 parent bd44fd8 commit 5f560a9
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 123 deletions.
43 changes: 40 additions & 3 deletions SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "SGFrame+Internal.h"
#import "SGPacket+Internal.h"
#import "SGDescriptor+Internal.h"
#import "SGAudioFormatter.h"
#import "SGCodecContext.h"
#import "SGAudioFrame.h"
#import "SGSonic.h"
Expand All @@ -26,6 +27,7 @@ @interface SGAudioDecoder ()
}

@property (nonatomic, strong, readonly) SGSonic *sonic;
@property (nonatomic, strong, readonly) SGAudioFormatter *formatter;
@property (nonatomic, strong, readonly) SGCodecContext *codecContext;
@property (nonatomic, strong, readonly) SGCodecDescriptor *codecDescriptor;
@property (nonatomic, strong, readonly) SGAudioDescriptor *audioDescriptor;
Expand Down Expand Up @@ -64,6 +66,7 @@ - (void)destroy
[self->_codecContext close];
self->_codecContext = nil;
self->_audioDescriptor = nil;
self->_formatter = nil;
}

#pragma mark - Control
Expand All @@ -75,11 +78,12 @@ - (void)flush
self->_flags.needsResetSonic = YES;
self->_flags.lastEndTimeStamp = kCMTimeInvalid;
[self->_codecContext flush];
[self->_formatter flush];
}

- (NSArray<__kindof SGFrame *> *)decode:(SGPacket *)packet
{
NSMutableArray<__kindof SGFrame *> *ret = [NSMutableArray array];
NSMutableArray *ret = [NSMutableArray array];
SGCodecDescriptor *cd = packet.codecDescriptor;
NSAssert(cd, @"Invalid codec descriptor.");
BOOL isEqual = [cd isEqualToDescriptor:self->_codecDescriptor];
Expand Down Expand Up @@ -122,8 +126,9 @@ - (void)flush
[ret addObject:obj];
}
}
if (ret.count) {
self->_flags.lastEndTimeStamp = CMTimeAdd(ret.lastObject.timeStamp, ret.lastObject.duration);
if (ret.count > 0) {
SGFrame *obj = ret.lastObject;
self->_flags.lastEndTimeStamp = CMTimeAdd(obj.timeStamp, obj.duration);
}
return ret;
}
Expand Down Expand Up @@ -170,6 +175,7 @@ - (void)flush
NSArray *objs = [self->_codecContext decode:packet];
objs = [self processFrames:objs done:!packet];
objs = [self clipFrames:objs timeRange:cd.timeRange];
objs = [self formatFrames:objs];
return objs;
}

Expand Down Expand Up @@ -276,6 +282,37 @@ - (void)flush
return ret;
}

- (NSArray<__kindof SGFrame *> *)formatFrames:(NSArray<__kindof SGFrame *> *)frames
{
NSArray<SGAudioDescriptor *> *descriptors = self->_options.supportedAudioDescriptors;
if (descriptors.count <= 0) {
return frames;
}
NSMutableArray *ret = [NSMutableArray array];
for (SGAudioFrame *obj in frames) {
BOOL supported = NO;
for (SGAudioDescriptor *descriptor in descriptors) {
if ([obj.descriptor isEqualToDescriptor:descriptor]) {
supported = YES;
break;
}
}
if (supported) {
[ret addObject:obj];
continue;
}
if (!self->_formatter) {
self->_formatter = [[SGAudioFormatter alloc] init];
self->_formatter.descriptor = descriptors.firstObject;
}
SGAudioFrame *newObj = [self->_formatter format:obj];
if (newObj) {
[ret addObject:newObj];
}
}
return ret;
}

- (SGAudioFrame *)readSonicFrame:(int64_t)pts
{
int nb_samples = [self->_sonic samplesAvailable];
Expand Down
57 changes: 56 additions & 1 deletion SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "SGPacket+Internal.h"
#import "SGCodecContext.h"
#import "SGVideoFrame.h"
#import "SGSWScale.h"

@interface SGVideoDecoder ()

Expand All @@ -23,9 +24,10 @@ @interface SGVideoDecoder ()
} _flags;
}

@property (nonatomic, strong, readonly) SGSWScale *scaler;
@property (nonatomic, strong, readonly) SGCodecContext *codecContext;
@property (nonatomic, strong, readonly) SGVideoFrame *lastDecodeFrame;
@property (nonatomic, strong, readonly) SGVideoFrame *lastOutputFrame;
@property (nonatomic, strong, readonly) SGCodecContext *codecContext;
@property (nonatomic, strong, readonly) SGCodecDescriptor *codecDescriptor;

@end
Expand Down Expand Up @@ -193,6 +195,7 @@ - (void)flush
objs = [self processFrames:objs done:!packet];
objs = [self clipKeyFrames:objs];
objs = [self clipFrames:objs timeRange:cd.timeRange];
objs = [self formatFrames:objs];
return objs;
}

Expand Down Expand Up @@ -280,4 +283,56 @@ - (void)flush
return ret;
}

- (NSArray<__kindof SGFrame *> *)formatFrames:(NSArray<__kindof SGFrame *> *)frames
{
NSArray<NSNumber *> *formats = self->_options.supportedPixelFormats;
if (formats.count <= 0) {
return frames;
}
NSMutableArray *ret = [NSMutableArray array];
for (SGVideoFrame *obj in frames) {
BOOL supported = NO;
for (NSNumber *format in formats) {
if (obj.pixelBuffer ||
obj.descriptor.format == format.intValue) {
supported = YES;
break;
}
}
if (supported) {
[ret addObject:obj];
continue;
}
int format = formats.firstObject.intValue;
if (![self->_scaler.inputDescriptor isEqualToDescriptor:obj.descriptor]) {
SGSWScale *scaler = [[SGSWScale alloc] init];
scaler.inputDescriptor = obj.descriptor;
scaler.outputDescriptor = obj.descriptor.copy;
scaler.outputDescriptor.format = format;
if ([scaler open]) {
self->_scaler = scaler;
}
}
if (!self->_scaler) {
[obj unlock];
continue;
}
SGVideoFrame *newObj = [SGVideoFrame frameWithDescriptor:self->_scaler.outputDescriptor];
int result = [self->_scaler convert:(void *)obj.data
inputLinesize:obj.linesize
outputData:newObj.core->data
outputLinesize:newObj.core->linesize];
if (result < 0) {
[newObj unlock];
[obj unlock];
continue;
}
[newObj setCodecDescriptor:obj.codecDescriptor];
[newObj fillWithTimeStamp:obj.timeStamp decodeTimeStamp:obj.decodeTimeStamp duration:obj.duration];
[ret addObject:newObj];
[obj unlock];
}
return ret;
}

@end
15 changes: 15 additions & 0 deletions SGPlayer/Classes/Core/SGOption/SGDecoderOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <Foundation/Foundation.h>
#import "SGAudioDescriptor.h"

@interface SGDecoderOptions : NSObject <NSCopying>

Expand Down Expand Up @@ -58,4 +59,18 @@
*/
@property (nonatomic) OSType preferredPixelFormat;

/*!
@property supportedPixelFormats
@abstract
Indicates the supported pixel formats.
*/
@property (nonatomic, copy) NSArray<NSNumber *> *supportedPixelFormats;

/*!
@property supportedAudioDescriptors
@abstract
Indicates the supported audio descriptors.
*/
@property (nonatomic, copy) NSArray<SGAudioDescriptor *> *supportedAudioDescriptors;

@end
6 changes: 6 additions & 0 deletions SGPlayer/Classes/Core/SGOption/SGDecoderOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

#import "SGDecoderOptions.h"
#import "SGAudioRenderer.h"
#import "SGVideoRenderer.h"
#import "SGMapping.h"

@implementation SGDecoderOptions
Expand All @@ -20,6 +22,8 @@ - (id)copyWithZone:(NSZone *)zone
obj->_hardwareDecodeH264 = self->_hardwareDecodeH264;
obj->_hardwareDecodeH265 = self->_hardwareDecodeH265;
obj->_preferredPixelFormat = self->_preferredPixelFormat;
obj->_supportedPixelFormats = self->_supportedPixelFormats.copy;
obj->_supportedAudioDescriptors = self->_supportedAudioDescriptors.copy;
return obj;
}

Expand All @@ -32,6 +36,8 @@ - (instancetype)init
self->_hardwareDecodeH264 = YES;
self->_hardwareDecodeH265 = YES;
self->_preferredPixelFormat = SGPixelFormatFF2AV(AV_PIX_FMT_NV12);
self->_supportedPixelFormats = [SGVideoRenderer supportedPixelFormats];
self->_supportedAudioDescriptors = @[[SGAudioRenderer supportedAudioDescriptor]];
}
return self;
}
Expand Down
15 changes: 0 additions & 15 deletions SGPlayer/Classes/Core/SGOption/SGProcessorOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

#import <Foundation/Foundation.h>
#import "SGAudioDescriptor.h"

@interface SGProcessorOptions : NSObject <NSCopying>

Expand All @@ -27,18 +26,4 @@
*/
@property (nonatomic, copy) Class videoClass;

/*!
@property supportedPixelFormats
@abstract
Indicates the supported pixel formats.
*/
@property (nonatomic, copy) NSArray<NSNumber *> *supportedPixelFormats;

/*!
@property supportedAudioDescriptor
@abstract
Indicates the supported audio descriptor.
*/
@property (nonatomic, copy) SGAudioDescriptor *supportedAudioDescriptor;

@end
4 changes: 0 additions & 4 deletions SGPlayer/Classes/Core/SGOption/SGProcessorOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#import "SGProcessorOptions.h"
#import "SGAudioProcessor.h"
#import "SGVideoProcessor.h"
#import "SGAudioRenderer.h"
#import "SGVideoRenderer.h"

@implementation SGProcessorOptions

Expand All @@ -27,8 +25,6 @@ - (instancetype)init
if (self = [super init]) {
self->_audioClass = [SGAudioProcessor class];
self->_videoClass = [SGVideoProcessor class];
self->_supportedPixelFormats = [SGVideoRenderer supportedPixelFormats];
self->_supportedAudioDescriptor = [SGAudioRenderer supportedAudioDescriptor];
}
return self;
}
Expand Down
7 changes: 1 addition & 6 deletions SGPlayer/Classes/Core/SGProcessor/SGAudioMixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
/**
*
*/
- (instancetype)initWithTracks:(NSArray<SGTrack *> *)tracks weights:(NSArray<NSNumber *> *)weights descriptor:(SGAudioDescriptor *)descriptor;

/**
*
*/
@property (nonatomic, copy, readonly) SGAudioDescriptor *descriptor;
- (instancetype)initWithTracks:(NSArray<SGTrack *> *)tracks weights:(NSArray<NSNumber *> *)weights;

/**
*
Expand Down
7 changes: 5 additions & 2 deletions SGPlayer/Classes/Core/SGProcessor/SGAudioMixer.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
@interface SGAudioMixer ()

@property (nonatomic, readonly) CMTime startTime;
@property (nonatomic, strong, readonly) SGAudioDescriptor *descriptor;
@property (nonatomic, strong, readonly) NSMutableDictionary<NSNumber *, SGAudioMixerUnit *> *units;

@end

@implementation SGAudioMixer

- (instancetype)initWithTracks:(NSArray<SGTrack *> *)tracks weights:(NSArray<NSNumber *> *)weights descriptor:(SGAudioDescriptor *)descriptor
- (instancetype)initWithTracks:(NSArray<SGTrack *> *)tracks weights:(NSArray<NSNumber *> *)weights
{
if (self = [super init]) {
self->_tracks = [tracks copy];
self->_weights = [weights copy];
self->_descriptor = [descriptor copy];
self->_startTime = kCMTimeNegativeInfinity;
self->_units = [NSMutableDictionary dictionary];
for (SGTrack *obj in self->_tracks) {
Expand All @@ -45,6 +45,9 @@ - (SGAudioFrame *)putFrame:(SGAudioFrame *)frame
[frame unlock];
return nil;
}
if (!self->_descriptor) {
self->_descriptor = frame.descriptor.copy;
}
NSAssert([self->_descriptor isEqualToDescriptor:frame.descriptor], @"Invalid Format.");
NSAssert(self->_descriptor.format == AV_SAMPLE_FMT_FLTP, @"Invalid Format.");
SGAudioMixerUnit *unit = [self->_units objectForKey:@(frame.track.index)];
Expand Down
5 changes: 0 additions & 5 deletions SGPlayer/Classes/Core/SGProcessor/SGAudioProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,4 @@

@interface SGAudioProcessor : NSObject <SGProcessor>

/**
*
*/
- (void)setDescriptor:(SGAudioDescriptor *)descriptor;

@end
Loading

0 comments on commit 5f560a9

Please sign in to comment.