blob: c855d4be41ea0237b35deb7588b3b78a9b1f22f4 [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;
Fabian Bergmark1bb36d22021-06-17 11:53:24 +020015import android.opengl.GLException;
Byoungchan Lee02334e02021-08-14 11:41:59 +090016import androidx.annotation.Nullable;
Magnus Jedvert577bc192016-10-19 15:29:02 +020017import java.nio.ByteBuffer;
Sami Kalliomäkicb98b112017-10-16 11:20:26 +020018import org.webrtc.VideoFrame.I420Buffer;
19import org.webrtc.VideoFrame.TextureBuffer;
Magnus Jedvert577bc192016-10-19 15:29:02 +020020
21/**
Magnus Jedvert1d270f82018-04-16 16:28:29 +020022 * Class for converting OES textures to a YUV ByteBuffer. It can be constructed on any thread, but
23 * should only be operated from a single thread with an active EGL context.
Magnus Jedvert577bc192016-10-19 15:29:02 +020024 */
Fabian Bergmark1bb36d22021-06-17 11:53:24 +020025public final class YuvConverter {
26 private static final String TAG = "YuvConverter";
27
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020028 private static final String FRAGMENT_SHADER =
Magnus Jedvert577bc192016-10-19 15:29:02 +020029 // Difference in texture coordinate corresponding to one
30 // sub-pixel in the x direction.
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020031 "uniform vec2 xUnit;\n"
Magnus Jedvert577bc192016-10-19 15:29:02 +020032 // Color conversion coefficients, including constant term
33 + "uniform vec4 coeffs;\n"
34 + "\n"
35 + "void main() {\n"
36 // Since the alpha read from the texture is always 1, this could
37 // be written as a mat4 x vec4 multiply. However, that seems to
38 // give a worse framerate, possibly because the additional
Niels Möllerb5b159d2022-07-05 09:59:27 +020039 // multiplies by 1.0 consume resources.
Magnus Jedvert577bc192016-10-19 15:29:02 +020040 + " gl_FragColor.r = coeffs.a + dot(coeffs.rgb,\n"
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020041 + " sample(tc - 1.5 * xUnit).rgb);\n"
Magnus Jedvert577bc192016-10-19 15:29:02 +020042 + " gl_FragColor.g = 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.b = coeffs.a + dot(coeffs.rgb,\n"
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020045 + " sample(tc + 0.5 * xUnit).rgb);\n"
Magnus Jedvert577bc192016-10-19 15:29:02 +020046 + " gl_FragColor.a = coeffs.a + dot(coeffs.rgb,\n"
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020047 + " sample(tc + 1.5 * xUnit).rgb);\n"
Sami Kalliomäkicb98b112017-10-16 11:20:26 +020048 + "}\n";
49
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020050 private static class ShaderCallbacks implements GlGenericDrawer.ShaderCallbacks {
Sami Kalliomäki8ccddff2018-09-05 11:43:38 +020051 // Y'UV444 to RGB888, see https://en.wikipedia.org/wiki/YUV#Y%E2%80%B2UV444_to_RGB888_conversion
52 // We use the ITU-R BT.601 coefficients for Y, U and V.
53 // The values in Wikipedia are inaccurate, the accurate values derived from the spec are:
54 // Y = 0.299 * R + 0.587 * G + 0.114 * B
55 // U = -0.168736 * R - 0.331264 * G + 0.5 * B + 0.5
56 // V = 0.5 * R - 0.418688 * G - 0.0813124 * B + 0.5
57 // To map the Y-values to range [16-235] and U- and V-values to range [16-240], the matrix has
58 // been multiplied with matrix:
59 // {{219 / 255, 0, 0, 16 / 255},
60 // {0, 224 / 255, 0, 16 / 255},
61 // {0, 0, 224 / 255, 16 / 255},
62 // {0, 0, 0, 1}}
63 private static final float[] yCoeffs =
64 new float[] {0.256788f, 0.504129f, 0.0979059f, 0.0627451f};
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020065 private static final float[] uCoeffs =
Sami Kalliomäki8ccddff2018-09-05 11:43:38 +020066 new float[] {-0.148223f, -0.290993f, 0.439216f, 0.501961f};
67 private static final float[] vCoeffs =
68 new float[] {0.439216f, -0.367788f, -0.0714274f, 0.501961f};
Magnus Jedvert65c61dc2018-06-15 09:33:20 +020069
70 private int xUnitLoc;
71 private int coeffsLoc;
72
73 private float[] coeffs;
74 private float stepSize;
75
76 public void setPlaneY() {
77 coeffs = yCoeffs;
78 stepSize = 1.0f;
79 }
80
81 public void setPlaneU() {
82 coeffs = uCoeffs;
83 stepSize = 2.0f;
84 }
85
86 public void setPlaneV() {
87 coeffs = vCoeffs;
88 stepSize = 2.0f;
89 }
90
91 @Override
92 public void onNewShader(GlShader shader) {
93 xUnitLoc = shader.getUniformLocation("xUnit");
94 coeffsLoc = shader.getUniformLocation("coeffs");
95 }
96
97 @Override
98 public void onPrepareShader(GlShader shader, float[] texMatrix, int frameWidth, int frameHeight,
99 int viewportWidth, int viewportHeight) {
100 GLES20.glUniform4fv(coeffsLoc, /* count= */ 1, coeffs, /* offset= */ 0);
101 // Matrix * (1;0;0;0) / (width / stepSize). Note that OpenGL uses column major order.
102 GLES20.glUniform2f(
103 xUnitLoc, stepSize * texMatrix[0] / frameWidth, stepSize * texMatrix[1] / frameWidth);
104 }
105 }
Magnus Jedvert577bc192016-10-19 15:29:02 +0200106
magjed1cb48232016-10-20 03:19:16 -0700107 private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker();
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200108 private final GlTextureFrameBuffer i420TextureFrameBuffer =
109 new GlTextureFrameBuffer(GLES20.GL_RGBA);
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200110 private final ShaderCallbacks shaderCallbacks = new ShaderCallbacks();
111 private final GlGenericDrawer drawer = new GlGenericDrawer(FRAGMENT_SHADER, shaderCallbacks);
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100112 private final VideoFrameDrawer videoFrameDrawer;
Magnus Jedvert577bc192016-10-19 15:29:02 +0200113
magjed1cb48232016-10-20 03:19:16 -0700114 /**
115 * This class should be constructed on a thread that has an active EGL context.
116 */
117 public YuvConverter() {
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100118 this(new VideoFrameDrawer());
119 }
120
121 public YuvConverter(VideoFrameDrawer videoFrameDrawer) {
122 this.videoFrameDrawer = videoFrameDrawer;
Magnus Jedvert1d270f82018-04-16 16:28:29 +0200123 threadChecker.detachThread();
Sami Kalliomäkicb98b112017-10-16 11:20:26 +0200124 }
125
126 /** Converts the texture buffer to I420. */
Fabian Bergmark1bb36d22021-06-17 11:53:24 +0200127 @Nullable
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200128 public I420Buffer convert(TextureBuffer inputTextureBuffer) {
Fabian Bergmark1bb36d22021-06-17 11:53:24 +0200129 try {
130 return convertInternal(inputTextureBuffer);
131 } catch (GLException e) {
132 Logging.w(TAG, "Failed to convert TextureBuffer", e);
133 }
134 return null;
135 }
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100136
Fabian Bergmark1bb36d22021-06-17 11:53:24 +0200137 private I420Buffer convertInternal(TextureBuffer inputTextureBuffer) {
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100138 TextureBuffer preparedBuffer = (TextureBuffer) videoFrameDrawer.prepareBufferForViewportSize(
139 inputTextureBuffer, inputTextureBuffer.getWidth(), inputTextureBuffer.getHeight());
140
Magnus Jedvert577bc192016-10-19 15:29:02 +0200141 // We draw into a buffer laid out like
142 //
143 // +---------+
144 // | |
145 // | Y |
146 // | |
147 // | |
148 // +----+----+
149 // | U | V |
150 // | | |
151 // +----+----+
152 //
153 // In memory, we use the same stride for all of Y, U and V. The
Artem Titovd7ac5812021-07-27 12:23:39 +0200154 // U data starts at offset `height` * `stride` from the Y data,
Magnus Jedvert577bc192016-10-19 15:29:02 +0200155 // and the V data starts at at offset |stride/2| from the U
156 // data, with rows of U and V data alternating.
157 //
158 // Now, it would have made sense to allocate a pixel buffer with
159 // a single byte per pixel (EGL10.EGL_COLOR_BUFFER_TYPE,
160 // EGL10.EGL_LUMINANCE_BUFFER,), but that seems to be
161 // unsupported by devices. So do the following hack: Allocate an
Artem Titovd7ac5812021-07-27 12:23:39 +0200162 // RGBA buffer, of width `stride`/4. To render each of these
Magnus Jedvert577bc192016-10-19 15:29:02 +0200163 // large pixels, sample the texture at 4 different x coordinates
164 // and store the results in the four components.
165 //
166 // Since the V data needs to start on a boundary of such a
Artem Titovd7ac5812021-07-27 12:23:39 +0200167 // larger pixel, it is not sufficient that `stride` is even, it
Magnus Jedvert577bc192016-10-19 15:29:02 +0200168 // has to be a multiple of 8 pixels.
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100169 final int frameWidth = preparedBuffer.getWidth();
170 final int frameHeight = preparedBuffer.getHeight();
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200171 final int stride = ((frameWidth + 7) / 8) * 8;
172 final int uvHeight = (frameHeight + 1) / 2;
173 // Total height of the combined memory layout.
174 final int totalHeight = frameHeight + uvHeight;
175 final ByteBuffer i420ByteBuffer = JniCommon.nativeAllocateByteBuffer(stride * totalHeight);
176 // Viewport width is divided by four since we are squeezing in four color bytes in each RGBA
177 // pixel.
178 final int viewportWidth = stride / 4;
Magnus Jedvert577bc192016-10-19 15:29:02 +0200179
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200180 // Produce a frame buffer starting at top-left corner, not bottom-left.
181 final Matrix renderMatrix = new Matrix();
182 renderMatrix.preTranslate(0.5f, 0.5f);
183 renderMatrix.preScale(1f, -1f);
184 renderMatrix.preTranslate(-0.5f, -0.5f);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200185
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200186 i420TextureFrameBuffer.setSize(viewportWidth, totalHeight);
sakal2fcd2dd2017-01-18 03:21:10 -0800187
magjed1cb48232016-10-20 03:19:16 -0700188 // Bind our framebuffer.
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200189 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, i420TextureFrameBuffer.getFrameBufferId());
magjed1cb48232016-10-20 03:19:16 -0700190 GlUtil.checkNoGLES2Error("glBindFramebuffer");
191
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200192 // Draw Y.
193 shaderCallbacks.setPlaneY();
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100194 VideoFrameDrawer.drawTexture(drawer, preparedBuffer, renderMatrix, frameWidth, frameHeight,
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200195 /* viewportX= */ 0, /* viewportY= */ 0, viewportWidth,
196 /* viewportHeight= */ frameHeight);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200197
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200198 // Draw U.
199 shaderCallbacks.setPlaneU();
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100200 VideoFrameDrawer.drawTexture(drawer, preparedBuffer, renderMatrix, frameWidth, frameHeight,
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200201 /* viewportX= */ 0, /* viewportY= */ frameHeight, viewportWidth / 2,
202 /* viewportHeight= */ uvHeight);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200203
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200204 // Draw V.
205 shaderCallbacks.setPlaneV();
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100206 VideoFrameDrawer.drawTexture(drawer, preparedBuffer, renderMatrix, frameWidth, frameHeight,
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200207 /* viewportX= */ viewportWidth / 2, /* viewportY= */ frameHeight, viewportWidth / 2,
208 /* viewportHeight= */ uvHeight);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200209
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200210 GLES20.glReadPixels(0, 0, i420TextureFrameBuffer.getWidth(), i420TextureFrameBuffer.getHeight(),
211 GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, i420ByteBuffer);
Magnus Jedvert577bc192016-10-19 15:29:02 +0200212
213 GlUtil.checkNoGLES2Error("YuvConverter.convert");
214
magjed1cb48232016-10-20 03:19:16 -0700215 // Restore normal framebuffer.
216 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
magjed1cb48232016-10-20 03:19:16 -0700217
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200218 // Prepare Y, U, and V ByteBuffer slices.
219 final int yPos = 0;
220 final int uPos = yPos + stride * frameHeight;
221 // Rows of U and V alternate in the buffer, so V data starts after the first row of U.
222 final int vPos = uPos + stride / 2;
223
224 i420ByteBuffer.position(yPos);
225 i420ByteBuffer.limit(yPos + stride * frameHeight);
226 final ByteBuffer dataY = i420ByteBuffer.slice();
227
228 i420ByteBuffer.position(uPos);
229 // The last row does not have padding.
230 final int uvSize = stride * (uvHeight - 1) + stride / 2;
231 i420ByteBuffer.limit(uPos + uvSize);
232 final ByteBuffer dataU = i420ByteBuffer.slice();
233
234 i420ByteBuffer.position(vPos);
235 i420ByteBuffer.limit(vPos + uvSize);
236 final ByteBuffer dataV = i420ByteBuffer.slice();
237
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100238 preparedBuffer.release();
239
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200240 return JavaI420Buffer.wrap(frameWidth, frameHeight, dataY, stride, dataU, stride, dataV, stride,
241 () -> { JniCommon.nativeFreeByteBuffer(i420ByteBuffer); });
Magnus Jedvert577bc192016-10-19 15:29:02 +0200242 }
243
magjed1cb48232016-10-20 03:19:16 -0700244 public void release() {
245 threadChecker.checkIsOnValidThread();
Magnus Jedvert65c61dc2018-06-15 09:33:20 +0200246 drawer.release();
247 i420TextureFrameBuffer.release();
Åsa Perssonf2889bb2019-02-25 16:20:01 +0100248 videoFrameDrawer.release();
Magnus Jedvert7b875302018-08-09 13:51:42 +0200249 // Allow this class to be reused.
250 threadChecker.detachThread();
Magnus Jedvert577bc192016-10-19 15:29:02 +0200251 }
252}