Skip to content

Commit 53e166a

Browse files
committed
Use more Spans
1 parent 650958a commit 53e166a

File tree

12 files changed

+153
-155
lines changed

12 files changed

+153
-155
lines changed

ATL/AudioData/AudioDataIOFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,21 +405,21 @@ public IAudioDataIO GetFromStream(Stream s)
405405
{
406406
// TODO : memorize initial offset?
407407
s.Seek(0, SeekOrigin.Begin);
408-
byte[] data = new byte[32];
408+
Span<byte> data = stackalloc byte[32];
409409
long offset = 0;
410410
bool hasID3v2 = false;
411-
if (s.Read(data, 0, 32) < 32) return GetFromFormat(IN_MEMORY, new AudioFormat(Format.UNKNOWN_FORMAT));
411+
if (s.Read(data) < 32) return GetFromFormat(IN_MEMORY, new AudioFormat(Format.UNKNOWN_FORMAT));
412412
// Hardcoded case of ID3v2 as it is the sole standard metadata system to appear at the beginning of file
413413
// NB : useful to detect files tagged with ID3v2 even though their format isn't compatible (e.g. MP4/M4A)
414414
if (ID3v2.IsValidHeader(data))
415415
{
416416
hasID3v2 = true;
417417

418-
var data2 = data.AsSpan()[6..10]; // bytes 6-9 only
418+
var data2 = data[6..10]; // bytes 6-9 only
419419
int id3v2Size = StreamUtils.DecodeSynchSafeInt32(data2) + 10; // 10 being the size of the header
420420
s.Seek(id3v2Size, SeekOrigin.Begin);
421421
offset = s.Position;
422-
if (s.Read(data, 0, 32) < 32) return GetFromFormat(IN_MEMORY, new AudioFormat(Format.UNKNOWN_FORMAT));
422+
if (s.Read(data) < 32) return GetFromFormat(IN_MEMORY, new AudioFormat(Format.UNKNOWN_FORMAT));
423423
}
424424
try
425425
{

ATL/AudioData/AudioDataManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ private bool read(Stream source, ReadTagParams readTagParams)
532532
ID3v2.Clear();
533533
// Test for ID3v2 regardless of it being supported, to properly handle files with illegal ID3v2 tags
534534
source.Position = 0;
535-
byte[] data = new byte[32];
536-
if (32 == source.Read(data, 0, 32) && IO.ID3v2.IsValidHeader(data))
535+
Span<byte> data = stackalloc byte[32];
536+
if (32 == source.Read(data) && IO.ID3v2.IsValidHeader(data))
537537
{
538538
source.Position = 0;
539539
readTagParams.ExtraID3v2PaddingDetection = isMetaSupported(TagType.ID3V2);

ATL/AudioData/IO/AC3.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
9999
{
100100
resetData();
101101

102-
byte[] buffer = new byte[2];
102+
Span<byte> buffer = stackalloc byte[2];
103103
source.Seek(0, SeekOrigin.Begin);
104-
if (source.Read(buffer, 0, 2) < 2) return false;
104+
if (source.Read(buffer) < 2) return false;
105105

106106
if (!IsValidHeader(buffer)) return false;
107107

108108
AudioDataOffset = source.Position - 2;
109109
AudioDataSize = sizeNfo.FileSize - sizeNfo.APESize - sizeNfo.ID3v1Size - AudioDataOffset;
110110

111111
source.Seek(2, SeekOrigin.Current);
112-
if (source.Read(buffer, 0, 1) < 1) return false;
112+
if (source.Read(buffer) < 2) return false;
113113

114114
// fscod
115115
sampleRate = (buffer[0] & 0xC0) switch
@@ -123,8 +123,7 @@ public bool Read(Stream source, SizeInfo sizeNfo, MetaDataIO.ReadTagParams readT
123123
// frmsizecod
124124
BitRate = BITRATES[(buffer[0] & 0x3F) >> 1];
125125

126-
source.Seek(1, SeekOrigin.Current);
127-
if (source.Read(buffer, 0, 2) < 2) return false;
126+
if (source.Read(buffer) < 2) return false;
128127

129128
// acmod, lfeon
130129
ChannelsArrangement = getChannelsArrangement(buffer[0] & 0xE0, (buffer[1] & 0x80) > 0);

ATL/AudioData/IO/FLAC.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public static WriteResult writeVorbisCommentBlock(Stream w, TagData tag, VorbisT
380380
{
381381
byte toWrite = META_VORBIS_COMMENT;
382382
if (isLastMetaBlock) toWrite |= FLAG_LAST_METADATA_BLOCK;
383-
w.Write(new byte[] { toWrite }, 0, 1);
383+
w.Write(new[] { toWrite }, 0, 1);
384384
var sizePos = w.Position;
385385
w.Write(new byte[] { 0, 0, 0 }, 0, 3); // Placeholder for 24-bit integer that will be rewritten at the end of the method
386386

ATL/AudioData/IO/Helpers/EBMLReader.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,18 @@ public double readFloat()
201201
switch (nbBytes)
202202
{
203203
case 4:
204-
byte[] tmpBuf = new byte[4];
205-
Array.Copy(buffer, tmpBuf, 4);
204+
Span<byte> tmpBuf = new Span<byte>(buffer, 0,4);
206205
return ToSingle(tmpBuf);
207206
case 8:
208207
return ToDouble(buffer);
209208
default: return buffer[0];
210209
}
211210
}
212211

213-
private static float ToSingle(byte[] bytes, int startIndex = 0)
212+
private static float ToSingle(Span<byte> bytes)
214213
{
215-
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
216-
return BitConverter.ToSingle(bytes, startIndex);
214+
if (BitConverter.IsLittleEndian) bytes.Reverse();
215+
return BitConverter.ToSingle(bytes);
217216
}
218217
private static double ToDouble(byte[] bytes, int startIndex = 0)
219218
{

ATL/AudioData/IO/Helpers/ListTag.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static string FromStream(Stream source, MetaDataIO meta, ReadTagParams re
2929
long initialPos = position;
3030

3131
// Purpose
32-
byte[] data = new byte[4];
33-
if (source.Read(data, 0, 4) < 4) return "";
34-
string typeId = Utils.Latin1Encoding.GetString(data, 0, 4);
32+
Span<byte> data = stackalloc byte[4];
33+
if (source.Read(data) < 4) return "";
34+
string typeId = Utils.Latin1Encoding.GetString(data);
3535

3636
long maxPos = initialPos + chunkSize - 4; // 4 being the purpose 32bits tag that belongs to the chunk
3737

@@ -70,15 +70,15 @@ private static void readInfoPurpose(Stream source, MetaDataIO meta, ReadTagParam
7070
private static void readDataListPurpose(Stream source, MetaDataIO meta, ReadTagParams readTagParams, long maxPos)
7171
{
7272
int position = 0;
73-
byte[] data = new byte[4];
73+
Span<byte> data = stackalloc byte[4];
7474

7575
while (source.Position < maxPos)
7676
{
7777
// Sub-chunk ID
78-
if (source.Read(data, 0, 4) < 4) return;
79-
var id = Utils.Latin1Encoding.GetString(data, 0, 4);
78+
if (source.Read(data) < 4) return;
79+
var id = Utils.Latin1Encoding.GetString(data);
8080
// Size
81-
if (source.Read(data, 0, 4) < 4) return;
81+
if (source.Read(data) < 4) return;
8282
var size = BinaryPrimitives.ReadInt32LittleEndian(data);
8383
if (size <= 0) continue;
8484

ATL/AudioData/IO/MP4.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -297,38 +297,39 @@ private void readQTChapters(Stream source, IList<MP4Sample> chapterTextTrackSamp
297297
double cumulatedDuration = 0;
298298

299299
// Text chapters are "master data"; picture chapters get attached to them
300+
Span<byte> data = stackalloc byte[2];
300301
for (int i = 0; i < chapterTextTrackSamples.Count; i++)
301302
{
302303
MP4Sample textSample = chapterTextTrackSamples[i];
303304
MP4Sample pictureSample = i < chapterPictureTrackSamples.Count ? chapterPictureTrackSamples[i] : null;
304-
if (textSample.ChunkOffset > 0)
305+
if (textSample.ChunkOffset <= 0) continue;
306+
307+
ChapterInfo chapter = new ChapterInfo
305308
{
306-
ChapterInfo chapter = new ChapterInfo();
307-
chapter.Format = ChapterInfo.FORMAT.QT;
308-
309-
source.Seek(textSample.ChunkOffset + textSample.RelativeOffset, SeekOrigin.Begin);
310-
byte[] data = new byte[2];
311-
if (source.Read(data, 0, 2) < 2) return;
312-
ushort strDataSize = BinaryPrimitives.ReadUInt16BigEndian(data);
313-
314-
byte[] strData = new byte[strDataSize];
315-
if (source.Read(strData, 0, strDataSize) < strDataSize) return;
316-
chapter.Title = Encoding.UTF8.GetString(strData);
317-
chapter.StartTime = (uint)Math.Round(cumulatedDuration);
318-
cumulatedDuration += textSample.Duration * 1000;
319-
chapter.EndTime = (uint)Math.Round(cumulatedDuration);
320-
321-
if (pictureSample != null && pictureSample.ChunkOffset > 0 && pictureSample.Size > 0)
322-
{
323-
source.Seek(pictureSample.ChunkOffset + pictureSample.RelativeOffset, SeekOrigin.Begin);
324-
int localSize = (int)pictureSample.Size;
325-
byte[] localData = new byte[localSize];
326-
if (source.Read(localData, 0, localSize) < localSize) return;
327-
chapter.Picture = PictureInfo.fromBinaryData(localData, PictureInfo.PIC_TYPE.Generic, getImplementedTagType());
328-
}
309+
Format = ChapterInfo.FORMAT.QT
310+
};
311+
312+
source.Seek(textSample.ChunkOffset + textSample.RelativeOffset, SeekOrigin.Begin);
313+
if (source.Read(data) < 2) return;
314+
ushort strDataSize = BinaryPrimitives.ReadUInt16BigEndian(data);
329315

330-
tagData.Chapters.Add(chapter);
316+
byte[] strData = new byte[strDataSize];
317+
if (source.Read(strData, 0, strDataSize) < strDataSize) return;
318+
chapter.Title = Encoding.UTF8.GetString(strData);
319+
chapter.StartTime = (uint)Math.Round(cumulatedDuration);
320+
cumulatedDuration += textSample.Duration * 1000;
321+
chapter.EndTime = (uint)Math.Round(cumulatedDuration);
322+
323+
if (pictureSample is { ChunkOffset: > 0, Size: > 0 })
324+
{
325+
source.Seek(pictureSample.ChunkOffset + pictureSample.RelativeOffset, SeekOrigin.Begin);
326+
int localSize = (int)pictureSample.Size;
327+
byte[] localData = new byte[localSize];
328+
if (source.Read(localData, 0, localSize) < localSize) return;
329+
chapter.Picture = PictureInfo.fromBinaryData(localData, PictureInfo.PIC_TYPE.Generic, getImplementedTagType());
331330
}
331+
332+
tagData.Chapters.Add(chapter);
332333
}
333334
}
334335

@@ -625,6 +626,7 @@ private void readMoof(Stream s)
625626

626627
uint moofSize = navigateToAtom(s, "moof");
627628
isFragmented = moofSize > 0;
629+
byte[] data = new byte[4];
628630
while (moofSize > 0)
629631
{
630632
var moofOffset = s.Position;
@@ -633,7 +635,6 @@ private void readMoof(Stream s)
633635
if (0 == navigateToAtom(s, "tfhd")) break;
634636
s.Seek(1, SeekOrigin.Current); // Version
635637

636-
byte[] data = new byte[4];
637638
if (s.Read(data, 0, 3) < 3) break;
638639
int flags = StreamUtils.DecodeBEInt24(data);
639640
if (0 == (flags & 0x00000008)) break;
@@ -1716,7 +1717,7 @@ private static Uuid readUuid(Stream source)
17161717
};
17171718
if (result.size >= 16 + 8)
17181719
{
1719-
Span<byte> key = new byte[16];
1720+
Span<byte> key = stackalloc byte[16];
17201721
if (source.Read(key) < key.Length) return result;
17211722
// Convert key to hex value
17221723
StringBuilder sbr = new StringBuilder();

ATL/AudioData/IO/MPEGaudio.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void Reset()
188188
EmphasisID = MPEG_EMPHASIS_UNKNOWN;
189189
}
190190

191-
public void LoadFromByteArray(byte[] data)
191+
public void LoadFromByteArray(ReadOnlySpan<byte> data)
192192
{
193193
VersionID = (byte)((data[1] >> 3) & 3);
194194
LayerID = (byte)((data[1] >> 1) & 3);
@@ -412,13 +412,13 @@ private static VBRData getXingInfo(Stream source)
412412
private static VBRData getFhGInfo(Stream source)
413413
{
414414
VBRData result = new VBRData();
415-
byte[] data = new byte[9];
415+
Span<byte> data = stackalloc byte[9];
416416

417417
// Extract FhG VBR info at given position
418418
result.Found = true;
419419
result.ID = VBR_ID_FHG.ToCharArray();
420420
source.Seek(5, SeekOrigin.Current);
421-
if (source.Read(data, 0, 9) < 9) return result;
421+
if (source.Read(data) < 9) return result;
422422

423423
result.Scale = data[0];
424424
result.Bytes =
@@ -439,12 +439,12 @@ private static VBRData getFhGInfo(Stream source)
439439

440440
private static VBRData findVBR(Stream source, long position)
441441
{
442-
byte[] data = new byte[4];
442+
Span<byte> data = stackalloc byte[4];
443443

444444
// Check for VBR header at given position
445445
source.Seek(position, SeekOrigin.Begin);
446446

447-
if (4 == source.Read(data, 0, 4))
447+
if (4 == source.Read(data))
448448
{
449449
string vbrId = Utils.Latin1Encoding.GetString(data);
450450
switch (vbrId)
@@ -481,13 +481,13 @@ public static bool HasValidFrame(Stream source)
481481
/// <summary>
482482
/// Find next MPEG frame starting from a given MPEG frame
483483
/// </summary>
484-
private static FrameHeader findNextFrame(Stream source, FrameHeader startingFrame, byte[] buffer)
484+
private static FrameHeader findNextFrame(Stream source, FrameHeader startingFrame, Span<byte> buffer)
485485
{
486486
FrameHeader result = new FrameHeader();
487487
result.Reset();
488488

489489
source.Seek(startingFrame.Offset + startingFrame.Size, SeekOrigin.Begin);
490-
if (source.Read(buffer, 0, 4) < 4 || !IsValidFrameHeader(buffer)) return result;
490+
if (source.Read(buffer) < 4 || !IsValidFrameHeader(buffer)) return result;
491491

492492
result.LoadFromByteArray(buffer);
493493
result.Offset = source.Position - 4;
@@ -506,10 +506,10 @@ private static FrameHeader findNextFrame(Stream source, FrameHeader startingFram
506506
private static FrameHeader findFirstFrame(Stream source, ref VBRData oVBR, SizeInfo sizeInfo)
507507
{
508508
FrameHeader result = new FrameHeader();
509-
byte[] buffer = new byte[4];
509+
Span<byte> buffer = stackalloc byte[4];
510510
long sourceOffset = source.Position;
511511

512-
if (source.Read(buffer, 0, 4) < 4) return result;
512+
if (source.Read(buffer) < 4) return result;
513513
result.Found = IsValidFrameHeader(buffer);
514514

515515
/*
@@ -532,7 +532,7 @@ private static FrameHeader findFirstFrame(Stream source, ref VBRData oVBR, SizeI
532532
// If padding uses 0xFF bytes, take one step back in case header lies there
533533
if (0xFF == buffer[0]) source.Seek(-1, SeekOrigin.Current);
534534

535-
if (source.Read(buffer, 0, 4) < 4) return result;
535+
if (source.Read(buffer) < 4) return result;
536536
result.Found = IsValidFrameHeader(buffer);
537537
}
538538

@@ -550,7 +550,7 @@ private static FrameHeader findFirstFrame(Stream source, ref VBRData oVBR, SizeI
550550
while (0xFF != source.ReadByte() && source.Position < limit) { /* just advance the stream */ }
551551

552552
source.Seek(-1, SeekOrigin.Current);
553-
if (source.Read(buffer, 0, 4) < 4) break;
553+
if (source.Read(buffer) < 4) break;
554554
result.Found = IsValidFrameHeader(buffer);
555555
}
556556

@@ -633,7 +633,7 @@ private static FrameHeader findFirstFrame(Stream source, ref VBRData oVBR, SizeI
633633

634634
private static FrameParsingResult parseExactAudioDataSize(BufferedBinaryReader reader, FrameHeader firstFrame)
635635
{
636-
byte[] buffer = new byte[4];
636+
Span<byte> buffer = stackalloc byte[4];
637637
var bitrates = new List<float> { firstFrame.BitRate };
638638
long topOffset = 0;
639639
reader.Seek(firstFrame.Offset, SeekOrigin.Begin);

ATL/AudioData/IO/MPEGplus.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ private bool readHeader(Stream source, ref HeaderRecord header)
139139
// If VS8 file, looks for the (mandatory) stream header packet
140140
if (80 == header.Version)
141141
{
142-
string packetKey;
143142
bool headerFound = false;
144143

145144
// Let's go back right after the 32-bit version marker
@@ -150,7 +149,7 @@ private bool readHeader(Stream source, ref HeaderRecord header)
150149
{
151150
long initialPos = source.Position;
152151
if (source.Read(buffer, 0, 2) < 2) break;
153-
packetKey = Utils.Latin1Encoding.GetString(buffer);
152+
var packetKey = Utils.Latin1Encoding.GetString(buffer);
154153

155154
readVariableSizeInteger(source); // Packet size (unused)
156155

@@ -300,12 +299,12 @@ private static long readVariableSizeInteger(Stream source)
300299
{
301300
long result = 0;
302301
byte b = 128;
303-
byte[] buffer = new byte[1];
302+
Span<byte> buffer = stackalloc byte[1];
304303

305304
// Data is coded with a Big-endian, 7-byte variable-length record
306305
while ((b & 128) > 0)
307306
{
308-
if (source.Read(buffer, 0, 1) < 1) break;
307+
if (source.Read(buffer) < 1) break;
309308
b = buffer[0];
310309
result = (result << 7) + (b & 127); // Big-endian
311310
}

0 commit comments

Comments
 (0)