| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | #![forbid(unsafe_code)] |
| 17 | |
| 18 | use std::collections::{HashMap, VecDeque}; |
| 19 | |
| 20 | use ec_core::{ |
| 21 | CodecId, CodecParameters, Decoder, Error, Frame, PixelFormat, Plane, Result, Timestamp, |
| 22 | VideoFrame, |
| 23 | }; |
| 24 | |
| 25 | |
| 26 | pub const MAX_DIMENSION: u32 = 4096; |
| 27 | |
| 28 | pub const MAX_PENDING: usize = 32; |
| 29 | |
| 30 | const SEG_PAGE: u8 = 0x10; |
| 31 | const SEG_REGION: u8 = 0x11; |
| 32 | const SEG_CLUT: u8 = 0x12; |
| 33 | const SEG_OBJECT: u8 = 0x13; |
| 34 | const SEG_DISPLAY: u8 = 0x14; |
| 35 | const SEG_END: u8 = 0x80; |
| 36 | |
| 37 | |
| 38 | #[derive(Debug, Clone)] |
| 39 | pub struct DvbCue { |
| 40 | |
| 41 | pub frame: VideoFrame, |
| 42 | |
| 43 | pub timeout_ms: u32, |
| 44 | |
| 45 | pub empty: bool, |
| 46 | |
| 47 | pub page_state: u8, |
| 48 | |
| 49 | pub regions: Vec<(u32, u32, u32, u32)>, |
| 50 | } |
| 51 | |
| 52 | type Rgba = [u8; 4]; |
| 53 | |
| 54 | struct Clut { |
| 55 | c2: [Rgba; 4], |
| 56 | c4: [Rgba; 16], |
| 57 | c8: [Rgba; 256], |
| 58 | } |
| 59 | |
| 60 | struct Region { |
| 61 | width: u32, |
| 62 | height: u32, |
| 63 | depth: u8, |
| 64 | clut: u8, |
| 65 | pix: Vec<u8>, |
| 66 | objects: Vec<(u16, u32, u32)>, |
| 67 | } |
| 68 | |
| 69 | |
| 70 | pub struct DvbSubDecoder { |
| 71 | params: CodecParameters, |
| 72 | regions: HashMap<u8, Region>, |
| 73 | cluts: HashMap<u8, Clut>, |
| 74 | page: Vec<(u8, u32, u32)>, |
| 75 | timeout: u8, |
| 76 | page_state: u8, |
| 77 | display: (u32, u32), |
| 78 | ready: VecDeque<DvbCue>, |
| 79 | pts: Option<Timestamp>, |
| 80 | } |
| 81 | |
| 82 | impl Default for DvbSubDecoder { |
| 83 | fn default() -> Self { |
| 84 | DvbSubDecoder::new() |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn default_clut() -> Clut { |
| 89 | let mut c = Clut { c2: [[0; 4]; 4], c4: [[0; 4]; 16], c8: [[0; 4]; 256] }; |
| 90 | c.c2 = [[0, 0, 0, 0], [255, 255, 255, 255], [0, 0, 0, 255], [127, 127, 127, 255]]; |
| 91 | for i in 1..16usize { |
| 92 | let bit = |m: usize, v: u8| if i & m != 0 { v } else { 0 }; |
| 93 | c.c4[i] = if i < 8 { |
| 94 | [bit(1, 255), bit(2, 255), bit(4, 255), 255] |
| 95 | } else { |
| 96 | [bit(1, 127), bit(2, 127), bit(4, 127), 255] |
| 97 | }; |
| 98 | } |
| 99 | for i in 1..256usize { |
| 100 | let f = |m: usize, v: u32| if i & m != 0 { v } else { 0 }; |
| 101 | let (r, g, b, a); |
| 102 | if i < 8 { |
| 103 | r = f(1, 255); |
| 104 | g = f(2, 255); |
| 105 | b = f(4, 255); |
| 106 | a = 63; |
| 107 | } else { |
| 108 | match i & 0x88 { |
| 109 | 0x00 => { |
| 110 | r = f(1, 85) + f(0x10, 170); |
| 111 | g = f(2, 85) + f(0x20, 170); |
| 112 | b = f(4, 85) + f(0x40, 170); |
| 113 | a = 255; |
| 114 | } |
| 115 | 0x08 => { |
| 116 | r = f(1, 85) + f(0x10, 170); |
| 117 | g = f(2, 85) + f(0x20, 170); |
| 118 | b = f(4, 85) + f(0x40, 170); |
| 119 | a = 127; |
| 120 | } |
| 121 | 0x80 => { |
| 122 | r = 127 + f(1, 43) + f(0x10, 85); |
| 123 | g = 127 + f(2, 43) + f(0x20, 85); |
| 124 | b = 127 + f(4, 43) + f(0x40, 85); |
| 125 | a = 255; |
| 126 | } |
| 127 | _ => { |
| 128 | r = f(1, 43) + f(0x10, 85); |
| 129 | g = f(2, 43) + f(0x20, 85); |
| 130 | b = f(4, 43) + f(0x40, 85); |
| 131 | a = 255; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | c.c8[i] = [r as u8, g as u8, b as u8, a as u8]; |
| 136 | } |
| 137 | c |
| 138 | } |
| 139 | |
| 140 | |
| 141 | fn ycc_to_rgb(y: i32, cr: i32, cb: i32) -> [u8; 3] { |
| 142 | let cb = cb - 128; |
| 143 | let cr = cr - 128; |
| 144 | let one_half = 1 << 15; |
| 145 | let fix = |x: f64| (x * f64::from(1 << 16) + 0.5) as i32; |
| 146 | let r_add = fix(1.402 * 255.0 / 224.0) * cr + one_half; |
| 147 | let g_add = -fix(0.34414 * 255.0 / 224.0) * cb - fix(0.71414 * 255.0 / 224.0) * cr + one_half; |
| 148 | let b_add = fix(1.772 * 255.0 / 224.0) * cb + one_half; |
| 149 | let yy = (y - 16) * fix(255.0 / 219.0); |
| 150 | let c = |v: i32| (v >> 16).clamp(0, 255) as u8; |
| 151 | [c(yy + r_add), c(yy + g_add), c(yy + b_add)] |
| 152 | } |
| 153 | |
| 154 | fn be16(d: &[u8], at: usize) -> Option<usize> { |
| 155 | Some(usize::from(u16::from_be_bytes([*d.get(at)?, *d.get(at + 1)?]))) |
| 156 | } |
| 157 | |
| 158 | struct Bits<'a> { |
| 159 | d: &'a [u8], |
| 160 | pos: usize, |
| 161 | } |
| 162 | |
| 163 | impl Bits<'_> { |
| 164 | fn get(&mut self, n: usize) -> Option<u32> { |
| 165 | if self.pos + n > self.d.len() * 8 { |
| 166 | return None; |
| 167 | } |
| 168 | let mut v = 0; |
| 169 | for _ in 0..n { |
| 170 | let b = (self.d[self.pos / 8] >> (7 - self.pos % 8)) & 1; |
| 171 | v = (v << 1) | u32::from(b); |
| 172 | self.pos += 1; |
| 173 | } |
| 174 | Some(v) |
| 175 | } |
| 176 | fn align(&mut self) { |
| 177 | self.pos = self.pos.div_ceil(8) * 8; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { |
| 184 | let mut b = Bits { d, pos: 0 }; |
| 185 | loop { |
| 186 | let r = (|| -> Option<Option<(usize, u8)>> { |
| 187 | Some(match depth { |
| 188 | 2 => { |
| 189 | let v = b.get(2)?; |
| 190 | if v != 0 { |
| 191 | Some((1, v as u8)) |
| 192 | } else if b.get(1)? == 1 { |
| 193 | let n = b.get(3)? as usize + 3; |
| 194 | Some((n, b.get(2)? as u8)) |
| 195 | } else if b.get(1)? == 1 { |
| 196 | Some((1, 0)) |
| 197 | } else { |
| 198 | match b.get(2)? { |
| 199 | 0 => None, |
| 200 | 1 => Some((2, 0)), |
| 201 | 2 => { |
| 202 | let n = b.get(4)? as usize + 12; |
| 203 | Some((n, b.get(2)? as u8)) |
| 204 | } |
| 205 | _ => { |
| 206 | let n = b.get(8)? as usize + 29; |
| 207 | Some((n, b.get(2)? as u8)) |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | 4 => { |
| 213 | let v = b.get(4)?; |
| 214 | if v != 0 { |
| 215 | Some((1, v as u8)) |
| 216 | } else if b.get(1)? == 0 { |
| 217 | let n = b.get(3)? as usize; |
| 218 | if n == 0 { None } else { Some((n + 2, 0)) } |
| 219 | } else if b.get(1)? == 0 { |
| 220 | let n = b.get(2)? as usize + 4; |
| 221 | Some((n, b.get(4)? as u8)) |
| 222 | } else { |
| 223 | match b.get(2)? { |
| 224 | 0 => Some((1, 0)), |
| 225 | 1 => Some((2, 0)), |
| 226 | 2 => { |
| 227 | let n = b.get(4)? as usize + 9; |
| 228 | Some((n, b.get(4)? as u8)) |
| 229 | } |
| 230 | _ => { |
| 231 | let n = b.get(8)? as usize + 25; |
| 232 | Some((n, b.get(4)? as u8)) |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | _ => { |
| 238 | let v = b.get(8)?; |
| 239 | if v != 0 { |
| 240 | Some((1, v as u8)) |
| 241 | } else if b.get(1)? == 0 { |
| 242 | let n = b.get(7)? as usize; |
| 243 | if n == 0 { None } else { Some((n, 0)) } |
| 244 | } else { |
| 245 | let n = b.get(7)? as usize; |
| 246 | Some((n, b.get(8)? as u8)) |
| 247 | } |
| 248 | } |
| 249 | }) |
| 250 | })(); |
| 251 | match r { |
| 252 | Some(Some((n, c))) => put(n, c), |
| 253 | Some(None) => { |
| 254 | b.align(); |
| 255 | return (b.pos / 8).min(d.len()); |
| 256 | } |
| 257 | None => return d.len(), |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | impl DvbSubDecoder { |
| 263 | |
| 264 | pub fn new() -> DvbSubDecoder { |
| 265 | DvbSubDecoder { |
| 266 | params: CodecParameters::new(CodecId::DvbSub), |
| 267 | regions: HashMap::new(), |
| 268 | cluts: HashMap::new(), |
| 269 | page: Vec::new(), |
| 270 | timeout: 0, |
| 271 | page_state: 0, |
| 272 | display: (720, 576), |
| 273 | ready: VecDeque::new(), |
| 274 | pts: None, |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 | |
| 280 | pub fn push_pes(&mut self, data: &[u8]) -> Result<()> { |
| 281 | let mut at = if data.len() >= 2 && data[0] == 0x20 && data[1] == 0x00 { 2 } else { 0 }; |
| 282 | let mut first = None; |
| 283 | while at + 6 <= data.len() && data[at] == 0x0F { |
| 284 | let kind = data[at + 1]; |
| 285 | let len = be16(data, at + 4).unwrap_or(0); |
| 286 | let end = at + 6 + len; |
| 287 | let Some(body) = data.get(at + 6..end) else { |
| 288 | first.get_or_insert(Error::corrupt("dvbsub: segment past the packet")); |
| 289 | break; |
| 290 | }; |
| 291 | let r = match kind { |
| 292 | SEG_PAGE => self.page_comp(body), |
| 293 | SEG_REGION => self.region_comp(body), |
| 294 | SEG_CLUT => self.clut_def(body), |
| 295 | SEG_OBJECT => self.object_data(body), |
| 296 | SEG_DISPLAY => self.display_def(body), |
| 297 | SEG_END => self.end_set(), |
| 298 | _ => Ok(()), |
| 299 | }; |
| 300 | if let Err(e) = r { |
| 301 | first.get_or_insert(e); |
| 302 | } |
| 303 | at = end; |
| 304 | } |
| 305 | match first { |
| 306 | Some(e) => Err(e), |
| 307 | None => Ok(()), |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
| 312 | pub fn take_cue(&mut self) -> Option<DvbCue> { |
| 313 | self.ready.pop_front() |
| 314 | } |
| 315 | |
| 316 | fn display_def(&mut self, b: &[u8]) -> Result<()> { |
| 317 | let w = be16(b, 1).ok_or_else(|| Error::corrupt("dvbsub: short display definition"))? as u32 + 1; |
| 318 | let h = be16(b, 3).ok_or_else(|| Error::corrupt("dvbsub: short display definition"))? as u32 + 1; |
| 319 | if w > MAX_DIMENSION || h > MAX_DIMENSION { |
| 320 | return Err(Error::corrupt("dvbsub: display past the dimension limit")); |
| 321 | } |
| 322 | self.display = (w, h); |
| 323 | Ok(()) |
| 324 | } |
| 325 | |
| 326 | fn page_comp(&mut self, b: &[u8]) -> Result<()> { |
| 327 | if b.len() < 2 { |
| 328 | return Err(Error::corrupt("dvbsub: short page composition")); |
| 329 | } |
| 330 | self.timeout = b[0]; |
| 331 | self.page_state = (b[1] >> 2) & 3; |
| 332 | if self.page_state != 0 { |
| 333 | |
| 334 | self.regions.clear(); |
| 335 | self.cluts.clear(); |
| 336 | } |
| 337 | self.page.clear(); |
| 338 | let mut at = 2; |
| 339 | while at + 6 <= b.len() { |
| 340 | let x = be16(b, at + 2).unwrap_or(0) as u32; |
| 341 | let y = be16(b, at + 4).unwrap_or(0) as u32; |
| 342 | self.page.push((b[at], x, y)); |
| 343 | at += 6; |
| 344 | } |
| 345 | Ok(()) |
| 346 | } |
| 347 | |
| 348 | fn region_comp(&mut self, b: &[u8]) -> Result<()> { |
| 349 | if b.len() < 10 { |
| 350 | return Err(Error::corrupt("dvbsub: short region composition")); |
| 351 | } |
| 352 | let id = b[0]; |
| 353 | let fill = b[1] & 0x08 != 0; |
| 354 | let w = be16(b, 2).unwrap() as u32; |
| 355 | let h = be16(b, 4).unwrap() as u32; |
| 356 | if w == 0 || h == 0 || w > MAX_DIMENSION || h > MAX_DIMENSION { |
| 357 | return Err(Error::corrupt("dvbsub: region size out of range")); |
| 358 | } |
| 359 | let depth = match (b[6] >> 2) & 7 { |
| 360 | 1 => 2, |
| 361 | 2 => 4, |
| 362 | 3 => 8, |
| 363 | _ => return Err(Error::corrupt("dvbsub: bad region depth")), |
| 364 | }; |
| 365 | let clut = b[7]; |
| 366 | let bg = match depth { |
| 367 | 2 => (b[9] >> 2) & 3, |
| 368 | 4 => b[9] >> 4, |
| 369 | _ => b[8], |
| 370 | }; |
| 371 | let mut pix = vec![0u8; (w * h) as usize]; |
| 372 | if fill { |
| 373 | pix.fill(bg); |
| 374 | } |
| 375 | let mut objects = Vec::new(); |
| 376 | let mut at = 10; |
| 377 | while at + 6 <= b.len() { |
| 378 | let oid = be16(b, at).unwrap() as u16; |
| 379 | let kind = b[at + 2] >> 6; |
| 380 | let x = (be16(b, at + 2).unwrap() & 0xFFF) as u32; |
| 381 | let y = (be16(b, at + 4).unwrap() & 0xFFF) as u32; |
| 382 | objects.push((oid, x, y)); |
| 383 | at += if kind == 1 || kind == 2 { 8 } else { 6 }; |
| 384 | } |
| 385 | self.regions.insert(id, Region { width: w, height: h, depth, clut, pix, objects }); |
| 386 | Ok(()) |
| 387 | } |
| 388 | |
| 389 | fn clut_def(&mut self, b: &[u8]) -> Result<()> { |
| 390 | if b.len() < 2 { |
| 391 | return Err(Error::corrupt("dvbsub: short CLUT")); |
| 392 | } |
| 393 | let clut = self.cluts.entry(b[0]).or_insert_with(default_clut); |
| 394 | let mut at = 2; |
| 395 | while at + 2 <= b.len() { |
| 396 | let id = usize::from(b[at]); |
| 397 | let flags = b[at + 1]; |
| 398 | at += 2; |
| 399 | let (y, cr, cb, t); |
| 400 | if flags & 1 != 0 { |
| 401 | let Some(e) = b.get(at..at + 4) else { break }; |
| 402 | y = i32::from(e[0]); |
| 403 | cr = i32::from(e[1]); |
| 404 | cb = i32::from(e[2]); |
| 405 | t = i32::from(e[3]); |
| 406 | at += 4; |
| 407 | } else { |
| 408 | let Some(e) = b.get(at..at + 2) else { break }; |
| 409 | y = i32::from(e[0] & 0xFC); |
| 410 | cr = i32::from((((e[0] & 3) << 2) | (e[1] >> 6)) << 4); |
| 411 | cb = i32::from((e[1] << 2) & 0xF0); |
| 412 | t = i32::from((e[1] << 6) & 0xC0); |
| 413 | at += 2; |
| 414 | } |
| 415 | let rgba = if y == 0 { |
| 416 | [0, 0, 0, 0] |
| 417 | } else { |
| 418 | let rgb = ycc_to_rgb(y, cr, cb); |
| 419 | [rgb[0], rgb[1], rgb[2], (255 - t) as u8] |
| 420 | }; |
| 421 | if flags & 0x80 != 0 && id < 4 { |
| 422 | clut.c2[id] = rgba; |
| 423 | } |
| 424 | if flags & 0x40 != 0 && id < 16 { |
| 425 | clut.c4[id] = rgba; |
| 426 | } |
| 427 | if flags & 0x20 != 0 { |
| 428 | clut.c8[id] = rgba; |
| 429 | } |
| 430 | } |
| 431 | Ok(()) |
| 432 | } |
| 433 | |
| 434 | fn object_data(&mut self, b: &[u8]) -> Result<()> { |
| 435 | if b.len() < 3 { |
| 436 | return Err(Error::corrupt("dvbsub: short object")); |
| 437 | } |
| 438 | let oid = be16(b, 0).unwrap() as u16; |
| 439 | let method = (b[2] >> 2) & 3; |
| 440 | if method != 0 { |
| 441 | return Ok(()); |
| 442 | } |
| 443 | let top_len = be16(b, 3).ok_or_else(|| Error::corrupt("dvbsub: short object"))?; |
| 444 | let bot_len = be16(b, 5).ok_or_else(|| Error::corrupt("dvbsub: short object"))?; |
| 445 | let top = b.get(7..7 + top_len).ok_or_else(|| Error::corrupt("dvbsub: field past object"))?; |
| 446 | let bot = if bot_len == 0 { |
| 447 | top |
| 448 | } else { |
| 449 | b.get(7 + top_len..7 + top_len + bot_len) |
| 450 | .ok_or_else(|| Error::corrupt("dvbsub: field past object"))? |
| 451 | }; |
| 452 | for region in self.regions.values_mut() { |
| 453 | let placed: Vec<(u32, u32)> = region |
| 454 | .objects |
| 455 | .iter() |
| 456 | .filter(|o| o.0 == oid) |
| 457 | .map(|o| (o.1, o.2)) |
| 458 | .collect(); |
| 459 | for (x, y) in placed { |
| 460 | paint_field(region, top, x, y, 0); |
| 461 | paint_field(region, bot, x, y, 1); |
| 462 | } |
| 463 | } |
| 464 | Ok(()) |
| 465 | } |
| 466 | |
| 467 | fn end_set(&mut self) -> Result<()> { |
| 468 | let (cw, ch) = self.display; |
| 469 | let stride = cw as usize * 4; |
| 470 | let mut px = vec![0u8; stride * ch as usize]; |
| 471 | let mut shown = Vec::new(); |
| 472 | let def = default_clut(); |
| 473 | for &(rid, rx, ry) in &self.page { |
| 474 | let Some(r) = self.regions.get(&rid) else { continue }; |
| 475 | let clut = self.cluts.get(&r.clut).unwrap_or(&def); |
| 476 | shown.push((rx, ry, r.width, r.height)); |
| 477 | for row in 0..r.height { |
| 478 | let ty = ry + row; |
| 479 | if ty >= ch { |
| 480 | break; |
| 481 | } |
| 482 | for col in 0..r.width { |
| 483 | let tx = rx + col; |
| 484 | if tx >= cw { |
| 485 | break; |
| 486 | } |
| 487 | let code = usize::from(r.pix[(row * r.width + col) as usize]); |
| 488 | let c = match r.depth { |
| 489 | 2 => clut.c2[code & 3], |
| 490 | 4 => clut.c4[code & 15], |
| 491 | _ => clut.c8[code], |
| 492 | }; |
| 493 | let o = ty as usize * stride + tx as usize * 4; |
| 494 | px[o..o + 4].copy_from_slice(&c); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | let frame = VideoFrame::try_new(PixelFormat::Rgba8, cw, ch, vec![Plane::new(px, stride)])?; |
| 499 | if self.ready.len() >= MAX_PENDING { |
| 500 | self.ready.pop_front(); |
| 501 | } |
| 502 | let mut frame = frame; |
| 503 | frame.pts = self.pts; |
| 504 | self.ready.push_back(DvbCue { |
| 505 | frame, |
| 506 | timeout_ms: u32::from(self.timeout) * 1000, |
| 507 | empty: shown.is_empty(), |
| 508 | page_state: self.page_state, |
| 509 | regions: shown, |
| 510 | }); |
| 511 | Ok(()) |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | |
| 516 | |
| 517 | fn paint_field(region: &mut Region, data: &[u8], x: u32, y: u32, field: u32) { |
| 518 | let (w, h) = (region.width as usize, region.height as usize); |
| 519 | let mut row = (y + field) as usize; |
| 520 | let mut col = x as usize; |
| 521 | let mut m24 = [0u8, 7, 8, 15]; |
| 522 | let mut m28 = [0u8, 0x77, 0x88, 0xFF]; |
| 523 | let mut m48: [u8; 16] = std::array::from_fn(|i| (i * 0x11) as u8); |
| 524 | let mut at = 0; |
| 525 | while at < data.len() { |
| 526 | let kind = data[at]; |
| 527 | at += 1; |
| 528 | match kind { |
| 529 | 0x10..=0x12 => { |
| 530 | let src_depth = match kind { |
| 531 | 0x10 => 2, |
| 532 | 0x11 => 4, |
| 533 | _ => 8, |
| 534 | }; |
| 535 | let depth = region.depth; |
| 536 | let (m24c, m28c, m48c) = (m24, m28, m48); |
| 537 | let mapped = |c: u8| -> u8 { |
| 538 | match (src_depth, depth) { |
| 539 | (2, 4) => m24c[usize::from(c & 3)], |
| 540 | (2, 8) => m28c[usize::from(c & 3)], |
| 541 | (4, 8) => m48c[usize::from(c & 15)], |
| 542 | _ => c, |
| 543 | } |
| 544 | }; |
| 545 | let used = code_string(&data[at..], src_depth, |n, c| { |
| 546 | if src_depth > depth || row >= h { |
| 547 | return; |
| 548 | } |
| 549 | let c = mapped(c); |
| 550 | for _ in 0..n { |
| 551 | if col < w { |
| 552 | region.pix[row * w + col] = c; |
| 553 | } |
| 554 | col += 1; |
| 555 | } |
| 556 | }); |
| 557 | let end = at + used; |
| 558 | at = end; |
| 559 | } |
| 560 | 0x20 => { |
| 561 | if let Some(t) = data.get(at..at + 2) { |
| 562 | m24 = [t[0] >> 4, t[0] & 15, t[1] >> 4, t[1] & 15]; |
| 563 | } |
| 564 | at += 2; |
| 565 | } |
| 566 | 0x21 => { |
| 567 | if let Some(t) = data.get(at..at + 4) { |
| 568 | m28 = [t[0], t[1], t[2], t[3]]; |
| 569 | } |
| 570 | at += 4; |
| 571 | } |
| 572 | 0x22 => { |
| 573 | if let Some(t) = data.get(at..at + 16) { |
| 574 | m48.copy_from_slice(t); |
| 575 | } |
| 576 | at += 16; |
| 577 | } |
| 578 | 0xF0 => { |
| 579 | row += 2; |
| 580 | col = x as usize; |
| 581 | } |
| 582 | _ => return, |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | impl Decoder for DvbSubDecoder { |
| 588 | fn codec_parameters(&self) -> &CodecParameters { |
| 589 | &self.params |
| 590 | } |
| 591 | |
| 592 | fn send_packet(&mut self, packet: &ec_core::Packet) -> Result<()> { |
| 593 | self.pts = packet.pts.map(|t| Timestamp::new(t, packet.time_base)); |
| 594 | self.push_pes(&packet.data) |
| 595 | } |
| 596 | |
| 597 | fn receive_frame(&mut self) -> Result<Frame> { |
| 598 | match self.take_cue() { |
| 599 | Some(c) => Ok(Frame::Video(c.frame)), |
| 600 | None => Err(Error::NeedMore), |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | fn flush(&mut self) -> Result<()> { |
| 605 | Ok(()) |
| 606 | } |
| 607 | |
| 608 | fn reset(&mut self) { |
| 609 | *self = DvbSubDecoder::new(); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | |
| 614 | pub fn make_decoder(_params: &CodecParameters) -> Result<Box<dyn Decoder>> { |
| 615 | Ok(Box::new(DvbSubDecoder::new())) |
| 616 | } |
| 617 | |
| 618 | #[cfg(test)] |
| 619 | mod tests { |
| 620 | use super::*; |
| 621 | |
| 622 | fn seg(kind: u8, body: &[u8]) -> Vec<u8> { |
| 623 | let mut v = vec![0x0F, kind, 0, 1]; |
| 624 | v.extend_from_slice(&(body.len() as u16).to_be_bytes()); |
| 625 | v.extend_from_slice(body); |
| 626 | v |
| 627 | } |
| 628 | |
| 629 | fn set(depth_code: u8, string_type: u8, string: &[u8]) -> Vec<u8> { |
| 630 | let mut p = vec![0x20, 0x00]; |
| 631 | p.extend(seg(SEG_PAGE, &[5, 0x08, 0, 0, 0, 0, 0, 0])); |
| 632 | |
| 633 | p.extend(seg(SEG_REGION, &[0, 0, 0, 4, 0, 1, depth_code << 2, 0, 0, 0, 0, 7, 0, 0, 0, 0])); |
| 634 | let mut obj = vec![0, 7, 0]; |
| 635 | let field = [&[string_type][..], string, &[0xF0]].concat(); |
| 636 | obj.extend_from_slice(&(field.len() as u16).to_be_bytes()); |
| 637 | obj.extend_from_slice(&[0, 0]); |
| 638 | obj.extend_from_slice(&field); |
| 639 | p.extend(seg(SEG_OBJECT, &obj)); |
| 640 | p.extend(seg(SEG_END, &[])); |
| 641 | p.push(0xFF); |
| 642 | p |
| 643 | } |
| 644 | |
| 645 | fn row(d: &mut DvbSubDecoder) -> Vec<[u8; 4]> { |
| 646 | let c = d.take_cue().expect("a display set"); |
| 647 | c.frame.planes[0].data[..16].chunks(4).map(|p| [p[0], p[1], p[2], p[3]]).collect() |
| 648 | } |
| 649 | |
| 650 | #[test] |
| 651 | fn two_bit_runs_use_the_default_clut() { |
| 652 | let mut d = DvbSubDecoder::new(); |
| 653 | |
| 654 | d.push_pes(&set(1, 0x10, &[0x56, 0x00])).unwrap(); |
| 655 | let r = row(&mut d); |
| 656 | assert_eq!(r, vec![[255, 255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255], [0, 0, 0, 255]]); |
| 657 | } |
| 658 | |
| 659 | #[test] |
| 660 | fn four_and_eight_bit_strings_decode_runs() { |
| 661 | let mut d = DvbSubDecoder::new(); |
| 662 | |
| 663 | |
| 664 | d.push_pes(&set(2, 0x11, &[0x10, 0x01, 0x00])).unwrap(); |
| 665 | let r = row(&mut d); |
| 666 | assert_eq!(r[0], [255, 0, 0, 255]); |
| 667 | assert_eq!(r[1][3], 0); |
| 668 | |
| 669 | d.push_pes(&set(3, 0x12, &[9, 9, 0, 0])).unwrap(); |
| 670 | let r = row(&mut d); |
| 671 | assert_eq!(r[0], r[1]); |
| 672 | assert_ne!(r[0][3], 0); |
| 673 | } |
| 674 | |
| 675 | #[test] |
| 676 | fn rubbish_never_panics() { |
| 677 | let mut x = 0x1234_5678u32; |
| 678 | let mut d = DvbSubDecoder::new(); |
| 679 | for n in 0..2000 { |
| 680 | let len = (n % 300) + 1; |
| 681 | let mut buf: Vec<u8> = (0..len) |
| 682 | .map(|_| { |
| 683 | x = x.wrapping_mul(1664525).wrapping_add(1013904223); |
| 684 | (x >> 24) as u8 |
| 685 | }) |
| 686 | .collect(); |
| 687 | if n % 2 == 0 { |
| 688 | buf[0] = 0x0F; |
| 689 | if buf.len() > 1 { |
| 690 | buf[1] = 0x10 + (n % 6) as u8; |
| 691 | } |
| 692 | } |
| 693 | let _ = d.push_pes(&buf); |
| 694 | while d.take_cue().is_some() {} |
| 695 | } |
| 696 | } |
| 697 | } |