diff --git a/SGPlayer/Classes/Core/SGData/SGData.h b/SGPlayer/Classes/Core/SGData/SGData.h index 8c54e688..28c8ff5c 100644 --- a/SGPlayer/Classes/Core/SGData/SGData.h +++ b/SGPlayer/Classes/Core/SGData/SGData.h @@ -9,8 +9,20 @@ #import #import +/** + * + */ +typedef NS_OPTIONS(NSUInteger, SGDataFlags) { + SGDataFlagPadding = 1 << 0, +}; + @protocol SGData +/** + * + */ +@property (nonatomic) SGDataFlags flags; + /** * */ diff --git a/SGPlayer/Classes/Core/SGData/SGFrame.m b/SGPlayer/Classes/Core/SGData/SGFrame.m index 6a57479e..af7d61cc 100644 --- a/SGPlayer/Classes/Core/SGData/SGFrame.m +++ b/SGPlayer/Classes/Core/SGData/SGFrame.m @@ -21,6 +21,7 @@ @interface SGFrame () @implementation SGFrame +@synthesize flags = _flags; @synthesize reuseName = _reuseName; - (instancetype)init @@ -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; diff --git a/SGPlayer/Classes/Core/SGData/SGPacket.m b/SGPlayer/Classes/Core/SGData/SGPacket.m index a780b612..6e5371d9 100644 --- a/SGPlayer/Classes/Core/SGData/SGPacket.m +++ b/SGPlayer/Classes/Core/SGData/SGPacket.m @@ -21,6 +21,7 @@ @interface SGPacket () @implementation SGPacket +@synthesize flags = _flags; @synthesize reuseName = _reuseName; - (instancetype)init @@ -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; diff --git a/SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m b/SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m index 35764bd4..0fae6e40 100644 --- a/SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m +++ b/SGPlayer/Classes/Core/SGDecoder/SGAudioDecoder.m @@ -88,33 +88,28 @@ - (void)flush [self setup]; } [cd fillToDescriptor:self->_codecDescriptor]; - switch (packet.codecDescriptor.type) { - case SGCodecTypeDecode: { - NSArray *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 *objs = [self processPacket:packet]; + for (SGFrame *obj in objs) { + [ret addObject:obj]; } - break; } return ret; } diff --git a/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m b/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m index 5795b4ee..c2aa07cd 100644 --- a/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m +++ b/SGPlayer/Classes/Core/SGDecoder/SGVideoDecoder.m @@ -94,18 +94,13 @@ - (void)flush [self setup]; } [cd fillToDescriptor:self->_codecDescriptor]; - switch (packet.codecDescriptor.type) { - case SGCodecTypeDecode: { - NSArray *objs = [self processPacket:packet]; - for (SGFrame *obj in objs) { - [ret addObject:obj]; - } - } - break; - case SGCodecTypePadding: { - + if (packet.flags & SGDataFlagPadding) { + + } else { + NSArray *objs = [self processPacket:packet]; + for (SGFrame *obj in objs) { + [ret addObject:obj]; } - break; } self->_flags.outputCount += ret.count; return ret; @@ -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]; diff --git a/SGPlayer/Classes/Core/SGDemuxer/SGPaddingDemuxer.m b/SGPlayer/Classes/Core/SGDemuxer/SGPaddingDemuxer.m index 335f776b..d3709185 100644 --- a/SGPlayer/Classes/Core/SGDemuxer/SGPaddingDemuxer.m +++ b/SGPlayer/Classes/Core/SGDemuxer/SGPaddingDemuxer.m @@ -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]; diff --git a/SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.h b/SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.h index 6ea9a8b6..8f3d4a68 100644 --- a/SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.h +++ b/SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.h @@ -11,18 +11,8 @@ #import "SGFFmpeg.h" #import "SGTrack.h" -typedef NS_ENUM(NSUInteger, SGCodecType) { - SGCodecTypeDecode = 0, - SGCodecTypePadding = 1, -}; - @interface SGCodecDescriptor : NSObject -/** - * - */ -@property (nonatomic) SGCodecType type; - /** * */ diff --git a/SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.m b/SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.m index be54b65a..3a0ae125 100644 --- a/SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.m +++ b/SGPlayer/Classes/Core/SGDescription/SGCodecDescriptor.m @@ -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);