0

我正在尝试编写一个解析器来从以下FLAC文件中提取信息:

$ hd audio.flac | head -n 6
00000000  66 4c 61 43 00 00 00 22  12 00 12 00 00 00 00 00  |fLaC..."........|
00000010  00 00 0a c4 42 f0 00 78  9f 30 00 00 00 00 00 00  |....B..x.0......|
00000020  00 00 00 00 00 00 00 00  00 00 84 00 02 64 1f 00  |.............d..|
00000030  00 00 47 53 74 72 65 61  6d 65 72 20 65 6e 63 6f  |..GStreamer enco|
00000040  64 65 64 20 76 6f 72 62  69 73 63 6f 6d 6d 65 6e  |ded vorbiscommen|
00000050  74 10 00 00 00 12 00 00  00 54 49 54 4c 45 3d 52  |t........TITLE=R|

现在,根据规范,格式应如下(数字以位为单位):

 <32>  "fLaC", the FLAC stream marker in ASCII
 <16>  The minimum block size (in samples) used in the stream.
 <16>  The maximum block size (in samples) used in the stream.
 <24>  The minimum frame size (in bytes) used in the stream.
 <24>  The maximum frame size (in bytes) used in the stream.
 <20>  Sample rate in Hz.
  <3>  (number of channels)-1. FLAC supports from 1 to 8 channels
  <5>  (bits per sample)-1. FLAC supports from 4 to 32 bits per sample.
 <36>  Total samples in stream.
<128>  MD5 signature of the unencoded audio data.

因此,我开始编写解析器,并在测试时得到非常奇怪的结果。所以我用一个“真实的”元数据提取器进行测试:

$ metaflac --list audio.flac 
METADATA block #0
  type: 0 (STREAMINFO)
  is last: false
  length: 34
  minimum blocksize: 4608 samples
  maximum blocksize: 4608 samples
  minimum framesize: 0 bytes
  maximum framesize: 0 bytes
  sample_rate: 44100 Hz
  channels: 2
  bits-per-sample: 16
  total samples: 7905072
  MD5 signature: 00000000000000000000000000000000

从数字中,我可以推断出以下几点:

66 4c 61 43 00 00 00 22  12 00 12 00 00 00 00 00
~~~~~~~~~~~ ~~~~~~~~~~~  ~~~~~ ~~~~~ ~~~~~~~~ ~~
     ^           ^         ^     ^       ^    ^
     |           |         |     |       |    |
     |           |         |     |       |    + Etc.
     |           |         |     |       + Minimum frame size
     |           |         |     + Maximum block size
     |           |         + Minimum block size
     |           + What is that ?!?
     + FLAC stream marker

那 32 位是从哪里来的?我看到它们代表了标题的长度,但是把它放在那里不违反标准(考虑到我们已经知道长度:(32+16+16+24+20+3+5+36+ 128)/8)?

4

1 回答 1

2

0x22 (34)确实是以字节为单位的标头块大小,作为 METADATA_BLOCK_HEADER 的一部分,它跟在流中fLaC标记之后。在前 8 位 (00) 中,第 7 位表示后面还有更多元数据块,接下来的 7 位表示它是一个STREAMINFO块。下面的3个字节(00 00 22)是块内容的长度;

16 + 16 + 24 + 24 + 20 + 3 + 5 + 36 + 128 = 272 bits
272 bits / 8 = 34 (0x22) bytes.
于 2013-05-20T15:50:11.877 回答