+/*\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
+#include "../ffmpeg_error.h"\r
+#include "../tbb_avcodec.h"\r
\r
#include <core/video_format.h>\r
\r
#include <common/diagnostics/graph.h>\r
\r
#include <tbb/concurrent_queue.h>\r
-#include <tbb/queuing_mutex.h>\r
+#include <tbb/mutex.h>\r
\r
-#include <boost/exception/error_info.hpp>\r
-#include <boost/thread/once.hpp>\r
-#include <boost/thread/thread.hpp>\r
-\r
-#include <errno.h>\r
-#include <system_error>\r
- \r
-#if defined(_MSC_VER)\r
-#pragma warning (disable : 4244)\r
-#endif\r
+#include <boost/range/iterator_range.hpp>\r
+#include <boost/range/algorithm.hpp>\r
\r
extern "C" \r
{\r
}\r
\r
namespace caspar {\r
- \r
-struct input::implementation : boost::noncopyable\r
-{ \r
- static const size_t PACKET_BUFFER_COUNT = 50;\r
+ \r
+static const size_t PACKET_BUFFER_COUNT = 100; // Assume that av_read_frame distance between audio and video packets is less than PACKET_BUFFER_COUNT.\r
\r
- safe_ptr<diagnostics::graph> graph_;\r
+class stream\r
+{\r
+ std::shared_ptr<AVCodecContext> ctx_;\r
+ int index_;\r
+ tbb::concurrent_bounded_queue<std::shared_ptr<AVPacket>> buffer_;\r
\r
- std::shared_ptr<AVFormatContext> format_context_; // Destroy this last\r
+public:\r
\r
- std::shared_ptr<AVCodecContext> video_codec_context_;\r
- std::shared_ptr<AVCodecContext> audio_codex_context_;\r
+ stream() : index_(-1)\r
+ {\r
+ buffer_.set_capacity(PACKET_BUFFER_COUNT);\r
+ }\r
\r
- const std::wstring filename_;\r
+ int open(std::shared_ptr<AVFormatContext>& fctx, AVMediaType media_type)\r
+ { \r
+ const auto streams = boost::iterator_range<AVStream**>(fctx->streams, fctx->streams+fctx->nb_streams);\r
+ const auto it = boost::find_if(streams, [&](AVStream* stream) \r
+ {\r
+ return stream && stream->codec->codec_type == media_type;\r
+ });\r
+ \r
+ if(it == streams.end()) \r
+ return AVERROR_STREAM_NOT_FOUND;\r
+ \r
+ auto codec = avcodec_find_decoder((*it)->codec->codec_id); \r
+ if(!codec)\r
+ return AVERROR_DECODER_NOT_FOUND;\r
+ \r
+ index_ = (*it)->index;\r
+\r
+ int errn = tbb_avcodec_open((*it)->codec, codec);\r
+ if(errn < 0)\r
+ return errn;\r
+ \r
+ ctx_.reset((*it)->codec, tbb_avcodec_close);\r
\r
- bool loop_;\r
- int video_s_index_;\r
- int audio_s_index_;\r
+ // Some files give an invalid time_base numerator, try to fix it.\r
+ if(ctx_ && ctx_->time_base.num == 1)\r
+ ctx_->time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(ctx_->time_base.den)))-1));\r
\r
- tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>> video_packet_buffer_;\r
- tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>> audio_packet_buffer_;\r
+ return errn; \r
+ }\r
\r
- boost::condition_variable cond_;\r
- boost::mutex mutex_;\r
+ bool try_pop(std::shared_ptr<AVPacket>& pkt)\r
+ {\r
+ return buffer_.try_pop(pkt);\r
+ }\r
+\r
+ void push(const std::shared_ptr<AVPacket>& pkt)\r
+ {\r
+ if(pkt && pkt->stream_index != index_)\r
+ return;\r
+\r
+ if(!ctx_)\r
+ return;\r
+\r
+ if(pkt)\r
+ av_dup_packet(pkt.get());\r
+\r
+ buffer_.push(pkt); \r
+ }\r
+\r
+ int index() const {return index_;}\r
\r
+ const std::shared_ptr<AVCodecContext>& ctx() const { return ctx_; }\r
+\r
+ operator bool(){return ctx_ != nullptr;}\r
+\r
+ double fps() const { return !ctx_ ? -1.0 : static_cast<double>(ctx_->time_base.den) / static_cast<double>(ctx_->time_base.num); }\r
+\r
+ bool empty() const { return buffer_.empty();}\r
+ int size() const { return buffer_.size();}\r
+};\r
+ \r
+struct input::implementation : boost::noncopyable\r
+{ \r
+ safe_ptr<diagnostics::graph> graph_;\r
+\r
+ std::shared_ptr<AVFormatContext> format_context_; // Destroy this last\r
+ \r
+ const std::wstring filename_;\r
+ const bool loop_;\r
+ const int start_; \r
+ double fps_;\r
+\r
+ stream video_stream_;\r
+ stream audio_stream_;\r
+ \r
+ std::exception_ptr exception_;\r
executor executor_;\r
public:\r
- explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop) \r
+ explicit implementation(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start) \r
: graph_(graph)\r
, loop_(loop)\r
- , video_s_index_(-1)\r
- , audio_s_index_(-1)\r
, filename_(filename)\r
, executor_(print())\r
- { \r
- graph_->set_color("input-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));\r
- graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f)); \r
-\r
+ , start_(std::max(start, 0))\r
+ { \r
int errn;\r
- AVFormatContext* weak_format_context_;\r
- if((errn = -av_open_input_file(&weak_format_context_, narrow(filename).c_str(), nullptr, 0, nullptr)) > 0)\r
+\r
+ AVFormatContext* weak_format_context_ = nullptr;\r
+ errn = av_open_input_file(&weak_format_context_, narrow(filename).c_str(), nullptr, 0, nullptr);\r
+ if(errn < 0 || weak_format_context_ == nullptr)\r
+ { \r
BOOST_THROW_EXCEPTION(\r
file_read_error() << \r
source_info(narrow(print())) << \r
- msg_info("No format context found.") << \r
+ msg_info(av_error_str(errn)) <<\r
boost::errinfo_api_function("av_open_input_file") <<\r
- boost::errinfo_errno(errn) <<\r
+ boost::errinfo_errno(AVUNERROR(errn)) <<\r
boost::errinfo_file_name(narrow(filename)));\r
+ }\r
\r
format_context_.reset(weak_format_context_, av_close_input_file);\r
\r
- if((errn = -av_find_stream_info(format_context_.get())) > 0)\r
+ errn = av_find_stream_info(format_context_.get());\r
+ if(errn < 0)\r
+ { \r
BOOST_THROW_EXCEPTION(\r
file_read_error() << \r
source_info(narrow(print())) << \r
+ msg_info(av_error_str(errn)) <<\r
boost::errinfo_api_function("av_find_stream_info") <<\r
- msg_info("No stream found.") << \r
- boost::errinfo_errno(errn));\r
-\r
- video_codec_context_ = open_stream(CODEC_TYPE_VIDEO, video_s_index_);\r
- if(!video_codec_context_)\r
- CASPAR_LOG(warning) << print() << " Could not open any video stream.";\r
- else\r
- fix_time_base(video_codec_context_.get());\r
+ boost::errinfo_errno(AVUNERROR(errn)));\r
+ }\r
\r
- audio_codex_context_ = open_stream(CODEC_TYPE_AUDIO, audio_s_index_);\r
- if(!audio_codex_context_)\r
- CASPAR_LOG(warning) << print() << " Could not open any audio stream.";\r
- else\r
- fix_time_base(video_codec_context_.get());\r
-\r
- if(!video_codec_context_ && !audio_codex_context_)\r
+ errn = video_stream_.open(format_context_, AVMEDIA_TYPE_VIDEO);\r
+ if(errn < 0)\r
+ CASPAR_LOG(warning) << print() << L" Could not open video stream: " << widen(av_error_str(errn));\r
+ \r
+ errn = audio_stream_.open(format_context_, AVMEDIA_TYPE_AUDIO);\r
+ if(errn < 0)\r
+ CASPAR_LOG(warning) << print() << L" Could not open audio stream: " << widen(av_error_str(errn));\r
+ \r
+ if(!video_stream_ && !audio_stream_)\r
+ { \r
BOOST_THROW_EXCEPTION(\r
file_read_error() << \r
source_info(narrow(print())) << \r
- msg_info("No video or audio codec context found.")); \r
- \r
- executor_.start();\r
+ msg_info("No video or audio codec context found.")); \r
+ }\r
+\r
+ fps_ = video_stream_ ? video_stream_.fps() : audio_stream_.fps();\r
+\r
+ if(start_ != 0) \r
+ seek_frame(start_);\r
+\r
+ for(size_t n = 0; n < 32; ++n) // Read some packets for pre-rolling.\r
+ read_next_packet();\r
+ \r
+ if(audio_stream_)\r
+ graph_->set_color("audio-input-buffer", diagnostics::color(0.5f, 1.0f, 0.2f));\r
+ \r
+ if(video_stream_)\r
+ graph_->set_color("video-input-buffer", diagnostics::color(0.2f, 0.5f, 1.0f));\r
+ \r
+ graph_->set_color("seek", diagnostics::color(0.5f, 1.0f, 0.5f)); \r
+\r
executor_.begin_invoke([this]{read_file();});\r
CASPAR_LOG(info) << print() << " Started.";\r
}\r
\r
~implementation()\r
{\r
- executor_.clear();\r
- executor_.stop();\r
- cond_.notify_all();\r
- CASPAR_LOG(info) << print() << " Stopped.";\r
+ stop();\r
+ // Unblock thread.\r
+ std::shared_ptr<AVPacket> packet;\r
+ try_pop_video_packet(packet);\r
+ try_pop_audio_packet(packet);\r
}\r
- \r
- void fix_time_base(AVCodecContext* context) // Some files give an invalid numerator, try to fix it.\r
+ \r
+ bool try_pop_video_packet(std::shared_ptr<AVPacket>& packet)\r
{\r
- if(context && context->time_base.num == 1)\r
- context->time_base.num = static_cast<int>(std::pow(10.0, static_cast<int>(std::log10(static_cast<float>(context->time_base.den)))-1));\r
+ bool result = video_stream_.try_pop(packet);\r
+ if(result && !packet)\r
+ graph_->add_tag("video-input-buffer");\r
+ return result;\r
}\r
\r
- std::shared_ptr<AVCodecContext> open_stream(int codec_type, int& s_index)\r
- { \r
- AVStream** streams_end = format_context_->streams+format_context_->nb_streams;\r
- AVStream** stream = std::find_if(format_context_->streams, streams_end, \r
- [&](AVStream* stream) { return stream != nullptr && stream->codec->codec_type == codec_type ;});\r
- \r
- if(stream == streams_end) \r
- return nullptr;\r
-\r
- s_index = (*stream)->index;\r
- \r
- auto codec = avcodec_find_decoder((*stream)->codec->codec_id); \r
- if(codec == nullptr)\r
- return nullptr;\r
- \r
- if((-avcodec_open((*stream)->codec, codec)) > 0) \r
- return nullptr;\r
-\r
- return std::shared_ptr<AVCodecContext>((*stream)->codec, avcodec_close);\r
+ bool try_pop_audio_packet(std::shared_ptr<AVPacket>& packet)\r
+ { \r
+ bool result = audio_stream_.try_pop(packet);\r
+ if(result && !packet)\r
+ graph_->add_tag("audio-input-buffer");\r
+ return result;\r
}\r
- \r
- void read_file()\r
- { \r
- AVPacket tmp_packet;\r
- safe_ptr<AVPacket> read_packet(&tmp_packet, av_free_packet); \r
\r
- if (av_read_frame(format_context_.get(), read_packet.get()) >= 0) // NOTE: read_packet is only valid until next call of av_read_frame or av_close_input_file\r
- {\r
- auto packet = std::make_shared<aligned_buffer>(read_packet->data, read_packet->data + read_packet->size);\r
- if(read_packet->stream_index == video_s_index_) \r
- video_packet_buffer_.try_push(std::move(packet)); \r
- else if(read_packet->stream_index == audio_s_index_) \r
- audio_packet_buffer_.try_push(std::move(packet)); \r
- }\r
- else if(!loop_ || av_seek_frame(format_context_.get(), -1, 0, AVSEEK_FLAG_BACKWARD) < 0) // TODO: av_seek_frame does not work for all formats\r
- executor_.stop();\r
- else\r
- graph_->add_tag("seek"); \r
- \r
- graph_->update_value("input-buffer", static_cast<float>(video_packet_buffer_.size())/static_cast<float>(PACKET_BUFFER_COUNT)); \r
- \r
- executor_.begin_invoke([this]{read_file();}); \r
- boost::unique_lock<boost::mutex> lock(mutex_);\r
- while(executor_.is_running() && audio_packet_buffer_.size() > PACKET_BUFFER_COUNT && video_packet_buffer_.size() > PACKET_BUFFER_COUNT)\r
- cond_.wait(lock); \r
- }\r
- \r
- aligned_buffer get_video_packet()\r
+ double fps()\r
{\r
- return get_packet(video_packet_buffer_);\r
+ return fps_;\r
}\r
\r
- aligned_buffer get_audio_packet()\r
+private:\r
+\r
+ void stop()\r
{\r
- return get_packet(audio_packet_buffer_);\r
+ executor_.stop();\r
+ \r
+ CASPAR_LOG(info) << print() << " Stopping.";\r
}\r
- \r
- aligned_buffer get_packet(tbb::concurrent_bounded_queue<std::shared_ptr<aligned_buffer>>& buffer)\r
- {\r
- cond_.notify_all();\r
- std::shared_ptr<aligned_buffer> packet;\r
- return buffer.try_pop(packet) ? std::move(*packet) : aligned_buffer();\r
+\r
+ void read_file()\r
+ { \r
+ if(video_stream_.size() > 4 || audio_stream_.size() > 4) // audio is always before video.\r
+ Sleep(5); // There are enough packets, no hurry.\r
+\r
+ read_next_packet();\r
+\r
+ executor_.begin_invoke([this]{read_file();});\r
}\r
+ \r
+ void read_next_packet()\r
+ { \r
+ try\r
+ {\r
+ std::shared_ptr<AVPacket> read_packet(new AVPacket, [](AVPacket* p)\r
+ {\r
+ av_free_packet(p);\r
+ delete p;\r
+ });\r
+ av_init_packet(read_packet.get());\r
\r
- bool is_eof() const\r
- {\r
- return !executor_.is_running() && video_packet_buffer_.empty() && audio_packet_buffer_.empty();\r
+ const int errn = av_read_frame(format_context_.get(), read_packet.get()); // read_packet is only valid until next call of av_read_frame.\r
+ if(is_eof(errn)) // Use av_dup_packet to extend its life.\r
+ {\r
+ if(loop_)\r
+ {\r
+ seek_frame(start_, AVSEEK_FLAG_BACKWARD);\r
+ graph_->add_tag("seek"); \r
+ //CASPAR_LOG(info) << print() << " Received EOF. Looping."; \r
+ } \r
+ else\r
+ {\r
+ stop();\r
+ //CASPAR_LOG(info) << print() << " Received EOF. Stopping.";\r
+ }\r
+ }\r
+ else if(errn < 0)\r
+ {\r
+ BOOST_THROW_EXCEPTION(\r
+ file_read_error() <<\r
+ msg_info(av_error_str(errn)) <<\r
+ source_info(narrow(print())) << \r
+ boost::errinfo_api_function("av_read_frame") <<\r
+ boost::errinfo_errno(AVUNERROR(errn)));\r
+ }\r
+ else\r
+ {\r
+ if(video_stream_)\r
+ { \r
+ video_stream_.push(read_packet);\r
+ graph_->update_value("video-input-buffer", static_cast<float>(video_stream_.size())/static_cast<float>(PACKET_BUFFER_COUNT)); \r
+ }\r
+ if(audio_stream_)\r
+ { \r
+ audio_stream_.push(read_packet);\r
+ graph_->update_value("audio-input-buffer", static_cast<float>(audio_stream_.size())/static_cast<float>(PACKET_BUFFER_COUNT)); \r
+ }\r
+ } \r
+ }\r
+ catch(...)\r
+ {\r
+ stop();\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ return;\r
+ } \r
}\r
- \r
- double fps() const\r
+\r
+ void seek_frame(int64_t frame, int flags = 0)\r
+ { \r
+ static const AVRational base_q = {1, AV_TIME_BASE};\r
+\r
+ // Convert from frames into seconds.\r
+ auto seek_target = frame*static_cast<int64_t>(AV_TIME_BASE/fps_);\r
+\r
+ int stream_index = video_stream_.index() >= 0 ? video_stream_.index() : audio_stream_.index();\r
+\r
+ if(stream_index >= 0) \r
+ seek_target = av_rescale_q(seek_target, base_q, format_context_->streams[stream_index]->time_base);\r
+\r
+ const int errn = av_seek_frame(format_context_.get(), stream_index, seek_target, flags);\r
+ if(errn < 0)\r
+ { \r
+ BOOST_THROW_EXCEPTION(\r
+ invalid_operation() << \r
+ source_info(narrow(print())) << \r
+ msg_info(av_error_str(errn)) <<\r
+ boost::errinfo_api_function("av_seek_frame") <<\r
+ boost::errinfo_errno(AVUNERROR(errn)));\r
+ }\r
+\r
+ video_stream_.push(nullptr);\r
+ audio_stream_.push(nullptr);\r
+ } \r
+\r
+ bool is_eof(int errn)\r
{\r
- return static_cast<double>(video_codec_context_->time_base.den) / static_cast<double>(video_codec_context_->time_base.num);\r
- }\r
+ //if(errn == AVERROR(EIO))\r
+ // CASPAR_LOG(warning) << print() << " Received EIO, assuming EOF";\r
\r
+ return errn == AVERROR_EOF || errn == AVERROR(EIO); // av_read_frame doesn't always correctly return AVERROR_EOF;\r
+ }\r
+ \r
std::wstring print() const\r
{\r
- return L"ffmpeg_input";\r
+ const auto video = widen(video_stream_.ctx() ? video_stream_.ctx()->codec->name : "no-video");\r
+ const auto audio = widen(audio_stream_.ctx() ? audio_stream_.ctx()->codec->name : "no-audio");\r
+\r
+ return L"ffmpeg_input[" + filename_ + L"(" + boost::lexical_cast<std::wstring>(static_cast<int>(100*fps_)) + L"|" + video + L"|" + audio + L")]";\r
}\r
};\r
\r
-input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop) : impl_(new implementation(graph, filename, loop)){}\r
-const std::shared_ptr<AVCodecContext>& input::get_video_codec_context() const{return impl_->video_codec_context_;}\r
-const std::shared_ptr<AVCodecContext>& input::get_audio_codec_context() const{return impl_->audio_codex_context_;}\r
-bool input::is_eof() const{return impl_->is_eof();}\r
-aligned_buffer input::get_video_packet(){return impl_->get_video_packet();}\r
-aligned_buffer input::get_audio_packet(){return impl_->get_audio_packet();}\r
+input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, int start, int length) \r
+ : impl_(new implementation(graph, filename, loop, start)){}\r
+const std::shared_ptr<AVCodecContext>& input::get_video_codec_context() const{return impl_->video_stream_.ctx();}\r
+const std::shared_ptr<AVCodecContext>& input::get_audio_codec_context() const{return impl_->audio_stream_.ctx();}\r
+bool input::is_running() const {return impl_->executor_.is_running();}\r
+bool input::try_pop_video_packet(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_video_packet(packet);}\r
+bool input::try_pop_audio_packet(std::shared_ptr<AVPacket>& packet){return impl_->try_pop_audio_packet(packet);}\r
double input::fps() const { return impl_->fps(); }\r
}
\ No newline at end of file