Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | package org.webrtc; |
| 12 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 13 | import android.graphics.Matrix; |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 14 | import android.opengl.GLES20; |
Fabian Bergmark | 1bb36d2 | 2021-06-17 11:53:24 +0200 | [diff] [blame] | 15 | import android.opengl.GLException; |
| 16 | import android.support.annotation.Nullable; |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 17 | import java.nio.ByteBuffer; |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 18 | import org.webrtc.VideoFrame.I420Buffer; |
| 19 | import org.webrtc.VideoFrame.TextureBuffer; |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 20 | |
| 21 | /** |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 22 | * 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 Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 24 | */ |
Fabian Bergmark | 1bb36d2 | 2021-06-17 11:53:24 +0200 | [diff] [blame] | 25 | public final class YuvConverter { |
| 26 | private static final String TAG = "YuvConverter"; |
| 27 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 28 | private static final String FRAGMENT_SHADER = |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 29 | // Difference in texture coordinate corresponding to one |
| 30 | // sub-pixel in the x direction. |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 31 | "uniform vec2 xUnit;\n" |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 32 | // 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 |
| 39 | // multiplies by 1.0 consume resources. TODO(nisse): Could also |
| 40 | // try to do it as a vec3 x mat3x4, followed by an add in of a |
| 41 | // constant vector. |
| 42 | + " gl_FragColor.r = coeffs.a + dot(coeffs.rgb,\n" |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 43 | + " sample(tc - 1.5 * xUnit).rgb);\n" |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 44 | + " gl_FragColor.g = coeffs.a + dot(coeffs.rgb,\n" |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 45 | + " sample(tc - 0.5 * xUnit).rgb);\n" |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 46 | + " gl_FragColor.b = coeffs.a + dot(coeffs.rgb,\n" |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 47 | + " sample(tc + 0.5 * xUnit).rgb);\n" |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 48 | + " gl_FragColor.a = coeffs.a + dot(coeffs.rgb,\n" |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 49 | + " sample(tc + 1.5 * xUnit).rgb);\n" |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 50 | + "}\n"; |
| 51 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 52 | private static class ShaderCallbacks implements GlGenericDrawer.ShaderCallbacks { |
Sami Kalliomäki | 8ccddff | 2018-09-05 11:43:38 +0200 | [diff] [blame] | 53 | // Y'UV444 to RGB888, see https://en.wikipedia.org/wiki/YUV#Y%E2%80%B2UV444_to_RGB888_conversion |
| 54 | // We use the ITU-R BT.601 coefficients for Y, U and V. |
| 55 | // The values in Wikipedia are inaccurate, the accurate values derived from the spec are: |
| 56 | // Y = 0.299 * R + 0.587 * G + 0.114 * B |
| 57 | // U = -0.168736 * R - 0.331264 * G + 0.5 * B + 0.5 |
| 58 | // V = 0.5 * R - 0.418688 * G - 0.0813124 * B + 0.5 |
| 59 | // To map the Y-values to range [16-235] and U- and V-values to range [16-240], the matrix has |
| 60 | // been multiplied with matrix: |
| 61 | // {{219 / 255, 0, 0, 16 / 255}, |
| 62 | // {0, 224 / 255, 0, 16 / 255}, |
| 63 | // {0, 0, 224 / 255, 16 / 255}, |
| 64 | // {0, 0, 0, 1}} |
| 65 | private static final float[] yCoeffs = |
| 66 | new float[] {0.256788f, 0.504129f, 0.0979059f, 0.0627451f}; |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 67 | private static final float[] uCoeffs = |
Sami Kalliomäki | 8ccddff | 2018-09-05 11:43:38 +0200 | [diff] [blame] | 68 | new float[] {-0.148223f, -0.290993f, 0.439216f, 0.501961f}; |
| 69 | private static final float[] vCoeffs = |
| 70 | new float[] {0.439216f, -0.367788f, -0.0714274f, 0.501961f}; |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 71 | |
| 72 | private int xUnitLoc; |
| 73 | private int coeffsLoc; |
| 74 | |
| 75 | private float[] coeffs; |
| 76 | private float stepSize; |
| 77 | |
| 78 | public void setPlaneY() { |
| 79 | coeffs = yCoeffs; |
| 80 | stepSize = 1.0f; |
| 81 | } |
| 82 | |
| 83 | public void setPlaneU() { |
| 84 | coeffs = uCoeffs; |
| 85 | stepSize = 2.0f; |
| 86 | } |
| 87 | |
| 88 | public void setPlaneV() { |
| 89 | coeffs = vCoeffs; |
| 90 | stepSize = 2.0f; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onNewShader(GlShader shader) { |
| 95 | xUnitLoc = shader.getUniformLocation("xUnit"); |
| 96 | coeffsLoc = shader.getUniformLocation("coeffs"); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void onPrepareShader(GlShader shader, float[] texMatrix, int frameWidth, int frameHeight, |
| 101 | int viewportWidth, int viewportHeight) { |
| 102 | GLES20.glUniform4fv(coeffsLoc, /* count= */ 1, coeffs, /* offset= */ 0); |
| 103 | // Matrix * (1;0;0;0) / (width / stepSize). Note that OpenGL uses column major order. |
| 104 | GLES20.glUniform2f( |
| 105 | xUnitLoc, stepSize * texMatrix[0] / frameWidth, stepSize * texMatrix[1] / frameWidth); |
| 106 | } |
| 107 | } |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 108 | |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 109 | private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker(); |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 110 | private final GlTextureFrameBuffer i420TextureFrameBuffer = |
| 111 | new GlTextureFrameBuffer(GLES20.GL_RGBA); |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 112 | private final ShaderCallbacks shaderCallbacks = new ShaderCallbacks(); |
| 113 | private final GlGenericDrawer drawer = new GlGenericDrawer(FRAGMENT_SHADER, shaderCallbacks); |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 114 | private final VideoFrameDrawer videoFrameDrawer; |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 115 | |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 116 | /** |
| 117 | * This class should be constructed on a thread that has an active EGL context. |
| 118 | */ |
| 119 | public YuvConverter() { |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 120 | this(new VideoFrameDrawer()); |
| 121 | } |
| 122 | |
| 123 | public YuvConverter(VideoFrameDrawer videoFrameDrawer) { |
| 124 | this.videoFrameDrawer = videoFrameDrawer; |
Magnus Jedvert | 1d270f8 | 2018-04-16 16:28:29 +0200 | [diff] [blame] | 125 | threadChecker.detachThread(); |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** Converts the texture buffer to I420. */ |
Fabian Bergmark | 1bb36d2 | 2021-06-17 11:53:24 +0200 | [diff] [blame] | 129 | @Nullable |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 130 | public I420Buffer convert(TextureBuffer inputTextureBuffer) { |
Fabian Bergmark | 1bb36d2 | 2021-06-17 11:53:24 +0200 | [diff] [blame] | 131 | try { |
| 132 | return convertInternal(inputTextureBuffer); |
| 133 | } catch (GLException e) { |
| 134 | Logging.w(TAG, "Failed to convert TextureBuffer", e); |
| 135 | } |
| 136 | return null; |
| 137 | } |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 138 | |
Fabian Bergmark | 1bb36d2 | 2021-06-17 11:53:24 +0200 | [diff] [blame] | 139 | private I420Buffer convertInternal(TextureBuffer inputTextureBuffer) { |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 140 | TextureBuffer preparedBuffer = (TextureBuffer) videoFrameDrawer.prepareBufferForViewportSize( |
| 141 | inputTextureBuffer, inputTextureBuffer.getWidth(), inputTextureBuffer.getHeight()); |
| 142 | |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 143 | // We draw into a buffer laid out like |
| 144 | // |
| 145 | // +---------+ |
| 146 | // | | |
| 147 | // | Y | |
| 148 | // | | |
| 149 | // | | |
| 150 | // +----+----+ |
| 151 | // | U | V | |
| 152 | // | | | |
| 153 | // +----+----+ |
| 154 | // |
| 155 | // In memory, we use the same stride for all of Y, U and V. The |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 156 | // U data starts at offset `height` * `stride` from the Y data, |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 157 | // and the V data starts at at offset |stride/2| from the U |
| 158 | // data, with rows of U and V data alternating. |
| 159 | // |
| 160 | // Now, it would have made sense to allocate a pixel buffer with |
| 161 | // a single byte per pixel (EGL10.EGL_COLOR_BUFFER_TYPE, |
| 162 | // EGL10.EGL_LUMINANCE_BUFFER,), but that seems to be |
| 163 | // unsupported by devices. So do the following hack: Allocate an |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 164 | // RGBA buffer, of width `stride`/4. To render each of these |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 165 | // large pixels, sample the texture at 4 different x coordinates |
| 166 | // and store the results in the four components. |
| 167 | // |
| 168 | // Since the V data needs to start on a boundary of such a |
Artem Titov | d7ac581 | 2021-07-27 12:23:39 +0200 | [diff] [blame] | 169 | // larger pixel, it is not sufficient that `stride` is even, it |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 170 | // has to be a multiple of 8 pixels. |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 171 | final int frameWidth = preparedBuffer.getWidth(); |
| 172 | final int frameHeight = preparedBuffer.getHeight(); |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 173 | final int stride = ((frameWidth + 7) / 8) * 8; |
| 174 | final int uvHeight = (frameHeight + 1) / 2; |
| 175 | // Total height of the combined memory layout. |
| 176 | final int totalHeight = frameHeight + uvHeight; |
| 177 | final ByteBuffer i420ByteBuffer = JniCommon.nativeAllocateByteBuffer(stride * totalHeight); |
| 178 | // Viewport width is divided by four since we are squeezing in four color bytes in each RGBA |
| 179 | // pixel. |
| 180 | final int viewportWidth = stride / 4; |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 181 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 182 | // Produce a frame buffer starting at top-left corner, not bottom-left. |
| 183 | final Matrix renderMatrix = new Matrix(); |
| 184 | renderMatrix.preTranslate(0.5f, 0.5f); |
| 185 | renderMatrix.preScale(1f, -1f); |
| 186 | renderMatrix.preTranslate(-0.5f, -0.5f); |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 187 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 188 | i420TextureFrameBuffer.setSize(viewportWidth, totalHeight); |
sakal | 2fcd2dd | 2017-01-18 03:21:10 -0800 | [diff] [blame] | 189 | |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 190 | // Bind our framebuffer. |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 191 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, i420TextureFrameBuffer.getFrameBufferId()); |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 192 | GlUtil.checkNoGLES2Error("glBindFramebuffer"); |
| 193 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 194 | // Draw Y. |
| 195 | shaderCallbacks.setPlaneY(); |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 196 | VideoFrameDrawer.drawTexture(drawer, preparedBuffer, renderMatrix, frameWidth, frameHeight, |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 197 | /* viewportX= */ 0, /* viewportY= */ 0, viewportWidth, |
| 198 | /* viewportHeight= */ frameHeight); |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 199 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 200 | // Draw U. |
| 201 | shaderCallbacks.setPlaneU(); |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 202 | VideoFrameDrawer.drawTexture(drawer, preparedBuffer, renderMatrix, frameWidth, frameHeight, |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 203 | /* viewportX= */ 0, /* viewportY= */ frameHeight, viewportWidth / 2, |
| 204 | /* viewportHeight= */ uvHeight); |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 205 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 206 | // Draw V. |
| 207 | shaderCallbacks.setPlaneV(); |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 208 | VideoFrameDrawer.drawTexture(drawer, preparedBuffer, renderMatrix, frameWidth, frameHeight, |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 209 | /* viewportX= */ viewportWidth / 2, /* viewportY= */ frameHeight, viewportWidth / 2, |
| 210 | /* viewportHeight= */ uvHeight); |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 211 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 212 | GLES20.glReadPixels(0, 0, i420TextureFrameBuffer.getWidth(), i420TextureFrameBuffer.getHeight(), |
| 213 | GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, i420ByteBuffer); |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 214 | |
| 215 | GlUtil.checkNoGLES2Error("YuvConverter.convert"); |
| 216 | |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 217 | // Restore normal framebuffer. |
| 218 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 219 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 220 | // Prepare Y, U, and V ByteBuffer slices. |
| 221 | final int yPos = 0; |
| 222 | final int uPos = yPos + stride * frameHeight; |
| 223 | // Rows of U and V alternate in the buffer, so V data starts after the first row of U. |
| 224 | final int vPos = uPos + stride / 2; |
| 225 | |
| 226 | i420ByteBuffer.position(yPos); |
| 227 | i420ByteBuffer.limit(yPos + stride * frameHeight); |
| 228 | final ByteBuffer dataY = i420ByteBuffer.slice(); |
| 229 | |
| 230 | i420ByteBuffer.position(uPos); |
| 231 | // The last row does not have padding. |
| 232 | final int uvSize = stride * (uvHeight - 1) + stride / 2; |
| 233 | i420ByteBuffer.limit(uPos + uvSize); |
| 234 | final ByteBuffer dataU = i420ByteBuffer.slice(); |
| 235 | |
| 236 | i420ByteBuffer.position(vPos); |
| 237 | i420ByteBuffer.limit(vPos + uvSize); |
| 238 | final ByteBuffer dataV = i420ByteBuffer.slice(); |
| 239 | |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 240 | preparedBuffer.release(); |
| 241 | |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 242 | return JavaI420Buffer.wrap(frameWidth, frameHeight, dataY, stride, dataU, stride, dataV, stride, |
| 243 | () -> { JniCommon.nativeFreeByteBuffer(i420ByteBuffer); }); |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 244 | } |
| 245 | |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 246 | public void release() { |
| 247 | threadChecker.checkIsOnValidThread(); |
Magnus Jedvert | 65c61dc | 2018-06-15 09:33:20 +0200 | [diff] [blame] | 248 | drawer.release(); |
| 249 | i420TextureFrameBuffer.release(); |
Åsa Persson | f2889bb | 2019-02-25 16:20:01 +0100 | [diff] [blame] | 250 | videoFrameDrawer.release(); |
Magnus Jedvert | 7b87530 | 2018-08-09 13:51:42 +0200 | [diff] [blame] | 251 | // Allow this class to be reused. |
| 252 | threadChecker.detachThread(); |
Magnus Jedvert | 577bc19 | 2016-10-19 15:29:02 +0200 | [diff] [blame] | 253 | } |
| 254 | } |