X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fffmpeg%2Fproducer%2Fffmpeg_producer.cpp;h=115b663954a24143b4dfb260af446fb3c25beb04;hb=bc1a65afed2d7207ce6bba139f90638824261ec6;hp=362851b621998ca6189f63f0133d56eed7010a44;hpb=dfbde5d6d0fcff021c60c0240d94fece52481cf5;p=casparcg diff --git a/modules/ffmpeg/producer/ffmpeg_producer.cpp b/modules/ffmpeg/producer/ffmpeg_producer.cpp index 362851b62..115b66395 100644 --- a/modules/ffmpeg/producer/ffmpeg_producer.cpp +++ b/modules/ffmpeg/producer/ffmpeg_producer.cpp @@ -21,156 +21,197 @@ #include "ffmpeg_producer.h" +#include "frame_muxer.h" #include "input.h" +#include "util.h" #include "audio/audio_decoder.h" #include "video/video_decoder.h" +#include #include -#include #include -#include -#include -#include #include +#include +#include +#include -#include - -#include - +#include #include -#include -#include +#include +#include +#include +#include -#include +#include namespace caspar { - + struct ffmpeg_producer : public core::frame_producer { - const std::wstring filename_; + const std::wstring filename_; - const safe_ptr graph_; - boost::timer frame_timer_; + const safe_ptr graph_; + boost::timer frame_timer_; + boost::timer video_timer_; + boost::timer audio_timer_; - const safe_ptr frame_factory_; + const safe_ptr frame_factory_; + const core::video_format_desc format_desc_; + + input input_; + video_decoder video_decoder_; + audio_decoder audio_decoder_; + + frame_muxer muxer_; - input input_; - std::unique_ptr video_decoder_; - std::unique_ptr audio_decoder_; + int late_frames_; + const int start_; + const bool loop_; - std::deque>> audio_chunks_; - std::deque>> video_frames_; + safe_ptr last_frame_; + + tbb::task_group tasks_; + public: - explicit ffmpeg_producer(const safe_ptr& frame_factory, const std::wstring& filename, 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, int length) : filename_(filename) , graph_(diagnostics::create_graph(narrow(print()))) , frame_factory_(frame_factory) - , input_(safe_ptr(graph_), filename_, loop, start, length) + , 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) + , late_frames_(0) + , start_(start) + , loop_(loop) + , last_frame_(core::basic_frame::empty()) { graph_->add_guide("frame-time", 0.5); - graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f)); + 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)); - double frame_time = 1.0f/input_.fps(); - double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps; - if(abs(frame_time - format_frame_time) > 0.0001 && abs(frame_time - format_frame_time/2) > 0.0001) - CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time; - - video_decoder_.reset(input_.get_video_codec_context() ? - new video_decoder(input_, frame_factory) : nullptr); - - audio_decoder_.reset(input_.get_audio_codec_context() ? - new audio_decoder(input_, frame_factory->get_video_format_desc()) : nullptr); - - // Fill buffers. - decode_packets(); + for(int n = 0; n < 128 && muxer_.size() < 2; ++n) + decode_frame(0); } - virtual safe_ptr receive() + ~ffmpeg_producer() { - frame_timer_.restart(); - - auto result = decode_frame(); - - graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()*frame_factory_->get_video_format_desc().fps*0.5)); - - return result; + try + { + tasks_.cancel(); + tasks_.wait(); + } + catch(...) + { + CASPAR_LOG_CURRENT_EXCEPTION(); + } } - - virtual std::wstring print() const + + virtual safe_ptr receive(int hints) { - return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]"; - } + auto frame = core::basic_frame::late(); + + frame_timer_.restart(); + + for(int n = 0; n < 8 && muxer_.empty(); ++n) + decode_frame(hints); - void decode_packets() - { - tbb::parallel_invoke - ( - [&] - { - if(video_decoder_ && video_frames_.size() < 2) - video_frames_ = video_decoder_->receive(); - }, - [&] + if(!muxer_.empty()) + frame = last_frame_ = muxer_.pop(); + else + { + if(input_.eof()) + return last_frame(); + else { - if(audio_decoder_ && audio_chunks_.size() < 2) - audio_chunks_ = audio_decoder_->receive(); + graph_->add_tag("underflow"); + ++late_frames_; } - ); - - if(audio_decoder_ && video_decoder_ && !video_frames_.empty() && !audio_chunks_.empty() && - video_frames_.front().first == 0 && audio_chunks_.front().first != 0) - { - audio_decoder_->restart(); - audio_chunks_ = audio_decoder_->receive(); } - CASPAR_ASSERT(!(video_decoder_ && audio_decoder_ && !video_frames_.empty() && !audio_chunks_.empty()) || - video_frames_.front().first == audio_chunks_.front().first); + graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()*format_desc_.fps*0.5)); + + return frame; } - safe_ptr decode_frame() + virtual safe_ptr last_frame() const { - decode_packets(); + return disable_audio(last_frame_); + } - if(video_decoder_ && audio_decoder_ && !video_frames_.empty() && !audio_chunks_.empty()) - { - auto frame = std::move(video_frames_.front().second); - video_frames_.pop_front(); - - frame->audio_data() = std::move(audio_chunks_.front().second); - audio_chunks_.pop_front(); - - return frame; - } - else if(video_decoder_ && !audio_decoder_ && !video_frames_.empty()) + 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) { - auto frame = std::move(video_frames_.front().second); - video_frames_.pop_front(); - frame->get_audio_transform().set_has_audio(false); - - return frame; + std::shared_ptr pkt; + if(input_.try_pop(pkt)) + { + video_decoder_.push(pkt); + audio_decoder_.push(pkt); + } } - else if(audio_decoder_ && !video_decoder_ && !audio_chunks_.empty()) + + if(!muxer_.video_ready()) { - auto frame = frame_factory_->create_frame(this, 1, 1); - std::fill(frame->image_data().begin(), frame->image_data().end(), 0); - - frame->audio_data() = std::move(audio_chunks_.front().second); - audio_chunks_.pop_front(); + tasks_.run([=] + { + video_timer_.restart(); - return frame; - } - else if(!input_.is_running() || (!video_decoder_ && !audio_decoder_)) + 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()) { - return core::basic_frame::eof(); + tasks_.run([=] + { + audio_timer_.restart(); + + 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)); + }); } - else + } + + virtual int64_t nb_frames() const + { + if(loop_) + 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 { - graph_->add_tag("underflow"); - return core::basic_frame::late(); + 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)); } + + // TODO: Might need to scale nb_frames av frame_muxer transformations. + + return nb_frames + late_frames_ - start_; + } + + virtual std::wstring print() const + { + return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]"; } }; @@ -182,7 +223,7 @@ safe_ptr create_ffmpeg_producer(const safe_ptr create_ffmpeg_producer(const safe_ptr\\d+)(,(?\\d+)?)?\\)");//(,(?\\d+))?\\]"); // boost::regex has no repeated captures? - boost::wsmatch what; - auto it = std::find_if(params.begin(), params.end(), [&](const std::wstring& str) - { - return boost::regex_match(str, what, expr); - }); - int start = -1; int length = -1; + + auto seek_it = std::find(params.begin(), params.end(), L"SEEK"); + if(seek_it != params.end()) + { + if(++seek_it != params.end()) + start = boost::lexical_cast(*seek_it); + } - if(it != params.end()) + std::wstring filter = L""; + auto filter_it = std::find(params.begin(), params.end(), L"FILTER"); + if(filter_it != params.end()) { - start = lexical_cast_or_default(what["START"].str(), -1); - if(what["LENGTH"].matched) - length = lexical_cast_or_default(what["LENGTH"].str(), -1); + if(++filter_it != params.end()) + filter = *filter_it; } - - return make_safe(frame_factory, path, loop, start, length); + + return make_safe(frame_factory, path, filter, loop, start, length); } } \ No newline at end of file