libmpegts 0.3.2

MPEG-TS Library
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
mod data;

use libmpegts::{
    psi::*,
    slicer::TsSlicer,
    ts::{
        PACKET_SIZE,
        TsPacketRef,
    },
    utils::crc32b,
};

fn psi_check_crc32(data: &[u8]) -> bool {
    let crc32_calculated = crc32b(&data[.. data.len() - 4]);
    let crc32_expected = u32::from_be_bytes([
        data[data.len() - 4],
        data[data.len() - 3],
        data[data.len() - 2],
        data[data.len() - 1],
    ]);
    crc32_calculated == crc32_expected
}

#[test]
fn test_no_psi() {
    let ts_no_payload = TsPacketRef::from(&[
        0x47, 0x00, 0x00, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    ]);

    let mut psi = Psi::default();
    assert!(psi.assemble(&ts_no_payload).is_none());
    assert!(psi.payload().is_none());
}

#[test]
fn test_invalid_pointer_field() {
    let ts_invalid = TsPacketRef::from(&[
        0x47, 0x40, 0x00, 0x10, 200, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    ]);

    let mut psi = Psi::default();
    assert!(psi.assemble(&ts_invalid).is_none());
    assert!(psi.payload().is_none());
}

#[test]
fn test_one_ts_one_psi() {
    // PSI section begins after the pointer field
    let mut pat = Psi::default();
    TsSlicer::new().slice(data::PAT).for_each(|p| {
        pat.assemble(&p);
    });
    let payload = pat.payload().expect("PAT section expected");
    assert_eq!(payload[0], 0x00); // table_id
    assert_eq!(payload.len(), 40); // section length
    assert!(psi_check_crc32(payload));
}

#[test]
fn test_two_ts_one_psi() {
    // PSI section spans multiple TS packets
    // Second TS packet without payload start indicator
    let mut sdt = Psi::default();
    TsSlicer::new().slice(data::SDT).for_each(|p| {
        sdt.assemble(&p);
    });
    let payload = sdt.payload().expect("SDT section expected");
    assert_eq!(payload[0], 0x42); // table_id
    assert_eq!(payload.len(), 216); // section length
    assert!(psi_check_crc32(payload));
}

#[test]
fn test_two_ts_two_psi() {
    // First PSI in first and second TS packets
    // Second PSI in second TS packet only
    // Second TS packet with payload start indicator
    let first_ts = TsPacketRef::from(&[
        0x47, 0x40, 0x14, 0x1e, 0x03, 0xff, 0xff, 0xff, 0x71, 0x70, 0xb4, 0xe3, 0xc5, 0x22, 0x16,
        0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
    ]);

    let second_ts = TsPacketRef::from(&[
        0x47, 0x40, 0x14, 0x1f, 0x03, 0x17, 0x4a, 0x1d, 0x72, 0x70, 0x09, 0xe3, 0xc5, 0x22, 0x16,
        0x00, 0x23, 0xdb, 0xf3, 0xac, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, //
    ]);

    let mut psi = Psi::default();

    assert!(psi.assemble(&first_ts).is_none());
    assert!(psi.payload().is_none());

    assert!(psi.assemble(&second_ts).is_some());
    let first_psi = psi.payload().expect("first PSI section expected");
    assert_eq!(first_psi[0], 0x71); // table_id
    assert_eq!(first_psi.len(), 183); // section length
    assert!(psi_check_crc32(first_psi));

    assert!(psi.assemble(&second_ts).is_some());
    let second_psi = psi.payload().expect("second PSI section expected");
    assert_eq!(second_psi[0], 0x72);
    assert_eq!(second_psi.len(), 12); // section length
    assert!(psi_check_crc32(second_psi));
}

#[test]
fn test_psi_assemble_small_psi() {
    // First TS contains only two bytes of PSI after pointer field
    let first_ts = TsPacketRef::from(&[
        0x47, 0x40, 0x14, 0x1e, 0xb5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xb0,
    ]);

    let second_ts = TsPacketRef::from(&[
        0x47, 0x00, 0x14, 0x1f, 0x25, 0x00, 0x01, 0xc3, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x10, 0x00,
        0x01, 0xe4, 0x07, 0x00, 0x02, 0xe4, 0x08, 0x00, 0x03, 0xe4, 0x09, 0x00, 0x04, 0xe4, 0x0a,
        0x00, 0x05, 0xe4, 0x0b, 0x00, 0x06, 0xe4, 0x0c, 0xb2, 0xd1, 0xb8, 0xde, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, //
    ]);

    let mut psi = Psi::default();
    assert!(psi.assemble(&first_ts).is_none());
    assert!(psi.assemble(&second_ts).is_some());

    let payload = psi.payload().expect("PSI section expected");
    assert_eq!(payload[0], 0x00); // table_id
    assert_eq!(payload.len(), 40); // section length
    assert!(psi_check_crc32(payload));
}

#[test]
fn test_packetizer_single_packet() {
    // Small PAT: 7 programs = 8 + (7 * 4) + 4 = 40 bytes, fits in one TS packet
    let sections = PatBuilder::build(PatConfig {
        transport_stream_id: 1,
        version: 1,
        programs: vec![
            PatProgram {
                program_number: 0,
                pid: 16,
            },
            PatProgram {
                program_number: 1,
                pid: 1031,
            },
            PatProgram {
                program_number: 2,
                pid: 1032,
            },
            PatProgram {
                program_number: 3,
                pid: 1033,
            },
            PatProgram {
                program_number: 4,
                pid: 1034,
            },
            PatProgram {
                program_number: 5,
                pid: 1035,
            },
            PatProgram {
                program_number: 6,
                pid: 1036,
            },
        ],
    });

    let section_data = sections[0].to_vec();
    assert_eq!(section_data.len(), 40);

    let mut packetizer = PsiPacketizer::new(0);
    packetizer.set_sections(sections);

    let mut packet = [0u8; PACKET_SIZE];

    assert!(!packetizer.is_empty());
    assert!(packetizer.next(&mut packet));
    assert!(packetizer.is_empty());
    assert!(!packetizer.next(&mut packet));

    // Verify TS header
    assert_eq!(packet[0], 0x47); // sync
    let ts = TsPacketRef::from(&packet);
    assert!(ts.is_payload_start()); // PUSI
    assert_eq!(ts.pid(), 0x0000); // PAT PID
    assert_eq!(ts.cc(), 0);

    // Verify pointer_field + section data
    assert_eq!(packet[4], 0x00); // pointer_field
    assert_eq!(&packet[5 .. 5 + 40], &section_data[..]);

    // Verify stuffing bytes
    assert!(packet[5 + 40 ..].iter().all(|&b| b == 0xFF));
}

#[test]
fn test_packetizer_multi_packet() {
    // Large PAT: 44 programs = 8 + (44 * 4) + 4 = 188 bytes
    // First packet: 183 bytes, second packet: 5 bytes
    let sections = PatBuilder::build(PatConfig {
        transport_stream_id: 1,
        version: 0,
        programs: (0 .. 44)
            .map(|i| PatProgram {
                program_number: i,
                pid: 100 + i,
            })
            .collect(),
    });
    assert_eq!(sections.len(), 1);

    let section_data = sections[0].to_vec();
    assert_eq!(section_data.len(), 188);

    let mut packetizer = PsiPacketizer::new(0);
    packetizer.set_sections(sections);

    let mut p1 = [0u8; PACKET_SIZE];
    let mut p2 = [0u8; PACKET_SIZE];

    assert!(packetizer.next(&mut p1));
    assert!(!packetizer.is_empty());
    assert!(packetizer.next(&mut p2));
    assert!(packetizer.is_empty());
    assert!(!packetizer.next(&mut [0u8; PACKET_SIZE]));

    // First packet: PUSI, CC=0
    let ts1 = TsPacketRef::from(&p1);
    assert!(ts1.is_payload_start());
    assert_eq!(ts1.cc(), 0);
    assert_eq!(p1[4], 0x00); // pointer_field
    assert_eq!(&p1[5 ..], &section_data[.. 183]);

    // Second packet: no PUSI, CC=1
    let ts2 = TsPacketRef::from(&p2);
    assert!(!ts2.is_payload_start());
    assert_eq!(ts2.cc(), 1);
    assert_eq!(&p2[4 .. 4 + 5], &section_data[183 ..]);
    assert!(p2[4 + 5 ..].iter().all(|&b| b == 0xFF));
}

#[test]
fn test_packetizer_preserves_cc() {
    let sections = PatBuilder::build(PatConfig {
        transport_stream_id: 1,
        version: 0,
        programs: vec![PatProgram {
            program_number: 1,
            pid: 100,
        }],
    });

    let mut packetizer = PsiPacketizer::new(0);
    packetizer.set_sections(sections);

    let mut packet = [0u8; PACKET_SIZE];

    // First send cycle: CC=0
    assert!(packetizer.next(&mut packet));
    assert_eq!(TsPacketRef::from(&packet).cc(), 0);
    assert!(packetizer.is_empty());

    // Reset and send again: CC=1
    packetizer.reset();
    assert!(!packetizer.is_empty());
    assert!(packetizer.next(&mut packet));
    assert_eq!(TsPacketRef::from(&packet).cc(), 1);

    // Replace sections - CC continues from 2
    let sections = PatBuilder::build(PatConfig {
        transport_stream_id: 1,
        version: 1,
        programs: vec![PatProgram {
            program_number: 1,
            pid: 200,
        }],
    });
    packetizer.set_sections(sections);

    packetizer.next(&mut packet);
    assert_eq!(TsPacketRef::from(&packet).cc(), 2);
}

#[test]
fn test_packetizer_roundtrip() {
    let sections = PatBuilder::build(PatConfig {
        transport_stream_id: 42,
        version: 0,
        programs: (0 .. 44)
            .map(|i| PatProgram {
                program_number: i,
                pid: 200 + i,
            })
            .collect(),
    });

    let original = sections[0].to_vec();
    let mut packetizer = PsiPacketizer::new(0);
    packetizer.set_sections(sections);

    let mut psi = Psi::default();
    let mut packet = [0u8; PACKET_SIZE];

    while packetizer.next(&mut packet) {
        let ts = TsPacketRef::from(&packet);
        psi.assemble(&ts);
    }

    let payload = psi.payload().expect("Reassembled PAT section");
    assert_eq!(payload, &original[..]);
    assert!(psi_check_crc32(payload));

    let pat = PatSectionRef::try_from(payload).expect("Valid PAT");
    assert_eq!(pat.transport_stream_id(), 42);
    assert_eq!(pat.programs().count(), 44);
}

#[test]
fn test_packetizer_multiple_sections() {
    // Build PAT with enough programs to auto-split into 2 sections
    // Max programs per section: (1024 - 8 - 4) / 4 = 253
    let sections = PatBuilder::build(PatConfig {
        transport_stream_id: 1,
        version: 0,
        programs: (0 .. 254)
            .map(|i| PatProgram {
                program_number: i,
                pid: 100 + i,
            })
            .collect(),
    });
    assert_eq!(sections.len(), 2);

    let s0 = sections[0].to_vec();
    let s1 = sections[1].to_vec();

    let mut packetizer = PsiPacketizer::new(0);
    packetizer.set_sections(sections);

    let mut packets = Vec::new();
    loop {
        let mut packet = [0u8; PACKET_SIZE];
        if !packetizer.next(&mut packet) {
            break;
        }
        packets.push(packet);
    }

    assert!(packetizer.is_empty());

    // Verify CC is continuous across all packets
    for (i, pkt) in packets.iter().enumerate() {
        let ts = TsPacketRef::from(pkt);
        assert_eq!(ts.cc(), (i as u8) & 0x0F);
    }

    // Verify first packet of each section has PUSI
    let ts_first = TsPacketRef::from(&packets[0]);
    assert!(ts_first.is_payload_start());

    // Find start of second section - it's the first packet after section 0 is done
    let s0_remaining = s0.len() - 183;
    let s0_total_packets = 1 + (s0_remaining + 183) / 184;

    let ts_second_start = TsPacketRef::from(&packets[s0_total_packets]);
    assert!(ts_second_start.is_payload_start());

    // Verify continuation packets don't have PUSI
    if s0_total_packets > 1 {
        let ts_cont = TsPacketRef::from(&packets[1]);
        assert!(!ts_cont.is_payload_start());
    }

    // Verify sections via PatSectionRef
    PatSectionRef::try_from(&s0[..]).expect("Valid first section");
    PatSectionRef::try_from(&s1[..]).expect("Valid second section");
}