Skip to content
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
28 changes: 22 additions & 6 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ VapourBox/
| `worker/src/cpu.rs` | The x86 deps CPU tier (`v3`/`v2`) — the single decision; see "CPU tiers" |
| `worker/src/source_decode.rs` | Decoder ffmpeg args for encode + preview: full stored frame, size-guarded, PTS reset after a seek, invalid colour tags cleared; the video start offset, an AVI's decoder delay, and the one seek decision, `source_seek` / `seek_arg`, the source grid every constant-rate decoder starts on (`decode_start`; see "Source Frame Size", "Colour metadata") |
| `worker/src/dvd_reader.rs` | DVD import: IFO parsing for the title picker, `title_cells` (a title's cells for a chapter range and angle), the copy plan, the NAV walk for interleaved cells, and the libdvdread copy (see "DVD Import") |
| `worker/src/source_model.rs` | `SourceModel` and `--probe-source`: every fact about a source a decoder needs, probed once (the frame-count rules included); `VideoJob::apply_source_model` fills a job from it |
| `worker/src/source_model.rs` | `SourceModel` and `--probe-source`: every fact about a source a decoder needs, probed once (the frame-count rules included, an MP4 edit list's discards too); `VideoJob::apply_source_model` fills a job from it, its frame count included |
| `worker/src/source_continuity.rs` | Timestamp resets in an MPEG-PS/TS: the rule, `--scan-timestamps`, and the continuous same-container copy decoded instead (see "Source Frame Size") |
| `worker/src/vspipe_failure.rs` | A failed encode's blame (encoder first, unless it only saw vspipe's empty pipe) and vspipe's stderr turned into the job error (see Debugging Tips #11) |
| `worker/src/source_timing.rs` | A variable-frame-rate source's stored frames placed on the constant-rate grid (see "Variable Frame Rate Sources") |
Expand Down Expand Up @@ -1188,11 +1188,27 @@ the **single** builder of both decoders' arguments (encode + preview):
misses a last picture whose packet has no PTS (DVD: 149 of 150), so the
probe decodes the last 3 s and counts it as ffmpeg's decoder stamps it
(previous frame + its duration; `untimed_last_picture`), never lowering
the count (task #26). A job **without** a count (CLI, tests) is still
counted by decoding (`-count_frames`, `resolve_source`): it is wrong on
AVI null frames, open GOPs and soft-telecine repeats but right on an
MP4 whose edit list discards pre-roll, where `nb_frames` (and so the
model) is 3 over (docs/reports/task-50-source-model).
the count (task #26).
- **An MP4/MOV edit list's discarded packets are not frames (task #54).**
The demuxer keeps an edit's pre-roll (and B pictures past its end) for
decoding and marks it discard (`D` in `packet=flags`); `nb_frames`
counts it (`small_clip.mp4`: 3154 stored, 3151 decoded). When the head
probe (`VIDEO_START_PROBE_ARGS`, now with `flags`,
`discards_from_probe`) or an MP4/MOV's tail probe (`probe_tail`, the
last 3 s, run with the others; `TailPictures`) sees one, the count is
the last **kept** picture's grid slot + 1. Never "packets not
discarded": a stream-copy cut leaves holes the grid fills
(`edit_list_bframes.mp4`: 146 stored, 125 kept, 128 slots). A file
whose discards are only mid-file (a multi-entry edit list) still counts
`nb_frames` (docs/reports/task-54-edit-list-count).
- **A job without a count takes the model's** (`apply_source_model`, over
its trim), as the app's always has. Only `VideoJob::counts_by_decoding`
(no model, or a raw MPEG-PS/TS whose timestamps reset in a job without
`continuity_checked`, whose model counts the last segment) is still
counted with `-count_frames` (`resolve_source`), which is wrong on AVI
null frames, open GOPs, soft-telecine repeats and holes. Audio
Restoration's drift reads the whole source's length the same way
(`model_frame_count`).
- **The output keeps the source's layout: sound first, picture delayed.**
`VideoJob::output_video_delay` is the one decision (0 when trimmed, when
there is no sound, or with Audio Restoration's `startWithPicture`); the
Expand Down
Binary file added Tests/TestResources/edit_list_bframes.mp4
Binary file not shown.
56 changes: 56 additions & 0 deletions Tests/TestResources/generate_edit_list_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
# Generate edit_list_bframes.mp4: an MP4 whose edit list discards pre-roll,
# with holes in its picture timestamps before the end (task #54).
#
# Why this fixture exists: an MP4/MOV edit list that starts after the media's
# first sample keeps the samples from the keyframe before it so the decoder
# can reach the first shown picture, and the mov demuxer marks them discard
# (ffprobe `packet=flags` "D"). They are never output, but `nb_frames` (the
# sample table) counts them, so a count from `nb_frames` asked the decoder for
# frames it never delivers (small_clip.mp4: 3154 stored, 3151 decoded). This
# one is harder: the stream copy's `-t` also drops the last B pictures, so the
# kept pictures' timestamps have holes, which the source grid fills by
# repeating the slot before (issue #174). Counting the packets not discarded
# gives 125, as does `-count_frames`; the grid decode, and so the right count,
# is the last kept picture's slot + 1: 128.
#
# Content: testsrc2 (every frame differs), 320x240 at 30000/1001, H.264 with
# 3 B-frames and a 48-frame GOP, 12 s, cut by stream copy at 2.3 s for 4.1 s.
# Committed: x264 versions differ, and a regenerated file may differ in its
# numbers (the script checks the shape, not the counts).
#
# Usage: ./generate_edit_list_test.sh [output_dir] [ffmpeg]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
OUTPUT_DIR="${1:-$SCRIPT_DIR}"
FFMPEG="${2:-ffmpeg}"
FFPROBE="$(dirname "$FFMPEG")/ffprobe"
[ -x "$FFPROBE" ] || FFPROBE=ffprobe
OUT="$OUTPUT_DIR/edit_list_bframes.mp4"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

"$FFMPEG" -y -v error \
-f lavfi -i "testsrc2=size=320x240:rate=30000/1001" -t 12 \
-c:v libx264 -preset veryfast -crf 20 -bf 3 -g 48 -pix_fmt yuv420p \
"$TMP/src.mp4"
"$FFMPEG" -y -v error -ss 2.3 -t 4.1 -i "$TMP/src.mp4" -c copy "$OUT"

packets="$("$FFPROBE" -v error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=p=0 "$OUT")"
head_discards="$(echo "$packets" | head -64 | grep -c D || true)"
kept="$(echo "$packets" | grep -vc D)"
stored="$("$FFPROBE" -v error -select_streams v:0 -show_entries stream=nb_frames -of csv=p=0 "$OUT")"
last="$(echo "$packets" | grep -v D | cut -d, -f1 | sort -g | tail -1)"
slots="$(python3 -c "print(round($last * 30000 / 1001) + 1)")"

if [ "$head_discards" -eq 0 ]; then
echo "expected discarded packets at the head (an edit list's pre-roll)" >&2
exit 1
fi
if [ "$slots" -le "$kept" ]; then
echo "expected holes in the kept pictures' timestamps ($kept kept, $slots slots)" >&2
exit 1
fi
echo "Wrote $OUT ($stored stored, $head_discards discarded at the head, $kept kept, $slots slots)"
33 changes: 33 additions & 0 deletions app/test/integration_source_grid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
// stored frame decoded once (`-fps_mode passthrough`) and placed on the grid
// by its ffprobe timestamp. Never one pipeline path against another.
// See docs/reports/issue-174-source-grid.
//
// Task #54 added the MP4s whose edit list discards pre-roll: their stored
// count (`nb_frames`) counts packets no decode outputs, so the app's
// timeline, and a job sent without a count, asked for frames that do not
// exist (small_clip.mp4: 3154 for 3151). See
// docs/reports/task-54-edit-list-count.
@Tags(['heavy'])
library;

Expand Down Expand Up @@ -58,6 +64,13 @@ const _fixtures = [
'DVD-shaped soft telecine, MPEG-PS at 29.97: 3:2 repeats', [3, 10, 43]),
_Fixture('soft_telecine_dvd.mkv',
'the same stream with MakeMKV-like 23.976 timestamps', [3, 10, 43]),
_Fixture('small_clip.mp4',
'edit list discarding 3 pre-roll packets: 3154 stored, 3151 slots',
[1, 1575, 3148, 3150]),
_Fixture('edit_list_bframes.mp4',
'edit list pre-roll and holes before the end: 146 stored, 125 kept, '
'128 slots',
[1, 60, 125, 127]),
];

/// The reference: the source as the app numbers it.
Expand Down Expand Up @@ -271,6 +284,26 @@ void main() {
});
}

// A job without a count (the CLI, a hand-written job) takes the source
// model's, as the app's does (task #54). `-count_frames`, which such a job
// used before, counted 75 for interlaced_test.avi's 79 slots and 125 for
// edit_list_bframes.mp4's 128.
group('a job sent without a count encodes every slot', () {
for (final file in ['interlaced_test.avi', 'edit_list_bframes.mp4']) {
test(file, () async {
final video = _resource(file);
final ref = await _reference(video);
final stem = p.basenameWithoutExtension(file);
final r = await WorkerHarness.runJob(
_job(video, ref, 'nocount_$stem').toJson(),
label: 'grid_no_count');
expect(r.success, isTrue, reason: r.error);
expect(await _lumaHashes(r.outputPath!, []),
ref.slotHashes(0, ref.slots.length - 1));
});
}
});

group('the Soft Telecine method keeps every film frame', () {
for (final (file, fps) in [
('soft_telecine_test.mkv', '23.976 (already film: nothing removed)'),
Expand Down
10 changes: 5 additions & 5 deletions app/test/integration_v210_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ void main() {
countFrames: true);
expect(s!['codec_name'], 'v210');
expect(s['pix_fmt'], 'yuv422p10le');
// The AVI index says 79, but four of its entries are empty: what decodes
// is the count to match.
final source = await WorkerHarness.firstStream(src,
selector: 'v:0', entries: ['nb_read_frames'], countFrames: true);
// The AVI index says 79, four of its entries empty (null frames): each
// is a slot repeating the frame before (issue #174), so the job, which
// sends no count, encodes the source model's 79 (task #54), not the 75
// pictures `-count_frames` decodes.
expect(int.parse(s['nb_read_frames'].toString()),
int.parse(source!['nb_read_frames'].toString()));
(await WorkerHarness.sourceModel(src)).frameCount);

final pv = await WorkerHarness.runPreview(job, frame: 10, label: 'v210 preview');
expect(pv.png, isNotNull, reason: '${pv.error}\n${pv.logs}');
Expand Down
34 changes: 31 additions & 3 deletions app/test/integration_video_start_offset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ Future<void> _expectFrame(String what, Uint8List img, String video, int n,
expect(diffs[n]!, lessThan(maxDiff), reason: '$what is not source frame $n ($table)');
}

/// Luma MD5s of the last [n] pictures [path] decodes.
Future<List<String>> _lastLumaHashes(String path, int n) async {
final r = await Process.run(
WorkerHarness.ffmpegPath,
['-v', 'error', '-sseof', '-2', '-i', path, '-map', '0:v:0',
'-fps_mode', 'passthrough', '-pix_fmt', 'gray', '-f', 'framemd5', '-'],
environment: WorkerHarness.ffmpegEnv,
);
expect(r.exitCode, 0, reason: 'framemd5 of $path: ${r.stderr}');
final hashes = [
for (final line in (r.stdout as String).split('\n'))
if (line.isNotEmpty && !line.startsWith('#')) line.split(',').last.trim(),
];
expect(hashes.length, greaterThanOrEqualTo(n));
return hashes.sublist(hashes.length - n);
}

Future<String> _encode(VideoJob job, String label) async {
final r = await WorkerHarness.runJob(job.toJson(), label: label);
expect(r.success, isTrue, reason: r.error);
Expand Down Expand Up @@ -338,7 +355,11 @@ void main() {
});
}

test('the DVB-T capture to AVI: its 126 pictures at 25/1, the first at 0 (task #30)', () async {
// The job carries no count, so it takes the source model's: 128, the grid's
// slots (task #54). Two slots repeat the picture before them, where the
// field-coded stream leaves a gap; `-count_frames`, which such a job used
// before, counted the 126 decoded pictures. An app job has always sent 128.
test('the DVB-T capture to AVI: its 128 slots at 25/1, the first at 0 (task #30)', () async {
final video = _resource('pal-dvbt-fieldcoded-25i.ts');
final job = VideoJob(
id: const Uuid().v4(),
Expand All @@ -355,7 +376,7 @@ void main() {
);
final v = await aviVideo(await _encode(job, 'vstart_dvbt_avi'));
expect(v.rate, '25/1');
expect(v.stored, 126, reason: 'every picture once, no null frames');
expect(v.stored, 128, reason: 'every slot once, no null frames');
expect(v.pts.take(2).toList(), [0.0, 0.04]);
});

Expand Down Expand Up @@ -579,9 +600,16 @@ void main() {
),
inputFrameRate: 2997 / 100,
);
final v = await aviVideo(await _encode(job, 'vstart_small_clip_copy'));
final out = await _encode(job, 'vstart_small_clip_copy');
final v = await aviVideo(out);
expect(v.stored, 3151, reason: 'stored frames, null frames included');
expect(v.pts.first, 0.0);
// The count is the model's (task #54), which no longer counts the edit
// list's 3 discarded packets: the output ends on the source's last
// picture, not on 3 copies of it.
expect(await _lastLumaHashes(out, 4),
await _lastLumaHashes(_resource('small_clip.mp4'), 4),
reason: 'the last pictures are the source\'s last pictures');
});
});
}
31 changes: 31 additions & 0 deletions docs/ENGINEERING_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4766,10 +4766,41 @@ Full evidence:
`-count_frames`, used for a job without a count, is wrong on AVI null
frames, the DVB-T clip's leading pictures and soft-telecine repeats, but
right on that MP4, so it is kept for such jobs until the follow-up.
*Fixed by task #54 (below): the model counts the edit list right, and
`-count_frames` is gone except for a raw reset TS/PS.*
- **Time.** An app preview no longer probes the source: 157–214 ms against
263–385 ms before on the fixtures (and no whole-file packet read per
scrub on a VFR source).
- Evidence: [docs/reports/task-50-source-model/](reports/task-50-source-model/index.html).

### An MP4 edit list's discarded packets are not frames (task #54, 2026-11-17)

- **Fault.** `nb_frames` of an MP4/MOV is the sample table's count, and an
edit list's pre-roll (kept for decoding, marked discard, never output) is
in it: `small_clip.mp4` stores 3154 and decodes 3151, so the app's
timeline ran 3 frames long and an untrimmed encode ended on 3 copies of
the last picture (`pipe_source` repeats it past EOF).
- **"Packets not discarded" is wrong too.** A stream-copy cut
(`edit_list_bframes.mp4`, `generate_edit_list_test.sh`) stores 146,
discards 21 and keeps 125 (so does `-count_frames`), but the copy's `-t`
dropped the last B pictures and the grid decode fills those holes: 128.
The count is the last kept picture's grid slot + 1, Matroska's rule.
- **Cheap.** Discards are seen by the head probe (64 packets, now with
`flags`) or an MP4/MOV's tail probe (last 3 s, run in parallel with the
others). On a 2 h MP4 (480 MB): tail 0.05 s, the whole `--probe-source`
0.13 s, a whole-file packet read 0.65 s warm (minutes on a NAS), and
`-count_frames` 20.7 s; all four counts agree (220 777). Every other
committed fixture's count is unchanged; the model now equals the grid
decode on every grid fixture but the raw reset files.
- **Jobs without a count take the model's** (`apply_source_model`); only no
model, or a raw reset TS/PS outside the app (`counts_by_decoding`), still
decodes to count. A CLI job on the DVB-T clip now encodes its 128 slots
(126 pictures, two repeated at field-coding gaps), as an app job always
did.
- **Not covered**: discards only mid-file (a multi-entry edit list) keep
`nb_frames`; nothing cheap detects them.
- Evidence: [docs/reports/task-54-edit-list-count/](reports/task-54-edit-list-count/index.html).

### Plugin format limits probed from the bundle; one `run_within` (task #48, 2026-11-16)

Task #42's plan, R2b. Report: [docs/reports/task-48-plugin-format-contracts](reports/task-48-plugin-format-contracts/index.html).
Expand Down
1 change: 1 addition & 0 deletions docs/reports/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ to it.
|---|---|---|
| 2026-11-16 | [macOS x64 FFmpeg with SVT-AV1](task-45-macos-x64-ffmpeg-svtav1/index.html) | Shipped in deps 1.17.0-rc2: evermeet's x64 FFmpeg 9.0.1 has no `libsvtav1`, so AV1 was unavailable on Intel Macs. martin-riedl.de's static amd64 9.0.2 (same build run as arm64) has SVT-AV1 v4.2.0, minos 12.0, system frameworks only; pinned by build ID + SHA-256. Native encoders byte-identical; x264/x265/MP3 differ only in their version strings (decoded output identical on three fixtures); all 30 fixtures decode identically; tone-map and burn-in identical. Lost only components nothing uses. Measured under Rosetta; pre-AVX Macs rely on run-time SIMD dispatch, as before |
| 2026-11-16 | [One source model, owned by the worker](task-50-source-model/index.html) | Built: `--probe-source` and a `SourceModel` every job carries; the app's Dart probe twins deleted. Decode plans and preview PNGs identical before/after on 30 fixtures × 5 frames, and the model equals the app's old values on all 30. App previews 89–182 ms faster (no per-scrub probe). Frame counts: the model matches the worker's own decode except an MP4 edit list (3154 vs 3151, pre-existing, follow-up); `-count_frames` is wrong on AVI null frames, an open GOP and soft-telecine repeats. One platform; large/NAS files not timed |
| 2026-11-17 | [Task #54: an MP4 edit list's discarded packets are not frames](task-54-edit-list-count/index.html) | Built: the model counts an edit-list MP4/MOV as its last kept picture's grid slot + 1 (small_clip 3154 → 3151; a cut with timestamp holes 146 → 128, where "packets not discarded" says 125); 0.05 s tail probe vs 20.7 s `-count_frames` on a 2 h file. Jobs without a count take the model's; `-count_frames` only for a raw reset TS/PS. One platform; NAS not timed |
| 2026-11-16 | [Plugin format limits, measured, and one helper for running a plugin outside them](task-48-plugin-format-contracts/index.html) | Built: all 69 pass-level plugin calls probed at every reachable format (3 subsamplings × 4 depths × 3 field flags) and sizes to 1080 lines, committed as a table that CI re-checks against the bundle. The five depth-limited plugins (DeScratch, DeDot, Bifrost, LUTDeCrawl, LUTDeRainbow) run through one `run_within()` that adds only the plugin's change; 4:1:1 is converted once at the top. Output identical before and after on every pass and format (FFV1), except RemoveDirt, which differs run to run on either side. Found: 720x486 4:2:0 refused by 11 calls, MCTemporalDenoise at width 2 mod 4 (filed). "Accepted" is not "read correctly"; macOS arm64 only |
| 2026-11-16 | [TCanny's per-thread buffers race](task-52-tcanny-race/index.html) | Fixed: TCanny r14 (every bundle) fills shared per-thread `unordered_map`s with no lock, and the `std::out_of_range` that follows aborts vspipe (MCDegrainSharp, nightly macOS x64, 1 failure in ~25 nightlies). The templates used only its Gaussian (`mode=-1`): `gauss_blur.gaussian_blur` computes it with `std.Convolution`, padded TCanny's way (Convolution repeats the edge sample, which first cost 7–15 levels at the borders), within 1 level at 8/10/16-bit, faster on arm64. Absence of the abort can't be proven; x86 speed not measured |
| 2026-11-16 | [IVTC and Soft Telecine previewed a frame before the target](issue-46-ivtc-preview/index.html) | Fixed: the preview ran VFM without VDecimate while the worker picked its frame as if decimated, so it showed source frame `target - ceil(radius/5)`'s film frame (1 early at radius 5, 3 at 12; 22 of 135 previews right), and IVTC then 59.94 converted the undecimated 29.97. The preview now runs VDecimate on a clip arranged as the encode's (in-point skip, phase padding, the encode's end) and reads which frame of the target's cycle was dropped: 147 of 147 previews are the encode's frame, MAD 0.00 on 8-bit. VDecimate's rules probed on the bundle. Synthetic 3:2 fixtures, Apple Silicon only |
Expand Down
Loading