#include "../producer/tbb_avcodec.h"
#include <core/frame/frame.h>
+#include <core/frame/audio_channel_layout.h>
#include <core/mixer/audio/audio_util.h>
#include <core/consumer/frame_consumer.h>
#include <core/video_format.h>
+#include <core/help/help_repository.h>
+#include <core/help/help_sink.h>
#include <common/array.h>
#include <common/env.h>
#include <common/except.h>
#include <common/executor.h>
+#include <common/future.h>
#include <common/diagnostics/graph.h>
#include <common/lock.h>
#include <common/memory.h>
#include <common/param.h>
#include <common/utf.h>
#include <common/assert.h>
+#include <common/memshfl.h>
+#include <common/timer.h>
+#include <common/ptree.h>
#include <boost/algorithm/string.hpp>
-#include <boost/timer.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/filesystem.hpp>
#include <boost/range/algorithm.hpp>
#include <tbb/spin_mutex.h>
#include <numeric>
+#include <cstring>
#if defined(_MSC_VER)
#pragma warning (push)
AVOutputFormat* format;
int width;
int height;
- CodecID vcodec;
- CodecID acodec;
+ AVCodecID vcodec;
+ AVCodecID acodec;
int croptop;
int cropbot;
format = av_guess_format(value.c_str(), nullptr, nullptr);
if(format == nullptr)
- CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("f"));
+ CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown format " + value));
return true;
}
{
auto c = avcodec_find_encoder_by_name(value.c_str());
if(c == nullptr)
- CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("vcodec"));
+ CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown video codec " + value));
vcodec = avcodec_find_encoder_by_name(value.c_str())->id;
return true;
{
auto c = avcodec_find_encoder_by_name(value.c_str());
if(c == nullptr)
- CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("acodec"));
+ CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown audio codec " + value));
acodec = avcodec_find_encoder_by_name(value.c_str())->id;
else if(name == "s")
{
if(av_parse_video_size(&width, &height, value.c_str()) < 0)
- CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("s"));
+ CASPAR_THROW_EXCEPTION(user_error() << msg_info("Unknown video size " + value));
return true;
}
}
};
-typedef std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> byte_vector;
+typedef cache_aligned_vector<uint8_t> byte_vector;
struct ffmpeg_consumer : boost::noncopyable
{
const spl::shared_ptr<diagnostics::graph> graph_;
- const std::string filename_;
- const std::shared_ptr<AVFormatContext> oc_;
- const core::video_format_desc format_desc_;
+ const std::string filename_;
+ const std::string full_filename_ = u8(env::media_folder()) + filename_;
+ const std::shared_ptr<AVFormatContext> oc_ { avformat_alloc_context(), avformat_free_context };
+ const core::video_format_desc format_desc_;
+ const core::audio_channel_layout channel_layout_;
- monitor::basic_subject event_subject_;
+ core::monitor::subject monitor_subject_;
tbb::spin_mutex exception_mutex_;
std::exception_ptr exception_;
std::shared_ptr<AVStream> video_st_;
byte_vector picture_buffer_;
+ byte_vector key_picture_buf_;
byte_vector audio_buffer_;
std::shared_ptr<SwrContext> swr_;
std::shared_ptr<SwsContext> sws_;
- int64_t frame_number_;
+ int64_t frame_number_ = 0;
output_format output_format_;
-
+ bool key_only_;
+ tbb::atomic<int64_t> current_encoding_delay_;
+
executor executor_;
public:
- ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, std::vector<option> options)
+ ffmpeg_consumer(
+ const std::string& filename,
+ const core::video_format_desc& format_desc,
+ const core::audio_channel_layout& channel_layout,
+ std::vector<option> options,
+ bool key_only)
: filename_(filename)
- , oc_(avformat_alloc_context(), av_free)
, format_desc_(format_desc)
- , frame_number_(0)
- , output_format_(format_desc, filename, options)
+ , channel_layout_(channel_layout)
+ , output_format_(format_desc, full_filename_, options)
+ , key_only_(key_only)
, executor_(print())
{
+ current_encoding_delay_ = 0;
check_space();
// TODO: Ask stakeholders about case where file already exists.
- boost::filesystem::remove(boost::filesystem::path(env::media_folder() + u16(filename))); // Delete the file if it exists
+ boost::filesystem::remove(boost::filesystem::path(full_filename_)); // Delete the file if it exists
graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
+ graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
graph_->set_text(print());
diagnostics::register_graph(graph_);
oc_->oformat = output_format_.format;
- strcpy_s(oc_->filename, filename_.c_str());
+ std::strcpy(oc_->filename, full_filename_.c_str());
// Add the audio and video streams using the default format codecs and initialize the codecs.
video_st_ = add_video_stream(options);
- audio_st_ = add_audio_stream(options);
+
+ if (!key_only)
+ audio_st_ = add_audio_stream(options);
- av_dump_format(oc_.get(), 0, filename_.c_str(), 1);
+ av_dump_format(oc_.get(), 0, full_filename_.c_str(), 1);
// Open the output ffmpeg, if needed.
if (!(oc_->oformat->flags & AVFMT_NOFILE))
- THROW_ON_ERROR2(avio_open(&oc_->pb, filename.c_str(), AVIO_FLAG_WRITE), "[ffmpeg_consumer]");
+ THROW_ON_ERROR2(avio_open(&oc_->pb, full_filename_.c_str(), AVIO_FLAG_WRITE), "[ffmpeg_consumer]");
THROW_ON_ERROR2(avformat_write_header(oc_.get(), nullptr), "[ffmpeg_consumer]");
if(options.size() > 0)
{
- BOOST_FOREACH(auto& option, options)
+ for (auto& option : options)
CASPAR_LOG(warning) << L"Invalid option: -" << u16(option.name) << L" " << u16(option.value);
}
-
- CASPAR_LOG(info) << print() << L" Successfully Initialized.";
}
~ffmpeg_consumer()
LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");
- audio_st_.reset();
+ if (!key_only_)
+ audio_st_.reset();
+
video_st_.reset();
if (!(oc_->oformat->flags & AVFMT_NOFILE))
LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]");
-
- CASPAR_LOG(info) << print() << L" Successfully Uninitialized.";
}
// frame_consumer
- bool send(core::const_frame& frame)
+ void send(core::const_frame& frame)
{
auto exception = lock(exception_mutex_, [&]
{
if(exception != nullptr)
std::rethrow_exception(exception);
-
+
executor_.begin_invoke([=]
{
encode(frame);
+ current_encoding_delay_ = frame.get_age_millis();
});
-
- return true;
+ }
+
+ bool ready_for_frame() const
+ {
+ return !executor_.is_full();
+ }
+
+ void mark_dropped()
+ {
+ graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
}
std::wstring print() const
return L"ffmpeg[" + u16(filename_) + L"]";
}
- void subscribe(const monitor::observable::observer_ptr& o)
+ core::monitor::subject& monitor_output()
{
- event_subject_.subscribe(o);
+ return monitor_subject_;
}
- void unsubscribe(const monitor::observable::observer_ptr& o)
- {
- event_subject_.unsubscribe(o);
- }
-
private:
std::shared_ptr<AVStream> add_video_stream(std::vector<option>& options)
{
if(output_format_.vcodec == CODEC_ID_NONE)
return nullptr;
- auto st = av_new_stream(oc_.get(), 0);
+ auto st = avformat_new_stream(oc_.get(), 0);
if (!st)
CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
if(output_format_.format->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
- THROW_ON_ERROR2(tbb_avcodec_open(c, encoder), "[ffmpeg_consumer]");
+ THROW_ON_ERROR2(tbb_avcodec_open(c, encoder, false), "[ffmpeg_consumer]");
return std::shared_ptr<AVStream>(st, [](AVStream* st)
{
LOG_ON_ERROR2(tbb_avcodec_close(st->codec), "[ffmpeg_consumer]");
- av_freep(&st->codec);
- av_freep(&st);
});
}
if(output_format_.acodec == CODEC_ID_NONE)
return nullptr;
- auto st = av_new_stream(oc_.get(), 1);
+ auto st = avformat_new_stream(oc_.get(), nullptr);
if(!st)
CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));
return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
});
- THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");
+ THROW_ON_ERROR2(avcodec_open2(c, encoder, nullptr), "[ffmpeg_consumer]");
return std::shared_ptr<AVStream>(st, [](AVStream* st)
{
- LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");;
- av_freep(&st->codec);
- av_freep(&st);
+ LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");
});
}
auto av_frame = convert_video(frame, enc);
av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper;
- av_frame->pts = frame_number_++;
+ av_frame->pts = frame_number_++;
- event_subject_ << monitor::event("frame") % static_cast<int64_t>(frame_number_)
- % static_cast<int64_t>(std::numeric_limits<int64_t>::max());
+ monitor_subject_
+ << core::monitor::message("/frame") % static_cast<int64_t>(frame_number_)
+ << core::monitor::message("/path") % filename_
+ << core::monitor::message("/fps") % format_desc_.fps;
AVPacket pkt;
av_init_packet(&pkt);
while(audio_buffer_.size() >= frame_size)
{
- std::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
+ std::shared_ptr<AVFrame> av_frame(av_frame_alloc(), [=](AVFrame* p) { av_frame_free(&p); });
avcodec_get_frame_defaults(av_frame.get());
av_frame->nb_samples = frame_size / (enc->channels * av_get_bytes_per_sample(enc->sample_fmt));
std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);
- avpicture_fill(reinterpret_cast<AVPicture*>(in_frame.get()),
- const_cast<uint8_t*>(frame.image_data().begin()),
- PIX_FMT_BGRA,
- format_desc_.width,
- format_desc_.height - output_format_.croptop - output_format_.cropbot);
+ auto in_picture = reinterpret_cast<AVPicture*>(in_frame.get());
+
+ if (key_only_)
+ {
+ key_picture_buf_.resize(frame.image_data().size());
+ in_picture->linesize[0] = format_desc_.width * 4;
+ in_picture->data[0] = key_picture_buf_.data();
+
+ aligned_memshfl(in_picture->data[0], frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
+ }
+ else
+ {
+ avpicture_fill(
+ in_picture,
+ const_cast<uint8_t*>(frame.image_data().begin()),
+ PIX_FMT_BGRA,
+ format_desc_.width,
+ format_desc_.height - output_format_.croptop - output_format_.cropbot);
+ }
+
// crop-top
for(int n = 0; n < 4; ++n)
out_frame->data,
out_frame->linesize);
+ out_frame->format = c->pix_fmt;
+ out_frame->width = c->width;
+ out_frame->height = c->height;
+
return out_frame;
}
{
swr_ = std::shared_ptr<SwrContext>(swr_alloc_set_opts(nullptr,
get_channel_layout(c), c->sample_fmt, c->sample_rate,
- av_get_default_channel_layout(format_desc_.audio_channels), AV_SAMPLE_FMT_S32, format_desc_.audio_sample_rate,
+ av_get_default_channel_layout(channel_layout_.num_channels), AV_SAMPLE_FMT_S32, format_desc_.audio_sample_rate,
0, nullptr), [](SwrContext* p){swr_free(&p);});
if(!swr_)
auto channel_samples = swr_convert(swr_.get(),
out, static_cast<int>(buffer.size()) / c->channels / av_get_bytes_per_sample(c->sample_fmt),
- in, static_cast<int>(frame.audio_data().size()/format_desc_.audio_channels));
+ in, static_cast<int>(frame.audio_data().size()/channel_layout_.num_channels));
buffer.resize(channel_samples * c->channels * av_get_bytes_per_sample(c->sample_fmt));
void check_space()
{
- auto space = boost::filesystem::space(boost::filesystem::path(filename_).parent_path());
+ auto space = boost::filesystem::space(boost::filesystem::path(full_filename_).parent_path());
if(space.available < 512*1000000)
- BOOST_THROW_EXCEPTION(file_write_error() << msg_info("out of space"));
+ CASPAR_THROW_EXCEPTION(file_write_error() << msg_info("out of space"));
}
void encode(const core::const_frame& frame)
if(frame_number_ % 25 == 0)
check_space();
- boost::timer frame_timer;
+ caspar::timer frame_timer;
encode_video_frame(frame);
encode_audio_frame(frame);
struct ffmpeg_consumer_proxy : public core::frame_consumer
{
- const std::wstring filename_;
- const std::vector<option> options_;
+ const std::wstring filename_;
+ const std::vector<option> options_;
+ const bool separate_key_;
std::unique_ptr<ffmpeg_consumer> consumer_;
+ std::unique_ptr<ffmpeg_consumer> key_only_consumer_;
public:
- ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options)
+ ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options, bool separate_key)
: filename_(filename)
, options_(options)
+ , separate_key_(separate_key)
{
}
- virtual void initialize(const core::video_format_desc& format_desc, int)
+ void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int) override
{
if(consumer_)
- BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
+ CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
+
+ consumer_.reset(new ffmpeg_consumer(u8(filename_), format_desc, channel_layout, options_, false));
- consumer_.reset(new ffmpeg_consumer(u8(filename_), format_desc, options_));
+ if (separate_key_)
+ {
+ boost::filesystem::path fill_file(filename_);
+ auto without_extension = u16(fill_file.stem().string());
+ auto key_file = without_extension + L"_A" + u16(fill_file.extension().string());
+
+ key_only_consumer_.reset(new ffmpeg_consumer(u8(key_file), format_desc, channel_layout, options_, true));
+ }
}
-
- bool send(core::const_frame frame) override
+
+ int64_t presentation_frame_age_millis() const override
{
- return consumer_->send(frame);
+ return consumer_ ? static_cast<int64_t>(consumer_->current_encoding_delay_) : 0;
+ }
+
+ std::future<bool> send(core::const_frame frame) override
+ {
+ bool ready_for_frame = consumer_->ready_for_frame();
+
+ if (ready_for_frame && separate_key_)
+ ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
+
+ if (ready_for_frame)
+ {
+ consumer_->send(frame);
+
+ if (separate_key_)
+ key_only_consumer_->send(frame);
+ }
+ else
+ {
+ consumer_->mark_dropped();
+
+ if (separate_key_)
+ key_only_consumer_->mark_dropped();
+ }
+
+ return make_ready_future(true);
}
std::wstring print() const override
boost::property_tree::wptree info;
info.add(L"type", L"file");
info.add(L"filename", filename_);
+ info.add(L"separate_key", separate_key_);
return info;
}
int buffer_depth() const override
{
- return 1;
+ return -1;
}
int index() const override
return 200;
}
- void subscribe(const monitor::observable::observer_ptr& o) override
+ core::monitor::subject& monitor_output()
{
- consumer_->subscribe(o);
+ return consumer_->monitor_output();
}
+};
- void unsubscribe(const monitor::observable::observer_ptr& o) override
- {
- consumer_->unsubscribe(o);
- }
-};
-spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
+void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
{
- auto str = std::accumulate(params.begin(), params.end(), std::wstring(), [](const std::wstring& lhs, const std::wstring& rhs) {return lhs + L" " + rhs;});
+ sink.short_description(L"Can record a channel to a file supported by FFmpeg.");
+ sink.syntax(L"FILE [filename:string] {-[ffmpeg_param1:string] [value1:string] {-[ffmpeg_param2:string] [value2:string] {...}}} {[separate_key:SEPARATE_KEY]}");
+ sink.para()->text(L"Can record a channel to a file supported by FFmpeg.");
+ sink.definitions()
+ ->item(L"filename", L"The filename under the media folder including the extension (decides which kind of container format that will be used).")
+ ->item(L"ffmpeg_paramX", L"A parameter supported by FFmpeg. For example vcodec or acodec etc.")
+ ->item(L"separate_key", L"If defined will create two files simultaneously -- One for fill and one for key (_A will be appended).")
+ ;
+ sink.para()->text(L"Examples:");
+ sink.example(L">> ADD 1 FILE output.mov -vcodec dnxhd");
+ sink.example(L">> ADD 1 FILE output.mov -vcodec prores");
+ sink.example(L">> ADD 1 FILE output.mov -vcodec dvvideo");
+ sink.example(L">> ADD 1 FILE output.mov -vcodec libx264 -preset ultrafast -tune fastdecode -crf 25");
+ 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");
+}
+
+spl::shared_ptr<core::frame_consumer> create_consumer(
+ const std::vector<std::wstring>& params, core::interaction_sink*)
+{
+ auto params2 = params;
+ auto separate_key_it = std::find_if(params2.begin(), params2.end(), param_comparer(L"SEPARATE_KEY"));
+ bool separate_key = false;
+
+ if (separate_key_it != params2.end())
+ {
+ separate_key = true;
+ params2.erase(separate_key_it);
+ }
+
+ auto str = std::accumulate(params2.begin(), params2.end(), std::wstring(), [](const std::wstring& lhs, const std::wstring& rhs) {return lhs + L" " + rhs;});
- boost::wregex path_exp(L"\\s*FILE(\\s(?<PATH>.+\\.[^\\s]+))?.*", boost::regex::icase);
+ boost::wregex path_exp(LR"(\s*FILE(\s(?<PATH>.+\.[^\s]+))?.*)", boost::regex::icase);
boost::wsmatch path;
if(!boost::regex_match(str, path, path_exp))
return core::frame_consumer::empty();
- boost::wregex opt_exp(L"-((?<NAME>[^\\s]+)\\s+(?<VALUE>[^\\s]+))");
+ boost::wregex opt_exp(LR"(-((?<NAME>[^\s]+)\s+(?<VALUE>[^\s]+)))");
std::vector<option> options;
for(boost::wsregex_iterator it(str.begin(), str.end(), opt_exp); it != boost::wsregex_iterator(); ++it)
options.push_back(option(name, value));
}
- return spl::make_shared<ffmpeg_consumer_proxy>(env::media_folder() + path["PATH"].str(), options);
+ return spl::make_shared<ffmpeg_consumer_proxy>(path["PATH"].str(), options, separate_key);
}
-spl::shared_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)
+spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
+ const boost::property_tree::wptree& ptree, core::interaction_sink*)
{
- auto filename = ptree.get<std::wstring>(L"path");
- auto codec = ptree.get(L"vcodec", L"libx264");
+ auto filename = ptree_get<std::wstring>(ptree, L"path");
+ auto codec = ptree.get(L"vcodec", L"libx264");
+ auto separate_key = ptree.get(L"separate-key", false);
std::vector<option> options;
options.push_back(option("vcodec", u8(codec)));
- return spl::make_shared<ffmpeg_consumer_proxy>(env::media_folder() + filename, options);
+ return spl::make_shared<ffmpeg_consumer_proxy>(filename, options, separate_key);
}
}}