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

Add support for HEIF based images #2633

Open
wants to merge 180 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
180 commits
Select commit Hold shift + click to select a range
0829e86
Scaffolding for HEIC file format
ynse01 Dec 25, 2023
b5ce29d
Fix build
ynse01 Dec 25, 2023
bc97554
Initial Metadata parsing
ynse01 Dec 26, 2023
235aa38
HEIC test images
ynse01 Dec 26, 2023
c0297a3
Adapt code to .NET 8
ynse01 Dec 26, 2023
96b92e8
4CC generated by template
ynse01 Dec 27, 2023
9ad76ce
Rename Heic4CharCode files
ynse01 Dec 27, 2023
c3ce980
Use single buffer per decoder instance
ynse01 Dec 27, 2023
ff9629a
Add another sample image
ynse01 Dec 27, 2023
028e8a5
Initial tests
ynse01 Dec 27, 2023
f892c04
Byte alignment fixes
ynse01 Dec 27, 2023
193fd94
Boundary checking before box allocation
ynse01 Dec 27, 2023
8366d72
Add HIF test image
ynse01 Dec 27, 2023
5f9db61
Add handler parsing
ynse01 Dec 27, 2023
d6dc2b1
Parse HIF metadata
ynse01 Dec 27, 2023
f90d4eb
More HIF support
ynse01 Dec 27, 2023
f234c8b
Decoding of thumbnail with JPEG
ynse01 Dec 27, 2023
e972551
Exposed used compression method in metadata
ynse01 Dec 28, 2023
183b873
Reference image for Heic Decoder Test
ynse01 Dec 28, 2023
0bfa0fe
Initial encoder
ynse01 Dec 28, 2023
b60ce90
Move to Memory namespace
ynse01 Dec 28, 2023
23388b2
Box size predictions
ynse01 Dec 28, 2023
3f4e74b
Fix Heic encoder test
ynse01 Dec 29, 2023
54db9c7
Box size improvements
ynse01 Dec 29, 2023
6372a9a
Add AVIF test image
ynse01 Dec 29, 2023
bb3dc4e
Merge branch 'heic-support' of https://github.com/ynse01/ImageSharp i…
ynse01 Dec 29, 2023
3fae594
Identify AVIF files
ynse01 Dec 29, 2023
046b28f
Tests for AutoExpandingMemory class
ynse01 Dec 29, 2023
9d99b1a
Move to Heif directory
ynse01 Dec 29, 2023
4645e40
Rename namespace and classes to Heif
ynse01 Dec 29, 2023
5ad3d67
Fix configuration test
ynse01 Dec 29, 2023
42ff8f9
Remove reference to x265
ynse01 Jan 5, 2024
934dd61
Include SVT reference
ynse01 Jan 5, 2024
ccbae05
Consistent use of name HEIF
ynse01 Jan 5, 2024
0301aed
Decode ICC profile into metadata
ynse01 Jan 19, 2024
5dedc94
Initial av1 bitstream parsing
ynse01 Jan 7, 2024
f934927
Read parameters
ynse01 Jan 8, 2024
e45a41b
Av1 bitstream tests
ynse01 Jan 12, 2024
7b961ff
Optimized WriteBoolean()
ynse01 Jan 12, 2024
961d55e
Skeleton
ynse01 Jan 19, 2024
790207f
Prepare quantization
ynse01 Jan 20, 2024
03eda75
Merge branch 'main' into heic-support
JimBobSquarePants Feb 7, 2024
43569db
Merge branch 'main' into heic-support
JimBobSquarePants Mar 14, 2024
db44498
Casing in Heif4CharCode
ynse01 Mar 14, 2024
73bdbd8
Make some classes internal
ynse01 Mar 14, 2024
de392d8
Read header from stack allocated buffer
ynse01 Mar 15, 2024
d146069
Check Stream.Read return value
ynse01 Mar 17, 2024
c847f5d
Fix build
ynse01 Mar 17, 2024
a39182d
Merge remote-tracking branch 'upstream/main' into avif-in-heif
ynse01 Apr 5, 2024
b27765f
Merge branch 'heic-support' into avif-in-heif
ynse01 Apr 5, 2024
80140c4
Inverse quantization
ynse01 Apr 6, 2024
2c8dd67
Merge branch 'main' into heic-support
ynse01 Apr 17, 2024
40e9d68
Fix reading of larger literals
ynse01 Apr 17, 2024
66803d0
Merge remote-tracking branch 'upstream/main' into avif-in-heif
ynse01 Apr 17, 2024
894bbf2
Update infra
ynse01 Apr 17, 2024
47dc774
Obu header decoding
ynse01 Apr 21, 2024
6aa74f2
Rename to Av1Decoder
ynse01 Apr 21, 2024
f2bfd5a
Tile decoding interface
ynse01 Apr 23, 2024
73f4dce
Merge branch 'avif-in-heif' into heic-support
ynse01 Apr 23, 2024
a1a1a5a
Fix build
ynse01 Apr 23, 2024
07138c4
Use Tile Decoder interface
ynse01 Apr 23, 2024
6863542
Merge branch 'main' into heic-support
JimBobSquarePants Apr 26, 2024
a1cb2dd
Introduce ObuWriter
ynse01 Apr 28, 2024
74677ed
Fix bitstream writer
ynse01 May 1, 2024
ef572ac
ObuHeader round trips
ynse01 May 3, 2024
95d9f09
Initial symbol parsing
ynse01 May 12, 2024
06b0c04
Fix literal reading and writing
ynse01 May 17, 2024
e34b831
First symbol coding roundtrip
ynse01 May 17, 2024
fec0413
Initial symbol decoding
ynse01 May 28, 2024
b26c0fa
PartitionType symbol round trips
ynse01 Jun 1, 2024
b4f9e31
Merge branch 'main' into heic-support
ynse01 Jun 1, 2024
1184968
Introduce Tile Decoder
ynse01 Jun 1, 2024
881199e
Introduce Av1PartitionInfo and fill it during decoding
ynse01 Jun 2, 2024
98bad4d
Superblock decoding
ynse01 Jun 3, 2024
c0fcb42
Namespace rename
ynse01 Jun 3, 2024
bfed4ee
Add spec reference
ynse01 Jun 3, 2024
a8539f2
Rename Av1Constants class
ynse01 Jun 3, 2024
671c89d
Extend readme
ynse01 Jun 3, 2024
19ca2ea
Introduce Frame buffer class
ynse01 Jun 5, 2024
e64d74d
Simplify IAv1TileDecoder
ynse01 Jun 8, 2024
60869e1
Bug fixes in OBU parsing
ynse01 Jun 9, 2024
d8e7078
Implement reading AV1 Codec Configuration Record
brianpopow Jun 23, 2024
84127fa
Fix mistake in switch case for parsing Heif4CharCode.Av1C
brianpopow Jun 23, 2024
cb4f9ee
Add comments where the methods can be found in the spec
brianpopow Jun 24, 2024
aa44d0c
Add av1 spec file
brianpopow Jun 24, 2024
9ee8d92
Merge branch 'main' into heic-support
ynse01 Jun 24, 2024
c918eca
Merge branch 'heic-support' of https://github.com/ynse01/ImageSharp i…
ynse01 Jun 24, 2024
0ffdf8d
Fix merge errors
ynse01 Jun 24, 2024
a674546
Fix code style violations
ynse01 Jun 24, 2024
215ca66
Fix bug in ObuFrameHeader parsing
ynse01 Jun 25, 2024
5d793a2
Implement ReadSegmentationParameters
brianpopow Jun 25, 2024
dd9f383
Fix escape < sign in XML comment
brianpopow Jun 25, 2024
1d288d3
Implement ReadFilmGrainFilterParameters()
brianpopow Jun 26, 2024
4d79d59
Fix stylecop warnings
brianpopow Jun 26, 2024
6aa609f
Strength test criteria
ynse01 Jun 26, 2024
136110f
Adjustments in ReadSequenceHeader() for case when image is not Reduce…
brianpopow Jun 28, 2024
2accf95
Implement 5.5.4. read Decoder model info
brianpopow Jun 29, 2024
41b980c
Implement missing parts in ReadSequenceHeader for case when ReducedSt…
brianpopow Jun 29, 2024
c4d34c5
Fix issue in ReadSequenceHeader() not reading OrderHintBits
brianpopow Jun 29, 2024
a0fdf02
Implement partition context logic
ynse01 Jun 29, 2024
3556921
Implement ReadTimingInfo, it is needed in ReadUncompressedFrameHeader…
brianpopow Jun 30, 2024
0cf2726
Tiling initialization fixes
ynse01 Jun 30, 2024
5792d05
Annotate Av1TileDecoder methods with spec sections
brianpopow Jul 2, 2024
3382518
Fix build
ynse01 Jul 2, 2024
aaac78a
When TemporalDelimiter header is encountered, seenFrameHeader flag wi…
brianpopow Jul 3, 2024
e60b39f
Implement Transform parsing
ynse01 Jul 3, 2024
9fb5826
Smoke test for tiling
ynse01 Jul 3, 2024
13173a3
Implement GetPlaneResidualSize()
brianpopow Jul 4, 2024
3d84a51
Suppress warning about SA1500: Braces for multi-line statements
brianpopow Jul 4, 2024
6c4e213
Implement Residual
ynse01 Jul 4, 2024
1ff0150
Implement TransformBlock
ynse01 Jul 4, 2024
4e53f7c
Add some code comments
ynse01 Jul 5, 2024
d01cb8e
Merge branch 'main' into heic-support
ynse01 Jul 5, 2024
38c23a5
Disable failing test for in-progress code
ynse01 Jul 5, 2024
30f8884
Implement Coefficient syntax
ynse01 Jul 5, 2024
c8a6203
Implement ScanOrder scan constants
ynse01 Jul 6, 2024
1d83ac3
Implement remaining distributions
ynse01 Jul 6, 2024
23d533a
Some bug fixes
ynse01 Jul 6, 2024
a48e271
Fully implement Heif Item Location
ynse01 Jul 8, 2024
c771350
Prune ITileDecoder interface
ynse01 Jul 9, 2024
be134e8
Various tiling bug fixes
ynse01 Jul 9, 2024
30d7c09
More bug fixes
ynse01 Jul 11, 2024
a1f0f9e
Merge remote-tracking branch 'upstream/main' into heic-support
ynse01 Jul 11, 2024
a4ba77b
Implement mode info indexing in frame buffer
ynse01 Jul 12, 2024
471e3d3
Fix issue in ReadLoopRestorationParameters(): LoopRestorationParamete…
brianpopow Jul 12, 2024
c445729
Fix issue in coefficient parsing
ynse01 Jul 12, 2024
d527d0e
Rename ChannelCount property to PlaneCount
ynse01 Jul 13, 2024
68be923
PlaneCount calculation inside ObuColorConfig
ynse01 Jul 13, 2024
a6a32d4
TransformInfo retrieval changes
ynse01 Jul 13, 2024
d762207
Transform Unit Count variable rename
ynse01 Jul 13, 2024
cb736d7
Continue implement case !IsReducedStillPictureHeader in ReadUncompres…
brianpopow Jul 14, 2024
541ab35
Fix issue in Transform Unit Count
ynse01 Jul 15, 2024
441949f
Simplify code by using constants
ynse01 Jul 15, 2024
5d41465
Renaming Superblock variables
ynse01 Jul 16, 2024
4e1aca9
More Superblock renaming
ynse01 Jul 19, 2024
3976aa2
Merge branch 'main' into heic-support
ynse01 Jul 19, 2024
1812983
Improvements to pretty print routines for OBU headers
ynse01 Jul 19, 2024
62728b6
Add test bitreader test ReadLiteral32BitsWithMsbSet()
brianpopow Jul 20, 2024
38a6f43
Add suggestion howto change ReadLiteral()
brianpopow Jul 20, 2024
72c4263
Add tests for ReadSignedFromUnsigned and ReadLittleEndian
brianpopow Jul 20, 2024
0313bdf
Replace Av1BitStreamReader implementation
ynse01 Jul 20, 2024
cb3f51b
Add tests for ReadUnsignedVariableLength()
brianpopow Jul 20, 2024
64f4241
Additional tests for ReadLittleEndianBytes128()
brianpopow Jul 20, 2024
e8b7682
Also assert bit position
brianpopow Jul 20, 2024
c524346
OBU reader bug fixes
ynse01 Jul 20, 2024
e45bf57
Combine Delta parameters implementation
ynse01 Jul 21, 2024
6e10cc9
Few Obu unit tests taken over from libgav1
ynse01 Jul 21, 2024
4c019f5
Bug fixes in TileDecoder, inspired by libgav1
ynse01 Jul 21, 2024
22b7c72
Implement Read Chroma from Luma syntax
ynse01 Jul 21, 2024
4323205
Misc tile parsing fixes
ynse01 Jul 21, 2024
984d0ac
Additional unit tests for OBU bitstream
ynse01 Jul 23, 2024
e4743ce
Fix build
ynse01 Jul 23, 2024
26d8c22
Scan order unit test and fixes
ynse01 Jul 25, 2024
73718f1
Unit tests for block and transform sizes
ynse01 Jul 25, 2024
08cbc5a
Unit test for Partition Type
ynse01 Jul 26, 2024
5d27f30
Add extremely small AVIF test image
ynse01 Jul 26, 2024
fa2e1b0
Add tests for Orange4x4 image
ynse01 Jul 26, 2024
9409a12
Merge branch 'main' into heic-support
ynse01 Jul 26, 2024
96329f4
Rename TileDecoder into TileReader
ynse01 Jul 27, 2024
056814f
Namespace update
ynse01 Jul 27, 2024
ab2ae29
ObuWriter improvements
ynse01 Aug 4, 2024
8b28187
Merge remote-tracking branch 'upstream/main' into heic-support
ynse01 Aug 4, 2024
b8322fc
Merge in required changes from upstream
ynse01 Aug 4, 2024
23bf5de
Obu reading and writing improvements
ynse01 Aug 4, 2024
729f351
Obu bitstream writer writes to Span iso Stream
ynse01 Aug 4, 2024
2bac577
Fix AutoExpandingMemoryTest
ynse01 Aug 4, 2024
0b1639b
Sync method arguments
ynse01 Aug 5, 2024
1385266
Binary identical OBU read write round trip
ynse01 Aug 5, 2024
1fe733a
Merge remote-tracking branch 'upstream/main' into heic-support
ynse01 Aug 6, 2024
0ff8911
Introduce Av1FrameBuffer class and renaming others
ynse01 Aug 9, 2024
4f4ace4
Skeleton code for pixel pipeline
ynse01 Aug 19, 2024
d93b89c
Merge remote-tracking branch 'upstream/main' into heic-support
ynse01 Aug 19, 2024
2b60337
Rename according to naming scheme
ynse01 Aug 19, 2024
dfff87e
Implement interface in HeifMetadata
ynse01 Aug 19, 2024
d748a0d
Introduce DC predictors
ynse01 Aug 20, 2024
c9baaf0
Prediction decoding
ynse01 Aug 24, 2024
aa1b299
Implement Block Decoder
ynse01 Aug 24, 2024
ad57a99
Skeleton code for Transform
ynse01 Sep 5, 2024
fd76bd8
Disable test which has no implementation yet
ynse01 Sep 5, 2024
97052eb
Implement 4x4 forward DCT transform
ynse01 Sep 30, 2024
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
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,6 @@
*.ico filter=lfs diff=lfs merge=lfs -text
*.cur filter=lfs diff=lfs merge=lfs -text
*.ani filter=lfs diff=lfs merge=lfs -text
*.heic filter=lfs diff=lfs merge=lfs -text
*.hif filter=lfs diff=lfs merge=lfs -text
*.avif filter=lfs diff=lfs merge=lfs -text
18 changes: 16 additions & 2 deletions ImageSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{815C0625-CD3
ProjectSection(SolutionItems) = preProject
src\Directory.Build.props = src\Directory.Build.props
src\Directory.Build.targets = src\Directory.Build.targets
src\README.md = src\README.md
src\ImageSharp.ruleset = src\ImageSharp.ruleset
src\README.md = src\README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageSharp", "src\ImageSharp\ImageSharp.csproj", "{2AA31A1F-142C-43F4-8687-09ABCA4B3A26}"
Expand Down Expand Up @@ -215,6 +215,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "issues", "issues", "{5C9B68
ProjectSection(SolutionItems) = preProject
tests\Images\Input\Jpg\issues\issue-1076-invalid-subsampling.jpg = tests\Images\Input\Jpg\issues\issue-1076-invalid-subsampling.jpg
tests\Images\Input\Jpg\issues\issue-1221-identify-multi-frame.jpg = tests\Images\Input\Jpg\issues\issue-1221-identify-multi-frame.jpg
tests\Images\Input\Jpg\issues\issue-2067-comment.jpg = tests\Images\Input\Jpg\issues\issue-2067-comment.jpg
tests\Images\Input\Jpg\issues\issue1006-incorrect-resize.jpg = tests\Images\Input\Jpg\issues\issue1006-incorrect-resize.jpg
tests\Images\Input\Jpg\issues\issue1049-exif-resize.jpg = tests\Images\Input\Jpg\issues\issue1049-exif-resize.jpg
tests\Images\Input\Jpg\issues\Issue159-MissingFF00-Progressive-Bedroom.jpg = tests\Images\Input\Jpg\issues\Issue159-MissingFF00-Progressive-Bedroom.jpg
Expand All @@ -238,7 +239,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "issues", "issues", "{5C9B68
tests\Images\Input\Jpg\issues\issue750-exif-tranform.jpg = tests\Images\Input\Jpg\issues\issue750-exif-tranform.jpg
tests\Images\Input\Jpg\issues\Issue845-Incorrect-Quality99.jpg = tests\Images\Input\Jpg\issues\Issue845-Incorrect-Quality99.jpg
tests\Images\Input\Jpg\issues\issue855-incorrect-colorspace.jpg = tests\Images\Input\Jpg\issues\issue855-incorrect-colorspace.jpg
tests\Images\Input\Jpg\issues\issue-2067-comment.jpg = tests\Images\Input\Jpg\issues\issue-2067-comment.jpg
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "fuzz", "fuzz", "{516A3532-6AC2-417B-AD79-9BD5D0D378A0}"
Expand Down Expand Up @@ -661,6 +661,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Qoi", "Qoi", "{E801B508-493
tests\Images\Input\Qoi\wikipedia_008.qoi = tests\Images\Input\Qoi\wikipedia_008.qoi
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Heif", "Heif", "{BA5D603A-C84C-43E5-B300-8BB886B02936}"
ProjectSection(SolutionItems) = preProject
tests\Images\Input\Heif\dwsample-heic-640.heic = tests\Images\Input\Heif\dwsample-heic-640.heic
tests\Images\Input\Heif\image1.heic = tests\Images\Input\Heif\image1.heic
tests\Images\Input\Heif\image2.heic = tests\Images\Input\Heif\image2.heic
tests\Images\Input\Heif\image3.heic = tests\Images\Input\Heif\image3.heic
tests\Images\Input\Heif\image4.heic = tests\Images\Input\Heif\image4.heic
tests\Images\Input\Heif\IMG-20230508-0053.hif = tests\Images\Input\Heif\IMG-20230508-0053.hif
tests\Images\Input\Heif\Irvine_CA.avif = tests\Images\Input\Heif\Irvine_CA.avif
tests\Images\Input\Heif\jpeg444_xnconvert.avif = tests\Images\Input\Heif\jpeg444_xnconvert.avif
tests\Images\Input\Heif\Orange4x4.avif = tests\Images\Input\Heif\Orange4x4.avif
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Icon", "Icon", "{95E45DDE-A67D-48AD-BBA8-5FAA151B860D}"
ProjectSection(SolutionItems) = preProject
tests\Images\Input\Icon\aero_arrow.cur = tests\Images\Input\Icon\aero_arrow.cur
Expand Down Expand Up @@ -720,6 +733,7 @@ Global
{670DD46C-82E9-499A-B2D2-00A802ED0141} = {E1C42A6F-913B-4A7B-B1A8-2BB62843B254}
{5DFC394F-136F-4B76-9BCA-3BA786515EFC} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
{E801B508-4935-41CD-BA85-CF11BFF55A45} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
{BA5D603A-C84C-43E5-B300-8BB886B02936} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
{95E45DDE-A67D-48AD-BBA8-5FAA151B860D} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
2 changes: 1 addition & 1 deletion shared-infrastructure
3 changes: 3 additions & 0 deletions src/ImageSharp/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Cur;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Heif;
using SixLabors.ImageSharp.Formats.Ico;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Pbm;
Expand Down Expand Up @@ -213,6 +214,7 @@ public void Configure(IImageFormatConfigurationModule configuration)
/// <see cref="TiffConfigurationModule"/>.
/// <see cref="WebpConfigurationModule"/>.
/// <see cref="QoiConfigurationModule"/>.
/// <see cref="HeifConfigurationModule"/>.
/// </summary>
/// <returns>The default configuration of <see cref="Configuration"/>.</returns>
internal static Configuration CreateDefaultInstance() => new(
Expand All @@ -225,6 +227,7 @@ public void Configure(IImageFormatConfigurationModule configuration)
new TiffConfigurationModule(),
new WebpConfigurationModule(),
new QoiConfigurationModule(),
new HeifConfigurationModule(),
new IcoConfigurationModule(),
new CurConfigurationModule());
}
11 changes: 11 additions & 0 deletions src/ImageSharp/Formats/Heif/Av1/Av1BitDepth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.ImageSharp.Formats.Heif.Av1;

internal enum Av1BitDepth : int
{
EightBit = 0,
TenBit = 1,
TwelveBit = 2,
}
11 changes: 11 additions & 0 deletions src/ImageSharp/Formats/Heif/Av1/Av1BitDepthExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.ImageSharp.Formats.Heif.Av1.Transform;

namespace SixLabors.ImageSharp.Formats.Heif.Av1;

internal static class Av1BitDepthExtensions
{
public static int GetBitCount(this Av1BitDepth bitDepth) => 8 + ((int)bitDepth << 1);
}
157 changes: 157 additions & 0 deletions src/ImageSharp/Formats/Heif/Av1/Av1BitStreamReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.ImageSharp.Formats.Heif.Av1;

internal ref struct Av1BitStreamReader
{
private readonly Span<byte> data;

public Av1BitStreamReader(Span<byte> data) => this.data = data;

public int BitPosition { get; private set; } = 0;

/// <summary>
/// Gets the number of bytes in the readers buffer.
/// </summary>
public readonly int Length => this.data.Length;

public void Reset() => this.BitPosition = 0;

public void Skip(int bitCount) => this.BitPosition += bitCount;

public uint ReadLiteral(int bitCount)
{
DebugGuard.MustBeBetweenOrEqualTo(bitCount, 0, 32, nameof(bitCount));

uint literal = 0;
for (int bit = bitCount - 1; bit >= 0; bit--)
{
literal |= this.ReadBit() << bit;
}

return literal;
}

internal uint ReadBit()
{
int byteOffset = Av1Math.DivideBy8Floor(this.BitPosition);
byte shift = (byte)(7 - Av1Math.Modulus8(this.BitPosition));
this.BitPosition++;
return (uint)((this.data[byteOffset] >> shift) & 0x01);
}

internal bool ReadBoolean() => this.ReadLiteral(1) > 0;

public ulong ReadLittleEndianBytes128(out int length)
{
// See section 4.10.5 of the AV1-Specification
DebugGuard.IsTrue((this.BitPosition & 0x07) == 0, $"Reading of Little Endian 128 value only allowed on byte alignment (offset {this.BitPosition}).");

ulong value = 0;
length = 0;
for (int i = 0; i < 56; i += 7)
{
uint leb128Byte = this.ReadLiteral(8);
value |= (leb128Byte & 0x7FUL) << i;
length++;
if ((leb128Byte & 0x80U) == 0)
{
break;
}
}

return value;
}

public uint ReadUnsignedVariableLength()
{
// See section 4.10.3 of the AV1-Specification
int leadingZerosCount = 0;
while (leadingZerosCount < 32)
{
uint bit = this.ReadLiteral(1);
if (bit == 1)
{
break;
}

leadingZerosCount++;
}

if (leadingZerosCount == 32)
{
return uint.MaxValue;
}

if (leadingZerosCount != 0)
{
uint basis = (1U << leadingZerosCount) - 1U;
uint value = this.ReadLiteral(leadingZerosCount);
return basis + value;
}

return 0;
}

public uint ReadNonSymmetric(uint n)
{
// See section 4.10.7 of the AV1-Specification
if (n <= 1)
{
return 0;
}

int w = (int)(Av1Math.FloorLog2(n) + 1);
uint m = (uint)((1 << w) - n);
uint v = this.ReadLiteral(w - 1);
if (v < m)
{
return v;
}

return (v << 1) - m + this.ReadLiteral(1);
}

public int ReadSignedFromUnsigned(int n)
{
// See section 4.10.6 of the AV1-Specification
int signedValue;
uint value = this.ReadLiteral(n);
uint signMask = 1U << (n - 1);
if ((value & signMask) == signMask)
{
// Prevent overflow by casting to long;
signedValue = (int)((long)value - (signMask << 1));
}
else
{
signedValue = (int)value;
}

return signedValue;
}

public uint ReadLittleEndian(int n)
{
// See section 4.10.4 of the AV1-Specification
DebugGuard.IsTrue(Av1Math.Modulus8(this.BitPosition) == 0, "Reading of Little Endian value only allowed on byte alignment");

uint t = 0;
for (int i = 0; i < 8 * n; i += 8)
{
t += this.ReadLiteral(8) << i;
}

return t;
}

public Span<byte> GetSymbolReader(int tileDataSize)
{
DebugGuard.IsTrue(Av1Math.Modulus8(this.BitPosition) == 0, "Symbol reading needs to start on byte boundary.");
int bytesRead = Av1Math.DivideBy8Floor(this.BitPosition);
Span<byte> span = this.data.Slice(bytesRead, tileDataSize);
this.Skip(tileDataSize << 3);
return span;
}
}
Loading
Loading