]> git.sesse.net Git - casparcg/commitdiff
[ffmpeg] Copied flush logic when seeking from 2.0, as well as current frame in clip...
authorHelge Norberg <helge.norberg@svt.se>
Fri, 9 Sep 2016 15:04:51 +0000 (17:04 +0200)
committerHelge Norberg <helge.norberg@svt.se>
Fri, 9 Sep 2016 15:04:51 +0000 (17:04 +0200)
modules/ffmpeg/ffmpeg_pipeline_backend_internal.cpp
modules/ffmpeg/producer/audio/audio_decoder.cpp
modules/ffmpeg/producer/input/input.cpp
modules/ffmpeg/producer/util/util.cpp
modules/ffmpeg/producer/util/util.h
modules/ffmpeg/producer/video/video_decoder.cpp

index 663d7dee3274edc02e589ec87f0d6c1ab57bba38..9feb2d734c147d6d7c42a7e999c9d17a94930290 100644 (file)
@@ -357,7 +357,7 @@ public:
                                {
                                        frame = (*a_decoder)();
 
-                                       if (frame && frame->data[0])
+                                       if (frame == flush() || (frame && frame->data[0]))
                                                break;
                                        else
                                                frame.reset();
@@ -376,7 +376,7 @@ public:
                        {
                                frame = (*v_decoder)();
 
-                               if (frame && frame->data[0])
+                               if (frame == flush() || (frame && frame->data[0]))
                                        return { frame };
                        }
                }
@@ -604,11 +604,12 @@ struct sink
        virtual void                                                    framerate(boost::rational<int> framerate)                                                                               { CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info(print() + L" not an encoder.")); }
        virtual void                                                    start(bool has_audio, bool has_video)                                                                                   { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
        virtual void                                                    stop()                                                                                                                                                  { }
+       virtual void                                                    flush_all()                                                                                                                                             { }
        virtual std::vector<AVSampleFormat>             supported_sample_formats() const                                                                                                { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
        virtual std::vector<int>                                supported_samplerates() const                                                                                                   { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
        virtual std::vector<AVPixelFormat>              supported_pixel_formats() const                                                                                                 { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
        virtual int                                                             wanted_num_audio_streams() const                                                                                                { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
-       virtual boost::optional<int>                    wanted_num_channels_per_stream() const                                                                          { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
+       virtual boost::optional<int>                    wanted_num_channels_per_stream() const                                                                                  { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
        virtual boost::optional<AVMediaType>    try_push(AVMediaType type, int stream_index, spl::shared_ptr<AVFrame> frame)    { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
        virtual void                                                    eof()                                                                                                                                                   { CASPAR_THROW_EXCEPTION(not_implemented() << msg_info(print())); }
 };
@@ -738,6 +739,14 @@ public:
                return boost::none;
        }
 
+       void flush_all() override
+       {
+               audio_samples_.clear();
+
+               while (!video_frames_.empty())
+                       video_frames_.pop();
+       }
+
        boost::optional<AVMediaType> try_push(AVMediaType type, int stream_index, spl::shared_ptr<AVFrame> av_frame) override
        {
                if (!has_audio_ && !has_video_)
@@ -1110,8 +1119,21 @@ private:
                        {
                                auto needed                                             = *result;
                                auto input_frames_for_streams   = source_->get_input_frames_for_streams(needed);
+                               bool flush_all                                  = !input_frames_for_streams.empty() && input_frames_for_streams.at(0) == flush();
+
+                               if (flush_all)
+                               {
+                                       sink_->flush_all();
+                                       
+                                       if (source_->has_audio() && source_->has_video())
+                                               result = needed == AVMediaType::AVMEDIA_TYPE_AUDIO ? AVMediaType::AVMEDIA_TYPE_VIDEO : AVMediaType::AVMEDIA_TYPE_AUDIO;
+
+                                       continue;
+                               }
+
+                               bool got_requested_media_type   = !input_frames_for_streams.empty() && input_frames_for_streams.at(0);
 
-                               if (!input_frames_for_streams.empty() && input_frames_for_streams.at(0))
+                               if (got_requested_media_type)
                                {
                                        for (int input_stream_index = 0; input_stream_index < input_frames_for_streams.size(); ++input_stream_index)
                                        {
index 2366c10d8032260be15a9ca11197455926c528ce..e92a7c18134859e05370fc65b13e6fb18a5d720e 100644 (file)
@@ -108,17 +108,17 @@ public:
                
                std::shared_ptr<AVFrame> audio;
 
-               if(!current_packet_)    
-               {
-                       avcodec_flush_buffers(codec_context_.get());    
-               }
-               else if(!current_packet_->data)
+               if (!current_packet_->data)
                {
                        if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)                       
                                audio = decode(*current_packet_);
                        
-                       if(!audio)
+                       if (!audio)
+                       {
+                               avcodec_flush_buffers(codec_context_.get());
                                current_packet_.reset();
+                               audio = flush();
+                       }
                }
                else
                {
index 13ca48d5e16aee23ae0d95e2536ea66c2f15eccf..c51cf152d406c503b0807e55e139664eba10a045 100644 (file)
@@ -281,7 +281,7 @@ private:
                
                auto stream                             = format_context_->streams[default_stream_index_];
                auto fps                                = read_fps(*format_context_, 0.0);
-               auto target_timestamp = static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num);
+               auto target_timestamp   = static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num);
                
                THROW_ON_ERROR2(avformat_seek_file(
                                format_context_.get(),
@@ -290,11 +290,16 @@ private:
                                target_timestamp,
                                std::numeric_limits<int64_t>::max(),
                                0), print());
+
+               auto flush_packet       = create_packet();
+               flush_packet->data      = nullptr;
+               flush_packet->size      = 0;
+               flush_packet->pos       = target;
                
-               video_stream_.push(nullptr);
+               video_stream_.push(flush_packet);
 
                for (auto& audio_stream : audio_streams_)
-                       audio_stream.push(nullptr);
+                       audio_stream.push(flush_packet);
        }
 
        void tick()
index 3546305f55ae61eeee43b1dd0d33a08b249c96ed..eb773c6c115f5818572bdee16ddd1072e409a4ba 100644 (file)
@@ -491,6 +491,16 @@ spl::shared_ptr<AVFrame> create_frame()
        return frame;
 }
 
+std::shared_ptr<AVFrame> flush()
+{
+       static std::shared_ptr<AVFrame> dummy(av_frame_alloc(), [](AVFrame* p)
+       {
+               av_frame_free(&p);
+       });
+
+       return dummy;
+}
+
 spl::shared_ptr<AVCodecContext> open_codec(AVFormatContext& context, enum AVMediaType type, int& index, bool single_threaded)
 {      
        AVCodec* decoder;
index 626cccfb454639d6f6ed80c4838acf38fdcfecc3..abd167a0180f3708a2105ec736db7a33b49c001c 100644 (file)
@@ -71,6 +71,8 @@ spl::shared_ptr<AVFormatContext> open_input(const std::wstring& filename);
 bool is_sane_fps(AVRational time_base);
 AVRational fix_time_base(AVRational time_base);
 
+std::shared_ptr<AVFrame> flush();
+
 double read_fps(AVFormatContext& context, double fail_value);
 boost::rational<int> read_framerate(AVFormatContext& context, const boost::rational<int>& fail_value);
 
index 87ee744dc8a56133de3d5c9808f72ae30875c147..bc379a8c0b9378d1aab1517dbef8d9f9e2d1e1f8 100644 (file)
@@ -34,6 +34,8 @@
 
 #include <boost/filesystem.hpp>
 
+#include <tbb/atomic.h>
+
 #include <queue>
 
 #if defined(_MSC_VER)
@@ -65,8 +67,8 @@ struct video_decoder::impl : boost::noncopyable
        const int                                                               width_;
        const int                                                               height_;
 
-       bool                                                                    is_progressive_;
-       uint32_t                                                                file_frame_number_;
+       tbb::atomic<bool>                                               is_progressive_;
+       tbb::atomic<uint32_t>                                   file_frame_number_;
        boost::rational<int>                                    framerate_;
        
        std::shared_ptr<AVPacket>                               current_packet_;
@@ -79,9 +81,10 @@ public:
                , nb_frames_(static_cast<uint32_t>(stream_->nb_frames))
                , width_(codec_context_->width)
                , height_(codec_context_->height)
-               , file_frame_number_(0)
                , framerate_(read_framerate(input_->context(), 0))
        {
+               is_progressive_ = false;
+               file_frame_number_ = 0;
        }
        
        std::shared_ptr<AVFrame> poll()
@@ -91,17 +94,18 @@ public:
                
                std::shared_ptr<AVFrame> frame;
 
-               if(!current_packet_)            
-               {
-                       avcodec_flush_buffers(codec_context_.get());    
-               }
-               else if(!current_packet_->data)
+               if (!current_packet_->data)
                {
                        if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)                       
                                frame = decode(*current_packet_);
                        
-                       if(!frame)
+                       if (!frame)
+                       {
+                               file_frame_number_ = static_cast<uint32_t>(current_packet_->pos);
+                               avcodec_flush_buffers(codec_context_.get());
                                current_packet_.reset();
+                               frame = flush();
+                       }
                }
                else
                {
@@ -133,11 +137,7 @@ public:
                if(got_frame == 0)      
                        return nullptr;
                
-               auto stream_time_base           = stream_->time_base;
-               auto fps = static_cast<double>(framerate_.numerator()) / static_cast<double>(framerate_.denominator());
-               auto packet_frame_number        = static_cast<uint32_t>((static_cast<double>(pkt.pts * stream_time_base.num) / stream_time_base.den) * fps);
-
-               file_frame_number_ = packet_frame_number;
+               ++file_frame_number_;
 
                is_progressive_ = !frame->interlaced_frame;
                
@@ -154,7 +154,7 @@ public:
        
        uint32_t nb_frames() const
        {
-               return std::max(nb_frames_, file_frame_number_);
+               return std::max(nb_frames_, static_cast<uint32_t>(file_frame_number_));
        }
 
        std::wstring print() const