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