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