/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
*\r
-* This ffmpeg is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\r
*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU General Public License for more details.\r
-\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
*\r
+* Author: Robert Nagy, ronag89@gmail.com\r
*/\r
\r
#include "../StdAfx.h"\r
\r
#include <common/concurrency/executor.h>\r
#include <common/diagnostics/graph.h>\r
-#include <common/utility/string.h>\r
#include <common/env.h>\r
+#include <common/utility/string.h>\r
+#include <common/utility/param.h>\r
\r
-#include <boost/timer.hpp>\r
-#include <boost/thread/once.hpp>\r
-#include <boost/thread.hpp>\r
#include <boost/algorithm/string.hpp>\r
+#include <boost/timer.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
\r
#include <tbb/cache_aligned_allocator.h>\r
#include <tbb/parallel_invoke.h>\r
\r
-#include <cstdio>\r
-\r
#if defined(_MSC_VER)\r
#pragma warning (push)\r
#pragma warning (disable : 4244)\r
\r
// Audio\r
std::shared_ptr<AVStream> audio_st_;\r
- std::vector<uint8_t> audio_outbuf_;\r
-\r
- std::vector<int16_t> audio_input_buffer_;\r
-\r
+ \r
// Video\r
std::shared_ptr<AVStream> video_st_;\r
- std::vector<uint8_t> video_outbuf_;\r
\r
+ std::vector<uint8_t> video_outbuf_;\r
std::vector<uint8_t> picture_buf_;\r
- std::shared_ptr<SwsContext> img_convert_ctx_;\r
+ std::shared_ptr<SwsContext> sws_;\r
\r
int64_t frame_number_;\r
\r
public:\r
ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, const std::string& codec, const std::string& options)\r
- : filename_(filename + ".mov")\r
+ : filename_(filename)\r
, video_outbuf_(1920*1080*8)\r
- , audio_outbuf_(48000)\r
, oc_(avformat_alloc_context(), av_free)\r
, format_desc_(format_desc)\r
, executor_(print())\r
, file_write_executor_(print() + L"/output")\r
, frame_number_(0)\r
{\r
+ // TODO: Ask stakeholders about case where file already exists.\r
+ boost::filesystem2::remove(boost::filesystem2::wpath(env::media_folder() + widen(filename))); // Delete the file if it exists\r
+\r
graph_->add_guide("frame-time", 0.5);\r
graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
graph_->set_color("write-time", diagnostics::color(0.5f, 0.5f, 0.1f));\r
c->time_base.den = format_desc_.time_scale;\r
c->time_base.num = format_desc_.duration;\r
c->gop_size = 25;\r
+ c->flags |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);\r
\r
if(c->codec_id == CODEC_ID_PRORES)\r
{ \r
- c->bit_rate = c->bit_rate > 0 ? c->bit_rate : format_desc_.width < 1280 ? 42*1000000 : 147*1000000;\r
+ c->bit_rate = format_desc_.width < 1280 ? 63*1000000 : 220*1000000;\r
c->pix_fmt = PIX_FMT_YUV422P10;\r
THROW_ON_ERROR2(av_set_options_string(c->priv_data, options.c_str(), "=", ":"), "[ffmpeg_consumer]");\r
}\r
if(format_desc_.width < 1280 || format_desc_.height < 720)\r
BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("unsupported dimension"));\r
\r
- c->bit_rate = c->bit_rate > 0 ? c->bit_rate : 220*1000000;\r
+ c->bit_rate = 220*1000000;\r
c->pix_fmt = PIX_FMT_YUV422P;\r
\r
THROW_ON_ERROR2(av_set_options_string(c->priv_data, options.c_str(), "=", ":"), "[ffmpeg_consumer]");\r
}\r
else if(c->codec_id == CODEC_ID_DVVIDEO)\r
{\r
- c->bit_rate = c->bit_rate > 0 ? c->bit_rate : format_desc_.width < 1280 ? 50*1000000 : 100*1000000;\r
+ c->bit_rate = format_desc_.width < 1280 ? 50*1000000 : 100*1000000;\r
c->pix_fmt = PIX_FMT_YUV422P;\r
\r
+ c->width = format_desc_.height == 1280 ? 960 : c->width;\r
+\r
+ if(format_desc_.duration == 1001) \r
+ c->width = format_desc_.height == 1080 ? 1280 : c->width; \r
+ else\r
+ c->width = format_desc_.height == 1080 ? 1440 : c->width;\r
+ \r
THROW_ON_ERROR2(av_set_options_string(c->priv_data, options.c_str(), "=", ":"), "[ffmpeg_consumer]");\r
}\r
else if(c->codec_id == CODEC_ID_H264)\r
\r
std::shared_ptr<AVFrame> convert_video_frame(const safe_ptr<core::read_frame>& frame, AVCodecContext* c)\r
{\r
- if(!img_convert_ctx_) \r
+ if(!sws_) \r
{\r
- img_convert_ctx_.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
- if (img_convert_ctx_ == nullptr) \r
+ 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
+ if (sws_ == nullptr) \r
BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));\r
}\r
\r
picture_buf_.resize(avpicture_get_size(c->pix_fmt, format_desc_.width, format_desc_.height));\r
avpicture_fill(reinterpret_cast<AVPicture*>(local_av_frame.get()), picture_buf_.data(), c->pix_fmt, format_desc_.width, format_desc_.height);\r
\r
- sws_scale(img_convert_ctx_.get(), av_frame->data, av_frame->linesize, 0, c->height, local_av_frame->data, local_av_frame->linesize);\r
+ sws_scale(sws_.get(), av_frame->data, av_frame->linesize, 0, c->height, local_av_frame->data, local_av_frame->linesize);\r
\r
return local_av_frame;\r
}\r
{\r
const std::wstring filename_;\r
const bool key_only_;\r
- const std::string codec_;\r
- const std::string options_;\r
+ const std::wstring codec_;\r
+ const std::wstring options_;\r
\r
std::unique_ptr<ffmpeg_consumer> consumer_;\r
\r
public:\r
\r
- ffmpeg_consumer_proxy(const std::wstring& filename, bool key_only, const std::string codec, const std::string& options)\r
+ ffmpeg_consumer_proxy(const std::wstring& filename, bool key_only, const std::wstring codec, const std::wstring& options)\r
: filename_(filename)\r
, key_only_(key_only)\r
, codec_(boost::to_lower_copy(codec))\r
{\r
}\r
\r
- virtual void initialize(const core::video_format_desc& format_desc, int, int)\r
+ virtual void initialize(const core::video_format_desc& format_desc, int)\r
{\r
consumer_.reset();\r
- consumer_.reset(new ffmpeg_consumer(narrow(filename_), format_desc, codec_, options_));\r
+ consumer_.reset(new ffmpeg_consumer(narrow(filename_), format_desc, narrow(codec_), narrow(options_)));\r
}\r
\r
virtual bool send(const safe_ptr<core::read_frame>& frame) override\r
{\r
return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";\r
}\r
+\r
+ virtual boost::property_tree::wptree info() const override\r
+ {\r
+ boost::property_tree::wptree info;\r
+ info.add(L"type", L"ffmpeg-consumer");\r
+ info.add(L"key-only", key_only_);\r
+ info.add(L"filename", filename_);\r
+ info.add(L"codec", codec_);\r
+ info.add(L"options", options_);\r
+ return info;\r
+ }\r
\r
virtual bool has_synchronization_clock() const override\r
{\r
{\r
return 1;\r
}\r
+\r
+ virtual int index() const override\r
+ {\r
+ return 200;\r
+ }\r
}; \r
\r
-safe_ptr<core::frame_consumer> create_ffmpeg_consumer(const std::vector<std::wstring>& params)\r
+safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
{\r
- if(params.size() < 2 || params[0] != L"FILE")\r
+ if(params.size() < 1 || params[0] != L"FILE")\r
return core::frame_consumer::empty();\r
\r
- // TODO: Ask stakeholders about case where file already exists.\r
- boost::filesystem::remove(boost::filesystem::wpath(env::media_folder() + params[1])); // Delete the file if it exists\r
- bool key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end();\r
-\r
- std::string codec = "dnxhd";\r
- auto codec_it = std::find(params.begin(), params.end(), L"CODEC");\r
- if(codec_it != params.end() && codec_it++ != params.end())\r
- codec = narrow(*codec_it);\r
+ auto filename = (params.size() > 1 ? params[1] : L"");\r
+ bool key_only = get_param(L"KEY_ONLY", params, false);\r
+ auto codec = get_param(L"CODEC", params, L"libx264");\r
+ auto options = get_param(L"OPTIONS", params);\r
+ \r
+ if(codec == L"H264")\r
+ codec = L"libx264";\r
\r
- if(codec == "H264" || codec == "h264")\r
- codec = "libx264";\r
+ if(codec == L"DVCPRO")\r
+ codec = L"dvvideo";\r
\r
- std::string options = "";\r
- auto options_it = std::find(params.begin(), params.end(), L"OPTIONS");\r
- if(options_it != params.end() && options_it++ != params.end())\r
- options = narrow(*options_it);\r
+ boost::to_lower(options);\r
\r
- return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + params[1], key_only, codec, boost::to_lower_copy(options));\r
+ return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, key_only, codec, options);\r
}\r
\r
-safe_ptr<core::frame_consumer> create_ffmpeg_consumer(const boost::property_tree::ptree& ptree)\r
+safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::ptree& ptree)\r
{\r
std::string filename = ptree.get<std::string>("path");\r
auto key_only = ptree.get("key-only", false);\r
- auto codec = ptree.get("codec", "dnxhd");\r
+ auto codec = ptree.get("codec", "libx264");\r
auto options = ptree.get("options", "");\r
\r
- return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + widen(filename), key_only, codec, options);\r
+ return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + widen(filename), key_only, widen(codec), widen(options));\r
}\r
\r
}}\r