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