3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program 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 2, or (at your option)
11 * This Program 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.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #if (defined HAVE_CONFIG_H) && (!defined WIN32)
25 #include "DynamicDll.h"
26 #include "DllAvCodec.h"
27 #include "DllAvFormat.h"
28 #include "DllSwResample.h"
29 #include "utils/log.h"
35 #ifndef __STDC_CONSTANT_MACROS
36 #define __STDC_CONSTANT_MACROS
40 #pragma warning(disable:4244)
43 #if (defined USE_EXTERNAL_FFMPEG)
44 #include <libavfilter/avfilter.h>
45 #include <libavfilter/avfiltergraph.h>
46 #include <libavfilter/buffersink.h>
47 #if LIBAVFILTER_VERSION_MICRO >= 100 && LIBAVFILTER_VERSION_INT < AV_VERSION_INT(3,43,100)
48 #include <libavfilter/avcodec.h>
50 #include <libavfilter/buffersrc.h>
52 #include "libavfilter/avfilter.h"
53 #include "libavfilter/avfiltergraph.h"
54 #include "libavfilter/buffersink.h"
55 #if LIBAVFILTER_VERSION_MICRO >= 100 && LIBAVFILTER_VERSION_INT < AV_VERSION_INT(3,43,100)
56 #include "libavfilter/avcodec.h"
58 #include "libavfilter/buffersrc.h"
62 #if LIBAVFILTER_VERSION_INT >= AV_VERSION_INT(3,43,100)
63 #define LIBAVFILTER_AVFRAME_BASED
66 #if LIBAVFILTER_VERSION_INT >= AV_VERSION_INT(3,78,100)
67 #define HAVE_AVFILTER_GRAPH_PARSE_PTR
70 #include "threads/SingleLock.h"
72 class DllAvFilterInterface
75 virtual ~DllAvFilterInterface() {}
76 virtual void avfilter_free(AVFilterContext *filter)=0;
77 virtual void avfilter_graph_free(AVFilterGraph **graph)=0;
78 virtual int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt, const char *name, const char *args, void *opaque, AVFilterGraph *graph_ctx)=0;
79 virtual AVFilter *avfilter_get_by_name(const char *name)=0;
80 virtual AVFilterGraph *avfilter_graph_alloc(void)=0;
81 virtual AVFilterInOut *avfilter_inout_alloc()=0;
82 virtual void avfilter_inout_free(AVFilterInOut **inout)=0;
83 #if defined(HAVE_AVFILTER_GRAPH_PARSE_PTR)
84 virtual int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)=0;
86 virtual int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)=0;
88 virtual int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)=0;
89 #if defined(LIBAVFILTER_AVFRAME_BASED)
90 virtual int av_buffersrc_add_frame(AVFilterContext *buffer_filter, AVFrame *frame)=0;
92 virtual int av_buffersrc_add_frame(AVFilterContext *buffer_filter, AVFrame *frame, int flags)=0;
93 virtual void avfilter_unref_buffer(AVFilterBufferRef *ref)=0;
95 virtual int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)=0;
96 #if defined(LIBAVFILTER_AVFRAME_BASED)
97 virtual int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame) = 0;
99 virtual int av_buffersink_get_buffer_ref(AVFilterContext *buffer_sink, AVFilterBufferRef **bufref, int flags)=0;
101 virtual AVBufferSinkParams *av_buffersink_params_alloc()=0;
102 #if !defined(LIBAVFILTER_AVFRAME_BASED)
103 virtual int av_buffersink_poll_frame(AVFilterContext *ctx)=0;
107 #if (defined USE_EXTERNAL_FFMPEG) || (defined TARGET_DARWIN) || (defined USE_STATIC_FFMPEG)
108 // Use direct mapping
109 class DllAvFilter : public DllDynamic, DllAvFilterInterface
112 virtual ~DllAvFilter() {}
113 virtual void avfilter_free(AVFilterContext *filter)
115 CSingleLock lock(DllAvCodec::m_critSection);
116 ::avfilter_free(filter);
118 virtual void avfilter_graph_free(AVFilterGraph **graph)
120 CSingleLock lock(DllAvCodec::m_critSection);
121 ::avfilter_graph_free(graph);
123 void avfilter_register_all()
125 CSingleLock lock(DllAvCodec::m_critSection);
126 ::avfilter_register_all();
128 virtual int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt, const char *name, const char *args, void *opaque, AVFilterGraph *graph_ctx) { return ::avfilter_graph_create_filter(filt_ctx, filt, name, args, opaque, graph_ctx); }
129 virtual AVFilter *avfilter_get_by_name(const char *name) { return ::avfilter_get_by_name(name); }
130 virtual AVFilterGraph *avfilter_graph_alloc() { return ::avfilter_graph_alloc(); }
131 virtual AVFilterInOut *avfilter_inout_alloc()
133 CSingleLock lock(DllAvCodec::m_critSection);
134 return ::avfilter_inout_alloc();
136 virtual void avfilter_inout_free(AVFilterInOut **inout)
138 CSingleLock lock(DllAvCodec::m_critSection);
139 ::avfilter_inout_free(inout);
141 #if defined(HAVE_AVFILTER_GRAPH_PARSE_PTR)
142 virtual int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)
144 CSingleLock lock(DllAvCodec::m_critSection);
145 return ::avfilter_graph_parse_ptr(graph, filters, inputs, outputs, log_ctx);
148 virtual int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)
150 CSingleLock lock(DllAvCodec::m_critSection);
151 return ::avfilter_graph_parse(graph, filters, inputs, outputs, log_ctx);
154 virtual int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
156 return ::avfilter_graph_config(graphctx, log_ctx);
158 #if defined(LIBAVFILTER_AVFRAME_BASED)
159 virtual int av_buffersrc_add_frame(AVFilterContext *buffer_filter, AVFrame* frame) { return ::av_buffersrc_add_frame(buffer_filter, frame); }
161 virtual int av_buffersrc_add_frame(AVFilterContext *buffer_filter, AVFrame* frame, int flags) { return ::av_buffersrc_add_frame(buffer_filter, frame, flags); }
162 virtual void avfilter_unref_buffer(AVFilterBufferRef *ref) { ::avfilter_unref_buffer(ref); }
164 virtual int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad) { return ::avfilter_link(src, srcpad, dst, dstpad); }
165 #if defined(LIBAVFILTER_AVFRAME_BASED)
166 virtual int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame) { return ::av_buffersink_get_frame(ctx, frame); }
168 virtual int av_buffersink_get_buffer_ref(AVFilterContext *buffer_sink, AVFilterBufferRef **bufref, int flags) { return ::av_buffersink_get_buffer_ref(buffer_sink, bufref, flags); }
170 virtual AVBufferSinkParams *av_buffersink_params_alloc() { return ::av_buffersink_params_alloc(); }
171 #if !defined(LIBAVFILTER_AVFRAME_BASED)
172 virtual int av_buffersink_poll_frame(AVFilterContext *ctx) { return ::av_buffersink_poll_frame(ctx); }
175 virtual bool ResolveExports() { return true; }
176 virtual bool Load() {
177 #if !defined(TARGET_DARWIN)
178 CLog::Log(LOGDEBUG, "DllAvFilter: Using libavfilter system library");
182 virtual void Unload() {}
185 class DllAvFilter : public DllDynamic, DllAvFilterInterface
187 DECLARE_DLL_WRAPPER(DllAvFilter, DLL_PATH_LIBAVFILTER)
191 DEFINE_METHOD1(void, avfilter_free_dont_call, (AVFilterContext *p1))
192 DEFINE_METHOD1(void, avfilter_graph_free_dont_call, (AVFilterGraph **p1))
193 DEFINE_METHOD0(void, avfilter_register_all_dont_call)
194 DEFINE_METHOD6(int, avfilter_graph_create_filter, (AVFilterContext **p1, AVFilter *p2, const char *p3, const char *p4, void *p5, AVFilterGraph *p6))
195 DEFINE_METHOD1(AVFilter*, avfilter_get_by_name, (const char *p1))
196 DEFINE_METHOD0(AVFilterGraph*, avfilter_graph_alloc)
197 DEFINE_METHOD0(AVFilterInOut*, avfilter_inout_alloc_dont_call)
198 DEFINE_METHOD1(void, avfilter_inout_free_dont_call, (AVFilterInOut **p1))
199 #if defined(HAVE_AVFILTER_GRAPH_PARSE_PTR)
200 DEFINE_FUNC_ALIGNED5(int, __cdecl, avfilter_graph_parse_ptr_dont_call, AVFilterGraph *, const char *, AVFilterInOut **, AVFilterInOut **, void *)
202 DEFINE_FUNC_ALIGNED5(int, __cdecl, avfilter_graph_parse_dont_call, AVFilterGraph *, const char *, AVFilterInOut **, AVFilterInOut **, void *)
204 DEFINE_FUNC_ALIGNED2(int, __cdecl, avfilter_graph_config_dont_call, AVFilterGraph *, void *)
205 #if defined(LIBAVFILTER_AVFRAME_BASED)
206 DEFINE_METHOD2(int, av_buffersrc_add_frame, (AVFilterContext *p1, AVFrame *p2))
208 DEFINE_METHOD3(int, av_buffersrc_add_frame, (AVFilterContext *p1, AVFrame *p2, int p3))
209 DEFINE_METHOD1(void, avfilter_unref_buffer, (AVFilterBufferRef *p1))
211 DEFINE_METHOD4(int, avfilter_link, (AVFilterContext *p1, unsigned p2, AVFilterContext *p3, unsigned p4))
212 #if defined(LIBAVFILTER_AVFRAME_BASED)
213 DEFINE_FUNC_ALIGNED2(int , __cdecl, av_buffersink_get_frame, AVFilterContext *, AVFrame *);
215 DEFINE_FUNC_ALIGNED3(int , __cdecl, av_buffersink_get_buffer_ref, AVFilterContext *, AVFilterBufferRef **, int);
217 DEFINE_FUNC_ALIGNED0(AVBufferSinkParams*, __cdecl, av_buffersink_params_alloc);
218 #if !defined(LIBAVFILTER_AVFRAME_BASED)
219 DEFINE_FUNC_ALIGNED1(int , __cdecl, av_buffersink_poll_frame, AVFilterContext *);
222 BEGIN_METHOD_RESOLVE()
223 RESOLVE_METHOD_RENAME(avfilter_free, avfilter_free_dont_call)
224 RESOLVE_METHOD_RENAME(avfilter_graph_free, avfilter_graph_free_dont_call)
225 RESOLVE_METHOD_RENAME(avfilter_register_all, avfilter_register_all_dont_call)
226 RESOLVE_METHOD(avfilter_graph_create_filter)
227 RESOLVE_METHOD(avfilter_get_by_name)
228 RESOLVE_METHOD(avfilter_graph_alloc)
229 RESOLVE_METHOD_RENAME(avfilter_inout_alloc, avfilter_inout_alloc_dont_call)
230 RESOLVE_METHOD_RENAME(avfilter_inout_free, avfilter_inout_free_dont_call)
231 #if defined(HAVE_AVFILTER_GRAPH_PARSE_PTR)
232 RESOLVE_METHOD_RENAME(avfilter_graph_parse_ptr, avfilter_graph_parse_ptr_dont_call)
234 RESOLVE_METHOD_RENAME(avfilter_graph_parse, avfilter_graph_parse_dont_call)
236 RESOLVE_METHOD_RENAME(avfilter_graph_config, avfilter_graph_config_dont_call)
237 RESOLVE_METHOD(av_buffersrc_add_frame)
238 #if !defined(LIBAVFILTER_AVFRAME_BASED)
239 RESOLVE_METHOD(avfilter_unref_buffer)
241 RESOLVE_METHOD(avfilter_link)
242 #if defined(LIBAVFILTER_AVFRAME_BASED)
243 RESOLVE_METHOD(av_buffersink_get_frame)
245 RESOLVE_METHOD(av_buffersink_get_buffer_ref)
247 RESOLVE_METHOD(av_buffersink_params_alloc)
248 #if !defined(LIBAVFILTER_AVFRAME_BASED)
249 RESOLVE_METHOD(av_buffersink_poll_frame)
253 /* dependencies of libavfilter */
254 DllAvUtil m_dllAvUtil;
255 DllSwResample m_dllSwResample;
256 DllAvFormat m_dllAvFormat;
259 void avfilter_free(AVFilterContext *filter)
261 CSingleLock lock(DllAvCodec::m_critSection);
262 avfilter_free_dont_call(filter);
264 void avfilter_graph_free(AVFilterGraph **graph)
266 CSingleLock lock(DllAvCodec::m_critSection);
267 avfilter_graph_free_dont_call(graph);
269 void avfilter_register_all()
271 CSingleLock lock(DllAvCodec::m_critSection);
272 avfilter_register_all_dont_call();
274 AVFilterInOut* avfilter_inout_alloc()
276 CSingleLock lock(DllAvCodec::m_critSection);
277 return avfilter_inout_alloc_dont_call();
279 #if defined(HAVE_AVFILTER_GRAPH_PARSE_PTR)
280 int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)
282 CSingleLock lock(DllAvCodec::m_critSection);
283 return avfilter_graph_parse_ptr_dont_call(graph, filters, inputs, outputs, log_ctx);
286 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)
288 CSingleLock lock(DllAvCodec::m_critSection);
289 return avfilter_graph_parse_dont_call(graph, filters, inputs, outputs, log_ctx);
292 void avfilter_inout_free(AVFilterInOut **inout)
294 CSingleLock lock(DllAvCodec::m_critSection);
295 avfilter_inout_free_dont_call(inout);
297 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
299 CSingleLock lock(DllAvCodec::m_critSection);
300 return avfilter_graph_config_dont_call(graphctx, log_ctx);
304 if (!m_dllAvUtil.Load())
306 if (!m_dllSwResample.Load())
308 if (!m_dllAvFormat.Load())
310 return DllDynamic::Load();