-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTheoraPlay.cs
202 lines (173 loc) · 6.34 KB
/
TheoraPlay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* TheoraPlay# - C# Wrapper for TheoraPlay
*
* Copyright (c) 2013-2014 Ethan Lee.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Ethan "flibitijibibo" Lee <[email protected]>
*
*/
using System;
using System.Runtime.InteropServices;
// TODO: THEORAPLAY_Io and its functions.
public class TheoraPlay
{
const string theoraplay_libname = "libtheoraplay.dll";
public enum THEORAPLAY_VideoFormat
{
THEORAPLAY_VIDFMT_YV12,
THEORAPLAY_VIDFMT_IYUV,
THEORAPLAY_VIDFMT_RGB,
THEORAPLAY_VIDFMT_RGBA
}
[StructLayout(LayoutKind.Explicit)]
private struct THEORAPLAY_VideoFrame32
{
[FieldOffset(0)]
public uint playms;
[FieldOffset(4)]
public double fps;
[FieldOffset(12)]
public uint width;
[FieldOffset(16)]
public uint height;
[FieldOffset(20)]
public THEORAPLAY_VideoFormat format;
[FieldOffset(24)]
public IntPtr pixels; // unsigned char*
[FieldOffset(32)]
public IntPtr next; // struct THEORAPLAY_VideoFrame*
}
[StructLayout(LayoutKind.Sequential)]
public struct THEORAPLAY_VideoFrame
{
public uint playms;
public double fps;
public uint width;
public uint height;
public THEORAPLAY_VideoFormat format;
public IntPtr pixels; // unsigned char*
public IntPtr next; // struct THEORAPLAY_VideoFrame*
}
[StructLayout(LayoutKind.Sequential)]
public struct THEORAPLAY_AudioPacket
{
public uint playms; // playback start time in milliseconds.
public int channels;
public int freq;
public int frames;
public IntPtr samples; // float*; frames * channels float32 samples.
public IntPtr next; // struct THEORAPLAY_AudioPacket*
}
/* Note: The IntPtr return value is a THEORAPLAY_Decoder. */
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr THEORAPLAY_startDecodeFile(
[In()] [MarshalAs(UnmanagedType.LPStr)]
string fname, // const char*
uint maxframes,
THEORAPLAY_VideoFormat vidfmt
);
/* Decoder State Functions
* The IntPtr parameter is a THEORAPLAY_Decoder*.
*/
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern void THEORAPLAY_stopDecode(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern int THEORAPLAY_isDecoding(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern int THEORAPLAY_decodingError(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern int THEORAPLAY_isInitialized(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern int THEORAPLAY_hasVideoStream(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern int THEORAPLAY_hasAudioStream(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern uint THEORAPLAY_availableVideo(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern uint THEORAPLAY_availableAudio(IntPtr decoder);
/* Audio Data Functions
* For these functions, IntPtr refers to a THEORAPLAY_AudioPacket*.
* The exception is the decoder, which is still a THEORAPLAY_Decoder*.
*/
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr THEORAPLAY_getAudio(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern void THEORAPLAY_freeAudio(IntPtr item);
/* Video Data Functions
* For these functions, IntPtr refers to a THEORAPLAY_VideoFrame*.
* The exception is the decoder, which is still a THEORAPLAY_Decoder*.
*/
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr THEORAPLAY_getVideo(IntPtr decoder);
[DllImport(theoraplay_libname, CallingConvention = CallingConvention.Cdecl)]
public static extern void THEORAPLAY_freeVideo(IntPtr item);
/* These are public methods that are NOT a part of TheoraPlay.
* We have this to prevent architecture incompatibility.
*/
public static unsafe THEORAPLAY_VideoFrame getVideoFrame(IntPtr frame)
{
THEORAPLAY_VideoFrame theFrame;
unsafe
{
/* This is only a problem for Mono. Ignore for Win32 */
if ( Environment.OSVersion.Platform != PlatformID.Win32NT &&
IntPtr.Size == 4 )
{
THEORAPLAY_VideoFrame32* frame32Ptr = (THEORAPLAY_VideoFrame32*) frame;
THEORAPLAY_VideoFrame32 frame32 = *frame32Ptr;
theFrame.playms = frame32.playms;
theFrame.fps = frame32.fps;
theFrame.width = frame32.width;
theFrame.height = frame32.height;
theFrame.format = frame32.format;
theFrame.pixels = frame32.pixels;
theFrame.next = frame32.next;
}
else
{
THEORAPLAY_VideoFrame* framePtr = (THEORAPLAY_VideoFrame*) frame;
theFrame = *framePtr;
}
}
return theFrame;
}
public static unsafe THEORAPLAY_AudioPacket getAudioPacket(IntPtr packet)
{
THEORAPLAY_AudioPacket thePacket;
unsafe
{
THEORAPLAY_AudioPacket* packetPtr = (THEORAPLAY_AudioPacket*) packet;
thePacket = *packetPtr;
}
return thePacket;
}
public static float[] getSamples(IntPtr samples, int packetSize)
{
float[] theSamples = new float[packetSize];
Marshal.Copy(samples, theSamples, 0, packetSize);
return theSamples;
}
public static byte[] getPixels(IntPtr pixels, int imageSize)
{
byte[] thePixels = new byte[imageSize];
Marshal.Copy(pixels, thePixels, 0, imageSize);
return thePixels;
}
}