Skip to content

Commit

Permalink
SGPlayer: Add video processor.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Nov 16, 2019
1 parent 40b68c9 commit 6cb8c2b
Show file tree
Hide file tree
Showing 37 changed files with 424 additions and 193 deletions.
41 changes: 21 additions & 20 deletions SGPlayer/Classes/Core/SGAsset/SGTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@
#import <Foundation/Foundation.h>
#import "SGDefines.h"

@class SGTrack;

/*!
@abstract
Get track with index.
*/
SGTrack *SGTrackWithIndex(NSArray<SGTrack *> *tracks, NSInteger index);

/*!
@abstract
Get track with media type.
*/
SGTrack *SGTrackWithType(NSArray<SGTrack *> *tracks, SGMediaType type);

/*!
@abstract
Get tracks with media types.
*/
NSArray<SGTrack *> *SGTracksWithType(NSArray<SGTrack *> *tracks, SGMediaType type);

@interface SGTrack : NSObject <NSCopying>

+ (instancetype)new NS_UNAVAILABLE;
Expand All @@ -55,4 +35,25 @@ NSArray<SGTrack *> *SGTracksWithType(NSArray<SGTrack *> *tracks, SGMediaType typ
*/
@property (nonatomic, readonly) NSInteger index;

/*!
@method trackWithTracks:type:
@abstract
Get track with media type.
*/
+ (SGTrack *)trackWithTracks:(NSArray<SGTrack *> *)tracks type:(SGMediaType)type;

/*!
@method trackWithTracks:index:
@abstract
Get track with index.
*/
+ (SGTrack *)trackWithTracks:(NSArray<SGTrack *> *)tracks index:(NSInteger)index;

/*!
@method tracksWithTracks:type:
@abstract
Get tracks with media types.
*/
+ (NSArray<SGTrack *> *)tracksWithTracks:(NSArray<SGTrack *> *)tracks type:(SGMediaType)type;

@end
60 changes: 30 additions & 30 deletions SGPlayer/Classes/Core/SGAsset/SGTrack.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,52 @@
#import "SGTrack.h"
#import "SGTrack+Internal.h"

SGTrack *SGTrackWithIndex(NSArray<SGTrack *> *tracks, NSInteger index)
@implementation SGTrack

- (id)copyWithZone:(NSZone *)zone
{
SGTrack *obj = [[self.class alloc] init];
obj->_type = self->_type;
obj->_index = self->_index;
obj->_core = self->_core;
return obj;
}

- (instancetype)initWithType:(SGMediaType)type index:(NSInteger)index
{
if (self = [super init]) {
self->_type = type;
self->_index = index;
}
return self;
}

- (void *)coreptr
{
return self->_core;
}

+ (SGTrack *)trackWithTracks:(NSArray<SGTrack *> *)tracks type:(SGMediaType)type
{
for (SGTrack *obj in tracks) {
if (obj.index == index) {
if (obj.type == type) {
return obj;
}
}
return nil;
}

SGTrack *SGTrackWithType(NSArray<SGTrack *> *tracks, SGMediaType type)
+ (SGTrack *)trackWithTracks:(NSArray<SGTrack *> *)tracks index:(NSInteger)index
{
for (SGTrack *obj in tracks) {
if (obj.type == type) {
if (obj.index == index) {
return obj;
}
}
return nil;
}

NSArray<SGTrack *> *SGTracksWithType(NSArray<SGTrack *> *tracks, SGMediaType type)
+ (NSArray<SGTrack *> *)tracksWithTracks:(NSArray<SGTrack *> *)tracks type:(SGMediaType)type
{
NSMutableArray *array = [NSMutableArray array];
for (SGTrack *obj in tracks) {
Expand All @@ -40,29 +65,4 @@
return array.count ? [array copy] : nil;
}

@implementation SGTrack

- (id)copyWithZone:(NSZone *)zone
{
SGTrack *obj = [[self.class alloc] init];
obj->_type = self->_type;
obj->_index = self->_index;
obj->_core = self->_core;
return obj;
}

- (instancetype)initWithType:(SGMediaType)type index:(NSInteger)index
{
if (self = [super init]) {
self->_type = type;
self->_index = index;
}
return self;
}

- (void *)coreptr
{
return self->_core;
}

@end
5 changes: 0 additions & 5 deletions SGPlayer/Classes/Core/SGData/SGAudioFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

@interface SGAudioFrame : SGFrame

/**
*
*/
+ (NSString *)commonReuseName;

/**
*
*/
Expand Down
39 changes: 23 additions & 16 deletions SGPlayer/Classes/Core/SGData/SGAudioFrame.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ @interface SGAudioFrame ()

@implementation SGAudioFrame

+ (instancetype)audioFrameWithDescriptor:(SGAudioDescriptor *)descriptor numberOfSamples:(int)numberOfSamples
+ (instancetype)frame
{
SGAudioFrame *frame = [[SGObjectPool sharedPool] objectWithClass:[SGAudioFrame class] reuseName:[SGAudioFrame commonReuseName]];
static NSString *name = @"SGAudioFrame";
return [[SGObjectPool sharedPool] objectWithClass:[self class] reuseName:name];
}

+ (instancetype)frameWithDescriptor:(SGAudioDescriptor *)descriptor numberOfSamples:(int)numberOfSamples
{
SGAudioFrame *frame = [SGAudioFrame frame];
frame.core->format = descriptor.format;
frame.core->nb_samples = numberOfSamples;
frame.core->sample_rate = descriptor.sampleRate;
frame.core->channels = descriptor.numberOfChannels;
frame.core->channel_layout = descriptor.channelLayout;
frame.core->nb_samples = numberOfSamples;
int linesize = [descriptor linesize:numberOfSamples];
int numberOfPlanes = descriptor.numberOfPlanes;
for (int i = 0; i < numberOfPlanes; i++) {
for (int i = 0; i < descriptor.numberOfPlanes; i++) {
uint8_t *data = av_mallocz(linesize);
memset(data, 0, linesize);
AVBufferRef *buffer = av_buffer_create(data, linesize, av_buffer_default_free, NULL, 0);
AVBufferRef *buffer = av_buffer_create(data, linesize, NULL, NULL, 0);
frame.core->buf[i] = buffer;
frame.core->data[i] = buffer->data;
frame.core->linesize[i] = buffer->size;
Expand All @@ -45,16 +50,6 @@ + (instancetype)audioFrameWithDescriptor:(SGAudioDescriptor *)descriptor numberO

#pragma mark - Setter & Getter

+ (NSString *)commonReuseName
{
static NSString *ret = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ret = NSStringFromClass(self.class);
});
return ret;
}

- (SGMediaType)type
{
return SGMediaTypeAudio;
Expand Down Expand Up @@ -99,6 +94,18 @@ - (void)fill
[self fillWithTimeStamp:timeStamp decodeTimeStamp:decodeTimeStamp duration:duration];
}

- (void)fillWithFrame:(SGFrame *)frame
{
[super fillWithFrame:frame];
SGAudioFrame *audioFrame = (SGAudioFrame *)frame;
self->_numberOfSamples = audioFrame->_numberOfSamples;
self->_descriptor = audioFrame->_descriptor.copy;
for (int i = 0; i < SGFramePlaneCount; i++) {
self->_data[i] = audioFrame->_data[i];
self->_linesize[i] = audioFrame->_linesize[i];
}
}

- (void)fillWithTimeStamp:(CMTime)timeStamp decodeTimeStamp:(CMTime)decodeTimeStamp duration:(CMTime)duration
{
[super fillWithTimeStamp:timeStamp decodeTimeStamp:decodeTimeStamp duration:duration];
Expand Down
3 changes: 2 additions & 1 deletion SGPlayer/Classes/Core/SGData/SGCapacity.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,6 @@ BOOL SGCapacityIsEmpty(SGCapacity c1)
return
c1.size == 0 &&
c1.count == 0 &&
CMTimeCompare(c1.duration, kCMTimeZero) == 0;
(CMTIME_IS_INVALID(c1.duration) ||
CMTimeCompare(c1.duration, kCMTimeZero) == 0);
}
22 changes: 21 additions & 1 deletion SGPlayer/Classes/Core/SGData/SGFrame+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

#import "SGFrame.h"
#import "SGAudioFrame.h"
#import "SGVideoFrame.h"
#import "SGCodecDescriptor.h"

@interface SGFrame ()

/**
*
*/
+ (instancetype)frame;

/**
*
*/
Expand All @@ -27,6 +33,11 @@
*/
- (void)fill;

/**
*
*/
- (void)fillWithFrame:(SGFrame *)frame;

/**
*
*/
Expand All @@ -39,6 +50,15 @@
/**
*
*/
+ (instancetype)audioFrameWithDescriptor:(SGAudioDescriptor *)descriptor numberOfSamples:(int)numberOfSamples;
+ (instancetype)frameWithDescriptor:(SGAudioDescriptor *)descriptor numberOfSamples:(int)numberOfSamples;

@end

@interface SGVideoFrame ()

/**
*
*/
+ (instancetype)frameWithDescriptor:(SGVideoDescriptor *)descriptor;

@end
18 changes: 18 additions & 0 deletions SGPlayer/Classes/Core/SGData/SGFrame.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ @implementation SGFrame
@synthesize flags = _flags;
@synthesize reuseName = _reuseName;

+ (instancetype)frame
{
NSAssert(NO, @"Subclass only.");
return nil;
}

- (instancetype)init
{
if (self = [super init]) {
Expand Down Expand Up @@ -115,6 +121,18 @@ - (void)fill
self->_decodeTimeStamp = [cd convertTimeStamp:decodeTimeStamp];
}

- (void)fillWithFrame:(SGFrame *)frame
{
av_frame_ref(self->_core, frame->_core);
self->_size = frame->_size;
self->_track = frame->_track;
self->_metadata = frame->_metadata;
self->_duration = frame->_duration;
self->_timeStamp = frame->_timeStamp;
self->_decodeTimeStamp = frame->_decodeTimeStamp;
self->_codecDescriptor = frame->_codecDescriptor.copy;
}

- (void)fillWithTimeStamp:(CMTime)timeStamp decodeTimeStamp:(CMTime)decodeTimeStamp duration:(CMTime)duration
{
AVFrame *frame = self->_core;
Expand Down
5 changes: 5 additions & 0 deletions SGPlayer/Classes/Core/SGData/SGPacket+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

@interface SGPacket ()

/**
*
*/
+ (instancetype)packet;

/**
*
*/
Expand Down
5 changes: 0 additions & 5 deletions SGPlayer/Classes/Core/SGData/SGPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

@interface SGPacket : NSObject <SGData>

/**
*
*/
+ (NSString *)commonReuseName;

/**
*
*/
Expand Down
16 changes: 6 additions & 10 deletions SGPlayer/Classes/Core/SGData/SGPacket.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ @implementation SGPacket
@synthesize flags = _flags;
@synthesize reuseName = _reuseName;

+ (instancetype)packet
{
static NSString *name = @"SGPacket";
return [[SGObjectPool sharedPool] objectWithClass:[self class] reuseName:name];
}

- (instancetype)init
{
if (self = [super init]) {
Expand Down Expand Up @@ -57,16 +63,6 @@ - (NSString *)description

#pragma mark - Setter & Getter

+ (NSString *)commonReuseName
{
static NSString *ret = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ret = NSStringFromClass(self.class);
});
return ret;
}

#pragma mark - Data

- (void)lock
Expand Down
5 changes: 0 additions & 5 deletions SGPlayer/Classes/Core/SGData/SGVideoFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

@interface SGVideoFrame : SGFrame

/**
*
*/
+ (NSString *)commonReuseName;

/**
*
*/
Expand Down
Loading

0 comments on commit 6cb8c2b

Please sign in to comment.