Merge pull request #2760 from aballier/ffmpeg_cleanups
[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 #ifndef __GNUC__
63 #pragma warning(pop)
64 #endif
65
66 // calback used for logging
67 void ff_avutil_log(void* ptr, int level, const char* format, va_list va);
68
69 class DllAvUtilInterface
70 {
71 public:
72   virtual ~DllAvUtilInterface() {}
73   virtual void av_log_set_callback(void (*)(void*, int, const char*, va_list))=0;
74   virtual void *av_malloc(unsigned int size)=0;
75   virtual void *av_mallocz(unsigned int size)=0;
76   virtual void *av_realloc(void *ptr, unsigned int size)=0;
77   virtual void av_free(void *ptr)=0;
78   virtual void av_freep(void *ptr)=0;
79   virtual int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding)=0;
80   virtual int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)=0;
81   virtual int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)=0;
82   virtual const AVCRC* av_crc_get_table(AVCRCId crc_id)=0;
83   virtual uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)=0;
84   virtual int av_opt_set(void *obj, const char *name, const char *val, int search_flags)=0;
85   virtual AVFifoBuffer *av_fifo_alloc(unsigned int size) = 0;
86   virtual void av_fifo_free(AVFifoBuffer *f) = 0;
87   virtual void av_fifo_reset(AVFifoBuffer *f) = 0;
88   virtual int av_fifo_size(AVFifoBuffer *f) = 0;
89   virtual int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int)) = 0;
90   virtual int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int)) = 0;
91   virtual char *av_strdup(const char *s)=0;
92   virtual int av_get_bytes_per_sample(enum AVSampleFormat p1) = 0;
93   virtual AVDictionaryEntry *av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags) = 0;
94   virtual int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)=0;
95   virtual void av_dict_free(AVDictionary **pm) = 0;
96   virtual int av_samples_get_buffer_size (int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align) = 0;
97   virtual int64_t av_get_default_channel_layout(int nb_channels)=0;
98 };
99
100 #if defined (USE_EXTERNAL_FFMPEG) || (defined TARGET_DARWIN)
101 // Use direct layer
102 class DllAvUtilBase : public DllDynamic, DllAvUtilInterface
103 {
104 public:
105
106   virtual ~DllAvUtilBase() {}
107    virtual void av_log_set_callback(void (*foo)(void*, int, const char*, va_list)) { ::av_log_set_callback(foo); }
108    virtual void *av_malloc(unsigned int size) { return ::av_malloc(size); }
109    virtual void *av_mallocz(unsigned int size) { return ::av_mallocz(size); }
110    virtual void *av_realloc(void *ptr, unsigned int size) { return ::av_realloc(ptr, size); }
111    virtual void av_free(void *ptr) { ::av_free(ptr); }
112    virtual void av_freep(void *ptr) { ::av_freep(ptr); }
113    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); }
114    virtual int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) { return ::av_rescale_q(a, bq, cq); }
115    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); }
116    virtual const AVCRC* av_crc_get_table(AVCRCId crc_id) { return ::av_crc_get_table(crc_id); }
117    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); }
118    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); }
119   virtual AVFifoBuffer *av_fifo_alloc(unsigned int size) {return ::av_fifo_alloc(size); }
120   virtual void av_fifo_free(AVFifoBuffer *f) { ::av_fifo_free(f); }
121   virtual void av_fifo_reset(AVFifoBuffer *f) { ::av_fifo_reset(f); }
122   virtual int av_fifo_size(AVFifoBuffer *f) { return ::av_fifo_size(f); }
123   virtual int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int))
124     { return ::av_fifo_generic_read(f, dest, buf_size, func); }
125   virtual int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int))
126     { return ::av_fifo_generic_write(f, src, size, func); }
127   virtual char *av_strdup(const char *s) { return ::av_strdup(s); }
128   virtual int av_get_bytes_per_sample(enum AVSampleFormat p1)
129     { return ::av_get_bytes_per_sample(p1); }
130   virtual AVDictionaryEntry *av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags){ return ::av_dict_get(m, key, prev, flags); }
131   virtual int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags) { return ::av_dict_set(pm, key, value, flags); }
132   virtual void av_dict_free(AVDictionary **pm) { ::av_dict_free(pm); }
133   virtual int av_samples_get_buffer_size (int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
134     { return ::av_samples_get_buffer_size(linesize, nb_channels, nb_samples, sample_fmt, align); }
135   virtual int64_t av_get_default_channel_layout(int nb_channels) { return ::av_get_default_channel_layout(nb_channels); }
136
137    // DLL faking.
138    virtual bool ResolveExports() { return true; }
139    virtual bool Load() {
140 #if !defined(TARGET_DARWIN)
141      CLog::Log(LOGDEBUG, "DllAvUtilBase: Using libavutil system library");
142 #endif
143      return true;
144    }
145    virtual void Unload() {}
146 };
147
148 #else
149
150 class DllAvUtilBase : public DllDynamic, DllAvUtilInterface
151 {
152   DECLARE_DLL_WRAPPER(DllAvUtilBase, DLL_PATH_LIBAVUTIL)
153
154   LOAD_SYMBOLS()
155
156   DEFINE_METHOD1(void, av_log_set_callback, (void (*p1)(void*, int, const char*, va_list)))
157   DEFINE_METHOD1(void*, av_malloc, (unsigned int p1))
158   DEFINE_METHOD1(void*, av_mallocz, (unsigned int p1))
159   DEFINE_METHOD2(void*, av_realloc, (void *p1, unsigned int p2))
160   DEFINE_METHOD1(void, av_free, (void *p1))
161   DEFINE_METHOD1(void, av_freep, (void *p1))
162   DEFINE_METHOD4(int64_t, av_rescale_rnd, (int64_t p1, int64_t p2, int64_t p3, enum AVRounding p4));
163   DEFINE_METHOD3(int64_t, av_rescale_q, (int64_t p1, AVRational p2, AVRational p3));
164   DEFINE_METHOD1(const AVCRC*, av_crc_get_table, (AVCRCId p1))
165   DEFINE_METHOD5(int, av_crc_init, (AVCRC *p1, int p2, int p3, uint32_t p4, int p5));
166   DEFINE_METHOD4(uint32_t, av_crc, (const AVCRC *p1, uint32_t p2, const uint8_t *p3, size_t p4));
167   DEFINE_METHOD4(int, av_opt_set, (void *p1, const char *p2, const char *p3, int p4));
168   DEFINE_METHOD1(AVFifoBuffer*, av_fifo_alloc, (unsigned int p1))
169   DEFINE_METHOD1(void, av_fifo_free, (AVFifoBuffer *p1))
170   DEFINE_METHOD1(void, av_fifo_reset, (AVFifoBuffer *p1))
171   DEFINE_METHOD1(int, av_fifo_size, (AVFifoBuffer *p1))
172   DEFINE_METHOD4(int, av_fifo_generic_read, (AVFifoBuffer *p1, void *p2, int p3, void (*p4)(void*, void*, int)))
173   DEFINE_METHOD4(int, av_fifo_generic_write, (AVFifoBuffer *p1, void *p2, int p3, int (*p4)(void*, void*, int)))
174   DEFINE_METHOD1(char*, av_strdup, (const char *p1))
175   DEFINE_METHOD1(int, av_get_bytes_per_sample, (enum AVSampleFormat p1))
176   DEFINE_METHOD4(AVDictionaryEntry *, av_dict_get, (AVDictionary *p1, const char *p2, const AVDictionaryEntry *p3, int p4))
177   DEFINE_METHOD4(int, av_dict_set, (AVDictionary **p1, const char *p2, const char *p3, int p4));
178   DEFINE_METHOD1(void, av_dict_free, (AVDictionary **p1));
179   DEFINE_METHOD5(int, av_samples_get_buffer_size, (int *p1, int p2, int p3, enum AVSampleFormat p4, int p5))
180   DEFINE_METHOD1(int64_t, av_get_default_channel_layout, (int p1))
181
182   public:
183   BEGIN_METHOD_RESOLVE()
184     RESOLVE_METHOD(av_log_set_callback)
185     RESOLVE_METHOD(av_malloc)
186     RESOLVE_METHOD(av_mallocz)
187     RESOLVE_METHOD(av_realloc)
188     RESOLVE_METHOD(av_free)
189     RESOLVE_METHOD(av_freep)
190     RESOLVE_METHOD(av_rescale_rnd)
191     RESOLVE_METHOD(av_rescale_q)
192     RESOLVE_METHOD(av_crc_init)
193     RESOLVE_METHOD(av_crc_get_table)
194     RESOLVE_METHOD(av_crc)
195     RESOLVE_METHOD(av_opt_set)
196     RESOLVE_METHOD(av_fifo_alloc)
197     RESOLVE_METHOD(av_fifo_free)
198     RESOLVE_METHOD(av_fifo_reset)
199     RESOLVE_METHOD(av_fifo_size)
200     RESOLVE_METHOD(av_fifo_generic_read)
201     RESOLVE_METHOD(av_fifo_generic_write)
202     RESOLVE_METHOD(av_strdup)
203     RESOLVE_METHOD(av_get_bytes_per_sample)
204     RESOLVE_METHOD(av_dict_get)
205     RESOLVE_METHOD(av_dict_set)
206     RESOLVE_METHOD(av_dict_free)
207     RESOLVE_METHOD(av_samples_get_buffer_size)
208     RESOLVE_METHOD(av_get_default_channel_layout)
209   END_METHOD_RESOLVE()
210 };
211
212 #endif
213
214 class DllAvUtil : public DllAvUtilBase
215 {
216 public:
217   virtual bool Load()
218   {
219     if( DllAvUtilBase::Load() )
220     {
221       DllAvUtilBase::av_log_set_callback(ff_avutil_log);
222       return true;
223     }
224     return false;
225   }
226 };