]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/input/input.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / modules / ffmpeg / producer / input / input.cpp
index 78793ce61197a834b7681dd93e43166e332ad74c..a6611d37e7934e27e509dbeca597f43e0df953ac 100644 (file)
-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-*  This file is part of CasparCG.\r
-*\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
-*\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-*/\r
-#if defined(_MSC_VER)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-\r
-#include "../../stdafx.h"\r
-\r
-#include "input.h"\r
-\r
-#include "../util/util.h"\r
-#include "../../ffmpeg_error.h"\r
-\r
-#include <core/video_format.h>\r
-\r
-#include <common/diagnostics/graph.h>\r
-#include <common/exception/exceptions.h>\r
-#include <common/exception/win32_exception.h>\r
-\r
-#include <tbb/atomic.h>\r
-\r
-#include <agents.h>\r
-#include <concrt_extras.h>\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (push)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-extern "C" \r
-{\r
-       #define __STDC_CONSTANT_MACROS\r
-       #define __STDC_LIMIT_MACROS\r
-       #include <libavformat/avformat.h>\r
-}\r
-#if defined(_MSC_VER)\r
-#pragma warning (pop)\r
-#endif\r
-\r
-\r
-#undef Yield\r
-using namespace Concurrency;\r
-\r
-namespace caspar { namespace ffmpeg {\r
-\r
-static const size_t MAX_PACKETS_SIZE = 16 * 1000000;\r
-static const size_t MAX_PACKETS_COUNT = 50;\r
-       \r
-struct input::implementation : public Concurrency::agent\r
-                                                        , public std::enable_shared_from_this<implementation>\r
-                                                        , boost::noncopyable\r
-{\r
-       input::target_t&                                                target_;\r
-\r
-       const std::wstring                                              filename_;\r
-       const safe_ptr<AVFormatContext>                 format_context_; // Destroy this last\r
-       int                                                                             default_stream_index_;\r
-       const boost::iterator_range<AVStream**> streams_;\r
-\r
-       safe_ptr<diagnostics::graph>                    graph_;\r
-               \r
-       tbb::atomic<bool>                                               loop_;\r
-       const size_t                                                    start_;         \r
-       const size_t                                                    length_;\r
-       size_t                                                                  frame_number_;\r
-                       \r
-       tbb::atomic<size_t>                                             nb_frames_;\r
-       tbb::atomic<size_t>                                             nb_loops_;      \r
-       tbb::atomic<size_t>                                             packets_count_;\r
-       tbb::atomic<size_t>                                             packets_size_;\r
-\r
-       tbb::atomic<bool>                                               is_running_;\r
-\r
-       Concurrency::event                                              event_;\r
-               \r
-public:\r
-       explicit implementation(input::target_t& target,\r
-                                                       const safe_ptr<diagnostics::graph>& graph, \r
-                                                       const std::wstring& filename, \r
-                                                       bool loop, \r
-                                                       size_t start,\r
-                                                       size_t length)\r
-               : target_(target)\r
-               , filename_(filename)\r
-               , format_context_(open_input(filename))         \r
-               , default_stream_index_(av_find_default_stream_index(format_context_.get()))\r
-               , streams_(format_context_->streams, format_context_->streams + format_context_->nb_streams)\r
-               , graph_(graph)\r
-               , start_(start)\r
-               , length_(length)\r
-               , frame_number_(0)\r
-       {               \r
-               event_.set();\r
-               loop_                   = loop;\r
-                               \r
-               //av_dump_format(format_context_.get(), 0, narrow(filename).c_str(), 0);\r
-                               \r
-               if(start_ > 0)                  \r
-                       seek_frame(start_);\r
-                                                               \r
-               graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));\r
-               graph_->set_color("buffer-count", diagnostics::color(0.7f, 0.4f, 0.4f));\r
-               graph_->set_color("buffer-size", diagnostics::color(1.0f, 1.0f, 0.0f)); \r
-                               \r
-               is_running_ = true;\r
-               agent::start();\r
-       }\r
-\r
-       ~implementation()\r
-       {\r
-               if(is_running_)\r
-                       stop();\r
-       }\r
-               \r
-       void stop()\r
-       {\r
-               is_running_ = false;\r
-               event_.set();\r
-               agent::wait(this);\r
-       }\r
-       \r
-       virtual void run()\r
-       {\r
-               win32_exception::install_handler();\r
-\r
-               try\r
-               {\r
-                       for(auto packet = read_next_packet(); packet && is_running_; packet = read_next_packet())\r
-                       {                               \r
-                               Concurrency::asend(target_, make_safe_ptr(packet));\r
-                               Context::Yield();\r
-                               event_.wait();\r
-                       }\r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-               }       \r
-       \r
-               BOOST_FOREACH(auto stream, streams_)\r
-                       Concurrency::send(target_, flush_packet(stream->index));        \r
-               BOOST_FOREACH(auto stream, streams_)\r
-                       Concurrency::send(target_, eof_packet(stream->index));  \r
-\r
-               done();\r
-       }\r
-\r
-       std::shared_ptr<AVPacket> read_next_packet()\r
-       {               \r
-               auto packet = create_packet();\r
-               \r
-               int ret = [&]() -> int\r
-               {\r
-                       Concurrency::scoped_oversubcription_token oversubscribe;\r
-                       return av_read_frame(format_context_.get(), packet.get()); // packet is only valid until next call of av_read_frame. Use av_dup_packet to extend its life.      \r
-               }();\r
-\r
-               if(is_eof(ret))                                                                                                              \r
-               {\r
-                       ++nb_loops_;\r
-                       frame_number_ = 0;\r
-\r
-                       if(loop_)\r
-                       {\r
-                               CASPAR_LOG(trace) << print() << " Looping.";\r
-                               seek_frame(start_, AVSEEK_FLAG_BACKWARD);               \r
-                               return read_next_packet();\r
-                       }       \r
-                       else\r
-                       {\r
-                               CASPAR_LOG(trace) << print() << " Stopping.";\r
-                               return nullptr;\r
-                       }\r
-               }\r
-\r
-               THROW_ON_ERROR(ret, "av_read_frame", print());\r
-\r
-               if(packet->stream_index == default_stream_index_)\r
-               {\r
-                       if(nb_loops_ == 0)\r
-                               ++nb_frames_;\r
-                       ++frame_number_;\r
-               }\r
-\r
-               THROW_ON_ERROR2(av_dup_packet(packet.get()), print());\r
-                               \r
-               // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.\r
-               auto size = packet->size;\r
-               auto data = packet->data;                       \r
-               \r
-               ++packets_count_;\r
-               packets_size_ += size;\r
-               \r
-               graph_->update_value("buffer-size", (packets_size_+0.001)/MAX_PACKETS_SIZE);\r
-               graph_->update_value("buffer-count", (packets_count_+0.001)/MAX_PACKETS_COUNT);\r
-\r
-               auto self = shared_from_this();\r
-               packet = safe_ptr<AVPacket>(packet.get(), [this, self, packet, size, data](AVPacket*)\r
-               {\r
-                       packet->size = size;\r
-                       packet->data = data;\r
-                       --packets_count_;\r
-                       packets_size_ -= size;\r
-                       event_.set();\r
-\r
-                       graph_->update_value("buffer-size", (packets_size_+0.001)/MAX_PACKETS_SIZE);\r
-                       graph_->update_value("buffer-count", (packets_count_+0.001)/MAX_PACKETS_COUNT);\r
-               });\r
-\r
-               if(is_running_ && (packets_count_ > MAX_PACKETS_COUNT || packets_size_ > MAX_PACKETS_SIZE))\r
-                       event_.reset();\r
-                                                                       \r
-               return packet;\r
-       }\r
-\r
-       void seek_frame(int64_t frame, int flags = 0)\r
-       {                       \r
-               if(flags == AVSEEK_FLAG_BACKWARD)\r
-               {\r
-                       // Fix VP6 seeking\r
-                       int vid_stream_index = av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
-                       if(vid_stream_index >= 0)\r
-                       {\r
-                               auto codec_id = format_context_->streams[vid_stream_index]->codec->codec_id;\r
-                               if(codec_id == CODEC_ID_VP6A || codec_id == CODEC_ID_VP6F || codec_id == CODEC_ID_VP6)\r
-                                       flags |= AVSEEK_FLAG_BYTE;\r
-                       }\r
-               }\r
-\r
-               THROW_ON_ERROR2(av_seek_frame(format_context_.get(), default_stream_index_, frame, flags), print());    \r
-               auto packet = create_packet();\r
-               packet->size = 0;\r
-\r
-               BOOST_FOREACH(auto stream, streams_)\r
-                       Concurrency::asend(target_, flush_packet(stream->index));       \r
-\r
-               graph_->add_tag("seek");                \r
-       }               \r
-\r
-       bool is_eof(int ret)\r
-       {\r
-               if(ret == AVERROR(EIO))\r
-                       CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. " << nb_frames_;\r
-               if(ret == AVERROR_EOF)\r
-                       CASPAR_LOG(trace) << print() << " Received EOF. " << nb_frames_;\r
-\r
-               CASPAR_VERIFY(ret >= 0 || ret == AVERROR_EOF || ret == AVERROR(EIO), ffmpeg_error() << source_info(narrow(print())));\r
-\r
-               return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;\r
-       }\r
-       \r
-       std::wstring print() const\r
-       {\r
-               return L"ffmpeg_input[" + filename_ + L")]";\r
-       }\r
-};\r
-\r
-input::input(input::target_t& target,\r
-                        const safe_ptr<diagnostics::graph>& graph, \r
-                        const std::wstring& filename, \r
-                        bool loop, \r
-                        size_t start, \r
-                        size_t length)\r
-       : impl_(new implementation(target, graph, filename, loop, start, length))\r
-{\r
-}\r
-\r
-safe_ptr<AVFormatContext> input::context()\r
-{\r
-       return safe_ptr<AVFormatContext>(impl_->format_context_);\r
-}\r
-\r
-size_t input::nb_frames() const\r
-{\r
-       return impl_->nb_frames_;\r
-}\r
-\r
-size_t input::nb_loops() const \r
-{\r
-       return impl_->nb_loops_;\r
-}\r
-\r
-void input::stop()\r
-{\r
-       impl_->stop();\r
-}\r
-\r
-bool input::loop() const\r
-{\r
-       return impl_->loop_;\r
-}\r
-void input::loop(bool value)\r
-{\r
-       impl_->loop_ = value;\r
-}\r
-\r
-}}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../stdafx.h"
+
+#include "input.h"
+
+#include "../util/util.h"
+#include "../../ffmpeg_error.h"
+
+#include <common/diagnostics/graph.h>
+#include <common/executor.h>
+#include <common/lock.h>
+#include <common/except.h>
+#include <common/log.h>
+
+#include <core/video_format.h>
+
+#include <tbb/concurrent_queue.h>
+#include <tbb/atomic.h>
+#include <tbb/recursive_mutex.h>
+
+#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)
+#endif
+extern "C" 
+{
+       #define __STDC_CONSTANT_MACROS
+       #define __STDC_LIMIT_MACROS
+       #include <libavformat/avformat.h>
+}
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+
+namespace caspar { namespace ffmpeg {
+
+static const int MIN_FRAMES = 25;
+
+class stream
+{
+       stream(const stream&);
+       stream& operator=(const stream&);
+
+       typedef tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>>::size_type size_type;
+
+       int                                                                                                              index_;
+       tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> packets_;
+public:
+
+       stream(int index) 
+               : index_(index)
+       {
+       }
+       
+       void push(const std::shared_ptr<AVPacket>& packet)
+       {
+               if(packet && packet->data && packet->stream_index != index_)
+                       return;
+
+               packets_.push(packet);
+       }
+
+       bool try_pop(std::shared_ptr<AVPacket>& packet)
+       {
+               return packets_.try_pop(packet);
+       }
+
+       void clear()
+       {
+               std::shared_ptr<AVPacket> packet;
+               while(packets_.try_pop(packet));
+       }
+               
+       size_type size() const
+       {
+               return index_ != -1 ? packets_.size() : std::numeric_limits<size_type>::max();
+       }
+};
+               
+struct input::impl : boost::noncopyable
+{              
+       const spl::shared_ptr<diagnostics::graph>                                       graph_;
+
+       const spl::shared_ptr<AVFormatContext>                                          format_context_; // Destroy this last
+       const int                                                                                                       default_stream_index_;
+                       
+       const std::wstring                                                                                      filename_;
+       tbb::atomic<uint32_t>                                                                           start_;         
+       tbb::atomic<uint32_t>                                                                           length_;
+       tbb::atomic<bool>                                                                                       loop_;
+       double                                                                                                          fps_;
+       uint32_t                                                                                                        frame_number_;
+       
+       stream                                                                                                          video_stream_;
+       stream                                                                                                          audio_stream_;
+
+       boost::optional<uint32_t>                                                                       seek_target_;
+
+       tbb::atomic<bool>                                                                                       is_running_;
+       boost::mutex                                                                                            mutex_;
+       boost::condition_variable                                                                       cond_;
+       boost::thread                                                                                           thread_;
+       
+       impl(const spl::shared_ptr<diagnostics::graph> graph, const std::wstring& filename, const bool loop, const uint32_t start, const uint32_t length) 
+               : graph_(graph)
+               , format_context_(open_input(filename))         
+               , default_stream_index_(av_find_default_stream_index(format_context_.get()))
+               , filename_(filename)
+               , frame_number_(0)
+               , fps_(read_fps(*format_context_, 0.0))
+               , video_stream_(av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0))
+               , audio_stream_(av_find_best_stream(format_context_.get(), AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0))
+       {               
+               start_                  = start;
+               length_                 = length;
+               loop_                   = loop;
+               is_running_             = true;
+
+               if(start_ != 0)
+                       seek_target_ = start_;
+                                                                                                               
+               graph_->set_color("seek", diagnostics::color(1.0f, 0.5f, 0.0f));        
+               graph_->set_color("audio-buffer", diagnostics::color(0.7f, 0.4f, 0.4f));
+               graph_->set_color("video-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));        
+               
+               for(int n = 0; n < 8; ++n)
+                       tick();
+
+               thread_ = boost::thread([this]{run();});
+       }
+
+       ~impl()
+       {
+               is_running_ = false;
+               cond_.notify_one();
+               thread_.join();
+       }
+       
+       bool try_pop_video(std::shared_ptr<AVPacket>& packet)
+       {                               
+               bool result = video_stream_.try_pop(packet);
+               if(result)
+                       cond_.notify_one();
+               
+               graph_->set_value("video-buffer", std::min(1.0, static_cast<double>(video_stream_.size()/MIN_FRAMES)));
+                               
+               return result;
+       }
+       
+       bool try_pop_audio(std::shared_ptr<AVPacket>& packet)
+       {                               
+               bool result = audio_stream_.try_pop(packet);
+               if(result)
+                       cond_.notify_one();
+                               
+               graph_->set_value("audio-buffer", std::min(1.0, static_cast<double>(audio_stream_.size()/MIN_FRAMES)));
+
+               return result;
+       }
+
+       void seek(uint32_t target)
+       {
+               {
+                       boost::lock_guard<boost::mutex> lock(mutex_);
+
+                       seek_target_ = target;
+                       video_stream_.clear();
+                       audio_stream_.clear();
+               }
+               
+               cond_.notify_one();
+       }
+               
+       std::wstring print() const
+       {
+               return L"ffmpeg_input[" + filename_ + L")]";
+       }
+
+private:
+       void internal_seek(uint32_t target)
+       {
+               graph_->set_tag("seek");        
+
+               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 codec  = stream->codec;
+               auto fixed_target = (target*stream->time_base.den*codec->time_base.num)/(stream->time_base.num*codec->time_base.den)*codec->ticks_per_frame;
+               
+               THROW_ON_ERROR2(avformat_seek_file(format_context_.get(), default_stream_index_, std::numeric_limits<int64_t>::min(), fixed_target, fixed_target, 0), print());         
+               
+               video_stream_.push(nullptr);
+               audio_stream_.push(nullptr);
+       }
+
+       void tick()
+       {
+               if(seek_target_)                                
+               {
+                       internal_seek(*seek_target_);
+                       seek_target_.reset();
+               }
+
+               auto packet = create_packet();
+               
+               auto ret = av_read_frame(format_context_.get(), packet.get()); // packet is only valid until next call of av_read_frame. Use av_dup_packet to extend its life.  
+               
+               if(is_eof(ret))                                                                                                              
+               {
+                       video_stream_.push(packet);
+                       audio_stream_.push(packet);
+
+                       if(loop_)                       
+                               internal_seek(start_);                          
+               }
+               else
+               {               
+                       THROW_ON_ERROR(ret, "av_read_frame", print());
+                                       
+                       THROW_ON_ERROR2(av_dup_packet(packet.get()), print());
+                               
+                       // Make sure that the packet is correctly deallocated even if size and data is modified during decoding.
+                       const auto size = packet->size;
+                       const auto data = packet->data;
+                       
+                       packet = spl::shared_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)
+                       {
+                               packet->size = size;
+                               packet->data = data;                            
+                       });
+                                       
+                       const auto stream_time_base = format_context_->streams[packet->stream_index]->time_base;
+                       const auto packet_frame_number = static_cast<uint32_t>((static_cast<double>(packet->pts * stream_time_base.num)/stream_time_base.den)*fps_);
+
+                       if(packet->stream_index == default_stream_index_)
+                               frame_number_ = packet_frame_number;
+                                       
+                       if(packet_frame_number >= start_ && packet_frame_number < length_)
+                       {
+                               video_stream_.push(packet);
+                               audio_stream_.push(packet);
+                       }
+               }       
+                                               
+               graph_->set_value("video-buffer", std::min(1.0, static_cast<double>(video_stream_.size()/MIN_FRAMES)));
+               graph_->set_value("audio-buffer", std::min(1.0, static_cast<double>(audio_stream_.size()/MIN_FRAMES)));
+       }
+                       
+       bool full() const
+       {
+               return video_stream_.size() > MIN_FRAMES && audio_stream_.size() > MIN_FRAMES;
+       }
+
+       void run()
+       {
+               win32_exception::install_handler();
+
+               while(is_running_)
+               {
+                       try
+                       {
+                               boost::this_thread::sleep(boost::posix_time::milliseconds(1));
+                               
+                               {
+                                       boost::unique_lock<boost::mutex> lock(mutex_);
+
+                                       while(full() && !seek_target_ && is_running_)
+                                               cond_.wait(lock);
+                                       
+                                       tick();
+                               }
+                       }
+                       catch(...)
+                       {
+                               CASPAR_LOG_CURRENT_EXCEPTION();
+                               is_running_ = false;
+                       }
+               }
+       }
+                       
+       bool is_eof(int ret)
+       {
+               #pragma warning (disable : 4146)
+               return ret == AVERROR_EOF || ret == AVERROR(EIO) || frame_number_ >= length_; // av_read_frame doesn't always correctly return AVERROR_EOF;
+       }
+};
+
+input::input(const spl::shared_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, uint32_t start, uint32_t length) 
+       : impl_(new impl(graph, filename, loop, start, length)){}
+bool input::try_pop_video(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_video(packet);}
+bool input::try_pop_audio(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_audio(packet);}
+AVFormatContext& input::context(){return *impl_->format_context_;}
+void input::loop(bool value){impl_->loop_ = value;}
+bool input::loop() const{return impl_->loop_;}
+void input::seek(uint32_t target){impl_->seek(target);}
+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_;}
+}}