]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
#405 Fixed typo.
[casparcg] / modules / ffmpeg / ffmpeg.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "StdAfx.h"
23
24 #include "ffmpeg.h"
25
26 #include "consumer/ffmpeg_consumer.h"
27 #include "consumer/streaming_consumer.h"
28 #include "producer/ffmpeg_producer.h"
29 #include "producer/util/util.h"
30
31 #include <common/log.h>
32 #include <common/os/general_protection_fault.h>
33
34 #include <core/consumer/frame_consumer.h>
35 #include <core/producer/frame_producer.h>
36 #include <core/producer/media_info/media_info.h>
37 #include <core/producer/media_info/media_info_repository.h>
38 #include <core/system_info_provider.h>
39
40 #include <boost/property_tree/ptree.hpp>
41 #include <boost/thread/tss.hpp>
42 #include <boost/bind.hpp>
43
44 #include <tbb/recursive_mutex.h>
45
46 #if defined(_MSC_VER)
47 #pragma warning (disable : 4244)
48 #pragma warning (disable : 4603)
49 #pragma warning (disable : 4996)
50 #endif
51
52 extern "C" 
53 {
54         #define __STDC_CONSTANT_MACROS
55         #define __STDC_LIMIT_MACROS
56         #include <libavformat/avformat.h>
57         #include <libswscale/swscale.h>
58         #include <libavutil/avutil.h>
59         #include <libavfilter/avfilter.h>
60 }
61
62 namespace caspar { namespace ffmpeg {
63         
64 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op) 
65
66         if(!mutex)
67                 return 0;
68
69         auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);
70         
71         switch(op) 
72         { 
73                 case AV_LOCK_CREATE: 
74                 { 
75                         *mutex = new tbb::recursive_mutex(); 
76                         break; 
77                 } 
78                 case AV_LOCK_OBTAIN: 
79                 { 
80                         if(my_mutex)
81                                 my_mutex->lock(); 
82                         break; 
83                 } 
84                 case AV_LOCK_RELEASE: 
85                 { 
86                         if(my_mutex)
87                                 my_mutex->unlock(); 
88                         break; 
89                 } 
90                 case AV_LOCK_DESTROY: 
91                 { 
92                         delete my_mutex;
93                         *mutex = nullptr;
94                         break; 
95                 } 
96         } 
97         return 0; 
98
99
100 static void sanitize(uint8_t *line)
101 {
102     while(*line)
103         {
104         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
105             *line='?';
106         line++;
107     }
108 }
109
110 void log_callback(void* ptr, int level, const char* fmt, va_list vl)
111 {
112     static int print_prefix=1;
113     //static int count;
114     static char prev[1024];
115     char line[8192];
116     //static int is_atty;
117     AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
118     if(level > av_log_get_level())
119         return;
120     line[0]=0;
121         
122 #undef fprintf
123     if(print_prefix && avc) 
124         {
125         if (avc->parent_log_context_offset) 
126                 {
127             AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
128             if(parent && *parent)
129                 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);            
130         }
131         std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
132     }
133
134     std::vsprintf(line + strlen(line), fmt, vl);
135
136     print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
137         
138     //if(print_prefix && !strcmp(line, prev)){
139     //    count++;
140     //    if(is_atty==1)
141     //        fprintf(stderr, "    Last message repeated %d times\r", count);
142     //    return;
143     //}
144     //if(count>0){
145     //    fprintf(stderr, "    Last message repeated %d times\n", count);
146     //    count=0;
147     //}
148     strcpy(prev, line);
149     sanitize((uint8_t*)line);
150
151         auto len = strlen(line);
152         if(len > 0)
153                 line[len-1] = 0;
154         
155         if(level == AV_LOG_DEBUG)
156                 CASPAR_LOG(debug) << L"[ffmpeg] " << line;
157         else if(level == AV_LOG_INFO)
158                 CASPAR_LOG(info) << L"[ffmpeg] " << line;
159         else if(level == AV_LOG_WARNING)
160                 CASPAR_LOG(warning) << L"[ffmpeg] " << line;
161         else if(level == AV_LOG_ERROR)
162                 CASPAR_LOG(error) << L"[ffmpeg] " << line;
163         else if(level == AV_LOG_FATAL)
164                 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;
165         else
166                 CASPAR_LOG(trace) << L"[ffmpeg] " << line;
167
168     //colored_fputs(av_clip(level>>3, 0, 6), line);
169 }
170
171 //static int query_yadif_formats(AVFilterContext *ctx)
172 //{
173 //    static const int pix_fmts[] = {
174 //        PIX_FMT_YUV444P,
175 //        PIX_FMT_YUV422P,
176 //        PIX_FMT_YUV420P,
177 //        PIX_FMT_YUV410P,
178 //        PIX_FMT_YUV411P,
179 //        PIX_FMT_GRAY8,
180 //        PIX_FMT_YUVJ444P,
181 //        PIX_FMT_YUVJ422P,
182 //        PIX_FMT_YUVJ420P,
183 //        AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
184 //        PIX_FMT_YUV440P,
185 //        PIX_FMT_YUVJ440P,
186 //        AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
187 //        AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
188 //        AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
189 //        PIX_FMT_YUVA420P,
190 //        PIX_FMT_NONE
191 //    };
192 //    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
193 //
194 //    return 0;
195 //}
196 //
197 //#pragma warning (push)
198 //#pragma warning (disable : 4706)
199 //void fix_yadif_filter_format_query()
200 //{
201 //      AVFilter** filter = nullptr;
202 //    while((filter = av_filter_next(filter)) && *filter)
203 //      {
204 //              if(strstr((*filter)->name, "yadif") != 0)
205 //                      (*filter)->query_formats = query_yadif_formats;
206 //      }
207 //}
208 //#pragma warning (pop)
209
210 std::wstring make_version(unsigned int ver)
211 {
212         std::wstringstream str;
213         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);
214         return str.str();
215 }
216
217 std::wstring avcodec_version()
218 {
219         return make_version(::avcodec_version());
220 }
221
222 std::wstring avformat_version()
223 {
224         return make_version(::avformat_version());
225 }
226
227 std::wstring avutil_version()
228 {
229         return make_version(::avutil_version());
230 }
231
232 std::wstring avfilter_version()
233 {
234         return make_version(::avfilter_version());
235 }
236
237 std::wstring swscale_version()
238 {
239         return make_version(::swscale_version());
240 }
241 bool& get_quiet_logging_for_thread()
242 {
243         static boost::thread_specific_ptr<bool> quiet_logging_for_thread;
244
245         auto local = quiet_logging_for_thread.get();
246
247         if (!local)
248         {
249                 local = new bool(false);
250                 quiet_logging_for_thread.reset(local);
251         }
252
253         return *local;
254 }
255
256 bool is_logging_quiet_for_thread()
257 {
258         return get_quiet_logging_for_thread();
259 }
260
261 std::shared_ptr<void> temporary_enable_quiet_logging_for_thread(bool enable)
262 {
263         if (!enable || is_logging_quiet_for_thread())
264                 return std::shared_ptr<void>();
265
266         get_quiet_logging_for_thread() = true;
267
268         return std::shared_ptr<void>(nullptr, [](void*)
269         {
270                 get_quiet_logging_for_thread() = false; // Only works correctly if destructed in same thread as original caller.
271         });
272 }
273
274 void log_for_thread(void* ptr, int level, const char* fmt, va_list vl)
275 {
276         ensure_gpf_handler_installed_for_thread("ffmpeg-thread");
277
278         int min_level = is_logging_quiet_for_thread() ? AV_LOG_DEBUG : AV_LOG_FATAL;
279
280         log_callback(ptr, std::max(level, min_level), fmt, vl);
281 }
282
283 void init(core::module_dependencies dependencies)
284 {
285         av_lockmgr_register(ffmpeg_lock_callback);
286         av_log_set_callback(log_for_thread);
287
288     avfilter_register_all();
289         //fix_yadif_filter_format_query();
290         av_register_all();
291     avformat_network_init();
292     avcodec_register_all();
293
294         auto info_repo = dependencies.media_info_repo;
295         
296         dependencies.consumer_registry->register_consumer_factory(L"FFmpeg Consumer", create_consumer, describe_consumer);
297         dependencies.consumer_registry->register_consumer_factory(L"Streaming Consumer",  create_streaming_consumer, describe_streaming_consumer);
298         dependencies.consumer_registry->register_preconfigured_consumer_factory(L"file", create_preconfigured_consumer);
299         dependencies.consumer_registry->register_preconfigured_consumer_factory(L"stream", create_preconfigured_streaming_consumer);
300         dependencies.producer_registry->register_producer_factory(L"FFmpeg Producer", boost::bind(&create_producer, _1, _2, info_repo), describe_producer);
301         dependencies.producer_registry->register_thumbnail_producer_factory(boost::bind(&create_thumbnail_producer, _1, _2, info_repo));
302
303         info_repo->register_extractor(
304                         [](const std::wstring& file, const std::wstring& extension, core::media_info& info) -> bool
305                         {
306                                 auto quiet_logging = temporary_enable_quiet_logging_for_thread(true);
307                                 if (extension == L".WAV" || extension == L".MP3")
308                                 {
309                                         info.clip_type = L"AUDIO";
310                                         return true;
311                                 }
312
313                                 if (!is_valid_file(file, true))
314                                         return false;
315
316                                 info.clip_type = L"MOVIE";
317
318                                 return try_get_duration(file, info.duration, info.time_base);
319                         });
320         dependencies.system_info_provider_repo->register_system_info_provider([](boost::property_tree::wptree& info)
321         {
322                 info.add(L"system.ffmpeg.avcodec", avcodec_version());
323                 info.add(L"system.ffmpeg.avformat", avformat_version());
324                 info.add(L"system.ffmpeg.avfilter", avfilter_version());
325                 info.add(L"system.ffmpeg.avutil", avutil_version());
326                 info.add(L"system.ffmpeg.swscale", swscale_version());
327         });
328 }
329
330 void uninit()
331 {
332         avfilter_uninit();
333     avformat_network_deinit();
334         av_lockmgr_register(nullptr);
335 }
336
337 }}