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