Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ffmpeg_init.cpp
Go to the documentation of this file.
1 //
2 //C++ Implementation: vid_file
3 //
4 
5 #include "vsfilesystem.h"
6 #include "config.h"
7 #include "ffmpeg_init.h"
8 
9 #include <string.h>
10 #include <utility>
11 
12 //define a 128k buffer for video streamers
13 #define BUFFER_SIZE 128*(1<<10)
14 
15 #ifndef ENOENT
16 #define ENOENT 2
17 #endif
18 
19 /*
20  * FOLLOWING CODE IS ONLY INCLUDED IF YOU HAVE FFMPEG
21  * ********************************************
22  */
23 #ifdef HAVE_FFMPEG
24 //#ifdef _WIN32
25 //#define offset_t xoffset_t
26 //#endif
27 
28 #ifndef offset_t
29  #if (LIBAVCODEC_VERSION_MAJOR >= 52) || (LIBAVCODEC_VERSION_INT >= ( ( 51<<16)+(49<<8)+0 ) ) || defined (__amd64__) \
30  || defined (_M_AMD64) || defined (__x86_64) || defined (__x86_64__)
31 typedef int64_t offset_t;
32  #else
33 typedef int offset_t;
34  #endif
35 #endif
36 
37 using namespace VSFileSystem;
38 
39 extern "C" int _url_open( URLContext *h, const char *filename, int flags )
40 {
41  if (strncmp( filename, "vsfile:", 7 ) != 0)
42  return AVERROR( ENOENT );
43 
44  const char *type = strchr( filename+7, '|' );
45  std::string path( filename+7, type ? type-filename-7 : strlen( filename+7 ) );
46  VSFileType vstype = ( (type && *type) ? (VSFileType) atoi( type+1 ) : VideoFile);
47 
48  VSFile *f = new VSFile();
49  if (f->OpenReadOnly( path, vstype ) > Ok) {
50  delete f;
51  return AVERROR( ENOENT );
52  } else {
53  h->priv_data = f;
54  return 0;
55  }
56 }
57 
58 extern "C" int _url_close( URLContext *h )
59 {
60  delete (VSFile*) (h->priv_data);
61  return 0;
62 }
63 
64 extern "C" int _url_read( URLContext *h, unsigned char *buf, int size )
65 {
66  return ( (VSFile*) (h->priv_data) )->Read( buf, size );
67 }
68 
69 // Changed on June 1, 2010. During minor version 67.
70 // See http://git.ffmpeg.org/?p=ffmpeg;a=commitdiff;h=2967315b9ee7afa15d2849b473e359f50a815696
71 extern "C" int _url_write( URLContext *h,
72 #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(52, 67, 0)
73  const
74 #endif
75  unsigned char *buf, int size )
76 {
77  //read-only please
78  return 0;
79 }
80 
81 extern "C" offset_t _url_seek( URLContext *h, offset_t pos, int whence )
82 {
83  if (whence != AVSEEK_SIZE) {
84  ( (VSFile*) (h->priv_data) )->GoTo( long(pos) );
85  return ( (VSFile*) (h->priv_data) )->GetPosition();
86  } else {
87  return ( (VSFile*) (h->priv_data) )->Size();
88  }
89 }
90 
91 struct URLProtocol vsFileProtocol = {
92  "vsfile",
93  _url_open,
94  _url_read,
95  _url_write,
96  _url_seek,
97  _url_close,
98 
99 #if (LIBAVCODEC_VERSION_MAJOR >= 53)
100  NULL, NULL, NULL, NULL,
101  0,
102  NULL,
103  0,
104  NULL
105 #endif
106 };
107 
108 namespace FFMpeg
109 {
110 void initLibraries()
111 {
112  static bool initted = false;
113  if (!initted) {
114  initted = true;
115  av_register_all();
116  #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
117  av_register_protocol2( &vsFileProtocol, sizeof(vsFileProtocol) );
118  #else
119  register_protocol( &vsFileProtocol );
120  #endif
121  }
122 }
123 };
124 
125 //Workaround for a missing export in libavcodec 52.47.0
126 #if (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR == 47 && LIBAVCODEC_VERSION_MICRO == 0)
127 extern "C" {
128 void av_free_packet( AVPacket *pkt )
129 {
130  if (pkt) {
131  if (pkt->destruct) pkt->destruct( pkt );
132  pkt->data = NULL;
133  pkt->size = 0;
134  }
135 }
136 }
137 #endif
138 
139 #else //No FFMPEG
140 
141 namespace FFMpeg
142 {
144 {
145  //No-op stub
146 }
147 };
148 
149 #endif
150