@@ -149,22 +149,33 @@ const (
149
149
150
150
func (i * OggWriter ) createPage (payload []uint8 , headerType uint8 , granulePos uint64 , pageIndex uint32 ) []byte {
151
151
i .lastPayloadSize = len (payload )
152
- page := make ([]byte , pageHeaderSize + 1 + i .lastPayloadSize )
152
+ nSegments := (len (payload ) / 255 ) + 1 // A segment can be at most 255 bytes long.
153
+
154
+ page := make ([]byte , pageHeaderSize + i .lastPayloadSize + nSegments )
153
155
154
156
copy (page [0 :], pageHeaderSignature ) // page headers starts with 'OggS'
155
157
page [4 ] = 0 // Version
156
158
page [5 ] = headerType // 1 = continuation, 2 = beginning of stream, 4 = end of stream
157
159
binary .LittleEndian .PutUint64 (page [6 :], granulePos ) // granule position
158
160
binary .LittleEndian .PutUint32 (page [14 :], i .serial ) // Bitstream serial number
159
161
binary .LittleEndian .PutUint32 (page [18 :], pageIndex ) // Page sequence number
160
- page [26 ] = 1 // Number of segments in page, giving always 1 segment
161
- page [27 ] = uint8 (i .lastPayloadSize ) // Segment Table inserting at 27th position since page header length is 27
162
- copy (page [28 :], payload ) // inserting at 28th since Segment Table(1) + header length(27)
162
+ page [26 ] = uint8 (nSegments ) // Number of segments in page.
163
+
164
+ // Filling segment table with the lacing values.
165
+ // First (nSegments - 1) values will always be 255.
166
+ for i := 0 ; i < nSegments - 1 ; i ++ {
167
+ page [pageHeaderSize + i ] = 255
168
+ }
169
+ // The last value will be the remainder.
170
+ page [pageHeaderSize + nSegments - 1 ] = uint8 (len (payload ) % 255 )
171
+
172
+ copy (page [pageHeaderSize + nSegments :], payload ) // Payload goes after the segment table, so at pageHeaderSize+nSegments.
163
173
164
174
var checksum uint32
165
175
for index := range page {
166
176
checksum = (checksum << 8 ) ^ i .checksumTable [byte (checksum >> 24 )^ page [index ]]
167
177
}
178
+
168
179
binary .LittleEndian .PutUint32 (page [22 :], checksum ) // Checksum - generating for page data and inserting at 22th position into 32 bits
169
180
170
181
return page
0 commit comments