3 #include "ffmpeg_consumer.h"
5 #include "../ffmpeg_error.h"
6 #include "../producer/util/util.h"
7 #include "../producer/filter/filter.h"
8 #include "../producer/filter/audio_filter.h"
10 #include <common/except.h>
11 #include <common/executor.h>
12 #include <common/assert.h>
13 #include <common/utf.h>
14 #include <common/future.h>
15 #include <common/diagnostics/graph.h>
16 #include <common/env.h>
17 #include <common/scope_exit.h>
18 #include <common/ptree.h>
19 #include <common/param.h>
20 #include <common/semaphore.h>
22 #include <core/consumer/frame_consumer.h>
23 #include <core/frame/frame.h>
24 #include <core/frame/audio_channel_layout.h>
25 #include <core/video_format.h>
26 #include <core/monitor/monitor.h>
27 #include <core/help/help_repository.h>
28 #include <core/help/help_sink.h>
30 #include <boost/noncopyable.hpp>
31 #include <boost/rational.hpp>
32 #include <boost/format.hpp>
33 #include <boost/algorithm/string/predicate.hpp>
34 #include <boost/property_tree/ptree.hpp>
37 #pragma warning(disable: 4244)
38 #pragma warning(disable: 4245)
39 #include <boost/crc.hpp>
42 #include <tbb/atomic.h>
43 #include <tbb/concurrent_queue.h>
44 #include <tbb/parallel_invoke.h>
45 #include <tbb/parallel_for.h>
50 #pragma warning(disable: 4244)
54 #define __STDC_CONSTANT_MACROS
55 #define __STDC_LIMIT_MACROS
56 #include <libavformat/avformat.h>
57 #include <libavcodec/avcodec.h>
58 #include <libavutil/avutil.h>
59 #include <libavutil/frame.h>
60 #include <libavutil/opt.h>
61 #include <libavutil/imgutils.h>
62 #include <libavutil/parseutils.h>
63 #include <libavfilter/avfilter.h>
64 #include <libavfilter/buffersink.h>
65 #include <libavfilter/buffersrc.h>
70 namespace caspar { namespace ffmpeg {
72 void set_pixel_format(AVFilterContext* sink, AVPixelFormat pix_fmt)
74 #pragma warning (push)
75 #pragma warning (disable : 4245)
77 FF(av_opt_set_int_list(
80 std::vector<AVPixelFormat>({ pix_fmt, AVPixelFormat::AV_PIX_FMT_NONE }).data(),
82 AV_OPT_SEARCH_CHILDREN));
87 void adjust_video_filter(const AVCodec& codec, const core::video_format_desc& in_format, AVFilterContext* sink, std::string& filter)
91 case AV_CODEC_ID_DVVIDEO:
93 if (in_format.format == core::video_format::ntsc)
94 filter = u8(append_filter(u16(filter), L"crop=720:480:0:2"));
96 // Pixel format selection
97 if (in_format.format == core::video_format::ntsc)
98 set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV411P);
99 else if (in_format.format == core::video_format::pal)
100 set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV420P);
102 set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV422P);
105 if (in_format.height == 1080)
106 filter = u8(append_filter(u16(filter), in_format.duration == 1001
108 : L"scale=1440:1080"));
109 else if (in_format.height == 720)
110 filter = u8(append_filter(u16(filter), L"scale=960:720"));
116 void setup_codec_defaults(AVCodecContext& encoder)
118 static const int MEGABIT = 1000000;
120 switch (encoder.codec_id)
122 case AV_CODEC_ID_DNXHD:
123 encoder.bit_rate = 220 * MEGABIT;
126 case AV_CODEC_ID_PRORES:
127 encoder.bit_rate = encoder.width < 1280
132 case AV_CODEC_ID_H264:
133 av_opt_set(encoder.priv_data, "preset", "ultrafast", 0);
134 av_opt_set(encoder.priv_data, "tune", "fastdecode", 0);
135 av_opt_set(encoder.priv_data, "crf", "5", 0);
141 bool is_pcm_s24le_not_supported(const AVFormatContext& container)
143 auto name = std::string(container.oformat->name);
145 if (name == "mp4" || name == "dv")
151 template<typename Out, typename In>
152 std::vector<Out> from_terminated_array(const In* array, In terminator)
154 std::vector<Out> result;
156 while (array != nullptr && *array != terminator)
159 Out casted = static_cast<Out>(val);
161 result.push_back(casted);
169 class ffmpeg_consumer
172 const spl::shared_ptr<diagnostics::graph> graph_;
173 core::monitor::subject subject_;
175 boost::filesystem::path full_path_;
177 std::map<std::string, std::string> options_;
180 core::video_format_desc in_video_format_;
181 core::audio_channel_layout in_channel_layout_ = core::audio_channel_layout::invalid();
183 std::shared_ptr<AVFormatContext> oc_;
184 tbb::atomic<bool> abort_request_;
186 std::shared_ptr<AVStream> video_st_;
187 std::vector<std::shared_ptr<AVStream>> audio_sts_;
189 std::int64_t video_pts_ = 0;
190 std::int64_t audio_pts_ = 0;
192 std::unique_ptr<audio_filter> audio_filter_;
194 // TODO: make use of already existent avfilter abstraction for video also
195 AVFilterContext* video_graph_in_;
196 AVFilterContext* video_graph_out_;
197 std::shared_ptr<AVFilterGraph> video_graph_;
199 executor video_encoder_executor_;
200 executor audio_encoder_executor_;
202 semaphore tokens_ { 0 };
204 tbb::atomic<int64_t> current_encoding_delay_;
206 executor write_executor_;
216 , mono_streams_(mono_streams)
217 , audio_encoder_executor_(print() + L" audio_encoder")
218 , video_encoder_executor_(print() + L" video_encoder")
219 , write_executor_(print() + L" io")
221 abort_request_ = false;
222 current_encoding_delay_ = 0;
225 boost::sregex_iterator(
228 boost::regex("-(?<NAME>[^-\\s]+)(\\s+(?<VALUE>[^\\s]+))?"));
229 it != boost::sregex_iterator();
232 options_[(*it)["NAME"].str()] = (*it)["VALUE"].matched ? (*it)["VALUE"].str() : "";
235 if (options_.find("threads") == options_.end())
236 options_["threads"] = "auto";
243 boost::regex("tokens")).get_value_or(2)));
252 video_encoder_executor_.begin_invoke([&] { encode_video(core::const_frame::empty(), nullptr); });
253 audio_encoder_executor_.begin_invoke([&] { encode_audio(core::const_frame::empty(), nullptr); });
255 video_encoder_executor_.stop();
256 audio_encoder_executor_.stop();
257 video_encoder_executor_.join();
258 audio_encoder_executor_.join();
260 video_graph_.reset();
261 audio_filter_.reset();
265 write_packet(nullptr, nullptr);
267 write_executor_.stop();
268 write_executor_.join();
270 FF(av_write_trailer(oc_.get()));
272 if (!(oc_->oformat->flags & AVFMT_NOFILE) && oc_->pb)
279 CASPAR_LOG_CURRENT_EXCEPTION();
285 const core::video_format_desc& format_desc,
286 const core::audio_channel_layout& channel_layout)
290 static boost::regex prot_exp("^.+:.*" );
292 if(!boost::regex_match(
296 if(!full_path_.is_complete())
300 env::media_folder()) +
304 if(boost::filesystem::exists(full_path_))
305 boost::filesystem::remove(full_path_);
307 boost::filesystem::create_directories(full_path_.parent_path());
310 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
311 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
312 graph_->set_text(print());
313 diagnostics::register_graph(graph_);
315 const auto oformat_name =
316 try_remove_arg<std::string>(
318 boost::regex("^f|format$"));
322 FF(avformat_alloc_output_context2(
325 oformat_name && !oformat_name->empty() ? oformat_name->c_str() : nullptr,
326 full_path_.string().c_str()));
330 avformat_free_context);
332 CASPAR_VERIFY(oc_->oformat);
334 oc_->interrupt_callback.callback = ffmpeg_consumer::interrupt_cb;
335 oc_->interrupt_callback.opaque = this;
337 CASPAR_VERIFY(format_desc.format != core::video_format::invalid);
339 in_video_format_ = format_desc;
340 in_channel_layout_ = channel_layout;
342 CASPAR_VERIFY(oc_->oformat);
344 const auto video_codec_name =
345 try_remove_arg<std::string>(
347 boost::regex("^c:v|codec:v|vcodec$"));
349 const auto video_codec =
351 ? avcodec_find_encoder_by_name(video_codec_name->c_str())
352 : avcodec_find_encoder(oc_->oformat->video_codec);
354 const auto audio_codec_name =
355 try_remove_arg<std::string>(
357 boost::regex("^c:a|codec:a|acodec$"));
359 const auto audio_codec =
361 ? avcodec_find_encoder_by_name(audio_codec_name->c_str())
362 : (is_pcm_s24le_not_supported(*oc_)
363 ? avcodec_find_encoder(oc_->oformat->audio_codec)
364 : avcodec_find_encoder_by_name("pcm_s24le"));
367 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
368 "Failed to find video codec " + (video_codec_name
370 : "with id " + boost::lexical_cast<std::string>(
371 oc_->oformat->video_codec))));
373 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
374 "Failed to find audio codec " + (audio_codec_name
376 : "with id " + boost::lexical_cast<std::string>(
377 oc_->oformat->audio_codec))));
382 configure_video_filters(
384 try_remove_arg<std::string>(options_,
385 boost::regex("vf|f:v|filter:v")).get_value_or(""));
387 configure_audio_filters(
389 try_remove_arg<std::string>(options_,
390 boost::regex("af|f:a|filter:a")).get_value_or(""));
396 auto video_options = options_;
397 auto audio_options = options_;
399 video_st_ = open_encoder(
404 for (int i = 0; i < audio_filter_->get_num_output_pads(); ++i)
405 audio_sts_.push_back(open_encoder(
410 auto it = options_.begin();
411 while(it != options_.end())
413 if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
414 it = options_.erase(it);
422 AVDictionary* av_opts = nullptr;
426 std::move(options_));
430 av_dict_free(&av_opts);
433 if (!(oc_->oformat->flags & AVFMT_NOFILE))
437 full_path_.string().c_str(),
439 &oc_->interrupt_callback,
443 FF(avformat_write_header(
447 options_ = to_map(av_opts);
458 for (const auto& option : options_)
461 << L"Invalid option: -"
464 << u16(option.second);
476 core::monitor::subject& monitor_output()
481 void send(core::const_frame frame)
483 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
485 auto frame_timer = spl::make_shared<caspar::timer>();
487 std::shared_ptr<void> token(
489 [this, frame, frame_timer](void*)
492 current_encoding_delay_ = frame.get_age_millis();
493 graph_->set_value("frame-time", frame_timer->elapsed() * in_video_format_.fps * 0.5);
497 video_encoder_executor_.begin_invoke([=]() mutable
504 audio_encoder_executor_.begin_invoke([=]() mutable
512 bool ready_for_frame() const
514 return tokens_.permits() > 0;
519 graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
522 std::wstring print() const
524 return L"ffmpeg_consumer[" + u16(path_) + L"]";
527 int64_t presentation_frame_age_millis() const
529 return current_encoding_delay_;
534 static int interrupt_cb(void* ctx)
537 return reinterpret_cast<ffmpeg_consumer*>(ctx)->abort_request_;
540 std::shared_ptr<AVStream> open_encoder(
541 const AVCodec& codec,
542 std::map<std::string,
543 std::string>& options,
544 int stream_number_for_media_type)
552 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("avformat_new_stream"));
554 auto enc = st->codec;
558 switch(enc->codec_type)
560 case AVMEDIA_TYPE_VIDEO:
562 enc->time_base = video_graph_out_->inputs[0]->time_base;
563 enc->pix_fmt = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
564 enc->sample_aspect_ratio = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
565 enc->width = video_graph_out_->inputs[0]->w;
566 enc->height = video_graph_out_->inputs[0]->h;
567 enc->bit_rate_tolerance = 400 * 1000000;
571 case AVMEDIA_TYPE_AUDIO:
573 enc->time_base = audio_filter_->get_output_pad_info(stream_number_for_media_type).time_base;
574 enc->sample_fmt = static_cast<AVSampleFormat>(audio_filter_->get_output_pad_info(stream_number_for_media_type).format);
575 enc->sample_rate = audio_filter_->get_output_pad_info(stream_number_for_media_type).sample_rate;
576 enc->channel_layout = audio_filter_->get_output_pad_info(stream_number_for_media_type).channel_layout;
577 enc->channels = audio_filter_->get_output_pad_info(stream_number_for_media_type).channels;
583 setup_codec_defaults(*enc);
585 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
586 enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
588 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
590 const auto char_id = char_id_map.at(enc->codec_type);
592 const auto codec_opts =
595 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
597 AVDictionary* av_codec_opts = nullptr;
612 av_codec_opts ? &av_codec_opts : nullptr));
621 AV_DICT_IGNORE_SUFFIX);
625 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
631 AV_DICT_IGNORE_SUFFIX);
634 av_dict_free(&av_codec_opts);
637 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
639 CASPAR_ASSERT(enc->frame_size > 0);
640 audio_filter_->set_guaranteed_output_num_samples_per_frame(
641 stream_number_for_media_type,
645 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
647 avcodec_close(st->codec);
651 void configure_video_filters(
652 const AVCodec& codec,
653 std::string filtergraph)
656 avfilter_graph_alloc(),
659 avfilter_graph_free(&p);
662 video_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
663 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
665 const auto sample_aspect_ratio =
666 boost::rational<int>(
667 in_video_format_.square_width,
668 in_video_format_.square_height) /
669 boost::rational<int>(
670 in_video_format_.width,
671 in_video_format_.height);
673 const auto vsrc_options = (boost::format("video_size=%1%x%2%:pix_fmt=%3%:time_base=%4%/%5%:pixel_aspect=%6%/%7%:frame_rate=%8%/%9%")
674 % in_video_format_.width % in_video_format_.height
675 % AVPixelFormat::AV_PIX_FMT_BGRA
676 % in_video_format_.duration % in_video_format_.time_scale
677 % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
678 % in_video_format_.time_scale % in_video_format_.duration).str();
680 AVFilterContext* filt_vsrc = nullptr;
681 FF(avfilter_graph_create_filter(
683 avfilter_get_by_name("buffer"),
684 "ffmpeg_consumer_buffer",
685 vsrc_options.c_str(),
687 video_graph_.get()));
689 AVFilterContext* filt_vsink = nullptr;
690 FF(avfilter_graph_create_filter(
692 avfilter_get_by_name("buffersink"),
693 "ffmpeg_consumer_buffersink",
696 video_graph_.get()));
698 #pragma warning (push)
699 #pragma warning (disable : 4245)
701 FF(av_opt_set_int_list(
706 AV_OPT_SEARCH_CHILDREN));
708 #pragma warning (pop)
710 adjust_video_filter(codec, in_video_format_, filt_vsink, filtergraph);
712 if (in_video_format_.width < 1280)
713 video_graph_->scale_sws_opts = "out_color_matrix=bt601";
715 video_graph_->scale_sws_opts = "out_color_matrix=bt709";
717 configure_filtergraph(
723 video_graph_in_ = filt_vsrc;
724 video_graph_out_ = filt_vsink;
727 << u16(std::string("\n")
728 + avfilter_graph_dump(
733 void configure_audio_filters(
734 const AVCodec& codec,
735 std::string filtergraph)
737 int num_output_pads = 1;
741 num_output_pads = in_channel_layout_.num_channels;
744 if (num_output_pads > 1)
746 std::string splitfilter = "[a:0]channelsplit=channel_layout=";
748 splitfilter += (boost::format("0x%|1$x|") % create_channel_layout_bitmask(in_channel_layout_.num_channels)).str();
750 for (int i = 0; i < num_output_pads; ++i)
751 splitfilter += "[aout:" + boost::lexical_cast<std::string>(i) + "]";
753 filtergraph = u8(append_filter(u16(filtergraph), u16(splitfilter)));
756 std::vector<audio_output_pad> output_pads(
759 from_terminated_array<int>( codec.supported_samplerates, 0),
760 from_terminated_array<AVSampleFormat>( codec.sample_fmts, AVSampleFormat::AV_SAMPLE_FMT_NONE),
761 from_terminated_array<uint64_t>( codec.channel_layouts, static_cast<uint64_t>(0))));
763 audio_filter_.reset(new audio_filter(
765 boost::rational<int>(1, in_video_format_.audio_sample_rate),
766 in_video_format_.audio_sample_rate,
767 AVSampleFormat::AV_SAMPLE_FMT_S32,
768 create_channel_layout_bitmask(in_channel_layout_.num_channels)) },
773 void configure_filtergraph(
774 AVFilterGraph& graph,
775 const std::string& filtergraph,
776 AVFilterContext& source_ctx,
777 AVFilterContext& sink_ctx)
779 AVFilterInOut* outputs = nullptr;
780 AVFilterInOut* inputs = nullptr;
782 if(!filtergraph.empty())
784 outputs = avfilter_inout_alloc();
785 inputs = avfilter_inout_alloc();
789 CASPAR_VERIFY(outputs && inputs);
791 outputs->name = av_strdup("in");
792 outputs->filter_ctx = &source_ctx;
793 outputs->pad_idx = 0;
794 outputs->next = nullptr;
796 inputs->name = av_strdup("out");
797 inputs->filter_ctx = &sink_ctx;
799 inputs->next = nullptr;
803 avfilter_inout_free(&outputs);
804 avfilter_inout_free(&inputs);
808 FF(avfilter_graph_parse(
824 FF(avfilter_graph_config(
829 void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
834 auto enc = video_st_->codec;
836 if(frame_ptr != core::const_frame::empty())
838 auto src_av_frame = create_frame();
840 const auto sample_aspect_ratio =
841 boost::rational<int>(
842 in_video_format_.square_width,
843 in_video_format_.square_height) /
844 boost::rational<int>(
845 in_video_format_.width,
846 in_video_format_.height);
848 src_av_frame->format = AVPixelFormat::AV_PIX_FMT_BGRA;
849 src_av_frame->width = in_video_format_.width;
850 src_av_frame->height = in_video_format_.height;
851 src_av_frame->sample_aspect_ratio.num = sample_aspect_ratio.numerator();
852 src_av_frame->sample_aspect_ratio.den = sample_aspect_ratio.denominator();
853 src_av_frame->pts = video_pts_;
858 << core::monitor::message("/frame") % video_pts_
859 << core::monitor::message("/path") % path_
860 << core::monitor::message("/fps") % in_video_format_.fps;
862 FF(av_image_fill_arrays(
864 src_av_frame->linesize,
865 frame_ptr.image_data().begin(),
866 static_cast<AVPixelFormat>(src_av_frame->format),
867 in_video_format_.width,
868 in_video_format_.height,
871 FF(av_buffersrc_add_frame(
873 src_av_frame.get()));
880 auto filt_frame = create_frame();
882 ret = av_buffersink_get_frame(
886 video_encoder_executor_.begin_invoke([=]
888 if(ret == AVERROR_EOF)
890 if(enc->codec->capabilities & CODEC_CAP_DELAY)
892 while(encode_av_frame(
894 avcodec_encode_video2,
897 boost::this_thread::yield(); // TODO:
901 else if(ret != AVERROR(EAGAIN))
903 FF_RET(ret, "av_buffersink_get_frame");
905 if (filt_frame->interlaced_frame)
907 if (enc->codec->id == AV_CODEC_ID_MJPEG)
908 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
910 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
913 enc->field_order = AV_FIELD_PROGRESSIVE;
915 filt_frame->quality = enc->global_quality;
917 if (!enc->me_threshold)
918 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
922 avcodec_encode_video2,
926 boost::this_thread::yield(); // TODO:
932 void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
934 if(audio_sts_.empty())
937 if(frame_ptr != core::const_frame::empty())
939 auto src_av_frame = create_frame();
941 src_av_frame->channels = in_channel_layout_.num_channels;
942 src_av_frame->channel_layout = create_channel_layout_bitmask(in_channel_layout_.num_channels);
943 src_av_frame->sample_rate = in_video_format_.audio_sample_rate;
944 src_av_frame->nb_samples = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
945 src_av_frame->format = AV_SAMPLE_FMT_S32;
946 src_av_frame->pts = audio_pts_;
948 audio_pts_ += src_av_frame->nb_samples;
950 FF(av_samples_fill_arrays(
951 src_av_frame->extended_data,
952 src_av_frame->linesize,
953 reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
954 src_av_frame->channels,
955 src_av_frame->nb_samples,
956 static_cast<AVSampleFormat>(src_av_frame->format),
959 audio_filter_->push(0, src_av_frame);
962 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
964 for (auto filt_frame : audio_filter_->poll_all(pad_id))
966 audio_encoder_executor_.begin_invoke([=]
969 *audio_sts_.at(pad_id),
970 avcodec_encode_audio2,
974 boost::this_thread::yield(); // TODO:
979 bool eof = frame_ptr == core::const_frame::empty();
983 audio_encoder_executor_.begin_invoke([=]
985 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
987 auto enc = audio_sts_.at(pad_id)->codec;
989 if (enc->codec->capabilities & CODEC_CAP_DELAY)
991 while (encode_av_frame(
992 *audio_sts_.at(pad_id),
993 avcodec_encode_audio2,
997 boost::this_thread::yield(); // TODO:
1005 template<typename F>
1006 bool encode_av_frame(
1009 const std::shared_ptr<AVFrame>& src_av_frame,
1010 std::shared_ptr<void> token)
1013 av_init_packet(&pkt);
1023 if(!got_packet || pkt.size <= 0)
1026 pkt.stream_index = st.index;
1028 if (pkt.pts != AV_NOPTS_VALUE)
1033 st.codec->time_base,
1037 if (pkt.dts != AV_NOPTS_VALUE)
1042 st.codec->time_base,
1050 st.codec->time_base, st.time_base));
1053 std::shared_ptr<AVPacket>(
1065 const std::shared_ptr<AVPacket>& pkt_ptr,
1066 std::shared_ptr<void> token)
1068 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1070 FF(av_interleaved_write_frame(
1076 template<typename T>
1077 static boost::optional<T> try_remove_arg(
1078 std::map<std::string, std::string>& options,
1079 const boost::regex& expr)
1081 for(auto it = options.begin(); it != options.end(); ++it)
1083 if(boost::regex_search(it->first, expr))
1085 auto arg = it->second;
1087 return boost::lexical_cast<T>(arg);
1091 return boost::optional<T>();
1094 static std::map<std::string, std::string> remove_options(
1095 std::map<std::string, std::string>& options,
1096 const boost::regex& expr)
1098 std::map<std::string, std::string> result;
1100 auto it = options.begin();
1101 while(it != options.end())
1104 if(boost::regex_search(it->first, what, expr))
1107 what.size() > 0 && what[1].matched
1109 : it->first] = it->second;
1110 it = options.erase(it);
1119 static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1121 for (const auto& entry : c)
1125 entry.first.c_str(),
1126 entry.second.c_str(), 0);
1130 static std::map<std::string, std::string> to_map(AVDictionary* dict)
1132 std::map<std::string, std::string> result;
1139 AV_DICT_IGNORE_SUFFIX)
1146 AV_DICT_IGNORE_SUFFIX))
1148 result[t->key] = t->value;
1155 int crc16(const std::string& str)
1157 boost::crc_16_type result;
1159 result.process_bytes(str.data(), str.length());
1161 return result.checksum();
1164 struct ffmpeg_consumer_proxy : public core::frame_consumer
1166 const std::string path_;
1167 const std::string options_;
1168 const bool separate_key_;
1169 const bool mono_streams_;
1170 const bool compatibility_mode_;
1171 int consumer_index_offset_;
1173 std::unique_ptr<ffmpeg_consumer> consumer_;
1174 std::unique_ptr<ffmpeg_consumer> key_only_consumer_;
1178 ffmpeg_consumer_proxy(const std::string& path, const std::string& options, bool separate_key, bool mono_streams, bool compatibility_mode)
1181 , separate_key_(separate_key)
1182 , mono_streams_(mono_streams)
1183 , compatibility_mode_(compatibility_mode)
1184 , consumer_index_offset_(crc16(path))
1188 void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int) override
1191 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
1193 consumer_.reset(new ffmpeg_consumer(path_, options_, mono_streams_));
1194 consumer_->initialize(format_desc, channel_layout);
1198 boost::filesystem::path fill_file(path_);
1199 auto without_extension = u16(fill_file.parent_path().string() + "/" + fill_file.stem().string());
1200 auto key_file = without_extension + L"_A" + u16(fill_file.extension().string());
1202 key_only_consumer_.reset(new ffmpeg_consumer(u8(key_file), options_, mono_streams_));
1203 key_only_consumer_->initialize(format_desc, channel_layout);
1207 int64_t presentation_frame_age_millis() const override
1209 return consumer_ ? static_cast<int64_t>(consumer_->presentation_frame_age_millis()) : 0;
1212 std::future<bool> send(core::const_frame frame) override
1214 bool ready_for_frame = consumer_->ready_for_frame();
1216 if (ready_for_frame && separate_key_)
1217 ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
1219 if (ready_for_frame)
1221 consumer_->send(frame);
1224 key_only_consumer_->send(frame.key_only());
1228 consumer_->mark_dropped();
1231 key_only_consumer_->mark_dropped();
1234 return make_ready_future(true);
1237 std::wstring print() const override
1239 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
1242 std::wstring name() const override
1247 boost::property_tree::wptree info() const override
1249 boost::property_tree::wptree info;
1251 info.add(L"type", L"ffmpeg");
1252 info.add(L"path", u16(path_));
1253 info.add(L"separate_key", separate_key_);
1254 info.add(L"mono_streams", mono_streams_);
1259 bool has_synchronization_clock() const override
1264 int buffer_depth() const override
1269 int index() const override
1271 return compatibility_mode_ ? 200 : 100000 + consumer_index_offset_;
1274 core::monitor::subject& monitor_output() override
1276 return consumer_->monitor_output();
1280 void describe_ffmpeg_consumer(core::help_sink& sink, const core::help_repository& repo)
1282 sink.short_description(L"For streaming/recording the contents of a channel using FFmpeg.");
1283 sink.syntax(L"FILE,STREAM [filename:string],[url:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}} {[separate_key:SEPARATE_KEY]} {[mono_streams:MONO_STREAMS]}");
1284 sink.para()->text(L"For recording or streaming the contents of a channel using FFmpeg");
1286 ->item(L"filename", L"The filename under the media folder including the extension (decides which kind of container format that will be used).")
1287 ->item(L"url", L"If the filename is given in the form of an URL a network stream will be created instead of a file on disk.")
1288 ->item(L"ffmpeg_paramX", L"A parameter supported by FFmpeg. For example vcodec or acodec etc.")
1289 ->item(L"separate_key", L"If defined will create two files simultaneously -- One for fill and one for key (_A will be appended).")
1290 ->item(L"mono_streams", L"If defined every audio channel will be written to its own audio stream.");
1291 sink.para()->text(L"Examples:");
1292 sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd");
1293 sink.example(L">> ADD 1 FILE output.mov -vcodec prores");
1294 sink.example(L">> ADD 1 FILE output.mov -vcodec dvvideo");
1295 sink.example(L">> ADD 1 FILE output.mov -vcodec libx264 -preset ultrafast -tune fastdecode -crf 25");
1296 sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd SEPARATE_KEY", L"for creating output.mov with fill and output_A.mov with key/alpha");
1297 sink.example(L">> ADD 1 FILE output.mxf -vcodec dnxhd MONO_STREAMS", L"for creating output.mxf with every audio channel encoded in its own mono stream.");
1298 sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast",
1299 L"for streaming over UDP instead of creating a local file.");
1302 spl::shared_ptr<core::frame_consumer> create_ffmpeg_consumer(
1303 const std::vector<std::wstring>& params, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1305 if (params.size() < 1 || (!boost::iequals(params.at(0), L"STREAM") && !boost::iequals(params.at(0), L"FILE")))
1306 return core::frame_consumer::empty();
1308 auto params2 = params;
1309 bool separate_key = get_and_consume_flag(L"SEPARATE_KEY", params2);
1310 bool mono_streams = get_and_consume_flag(L"MONO_STREAMS", params2);
1311 auto compatibility_mode = boost::iequals(params.at(0), L"FILE");
1312 auto path = u8(params2.size() > 1 ? params2.at(1) : L"");
1314 // remove FILE or STREAM
1315 params2.erase(params2.begin());
1319 params2.erase(params2.begin());
1321 // join only the args
1322 auto args = u8(boost::join(params2, L" "));
1324 return spl::make_shared<ffmpeg_consumer_proxy>(path, args, separate_key, mono_streams, compatibility_mode);
1327 spl::shared_ptr<core::frame_consumer> create_preconfigured_ffmpeg_consumer(
1328 const boost::property_tree::wptree& ptree, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1330 return spl::make_shared<ffmpeg_consumer_proxy>(
1331 u8(ptree_get<std::wstring>(ptree, L"path")),
1332 u8(ptree.get<std::wstring>(L"args", L"")),
1333 ptree.get<bool>(L"separate-key", false),
1334 ptree.get<bool>(L"mono-streams", false),