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