@@ -387,16 +387,21 @@ bool Decoder::init(
387387 for (unsigned int i = 0 ; i < inputCtx_->nb_streams ; i++) {
388388 if (
389389#if LIBAVUTIL_VERSION_MAJOR < 56 // Before FFMPEG 4.0
390- inputCtx_->streams [i]->codec ->codec_type == AVMEDIA_TYPE_VIDEO
390+ inputCtx_->streams [i]->codec ->codec_type == AVMEDIA_TYPE_VIDEO
391391#else // FFMPEG 4.0+
392- inputCtx_->streams [i]->codecpar ->codec_type == AVMEDIA_TYPE_VIDEO
392+ inputCtx_->streams [i]->codecpar ->codec_type == AVMEDIA_TYPE_VIDEO
393393#endif
394- && inputCtx_->streams [i]->duration > 0
395- ) {
396- // There is at least two 1/r_frame_rates from the frame before the last one until the video duration,
397- // let's prefer to set duration after the frame before the last one, but as early as possible
398- double correction = 2 * inputCtx_->streams [i]->r_frame_rate .den / (double ) inputCtx_->streams [i]->r_frame_rate .num - 1 / (double ) AV_TIME_BASE;
399- videoDurationMs_ = 1000 * inputCtx_->streams [i]->duration * inputCtx_->streams [i]->time_base .num / (double ) inputCtx_->streams [i]->time_base .den - 1000 * correction;
394+ && inputCtx_->streams [i]->duration > 0 ) {
395+ // There is at least two 1/r_frame_rates from the frame before the last
396+ // one until the video duration, let's prefer to set duration after the
397+ // frame before the last one, but as early as possible
398+ double correction = 2 * inputCtx_->streams [i]->r_frame_rate .den /
399+ (double )inputCtx_->streams [i]->r_frame_rate .num -
400+ 1 / (double )AV_TIME_BASE;
401+ videoDurationMs_ = 1000 * inputCtx_->streams [i]->duration *
402+ inputCtx_->streams [i]->time_base .num /
403+ (double )inputCtx_->streams [i]->time_base .den -
404+ 1000 * correction;
400405 break ;
401406 }
402407 }
@@ -568,7 +573,6 @@ int Decoder::getFrame(size_t workingTimeInMs) {
568573 continue ;
569574 }
570575
571-
572576 size_t numConsecutiveNoBytes = 0 ;
573577 // it can be only partial decoding of the package bytes
574578 do {
@@ -614,20 +618,27 @@ int Decoder::getFrame(size_t workingTimeInMs) {
614618
615619 if (params_.uniformSampling > 1 ) {
616620 if (doSeek_) {
617- double duration = videoDurationMs_ > 0 ? videoDurationMs_ : params_.expectedDuration ;
618- double step = (duration * AV_TIME_BASE) / (1000 * (params_.uniformSampling - 1 ));
619- avformat_seek_file (inputCtx_, -1 , static_cast <int64_t >(step * kFramesDecoded_ ) + 1 , static_cast <int64_t >(step * (kFramesDecoded_ + 1 )), static_cast <int64_t >(step * (kFramesDecoded_ + 1 )), 0 );
621+ double duration =
622+ videoDurationMs_ > 0 ? videoDurationMs_ : params_.expectedDuration ;
623+ double step =
624+ (duration * AV_TIME_BASE) / (1000 * (params_.uniformSampling - 1 ));
625+ avformat_seek_file (
626+ inputCtx_,
627+ -1 ,
628+ static_cast <int64_t >(step * kFramesDecoded_ ) + 1 ,
629+ static_cast <int64_t >(step * (kFramesDecoded_ + 1 )),
630+ static_cast <int64_t >(step * (kFramesDecoded_ + 1 )),
631+ 0 );
620632 ++kFramesDecoded_ ;
621633 doSeek_ = false ;
622634 }
623635 }
624636 }
625637
626638 av_packet_free (&avPacket);
627- VLOG (2 ) << " Interrupted loop"
628- << " , interrupted_ " << interrupted_ << " , inRange_.any() "
629- << inRange_.any () << " , decodedFrame " << decodedFrame << " , result "
630- << result;
639+ VLOG (2 ) << " Interrupted loop" << " , interrupted_ " << interrupted_
640+ << " , inRange_.any() " << inRange_.any () << " , decodedFrame "
641+ << decodedFrame << " , result " << result;
631642
632643 // loop can be terminated, either by:
633644 // 1. explicitly interrupted
@@ -691,7 +702,7 @@ int Decoder::processPacket(
691702 startCondition = msg.header .pts >= params_.startOffset ;
692703 }
693704 if (endInRange && startCondition) {
694- *hasMsg = pushMsg (std::move (msg));
705+ *hasMsg = pushMsg (std::move (msg));
695706 }
696707 }
697708 return result;
@@ -706,9 +717,12 @@ bool Decoder::pushMsg(DecoderOutputMessage&& msg) {
706717 return true ;
707718 }
708719
709- double duration = videoDurationMs_ > 0 ? videoDurationMs_ : params_.expectedDuration ;
710- double step = (duration * AV_TIME_BASE) / (1000 * (params_.uniformSampling - 1 ));
711- if (pastDecodedPTS_ < step * kFramesDecoded_ && step * kFramesDecoded_ <= currentDecodedPTS_) {
720+ double duration =
721+ videoDurationMs_ > 0 ? videoDurationMs_ : params_.expectedDuration ;
722+ double step =
723+ (duration * AV_TIME_BASE) / (1000 * (params_.uniformSampling - 1 ));
724+ if (pastDecodedPTS_ < step * kFramesDecoded_ &&
725+ step * kFramesDecoded_ <= currentDecodedPTS_) {
712726 push (std::move (msg));
713727 doSeek_ = true ;
714728 return true ;
0 commit comments