]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
* Refactored so that frame_producers are stored in a frame_producer_registry instance...
[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
33 #include <core/consumer/frame_consumer.h>
34 #include <core/producer/frame_producer.h>
35 #include <core/producer/media_info/media_info.h>
36 #include <core/producer/media_info/media_info_repository.h>
37 #include <core/system_info_provider.h>
38
39 #include <boost/property_tree/ptree.hpp>
40
41 #include <tbb/recursive_mutex.h>
42
43 #if defined(_MSC_VER)
44 #pragma warning (disable : 4244)
45 #pragma warning (disable : 4603)
46 #pragma warning (disable : 4996)
47 #endif
48
49 extern "C" 
50 {
51         #define __STDC_CONSTANT_MACROS
52         #define __STDC_LIMIT_MACROS
53         #include <libavformat/avformat.h>
54         #include <libswscale/swscale.h>
55         #include <libavutil/avutil.h>
56         #include <libavfilter/avfilter.h>
57 }
58
59 namespace caspar { namespace ffmpeg {
60         
61 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op) 
62
63         if(!mutex)
64                 return 0;
65
66         auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);
67         
68         switch(op) 
69         { 
70                 case AV_LOCK_CREATE: 
71                 { 
72                         *mutex = new tbb::recursive_mutex(); 
73                         break; 
74                 } 
75                 case AV_LOCK_OBTAIN: 
76                 { 
77                         if(my_mutex)
78                                 my_mutex->lock(); 
79                         break; 
80                 } 
81                 case AV_LOCK_RELEASE: 
82                 { 
83                         if(my_mutex)
84                                 my_mutex->unlock(); 
85                         break; 
86                 } 
87                 case AV_LOCK_DESTROY: 
88                 { 
89                         delete my_mutex;
90                         *mutex = nullptr;
91                         break; 
92                 } 
93         } 
94         return 0; 
95
96
97 static void sanitize(uint8_t *line)
98 {
99     while(*line)
100         {
101         if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
102             *line='?';
103         line++;
104     }
105 }
106
107 void log_callback(void* ptr, int level, const char* fmt, va_list vl)
108 {
109     static int print_prefix=1;
110     //static int count;
111     static char prev[1024];
112     char line[8192];
113     //static int is_atty;
114     AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
115     if(level > av_log_get_level())
116         return;
117     line[0]=0;
118         
119 #undef fprintf
120     if(print_prefix && avc) 
121         {
122         if (avc->parent_log_context_offset) 
123                 {
124             AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
125             if(parent && *parent)
126                 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);            
127         }
128         std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
129     }
130
131     std::vsprintf(line + strlen(line), fmt, vl);
132
133     print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
134         
135     //if(print_prefix && !strcmp(line, prev)){
136     //    count++;
137     //    if(is_atty==1)
138     //        fprintf(stderr, "    Last message repeated %d times\r", count);
139     //    return;
140     //}
141     //if(count>0){
142     //    fprintf(stderr, "    Last message repeated %d times\n", count);
143     //    count=0;
144     //}
145     strcpy(prev, line);
146     sanitize((uint8_t*)line);
147
148         auto len = strlen(line);
149         if(len > 0)
150                 line[len-1] = 0;
151         
152         if(level == AV_LOG_DEBUG)
153                 CASPAR_LOG(debug) << L"[ffmpeg] " << line;
154         else if(level == AV_LOG_INFO)
155                 CASPAR_LOG(info) << L"[ffmpeg] " << line;
156         else if(level == AV_LOG_WARNING)
157                 CASPAR_LOG(warning) << L"[ffmpeg] " << line;
158         else if(level == AV_LOG_ERROR)
159                 CASPAR_LOG(error) << L"[ffmpeg] " << line;
160         else if(level == AV_LOG_FATAL)
161                 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;
162         else
163                 CASPAR_LOG(trace) << L"[ffmpeg] " << line;
164
165     //colored_fputs(av_clip(level>>3, 0, 6), line);
166 }
167
168 //static int query_yadif_formats(AVFilterContext *ctx)
169 //{
170 //    static const int pix_fmts[] = {
171 //        PIX_FMT_YUV444P,
172 //        PIX_FMT_YUV422P,
173 //        PIX_FMT_YUV420P,
174 //        PIX_FMT_YUV410P,
175 //        PIX_FMT_YUV411P,
176 //        PIX_FMT_GRAY8,
177 //        PIX_FMT_YUVJ444P,
178 //        PIX_FMT_YUVJ422P,
179 //        PIX_FMT_YUVJ420P,
180 //        AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
181 //        PIX_FMT_YUV440P,
182 //        PIX_FMT_YUVJ440P,
183 //        AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
184 //        AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
185 //        AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
186 //        PIX_FMT_YUVA420P,
187 //        PIX_FMT_NONE
188 //    };
189 //    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
190 //
191 //    return 0;
192 //}
193 //
194 //#pragma warning (push)
195 //#pragma warning (disable : 4706)
196 //void fix_yadif_filter_format_query()
197 //{
198 //      AVFilter** filter = nullptr;
199 //    while((filter = av_filter_next(filter)) && *filter)
200 //      {
201 //              if(strstr((*filter)->name, "yadif") != 0)
202 //                      (*filter)->query_formats = query_yadif_formats;
203 //      }
204 //}
205 //#pragma warning (pop)
206
207 std::wstring make_version(unsigned int ver)
208 {
209         std::wstringstream str;
210         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);
211         return str.str();
212 }
213
214 std::wstring avcodec_version()
215 {
216         return make_version(::avcodec_version());
217 }
218
219 std::wstring avformat_version()
220 {
221         return make_version(::avformat_version());
222 }
223
224 std::wstring avutil_version()
225 {
226         return make_version(::avutil_version());
227 }
228
229 std::wstring avfilter_version()
230 {
231         return make_version(::avfilter_version());
232 }
233
234 std::wstring swscale_version()
235 {
236         return make_version(::swscale_version());
237 }
238
239 void init(core::module_dependencies dependencies)
240 {
241         av_lockmgr_register(ffmpeg_lock_callback);
242         av_log_set_callback(log_callback);
243
244     avfilter_register_all();
245         //fix_yadif_filter_format_query();
246         av_register_all();
247     avformat_network_init();
248     avcodec_register_all();
249         
250         core::register_consumer_factory(create_consumer);
251         core::register_consumer_factory(create_streaming_consumer);
252         core::register_preconfigured_consumer_factory(L"file", create_preconfigured_consumer);
253         core::register_preconfigured_consumer_factory(L"stream", create_preconfigured_streaming_consumer);
254         dependencies.producer_registry->register_producer_factory(create_producer);
255         
256         dependencies.media_info_repo->register_extractor(
257                         [](const std::wstring& file, const std::wstring& extension, core::media_info& info) -> bool
258                         {
259                                 // TODO: merge thumbnail generation from 2.0
260                                 //auto disable_logging = temporary_disable_logging_for_thread(true);
261                                 if (extension == L".WAV" || extension == L".MP3")
262                                 {
263                                         info.clip_type = L"AUDIO";
264                                         return true;
265                                 }
266
267                                 if (!is_valid_file(file))
268                                         return false;
269
270                                 info.clip_type = L"MOVIE";
271
272                                 return try_get_duration(file, info.duration, info.time_base);
273                         });
274         dependencies.system_info_provider_repo->register_system_info_provider([](boost::property_tree::wptree& info)
275         {
276                 info.add(L"system.ffmpeg.avcodec", avcodec_version());
277                 info.add(L"system.ffmpeg.avformat", avformat_version());
278                 info.add(L"system.ffmpeg.avfilter", avfilter_version());
279                 info.add(L"system.ffmpeg.avutil", avutil_version());
280                 info.add(L"system.ffmpeg.swscale", swscale_version());
281         });
282 }
283
284 void uninit()
285 {
286         avfilter_uninit();
287     avformat_network_deinit();
288         av_lockmgr_register(nullptr);
289 }
290
291 }}