DllAvUtil.h: Add LIBAVUTIL_FROM* macros.
[vuplus_xbmc] / lib / DllAvUtil.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://www.xbmc.org
5  *
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)
9  *  any later version.
10  *
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.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA  02110-1301, USA.
20  *  http://www.gnu.org/copyleft/gpl.html
21  *
22  */
23
24 #if (defined HAVE_CONFIG_H) && (!defined WIN32)
25   #include "config.h"
26 #endif
27 #include "DynamicDll.h"
28 #include "utils/log.h"
29
30 #ifndef __GNUC__
31 #pragma warning(push)
32 #pragma warning(disable:4244)
33 #endif
34
35 extern "C" {
36 #if (defined USE_EXTERNAL_FFMPEG)
37   #include <libavutil/avutil.h>
38   // for av_get_default_channel_layout
39   #include <libavutil/audioconvert.h>
40   #include <libavutil/crc.h>
41   #include <libavutil/fifo.h>
42   // for LIBAVCODEC_VERSION_INT:
43   #include <libavcodec/avcodec.h>
44   // for enum AVSampleFormat
45   #include <libavutil/samplefmt.h>
46   #include <libavutil/opt.h>
47   #include <libavutil/mem.h>
48   #include <libavutil/mathematics.h>
49 #else
50   #include "libavutil/avutil.h"
51   //for av_get_default_channel_layout
52   #include "libavutil/audioconvert.h"
53   #include "libavutil/crc.h"
54   #include "libavutil/opt.h"
55   #include "libavutil/mem.h"
56   #include "libavutil/fifo.h"
57   // for enum AVSampleFormat
58   #include "libavutil/samplefmt.h"
59 #endif
60 }
61
62 #if LIBAVUTIL_VERSION_MICRO >= 100
63   #define LIBAVUTIL_FROM_FFMPEG
64 #else
65   #define LIBAVUTIL_FROM_LIBAV
66 #endif
67
68
69 #ifndef __GNUC__
70 #pragma warning(pop)
71 #endif
72
73 // calback used for logging
74 void ff_avutil_log(void* ptr, int level, const char* format, va_list va);
75
76 class DllAvUtilInterface
77 {
78 public:
79   virtual ~DllAvUtilInterface() {}
80   virtual void av_log_set_callback(void (*)(void*, int, const char*, va_list))=0;
81   virtual void *av_malloc(unsigned int size)=0;
82   virtual void *av_mallocz(unsigned int size)=0;
83   virtual void *av_realloc(void *ptr, unsigned int size)=0;
84   virtual void av_free(void *ptr)=0;
85   virtual void av_freep(void *ptr)=0;
86   virtual int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding)=0;
87   virtual int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)=0;
88   virtual int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)=0;
89   virtual const AVCRC* av_crc_get_table(AVCRCId crc_id)=0;
90   virtual uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)=0;
91   virtual int av_opt_set(void *obj, const char *name, const char *val, int search_flags)=0;
92   virtual int av_opt_set_double(void *obj, const char *name, double val, int search_flags)=0;
93   virtual int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)=0;
94   virtual AVFifoBuffer *av_fifo_alloc(unsigned int size) = 0;
95   virtual void av_fifo_free(AVFifoBuffer *f) = 0;
96   virtual void av_fifo_reset(AVFifoBuffer *f) = 0;
97   virtual int av_fifo_size(AVFifoBuffer *f) = 0;
98   virtual int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int)) = 0;
99   virtual int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int)) = 0;
100   virtual char *av_strdup(const char *s)=0;
101   virtual int av_get_bytes_per_sample(enum AVSampleFormat p1) = 0;
102   virtual AVDictionaryEntry *av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags) = 0;
103   virtual int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)=0;
104   virtual void av_dict_free(AVDictionary **pm) = 0;
105   virtual int av_samples_get_buffer_size (int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align) = 0;
106   virtual int64_t av_get_default_channel_layout(int nb_channels)=0;
107   virtual int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align) = 0;
108   virtual int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt) = 0;
109   virtual int av_get_channel_layout_channel_index (uint64_t channel_layout, uint64_t channel) = 0;
110   virtual int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align) = 0;
111   virtual int av_samples_copy(uint8_t **dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt) = 0;
112 };
113
114 #if defined (USE_EXTERNAL_FFMPEG) || (defined TARGET_DARWIN)
115 // Use direct layer
116 class DllAvUtilBase : public DllDynamic, DllAvUtilInterface
117 {
118 public:
119
120   virtual ~DllAvUtilBase() {}
121    virtual void av_log_set_callback(void (*foo)(void*, int, const char*, va_list)) { ::av_log_set_callback(foo); }
122    virtual void *av_malloc(unsigned int size) { return ::av_malloc(size); }
123    virtual void *av_mallocz(unsigned int size) { return ::av_mallocz(size); }
124    virtual void *av_realloc(void *ptr, unsigned int size) { return ::av_realloc(ptr, size); }
125    virtual void av_free(void *ptr) { ::av_free(ptr); }
126    virtual void av_freep(void *ptr) { ::av_freep(ptr); }
127    virtual int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding d) { return ::av_rescale_rnd(a, b, c, d); }
128    virtual int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) { return ::av_rescale_q(a, bq, cq); }
129    virtual int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size) { return ::av_crc_init(ctx, le, bits, poly, ctx_size); }
130    virtual const AVCRC* av_crc_get_table(AVCRCId crc_id) { return ::av_crc_get_table(crc_id); }
131    virtual uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length) { return ::av_crc(ctx, crc, buffer, length); }
132    virtual int av_opt_set(void *obj, const char *name, const char *val, int search_flags) { return ::av_opt_set(obj, name, val, search_flags); }
133    virtual int av_opt_set_double(void *obj, const char *name, double val, int search_flags) { return ::av_opt_set_double(obj, name, val, search_flags); }
134    virtual int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags) { return ::av_opt_set_int(obj, name, val, search_flags); }
135   virtual AVFifoBuffer *av_fifo_alloc(unsigned int size) {return ::av_fifo_alloc(size); }
136   virtual void av_fifo_free(AVFifoBuffer *f) { ::av_fifo_free(f); }
137   virtual void av_fifo_reset(AVFifoBuffer *f) { ::av_fifo_reset(f); }
138   virtual int av_fifo_size(AVFifoBuffer *f) { return ::av_fifo_size(f); }
139   virtual int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int))
140     { return ::av_fifo_generic_read(f, dest, buf_size, func); }
141   virtual int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int))
142     { return ::av_fifo_generic_write(f, src, size, func); }
143   virtual char *av_strdup(const char *s) { return ::av_strdup(s); }
144   virtual int av_get_bytes_per_sample(enum AVSampleFormat p1)
145     { return ::av_get_bytes_per_sample(p1); }
146   virtual AVDictionaryEntry *av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags){ return ::av_dict_get(m, key, prev, flags); }
147   virtual int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags) { return ::av_dict_set(pm, key, value, flags); }
148   virtual void av_dict_free(AVDictionary **pm) { ::av_dict_free(pm); }
149   virtual int av_samples_get_buffer_size (int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
150     { return ::av_samples_get_buffer_size(linesize, nb_channels, nb_samples, sample_fmt, align); }
151   virtual int64_t av_get_default_channel_layout(int nb_channels) { return ::av_get_default_channel_layout(nb_channels); }
152   virtual int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
153     { return ::av_samples_alloc(audio_data, linesize, nb_channels, nb_samples, sample_fmt, align); }
154   virtual int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt) { return ::av_sample_fmt_is_planar(sample_fmt); }
155   virtual int av_get_channel_layout_channel_index (uint64_t channel_layout, uint64_t channel) { return ::av_get_channel_layout_channel_index(channel_layout, channel); }
156   virtual int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
157     { return ::av_samples_fill_arrays(audio_data, linesize, buf, nb_channels, nb_samples, AVSampleFormat sample_fmt, align); }
158   virtual int av_samples_copy(uint8_t **dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
159     { return ::av_samples_copy(dst, src, dst_offset, src_offset, nb_samples, nb_channels, sample_fmt); }
160
161    // DLL faking.
162    virtual bool ResolveExports() { return true; }
163    virtual bool Load() {
164 #if !defined(TARGET_DARWIN)
165      CLog::Log(LOGDEBUG, "DllAvUtilBase: Using libavutil system library");
166 #endif
167      return true;
168    }
169    virtual void Unload() {}
170 };
171
172 #else
173
174 class DllAvUtilBase : public DllDynamic, DllAvUtilInterface
175 {
176   DECLARE_DLL_WRAPPER(DllAvUtilBase, DLL_PATH_LIBAVUTIL)
177
178   LOAD_SYMBOLS()
179
180   DEFINE_METHOD1(void, av_log_set_callback, (void (*p1)(void*, int, const char*, va_list)))
181   DEFINE_METHOD1(void*, av_malloc, (unsigned int p1))
182   DEFINE_METHOD1(void*, av_mallocz, (unsigned int p1))
183   DEFINE_METHOD2(void*, av_realloc, (void *p1, unsigned int p2))
184   DEFINE_METHOD1(void, av_free, (void *p1))
185   DEFINE_METHOD1(void, av_freep, (void *p1))
186   DEFINE_METHOD4(int64_t, av_rescale_rnd, (int64_t p1, int64_t p2, int64_t p3, enum AVRounding p4));
187   DEFINE_METHOD3(int64_t, av_rescale_q, (int64_t p1, AVRational p2, AVRational p3));
188   DEFINE_METHOD1(const AVCRC*, av_crc_get_table, (AVCRCId p1))
189   DEFINE_METHOD5(int, av_crc_init, (AVCRC *p1, int p2, int p3, uint32_t p4, int p5));
190   DEFINE_METHOD4(uint32_t, av_crc, (const AVCRC *p1, uint32_t p2, const uint8_t *p3, size_t p4));
191   DEFINE_METHOD4(int, av_opt_set, (void *p1, const char *p2, const char *p3, int p4));
192   DEFINE_METHOD4(int, av_opt_set_double, (void *p1, const char *p2, double p3, int p4))
193   DEFINE_METHOD4(int, av_opt_set_int, (void *p1, const char *p2, int64_t p3, int p4))
194   DEFINE_METHOD1(AVFifoBuffer*, av_fifo_alloc, (unsigned int p1))
195   DEFINE_METHOD1(void, av_fifo_free, (AVFifoBuffer *p1))
196   DEFINE_METHOD1(void, av_fifo_reset, (AVFifoBuffer *p1))
197   DEFINE_METHOD1(int, av_fifo_size, (AVFifoBuffer *p1))
198   DEFINE_METHOD4(int, av_fifo_generic_read, (AVFifoBuffer *p1, void *p2, int p3, void (*p4)(void*, void*, int)))
199   DEFINE_METHOD4(int, av_fifo_generic_write, (AVFifoBuffer *p1, void *p2, int p3, int (*p4)(void*, void*, int)))
200   DEFINE_METHOD1(char*, av_strdup, (const char *p1))
201   DEFINE_METHOD1(int, av_get_bytes_per_sample, (enum AVSampleFormat p1))
202   DEFINE_METHOD4(AVDictionaryEntry *, av_dict_get, (AVDictionary *p1, const char *p2, const AVDictionaryEntry *p3, int p4))
203   DEFINE_METHOD4(int, av_dict_set, (AVDictionary **p1, const char *p2, const char *p3, int p4));
204   DEFINE_METHOD1(void, av_dict_free, (AVDictionary **p1));
205   DEFINE_METHOD5(int, av_samples_get_buffer_size, (int *p1, int p2, int p3, enum AVSampleFormat p4, int p5))
206   DEFINE_METHOD1(int64_t, av_get_default_channel_layout, (int p1))
207   DEFINE_METHOD6(int, av_samples_alloc, (uint8_t **p1, int *p2, int p3, int p4, enum AVSampleFormat p5, int p6))
208   DEFINE_METHOD1(int, av_sample_fmt_is_planar, (enum AVSampleFormat p1))
209   DEFINE_METHOD2(int, av_get_channel_layout_channel_index, (uint64_t p1, uint64_t p2))
210   DEFINE_METHOD7(int, av_samples_fill_arrays, (uint8_t **p1, int *p2, const uint8_t *p3, int p4, int p5, enum AVSampleFormat p6, int p7))
211   DEFINE_METHOD7(int, av_samples_copy, (uint8_t **p1, uint8_t *const *p2, int p3, int p4, int p5, int p6, enum AVSampleFormat p7))
212
213   public:
214   BEGIN_METHOD_RESOLVE()
215     RESOLVE_METHOD(av_log_set_callback)
216     RESOLVE_METHOD(av_malloc)
217     RESOLVE_METHOD(av_mallocz)
218     RESOLVE_METHOD(av_realloc)
219     RESOLVE_METHOD(av_free)
220     RESOLVE_METHOD(av_freep)
221     RESOLVE_METHOD(av_rescale_rnd)
222     RESOLVE_METHOD(av_rescale_q)
223     RESOLVE_METHOD(av_crc_init)
224     RESOLVE_METHOD(av_crc_get_table)
225     RESOLVE_METHOD(av_crc)
226     RESOLVE_METHOD(av_opt_set)
227     RESOLVE_METHOD(av_opt_set_double)
228     RESOLVE_METHOD(av_opt_set_int)
229     RESOLVE_METHOD(av_fifo_alloc)
230     RESOLVE_METHOD(av_fifo_free)
231     RESOLVE_METHOD(av_fifo_reset)
232     RESOLVE_METHOD(av_fifo_size)
233     RESOLVE_METHOD(av_fifo_generic_read)
234     RESOLVE_METHOD(av_fifo_generic_write)
235     RESOLVE_METHOD(av_strdup)
236     RESOLVE_METHOD(av_get_bytes_per_sample)
237     RESOLVE_METHOD(av_dict_get)
238     RESOLVE_METHOD(av_dict_set)
239     RESOLVE_METHOD(av_dict_free)
240     RESOLVE_METHOD(av_samples_get_buffer_size)
241     RESOLVE_METHOD(av_get_default_channel_layout)
242     RESOLVE_METHOD(av_samples_alloc)
243     RESOLVE_METHOD(av_sample_fmt_is_planar)
244     RESOLVE_METHOD(av_get_channel_layout_channel_index)
245     RESOLVE_METHOD(av_samples_fill_arrays)
246     RESOLVE_METHOD(av_samples_copy)
247   END_METHOD_RESOLVE()
248 };
249
250 #endif
251
252 class DllAvUtil : public DllAvUtilBase
253 {
254 public:
255   virtual bool Load()
256   {
257     if( DllAvUtilBase::Load() )
258     {
259       DllAvUtilBase::av_log_set_callback(ff_avutil_log);
260       return true;
261     }
262     return false;
263   }
264 };