为什么MP3文件使用Synchsafe整数? [英] Why do MP3 files use Synchsafe Integers?

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

问题描述

我开始用c ++读取mp3文件.

I started reading mp3-files in c++.

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

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

我找到了如何将其转换为普通整数的方法,但是我不停地问自己为什么以如此不必要的复杂方式存储整数值.

I found out how to convert it to an ordinary integer, but I cannot stop asking myself why an integer value is stored in such an unnecessarily complicated way.

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

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

推荐答案

要了解为什么使用同步安全整数,了解一下MP3数据的格式以及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写入size字段,那么我们声称是一个非常大的标签(实用上太大).当播放器尝试播放文件或流时,它将查找MP3数据帧的11位序列,但会在ID3标签标头中找到size字段并尝试播放标签,因为size字段具有前11个位设置.根据您的音乐喜好,这听起来通常不太好.解决方案是创建一个不包含全为11的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;
}

希望这会有所帮助.

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

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