Skip to content

Commit 534a3eb

Browse files
authored
FFmpeg abstractions API (#227)
* First prototype * First - something is working version * Drop old generator and update example to use new packages * simplify bindings * Working version * Test Dynamically Linked version * Working version * Add bsf.h * FunctionLoader -> FunctionResolver * Fix typo * Fix namings * Add in modifier for const fixed arrays #221 * Fix build warnings * Fix example project * Improve solution navigation Co-authored-by: Ruslan Balanukhin <[email protected]>
1 parent 14c0520 commit 534a3eb

File tree

124 files changed

+52536
-27070
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+52536
-27070
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,8 @@ UpgradeLog*.XML
106106
*.stackdump
107107

108108
# Local
109-
/FFmpeg.AutoGen.Example/frame.*.jpg
109+
/FFmpeg.AutoGen.Example/frames
110+
111+
# JetBrains Rider
112+
.idea/
113+
*.sln.iml

Directory.Build.props

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project>
22
<PropertyGroup>
33
<PackageId>$(AssemblyName)</PackageId>
4-
<Version>5.1.1</Version>
4+
<Version>5.1.1.1</Version>
55
<Authors>Ruslan Balanukhin</Authors>
6-
<Company>Rational Core</Company>
6+
<Company>Rationale One</Company>
77
<Product>FFmpeg.AutoGen</Product>
8-
<Copyright>Copyright © Ruslan Balanukhin 2021 All rights reserved.</Copyright>
8+
<Copyright>Copyright © Ruslan Balanukhin 2022 All rights reserved.</Copyright>
99
<PackageProjectUrl>https://github.com/Ruslan-B/FFmpeg.AutoGen</PackageProjectUrl>
1010
<RepositoryType>Git</RepositoryType>
1111
<AssemblyVersion>$(Version)</AssemblyVersion>
@@ -16,7 +16,8 @@
1616
</PropertyGroup>
1717

1818
<PropertyGroup>
19-
<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>
19+
<!--<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>-->
20+
<PublicSign>true</PublicSign>
2021
<SignAssembly>true</SignAssembly>
2122
<DelaySign>false</DelaySign>
2223
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)FFmpeg.AutoGen.snk</AssemblyOriginatorKeyFile>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace FFmpeg.AutoGen.Abstractions;
5+
6+
public class ConstCharPtrMarshaler : ICustomMarshaler
7+
{
8+
private static readonly ConstCharPtrMarshaler Instance = new();
9+
public object MarshalNativeToManaged(IntPtr pNativeData) => Marshal.PtrToStringAnsi(pNativeData);
10+
11+
public IntPtr MarshalManagedToNative(object managedObj) => IntPtr.Zero;
12+
13+
public void CleanUpNativeData(IntPtr pNativeData)
14+
{
15+
}
16+
17+
public void CleanUpManagedData(object managedObj)
18+
{
19+
}
20+
21+
public int GetNativeDataSize() => IntPtr.Size;
22+
23+
public static ICustomMarshaler GetInstance(string cookie) => Instance;
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.1;netstandard2.0;net45</TargetFrameworks>
5+
<Description>FFmpeg auto generated unsafe bindings for C#/.NET and Mono. Abstractions todo</Description>
6+
<GeneratePackageOnBuild Condition=" $(Configuration) == 'Release' ">true</GeneratePackageOnBuild>
7+
</PropertyGroup>
8+
9+
<PropertyGroup>
10+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
11+
<NoWarn>108;169;612;618;1573;1591;1701;1702;1705</NoWarn>
12+
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
13+
<WarningsAsErrors />
14+
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<!-- Build symbol package (.snupkg) to distribute the PDB containing Source Link -->
19+
<IncludeSymbols>true</IncludeSymbols>
20+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
21+
</PropertyGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<PackageReference Update="Microsoft.Net.Compilers.Toolset" Version="4.2.0" />
29+
</ItemGroup>
30+
31+
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace FFmpeg.AutoGen.Abstractions;
5+
6+
public static partial class ffmpeg
7+
{
8+
public static readonly int EAGAIN;
9+
10+
public static readonly int ENOMEM = 12;
11+
12+
public static readonly int EINVAL = 22;
13+
14+
public static readonly int EPIPE = 32;
15+
16+
static ffmpeg()
17+
{
18+
#if NET
19+
20+
#elif NETSTANDARD2_0_OR_GREATER
21+
EAGAIN = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 35 : 11;
22+
#else
23+
EAGAIN = Environment.OSVersion.Platform == PlatformID.MacOSX ? 35 : 11;
24+
#endif
25+
26+
}
27+
28+
public static ulong UINT64_C<T>(T a)
29+
=> Convert.ToUInt64(a);
30+
31+
public static int AVERROR<T1>(T1 a)
32+
=> -Convert.ToInt32(a);
33+
34+
public static int MKTAG<T1, T2, T3, T4>(T1 a, T2 b, T3 c, T4 d)
35+
=> (int)(Convert.ToUInt32(a) | (Convert.ToUInt32(b) << 8) | (Convert.ToUInt32(c) << 16) |
36+
(Convert.ToUInt32(d) << 24));
37+
38+
public static int FFERRTAG<T1, T2, T3, T4>(T1 a, T2 b, T3 c, T4 d)
39+
=> -MKTAG(a, b, c, d);
40+
41+
public static int AV_VERSION_INT<T1, T2, T3>(T1 a, T2 b, T3 c) =>
42+
(Convert.ToInt32(a) << 16) | (Convert.ToInt32(b) << 8) | Convert.ToInt32(c);
43+
44+
public static string AV_VERSION_DOT<T1, T2, T3>(T1 a, T2 b, T3 c)
45+
=> $"{a}.{b}.{c}";
46+
47+
public static string AV_VERSION<T1, T2, T3>(T1 a, T2 b, T3 c)
48+
=> AV_VERSION_DOT(a, b, c);
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace FFmpeg.AutoGen.Abstractions;
2+
3+
public interface IFixedArray
4+
{
5+
int Length { get; }
6+
}
7+
8+
internal interface IFixedArray<T> : IFixedArray
9+
{
10+
T this[uint index] { get; set; }
11+
T[] ToArray();
12+
void UpdateFrom(T[] array);
13+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Text;
4+
5+
namespace FFmpeg.AutoGen.Abstractions;
6+
7+
public class UTF8Marshaler : ICustomMarshaler
8+
{
9+
private static readonly UTF8Marshaler Instance = new();
10+
11+
public virtual object MarshalNativeToManaged(IntPtr pNativeData) => FromNative(Encoding.UTF8, pNativeData);
12+
13+
public virtual IntPtr MarshalManagedToNative(object managedObj)
14+
{
15+
if (managedObj == null)
16+
return IntPtr.Zero;
17+
18+
if (managedObj is not string str)
19+
throw new MarshalDirectiveException($"{GetType().Name} must be used on a string.");
20+
21+
return FromManaged(Encoding.UTF8, str);
22+
}
23+
24+
public virtual void CleanUpNativeData(IntPtr pNativeData)
25+
{
26+
//Free anything allocated by MarshalManagedToNative
27+
//This is called after the native function call completes
28+
29+
if (pNativeData != IntPtr.Zero)
30+
Marshal.FreeHGlobal(pNativeData);
31+
}
32+
33+
public void CleanUpManagedData(object managedObj)
34+
{
35+
//Free anything allocated by MarshalNativeToManaged
36+
//This is called after the native function call completes
37+
}
38+
39+
public int GetNativeDataSize() => -1; // Not a value type
40+
41+
public static ICustomMarshaler GetInstance(string cookie) => Instance;
42+
43+
public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData) => FromNative(encoding, (byte*)pNativeData);
44+
45+
public static unsafe string FromNative(Encoding encoding, byte* pNativeData)
46+
{
47+
if (pNativeData == null)
48+
return null;
49+
50+
var start = pNativeData;
51+
var walk = start;
52+
53+
// Find the end of the string
54+
while (*walk != 0) walk++;
55+
56+
if (walk == start)
57+
return string.Empty;
58+
59+
return new string((sbyte*)pNativeData, 0, (int)(walk - start), encoding);
60+
}
61+
62+
public static unsafe IntPtr FromManaged(Encoding encoding, string value)
63+
{
64+
if (encoding == null || value == null)
65+
return IntPtr.Zero;
66+
67+
var length = encoding.GetByteCount(value);
68+
var buffer = (byte*)Marshal.AllocHGlobal(length + 1).ToPointer();
69+
70+
if (length > 0)
71+
{
72+
fixed (char* pValue = value)
73+
{
74+
encoding.GetBytes(pValue, value.Length, buffer, length);
75+
}
76+
}
77+
78+
buffer[length] = 0;
79+
80+
return new IntPtr(buffer);
81+
}
82+
}

0 commit comments

Comments
 (0)