2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../StdAfx.h"
\r
24 #include "../ffmpeg_error.h"
\r
26 #include "ffmpeg_consumer.h"
\r
28 #include "../ffmpeg_params.h"
\r
29 #include "../producer/audio/audio_resampler.h"
\r
31 #include <core/parameters/parameters.h>
\r
32 #include <core/mixer/read_frame.h>
\r
33 #include <core/mixer/audio/audio_util.h>
\r
34 #include <core/consumer/frame_consumer.h>
\r
35 #include <core/video_format.h>
\r
37 #include <common/concurrency/executor.h>
\r
38 #include <common/concurrency/future_util.h>
\r
39 #include <common/diagnostics/graph.h>
\r
40 #include <common/env.h>
\r
41 #include <common/utility/string.h>
\r
42 #include <common/memory/memshfl.h>
\r
44 #include <boost/algorithm/string.hpp>
\r
45 #include <boost/timer.hpp>
\r
46 #include <boost/property_tree/ptree.hpp>
\r
48 #include <tbb/cache_aligned_allocator.h>
\r
49 #include <tbb/parallel_invoke.h>
\r
50 #include <tbb/atomic.h>
\r
52 #include <boost/range/algorithm.hpp>
\r
53 #include <boost/range/algorithm_ext.hpp>
\r
54 #include <boost/lexical_cast.hpp>
\r
58 #if defined(_MSC_VER)
\r
59 #pragma warning (push)
\r
60 #pragma warning (disable : 4244)
\r
64 #define __STDC_CONSTANT_MACROS
\r
65 #define __STDC_LIMIT_MACROS
\r
66 #include <libavformat/avformat.h>
\r
67 #include <libswscale/swscale.h>
\r
68 #include <libavutil/opt.h>
\r
69 #include <libavutil/pixdesc.h>
\r
70 #include <libavutil/parseutils.h>
\r
72 #if defined(_MSC_VER)
\r
73 #pragma warning (pop)
\r
76 namespace caspar { namespace ffmpeg {
\r
78 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
\r
80 AVClass* av_class = *(AVClass**)obj;
\r
82 if((strcmp(name, "pix_fmt") == 0 || strcmp(name, "pixel_format") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
\r
84 AVCodecContext* c = (AVCodecContext*)obj;
\r
85 auto pix_fmt = av_get_pix_fmt(val);
\r
86 if(pix_fmt == PIX_FMT_NONE)
\r
88 c->pix_fmt = pix_fmt;
\r
91 if((strcmp(name, "r") == 0 || strcmp(name, "frame_rate") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
\r
93 AVCodecContext* c = (AVCodecContext*)obj;
\r
95 if(c->codec_type != AVMEDIA_TYPE_VIDEO)
\r
99 int ret = av_parse_video_rate(&rate, val);
\r
103 c->time_base.num = rate.den;
\r
104 c->time_base.den = rate.num;
\r
108 return ::av_opt_set(obj, name, val, search_flags);
\r
111 struct output_format
\r
113 AVOutputFormat* format;
\r
119 output_format(const core::video_format_desc& format_desc, const std::string& filename, std::vector<option>& options)
\r
120 : format(av_guess_format(nullptr, filename.c_str(), nullptr))
\r
121 , width(format_desc.width)
\r
122 , height(format_desc.height)
\r
123 , vcodec(CODEC_ID_NONE)
\r
124 , acodec(CODEC_ID_NONE)
\r
126 boost::range::remove_erase_if(options, [&](const option& o)
\r
128 return set_opt(o.name, o.value);
\r
131 if(vcodec == CODEC_ID_NONE)
\r
132 vcodec = format->video_codec;
\r
134 if(acodec == CODEC_ID_NONE)
\r
135 acodec = format->audio_codec;
\r
137 if(vcodec == CODEC_ID_NONE)
\r
138 vcodec = CODEC_ID_H264;
\r
140 if(acodec == CODEC_ID_NONE)
\r
141 acodec = CODEC_ID_PCM_S16LE;
\r
144 bool set_opt(const std::string& name, const std::string& value)
\r
146 //if(name == "target")
\r
148 // enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
\r
150 // if(name.find("pal-") != std::string::npos)
\r
152 // else if(name.find("ntsc-") != std::string::npos)
\r
155 // if(norm == UNKNOWN)
\r
156 // BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("target"));
\r
158 // if (name.find("-dv") != std::string::npos)
\r
160 // set_opt("f", "dv");
\r
161 // set_opt("s", norm == PAL ? "720x576" : "720x480");
\r
162 // //set_opt("pix_fmt", name.find("-dv50") != std::string::npos ? "yuv422p" : norm == PAL ? "yuv420p" : "yuv411p");
\r
163 // //set_opt("ar", "48000");
\r
164 // //set_opt("ac", "2");
\r
169 format = av_guess_format(value.c_str(), nullptr, nullptr);
\r
171 if(format == nullptr)
\r
172 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("f"));
\r
176 else if(name == "vcodec")
\r
178 auto c = avcodec_find_encoder_by_name(value.c_str());
\r
180 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("vcodec"));
\r
182 vcodec = avcodec_find_encoder_by_name(value.c_str())->id;
\r
186 else if(name == "acodec")
\r
188 auto c = avcodec_find_encoder_by_name(value.c_str());
\r
190 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("acodec"));
\r
192 acodec = avcodec_find_encoder_by_name(value.c_str())->id;
\r
196 else if(name == "s")
\r
198 if(av_parse_video_size(&width, &height, value.c_str()) < 0)
\r
199 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("s"));
\r
208 typedef std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> byte_vector;
\r
210 struct ffmpeg_consumer : boost::noncopyable
\r
212 const std::string filename_;
\r
214 const std::shared_ptr<AVFormatContext> oc_;
\r
215 const core::video_format_desc format_desc_;
\r
216 const core::channel_layout channel_layout_;
\r
218 const safe_ptr<diagnostics::graph> graph_;
\r
220 executor encode_executor_;
\r
222 std::shared_ptr<AVStream> audio_st_;
\r
223 std::shared_ptr<AVStream> video_st_;
\r
225 byte_vector audio_outbuf_;
\r
226 byte_vector audio_buf_;
\r
227 byte_vector video_outbuf_;
\r
228 byte_vector key_picture_buf_;
\r
229 byte_vector picture_buf_;
\r
230 std::shared_ptr<audio_resampler> swr_;
\r
231 std::shared_ptr<SwsContext> sws_;
\r
233 int64_t in_frame_number_;
\r
234 int64_t out_frame_number_;
\r
236 output_format output_format_;
\r
238 tbb::atomic<int64_t> current_encoding_delay_;
\r
241 ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, std::vector<option> options, bool key_only, const core::channel_layout& audio_channel_layout)
\r
242 : filename_(filename)
\r
243 , video_outbuf_(1920*1080*8)
\r
244 , audio_outbuf_(10000)
\r
245 , oc_(avformat_alloc_context(), av_free)
\r
246 , format_desc_(format_desc)
\r
247 , channel_layout_(audio_channel_layout)
\r
248 , encode_executor_(print())
\r
249 , in_frame_number_(0)
\r
250 , out_frame_number_(0)
\r
251 , output_format_(format_desc, filename, options)
\r
252 , key_only_(key_only)
\r
254 current_encoding_delay_ = 0;
\r
256 // TODO: Ask stakeholders about case where file already exists.
\r
257 boost::filesystem2::remove(boost::filesystem2::wpath(env::media_folder() + widen(filename))); // Delete the file if it exists
\r
259 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
\r
260 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
\r
261 graph_->set_text(print());
\r
262 diagnostics::register_graph(graph_);
\r
264 encode_executor_.set_capacity(8);
\r
266 oc_->oformat = output_format_.format;
\r
268 THROW_ON_ERROR2(av_set_parameters(oc_.get(), nullptr), "[ffmpeg_consumer]");
\r
270 strcpy_s(oc_->filename, filename_.c_str());
\r
272 // Add the audio and video streams using the default format codecs and initialize the codecs.
\r
273 auto options2 = options;
\r
274 video_st_ = add_video_stream(options2);
\r
277 audio_st_ = add_audio_stream(options);
\r
279 dump_format(oc_.get(), 0, filename_.c_str(), 1);
\r
281 // Open the output ffmpeg, if needed.
\r
282 if (!(oc_->oformat->flags & AVFMT_NOFILE))
\r
283 THROW_ON_ERROR2(avio_open(&oc_->pb, filename_.c_str(), URL_WRONLY), "[ffmpeg_consumer]");
\r
285 THROW_ON_ERROR2(av_write_header(oc_.get()), "[ffmpeg_consumer]");
\r
287 if(options.size() > 0)
\r
289 BOOST_FOREACH(auto& option, options)
\r
290 CASPAR_LOG(warning) << L"Invalid option: -" << widen(option.name) << L" " << widen(option.value);
\r
293 CASPAR_LOG(info) << print() << L" Successfully Initialized.";
\r
298 encode_executor_.stop();
\r
299 encode_executor_.join();
\r
302 LOG_ON_ERROR2(av_interleaved_write_frame(oc_.get(), nullptr), "[ffmpeg_consumer]");
\r
304 LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");
\r
310 if (!(oc_->oformat->flags & AVFMT_NOFILE))
\r
311 LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]"); // Close the output ffmpeg.
\r
313 CASPAR_LOG(info) << print() << L" Successfully Uninitialized.";
\r
316 std::wstring print() const
\r
318 return L"ffmpeg[" + widen(filename_) + L"]";
\r
321 std::shared_ptr<AVStream> add_video_stream(std::vector<option>& options)
\r
323 if(output_format_.vcodec == CODEC_ID_NONE)
\r
326 auto st = av_new_stream(oc_.get(), 0);
\r
328 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
\r
330 auto encoder = avcodec_find_encoder(output_format_.vcodec);
\r
332 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Codec not found."));
\r
334 auto c = st->codec;
\r
336 avcodec_get_context_defaults3(c, encoder);
\r
338 c->codec_id = output_format_.vcodec;
\r
339 c->codec_type = AVMEDIA_TYPE_VIDEO;
\r
340 c->width = output_format_.width;
\r
341 c->height = output_format_.height;
\r
342 c->time_base.den = format_desc_.time_scale;
\r
343 c->time_base.num = format_desc_.duration;
\r
345 c->flags |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);
\r
346 if(c->pix_fmt == PIX_FMT_NONE)
\r
347 c->pix_fmt = PIX_FMT_YUV420P;
\r
349 if(c->codec_id == CODEC_ID_PRORES)
\r
351 c->bit_rate = c->width < 1280 ? 63*1000000 : 220*1000000;
\r
352 c->pix_fmt = PIX_FMT_YUV422P10;
\r
354 else if(c->codec_id == CODEC_ID_DNXHD)
\r
356 if(c->width < 1280 || c->height < 720)
\r
357 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Unsupported video dimensions."));
\r
359 c->bit_rate = 220*1000000;
\r
360 c->pix_fmt = PIX_FMT_YUV422P;
\r
362 else if(c->codec_id == CODEC_ID_DVVIDEO)
\r
364 c->width = c->height == 1280 ? 960 : c->width;
\r
366 if(format_desc_.format == core::video_format::ntsc)
\r
367 c->pix_fmt = PIX_FMT_YUV411P;
\r
368 else if(format_desc_.format == core::video_format::pal)
\r
369 c->pix_fmt = PIX_FMT_YUV420P;
\r
371 c->pix_fmt = PIX_FMT_YUV422P;
\r
373 if(format_desc_.duration == 1001)
\r
374 c->width = c->height == 1080 ? 1280 : c->width;
\r
376 c->width = c->height == 1080 ? 1440 : c->width;
\r
378 else if(c->codec_id == CODEC_ID_H264)
\r
380 c->pix_fmt = PIX_FMT_YUV420P;
\r
381 if(options.empty())
\r
383 av_opt_set(c->priv_data, "preset", "ultrafast", 0);
\r
384 av_opt_set(c->priv_data, "tune", "fastdecode", 0);
\r
385 av_opt_set(c->priv_data, "crf", "5", 0);
\r
388 else if(c->codec_id == CODEC_ID_QTRLE)
\r
390 c->pix_fmt = PIX_FMT_ARGB;
\r
393 c->max_b_frames = 0; // b-frames not supported.
\r
395 boost::range::remove_erase_if(options, [&](const option& o)
\r
397 return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1 ||
\r
398 ffmpeg::av_opt_set(c->priv_data, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
\r
401 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
\r
402 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
\r
404 c->thread_count = boost::thread::hardware_concurrency();
\r
405 if(avcodec_open(c, encoder) < 0)
\r
407 c->thread_count = 1;
\r
408 THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");
\r
411 return std::shared_ptr<AVStream>(st, [](AVStream* st)
\r
413 LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");
\r
414 av_freep(&st->codec);
\r
419 std::shared_ptr<AVStream> add_audio_stream(std::vector<option>& options)
\r
421 if(output_format_.acodec == CODEC_ID_NONE)
\r
424 auto st = av_new_stream(oc_.get(), 1);
\r
426 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));
\r
428 auto encoder = avcodec_find_encoder(output_format_.acodec);
\r
430 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));
\r
432 auto c = st->codec;
\r
434 avcodec_get_context_defaults3(c, encoder);
\r
436 c->codec_id = output_format_.acodec;
\r
437 c->codec_type = AVMEDIA_TYPE_AUDIO;
\r
438 c->sample_rate = 48000;
\r
439 c->channels = channel_layout_.num_channels;
\r
440 c->sample_fmt = SAMPLE_FMT_S16;
\r
442 if(output_format_.vcodec == CODEC_ID_FLV1)
\r
443 c->sample_rate = 44100;
\r
445 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
\r
446 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
\r
448 boost::range::remove_erase_if(options, [&](const option& o)
\r
450 return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
\r
453 THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");
\r
455 return std::shared_ptr<AVStream>(st, [](AVStream* st)
\r
457 LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");;
\r
458 av_freep(&st->codec);
\r
463 std::shared_ptr<AVFrame> convert_video(core::read_frame& frame, AVCodecContext* c)
\r
467 sws_.reset(sws_getContext(format_desc_.width, format_desc_.height, PIX_FMT_BGRA, c->width, c->height, c->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr), sws_freeContext);
\r
468 if (sws_ == nullptr)
\r
469 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));
\r
472 std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);
\r
473 auto in_picture = reinterpret_cast<AVPicture*>(in_frame.get());
\r
477 key_picture_buf_.resize(frame.image_data().size());
\r
478 in_picture->linesize[0] = format_desc_.width * 4;
\r
479 in_picture->data[0] = key_picture_buf_.data();
\r
481 fast_memshfl(in_picture->data[0], frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
\r
485 avpicture_fill(in_picture, const_cast<uint8_t*>(frame.image_data().begin()), PIX_FMT_BGRA, format_desc_.width, format_desc_.height);
\r
488 std::shared_ptr<AVFrame> out_frame(avcodec_alloc_frame(), av_free);
\r
489 picture_buf_.resize(avpicture_get_size(c->pix_fmt, c->width, c->height));
\r
490 avpicture_fill(reinterpret_cast<AVPicture*>(out_frame.get()), picture_buf_.data(), c->pix_fmt, c->width, c->height);
\r
492 sws_scale(sws_.get(), in_frame->data, in_frame->linesize, 0, format_desc_.height, out_frame->data, out_frame->linesize);
\r
497 void encode_video_frame(core::read_frame& frame)
\r
499 auto c = video_st_->codec;
\r
501 auto in_time = static_cast<double>(in_frame_number_) / format_desc_.fps;
\r
502 auto out_time = static_cast<double>(out_frame_number_) / (static_cast<double>(c->time_base.den) / static_cast<double>(c->time_base.num));
\r
504 in_frame_number_++;
\r
506 if(out_time - in_time > 0.01)
\r
509 auto av_frame = convert_video(frame, c);
\r
510 av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
\r
511 av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper;
\r
512 av_frame->pts = out_frame_number_++;
\r
514 int out_size = THROW_ON_ERROR2(avcodec_encode_video(c, video_outbuf_.data(), video_outbuf_.size(), av_frame.get()), "[ffmpeg_consumer]");
\r
518 safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)
\r
523 av_init_packet(pkt.get());
\r
525 if (c->coded_frame->pts != AV_NOPTS_VALUE)
\r
526 pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st_->time_base);
\r
528 if(c->coded_frame->key_frame)
\r
529 pkt->flags |= AV_PKT_FLAG_KEY;
\r
531 pkt->stream_index = video_st_->index;
\r
532 pkt->data = video_outbuf_.data();
\r
533 pkt->size = out_size;
\r
535 av_interleaved_write_frame(oc_.get(), pkt.get());
\r
538 byte_vector convert_audio(core::read_frame& frame, AVCodecContext* c)
\r
541 swr_.reset(new audio_resampler(c->channels, frame.num_channels(),
\r
542 c->sample_rate, format_desc_.audio_sample_rate,
\r
543 c->sample_fmt, AV_SAMPLE_FMT_S32));
\r
546 auto audio_data = frame.audio_data();
\r
548 std::vector<int8_t, tbb::cache_aligned_allocator<int8_t>> audio_resample_buffer;
\r
549 std::copy(reinterpret_cast<const uint8_t*>(audio_data.begin()),
\r
550 reinterpret_cast<const uint8_t*>(audio_data.begin()) + audio_data.size()*4,
\r
551 std::back_inserter(audio_resample_buffer));
\r
553 audio_resample_buffer = swr_->resample(std::move(audio_resample_buffer));
\r
555 return byte_vector(audio_resample_buffer.begin(), audio_resample_buffer.end());
\r
558 void encode_audio_frame(core::read_frame& frame)
\r
560 auto c = audio_st_->codec;
\r
562 boost::range::push_back(audio_buf_, convert_audio(frame, c));
\r
564 std::size_t frame_size = c->frame_size;
\r
565 auto input_audio_size = frame_size * av_get_bytes_per_sample(c->sample_fmt) * c->channels;
\r
567 while(audio_buf_.size() >= input_audio_size)
\r
569 safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)
\r
574 av_init_packet(pkt.get());
\r
578 pkt->size = avcodec_encode_audio(c, audio_outbuf_.data(), audio_outbuf_.size(), reinterpret_cast<short*>(audio_buf_.data()));
\r
579 audio_buf_.erase(audio_buf_.begin(), audio_buf_.begin() + input_audio_size);
\r
583 audio_outbuf_ = std::move(audio_buf_);
\r
584 audio_buf_.clear();
\r
585 pkt->size = audio_outbuf_.size();
\r
586 pkt->data = audio_outbuf_.data();
\r
592 if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)
\r
593 pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, audio_st_->time_base);
\r
595 pkt->flags |= AV_PKT_FLAG_KEY;
\r
596 pkt->stream_index = audio_st_->index;
\r
597 pkt->data = reinterpret_cast<uint8_t*>(audio_outbuf_.data());
\r
599 av_interleaved_write_frame(oc_.get(), pkt.get());
\r
603 void send(const safe_ptr<core::read_frame>& frame)
\r
605 encode_executor_.begin_invoke([=]
\r
607 boost::timer frame_timer;
\r
609 encode_video_frame(*frame);
\r
612 encode_audio_frame(*frame);
\r
614 graph_->set_value("frame-time", frame_timer.elapsed()*format_desc_.fps*0.5);
\r
615 current_encoding_delay_ = frame->get_age_millis();
\r
619 bool ready_for_frame()
\r
621 return encode_executor_.size() < encode_executor_.capacity();
\r
624 void mark_dropped()
\r
626 graph_->set_tag("dropped-frame");
\r
628 // TODO: adjust PTS accordingly to make dropped frames contribute
\r
629 // to the total playing time
\r
633 struct ffmpeg_consumer_proxy : public core::frame_consumer
\r
635 const std::wstring filename_;
\r
636 const std::vector<option> options_;
\r
637 const bool separate_key_;
\r
638 core::video_format_desc format_desc_;
\r
640 std::unique_ptr<ffmpeg_consumer> consumer_;
\r
641 std::unique_ptr<ffmpeg_consumer> key_only_consumer_;
\r
645 ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options, bool separate_key_)
\r
646 : filename_(filename)
\r
647 , options_(options)
\r
648 , separate_key_(separate_key_)
\r
652 virtual void initialize(const core::video_format_desc& format_desc, int)
\r
654 format_desc_ = format_desc;
\r
657 virtual int64_t presentation_frame_age_millis() const override
\r
659 return consumer_ ? consumer_->current_encoding_delay_ : 0;
\r
662 virtual boost::unique_future<bool> send(const safe_ptr<core::read_frame>& frame) override
\r
665 do_initialize(frame->multichannel_view().channel_layout());
\r
667 bool ready_for_frame = consumer_->ready_for_frame();
\r
669 if (ready_for_frame && separate_key_)
\r
670 ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
\r
672 if (ready_for_frame)
\r
674 consumer_->send(frame);
\r
677 key_only_consumer_->send(frame);
\r
681 consumer_->mark_dropped();
\r
684 key_only_consumer_->mark_dropped();
\r
687 return caspar::wrap_as_future(true);
\r
690 virtual std::wstring print() const override
\r
692 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
\r
695 virtual boost::property_tree::wptree info() const override
\r
697 boost::property_tree::wptree info;
\r
698 info.add(L"type", L"ffmpeg-consumer");
\r
699 info.add(L"filename", filename_);
\r
700 info.add(L"separate_key", separate_key_);
\r
704 virtual bool has_synchronization_clock() const override
\r
709 virtual size_t buffer_depth() const override
\r
714 virtual int index() const override
\r
719 void do_initialize(const core::channel_layout& channel_layout)
\r
722 key_only_consumer_.reset();
\r
723 consumer_.reset(new ffmpeg_consumer(
\r
732 boost::filesystem::wpath fill_file(filename_);
\r
733 auto without_extension = fill_file.stem();
\r
734 auto key_file = env::media_folder() + without_extension + L"_A" + fill_file.extension();
\r
736 key_only_consumer_.reset(new ffmpeg_consumer(
\r
746 safe_ptr<core::frame_consumer> create_consumer(const core::parameters& params)
\r
748 if(params.size() < 1 || params[0] != L"FILE")
\r
749 return core::frame_consumer::empty();
\r
751 auto params2 = params;
\r
753 auto filename = (params2.size() > 1 ? params2[1] : L"");
\r
754 bool separate_key = params2.remove_if_exists(L"SEPARATE_KEY");
\r
756 std::vector<option> options;
\r
758 if (params2.size() >= 3)
\r
760 for (auto opt_it = params2.begin() + 2; opt_it != params2.end();)
\r
762 auto name = narrow(boost::trim_copy(boost::to_lower_copy(*opt_it++))).substr(1);
\r
764 if (opt_it == params2.end())
\r
767 auto value = narrow(boost::trim_copy(boost::to_lower_copy(*opt_it++)));
\r
769 if (value == "h264")
\r
771 else if (value == "dvcpro")
\r
774 options.push_back(option(name, value));
\r
778 return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, options, separate_key);
\r
781 safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)
\r
783 auto filename = ptree.get<std::wstring>(L"path");
\r
784 auto codec = ptree.get(L"vcodec", L"libx264");
\r
785 auto separate_key = ptree.get(L"separate-key", false);
\r
787 std::vector<option> options;
\r
788 options.push_back(option("vcodec", narrow(codec)));
\r
790 return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, options, separate_key);
\r