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