Jump to content

IsAnamorphic not working right


paul777

Recommended Posts

paul777

I thought I'd start a new topic for this as it has wider implications that the original thread where it came up. The IsAnamorphic flag in the media info isn't picking up correctly.

 

I believe the code in MediaEncoderHelper.GetMediaStream needs to look something like this - which may be better added as a helper property on the actual streamInfo

if (string.Equals(streamInfo.sample_aspect_ratio, "1:1", StringComparison.OrdinalIgnoreCase))
	stream.IsAnamorphic = false;
else if (!((streamInfo.sample_aspect_ratio==String.Empty) || (string.Equals(streamInfo.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase))))
	stream.IsAnamorphic = true;
else if (streamInfo.display_aspect_ratio==String.Empty || string.Equals(streamInfo.display_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase))
	stream.IsAnamorphic = false;
else
{
	string[] ratioParts = streamInfo.display_aspect_ratio.Split(':');
	if (ratioParts.Length != 2)
		stream.IsAnamorphic = false;
	else
	{
		int ratio0;
		int ratio1;
		if (!Int32.TryParse(ratioParts[0],out ratio0))
			stream.IsAnamorphic = false;
		else if (!Int32.TryParse(ratioParts[1],out ratio1))
			stream.IsAnamorphic = false;
		else
			stream.IsAnamorphic = ((streamInfo.width * ratio1) != (stream.Height * ratio0));
	}
}

with this in place, if I force a refresh the Anamorphic flag looks correct across my library

Edited by paul777
Link to comment
Share on other sites

paul777

My ultimate goal is trying to get a DLNA which will transcode Anamorphic videos as many TVs don't support anamorphic streams properly. I have added this line to the video codec profiles

<ProfileCondition condition="NotEquals" property="IsAnamorphic" 
value="true" isRequired="false" />

and it is now performing transcoding, the only problem is that the output is still anamorphic. I have attached the transcode log file.

 

Can anyone tell me how to force the transcode to output non-anamorphic files?

 

 

 

thanks

Paul

transcode-587df496-05cb-4638-8796-c912b3b0384e.txt

Edited by paul777
Link to comment
Share on other sites

paul777

If I force a lower bitrate for the dlna profile the transcoding then produces non-anamorphic streams, the 2 command lines for ffmpeg are here

 

1. Still produces anamorphic stream

C:\Users\mceuser\AppData\Roaming\MediaBrowser-Server\ffmpeg\20150110\ffmpeg.exe 
-fflags +genpts -i file:"\\lounge\media\Movies\DVD 
AC3\21 Jump Street (2012).mp4" -map 0:0 -map 0:1 -map -0:s -codec:v:0 copy 
-bsf:v h264_mp4toannexb -map_metadata -1 -threads 0 -codec:a:0 ac3 -ac 2 -ab 
128000 -af "aresample=async=1" -y 
"D:\Mediabrowser\transcodeTemp\2eb014b981879eec95ad881968a96741.ts"

2. Produces low quality non-anamorphic stream due to forced low bitrate in dlna profile

C:\Users\mceuser\AppData\Roaming\MediaBrowser-Server\ffmpeg\20150110\ffmpeg.exe 
-fflags +genpts -i file:"\\lounge\media\Movies\DVD 
AC3\21 Jump Street (2012).mp4" -map 0:0 -map 0:1 -map -0:s -codec:v:0 libx264 
-force_key_frames expr:gte(t,n_forced*5) -vf 
"scale=min(iw\,426):trunc(ow/dar/2)*2" -pix_fmt yuv420p -preset superfast -crf 
23 -maxrate 122000 -bufsize 244000 -vsync vfr -level 51 -map_metadata -1 
-threads 0 -codec:a:0 ac3 -ac 2 -ab 128000 -af "aresample=async=1" -y 
"D:\Mediabrowser\transcodeTemp\eaa5a8b3e9ee5b5acd28a7daa6583711.ts"
Link to comment
Share on other sites

paul777

Ok, so I think I have answered this question MediaBrowser.Model.Dlna.StreamBuilder contains this code in ApplyTranscodingCondtions

case ProfileConditionValue.IsAnamorphic:
case ProfileConditionValue.AudioProfile:
case ProfileConditionValue.Has64BitOffsets:
case ProfileConditionValue.PacketLength:
case ProfileConditionValue.VideoTimestamp:
{
// Not supported 
yet
break;
}

so I guess we just have to wait for those to be implemented

Edited by paul777
Link to comment
Share on other sites

Good investigation. Your first post is still useful here although do you have a reference for that information? 

 

For IsAnamorphic it can currently be used as part of the direct stream vs transcoding decision making, but once it decides to transcode we haven't added anyway to configure parameters differently based on that value

Link to comment
Share on other sites

paul777

The code in my first post does seem to work well. the information has been gleaned from a lot of hunting. The basics are that ffprobe returns aspect of 0:1 when it is not specified in the source. A known non-anamorphic file will have sample apsect of 1:1. But where it isn't specified but we do have a display ratio we can calculate the sample ratio to check. One thing the code doesn't allow for is a tolerance - I have seen files with sample aspect ratios of very close to 1:1 and probably anything between 0.98 and 1.02 isn't worth correcting.

 

The issue now is that it triggers transcoding but only as a stream copy, we need to force it to actually do a transcode when we want to remove the anamophic nature of a stream.

Link to comment
Share on other sites

paul777

So I have got this to work - not sure if it is the way people would envisage but he code is in the attached zip based on a head cut downloaded on 3rd March. With these changes, the use of the below |CodecProfile condition, and a rebuild of metadata to ensure the Anamorphic flag in the media info is correct it all works as it should.

<ProfileCondition condition="NotEquals" property="IsAnamorphic" value="true" 
isRequired="true" />
Files in the attached
Api
  BaseStreamingService.cs  
  StreamRequest.cs
Model
  StreamInfo.cs  
  StreamBuilder.cs
MediaEncoding
  EncodingJobFactory.cs  
  MediaEncoder.cs
Controller
  EncodingJobOptions.cs  
  MediaEncoderHelper.cs

As an aside the way the Params string is built in StreamInfo.BuildDlnaParam and then parsed in BaseStreamingService.ParseParams seems very nasty and prone to bugs being introduced, the use of unknown index constants and creating a non-explicit tie between 2 libraries needing to be on the same version. It also took me some time to find it and figure it out. An object to wrap this that is common to both libraries with a SerializeToString and PasreFromString would probably be useful.

ChangedFiles.zip

Link to comment
Share on other sites

paul777

I have found that there is further work was needed - the above results in videos which are shrunk to fit the aspect ratio due to two issues I found

 

1) BaseStreamingService.GetOutputSizeParam needed tweeking to ensure that anamorphic videos are upscaled in one dimension to keep as much information as possible within the max/min limits allowed - previously a 720x432 2.40:0 source was scaled down to 720x300 and then the TV had to upscale to 1920x800. With the modification in the attached file the source is transcode to 1036x432 preserving all the possible quality.

 

2) BaseStreamingService.GetState calls the ResolutionNormalizer which has limits set that are aggressive when trying to allow an anamorphic video to upscale, the video I was testing with was 720x432 843kpbs that wanted upscaling to 1036x432 but was being blocked by the resolution normalizer. I removed the normalizer but if it is felt important then the limits need changing to have lower bitrate limits for each of the resolutions.The ResolutionNormalizer also currently causes the loss of the MaxHeight property which is probably not appropriate.

BaseStreamingService.zip

Link to comment
Share on other sites

  • 4 weeks later...
paul777

I have submitted the relevant changes as a pull request. The family noticed when an auto-update of the server wiped out my custom build of the server and broke it again - my 5year old asked why fireman sam was so thin today :-)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...