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