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_, boost::regex("vf|f:v|filter:v"))
386 try_remove_arg<std::string>(options_, boost::regex("pix_fmt")));
388 configure_audio_filters(
390 try_remove_arg<std::string>(options_,
391 boost::regex("af|f:a|filter:a")).get_value_or(""));
397 auto video_options = options_;
398 auto audio_options = options_;
400 video_st_ = open_encoder(
405 for (int i = 0; i < audio_filter_->get_num_output_pads(); ++i)
406 audio_sts_.push_back(open_encoder(
411 auto it = options_.begin();
412 while(it != options_.end())
414 if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
415 it = options_.erase(it);
423 AVDictionary* av_opts = nullptr;
427 std::move(options_));
431 av_dict_free(&av_opts);
434 if (!(oc_->oformat->flags & AVFMT_NOFILE))
438 full_path_.string().c_str(),
440 &oc_->interrupt_callback,
444 FF(avformat_write_header(
448 options_ = to_map(av_opts);
459 for (const auto& option : options_)
462 << L"Invalid option: -"
465 << u16(option.second);
477 core::monitor::subject& monitor_output()
482 void send(core::const_frame frame)
484 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
486 auto frame_timer = spl::make_shared<caspar::timer>();
488 std::shared_ptr<void> token(
490 [this, frame, frame_timer](void*)
493 current_encoding_delay_ = frame.get_age_millis();
494 graph_->set_value("frame-time", frame_timer->elapsed() * in_video_format_.fps * 0.5);
498 video_encoder_executor_.begin_invoke([=]() mutable
505 audio_encoder_executor_.begin_invoke([=]() mutable
513 bool ready_for_frame() const
515 return tokens_.permits() > 0;
520 graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
523 std::wstring print() const
525 return L"ffmpeg_consumer[" + u16(path_) + L"]";
528 int64_t presentation_frame_age_millis() const
530 return current_encoding_delay_;
535 static int interrupt_cb(void* ctx)
538 return reinterpret_cast<ffmpeg_consumer*>(ctx)->abort_request_;
541 std::shared_ptr<AVStream> open_encoder(
542 const AVCodec& codec,
543 std::map<std::string,
544 std::string>& options,
545 int stream_number_for_media_type)
553 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("avformat_new_stream"));
555 auto enc = st->codec;
559 switch(enc->codec_type)
561 case AVMEDIA_TYPE_VIDEO:
563 enc->time_base = video_graph_out_->inputs[0]->time_base;
564 enc->pix_fmt = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
565 enc->sample_aspect_ratio = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
566 enc->width = video_graph_out_->inputs[0]->w;
567 enc->height = video_graph_out_->inputs[0]->h;
568 enc->bit_rate_tolerance = 400 * 1000000;
572 case AVMEDIA_TYPE_AUDIO:
574 enc->time_base = audio_filter_->get_output_pad_info(stream_number_for_media_type).time_base;
575 enc->sample_fmt = static_cast<AVSampleFormat>(audio_filter_->get_output_pad_info(stream_number_for_media_type).format);
576 enc->sample_rate = audio_filter_->get_output_pad_info(stream_number_for_media_type).sample_rate;
577 enc->channel_layout = audio_filter_->get_output_pad_info(stream_number_for_media_type).channel_layout;
578 enc->channels = audio_filter_->get_output_pad_info(stream_number_for_media_type).channels;
584 setup_codec_defaults(*enc);
586 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
587 enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
589 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
591 const auto char_id = char_id_map.at(enc->codec_type);
593 const auto codec_opts =
596 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
598 AVDictionary* av_codec_opts = nullptr;
613 av_codec_opts ? &av_codec_opts : nullptr));
622 AV_DICT_IGNORE_SUFFIX);
626 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
632 AV_DICT_IGNORE_SUFFIX);
635 av_dict_free(&av_codec_opts);
638 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
640 CASPAR_ASSERT(enc->frame_size > 0);
641 audio_filter_->set_guaranteed_output_num_samples_per_frame(
642 stream_number_for_media_type,
646 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
648 avcodec_close(st->codec);
652 void configure_video_filters(
653 const AVCodec& codec,
654 std::string filtergraph,
655 const boost::optional<std::string>& preferred_pix_fmt)
658 avfilter_graph_alloc(),
661 avfilter_graph_free(&p);
664 video_graph_->nb_threads = boost::thread::hardware_concurrency()/2;
665 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
667 const auto sample_aspect_ratio =
668 boost::rational<int>(
669 in_video_format_.square_width,
670 in_video_format_.square_height) /
671 boost::rational<int>(
672 in_video_format_.width,
673 in_video_format_.height);
675 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%")
676 % in_video_format_.width % in_video_format_.height
677 % AVPixelFormat::AV_PIX_FMT_BGRA
678 % in_video_format_.duration % in_video_format_.time_scale
679 % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
680 % in_video_format_.time_scale % in_video_format_.duration).str();
682 AVFilterContext* filt_vsrc = nullptr;
683 FF(avfilter_graph_create_filter(
685 avfilter_get_by_name("buffer"),
686 "ffmpeg_consumer_buffer",
687 vsrc_options.c_str(),
689 video_graph_.get()));
691 AVFilterContext* filt_vsink = nullptr;
692 FF(avfilter_graph_create_filter(
694 avfilter_get_by_name("buffersink"),
695 "ffmpeg_consumer_buffersink",
698 video_graph_.get()));
700 #pragma warning (push)
701 #pragma warning (disable : 4245)
703 if (preferred_pix_fmt)
705 auto requested_fmt = av_get_pix_fmt(preferred_pix_fmt->c_str());
706 auto valid_fmts = from_terminated_array<AVPixelFormat>(codec.pix_fmts, AVPixelFormat::AV_PIX_FMT_NONE);
708 if (!cpplinq::from(valid_fmts).contains(requested_fmt))
709 CASPAR_THROW_EXCEPTION(user_error() << msg_info(*preferred_pix_fmt + " is not supported by codec."));
711 std::vector<AVPixelFormat> fmts = { requested_fmt, AVPixelFormat::AV_PIX_FMT_NONE };
713 FF(av_opt_set_int_list(
718 AV_OPT_SEARCH_CHILDREN));
722 FF(av_opt_set_int_list(
727 AV_OPT_SEARCH_CHILDREN));
731 #pragma warning (pop)
733 adjust_video_filter(codec, in_video_format_, filt_vsink, filtergraph);
735 if (in_video_format_.width < 1280)
736 video_graph_->scale_sws_opts = "out_color_matrix=bt601";
738 video_graph_->scale_sws_opts = "out_color_matrix=bt709";
740 configure_filtergraph(
746 video_graph_in_ = filt_vsrc;
747 video_graph_out_ = filt_vsink;
750 << u16(std::string("\n")
751 + avfilter_graph_dump(
756 void configure_audio_filters(
757 const AVCodec& codec,
758 std::string filtergraph)
760 int num_output_pads = 1;
764 num_output_pads = in_channel_layout_.num_channels;
767 if (num_output_pads > 1)
769 std::string splitfilter = "[a:0]channelsplit=channel_layout=";
771 splitfilter += (boost::format("0x%|1$x|") % create_channel_layout_bitmask(in_channel_layout_.num_channels)).str();
773 for (int i = 0; i < num_output_pads; ++i)
774 splitfilter += "[aout:" + boost::lexical_cast<std::string>(i) + "]";
776 filtergraph = u8(append_filter(u16(filtergraph), u16(splitfilter)));
779 std::vector<audio_output_pad> output_pads(
782 from_terminated_array<int>( codec.supported_samplerates, 0),
783 from_terminated_array<AVSampleFormat>( codec.sample_fmts, AVSampleFormat::AV_SAMPLE_FMT_NONE),
784 from_terminated_array<uint64_t>( codec.channel_layouts, static_cast<uint64_t>(0))));
786 audio_filter_.reset(new audio_filter(
788 boost::rational<int>(1, in_video_format_.audio_sample_rate),
789 in_video_format_.audio_sample_rate,
790 AVSampleFormat::AV_SAMPLE_FMT_S32,
791 create_channel_layout_bitmask(in_channel_layout_.num_channels)) },
796 void configure_filtergraph(
797 AVFilterGraph& graph,
798 const std::string& filtergraph,
799 AVFilterContext& source_ctx,
800 AVFilterContext& sink_ctx)
802 AVFilterInOut* outputs = nullptr;
803 AVFilterInOut* inputs = nullptr;
805 if(!filtergraph.empty())
807 outputs = avfilter_inout_alloc();
808 inputs = avfilter_inout_alloc();
812 CASPAR_VERIFY(outputs && inputs);
814 outputs->name = av_strdup("in");
815 outputs->filter_ctx = &source_ctx;
816 outputs->pad_idx = 0;
817 outputs->next = nullptr;
819 inputs->name = av_strdup("out");
820 inputs->filter_ctx = &sink_ctx;
822 inputs->next = nullptr;
826 avfilter_inout_free(&outputs);
827 avfilter_inout_free(&inputs);
831 FF(avfilter_graph_parse(
847 FF(avfilter_graph_config(
852 void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
857 auto enc = video_st_->codec;
859 if(frame_ptr != core::const_frame::empty())
861 auto src_av_frame = create_frame();
863 const auto sample_aspect_ratio =
864 boost::rational<int>(
865 in_video_format_.square_width,
866 in_video_format_.square_height) /
867 boost::rational<int>(
868 in_video_format_.width,
869 in_video_format_.height);
871 src_av_frame->format = AVPixelFormat::AV_PIX_FMT_BGRA;
872 src_av_frame->width = in_video_format_.width;
873 src_av_frame->height = in_video_format_.height;
874 src_av_frame->sample_aspect_ratio.num = sample_aspect_ratio.numerator();
875 src_av_frame->sample_aspect_ratio.den = sample_aspect_ratio.denominator();
876 src_av_frame->pts = video_pts_;
881 << core::monitor::message("/frame") % video_pts_
882 << core::monitor::message("/path") % path_
883 << core::monitor::message("/fps") % in_video_format_.fps;
885 FF(av_image_fill_arrays(
887 src_av_frame->linesize,
888 frame_ptr.image_data().begin(),
889 static_cast<AVPixelFormat>(src_av_frame->format),
890 in_video_format_.width,
891 in_video_format_.height,
894 FF(av_buffersrc_add_frame(
896 src_av_frame.get()));
903 auto filt_frame = create_frame();
905 ret = av_buffersink_get_frame(
909 video_encoder_executor_.begin_invoke([=]
911 if(ret == AVERROR_EOF)
913 if(enc->codec->capabilities & CODEC_CAP_DELAY)
915 while(encode_av_frame(
917 avcodec_encode_video2,
920 boost::this_thread::yield(); // TODO:
924 else if(ret != AVERROR(EAGAIN))
926 FF_RET(ret, "av_buffersink_get_frame");
928 if (filt_frame->interlaced_frame)
930 if (enc->codec->id == AV_CODEC_ID_MJPEG)
931 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
933 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
936 enc->field_order = AV_FIELD_PROGRESSIVE;
938 filt_frame->quality = enc->global_quality;
940 if (!enc->me_threshold)
941 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
945 avcodec_encode_video2,
949 boost::this_thread::yield(); // TODO:
955 void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
957 if(audio_sts_.empty())
960 if(frame_ptr != core::const_frame::empty())
962 auto src_av_frame = create_frame();
964 src_av_frame->channels = in_channel_layout_.num_channels;
965 src_av_frame->channel_layout = create_channel_layout_bitmask(in_channel_layout_.num_channels);
966 src_av_frame->sample_rate = in_video_format_.audio_sample_rate;
967 src_av_frame->nb_samples = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
968 src_av_frame->format = AV_SAMPLE_FMT_S32;
969 src_av_frame->pts = audio_pts_;
971 audio_pts_ += src_av_frame->nb_samples;
973 FF(av_samples_fill_arrays(
974 src_av_frame->extended_data,
975 src_av_frame->linesize,
976 reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
977 src_av_frame->channels,
978 src_av_frame->nb_samples,
979 static_cast<AVSampleFormat>(src_av_frame->format),
982 audio_filter_->push(0, src_av_frame);
985 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
987 for (auto filt_frame : audio_filter_->poll_all(pad_id))
989 audio_encoder_executor_.begin_invoke([=]
992 *audio_sts_.at(pad_id),
993 avcodec_encode_audio2,
997 boost::this_thread::yield(); // TODO:
1002 bool eof = frame_ptr == core::const_frame::empty();
1006 audio_encoder_executor_.begin_invoke([=]
1008 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
1010 auto enc = audio_sts_.at(pad_id)->codec;
1012 if (enc->codec->capabilities & CODEC_CAP_DELAY)
1014 while (encode_av_frame(
1015 *audio_sts_.at(pad_id),
1016 avcodec_encode_audio2,
1020 boost::this_thread::yield(); // TODO:
1028 template<typename F>
1029 bool encode_av_frame(
1032 const std::shared_ptr<AVFrame>& src_av_frame,
1033 std::shared_ptr<void> token)
1036 av_init_packet(&pkt);
1046 if(!got_packet || pkt.size <= 0)
1049 pkt.stream_index = st.index;
1051 if (pkt.pts != AV_NOPTS_VALUE)
1056 st.codec->time_base,
1060 if (pkt.dts != AV_NOPTS_VALUE)
1065 st.codec->time_base,
1073 st.codec->time_base, st.time_base));
1076 std::shared_ptr<AVPacket>(
1088 const std::shared_ptr<AVPacket>& pkt_ptr,
1089 std::shared_ptr<void> token)
1091 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1093 FF(av_interleaved_write_frame(
1099 template<typename T>
1100 static boost::optional<T> try_remove_arg(
1101 std::map<std::string, std::string>& options,
1102 const boost::regex& expr)
1104 for(auto it = options.begin(); it != options.end(); ++it)
1106 if(boost::regex_search(it->first, expr))
1108 auto arg = it->second;
1110 return boost::lexical_cast<T>(arg);
1114 return boost::optional<T>();
1117 static std::map<std::string, std::string> remove_options(
1118 std::map<std::string, std::string>& options,
1119 const boost::regex& expr)
1121 std::map<std::string, std::string> result;
1123 auto it = options.begin();
1124 while(it != options.end())
1127 if(boost::regex_search(it->first, what, expr))
1130 what.size() > 0 && what[1].matched
1132 : it->first] = it->second;
1133 it = options.erase(it);
1142 static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1144 for (const auto& entry : c)
1148 entry.first.c_str(),
1149 entry.second.c_str(), 0);
1153 static std::map<std::string, std::string> to_map(AVDictionary* dict)
1155 std::map<std::string, std::string> result;
1162 AV_DICT_IGNORE_SUFFIX)
1169 AV_DICT_IGNORE_SUFFIX))
1171 result[t->key] = t->value;
1178 int crc16(const std::string& str)
1180 boost::crc_16_type result;
1182 result.process_bytes(str.data(), str.length());
1184 return result.checksum();
1187 struct ffmpeg_consumer_proxy : public core::frame_consumer
1189 const std::string path_;
1190 const std::string options_;
1191 const bool separate_key_;
1192 const bool mono_streams_;
1193 const bool compatibility_mode_;
1194 int consumer_index_offset_;
1196 std::unique_ptr<ffmpeg_consumer> consumer_;
1197 std::unique_ptr<ffmpeg_consumer> key_only_consumer_;
1201 ffmpeg_consumer_proxy(const std::string& path, const std::string& options, bool separate_key, bool mono_streams, bool compatibility_mode)
1204 , separate_key_(separate_key)
1205 , mono_streams_(mono_streams)
1206 , compatibility_mode_(compatibility_mode)
1207 , consumer_index_offset_(crc16(path))
1211 void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int) override
1214 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
1216 consumer_.reset(new ffmpeg_consumer(path_, options_, mono_streams_));
1217 consumer_->initialize(format_desc, channel_layout);
1221 boost::filesystem::path fill_file(path_);
1222 auto without_extension = u16(fill_file.parent_path().string() + "/" + fill_file.stem().string());
1223 auto key_file = without_extension + L"_A" + u16(fill_file.extension().string());
1225 key_only_consumer_.reset(new ffmpeg_consumer(u8(key_file), options_, mono_streams_));
1226 key_only_consumer_->initialize(format_desc, channel_layout);
1230 int64_t presentation_frame_age_millis() const override
1232 return consumer_ ? static_cast<int64_t>(consumer_->presentation_frame_age_millis()) : 0;
1235 std::future<bool> send(core::const_frame frame) override
1237 bool ready_for_frame = consumer_->ready_for_frame();
1239 if (ready_for_frame && separate_key_)
1240 ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
1242 if (ready_for_frame)
1244 consumer_->send(frame);
1247 key_only_consumer_->send(frame.key_only());
1251 consumer_->mark_dropped();
1254 key_only_consumer_->mark_dropped();
1257 return make_ready_future(true);
1260 std::wstring print() const override
1262 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
1265 std::wstring name() const override
1270 boost::property_tree::wptree info() const override
1272 boost::property_tree::wptree info;
1274 info.add(L"type", L"ffmpeg");
1275 info.add(L"path", u16(path_));
1276 info.add(L"separate_key", separate_key_);
1277 info.add(L"mono_streams", mono_streams_);
1282 bool has_synchronization_clock() const override
1287 int buffer_depth() const override
1292 int index() const override
1294 return compatibility_mode_ ? 200 : 100000 + consumer_index_offset_;
1297 core::monitor::subject& monitor_output() override
1299 return consumer_->monitor_output();
1303 void describe_ffmpeg_consumer(core::help_sink& sink, const core::help_repository& repo)
1305 sink.short_description(L"For streaming/recording the contents of a channel using FFmpeg.");
1306 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]}");
1307 sink.para()->text(L"For recording or streaming the contents of a channel using FFmpeg");
1309 ->item(L"filename", L"The filename under the media folder including the extension (decides which kind of container format that will be used).")
1310 ->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.")
1311 ->item(L"ffmpeg_paramX", L"A parameter supported by FFmpeg. For example vcodec or acodec etc.")
1312 ->item(L"separate_key", L"If defined will create two files simultaneously -- One for fill and one for key (_A will be appended).")
1313 ->item(L"mono_streams", L"If defined every audio channel will be written to its own audio stream.");
1314 sink.para()->text(L"Examples:");
1315 sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd");
1316 sink.example(L">> ADD 1 FILE output.mov -vcodec prores");
1317 sink.example(L">> ADD 1 FILE output.mov -vcodec dvvideo");
1318 sink.example(L">> ADD 1 FILE output.mov -vcodec libx264 -preset ultrafast -tune fastdecode -crf 25");
1319 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");
1320 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.");
1321 sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast",
1322 L"for streaming over UDP instead of creating a local file.");
1325 spl::shared_ptr<core::frame_consumer> create_ffmpeg_consumer(
1326 const std::vector<std::wstring>& params, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1328 if (params.size() < 1 || (!boost::iequals(params.at(0), L"STREAM") && !boost::iequals(params.at(0), L"FILE")))
1329 return core::frame_consumer::empty();
1331 auto params2 = params;
1332 bool separate_key = get_and_consume_flag(L"SEPARATE_KEY", params2);
1333 bool mono_streams = get_and_consume_flag(L"MONO_STREAMS", params2);
1334 auto compatibility_mode = boost::iequals(params.at(0), L"FILE");
1335 auto path = u8(params2.size() > 1 ? params2.at(1) : L"");
1337 // remove FILE or STREAM
1338 params2.erase(params2.begin());
1342 params2.erase(params2.begin());
1344 // join only the args
1345 auto args = u8(boost::join(params2, L" "));
1347 return spl::make_shared<ffmpeg_consumer_proxy>(path, args, separate_key, mono_streams, compatibility_mode);
1350 spl::shared_ptr<core::frame_consumer> create_preconfigured_ffmpeg_consumer(
1351 const boost::property_tree::wptree& ptree, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1353 return spl::make_shared<ffmpeg_consumer_proxy>(
1354 u8(ptree_get<std::wstring>(ptree, L"path")),
1355 u8(ptree.get<std::wstring>(L"args", L"")),
1356 ptree.get<bool>(L"separate-key", false),
1357 ptree.get<bool>(L"mono-streams", false),