sws_scale 原始 yuv420p 数据
sws_scale on raw yuv420p data
我正在从我的 raspberry pi 中获取 yuv420p 格式的相机数据。我正在尝试使用 sws_scale
将它们转换为 RGBA 格式。这就是我初始化上下文的方式:
_sws_context = sws_getContext(CAMERA_WIDTH, CAMERA_HEIGHT, AV_PIX_FMT_YUV420P,
CAMERA_WIDTH, CAMERA_HEIGHT, AV_PIX_FMT_RGBA, 0, nullptr, nullptr, nullptr);
我现在对如何为 sws_scale
设置数据和行大小有点困惑。从相机我只是得到一个没有进一步结构的普通字节数组。我假设我必须以某种方式将其细分为平面。我的第一个方法是根本不把它分开,基本上有这样的东西(基于以下事实:
const uint8_t *src_data[] = {data.data()};
const int src_strides[] = {(int) std::ceil((CAMERA_WIDTH * 6) / 8)};
这是基于:
there are 12 bits for a 2x2 grid of pixels
所以我假设一行会使用一半。但这会导致分段错误。所以我想我必须以某种方式将 src_data
和 src_strides
拆分为各自的 YUV 平面,但我不确定如何执行此操作,尤其是因为 YUV420 数据的一个像素每个平面使用不到一个字节。 ..
原来比我想的还要简单!飞机一个接一个,这也让步伐非常明显:
const auto y = data.data();
const auto u = y + CAMERA_WIDTH * CAMERA_HEIGHT;
const auto v = u + (CAMERA_WIDTH * CAMERA_HEIGHT) / 4;
const auto stride_y = CAMERA_WIDTH;
const auto stride_u = CAMERA_WIDTH / 2;
const auto stride_v = CAMERA_WIDTH / 2;
我正在从我的 raspberry pi 中获取 yuv420p 格式的相机数据。我正在尝试使用 sws_scale
将它们转换为 RGBA 格式。这就是我初始化上下文的方式:
_sws_context = sws_getContext(CAMERA_WIDTH, CAMERA_HEIGHT, AV_PIX_FMT_YUV420P,
CAMERA_WIDTH, CAMERA_HEIGHT, AV_PIX_FMT_RGBA, 0, nullptr, nullptr, nullptr);
我现在对如何为 sws_scale
设置数据和行大小有点困惑。从相机我只是得到一个没有进一步结构的普通字节数组。我假设我必须以某种方式将其细分为平面。我的第一个方法是根本不把它分开,基本上有这样的东西(基于以下事实:
const uint8_t *src_data[] = {data.data()};
const int src_strides[] = {(int) std::ceil((CAMERA_WIDTH * 6) / 8)};
这是基于:
there are 12 bits for a 2x2 grid of pixels
所以我假设一行会使用一半。但这会导致分段错误。所以我想我必须以某种方式将 src_data
和 src_strides
拆分为各自的 YUV 平面,但我不确定如何执行此操作,尤其是因为 YUV420 数据的一个像素每个平面使用不到一个字节。 ..
原来比我想的还要简单!飞机一个接一个,这也让步伐非常明显:
const auto y = data.data();
const auto u = y + CAMERA_WIDTH * CAMERA_HEIGHT;
const auto v = u + (CAMERA_WIDTH * CAMERA_HEIGHT) / 4;
const auto stride_y = CAMERA_WIDTH;
const auto stride_u = CAMERA_WIDTH / 2;
const auto stride_v = CAMERA_WIDTH / 2;