X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fffmpeg%2Fproducer%2Fffmpeg_producer.cpp;h=02083d5bafaebb484cd020c9690771357817cf8a;hb=65199f18ae5ec831d809ea73b6e9dd01f530b4c5;hp=a8988ad753f2f3a37690d6d186fe3daee755a997;hpb=af660bb0772edbc122286d1e1bc1ded9bc6b2794;p=casparcg diff --git a/modules/ffmpeg/producer/ffmpeg_producer.cpp b/modules/ffmpeg/producer/ffmpeg_producer.cpp index a8988ad75..02083d5ba 100644 --- a/modules/ffmpeg/producer/ffmpeg_producer.cpp +++ b/modules/ffmpeg/producer/ffmpeg_producer.cpp @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -45,20 +46,8 @@ #include -namespace caspar { - -double validate_fps(double fps, int64_t nb_frames, double duration_sec) -{ - if(fps > 20.0 && fps < 80.0) - return fps; - - auto est_fps = nb_frames/duration_sec; - - CASPAR_LOG(warning) << L"Invalid framerate detected, trying to estimate, fps: " << fps << L" nb_frames: " << nb_frames << L" duration_sec: " << duration_sec << L" => " << est_fps << L" fps."; - - return est_fps; -} - +namespace caspar { namespace ffmpeg { + struct ffmpeg_producer : public core::frame_producer { const std::wstring filename_; @@ -74,35 +63,44 @@ struct ffmpeg_producer : public core::frame_producer input input_; video_decoder video_decoder_; audio_decoder audio_decoder_; + double fps_; frame_muxer muxer_; - int late_frames_; const int start_; const bool loop_; + const size_t length_; safe_ptr last_frame_; + + const size_t width_; + const size_t height_; + bool is_progressive_; public: - explicit ffmpeg_producer(const safe_ptr& frame_factory, const std::wstring& filename, const std::wstring& filter, bool loop, int start, int length) + explicit ffmpeg_producer(const safe_ptr& frame_factory, const std::wstring& filename, const std::wstring& filter, bool loop, int start, size_t length) : filename_(filename) - , graph_(diagnostics::create_graph(narrow(print()))) , frame_factory_(frame_factory) , format_desc_(frame_factory->get_video_format_desc()) , input_(graph_, filename_, loop, start, length) , video_decoder_(input_.context(), frame_factory, filter) , audio_decoder_(input_.context(), frame_factory->get_video_format_desc()) - , muxer_(validate_fps(video_decoder_.fps(), video_decoder_.nb_frames(), audio_decoder_.duration()), frame_factory) - , late_frames_(0) + , fps_(video_decoder_.fps()) + , muxer_(fps_, frame_factory) , start_(start) , loop_(loop) + , length_(length) , last_frame_(core::basic_frame::empty()) + , width_(video_decoder_.width()) + , height_(video_decoder_.height()) + , is_progressive_(true) { graph_->add_guide("frame-time", 0.5); - graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f)); - graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f)); + graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f)); + graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f)); + diagnostics::register_graph(graph_); - for(int n = 0; n < 32 && muxer_.empty(); ++n) - decode_frame(0); + for(int n = 0; n < 3; ++n) + frame_factory->create_frame(this, std::max(2, video_decoder_.width()), std::max(2, video_decoder_.height())); } virtual safe_ptr receive(int hints) @@ -122,12 +120,11 @@ public: { if(input_.eof()) return core::basic_frame::eof(); - else - { + else graph_->add_tag("underflow"); - ++late_frames_; - } } + + graph_->set_text(print()); return frame; } @@ -137,7 +134,7 @@ public: return disable_audio(last_frame_); } - void decode_frame(int hints) + void push_packets() { for(int n = 0; n < 16 && ((!muxer_.video_ready() && !video_decoder_.ready()) || (!muxer_.audio_ready() && !audio_decoder_.ready())); ++n) { @@ -148,6 +145,11 @@ public: audio_decoder_.push(pkt); } } + } + + void decode_frame(int hints) + { + push_packets(); tbb::parallel_invoke( [&] @@ -156,8 +158,11 @@ public: return; auto video_frames = video_decoder_.poll(); - BOOST_FOREACH(auto& video, video_frames) + BOOST_FOREACH(auto& video, video_frames) + { + is_progressive_ = video ? video->interlaced_frame == 0 : is_progressive_; muxer_.push(video, hints); + } }, [&] { @@ -185,26 +190,41 @@ public: int64_t video_nb_frames = video_decoder_.nb_frames(); int64_t audio_nb_frames = audio_decoder_.nb_frames(); - nb_frames = std::max(nb_frames, std::max(video_nb_frames, audio_nb_frames)); + nb_frames = std::min(static_cast(length_), std::max(nb_frames, std::max(video_nb_frames, audio_nb_frames))); } nb_frames = muxer_.calc_nb_frames(nb_frames); // TODO: Might need to scale nb_frames av frame_muxer transformations. - return nb_frames + late_frames_ - start_; + return nb_frames - start_; } virtual std::wstring print() const { - return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]"; + return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"|" + + boost::lexical_cast(width_) + L"x" + boost::lexical_cast(height_) + + (is_progressive_ ? L"p" : L"i") + boost::lexical_cast(is_progressive_ ? fps_ : 2.0 * fps_) + + L"]"; } }; -safe_ptr create_ffmpeg_producer(const safe_ptr& frame_factory, const std::vector& params) +safe_ptr create_producer(const safe_ptr& frame_factory, const std::vector& params) { static const std::vector extensions = boost::assign::list_of - (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"wmv")(L"wma")(L"ogg")(L"divx")(L"xvid")(L"wav")(L"mp3")(L"m2v"); + (L"mpg")(L"mpeg")(L"m2v")(L"m4v")(L"mp3")(L"mp4")(L"mpga") + (L"avi") + (L"mov") + (L"qt") + (L"webm") + (L"dv") + (L"f4v")(L"flv") + (L"mkv")(L"mka") + (L"wmv")(L"wma")(L"wav") + (L"rm")(L"ram") + (L"ogg")(L"ogv")(L"oga")(L"ogx") + (L"divx")(L"xvid"); + std::wstring filename = env::media_folder() + L"\\" + params[0]; auto ext = boost::find_if(extensions, [&](const std::wstring& ex) @@ -215,35 +235,16 @@ safe_ptr create_ffmpeg_producer(const safe_ptr::max(); - - auto seek_it = boost::find(params, L"SEEK"); - if(seek_it != params.end()) - { - if(++seek_it != params.end()) - start = boost::lexical_cast(*seek_it); - } + auto path = filename + L"." + *ext; + auto loop = boost::range::find(params, L"LOOP") != params.end(); + auto start = core::get_param(L"SEEK", params, 0); + auto length = core::get_param(L"LENGTH", params, std::numeric_limits::max()); + auto filter_str = core::get_param(L"FILTER", params, L""); + + boost::replace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1"); + boost::replace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=1:-1"); - auto length_it = boost::find(params, L"LENGTH"); - if(length_it != params.end()) - { - if(++length_it != params.end()) - length = boost::lexical_cast(*length_it); - } - - std::wstring filter = L""; - auto filter_it = boost::find(params, L"FILTER"); - if(filter_it != params.end()) - { - if(++filter_it != params.end()) - filter = *filter_it; - } - - return make_safe(frame_factory, path, filter, loop, start, length); + return make_safe(frame_factory, path, filter_str, loop, start, length); } -} \ No newline at end of file +}} \ No newline at end of file