/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright 2013 Sveriges Television AB http://casparcg.com/\r
*\r
-* This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\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
+* 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
+* 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
+* Author: Robert Nagy, ronag89@gmail.com\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 "../util/flv.h"\r
#include "../../ffmpeg_error.h"\r
+#include "../../ffmpeg_params.h"\r
+#include "../../ffmpeg.h"\r
\r
#include <core/video_format.h>\r
\r
#include <common/diagnostics/graph.h>\r
+#include <common/concurrency/executor.h>\r
+#include <common/concurrency/future_util.h>\r
#include <common/exception/exceptions.h>\r
#include <common/exception/win32_exception.h>\r
\r
#include <tbb/atomic.h>\r
#include <tbb/recursive_mutex.h>\r
\r
+#include <boost/rational.hpp>\r
#include <boost/range/algorithm.hpp>\r
#include <boost/thread/condition_variable.hpp>\r
#include <boost/thread/mutex.hpp>\r
#pragma warning (pop)\r
#endif\r
\r
-namespace caspar { namespace ffmpeg {\r
+static const size_t MAX_BUFFER_COUNT = 100;\r
+static const size_t MAX_BUFFER_COUNT_RT = 3;\r
+static const size_t MIN_BUFFER_COUNT = 50;\r
+static const size_t MAX_BUFFER_SIZE = 64 * 1000000;\r
\r
-static const size_t MAX_BUFFER_COUNT = 100;\r
-static const size_t MIN_BUFFER_COUNT = 4;\r
-static const size_t MAX_BUFFER_SIZE = 16 * 1000000;\r
- \r
+namespace caspar { namespace ffmpeg {\r
+ \r
struct input::implementation : boost::noncopyable\r
{ \r
- safe_ptr<diagnostics::graph> graph_;\r
+ const safe_ptr<diagnostics::graph> graph_;\r
\r
const safe_ptr<AVFormatContext> format_context_; // Destroy this last\r
const int default_stream_index_;\r
\r
const std::wstring filename_;\r
+ const uint32_t start_; \r
+ const uint32_t length_;\r
+ const bool thumbnail_mode_;\r
tbb::atomic<bool> loop_;\r
- const size_t start_; \r
- const size_t length_;\r
- size_t frame_number_;\r
+ uint32_t frame_number_;\r
\r
tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> buffer_;\r
tbb::atomic<size_t> buffer_size_;\r
- boost::condition_variable buffer_cond_;\r
- boost::mutex buffer_mutex_;\r
\r
- tbb::atomic<size_t> nb_frames_;\r
- tbb::atomic<size_t> nb_loops_;\r
-\r
- boost::thread thread_;\r
- tbb::atomic<bool> is_running_;\r
-\r
- tbb::recursive_mutex mutex_;\r
-\r
- explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, size_t start, size_t length) \r
+ executor executor_;\r
+ \r
+ explicit implementation(const safe_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_producer_params& vid_params) \r
: graph_(graph)\r
- , format_context_(open_input(filename)) \r
+ , format_context_(open_input(filename, resource_type, vid_params)) \r
, default_stream_index_(av_find_default_stream_index(format_context_.get()))\r
, filename_(filename)\r
, start_(start)\r
, length_(length)\r
+ , thumbnail_mode_(thumbnail_mode)\r
, frame_number_(0)\r
- { \r
- loop_ = loop;\r
- buffer_size_ = 0;\r
- nb_frames_ = 0;\r
- nb_loops_ = 0;\r
+ , executor_(print())\r
+ {\r
+ if (thumbnail_mode_)\r
+ executor_.invoke([]\r
+ {\r
+ disable_logging_for_thread();\r
+ });\r
\r
+ loop_ = loop;\r
buffer_size_ = 0;\r
- nb_frames_ = 0;\r
- nb_loops_ = 0;\r
\r
if(start_ > 0) \r
- do_seek(start_);\r
+ queued_seek(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
- thread_ = boost::thread([this]{run();});\r
\r
- CASPAR_LOG(info) << print() << L" Initialized.";\r
+ tick();\r
}\r
-\r
- ~implementation()\r
- {\r
- is_running_ = false;\r
- buffer_cond_.notify_all();\r
- thread_.join();\r
- }\r
- \r
+ \r
bool try_pop(std::shared_ptr<AVPacket>& packet)\r
{\r
- const bool result = buffer_.try_pop(packet);\r
-\r
+ auto result = buffer_.try_pop(packet);\r
+ \r
if(result)\r
{\r
if(packet)\r
buffer_size_ -= packet->size;\r
- buffer_cond_.notify_all();\r
+ tick();\r
}\r
\r
- graph_->update_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);\r
- graph_->update_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));\r
-\r
+ graph_->set_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);\r
+ graph_->set_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));\r
+ \r
return result;\r
}\r
- \r
- void run()\r
- { \r
- caspar::win32_exception::install_handler();\r
\r
- try\r
- {\r
- CASPAR_LOG(info) << print() << " Thread Started.";\r
- \r
- CASPAR_ASSERT(nb_frames_ < 1000);\r
+ std::ptrdiff_t get_max_buffer_count() const\r
+ {\r
+ return thumbnail_mode_ ? 1 : MAX_BUFFER_COUNT;\r
+ }\r
\r
- while(is_running_)\r
- {\r
- {\r
- boost::unique_lock<boost::mutex> lock(buffer_mutex_);\r
- while(full())\r
- buffer_cond_.timed_wait(lock, boost::posix_time::millisec(20));\r
- }\r
- read_next_packet(); \r
- }\r
+ std::ptrdiff_t get_min_buffer_count() const\r
+ {\r
+ return thumbnail_mode_ ? 0 : MIN_BUFFER_COUNT;\r
+ }\r
\r
- CASPAR_LOG(info) << print() << " Thread Stopped.";\r
- }\r
- catch(...)\r
+ boost::unique_future<bool> seek(uint32_t target)\r
+ {\r
+ if (!executor_.is_running())\r
+ return wrap_as_future(false);\r
+\r
+ return executor_.begin_invoke([=]() -> bool\r
{\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- is_running_ = false;\r
- }\r
+ std::shared_ptr<AVPacket> packet;\r
+ while(buffer_.try_pop(packet) && packet)\r
+ buffer_size_ -= packet->size;\r
+\r
+ queued_seek(target);\r
+\r
+ tick();\r
+\r
+ return true;\r
+ }, high_priority);\r
+ }\r
+ \r
+ std::wstring print() const\r
+ {\r
+ return L"ffmpeg_input[" + filename_ + L")]";\r
+ }\r
+ \r
+ bool full() const\r
+ {\r
+ return (buffer_size_ > MAX_BUFFER_SIZE || buffer_.size() > get_max_buffer_count()) && buffer_.size() > get_min_buffer_count();\r
}\r
- \r
- void read_next_packet()\r
- { \r
- tbb::recursive_mutex::scoped_lock lock(mutex_);\r
\r
- auto packet = create_packet();\r
- 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. \r
+ void tick()\r
+ { \r
+ if(!executor_.is_running())\r
+ return;\r
\r
- if(is_eof(ret)) \r
- {\r
- ++nb_loops_;\r
- frame_number_ = 0;\r
+ executor_.begin_invoke([this]\r
+ { \r
+ if(full())\r
+ return;\r
\r
- if(loop_)\r
- {\r
- do_seek(start_);\r
- graph_->add_tag("seek"); \r
- CASPAR_LOG(debug) << print() << " Looping."; \r
- } \r
- else\r
+ try\r
{\r
- is_running_ = false;\r
- CASPAR_LOG(debug) << print() << " Stopping.";\r
- }\r
- }\r
- else\r
- { \r
- THROW_ON_ERROR(ret, "av_read_frame", print());\r
+ auto packet = create_packet();\r
+ \r
+ 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. \r
+ \r
+ if(is_eof(ret)) \r
+ {\r
+ frame_number_ = 0;\r
+\r
+ if(loop_)\r
+ {\r
+ queued_seek(start_);\r
+ graph_->set_tag("seek"); \r
+ CASPAR_LOG(trace) << print() << " Looping."; \r
+ } \r
+ else\r
+ executor_.stop();\r
+ }\r
+ else\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
+ if(packet->stream_index == default_stream_index_)\r
+ ++frame_number_;\r
\r
- THROW_ON_ERROR2(av_dup_packet(packet.get()), print());\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
- packet = safe_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)\r
- {\r
- packet->size = size;\r
- packet->data = data;\r
- });\r
-\r
- buffer_.try_push(packet);\r
- buffer_size_ += packet->size;\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
+ packet = safe_ptr<AVPacket>(packet.get(), [packet, size, data](AVPacket*)\r
+ {\r
+ packet->size = size;\r
+ packet->data = data; \r
+ });\r
+\r
+ buffer_.try_push(packet);\r
+ buffer_size_ += packet->size;\r
\r
- graph_->update_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);\r
- graph_->update_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));\r
- } \r
- }\r
+ graph_->set_value("buffer-size", (static_cast<double>(buffer_size_)+0.001)/MAX_BUFFER_SIZE);\r
+ graph_->set_value("buffer-count", (static_cast<double>(buffer_.size()+0.001)/MAX_BUFFER_COUNT));\r
+ } \r
+ \r
+ tick(); \r
+ }\r
+ catch(...)\r
+ {\r
+ if (!thumbnail_mode_)\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ executor_.stop();\r
+ }\r
+ });\r
+ } \r
\r
- bool full() const\r
+ safe_ptr<AVFormatContext> open_input(const std::wstring resource_name, FFMPEG_Resource resource_type, const ffmpeg_producer_params& vid_params)\r
{\r
- return is_running_ && (buffer_size_ > MAX_BUFFER_SIZE || buffer_.size() > MAX_BUFFER_COUNT) && buffer_.size() > MIN_BUFFER_COUNT;\r
+ AVFormatContext* weak_context = nullptr;\r
+\r
+ switch (resource_type) {\r
+ case FFMPEG_FILE:\r
+ THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), nullptr, nullptr), resource_name);\r
+ break;\r
+ case FFMPEG_DEVICE: {\r
+ AVDictionary* format_options = NULL;\r
+ for (auto it = vid_params.options.begin(); it != vid_params.options.end(); ++it)\r
+ {\r
+ av_dict_set(&format_options, (*it).name.c_str(), (*it).value.c_str(), 0);\r
+ }\r
+ AVInputFormat* input_format = av_find_input_format("dshow");\r
+ THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), input_format, &format_options), resource_name);\r
+ if (format_options != nullptr)\r
+ {\r
+ std::string unsupported_tokens = "";\r
+ AVDictionaryEntry *t = NULL;\r
+ while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)\r
+ {\r
+ if (!unsupported_tokens.empty())\r
+ unsupported_tokens += ", ";\r
+ unsupported_tokens += t->key;\r
+ }\r
+ av_close_input_file(weak_context);\r
+ BOOST_THROW_EXCEPTION(ffmpeg_error() << msg_info(unsupported_tokens));\r
+ }\r
+ av_dict_free(&format_options);\r
+ } break;\r
+ case FFMPEG_STREAM: {\r
+ AVDictionary* format_options = NULL;\r
+ for (auto it = vid_params.options.begin(); it != vid_params.options.end(); ++it)\r
+ {\r
+ av_dict_set(&format_options, (*it).name.c_str(), (*it).value.c_str(), 0);\r
+ }\r
+ THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), nullptr, &format_options), resource_name);\r
+ if (format_options != nullptr)\r
+ {\r
+ std::string unsupported_tokens = "";\r
+ AVDictionaryEntry *t = NULL;\r
+ while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)\r
+ {\r
+ if (!unsupported_tokens.empty())\r
+ unsupported_tokens += ", ";\r
+ unsupported_tokens += t->key;\r
+ }\r
+ av_close_input_file(weak_context);\r
+ BOOST_THROW_EXCEPTION(ffmpeg_error() << msg_info(unsupported_tokens));\r
+ }\r
+ av_dict_free(&format_options);\r
+ } break;\r
+ };\r
+ safe_ptr<AVFormatContext> context(weak_context, av_close_input_file); \r
+ THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), resource_name);\r
+ fix_meta_data(*context);\r
+ return context;\r
}\r
- \r
- void do_seek(int64_t target)\r
+\r
+ void fix_meta_data(AVFormatContext& context)\r
+ {\r
+ auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
+\r
+ if(video_index > -1)\r
+ {\r
+ auto video_stream = context.streams[video_index];\r
+ auto video_context = context.streams[video_index]->codec;\r
+ \r
+ if(boost::filesystem2::path(context.filename).extension() == ".flv")\r
+ {\r
+ try\r
+ {\r
+ auto meta = read_flv_meta_info(context.filename);\r
+ double fps = boost::lexical_cast<double>(meta["framerate"]);\r
+ video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);\r
+ }\r
+ catch(...){}\r
+ }\r
+ else\r
+ {\r
+ auto stream_time = video_stream->time_base;\r
+ auto duration = video_stream->duration;\r
+ auto codec_time = video_context->time_base;\r
+ auto ticks = video_context->ticks_per_frame;\r
+\r
+ if(video_stream->nb_frames == 0)\r
+ video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks); \r
+ }\r
+ }\r
+ }\r
+ \r
+ void queued_seek(const uint32_t target)\r
{ \r
- CASPAR_LOG(debug) << print() << " Seeking: " << target;\r
+ if (!thumbnail_mode_)\r
+ CASPAR_LOG(debug) << print() << " Seeking: " << target;\r
\r
int flags = AVSEEK_FLAG_FRAME;\r
if(target == 0)\r
}\r
}\r
\r
- auto time_base = format_context_->streams[default_stream_index_]->time_base;\r
- target = (target*time_base.den)/time_base.num;\r
- auto fixed_time_base = fix_time_base(time_base);\r
- target = (target * fixed_time_base.num) / fixed_time_base.den;\r
-\r
- THROW_ON_ERROR2(avformat_seek_file(format_context_.get(), default_stream_index_, std::numeric_limits<int64_t>::min(), target, std::numeric_limits<int64_t>::max(), 0), print()); \r
-\r
- buffer_.push(flush_packet());\r
+ auto stream = format_context_->streams[default_stream_index_];\r
+ \r
+ \r
+ auto fps = read_fps(*format_context_, 0.0);\r
+ \r
+ THROW_ON_ERROR2(avformat_seek_file(\r
+ format_context_.get(), \r
+ default_stream_index_, \r
+ std::numeric_limits<int64_t>::min(),\r
+ static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num),\r
+ std::numeric_limits<int64_t>::max(), \r
+ 0), print());\r
+\r
+ auto flush_packet = create_packet();\r
+ flush_packet->data = nullptr;\r
+ flush_packet->size = 0;\r
+ flush_packet->pos = target;\r
+\r
+ buffer_.push(flush_packet);\r
} \r
\r
- void seek(int64_t target)\r
- {\r
- tbb::recursive_mutex::scoped_lock lock(mutex_);\r
-\r
- std::shared_ptr<AVPacket> packet;\r
- while(try_pop(packet))\r
- {\r
- }\r
-\r
- do_seek(target);\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
+ CASPAR_LOG(trace) << print() << " Received EIO, assuming EOF. ";\r
if(ret == AVERROR_EOF)\r
- CASPAR_LOG(debug) << print() << " Received EOF. " << nb_frames_;\r
+ CASPAR_LOG(trace) << print() << " Received EOF. ";\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(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, size_t start, size_t length) \r
- : impl_(new implementation(graph, filename, loop, start, length)){}\r
-bool input::eof() const {return !impl_->is_running_;}\r
+input::input(const safe_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_producer_params& vid_params) \r
+ : impl_(new implementation(graph, filename, resource_type, loop, start, length, thumbnail_mode, vid_params)){}\r
+bool input::eof() const {return !impl_->executor_.is_running();}\r
bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}\r
safe_ptr<AVFormatContext> input::context(){return impl_->format_context_;}\r
-size_t input::nb_frames() const {return impl_->nb_frames_;}\r
-size_t input::nb_loops() const {return impl_->nb_loops_;}\r
void input::loop(bool value){impl_->loop_ = value;}\r
bool input::loop() const{return impl_->loop_;}\r
-void input::seek(int64_t target){impl_->seek(target);}\r
+boost::unique_future<bool> input::seek(uint32_t target){return impl_->seek(target);}\r
}}\r