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