libpgs 0.6.0

Fast PGS subtitle extraction, encoding, and round-trip transformation for MKV and M2TS containers
Documentation
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
use std::process;
use std::time::Instant;

fn main() {
    let args: Vec<String> = std::env::args().collect();

    if args.len() < 2 {
        print_usage();
        process::exit(1);
    }

    let result = match args[1].as_str() {
        "tracks" => cmd_tracks(&args[2..]),
        "extract" => cmd_extract(&args[2..]),
        "stream" => cmd_stream(&args[2..]),
        "encode" => cmd_encode(&args[2..]),
        "bench" => cmd_bench(&args[2..]),
        "help" | "--help" | "-h" => {
            print_usage();
            Ok(())
        }
        other => {
            eprintln!("Unknown command: {other}");
            print_usage();
            process::exit(1);
        }
    };

    if let Err(e) = result {
        eprintln!("Error: {e}");
        process::exit(1);
    }
}

fn print_usage() {
    eprintln!("libpgs - Fast PGS subtitle extraction and encoding\n");
    eprintln!("Usage:");
    eprintln!("  libpgs tracks <file>                      List PGS tracks");
    eprintln!("  libpgs extract <file> -o <output.sup>     Extract all PGS tracks");
    eprintln!("  libpgs extract <file> -t <id> -o <out>    Extract specific track");
    eprintln!("  libpgs stream <file> [-t <id>] [--raw-payloads]  Stream PGS data as NDJSON");
    eprintln!("  libpgs encode -o <output.sup>             Encode NDJSON from stdin to .sup");
    eprintln!("  libpgs bench <file>                       Benchmark I/O efficiency");
    eprintln!("  libpgs help                               Show this help");
    eprintln!();
    eprintln!("Time range options (extract and stream commands):");
    eprintln!("  --start TIME    Start extracting from this time (HH:MM:SS, MM:SS, or seconds)");
    eprintln!("  --end TIME      Stop extracting after this time");
    eprintln!();
    eprintln!("When extracting all tracks, output files are named <stem>_track<id>.sup");
}

fn cmd_tracks(args: &[String]) -> Result<(), libpgs::error::PgsError> {
    if args.is_empty() {
        eprintln!("Usage: libpgs tracks <file>");
        process::exit(1);
    }

    let path = PathBuf::from(&args[0]);
    let tracks = libpgs::list_pgs_tracks(&path)?;

    if tracks.is_empty() {
        println!("No PGS tracks found.");
        return Ok(());
    }

    println!("PGS tracks found:");
    for track in &tracks {
        let lang = track.language.as_deref().unwrap_or("unknown");
        let mut extras = Vec::new();
        if let Some(name) = &track.name {
            extras.push(format!("name=\"{name}\""));
        }
        if let Some(true) = track.flag_default {
            extras.push("default".to_string());
        }
        if let Some(true) = track.flag_forced {
            extras.push("forced".to_string());
        }
        if let Some(count) = track.display_set_count {
            extras.push(format!("display_sets={count}"));
        }
        if let Some(cues) = track.has_cues {
            extras.push(format!("has_cues={cues}"));
        }
        let extra_str = if extras.is_empty() {
            String::new()
        } else {
            format!(", {}", extras.join(", "))
        };
        println!(
            "  Track {}: language={}, format={:?}{}",
            track.track_id, lang, track.container, extra_str
        );
    }

    Ok(())
}

fn cmd_bench(args: &[String]) -> Result<(), libpgs::error::PgsError> {
    if args.is_empty() {
        eprintln!("Usage: libpgs bench <file> [--strategy auto|sequential]");
        process::exit(1);
    }

    let path = PathBuf::from(&args[0]);

    let mut strategy = libpgs::MkvStrategy::Auto;
    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "--strategy" | "-s" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for --strategy");
                    process::exit(1);
                }
                strategy = match args[i].as_str() {
                    "auto" => libpgs::MkvStrategy::Auto,
                    "sequential" => libpgs::MkvStrategy::Sequential,
                    other => {
                        eprintln!("Unknown strategy: {other} (use auto, sequential)");
                        process::exit(1);
                    }
                };
            }
            other => {
                eprintln!("Unknown option: {other}");
                process::exit(1);
            }
        }
        i += 1;
    }

    let strategy_name = match strategy {
        libpgs::MkvStrategy::Auto => "auto",
        libpgs::MkvStrategy::Sequential => "sequential",
    };

    let start = Instant::now();

    let mut extractor = libpgs::Extractor::open(&path)?;
    if strategy != libpgs::MkvStrategy::Auto {
        extractor = extractor.with_mkv_strategy(strategy);
    }

    let track_info: Vec<_> = extractor.tracks().to_vec();
    let results: Vec<_> = extractor.by_ref().collect::<Result<Vec<_>, _>>()?;
    let stats = extractor.stats().clone();
    let elapsed = start.elapsed();

    // Group results by track for display.
    let mut track_ds_counts: std::collections::HashMap<u32, (usize, usize)> =
        std::collections::HashMap::new();
    for tds in &results {
        let entry = track_ds_counts.entry(tds.track_id).or_insert((0, 0));
        entry.0 += 1;
        entry.1 += tds.display_set.segments.len();
    }

    let total_display_sets = results.len();
    let total_segments: usize = results
        .iter()
        .map(|tds| tds.display_set.segments.len())
        .sum();
    let ratio = if stats.file_size > 0 {
        (stats.bytes_read as f64 / stats.file_size as f64) * 100.0
    } else {
        0.0
    };

    println!("File:          {}", path.display());
    println!("Strategy:      {}", strategy_name);
    println!(
        "File size:     {} ({:.2} MB)",
        stats.file_size,
        stats.file_size as f64 / (1024.0 * 1024.0)
    );
    println!(
        "Bytes read:    {} ({:.2} MB)",
        stats.bytes_read,
        stats.bytes_read as f64 / (1024.0 * 1024.0)
    );
    println!("Read ratio:    {:.2}%", ratio);
    println!("Tracks:        {}", track_info.len());
    for t in &track_info {
        let lang = t.language.as_deref().unwrap_or("unknown");
        if let Some(&(ds_count, seg_count)) = track_ds_counts.get(&t.track_id) {
            println!(
                "  Track {} ({}): {} display sets, {} segments",
                t.track_id, lang, ds_count, seg_count
            );
        }
    }
    println!(
        "Total:         {} display sets, {} segments",
        total_display_sets, total_segments
    );
    println!("Time:          {:.3}s", elapsed.as_secs_f64());

    Ok(())
}

/// Parse a comma-separated list of track IDs (e.g. "3", "3,5,8").
fn parse_track_ids(value: &str) -> Vec<u32> {
    value
        .split(',')
        .map(|s| {
            s.trim().parse().unwrap_or_else(|_| {
                eprintln!("Invalid track ID: {}", s.trim());
                process::exit(1);
            })
        })
        .collect()
}

/// Parse a timestamp string into milliseconds.
///
/// Accepts: `HH:MM:SS.mmm`, `MM:SS.mmm`, `SS.mmm`, or plain seconds (e.g. `123.456`).
fn parse_timestamp(s: &str) -> Option<f64> {
    let parts: Vec<&str> = s.split(':').collect();
    match parts.len() {
        1 => {
            // Plain seconds: "123.456"
            let secs: f64 = parts[0].parse().ok()?;
            Some(secs * 1000.0)
        }
        2 => {
            // MM:SS or MM:SS.mmm
            let mins: f64 = parts[0].parse().ok()?;
            let secs: f64 = parts[1].parse().ok()?;
            Some((mins * 60.0 + secs) * 1000.0)
        }
        3 => {
            // HH:MM:SS or HH:MM:SS.mmm
            let hours: f64 = parts[0].parse().ok()?;
            let mins: f64 = parts[1].parse().ok()?;
            let secs: f64 = parts[2].parse().ok()?;
            Some((hours * 3600.0 + mins * 60.0 + secs) * 1000.0)
        }
        _ => None,
    }
}

fn cmd_stream(args: &[String]) -> Result<(), libpgs::error::PgsError> {
    if args.is_empty() {
        eprintln!("Usage: libpgs stream <file> [-t <id>] [--start TIME] [--end TIME] [--raw-payloads] [--with-header]");
        process::exit(1);
    }

    let input = PathBuf::from(&args[0]);
    let mut track_ids: Vec<u32> = Vec::new();
    let mut raw_payloads = false;
    let mut with_header = false;
    let mut start_ms: Option<f64> = None;
    let mut end_ms: Option<f64> = None;

    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "-t" | "--track" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for -t");
                    process::exit(1);
                }
                track_ids.extend(parse_track_ids(&args[i]));
            }
            "--raw-payloads" => {
                raw_payloads = true;
            }
            "--with-header" => {
                with_header = true;
            }
            "--start" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for --start");
                    process::exit(1);
                }
                start_ms = Some(parse_timestamp(&args[i]).unwrap_or_else(|| {
                    eprintln!("Invalid timestamp for --start: {}", args[i]);
                    process::exit(1);
                }));
            }
            "--end" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for --end");
                    process::exit(1);
                }
                end_ms = Some(parse_timestamp(&args[i]).unwrap_or_else(|| {
                    eprintln!("Invalid timestamp for --end: {}", args[i]);
                    process::exit(1);
                }));
            }
            other => {
                eprintln!("Unknown option: {other}");
                process::exit(1);
            }
        }
        i += 1;
    }

    let mut extractor = libpgs::Extractor::open(&input)?;
    if !track_ids.is_empty() {
        extractor = extractor.with_track_filter(&track_ids);
    }
    if start_ms.is_some() || end_ms.is_some() {
        extractor = extractor.with_time_range(start_ms, end_ms);
    }
    extractor = extractor.with_history(false);

    let stdout = std::io::stdout();
    let mut out = stdout.lock();

    // Stream tracks header + display sets as NDJSON.
    // If the consumer closes the pipe (BrokenPipe), exit cleanly.
    match stream_ndjson(&mut out, &mut extractor, &input, raw_payloads, with_header) {
        Ok(()) => Ok(()),
        Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => Ok(()),
        Err(e) => Err(e.into()),
    }
}

/// Drain an NDJSON line to `out` in ~4KB chunks, then flush.
///
/// Chunking matters on Windows: a single large pipe write can block the
/// producer until the consumer drains the pipe, while many small writes
/// stay inside the pipe buffer. ~4KB matches common line-buffered reader
/// defaults (Python, Node, `wc`), keeping throughput high for piped
/// consumers without giving up the per-line-buffer benefit of coalescing
/// many small `write!` calls into one allocation.
fn flush_line<W: Write>(out: &mut W, line: &[u8]) -> std::io::Result<()> {
    const CHUNK: usize = 2048;
    for chunk in line.chunks(CHUNK) {
        out.write_all(chunk)?;
    }
    out.flush()
}

fn stream_ndjson(
    out: &mut impl Write,
    extractor: &mut libpgs::Extractor,
    input: &Path,
    raw_payloads: bool,
    with_header: bool,
) -> std::io::Result<()> {
    // Reused per-line scratch buffer: assemble the full NDJSON line here,
    // then issue a single write_all + flush so pipe consumers see one
    // atomic write per line (one syscall vs many).
    let mut line: Vec<u8> = Vec::with_capacity(16 * 1024);

    // For .sup inputs, pre-scan to emit a manifest header with total/content/clear
    // display-set counts so consumers can display a progress denominator immediately.
    if with_header && extractor.format() == libpgs::ContainerFormat::Sup {
        let counts = libpgs::sup::stream::count_display_sets(input)
            .map_err(std::io::Error::other)?;
        writeln!(
            line,
            "{{\"type\":\"header\",\"total_display_sets\":{},\"total_content_display_sets\":{},\"total_clear_display_sets\":{}}}",
            counts.total, counts.content, counts.clear,
        )?;
        flush_line(out, &line)?;
        line.clear();
    }

    // Emit tracks header as first line.
    let tracks = extractor.tracks();
    write!(line, "{{\"type\":\"tracks\",\"tracks\":[")?;
    for (ti, track) in tracks.iter().enumerate() {
        if ti > 0 {
            write!(line, ",")?;
        }
        write!(
            line,
            "{{\"track_id\":{},\"language\":{},\"container\":\"{}\",\
             \"name\":{},\"is_default\":{},\"is_forced\":{},\"display_set_count\":{},\
             \"indexed\":{}}}",
            track.track_id,
            json_string_or_null(track.language.as_deref()),
            container_name(track.container),
            json_string_or_null(track.name.as_deref()),
            json_bool_or_null(track.flag_default),
            json_bool_or_null(track.flag_forced),
            json_u64_or_null(track.display_set_count),
            json_bool_or_null(track.has_cues),
        )?;
    }
    writeln!(line, "]}}")?;
    flush_line(out, &line)?;
    line.clear();

    // Per-track index counter for display set sequence numbers.
    let mut track_indices: std::collections::HashMap<u32, u64> = std::collections::HashMap::new();

    // Stream display sets, one line per display set.
    for result in extractor {
        let tds = result.map_err(std::io::Error::other)?;
        let ds = &tds.display_set;
        let index = track_indices.entry(tds.track_id).or_insert(0);
        let current_index = *index;
        *index += 1;

        write!(
            line,
            "{{\"type\":\"display_set\",\"track_id\":{},\"index\":{},\
             \"pts\":{},\"pts_ms\":{:.4},",
            tds.track_id, current_index, ds.pts, ds.pts_ms,
        )?;

        // -- composition (from PCS) --
        write_composition(&mut line, &ds.segments, raw_payloads)?;
        write!(line, ",")?;

        // -- windows (from WDS) --
        write_windows(&mut line, &ds.segments, raw_payloads)?;
        write!(line, ",")?;

        // -- palettes (from PDS) --
        write_palettes(&mut line, &ds.segments, raw_payloads)?;
        write!(line, ",")?;

        // -- objects (from ODS) --
        write_objects(&mut line, &ds.segments, raw_payloads)?;

        writeln!(line, "}}")?;
        flush_line(out, &line)?;
        line.clear();
    }

    Ok(())
}

fn write_composition(
    out: &mut impl Write,
    segments: &[libpgs::pgs::PgsSegment],
    raw_payloads: bool,
) -> std::io::Result<()> {
    use libpgs::pgs::segment::SegmentType;

    let pcs_seg = segments
        .iter()
        .find(|s| s.segment_type == SegmentType::PresentationComposition);

    let Some(seg) = pcs_seg else {
        write!(out, "\"composition\":null")?;
        return Ok(());
    };

    let Some(pcs) = seg.parse_pcs() else {
        if raw_payloads {
            write_base64_field(out, b"\"composition\":{\"payload\":\"", &seg.payload)?;
            write!(out, "}}")?;
        } else {
            write!(out, "\"composition\":null")?;
        }
        return Ok(());
    };

    write!(
        out,
        "\"composition\":{{\"number\":{},\"state\":\"{}\",\
         \"video_width\":{},\"video_height\":{},\
         \"palette_only\":{},\"palette_id\":{},\"objects\":[",
        pcs.composition_number,
        composition_state_name(pcs.composition_state),
        pcs.video_width,
        pcs.video_height,
        pcs.palette_only,
        pcs.palette_id,
    )?;

    for (ci, obj) in pcs.objects.iter().enumerate() {
        if ci > 0 {
            write!(out, ",")?;
        }
        write!(
            out,
            "{{\"object_id\":{},\"window_id\":{},\"x\":{},\"y\":{},\"crop\":",
            obj.object_id, obj.window_id, obj.x, obj.y,
        )?;
        match &obj.crop {
            Some(crop) => write!(
                out,
                "{{\"x\":{},\"y\":{},\"width\":{},\"height\":{}}}",
                crop.x, crop.y, crop.width, crop.height,
            )?,
            None => write!(out, "null")?,
        }
        write!(out, "}}")?;
    }

    write!(out, "]")?;
    if raw_payloads {
        write_base64_field(out, b",\"payload\":\"", &seg.payload)?;
    }
    write!(out, "}}")?;

    Ok(())
}

fn write_windows(
    out: &mut impl Write,
    segments: &[libpgs::pgs::PgsSegment],
    raw_payloads: bool,
) -> std::io::Result<()> {
    use libpgs::pgs::segment::SegmentType;

    write!(out, "\"windows\":[")?;
    let mut first = true;
    for seg in segments
        .iter()
        .filter(|s| s.segment_type == SegmentType::WindowDefinition)
    {
        if let Some(wds) = seg.parse_wds() {
            for win in &wds.windows {
                if !first {
                    write!(out, ",")?;
                }
                first = false;
                write!(
                    out,
                    "{{\"id\":{},\"x\":{},\"y\":{},\"width\":{},\"height\":{}",
                    win.id, win.x, win.y, win.width, win.height,
                )?;
                if raw_payloads {
                    write_base64_field(out, b",\"payload\":\"", &seg.payload)?;
                }
                write!(out, "}}")?;
            }
        }
    }
    write!(out, "]")?;

    Ok(())
}

fn write_palettes(
    out: &mut impl Write,
    segments: &[libpgs::pgs::PgsSegment],
    raw_payloads: bool,
) -> std::io::Result<()> {
    use libpgs::pgs::segment::SegmentType;

    write!(out, "\"palettes\":[")?;
    let mut first = true;
    for seg in segments
        .iter()
        .filter(|s| s.segment_type == SegmentType::PaletteDefinition)
    {
        if let Some(pds) = seg.parse_pds() {
            if !first {
                write!(out, ",")?;
            }
            first = false;
            write!(
                out,
                "{{\"id\":{},\"version\":{},\"entries\":[",
                pds.id, pds.version,
            )?;
            for (ei, entry) in pds.entries.iter().enumerate() {
                if ei > 0 {
                    write!(out, ",")?;
                }
                write!(
                    out,
                    "{{\"id\":{},\"luminance\":{},\"cr\":{},\"cb\":{},\"alpha\":{}}}",
                    entry.id, entry.luminance, entry.cr, entry.cb, entry.alpha,
                )?;
            }
            write!(out, "]")?;
            if raw_payloads {
                write_base64_field(out, b",\"payload\":\"", &seg.payload)?;
            }
            write!(out, "}}")?;
        }
    }
    write!(out, "]")?;

    Ok(())
}

fn write_objects(
    out: &mut impl Write,
    segments: &[libpgs::pgs::PgsSegment],
    raw_payloads: bool,
) -> std::io::Result<()> {
    use libpgs::pgs::payload::ods_rle_data;
    use libpgs::pgs::rle::decode_rle;
    use libpgs::pgs::segment::SegmentType;

    // Collect ODS segments with parsed data.
    let ods_segments: Vec<_> = segments
        .iter()
        .filter(|s| s.segment_type == SegmentType::ObjectDefinition)
        .filter_map(|seg| seg.parse_ods().map(|ods| (seg, ods)))
        .collect();

    // Group by object ID, preserving first-appearance order.
    let mut groups: Vec<(u16, Vec<(&libpgs::pgs::PgsSegment, libpgs::pgs::OdsData)>)> = Vec::new();
    for (seg, ods) in ods_segments {
        if let Some(group) = groups.iter_mut().find(|(id, _)| *id == ods.id) {
            group.1.push((seg, ods));
        } else {
            groups.push((ods.id, vec![(seg, ods)]));
        }
    }

    write!(out, "\"objects\":[")?;
    for (gi, (_obj_id, fragments)) in groups.iter().enumerate() {
        if gi > 0 {
            write!(out, ",")?;
        }

        // Get metadata from the first/complete fragment.
        let first_ods = &fragments[0].1;
        let is_reassembled = fragments.len() > 1;
        let sequence_str = if is_reassembled {
            "reassembled"
        } else {
            first_ods.sequence.as_str()
        };

        write!(
            out,
            "{{\"id\":{},\"version\":{},\"sequence\":\"{}\",\"data_length\":{}",
            first_ods.id, first_ods.version, sequence_str, first_ods.data_length,
        )?;

        // Width/height from Complete or First fragment.
        let width = first_ods.width;
        let height = first_ods.height;
        if let Some(w) = width {
            write!(out, ",\"width\":{}", w)?;
        }
        if let Some(h) = height {
            write!(out, ",\"height\":{}", h)?;
        }

        // Decode bitmap: concatenate RLE data from all fragments, then decode.
        if let (Some(w), Some(h)) = (width, height) {
            let mut rle_data = Vec::new();
            let mut rle_ok = true;
            for (seg, ods) in fragments {
                if let Some(data) = ods_rle_data(&seg.payload, ods.sequence) {
                    rle_data.extend_from_slice(data);
                } else {
                    rle_ok = false;
                    break;
                }
            }
            if rle_ok {
                if let Some(pixels) = decode_rle(&rle_data, w, h) {
                    write_base64_field(out, b",\"bitmap\":\"", &pixels)?;
                } else {
                    write!(out, ",\"bitmap\":null")?;
                }
            } else {
                write!(out, ",\"bitmap\":null")?;
            }
        } else {
            write!(out, ",\"bitmap\":null")?;
        }

        if raw_payloads {
            // For reassembled objects, concatenate all raw payloads.
            if fragments.len() == 1 {
                write_base64_field(out, b",\"payload\":\"", &fragments[0].0.payload)?;
            } else {
                let mut combined = Vec::new();
                for (seg, _) in fragments {
                    combined.extend_from_slice(&seg.payload);
                }
                write_base64_field(out, b",\"payload\":\"", &combined)?;
            }
        }
        write!(out, "}}")?;
    }
    write!(out, "]")?;

    Ok(())
}

fn json_string_or_null(s: Option<&str>) -> String {
    match s {
        Some(v) => {
            let escaped = v
                .replace('\\', "\\\\")
                .replace('"', "\\\"")
                .replace('\n', "\\n")
                .replace('\r', "\\r")
                .replace('\t', "\\t");
            format!("\"{escaped}\"")
        }
        None => "null".to_string(),
    }
}

fn json_bool_or_null(b: Option<bool>) -> &'static str {
    match b {
        Some(true) => "true",
        Some(false) => "false",
        None => "null",
    }
}

fn json_u64_or_null(n: Option<u64>) -> String {
    match n {
        Some(v) => v.to_string(),
        None => "null".to_string(),
    }
}

fn container_name(c: libpgs::ContainerFormat) -> &'static str {
    match c {
        libpgs::ContainerFormat::Matroska => "Matroska",
        libpgs::ContainerFormat::M2ts => "M2TS",
        libpgs::ContainerFormat::TransportStream => "TransportStream",
        libpgs::ContainerFormat::Sup => "SUP",
    }
}

fn composition_state_name(cs: libpgs::pgs::segment::CompositionState) -> &'static str {
    match cs {
        libpgs::pgs::segment::CompositionState::Normal => "normal",
        libpgs::pgs::segment::CompositionState::AcquisitionPoint => "acquisition_point",
        libpgs::pgs::segment::CompositionState::EpochStart => "epoch_start",
    }
}

const BASE64_CHARS: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/// Write `<prefix><base64(data)>"` to the writer. Used for fields like
/// `,"bitmap":"` + base64 + closing `"`.
fn write_base64_field<W: Write>(out: &mut W, prefix: &[u8], data: &[u8]) -> std::io::Result<()> {
    out.write_all(prefix)?;
    write_base64(out, data)?;
    out.write_all(b"\"")?;
    Ok(())
}

/// Base64-encode `data` directly into the writer without an intermediate String.
/// Byte-for-byte identical to `base64_encode`.
fn write_base64<W: Write>(out: &mut W, data: &[u8]) -> std::io::Result<()> {
    let mut buf = [0u8; 1024];
    let mut chunks = data.chunks_exact(3);
    let mut pos = 0;
    for chunk in chunks.by_ref() {
        if pos + 4 > buf.len() {
            out.write_all(&buf[..pos])?;
            pos = 0;
        }
        let triple = ((chunk[0] as u32) << 16) | ((chunk[1] as u32) << 8) | (chunk[2] as u32);
        buf[pos] = BASE64_CHARS[((triple >> 18) & 0x3F) as usize];
        buf[pos + 1] = BASE64_CHARS[((triple >> 12) & 0x3F) as usize];
        buf[pos + 2] = BASE64_CHARS[((triple >> 6) & 0x3F) as usize];
        buf[pos + 3] = BASE64_CHARS[(triple & 0x3F) as usize];
        pos += 4;
    }
    let rem = chunks.remainder();
    if !rem.is_empty() {
        if pos + 4 > buf.len() {
            out.write_all(&buf[..pos])?;
            pos = 0;
        }
        let b0 = rem[0] as u32;
        let b1 = if rem.len() > 1 { rem[1] as u32 } else { 0 };
        let triple = (b0 << 16) | (b1 << 8);
        buf[pos] = BASE64_CHARS[((triple >> 18) & 0x3F) as usize];
        buf[pos + 1] = BASE64_CHARS[((triple >> 12) & 0x3F) as usize];
        buf[pos + 2] = if rem.len() > 1 {
            BASE64_CHARS[((triple >> 6) & 0x3F) as usize]
        } else {
            b'='
        };
        buf[pos + 3] = b'=';
        pos += 4;
    }
    if pos > 0 {
        out.write_all(&buf[..pos])?;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// encode command — JSON parser, base64 decoder, field extraction
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
enum JsonValue {
    Null,
    Bool(bool),
    Str(String),
    Num(f64),
    Array(Vec<JsonValue>),
    Object(Vec<(String, JsonValue)>),
}

impl JsonValue {
    fn as_str(&self) -> Option<&str> {
        match self {
            JsonValue::Str(s) => Some(s),
            _ => None,
        }
    }

    fn as_bool(&self) -> Option<bool> {
        match self {
            JsonValue::Bool(b) => Some(*b),
            _ => None,
        }
    }

    fn as_f64(&self) -> Option<f64> {
        match self {
            JsonValue::Num(n) => Some(*n),
            _ => None,
        }
    }

    fn as_u64(&self) -> Option<u64> {
        self.as_f64().map(|n| n as u64)
    }

    fn as_u16(&self) -> Option<u16> {
        self.as_f64().map(|n| n as u16)
    }

    fn as_u8(&self) -> Option<u8> {
        self.as_f64().map(|n| n as u8)
    }

    fn as_array(&self) -> Option<&[JsonValue]> {
        match self {
            JsonValue::Array(a) => Some(a),
            _ => None,
        }
    }

    fn is_null(&self) -> bool {
        matches!(self, JsonValue::Null)
    }

    fn get(&self, key: &str) -> Option<&JsonValue> {
        match self {
            JsonValue::Object(pairs) => pairs.iter().find(|(k, _)| k == key).map(|(_, v)| v),
            _ => None,
        }
    }
}

fn json_parse(s: &str) -> Result<JsonValue, String> {
    let bytes = s.as_bytes();
    let i = json_skip_ws(bytes, 0);
    if i >= bytes.len() {
        return Err("empty JSON input".into());
    }
    let (val, _) = json_parse_value(bytes, i)?;
    Ok(val)
}

fn json_skip_ws(b: &[u8], mut i: usize) -> usize {
    while i < b.len() && matches!(b[i], b' ' | b'\t' | b'\r' | b'\n') {
        i += 1;
    }
    i
}

fn json_parse_value(b: &[u8], i: usize) -> Result<(JsonValue, usize), String> {
    if i >= b.len() {
        return Err("unexpected end of input".into());
    }
    match b[i] {
        b'"' => json_parse_string(b, i),
        b'{' => json_parse_object(b, i),
        b'[' => json_parse_array(b, i),
        b'n' => {
            if b.len() < i + 4 || &b[i..i + 4] != b"null" {
                return Err(format!("expected 'null' at offset {i}"));
            }
            Ok((JsonValue::Null, i + 4))
        }
        b't' => {
            if b.len() < i + 4 || &b[i..i + 4] != b"true" {
                return Err(format!("expected 'true' at offset {i}"));
            }
            Ok((JsonValue::Bool(true), i + 4))
        }
        b'f' => {
            if b.len() < i + 5 || &b[i..i + 5] != b"false" {
                return Err(format!("expected 'false' at offset {i}"));
            }
            Ok((JsonValue::Bool(false), i + 5))
        }
        c if c.is_ascii_digit() || c == b'-' => json_parse_number(b, i),
        c => Err(format!("unexpected character '{}' at offset {i}", c as char)),
    }
}

fn json_parse_string(b: &[u8], i: usize) -> Result<(JsonValue, usize), String> {
    if b[i] != b'"' {
        return Err(format!("expected '\"' at offset {i}"));
    }
    let mut j = i + 1;
    let mut s = String::new();
    while j < b.len() && b[j] != b'"' {
        if b[j] == b'\\' {
            j += 1;
            if j >= b.len() {
                return Err("unterminated string escape".into());
            }
            match b[j] {
                b'"' => s.push('"'),
                b'\\' => s.push('\\'),
                b'/' => s.push('/'),
                b'n' => s.push('\n'),
                b'r' => s.push('\r'),
                b't' => s.push('\t'),
                _ => {
                    s.push('\\');
                    s.push(b[j] as char);
                }
            }
        } else {
            s.push(b[j] as char);
        }
        j += 1;
    }
    if j >= b.len() {
        return Err("unterminated string".into());
    }
    Ok((JsonValue::Str(s), j + 1))
}

fn json_parse_number(b: &[u8], i: usize) -> Result<(JsonValue, usize), String> {
    let mut j = i;
    while j < b.len()
        && (b[j].is_ascii_digit()
            || b[j] == b'.'
            || b[j] == b'-'
            || b[j] == b'e'
            || b[j] == b'E'
            || b[j] == b'+')
    {
        j += 1;
    }
    let s = std::str::from_utf8(&b[i..j]).map_err(|_| format!("invalid number at offset {i}"))?;
    let n: f64 = s
        .parse()
        .map_err(|_| format!("invalid number '{s}' at offset {i}"))?;
    Ok((JsonValue::Num(n), j))
}

fn json_parse_array(b: &[u8], i: usize) -> Result<(JsonValue, usize), String> {
    let mut j = json_skip_ws(b, i + 1);
    let mut items = Vec::new();
    if j < b.len() && b[j] == b']' {
        return Ok((JsonValue::Array(items), j + 1));
    }
    loop {
        let (val, next) = json_parse_value(b, j)?;
        items.push(val);
        j = json_skip_ws(b, next);
        if j >= b.len() {
            return Err("unterminated array".into());
        }
        if b[j] == b']' {
            return Ok((JsonValue::Array(items), j + 1));
        }
        if b[j] != b',' {
            return Err(format!("expected ',' or ']' at offset {j}"));
        }
        j = json_skip_ws(b, j + 1);
    }
}

fn json_parse_object(b: &[u8], i: usize) -> Result<(JsonValue, usize), String> {
    let mut j = json_skip_ws(b, i + 1);
    let mut pairs = Vec::new();
    if j < b.len() && b[j] == b'}' {
        return Ok((JsonValue::Object(pairs), j + 1));
    }
    loop {
        let (key_val, next) = json_parse_string(b, j)?;
        let key = match key_val {
            JsonValue::Str(s) => s,
            _ => unreachable!(),
        };
        j = json_skip_ws(b, next);
        if j >= b.len() || b[j] != b':' {
            return Err(format!("expected ':' after key '{key}' at offset {j}"));
        }
        j = json_skip_ws(b, j + 1);
        let (val, next) = json_parse_value(b, j)?;
        pairs.push((key, val));
        j = json_skip_ws(b, next);
        if j >= b.len() {
            return Err("unterminated object".into());
        }
        if b[j] == b'}' {
            return Ok((JsonValue::Object(pairs), j + 1));
        }
        if b[j] != b',' {
            return Err(format!("expected ',' or '}}' at offset {j}"));
        }
        j = json_skip_ws(b, j + 1);
    }
}

fn base64_decode(s: &str) -> Result<Vec<u8>, String> {
    const TABLE: [u8; 128] = {
        let mut t = [255u8; 128];
        let mut i = 0u8;
        while i < 26 {
            t[(b'A' + i) as usize] = i;
            t[(b'a' + i) as usize] = i + 26;
            i += 1;
        }
        let mut d = 0u8;
        while d < 10 {
            t[(b'0' + d) as usize] = d + 52;
            d += 1;
        }
        t[b'+' as usize] = 62;
        t[b'/' as usize] = 63;
        t
    };
    let bytes: Vec<u8> = s.bytes().filter(|&b| b != b'=').collect();
    let mut out = Vec::with_capacity(bytes.len() * 3 / 4);
    for chunk in bytes.chunks(4) {
        let mut buf = [0u32; 4];
        for (i, &b) in chunk.iter().enumerate() {
            if b >= 128 || TABLE[b as usize] == 255 {
                return Err(format!("invalid base64 character: '{}'", b as char));
            }
            buf[i] = TABLE[b as usize] as u32;
        }
        let triple = (buf[0] << 18) | (buf[1] << 12) | (buf[2] << 6) | buf[3];
        out.push((triple >> 16) as u8);
        if chunk.len() > 2 {
            out.push((triple >> 8) as u8);
        }
        if chunk.len() > 3 {
            out.push(triple as u8);
        }
    }
    Ok(out)
}

fn require_field<'a>(obj: &'a JsonValue, key: &str, line: usize) -> Result<&'a JsonValue, String> {
    obj.get(key)
        .ok_or_else(|| format!("line {line}: missing field '{key}'"))
}

fn require_u16(obj: &JsonValue, key: &str, line: usize) -> Result<u16, String> {
    require_field(obj, key, line)?
        .as_u16()
        .ok_or_else(|| format!("line {line}: field '{key}' is not a number"))
}

fn require_u8(obj: &JsonValue, key: &str, line: usize) -> Result<u8, String> {
    require_field(obj, key, line)?
        .as_u8()
        .ok_or_else(|| format!("line {line}: field '{key}' is not a number"))
}

fn parse_composition_state_str(s: &str) -> Option<libpgs::pgs::segment::CompositionState> {
    match s {
        "normal" => Some(libpgs::pgs::segment::CompositionState::Normal),
        "acquisition_point" => Some(libpgs::pgs::segment::CompositionState::AcquisitionPoint),
        "epoch_start" => Some(libpgs::pgs::segment::CompositionState::EpochStart),
        _ => None,
    }
}

fn parse_pcs_json(
    val: &JsonValue,
    line: usize,
) -> Result<libpgs::pgs::PcsData, String> {
    let state_str = require_field(val, "state", line)?
        .as_str()
        .ok_or_else(|| format!("line {line}: 'state' is not a string"))?;
    let composition_state = parse_composition_state_str(state_str)
        .ok_or_else(|| format!("line {line}: unknown composition state '{state_str}'"))?;

    let mut objects = Vec::new();
    if let Some(arr) = val.get("objects").and_then(|v| v.as_array()) {
        for obj in arr {
            let crop = if let Some(c) = obj.get("crop") {
                if c.is_null() {
                    None
                } else {
                    Some(libpgs::pgs::CropInfo {
                        x: require_u16(c, "x", line)?,
                        y: require_u16(c, "y", line)?,
                        width: require_u16(c, "width", line)?,
                        height: require_u16(c, "height", line)?,
                    })
                }
            } else {
                None
            };
            objects.push(libpgs::pgs::CompositionObject {
                object_id: require_u16(obj, "object_id", line)?,
                window_id: require_u8(obj, "window_id", line)?,
                x: require_u16(obj, "x", line)?,
                y: require_u16(obj, "y", line)?,
                crop,
            });
        }
    }

    Ok(libpgs::pgs::PcsData {
        video_width: require_u16(val, "video_width", line)?,
        video_height: require_u16(val, "video_height", line)?,
        composition_number: require_u16(val, "number", line)?,
        composition_state,
        palette_only: require_field(val, "palette_only", line)?
            .as_bool()
            .ok_or_else(|| format!("line {line}: 'palette_only' is not a boolean"))?,
        palette_id: require_u8(val, "palette_id", line)?,
        objects,
    })
}

fn parse_wds_json(
    arr: &[JsonValue],
    line: usize,
) -> Result<libpgs::pgs::WdsData, String> {
    let mut windows = Vec::with_capacity(arr.len());
    for w in arr {
        windows.push(libpgs::pgs::WindowDefinition {
            id: require_u8(w, "id", line)?,
            x: require_u16(w, "x", line)?,
            y: require_u16(w, "y", line)?,
            width: require_u16(w, "width", line)?,
            height: require_u16(w, "height", line)?,
        });
    }
    Ok(libpgs::pgs::WdsData { windows })
}

fn parse_pds_json(
    arr: &[JsonValue],
    line: usize,
) -> Result<Vec<libpgs::pgs::PdsData>, String> {
    let mut palettes = Vec::with_capacity(arr.len());
    for p in arr {
        let entries_arr = require_field(p, "entries", line)?
            .as_array()
            .ok_or_else(|| format!("line {line}: 'entries' is not an array"))?;
        let mut entries = Vec::with_capacity(entries_arr.len());
        for e in entries_arr {
            entries.push(libpgs::pgs::PaletteEntry {
                id: require_u8(e, "id", line)?,
                luminance: require_u8(e, "luminance", line)?,
                cr: require_u8(e, "cr", line)?,
                cb: require_u8(e, "cb", line)?,
                alpha: require_u8(e, "alpha", line)?,
            });
        }
        palettes.push(libpgs::pgs::PdsData {
            id: require_u8(p, "id", line)?,
            version: require_u8(p, "version", line)?,
            entries,
        });
    }
    Ok(palettes)
}

fn parse_object_json(
    val: &JsonValue,
    line: usize,
) -> Result<libpgs::pgs::ObjectBitmap, String> {
    let id = require_u16(val, "id", line)?;
    let version = require_u8(val, "version", line)?;
    let width = require_u16(val, "width", line)?;
    let height = require_u16(val, "height", line)?;

    let bitmap_val = require_field(val, "bitmap", line)?;
    if bitmap_val.is_null() {
        return Err(format!("line {line}: object {id} has null bitmap, cannot encode"));
    }
    let bitmap_b64 = bitmap_val
        .as_str()
        .ok_or_else(|| format!("line {line}: 'bitmap' is not a string"))?;
    let pixels = base64_decode(bitmap_b64)
        .map_err(|e| format!("line {line}: object {id} bitmap decode failed: {e}"))?;

    let expected = width as usize * height as usize;
    if pixels.len() != expected {
        return Err(format!(
            "line {line}: object {id} bitmap size mismatch: got {} bytes, expected {} ({}x{})",
            pixels.len(),
            expected,
            width,
            height
        ));
    }

    Ok(libpgs::pgs::ObjectBitmap {
        id,
        version,
        width,
        height,
        pixels,
    })
}

fn cmd_encode(args: &[String]) -> Result<(), libpgs::error::PgsError> {
    use libpgs::error::PgsError;

    if args.is_empty() {
        eprintln!("Usage: libpgs encode -o <output.sup>");
        eprintln!("  Reads NDJSON from stdin (same format as 'libpgs stream' output)");
        process::exit(1);
    }

    let mut output: Option<PathBuf> = None;
    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "-o" | "--output" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for -o");
                    process::exit(1);
                }
                output = Some(PathBuf::from(&args[i]));
            }
            other => {
                eprintln!("Unknown option: {other}");
                process::exit(1);
            }
        }
        i += 1;
    }

    let output = output.unwrap_or_else(|| {
        eprintln!("Missing -o <output.sup>");
        process::exit(1);
    });

    let stdin = std::io::stdin();
    let reader = std::io::BufReader::new(stdin.lock());

    // Collect display sets grouped by track_id.
    let mut track_display_sets: Vec<(u64, Vec<libpgs::pgs::DisplaySet>)> = Vec::new();
    let mut skipped = 0u64;

    for (line_idx, line_result) in reader.lines().enumerate() {
        let line = line_result.map_err(PgsError::Io)?;
        let line_num = line_idx + 1;

        if line.trim().is_empty() {
            continue;
        }

        let json = json_parse(&line)
            .map_err(|e| PgsError::EncodingError(format!("line {line_num}: {e}")))?;

        let type_str = json.get("type").and_then(|v| v.as_str());
        match type_str {
            Some("tracks") => continue,
            Some("display_set") => {}
            Some(other) => {
                eprintln!("line {line_num}: skipping unknown type '{other}'");
                continue;
            }
            None => {
                return Err(PgsError::EncodingError(format!(
                    "line {line_num}: missing 'type' field"
                )));
            }
        }

        // Extract PTS: prefer integer 'pts', fall back to 'pts_ms' * 90.
        let pts = if let Some(pts_val) = json.get("pts") {
            pts_val
                .as_u64()
                .ok_or_else(|| PgsError::EncodingError(format!(
                    "line {line_num}: 'pts' is not a number"
                )))?
        } else if let Some(pts_ms_val) = json.get("pts_ms") {
            let ms = pts_ms_val
                .as_f64()
                .ok_or_else(|| PgsError::EncodingError(format!(
                    "line {line_num}: 'pts_ms' is not a number"
                )))?;
            (ms * 90.0).round() as u64
        } else {
            return Err(PgsError::EncodingError(format!(
                "line {line_num}: missing 'pts' or 'pts_ms'"
            )));
        };

        let track_id = json.get("track_id").and_then(|v| v.as_u64()).unwrap_or(0);

        // Parse composition (required).
        let comp_val = json.get("composition");
        if comp_val.is_none() || comp_val.unwrap().is_null() {
            eprintln!("line {line_num}: skipping display set with null composition");
            skipped += 1;
            continue;
        }
        let pcs = parse_pcs_json(comp_val.unwrap(), line_num)
            .map_err(|e| PgsError::EncodingError(e))?;

        let mut builder = libpgs::pgs::DisplaySetBuilder::new(pts).pcs(pcs);

        // Parse windows.
        if let Some(arr) = json.get("windows").and_then(|v| v.as_array()) {
            if !arr.is_empty() {
                let wds = parse_wds_json(arr, line_num)
                    .map_err(|e| PgsError::EncodingError(e))?;
                builder = builder.wds(wds);
            }
        }

        // Parse palettes.
        if let Some(arr) = json.get("palettes").and_then(|v| v.as_array()) {
            if !arr.is_empty() {
                let palettes = parse_pds_json(arr, line_num)
                    .map_err(|e| PgsError::EncodingError(e))?;
                for pds in palettes {
                    builder = builder.palette(pds);
                }
            }
        }

        // Parse objects.
        if let Some(arr) = json.get("objects").and_then(|v| v.as_array()) {
            for obj_val in arr {
                let bitmap = parse_object_json(obj_val, line_num)
                    .map_err(|e| PgsError::EncodingError(e))?;
                builder = builder.object(bitmap);
            }
        }

        let ds = builder.build()?;

        // Group by track_id.
        if let Some(group) = track_display_sets.iter_mut().find(|(tid, _)| *tid == track_id) {
            group.1.push(ds);
        } else {
            track_display_sets.push((track_id, vec![ds]));
        }
    }

    if track_display_sets.is_empty() {
        eprintln!("No display sets found in input.");
        return Ok(());
    }

    // Write output.
    let total_ds: usize = track_display_sets.iter().map(|(_, dss)| dss.len()).sum();

    if track_display_sets.len() == 1 {
        // Single track: write directly to the output path.
        let (track_id, display_sets) = &track_display_sets[0];
        libpgs::write_sup_file(display_sets, &output)?;
        let file_size = std::fs::metadata(&output).map(|m| m.len()).unwrap_or(0);
        eprintln!(
            "Encoded {} display sets (track {}) to {} ({} bytes)",
            display_sets.len(),
            track_id,
            output.display(),
            file_size,
        );
    } else {
        // Multiple tracks: write <stem>_track<id>.<ext> per track.
        let stem = output
            .file_stem()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| "output".to_string());
        let ext = output
            .extension()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| "sup".to_string());
        let parent = output.parent().unwrap_or_else(|| std::path::Path::new("."));

        for (track_id, display_sets) in &track_display_sets {
            let track_output = parent.join(format!("{}_track{}.{}", stem, track_id, ext));
            libpgs::write_sup_file(display_sets, &track_output)?;
            let file_size = std::fs::metadata(&track_output)
                .map(|m| m.len())
                .unwrap_or(0);
            eprintln!(
                "  Track {} -> {} ({} display sets, {} bytes)",
                track_id,
                track_output.display(),
                display_sets.len(),
                file_size,
            );
        }
        eprintln!(
            "Encoded {} display sets across {} tracks",
            total_ds,
            track_display_sets.len()
        );
    }

    if skipped > 0 {
        eprintln!("Skipped {} display sets with null composition", skipped);
    }

    Ok(())
}

fn cmd_extract(args: &[String]) -> Result<(), libpgs::error::PgsError> {
    if args.is_empty() {
        eprintln!("Usage: libpgs extract <file> -o <out> [-t <id>] [--start TIME] [--end TIME]");
        process::exit(1);
    }

    let input = PathBuf::from(&args[0]);
    let mut output: Option<PathBuf> = None;
    let mut track_ids: Vec<u32> = Vec::new();
    let mut start_ms: Option<f64> = None;
    let mut end_ms: Option<f64> = None;

    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "-o" | "--output" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for -o");
                    process::exit(1);
                }
                output = Some(PathBuf::from(&args[i]));
            }
            "-t" | "--track" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for -t");
                    process::exit(1);
                }
                track_ids.extend(parse_track_ids(&args[i]));
            }
            "--start" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for --start");
                    process::exit(1);
                }
                start_ms = Some(parse_timestamp(&args[i]).unwrap_or_else(|| {
                    eprintln!("Invalid timestamp for --start: {}", args[i]);
                    process::exit(1);
                }));
            }
            "--end" => {
                i += 1;
                if i >= args.len() {
                    eprintln!("Missing value for --end");
                    process::exit(1);
                }
                end_ms = Some(parse_timestamp(&args[i]).unwrap_or_else(|| {
                    eprintln!("Invalid timestamp for --end: {}", args[i]);
                    process::exit(1);
                }));
            }
            other => {
                eprintln!("Unknown option: {other}");
                process::exit(1);
            }
        }
        i += 1;
    }

    let output = output.unwrap_or_else(|| {
        eprintln!("Missing -o <output.sup>");
        process::exit(1);
    });

    let has_time_range = start_ms.is_some() || end_ms.is_some();

    let start = Instant::now();

    if track_ids.len() == 1 {
        // Single-track extraction.
        let tid = track_ids[0];
        println!("Extracting PGS track {} from: {}", tid, input.display());

        // Use Extractor API when time range is set (batch helpers don't support it).
        let display_sets = if has_time_range {
            let extractor = libpgs::Extractor::open(&input)?
                .with_track_filter(&[tid])
                .with_time_range(start_ms, end_ms);
            extractor
                .collect::<Result<Vec<_>, _>>()?
                .into_iter()
                .map(|tds| tds.display_set)
                .collect::<Vec<_>>()
        } else {
            libpgs::extract_display_sets(&input, Some(tid))?
        };
        let elapsed_extract = start.elapsed();

        let total_segments: usize = display_sets.iter().map(|ds| ds.segments.len()).sum();
        println!(
            "Found {} display sets ({} segments) in {:.2}s",
            display_sets.len(),
            total_segments,
            elapsed_extract.as_secs_f64()
        );

        libpgs::write_sup_file(&display_sets, &output)?;
        let elapsed_total = start.elapsed();

        let file_size = std::fs::metadata(&output).map(|m| m.len()).unwrap_or(0);
        println!(
            "Written to: {} ({} bytes) in {:.2}s total",
            output.display(),
            file_size,
            elapsed_total.as_secs_f64()
        );
    } else {
        // Multi-track extraction: write <stem>_track<id>.sup per track.
        if track_ids.is_empty() {
            println!("Extracting all PGS tracks from: {}", input.display());
        } else {
            println!(
                "Extracting PGS tracks {:?} from: {}",
                track_ids,
                input.display()
            );
        }

        let track_results = if track_ids.is_empty() && !has_time_range {
            libpgs::extract_all_display_sets(&input)?
        } else {
            let mut ext = libpgs::Extractor::open(&input)?;
            if !track_ids.is_empty() {
                ext = ext.with_track_filter(&track_ids);
            }
            if has_time_range {
                ext = ext.with_time_range(start_ms, end_ms);
            }
            ext.collect_by_track()?
        };
        let elapsed_extract = start.elapsed();

        if track_results.is_empty() {
            if has_time_range {
                println!("No PGS display sets found in the requested time range.");
            } else {
                println!("No PGS display sets found.");
            }
            return Ok(());
        }

        let total_ds: usize = track_results.iter().map(|t| t.display_sets.len()).sum();
        println!(
            "Found {} tracks, {} display sets in {:.2}s",
            track_results.len(),
            total_ds,
            elapsed_extract.as_secs_f64()
        );

        // Derive output stem and extension from -o path.
        let stem = output
            .file_stem()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| "output".to_string());
        let ext = output
            .extension()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| "sup".to_string());
        let parent = output.parent().unwrap_or_else(|| std::path::Path::new("."));

        if track_results.len() == 1 {
            // Only one track — write directly to the specified output path.
            let t = &track_results[0];
            libpgs::write_sup_file(&t.display_sets, &output)?;
            let file_size = std::fs::metadata(&output).map(|m| m.len()).unwrap_or(0);
            println!(
                "  Track {} -> {} ({} bytes)",
                t.track.track_id,
                output.display(),
                file_size
            );
        } else {
            for t in &track_results {
                let track_output =
                    parent.join(format!("{}_track{}.{}", stem, t.track.track_id, ext));
                libpgs::write_sup_file(&t.display_sets, &track_output)?;
                let file_size = std::fs::metadata(&track_output)
                    .map(|m| m.len())
                    .unwrap_or(0);
                println!(
                    "  Track {} -> {} ({} bytes)",
                    t.track.track_id,
                    track_output.display(),
                    file_size
                );
            }
        }

        let elapsed_total = start.elapsed();
        println!("Done in {:.2}s total", elapsed_total.as_secs_f64());
    }

    Ok(())
}