为什么有Synchsafe Integer? [英] Why are there Synchsafe Integer?

查看:160
本文介绍了为什么有Synchsafe Integer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用c ++阅读mp3文件。

I started reading mp3-files in c++.

一切顺利,直到我阅读了ID3-Tag的规格。 ID3v2-Header中有一些关于它的大小存储在所谓的Synchsafe Integer中的信息。这是一个四字节整数,其中每个字节的最高有效位设置为零。

All went well until i read the specs of the ID3-Tag. There are some information in ID3v2-Header about it's size stored in so called Synchsafe Integer. That is a four byte integer where the most significant bit of each byte is set to zero.

我发现如何将它转换为ordenaty整数,但我不能停止问自己为什么整数值以这种不必要的复杂方式存储。

I found out how to convert it to an ordenaty integer, but i can not stop asking myself why an integer value is stored in such an unnecessery complicated way.

我希望有人可以告诉我为什么以这种方式存储它。

I hope there is somone who can tell me why it is stored this way.

推荐答案

要理解为什么使用同步安全整数,了解一下MP3数据的格式以及MP3的方式会有所帮助文件由媒体播放器播放。 MP3数据作为一系列帧存储在文件中。每帧包含一小部分以MP3格式编码的数字音乐以及有关帧本身的一些元数据。在每个MP3帧的开头是11位(有时是12)全部设置为1.这称为同步,它是媒体播放器在尝试播放MP3文件或流时所寻找的模式。如果播放器找到这个11位序列,那么它就知道它找到了一个可以解码和播放的MP3帧。

To understand why sync-safe integers are used, it's helpful to understand a little about the format of MP3 data as well as how an MP3 file is played by a media player. MP3 data is stored in a file as a series of frames. Each frame contains a small bit of digital music encoded in MP3 format as well as some meta data about the frame itself. At the beginning of each MP3 frame are 11 bits (sometimes 12) all set to 1. This is called the sync, and it's the pattern a media player looks for when attempting to play an MP3 file or stream. If the player finds this 11 bit sequence, then it knows its found an MP3 frame which can be decoded and played back.

参见: www.id3.org/mp3Frame

如您所知,ID3标签包含有关的数据整个赛道。 ID3标签 - 版本2.x及更高版本 - 位于文件的开头,或者甚至可以嵌入MP3流中(尽管通常不会这样做)。 ID3标记的标头包含32位大小的字段,表示标记中有多少字节。无符号的32位整数可以保持的最大值是0xFFFFFFFF。因此,如果我们在大小字段中写入0xFFFFFFFF,我们就会声称一个非常大的标记(实际上太大了)。当播放器尝试播放文件或流时,它会查找MP3数据帧的11位序列,而是在ID3标记标题中找到大小字段并尝试播放标记,因为大小字段具有前一个11位设置。根据您的音乐品味,这通常听起来不太好。解决方案是创建一个整数格式,其中不包含所有1的11位序列。因此,同步安全整数格式。

As you know an ID3 tag contains data about the track as a whole. An ID3 tag -- in version 2.x and later -- is located at the beginning of a file, or can even be embedded in an MP3 stream(though this is not often done). The header of an ID3 tag contains a 32 bit size field, which indicates how many bytes are in the tag. The max value an unsigned, 32 bit integer can hold is 0xFFFFFFFF. So if we write 0xFFFFFFFF into the size field, we're claiming a really big tag (pragmatically too big). When the player attempts to play the file or stream, it looks for the 11 bit sequence of an MP3 data frame, but instead finds the size field in the ID3 tag header and tries to play the tag, since the size field has the first 11 bits set. This usually doesn't sound as good, depending on your musical tastes. The solution is to create an integer format that contains no 11 bit sequences of all 1's. Hence the sync-safe integer format.

可以使用以下内容将同步安全整数转换为C / C ++中的整数:

A sync-safe integer can be converted to an integer in C/C++ using something like the following:

int ID3_sync_safe_to_int( uint8_t* sync_safe )
{
    uint32_t byte0 = sync_safe[0];
    uint32_t byte1 = sync_safe[1];
    uint32_t byte2 = sync_safe[2];
    uint32_t byte3 = sync_safe[3];

    return byte0 << 21 | byte1 << 14 | byte2 << 7 | byte3;
}

希望这有帮助。

这篇关于为什么有Synchsafe Integer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆