Skip to content

Commit

Permalink
Merge pull request #6403 from frenzibyte/fix-videos
Browse files Browse the repository at this point in the history
Prefer HDTV / Rec. 709 colorspace for videos with HD resolution and unspecified colorspace
  • Loading branch information
peppy authored Dec 9, 2024
2 parents 83ea26d + 7f436d5 commit c3439a0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
2 changes: 2 additions & 0 deletions osu-framework.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BNG/@EntryIndexedValue">BNG</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WM/@EntryIndexedValue">WM</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SDTV/@EntryIndexedValue">SDTV</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=HDTV/@EntryIndexedValue">HDTV</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/ApplyAutoDetectedRules/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&#xD;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added osu.Framework.Tests/Resources/Videos/h264-hd.mp4
Binary file not shown.
57 changes: 57 additions & 0 deletions osu.Framework.Tests/Visual/Sprites/TestSceneVideo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.Graphics.Video;
using osu.Framework.IO.Stores;
using osu.Framework.Timing;
using osuTK;

namespace osu.Framework.Tests.Visual.Sprites
{
Expand All @@ -32,6 +35,9 @@ public partial class TestSceneVideo : FrameworkTestScene
[Resolved]
private FrameworkConfigManager config { get; set; }

[Resolved]
private TextureStore textures { get; set; }

private static readonly string[] file_formats =
{
"h264.mp4",
Expand Down Expand Up @@ -239,6 +245,57 @@ public void TestShader()
AddToggleStep("Toggle rounding", v => video.Rounded = v);
}

[Test]
public void TestUnspecifiedColorspace()
{
AddStep("Reset clock", () =>
{
clock.CurrentTime = 0;
didDecode = false;
});
AddStep("load videos", () =>
{
videoContainer.Child = new FillFlowContainer
{
Scale = new Vector2(0.75f),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Children = new[]
{
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
new SpriteText { Text = "SDTV / Rec. 601" },
new TestVideo(videoStore.GetStream("h264.mp4")),
Empty().With(d => d.Height = 10),
new Sprite { Texture = textures.Get("h264-screenshot.png", WrapMode.ClampToEdge, WrapMode.ClampToEdge), Scale = new Vector2(2f) },
new SpriteText { Text = "Expected" },
}
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
new SpriteText { Text = "HDTV / Rec. 709" },
new TestVideo(videoStore.GetStream("h264-hd.mp4")) { Scale = new Vector2(270f / 576f) },
Empty().With(d => d.Height = 10),
new Sprite { Texture = textures.Get("h264-hd-screenshot.png", WrapMode.ClampToEdge, WrapMode.ClampToEdge), Scale = new Vector2(270f / 576f * 2f) },
new SpriteText { Text = "Expected" },
}
},
},
};
});
AddStep("Reset clock", () => clock.CurrentTime = 0);
}

private int currentSecond;
private int fps;
private int lastFramesProcessed;
Expand Down
30 changes: 17 additions & 13 deletions osu.Framework/Graphics/Video/VideoDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,25 @@ public Matrix3 GetConversionMatrix()
if (codecContext == null)
return Matrix3.Zero;

switch (codecContext->colorspace)
// this matches QuickTime Player's choice of colour spaces:
// - any video with width < 704 and height < 576 uses the SDTV colorspace.
// - any video with width >= 704 and height >= 576 uses the HDTV colorspace.
// (704x576 in particular has a special colour space, but we don't worry about it).
bool unspecifiedUsesHDTV = codecContext->width >= 704 || codecContext->height >= 576;

if (codecContext->colorspace == AVColorSpace.AVCOL_SPC_BT709
|| (codecContext->colorspace == AVColorSpace.AVCOL_SPC_UNSPECIFIED && unspecifiedUsesHDTV))
{
case AVColorSpace.AVCOL_SPC_BT709:
return new Matrix3(1.164f, 1.164f, 1.164f,
0.000f, -0.213f, 2.112f,
1.793f, -0.533f, 0.000f);

case AVColorSpace.AVCOL_SPC_UNSPECIFIED:
case AVColorSpace.AVCOL_SPC_SMPTE170M:
case AVColorSpace.AVCOL_SPC_SMPTE240M:
default:
return new Matrix3(1.164f, 1.164f, 1.164f,
0.000f, -0.392f, 2.017f,
1.596f, -0.813f, 0.000f);
// matrix coefficients for HDTV / Rec. 709 colorspace.
return new Matrix3(1.164f, 1.164f, 1.164f,
0.000f, -0.213f, 2.112f,
1.793f, -0.533f, 0.000f);
}

// matrix coefficients for SDTV / Rec. 601 colorspace.
return new Matrix3(1.164f, 1.164f, 1.164f,
0.000f, -0.392f, 2.017f,
1.596f, -0.813f, 0.000f);
}

[MonoPInvokeCallback(typeof(avio_alloc_context_read_packet))]
Expand Down

0 comments on commit c3439a0

Please sign in to comment.