Skip to content

Commit 209cdfc

Browse files
matmenclaude
andcommitted
Decode AVIF: AV1 ported from dav1d, RGB like libavif
The AV1 decoder (src/codecs/av1) is ported from dav1d 1.5.1 and decodes exactly like it: 4:2:0, 4:2:2, 4:4:4 and monochrome, 8-12 bit, every intra coding tool, every post filter and film grain. Verified against dav1d on the 240 libaom test vectors, 30 aomenc configurations and 96 film grain cases. Its tables are generated from dav1d (scripts/av1-tables.c) and packed (scripts/av1-pack.js). The container (src/codecs/avif.ts): the primary item or a grid (alpha per tile too), alpha (also premultiplied), clap/irot/imir, colr, the first frame of image sequences. Converted to RGB like libavif's reference path (single precision, bilinear chroma), so every SDR file of the AVIF conformance set that avifdec decodes is pixel for pixel the same. PQ and HLG are converted to sRGB with toneMapping. Fixtures from scripts/avif-fixtures.py (aomenc, our own container writer, avifdec references) in tests/avif. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJgRK5qpwJEbHPuFv6kgAQ
1 parent fe3e9a2 commit 209cdfc

45 files changed

Lines changed: 6775 additions & 22 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,21 @@
1414
AND masks (32-bit ones with alpha, unless it's zero everywhere), or PNGs.
1515
- Netpbm: plain and raw, samples of up to 16 bits, PAM's tuple types.
1616
- Radiance HDRs: RGBE and XYZE, every orientation, both run length encodings.
17-
- `DecodeOptions.toneMapping`: HDRs' and PFMs' linear light can be brighter than white. By default (`'reinhard'`) the
18-
brightest parts are compressed like Reinhard's extended tone mapping operator does, the brightest pixel becomes
19-
white (without the operator's automatic exposure, which turns night scenes grey); images no brighter than white
20-
stay as they are. `'clamp'` clamps everything brighter than white instead. Either way the result is sRGB.
17+
- `Image.decode` reads AVIFs, and `ImageType` recognizes them (`'avif'`, `ImageType.isAVIF`). The AV1 decoder is
18+
ported from dav1d and decodes exactly like it: 4:2:0, 4:2:2, 4:4:4 and monochrome, 8, 10 and 12 bits, every intra
19+
coding tool (palettes, intra block copy, filter intra, chroma from luma, quantizer matrices, lossless, 128x128
20+
superblocks, tiles), every post filter (deblocking, CDEF, superres, loop restoration) and film grain. The container:
21+
the primary item or a grid of them, alpha (also premultiplied, or one per grid tile), the crop, rotation and
22+
mirroring properties (`clap`, `irot`, `imir`), the color description of `colr`, and the first frame of image
23+
sequences (also from `Animation.decode`). Converted to RGB like libavif does, so the pixels are libavif's; HDR
24+
AVIFs (PQ and HLG, BT.2020 and Display P3 primaries) are converted to sRGB with `toneMapping`. Not supported:
25+
frames that use other frames (inter frames): image sequences only give their first frame, and of layered images
26+
whose shown layer is inter coded the highest intra coded layer is shown if it has the image's size (else they
27+
throw).
28+
- `DecodeOptions.toneMapping`: HDRs', PFMs' and HDR AVIFs' light can be brighter than (SDR) white. By default
29+
(`'reinhard'`) the brightest parts are compressed like Reinhard's extended tone mapping operator does, the brightest
30+
pixel becomes white (without the operator's automatic exposure, which turns night scenes grey); images no brighter
31+
than white stay as they are. `'clamp'` clamps everything brighter than white instead. Either way the result is sRGB.
2132
- `Image.encodeBMP`, `Image.encodeQOI` and `Image.encodePNM`:
2233
- BMPs with a palette of 2, 16 or 256 colors if an opaque image has at most that many, else 24-bit, or 32-bit with
2334
an alpha mask (a version 4 header) if the image has transparency

‎README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ deno add jsr:@matmen/imagescript # Deno (npm:imagescript works too)
3333
- Radiance HDRs (RGBE and XYZE, every orientation), tone mapped to sRGB like Reinhard's operator does (or clamped,
3434
`toneMapping`)
3535
- TGAs (color mapped, truecolor and greyscale; uncompressed and run length encoded)
36+
- AVIFs (4:2:0, 4:2:2, 4:4:4 and monochrome, 8-12 bit; grids, alpha, crop, rotation and mirroring; the first frame
37+
of image sequences; bit-exact with dav1d and libavif), HDR ones (PQ, HLG) tone mapped to sRGB
3638
- JPEGs, TIFFs, PNGs and WebPs are rotated as their Exif (or, for PNGs, XMP) orientation says
3739
- images larger than 268 megapixels (`maxPixels`) are rejected before decoding, so small files declaring huge images
3840
can't exhaust the memory
@@ -115,7 +117,9 @@ The JPEG codec is based in part on the work of the Independent JPEG Group (libjp
115117
and reproduce [libwebp](https://chromium.googlesource.com/webm/libwebp)'s color conversion, the TrueType hinting follows
116118
[FreeType](https://freetype.org)'s interpreter (portions of this software are copyright © 2026 The FreeType Project),
117119
the text shaping is ported from [HarfBuzz](https://github.com/harfbuzz/harfbuzz) and uses data of the
118-
[Unicode Character Database](https://www.unicode.org/ucd/).
120+
[Unicode Character Database](https://www.unicode.org/ucd/). The AV1 decoder is ported from
121+
[dav1d](https://code.videolan.org/videolan/dav1d), AVIFs are converted to RGB like
122+
[libavif](https://github.com/AOMediaCodec/libavif) does.
119123
See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for their licenses.
120124

121125
---

‎THIRD_PARTY_NOTICES.md‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ following projects, under their own licenses. Each derived file says so in its h
1313
| [RFC 9649](https://www.rfc-editor.org/rfc/rfc9649) (WebP Image Format) | `src/codecs/vp8l.ts` (lossless bitstream, short distance code table), `src/codecs/webp.ts` (container) | BSD-3-Clause (Revised BSD) |
1414
| [RFC 6386](https://www.rfc-editor.org/rfc/rfc6386) (VP8 Data Format and Decoding Guide, with the libvpx reference decoder) | `src/codecs/vp8-tables.ts` (probability and quantizer tables), `src/codecs/vp8.ts` (decoding procedures) | BSD-3-Clause |
1515
| [libwebp](https://chromium.googlesource.com/webm/libwebp) | `src/codecs/vp8.ts` (YUV to RGB conversion, "fancy" chroma upsampling), `src/codecs/webp.ts` (animation frame blending) | BSD-3-Clause |
16+
| [dav1d](https://code.videolan.org/videolan/dav1d) by VideoLAN, the dav1d authors and Two Orioles | `src/codecs/av1/*` (the AV1 decoder: bitstream parsing, symbol decoding, prediction, inverse transforms, loop filters, film grain; the tables in `src/codecs/av1/data.ts`, generated from dav1d by `scripts/av1-tables.c`) | BSD-2-Clause |
17+
| [libavif](https://github.com/AOMediaCodec/libavif) by Joe Drago | `src/codecs/avif.ts` (YUV to RGB conversion with bilinear chroma upsampling, alpha range expansion and unpremultiplication) | BSD-2-Clause |
1618
| [FreeType](https://freetype.org) | `src/render/hinting.ts` (behavior and parts of the logic of the TrueType interpreter) | FreeType License |
1719
| [QOI](https://github.com/phoboslab/qoi) by Dominic Szablewski | `src/codecs/qoi.ts` (the reference encoder's choices of operations) | MIT |
1820
| [Pillow](https://github.com/python-pillow/Pillow) | `src/util/resample.ts` (filter coefficients, box, bilinear and bicubic filters) | MIT-CMU |
@@ -149,6 +151,69 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
149151
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
150152
```
151153

154+
## dav1d
155+
156+
The AV1 decoder is ported from dav1d 1.5.1 and decodes exactly like it (the frames of its intra coding tools, with
157+
every post filter and film grain); its tables are generated from dav1d's. The ported files are Copyright © 2018-2021,
158+
VideoLAN and dav1d authors, and Copyright © 2018, Two Orioles, LLC, under dav1d's license. AV1 is covered by the
159+
[Alliance for Open Media Patent License 1.0](https://aomedia.org/license/patent-license/).
160+
161+
```
162+
Copyright © 2018-2019, VideoLAN and dav1d authors
163+
All rights reserved.
164+
165+
Redistribution and use in source and binary forms, with or without
166+
modification, are permitted provided that the following conditions are met:
167+
168+
1. Redistributions of source code must retain the above copyright notice, this
169+
list of conditions and the following disclaimer.
170+
171+
2. Redistributions in binary form must reproduce the above copyright notice,
172+
this list of conditions and the following disclaimer in the documentation
173+
and/or other materials provided with the distribution.
174+
175+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
176+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
177+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
178+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
179+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
180+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
181+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
182+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
183+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
184+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
185+
```
186+
187+
## libavif
188+
189+
The conversion of decoded AVIFs to RGB reproduces libavif's reference conversion (in floating point, with bilinear
190+
chroma upsampling), so the pixels are the ones libavif gives.
191+
192+
```
193+
Copyright 2019 Joe Drago. All rights reserved.
194+
195+
Redistribution and use in source and binary forms, with or without
196+
modification, are permitted provided that the following conditions are met:
197+
198+
1. Redistributions of source code must retain the above copyright notice, this
199+
list of conditions and the following disclaimer.
200+
201+
2. Redistributions in binary form must reproduce the above copyright notice,
202+
this list of conditions and the following disclaimer in the documentation
203+
and/or other materials provided with the distribution.
204+
205+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
206+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
207+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
208+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
209+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
211+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
212+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
213+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
214+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
215+
```
216+
152217
## FreeType
153218

154219
Portions of this software are copyright © 2026 The FreeType Project (https://freetype.org). All rights reserved.

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
"picture",
5252
"thumbnail",
5353
"webp",
54+
"avif",
55+
"av1",
5456
"apng",
5557
"tiff",
5658
"tga",

‎scripts/av1-pack.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Compresses the packed tables of the output of scripts/av1-tables.c (stdin to stdout): the PACKED constant's base64
2+
// data becomes the base64 of a zlib stream of it
3+
const zlib = require('zlib');
4+
5+
let text = require('fs').readFileSync(0, 'utf8');
6+
text = text.replace(/export const PACKED = '([^']*)'/, (_, data) => `export const PACKED = '${zlib.deflateSync(Buffer.from(data, 'base64'), { level: 9 }).toString('base64')}'`);
7+
process.stdout.write(text);

0 commit comments

Comments
 (0)