Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small perf fixes and some code reuse #413

Merged
merged 4 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MetadataExtractor/Formats/Jpeg/JpegSegmentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public static bool CanContainMetadata(this JpegSegmentType type)

/// <summary>Gets JPEG segment types that might contain metadata.</summary>
#if NET5_0_OR_GREATER
public static IReadOnlyList<JpegSegmentType> CanContainMetadataTypes { get; } = Enum.GetValues<JpegSegmentType>().Cast<JpegSegmentType>().Where(type => type.CanContainMetadata()).ToList();
public static IReadOnlyList<JpegSegmentType> CanContainMetadataTypes { get; } = Enum.GetValues<JpegSegmentType>().Where(type => type.CanContainMetadata()).ToList();
#else
public static IReadOnlyList<JpegSegmentType> CanContainMetadataTypes { get; } = Enum.GetValues(typeof(JpegSegmentType)).Cast<JpegSegmentType>().Where(type => type.CanContainMetadata()).ToList();
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ void MoovHandler(AtomCallbackArgs a)
case "uuid":
{
ReadOnlySpan<byte> cr3 = [0x85, 0xc0, 0xb6, 0x87, 0x82, 0x0f, 0x11, 0xe0, 0x81, 0x11, 0xf4, 0xce, 0x46, 0x2b, 0x6a, 0x48];
var uuid = a.Reader.GetBytes(cr3.Length);
if (cr3.SequenceEqual(uuid))
Span<byte> actual = stackalloc byte[cr3.Length];
a.Reader.GetBytes(actual);
if (cr3.SequenceEqual(actual))
{
QuickTimeReader.ProcessAtoms(stream, UuidHandler, a.BytesLeft);
}
Expand Down
16 changes: 3 additions & 13 deletions MetadataExtractor/Formats/QuickTime/QuickTimeReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using MetadataExtractor.Formats.Iso14496;

namespace MetadataExtractor.Formats.QuickTime
{
/// <summary>
Expand Down Expand Up @@ -43,19 +45,7 @@ public sealed class AtomCallbackArgs(uint type, long size, Stream stream, long s
/// <summary>
/// Gets the string representation of this atom's type.
/// </summary>
public string TypeString
{
get
{
var bytes = BitConverter.GetBytes(Type);
Array.Reverse(bytes);
#if NETSTANDARD1_3
return Encoding.UTF8.GetString(bytes);
#else
return Encoding.ASCII.GetString(bytes);
#endif
}
}
public string TypeString => TypeStringConverter.ToTypeString(Type);

/// <summary>
/// Computes the number of bytes remaining in the atom, given the <see cref="Stream"/> position.
Expand Down
12 changes: 7 additions & 5 deletions MetadataExtractor/Formats/QuickTime/QuickTimeReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ public static string Get4ccString(this SequentialReader reader)
// https://en.wikipedia.org/wiki/FourCC

Span<byte> bytes = stackalloc byte[4];
Span<char> chars = stackalloc char[4];

reader.GetBytes(bytes);

// NOTE we cannot just use Encoding.ASCII here, as that can replace certain non-printable characters with '?'
chars[0] = (char)bytes[0];
chars[1] = (char)bytes[1];
chars[2] = (char)bytes[2];
chars[3] = (char)bytes[3];
Span<char> chars =
[
(char)bytes[0],
(char)bytes[1],
(char)bytes[2],
(char)bytes[3]
];

#if NET462 || NETSTANDARD1_3
fixed (char* pChars = chars)
Expand Down
Loading