Skip to content

Commit

Permalink
fix the hw acceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
zgabi committed Jun 10, 2024
1 parent fe57642 commit 6266a1c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
12 changes: 7 additions & 5 deletions Unosquare.FFME.Windows.Sample/MainWindow.MediaEvents.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Unosquare.FFME.Windows.Sample
using System.Collections.Generic;

namespace Unosquare.FFME.Windows.Sample
{
using ClosedCaptions;
using FFmpeg.AutoGen;
Expand Down Expand Up @@ -286,16 +288,16 @@ private async void OnMediaOpening(object sender, MediaOpeningEventArgs e)
// Hardware device selection
if (videoStream.FPS <= 30)
{
var devices = new List<HardwareDeviceInfo>(deviceCandidates.Length);
foreach (var deviceType in deviceCandidates)
{
var accelerator = videoStream.HardwareDevices.FirstOrDefault(d => d.DeviceType == deviceType);
if (accelerator == null) continue;

if (Debugger.IsAttached)
e.Options.VideoHardwareDevice = accelerator;

break;
devices.Add(accelerator);
}

e.Options.VideoHardwareDevices = devices.ToArray();
}

// Start building a video filter
Expand Down
2 changes: 1 addition & 1 deletion Unosquare.FFME/Common/MediaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal MediaOptions()
/// Use Stream's HardwareDevices property to get a list of
/// compatible hardware accelerators.
/// </summary>
public HardwareDeviceInfo VideoHardwareDevice { get; set; }
public HardwareDeviceInfo[] VideoHardwareDevices { get; set; }

/// <summary>
/// Prevent reading from audio stream components.
Expand Down
2 changes: 1 addition & 1 deletion Unosquare.FFME/Container/MediaComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected MediaComponent(MediaContainer container, int streamIndex)
codecOptions.SetCopyOpaque();

// Enable Hardware acceleration if requested
(this as VideoComponent)?.AttachHardwareDevice(container.MediaOptions.VideoHardwareDevice);
(this as VideoComponent)?.AttachHardwareDevice(container.MediaOptions.VideoHardwareDevices);

// Open the CodecContext. This requires exclusive FFmpeg access
lock (CodecLock)
Expand Down
48 changes: 26 additions & 22 deletions Unosquare.FFME/Container/VideoComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,37 +157,41 @@ internal VideoComponent(MediaContainer container, int streamIndex)
/// <summary>
/// Attaches a hardware accelerator to this video component.
/// </summary>
/// <param name="selectedConfig">The selected configuration.</param>
/// <param name="selectedConfigs">The selected configurations.</param>
/// <returns>
/// Whether or not the hardware accelerator was attached.
/// </returns>
public bool AttachHardwareDevice(HardwareDeviceInfo selectedConfig)
public bool AttachHardwareDevice(HardwareDeviceInfo[] selectedConfigs)
{
// Check for no device selection
if (selectedConfig == null)
if (selectedConfigs == null || selectedConfigs.Length == 0)
return false;

try
{
var accelerator = new HardwareAccelerator(this, selectedConfig);

AVBufferRef* devContextRef = null;
var initResultCode = ffmpeg.av_hwdevice_ctx_create(&devContextRef, accelerator.DeviceType, null, null, 0);
if (initResultCode < 0)
throw new MediaContainerException($"Unable to initialize hardware context for device {accelerator.Name}");

HardwareDeviceContext = devContextRef;
HardwareAccelerator = accelerator;
CodecContext->hw_device_ctx = ffmpeg.av_buffer_ref(HardwareDeviceContext);
CodecContext->get_format = accelerator.GetFormatCallback;

return true;
}
catch (Exception ex)
foreach (var selectedConfig in selectedConfigs)
{
this.LogError(Aspects.Component, "Could not attach hardware decoder.", ex);
return false;
try
{
AVBufferRef* devContextRef = null;
var initResultCode = ffmpeg.av_hwdevice_ctx_create(&devContextRef, selectedConfig.DeviceType, null, null, 0);
if (initResultCode < 0)
continue;

var accelerator = new HardwareAccelerator(this, selectedConfig);
HardwareDeviceContext = devContextRef;
HardwareAccelerator = accelerator;
CodecContext->hw_device_ctx = ffmpeg.av_buffer_ref(HardwareDeviceContext);
CodecContext->get_format = accelerator.GetFormatCallback;

return true;
}
catch (Exception ex)
{
this.LogError(Aspects.Component, "Could not attach hardware decoder.", ex);
}
}

this.LogError(Aspects.Component, "Could not attach any hardware decoder.");
return false;
}

/// <summary>
Expand Down

0 comments on commit 6266a1c

Please sign in to comment.