gpt4 book ai didi

android - SDL_LockTexture() 返回意外的间距值

转载 作者:行者123 更新时间:2023-11-29 16:45:57 26 4
gpt4 key购买 nike

我正在使用 SDL2.0.7(Nexus 5、armeabi-v7a、ndk 16.1)在 native 应用程序上显示 Android Camera 预览。

我使用的是 N21 YUV 图像格式,我希望在创建该格式的 SDL_Texture 时返回 pitch 值通过 SDL_LockTexture() 会比我现在得到的更好。

示例代码:

SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_NV21, SDL_TEXTUREACCESS_STREAMING, 720, 480);
int res = SDL_LockTexture(texture, nullptr, &pixels, &pitch);

我期望获得 720 * 12/8 的音高值,即 1080,其中 720 是一行的宽度,12 是单个 YUV N21 像素的位数,8 是一个字节中的位数,因为 the SDL2 documentation指出:

pitch: this is filled in with the pitch of the locked pixels; the pitch is the length of one row in bytes

现在,函数调用实际返回的 pitch 值为 720。事实是,如果我使用 720 值,相机预览将以不自然的绿色优势显示,而如果我忽略 720 间距并使用我预期的 1080 间距,相机预览效果不错(应用程序运行也很流畅)。

我的假设有什么问题吗(例如,N21 像素的位数是 12,正如 Android 的 ImageFormat.getBitsPerPixel() 所建议的那样)?我是否以错误的方式使用了 SDL 库?

最佳答案

流式纹理的创建纹理实用程序在内部执行此操作:

// ...

if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
size_t size;
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
size = texture->h * data->pitch;
if (data->yuv) {
/* Need to add size for the U and V planes */
size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
}
if (data->nv12) {
/* Need to add size for the U/V plane */
size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
}
data->pixel_data = SDL_calloc(1, size);
if (!data->pixel_data) {
SDL_free(data);
return SDL_OutOfMemory();
}
}

// ...

在您的情况下,因为您使用的是 SDL_PIXELFORMAT_NV21yuv 为 false 而 nv12 为 true(如果听起来不错,请使用调试器尝试奇怪)。
这意味着分配的大小不是 pitch * height。相反,它是:

(pitch * height) + 2 * ((height + 1) / 2) * ((pitch + 1) / 2);

在这种情况下,pitch 等于 width

归根结底,它是图像大小的 1.5 倍,即每像素 12 位。


可以肯定的是,pitch 的值在某种程度上是错误的,因为它等于width
实际上,由于分配的空间,它应该可能是您预期的图像宽度的 1.5 倍。
但是我不太确定,因为我不是 SDL2 的专家。

关于android - SDL_LockTexture() 返回意外的间距值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48079166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com