FFMPEG / libav:UYVY422 是如何写在 AVFrame 结构中的?
FFMPEG / libav: How is UYVY422 written inside AVFrame structure?
我正在尝试将帧数据从 AVFrame
结构复制到缓冲区。我知道如何使用 YUV420P
格式来做到这一点,因为 Y 数据存储在 AVFrame frame->data[0]
中,U 数据存储在 AVFrame frame->data[1]
中,V 数据存储在 AVFrame frame->data[2]
中,所以它是易于 memcpy()
Y、U 和 V 数据分开 + 它是平面格式,所以我能够轻松地做到这一点:
for (y = 0; y < height; y++)
{
memcpy(buffer + y*frame->linesize[0], frame->data[0] + y*frame->linesize[0], width);
}
buffer += ySize;
for (y = 0; y < height / 2; y++)
{
memcpy(buffer + y*frame->linesize[1], frame->data[1] + y*frame->linesize[1], width / 2);
}
buffer += uSize;
for (y = 0; y < height / 2; y++)
{
memcpy(buffer + y*frame->linesize[2], frame->data[2] + y*frame->linesize[2], width / 2);
}
但是谈到 UYVY422
我不知道数据是如何存储在结构中的。我对 UYVY422
格式有一般了解,它的写法就像它的名字暗示的 UYVYUYVYUYVY... 等等。但我的问题是我如何知道 AVFrame frame->data[0]
、AVFrame frame->data[1]
和 AVFrame frame->data[2]
字段中存储了多少数据,以便我可以 memcpy()
确切数量的缓冲区?
对于 UYVY,数据专门存储在 frame->data[0] 中,每行你应该复制 width * 2 字节:
for (y = 0; y < height; y++)
{
memcpy(output_buffer + y*frame->linesize[0],
frame->data[0] + y*frame->linesize[0], width * 2);
}
如果您有兴趣,可以通过编程方式推导它。每个 AVPixelFormat
在这里都有一个 AVPixFmtDescriptor
that describes its packing in AVFrame->data[]
. To get yours, use av_pix_fmt_desc_get
(
AV_PIX_FMT_UYVY
)
. The returned item is this one (see struct reference for AVComponentDescriptor
)。你会看到 desc->nb_components
是 3,desc->log2_chroma_w
是 1,这意味着 U/V 被水平二次采样 1,而 desc->comp[0-2].plane
是 0,这意味着所有数据都在 AVFrame->data[0]
。 desc->comp[0-2]
中的 offset
/step
/depth
告诉你其余的,以防你想要一种完全动态的方式来阅读任何 pix_fmt。我不认为你个人需要它,但至少它允许任何人在 AVFrame->data[]
.
中导出任何 pix_fmt 的包装
[编辑] 请参阅以下示例代码(可能有错误):
#include <assert.h>
#include <stdio.h>
#include <libavutil/pixdesc.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s [fmt]\n", argv[0]);
return 1;
}
const char *fmtname = argv[1];
enum AVPixelFormat fmt = av_get_pix_fmt(fmtname);
if (fmt == AV_PIX_FMT_NONE) {
fprintf(stderr, "Unknown pixfmt %s\n", fmtname);
return 1;
}
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
assert(desc != NULL);
printf("N planes: %d, %d bits/element\n", desc->nb_components, desc->comp[0].depth);
int n;
int epl[4] = { 0, 0, 0, 0 };
int width = 0x100;
for (n = 0; n < desc->nb_components; n++) {
int is_y = n == 0;
int is_a = !(desc->nb_components & 1) && n == desc->nb_components - 1;
int h_ss = (is_y || is_a) ? 0 : desc->log2_chroma_w;
epl[desc->comp[n].plane] += width >> h_ss;
}
for (n = 0; n < 4; n++) {
int is_y = n == 0;
int is_a = !(desc->nb_components & 1) && n == desc->nb_components - 1;
int v_ss = (is_y || is_a) ? 0 : desc->log2_chroma_h;
if (epl[n] == 0) continue;
printf("Plane %d has %lf elements/y_pixel (horizontally) and %lf lines/y_pixel (vertically)\n",
n, epl[n] / (double) width, (width >> v_ss) / (double) width);
}
return 0;
}
给出以下输出:
$ for fmt in yuyv422 uyvy422 yuv420p yuva420p10; do /tmp/test $fmt; done
N planes: 3, 8 bits/element
Plane 0 has 2.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
N planes: 3, 8 bits/element
Plane 0 has 2.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
N planes: 3, 8 bits/element
Plane 0 has 1.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
Plane 1 has 0.500000 elements/y_pixel (horizontally) and 0.500000 lines/y_pixel (vertically)
Plane 2 has 0.500000 elements/y_pixel (horizontally) and 0.500000 lines/y_pixel (vertically)
N planes: 4, 10 bits/element
Plane 0 has 1.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
Plane 1 has 0.500000 elements/y_pixel (horizontally) and 0.500000 lines/y_pixel (vertically)
Plane 2 has 0.500000 elements/y_pixel (horizontally) and 0.500000 lines/y_pixel (vertically)
Plane 3 has 1.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
我正在尝试将帧数据从 AVFrame
结构复制到缓冲区。我知道如何使用 YUV420P
格式来做到这一点,因为 Y 数据存储在 AVFrame frame->data[0]
中,U 数据存储在 AVFrame frame->data[1]
中,V 数据存储在 AVFrame frame->data[2]
中,所以它是易于 memcpy()
Y、U 和 V 数据分开 + 它是平面格式,所以我能够轻松地做到这一点:
for (y = 0; y < height; y++)
{
memcpy(buffer + y*frame->linesize[0], frame->data[0] + y*frame->linesize[0], width);
}
buffer += ySize;
for (y = 0; y < height / 2; y++)
{
memcpy(buffer + y*frame->linesize[1], frame->data[1] + y*frame->linesize[1], width / 2);
}
buffer += uSize;
for (y = 0; y < height / 2; y++)
{
memcpy(buffer + y*frame->linesize[2], frame->data[2] + y*frame->linesize[2], width / 2);
}
但是谈到 UYVY422
我不知道数据是如何存储在结构中的。我对 UYVY422
格式有一般了解,它的写法就像它的名字暗示的 UYVYUYVYUYVY... 等等。但我的问题是我如何知道 AVFrame frame->data[0]
、AVFrame frame->data[1]
和 AVFrame frame->data[2]
字段中存储了多少数据,以便我可以 memcpy()
确切数量的缓冲区?
对于 UYVY,数据专门存储在 frame->data[0] 中,每行你应该复制 width * 2 字节:
for (y = 0; y < height; y++)
{
memcpy(output_buffer + y*frame->linesize[0],
frame->data[0] + y*frame->linesize[0], width * 2);
}
如果您有兴趣,可以通过编程方式推导它。每个 AVPixelFormat
在这里都有一个 AVPixFmtDescriptor
that describes its packing in AVFrame->data[]
. To get yours, use av_pix_fmt_desc_get
(
AV_PIX_FMT_UYVY
)
. The returned item is this one (see struct reference for AVComponentDescriptor
)。你会看到 desc->nb_components
是 3,desc->log2_chroma_w
是 1,这意味着 U/V 被水平二次采样 1,而 desc->comp[0-2].plane
是 0,这意味着所有数据都在 AVFrame->data[0]
。 desc->comp[0-2]
中的 offset
/step
/depth
告诉你其余的,以防你想要一种完全动态的方式来阅读任何 pix_fmt。我不认为你个人需要它,但至少它允许任何人在 AVFrame->data[]
.
[编辑] 请参阅以下示例代码(可能有错误):
#include <assert.h>
#include <stdio.h>
#include <libavutil/pixdesc.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s [fmt]\n", argv[0]);
return 1;
}
const char *fmtname = argv[1];
enum AVPixelFormat fmt = av_get_pix_fmt(fmtname);
if (fmt == AV_PIX_FMT_NONE) {
fprintf(stderr, "Unknown pixfmt %s\n", fmtname);
return 1;
}
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
assert(desc != NULL);
printf("N planes: %d, %d bits/element\n", desc->nb_components, desc->comp[0].depth);
int n;
int epl[4] = { 0, 0, 0, 0 };
int width = 0x100;
for (n = 0; n < desc->nb_components; n++) {
int is_y = n == 0;
int is_a = !(desc->nb_components & 1) && n == desc->nb_components - 1;
int h_ss = (is_y || is_a) ? 0 : desc->log2_chroma_w;
epl[desc->comp[n].plane] += width >> h_ss;
}
for (n = 0; n < 4; n++) {
int is_y = n == 0;
int is_a = !(desc->nb_components & 1) && n == desc->nb_components - 1;
int v_ss = (is_y || is_a) ? 0 : desc->log2_chroma_h;
if (epl[n] == 0) continue;
printf("Plane %d has %lf elements/y_pixel (horizontally) and %lf lines/y_pixel (vertically)\n",
n, epl[n] / (double) width, (width >> v_ss) / (double) width);
}
return 0;
}
给出以下输出:
$ for fmt in yuyv422 uyvy422 yuv420p yuva420p10; do /tmp/test $fmt; done
N planes: 3, 8 bits/element
Plane 0 has 2.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
N planes: 3, 8 bits/element
Plane 0 has 2.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
N planes: 3, 8 bits/element
Plane 0 has 1.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
Plane 1 has 0.500000 elements/y_pixel (horizontally) and 0.500000 lines/y_pixel (vertically)
Plane 2 has 0.500000 elements/y_pixel (horizontally) and 0.500000 lines/y_pixel (vertically)
N planes: 4, 10 bits/element
Plane 0 has 1.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)
Plane 1 has 0.500000 elements/y_pixel (horizontally) and 0.500000 lines/y_pixel (vertically)
Plane 2 has 0.500000 elements/y_pixel (horizontally) and 0.500000 lines/y_pixel (vertically)
Plane 3 has 1.000000 elements/y_pixel (horizontally) and 1.000000 lines/y_pixel (vertically)