Skip to content

Commit

Permalink
FFmpeg abstractions API (#227)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Ruslan-B and Ruslan Balanukhin committed Oct 12, 2022
1 parent 14c0520 commit 534a3eb
Show file tree
Hide file tree
Showing 124 changed files with 52,536 additions and 27,070 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@ UpgradeLog*.XML
*.stackdump

# Local
/FFmpeg.AutoGen.Example/frame.*.jpg
/FFmpeg.AutoGen.Example/frames

# JetBrains Rider
.idea/
*.sln.iml
9 changes: 5 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project>
<PropertyGroup>
<PackageId>$(AssemblyName)</PackageId>
<Version>5.1.1</Version>
<Version>5.1.1.1</Version>
<Authors>Ruslan Balanukhin</Authors>
<Company>Rational Core</Company>
<Company>Rationale One</Company>
<Product>FFmpeg.AutoGen</Product>
<Copyright>Copyright © Ruslan Balanukhin 2021 All rights reserved.</Copyright>
<Copyright>Copyright © Ruslan Balanukhin 2022 All rights reserved.</Copyright>
<PackageProjectUrl>https://github.com/Ruslan-B/FFmpeg.AutoGen</PackageProjectUrl>
<RepositoryType>Git</RepositoryType>
<AssemblyVersion>$(Version)</AssemblyVersion>
Expand All @@ -16,7 +16,8 @@
</PropertyGroup>

<PropertyGroup>
<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>
<!--<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>-->
<PublicSign>true</PublicSign>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)FFmpeg.AutoGen.snk</AssemblyOriginatorKeyFile>
Expand Down
24 changes: 24 additions & 0 deletions FFmpeg.AutoGen.Abstractions/ConstCharPtrMarshaler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Runtime.InteropServices;

namespace FFmpeg.AutoGen.Abstractions;

public class ConstCharPtrMarshaler : ICustomMarshaler
{
private static readonly ConstCharPtrMarshaler Instance = new();
public object MarshalNativeToManaged(IntPtr pNativeData) => Marshal.PtrToStringAnsi(pNativeData);

public IntPtr MarshalManagedToNative(object managedObj) => IntPtr.Zero;

public void CleanUpNativeData(IntPtr pNativeData)
{
}

public void CleanUpManagedData(object managedObj)
{
}

public int GetNativeDataSize() => IntPtr.Size;

public static ICustomMarshaler GetInstance(string cookie) => Instance;
}
31 changes: 31 additions & 0 deletions FFmpeg.AutoGen.Abstractions/FFmpeg.AutoGen.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net45</TargetFrameworks>
<Description>FFmpeg auto generated unsafe bindings for C#/.NET and Mono. Abstractions todo</Description>
<GeneratePackageOnBuild Condition=" $(Configuration) == 'Release' ">true</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<NoWarn>108;169;612;618;1573;1591;1701;1702;1705</NoWarn>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsAsErrors />
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup>
<!-- Build symbol package (.snupkg) to distribute the PDB containing Source Link -->
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.Net.Compilers.Toolset" Version="4.2.0" />
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions FFmpeg.AutoGen.Abstractions/FFmpeg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Runtime.InteropServices;

namespace FFmpeg.AutoGen.Abstractions;

public static partial class ffmpeg
{
public static readonly int EAGAIN;

public static readonly int ENOMEM = 12;

public static readonly int EINVAL = 22;

public static readonly int EPIPE = 32;

static ffmpeg()
{
#if NET

#elif NETSTANDARD2_0_OR_GREATER
EAGAIN = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 35 : 11;
#else
EAGAIN = Environment.OSVersion.Platform == PlatformID.MacOSX ? 35 : 11;
#endif

}

public static ulong UINT64_C<T>(T a)
=> Convert.ToUInt64(a);

public static int AVERROR<T1>(T1 a)
=> -Convert.ToInt32(a);

public static int MKTAG<T1, T2, T3, T4>(T1 a, T2 b, T3 c, T4 d)
=> (int)(Convert.ToUInt32(a) | (Convert.ToUInt32(b) << 8) | (Convert.ToUInt32(c) << 16) |
(Convert.ToUInt32(d) << 24));

public static int FFERRTAG<T1, T2, T3, T4>(T1 a, T2 b, T3 c, T4 d)
=> -MKTAG(a, b, c, d);

public static int AV_VERSION_INT<T1, T2, T3>(T1 a, T2 b, T3 c) =>
(Convert.ToInt32(a) << 16) | (Convert.ToInt32(b) << 8) | Convert.ToInt32(c);

public static string AV_VERSION_DOT<T1, T2, T3>(T1 a, T2 b, T3 c)
=> $"{a}.{b}.{c}";

public static string AV_VERSION<T1, T2, T3>(T1 a, T2 b, T3 c)
=> AV_VERSION_DOT(a, b, c);
}
13 changes: 13 additions & 0 deletions FFmpeg.AutoGen.Abstractions/IFixedArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace FFmpeg.AutoGen.Abstractions;

public interface IFixedArray
{
int Length { get; }
}

internal interface IFixedArray<T> : IFixedArray
{
T this[uint index] { get; set; }
T[] ToArray();
void UpdateFrom(T[] array);
}
82 changes: 82 additions & 0 deletions FFmpeg.AutoGen.Abstractions/UTF8Marshaler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace FFmpeg.AutoGen.Abstractions;

public class UTF8Marshaler : ICustomMarshaler
{
private static readonly UTF8Marshaler Instance = new();

public virtual object MarshalNativeToManaged(IntPtr pNativeData) => FromNative(Encoding.UTF8, pNativeData);

public virtual IntPtr MarshalManagedToNative(object managedObj)
{
if (managedObj == null)
return IntPtr.Zero;

if (managedObj is not string str)
throw new MarshalDirectiveException($"{GetType().Name} must be used on a string.");

return FromManaged(Encoding.UTF8, str);
}

public virtual void CleanUpNativeData(IntPtr pNativeData)
{
//Free anything allocated by MarshalManagedToNative
//This is called after the native function call completes

if (pNativeData != IntPtr.Zero)
Marshal.FreeHGlobal(pNativeData);
}

public void CleanUpManagedData(object managedObj)
{
//Free anything allocated by MarshalNativeToManaged
//This is called after the native function call completes
}

public int GetNativeDataSize() => -1; // Not a value type

public static ICustomMarshaler GetInstance(string cookie) => Instance;

public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData) => FromNative(encoding, (byte*)pNativeData);

public static unsafe string FromNative(Encoding encoding, byte* pNativeData)
{
if (pNativeData == null)
return null;

var start = pNativeData;
var walk = start;

// Find the end of the string
while (*walk != 0) walk++;

if (walk == start)
return string.Empty;

return new string((sbyte*)pNativeData, 0, (int)(walk - start), encoding);
}

public static unsafe IntPtr FromManaged(Encoding encoding, string value)
{
if (encoding == null || value == null)
return IntPtr.Zero;

var length = encoding.GetByteCount(value);
var buffer = (byte*)Marshal.AllocHGlobal(length + 1).ToPointer();

if (length > 0)
{
fixed (char* pValue = value)
{
encoding.GetBytes(pValue, value.Length, buffer, length);
}
}

buffer[length] = 0;

return new IntPtr(buffer);
}
}
Loading

0 comments on commit 534a3eb

Please sign in to comment.