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
26 #include "consumer/ffmpeg_consumer.h"
27 #include "producer/ffmpeg_producer.h"
28 #include "producer/util/util.h"
30 #include <common/log.h>
31 #include <common/os/general_protection_fault.h>
33 #include <core/consumer/frame_consumer.h>
34 #include <core/frame/draw_frame.h>
35 #include <core/producer/frame_producer.h>
36 #include <core/producer/media_info/media_info.h>
37 #include <core/producer/media_info/media_info_repository.h>
38 #include <core/system_info_provider.h>
40 #include <boost/property_tree/ptree.hpp>
41 #include <boost/thread/tss.hpp>
42 #include <boost/bind.hpp>
44 #include <tbb/recursive_mutex.h>
47 #pragma warning (disable : 4244)
48 #pragma warning (disable : 4603)
49 #pragma warning (disable : 4996)
54 #define __STDC_CONSTANT_MACROS
55 #define __STDC_LIMIT_MACROS
56 #include <libavformat/avformat.h>
57 #include <libswscale/swscale.h>
58 #include <libavutil/avutil.h>
59 #include <libavfilter/avfilter.h>
60 #include <libavdevice/avdevice.h>
63 namespace caspar { namespace ffmpeg {
64 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op)
69 auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);
75 *mutex = new tbb::recursive_mutex();
100 static void sanitize(uint8_t *line)
104 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
110 void log_callback(void* ptr, int level, const char* fmt, va_list vl)
112 static int print_prefix=1;
113 static char prev[1024];
115 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
116 if (level > AV_LOG_DEBUG)
121 if(print_prefix && avc)
123 if (avc->parent_log_context_offset)
125 AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
126 if(parent && *parent)
127 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);
129 std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
132 std::vsprintf(line + strlen(line), fmt, vl);
134 print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
137 sanitize((uint8_t*)line);
139 auto len = strlen(line);
145 if (level == AV_LOG_VERBOSE)
146 CASPAR_LOG(debug) << L"[ffmpeg] " << line;
147 else if (level == AV_LOG_INFO)
148 CASPAR_LOG(info) << L"[ffmpeg] " << line;
149 else if (level == AV_LOG_WARNING)
150 CASPAR_LOG(warning) << L"[ffmpeg] " << line;
151 else if (level == AV_LOG_ERROR)
152 CASPAR_LOG(error) << L"[ffmpeg] " << line;
153 else if (level == AV_LOG_FATAL)
154 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;
156 CASPAR_LOG(trace) << L"[ffmpeg] " << line;
163 std::wstring make_version(unsigned int ver)
165 std::wstringstream str;
166 str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);
170 std::wstring avcodec_version()
172 return make_version(::avcodec_version());
175 std::wstring avformat_version()
177 return make_version(::avformat_version());
180 std::wstring avutil_version()
182 return make_version(::avutil_version());
185 std::wstring avfilter_version()
187 return make_version(::avfilter_version());
190 std::wstring swscale_version()
192 return make_version(::swscale_version());
194 bool& get_quiet_logging_for_thread()
196 static boost::thread_specific_ptr<bool> quiet_logging_for_thread;
198 auto local = quiet_logging_for_thread.get();
202 local = new bool(false);
203 quiet_logging_for_thread.reset(local);
209 void enable_quiet_logging_for_thread()
211 get_quiet_logging_for_thread() = true;
214 bool is_logging_quiet_for_thread()
216 return get_quiet_logging_for_thread();
219 std::shared_ptr<void> temporary_enable_quiet_logging_for_thread(bool enable)
221 if (!enable || is_logging_quiet_for_thread())
222 return std::shared_ptr<void>();
224 get_quiet_logging_for_thread() = true;
226 return std::shared_ptr<void>(nullptr, [](void*)
228 get_quiet_logging_for_thread() = false; // Only works correctly if destructed in same thread as original caller.
232 void log_for_thread(void* ptr, int level, const char* fmt, va_list vl)
234 ensure_gpf_handler_installed_for_thread("ffmpeg-thread");
236 int min_level = is_logging_quiet_for_thread() ? AV_LOG_DEBUG : AV_LOG_FATAL;
238 log_callback(ptr, std::max(level, min_level), fmt, vl);
241 void init(core::module_dependencies dependencies)
243 av_lockmgr_register(ffmpeg_lock_callback);
244 av_log_set_callback(log_for_thread);
246 avfilter_register_all();
247 //fix_yadif_filter_format_query();
249 avformat_network_init();
250 avcodec_register_all();
251 avdevice_register_all();
253 auto info_repo = dependencies.media_info_repo;
255 dependencies.consumer_registry->register_consumer_factory(L"FFmpeg Consumer", create_ffmpeg_consumer, describe_ffmpeg_consumer);
256 dependencies.consumer_registry->register_preconfigured_consumer_factory(L"ffmpeg", create_preconfigured_ffmpeg_consumer);
257 dependencies.producer_registry->register_producer_factory(L"FFmpeg Producer", boost::bind(&create_producer, _1, _2, info_repo), describe_producer);
258 dependencies.producer_registry->register_thumbnail_producer(boost::bind(&create_thumbnail_frame, _1, _2, info_repo));
260 info_repo->register_extractor(
261 [](const std::wstring& file, const std::wstring& extension, core::media_info& info) -> bool
263 auto quiet_logging = temporary_enable_quiet_logging_for_thread(true);
264 if (extension == L".WAV" || extension == L".MP3")
266 info.clip_type = L"AUDIO";
270 if (!is_valid_file(file, true))
273 info.clip_type = L"MOVIE";
275 return try_get_duration(file, info.duration, info.time_base);
277 dependencies.system_info_provider_repo->register_system_info_provider([](boost::property_tree::wptree& info)
279 info.add(L"system.ffmpeg.avcodec", avcodec_version());
280 info.add(L"system.ffmpeg.avformat", avformat_version());
281 info.add(L"system.ffmpeg.avfilter", avfilter_version());
282 info.add(L"system.ffmpeg.avutil", avutil_version());
283 info.add(L"system.ffmpeg.swscale", swscale_version());
290 avformat_network_deinit();
291 av_lockmgr_register(nullptr);