]> git.sesse.net Git - casparcg/blobdiff - core/producer/ffmpeg/ffmpeg_producer.cpp
2.0.0.2: Per sample audio transitions.
[casparcg] / core / producer / ffmpeg / ffmpeg_producer.cpp
index ac38941f2c80b21479ee0141f34e946d0222b0d4..65f68c479b14919f1dbfeada75b5bdfcd3a66189 100644 (file)
 \r
 #include "ffmpeg_producer.h"\r
 \r
-#if defined(_MSC_VER)\r
-#pragma warning (push)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-\r
-extern "C" \r
-{\r
-       #define __STDC_CONSTANT_MACROS\r
-       #define __STDC_LIMIT_MACROS\r
-       #include <libavcodec/avcodec.h>\r
-       #include <libavformat/avformat.h>\r
-       #include <libavutil/avutil.h>\r
-       #include <libswscale/swscale.h>\r
-}\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (pop)\r
-#endif\r
-\r
 #include "input.h"\r
-\r
 #include "audio/audio_decoder.h"\r
 #include "video/video_decoder.h"\r
-#include "video/video_transformer.h"\r
 \r
-#include "../../format/video_format.h"\r
-#include "../../../common/utility/scope_exit.h"\r
-#include "../../server.h"\r
+#include <core/video_format.h>\r
+#include <common/utility/timer.h>\r
+#include <common/diagnostics/graph.h>\r
+#include <mixer/frame/draw_frame.h>\r
+#include <mixer/audio/audio_transform.h>\r
 \r
-#include <tbb/mutex.h>\r
-#include <tbb/parallel_invoke.h>\r
-#include <tbb/task_group.h>\r
+#include <common/env.h>\r
 \r
-#include <boost/algorithm/string/case_conv.hpp>\r
-#include <boost/lexical_cast.hpp>\r
-#include <boost/thread.hpp>\r
-#include <boost/thread/once.hpp>\r
+#include <tbb/parallel_invoke.h>\r
 \r
-using namespace boost::assign;\r
+#include <deque>\r
+#include <functional>\r
 \r
 namespace caspar { namespace core { namespace ffmpeg{\r
        \r
 struct ffmpeg_producer : public frame_producer\r
 {\r
-public:\r
-       ffmpeg_producer(const std::wstring& filename, const  std::vector<std::wstring>& params) \r
-               : filename_(filename), underrun_count_(0)\r
-       {\r
-               if(!boost::filesystem::exists(filename))\r
-                       BOOST_THROW_EXCEPTION(file_not_found() <<  boost::errinfo_file_name(common::narrow(filename)));\r
+       const std::wstring                                      filename_;\r
+       const bool                                                      loop_;\r
+       printer                                                         parent_printer_;\r
+       \r
+       std::shared_ptr<diagnostics::graph>     graph_;\r
+       timer                                                           perf_timer_;\r
                \r
-               static boost::once_flag av_register_all_flag = BOOST_ONCE_INIT;\r
-               boost::call_once(av_register_all, av_register_all_flag);        \r
+       std::unique_ptr<audio_decoder>          audio_decoder_;\r
+       std::unique_ptr<video_decoder>          video_decoder_;\r
+\r
+       std::deque<safe_ptr<write_frame>>       video_frame_channel_;   \r
+       std::deque<std::vector<short>>          audio_chunk_channel_;\r
+\r
+       std::queue<safe_ptr<draw_frame>>        ouput_channel_;\r
                \r
-               static boost::once_flag avcodec_init_flag = BOOST_ONCE_INIT;\r
-               boost::call_once(avcodec_init, avcodec_init_flag);      \r
-                               \r
-               input_.reset(new input());\r
-               input_->set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());\r
-               input_->load(common::narrow(filename_));\r
-               video_decoder_.reset(new video_decoder(input_->get_video_codec_context().get()));\r
-               video_transformer_.reset(new video_transformer(input_->get_video_codec_context().get()));\r
-               audio_decoder_.reset(new audio_decoder(input_->get_audio_codec_context().get()));\r
-               has_audio_ = input_->get_audio_codec_context() != nullptr;\r
-\r
-               auto seek = std::find(params.begin(), params.end(), L"SEEK");\r
-               if(seek != params.end() && ++seek != params.end())\r
-               {\r
-                       if(!input_->seek(boost::lexical_cast<unsigned long long>(*seek)))\r
-                               CASPAR_LOG(warning) << "Failed to seek file: " << filename_  << "to frame" << *seek;\r
-               }\r
-       }\r
+       safe_ptr<draw_frame>                            last_frame_;\r
+       std::shared_ptr<frame_factory>          frame_factory_;\r
+\r
+       std::unique_ptr<input>                          input_; \r
+public:\r
+       explicit ffmpeg_producer(const std::wstring& filename, bool loop) \r
+               : filename_(filename)\r
+               , loop_(loop) \r
+               , last_frame_(draw_frame(draw_frame::empty()))\r
                \r
-       void initialize(const frame_processor_device_ptr& frame_processor)\r
        {\r
-               video_transformer_->initialize(frame_processor);\r
+               graph_ = diagnostics::create_graph(boost::bind(&ffmpeg_producer::print, this)); \r
+               graph_->guide("frame-time", 0.5);\r
+               graph_->set_color("frame-time",  diagnostics::color(1.0f, 0.0f, 0.0f));\r
+       }\r
+       \r
+       virtual void initialize(const safe_ptr<frame_factory>& frame_factory)\r
+       {\r
+               frame_factory_ = frame_factory;\r
+               input_.reset(new input(safe_ptr<diagnostics::graph>(graph_), filename_, loop_, std::bind(&ffmpeg_producer::print, this)));\r
+               video_decoder_.reset(input_->get_video_codec_context().get() ? new video_decoder(input_->get_video_codec_context().get(), frame_factory) : nullptr);\r
+               audio_decoder_.reset(input_->get_audio_codec_context().get() ? new audio_decoder(input_->get_audio_codec_context().get(), frame_factory->get_video_format_desc().fps) : nullptr);\r
        }\r
                \r
-       frame_ptr render_frame()\r
+       virtual void set_parent_printer(const printer& parent_printer) \r
        {\r
+               parent_printer_ = parent_printer;\r
+       }\r
+\r
+       virtual safe_ptr<draw_frame> receive()\r
+       {\r
+               perf_timer_.reset();\r
+\r
                while(ouput_channel_.empty() && !input_->is_eof())\r
                {       \r
-                       auto video_packet = input_->get_video_packet();         \r
-                       auto audio_packet = input_->get_audio_packet();         \r
+                       aligned_buffer video_packet;\r
+                       if(video_frame_channel_.size() < 3 && video_decoder_)   \r
+                               video_packet = input_->get_video_packet();              \r
+                       \r
+                       aligned_buffer audio_packet;\r
+                       if(audio_chunk_channel_.size() < 3 && audio_decoder_)   \r
+                               audio_packet = input_->get_audio_packet();              \r
+\r
                        tbb::parallel_invoke(\r
                        [&]\r
                        { // Video Decoding and Scaling\r
-                               if(!video_packet.empty())\r
+                               if(!video_packet.empty() && video_decoder_)\r
                                {\r
-                                       auto decoded_frame = video_decoder_->execute(video_packet);\r
-                                       auto transformed_frame = video_transformer_->execute(decoded_frame);\r
-                                       video_frame_channel_.push_back(transformed_frame);      \r
+                                       try\r
+                                       {\r
+                                               auto frame = video_decoder_->execute(video_packet);\r
+                                               frame->tag(reinterpret_cast<int>(this));\r
+                                               video_frame_channel_.push_back(std::move(frame));\r
+                                       }\r
+                                       catch(...)\r
+                                       {\r
+                                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                                               video_decoder_.reset();\r
+                                               CASPAR_LOG(warning) << print() << " removed video-stream.";\r
+                                       }\r
                                }\r
                        }, \r
                        [&] \r
                        { // Audio Decoding\r
-                               if(!audio_packet.empty())\r
+                               if(!audio_packet.empty() && audio_decoder_)\r
                                {\r
-                                       auto chunks = audio_decoder_->execute(audio_packet);\r
-                                       audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
+                                       try\r
+                                       {\r
+                                               auto chunks = audio_decoder_->execute(audio_packet);\r
+                                               audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
+                                       }\r
+                                       catch(...)\r
+                                       {\r
+                                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                                               audio_decoder_.reset();\r
+                                               CASPAR_LOG(warning) << print() << " removed audio-stream.";\r
+                                       }\r
                                }\r
                        });\r
 \r
-                       if(video_packet.empty() && audio_packet.empty())\r
-                       {\r
-                               if(underrun_count_++ == 0)\r
-                                       CASPAR_LOG(warning) << "### File read underflow has STARTED.";\r
-\r
-                               // Return last frame without audio.\r
-                               last_frame_->audio_data().clear();\r
-                               return last_frame_;\r
-                       }\r
-                       else if(underrun_count_ > 0)\r
+                       while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))\r
                        {\r
-                               CASPAR_LOG(trace) << "### File Read Underrun has ENDED with " << underrun_count_ << " ticks.";\r
-                               underrun_count_ = 0;\r
-                       }\r
+                               std::shared_ptr<write_frame> frame;\r
 \r
-                       while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || !has_audio_))\r
-                       {\r
-                               if(has_audio_ && video_frame_channel_.front() != nullptr)\r
+                               if(video_decoder_)\r
                                {\r
-                                       video_frame_channel_.front()->audio_data() = std::move(audio_chunk_channel_.front());\r
+                                       frame = video_frame_channel_.front();\r
+                                       video_frame_channel_.pop_front();\r
+                               }\r
+\r
+                               if(audio_decoder_) \r
+                               {\r
+                                       if(!frame)\r
+                                       {\r
+                                               frame = frame_factory_->create_frame(1, 1);\r
+                                               std::fill(frame->image_data().begin(), frame->image_data().end(), 0);\r
+                                       }\r
+                                       \r
+                                       frame->audio_data() = std::move(audio_chunk_channel_.front());\r
                                        audio_chunk_channel_.pop_front();\r
                                }\r
-                               \r
-                               frame_ptr frame = video_frame_channel_.front();\r
-                               video_frame_channel_.pop_front();\r
-                               ouput_channel_.push(std::move(frame));\r
+                                                       \r
+                               ouput_channel_.push(safe_ptr<write_frame>(frame));                              \r
                        }                               \r
+\r
+                       if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())                      \r
+                               return last_frame_;                     \r
                }\r
+               \r
+               graph_->update("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));\r
 \r
+               auto result = last_frame_;\r
                if(!ouput_channel_.empty())\r
                {\r
-                       last_frame_ = ouput_channel_.front();\r
+                       result = std::move(ouput_channel_.front());\r
+                       last_frame_ = draw_frame(result);\r
+                       last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio\r
                        ouput_channel_.pop();\r
                }\r
                else if(input_->is_eof())\r
-                       last_frame_ = nullptr;\r
+                       return draw_frame::eof();\r
 \r
-               return last_frame_;\r
+               return result;\r
        }\r
 \r
-       std::wstring print()\r
+       virtual std::wstring print() const\r
        {\r
-               std::wstringstream str;\r
-               str << L"ffmpeg_producer " << filename_ << L".";\r
-               return str.str();\r
+               return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
        }\r
-                       \r
-       bool has_audio_;\r
-\r
-       input_uptr                                                      input_;         \r
-\r
-       video_decoder_uptr                                      video_decoder_;\r
-       video_transformer_uptr                          video_transformer_;\r
-       std::deque<frame_ptr>                           video_frame_channel_;\r
-       \r
-       audio_decoder_ptr                                       audio_decoder_;\r
-       std::deque<std::vector<short>>          audio_chunk_channel_;\r
-\r
-       std::queue<frame_ptr>                           ouput_channel_;\r
-       \r
-       std::wstring                                            filename_;\r
-\r
-       long                                                            underrun_count_;\r
-\r
-       frame_ptr                                                       last_frame_;\r
 };\r
 \r
-frame_producer_ptr create_ffmpeg_producer(const  std::vector<std::wstring>& params)\r
-{      \r
-       static const std::vector<std::wstring> extensions = list_of(L"mpg")(L"avi")(L"mov")(L"dv")(L"wav")(L"mp3")(L"mp4")(L"f4v")(L"flv");\r
-       std::wstring filename = server::media_folder() + L"\\" + params[0];\r
+safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)\r
+{                      \r
+       static const std::vector<std::wstring> extensions = boost::assign::list_of\r
+               (L"mpg")(L"mpeg")(L"avi")(L"mov")(L"qt")(L"webm")(L"dv")(L"mp4")(L"f4v")(L"flv")(L"mkv")(L"mka")(L"wmw")(L"wma")(L"ogg")(L"divx")(L"wav")(L"mp3");\r
+       std::wstring filename = env::media_folder() + L"\\" + params[0];\r
        \r
        auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
                {                                       \r
@@ -183,9 +181,12 @@ frame_producer_ptr create_ffmpeg_producer(const  std::vector<std::wstring>& para
                });\r
 \r
        if(ext == extensions.end())\r
-               return nullptr;\r
+               return frame_producer::empty();\r
 \r
-       return std::make_shared<ffmpeg_producer>(filename + L"." + *ext, params);\r
+       std::wstring path = filename + L"." + *ext;\r
+       bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();\r
+       \r
+       return make_safe<ffmpeg_producer>(path, loop);\r
 }\r
 \r
 }}}
\ No newline at end of file