2 * Copyright (c) 2011 Sveriges Television AB <info@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 "../producer/tbb_avcodec.h"
\r
30 #include <core/frame/frame.h>
\r
31 #include <core/mixer/audio/audio_util.h>
\r
32 #include <core/consumer/frame_consumer.h>
\r
33 #include <core/video_format.h>
\r
35 #include <common/array.h>
\r
36 #include <common/env.h>
\r
37 #include <common/except.h>
\r
38 #include <common/executor.h>
\r
39 #include <common/diagnostics/graph.h>
\r
40 #include <common/lock.h>
\r
41 #include <common/memory.h>
\r
42 #include <common/param.h>
\r
43 #include <common/utf.h>
\r
45 #include <boost/algorithm/string.hpp>
\r
46 #include <boost/timer.hpp>
\r
47 #include <boost/property_tree/ptree.hpp>
\r
48 #include <boost/filesystem.hpp>
\r
49 #include <boost/range/algorithm.hpp>
\r
50 #include <boost/range/algorithm_ext.hpp>
\r
51 #include <boost/lexical_cast.hpp>
\r
53 #include <tbb/spin_mutex.h>
\r
55 #if defined(_MSC_VER)
\r
56 #pragma warning (push)
\r
57 #pragma warning (disable : 4244)
\r
61 #define __STDC_CONSTANT_MACROS
\r
62 #define __STDC_LIMIT_MACROS
\r
63 #include <libavformat/avformat.h>
\r
64 #include <libswscale/swscale.h>
\r
65 #include <libavutil/opt.h>
\r
66 #include <libavutil/pixdesc.h>
\r
67 #include <libavutil/parseutils.h>
\r
68 #include <libavutil/samplefmt.h>
\r
69 #include <libswresample/swresample.h>
\r
71 #if defined(_MSC_VER)
\r
72 #pragma warning (pop)
\r
75 namespace caspar { namespace ffmpeg {
\r
77 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
\r
79 AVClass* av_class = *(AVClass**)obj;
\r
81 if((strcmp(name, "pix_fmt") == 0 || strcmp(name, "pixel_format") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
\r
83 AVCodecContext* c = (AVCodecContext*)obj;
\r
84 auto pix_fmt = av_get_pix_fmt(val);
\r
85 if(pix_fmt == PIX_FMT_NONE)
\r
87 c->pix_fmt = pix_fmt;
\r
90 //if((strcmp(name, "r") == 0 || strcmp(name, "frame_rate") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
\r
92 // AVCodecContext* c = (AVCodecContext*)obj;
\r
94 // if(c->codec_type != AVMEDIA_TYPE_VIDEO)
\r
98 // int ret = av_parse_video_rate(&rate, val);
\r
102 // c->time_base.num = rate.den;
\r
103 // c->time_base.den = rate.num;
\r
107 return ::av_opt_set(obj, name, val, search_flags);
\r
115 option(std::string name, std::string value)
\r
116 : name(std::move(name))
\r
117 , value(std::move(value))
\r
122 struct output_format
\r
124 AVOutputFormat* format;
\r
132 output_format(const core::video_format_desc& format_desc, const std::string& filename, std::vector<option>& options)
\r
133 : format(av_guess_format(nullptr, filename.c_str(), nullptr))
\r
134 , width(format_desc.width)
\r
135 , height(format_desc.height)
\r
136 , vcodec(CODEC_ID_NONE)
\r
137 , acodec(CODEC_ID_NONE)
\r
141 boost::range::remove_erase_if(options, [&](const option& o)
\r
143 return set_opt(o.name, o.value);
\r
146 if(vcodec == CODEC_ID_NONE)
\r
147 vcodec = format->video_codec;
\r
149 if(acodec == CODEC_ID_NONE)
\r
150 acodec = format->audio_codec;
\r
152 if(vcodec == CODEC_ID_NONE)
\r
153 vcodec = CODEC_ID_H264;
\r
155 if(acodec == CODEC_ID_NONE)
\r
156 acodec = CODEC_ID_PCM_S16LE;
\r
159 bool set_opt(const std::string& name, const std::string& value)
\r
161 //if(name == "target")
\r
163 // enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
\r
165 // if(name.find("pal-") != std::string::npos)
\r
167 // else if(name.find("ntsc-") != std::string::npos)
\r
170 // if(norm == UNKNOWN)
\r
171 // CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("target"));
\r
173 // if (name.find("-dv") != std::string::npos)
\r
175 // set_opt("f", "dv");
\r
176 // set_opt("s", norm == PAL ? "720x576" : "720x480");
\r
177 // //set_opt("pix_fmt", name.find("-dv50") != std::string::npos ? "yuv422p" : norm == PAL ? "yuv420p" : "yuv411p");
\r
178 // //set_opt("ar", "48000");
\r
179 // //set_opt("ac", "2");
\r
184 format = av_guess_format(value.c_str(), nullptr, nullptr);
\r
186 if(format == nullptr)
\r
187 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("f"));
\r
191 else if(name == "vcodec")
\r
193 auto c = avcodec_find_encoder_by_name(value.c_str());
\r
195 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("vcodec"));
\r
197 vcodec = avcodec_find_encoder_by_name(value.c_str())->id;
\r
201 else if(name == "acodec")
\r
203 auto c = avcodec_find_encoder_by_name(value.c_str());
\r
205 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("acodec"));
\r
207 acodec = avcodec_find_encoder_by_name(value.c_str())->id;
\r
211 else if(name == "s")
\r
213 if(av_parse_video_size(&width, &height, value.c_str()) < 0)
\r
214 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("s"));
\r
218 else if(name == "croptop")
\r
220 croptop = boost::lexical_cast<int>(value);
\r
224 else if(name == "cropbot")
\r
226 cropbot = boost::lexical_cast<int>(value);
\r
235 typedef std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> byte_vector;
\r
237 struct ffmpeg_consumer : boost::noncopyable
\r
239 const spl::shared_ptr<diagnostics::graph> graph_;
\r
240 const std::string filename_;
\r
241 const std::shared_ptr<AVFormatContext> oc_;
\r
242 const core::video_format_desc format_desc_;
\r
244 monitor::basic_subject event_subject_;
\r
246 tbb::spin_mutex exception_mutex_;
\r
247 std::exception_ptr exception_;
\r
249 std::shared_ptr<AVStream> audio_st_;
\r
250 std::shared_ptr<AVStream> video_st_;
\r
252 byte_vector picture_buffer_;
\r
253 byte_vector audio_buffer_;
\r
254 std::shared_ptr<SwrContext> swr_;
\r
255 std::shared_ptr<SwsContext> sws_;
\r
257 int64_t frame_number_;
\r
259 output_format output_format_;
\r
261 executor executor_;
\r
263 ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, std::vector<option> options)
\r
264 : filename_(filename)
\r
265 , oc_(avformat_alloc_context(), av_free)
\r
266 , format_desc_(format_desc)
\r
268 , output_format_(format_desc, filename, options)
\r
269 , executor_(print())
\r
273 // TODO: Ask stakeholders about case where file already exists.
\r
274 boost::filesystem::remove(boost::filesystem::path(env::media_folder() + u16(filename))); // Delete the file if it exists
\r
276 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
\r
277 graph_->set_text(print());
\r
278 diagnostics::register_graph(graph_);
\r
280 executor_.set_capacity(8);
\r
282 oc_->oformat = output_format_.format;
\r
284 strcpy_s(oc_->filename, filename_.c_str());
\r
286 // Add the audio and video streams using the default format codecs and initialize the codecs.
\r
287 video_st_ = add_video_stream(options);
\r
288 audio_st_ = add_audio_stream(options);
\r
290 av_dump_format(oc_.get(), 0, filename_.c_str(), 1);
\r
292 // Open the output ffmpeg, if needed.
\r
293 if (!(oc_->oformat->flags & AVFMT_NOFILE))
\r
294 THROW_ON_ERROR2(avio_open(&oc_->pb, filename.c_str(), AVIO_FLAG_WRITE), "[ffmpeg_consumer]");
\r
296 THROW_ON_ERROR2(avformat_write_header(oc_.get(), nullptr), "[ffmpeg_consumer]");
\r
298 if(options.size() > 0)
\r
300 BOOST_FOREACH(auto& option, options)
\r
301 CASPAR_LOG(warning) << L"Invalid option: -" << u16(option.name) << L" " << u16(option.value);
\r
304 CASPAR_LOG(info) << print() << L" Successfully Initialized.";
\r
311 LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");
\r
316 if (!(oc_->oformat->flags & AVFMT_NOFILE))
\r
317 LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]");
\r
319 CASPAR_LOG(info) << print() << L" Successfully Uninitialized.";
\r
324 bool send(core::const_frame& frame)
\r
326 auto exception = lock(exception_mutex_, [&]
\r
331 if(exception != nullptr)
\r
332 std::rethrow_exception(exception);
\r
334 executor_.begin_invoke([=]
\r
342 std::wstring print() const
\r
344 return L"ffmpeg[" + u16(filename_) + L"]";
\r
347 void subscribe(const monitor::observable::observer_ptr& o)
\r
349 event_subject_.subscribe(o);
\r
352 void unsubscribe(const monitor::observable::observer_ptr& o)
\r
354 event_subject_.unsubscribe(o);
\r
358 std::shared_ptr<AVStream> add_video_stream(std::vector<option>& options)
\r
360 if(output_format_.vcodec == CODEC_ID_NONE)
\r
363 auto st = av_new_stream(oc_.get(), 0);
\r
365 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
\r
367 auto encoder = avcodec_find_encoder(output_format_.vcodec);
\r
369 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Codec not found."));
\r
371 auto c = st->codec;
\r
373 avcodec_get_context_defaults3(c, encoder);
\r
375 c->codec_id = output_format_.vcodec;
\r
376 c->codec_type = AVMEDIA_TYPE_VIDEO;
\r
377 c->width = output_format_.width;
\r
378 c->height = output_format_.height - output_format_.croptop - output_format_.cropbot;
\r
379 c->time_base.den = format_desc_.time_scale;
\r
380 c->time_base.num = format_desc_.duration;
\r
382 c->flags |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);
\r
383 c->pix_fmt = c->pix_fmt != PIX_FMT_NONE ? c->pix_fmt : PIX_FMT_YUV420P;
\r
385 if(c->codec_id == CODEC_ID_PRORES)
\r
387 c->bit_rate = c->width < 1280 ? 63*1000000 : 220*1000000;
\r
388 c->pix_fmt = PIX_FMT_YUV422P10;
\r
390 else if(c->codec_id == CODEC_ID_DNXHD)
\r
392 if(c->width < 1280 || c->height < 720)
\r
393 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Unsupported video dimensions."));
\r
395 c->bit_rate = 220*1000000;
\r
396 c->pix_fmt = PIX_FMT_YUV422P;
\r
398 else if(c->codec_id == CODEC_ID_DVVIDEO)
\r
400 c->width = c->height == 1280 ? 960 : c->width;
\r
402 if(format_desc_.format == core::video_format::ntsc)
\r
403 c->pix_fmt = PIX_FMT_YUV411P;
\r
404 else if(format_desc_.format == core::video_format::pal)
\r
405 c->pix_fmt = PIX_FMT_YUV420P;
\r
407 c->pix_fmt = PIX_FMT_YUV422P;
\r
409 if(format_desc_.duration == 1001)
\r
410 c->width = c->height == 1080 ? 1280 : c->width;
\r
412 c->width = c->height == 1080 ? 1440 : c->width;
\r
414 else if(c->codec_id == CODEC_ID_H264)
\r
416 c->pix_fmt = PIX_FMT_YUV420P;
\r
417 av_opt_set(c->priv_data, "preset", "ultrafast", 0);
\r
418 av_opt_set(c->priv_data, "tune", "fastdecode", 0);
\r
419 av_opt_set(c->priv_data, "crf", "5", 0);
\r
421 else if(c->codec_id == CODEC_ID_QTRLE)
\r
423 c->pix_fmt = PIX_FMT_ARGB;
\r
426 boost::range::remove_erase_if(options, [&](const option& o)
\r
428 return o.name.at(0) != 'a' && ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
\r
431 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
\r
432 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
\r
434 THROW_ON_ERROR2(tbb_avcodec_open(c, encoder), "[ffmpeg_consumer]");
\r
436 return std::shared_ptr<AVStream>(st, [](AVStream* st)
\r
438 LOG_ON_ERROR2(tbb_avcodec_close(st->codec), "[ffmpeg_consumer]");
\r
439 av_freep(&st->codec);
\r
444 std::shared_ptr<AVStream> add_audio_stream(std::vector<option>& options)
\r
446 if(output_format_.acodec == CODEC_ID_NONE)
\r
449 auto st = av_new_stream(oc_.get(), 1);
\r
451 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));
\r
453 auto encoder = avcodec_find_encoder(output_format_.acodec);
\r
455 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));
\r
457 auto c = st->codec;
\r
459 avcodec_get_context_defaults3(c, encoder);
\r
461 c->codec_id = output_format_.acodec;
\r
462 c->codec_type = AVMEDIA_TYPE_AUDIO;
\r
463 c->sample_rate = 48000;
\r
465 c->sample_fmt = AV_SAMPLE_FMT_S16;
\r
466 c->time_base.num = 1;
\r
467 c->time_base.den = c->sample_rate;
\r
469 if(output_format_.vcodec == CODEC_ID_FLV1)
\r
470 c->sample_rate = 44100;
\r
472 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
\r
473 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
\r
475 boost::range::remove_erase_if(options, [&](const option& o)
\r
477 return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
\r
480 THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");
\r
482 return std::shared_ptr<AVStream>(st, [](AVStream* st)
\r
484 LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");;
\r
485 av_freep(&st->codec);
\r
490 void encode_video_frame(core::const_frame frame)
\r
495 auto enc = video_st_->codec;
\r
497 auto av_frame = convert_video(frame, enc);
\r
498 av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
\r
499 av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper;
\r
500 av_frame->pts = frame_number_++;
\r
502 event_subject_ << monitor::event("frame") % static_cast<int64_t>(frame_number_)
\r
503 % static_cast<int64_t>(std::numeric_limits<int64_t>::max());
\r
506 av_init_packet(&pkt);
\r
507 pkt.data = nullptr;
\r
510 int got_packet = 0;
\r
511 THROW_ON_ERROR2(avcodec_encode_video2(enc, &pkt, av_frame.get(), &got_packet), "[ffmpeg_consumer]");
\r
512 std::shared_ptr<AVPacket> guard(&pkt, av_free_packet);
\r
517 if (pkt.pts != AV_NOPTS_VALUE)
\r
518 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, video_st_->time_base);
\r
519 if (pkt.dts != AV_NOPTS_VALUE)
\r
520 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, video_st_->time_base);
\r
522 pkt.stream_index = video_st_->index;
\r
524 THROW_ON_ERROR2(av_interleaved_write_frame(oc_.get(), &pkt), "[ffmpeg_consumer]");
\r
527 uint64_t get_channel_layout(AVCodecContext* dec)
\r
529 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);
\r
533 void encode_audio_frame(core::const_frame frame)
\r
538 auto enc = audio_st_->codec;
\r
540 boost::push_back(audio_buffer_, convert_audio(frame, enc));
\r
542 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());
\r
544 while(audio_buffer_.size() >= frame_size)
\r
546 std::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
547 avcodec_get_frame_defaults(av_frame.get());
\r
548 av_frame->nb_samples = frame_size / (enc->channels * av_get_bytes_per_sample(enc->sample_fmt));
\r
551 av_init_packet(&pkt);
\r
552 pkt.data = nullptr;
\r
555 THROW_ON_ERROR2(avcodec_fill_audio_frame(av_frame.get(), enc->channels, enc->sample_fmt, audio_buffer_.data(), frame_size, 1), "[ffmpeg_consumer]");
\r
557 int got_packet = 0;
\r
558 THROW_ON_ERROR2(avcodec_encode_audio2(enc, &pkt, av_frame.get(), &got_packet), "[ffmpeg_consumer]");
\r
559 std::shared_ptr<AVPacket> guard(&pkt, av_free_packet);
\r
561 audio_buffer_.erase(audio_buffer_.begin(), audio_buffer_.begin() + frame_size);
\r
566 if (pkt.pts != AV_NOPTS_VALUE)
\r
567 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, audio_st_->time_base);
\r
568 if (pkt.dts != AV_NOPTS_VALUE)
\r
569 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, audio_st_->time_base);
\r
570 if (pkt.duration > 0)
\r
571 pkt.duration = static_cast<int>(av_rescale_q(pkt.duration, enc->time_base, audio_st_->time_base));
\r
573 pkt.stream_index = audio_st_->index;
\r
575 THROW_ON_ERROR2(av_interleaved_write_frame(oc_.get(), &pkt), "[ffmpeg_consumer]");
\r
579 std::shared_ptr<AVFrame> convert_video(core::const_frame frame, AVCodecContext* c)
\r
583 sws_.reset(sws_getContext(format_desc_.width,
\r
584 format_desc_.height - output_format_.croptop - output_format_.cropbot,
\r
589 SWS_BICUBIC, nullptr, nullptr, nullptr),
\r
591 if (sws_ == nullptr)
\r
592 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));
\r
595 std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);
\r
597 avpicture_fill(reinterpret_cast<AVPicture*>(in_frame.get()),
\r
598 const_cast<uint8_t*>(frame.image_data().begin()),
\r
600 format_desc_.width,
\r
601 format_desc_.height - output_format_.croptop - output_format_.cropbot);
\r
603 for(int n = 0; n < 4; ++n)
\r
604 in_frame->data[n] += in_frame->linesize[n] * output_format_.croptop;
\r
606 picture_buffer_.resize(avpicture_get_size(c->pix_fmt, c->width, c->height));
\r
608 std::shared_ptr<AVFrame> out_frame(avcodec_alloc_frame(), av_free);
\r
610 avpicture_fill(reinterpret_cast<AVPicture*>(out_frame.get()),
\r
611 picture_buffer_.data(),
\r
616 sws_scale(sws_.get(),
\r
618 in_frame->linesize,
\r
620 format_desc_.height - output_format_.cropbot - output_format_.croptop,
\r
622 out_frame->linesize);
\r
627 byte_vector convert_audio(core::const_frame& frame, AVCodecContext* c)
\r
631 swr_ = std::shared_ptr<SwrContext>(swr_alloc_set_opts(nullptr,
\r
632 get_channel_layout(c), c->sample_fmt, c->sample_rate,
\r
633 av_get_default_channel_layout(format_desc_.audio_channels), AV_SAMPLE_FMT_S32, format_desc_.audio_sample_rate,
\r
634 0, nullptr), [](SwrContext* p){swr_free(&p);});
\r
637 CASPAR_THROW_EXCEPTION(bad_alloc());
\r
639 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
\r
642 byte_vector buffer(48000);
\r
644 const uint8_t* in[] = {reinterpret_cast<const uint8_t*>(frame.audio_data().data())};
\r
645 uint8_t* out[] = {buffer.data()};
\r
647 auto channel_samples = swr_convert(swr_.get(),
\r
648 out, static_cast<int>(buffer.size()) / c->channels / av_get_bytes_per_sample(c->sample_fmt),
\r
649 in, static_cast<int>(frame.audio_data().size()/format_desc_.audio_channels));
\r
651 buffer.resize(channel_samples * c->channels * av_get_bytes_per_sample(c->sample_fmt));
\r
658 auto space = boost::filesystem::space(boost::filesystem::path(filename_).parent_path());
\r
659 if(space.available < 512*1000000)
\r
660 BOOST_THROW_EXCEPTION(file_write_error() << msg_info("out of space"));
\r
663 void encode(const core::const_frame& frame)
\r
667 if(frame_number_ % 25 == 0)
\r
670 boost::timer frame_timer;
\r
672 encode_video_frame(frame);
\r
673 encode_audio_frame(frame);
\r
675 graph_->set_value("frame-time", frame_timer.elapsed()*format_desc_.fps*0.5);
\r
679 lock(exception_mutex_, [&]
\r
681 exception_ = std::current_exception();
\r
687 struct ffmpeg_consumer_proxy : public core::frame_consumer
\r
689 const std::wstring filename_;
\r
690 const std::vector<option> options_;
\r
692 std::unique_ptr<ffmpeg_consumer> consumer_;
\r
696 ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options)
\r
697 : filename_(filename)
\r
698 , options_(options)
\r
702 virtual void initialize(const core::video_format_desc& format_desc, int)
\r
705 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
\r
707 consumer_.reset(new ffmpeg_consumer(u8(filename_), format_desc, options_));
\r
710 bool send(core::const_frame frame) override
\r
712 return consumer_->send(frame);
\r
715 std::wstring print() const override
\r
717 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
\r
720 std::wstring name() const override
\r
725 boost::property_tree::wptree info() const override
\r
727 boost::property_tree::wptree info;
\r
728 info.add(L"type", L"file");
\r
729 info.add(L"filename", filename_);
\r
733 bool has_synchronization_clock() const override
\r
738 int buffer_depth() const override
\r
743 int index() const override
\r
748 void subscribe(const monitor::observable::observer_ptr& o) override
\r
750 consumer_->subscribe(o);
\r
753 void unsubscribe(const monitor::observable::observer_ptr& o) override
\r
755 consumer_->unsubscribe(o);
\r
758 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
760 if(params.size() < 1 || params[0] != L"FILE")
\r
761 return core::frame_consumer::empty();
\r
763 auto filename = (params.size() > 1 ? params[1] : L"");
\r
765 std::vector<option> options;
\r
767 if(params.size() >= 3)
\r
769 for(auto opt_it = params.begin()+2; opt_it != params.end();)
\r
771 auto name = u8(boost::trim_copy(boost::to_lower_copy(*opt_it++))).substr(1);
\r
772 auto value = u8(boost::trim_copy(boost::to_lower_copy(*opt_it++)));
\r
774 if(value == "h264")
\r
776 else if(value == "dvcpro")
\r
779 options.push_back(option(name, value));
\r
783 return spl::make_shared<ffmpeg_consumer_proxy>(env::media_folder() + filename, options);
\r
786 spl::shared_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)
\r
788 auto filename = ptree.get<std::wstring>(L"path");
\r
789 auto codec = ptree.get(L"vcodec", L"libx264");
\r
791 std::vector<option> options;
\r
792 options.push_back(option("vcodec", u8(codec)));
\r
794 return spl::make_shared<ffmpeg_consumer_proxy>(env::media_folder() + filename, options);
\r