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
24 #include "consumer/ffmpeg_consumer.h"
\r
25 #include "producer/ffmpeg_producer.h"
\r
27 #include <common/log/log.h>
\r
29 #include <core/consumer/frame_consumer.h>
\r
30 #include <core/producer/frame_producer.h>
\r
32 #include <tbb/recursive_mutex.h>
\r
34 #include <boost/thread.hpp>
\r
36 #if defined(_MSC_VER)
\r
37 #pragma warning (disable : 4244)
\r
38 #pragma warning (disable : 4603)
\r
39 #pragma warning (disable : 4996)
\r
44 #define __STDC_CONSTANT_MACROS
\r
45 #define __STDC_LIMIT_MACROS
\r
46 #include <libavformat/avformat.h>
\r
47 #include <libswscale/swscale.h>
\r
48 #include <libavutil/avutil.h>
\r
49 #include <libavfilter/avfilter.h>
\r
52 namespace caspar { namespace ffmpeg {
\r
54 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op)
\r
59 auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);
\r
63 case AV_LOCK_CREATE:
\r
65 *mutex = new tbb::recursive_mutex();
\r
68 case AV_LOCK_OBTAIN:
\r
74 case AV_LOCK_RELEASE:
\r
77 my_mutex->unlock();
\r
80 case AV_LOCK_DESTROY:
\r
90 static void sanitize(uint8_t *line)
\r
94 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
\r
100 void log_callback(void* ptr, int level, const char* fmt, va_list vl)
\r
102 static int print_prefix=1;
\r
104 static char prev[1024];
\r
106 static int is_atty;
\r
107 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
\r
108 if(level > av_log_get_level())
\r
113 if(print_prefix && avc)
\r
115 if (avc->parent_log_context_offset)
\r
117 AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
\r
118 if(parent && *parent)
\r
119 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);
\r
121 std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
\r
124 std::vsprintf(line + strlen(line), fmt, vl);
\r
126 print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
\r
128 //if(print_prefix && !strcmp(line, prev)){
\r
131 // fprintf(stderr, " Last message repeated %d times\r", count);
\r
135 // fprintf(stderr, " Last message repeated %d times\n", count);
\r
138 strcpy(prev, line);
\r
139 sanitize((uint8_t*)line);
\r
141 int len = strlen(line);
\r
145 if(level == AV_LOG_DEBUG)
\r
146 CASPAR_LOG(debug) << L"[ffmpeg] " << line;
\r
147 else if(level == AV_LOG_INFO)
\r
148 CASPAR_LOG(info) << L"[ffmpeg] " << line;
\r
149 else if(level == AV_LOG_WARNING)
\r
150 CASPAR_LOG(warning) << L"[ffmpeg] " << line;
\r
151 else if(level == AV_LOG_ERROR)
\r
152 CASPAR_LOG(error) << L"[ffmpeg] " << line;
\r
153 else if(level == AV_LOG_FATAL)
\r
154 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;
\r
156 CASPAR_LOG(trace) << L"[ffmpeg] " << line;
\r
158 //colored_fputs(av_clip(level>>3, 0, 6), line);
\r
161 boost::thread_specific_ptr<bool>& get_disable_logging_for_thread()
\r
163 static boost::thread_specific_ptr<bool> disable_logging_for_thread;
\r
165 return disable_logging_for_thread;
\r
168 void disable_logging_for_thread()
\r
170 if (get_disable_logging_for_thread().get() == nullptr)
\r
171 get_disable_logging_for_thread().reset(new bool); // bool value does not matter
\r
174 bool is_logging_already_disabled_for_thread()
\r
176 return get_disable_logging_for_thread().get() != nullptr;
\r
179 std::shared_ptr<void> temporary_disable_logging_for_thread(bool disable)
\r
181 if (!disable || is_logging_already_disabled_for_thread())
\r
182 return std::shared_ptr<void>();
\r
184 disable_logging_for_thread();
\r
186 return std::shared_ptr<void>(nullptr, [] (void*)
\r
188 get_disable_logging_for_thread().release(); // Only works correctly if destructed in same thread as original caller.
\r
192 void log_for_thread(void* ptr, int level, const char* fmt, va_list vl)
\r
194 //if (get_disable_logging_for_thread().get() == nullptr) // It does not matter what the value of the bool is
\r
195 log_callback(ptr, level, fmt, vl);
\r
198 //static int query_yadif_formats(AVFilterContext *ctx)
\r
200 // static const int pix_fmts[] = {
\r
201 // PIX_FMT_YUV444P,
\r
202 // PIX_FMT_YUV422P,
\r
203 // PIX_FMT_YUV420P,
\r
204 // PIX_FMT_YUV410P,
\r
205 // PIX_FMT_YUV411P,
\r
207 // PIX_FMT_YUVJ444P,
\r
208 // PIX_FMT_YUVJ422P,
\r
209 // PIX_FMT_YUVJ420P,
\r
210 // AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
\r
211 // PIX_FMT_YUV440P,
\r
212 // PIX_FMT_YUVJ440P,
\r
213 // AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
\r
214 // AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
\r
215 // AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
\r
216 // PIX_FMT_YUVA420P,
\r
219 // avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
\r
224 //#pragma warning (push)
\r
225 //#pragma warning (disable : 4706)
\r
226 //void fix_yadif_filter_format_query()
\r
228 // AVFilter** filter = nullptr;
\r
229 // while((filter = av_filter_next(filter)) && *filter)
\r
231 // if(strstr((*filter)->name, "yadif") != 0)
\r
232 // (*filter)->query_formats = query_yadif_formats;
\r
235 //#pragma warning (pop)
\r
239 av_lockmgr_register(ffmpeg_lock_callback);
\r
240 av_log_set_callback(log_for_thread);
\r
242 avfilter_register_all();
\r
243 //fix_yadif_filter_format_query();
\r
245 avformat_network_init();
\r
247 avcodec_register_all();
\r
249 core::register_consumer_factory([](const std::vector<std::wstring>& params){return create_consumer(params);});
\r
250 core::register_producer_factory(create_producer);
\r
251 core::register_thumbnail_producer_factory(create_thumbnail_producer);
\r
257 avformat_network_deinit();
\r
258 av_lockmgr_register(nullptr);
\r
261 std::wstring make_version(unsigned int ver)
\r
263 std::wstringstream str;
\r
264 str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);
\r
268 std::wstring get_avcodec_version()
\r
270 return make_version(avcodec_version());
\r
273 std::wstring get_avformat_version()
\r
275 return make_version(avformat_version());
\r
278 std::wstring get_avutil_version()
\r
280 return make_version(avutil_version());
\r
283 std::wstring get_avfilter_version()
\r
285 return make_version(avfilter_version());
\r
288 std::wstring get_swscale_version()
\r
290 return make_version(swscale_version());
\r