2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
24 #include "../ffmpeg_error.h"
26 #include "ffmpeg_consumer.h"
28 #include "../producer/tbb_avcodec.h"
30 #include <core/frame/frame.h>
31 #include <core/mixer/audio/audio_util.h>
32 #include <core/consumer/frame_consumer.h>
33 #include <core/video_format.h>
35 #include <common/array.h>
36 #include <common/env.h>
37 #include <common/except.h>
38 #include <common/executor.h>
39 #include <common/future.h>
40 #include <common/diagnostics/graph.h>
41 #include <common/lock.h>
42 #include <common/memory.h>
43 #include <common/param.h>
44 #include <common/utf.h>
45 #include <common/assert.h>
46 #include <common/memshfl.h>
48 #include <boost/algorithm/string.hpp>
49 #include <boost/timer.hpp>
50 #include <boost/property_tree/ptree.hpp>
51 #include <boost/filesystem.hpp>
52 #include <boost/range/algorithm.hpp>
53 #include <boost/range/algorithm_ext.hpp>
54 #include <boost/lexical_cast.hpp>
56 #include <tbb/spin_mutex.h>
61 #pragma warning (push)
62 #pragma warning (disable : 4244)
66 #define __STDC_CONSTANT_MACROS
67 #define __STDC_LIMIT_MACROS
68 #include <libavformat/avformat.h>
69 #include <libswscale/swscale.h>
70 #include <libavutil/opt.h>
71 #include <libavutil/pixdesc.h>
72 #include <libavutil/parseutils.h>
73 #include <libavutil/samplefmt.h>
74 #include <libswresample/swresample.h>
80 namespace caspar { namespace ffmpeg {
82 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
84 AVClass* av_class = *(AVClass**)obj;
86 if((strcmp(name, "pix_fmt") == 0 || strcmp(name, "pixel_format") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
88 AVCodecContext* c = (AVCodecContext*)obj;
89 auto pix_fmt = av_get_pix_fmt(val);
90 if(pix_fmt == PIX_FMT_NONE)
95 //if((strcmp(name, "r") == 0 || strcmp(name, "frame_rate") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
97 // AVCodecContext* c = (AVCodecContext*)obj;
99 // if(c->codec_type != AVMEDIA_TYPE_VIDEO)
103 // int ret = av_parse_video_rate(&rate, val);
107 // c->time_base.num = rate.den;
108 // c->time_base.den = rate.num;
112 return ::av_opt_set(obj, name, val, search_flags);
120 option(std::string name, std::string value)
121 : name(std::move(name))
122 , value(std::move(value))
129 AVOutputFormat* format;
137 output_format(const core::video_format_desc& format_desc, const std::string& filename, std::vector<option>& options)
138 : format(av_guess_format(nullptr, filename.c_str(), nullptr))
139 , width(format_desc.width)
140 , height(format_desc.height)
141 , vcodec(CODEC_ID_NONE)
142 , acodec(CODEC_ID_NONE)
146 if(boost::iequals(boost::filesystem::path(filename).extension().string(), ".dv"))
149 boost::range::remove_erase_if(options, [&](const option& o)
151 return set_opt(o.name, o.value);
154 if(vcodec == CODEC_ID_NONE && format)
155 vcodec = format->video_codec;
157 if(acodec == CODEC_ID_NONE && format)
158 acodec = format->audio_codec;
160 if(vcodec == CODEC_ID_NONE)
161 vcodec = CODEC_ID_H264;
163 if(acodec == CODEC_ID_NONE)
164 acodec = CODEC_ID_PCM_S16LE;
167 bool set_opt(const std::string& name, const std::string& value)
169 //if(name == "target")
171 // enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
173 // if(name.find("pal-") != std::string::npos)
175 // else if(name.find("ntsc-") != std::string::npos)
178 // if(norm == UNKNOWN)
179 // CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("target"));
181 // if (name.find("-dv") != std::string::npos)
183 // set_opt("f", "dv");
186 // set_opt("s", "720x576");
190 // set_opt("s", "720x480");
193 // set_opt("croptop", "2");
194 // set_opt("cropbot", "4");
197 // set_opt("s", norm == PAL ? "720x576" : "720x480");
205 format = av_guess_format(value.c_str(), nullptr, nullptr);
207 if(format == nullptr)
208 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("f"));
212 else if(name == "vcodec" || name == "v:codec")
214 auto c = avcodec_find_encoder_by_name(value.c_str());
216 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("vcodec"));
218 vcodec = avcodec_find_encoder_by_name(value.c_str())->id;
222 else if(name == "acodec" || name == "a:codec")
224 auto c = avcodec_find_encoder_by_name(value.c_str());
226 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("acodec"));
228 acodec = avcodec_find_encoder_by_name(value.c_str())->id;
234 if(av_parse_video_size(&width, &height, value.c_str()) < 0)
235 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("s"));
239 else if(name == "croptop")
241 croptop = boost::lexical_cast<int>(value);
245 else if(name == "cropbot")
247 cropbot = boost::lexical_cast<int>(value);
256 typedef std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> byte_vector;
258 struct ffmpeg_consumer : boost::noncopyable
260 const spl::shared_ptr<diagnostics::graph> graph_;
261 const std::string filename_;
262 const std::shared_ptr<AVFormatContext> oc_;
263 const core::video_format_desc format_desc_;
265 core::monitor::subject monitor_subject_;
267 tbb::spin_mutex exception_mutex_;
268 std::exception_ptr exception_;
270 std::shared_ptr<AVStream> audio_st_;
271 std::shared_ptr<AVStream> video_st_;
273 byte_vector picture_buffer_;
274 byte_vector key_picture_buf_;
275 byte_vector audio_buffer_;
276 std::shared_ptr<SwrContext> swr_;
277 std::shared_ptr<SwsContext> sws_;
279 int64_t frame_number_;
281 output_format output_format_;
286 ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, std::vector<option> options, bool key_only)
287 : filename_(filename)
288 , oc_(avformat_alloc_context(), av_free)
289 , format_desc_(format_desc)
291 , output_format_(format_desc, filename, options)
292 , key_only_(key_only)
297 // TODO: Ask stakeholders about case where file already exists.
298 boost::filesystem::remove(boost::filesystem::path(env::media_folder() + u16(filename))); // Delete the file if it exists
300 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
301 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
302 graph_->set_text(print());
303 diagnostics::register_graph(graph_);
305 executor_.set_capacity(8);
307 oc_->oformat = output_format_.format;
309 strcpy_s(oc_->filename, filename_.c_str());
311 // Add the audio and video streams using the default format codecs and initialize the codecs.
312 video_st_ = add_video_stream(options);
315 audio_st_ = add_audio_stream(options);
317 av_dump_format(oc_.get(), 0, filename_.c_str(), 1);
319 // Open the output ffmpeg, if needed.
320 if (!(oc_->oformat->flags & AVFMT_NOFILE))
321 THROW_ON_ERROR2(avio_open(&oc_->pb, filename.c_str(), AVIO_FLAG_WRITE), "[ffmpeg_consumer]");
323 THROW_ON_ERROR2(avformat_write_header(oc_.get(), nullptr), "[ffmpeg_consumer]");
325 if(options.size() > 0)
327 BOOST_FOREACH(auto& option, options)
328 CASPAR_LOG(warning) << L"Invalid option: -" << u16(option.name) << L" " << u16(option.value);
331 CASPAR_LOG(info) << print() << L" Successfully Initialized.";
342 CASPAR_LOG_CURRENT_EXCEPTION();
345 LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");
352 if (!(oc_->oformat->flags & AVFMT_NOFILE))
353 LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]");
355 CASPAR_LOG(info) << print() << L" Successfully Uninitialized.";
360 void send(core::const_frame& frame)
362 auto exception = lock(exception_mutex_, [&]
367 if(exception != nullptr)
368 std::rethrow_exception(exception);
370 executor_.begin_invoke([=]
376 bool ready_for_frame() const
378 return !executor_.is_full();
383 graph_->set_tag("dropped-frame");
386 std::wstring print() const
388 return L"ffmpeg[" + u16(filename_) + L"]";
391 core::monitor::subject& monitor_output()
393 return monitor_subject_;
397 std::shared_ptr<AVStream> add_video_stream(std::vector<option>& options)
399 if(output_format_.vcodec == CODEC_ID_NONE)
402 auto st = av_new_stream(oc_.get(), 0);
404 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
406 auto encoder = avcodec_find_encoder(output_format_.vcodec);
408 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Codec not found."));
412 avcodec_get_context_defaults3(c, encoder);
414 c->codec_id = output_format_.vcodec;
415 c->codec_type = AVMEDIA_TYPE_VIDEO;
416 c->width = output_format_.width;
417 c->height = output_format_.height - output_format_.croptop - output_format_.cropbot;
418 c->time_base.den = format_desc_.time_scale;
419 c->time_base.num = format_desc_.duration;
421 c->flags |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);
422 c->pix_fmt = c->pix_fmt != PIX_FMT_NONE ? c->pix_fmt : PIX_FMT_YUV420P;
424 if(c->codec_id == CODEC_ID_PRORES)
426 c->bit_rate = output_format_.width < 1280 ? 63*1000000 : 220*1000000;
427 c->pix_fmt = PIX_FMT_YUV422P10;
429 else if(c->codec_id == CODEC_ID_DNXHD)
431 if(c->width < 1280 || c->height < 720)
432 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Unsupported video dimensions."));
434 c->bit_rate = 220*1000000;
435 c->pix_fmt = PIX_FMT_YUV422P;
437 else if(c->codec_id == CODEC_ID_DVVIDEO)
439 c->width = c->height == 1280 ? 960 : c->width;
441 if(format_desc_.format == core::video_format::ntsc)
443 c->pix_fmt = PIX_FMT_YUV411P;
444 output_format_.croptop = 2;
445 output_format_.cropbot = 4;
446 c->height = output_format_.height - output_format_.croptop - output_format_.cropbot;
448 else if(format_desc_.format == core::video_format::pal)
449 c->pix_fmt = PIX_FMT_YUV420P;
451 c->pix_fmt = PIX_FMT_YUV422P;
453 if(format_desc_.duration == 1001)
454 c->width = c->height == 1080 ? 1280 : c->width;
456 c->width = c->height == 1080 ? 1440 : c->width;
458 else if(c->codec_id == CODEC_ID_H264)
460 c->pix_fmt = PIX_FMT_YUV420P;
461 av_opt_set(c->priv_data, "preset", "ultrafast", 0);
462 av_opt_set(c->priv_data, "tune", "fastdecode", 0);
463 av_opt_set(c->priv_data, "crf", "5", 0);
465 else if(c->codec_id == CODEC_ID_QTRLE)
467 c->pix_fmt = PIX_FMT_ARGB;
470 boost::range::remove_erase_if(options, [&](const option& o)
472 return o.name.at(0) != 'a' && ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
475 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
476 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
478 THROW_ON_ERROR2(tbb_avcodec_open(c, encoder), "[ffmpeg_consumer]");
480 return std::shared_ptr<AVStream>(st, [](AVStream* st)
482 LOG_ON_ERROR2(tbb_avcodec_close(st->codec), "[ffmpeg_consumer]");
483 av_freep(&st->codec);
488 std::shared_ptr<AVStream> add_audio_stream(std::vector<option>& options)
490 if(output_format_.acodec == CODEC_ID_NONE)
493 auto st = av_new_stream(oc_.get(), 1);
495 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));
497 auto encoder = avcodec_find_encoder(output_format_.acodec);
499 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));
503 avcodec_get_context_defaults3(c, encoder);
505 c->codec_id = output_format_.acodec;
506 c->codec_type = AVMEDIA_TYPE_AUDIO;
507 c->sample_rate = 48000;
509 c->sample_fmt = AV_SAMPLE_FMT_S16;
510 c->time_base.num = 1;
511 c->time_base.den = c->sample_rate;
513 if(output_format_.vcodec == CODEC_ID_FLV1)
514 c->sample_rate = 44100;
516 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
517 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
519 boost::range::remove_erase_if(options, [&](const option& o)
521 return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
524 THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");
526 return std::shared_ptr<AVStream>(st, [](AVStream* st)
528 LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");;
529 av_freep(&st->codec);
534 void encode_video_frame(core::const_frame frame)
539 auto enc = video_st_->codec;
541 auto av_frame = convert_video(frame, enc);
542 av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
543 av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper;
544 av_frame->pts = frame_number_++;
546 monitor_subject_ << core::monitor::message("/frame")
547 % static_cast<int64_t>(frame_number_)
548 % static_cast<int64_t>(std::numeric_limits<int64_t>::max());
551 av_init_packet(&pkt);
556 THROW_ON_ERROR2(avcodec_encode_video2(enc, &pkt, av_frame.get(), &got_packet), "[ffmpeg_consumer]");
557 std::shared_ptr<AVPacket> guard(&pkt, av_free_packet);
562 if (pkt.pts != AV_NOPTS_VALUE)
563 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, video_st_->time_base);
564 if (pkt.dts != AV_NOPTS_VALUE)
565 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, video_st_->time_base);
567 pkt.stream_index = video_st_->index;
569 THROW_ON_ERROR2(av_interleaved_write_frame(oc_.get(), &pkt), "[ffmpeg_consumer]");
572 uint64_t get_channel_layout(AVCodecContext* dec)
574 auto layout = (dec->channel_layout && dec->channels == av_get_channel_layout_nb_channels(dec->channel_layout)) ? dec->channel_layout : av_get_default_channel_layout(dec->channels);
578 void encode_audio_frame(core::const_frame frame)
583 auto enc = audio_st_->codec;
585 boost::push_back(audio_buffer_, convert_audio(frame, enc));
587 auto frame_size = enc->frame_size != 0 ? enc->frame_size * enc->channels * av_get_bytes_per_sample(enc->sample_fmt) : static_cast<int>(audio_buffer_.size());
589 while(audio_buffer_.size() >= frame_size)
591 std::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
592 avcodec_get_frame_defaults(av_frame.get());
593 av_frame->nb_samples = frame_size / (enc->channels * av_get_bytes_per_sample(enc->sample_fmt));
596 av_init_packet(&pkt);
600 THROW_ON_ERROR2(avcodec_fill_audio_frame(av_frame.get(), enc->channels, enc->sample_fmt, audio_buffer_.data(), frame_size, 1), "[ffmpeg_consumer]");
603 THROW_ON_ERROR2(avcodec_encode_audio2(enc, &pkt, av_frame.get(), &got_packet), "[ffmpeg_consumer]");
604 std::shared_ptr<AVPacket> guard(&pkt, av_free_packet);
606 audio_buffer_.erase(audio_buffer_.begin(), audio_buffer_.begin() + frame_size);
611 if (pkt.pts != AV_NOPTS_VALUE)
612 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, audio_st_->time_base);
613 if (pkt.dts != AV_NOPTS_VALUE)
614 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, audio_st_->time_base);
615 if (pkt.duration > 0)
616 pkt.duration = static_cast<int>(av_rescale_q(pkt.duration, enc->time_base, audio_st_->time_base));
618 pkt.stream_index = audio_st_->index;
620 THROW_ON_ERROR2(av_interleaved_write_frame(oc_.get(), &pkt), "[ffmpeg_consumer]");
624 std::shared_ptr<AVFrame> convert_video(core::const_frame frame, AVCodecContext* c)
628 sws_.reset(sws_getContext(format_desc_.width,
629 format_desc_.height - output_format_.croptop - output_format_.cropbot,
634 SWS_BICUBIC, nullptr, nullptr, nullptr),
637 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));
642 std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);
644 auto in_picture = reinterpret_cast<AVPicture*>(in_frame.get());
648 key_picture_buf_.resize(frame.image_data().size());
649 in_picture->linesize[0] = format_desc_.width * 4;
650 in_picture->data[0] = key_picture_buf_.data();
652 aligned_memshfl(in_picture->data[0], frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
658 const_cast<uint8_t*>(frame.image_data().begin()),
661 format_desc_.height - output_format_.croptop - output_format_.cropbot);
666 for(int n = 0; n < 4; ++n)
667 in_frame->data[n] += in_frame->linesize[n] * output_format_.croptop;
671 std::shared_ptr<AVFrame> out_frame(avcodec_alloc_frame(), av_free);
673 av_image_fill_linesizes(out_frame->linesize, c->pix_fmt, c->width);
674 for(int n = 0; n < 4; ++n)
675 out_frame->linesize[n] += 32 - (out_frame->linesize[n] % 32); // align
677 picture_buffer_.resize(av_image_fill_pointers(out_frame->data, c->pix_fmt, c->height, nullptr, out_frame->linesize));
678 av_image_fill_pointers(out_frame->data, c->pix_fmt, c->height, picture_buffer_.data(), out_frame->linesize);
682 sws_scale(sws_.get(),
686 format_desc_.height - output_format_.cropbot - output_format_.croptop,
688 out_frame->linesize);
693 byte_vector convert_audio(core::const_frame& frame, AVCodecContext* c)
697 swr_ = std::shared_ptr<SwrContext>(swr_alloc_set_opts(nullptr,
698 get_channel_layout(c), c->sample_fmt, c->sample_rate,
699 av_get_default_channel_layout(format_desc_.audio_channels), AV_SAMPLE_FMT_S32, format_desc_.audio_sample_rate,
700 0, nullptr), [](SwrContext* p){swr_free(&p);});
703 CASPAR_THROW_EXCEPTION(bad_alloc());
705 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
708 byte_vector buffer(48000);
710 const uint8_t* in[] = {reinterpret_cast<const uint8_t*>(frame.audio_data().data())};
711 uint8_t* out[] = {buffer.data()};
713 auto channel_samples = swr_convert(swr_.get(),
714 out, static_cast<int>(buffer.size()) / c->channels / av_get_bytes_per_sample(c->sample_fmt),
715 in, static_cast<int>(frame.audio_data().size()/format_desc_.audio_channels));
717 buffer.resize(channel_samples * c->channels * av_get_bytes_per_sample(c->sample_fmt));
724 auto space = boost::filesystem::space(boost::filesystem::path(filename_).parent_path());
725 if(space.available < 512*1000000)
726 BOOST_THROW_EXCEPTION(file_write_error() << msg_info("out of space"));
729 void encode(const core::const_frame& frame)
733 if(frame_number_ % 25 == 0)
736 boost::timer frame_timer;
738 encode_video_frame(frame);
739 encode_audio_frame(frame);
741 graph_->set_value("frame-time", frame_timer.elapsed()*format_desc_.fps*0.5);
745 lock(exception_mutex_, [&]
747 exception_ = std::current_exception();
753 struct ffmpeg_consumer_proxy : public core::frame_consumer
755 const std::wstring filename_;
756 const std::vector<option> options_;
757 const bool separate_key_;
759 std::unique_ptr<ffmpeg_consumer> consumer_;
760 std::unique_ptr<ffmpeg_consumer> key_only_consumer_;
764 ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options, bool separate_key)
765 : filename_(filename)
767 , separate_key_(separate_key_)
771 virtual void initialize(const core::video_format_desc& format_desc, int)
774 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
776 consumer_.reset(new ffmpeg_consumer(u8(filename_), format_desc, options_, false));
780 boost::filesystem::wpath fill_file(filename_);
781 auto without_extension = u16(fill_file.stem().string());
782 auto key_file = env::media_folder() + without_extension + L"_A" + u16(fill_file.extension().string());
784 key_only_consumer_.reset(new ffmpeg_consumer(u8(key_file), format_desc, options_, true));
788 boost::unique_future<bool> send(core::const_frame frame) override
790 bool ready_for_frame = consumer_->ready_for_frame();
792 if (ready_for_frame && separate_key_)
793 ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();
797 consumer_->send(frame);
800 key_only_consumer_->send(frame);
804 consumer_->mark_dropped();
807 key_only_consumer_->mark_dropped();
810 return caspar::wrap_as_future(true);
813 std::wstring print() const override
815 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
818 std::wstring name() const override
823 boost::property_tree::wptree info() const override
825 boost::property_tree::wptree info;
826 info.add(L"type", L"file");
827 info.add(L"filename", filename_);
828 info.add(L"separate_key", separate_key_);
832 bool has_synchronization_clock() const override
837 int buffer_depth() const override
842 int index() const override
847 core::monitor::subject& monitor_output()
849 return consumer_->monitor_output();
852 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
854 auto params2 = params;
855 auto separate_key_it = std::find(params2.begin(), params2.end(), L"SEPARATE_KEY");
856 bool separate_key = false;
858 if (separate_key_it != params2.end())
861 params2.erase(separate_key_it);
864 auto str = std::accumulate(params2.begin(), params2.end(), std::wstring(), [](const std::wstring& lhs, const std::wstring& rhs) {return lhs + L" " + rhs;});
866 boost::wregex path_exp(L"\\s*FILE(\\s(?<PATH>.+\\.[^\\s]+))?.*", boost::regex::icase);
869 if(!boost::regex_match(str, path, path_exp))
870 return core::frame_consumer::empty();
872 boost::wregex opt_exp(L"-((?<NAME>[^\\s]+)\\s+(?<VALUE>[^\\s]+))");
874 std::vector<option> options;
875 for(boost::wsregex_iterator it(str.begin(), str.end(), opt_exp); it != boost::wsregex_iterator(); ++it)
877 auto name = u8(boost::trim_copy(boost::to_lower_copy((*it)["NAME"].str())));
878 auto value = u8(boost::trim_copy(boost::to_lower_copy((*it)["VALUE"].str())));
882 else if(value == "dvcpro")
885 options.push_back(option(name, value));
888 return spl::make_shared<ffmpeg_consumer_proxy>(env::media_folder() + path["PATH"].str(), options, separate_key);
891 spl::shared_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)
893 auto filename = ptree.get<std::wstring>(L"path");
894 auto codec = ptree.get(L"vcodec", L"libx264");
895 auto separate_key = ptree.get(L"separate-key", false);
897 std::vector<option> options;
898 options.push_back(option("vcodec", u8(codec)));
900 return spl::make_shared<ffmpeg_consumer_proxy>(env::media_folder() + filename, options, separate_key);