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 <core/mixer/read_frame.h>
\r
29 #include <core/mixer/audio/audio_util.h>
\r
30 #include <core/consumer/frame_consumer.h>
\r
31 #include <core/video_format.h>
\r
33 #include <common/concurrency/executor.h>
\r
34 #include <common/diagnostics/graph.h>
\r
35 #include <common/env.h>
\r
36 #include <common/utility/string.h>
\r
37 #include <common/utility/param.h>
\r
39 #include <boost/algorithm/string.hpp>
\r
40 #include <boost/timer.hpp>
\r
41 #include <boost/property_tree/ptree.hpp>
\r
43 #include <tbb/cache_aligned_allocator.h>
\r
44 #include <tbb/parallel_invoke.h>
\r
46 #if defined(_MSC_VER)
\r
47 #pragma warning (push)
\r
48 #pragma warning (disable : 4244)
\r
52 #define __STDC_CONSTANT_MACROS
\r
53 #define __STDC_LIMIT_MACROS
\r
54 #include <libavformat/avformat.h>
\r
55 #include <libswscale/swscale.h>
\r
56 #include <libavutil/opt.h>
\r
58 #if defined(_MSC_VER)
\r
59 #pragma warning (pop)
\r
62 namespace caspar { namespace ffmpeg {
\r
64 struct ffmpeg_consumer : boost::noncopyable
\r
66 const std::string filename_;
\r
68 const std::shared_ptr<AVFormatContext> oc_;
\r
69 const core::video_format_desc format_desc_;
\r
71 const safe_ptr<diagnostics::graph> graph_;
\r
72 boost::timer frame_timer_;
\r
73 boost::timer write_timer_;
\r
76 executor file_write_executor_;
\r
79 std::shared_ptr<AVStream> audio_st_;
\r
82 std::shared_ptr<AVStream> video_st_;
\r
84 std::vector<uint8_t> video_outbuf_;
\r
85 std::vector<uint8_t> picture_buf_;
\r
86 std::shared_ptr<SwsContext> sws_;
\r
88 int64_t frame_number_;
\r
91 ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, const std::string& codec, const std::vector<std::string>& options)
\r
92 : filename_(filename)
\r
93 , video_outbuf_(1920*1080*8)
\r
94 , oc_(avformat_alloc_context(), av_free)
\r
95 , format_desc_(format_desc)
\r
96 , executor_(print())
\r
97 , file_write_executor_(print() + L"/output")
\r
100 // TODO: Ask stakeholders about case where file already exists.
\r
101 boost::filesystem2::remove(boost::filesystem2::wpath(env::media_folder() + widen(filename))); // Delete the file if it exists
\r
103 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
\r
104 graph_->set_color("write-time", diagnostics::color(0.5f, 0.5f, 0.1f));
\r
105 graph_->set_text(print());
\r
106 diagnostics::register_graph(graph_);
\r
108 executor_.set_capacity(8);
\r
109 file_write_executor_.set_capacity(8);
\r
111 oc_->oformat = av_guess_format(nullptr, filename_.c_str(), nullptr);
\r
113 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not find suitable output format."));
\r
115 THROW_ON_ERROR2(av_set_parameters(oc_.get(), nullptr), "[ffmpeg_consumer]");
\r
117 strcpy_s(oc_->filename, filename_.c_str());
\r
119 auto video_codec = avcodec_find_encoder_by_name(codec.c_str());
\r
120 if(video_codec == nullptr)
\r
121 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info(codec));
\r
123 // Add the audio and video streams using the default format codecs and initialize the codecs .
\r
124 video_st_ = add_video_stream(video_codec->id, options);
\r
125 audio_st_ = add_audio_stream();
\r
127 dump_format(oc_.get(), 0, filename_.c_str(), 1);
\r
129 // Open the output ffmpeg, if needed.
\r
130 if (!(oc_->oformat->flags & AVFMT_NOFILE))
\r
131 THROW_ON_ERROR2(avio_open(&oc_->pb, filename_.c_str(), URL_WRONLY), "[ffmpeg_consumer]");
\r
133 THROW_ON_ERROR2(av_write_header(oc_.get()), "[ffmpeg_consumer]");
\r
135 CASPAR_LOG(info) << print() << L" Successfully Initialized.";
\r
143 file_write_executor_.stop();
\r
144 file_write_executor_.join();
\r
146 LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");
\r
151 if (!(oc_->oformat->flags & AVFMT_NOFILE))
\r
152 LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]"); // Close the output ffmpeg.
\r
154 CASPAR_LOG(info) << print() << L" Successfully Uninitialized.";
\r
157 std::wstring print() const
\r
159 return L"ffmpeg[" + widen(filename_) + L"]";
\r
162 std::shared_ptr<AVStream> add_video_stream(enum CodecID codec_id, const std::vector<std::string>& options)
\r
164 auto st = av_new_stream(oc_.get(), 0);
\r
166 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream") << boost::errinfo_api_function("av_new_stream"));
\r
168 auto encoder = avcodec_find_encoder(codec_id);
\r
170 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));
\r
172 auto c = st->codec;
\r
174 avcodec_get_context_defaults3(c, encoder);
\r
176 c->codec_id = codec_id;
\r
177 c->codec_type = AVMEDIA_TYPE_VIDEO;
\r
178 c->width = format_desc_.width;
\r
179 c->height = format_desc_.height;
\r
180 c->time_base.den = format_desc_.time_scale;
\r
181 c->time_base.num = format_desc_.duration;
\r
183 c->flags |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);
\r
185 if(c->codec_id == CODEC_ID_PRORES)
\r
187 c->bit_rate = format_desc_.width < 1280 ? 63*1000000 : 220*1000000;
\r
188 c->pix_fmt = PIX_FMT_YUV422P10;
\r
190 else if(c->codec_id == CODEC_ID_DNXHD)
\r
192 if(format_desc_.width < 1280 || format_desc_.height < 720)
\r
193 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("unsupported dimension"));
\r
195 c->bit_rate = 220*1000000;
\r
196 c->pix_fmt = PIX_FMT_YUV422P;
\r
198 else if(c->codec_id == CODEC_ID_DVVIDEO)
\r
200 c->bit_rate = format_desc_.width < 1280 ? 50*1000000 : 100*1000000;
\r
201 c->pix_fmt = PIX_FMT_YUV422P;
\r
203 c->width = format_desc_.height == 1280 ? 960 : c->width;
\r
205 if(format_desc_.duration == 1001)
\r
206 c->width = format_desc_.height == 1080 ? 1280 : c->width;
\r
208 c->width = format_desc_.height == 1080 ? 1440 : c->width;
\r
210 else if(c->codec_id == CODEC_ID_H264)
\r
212 c->pix_fmt = PIX_FMT_YUV420P;
\r
213 if(options.empty())
\r
215 av_opt_set(c->priv_data, "preset", "ultrafast", 0);
\r
216 av_opt_set(c->priv_data, "tune", "fastdecode", 0);
\r
217 av_opt_set(c->priv_data, "crf", "5", 0);
\r
220 else if(c->codec_id == CODEC_ID_QTRLE)
\r
222 c->pix_fmt = PIX_FMT_ARGB;
\r
226 BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported output parameters."));
\r
229 c->max_b_frames = 0; // b-frames not supported.
\r
231 for(size_t n = 0; n < options.size()/2; ++n)
\r
232 THROW_ON_ERROR2(av_opt_set(c, options[n*2+0].c_str(), options[n*2+1].c_str(), AV_OPT_SEARCH_CHILDREN), "[ffmpeg_consumer]");
\r
234 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
\r
235 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
\r
237 c->thread_count = boost::thread::hardware_concurrency();
\r
238 THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");
\r
240 return std::shared_ptr<AVStream>(st, [](AVStream* st)
\r
242 LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");
\r
243 av_freep(&st->codec);
\r
248 std::shared_ptr<AVStream> add_audio_stream()
\r
250 auto st = av_new_stream(oc_.get(), 1);
\r
252 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));
\r
254 st->codec->codec_id = CODEC_ID_PCM_S16LE;
\r
255 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
\r
256 st->codec->sample_rate = 48000;
\r
257 st->codec->channels = 2;
\r
258 st->codec->sample_fmt = SAMPLE_FMT_S16;
\r
260 if(oc_->oformat->flags & AVFMT_GLOBALHEADER)
\r
261 st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
\r
263 auto codec = avcodec_find_encoder(st->codec->codec_id);
\r
265 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));
\r
267 THROW_ON_ERROR2(avcodec_open(st->codec, codec), "[ffmpeg_consumer]");
\r
269 return std::shared_ptr<AVStream>(st, [](AVStream* st)
\r
271 LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");;
\r
272 av_freep(&st->codec);
\r
277 std::shared_ptr<AVFrame> convert_video_frame(const safe_ptr<core::read_frame>& frame, AVCodecContext* c)
\r
281 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
282 if (sws_ == nullptr)
\r
283 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));
\r
286 std::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
\r
287 avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), const_cast<uint8_t*>(frame->image_data().begin()), PIX_FMT_BGRA, format_desc_.width, format_desc_.height);
\r
289 std::shared_ptr<AVFrame> local_av_frame(avcodec_alloc_frame(), av_free);
\r
290 picture_buf_.resize(avpicture_get_size(c->pix_fmt, format_desc_.width, format_desc_.height));
\r
291 avpicture_fill(reinterpret_cast<AVPicture*>(local_av_frame.get()), picture_buf_.data(), c->pix_fmt, format_desc_.width, format_desc_.height);
\r
293 sws_scale(sws_.get(), av_frame->data, av_frame->linesize, 0, c->height, local_av_frame->data, local_av_frame->linesize);
\r
295 return local_av_frame;
\r
298 std::shared_ptr<AVPacket> encode_video_frame(const safe_ptr<core::read_frame>& frame)
\r
300 auto c = video_st_->codec;
\r
302 auto av_frame = convert_video_frame(frame, c);
\r
303 av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
\r
304 av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper;
\r
305 av_frame->pts = frame_number_++;
\r
307 int out_size = THROW_ON_ERROR2(avcodec_encode_video(c, video_outbuf_.data(), video_outbuf_.size(), av_frame.get()), "[ffmpeg_consumer]");
\r
310 safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)
\r
315 av_init_packet(pkt.get());
\r
317 if (c->coded_frame->pts != AV_NOPTS_VALUE)
\r
318 pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st_->time_base);
\r
320 if(c->coded_frame->key_frame)
\r
321 pkt->flags |= AV_PKT_FLAG_KEY;
\r
323 pkt->stream_index = video_st_->index;
\r
324 pkt->data = video_outbuf_.data();
\r
325 pkt->size = out_size;
\r
327 av_dup_packet(pkt.get());
\r
333 std::shared_ptr<AVPacket> encode_audio_frame(const safe_ptr<core::read_frame>& frame)
\r
335 auto c = audio_st_->codec;
\r
337 auto audio_data = core::audio_32_to_16(frame->audio_data());
\r
339 safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)
\r
344 av_init_packet(pkt.get());
\r
346 if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)
\r
347 pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, audio_st_->time_base);
\r
349 pkt->flags |= AV_PKT_FLAG_KEY;
\r
350 pkt->stream_index = audio_st_->index;
\r
351 pkt->size = audio_data.size()*2;
\r
352 pkt->data = reinterpret_cast<uint8_t*>(audio_data.data());
\r
354 av_dup_packet(pkt.get());
\r
358 void send(const safe_ptr<core::read_frame>& frame)
\r
360 executor_.begin_invoke([=]
\r
362 frame_timer_.restart();
\r
364 auto video = encode_video_frame(frame);
\r
365 auto audio = encode_audio_frame(frame);
\r
367 graph_->set_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5);
\r
369 file_write_executor_.begin_invoke([=]
\r
371 write_timer_.restart();
\r
374 av_write_frame(oc_.get(), video.get());
\r
376 av_write_frame(oc_.get(), audio.get());
\r
378 graph_->set_value("write-time", write_timer_.elapsed()*format_desc_.fps*0.5);
\r
384 struct ffmpeg_consumer_proxy : public core::frame_consumer
\r
386 const std::wstring filename_;
\r
387 const bool key_only_;
\r
388 const std::wstring codec_;
\r
389 const std::vector<std::string> options_;
\r
391 std::unique_ptr<ffmpeg_consumer> consumer_;
\r
395 ffmpeg_consumer_proxy(const std::wstring& filename, bool key_only, const std::wstring codec, const std::vector<std::string>& options)
\r
396 : filename_(filename)
\r
397 , key_only_(key_only)
\r
398 , codec_(boost::to_lower_copy(codec))
\r
399 , options_(options)
\r
403 virtual void initialize(const core::video_format_desc& format_desc, int)
\r
406 consumer_.reset(new ffmpeg_consumer(narrow(filename_), format_desc, narrow(codec_), options_));
\r
409 virtual bool send(const safe_ptr<core::read_frame>& frame) override
\r
411 consumer_->send(frame);
\r
415 virtual std::wstring print() const override
\r
417 return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";
\r
420 virtual boost::property_tree::wptree info() const override
\r
422 boost::property_tree::wptree info;
\r
423 info.add(L"type", L"ffmpeg-consumer");
\r
424 info.add(L"key-only", key_only_);
\r
425 info.add(L"filename", filename_);
\r
426 info.add(L"vcodec", codec_);
\r
430 virtual bool has_synchronization_clock() const override
\r
435 virtual size_t buffer_depth() const override
\r
440 virtual int index() const override
\r
446 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
\r
448 if(params.size() < 1 || params[0] != L"FILE")
\r
449 return core::frame_consumer::empty();
\r
451 auto filename = (params.size() > 1 ? params[1] : L"");
\r
452 bool key_only = get_param(L"KEY_ONLY", params, false);
\r
453 auto codec = get_param(L"-VCODEC", params, get_param(L"CODEC", params, L"libx264"));
\r
455 std::vector<std::string> options;
\r
456 auto opt_it = std::find(params.begin(), params.end(), L"-VCODEC");
\r
458 if(std::distance(opt_it, params.end()) > 2)
\r
460 std::advance(opt_it, 2);
\r
461 while(opt_it != params.end())
\r
462 options.push_back(narrow(boost::replace_all_copy(boost::trim_copy(boost::to_lower_copy(*opt_it++)), L"-", L"")));
\r
467 if(codec == L"H264")
\r
468 codec = L"libx264";
\r
470 if(codec == L"DVCPRO")
\r
471 codec = L"dvvideo";
\r
473 return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, key_only, codec, options);
\r
476 safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)
\r
478 auto filename = ptree.get<std::wstring>(L"path");
\r
479 auto key_only = ptree.get(L"key-only", false);
\r
480 auto codec = ptree.get(L"vcodec", L"libx264");
\r
482 return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, key_only, codec, std::vector<std::string>());
\r