デコーダを LuaScript.Media プロジェクトへ分離

このコミットが含まれているのは:
routersys committed 2026-07-03 17:25:43 +09:00
1 親 3cd67212a1
コミット 1b26df959f
7ファイル変更
+53

検索条件に一致するファイルはありません

-97
ファイルの表示
@@ -1,97 +0,0 @@
using System;
using System.IO;
using Vortice.Mathematics;
using Vortice.WIC;
namespace LuaScript
{
internal sealed class ImageDecoder : IDisposable
{
private const int MaxDimension = 8192;
private IWICImagingFactory? _factory;
public unsafe byte[] Decode(string path, out int width, out int height)
{
width = 1;
height = 1;
if (string.IsNullOrEmpty(path) || !IsSupportedImage(path))
return new byte[4];
try
{
_factory ??= new IWICImagingFactory();
using var stream = _factory.CreateStream(path, FileAccess.Read);
using var decoder = _factory.CreateDecoderFromStream(stream, DecodeOptions.CacheOnDemand);
using var frame = decoder.GetFrame(0);
using var converter = _factory.CreateFormatConverter();
converter.Initialize(
frame,
Vortice.WIC.PixelFormat.Format32bppPBGRA,
BitmapDitherType.None,
null,
0d,
BitmapPaletteType.Custom);
converter.GetSize(out int w, out int h);
if (w <= 0 || h <= 0 || w > MaxDimension || h > MaxDimension)
return new byte[4];
var buffer = new byte[w * h * 4];
fixed (byte* p = buffer)
converter.CopyPixels(new RectI(0, 0, w, h), w * 4, buffer.Length, (nint)p);
width = w;
height = h;
return buffer;
}
catch
{
width = 1;
height = 1;
return new byte[4];
}
}
private static bool IsSupportedImage(string path)
{
try
{
if (!File.Exists(path))
return false;
Span<byte> head = stackalloc byte[12];
using var stream = File.OpenRead(path);
int read = stream.Read(head);
if (read < 4)
return false;
if (head[0] == 0x89 && head[1] == 0x50 && head[2] == 0x4E && head[3] == 0x47)
return true;
if (head[0] == 0xFF && head[1] == 0xD8 && head[2] == 0xFF)
return true;
if (head[0] == 0x47 && head[1] == 0x49 && head[2] == 0x46 && head[3] == 0x38)
return true;
if (head[0] == 0x42 && head[1] == 0x4D)
return true;
if ((head[0] == 0x49 && head[1] == 0x49 && head[2] == 0x2A && head[3] == 0x00) ||
(head[0] == 0x4D && head[1] == 0x4D && head[2] == 0x00 && head[3] == 0x2A))
return true;
if (read >= 12 &&
head[0] == 0x52 && head[1] == 0x49 && head[2] == 0x46 && head[3] == 0x46 &&
head[8] == 0x57 && head[9] == 0x45 && head[10] == 0x42 && head[11] == 0x50)
return true;
return false;
}
catch
{
return false;
}
}
public void Dispose()
{
_factory?.Dispose();
_factory = null;
}
}
}
+1
ファイルの表示
@@ -97,6 +97,7 @@
<ItemGroup>
<ProjectReference Include="..\LuaScript.Kernel\LuaScript.Kernel.csproj" />
<ProjectReference Include="..\LuaScript.Compat\LuaScript.Compat.csproj" />
<ProjectReference Include="..\LuaScript.Media\LuaScript.Media.csproj" />
<ProjectReference Include="..\LuaScript.Runtime\LuaScript.Runtime.csproj" />
</ItemGroup>
-213
ファイルの表示
@@ -1,213 +0,0 @@
using System;
using System.IO;
using SharpGen.Runtime.Win32;
using Vortice.MediaFoundation;
namespace LuaScript
{
internal sealed class MovieDecoder : IDisposable
{
private const int MaxDimension = 8192;
private const int FirstVideoStream = -4;
private const int AllStreams = -2;
private const int EndOfStreamFlag = 0x2;
private bool _mfStarted;
private string? _path;
private IMFSourceReader? _reader;
private int _width;
private int _height;
private int _stride;
public unsafe byte[] Decode(string path, double timeSeconds, out int width, out int height)
{
width = 1;
height = 1;
if (string.IsNullOrEmpty(path) || !IsSupportedVideo(path))
return new byte[4];
try
{
EnsureReader(path);
if (_reader is null || _width <= 0 || _height <= 0)
return new byte[4];
SetPosition(_reader, (long)(Math.Max(0d, timeSeconds) * 10_000_000d));
using var sample = ReadVideoSample(_reader);
if (sample is null)
return new byte[4];
using var buffer = sample.ConvertToContiguousBuffer();
buffer.Lock(out nint data, out _, out _);
try
{
int w = _width;
int h = _height;
int absStride = Math.Abs(_stride);
bool bottomUp = _stride < 0;
var result = new byte[w * h * 4];
byte* src = (byte*)data;
fixed (byte* dst = result)
{
for (int y = 0; y < h; y++)
{
int sourceRow = bottomUp ? h - 1 - y : y;
byte* s = src + sourceRow * absStride;
byte* d = dst + y * w * 4;
for (int x = 0; x < w; x++)
{
d[0] = s[0];
d[1] = s[1];
d[2] = s[2];
d[3] = 255;
s += 4;
d += 4;
}
}
}
width = w;
height = h;
return result;
}
finally
{
buffer.Unlock();
}
}
catch
{
width = 1;
height = 1;
return new byte[4];
}
}
private void EnsureReader(string path)
{
if (_reader is not null && string.Equals(_path, path, StringComparison.Ordinal))
return;
DisposeReader();
if (!_mfStarted)
{
MediaFactory.MFStartup(false);
_mfStarted = true;
}
using var attributes = MediaFactory.MFCreateAttributes(1);
attributes.Set(SourceReaderAttributeKeys.EnableAdvancedVideoProcessing, 1);
var reader = MediaFactory.MFCreateSourceReaderFromURL(path, attributes);
reader.SetStreamSelection(AllStreams, false);
reader.SetStreamSelection(FirstVideoStream, true);
using (var mediaType = MediaFactory.MFCreateMediaType())
{
mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);
mediaType.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.Rgb32);
reader.SetCurrentMediaType(FirstVideoStream, mediaType);
}
using var current = reader.GetCurrentMediaType(FirstVideoStream);
var mediaAttributes = (IMFAttributes)current;
ulong frameSize = Convert.ToUInt64(mediaAttributes.Get(MediaTypeAttributeKeys.FrameSize));
int w = (int)(frameSize >> 32);
int h = (int)(frameSize & 0xFFFFFFFF);
int stride;
try { stride = Convert.ToInt32(mediaAttributes.Get(MediaTypeAttributeKeys.DefaultStride)); }
catch { stride = w * 4; }
if (stride == 0)
stride = w * 4;
if (w <= 0 || h <= 0 || w > MaxDimension || h > MaxDimension)
{
reader.Dispose();
return;
}
_reader = reader;
_path = path;
_width = w;
_height = h;
_stride = stride;
}
private static void SetPosition(IMFSourceReader reader, long ticks)
{
var variant = new Variant
{
Value = ticks,
ElementType = (VariantElementType)20,
};
reader.SetCurrentPosition(Guid.Empty, variant);
}
private static IMFSample? ReadVideoSample(IMFSourceReader reader)
{
for (int guard = 0; guard < 256; guard++)
{
reader.ReadSample(FirstVideoStream, 0, out _, out int flags, out _, out IMFSample? sample);
if (sample is not null)
return sample;
if ((flags & EndOfStreamFlag) != 0)
return null;
}
return null;
}
private static bool IsSupportedVideo(string path)
{
try
{
if (!File.Exists(path))
return false;
Span<byte> head = stackalloc byte[16];
using var stream = File.OpenRead(path);
int read = stream.Read(head);
if (read < 12)
return false;
if (head[4] == 0x66 && head[5] == 0x74 && head[6] == 0x79 && head[7] == 0x70)
return true;
if (head[0] == 0x1A && head[1] == 0x45 && head[2] == 0xDF && head[3] == 0xA3)
return true;
if (head[0] == 0x52 && head[1] == 0x49 && head[2] == 0x46 && head[3] == 0x46 &&
head[8] == 0x41 && head[9] == 0x56 && head[10] == 0x49 && head[11] == 0x20)
return true;
if (head[0] == 0x46 && head[1] == 0x4C && head[2] == 0x56)
return true;
if (head[0] == 0x30 && head[1] == 0x26 && head[2] == 0xB2 && head[3] == 0x75)
return true;
return false;
}
catch
{
return false;
}
}
private void DisposeReader()
{
_reader?.Dispose();
_reader = null;
_path = null;
_width = 0;
_height = 0;
_stride = 0;
}
public void Dispose()
{
DisposeReader();
if (_mfStarted)
{
try { MediaFactory.MFShutdown(); }
catch { }
_mfStarted = false;
}
}
}
}
-97
ファイルの表示
@@ -1,97 +0,0 @@
using System;
using System.Numerics;
using Vortice.Direct2D1;
using Vortice.DirectWrite;
using Vortice.Mathematics;
using Vortice.WIC;
namespace LuaScript
{
internal sealed class TextRenderer : IDisposable
{
private const int MaxDimension = 8192;
private const string FallbackFamily = "Yu Gothic UI";
private ID2D1Factory? _d2dFactory;
private IWICImagingFactory? _wicFactory;
private IDWriteFactory? _dwriteFactory;
private void EnsureFactories()
{
_d2dFactory ??= D2D1.D2D1CreateFactory<ID2D1Factory>(
Vortice.Direct2D1.FactoryType.MultiThreaded, DebugLevel.None);
_wicFactory ??= new IWICImagingFactory();
_dwriteFactory ??= DWrite.DWriteCreateFactory<IDWriteFactory>(
Vortice.DirectWrite.FactoryType.Shared);
}
public unsafe byte[] Render(
string text, string fontFamily, double fontSize,
bool bold, bool italic, int colorRgb,
out int width, out int height)
{
width = 1;
height = 1;
if (string.IsNullOrEmpty(text))
return new byte[4];
EnsureFactories();
float size = (float)Math.Clamp(fontSize, 1d, 4096d);
using var format = _dwriteFactory!.CreateTextFormat(
string.IsNullOrWhiteSpace(fontFamily) ? FallbackFamily : fontFamily,
null,
bold ? FontWeight.Bold : FontWeight.Normal,
italic ? FontStyle.Italic : FontStyle.Normal,
FontStretch.Normal,
size,
string.Empty);
using var layout = _dwriteFactory.CreateTextLayout(text, format, MaxDimension, MaxDimension);
var metrics = layout.Metrics;
int w = Math.Clamp((int)Math.Ceiling(metrics.WidthIncludingTrailingWhitespace), 1, MaxDimension);
int h = Math.Clamp((int)Math.Ceiling(metrics.Height), 1, MaxDimension);
using var wic = _wicFactory!.CreateBitmap(
w, h, Vortice.WIC.PixelFormat.Format32bppPBGRA, BitmapCreateCacheOption.CacheOnLoad);
var properties = new RenderTargetProperties(
RenderTargetType.Default,
new Vortice.DCommon.PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied),
96f, 96f,
RenderTargetUsage.None,
FeatureLevel.Default);
float r = ((colorRgb >> 16) & 0xFF) / 255f;
float g = ((colorRgb >> 8) & 0xFF) / 255f;
float b = (colorRgb & 0xFF) / 255f;
using (var target = _d2dFactory!.CreateWicBitmapRenderTarget(wic, properties))
{
target.BeginDraw();
target.Clear(new Color4(0f, 0f, 0f, 0f));
using (var brush = target.CreateSolidColorBrush(new Color4(r, g, b, 1f)))
target.DrawTextLayout(new Vector2(-metrics.Left, -metrics.Top), layout, brush);
target.EndDraw();
}
var buffer = new byte[w * h * 4];
fixed (byte* p = buffer)
wic.CopyPixels(new RectI(0, 0, w, h), w * 4, buffer.Length, (nint)p);
width = w;
height = h;
return buffer;
}
public void Dispose()
{
_dwriteFactory?.Dispose();
_wicFactory?.Dispose();
_d2dFactory?.Dispose();
_dwriteFactory = null;
_wicFactory = null;
_d2dFactory = null;
}
}
}