]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
Merged relevant changes from pull #275
[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 "consumer/streaming_consumer.h"\r
26 #include "producer/ffmpeg_producer.h"\r
27 #include "producer/util/util.h"\r
28 \r
29 #include <common/log/log.h>\r
30 #include <common/exception/win32_exception.h>\r
31 \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
37 \r
38 #include <tbb/recursive_mutex.h>\r
39 \r
40 #include <boost/thread.hpp>\r
41 \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
46 #endif\r
47 \r
48 extern "C" \r
49 {\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
57 }\r
58 \r
59 namespace caspar { namespace ffmpeg {\r
60         \r
61 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op) \r
62\r
63         win32_exception::ensure_handler_installed_for_thread("ffmpeg-thread");\r
64         if(!mutex)\r
65                 return 0;\r
66 \r
67         auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);\r
68         \r
69         switch(op) \r
70         { \r
71                 case AV_LOCK_CREATE: \r
72                 { \r
73                         *mutex = new tbb::recursive_mutex(); \r
74                         break; \r
75                 } \r
76                 case AV_LOCK_OBTAIN: \r
77                 { \r
78                         if(my_mutex)\r
79                                 my_mutex->lock(); \r
80                         break; \r
81                 } \r
82                 case AV_LOCK_RELEASE: \r
83                 { \r
84                         if(my_mutex)\r
85                                 my_mutex->unlock(); \r
86                         break; \r
87                 } \r
88                 case AV_LOCK_DESTROY: \r
89                 { \r
90                         delete my_mutex;\r
91                         *mutex = nullptr;\r
92                         break; \r
93                 } \r
94         } \r
95         return 0; \r
96\r
97 \r
98 static void sanitize(uint8_t *line)\r
99 {\r
100     while(*line)\r
101         {\r
102         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))\r
103             *line='?';\r
104         line++;\r
105     }\r
106 }\r
107 \r
108 void log_callback(void* ptr, int level, const char* fmt, va_list vl)\r
109 {\r
110     static int print_prefix=1;\r
111     static int count;\r
112     static char prev[1024];\r
113     char line[8192];\r
114     static int is_atty;\r
115     AVClass* avc= ptr ? *(AVClass**)ptr : NULL;\r
116     if(level > av_log_get_level())\r
117         return;\r
118     line[0]=0;\r
119         \r
120 #undef fprintf\r
121     if(print_prefix && avc) \r
122         {\r
123         if (avc->parent_log_context_offset) \r
124                 {\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
128         }\r
129         std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);\r
130     }\r
131 \r
132     std::vsprintf(line + strlen(line), fmt, vl);\r
133 \r
134     print_prefix = strlen(line) && line[strlen(line)-1] == '\n';\r
135         \r
136     //if(print_prefix && !strcmp(line, prev)){\r
137     //    count++;\r
138     //    if(is_atty==1)\r
139     //        fprintf(stderr, "    Last message repeated %d times\r", count);\r
140     //    return;\r
141     //}\r
142     //if(count>0){\r
143     //    fprintf(stderr, "    Last message repeated %d times\n", count);\r
144     //    count=0;\r
145     //}\r
146     strcpy(prev, line);\r
147     sanitize((uint8_t*)line);\r
148 \r
149         int len = strlen(line);\r
150         if(len > 0)\r
151                 line[len-1] = 0;\r
152         \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
163         else\r
164                 CASPAR_LOG(trace) << L"[ffmpeg] " << line;\r
165 \r
166     //colored_fputs(av_clip(level>>3, 0, 6), line);\r
167 }\r
168 \r
169 boost::thread_specific_ptr<bool>& get_disable_logging_for_thread()\r
170 {\r
171         static boost::thread_specific_ptr<bool> disable_logging_for_thread;\r
172 \r
173         return disable_logging_for_thread;\r
174 }\r
175 \r
176 void disable_logging_for_thread()\r
177 {\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
180 }\r
181 \r
182 bool is_logging_already_disabled_for_thread()\r
183 {\r
184         return get_disable_logging_for_thread().get() != nullptr;\r
185 }\r
186 \r
187 std::shared_ptr<void> temporary_disable_logging_for_thread(bool disable)\r
188 {\r
189         if (!disable || is_logging_already_disabled_for_thread())\r
190                 return std::shared_ptr<void>();\r
191 \r
192         disable_logging_for_thread();\r
193 \r
194         return std::shared_ptr<void>(nullptr, [] (void*)\r
195         {\r
196                 get_disable_logging_for_thread().release(); // Only works correctly if destructed in same thread as original caller.\r
197         });\r
198 }\r
199 \r
200 void log_for_thread(void* ptr, int level, const char* fmt, va_list vl)\r
201 {\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
205 }\r
206 \r
207 //static int query_yadif_formats(AVFilterContext *ctx)\r
208 //{\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
215 //        PIX_FMT_GRAY8,\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
226 //        PIX_FMT_NONE\r
227 //    };\r
228 //    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));\r
229 //\r
230 //    return 0;\r
231 //}\r
232 //\r
233 //#pragma warning (push)\r
234 //#pragma warning (disable : 4706)\r
235 //void fix_yadif_filter_format_query()\r
236 //{\r
237 //      AVFilter** filter = nullptr;\r
238 //    while((filter = av_filter_next(filter)) && *filter)\r
239 //      {\r
240 //              if(strstr((*filter)->name, "yadif") != 0)\r
241 //                      (*filter)->query_formats = query_yadif_formats;\r
242 //      }\r
243 //}\r
244 //#pragma warning (pop)\r
245 \r
246 void init(const safe_ptr<core::media_info_repository>& media_info_repo)\r
247 {\r
248         av_lockmgr_register(ffmpeg_lock_callback);\r
249         av_log_set_callback(log_for_thread);\r
250                 \r
251     avcodec_register_all();     \r
252     avdevice_register_all();\r
253     avfilter_register_all();\r
254     av_register_all();\r
255     avformat_network_init();\r
256         \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
261 \r
262         media_info_repo->register_extractor(\r
263                         [](const std::wstring& file, core::media_info& info) -> bool\r
264                         {\r
265                                 auto disable_logging = temporary_disable_logging_for_thread(true);\r
266 \r
267                                 return is_valid_file(file) && try_get_duration(file, info.duration, info.time_base);\r
268                         });\r
269 }\r
270 \r
271 void uninit()\r
272 {\r
273         avfilter_uninit();\r
274     avformat_network_deinit();\r
275         av_lockmgr_register(nullptr);\r
276 }\r
277 \r
278 std::wstring make_version(unsigned int ver)\r
279 {\r
280         std::wstringstream str;\r
281         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);\r
282         return str.str();\r
283 }\r
284 \r
285 std::wstring get_avcodec_version()\r
286 {\r
287         return make_version(avcodec_version());\r
288 }\r
289 \r
290 std::wstring get_avformat_version()\r
291 {\r
292         return make_version(avformat_version());\r
293 }\r
294 \r
295 std::wstring get_avutil_version()\r
296 {\r
297         return make_version(avutil_version());\r
298 }\r
299 \r
300 std::wstring get_avfilter_version()\r
301 {\r
302         return make_version(avfilter_version());\r
303 }\r
304 \r
305 std::wstring get_swscale_version()\r
306 {\r
307         return make_version(swscale_version());\r
308 }\r
309 \r
310 }}