Skip to content

Commit 0e49ee5

Browse files
donislawdevclaude
andauthored
An MP4 or a WebM cut short loses the share of its pictures asked for (#243)
* An MP4 or a WebM cut short loses the share of its pictures asked for --damage truncate cut only the padding of a film of 1 MB or more, because both containers kept all of it behind the pictures. A film a damage will cut now splits its padding around them: free boxes between moov and mdat in an MP4, its chunk places moved with them and widened to co64 only when the media data then passes 4 GiB, and a Void between the Tracks and the first cluster in a WebM, its Cues moved with it. ffmpeg's faststart and mkvmerge write the same layouts. A WebM a damage will cut is coded twice, once for the lengths of its clusters, as every MP4 is. So its map, and the manifest's cut, gives the length of its pictures, which it left empty. Films nothing cuts are byte for byte what they were. The structural checker allows both layouts and still requires the tables to land. Both formats leave the ratchet of formats that do not cut at the share. The new guard holds every size and cut it sweeps to the share, and every sample to the bytes of the same sample in the film nothing cuts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * The cut guards reach every case they plan and keep a witness behind the content An MP4 no longer cuts in the padding behind its pictures, and it was the case that showed the manifest's note for a cut there. The note's case moves to the one format that still cuts there: a WebP of 4 KiB cut by 1%, whose picture grows to fill the file and leaves more behind it than the cut takes. The share sweep asked only the files its manifest listed, so a format that lost one planned file passed on the others. A planned file the manifest leaves out is now an error, with a canary. A switch over the roles of a film's map names its padding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ce24d1a commit 0e49ee5

12 files changed

Lines changed: 790 additions & 90 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ because it turns other people's test suites red.
131131
lower detail in Chrome and in libjxl with partial files allowed, because the
132132
format sends a coarse picture first. A cut of 90% shows nothing. An AVIF
133133
nothing cuts is unchanged. Every JPEG XL changes (see Breaking).
134+
- **`truncate` cuts the pictures of an MP4 or a WebM film by the percentage
135+
you set.** A cut used to take only padding, leaving every frame. Now
136+
`--damage truncate:missing=50` removes half of the coded pictures, as an
137+
interrupted download does, and ffmpeg gives back the frames in front of the
138+
cut. Chrome and Firefox stop a cut MP4 with an error and end a cut WebM
139+
without one. The manifest of a cut WebM now says how long its pictures are,
140+
where it said nothing. Films nothing cuts are unchanged.
134141

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

‎internal/format/mp4/cut.go‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package mp4
2+
3+
import (
4+
"math"
5+
6+
"github.com/donislawdev/TestingFilesGenerator/internal/format"
7+
)
8+
9+
// splitForACut settles how much of the padding goes between the movie box and
10+
// the media data of a film a damage will cut after its first cutAt bytes, so
11+
// that the cut keeps the share of the media data it keeps of the whole file
12+
// (format.Request.CutAt), and gives back the padding left behind the media
13+
// data. content is the media data's samples, as the first pass measured them.
14+
//
15+
// With all of the padding behind the media data, as an uncut film has it, a
16+
// large film cut at any share kept every picture and lost only padding. A free
17+
// box may stand anywhere among the top level boxes, and readers skip it in
18+
// front of the media data as they do behind it - ffmpeg's faststart writes one
19+
// there (docs/TRUNCATE-2026-11-11.md section 14.4). The one thing a box there
20+
// changes is where every chunk starts, which stco says in bytes from the start
21+
// of the file, so every place moves by the gap: front counts it, and the places
22+
// are written from front.
23+
//
24+
// The places take four bytes unless the layout already asked for eight. One
25+
// that the gap takes past 4 GiB widens the table to co64, as a film whose
26+
// pictures take their whole reserve past 4 GiB has it, and the movie box grows
27+
// by four bytes a chunk - only then. When the padding cannot give up those
28+
// bytes and still hold a free box, the film is laid out as one nothing cuts,
29+
// and the manifest's cut says where the cut fell. That takes a film whose
30+
// pictures came within four bytes a chunk of their whole reserve, which the
31+
// guard cannot reach (section 14.3).
32+
//
33+
// A film whose share wants nothing in front - and every film nothing cuts - is
34+
// laid out as one nothing cuts. It sets l.gap, and l.wide when it widens.
35+
func splitForACut(l *layout, content, total, cutAt int64) (back int64) {
36+
before := int64(len(l.ftyp)) + l.moovLen()
37+
mdat := l.front() - before + content
38+
pad := total - before - mdat
39+
l.gap = format.SplitFront(pad, before, mdat, cutAt, total, minFree)
40+
if l.gap > 0 && !l.wide && l.front()+content > math.MaxUint32 {
41+
grown := (wideChunk - narrowChunk) * l.chunks
42+
l.gap = 0
43+
if pad-grown >= minFree {
44+
l.wide = true
45+
l.gap = format.SplitFront(pad-grown, before+grown, mdat, cutAt, total, minFree)
46+
l.wide = l.gap > 0
47+
}
48+
}
49+
return total - l.front() - content
50+
}
51+
52+
// mediaData is how long the samples of the media data come to, chunk by
53+
// chunk.
54+
func (z sizes) mediaData(l layout) int64 {
55+
var n int64
56+
l.eachChunk(func(first, samples int64) { n += z.chunk(first, samples) })
57+
return n
58+
}

‎internal/format/mp4/layout.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ type layout struct {
4343
// wide is whether the chunks' places take eight bytes each (co64), and
4444
// bigData whether the media data's length does - each only when a film
4545
// whose pictures take their whole reserve would need it, so a smaller film
46-
// never has a field it could not fill.
46+
// never has a field it could not fill. A film a damage will cut may widen
47+
// its places for the padding in front of the media data (cut.go).
4748
wide, bigData bool
49+
// gap is the padding between the movie box and the media data, nothing
50+
// but in a film a damage will cut (cut.go). Planning counts none.
51+
gap int64
4852

4953
ftyp, mvhd, tkhd, mdhd, hdlr, vmhd, dinf, stsd, stts []byte
5054
}
@@ -92,13 +96,13 @@ func (l layout) boundContent() int64 {
9296
}
9397

9498
// front is where the media data's content starts: the file type, the movie
95-
// box and the media data's header.
99+
// box, the padding in front of the media data and the media data's header.
96100
func (l layout) front() int64 {
97101
head := int64(isobmff.BoxHeader)
98102
if l.bigData {
99103
head += 8
100104
}
101-
return int64(len(l.ftyp)) + l.moovLen() + head
105+
return int64(len(l.ftyp)) + l.moovLen() + l.gap + head
102106
}
103107

104108
// need is the file without its padding when every picture takes its whole

‎internal/format/mp4/mp4.go‎

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
// with the probe at 8, 9, 15, 16 and 1 048 576 bytes and with none. A free box
2222
// is always written, at least its eight bytes, which leaves no size above the
2323
// minimum that cannot be reached, and one larger than a box length can say is
24-
// several (internal/format/isobmff).
24+
// several (internal/format/isobmff). A film a damage will cut splits them
25+
// around the media data, so the cut keeps the share of the pictures asked for
26+
// (cut.go).
2527
package mp4
2628

2729
import (
@@ -138,34 +140,54 @@ func (generator) Write(ctx context.Context, w io.Writer, p format.Plan) error {
138140
}
139141
defer pics.Close()
140142

141-
// The map: the file type and the movie box, then the media data box with
142-
// the pictures, then the free boxes that make up the size. The movie box
143-
// is the same length whatever the pictures come to, so it is marked before
144-
// a byte of it goes out, and the rest once the first pass has the length.
145-
mdatAt := int64(len(l.ftyp)) + l.moovLen()
146-
format.Mark(w, format.RoleStructure, 0, mdatAt)
143+
// The padding behind the media data is what the first pass's lengths
144+
// leave, not what the second pass wrote, so a second pass that came out
145+
// different leaves the file a different size from the plan - which the
146+
// engine refuses - rather than a movie box that describes samples it does
147+
// not carry. A film a damage will cut has part of its padding in front of
148+
// the media data (cut.go).
149+
content := z.mediaData(l)
150+
back := splitForACut(&l, content, p.Bytes, f.CutAt)
151+
if back < 0 || back > 0 && back < minFree || l.gap+back < minFree {
152+
return core.Defect(fmt.Errorf("mp4: the pictures came to %d B of media data and the file is %d B, which leaves no room for the free box every one of these carries",
153+
content, p.Bytes))
154+
}
147155

148-
o := &out{w: w, buf: make([]byte, 0, 64<<10)}
149-
content := l.writeMovie(o, z)
156+
// The map: the file type and the movie box, the free boxes in front of the
157+
// media data when a damage will cut the film, then the media data box with
158+
// the pictures, then the free boxes that make up the size. The first pass
159+
// has every length, so all of it is marked before a byte goes out.
160+
movie := int64(len(l.ftyp)) + l.moovLen()
161+
mdatAt := movie + l.gap
162+
format.Mark(w, format.RoleStructure, 0, movie)
163+
if l.gap > 0 {
164+
format.Mark(w, format.RolePadding, movie, l.gap)
165+
}
150166
format.Mark(w, format.RoleContent, mdatAt, l.front()-mdatAt+content)
151-
format.Mark(w, format.RolePadding, l.front()+content, p.Bytes-l.front()-content)
167+
if back > 0 {
168+
format.Mark(w, format.RolePadding, l.front()+content, back)
169+
}
170+
171+
// Both sides of the padding draw their filler from the film's seed. Nobody
172+
// reads padding, and the side behind the media data lies past any cut.
173+
o := &out{w: w, buf: make([]byte, 0, 64<<10)}
174+
l.writeMovie(o, z)
175+
if l.gap > 0 {
176+
if o.flush(); o.err != nil {
177+
return o.err
178+
}
179+
if err := isobmff.WritePadding(ctx, w, f.Choice.Seed, l.gap); err != nil {
180+
return err
181+
}
182+
}
152183
o.mediaHeader(l.bigData, content)
153184
if err := writeSamples(ctx, o, l, pics); err != nil {
154185
return err
155186
}
156187
if o.flush(); o.err != nil {
157188
return o.err
158189
}
159-
// The padding is what the first pass's lengths leave, not what the second
160-
// pass wrote, so a second pass that came out different leaves the file a
161-
// different size from the plan - which the engine refuses - rather than a
162-
// movie box that describes samples it does not carry.
163-
pad := p.Bytes - l.front() - content
164-
if pad < minFree {
165-
return core.Defect(fmt.Errorf("mp4: the pictures came to %d B of media data and the file is %d B, which leaves no room for the free box every one of these carries",
166-
content, p.Bytes))
167-
}
168-
return isobmff.WritePadding(ctx, w, f.Choice.Seed, pad)
190+
return isobmff.WritePadding(ctx, w, f.Choice.Seed, back)
169191
}
170192

171193
// sizes is how long each picture's two samples came out in the first pass, by
@@ -232,8 +254,8 @@ func measure(ctx context.Context, f video.Film, st video.Stream) (sizes, *video.
232254
}
233255

234256
// writeMovie writes the file type and the movie box, with the sample tables
235-
// from the first pass's lengths, and gives back how long the media data is.
236-
func (l layout) writeMovie(o *out, z sizes) int64 {
257+
// from the first pass's lengths.
258+
func (l layout) writeMovie(o *out, z sizes) {
237259
o.write(l.ftyp)
238260
o.header("moov", l.moovLen())
239261
o.write(l.mvhd)
@@ -252,7 +274,7 @@ func (l layout) writeMovie(o *out, z sizes) int64 {
252274
l.writeDependencies(o)
253275
l.writeChunkRuns(o)
254276
l.writeSizes(o, z)
255-
return l.writeChunkPlaces(o, z)
277+
l.writeChunkPlaces(o, z)
256278
}
257279

258280
// writeSyncs is stss: every key frame, numbered from one. Each is a sample a
@@ -315,9 +337,10 @@ func (l layout) writeSizes(o *out, z sizes) {
315337
}
316338
}
317339

318-
// writeChunkPlaces is stco, or co64 when the layout asked for eight bytes, and
319-
// gives back where the last chunk ends, counted from the media data's start.
320-
func (l layout) writeChunkPlaces(o *out, z sizes) int64 {
340+
// writeChunkPlaces is stco, or co64 when the layout asked for eight bytes:
341+
// each chunk's place in bytes from the start of the file, which front - and so
342+
// any padding in front of the media data - is counted in.
343+
func (l layout) writeChunkPlaces(o *out, z sizes) {
321344
kind := "stco"
322345
if l.wide {
323346
kind = "co64"
@@ -333,7 +356,6 @@ func (l layout) writeChunkPlaces(o *out, z sizes) int64 {
333356
}
334357
content += z.chunk(first, samples)
335358
})
336-
return content
337359
}
338360

339361
// writeSamples is the second pass: every frame's sample, chunk by chunk, the

‎internal/format/video/plan.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
type Film struct {
1212
Choice Choice
1313
Settings Settings
14+
// CutAt is where a damage will cut the file, zero when nothing does
15+
// (format.Request.CutAt). Each container splits its padding around the
16+
// pictures for it.
17+
CutAt int64
1418
}
1519

1620
// Stream is the stream the film is written in, the one planning sized it by.
@@ -50,7 +54,7 @@ func Plan(formatID string, r format.Request, need func(Stream) int64, refuse fun
5054
Exact: true,
5155
Determinism: format.DeterminismByte,
5256
Properties: Facts(c, s),
53-
Memo: Film{Choice: c, Settings: s},
57+
Memo: Film{Choice: c, Settings: s, CutAt: r.CutAt},
5458
Work: st.Work(),
5559
Holds: Holds(c.Width, c.Height),
5660
}

‎internal/format/webm/cut.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package webm
2+
3+
import (
4+
"context"
5+
6+
"github.com/donislawdev/TestingFilesGenerator/internal/format"
7+
"github.com/donislawdev/TestingFilesGenerator/internal/format/video"
8+
)
9+
10+
// A film a damage will cut (format.Request.CutAt) has its padding split around
11+
// the clusters, so that the cut keeps the share of the clusters it keeps of the
12+
// whole file: a Void between the Tracks and the first cluster, and the rest in
13+
// the Void before the Cues - none there when the share wants all of it in
14+
// front, and the Cues follow the last cluster.
15+
//
16+
// With all of the padding behind the clusters, as an uncut film has it, a
17+
// large film cut at any share kept every picture and lost only padding. A Void
18+
// is an EBML element allowed at every level, and readers skip it in front of
19+
// the clusters as they do behind them - mkvmerge writes one there
20+
// (docs/TRUNCATE-2026-11-11.md section 14.4). The one thing a Void there
21+
// changes is where every cluster starts, which the Cues say from the start of
22+
// the segment's content, and they are written last from where the clusters
23+
// went. The SeekHead's places stay: Info and Tracks stand in front of the
24+
// Void, and the Cues end the file.
25+
//
26+
// How much goes in front depends on how long the clusters come to, and a
27+
// cluster's length is known only once its picture is coded. So a film a damage
28+
// will cut is coded twice, as every MP4 is: once for the lengths (measure) and
29+
// again to write it, the second pass taking every tile the first one kept
30+
// (video.Pictures.Again). A film nothing cuts is coded once, as before.
31+
32+
// measure is the first pass of a film a damage will cut: every cluster's
33+
// length, nothing written, so the padding can be split before the file has a
34+
// byte. It gives back what the clusters come to and the coder of the second
35+
// pass. The work coding is worth is reported here, and the second pass, which
36+
// codes only what this one could not keep, reports none.
37+
//
38+
// first is closed on every way out, by Again or by the caller's deferred
39+
// Close - closing it twice is harmless.
40+
func measure(ctx context.Context, l layout, first *video.Pictures) (int64, *video.Pictures, error) {
41+
var content uint64
42+
err := eachCluster(ctx, l, first, true, func(from, to int64) error {
43+
content += elementLen(idCluster, l.clusterContent(from, to, first.SampleLens()))
44+
return nil
45+
})
46+
if err != nil {
47+
return 0, nil, err
48+
}
49+
again, err := first.Again()
50+
if err != nil {
51+
return 0, nil, err
52+
}
53+
return int64(content), again, nil
54+
}
55+
56+
// splitForACut is how much of the padding pad goes in front of the clusters,
57+
// which come to content bytes after before bytes of the file, for a cut after
58+
// the file's first cutAt bytes of whole. Each side is nothing or a Void of at
59+
// least its two bytes (format.SplitFront).
60+
func splitForACut(pad, before, content, cutAt, whole int64) int64 {
61+
return format.SplitFront(pad, before, content, cutAt, whole, minVoid)
62+
}

0 commit comments

Comments
 (0)