Closed SuRGeoNix closed 1 month ago
Here an update for easy copy/paste in FFmpegOptions.cs
public void Set<T>(string name, T[] value, AV_OPT_SEARCH searchFlags = AV_OPT_SEARCH.Children) where T : Enum
{
Span<int> arr = stackalloc int[value.Length];
for (int i = 0; i < arr.Length; i++)
arr[i] = Convert.ToInt32(value[i]);
fixed(int* ptr = arr)
av_opt_set_bin(_obj, name, (byte*)ptr, sizeof(int) * arr.Length, (int)searchFlags).ThrowIfError();
}
public void Set<T>(string name, List<T> value, AV_OPT_SEARCH searchFlags = AV_OPT_SEARCH.Children) where T : Enum
{
Span<int> arr = stackalloc int[value.Count];
for (int i = 0; i < arr.Length; i++)
arr[i] = Convert.ToInt32(value[i]);
fixed(int* ptr = arr)
av_opt_set_bin(_obj, name, (byte*)ptr, sizeof(int) * arr.Length, (int)searchFlags).ThrowIfError();
}
public void Set(string name, int[] value, AV_OPT_SEARCH searchFlags = AV_OPT_SEARCH.Children)
{
Span<int> arr = stackalloc int[value.Length];
for (int i = 0; i < arr.Length; i++)
arr[i] = value[i];
fixed(int* ptr = arr)
av_opt_set_bin(_obj, name, (byte*)ptr, sizeof(int) * arr.Length, (int)searchFlags).ThrowIfError();
}
public void Set(string name, List<int> value, AV_OPT_SEARCH searchFlags = AV_OPT_SEARCH.Children)
{
Span<int> arr = stackalloc int[value.Count];
for (int i = 0; i < arr.Length; i++)
arr[i] = value[i];
fixed(int* ptr = arr)
av_opt_set_bin(_obj, name, (byte*)ptr, sizeof(int) * arr.Length, (int)searchFlags).ThrowIfError();
}
public void Set(string name, ulong[] value, AV_OPT_SEARCH searchFlags = AV_OPT_SEARCH.Children)
{
Span<ulong> arr = stackalloc ulong[value.Length];
for (int i = 0; i < arr.Length; i++)
arr[i] = value[i];
fixed(ulong* ptr = arr)
av_opt_set_bin(_obj, name, (byte*)ptr, sizeof(ulong) * arr.Length, (int)searchFlags).ThrowIfError();
}
That was another issue, this is happening when passing AVPixelFormat.None (which is -1 = FF FF FF FF) not sure why (probably FFmpeg assert down to set pixel formats in options) but that's not because what I thought initially. The marshalling works fine, even if the C# array has an overhead the fixed pointer will point directly to the data and not at the start of the actual array (pointing to overhead).
Invalid marshalling from C# array to C array which can be reproduced by using multiple entries (not just one, i think for one would work fine) :-
Currently using the following helpers to resolve this (that might help) :-