nanocodec-cli 0.1.0

nanocodec command-line encoder and decoder (the `nanocodec` binary)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! The command line's commands and options, grouped: the one list that
//! `nanocodec help` prints and that the project site's command-line page is
//! generated from (`web/docs/data/cli.json`, written by the
//! `cli_reference_is_current` test with `NC_BLESS=1`). A test checks that
//! every option the parsers read is listed here and every listed one is read,
//! so the help and the page cannot drift from the code.

use std::fmt::Write as _;

/// One option of a command.
pub struct OptionDoc {
    /// The flag, e.g. `--bitrate`.
    pub flag: &'static str,
    /// Its value's placeholder (empty for a switch).
    pub value: &'static str,
    /// What it does, one or two sentences.
    pub help: &'static str,
}

/// A titled group of a command's options.
pub struct GroupDoc {
    /// The group's heading, e.g. "Rate control (CBR) and buffer".
    pub title: &'static str,
    /// Its options, in the order they are listed.
    pub options: &'static [OptionDoc],
}

/// A subcommand: its name, a one-line description and its option groups.
pub struct CommandDoc {
    /// The subcommand, e.g. `encode`.
    pub name: &'static str,
    /// What it does, one line.
    pub about: &'static str,
    /// Its options, grouped.
    pub groups: &'static [GroupDoc],
}

const fn opt(flag: &'static str, value: &'static str, help: &'static str) -> OptionDoc {
    OptionDoc { flag, value, help }
}

const CODECS: &str = "av1|h264|hevc|vvc|vp9|av2";

const ENCODE_GROUPS: &[GroupDoc] = &[
    GroupDoc {
        title: "Input",
        options: &[
            opt(
                "--input",
                "PATH",
                "Raw 8-bit 4:2:0 frames, back to back; `-` reads stdin.",
            ),
            opt(
                "--input-format",
                "i420|yv12|nv12",
                "Plane layout of the input (default i420).",
            ),
            opt("--width", "W", "Frame width in pixels (even)."),
            opt("--height", "H", "Frame height in pixels (even)."),
            opt(
                "--fps",
                "F",
                "Nominal frame rate: `30`, `29.97` or `30000/1001`.",
            ),
            opt("--frames", "N", "Number of input frames to code."),
            opt(
                "--timestamps",
                "FILE",
                "Capture time of each frame (mkvmerge timestamp format v2); rate control budgets \
                 the real frame intervals.",
            ),
            opt(
                "--resize",
                "FRAME:WxH",
                "The input's frames have this size from FRAME on (a key frame with new headers); \
                 repeatable.",
            ),
        ],
    },
    GroupDoc {
        title: "Output",
        options: &[
            opt(
                "--output",
                "PATH",
                "The stream: IVF for AV1, VP9 and AV2, Annex-B for H.264, HEVC and VVC; `-` \
                 writes stdout.",
            ),
            opt(
                "--recon",
                "PATH",
                "The reconstruction of every coded frame (I420).",
            ),
            opt(
                "--display-recon",
                "PATH",
                "One picture per input frame, as a decoder shows it: a frame left uncoded repeats \
                 the last one.",
            ),
            opt("--timestamps-out", "FILE", "The time of every coded frame."),
        ],
    },
    GroupDoc {
        title: "Codec and preset",
        options: &[
            opt(
                "--codec",
                CODECS,
                "Output format (`h266` is accepted for vvc). Required.",
            ),
            opt(
                "--preset",
                "veryfast|faster|fast|medium|slow",
                "A rung of the speed ladder; medium is the codec's Default and is used without \
                 --preset. Not every codec has every rung (`nanocodec params --codec C`); with \
                 --scalability L2*/L3* a rung takes the codec's file for that many layers.",
            ),
            opt(
                "--block",
                "8|16|32|64",
                "Fixed leaf block size where the codec's planner uses one (default 16; AV2 8, \
                 VVC 32).",
            ),
        ],
    },
    GroupDoc {
        title: "Fixed QP",
        options: &[
            opt(
                "--qp",
                "Q",
                "Fixed quantiser in the codec's scale (H.264, HEVC: QP 0-51; VVC: QP 0-63; AV1, \
                 VP9, AV2: base_q_idx); with --bitrate, the starting quantiser only. One of --qp \
                 and --bitrate is required.",
            ),
            opt(
                "--key-qp-offset",
                "D",
                "Code fixed-QP key frames D quantiser steps finer than --qp (default: the codec \
                 chooses).",
            ),
        ],
    },
    GroupDoc {
        title: "Rate control (CBR) and buffer",
        options: &[
            opt(
                "--bitrate",
                "KBPS",
                "Real-time rate control at this target bitrate.",
            ),
            opt(
                "--peak-bitrate",
                "KBPS",
                "Peak drain rate (default twice the target).",
            ),
            opt(
                "--buffer-ms",
                "MS",
                "Virtual decoder buffer duration (default 1000 ms).",
            ),
            opt("--min-qp", "Q", "Lowest quantiser rate control may use."),
            opt("--max-qp", "Q", "Highest quantiser rate control may use."),
            opt(
                "--max-qp-step",
                "N",
                "Largest quantiser change between inter frames (default 4 QP, 20 AV1 indices).",
            ),
            opt(
                "--frame-drop",
                "",
                "Skip an inter frame that would overflow the buffer even at the highest \
                 quantiser.",
            ),
            opt(
                "--rate-change",
                "FRAME:KBPS[:PEAK_KBPS[:BUFFER_MS]]",
                "Change the target (and peak, buffer) before frame FRAME, as a transport's \
                 bandwidth estimate would; repeatable.",
            ),
        ],
    },
    GroupDoc {
        title: "Key frames and resilience",
        options: &[
            opt(
                "--intra-period",
                "N",
                "A key frame every N frames (default: every 10 seconds of video).",
            ),
            opt(
                "--keyframe",
                "FRAME",
                "Code input frame FRAME as a key frame, as a receiver's request would; \
                 repeatable.",
            ),
            opt(
                "--intra-refresh",
                "FRAMES",
                "Intra refresh sweep over this many frames in place of periodic key frames \
                 (`refresh.period`).",
            ),
            opt(
                "--ltr-interval",
                "FRAMES",
                "Mark a long-term reference every N frames, for loss recovery (`ltr.interval`).",
            ),
            opt(
                "--temporal-layers",
                "1|2|3",
                "Hierarchical-P temporal layers; the top layer is droppable. Not with \
                 --scalability.",
            ),
            opt(
                "--scalability",
                "LxTy[_KEY]",
                "Scalability mode L1T1..L3T3: spatial layers (L2, L3: 2:1 steps) and temporal \
                 layers in one stream; `_KEY` codes K-SVC.",
            ),
        ],
    },
    GroupDoc {
        title: "Content",
        options: &[
            opt(
                "--still-camera",
                "",
                "Noise handling for a fixed camera over the preset: lowers the bitrate on still \
                 noisy scenes; off in every preset.",
            ),
            opt(
                "--film-grain",
                "[STRENGTH]",
                "AV1 only: film grain synthesis, STRENGTH relative to the measured source noise.",
            ),
        ],
    },
    GroupDoc {
        title: "Signalling",
        options: &[
            opt(
                "--colour",
                "bt709|bt601|bt601-625|P,T,M",
                "Colour description to signal (ISO/IEC 23091-2 code points for P,T,M).",
            ),
            opt("--range", "full|limited", "Sample range to signal."),
            opt(
                "--timing",
                "nominal|constant|variable",
                "Header timing information (default nominal; variable with --timestamps).",
            ),
        ],
    },
    GroupDoc {
        title: "Parameters",
        options: &[
            opt(
                "--params",
                "FILE",
                "A parameter file (one `name = value` per line, `#` comments), over the preset.",
            ),
            opt(
                "--param",
                "NAME=VALUE",
                "One parameter, over the file; repeatable. `nanocodec params` lists them.",
            ),
        ],
    },
    GroupDoc {
        title: "Diagnostics",
        options: &[
            opt(
                "--rate-log",
                "FILE",
                "With --bitrate: one CSV row per frame of rate control's state.",
            ),
            opt(
                "--sse-log",
                "FILE",
                "Per-plane squared error of every shown picture against --sse-reference (CSV).",
            ),
            opt(
                "--sse-reference",
                "PATH",
                "The reference frames for --sse-log.",
            ),
            opt(
                "--layer-md5",
                "FILE",
                "CSV of each layer picture's size, bytes and MD5.",
            ),
            opt(
                "--spatial-stats",
                "FILE",
                "JSON summary of per-layer bytes, rates and references.",
            ),
            opt(
                "--qp-replay",
                "FILE",
                "With --bitrate: impose these quantisers on rate control's plan, by frame.",
            ),
            opt(
                "--simulate-loss",
                "FRAME[:LAYER]",
                "Report frame FRAME (or spatial layer LAYER of it) lost; every other frame is \
                 acknowledged.",
            ),
        ],
    },
];

/// Every command, in the order `nanocodec help` prints them.
pub const COMMANDS: &[CommandDoc] = &[
    CommandDoc {
        name: "encode",
        about: "Encode raw video to a stream.",
        groups: ENCODE_GROUPS,
    },
    CommandDoc {
        name: "decode",
        about: "Decode one of our streams with our own subset decoder.",
        groups: &[GroupDoc {
            title: "Options",
            options: &[
                opt("--codec", CODECS, "Stream format. Required."),
                opt(
                    "--input",
                    "PATH",
                    "The stream (IVF or Annex-B); `-` reads stdin.",
                ),
                opt(
                    "--output",
                    "PATH",
                    "Decoded frames as raw I420; `-` writes stdout.",
                ),
                opt("--frame-md5", "FILE", "One MD5 line per shown frame."),
                opt(
                    "--discard",
                    "0|1",
                    "1: decode every frame and write nothing (for timing).",
                ),
                opt(
                    "--film-grain",
                    "0|1",
                    "AV1: 0 shows frames without film grain.",
                ),
                opt(
                    "--operating-point",
                    "N",
                    "AV1 or VP9 operating point to decode.",
                ),
                opt(
                    "--all-layers",
                    "0|1",
                    "AV1 or VP9: 1 writes every spatial layer's picture.",
                ),
            ],
        }],
    },
    CommandDoc {
        name: "extract",
        about: "Keep a temporal or spatial sub-stream of a stream, without re-encoding.",
        groups: &[GroupDoc {
            title: "Options",
            options: &[
                opt("--codec", CODECS, "Stream format. Required."),
                opt("--input", "PATH", "The stream."),
                opt("--output", "PATH", "The kept sub-stream."),
                opt(
                    "--temporal-layers",
                    "N",
                    "Temporal layers the stream was coded with.",
                ),
                opt(
                    "--max-layer",
                    "M",
                    "Keep temporal layers 0..M (below --temporal-layers).",
                ),
                opt(
                    "--list",
                    "",
                    "List each access unit's temporal layer instead of extracting.",
                ),
                opt(
                    "--base-only",
                    "",
                    "AV1 or VP9: keep the spatial base layer.",
                ),
                opt(
                    "--operating-point",
                    "N",
                    "AV1 or VP9: keep this operating point.",
                ),
                opt(
                    "--layer-md5",
                    "FILE",
                    "VP9: the encoder's layer sidecar to split by.",
                ),
            ],
        }],
    },
    CommandDoc {
        name: "session",
        about: "Encoder and receiver stepped by commands on stdin (encode options, no --output).",
        groups: &[GroupDoc {
            title: "Options",
            options: &[
                opt(
                    "--display",
                    "PATH",
                    "One picture per input frame: what the viewer saw.",
                ),
                opt(
                    "--receiver",
                    "transport|bitstream",
                    "Decode only frames whose listed references arrived (transport, the default), \
                     or every arrived frame.",
                ),
                opt(
                    "--operating-point",
                    "N",
                    "AV1 operating point the receiver decodes.",
                ),
            ],
        }],
    },
    CommandDoc {
        name: "params",
        about: "List every tunable parameter with its default and range, and the presets.",
        groups: &[GroupDoc {
            title: "Options",
            options: &[opt(
                "--codec",
                CODECS,
                "Show this codec's defaults and presets.",
            )],
        }],
    },
    CommandDoc {
        name: "stat",
        about: "Coding-tool statistics of a stream (analysis builds: --features analysis).",
        groups: &[GroupDoc {
            title: "Options",
            options: &[
                opt("--codec", CODECS, "Stream format."),
                opt("--input", "PATH", "The stream."),
                opt("--json", "FILE", "Write the statistics as JSON."),
            ],
        }],
    },
    CommandDoc {
        name: "help",
        about: "Print this help.",
        groups: &[],
    },
];

/// `nanocodec help`: every command with its grouped options.
pub fn text() -> String {
    let mut out = String::from(
        "usage: nanocodec COMMAND [OPTIONS]   (nanocodec --version: version and commit;\n       --help: this help)\n",
    );
    for command in COMMANDS {
        let _ = write!(out, "\nnanocodec {}: {}\n", command.name, command.about);
        for group in command.groups {
            let _ = writeln!(out, "  {}", group.title);
            for option in group.options {
                let usage = format!("{} {}", option.flag, option.value);
                let _ = writeln!(out, "    {:<40} {}", usage.trim_end(), option.help);
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeSet;
    use std::path::Path;

    /// The parsers' sources, whose flag literals must all be documented.
    const PARSER_SOURCES: &[&str] = &[
        include_str!("main.rs"),
        include_str!("rate_control.rs"),
        include_str!("params.rs"),
        include_str!("colour.rs"),
        include_str!("decode.rs"),
        include_str!("extract.rs"),
        include_str!("session.rs"),
        include_str!("stat.rs"),
        include_str!("stdio.rs"),
    ];
    /// The flags `main` reads before any command (the usage line names them).
    const TOP_LEVEL_FLAGS: &[&str] = &["--help", "--version"];
    /// Flag literals of the parsers' own unit tests, not options.
    const TEST_ONLY_FLAGS: &[&str] = &["--unknown"];
    /// The checked-in site data this list generates.
    const SITE_JSON: &str = "../../web/docs/data/cli.json";
    /// The codecs whose presets the site lists, in its order.
    const SITE_CODECS: &[&str] = &["h264", "hevc", "vvc", "av1", "vp9", "av2"];

    /// Every `"--name"` literal in `source`, outside its unit tests.
    fn flag_literals(source: &str) -> BTreeSet<String> {
        let code = source
            .split("#[cfg(test)]\nmod tests")
            .next()
            .unwrap_or(source);
        code.split("\"--")
            .skip(1)
            .filter_map(|rest| {
                let name: String = rest
                    .chars()
                    .take_while(|c| c.is_ascii_alphanumeric() || *c == '-')
                    .collect();
                (!name.is_empty() && rest[name.len()..].starts_with('"'))
                    .then(|| format!("--{name}"))
            })
            .collect()
    }

    fn documented() -> BTreeSet<&'static str> {
        COMMANDS
            .iter()
            .flat_map(|c| c.groups)
            .flat_map(|g| g.options)
            .map(|o| o.flag)
            .chain(TOP_LEVEL_FLAGS.iter().copied())
            .collect()
    }

    #[test]
    fn every_parsed_flag_is_documented_and_every_documented_flag_parsed() {
        let parsed: BTreeSet<String> = PARSER_SOURCES
            .iter()
            .flat_map(|s| flag_literals(s))
            .filter(|f| !TEST_ONLY_FLAGS.contains(&f.as_str()))
            .collect();
        let documented = documented();
        let missing: Vec<_> = parsed
            .iter()
            .filter(|f| !documented.contains(f.as_str()))
            .collect();
        assert!(
            missing.is_empty(),
            "flags read but not in help.rs: {missing:?}"
        );
        let unread: Vec<_> = documented
            .iter()
            .filter(|f| !parsed.contains(**f))
            .collect();
        assert!(
            unread.is_empty(),
            "flags in help.rs no parser reads: {unread:?}"
        );
    }

    fn json_string(text: &str) -> String {
        let mut out = String::from("\"");
        for c in text.chars() {
            match c {
                '"' => out.push_str("\\\""),
                '\\' => out.push_str("\\\\"),
                '\n' => out.push_str("\\n"),
                c => out.push(c),
            }
        }
        out.push('"');
        out
    }

    /// The presets `nanocodec params --codec C` reports.
    fn presets(codec: &str) -> Vec<String> {
        let listing = crate::params::listing(Some(codec)).unwrap();
        let line = listing
            .lines()
            .find_map(|l| l.strip_prefix("# presets: "))
            .expect("the listing names its presets");
        let names = line.split(" (").next().unwrap_or(line);
        names.split(", ").map(str::to_owned).collect()
    }

    /// The site's command-line reference: every command's groups and options,
    /// and each codec's presets.
    fn site_json() -> String {
        let mut out = String::from("{\n \"commands\": [\n");
        for (ci, command) in COMMANDS.iter().enumerate() {
            let _ = write!(
                out,
                "  {{\"name\": {}, \"about\": {}, \"groups\": [",
                json_string(command.name),
                json_string(command.about)
            );
            for (gi, group) in command.groups.iter().enumerate() {
                let _ = write!(
                    out,
                    "\n   {{\"title\": {}, \"options\": [",
                    json_string(group.title)
                );
                for (oi, option) in group.options.iter().enumerate() {
                    let _ = write!(
                        out,
                        "\n    {{\"flag\": {}, \"value\": {}, \"help\": {}}}{}",
                        json_string(option.flag),
                        json_string(option.value),
                        json_string(option.help),
                        if oi + 1 < group.options.len() {
                            ","
                        } else {
                            ""
                        }
                    );
                }
                let _ = write!(
                    out,
                    "]}}{}",
                    if gi + 1 < command.groups.len() {
                        ","
                    } else {
                        ""
                    }
                );
            }
            let _ = writeln!(out, "]}}{}", if ci + 1 < COMMANDS.len() { "," } else { "" });
        }
        out.push_str(" ],\n \"presets\": {");
        for (i, codec) in SITE_CODECS.iter().enumerate() {
            let names: Vec<String> = presets(codec).iter().map(|n| json_string(n)).collect();
            let _ = write!(
                out,
                "\n  {}: [{}]{}",
                json_string(codec),
                names.join(", "),
                if i + 1 < SITE_CODECS.len() { "," } else { "" }
            );
        }
        out.push_str("\n }\n}\n");
        out
    }

    /// `web/docs/data/cli.json` is this list; `NC_BLESS=1` rewrites it.
    #[test]
    fn cli_reference_is_current() {
        let path = Path::new(env!("CARGO_MANIFEST_DIR")).join(SITE_JSON);
        let expected = site_json();
        if std::env::var_os("NC_BLESS").is_some() {
            std::fs::write(&path, &expected).unwrap();
        }
        let actual = std::fs::read_to_string(&path).unwrap_or_default();
        assert!(
            actual == expected,
            "{} is out of date: NC_BLESS=1 cargo test -p nanocodec-cli --bin nanocodec help::",
            path.display()
        );
    }
}