Я работаю над приложением UWP в Unity с SharpDX и ffmpeginteropX. Моя цель — иметь возможность рисовать кадры из ffmpeg на текстуре в Unity. В настоящее время я могу рисовать текстуру Unity и захватывать кадры из ffmpeg, но не могу понять, как их объединить. Раньше я вручную копировал кадры через Unity, но копирование с графического процессора и процессора происходило безумно медленно.
Вот как я настроил SharpDX и мою текстуру:
// Class member variables
public UnityEngine.UI.RawImage target;
SharpDX.Direct3D11.Texture2D m_DstTexture;
SharpDX.Direct3D11.Device device;
SharpDX.Direct3D11.DeviceContext deviceContext;
// Dummy texture hack to get device and context from Unity
UnityEngine.Texture2D targetX = new UnityEngine.Texture2D(512, 512, TextureFormat.BGRA32, false);
IntPtr texturePtr = targetX.GetNativeTexturePtr();
SharpDX.Direct3D11.Texture2D dstTextureX = new SharpDX.Direct3D11.Texture2D(texturePtr);
// Create DirectX device and context from texture
device = dstTextureX.Device;
deviceContext = device.ImmediateContext;
// Create shared texture for FFmpeg and Unity to access
SharpDX.Direct3D11.Texture2DDescription sharedTexture2DDescription = dstTextureX.Description;
sharedTexture2DDescription.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.Shared;
m_DstTexture = new SharpDX.Direct3D11.Texture2D(device, sharedTexture2DDescription);
// Get Shader Resource View to shared texture
var d3d11ShaderResourceView = new ShaderResourceView(device, m_DstTexture);
// Assign the shader resource view to the target RawImage
target.texture = UnityEngine.Texture2D.CreateExternalTexture(512, 512, TextureFormat.BGRA32, false, false, texturePtr);
Настройка FFmpeg:
// Member variables
private MediaPlayer mediaPlayer;
private MediaPlaybackItem mediaPlaybackItem;
private FFmpegMediaSource ffmpegMediaSource;
private SoftwareBitmap frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Rgba8, 512, 512, BitmapAlphaMode.Premultiplied );
private UnityEngine.Texture2D tex;
private CanvasDevice canvasDevice;
private IDirect3DSurface surface;
private async void InitializeMediaPlayer()
{
try
{
FFmpegInteropLogging.SetDefaultLogProvider();
MediaSourceConfig configuration = new MediaSourceConfig()
{
MaxVideoThreads = 8,
SkipErrors = uint.MaxValue,
ReadAheadBufferDuration = TimeSpan.Zero,
FastSeek = true,
VideoDecoderMode = VideoDecoderMode.ForceFFmpegSoftwareDecoder
};
Debug.Log("FFmpegInteropX configuration set up successfully.");
// Sample stream source
string uri = "https://test-videos.co.uk/vids/sintel/m ... 0s_1MB.mp4";
//string uri = "udp://@192.168.10.1:11111";
// Create FFmpegMediaSource from sample stream
Debug.Log($"Attempting to create media source from URI: {uri}");
ffmpegMediaSource = await FFmpegMediaSource.CreateFromUriAsync(uri, configuration);
// Create MediaPlaybackItem from FFmpegMediaSource
mediaPlaybackItem = ffmpegMediaSource.CreateMediaPlaybackItem();
mediaPlayer = new MediaPlayer
{
Source = mediaPlaybackItem,
IsVideoFrameServerEnabled = true
};
mediaPlayer.VideoFrameAvailable += MediaPlayer_VideoFrameAvailable;
mediaPlayer.Play();
}
catch (Exception ex)
{
Debug.LogError($"Exception while setting up FFmpegInteropX: {ex.Message}");
}
}
Событие для обработки кадров, здесь должно произойти преобразование
private async void MediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
{
try
{
// Here frame should be copied to target, however types are not the same
// MediaPlayer returns frames as IDirect3DSurface
sender.CopyFrameToVideoSurface(surface);
}
catch (Exception ex)
{
Debug.LogError($"Exception during VideoFrameAvailable: {ex.Message}");
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... -texture2d
Поверхность Unity SharpDX IDirect3DSurface для SharpDX.Direct3D11.Texture2D ⇐ C#
Место общения программистов C#
1722880014
Anonymous
Я работаю над приложением UWP в Unity с SharpDX и ffmpeginteropX. Моя цель — иметь возможность рисовать кадры из ffmpeg на текстуре в Unity. В настоящее время я могу рисовать текстуру Unity и захватывать кадры из ffmpeg, но не могу понять, как их объединить. Раньше я вручную копировал кадры через Unity, но копирование с графического процессора и процессора происходило безумно медленно.
Вот как я настроил SharpDX и мою текстуру:
// Class member variables
public UnityEngine.UI.RawImage target;
SharpDX.Direct3D11.Texture2D m_DstTexture;
SharpDX.Direct3D11.Device device;
SharpDX.Direct3D11.DeviceContext deviceContext;
// Dummy texture hack to get device and context from Unity
UnityEngine.Texture2D targetX = new UnityEngine.Texture2D(512, 512, TextureFormat.BGRA32, false);
IntPtr texturePtr = targetX.GetNativeTexturePtr();
SharpDX.Direct3D11.Texture2D dstTextureX = new SharpDX.Direct3D11.Texture2D(texturePtr);
// Create DirectX device and context from texture
device = dstTextureX.Device;
deviceContext = device.ImmediateContext;
// Create shared texture for FFmpeg and Unity to access
SharpDX.Direct3D11.Texture2DDescription sharedTexture2DDescription = dstTextureX.Description;
sharedTexture2DDescription.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.Shared;
m_DstTexture = new SharpDX.Direct3D11.Texture2D(device, sharedTexture2DDescription);
// Get Shader Resource View to shared texture
var d3d11ShaderResourceView = new ShaderResourceView(device, m_DstTexture);
// Assign the shader resource view to the target RawImage
target.texture = UnityEngine.Texture2D.CreateExternalTexture(512, 512, TextureFormat.BGRA32, false, false, texturePtr);
Настройка FFmpeg:
// Member variables
private MediaPlayer mediaPlayer;
private MediaPlaybackItem mediaPlaybackItem;
private FFmpegMediaSource ffmpegMediaSource;
private SoftwareBitmap frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Rgba8, 512, 512, BitmapAlphaMode.Premultiplied );
private UnityEngine.Texture2D tex;
private CanvasDevice canvasDevice;
private IDirect3DSurface surface;
private async void InitializeMediaPlayer()
{
try
{
FFmpegInteropLogging.SetDefaultLogProvider();
MediaSourceConfig configuration = new MediaSourceConfig()
{
MaxVideoThreads = 8,
SkipErrors = uint.MaxValue,
ReadAheadBufferDuration = TimeSpan.Zero,
FastSeek = true,
VideoDecoderMode = VideoDecoderMode.ForceFFmpegSoftwareDecoder
};
Debug.Log("FFmpegInteropX configuration set up successfully.");
// Sample stream source
string uri = "https://test-videos.co.uk/vids/sintel/mp4/h264/720/Sintel_720_10s_1MB.mp4";
//string uri = "udp://@192.168.10.1:11111";
// Create FFmpegMediaSource from sample stream
Debug.Log($"Attempting to create media source from URI: {uri}");
ffmpegMediaSource = await FFmpegMediaSource.CreateFromUriAsync(uri, configuration);
// Create MediaPlaybackItem from FFmpegMediaSource
mediaPlaybackItem = ffmpegMediaSource.CreateMediaPlaybackItem();
mediaPlayer = new MediaPlayer
{
Source = mediaPlaybackItem,
IsVideoFrameServerEnabled = true
};
mediaPlayer.VideoFrameAvailable += MediaPlayer_VideoFrameAvailable;
mediaPlayer.Play();
}
catch (Exception ex)
{
Debug.LogError($"Exception while setting up FFmpegInteropX: {ex.Message}");
}
}
Событие для обработки кадров, здесь должно произойти преобразование
private async void MediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
{
try
{
// Here frame should be copied to target, however types are not the same
// MediaPlayer returns frames as IDirect3DSurface
sender.CopyFrameToVideoSurface(surface);
}
catch (Exception ex)
{
Debug.LogError($"Exception during VideoFrameAvailable: {ex.Message}");
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/78835789/unity-sharpdx-idirect3dsurface-surface-to-sharpdx-direct3d11-texture2d[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия