Debian Codegraph @

internal/mp3/frame.go

raw symbols SCIP
1 package mp3
2
3 import "bytes"
4
5 // minTruncatedSyncMatches is the minimum number of confirmed successive
6 // header matches required to accept a candidate frame when the buffer ends
7 // before maxFrameSyncMatches confirmations can be performed.
8 const minTruncatedSyncMatches = 2
9
10 func ExtractFrame(b []byte) (start, size int) {
11 limit := min(len(b), 2048+headerSize)
12 for i := 0; i < limit-headerSize; i++ {
13 j := bytes.IndexByte(b[i:limit-headerSize], 0xFF)
14 if j < 0 {
15 break
16 }
17 i += j
18 hdr := header{b[i], b[i+1], b[i+2], b[i+3]}
19 if !hdr.valid() {
20 continue
21 }
22 frameBytes := hdr.frameBytes()
23 frameAndPad := frameBytes + hdr.padding()
24
25 validHere := frameBytes > 0 && i+frameAndPad <= len(b) && matchFrame(b[i:])
26 // When the buffer is exactly one frame, matchFrame cannot look ahead for
27 // a subsequent header to confirm the stream. Trust the validated header.
28 exact := i == 0 && frameAndPad == len(b)
29 if validHere || exact {
30 return i, frameAndPad
31 }
32 }
33 return 0, 0
34 }
35
36 // matchFrame confirms a candidate header by stepping forward and checking that
37 // subsequent headers are consistent.
38 func matchFrame(buf []byte) bool {
39 // maxFrameSyncMatches limits how many valid frames we look at.
40 const maxFrameSyncMatches = 10
41 hdr := header{buf[0], buf[1], buf[2], buf[3]}
42 i := hdr.frameBytes() + hdr.padding()
43 for nmatch := 0; nmatch < maxFrameSyncMatches; nmatch++ {
44 if i+headerSize > len(buf) {
45 return nmatch >= minTruncatedSyncMatches
46 }
47 cmp := header{buf[i], buf[i+1], buf[i+2], buf[i+3]}
48 if !hdr.compatibleWith(cmp) {
49 return false
50 }
51 i += cmp.frameBytes() + cmp.padding()
52 }
53 return true
54 }
55
56 const headerSize = 4
57
58 type header [headerSize]byte
59
60 func (h header) isFreeFormat() bool { return h[2]&0xF0 == 0 }
61 func (h header) isMPEG1() bool { return h[1]&0x8 != 0 }
62 func (h header) isMPEG25() bool { return h[1]&0x10 == 0 }
63 func (h header) rawLayer() byte { return h[1] >> 1 & 3 }
64 func (h header) rawBitrate() byte { return h[2] >> 4 }
65 func (h header) rawSampleRate() byte { return h[2] >> 2 & 3 }
66 func (h header) rawEmphasis() byte { return h[3] & 0b11 }
67 func (h header) isFrame576() bool { return h[1]&14 == 2 }
68 func (h header) padding() int {
69 if h[2]&0x2 != 0 {
70 return 1
71 }
72 return 0
73 }
74
75 // valid reports whether the four bytes form a syntactically valid MP3 header.
76 func (h header) valid() bool {
77 return h[0] == 0xff &&
78 ((h[1]&0xF0) == 0xf0 || (h[1]&0xFE) == 0xe2) &&
79 h.rawLayer() == 1 && // Layer III
80 h.rawBitrate() != 15 && // Not allowed by spec.
81 h.rawSampleRate() != 3 &&
82 h.rawEmphasis() != 2 &&
83 // The code for extracting frame size for free-format is tedious and
84 // free-format MP3s are extinct.
85 !h.isFreeFormat()
86 }
87
88 // compatibleWith reports whether two headers describe frames belonging to the
89 // same MP3 stream — same MPEG version, layer, sample-rate index.
90 func (h header) compatibleWith(o header) bool {
91 return o.valid() &&
92 (h[1]^o[1])&0xFE == 0 &&
93 (h[2]^o[2])&0x0C == 0
94 }
95
96 // bitrateKbps returns the bitrate of the frame in kilobits per second.
97 func (h header) bitrateKbps() int {
98 // halfrate[mpeg1?][bitrate_idx] holds bitrate/2 in kbps.
99 halfrate := [2][15]uint8{
100 {0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 72, 80},
101 {0, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160},
102 }
103 mpeg1 := 0
104 if h.isMPEG1() {
105 mpeg1 = 1
106 }
107 return 2 * int(halfrate[mpeg1][h.rawBitrate()])
108 }
109
110 // sampleRateHz returns the sampling rate of the frame in Hz.
111 func (h header) sampleRateHz() int {
112 base := [3]int{44100, 48000, 32000}[h.rawSampleRate()]
113 if !h.isMPEG1() {
114 base >>= 1
115 }
116 if h.isMPEG25() {
117 base >>= 1
118 }
119 return base
120 }
121
122 // frameSamples returns the number of audio samples per channel encoded in
123 // the frame.
124 func (h header) frameSamples() int {
125 if h.isFrame576() {
126 return 576
127 }
128 return 1152
129 }
130
131 // frameBytes returns the size of the frame body (header + side info + audio
132 // data, excluding padding) in bytes.
133 func (h header) frameBytes() int {
134 br := h.bitrateKbps()
135 sr := h.sampleRateHz()
136 if br == 0 || sr == 0 {
137 return 0
138 }
139 return h.frameSamples() * br * 125 / sr
140 }