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>
51 #pragma warning(disable: 4244)
55 #define __STDC_CONSTANT_MACROS
56 #define __STDC_LIMIT_MACROS
57 #include <libavformat/avformat.h>
58 #include <libavcodec/avcodec.h>
59 #include <libavutil/avutil.h>
60 #include <libavutil/frame.h>
61 #include <libavutil/opt.h>
62 #include <libavutil/imgutils.h>
63 #include <libavutil/parseutils.h>
64 #include <libavfilter/avfilter.h>
65 #include <libavfilter/buffersink.h>
66 #include <libavfilter/buffersrc.h>
71 namespace caspar { namespace ffmpeg {
73 void set_pixel_format(AVFilterContext* sink, AVPixelFormat pix_fmt)
75 #pragma warning (push)
76 #pragma warning (disable : 4245)
78 FF(av_opt_set_int_list(
81 std::vector<AVPixelFormat>({ pix_fmt, AVPixelFormat::AV_PIX_FMT_NONE }).data(),
83 AV_OPT_SEARCH_CHILDREN));
88 void adjust_video_filter(const AVCodec& codec, const core::video_format_desc& in_format, AVFilterContext* sink, std::string& filter)
92 case AV_CODEC_ID_DVVIDEO:
94 if (in_format.format == core::video_format::ntsc)
95 filter = u8(append_filter(u16(filter), L"crop=720:480:0:2"));
97 // Pixel format selection
98 if (in_format.format == core::video_format::ntsc)
99 set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV411P);
100 else if (in_format.format == core::video_format::pal)
101 set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV420P);
103 set_pixel_format(sink, AVPixelFormat::AV_PIX_FMT_YUV422P);
106 if (in_format.height == 1080)
107 filter = u8(append_filter(u16(filter), in_format.duration == 1001
109 : L"scale=1440:1080"));
110 else if (in_format.height == 720)
111 filter = u8(append_filter(u16(filter), L"scale=960:720"));
117 void setup_codec_defaults(AVCodecContext& encoder)
119 static const int MEGABIT = 1000000;
121 switch (encoder.codec_id)
123 case AV_CODEC_ID_DNXHD:
124 encoder.bit_rate = 220 * MEGABIT;
127 case AV_CODEC_ID_PRORES:
128 encoder.bit_rate = encoder.width < 1280
133 case AV_CODEC_ID_H264:
134 av_opt_set(encoder.priv_data, "preset", "ultrafast", 0);
135 av_opt_set(encoder.priv_data, "tune", "fastdecode", 0);
136 av_opt_set(encoder.priv_data, "crf", "5", 0);
142 bool is_pcm_s24le_not_supported(const AVFormatContext& container)
144 auto name = std::string(container.oformat->name);
146 if (name == "mp4" || name == "dv")
152 template<typename Out, typename In>
153 std::vector<Out> from_terminated_array(const In* array, In terminator)
155 std::vector<Out> result;
157 while (array != nullptr && *array != terminator)
160 Out casted = static_cast<Out>(val);
162 result.push_back(casted);
170 class ffmpeg_consumer
173 const spl::shared_ptr<diagnostics::graph> graph_;
174 core::monitor::subject subject_;
176 boost::filesystem::path full_path_;
178 std::map<std::string, std::string> options_;
181 core::video_format_desc in_video_format_;
182 core::audio_channel_layout in_channel_layout_ = core::audio_channel_layout::invalid();
184 std::shared_ptr<AVFormatContext> oc_;
185 tbb::atomic<bool> abort_request_;
187 std::shared_ptr<AVStream> video_st_;
188 std::vector<std::shared_ptr<AVStream>> audio_sts_;
190 std::int64_t video_pts_ = 0;
191 std::int64_t audio_pts_ = 0;
193 std::unique_ptr<audio_filter> audio_filter_;
195 // TODO: make use of already existent avfilter abstraction for video also
196 AVFilterContext* video_graph_in_;
197 AVFilterContext* video_graph_out_;
198 std::shared_ptr<AVFilterGraph> video_graph_;
200 executor video_encoder_executor_;
201 executor audio_encoder_executor_;
203 semaphore tokens_ { 0 };
205 tbb::atomic<int64_t> current_encoding_delay_;
207 executor write_executor_;
217 , mono_streams_(mono_streams)
218 , audio_encoder_executor_(print() + L" audio_encoder")
219 , video_encoder_executor_(print() + L" video_encoder")
220 , write_executor_(print() + L" io")
222 abort_request_ = false;
223 current_encoding_delay_ = 0;
226 boost::sregex_iterator(
229 boost::regex("-(?<NAME>[^-\\s]+)(\\s+(?<VALUE>[^\\s]+))?"));
230 it != boost::sregex_iterator();
233 options_[(*it)["NAME"].str()] = (*it)["VALUE"].matched ? (*it)["VALUE"].str() : "";
236 if (options_.find("threads") == options_.end())
237 options_["threads"] = "auto";
244 boost::regex("tokens")).get_value_or(2)));
253 video_encoder_executor_.begin_invoke([&] { encode_video(core::const_frame::empty(), nullptr); });
254 audio_encoder_executor_.begin_invoke([&] { encode_audio(core::const_frame::empty(), nullptr); });
256 video_encoder_executor_.stop();
257 audio_encoder_executor_.stop();
258 video_encoder_executor_.join();
259 audio_encoder_executor_.join();
261 video_graph_.reset();
262 audio_filter_.reset();
266 write_packet(nullptr, nullptr);
268 write_executor_.stop();
269 write_executor_.join();
271 FF(av_write_trailer(oc_.get()));
273 if (!(oc_->oformat->flags & AVFMT_NOFILE) && oc_->pb)
280 CASPAR_LOG_CURRENT_EXCEPTION();
286 const core::video_format_desc& format_desc,
287 const core::audio_channel_layout& channel_layout)
291 static boost::regex prot_exp("^.+:.*" );
293 if(!boost::regex_match(
297 if(!full_path_.is_complete())
301 env::media_folder()) +
305 if(boost::filesystem::exists(full_path_))
306 boost::filesystem::remove(full_path_);
308 boost::filesystem::create_directories(full_path_.parent_path());
311 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
312 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
313 graph_->set_text(print());
314 diagnostics::register_graph(graph_);
316 const auto oformat_name =
317 try_remove_arg<std::string>(
319 boost::regex("^f|format$"));
323 FF(avformat_alloc_output_context2(
326 oformat_name && !oformat_name->empty() ? oformat_name->c_str() : nullptr,
327 full_path_.string().c_str()));
331 avformat_free_context);
333 CASPAR_VERIFY(oc_->oformat);
335 oc_->interrupt_callback.callback = ffmpeg_consumer::interrupt_cb;
336 oc_->interrupt_callback.opaque = this;
338 CASPAR_VERIFY(format_desc.format != core::video_format::invalid);
340 in_video_format_ = format_desc;
341 in_channel_layout_ = channel_layout;
343 CASPAR_VERIFY(oc_->oformat);
345 const auto video_codec_name =
346 try_remove_arg<std::string>(
348 boost::regex("^c:v|codec:v|vcodec$"));
350 const auto video_codec =
352 ? avcodec_find_encoder_by_name(video_codec_name->c_str())
353 : avcodec_find_encoder(oc_->oformat->video_codec);
355 const auto audio_codec_name =
356 try_remove_arg<std::string>(
358 boost::regex("^c:a|codec:a|acodec$"));
360 const auto audio_codec =
362 ? avcodec_find_encoder_by_name(audio_codec_name->c_str())
363 : (is_pcm_s24le_not_supported(*oc_)
364 ? avcodec_find_encoder(oc_->oformat->audio_codec)
365 : avcodec_find_encoder_by_name("pcm_s24le"));
368 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
369 "Failed to find video codec " + (video_codec_name
371 : "with id " + std::to_string(
372 oc_->oformat->video_codec))));
374 CASPAR_THROW_EXCEPTION(user_error() << msg_info(
375 "Failed to find audio codec " + (audio_codec_name
377 : "with id " + std::to_string(
378 oc_->oformat->audio_codec))));
383 configure_video_filters(
385 try_remove_arg<std::string>(options_, boost::regex("vf|f:v|filter:v"))
387 try_remove_arg<std::string>(options_, boost::regex("pix_fmt")));
389 configure_audio_filters(
391 try_remove_arg<std::string>(options_,
392 boost::regex("af|f:a|filter:a")).get_value_or(""));
398 auto video_options = options_;
399 auto audio_options = options_;
401 video_st_ = open_encoder(
406 for (int i = 0; i < audio_filter_->get_num_output_pads(); ++i)
407 audio_sts_.push_back(open_encoder(
412 auto it = options_.begin();
413 while(it != options_.end())
415 if(video_options.find(it->first) == video_options.end() || audio_options.find(it->first) == audio_options.end())
416 it = options_.erase(it);
424 AVDictionary* av_opts = nullptr;
428 std::move(options_));
432 av_dict_free(&av_opts);
435 if (!(oc_->oformat->flags & AVFMT_NOFILE))
439 full_path_.string().c_str(),
441 &oc_->interrupt_callback,
445 FF(avformat_write_header(
449 options_ = to_map(av_opts);
460 for (const auto& option : options_)
463 << L"Invalid option: -"
466 << u16(option.second);
478 core::monitor::subject& monitor_output()
483 void send(core::const_frame frame)
485 CASPAR_VERIFY(in_video_format_.format != core::video_format::invalid);
487 auto frame_timer = spl::make_shared<caspar::timer>();
489 std::shared_ptr<void> token(
491 [this, frame, frame_timer](void*)
494 current_encoding_delay_ = frame.get_age_millis();
495 graph_->set_value("frame-time", frame_timer->elapsed() * in_video_format_.fps * 0.5);
499 video_encoder_executor_.begin_invoke([=]() mutable
506 audio_encoder_executor_.begin_invoke([=]() mutable
514 bool ready_for_frame() const
516 return tokens_.permits() > 0;
521 graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
524 std::wstring print() const
526 return L"ffmpeg_consumer[" + u16(path_) + L"]";
529 int64_t presentation_frame_age_millis() const
531 return current_encoding_delay_;
536 static int interrupt_cb(void* ctx)
539 return reinterpret_cast<ffmpeg_consumer*>(ctx)->abort_request_;
542 std::shared_ptr<AVStream> open_encoder(
543 const AVCodec& codec,
544 std::map<std::string,
545 std::string>& options,
546 int stream_number_for_media_type)
554 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("avformat_new_stream"));
556 auto enc = st->codec;
560 switch(enc->codec_type)
562 case AVMEDIA_TYPE_VIDEO:
564 enc->time_base = video_graph_out_->inputs[0]->time_base;
565 enc->pix_fmt = static_cast<AVPixelFormat>(video_graph_out_->inputs[0]->format);
566 enc->sample_aspect_ratio = st->sample_aspect_ratio = video_graph_out_->inputs[0]->sample_aspect_ratio;
567 enc->width = video_graph_out_->inputs[0]->w;
568 enc->height = video_graph_out_->inputs[0]->h;
569 enc->bit_rate_tolerance = 400 * 1000000;
573 case AVMEDIA_TYPE_AUDIO:
575 enc->time_base = audio_filter_->get_output_pad_info(stream_number_for_media_type).time_base;
576 enc->sample_fmt = static_cast<AVSampleFormat>(audio_filter_->get_output_pad_info(stream_number_for_media_type).format);
577 enc->sample_rate = audio_filter_->get_output_pad_info(stream_number_for_media_type).sample_rate;
578 enc->channel_layout = audio_filter_->get_output_pad_info(stream_number_for_media_type).channel_layout;
579 enc->channels = audio_filter_->get_output_pad_info(stream_number_for_media_type).channels;
585 setup_codec_defaults(*enc);
587 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
588 enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
590 static const std::array<std::string, 4> char_id_map = {{"v", "a", "d", "s"}};
592 const auto char_id = char_id_map.at(enc->codec_type);
594 const auto codec_opts =
597 boost::regex("^(" + char_id + "?[^:]+):" + char_id + "$"));
599 AVDictionary* av_codec_opts = nullptr;
614 av_codec_opts ? &av_codec_opts : nullptr));
623 AV_DICT_IGNORE_SUFFIX);
627 options[t->key + (codec_opts.find(t->key) != codec_opts.end() ? ":" + char_id : "")] = t->value;
633 AV_DICT_IGNORE_SUFFIX);
636 av_dict_free(&av_codec_opts);
639 if(enc->codec_type == AVMEDIA_TYPE_AUDIO && !(codec.capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
641 CASPAR_ASSERT(enc->frame_size > 0);
642 audio_filter_->set_guaranteed_output_num_samples_per_frame(
643 stream_number_for_media_type,
647 return std::shared_ptr<AVStream>(st, [this](AVStream* st)
649 avcodec_close(st->codec);
653 void configure_video_filters(
654 const AVCodec& codec,
655 std::string filtergraph,
656 const boost::optional<std::string>& preferred_pix_fmt)
659 avfilter_graph_alloc(),
662 avfilter_graph_free(&p);
665 video_graph_->nb_threads = std::thread::hardware_concurrency()/2;
666 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
668 const auto sample_aspect_ratio =
669 boost::rational<int>(
670 in_video_format_.square_width,
671 in_video_format_.square_height) /
672 boost::rational<int>(
673 in_video_format_.width,
674 in_video_format_.height);
676 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%")
677 % in_video_format_.width % in_video_format_.height
678 % AVPixelFormat::AV_PIX_FMT_BGRA
679 % in_video_format_.duration % in_video_format_.time_scale
680 % sample_aspect_ratio.numerator() % sample_aspect_ratio.denominator()
681 % in_video_format_.time_scale % in_video_format_.duration).str();
683 AVFilterContext* filt_vsrc = nullptr;
684 FF(avfilter_graph_create_filter(
686 avfilter_get_by_name("buffer"),
687 "ffmpeg_consumer_buffer",
688 vsrc_options.c_str(),
690 video_graph_.get()));
692 AVFilterContext* filt_vsink = nullptr;
693 FF(avfilter_graph_create_filter(
695 avfilter_get_by_name("buffersink"),
696 "ffmpeg_consumer_buffersink",
699 video_graph_.get()));
701 #pragma warning (push)
702 #pragma warning (disable : 4245)
704 FF(av_opt_set_int_list(
709 AV_OPT_SEARCH_CHILDREN));
712 #pragma warning (pop)
714 adjust_video_filter(codec, in_video_format_, filt_vsink, filtergraph);
716 if (preferred_pix_fmt)
718 auto requested_fmt = av_get_pix_fmt(preferred_pix_fmt->c_str());
719 auto valid_fmts = from_terminated_array<AVPixelFormat>(codec.pix_fmts, AVPixelFormat::AV_PIX_FMT_NONE);
721 if (!cpplinq::from(valid_fmts).contains(requested_fmt))
722 CASPAR_THROW_EXCEPTION(user_error() << msg_info(*preferred_pix_fmt + " is not supported by codec."));
724 set_pixel_format(filt_vsink, requested_fmt);
727 if (in_video_format_.width < 1280)
728 video_graph_->scale_sws_opts = "out_color_matrix=bt601";
730 video_graph_->scale_sws_opts = "out_color_matrix=bt709";
732 configure_filtergraph(
738 video_graph_in_ = filt_vsrc;
739 video_graph_out_ = filt_vsink;
742 << u16(std::string("\n")
743 + avfilter_graph_dump(
748 void configure_audio_filters(
749 const AVCodec& codec,
750 std::string filtergraph)
752 int num_output_pads = 1;
756 num_output_pads = in_channel_layout_.num_channels;
759 if (num_output_pads > 1)
761 std::string splitfilter = "[a:0]channelsplit=channel_layout=";
763 splitfilter += (boost::format("0x%|1$x|") % create_channel_layout_bitmask(in_channel_layout_.num_channels)).str();
765 for (int i = 0; i < num_output_pads; ++i)
766 splitfilter += "[aout:" + std::to_string(i) + "]";
768 filtergraph = u8(append_filter(u16(filtergraph), u16(splitfilter)));
771 std::vector<audio_output_pad> output_pads(
774 from_terminated_array<int>( codec.supported_samplerates, 0),
775 from_terminated_array<AVSampleFormat>( codec.sample_fmts, AVSampleFormat::AV_SAMPLE_FMT_NONE),
776 from_terminated_array<uint64_t>( codec.channel_layouts, static_cast<uint64_t>(0))));
778 audio_filter_.reset(new audio_filter(
780 boost::rational<int>(1, in_video_format_.audio_sample_rate),
781 in_video_format_.audio_sample_rate,
782 AVSampleFormat::AV_SAMPLE_FMT_S32,
783 create_channel_layout_bitmask(in_channel_layout_.num_channels)) },
788 void configure_filtergraph(
789 AVFilterGraph& graph,
790 const std::string& filtergraph,
791 AVFilterContext& source_ctx,
792 AVFilterContext& sink_ctx)
794 AVFilterInOut* outputs = nullptr;
795 AVFilterInOut* inputs = nullptr;
797 if(!filtergraph.empty())
799 outputs = avfilter_inout_alloc();
800 inputs = avfilter_inout_alloc();
804 CASPAR_VERIFY(outputs && inputs);
806 outputs->name = av_strdup("in");
807 outputs->filter_ctx = &source_ctx;
808 outputs->pad_idx = 0;
809 outputs->next = nullptr;
811 inputs->name = av_strdup("out");
812 inputs->filter_ctx = &sink_ctx;
814 inputs->next = nullptr;
818 avfilter_inout_free(&outputs);
819 avfilter_inout_free(&inputs);
823 FF(avfilter_graph_parse(
839 FF(avfilter_graph_config(
844 void encode_video(core::const_frame frame_ptr, std::shared_ptr<void> token)
849 auto enc = video_st_->codec;
851 if(frame_ptr != core::const_frame::empty())
853 auto src_av_frame = create_frame();
855 const auto sample_aspect_ratio =
856 boost::rational<int>(
857 in_video_format_.square_width,
858 in_video_format_.square_height) /
859 boost::rational<int>(
860 in_video_format_.width,
861 in_video_format_.height);
863 src_av_frame->format = AVPixelFormat::AV_PIX_FMT_BGRA;
864 src_av_frame->width = in_video_format_.width;
865 src_av_frame->height = in_video_format_.height;
866 src_av_frame->sample_aspect_ratio.num = sample_aspect_ratio.numerator();
867 src_av_frame->sample_aspect_ratio.den = sample_aspect_ratio.denominator();
868 src_av_frame->pts = video_pts_;
869 src_av_frame->interlaced_frame = in_video_format_.field_mode != core::field_mode::progressive;
870 src_av_frame->top_field_first = (in_video_format_.field_mode & core::field_mode::upper) == core::field_mode::upper ? 1 : 0;
875 << core::monitor::message("/frame") % video_pts_
876 << core::monitor::message("/path") % path_
877 << core::monitor::message("/fps") % in_video_format_.fps;
879 FF(av_image_fill_arrays(
881 src_av_frame->linesize,
882 frame_ptr.image_data().begin(),
883 static_cast<AVPixelFormat>(src_av_frame->format),
884 in_video_format_.width,
885 in_video_format_.height,
888 FF(av_buffersrc_add_frame(
890 src_av_frame.get()));
897 auto filt_frame = create_frame();
899 ret = av_buffersink_get_frame(
903 video_encoder_executor_.begin_invoke([=]
905 if(ret == AVERROR_EOF)
907 if(enc->codec->capabilities & CODEC_CAP_DELAY)
909 while(encode_av_frame(
911 avcodec_encode_video2,
914 boost::this_thread::yield(); // TODO:
918 else if(ret != AVERROR(EAGAIN))
920 FF_RET(ret, "av_buffersink_get_frame");
922 if (filt_frame->interlaced_frame)
924 if (enc->codec->id == AV_CODEC_ID_MJPEG)
925 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
927 enc->field_order = filt_frame->top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
930 enc->field_order = AV_FIELD_PROGRESSIVE;
932 filt_frame->quality = enc->global_quality;
934 if (!enc->me_threshold)
935 filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
939 avcodec_encode_video2,
943 boost::this_thread::yield(); // TODO:
949 void encode_audio(core::const_frame frame_ptr, std::shared_ptr<void> token)
951 if(audio_sts_.empty())
954 if(frame_ptr != core::const_frame::empty())
956 auto src_av_frame = create_frame();
958 src_av_frame->channels = in_channel_layout_.num_channels;
959 src_av_frame->channel_layout = create_channel_layout_bitmask(in_channel_layout_.num_channels);
960 src_av_frame->sample_rate = in_video_format_.audio_sample_rate;
961 src_av_frame->nb_samples = static_cast<int>(frame_ptr.audio_data().size()) / src_av_frame->channels;
962 src_av_frame->format = AV_SAMPLE_FMT_S32;
963 src_av_frame->pts = audio_pts_;
965 audio_pts_ += src_av_frame->nb_samples;
967 FF(av_samples_fill_arrays(
968 src_av_frame->extended_data,
969 src_av_frame->linesize,
970 reinterpret_cast<const std::uint8_t*>(&*frame_ptr.audio_data().begin()),
971 src_av_frame->channels,
972 src_av_frame->nb_samples,
973 static_cast<AVSampleFormat>(src_av_frame->format),
976 audio_filter_->push(0, src_av_frame);
979 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
981 for (auto filt_frame : audio_filter_->poll_all(pad_id))
983 audio_encoder_executor_.begin_invoke([=]
986 *audio_sts_.at(pad_id),
987 avcodec_encode_audio2,
991 std::this_thread::yield(); // TODO:
996 bool eof = frame_ptr == core::const_frame::empty();
1000 audio_encoder_executor_.begin_invoke([=]
1002 for (int pad_id = 0; pad_id < audio_filter_->get_num_output_pads(); ++pad_id)
1004 auto enc = audio_sts_.at(pad_id)->codec;
1006 if (enc->codec->capabilities & CODEC_CAP_DELAY)
1008 while (encode_av_frame(
1009 *audio_sts_.at(pad_id),
1010 avcodec_encode_audio2,
1014 std::this_thread::yield(); // TODO:
1022 template<typename F>
1023 bool encode_av_frame(
1026 const std::shared_ptr<AVFrame>& src_av_frame,
1027 std::shared_ptr<void> token)
1030 av_init_packet(&pkt);
1040 if(!got_packet || pkt.size <= 0)
1043 pkt.stream_index = st.index;
1045 if (pkt.pts != AV_NOPTS_VALUE)
1050 st.codec->time_base,
1054 if (pkt.dts != AV_NOPTS_VALUE)
1059 st.codec->time_base,
1067 st.codec->time_base, st.time_base));
1070 std::shared_ptr<AVPacket>(
1082 const std::shared_ptr<AVPacket>& pkt_ptr,
1083 std::shared_ptr<void> token)
1085 write_executor_.begin_invoke([this, pkt_ptr, token]() mutable
1087 FF(av_interleaved_write_frame(
1093 template<typename T>
1094 static boost::optional<T> try_remove_arg(
1095 std::map<std::string, std::string>& options,
1096 const boost::regex& expr)
1098 for(auto it = options.begin(); it != options.end(); ++it)
1100 if(boost::regex_search(it->first, expr))
1102 auto arg = it->second;
1104 return boost::lexical_cast<T>(arg);
1108 return boost::optional<T>();
1111 static std::map<std::string, std::string> remove_options(
1112 std::map<std::string, std::string>& options,
1113 const boost::regex& expr)
1115 std::map<std::string, std::string> result;
1117 auto it = options.begin();
1118 while(it != options.end())
1121 if(boost::regex_search(it->first, what, expr))
1124 what.size() > 0 && what[1].matched
1126 : it->first] = it->second;
1127 it = options.erase(it);
1136 static void to_dict(AVDictionary** dest, const std::map<std::string, std::string>& c)
1138 for (const auto& entry : c)
1142 entry.first.c_str(),
1143 entry.second.c_str(), 0);
1147 static std::map<std::string, std::string> to_map(AVDictionary* dict)
1149 std::map<std::string, std::string> result;
1156 AV_DICT_IGNORE_SUFFIX)
1163 AV_DICT_IGNORE_SUFFIX))
1165 result[t->key] = t->value;
1172 int crc16(const std::string& str)
1174 boost::crc_16_type result;
1176 result.process_bytes(str.data(), str.length());
1178 return result.checksum();
1181 struct ffmpeg_consumer_proxy : public core::frame_consumer
1183 const std::string path_;
1184 const std::string options_;
1185 const bool separate_key_;
1186 const bool mono_streams_;
1187 const bool compatibility_mode_;
1188 int consumer_index_offset_;
1190 std::unique_ptr<ffmpeg_consumer> consumer_;
1191 std::unique_ptr<ffmpeg_consumer> key_only_consumer_;
1195 ffmpeg_consumer_proxy(const std::string& path, const std::string& options, bool separate_key, bool mono_streams, bool compatibility_mode)
1198 , separate_key_(separate_key)
1199 , mono_streams_(mono_streams)
1200 , compatibility_mode_(compatibility_mode)
1201 , consumer_index_offset_(crc16(path))
1205 void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int) override
1208 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
1210 consumer_.reset(new ffmpeg_consumer(path_, options_, mono_streams_));
1211 consumer_->initialize(format_desc, channel_layout);
1215 boost::filesystem::path fill_file(path_);
1216 auto without_extension = u16(fill_file.parent_path().string() + "/" + fill_file.stem().string());
1217 auto key_file = without_extension + L"_A" + u16(fill_file.extension().string());
1219 key_only_consumer_.reset(new ffmpeg_consumer(u8(key_file), options_, mono_streams_));
1220 key_only_consumer_->initialize(format_desc, channel_layout);
1224 int64_t presentation_frame_age_millis() const override
1226 return consumer_ ? static_cast<int64_t>(consumer_->presentation_frame_age_millis()) : 0;
1229 std::future<bool> send(core::const_frame frame) override
1231 bool ready_for_frame = consumer_->ready_for_frame();
1233 if (ready_for_frame && separate_key_)
1234 ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
1236 if (ready_for_frame)
1238 consumer_->send(frame);
1241 key_only_consumer_->send(frame.key_only());
1245 consumer_->mark_dropped();
1248 key_only_consumer_->mark_dropped();
1251 return make_ready_future(true);
1254 std::wstring print() const override
1256 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
1259 std::wstring name() const override
1264 boost::property_tree::wptree info() const override
1266 boost::property_tree::wptree info;
1268 info.add(L"type", L"ffmpeg");
1269 info.add(L"path", u16(path_));
1270 info.add(L"separate_key", separate_key_);
1271 info.add(L"mono_streams", mono_streams_);
1276 bool has_synchronization_clock() const override
1281 int buffer_depth() const override
1286 int index() const override
1288 return compatibility_mode_ ? 200 : 100000 + consumer_index_offset_;
1291 core::monitor::subject& monitor_output() override
1293 return consumer_->monitor_output();
1297 void describe_ffmpeg_consumer(core::help_sink& sink, const core::help_repository& repo)
1299 sink.short_description(L"For streaming/recording the contents of a channel using FFmpeg.");
1300 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]}");
1301 sink.para()->text(L"For recording or streaming the contents of a channel using FFmpeg");
1303 ->item(L"filename", L"The filename under the media folder including the extension (decides which kind of container format that will be used).")
1304 ->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.")
1305 ->item(L"ffmpeg_paramX", L"A parameter supported by FFmpeg. For example vcodec or acodec etc.")
1306 ->item(L"separate_key", L"If defined will create two files simultaneously -- One for fill and one for key (_A will be appended).")
1307 ->item(L"mono_streams", L"If defined every audio channel will be written to its own audio stream.");
1308 sink.para()->text(L"Examples:");
1309 sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd");
1310 sink.example(L">> ADD 1 FILE output.mov -vcodec prores");
1311 sink.example(L">> ADD 1 FILE output.mov -vcodec dvvideo");
1312 sink.example(L">> ADD 1 FILE output.mov -vcodec libx264 -preset ultrafast -tune fastdecode -crf 25");
1313 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");
1314 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.");
1315 sink.example(L">> ADD 1 STREAM udp://<client_ip_address>:9250 -format mpegts -vcodec libx264 -crf 25 -tune zerolatency -preset ultrafast",
1316 L"for streaming over UDP instead of creating a local file.");
1319 spl::shared_ptr<core::frame_consumer> create_ffmpeg_consumer(
1320 const std::vector<std::wstring>& params, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1322 if (params.size() < 1 || (!boost::iequals(params.at(0), L"STREAM") && !boost::iequals(params.at(0), L"FILE")))
1323 return core::frame_consumer::empty();
1325 auto params2 = params;
1326 bool separate_key = get_and_consume_flag(L"SEPARATE_KEY", params2);
1327 bool mono_streams = get_and_consume_flag(L"MONO_STREAMS", params2);
1328 auto compatibility_mode = boost::iequals(params.at(0), L"FILE");
1329 auto path = u8(params2.size() > 1 ? params2.at(1) : L"");
1331 // remove FILE or STREAM
1332 params2.erase(params2.begin());
1336 params2.erase(params2.begin());
1338 // join only the args
1339 auto args = u8(boost::join(params2, L" "));
1341 return spl::make_shared<ffmpeg_consumer_proxy>(path, args, separate_key, mono_streams, compatibility_mode);
1344 spl::shared_ptr<core::frame_consumer> create_preconfigured_ffmpeg_consumer(
1345 const boost::property_tree::wptree& ptree, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
1347 return spl::make_shared<ffmpeg_consumer_proxy>(
1348 u8(ptree_get<std::wstring>(ptree, L"path")),
1349 u8(ptree.get<std::wstring>(L"args", L"")),
1350 ptree.get<bool>(L"separate-key", false),
1351 ptree.get<bool>(L"mono-streams", false),