[cosmetics] update date in GPL header
[vuplus_xbmc] / lib / DllSwResample.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
18  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  *  http://www.gnu.org/copyleft/gpl.html
20  *
21  */
22
23 #if (defined HAVE_CONFIG_H) && (!defined WIN32)
24   #include "config.h"
25 #endif
26 #include "DynamicDll.h"
27 #include "utils/log.h"
28
29 extern "C" {
30 #ifndef HAVE_MMX
31 #define HAVE_MMX
32 #endif
33 #ifndef __STDC_CONSTANT_MACROS
34 #define __STDC_CONSTANT_MACROS
35 #endif
36 #ifndef __GNUC__
37 #pragma warning(disable:4244)
38 #endif
39 #if (defined USE_EXTERNAL_FFMPEG)
40   #if HAVE_LIBSWRESAMPLE_SWRESAMPLE_H
41     #include <libswresample/swresample.h>
42   #elif HAVE_LIBAVRESAMPLE_AVRESAMPLE_H
43     #include <libavresample/avresample.h>
44     #include <libavutil/opt.h>
45     #include <libavutil/samplefmt.h>
46     #define SwrContext AVAudioResampleContext
47   #else
48     #error "Either libswresample or libavresample is needed!"
49   #endif
50 #else
51   #include "libswresample/swresample.h"
52 #endif
53 }
54
55 class DllSwResampleInterface
56 {
57 public:
58   virtual ~DllSwResampleInterface() {}
59   virtual struct SwrContext *swr_alloc_set_opts(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx)=0;
60   virtual int swr_init(struct SwrContext *s)=0;
61   virtual void swr_free(struct SwrContext **s)=0;
62   virtual int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in , int in_count)=0;
63 };
64
65 #if (defined USE_EXTERNAL_FFMPEG) || (defined TARGET_DARWIN) 
66
67 #if HAVE_LIBSWRESAMPLE_SWRESAMPLE_H || (defined TARGET_DARWIN)
68 // Use direct mapping
69 class DllSwResample : public DllDynamic, DllSwResampleInterface
70 {
71 public:
72   virtual ~DllSwResample() {}
73
74   // DLL faking.
75   virtual bool ResolveExports() { return true; }
76   virtual bool Load() {
77 #if !defined(TARGET_DARWIN)
78     CLog::Log(LOGDEBUG, "DllAvFormat: Using libswresample system library");
79 #endif
80     return true;
81   }
82   virtual void Unload() {}
83   virtual struct SwrContext *swr_alloc_set_opts(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx) { return ::swr_alloc_set_opts(s, out_ch_layout, out_sample_fmt, out_sample_rate, in_ch_layout, in_sample_fmt, in_sample_rate, log_offset, log_ctx); }
84   virtual int swr_init(struct SwrContext *s) { return ::swr_init(s); }
85   virtual void swr_free(struct SwrContext **s){ return ::swr_free(s); }
86   virtual int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in , int in_count){ return ::swr_convert(s, out, out_count, in, in_count); }
87 };
88 #else
89 // Wrap the same API through libavresample.
90 class DllSwResample : public DllDynamic, DllSwResampleInterface
91 {
92 public:
93   virtual ~DllSwResample() {}
94
95   // DLL faking.
96   virtual bool ResolveExports() { return true; }
97   virtual bool Load() {
98 #if !defined(TARGET_DARWIN)
99     CLog::Log(LOGDEBUG, "DllAvFormat: Using libavresample system library");
100 #endif
101     return true;
102   }
103   virtual void Unload() {}
104   virtual struct SwrContext *swr_alloc_set_opts(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx) {
105           AVAudioResampleContext *ret = ::avresample_alloc_context();
106           av_opt_set_int(ret, "out_channel_layout", out_ch_layout  , 0);
107           av_opt_set_int(ret, "out_sample_fmt"    , out_sample_fmt , 0);
108           av_opt_set_int(ret, "out_sample_rate"   , out_sample_rate, 0);
109           av_opt_set_int(ret, "in_channel_layout" , in_ch_layout   , 0);
110           av_opt_set_int(ret, "in_sample_fmt"     , in_sample_fmt  , 0);
111           av_opt_set_int(ret, "in_sample_rate"    , in_sample_rate , 0);
112           return ret;
113   }
114   virtual int swr_init(struct SwrContext *s) { return ::avresample_open(s); }
115   virtual void swr_free(struct SwrContext **s){ ::avresample_close(*s); *s = NULL; }
116   virtual int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in , int in_count){ return ::avresample_convert(s, out, 0, out_count, (uint8_t**)in, 0,in_count); }
117 };
118 #endif
119
120 #else
121
122 class DllSwResample : public DllDynamic, DllSwResampleInterface
123 {
124   DECLARE_DLL_WRAPPER(DllSwResample, DLL_PATH_LIBSWRESAMPLE)
125
126   LOAD_SYMBOLS()
127
128   DEFINE_METHOD9(SwrContext*, swr_alloc_set_opts, (struct SwrContext *p1, int64_t p2, enum AVSampleFormat p3, int p4, int64_t p5, enum AVSampleFormat p6, int p7, int p8, void * p9));
129   DEFINE_METHOD1(int, swr_init, (struct SwrContext *p1))
130   DEFINE_METHOD1(void, swr_free, (struct SwrContext **p1))
131   DEFINE_METHOD5(int, swr_convert, (struct SwrContext *p1, uint8_t **p2, int p3, const uint8_t **p4, int p5))
132
133   BEGIN_METHOD_RESOLVE()
134     RESOLVE_METHOD(swr_alloc_set_opts)
135     RESOLVE_METHOD(swr_init)
136     RESOLVE_METHOD(swr_free)
137     RESOLVE_METHOD(swr_convert)
138   END_METHOD_RESOLVE()
139
140   /* dependencies of libavformat */
141   DllAvUtil m_dllAvUtil;
142
143 public:
144
145   virtual bool Load()
146   {
147     if (!m_dllAvUtil.Load())
148       return false;
149     return DllDynamic::Load();
150   }
151 };
152
153 #endif