问题描述
我几乎可以肯定这比我想象的要简单得多,但是我一直在互联网上搜索的时间比我承认试图弄清楚如何转换两种格式。我能够从无符号字节流(unsigned char)中提取Y0,Cb,Y1,Cr数据,但我不知道这些字节是如何排列在 - 这个文件暗示各种值实际上包含在不同的行中吗?
I am almost sure that this is much simpler than I think it is, but I have been scouring the internet for a much longer time than I care to admit to try and figure out how the frig to convert the two formats. I am able to extract the Y0, Cb, Y1, Cr data from a stream of unsigned bytes (unsigned char), but I have no idea how these bytes are arranged in YV12 - is this document implying that the various values are actually contained in different rows?
我是字面上一直在寻找像c ++转换YUY2到YV12这样的东西,并且完全没有教程或代码示例。我认为这会有一些我可以使用的文档形式,但是这个特定主题的信息似乎很少。
I've literally been searching all day for things like "c++ convert YUY2 to YV12," and have turned up absolutely no tutorials or code samples. I would think that this would have some form of documentation that I could use, but the information seems to be scarce on this particular subject.
推荐答案
看起来像以及非常清楚:
Looks like the linked entry on YUY2 together with the Wikipedia article on YV12 makes this pretty clear:
-
YUY2将每两个相邻的水平像素存储为四个字节,
[Y1,U,Y2,V]
。
YV12在一个连续的数组中存储一个完整的 M × N 框架 M * N + 2 *(M / 2 * N / 2)
字节。我们调用数组字节帧[M * N * 3/2]
。我们有:
YV12 stores an entire M × N frame in a contiguous array of M*N + 2 * (M/2 * N/2)
bytes. Let's call the array byte frame[M * N * 3 / 2]
. We have:
-
frame [i]
fori
是 Y - 像素的值。[0,M * N)
中的 -
frame [j]
forj
in[M * N,M * N * 5/4)
是每个2 × 2像素磁贴的 V 值。 -
frame [j]
fork
in[M * N * 5/4,M * N * 6/4)
是每个2 × 2像素磁贴的 U 值。
frame[i]
fori
in[0, M * N)
are the Y-values of the pixels.frame[j]
forj
in[M * N, M * N * 5/4)
are the V-values of each 2 × 2-pixel tile.frame[j]
fork
in[M * N * 5/4, M * N * 6/4)
are the U-values of each 2 × 2-pixel tile.
因此,当您从YUY2转换为YV12时,您必须将 U 的数量减半 - 和 V -data,可能取两个相邻行的平均值。
So as you convert from YUY2 to YV12, you have to halve the amount of U- and V-data, possibly by taking the average of two adjacent rows.
示例:
byte YUY2Source[M * N * 2] = /* source frame */;
byte YV12Dest[M * N * 3/2];
for (unsigned int i = 0; i != M * N; ++i)
{
YV12Dest[i] = YUY2Source[2 * i];
}
for (unsigned int j = 0; j != M * N / 4; ++j)
{
YV12Dest[M * N + j] = ( YUY2Source[N*(j / N/2 ) + 4 * j + 3]
+ YUY2Source[N*(j / N/2 + 1) + 4 * j + 3] ) / 2;
YV12Dest[M * N * 5/4 + j] = ( YUY2Source[N*(j / N/2 ) + 4 * j + 1]
+ YUY2Source[N*(j / N/2 + 1) + 4 * j + 1] ) / 2;
}
这篇关于将YUY2转换为YV12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!