blob: be8d43e72c25874c0fd6e4077a65e135d743c856 [file] [log] [blame]
Magnus Jedvert577bc192016-10-19 15:29:02 +02001/*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11package org.webrtc;
12
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020013import android.graphics.Matrix;
Magnus Jedvert577bc192016-10-19 15:29:02 +020014import android.opengl.GLES20;
15import java.nio.ByteBuffer;
Sami Kalliomäkicb98b112017-10-16 11:20:26 +020016import org.webrtc.VideoFrame.I420Buffer;
17import org.webrtc.VideoFrame.TextureBuffer;
Magnus Jedvert577bc192016-10-19 15:29:02 +020018
19/**
Magnus Jedvert1d270f82018-04-16 16:28:29 +020020 * Class for converting OES textures to a YUV ByteBuffer. It can be constructed on any thread, but
21 * should only be operated from a single thread with an active EGL context.
Magnus Jedvert577bc192016-10-19 15:29:02 +020022 */
Sami Kalliomäki6bf70d22017-10-17 09:22:23 +020023public class YuvConverter {
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020024 private static final String FRAGMENT_SHADER =
Magnus Jedvert577bc192016-10-19 15:29:02 +020025 // Difference in texture coordinate corresponding to one
26 // sub-pixel in the x direction.
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020027 "uniform vec2 xUnit;\n"
Magnus Jedvert577bc192016-10-19 15:29:02 +020028 // Color conversion coefficients, including constant term
29 + "uniform vec4 coeffs;\n"
30 + "\n"
31 + "void main() {\n"
32 // Since the alpha read from the texture is always 1, this could
33 // be written as a mat4 x vec4 multiply. However, that seems to
34 // give a worse framerate, possibly because the additional
35 // multiplies by 1.0 consume resources. TODO(nisse): Could also
36 // try to do it as a vec3 x mat3x4, followed by an add in of a
37 // constant vector.
38 + " gl_FragColor.r = coeffs.a + dot(coeffs.rgb,\n"
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020039 + " sample(tc - 1.5 * xUnit).rgb);\n"
Magnus Jedvert577bc192016-10-19 15:29:02 +020040 + " gl_FragColor.g = coeffs.a + dot(coeffs.rgb,\n"
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020041 + " sample(tc - 0.5 * xUnit).rgb);\n"
Magnus Jedvert577bc192016-10-19 15:29:02 +020042 + " gl_FragColor.b = coeffs.a + dot(coeffs.rgb,\n"
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020043 + " sample(tc + 0.5 * xUnit).rgb);\n"
Magnus Jedvert577bc192016-10-19 15:29:02 +020044 + " gl_FragColor.a = coeffs.a + dot(coeffs.rgb,\n"
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020045 + " sample(tc + 1.5 * xUnit).rgb);\n"
Sami Kalliomäkicb98b112017-10-16 11:20:26 +020046 + "}\n";
47
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020048 private static class ShaderCallbacks implements GlGenericDrawer.ShaderCallbacks {
49 // Y'UV444 to RGB888, see https://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion. We
50 // use the ITU-R coefficients for U and V.
51 private static final float[] yCoeffs = new float[] {0.2987856f, 0.5871095f, 0.1141049f, 0.0f};
52 private static final float[] uCoeffs =
53 new float[] {-0.168805420f, -0.3317003f, 0.5005057f, 0.5f};
54 private static final float[] vCoeffs = new float[] {0.4997964f, -0.4184672f, -0.0813292f, 0.5f};
55
56 private int xUnitLoc;
57 private int coeffsLoc;
58
59 private float[] coeffs;
60 private float stepSize;
61
62 public void setPlaneY() {
63 coeffs = yCoeffs;
64 stepSize = 1.0f;
65 }
66
67 public void setPlaneU() {
68 coeffs = uCoeffs;
69 stepSize = 2.0f;
70 }
71
72 public void setPlaneV() {
73 coeffs = vCoeffs;
74 stepSize = 2.0f;
75 }
76
77 @Override
78 public void onNewShader(GlShader shader) {
79 xUnitLoc = shader.getUniformLocation("xUnit");
80 coeffsLoc = shader.getUniformLocation("coeffs");
81 }
82
83 @Override
84 public void onPrepareShader(GlShader shader, float[] texMatrix, int frameWidth, int frameHeight,
85 int viewportWidth, int viewportHeight) {
86 GLES20.glUniform4fv(coeffsLoc, /* count= */ 1, coeffs, /* offset= */ 0);
87 // Matrix * (1;0;0;0) / (width / stepSize). Note that OpenGL uses column major order.
88 GLES20.glUniform2f(
89 xUnitLoc, stepSize * texMatrix[0] / frameWidth, stepSize * texMatrix[1] / frameWidth);
90 }
91 }
Magnus Jedvert577bc192016-10-19 15:29:02 +020092
magjed1cb48232016-10-20 03:19:16 -070093 private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker();
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020094 private final GlTextureFrameBuffer i420TextureFrameBuffer =
95 new GlTextureFrameBuffer(GLES20.GL_RGBA);
magjed1cb48232016-10-20 03:19:16 -070096 private boolean released = false;
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020097 private final ShaderCallbacks shaderCallbacks = new ShaderCallbacks();
98 private final GlGenericDrawer drawer = new GlGenericDrawer(FRAGMENT_SHADER, shaderCallbacks);
Magnus Jedvert577bc192016-10-19 15:29:02 +020099
magjed1cb48232016-10-20 03:19:16 -0700100 /**
101 * This class should be constructed on a thread that has an active EGL context.
102 */
103 public YuvConverter() {
Magnus Jedvert1d270f82018-04-16 16:28:29 +0200104 threadChecker.detachThread();
Sami Kalliomäkicb98b112017-10-16 11:20:26 +0200105 }
106
107 /** Converts the texture buffer to I420. */
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200108 public I420Buffer convert(TextureBuffer inputTextureBuffer) {
magjed1cb48232016-10-20 03:19:16 -0700109 threadChecker.checkIsOnValidThread();
Magnus Jedvert577bc192016-10-19 15:29:02 +0200110 if (released) {
111 throw new IllegalStateException("YuvConverter.convert called on released object");
112 }
Magnus Jedvert577bc192016-10-19 15:29:02 +0200113 // We draw into a buffer laid out like
114 //
115 // +---------+
116 // | |
117 // | Y |
118 // | |
119 // | |
120 // +----+----+
121 // | U | V |
122 // | | |
123 // +----+----+
124 //
125 // In memory, we use the same stride for all of Y, U and V. The
126 // U data starts at offset |height| * |stride| from the Y data,
127 // and the V data starts at at offset |stride/2| from the U
128 // data, with rows of U and V data alternating.
129 //
130 // Now, it would have made sense to allocate a pixel buffer with
131 // a single byte per pixel (EGL10.EGL_COLOR_BUFFER_TYPE,
132 // EGL10.EGL_LUMINANCE_BUFFER,), but that seems to be
133 // unsupported by devices. So do the following hack: Allocate an
134 // RGBA buffer, of width |stride|/4. To render each of these
135 // large pixels, sample the texture at 4 different x coordinates
136 // and store the results in the four components.
137 //
138 // Since the V data needs to start on a boundary of such a
139 // larger pixel, it is not sufficient that |stride| is even, it
140 // has to be a multiple of 8 pixels.
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200141 final int frameWidth = inputTextureBuffer.getWidth();
142 final int frameHeight = inputTextureBuffer.getHeight();
143 final int stride = ((frameWidth + 7) / 8) * 8;
144 final int uvHeight = (frameHeight + 1) / 2;
145 // Total height of the combined memory layout.
146 final int totalHeight = frameHeight + uvHeight;
147 final ByteBuffer i420ByteBuffer = JniCommon.nativeAllocateByteBuffer(stride * totalHeight);
148 // Viewport width is divided by four since we are squeezing in four color bytes in each RGBA
149 // pixel.
150 final int viewportWidth = stride / 4;
Magnus Jedvert577bc192016-10-19 15:29:02 +0200151
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200152 // Produce a frame buffer starting at top-left corner, not bottom-left.
153 final Matrix renderMatrix = new Matrix();
154 renderMatrix.preTranslate(0.5f, 0.5f);
155 renderMatrix.preScale(1f, -1f);
156 renderMatrix.preTranslate(-0.5f, -0.5f);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200157
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200158 i420TextureFrameBuffer.setSize(viewportWidth, totalHeight);
sakal2fcd2dd2017-01-18 03:21:10 -0800159
magjed1cb48232016-10-20 03:19:16 -0700160 // Bind our framebuffer.
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200161 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, i420TextureFrameBuffer.getFrameBufferId());
magjed1cb48232016-10-20 03:19:16 -0700162 GlUtil.checkNoGLES2Error("glBindFramebuffer");
163
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200164 // Draw Y.
165 shaderCallbacks.setPlaneY();
166 VideoFrameDrawer.drawTexture(drawer, inputTextureBuffer, renderMatrix, frameWidth, frameHeight,
167 /* viewportX= */ 0, /* viewportY= */ 0, viewportWidth,
168 /* viewportHeight= */ frameHeight);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200169
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200170 // Draw U.
171 shaderCallbacks.setPlaneU();
172 VideoFrameDrawer.drawTexture(drawer, inputTextureBuffer, renderMatrix, frameWidth, frameHeight,
173 /* viewportX= */ 0, /* viewportY= */ frameHeight, viewportWidth / 2,
174 /* viewportHeight= */ uvHeight);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200175
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200176 // Draw V.
177 shaderCallbacks.setPlaneV();
178 VideoFrameDrawer.drawTexture(drawer, inputTextureBuffer, renderMatrix, frameWidth, frameHeight,
179 /* viewportX= */ viewportWidth / 2, /* viewportY= */ frameHeight, viewportWidth / 2,
180 /* viewportHeight= */ uvHeight);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200181
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200182 GLES20.glReadPixels(0, 0, i420TextureFrameBuffer.getWidth(), i420TextureFrameBuffer.getHeight(),
183 GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, i420ByteBuffer);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200184
185 GlUtil.checkNoGLES2Error("YuvConverter.convert");
186
magjed1cb48232016-10-20 03:19:16 -0700187 // Restore normal framebuffer.
188 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
magjed1cb48232016-10-20 03:19:16 -0700189
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200190 // Prepare Y, U, and V ByteBuffer slices.
191 final int yPos = 0;
192 final int uPos = yPos + stride * frameHeight;
193 // Rows of U and V alternate in the buffer, so V data starts after the first row of U.
194 final int vPos = uPos + stride / 2;
195
196 i420ByteBuffer.position(yPos);
197 i420ByteBuffer.limit(yPos + stride * frameHeight);
198 final ByteBuffer dataY = i420ByteBuffer.slice();
199
200 i420ByteBuffer.position(uPos);
201 // The last row does not have padding.
202 final int uvSize = stride * (uvHeight - 1) + stride / 2;
203 i420ByteBuffer.limit(uPos + uvSize);
204 final ByteBuffer dataU = i420ByteBuffer.slice();
205
206 i420ByteBuffer.position(vPos);
207 i420ByteBuffer.limit(vPos + uvSize);
208 final ByteBuffer dataV = i420ByteBuffer.slice();
209
210 return JavaI420Buffer.wrap(frameWidth, frameHeight, dataY, stride, dataU, stride, dataV, stride,
211 () -> { JniCommon.nativeFreeByteBuffer(i420ByteBuffer); });
Magnus Jedvert577bc192016-10-19 15:29:02 +0200212 }
213
magjed1cb48232016-10-20 03:19:16 -0700214 public void release() {
215 threadChecker.checkIsOnValidThread();
Magnus Jedvert577bc192016-10-19 15:29:02 +0200216 released = true;
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200217 drawer.release();
218 i420TextureFrameBuffer.release();
Magnus Jedvert577bc192016-10-19 15:29:02 +0200219 }
220}