From 4415506837930b9de4c00dc9ecb2f6c6e361bc7a Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 26 Nov 2015 23:33:20 -0500 Subject: [PATCH] support qsv encoding --- .../Playback/BaseStreamingService.cs | 49 ++++++++++--------- .../Playback/Dash/MpegDashService.cs | 2 +- .../Playback/Hls/DynamicHlsService.cs | 40 +-------------- .../Playback/Hls/VideoHlsService.cs | 2 +- MediaBrowser.Api/Playback/StreamRequest.cs | 1 - .../Playback/TranscodingThrottler.cs | 2 +- .../Savers/GameXmlSaver.cs | 14 ------ .../Encoder/BaseEncoder.cs | 2 +- .../Configuration/EncodingOptions.cs | 6 +-- .../TunerHosts/HdHomerun/HdHomerunHost.cs | 2 +- 10 files changed, 37 insertions(+), 83 deletions(-) diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index 36207c8b4..43866889f 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -130,7 +130,6 @@ namespace MediaBrowser.Api.Playback data += "-" + (state.Request.DeviceId ?? string.Empty); data += "-" + (state.Request.PlaySessionId ?? string.Empty); - data += "-" + (state.Request.ClientTime ?? string.Empty); var dataHash = data.GetMD5().ToString("N"); @@ -287,12 +286,18 @@ namespace MediaBrowser.Api.Playback return threads; } - protected string H264Encoder + protected string GetH264Encoder(StreamState state) { - get + if (string.Equals(ApiEntryPoint.Instance.GetEncodingOptions().HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase)) { - return "libx264"; + // It's currently failing on live tv + if (state.RunTimeTicks.HasValue) + { + return "h264_qsv"; + } } + + return "libx264"; } /// @@ -313,7 +318,7 @@ namespace MediaBrowser.Api.Playback { param = "-preset superfast"; - param += " -crf 23"; + param += " -crf 23 -rc-lookahead 0 -muxdelay 0 -refs 1"; } else if (string.Equals(videoCodec, "libx265", StringComparison.OrdinalIgnoreCase)) @@ -326,7 +331,7 @@ namespace MediaBrowser.Api.Playback // h264 (h264_qsv) else if (string.Equals(videoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase)) { - param = "-preset 7"; + param = "-preset 7 -look_ahead 0"; } @@ -400,8 +405,10 @@ namespace MediaBrowser.Api.Playback if (!string.IsNullOrEmpty(state.VideoRequest.Level)) { + var h264Encoder = GetH264Encoder(state); + // h264_qsv and libnvenc expect levels to be expressed as a decimal. libx264 supports decimal and non-decimal format - if (String.Equals(H264Encoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) || String.Equals(H264Encoder, "libnvenc", StringComparison.OrdinalIgnoreCase)) + if (String.Equals(h264Encoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) || String.Equals(h264Encoder, "libnvenc", StringComparison.OrdinalIgnoreCase)) { switch (state.VideoRequest.Level) { @@ -561,7 +568,10 @@ namespace MediaBrowser.Api.Playback if (string.Equals(outputVideoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase)) { - filters[filters.Count - 1] += ":flags=fast_bilinear"; + if (filters.Count > 1) + { + //filters[filters.Count - 1] += ":flags=fast_bilinear"; + } } var output = string.Empty; @@ -782,7 +792,7 @@ namespace MediaBrowser.Api.Playback { if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase)) { - return H264Encoder; + return GetH264Encoder(state); } if (string.Equals(codec, "vpx", StringComparison.OrdinalIgnoreCase)) { @@ -810,7 +820,7 @@ namespace MediaBrowser.Api.Playback /// System.String. protected string GetVideoDecoder(StreamState state) { - if (string.Equals(ApiEntryPoint.Instance.GetEncodingOptions().HardwareVideoDecoder, "qsv", StringComparison.OrdinalIgnoreCase)) + if (string.Equals(ApiEntryPoint.Instance.GetEncodingOptions().HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase)) { if (state.VideoStream != null && !string.IsNullOrWhiteSpace(state.VideoStream.Codec)) { @@ -1415,50 +1425,46 @@ namespace MediaBrowser.Api.Playback } } else if (i == 16) - { - request.ClientTime = val; - } - else if (i == 17) { if (videoRequest != null) { videoRequest.MaxRefFrames = int.Parse(val, UsCulture); } } - else if (i == 18) + else if (i == 17) { if (videoRequest != null) { videoRequest.MaxVideoBitDepth = int.Parse(val, UsCulture); } } - else if (i == 19) + else if (i == 18) { if (videoRequest != null) { videoRequest.Profile = val; } } - else if (i == 20) + else if (i == 19) { if (videoRequest != null) { videoRequest.Cabac = string.Equals("true", val, StringComparison.OrdinalIgnoreCase); } } - else if (i == 21) + else if (i == 20) { request.PlaySessionId = val; } - else if (i == 22) + else if (i == 21) { // api_key } - else if (i == 23) + else if (i == 22) { request.LiveStreamId = val; } - else if (i == 24) + else if (i == 23) { // Duplicating ItemId because of MediaMonkey } @@ -1662,7 +1668,6 @@ namespace MediaBrowser.Api.Playback state.InputContainer = mediaSource.Container; state.InputFileSize = mediaSource.Size; state.InputBitrate = mediaSource.Bitrate; - state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate; state.RunTimeTicks = mediaSource.RunTimeTicks; state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; diff --git a/MediaBrowser.Api/Playback/Dash/MpegDashService.cs b/MediaBrowser.Api/Playback/Dash/MpegDashService.cs index 49d3db110..9ec16fcc7 100644 --- a/MediaBrowser.Api/Playback/Dash/MpegDashService.cs +++ b/MediaBrowser.Api/Playback/Dash/MpegDashService.cs @@ -430,7 +430,7 @@ namespace MediaBrowser.Api.Playback.Dash var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream; - args += " " + GetVideoQualityParam(state, H264Encoder, true) + keyFrameArg; + args += " " + GetVideoQualityParam(state, GetH264Encoder(state), true) + keyFrameArg; // Add resolution params, if specified if (!hasGraphicalSubs) diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index 5be6ca545..546b1ec5f 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -1,5 +1,4 @@ -using MediaBrowser.Common.IO; -using MediaBrowser.Common.Net; +using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Devices; using MediaBrowser.Controller.Dlna; @@ -475,41 +474,6 @@ namespace MediaBrowser.Api.Playback.Hls await Task.Delay(100, cancellationToken).ConfigureAwait(false); } - // if a different file is encoding, it's done - //var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath); - //if (currentTranscodingIndex > segmentIndex) - //{ - //return GetSegmentResult(segmentPath, segmentIndex); - //} - - //// Wait for the file to stop being written to, then stream it - //var length = new FileInfo(segmentPath).Length; - //var eofCount = 0; - - //while (eofCount < 10) - //{ - // var info = new FileInfo(segmentPath); - - // if (!info.Exists) - // { - // break; - // } - - // var newLength = info.Length; - - // if (newLength == length) - // { - // eofCount++; - // } - // else - // { - // eofCount = 0; - // } - - // length = newLength; - // await Task.Delay(100, cancellationToken).ConfigureAwait(false); - //} - cancellationToken.ThrowIfCancellationRequested(); return GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob); } @@ -871,7 +835,7 @@ namespace MediaBrowser.Api.Playback.Hls var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream; - args += " " + GetVideoQualityParam(state, H264Encoder, true) + keyFrameArg; + args += " " + GetVideoQualityParam(state, GetH264Encoder(state), true) + keyFrameArg; //args += " -mixed-refs 0 -refs 3 -x264opts b_pyramid=0:weightb=0:weightp=0"; diff --git a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs index c5392b38f..be1db1a4d 100644 --- a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs @@ -106,7 +106,7 @@ namespace MediaBrowser.Api.Playback.Hls var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream; - args += " " + GetVideoQualityParam(state, H264Encoder, true) + keyFrameArg; + args += " " + GetVideoQualityParam(state, GetH264Encoder(state), true) + keyFrameArg; // Add resolution params, if specified if (!hasGraphicalSubs) diff --git a/MediaBrowser.Api/Playback/StreamRequest.cs b/MediaBrowser.Api/Playback/StreamRequest.cs index 75242e604..69f8e6e04 100644 --- a/MediaBrowser.Api/Playback/StreamRequest.cs +++ b/MediaBrowser.Api/Playback/StreamRequest.cs @@ -70,7 +70,6 @@ namespace MediaBrowser.Api.Playback public string DeviceProfileId { get; set; } public string Params { get; set; } - public string ClientTime { get; set; } public string PlaySessionId { get; set; } public string LiveStreamId { get; set; } } diff --git a/MediaBrowser.Api/Playback/TranscodingThrottler.cs b/MediaBrowser.Api/Playback/TranscodingThrottler.cs index fec3dda86..ece455009 100644 --- a/MediaBrowser.Api/Playback/TranscodingThrottler.cs +++ b/MediaBrowser.Api/Playback/TranscodingThrottler.cs @@ -42,7 +42,7 @@ namespace MediaBrowser.Api.Playback var options = GetOptions(); - if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleThresholdInSeconds)) + if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleThresholdSeconds)) { PauseTranscoding(); } diff --git a/MediaBrowser.LocalMetadata/Savers/GameXmlSaver.cs b/MediaBrowser.LocalMetadata/Savers/GameXmlSaver.cs index 89c274e12..53efc7811 100644 --- a/MediaBrowser.LocalMetadata/Savers/GameXmlSaver.cs +++ b/MediaBrowser.LocalMetadata/Savers/GameXmlSaver.cs @@ -79,20 +79,6 @@ namespace MediaBrowser.LocalMetadata.Savers builder.Append("" + SecurityElement.Escape(game.GameSystem) + ""); } - var val = game.GetProviderId(MetadataProviders.NesBox); - - if (!string.IsNullOrEmpty(val)) - { - builder.Append("" + SecurityElement.Escape(val) + ""); - } - - val = game.GetProviderId(MetadataProviders.NesBoxRom); - - if (!string.IsNullOrEmpty(val)) - { - builder.Append("" + SecurityElement.Escape(val) + ""); - } - XmlSaverHelpers.AddCommonNodes(game, _libraryManager, builder); builder.Append(""); diff --git a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs index 912110c04..98e4a58a6 100644 --- a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs @@ -359,7 +359,7 @@ namespace MediaBrowser.MediaEncoding.Encoder /// System.String. protected string GetVideoDecoder(EncodingJob state) { - if (string.Equals(GetEncodingOptions().HardwareVideoDecoder, "qsv", StringComparison.OrdinalIgnoreCase)) + if (string.Equals(GetEncodingOptions().HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase)) { if (state.VideoStream != null && !string.IsNullOrWhiteSpace(state.VideoStream.Codec)) { diff --git a/MediaBrowser.Model/Configuration/EncodingOptions.cs b/MediaBrowser.Model/Configuration/EncodingOptions.cs index 77b894eb8..b41ad7eb0 100644 --- a/MediaBrowser.Model/Configuration/EncodingOptions.cs +++ b/MediaBrowser.Model/Configuration/EncodingOptions.cs @@ -8,14 +8,14 @@ namespace MediaBrowser.Model.Configuration public double DownMixAudioBoost { get; set; } public bool EnableDebugLogging { get; set; } public bool EnableThrottling { get; set; } - public int ThrottleThresholdInSeconds { get; set; } - public string HardwareVideoDecoder { get; set; } + public int ThrottleThresholdSeconds { get; set; } + public string HardwareAccelerationType { get; set; } public EncodingOptions() { DownMixAudioBoost = 2; EnableThrottling = true; - ThrottleThresholdInSeconds = 120; + ThrottleThresholdSeconds = 110; EncodingThreadCount = -1; } } diff --git a/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs b/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs index a237bb226..fb8e77006 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs @@ -234,7 +234,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.HdHomerun int? width = null; int? height = null; bool isInterlaced = true; - var videoCodec = !string.IsNullOrWhiteSpace(GetEncodingOptions().HardwareVideoDecoder) ? null : "mpeg2video"; + var videoCodec = !string.IsNullOrWhiteSpace(GetEncodingOptions().HardwareAccelerationType) ? null : "mpeg2video"; int? videoBitrate = null;