]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
2.0. ffmpeg: Header file optimization and fixes.
[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 {\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         CASPAR_LOG(trace) << L"[FFMPEG] [" << av_clip(level>>3, 0, 6) << L"] " << line;\r
139 \r
140     //colored_fputs(av_clip(level>>3, 0, 6), line);\r
141 }\r
142 \r
143 void init_ffmpeg()\r
144 {\r
145     avfilter_register_all();\r
146         av_register_all();\r
147         avcodec_init();\r
148     avcodec_register_all();\r
149         av_lockmgr_register(ffmpeg_lock_callback);\r
150         av_log_set_callback(log_callback);\r
151         \r
152         //core::register_consumer_factory([](const std::vector<std::wstring>& params){return create_ffmpeg_consumer(params);});\r
153         core::register_producer_factory(create_ffmpeg_producer);\r
154 }\r
155 \r
156 void uninit_ffmpeg()\r
157 {\r
158         avfilter_uninit();\r
159         av_lockmgr_register(nullptr);\r
160 }\r
161 \r
162 std::wstring make_version(unsigned int ver)\r
163 {\r
164         std::wstringstream str;\r
165         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);\r
166         return str.str();\r
167 }\r
168 \r
169 std::wstring get_avcodec_version()\r
170 {\r
171         return make_version(avcodec_version());\r
172 }\r
173 \r
174 std::wstring get_avformat_version()\r
175 {\r
176         return make_version(avformat_version());\r
177 }\r
178 \r
179 std::wstring get_avutil_version()\r
180 {\r
181         return make_version(avutil_version());\r
182 }\r
183 \r
184 std::wstring get_avfilter_version()\r
185 {\r
186         return make_version(avfilter_version());\r
187 }\r
188 \r
189 std::wstring get_swscale_version()\r
190 {\r
191         return make_version(swscale_version());\r
192 }\r
193 \r
194 }