2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
24 #include "consumer/ffmpeg_consumer.h"
\r
25 #include "producer/ffmpeg_producer.h"
\r
27 #include <common/log/log.h>
\r
29 #include <core/consumer/frame_consumer.h>
\r
30 #include <core/producer/frame_producer.h>
\r
32 #include <tbb/recursive_mutex.h>
\r
34 #if defined(_MSC_VER)
\r
35 #pragma warning (disable : 4244)
\r
36 #pragma warning (disable : 4603)
\r
37 #pragma warning (disable : 4996)
\r
42 #define __STDC_CONSTANT_MACROS
\r
43 #define __STDC_LIMIT_MACROS
\r
44 #include <libavformat/avformat.h>
\r
45 #include <libswscale/swscale.h>
\r
46 #include <libavutil/avutil.h>
\r
47 #include <libavfilter/avfilter.h>
\r
50 namespace caspar { namespace ffmpeg {
\r
52 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op)
\r
57 auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);
\r
61 case AV_LOCK_CREATE:
\r
63 *mutex = new tbb::recursive_mutex();
\r
66 case AV_LOCK_OBTAIN:
\r
72 case AV_LOCK_RELEASE:
\r
75 my_mutex->unlock();
\r
78 case AV_LOCK_DESTROY:
\r
88 static void sanitize(uint8_t *line)
\r
92 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
\r
98 void log_callback(void* ptr, int level, const char* fmt, va_list vl)
\r
100 static int print_prefix=1;
\r
102 static char prev[1024];
\r
104 static int is_atty;
\r
105 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
\r
106 if(level > av_log_get_level())
\r
111 if(print_prefix && avc)
\r
113 if (avc->parent_log_context_offset)
\r
115 AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
\r
116 if(parent && *parent)
\r
117 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);
\r
119 std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
\r
122 std::vsprintf(line + strlen(line), fmt, vl);
\r
124 print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
\r
126 //if(print_prefix && !strcmp(line, prev)){
\r
129 // fprintf(stderr, " Last message repeated %d times\r", count);
\r
133 // fprintf(stderr, " Last message repeated %d times\n", count);
\r
136 strcpy(prev, line);
\r
137 sanitize((uint8_t*)line);
\r
139 if(level == AV_LOG_DEBUG)
\r
140 CASPAR_LOG(debug) << L"[ffmpeg] " << line;
\r
141 else if(level == AV_LOG_INFO)
\r
142 CASPAR_LOG(info) << L"[ffmpeg] " << line;
\r
143 else if(level == AV_LOG_WARNING)
\r
144 CASPAR_LOG(warning) << L"[ffmpeg] " << line;
\r
145 else if(level == AV_LOG_ERROR)
\r
146 CASPAR_LOG(error) << L"[ffmpeg] " << line;
\r
147 else if(level == AV_LOG_FATAL)
\r
148 CASPAR_LOG(fatal) << L"[ffmpeg] " << line;
\r
150 CASPAR_LOG(trace) << L"[ffmpeg] " << line;
\r
152 //colored_fputs(av_clip(level>>3, 0, 6), line);
\r
155 //static int query_yadif_formats(AVFilterContext *ctx)
\r
157 // static const int pix_fmts[] = {
\r
158 // PIX_FMT_YUV444P,
\r
159 // PIX_FMT_YUV422P,
\r
160 // PIX_FMT_YUV420P,
\r
161 // PIX_FMT_YUV410P,
\r
162 // PIX_FMT_YUV411P,
\r
164 // PIX_FMT_YUVJ444P,
\r
165 // PIX_FMT_YUVJ422P,
\r
166 // PIX_FMT_YUVJ420P,
\r
167 // AV_NE( PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE ),
\r
168 // PIX_FMT_YUV440P,
\r
169 // PIX_FMT_YUVJ440P,
\r
170 // AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
\r
171 // AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
\r
172 // AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
\r
173 // PIX_FMT_YUVA420P,
\r
176 // avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
\r
181 //#pragma warning (push)
\r
182 //#pragma warning (disable : 4706)
\r
183 //void fix_yadif_filter_format_query()
\r
185 // AVFilter** filter = nullptr;
\r
186 // while((filter = av_filter_next(filter)) && *filter)
\r
188 // if(strstr((*filter)->name, "yadif") != 0)
\r
189 // (*filter)->query_formats = query_yadif_formats;
\r
192 //#pragma warning (pop)
\r
196 avfilter_register_all();
\r
197 //fix_yadif_filter_format_query();
\r
200 avcodec_register_all();
\r
201 av_lockmgr_register(ffmpeg_lock_callback);
\r
202 av_log_set_callback(log_callback);
\r
204 core::register_consumer_factory([](const std::vector<std::wstring>& params){return create_consumer(params);});
\r
205 core::register_producer_factory(create_producer);
\r
211 av_lockmgr_register(nullptr);
\r
214 std::wstring make_version(unsigned int ver)
\r
216 std::wstringstream str;
\r
217 str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);
\r
221 std::wstring get_avcodec_version()
\r
223 return make_version(avcodec_version());
\r
226 std::wstring get_avformat_version()
\r
228 return make_version(avformat_version());
\r
231 std::wstring get_avutil_version()
\r
233 return make_version(avutil_version());
\r
236 std::wstring get_avfilter_version()
\r
238 return make_version(avfilter_version());
\r
241 std::wstring get_swscale_version()
\r
243 return make_version(swscale_version());
\r