]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/video/video_decoder.cpp
[ffmpeg] Copied flush logic when seeking from 2.0, as well as current frame in clip...
[casparcg] / modules / ffmpeg / producer / video / video_decoder.cpp
index 2778a5cebe08b9646d766a52263280f55c9884b4..bc379a8c0b9378d1aab1517dbef8d9f9e2d1e1f8 100644 (file)
@@ -34,6 +34,8 @@
 
 #include <boost/filesystem.hpp>
 
+#include <tbb/atomic.h>
+
 #include <queue>
 
 #if defined(_MSC_VER)
@@ -55,7 +57,7 @@ struct video_decoder::impl : boost::noncopyable
 {
        core::monitor::subject                                  monitor_subject_;
        input*                                                                  input_;
-       int                                                                             index_;
+       int                                                                             index_                          = -1;
        const spl::shared_ptr<AVCodecContext>   codec_context_;
 
        std::queue<spl::shared_ptr<AVPacket>>   packets_;
@@ -65,9 +67,9 @@ struct video_decoder::impl : boost::noncopyable
        const int                                                               width_;
        const int                                                               height_;
 
-       bool                                                                    is_progressive_;
-       uint32_t                                                                file_frame_number_;
-       double                                                                  fps_;
+       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)
-               , fps_(read_fps(input_->context(), 0.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,10 +137,7 @@ public:
                if(got_frame == 0)      
                        return nullptr;
                
-               auto stream_time_base    = stream_->time_base;
-               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;
                
@@ -153,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
@@ -170,6 +171,7 @@ int video_decoder::width() const{return impl_->width_;}
 int video_decoder::height() const{return impl_->height_;}
 uint32_t video_decoder::nb_frames() const{return impl_->nb_frames();}
 uint32_t video_decoder::file_frame_number() const{return impl_->file_frame_number_;}
+boost::rational<int> video_decoder::framerate() const { return impl_->framerate_; }
 bool video_decoder::is_progressive() const{return impl_->is_progressive_;}
 std::wstring video_decoder::print() const{return impl_->print();}
 core::monitor::subject& video_decoder::monitor_output() { return impl_->monitor_subject_; }