在Android中将YUV数据绘制到Surface的最佳方式是什么?

问题描述 投票:0回答:1

我需要以特定方式播放视频。这就是为什么我需要“预解码”一些帧并保留它并稍后显示。我使用 MediaCodec 来解码视频帧。输出缓冲区是 YUV(420) 格式,问题是,稍后在屏幕上显示它们的最佳方式是什么?

  1. 复制编解码器的输出更好。GetOutputBuffer() 还是保留代码。GetOutputImage()?
  2. 是否有硬件加速方法可以作为 Java 函数从 YUV 转换为 RGB?
  3. 如何高效地将数据绘制出来?
  4. 是否有必要进行转换?是否可以使用较新的(>=8.0)Android版本直接将YUV数据(以任何形式,如ByteBuffer或Image或其他形式)绘制到Surface?
  5. 无法指示 mp4 编解码器产生 RGB 输出而不是 YUV,对吗?

我浏览了许多类似(或相同)的问题,但大多数都是旧问题,我只是看到一些手动 YUV 到 RGB 转换,这对我来说似乎太慢了。我希望新的 Android 版本的情况能好一点;)

感谢您的任何想法。

android android-mediacodec yuv hardware-acceleration
1个回答
1
投票

从 RGB 转换为 yuv 并将其转换回 RGB 的着色器示例。 为了简单起见,我将 2 个相邻的 rgba 像素转换为 yuv 并将其保存为 (y1y2uv) 格式的 1 个像素。 这不是一个有效的示例,但我希望它能让您进入正确的方向。

// From rgb to yuv
companion object {
    val FRAGMENT_SHADER = "" +
        "precision highp float;\n" +
        "varying vec2 textureCoordinate;\n" +
        " \n" +
        "uniform sampler2D inputImageTexture;\n" +
        "uniform float step;\n" +
        "const vec3 multiplier = vec3(0.299, 0.587, 0.114);\n" +
        " \n" +
        "void main()\n" +
        "{\n" +
        "     vec4 first= texture2D(inputImageTexture, textureCoordinate);\n" +
        "     float y1 = clamp(dot(multiplier, first.rgb), 0.0, 1.0);\n" +
        "     vec2 secondCoord = textureCoordinate;\n" +
        "     secondCoord.x = secondCoord.x + step;\n" +
        "     vec4 second= texture2D(inputImageTexture, secondCoord);\n" +
        "     float y2 = clamp(dot(multiplier, second.rgb), 0.0, 1.0);\n" +
        "     vec3 rgbAve = mix(first.rgb, second.rgb, 0.5);\n" +
        "     float yAve = 0.5 * (y1 + y2);\n" +
        "     float u = clamp((rgbAve.b - yAve) * 0.565 + 0.5, 0.0, 1.0);\n" +
        "     float v = clamp((rgbAve.r - yAve) * 0.713 + 0.5, 0.0, 1.0);\n" +
        "     gl_FragColor.r = y1;\n" +
        "     gl_FragColor.g = y2;\n" +
        "     gl_FragColor.b = u;\n" +
        "     gl_FragColor.a = v;\n" +
        "}"
}

// From yuv to rgb
companion object {
    val FRAGMENT_SHADER = "" +
        "precision highp float;\n" +
        "varying vec2 textureCoordinate;\n" +
        " \n" +
        "uniform sampler2D inputImageTexture;\n" +
        "uniform float step;\n" +
        " \n" +
        "void main()\n" +
        "{\n" +
        "     vec2 secondCoord = textureCoordinate;\n" +
        "     float correction = secondCoord.x - 2.0 * step * floor(secondCoord.x / (2.0 * step));\n" +
        "     correction = clamp((correction - (step/2.0))/ (step/2.0), 0.0, 1.0);\n" +
        "     secondCoord.x = secondCoord.x - (correction * step);\n" +
        "     vec4 yuv = texture2D(inputImageTexture, secondCoord);\n" +
        "     yuv.r = (1.0 - correction) * yuv.r + correction * yuv.g;\n" +
        "     yuv.g = yuv.b - 0.5;\n" +
        "     yuv.b = yuv.a - 0.5;\n" +
        "     gl_FragColor.r = clamp(yuv.r + 1.403 * yuv.b, 0.0, 1.0);\n" +
        "     gl_FragColor.g = clamp(yuv.r - 0.344 * yuv.g - 0.714 * yuv.b, 0.0, 1.0);\n" +
        "     gl_FragColor.b = clamp(yuv.r + 1.770 * yuv.g, 0.0, 1.0);\n" +
        "     gl_FragColor.a = 1.0;\n" +
        "}"
}
© www.soinside.com 2019 - 2024. All rights reserved.