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 "consumer/streaming_consumer.h"
28 #include "producer/ffmpeg_producer.h"
29 #include "producer/util/util.h"
31 #include <common/log.h>
32 #include <common/os/general_protection_fault.h>
34 #include <core/consumer/frame_consumer.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>
43 #include <tbb/recursive_mutex.h>
46 #pragma warning (disable : 4244)
47 #pragma warning (disable : 4603)
48 #pragma warning (disable : 4996)
53 #define __STDC_CONSTANT_MACROS
54 #define __STDC_LIMIT_MACROS
55 #include <libavformat/avformat.h>
56 #include <libswscale/swscale.h>
57 #include <libavutil/avutil.h>
58 #include <libavfilter/avfilter.h>
61 namespace caspar { namespace ffmpeg {
63 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op)
68 auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);
74 *mutex = new tbb::recursive_mutex();
99 static void sanitize(uint8_t *line)
103 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
109 void log_callback(void* ptr, int level, const char* fmt, va_list vl)
111 static int print_prefix=1;
113 static char prev[1024];
115 //static int is_atty;
116 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
117 if(level > av_log_get_level())
122 if(print_prefix && avc)
124 if (avc->parent_log_context_offset)
126 AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
127 if(parent && *parent)
128 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);
130 std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
133 std::vsprintf(line + strlen(line), fmt, vl);
135 print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
137 //if(print_prefix && !strcmp(line, prev)){
140 // fprintf(stderr, " Last message repeated %d times\r", count);
144 // fprintf(stderr, " Last message repeated %d times\n", count);
148 sanitize((uint8_t*)line);
150 auto len = strlen(line);
154 if(level == AV_LOG_DEBUG)
155 CASPAR_LOG(debug) << L"[ffmpeg] " << line;
156 else if(level == AV_LOG_INFO)
157 CASPAR_LOG(info) << L"[ffmpeg] " << line;
158 else if(level == AV_LOG_WARNING)
159 CASPAR_LOG(warning) << L"[ffmpeg] " << line;
160 else if(level == AV_LOG_ERROR)
161 CASPAR_LOG(error) << L"[ffmpeg] " << line;
162 else if(level == AV_LOG_FATAL)
163 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;
165 CASPAR_LOG(trace) << L"[ffmpeg] " << line;
167 //colored_fputs(av_clip(level>>3, 0, 6), line);
170 //static int query_yadif_formats(AVFilterContext *ctx)
172 // static const int pix_fmts[] = {
182 // AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
185 // AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
186 // AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
187 // AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
191 // avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
196 //#pragma warning (push)
197 //#pragma warning (disable : 4706)
198 //void fix_yadif_filter_format_query()
200 // AVFilter** filter = nullptr;
201 // while((filter = av_filter_next(filter)) && *filter)
203 // if(strstr((*filter)->name, "yadif") != 0)
204 // (*filter)->query_formats = query_yadif_formats;
207 //#pragma warning (pop)
209 std::wstring make_version(unsigned int ver)
211 std::wstringstream str;
212 str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);
216 std::wstring avcodec_version()
218 return make_version(::avcodec_version());
221 std::wstring avformat_version()
223 return make_version(::avformat_version());
226 std::wstring avutil_version()
228 return make_version(::avutil_version());
231 std::wstring avfilter_version()
233 return make_version(::avfilter_version());
236 std::wstring swscale_version()
238 return make_version(::swscale_version());
240 bool& get_disable_logging_for_thread()
242 static boost::thread_specific_ptr<bool> disable_logging_for_thread;
244 auto local = disable_logging_for_thread.get();
248 local = new bool(false);
249 disable_logging_for_thread.reset(local);
255 void disable_logging_for_thread()
257 get_disable_logging_for_thread() = true;
260 bool is_logging_disabled_for_thread()
262 return get_disable_logging_for_thread();
265 std::shared_ptr<void> temporary_disable_logging_for_thread(bool disable)
267 if (!disable || is_logging_disabled_for_thread())
268 return std::shared_ptr<void>();
270 disable_logging_for_thread();
272 return std::shared_ptr<void>(nullptr, [](void*)
274 get_disable_logging_for_thread() = false; // Only works correctly if destructed in same thread as original caller.
278 void log_for_thread(void* ptr, int level, const char* fmt, va_list vl)
280 ensure_gpf_handler_installed_for_thread("ffmpeg-thread");
281 if (!get_disable_logging_for_thread()) // It does not matter what the value of the bool is
282 log_callback(ptr, level, fmt, vl);
285 void init(core::module_dependencies dependencies)
287 av_lockmgr_register(ffmpeg_lock_callback);
288 av_log_set_callback(log_for_thread);
290 avfilter_register_all();
291 //fix_yadif_filter_format_query();
293 avformat_network_init();
294 avcodec_register_all();
296 dependencies.consumer_registry->register_consumer_factory(L"FFMpeg Consumer", create_consumer, describe_consumer);
297 dependencies.consumer_registry->register_consumer_factory(L"Streaming Consumer", create_streaming_consumer, describe_streaming_consumer);
298 dependencies.consumer_registry->register_preconfigured_consumer_factory(L"file", create_preconfigured_consumer);
299 dependencies.consumer_registry->register_preconfigured_consumer_factory(L"stream", create_preconfigured_streaming_consumer);
300 dependencies.producer_registry->register_producer_factory(L"FFmpeg Producer", create_producer, describe_producer);
302 dependencies.media_info_repo->register_extractor(
303 [](const std::wstring& file, const std::wstring& extension, core::media_info& info) -> bool
305 // TODO: merge thumbnail generation from 2.0
306 //auto disable_logging = temporary_disable_logging_for_thread(true);
307 if (extension == L".WAV" || extension == L".MP3")
309 info.clip_type = L"AUDIO";
313 if (!is_valid_file(file))
316 info.clip_type = L"MOVIE";
318 return try_get_duration(file, info.duration, info.time_base);
320 dependencies.system_info_provider_repo->register_system_info_provider([](boost::property_tree::wptree& info)
322 info.add(L"system.ffmpeg.avcodec", avcodec_version());
323 info.add(L"system.ffmpeg.avformat", avformat_version());
324 info.add(L"system.ffmpeg.avfilter", avfilter_version());
325 info.add(L"system.ffmpeg.avutil", avutil_version());
326 info.add(L"system.ffmpeg.swscale", swscale_version());
333 avformat_network_deinit();
334 av_lockmgr_register(nullptr);