Merge pull request #2810 from jmarshallnz/eval_conditions_before_actions
[vuplus_xbmc] / lib / DllAvFormat.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 "DllAvCodec.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   #include <libavformat/avformat.h>
41   /* xbmc_read_frame_flush() is defined for us in lib/xbmc-dll-symbols/DllAvFormat.c */
42   void xbmc_read_frame_flush(AVFormatContext *s);
43 #else
44   #include "libavformat/avformat.h"
45   #if defined(TARGET_DARWIN)
46     void ff_read_frame_flush(AVFormatContext *s);    // internal replacement 
47     #define xbmc_read_frame_flush ff_read_frame_flush
48   #endif
49 #endif
50 }
51
52 /* Flag introduced without a version bump */
53 #ifndef AVSEEK_FORCE
54 #define AVSEEK_FORCE 0x20000
55 #endif
56
57 typedef int64_t offset_t;
58
59 class DllAvFormatInterface
60 {
61 public:
62   virtual ~DllAvFormatInterface() {}
63   virtual void av_register_all_dont_call(void)=0;
64   virtual void avformat_network_init_dont_call(void)=0;
65   virtual void avformat_network_deinit_dont_call(void)=0;
66   virtual AVInputFormat *av_find_input_format(const char *short_name)=0;
67   virtual void avformat_close_input(AVFormatContext **s)=0;
68   virtual int av_read_frame(AVFormatContext *s, AVPacket *pkt)=0;
69   virtual void av_read_frame_flush(AVFormatContext *s)=0;
70   virtual int av_read_play(AVFormatContext *s)=0;
71   virtual int av_read_pause(AVFormatContext *s)=0;
72   virtual int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)=0;
73 #if (!defined USE_EXTERNAL_FFMPEG) && (!defined TARGET_DARWIN)
74   virtual int avformat_find_stream_info_dont_call(AVFormatContext *ic, AVDictionary **options)=0;
75 #endif
76   virtual int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)=0;
77   virtual AVIOContext *avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque,
78                             int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
79                             int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
80                             offset_t (*seek)(void *opaque, offset_t offset, int whence))=0;
81   virtual AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)=0;
82   virtual AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)=0;
83   virtual int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt, const char *filename, void *logctx, unsigned int offset, unsigned int max_probe_size)=0;
84   virtual void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)=0;
85   virtual int avio_open(AVIOContext **s, const char *filename, int flags)=0;
86   virtual int avio_close(AVIOContext *s)=0;
87   virtual int avio_open_dyn_buf(AVIOContext **s)=0;
88   virtual int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)=0;
89   virtual offset_t avio_seek(AVIOContext *s, offset_t offset, int whence)=0;
90   virtual int avio_read(AVIOContext *s, unsigned char *buf, int size)=0;
91   virtual void avio_w8(AVIOContext *s, int b)=0;
92   virtual void avio_write(AVIOContext *s, const unsigned char *buf, int size)=0;
93   virtual void avio_wb24(AVIOContext *s, unsigned int val)=0;
94   virtual void avio_wb32(AVIOContext *s, unsigned int val)=0;
95   virtual void avio_wb16(AVIOContext *s, unsigned int val)=0;
96   virtual AVFormatContext *avformat_alloc_context(void)=0;
97   virtual AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c)=0;
98   virtual AVOutputFormat *av_guess_format(const char *short_name, const char *filename, const char *mime_type)=0;
99   virtual int avformat_write_header (AVFormatContext *s, AVDictionary **options)=0;
100   virtual int av_write_trailer(AVFormatContext *s)=0;
101   virtual int av_write_frame  (AVFormatContext *s, AVPacket *pkt)=0;
102 };
103
104 #if (defined USE_EXTERNAL_FFMPEG) || (defined TARGET_DARWIN) 
105
106 // Use direct mapping
107 class DllAvFormat : public DllDynamic, DllAvFormatInterface
108 {
109 public:
110   virtual ~DllAvFormat() {}
111   virtual void av_register_all() 
112   { 
113     CSingleLock lock(DllAvCodec::m_critSection);
114     return ::av_register_all();
115   } 
116   virtual void av_register_all_dont_call() { *(volatile int* )0x0 = 0; } 
117   virtual void avformat_network_init_dont_call() { *(volatile int* )0x0 = 0; } 
118   virtual void avformat_network_deinit_dont_call() { *(volatile int* )0x0 = 0; } 
119   virtual AVInputFormat *av_find_input_format(const char *short_name) { return ::av_find_input_format(short_name); }
120   virtual void avformat_close_input(AVFormatContext **s) { ::avformat_close_input(s); }
121   virtual int av_read_frame(AVFormatContext *s, AVPacket *pkt) { return ::av_read_frame(s, pkt); }
122   virtual void av_read_frame_flush(AVFormatContext *s) { ::xbmc_read_frame_flush(s); }
123   virtual int av_read_play(AVFormatContext *s) { return ::av_read_play(s); }
124   virtual int av_read_pause(AVFormatContext *s) { return ::av_read_pause(s); }
125   virtual int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) { return ::av_seek_frame(s, stream_index, timestamp, flags); }
126   virtual int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
127   {
128     CSingleLock lock(DllAvCodec::m_critSection);
129     return ::avformat_find_stream_info(ic, options);
130   }
131   virtual int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
132   { return ::avformat_open_input(ps, filename, fmt, options); }
133   virtual AVIOContext *avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque,
134                             int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
135                             int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
136                             offset_t (*seek)(void *opaque, offset_t offset, int whence)) { return ::avio_alloc_context(buffer, buffer_size, write_flag, opaque, read_packet, write_packet, seek); }
137   virtual AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) {return ::av_probe_input_format(pd, is_opened); }
138   virtual AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max) {*score_max = 100; return ::av_probe_input_format(pd, is_opened); } // Use av_probe_input_format, this is not exported by ffmpeg's headers
139   virtual int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt, const char *filename, void *logctx, unsigned int offset, unsigned int max_probe_size) { return ::av_probe_input_buffer(pb, fmt, filename, logctx, offset, max_probe_size); }
140   virtual void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output) { ::av_dump_format(ic, index, url, is_output); }
141   virtual int avio_open(AVIOContext **s, const char *filename, int flags) { return ::avio_open(s, filename, flags); }
142   virtual int avio_close(AVIOContext *s) { return ::avio_close(s); }
143   virtual int avio_open_dyn_buf(AVIOContext **s) { return ::avio_open_dyn_buf(s); }
144   virtual int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer) { return ::avio_close_dyn_buf(s, pbuffer); }
145   virtual offset_t avio_seek(AVIOContext *s, offset_t offset, int whence) { return ::avio_seek(s, offset, whence); }
146   virtual int avio_read(AVIOContext *s, unsigned char *buf, int size) { return ::avio_read(s, buf, size); }
147   virtual void avio_w8(AVIOContext *s, int b) { ::avio_w8(s, b); }
148   virtual void avio_write(AVIOContext *s, const unsigned char *buf, int size) { ::avio_write(s, buf, size); }
149   virtual void avio_wb24(AVIOContext *s, unsigned int val) { ::avio_wb24(s, val); }
150   virtual void avio_wb32(AVIOContext *s, unsigned int val) { ::avio_wb32(s, val); }
151   virtual void avio_wb16(AVIOContext *s, unsigned int val) { ::avio_wb16(s, val); }
152   virtual AVFormatContext *avformat_alloc_context() { return ::avformat_alloc_context(); }
153   virtual AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c) { return ::avformat_new_stream(s, c); }
154   virtual AVOutputFormat *av_guess_format(const char *short_name, const char *filename, const char *mime_type) { return ::av_guess_format(short_name, filename, mime_type); }
155   virtual int avformat_write_header (AVFormatContext *s, AVDictionary **options) { return ::avformat_write_header (s, options); }
156   virtual int av_write_trailer(AVFormatContext *s) { return ::av_write_trailer(s); }
157   virtual int av_write_frame  (AVFormatContext *s, AVPacket *pkt) { return ::av_write_frame(s, pkt); }
158
159   // DLL faking.
160   virtual bool ResolveExports() { return true; }
161   virtual bool Load() {
162 #if !defined(TARGET_DARWIN)
163     CLog::Log(LOGDEBUG, "DllAvFormat: Using libavformat system library");
164 #endif
165     CSingleLock lock(DllAvCodec::m_critSection);
166     if (++m_avformat_refcnt == 1)
167       ::avformat_network_init();
168     return true;
169   }
170   virtual void Unload() {
171     CSingleLock lock(DllAvCodec::m_critSection);
172     if (--m_avformat_refcnt == 0)
173       ::avformat_network_deinit();
174   }
175
176 protected:
177   static int m_avformat_refcnt;
178 };
179
180 #else
181
182 class DllAvFormat : public DllDynamic, DllAvFormatInterface
183 {
184   DECLARE_DLL_WRAPPER(DllAvFormat, DLL_PATH_LIBAVFORMAT)
185
186   LOAD_SYMBOLS()
187
188   DEFINE_METHOD0(void, av_register_all_dont_call)
189   DEFINE_METHOD0(void, avformat_network_init_dont_call)
190   DEFINE_METHOD0(void, avformat_network_deinit_dont_call)
191   DEFINE_METHOD1(AVInputFormat*, av_find_input_format, (const char *p1))
192   DEFINE_METHOD1(void, avformat_close_input, (AVFormatContext **p1))
193   DEFINE_METHOD1(int, av_read_play, (AVFormatContext *p1))
194   DEFINE_METHOD1(int, av_read_pause, (AVFormatContext *p1))
195   DEFINE_METHOD1(void, av_read_frame_flush, (AVFormatContext *p1))
196   DEFINE_FUNC_ALIGNED2(int, __cdecl, av_read_frame, AVFormatContext *, AVPacket *)
197   DEFINE_FUNC_ALIGNED4(int, __cdecl, av_seek_frame, AVFormatContext*, int, int64_t, int)
198   DEFINE_FUNC_ALIGNED2(int, __cdecl, avformat_find_stream_info_dont_call, AVFormatContext*, AVDictionary **)
199   DEFINE_FUNC_ALIGNED4(int, __cdecl, avformat_open_input, AVFormatContext **, const char *, AVInputFormat *, AVDictionary **)
200   DEFINE_FUNC_ALIGNED2(AVInputFormat*, __cdecl, av_probe_input_format, AVProbeData*, int)
201   DEFINE_FUNC_ALIGNED3(AVInputFormat*, __cdecl, av_probe_input_format2, AVProbeData*, int, int*)
202   DEFINE_FUNC_ALIGNED6(int, __cdecl, av_probe_input_buffer, AVIOContext *, AVInputFormat **, const char *, void *, unsigned int, unsigned int)
203   DEFINE_FUNC_ALIGNED3(int, __cdecl, avio_read, AVIOContext*, unsigned char *, int)
204   DEFINE_FUNC_ALIGNED2(void, __cdecl, avio_w8, AVIOContext*, int)
205   DEFINE_FUNC_ALIGNED3(void, __cdecl, avio_write, AVIOContext*, const unsigned char *, int)
206   DEFINE_FUNC_ALIGNED2(void, __cdecl, avio_wb24, AVIOContext*, unsigned int)
207   DEFINE_FUNC_ALIGNED2(void, __cdecl, avio_wb32, AVIOContext*, unsigned int)
208   DEFINE_FUNC_ALIGNED2(void, __cdecl, avio_wb16, AVIOContext*, unsigned int)
209   DEFINE_METHOD7(AVIOContext *, avio_alloc_context, (unsigned char *p1, int p2, int p3, void *p4,
210                   int (*p5)(void *opaque, uint8_t *buf, int buf_size),
211                   int (*p6)(void *opaque, uint8_t *buf, int buf_size),
212                   offset_t (*p7)(void *opaque, offset_t offset, int whence)))
213   DEFINE_METHOD4(void, av_dump_format, (AVFormatContext *p1, int p2, const char *p3, int p4))
214   DEFINE_METHOD3(int, avio_open, (AVIOContext **p1, const char *p2, int p3))
215   DEFINE_METHOD1(int, avio_close, (AVIOContext *p1))
216   DEFINE_METHOD1(int, avio_open_dyn_buf, (AVIOContext **p1))
217   DEFINE_METHOD2(int, avio_close_dyn_buf, (AVIOContext *p1, uint8_t **p2))
218   DEFINE_METHOD3(offset_t, avio_seek, (AVIOContext *p1, offset_t p2, int p3))
219   DEFINE_METHOD0(AVFormatContext *, avformat_alloc_context)
220   DEFINE_METHOD2(AVStream *, avformat_new_stream, (AVFormatContext *p1, AVCodec *p2))
221   DEFINE_METHOD3(AVOutputFormat *, av_guess_format, (const char *p1, const char *p2, const char *p3))
222   DEFINE_METHOD2(int, avformat_write_header , (AVFormatContext *p1, AVDictionary **p2))
223   DEFINE_METHOD1(int, av_write_trailer, (AVFormatContext *p1))
224   DEFINE_METHOD2(int, av_write_frame  , (AVFormatContext *p1, AVPacket *p2))
225   BEGIN_METHOD_RESOLVE()
226     RESOLVE_METHOD_RENAME(av_register_all, av_register_all_dont_call)
227     RESOLVE_METHOD_RENAME(avformat_network_init,   avformat_network_init_dont_call)
228     RESOLVE_METHOD_RENAME(avformat_network_deinit, avformat_network_deinit_dont_call)
229     RESOLVE_METHOD(av_find_input_format)
230     RESOLVE_METHOD(avformat_close_input)
231     RESOLVE_METHOD(av_read_frame)
232     RESOLVE_METHOD(av_read_play)
233     RESOLVE_METHOD(av_read_pause)
234     RESOLVE_METHOD(av_read_frame_flush)
235     RESOLVE_METHOD(av_seek_frame)
236     RESOLVE_METHOD_RENAME(avformat_find_stream_info, avformat_find_stream_info_dont_call)
237     RESOLVE_METHOD(avformat_open_input)
238     RESOLVE_METHOD(avio_alloc_context)
239     RESOLVE_METHOD(av_probe_input_format)
240     RESOLVE_METHOD(av_probe_input_format2)
241     RESOLVE_METHOD(av_probe_input_buffer)
242     RESOLVE_METHOD(av_dump_format)
243     RESOLVE_METHOD(avio_open)
244     RESOLVE_METHOD(avio_close)
245     RESOLVE_METHOD(avio_open_dyn_buf)
246     RESOLVE_METHOD(avio_close_dyn_buf)
247     RESOLVE_METHOD(avio_seek)
248     RESOLVE_METHOD(avio_read)
249     RESOLVE_METHOD(avio_w8)
250     RESOLVE_METHOD(avio_write)
251     RESOLVE_METHOD(avio_wb24)
252     RESOLVE_METHOD(avio_wb32)
253     RESOLVE_METHOD(avio_wb16)
254     RESOLVE_METHOD(avformat_alloc_context)
255     RESOLVE_METHOD(avformat_new_stream)
256     RESOLVE_METHOD(av_guess_format)
257     RESOLVE_METHOD(avformat_write_header)
258     RESOLVE_METHOD(av_write_trailer)
259     RESOLVE_METHOD(av_write_frame)
260   END_METHOD_RESOLVE()
261
262   /* dependencies of libavformat */
263   DllAvCodec m_dllAvCodec;
264   // DllAvUtil loaded implicitely by m_dllAvCodec
265
266 public:
267   void av_register_all()
268   {
269     CSingleLock lock(DllAvCodec::m_critSection);
270     av_register_all_dont_call();
271   }
272   int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
273   {
274     CSingleLock lock(DllAvCodec::m_critSection);
275     return avformat_find_stream_info_dont_call(ic, options);
276   }
277
278   virtual bool Load()
279   {
280     if (!m_dllAvCodec.Load())
281       return false;
282     bool loaded = DllDynamic::Load();
283
284     CSingleLock lock(DllAvCodec::m_critSection);
285     if (++m_avformat_refcnt == 1 && loaded)
286       avformat_network_init_dont_call();
287     return loaded;
288   }
289
290   virtual void Unload()
291   {
292     CSingleLock lock(DllAvCodec::m_critSection);
293     if (--m_avformat_refcnt == 0 && DllDynamic::IsLoaded())
294       avformat_network_deinit_dont_call();
295
296     DllDynamic::Unload();
297   }
298
299 protected:
300   static int m_avformat_refcnt;
301 };
302
303 #endif