]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
2.0. Updated namespaces.
[casparcg] / modules / ffmpeg / ffmpeg.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 \r
21 #include "StdAfx.h"\r
22 \r
23 #include "consumer/ffmpeg_consumer.h"\r
24 #include "producer/ffmpeg_producer.h"\r
25 \r
26 #include <common/log/log.h>\r
27 \r
28 #include <core/consumer/frame_consumer.h>\r
29 #include <core/producer/frame_producer.h>\r
30 \r
31 #include <tbb/recursive_mutex.h>\r
32 \r
33 #if defined(_MSC_VER)\r
34 #pragma warning (disable : 4244)\r
35 #pragma warning (disable : 4603)\r
36 #pragma warning (disable : 4996)\r
37 #endif\r
38 \r
39 extern "C" \r
40 {\r
41         #define __STDC_CONSTANT_MACROS\r
42         #define __STDC_LIMIT_MACROS\r
43         #include <libavformat/avformat.h>\r
44         #include <libswscale/swscale.h>\r
45         #include <libavutil/avutil.h>\r
46         #include <libavfilter/avfilter.h>\r
47 }\r
48 \r
49 namespace caspar { namespace ffmpeg {\r
50         \r
51 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op) \r
52\r
53         if(!mutex)\r
54                 return 0;\r
55 \r
56         auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);\r
57         \r
58         switch(op) \r
59         { \r
60                 case AV_LOCK_CREATE: \r
61                 { \r
62                         *mutex = new tbb::recursive_mutex(); \r
63                         break; \r
64                 } \r
65                 case AV_LOCK_OBTAIN: \r
66                 { \r
67                         if(my_mutex)\r
68                                 my_mutex->lock(); \r
69                         break; \r
70                 } \r
71                 case AV_LOCK_RELEASE: \r
72                 { \r
73                         if(my_mutex)\r
74                                 my_mutex->unlock(); \r
75                         break; \r
76                 } \r
77                 case AV_LOCK_DESTROY: \r
78                 { \r
79                         delete my_mutex;\r
80                         *mutex = nullptr;\r
81                         break; \r
82                 } \r
83         } \r
84         return 0; \r
85\r
86 \r
87 static void sanitize(uint8_t *line)\r
88 {\r
89     while(*line)\r
90         {\r
91         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))\r
92             *line='?';\r
93         line++;\r
94     }\r
95 }\r
96 \r
97 void log_callback(void* ptr, int level, const char* fmt, va_list vl)\r
98 {\r
99     static int print_prefix=1;\r
100     static int count;\r
101     static char prev[1024];\r
102     char line[8192];\r
103     static int is_atty;\r
104     AVClass* avc= ptr ? *(AVClass**)ptr : NULL;\r
105     if(level > av_log_get_level())\r
106         return;\r
107     line[0]=0;\r
108         \r
109 #undef fprintf\r
110     if(print_prefix && avc) \r
111         {\r
112         if (avc->parent_log_context_offset) \r
113                 {\r
114             AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);\r
115             if(parent && *parent)\r
116                 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);            \r
117         }\r
118         std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);\r
119     }\r
120 \r
121     std::vsprintf(line + strlen(line), fmt, vl);\r
122 \r
123     print_prefix = strlen(line) && line[strlen(line)-1] == '\n';\r
124         \r
125     //if(print_prefix && !strcmp(line, prev)){\r
126     //    count++;\r
127     //    if(is_atty==1)\r
128     //        fprintf(stderr, "    Last message repeated %d times\r", count);\r
129     //    return;\r
130     //}\r
131     //if(count>0){\r
132     //    fprintf(stderr, "    Last message repeated %d times\n", count);\r
133     //    count=0;\r
134     //}\r
135     strcpy(prev, line);\r
136     sanitize((uint8_t*)line);\r
137         \r
138         if(level == AV_LOG_DEBUG)\r
139                 CASPAR_LOG(debug) << L"[ffmpeg] " << line;\r
140         else if(level == AV_LOG_INFO)\r
141                 CASPAR_LOG(info) << L"[ffmpeg] " << line;\r
142         else if(level == AV_LOG_WARNING)\r
143                 CASPAR_LOG(warning) << L"[ffmpeg] " << line;\r
144         else if(level == AV_LOG_ERROR)\r
145                 CASPAR_LOG(error) << L"[ffmpeg] " << line;\r
146         else if(level == AV_LOG_FATAL)\r
147                 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;\r
148         else\r
149                 CASPAR_LOG(trace) << L"[ffmpeg] " << line;\r
150 \r
151     //colored_fputs(av_clip(level>>3, 0, 6), line);\r
152 }\r
153 \r
154 void init()\r
155 {\r
156     avfilter_register_all();\r
157         av_register_all();\r
158         avcodec_init();\r
159     avcodec_register_all();\r
160         av_lockmgr_register(ffmpeg_lock_callback);\r
161         av_log_set_callback(log_callback);\r
162         \r
163         //core::register_consumer_factory([](const std::vector<std::wstring>& params){return create_ffmpeg_consumer(params);});\r
164         core::register_producer_factory(create_producer);\r
165 }\r
166 \r
167 void uninit()\r
168 {\r
169         avfilter_uninit();\r
170         av_lockmgr_register(nullptr);\r
171 }\r
172 \r
173 std::wstring make_version(unsigned int ver)\r
174 {\r
175         std::wstringstream str;\r
176         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);\r
177         return str.str();\r
178 }\r
179 \r
180 std::wstring get_avcodec_version()\r
181 {\r
182         return make_version(avcodec_version());\r
183 }\r
184 \r
185 std::wstring get_avformat_version()\r
186 {\r
187         return make_version(avformat_version());\r
188 }\r
189 \r
190 std::wstring get_avutil_version()\r
191 {\r
192         return make_version(avutil_version());\r
193 }\r
194 \r
195 std::wstring get_avfilter_version()\r
196 {\r
197         return make_version(avfilter_version());\r
198 }\r
199 \r
200 std::wstring get_swscale_version()\r
201 {\r
202         return make_version(swscale_version());\r
203 }\r
204 \r
205 }}