Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 13 import android.opengl.GLES11Ext; | |
| 14 import android.opengl.GLES20; | |
| 15 import java.nio.ByteBuffer; | |
| 16 import java.nio.FloatBuffer; | |
| 17 | |
| 18 /** | |
| 19 * Class for converting OES textures to a YUV ByteBuffer. | |
| 20 */ | |
| 21 public class YuvConverter { | |
|
sakal
2016/10/19 11:16:02
nit: Do we want to expose this class to external u
magjed_webrtc
2016/10/19 12:27:01
Ok, let's make it package private for now.
| |
| 22 private final EglBase eglBase; | |
| 23 private final GlShader shader; | |
| 24 private boolean released = false; | |
| 25 | |
| 26 // Vertex coordinates in Normalized Device Coordinates, i.e. | |
| 27 // (-1, -1) is bottom-left and (1, 1) is top-right. | |
| 28 private static final FloatBuffer DEVICE_RECTANGLE = GlUtil.createFloatBuffer(n ew float[] { | |
| 29 -1.0f, -1.0f, // Bottom left. | |
| 30 1.0f, -1.0f, // Bottom right. | |
| 31 -1.0f, 1.0f, // Top left. | |
| 32 1.0f, 1.0f, // Top right. | |
| 33 }); | |
| 34 | |
| 35 // Texture coordinates - (0, 0) is bottom-left and (1, 1) is top-right. | |
| 36 private static final FloatBuffer TEXTURE_RECTANGLE = GlUtil.createFloatBuffer( new float[] { | |
| 37 0.0f, 0.0f, // Bottom left. | |
| 38 1.0f, 0.0f, // Bottom right. | |
| 39 0.0f, 1.0f, // Top left. | |
| 40 1.0f, 1.0f // Top right. | |
| 41 }); | |
| 42 | |
| 43 // clang-format off | |
| 44 private static final String VERTEX_SHADER = | |
| 45 "varying vec2 interp_tc;\n" | |
| 46 + "attribute vec4 in_pos;\n" | |
| 47 + "attribute vec4 in_tc;\n" | |
| 48 + "\n" | |
| 49 + "uniform mat4 texMatrix;\n" | |
| 50 + "\n" | |
| 51 + "void main() {\n" | |
| 52 + " gl_Position = in_pos;\n" | |
| 53 + " interp_tc = (texMatrix * in_tc).xy;\n" | |
| 54 + "}\n"; | |
| 55 | |
| 56 private static final String FRAGMENT_SHADER = | |
| 57 "#extension GL_OES_EGL_image_external : require\n" | |
| 58 + "precision mediump float;\n" | |
| 59 + "varying vec2 interp_tc;\n" | |
| 60 + "\n" | |
| 61 + "uniform samplerExternalOES oesTex;\n" | |
| 62 // Difference in texture coordinate corresponding to one | |
| 63 // sub-pixel in the x direction. | |
| 64 + "uniform vec2 xUnit;\n" | |
| 65 // Color conversion coefficients, including constant term | |
| 66 + "uniform vec4 coeffs;\n" | |
| 67 + "\n" | |
| 68 + "void main() {\n" | |
| 69 // Since the alpha read from the texture is always 1, this could | |
| 70 // be written as a mat4 x vec4 multiply. However, that seems to | |
| 71 // give a worse framerate, possibly because the additional | |
| 72 // multiplies by 1.0 consume resources. TODO(nisse): Could also | |
| 73 // try to do it as a vec3 x mat3x4, followed by an add in of a | |
| 74 // constant vector. | |
| 75 + " gl_FragColor.r = coeffs.a + dot(coeffs.rgb,\n" | |
| 76 + " texture2D(oesTex, interp_tc - 1.5 * xUnit).rgb);\n" | |
| 77 + " gl_FragColor.g = coeffs.a + dot(coeffs.rgb,\n" | |
| 78 + " texture2D(oesTex, interp_tc - 0.5 * xUnit).rgb);\n" | |
| 79 + " gl_FragColor.b = coeffs.a + dot(coeffs.rgb,\n" | |
| 80 + " texture2D(oesTex, interp_tc + 0.5 * xUnit).rgb);\n" | |
| 81 + " gl_FragColor.a = coeffs.a + dot(coeffs.rgb,\n" | |
| 82 + " texture2D(oesTex, interp_tc + 1.5 * xUnit).rgb);\n" | |
| 83 + "}\n"; | |
| 84 // clang-format on | |
| 85 | |
| 86 private int texMatrixLoc; | |
| 87 private int xUnitLoc; | |
| 88 private int coeffsLoc; | |
| 89 | |
| 90 YuvConverter(EglBase.Context sharedContext) { | |
| 91 eglBase = EglBase.create(sharedContext, EglBase.CONFIG_PIXEL_RGBA_BUFFER); | |
| 92 eglBase.createDummyPbufferSurface(); | |
| 93 eglBase.makeCurrent(); | |
| 94 | |
| 95 shader = new GlShader(VERTEX_SHADER, FRAGMENT_SHADER); | |
| 96 shader.useProgram(); | |
| 97 texMatrixLoc = shader.getUniformLocation("texMatrix"); | |
| 98 xUnitLoc = shader.getUniformLocation("xUnit"); | |
| 99 coeffsLoc = shader.getUniformLocation("coeffs"); | |
| 100 GLES20.glUniform1i(shader.getUniformLocation("oesTex"), 0); | |
| 101 GlUtil.checkNoGLES2Error("Initialize fragment shader uniform values."); | |
| 102 // Initialize vertex shader attributes. | |
| 103 shader.setVertexAttribArray("in_pos", 2, DEVICE_RECTANGLE); | |
| 104 // If the width is not a multiple of 4 pixels, the texture | |
| 105 // will be scaled up slightly and clipped at the right border. | |
| 106 shader.setVertexAttribArray("in_tc", 2, TEXTURE_RECTANGLE); | |
| 107 eglBase.detachCurrent(); | |
| 108 } | |
| 109 | |
| 110 synchronized void convert( | |
| 111 ByteBuffer buf, int width, int height, int stride, int textureId, float[] transformMatrix) { | |
| 112 if (released) { | |
| 113 throw new IllegalStateException("YuvConverter.convert called on released o bject"); | |
| 114 } | |
| 115 | |
| 116 // We draw into a buffer laid out like | |
| 117 // | |
| 118 // +---------+ | |
| 119 // | | | |
| 120 // | Y | | |
| 121 // | | | |
| 122 // | | | |
| 123 // +----+----+ | |
| 124 // | U | V | | |
| 125 // | | | | |
| 126 // +----+----+ | |
| 127 // | |
| 128 // In memory, we use the same stride for all of Y, U and V. The | |
| 129 // U data starts at offset |height| * |stride| from the Y data, | |
| 130 // and the V data starts at at offset |stride/2| from the U | |
| 131 // data, with rows of U and V data alternating. | |
| 132 // | |
| 133 // Now, it would have made sense to allocate a pixel buffer with | |
| 134 // a single byte per pixel (EGL10.EGL_COLOR_BUFFER_TYPE, | |
| 135 // EGL10.EGL_LUMINANCE_BUFFER,), but that seems to be | |
| 136 // unsupported by devices. So do the following hack: Allocate an | |
| 137 // RGBA buffer, of width |stride|/4. To render each of these | |
| 138 // large pixels, sample the texture at 4 different x coordinates | |
| 139 // and store the results in the four components. | |
| 140 // | |
| 141 // Since the V data needs to start on a boundary of such a | |
| 142 // larger pixel, it is not sufficient that |stride| is even, it | |
| 143 // has to be a multiple of 8 pixels. | |
| 144 | |
| 145 if (stride % 8 != 0) { | |
| 146 throw new IllegalArgumentException("Invalid stride, must be a multiple of 8"); | |
| 147 } | |
| 148 if (stride < width) { | |
| 149 throw new IllegalArgumentException("Invalid stride, must >= width"); | |
| 150 } | |
| 151 | |
| 152 int y_width = (width + 3) / 4; | |
| 153 int uv_width = (width + 7) / 8; | |
| 154 int uv_height = (height + 1) / 2; | |
| 155 int total_height = height + uv_height; | |
| 156 int size = stride * total_height; | |
| 157 | |
| 158 if (buf.capacity() < size) { | |
| 159 throw new IllegalArgumentException("YuvConverter.convert called with too s mall buffer"); | |
| 160 } | |
| 161 // Produce a frame buffer starting at top-left corner, not | |
| 162 // bottom-left. | |
| 163 transformMatrix = | |
| 164 RendererCommon.multiplyMatrices(transformMatrix, RendererCommon.vertical FlipMatrix()); | |
| 165 | |
| 166 // Create new pBuffferSurface with the correct size if needed. | |
| 167 if (eglBase.hasSurface()) { | |
| 168 if (eglBase.surfaceWidth() != stride / 4 || eglBase.surfaceHeight() != tot al_height) { | |
| 169 eglBase.releaseSurface(); | |
| 170 eglBase.createPbufferSurface(stride / 4, total_height); | |
| 171 } | |
| 172 } else { | |
| 173 eglBase.createPbufferSurface(stride / 4, total_height); | |
| 174 } | |
| 175 | |
| 176 eglBase.makeCurrent(); | |
| 177 | |
| 178 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); | |
| 179 GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); | |
| 180 GLES20.glUniformMatrix4fv(texMatrixLoc, 1, false, transformMatrix, 0); | |
| 181 | |
| 182 // Draw Y | |
| 183 GLES20.glViewport(0, 0, y_width, height); | |
| 184 // Matrix * (1;0;0;0) / width. Note that opengl uses column major order. | |
| 185 GLES20.glUniform2f(xUnitLoc, transformMatrix[0] / width, transformMatrix[1] / width); | |
| 186 // Y'UV444 to RGB888, see | |
| 187 // https://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion. | |
| 188 // We use the ITU-R coefficients for U and V */ | |
| 189 GLES20.glUniform4f(coeffsLoc, 0.299f, 0.587f, 0.114f, 0.0f); | |
| 190 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); | |
| 191 | |
| 192 // Draw U | |
| 193 GLES20.glViewport(0, height, uv_width, uv_height); | |
| 194 // Matrix * (1;0;0;0) / (width / 2). Note that opengl uses column major orde r. | |
| 195 GLES20.glUniform2f( | |
| 196 xUnitLoc, 2.0f * transformMatrix[0] / width, 2.0f * transformMatrix[1] / width); | |
| 197 GLES20.glUniform4f(coeffsLoc, -0.169f, -0.331f, 0.499f, 0.5f); | |
| 198 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); | |
| 199 | |
| 200 // Draw V | |
| 201 GLES20.glViewport(stride / 8, height, uv_width, uv_height); | |
| 202 GLES20.glUniform4f(coeffsLoc, 0.499f, -0.418f, -0.0813f, 0.5f); | |
| 203 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); | |
| 204 | |
| 205 GLES20.glReadPixels( | |
| 206 0, 0, stride / 4, total_height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf); | |
| 207 | |
| 208 GlUtil.checkNoGLES2Error("YuvConverter.convert"); | |
| 209 | |
| 210 // Unbind texture. Reportedly needed on some devices to get | |
| 211 // the texture updated from the camera. | |
| 212 GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0); | |
| 213 eglBase.detachCurrent(); | |
| 214 } | |
| 215 | |
| 216 synchronized void release() { | |
| 217 released = true; | |
| 218 eglBase.makeCurrent(); | |
| 219 shader.release(); | |
| 220 eglBase.release(); | |
| 221 } | |
| 222 } | |
| OLD | NEW |