Skip to content

Commit

Permalink
SGData: Add flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Nov 14, 2019
1 parent d9205d5 commit 40b68c9
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 48 deletions.
12 changes: 12 additions & 0 deletions SGPlayer/Classes/Core/SGData/SGData.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@
#import <Foundation/Foundation.h>
#import <CoreMedia/CoreMedia.h>

/**
*
*/
typedef NS_OPTIONS(NSUInteger, SGDataFlags) {
SGDataFlagPadding = 1 << 0,
};

@protocol SGData <NSObject>

/**
*
*/
@property (nonatomic) SGDataFlags flags;

/**
*
*/
Expand Down
2 changes: 2 additions & 0 deletions SGPlayer/Classes/Core/SGData/SGFrame.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ @interface SGFrame ()

@implementation SGFrame

@synthesize flags = _flags;
@synthesize reuseName = _reuseName;

- (instancetype)init
Expand Down Expand Up @@ -88,6 +89,7 @@ - (void)clear
av_frame_unref(self->_core);
}
self->_size = 0;
self->_flags = 0;
self->_track = nil;
self->_duration = kCMTimeZero;
self->_timeStamp = kCMTimeZero;
Expand Down
2 changes: 2 additions & 0 deletions SGPlayer/Classes/Core/SGData/SGPacket.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ @interface SGPacket ()

@implementation SGPacket

@synthesize flags = _flags;
@synthesize reuseName = _reuseName;

- (instancetype)init
Expand Down Expand Up @@ -93,6 +94,7 @@ - (void)clear
av_packet_unref(self->_core);
}
self->_size = 0;
self->_flags = 0;
self->_track = nil;
self->_duration = kCMTimeZero;
self->_timeStamp = kCMTimeZero;
Expand Down
45 changes: 20 additions & 25 deletions SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,28 @@ - (void)flush
[self setup];
}
[cd fillToDescriptor:self->_codecDescriptor];
switch (packet.codecDescriptor.type) {
case SGCodecTypeDecode: {
NSArray<SGFrame *> *objs = [self processPacket:packet];
for (SGFrame *obj in objs) {
[ret addObject:obj];
}
if (packet.flags & SGDataFlagPadding) {
SGAudioDescriptor *ad = self->_audioDescriptor;
if (ad == nil) {
ad = [[SGAudioDescriptor alloc] init];
}
break;
case SGCodecTypePadding: {
SGAudioDescriptor *ad = self->_audioDescriptor;
if (ad == nil) {
ad = [[SGAudioDescriptor alloc] init];
}
CMTime start = packet.timeStamp;
CMTime duration = packet.duration;
int nb_samples = (int)CMTimeConvertScale(duration, ad.sampleRate, kCMTimeRoundingMethod_RoundTowardZero).value;
if (nb_samples > 0) {
duration = CMTimeMake(nb_samples, ad.sampleRate);
SGAudioFrame *obj = [SGAudioFrame audioFrameWithDescriptor:ad numberOfSamples:nb_samples];
SGCodecDescriptor *cd = [[SGCodecDescriptor alloc] init];
cd.track = packet.track;
[obj setCodecDescriptor:cd];
[obj fillWithTimeStamp:start decodeTimeStamp:start duration:duration];
[ret addObject:obj];
}
CMTime start = packet.timeStamp;
CMTime duration = packet.duration;
int nb_samples = (int)CMTimeConvertScale(duration, ad.sampleRate, kCMTimeRoundingMethod_RoundTowardZero).value;
if (nb_samples > 0) {
duration = CMTimeMake(nb_samples, ad.sampleRate);
SGAudioFrame *obj = [SGAudioFrame audioFrameWithDescriptor:ad numberOfSamples:nb_samples];
SGCodecDescriptor *cd = [[SGCodecDescriptor alloc] init];
cd.track = packet.track;
[obj setCodecDescriptor:cd];
[obj fillWithTimeStamp:start decodeTimeStamp:start duration:duration];
[ret addObject:obj];
}
} else {
NSArray<SGFrame *> *objs = [self processPacket:packet];
for (SGFrame *obj in objs) {
[ret addObject:obj];
}
break;
}
return ret;
}
Expand Down
18 changes: 7 additions & 11 deletions SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,13 @@ - (void)flush
[self setup];
}
[cd fillToDescriptor:self->_codecDescriptor];
switch (packet.codecDescriptor.type) {
case SGCodecTypeDecode: {
NSArray<SGFrame *> *objs = [self processPacket:packet];
for (SGFrame *obj in objs) {
[ret addObject:obj];
}
}
break;
case SGCodecTypePadding: {

if (packet.flags & SGDataFlagPadding) {

} else {
NSArray<SGFrame *> *objs = [self processPacket:packet];
for (SGFrame *obj in objs) {
[ret addObject:obj];
}
break;
}
self->_flags.outputCount += ret.count;
return ret;
Expand All @@ -117,6 +112,7 @@ - (void)flush
if (objs.count == 0 &&
self->_lastFrame &&
self->_flags.outputCount == 0) {
self->_lastFrame.flags |= SGDataFlagPadding;
objs = @[self->_lastFrame];
} else {
[self->_lastFrame unlock];
Expand Down
2 changes: 1 addition & 1 deletion SGPlayer/Classes/Core/SGDemuxer/SGPaddingDemuxer.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ - (NSError *)nextPacket:(SGPacket **)packet
CMTime timeStamp = self->_lasttime;
CMTime duration = CMTimeSubtract(self->_duration, self->_lasttime);
SGPacket *pkt = [[SGObjectPool sharedPool] objectWithClass:[SGPacket class] reuseName:[SGPacket commonReuseName]];
pkt.flags |= SGDataFlagPadding;
pkt.core->size = 1;
pkt.core->pts = av_rescale(AV_TIME_BASE, timeStamp.value, timeStamp.timescale);
pkt.core->dts = av_rescale(AV_TIME_BASE, timeStamp.value, timeStamp.timescale);
pkt.core->duration = av_rescale(AV_TIME_BASE, duration.value, duration.timescale);
SGCodecDescriptor *cd = [[SGCodecDescriptor alloc] init];
cd.type = SGCodecTypePadding;
cd.timebase = AV_TIME_BASE_Q;
[pkt setCodecDescriptor:cd];
[pkt fill];
Expand Down
10 changes: 0 additions & 10 deletions SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,8 @@
#import "SGFFmpeg.h"
#import "SGTrack.h"

typedef NS_ENUM(NSUInteger, SGCodecType) {
SGCodecTypeDecode = 0,
SGCodecTypePadding = 1,
};

@interface SGCodecDescriptor : NSObject <NSCopying>

/**
*
*/
@property (nonatomic) SGCodecType type;

/**
*
*/
Expand Down
1 change: 0 additions & 1 deletion SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ - (id)copyWithZone:(NSZone *)zone
- (instancetype)init
{
if (self = [super init]) {
self->_type = SGCodecTypeDecode;
self->_scale = CMTimeMake(1, 1);
self->_timebase = AV_TIME_BASE_Q;
self->_timeRange = CMTimeRangeMake(kCMTimeNegativeInfinity, kCMTimePositiveInfinity);
Expand Down

0 comments on commit 40b68c9

Please sign in to comment.