/*
 *      Copyright (C) 2010-2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#version 100

precision mediump float;

uniform sampler2D m_sampY;
uniform sampler2D m_sampU;
uniform sampler2D m_sampV;
varying vec2 m_cordY;
varying vec2 m_cordU;
varying vec2 m_cordV;
uniform vec2 m_step;
uniform mat4 m_yuvmat;
uniform mat3 m_primMat;
uniform float m_gammaDstInv;
uniform float m_gammaSrc;
uniform float m_toneP1;
uniform float m_luminance;
uniform vec3 m_coefsDst;
uniform float m_alpha;

void main()
{
  vec4 rgb;
  vec4 yuv;

#if defined(XBMC_YV12) || defined(XBMC_NV12)

  yuv = vec4(texture2D(m_sampY, m_cordY).r,
             texture2D(m_sampU, m_cordU).g,
             texture2D(m_sampV, m_cordV).a,
             1.0);

#elif defined(XBMC_YV12_HI)

  yuv = vec4(texture2D(m_sampY, m_cordY).r,
             texture2D(m_sampU, m_cordU).r,
             texture2D(m_sampV, m_cordV).r,
             1.0);

#elif defined(XBMC_NV12_RRG)

  yuv = vec4(texture2D(m_sampY, m_cordY).r,
             texture2D(m_sampU, m_cordU).r,
             texture2D(m_sampV, m_cordV).g,
             1.0);

#elif defined(XBMC_YUY2) || defined(XBMC_UYVY)

  vec2 stepxy = m_step;
  vec2 pos    = m_cordY;
  pos         = vec2(pos.x - stepxy.x * 0.25, pos.y);
  vec2 f      = fract(pos / stepxy);

  //y axis will be correctly interpolated by opengl
  //x axis will not, so we grab two pixels at the center of two columns and interpolate ourselves
  vec4 c1 = texture2D(m_sampY, vec2(pos.x + (0.5 - f.x) * stepxy.x, pos.y));
  vec4 c2 = texture2D(m_sampY, vec2(pos.x + (1.5 - f.x) * stepxy.x, pos.y));

  /* each pixel has two Y subpixels and one UV subpixel
     YUV  Y  YUV
     check if we're left or right of the middle Y subpixel and interpolate accordingly*/
#ifdef XBMC_YUY2 //BGRA = YUYV
  float leftY   = mix(c1.b, c1.r, f.x * 2.0);
  float rightY  = mix(c1.r, c2.b, f.x * 2.0 - 1.0);
  vec2  outUV   = mix(c1.ga, c2.ga, f.x);
#else //BGRA = UYVY
  float leftY   = mix(c1.g, c1.a, f.x * 2.0);
  float rightY  = mix(c1.a, c2.g, f.x * 2.0 - 1.0);
  vec2  outUV   = mix(c1.br, c2.br, f.x);
#endif //XBMC_YUY2

  float outY = mix(leftY, rightY, step(0.5, f.x));

  yuv = vec4(outY, outUV, 1.0);

#elif defined(XBMC_Y210)

  // Packed 4:2:2 in 16-bit channels (covers Y210 10-bit, Y212 12-bit, Y216
  // 16-bit). Mesa imports DRM_FORMAT_Y210/Y212/Y216 as GL_RGBA16; channels
  // map RGBA -> Y0, Cb, Y1, Cr. Each texel spans two luma samples sharing
  // one chroma pair, like YUY2 but in wider channels.
  vec2 stepxy = m_step;
  vec2 pos    = m_cordY;
  pos         = vec2(pos.x - stepxy.x * 0.25, pos.y);
  vec2 f      = fract(pos / stepxy);

  vec4 c1 = texture2D(m_sampY, vec2(pos.x + (0.5 - f.x) * stepxy.x, pos.y));
  vec4 c2 = texture2D(m_sampY, vec2(pos.x + (1.5 - f.x) * stepxy.x, pos.y));

  float leftY  = mix(c1.r, c1.b, f.x * 2.0);
  float rightY = mix(c1.b, c2.r, f.x * 2.0 - 1.0);
  vec2  outUV  = mix(c1.ga, c2.ga, f.x);
  float outY   = mix(leftY, rightY, step(0.5, f.x));

  yuv = vec4(outY, outUV, 1.0);

#elif defined(XBMC_AYUV)

  // Packed 4:4:4 in 8-bit channels (AYUV, XYUV). DRM_FORMAT_AYUV stores
  // bytes V, U, Y, A in memory; EGL imports as GL_RGBA8 so RGBA = V, U, Y,
  // alpha. Shader sees one full-resolution YUV sample per texel.
  vec4 t = texture2D(m_sampY, m_cordY);
  yuv = vec4(t.b, t.g, t.r, 1.0);

#elif defined(XBMC_Y410)

  // Packed 4:4:4 10-bit in a 32-bit word (DRM_FORMAT_Y410). EGL imports as
  // GL_RGB10_A2; the format's standard bit layout puts A2 V10 U10 Y10
  // little-endian, which surfaces as RGBA = U, Y, V, A after import.
  vec4 t = texture2D(m_sampY, m_cordY);
  yuv = vec4(t.g, t.r, t.b, 1.0);

#elif defined(XBMC_Y412)

  // Packed 4:4:4 in 16-bit channels (Y412 12-bit, Y416 16-bit). EGL imports
  // as GL_RGBA16 with channel order RGBA = U, Y, V, A.
  vec4 t = texture2D(m_sampY, m_cordY);
  yuv = vec4(t.g, t.r, t.b, 1.0);

#endif

  rgb = m_yuvmat * yuv;
  rgb.a = m_alpha;

#if defined(XBMC_COL_CONVERSION)
  rgb.rgb = pow(max(vec3(0), rgb.rgb), vec3(m_gammaSrc));
  rgb.rgb = max(vec3(0), m_primMat * rgb.rgb);
  rgb.rgb = pow(rgb.rgb, vec3(m_gammaDstInv));

#if defined(KODI_TONE_MAPPING_REINHARD)
  float luma = dot(rgb.rgb, m_coefsDst);
  rgb.rgb *= reinhard(luma) / luma;

#elif defined(KODI_TONE_MAPPING_ACES)
  rgb.rgb = inversePQ(rgb.rgb);
  rgb.rgb *= (10000.0 / m_luminance) * (2.0 / m_toneP1);
  rgb.rgb = aces(rgb.rgb);
  rgb.rgb *= (1.24 / m_toneP1);
  rgb.rgb = pow(rgb.rgb, vec3(0.27));

#elif defined(KODI_TONE_MAPPING_HABLE)
  rgb.rgb = inversePQ(rgb.rgb);
  rgb.rgb *= m_toneP1;
  float wp = m_luminance / 100.0;
  rgb.rgb = hable(rgb.rgb * wp) / hable(vec3(wp));
  rgb.rgb = pow(rgb.rgb, vec3(1.0 / 2.2));
#endif

#endif

  gl_FragColor = rgb;
}

