从 jpeg 文件生成 AC 元素
我正在解码 jpeg 文件。我已经生成了霍夫曼表和量化表,并且已经达到了必须解码直流和交流元素的程度。例如,假设我有下一个数据,
FFDA 00 0C 03 01 00 02 11 03 11 00 3F 00 F2 A6 2A FD 54 C5 5F FFD9
如果我们忽略 SOS 标记中的几个字节,我的真实数据将从 F2
字节开始。因此,让我们将其写成二进制(从 F2 字节开始):
1111 0010 1010 0110 0010 1010 1111 1101 0101 0100 1100 0101 0101 1111
F 2 A 6 2 A F D 5 4 C 5 5 F
解码时,第一个元素是亮度 DC 元素,所以让我们对其进行解码。
[1111 0]010 1010 0110 0010 1010 1111 1101 0101 0100 1100 0101 0101 1111
F 2 A 6 2 A F D 5 4 C 5 5 F
所以 11110 是元素 08
的霍夫曼代码(在我的例子中)。这意味着接下来的 8 位是我的 DC 值。当我取接下来的 8 位时,值为:
1111 0[010 1010 0]110 0010 1010 1111 1101 0101 0100 1100 0101 0101 1111
F 2 A 6 2 A F D 5 4 C 5 5 F
DC 元素值为 -171。
这是我的问题:接下来是亮度 AC 值,但我真的不明白 AC 非零情况下的标准?嗯!
I'm decoding jpeg file. I have generated huffman tables, and quantization tables, and I have reach the point where I have to decode DC and AC elements. For example lets say I have next data
FFDA 00 0C 03 01 00 02 11 03 11 00 3F 00 F2 A6 2A FD 54 C5 5F FFD9
If we ignore few bytes from SOS marker, my real data is starting from F2
byte. So lets write it in binary (starting from F2 byte):
1111 0010 1010 0110 0010 1010 1111 1101 0101 0100 1100 0101 0101 1111
F 2 A 6 2 A F D 5 4 C 5 5 F
When decoding, first element is luminance DC element so let's decode it.
[1111 0]010 1010 0110 0010 1010 1111 1101 0101 0100 1100 0101 0101 1111
F 2 A 6 2 A F D 5 4 C 5 5 F
So 11110 is Huffman code (in my case) for element 08
. This means that next 8 bits are my DC value. When I take next 8 bits the value is:
1111 0[010 1010 0]110 0010 1010 1111 1101 0101 0100 1100 0101 0101 1111
F 2 A 6 2 A F D 5 4 C 5 5 F
DC element value is -171.
Here is my problem: next is luminance AC value, but I don't really understand standard in a case when is AC non zero? Tnx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所看到的,DC 值定义为指定正或负 DC 值的“额外”位数。 AC 系数的编码方式不同,因为它们大多数为 0。霍夫曼表使用“跳过”值和“额外位”长度定义 AC 系数的每个条目。跳过值是在存储该值之前要跳过多少个 AC 系数,并且额外的位的处理方式与 DC 值相同。解码 AC 系数时,您可以解码从 1 到 63 的值,但 MCU 的编码结束方式可能会有所不同。您可以将实际值存储在索引 63 处,或者如果您位于索引 > 处,则可以将实际值存储在索引处。 48,您可以获得 ZRL(零游程长度 = 16 个零),或任何带您越过终点的组合。简化的解码循环:
颜色分量可以交错(典型)或存储在单独的扫描中。这些元素在每个 MCU 中以锯齿形顺序编码(低频元素优先)。定义 MCU 的 8x8 系数块的数量根据颜色子采样而变化。对于 1:1,将有 1 个 Y,后跟 1 个 Cr 和 1 个 Cb。对于典型的数码相机图像,水平轴被二次采样,因此您将获得 2 个 Y 块,后跟 1 个 Cr 和 1 个 Cb。压缩图像的质量设置决定了所使用的量化表以及编码了多少个零AC系数。质量越低,每个 MCU 的零值就越多。当您在 MCU 上执行逆 DCT 时,零的数量将决定 8x8、16x8、8x16 或 16x16 像素块中保留多少细节。基本步骤如下:
1)对8x8的系数块进行熵解码,每个颜色分量单独存储
2) 对系数进行去锯齿形和去量化
3) 对系数执行逆 DCT(对于 4:2:0 子采样可能是 6 个 8x8 块)
4) 将颜色空间从 YCrCb 转换为 RGB 或任何您需要的
The DC values, as you've seen, are defined as the number of "extra" bits which specify the positive or negative DC value. The AC coefficients are encoded differently because most of them are 0. The Huffman table defines each entry for AC coefficients with a "skip" value and an "extra bits" length. The skip value is how many AC coefficients to skip before storing the value, and the extra bits are treated the same way as DC values. When decoding AC coefficients, you decode values from 1 to 63, but the way the encoding of the MCU ends can vary. You can have an actual value stored at index 63 or at if you're at index > 48, you could get a ZRL (zero run length = 16 zeros), or any combination which takes you past the end. A simplified decode loop:
The color components can be interleaved (typical) or stored in separate scans. The elements are encoded in zigzag order in each MCU (low frequency elements first). The number of 8x8 blocks of coefficients which define an MCU varies depending on the color subsampling. For 1:1, there will be 1 Y followed by 1 Cr and 1 Cb. For typical digital camera images, the horizontal axis is subsampled, so you will get 2 Y blocks followed by 1 Cr and 1 Cb. The quality setting of the compressed image determines the quantization table used and how many zero AC coefficients are encoded. The lower the quality, the more of each MCU will be zeros. When you do the inverse DCT on your MCU, the number of zeros will determine how much detail is preserved in your 8x8, 16x8, 8x16 or 16x16 block of pixels. Here are the basic steps:
1) Entropy decode the 8x8 coefficient blocks, each color component is stored separately
2) De-zigzag and de-quantize the coefficients
3) Perform inverse DCT on the coefficients (might be 6 8x8 blocks for 4:2:0 subsampling)
4) Convert the colorspace from YCrCb to RGB or whatever you need