OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 #import "RTCShader.h" |
| 12 |
| 13 // Native CVPixelBufferRef rendering is only supported on iPhone because it |
| 14 // depends on CVOpenGLESTextureCacheCreate. |
| 15 #if TARGET_OS_IPHONE |
| 16 |
| 17 #import <CoreVideo/CVOpenGLESTextureCache.h> |
| 18 |
| 19 #import "RTCShader+Private.h" |
| 20 #import "WebRTC/RTCVideoFrame.h" |
| 21 |
| 22 #include "webrtc/base/checks.h" |
| 23 |
| 24 static const char kNV12FragmentShaderSource[] = |
| 25 SHADER_VERSION |
| 26 "precision mediump float;" |
| 27 FRAGMENT_SHADER_IN " vec2 v_texcoord;\n" |
| 28 "uniform lowp sampler2D s_textureY;\n" |
| 29 "uniform lowp sampler2D s_textureUV;\n" |
| 30 FRAGMENT_SHADER_OUT |
| 31 "void main() {\n" |
| 32 " mediump float y;\n" |
| 33 " mediump vec2 uv;\n" |
| 34 " y = " FRAGMENT_SHADER_TEXTURE "(s_textureY, v_texcoord).r;\n" |
| 35 " uv = " FRAGMENT_SHADER_TEXTURE "(s_textureUV, v_texcoord).ra -\n" |
| 36 " vec2(0.5, 0.5);\n" |
| 37 " " FRAGMENT_SHADER_COLOR " = vec4(y + 1.403 * uv.y,\n" |
| 38 " y - 0.344 * uv.x - 0.714 * uv.y,\n" |
| 39 " y + 1.770 * uv.x,\n" |
| 40 " 1.0);\n" |
| 41 " }\n"; |
| 42 |
| 43 @implementation RTCNativeNV12Shader { |
| 44 GLuint _vertexBuffer; |
| 45 GLuint _nv12Program; |
| 46 GLint _ySampler; |
| 47 GLint _uvSampler; |
| 48 CVOpenGLESTextureCacheRef _textureCache; |
| 49 } |
| 50 |
| 51 - (instancetype)initWithContext:(GlContextType *)context { |
| 52 if (self = [super init]) { |
| 53 if (![self setupNV12Program] || ![self setupTextureCacheWithContext:context]
|| |
| 54 !RTCSetupVerticesForProgram(_nv12Program, &_vertexBuffer, nullptr)) { |
| 55 self = nil; |
| 56 } |
| 57 } |
| 58 return self; |
| 59 } |
| 60 |
| 61 - (void)dealloc { |
| 62 glDeleteProgram(_nv12Program); |
| 63 glDeleteBuffers(1, &_vertexBuffer); |
| 64 if (_textureCache) { |
| 65 CFRelease(_textureCache); |
| 66 _textureCache = nullptr; |
| 67 } |
| 68 } |
| 69 |
| 70 - (BOOL)setupNV12Program { |
| 71 _nv12Program = RTCCreateProgramFromFragmentSource(kNV12FragmentShaderSource); |
| 72 if (!_nv12Program) { |
| 73 return NO; |
| 74 } |
| 75 _ySampler = glGetUniformLocation(_nv12Program, "s_textureY"); |
| 76 _uvSampler = glGetUniformLocation(_nv12Program, "s_textureUV"); |
| 77 |
| 78 return (_ySampler >= 0 && _uvSampler >= 0); |
| 79 } |
| 80 |
| 81 - (BOOL)setupTextureCacheWithContext:(GlContextType *)context { |
| 82 CVReturn ret = CVOpenGLESTextureCacheCreate( |
| 83 kCFAllocatorDefault, NULL, |
| 84 #if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API |
| 85 context, |
| 86 #else |
| 87 (__bridge void *)context, |
| 88 #endif |
| 89 NULL, &_textureCache); |
| 90 return ret == kCVReturnSuccess; |
| 91 } |
| 92 |
| 93 - (BOOL)drawFrame:(RTCVideoFrame *)frame { |
| 94 CVPixelBufferRef pixelBuffer = frame.nativeHandle; |
| 95 RTC_CHECK(pixelBuffer); |
| 96 glUseProgram(_nv12Program); |
| 97 const OSType pixelFormat = CVPixelBufferGetPixelFormatType(pixelBuffer); |
| 98 RTC_CHECK(pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange || |
| 99 pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) |
| 100 << "Unsupported native pixel format: " << pixelFormat; |
| 101 |
| 102 // Y-plane. |
| 103 const int lumaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); |
| 104 const int lumaHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); |
| 105 |
| 106 CVOpenGLESTextureRef lumaTexture = nullptr; |
| 107 glActiveTexture(GL_TEXTURE0); |
| 108 glUniform1i(_ySampler, 0); |
| 109 CVReturn ret = CVOpenGLESTextureCacheCreateTextureFromImage( |
| 110 kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, |
| 111 RTC_PIXEL_FORMAT, lumaWidth, lumaHeight, RTC_PIXEL_FORMAT, |
| 112 GL_UNSIGNED_BYTE, 0, &lumaTexture); |
| 113 if (ret != kCVReturnSuccess) { |
| 114 CFRelease(lumaTexture); |
| 115 return NO; |
| 116 } |
| 117 |
| 118 RTC_CHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), |
| 119 CVOpenGLESTextureGetTarget(lumaTexture)); |
| 120 glBindTexture(GL_TEXTURE_2D, CVOpenGLESTextureGetName(lumaTexture)); |
| 121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 125 |
| 126 // UV-plane. |
| 127 const int chromaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1); |
| 128 const int chromeHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1); |
| 129 |
| 130 CVOpenGLESTextureRef chromaTexture = nullptr; |
| 131 glActiveTexture(GL_TEXTURE1); |
| 132 glUniform1i(_uvSampler, 1); |
| 133 ret = CVOpenGLESTextureCacheCreateTextureFromImage( |
| 134 kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, |
| 135 GL_LUMINANCE_ALPHA, chromaWidth, chromeHeight, GL_LUMINANCE_ALPHA, |
| 136 GL_UNSIGNED_BYTE, 1, &chromaTexture); |
| 137 if (ret != kCVReturnSuccess) { |
| 138 CFRelease(chromaTexture); |
| 139 CFRelease(lumaTexture); |
| 140 return NO; |
| 141 } |
| 142 |
| 143 RTC_CHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), |
| 144 CVOpenGLESTextureGetTarget(chromaTexture)); |
| 145 glBindTexture(GL_TEXTURE_2D, CVOpenGLESTextureGetName(chromaTexture)); |
| 146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 150 |
| 151 glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); |
| 152 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 153 |
| 154 CFRelease(chromaTexture); |
| 155 CFRelease(lumaTexture); |
| 156 |
| 157 return YES; |
| 158 } |
| 159 |
| 160 @end |
| 161 #endif // TARGET_OS_IPHONE |
OLD | NEW |