diff options
| author | Luke <luke.pulverenti@gmail.com> | 2017-10-01 23:35:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-01 23:35:45 -0400 |
| commit | a73da532d52ae68ad01965dbf368bacc7d56a218 (patch) | |
| tree | a6f05e95515f822dfc129c36554f74f1da4d4c23 /MediaBrowser.Controller/MediaEncoding | |
| parent | f651fd6ffba80a6f8e54fca7d014622692cc08f7 (diff) | |
| parent | 796f374359d75e22d2095b3f3a8f9b220cacde27 (diff) | |
Merge pull request #2931 from MediaBrowser/beta
Beta
Diffstat (limited to 'MediaBrowser.Controller/MediaEncoding')
3 files changed, 222 insertions, 74 deletions
diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs index 657b9c959..8b612f809 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs @@ -346,7 +346,8 @@ namespace MediaBrowser.Controller.MediaEncoding "Constrained High" }; - return Array.FindIndex(list.ToArray(), t => string.Equals(t, profile, StringComparison.OrdinalIgnoreCase)); + // strip spaces because they may be stripped out on the query string + return Array.FindIndex(list.ToArray(), t => string.Equals(t.Replace(" ", ""), profile.Replace(" ", ""), StringComparison.OrdinalIgnoreCase)); } public string GetInputPathArgument(EncodingJobInfo state) @@ -529,7 +530,8 @@ namespace MediaBrowser.Controller.MediaEncoding { var seconds = Math.Round(TimeSpan.FromTicks(state.StartTimeTicks ?? 0).TotalSeconds); - var setPtsParam = state.CopyTimestamps + // hls always copies timestamps + var setPtsParam = state.CopyTimestamps || state.TranscodingType != TranscodingJobType.Progressive ? string.Empty : string.Format(",setpts=PTS -{0}/TB", seconds.ToString(_usCulture)); @@ -691,22 +693,26 @@ namespace MediaBrowser.Controller.MediaEncoding param += string.Format(" -r {0}", framerate.Value.ToString(_usCulture)); } - var request = state.BaseRequest; + var targetVideoCodec = state.ActualOutputVideoCodec; - if (!string.IsNullOrEmpty(request.Profile)) + var request = state.BaseRequest; + var profile = state.GetRequestedProfiles(targetVideoCodec).FirstOrDefault(); + if (!string.IsNullOrEmpty(profile)) { if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase) && !string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) && !string.Equals(videoEncoder, "h264_v4l2m2m", StringComparison.OrdinalIgnoreCase)) { // not supported by h264_omx - param += " -profile:v " + request.Profile; + param += " -profile:v " + profile; } } - if (!string.IsNullOrEmpty(request.Level)) + var level = state.GetRequestedLevel(targetVideoCodec); + + if (!string.IsNullOrEmpty(level)) { - var level = NormalizeTranscodingLevel(state.OutputVideoCodec, request.Level); + level = NormalizeTranscodingLevel(state.OutputVideoCodec, level); // h264_qsv and h264_nvenc expect levels to be expressed as a decimal. libx264 supports decimal and non-decimal format // also needed for libx264 due to https://trac.ffmpeg.org/ticket/3307 @@ -756,7 +762,6 @@ namespace MediaBrowser.Controller.MediaEncoding { param += " -level " + level; } - } if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase)) @@ -796,7 +801,7 @@ namespace MediaBrowser.Controller.MediaEncoding if (videoStream.IsInterlaced) { - if (request.DeInterlace) + if (state.DeInterlace(videoStream.Codec, false)) { return false; } @@ -828,23 +833,27 @@ namespace MediaBrowser.Controller.MediaEncoding } // Source and target codecs must match - if (string.IsNullOrEmpty(videoStream.Codec) || !state.SupportedVideoCodecs.Contains(videoStream.Codec, StringComparer.OrdinalIgnoreCase)) + if (string.IsNullOrWhiteSpace(videoStream.Codec) || !state.SupportedVideoCodecs.Contains(videoStream.Codec, StringComparer.OrdinalIgnoreCase)) { return false; } + var requestedProfiles = state.GetRequestedProfiles(videoStream.Codec); + // If client is requesting a specific video profile, it must match the source - if (!string.IsNullOrEmpty(request.Profile)) + if (requestedProfiles.Length > 0) { - if (string.IsNullOrEmpty(videoStream.Profile)) + if (string.IsNullOrWhiteSpace(videoStream.Profile)) { //return false; } - if (!string.IsNullOrEmpty(videoStream.Profile) && !string.Equals(request.Profile, videoStream.Profile, StringComparison.OrdinalIgnoreCase)) + var requestedProfile = requestedProfiles[0]; + // strip spaces because they may be stripped out on the query string as well + if (!string.IsNullOrWhiteSpace(videoStream.Profile) && !requestedProfiles.Contains(videoStream.Profile.Replace(" ", ""), StringComparer.OrdinalIgnoreCase)) { var currentScore = GetVideoProfileScore(videoStream.Profile); - var requestedScore = GetVideoProfileScore(request.Profile); + var requestedScore = GetVideoProfileScore(requestedProfile); if (currentScore == -1 || currentScore > requestedScore) { @@ -909,11 +918,12 @@ namespace MediaBrowser.Controller.MediaEncoding } // If a specific level was requested, the source must match or be less than - if (!string.IsNullOrEmpty(request.Level)) + var level = state.GetRequestedLevel(videoStream.Codec); + if (!string.IsNullOrEmpty(level)) { double requestLevel; - if (double.TryParse(request.Level, NumberStyles.Any, _usCulture, out requestLevel)) + if (double.TryParse(level, NumberStyles.Any, _usCulture, out requestLevel)) { if (!videoStream.Level.HasValue) { @@ -1074,7 +1084,8 @@ namespace MediaBrowser.Controller.MediaEncoding } } - if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode && !state.CopyTimestamps) + var isCopyingTimestamps = state.CopyTimestamps || state.TranscodingType != TranscodingJobType.Progressive; + if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode && !isCopyingTimestamps) { var seconds = TimeSpan.FromTicks(state.StartTimeTicks ?? 0).TotalSeconds; @@ -1357,9 +1368,10 @@ namespace MediaBrowser.Controller.MediaEncoding filters.Add("hwupload"); } - if (state.DeInterlace && !string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase)) + if (state.DeInterlace("h264", true) && !string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase)) { - if (string.Equals(options.DeinterlaceMethod, "bobandweave", StringComparison.OrdinalIgnoreCase)) + // If it is already 60fps then it will create an output framerate that is much too high for roku and others to handle + if (string.Equals(options.DeinterlaceMethod, "bobandweave", StringComparison.OrdinalIgnoreCase) && (state.VideoStream.RealFrameRate ?? 60) <= 30) { filters.Add("yadif=1:-1:0"); } @@ -1523,11 +1535,18 @@ namespace MediaBrowser.Controller.MediaEncoding /// <returns>System.Int32.</returns> public int GetNumberOfThreads(EncodingJobInfo state, EncodingOptions encodingOptions, bool isWebm) { - var threads = GetNumberOfThreadsInternal(state, encodingOptions, isWebm); + if (isWebm) + { + // Recommended per docs + return Math.Max(Environment.ProcessorCount - 1, 2); + } + + var threads = state.BaseRequest.CpuCoreLimit ?? encodingOptions.EncodingThreadCount; - if (state.BaseRequest.CpuCoreLimit.HasValue && state.BaseRequest.CpuCoreLimit.Value > 0) + // Automatic + if (threads <= 0 || threads >= Environment.ProcessorCount) { - threads = Math.Min(threads, state.BaseRequest.CpuCoreLimit.Value); + return 0; } return threads; @@ -1799,11 +1818,6 @@ namespace MediaBrowser.Controller.MediaEncoding state.InternalSubtitleStreamOffset = mediaStreams.Where(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal).ToList().IndexOf(state.SubtitleStream); } - if (state.VideoStream != null && state.VideoStream.IsInterlaced) - { - state.DeInterlace = true; - } - EnforceResolutionLimit(state); NormalizeSubtitleEmbed(state); @@ -1954,29 +1968,6 @@ namespace MediaBrowser.Controller.MediaEncoding return null; } - /// <summary> - /// Gets the number of threads. - /// </summary> - /// <returns>System.Int32.</returns> - private int GetNumberOfThreadsInternal(EncodingJobInfo state, EncodingOptions encodingOptions, bool isWebm) - { - var threads = encodingOptions.EncodingThreadCount; - - if (isWebm) - { - // Recommended per docs - return Math.Max(Environment.ProcessorCount - 1, 2); - } - - // Automatic - if (threads == -1) - { - return 0; - } - - return threads; - } - public string GetSubtitleEmbedArguments(EncodingJobInfo state) { if (state.SubtitleStream == null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Embed) diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingJobInfo.cs b/MediaBrowser.Controller/MediaEncoding/EncodingJobInfo.cs index e76217fda..506fce3ca 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingJobInfo.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingJobInfo.cs @@ -160,7 +160,97 @@ namespace MediaBrowser.Controller.MediaEncoding public int? OutputAudioBitrate; public int? OutputAudioChannels; - public bool DeInterlace { get; set; } + + public bool DeInterlace(string videoCodec, bool forceDeinterlaceIfSourceIsInterlaced) + { + var videoStream = VideoStream; + var isInputInterlaced = videoStream != null && videoStream.IsInterlaced; + + if (!isInputInterlaced) + { + return false; + } + + // Support general param + if (BaseRequest.DeInterlace) + { + return true; + } + + if (!string.IsNullOrWhiteSpace(videoCodec)) + { + if (string.Equals(BaseRequest.GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + if (forceDeinterlaceIfSourceIsInterlaced) + { + if (isInputInterlaced) + { + return true; + } + } + + return false; + } + + public string[] GetRequestedProfiles(string codec) + { + if (!string.IsNullOrWhiteSpace(BaseRequest.Profile)) + { + return BaseRequest.Profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries); + } + + if (!string.IsNullOrWhiteSpace(codec)) + { + var profile = BaseRequest.GetOption(codec, "profile"); + + if (!string.IsNullOrWhiteSpace(profile)) + { + return profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries); + } + } + + return new string[] { }; + } + + public string GetRequestedLevel(string codec) + { + if (!string.IsNullOrWhiteSpace(BaseRequest.Level)) + { + return BaseRequest.Level; + } + + if (!string.IsNullOrWhiteSpace(codec)) + { + return BaseRequest.GetOption(codec, "level"); + } + + return null; + } + + public int? GetRequestedMaxRefFrames(string codec) + { + if (!string.IsNullOrWhiteSpace(BaseRequest.Level)) + { + return BaseRequest.MaxRefFrames; + } + + if (!string.IsNullOrWhiteSpace(codec)) + { + var value = BaseRequest.GetOption(codec, "maxrefframes"); + int result; + if (!string.IsNullOrWhiteSpace(value) && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) + { + return result; + } + } + + return null; + } + public bool IsVideoRequest { get; set; } public TranscodingJobType TranscodingType { get; set; } @@ -169,7 +259,7 @@ namespace MediaBrowser.Controller.MediaEncoding _logger = logger; TranscodingType = jobType; RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); - PlayableStreamFileNames = new string[]{}; + PlayableStreamFileNames = new string[] { }; SupportedAudioCodecs = new List<string>(); SupportedVideoCodecs = new List<string>(); SupportedSubtitleCodecs = new List<string>(); @@ -319,12 +409,19 @@ namespace MediaBrowser.Controller.MediaEncoding { get { - var stream = VideoStream; - var request = BaseRequest; + if (BaseRequest.Static) + { + return VideoStream == null ? null : VideoStream.Level; + } - return !string.IsNullOrEmpty(request.Level) && !request.Static - ? double.Parse(request.Level, CultureInfo.InvariantCulture) - : stream == null ? null : stream.Level; + var level = GetRequestedLevel(ActualOutputVideoCodec); + double result; + if (!string.IsNullOrWhiteSpace(level) && double.TryParse(level, NumberStyles.Any, CultureInfo.InvariantCulture, out result)) + { + return result; + } + + return null; } } @@ -348,8 +445,12 @@ namespace MediaBrowser.Controller.MediaEncoding { get { - var stream = VideoStream; - return stream == null || !BaseRequest.Static ? null : stream.RefFrames; + if (BaseRequest.Static) + { + return VideoStream == null ? null : VideoStream.RefFrames; + } + + return null; } } @@ -404,10 +505,18 @@ namespace MediaBrowser.Controller.MediaEncoding { get { - var stream = VideoStream; - return !string.IsNullOrEmpty(BaseRequest.Profile) && !BaseRequest.Static - ? BaseRequest.Profile - : stream == null ? null : stream.Profile; + if (BaseRequest.Static) + { + return VideoStream == null ? null : VideoStream.Profile; + } + + var requestedProfile = GetRequestedProfiles(ActualOutputVideoCodec).FirstOrDefault(); + if (!string.IsNullOrWhiteSpace(requestedProfile)) + { + return requestedProfile; + } + + return null; } } @@ -435,6 +544,28 @@ namespace MediaBrowser.Controller.MediaEncoding } } + public string ActualOutputVideoCodec + { + get + { + var codec = OutputVideoCodec; + + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var stream = VideoStream; + + if (stream != null) + { + return stream.Codec; + } + + return null; + } + + return codec; + } + } + public bool? IsTargetInterlaced { get @@ -444,7 +575,7 @@ namespace MediaBrowser.Controller.MediaEncoding return VideoStream == null ? (bool?)null : VideoStream.IsInterlaced; } - if (DeInterlace) + if (DeInterlace(ActualOutputVideoCodec, true)) { return false; } diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs b/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs index 5fc93bf38..bac2a6e65 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs @@ -1,4 +1,7 @@ -using System.Globalization; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Services; @@ -37,18 +40,16 @@ namespace MediaBrowser.Controller.MediaEncoding MaxWidth = info.MaxWidth; MaxHeight = info.MaxHeight; MaxFramerate = info.MaxFramerate; - Profile = info.VideoProfile; ItemId = info.ItemId; MediaSourceId = info.MediaSourceId; - AudioCodec = info.TargetAudioCodec; + AudioCodec = info.TargetAudioCodec.FirstOrDefault(); MaxAudioChannels = info.MaxAudioChannels; AudioBitRate = info.AudioBitrate; AudioSampleRate = info.TargetAudioSampleRate; DeviceProfile = deviceProfile; - VideoCodec = info.TargetVideoCodec; + VideoCodec = info.TargetVideoCodec.FirstOrDefault(); VideoBitRate = info.VideoBitrate; AudioStreamIndex = info.AudioStreamIndex; - MaxRefFrames = info.MaxRefFrames; MaxVideoBitDepth = info.MaxVideoBitDepth; SubtitleMethod = info.SubtitleDeliveryMethod; Context = info.Context; @@ -58,11 +59,7 @@ namespace MediaBrowser.Controller.MediaEncoding { SubtitleStreamIndex = info.SubtitleStreamIndex; } - - if (info.VideoLevel.HasValue) - { - Level = info.VideoLevel.Value.ToString(_usCulture); - } + StreamOptions = info.StreamOptions; } } @@ -224,12 +221,41 @@ namespace MediaBrowser.Controller.MediaEncoding public EncodingContext Context { get; set; } + public void SetOption(string qualifier, string name, string value) + { + SetOption(qualifier + "-" + name, value); + } + + public Dictionary<string, string> StreamOptions { get; set; } + + public void SetOption(string name, string value) + { + StreamOptions[name] = value; + } + + public string GetOption(string qualifier, string name) + { + return GetOption(qualifier + "-" + name); + } + + public string GetOption(string name) + { + string value; + if (StreamOptions.TryGetValue(name, out value)) + { + return value; + } + + return null; + } + public BaseEncodingJobOptions() { EnableAutoStreamCopy = true; AllowVideoStreamCopy = true; AllowAudioStreamCopy = true; Context = EncodingContext.Streaming; + StreamOptions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); } } } |
