]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
FFmpeg: reenabled curiously disabled logging suppression during thumbnail genera...
[casparcg] / modules / ffmpeg / ffmpeg.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\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
10 *\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
15 *\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
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "StdAfx.h"\r
23 \r
24 #include "consumer/ffmpeg_consumer.h"\r
25 #include "producer/ffmpeg_producer.h"\r
26 #include "producer/util/util.h"\r
27 \r
28 #include <common/log/log.h>\r
29 #include <common/exception/win32_exception.h>\r
30 \r
31 #include <core/parameters/parameters.h>\r
32 #include <core/consumer/frame_consumer.h>\r
33 #include <core/producer/frame_producer.h>\r
34 #include <core/producer/media_info/media_info.h>\r
35 #include <core/producer/media_info/media_info_repository.h>\r
36 \r
37 #include <tbb/recursive_mutex.h>\r
38 \r
39 #include <boost/thread.hpp>\r
40 \r
41 #if defined(_MSC_VER)\r
42 #pragma warning (disable : 4244)\r
43 #pragma warning (disable : 4603)\r
44 #pragma warning (disable : 4996)\r
45 #endif\r
46 \r
47 extern "C" \r
48 {\r
49         #define __STDC_CONSTANT_MACROS\r
50         #define __STDC_LIMIT_MACROS\r
51         #include <libavformat/avformat.h>\r
52         #include <libswscale/swscale.h>\r
53         #include <libavutil/avutil.h>\r
54         #include <libavfilter/avfilter.h>\r
55         #include <libavdevice/avdevice.h>\r
56 }\r
57 \r
58 namespace caspar { namespace ffmpeg {\r
59         \r
60 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op) \r
61\r
62         win32_exception::ensure_handler_installed_for_thread("ffmpeg-thread");\r
63         if(!mutex)\r
64                 return 0;\r
65 \r
66         auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);\r
67         \r
68         switch(op) \r
69         { \r
70                 case AV_LOCK_CREATE: \r
71                 { \r
72                         *mutex = new tbb::recursive_mutex(); \r
73                         break; \r
74                 } \r
75                 case AV_LOCK_OBTAIN: \r
76                 { \r
77                         if(my_mutex)\r
78                                 my_mutex->lock(); \r
79                         break; \r
80                 } \r
81                 case AV_LOCK_RELEASE: \r
82                 { \r
83                         if(my_mutex)\r
84                                 my_mutex->unlock(); \r
85                         break; \r
86                 } \r
87                 case AV_LOCK_DESTROY: \r
88                 { \r
89                         delete my_mutex;\r
90                         *mutex = nullptr;\r
91                         break; \r
92                 } \r
93         } \r
94         return 0; \r
95\r
96 \r
97 static void sanitize(uint8_t *line)\r
98 {\r
99     while(*line)\r
100         {\r
101         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))\r
102             *line='?';\r
103         line++;\r
104     }\r
105 }\r
106 \r
107 void log_callback(void* ptr, int level, const char* fmt, va_list vl)\r
108 {\r
109     static int print_prefix=1;\r
110     static int count;\r
111     static char prev[1024];\r
112     char line[8192];\r
113     static int is_atty;\r
114     AVClass* avc= ptr ? *(AVClass**)ptr : NULL;\r
115     if(level > av_log_get_level())\r
116         return;\r
117     line[0]=0;\r
118         \r
119 #undef fprintf\r
120     if(print_prefix && avc) \r
121         {\r
122         if (avc->parent_log_context_offset) \r
123                 {\r
124             AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);\r
125             if(parent && *parent)\r
126                 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);            \r
127         }\r
128         std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);\r
129     }\r
130 \r
131     std::vsprintf(line + strlen(line), fmt, vl);\r
132 \r
133     print_prefix = strlen(line) && line[strlen(line)-1] == '\n';\r
134         \r
135     //if(print_prefix && !strcmp(line, prev)){\r
136     //    count++;\r
137     //    if(is_atty==1)\r
138     //        fprintf(stderr, "    Last message repeated %d times\r", count);\r
139     //    return;\r
140     //}\r
141     //if(count>0){\r
142     //    fprintf(stderr, "    Last message repeated %d times\n", count);\r
143     //    count=0;\r
144     //}\r
145     strcpy(prev, line);\r
146     sanitize((uint8_t*)line);\r
147 \r
148         int len = strlen(line);\r
149         if(len > 0)\r
150                 line[len-1] = 0;\r
151         \r
152         if(level == AV_LOG_DEBUG)\r
153                 CASPAR_LOG(debug) << L"[ffmpeg] " << line;\r
154         else if(level == AV_LOG_INFO)\r
155                 CASPAR_LOG(info) << L"[ffmpeg] " << line;\r
156         else if(level == AV_LOG_WARNING)\r
157                 CASPAR_LOG(warning) << L"[ffmpeg] " << line;\r
158         else if(level == AV_LOG_ERROR)\r
159                 CASPAR_LOG(error) << L"[ffmpeg] " << line;\r
160         else if(level == AV_LOG_FATAL)\r
161                 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;\r
162         else\r
163                 CASPAR_LOG(trace) << L"[ffmpeg] " << line;\r
164 \r
165     //colored_fputs(av_clip(level>>3, 0, 6), line);\r
166 }\r
167 \r
168 boost::thread_specific_ptr<bool>& get_disable_logging_for_thread()\r
169 {\r
170         static boost::thread_specific_ptr<bool> disable_logging_for_thread;\r
171 \r
172         return disable_logging_for_thread;\r
173 }\r
174 \r
175 void disable_logging_for_thread()\r
176 {\r
177         if (get_disable_logging_for_thread().get() == nullptr)\r
178                 get_disable_logging_for_thread().reset(new bool); // bool value does not matter\r
179 }\r
180 \r
181 bool is_logging_already_disabled_for_thread()\r
182 {\r
183         return get_disable_logging_for_thread().get() != nullptr;\r
184 }\r
185 \r
186 std::shared_ptr<void> temporary_disable_logging_for_thread(bool disable)\r
187 {\r
188         if (!disable || is_logging_already_disabled_for_thread())\r
189                 return std::shared_ptr<void>();\r
190 \r
191         disable_logging_for_thread();\r
192 \r
193         return std::shared_ptr<void>(nullptr, [] (void*)\r
194         {\r
195                 get_disable_logging_for_thread().release(); // Only works correctly if destructed in same thread as original caller.\r
196         });\r
197 }\r
198 \r
199 void log_for_thread(void* ptr, int level, const char* fmt, va_list vl)\r
200 {\r
201         win32_exception::ensure_handler_installed_for_thread("ffmpeg-thread");\r
202         if (get_disable_logging_for_thread().get() == nullptr) // It does not matter what the value of the bool is\r
203                 log_callback(ptr, level, fmt, vl);\r
204 }\r
205 \r
206 //static int query_yadif_formats(AVFilterContext *ctx)\r
207 //{\r
208 //    static const int pix_fmts[] = {\r
209 //        PIX_FMT_YUV444P,\r
210 //        PIX_FMT_YUV422P,\r
211 //        PIX_FMT_YUV420P,\r
212 //        PIX_FMT_YUV410P,\r
213 //        PIX_FMT_YUV411P,\r
214 //        PIX_FMT_GRAY8,\r
215 //        PIX_FMT_YUVJ444P,\r
216 //        PIX_FMT_YUVJ422P,\r
217 //        PIX_FMT_YUVJ420P,\r
218 //        AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),\r
219 //        PIX_FMT_YUV440P,\r
220 //        PIX_FMT_YUVJ440P,\r
221 //        AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),\r
222 //        AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),\r
223 //        AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),\r
224 //        PIX_FMT_YUVA420P,\r
225 //        PIX_FMT_NONE\r
226 //    };\r
227 //    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));\r
228 //\r
229 //    return 0;\r
230 //}\r
231 //\r
232 //#pragma warning (push)\r
233 //#pragma warning (disable : 4706)\r
234 //void fix_yadif_filter_format_query()\r
235 //{\r
236 //      AVFilter** filter = nullptr;\r
237 //    while((filter = av_filter_next(filter)) && *filter)\r
238 //      {\r
239 //              if(strstr((*filter)->name, "yadif") != 0)\r
240 //                      (*filter)->query_formats = query_yadif_formats;\r
241 //      }\r
242 //}\r
243 //#pragma warning (pop)\r
244 \r
245 void init(const safe_ptr<core::media_info_repository>& media_info_repo)\r
246 {\r
247         av_lockmgr_register(ffmpeg_lock_callback);\r
248         av_log_set_callback(log_for_thread);\r
249                 \r
250     avcodec_register_all();     \r
251     avdevice_register_all();\r
252     avfilter_register_all();\r
253     av_register_all();\r
254     avformat_network_init();\r
255         \r
256         core::register_consumer_factory([](const core::parameters& params){return ffmpeg::create_consumer(params);});\r
257         core::register_producer_factory(create_producer);\r
258         core::register_thumbnail_producer_factory(create_thumbnail_producer);\r
259 \r
260         media_info_repo->register_extractor(\r
261                         [](const std::wstring& file, core::media_info& info) -> bool\r
262                         {\r
263                                 auto disable_logging = temporary_disable_logging_for_thread(true);\r
264 \r
265                                 return is_valid_file(file) && try_get_duration(file, info.duration, info.time_base);\r
266                         });\r
267 }\r
268 \r
269 void uninit()\r
270 {\r
271         avfilter_uninit();\r
272     avformat_network_deinit();\r
273         av_lockmgr_register(nullptr);\r
274 }\r
275 \r
276 std::wstring make_version(unsigned int ver)\r
277 {\r
278         std::wstringstream str;\r
279         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);\r
280         return str.str();\r
281 }\r
282 \r
283 std::wstring get_avcodec_version()\r
284 {\r
285         return make_version(avcodec_version());\r
286 }\r
287 \r
288 std::wstring get_avformat_version()\r
289 {\r
290         return make_version(avformat_version());\r
291 }\r
292 \r
293 std::wstring get_avutil_version()\r
294 {\r
295         return make_version(avutil_version());\r
296 }\r
297 \r
298 std::wstring get_avfilter_version()\r
299 {\r
300         return make_version(avfilter_version());\r
301 }\r
302 \r
303 std::wstring get_swscale_version()\r
304 {\r
305         return make_version(swscale_version());\r
306 }\r
307 \r
308 }}