1use crate::error::{Error, Result};
40use dvb_common::{Parse, Serialize};
41
42pub const TABLE_ID: u8 = 0x78;
44
45pub const PID: u16 = 0x0000;
48
49const HEADER_LEN: usize = 3;
51
52const EXTENSION_HEADER_LEN: usize = 5;
55
56const RTP_LEN: usize = 4;
58
59const CRC_LEN: usize = 4;
61
62const MIN_LEN: usize = HEADER_LEN + EXTENSION_HEADER_LEN + RTP_LEN + CRC_LEN;
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize))]
71pub struct RealTimeParameters {
72 pub delta_t: u16,
75 pub table_boundary: bool,
77 pub frame_boundary: bool,
79 pub address: u32,
82}
83
84impl RealTimeParameters {
85 fn from_bytes(b: [u8; RTP_LEN]) -> Self {
87 let delta_t = ((b[0] as u16) << 4) | ((b[1] >> 4) as u16);
89 let table_boundary = (b[1] & 0x08) != 0;
90 let frame_boundary = (b[1] & 0x04) != 0;
91 let address = (((b[1] & 0x03) as u32) << 16) | ((b[2] as u32) << 8) | (b[3] as u32);
93 RealTimeParameters {
94 delta_t,
95 table_boundary,
96 frame_boundary,
97 address,
98 }
99 }
100
101 fn to_bytes(self) -> [u8; RTP_LEN] {
103 let dt = self.delta_t & 0x0FFF;
104 let addr = self.address & 0x0003_FFFF;
105 [
106 (dt >> 4) as u8,
107 (((dt & 0x0F) as u8) << 4)
108 | (u8::from(self.table_boundary) << 3)
109 | (u8::from(self.frame_boundary) << 2)
110 | ((addr >> 16) as u8 & 0x03),
111 ((addr >> 8) & 0xFF) as u8,
112 (addr & 0xFF) as u8,
113 ]
114 }
115}
116
117#[derive(Debug, Clone, PartialEq, Eq)]
119#[cfg_attr(feature = "serde", derive(serde::Serialize))]
120#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
121pub struct MpeFecSection<'a> {
122 pub private_indicator: bool,
124 pub padding_columns: u8,
126 pub current_next_indicator: bool,
128 pub section_number: u8,
130 pub last_section_number: u8,
132 pub real_time_parameters: RealTimeParameters,
134 pub rs_data: &'a [u8],
137}
138
139impl<'a> Parse<'a> for MpeFecSection<'a> {
140 type Error = crate::error::Error;
141
142 fn parse(bytes: &'a [u8]) -> Result<Self> {
143 if bytes.len() < MIN_LEN {
144 return Err(Error::BufferTooShort {
145 need: MIN_LEN,
146 have: bytes.len(),
147 what: "MpeFecSection",
148 });
149 }
150
151 if bytes[0] != TABLE_ID {
152 return Err(Error::UnexpectedTableId {
153 table_id: bytes[0],
154 what: "MpeFecSection",
155 expected: &[TABLE_ID],
156 });
157 }
158
159 let section_length = (((bytes[1] & 0x0F) as usize) << 8) | bytes[2] as usize;
160 let total = super::check_section_length(bytes.len(), HEADER_LEN, section_length, MIN_LEN)?;
161
162 let private_indicator = (bytes[1] & 0x40) != 0;
163 let padding_columns = bytes[3];
164 let current_next_indicator = (bytes[5] & 0x01) != 0;
167 let section_number = bytes[6];
168 let last_section_number = bytes[7];
169
170 let rtp_start = HEADER_LEN + EXTENSION_HEADER_LEN;
171 let real_time_parameters = RealTimeParameters::from_bytes([
172 bytes[rtp_start],
173 bytes[rtp_start + 1],
174 bytes[rtp_start + 2],
175 bytes[rtp_start + 3],
176 ]);
177
178 let data_start = rtp_start + RTP_LEN;
179 let data_end = total - CRC_LEN;
180 let rs_data = &bytes[data_start..data_end];
181
182 Ok(MpeFecSection {
183 private_indicator,
184 padding_columns,
185 current_next_indicator,
186 section_number,
187 last_section_number,
188 real_time_parameters,
189 rs_data,
190 })
191 }
192}
193
194impl Serialize for MpeFecSection<'_> {
195 type Error = crate::error::Error;
196
197 fn serialized_len(&self) -> usize {
198 HEADER_LEN + EXTENSION_HEADER_LEN + RTP_LEN + self.rs_data.len() + CRC_LEN
199 }
200
201 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
202 let len = self.serialized_len();
203 if buf.len() < len {
204 return Err(Error::OutputBufferTooSmall {
205 need: len,
206 have: buf.len(),
207 });
208 }
209
210 let section_length = (len - HEADER_LEN) as u16;
211 if section_length > 0x0FFF {
212 return Err(Error::SectionLengthOverflow {
213 declared: section_length as usize,
214 available: 0x0FFF,
215 });
216 }
217
218 buf[0] = TABLE_ID;
220 buf[1] = 0x80
223 | (u8::from(self.private_indicator) << 6)
224 | 0x30
225 | ((section_length >> 8) as u8 & 0x0F);
226 buf[2] = (section_length & 0xFF) as u8;
227
228 buf[3] = self.padding_columns;
230 buf[4] = 0xFF; buf[5] = 0xFE | u8::from(self.current_next_indicator);
233 buf[6] = self.section_number;
234 buf[7] = self.last_section_number;
235
236 let rtp_start = HEADER_LEN + EXTENSION_HEADER_LEN;
238 buf[rtp_start..rtp_start + RTP_LEN].copy_from_slice(&self.real_time_parameters.to_bytes());
239
240 let data_start = rtp_start + RTP_LEN;
242 let data_end = data_start + self.rs_data.len();
243 buf[data_start..data_end].copy_from_slice(self.rs_data);
244
245 let crc = dvb_common::crc32_mpeg2::compute(&buf[..data_end]);
247 buf[data_end..len].copy_from_slice(&crc.to_be_bytes());
248
249 Ok(len)
250 }
251}
252impl<'a> crate::traits::TableDef<'a> for MpeFecSection<'a> {
253 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
254 const NAME: &'static str = "MPE_FEC";
255}
256
257#[cfg(test)]
258mod tests {
259 use super::*;
260
261 fn build_mpe_fec(
262 padding_columns: u8,
263 current_next: bool,
264 section_number: u8,
265 last_section_number: u8,
266 rtp: RealTimeParameters,
267 rs_data: &[u8],
268 ) -> Vec<u8> {
269 let s = MpeFecSection {
270 private_indicator: true,
271 padding_columns,
272 current_next_indicator: current_next,
273 section_number,
274 last_section_number,
275 real_time_parameters: rtp,
276 rs_data,
277 };
278 let mut buf = vec![0u8; s.serialized_len()];
279 s.serialize_into(&mut buf).unwrap();
280 buf
281 }
282
283 fn sample_rtp() -> RealTimeParameters {
284 RealTimeParameters {
285 delta_t: 0x0ABC,
286 table_boundary: true,
287 frame_boundary: false,
288 address: 0x0001_2345,
289 }
290 }
291
292 #[test]
293 fn parse_happy_path() {
294 let rs = [0x11u8, 0x22, 0x33, 0x44];
295 let bytes = build_mpe_fec(7, true, 1, 3, sample_rtp(), &rs);
296 let s = MpeFecSection::parse(&bytes).unwrap();
297 assert!(s.private_indicator);
298 assert_eq!(s.padding_columns, 7);
299 assert!(s.current_next_indicator);
300 assert_eq!(s.section_number, 1);
301 assert_eq!(s.last_section_number, 3);
302 assert_eq!(s.real_time_parameters, sample_rtp());
303 assert_eq!(s.rs_data, &rs[..]);
304 }
305
306 #[test]
307 fn parse_empty_rs_data() {
308 let bytes = build_mpe_fec(0, false, 0, 0, sample_rtp(), &[]);
309 let s = MpeFecSection::parse(&bytes).unwrap();
310 assert_eq!(s.padding_columns, 0);
311 assert!(!s.current_next_indicator);
312 assert!(s.rs_data.is_empty());
313 assert_eq!(s.real_time_parameters, sample_rtp());
314 }
315
316 #[test]
317 fn rtp_bit_packing_round_trips_extremes() {
318 let rtp = RealTimeParameters {
320 delta_t: 0x0FFF,
321 table_boundary: false,
322 frame_boundary: true,
323 address: 0x0003_FFFF,
324 };
325 assert_eq!(RealTimeParameters::from_bytes(rtp.to_bytes()), rtp);
326 }
327
328 #[test]
329 fn parse_rejects_wrong_tag() {
330 let mut bytes = build_mpe_fec(0, true, 0, 0, sample_rtp(), &[]);
331 bytes[0] = 0x70; assert!(matches!(
333 MpeFecSection::parse(&bytes).unwrap_err(),
334 Error::UnexpectedTableId { table_id: 0x70, .. }
335 ));
336 }
337
338 #[test]
339 fn parse_rejects_short_buffer() {
340 assert!(matches!(
341 MpeFecSection::parse(&[0x78, 0x80]).unwrap_err(),
342 Error::BufferTooShort { .. }
343 ));
344 }
345
346 #[test]
347 fn parse_rejects_section_length_overflow() {
348 let mut bytes = build_mpe_fec(0, true, 0, 0, sample_rtp(), &[]);
349 let fake_sl: u16 = (bytes.len() as u16) + 100 - HEADER_LEN as u16;
350 bytes[1] = (bytes[1] & 0xF0) | ((fake_sl >> 8) as u8 & 0x0F);
351 bytes[2] = (fake_sl & 0xFF) as u8;
352 assert!(matches!(
353 MpeFecSection::parse(&bytes).unwrap_err(),
354 Error::SectionLengthOverflow { .. }
355 ));
356 }
357
358 #[test]
359 fn serialize_round_trip() {
360 let rs = [0xDEu8, 0xAD, 0xBE, 0xEF, 0x00];
361 let original = MpeFecSection {
362 private_indicator: false,
363 padding_columns: 191,
364 current_next_indicator: false,
365 section_number: 2,
366 last_section_number: 4,
367 real_time_parameters: sample_rtp(),
368 rs_data: &rs,
369 };
370 let mut buf = vec![0u8; original.serialized_len()];
371 original.serialize_into(&mut buf).unwrap();
372 assert_eq!(MpeFecSection::parse(&buf).unwrap(), original);
373 }
374
375 #[test]
376 fn serialize_rejects_output_buffer_too_small() {
377 let s = MpeFecSection {
378 private_indicator: false,
379 padding_columns: 0,
380 current_next_indicator: true,
381 section_number: 0,
382 last_section_number: 0,
383 real_time_parameters: sample_rtp(),
384 rs_data: &[],
385 };
386 let mut buf = vec![0u8; 2];
387 assert!(matches!(
388 s.serialize_into(&mut buf).unwrap_err(),
389 Error::OutputBufferTooSmall { .. }
390 ));
391 }
392
393 #[test]
394 fn table_trait_constants() {
395 assert_eq!(TABLE_ID, 0x78);
396 assert_eq!(PID, 0x0000);
397 }
398
399 #[test]
400 fn parse_rejects_zero_section_length() {
401 let mut buf = vec![0u8; 64];
402 buf[0] = TABLE_ID;
403 buf[1] = 0xF0;
404 buf[2] = 0x00;
405 for b in &mut buf[3..] {
406 *b = 0xFF;
407 }
408 assert!(matches!(
409 MpeFecSection::parse(&buf).unwrap_err(),
410 Error::SectionLengthOverflow { .. }
411 ));
412 }
413
414 #[cfg(feature = "serde")]
415 #[test]
416 fn serde_json_serializes_fields() {
417 let rs = [0x01u8, 0x02];
419 let bytes = build_mpe_fec(12, true, 0, 0, sample_rtp(), &rs);
420 let s = MpeFecSection::parse(&bytes).unwrap();
421 let v: serde_json::Value = serde_json::to_value(&s).unwrap();
422 assert_eq!(v["padding_columns"], 12);
423 assert_eq!(v["current_next_indicator"], true);
424 assert_eq!(v["rs_data"], serde_json::json!([0x01, 0x02]));
425 assert_eq!(v["real_time_parameters"]["delta_t"], 0x0ABC);
426 assert_eq!(v["real_time_parameters"]["table_boundary"], true);
427 }
428}