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
//! Parsed PGS segment payloads.
//!
//! Each segment type (PCS, WDS, PDS, ODS) has a corresponding struct with a
//! `parse(&[u8]) -> Option<Self>` constructor that decodes the raw payload bytes.
//! Returns `None` if the payload is truncated or malformed.

use crate::pgs::segment::CompositionState;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn u16_be(data: &[u8], offset: usize) -> u16 {
    u16::from_be_bytes([data[offset], data[offset + 1]])
}

fn u24_be(data: &[u8], offset: usize) -> u32 {
    ((data[offset] as u32) << 16) | ((data[offset + 1] as u32) << 8) | (data[offset + 2] as u32)
}

fn push_u16_be(buf: &mut Vec<u8>, val: u16) {
    buf.extend_from_slice(&val.to_be_bytes());
}

fn push_u24_be(buf: &mut Vec<u8>, val: u32) {
    buf.push((val >> 16) as u8);
    buf.push((val >> 8) as u8);
    buf.push(val as u8);
}

// ---------------------------------------------------------------------------
// PCS — Presentation Composition Segment (0x16)
// ---------------------------------------------------------------------------

/// Parsed Presentation Composition Segment payload.
#[derive(Debug, Clone)]
pub struct PcsData {
    pub video_width: u16,
    pub video_height: u16,
    pub composition_number: u16,
    pub composition_state: CompositionState,
    pub palette_only: bool,
    pub palette_id: u8,
    pub objects: Vec<CompositionObject>,
}

/// A placement instruction within a PCS: draw object X in window Y at (x, y).
#[derive(Debug, Clone)]
pub struct CompositionObject {
    pub object_id: u16,
    pub window_id: u8,
    pub x: u16,
    pub y: u16,
    pub crop: Option<CropInfo>,
}

/// Cropping rectangle for a composition object.
#[derive(Debug, Clone)]
pub struct CropInfo {
    pub x: u16,
    pub y: u16,
    pub width: u16,
    pub height: u16,
}

impl PcsData {
    /// Parse a PCS payload. Returns `None` if truncated or malformed.
    pub fn parse(payload: &[u8]) -> Option<Self> {
        if payload.len() < 11 {
            return None;
        }

        let video_width = u16_be(payload, 0);
        let video_height = u16_be(payload, 2);
        // Byte 4: frame rate (always 0x10, ignored)
        let composition_number = u16_be(payload, 5);
        let composition_state = CompositionState::from_byte(payload[7])?;
        let palette_only = payload[8] == 0x80;
        let palette_id = payload[9];
        let num_objects = payload[10] as usize;

        let mut objects = Vec::with_capacity(num_objects);
        let mut offset = 11;

        for _ in 0..num_objects {
            if offset + 8 > payload.len() {
                return None;
            }

            let object_id = u16_be(payload, offset);
            let window_id = payload[offset + 2];
            let cropped = payload[offset + 3] & 0x80 != 0;
            let x = u16_be(payload, offset + 4);
            let y = u16_be(payload, offset + 6);
            offset += 8;

            let crop = if cropped {
                if offset + 8 > payload.len() {
                    return None;
                }
                let crop = CropInfo {
                    x: u16_be(payload, offset),
                    y: u16_be(payload, offset + 2),
                    width: u16_be(payload, offset + 4),
                    height: u16_be(payload, offset + 6),
                };
                offset += 8;
                Some(crop)
            } else {
                None
            };

            objects.push(CompositionObject {
                object_id,
                window_id,
                x,
                y,
                crop,
            });
        }

        Some(PcsData {
            video_width,
            video_height,
            composition_number,
            composition_state,
            palette_only,
            palette_id,
            objects,
        })
    }

    /// Serialize this PCS payload to bytes (reverse of `parse`).
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        push_u16_be(&mut buf, self.video_width);
        push_u16_be(&mut buf, self.video_height);
        buf.push(0x10); // frame rate (constant)
        push_u16_be(&mut buf, self.composition_number);
        buf.push(self.composition_state.to_byte());
        buf.push(if self.palette_only { 0x80 } else { 0x00 });
        buf.push(self.palette_id);
        buf.push(self.objects.len() as u8);

        for obj in &self.objects {
            push_u16_be(&mut buf, obj.object_id);
            buf.push(obj.window_id);
            buf.push(if obj.crop.is_some() { 0x80 } else { 0x00 });
            push_u16_be(&mut buf, obj.x);
            push_u16_be(&mut buf, obj.y);
            if let Some(crop) = &obj.crop {
                push_u16_be(&mut buf, crop.x);
                push_u16_be(&mut buf, crop.y);
                push_u16_be(&mut buf, crop.width);
                push_u16_be(&mut buf, crop.height);
            }
        }

        buf
    }
}

// ---------------------------------------------------------------------------
// WDS — Window Definition Segment (0x17)
// ---------------------------------------------------------------------------

/// Parsed Window Definition Segment payload.
#[derive(Debug, Clone)]
pub struct WdsData {
    pub windows: Vec<WindowDefinition>,
}

/// A rectangular screen region where objects are drawn.
#[derive(Debug, Clone)]
pub struct WindowDefinition {
    pub id: u8,
    pub x: u16,
    pub y: u16,
    pub width: u16,
    pub height: u16,
}

impl WdsData {
    /// Parse a WDS payload. Returns `None` if truncated.
    pub fn parse(payload: &[u8]) -> Option<Self> {
        if payload.is_empty() {
            return None;
        }

        let num_windows = payload[0] as usize;
        let mut windows = Vec::with_capacity(num_windows);
        let mut offset = 1;

        for _ in 0..num_windows {
            if offset + 9 > payload.len() {
                return None;
            }

            windows.push(WindowDefinition {
                id: payload[offset],
                x: u16_be(payload, offset + 1),
                y: u16_be(payload, offset + 3),
                width: u16_be(payload, offset + 5),
                height: u16_be(payload, offset + 7),
            });
            offset += 9;
        }

        Some(WdsData { windows })
    }

    /// Serialize this WDS payload to bytes (reverse of `parse`).
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        buf.push(self.windows.len() as u8);
        for w in &self.windows {
            buf.push(w.id);
            push_u16_be(&mut buf, w.x);
            push_u16_be(&mut buf, w.y);
            push_u16_be(&mut buf, w.width);
            push_u16_be(&mut buf, w.height);
        }
        buf
    }
}

// ---------------------------------------------------------------------------
// PDS — Palette Definition Segment (0x14)
// ---------------------------------------------------------------------------

/// Parsed Palette Definition Segment payload.
#[derive(Debug, Clone)]
pub struct PdsData {
    pub id: u8,
    pub version: u8,
    pub entries: Vec<PaletteEntry>,
}

/// A single palette color entry (YCrCb + alpha).
#[derive(Debug, Clone)]
pub struct PaletteEntry {
    pub id: u8,
    pub luminance: u8,
    pub cr: u8,
    pub cb: u8,
    pub alpha: u8,
}

impl PdsData {
    /// Parse a PDS payload. Returns `None` if truncated.
    pub fn parse(payload: &[u8]) -> Option<Self> {
        if payload.len() < 2 {
            return None;
        }

        let id = payload[0];
        let version = payload[1];

        let remaining = &payload[2..];
        if !remaining.len().is_multiple_of(5) {
            return None;
        }

        let num_entries = remaining.len() / 5;
        let mut entries = Vec::with_capacity(num_entries);

        for i in 0..num_entries {
            let base = i * 5;
            entries.push(PaletteEntry {
                id: remaining[base],
                luminance: remaining[base + 1],
                cr: remaining[base + 2],
                cb: remaining[base + 3],
                alpha: remaining[base + 4],
            });
        }

        Some(PdsData {
            id,
            version,
            entries,
        })
    }

    /// Serialize this PDS payload to bytes (reverse of `parse`).
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(2 + self.entries.len() * 5);
        buf.push(self.id);
        buf.push(self.version);
        for e in &self.entries {
            buf.push(e.id);
            buf.push(e.luminance);
            buf.push(e.cr);
            buf.push(e.cb);
            buf.push(e.alpha);
        }
        buf
    }
}

// ---------------------------------------------------------------------------
// ODS — Object Definition Segment (0x15)
// ---------------------------------------------------------------------------

/// Fragment position within a (possibly multi-segment) object.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SequenceFlag {
    /// Complete object in a single segment (0xC0).
    Complete,
    /// First fragment (0x80).
    First,
    /// Last fragment (0x40).
    Last,
    /// Middle fragment (0x00).
    Continuation,
}

impl SequenceFlag {
    pub fn from_byte(b: u8) -> Option<Self> {
        match b {
            0xC0 => Some(SequenceFlag::Complete),
            0x80 => Some(SequenceFlag::First),
            0x40 => Some(SequenceFlag::Last),
            0x00 => Some(SequenceFlag::Continuation),
            _ => None,
        }
    }

    pub fn to_byte(self) -> u8 {
        match self {
            SequenceFlag::Complete => 0xC0,
            SequenceFlag::First => 0x80,
            SequenceFlag::Last => 0x40,
            SequenceFlag::Continuation => 0x00,
        }
    }

    /// Returns the JSON string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            SequenceFlag::Complete => "complete",
            SequenceFlag::First => "first",
            SequenceFlag::Last => "last",
            SequenceFlag::Continuation => "continuation",
        }
    }
}

/// Parsed Object Definition Segment payload.
#[derive(Debug, Clone)]
pub struct OdsData {
    pub id: u16,
    pub version: u8,
    pub sequence: SequenceFlag,
    /// Total object data length (u24), includes 4 bytes for width+height.
    pub data_length: u32,
    /// Image width — present only on `Complete` or `First` fragments.
    pub width: Option<u16>,
    /// Image height — present only on `Complete` or `First` fragments.
    pub height: Option<u16>,
    /// Raw RLE bitmap bytes (after the ODS header).
    pub rle_data: Vec<u8>,
}

impl OdsData {
    /// Parse an ODS payload. Returns `None` if truncated or malformed.
    pub fn parse(payload: &[u8]) -> Option<Self> {
        if payload.len() < 4 {
            return None;
        }

        let id = u16_be(payload, 0);
        let version = payload[2];
        let sequence = SequenceFlag::from_byte(payload[3])?;

        let is_first = matches!(sequence, SequenceFlag::Complete | SequenceFlag::First);

        // Only First/Complete fragments have data_length + width + height after
        // the 4-byte header. Continuation/Last fragments go straight to RLE data.
        let (data_length, width, height, rle_offset) = if is_first {
            if payload.len() < 11 {
                return None;
            }
            (
                u24_be(payload, 4),
                Some(u16_be(payload, 7)),
                Some(u16_be(payload, 9)),
                11,
            )
        } else {
            (0, None, None, 4)
        };

        let rle_data = payload[rle_offset..].to_vec();

        Some(OdsData {
            id,
            version,
            sequence,
            data_length,
            width,
            height,
            rle_data,
        })
    }

    /// Serialize this ODS payload to bytes (reverse of `parse`).
    pub fn to_bytes(&self) -> Vec<u8> {
        let is_first = matches!(self.sequence, SequenceFlag::Complete | SequenceFlag::First);
        let mut buf = Vec::new();
        push_u16_be(&mut buf, self.id);
        buf.push(self.version);
        buf.push(self.sequence.to_byte());

        if is_first {
            let data_length = self.rle_data.len() as u32 + 4; // +4 for width+height
            push_u24_be(&mut buf, data_length);
            push_u16_be(&mut buf, self.width.unwrap_or(0));
            push_u16_be(&mut buf, self.height.unwrap_or(0));
        }

        buf.extend_from_slice(&self.rle_data);
        buf
    }
}

// ---------------------------------------------------------------------------
// ODS RLE data extraction
// ---------------------------------------------------------------------------

/// Extract the RLE data bytes from an ODS segment payload.
///
/// For Complete/First segments, RLE starts at byte 11 (after 4-byte header + 3-byte data_length + 4-byte dimensions).
/// For Continuation/Last segments, RLE starts at byte 4 (after 4-byte header only — no data_length or dimensions).
///
/// Returns `None` if the payload is too short.
pub fn ods_rle_data(payload: &[u8], sequence: SequenceFlag) -> Option<&[u8]> {
    let offset = match sequence {
        SequenceFlag::Complete | SequenceFlag::First => 11,
        SequenceFlag::Continuation | SequenceFlag::Last => 4,
    };
    if payload.len() < offset {
        return None;
    }
    Some(&payload[offset..])
}

// ---------------------------------------------------------------------------
// ParsedPayload — dispatch enum
// ---------------------------------------------------------------------------

/// A parsed segment payload, dispatched by segment type.
#[derive(Debug, Clone)]
pub enum ParsedPayload {
    Pcs(PcsData),
    Wds(WdsData),
    Pds(PdsData),
    Ods(OdsData),
    End,
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // -- PCS tests --

    #[test]
    fn test_pcs_no_objects() {
        let payload = vec![
            0x07, 0x80, // width: 1920
            0x04, 0x38, // height: 1080
            0x10, // frame rate
            0x00, 0x01, // composition number: 1
            0x80, // composition state: Epoch Start
            0x00, // palette update: false
            0x00, // palette id: 0
            0x00, // num objects: 0
        ];
        let pcs = PcsData::parse(&payload).unwrap();
        assert_eq!(pcs.video_width, 1920);
        assert_eq!(pcs.video_height, 1080);
        assert_eq!(pcs.composition_number, 1);
        assert_eq!(pcs.composition_state, CompositionState::EpochStart);
        assert!(!pcs.palette_only);
        assert_eq!(pcs.palette_id, 0);
        assert!(pcs.objects.is_empty());
    }

    #[test]
    fn test_pcs_one_uncropped_object() {
        let payload = vec![
            0x07, 0x80, // width: 1920
            0x04, 0x38, // height: 1080
            0x10, // frame rate
            0x01, 0xAE, // composition number: 430
            0x80, // composition state: Epoch Start
            0x00, // palette update: false
            0x00, // palette id
            0x01, // num objects: 1
            // Composition object:
            0x00, 0x00, // object_id: 0
            0x00, // window_id: 0
            0x00, // cropped: false
            0x03, 0x05, // x: 773
            0x00, 0x6C, // y: 108
        ];
        let pcs = PcsData::parse(&payload).unwrap();
        assert_eq!(pcs.objects.len(), 1);
        let obj = &pcs.objects[0];
        assert_eq!(obj.object_id, 0);
        assert_eq!(obj.window_id, 0);
        assert_eq!(obj.x, 773);
        assert_eq!(obj.y, 108);
        assert!(obj.crop.is_none());
    }

    #[test]
    fn test_pcs_cropped_object() {
        let payload = vec![
            0x07, 0x80, 0x04, 0x38, 0x10, // width, height, framerate
            0x00, 0x01, // composition number
            0x00, // Normal
            0x00, 0x00, // palette update, palette id
            0x01, // num objects: 1
            // Composition object:
            0x00, 0x01, // object_id: 1
            0x00, // window_id: 0
            0x80, // cropped: true
            0x00, 0x64, // x: 100
            0x00, 0xC8, // y: 200
            // Crop info:
            0x00, 0x0A, // crop x: 10
            0x00, 0x14, // crop y: 20
            0x00, 0x50, // crop width: 80
            0x00, 0x28, // crop height: 40
        ];
        let pcs = PcsData::parse(&payload).unwrap();
        let obj = &pcs.objects[0];
        assert!(obj.crop.is_some());
        let crop = obj.crop.as_ref().unwrap();
        assert_eq!(crop.x, 10);
        assert_eq!(crop.y, 20);
        assert_eq!(crop.width, 80);
        assert_eq!(crop.height, 40);
    }

    #[test]
    fn test_pcs_palette_only_update() {
        let payload = vec![
            0x07, 0x80, 0x04, 0x38, 0x10, 0x00, 0x02, 0x00, // Normal state
            0x80, // palette update: true
            0x03, // palette id: 3
            0x00, // num objects: 0
        ];
        let pcs = PcsData::parse(&payload).unwrap();
        assert!(pcs.palette_only);
        assert_eq!(pcs.palette_id, 3);
    }

    #[test]
    fn test_pcs_truncated() {
        assert!(PcsData::parse(&[0x07, 0x80]).is_none());
        // Truncated: says 1 object but no object data
        let payload = vec![
            0x07, 0x80, 0x04, 0x38, 0x10, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01,
        ];
        assert!(PcsData::parse(&payload).is_none());
    }

    // -- WDS tests --

    #[test]
    fn test_wds_two_windows() {
        let payload = vec![
            0x02, // 2 windows
            // Window 0:
            0x00, // id: 0
            0x03, 0x05, // x: 773
            0x00, 0x6C, // y: 108
            0x01, 0x79, // width: 377
            0x00, 0x2B, // height: 43
            // Window 1:
            0x01, // id: 1
            0x02, 0xE3, // x: 739
            0x03, 0xA0, // y: 928
            0x01, 0xD8, // width: 472
            0x00, 0x2B, // height: 43
        ];
        let wds = WdsData::parse(&payload).unwrap();
        assert_eq!(wds.windows.len(), 2);
        assert_eq!(wds.windows[0].id, 0);
        assert_eq!(wds.windows[0].x, 773);
        assert_eq!(wds.windows[0].width, 377);
        assert_eq!(wds.windows[1].id, 1);
        assert_eq!(wds.windows[1].x, 739);
        assert_eq!(wds.windows[1].width, 472);
    }

    #[test]
    fn test_wds_truncated() {
        assert!(WdsData::parse(&[]).is_none());
        // Says 1 window but only 5 bytes of data
        assert!(WdsData::parse(&[0x01, 0x00, 0x00, 0x00, 0x00, 0x00]).is_none());
    }

    // -- PDS tests --

    #[test]
    fn test_pds_multiple_entries() {
        let payload = vec![
            0x00, // palette id: 0
            0x00, // version: 0
            // Entry 0:
            0x00, 0x10, 0x80, 0x80, 0x00, // id=0, Y=16, Cr=128, Cb=128, A=0
            // Entry 1:
            0x01, 0x10, 0x80, 0x80, 0xFF, // id=1, Y=16, Cr=128, Cb=128, A=255
            // Entry 255:
            0xFF, 0xEB, 0x80, 0x80, 0xFF, // id=255, Y=235, Cr=128, Cb=128, A=255
        ];
        let pds = PdsData::parse(&payload).unwrap();
        assert_eq!(pds.id, 0);
        assert_eq!(pds.version, 0);
        assert_eq!(pds.entries.len(), 3);
        assert_eq!(pds.entries[0].id, 0);
        assert_eq!(pds.entries[0].luminance, 16);
        assert_eq!(pds.entries[0].alpha, 0);
        assert_eq!(pds.entries[2].id, 255);
        assert_eq!(pds.entries[2].luminance, 235);
    }

    #[test]
    fn test_pds_empty_palette() {
        let payload = vec![0x00, 0x00]; // id=0, version=0, no entries
        let pds = PdsData::parse(&payload).unwrap();
        assert!(pds.entries.is_empty());
    }

    #[test]
    fn test_pds_truncated() {
        assert!(PdsData::parse(&[0x00]).is_none());
        // Incomplete entry (3 bytes instead of 5)
        assert!(PdsData::parse(&[0x00, 0x00, 0x01, 0x10, 0x80]).is_none());
    }

    // -- ODS tests --

    #[test]
    fn test_ods_complete() {
        let payload = vec![
            0x00, 0x00, // object id: 0
            0x00, // version: 0
            0xC0, // sequence: first and last (complete)
            0x00, 0x21, 0xBB, // data_length: 8635
            0x01, 0x79, // width: 377
            0x00, 0x2B, // height: 43
            // RLE data would follow...
            0x00, 0x01, 0x02,
        ];
        let ods = OdsData::parse(&payload).unwrap();
        assert_eq!(ods.id, 0);
        assert_eq!(ods.version, 0);
        assert_eq!(ods.sequence, SequenceFlag::Complete);
        assert_eq!(ods.data_length, 8635);
        assert_eq!(ods.width, Some(377));
        assert_eq!(ods.height, Some(43));
    }

    #[test]
    fn test_ods_continuation_fragment() {
        // Continuation fragments have only the 4-byte header (no data_length/width/height).
        let payload = vec![
            0x00, 0x00, // object id: 0
            0x01, // version: 1
            0x00, // sequence: continuation
            // RLE data follows immediately:
            0xAA, 0xBB,
        ];
        let ods = OdsData::parse(&payload).unwrap();
        assert_eq!(ods.sequence, SequenceFlag::Continuation);
        assert_eq!(ods.data_length, 0);
        assert!(ods.width.is_none());
        assert!(ods.height.is_none());
    }

    #[test]
    fn test_ods_last_fragment() {
        // Last fragments have only the 4-byte header (no data_length/width/height).
        let payload = vec![
            0x00, 0x01, // object id: 1
            0x00, // version: 0
            0x40, // sequence: last
            // RLE data follows immediately:
            0xCC, 0xDD,
        ];
        let ods = OdsData::parse(&payload).unwrap();
        assert_eq!(ods.sequence, SequenceFlag::Last);
        assert_eq!(ods.data_length, 0);
        assert!(ods.width.is_none());
    }

    #[test]
    fn test_ods_first_fragment() {
        let payload = vec![
            0x00, 0x00, // object id: 0
            0x00, // version
            0x80, // sequence: first
            0x00, 0x40, 0x00, // data_length
            0x01, 0x79, // width: 377
            0x00, 0x2B, // height: 43
        ];
        let ods = OdsData::parse(&payload).unwrap();
        assert_eq!(ods.sequence, SequenceFlag::First);
        assert_eq!(ods.width, Some(377));
        assert_eq!(ods.height, Some(43));
    }

    #[test]
    fn test_ods_truncated() {
        // Less than 4-byte header.
        assert!(OdsData::parse(&[0x00, 0x00, 0x00]).is_none());
        // First/complete fragment but missing width/height (only 7 bytes, need 11).
        let payload = vec![0x00, 0x00, 0x00, 0xC0, 0x00, 0x10, 0x00];
        assert!(OdsData::parse(&payload).is_none());
    }

    #[test]
    fn test_ods_invalid_sequence_flag() {
        let payload = vec![
            0x00, 0x00, 0x00, 0x20, // invalid sequence flag
            0x00, 0x10, 0x00,
        ];
        assert!(OdsData::parse(&payload).is_none());
    }

    // -- SequenceFlag tests --

    #[test]
    fn test_sequence_flag_as_str() {
        assert_eq!(SequenceFlag::Complete.as_str(), "complete");
        assert_eq!(SequenceFlag::First.as_str(), "first");
        assert_eq!(SequenceFlag::Last.as_str(), "last");
        assert_eq!(SequenceFlag::Continuation.as_str(), "continuation");
    }

    // -- Round-trip tests (to_bytes -> parse) --

    #[test]
    fn test_pcs_roundtrip_no_objects() {
        let payload = vec![
            0x07, 0x80, 0x04, 0x38, 0x10,
            0x00, 0x01, 0x80, 0x00, 0x00, 0x00,
        ];
        let pcs = PcsData::parse(&payload).unwrap();
        assert_eq!(pcs.to_bytes(), payload);
    }

    #[test]
    fn test_pcs_roundtrip_with_object() {
        let payload = vec![
            0x07, 0x80, 0x04, 0x38, 0x10,
            0x01, 0xAE, 0x80, 0x00, 0x00, 0x01,
            0x00, 0x00, 0x00, 0x00, 0x03, 0x05, 0x00, 0x6C,
        ];
        let pcs = PcsData::parse(&payload).unwrap();
        assert_eq!(pcs.to_bytes(), payload);
    }

    #[test]
    fn test_pcs_roundtrip_cropped() {
        let payload = vec![
            0x07, 0x80, 0x04, 0x38, 0x10,
            0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
            0x00, 0x01, 0x00, 0x80, 0x00, 0x64, 0x00, 0xC8,
            0x00, 0x0A, 0x00, 0x14, 0x00, 0x50, 0x00, 0x28,
        ];
        let pcs = PcsData::parse(&payload).unwrap();
        assert_eq!(pcs.to_bytes(), payload);
    }

    #[test]
    fn test_wds_roundtrip() {
        let payload = vec![
            0x02,
            0x00, 0x03, 0x05, 0x00, 0x6C, 0x01, 0x79, 0x00, 0x2B,
            0x01, 0x02, 0xE3, 0x03, 0xA0, 0x01, 0xD8, 0x00, 0x2B,
        ];
        let wds = WdsData::parse(&payload).unwrap();
        assert_eq!(wds.to_bytes(), payload);
    }

    #[test]
    fn test_pds_roundtrip() {
        let payload = vec![
            0x00, 0x00,
            0x00, 0x10, 0x80, 0x80, 0x00,
            0x01, 0x10, 0x80, 0x80, 0xFF,
            0xFF, 0xEB, 0x80, 0x80, 0xFF,
        ];
        let pds = PdsData::parse(&payload).unwrap();
        assert_eq!(pds.to_bytes(), payload);
    }

    #[test]
    fn test_ods_roundtrip_complete() {
        let payload = vec![
            0x00, 0x00, 0x00, 0xC0,
            0x00, 0x00, 0x07, // data_length: 3 + 4 = 7
            0x01, 0x79, 0x00, 0x2B,
            0x00, 0x01, 0x02,
        ];
        let ods = OdsData::parse(&payload).unwrap();
        assert_eq!(ods.rle_data, vec![0x00, 0x01, 0x02]);
        assert_eq!(ods.to_bytes(), payload);
    }

    #[test]
    fn test_ods_roundtrip_continuation() {
        let payload = vec![
            0x00, 0x00, 0x01, 0x00,
            0xAA, 0xBB,
        ];
        let ods = OdsData::parse(&payload).unwrap();
        assert_eq!(ods.rle_data, vec![0xAA, 0xBB]);
        assert_eq!(ods.to_bytes(), payload);
    }

    #[test]
    fn test_ods_roundtrip_last() {
        let payload = vec![
            0x00, 0x01, 0x00, 0x40,
            0xCC, 0xDD,
        ];
        let ods = OdsData::parse(&payload).unwrap();
        assert_eq!(ods.to_bytes(), payload);
    }

    #[test]
    fn test_sequence_flag_roundtrip() {
        for flag in [SequenceFlag::Complete, SequenceFlag::First, SequenceFlag::Last, SequenceFlag::Continuation] {
            assert_eq!(SequenceFlag::from_byte(flag.to_byte()), Some(flag));
        }
    }

    #[test]
    fn test_composition_state_roundtrip() {
        for state in [CompositionState::Normal, CompositionState::AcquisitionPoint, CompositionState::EpochStart] {
            assert_eq!(CompositionState::from_byte(state.to_byte()), Some(state));
        }
    }
}