]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/input/input.cpp
[ffmpeg_producer] Remove unused flags variable in queued_seek
[casparcg] / modules / ffmpeg / producer / input / input.cpp
index bc1730314715698ac072490a4eea1a07a58e8fe4..c4c3b73446824f74efa6a8f02308a6f7f7cdfbd6 100644 (file)
 #include <common/executor.h>
 #include <common/except.h>
 #include <common/os/general_protection_fault.h>
+#include <common/param.h>
+#include <common/scope_exit.h>
 
 #include <tbb/concurrent_queue.h>
 #include <tbb/atomic.h>
 #include <tbb/recursive_mutex.h>
 
-#include <boost/rational.hpp>
-#include <boost/range/algorithm.hpp>
-#include <boost/thread/condition_variable.hpp>
-#include <boost/thread/mutex.hpp>
-#include <boost/thread/thread.hpp>
-
 #if defined(_MSC_VER)
 #pragma warning (push)
 #pragma warning (disable : 4244)
@@ -65,7 +61,7 @@ static const size_t MIN_BUFFER_COUNT    = 50;
 static const size_t MAX_BUFFER_SIZE     = 64 * 1000000;
 
 namespace caspar { namespace ffmpeg {
-struct input::implementation : boost::noncopyable
+struct input::impl : boost::noncopyable
 {
        const spl::shared_ptr<diagnostics::graph>                                       graph_;
 
@@ -73,22 +69,21 @@ struct input::implementation : boost::noncopyable
        const int                                                                                                       default_stream_index_   = av_find_default_stream_index(format_context_.get());
 
        const std::wstring                                                                                      filename_;
-       tbb::atomic<uint32_t>                                                                           start_;
-       tbb::atomic<uint32_t>                                                                           length_;
+       tbb::atomic<uint32_t>                                                                           in_;
+       tbb::atomic<uint32_t>                                                                           out_;
        const bool                                                                                                      thumbnail_mode_;
        tbb::atomic<bool>                                                                                       loop_;
-       uint32_t                                                                                                        frame_number_                   = 0;
-       boost::rational<int>                                                                            framerate_                              = read_framerate(*format_context_, 1);
+       uint32_t                                                                                                        file_frame_number_              = 0;
 
        tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>>        buffer_;
        tbb::atomic<size_t>                                                                                     buffer_size_;
 
        executor                                                                                                        executor_;
 
-       explicit implementation(const spl::shared_ptr<diagnostics::graph> graph, const std::wstring& filename, FFMPEG_Resource resource_type, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_options& vid_params)
+       explicit impl(const spl::shared_ptr<diagnostics::graph> graph, const std::wstring& url_or_file, bool loop, uint32_t in, uint32_t out, bool thumbnail_mode, const ffmpeg_options& vid_params)
                : graph_(graph)
-               , format_context_(open_input(filename, resource_type, vid_params))
-               , filename_(filename)
+               , format_context_(open_input(url_or_file, vid_params))
+               , filename_(url_or_file)
                , thumbnail_mode_(thumbnail_mode)
                , executor_(print())
        {
@@ -98,13 +93,13 @@ struct input::implementation : boost::noncopyable
                                enable_quiet_logging_for_thread();
                        });
 
-               start_                  = start;
-               length_                 = length;
+               in_                             = in;
+               out_                    = out;
                loop_                   = loop;
                buffer_size_    = 0;
 
-               if(start_ > 0)
-                       queued_seek(start_);
+               if(in_ > 0)
+                       queued_seek(in_);
 
                graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));
                graph_->set_color("buffer-count", diagnostics::color(0.7f, 0.4f, 0.4f));
@@ -187,23 +182,33 @@ struct input::implementation : boost::noncopyable
 
                                if(is_eof(ret))
                                {
-                                       frame_number_   = 0;
+                                       file_frame_number_ = 0;
 
                                        if(loop_)
                                        {
-                                               queued_seek(start_);
+                                               queued_seek(in_);
                                                graph_->set_tag(diagnostics::tag_severity::INFO, "seek");
                                                CASPAR_LOG(trace) << print() << " Looping.";
                                        }
                                        else
+                                       {
+                                               // Needed by some decoders to decode remaining frames based on last packet.
+                                               auto flush_packet = create_packet();
+                                               flush_packet->data = nullptr;
+                                               flush_packet->size = 0;
+                                               flush_packet->pos = -1;
+
+                                               buffer_.push(flush_packet);
+
                                                executor_.stop();
+                                       }
                                }
                                else
                                {
                                        THROW_ON_ERROR(ret, "av_read_frame", print());
 
                                        if(packet->stream_index == default_stream_index_)
-                                               ++frame_number_;
+                                               ++file_frame_number_;
 
                                        THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
 
@@ -235,69 +240,59 @@ struct input::implementation : boost::noncopyable
                });
        }
 
-       spl::shared_ptr<AVFormatContext> open_input(const std::wstring resource_name, FFMPEG_Resource resource_type, const ffmpeg_options& vid_params)
+       spl::shared_ptr<AVFormatContext> open_input(const std::wstring& url_or_file, const ffmpeg_options& vid_params)
        {
-               AVFormatContext* weak_context = nullptr;
+               AVDictionary* format_options = nullptr;
 
-               switch (resource_type) {
-               case FFMPEG_Resource::FFMPEG_FILE:
-                       THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(resource_name).c_str(), nullptr, nullptr), resource_name);
-                       break;
-               case FFMPEG_Resource::FFMPEG_DEVICE:
-                       {
-                               AVDictionary* format_options = NULL;
-                               for (auto& option  : vid_params)
-                               {
-                                       av_dict_set(&format_options, option.first.c_str(), option.second.c_str(), 0);
-                               }
-                               AVInputFormat* input_format = av_find_input_format("dshow");
-                               THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(resource_name).c_str(), input_format, &format_options), resource_name);
-                               if (format_options != nullptr)
-                               {
-                                       std::string unsupported_tokens = "";
-                                       AVDictionaryEntry *t = NULL;
-                                       while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)
-                                       {
-                                               if (!unsupported_tokens.empty())
-                                                       unsupported_tokens += ", ";
-                                               unsupported_tokens += t->key;
-                                       }
-                                       avformat_close_input(&weak_context);
-                                       BOOST_THROW_EXCEPTION(ffmpeg_error() << msg_info(unsupported_tokens));
-                               }
-                               av_dict_free(&format_options);
-                       }
-                       break;
-               case FFMPEG_Resource::FFMPEG_STREAM:
-                       {
-                               AVDictionary* format_options = NULL;
-                               for (auto& option : vid_params)
-                               {
-                                       av_dict_set(&format_options, option.first.c_str(), option.second.c_str(), 0);
-                               }
-                               THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(resource_name).c_str(), nullptr, &format_options), resource_name);
-                               if (format_options != nullptr)
-                               {
-                                       std::string unsupported_tokens = "";
-                                       AVDictionaryEntry *t = NULL;
-                                       while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)
-                                       {
-                                               if (!unsupported_tokens.empty())
-                                                       unsupported_tokens += ", ";
-                                               unsupported_tokens += t->key;
-                                       }
-                                       avformat_close_input(&weak_context);
-                                       BOOST_THROW_EXCEPTION(ffmpeg_error() << msg_info(unsupported_tokens));
-                               }
+               CASPAR_SCOPE_EXIT
+               {
+                       if (format_options)
                                av_dict_free(&format_options);
-                       }
-                       break;
                };
-               spl::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* p)
+
+               for (auto& option : vid_params)
+                       av_dict_set(&format_options, option.first.c_str(), option.second.c_str(), 0);
+
+               auto resource_name                      = std::wstring();
+               auto parts                                      = caspar::protocol_split(url_or_file);
+               auto protocol                           = parts.at(0);
+               auto path                                       = parts.at(1);
+               AVInputFormat* input_format     = nullptr;
+
+               static const std::set<std::wstring> PROTOCOLS_TREATED_AS_FORMATS = { L"dshow", L"v4l2" };
+
+               if (protocol.empty())
+                       resource_name = path;
+               else if (PROTOCOLS_TREATED_AS_FORMATS.find(protocol) != PROTOCOLS_TREATED_AS_FORMATS.end())
+               {
+                       input_format = av_find_input_format(u8(protocol).c_str());
+                       resource_name = path;
+               }
+               else
+                       resource_name = protocol + L"://" + path;
+
+               AVFormatContext* weak_context = nullptr;
+               THROW_ON_ERROR2(avformat_open_input(&weak_context, u8(resource_name).c_str(), input_format, &format_options), resource_name);
+
+               spl::shared_ptr<AVFormatContext> context(weak_context, [](AVFormatContext* ptr)
                {
-                       avformat_close_input(&p);
+                       avformat_close_input(&ptr);
                });
-               THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), resource_name);
+
+               if (format_options)
+               {
+                       std::string unsupported_tokens = "";
+                       AVDictionaryEntry *t = NULL;
+                       while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)
+                       {
+                               if (!unsupported_tokens.empty())
+                                       unsupported_tokens += ", ";
+                               unsupported_tokens += t->key;
+                       }
+                       CASPAR_THROW_EXCEPTION(user_error() << msg_info(unsupported_tokens));
+               }
+
+               THROW_ON_ERROR2(avformat_find_stream_info(context.get(), nullptr), resource_name);
                fix_meta_data(*context);
                return context;
        }
@@ -339,22 +334,8 @@ struct input::implementation : boost::noncopyable
                if (!thumbnail_mode_)
                        CASPAR_LOG(debug) << print() << " Seeking: " << target;
 
-               int flags = AVSEEK_FLAG_FRAME;
-               if(target == 0)
-               {
-                       // Fix VP6 seeking
-                       int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);
-                       if(vid_stream_index >= 0)
-                       {
-                               auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;
-                               if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)
-                                       flags = AVSEEK_FLAG_BYTE;
-                       }
-               }
-
                auto stream = format_context_->streams[default_stream_index_];
 
-
                auto fps = read_fps(*format_context_, 0.0);
 
                THROW_ON_ERROR2(avformat_seek_file(
@@ -365,6 +346,8 @@ struct input::implementation : boost::noncopyable
                        std::numeric_limits<int64_t>::max(),
                        0), print());
 
+               file_frame_number_ = target;
+
                auto flush_packet       = create_packet();
                flush_packet->data      = nullptr;
                flush_packet->size      = 0;
@@ -380,32 +363,28 @@ struct input::implementation : boost::noncopyable
                if(ret == AVERROR_EOF)
                        CASPAR_LOG(trace) << print() << " Received EOF. ";
 
-               return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
+               return ret == AVERROR_EOF || ret == AVERROR(EIO) || file_frame_number_ >= out_; // av_read_frame doesn't always correctly return AVERROR_EOF;
        }
 
        int num_audio_streams() const
        {
                return 0; // TODO
        }
-
-       boost::rational<int> framerate() const
-       {
-               return framerate_;
-       }
 };
 
-input::input(const spl::shared_ptr<diagnostics::graph>& graph, const std::wstring& filename, FFMPEG_Resource resource_type, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_options& vid_params)
-       : impl_(new implementation(graph, filename, resource_type, loop, start, length, thumbnail_mode, vid_params)){}
+input::input(const spl::shared_ptr<diagnostics::graph>& graph, const std::wstring& url_or_file, bool loop, uint32_t in, uint32_t out, bool thumbnail_mode, const ffmpeg_options& vid_params)
+       : impl_(new impl(graph, url_or_file, loop, in, out, thumbnail_mode, vid_params)){}
 bool input::eof() const {return !impl_->executor_.is_running();}
 bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}
 spl::shared_ptr<AVFormatContext> input::context(){return impl_->format_context_;}
-void input::start(uint32_t value){impl_->start_ = value;}
-uint32_t input::start() const{return impl_->start_;}
-void input::length(uint32_t value){impl_->length_ = value;}
-uint32_t input::length() const{return impl_->length_;}
+void input::in(uint32_t value){impl_->in_ = value;}
+uint32_t input::in() const{return impl_->in_;}
+void input::out(uint32_t value){impl_->out_ = value;}
+uint32_t input::out() const{return impl_->out_;}
+void input::length(uint32_t value){impl_->out_ = impl_->in_ + value;}
+uint32_t input::length() const{return impl_->out_ - impl_->in_;}
 void input::loop(bool value){impl_->loop_ = value;}
 bool input::loop() const{return impl_->loop_;}
 int input::num_audio_streams() const { return impl_->num_audio_streams(); }
-boost::rational<int> input::framerate() const { return impl_->framerate(); }
 std::future<bool> input::seek(uint32_t target){return impl_->seek(target);}
 }}