Created
August 26, 2016 05:14
-
-
Save ousttrue/0a9563d4ee769ca7d01d323a77ad93e1 to your computer and use it in GitHub Desktop.
YUV decoder. http://www.loopsketch.com/blog/?p=350
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Texture2D diffuseTexture; | |
SamplerState diffuseTextureSampler; | |
struct VS_IN | |
{ | |
float4 Position: POSITION; | |
float4 Color: COLOR; | |
float2 Tex: TEXCOORD0; | |
}; | |
struct VS_OUT | |
{ | |
float4 Position: SV_POSITION; | |
float4 Color: COLOR; | |
float2 Tex: TEXCOORD0; | |
}; | |
typedef VS_OUT PS_IN; | |
cbuffer cb0 | |
{ | |
row_major matrix ModelMatrix; | |
} | |
cbuffer cb1 | |
{ | |
row_major matrix ViewMatrix; | |
} | |
cbuffer cb2 | |
{ | |
row_major matrix ProjectionMatrix; | |
}; | |
VS_OUT vsMain(VS_IN input) | |
{ | |
VS_OUT Output; | |
Output.Position = input.Position; | |
Output.Color = input.Color; | |
Output.Tex = input.Tex; | |
return Output; | |
} | |
float4 getYuvEven(float2 tex) | |
{ | |
float Y = diffuseTexture.Sample(diffuseTextureSampler | |
, float2(tex.x, tex.y * 2 / 3)).r; | |
float U = diffuseTexture.Sample(diffuseTextureSampler | |
, float2(tex.x / 2, (4 + tex.y) / 6)).r - 0.5; | |
float V = diffuseTexture.Sample(diffuseTextureSampler | |
, float2(tex.x / 2, (5 + tex.y) / 6)).r - 0.5; | |
return float4(Y, U, V, 1); | |
} | |
float4 getYuvOdd(float2 tex) | |
{ | |
//return float4(0, 0, 0, 0); | |
float Y = diffuseTexture.Sample(diffuseTextureSampler | |
, float2(tex.x, tex.y * 2 / 3)).r; | |
float U = diffuseTexture.Sample(diffuseTextureSampler | |
, float2((1+tex.x) / 2, (4 + tex.y) / 6)).r - 0.5; | |
float V = diffuseTexture.Sample(diffuseTextureSampler | |
, float2((1+tex.x) / 2, (5 + tex.y) / 6)).r - 0.5; | |
return float4(Y, U, V, 1); | |
} | |
float4 psMain(PS_IN input) : SV_TARGET | |
{ | |
uint w, h; | |
diffuseTexture.GetDimensions(w, h); | |
uint2 P = input.Tex * uint2(w, h); | |
float4 yuv = P.y % 2 == 0 ? getYuvEven(input.Tex) : getYuvOdd(input.Tex); | |
const float4x4 YUVMatrix = { | |
1, 0, 1.402, 0, | |
1, -0.34414, -0.71414, 0, | |
1, 1.772, 0, 0, | |
1, 1, 1, 1 }; | |
return (mul(YUVMatrix, yuv) - 0.0627f) * 1.164f; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment