Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for anamorphic transcoding to upscale and maintain available … #1250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions MediaBrowser.Api/Playback/BaseStreamingService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using CommonIO;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
Expand All @@ -23,7 +22,6 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CommonIO;

namespace MediaBrowser.Api.Playback
{
Expand Down Expand Up @@ -518,18 +516,20 @@ protected string GetOutputSizeParam(StreamState state,
filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", widthParam, heightParam));
}

// If Max dimensions were supplied, for width selects lowest even number between input width and width req size and selects lowest even number from in width*display aspect and requested size
// Fit the video into the specified bounds while ensuring upscaling only occurs in one dimension
else if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
{
var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);

// picks the highest resolution that will fit within the max/min boundaries that only causes upscaling in one or other dimension
filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,min({0}\\,{1}*dar))/2)*2:trunc(min(max(iw/dar\\,ih)\\,min({0}/dar\\,{1}))/2)*2", maxWidthParam, maxHeightParam));
}

// If a fixed width was requested
else if (request.Width.HasValue)
{
// currently assumes Width < MaxWidth and ignores any MaxHeight
var widthParam = request.Width.Value.ToString(UsCulture);

filters.Add(string.Format("scale={0}:trunc(ow/a/2)*2", widthParam));
Expand All @@ -538,6 +538,7 @@ protected string GetOutputSizeParam(StreamState state,
// If a fixed height was requested
else if (request.Height.HasValue)
{
// currently assume Height < MaxHeight and ignores and MaxWidth
var heightParam = request.Height.Value.ToString(UsCulture);

filters.Add(string.Format("scale=trunc(oh*a*2)/2:{0}", heightParam));
Expand All @@ -548,15 +549,15 @@ protected string GetOutputSizeParam(StreamState state,
{
var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);

filters.Add(string.Format("scale=min(iw\\,{0}):trunc(ow/dar/2)*2", maxWidthParam));
filters.Add(string.Format("scale=min(max(iw\\,ih*dar)\\,{0}):trunc(ow/dar/2)*2", maxWidthParam));
}

// If a max height was requested
else if (request.MaxHeight.HasValue)
{
var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);

filters.Add(string.Format("scale=trunc(oh*a/2)*2:min(ih\\,{0})", maxHeightParam));
filters.Add(string.Format("scale=trunc(oh*a/2)*2:min(max(iw/dar\\,ih)\\,{0})", maxHeightParam));
}

if (string.Equals(outputVideoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -1167,6 +1168,9 @@ private void ParseLogLine(string line, TranscodingJob transcodingJob, StreamStat
isUpscaling = true;
}

if (videoStream.IsAnamorphic.HasValue && videoStream.IsAnamorphic.Value)
isUpscaling = true;

// Don't allow bitrate increases unless upscaling
if (!isUpscaling)
{
Expand Down
41 changes: 39 additions & 2 deletions MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,45 @@ private MediaStream GetMediaStream(bool isAudio, MediaStreamInfo streamInfo, Med
// string.Equals(stream.AspectRatio, "2.35:1", StringComparison.OrdinalIgnoreCase) ||
// string.Equals(stream.AspectRatio, "2.40:1", StringComparison.OrdinalIgnoreCase);

// http://stackoverflow.com/questions/17353387/how-to-detect-anamorphic-video-with-ffprobe
stream.IsAnamorphic = string.Equals(streamInfo.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase);
if (string.Equals(streamInfo.sample_aspect_ratio, "1:1", StringComparison.OrdinalIgnoreCase))
{
stream.IsAnamorphic = false;
}
else if (!((string.IsNullOrWhiteSpace(streamInfo.sample_aspect_ratio) || string.Equals(streamInfo.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase))))
{
stream.IsAnamorphic = true;
}
else if (string.IsNullOrWhiteSpace(streamInfo.display_aspect_ratio) || string.Equals(streamInfo.display_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase))
{
stream.IsAnamorphic = false;
}
else
{
var ratioParts = streamInfo.display_aspect_ratio.Split(':');
if (ratioParts.Length != 2)
{
stream.IsAnamorphic = false;
}
else
{
int ratio0;
int ratio1;
if (!Int32.TryParse(ratioParts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out ratio0))
{
stream.IsAnamorphic = false;
}
else if (!Int32.TryParse(ratioParts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out ratio1))
{
stream.IsAnamorphic = false;
}
else
{
// allow up to 2% difference between stated display aspect and the calculated ratio
stream.IsAnamorphic = (Math.Abs((streamInfo.width * ratio1) - (streamInfo.height * ratio0)) * 100.0f) / Math.Max((streamInfo.height * ratio0), (streamInfo.width * ratio1)) > 2;
}
}
}


if (streamInfo.refs > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ private bool IsFileLocked(string path)

try
{
using (_fileSystem.GetFileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
using (_fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
if (_updateTimer != null)
{
Expand Down