YUV420p 上的大量绿色 --> iOS 上 OpenGL 2.0 着色器中的 RGB

2024-04-29

我想使用 ffmpeg 和 OpenGL ES 2.0 为 iOS 制作一个电影播放器 但我有一些问题。输出 RGB 图像有很多绿色。 这是代码和图像

  • 480x320 宽度和高度:
  • 512x512 纹理宽度和高度

我从 ffmpeg AVFrame 获得了 YUV420p 行数据。

    for (int i = 0, nDataLen = 0; i < 3; i++) {
        int nShift = (i == 0) ? 0 : 1;
        uint8_t *pYUVData = (uint8_t *)_frame->data[i];
        for (int j = 0; j < (mHeight >> nShift); j++) {
            memcpy(&pData->pOutBuffer[nDataLen], pYUVData, (mWidth >> nShift));
            pYUVData += _frame->linesize[i];
            nDataLen += (mWidth >> nShift);
        }
    }

并为Y、U和V通道准备纹理。

//: U Texture
    if (sampler1Texture) glDeleteTextures(1, &sampler1Texture);

    glActiveTexture(GL_TEXTURE1);
    glGenTextures(1, &sampler1Texture);
    glBindTexture(GL_TEXTURE_2D, sampler1Texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // This is necessary for non-power-of-two textures
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glEnable(GL_TEXTURE_2D);

    glTexImage2D(GL_TEXTURE_2D, 
                 0, 
                 GL_LUMINANCE, 
                 texW / 2, 
                 texH / 2, 
                 0, 
                 GL_LUMINANCE, 
                 GL_UNSIGNED_BYTE,
                 NULL);

    //: V Texture
    if (sampler2Texture) glDeleteTextures(1, &sampler2Texture);

    glActiveTexture(GL_TEXTURE2);
    glGenTextures(1, &sampler2Texture);
    glBindTexture(GL_TEXTURE_2D, sampler2Texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // This is necessary for non-power-of-two textures
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glEnable(GL_TEXTURE_2D);

    glTexImage2D(GL_TEXTURE_2D, 
                 0, 
                 GL_LUMINANCE, 
                 texW / 2, 
                 texH / 2, 
                 0, 
                 GL_LUMINANCE,
                 GL_UNSIGNED_BYTE,
                 NULL);

    //: Y Texture
    if (sampler0Texture) glDeleteTextures(1, &sampler0Texture);

    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &sampler0Texture);
    glBindTexture(GL_TEXTURE_2D, sampler0Texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // This is necessary for non-power-of-two textures
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glEnable(GL_TEXTURE_2D);

    glTexImage2D(GL_TEXTURE_2D, 
                 0, 
                 GL_LUMINANCE, 
                 texW, 
                 texH, 
                 0, 
                 GL_LUMINANCE,
                 GL_UNSIGNED_BYTE, 
                 NULL);

渲染部分如下。

int _idxU = mFrameW * mFrameH;
int _idxV = _idxU + (_idxU / 4);

// U data
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, sampler1Texture);
glUniform1i(sampler1Uniform, 1);

glTexSubImage2D(
                GL_TEXTURE_2D, 
                0, 
                0, 
                0, 
                mFrameW / 2,            // source width
                mFrameH / 2,            // source height
                GL_LUMINANCE,
                GL_UNSIGNED_BYTE, 
                &_frameData[_idxU]);

// V data
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, sampler2Texture);
glUniform1i(sampler2Texture, 2);

glTexSubImage2D(
                GL_TEXTURE_2D, 
                0, 
                0, 
                0, 
                mFrameW / 2,            // source width
                mFrameH / 2,            // source height
                GL_LUMINANCE,
                GL_UNSIGNED_BYTE,
                &_frameData[_idxV]);

// Y data
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sampler0Texture);
glUniform1i(sampler0Uniform, 0);

glTexSubImage2D(
                GL_TEXTURE_2D, 
                0, 
                0, 
                0, 
                mFrameW,            // source width
                mFrameH,            // source height
                GL_LUMINANCE,
                GL_UNSIGNED_BYTE,
                _frameData);

顶点着色器和片段着色器如下。

attribute vec4 Position;
attribute vec2 TexCoordIn;

varying vec2 TexCoordOut;
varying vec2 TexCoordOut_UV;

uniform mat4 Projection;
uniform mat4 Modelview;

void main()
{
    gl_Position = Projection * Modelview * Position;
    TexCoordOut = TexCoordIn;
}



uniform sampler2D sampler0; // Y Texture Sampler
uniform sampler2D sampler1; // U Texture Sampler
uniform sampler2D sampler2; // V Texture Sampler

varying highp vec2 TexCoordOut;

void main()
{
    highp float y = texture2D(sampler0, TexCoordOut).r;
    highp float u = texture2D(sampler2, TexCoordOut).r - 0.5;
    highp float v = texture2D(sampler1, TexCoordOut).r - 0.5;

    //y = 0.0;
    //u = 0.0;
    //v = 0.0;

    highp float r = y + 1.13983 * v;
    highp float g = y - 0.39465 * u - 0.58060 * v;
    highp float b = y + 2.03211 * u;

    gl_FragColor = vec4(r, g, b, 1.0);
}

Y 纹理(灰度)是正确的,但 U 和 V 有很多绿色。 所以最终的 RGB 图像 (Y+U+V) 有很多绿色。 有什么问题?

请帮忙。 谢谢。


Change u and v制服(反之亦然),你就会得到正确的结果。

所以像素着色器(保持不变):

uniform sampler2D sampler0; // Y Texture Sampler
uniform sampler2D sampler1; // U Texture Sampler
uniform sampler2D sampler2; // V Texture Sampler

varying highp vec2 TexCoordOut;

void main()
{
    highp float y = texture2D(sampler0, TexCoordOut).r;
    highp float u = texture2D(sampler2, TexCoordOut).r - 0.5;
    highp float v = texture2D(sampler1, TexCoordOut).r - 0.5;

    highp float r = y + 1.13983 * v;
    highp float g = y - 0.39465 * u - 0.58060 * v;
    highp float b = y + 2.03211 * u;

    gl_FragColor = vec4(r, g, b, 1.0);
}

和渲染代码:

// RENDERING

    int _idxU = mFrameW * mFrameH;
    int _idxV = _idxU + (_idxU / 4);

    // U data
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, sampler1Texture);

    GLint sampler1Uniform = glGetUniformLocation(programStandard, "sampler2");

    glUniform1i(sampler1Uniform, 1);

    glTexSubImage2D(
                    GL_TEXTURE_2D,
                    0,
                    0,
                    0,
                    mFrameW / 2,            // source width
                    mFrameH / 2,            // source height
                    GL_LUMINANCE,
                    GL_UNSIGNED_BYTE,
                    &_frameData[_idxU]);

    // V data
    glActiveTexture(GL_TEXTURE2);
    glBindTexture(GL_TEXTURE_2D, sampler2Texture);

    GLint sampler2Uniform = glGetUniformLocation(programStandard, "sampler1");
    glUniform1i(sampler2Uniform, 2);

    glTexSubImage2D(
                    GL_TEXTURE_2D,
                    0,
                    0,
                    0,
                    mFrameW / 2,            // source width
                    mFrameH / 2,            // source height
                    GL_LUMINANCE,
                    GL_UNSIGNED_BYTE,
                    &_frameData[_idxV]);

    // Y data
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, sampler0Texture);

    GLint sampler0Uniform = glGetUniformLocation(programStandard, "sampler0");
    glUniform1i(sampler0Uniform, 0);

    glTexSubImage2D(
                    GL_TEXTURE_2D,
                    0,
                    0,
                    0,
                    mFrameW,            // source width
                    mFrameH,            // source height
                    GL_LUMINANCE,
                    GL_UNSIGNED_BYTE,
                    _frameData);


    //draw RECT
    glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, squareVertices);
    glEnableVertexAttribArray(ATTRIB_VERTEX);

    //ATTRIB_TEXTUREPOSITON
    glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureCoords);
    glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    free(_frameData);

    [(EAGLView *)self.view presentFramebuffer];

结论:u <-> v制服。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

YUV420p 上的大量绿色 --> iOS 上 OpenGL 2.0 着色器中的 RGB 的相关文章

  • 由于没有系统应用程序而终止

    这个错误让我发疯 我有一个按钮 我想移动到视图控制器 如果我将其设置为初始视图控制器 我可以打开该视图控制器 但现在我通过单击按钮来调用它 视图控制器包含一个自定义视图 我在绘制矩形上放置了一个断点 当绘制矩形完成时 模拟器终止 我在屏幕上
  • IOS 8 CLLocationManager 问题(授权不起作用)

    import MyLocationViewController h define NSLog FORMAT printf s n NSString stringWithFormat FORMAT VA ARGS UTF8String int
  • UIView 和 UITableView 中的 UITapGestureRecognizer 冲突

    我有一个UIView我在其中添加了一个UITapGestureRecognizer 在该视图中 我还有一个子视图 其中基本上是某种UITableView 问题是为什么不UITableView识别连续点击 而是始终转到点击手势识别器的处理程序
  • 如何让应用更新以吸引人的屏幕形式提供给用户? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我最近在使用 Make My Trip 应用程序 我发现每当我启动应用程序时都会出现一个非常有吸引力的应用程序更新弹出屏幕 它说要更新应用程
  • prepareForSegue 和 PerformSegueWithIdentifier 发送方

    我想知道标题中的函数如何工作以及发送者参数 假设单击按钮调用了performSegue方法 那么这是否也调用了prepareSegue方法 是否在performSegue方法之前但在按下按钮之后调用prepareSegue方法 另外 这两个
  • 如何为移动应用程序创建无密码登录

    我有兴趣在移动应用程序和 API 之间构建某种无密码登录 假设我可以控制两者 动机是必须登录对用户来说非常烦人并且存在安全风险 例如 用户将重复使用现有密码 我希望用户能够立即开始使用该应用程序 我想知道是否有一些可行的技术 例如 在移动设
  • 方法调用中的插入符[重复]

    这个问题在这里已经有答案了 我正在阅读本教程 并遇到了这行代码 这让我感到困惑 localSearch startWithCompletionHandler MKLocalSearchResponse response NSError er
  • 如何从 Xib 文件加载自定义 UITableViewCell?

    问题很简单 如何加载自定义UITableViewCell来自 Xib 文件 这样做可以让您使用 Interface Builder 来设计您的单元 由于内存管理问题 答案显然并不简单 这个线程 https stackoverflow com
  • 从 AVAudioRecorder 获取分贝

    我正在尝试从 AVAudio 录音机获取分贝值 这是我目前的代码 我有一个启动录音机的方法 然后是一个读取分贝值的方法 var recorder AVAudioRecorder 全局定义的记录器 然后在此处使用 func init reco
  • 使用 iPhone 控制蓝牙音频设备

    我正在寻找为 iPhone 编写应用程序 它将能够控制汽车中的收音机和 CD 播放器 收音机和播放器具有可用的蓝牙连接 我开始这个问题是为了获得这个地方所需的所有信息 我有几个问题 但如果您发现任何我没有要求的对我开始开发此应用程序不重要的
  • 如何快速防止标签中出现孤儿?

    我有一个可以有一两行的标签 如果它有两行 我希望第二行至少有两个 或者可能三个 单词 而不仅仅是一个 关于如何使用 swift 实现这一点有什么想法吗 提前致谢 Daniel 编辑 我删除了我愚蠢的第一个想法 这些想法并没有真正的帮助 好吧
  • 返回一个dispatch_async获取的变量[重复]

    这个问题在这里已经有答案了 基本上 一个方法需要返回一个在dispatch async中获取的NSDictionary 这是我尝试过的 NSDictionary fetchNSDictionary dispatch queue t Queu
  • Cordova Phonegap“导出失败”错误代码 70 构建 ios 时

    我目前正在使用 Cordova Phonegap 构建 iOS 应用程序 本来工作正常 但现在运行时出现错误cordova build ios在终端中 我收到以下错误 导出失败 错误 命令的错误代码 70 带有参数的 xcodebuild
  • 如何在 swift 中以编程方式使用坐标打开地图应用程序?

    我想在地图应用程序中打开纬度和经度 我尝试了这段代码HERE https stackoverflow com questions 12504294 programmatically open maps app in ios 6 func g
  • 尝试注册 RCTBridgeModule 类 RCTFileReaderModule

    尝试为名称 FileReaderModule 注册 RCTBridgeModule 类 RCTFileReaderModule 但该名称已由类 FileReaderModule 注册 尝试使用命令react native run ios在i
  • TableView 中图像的大小不正确

    我正在使用来自 URL 的图像创建一个表视图 但图像不会调整到所有视图的大小 直到我将其按入行中 知道为什么会发生这种情况吗 这是一个自定义的表格视图 我的代码是 UITableViewCell tableView UITableView
  • Xamarin.Forms DataTemplateSelector 不适用于 iOS(未调用构造函数)

    我正在膨胀数据模板选择器 如下所示
  • GoogleSignIn ios 附加到谷歌表格

    我目前正在开发一个 iOS 应用程序 该应用程序需要写入登录用户拥有的 Google 工作表 要登录我正在使用的用户GoogleSignInpod 并附加到我正在使用的谷歌表GoogleAPIClientForREST Sheets pod
  • 如何在运行仅 iPhone 应用程序(非通用)的 iPad 上的 UIPickerView 中支持 VoiceOver?

    一般来说 我会查看 Apple 的 UICatalog 示例代码以获取基本的 VoiceOver 支持 但示例代码中似乎有对 UIPickerViews 的 VoiceOver 支持 我是否需要在某处提供accessibilityLabel
  • 弱变量中间为零

    弱变量什么时候变为零 weak var backgroundNode SKSpriteNode texture SKTexture image initialBackgroundImage backgroundNode position C

随机推荐