]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
2.0.2: Changed ADD syntax. sub-index no longer required. e.g. ADD 1 SCREEN, REMOVE...
[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 //static int query_yadif_formats(AVFilterContext *ctx)\r
155 //{\r
156 //    static const int pix_fmts[] = {\r
157 //        PIX_FMT_YUV444P,\r
158 //        PIX_FMT_YUV422P,\r
159 //        PIX_FMT_YUV420P,\r
160 //        PIX_FMT_YUV410P,\r
161 //        PIX_FMT_YUV411P,\r
162 //        PIX_FMT_GRAY8,\r
163 //        PIX_FMT_YUVJ444P,\r
164 //        PIX_FMT_YUVJ422P,\r
165 //        PIX_FMT_YUVJ420P,\r
166 //        AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),\r
167 //        PIX_FMT_YUV440P,\r
168 //        PIX_FMT_YUVJ440P,\r
169 //        AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),\r
170 //        AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),\r
171 //        AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),\r
172 //        PIX_FMT_YUVA420P,\r
173 //        PIX_FMT_NONE\r
174 //    };\r
175 //    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));\r
176 //\r
177 //    return 0;\r
178 //}\r
179 //\r
180 //#pragma warning (push)\r
181 //#pragma warning (disable : 4706)\r
182 //void fix_yadif_filter_format_query()\r
183 //{\r
184 //      AVFilter** filter = nullptr;\r
185 //    while((filter = av_filter_next(filter)) && *filter)\r
186 //      {\r
187 //              if(strstr((*filter)->name, "yadif") != 0)\r
188 //                      (*filter)->query_formats = query_yadif_formats;\r
189 //      }\r
190 //}\r
191 //#pragma warning (pop)\r
192 \r
193 void init()\r
194 {\r
195     avfilter_register_all();\r
196         //fix_yadif_filter_format_query();\r
197         av_register_all();\r
198         avcodec_init();\r
199     avcodec_register_all();\r
200         av_lockmgr_register(ffmpeg_lock_callback);\r
201         av_log_set_callback(log_callback);\r
202         \r
203         core::register_consumer_factory([](const std::vector<std::wstring>& params){return create_consumer(params);});\r
204         core::register_producer_factory(create_producer);\r
205 }\r
206 \r
207 void uninit()\r
208 {\r
209         avfilter_uninit();\r
210         av_lockmgr_register(nullptr);\r
211 }\r
212 \r
213 std::wstring make_version(unsigned int ver)\r
214 {\r
215         std::wstringstream str;\r
216         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);\r
217         return str.str();\r
218 }\r
219 \r
220 std::wstring get_avcodec_version()\r
221 {\r
222         return make_version(avcodec_version());\r
223 }\r
224 \r
225 std::wstring get_avformat_version()\r
226 {\r
227         return make_version(avformat_version());\r
228 }\r
229 \r
230 std::wstring get_avutil_version()\r
231 {\r
232         return make_version(avutil_version());\r
233 }\r
234 \r
235 std::wstring get_avfilter_version()\r
236 {\r
237         return make_version(avfilter_version());\r
238 }\r
239 \r
240 std::wstring get_swscale_version()\r
241 {\r
242         return make_version(swscale_version());\r
243 }\r
244 \r
245 }}