X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fffmpeg%2Fproducer%2Fffmpeg_producer.cpp;h=4a9ad443f9b903f66777ca01b6bfcdd513b6d0db;hb=0d225c5442d5a127b0eefd08c1cf723a37357211;hp=e430659d39317f0f3d3d9ed23f8f822b97344df9;hpb=92ee16d5c382f9ba1bc85528a031c5da87809e5b;p=casparcg diff --git a/modules/ffmpeg/producer/ffmpeg_producer.cpp b/modules/ffmpeg/producer/ffmpeg_producer.cpp index e430659d3..4a9ad443f 100644 --- a/modules/ffmpeg/producer/ffmpeg_producer.cpp +++ b/modules/ffmpeg/producer/ffmpeg_producer.cpp @@ -43,9 +43,21 @@ #include #include -#include +#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; +} struct ffmpeg_producer : public core::frame_producer { @@ -61,8 +73,8 @@ struct ffmpeg_producer : public core::frame_producer input input_; video_decoder video_decoder_; - audio_decoder audio_decoder_; - + audio_decoder audio_decoder_; + double fps_; frame_muxer muxer_; int late_frames_; @@ -71,61 +83,54 @@ struct ffmpeg_producer : public core::frame_producer safe_ptr last_frame_; - tbb::task_group tasks_; + const size_t width_; + const size_t height_; + bool progressive_; public: explicit ffmpeg_producer(const safe_ptr& frame_factory, const std::wstring& filename, const std::wstring& filter, bool loop, int start, int length) : filename_(filename) - , graph_(diagnostics::create_graph(narrow(print()))) + , graph_(diagnostics::create_graph([this]{return 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_(video_decoder_.fps(), frame_factory) + , fps_(validate_fps(video_decoder_.fps(), video_decoder_.nb_frames(), audio_decoder_.duration())) + , muxer_(fps_, frame_factory) , late_frames_(0) , start_(start) , loop_(loop) , last_frame_(core::basic_frame::empty()) + , width_(video_decoder_.width()) + , height_(video_decoder_.height()) + , 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("video-time", diagnostics::color(1.0f, 1.0f, 0.0f)); - graph_->set_color("audio-time", diagnostics::color(0.2f, 1.0f, 0.2f)); - 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)); - for(int n = 0; n < 128 && muxer_.size() < 2; ++n) + for(int n = 0; n < 32 && muxer_.empty(); ++n) decode_frame(0); } - - ~ffmpeg_producer() - { - try - { - tasks_.cancel(); - tasks_.wait(); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - } - } - + virtual safe_ptr receive(int hints) { auto frame = core::basic_frame::late(); frame_timer_.restart(); - for(int n = 0; n < 8 && muxer_.empty(); ++n) + for(int n = 0; n < 64 && muxer_.empty(); ++n) decode_frame(hints); + + graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()*format_desc_.fps*0.5)); if(!muxer_.empty()) frame = last_frame_ = muxer_.pop(); else { if(input_.eof()) - return frame = core::basic_frame::eof(); + return core::basic_frame::eof(); else { graph_->add_tag("underflow"); @@ -133,8 +138,6 @@ public: } } - graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()*format_desc_.fps*0.5)); - return frame; } @@ -145,10 +148,6 @@ public: void decode_frame(int hints) { - tasks_.wait(); - - muxer_.commit(); - for(int n = 0; n < 16 && ((!muxer_.video_ready() && !video_decoder_.ready()) || (!muxer_.audio_ready() && !audio_decoder_.ready())); ++n) { std::shared_ptr pkt; @@ -159,54 +158,60 @@ public: } } - if(!muxer_.video_ready()) + tbb::parallel_invoke( + [&] { - tasks_.run([=] - { - video_timer_.restart(); + if(muxer_.video_ready()) + return; - auto video_frames = video_decoder_.poll(); - BOOST_FOREACH(auto& video, video_frames) - muxer_.push(video, hints); - - graph_->update_value("video-time", static_cast(video_timer_.elapsed()*format_desc_.fps*0.5)); - }); - } - - if(!muxer_.audio_ready()) - { - tasks_.run([=] + auto video_frames = video_decoder_.poll(); + BOOST_FOREACH(auto& video, video_frames) { - audio_timer_.restart(); + progressive_ &= video->interlaced_frame != 0; + muxer_.push(video, hints); + } + }, + [&] + { + if(muxer_.audio_ready()) + return; - auto audio_samples = audio_decoder_.poll(); - BOOST_FOREACH(auto& audio, audio_samples) - muxer_.push(audio); + auto audio_samples = audio_decoder_.poll(); + BOOST_FOREACH(auto& audio, audio_samples) + muxer_.push(audio); + }); - graph_->update_value("audio-time", static_cast(audio_timer_.elapsed()*format_desc_.fps*0.5)); - }); - } + muxer_.commit(); } - virtual int64_t nb_frames() const + virtual int64_t nb_frames() const { if(loop_) - return 0; + return std::numeric_limits::max(); + + // This function estimates nb_frames until input has read all packets for one loop, at which point the count should be accurate. int64_t nb_frames = input_.nb_frames(); + if(input_.nb_loops() < 1) // input still hasn't counted all frames + { + 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 = muxer_.calc_nb_frames(nb_frames); + + // TODO: Might need to scale nb_frames av frame_muxer transformations. - if(nb_frames == 0) - nb_frames = video_decoder_.nb_frames(); - - if(nb_frames == 0) - nb_frames = audio_decoder_.nb_frames(); - return nb_frames + late_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(fps_) + (progressive_ ? L"p" : L"i") +L"|" + + boost::lexical_cast(width_) + L"x" + boost::lexical_cast(height_) + L"]"; } }; @@ -227,18 +232,25 @@ safe_ptr create_ffmpeg_producer(const safe_ptr::max(); - auto seek_it = std::find(params.begin(), params.end(), L"SEEK"); + auto seek_it = boost::find(params, L"SEEK"); if(seek_it != params.end()) { if(++seek_it != params.end()) - start = boost::lexical_cast(*seek_it); + start = boost::lexical_cast(*seek_it); + } + + 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 = std::find(params.begin(), params.end(), L"FILTER"); + auto filter_it = boost::find(params, L"FILTER"); if(filter_it != params.end()) { if(++filter_it != params.end())