cartouche 0.2.0

Encoding and decoding for HDMI InfoFrames.
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
use display_types::ColorFormat;

use crate::decoded::Decoded;
use crate::encode::{IntoPackets, SinglePacketIter};
use crate::error::DecodeError;
use crate::warn::AviWarning;

/// Bar data presence flags (B field, PB1 bits 3–2).
///
/// Indicates which bar data fields in PB6–PB13 carry valid measurements.
/// Fields not indicated as present contain undefined bytes on the wire and
/// should not be interpreted.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BarInfo {
    /// No bar data present (B = 0b00).
    NotPresent,
    /// Left and right bar pixel numbers are valid (B = 0b01).
    VerticalBarsPresent,
    /// Top and bottom bar line numbers are valid (B = 0b10).
    HorizontalBarsPresent,
    /// All four bar measurements are valid (B = 0b11).
    BothPresent,
}

/// Scan information (S field, PB1 bits 1–0).
///
/// Describes how the source expects the sink to handle overscan.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ScanInfo {
    /// No scan information available (S = 0).
    NoData,
    /// Content is composed for a display that overscans (S = 1).
    Overscanned,
    /// Content is composed for a display that underscans (S = 2).
    Underscanned,
}

/// Primary colorimetry (C field, PB2 bits 7–6).
///
/// When set to [`Extended`](Colorimetry::Extended), the
/// [`extended_colorimetry`](AviInfoFrame::extended_colorimetry) field carries the
/// precise colorimetry value.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Colorimetry {
    /// No colorimetry data (C = 0b00).
    NoData,
    /// SMPTE 170M / ITU-R BT.601 (C = 0b01).
    Bt601,
    /// ITU-R BT.709 (C = 0b10).
    Bt709,
    /// Extended colorimetry — refer to the EC field (C = 0b11).
    Extended,
}

/// Extended colorimetry (EC field, PB3 bits 6–4).
///
/// Only meaningful when [`colorimetry`](AviInfoFrame::colorimetry) is
/// [`Colorimetry::Extended`].
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ExtendedColorimetry {
    /// xvYCC 601 (EC = 0).
    XvYCC601,
    /// xvYCC 709 (EC = 1).
    XvYCC709,
    /// sYCC 601 (EC = 2).
    SyCC601,
    /// opYCC 601 (EC = 3).
    OpYCC601,
    /// opRGB (EC = 4).
    OpRgb,
    /// BT.2020 constant luminance YCbCr (EC = 5).
    Bt2020cYCC,
    /// BT.2020 non-constant luminance YCbCr (EC = 6).
    Bt2020YCC,
    /// Additional Colorimetry Extension present (EC = 7).
    ///
    /// Defined in CTA-861-H for AVI InfoFrame version 3 and later.
    /// Version 2 (HDMI 2.1) sinks may not recognise this value.
    AdditionalColorimetryExtension,
}

/// Picture aspect ratio (M field, PB2 bits 5–4).
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PictureAspectRatio {
    /// No aspect ratio data (M = 0b00).
    NoData,
    /// 4:3 picture aspect ratio (M = 0b01).
    FourByThree,
    /// 16:9 picture aspect ratio (M = 0b10).
    SixteenByNine,
}

/// RGB quantization range (Q field, PB3 bits 3–2).
///
/// Only meaningful when [`color_format`](AviInfoFrame::color_format) is
/// [`ColorFormat::Rgb444`].
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RgbQuantization {
    /// Default quantization range for the video format (Q = 0b00).
    Default,
    /// Limited range: levels 16–235 for Y, 16–240 for Cb/Cr (Q = 0b01).
    LimitedRange,
    /// Full range: levels 0–255 (Q = 0b10).
    FullRange,
}

/// YCC quantization range (YQ field, PB5 bits 7–6).
///
/// Only meaningful when [`color_format`](AviInfoFrame::color_format) is a
/// YCbCr variant.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum YccQuantization {
    /// Limited YCC quantization range (YQ = 0b00).
    LimitedRange,
    /// Full YCC quantization range (YQ = 0b01).
    FullRange,
}

/// Non-uniform picture scaling (SC field, PB3 bits 1–0).
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum NonUniformScaling {
    /// No known non-uniform scaling (SC = 0b00).
    None,
    /// Picture has been scaled horizontally (SC = 0b01).
    Horizontal,
    /// Picture has been scaled vertically (SC = 0b10).
    Vertical,
    /// Picture has been scaled both horizontally and vertically (SC = 0b11).
    Both,
}

/// IT content type (CN field, PB5 bits 5–4).
///
/// Only meaningful when [`it_content`](AviInfoFrame::it_content) is `true`.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ItContentType {
    /// Graphics content (CN = 0b00).
    Graphics,
    /// Photo content (CN = 0b01).
    Photo,
    /// Cinema content (CN = 0b10).
    Cinema,
    /// Game content (CN = 0b11).
    Game,
}

/// An AVI InfoFrame (CTA-861, type code 0x82).
///
/// Carries the color space, colorimetry, quantization range, aspect ratio,
/// active format, VIC, pixel repetition count, and optional bar data.
/// Required for the sink to configure its display pipeline correctly.
///
/// # Bit layout reference (CTA-861-H §6.4)
///
/// - PB1: `color_format`\[7:5\], `active_format_present`\[4\], `bar_info`\[3:2\], `scan_info`\[1:0\]
/// - PB2: `colorimetry`\[7:6\], `picture_aspect_ratio`\[5:4\], `active_format_aspect_ratio`\[3:0\]
/// - PB3: `it_content`\[7\], `extended_colorimetry`\[6:4\], `rgb_quantization`\[3:2\],
///   `non_uniform_scaling`\[1:0\]
/// - PB4: reserved\[7\], `vic`\[6:0\]
/// - PB5: `ycc_quantization`\[7:6\], `it_content_type`\[5:4\], `pixel_repetition`\[3:0\]
/// - PB6–PB7: `top_bar` (little-endian)
/// - PB8–PB9: `bottom_bar` (little-endian)
/// - PB10–PB11: `left_bar` (little-endian)
/// - PB12–PB13: `right_bar` (little-endian)
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AviInfoFrame {
    // ---- PB1 ----
    /// Video color format (Y\[2:0\]).
    pub color_format: ColorFormat,
    /// Active format description is present in [`active_format_aspect_ratio`](Self::active_format_aspect_ratio).
    pub active_format_present: bool,
    /// Which bar measurements (if any) are valid.
    pub bar_info: BarInfo,
    /// Overscan/underscan indication.
    pub scan_info: ScanInfo,

    // ---- PB2 ----
    /// Primary colorimetry standard.
    pub colorimetry: Colorimetry,
    /// Extended colorimetry standard; valid only when `colorimetry` is [`Colorimetry::Extended`].
    pub extended_colorimetry: ExtendedColorimetry,
    /// Coded picture aspect ratio.
    pub picture_aspect_ratio: PictureAspectRatio,
    /// Active Format Description (AFD) code, R\[3:0\]. See CTA-861 §6.4.
    ///
    /// Only meaningful when [`active_format_present`](Self::active_format_present) is `true`.
    pub active_format_aspect_ratio: u8,

    // ---- PB3 ----
    /// IT (Information Technology) content flag.
    pub it_content: bool,
    /// RGB quantization range; valid only when `color_format` is [`ColorFormat::Rgb444`].
    pub rgb_quantization: RgbQuantization,
    /// Non-uniform picture scaling applied by the source.
    pub non_uniform_scaling: NonUniformScaling,

    // ---- PB4 ----
    /// Video Identification Code (VIC), 0–127.
    pub vic: u8,

    // ---- PB5 ----
    /// YCC quantization range; valid only when `color_format` is a YCbCr variant.
    pub ycc_quantization: YccQuantization,
    /// IT content type; valid only when [`it_content`](Self::it_content) is `true`.
    pub it_content_type: ItContentType,
    /// Pixel repetition factor. `0` = no repetition (sent once), `1` = sent twice, …, `9` = sent ten times.
    pub pixel_repetition: u8,

    // ---- PB6–PB13: bar data ----
    /// End of top bar (line number). Valid only when `bar_info` indicates horizontal bars.
    pub top_bar: u16,
    /// Start of bottom bar (line number). Valid only when `bar_info` indicates horizontal bars.
    pub bottom_bar: u16,
    /// End of left bar (pixel number). Valid only when `bar_info` indicates vertical bars.
    pub left_bar: u16,
    /// Start of right bar (pixel number). Valid only when `bar_info` indicates vertical bars.
    pub right_bar: u16,
}

impl AviInfoFrame {
    /// Decode an AVI InfoFrame from a 31-byte wire packet.
    ///
    /// # Errors
    ///
    /// Returns [`DecodeError::Truncated`] if `packet[2] > 27`.
    ///
    /// # Warnings
    ///
    /// The returned [`Decoded`] may carry any of:
    /// - [`AviWarning::ChecksumMismatch`]
    /// - [`AviWarning::ReservedFieldNonZero`]
    /// - [`AviWarning::UnknownEnumValue`]
    pub fn decode(packet: &[u8; 31]) -> Result<Decoded<AviInfoFrame, AviWarning>, DecodeError> {
        let length = packet[2];
        if length > 27 {
            return Err(DecodeError::Truncated { claimed: length });
        }

        let mut decoded = Decoded::new(AviInfoFrame {
            color_format: ColorFormat::Rgb444,
            active_format_present: false,
            bar_info: BarInfo::NotPresent,
            scan_info: ScanInfo::NoData,
            colorimetry: Colorimetry::NoData,
            extended_colorimetry: ExtendedColorimetry::XvYCC601,
            picture_aspect_ratio: PictureAspectRatio::NoData,
            active_format_aspect_ratio: 0,
            it_content: false,
            rgb_quantization: RgbQuantization::Default,
            non_uniform_scaling: NonUniformScaling::None,
            vic: 0,
            ycc_quantization: YccQuantization::LimitedRange,
            it_content_type: ItContentType::Graphics,
            pixel_repetition: 0,
            top_bar: 0,
            bottom_bar: 0,
            left_bar: 0,
            right_bar: 0,
        });

        // Checksum.
        let total: u8 = packet.iter().fold(0u8, |acc, &b| acc.wrapping_add(b));
        if total != 0x00 {
            let expected = crate::checksum::compute_checksum(packet[..30].try_into().unwrap());
            decoded.push_warning(AviWarning::ChecksumMismatch {
                expected,
                found: packet[3],
            });
        }

        // PB1 (byte 4): Y2[7]|Y1[6]|Y0[5]|A0[4]|B1[3]|B0[2]|S1[1]|S0[0]
        let pb1 = packet[4];
        let y_raw = (pb1 >> 5) & 0x07;
        decoded.value.color_format = match y_raw {
            0 => ColorFormat::Rgb444,
            1 => ColorFormat::YCbCr422,
            2 => ColorFormat::YCbCr444,
            3 => ColorFormat::YCbCr420,
            _ => {
                decoded.push_warning(AviWarning::UnknownEnumValue {
                    field: "color_format",
                    raw: y_raw,
                });
                ColorFormat::Rgb444
            }
        };
        decoded.value.active_format_present = pb1 & 0x10 != 0;
        decoded.value.bar_info = match (pb1 >> 2) & 0x03 {
            0 => BarInfo::NotPresent,
            1 => BarInfo::VerticalBarsPresent,
            2 => BarInfo::HorizontalBarsPresent,
            _ => BarInfo::BothPresent,
        };
        let s_raw = pb1 & 0x03;
        decoded.value.scan_info = match s_raw {
            0 => ScanInfo::NoData,
            1 => ScanInfo::Overscanned,
            2 => ScanInfo::Underscanned,
            _ => {
                decoded.push_warning(AviWarning::UnknownEnumValue {
                    field: "scan_info",
                    raw: s_raw,
                });
                ScanInfo::NoData
            }
        };

        // PB2 (byte 5): C1[7]|C0[6]|M1[5]|M0[4]|R3[3]|R2[2]|R1[1]|R0[0]
        let pb2 = packet[5];
        decoded.value.colorimetry = match (pb2 >> 6) & 0x03 {
            0 => Colorimetry::NoData,
            1 => Colorimetry::Bt601,
            2 => Colorimetry::Bt709,
            _ => Colorimetry::Extended,
        };
        let m_raw = (pb2 >> 4) & 0x03;
        decoded.value.picture_aspect_ratio = match m_raw {
            0 => PictureAspectRatio::NoData,
            1 => PictureAspectRatio::FourByThree,
            2 => PictureAspectRatio::SixteenByNine,
            _ => {
                decoded.push_warning(AviWarning::UnknownEnumValue {
                    field: "picture_aspect_ratio",
                    raw: m_raw,
                });
                PictureAspectRatio::NoData
            }
        };
        decoded.value.active_format_aspect_ratio = pb2 & 0x0F;

        // PB3 (byte 6): IT[7]|EC2[6]|EC1[5]|EC0[4]|Q1[3]|Q0[2]|SC1[1]|SC0[0]
        let pb3 = packet[6];
        decoded.value.it_content = pb3 & 0x80 != 0;
        decoded.value.extended_colorimetry = match (pb3 >> 4) & 0x07 {
            0 => ExtendedColorimetry::XvYCC601,
            1 => ExtendedColorimetry::XvYCC709,
            2 => ExtendedColorimetry::SyCC601,
            3 => ExtendedColorimetry::OpYCC601,
            4 => ExtendedColorimetry::OpRgb,
            5 => ExtendedColorimetry::Bt2020cYCC,
            6 => ExtendedColorimetry::Bt2020YCC,
            _ => ExtendedColorimetry::AdditionalColorimetryExtension,
        };
        let q_raw = (pb3 >> 2) & 0x03;
        decoded.value.rgb_quantization = match q_raw {
            0 => RgbQuantization::Default,
            1 => RgbQuantization::LimitedRange,
            2 => RgbQuantization::FullRange,
            _ => {
                decoded.push_warning(AviWarning::UnknownEnumValue {
                    field: "rgb_quantization",
                    raw: q_raw,
                });
                RgbQuantization::Default
            }
        };
        decoded.value.non_uniform_scaling = match pb3 & 0x03 {
            0 => NonUniformScaling::None,
            1 => NonUniformScaling::Horizontal,
            2 => NonUniformScaling::Vertical,
            _ => NonUniformScaling::Both,
        };

        // PB4 (byte 7): rsvd[7]|VIC[6:0]
        let pb4 = packet[7];
        if pb4 & 0x80 != 0 {
            decoded.push_warning(AviWarning::ReservedFieldNonZero { byte: 7, bit: 7 });
        }
        decoded.value.vic = pb4 & 0x7F;

        // PB5 (byte 8): YQ1[7]|YQ0[6]|CN1[5]|CN0[4]|PR3[3]|PR2[2]|PR1[1]|PR0[0]
        let pb5 = packet[8];
        let yq_raw = (pb5 >> 6) & 0x03;
        decoded.value.ycc_quantization = match yq_raw {
            0 => YccQuantization::LimitedRange,
            1 => YccQuantization::FullRange,
            _ => {
                decoded.push_warning(AviWarning::UnknownEnumValue {
                    field: "ycc_quantization",
                    raw: yq_raw,
                });
                YccQuantization::LimitedRange
            }
        };
        decoded.value.it_content_type = match (pb5 >> 4) & 0x03 {
            0 => ItContentType::Graphics,
            1 => ItContentType::Photo,
            2 => ItContentType::Cinema,
            _ => ItContentType::Game,
        };
        decoded.value.pixel_repetition = pb5 & 0x0F;

        // PB6–PB13 (bytes 9–16): bar data.
        // Only present when length is sufficient; left at the zero default otherwise.
        if length >= 7 {
            decoded.value.top_bar = u16::from_le_bytes([packet[9], packet[10]]);
        }
        if length >= 9 {
            decoded.value.bottom_bar = u16::from_le_bytes([packet[11], packet[12]]);
        }
        if length >= 11 {
            decoded.value.left_bar = u16::from_le_bytes([packet[13], packet[14]]);
        }
        if length >= 13 {
            decoded.value.right_bar = u16::from_le_bytes([packet[15], packet[16]]);
        }

        Ok(decoded)
    }
}

impl IntoPackets for AviInfoFrame {
    type Iter = SinglePacketIter;
    type Warning = AviWarning;

    fn into_packets(self) -> crate::decoded::Decoded<SinglePacketIter, AviWarning> {
        let vic = self.vic;
        let mut hp = [0u8; 30];
        hp[0] = 0x82; // type code (AVI)
        hp[1] = 0x02; // version
        hp[2] = 13; // length: PB1–PB13

        // PB1: Y2[7]|Y1[6]|Y0[5]|A0[4]|B1[3]|B0[2]|S1[1]|S0[0]
        let y_raw: u8 = match self.color_format {
            ColorFormat::Rgb444 => 0,
            ColorFormat::YCbCr422 => 1,
            ColorFormat::YCbCr444 => 2,
            ColorFormat::YCbCr420 => 3,
            // ColorFormat is #[non_exhaustive] from display-types; unknown future
            // variants fall back to Rgb444 (Y=0), the safest default per CTA-861.
            _ => 0,
        };
        let b_raw: u8 = match self.bar_info {
            BarInfo::NotPresent => 0,
            BarInfo::VerticalBarsPresent => 1,
            BarInfo::HorizontalBarsPresent => 2,
            BarInfo::BothPresent => 3,
        };
        let s_raw: u8 = match self.scan_info {
            ScanInfo::NoData => 0,
            ScanInfo::Overscanned => 1,
            ScanInfo::Underscanned => 2,
        };
        hp[3] = (y_raw << 5) | ((self.active_format_present as u8) << 4) | (b_raw << 2) | s_raw;

        // PB2: C1[7]|C0[6]|M1[5]|M0[4]|R3[3]|R2[2]|R1[1]|R0[0]
        let c_raw: u8 = match self.colorimetry {
            Colorimetry::NoData => 0,
            Colorimetry::Bt601 => 1,
            Colorimetry::Bt709 => 2,
            Colorimetry::Extended => 3,
        };
        let m_raw: u8 = match self.picture_aspect_ratio {
            PictureAspectRatio::NoData => 0,
            PictureAspectRatio::FourByThree => 1,
            PictureAspectRatio::SixteenByNine => 2,
        };
        hp[4] = (c_raw << 6) | (m_raw << 4) | (self.active_format_aspect_ratio & 0x0F);

        // PB3: IT[7]|EC2[6]|EC1[5]|EC0[4]|Q1[3]|Q0[2]|SC1[1]|SC0[0]
        let ec_raw: u8 = match self.extended_colorimetry {
            ExtendedColorimetry::XvYCC601 => 0,
            ExtendedColorimetry::XvYCC709 => 1,
            ExtendedColorimetry::SyCC601 => 2,
            ExtendedColorimetry::OpYCC601 => 3,
            ExtendedColorimetry::OpRgb => 4,
            ExtendedColorimetry::Bt2020cYCC => 5,
            ExtendedColorimetry::Bt2020YCC => 6,
            ExtendedColorimetry::AdditionalColorimetryExtension => 7,
        };
        let q_raw: u8 = match self.rgb_quantization {
            RgbQuantization::Default => 0,
            RgbQuantization::LimitedRange => 1,
            RgbQuantization::FullRange => 2,
        };
        let sc_raw: u8 = match self.non_uniform_scaling {
            NonUniformScaling::None => 0,
            NonUniformScaling::Horizontal => 1,
            NonUniformScaling::Vertical => 2,
            NonUniformScaling::Both => 3,
        };
        hp[5] = ((self.it_content as u8) << 7) | (ec_raw << 4) | (q_raw << 2) | sc_raw;

        // PB4: rsvd[7]|VIC[6:0]
        hp[6] = self.vic & 0x7F;

        // PB5: YQ1[7]|YQ0[6]|CN1[5]|CN0[4]|PR3[3]|PR2[2]|PR1[1]|PR0[0]
        let yq_raw: u8 = match self.ycc_quantization {
            YccQuantization::LimitedRange => 0,
            YccQuantization::FullRange => 1,
        };
        let cn_raw: u8 = match self.it_content_type {
            ItContentType::Graphics => 0,
            ItContentType::Photo => 1,
            ItContentType::Cinema => 2,
            ItContentType::Game => 3,
        };
        hp[7] = (yq_raw << 6) | (cn_raw << 4) | (self.pixel_repetition & 0x0F);

        // PB6–PB13: bar data (little-endian u16 pairs).
        let [lo, hi] = self.top_bar.to_le_bytes();
        hp[8] = lo;
        hp[9] = hi;
        let [lo, hi] = self.bottom_bar.to_le_bytes();
        hp[10] = lo;
        hp[11] = hi;
        let [lo, hi] = self.left_bar.to_le_bytes();
        hp[12] = lo;
        hp[13] = hi;
        let [lo, hi] = self.right_bar.to_le_bytes();
        hp[14] = lo;
        hp[15] = hi;

        let checksum = crate::checksum::compute_checksum(&hp);

        let mut packet = [0u8; 31];
        packet[..3].copy_from_slice(&hp[..3]);
        packet[3] = checksum;
        packet[4..].copy_from_slice(&hp[3..]);

        let mut result = crate::decoded::Decoded::new(SinglePacketIter::new(packet));
        if vic > 127 {
            result.push_warning(AviWarning::UnknownEnumValue {
                field: "vic",
                raw: vic,
            });
        }
        result
    }
}

#[cfg(test)]
#[path = "avi_tests.rs"]
mod tests;