FFmpeg跳至最新帧

时间:2019-03-07 13:15:32

标签: objective-c ffmpeg

我正在寻找有关删除/跳过FFmpeg帧的帮助。我正在处理的项目是实时视频流,当应用程序进入后台时,返回到活动状态后,视频流将花费很长时间来将自身快速转发到当前帧。这不是理想的选择,我的目标是让应用程序立即跳转到最新的框架。

我需要做的是删除快速转发的帧数,以赶上最新的帧。这可能吗?这是我当前对帧进行解码的代码:

- (NSArray *) decodeFrames: (CGFloat) minDuration
{
    NSMutableArray *result = [NSMutableArray array];

    @synchronized (lock) {

        if([_reading integerValue] != 1){

            _reading = [NSNumber numberWithInt:1];

            @synchronized (_seekPosition) {
                if([_seekPosition integerValue] != -1 && _seekPosition){
                    [self seekDecoder:[_seekPosition longLongValue]];
                    _seekPosition = [NSNumber numberWithInt:-1];
                }
            }

            if (_videoStream == -1 &&
                _audioStream == -1)
                return nil;

            AVPacket packet;

            CGFloat decodedDuration = 0;

            CGFloat totalDuration = [TimeHelper calculateTimeDifference];

            do {

                BOOL finished = NO;
                int count = 0;


                while (!finished) {

                    if (av_read_frame(_formatCtx, &packet) < 0) {
                        _isEOF = YES;
                        [self endOfFileReached];
                        break;
                    }

                    [self frameRead];

                    if (packet.stream_index ==_videoStream) {

                        int pktSize = packet.size;

                        while (pktSize > 0) {

                            int gotframe = 0;
                            int len = avcodec_decode_video2(_videoCodecCtx,
                                                            _videoFrame,
                                                            &gotframe,
                                                            &packet);

                            if (len < 0) {
                                LoggerVideo(0, @"decode video error, skip packet");
                                break;
                            }

                            if (gotframe) {


                                    if (!_disableDeinterlacing &&
                                        _videoFrame->interlaced_frame) {

                                        avpicture_deinterlace((AVPicture*)_videoFrame,
                                                              (AVPicture*)_videoFrame,
                                                              _videoCodecCtx->pix_fmt,
                                                              _videoCodecCtx->width,
                                                              _videoCodecCtx->height);
                                    }

                                    KxVideoFrame *frame = [self handleVideoFrame];
                                    if (frame) {

                                        [result addObject:frame];
                                        _position = frame.position;
                                        decodedDuration += frame.duration;
                                        if (decodedDuration > minDuration)
                                            finished = YES;
                            }



                            } else {
                                count++;
                            }

                            if (0 == len)
                                break;

                            pktSize -= len;
                        }
                    }
                    av_free_packet(&packet);
                }
            } while (totalDuration > 0);
            _reading = [NSNumber numberWithInt:0];
            return result;
        }

    }

    return result;

0 个答案:

没有答案