Skip to content

Commit

Permalink
Merge pull request #490 from opentok/VIDCS-1598
Browse files Browse the repository at this point in the history
Android Sample: vp9 stride value fix
  • Loading branch information
IGitGotIt authored Jan 9, 2024
2 parents c2574b3 + 61e4eb7 commit be9fac2
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,49 +202,53 @@ void setupTextures(Frame frame) {
textureHeight = frame.getHeight();
}

void GlTexSubImage2D(int width, int height, int stride,
ByteBuffer buf) {
if (stride == width) {
// Yay! We can upload the entire plane in a single GL call.
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
buf);
} else {
for (int row = 0; row < height; ++row) {
buf.position( row * stride);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, row, width,
1, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
buf);
}
}
}

void updateTextures(Frame frame) {
int width = frame.getWidth();
int height = frame.getHeight();
int half_width = (width + 1) >> 1;
int half_height = (height + 1) >> 1;
int y_size = width * height;
int uv_size = half_width * half_height;

ByteBuffer bb = frame.getBuffer();
// If we are reusing this frame, make sure we reset position and
// limit
bb.clear();

if (bb.remaining() == y_size + uv_size * 2) {
bb.position(0);

GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
bb);

bb.position(y_size);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[1]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
half_width, half_height, GLES20.GL_LUMINANCE,
GLES20.GL_UNSIGNED_BYTE, bb);

bb.position(y_size + uv_size);
GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[2]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
half_width, half_height, GLES20.GL_LUMINANCE,
GLES20.GL_UNSIGNED_BYTE, bb);
} else {
//check if buffer data is correctly sized.
if (bb.remaining() != frame.getYplaneSize() + frame.getUVplaneSize() * 2) {
textureWidth = 0;
textureHeight = 0;
return;
}

bb.position(0);
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
GlTexSubImage2D(width, height, frame.getYstride(), frame.getYplane());

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[1]);
GlTexSubImage2D(half_width, half_height, frame.getUvStride(), frame.getUplane());

GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[2]);
GlTexSubImage2D(half_width, half_height, frame.getUvStride(), frame.getVplane());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static class MyRenderer implements GLSurfaceView.Renderer {
+ " v=texture2D(Vtex,vec2(nx,ny)).r;\n"

+ " y=1.0-1.1643*(y-0.0625);\n" // this line produces the inverted effect
// + " y=1.1643*(y-0.0625);\n" // use this line instead if you want to have normal colors
// + " y=1.1643*(y-0.0625);\n" // use this line instead if you want to have normal colors

+ " u=u-0.5;\n" + " v=v-0.5;\n" + " r=y+1.5958*v;\n"
+ " g=y-0.39173*u-0.81290*v;\n" + " b=y+2.017*u;\n"
Expand Down Expand Up @@ -202,49 +202,52 @@ void setupTextures(Frame frame) {
textureHeight = frame.getHeight();
}

void GlTexSubImage2D(int width, int height, int stride,
ByteBuffer buf) {
if (stride == width) {
// Yay! We can upload the entire plane in a single GL call.
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
buf);
} else {
for (int row = 0; row < height; ++row) {
buf.position( row * stride);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, row, width,
1, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
buf);
}
}
}

void updateTextures(Frame frame) {
int width = frame.getWidth();
int height = frame.getHeight();
int half_width = (width + 1) >> 1;
int half_height = (height + 1) >> 1;
int y_size = width * height;
int uv_size = half_width * half_height;

ByteBuffer bb = frame.getBuffer();
// If we are reusing this frame, make sure we reset position and
// limit
bb.clear();

if (bb.remaining() == y_size + uv_size * 2) {
bb.position(0);

GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
bb);

bb.position(y_size);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[1]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
half_width, half_height, GLES20.GL_LUMINANCE,
GLES20.GL_UNSIGNED_BYTE, bb);

bb.position(y_size + uv_size);
GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[2]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
half_width, half_height, GLES20.GL_LUMINANCE,
GLES20.GL_UNSIGNED_BYTE, bb);
} else {
//check if buffer data is correctly sized.
if (bb.remaining() != frame.getYplaneSize() + frame.getUVplaneSize() * 2) {
textureWidth = 0;
textureHeight = 0;
return;
}
bb.position(0);
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
GlTexSubImage2D(width, height, frame.getYstride(), frame.getYplane());

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[1]);
GlTexSubImage2D(half_width, half_height, frame.getUvStride(), frame.getUplane());

GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[2]);
GlTexSubImage2D(half_width, half_height, frame.getUvStride(), frame.getVplane());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,49 +198,53 @@ void setupTextures(Frame frame) {
textureHeight = frame.getHeight();
}

void GlTexSubImage2D(int width, int height, int stride,
ByteBuffer buf) {
if (stride == width) {
// Yay! We can upload the entire plane in a single GL call.
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
buf);
} else {
for (int row = 0; row < height; ++row) {
buf.position( row * stride);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, row, width,
1, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
buf);
}
}
}

void updateTextures(Frame frame) {
int width = frame.getWidth();
int height = frame.getHeight();
int half_width = (width + 1) >> 1;
int half_height = (height + 1) >> 1;
int y_size = width * height;
int uv_size = half_width * half_height;

ByteBuffer bb = frame.getBuffer();
// If we are reusing this frame, make sure we reset position and
// limit
bb.clear();

if (bb.remaining() == y_size + uv_size * 2) {
bb.position(0);

GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
bb);

bb.position(y_size);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[1]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
half_width, half_height, GLES20.GL_LUMINANCE,
GLES20.GL_UNSIGNED_BYTE, bb);

bb.position(y_size + uv_size);
GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[2]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
half_width, half_height, GLES20.GL_LUMINANCE,
GLES20.GL_UNSIGNED_BYTE, bb);
} else {
//check if buffer data is correctly sized.
if (bb.remaining() != frame.getYplaneSize() + frame.getUVplaneSize() * 2) {
textureWidth = 0;
textureHeight = 0;
return;
}

bb.position(0);
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
GlTexSubImage2D(width, height, frame.getYstride(), frame.getYplane());

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[1]);
GlTexSubImage2D(half_width, half_height, frame.getUvStride(), frame.getUplane());

GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[2]);
GlTexSubImage2D(half_width, half_height, frame.getUvStride(), frame.getVplane());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,44 +198,53 @@ void setupTextures(Frame frame) {
textureHeight = frame.getHeight();
}

void GlTexSubImage2D(int width, int height, int stride,
ByteBuffer buf) {
if (stride == width) {
// Yay! We can upload the entire plane in a single GL call.
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
buf);
} else {
for (int row = 0; row < height; ++row) {
buf.position( row * stride);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, row, width,
1, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
buf);
}
}
}

void updateTextures(Frame frame) {
int width = frame.getWidth();
int height = frame.getHeight();
int half_width = (width + 1) >> 1;
int half_height = (height + 1) >> 1;
int y_size = width * height;
int uv_size = half_width * half_height;

ByteBuffer bb = frame.getBuffer();
// If we are reusing this frame, make sure we reset position and
// limit
bb.clear();

if (bb.remaining() == y_size + uv_size * 2) {
bb.position(0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
bb);

bb.position(y_size);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[1]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
half_width, half_height, GLES20.GL_LUMINANCE,
GLES20.GL_UNSIGNED_BYTE, bb);

bb.position(y_size + uv_size);
GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[2]);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
half_width, half_height, GLES20.GL_LUMINANCE,
GLES20.GL_UNSIGNED_BYTE, bb);
} else {
//check if buffer data is correctly sized.
if (bb.remaining() != frame.getYplaneSize() + frame.getUVplaneSize() * 2) {
textureWidth = 0;
textureHeight = 0;
return;
}

bb.position(0);
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0]);
GlTexSubImage2D(width, height, frame.getYstride(), frame.getYplane());

GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[1]);
GlTexSubImage2D(half_width, half_height, frame.getUvStride(), frame.getUplane());

GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[2]);
GlTexSubImage2D(half_width, half_height, frame.getUvStride(), frame.getVplane());
}

@Override
Expand Down
Loading

0 comments on commit be9fac2

Please sign in to comment.