2 * Copyright 2013 Sveriges Television AB http://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
24 #include "consumer/ffmpeg_consumer.h"
\r
25 #include "consumer/streaming_consumer.h"
\r
26 #include "producer/ffmpeg_producer.h"
\r
27 #include "producer/util/util.h"
\r
29 #include <common/log/log.h>
\r
30 #include <common/exception/win32_exception.h>
\r
32 #include <core/parameters/parameters.h>
\r
33 #include <core/consumer/frame_consumer.h>
\r
34 #include <core/producer/frame_producer.h>
\r
35 #include <core/producer/media_info/media_info.h>
\r
36 #include <core/producer/media_info/media_info_repository.h>
\r
38 #include <tbb/recursive_mutex.h>
\r
40 #include <boost/thread.hpp>
\r
42 #if defined(_MSC_VER)
\r
43 #pragma warning (disable : 4244)
\r
44 #pragma warning (disable : 4603)
\r
45 #pragma warning (disable : 4996)
\r
50 #define __STDC_CONSTANT_MACROS
\r
51 #define __STDC_LIMIT_MACROS
\r
52 #include <libavformat/avformat.h>
\r
53 #include <libswscale/swscale.h>
\r
54 #include <libavutil/avutil.h>
\r
55 #include <libavfilter/avfilter.h>
\r
56 #include <libavdevice/avdevice.h>
\r
59 namespace caspar { namespace ffmpeg {
\r
61 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op)
\r
63 win32_exception::ensure_handler_installed_for_thread("ffmpeg-thread");
\r
67 auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);
\r
71 case AV_LOCK_CREATE:
\r
73 *mutex = new tbb::recursive_mutex();
\r
76 case AV_LOCK_OBTAIN:
\r
82 case AV_LOCK_RELEASE:
\r
85 my_mutex->unlock();
\r
88 case AV_LOCK_DESTROY:
\r
98 static void sanitize(uint8_t *line)
\r
102 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
\r
108 void log_callback(void* ptr, int level, const char* fmt, va_list vl)
\r
110 static int print_prefix=1;
\r
112 static char prev[1024];
\r
114 static int is_atty;
\r
115 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
\r
116 if(level > av_log_get_level())
\r
121 if(print_prefix && avc)
\r
123 if (avc->parent_log_context_offset)
\r
125 AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
\r
126 if(parent && *parent)
\r
127 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);
\r
129 std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
\r
132 std::vsprintf(line + strlen(line), fmt, vl);
\r
134 print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
\r
136 //if(print_prefix && !strcmp(line, prev)){
\r
139 // fprintf(stderr, " Last message repeated %d times\r", count);
\r
143 // fprintf(stderr, " Last message repeated %d times\n", count);
\r
146 strcpy(prev, line);
\r
147 sanitize((uint8_t*)line);
\r
149 int len = strlen(line);
\r
153 if(level == AV_LOG_DEBUG)
\r
154 CASPAR_LOG(debug) << L"[ffmpeg] " << line;
\r
155 else if(level == AV_LOG_INFO)
\r
156 CASPAR_LOG(info) << L"[ffmpeg] " << line;
\r
157 else if(level == AV_LOG_WARNING)
\r
158 CASPAR_LOG(warning) << L"[ffmpeg] " << line;
\r
159 else if(level == AV_LOG_ERROR)
\r
160 CASPAR_LOG(error) << L"[ffmpeg] " << line;
\r
161 else if(level == AV_LOG_FATAL)
\r
162 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;
\r
164 CASPAR_LOG(trace) << L"[ffmpeg] " << line;
\r
166 //colored_fputs(av_clip(level>>3, 0, 6), line);
\r
169 boost::thread_specific_ptr<bool>& get_disable_logging_for_thread()
\r
171 static boost::thread_specific_ptr<bool> disable_logging_for_thread;
\r
173 return disable_logging_for_thread;
\r
176 void disable_logging_for_thread()
\r
178 if (get_disable_logging_for_thread().get() == nullptr)
\r
179 get_disable_logging_for_thread().reset(new bool); // bool value does not matter
\r
182 bool is_logging_already_disabled_for_thread()
\r
184 return get_disable_logging_for_thread().get() != nullptr;
\r
187 std::shared_ptr<void> temporary_disable_logging_for_thread(bool disable)
\r
189 if (!disable || is_logging_already_disabled_for_thread())
\r
190 return std::shared_ptr<void>();
\r
192 disable_logging_for_thread();
\r
194 return std::shared_ptr<void>(nullptr, [] (void*)
\r
196 get_disable_logging_for_thread().release(); // Only works correctly if destructed in same thread as original caller.
\r
200 void log_for_thread(void* ptr, int level, const char* fmt, va_list vl)
\r
202 win32_exception::ensure_handler_installed_for_thread("ffmpeg-thread");
\r
203 if (get_disable_logging_for_thread().get() == nullptr) // It does not matter what the value of the bool is
\r
204 log_callback(ptr, level, fmt, vl);
\r
207 //static int query_yadif_formats(AVFilterContext *ctx)
\r
209 // static const int pix_fmts[] = {
\r
210 // PIX_FMT_YUV444P,
\r
211 // PIX_FMT_YUV422P,
\r
212 // PIX_FMT_YUV420P,
\r
213 // PIX_FMT_YUV410P,
\r
214 // PIX_FMT_YUV411P,
\r
216 // PIX_FMT_YUVJ444P,
\r
217 // PIX_FMT_YUVJ422P,
\r
218 // PIX_FMT_YUVJ420P,
\r
219 // AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
\r
220 // PIX_FMT_YUV440P,
\r
221 // PIX_FMT_YUVJ440P,
\r
222 // AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
\r
223 // AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
\r
224 // AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
\r
225 // PIX_FMT_YUVA420P,
\r
228 // avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
\r
233 //#pragma warning (push)
\r
234 //#pragma warning (disable : 4706)
\r
235 //void fix_yadif_filter_format_query()
\r
237 // AVFilter** filter = nullptr;
\r
238 // while((filter = av_filter_next(filter)) && *filter)
\r
240 // if(strstr((*filter)->name, "yadif") != 0)
\r
241 // (*filter)->query_formats = query_yadif_formats;
\r
244 //#pragma warning (pop)
\r
246 void init(const safe_ptr<core::media_info_repository>& media_info_repo)
\r
248 av_lockmgr_register(ffmpeg_lock_callback);
\r
249 av_log_set_callback(log_for_thread);
\r
251 avcodec_register_all();
\r
252 avdevice_register_all();
\r
253 avfilter_register_all();
\r
255 avformat_network_init();
\r
257 core::register_consumer_factory([](const core::parameters& params){return ffmpeg::create_consumer(params);});
\r
258 core::register_consumer_factory([](const core::parameters& params){return ffmpeg::create_streaming_consumer(params);});
\r
259 core::register_producer_factory(create_producer);
\r
260 core::register_thumbnail_producer_factory(create_thumbnail_producer);
\r
262 media_info_repo->register_extractor(
\r
263 [](const std::wstring& file, core::media_info& info) -> bool
\r
265 auto disable_logging = temporary_disable_logging_for_thread(true);
\r
267 return is_valid_file(file) && try_get_duration(file, info.duration, info.time_base);
\r
274 avformat_network_deinit();
\r
275 av_lockmgr_register(nullptr);
\r
278 std::wstring make_version(unsigned int ver)
\r
280 std::wstringstream str;
\r
281 str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);
\r
285 std::wstring get_avcodec_version()
\r
287 return make_version(avcodec_version());
\r
290 std::wstring get_avformat_version()
\r
292 return make_version(avformat_version());
\r
295 std::wstring get_avutil_version()
\r
297 return make_version(avutil_version());
\r
300 std::wstring get_avfilter_version()
\r
302 return make_version(avfilter_version());
\r
305 std::wstring get_swscale_version()
\r
307 return make_version(swscale_version());
\r