| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | #pragma warning disable CS1591 |
| | 4 | | // We need lowercase normalized string for ffmpeg |
| | 5 | | #pragma warning disable CA1308 |
| | 6 | |
|
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Globalization; |
| | 10 | | using System.IO; |
| | 11 | | using System.Linq; |
| | 12 | | using System.Runtime.InteropServices; |
| | 13 | | using System.Text; |
| | 14 | | using System.Text.RegularExpressions; |
| | 15 | | using System.Threading; |
| | 16 | | using Jellyfin.Data; |
| | 17 | | using Jellyfin.Data.Enums; |
| | 18 | | using Jellyfin.Database.Implementations.Enums; |
| | 19 | | using Jellyfin.Extensions; |
| | 20 | | using MediaBrowser.Common.Configuration; |
| | 21 | | using MediaBrowser.Controller.Extensions; |
| | 22 | | using MediaBrowser.Controller.IO; |
| | 23 | | using MediaBrowser.Model.Configuration; |
| | 24 | | using MediaBrowser.Model.Dlna; |
| | 25 | | using MediaBrowser.Model.Dto; |
| | 26 | | using MediaBrowser.Model.Entities; |
| | 27 | | using MediaBrowser.Model.MediaInfo; |
| | 28 | | using Microsoft.Extensions.Configuration; |
| | 29 | | using IConfigurationManager = MediaBrowser.Common.Configuration.IConfigurationManager; |
| | 30 | |
|
| | 31 | | namespace MediaBrowser.Controller.MediaEncoding |
| | 32 | | { |
| | 33 | | public partial class EncodingHelper |
| | 34 | | { |
| | 35 | | /// <summary> |
| | 36 | | /// The codec validation regex. |
| | 37 | | /// This regular expression matches strings that consist of alphanumeric characters, hyphens, |
| | 38 | | /// periods, underscores, commas, and vertical bars, with a length between 0 and 40 characters. |
| | 39 | | /// This should matches all common valid codecs. |
| | 40 | | /// </summary> |
| | 41 | | public const string ContainerValidationRegex = @"^[a-zA-Z0-9\-\._,|]{0,40}$"; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// The level validation regex. |
| | 45 | | /// This regular expression matches strings representing a double. |
| | 46 | | /// </summary> |
| | 47 | | public const string LevelValidationRegex = @"-?[0-9]+(?:\.[0-9]+)?"; |
| | 48 | |
|
| | 49 | | private const string _defaultMjpegEncoder = "mjpeg"; |
| | 50 | |
|
| | 51 | | private const string QsvAlias = "qs"; |
| | 52 | | private const string VaapiAlias = "va"; |
| | 53 | | private const string D3d11vaAlias = "dx11"; |
| | 54 | | private const string VideotoolboxAlias = "vt"; |
| | 55 | | private const string RkmppAlias = "rk"; |
| | 56 | | private const string OpenclAlias = "ocl"; |
| | 57 | | private const string CudaAlias = "cu"; |
| | 58 | | private const string DrmAlias = "dr"; |
| | 59 | | private const string VulkanAlias = "vk"; |
| | 60 | | private readonly IApplicationPaths _appPaths; |
| | 61 | | private readonly IMediaEncoder _mediaEncoder; |
| | 62 | | private readonly ISubtitleEncoder _subtitleEncoder; |
| | 63 | | private readonly IConfiguration _config; |
| | 64 | | private readonly IConfigurationManager _configurationManager; |
| | 65 | | private readonly IPathManager _pathManager; |
| | 66 | |
|
| | 67 | | // i915 hang was fixed by linux 6.2 (3f882f2) |
| 21 | 68 | | private readonly Version _minKerneli915Hang = new Version(5, 18); |
| 21 | 69 | | private readonly Version _maxKerneli915Hang = new Version(6, 1, 3); |
| 21 | 70 | | private readonly Version _minFixedKernel60i915Hang = new Version(6, 0, 18); |
| 21 | 71 | | private readonly Version _minKernelVersionAmdVkFmtModifier = new Version(5, 15); |
| | 72 | |
|
| 21 | 73 | | private readonly Version _minFFmpegImplicitHwaccel = new Version(6, 0); |
| 21 | 74 | | private readonly Version _minFFmpegHwaUnsafeOutput = new Version(6, 0); |
| 21 | 75 | | private readonly Version _minFFmpegOclCuTonemapMode = new Version(5, 1, 3); |
| 21 | 76 | | private readonly Version _minFFmpegSvtAv1Params = new Version(5, 1); |
| 21 | 77 | | private readonly Version _minFFmpegVaapiH26xEncA53CcSei = new Version(6, 0); |
| 21 | 78 | | private readonly Version _minFFmpegReadrateOption = new Version(5, 0); |
| 21 | 79 | | private readonly Version _minFFmpegWorkingVtHwSurface = new Version(7, 0, 1); |
| 21 | 80 | | private readonly Version _minFFmpegDisplayRotationOption = new Version(6, 0); |
| 21 | 81 | | private readonly Version _minFFmpegAdvancedTonemapMode = new Version(7, 0, 1); |
| 21 | 82 | | private readonly Version _minFFmpegAlteredVaVkInterop = new Version(7, 0, 1); |
| 21 | 83 | | private readonly Version _minFFmpegQsvVppTonemapOption = new Version(7, 0, 1); |
| 21 | 84 | | private readonly Version _minFFmpegQsvVppOutRangeOption = new Version(7, 0, 1); |
| 21 | 85 | | private readonly Version _minFFmpegVaapiDeviceVendorId = new Version(7, 0, 1); |
| 21 | 86 | | private readonly Version _minFFmpegQsvVppScaleModeOption = new Version(6, 0); |
| 21 | 87 | | private readonly Version _minFFmpegRkmppHevcDecDoviRpu = new Version(7, 1, 1); |
| | 88 | |
|
| 0 | 89 | | private static readonly Regex _containerValidationRegex = new(ContainerValidationRegex, RegexOptions.Compiled); |
| | 90 | |
|
| 0 | 91 | | private static readonly string[] _videoProfilesH264 = |
| 0 | 92 | | [ |
| 0 | 93 | | "ConstrainedBaseline", |
| 0 | 94 | | "Baseline", |
| 0 | 95 | | "Extended", |
| 0 | 96 | | "Main", |
| 0 | 97 | | "High", |
| 0 | 98 | | "ProgressiveHigh", |
| 0 | 99 | | "ConstrainedHigh", |
| 0 | 100 | | "High10" |
| 0 | 101 | | ]; |
| | 102 | |
|
| 0 | 103 | | private static readonly string[] _videoProfilesH265 = |
| 0 | 104 | | [ |
| 0 | 105 | | "Main", |
| 0 | 106 | | "Main10" |
| 0 | 107 | | ]; |
| | 108 | |
|
| 0 | 109 | | private static readonly string[] _videoProfilesAv1 = |
| 0 | 110 | | [ |
| 0 | 111 | | "Main", |
| 0 | 112 | | "High", |
| 0 | 113 | | "Professional", |
| 0 | 114 | | ]; |
| | 115 | |
|
| 0 | 116 | | private static readonly HashSet<string> _mp4ContainerNames = new(StringComparer.OrdinalIgnoreCase) |
| 0 | 117 | | { |
| 0 | 118 | | "mp4", |
| 0 | 119 | | "m4a", |
| 0 | 120 | | "m4p", |
| 0 | 121 | | "m4b", |
| 0 | 122 | | "m4r", |
| 0 | 123 | | "m4v", |
| 0 | 124 | | }; |
| | 125 | |
|
| 0 | 126 | | private static readonly TonemappingMode[] _legacyTonemapModes = [TonemappingMode.max, TonemappingMode.rgb]; |
| 0 | 127 | | private static readonly TonemappingMode[] _advancedTonemapModes = [TonemappingMode.lum, TonemappingMode.itp]; |
| | 128 | |
|
| | 129 | | // Set max transcoding channels for encoders that can't handle more than a set amount of channels |
| | 130 | | // AAC, FLAC, ALAC, libopus, libvorbis encoders all support at least 8 channels |
| 0 | 131 | | private static readonly Dictionary<string, int> _audioTranscodeChannelLookup = new(StringComparer.OrdinalIgnoreC |
| 0 | 132 | | { |
| 0 | 133 | | { "libmp3lame", 2 }, |
| 0 | 134 | | { "libfdk_aac", 6 }, |
| 0 | 135 | | { "ac3", 6 }, |
| 0 | 136 | | { "eac3", 6 }, |
| 0 | 137 | | { "dca", 6 }, |
| 0 | 138 | | { "mlp", 6 }, |
| 0 | 139 | | { "truehd", 6 }, |
| 0 | 140 | | }; |
| | 141 | |
|
| 0 | 142 | | private static readonly Dictionary<HardwareAccelerationType, string> _mjpegCodecMap = new() |
| 0 | 143 | | { |
| 0 | 144 | | { HardwareAccelerationType.vaapi, _defaultMjpegEncoder + "_vaapi" }, |
| 0 | 145 | | { HardwareAccelerationType.qsv, _defaultMjpegEncoder + "_qsv" }, |
| 0 | 146 | | { HardwareAccelerationType.videotoolbox, _defaultMjpegEncoder + "_videotoolbox" }, |
| 0 | 147 | | { HardwareAccelerationType.rkmpp, _defaultMjpegEncoder + "_rkmpp" } |
| 0 | 148 | | }; |
| | 149 | |
|
| 0 | 150 | | public static readonly string[] LosslessAudioCodecs = |
| 0 | 151 | | [ |
| 0 | 152 | | "alac", |
| 0 | 153 | | "ape", |
| 0 | 154 | | "flac", |
| 0 | 155 | | "mlp", |
| 0 | 156 | | "truehd", |
| 0 | 157 | | "wavpack" |
| 0 | 158 | | ]; |
| | 159 | |
|
| | 160 | | public EncodingHelper( |
| | 161 | | IApplicationPaths appPaths, |
| | 162 | | IMediaEncoder mediaEncoder, |
| | 163 | | ISubtitleEncoder subtitleEncoder, |
| | 164 | | IConfiguration config, |
| | 165 | | IConfigurationManager configurationManager, |
| | 166 | | IPathManager pathManager) |
| | 167 | | { |
| 21 | 168 | | _appPaths = appPaths; |
| 21 | 169 | | _mediaEncoder = mediaEncoder; |
| 21 | 170 | | _subtitleEncoder = subtitleEncoder; |
| 21 | 171 | | _config = config; |
| 21 | 172 | | _configurationManager = configurationManager; |
| 21 | 173 | | _pathManager = pathManager; |
| 21 | 174 | | } |
| | 175 | |
|
| | 176 | | private enum DynamicHdrMetadataRemovalPlan |
| | 177 | | { |
| | 178 | | None, |
| | 179 | | RemoveDovi, |
| | 180 | | RemoveHdr10Plus, |
| | 181 | | } |
| | 182 | |
|
| | 183 | | [GeneratedRegex(@"\s+")] |
| | 184 | | private static partial Regex WhiteSpaceRegex(); |
| | 185 | |
|
| | 186 | | public string GetH264Encoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| 0 | 187 | | => GetH26xOrAv1Encoder("libx264", "h264", state, encodingOptions); |
| | 188 | |
|
| | 189 | | public string GetH265Encoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| 0 | 190 | | => GetH26xOrAv1Encoder("libx265", "hevc", state, encodingOptions); |
| | 191 | |
|
| | 192 | | public string GetAv1Encoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| 0 | 193 | | => GetH26xOrAv1Encoder("libsvtav1", "av1", state, encodingOptions); |
| | 194 | |
|
| | 195 | | private string GetH26xOrAv1Encoder(string defaultEncoder, string hwEncoder, EncodingJobInfo state, EncodingOptio |
| | 196 | | { |
| | 197 | | // Only use alternative encoders for video files. |
| | 198 | | // When using concat with folder rips, if the mfx session fails to initialize, ffmpeg will be stuck retrying |
| | 199 | | // Since transcoding of folder rips is experimental anyway, it's not worth adding additional variables such |
| 0 | 200 | | if (state.VideoType == VideoType.VideoFile) |
| | 201 | | { |
| 0 | 202 | | var hwType = encodingOptions.HardwareAccelerationType; |
| | 203 | |
|
| 0 | 204 | | var codecMap = new Dictionary<HardwareAccelerationType, string>() |
| 0 | 205 | | { |
| 0 | 206 | | { HardwareAccelerationType.amf, hwEncoder + "_amf" }, |
| 0 | 207 | | { HardwareAccelerationType.nvenc, hwEncoder + "_nvenc" }, |
| 0 | 208 | | { HardwareAccelerationType.qsv, hwEncoder + "_qsv" }, |
| 0 | 209 | | { HardwareAccelerationType.vaapi, hwEncoder + "_vaapi" }, |
| 0 | 210 | | { HardwareAccelerationType.videotoolbox, hwEncoder + "_videotoolbox" }, |
| 0 | 211 | | { HardwareAccelerationType.v4l2m2m, hwEncoder + "_v4l2m2m" }, |
| 0 | 212 | | { HardwareAccelerationType.rkmpp, hwEncoder + "_rkmpp" }, |
| 0 | 213 | | }; |
| | 214 | |
|
| 0 | 215 | | if (hwType != HardwareAccelerationType.none |
| 0 | 216 | | && encodingOptions.EnableHardwareEncoding |
| 0 | 217 | | && codecMap.TryGetValue(hwType, out var preferredEncoder) |
| 0 | 218 | | && _mediaEncoder.SupportsEncoder(preferredEncoder)) |
| | 219 | | { |
| 0 | 220 | | return preferredEncoder; |
| | 221 | | } |
| | 222 | | } |
| | 223 | |
|
| 0 | 224 | | return defaultEncoder; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | private string GetMjpegEncoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| | 228 | | { |
| 0 | 229 | | if (state.VideoType == VideoType.VideoFile) |
| | 230 | | { |
| 0 | 231 | | var hwType = encodingOptions.HardwareAccelerationType; |
| | 232 | |
|
| | 233 | | // Only Intel has VA-API MJPEG encoder |
| 0 | 234 | | if (hwType == HardwareAccelerationType.vaapi |
| 0 | 235 | | && !(_mediaEncoder.IsVaapiDeviceInteliHD |
| 0 | 236 | | || _mediaEncoder.IsVaapiDeviceInteli965)) |
| | 237 | | { |
| 0 | 238 | | return _defaultMjpegEncoder; |
| | 239 | | } |
| | 240 | |
|
| 0 | 241 | | if (hwType != HardwareAccelerationType.none |
| 0 | 242 | | && encodingOptions.EnableHardwareEncoding |
| 0 | 243 | | && _mjpegCodecMap.TryGetValue(hwType, out var preferredEncoder) |
| 0 | 244 | | && _mediaEncoder.SupportsEncoder(preferredEncoder)) |
| | 245 | | { |
| 0 | 246 | | return preferredEncoder; |
| | 247 | | } |
| | 248 | | } |
| | 249 | |
|
| 0 | 250 | | return _defaultMjpegEncoder; |
| | 251 | | } |
| | 252 | |
|
| | 253 | | private bool IsVaapiSupported(EncodingJobInfo state) |
| | 254 | | { |
| | 255 | | // vaapi will throw an error with this input |
| | 256 | | // [vaapi @ 0x7faed8000960] No VAAPI support for codec mpeg4 profile -99. |
| 0 | 257 | | if (string.Equals(state.VideoStream?.Codec, "mpeg4", StringComparison.OrdinalIgnoreCase)) |
| | 258 | | { |
| 0 | 259 | | return false; |
| | 260 | | } |
| | 261 | |
|
| 0 | 262 | | return _mediaEncoder.SupportsHwaccel("vaapi"); |
| | 263 | | } |
| | 264 | |
|
| | 265 | | private bool IsVaapiFullSupported() |
| | 266 | | { |
| 0 | 267 | | return _mediaEncoder.SupportsHwaccel("drm") |
| 0 | 268 | | && _mediaEncoder.SupportsHwaccel("vaapi") |
| 0 | 269 | | && _mediaEncoder.SupportsFilter("scale_vaapi") |
| 0 | 270 | | && _mediaEncoder.SupportsFilter("deinterlace_vaapi") |
| 0 | 271 | | && _mediaEncoder.SupportsFilter("tonemap_vaapi") |
| 0 | 272 | | && _mediaEncoder.SupportsFilter("procamp_vaapi") |
| 0 | 273 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.OverlayVaapiFrameSync) |
| 0 | 274 | | && _mediaEncoder.SupportsFilter("transpose_vaapi") |
| 0 | 275 | | && _mediaEncoder.SupportsFilter("hwupload_vaapi"); |
| | 276 | | } |
| | 277 | |
|
| | 278 | | private bool IsRkmppFullSupported() |
| | 279 | | { |
| 0 | 280 | | return _mediaEncoder.SupportsHwaccel("rkmpp") |
| 0 | 281 | | && _mediaEncoder.SupportsFilter("scale_rkrga") |
| 0 | 282 | | && _mediaEncoder.SupportsFilter("vpp_rkrga") |
| 0 | 283 | | && _mediaEncoder.SupportsFilter("overlay_rkrga"); |
| | 284 | | } |
| | 285 | |
|
| | 286 | | private bool IsOpenclFullSupported() |
| | 287 | | { |
| 0 | 288 | | return _mediaEncoder.SupportsHwaccel("opencl") |
| 0 | 289 | | && _mediaEncoder.SupportsFilter("scale_opencl") |
| 0 | 290 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.TonemapOpenclBt2390) |
| 0 | 291 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.OverlayOpenclFrameSync); |
| | 292 | |
|
| | 293 | | // Let transpose_opencl optional for the time being. |
| | 294 | | } |
| | 295 | |
|
| | 296 | | private bool IsCudaFullSupported() |
| | 297 | | { |
| 0 | 298 | | return _mediaEncoder.SupportsHwaccel("cuda") |
| 0 | 299 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.ScaleCudaFormat) |
| 0 | 300 | | && _mediaEncoder.SupportsFilter("yadif_cuda") |
| 0 | 301 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.TonemapCudaName) |
| 0 | 302 | | && _mediaEncoder.SupportsFilter("overlay_cuda") |
| 0 | 303 | | && _mediaEncoder.SupportsFilter("hwupload_cuda"); |
| | 304 | |
|
| | 305 | | // Let transpose_cuda optional for the time being. |
| | 306 | | } |
| | 307 | |
|
| | 308 | | private bool IsVulkanFullSupported() |
| | 309 | | { |
| 0 | 310 | | return _mediaEncoder.SupportsHwaccel("vulkan") |
| 0 | 311 | | && _mediaEncoder.SupportsFilter("libplacebo") |
| 0 | 312 | | && _mediaEncoder.SupportsFilter("scale_vulkan") |
| 0 | 313 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.OverlayVulkanFrameSync) |
| 0 | 314 | | && _mediaEncoder.SupportsFilter("transpose_vulkan") |
| 0 | 315 | | && _mediaEncoder.SupportsFilter("flip_vulkan"); |
| | 316 | | } |
| | 317 | |
|
| | 318 | | private bool IsVideoToolboxFullSupported() |
| | 319 | | { |
| 0 | 320 | | return _mediaEncoder.SupportsHwaccel("videotoolbox") |
| 0 | 321 | | && _mediaEncoder.SupportsFilter("yadif_videotoolbox") |
| 0 | 322 | | && _mediaEncoder.SupportsFilter("overlay_videotoolbox") |
| 0 | 323 | | && _mediaEncoder.SupportsFilter("tonemap_videotoolbox") |
| 0 | 324 | | && _mediaEncoder.SupportsFilter("scale_vt"); |
| | 325 | |
|
| | 326 | | // Let transpose_vt optional for the time being. |
| | 327 | | } |
| | 328 | |
|
| | 329 | | private bool IsSwTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 330 | | { |
| 0 | 331 | | if (state.VideoStream is null |
| 0 | 332 | | || GetVideoColorBitDepth(state) < 10 |
| 0 | 333 | | || !_mediaEncoder.SupportsFilter("tonemapx")) |
| | 334 | | { |
| 0 | 335 | | return false; |
| | 336 | | } |
| | 337 | |
|
| 0 | 338 | | return state.VideoStream.VideoRange == VideoRange.HDR; |
| | 339 | | } |
| | 340 | |
|
| | 341 | | private bool IsHwTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 342 | | { |
| 0 | 343 | | if (state.VideoStream is null |
| 0 | 344 | | || !options.EnableTonemapping |
| 0 | 345 | | || GetVideoColorBitDepth(state) < 10) |
| | 346 | | { |
| 0 | 347 | | return false; |
| | 348 | | } |
| | 349 | |
|
| 0 | 350 | | if (state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 351 | | && state.VideoStream.VideoRangeType == VideoRangeType.DOVI) |
| | 352 | | { |
| | 353 | | // Only native SW decoder, HW accelerator and hevc_rkmpp decoder can parse dovi rpu. |
| 0 | 354 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| | 355 | |
|
| 0 | 356 | | var isRkmppDecoder = vidDecoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 357 | | if (isRkmppDecoder |
| 0 | 358 | | && _mediaEncoder.EncoderVersion >= _minFFmpegRkmppHevcDecDoviRpu |
| 0 | 359 | | && string.Equals(state.VideoStream?.Codec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | 360 | | { |
| 0 | 361 | | return true; |
| | 362 | | } |
| | 363 | |
|
| 0 | 364 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 365 | | var isNvdecDecoder = vidDecoder.Contains("cuda", StringComparison.OrdinalIgnoreCase); |
| 0 | 366 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 367 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 368 | | var isVideoToolBoxDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 369 | | return isSwDecoder || isNvdecDecoder || isVaapiDecoder || isD3d11vaDecoder || isVideoToolBoxDecoder; |
| | 370 | | } |
| | 371 | |
|
| | 372 | | // GPU tonemapping supports all HDR RangeTypes |
| 0 | 373 | | return state.VideoStream.VideoRange == VideoRange.HDR; |
| | 374 | | } |
| | 375 | |
|
| | 376 | | private bool IsVulkanHwTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 377 | | { |
| 0 | 378 | | if (state.VideoStream is null) |
| | 379 | | { |
| 0 | 380 | | return false; |
| | 381 | | } |
| | 382 | |
|
| | 383 | | // libplacebo has partial Dolby Vision to SDR tonemapping support. |
| 0 | 384 | | return options.EnableTonemapping |
| 0 | 385 | | && state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 386 | | && GetVideoColorBitDepth(state) == 10; |
| | 387 | | } |
| | 388 | |
|
| | 389 | | private bool IsIntelVppTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 390 | | { |
| 0 | 391 | | if (state.VideoStream is null |
| 0 | 392 | | || !options.EnableVppTonemapping |
| 0 | 393 | | || GetVideoColorBitDepth(state) < 10) |
| | 394 | | { |
| 0 | 395 | | return false; |
| | 396 | | } |
| | 397 | |
|
| | 398 | | // prefer 'tonemap_vaapi' over 'vpp_qsv' on Linux for supporting Gen9/KBLx. |
| | 399 | | // 'vpp_qsv' requires VPL, which is only supported on Gen12/TGLx and newer. |
| 0 | 400 | | if (OperatingSystem.IsWindows() |
| 0 | 401 | | && options.HardwareAccelerationType == HardwareAccelerationType.qsv |
| 0 | 402 | | && _mediaEncoder.EncoderVersion < _minFFmpegQsvVppTonemapOption) |
| | 403 | | { |
| 0 | 404 | | return false; |
| | 405 | | } |
| | 406 | |
|
| 0 | 407 | | return state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 408 | | && IsDoviWithHdr10Bl(state.VideoStream); |
| | 409 | | } |
| | 410 | |
|
| | 411 | | private bool IsVideoToolboxTonemapAvailable(EncodingJobInfo state, EncodingOptions options) |
| | 412 | | { |
| 0 | 413 | | if (state.VideoStream is null |
| 0 | 414 | | || !options.EnableVideoToolboxTonemapping |
| 0 | 415 | | || GetVideoColorBitDepth(state) < 10) |
| | 416 | | { |
| 0 | 417 | | return false; |
| | 418 | | } |
| | 419 | |
|
| | 420 | | // Certain DV profile 5 video works in Safari with direct playing, but the VideoToolBox does not produce cor |
| | 421 | | // All other HDR formats working. |
| 0 | 422 | | return state.VideoStream.VideoRange == VideoRange.HDR |
| 0 | 423 | | && (IsDoviWithHdr10Bl(state.VideoStream) |
| 0 | 424 | | || state.VideoStream.VideoRangeType is VideoRangeType.HLG); |
| | 425 | | } |
| | 426 | |
|
| | 427 | | private bool IsVideoStreamHevcRext(EncodingJobInfo state) |
| | 428 | | { |
| 0 | 429 | | var videoStream = state.VideoStream; |
| 0 | 430 | | if (videoStream is null) |
| | 431 | | { |
| 0 | 432 | | return false; |
| | 433 | | } |
| | 434 | |
|
| 0 | 435 | | return string.Equals(videoStream.Codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 436 | | && (string.Equals(videoStream.Profile, "Rext", StringComparison.OrdinalIgnoreCase) |
| 0 | 437 | | || string.Equals(videoStream.PixelFormat, "yuv420p12le", StringComparison.OrdinalIgnoreCase) |
| 0 | 438 | | || string.Equals(videoStream.PixelFormat, "yuv422p", StringComparison.OrdinalIgnoreCase) |
| 0 | 439 | | || string.Equals(videoStream.PixelFormat, "yuv422p10le", StringComparison.OrdinalIgnoreCase) |
| 0 | 440 | | || string.Equals(videoStream.PixelFormat, "yuv422p12le", StringComparison.OrdinalIgnoreCase) |
| 0 | 441 | | || string.Equals(videoStream.PixelFormat, "yuv444p", StringComparison.OrdinalIgnoreCase) |
| 0 | 442 | | || string.Equals(videoStream.PixelFormat, "yuv444p10le", StringComparison.OrdinalIgnoreCase) |
| 0 | 443 | | || string.Equals(videoStream.PixelFormat, "yuv444p12le", StringComparison.OrdinalIgnoreCase)); |
| | 444 | | } |
| | 445 | |
|
| | 446 | | /// <summary> |
| | 447 | | /// Gets the name of the output video codec. |
| | 448 | | /// </summary> |
| | 449 | | /// <param name="state">Encoding state.</param> |
| | 450 | | /// <param name="encodingOptions">Encoding options.</param> |
| | 451 | | /// <returns>Encoder string.</returns> |
| | 452 | | public string GetVideoEncoder(EncodingJobInfo state, EncodingOptions encodingOptions) |
| | 453 | | { |
| 0 | 454 | | var codec = state.OutputVideoCodec; |
| | 455 | |
|
| 0 | 456 | | if (!string.IsNullOrEmpty(codec)) |
| | 457 | | { |
| 0 | 458 | | if (string.Equals(codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 459 | | { |
| 0 | 460 | | return GetAv1Encoder(state, encodingOptions); |
| | 461 | | } |
| | 462 | |
|
| 0 | 463 | | if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase) |
| 0 | 464 | | || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | 465 | | { |
| 0 | 466 | | return GetH265Encoder(state, encodingOptions); |
| | 467 | | } |
| | 468 | |
|
| 0 | 469 | | if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 470 | | { |
| 0 | 471 | | return GetH264Encoder(state, encodingOptions); |
| | 472 | | } |
| | 473 | |
|
| 0 | 474 | | if (string.Equals(codec, "mjpeg", StringComparison.OrdinalIgnoreCase)) |
| | 475 | | { |
| 0 | 476 | | return GetMjpegEncoder(state, encodingOptions); |
| | 477 | | } |
| | 478 | |
|
| 0 | 479 | | if (_containerValidationRegex.IsMatch(codec)) |
| | 480 | | { |
| 0 | 481 | | return codec.ToLowerInvariant(); |
| | 482 | | } |
| | 483 | | } |
| | 484 | |
|
| 0 | 485 | | return "copy"; |
| | 486 | | } |
| | 487 | |
|
| | 488 | | /// <summary> |
| | 489 | | /// Gets the user agent param. |
| | 490 | | /// </summary> |
| | 491 | | /// <param name="state">The state.</param> |
| | 492 | | /// <returns>System.String.</returns> |
| | 493 | | public string GetUserAgentParam(EncodingJobInfo state) |
| | 494 | | { |
| 0 | 495 | | if (state.RemoteHttpHeaders.TryGetValue("User-Agent", out string useragent)) |
| | 496 | | { |
| 0 | 497 | | return "-user_agent \"" + useragent + "\""; |
| | 498 | | } |
| | 499 | |
|
| 0 | 500 | | return string.Empty; |
| | 501 | | } |
| | 502 | |
|
| | 503 | | /// <summary> |
| | 504 | | /// Gets the referer param. |
| | 505 | | /// </summary> |
| | 506 | | /// <param name="state">The state.</param> |
| | 507 | | /// <returns>System.String.</returns> |
| | 508 | | public string GetRefererParam(EncodingJobInfo state) |
| | 509 | | { |
| 0 | 510 | | if (state.RemoteHttpHeaders.TryGetValue("Referer", out string referer)) |
| | 511 | | { |
| 0 | 512 | | return "-referer \"" + referer + "\""; |
| | 513 | | } |
| | 514 | |
|
| 0 | 515 | | return string.Empty; |
| | 516 | | } |
| | 517 | |
|
| | 518 | | public static string GetInputFormat(string container) |
| | 519 | | { |
| 0 | 520 | | if (string.IsNullOrEmpty(container) || !_containerValidationRegex.IsMatch(container)) |
| | 521 | | { |
| 0 | 522 | | return null; |
| | 523 | | } |
| | 524 | |
|
| 0 | 525 | | container = container.Replace("mkv", "matroska", StringComparison.OrdinalIgnoreCase); |
| | 526 | |
|
| 0 | 527 | | if (string.Equals(container, "ts", StringComparison.OrdinalIgnoreCase)) |
| | 528 | | { |
| 0 | 529 | | return "mpegts"; |
| | 530 | | } |
| | 531 | |
|
| | 532 | | // For these need to find out the ffmpeg names |
| 0 | 533 | | if (string.Equals(container, "m2ts", StringComparison.OrdinalIgnoreCase)) |
| | 534 | | { |
| 0 | 535 | | return null; |
| | 536 | | } |
| | 537 | |
|
| 0 | 538 | | if (string.Equals(container, "wmv", StringComparison.OrdinalIgnoreCase)) |
| | 539 | | { |
| 0 | 540 | | return null; |
| | 541 | | } |
| | 542 | |
|
| 0 | 543 | | if (string.Equals(container, "mts", StringComparison.OrdinalIgnoreCase)) |
| | 544 | | { |
| 0 | 545 | | return null; |
| | 546 | | } |
| | 547 | |
|
| 0 | 548 | | if (string.Equals(container, "vob", StringComparison.OrdinalIgnoreCase)) |
| | 549 | | { |
| 0 | 550 | | return null; |
| | 551 | | } |
| | 552 | |
|
| 0 | 553 | | if (string.Equals(container, "mpg", StringComparison.OrdinalIgnoreCase)) |
| | 554 | | { |
| 0 | 555 | | return null; |
| | 556 | | } |
| | 557 | |
|
| 0 | 558 | | if (string.Equals(container, "mpeg", StringComparison.OrdinalIgnoreCase)) |
| | 559 | | { |
| 0 | 560 | | return null; |
| | 561 | | } |
| | 562 | |
|
| 0 | 563 | | if (string.Equals(container, "rec", StringComparison.OrdinalIgnoreCase)) |
| | 564 | | { |
| 0 | 565 | | return null; |
| | 566 | | } |
| | 567 | |
|
| 0 | 568 | | if (string.Equals(container, "dvr-ms", StringComparison.OrdinalIgnoreCase)) |
| | 569 | | { |
| 0 | 570 | | return null; |
| | 571 | | } |
| | 572 | |
|
| 0 | 573 | | if (string.Equals(container, "ogm", StringComparison.OrdinalIgnoreCase)) |
| | 574 | | { |
| 0 | 575 | | return null; |
| | 576 | | } |
| | 577 | |
|
| 0 | 578 | | if (string.Equals(container, "divx", StringComparison.OrdinalIgnoreCase)) |
| | 579 | | { |
| 0 | 580 | | return null; |
| | 581 | | } |
| | 582 | |
|
| 0 | 583 | | if (string.Equals(container, "tp", StringComparison.OrdinalIgnoreCase)) |
| | 584 | | { |
| 0 | 585 | | return null; |
| | 586 | | } |
| | 587 | |
|
| 0 | 588 | | if (string.Equals(container, "rmvb", StringComparison.OrdinalIgnoreCase)) |
| | 589 | | { |
| 0 | 590 | | return null; |
| | 591 | | } |
| | 592 | |
|
| 0 | 593 | | if (string.Equals(container, "rtp", StringComparison.OrdinalIgnoreCase)) |
| | 594 | | { |
| 0 | 595 | | return null; |
| | 596 | | } |
| | 597 | |
|
| | 598 | | // Seeing reported failures here, not sure yet if this is related to specifying input format |
| 0 | 599 | | if (string.Equals(container, "m4v", StringComparison.OrdinalIgnoreCase)) |
| | 600 | | { |
| 0 | 601 | | return null; |
| | 602 | | } |
| | 603 | |
|
| | 604 | | // obviously don't do this for strm files |
| 0 | 605 | | if (string.Equals(container, "strm", StringComparison.OrdinalIgnoreCase)) |
| | 606 | | { |
| 0 | 607 | | return null; |
| | 608 | | } |
| | 609 | |
|
| | 610 | | // ISO files don't have an ffmpeg format |
| 0 | 611 | | if (string.Equals(container, "iso", StringComparison.OrdinalIgnoreCase)) |
| | 612 | | { |
| 0 | 613 | | return null; |
| | 614 | | } |
| | 615 | |
|
| 0 | 616 | | return container; |
| | 617 | | } |
| | 618 | |
|
| | 619 | | /// <summary> |
| | 620 | | /// Gets decoder from a codec. |
| | 621 | | /// </summary> |
| | 622 | | /// <param name="codec">Codec to use.</param> |
| | 623 | | /// <returns>Decoder string.</returns> |
| | 624 | | public string GetDecoderFromCodec(string codec) |
| | 625 | | { |
| | 626 | | // For these need to find out the ffmpeg names |
| 0 | 627 | | if (string.Equals(codec, "mp2", StringComparison.OrdinalIgnoreCase)) |
| | 628 | | { |
| 0 | 629 | | return null; |
| | 630 | | } |
| | 631 | |
|
| 0 | 632 | | if (string.Equals(codec, "aac_latm", StringComparison.OrdinalIgnoreCase)) |
| | 633 | | { |
| 0 | 634 | | return null; |
| | 635 | | } |
| | 636 | |
|
| 0 | 637 | | if (string.Equals(codec, "eac3", StringComparison.OrdinalIgnoreCase)) |
| | 638 | | { |
| 0 | 639 | | return null; |
| | 640 | | } |
| | 641 | |
|
| 0 | 642 | | if (_mediaEncoder.SupportsDecoder(codec)) |
| | 643 | | { |
| 0 | 644 | | return codec; |
| | 645 | | } |
| | 646 | |
|
| 0 | 647 | | return null; |
| | 648 | | } |
| | 649 | |
|
| | 650 | | /// <summary> |
| | 651 | | /// Infers the audio codec based on the url. |
| | 652 | | /// </summary> |
| | 653 | | /// <param name="container">Container to use.</param> |
| | 654 | | /// <returns>Codec string.</returns> |
| | 655 | | public string InferAudioCodec(string container) |
| | 656 | | { |
| 0 | 657 | | if (string.IsNullOrWhiteSpace(container)) |
| | 658 | | { |
| | 659 | | // this may not work, but if the client is that broken we cannot do anything better |
| 0 | 660 | | return "aac"; |
| | 661 | | } |
| | 662 | |
|
| 0 | 663 | | var inferredCodec = container.ToLowerInvariant(); |
| | 664 | |
|
| 0 | 665 | | return inferredCodec switch |
| 0 | 666 | | { |
| 0 | 667 | | "ogg" or "oga" or "ogv" or "webm" or "webma" => "opus", |
| 0 | 668 | | "m4a" or "m4b" or "mp4" or "mov" or "mkv" or "mka" => "aac", |
| 0 | 669 | | "ts" or "avi" or "flv" or "f4v" or "swf" => "mp3", |
| 0 | 670 | | _ => inferredCodec |
| 0 | 671 | | }; |
| | 672 | | } |
| | 673 | |
|
| | 674 | | /// <summary> |
| | 675 | | /// Infers the video codec. |
| | 676 | | /// </summary> |
| | 677 | | /// <param name="url">The URL.</param> |
| | 678 | | /// <returns>System.Nullable{VideoCodecs}.</returns> |
| | 679 | | public string InferVideoCodec(string url) |
| | 680 | | { |
| 0 | 681 | | var ext = Path.GetExtension(url.AsSpan()); |
| | 682 | |
|
| 0 | 683 | | if (ext.Equals(".asf", StringComparison.OrdinalIgnoreCase)) |
| | 684 | | { |
| 0 | 685 | | return "wmv"; |
| | 686 | | } |
| | 687 | |
|
| 0 | 688 | | if (ext.Equals(".webm", StringComparison.OrdinalIgnoreCase)) |
| | 689 | | { |
| | 690 | | // TODO: this may not always mean VP8, as the codec ages |
| 0 | 691 | | return "vp8"; |
| | 692 | | } |
| | 693 | |
|
| 0 | 694 | | if (ext.Equals(".ogg", StringComparison.OrdinalIgnoreCase) || ext.Equals(".ogv", StringComparison.OrdinalIgn |
| | 695 | | { |
| 0 | 696 | | return "theora"; |
| | 697 | | } |
| | 698 | |
|
| 0 | 699 | | if (ext.Equals(".m3u8", StringComparison.OrdinalIgnoreCase) || ext.Equals(".ts", StringComparison.OrdinalIgn |
| | 700 | | { |
| 0 | 701 | | return "h264"; |
| | 702 | | } |
| | 703 | |
|
| 0 | 704 | | return "copy"; |
| | 705 | | } |
| | 706 | |
|
| | 707 | | public int GetVideoProfileScore(string videoCodec, string videoProfile) |
| | 708 | | { |
| | 709 | | // strip spaces because they may be stripped out on the query string |
| 0 | 710 | | string profile = videoProfile.Replace(" ", string.Empty, StringComparison.Ordinal); |
| 0 | 711 | | if (string.Equals("h264", videoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 712 | | { |
| 0 | 713 | | return Array.FindIndex(_videoProfilesH264, x => string.Equals(x, profile, StringComparison.OrdinalIgnore |
| | 714 | | } |
| | 715 | |
|
| 0 | 716 | | if (string.Equals("hevc", videoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 717 | | { |
| 0 | 718 | | return Array.FindIndex(_videoProfilesH265, x => string.Equals(x, profile, StringComparison.OrdinalIgnore |
| | 719 | | } |
| | 720 | |
|
| 0 | 721 | | if (string.Equals("av1", videoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 722 | | { |
| 0 | 723 | | return Array.FindIndex(_videoProfilesAv1, x => string.Equals(x, profile, StringComparison.OrdinalIgnoreC |
| | 724 | | } |
| | 725 | |
|
| 0 | 726 | | return -1; |
| | 727 | | } |
| | 728 | |
|
| | 729 | | /// <summary> |
| | 730 | | /// Gets the audio encoder. |
| | 731 | | /// </summary> |
| | 732 | | /// <param name="state">The state.</param> |
| | 733 | | /// <returns>System.String.</returns> |
| | 734 | | public string GetAudioEncoder(EncodingJobInfo state) |
| | 735 | | { |
| 0 | 736 | | var codec = state.OutputAudioCodec; |
| | 737 | |
|
| 0 | 738 | | if (!_containerValidationRegex.IsMatch(codec)) |
| | 739 | | { |
| 0 | 740 | | codec = "aac"; |
| | 741 | | } |
| | 742 | |
|
| 0 | 743 | | if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase)) |
| | 744 | | { |
| | 745 | | // Use Apple's aac encoder if available as it provides best audio quality |
| 0 | 746 | | if (_mediaEncoder.SupportsEncoder("aac_at")) |
| | 747 | | { |
| 0 | 748 | | return "aac_at"; |
| | 749 | | } |
| | 750 | |
|
| | 751 | | // Use libfdk_aac for better audio quality if using custom build of FFmpeg which has fdk_aac support |
| 0 | 752 | | if (_mediaEncoder.SupportsEncoder("libfdk_aac")) |
| | 753 | | { |
| 0 | 754 | | return "libfdk_aac"; |
| | 755 | | } |
| | 756 | |
|
| 0 | 757 | | return "aac"; |
| | 758 | | } |
| | 759 | |
|
| 0 | 760 | | if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase)) |
| | 761 | | { |
| 0 | 762 | | return "libmp3lame"; |
| | 763 | | } |
| | 764 | |
|
| 0 | 765 | | if (string.Equals(codec, "vorbis", StringComparison.OrdinalIgnoreCase)) |
| | 766 | | { |
| 0 | 767 | | return "libvorbis"; |
| | 768 | | } |
| | 769 | |
|
| 0 | 770 | | if (string.Equals(codec, "opus", StringComparison.OrdinalIgnoreCase)) |
| | 771 | | { |
| 0 | 772 | | return "libopus"; |
| | 773 | | } |
| | 774 | |
|
| 0 | 775 | | if (string.Equals(codec, "flac", StringComparison.OrdinalIgnoreCase)) |
| | 776 | | { |
| 0 | 777 | | return "flac"; |
| | 778 | | } |
| | 779 | |
|
| 0 | 780 | | if (string.Equals(codec, "dts", StringComparison.OrdinalIgnoreCase)) |
| | 781 | | { |
| 0 | 782 | | return "dca"; |
| | 783 | | } |
| | 784 | |
|
| 0 | 785 | | if (string.Equals(codec, "alac", StringComparison.OrdinalIgnoreCase)) |
| | 786 | | { |
| | 787 | | // The ffmpeg upstream breaks the AudioToolbox ALAC encoder in version 6.1 but fixes it in version 7.0. |
| | 788 | | // Since ALAC is lossless in quality and the AudioToolbox encoder is not faster, |
| | 789 | | // its only benefit is a smaller file size. |
| | 790 | | // To prevent problems, use the ffmpeg native encoder instead. |
| 0 | 791 | | return "alac"; |
| | 792 | | } |
| | 793 | |
|
| 0 | 794 | | return codec.ToLowerInvariant(); |
| | 795 | | } |
| | 796 | |
|
| | 797 | | private string GetRkmppDeviceArgs(string alias) |
| | 798 | | { |
| 0 | 799 | | alias ??= RkmppAlias; |
| | 800 | |
|
| | 801 | | // device selection in rk is not supported. |
| 0 | 802 | | return " -init_hw_device rkmpp=" + alias; |
| | 803 | | } |
| | 804 | |
|
| | 805 | | private string GetVideoToolboxDeviceArgs(string alias) |
| | 806 | | { |
| 0 | 807 | | alias ??= VideotoolboxAlias; |
| | 808 | |
|
| | 809 | | // device selection in vt is not supported. |
| 0 | 810 | | return " -init_hw_device videotoolbox=" + alias; |
| | 811 | | } |
| | 812 | |
|
| | 813 | | private string GetCudaDeviceArgs(int deviceIndex, string alias) |
| | 814 | | { |
| 0 | 815 | | alias ??= CudaAlias; |
| 0 | 816 | | deviceIndex = deviceIndex >= 0 |
| 0 | 817 | | ? deviceIndex |
| 0 | 818 | | : 0; |
| | 819 | |
|
| 0 | 820 | | return string.Format( |
| 0 | 821 | | CultureInfo.InvariantCulture, |
| 0 | 822 | | " -init_hw_device cuda={0}:{1}", |
| 0 | 823 | | alias, |
| 0 | 824 | | deviceIndex); |
| | 825 | | } |
| | 826 | |
|
| | 827 | | private string GetVulkanDeviceArgs(int deviceIndex, string deviceName, string srcDeviceAlias, string alias) |
| | 828 | | { |
| 0 | 829 | | alias ??= VulkanAlias; |
| 0 | 830 | | deviceIndex = deviceIndex >= 0 |
| 0 | 831 | | ? deviceIndex |
| 0 | 832 | | : 0; |
| 0 | 833 | | var vendorOpts = string.IsNullOrEmpty(deviceName) |
| 0 | 834 | | ? ":" + deviceIndex |
| 0 | 835 | | : ":" + "\"" + deviceName + "\""; |
| 0 | 836 | | var options = string.IsNullOrEmpty(srcDeviceAlias) |
| 0 | 837 | | ? vendorOpts |
| 0 | 838 | | : "@" + srcDeviceAlias; |
| | 839 | |
|
| 0 | 840 | | return string.Format( |
| 0 | 841 | | CultureInfo.InvariantCulture, |
| 0 | 842 | | " -init_hw_device vulkan={0}{1}", |
| 0 | 843 | | alias, |
| 0 | 844 | | options); |
| | 845 | | } |
| | 846 | |
|
| | 847 | | private string GetOpenclDeviceArgs(int deviceIndex, string deviceVendorName, string srcDeviceAlias, string alias |
| | 848 | | { |
| 0 | 849 | | alias ??= OpenclAlias; |
| 0 | 850 | | deviceIndex = deviceIndex >= 0 |
| 0 | 851 | | ? deviceIndex |
| 0 | 852 | | : 0; |
| 0 | 853 | | var vendorOpts = string.IsNullOrEmpty(deviceVendorName) |
| 0 | 854 | | ? ":0.0" |
| 0 | 855 | | : ":." + deviceIndex + ",device_vendor=\"" + deviceVendorName + "\""; |
| 0 | 856 | | var options = string.IsNullOrEmpty(srcDeviceAlias) |
| 0 | 857 | | ? vendorOpts |
| 0 | 858 | | : "@" + srcDeviceAlias; |
| | 859 | |
|
| 0 | 860 | | return string.Format( |
| 0 | 861 | | CultureInfo.InvariantCulture, |
| 0 | 862 | | " -init_hw_device opencl={0}{1}", |
| 0 | 863 | | alias, |
| 0 | 864 | | options); |
| | 865 | | } |
| | 866 | |
|
| | 867 | | private string GetD3d11vaDeviceArgs(int deviceIndex, string deviceVendorId, string alias) |
| | 868 | | { |
| 0 | 869 | | alias ??= D3d11vaAlias; |
| 0 | 870 | | deviceIndex = deviceIndex >= 0 ? deviceIndex : 0; |
| 0 | 871 | | var options = string.IsNullOrEmpty(deviceVendorId) |
| 0 | 872 | | ? deviceIndex.ToString(CultureInfo.InvariantCulture) |
| 0 | 873 | | : ",vendor=" + deviceVendorId; |
| | 874 | |
|
| 0 | 875 | | return string.Format( |
| 0 | 876 | | CultureInfo.InvariantCulture, |
| 0 | 877 | | " -init_hw_device d3d11va={0}:{1}", |
| 0 | 878 | | alias, |
| 0 | 879 | | options); |
| | 880 | | } |
| | 881 | |
|
| | 882 | | private string GetVaapiDeviceArgs(string renderNodePath, string driver, string kernelDriver, string vendorId, st |
| | 883 | | { |
| 0 | 884 | | alias ??= VaapiAlias; |
| 0 | 885 | | var haveVendorId = !string.IsNullOrEmpty(vendorId) |
| 0 | 886 | | && _mediaEncoder.EncoderVersion >= _minFFmpegVaapiDeviceVendorId; |
| | 887 | |
|
| | 888 | | // Priority: 'renderNodePath' > 'vendorId' > 'kernelDriver' |
| 0 | 889 | | var driverOpts = File.Exists(renderNodePath) |
| 0 | 890 | | ? renderNodePath |
| 0 | 891 | | : (haveVendorId ? $",vendor_id={vendorId}" : (string.IsNullOrEmpty(kernelDriver) ? string.Empty : $",ker |
| | 892 | |
|
| | 893 | | // 'driver' behaves similarly to env LIBVA_DRIVER_NAME |
| 0 | 894 | | driverOpts += string.IsNullOrEmpty(driver) ? string.Empty : ",driver=" + driver; |
| | 895 | |
|
| 0 | 896 | | var options = string.IsNullOrEmpty(srcDeviceAlias) |
| 0 | 897 | | ? (string.IsNullOrEmpty(driverOpts) ? string.Empty : ":" + driverOpts) |
| 0 | 898 | | : "@" + srcDeviceAlias; |
| | 899 | |
|
| 0 | 900 | | return string.Format( |
| 0 | 901 | | CultureInfo.InvariantCulture, |
| 0 | 902 | | " -init_hw_device vaapi={0}{1}", |
| 0 | 903 | | alias, |
| 0 | 904 | | options); |
| | 905 | | } |
| | 906 | |
|
| | 907 | | private string GetDrmDeviceArgs(string renderNodePath, string alias) |
| | 908 | | { |
| 0 | 909 | | alias ??= DrmAlias; |
| 0 | 910 | | renderNodePath = renderNodePath ?? "/dev/dri/renderD128"; |
| | 911 | |
|
| 0 | 912 | | return string.Format( |
| 0 | 913 | | CultureInfo.InvariantCulture, |
| 0 | 914 | | " -init_hw_device drm={0}:{1}", |
| 0 | 915 | | alias, |
| 0 | 916 | | renderNodePath); |
| | 917 | | } |
| | 918 | |
|
| | 919 | | private string GetQsvDeviceArgs(string renderNodePath, string alias) |
| | 920 | | { |
| 0 | 921 | | var arg = " -init_hw_device qsv=" + (alias ?? QsvAlias); |
| 0 | 922 | | if (OperatingSystem.IsLinux()) |
| | 923 | | { |
| | 924 | | // derive qsv from vaapi device |
| 0 | 925 | | return GetVaapiDeviceArgs(renderNodePath, "iHD", "i915", "0x8086", null, VaapiAlias) + arg + "@" + Vaapi |
| | 926 | | } |
| | 927 | |
|
| 0 | 928 | | if (OperatingSystem.IsWindows()) |
| | 929 | | { |
| | 930 | | // on Windows, the deviceIndex is an int |
| 0 | 931 | | if (int.TryParse(renderNodePath, NumberStyles.Integer, CultureInfo.InvariantCulture, out int deviceIndex |
| | 932 | | { |
| 0 | 933 | | return GetD3d11vaDeviceArgs(deviceIndex, string.Empty, D3d11vaAlias) + arg + "@" + D3d11vaAlias; |
| | 934 | | } |
| | 935 | |
|
| | 936 | | // derive qsv from d3d11va device |
| 0 | 937 | | return GetD3d11vaDeviceArgs(0, "0x8086", D3d11vaAlias) + arg + "@" + D3d11vaAlias; |
| | 938 | | } |
| | 939 | |
|
| 0 | 940 | | return null; |
| | 941 | | } |
| | 942 | |
|
| | 943 | | private string GetFilterHwDeviceArgs(string alias) |
| | 944 | | { |
| 0 | 945 | | return string.IsNullOrEmpty(alias) |
| 0 | 946 | | ? string.Empty |
| 0 | 947 | | : " -filter_hw_device " + alias; |
| | 948 | | } |
| | 949 | |
|
| | 950 | | public string GetGraphicalSubCanvasSize(EncodingJobInfo state) |
| | 951 | | { |
| | 952 | | // DVBSUB uses the fixed canvas size 720x576 |
| 0 | 953 | | if (state.SubtitleStream is not null |
| 0 | 954 | | && ShouldEncodeSubtitle(state) |
| 0 | 955 | | && !state.SubtitleStream.IsTextSubtitleStream |
| 0 | 956 | | && !string.Equals(state.SubtitleStream.Codec, "DVBSUB", StringComparison.OrdinalIgnoreCase)) |
| | 957 | | { |
| 0 | 958 | | var subtitleWidth = state.SubtitleStream?.Width; |
| 0 | 959 | | var subtitleHeight = state.SubtitleStream?.Height; |
| | 960 | |
|
| 0 | 961 | | if (subtitleWidth.HasValue |
| 0 | 962 | | && subtitleHeight.HasValue |
| 0 | 963 | | && subtitleWidth.Value > 0 |
| 0 | 964 | | && subtitleHeight.Value > 0) |
| | 965 | | { |
| 0 | 966 | | return string.Format( |
| 0 | 967 | | CultureInfo.InvariantCulture, |
| 0 | 968 | | " -canvas_size {0}x{1}", |
| 0 | 969 | | subtitleWidth.Value, |
| 0 | 970 | | subtitleHeight.Value); |
| | 971 | | } |
| | 972 | | } |
| | 973 | |
|
| 0 | 974 | | return string.Empty; |
| | 975 | | } |
| | 976 | |
|
| | 977 | | /// <summary> |
| | 978 | | /// Gets the input video hwaccel argument. |
| | 979 | | /// </summary> |
| | 980 | | /// <param name="state">Encoding state.</param> |
| | 981 | | /// <param name="options">Encoding options.</param> |
| | 982 | | /// <returns>Input video hwaccel arguments.</returns> |
| | 983 | | public string GetInputVideoHwaccelArgs(EncodingJobInfo state, EncodingOptions options) |
| | 984 | | { |
| 0 | 985 | | if (!state.IsVideoRequest) |
| | 986 | | { |
| 0 | 987 | | return string.Empty; |
| | 988 | | } |
| | 989 | |
|
| 0 | 990 | | var vidEncoder = GetVideoEncoder(state, options) ?? string.Empty; |
| 0 | 991 | | if (IsCopyCodec(vidEncoder)) |
| | 992 | | { |
| 0 | 993 | | return string.Empty; |
| | 994 | | } |
| | 995 | |
|
| 0 | 996 | | var args = new StringBuilder(); |
| 0 | 997 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 998 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 999 | | var isMacOS = OperatingSystem.IsMacOS(); |
| 0 | 1000 | | var optHwaccelType = options.HardwareAccelerationType; |
| 0 | 1001 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 1002 | | var isHwTonemapAvailable = IsHwTonemapAvailable(state, options); |
| | 1003 | |
|
| 0 | 1004 | | if (optHwaccelType == HardwareAccelerationType.vaapi) |
| | 1005 | | { |
| 0 | 1006 | | if (!isLinux || !_mediaEncoder.SupportsHwaccel("vaapi")) |
| | 1007 | | { |
| 0 | 1008 | | return string.Empty; |
| | 1009 | | } |
| | 1010 | |
|
| 0 | 1011 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 1012 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 1013 | | if (!isVaapiDecoder && !isVaapiEncoder) |
| | 1014 | | { |
| 0 | 1015 | | return string.Empty; |
| | 1016 | | } |
| | 1017 | |
|
| 0 | 1018 | | if (_mediaEncoder.IsVaapiDeviceInteliHD) |
| | 1019 | | { |
| 0 | 1020 | | args.Append(GetVaapiDeviceArgs(options.VaapiDevice, "iHD", null, null, null, VaapiAlias)); |
| | 1021 | | } |
| 0 | 1022 | | else if (_mediaEncoder.IsVaapiDeviceInteli965) |
| | 1023 | | { |
| | 1024 | | // Only override i965 since it has lower priority than iHD in libva lookup. |
| 0 | 1025 | | Environment.SetEnvironmentVariable("LIBVA_DRIVER_NAME", "i965"); |
| 0 | 1026 | | Environment.SetEnvironmentVariable("LIBVA_DRIVER_NAME_JELLYFIN", "i965"); |
| 0 | 1027 | | args.Append(GetVaapiDeviceArgs(options.VaapiDevice, "i965", null, null, null, VaapiAlias)); |
| | 1028 | | } |
| | 1029 | |
|
| 0 | 1030 | | var filterDevArgs = string.Empty; |
| 0 | 1031 | | var doOclTonemap = isHwTonemapAvailable && IsOpenclFullSupported(); |
| | 1032 | |
|
| 0 | 1033 | | if (_mediaEncoder.IsVaapiDeviceInteliHD || _mediaEncoder.IsVaapiDeviceInteli965) |
| | 1034 | | { |
| 0 | 1035 | | if (doOclTonemap && !isVaapiDecoder) |
| | 1036 | | { |
| 0 | 1037 | | args.Append(GetOpenclDeviceArgs(0, null, VaapiAlias, OpenclAlias)); |
| 0 | 1038 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1039 | | } |
| | 1040 | | } |
| 0 | 1041 | | else if (_mediaEncoder.IsVaapiDeviceAmd) |
| | 1042 | | { |
| | 1043 | | // Disable AMD EFC feature since it's still unstable in upstream Mesa. |
| 0 | 1044 | | Environment.SetEnvironmentVariable("AMD_DEBUG", "noefc"); |
| | 1045 | |
|
| 0 | 1046 | | if (IsVulkanFullSupported() |
| 0 | 1047 | | && _mediaEncoder.IsVaapiDeviceSupportVulkanDrmInterop |
| 0 | 1048 | | && Environment.OSVersion.Version >= _minKernelVersionAmdVkFmtModifier) |
| | 1049 | | { |
| 0 | 1050 | | args.Append(GetDrmDeviceArgs(options.VaapiDevice, DrmAlias)); |
| 0 | 1051 | | args.Append(GetVaapiDeviceArgs(null, null, null, null, DrmAlias, VaapiAlias)); |
| 0 | 1052 | | args.Append(GetVulkanDeviceArgs(0, null, DrmAlias, VulkanAlias)); |
| | 1053 | |
|
| | 1054 | | // libplacebo wants an explicitly set vulkan filter device. |
| 0 | 1055 | | filterDevArgs = GetFilterHwDeviceArgs(VulkanAlias); |
| | 1056 | | } |
| | 1057 | | else |
| | 1058 | | { |
| 0 | 1059 | | args.Append(GetVaapiDeviceArgs(options.VaapiDevice, null, null, null, null, VaapiAlias)); |
| 0 | 1060 | | filterDevArgs = GetFilterHwDeviceArgs(VaapiAlias); |
| | 1061 | |
|
| 0 | 1062 | | if (doOclTonemap) |
| | 1063 | | { |
| | 1064 | | // ROCm/ROCr OpenCL runtime |
| 0 | 1065 | | args.Append(GetOpenclDeviceArgs(0, "Advanced Micro Devices", null, OpenclAlias)); |
| 0 | 1066 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1067 | | } |
| | 1068 | | } |
| | 1069 | | } |
| 0 | 1070 | | else if (doOclTonemap) |
| | 1071 | | { |
| 0 | 1072 | | args.Append(GetOpenclDeviceArgs(0, null, null, OpenclAlias)); |
| 0 | 1073 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1074 | | } |
| | 1075 | |
|
| 0 | 1076 | | args.Append(filterDevArgs); |
| | 1077 | | } |
| 0 | 1078 | | else if (optHwaccelType == HardwareAccelerationType.qsv) |
| | 1079 | | { |
| 0 | 1080 | | if ((!isLinux && !isWindows) || !_mediaEncoder.SupportsHwaccel("qsv")) |
| | 1081 | | { |
| 0 | 1082 | | return string.Empty; |
| | 1083 | | } |
| | 1084 | |
|
| 0 | 1085 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 1086 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 1087 | | var isQsvDecoder = vidDecoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 1088 | | var isQsvEncoder = vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 1089 | | var isHwDecoder = isQsvDecoder || isVaapiDecoder || isD3d11vaDecoder; |
| 0 | 1090 | | if (!isHwDecoder && !isQsvEncoder) |
| | 1091 | | { |
| 0 | 1092 | | return string.Empty; |
| | 1093 | | } |
| | 1094 | |
|
| 0 | 1095 | | args.Append(GetQsvDeviceArgs(options.QsvDevice, QsvAlias)); |
| 0 | 1096 | | var filterDevArgs = GetFilterHwDeviceArgs(QsvAlias); |
| | 1097 | | // child device used by qsv. |
| 0 | 1098 | | if (_mediaEncoder.SupportsHwaccel("vaapi") || _mediaEncoder.SupportsHwaccel("d3d11va")) |
| | 1099 | | { |
| 0 | 1100 | | if (isHwTonemapAvailable && IsOpenclFullSupported()) |
| | 1101 | | { |
| 0 | 1102 | | var srcAlias = isLinux ? VaapiAlias : D3d11vaAlias; |
| 0 | 1103 | | args.Append(GetOpenclDeviceArgs(0, null, srcAlias, OpenclAlias)); |
| 0 | 1104 | | if (!isHwDecoder) |
| | 1105 | | { |
| 0 | 1106 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1107 | | } |
| | 1108 | | } |
| | 1109 | | } |
| | 1110 | |
|
| 0 | 1111 | | args.Append(filterDevArgs); |
| | 1112 | | } |
| 0 | 1113 | | else if (optHwaccelType == HardwareAccelerationType.nvenc) |
| | 1114 | | { |
| 0 | 1115 | | if ((!isLinux && !isWindows) || !IsCudaFullSupported()) |
| | 1116 | | { |
| 0 | 1117 | | return string.Empty; |
| | 1118 | | } |
| | 1119 | |
|
| 0 | 1120 | | var isCuvidDecoder = vidDecoder.Contains("cuvid", StringComparison.OrdinalIgnoreCase); |
| 0 | 1121 | | var isNvdecDecoder = vidDecoder.Contains("cuda", StringComparison.OrdinalIgnoreCase); |
| 0 | 1122 | | var isNvencEncoder = vidEncoder.Contains("nvenc", StringComparison.OrdinalIgnoreCase); |
| 0 | 1123 | | var isHwDecoder = isNvdecDecoder || isCuvidDecoder; |
| 0 | 1124 | | if (!isHwDecoder && !isNvencEncoder) |
| | 1125 | | { |
| 0 | 1126 | | return string.Empty; |
| | 1127 | | } |
| | 1128 | |
|
| 0 | 1129 | | args.Append(GetCudaDeviceArgs(0, CudaAlias)) |
| 0 | 1130 | | .Append(GetFilterHwDeviceArgs(CudaAlias)); |
| | 1131 | | } |
| 0 | 1132 | | else if (optHwaccelType == HardwareAccelerationType.amf) |
| | 1133 | | { |
| 0 | 1134 | | if (!isWindows || !_mediaEncoder.SupportsHwaccel("d3d11va")) |
| | 1135 | | { |
| 0 | 1136 | | return string.Empty; |
| | 1137 | | } |
| | 1138 | |
|
| 0 | 1139 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 1140 | | var isAmfEncoder = vidEncoder.Contains("amf", StringComparison.OrdinalIgnoreCase); |
| 0 | 1141 | | if (!isD3d11vaDecoder && !isAmfEncoder) |
| | 1142 | | { |
| 0 | 1143 | | return string.Empty; |
| | 1144 | | } |
| | 1145 | |
|
| | 1146 | | // no dxva video processor hw filter. |
| 0 | 1147 | | args.Append(GetD3d11vaDeviceArgs(0, "0x1002", D3d11vaAlias)); |
| 0 | 1148 | | var filterDevArgs = string.Empty; |
| 0 | 1149 | | if (IsOpenclFullSupported()) |
| | 1150 | | { |
| 0 | 1151 | | args.Append(GetOpenclDeviceArgs(0, null, D3d11vaAlias, OpenclAlias)); |
| 0 | 1152 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1153 | | } |
| | 1154 | |
|
| 0 | 1155 | | args.Append(filterDevArgs); |
| | 1156 | | } |
| 0 | 1157 | | else if (optHwaccelType == HardwareAccelerationType.videotoolbox) |
| | 1158 | | { |
| 0 | 1159 | | if (!isMacOS || !_mediaEncoder.SupportsHwaccel("videotoolbox")) |
| | 1160 | | { |
| 0 | 1161 | | return string.Empty; |
| | 1162 | | } |
| | 1163 | |
|
| 0 | 1164 | | var isVideotoolboxDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 1165 | | var isVideotoolboxEncoder = vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 1166 | | if (!isVideotoolboxDecoder && !isVideotoolboxEncoder) |
| | 1167 | | { |
| 0 | 1168 | | return string.Empty; |
| | 1169 | | } |
| | 1170 | |
|
| | 1171 | | // videotoolbox hw filter does not require device selection |
| 0 | 1172 | | args.Append(GetVideoToolboxDeviceArgs(VideotoolboxAlias)); |
| | 1173 | | } |
| 0 | 1174 | | else if (optHwaccelType == HardwareAccelerationType.rkmpp) |
| | 1175 | | { |
| 0 | 1176 | | if (!isLinux || !_mediaEncoder.SupportsHwaccel("rkmpp")) |
| | 1177 | | { |
| 0 | 1178 | | return string.Empty; |
| | 1179 | | } |
| | 1180 | |
|
| 0 | 1181 | | var isRkmppDecoder = vidDecoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 1182 | | var isRkmppEncoder = vidEncoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 1183 | | if (!isRkmppDecoder && !isRkmppEncoder) |
| | 1184 | | { |
| 0 | 1185 | | return string.Empty; |
| | 1186 | | } |
| | 1187 | |
|
| 0 | 1188 | | args.Append(GetRkmppDeviceArgs(RkmppAlias)); |
| | 1189 | |
|
| 0 | 1190 | | var filterDevArgs = string.Empty; |
| 0 | 1191 | | var doOclTonemap = isHwTonemapAvailable && IsOpenclFullSupported(); |
| | 1192 | |
|
| 0 | 1193 | | if (doOclTonemap && !isRkmppDecoder) |
| | 1194 | | { |
| 0 | 1195 | | args.Append(GetOpenclDeviceArgs(0, null, RkmppAlias, OpenclAlias)); |
| 0 | 1196 | | filterDevArgs = GetFilterHwDeviceArgs(OpenclAlias); |
| | 1197 | | } |
| | 1198 | |
|
| 0 | 1199 | | args.Append(filterDevArgs); |
| | 1200 | | } |
| | 1201 | |
|
| 0 | 1202 | | if (!string.IsNullOrEmpty(vidDecoder)) |
| | 1203 | | { |
| 0 | 1204 | | args.Append(vidDecoder); |
| | 1205 | | } |
| | 1206 | |
|
| 0 | 1207 | | return args.ToString().Trim(); |
| | 1208 | | } |
| | 1209 | |
|
| | 1210 | | /// <summary> |
| | 1211 | | /// Gets the input argument. |
| | 1212 | | /// </summary> |
| | 1213 | | /// <param name="state">Encoding state.</param> |
| | 1214 | | /// <param name="options">Encoding options.</param> |
| | 1215 | | /// <param name="segmentContainer">Segment Container.</param> |
| | 1216 | | /// <returns>Input arguments.</returns> |
| | 1217 | | public string GetInputArgument(EncodingJobInfo state, EncodingOptions options, string segmentContainer) |
| | 1218 | | { |
| 0 | 1219 | | var arg = new StringBuilder(); |
| 0 | 1220 | | var inputVidHwaccelArgs = GetInputVideoHwaccelArgs(state, options); |
| | 1221 | |
|
| 0 | 1222 | | if (!string.IsNullOrEmpty(inputVidHwaccelArgs)) |
| | 1223 | | { |
| 0 | 1224 | | arg.Append(inputVidHwaccelArgs); |
| | 1225 | | } |
| | 1226 | |
|
| 0 | 1227 | | var canvasArgs = GetGraphicalSubCanvasSize(state); |
| 0 | 1228 | | if (!string.IsNullOrEmpty(canvasArgs)) |
| | 1229 | | { |
| 0 | 1230 | | arg.Append(canvasArgs); |
| | 1231 | | } |
| | 1232 | |
|
| 0 | 1233 | | if (state.MediaSource.VideoType == VideoType.Dvd || state.MediaSource.VideoType == VideoType.BluRay) |
| | 1234 | | { |
| 0 | 1235 | | var concatFilePath = Path.Join(_configurationManager.CommonApplicationPaths.CachePath, "concat", state.M |
| 0 | 1236 | | if (!File.Exists(concatFilePath)) |
| | 1237 | | { |
| 0 | 1238 | | _mediaEncoder.GenerateConcatConfig(state.MediaSource, concatFilePath); |
| | 1239 | | } |
| | 1240 | |
|
| 0 | 1241 | | arg.Append(" -f concat -safe 0 -i \"") |
| 0 | 1242 | | .Append(concatFilePath) |
| 0 | 1243 | | .Append("\" "); |
| | 1244 | | } |
| | 1245 | | else |
| | 1246 | | { |
| 0 | 1247 | | arg.Append(" -i ") |
| 0 | 1248 | | .Append(_mediaEncoder.GetInputPathArgument(state)); |
| | 1249 | | } |
| | 1250 | |
|
| | 1251 | | // sub2video for external graphical subtitles |
| 0 | 1252 | | if (state.SubtitleStream is not null |
| 0 | 1253 | | && ShouldEncodeSubtitle(state) |
| 0 | 1254 | | && !state.SubtitleStream.IsTextSubtitleStream |
| 0 | 1255 | | && state.SubtitleStream.IsExternal) |
| | 1256 | | { |
| 0 | 1257 | | var subtitlePath = state.SubtitleStream.Path; |
| 0 | 1258 | | var subtitleExtension = Path.GetExtension(subtitlePath.AsSpan()); |
| | 1259 | |
|
| | 1260 | | // dvdsub/vobsub graphical subtitles use .sub+.idx pairs |
| 0 | 1261 | | if (subtitleExtension.Equals(".sub", StringComparison.OrdinalIgnoreCase)) |
| | 1262 | | { |
| 0 | 1263 | | var idxFile = Path.ChangeExtension(subtitlePath, ".idx"); |
| 0 | 1264 | | if (File.Exists(idxFile)) |
| | 1265 | | { |
| 0 | 1266 | | subtitlePath = idxFile; |
| | 1267 | | } |
| | 1268 | | } |
| | 1269 | |
|
| | 1270 | | // Also seek the external subtitles stream. |
| 0 | 1271 | | var seekSubParam = GetFastSeekCommandLineParameter(state, options, segmentContainer); |
| 0 | 1272 | | if (!string.IsNullOrEmpty(seekSubParam)) |
| | 1273 | | { |
| 0 | 1274 | | arg.Append(' ').Append(seekSubParam); |
| | 1275 | | } |
| | 1276 | |
|
| 0 | 1277 | | if (!string.IsNullOrEmpty(canvasArgs)) |
| | 1278 | | { |
| 0 | 1279 | | arg.Append(canvasArgs); |
| | 1280 | | } |
| | 1281 | |
|
| 0 | 1282 | | arg.Append(" -i file:\"").Append(subtitlePath).Append('\"'); |
| | 1283 | | } |
| | 1284 | |
|
| 0 | 1285 | | if (state.AudioStream is not null && state.AudioStream.IsExternal) |
| | 1286 | | { |
| | 1287 | | // Also seek the external audio stream. |
| 0 | 1288 | | var seekAudioParam = GetFastSeekCommandLineParameter(state, options, segmentContainer); |
| 0 | 1289 | | if (!string.IsNullOrEmpty(seekAudioParam)) |
| | 1290 | | { |
| 0 | 1291 | | arg.Append(' ').Append(seekAudioParam); |
| | 1292 | | } |
| | 1293 | |
|
| 0 | 1294 | | arg.Append(" -i \"").Append(state.AudioStream.Path).Append('"'); |
| | 1295 | | } |
| | 1296 | |
|
| | 1297 | | // Disable auto inserted SW scaler for HW decoders in case of changed resolution. |
| 0 | 1298 | | var isSwDecoder = string.IsNullOrEmpty(GetHardwareVideoDecoder(state, options)); |
| 0 | 1299 | | if (!isSwDecoder) |
| | 1300 | | { |
| 0 | 1301 | | arg.Append(" -noautoscale"); |
| | 1302 | | } |
| | 1303 | |
|
| 0 | 1304 | | return arg.ToString(); |
| | 1305 | | } |
| | 1306 | |
|
| | 1307 | | /// <summary> |
| | 1308 | | /// Determines whether the specified stream is H264. |
| | 1309 | | /// </summary> |
| | 1310 | | /// <param name="stream">The stream.</param> |
| | 1311 | | /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns> |
| | 1312 | | public static bool IsH264(MediaStream stream) |
| | 1313 | | { |
| 0 | 1314 | | var codec = stream.Codec ?? string.Empty; |
| | 1315 | |
|
| 0 | 1316 | | return codec.Contains("264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1317 | | || codec.Contains("avc", StringComparison.OrdinalIgnoreCase); |
| | 1318 | | } |
| | 1319 | |
|
| | 1320 | | public static bool IsH265(MediaStream stream) |
| | 1321 | | { |
| 0 | 1322 | | var codec = stream.Codec ?? string.Empty; |
| | 1323 | |
|
| 0 | 1324 | | return codec.Contains("265", StringComparison.OrdinalIgnoreCase) |
| 0 | 1325 | | || codec.Contains("hevc", StringComparison.OrdinalIgnoreCase); |
| | 1326 | | } |
| | 1327 | |
|
| | 1328 | | public static bool IsAv1(MediaStream stream) |
| | 1329 | | { |
| 0 | 1330 | | var codec = stream.Codec ?? string.Empty; |
| | 1331 | |
|
| 0 | 1332 | | return codec.Contains("av1", StringComparison.OrdinalIgnoreCase); |
| | 1333 | | } |
| | 1334 | |
|
| | 1335 | | public static bool IsAAC(MediaStream stream) |
| | 1336 | | { |
| 0 | 1337 | | var codec = stream.Codec ?? string.Empty; |
| | 1338 | |
|
| 0 | 1339 | | return codec.Contains("aac", StringComparison.OrdinalIgnoreCase); |
| | 1340 | | } |
| | 1341 | |
|
| | 1342 | | public static bool IsDoviWithHdr10Bl(MediaStream stream) |
| | 1343 | | { |
| 0 | 1344 | | var rangeType = stream?.VideoRangeType; |
| | 1345 | |
|
| 0 | 1346 | | return rangeType is VideoRangeType.DOVIWithHDR10 |
| 0 | 1347 | | or VideoRangeType.DOVIWithEL |
| 0 | 1348 | | or VideoRangeType.DOVIWithHDR10Plus |
| 0 | 1349 | | or VideoRangeType.DOVIWithELHDR10Plus |
| 0 | 1350 | | or VideoRangeType.DOVIInvalid; |
| | 1351 | | } |
| | 1352 | |
|
| | 1353 | | public static bool IsDovi(MediaStream stream) |
| | 1354 | | { |
| 0 | 1355 | | var rangeType = stream?.VideoRangeType; |
| | 1356 | |
|
| 0 | 1357 | | return IsDoviWithHdr10Bl(stream) |
| 0 | 1358 | | || (rangeType is VideoRangeType.DOVI |
| 0 | 1359 | | or VideoRangeType.DOVIWithHLG |
| 0 | 1360 | | or VideoRangeType.DOVIWithSDR); |
| | 1361 | | } |
| | 1362 | |
|
| | 1363 | | public static bool IsHdr10Plus(MediaStream stream) |
| | 1364 | | { |
| 0 | 1365 | | var rangeType = stream?.VideoRangeType; |
| | 1366 | |
|
| 0 | 1367 | | return rangeType is VideoRangeType.HDR10Plus |
| 0 | 1368 | | or VideoRangeType.DOVIWithHDR10Plus |
| 0 | 1369 | | or VideoRangeType.DOVIWithELHDR10Plus; |
| | 1370 | | } |
| | 1371 | |
|
| | 1372 | | /// <summary> |
| | 1373 | | /// Check if dynamic HDR metadata should be removed during stream copy. |
| | 1374 | | /// Please note this check assumes the range check has already been done |
| | 1375 | | /// and trivial fallbacks like HDR10+ to HDR10, DOVIWithHDR10 to HDR10 is already checked. |
| | 1376 | | /// </summary> |
| | 1377 | | private static DynamicHdrMetadataRemovalPlan ShouldRemoveDynamicHdrMetadata(EncodingJobInfo state) |
| | 1378 | | { |
| 0 | 1379 | | var videoStream = state.VideoStream; |
| 0 | 1380 | | if (videoStream.VideoRange is not VideoRange.HDR) |
| | 1381 | | { |
| 0 | 1382 | | return DynamicHdrMetadataRemovalPlan.None; |
| | 1383 | | } |
| | 1384 | |
|
| 0 | 1385 | | var requestedRangeTypes = state.GetRequestedRangeTypes(state.VideoStream.Codec); |
| 0 | 1386 | | if (requestedRangeTypes.Length == 0) |
| | 1387 | | { |
| 0 | 1388 | | return DynamicHdrMetadataRemovalPlan.None; |
| | 1389 | | } |
| | 1390 | |
|
| 0 | 1391 | | var requestHasHDR10 = requestedRangeTypes.Contains(VideoRangeType.HDR10.ToString(), StringComparison.Ordinal |
| 0 | 1392 | | var requestHasDOVI = requestedRangeTypes.Contains(VideoRangeType.DOVI.ToString(), StringComparison.OrdinalIg |
| 0 | 1393 | | var requestHasDOVIwithEL = requestedRangeTypes.Contains(VideoRangeType.DOVIWithEL.ToString(), StringComparis |
| 0 | 1394 | | var requestHasDOVIwithELHDR10plus = requestedRangeTypes.Contains(VideoRangeType.DOVIWithELHDR10Plus.ToString |
| | 1395 | |
|
| 0 | 1396 | | var shouldRemoveHdr10Plus = false; |
| | 1397 | | // Case 1: Client supports HDR10, does not support DOVI with EL but EL presets |
| 0 | 1398 | | var shouldRemoveDovi = (!requestHasDOVIwithEL && requestHasHDR10) && videoStream.VideoRangeType == VideoRang |
| | 1399 | |
|
| | 1400 | | // Case 2: Client supports DOVI, does not support broken DOVI config |
| | 1401 | | // Client does not report DOVI support should be allowed to copy bad data for remuxing as HDR10 players woul |
| 0 | 1402 | | shouldRemoveDovi = shouldRemoveDovi || (requestHasDOVI && videoStream.VideoRangeType == VideoRangeType.DOVII |
| | 1403 | |
|
| | 1404 | | // Special case: we have a video with both EL and HDR10+ |
| | 1405 | | // If the client supports EL but not in the case of coexistence with HDR10+, remove HDR10+ for compatibility |
| | 1406 | | // Otherwise, remove DOVI if the client is not a DOVI player |
| 0 | 1407 | | if (videoStream.VideoRangeType == VideoRangeType.DOVIWithELHDR10Plus) |
| | 1408 | | { |
| 0 | 1409 | | shouldRemoveHdr10Plus = requestHasDOVIwithEL && !requestHasDOVIwithELHDR10plus; |
| 0 | 1410 | | shouldRemoveDovi = shouldRemoveDovi || !shouldRemoveHdr10Plus; |
| | 1411 | | } |
| | 1412 | |
|
| 0 | 1413 | | if (shouldRemoveDovi) |
| | 1414 | | { |
| 0 | 1415 | | return DynamicHdrMetadataRemovalPlan.RemoveDovi; |
| | 1416 | | } |
| | 1417 | |
|
| | 1418 | | // If the client is a Dolby Vision Player, remove the HDR10+ metadata to avoid playback issues |
| 0 | 1419 | | shouldRemoveHdr10Plus = shouldRemoveHdr10Plus || (requestHasDOVI && videoStream.VideoRangeType == VideoRange |
| 0 | 1420 | | return shouldRemoveHdr10Plus ? DynamicHdrMetadataRemovalPlan.RemoveHdr10Plus : DynamicHdrMetadataRemovalPlan |
| | 1421 | | } |
| | 1422 | |
|
| | 1423 | | private bool CanEncoderRemoveDynamicHdrMetadata(DynamicHdrMetadataRemovalPlan plan, MediaStream videoStream) |
| | 1424 | | { |
| 0 | 1425 | | return plan switch |
| 0 | 1426 | | { |
| 0 | 1427 | | DynamicHdrMetadataRemovalPlan.RemoveDovi => _mediaEncoder.SupportsBitStreamFilterWithOption(BitStreamFil |
| 0 | 1428 | | || (IsH265(videoStream) && _mediaEncoder.SupportsBitStreamFi |
| 0 | 1429 | | || (IsAv1(videoStream) && _mediaEncoder.SupportsBitStreamFil |
| 0 | 1430 | | DynamicHdrMetadataRemovalPlan.RemoveHdr10Plus => (IsH265(videoStream) && _mediaEncoder.SupportsBitStream |
| 0 | 1431 | | || (IsAv1(videoStream) && _mediaEncoder.SupportsBitStre |
| 0 | 1432 | | _ => true, |
| 0 | 1433 | | }; |
| | 1434 | | } |
| | 1435 | |
|
| | 1436 | | public bool IsDoviRemoved(EncodingJobInfo state) |
| | 1437 | | { |
| 0 | 1438 | | return state?.VideoStream is not null && ShouldRemoveDynamicHdrMetadata(state) == DynamicHdrMetadataRemovalP |
| 0 | 1439 | | && CanEncoderRemoveDynamicHdrMetadata(DynamicHdrMetadataRemovalPlan.Remove |
| | 1440 | | } |
| | 1441 | |
|
| | 1442 | | public bool IsHdr10PlusRemoved(EncodingJobInfo state) |
| | 1443 | | { |
| 0 | 1444 | | return state?.VideoStream is not null && ShouldRemoveDynamicHdrMetadata(state) == DynamicHdrMetadataRemovalP |
| 0 | 1445 | | && CanEncoderRemoveDynamicHdrMetadata(DynamicHdrMetadataRemovalPlan.Re |
| | 1446 | | } |
| | 1447 | |
|
| | 1448 | | public string GetBitStreamArgs(EncodingJobInfo state, MediaStreamType streamType) |
| | 1449 | | { |
| 0 | 1450 | | if (state is null) |
| | 1451 | | { |
| 0 | 1452 | | return null; |
| | 1453 | | } |
| | 1454 | |
|
| 0 | 1455 | | var stream = streamType switch |
| 0 | 1456 | | { |
| 0 | 1457 | | MediaStreamType.Audio => state.AudioStream, |
| 0 | 1458 | | MediaStreamType.Video => state.VideoStream, |
| 0 | 1459 | | _ => state.VideoStream |
| 0 | 1460 | | }; |
| | 1461 | | // TODO This is auto inserted into the mpegts mux so it might not be needed. |
| | 1462 | | // https://www.ffmpeg.org/ffmpeg-bitstream-filters.html#h264_005fmp4toannexb |
| 0 | 1463 | | if (IsH264(stream)) |
| | 1464 | | { |
| 0 | 1465 | | return "-bsf:v h264_mp4toannexb"; |
| | 1466 | | } |
| | 1467 | |
|
| 0 | 1468 | | if (IsAAC(stream)) |
| | 1469 | | { |
| | 1470 | | // Convert adts header(mpegts) to asc header(mp4). |
| 0 | 1471 | | return "-bsf:a aac_adtstoasc"; |
| | 1472 | | } |
| | 1473 | |
|
| 0 | 1474 | | if (IsH265(stream)) |
| | 1475 | | { |
| 0 | 1476 | | var filter = "-bsf:v hevc_mp4toannexb"; |
| | 1477 | |
|
| | 1478 | | // The following checks are not complete because the copy would be rejected |
| | 1479 | | // if the encoder cannot remove required metadata. |
| | 1480 | | // And if bsf is used, we must already be using copy codec. |
| 0 | 1481 | | switch (ShouldRemoveDynamicHdrMetadata(state)) |
| | 1482 | | { |
| | 1483 | | default: |
| | 1484 | | case DynamicHdrMetadataRemovalPlan.None: |
| | 1485 | | break; |
| | 1486 | | case DynamicHdrMetadataRemovalPlan.RemoveDovi: |
| 0 | 1487 | | filter += _mediaEncoder.SupportsBitStreamFilterWithOption(BitStreamFilterOptionType.HevcMetadata |
| 0 | 1488 | | ? ",hevc_metadata=remove_dovi=1" |
| 0 | 1489 | | : ",dovi_rpu=strip=1"; |
| 0 | 1490 | | break; |
| | 1491 | | case DynamicHdrMetadataRemovalPlan.RemoveHdr10Plus: |
| 0 | 1492 | | filter += ",hevc_metadata=remove_hdr10plus=1"; |
| | 1493 | | break; |
| | 1494 | | } |
| | 1495 | |
|
| 0 | 1496 | | return filter; |
| | 1497 | | } |
| | 1498 | |
|
| 0 | 1499 | | if (IsAv1(stream)) |
| | 1500 | | { |
| 0 | 1501 | | switch (ShouldRemoveDynamicHdrMetadata(state)) |
| | 1502 | | { |
| | 1503 | | default: |
| | 1504 | | case DynamicHdrMetadataRemovalPlan.None: |
| 0 | 1505 | | return null; |
| | 1506 | | case DynamicHdrMetadataRemovalPlan.RemoveDovi: |
| 0 | 1507 | | return _mediaEncoder.SupportsBitStreamFilterWithOption(BitStreamFilterOptionType.Av1MetadataRemo |
| 0 | 1508 | | ? "-bsf:v av1_metadata=remove_dovi=1" |
| 0 | 1509 | | : "-bsf:v dovi_rpu=strip=1"; |
| | 1510 | | case DynamicHdrMetadataRemovalPlan.RemoveHdr10Plus: |
| 0 | 1511 | | return "-bsf:v av1_metadata=remove_hdr10plus=1"; |
| | 1512 | | } |
| | 1513 | | } |
| | 1514 | |
|
| 0 | 1515 | | return null; |
| | 1516 | | } |
| | 1517 | |
|
| | 1518 | | public string GetAudioBitStreamArguments(EncodingJobInfo state, string segmentContainer, string mediaSourceConta |
| | 1519 | | { |
| 0 | 1520 | | var bitStreamArgs = string.Empty; |
| 0 | 1521 | | var segmentFormat = GetSegmentFileExtension(segmentContainer).TrimStart('.'); |
| | 1522 | |
|
| | 1523 | | // Apply aac_adtstoasc bitstream filter when media source is in mpegts. |
| 0 | 1524 | | if (string.Equals(segmentFormat, "mp4", StringComparison.OrdinalIgnoreCase) |
| 0 | 1525 | | && (string.Equals(mediaSourceContainer, "ts", StringComparison.OrdinalIgnoreCase) |
| 0 | 1526 | | || string.Equals(mediaSourceContainer, "aac", StringComparison.OrdinalIgnoreCase) |
| 0 | 1527 | | || string.Equals(mediaSourceContainer, "hls", StringComparison.OrdinalIgnoreCase))) |
| | 1528 | | { |
| 0 | 1529 | | bitStreamArgs = GetBitStreamArgs(state, MediaStreamType.Audio); |
| 0 | 1530 | | bitStreamArgs = string.IsNullOrEmpty(bitStreamArgs) ? string.Empty : " " + bitStreamArgs; |
| | 1531 | | } |
| | 1532 | |
|
| 0 | 1533 | | return bitStreamArgs; |
| | 1534 | | } |
| | 1535 | |
|
| | 1536 | | public static string GetSegmentFileExtension(string segmentContainer) |
| | 1537 | | { |
| 0 | 1538 | | if (!string.IsNullOrWhiteSpace(segmentContainer)) |
| | 1539 | | { |
| 0 | 1540 | | return "." + segmentContainer; |
| | 1541 | | } |
| | 1542 | |
|
| 0 | 1543 | | return ".ts"; |
| | 1544 | | } |
| | 1545 | |
|
| | 1546 | | private string GetVideoBitrateParam(EncodingJobInfo state, string videoCodec) |
| | 1547 | | { |
| 0 | 1548 | | if (state.OutputVideoBitrate is null) |
| | 1549 | | { |
| 0 | 1550 | | return string.Empty; |
| | 1551 | | } |
| | 1552 | |
|
| 0 | 1553 | | int bitrate = state.OutputVideoBitrate.Value; |
| | 1554 | |
|
| | 1555 | | // Bit rate under 1000k is not allowed in h264_qsv |
| 0 | 1556 | | if (string.Equals(videoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase)) |
| | 1557 | | { |
| 0 | 1558 | | bitrate = Math.Max(bitrate, 1000); |
| | 1559 | | } |
| | 1560 | |
|
| | 1561 | | // Currently use the same buffer size for all encoders |
| 0 | 1562 | | int bufsize = bitrate * 2; |
| | 1563 | |
|
| 0 | 1564 | | if (string.Equals(videoCodec, "libsvtav1", StringComparison.OrdinalIgnoreCase)) |
| | 1565 | | { |
| 0 | 1566 | | return FormattableString.Invariant($" -b:v {bitrate} -bufsize {bufsize}"); |
| | 1567 | | } |
| | 1568 | |
|
| 0 | 1569 | | if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1570 | | || string.Equals(videoCodec, "libx265", StringComparison.OrdinalIgnoreCase)) |
| | 1571 | | { |
| 0 | 1572 | | return FormattableString.Invariant($" -maxrate {bitrate} -bufsize {bufsize}"); |
| | 1573 | | } |
| | 1574 | |
|
| 0 | 1575 | | if (string.Equals(videoCodec, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1576 | | || string.Equals(videoCodec, "hevc_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1577 | | || string.Equals(videoCodec, "av1_amf", StringComparison.OrdinalIgnoreCase)) |
| | 1578 | | { |
| | 1579 | | // Override the too high default qmin 18 in transcoding preset |
| 0 | 1580 | | return FormattableString.Invariant($" -rc cbr -qmin 0 -qmax 32 -b:v {bitrate} -maxrate {bitrate} -bufsiz |
| | 1581 | | } |
| | 1582 | |
|
| 0 | 1583 | | if (string.Equals(videoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1584 | | || string.Equals(videoCodec, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1585 | | || string.Equals(videoCodec, "av1_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1586 | | { |
| | 1587 | | // VBR in i965 driver may result in pixelated output. |
| 0 | 1588 | | if (_mediaEncoder.IsVaapiDeviceInteli965) |
| | 1589 | | { |
| 0 | 1590 | | return FormattableString.Invariant($" -rc_mode CBR -b:v {bitrate} -maxrate {bitrate} -bufsize {bufsi |
| | 1591 | | } |
| | 1592 | |
|
| 0 | 1593 | | return FormattableString.Invariant($" -rc_mode VBR -b:v {bitrate} -maxrate {bitrate} -bufsize {bufsize}" |
| | 1594 | | } |
| | 1595 | |
|
| 0 | 1596 | | if (string.Equals(videoCodec, "h264_videotoolbox", StringComparison.OrdinalIgnoreCase) |
| 0 | 1597 | | || string.Equals(videoCodec, "hevc_videotoolbox", StringComparison.OrdinalIgnoreCase)) |
| | 1598 | | { |
| | 1599 | | // The `maxrate` and `bufsize` options can potentially lead to performance regression |
| | 1600 | | // and even encoder hangs, especially when the value is very high. |
| 0 | 1601 | | return FormattableString.Invariant($" -b:v {bitrate} -qmin -1 -qmax -1"); |
| | 1602 | | } |
| | 1603 | |
|
| 0 | 1604 | | return FormattableString.Invariant($" -b:v {bitrate} -maxrate {bitrate} -bufsize {bufsize}"); |
| | 1605 | | } |
| | 1606 | |
|
| | 1607 | | private string GetEncoderParam(EncoderPreset? preset, EncoderPreset defaultPreset, EncodingOptions encodingOptio |
| | 1608 | | { |
| 0 | 1609 | | var param = string.Empty; |
| 0 | 1610 | | var encoderPreset = preset ?? defaultPreset; |
| 0 | 1611 | | if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) || isLibX265) |
| | 1612 | | { |
| 0 | 1613 | | var presetString = encoderPreset switch |
| 0 | 1614 | | { |
| 0 | 1615 | | EncoderPreset.auto => EncoderPreset.veryfast.ToString().ToLowerInvariant(), |
| 0 | 1616 | | _ => encoderPreset.ToString().ToLowerInvariant() |
| 0 | 1617 | | }; |
| | 1618 | |
|
| 0 | 1619 | | param += " -preset " + presetString; |
| | 1620 | |
|
| 0 | 1621 | | int encodeCrf = encodingOptions.H264Crf; |
| 0 | 1622 | | if (isLibX265) |
| | 1623 | | { |
| 0 | 1624 | | encodeCrf = encodingOptions.H265Crf; |
| | 1625 | | } |
| | 1626 | |
|
| 0 | 1627 | | if (encodeCrf >= 0 && encodeCrf <= 51) |
| | 1628 | | { |
| 0 | 1629 | | param += " -crf " + encodeCrf.ToString(CultureInfo.InvariantCulture); |
| | 1630 | | } |
| | 1631 | | else |
| | 1632 | | { |
| 0 | 1633 | | string defaultCrf = "23"; |
| 0 | 1634 | | if (isLibX265) |
| | 1635 | | { |
| 0 | 1636 | | defaultCrf = "28"; |
| | 1637 | | } |
| | 1638 | |
|
| 0 | 1639 | | param += " -crf " + defaultCrf; |
| | 1640 | | } |
| | 1641 | | } |
| 0 | 1642 | | else if (string.Equals(videoEncoder, "libsvtav1", StringComparison.OrdinalIgnoreCase)) |
| | 1643 | | { |
| | 1644 | | // Default to use the recommended preset 10. |
| | 1645 | | // Omit presets < 5, which are too slow for on the fly encoding. |
| | 1646 | | // https://gitlab.com/AOMediaCodec/SVT-AV1/-/blob/master/Docs/Ffmpeg.md |
| 0 | 1647 | | param += encoderPreset switch |
| 0 | 1648 | | { |
| 0 | 1649 | | EncoderPreset.veryslow => " -preset 5", |
| 0 | 1650 | | EncoderPreset.slower => " -preset 6", |
| 0 | 1651 | | EncoderPreset.slow => " -preset 7", |
| 0 | 1652 | | EncoderPreset.medium => " -preset 8", |
| 0 | 1653 | | EncoderPreset.fast => " -preset 9", |
| 0 | 1654 | | EncoderPreset.faster => " -preset 10", |
| 0 | 1655 | | EncoderPreset.veryfast => " -preset 11", |
| 0 | 1656 | | EncoderPreset.superfast => " -preset 12", |
| 0 | 1657 | | EncoderPreset.ultrafast => " -preset 13", |
| 0 | 1658 | | _ => " -preset 10" |
| 0 | 1659 | | }; |
| | 1660 | | } |
| 0 | 1661 | | else if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1662 | | || string.Equals(videoEncoder, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1663 | | || string.Equals(videoEncoder, "av1_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1664 | | { |
| | 1665 | | // -compression_level is not reliable on AMD. |
| 0 | 1666 | | if (_mediaEncoder.IsVaapiDeviceInteliHD) |
| | 1667 | | { |
| 0 | 1668 | | param += encoderPreset switch |
| 0 | 1669 | | { |
| 0 | 1670 | | EncoderPreset.veryslow => " -compression_level 1", |
| 0 | 1671 | | EncoderPreset.slower => " -compression_level 2", |
| 0 | 1672 | | EncoderPreset.slow => " -compression_level 3", |
| 0 | 1673 | | EncoderPreset.medium => " -compression_level 4", |
| 0 | 1674 | | EncoderPreset.fast => " -compression_level 5", |
| 0 | 1675 | | EncoderPreset.faster => " -compression_level 6", |
| 0 | 1676 | | EncoderPreset.veryfast => " -compression_level 7", |
| 0 | 1677 | | EncoderPreset.superfast => " -compression_level 7", |
| 0 | 1678 | | EncoderPreset.ultrafast => " -compression_level 7", |
| 0 | 1679 | | _ => string.Empty |
| 0 | 1680 | | }; |
| | 1681 | | } |
| | 1682 | | } |
| 0 | 1683 | | else if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) // h264 (h264_qsv) |
| 0 | 1684 | | || string.Equals(videoEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase) // hevc (hevc_qsv) |
| 0 | 1685 | | || string.Equals(videoEncoder, "av1_qsv", StringComparison.OrdinalIgnoreCase)) // av1 (av1_qsv) |
| | 1686 | | { |
| 0 | 1687 | | EncoderPreset[] valid_presets = [EncoderPreset.veryslow, EncoderPreset.slower, EncoderPreset.slow, Encod |
| | 1688 | |
|
| 0 | 1689 | | param += " -preset " + (valid_presets.Contains(encoderPreset) ? encoderPreset : EncoderPreset.veryfast). |
| | 1690 | | } |
| 0 | 1691 | | else if (string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) // h264 (h264_nvenc) |
| 0 | 1692 | | || string.Equals(videoEncoder, "hevc_nvenc", StringComparison.OrdinalIgnoreCase) // hevc (hevc_n |
| 0 | 1693 | | || string.Equals(videoEncoder, "av1_nvenc", StringComparison.OrdinalIgnoreCase) // av1 (av1_nven |
| 0 | 1694 | | ) |
| | 1695 | | { |
| 0 | 1696 | | param += encoderPreset switch |
| 0 | 1697 | | { |
| 0 | 1698 | | EncoderPreset.veryslow => " -preset p7", |
| 0 | 1699 | | EncoderPreset.slower => " -preset p6", |
| 0 | 1700 | | EncoderPreset.slow => " -preset p5", |
| 0 | 1701 | | EncoderPreset.medium => " -preset p4", |
| 0 | 1702 | | EncoderPreset.fast => " -preset p3", |
| 0 | 1703 | | EncoderPreset.faster => " -preset p2", |
| 0 | 1704 | | _ => " -preset p1" |
| 0 | 1705 | | }; |
| | 1706 | | } |
| 0 | 1707 | | else if (string.Equals(videoEncoder, "h264_amf", StringComparison.OrdinalIgnoreCase) // h264 (h264_amf) |
| 0 | 1708 | | || string.Equals(videoEncoder, "hevc_amf", StringComparison.OrdinalIgnoreCase) // hevc (hevc_amf |
| 0 | 1709 | | || string.Equals(videoEncoder, "av1_amf", StringComparison.OrdinalIgnoreCase) // av1 (av1_amf) |
| 0 | 1710 | | ) |
| | 1711 | | { |
| 0 | 1712 | | param += encoderPreset switch |
| 0 | 1713 | | { |
| 0 | 1714 | | EncoderPreset.veryslow => " -quality quality", |
| 0 | 1715 | | EncoderPreset.slower => " -quality quality", |
| 0 | 1716 | | EncoderPreset.slow => " -quality quality", |
| 0 | 1717 | | EncoderPreset.medium => " -quality balanced", |
| 0 | 1718 | | _ => " -quality speed" |
| 0 | 1719 | | }; |
| | 1720 | |
|
| 0 | 1721 | | if (string.Equals(videoEncoder, "hevc_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1722 | | || string.Equals(videoEncoder, "av1_amf", StringComparison.OrdinalIgnoreCase)) |
| | 1723 | | { |
| 0 | 1724 | | param += " -header_insertion_mode gop"; |
| | 1725 | | } |
| | 1726 | |
|
| 0 | 1727 | | if (string.Equals(videoEncoder, "hevc_amf", StringComparison.OrdinalIgnoreCase)) |
| | 1728 | | { |
| 0 | 1729 | | param += " -gops_per_idr 1"; |
| | 1730 | | } |
| | 1731 | | } |
| 0 | 1732 | | else if (string.Equals(videoEncoder, "h264_videotoolbox", StringComparison.OrdinalIgnoreCase) // h264 (h264_ |
| 0 | 1733 | | || string.Equals(videoEncoder, "hevc_videotoolbox", StringComparison.OrdinalIgnoreCase) // hevc |
| 0 | 1734 | | ) |
| | 1735 | | { |
| 0 | 1736 | | param += encoderPreset switch |
| 0 | 1737 | | { |
| 0 | 1738 | | EncoderPreset.veryslow => " -prio_speed 0", |
| 0 | 1739 | | EncoderPreset.slower => " -prio_speed 0", |
| 0 | 1740 | | EncoderPreset.slow => " -prio_speed 0", |
| 0 | 1741 | | EncoderPreset.medium => " -prio_speed 0", |
| 0 | 1742 | | _ => " -prio_speed 1" |
| 0 | 1743 | | }; |
| | 1744 | | } |
| | 1745 | |
|
| 0 | 1746 | | return param; |
| | 1747 | | } |
| | 1748 | |
|
| | 1749 | | public static string NormalizeTranscodingLevel(EncodingJobInfo state, string level) |
| | 1750 | | { |
| 0 | 1751 | | if (double.TryParse(level, CultureInfo.InvariantCulture, out double requestLevel)) |
| | 1752 | | { |
| 0 | 1753 | | if (string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 1754 | | { |
| | 1755 | | // Transcode to level 5.3 (15) and lower for maximum compatibility. |
| | 1756 | | // https://en.wikipedia.org/wiki/AV1#Levels |
| 0 | 1757 | | if (requestLevel < 0 || requestLevel >= 15) |
| | 1758 | | { |
| 0 | 1759 | | return "15"; |
| | 1760 | | } |
| | 1761 | | } |
| 0 | 1762 | | else if (string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1763 | | || string.Equals(state.ActualOutputVideoCodec, "h265", StringComparison.OrdinalIgnoreCase)) |
| | 1764 | | { |
| | 1765 | | // Transcode to level 5.0 and lower for maximum compatibility. |
| | 1766 | | // Level 5.0 is suitable for up to 4k 30fps hevc encoding, otherwise let the encoder to handle it. |
| | 1767 | | // https://en.wikipedia.org/wiki/High_Efficiency_Video_Coding_tiers_and_levels |
| | 1768 | | // MaxLumaSampleRate = 3840*2160*30 = 248832000 < 267386880. |
| 0 | 1769 | | if (requestLevel < 0 || requestLevel >= 150) |
| | 1770 | | { |
| 0 | 1771 | | return "150"; |
| | 1772 | | } |
| | 1773 | | } |
| 0 | 1774 | | else if (string.Equals(state.ActualOutputVideoCodec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 1775 | | { |
| | 1776 | | // Transcode to level 5.1 and lower for maximum compatibility. |
| | 1777 | | // h264 4k 30fps requires at least level 5.1 otherwise it will break on safari fmp4. |
| | 1778 | | // https://en.wikipedia.org/wiki/Advanced_Video_Coding#Levels |
| 0 | 1779 | | if (requestLevel < 0 || requestLevel >= 51) |
| | 1780 | | { |
| 0 | 1781 | | return "51"; |
| | 1782 | | } |
| | 1783 | | } |
| | 1784 | | } |
| | 1785 | |
|
| 0 | 1786 | | return level; |
| | 1787 | | } |
| | 1788 | |
|
| | 1789 | | /// <summary> |
| | 1790 | | /// Gets the text subtitle param. |
| | 1791 | | /// </summary> |
| | 1792 | | /// <param name="state">The state.</param> |
| | 1793 | | /// <param name="enableAlpha">Enable alpha processing.</param> |
| | 1794 | | /// <param name="enableSub2video">Enable sub2video mode.</param> |
| | 1795 | | /// <returns>System.String.</returns> |
| | 1796 | | public string GetTextSubtitlesFilter(EncodingJobInfo state, bool enableAlpha, bool enableSub2video) |
| | 1797 | | { |
| 0 | 1798 | | var seconds = Math.Round(TimeSpan.FromTicks(state.StartTimeTicks ?? 0).TotalSeconds); |
| | 1799 | |
|
| | 1800 | | // hls always copies timestamps |
| 0 | 1801 | | var setPtsParam = state.CopyTimestamps || state.TranscodingType != TranscodingJobType.Progressive |
| 0 | 1802 | | ? string.Empty |
| 0 | 1803 | | : string.Format(CultureInfo.InvariantCulture, ",setpts=PTS -{0}/TB", seconds); |
| | 1804 | |
|
| 0 | 1805 | | var alphaParam = enableAlpha ? ":alpha=1" : string.Empty; |
| 0 | 1806 | | var sub2videoParam = enableSub2video ? ":sub2video=1" : string.Empty; |
| | 1807 | |
|
| 0 | 1808 | | var fontPath = _pathManager.GetAttachmentFolderPath(state.MediaSource.Id); |
| 0 | 1809 | | var fontParam = string.Format( |
| 0 | 1810 | | CultureInfo.InvariantCulture, |
| 0 | 1811 | | ":fontsdir='{0}'", |
| 0 | 1812 | | _mediaEncoder.EscapeSubtitleFilterPath(fontPath)); |
| | 1813 | |
|
| 0 | 1814 | | if (state.SubtitleStream.IsExternal) |
| | 1815 | | { |
| 0 | 1816 | | var charsetParam = string.Empty; |
| | 1817 | |
|
| 0 | 1818 | | if (!string.IsNullOrEmpty(state.SubtitleStream.Language)) |
| | 1819 | | { |
| 0 | 1820 | | var charenc = _subtitleEncoder.GetSubtitleFileCharacterSet( |
| 0 | 1821 | | state.SubtitleStream, |
| 0 | 1822 | | state.SubtitleStream.Language, |
| 0 | 1823 | | state.MediaSource, |
| 0 | 1824 | | CancellationToken.None).GetAwaiter().GetResult(); |
| | 1825 | |
|
| 0 | 1826 | | if (!string.IsNullOrEmpty(charenc)) |
| | 1827 | | { |
| 0 | 1828 | | charsetParam = ":charenc=" + charenc; |
| | 1829 | | } |
| | 1830 | | } |
| | 1831 | |
|
| 0 | 1832 | | return string.Format( |
| 0 | 1833 | | CultureInfo.InvariantCulture, |
| 0 | 1834 | | "subtitles=f='{0}'{1}{2}{3}{4}{5}", |
| 0 | 1835 | | _mediaEncoder.EscapeSubtitleFilterPath(state.SubtitleStream.Path), |
| 0 | 1836 | | charsetParam, |
| 0 | 1837 | | alphaParam, |
| 0 | 1838 | | sub2videoParam, |
| 0 | 1839 | | fontParam, |
| 0 | 1840 | | setPtsParam); |
| | 1841 | | } |
| | 1842 | |
|
| 0 | 1843 | | var subtitlePath = _subtitleEncoder.GetSubtitleFilePath( |
| 0 | 1844 | | state.SubtitleStream, |
| 0 | 1845 | | state.MediaSource, |
| 0 | 1846 | | CancellationToken.None).GetAwaiter().GetResult(); |
| | 1847 | |
|
| 0 | 1848 | | return string.Format( |
| 0 | 1849 | | CultureInfo.InvariantCulture, |
| 0 | 1850 | | "subtitles=f='{0}'{1}{2}{3}{4}", |
| 0 | 1851 | | _mediaEncoder.EscapeSubtitleFilterPath(subtitlePath), |
| 0 | 1852 | | alphaParam, |
| 0 | 1853 | | sub2videoParam, |
| 0 | 1854 | | fontParam, |
| 0 | 1855 | | setPtsParam); |
| | 1856 | | } |
| | 1857 | |
|
| | 1858 | | public double? GetFramerateParam(EncodingJobInfo state) |
| | 1859 | | { |
| 0 | 1860 | | var request = state.BaseRequest; |
| | 1861 | |
|
| 0 | 1862 | | if (request.Framerate.HasValue) |
| | 1863 | | { |
| 0 | 1864 | | return request.Framerate.Value; |
| | 1865 | | } |
| | 1866 | |
|
| 0 | 1867 | | var maxrate = request.MaxFramerate; |
| | 1868 | |
|
| 0 | 1869 | | if (maxrate.HasValue && state.VideoStream is not null) |
| | 1870 | | { |
| 0 | 1871 | | var contentRate = state.VideoStream.ReferenceFrameRate; |
| | 1872 | |
|
| 0 | 1873 | | if (contentRate.HasValue && contentRate.Value > maxrate.Value) |
| | 1874 | | { |
| 0 | 1875 | | return maxrate; |
| | 1876 | | } |
| | 1877 | | } |
| | 1878 | |
|
| 0 | 1879 | | return null; |
| | 1880 | | } |
| | 1881 | |
|
| | 1882 | | public string GetHlsVideoKeyFrameArguments( |
| | 1883 | | EncodingJobInfo state, |
| | 1884 | | string codec, |
| | 1885 | | int segmentLength, |
| | 1886 | | bool isEventPlaylist, |
| | 1887 | | int? startNumber) |
| | 1888 | | { |
| 0 | 1889 | | var args = string.Empty; |
| 0 | 1890 | | var gopArg = string.Empty; |
| | 1891 | |
|
| 0 | 1892 | | var keyFrameArg = string.Format( |
| 0 | 1893 | | CultureInfo.InvariantCulture, |
| 0 | 1894 | | " -force_key_frames:0 \"expr:gte(t,n_forced*{0})\"", |
| 0 | 1895 | | segmentLength); |
| | 1896 | |
|
| 0 | 1897 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 1898 | | if (framerate.HasValue) |
| | 1899 | | { |
| | 1900 | | // This is to make sure keyframe interval is limited to our segment, |
| | 1901 | | // as forcing keyframes is not enough. |
| | 1902 | | // Example: we encoded half of desired length, then codec detected |
| | 1903 | | // scene cut and inserted a keyframe; next forced keyframe would |
| | 1904 | | // be created outside of segment, which breaks seeking. |
| 0 | 1905 | | gopArg = string.Format( |
| 0 | 1906 | | CultureInfo.InvariantCulture, |
| 0 | 1907 | | " -g:v:0 {0} -keyint_min:v:0 {0}", |
| 0 | 1908 | | Math.Ceiling(segmentLength * framerate.Value)); |
| | 1909 | | } |
| | 1910 | |
|
| | 1911 | | // Unable to force key frames using these encoders, set key frames by GOP. |
| 0 | 1912 | | if (string.Equals(codec, "h264_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1913 | | || string.Equals(codec, "h264_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1914 | | || string.Equals(codec, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1915 | | || string.Equals(codec, "h264_rkmpp", StringComparison.OrdinalIgnoreCase) |
| 0 | 1916 | | || string.Equals(codec, "hevc_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1917 | | || string.Equals(codec, "hevc_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1918 | | || string.Equals(codec, "hevc_rkmpp", StringComparison.OrdinalIgnoreCase) |
| 0 | 1919 | | || string.Equals(codec, "av1_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 1920 | | || string.Equals(codec, "av1_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 1921 | | || string.Equals(codec, "av1_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 1922 | | || string.Equals(codec, "libsvtav1", StringComparison.OrdinalIgnoreCase)) |
| | 1923 | | { |
| 0 | 1924 | | args += gopArg; |
| | 1925 | | } |
| 0 | 1926 | | else if (string.Equals(codec, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 1927 | | || string.Equals(codec, "libx265", StringComparison.OrdinalIgnoreCase) |
| 0 | 1928 | | || string.Equals(codec, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1929 | | || string.Equals(codec, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1930 | | || string.Equals(codec, "av1_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1931 | | { |
| 0 | 1932 | | args += keyFrameArg; |
| | 1933 | |
|
| | 1934 | | // prevent the libx264 from post processing to break the set keyframe. |
| 0 | 1935 | | if (string.Equals(codec, "libx264", StringComparison.OrdinalIgnoreCase)) |
| | 1936 | | { |
| 0 | 1937 | | args += " -sc_threshold:v:0 0"; |
| | 1938 | | } |
| | 1939 | | } |
| | 1940 | | else |
| | 1941 | | { |
| 0 | 1942 | | args += keyFrameArg + gopArg; |
| | 1943 | | } |
| | 1944 | |
|
| | 1945 | | // global_header produced by AMD HEVC VA-API encoder causes non-playable fMP4 on iOS |
| 0 | 1946 | | if (string.Equals(codec, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 1947 | | && _mediaEncoder.IsVaapiDeviceAmd) |
| | 1948 | | { |
| 0 | 1949 | | args += " -flags:v -global_header"; |
| | 1950 | | } |
| | 1951 | |
|
| 0 | 1952 | | return args; |
| | 1953 | | } |
| | 1954 | |
|
| | 1955 | | /// <summary> |
| | 1956 | | /// Gets the video bitrate to specify on the command line. |
| | 1957 | | /// </summary> |
| | 1958 | | /// <param name="state">Encoding state.</param> |
| | 1959 | | /// <param name="videoEncoder">Video encoder to use.</param> |
| | 1960 | | /// <param name="encodingOptions">Encoding options.</param> |
| | 1961 | | /// <param name="defaultPreset">Default present to use for encoding.</param> |
| | 1962 | | /// <returns>Video bitrate.</returns> |
| | 1963 | | public string GetVideoQualityParam(EncodingJobInfo state, string videoEncoder, EncodingOptions encodingOptions, |
| | 1964 | | { |
| 0 | 1965 | | var param = string.Empty; |
| | 1966 | |
|
| | 1967 | | // Tutorials: Enable Intel GuC / HuC firmware loading for Low Power Encoding. |
| | 1968 | | // https://01.org/group/43/downloads/firmware |
| | 1969 | | // https://wiki.archlinux.org/title/intel_graphics#Enable_GuC_/_HuC_firmware_loading |
| | 1970 | | // Intel Low Power Encoding can save unnecessary CPU-GPU synchronization, |
| | 1971 | | // which will reduce overhead in performance intensive tasks such as 4k transcoding and tonemapping. |
| 0 | 1972 | | var intelLowPowerHwEncoding = false; |
| | 1973 | |
|
| | 1974 | | // Workaround for linux 5.18 to 6.1.3 i915 hang at cost of performance. |
| | 1975 | | // https://github.com/intel/media-driver/issues/1456 |
| 0 | 1976 | | var enableWaFori915Hang = false; |
| | 1977 | |
|
| 0 | 1978 | | var hardwareAccelerationType = encodingOptions.HardwareAccelerationType; |
| | 1979 | |
|
| 0 | 1980 | | if (hardwareAccelerationType == HardwareAccelerationType.vaapi) |
| | 1981 | | { |
| 0 | 1982 | | var isIntelVaapiDriver = _mediaEncoder.IsVaapiDeviceInteliHD || _mediaEncoder.IsVaapiDeviceInteli965; |
| | 1983 | |
|
| 0 | 1984 | | if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1985 | | { |
| 0 | 1986 | | intelLowPowerHwEncoding = encodingOptions.EnableIntelLowPowerH264HwEncoder && isIntelVaapiDriver; |
| | 1987 | | } |
| 0 | 1988 | | else if (string.Equals(videoEncoder, "hevc_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 1989 | | { |
| 0 | 1990 | | intelLowPowerHwEncoding = encodingOptions.EnableIntelLowPowerHevcHwEncoder && isIntelVaapiDriver; |
| | 1991 | | } |
| | 1992 | | } |
| 0 | 1993 | | else if (hardwareAccelerationType == HardwareAccelerationType.qsv) |
| | 1994 | | { |
| 0 | 1995 | | if (OperatingSystem.IsLinux()) |
| | 1996 | | { |
| 0 | 1997 | | var ver = Environment.OSVersion.Version; |
| 0 | 1998 | | var isFixedKernel60 = ver.Major == 6 && ver.Minor == 0 && ver >= _minFixedKernel60i915Hang; |
| 0 | 1999 | | var isUnaffectedKernel = ver < _minKerneli915Hang || ver > _maxKerneli915Hang; |
| | 2000 | |
|
| 0 | 2001 | | if (!(isUnaffectedKernel || isFixedKernel60)) |
| | 2002 | | { |
| 0 | 2003 | | var vidDecoder = GetHardwareVideoDecoder(state, encodingOptions) ?? string.Empty; |
| 0 | 2004 | | var isIntelDecoder = vidDecoder.Contains("qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 2005 | | || vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 2006 | | var doOclTonemap = _mediaEncoder.SupportsHwaccel("qsv") |
| 0 | 2007 | | && IsVaapiSupported(state) |
| 0 | 2008 | | && IsOpenclFullSupported() |
| 0 | 2009 | | && !IsIntelVppTonemapAvailable(state, encodingOptions) |
| 0 | 2010 | | && IsHwTonemapAvailable(state, encodingOptions); |
| | 2011 | |
|
| 0 | 2012 | | enableWaFori915Hang = isIntelDecoder && doOclTonemap; |
| | 2013 | | } |
| | 2014 | | } |
| | 2015 | |
|
| 0 | 2016 | | if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase)) |
| | 2017 | | { |
| 0 | 2018 | | intelLowPowerHwEncoding = encodingOptions.EnableIntelLowPowerH264HwEncoder; |
| | 2019 | | } |
| 0 | 2020 | | else if (string.Equals(videoEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase)) |
| | 2021 | | { |
| 0 | 2022 | | intelLowPowerHwEncoding = encodingOptions.EnableIntelLowPowerHevcHwEncoder; |
| | 2023 | | } |
| | 2024 | | else |
| | 2025 | | { |
| 0 | 2026 | | enableWaFori915Hang = false; |
| | 2027 | | } |
| | 2028 | | } |
| | 2029 | |
|
| 0 | 2030 | | if (intelLowPowerHwEncoding) |
| | 2031 | | { |
| 0 | 2032 | | param += " -low_power 1"; |
| | 2033 | | } |
| | 2034 | |
|
| 0 | 2035 | | if (enableWaFori915Hang) |
| | 2036 | | { |
| 0 | 2037 | | param += " -async_depth 1"; |
| | 2038 | | } |
| | 2039 | |
|
| 0 | 2040 | | var isLibX265 = string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase); |
| 0 | 2041 | | var encodingPreset = encodingOptions.EncoderPreset; |
| | 2042 | |
|
| 0 | 2043 | | param += GetEncoderParam(encodingPreset, defaultPreset, encodingOptions, videoEncoder, isLibX265); |
| 0 | 2044 | | param += GetVideoBitrateParam(state, videoEncoder); |
| | 2045 | |
|
| 0 | 2046 | | var framerate = GetFramerateParam(state); |
| 0 | 2047 | | if (framerate.HasValue) |
| | 2048 | | { |
| 0 | 2049 | | param += string.Format(CultureInfo.InvariantCulture, " -r {0}", framerate.Value.ToString(CultureInfo.Inv |
| | 2050 | | } |
| | 2051 | |
|
| 0 | 2052 | | var targetVideoCodec = state.ActualOutputVideoCodec; |
| 0 | 2053 | | if (string.Equals(targetVideoCodec, "h265", StringComparison.OrdinalIgnoreCase) |
| 0 | 2054 | | || string.Equals(targetVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | 2055 | | { |
| 0 | 2056 | | targetVideoCodec = "hevc"; |
| | 2057 | | } |
| | 2058 | |
|
| 0 | 2059 | | var profile = state.GetRequestedProfiles(targetVideoCodec).FirstOrDefault() ?? string.Empty; |
| 0 | 2060 | | profile = WhiteSpaceRegex().Replace(profile, string.Empty).ToLowerInvariant(); |
| | 2061 | |
|
| 0 | 2062 | | var videoProfiles = Array.Empty<string>(); |
| 0 | 2063 | | if (string.Equals("h264", targetVideoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 2064 | | { |
| 0 | 2065 | | videoProfiles = _videoProfilesH264; |
| | 2066 | | } |
| 0 | 2067 | | else if (string.Equals("hevc", targetVideoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 2068 | | { |
| 0 | 2069 | | videoProfiles = _videoProfilesH265; |
| | 2070 | | } |
| 0 | 2071 | | else if (string.Equals("av1", targetVideoCodec, StringComparison.OrdinalIgnoreCase)) |
| | 2072 | | { |
| 0 | 2073 | | videoProfiles = _videoProfilesAv1; |
| | 2074 | | } |
| | 2075 | |
|
| 0 | 2076 | | if (!videoProfiles.Contains(profile, StringComparison.OrdinalIgnoreCase)) |
| | 2077 | | { |
| 0 | 2078 | | profile = string.Empty; |
| | 2079 | | } |
| | 2080 | |
|
| | 2081 | | // We only transcode to HEVC 8-bit for now, force Main Profile. |
| 0 | 2082 | | if (profile.Contains("main10", StringComparison.OrdinalIgnoreCase) |
| 0 | 2083 | | || profile.Contains("mainstill", StringComparison.OrdinalIgnoreCase)) |
| | 2084 | | { |
| 0 | 2085 | | profile = "main"; |
| | 2086 | | } |
| | 2087 | |
|
| | 2088 | | // Extended Profile is not supported by any known h264 encoders, force Main Profile. |
| 0 | 2089 | | if (profile.Contains("extended", StringComparison.OrdinalIgnoreCase)) |
| | 2090 | | { |
| 0 | 2091 | | profile = "main"; |
| | 2092 | | } |
| | 2093 | |
|
| | 2094 | | // Only libx264 support encoding H264 High 10 Profile, otherwise force High Profile. |
| 0 | 2095 | | if (!string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 2096 | | && profile.Contains("high10", StringComparison.OrdinalIgnoreCase)) |
| | 2097 | | { |
| 0 | 2098 | | profile = "high"; |
| | 2099 | | } |
| | 2100 | |
|
| | 2101 | | // We only need Main profile of AV1 encoders. |
| 0 | 2102 | | if (videoEncoder.Contains("av1", StringComparison.OrdinalIgnoreCase) |
| 0 | 2103 | | && (profile.Contains("high", StringComparison.OrdinalIgnoreCase) |
| 0 | 2104 | | || profile.Contains("professional", StringComparison.OrdinalIgnoreCase))) |
| | 2105 | | { |
| 0 | 2106 | | profile = "main"; |
| | 2107 | | } |
| | 2108 | |
|
| | 2109 | | // h264_vaapi does not support Baseline profile, force Constrained Baseline in this case, |
| | 2110 | | // which is compatible (and ugly). |
| 0 | 2111 | | if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 2112 | | && profile.Contains("baseline", StringComparison.OrdinalIgnoreCase)) |
| | 2113 | | { |
| 0 | 2114 | | profile = "constrained_baseline"; |
| | 2115 | | } |
| | 2116 | |
|
| | 2117 | | // libx264, h264_{qsv,nvenc,rkmpp} does not support Constrained Baseline profile, force Baseline in this cas |
| 0 | 2118 | | if ((string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 2119 | | || string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 2120 | | || string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2121 | | || string.Equals(videoEncoder, "h264_rkmpp", StringComparison.OrdinalIgnoreCase)) |
| 0 | 2122 | | && profile.Contains("baseline", StringComparison.OrdinalIgnoreCase)) |
| | 2123 | | { |
| 0 | 2124 | | profile = "baseline"; |
| | 2125 | | } |
| | 2126 | |
|
| | 2127 | | // libx264, h264_{qsv,nvenc,vaapi,rkmpp} does not support Constrained High profile, force High in this case. |
| 0 | 2128 | | if ((string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase) |
| 0 | 2129 | | || string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 2130 | | || string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2131 | | || string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 2132 | | || string.Equals(videoEncoder, "h264_rkmpp", StringComparison.OrdinalIgnoreCase)) |
| 0 | 2133 | | && profile.Contains("high", StringComparison.OrdinalIgnoreCase)) |
| | 2134 | | { |
| 0 | 2135 | | profile = "high"; |
| | 2136 | | } |
| | 2137 | |
|
| 0 | 2138 | | if (string.Equals(videoEncoder, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 2139 | | && profile.Contains("baseline", StringComparison.OrdinalIgnoreCase)) |
| | 2140 | | { |
| 0 | 2141 | | profile = "constrained_baseline"; |
| | 2142 | | } |
| | 2143 | |
|
| 0 | 2144 | | if (string.Equals(videoEncoder, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 2145 | | && profile.Contains("constrainedhigh", StringComparison.OrdinalIgnoreCase)) |
| | 2146 | | { |
| 0 | 2147 | | profile = "constrained_high"; |
| | 2148 | | } |
| | 2149 | |
|
| 0 | 2150 | | if (string.Equals(videoEncoder, "h264_videotoolbox", StringComparison.OrdinalIgnoreCase) |
| 0 | 2151 | | && profile.Contains("constrainedbaseline", StringComparison.OrdinalIgnoreCase)) |
| | 2152 | | { |
| 0 | 2153 | | profile = "constrained_baseline"; |
| | 2154 | | } |
| | 2155 | |
|
| 0 | 2156 | | if (string.Equals(videoEncoder, "h264_videotoolbox", StringComparison.OrdinalIgnoreCase) |
| 0 | 2157 | | && profile.Contains("constrainedhigh", StringComparison.OrdinalIgnoreCase)) |
| | 2158 | | { |
| 0 | 2159 | | profile = "constrained_high"; |
| | 2160 | | } |
| | 2161 | |
|
| 0 | 2162 | | if (!string.IsNullOrEmpty(profile)) |
| | 2163 | | { |
| | 2164 | | // Currently there's no profile option in av1_nvenc encoder |
| 0 | 2165 | | if (!(string.Equals(videoEncoder, "av1_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2166 | | || string.Equals(videoEncoder, "h264_v4l2m2m", StringComparison.OrdinalIgnoreCase))) |
| | 2167 | | { |
| 0 | 2168 | | param += " -profile:v:0 " + profile; |
| | 2169 | | } |
| | 2170 | | } |
| | 2171 | |
|
| 0 | 2172 | | var level = state.GetRequestedLevel(targetVideoCodec); |
| | 2173 | |
|
| 0 | 2174 | | if (!string.IsNullOrEmpty(level)) |
| | 2175 | | { |
| 0 | 2176 | | level = NormalizeTranscodingLevel(state, level); |
| | 2177 | |
|
| | 2178 | | // libx264, QSV, AMF can adjust the given level to match the output. |
| 0 | 2179 | | if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 2180 | | || string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase)) |
| | 2181 | | { |
| 0 | 2182 | | param += " -level " + level; |
| | 2183 | | } |
| 0 | 2184 | | else if (string.Equals(videoEncoder, "hevc_qsv", StringComparison.OrdinalIgnoreCase)) |
| | 2185 | | { |
| | 2186 | | // hevc_qsv use -level 51 instead of -level 153. |
| 0 | 2187 | | if (double.TryParse(level, CultureInfo.InvariantCulture, out double hevcLevel)) |
| | 2188 | | { |
| 0 | 2189 | | param += " -level " + (hevcLevel / 3); |
| | 2190 | | } |
| | 2191 | | } |
| 0 | 2192 | | else if (string.Equals(videoEncoder, "av1_qsv", StringComparison.OrdinalIgnoreCase) |
| 0 | 2193 | | || string.Equals(videoEncoder, "libsvtav1", StringComparison.OrdinalIgnoreCase)) |
| | 2194 | | { |
| | 2195 | | // libsvtav1 and av1_qsv use -level 60 instead of -level 16 |
| | 2196 | | // https://aomedia.org/av1/specification/annex-a/ |
| 0 | 2197 | | if (int.TryParse(level, NumberStyles.Any, CultureInfo.InvariantCulture, out int av1Level)) |
| | 2198 | | { |
| 0 | 2199 | | var x = 2 + (av1Level >> 2); |
| 0 | 2200 | | var y = av1Level & 3; |
| 0 | 2201 | | var res = (x * 10) + y; |
| 0 | 2202 | | param += " -level " + res; |
| | 2203 | | } |
| | 2204 | | } |
| 0 | 2205 | | else if (string.Equals(videoEncoder, "h264_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 2206 | | || string.Equals(videoEncoder, "hevc_amf", StringComparison.OrdinalIgnoreCase) |
| 0 | 2207 | | || string.Equals(videoEncoder, "av1_amf", StringComparison.OrdinalIgnoreCase)) |
| | 2208 | | { |
| 0 | 2209 | | param += " -level " + level; |
| | 2210 | | } |
| 0 | 2211 | | else if (string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2212 | | || string.Equals(videoEncoder, "hevc_nvenc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2213 | | || string.Equals(videoEncoder, "av1_nvenc", StringComparison.OrdinalIgnoreCase)) |
| | 2214 | | { |
| | 2215 | | // level option may cause NVENC to fail. |
| | 2216 | | // NVENC cannot adjust the given level, just throw an error. |
| | 2217 | | } |
| 0 | 2218 | | else if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 2219 | | || string.Equals(videoEncoder, "hevc_vaapi", StringComparison.OrdinalIgnoreCase) |
| 0 | 2220 | | || string.Equals(videoEncoder, "av1_vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 2221 | | { |
| | 2222 | | // level option may cause corrupted frames on AMD VAAPI. |
| 0 | 2223 | | if (_mediaEncoder.IsVaapiDeviceInteliHD || _mediaEncoder.IsVaapiDeviceInteli965) |
| | 2224 | | { |
| 0 | 2225 | | param += " -level " + level; |
| | 2226 | | } |
| | 2227 | | } |
| 0 | 2228 | | else if (string.Equals(videoEncoder, "h264_rkmpp", StringComparison.OrdinalIgnoreCase) |
| 0 | 2229 | | || string.Equals(videoEncoder, "hevc_rkmpp", StringComparison.OrdinalIgnoreCase)) |
| | 2230 | | { |
| 0 | 2231 | | param += " -level " + level; |
| | 2232 | | } |
| 0 | 2233 | | else if (!string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase)) |
| | 2234 | | { |
| 0 | 2235 | | param += " -level " + level; |
| | 2236 | | } |
| | 2237 | | } |
| | 2238 | |
|
| 0 | 2239 | | if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase)) |
| | 2240 | | { |
| 0 | 2241 | | param += " -x264opts:0 subme=0:me_range=16:rc_lookahead=10:me=hex:open_gop=0"; |
| | 2242 | | } |
| | 2243 | |
|
| 0 | 2244 | | if (string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase)) |
| | 2245 | | { |
| | 2246 | | // libx265 only accept level option in -x265-params. |
| | 2247 | | // level option may cause libx265 to fail. |
| | 2248 | | // libx265 cannot adjust the given level, just throw an error. |
| 0 | 2249 | | param += " -x265-params:0 no-scenecut=1:no-open-gop=1:no-info=1"; |
| | 2250 | |
|
| 0 | 2251 | | if (encodingOptions.EncoderPreset < EncoderPreset.ultrafast) |
| | 2252 | | { |
| | 2253 | | // The following params are slower than the ultrafast preset, don't use when ultrafast is selected. |
| 0 | 2254 | | param += ":subme=3:merange=25:rc-lookahead=10:me=star:ctu=32:max-tu-size=32:min-cu-size=16:rskip=2:r |
| | 2255 | | } |
| | 2256 | | } |
| | 2257 | |
|
| 0 | 2258 | | if (string.Equals(videoEncoder, "libsvtav1", StringComparison.OrdinalIgnoreCase) |
| 0 | 2259 | | && _mediaEncoder.EncoderVersion >= _minFFmpegSvtAv1Params) |
| | 2260 | | { |
| 0 | 2261 | | param += " -svtav1-params:0 rc=1:tune=0:film-grain=0:enable-overlays=1:enable-tf=0"; |
| | 2262 | | } |
| | 2263 | |
|
| | 2264 | | /* Access unit too large: 8192 < 20880 error */ |
| 0 | 2265 | | if ((string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) || |
| 0 | 2266 | | string.Equals(videoEncoder, "hevc_vaapi", StringComparison.OrdinalIgnoreCase)) && |
| 0 | 2267 | | _mediaEncoder.EncoderVersion >= _minFFmpegVaapiH26xEncA53CcSei) |
| | 2268 | | { |
| 0 | 2269 | | param += " -sei -a53_cc"; |
| | 2270 | | } |
| | 2271 | |
|
| 0 | 2272 | | return param; |
| | 2273 | | } |
| | 2274 | |
|
| | 2275 | | public bool CanStreamCopyVideo(EncodingJobInfo state, MediaStream videoStream) |
| | 2276 | | { |
| 0 | 2277 | | var request = state.BaseRequest; |
| | 2278 | |
|
| 0 | 2279 | | if (!request.AllowVideoStreamCopy) |
| | 2280 | | { |
| 0 | 2281 | | return false; |
| | 2282 | | } |
| | 2283 | |
|
| 0 | 2284 | | if (videoStream.IsInterlaced |
| 0 | 2285 | | && state.DeInterlace(videoStream.Codec, false)) |
| | 2286 | | { |
| 0 | 2287 | | return false; |
| | 2288 | | } |
| | 2289 | |
|
| 0 | 2290 | | if (videoStream.IsAnamorphic ?? false) |
| | 2291 | | { |
| 0 | 2292 | | if (request.RequireNonAnamorphic) |
| | 2293 | | { |
| 0 | 2294 | | return false; |
| | 2295 | | } |
| | 2296 | | } |
| | 2297 | |
|
| | 2298 | | // Can't stream copy if we're burning in subtitles |
| 0 | 2299 | | if (request.SubtitleStreamIndex.HasValue |
| 0 | 2300 | | && request.SubtitleStreamIndex.Value >= 0 |
| 0 | 2301 | | && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode) |
| | 2302 | | { |
| 0 | 2303 | | return false; |
| | 2304 | | } |
| | 2305 | |
|
| 0 | 2306 | | if (string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 2307 | | && videoStream.IsAVC.HasValue |
| 0 | 2308 | | && !videoStream.IsAVC.Value |
| 0 | 2309 | | && request.RequireAvc) |
| | 2310 | | { |
| 0 | 2311 | | return false; |
| | 2312 | | } |
| | 2313 | |
|
| | 2314 | | // Source and target codecs must match |
| 0 | 2315 | | if (string.IsNullOrEmpty(videoStream.Codec) |
| 0 | 2316 | | || (state.SupportedVideoCodecs.Length != 0 |
| 0 | 2317 | | && !state.SupportedVideoCodecs.Contains(videoStream.Codec, StringComparison.OrdinalIgnoreCase))) |
| | 2318 | | { |
| 0 | 2319 | | return false; |
| | 2320 | | } |
| | 2321 | |
|
| 0 | 2322 | | var requestedProfiles = state.GetRequestedProfiles(videoStream.Codec); |
| | 2323 | |
|
| | 2324 | | // If client is requesting a specific video profile, it must match the source |
| 0 | 2325 | | if (requestedProfiles.Length > 0) |
| | 2326 | | { |
| 0 | 2327 | | if (string.IsNullOrEmpty(videoStream.Profile)) |
| | 2328 | | { |
| | 2329 | | // return false; |
| | 2330 | | } |
| | 2331 | |
|
| 0 | 2332 | | var requestedProfile = requestedProfiles[0]; |
| | 2333 | | // strip spaces because they may be stripped out on the query string as well |
| 0 | 2334 | | if (!string.IsNullOrEmpty(videoStream.Profile) |
| 0 | 2335 | | && !requestedProfiles.Contains(videoStream.Profile.Replace(" ", string.Empty, StringComparison.Ordin |
| | 2336 | | { |
| 0 | 2337 | | var currentScore = GetVideoProfileScore(videoStream.Codec, videoStream.Profile); |
| 0 | 2338 | | var requestedScore = GetVideoProfileScore(videoStream.Codec, requestedProfile); |
| | 2339 | |
|
| 0 | 2340 | | if (currentScore == -1 || currentScore > requestedScore) |
| | 2341 | | { |
| 0 | 2342 | | return false; |
| | 2343 | | } |
| | 2344 | | } |
| | 2345 | | } |
| | 2346 | |
|
| 0 | 2347 | | var requestedRangeTypes = state.GetRequestedRangeTypes(videoStream.Codec); |
| 0 | 2348 | | if (requestedRangeTypes.Length > 0) |
| | 2349 | | { |
| 0 | 2350 | | if (videoStream.VideoRangeType == VideoRangeType.Unknown) |
| | 2351 | | { |
| 0 | 2352 | | return false; |
| | 2353 | | } |
| | 2354 | |
|
| | 2355 | | // DOVIWithHDR10 should be compatible with HDR10 supporting players. Same goes with HLG and of course SD |
| 0 | 2356 | | var requestHasHDR10 = requestedRangeTypes.Contains(VideoRangeType.HDR10.ToString(), StringComparison.Ord |
| 0 | 2357 | | var requestHasHLG = requestedRangeTypes.Contains(VideoRangeType.HLG.ToString(), StringComparison.Ordinal |
| 0 | 2358 | | var requestHasSDR = requestedRangeTypes.Contains(VideoRangeType.SDR.ToString(), StringComparison.Ordinal |
| | 2359 | |
|
| 0 | 2360 | | if (!requestedRangeTypes.Contains(videoStream.VideoRangeType.ToString(), StringComparison.OrdinalIgnoreC |
| 0 | 2361 | | && !((requestHasHDR10 && videoStream.VideoRangeType == VideoRangeType.DOVIWithHDR10) |
| 0 | 2362 | | || (requestHasHLG && videoStream.VideoRangeType == VideoRangeType.DOVIWithHLG) |
| 0 | 2363 | | || (requestHasSDR && videoStream.VideoRangeType == VideoRangeType.DOVIWithSDR) |
| 0 | 2364 | | || (requestHasHDR10 && videoStream.VideoRangeType == VideoRangeType.HDR10Plus))) |
| | 2365 | | { |
| | 2366 | | // Check complicated cases where we need to remove dynamic metadata |
| | 2367 | | // Conservatively refuse to copy if the encoder can't remove dynamic metadata, |
| | 2368 | | // but a removal is required for compatability reasons. |
| 0 | 2369 | | var dynamicHdrMetadataRemovalPlan = ShouldRemoveDynamicHdrMetadata(state); |
| 0 | 2370 | | if (!CanEncoderRemoveDynamicHdrMetadata(dynamicHdrMetadataRemovalPlan, videoStream)) |
| | 2371 | | { |
| 0 | 2372 | | return false; |
| | 2373 | | } |
| | 2374 | | } |
| | 2375 | | } |
| | 2376 | |
|
| | 2377 | | // Video width must fall within requested value |
| 0 | 2378 | | if (request.MaxWidth.HasValue |
| 0 | 2379 | | && (!videoStream.Width.HasValue || videoStream.Width.Value > request.MaxWidth.Value)) |
| | 2380 | | { |
| 0 | 2381 | | return false; |
| | 2382 | | } |
| | 2383 | |
|
| | 2384 | | // Video height must fall within requested value |
| 0 | 2385 | | if (request.MaxHeight.HasValue |
| 0 | 2386 | | && (!videoStream.Height.HasValue || videoStream.Height.Value > request.MaxHeight.Value)) |
| | 2387 | | { |
| 0 | 2388 | | return false; |
| | 2389 | | } |
| | 2390 | |
|
| | 2391 | | // Video framerate must fall within requested value |
| 0 | 2392 | | var requestedFramerate = request.MaxFramerate ?? request.Framerate; |
| 0 | 2393 | | if (requestedFramerate.HasValue) |
| | 2394 | | { |
| 0 | 2395 | | var videoFrameRate = videoStream.ReferenceFrameRate; |
| | 2396 | |
|
| | 2397 | | // Add a little tolerance to the framerate check because some videos might record a framerate |
| | 2398 | | // that is slightly greater than the intended framerate, but the device can still play it correctly. |
| | 2399 | | // 0.05 fps tolerance should be safe enough. |
| 0 | 2400 | | if (!videoFrameRate.HasValue || videoFrameRate.Value > requestedFramerate.Value + 0.05f) |
| | 2401 | | { |
| 0 | 2402 | | return false; |
| | 2403 | | } |
| | 2404 | | } |
| | 2405 | |
|
| | 2406 | | // Video bitrate must fall within requested value |
| 0 | 2407 | | if (request.VideoBitRate.HasValue |
| 0 | 2408 | | && (!videoStream.BitRate.HasValue || videoStream.BitRate.Value > request.VideoBitRate.Value)) |
| | 2409 | | { |
| | 2410 | | // For LiveTV that has no bitrate, let's try copy if other conditions are met |
| 0 | 2411 | | if (string.IsNullOrWhiteSpace(request.LiveStreamId) || videoStream.BitRate.HasValue) |
| | 2412 | | { |
| 0 | 2413 | | return false; |
| | 2414 | | } |
| | 2415 | | } |
| | 2416 | |
|
| 0 | 2417 | | var maxBitDepth = state.GetRequestedVideoBitDepth(videoStream.Codec); |
| 0 | 2418 | | if (maxBitDepth.HasValue) |
| | 2419 | | { |
| 0 | 2420 | | if (videoStream.BitDepth.HasValue && videoStream.BitDepth.Value > maxBitDepth.Value) |
| | 2421 | | { |
| 0 | 2422 | | return false; |
| | 2423 | | } |
| | 2424 | | } |
| | 2425 | |
|
| 0 | 2426 | | var maxRefFrames = state.GetRequestedMaxRefFrames(videoStream.Codec); |
| 0 | 2427 | | if (maxRefFrames.HasValue |
| 0 | 2428 | | && videoStream.RefFrames.HasValue && videoStream.RefFrames.Value > maxRefFrames.Value) |
| | 2429 | | { |
| 0 | 2430 | | return false; |
| | 2431 | | } |
| | 2432 | |
|
| | 2433 | | // If a specific level was requested, the source must match or be less than |
| 0 | 2434 | | var level = state.GetRequestedLevel(videoStream.Codec); |
| 0 | 2435 | | if (double.TryParse(level, CultureInfo.InvariantCulture, out var requestLevel)) |
| | 2436 | | { |
| 0 | 2437 | | if (!videoStream.Level.HasValue) |
| | 2438 | | { |
| | 2439 | | // return false; |
| | 2440 | | } |
| | 2441 | |
|
| 0 | 2442 | | if (videoStream.Level.HasValue && videoStream.Level.Value > requestLevel) |
| | 2443 | | { |
| 0 | 2444 | | return false; |
| | 2445 | | } |
| | 2446 | | } |
| | 2447 | |
|
| 0 | 2448 | | if (string.Equals(state.InputContainer, "avi", StringComparison.OrdinalIgnoreCase) |
| 0 | 2449 | | && string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase) |
| 0 | 2450 | | && !(videoStream.IsAVC ?? false)) |
| | 2451 | | { |
| | 2452 | | // see Coach S01E01 - Kelly and the Professor(0).avi |
| 0 | 2453 | | return false; |
| | 2454 | | } |
| | 2455 | |
|
| 0 | 2456 | | return true; |
| | 2457 | | } |
| | 2458 | |
|
| | 2459 | | public bool CanStreamCopyAudio(EncodingJobInfo state, MediaStream audioStream, IEnumerable<string> supportedAudi |
| | 2460 | | { |
| 0 | 2461 | | var request = state.BaseRequest; |
| | 2462 | |
|
| 0 | 2463 | | if (!request.AllowAudioStreamCopy) |
| | 2464 | | { |
| 0 | 2465 | | return false; |
| | 2466 | | } |
| | 2467 | |
|
| 0 | 2468 | | var maxBitDepth = state.GetRequestedAudioBitDepth(audioStream.Codec); |
| 0 | 2469 | | if (maxBitDepth.HasValue |
| 0 | 2470 | | && audioStream.BitDepth.HasValue |
| 0 | 2471 | | && audioStream.BitDepth.Value > maxBitDepth.Value) |
| | 2472 | | { |
| 0 | 2473 | | return false; |
| | 2474 | | } |
| | 2475 | |
|
| | 2476 | | // Source and target codecs must match |
| 0 | 2477 | | if (string.IsNullOrEmpty(audioStream.Codec) |
| 0 | 2478 | | || !supportedAudioCodecs.Contains(audioStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 2479 | | { |
| 0 | 2480 | | return false; |
| | 2481 | | } |
| | 2482 | |
|
| | 2483 | | // Channels must fall within requested value |
| 0 | 2484 | | var channels = state.GetRequestedAudioChannels(audioStream.Codec); |
| 0 | 2485 | | if (channels.HasValue) |
| | 2486 | | { |
| 0 | 2487 | | if (!audioStream.Channels.HasValue || audioStream.Channels.Value <= 0) |
| | 2488 | | { |
| 0 | 2489 | | return false; |
| | 2490 | | } |
| | 2491 | |
|
| 0 | 2492 | | if (audioStream.Channels.Value > channels.Value) |
| | 2493 | | { |
| 0 | 2494 | | return false; |
| | 2495 | | } |
| | 2496 | | } |
| | 2497 | |
|
| | 2498 | | // Sample rate must fall within requested value |
| 0 | 2499 | | if (request.AudioSampleRate.HasValue) |
| | 2500 | | { |
| 0 | 2501 | | if (!audioStream.SampleRate.HasValue || audioStream.SampleRate.Value <= 0) |
| | 2502 | | { |
| 0 | 2503 | | return false; |
| | 2504 | | } |
| | 2505 | |
|
| 0 | 2506 | | if (audioStream.SampleRate.Value > request.AudioSampleRate.Value) |
| | 2507 | | { |
| 0 | 2508 | | return false; |
| | 2509 | | } |
| | 2510 | | } |
| | 2511 | |
|
| | 2512 | | // Audio bitrate must fall within requested value |
| 0 | 2513 | | if (request.AudioBitRate.HasValue |
| 0 | 2514 | | && audioStream.BitRate.HasValue |
| 0 | 2515 | | && audioStream.BitRate.Value > request.AudioBitRate.Value) |
| | 2516 | | { |
| 0 | 2517 | | return false; |
| | 2518 | | } |
| | 2519 | |
|
| 0 | 2520 | | return request.EnableAutoStreamCopy; |
| | 2521 | | } |
| | 2522 | |
|
| | 2523 | | public int GetVideoBitrateParamValue(BaseEncodingJobOptions request, MediaStream videoStream, string outputVideo |
| | 2524 | | { |
| 0 | 2525 | | var bitrate = request.VideoBitRate; |
| | 2526 | |
|
| 0 | 2527 | | if (videoStream is not null) |
| | 2528 | | { |
| 0 | 2529 | | var isUpscaling = request.Height.HasValue |
| 0 | 2530 | | && videoStream.Height.HasValue |
| 0 | 2531 | | && request.Height.Value > videoStream.Height.Value |
| 0 | 2532 | | && request.Width.HasValue |
| 0 | 2533 | | && videoStream.Width.HasValue |
| 0 | 2534 | | && request.Width.Value > videoStream.Width.Value; |
| | 2535 | |
|
| | 2536 | | // Don't allow bitrate increases unless upscaling |
| 0 | 2537 | | if (!isUpscaling && bitrate.HasValue && videoStream.BitRate.HasValue) |
| | 2538 | | { |
| 0 | 2539 | | bitrate = GetMinBitrate(videoStream.BitRate.Value, bitrate.Value); |
| | 2540 | | } |
| | 2541 | |
|
| 0 | 2542 | | if (bitrate.HasValue) |
| | 2543 | | { |
| 0 | 2544 | | var inputVideoCodec = videoStream.Codec; |
| 0 | 2545 | | bitrate = ScaleBitrate(bitrate.Value, inputVideoCodec, outputVideoCodec); |
| | 2546 | |
|
| | 2547 | | // If a max bitrate was requested, don't let the scaled bitrate exceed it |
| 0 | 2548 | | if (request.VideoBitRate.HasValue) |
| | 2549 | | { |
| 0 | 2550 | | bitrate = Math.Min(bitrate.Value, request.VideoBitRate.Value); |
| | 2551 | | } |
| | 2552 | | } |
| | 2553 | | } |
| | 2554 | |
|
| | 2555 | | // Cap the max target bitrate to intMax/2 to satisfy the bufsize=bitrate*2. |
| 0 | 2556 | | return Math.Min(bitrate ?? 0, int.MaxValue / 2); |
| | 2557 | | } |
| | 2558 | |
|
| | 2559 | | private int GetMinBitrate(int sourceBitrate, int requestedBitrate) |
| | 2560 | | { |
| | 2561 | | // these values were chosen from testing to improve low bitrate streams |
| 0 | 2562 | | if (sourceBitrate <= 2000000) |
| | 2563 | | { |
| 0 | 2564 | | sourceBitrate = Convert.ToInt32(sourceBitrate * 2.5); |
| | 2565 | | } |
| 0 | 2566 | | else if (sourceBitrate <= 3000000) |
| | 2567 | | { |
| 0 | 2568 | | sourceBitrate *= 2; |
| | 2569 | | } |
| | 2570 | |
|
| 0 | 2571 | | var bitrate = Math.Min(sourceBitrate, requestedBitrate); |
| | 2572 | |
|
| 0 | 2573 | | return bitrate; |
| | 2574 | | } |
| | 2575 | |
|
| | 2576 | | private static double GetVideoBitrateScaleFactor(string codec) |
| | 2577 | | { |
| | 2578 | | // hevc & vp9 - 40% more efficient than h.264 |
| 0 | 2579 | | if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase) |
| 0 | 2580 | | || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 2581 | | || string.Equals(codec, "vp9", StringComparison.OrdinalIgnoreCase)) |
| | 2582 | | { |
| 0 | 2583 | | return .6; |
| | 2584 | | } |
| | 2585 | |
|
| | 2586 | | // av1 - 50% more efficient than h.264 |
| 0 | 2587 | | if (string.Equals(codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 2588 | | { |
| 0 | 2589 | | return .5; |
| | 2590 | | } |
| | 2591 | |
|
| 0 | 2592 | | return 1; |
| | 2593 | | } |
| | 2594 | |
|
| | 2595 | | public static int ScaleBitrate(int bitrate, string inputVideoCodec, string outputVideoCodec) |
| | 2596 | | { |
| 0 | 2597 | | var inputScaleFactor = GetVideoBitrateScaleFactor(inputVideoCodec); |
| 0 | 2598 | | var outputScaleFactor = GetVideoBitrateScaleFactor(outputVideoCodec); |
| | 2599 | |
|
| | 2600 | | // Don't scale the real bitrate lower than the requested bitrate |
| 0 | 2601 | | var scaleFactor = Math.Max(outputScaleFactor / inputScaleFactor, 1); |
| | 2602 | |
|
| 0 | 2603 | | if (bitrate <= 500000) |
| | 2604 | | { |
| 0 | 2605 | | scaleFactor = Math.Max(scaleFactor, 4); |
| | 2606 | | } |
| 0 | 2607 | | else if (bitrate <= 1000000) |
| | 2608 | | { |
| 0 | 2609 | | scaleFactor = Math.Max(scaleFactor, 3); |
| | 2610 | | } |
| 0 | 2611 | | else if (bitrate <= 2000000) |
| | 2612 | | { |
| 0 | 2613 | | scaleFactor = Math.Max(scaleFactor, 2.5); |
| | 2614 | | } |
| 0 | 2615 | | else if (bitrate <= 3000000) |
| | 2616 | | { |
| 0 | 2617 | | scaleFactor = Math.Max(scaleFactor, 2); |
| | 2618 | | } |
| 0 | 2619 | | else if (bitrate >= 30000000) |
| | 2620 | | { |
| | 2621 | | // Don't scale beyond 30Mbps, it is hardly visually noticeable for most codecs with our prefer speed enc |
| | 2622 | | // and will cause extremely high bitrate to be used for av1->h264 transcoding that will overload clients |
| 0 | 2623 | | scaleFactor = 1; |
| | 2624 | | } |
| | 2625 | |
|
| 0 | 2626 | | return Convert.ToInt32(scaleFactor * bitrate); |
| | 2627 | | } |
| | 2628 | |
|
| | 2629 | | public int? GetAudioBitrateParam(BaseEncodingJobOptions request, MediaStream audioStream, int? outputAudioChanne |
| | 2630 | | { |
| 0 | 2631 | | return GetAudioBitrateParam(request.AudioBitRate, request.AudioCodec, audioStream, outputAudioChannels); |
| | 2632 | | } |
| | 2633 | |
|
| | 2634 | | public int? GetAudioBitrateParam(int? audioBitRate, string audioCodec, MediaStream audioStream, int? outputAudio |
| | 2635 | | { |
| 0 | 2636 | | if (audioStream is null) |
| | 2637 | | { |
| 0 | 2638 | | return null; |
| | 2639 | | } |
| | 2640 | |
|
| 0 | 2641 | | var inputChannels = audioStream.Channels ?? 0; |
| 0 | 2642 | | var outputChannels = outputAudioChannels ?? 0; |
| 0 | 2643 | | var bitrate = audioBitRate ?? int.MaxValue; |
| | 2644 | |
|
| 0 | 2645 | | if (string.IsNullOrEmpty(audioCodec) |
| 0 | 2646 | | || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase) |
| 0 | 2647 | | || string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase) |
| 0 | 2648 | | || string.Equals(audioCodec, "opus", StringComparison.OrdinalIgnoreCase) |
| 0 | 2649 | | || string.Equals(audioCodec, "vorbis", StringComparison.OrdinalIgnoreCase) |
| 0 | 2650 | | || string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase) |
| 0 | 2651 | | || string.Equals(audioCodec, "eac3", StringComparison.OrdinalIgnoreCase)) |
| | 2652 | | { |
| 0 | 2653 | | return (inputChannels, outputChannels) switch |
| 0 | 2654 | | { |
| 0 | 2655 | | (>= 6, >= 6 or 0) => Math.Min(640000, bitrate), |
| 0 | 2656 | | (> 0, > 0) => Math.Min(outputChannels * 128000, bitrate), |
| 0 | 2657 | | (> 0, _) => Math.Min(inputChannels * 128000, bitrate), |
| 0 | 2658 | | (_, _) => Math.Min(384000, bitrate) |
| 0 | 2659 | | }; |
| | 2660 | | } |
| | 2661 | |
|
| 0 | 2662 | | if (string.Equals(audioCodec, "dts", StringComparison.OrdinalIgnoreCase) |
| 0 | 2663 | | || string.Equals(audioCodec, "dca", StringComparison.OrdinalIgnoreCase)) |
| | 2664 | | { |
| 0 | 2665 | | return (inputChannels, outputChannels) switch |
| 0 | 2666 | | { |
| 0 | 2667 | | (>= 6, >= 6 or 0) => Math.Min(768000, bitrate), |
| 0 | 2668 | | (> 0, > 0) => Math.Min(outputChannels * 136000, bitrate), |
| 0 | 2669 | | (> 0, _) => Math.Min(inputChannels * 136000, bitrate), |
| 0 | 2670 | | (_, _) => Math.Min(672000, bitrate) |
| 0 | 2671 | | }; |
| | 2672 | | } |
| | 2673 | |
|
| | 2674 | | // Empty bitrate area is not allow on iOS |
| | 2675 | | // Default audio bitrate to 128K per channel if we don't have codec specific defaults |
| | 2676 | | // https://ffmpeg.org/ffmpeg-codecs.html#toc-Codec-Options |
| 0 | 2677 | | return 128000 * (outputAudioChannels ?? audioStream.Channels ?? 2); |
| | 2678 | | } |
| | 2679 | |
|
| | 2680 | | public string GetAudioVbrModeParam(string encoder, int bitrate, int channels) |
| | 2681 | | { |
| 0 | 2682 | | var bitratePerChannel = bitrate / Math.Max(channels, 1); |
| 0 | 2683 | | if (string.Equals(encoder, "libfdk_aac", StringComparison.OrdinalIgnoreCase)) |
| | 2684 | | { |
| 0 | 2685 | | return " -vbr:a " + bitratePerChannel switch |
| 0 | 2686 | | { |
| 0 | 2687 | | < 32000 => "1", |
| 0 | 2688 | | < 48000 => "2", |
| 0 | 2689 | | < 64000 => "3", |
| 0 | 2690 | | < 96000 => "4", |
| 0 | 2691 | | _ => "5" |
| 0 | 2692 | | }; |
| | 2693 | | } |
| | 2694 | |
|
| 0 | 2695 | | if (string.Equals(encoder, "libmp3lame", StringComparison.OrdinalIgnoreCase)) |
| | 2696 | | { |
| | 2697 | | // lame's VBR is only good for a certain bitrate range |
| | 2698 | | // For very low and very high bitrate, use abr mode |
| 0 | 2699 | | if (bitratePerChannel is < 122500 and > 48000) |
| | 2700 | | { |
| 0 | 2701 | | return " -qscale:a " + bitratePerChannel switch |
| 0 | 2702 | | { |
| 0 | 2703 | | < 64000 => "6", |
| 0 | 2704 | | < 88000 => "4", |
| 0 | 2705 | | < 112000 => "2", |
| 0 | 2706 | | _ => "0" |
| 0 | 2707 | | }; |
| | 2708 | | } |
| | 2709 | |
|
| 0 | 2710 | | return " -abr:a 1" + " -b:a " + bitrate; |
| | 2711 | | } |
| | 2712 | |
|
| 0 | 2713 | | if (string.Equals(encoder, "aac_at", StringComparison.OrdinalIgnoreCase)) |
| | 2714 | | { |
| | 2715 | | // aac_at's CVBR mode |
| 0 | 2716 | | return " -aac_at_mode:a 2" + " -b:a " + bitrate; |
| | 2717 | | } |
| | 2718 | |
|
| 0 | 2719 | | if (string.Equals(encoder, "libvorbis", StringComparison.OrdinalIgnoreCase)) |
| | 2720 | | { |
| 0 | 2721 | | return " -qscale:a " + bitratePerChannel switch |
| 0 | 2722 | | { |
| 0 | 2723 | | < 40000 => "0", |
| 0 | 2724 | | < 56000 => "2", |
| 0 | 2725 | | < 80000 => "4", |
| 0 | 2726 | | < 112000 => "6", |
| 0 | 2727 | | _ => "8" |
| 0 | 2728 | | }; |
| | 2729 | | } |
| | 2730 | |
|
| 0 | 2731 | | return null; |
| | 2732 | | } |
| | 2733 | |
|
| | 2734 | | public string GetAudioFilterParam(EncodingJobInfo state, EncodingOptions encodingOptions) |
| | 2735 | | { |
| 0 | 2736 | | var channels = state.OutputAudioChannels; |
| | 2737 | |
|
| 0 | 2738 | | var filters = new List<string>(); |
| | 2739 | |
|
| 0 | 2740 | | if (channels is 2 && state.AudioStream?.Channels is > 2) |
| | 2741 | | { |
| 0 | 2742 | | var hasDownMixFilter = DownMixAlgorithmsHelper.AlgorithmFilterStrings.TryGetValue((encodingOptions.DownM |
| 0 | 2743 | | if (hasDownMixFilter) |
| | 2744 | | { |
| 0 | 2745 | | filters.Add(downMixFilterString); |
| | 2746 | | } |
| | 2747 | |
|
| 0 | 2748 | | if (!encodingOptions.DownMixAudioBoost.Equals(1)) |
| | 2749 | | { |
| 0 | 2750 | | filters.Add("volume=" + encodingOptions.DownMixAudioBoost.ToString(CultureInfo.InvariantCulture)); |
| | 2751 | | } |
| | 2752 | | } |
| | 2753 | |
|
| 0 | 2754 | | var isCopyingTimestamps = state.CopyTimestamps || state.TranscodingType != TranscodingJobType.Progressive; |
| 0 | 2755 | | if (state.SubtitleStream is not null && state.SubtitleStream.IsTextSubtitleStream && ShouldEncodeSubtitle(st |
| | 2756 | | { |
| 0 | 2757 | | var seconds = TimeSpan.FromTicks(state.StartTimeTicks ?? 0).TotalSeconds; |
| | 2758 | |
|
| 0 | 2759 | | filters.Add( |
| 0 | 2760 | | string.Format( |
| 0 | 2761 | | CultureInfo.InvariantCulture, |
| 0 | 2762 | | "asetpts=PTS-{0}/TB", |
| 0 | 2763 | | Math.Round(seconds))); |
| | 2764 | | } |
| | 2765 | |
|
| 0 | 2766 | | if (filters.Count > 0) |
| | 2767 | | { |
| 0 | 2768 | | return " -af \"" + string.Join(',', filters) + "\""; |
| | 2769 | | } |
| | 2770 | |
|
| 0 | 2771 | | return string.Empty; |
| | 2772 | | } |
| | 2773 | |
|
| | 2774 | | /// <summary> |
| | 2775 | | /// Gets the number of audio channels to specify on the command line. |
| | 2776 | | /// </summary> |
| | 2777 | | /// <param name="state">The state.</param> |
| | 2778 | | /// <param name="audioStream">The audio stream.</param> |
| | 2779 | | /// <param name="outputAudioCodec">The output audio codec.</param> |
| | 2780 | | /// <returns>System.Nullable{System.Int32}.</returns> |
| | 2781 | | public int? GetNumAudioChannelsParam(EncodingJobInfo state, MediaStream audioStream, string outputAudioCodec) |
| | 2782 | | { |
| 0 | 2783 | | if (audioStream is null) |
| | 2784 | | { |
| 0 | 2785 | | return null; |
| | 2786 | | } |
| | 2787 | |
|
| 0 | 2788 | | var request = state.BaseRequest; |
| | 2789 | |
|
| 0 | 2790 | | var codec = outputAudioCodec ?? string.Empty; |
| | 2791 | |
|
| 0 | 2792 | | int? resultChannels = state.GetRequestedAudioChannels(codec); |
| | 2793 | |
|
| 0 | 2794 | | var inputChannels = audioStream.Channels; |
| | 2795 | |
|
| 0 | 2796 | | if (inputChannels > 0) |
| | 2797 | | { |
| 0 | 2798 | | resultChannels = inputChannels < resultChannels ? inputChannels : resultChannels ?? inputChannels; |
| | 2799 | | } |
| | 2800 | |
|
| 0 | 2801 | | var isTranscodingAudio = !IsCopyCodec(codec); |
| | 2802 | |
|
| 0 | 2803 | | if (isTranscodingAudio) |
| | 2804 | | { |
| 0 | 2805 | | var audioEncoder = GetAudioEncoder(state); |
| 0 | 2806 | | if (!_audioTranscodeChannelLookup.TryGetValue(audioEncoder, out var transcoderChannelLimit)) |
| | 2807 | | { |
| | 2808 | | // Set default max transcoding channels to 8 to prevent encoding errors due to asking for too many c |
| 0 | 2809 | | transcoderChannelLimit = 8; |
| | 2810 | | } |
| | 2811 | |
|
| | 2812 | | // Set resultChannels to minimum between resultChannels, TranscodingMaxAudioChannels, transcoderChannelL |
| 0 | 2813 | | resultChannels = transcoderChannelLimit < resultChannels ? transcoderChannelLimit : resultChannels ?? tr |
| | 2814 | |
|
| 0 | 2815 | | if (request.TranscodingMaxAudioChannels < resultChannels) |
| | 2816 | | { |
| 0 | 2817 | | resultChannels = request.TranscodingMaxAudioChannels; |
| | 2818 | | } |
| | 2819 | |
|
| | 2820 | | // Avoid transcoding to audio channels other than 1ch, 2ch, 6ch (5.1 layout) and 8ch (7.1 layout). |
| | 2821 | | // https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_d |
| 0 | 2822 | | if (state.TranscodingType != TranscodingJobType.Progressive |
| 0 | 2823 | | && ((resultChannels > 2 && resultChannels < 6) || resultChannels == 7)) |
| | 2824 | | { |
| | 2825 | | // We can let FFMpeg supply an extra LFE channel for 5ch and 7ch to make them 5.1 and 7.1 |
| 0 | 2826 | | if (resultChannels == 5) |
| | 2827 | | { |
| 0 | 2828 | | resultChannels = 6; |
| | 2829 | | } |
| 0 | 2830 | | else if (resultChannels == 7) |
| | 2831 | | { |
| 0 | 2832 | | resultChannels = 8; |
| | 2833 | | } |
| | 2834 | | else |
| | 2835 | | { |
| | 2836 | | // For other weird layout, just downmix to stereo for compatibility |
| 0 | 2837 | | resultChannels = 2; |
| | 2838 | | } |
| | 2839 | | } |
| | 2840 | | } |
| | 2841 | |
|
| 0 | 2842 | | return resultChannels; |
| | 2843 | | } |
| | 2844 | |
|
| | 2845 | | /// <summary> |
| | 2846 | | /// Enforces the resolution limit. |
| | 2847 | | /// </summary> |
| | 2848 | | /// <param name="state">The state.</param> |
| | 2849 | | public void EnforceResolutionLimit(EncodingJobInfo state) |
| | 2850 | | { |
| 0 | 2851 | | var videoRequest = state.BaseRequest; |
| | 2852 | |
|
| | 2853 | | // Switch the incoming params to be ceilings rather than fixed values |
| 0 | 2854 | | videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width; |
| 0 | 2855 | | videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height; |
| | 2856 | |
|
| 0 | 2857 | | videoRequest.Width = null; |
| 0 | 2858 | | videoRequest.Height = null; |
| 0 | 2859 | | } |
| | 2860 | |
|
| | 2861 | | /// <summary> |
| | 2862 | | /// Gets the fast seek command line parameter. |
| | 2863 | | /// </summary> |
| | 2864 | | /// <param name="state">The state.</param> |
| | 2865 | | /// <param name="options">The options.</param> |
| | 2866 | | /// <param name="segmentContainer">Segment Container.</param> |
| | 2867 | | /// <returns>System.String.</returns> |
| | 2868 | | /// <value>The fast seek command line parameter.</value> |
| | 2869 | | public string GetFastSeekCommandLineParameter(EncodingJobInfo state, EncodingOptions options, string segmentCont |
| | 2870 | | { |
| 0 | 2871 | | var time = state.BaseRequest.StartTimeTicks ?? 0; |
| 0 | 2872 | | var maxTime = state.RunTimeTicks ?? 0; |
| 0 | 2873 | | var seekParam = string.Empty; |
| | 2874 | |
|
| 0 | 2875 | | if (time > 0) |
| | 2876 | | { |
| | 2877 | | // For direct streaming/remuxing, we seek at the exact position of the keyframe |
| | 2878 | | // However, ffmpeg will seek to previous keyframe when the exact time is the input |
| | 2879 | | // Workaround this by adding 0.5s offset to the seeking time to get the exact keyframe on most videos. |
| | 2880 | | // This will help subtitle syncing. |
| 0 | 2881 | | var isHlsRemuxing = state.IsVideoRequest && state.TranscodingType is TranscodingJobType.Hls && IsCopyCod |
| 0 | 2882 | | var seekTick = isHlsRemuxing ? time + 5000000L : time; |
| | 2883 | |
|
| | 2884 | | // Seeking beyond EOF makes no sense in transcoding. Clamp the seekTick value to |
| | 2885 | | // [0, RuntimeTicks - 5.0s], so that the muxer gets packets and avoid error codes. |
| 0 | 2886 | | if (maxTime > 0) |
| | 2887 | | { |
| 0 | 2888 | | seekTick = Math.Clamp(seekTick, 0, Math.Max(maxTime - 50000000L, 0)); |
| | 2889 | | } |
| | 2890 | |
|
| 0 | 2891 | | seekParam += string.Format(CultureInfo.InvariantCulture, "-ss {0}", _mediaEncoder.GetTimeParameter(seekT |
| | 2892 | |
|
| 0 | 2893 | | if (state.IsVideoRequest) |
| | 2894 | | { |
| 0 | 2895 | | var outputVideoCodec = GetVideoEncoder(state, options); |
| 0 | 2896 | | var segmentFormat = GetSegmentFileExtension(segmentContainer).TrimStart('.'); |
| | 2897 | |
|
| | 2898 | | // Important: If this is ever re-enabled, make sure not to use it with wtv because it breaks seeking |
| | 2899 | | // Disable -noaccurate_seek on mpegts container due to the timestamps issue on some clients, |
| | 2900 | | // but it's still required for fMP4 container otherwise the audio can't be synced to the video. |
| 0 | 2901 | | if (!string.Equals(state.InputContainer, "wtv", StringComparison.OrdinalIgnoreCase) |
| 0 | 2902 | | && !string.Equals(segmentFormat, "ts", StringComparison.OrdinalIgnoreCase) |
| 0 | 2903 | | && state.TranscodingType != TranscodingJobType.Progressive |
| 0 | 2904 | | && !state.EnableBreakOnNonKeyFrames(outputVideoCodec) |
| 0 | 2905 | | && (state.BaseRequest.StartTimeTicks ?? 0) > 0) |
| | 2906 | | { |
| 0 | 2907 | | seekParam += " -noaccurate_seek"; |
| | 2908 | | } |
| | 2909 | | } |
| | 2910 | | } |
| | 2911 | |
|
| 0 | 2912 | | return seekParam; |
| | 2913 | | } |
| | 2914 | |
|
| | 2915 | | /// <summary> |
| | 2916 | | /// Gets the map args. |
| | 2917 | | /// </summary> |
| | 2918 | | /// <param name="state">The state.</param> |
| | 2919 | | /// <returns>System.String.</returns> |
| | 2920 | | public string GetMapArgs(EncodingJobInfo state) |
| | 2921 | | { |
| | 2922 | | // If we don't have known media info |
| | 2923 | | // If input is video, use -sn to drop subtitles |
| | 2924 | | // Otherwise just return empty |
| 0 | 2925 | | if (state.VideoStream is null && state.AudioStream is null) |
| | 2926 | | { |
| 0 | 2927 | | return state.IsInputVideo ? "-sn" : string.Empty; |
| | 2928 | | } |
| | 2929 | |
|
| | 2930 | | // We have media info, but we don't know the stream index |
| 0 | 2931 | | if (state.VideoStream is not null && state.VideoStream.Index == -1) |
| | 2932 | | { |
| 0 | 2933 | | return "-sn"; |
| | 2934 | | } |
| | 2935 | |
|
| | 2936 | | // We have media info, but we don't know the stream index |
| 0 | 2937 | | if (state.AudioStream is not null && state.AudioStream.Index == -1) |
| | 2938 | | { |
| 0 | 2939 | | return state.IsInputVideo ? "-sn" : string.Empty; |
| | 2940 | | } |
| | 2941 | |
|
| 0 | 2942 | | var args = string.Empty; |
| | 2943 | |
|
| 0 | 2944 | | if (state.VideoStream is not null) |
| | 2945 | | { |
| 0 | 2946 | | int videoStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.VideoStream); |
| | 2947 | |
|
| 0 | 2948 | | args += string.Format( |
| 0 | 2949 | | CultureInfo.InvariantCulture, |
| 0 | 2950 | | "-map 0:{0}", |
| 0 | 2951 | | videoStreamIndex); |
| | 2952 | | } |
| | 2953 | | else |
| | 2954 | | { |
| | 2955 | | // No known video stream |
| 0 | 2956 | | args += "-vn"; |
| | 2957 | | } |
| | 2958 | |
|
| 0 | 2959 | | if (state.AudioStream is not null) |
| | 2960 | | { |
| 0 | 2961 | | int audioStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.AudioStream); |
| 0 | 2962 | | if (state.AudioStream.IsExternal) |
| | 2963 | | { |
| 0 | 2964 | | bool hasExternalGraphicsSubs = state.SubtitleStream is not null |
| 0 | 2965 | | && ShouldEncodeSubtitle(state) |
| 0 | 2966 | | && state.SubtitleStream.IsExternal |
| 0 | 2967 | | && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 2968 | | int externalAudioMapIndex = hasExternalGraphicsSubs ? 2 : 1; |
| | 2969 | |
|
| 0 | 2970 | | args += string.Format( |
| 0 | 2971 | | CultureInfo.InvariantCulture, |
| 0 | 2972 | | " -map {0}:{1}", |
| 0 | 2973 | | externalAudioMapIndex, |
| 0 | 2974 | | audioStreamIndex); |
| | 2975 | | } |
| | 2976 | | else |
| | 2977 | | { |
| 0 | 2978 | | args += string.Format( |
| 0 | 2979 | | CultureInfo.InvariantCulture, |
| 0 | 2980 | | " -map 0:{0}", |
| 0 | 2981 | | audioStreamIndex); |
| | 2982 | | } |
| | 2983 | | } |
| | 2984 | | else |
| | 2985 | | { |
| 0 | 2986 | | args += " -map -0:a"; |
| | 2987 | | } |
| | 2988 | |
|
| 0 | 2989 | | var subtitleMethod = state.SubtitleDeliveryMethod; |
| 0 | 2990 | | if (state.SubtitleStream is null || subtitleMethod == SubtitleDeliveryMethod.Hls) |
| | 2991 | | { |
| 0 | 2992 | | args += " -map -0:s"; |
| | 2993 | | } |
| 0 | 2994 | | else if (subtitleMethod == SubtitleDeliveryMethod.Embed) |
| | 2995 | | { |
| 0 | 2996 | | int subtitleStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.SubtitleStream); |
| | 2997 | |
|
| 0 | 2998 | | args += string.Format( |
| 0 | 2999 | | CultureInfo.InvariantCulture, |
| 0 | 3000 | | " -map 0:{0}", |
| 0 | 3001 | | subtitleStreamIndex); |
| | 3002 | | } |
| 0 | 3003 | | else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream) |
| | 3004 | | { |
| 0 | 3005 | | int externalSubtitleStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.SubtitleStream); |
| | 3006 | |
|
| 0 | 3007 | | args += string.Format( |
| 0 | 3008 | | CultureInfo.InvariantCulture, |
| 0 | 3009 | | " -map 1:{0} -sn", |
| 0 | 3010 | | externalSubtitleStreamIndex); |
| | 3011 | | } |
| | 3012 | |
|
| 0 | 3013 | | return args; |
| | 3014 | | } |
| | 3015 | |
|
| | 3016 | | /// <summary> |
| | 3017 | | /// Gets the negative map args by filters. |
| | 3018 | | /// </summary> |
| | 3019 | | /// <param name="state">The state.</param> |
| | 3020 | | /// <param name="videoProcessFilters">The videoProcessFilters.</param> |
| | 3021 | | /// <returns>System.String.</returns> |
| | 3022 | | public string GetNegativeMapArgsByFilters(EncodingJobInfo state, string videoProcessFilters) |
| | 3023 | | { |
| 0 | 3024 | | string args = string.Empty; |
| | 3025 | |
|
| | 3026 | | // http://ffmpeg.org/ffmpeg-all.html#toc-Complex-filtergraphs-1 |
| 0 | 3027 | | if (state.VideoStream is not null && videoProcessFilters.Contains("-filter_complex", StringComparison.Ordina |
| | 3028 | | { |
| 0 | 3029 | | int videoStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.VideoStream); |
| | 3030 | |
|
| 0 | 3031 | | args += string.Format( |
| 0 | 3032 | | CultureInfo.InvariantCulture, |
| 0 | 3033 | | "-map -0:{0} ", |
| 0 | 3034 | | videoStreamIndex); |
| | 3035 | | } |
| | 3036 | |
|
| 0 | 3037 | | return args; |
| | 3038 | | } |
| | 3039 | |
|
| | 3040 | | /// <summary> |
| | 3041 | | /// Determines which stream will be used for playback. |
| | 3042 | | /// </summary> |
| | 3043 | | /// <param name="allStream">All stream.</param> |
| | 3044 | | /// <param name="desiredIndex">Index of the desired.</param> |
| | 3045 | | /// <param name="type">The type.</param> |
| | 3046 | | /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param> |
| | 3047 | | /// <returns>MediaStream.</returns> |
| | 3048 | | public MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, b |
| | 3049 | | { |
| 0 | 3050 | | var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList(); |
| | 3051 | |
|
| 0 | 3052 | | if (desiredIndex.HasValue) |
| | 3053 | | { |
| 0 | 3054 | | var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value); |
| | 3055 | |
|
| 0 | 3056 | | if (stream is not null) |
| | 3057 | | { |
| 0 | 3058 | | return stream; |
| | 3059 | | } |
| | 3060 | | } |
| | 3061 | |
|
| 0 | 3062 | | if (returnFirstIfNoIndex && type == MediaStreamType.Audio) |
| | 3063 | | { |
| 0 | 3064 | | return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ?? |
| 0 | 3065 | | streams.FirstOrDefault(); |
| | 3066 | | } |
| | 3067 | |
|
| | 3068 | | // Just return the first one |
| 0 | 3069 | | return returnFirstIfNoIndex ? streams.FirstOrDefault() : null; |
| | 3070 | | } |
| | 3071 | |
|
| | 3072 | | public static (int? Width, int? Height) GetFixedOutputSize( |
| | 3073 | | int? videoWidth, |
| | 3074 | | int? videoHeight, |
| | 3075 | | int? requestedWidth, |
| | 3076 | | int? requestedHeight, |
| | 3077 | | int? requestedMaxWidth, |
| | 3078 | | int? requestedMaxHeight) |
| | 3079 | | { |
| 0 | 3080 | | if (!videoWidth.HasValue && !requestedWidth.HasValue) |
| | 3081 | | { |
| 0 | 3082 | | return (null, null); |
| | 3083 | | } |
| | 3084 | |
|
| 0 | 3085 | | if (!videoHeight.HasValue && !requestedHeight.HasValue) |
| | 3086 | | { |
| 0 | 3087 | | return (null, null); |
| | 3088 | | } |
| | 3089 | |
|
| 0 | 3090 | | int inputWidth = Convert.ToInt32(videoWidth ?? requestedWidth, CultureInfo.InvariantCulture); |
| 0 | 3091 | | int inputHeight = Convert.ToInt32(videoHeight ?? requestedHeight, CultureInfo.InvariantCulture); |
| 0 | 3092 | | int outputWidth = requestedWidth ?? inputWidth; |
| 0 | 3093 | | int outputHeight = requestedHeight ?? inputHeight; |
| | 3094 | |
|
| | 3095 | | // Don't transcode video to bigger than 4k when using HW. |
| 0 | 3096 | | int maximumWidth = Math.Min(requestedMaxWidth ?? outputWidth, 4096); |
| 0 | 3097 | | int maximumHeight = Math.Min(requestedMaxHeight ?? outputHeight, 4096); |
| | 3098 | |
|
| 0 | 3099 | | if (outputWidth > maximumWidth || outputHeight > maximumHeight) |
| | 3100 | | { |
| 0 | 3101 | | var scaleW = (double)maximumWidth / outputWidth; |
| 0 | 3102 | | var scaleH = (double)maximumHeight / outputHeight; |
| 0 | 3103 | | var scale = Math.Min(scaleW, scaleH); |
| 0 | 3104 | | outputWidth = Math.Min(maximumWidth, Convert.ToInt32(outputWidth * scale)); |
| 0 | 3105 | | outputHeight = Math.Min(maximumHeight, Convert.ToInt32(outputHeight * scale)); |
| | 3106 | | } |
| | 3107 | |
|
| 0 | 3108 | | outputWidth = 2 * (outputWidth / 2); |
| 0 | 3109 | | outputHeight = 2 * (outputHeight / 2); |
| | 3110 | |
|
| 0 | 3111 | | return (outputWidth, outputHeight); |
| | 3112 | | } |
| | 3113 | |
|
| | 3114 | | public static bool IsScaleRatioSupported( |
| | 3115 | | int? videoWidth, |
| | 3116 | | int? videoHeight, |
| | 3117 | | int? requestedWidth, |
| | 3118 | | int? requestedHeight, |
| | 3119 | | int? requestedMaxWidth, |
| | 3120 | | int? requestedMaxHeight, |
| | 3121 | | double? maxScaleRatio) |
| | 3122 | | { |
| 0 | 3123 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 3124 | | videoWidth, |
| 0 | 3125 | | videoHeight, |
| 0 | 3126 | | requestedWidth, |
| 0 | 3127 | | requestedHeight, |
| 0 | 3128 | | requestedMaxWidth, |
| 0 | 3129 | | requestedMaxHeight); |
| | 3130 | |
|
| 0 | 3131 | | if (!videoWidth.HasValue |
| 0 | 3132 | | || !videoHeight.HasValue |
| 0 | 3133 | | || !outWidth.HasValue |
| 0 | 3134 | | || !outHeight.HasValue |
| 0 | 3135 | | || !maxScaleRatio.HasValue |
| 0 | 3136 | | || (maxScaleRatio.Value < 1.0f)) |
| | 3137 | | { |
| 0 | 3138 | | return false; |
| | 3139 | | } |
| | 3140 | |
|
| 0 | 3141 | | var minScaleRatio = 1.0f / maxScaleRatio; |
| 0 | 3142 | | var scaleRatioW = (double)outWidth / (double)videoWidth; |
| 0 | 3143 | | var scaleRatioH = (double)outHeight / (double)videoHeight; |
| | 3144 | |
|
| 0 | 3145 | | if (scaleRatioW < minScaleRatio |
| 0 | 3146 | | || scaleRatioW > maxScaleRatio |
| 0 | 3147 | | || scaleRatioH < minScaleRatio |
| 0 | 3148 | | || scaleRatioH > maxScaleRatio) |
| | 3149 | | { |
| 0 | 3150 | | return false; |
| | 3151 | | } |
| | 3152 | |
|
| 0 | 3153 | | return true; |
| | 3154 | | } |
| | 3155 | |
|
| | 3156 | | public static string GetHwScaleFilter( |
| | 3157 | | string hwScalePrefix, |
| | 3158 | | string hwScaleSuffix, |
| | 3159 | | string videoFormat, |
| | 3160 | | bool swapOutputWandH, |
| | 3161 | | int? videoWidth, |
| | 3162 | | int? videoHeight, |
| | 3163 | | int? requestedWidth, |
| | 3164 | | int? requestedHeight, |
| | 3165 | | int? requestedMaxWidth, |
| | 3166 | | int? requestedMaxHeight) |
| | 3167 | | { |
| 0 | 3168 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 3169 | | videoWidth, |
| 0 | 3170 | | videoHeight, |
| 0 | 3171 | | requestedWidth, |
| 0 | 3172 | | requestedHeight, |
| 0 | 3173 | | requestedMaxWidth, |
| 0 | 3174 | | requestedMaxHeight); |
| | 3175 | |
|
| 0 | 3176 | | var isFormatFixed = !string.IsNullOrEmpty(videoFormat); |
| 0 | 3177 | | var isSizeFixed = !videoWidth.HasValue |
| 0 | 3178 | | || outWidth.Value != videoWidth.Value |
| 0 | 3179 | | || !videoHeight.HasValue |
| 0 | 3180 | | || outHeight.Value != videoHeight.Value; |
| | 3181 | |
|
| 0 | 3182 | | var swpOutW = swapOutputWandH ? outHeight.Value : outWidth.Value; |
| 0 | 3183 | | var swpOutH = swapOutputWandH ? outWidth.Value : outHeight.Value; |
| | 3184 | |
|
| 0 | 3185 | | var arg1 = isSizeFixed ? $"=w={swpOutW}:h={swpOutH}" : string.Empty; |
| 0 | 3186 | | var arg2 = isFormatFixed ? $"format={videoFormat}" : string.Empty; |
| 0 | 3187 | | if (isFormatFixed) |
| | 3188 | | { |
| 0 | 3189 | | arg2 = (isSizeFixed ? ':' : '=') + arg2; |
| | 3190 | | } |
| | 3191 | |
|
| 0 | 3192 | | if (!string.IsNullOrEmpty(hwScaleSuffix) && (isSizeFixed || isFormatFixed)) |
| | 3193 | | { |
| 0 | 3194 | | return string.Format( |
| 0 | 3195 | | CultureInfo.InvariantCulture, |
| 0 | 3196 | | "{0}_{1}{2}{3}", |
| 0 | 3197 | | hwScalePrefix ?? "scale", |
| 0 | 3198 | | hwScaleSuffix, |
| 0 | 3199 | | arg1, |
| 0 | 3200 | | arg2); |
| | 3201 | | } |
| | 3202 | |
|
| 0 | 3203 | | return string.Empty; |
| | 3204 | | } |
| | 3205 | |
|
| | 3206 | | public static string GetGraphicalSubPreProcessFilters( |
| | 3207 | | int? videoWidth, |
| | 3208 | | int? videoHeight, |
| | 3209 | | int? subtitleWidth, |
| | 3210 | | int? subtitleHeight, |
| | 3211 | | int? requestedWidth, |
| | 3212 | | int? requestedHeight, |
| | 3213 | | int? requestedMaxWidth, |
| | 3214 | | int? requestedMaxHeight) |
| | 3215 | | { |
| 0 | 3216 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 3217 | | videoWidth, |
| 0 | 3218 | | videoHeight, |
| 0 | 3219 | | requestedWidth, |
| 0 | 3220 | | requestedHeight, |
| 0 | 3221 | | requestedMaxWidth, |
| 0 | 3222 | | requestedMaxHeight); |
| | 3223 | |
|
| 0 | 3224 | | if (!outWidth.HasValue |
| 0 | 3225 | | || !outHeight.HasValue |
| 0 | 3226 | | || outWidth.Value <= 0 |
| 0 | 3227 | | || outHeight.Value <= 0) |
| | 3228 | | { |
| 0 | 3229 | | return string.Empty; |
| | 3230 | | } |
| | 3231 | |
|
| | 3232 | | // Automatically add padding based on subtitle input |
| 0 | 3233 | | var filters = @"scale,scale=-1:{1}:fast_bilinear,crop,pad=max({0}\,iw):max({1}\,ih):(ow-iw)/2:(oh-ih)/2:blac |
| | 3234 | |
|
| 0 | 3235 | | if (subtitleWidth.HasValue |
| 0 | 3236 | | && subtitleHeight.HasValue |
| 0 | 3237 | | && subtitleWidth.Value > 0 |
| 0 | 3238 | | && subtitleHeight.Value > 0) |
| | 3239 | | { |
| 0 | 3240 | | var videoDar = (double)outWidth.Value / outHeight.Value; |
| 0 | 3241 | | var subtitleDar = (double)subtitleWidth.Value / subtitleHeight.Value; |
| | 3242 | |
|
| | 3243 | | // No need to add padding when DAR is the same -> 1080p PGSSUB on 2160p video |
| 0 | 3244 | | if (Math.Abs(videoDar - subtitleDar) < 0.01f) |
| | 3245 | | { |
| 0 | 3246 | | filters = @"scale,scale={0}:{1}:fast_bilinear"; |
| | 3247 | | } |
| | 3248 | | } |
| | 3249 | |
|
| 0 | 3250 | | return string.Format( |
| 0 | 3251 | | CultureInfo.InvariantCulture, |
| 0 | 3252 | | filters, |
| 0 | 3253 | | outWidth.Value, |
| 0 | 3254 | | outHeight.Value); |
| | 3255 | | } |
| | 3256 | |
|
| | 3257 | | public static string GetAlphaSrcFilter( |
| | 3258 | | EncodingJobInfo state, |
| | 3259 | | int? videoWidth, |
| | 3260 | | int? videoHeight, |
| | 3261 | | int? requestedWidth, |
| | 3262 | | int? requestedHeight, |
| | 3263 | | int? requestedMaxWidth, |
| | 3264 | | int? requestedMaxHeight, |
| | 3265 | | float? framerate) |
| | 3266 | | { |
| 0 | 3267 | | var reqTicks = state.BaseRequest.StartTimeTicks ?? 0; |
| 0 | 3268 | | var startTime = TimeSpan.FromTicks(reqTicks).ToString(@"hh\\\:mm\\\:ss\\\.fff", CultureInfo.InvariantCulture |
| 0 | 3269 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 3270 | | videoWidth, |
| 0 | 3271 | | videoHeight, |
| 0 | 3272 | | requestedWidth, |
| 0 | 3273 | | requestedHeight, |
| 0 | 3274 | | requestedMaxWidth, |
| 0 | 3275 | | requestedMaxHeight); |
| | 3276 | |
|
| 0 | 3277 | | if (outWidth.HasValue && outHeight.HasValue) |
| | 3278 | | { |
| 0 | 3279 | | return string.Format( |
| 0 | 3280 | | CultureInfo.InvariantCulture, |
| 0 | 3281 | | "alphasrc=s={0}x{1}:r={2}:start='{3}'", |
| 0 | 3282 | | outWidth.Value, |
| 0 | 3283 | | outHeight.Value, |
| 0 | 3284 | | framerate ?? 25, |
| 0 | 3285 | | reqTicks > 0 ? startTime : 0); |
| | 3286 | | } |
| | 3287 | |
|
| 0 | 3288 | | return string.Empty; |
| | 3289 | | } |
| | 3290 | |
|
| | 3291 | | public static string GetSwScaleFilter( |
| | 3292 | | EncodingJobInfo state, |
| | 3293 | | EncodingOptions options, |
| | 3294 | | string videoEncoder, |
| | 3295 | | int? videoWidth, |
| | 3296 | | int? videoHeight, |
| | 3297 | | Video3DFormat? threedFormat, |
| | 3298 | | int? requestedWidth, |
| | 3299 | | int? requestedHeight, |
| | 3300 | | int? requestedMaxWidth, |
| | 3301 | | int? requestedMaxHeight) |
| | 3302 | | { |
| 0 | 3303 | | var isV4l2 = string.Equals(videoEncoder, "h264_v4l2m2m", StringComparison.OrdinalIgnoreCase); |
| 0 | 3304 | | var isMjpeg = videoEncoder is not null && videoEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase) |
| 0 | 3305 | | var scaleVal = isV4l2 ? 64 : 2; |
| 0 | 3306 | | var targetAr = isMjpeg ? "(a*sar)" : "a"; // manually calculate AR when using mjpeg encoder |
| | 3307 | |
|
| | 3308 | | // If fixed dimensions were supplied |
| 0 | 3309 | | if (requestedWidth.HasValue && requestedHeight.HasValue) |
| | 3310 | | { |
| 0 | 3311 | | if (isV4l2) |
| | 3312 | | { |
| 0 | 3313 | | var widthParam = requestedWidth.Value.ToString(CultureInfo.InvariantCulture); |
| 0 | 3314 | | var heightParam = requestedHeight.Value.ToString(CultureInfo.InvariantCulture); |
| | 3315 | |
|
| 0 | 3316 | | return string.Format( |
| 0 | 3317 | | CultureInfo.InvariantCulture, |
| 0 | 3318 | | "scale=trunc({0}/64)*64:trunc({1}/2)*2", |
| 0 | 3319 | | widthParam, |
| 0 | 3320 | | heightParam); |
| | 3321 | | } |
| | 3322 | |
|
| 0 | 3323 | | return GetFixedSwScaleFilter(threedFormat, requestedWidth.Value, requestedHeight.Value); |
| | 3324 | | } |
| | 3325 | |
|
| | 3326 | | // If Max dimensions were supplied, for width selects lowest even number between input width and width req s |
| | 3327 | |
|
| 0 | 3328 | | if (requestedMaxWidth.HasValue && requestedMaxHeight.HasValue) |
| | 3329 | | { |
| 0 | 3330 | | var maxWidthParam = requestedMaxWidth.Value.ToString(CultureInfo.InvariantCulture); |
| 0 | 3331 | | var maxHeightParam = requestedMaxHeight.Value.ToString(CultureInfo.InvariantCulture); |
| | 3332 | |
|
| 0 | 3333 | | return string.Format( |
| 0 | 3334 | | CultureInfo.InvariantCulture, |
| 0 | 3335 | | @"scale=trunc(min(max(iw\,ih*{3})\,min({0}\,{1}*{3}))/{2})*{2}:trunc(min(max(iw/{3}\,ih)\,min({0}/{3 |
| 0 | 3336 | | maxWidthParam, |
| 0 | 3337 | | maxHeightParam, |
| 0 | 3338 | | scaleVal, |
| 0 | 3339 | | targetAr); |
| | 3340 | | } |
| | 3341 | |
|
| | 3342 | | // If a fixed width was requested |
| 0 | 3343 | | if (requestedWidth.HasValue) |
| | 3344 | | { |
| 0 | 3345 | | if (threedFormat.HasValue) |
| | 3346 | | { |
| | 3347 | | // This method can handle 0 being passed in for the requested height |
| 0 | 3348 | | return GetFixedSwScaleFilter(threedFormat, requestedWidth.Value, 0); |
| | 3349 | | } |
| | 3350 | |
|
| 0 | 3351 | | var widthParam = requestedWidth.Value.ToString(CultureInfo.InvariantCulture); |
| | 3352 | |
|
| 0 | 3353 | | return string.Format( |
| 0 | 3354 | | CultureInfo.InvariantCulture, |
| 0 | 3355 | | "scale={0}:trunc(ow/{1}/2)*2", |
| 0 | 3356 | | widthParam, |
| 0 | 3357 | | targetAr); |
| | 3358 | | } |
| | 3359 | |
|
| | 3360 | | // If a fixed height was requested |
| 0 | 3361 | | if (requestedHeight.HasValue) |
| | 3362 | | { |
| 0 | 3363 | | var heightParam = requestedHeight.Value.ToString(CultureInfo.InvariantCulture); |
| | 3364 | |
|
| 0 | 3365 | | return string.Format( |
| 0 | 3366 | | CultureInfo.InvariantCulture, |
| 0 | 3367 | | "scale=trunc(oh*{2}/{1})*{1}:{0}", |
| 0 | 3368 | | heightParam, |
| 0 | 3369 | | scaleVal, |
| 0 | 3370 | | targetAr); |
| | 3371 | | } |
| | 3372 | |
|
| | 3373 | | // If a max width was requested |
| 0 | 3374 | | if (requestedMaxWidth.HasValue) |
| | 3375 | | { |
| 0 | 3376 | | var maxWidthParam = requestedMaxWidth.Value.ToString(CultureInfo.InvariantCulture); |
| | 3377 | |
|
| 0 | 3378 | | return string.Format( |
| 0 | 3379 | | CultureInfo.InvariantCulture, |
| 0 | 3380 | | @"scale=trunc(min(max(iw\,ih*{2})\,{0})/{1})*{1}:trunc(ow/{2}/2)*2", |
| 0 | 3381 | | maxWidthParam, |
| 0 | 3382 | | scaleVal, |
| 0 | 3383 | | targetAr); |
| | 3384 | | } |
| | 3385 | |
|
| | 3386 | | // If a max height was requested |
| 0 | 3387 | | if (requestedMaxHeight.HasValue) |
| | 3388 | | { |
| 0 | 3389 | | var maxHeightParam = requestedMaxHeight.Value.ToString(CultureInfo.InvariantCulture); |
| | 3390 | |
|
| 0 | 3391 | | return string.Format( |
| 0 | 3392 | | CultureInfo.InvariantCulture, |
| 0 | 3393 | | @"scale=trunc(oh*{2}/{1})*{1}:min(max(iw/{2}\,ih)\,{0})", |
| 0 | 3394 | | maxHeightParam, |
| 0 | 3395 | | scaleVal, |
| 0 | 3396 | | targetAr); |
| | 3397 | | } |
| | 3398 | |
|
| 0 | 3399 | | return string.Empty; |
| | 3400 | | } |
| | 3401 | |
|
| | 3402 | | private static string GetFixedSwScaleFilter(Video3DFormat? threedFormat, int requestedWidth, int requestedHeight |
| | 3403 | | { |
| 0 | 3404 | | var widthParam = requestedWidth.ToString(CultureInfo.InvariantCulture); |
| 0 | 3405 | | var heightParam = requestedHeight.ToString(CultureInfo.InvariantCulture); |
| | 3406 | |
|
| 0 | 3407 | | string filter = null; |
| | 3408 | |
|
| 0 | 3409 | | if (threedFormat.HasValue) |
| | 3410 | | { |
| 0 | 3411 | | switch (threedFormat.Value) |
| | 3412 | | { |
| | 3413 | | case Video3DFormat.HalfSideBySide: |
| 0 | 3414 | | filter = @"crop=iw/2:ih:0:0,scale=(iw*2):ih,setdar=dar=a,crop=min(iw\,ih*dar):min(ih\,iw/dar):(i |
| | 3415 | | // hsbs crop width in half,scale to correct size, set the display aspect,crop out any black bars |
| 0 | 3416 | | break; |
| | 3417 | | case Video3DFormat.FullSideBySide: |
| 0 | 3418 | | filter = @"crop=iw/2:ih:0:0,setdar=dar=a,crop=min(iw\,ih*dar):min(ih\,iw/dar):(iw-min(iw\,iw*sar |
| | 3419 | | // fsbs crop width in half,set the display aspect,crop out any black bars we may have made the s |
| 0 | 3420 | | break; |
| | 3421 | | case Video3DFormat.HalfTopAndBottom: |
| 0 | 3422 | | filter = @"crop=iw:ih/2:0:0,scale=(iw*2):ih),setdar=dar=a,crop=min(iw\,ih*dar):min(ih\,iw/dar):( |
| | 3423 | | // htab crop height in half,scale to correct size, set the display aspect,crop out any black bar |
| 0 | 3424 | | break; |
| | 3425 | | case Video3DFormat.FullTopAndBottom: |
| 0 | 3426 | | filter = @"crop=iw:ih/2:0:0,setdar=dar=a,crop=min(iw\,ih*dar):min(ih\,iw/dar):(iw-min(iw\,iw*sar |
| | 3427 | | // ftab crop height in half, set the display aspect,crop out any black bars we may have made the |
| | 3428 | | break; |
| | 3429 | | default: |
| | 3430 | | break; |
| | 3431 | | } |
| | 3432 | | } |
| | 3433 | |
|
| | 3434 | | // default |
| 0 | 3435 | | if (filter is null) |
| | 3436 | | { |
| 0 | 3437 | | if (requestedHeight > 0) |
| | 3438 | | { |
| 0 | 3439 | | filter = "scale=trunc({0}/2)*2:trunc({1}/2)*2"; |
| | 3440 | | } |
| | 3441 | | else |
| | 3442 | | { |
| 0 | 3443 | | filter = "scale={0}:trunc({0}/a/2)*2"; |
| | 3444 | | } |
| | 3445 | | } |
| | 3446 | |
|
| 0 | 3447 | | return string.Format(CultureInfo.InvariantCulture, filter, widthParam, heightParam); |
| | 3448 | | } |
| | 3449 | |
|
| | 3450 | | public static string GetSwDeinterlaceFilter(EncodingJobInfo state, EncodingOptions options) |
| | 3451 | | { |
| 0 | 3452 | | var doubleRateDeint = options.DeinterlaceDoubleRate && state.VideoStream?.ReferenceFrameRate <= 30; |
| 0 | 3453 | | return string.Format( |
| 0 | 3454 | | CultureInfo.InvariantCulture, |
| 0 | 3455 | | "{0}={1}:-1:0", |
| 0 | 3456 | | options.DeinterlaceMethod.ToString().ToLowerInvariant(), |
| 0 | 3457 | | doubleRateDeint ? "1" : "0"); |
| | 3458 | | } |
| | 3459 | |
|
| | 3460 | | public string GetHwDeinterlaceFilter(EncodingJobInfo state, EncodingOptions options, string hwDeintSuffix) |
| | 3461 | | { |
| 0 | 3462 | | var doubleRateDeint = options.DeinterlaceDoubleRate && (state.VideoStream?.ReferenceFrameRate ?? 60) <= 30; |
| 0 | 3463 | | if (hwDeintSuffix.Contains("cuda", StringComparison.OrdinalIgnoreCase)) |
| | 3464 | | { |
| 0 | 3465 | | var useBwdif = options.DeinterlaceMethod == DeinterlaceMethod.bwdif && _mediaEncoder.SupportsFilter("bwd |
| | 3466 | |
|
| 0 | 3467 | | return string.Format( |
| 0 | 3468 | | CultureInfo.InvariantCulture, |
| 0 | 3469 | | "{0}_cuda={1}:-1:0", |
| 0 | 3470 | | useBwdif ? "bwdif" : "yadif", |
| 0 | 3471 | | doubleRateDeint ? "1" : "0"); |
| | 3472 | | } |
| | 3473 | |
|
| 0 | 3474 | | if (hwDeintSuffix.Contains("vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 3475 | | { |
| 0 | 3476 | | return string.Format( |
| 0 | 3477 | | CultureInfo.InvariantCulture, |
| 0 | 3478 | | "deinterlace_vaapi=rate={0}", |
| 0 | 3479 | | doubleRateDeint ? "field" : "frame"); |
| | 3480 | | } |
| | 3481 | |
|
| 0 | 3482 | | if (hwDeintSuffix.Contains("qsv", StringComparison.OrdinalIgnoreCase)) |
| | 3483 | | { |
| 0 | 3484 | | return "deinterlace_qsv=mode=2"; |
| | 3485 | | } |
| | 3486 | |
|
| 0 | 3487 | | if (hwDeintSuffix.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase)) |
| | 3488 | | { |
| 0 | 3489 | | var useBwdif = options.DeinterlaceMethod == DeinterlaceMethod.bwdif && _mediaEncoder.SupportsFilter("bwd |
| | 3490 | |
|
| 0 | 3491 | | return string.Format( |
| 0 | 3492 | | CultureInfo.InvariantCulture, |
| 0 | 3493 | | "{0}_videotoolbox={1}:-1:0", |
| 0 | 3494 | | useBwdif ? "bwdif" : "yadif", |
| 0 | 3495 | | doubleRateDeint ? "1" : "0"); |
| | 3496 | | } |
| | 3497 | |
|
| 0 | 3498 | | return string.Empty; |
| | 3499 | | } |
| | 3500 | |
|
| | 3501 | | private string GetHwTonemapFilter(EncodingOptions options, string hwTonemapSuffix, string videoFormat, bool forc |
| | 3502 | | { |
| 0 | 3503 | | if (string.IsNullOrEmpty(hwTonemapSuffix)) |
| | 3504 | | { |
| 0 | 3505 | | return string.Empty; |
| | 3506 | | } |
| | 3507 | |
|
| 0 | 3508 | | var args = string.Empty; |
| 0 | 3509 | | var algorithm = options.TonemappingAlgorithm.ToString().ToLowerInvariant(); |
| 0 | 3510 | | var mode = options.TonemappingMode.ToString().ToLowerInvariant(); |
| 0 | 3511 | | var range = forceFullRange ? TonemappingRange.pc : options.TonemappingRange; |
| 0 | 3512 | | var rangeString = range.ToString().ToLowerInvariant(); |
| | 3513 | |
|
| 0 | 3514 | | if (string.Equals(hwTonemapSuffix, "vaapi", StringComparison.OrdinalIgnoreCase)) |
| | 3515 | | { |
| 0 | 3516 | | var doVaVppProcamp = false; |
| 0 | 3517 | | var procampParams = string.Empty; |
| 0 | 3518 | | if (options.VppTonemappingBrightness != 0 |
| 0 | 3519 | | && options.VppTonemappingBrightness >= -100 |
| 0 | 3520 | | && options.VppTonemappingBrightness <= 100) |
| | 3521 | | { |
| 0 | 3522 | | procampParams += "procamp_vaapi=b={0}"; |
| 0 | 3523 | | doVaVppProcamp = true; |
| | 3524 | | } |
| | 3525 | |
|
| 0 | 3526 | | if (options.VppTonemappingContrast > 1 |
| 0 | 3527 | | && options.VppTonemappingContrast <= 10) |
| | 3528 | | { |
| 0 | 3529 | | procampParams += doVaVppProcamp ? ":c={1}" : "procamp_vaapi=c={1}"; |
| 0 | 3530 | | doVaVppProcamp = true; |
| | 3531 | | } |
| | 3532 | |
|
| 0 | 3533 | | args = procampParams + "{2}tonemap_vaapi=format={3}:p=bt709:t=bt709:m=bt709:extra_hw_frames=32"; |
| | 3534 | |
|
| 0 | 3535 | | return string.Format( |
| 0 | 3536 | | CultureInfo.InvariantCulture, |
| 0 | 3537 | | args, |
| 0 | 3538 | | options.VppTonemappingBrightness, |
| 0 | 3539 | | options.VppTonemappingContrast, |
| 0 | 3540 | | doVaVppProcamp ? "," : string.Empty, |
| 0 | 3541 | | videoFormat ?? "nv12"); |
| | 3542 | | } |
| | 3543 | | else |
| | 3544 | | { |
| 0 | 3545 | | args = "tonemap_{0}=format={1}:p=bt709:t=bt709:m=bt709:tonemap={2}:peak={3}:desat={4}"; |
| | 3546 | |
|
| 0 | 3547 | | var useLegacyTonemapModes = _mediaEncoder.EncoderVersion >= _minFFmpegOclCuTonemapMode |
| 0 | 3548 | | && _legacyTonemapModes.Contains(options.TonemappingMode); |
| | 3549 | |
|
| 0 | 3550 | | var useAdvancedTonemapModes = _mediaEncoder.EncoderVersion >= _minFFmpegAdvancedTonemapMode |
| 0 | 3551 | | && _advancedTonemapModes.Contains(options.TonemappingMode); |
| | 3552 | |
|
| 0 | 3553 | | if (useLegacyTonemapModes || useAdvancedTonemapModes) |
| | 3554 | | { |
| 0 | 3555 | | args += ":tonemap_mode={5}"; |
| | 3556 | | } |
| | 3557 | |
|
| 0 | 3558 | | if (options.TonemappingParam != 0) |
| | 3559 | | { |
| 0 | 3560 | | args += ":param={6}"; |
| | 3561 | | } |
| | 3562 | |
|
| 0 | 3563 | | if (range == TonemappingRange.tv || range == TonemappingRange.pc) |
| | 3564 | | { |
| 0 | 3565 | | args += ":range={7}"; |
| | 3566 | | } |
| | 3567 | | } |
| | 3568 | |
|
| 0 | 3569 | | return string.Format( |
| 0 | 3570 | | CultureInfo.InvariantCulture, |
| 0 | 3571 | | args, |
| 0 | 3572 | | hwTonemapSuffix, |
| 0 | 3573 | | videoFormat ?? "nv12", |
| 0 | 3574 | | algorithm, |
| 0 | 3575 | | options.TonemappingPeak, |
| 0 | 3576 | | options.TonemappingDesat, |
| 0 | 3577 | | mode, |
| 0 | 3578 | | options.TonemappingParam, |
| 0 | 3579 | | rangeString); |
| | 3580 | | } |
| | 3581 | |
|
| | 3582 | | private string GetLibplaceboFilter( |
| | 3583 | | EncodingOptions options, |
| | 3584 | | string videoFormat, |
| | 3585 | | bool doTonemap, |
| | 3586 | | int? videoWidth, |
| | 3587 | | int? videoHeight, |
| | 3588 | | int? requestedWidth, |
| | 3589 | | int? requestedHeight, |
| | 3590 | | int? requestedMaxWidth, |
| | 3591 | | int? requestedMaxHeight, |
| | 3592 | | bool forceFullRange) |
| | 3593 | | { |
| 0 | 3594 | | var (outWidth, outHeight) = GetFixedOutputSize( |
| 0 | 3595 | | videoWidth, |
| 0 | 3596 | | videoHeight, |
| 0 | 3597 | | requestedWidth, |
| 0 | 3598 | | requestedHeight, |
| 0 | 3599 | | requestedMaxWidth, |
| 0 | 3600 | | requestedMaxHeight); |
| | 3601 | |
|
| 0 | 3602 | | var isFormatFixed = !string.IsNullOrEmpty(videoFormat); |
| 0 | 3603 | | var isSizeFixed = !videoWidth.HasValue |
| 0 | 3604 | | || outWidth.Value != videoWidth.Value |
| 0 | 3605 | | || !videoHeight.HasValue |
| 0 | 3606 | | || outHeight.Value != videoHeight.Value; |
| | 3607 | |
|
| 0 | 3608 | | var sizeArg = isSizeFixed ? (":w=" + outWidth.Value + ":h=" + outHeight.Value) : string.Empty; |
| 0 | 3609 | | var formatArg = isFormatFixed ? (":format=" + videoFormat) : string.Empty; |
| 0 | 3610 | | var tonemapArg = string.Empty; |
| | 3611 | |
|
| 0 | 3612 | | if (doTonemap) |
| | 3613 | | { |
| 0 | 3614 | | var algorithm = options.TonemappingAlgorithm; |
| 0 | 3615 | | var algorithmString = "clip"; |
| 0 | 3616 | | var mode = options.TonemappingMode; |
| 0 | 3617 | | var range = forceFullRange ? TonemappingRange.pc : options.TonemappingRange; |
| | 3618 | |
|
| 0 | 3619 | | if (algorithm == TonemappingAlgorithm.bt2390) |
| | 3620 | | { |
| 0 | 3621 | | algorithmString = "bt.2390"; |
| | 3622 | | } |
| 0 | 3623 | | else if (algorithm != TonemappingAlgorithm.none) |
| | 3624 | | { |
| 0 | 3625 | | algorithmString = algorithm.ToString().ToLowerInvariant(); |
| | 3626 | | } |
| | 3627 | |
|
| 0 | 3628 | | tonemapArg = $":tonemapping={algorithmString}:peak_detect=0:color_primaries=bt709:color_trc=bt709:colors |
| | 3629 | |
|
| 0 | 3630 | | if (range == TonemappingRange.tv || range == TonemappingRange.pc) |
| | 3631 | | { |
| 0 | 3632 | | tonemapArg += ":range=" + range.ToString().ToLowerInvariant(); |
| | 3633 | | } |
| | 3634 | | } |
| | 3635 | |
|
| 0 | 3636 | | return string.Format( |
| 0 | 3637 | | CultureInfo.InvariantCulture, |
| 0 | 3638 | | "libplacebo=upscaler=none:downscaler=none{0}{1}{2}", |
| 0 | 3639 | | sizeArg, |
| 0 | 3640 | | formatArg, |
| 0 | 3641 | | tonemapArg); |
| | 3642 | | } |
| | 3643 | |
|
| | 3644 | | public string GetVideoTransposeDirection(EncodingJobInfo state) |
| | 3645 | | { |
| 0 | 3646 | | return (state.VideoStream?.Rotation ?? 0) switch |
| 0 | 3647 | | { |
| 0 | 3648 | | 90 => "cclock", |
| 0 | 3649 | | 180 => "reversal", |
| 0 | 3650 | | -90 => "clock", |
| 0 | 3651 | | -180 => "reversal", |
| 0 | 3652 | | _ => string.Empty |
| 0 | 3653 | | }; |
| | 3654 | | } |
| | 3655 | |
|
| | 3656 | | /// <summary> |
| | 3657 | | /// Gets the parameter of software filter chain. |
| | 3658 | | /// </summary> |
| | 3659 | | /// <param name="state">Encoding state.</param> |
| | 3660 | | /// <param name="options">Encoding options.</param> |
| | 3661 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 3662 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 3663 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetSwVidFilterChain( |
| | 3664 | | EncodingJobInfo state, |
| | 3665 | | EncodingOptions options, |
| | 3666 | | string vidEncoder) |
| | 3667 | | { |
| 0 | 3668 | | var inW = state.VideoStream?.Width; |
| 0 | 3669 | | var inH = state.VideoStream?.Height; |
| 0 | 3670 | | var reqW = state.BaseRequest.Width; |
| 0 | 3671 | | var reqH = state.BaseRequest.Height; |
| 0 | 3672 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 3673 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 3674 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 3675 | |
|
| 0 | 3676 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 3677 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 3678 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 3679 | | var isV4l2Encoder = vidEncoder.Contains("h264_v4l2m2m", StringComparison.OrdinalIgnoreCase); |
| | 3680 | |
|
| 0 | 3681 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 3682 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 3683 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 3684 | | var doToneMap = IsSwTonemapAvailable(state, options); |
| 0 | 3685 | | var requireDoviReshaping = doToneMap && state.VideoStream.VideoRangeType == VideoRangeType.DOVI; |
| | 3686 | |
|
| 0 | 3687 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 3688 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 3689 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| | 3690 | |
|
| 0 | 3691 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 3692 | | var swapWAndH = Math.Abs(rotation) == 90; |
| 0 | 3693 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 3694 | | var swpInH = swapWAndH ? inW : inH; |
| | 3695 | |
|
| | 3696 | | /* Make main filters for video stream */ |
| 0 | 3697 | | var mainFilters = new List<string>(); |
| | 3698 | |
|
| 0 | 3699 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doToneMap)); |
| | 3700 | |
|
| | 3701 | | // INPUT sw surface(memory/copy-back from vram) |
| | 3702 | | // sw deint |
| 0 | 3703 | | if (doDeintH2645) |
| | 3704 | | { |
| 0 | 3705 | | var deintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 3706 | | mainFilters.Add(deintFilter); |
| | 3707 | | } |
| | 3708 | |
|
| 0 | 3709 | | var outFormat = isSwDecoder ? "yuv420p" : "nv12"; |
| 0 | 3710 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, reqH, r |
| 0 | 3711 | | if (isVaapiEncoder) |
| | 3712 | | { |
| 0 | 3713 | | outFormat = "nv12"; |
| | 3714 | | } |
| 0 | 3715 | | else if (isV4l2Encoder) |
| | 3716 | | { |
| 0 | 3717 | | outFormat = "yuv420p"; |
| | 3718 | | } |
| | 3719 | |
|
| | 3720 | | // sw scale |
| 0 | 3721 | | mainFilters.Add(swScaleFilter); |
| | 3722 | |
|
| | 3723 | | // sw tonemap |
| 0 | 3724 | | if (doToneMap) |
| | 3725 | | { |
| | 3726 | | // tonemapx requires yuv420p10 input for dovi reshaping, let ffmpeg convert the frame when necessary |
| 0 | 3727 | | var tonemapFormat = requireDoviReshaping ? "yuv420p" : outFormat; |
| 0 | 3728 | | var tonemapArgString = "tonemapx=tonemap={0}:desat={1}:peak={2}:t=bt709:m=bt709:p=bt709:format={3}"; |
| | 3729 | |
|
| 0 | 3730 | | if (options.TonemappingParam != 0) |
| | 3731 | | { |
| 0 | 3732 | | tonemapArgString += ":param={4}"; |
| | 3733 | | } |
| | 3734 | |
|
| 0 | 3735 | | var range = options.TonemappingRange; |
| 0 | 3736 | | if (range == TonemappingRange.tv || range == TonemappingRange.pc) |
| | 3737 | | { |
| 0 | 3738 | | tonemapArgString += ":range={5}"; |
| | 3739 | | } |
| | 3740 | |
|
| 0 | 3741 | | var tonemapArgs = string.Format( |
| 0 | 3742 | | CultureInfo.InvariantCulture, |
| 0 | 3743 | | tonemapArgString, |
| 0 | 3744 | | options.TonemappingAlgorithm, |
| 0 | 3745 | | options.TonemappingDesat, |
| 0 | 3746 | | options.TonemappingPeak, |
| 0 | 3747 | | tonemapFormat, |
| 0 | 3748 | | options.TonemappingParam, |
| 0 | 3749 | | options.TonemappingRange); |
| | 3750 | |
|
| 0 | 3751 | | mainFilters.Add(tonemapArgs); |
| | 3752 | | } |
| | 3753 | | else |
| | 3754 | | { |
| | 3755 | | // OUTPUT yuv420p/nv12 surface(memory) |
| 0 | 3756 | | mainFilters.Add("format=" + outFormat); |
| | 3757 | | } |
| | 3758 | |
|
| | 3759 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 3760 | | var subFilters = new List<string>(); |
| 0 | 3761 | | var overlayFilters = new List<string>(); |
| 0 | 3762 | | if (hasTextSubs) |
| | 3763 | | { |
| | 3764 | | // subtitles=f='*.ass':alpha=0 |
| 0 | 3765 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 3766 | | mainFilters.Add(textSubtitlesFilter); |
| | 3767 | | } |
| 0 | 3768 | | else if (hasGraphicalSubs) |
| | 3769 | | { |
| 0 | 3770 | | var subW = state.SubtitleStream?.Width; |
| 0 | 3771 | | var subH = state.SubtitleStream?.Height; |
| 0 | 3772 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, reqMaxW |
| 0 | 3773 | | subFilters.Add(subPreProcFilters); |
| 0 | 3774 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 3775 | | } |
| | 3776 | |
|
| 0 | 3777 | | return (mainFilters, subFilters, overlayFilters); |
| | 3778 | | } |
| | 3779 | |
|
| | 3780 | | /// <summary> |
| | 3781 | | /// Gets the parameter of Nvidia NVENC filter chain. |
| | 3782 | | /// </summary> |
| | 3783 | | /// <param name="state">Encoding state.</param> |
| | 3784 | | /// <param name="options">Encoding options.</param> |
| | 3785 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 3786 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 3787 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetNvidiaVidFilterChain( |
| | 3788 | | EncodingJobInfo state, |
| | 3789 | | EncodingOptions options, |
| | 3790 | | string vidEncoder) |
| | 3791 | | { |
| 0 | 3792 | | if (options.HardwareAccelerationType != HardwareAccelerationType.nvenc) |
| | 3793 | | { |
| 0 | 3794 | | return (null, null, null); |
| | 3795 | | } |
| | 3796 | |
|
| 0 | 3797 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 3798 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 3799 | | var isSwEncoder = !vidEncoder.Contains("nvenc", StringComparison.OrdinalIgnoreCase); |
| | 3800 | |
|
| | 3801 | | // legacy cuvid pipeline(copy-back) |
| 0 | 3802 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 3803 | | || !IsCudaFullSupported() |
| 0 | 3804 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 3805 | | { |
| 0 | 3806 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 3807 | | } |
| | 3808 | |
|
| | 3809 | | // preferred nvdec/cuvid + cuda filters + nvenc pipeline |
| 0 | 3810 | | return GetNvidiaVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 3811 | | } |
| | 3812 | |
|
| | 3813 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetNvidiaVidFiltersPrefe |
| | 3814 | | EncodingJobInfo state, |
| | 3815 | | EncodingOptions options, |
| | 3816 | | string vidDecoder, |
| | 3817 | | string vidEncoder) |
| | 3818 | | { |
| 0 | 3819 | | var inW = state.VideoStream?.Width; |
| 0 | 3820 | | var inH = state.VideoStream?.Height; |
| 0 | 3821 | | var reqW = state.BaseRequest.Width; |
| 0 | 3822 | | var reqH = state.BaseRequest.Height; |
| 0 | 3823 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 3824 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 3825 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 3826 | |
|
| 0 | 3827 | | var isNvDecoder = vidDecoder.Contains("cuda", StringComparison.OrdinalIgnoreCase); |
| 0 | 3828 | | var isNvencEncoder = vidEncoder.Contains("nvenc", StringComparison.OrdinalIgnoreCase); |
| 0 | 3829 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 3830 | | var isSwEncoder = !isNvencEncoder; |
| 0 | 3831 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 3832 | | var isCuInCuOut = isNvDecoder && isNvencEncoder; |
| | 3833 | |
|
| 0 | 3834 | | var doubleRateDeint = options.DeinterlaceDoubleRate && (state.VideoStream?.ReferenceFrameRate ?? 60) <= 30; |
| 0 | 3835 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 3836 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 3837 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 3838 | | var doCuTonemap = IsHwTonemapAvailable(state, options); |
| | 3839 | |
|
| 0 | 3840 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 3841 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 3842 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 3843 | | var hasAssSubs = hasSubs |
| 0 | 3844 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 3845 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 3846 | | var subW = state.SubtitleStream?.Width; |
| 0 | 3847 | | var subH = state.SubtitleStream?.Height; |
| | 3848 | |
|
| 0 | 3849 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 3850 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 3851 | | var doCuTranspose = !string.IsNullOrEmpty(transposeDir) && _mediaEncoder.SupportsFilter("transpose_cuda"); |
| 0 | 3852 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isNvDecoder && doCuTranspose)); |
| 0 | 3853 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 3854 | | var swpInH = swapWAndH ? inW : inH; |
| | 3855 | |
|
| | 3856 | | /* Make main filters for video stream */ |
| 0 | 3857 | | var mainFilters = new List<string>(); |
| | 3858 | |
|
| 0 | 3859 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doCuTonemap)); |
| | 3860 | |
|
| 0 | 3861 | | if (isSwDecoder) |
| | 3862 | | { |
| | 3863 | | // INPUT sw surface(memory) |
| | 3864 | | // sw deint |
| 0 | 3865 | | if (doDeintH2645) |
| | 3866 | | { |
| 0 | 3867 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 3868 | | mainFilters.Add(swDeintFilter); |
| | 3869 | | } |
| | 3870 | |
|
| 0 | 3871 | | var outFormat = doCuTonemap ? "yuv420p10le" : "yuv420p"; |
| 0 | 3872 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| | 3873 | | // sw scale |
| 0 | 3874 | | mainFilters.Add(swScaleFilter); |
| 0 | 3875 | | mainFilters.Add($"format={outFormat}"); |
| | 3876 | |
|
| | 3877 | | // sw => hw |
| 0 | 3878 | | if (doCuTonemap) |
| | 3879 | | { |
| 0 | 3880 | | mainFilters.Add("hwupload=derive_device=cuda"); |
| | 3881 | | } |
| | 3882 | | } |
| | 3883 | |
|
| 0 | 3884 | | if (isNvDecoder) |
| | 3885 | | { |
| | 3886 | | // INPUT cuda surface(vram) |
| | 3887 | | // hw deint |
| 0 | 3888 | | if (doDeintH2645) |
| | 3889 | | { |
| 0 | 3890 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "cuda"); |
| 0 | 3891 | | mainFilters.Add(deintFilter); |
| | 3892 | | } |
| | 3893 | |
|
| | 3894 | | // hw transpose |
| 0 | 3895 | | if (doCuTranspose) |
| | 3896 | | { |
| 0 | 3897 | | mainFilters.Add($"transpose_cuda=dir={transposeDir}"); |
| | 3898 | | } |
| | 3899 | |
|
| 0 | 3900 | | var isRext = IsVideoStreamHevcRext(state); |
| 0 | 3901 | | var outFormat = doCuTonemap ? (isRext ? "p010" : string.Empty) : "yuv420p"; |
| 0 | 3902 | | var hwScaleFilter = GetHwScaleFilter("scale", "cuda", outFormat, false, swpInW, swpInH, reqW, reqH, reqM |
| | 3903 | | // hw scale |
| 0 | 3904 | | mainFilters.Add(hwScaleFilter); |
| | 3905 | | } |
| | 3906 | |
|
| | 3907 | | // hw tonemap |
| 0 | 3908 | | if (doCuTonemap) |
| | 3909 | | { |
| 0 | 3910 | | var tonemapFilter = GetHwTonemapFilter(options, "cuda", "yuv420p", isMjpegEncoder); |
| 0 | 3911 | | mainFilters.Add(tonemapFilter); |
| | 3912 | | } |
| | 3913 | |
|
| 0 | 3914 | | var memoryOutput = false; |
| 0 | 3915 | | var isUploadForCuTonemap = isSwDecoder && doCuTonemap; |
| 0 | 3916 | | if ((isNvDecoder && isSwEncoder) || (isUploadForCuTonemap && hasSubs)) |
| | 3917 | | { |
| 0 | 3918 | | memoryOutput = true; |
| | 3919 | |
|
| | 3920 | | // OUTPUT yuv420p surface(memory) |
| 0 | 3921 | | mainFilters.Add("hwdownload"); |
| 0 | 3922 | | mainFilters.Add("format=yuv420p"); |
| | 3923 | | } |
| | 3924 | |
|
| | 3925 | | // OUTPUT yuv420p surface(memory) |
| 0 | 3926 | | if (isSwDecoder && isNvencEncoder && !isUploadForCuTonemap) |
| | 3927 | | { |
| 0 | 3928 | | memoryOutput = true; |
| | 3929 | | } |
| | 3930 | |
|
| 0 | 3931 | | if (memoryOutput) |
| | 3932 | | { |
| | 3933 | | // text subtitles |
| 0 | 3934 | | if (hasTextSubs) |
| | 3935 | | { |
| 0 | 3936 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 3937 | | mainFilters.Add(textSubtitlesFilter); |
| | 3938 | | } |
| | 3939 | | } |
| | 3940 | |
|
| | 3941 | | // OUTPUT cuda(yuv420p) surface(vram) |
| | 3942 | |
|
| | 3943 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 3944 | | var subFilters = new List<string>(); |
| 0 | 3945 | | var overlayFilters = new List<string>(); |
| 0 | 3946 | | if (isCuInCuOut) |
| | 3947 | | { |
| 0 | 3948 | | if (hasSubs) |
| | 3949 | | { |
| 0 | 3950 | | if (hasGraphicalSubs) |
| | 3951 | | { |
| 0 | 3952 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 3953 | | subFilters.Add(subPreProcFilters); |
| 0 | 3954 | | subFilters.Add("format=yuva420p"); |
| | 3955 | | } |
| 0 | 3956 | | else if (hasTextSubs) |
| | 3957 | | { |
| 0 | 3958 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 3959 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 3960 | |
|
| | 3961 | | // alphasrc=s=1280x720:r=10:start=0,format=yuva420p,subtitles,hwupload |
| 0 | 3962 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subF |
| 0 | 3963 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 3964 | | subFilters.Add(alphaSrcFilter); |
| 0 | 3965 | | subFilters.Add("format=yuva420p"); |
| 0 | 3966 | | subFilters.Add(subTextSubtitlesFilter); |
| | 3967 | | } |
| | 3968 | |
|
| 0 | 3969 | | subFilters.Add("hwupload=derive_device=cuda"); |
| 0 | 3970 | | overlayFilters.Add("overlay_cuda=eof_action=pass:repeatlast=0"); |
| | 3971 | | } |
| | 3972 | | } |
| | 3973 | | else |
| | 3974 | | { |
| 0 | 3975 | | if (hasGraphicalSubs) |
| | 3976 | | { |
| 0 | 3977 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 3978 | | subFilters.Add(subPreProcFilters); |
| 0 | 3979 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 3980 | | } |
| | 3981 | | } |
| | 3982 | |
|
| 0 | 3983 | | return (mainFilters, subFilters, overlayFilters); |
| | 3984 | | } |
| | 3985 | |
|
| | 3986 | | /// <summary> |
| | 3987 | | /// Gets the parameter of AMD AMF filter chain. |
| | 3988 | | /// </summary> |
| | 3989 | | /// <param name="state">Encoding state.</param> |
| | 3990 | | /// <param name="options">Encoding options.</param> |
| | 3991 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 3992 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 3993 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAmdVidFilterChain( |
| | 3994 | | EncodingJobInfo state, |
| | 3995 | | EncodingOptions options, |
| | 3996 | | string vidEncoder) |
| | 3997 | | { |
| 0 | 3998 | | if (options.HardwareAccelerationType != HardwareAccelerationType.amf) |
| | 3999 | | { |
| 0 | 4000 | | return (null, null, null); |
| | 4001 | | } |
| | 4002 | |
|
| 0 | 4003 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 4004 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 4005 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4006 | | var isSwEncoder = !vidEncoder.Contains("amf", StringComparison.OrdinalIgnoreCase); |
| 0 | 4007 | | var isAmfDx11OclSupported = isWindows && _mediaEncoder.SupportsHwaccel("d3d11va") && IsOpenclFullSupported() |
| | 4008 | |
|
| | 4009 | | // legacy d3d11va pipeline(copy-back) |
| 0 | 4010 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 4011 | | || !isAmfDx11OclSupported |
| 0 | 4012 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 4013 | | { |
| 0 | 4014 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 4015 | | } |
| | 4016 | |
|
| | 4017 | | // preferred d3d11va + opencl filters + amf pipeline |
| 0 | 4018 | | return GetAmdDx11VidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4019 | | } |
| | 4020 | |
|
| | 4021 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAmdDx11VidFiltersPref |
| | 4022 | | EncodingJobInfo state, |
| | 4023 | | EncodingOptions options, |
| | 4024 | | string vidDecoder, |
| | 4025 | | string vidEncoder) |
| | 4026 | | { |
| 0 | 4027 | | var inW = state.VideoStream?.Width; |
| 0 | 4028 | | var inH = state.VideoStream?.Height; |
| 0 | 4029 | | var reqW = state.BaseRequest.Width; |
| 0 | 4030 | | var reqH = state.BaseRequest.Height; |
| 0 | 4031 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 4032 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 4033 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 4034 | |
|
| 0 | 4035 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 4036 | | var isAmfEncoder = vidEncoder.Contains("amf", StringComparison.OrdinalIgnoreCase); |
| 0 | 4037 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4038 | | var isSwEncoder = !isAmfEncoder; |
| 0 | 4039 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 4040 | | var isDxInDxOut = isD3d11vaDecoder && isAmfEncoder; |
| | 4041 | |
|
| 0 | 4042 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 4043 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 4044 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 4045 | | var doOclTonemap = IsHwTonemapAvailable(state, options); |
| | 4046 | |
|
| 0 | 4047 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 4048 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4049 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4050 | | var hasAssSubs = hasSubs |
| 0 | 4051 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 4052 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 4053 | | var subW = state.SubtitleStream?.Width; |
| 0 | 4054 | | var subH = state.SubtitleStream?.Height; |
| | 4055 | |
|
| 0 | 4056 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 4057 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 4058 | | var doOclTranspose = !string.IsNullOrEmpty(transposeDir) |
| 0 | 4059 | | && _mediaEncoder.SupportsFilterWithOption(FilterOptionType.TransposeOpenclReversal); |
| 0 | 4060 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isD3d11vaDecoder && doOclTranspose)); |
| 0 | 4061 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 4062 | | var swpInH = swapWAndH ? inW : inH; |
| | 4063 | |
|
| | 4064 | | /* Make main filters for video stream */ |
| 0 | 4065 | | var mainFilters = new List<string>(); |
| | 4066 | |
|
| 0 | 4067 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doOclTonemap)); |
| | 4068 | |
|
| 0 | 4069 | | if (isSwDecoder) |
| | 4070 | | { |
| | 4071 | | // INPUT sw surface(memory) |
| | 4072 | | // sw deint |
| 0 | 4073 | | if (doDeintH2645) |
| | 4074 | | { |
| 0 | 4075 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 4076 | | mainFilters.Add(swDeintFilter); |
| | 4077 | | } |
| | 4078 | |
|
| 0 | 4079 | | var outFormat = doOclTonemap ? "yuv420p10le" : "yuv420p"; |
| 0 | 4080 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| | 4081 | | // sw scale |
| 0 | 4082 | | mainFilters.Add(swScaleFilter); |
| 0 | 4083 | | mainFilters.Add($"format={outFormat}"); |
| | 4084 | |
|
| | 4085 | | // keep video at memory except ocl tonemap, |
| | 4086 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 4087 | | // sw => hw |
| 0 | 4088 | | if (doOclTonemap) |
| | 4089 | | { |
| 0 | 4090 | | mainFilters.Add("hwupload=derive_device=d3d11va:extra_hw_frames=24"); |
| 0 | 4091 | | mainFilters.Add("format=d3d11"); |
| 0 | 4092 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 4093 | | } |
| | 4094 | | } |
| | 4095 | |
|
| 0 | 4096 | | if (isD3d11vaDecoder) |
| | 4097 | | { |
| | 4098 | | // INPUT d3d11 surface(vram) |
| | 4099 | | // map from d3d11va to opencl via d3d11-opencl interop. |
| 0 | 4100 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 4101 | |
|
| | 4102 | | // hw deint <= TODO: finish the 'yadif_opencl' filter |
| | 4103 | |
|
| | 4104 | | // hw transpose |
| 0 | 4105 | | if (doOclTranspose) |
| | 4106 | | { |
| 0 | 4107 | | mainFilters.Add($"transpose_opencl=dir={transposeDir}"); |
| | 4108 | | } |
| | 4109 | |
|
| 0 | 4110 | | var outFormat = doOclTonemap ? string.Empty : "nv12"; |
| 0 | 4111 | | var hwScaleFilter = GetHwScaleFilter("scale", "opencl", outFormat, false, swpInW, swpInH, reqW, reqH, re |
| | 4112 | | // hw scale |
| 0 | 4113 | | mainFilters.Add(hwScaleFilter); |
| | 4114 | | } |
| | 4115 | |
|
| | 4116 | | // hw tonemap |
| 0 | 4117 | | if (doOclTonemap) |
| | 4118 | | { |
| 0 | 4119 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 4120 | | mainFilters.Add(tonemapFilter); |
| | 4121 | | } |
| | 4122 | |
|
| 0 | 4123 | | var memoryOutput = false; |
| 0 | 4124 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 4125 | | if (isD3d11vaDecoder && isSwEncoder) |
| | 4126 | | { |
| 0 | 4127 | | memoryOutput = true; |
| | 4128 | |
|
| | 4129 | | // OUTPUT nv12 surface(memory) |
| | 4130 | | // prefer hwmap to hwdownload on opencl. |
| 0 | 4131 | | var hwTransferFilter = hasGraphicalSubs ? "hwdownload" : "hwmap=mode=read"; |
| 0 | 4132 | | mainFilters.Add(hwTransferFilter); |
| 0 | 4133 | | mainFilters.Add("format=nv12"); |
| | 4134 | | } |
| | 4135 | |
|
| | 4136 | | // OUTPUT yuv420p surface |
| 0 | 4137 | | if (isSwDecoder && isAmfEncoder && !isUploadForOclTonemap) |
| | 4138 | | { |
| 0 | 4139 | | memoryOutput = true; |
| | 4140 | | } |
| | 4141 | |
|
| 0 | 4142 | | if (memoryOutput) |
| | 4143 | | { |
| | 4144 | | // text subtitles |
| 0 | 4145 | | if (hasTextSubs) |
| | 4146 | | { |
| 0 | 4147 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 4148 | | mainFilters.Add(textSubtitlesFilter); |
| | 4149 | | } |
| | 4150 | | } |
| | 4151 | |
|
| 0 | 4152 | | if ((isDxInDxOut || isUploadForOclTonemap) && !hasSubs) |
| | 4153 | | { |
| | 4154 | | // OUTPUT d3d11(nv12) surface(vram) |
| | 4155 | | // reverse-mapping via d3d11-opencl interop. |
| 0 | 4156 | | mainFilters.Add("hwmap=derive_device=d3d11va:mode=write:reverse=1"); |
| 0 | 4157 | | mainFilters.Add("format=d3d11"); |
| | 4158 | | } |
| | 4159 | |
|
| | 4160 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 4161 | | var subFilters = new List<string>(); |
| 0 | 4162 | | var overlayFilters = new List<string>(); |
| 0 | 4163 | | if (isDxInDxOut || isUploadForOclTonemap) |
| | 4164 | | { |
| 0 | 4165 | | if (hasSubs) |
| | 4166 | | { |
| 0 | 4167 | | if (hasGraphicalSubs) |
| | 4168 | | { |
| 0 | 4169 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 4170 | | subFilters.Add(subPreProcFilters); |
| 0 | 4171 | | subFilters.Add("format=yuva420p"); |
| | 4172 | | } |
| 0 | 4173 | | else if (hasTextSubs) |
| | 4174 | | { |
| 0 | 4175 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 4176 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 4177 | |
|
| | 4178 | | // alphasrc=s=1280x720:r=10:start=0,format=yuva420p,subtitles,hwupload |
| 0 | 4179 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subF |
| 0 | 4180 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 4181 | | subFilters.Add(alphaSrcFilter); |
| 0 | 4182 | | subFilters.Add("format=yuva420p"); |
| 0 | 4183 | | subFilters.Add(subTextSubtitlesFilter); |
| | 4184 | | } |
| | 4185 | |
|
| 0 | 4186 | | subFilters.Add("hwupload=derive_device=opencl"); |
| 0 | 4187 | | overlayFilters.Add("overlay_opencl=eof_action=pass:repeatlast=0"); |
| 0 | 4188 | | overlayFilters.Add("hwmap=derive_device=d3d11va:mode=write:reverse=1"); |
| 0 | 4189 | | overlayFilters.Add("format=d3d11"); |
| | 4190 | | } |
| | 4191 | | } |
| 0 | 4192 | | else if (memoryOutput) |
| | 4193 | | { |
| 0 | 4194 | | if (hasGraphicalSubs) |
| | 4195 | | { |
| 0 | 4196 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 4197 | | subFilters.Add(subPreProcFilters); |
| 0 | 4198 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 4199 | | } |
| | 4200 | | } |
| | 4201 | |
|
| 0 | 4202 | | return (mainFilters, subFilters, overlayFilters); |
| | 4203 | | } |
| | 4204 | |
|
| | 4205 | | /// <summary> |
| | 4206 | | /// Gets the parameter of Intel QSV filter chain. |
| | 4207 | | /// </summary> |
| | 4208 | | /// <param name="state">Encoding state.</param> |
| | 4209 | | /// <param name="options">Encoding options.</param> |
| | 4210 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 4211 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 4212 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetIntelVidFilterChain( |
| | 4213 | | EncodingJobInfo state, |
| | 4214 | | EncodingOptions options, |
| | 4215 | | string vidEncoder) |
| | 4216 | | { |
| 0 | 4217 | | if (options.HardwareAccelerationType != HardwareAccelerationType.qsv) |
| | 4218 | | { |
| 0 | 4219 | | return (null, null, null); |
| | 4220 | | } |
| | 4221 | |
|
| 0 | 4222 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 4223 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 4224 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 4225 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4226 | | var isSwEncoder = !vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4227 | | var isQsvOclSupported = _mediaEncoder.SupportsHwaccel("qsv") && IsOpenclFullSupported(); |
| 0 | 4228 | | var isIntelDx11OclSupported = isWindows |
| 0 | 4229 | | && _mediaEncoder.SupportsHwaccel("d3d11va") |
| 0 | 4230 | | && isQsvOclSupported; |
| 0 | 4231 | | var isIntelVaapiOclSupported = isLinux |
| 0 | 4232 | | && IsVaapiSupported(state) |
| 0 | 4233 | | && isQsvOclSupported; |
| | 4234 | |
|
| | 4235 | | // legacy qsv pipeline(copy-back) |
| 0 | 4236 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 4237 | | || (!isIntelVaapiOclSupported && !isIntelDx11OclSupported) |
| 0 | 4238 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 4239 | | { |
| 0 | 4240 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 4241 | | } |
| | 4242 | |
|
| | 4243 | | // preferred qsv(vaapi) + opencl filters pipeline |
| 0 | 4244 | | if (isIntelVaapiOclSupported) |
| | 4245 | | { |
| 0 | 4246 | | return GetIntelQsvVaapiVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4247 | | } |
| | 4248 | |
|
| | 4249 | | // preferred qsv(d3d11) + opencl filters pipeline |
| 0 | 4250 | | if (isIntelDx11OclSupported) |
| | 4251 | | { |
| 0 | 4252 | | return GetIntelQsvDx11VidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4253 | | } |
| | 4254 | |
|
| 0 | 4255 | | return (null, null, null); |
| | 4256 | | } |
| | 4257 | |
|
| | 4258 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetIntelQsvDx11VidFilter |
| | 4259 | | EncodingJobInfo state, |
| | 4260 | | EncodingOptions options, |
| | 4261 | | string vidDecoder, |
| | 4262 | | string vidEncoder) |
| | 4263 | | { |
| 0 | 4264 | | var inW = state.VideoStream?.Width; |
| 0 | 4265 | | var inH = state.VideoStream?.Height; |
| 0 | 4266 | | var reqW = state.BaseRequest.Width; |
| 0 | 4267 | | var reqH = state.BaseRequest.Height; |
| 0 | 4268 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 4269 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 4270 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 4271 | |
|
| 0 | 4272 | | var isD3d11vaDecoder = vidDecoder.Contains("d3d11va", StringComparison.OrdinalIgnoreCase); |
| 0 | 4273 | | var isQsvDecoder = vidDecoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4274 | | var isQsvEncoder = vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4275 | | var isHwDecoder = isD3d11vaDecoder || isQsvDecoder; |
| 0 | 4276 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4277 | | var isSwEncoder = !isQsvEncoder; |
| 0 | 4278 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 4279 | | var isQsvInQsvOut = isHwDecoder && isQsvEncoder; |
| | 4280 | |
|
| 0 | 4281 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 4282 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 4283 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 4284 | | var doVppTonemap = IsIntelVppTonemapAvailable(state, options); |
| 0 | 4285 | | var doOclTonemap = !doVppTonemap && IsHwTonemapAvailable(state, options); |
| 0 | 4286 | | var doTonemap = doVppTonemap || doOclTonemap; |
| | 4287 | |
|
| 0 | 4288 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 4289 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4290 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4291 | | var hasAssSubs = hasSubs |
| 0 | 4292 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 4293 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 4294 | | var subW = state.SubtitleStream?.Width; |
| 0 | 4295 | | var subH = state.SubtitleStream?.Height; |
| | 4296 | |
|
| 0 | 4297 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 4298 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 4299 | | var doVppTranspose = !string.IsNullOrEmpty(transposeDir); |
| 0 | 4300 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || ((isD3d11vaDecoder || isQsvDecoder) && doVppTran |
| 0 | 4301 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 4302 | | var swpInH = swapWAndH ? inW : inH; |
| | 4303 | |
|
| | 4304 | | /* Make main filters for video stream */ |
| 0 | 4305 | | var mainFilters = new List<string>(); |
| | 4306 | |
|
| 0 | 4307 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doTonemap)); |
| | 4308 | |
|
| 0 | 4309 | | if (isSwDecoder) |
| | 4310 | | { |
| | 4311 | | // INPUT sw surface(memory) |
| | 4312 | | // sw deint |
| 0 | 4313 | | if (doDeintH2645) |
| | 4314 | | { |
| 0 | 4315 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 4316 | | mainFilters.Add(swDeintFilter); |
| | 4317 | | } |
| | 4318 | |
|
| 0 | 4319 | | var outFormat = doOclTonemap ? "yuv420p10le" : (hasGraphicalSubs ? "yuv420p" : "nv12"); |
| 0 | 4320 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 4321 | | if (isMjpegEncoder && !doOclTonemap) |
| | 4322 | | { |
| | 4323 | | // sw decoder + hw mjpeg encoder |
| 0 | 4324 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 4325 | | } |
| | 4326 | |
|
| | 4327 | | // sw scale |
| 0 | 4328 | | mainFilters.Add(swScaleFilter); |
| 0 | 4329 | | mainFilters.Add($"format={outFormat}"); |
| | 4330 | |
|
| | 4331 | | // keep video at memory except ocl tonemap, |
| | 4332 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 4333 | | // sw => hw |
| 0 | 4334 | | if (doOclTonemap) |
| | 4335 | | { |
| 0 | 4336 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 4337 | | } |
| | 4338 | | } |
| 0 | 4339 | | else if (isD3d11vaDecoder || isQsvDecoder) |
| | 4340 | | { |
| 0 | 4341 | | var isRext = IsVideoStreamHevcRext(state); |
| 0 | 4342 | | var twoPassVppTonemap = false; |
| 0 | 4343 | | var doVppFullRangeOut = isMjpegEncoder |
| 0 | 4344 | | && _mediaEncoder.EncoderVersion >= _minFFmpegQsvVppOutRangeOption; |
| 0 | 4345 | | var doVppScaleModeHq = isMjpegEncoder |
| 0 | 4346 | | && _mediaEncoder.EncoderVersion >= _minFFmpegQsvVppScaleModeOption; |
| 0 | 4347 | | var doVppProcamp = false; |
| 0 | 4348 | | var procampParams = string.Empty; |
| 0 | 4349 | | var procampParamsString = string.Empty; |
| 0 | 4350 | | if (doVppTonemap) |
| | 4351 | | { |
| 0 | 4352 | | if (isRext) |
| | 4353 | | { |
| | 4354 | | // VPP tonemap requires p010 input |
| 0 | 4355 | | twoPassVppTonemap = true; |
| | 4356 | | } |
| | 4357 | |
|
| 0 | 4358 | | if (options.VppTonemappingBrightness != 0 |
| 0 | 4359 | | && options.VppTonemappingBrightness >= -100 |
| 0 | 4360 | | && options.VppTonemappingBrightness <= 100) |
| | 4361 | | { |
| 0 | 4362 | | procampParamsString += ":brightness={0}"; |
| 0 | 4363 | | twoPassVppTonemap = doVppProcamp = true; |
| | 4364 | | } |
| | 4365 | |
|
| 0 | 4366 | | if (options.VppTonemappingContrast > 1 |
| 0 | 4367 | | && options.VppTonemappingContrast <= 10) |
| | 4368 | | { |
| 0 | 4369 | | procampParamsString += ":contrast={1}"; |
| 0 | 4370 | | twoPassVppTonemap = doVppProcamp = true; |
| | 4371 | | } |
| | 4372 | |
|
| 0 | 4373 | | if (doVppProcamp) |
| | 4374 | | { |
| 0 | 4375 | | procampParamsString += ":procamp=1:async_depth=2"; |
| 0 | 4376 | | procampParams = string.Format( |
| 0 | 4377 | | CultureInfo.InvariantCulture, |
| 0 | 4378 | | procampParamsString, |
| 0 | 4379 | | options.VppTonemappingBrightness, |
| 0 | 4380 | | options.VppTonemappingContrast); |
| | 4381 | | } |
| | 4382 | | } |
| | 4383 | |
|
| 0 | 4384 | | var outFormat = doOclTonemap ? ((doVppTranspose || isRext) ? "p010" : string.Empty) : "nv12"; |
| 0 | 4385 | | outFormat = twoPassVppTonemap ? "p010" : outFormat; |
| | 4386 | |
|
| 0 | 4387 | | var swapOutputWandH = doVppTranspose && swapWAndH; |
| 0 | 4388 | | var hwScaleFilter = GetHwScaleFilter("vpp", "qsv", outFormat, swapOutputWandH, swpInW, swpInH, reqW, req |
| | 4389 | |
|
| 0 | 4390 | | if (!string.IsNullOrEmpty(hwScaleFilter) && doVppTranspose) |
| | 4391 | | { |
| 0 | 4392 | | hwScaleFilter += $":transpose={transposeDir}"; |
| | 4393 | | } |
| | 4394 | |
|
| 0 | 4395 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder) |
| | 4396 | | { |
| 0 | 4397 | | hwScaleFilter += (doVppFullRangeOut && !doOclTonemap) ? ":out_range=pc" : string.Empty; |
| 0 | 4398 | | hwScaleFilter += doVppScaleModeHq ? ":scale_mode=hq" : string.Empty; |
| | 4399 | | } |
| | 4400 | |
|
| 0 | 4401 | | if (!string.IsNullOrEmpty(hwScaleFilter) && doVppTonemap) |
| | 4402 | | { |
| 0 | 4403 | | hwScaleFilter += doVppProcamp ? procampParams : (twoPassVppTonemap ? string.Empty : ":tonemap=1"); |
| | 4404 | | } |
| | 4405 | |
|
| 0 | 4406 | | if (isD3d11vaDecoder) |
| | 4407 | | { |
| 0 | 4408 | | if (!string.IsNullOrEmpty(hwScaleFilter) || doDeintH2645) |
| | 4409 | | { |
| | 4410 | | // INPUT d3d11 surface(vram) |
| | 4411 | | // map from d3d11va to qsv. |
| 0 | 4412 | | mainFilters.Add("hwmap=derive_device=qsv"); |
| | 4413 | | } |
| | 4414 | | } |
| | 4415 | |
|
| | 4416 | | // hw deint |
| 0 | 4417 | | if (doDeintH2645) |
| | 4418 | | { |
| 0 | 4419 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "qsv"); |
| 0 | 4420 | | mainFilters.Add(deintFilter); |
| | 4421 | | } |
| | 4422 | |
|
| | 4423 | | // hw transpose & scale & tonemap(w/o procamp) |
| 0 | 4424 | | mainFilters.Add(hwScaleFilter); |
| | 4425 | |
|
| | 4426 | | // hw tonemap(w/ procamp) |
| 0 | 4427 | | if (doVppTonemap && twoPassVppTonemap) |
| | 4428 | | { |
| 0 | 4429 | | mainFilters.Add("vpp_qsv=tonemap=1:format=nv12:async_depth=2"); |
| | 4430 | | } |
| | 4431 | |
|
| | 4432 | | // force bt709 just in case vpp tonemap is not triggered or using MSDK instead of VPL. |
| 0 | 4433 | | if (doVppTonemap) |
| | 4434 | | { |
| 0 | 4435 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, false)); |
| | 4436 | | } |
| | 4437 | | } |
| | 4438 | |
|
| 0 | 4439 | | if (doOclTonemap && isHwDecoder) |
| | 4440 | | { |
| | 4441 | | // map from qsv to opencl via qsv(d3d11)-opencl interop. |
| 0 | 4442 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 4443 | | } |
| | 4444 | |
|
| | 4445 | | // hw tonemap |
| 0 | 4446 | | if (doOclTonemap) |
| | 4447 | | { |
| 0 | 4448 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 4449 | | mainFilters.Add(tonemapFilter); |
| | 4450 | | } |
| | 4451 | |
|
| 0 | 4452 | | var memoryOutput = false; |
| 0 | 4453 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 4454 | | var isHwmapUsable = isSwEncoder && doOclTonemap; |
| 0 | 4455 | | if ((isHwDecoder && isSwEncoder) || isUploadForOclTonemap) |
| | 4456 | | { |
| 0 | 4457 | | memoryOutput = true; |
| | 4458 | |
|
| | 4459 | | // OUTPUT nv12 surface(memory) |
| | 4460 | | // prefer hwmap to hwdownload on opencl. |
| | 4461 | | // qsv hwmap is not fully implemented for the time being. |
| 0 | 4462 | | mainFilters.Add(isHwmapUsable ? "hwmap=mode=read" : "hwdownload"); |
| 0 | 4463 | | mainFilters.Add("format=nv12"); |
| | 4464 | | } |
| | 4465 | |
|
| | 4466 | | // OUTPUT nv12 surface(memory) |
| 0 | 4467 | | if (isSwDecoder && isQsvEncoder) |
| | 4468 | | { |
| 0 | 4469 | | memoryOutput = true; |
| | 4470 | | } |
| | 4471 | |
|
| 0 | 4472 | | if (memoryOutput) |
| | 4473 | | { |
| | 4474 | | // text subtitles |
| 0 | 4475 | | if (hasTextSubs) |
| | 4476 | | { |
| 0 | 4477 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 4478 | | mainFilters.Add(textSubtitlesFilter); |
| | 4479 | | } |
| | 4480 | | } |
| | 4481 | |
|
| 0 | 4482 | | if (isQsvInQsvOut && doOclTonemap) |
| | 4483 | | { |
| | 4484 | | // OUTPUT qsv(nv12) surface(vram) |
| | 4485 | | // reverse-mapping via qsv(d3d11)-opencl interop. |
| 0 | 4486 | | mainFilters.Add("hwmap=derive_device=qsv:mode=write:reverse=1"); |
| 0 | 4487 | | mainFilters.Add("format=qsv"); |
| | 4488 | | } |
| | 4489 | |
|
| | 4490 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 4491 | | var subFilters = new List<string>(); |
| 0 | 4492 | | var overlayFilters = new List<string>(); |
| 0 | 4493 | | if (isQsvInQsvOut) |
| | 4494 | | { |
| 0 | 4495 | | if (hasSubs) |
| | 4496 | | { |
| 0 | 4497 | | if (hasGraphicalSubs) |
| | 4498 | | { |
| | 4499 | | // overlay_qsv can handle overlay scaling, setup a smaller height to reduce transfer overhead |
| 0 | 4500 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 4501 | | subFilters.Add(subPreProcFilters); |
| 0 | 4502 | | subFilters.Add("format=bgra"); |
| | 4503 | | } |
| 0 | 4504 | | else if (hasTextSubs) |
| | 4505 | | { |
| 0 | 4506 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 4507 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 4508 | |
|
| | 4509 | | // alphasrc=s=1280x720:r=10:start=0,format=bgra,subtitles,hwupload |
| 0 | 4510 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, 1080, subFram |
| 0 | 4511 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 4512 | | subFilters.Add(alphaSrcFilter); |
| 0 | 4513 | | subFilters.Add("format=bgra"); |
| 0 | 4514 | | subFilters.Add(subTextSubtitlesFilter); |
| | 4515 | | } |
| | 4516 | |
|
| | 4517 | | // qsv requires a fixed pool size. |
| | 4518 | | // default to 64 otherwise it will fail on certain iGPU. |
| 0 | 4519 | | subFilters.Add("hwupload=derive_device=qsv:extra_hw_frames=64"); |
| | 4520 | |
|
| 0 | 4521 | | var (overlayW, overlayH) = GetFixedOutputSize(swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH); |
| 0 | 4522 | | var overlaySize = (overlayW.HasValue && overlayH.HasValue) |
| 0 | 4523 | | ? $":w={overlayW.Value}:h={overlayH.Value}" |
| 0 | 4524 | | : string.Empty; |
| 0 | 4525 | | var overlayQsvFilter = string.Format( |
| 0 | 4526 | | CultureInfo.InvariantCulture, |
| 0 | 4527 | | "overlay_qsv=eof_action=pass:repeatlast=0{0}", |
| 0 | 4528 | | overlaySize); |
| 0 | 4529 | | overlayFilters.Add(overlayQsvFilter); |
| | 4530 | | } |
| | 4531 | | } |
| 0 | 4532 | | else if (memoryOutput) |
| | 4533 | | { |
| 0 | 4534 | | if (hasGraphicalSubs) |
| | 4535 | | { |
| 0 | 4536 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 4537 | | subFilters.Add(subPreProcFilters); |
| 0 | 4538 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 4539 | | } |
| | 4540 | | } |
| | 4541 | |
|
| 0 | 4542 | | return (mainFilters, subFilters, overlayFilters); |
| | 4543 | | } |
| | 4544 | |
|
| | 4545 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetIntelQsvVaapiVidFilte |
| | 4546 | | EncodingJobInfo state, |
| | 4547 | | EncodingOptions options, |
| | 4548 | | string vidDecoder, |
| | 4549 | | string vidEncoder) |
| | 4550 | | { |
| 0 | 4551 | | var inW = state.VideoStream?.Width; |
| 0 | 4552 | | var inH = state.VideoStream?.Height; |
| 0 | 4553 | | var reqW = state.BaseRequest.Width; |
| 0 | 4554 | | var reqH = state.BaseRequest.Height; |
| 0 | 4555 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 4556 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 4557 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 4558 | |
|
| 0 | 4559 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4560 | | var isQsvDecoder = vidDecoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4561 | | var isQsvEncoder = vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase); |
| 0 | 4562 | | var isHwDecoder = isVaapiDecoder || isQsvDecoder; |
| 0 | 4563 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4564 | | var isSwEncoder = !isQsvEncoder; |
| 0 | 4565 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 4566 | | var isQsvInQsvOut = isHwDecoder && isQsvEncoder; |
| | 4567 | |
|
| 0 | 4568 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 4569 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 4570 | | var doVaVppTonemap = IsIntelVppTonemapAvailable(state, options); |
| 0 | 4571 | | var doOclTonemap = !doVaVppTonemap && IsHwTonemapAvailable(state, options); |
| 0 | 4572 | | var doTonemap = doVaVppTonemap || doOclTonemap; |
| 0 | 4573 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| | 4574 | |
|
| 0 | 4575 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 4576 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4577 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4578 | | var hasAssSubs = hasSubs |
| 0 | 4579 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 4580 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 4581 | | var subW = state.SubtitleStream?.Width; |
| 0 | 4582 | | var subH = state.SubtitleStream?.Height; |
| | 4583 | |
|
| 0 | 4584 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 4585 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 4586 | | var doVppTranspose = !string.IsNullOrEmpty(transposeDir); |
| 0 | 4587 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || ((isVaapiDecoder || isQsvDecoder) && doVppTransp |
| 0 | 4588 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 4589 | | var swpInH = swapWAndH ? inW : inH; |
| | 4590 | |
|
| | 4591 | | /* Make main filters for video stream */ |
| 0 | 4592 | | var mainFilters = new List<string>(); |
| | 4593 | |
|
| 0 | 4594 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doTonemap)); |
| | 4595 | |
|
| 0 | 4596 | | if (isSwDecoder) |
| | 4597 | | { |
| | 4598 | | // INPUT sw surface(memory) |
| | 4599 | | // sw deint |
| 0 | 4600 | | if (doDeintH2645) |
| | 4601 | | { |
| 0 | 4602 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 4603 | | mainFilters.Add(swDeintFilter); |
| | 4604 | | } |
| | 4605 | |
|
| 0 | 4606 | | var outFormat = doOclTonemap ? "yuv420p10le" : (hasGraphicalSubs ? "yuv420p" : "nv12"); |
| 0 | 4607 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 4608 | | if (isMjpegEncoder && !doOclTonemap) |
| | 4609 | | { |
| | 4610 | | // sw decoder + hw mjpeg encoder |
| 0 | 4611 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 4612 | | } |
| | 4613 | |
|
| | 4614 | | // sw scale |
| 0 | 4615 | | mainFilters.Add(swScaleFilter); |
| 0 | 4616 | | mainFilters.Add($"format={outFormat}"); |
| | 4617 | |
|
| | 4618 | | // keep video at memory except ocl tonemap, |
| | 4619 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 4620 | | // sw => hw |
| 0 | 4621 | | if (doOclTonemap) |
| | 4622 | | { |
| 0 | 4623 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 4624 | | } |
| | 4625 | | } |
| 0 | 4626 | | else if (isVaapiDecoder || isQsvDecoder) |
| | 4627 | | { |
| 0 | 4628 | | var hwFilterSuffix = isVaapiDecoder ? "vaapi" : "qsv"; |
| 0 | 4629 | | var isRext = IsVideoStreamHevcRext(state); |
| 0 | 4630 | | var doVppFullRangeOut = isMjpegEncoder |
| 0 | 4631 | | && _mediaEncoder.EncoderVersion >= _minFFmpegQsvVppOutRangeOption; |
| 0 | 4632 | | var doVppScaleModeHq = isMjpegEncoder |
| 0 | 4633 | | && _mediaEncoder.EncoderVersion >= _minFFmpegQsvVppScaleModeOption; |
| | 4634 | |
|
| | 4635 | | // INPUT vaapi/qsv surface(vram) |
| | 4636 | | // hw deint |
| 0 | 4637 | | if (doDeintH2645) |
| | 4638 | | { |
| 0 | 4639 | | var deintFilter = GetHwDeinterlaceFilter(state, options, hwFilterSuffix); |
| 0 | 4640 | | mainFilters.Add(deintFilter); |
| | 4641 | | } |
| | 4642 | |
|
| | 4643 | | // hw transpose(vaapi vpp) |
| 0 | 4644 | | if (isVaapiDecoder && doVppTranspose) |
| | 4645 | | { |
| 0 | 4646 | | mainFilters.Add($"transpose_vaapi=dir={transposeDir}"); |
| | 4647 | | } |
| | 4648 | |
|
| 0 | 4649 | | var outFormat = doTonemap ? (((isQsvDecoder && doVppTranspose) || isRext) ? "p010" : string.Empty) : "nv |
| 0 | 4650 | | var swapOutputWandH = isQsvDecoder && doVppTranspose && swapWAndH; |
| 0 | 4651 | | var hwScalePrefix = isQsvDecoder ? "vpp" : "scale"; |
| 0 | 4652 | | var hwScaleFilter = GetHwScaleFilter(hwScalePrefix, hwFilterSuffix, outFormat, swapOutputWandH, swpInW, |
| | 4653 | |
|
| 0 | 4654 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isQsvDecoder && doVppTranspose) |
| | 4655 | | { |
| 0 | 4656 | | hwScaleFilter += $":transpose={transposeDir}"; |
| | 4657 | | } |
| | 4658 | |
|
| 0 | 4659 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder) |
| | 4660 | | { |
| 0 | 4661 | | hwScaleFilter += ((isQsvDecoder && !doVppFullRangeOut) || doOclTonemap) ? string.Empty : ":out_range |
| 0 | 4662 | | hwScaleFilter += isQsvDecoder ? (doVppScaleModeHq ? ":scale_mode=hq" : string.Empty) : ":mode=hq"; |
| | 4663 | | } |
| | 4664 | |
|
| | 4665 | | // allocate extra pool sizes for vaapi vpp scale |
| 0 | 4666 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isVaapiDecoder) |
| | 4667 | | { |
| 0 | 4668 | | hwScaleFilter += ":extra_hw_frames=24"; |
| | 4669 | | } |
| | 4670 | |
|
| | 4671 | | // hw transpose(qsv vpp) & scale |
| 0 | 4672 | | mainFilters.Add(hwScaleFilter); |
| | 4673 | | } |
| | 4674 | |
|
| | 4675 | | // vaapi vpp tonemap |
| 0 | 4676 | | if (doVaVppTonemap && isHwDecoder) |
| | 4677 | | { |
| 0 | 4678 | | if (isQsvDecoder) |
| | 4679 | | { |
| | 4680 | | // map from qsv to vaapi. |
| 0 | 4681 | | mainFilters.Add("hwmap=derive_device=vaapi"); |
| 0 | 4682 | | mainFilters.Add("format=vaapi"); |
| | 4683 | | } |
| | 4684 | |
|
| 0 | 4685 | | var tonemapFilter = GetHwTonemapFilter(options, "vaapi", "nv12", isMjpegEncoder); |
| 0 | 4686 | | mainFilters.Add(tonemapFilter); |
| | 4687 | |
|
| 0 | 4688 | | if (isQsvDecoder) |
| | 4689 | | { |
| | 4690 | | // map from vaapi to qsv. |
| 0 | 4691 | | mainFilters.Add("hwmap=derive_device=qsv"); |
| 0 | 4692 | | mainFilters.Add("format=qsv"); |
| | 4693 | | } |
| | 4694 | | } |
| | 4695 | |
|
| 0 | 4696 | | if (doOclTonemap && isHwDecoder) |
| | 4697 | | { |
| | 4698 | | // map from qsv to opencl via qsv(vaapi)-opencl interop. |
| 0 | 4699 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 4700 | | } |
| | 4701 | |
|
| | 4702 | | // ocl tonemap |
| 0 | 4703 | | if (doOclTonemap) |
| | 4704 | | { |
| 0 | 4705 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 4706 | | mainFilters.Add(tonemapFilter); |
| | 4707 | | } |
| | 4708 | |
|
| 0 | 4709 | | var memoryOutput = false; |
| 0 | 4710 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 4711 | | var isHwmapUsable = isSwEncoder && (doOclTonemap || isVaapiDecoder); |
| 0 | 4712 | | if ((isHwDecoder && isSwEncoder) || isUploadForOclTonemap) |
| | 4713 | | { |
| 0 | 4714 | | memoryOutput = true; |
| | 4715 | |
|
| | 4716 | | // OUTPUT nv12 surface(memory) |
| | 4717 | | // prefer hwmap to hwdownload on opencl/vaapi. |
| | 4718 | | // qsv hwmap is not fully implemented for the time being. |
| 0 | 4719 | | mainFilters.Add(isHwmapUsable ? "hwmap=mode=read" : "hwdownload"); |
| 0 | 4720 | | mainFilters.Add("format=nv12"); |
| | 4721 | | } |
| | 4722 | |
|
| | 4723 | | // OUTPUT nv12 surface(memory) |
| 0 | 4724 | | if (isSwDecoder && isQsvEncoder) |
| | 4725 | | { |
| 0 | 4726 | | memoryOutput = true; |
| | 4727 | | } |
| | 4728 | |
|
| 0 | 4729 | | if (memoryOutput) |
| | 4730 | | { |
| | 4731 | | // text subtitles |
| 0 | 4732 | | if (hasTextSubs) |
| | 4733 | | { |
| 0 | 4734 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 4735 | | mainFilters.Add(textSubtitlesFilter); |
| | 4736 | | } |
| | 4737 | | } |
| | 4738 | |
|
| 0 | 4739 | | if (isQsvInQsvOut) |
| | 4740 | | { |
| 0 | 4741 | | if (doOclTonemap) |
| | 4742 | | { |
| | 4743 | | // OUTPUT qsv(nv12) surface(vram) |
| | 4744 | | // reverse-mapping via qsv(vaapi)-opencl interop. |
| | 4745 | | // add extra pool size to avoid the 'cannot allocate memory' error on hevc_qsv. |
| 0 | 4746 | | mainFilters.Add("hwmap=derive_device=qsv:mode=write:reverse=1:extra_hw_frames=16"); |
| 0 | 4747 | | mainFilters.Add("format=qsv"); |
| | 4748 | | } |
| 0 | 4749 | | else if (isVaapiDecoder) |
| | 4750 | | { |
| 0 | 4751 | | mainFilters.Add("hwmap=derive_device=qsv"); |
| 0 | 4752 | | mainFilters.Add("format=qsv"); |
| | 4753 | | } |
| | 4754 | | } |
| | 4755 | |
|
| | 4756 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 4757 | | var subFilters = new List<string>(); |
| 0 | 4758 | | var overlayFilters = new List<string>(); |
| 0 | 4759 | | if (isQsvInQsvOut) |
| | 4760 | | { |
| 0 | 4761 | | if (hasSubs) |
| | 4762 | | { |
| 0 | 4763 | | if (hasGraphicalSubs) |
| | 4764 | | { |
| | 4765 | | // overlay_qsv can handle overlay scaling, setup a smaller height to reduce transfer overhead |
| 0 | 4766 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 4767 | | subFilters.Add(subPreProcFilters); |
| 0 | 4768 | | subFilters.Add("format=bgra"); |
| | 4769 | | } |
| 0 | 4770 | | else if (hasTextSubs) |
| | 4771 | | { |
| 0 | 4772 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 4773 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 4774 | |
|
| 0 | 4775 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, 1080, subFram |
| 0 | 4776 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 4777 | | subFilters.Add(alphaSrcFilter); |
| 0 | 4778 | | subFilters.Add("format=bgra"); |
| 0 | 4779 | | subFilters.Add(subTextSubtitlesFilter); |
| | 4780 | | } |
| | 4781 | |
|
| | 4782 | | // qsv requires a fixed pool size. |
| | 4783 | | // default to 64 otherwise it will fail on certain iGPU. |
| 0 | 4784 | | subFilters.Add("hwupload=derive_device=qsv:extra_hw_frames=64"); |
| | 4785 | |
|
| 0 | 4786 | | var (overlayW, overlayH) = GetFixedOutputSize(swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH); |
| 0 | 4787 | | var overlaySize = (overlayW.HasValue && overlayH.HasValue) |
| 0 | 4788 | | ? $":w={overlayW.Value}:h={overlayH.Value}" |
| 0 | 4789 | | : string.Empty; |
| 0 | 4790 | | var overlayQsvFilter = string.Format( |
| 0 | 4791 | | CultureInfo.InvariantCulture, |
| 0 | 4792 | | "overlay_qsv=eof_action=pass:repeatlast=0{0}", |
| 0 | 4793 | | overlaySize); |
| 0 | 4794 | | overlayFilters.Add(overlayQsvFilter); |
| | 4795 | | } |
| | 4796 | | } |
| 0 | 4797 | | else if (memoryOutput) |
| | 4798 | | { |
| 0 | 4799 | | if (hasGraphicalSubs) |
| | 4800 | | { |
| 0 | 4801 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 4802 | | subFilters.Add(subPreProcFilters); |
| 0 | 4803 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 4804 | | } |
| | 4805 | | } |
| | 4806 | |
|
| 0 | 4807 | | return (mainFilters, subFilters, overlayFilters); |
| | 4808 | | } |
| | 4809 | |
|
| | 4810 | | /// <summary> |
| | 4811 | | /// Gets the parameter of Intel/AMD VAAPI filter chain. |
| | 4812 | | /// </summary> |
| | 4813 | | /// <param name="state">Encoding state.</param> |
| | 4814 | | /// <param name="options">Encoding options.</param> |
| | 4815 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 4816 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 4817 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetVaapiVidFilterChain( |
| | 4818 | | EncodingJobInfo state, |
| | 4819 | | EncodingOptions options, |
| | 4820 | | string vidEncoder) |
| | 4821 | | { |
| 0 | 4822 | | if (options.HardwareAccelerationType != HardwareAccelerationType.vaapi) |
| | 4823 | | { |
| 0 | 4824 | | return (null, null, null); |
| | 4825 | | } |
| | 4826 | |
|
| 0 | 4827 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 4828 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 4829 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4830 | | var isSwEncoder = !vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4831 | | var isVaapiFullSupported = isLinux && IsVaapiSupported(state) && IsVaapiFullSupported(); |
| 0 | 4832 | | var isVaapiOclSupported = isVaapiFullSupported && IsOpenclFullSupported(); |
| 0 | 4833 | | var isVaapiVkSupported = isVaapiFullSupported && IsVulkanFullSupported(); |
| | 4834 | |
|
| | 4835 | | // legacy vaapi pipeline(copy-back) |
| 0 | 4836 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 4837 | | || !isVaapiOclSupported |
| 0 | 4838 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 4839 | | { |
| 0 | 4840 | | var swFilterChain = GetSwVidFilterChain(state, options, vidEncoder); |
| | 4841 | |
|
| 0 | 4842 | | if (!isSwEncoder) |
| | 4843 | | { |
| 0 | 4844 | | var newfilters = new List<string>(); |
| 0 | 4845 | | var noOverlay = swFilterChain.OverlayFilters.Count == 0; |
| 0 | 4846 | | newfilters.AddRange(noOverlay ? swFilterChain.MainFilters : swFilterChain.OverlayFilters); |
| 0 | 4847 | | newfilters.Add("hwupload=derive_device=vaapi"); |
| | 4848 | |
|
| 0 | 4849 | | var mainFilters = noOverlay ? newfilters : swFilterChain.MainFilters; |
| 0 | 4850 | | var overlayFilters = noOverlay ? swFilterChain.OverlayFilters : newfilters; |
| 0 | 4851 | | return (mainFilters, swFilterChain.SubFilters, overlayFilters); |
| | 4852 | | } |
| | 4853 | |
|
| 0 | 4854 | | return swFilterChain; |
| | 4855 | | } |
| | 4856 | |
|
| | 4857 | | // preferred vaapi + opencl filters pipeline |
| 0 | 4858 | | if (_mediaEncoder.IsVaapiDeviceInteliHD) |
| | 4859 | | { |
| | 4860 | | // Intel iHD path, with extra vpp tonemap and overlay support. |
| 0 | 4861 | | return GetIntelVaapiFullVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4862 | | } |
| | 4863 | |
|
| | 4864 | | // preferred vaapi + vulkan filters pipeline |
| 0 | 4865 | | if (_mediaEncoder.IsVaapiDeviceAmd |
| 0 | 4866 | | && isVaapiVkSupported |
| 0 | 4867 | | && _mediaEncoder.IsVaapiDeviceSupportVulkanDrmInterop |
| 0 | 4868 | | && Environment.OSVersion.Version >= _minKernelVersionAmdVkFmtModifier) |
| | 4869 | | { |
| | 4870 | | // AMD radeonsi path(targeting Polaris/gfx8+), with extra vulkan tonemap and overlay support. |
| 0 | 4871 | | return GetAmdVaapiFullVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4872 | | } |
| | 4873 | |
|
| | 4874 | | // Intel i965 and Amd legacy driver path, only featuring scale and deinterlace support. |
| 0 | 4875 | | return GetVaapiLimitedVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 4876 | | } |
| | 4877 | |
|
| | 4878 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetIntelVaapiFullVidFilt |
| | 4879 | | EncodingJobInfo state, |
| | 4880 | | EncodingOptions options, |
| | 4881 | | string vidDecoder, |
| | 4882 | | string vidEncoder) |
| | 4883 | | { |
| 0 | 4884 | | var inW = state.VideoStream?.Width; |
| 0 | 4885 | | var inH = state.VideoStream?.Height; |
| 0 | 4886 | | var reqW = state.BaseRequest.Width; |
| 0 | 4887 | | var reqH = state.BaseRequest.Height; |
| 0 | 4888 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 4889 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 4890 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 4891 | |
|
| 0 | 4892 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4893 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 4894 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 4895 | | var isSwEncoder = !isVaapiEncoder; |
| 0 | 4896 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 4897 | | var isVaInVaOut = isVaapiDecoder && isVaapiEncoder; |
| | 4898 | |
|
| 0 | 4899 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 4900 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 4901 | | var doVaVppTonemap = isVaapiDecoder && IsIntelVppTonemapAvailable(state, options); |
| 0 | 4902 | | var doOclTonemap = !doVaVppTonemap && IsHwTonemapAvailable(state, options); |
| 0 | 4903 | | var doTonemap = doVaVppTonemap || doOclTonemap; |
| 0 | 4904 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| | 4905 | |
|
| 0 | 4906 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 4907 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4908 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 4909 | | var hasAssSubs = hasSubs |
| 0 | 4910 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 4911 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 4912 | | var subW = state.SubtitleStream?.Width; |
| 0 | 4913 | | var subH = state.SubtitleStream?.Height; |
| | 4914 | |
|
| 0 | 4915 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 4916 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 4917 | | var doVaVppTranspose = !string.IsNullOrEmpty(transposeDir); |
| 0 | 4918 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isVaapiDecoder && doVaVppTranspose)); |
| 0 | 4919 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 4920 | | var swpInH = swapWAndH ? inW : inH; |
| | 4921 | |
|
| | 4922 | | /* Make main filters for video stream */ |
| 0 | 4923 | | var mainFilters = new List<string>(); |
| | 4924 | |
|
| 0 | 4925 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doTonemap)); |
| | 4926 | |
|
| 0 | 4927 | | if (isSwDecoder) |
| | 4928 | | { |
| | 4929 | | // INPUT sw surface(memory) |
| | 4930 | | // sw deint |
| 0 | 4931 | | if (doDeintH2645) |
| | 4932 | | { |
| 0 | 4933 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 4934 | | mainFilters.Add(swDeintFilter); |
| | 4935 | | } |
| | 4936 | |
|
| 0 | 4937 | | var outFormat = doOclTonemap ? "yuv420p10le" : "nv12"; |
| 0 | 4938 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 4939 | | if (isMjpegEncoder && !doOclTonemap) |
| | 4940 | | { |
| | 4941 | | // sw decoder + hw mjpeg encoder |
| 0 | 4942 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 4943 | | } |
| | 4944 | |
|
| | 4945 | | // sw scale |
| 0 | 4946 | | mainFilters.Add(swScaleFilter); |
| 0 | 4947 | | mainFilters.Add($"format={outFormat}"); |
| | 4948 | |
|
| | 4949 | | // keep video at memory except ocl tonemap, |
| | 4950 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 4951 | | // sw => hw |
| 0 | 4952 | | if (doOclTonemap) |
| | 4953 | | { |
| 0 | 4954 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 4955 | | } |
| | 4956 | | } |
| 0 | 4957 | | else if (isVaapiDecoder) |
| | 4958 | | { |
| 0 | 4959 | | var isRext = IsVideoStreamHevcRext(state); |
| | 4960 | |
|
| | 4961 | | // INPUT vaapi surface(vram) |
| | 4962 | | // hw deint |
| 0 | 4963 | | if (doDeintH2645) |
| | 4964 | | { |
| 0 | 4965 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 4966 | | mainFilters.Add(deintFilter); |
| | 4967 | | } |
| | 4968 | |
|
| | 4969 | | // hw transpose |
| 0 | 4970 | | if (doVaVppTranspose) |
| | 4971 | | { |
| 0 | 4972 | | mainFilters.Add($"transpose_vaapi=dir={transposeDir}"); |
| | 4973 | | } |
| | 4974 | |
|
| 0 | 4975 | | var outFormat = doTonemap ? (isRext ? "p010" : string.Empty) : "nv12"; |
| 0 | 4976 | | var hwScaleFilter = GetHwScaleFilter("scale", "vaapi", outFormat, false, swpInW, swpInH, reqW, reqH, req |
| | 4977 | |
|
| 0 | 4978 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder) |
| | 4979 | | { |
| 0 | 4980 | | hwScaleFilter += doOclTonemap ? string.Empty : ":out_range=pc"; |
| 0 | 4981 | | hwScaleFilter += ":mode=hq"; |
| | 4982 | | } |
| | 4983 | |
|
| | 4984 | | // allocate extra pool sizes for vaapi vpp |
| 0 | 4985 | | if (!string.IsNullOrEmpty(hwScaleFilter)) |
| | 4986 | | { |
| 0 | 4987 | | hwScaleFilter += ":extra_hw_frames=24"; |
| | 4988 | | } |
| | 4989 | |
|
| | 4990 | | // hw scale |
| 0 | 4991 | | mainFilters.Add(hwScaleFilter); |
| | 4992 | | } |
| | 4993 | |
|
| | 4994 | | // vaapi vpp tonemap |
| 0 | 4995 | | if (doVaVppTonemap && isVaapiDecoder) |
| | 4996 | | { |
| 0 | 4997 | | var tonemapFilter = GetHwTonemapFilter(options, "vaapi", "nv12", isMjpegEncoder); |
| 0 | 4998 | | mainFilters.Add(tonemapFilter); |
| | 4999 | | } |
| | 5000 | |
|
| 0 | 5001 | | if (doOclTonemap && isVaapiDecoder) |
| | 5002 | | { |
| | 5003 | | // map from vaapi to opencl via vaapi-opencl interop(Intel only). |
| 0 | 5004 | | mainFilters.Add("hwmap=derive_device=opencl:mode=read"); |
| | 5005 | | } |
| | 5006 | |
|
| | 5007 | | // ocl tonemap |
| 0 | 5008 | | if (doOclTonemap) |
| | 5009 | | { |
| 0 | 5010 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 5011 | | mainFilters.Add(tonemapFilter); |
| | 5012 | | } |
| | 5013 | |
|
| 0 | 5014 | | if (doOclTonemap && isVaInVaOut) |
| | 5015 | | { |
| | 5016 | | // OUTPUT vaapi(nv12) surface(vram) |
| | 5017 | | // reverse-mapping via vaapi-opencl interop. |
| 0 | 5018 | | mainFilters.Add("hwmap=derive_device=vaapi:mode=write:reverse=1"); |
| 0 | 5019 | | mainFilters.Add("format=vaapi"); |
| | 5020 | | } |
| | 5021 | |
|
| 0 | 5022 | | var memoryOutput = false; |
| 0 | 5023 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 5024 | | var isHwmapNotUsable = isUploadForOclTonemap && isVaapiEncoder; |
| 0 | 5025 | | if ((isVaapiDecoder && isSwEncoder) || isUploadForOclTonemap) |
| | 5026 | | { |
| 0 | 5027 | | memoryOutput = true; |
| | 5028 | |
|
| | 5029 | | // OUTPUT nv12 surface(memory) |
| | 5030 | | // prefer hwmap to hwdownload on opencl/vaapi. |
| 0 | 5031 | | mainFilters.Add(isHwmapNotUsable ? "hwdownload" : "hwmap=mode=read"); |
| 0 | 5032 | | mainFilters.Add("format=nv12"); |
| | 5033 | | } |
| | 5034 | |
|
| | 5035 | | // OUTPUT nv12 surface(memory) |
| 0 | 5036 | | if (isSwDecoder && isVaapiEncoder) |
| | 5037 | | { |
| 0 | 5038 | | memoryOutput = true; |
| | 5039 | | } |
| | 5040 | |
|
| 0 | 5041 | | if (memoryOutput) |
| | 5042 | | { |
| | 5043 | | // text subtitles |
| 0 | 5044 | | if (hasTextSubs) |
| | 5045 | | { |
| 0 | 5046 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 5047 | | mainFilters.Add(textSubtitlesFilter); |
| | 5048 | | } |
| | 5049 | | } |
| | 5050 | |
|
| 0 | 5051 | | if (memoryOutput && isVaapiEncoder) |
| | 5052 | | { |
| 0 | 5053 | | if (!hasGraphicalSubs) |
| | 5054 | | { |
| 0 | 5055 | | mainFilters.Add("hwupload_vaapi"); |
| | 5056 | | } |
| | 5057 | | } |
| | 5058 | |
|
| | 5059 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5060 | | var subFilters = new List<string>(); |
| 0 | 5061 | | var overlayFilters = new List<string>(); |
| 0 | 5062 | | if (isVaInVaOut) |
| | 5063 | | { |
| 0 | 5064 | | if (hasSubs) |
| | 5065 | | { |
| 0 | 5066 | | if (hasGraphicalSubs) |
| | 5067 | | { |
| | 5068 | | // overlay_vaapi can handle overlay scaling, setup a smaller height to reduce transfer overhead |
| 0 | 5069 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 5070 | | subFilters.Add(subPreProcFilters); |
| 0 | 5071 | | subFilters.Add("format=bgra"); |
| | 5072 | | } |
| 0 | 5073 | | else if (hasTextSubs) |
| | 5074 | | { |
| 0 | 5075 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 5076 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 5077 | |
|
| 0 | 5078 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, 1080, subFram |
| 0 | 5079 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 5080 | | subFilters.Add(alphaSrcFilter); |
| 0 | 5081 | | subFilters.Add("format=bgra"); |
| 0 | 5082 | | subFilters.Add(subTextSubtitlesFilter); |
| | 5083 | | } |
| | 5084 | |
|
| 0 | 5085 | | subFilters.Add("hwupload=derive_device=vaapi"); |
| | 5086 | |
|
| 0 | 5087 | | var (overlayW, overlayH) = GetFixedOutputSize(swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH); |
| 0 | 5088 | | var overlaySize = (overlayW.HasValue && overlayH.HasValue) |
| 0 | 5089 | | ? $":w={overlayW.Value}:h={overlayH.Value}" |
| 0 | 5090 | | : string.Empty; |
| 0 | 5091 | | var overlayVaapiFilter = string.Format( |
| 0 | 5092 | | CultureInfo.InvariantCulture, |
| 0 | 5093 | | "overlay_vaapi=eof_action=pass:repeatlast=0{0}", |
| 0 | 5094 | | overlaySize); |
| 0 | 5095 | | overlayFilters.Add(overlayVaapiFilter); |
| | 5096 | | } |
| | 5097 | | } |
| 0 | 5098 | | else if (memoryOutput) |
| | 5099 | | { |
| 0 | 5100 | | if (hasGraphicalSubs) |
| | 5101 | | { |
| 0 | 5102 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 5103 | | subFilters.Add(subPreProcFilters); |
| 0 | 5104 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 5105 | |
|
| 0 | 5106 | | if (isVaapiEncoder) |
| | 5107 | | { |
| 0 | 5108 | | overlayFilters.Add("hwupload_vaapi"); |
| | 5109 | | } |
| | 5110 | | } |
| | 5111 | | } |
| | 5112 | |
|
| 0 | 5113 | | return (mainFilters, subFilters, overlayFilters); |
| | 5114 | | } |
| | 5115 | |
|
| | 5116 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAmdVaapiFullVidFilter |
| | 5117 | | EncodingJobInfo state, |
| | 5118 | | EncodingOptions options, |
| | 5119 | | string vidDecoder, |
| | 5120 | | string vidEncoder) |
| | 5121 | | { |
| 0 | 5122 | | var inW = state.VideoStream?.Width; |
| 0 | 5123 | | var inH = state.VideoStream?.Height; |
| 0 | 5124 | | var reqW = state.BaseRequest.Width; |
| 0 | 5125 | | var reqH = state.BaseRequest.Height; |
| 0 | 5126 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 5127 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 5128 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 5129 | |
|
| 0 | 5130 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 5131 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 5132 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 5133 | | var isSwEncoder = !isVaapiEncoder; |
| 0 | 5134 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| | 5135 | |
|
| 0 | 5136 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 5137 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 5138 | | var doVkTonemap = IsVulkanHwTonemapAvailable(state, options); |
| 0 | 5139 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| | 5140 | |
|
| 0 | 5141 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 5142 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5143 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5144 | | var hasAssSubs = hasSubs |
| 0 | 5145 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 5146 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| | 5147 | |
|
| 0 | 5148 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 5149 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 5150 | | var doVkTranspose = isVaapiDecoder && !string.IsNullOrEmpty(transposeDir); |
| 0 | 5151 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isVaapiDecoder && doVkTranspose)); |
| 0 | 5152 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 5153 | | var swpInH = swapWAndH ? inW : inH; |
| | 5154 | |
|
| | 5155 | | /* Make main filters for video stream */ |
| 0 | 5156 | | var mainFilters = new List<string>(); |
| | 5157 | |
|
| 0 | 5158 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doVkTonemap)); |
| | 5159 | |
|
| 0 | 5160 | | if (isSwDecoder) |
| | 5161 | | { |
| | 5162 | | // INPUT sw surface(memory) |
| | 5163 | | // sw deint |
| 0 | 5164 | | if (doDeintH2645) |
| | 5165 | | { |
| 0 | 5166 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 5167 | | mainFilters.Add(swDeintFilter); |
| | 5168 | | } |
| | 5169 | |
|
| 0 | 5170 | | if (doVkTonemap || hasSubs) |
| | 5171 | | { |
| | 5172 | | // sw => hw |
| 0 | 5173 | | mainFilters.Add("hwupload=derive_device=vulkan"); |
| 0 | 5174 | | mainFilters.Add("format=vulkan"); |
| | 5175 | | } |
| | 5176 | | else |
| | 5177 | | { |
| | 5178 | | // sw scale |
| 0 | 5179 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, |
| 0 | 5180 | | mainFilters.Add(swScaleFilter); |
| 0 | 5181 | | mainFilters.Add("format=nv12"); |
| | 5182 | | } |
| | 5183 | | } |
| 0 | 5184 | | else if (isVaapiDecoder) |
| | 5185 | | { |
| | 5186 | | // INPUT vaapi surface(vram) |
| 0 | 5187 | | if (doVkTranspose || doVkTonemap || hasSubs) |
| | 5188 | | { |
| | 5189 | | // map from vaapi to vulkan/drm via interop (Polaris/gfx8+). |
| 0 | 5190 | | if (_mediaEncoder.EncoderVersion >= _minFFmpegAlteredVaVkInterop) |
| | 5191 | | { |
| 0 | 5192 | | if (doVkTranspose || !_mediaEncoder.IsVaapiDeviceSupportVulkanDrmModifier) |
| | 5193 | | { |
| | 5194 | | // disable the indirect va-drm-vk mapping since it's no longer reliable. |
| 0 | 5195 | | mainFilters.Add("hwmap=derive_device=drm"); |
| 0 | 5196 | | mainFilters.Add("format=drm_prime"); |
| 0 | 5197 | | mainFilters.Add("hwmap=derive_device=vulkan"); |
| 0 | 5198 | | mainFilters.Add("format=vulkan"); |
| | 5199 | |
|
| | 5200 | | // workaround for libplacebo using the imported vulkan frame on gfx8. |
| 0 | 5201 | | if (!_mediaEncoder.IsVaapiDeviceSupportVulkanDrmModifier) |
| | 5202 | | { |
| 0 | 5203 | | mainFilters.Add("scale_vulkan"); |
| | 5204 | | } |
| | 5205 | | } |
| 0 | 5206 | | else if (doVkTonemap || hasSubs) |
| | 5207 | | { |
| | 5208 | | // non ad-hoc libplacebo also accepts drm_prime direct input. |
| 0 | 5209 | | mainFilters.Add("hwmap=derive_device=drm"); |
| 0 | 5210 | | mainFilters.Add("format=drm_prime"); |
| | 5211 | | } |
| | 5212 | | } |
| | 5213 | | else // legacy va-vk mapping that works only in jellyfin-ffmpeg6 |
| | 5214 | | { |
| 0 | 5215 | | mainFilters.Add("hwmap=derive_device=vulkan"); |
| 0 | 5216 | | mainFilters.Add("format=vulkan"); |
| | 5217 | | } |
| | 5218 | | } |
| | 5219 | | else |
| | 5220 | | { |
| | 5221 | | // hw deint |
| 0 | 5222 | | if (doDeintH2645) |
| | 5223 | | { |
| 0 | 5224 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 5225 | | mainFilters.Add(deintFilter); |
| | 5226 | | } |
| | 5227 | |
|
| | 5228 | | // hw scale |
| 0 | 5229 | | var hwScaleFilter = GetHwScaleFilter("scale", "vaapi", "nv12", false, inW, inH, reqW, reqH, reqMaxW, |
| | 5230 | |
|
| 0 | 5231 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder && !doVkTonemap) |
| | 5232 | | { |
| 0 | 5233 | | hwScaleFilter += ":out_range=pc:mode=hq"; |
| | 5234 | | } |
| | 5235 | |
|
| 0 | 5236 | | mainFilters.Add(hwScaleFilter); |
| | 5237 | | } |
| | 5238 | | } |
| | 5239 | |
|
| | 5240 | | // vk transpose |
| 0 | 5241 | | if (doVkTranspose) |
| | 5242 | | { |
| 0 | 5243 | | if (string.Equals(transposeDir, "reversal", StringComparison.OrdinalIgnoreCase)) |
| | 5244 | | { |
| 0 | 5245 | | mainFilters.Add("flip_vulkan"); |
| | 5246 | | } |
| | 5247 | | else |
| | 5248 | | { |
| 0 | 5249 | | mainFilters.Add($"transpose_vulkan=dir={transposeDir}"); |
| | 5250 | | } |
| | 5251 | | } |
| | 5252 | |
|
| | 5253 | | // vk libplacebo |
| 0 | 5254 | | if (doVkTonemap || hasSubs) |
| | 5255 | | { |
| 0 | 5256 | | var libplaceboFilter = GetLibplaceboFilter(options, "bgra", doVkTonemap, swpInW, swpInH, reqW, reqH, req |
| 0 | 5257 | | mainFilters.Add(libplaceboFilter); |
| 0 | 5258 | | mainFilters.Add("format=vulkan"); |
| | 5259 | | } |
| | 5260 | |
|
| 0 | 5261 | | if (doVkTonemap && !hasSubs) |
| | 5262 | | { |
| | 5263 | | // OUTPUT vaapi(nv12) surface(vram) |
| | 5264 | | // map from vulkan/drm to vaapi via interop (Polaris/gfx8+). |
| 0 | 5265 | | mainFilters.Add("hwmap=derive_device=vaapi"); |
| 0 | 5266 | | mainFilters.Add("format=vaapi"); |
| | 5267 | |
|
| | 5268 | | // clear the surf->meta_offset and output nv12 |
| 0 | 5269 | | mainFilters.Add("scale_vaapi=format=nv12"); |
| | 5270 | |
|
| | 5271 | | // hw deint |
| 0 | 5272 | | if (doDeintH2645) |
| | 5273 | | { |
| 0 | 5274 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 5275 | | mainFilters.Add(deintFilter); |
| | 5276 | | } |
| | 5277 | | } |
| | 5278 | |
|
| 0 | 5279 | | if (!hasSubs) |
| | 5280 | | { |
| | 5281 | | // OUTPUT nv12 surface(memory) |
| 0 | 5282 | | if (isSwEncoder && (doVkTonemap || isVaapiDecoder)) |
| | 5283 | | { |
| 0 | 5284 | | mainFilters.Add("hwdownload"); |
| 0 | 5285 | | mainFilters.Add("format=nv12"); |
| | 5286 | | } |
| | 5287 | |
|
| 0 | 5288 | | if (isSwDecoder && isVaapiEncoder && !doVkTonemap) |
| | 5289 | | { |
| 0 | 5290 | | mainFilters.Add("hwupload_vaapi"); |
| | 5291 | | } |
| | 5292 | | } |
| | 5293 | |
|
| | 5294 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5295 | | var subFilters = new List<string>(); |
| 0 | 5296 | | var overlayFilters = new List<string>(); |
| 0 | 5297 | | if (hasSubs) |
| | 5298 | | { |
| 0 | 5299 | | if (hasGraphicalSubs) |
| | 5300 | | { |
| 0 | 5301 | | var subW = state.SubtitleStream?.Width; |
| 0 | 5302 | | var subH = state.SubtitleStream?.Height; |
| 0 | 5303 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 5304 | | subFilters.Add(subPreProcFilters); |
| 0 | 5305 | | subFilters.Add("format=bgra"); |
| | 5306 | | } |
| 0 | 5307 | | else if (hasTextSubs) |
| | 5308 | | { |
| 0 | 5309 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 5310 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 5311 | |
|
| 0 | 5312 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subFrame |
| 0 | 5313 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 5314 | | subFilters.Add(alphaSrcFilter); |
| 0 | 5315 | | subFilters.Add("format=bgra"); |
| 0 | 5316 | | subFilters.Add(subTextSubtitlesFilter); |
| | 5317 | | } |
| | 5318 | |
|
| 0 | 5319 | | subFilters.Add("hwupload=derive_device=vulkan"); |
| 0 | 5320 | | subFilters.Add("format=vulkan"); |
| | 5321 | |
|
| 0 | 5322 | | overlayFilters.Add("overlay_vulkan=eof_action=pass:repeatlast=0"); |
| | 5323 | |
|
| 0 | 5324 | | if (isSwEncoder) |
| | 5325 | | { |
| | 5326 | | // OUTPUT nv12 surface(memory) |
| 0 | 5327 | | overlayFilters.Add("scale_vulkan=format=nv12"); |
| 0 | 5328 | | overlayFilters.Add("hwdownload"); |
| 0 | 5329 | | overlayFilters.Add("format=nv12"); |
| | 5330 | | } |
| 0 | 5331 | | else if (isVaapiEncoder) |
| | 5332 | | { |
| | 5333 | | // OUTPUT vaapi(nv12) surface(vram) |
| | 5334 | | // map from vulkan/drm to vaapi via interop (Polaris/gfx8+). |
| 0 | 5335 | | overlayFilters.Add("hwmap=derive_device=vaapi"); |
| 0 | 5336 | | overlayFilters.Add("format=vaapi"); |
| | 5337 | |
|
| | 5338 | | // clear the surf->meta_offset and output nv12 |
| 0 | 5339 | | overlayFilters.Add("scale_vaapi=format=nv12"); |
| | 5340 | |
|
| | 5341 | | // hw deint |
| 0 | 5342 | | if (doDeintH2645) |
| | 5343 | | { |
| 0 | 5344 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 5345 | | overlayFilters.Add(deintFilter); |
| | 5346 | | } |
| | 5347 | | } |
| | 5348 | | } |
| | 5349 | |
|
| 0 | 5350 | | return (mainFilters, subFilters, overlayFilters); |
| | 5351 | | } |
| | 5352 | |
|
| | 5353 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetVaapiLimitedVidFilter |
| | 5354 | | EncodingJobInfo state, |
| | 5355 | | EncodingOptions options, |
| | 5356 | | string vidDecoder, |
| | 5357 | | string vidEncoder) |
| | 5358 | | { |
| 0 | 5359 | | var inW = state.VideoStream?.Width; |
| 0 | 5360 | | var inH = state.VideoStream?.Height; |
| 0 | 5361 | | var reqW = state.BaseRequest.Width; |
| 0 | 5362 | | var reqH = state.BaseRequest.Height; |
| 0 | 5363 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 5364 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 5365 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 5366 | |
|
| 0 | 5367 | | var isVaapiDecoder = vidDecoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 5368 | | var isVaapiEncoder = vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase); |
| 0 | 5369 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 5370 | | var isSwEncoder = !isVaapiEncoder; |
| 0 | 5371 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 5372 | | var isVaInVaOut = isVaapiDecoder && isVaapiEncoder; |
| 0 | 5373 | | var isi965Driver = _mediaEncoder.IsVaapiDeviceInteli965; |
| 0 | 5374 | | var isAmdDriver = _mediaEncoder.IsVaapiDeviceAmd; |
| | 5375 | |
|
| 0 | 5376 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 5377 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 5378 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 5379 | | var doOclTonemap = IsHwTonemapAvailable(state, options); |
| | 5380 | |
|
| 0 | 5381 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 5382 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5383 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| | 5384 | |
|
| 0 | 5385 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 5386 | | var swapWAndH = Math.Abs(rotation) == 90 && isSwDecoder; |
| 0 | 5387 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 5388 | | var swpInH = swapWAndH ? inW : inH; |
| | 5389 | |
|
| | 5390 | | /* Make main filters for video stream */ |
| 0 | 5391 | | var mainFilters = new List<string>(); |
| | 5392 | |
|
| 0 | 5393 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doOclTonemap)); |
| | 5394 | |
|
| 0 | 5395 | | var outFormat = string.Empty; |
| 0 | 5396 | | if (isSwDecoder) |
| | 5397 | | { |
| | 5398 | | // INPUT sw surface(memory) |
| | 5399 | | // sw deint |
| 0 | 5400 | | if (doDeintH2645) |
| | 5401 | | { |
| 0 | 5402 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 5403 | | mainFilters.Add(swDeintFilter); |
| | 5404 | | } |
| | 5405 | |
|
| 0 | 5406 | | outFormat = doOclTonemap ? "yuv420p10le" : "nv12"; |
| 0 | 5407 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 5408 | | if (isMjpegEncoder && !doOclTonemap) |
| | 5409 | | { |
| | 5410 | | // sw decoder + hw mjpeg encoder |
| 0 | 5411 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 5412 | | } |
| | 5413 | |
|
| | 5414 | | // sw scale |
| 0 | 5415 | | mainFilters.Add(swScaleFilter); |
| 0 | 5416 | | mainFilters.Add("format=" + outFormat); |
| | 5417 | |
|
| | 5418 | | // keep video at memory except ocl tonemap, |
| | 5419 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 5420 | | // sw => hw |
| 0 | 5421 | | if (doOclTonemap) |
| | 5422 | | { |
| 0 | 5423 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 5424 | | } |
| | 5425 | | } |
| 0 | 5426 | | else if (isVaapiDecoder) |
| | 5427 | | { |
| | 5428 | | // INPUT vaapi surface(vram) |
| | 5429 | | // hw deint |
| 0 | 5430 | | if (doDeintH2645) |
| | 5431 | | { |
| 0 | 5432 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "vaapi"); |
| 0 | 5433 | | mainFilters.Add(deintFilter); |
| | 5434 | | } |
| | 5435 | |
|
| 0 | 5436 | | outFormat = doOclTonemap ? string.Empty : "nv12"; |
| 0 | 5437 | | var hwScaleFilter = GetHwScaleFilter("scale", "vaapi", outFormat, false, inW, inH, reqW, reqH, reqMaxW, |
| | 5438 | |
|
| 0 | 5439 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isMjpegEncoder) |
| | 5440 | | { |
| 0 | 5441 | | hwScaleFilter += doOclTonemap ? string.Empty : ":out_range=pc"; |
| 0 | 5442 | | hwScaleFilter += ":mode=hq"; |
| | 5443 | | } |
| | 5444 | |
|
| | 5445 | | // allocate extra pool sizes for vaapi vpp |
| 0 | 5446 | | if (!string.IsNullOrEmpty(hwScaleFilter)) |
| | 5447 | | { |
| 0 | 5448 | | hwScaleFilter += ":extra_hw_frames=24"; |
| | 5449 | | } |
| | 5450 | |
|
| | 5451 | | // hw scale |
| 0 | 5452 | | mainFilters.Add(hwScaleFilter); |
| | 5453 | | } |
| | 5454 | |
|
| 0 | 5455 | | if (doOclTonemap && isVaapiDecoder) |
| | 5456 | | { |
| 0 | 5457 | | if (isi965Driver) |
| | 5458 | | { |
| | 5459 | | // map from vaapi to opencl via vaapi-opencl interop(Intel only). |
| 0 | 5460 | | mainFilters.Add("hwmap=derive_device=opencl"); |
| | 5461 | | } |
| | 5462 | | else |
| | 5463 | | { |
| 0 | 5464 | | mainFilters.Add("hwdownload"); |
| 0 | 5465 | | mainFilters.Add("format=p010le"); |
| 0 | 5466 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 5467 | | } |
| | 5468 | | } |
| | 5469 | |
|
| | 5470 | | // ocl tonemap |
| 0 | 5471 | | if (doOclTonemap) |
| | 5472 | | { |
| 0 | 5473 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 5474 | | mainFilters.Add(tonemapFilter); |
| | 5475 | | } |
| | 5476 | |
|
| 0 | 5477 | | if (doOclTonemap && isVaInVaOut) |
| | 5478 | | { |
| 0 | 5479 | | if (isi965Driver) |
| | 5480 | | { |
| | 5481 | | // OUTPUT vaapi(nv12) surface(vram) |
| | 5482 | | // reverse-mapping via vaapi-opencl interop. |
| 0 | 5483 | | mainFilters.Add("hwmap=derive_device=vaapi:reverse=1"); |
| 0 | 5484 | | mainFilters.Add("format=vaapi"); |
| | 5485 | | } |
| | 5486 | | } |
| | 5487 | |
|
| 0 | 5488 | | var memoryOutput = false; |
| 0 | 5489 | | var isUploadForOclTonemap = doOclTonemap && (isSwDecoder || (isVaapiDecoder && !isi965Driver)); |
| 0 | 5490 | | var isHwmapNotUsable = hasGraphicalSubs || isUploadForOclTonemap; |
| 0 | 5491 | | var isHwmapForSubs = hasSubs && isVaapiDecoder; |
| 0 | 5492 | | var isHwUnmapForTextSubs = hasTextSubs && isVaInVaOut && !isUploadForOclTonemap; |
| 0 | 5493 | | if ((isVaapiDecoder && isSwEncoder) || isUploadForOclTonemap || isHwmapForSubs) |
| | 5494 | | { |
| 0 | 5495 | | memoryOutput = true; |
| | 5496 | |
|
| | 5497 | | // OUTPUT nv12 surface(memory) |
| | 5498 | | // prefer hwmap to hwdownload on opencl/vaapi. |
| 0 | 5499 | | mainFilters.Add(isHwmapNotUsable ? "hwdownload" : "hwmap"); |
| 0 | 5500 | | mainFilters.Add("format=nv12"); |
| | 5501 | | } |
| | 5502 | |
|
| | 5503 | | // OUTPUT nv12 surface(memory) |
| 0 | 5504 | | if (isSwDecoder && isVaapiEncoder) |
| | 5505 | | { |
| 0 | 5506 | | memoryOutput = true; |
| | 5507 | | } |
| | 5508 | |
|
| 0 | 5509 | | if (memoryOutput) |
| | 5510 | | { |
| | 5511 | | // text subtitles |
| 0 | 5512 | | if (hasTextSubs) |
| | 5513 | | { |
| 0 | 5514 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 5515 | | mainFilters.Add(textSubtitlesFilter); |
| | 5516 | | } |
| | 5517 | | } |
| | 5518 | |
|
| 0 | 5519 | | if (isHwUnmapForTextSubs) |
| | 5520 | | { |
| 0 | 5521 | | mainFilters.Add("hwmap"); |
| 0 | 5522 | | mainFilters.Add("format=vaapi"); |
| | 5523 | | } |
| 0 | 5524 | | else if (memoryOutput && isVaapiEncoder) |
| | 5525 | | { |
| 0 | 5526 | | if (!hasGraphicalSubs) |
| | 5527 | | { |
| 0 | 5528 | | mainFilters.Add("hwupload_vaapi"); |
| | 5529 | | } |
| | 5530 | | } |
| | 5531 | |
|
| | 5532 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5533 | | var subFilters = new List<string>(); |
| 0 | 5534 | | var overlayFilters = new List<string>(); |
| 0 | 5535 | | if (memoryOutput) |
| | 5536 | | { |
| 0 | 5537 | | if (hasGraphicalSubs) |
| | 5538 | | { |
| 0 | 5539 | | var subW = state.SubtitleStream?.Width; |
| 0 | 5540 | | var subH = state.SubtitleStream?.Height; |
| 0 | 5541 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 5542 | | subFilters.Add(subPreProcFilters); |
| 0 | 5543 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 5544 | |
|
| 0 | 5545 | | if (isVaapiEncoder) |
| | 5546 | | { |
| 0 | 5547 | | overlayFilters.Add("hwupload_vaapi"); |
| | 5548 | | } |
| | 5549 | | } |
| | 5550 | | } |
| | 5551 | |
|
| 0 | 5552 | | return (mainFilters, subFilters, overlayFilters); |
| | 5553 | | } |
| | 5554 | |
|
| | 5555 | | /// <summary> |
| | 5556 | | /// Gets the parameter of Apple VideoToolBox filter chain. |
| | 5557 | | /// </summary> |
| | 5558 | | /// <param name="state">Encoding state.</param> |
| | 5559 | | /// <param name="options">Encoding options.</param> |
| | 5560 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 5561 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 5562 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAppleVidFilterChain( |
| | 5563 | | EncodingJobInfo state, |
| | 5564 | | EncodingOptions options, |
| | 5565 | | string vidEncoder) |
| | 5566 | | { |
| 0 | 5567 | | if (options.HardwareAccelerationType != HardwareAccelerationType.videotoolbox) |
| | 5568 | | { |
| 0 | 5569 | | return (null, null, null); |
| | 5570 | | } |
| | 5571 | |
|
| | 5572 | | // ReSharper disable once InconsistentNaming |
| 0 | 5573 | | var isMacOS = OperatingSystem.IsMacOS(); |
| 0 | 5574 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 5575 | | var isVtDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 5576 | | var isVtEncoder = vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 5577 | | var isVtFullSupported = isMacOS && IsVideoToolboxFullSupported(); |
| | 5578 | |
|
| | 5579 | | // legacy videotoolbox pipeline (disable hw filters) |
| 0 | 5580 | | if (!(isVtEncoder || isVtDecoder) |
| 0 | 5581 | | || !isVtFullSupported |
| 0 | 5582 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 5583 | | { |
| 0 | 5584 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 5585 | | } |
| | 5586 | |
|
| | 5587 | | // preferred videotoolbox + metal filters pipeline |
| 0 | 5588 | | return GetAppleVidFiltersPreferred(state, options, vidDecoder, vidEncoder); |
| | 5589 | | } |
| | 5590 | |
|
| | 5591 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetAppleVidFiltersPrefer |
| | 5592 | | EncodingJobInfo state, |
| | 5593 | | EncodingOptions options, |
| | 5594 | | string vidDecoder, |
| | 5595 | | string vidEncoder) |
| | 5596 | | { |
| 0 | 5597 | | var isVtEncoder = vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 5598 | | var isVtDecoder = vidDecoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase); |
| 0 | 5599 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| | 5600 | |
|
| 0 | 5601 | | var inW = state.VideoStream?.Width; |
| 0 | 5602 | | var inH = state.VideoStream?.Height; |
| 0 | 5603 | | var reqW = state.BaseRequest.Width; |
| 0 | 5604 | | var reqH = state.BaseRequest.Height; |
| 0 | 5605 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 5606 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 5607 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 5608 | |
|
| 0 | 5609 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 5610 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 5611 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 5612 | | var doVtTonemap = IsVideoToolboxTonemapAvailable(state, options); |
| 0 | 5613 | | var doMetalTonemap = !doVtTonemap && IsHwTonemapAvailable(state, options); |
| 0 | 5614 | | var usingHwSurface = isVtDecoder && (_mediaEncoder.EncoderVersion >= _minFFmpegWorkingVtHwSurface); |
| | 5615 | |
|
| 0 | 5616 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 5617 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 5618 | | var doVtTranspose = !string.IsNullOrEmpty(transposeDir) && _mediaEncoder.SupportsFilter("transpose_vt"); |
| 0 | 5619 | | var swapWAndH = Math.Abs(rotation) == 90 && doVtTranspose; |
| 0 | 5620 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 5621 | | var swpInH = swapWAndH ? inW : inH; |
| | 5622 | |
|
| 0 | 5623 | | var scaleFormat = string.Empty; |
| | 5624 | | // Use P010 for Metal tone mapping, otherwise force an 8bit output. |
| 0 | 5625 | | if (!string.Equals(state.VideoStream.PixelFormat, "yuv420p", StringComparison.OrdinalIgnoreCase)) |
| | 5626 | | { |
| 0 | 5627 | | if (doMetalTonemap) |
| | 5628 | | { |
| 0 | 5629 | | if (!string.Equals(state.VideoStream.PixelFormat, "yuv420p10le", StringComparison.OrdinalIgnoreCase) |
| | 5630 | | { |
| 0 | 5631 | | scaleFormat = "p010le"; |
| | 5632 | | } |
| | 5633 | | } |
| | 5634 | | else |
| | 5635 | | { |
| 0 | 5636 | | scaleFormat = "nv12"; |
| | 5637 | | } |
| | 5638 | | } |
| | 5639 | |
|
| 0 | 5640 | | var hwScaleFilter = GetHwScaleFilter("scale", "vt", scaleFormat, false, swpInW, swpInH, reqW, reqH, reqMaxW, |
| | 5641 | |
|
| 0 | 5642 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 5643 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5644 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5645 | | var hasAssSubs = hasSubs |
| 0 | 5646 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 5647 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| | 5648 | |
|
| | 5649 | | /* Make main filters for video stream */ |
| 0 | 5650 | | var mainFilters = new List<string>(); |
| | 5651 | |
|
| | 5652 | | // hw deint |
| 0 | 5653 | | if (doDeintH2645) |
| | 5654 | | { |
| 0 | 5655 | | var deintFilter = GetHwDeinterlaceFilter(state, options, "videotoolbox"); |
| 0 | 5656 | | mainFilters.Add(deintFilter); |
| | 5657 | | } |
| | 5658 | |
|
| | 5659 | | // hw transpose |
| 0 | 5660 | | if (doVtTranspose) |
| | 5661 | | { |
| 0 | 5662 | | mainFilters.Add($"transpose_vt=dir={transposeDir}"); |
| | 5663 | | } |
| | 5664 | |
|
| 0 | 5665 | | if (doVtTonemap) |
| | 5666 | | { |
| | 5667 | | const string VtTonemapArgs = "color_matrix=bt709:color_primaries=bt709:color_transfer=bt709"; |
| | 5668 | |
|
| | 5669 | | // scale_vt can handle scaling & tonemapping in one shot, just like vpp_qsv. |
| 0 | 5670 | | hwScaleFilter = string.IsNullOrEmpty(hwScaleFilter) |
| 0 | 5671 | | ? "scale_vt=" + VtTonemapArgs |
| 0 | 5672 | | : hwScaleFilter + ":" + VtTonemapArgs; |
| | 5673 | | } |
| | 5674 | |
|
| | 5675 | | // hw scale & vt tonemap |
| 0 | 5676 | | mainFilters.Add(hwScaleFilter); |
| | 5677 | |
|
| | 5678 | | // Metal tonemap |
| 0 | 5679 | | if (doMetalTonemap) |
| | 5680 | | { |
| 0 | 5681 | | var tonemapFilter = GetHwTonemapFilter(options, "videotoolbox", "nv12", isMjpegEncoder); |
| 0 | 5682 | | mainFilters.Add(tonemapFilter); |
| | 5683 | | } |
| | 5684 | |
|
| | 5685 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5686 | | var subFilters = new List<string>(); |
| 0 | 5687 | | var overlayFilters = new List<string>(); |
| | 5688 | |
|
| 0 | 5689 | | if (hasSubs) |
| | 5690 | | { |
| 0 | 5691 | | if (hasGraphicalSubs) |
| | 5692 | | { |
| 0 | 5693 | | var subW = state.SubtitleStream?.Width; |
| 0 | 5694 | | var subH = state.SubtitleStream?.Height; |
| 0 | 5695 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 5696 | | subFilters.Add(subPreProcFilters); |
| 0 | 5697 | | subFilters.Add("format=bgra"); |
| | 5698 | | } |
| 0 | 5699 | | else if (hasTextSubs) |
| | 5700 | | { |
| 0 | 5701 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 5702 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 5703 | |
|
| 0 | 5704 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subFrame |
| 0 | 5705 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 5706 | | subFilters.Add(alphaSrcFilter); |
| 0 | 5707 | | subFilters.Add("format=bgra"); |
| 0 | 5708 | | subFilters.Add(subTextSubtitlesFilter); |
| | 5709 | | } |
| | 5710 | |
|
| 0 | 5711 | | subFilters.Add("hwupload"); |
| 0 | 5712 | | overlayFilters.Add("overlay_videotoolbox=eof_action=pass:repeatlast=0"); |
| | 5713 | | } |
| | 5714 | |
|
| 0 | 5715 | | if (usingHwSurface) |
| | 5716 | | { |
| 0 | 5717 | | if (!isVtEncoder) |
| | 5718 | | { |
| 0 | 5719 | | mainFilters.Add("hwdownload"); |
| 0 | 5720 | | mainFilters.Add("format=nv12"); |
| | 5721 | | } |
| | 5722 | |
|
| 0 | 5723 | | return (mainFilters, subFilters, overlayFilters); |
| | 5724 | | } |
| | 5725 | |
|
| | 5726 | | // For old jellyfin-ffmpeg that has broken hwsurface, add a hwupload |
| 0 | 5727 | | var needFiltering = mainFilters.Any(f => !string.IsNullOrEmpty(f)) || |
| 0 | 5728 | | subFilters.Any(f => !string.IsNullOrEmpty(f)) || |
| 0 | 5729 | | overlayFilters.Any(f => !string.IsNullOrEmpty(f)); |
| 0 | 5730 | | if (needFiltering) |
| | 5731 | | { |
| | 5732 | | // INPUT videotoolbox/memory surface(vram/uma) |
| | 5733 | | // this will pass-through automatically if in/out format matches. |
| 0 | 5734 | | mainFilters.Insert(0, "hwupload"); |
| 0 | 5735 | | mainFilters.Insert(0, "format=nv12|p010le|videotoolbox_vld"); |
| | 5736 | |
|
| 0 | 5737 | | if (!isVtEncoder) |
| | 5738 | | { |
| 0 | 5739 | | mainFilters.Add("hwdownload"); |
| 0 | 5740 | | mainFilters.Add("format=nv12"); |
| | 5741 | | } |
| | 5742 | | } |
| | 5743 | |
|
| 0 | 5744 | | return (mainFilters, subFilters, overlayFilters); |
| | 5745 | | } |
| | 5746 | |
|
| | 5747 | | /// <summary> |
| | 5748 | | /// Gets the parameter of Rockchip RKMPP/RKRGA filter chain. |
| | 5749 | | /// </summary> |
| | 5750 | | /// <param name="state">Encoding state.</param> |
| | 5751 | | /// <param name="options">Encoding options.</param> |
| | 5752 | | /// <param name="vidEncoder">Video encoder to use.</param> |
| | 5753 | | /// <returns>The tuple contains three lists: main, sub and overlay filters.</returns> |
| | 5754 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetRkmppVidFilterChain( |
| | 5755 | | EncodingJobInfo state, |
| | 5756 | | EncodingOptions options, |
| | 5757 | | string vidEncoder) |
| | 5758 | | { |
| 0 | 5759 | | if (options.HardwareAccelerationType != HardwareAccelerationType.rkmpp) |
| | 5760 | | { |
| 0 | 5761 | | return (null, null, null); |
| | 5762 | | } |
| | 5763 | |
|
| 0 | 5764 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 5765 | | var vidDecoder = GetHardwareVideoDecoder(state, options) ?? string.Empty; |
| 0 | 5766 | | var isSwDecoder = string.IsNullOrEmpty(vidDecoder); |
| 0 | 5767 | | var isSwEncoder = !vidEncoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 5768 | | var isRkmppOclSupported = isLinux && IsRkmppFullSupported() && IsOpenclFullSupported(); |
| | 5769 | |
|
| 0 | 5770 | | if ((isSwDecoder && isSwEncoder) |
| 0 | 5771 | | || !isRkmppOclSupported |
| 0 | 5772 | | || !_mediaEncoder.SupportsFilter("alphasrc")) |
| | 5773 | | { |
| 0 | 5774 | | return GetSwVidFilterChain(state, options, vidEncoder); |
| | 5775 | | } |
| | 5776 | |
|
| | 5777 | | // preferred rkmpp + rkrga + opencl filters pipeline |
| 0 | 5778 | | if (isRkmppOclSupported) |
| | 5779 | | { |
| 0 | 5780 | | return GetRkmppVidFiltersPrefered(state, options, vidDecoder, vidEncoder); |
| | 5781 | | } |
| | 5782 | |
|
| 0 | 5783 | | return (null, null, null); |
| | 5784 | | } |
| | 5785 | |
|
| | 5786 | | public (List<string> MainFilters, List<string> SubFilters, List<string> OverlayFilters) GetRkmppVidFiltersPrefer |
| | 5787 | | EncodingJobInfo state, |
| | 5788 | | EncodingOptions options, |
| | 5789 | | string vidDecoder, |
| | 5790 | | string vidEncoder) |
| | 5791 | | { |
| 0 | 5792 | | var inW = state.VideoStream?.Width; |
| 0 | 5793 | | var inH = state.VideoStream?.Height; |
| 0 | 5794 | | var reqW = state.BaseRequest.Width; |
| 0 | 5795 | | var reqH = state.BaseRequest.Height; |
| 0 | 5796 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 5797 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| 0 | 5798 | | var threeDFormat = state.MediaSource.Video3DFormat; |
| | 5799 | |
|
| 0 | 5800 | | var isRkmppDecoder = vidDecoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 5801 | | var isRkmppEncoder = vidEncoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase); |
| 0 | 5802 | | var isSwDecoder = !isRkmppDecoder; |
| 0 | 5803 | | var isSwEncoder = !isRkmppEncoder; |
| 0 | 5804 | | var isMjpegEncoder = vidEncoder.Contains("mjpeg", StringComparison.OrdinalIgnoreCase); |
| 0 | 5805 | | var isDrmInDrmOut = isRkmppDecoder && isRkmppEncoder; |
| 0 | 5806 | | var isEncoderSupportAfbc = isRkmppEncoder |
| 0 | 5807 | | && (vidEncoder.Contains("h264", StringComparison.OrdinalIgnoreCase) |
| 0 | 5808 | | || vidEncoder.Contains("hevc", StringComparison.OrdinalIgnoreCase)); |
| | 5809 | |
|
| 0 | 5810 | | var doDeintH264 = state.DeInterlace("h264", true) || state.DeInterlace("avc", true); |
| 0 | 5811 | | var doDeintHevc = state.DeInterlace("h265", true) || state.DeInterlace("hevc", true); |
| 0 | 5812 | | var doDeintH2645 = doDeintH264 || doDeintHevc; |
| 0 | 5813 | | var doOclTonemap = IsHwTonemapAvailable(state, options); |
| | 5814 | |
|
| 0 | 5815 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 5816 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5817 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 5818 | | var hasAssSubs = hasSubs |
| 0 | 5819 | | && (string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| 0 | 5820 | | || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)); |
| 0 | 5821 | | var subW = state.SubtitleStream?.Width; |
| 0 | 5822 | | var subH = state.SubtitleStream?.Height; |
| | 5823 | |
|
| 0 | 5824 | | var rotation = state.VideoStream?.Rotation ?? 0; |
| 0 | 5825 | | var transposeDir = rotation == 0 ? string.Empty : GetVideoTransposeDirection(state); |
| 0 | 5826 | | var doRkVppTranspose = !string.IsNullOrEmpty(transposeDir); |
| 0 | 5827 | | var swapWAndH = Math.Abs(rotation) == 90 && (isSwDecoder || (isRkmppDecoder && doRkVppTranspose)); |
| 0 | 5828 | | var swpInW = swapWAndH ? inH : inW; |
| 0 | 5829 | | var swpInH = swapWAndH ? inW : inH; |
| | 5830 | |
|
| | 5831 | | /* Make main filters for video stream */ |
| 0 | 5832 | | var mainFilters = new List<string>(); |
| | 5833 | |
|
| 0 | 5834 | | mainFilters.Add(GetOverwriteColorPropertiesParam(state, doOclTonemap)); |
| | 5835 | |
|
| 0 | 5836 | | if (isSwDecoder) |
| | 5837 | | { |
| | 5838 | | // INPUT sw surface(memory) |
| | 5839 | | // sw deint |
| 0 | 5840 | | if (doDeintH2645) |
| | 5841 | | { |
| 0 | 5842 | | var swDeintFilter = GetSwDeinterlaceFilter(state, options); |
| 0 | 5843 | | mainFilters.Add(swDeintFilter); |
| | 5844 | | } |
| | 5845 | |
|
| 0 | 5846 | | var outFormat = doOclTonemap ? "yuv420p10le" : (hasGraphicalSubs ? "yuv420p" : "nv12"); |
| 0 | 5847 | | var swScaleFilter = GetSwScaleFilter(state, options, vidEncoder, swpInW, swpInH, threeDFormat, reqW, req |
| 0 | 5848 | | if (isMjpegEncoder && !doOclTonemap) |
| | 5849 | | { |
| | 5850 | | // sw decoder + hw mjpeg encoder |
| 0 | 5851 | | swScaleFilter = string.IsNullOrEmpty(swScaleFilter) ? "scale=out_range=pc" : $"{swScaleFilter}:out_r |
| | 5852 | | } |
| | 5853 | |
|
| 0 | 5854 | | if (!string.IsNullOrEmpty(swScaleFilter)) |
| | 5855 | | { |
| 0 | 5856 | | swScaleFilter += ":flags=fast_bilinear"; |
| | 5857 | | } |
| | 5858 | |
|
| | 5859 | | // sw scale |
| 0 | 5860 | | mainFilters.Add(swScaleFilter); |
| 0 | 5861 | | mainFilters.Add($"format={outFormat}"); |
| | 5862 | |
|
| | 5863 | | // keep video at memory except ocl tonemap, |
| | 5864 | | // since the overhead caused by hwupload >>> using sw filter. |
| | 5865 | | // sw => hw |
| 0 | 5866 | | if (doOclTonemap) |
| | 5867 | | { |
| 0 | 5868 | | mainFilters.Add("hwupload=derive_device=opencl"); |
| | 5869 | | } |
| | 5870 | | } |
| 0 | 5871 | | else if (isRkmppDecoder) |
| | 5872 | | { |
| | 5873 | | // INPUT rkmpp/drm surface(gem/dma-heap) |
| | 5874 | |
|
| 0 | 5875 | | var isFullAfbcPipeline = isEncoderSupportAfbc && isDrmInDrmOut && !doOclTonemap; |
| 0 | 5876 | | var swapOutputWandH = doRkVppTranspose && swapWAndH; |
| 0 | 5877 | | var outFormat = doOclTonemap ? "p010" : (isMjpegEncoder ? "bgra" : "nv12"); // RGA only support full ran |
| 0 | 5878 | | var hwScaleFilter = GetHwScaleFilter("vpp", "rkrga", outFormat, swapOutputWandH, swpInW, swpInH, reqW, r |
| 0 | 5879 | | var doScaling = GetHwScaleFilter("vpp", "rkrga", string.Empty, swapOutputWandH, swpInW, swpInH, reqW, re |
| | 5880 | |
|
| 0 | 5881 | | if (!hasSubs |
| 0 | 5882 | | || doRkVppTranspose |
| 0 | 5883 | | || !isFullAfbcPipeline |
| 0 | 5884 | | || !string.IsNullOrEmpty(doScaling)) |
| | 5885 | | { |
| | 5886 | | // RGA3 hardware only support (1/8 ~ 8) scaling in each blit operation, |
| | 5887 | | // but in Trickplay there's a case: (3840/320 == 12), enable 2pass for it |
| 0 | 5888 | | if (!string.IsNullOrEmpty(doScaling) |
| 0 | 5889 | | && !IsScaleRatioSupported(inW, inH, reqW, reqH, reqMaxW, reqMaxH, 8.0f)) |
| | 5890 | | { |
| | 5891 | | // Vendor provided BSP kernel has an RGA driver bug that causes the output to be corrupted for P |
| | 5892 | | // Use NV15 instead of P010 to avoid the issue. |
| | 5893 | | // SDR inputs are using BGRA formats already which is not affected. |
| 0 | 5894 | | var intermediateFormat = string.Equals(outFormat, "p010", StringComparison.OrdinalIgnoreCase) ? |
| 0 | 5895 | | var hwScaleFilterFirstPass = $"scale_rkrga=w=iw/7.9:h=ih/7.9:format={intermediateFormat}:force_d |
| 0 | 5896 | | mainFilters.Add(hwScaleFilterFirstPass); |
| | 5897 | | } |
| | 5898 | |
|
| 0 | 5899 | | if (!string.IsNullOrEmpty(hwScaleFilter) && doRkVppTranspose) |
| | 5900 | | { |
| 0 | 5901 | | hwScaleFilter += $":transpose={transposeDir}"; |
| | 5902 | | } |
| | 5903 | |
|
| | 5904 | | // try enabling AFBC to save DDR bandwidth |
| 0 | 5905 | | if (!string.IsNullOrEmpty(hwScaleFilter) && isFullAfbcPipeline) |
| | 5906 | | { |
| 0 | 5907 | | hwScaleFilter += ":afbc=1"; |
| | 5908 | | } |
| | 5909 | |
|
| | 5910 | | // hw transpose & scale |
| 0 | 5911 | | mainFilters.Add(hwScaleFilter); |
| | 5912 | | } |
| | 5913 | | } |
| | 5914 | |
|
| 0 | 5915 | | if (doOclTonemap && isRkmppDecoder) |
| | 5916 | | { |
| | 5917 | | // map from rkmpp/drm to opencl via drm-opencl interop. |
| 0 | 5918 | | mainFilters.Add("hwmap=derive_device=opencl"); |
| | 5919 | | } |
| | 5920 | |
|
| | 5921 | | // ocl tonemap |
| 0 | 5922 | | if (doOclTonemap) |
| | 5923 | | { |
| 0 | 5924 | | var tonemapFilter = GetHwTonemapFilter(options, "opencl", "nv12", isMjpegEncoder); |
| 0 | 5925 | | mainFilters.Add(tonemapFilter); |
| | 5926 | | } |
| | 5927 | |
|
| 0 | 5928 | | var memoryOutput = false; |
| 0 | 5929 | | var isUploadForOclTonemap = isSwDecoder && doOclTonemap; |
| 0 | 5930 | | if ((isRkmppDecoder && isSwEncoder) || isUploadForOclTonemap) |
| | 5931 | | { |
| 0 | 5932 | | memoryOutput = true; |
| | 5933 | |
|
| | 5934 | | // OUTPUT nv12 surface(memory) |
| 0 | 5935 | | mainFilters.Add("hwdownload"); |
| 0 | 5936 | | mainFilters.Add("format=nv12"); |
| | 5937 | | } |
| | 5938 | |
|
| | 5939 | | // OUTPUT nv12 surface(memory) |
| 0 | 5940 | | if (isSwDecoder && isRkmppEncoder) |
| | 5941 | | { |
| 0 | 5942 | | memoryOutput = true; |
| | 5943 | | } |
| | 5944 | |
|
| 0 | 5945 | | if (memoryOutput) |
| | 5946 | | { |
| | 5947 | | // text subtitles |
| 0 | 5948 | | if (hasTextSubs) |
| | 5949 | | { |
| 0 | 5950 | | var textSubtitlesFilter = GetTextSubtitlesFilter(state, false, false); |
| 0 | 5951 | | mainFilters.Add(textSubtitlesFilter); |
| | 5952 | | } |
| | 5953 | | } |
| | 5954 | |
|
| 0 | 5955 | | if (isDrmInDrmOut) |
| | 5956 | | { |
| 0 | 5957 | | if (doOclTonemap) |
| | 5958 | | { |
| | 5959 | | // OUTPUT drm(nv12) surface(gem/dma-heap) |
| | 5960 | | // reverse-mapping via drm-opencl interop. |
| 0 | 5961 | | mainFilters.Add("hwmap=derive_device=rkmpp:reverse=1"); |
| 0 | 5962 | | mainFilters.Add("format=drm_prime"); |
| | 5963 | | } |
| | 5964 | | } |
| | 5965 | |
|
| | 5966 | | /* Make sub and overlay filters for subtitle stream */ |
| 0 | 5967 | | var subFilters = new List<string>(); |
| 0 | 5968 | | var overlayFilters = new List<string>(); |
| 0 | 5969 | | if (isDrmInDrmOut) |
| | 5970 | | { |
| 0 | 5971 | | if (hasSubs) |
| | 5972 | | { |
| 0 | 5973 | | if (hasGraphicalSubs) |
| | 5974 | | { |
| 0 | 5975 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, |
| 0 | 5976 | | subFilters.Add(subPreProcFilters); |
| 0 | 5977 | | subFilters.Add("format=bgra"); |
| | 5978 | | } |
| 0 | 5979 | | else if (hasTextSubs) |
| | 5980 | | { |
| 0 | 5981 | | var framerate = state.VideoStream?.RealFrameRate; |
| 0 | 5982 | | var subFramerate = hasAssSubs ? Math.Min(framerate ?? 25, 60) : 10; |
| | 5983 | |
|
| | 5984 | | // alphasrc=s=1280x720:r=10:start=0,format=bgra,subtitles,hwupload |
| 0 | 5985 | | var alphaSrcFilter = GetAlphaSrcFilter(state, swpInW, swpInH, reqW, reqH, reqMaxW, reqMaxH, subF |
| 0 | 5986 | | var subTextSubtitlesFilter = GetTextSubtitlesFilter(state, true, true); |
| 0 | 5987 | | subFilters.Add(alphaSrcFilter); |
| 0 | 5988 | | subFilters.Add("format=bgra"); |
| 0 | 5989 | | subFilters.Add(subTextSubtitlesFilter); |
| | 5990 | | } |
| | 5991 | |
|
| 0 | 5992 | | subFilters.Add("hwupload=derive_device=rkmpp"); |
| | 5993 | |
|
| | 5994 | | // try enabling AFBC to save DDR bandwidth |
| 0 | 5995 | | var hwOverlayFilter = "overlay_rkrga=eof_action=pass:repeatlast=0:format=nv12"; |
| 0 | 5996 | | if (isEncoderSupportAfbc) |
| | 5997 | | { |
| 0 | 5998 | | hwOverlayFilter += ":afbc=1"; |
| | 5999 | | } |
| | 6000 | |
|
| 0 | 6001 | | overlayFilters.Add(hwOverlayFilter); |
| | 6002 | | } |
| | 6003 | | } |
| 0 | 6004 | | else if (memoryOutput) |
| | 6005 | | { |
| 0 | 6006 | | if (hasGraphicalSubs) |
| | 6007 | | { |
| 0 | 6008 | | var subPreProcFilters = GetGraphicalSubPreProcessFilters(swpInW, swpInH, subW, subH, reqW, reqH, req |
| 0 | 6009 | | subFilters.Add(subPreProcFilters); |
| 0 | 6010 | | overlayFilters.Add("overlay=eof_action=pass:repeatlast=0"); |
| | 6011 | | } |
| | 6012 | | } |
| | 6013 | |
|
| 0 | 6014 | | return (mainFilters, subFilters, overlayFilters); |
| | 6015 | | } |
| | 6016 | |
|
| | 6017 | | /// <summary> |
| | 6018 | | /// Gets the parameter of video processing filters. |
| | 6019 | | /// </summary> |
| | 6020 | | /// <param name="state">Encoding state.</param> |
| | 6021 | | /// <param name="options">Encoding options.</param> |
| | 6022 | | /// <param name="outputVideoCodec">Video codec to use.</param> |
| | 6023 | | /// <returns>The video processing filters parameter.</returns> |
| | 6024 | | public string GetVideoProcessingFilterParam( |
| | 6025 | | EncodingJobInfo state, |
| | 6026 | | EncodingOptions options, |
| | 6027 | | string outputVideoCodec) |
| | 6028 | | { |
| 0 | 6029 | | var videoStream = state.VideoStream; |
| 0 | 6030 | | if (videoStream is null) |
| | 6031 | | { |
| 0 | 6032 | | return string.Empty; |
| | 6033 | | } |
| | 6034 | |
|
| 0 | 6035 | | var hasSubs = state.SubtitleStream is not null && ShouldEncodeSubtitle(state); |
| 0 | 6036 | | var hasTextSubs = hasSubs && state.SubtitleStream.IsTextSubtitleStream; |
| 0 | 6037 | | var hasGraphicalSubs = hasSubs && !state.SubtitleStream.IsTextSubtitleStream; |
| | 6038 | |
|
| | 6039 | | List<string> mainFilters; |
| | 6040 | | List<string> subFilters; |
| | 6041 | | List<string> overlayFilters; |
| | 6042 | |
|
| 0 | 6043 | | (mainFilters, subFilters, overlayFilters) = options.HardwareAccelerationType switch |
| 0 | 6044 | | { |
| 0 | 6045 | | HardwareAccelerationType.vaapi => GetVaapiVidFilterChain(state, options, outputVideoCodec), |
| 0 | 6046 | | HardwareAccelerationType.amf => GetAmdVidFilterChain(state, options, outputVideoCodec), |
| 0 | 6047 | | HardwareAccelerationType.qsv => GetIntelVidFilterChain(state, options, outputVideoCodec), |
| 0 | 6048 | | HardwareAccelerationType.nvenc => GetNvidiaVidFilterChain(state, options, outputVideoCodec), |
| 0 | 6049 | | HardwareAccelerationType.videotoolbox => GetAppleVidFilterChain(state, options, outputVideoCodec), |
| 0 | 6050 | | HardwareAccelerationType.rkmpp => GetRkmppVidFilterChain(state, options, outputVideoCodec), |
| 0 | 6051 | | _ => GetSwVidFilterChain(state, options, outputVideoCodec), |
| 0 | 6052 | | }; |
| | 6053 | |
|
| 0 | 6054 | | mainFilters?.RemoveAll(string.IsNullOrEmpty); |
| 0 | 6055 | | subFilters?.RemoveAll(string.IsNullOrEmpty); |
| 0 | 6056 | | overlayFilters?.RemoveAll(string.IsNullOrEmpty); |
| | 6057 | |
|
| 0 | 6058 | | var framerate = GetFramerateParam(state); |
| 0 | 6059 | | if (framerate.HasValue) |
| | 6060 | | { |
| 0 | 6061 | | mainFilters.Insert(0, string.Format( |
| 0 | 6062 | | CultureInfo.InvariantCulture, |
| 0 | 6063 | | "fps={0}", |
| 0 | 6064 | | framerate.Value)); |
| | 6065 | | } |
| | 6066 | |
|
| 0 | 6067 | | var mainStr = string.Empty; |
| 0 | 6068 | | if (mainFilters?.Count > 0) |
| | 6069 | | { |
| 0 | 6070 | | mainStr = string.Format( |
| 0 | 6071 | | CultureInfo.InvariantCulture, |
| 0 | 6072 | | "{0}", |
| 0 | 6073 | | string.Join(',', mainFilters)); |
| | 6074 | | } |
| | 6075 | |
|
| 0 | 6076 | | if (overlayFilters?.Count == 0) |
| | 6077 | | { |
| | 6078 | | // -vf "scale..." |
| 0 | 6079 | | return string.IsNullOrEmpty(mainStr) ? string.Empty : " -vf \"" + mainStr + "\""; |
| | 6080 | | } |
| | 6081 | |
|
| 0 | 6082 | | if (overlayFilters?.Count > 0 |
| 0 | 6083 | | && subFilters?.Count > 0 |
| 0 | 6084 | | && state.SubtitleStream is not null) |
| | 6085 | | { |
| | 6086 | | // overlay graphical/text subtitles |
| 0 | 6087 | | var subStr = string.Format( |
| 0 | 6088 | | CultureInfo.InvariantCulture, |
| 0 | 6089 | | "{0}", |
| 0 | 6090 | | string.Join(',', subFilters)); |
| | 6091 | |
|
| 0 | 6092 | | var overlayStr = string.Format( |
| 0 | 6093 | | CultureInfo.InvariantCulture, |
| 0 | 6094 | | "{0}", |
| 0 | 6095 | | string.Join(',', overlayFilters)); |
| | 6096 | |
|
| 0 | 6097 | | var mapPrefix = Convert.ToInt32(state.SubtitleStream.IsExternal); |
| 0 | 6098 | | var subtitleStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.SubtitleStream); |
| 0 | 6099 | | var videoStreamIndex = FindIndex(state.MediaSource.MediaStreams, state.VideoStream); |
| | 6100 | |
|
| 0 | 6101 | | if (hasSubs) |
| | 6102 | | { |
| | 6103 | | // -filter_complex "[0:s]scale=s[sub]..." |
| 0 | 6104 | | var filterStr = string.IsNullOrEmpty(mainStr) |
| 0 | 6105 | | ? " -filter_complex \"[{0}:{1}]{4}[sub];[0:{2}][sub]{5}\"" |
| 0 | 6106 | | : " -filter_complex \"[{0}:{1}]{4}[sub];[0:{2}]{3}[main];[main][sub]{5}\""; |
| | 6107 | |
|
| 0 | 6108 | | if (hasTextSubs) |
| | 6109 | | { |
| 0 | 6110 | | filterStr = string.IsNullOrEmpty(mainStr) |
| 0 | 6111 | | ? " -filter_complex \"{4}[sub];[0:{2}][sub]{5}\"" |
| 0 | 6112 | | : " -filter_complex \"{4}[sub];[0:{2}]{3}[main];[main][sub]{5}\""; |
| | 6113 | | } |
| | 6114 | |
|
| 0 | 6115 | | return string.Format( |
| 0 | 6116 | | CultureInfo.InvariantCulture, |
| 0 | 6117 | | filterStr, |
| 0 | 6118 | | mapPrefix, |
| 0 | 6119 | | subtitleStreamIndex, |
| 0 | 6120 | | videoStreamIndex, |
| 0 | 6121 | | mainStr, |
| 0 | 6122 | | subStr, |
| 0 | 6123 | | overlayStr); |
| | 6124 | | } |
| | 6125 | | } |
| | 6126 | |
|
| 0 | 6127 | | return string.Empty; |
| | 6128 | | } |
| | 6129 | |
|
| | 6130 | | public string GetOverwriteColorPropertiesParam(EncodingJobInfo state, bool isTonemapAvailable) |
| | 6131 | | { |
| 0 | 6132 | | if (isTonemapAvailable) |
| | 6133 | | { |
| 0 | 6134 | | return GetInputHdrParam(state.VideoStream?.ColorTransfer); |
| | 6135 | | } |
| | 6136 | |
|
| 0 | 6137 | | return GetOutputSdrParam(null); |
| | 6138 | | } |
| | 6139 | |
|
| | 6140 | | public string GetInputHdrParam(string colorTransfer) |
| | 6141 | | { |
| 0 | 6142 | | if (string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase)) |
| | 6143 | | { |
| | 6144 | | // HLG |
| 0 | 6145 | | return "setparams=color_primaries=bt2020:color_trc=arib-std-b67:colorspace=bt2020nc"; |
| | 6146 | | } |
| | 6147 | |
|
| | 6148 | | // HDR10 |
| 0 | 6149 | | return "setparams=color_primaries=bt2020:color_trc=smpte2084:colorspace=bt2020nc"; |
| | 6150 | | } |
| | 6151 | |
|
| | 6152 | | public string GetOutputSdrParam(string tonemappingRange) |
| | 6153 | | { |
| | 6154 | | // SDR |
| 0 | 6155 | | if (string.Equals(tonemappingRange, "tv", StringComparison.OrdinalIgnoreCase)) |
| | 6156 | | { |
| 0 | 6157 | | return "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709:range=tv"; |
| | 6158 | | } |
| | 6159 | |
|
| 0 | 6160 | | if (string.Equals(tonemappingRange, "pc", StringComparison.OrdinalIgnoreCase)) |
| | 6161 | | { |
| 0 | 6162 | | return "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709:range=pc"; |
| | 6163 | | } |
| | 6164 | |
|
| 0 | 6165 | | return "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709"; |
| | 6166 | | } |
| | 6167 | |
|
| | 6168 | | public static int GetVideoColorBitDepth(EncodingJobInfo state) |
| | 6169 | | { |
| 0 | 6170 | | var videoStream = state.VideoStream; |
| 0 | 6171 | | if (videoStream is not null) |
| | 6172 | | { |
| 0 | 6173 | | if (videoStream.BitDepth.HasValue) |
| | 6174 | | { |
| 0 | 6175 | | return videoStream.BitDepth.Value; |
| | 6176 | | } |
| | 6177 | |
|
| 0 | 6178 | | if (string.Equals(videoStream.PixelFormat, "yuv420p", StringComparison.OrdinalIgnoreCase) |
| 0 | 6179 | | || string.Equals(videoStream.PixelFormat, "yuvj420p", StringComparison.OrdinalIgnoreCase) |
| 0 | 6180 | | || string.Equals(videoStream.PixelFormat, "yuv422p", StringComparison.OrdinalIgnoreCase) |
| 0 | 6181 | | || string.Equals(videoStream.PixelFormat, "yuv444p", StringComparison.OrdinalIgnoreCase)) |
| | 6182 | | { |
| 0 | 6183 | | return 8; |
| | 6184 | | } |
| | 6185 | |
|
| 0 | 6186 | | if (string.Equals(videoStream.PixelFormat, "yuv420p10le", StringComparison.OrdinalIgnoreCase) |
| 0 | 6187 | | || string.Equals(videoStream.PixelFormat, "yuv422p10le", StringComparison.OrdinalIgnoreCase) |
| 0 | 6188 | | || string.Equals(videoStream.PixelFormat, "yuv444p10le", StringComparison.OrdinalIgnoreCase)) |
| | 6189 | | { |
| 0 | 6190 | | return 10; |
| | 6191 | | } |
| | 6192 | |
|
| 0 | 6193 | | if (string.Equals(videoStream.PixelFormat, "yuv420p12le", StringComparison.OrdinalIgnoreCase) |
| 0 | 6194 | | || string.Equals(videoStream.PixelFormat, "yuv422p12le", StringComparison.OrdinalIgnoreCase) |
| 0 | 6195 | | || string.Equals(videoStream.PixelFormat, "yuv444p12le", StringComparison.OrdinalIgnoreCase)) |
| | 6196 | | { |
| 0 | 6197 | | return 12; |
| | 6198 | | } |
| | 6199 | |
|
| 0 | 6200 | | return 8; |
| | 6201 | | } |
| | 6202 | |
|
| 0 | 6203 | | return 0; |
| | 6204 | | } |
| | 6205 | |
|
| | 6206 | | /// <summary> |
| | 6207 | | /// Gets the ffmpeg option string for the hardware accelerated video decoder. |
| | 6208 | | /// </summary> |
| | 6209 | | /// <param name="state">The encoding job info.</param> |
| | 6210 | | /// <param name="options">The encoding options.</param> |
| | 6211 | | /// <returns>The option string or null if none available.</returns> |
| | 6212 | | protected string GetHardwareVideoDecoder(EncodingJobInfo state, EncodingOptions options) |
| | 6213 | | { |
| 0 | 6214 | | var videoStream = state.VideoStream; |
| 0 | 6215 | | var mediaSource = state.MediaSource; |
| 0 | 6216 | | if (videoStream is null || mediaSource is null) |
| | 6217 | | { |
| 0 | 6218 | | return null; |
| | 6219 | | } |
| | 6220 | |
|
| | 6221 | | // HWA decoders can handle both video files and video folders. |
| 0 | 6222 | | var videoType = state.VideoType; |
| 0 | 6223 | | if (videoType != VideoType.VideoFile |
| 0 | 6224 | | && videoType != VideoType.Iso |
| 0 | 6225 | | && videoType != VideoType.Dvd |
| 0 | 6226 | | && videoType != VideoType.BluRay) |
| | 6227 | | { |
| 0 | 6228 | | return null; |
| | 6229 | | } |
| | 6230 | |
|
| 0 | 6231 | | if (IsCopyCodec(state.OutputVideoCodec)) |
| | 6232 | | { |
| 0 | 6233 | | return null; |
| | 6234 | | } |
| | 6235 | |
|
| 0 | 6236 | | var hardwareAccelerationType = options.HardwareAccelerationType; |
| | 6237 | |
|
| 0 | 6238 | | if (!string.IsNullOrEmpty(videoStream.Codec) && hardwareAccelerationType != HardwareAccelerationType.none) |
| | 6239 | | { |
| 0 | 6240 | | var bitDepth = GetVideoColorBitDepth(state); |
| | 6241 | |
|
| | 6242 | | // Only HEVC, VP9 and AV1 formats have 10-bit hardware decoder support for most platforms |
| 0 | 6243 | | if (bitDepth == 10 |
| 0 | 6244 | | && !(string.Equals(videoStream.Codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6245 | | || string.Equals(videoStream.Codec, "h265", StringComparison.OrdinalIgnoreCase) |
| 0 | 6246 | | || string.Equals(videoStream.Codec, "vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6247 | | || string.Equals(videoStream.Codec, "av1", StringComparison.OrdinalIgnoreCase))) |
| | 6248 | | { |
| | 6249 | | // RKMPP has H.264 Hi10P decoder |
| 0 | 6250 | | bool hasHardwareHi10P = hardwareAccelerationType == HardwareAccelerationType.rkmpp; |
| | 6251 | |
|
| | 6252 | | // VideoToolbox on Apple Silicon has H.264 Hi10P mode enabled after macOS 14.6 |
| 0 | 6253 | | if (hardwareAccelerationType == HardwareAccelerationType.videotoolbox) |
| | 6254 | | { |
| 0 | 6255 | | var ver = Environment.OSVersion.Version; |
| 0 | 6256 | | var arch = RuntimeInformation.OSArchitecture; |
| 0 | 6257 | | if (arch.Equals(Architecture.Arm64) && ver >= new Version(14, 6)) |
| | 6258 | | { |
| 0 | 6259 | | hasHardwareHi10P = true; |
| | 6260 | | } |
| | 6261 | | } |
| | 6262 | |
|
| 0 | 6263 | | if (!hasHardwareHi10P |
| 0 | 6264 | | && string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 6265 | | { |
| 0 | 6266 | | return null; |
| | 6267 | | } |
| | 6268 | | } |
| | 6269 | |
|
| 0 | 6270 | | var decoder = hardwareAccelerationType switch |
| 0 | 6271 | | { |
| 0 | 6272 | | HardwareAccelerationType.vaapi => GetVaapiVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6273 | | HardwareAccelerationType.amf => GetAmfVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6274 | | HardwareAccelerationType.qsv => GetQsvHwVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6275 | | HardwareAccelerationType.nvenc => GetNvdecVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6276 | | HardwareAccelerationType.videotoolbox => GetVideotoolboxVidDecoder(state, options, videoStream, bitD |
| 0 | 6277 | | HardwareAccelerationType.rkmpp => GetRkmppVidDecoder(state, options, videoStream, bitDepth), |
| 0 | 6278 | | _ => string.Empty |
| 0 | 6279 | | }; |
| | 6280 | |
|
| 0 | 6281 | | if (!string.IsNullOrEmpty(decoder)) |
| | 6282 | | { |
| 0 | 6283 | | return decoder; |
| | 6284 | | } |
| | 6285 | | } |
| | 6286 | |
|
| | 6287 | | // leave blank so ffmpeg will decide |
| 0 | 6288 | | return null; |
| | 6289 | | } |
| | 6290 | |
|
| | 6291 | | /// <summary> |
| | 6292 | | /// Gets a hw decoder name. |
| | 6293 | | /// </summary> |
| | 6294 | | /// <param name="options">Encoding options.</param> |
| | 6295 | | /// <param name="decoderPrefix">Decoder prefix.</param> |
| | 6296 | | /// <param name="decoderSuffix">Decoder suffix.</param> |
| | 6297 | | /// <param name="videoCodec">Video codec to use.</param> |
| | 6298 | | /// <param name="bitDepth">Video color bit depth.</param> |
| | 6299 | | /// <returns>Hardware decoder name.</returns> |
| | 6300 | | public string GetHwDecoderName(EncodingOptions options, string decoderPrefix, string decoderSuffix, string video |
| | 6301 | | { |
| 0 | 6302 | | if (string.IsNullOrEmpty(decoderPrefix) || string.IsNullOrEmpty(decoderSuffix)) |
| | 6303 | | { |
| 0 | 6304 | | return null; |
| | 6305 | | } |
| | 6306 | |
|
| 0 | 6307 | | var decoderName = decoderPrefix + '_' + decoderSuffix; |
| | 6308 | |
|
| 0 | 6309 | | var isCodecAvailable = _mediaEncoder.SupportsDecoder(decoderName) && options.HardwareDecodingCodecs.Contains |
| | 6310 | |
|
| | 6311 | | // VideoToolbox decoders have built-in SW fallback |
| 0 | 6312 | | if (bitDepth == 10 |
| 0 | 6313 | | && isCodecAvailable |
| 0 | 6314 | | && (options.HardwareAccelerationType != HardwareAccelerationType.videotoolbox)) |
| | 6315 | | { |
| 0 | 6316 | | if (string.Equals(videoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6317 | | && options.HardwareDecodingCodecs.Contains("hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6318 | | && !options.EnableDecodingColorDepth10Hevc) |
| | 6319 | | { |
| 0 | 6320 | | return null; |
| | 6321 | | } |
| | 6322 | |
|
| 0 | 6323 | | if (string.Equals(videoCodec, "vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6324 | | && options.HardwareDecodingCodecs.Contains("vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6325 | | && !options.EnableDecodingColorDepth10Vp9) |
| | 6326 | | { |
| 0 | 6327 | | return null; |
| | 6328 | | } |
| | 6329 | | } |
| | 6330 | |
|
| 0 | 6331 | | if (string.Equals(decoderSuffix, "cuvid", StringComparison.OrdinalIgnoreCase) && options.EnableEnhancedNvdec |
| | 6332 | | { |
| 0 | 6333 | | return null; |
| | 6334 | | } |
| | 6335 | |
|
| 0 | 6336 | | if (string.Equals(decoderSuffix, "qsv", StringComparison.OrdinalIgnoreCase) && options.PreferSystemNativeHwD |
| | 6337 | | { |
| 0 | 6338 | | return null; |
| | 6339 | | } |
| | 6340 | |
|
| 0 | 6341 | | if (string.Equals(decoderSuffix, "rkmpp", StringComparison.OrdinalIgnoreCase)) |
| | 6342 | | { |
| 0 | 6343 | | return null; |
| | 6344 | | } |
| | 6345 | |
|
| 0 | 6346 | | return isCodecAvailable ? (" -c:v " + decoderName) : null; |
| | 6347 | | } |
| | 6348 | |
|
| | 6349 | | /// <summary> |
| | 6350 | | /// Gets a hwaccel type to use as a hardware decoder depending on the system. |
| | 6351 | | /// </summary> |
| | 6352 | | /// <param name="state">Encoding state.</param> |
| | 6353 | | /// <param name="options">Encoding options.</param> |
| | 6354 | | /// <param name="videoCodec">Video codec to use.</param> |
| | 6355 | | /// <param name="bitDepth">Video color bit depth.</param> |
| | 6356 | | /// <param name="outputHwSurface">Specifies if output hw surface.</param> |
| | 6357 | | /// <returns>Hardware accelerator type.</returns> |
| | 6358 | | public string GetHwaccelType(EncodingJobInfo state, EncodingOptions options, string videoCodec, int bitDepth, bo |
| | 6359 | | { |
| 0 | 6360 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 6361 | | var isLinux = OperatingSystem.IsLinux(); |
| 0 | 6362 | | var isMacOS = OperatingSystem.IsMacOS(); |
| 0 | 6363 | | var isD3d11Supported = isWindows && _mediaEncoder.SupportsHwaccel("d3d11va"); |
| 0 | 6364 | | var isVaapiSupported = isLinux && IsVaapiSupported(state); |
| 0 | 6365 | | var isCudaSupported = (isLinux || isWindows) && IsCudaFullSupported(); |
| 0 | 6366 | | var isQsvSupported = (isLinux || isWindows) && _mediaEncoder.SupportsHwaccel("qsv"); |
| 0 | 6367 | | var isVideotoolboxSupported = isMacOS && _mediaEncoder.SupportsHwaccel("videotoolbox"); |
| 0 | 6368 | | var isRkmppSupported = isLinux && IsRkmppFullSupported(); |
| 0 | 6369 | | var isCodecAvailable = options.HardwareDecodingCodecs.Contains(videoCodec, StringComparison.OrdinalIgnoreCas |
| 0 | 6370 | | var hardwareAccelerationType = options.HardwareAccelerationType; |
| | 6371 | |
|
| 0 | 6372 | | var ffmpegVersion = _mediaEncoder.EncoderVersion; |
| | 6373 | |
|
| | 6374 | | // Set the av1 codec explicitly to trigger hw accelerator, otherwise libdav1d will be used. |
| 0 | 6375 | | var isAv1 = ffmpegVersion < _minFFmpegImplicitHwaccel |
| 0 | 6376 | | && string.Equals(videoCodec, "av1", StringComparison.OrdinalIgnoreCase); |
| | 6377 | |
|
| | 6378 | | // Allow profile mismatch if decoding H.264 baseline with d3d11va and vaapi hwaccels. |
| 0 | 6379 | | var profileMismatch = string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase) |
| 0 | 6380 | | && string.Equals(state.VideoStream?.Profile, "baseline", StringComparison.OrdinalIgnoreCase); |
| | 6381 | |
|
| | 6382 | | // Disable the extra internal copy in nvdec. We already handle it in filter chain. |
| 0 | 6383 | | var nvdecNoInternalCopy = ffmpegVersion >= _minFFmpegHwaUnsafeOutput; |
| | 6384 | |
|
| | 6385 | | // Strip the display rotation side data from the transposed fmp4 output stream. |
| 0 | 6386 | | var stripRotationData = (state.VideoStream?.Rotation ?? 0) != 0 |
| 0 | 6387 | | && ffmpegVersion >= _minFFmpegDisplayRotationOption; |
| 0 | 6388 | | var stripRotationDataArgs = stripRotationData ? " -display_rotation 0" : string.Empty; |
| | 6389 | |
|
| | 6390 | | // VideoToolbox decoders have built-in SW fallback |
| 0 | 6391 | | if (isCodecAvailable |
| 0 | 6392 | | && (options.HardwareAccelerationType != HardwareAccelerationType.videotoolbox)) |
| | 6393 | | { |
| 0 | 6394 | | if (string.Equals(videoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6395 | | && options.HardwareDecodingCodecs.Contains("hevc", StringComparison.OrdinalIgnoreCase)) |
| | 6396 | | { |
| 0 | 6397 | | if (IsVideoStreamHevcRext(state)) |
| | 6398 | | { |
| 0 | 6399 | | if (bitDepth <= 10 && !options.EnableDecodingColorDepth10HevcRext) |
| | 6400 | | { |
| 0 | 6401 | | return null; |
| | 6402 | | } |
| | 6403 | |
|
| 0 | 6404 | | if (bitDepth == 12 && !options.EnableDecodingColorDepth12HevcRext) |
| | 6405 | | { |
| 0 | 6406 | | return null; |
| | 6407 | | } |
| | 6408 | |
|
| 0 | 6409 | | if (hardwareAccelerationType == HardwareAccelerationType.vaapi |
| 0 | 6410 | | && !_mediaEncoder.IsVaapiDeviceInteliHD) |
| | 6411 | | { |
| 0 | 6412 | | return null; |
| | 6413 | | } |
| | 6414 | | } |
| 0 | 6415 | | else if (bitDepth == 10 && !options.EnableDecodingColorDepth10Hevc) |
| | 6416 | | { |
| 0 | 6417 | | return null; |
| | 6418 | | } |
| | 6419 | | } |
| | 6420 | |
|
| 0 | 6421 | | if (string.Equals(videoCodec, "vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6422 | | && options.HardwareDecodingCodecs.Contains("vp9", StringComparison.OrdinalIgnoreCase) |
| 0 | 6423 | | && bitDepth == 10 |
| 0 | 6424 | | && !options.EnableDecodingColorDepth10Vp9) |
| | 6425 | | { |
| 0 | 6426 | | return null; |
| | 6427 | | } |
| | 6428 | | } |
| | 6429 | |
|
| | 6430 | | // Intel qsv/d3d11va/vaapi |
| 0 | 6431 | | if (hardwareAccelerationType == HardwareAccelerationType.qsv) |
| | 6432 | | { |
| 0 | 6433 | | if (options.PreferSystemNativeHwDecoder) |
| | 6434 | | { |
| 0 | 6435 | | if (isVaapiSupported && isCodecAvailable) |
| | 6436 | | { |
| 0 | 6437 | | return " -hwaccel vaapi" + (outputHwSurface ? " -hwaccel_output_format vaapi -noautorotate" + st |
| 0 | 6438 | | + (profileMismatch ? " -hwaccel_flags +allow_profile_mismatch" : string.Empty) + (isAv1 ? " |
| | 6439 | | } |
| | 6440 | |
|
| 0 | 6441 | | if (isD3d11Supported && isCodecAvailable) |
| | 6442 | | { |
| 0 | 6443 | | return " -hwaccel d3d11va" + (outputHwSurface ? " -hwaccel_output_format d3d11 -noautorotate" + |
| 0 | 6444 | | + (profileMismatch ? " -hwaccel_flags +allow_profile_mismatch" : string.Empty) + " -threads |
| | 6445 | | } |
| | 6446 | | } |
| | 6447 | | else |
| | 6448 | | { |
| 0 | 6449 | | if (isQsvSupported && isCodecAvailable) |
| | 6450 | | { |
| 0 | 6451 | | return " -hwaccel qsv" + (outputHwSurface ? " -hwaccel_output_format qsv -noautorotate" + stripR |
| | 6452 | | } |
| | 6453 | | } |
| | 6454 | | } |
| | 6455 | |
|
| | 6456 | | // Nvidia cuda |
| 0 | 6457 | | if (hardwareAccelerationType == HardwareAccelerationType.nvenc) |
| | 6458 | | { |
| 0 | 6459 | | if (isCudaSupported && isCodecAvailable) |
| | 6460 | | { |
| 0 | 6461 | | if (options.EnableEnhancedNvdecDecoder) |
| | 6462 | | { |
| | 6463 | | // set -threads 1 to nvdec decoder explicitly since it doesn't implement threading support. |
| 0 | 6464 | | return " -hwaccel cuda" + (outputHwSurface ? " -hwaccel_output_format cuda -noautorotate" + stri |
| 0 | 6465 | | + (nvdecNoInternalCopy ? " -hwaccel_flags +unsafe_output" : string.Empty) + " -threads 1" + |
| | 6466 | | } |
| | 6467 | |
|
| | 6468 | | // cuvid decoder doesn't have threading issue. |
| 0 | 6469 | | return " -hwaccel cuda" + (outputHwSurface ? " -hwaccel_output_format cuda -noautorotate" + stripRot |
| | 6470 | | } |
| | 6471 | | } |
| | 6472 | |
|
| | 6473 | | // Amd d3d11va |
| 0 | 6474 | | if (hardwareAccelerationType == HardwareAccelerationType.amf) |
| | 6475 | | { |
| 0 | 6476 | | if (isD3d11Supported && isCodecAvailable) |
| | 6477 | | { |
| 0 | 6478 | | return " -hwaccel d3d11va" + (outputHwSurface ? " -hwaccel_output_format d3d11 -noautorotate" + stri |
| 0 | 6479 | | + (profileMismatch ? " -hwaccel_flags +allow_profile_mismatch" : string.Empty) + (isAv1 ? " -c:v |
| | 6480 | | } |
| | 6481 | | } |
| | 6482 | |
|
| | 6483 | | // Vaapi |
| 0 | 6484 | | if (hardwareAccelerationType == HardwareAccelerationType.vaapi |
| 0 | 6485 | | && isVaapiSupported |
| 0 | 6486 | | && isCodecAvailable) |
| | 6487 | | { |
| 0 | 6488 | | return " -hwaccel vaapi" + (outputHwSurface ? " -hwaccel_output_format vaapi -noautorotate" + stripRotat |
| 0 | 6489 | | + (profileMismatch ? " -hwaccel_flags +allow_profile_mismatch" : string.Empty) + (isAv1 ? " -c:v av1 |
| | 6490 | | } |
| | 6491 | |
|
| | 6492 | | // Apple videotoolbox |
| 0 | 6493 | | if (hardwareAccelerationType == HardwareAccelerationType.videotoolbox |
| 0 | 6494 | | && isVideotoolboxSupported |
| 0 | 6495 | | && isCodecAvailable) |
| | 6496 | | { |
| 0 | 6497 | | return " -hwaccel videotoolbox" + (outputHwSurface ? " -hwaccel_output_format videotoolbox_vld" : string |
| | 6498 | | } |
| | 6499 | |
|
| | 6500 | | // Rockchip rkmpp |
| 0 | 6501 | | if (hardwareAccelerationType == HardwareAccelerationType.rkmpp |
| 0 | 6502 | | && isRkmppSupported |
| 0 | 6503 | | && isCodecAvailable) |
| | 6504 | | { |
| 0 | 6505 | | return " -hwaccel rkmpp" + (outputHwSurface ? " -hwaccel_output_format drm_prime -noautorotate" + stripR |
| | 6506 | | } |
| | 6507 | |
|
| 0 | 6508 | | return null; |
| | 6509 | | } |
| | 6510 | |
|
| | 6511 | | public string GetQsvHwVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bi |
| | 6512 | | { |
| 0 | 6513 | | var isWindows = OperatingSystem.IsWindows(); |
| 0 | 6514 | | var isLinux = OperatingSystem.IsLinux(); |
| | 6515 | |
|
| 0 | 6516 | | if ((!isWindows && !isLinux) |
| 0 | 6517 | | || options.HardwareAccelerationType != HardwareAccelerationType.qsv) |
| | 6518 | | { |
| 0 | 6519 | | return null; |
| | 6520 | | } |
| | 6521 | |
|
| 0 | 6522 | | var isQsvOclSupported = _mediaEncoder.SupportsHwaccel("qsv") && IsOpenclFullSupported(); |
| 0 | 6523 | | var isIntelDx11OclSupported = isWindows |
| 0 | 6524 | | && _mediaEncoder.SupportsHwaccel("d3d11va") |
| 0 | 6525 | | && isQsvOclSupported; |
| 0 | 6526 | | var isIntelVaapiOclSupported = isLinux |
| 0 | 6527 | | && IsVaapiSupported(state) |
| 0 | 6528 | | && isQsvOclSupported; |
| 0 | 6529 | | var hwSurface = (isIntelDx11OclSupported || isIntelVaapiOclSupported) |
| 0 | 6530 | | && _mediaEncoder.SupportsFilter("alphasrc"); |
| | 6531 | |
|
| 0 | 6532 | | var is8bitSwFormatsQsv = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCas |
| 0 | 6533 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgnor |
| 0 | 6534 | | var is8_10bitSwFormatsQsv = is8bitSwFormatsQsv || string.Equals("yuv420p10le", videoStream.PixelFormat, Stri |
| 0 | 6535 | | var is8_10_12bitSwFormatsQsv = is8_10bitSwFormatsQsv |
| 0 | 6536 | | || string.Equals("yuv422p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6537 | | || string.Equals("yuv444p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6538 | | || string.Equals("yuv422p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6539 | | || string.Equals("yuv444p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6540 | | || string.Equals("yuv420p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6541 | | || string.Equals("yuv422p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6542 | | || string.Equals("yuv444p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase); |
| | 6543 | | // TODO: add more 8/10bit and 4:4:4 formats for Qsv after finishing the ffcheck tool |
| | 6544 | |
|
| 0 | 6545 | | if (is8bitSwFormatsQsv) |
| | 6546 | | { |
| 0 | 6547 | | if (string.Equals(videoStream.Codec, "avc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6548 | | || string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 6549 | | { |
| 0 | 6550 | | return GetHwaccelType(state, options, "h264", bitDepth, hwSurface) + GetHwDecoderName(options, "h264 |
| | 6551 | | } |
| | 6552 | |
|
| 0 | 6553 | | if (string.Equals(videoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase)) |
| | 6554 | | { |
| 0 | 6555 | | return GetHwaccelType(state, options, "vc1", bitDepth, hwSurface) + GetHwDecoderName(options, "vc1", |
| | 6556 | | } |
| | 6557 | |
|
| 0 | 6558 | | if (string.Equals(videoStream.Codec, "vp8", StringComparison.OrdinalIgnoreCase)) |
| | 6559 | | { |
| 0 | 6560 | | return GetHwaccelType(state, options, "vp8", bitDepth, hwSurface) + GetHwDecoderName(options, "vp8", |
| | 6561 | | } |
| | 6562 | |
|
| 0 | 6563 | | if (string.Equals(videoStream.Codec, "mpeg2video", StringComparison.OrdinalIgnoreCase)) |
| | 6564 | | { |
| 0 | 6565 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface) + GetHwDecoderName(options, |
| | 6566 | | } |
| | 6567 | | } |
| | 6568 | |
|
| 0 | 6569 | | if (is8_10bitSwFormatsQsv) |
| | 6570 | | { |
| 0 | 6571 | | if (string.Equals(videoStream.Codec, "vp9", StringComparison.OrdinalIgnoreCase)) |
| | 6572 | | { |
| 0 | 6573 | | return GetHwaccelType(state, options, "vp9", bitDepth, hwSurface) + GetHwDecoderName(options, "vp9", |
| | 6574 | | } |
| | 6575 | |
|
| 0 | 6576 | | if (string.Equals(videoStream.Codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 6577 | | { |
| 0 | 6578 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface) + GetHwDecoderName(options, "av1", |
| | 6579 | | } |
| | 6580 | | } |
| | 6581 | |
|
| 0 | 6582 | | if (is8_10_12bitSwFormatsQsv) |
| | 6583 | | { |
| 0 | 6584 | | if (string.Equals(videoStream.Codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6585 | | || string.Equals(videoStream.Codec, "h265", StringComparison.OrdinalIgnoreCase)) |
| | 6586 | | { |
| 0 | 6587 | | return GetHwaccelType(state, options, "hevc", bitDepth, hwSurface) + GetHwDecoderName(options, "hevc |
| | 6588 | | } |
| | 6589 | | } |
| | 6590 | |
|
| 0 | 6591 | | return null; |
| | 6592 | | } |
| | 6593 | |
|
| | 6594 | | public string GetNvdecVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bi |
| | 6595 | | { |
| 0 | 6596 | | if ((!OperatingSystem.IsWindows() && !OperatingSystem.IsLinux()) |
| 0 | 6597 | | || options.HardwareAccelerationType != HardwareAccelerationType.nvenc) |
| | 6598 | | { |
| 0 | 6599 | | return null; |
| | 6600 | | } |
| | 6601 | |
|
| 0 | 6602 | | var hwSurface = IsCudaFullSupported() && _mediaEncoder.SupportsFilter("alphasrc"); |
| 0 | 6603 | | var is8bitSwFormatsNvdec = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreC |
| 0 | 6604 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgn |
| 0 | 6605 | | var is8_10bitSwFormatsNvdec = is8bitSwFormatsNvdec || string.Equals("yuv420p10le", videoStream.PixelFormat, |
| 0 | 6606 | | var is8_10_12bitSwFormatsNvdec = is8_10bitSwFormatsNvdec |
| 0 | 6607 | | || string.Equals("yuv444p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6608 | | || string.Equals("yuv444p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6609 | | || string.Equals("yuv420p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6610 | | || string.Equals("yuv444p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase); |
| | 6611 | | // TODO: add more 8/10/12bit and 4:4:4 formats for Nvdec after finishing the ffcheck tool |
| | 6612 | |
|
| 0 | 6613 | | if (is8bitSwFormatsNvdec) |
| | 6614 | | { |
| 0 | 6615 | | if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6616 | | || string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6617 | | { |
| 0 | 6618 | | return GetHwaccelType(state, options, "h264", bitDepth, hwSurface) + GetHwDecoderName(options, "h264 |
| | 6619 | | } |
| | 6620 | |
|
| 0 | 6621 | | if (string.Equals("mpeg2video", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6622 | | { |
| 0 | 6623 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface) + GetHwDecoderName(options, |
| | 6624 | | } |
| | 6625 | |
|
| 0 | 6626 | | if (string.Equals("vc1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6627 | | { |
| 0 | 6628 | | return GetHwaccelType(state, options, "vc1", bitDepth, hwSurface) + GetHwDecoderName(options, "vc1", |
| | 6629 | | } |
| | 6630 | |
|
| 0 | 6631 | | if (string.Equals("mpeg4", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6632 | | { |
| 0 | 6633 | | return GetHwaccelType(state, options, "mpeg4", bitDepth, hwSurface) + GetHwDecoderName(options, "mpe |
| | 6634 | | } |
| | 6635 | |
|
| 0 | 6636 | | if (string.Equals("vp8", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6637 | | { |
| 0 | 6638 | | return GetHwaccelType(state, options, "vp8", bitDepth, hwSurface) + GetHwDecoderName(options, "vp8", |
| | 6639 | | } |
| | 6640 | | } |
| | 6641 | |
|
| 0 | 6642 | | if (is8_10bitSwFormatsNvdec) |
| | 6643 | | { |
| 0 | 6644 | | if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6645 | | { |
| 0 | 6646 | | return GetHwaccelType(state, options, "vp9", bitDepth, hwSurface) + GetHwDecoderName(options, "vp9", |
| | 6647 | | } |
| | 6648 | |
|
| 0 | 6649 | | if (string.Equals("av1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6650 | | { |
| 0 | 6651 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface) + GetHwDecoderName(options, "av1", |
| | 6652 | | } |
| | 6653 | | } |
| | 6654 | |
|
| 0 | 6655 | | if (is8_10_12bitSwFormatsNvdec) |
| | 6656 | | { |
| 0 | 6657 | | if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6658 | | || string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6659 | | { |
| 0 | 6660 | | return GetHwaccelType(state, options, "hevc", bitDepth, hwSurface) + GetHwDecoderName(options, "hevc |
| | 6661 | | } |
| | 6662 | | } |
| | 6663 | |
|
| 0 | 6664 | | return null; |
| | 6665 | | } |
| | 6666 | |
|
| | 6667 | | public string GetAmfVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bitD |
| | 6668 | | { |
| 0 | 6669 | | if (!OperatingSystem.IsWindows() |
| 0 | 6670 | | || options.HardwareAccelerationType != HardwareAccelerationType.amf) |
| | 6671 | | { |
| 0 | 6672 | | return null; |
| | 6673 | | } |
| | 6674 | |
|
| 0 | 6675 | | var hwSurface = _mediaEncoder.SupportsHwaccel("d3d11va") |
| 0 | 6676 | | && IsOpenclFullSupported() |
| 0 | 6677 | | && _mediaEncoder.SupportsFilter("alphasrc"); |
| 0 | 6678 | | var is8bitSwFormatsAmf = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCas |
| 0 | 6679 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgnor |
| 0 | 6680 | | var is8_10bitSwFormatsAmf = is8bitSwFormatsAmf || string.Equals("yuv420p10le", videoStream.PixelFormat, Stri |
| | 6681 | |
|
| 0 | 6682 | | if (is8bitSwFormatsAmf) |
| | 6683 | | { |
| 0 | 6684 | | if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6685 | | || string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6686 | | { |
| 0 | 6687 | | return GetHwaccelType(state, options, "h264", bitDepth, hwSurface); |
| | 6688 | | } |
| | 6689 | |
|
| 0 | 6690 | | if (string.Equals("mpeg2video", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6691 | | { |
| 0 | 6692 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface); |
| | 6693 | | } |
| | 6694 | |
|
| 0 | 6695 | | if (string.Equals("vc1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6696 | | { |
| 0 | 6697 | | return GetHwaccelType(state, options, "vc1", bitDepth, hwSurface); |
| | 6698 | | } |
| | 6699 | | } |
| | 6700 | |
|
| 0 | 6701 | | if (is8_10bitSwFormatsAmf) |
| | 6702 | | { |
| 0 | 6703 | | if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6704 | | || string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6705 | | { |
| 0 | 6706 | | return GetHwaccelType(state, options, "hevc", bitDepth, hwSurface); |
| | 6707 | | } |
| | 6708 | |
|
| 0 | 6709 | | if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6710 | | { |
| 0 | 6711 | | return GetHwaccelType(state, options, "vp9", bitDepth, hwSurface); |
| | 6712 | | } |
| | 6713 | |
|
| 0 | 6714 | | if (string.Equals("av1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6715 | | { |
| 0 | 6716 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface); |
| | 6717 | | } |
| | 6718 | | } |
| | 6719 | |
|
| 0 | 6720 | | return null; |
| | 6721 | | } |
| | 6722 | |
|
| | 6723 | | public string GetVaapiVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bi |
| | 6724 | | { |
| 0 | 6725 | | if (!OperatingSystem.IsLinux() |
| 0 | 6726 | | || options.HardwareAccelerationType != HardwareAccelerationType.vaapi) |
| | 6727 | | { |
| 0 | 6728 | | return null; |
| | 6729 | | } |
| | 6730 | |
|
| 0 | 6731 | | var hwSurface = IsVaapiSupported(state) |
| 0 | 6732 | | && IsVaapiFullSupported() |
| 0 | 6733 | | && IsOpenclFullSupported() |
| 0 | 6734 | | && _mediaEncoder.SupportsFilter("alphasrc"); |
| 0 | 6735 | | var is8bitSwFormatsVaapi = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreC |
| 0 | 6736 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgn |
| 0 | 6737 | | var is8_10bitSwFormatsVaapi = is8bitSwFormatsVaapi || string.Equals("yuv420p10le", videoStream.PixelFormat, |
| 0 | 6738 | | var is8_10_12bitSwFormatsVaapi = is8_10bitSwFormatsVaapi |
| 0 | 6739 | | || string.Equals("yuv422p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6740 | | || string.Equals("yuv444p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6741 | | || string.Equals("yuv422p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6742 | | || string.Equals("yuv444p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6743 | | || string.Equals("yuv420p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6744 | | || string.Equals("yuv422p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6745 | | || string.Equals("yuv444p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase); |
| | 6746 | |
|
| 0 | 6747 | | if (is8bitSwFormatsVaapi) |
| | 6748 | | { |
| 0 | 6749 | | if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6750 | | || string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6751 | | { |
| 0 | 6752 | | return GetHwaccelType(state, options, "h264", bitDepth, hwSurface); |
| | 6753 | | } |
| | 6754 | |
|
| 0 | 6755 | | if (string.Equals("mpeg2video", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6756 | | { |
| 0 | 6757 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface); |
| | 6758 | | } |
| | 6759 | |
|
| 0 | 6760 | | if (string.Equals("vc1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6761 | | { |
| 0 | 6762 | | return GetHwaccelType(state, options, "vc1", bitDepth, hwSurface); |
| | 6763 | | } |
| | 6764 | |
|
| 0 | 6765 | | if (string.Equals("vp8", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6766 | | { |
| 0 | 6767 | | return GetHwaccelType(state, options, "vp8", bitDepth, hwSurface); |
| | 6768 | | } |
| | 6769 | | } |
| | 6770 | |
|
| 0 | 6771 | | if (is8_10bitSwFormatsVaapi) |
| | 6772 | | { |
| 0 | 6773 | | if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6774 | | { |
| 0 | 6775 | | return GetHwaccelType(state, options, "vp9", bitDepth, hwSurface); |
| | 6776 | | } |
| | 6777 | |
|
| 0 | 6778 | | if (string.Equals("av1", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6779 | | { |
| 0 | 6780 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface); |
| | 6781 | | } |
| | 6782 | | } |
| | 6783 | |
|
| 0 | 6784 | | if (is8_10_12bitSwFormatsVaapi) |
| | 6785 | | { |
| 0 | 6786 | | if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6787 | | || string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6788 | | { |
| 0 | 6789 | | return GetHwaccelType(state, options, "hevc", bitDepth, hwSurface); |
| | 6790 | | } |
| | 6791 | | } |
| | 6792 | |
|
| 0 | 6793 | | return null; |
| | 6794 | | } |
| | 6795 | |
|
| | 6796 | | public string GetVideotoolboxVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, |
| | 6797 | | { |
| 0 | 6798 | | if (!OperatingSystem.IsMacOS() |
| 0 | 6799 | | || options.HardwareAccelerationType != HardwareAccelerationType.videotoolbox) |
| | 6800 | | { |
| 0 | 6801 | | return null; |
| | 6802 | | } |
| | 6803 | |
|
| 0 | 6804 | | var is8bitSwFormatsVt = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase |
| 0 | 6805 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgnore |
| 0 | 6806 | | var is8_10bitSwFormatsVt = is8bitSwFormatsVt || string.Equals("yuv420p10le", videoStream.PixelFormat, String |
| 0 | 6807 | | var is8_10_12bitSwFormatsVt = is8_10bitSwFormatsVt |
| 0 | 6808 | | || string.Equals("yuv422p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6809 | | || string.Equals("yuv444p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6810 | | || string.Equals("yuv422p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6811 | | || string.Equals("yuv444p10le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6812 | | || string.Equals("yuv420p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6813 | | || string.Equals("yuv422p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase) |
| 0 | 6814 | | || string.Equals("yuv444p12le", videoStream.PixelFormat, StringComparison.OrdinalIgnoreCase); |
| 0 | 6815 | | var isAv1SupportedSwFormatsVt = is8_10bitSwFormatsVt || string.Equals("yuv420p12le", videoStream.PixelFormat |
| | 6816 | |
|
| | 6817 | | // The related patches make videotoolbox hardware surface working is only available in jellyfin-ffmpeg 7.0.1 |
| 0 | 6818 | | bool useHwSurface = (_mediaEncoder.EncoderVersion >= _minFFmpegWorkingVtHwSurface) && IsVideoToolboxFullSupp |
| | 6819 | |
|
| 0 | 6820 | | if (is8bitSwFormatsVt) |
| | 6821 | | { |
| 0 | 6822 | | if (string.Equals("vp8", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6823 | | { |
| 0 | 6824 | | return GetHwaccelType(state, options, "vp8", bitDepth, useHwSurface); |
| | 6825 | | } |
| | 6826 | | } |
| | 6827 | |
|
| 0 | 6828 | | if (is8_10bitSwFormatsVt) |
| | 6829 | | { |
| 0 | 6830 | | if (string.Equals("avc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6831 | | || string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6832 | | { |
| 0 | 6833 | | return GetHwaccelType(state, options, "h264", bitDepth, useHwSurface); |
| | 6834 | | } |
| | 6835 | |
|
| 0 | 6836 | | if (string.Equals("vp9", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6837 | | { |
| 0 | 6838 | | return GetHwaccelType(state, options, "vp9", bitDepth, useHwSurface); |
| | 6839 | | } |
| | 6840 | | } |
| | 6841 | |
|
| 0 | 6842 | | if (is8_10_12bitSwFormatsVt) |
| | 6843 | | { |
| 0 | 6844 | | if (string.Equals("hevc", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6845 | | || string.Equals("h265", videoStream.Codec, StringComparison.OrdinalIgnoreCase)) |
| | 6846 | | { |
| 0 | 6847 | | return GetHwaccelType(state, options, "hevc", bitDepth, useHwSurface); |
| | 6848 | | } |
| | 6849 | |
|
| 0 | 6850 | | if (string.Equals("av1", videoStream.Codec, StringComparison.OrdinalIgnoreCase) |
| 0 | 6851 | | && isAv1SupportedSwFormatsVt |
| 0 | 6852 | | && _mediaEncoder.IsVideoToolboxAv1DecodeAvailable) |
| | 6853 | | { |
| 0 | 6854 | | return GetHwaccelType(state, options, "av1", bitDepth, useHwSurface); |
| | 6855 | | } |
| | 6856 | | } |
| | 6857 | |
|
| 0 | 6858 | | return null; |
| | 6859 | | } |
| | 6860 | |
|
| | 6861 | | public string GetRkmppVidDecoder(EncodingJobInfo state, EncodingOptions options, MediaStream videoStream, int bi |
| | 6862 | | { |
| 0 | 6863 | | var isLinux = OperatingSystem.IsLinux(); |
| | 6864 | |
|
| 0 | 6865 | | if (!isLinux |
| 0 | 6866 | | || options.HardwareAccelerationType != HardwareAccelerationType.rkmpp) |
| | 6867 | | { |
| 0 | 6868 | | return null; |
| | 6869 | | } |
| | 6870 | |
|
| 0 | 6871 | | var inW = state.VideoStream?.Width; |
| 0 | 6872 | | var inH = state.VideoStream?.Height; |
| 0 | 6873 | | var reqW = state.BaseRequest.Width; |
| 0 | 6874 | | var reqH = state.BaseRequest.Height; |
| 0 | 6875 | | var reqMaxW = state.BaseRequest.MaxWidth; |
| 0 | 6876 | | var reqMaxH = state.BaseRequest.MaxHeight; |
| | 6877 | |
|
| | 6878 | | // rkrga RGA2e supports range from 1/16 to 16 |
| 0 | 6879 | | if (!IsScaleRatioSupported(inW, inH, reqW, reqH, reqMaxW, reqMaxH, 16.0f)) |
| | 6880 | | { |
| 0 | 6881 | | return null; |
| | 6882 | | } |
| | 6883 | |
|
| 0 | 6884 | | var isRkmppOclSupported = IsRkmppFullSupported() && IsOpenclFullSupported(); |
| 0 | 6885 | | var hwSurface = isRkmppOclSupported |
| 0 | 6886 | | && _mediaEncoder.SupportsFilter("alphasrc"); |
| | 6887 | |
|
| | 6888 | | // rkrga RGA3 supports range from 1/8 to 8 |
| 0 | 6889 | | var isAfbcSupported = hwSurface && IsScaleRatioSupported(inW, inH, reqW, reqH, reqMaxW, reqMaxH, 8.0f); |
| | 6890 | |
|
| | 6891 | | // TODO: add more 8/10bit and 4:2:2 formats for Rkmpp after finishing the ffcheck tool |
| 0 | 6892 | | var is8bitSwFormatsRkmpp = string.Equals("yuv420p", videoStream.PixelFormat, StringComparison.OrdinalIgnoreC |
| 0 | 6893 | | || string.Equals("yuvj420p", videoStream.PixelFormat, StringComparison.OrdinalIgn |
| 0 | 6894 | | var is10bitSwFormatsRkmpp = string.Equals("yuv420p10le", videoStream.PixelFormat, StringComparison.OrdinalIg |
| 0 | 6895 | | var is8_10bitSwFormatsRkmpp = is8bitSwFormatsRkmpp || is10bitSwFormatsRkmpp; |
| | 6896 | |
|
| | 6897 | | // nv15 and nv20 are bit-stream only formats |
| 0 | 6898 | | if (is10bitSwFormatsRkmpp && !hwSurface) |
| | 6899 | | { |
| 0 | 6900 | | return null; |
| | 6901 | | } |
| | 6902 | |
|
| 0 | 6903 | | if (is8bitSwFormatsRkmpp) |
| | 6904 | | { |
| 0 | 6905 | | if (string.Equals(videoStream.Codec, "mpeg1video", StringComparison.OrdinalIgnoreCase)) |
| | 6906 | | { |
| 0 | 6907 | | return GetHwaccelType(state, options, "mpeg1video", bitDepth, hwSurface); |
| | 6908 | | } |
| | 6909 | |
|
| 0 | 6910 | | if (string.Equals(videoStream.Codec, "mpeg2video", StringComparison.OrdinalIgnoreCase)) |
| | 6911 | | { |
| 0 | 6912 | | return GetHwaccelType(state, options, "mpeg2video", bitDepth, hwSurface); |
| | 6913 | | } |
| | 6914 | |
|
| 0 | 6915 | | if (string.Equals(videoStream.Codec, "mpeg4", StringComparison.OrdinalIgnoreCase)) |
| | 6916 | | { |
| 0 | 6917 | | return GetHwaccelType(state, options, "mpeg4", bitDepth, hwSurface); |
| | 6918 | | } |
| | 6919 | |
|
| 0 | 6920 | | if (string.Equals(videoStream.Codec, "vp8", StringComparison.OrdinalIgnoreCase)) |
| | 6921 | | { |
| 0 | 6922 | | return GetHwaccelType(state, options, "vp8", bitDepth, hwSurface); |
| | 6923 | | } |
| | 6924 | | } |
| | 6925 | |
|
| 0 | 6926 | | if (is8_10bitSwFormatsRkmpp) |
| | 6927 | | { |
| 0 | 6928 | | if (string.Equals(videoStream.Codec, "avc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6929 | | || string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | 6930 | | { |
| 0 | 6931 | | var accelType = GetHwaccelType(state, options, "h264", bitDepth, hwSurface); |
| 0 | 6932 | | return accelType + ((!string.IsNullOrEmpty(accelType) && isAfbcSupported) ? " -afbc rga" : string.Em |
| | 6933 | | } |
| | 6934 | |
|
| 0 | 6935 | | if (string.Equals(videoStream.Codec, "hevc", StringComparison.OrdinalIgnoreCase) |
| 0 | 6936 | | || string.Equals(videoStream.Codec, "h265", StringComparison.OrdinalIgnoreCase)) |
| | 6937 | | { |
| 0 | 6938 | | var accelType = GetHwaccelType(state, options, "hevc", bitDepth, hwSurface); |
| 0 | 6939 | | return accelType + ((!string.IsNullOrEmpty(accelType) && isAfbcSupported) ? " -afbc rga" : string.Em |
| | 6940 | | } |
| | 6941 | |
|
| 0 | 6942 | | if (string.Equals(videoStream.Codec, "vp9", StringComparison.OrdinalIgnoreCase)) |
| | 6943 | | { |
| 0 | 6944 | | var accelType = GetHwaccelType(state, options, "vp9", bitDepth, hwSurface); |
| 0 | 6945 | | return accelType + ((!string.IsNullOrEmpty(accelType) && isAfbcSupported) ? " -afbc rga" : string.Em |
| | 6946 | | } |
| | 6947 | |
|
| 0 | 6948 | | if (string.Equals(videoStream.Codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | 6949 | | { |
| 0 | 6950 | | return GetHwaccelType(state, options, "av1", bitDepth, hwSurface); |
| | 6951 | | } |
| | 6952 | | } |
| | 6953 | |
|
| 0 | 6954 | | return null; |
| | 6955 | | } |
| | 6956 | |
|
| | 6957 | | /// <summary> |
| | 6958 | | /// Gets the number of threads. |
| | 6959 | | /// </summary> |
| | 6960 | | /// <param name="state">Encoding state.</param> |
| | 6961 | | /// <param name="encodingOptions">Encoding options.</param> |
| | 6962 | | /// <param name="outputVideoCodec">Video codec to use.</param> |
| | 6963 | | /// <returns>Number of threads.</returns> |
| | 6964 | | #nullable enable |
| | 6965 | | public static int GetNumberOfThreads(EncodingJobInfo? state, EncodingOptions encodingOptions, string? outputVide |
| | 6966 | | { |
| 0 | 6967 | | var threads = state?.BaseRequest.CpuCoreLimit ?? encodingOptions.EncodingThreadCount; |
| | 6968 | |
|
| 0 | 6969 | | if (threads <= 0) |
| | 6970 | | { |
| | 6971 | | // Automatically set thread count |
| 0 | 6972 | | return 0; |
| | 6973 | | } |
| | 6974 | |
|
| 0 | 6975 | | return Math.Min(threads, Environment.ProcessorCount); |
| | 6976 | | } |
| | 6977 | |
|
| | 6978 | | #nullable disable |
| | 6979 | | public void TryStreamCopy(EncodingJobInfo state) |
| | 6980 | | { |
| 0 | 6981 | | if (state.VideoStream is not null && CanStreamCopyVideo(state, state.VideoStream)) |
| | 6982 | | { |
| 0 | 6983 | | state.OutputVideoCodec = "copy"; |
| | 6984 | | } |
| | 6985 | | else |
| | 6986 | | { |
| 0 | 6987 | | var user = state.User; |
| | 6988 | |
|
| | 6989 | | // If the user doesn't have access to transcoding, then force stream copy, regardless of whether it will |
| 0 | 6990 | | if (user is not null && !user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding)) |
| | 6991 | | { |
| 0 | 6992 | | state.OutputVideoCodec = "copy"; |
| | 6993 | | } |
| | 6994 | | } |
| | 6995 | |
|
| 0 | 6996 | | if (state.AudioStream is not null |
| 0 | 6997 | | && CanStreamCopyAudio(state, state.AudioStream, state.SupportedAudioCodecs)) |
| | 6998 | | { |
| 0 | 6999 | | state.OutputAudioCodec = "copy"; |
| | 7000 | | } |
| | 7001 | | else |
| | 7002 | | { |
| 0 | 7003 | | var user = state.User; |
| | 7004 | |
|
| | 7005 | | // If the user doesn't have access to transcoding, then force stream copy, regardless of whether it will |
| 0 | 7006 | | if (user is not null && !user.HasPermission(PermissionKind.EnableAudioPlaybackTranscoding)) |
| | 7007 | | { |
| 0 | 7008 | | state.OutputAudioCodec = "copy"; |
| | 7009 | | } |
| | 7010 | | } |
| 0 | 7011 | | } |
| | 7012 | |
|
| | 7013 | | public string GetInputModifier(EncodingJobInfo state, EncodingOptions encodingOptions, string segmentContainer) |
| | 7014 | | { |
| 0 | 7015 | | var inputModifier = string.Empty; |
| 0 | 7016 | | var analyzeDurationArgument = string.Empty; |
| | 7017 | |
|
| | 7018 | | // Apply -analyzeduration as per the environment variable, |
| | 7019 | | // otherwise ffmpeg will break on certain files due to default value is 0. |
| 0 | 7020 | | var ffmpegAnalyzeDuration = _config.GetFFmpegAnalyzeDuration() ?? string.Empty; |
| | 7021 | |
|
| 0 | 7022 | | if (state.MediaSource.AnalyzeDurationMs > 0) |
| | 7023 | | { |
| 0 | 7024 | | analyzeDurationArgument = "-analyzeduration " + (state.MediaSource.AnalyzeDurationMs.Value * 1000).ToStr |
| | 7025 | | } |
| 0 | 7026 | | else if (!string.IsNullOrEmpty(ffmpegAnalyzeDuration)) |
| | 7027 | | { |
| 0 | 7028 | | analyzeDurationArgument = "-analyzeduration " + ffmpegAnalyzeDuration; |
| | 7029 | | } |
| | 7030 | |
|
| 0 | 7031 | | if (!string.IsNullOrEmpty(analyzeDurationArgument)) |
| | 7032 | | { |
| 0 | 7033 | | inputModifier += " " + analyzeDurationArgument; |
| | 7034 | | } |
| | 7035 | |
|
| 0 | 7036 | | inputModifier = inputModifier.Trim(); |
| | 7037 | |
|
| | 7038 | | // Apply -probesize if configured |
| 0 | 7039 | | var ffmpegProbeSize = _config.GetFFmpegProbeSize(); |
| | 7040 | |
|
| 0 | 7041 | | if (!string.IsNullOrEmpty(ffmpegProbeSize)) |
| | 7042 | | { |
| 0 | 7043 | | inputModifier += $" -probesize {ffmpegProbeSize}"; |
| | 7044 | | } |
| | 7045 | |
|
| 0 | 7046 | | var userAgentParam = GetUserAgentParam(state); |
| | 7047 | |
|
| 0 | 7048 | | if (!string.IsNullOrEmpty(userAgentParam)) |
| | 7049 | | { |
| 0 | 7050 | | inputModifier += " " + userAgentParam; |
| | 7051 | | } |
| | 7052 | |
|
| 0 | 7053 | | inputModifier = inputModifier.Trim(); |
| | 7054 | |
|
| 0 | 7055 | | var refererParam = GetRefererParam(state); |
| | 7056 | |
|
| 0 | 7057 | | if (!string.IsNullOrEmpty(refererParam)) |
| | 7058 | | { |
| 0 | 7059 | | inputModifier += " " + refererParam; |
| | 7060 | | } |
| | 7061 | |
|
| 0 | 7062 | | inputModifier = inputModifier.Trim(); |
| | 7063 | |
|
| 0 | 7064 | | inputModifier += " " + GetFastSeekCommandLineParameter(state, encodingOptions, segmentContainer); |
| 0 | 7065 | | inputModifier = inputModifier.Trim(); |
| | 7066 | |
|
| 0 | 7067 | | if (state.InputProtocol == MediaProtocol.Rtsp) |
| | 7068 | | { |
| 0 | 7069 | | inputModifier += " -rtsp_transport tcp+udp -rtsp_flags prefer_tcp"; |
| | 7070 | | } |
| | 7071 | |
|
| 0 | 7072 | | if (!string.IsNullOrEmpty(state.InputAudioSync)) |
| | 7073 | | { |
| 0 | 7074 | | inputModifier += " -async " + state.InputAudioSync; |
| | 7075 | | } |
| | 7076 | |
|
| 0 | 7077 | | if (!string.IsNullOrEmpty(state.InputVideoSync)) |
| | 7078 | | { |
| 0 | 7079 | | inputModifier += GetVideoSyncOption(state.InputVideoSync, _mediaEncoder.EncoderVersion); |
| | 7080 | | } |
| | 7081 | |
|
| 0 | 7082 | | if (state.ReadInputAtNativeFramerate && state.InputProtocol != MediaProtocol.Rtsp) |
| | 7083 | | { |
| 0 | 7084 | | inputModifier += " -re"; |
| | 7085 | | } |
| 0 | 7086 | | else if (encodingOptions.EnableSegmentDeletion |
| 0 | 7087 | | && state.VideoStream is not null |
| 0 | 7088 | | && state.TranscodingType == TranscodingJobType.Hls |
| 0 | 7089 | | && IsCopyCodec(state.OutputVideoCodec) |
| 0 | 7090 | | && _mediaEncoder.EncoderVersion >= _minFFmpegReadrateOption) |
| | 7091 | | { |
| | 7092 | | // Set an input read rate limit 10x for using SegmentDeletion with stream-copy |
| | 7093 | | // to prevent ffmpeg from exiting prematurely (due to fast drive) |
| 0 | 7094 | | inputModifier += " -readrate 10"; |
| | 7095 | | } |
| | 7096 | |
|
| 0 | 7097 | | var flags = new List<string>(); |
| 0 | 7098 | | if (state.IgnoreInputDts) |
| | 7099 | | { |
| 0 | 7100 | | flags.Add("+igndts"); |
| | 7101 | | } |
| | 7102 | |
|
| 0 | 7103 | | if (state.IgnoreInputIndex) |
| | 7104 | | { |
| 0 | 7105 | | flags.Add("+ignidx"); |
| | 7106 | | } |
| | 7107 | |
|
| 0 | 7108 | | if (state.GenPtsInput || IsCopyCodec(state.OutputVideoCodec)) |
| | 7109 | | { |
| 0 | 7110 | | flags.Add("+genpts"); |
| | 7111 | | } |
| | 7112 | |
|
| 0 | 7113 | | if (state.DiscardCorruptFramesInput) |
| | 7114 | | { |
| 0 | 7115 | | flags.Add("+discardcorrupt"); |
| | 7116 | | } |
| | 7117 | |
|
| 0 | 7118 | | if (state.EnableFastSeekInput) |
| | 7119 | | { |
| 0 | 7120 | | flags.Add("+fastseek"); |
| | 7121 | | } |
| | 7122 | |
|
| 0 | 7123 | | if (flags.Count > 0) |
| | 7124 | | { |
| 0 | 7125 | | inputModifier += " -fflags " + string.Join(string.Empty, flags); |
| | 7126 | | } |
| | 7127 | |
|
| 0 | 7128 | | if (state.IsVideoRequest) |
| | 7129 | | { |
| 0 | 7130 | | if (!string.IsNullOrEmpty(state.InputContainer) && state.VideoType == VideoType.VideoFile && encodingOpt |
| | 7131 | | { |
| 0 | 7132 | | var inputFormat = GetInputFormat(state.InputContainer); |
| 0 | 7133 | | if (!string.IsNullOrEmpty(inputFormat)) |
| | 7134 | | { |
| 0 | 7135 | | inputModifier += " -f " + inputFormat; |
| | 7136 | | } |
| | 7137 | | } |
| | 7138 | | } |
| | 7139 | |
|
| 0 | 7140 | | if (state.MediaSource.RequiresLooping) |
| | 7141 | | { |
| 0 | 7142 | | inputModifier += " -stream_loop -1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2"; |
| | 7143 | | } |
| | 7144 | |
|
| 0 | 7145 | | return inputModifier; |
| | 7146 | | } |
| | 7147 | |
|
| | 7148 | | public void AttachMediaSourceInfo( |
| | 7149 | | EncodingJobInfo state, |
| | 7150 | | EncodingOptions encodingOptions, |
| | 7151 | | MediaSourceInfo mediaSource, |
| | 7152 | | string requestedUrl) |
| | 7153 | | { |
| 0 | 7154 | | ArgumentNullException.ThrowIfNull(state); |
| | 7155 | |
|
| 0 | 7156 | | ArgumentNullException.ThrowIfNull(mediaSource); |
| | 7157 | |
|
| 0 | 7158 | | var path = mediaSource.Path; |
| 0 | 7159 | | var protocol = mediaSource.Protocol; |
| | 7160 | |
|
| 0 | 7161 | | if (!string.IsNullOrEmpty(mediaSource.EncoderPath) && mediaSource.EncoderProtocol.HasValue) |
| | 7162 | | { |
| 0 | 7163 | | path = mediaSource.EncoderPath; |
| 0 | 7164 | | protocol = mediaSource.EncoderProtocol.Value; |
| | 7165 | | } |
| | 7166 | |
|
| 0 | 7167 | | state.MediaPath = path; |
| 0 | 7168 | | state.InputProtocol = protocol; |
| 0 | 7169 | | state.InputContainer = mediaSource.Container; |
| 0 | 7170 | | state.RunTimeTicks = mediaSource.RunTimeTicks; |
| 0 | 7171 | | state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; |
| | 7172 | |
|
| 0 | 7173 | | state.IsoType = mediaSource.IsoType; |
| | 7174 | |
|
| 0 | 7175 | | if (mediaSource.Timestamp.HasValue) |
| | 7176 | | { |
| 0 | 7177 | | state.InputTimestamp = mediaSource.Timestamp.Value; |
| | 7178 | | } |
| | 7179 | |
|
| 0 | 7180 | | state.RunTimeTicks = mediaSource.RunTimeTicks; |
| 0 | 7181 | | state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; |
| 0 | 7182 | | state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate; |
| | 7183 | |
|
| 0 | 7184 | | if ((state.ReadInputAtNativeFramerate && !state.IsSegmentedLiveStream) |
| 0 | 7185 | | || (mediaSource.Protocol == MediaProtocol.File |
| 0 | 7186 | | && string.Equals(mediaSource.Container, "wtv", StringComparison.OrdinalIgnoreCase))) |
| | 7187 | | { |
| 0 | 7188 | | state.InputVideoSync = "-1"; |
| 0 | 7189 | | state.InputAudioSync = "1"; |
| | 7190 | | } |
| | 7191 | |
|
| 0 | 7192 | | if (string.Equals(mediaSource.Container, "wma", StringComparison.OrdinalIgnoreCase) |
| 0 | 7193 | | || string.Equals(mediaSource.Container, "asf", StringComparison.OrdinalIgnoreCase)) |
| | 7194 | | { |
| | 7195 | | // Seeing some stuttering when transcoding wma to audio-only HLS |
| 0 | 7196 | | state.InputAudioSync = "1"; |
| | 7197 | | } |
| | 7198 | |
|
| 0 | 7199 | | var mediaStreams = mediaSource.MediaStreams; |
| | 7200 | |
|
| 0 | 7201 | | if (state.IsVideoRequest) |
| | 7202 | | { |
| 0 | 7203 | | var videoRequest = state.BaseRequest; |
| | 7204 | |
|
| 0 | 7205 | | if (string.IsNullOrEmpty(videoRequest.VideoCodec)) |
| | 7206 | | { |
| 0 | 7207 | | if (string.IsNullOrEmpty(requestedUrl)) |
| | 7208 | | { |
| 0 | 7209 | | requestedUrl = "test." + videoRequest.Container; |
| | 7210 | | } |
| | 7211 | |
|
| 0 | 7212 | | videoRequest.VideoCodec = InferVideoCodec(requestedUrl); |
| | 7213 | | } |
| | 7214 | |
|
| 0 | 7215 | | state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video); |
| 0 | 7216 | | state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Su |
| 0 | 7217 | | state.SubtitleDeliveryMethod = videoRequest.SubtitleMethod; |
| 0 | 7218 | | state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio); |
| | 7219 | |
|
| 0 | 7220 | | if (state.SubtitleStream is not null && !state.SubtitleStream.IsExternal) |
| | 7221 | | { |
| 0 | 7222 | | state.InternalSubtitleStreamOffset = mediaStreams.Where(i => i.Type == MediaStreamType.Subtitle && ! |
| | 7223 | | } |
| | 7224 | |
|
| 0 | 7225 | | EnforceResolutionLimit(state); |
| | 7226 | |
|
| 0 | 7227 | | NormalizeSubtitleEmbed(state); |
| | 7228 | | } |
| | 7229 | | else |
| | 7230 | | { |
| 0 | 7231 | | state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true); |
| | 7232 | | } |
| | 7233 | |
|
| 0 | 7234 | | state.MediaSource = mediaSource; |
| | 7235 | |
|
| 0 | 7236 | | var request = state.BaseRequest; |
| 0 | 7237 | | var supportedAudioCodecs = state.SupportedAudioCodecs; |
| 0 | 7238 | | if (request is not null && supportedAudioCodecs is not null && supportedAudioCodecs.Length > 0) |
| | 7239 | | { |
| 0 | 7240 | | var supportedAudioCodecsList = supportedAudioCodecs.ToList(); |
| | 7241 | |
|
| 0 | 7242 | | ShiftAudioCodecsIfNeeded(supportedAudioCodecsList, state.AudioStream); |
| | 7243 | |
|
| 0 | 7244 | | state.SupportedAudioCodecs = supportedAudioCodecsList.ToArray(); |
| | 7245 | |
|
| 0 | 7246 | | request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(_mediaEncoder.CanEncodeToAudioCodec) |
| 0 | 7247 | | ?? state.SupportedAudioCodecs.FirstOrDefault(); |
| | 7248 | | } |
| | 7249 | |
|
| 0 | 7250 | | var supportedVideoCodecs = state.SupportedVideoCodecs; |
| 0 | 7251 | | if (request is not null && supportedVideoCodecs is not null && supportedVideoCodecs.Length > 0) |
| | 7252 | | { |
| 0 | 7253 | | var supportedVideoCodecsList = supportedVideoCodecs.ToList(); |
| | 7254 | |
|
| 0 | 7255 | | ShiftVideoCodecsIfNeeded(supportedVideoCodecsList, encodingOptions); |
| | 7256 | |
|
| 0 | 7257 | | state.SupportedVideoCodecs = supportedVideoCodecsList.ToArray(); |
| | 7258 | |
|
| 0 | 7259 | | request.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault(); |
| | 7260 | | } |
| 0 | 7261 | | } |
| | 7262 | |
|
| | 7263 | | private void ShiftAudioCodecsIfNeeded(List<string> audioCodecs, MediaStream audioStream) |
| | 7264 | | { |
| | 7265 | | // No need to shift if there is only one supported audio codec. |
| 0 | 7266 | | if (audioCodecs.Count < 2) |
| | 7267 | | { |
| 0 | 7268 | | return; |
| | 7269 | | } |
| | 7270 | |
|
| 0 | 7271 | | var inputChannels = audioStream is null ? 6 : audioStream.Channels ?? 6; |
| 0 | 7272 | | var shiftAudioCodecs = new List<string>(); |
| 0 | 7273 | | if (inputChannels >= 6) |
| | 7274 | | { |
| | 7275 | | // DTS and TrueHD are not supported by HLS |
| | 7276 | | // Keep them in the supported codecs list, but shift them to the end of the list so that if transcoding |
| 0 | 7277 | | shiftAudioCodecs.Add("dts"); |
| 0 | 7278 | | shiftAudioCodecs.Add("truehd"); |
| | 7279 | | } |
| | 7280 | | else |
| | 7281 | | { |
| | 7282 | | // Transcoding to 2ch ac3 or eac3 almost always causes a playback failure |
| | 7283 | | // Keep them in the supported codecs list, but shift them to the end of the list so that if transcoding |
| 0 | 7284 | | shiftAudioCodecs.Add("ac3"); |
| 0 | 7285 | | shiftAudioCodecs.Add("eac3"); |
| | 7286 | | } |
| | 7287 | |
|
| 0 | 7288 | | if (audioCodecs.All(i => shiftAudioCodecs.Contains(i, StringComparison.OrdinalIgnoreCase))) |
| | 7289 | | { |
| 0 | 7290 | | return; |
| | 7291 | | } |
| | 7292 | |
|
| 0 | 7293 | | while (shiftAudioCodecs.Contains(audioCodecs[0], StringComparison.OrdinalIgnoreCase)) |
| | 7294 | | { |
| 0 | 7295 | | var removed = audioCodecs[0]; |
| 0 | 7296 | | audioCodecs.RemoveAt(0); |
| 0 | 7297 | | audioCodecs.Add(removed); |
| | 7298 | | } |
| 0 | 7299 | | } |
| | 7300 | |
|
| | 7301 | | private void ShiftVideoCodecsIfNeeded(List<string> videoCodecs, EncodingOptions encodingOptions) |
| | 7302 | | { |
| | 7303 | | // No need to shift if there is only one supported video codec. |
| 0 | 7304 | | if (videoCodecs.Count < 2) |
| | 7305 | | { |
| 0 | 7306 | | return; |
| | 7307 | | } |
| | 7308 | |
|
| | 7309 | | // Shift codecs to the end of list if it's not allowed. |
| 0 | 7310 | | var shiftVideoCodecs = new List<string>(); |
| 0 | 7311 | | if (!encodingOptions.AllowHevcEncoding) |
| | 7312 | | { |
| 0 | 7313 | | shiftVideoCodecs.Add("hevc"); |
| 0 | 7314 | | shiftVideoCodecs.Add("h265"); |
| | 7315 | | } |
| | 7316 | |
|
| 0 | 7317 | | if (!encodingOptions.AllowAv1Encoding) |
| | 7318 | | { |
| 0 | 7319 | | shiftVideoCodecs.Add("av1"); |
| | 7320 | | } |
| | 7321 | |
|
| 0 | 7322 | | if (videoCodecs.All(i => shiftVideoCodecs.Contains(i, StringComparison.OrdinalIgnoreCase))) |
| | 7323 | | { |
| 0 | 7324 | | return; |
| | 7325 | | } |
| | 7326 | |
|
| 0 | 7327 | | while (shiftVideoCodecs.Contains(videoCodecs[0], StringComparison.OrdinalIgnoreCase)) |
| | 7328 | | { |
| 0 | 7329 | | var removed = videoCodecs[0]; |
| 0 | 7330 | | videoCodecs.RemoveAt(0); |
| 0 | 7331 | | videoCodecs.Add(removed); |
| | 7332 | | } |
| 0 | 7333 | | } |
| | 7334 | |
|
| | 7335 | | private void NormalizeSubtitleEmbed(EncodingJobInfo state) |
| | 7336 | | { |
| 0 | 7337 | | if (state.SubtitleStream is null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Embed) |
| | 7338 | | { |
| 0 | 7339 | | return; |
| | 7340 | | } |
| | 7341 | |
|
| | 7342 | | // This is tricky to remux in, after converting to dvdsub it's not positioned correctly |
| | 7343 | | // Therefore, let's just burn it in |
| 0 | 7344 | | if (string.Equals(state.SubtitleStream.Codec, "DVBSUB", StringComparison.OrdinalIgnoreCase)) |
| | 7345 | | { |
| 0 | 7346 | | state.SubtitleDeliveryMethod = SubtitleDeliveryMethod.Encode; |
| | 7347 | | } |
| 0 | 7348 | | } |
| | 7349 | |
|
| | 7350 | | public string GetSubtitleEmbedArguments(EncodingJobInfo state) |
| | 7351 | | { |
| 0 | 7352 | | if (state.SubtitleStream is null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Embed) |
| | 7353 | | { |
| 0 | 7354 | | return string.Empty; |
| | 7355 | | } |
| | 7356 | |
|
| 0 | 7357 | | var format = state.SupportedSubtitleCodecs.FirstOrDefault(); |
| | 7358 | | string codec; |
| | 7359 | |
|
| 0 | 7360 | | if (string.IsNullOrEmpty(format) || string.Equals(format, state.SubtitleStream.Codec, StringComparison.Ordin |
| | 7361 | | { |
| 0 | 7362 | | codec = "copy"; |
| | 7363 | | } |
| | 7364 | | else |
| | 7365 | | { |
| 0 | 7366 | | codec = format; |
| | 7367 | | } |
| | 7368 | |
|
| 0 | 7369 | | return " -codec:s:0 " + codec + " -disposition:s:0 default"; |
| | 7370 | | } |
| | 7371 | |
|
| | 7372 | | public string GetProgressiveVideoFullCommandLine(EncodingJobInfo state, EncodingOptions encodingOptions, Encoder |
| | 7373 | | { |
| | 7374 | | // Get the output codec name |
| 0 | 7375 | | var videoCodec = GetVideoEncoder(state, encodingOptions); |
| | 7376 | |
|
| 0 | 7377 | | var format = string.Empty; |
| 0 | 7378 | | var keyFrame = string.Empty; |
| 0 | 7379 | | var outputPath = state.OutputFilePath; |
| | 7380 | |
|
| 0 | 7381 | | if (Path.GetExtension(outputPath.AsSpan()).Equals(".mp4", StringComparison.OrdinalIgnoreCase) |
| 0 | 7382 | | && state.BaseRequest.Context == EncodingContext.Streaming) |
| | 7383 | | { |
| | 7384 | | // Comparison: https://github.com/jansmolders86/mediacenterjs/blob/master/lib/transcoding/desktop.js |
| 0 | 7385 | | format = " -f mp4 -movflags frag_keyframe+empty_moov+delay_moov"; |
| | 7386 | | } |
| | 7387 | |
|
| 0 | 7388 | | var threads = GetNumberOfThreads(state, encodingOptions, videoCodec); |
| | 7389 | |
|
| 0 | 7390 | | var inputModifier = GetInputModifier(state, encodingOptions, null); |
| | 7391 | |
|
| 0 | 7392 | | return string.Format( |
| 0 | 7393 | | CultureInfo.InvariantCulture, |
| 0 | 7394 | | "{0} {1}{2} {3} {4} -map_metadata -1 -map_chapters -1 -threads {5} {6}{7}{8} -y \"{9}\"", |
| 0 | 7395 | | inputModifier, |
| 0 | 7396 | | GetInputArgument(state, encodingOptions, null), |
| 0 | 7397 | | keyFrame, |
| 0 | 7398 | | GetMapArgs(state), |
| 0 | 7399 | | GetProgressiveVideoArguments(state, encodingOptions, videoCodec, defaultPreset), |
| 0 | 7400 | | threads, |
| 0 | 7401 | | GetProgressiveVideoAudioArguments(state, encodingOptions), |
| 0 | 7402 | | GetSubtitleEmbedArguments(state), |
| 0 | 7403 | | format, |
| 0 | 7404 | | outputPath).Trim(); |
| | 7405 | | } |
| | 7406 | |
|
| | 7407 | | public string GetOutputFFlags(EncodingJobInfo state) |
| | 7408 | | { |
| 0 | 7409 | | var flags = new List<string>(); |
| 0 | 7410 | | if (state.GenPtsOutput) |
| | 7411 | | { |
| 0 | 7412 | | flags.Add("+genpts"); |
| | 7413 | | } |
| | 7414 | |
|
| 0 | 7415 | | if (flags.Count > 0) |
| | 7416 | | { |
| 0 | 7417 | | return " -fflags " + string.Join(string.Empty, flags); |
| | 7418 | | } |
| | 7419 | |
|
| 0 | 7420 | | return string.Empty; |
| | 7421 | | } |
| | 7422 | |
|
| | 7423 | | public string GetProgressiveVideoArguments(EncodingJobInfo state, EncodingOptions encodingOptions, string videoC |
| | 7424 | | { |
| 0 | 7425 | | var args = "-codec:v:0 " + videoCodec; |
| | 7426 | |
|
| 0 | 7427 | | if (state.BaseRequest.EnableMpegtsM2TsMode) |
| | 7428 | | { |
| 0 | 7429 | | args += " -mpegts_m2ts_mode 1"; |
| | 7430 | | } |
| | 7431 | |
|
| 0 | 7432 | | if (IsCopyCodec(videoCodec)) |
| | 7433 | | { |
| 0 | 7434 | | if (state.VideoStream is not null |
| 0 | 7435 | | && string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) |
| 0 | 7436 | | && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) |
| | 7437 | | { |
| 0 | 7438 | | string bitStreamArgs = GetBitStreamArgs(state, MediaStreamType.Video); |
| 0 | 7439 | | if (!string.IsNullOrEmpty(bitStreamArgs)) |
| | 7440 | | { |
| 0 | 7441 | | args += " " + bitStreamArgs; |
| | 7442 | | } |
| | 7443 | | } |
| | 7444 | |
|
| 0 | 7445 | | if (state.RunTimeTicks.HasValue && state.BaseRequest.CopyTimestamps) |
| | 7446 | | { |
| 0 | 7447 | | args += " -copyts -avoid_negative_ts disabled -start_at_zero"; |
| | 7448 | | } |
| | 7449 | |
|
| 0 | 7450 | | if (!state.RunTimeTicks.HasValue) |
| | 7451 | | { |
| 0 | 7452 | | args += " -fflags +genpts"; |
| | 7453 | | } |
| | 7454 | | } |
| | 7455 | | else |
| | 7456 | | { |
| 0 | 7457 | | var keyFrameArg = string.Format( |
| 0 | 7458 | | CultureInfo.InvariantCulture, |
| 0 | 7459 | | " -force_key_frames \"expr:gte(t,n_forced*{0})\"", |
| 0 | 7460 | | 5); |
| | 7461 | |
|
| 0 | 7462 | | args += keyFrameArg; |
| | 7463 | |
|
| 0 | 7464 | | var hasGraphicalSubs = state.SubtitleStream is not null && !state.SubtitleStream.IsTextSubtitleStream && |
| | 7465 | |
|
| 0 | 7466 | | var hasCopyTs = false; |
| | 7467 | |
|
| | 7468 | | // video processing filters. |
| 0 | 7469 | | var videoProcessParam = GetVideoProcessingFilterParam(state, encodingOptions, videoCodec); |
| | 7470 | |
|
| 0 | 7471 | | var negativeMapArgs = GetNegativeMapArgsByFilters(state, videoProcessParam); |
| | 7472 | |
|
| 0 | 7473 | | args = negativeMapArgs + args + videoProcessParam; |
| | 7474 | |
|
| 0 | 7475 | | hasCopyTs = videoProcessParam.Contains("copyts", StringComparison.OrdinalIgnoreCase); |
| | 7476 | |
|
| 0 | 7477 | | if (state.RunTimeTicks.HasValue && state.BaseRequest.CopyTimestamps) |
| | 7478 | | { |
| 0 | 7479 | | if (!hasCopyTs) |
| | 7480 | | { |
| 0 | 7481 | | args += " -copyts"; |
| | 7482 | | } |
| | 7483 | |
|
| 0 | 7484 | | args += " -avoid_negative_ts disabled"; |
| | 7485 | |
|
| 0 | 7486 | | if (!(state.SubtitleStream is not null && state.SubtitleStream.IsExternal && !state.SubtitleStream.I |
| | 7487 | | { |
| 0 | 7488 | | args += " -start_at_zero"; |
| | 7489 | | } |
| | 7490 | | } |
| | 7491 | |
|
| 0 | 7492 | | var qualityParam = GetVideoQualityParam(state, videoCodec, encodingOptions, defaultPreset); |
| | 7493 | |
|
| 0 | 7494 | | if (!string.IsNullOrEmpty(qualityParam)) |
| | 7495 | | { |
| 0 | 7496 | | args += " " + qualityParam.Trim(); |
| | 7497 | | } |
| | 7498 | | } |
| | 7499 | |
|
| 0 | 7500 | | if (!string.IsNullOrEmpty(state.OutputVideoSync)) |
| | 7501 | | { |
| 0 | 7502 | | args += GetVideoSyncOption(state.OutputVideoSync, _mediaEncoder.EncoderVersion); |
| | 7503 | | } |
| | 7504 | |
|
| 0 | 7505 | | args += GetOutputFFlags(state); |
| | 7506 | |
|
| 0 | 7507 | | return args; |
| | 7508 | | } |
| | 7509 | |
|
| | 7510 | | public string GetProgressiveVideoAudioArguments(EncodingJobInfo state, EncodingOptions encodingOptions) |
| | 7511 | | { |
| | 7512 | | // If the video doesn't have an audio stream, return a default. |
| 0 | 7513 | | if (state.AudioStream is null && state.VideoStream is not null) |
| | 7514 | | { |
| 0 | 7515 | | return string.Empty; |
| | 7516 | | } |
| | 7517 | |
|
| | 7518 | | // Get the output codec name |
| 0 | 7519 | | var codec = GetAudioEncoder(state); |
| | 7520 | |
|
| 0 | 7521 | | var args = "-codec:a:0 " + codec; |
| | 7522 | |
|
| 0 | 7523 | | if (IsCopyCodec(codec)) |
| | 7524 | | { |
| 0 | 7525 | | return args; |
| | 7526 | | } |
| | 7527 | |
|
| 0 | 7528 | | var channels = state.OutputAudioChannels; |
| | 7529 | |
|
| 0 | 7530 | | var useDownMixAlgorithm = state.AudioStream is not null |
| 0 | 7531 | | && DownMixAlgorithmsHelper.AlgorithmFilterStrings.ContainsKey((encodingOptions.Dow |
| | 7532 | |
|
| 0 | 7533 | | if (channels.HasValue && !useDownMixAlgorithm) |
| | 7534 | | { |
| 0 | 7535 | | args += " -ac " + channels.Value; |
| | 7536 | | } |
| | 7537 | |
|
| 0 | 7538 | | var bitrate = state.OutputAudioBitrate; |
| 0 | 7539 | | if (bitrate.HasValue && !LosslessAudioCodecs.Contains(codec, StringComparison.OrdinalIgnoreCase)) |
| | 7540 | | { |
| 0 | 7541 | | var vbrParam = GetAudioVbrModeParam(codec, bitrate.Value, channels ?? 2); |
| 0 | 7542 | | if (encodingOptions.EnableAudioVbr && state.EnableAudioVbrEncoding && vbrParam is not null) |
| | 7543 | | { |
| 0 | 7544 | | args += vbrParam; |
| | 7545 | | } |
| | 7546 | | else |
| | 7547 | | { |
| 0 | 7548 | | args += " -ab " + bitrate.Value.ToString(CultureInfo.InvariantCulture); |
| | 7549 | | } |
| | 7550 | | } |
| | 7551 | |
|
| 0 | 7552 | | if (state.OutputAudioSampleRate.HasValue) |
| | 7553 | | { |
| 0 | 7554 | | args += " -ar " + state.OutputAudioSampleRate.Value.ToString(CultureInfo.InvariantCulture); |
| | 7555 | | } |
| | 7556 | |
|
| 0 | 7557 | | args += GetAudioFilterParam(state, encodingOptions); |
| | 7558 | |
|
| 0 | 7559 | | return args; |
| | 7560 | | } |
| | 7561 | |
|
| | 7562 | | public string GetProgressiveAudioFullCommandLine(EncodingJobInfo state, EncodingOptions encodingOptions, string |
| | 7563 | | { |
| 0 | 7564 | | var audioTranscodeParams = new List<string>(); |
| | 7565 | |
|
| 0 | 7566 | | var bitrate = state.OutputAudioBitrate; |
| 0 | 7567 | | var channels = state.OutputAudioChannels; |
| 0 | 7568 | | var outputCodec = state.OutputAudioCodec; |
| | 7569 | |
|
| 0 | 7570 | | if (bitrate.HasValue && !LosslessAudioCodecs.Contains(outputCodec, StringComparison.OrdinalIgnoreCase)) |
| | 7571 | | { |
| 0 | 7572 | | var vbrParam = GetAudioVbrModeParam(GetAudioEncoder(state), bitrate.Value, channels ?? 2); |
| 0 | 7573 | | if (encodingOptions.EnableAudioVbr && state.EnableAudioVbrEncoding && vbrParam is not null) |
| | 7574 | | { |
| 0 | 7575 | | audioTranscodeParams.Add(vbrParam); |
| | 7576 | | } |
| | 7577 | | else |
| | 7578 | | { |
| 0 | 7579 | | audioTranscodeParams.Add("-ab " + bitrate.Value.ToString(CultureInfo.InvariantCulture)); |
| | 7580 | | } |
| | 7581 | | } |
| | 7582 | |
|
| 0 | 7583 | | if (channels.HasValue) |
| | 7584 | | { |
| 0 | 7585 | | audioTranscodeParams.Add("-ac " + state.OutputAudioChannels.Value.ToString(CultureInfo.InvariantCulture) |
| | 7586 | | } |
| | 7587 | |
|
| 0 | 7588 | | if (!string.IsNullOrEmpty(outputCodec)) |
| | 7589 | | { |
| 0 | 7590 | | audioTranscodeParams.Add("-acodec " + GetAudioEncoder(state)); |
| | 7591 | | } |
| | 7592 | |
|
| 0 | 7593 | | if (GetAudioEncoder(state).StartsWith("pcm_", StringComparison.Ordinal)) |
| | 7594 | | { |
| 0 | 7595 | | audioTranscodeParams.Add(string.Concat("-f ", GetAudioEncoder(state).AsSpan(4))); |
| 0 | 7596 | | audioTranscodeParams.Add("-ar " + state.BaseRequest.AudioBitRate); |
| | 7597 | | } |
| | 7598 | |
|
| 0 | 7599 | | if (!string.Equals(outputCodec, "opus", StringComparison.OrdinalIgnoreCase)) |
| | 7600 | | { |
| | 7601 | | // opus only supports specific sampling rates |
| 0 | 7602 | | var sampleRate = state.OutputAudioSampleRate; |
| 0 | 7603 | | if (sampleRate.HasValue) |
| | 7604 | | { |
| 0 | 7605 | | var sampleRateValue = sampleRate.Value switch |
| 0 | 7606 | | { |
| 0 | 7607 | | <= 8000 => 8000, |
| 0 | 7608 | | <= 12000 => 12000, |
| 0 | 7609 | | <= 16000 => 16000, |
| 0 | 7610 | | <= 24000 => 24000, |
| 0 | 7611 | | _ => 48000 |
| 0 | 7612 | | }; |
| | 7613 | |
|
| 0 | 7614 | | audioTranscodeParams.Add("-ar " + sampleRateValue.ToString(CultureInfo.InvariantCulture)); |
| | 7615 | | } |
| | 7616 | | } |
| | 7617 | |
|
| | 7618 | | // Copy the movflags from GetProgressiveVideoFullCommandLine |
| | 7619 | | // See #9248 and the associated PR for why this is needed |
| 0 | 7620 | | if (_mp4ContainerNames.Contains(state.OutputContainer)) |
| | 7621 | | { |
| 0 | 7622 | | audioTranscodeParams.Add("-movflags empty_moov+delay_moov"); |
| | 7623 | | } |
| | 7624 | |
|
| 0 | 7625 | | var threads = GetNumberOfThreads(state, encodingOptions, null); |
| | 7626 | |
|
| 0 | 7627 | | var inputModifier = GetInputModifier(state, encodingOptions, null); |
| | 7628 | |
|
| 0 | 7629 | | return string.Format( |
| 0 | 7630 | | CultureInfo.InvariantCulture, |
| 0 | 7631 | | "{0} {1}{7}{8} -threads {2}{3} {4} -id3v2_version 3 -write_id3v1 1{6} -y \"{5}\"", |
| 0 | 7632 | | inputModifier, |
| 0 | 7633 | | GetInputArgument(state, encodingOptions, null), |
| 0 | 7634 | | threads, |
| 0 | 7635 | | " -vn", |
| 0 | 7636 | | string.Join(' ', audioTranscodeParams), |
| 0 | 7637 | | outputPath, |
| 0 | 7638 | | string.Empty, |
| 0 | 7639 | | string.Empty, |
| 0 | 7640 | | string.Empty).Trim(); |
| | 7641 | | } |
| | 7642 | |
|
| | 7643 | | public static int FindIndex(IReadOnlyList<MediaStream> mediaStreams, MediaStream streamToFind) |
| | 7644 | | { |
| 0 | 7645 | | var index = 0; |
| 0 | 7646 | | var length = mediaStreams.Count; |
| | 7647 | |
|
| 0 | 7648 | | for (var i = 0; i < length; i++) |
| | 7649 | | { |
| 0 | 7650 | | var currentMediaStream = mediaStreams[i]; |
| 0 | 7651 | | if (currentMediaStream == streamToFind) |
| | 7652 | | { |
| 0 | 7653 | | return index; |
| | 7654 | | } |
| | 7655 | |
|
| 0 | 7656 | | if (string.Equals(currentMediaStream.Path, streamToFind.Path, StringComparison.Ordinal)) |
| | 7657 | | { |
| 0 | 7658 | | index++; |
| | 7659 | | } |
| | 7660 | | } |
| | 7661 | |
|
| 0 | 7662 | | return -1; |
| | 7663 | | } |
| | 7664 | |
|
| | 7665 | | public static bool IsCopyCodec(string codec) |
| | 7666 | | { |
| 0 | 7667 | | return string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase); |
| | 7668 | | } |
| | 7669 | |
|
| | 7670 | | private static bool ShouldEncodeSubtitle(EncodingJobInfo state) |
| | 7671 | | { |
| 0 | 7672 | | return state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode |
| 0 | 7673 | | || (state.BaseRequest.AlwaysBurnInSubtitleWhenTranscoding && !IsCopyCodec(state.OutputVideoCodec)); |
| | 7674 | | } |
| | 7675 | |
|
| | 7676 | | public static string GetVideoSyncOption(string videoSync, Version encoderVersion) |
| | 7677 | | { |
| 0 | 7678 | | if (string.IsNullOrEmpty(videoSync)) |
| | 7679 | | { |
| 0 | 7680 | | return string.Empty; |
| | 7681 | | } |
| | 7682 | |
|
| 0 | 7683 | | if (encoderVersion >= new Version(5, 1)) |
| | 7684 | | { |
| 0 | 7685 | | if (int.TryParse(videoSync, CultureInfo.InvariantCulture, out var vsync)) |
| | 7686 | | { |
| 0 | 7687 | | return vsync switch |
| 0 | 7688 | | { |
| 0 | 7689 | | -1 => " -fps_mode auto", |
| 0 | 7690 | | 0 => " -fps_mode passthrough", |
| 0 | 7691 | | 1 => " -fps_mode cfr", |
| 0 | 7692 | | 2 => " -fps_mode vfr", |
| 0 | 7693 | | _ => string.Empty |
| 0 | 7694 | | }; |
| | 7695 | | } |
| | 7696 | |
|
| 0 | 7697 | | return string.Empty; |
| | 7698 | | } |
| | 7699 | |
|
| | 7700 | | // -vsync is deprecated in FFmpeg 5.1 and will be removed in the future. |
| 0 | 7701 | | return $" -vsync {videoSync}"; |
| | 7702 | | } |
| | 7703 | | } |
| | 7704 | | } |