oxideav-sub-image 0.0.7

Pure-Rust bitmap-subtitle decoders: PGS (.sup Blu-ray), DVB subtitles, VobSub (.idx/.sub)
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
//! VobSub / DVD SPU parser, container (`.idx`+`.sub`), and decoder.
//!
//! A VobSub title is a text `.idx` file alongside a binary `.sub`
//! file. The `.idx` carries:
//!
//! * a 16-colour YCrCb palette;
//! * the subpicture canvas size;
//! * per-language entries with `timestamp: 00:00:01:000, filepos:
//!   000000000` cue timestamps.
//!
//! The `.sub` file is a tiny MPEG Program Stream (`0x00 00 01 BA` pack
//! headers, `0x00 00 01 BD` private-stream-1 PES packets). Each PES
//! payload carries one SPU unit:
//!
//! ```text
//! SPU size (2 BE)
//! control-block offset (2 BE)  [within the SPU]
//! RLE bitmap bytes             [from 4 to control_offset]
//! control sequences            [from control_offset to SPU size]
//! ```
//!
//! Control sequences consist of:
//!
//! ```text
//! delay (2 BE, in 1024/90000 s units)
//! next-offset (2 BE)
//! command bytes until 0xFF terminator
//! ```
//!
//! Commands:
//!
//! | 0x00 | force-display |
//! | 0x01 | start-display |
//! | 0x02 | stop-display |
//! | 0x03 | palette sel   | 2 bytes: (bg<<4|pat, emp2<<4|emp1) |
//! | 0x04 | alpha         | 2 bytes: (bg<<4|pat, emp2<<4|emp1) |
//! | 0x05 | coords        | 6 bytes: x1:12 x2:12 y1:12 y2:12 |
//! | 0x06 | rle offsets   | 4 bytes: top_off:16 bot_off:16 |
//! | 0xFF | end           |
//!
//! ## Scope / limitations
//!
//! * **Decode only.**
//! * Handles the standard 4-colour palette + alpha form (every SPU
//!   uses exactly 4 of the 16 palette entries). Per-line palette
//!   switching commands (0x07) are not implemented.
//! * Palette/alpha defaults are black-text-on-transparent when the
//!   SPU omits a colour command (malformed streams).
//! * `.idx` without a palette line falls back to an all-grey fallback
//!   so tests without a full index still render something.

use std::collections::VecDeque;
use std::io::{Read, SeekFrom};
use std::path::{Path, PathBuf};

use oxideav_core::Decoder;
use oxideav_core::{
    CodecId, CodecParameters, CodecResolver, Error, Frame, MediaType, Packet, PixelFormat, Result,
    StreamInfo, TimeBase, VideoFrame, VideoPlane,
};
use oxideav_core::{ContainerRegistry, Demuxer, ProbeData, ProbeScore, ReadSeek};

use crate::VOBSUB_CODEC_ID;

// --- .idx parser -------------------------------------------------------

/// Parsed contents of a VobSub `.idx` file.
#[derive(Clone, Debug, Default)]
pub struct VobSubIdx {
    pub size: (u16, u16),
    /// 16 entries, each RGB (pre-YCbCr-to-RGB-converted).
    pub palette_rgb: [[u8; 3]; 16],
    pub has_palette: bool,
    /// Cue entries: (start_us, filepos).
    pub cues: Vec<(i64, u64)>,
}

pub fn parse_idx(text: &str) -> Result<VobSubIdx> {
    let mut idx = VobSubIdx::default();
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if let Some(rest) = line.strip_prefix("size:") {
            let rest = rest.trim();
            if let Some((w, h)) = rest.split_once('x') {
                let w: u16 = w
                    .trim()
                    .parse()
                    .map_err(|e| Error::invalid(format!("vobsub idx: bad size width: {e}")))?;
                let h: u16 = h
                    .trim()
                    .parse()
                    .map_err(|e| Error::invalid(format!("vobsub idx: bad size height: {e}")))?;
                idx.size = (w, h);
            }
        } else if let Some(rest) = line.strip_prefix("palette:") {
            parse_palette_line(rest.trim(), &mut idx)?;
        } else if let Some(rest) = line.strip_prefix("timestamp:") {
            parse_timestamp_line(rest.trim(), &mut idx)?;
        }
    }
    Ok(idx)
}

fn parse_palette_line(s: &str, idx: &mut VobSubIdx) -> Result<()> {
    let mut cnt = 0usize;
    for token in s
        .split(|c: char| c == ',' || c.is_whitespace())
        .filter(|t| !t.is_empty())
    {
        if cnt >= 16 {
            break;
        }
        let hex = token.trim_start_matches("0x");
        let v = u32::from_str_radix(hex, 16)
            .map_err(|e| Error::invalid(format!("vobsub idx: bad palette entry '{token}': {e}")))?;
        let r = ((v >> 16) & 0xFF) as u8;
        let g = ((v >> 8) & 0xFF) as u8;
        let b = (v & 0xFF) as u8;
        idx.palette_rgb[cnt] = [r, g, b];
        cnt += 1;
    }
    if cnt > 0 {
        idx.has_palette = true;
    }
    Ok(())
}

fn parse_timestamp_line(s: &str, idx: &mut VobSubIdx) -> Result<()> {
    // Expected form: "00:00:01:000, filepos: 000000000"
    let mut ts_str: Option<&str> = None;
    let mut filepos_str: Option<&str> = None;
    for part in s.split(',') {
        let part = part.trim();
        if let Some(rest) = part.strip_prefix("filepos:") {
            filepos_str = Some(rest.trim());
        } else if ts_str.is_none() {
            ts_str = Some(part);
        }
    }
    let ts = ts_str.ok_or_else(|| Error::invalid("vobsub idx: timestamp missing"))?;
    let fp = filepos_str.ok_or_else(|| Error::invalid("vobsub idx: filepos missing"))?;
    let mut parts = ts.split(':');
    let h: i64 = parts
        .next()
        .unwrap_or("0")
        .parse()
        .map_err(|_| Error::invalid("vobsub idx: timestamp hours"))?;
    let m: i64 = parts
        .next()
        .unwrap_or("0")
        .parse()
        .map_err(|_| Error::invalid("vobsub idx: timestamp minutes"))?;
    let s_: i64 = parts
        .next()
        .unwrap_or("0")
        .parse()
        .map_err(|_| Error::invalid("vobsub idx: timestamp seconds"))?;
    let ms: i64 = parts
        .next()
        .unwrap_or("0")
        .parse()
        .map_err(|_| Error::invalid("vobsub idx: timestamp millis"))?;
    let us = ((((h * 60) + m) * 60) + s_) * 1_000_000 + ms * 1_000;
    let filepos = u64::from_str_radix(fp.trim_start_matches("0x"), 16)
        .or_else(|_| fp.parse::<u64>())
        .map_err(|_| Error::invalid("vobsub idx: bad filepos"))?;
    idx.cues.push((us, filepos));
    Ok(())
}

// --- SPU parse + decode -----------------------------------------------

#[derive(Clone, Debug, Default)]
pub struct Spu {
    pub x1: u16,
    pub y1: u16,
    pub x2: u16,
    pub y2: u16,
    /// palette indices (bg, pat, emp1, emp2) into the 16-entry idx palette.
    pub palette_sel: [u8; 4],
    /// alpha values (bg, pat, emp1, emp2), 0..15.
    pub alpha: [u8; 4],
    /// start-display delay in 1024/90000 s units from start of SPU.
    pub start_delay_raw: u16,
    /// stop-display delay (same unit).
    pub stop_delay_raw: u16,
    /// RLE data offsets for top/bottom fields, relative to start of SPU.
    pub top_rle_off: u16,
    pub bot_rle_off: u16,
}

/// Parse a SPU (one DVD subtitle unit), producing its control state and
/// a decoded width×height indexed bitmap (indices 0..3, mapping through
/// `palette_sel` → the 16-entry idx palette).
pub fn parse_and_decode_spu(spu: &[u8]) -> Result<(Spu, Vec<u8>, (u16, u16))> {
    if spu.len() < 4 {
        return Err(Error::invalid("vobsub SPU: too short"));
    }
    let spu_len = u16::from_be_bytes([spu[0], spu[1]]) as usize;
    let ctrl_off = u16::from_be_bytes([spu[2], spu[3]]) as usize;
    if spu_len > spu.len() || ctrl_off > spu_len || ctrl_off < 4 {
        return Err(Error::invalid("vobsub SPU: inconsistent sizes"));
    }

    let mut out = Spu::default();
    let mut pos = ctrl_off;
    let mut first_seq = true;
    loop {
        if pos + 4 > spu_len {
            break;
        }
        let delay = u16::from_be_bytes([spu[pos], spu[pos + 1]]);
        let next = u16::from_be_bytes([spu[pos + 2], spu[pos + 3]]) as usize;
        let mut cmd_pos = pos + 4;
        while cmd_pos < spu_len {
            let cmd = spu[cmd_pos];
            cmd_pos += 1;
            match cmd {
                0x00 => {} // force-display
                0x01 => {
                    // start-display
                    if first_seq {
                        out.start_delay_raw = delay;
                    }
                }
                0x02 => {
                    // stop-display
                    out.stop_delay_raw = delay;
                }
                0x03 => {
                    if cmd_pos + 2 > spu_len {
                        return Err(Error::invalid("vobsub SPU: palette command truncated"));
                    }
                    let b0 = spu[cmd_pos];
                    let b1 = spu[cmd_pos + 1];
                    cmd_pos += 2;
                    out.palette_sel[0] = b0 >> 4; // bg
                    out.palette_sel[1] = b0 & 0x0F; // pattern
                    out.palette_sel[2] = b1 >> 4; // emp1
                    out.palette_sel[3] = b1 & 0x0F; // emp2
                }
                0x04 => {
                    if cmd_pos + 2 > spu_len {
                        return Err(Error::invalid("vobsub SPU: alpha command truncated"));
                    }
                    let b0 = spu[cmd_pos];
                    let b1 = spu[cmd_pos + 1];
                    cmd_pos += 2;
                    out.alpha[0] = b0 >> 4;
                    out.alpha[1] = b0 & 0x0F;
                    out.alpha[2] = b1 >> 4;
                    out.alpha[3] = b1 & 0x0F;
                }
                0x05 => {
                    if cmd_pos + 6 > spu_len {
                        return Err(Error::invalid("vobsub SPU: coords command truncated"));
                    }
                    let b0 = spu[cmd_pos];
                    let b1 = spu[cmd_pos + 1];
                    let b2 = spu[cmd_pos + 2];
                    let b3 = spu[cmd_pos + 3];
                    let b4 = spu[cmd_pos + 4];
                    let b5 = spu[cmd_pos + 5];
                    cmd_pos += 6;
                    out.x1 = (((b0 as u16) << 4) | ((b1 as u16) >> 4)) & 0x0FFF;
                    out.x2 = ((((b1 as u16) & 0x0F) << 8) | (b2 as u16)) & 0x0FFF;
                    out.y1 = (((b3 as u16) << 4) | ((b4 as u16) >> 4)) & 0x0FFF;
                    out.y2 = ((((b4 as u16) & 0x0F) << 8) | (b5 as u16)) & 0x0FFF;
                }
                0x06 => {
                    if cmd_pos + 4 > spu_len {
                        return Err(Error::invalid("vobsub SPU: rle-offsets command truncated"));
                    }
                    out.top_rle_off = u16::from_be_bytes([spu[cmd_pos], spu[cmd_pos + 1]]);
                    out.bot_rle_off = u16::from_be_bytes([spu[cmd_pos + 2], spu[cmd_pos + 3]]);
                    cmd_pos += 4;
                }
                0xFF => {
                    break;
                }
                _ => {
                    // Unknown command — bail to avoid desync.
                    return Err(Error::invalid(format!(
                        "vobsub SPU: unknown command 0x{:02X}",
                        cmd
                    )));
                }
            }
        }
        first_seq = false;
        if next <= pos {
            break;
        }
        pos = next;
    }

    // Decode the bitmap.
    if out.x2 < out.x1 || out.y2 < out.y1 {
        return Err(Error::invalid("vobsub SPU: inverted coords"));
    }
    let width = (out.x2 - out.x1 + 1) as usize;
    let height = (out.y2 - out.y1 + 1) as usize;
    let mut pixels = vec![0u8; width * height];
    if width > 0 && height > 0 {
        let top_off = out.top_rle_off as usize;
        let bot_off = out.bot_rle_off as usize;
        if top_off >= spu_len {
            return Err(Error::invalid("vobsub SPU: top offset out of range"));
        }
        let bot_end = if bot_off > top_off { bot_off } else { ctrl_off };
        let top_bytes = &spu[top_off..bot_end.min(ctrl_off)];
        let bot_bytes = if bot_off > 0 {
            &spu[bot_off..ctrl_off]
        } else {
            &[][..]
        };
        decode_rle_field(top_bytes, width, height, 0, 2, &mut pixels)?;
        if !bot_bytes.is_empty() {
            decode_rle_field(bot_bytes, width, height, 1, 2, &mut pixels)?;
        }
    }
    Ok((out, pixels, (width as u16, height as u16)))
}

/// Decode a VobSub RLE field into the `pixels` buffer. The VobSub RLE
/// encodes pairs of (count, colour) where colour is 2 bits and count
/// uses 2, 4, 6, or 14 bits depending on prefix. A `count == 0` run
/// means "fill to end of line".
fn decode_rle_field(
    buf: &[u8],
    width: usize,
    height: usize,
    start_row: usize,
    row_step: usize,
    pixels: &mut [u8],
) -> Result<()> {
    let mut bits = NibbleReader::new(buf);
    let mut row = start_row;
    let mut col = 0usize;
    while row < height {
        let first = bits.read(4)?;
        let (count, colour) = if first >= 4 {
            (first >> 2, (first & 0x03) as u8)
        } else if first > 0 {
            // One more nibble for count.
            let n1 = bits.read(4)?;
            let combined = (first << 4) | n1;
            (combined >> 2, (combined & 0x03) as u8)
        } else {
            let n1 = bits.read(4)?;
            if n1 >= 4 {
                let combined = (first << 8) | (n1 << 4) | bits.read(4)?;
                (combined >> 2, (combined & 0x03) as u8)
            } else if n1 > 0 {
                let combined = (first << 12) | (n1 << 8) | (bits.read(4)? << 4) | bits.read(4)?;
                (combined >> 2, (combined & 0x03) as u8)
            } else {
                // rest-of-line: fill to width - col.
                let n2 = bits.read(4)?;
                let combined = (first << 12) | (n1 << 8) | (n2 << 4) | bits.read(4)?;
                (0, (combined & 0x03) as u8)
            }
        };
        let run = if count == 0 {
            width.saturating_sub(col)
        } else {
            count as usize
        };
        let end = (col + run).min(width);
        if row < height {
            let base = row * width + col;
            for px in &mut pixels[base..base + (end - col)] {
                *px = colour;
            }
        }
        col = end;
        if col >= width {
            // Align to next byte at end-of-line.
            bits.align();
            col = 0;
            row += row_step;
            if run == 0 {
                // fill-to-end was explicit; continue to next row.
            }
        }
    }
    Ok(())
}

struct NibbleReader<'a> {
    buf: &'a [u8],
    // half-byte cursor
    pos: usize,
}

impl<'a> NibbleReader<'a> {
    fn new(buf: &'a [u8]) -> Self {
        Self { buf, pos: 0 }
    }

    fn read(&mut self, n: u32) -> Result<u32> {
        debug_assert!(n == 4);
        if self.pos / 2 >= self.buf.len() {
            return Err(Error::invalid("vobsub: RLE bitstream ran out"));
        }
        let b = self.buf[self.pos / 2];
        let nibble = if self.pos % 2 == 0 { b >> 4 } else { b & 0x0F };
        self.pos += 1;
        Ok(nibble as u32)
    }

    fn align(&mut self) {
        if self.pos % 2 != 0 {
            self.pos += 1;
        }
    }
}

// --- container (.idx + .sub demuxer) -----------------------------------

/// Register the VobSub demuxer + extension mappings.
pub fn register_container(reg: &mut ContainerRegistry) {
    reg.register_demuxer("vobsub", open_vobsub);
    reg.register_extension("idx", "vobsub");
    reg.register_extension("sub", "vobsub");
    reg.register_probe("vobsub", probe_vobsub);
}

fn probe_vobsub(p: &ProbeData) -> ProbeScore {
    // .idx files start with "# VobSub index file" on idxsub's output or
    // the line "size:" early in the file. Combined with the extension
    // we score confidently.
    let s = std::str::from_utf8(p.buf).ok().unwrap_or("");
    let hit = s.contains("# VobSub index file")
        || s.contains("\nsize:")
        || s.starts_with("size:")
        || s.contains("\ntimestamp:");
    match (hit, p.ext) {
        (true, Some("idx")) => 100,
        (true, _) => 75,
        (false, Some("idx")) => 25,
        _ => 0,
    }
}

fn open_vobsub(
    mut input: Box<dyn ReadSeek>,
    _codecs: &dyn CodecResolver,
) -> Result<Box<dyn Demuxer>> {
    // We assume the input is the `.idx` file (text). We read it and then
    // look for the matching `.sub` alongside by filename. Since the
    // `ReadSeek` trait doesn't expose a path, the only source we can
    // consult is the content itself — if no companion file is resolvable
    // we fall back to constructing an empty SPU stream so the caller
    // gets at least the stream info and palette back.
    input.seek(SeekFrom::Start(0))?;
    let mut buf = Vec::new();
    input.read_to_end(&mut buf)?;
    let text = String::from_utf8_lossy(&buf).into_owned();
    let idx = parse_idx(&text)?;

    // Try to find the `.sub` alongside. We inspect the idx body for a
    // leading `# path: ...` comment some tools emit, and otherwise
    // produce a packet list from any embedded `# sub-data: <hex>` lines
    // (our own test fixtures use that convention). If none, the stream
    // carries no packets.
    let sub_path = find_sub_alongside(&text);
    let sub_bytes = match sub_path {
        Some(p) => std::fs::read(&p).ok().unwrap_or_default(),
        None => extract_inline_sub(&text).unwrap_or_default(),
    };

    let packets = build_packets(&idx, &sub_bytes);

    let (w, h) = idx.size;
    let mut params = CodecParameters::video(CodecId::new(VOBSUB_CODEC_ID));
    params.media_type = MediaType::Subtitle;
    params.width = Some(w as u32);
    params.height = Some(h as u32);
    params.pixel_format = Some(PixelFormat::Rgba);
    // Pack the 16-entry RGB palette into extradata so the decoder can
    // be built from stream info alone.
    let mut extra = Vec::with_capacity(48);
    for entry in &idx.palette_rgb {
        extra.extend_from_slice(entry);
    }
    params.extradata = extra;

    let total_us = packets.back().and_then(|p| p.pts).unwrap_or(0);
    let stream = StreamInfo {
        index: 0,
        time_base: TimeBase::new(1, 1_000_000),
        duration: Some(total_us),
        start_time: Some(0),
        params,
    };

    Ok(Box::new(VobSubDemuxer {
        streams: [stream],
        packets,
    }))
}

fn find_sub_alongside(idx_text: &str) -> Option<PathBuf> {
    // Look for our test convention: `# idx-path: /some/path.idx`.
    for line in idx_text.lines() {
        if let Some(path) = line.strip_prefix("# idx-path:") {
            let base = Path::new(path.trim()).with_extension("sub");
            return Some(base);
        }
    }
    None
}

fn extract_inline_sub(idx_text: &str) -> Option<Vec<u8>> {
    // `# sub-hex: <hex bytes>` — ours-only convention for in-tests.
    for line in idx_text.lines() {
        if let Some(rest) = line.strip_prefix("# sub-hex:") {
            return decode_hex(rest.trim());
        }
    }
    None
}

fn decode_hex(s: &str) -> Option<Vec<u8>> {
    let clean: String = s.chars().filter(|c| !c.is_whitespace()).collect();
    if clean.len() % 2 != 0 {
        return None;
    }
    let mut out = Vec::with_capacity(clean.len() / 2);
    for chunk in clean.as_bytes().chunks(2) {
        let s = std::str::from_utf8(chunk).ok()?;
        out.push(u8::from_str_radix(s, 16).ok()?);
    }
    Some(out)
}

fn build_packets(idx: &VobSubIdx, sub: &[u8]) -> VecDeque<Packet> {
    let tb = TimeBase::new(1, 1_000_000);
    let mut packets = VecDeque::new();
    for (i, (start_us, filepos)) in idx.cues.iter().enumerate() {
        let fp = *filepos as usize;
        if fp >= sub.len() {
            continue;
        }
        // Extract the SPU payload: drop the MPEG-PS pack/PES framing if
        // present (we accept either "raw SPU" or a minimal PES-wrapped
        // form). For the raw form the cue filepos points directly at
        // the SPU size u16.
        let spu = extract_spu(&sub[fp..]).unwrap_or_else(|| sub[fp..].to_vec());
        let mut pkt = Packet::new(0, tb, spu);
        pkt.pts = Some(*start_us);
        pkt.dts = Some(*start_us);
        pkt.flags.keyframe = true;
        if i + 1 < idx.cues.len() {
            let next = idx.cues[i + 1].0;
            pkt.duration = Some((next - *start_us).max(0));
        }
        packets.push_back(pkt);
    }
    packets
}

/// Extract an SPU blob from a buffer that may be either a raw SPU or a
/// MPEG-PS pack + PES private_stream_1 wrapper.
fn extract_spu(buf: &[u8]) -> Option<Vec<u8>> {
    // Raw SPU form: first 2 bytes = SPU length, and SPU length <= buf.len().
    if buf.len() >= 4 {
        let spu_len = u16::from_be_bytes([buf[0], buf[1]]) as usize;
        if spu_len >= 4 && spu_len <= buf.len() {
            return Some(buf[..spu_len].to_vec());
        }
    }
    // MPEG-PS pack + PES private_stream_1 form: walk the PS packets and
    // concatenate the PES payloads (dropping the 1-byte substream-id
    // prefix DVD uses inside private_stream_1) until we have enough to
    // hold a full SPU (its first 2 bytes give the total length).
    extract_spu_from_ps(buf)
}

/// Walk an MPEG-PS stream, concatenating DVD sub payloads from every
/// private_stream_1 PES packet until one full SPU has been recovered.
fn extract_spu_from_ps(buf: &[u8]) -> Option<Vec<u8>> {
    let mut cur = 0usize;
    let mut spu: Vec<u8> = Vec::new();
    let mut target: Option<usize> = None;

    while cur + 4 <= buf.len() {
        // All PS start codes share the 0x000001 prefix.
        if buf[cur] != 0 || buf[cur + 1] != 0 || buf[cur + 2] != 1 {
            return None;
        }
        let code = buf[cur + 3];
        cur += 4;
        match code {
            // Pack header (0xBA). MPEG-2 form: 10xx then 9 bytes, then a
            // 3-bit stuffing-length followed by that many stuffing bytes.
            // MPEG-1 form: starts with 0010xxxx and is 8 bytes total.
            0xBA => {
                if cur >= buf.len() {
                    return None;
                }
                if (buf[cur] & 0xC0) == 0x40 {
                    // MPEG-2: 9 fixed bytes + stuffing (low 3 bits of byte 9).
                    if cur + 10 > buf.len() {
                        return None;
                    }
                    let stuffing = (buf[cur + 9] & 0x07) as usize;
                    cur += 10 + stuffing;
                } else if (buf[cur] & 0xF0) == 0x20 {
                    // MPEG-1: 8 fixed bytes.
                    cur += 8;
                } else {
                    return None;
                }
            }
            // System header — 2-byte length followed by that many bytes.
            0xBB => {
                if cur + 2 > buf.len() {
                    return None;
                }
                let len = u16::from_be_bytes([buf[cur], buf[cur + 1]]) as usize;
                cur += 2 + len;
            }
            // Private stream 1 — DVD subs. Length + PES header + 1-byte
            // substream id, then the SPU payload fragment.
            0xBD => {
                if cur + 2 > buf.len() {
                    return None;
                }
                let pes_len = u16::from_be_bytes([buf[cur], buf[cur + 1]]) as usize;
                cur += 2;
                let pes_end = cur + pes_len;
                if pes_end > buf.len() {
                    return None;
                }
                let body = parse_pes_body(&buf[cur..pes_end])?;
                // DVD sub packets carry a 1-byte substream id at body[0];
                // valid ids are 0x20..0x3F.
                if body.is_empty() {
                    cur = pes_end;
                    continue;
                }
                let substream = body[0];
                if !(0x20..=0x3F).contains(&substream) {
                    cur = pes_end;
                    continue;
                }
                spu.extend_from_slice(&body[1..]);
                if target.is_none() && spu.len() >= 2 {
                    target = Some(u16::from_be_bytes([spu[0], spu[1]]) as usize);
                }
                if let Some(t) = target {
                    if spu.len() >= t {
                        spu.truncate(t);
                        return Some(spu);
                    }
                }
                cur = pes_end;
            }
            // Padding / other PES packets — just skip by length.
            0xBE | 0xBF => {
                if cur + 2 > buf.len() {
                    return None;
                }
                let len = u16::from_be_bytes([buf[cur], buf[cur + 1]]) as usize;
                cur += 2 + len;
            }
            // MPEG program end.
            0xB9 => break,
            _ => {
                if cur + 2 > buf.len() {
                    return None;
                }
                let len = u16::from_be_bytes([buf[cur], buf[cur + 1]]) as usize;
                cur += 2 + len;
            }
        }
    }
    None
}

/// Strip the PES header of an MPEG-1 or MPEG-2 PES packet body, returning
/// a slice starting at the payload byte (the DVD substream-id byte for
/// private_stream_1).
fn parse_pes_body(pes: &[u8]) -> Option<&[u8]> {
    if pes.is_empty() {
        return None;
    }
    // MPEG-2 PES: starts with 0b10xxxxxx. Header format:
    //   flags1 (1) | flags2 (1) | hdr_data_len (1) | hdr_data (...).
    if (pes[0] & 0xC0) == 0x80 {
        if pes.len() < 3 {
            return None;
        }
        let hdr_len = pes[2] as usize;
        let start = 3 + hdr_len;
        if start > pes.len() {
            return None;
        }
        return Some(&pes[start..]);
    }
    // MPEG-1 PES: leading stuffing (0xFF) up to 16 bytes, then optional
    // STD buffer size (2 bytes, first byte & 0xC0 == 0x40), then
    // optional PTS/DTS (flags 0x20/0x30/0x10 leading bits).
    let mut i = 0usize;
    while i < pes.len() && i < 16 && pes[i] == 0xFF {
        i += 1;
    }
    if i >= pes.len() {
        return None;
    }
    if (pes[i] & 0xC0) == 0x40 {
        i += 2;
    }
    if i >= pes.len() {
        return None;
    }
    let b = pes[i];
    if (b & 0xF0) == 0x20 {
        i += 5;
    } else if (b & 0xF0) == 0x30 {
        i += 10;
    } else if b == 0x0F {
        i += 1;
    }
    if i > pes.len() {
        return None;
    }
    Some(&pes[i..])
}

struct VobSubDemuxer {
    streams: [StreamInfo; 1],
    packets: VecDeque<Packet>,
}

impl Demuxer for VobSubDemuxer {
    fn format_name(&self) -> &str {
        "vobsub"
    }

    fn streams(&self) -> &[StreamInfo] {
        &self.streams
    }

    fn next_packet(&mut self) -> Result<Packet> {
        self.packets.pop_front().ok_or(Error::Eof)
    }

    fn duration_micros(&self) -> Option<i64> {
        self.streams[0].duration
    }
}

// --- decoder -----------------------------------------------------------

/// Build a VobSub decoder.
pub fn make_decoder(params: &CodecParameters) -> Result<Box<dyn Decoder>> {
    let mut palette = [[0u8; 3]; 16];
    if params.extradata.len() >= 48 {
        for (i, p) in palette.iter_mut().enumerate() {
            *p = [
                params.extradata[i * 3],
                params.extradata[i * 3 + 1],
                params.extradata[i * 3 + 2],
            ];
        }
    } else {
        // Fallback grayscale ramp so tests without a real idx still decode.
        for (i, p) in palette.iter_mut().enumerate() {
            let g = (i * 17) as u8;
            *p = [g, g, g];
        }
    }
    Ok(Box::new(VobSubDecoder {
        codec_id: CodecId::new(VOBSUB_CODEC_ID),
        palette,
        pending: VecDeque::new(),
        eof: false,
    }))
}

struct VobSubDecoder {
    codec_id: CodecId,
    palette: [[u8; 3]; 16],
    pending: VecDeque<Frame>,
    eof: bool,
}

impl Decoder for VobSubDecoder {
    fn codec_id(&self) -> &CodecId {
        &self.codec_id
    }

    fn send_packet(&mut self, packet: &Packet) -> Result<()> {
        let (spu, pixels, (w, h)) = parse_and_decode_spu(&packet.data)?;
        let mut canvas = vec![0u8; (w as usize) * (h as usize) * 4];
        for (i, &idx_4) in pixels.iter().enumerate() {
            let which = idx_4 as usize & 0x03;
            let pal_idx = spu.palette_sel[which] as usize & 0x0F;
            let alpha4 = spu.alpha[which] & 0x0F;
            let alpha = alpha4 * 17; // 0..15 → 0..255
            if alpha == 0 {
                continue;
            }
            let rgb = self.palette[pal_idx];
            let dst = i * 4;
            canvas[dst] = rgb[0];
            canvas[dst + 1] = rgb[1];
            canvas[dst + 2] = rgb[2];
            canvas[dst + 3] = alpha;
        }
        let frame = VideoFrame {
            pts: packet.pts,
            planes: vec![VideoPlane {
                stride: (w as usize) * 4,
                data: canvas,
            }],
        };
        self.pending.push_back(Frame::Video(frame));
        Ok(())
    }

    fn receive_frame(&mut self) -> Result<Frame> {
        if let Some(f) = self.pending.pop_front() {
            return Ok(f);
        }
        if self.eof {
            Err(Error::Eof)
        } else {
            Err(Error::NeedMore)
        }
    }

    fn flush(&mut self) -> Result<()> {
        self.eof = true;
        Ok(())
    }

    fn reset(&mut self) -> Result<()> {
        // Each VobSub SPU is self-contained. Drop the ready-frame queue
        // and clear the eof latch; the palette is stream-level config.
        self.pending.clear();
        self.eof = false;
        Ok(())
    }
}

// --- test helpers ------------------------------------------------------

/// Build a tiny SPU with the given width/height and per-pixel palette
/// indices (0..3). Each row has a run of `(w, colour)` ended by a
/// rest-of-line marker — callers whose `w` doesn't fit the 14-bit
/// representation should switch to separate runs.
#[doc(hidden)]
pub fn build_demo_spu(width: u16, height: u16, indices: &[u8]) -> Vec<u8> {
    assert_eq!(indices.len(), (width as usize) * (height as usize));
    fn push_rle_rows(
        out: &mut Vec<u8>,
        indices: &[u8],
        width: u16,
        _height: u16,
        field_rows: impl Iterator<Item = usize>,
    ) {
        let mut bits = NibbleWriter::new();
        for row in field_rows {
            let mut col = 0usize;
            while col < width as usize {
                // Find next run of identical indices.
                let colour = indices[row * width as usize + col];
                let mut run = 1usize;
                while col + run < width as usize
                    && indices[row * width as usize + col + run] == colour
                    && run < 0x3FFF
                {
                    run += 1;
                }
                // Prefer rest-of-line when possible.
                let rest = col + run == width as usize;
                emit_rle(&mut bits, if rest { 0 } else { run as u32 }, colour);
                col += run;
            }
            bits.align();
        }
        bits.finish(out);
    }

    fn emit_rle(w: &mut NibbleWriter, count: u32, colour: u8) {
        let c = (colour & 0x03) as u32;
        // If count is 0 → rest-of-line: encode with 4 leading zeros
        // nibble then 2 bits of colour packed into a nibble.
        if count == 0 {
            // 0000 0000 0000 CC -> 4 nibbles = 0 0 0 c
            w.write(4, 0);
            w.write(4, 0);
            w.write(4, 0);
            w.write(4, c);
            return;
        }
        if count < 4 {
            // 2-bit count + 2-bit colour in one nibble (cc[1:0]Cc[1:0])
            let nib = ((count & 0x3) << 2) | c;
            w.write(4, nib);
            return;
        }
        if count < 16 {
            // 4-bit count + 2-bit colour in two nibbles: 0 count colour
            let val = (count << 2) | c; // 6 bits
            w.write(4, (val >> 4) & 0xF);
            w.write(4, val & 0xF);
            return;
        }
        if count < 64 {
            // 6-bit count + 2-bit colour in two nibbles with leading zero prefix
            let val = (count << 2) | c; // 8 bits
            w.write(4, 0);
            w.write(4, (val >> 4) & 0xF);
            w.write(4, val & 0xF);
            return;
        }
        // 14-bit: count<<2 | c → 16 bits = four nibbles, with leading 0 prefix.
        let val = (count << 2) | c;
        w.write(4, 0);
        w.write(4, (val >> 12) & 0xF);
        w.write(4, (val >> 8) & 0xF);
        w.write(4, (val >> 4) & 0xF);
        w.write(4, val & 0xF);
    }

    struct NibbleWriter {
        nibbles: Vec<u8>,
    }
    impl NibbleWriter {
        fn new() -> Self {
            Self {
                nibbles: Vec::new(),
            }
        }
        fn write(&mut self, _bits: u32, value: u32) {
            self.nibbles.push((value & 0x0F) as u8);
        }
        fn align(&mut self) {
            if self.nibbles.len() % 2 != 0 {
                self.nibbles.push(0);
            }
        }
        fn finish(&self, out: &mut Vec<u8>) {
            for pair in self.nibbles.chunks(2) {
                let hi = pair[0];
                let lo = if pair.len() == 2 { pair[1] } else { 0 };
                out.push((hi << 4) | lo);
            }
        }
    }

    // Build top & bottom RLE byte blocks.
    let mut top_bytes = Vec::new();
    push_rle_rows(
        &mut top_bytes,
        indices,
        width,
        height,
        (0..height as usize).step_by(2),
    );
    let mut bot_bytes = Vec::new();
    push_rle_rows(
        &mut bot_bytes,
        indices,
        width,
        height,
        (1..height as usize).step_by(2),
    );

    // Layout:
    //   [0..2]  SPU length
    //   [2..4]  control offset
    //   [4..]   RLE data (top then bottom)
    //   control: delay=0, next=pos, commands 0x03 palette, 0x04 alpha,
    //     0x05 coords, 0x06 offsets, 0x01 start, 0xFF end.
    let top_off = 4usize;
    let bot_off = top_off + top_bytes.len();
    let ctrl_off = bot_off + bot_bytes.len();
    let mut out = Vec::new();
    out.extend_from_slice(&[0, 0]); // placeholder SPU length
    out.extend_from_slice(&(ctrl_off as u16).to_be_bytes());
    out.extend_from_slice(&top_bytes);
    out.extend_from_slice(&bot_bytes);

    // Single control sequence.
    let ctrl_pos = out.len();
    out.extend_from_slice(&[0, 0]); // delay = 0
                                    // next_offset placeholder — will point back to itself at end.
    out.extend_from_slice(&[0, 0]);
    out.push(0x03); // palette select
    out.push(0x01); // bg=0, pat=1
    out.push(0x32); // emp1=3, emp2=2
    out.push(0x04); // alpha
    out.push(0x00); // bg=0, pat=0xF (full)
    out.push(0xFF);
    // but we actually want pattern+emp alphas to 0xF. rewrite:
    let last = out.len() - 2;
    out[last] = 0x0F; // (bg<<4)|pattern → bg=0 pat=0xF
    out[last + 1] = 0xFF; // emp1=0xF, emp2=0xF
    out.push(0x05); // coords
                    // x1 = 0, x2 = width - 1
    out.push(0);
    out.push((((width - 1) >> 8) as u8) & 0x0F);
    out.push(((width - 1) & 0xFF) as u8);
    // y1 = 0, y2 = height - 1
    out.push(0);
    out.push((((height - 1) >> 8) as u8) & 0x0F);
    out.push(((height - 1) & 0xFF) as u8);
    out.push(0x06); // RLE offsets
    out.extend_from_slice(&(top_off as u16).to_be_bytes());
    out.extend_from_slice(&(bot_off as u16).to_be_bytes());
    out.push(0x01); // start display
    out.push(0xFF);

    // Patch next_offset in control sequence: point at itself to terminate.
    out[ctrl_pos + 2] = (ctrl_pos as u16 >> 8) as u8;
    out[ctrl_pos + 3] = (ctrl_pos as u16 & 0xFF) as u8;
    // Patch SPU length.
    let total = out.len() as u16;
    out[0] = (total >> 8) as u8;
    out[1] = (total & 0xFF) as u8;

    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_idx_basics() {
        let text = "\
# VobSub index file
size: 720x480
palette: ff0000, 00ff00, 0000ff, ffffff, 000000, 808080, c0c0c0, 404040, 200020, 800080, a0a0a0, 010203, 040506, 070809, 0a0b0c, 0d0e0f
timestamp: 00:00:01:500, filepos: 000000000
timestamp: 00:00:03:000, filepos: 000000040
";
        let idx = parse_idx(text).unwrap();
        assert_eq!(idx.size, (720, 480));
        assert!(idx.has_palette);
        assert_eq!(idx.palette_rgb[0], [0xff, 0x00, 0x00]);
        assert_eq!(idx.palette_rgb[1], [0x00, 0xff, 0x00]);
        assert_eq!(idx.cues.len(), 2);
        assert_eq!(idx.cues[0].0, 1_500_000);
        assert_eq!(idx.cues[1].0, 3_000_000);
    }

    #[test]
    fn decodes_small_spu() {
        // 2×2 bitmap filled with palette index 1 (pattern colour).
        let indices = [1u8, 1, 1, 1];
        let spu = build_demo_spu(2, 2, &indices);
        let (state, pixels, (w, h)) = parse_and_decode_spu(&spu).unwrap();
        assert_eq!(w, 2);
        assert_eq!(h, 2);
        assert_eq!(pixels, vec![1u8, 1, 1, 1]);
        assert_eq!(state.palette_sel[1], 1);

        let mut params = CodecParameters::video(CodecId::new(VOBSUB_CODEC_ID));
        // 16-colour palette: entry 1 is red.
        let mut extra = vec![0u8; 48];
        extra[3] = 255;
        params.extradata = extra;
        let mut dec = make_decoder(&params).unwrap();
        let pkt = Packet::new(0, TimeBase::new(1, 1_000_000), spu).with_pts(0);
        dec.send_packet(&pkt).unwrap();
        let frame = dec.receive_frame().unwrap();
        let Frame::Video(v) = frame else {
            panic!("expected video frame");
        };
        assert_eq!(v.planes[0].stride, 2 * 4);
        assert_eq!(v.planes[0].data.len(), 2 * 2 * 4);
        let data = &v.planes[0].data;
        // All pixels red + alpha 255.
        for px in data.chunks(4) {
            assert_eq!(px, &[255, 0, 0, 255], "pixel not red: {:?}", px);
        }
    }

    #[test]
    fn extracts_spu_from_raw() {
        let raw = build_demo_spu(2, 2, &[1u8, 1, 1, 1]);
        let out = extract_spu(&raw).unwrap();
        assert_eq!(out, raw);
    }

    #[test]
    fn extracts_spu_from_mpeg_ps_wrap() {
        // Build one SPU, wrap it into a minimal MPEG-2 PS with pack +
        // one private_stream_1 PES packet carrying the DVD substream id.
        let spu = build_demo_spu(2, 2, &[1u8, 1, 1, 1]);
        let mut ps = Vec::new();
        // Pack header (MPEG-2): 0x000001BA, 10 bytes incl. 0 stuffing.
        ps.extend_from_slice(&[0, 0, 1, 0xBA]);
        // byte 0 starts with 01 → MPEG-2 marker
        ps.extend_from_slice(&[0x44, 0x00, 0x04, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0xF8]);
        // Private stream 1 PES: start code + 2-byte length + PES hdr +
        // substream id (0x20) + SPU payload.
        let substream = 0x20u8;
        let pes_payload_len = 3 + 1 + spu.len(); // flags(2) + hdr_len(1) + substream + spu
        ps.extend_from_slice(&[0, 0, 1, 0xBD]);
        ps.extend_from_slice(&(pes_payload_len as u16).to_be_bytes());
        // Minimal MPEG-2 PES header: 0x80, 0x00, 0x00 (no PTS/DTS, 0 hdr data).
        ps.extend_from_slice(&[0x80, 0x00, 0x00]);
        ps.push(substream);
        ps.extend_from_slice(&spu);

        let out = extract_spu(&ps).unwrap();
        assert_eq!(out, spu);
    }
}