Skip to content

Commit ce24d1a

Browse files
donislawdevclaude
andauthored
An AVIF or a JPEG XL cut short loses the share of its picture asked for (#242)
* An AVIF or a JPEG XL cut short loses the share of its picture asked for An AVIF or a JPEG XL a damage cuts splits its padding around the picture, so the cut keeps the share of the picture it keeps of the whole file. Before, all of the padding stood in free boxes behind the picture, and a cut of any share took only padding and left the picture whole. An AVIF puts free boxes between the coder's meta box and its media data. The item locations give the picture's place in bytes from the start of the file, so every one moves by as much, and an offset of four bytes that the move takes past 4 GiB is widened to eight - only then, so a smaller file keeps the coder's meta box but for the numbers. A JPEG XL puts skip boxes between its file type box and its codestream, which knows nothing of where it sits: ffmpeg takes a JPEG XL with a free box near its start for a movie and refuses it, and a skip box, the same to the format, for nothing. The front is rounded to whole boxes by format.SplitFront, which PNG's front padding became with the size of its piece as a parameter. Files nothing cuts keep their bytes, and PNG's do cut or not. The structural checker allows padding right in front of the picture as well as behind it, and requires every item location of an AVIF to land inside its media data. Both formats leave the share ratchet. A new guard writes consecutive sizes and cuts a byte apart where the front starts, where it rounds to a box and where the padding behind runs out, decodes one file of each layout, and plans an AVIF of 5 GiB whose item location needs eight bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * A JPEG XL opens in ffmpeg: padding in skip boxes in front of the picture Breaking: no JPEG XL is byte for byte what 0.5.0 wrote. A JPEG XL carried its padding in a free box behind its codestream. ffmpeg 7.1.5 with libjxl never finishes a JPEG XL with any box behind its codestream - free, skip or private, eight bytes or a thousand - so it read no file this tool wrote. ffmpeg 9.0.1 tells a JPEG XL by its first two kilobytes and takes one with a free box there for a movie, so it refused every one below about 3.8 KB. All of the padding now stands in skip boxes between the file type box and the codestream box, and nothing follows the codestream. A skip box is the other name ISO 14496-12 section 8.1.2 gives a box of free space, and ffmpeg does not take it for a movie. Pillow, ImageMagick, the Windows Imaging Component, Chrome, djxl and both ffmpeg read the same pixels from 2 KB to 10 MB. A file a damage cuts keeps the rest of its padding behind the codestream, past the cut, so no file on disk has a box behind it. Sizes are unchanged and the box costs the same eight bytes. The two sentences about the smallest JPEG XL say a box of free space. The structural checker refuses a free box in a JPEG XL. The four golden JPEG XL values are remeasured, and a new guard requires the codestream to be the last box of a file nothing cuts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * One name for the box that pads an AVIF, a JPEG XL or an MP4 The changelog entry on cutting an AVIF or a JPEG XL said that files nothing cuts are unchanged, which the breaking entry on JPEG XL contradicts. It now says so of AVIF only. The sentences on why an AVIF, a JPEG XL or an MP4 cannot be smaller named the box that pads the file in three ways: a free box, a box of free space, and in Polish also by the box's type. Free space is what the tool calls room on a disk. All five sentences now say a padding box, as the PNG sentence says the padding chunk, in English and in Polish. The screen of a refused size is redrawn, still three lines. The guard that sweeps a cut AVIF or JPEG XL wraps the decoder's error, and fails on an end region, which neither format writes and which its count of what stands in front of the padding would miss. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7c64b28 commit ce24d1a

20 files changed

Lines changed: 1048 additions & 124 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ because it turns other people's test suites red.
4343
padding, which busybox reads, and the padding file inside the archive takes
4444
the rest, as it did for larger archives. Those archives are not byte for
4545
byte what 0.5.0 wrote. Every other tar.gz is unchanged.
46+
- **A JPEG XL now opens in ffmpeg.** ffmpeg 7.1 never finished one, and
47+
ffmpeg 9 took one below about 3.8 KB for a video and refused it, because of
48+
the padding box behind the picture. The padding now stands in front of the
49+
picture, in a box of the kind ffmpeg passes over, and nothing follows the
50+
picture. Pillow, ImageMagick, Windows, Chrome and libjxl show the same
51+
picture as before. Sizes are unchanged, but no JPEG XL is byte for byte what
52+
0.5.0 wrote.
4653

4754
### Added
4855

@@ -116,6 +123,14 @@ because it turns other people's test suites red.
116123
manifest says so: a few percent of a small PDF, falling in the index, and
117124
one keeping 10 billion bytes or more, which the index cannot reach. Files
118125
nothing cuts are unchanged.
126+
- **`truncate` cuts the picture of an AVIF or a JPEG XL by the percentage you
127+
set.** A cut used to take only padding, leaving the whole picture. Now
128+
`--damage truncate:missing=50` removes half of the coded picture, as an
129+
interrupted download does. Chrome, Firefox, Windows, libavif and Pillow
130+
refuse the cut AVIF. Half of a JPEG XL still shows the whole picture at
131+
lower detail in Chrome and in libjxl with partial files allowed, because the
132+
format sends a coarse picture first. A cut of 90% shows nothing. An AVIF
133+
nothing cuts is unchanged. Every JPEG XL changes (see Breaking).
119134

120135
- **tfg is on npm, for tests in Node.**
121136
`npm install --save-dev testing-files-generator` gives Playwright, Vitest

‎internal/format/avif/avif.go‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
// sixty four bit box length that files above four gigabytes need. There is no
3636
// dead zone above the minimum. Pillow reads every one of them and refuses a
3737
// truncated or corrupted file, which is what makes it a witness. ffprobe reads
38-
// the broken ones too, so it is not.
38+
// the broken ones too, so it is not. A file a damage will cut has part of its
39+
// free boxes in front of the media data instead (cut.go).
3940
package avif
4041

4142
import (
@@ -166,6 +167,9 @@ type memo struct {
166167
// which is what makes every size above the minimum reachable a byte at a
167168
// time.
168169
total int64
170+
// cutAt is where a damage will cut the file, zero when nothing does
171+
// (format.Request.CutAt).
172+
cutAt int64
169173
}
170174

171175
func (generator) Plan(r format.Request) (format.Plan, error) {
@@ -187,7 +191,7 @@ func (generator) Plan(r format.Request) (format.Plan, error) {
187191
if err := checkFits(r.Bytes, m); err != nil {
188192
return format.Plan{}, err
189193
}
190-
m.total = r.Bytes
194+
m.total, m.cutAt = r.Bytes, r.CutAt
191195

192196
p := format.Plan{
193197
Bytes: r.Bytes,
@@ -244,7 +248,7 @@ const holdsPerPixel = 8
244248
func checkFits(want int64, m memo) error {
245249
floor := int64(minimumBytes)
246250
reason := core.Says("avif.MinimumReason",
247-
"the smallest picture this format draws codes to %d B at worst, and the file always carries a free box, which costs %d B even when it holds nothing",
251+
"the smallest picture this format draws codes to %d B at worst, and the file always carries a padding box, which costs %d B even when it holds nothing",
248252
core.A("Coded", minimumBytes-boxHeader), core.A("Box", boxHeader))
249253

250254
// A picture named by hand can be larger than the smallest one, and then its
@@ -254,7 +258,7 @@ func checkFits(want int64, m memo) error {
254258
if own := int64(len(m.coded)) + boxHeader; own > floor {
255259
floor = own
256260
reason = core.Says("avif.MinimumReasonOwn",
257-
"a %dx%d picture at quality %d codes to %d B, and the file always carries a free box, which costs %d B even when it holds nothing",
261+
"a %dx%d picture at quality %d codes to %d B, and the file always carries a padding box, which costs %d B even when it holds nothing",
258262
core.A("Width", m.width), core.A("Height", m.height), core.A("Quality", m.quality), core.A("Coded", len(m.coded)), core.A("Box", boxHeader))
259263
}
260264
}
@@ -491,6 +495,12 @@ func (generator) Write(ctx context.Context, w io.Writer, p format.Plan) error {
491495
m.width, m.height, len(coded), m.total))
492496
}
493497

498+
if m.cutAt > 0 {
499+
if done, err := writeForACut(ctx, w, m, coded); done || err != nil {
500+
return err
501+
}
502+
}
503+
494504
// The map: the coder's boxes, its media data the content, and the free
495505
// boxes after them that make up the size.
496506
markBoxes(w, coded)

0 commit comments

Comments
 (0)