Skip to content
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
98 changes: 88 additions & 10 deletions android/src/main/cpp/iris_rtc_rendering_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <jni.h>
#include <memory>
#include <vector>
#include <cstring>

namespace agora {
namespace iris {
Expand Down Expand Up @@ -508,6 +509,11 @@ class YUVRendering final : public RenderingOp {
uTextureLoc_ = glGetUniformLocation(program, "uTexture");
vTextureLoc_ = glGetUniformLocation(program, "vTexture");

// Get locations for ColorSpace uniforms
colorMatrixLoc_ = glGetUniformLocation(program, "uColorMatrix");
rangeLoc_ = glGetUniformLocation(program, "uRange");
transferLoc_ = glGetUniformLocation(program, "uTransfer");

glGenTextures(3, texs_);
CHECK_GL_ERROR()
}
Expand Down Expand Up @@ -618,6 +624,52 @@ class YUVRendering final : public RenderingOp {
GL_LUMINANCE, GL_UNSIGNED_BYTE, vBuffer);
CHECK_GL_ERROR()

// Update color space uniforms if available
if (colorMatrixLoc_ != -1) {
float bt709[9] = {
1.0f, 1.0f, 1.0f,
0.0f, -0.39465f, 2.03211f,
1.13983f, -0.58060f, 0.0f
};
float bt2020[9] = {
1.0f, 1.0f, 1.0f,
0.0f, -0.16455f, 1.88140f,
1.47460f, -0.57135f, 0.0f
};
float smpte170m[9] = {
1.0f, 1.0f, 1.0f,
0.0f, -0.39173f, 2.01700f,
1.59580f, -0.81290f, 0.0f
};

float mat[9];
int matrixId = (int)video_frame->colorSpace.matrix;
switch (matrixId) {
case agora::media::base::ColorSpace::MATRIXID_BT2020_NCL:
memcpy(mat, bt2020, sizeof(mat));
break;
case agora::media::base::ColorSpace::MATRIXID_SMPTE170M:
memcpy(mat, smpte170m, sizeof(mat));
break;
case agora::media::base::ColorSpace::MATRIXID_BT709:
default:
memcpy(mat, bt709, sizeof(mat));
break;
}
glUniformMatrix3fv(colorMatrixLoc_, 1, GL_FALSE, mat);
}

// 2. Set Range
if (rangeLoc_ != -1) {
int rangeVal = (video_frame->colorSpace.range == agora::media::base::ColorSpace::RANGEID_LIMITED) ? 1 : 0;
glUniform1i(rangeLoc_, rangeVal);
}

// 3. Set Transfer Function
if (transferLoc_ != -1) {
glUniform1i(transferLoc_, (int)video_frame->colorSpace.transfer);
}

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CHECK_GL_ERROR()

Expand All @@ -642,8 +694,8 @@ class YUVRendering final : public RenderingOp {
"attribute vec2 aTextCoord;\n"
"varying vec2 vTextCoord;\n"
"void main() {\n"
" vTextCoord = vec2(aTextCoord.x, 1.0 - aTextCoord.y);\n"
" gl_Position = aPosition;\n"
" gl_Position = aPosition;\n"
" vTextCoord = aTextCoord;\n"
"}\n";

const char *frag_shader_yuv_ =
Expand All @@ -652,15 +704,38 @@ class YUVRendering final : public RenderingOp {
"uniform sampler2D yTexture;\n"
"uniform sampler2D uTexture;\n"
"uniform sampler2D vTexture;\n"
"uniform mat3 uColorMatrix;\n"
"uniform int uRange;\n"
"uniform int uTransfer;\n"
"\n"
"vec3 applyTransfer(vec3 c, int transferId) {\n"
" if (transferId == 8) { return c; }\n" // LINEAR
" else if (transferId == 4) { return pow(c, vec3(1.0/2.2)); }\n" // GAMMA22
" else if (transferId == 5) { return pow(c, vec3(1.0/2.8)); }\n" // GAMMA28
" else if (transferId == 1) { return pow(c, vec3(1.0/2.4)); }\n" // BT709 approx
" return c;\n"
"}\n"
"\n"
"void main() {\n"
" vec3 yuv;\n"
" vec3 rgb;\n"
" yuv.r = texture2D(yTexture, vTextCoord).r;\n"
" yuv.g = texture2D(uTexture, vTextCoord).r - 0.5;\n"
" yuv.b = texture2D(vTexture, vTextCoord).r - 0.5;\n"
" rgb = mat3(1.0, 1.0, 1.0, 0.0, -0.39465, 2.03211, 1.13983, "
"-0.58060, 0.0) * yuv;\n"
" gl_FragColor = vec4(rgb, 1.0);\n"
" float y = texture2D(yTexture, vTextCoord).r;\n"
" float u = texture2D(uTexture, vTextCoord).r - 0.5;\n"
" float v = texture2D(vTexture, vTextCoord).r - 0.5;\n"
"\n"
" // 1. Range Conversion\n"
" if (uRange == 1) { // Limited Range to Full Range\n"
" y = (y - 16.0/255.0) * (255.0/219.0);\n"
" u = u * (224.0/255.0);\n"
" v = v * (224.0/255.0);\n"
" }\n"
"\n"
" // 2. YUV to RGB Conversion with dynamic matrix\n"
" vec3 yuv = vec3(y, u, v);\n"
" vec3 rgb = uColorMatrix * yuv;\n"
"\n"
" // 3. Apply Transfer Function (Gamma Correction)\n"
" rgb = applyTransfer(rgb, uTransfer);\n"
"\n"
" gl_FragColor = vec4(clamp(rgb, 0.0, 1.0), 1.0);\n"
"}\n";

// clang-format off
Expand All @@ -680,6 +755,9 @@ class YUVRendering final : public RenderingOp {
GLint yTextureLoc_;
GLint uTextureLoc_;
GLint vTextureLoc_;
GLint colorMatrixLoc_ = -1;
GLint rangeLoc_ = -1;
GLint transferLoc_ = -1;

std::unique_ptr<ScopedShader> shader_;
};
Expand Down
Loading
Loading