]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
2.1.0: Reverted utf8. To complicated to fully fix for now.
[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         if(level == AV_LOG_DEBUG)\r
140                 CASPAR_LOG(debug) << L"[ffmpeg] " << line;\r
141         else if(level == AV_LOG_INFO)\r
142                 CASPAR_LOG(info) << L"[ffmpeg] " << line;\r
143         else if(level == AV_LOG_WARNING)\r
144                 CASPAR_LOG(warning) << L"[ffmpeg] " << line;\r
145         else if(level == AV_LOG_ERROR)\r
146                 CASPAR_LOG(error) << L"[ffmpeg] " << line;\r
147         else if(level == AV_LOG_FATAL)\r
148                 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;\r
149         else\r
150                 CASPAR_LOG(trace) << L"[ffmpeg] " << line;\r
151 \r
152     //colored_fputs(av_clip(level>>3, 0, 6), line);\r
153 }\r
154 \r
155 //static int query_yadif_formats(AVFilterContext *ctx)\r
156 //{\r
157 //    static const int pix_fmts[] = {\r
158 //        PIX_FMT_YUV444P,\r
159 //        PIX_FMT_YUV422P,\r
160 //        PIX_FMT_YUV420P,\r
161 //        PIX_FMT_YUV410P,\r
162 //        PIX_FMT_YUV411P,\r
163 //        PIX_FMT_GRAY8,\r
164 //        PIX_FMT_YUVJ444P,\r
165 //        PIX_FMT_YUVJ422P,\r
166 //        PIX_FMT_YUVJ420P,\r
167 //        AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),\r
168 //        PIX_FMT_YUV440P,\r
169 //        PIX_FMT_YUVJ440P,\r
170 //        AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),\r
171 //        AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),\r
172 //        AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),\r
173 //        PIX_FMT_YUVA420P,\r
174 //        PIX_FMT_NONE\r
175 //    };\r
176 //    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));\r
177 //\r
178 //    return 0;\r
179 //}\r
180 //\r
181 //#pragma warning (push)\r
182 //#pragma warning (disable : 4706)\r
183 //void fix_yadif_filter_format_query()\r
184 //{\r
185 //      AVFilter** filter = nullptr;\r
186 //    while((filter = av_filter_next(filter)) && *filter)\r
187 //      {\r
188 //              if(strstr((*filter)->name, "yadif") != 0)\r
189 //                      (*filter)->query_formats = query_yadif_formats;\r
190 //      }\r
191 //}\r
192 //#pragma warning (pop)\r
193 \r
194 void init()\r
195 {\r
196     avfilter_register_all();\r
197         //fix_yadif_filter_format_query();\r
198         av_register_all();
199     avformat_network_init();\r
200         avcodec_init();\r
201     avcodec_register_all();\r
202         av_lockmgr_register(ffmpeg_lock_callback);\r
203         av_log_set_callback(log_callback);\r
204         \r
205         core::register_consumer_factory([](const std::vector<std::wstring>& params){return create_consumer(params);});\r
206         core::register_producer_factory(create_producer);\r
207 }\r
208 \r
209 void uninit()\r
210 {\r
211         avfilter_uninit();
212     avformat_network_deinit();\r
213         av_lockmgr_register(nullptr);\r
214 }\r
215 \r
216 std::wstring make_version(unsigned int ver)\r
217 {\r
218         std::wstringstream str;\r
219         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);\r
220         return str.str();\r
221 }\r
222 \r
223 std::wstring get_avcodec_version()\r
224 {\r
225         return make_version(avcodec_version());\r
226 }\r
227 \r
228 std::wstring get_avformat_version()\r
229 {\r
230         return make_version(avformat_version());\r
231 }\r
232 \r
233 std::wstring get_avutil_version()\r
234 {\r
235         return make_version(avutil_version());\r
236 }\r
237 \r
238 std::wstring get_avfilter_version()\r
239 {\r
240         return make_version(avfilter_version());\r
241 }\r
242 \r
243 std::wstring get_swscale_version()\r
244 {\r
245         return make_version(swscale_version());\r
246 }\r
247 \r
248 }}