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
44 #include <common/assert.h>
\r
46 #include <boost/algorithm/string.hpp>
\r
47 #include <boost/timer.hpp>
\r
48 #include <boost/property_tree/ptree.hpp>
\r
49 #include <boost/filesystem.hpp>
\r
50 #include <boost/range/algorithm.hpp>
\r
51 #include <boost/range/algorithm_ext.hpp>
\r
52 #include <boost/lexical_cast.hpp>
\r
54 #include <tbb/spin_mutex.h>
\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
71 #include <libavutil/samplefmt.h>
\r
72 #include <libswresample/swresample.h>
\r
74 #if defined(_MSC_VER)
\r
75 #pragma warning (pop)
\r
78 namespace caspar { namespace ffmpeg {
\r
80 int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
\r
82 AVClass* av_class = *(AVClass**)obj;
\r
84 if((strcmp(name, "pix_fmt") == 0 || strcmp(name, "pixel_format") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
\r
86 AVCodecContext* c = (AVCodecContext*)obj;
\r
87 auto pix_fmt = av_get_pix_fmt(val);
\r
88 if(pix_fmt == PIX_FMT_NONE)
\r
90 c->pix_fmt = pix_fmt;
\r
93 //if((strcmp(name, "r") == 0 || strcmp(name, "frame_rate") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)
\r
95 // AVCodecContext* c = (AVCodecContext*)obj;
\r
97 // if(c->codec_type != AVMEDIA_TYPE_VIDEO)
\r
100 // AVRational rate;
\r
101 // int ret = av_parse_video_rate(&rate, val);
\r
105 // c->time_base.num = rate.den;
\r
106 // c->time_base.den = rate.num;
\r
110 return ::av_opt_set(obj, name, val, search_flags);
\r
118 option(std::string name, std::string value)
\r
119 : name(std::move(name))
\r
120 , value(std::move(value))
\r
125 struct output_format
\r
127 AVOutputFormat* format;
\r
135 output_format(const core::video_format_desc& format_desc, const std::string& filename, std::vector<option>& options)
\r
136 : format(av_guess_format(nullptr, filename.c_str(), nullptr))
\r
137 , width(format_desc.width)
\r
138 , height(format_desc.height)
\r
139 , vcodec(CODEC_ID_NONE)
\r
140 , acodec(CODEC_ID_NONE)
\r
144 if(boost::iequals(boost::filesystem::path(filename).extension().string(), ".dv"))
\r
145 set_opt("f", "dv");
\r
147 boost::range::remove_erase_if(options, [&](const option& o)
\r
149 return set_opt(o.name, o.value);
\r
152 if(vcodec == CODEC_ID_NONE)
\r
153 vcodec = format->video_codec;
\r
155 if(acodec == CODEC_ID_NONE)
\r
156 acodec = format->audio_codec;
\r
158 if(vcodec == CODEC_ID_NONE)
\r
159 vcodec = CODEC_ID_H264;
\r
161 if(acodec == CODEC_ID_NONE)
\r
162 acodec = CODEC_ID_PCM_S16LE;
\r
165 bool set_opt(const std::string& name, const std::string& value)
\r
167 //if(name == "target")
\r
169 // enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
\r
171 // if(name.find("pal-") != std::string::npos)
\r
173 // else if(name.find("ntsc-") != std::string::npos)
\r
176 // if(norm == UNKNOWN)
\r
177 // CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("target"));
\r
179 // if (name.find("-dv") != std::string::npos)
\r
181 // set_opt("f", "dv");
\r
184 // set_opt("s", "720x576");
\r
188 // set_opt("s", "720x480");
\r
189 // if(height == 486)
\r
191 // set_opt("croptop", "2");
\r
192 // set_opt("cropbot", "4");
\r
195 // set_opt("s", norm == PAL ? "720x576" : "720x480");
\r
203 format = av_guess_format(value.c_str(), nullptr, nullptr);
\r
205 if(format == nullptr)
\r
206 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("f"));
\r
210 else if(name == "vcodec" || name == "v:codec")
\r
212 auto c = avcodec_find_encoder_by_name(value.c_str());
\r
214 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("vcodec"));
\r
216 vcodec = avcodec_find_encoder_by_name(value.c_str())->id;
\r
220 else if(name == "acodec" || name == "a:codec")
\r
222 auto c = avcodec_find_encoder_by_name(value.c_str());
\r
224 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("acodec"));
\r
226 acodec = avcodec_find_encoder_by_name(value.c_str())->id;
\r
230 else if(name == "s")
\r
232 if(av_parse_video_size(&width, &height, value.c_str()) < 0)
\r
233 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("s"));
\r
237 else if(name == "croptop")
\r
239 croptop = boost::lexical_cast<int>(value);
\r
243 else if(name == "cropbot")
\r
245 cropbot = boost::lexical_cast<int>(value);
\r
254 typedef std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>> byte_vector;
\r
256 struct ffmpeg_consumer : boost::noncopyable
\r
258 const spl::shared_ptr<diagnostics::graph> graph_;
\r
259 const std::string filename_;
\r
260 const std::shared_ptr<AVFormatContext> oc_;
\r
261 const core::video_format_desc format_desc_;
\r
263 monitor::basic_subject event_subject_;
\r
265 tbb::spin_mutex exception_mutex_;
\r
266 std::exception_ptr exception_;
\r
268 std::shared_ptr<AVStream> audio_st_;
\r
269 std::shared_ptr<AVStream> video_st_;
\r
271 byte_vector picture_buffer_;
\r
272 byte_vector audio_buffer_;
\r
273 std::shared_ptr<SwrContext> swr_;
\r
274 std::shared_ptr<SwsContext> sws_;
\r
276 int64_t frame_number_;
\r
278 output_format output_format_;
\r
280 executor executor_;
\r
282 ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, std::vector<option> options)
\r
283 : filename_(filename)
\r
284 , oc_(avformat_alloc_context(), av_free)
\r
285 , format_desc_(format_desc)
\r
287 , output_format_(format_desc, filename, options)
\r
288 , executor_(print())
\r
292 // TODO: Ask stakeholders about case where file already exists.
\r
293 boost::filesystem::remove(boost::filesystem::path(env::media_folder() + u16(filename))); // Delete the file if it exists
\r
295 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
\r
296 graph_->set_text(print());
\r
297 diagnostics::register_graph(graph_);
\r
299 executor_.set_capacity(8);
\r
301 oc_->oformat = output_format_.format;
\r
303 strcpy_s(oc_->filename, filename_.c_str());
\r
305 // Add the audio and video streams using the default format codecs and initialize the codecs.
\r
306 video_st_ = add_video_stream(options);
\r
307 audio_st_ = add_audio_stream(options);
\r
309 av_dump_format(oc_.get(), 0, filename_.c_str(), 1);
\r
311 // Open the output ffmpeg, if needed.
\r
312 if (!(oc_->oformat->flags & AVFMT_NOFILE))
\r
313 THROW_ON_ERROR2(avio_open(&oc_->pb, filename.c_str(), AVIO_FLAG_WRITE), "[ffmpeg_consumer]");
\r
315 THROW_ON_ERROR2(avformat_write_header(oc_.get(), nullptr), "[ffmpeg_consumer]");
\r
317 if(options.size() > 0)
\r
319 BOOST_FOREACH(auto& option, options)
\r
320 CASPAR_LOG(warning) << L"Invalid option: -" << u16(option.name) << L" " << u16(option.value);
\r
323 CASPAR_LOG(info) << print() << L" Successfully Initialized.";
\r
330 LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");
\r
335 if (!(oc_->oformat->flags & AVFMT_NOFILE))
\r
336 LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]");
\r
338 CASPAR_LOG(info) << print() << L" Successfully Uninitialized.";
\r
343 bool send(core::const_frame& frame)
\r
345 auto exception = lock(exception_mutex_, [&]
\r
350 if(exception != nullptr)
\r
351 std::rethrow_exception(exception);
\r
353 executor_.begin_invoke([=]
\r
361 std::wstring print() const
\r
363 return L"ffmpeg[" + u16(filename_) + L"]";
\r
366 void subscribe(const monitor::observable::observer_ptr& o)
\r
368 event_subject_.subscribe(o);
\r
371 void unsubscribe(const monitor::observable::observer_ptr& o)
\r
373 event_subject_.unsubscribe(o);
\r
377 std::shared_ptr<AVStream> add_video_stream(std::vector<option>& options)
\r
379 if(output_format_.vcodec == CODEC_ID_NONE)
\r
382 auto st = av_new_stream(oc_.get(), 0);
\r
384 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream.") << boost::errinfo_api_function("av_new_stream"));
\r
386 auto encoder = avcodec_find_encoder(output_format_.vcodec);
\r
388 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Codec not found."));
\r
390 auto c = st->codec;
\r
392 avcodec_get_context_defaults3(c, encoder);
\r
394 c->codec_id = output_format_.vcodec;
\r
395 c->codec_type = AVMEDIA_TYPE_VIDEO;
\r
396 c->width = output_format_.width;
\r
397 c->height = output_format_.height - output_format_.croptop - output_format_.cropbot;
\r
398 c->time_base.den = format_desc_.time_scale;
\r
399 c->time_base.num = format_desc_.duration;
\r
401 c->flags |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);
\r
402 c->pix_fmt = c->pix_fmt != PIX_FMT_NONE ? c->pix_fmt : PIX_FMT_YUV420P;
\r
404 if(c->codec_id == CODEC_ID_PRORES)
\r
406 c->bit_rate = output_format_.width < 1280 ? 63*1000000 : 220*1000000;
\r
407 c->pix_fmt = PIX_FMT_YUV422P10;
\r
409 else if(c->codec_id == CODEC_ID_DNXHD)
\r
411 if(c->width < 1280 || c->height < 720)
\r
412 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Unsupported video dimensions."));
\r
414 c->bit_rate = 220*1000000;
\r
415 c->pix_fmt = PIX_FMT_YUV422P;
\r
417 else if(c->codec_id == CODEC_ID_DVVIDEO)
\r
419 c->width = c->height == 1280 ? 960 : c->width;
\r
421 if(format_desc_.format == core::video_format::ntsc)
\r
423 c->pix_fmt = PIX_FMT_YUV411P;
\r
424 output_format_.croptop = 2;
\r
425 output_format_.cropbot = 4;
\r
426 c->height = output_format_.height - output_format_.croptop - output_format_.cropbot;
\r
428 else if(format_desc_.format == core::video_format::pal)
\r
429 c->pix_fmt = PIX_FMT_YUV420P;
\r
431 c->pix_fmt = PIX_FMT_YUV422P;
\r
433 if(format_desc_.duration == 1001)
\r
434 c->width = c->height == 1080 ? 1280 : c->width;
\r
436 c->width = c->height == 1080 ? 1440 : c->width;
\r
438 else if(c->codec_id == CODEC_ID_H264)
\r
440 c->pix_fmt = PIX_FMT_YUV420P;
\r
441 av_opt_set(c->priv_data, "preset", "ultrafast", 0);
\r
442 av_opt_set(c->priv_data, "tune", "fastdecode", 0);
\r
443 av_opt_set(c->priv_data, "crf", "5", 0);
\r
445 else if(c->codec_id == CODEC_ID_QTRLE)
\r
447 c->pix_fmt = PIX_FMT_ARGB;
\r
450 boost::range::remove_erase_if(options, [&](const option& o)
\r
452 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
455 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
\r
456 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
\r
458 THROW_ON_ERROR2(tbb_avcodec_open(c, encoder), "[ffmpeg_consumer]");
\r
460 return std::shared_ptr<AVStream>(st, [](AVStream* st)
\r
462 LOG_ON_ERROR2(tbb_avcodec_close(st->codec), "[ffmpeg_consumer]");
\r
463 av_freep(&st->codec);
\r
468 std::shared_ptr<AVStream> add_audio_stream(std::vector<option>& options)
\r
470 if(output_format_.acodec == CODEC_ID_NONE)
\r
473 auto st = av_new_stream(oc_.get(), 1);
\r
475 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));
\r
477 auto encoder = avcodec_find_encoder(output_format_.acodec);
\r
479 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));
\r
481 auto c = st->codec;
\r
483 avcodec_get_context_defaults3(c, encoder);
\r
485 c->codec_id = output_format_.acodec;
\r
486 c->codec_type = AVMEDIA_TYPE_AUDIO;
\r
487 c->sample_rate = 48000;
\r
489 c->sample_fmt = AV_SAMPLE_FMT_S16;
\r
490 c->time_base.num = 1;
\r
491 c->time_base.den = c->sample_rate;
\r
493 if(output_format_.vcodec == CODEC_ID_FLV1)
\r
494 c->sample_rate = 44100;
\r
496 if(output_format_.format->flags & AVFMT_GLOBALHEADER)
\r
497 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
\r
499 boost::range::remove_erase_if(options, [&](const option& o)
\r
501 return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;
\r
504 THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");
\r
506 return std::shared_ptr<AVStream>(st, [](AVStream* st)
\r
508 LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");;
\r
509 av_freep(&st->codec);
\r
514 void encode_video_frame(core::const_frame frame)
\r
519 auto enc = video_st_->codec;
\r
521 auto av_frame = convert_video(frame, enc);
\r
522 av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
\r
523 av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper;
\r
524 av_frame->pts = frame_number_++;
\r
526 event_subject_ << monitor::event("frame") % static_cast<int64_t>(frame_number_)
\r
527 % static_cast<int64_t>(std::numeric_limits<int64_t>::max());
\r
530 av_init_packet(&pkt);
\r
531 pkt.data = nullptr;
\r
534 int got_packet = 0;
\r
535 THROW_ON_ERROR2(avcodec_encode_video2(enc, &pkt, av_frame.get(), &got_packet), "[ffmpeg_consumer]");
\r
536 std::shared_ptr<AVPacket> guard(&pkt, av_free_packet);
\r
541 if (pkt.pts != AV_NOPTS_VALUE)
\r
542 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, video_st_->time_base);
\r
543 if (pkt.dts != AV_NOPTS_VALUE)
\r
544 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, video_st_->time_base);
\r
546 pkt.stream_index = video_st_->index;
\r
548 THROW_ON_ERROR2(av_interleaved_write_frame(oc_.get(), &pkt), "[ffmpeg_consumer]");
\r
551 uint64_t get_channel_layout(AVCodecContext* dec)
\r
553 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
557 void encode_audio_frame(core::const_frame frame)
\r
562 auto enc = audio_st_->codec;
\r
564 boost::push_back(audio_buffer_, convert_audio(frame, enc));
\r
566 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
568 while(audio_buffer_.size() >= frame_size)
\r
570 std::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
571 avcodec_get_frame_defaults(av_frame.get());
\r
572 av_frame->nb_samples = frame_size / (enc->channels * av_get_bytes_per_sample(enc->sample_fmt));
\r
575 av_init_packet(&pkt);
\r
576 pkt.data = nullptr;
\r
579 THROW_ON_ERROR2(avcodec_fill_audio_frame(av_frame.get(), enc->channels, enc->sample_fmt, audio_buffer_.data(), frame_size, 1), "[ffmpeg_consumer]");
\r
581 int got_packet = 0;
\r
582 THROW_ON_ERROR2(avcodec_encode_audio2(enc, &pkt, av_frame.get(), &got_packet), "[ffmpeg_consumer]");
\r
583 std::shared_ptr<AVPacket> guard(&pkt, av_free_packet);
\r
585 audio_buffer_.erase(audio_buffer_.begin(), audio_buffer_.begin() + frame_size);
\r
590 if (pkt.pts != AV_NOPTS_VALUE)
\r
591 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, audio_st_->time_base);
\r
592 if (pkt.dts != AV_NOPTS_VALUE)
\r
593 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, audio_st_->time_base);
\r
594 if (pkt.duration > 0)
\r
595 pkt.duration = static_cast<int>(av_rescale_q(pkt.duration, enc->time_base, audio_st_->time_base));
\r
597 pkt.stream_index = audio_st_->index;
\r
599 THROW_ON_ERROR2(av_interleaved_write_frame(oc_.get(), &pkt), "[ffmpeg_consumer]");
\r
603 std::shared_ptr<AVFrame> convert_video(core::const_frame frame, AVCodecContext* c)
\r
607 sws_.reset(sws_getContext(format_desc_.width,
\r
608 format_desc_.height - output_format_.croptop - output_format_.cropbot,
\r
613 SWS_BICUBIC, nullptr, nullptr, nullptr),
\r
615 if (sws_ == nullptr)
\r
616 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));
\r
621 std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);
\r
623 avpicture_fill(reinterpret_cast<AVPicture*>(in_frame.get()),
\r
624 const_cast<uint8_t*>(frame.image_data().begin()),
\r
626 format_desc_.width,
\r
627 format_desc_.height - output_format_.croptop - output_format_.cropbot);
\r
630 for(int n = 0; n < 4; ++n)
\r
631 in_frame->data[n] += in_frame->linesize[n] * output_format_.croptop;
\r
635 std::shared_ptr<AVFrame> out_frame(avcodec_alloc_frame(), av_free);
\r
637 av_image_fill_linesizes(out_frame->linesize, c->pix_fmt, c->width);
\r
638 for(int n = 0; n < 4; ++n)
\r
639 out_frame->linesize[n] += 32 - (out_frame->linesize[n] % 32); // align
\r
641 picture_buffer_.resize(av_image_fill_pointers(out_frame->data, c->pix_fmt, c->height, nullptr, out_frame->linesize));
\r
642 av_image_fill_pointers(out_frame->data, c->pix_fmt, c->height, picture_buffer_.data(), out_frame->linesize);
\r
646 sws_scale(sws_.get(),
\r
648 in_frame->linesize,
\r
650 format_desc_.height - output_format_.cropbot - output_format_.croptop,
\r
652 out_frame->linesize);
\r
657 byte_vector convert_audio(core::const_frame& frame, AVCodecContext* c)
\r
661 swr_ = std::shared_ptr<SwrContext>(swr_alloc_set_opts(nullptr,
\r
662 get_channel_layout(c), c->sample_fmt, c->sample_rate,
\r
663 av_get_default_channel_layout(format_desc_.audio_channels), AV_SAMPLE_FMT_S32, format_desc_.audio_sample_rate,
\r
664 0, nullptr), [](SwrContext* p){swr_free(&p);});
\r
667 CASPAR_THROW_EXCEPTION(bad_alloc());
\r
669 THROW_ON_ERROR2(swr_init(swr_.get()), "[audio_decoder]");
\r
672 byte_vector buffer(48000);
\r
674 const uint8_t* in[] = {reinterpret_cast<const uint8_t*>(frame.audio_data().data())};
\r
675 uint8_t* out[] = {buffer.data()};
\r
677 auto channel_samples = swr_convert(swr_.get(),
\r
678 out, static_cast<int>(buffer.size()) / c->channels / av_get_bytes_per_sample(c->sample_fmt),
\r
679 in, static_cast<int>(frame.audio_data().size()/format_desc_.audio_channels));
\r
681 buffer.resize(channel_samples * c->channels * av_get_bytes_per_sample(c->sample_fmt));
\r
688 auto space = boost::filesystem::space(boost::filesystem::path(filename_).parent_path());
\r
689 if(space.available < 512*1000000)
\r
690 BOOST_THROW_EXCEPTION(file_write_error() << msg_info("out of space"));
\r
693 void encode(const core::const_frame& frame)
\r
697 if(frame_number_ % 25 == 0)
\r
700 boost::timer frame_timer;
\r
702 encode_video_frame(frame);
\r
703 encode_audio_frame(frame);
\r
705 graph_->set_value("frame-time", frame_timer.elapsed()*format_desc_.fps*0.5);
\r
709 lock(exception_mutex_, [&]
\r
711 exception_ = std::current_exception();
\r
717 struct ffmpeg_consumer_proxy : public core::frame_consumer
\r
719 const std::wstring filename_;
\r
720 const std::vector<option> options_;
\r
722 std::unique_ptr<ffmpeg_consumer> consumer_;
\r
726 ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options)
\r
727 : filename_(filename)
\r
728 , options_(options)
\r
732 virtual void initialize(const core::video_format_desc& format_desc, int)
\r
735 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot reinitialize ffmpeg-consumer."));
\r
737 consumer_.reset(new ffmpeg_consumer(u8(filename_), format_desc, options_));
\r
740 bool send(core::const_frame frame) override
\r
742 return consumer_->send(frame);
\r
745 std::wstring print() const override
\r
747 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
\r
750 std::wstring name() const override
\r
755 boost::property_tree::wptree info() const override
\r
757 boost::property_tree::wptree info;
\r
758 info.add(L"type", L"file");
\r
759 info.add(L"filename", filename_);
\r
763 bool has_synchronization_clock() const override
\r
768 int buffer_depth() const override
\r
773 int index() const override
\r
778 void subscribe(const monitor::observable::observer_ptr& o) override
\r
780 consumer_->subscribe(o);
\r
783 void unsubscribe(const monitor::observable::observer_ptr& o) override
\r
785 consumer_->unsubscribe(o);
\r
788 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
790 auto str = std::accumulate(params.begin(), params.end(), std::wstring(), [](const std::wstring& lhs, const std::wstring& rhs) {return lhs + L" " + rhs;});
\r
792 boost::wregex path_exp(L"(FILE)? (?<PATH>.+\\..+).*", boost::regex::icase);
\r
794 boost::wsmatch path;
\r
795 if(!boost::regex_match(str, path, path_exp))
\r
796 return core::frame_consumer::empty();
\r
798 boost::wregex opt_exp(L"-((?<NAME>[^\\s]+)\\s+(?<VALUE>[^\\s]+))");
\r
800 std::vector<option> options;
\r
801 for(boost::wsregex_iterator it(str.begin(), str.end(), opt_exp); it != boost::wsregex_iterator(); ++it)
\r
803 auto name = u8(boost::trim_copy(boost::to_lower_copy((*it)["NAME"].str())));
\r
804 auto value = u8(boost::trim_copy(boost::to_lower_copy((*it)["VALUE"].str())));
\r
806 if(value == "h264")
\r
808 else if(value == "dvcpro")
\r
811 options.push_back(option(name, value));
\r
814 return spl::make_shared<ffmpeg_consumer_proxy>(env::media_folder() + path["PATH"].str(), options);
\r
817 spl::shared_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)
\r
819 auto filename = ptree.get<std::wstring>(L"path");
\r
820 auto codec = ptree.get(L"vcodec", L"libx264");
\r
822 std::vector<option> options;
\r
823 options.push_back(option("vcodec", u8(codec)));
\r
825 return spl::make_shared<ffmpeg_consumer_proxy>(env::media_folder() + filename, options);
\r