X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fffmpeg%2Fproducer%2Fffmpeg_producer.cpp;h=d20cbaca4f0a73f4d0280d3e121e1720b8ccdbd1;hb=53977d8992163fff46099451ac687794db6b7c36;hp=eec215c625fea64b0966b4289c487c32f4af21f6;hpb=8eba091f1862cd302b59041766afd867687a4a8a;p=casparcg diff --git a/modules/ffmpeg/producer/ffmpeg_producer.cpp b/modules/ffmpeg/producer/ffmpeg_producer.cpp index eec215c62..d20cbaca4 100644 --- a/modules/ffmpeg/producer/ffmpeg_producer.cpp +++ b/modules/ffmpeg/producer/ffmpeg_producer.cpp @@ -21,244 +21,223 @@ #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 +#include namespace caspar { - + struct ffmpeg_producer : public core::frame_producer { - static const size_t DECODED_PACKET_BUFFER_SIZE = 4; - static const size_t MAX_PACKET_OFFSET = 64; // Avoid infinite looping. - - const std::wstring filename_; - const bool loop_; + const std::wstring filename_; - const safe_ptr graph_; - boost::timer frame_timer_; - - std::deque> video_frame_buffer_; - std::deque> audio_chunk_buffer_; - - const safe_ptr frame_factory_; + const safe_ptr graph_; + boost::timer frame_timer_; + boost::timer video_timer_; + boost::timer audio_timer_; + + const safe_ptr frame_factory_; + const core::video_format_desc format_desc_; + + input input_; + video_decoder video_decoder_; + audio_decoder audio_decoder_; + double fps_; + frame_muxer muxer_; + + const int start_; + const bool loop_; + const size_t length_; - input input_; - std::unique_ptr video_decoder_; - std::unique_ptr audio_decoder_; + 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, 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) - , loop_(loop) - , graph_(diagnostics::create_graph(narrow(print()))) + , graph_(diagnostics::create_graph([this]{return 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()) + , 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)); - 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; - - try - { - video_decoder_.reset(input_.get_video_codec_context() ? - new video_decoder(*input_.get_video_codec_context(), frame_factory) : nullptr); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - CASPAR_LOG(warning) << print() << " failed to initialize video-decoder."; - } - - try - { - audio_decoder_.reset(input_.get_audio_codec_context() ? - new audio_decoder(*input_.get_audio_codec_context(), frame_factory->get_video_format_desc()) : nullptr); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - CASPAR_LOG(warning) << print() << " failed to initialize audio-decoder."; - } - - if(!video_decoder_ && !audio_decoder_) - { - BOOST_THROW_EXCEPTION( - caspar_exception() << - source_info(narrow(print())) << - msg_info("Failed to initialize any decoder")); - } + for(int n = 0; n < 32 && muxer_.empty(); ++n) + decode_frame(0); } - - virtual safe_ptr receive() + + virtual safe_ptr receive(int hints) { + auto frame = core::basic_frame::late(); + frame_timer_.restart(); - - std::shared_ptr frame; - for(size_t n = 0; !frame && input_.has_packet() && n < MAX_PACKET_OFFSET ; ++n) - frame = try_decode_frame(); - graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()*frame_factory_->get_video_format_desc().fps*0.5)); - - if(frame) - return make_safe(frame); + for(int n = 0; n < 64 && muxer_.empty(); ++n) + decode_frame(hints); - if(!input_.is_running()) - return core::basic_frame::eof(); + graph_->update_value("frame-time", static_cast(frame_timer_.elapsed()*format_desc_.fps*0.5)); - if(!video_decoder_ && !audio_decoder_) - return core::basic_frame::eof(); - - graph_->add_tag("underflow"); - return core::basic_frame::late(); + if(!muxer_.empty()) + frame = last_frame_ = muxer_.pop(); + else + { + if(input_.eof()) + return core::basic_frame::eof(); + else + graph_->add_tag("underflow"); + } + + return frame; } - - virtual std::wstring print() const + + virtual safe_ptr last_frame() const { - return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]"; + return disable_audio(last_frame_); } - - std::shared_ptr try_decode_frame() + + void decode_frame(int hints) { - tbb::parallel_invoke - ( - [&] + for(int n = 0; n < 16 && ((!muxer_.video_ready() && !video_decoder_.ready()) || (!muxer_.audio_ready() && !audio_decoder_.ready())); ++n) + { + std::shared_ptr pkt; + if(input_.try_pop(pkt)) { - if(video_frame_buffer_.size() < DECODED_PACKET_BUFFER_SIZE) - try_decode_video_packet(input_.get_video_packet()); - }, - [&] + video_decoder_.push(pkt); + audio_decoder_.push(pkt); + } + } + + tbb::parallel_invoke( + [&] + { + if(muxer_.video_ready()) + return; + + auto video_frames = video_decoder_.poll(); + BOOST_FOREACH(auto& video, video_frames) { - if(audio_chunk_buffer_.size() < DECODED_PACKET_BUFFER_SIZE) - try_decode_audio_packet(input_.get_audio_packet()); + is_progressive_ = video ? video->interlaced_frame == 0 : is_progressive_; + muxer_.push(video, hints); } - ); + }, + [&] + { + if(muxer_.audio_ready()) + return; + + auto audio_samples = audio_decoder_.poll(); + BOOST_FOREACH(auto& audio, audio_samples) + muxer_.push(audio); + }); - return try_merge_audio_and_video(); + muxer_.commit(); } - void try_decode_video_packet(const packet& video_packet) + virtual int64_t nb_frames() const { - if(!video_decoder_) - return; + if(loop_) + return std::numeric_limits::max(); - try - { - boost::range::push_back(video_frame_buffer_, video_decoder_->execute(video_packet)); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - video_decoder_.reset(); - CASPAR_LOG(warning) << print() << " removed video-stream."; - } - } - - void try_decode_audio_packet(const packet& audio_packet) - { - if(!audio_decoder_) - return; + // This function estimates nb_frames until input has read all packets for one loop, at which point the count should be accurate. - try + int64_t nb_frames = input_.nb_frames(); + if(input_.nb_loops() < 1) // input still hasn't counted all frames { - boost::range::push_back(audio_chunk_buffer_, audio_decoder_->execute(audio_packet)); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - audio_decoder_.reset(); - CASPAR_LOG(warning) << print() << " removed audio-stream."; + int64_t video_nb_frames = video_decoder_.nb_frames(); + int64_t audio_nb_frames = audio_decoder_.nb_frames(); + + nb_frames = std::min(static_cast(length_), std::max(nb_frames, std::max(video_nb_frames, audio_nb_frames))); } - } - std::shared_ptr try_merge_audio_and_video() - { - std::shared_ptr frame; + nb_frames = muxer_.calc_nb_frames(nb_frames); - if(!video_frame_buffer_.empty() && !audio_chunk_buffer_.empty()) - { - frame = video_frame_buffer_.front(); - video_frame_buffer_.pop_front(); - - frame->audio_data() = std::move(audio_chunk_buffer_.front()); - audio_chunk_buffer_.pop_front(); - } - else if(!video_frame_buffer_.empty() && !audio_decoder_) - { - frame = video_frame_buffer_.front(); - video_frame_buffer_.pop_front(); - frame->get_audio_transform().set_has_audio(false); - } - else if(!audio_chunk_buffer_.empty() && !video_decoder_) - { - frame = frame_factory_->create_frame(this, 1, 1); - std::fill(frame->image_data().begin(), frame->image_data().end(), 0); + // TODO: Might need to scale nb_frames av frame_muxer transformations. + + return nb_frames - start_; + } - frame->audio_data() = std::move(audio_chunk_buffer_.front()); - audio_chunk_buffer_.pop_front(); - } - - return frame; + virtual std::wstring print() const + { + 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(fps_) + + L"]"; } }; safe_ptr create_ffmpeg_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) { - return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex)); + return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename + L"." + ex)); }); if(ext == extensions.end()) return core::frame_producer::empty(); - std::wstring path = filename + L"." + *ext; - bool loop = boost::find(params, L"LOOP") != params.end(); - - static const boost::wregex expr(L"\\((?\\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; - - if(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); - } + 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"); - return make_safe(frame_factory, path, loop, start, length); + return make_safe(frame_factory, path, filter_str, loop, start, length); } } \ No newline at end of file