From 2425989e4ac447e52951c95fdd4c745c5267b2f2 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Fri, 16 Jun 2023 18:06:31 +0200 Subject: [PATCH] improve OSD a bit --- videoplayer/src/main.rs | 2 + videoplayer/src/osd.rs | 552 ++++++++++++++++++++++------------------ 2 files changed, 305 insertions(+), 249 deletions(-) diff --git a/videoplayer/src/main.rs b/videoplayer/src/main.rs index bc453c3..0b4446d 100644 --- a/videoplayer/src/main.rs +++ b/videoplayer/src/main.rs @@ -540,6 +540,8 @@ impl Player { } self.has_video = false; self.has_audio = false; + self.osd.reset(); + self.osd.set_duration(duration); for i in 0..dmx.get_num_streams() { let s = dmx.get_stream(i).unwrap(); let info = s.get_info(); diff --git a/videoplayer/src/osd.rs b/videoplayer/src/osd.rs index 74a8a95..0262c8e 100644 --- a/videoplayer/src/osd.rs +++ b/videoplayer/src/osd.rs @@ -1,14 +1,24 @@ use std::time::Instant; +const XOFF: usize = 10; +const YOFF: usize = 4; + #[derive(Default)] pub struct OSD { time: Option, text: Vec, text_stride: usize, + duration: u64, } impl OSD { pub fn new() -> Self { Self::default() } + pub fn reset(&mut self) { + self.text.clear(); + self.duration = 0; + self.time = None; + } + pub fn set_duration(&mut self, duration: u64) { self.duration = duration; } pub fn toggle(&mut self) { if self.time.is_none() { self.time = Some(Instant::now()); @@ -29,7 +39,11 @@ impl OSD { if !self.is_active() { return; } - let time_str = format_time(ts); + let mut time_str = format_time(ts); + if self.duration != 0 { + time_str += "/"; + time_str += format_time(self.duration).as_str(); + } let mut w = 0; let mut syms = Vec::with_capacity(time_str.len()); for chr in time_str.chars() { @@ -62,8 +76,8 @@ impl OSD { if !self.is_active() || self.text_stride == 0 { return; } - for (dline, sline) in buffer.chunks_exact_mut(pitch).zip(self.text.chunks_exact(self.text_stride)) { - for (dst, &src) in dline.iter_mut().zip(sline.iter()) { + for (dline, sline) in buffer.chunks_exact_mut(pitch).skip(YOFF).zip(self.text.chunks_exact(self.text_stride)) { + for (dst, &src) in dline.iter_mut().skip(XOFF).zip(sline.iter()) { match src { 2 => *dst = 0xFF, 1 => *dst = 0x00, @@ -76,8 +90,8 @@ impl OSD { if !self.is_active() || self.text_stride == 0 { return; } - for (dline, sline) in buffer.chunks_exact_mut(pitch).zip(self.text.chunks_exact(self.text_stride)) { - for (dst, &src) in dline.chunks_exact_mut(3).zip(sline.iter()) { + for (dline, sline) in buffer.chunks_exact_mut(pitch).skip(YOFF).zip(self.text.chunks_exact(self.text_stride)) { + for (dst, &src) in dline.chunks_exact_mut(3).skip(XOFF).zip(sline.iter()) { match src { 2 => dst.copy_from_slice(&[0xFF, 0xFF, 0xFF]), 1 => dst.copy_from_slice(&[0x00, 0x00, 0x00]), @@ -94,7 +108,7 @@ fn format_time(ms: u64) -> String { let (h, min) = (min / 60, min % 60); if h == 0 { if min == 0 { - format!("{}", s) + format!("0:{:02}", s) } else { format!("{}:{:02}", min, s) } @@ -112,62 +126,62 @@ struct Glyph { const OSD_GLYPHS: &[Glyph] = &[ Glyph { - width: 16, + width: 14, sym: '?', bits: [ 0b_0000000000000000, - 0b_0000111111100000, - 0b_0001100001110000, - 0b_0000000000110000, + 0b_0000011111110000, + 0b_0000110000111000, + 0b_0000000000011000, + 0b_0000000000011000, 0b_0000000000110000, 0b_0000000001100000, 0b_0000000011000000, - 0b_0000000110000000, - 0b_0000000110000000, - 0b_0000000110000000, + 0b_0000000011000000, + 0b_0000000011000000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000001111000000, - 0b_0000001111000000, + 0b_0000000111100000, + 0b_0000000111100000, 0b_0000000000000000 ], mask: [ - 0b_0000111111100000, - 0b_0001111111110000, - 0b_0011111111111000, - 0b_0001100001111000, + 0b_0000011111110000, + 0b_0000111111111000, + 0b_0001111111111100, + 0b_0000110000111100, + 0b_0000000000111100, 0b_0000000001111000, 0b_0000000011110000, 0b_0000000111100000, - 0b_0000001111000000, - 0b_0000001111000000, - 0b_0000001111000000, - 0b_0000000110000000, - 0b_0000000000000000, - 0b_0000001111000000, - 0b_0000011111100000, - 0b_0000011111100000, - 0b_0000001111000000 + 0b_0000000111100000, + 0b_0000000111100000, + 0b_0000000011000000, + 0b_0000000000000000, + 0b_0000000111100000, + 0b_0000001111110000, + 0b_0000001111110000, + 0b_0000000111100000 ], }, Glyph { - width: 14, + width: 12, sym: '0', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000111100000, - 0b_0000001000010000, - 0b_0000010000011000, - 0b_0000010000101000, - 0b_0000010001001000, - 0b_0000010010001000, - 0b_0000010100001000, - 0b_0000011000001000, - 0b_0000001000010000, - 0b_0000000111100000, + 0b_0000000011110000, + 0b_0000000100001000, + 0b_0000001000001100, + 0b_0000001000010100, + 0b_0000001000100100, + 0b_0000001001000100, + 0b_0000001010000100, + 0b_0000001100000100, + 0b_0000000100001000, + 0b_0000000011110000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -175,39 +189,39 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000001111110000, - 0b_0000011000011000, - 0b_0000110111101100, - 0b_0000101000100100, - 0b_0000101001010100, - 0b_0000101010110100, - 0b_0000101101010100, - 0b_0000101010010100, - 0b_0000100100110100, - 0b_0000110111101100, - 0b_0000011000011000, - 0b_0000001111110000, + 0b_0000000111111000, + 0b_0000001100001100, + 0b_0000011011110110, + 0b_0000010100010010, + 0b_0000010100101010, + 0b_0000010101011010, + 0b_0000010110101010, + 0b_0000010101001010, + 0b_0000010010011010, + 0b_0000011011110110, + 0b_0000001100001100, + 0b_0000000111111000, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '1', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000001100000, - 0b_0000000011100000, - 0b_0000000110100000, - 0b_0000000000100000, - 0b_0000000000100000, - 0b_0000000000100000, - 0b_0000000000100000, - 0b_0000000000100000, - 0b_0000000000100000, - 0b_0000000011111000, + 0b_0000000000110000, + 0b_0000000001110000, + 0b_0000000011010000, + 0b_0000000000010000, + 0b_0000000000010000, + 0b_0000000000010000, + 0b_0000000000010000, + 0b_0000000000010000, + 0b_0000000000010000, + 0b_0000000001111100, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -215,79 +229,79 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000001110000, - 0b_0000000010010000, - 0b_0000000100010000, - 0b_0000001001010000, - 0b_0000001111010000, - 0b_0000000001010000, - 0b_0000000001010000, - 0b_0000000001010000, - 0b_0000000001010000, - 0b_0000000111011100, - 0b_0000000100000100, - 0b_0000000111111100, + 0b_0000000000111000, + 0b_0000000001001000, + 0b_0000000010001000, + 0b_0000000100101000, + 0b_0000000111101000, + 0b_0000000000101000, + 0b_0000000000101000, + 0b_0000000000101000, + 0b_0000000000101000, + 0b_0000000011101110, + 0b_0000000010000010, + 0b_0000000011111110, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '2', bits: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000011100000, - 0b_0000000100010000, - 0b_0000000000001000, - 0b_0000000000001000, + 0b_0000000001110000, + 0b_0000000010001000, + 0b_0000000000000100, + 0b_0000000000000100, + 0b_0000000000000100, 0b_0000000000001000, 0b_0000000000010000, 0b_0000000000100000, 0b_0000000001000000, 0b_0000000010000000, - 0b_0000000100000000, - 0b_0000000111111000, + 0b_0000000011111100, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 ], mask: [ 0b_0000000000000000, - 0b_0000000011100000, - 0b_0000000100010000, - 0b_0000001011101000, - 0b_0000001100010100, - 0b_0000000000010100, + 0b_0000000001110000, + 0b_0000000010001000, + 0b_0000000101110100, + 0b_0000000110001010, + 0b_0000000000001010, + 0b_0000000000001010, 0b_0000000000010100, 0b_0000000000101000, 0b_0000000001010000, 0b_0000000010100000, - 0b_0000000101000000, - 0b_0000001011111100, - 0b_0000001000000100, - 0b_0000001111111100, + 0b_0000000101111110, + 0b_0000000100000010, + 0b_0000000111111110, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '3', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000111111000, - 0b_0000000100001000, + 0b_0000000011111100, + 0b_0000000010000100, + 0b_0000000000001000, 0b_0000000000010000, 0b_0000000000100000, - 0b_0000000001000000, - 0b_0000000000100000, 0b_0000000000010000, 0b_0000000000001000, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000000000100, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -295,39 +309,39 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000001111111100, - 0b_0000001000000100, - 0b_0000001011110100, - 0b_0000001110101000, - 0b_0000000001010000, - 0b_0000000010100000, + 0b_0000000111111110, + 0b_0000000100000010, + 0b_0000000101111010, + 0b_0000000111010100, + 0b_0000000000101000, 0b_0000000001010000, 0b_0000000000101000, - 0b_0000001100010100, - 0b_0000001011110100, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000000010100, + 0b_0000000110001010, + 0b_0000000101111010, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '4', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000000011000, - 0b_0000000000101000, - 0b_0000000001001000, - 0b_0000000010001000, - 0b_0000000100001000, - 0b_0000000111111000, - 0b_0000000000001000, - 0b_0000000000001000, - 0b_0000000000001000, - 0b_0000000000001000, + 0b_0000000000001100, + 0b_0000000000010100, + 0b_0000000000100100, + 0b_0000000001000100, + 0b_0000000010000100, + 0b_0000000011111100, + 0b_0000000000000100, + 0b_0000000000000100, + 0b_0000000000000100, + 0b_0000000000000100, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -335,39 +349,39 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000000011100, - 0b_0000000000100100, - 0b_0000000001010100, - 0b_0000000010110100, - 0b_0000000101010100, - 0b_0000001011110100, - 0b_0000001000000100, - 0b_0000001111110100, - 0b_0000000000010100, - 0b_0000000000010100, - 0b_0000000000010100, - 0b_0000000000011100, + 0b_0000000000001110, + 0b_0000000000010010, + 0b_0000000000101010, + 0b_0000000001011010, + 0b_0000000010101010, + 0b_0000000101111010, + 0b_0000000100000010, + 0b_0000000111111010, + 0b_0000000000001010, + 0b_0000000000001010, + 0b_0000000000001010, + 0b_0000000000001110, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '5', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000111111000, - 0b_0000000100001000, - 0b_0000000100000000, + 0b_0000000011111100, + 0b_0000000010000100, 0b_0000000010000000, 0b_0000000001000000, - 0b_0000000000110000, - 0b_0000000000001000, - 0b_0000000000001000, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000000100000, + 0b_0000000000011000, + 0b_0000000000000100, + 0b_0000000000000100, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -375,39 +389,39 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000001111111100, - 0b_0000001000000100, - 0b_0000001011110100, - 0b_0000001010011100, - 0b_0000000101000000, - 0b_0000000010110000, - 0b_0000000001001000, - 0b_0000000000110100, - 0b_0000001100010100, - 0b_0000001011110100, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000111111110, + 0b_0000000100000010, + 0b_0000000101111010, + 0b_0000000101001110, + 0b_0000000010100000, + 0b_0000000001011000, + 0b_0000000000100100, + 0b_0000000000011010, + 0b_0000000110001010, + 0b_0000000101111010, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '6', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000011110000, - 0b_0000000100001000, - 0b_0000000100000000, - 0b_0000000100000000, - 0b_0000000100000000, - 0b_0000000111110000, - 0b_0000000100001000, - 0b_0000000100001000, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000001111000, + 0b_0000000010000100, + 0b_0000000010000000, + 0b_0000000010000000, + 0b_0000000010000000, + 0b_0000000011111000, + 0b_0000000010000100, + 0b_0000000010000100, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -415,39 +429,39 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000011110000, - 0b_0000000100001000, - 0b_0000001011110100, - 0b_0000001010011100, - 0b_0000001010000000, - 0b_0000001011110000, - 0b_0000001000001000, - 0b_0000001011110100, - 0b_0000001010010100, - 0b_0000001011110100, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000001111000, + 0b_0000000010000100, + 0b_0000000101111010, + 0b_0000000101001110, + 0b_0000000101000000, + 0b_0000000101111000, + 0b_0000000100000100, + 0b_0000000101111010, + 0b_0000000101001010, + 0b_0000000101111010, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '7', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000001111111000, - 0b_0000001000001000, + 0b_0000000111111100, + 0b_0000000100000100, + 0b_0000000000000100, + 0b_0000000000001000, + 0b_0000000000001000, 0b_0000000000001000, 0b_0000000000010000, 0b_0000000000010000, - 0b_0000000000010000, - 0b_0000000000100000, + 0b_0000000000110000, 0b_0000000000100000, - 0b_0000000001100000, - 0b_0000000001000000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -455,39 +469,39 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000011111111100, - 0b_0000010000000100, - 0b_0000010111110100, - 0b_0000011100010100, - 0b_0000000000101000, + 0b_0000001111111110, + 0b_0000001000000010, + 0b_0000001011111010, + 0b_0000001110001010, + 0b_0000000000010100, + 0b_0000000000010100, + 0b_0000000000010100, 0b_0000000000101000, 0b_0000000000101000, + 0b_0000000001001000, 0b_0000000001010000, - 0b_0000000001010000, - 0b_0000000010010000, - 0b_0000000010100000, - 0b_0000000011100000, + 0b_0000000001110000, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '8', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000011110000, - 0b_0000000100001000, - 0b_0000000100001000, - 0b_0000000100001000, - 0b_0000000011110000, - 0b_0000000010010000, - 0b_0000000100001000, - 0b_0000000100001000, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000001111000, + 0b_0000000010000100, + 0b_0000000010000100, + 0b_0000000010000100, + 0b_0000000001111000, + 0b_0000000001001000, + 0b_0000000010000100, + 0b_0000000010000100, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -495,39 +509,39 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000011110000, - 0b_0000000100001000, - 0b_0000001011110100, - 0b_0000001010010100, - 0b_0000001011110100, - 0b_0000000100001000, - 0b_0000000101101000, - 0b_0000001010010100, - 0b_0000001010010100, - 0b_0000001011110100, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000001111000, + 0b_0000000010000100, + 0b_0000000101111010, + 0b_0000000101001010, + 0b_0000000101111010, + 0b_0000000010000100, + 0b_0000000010110100, + 0b_0000000101001010, + 0b_0000000101001010, + 0b_0000000101111010, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 12, + width: 10, sym: '9', bits: [ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000011110000, - 0b_0000000100001000, - 0b_0000000100001000, - 0b_0000000100001000, - 0b_0000000011111000, - 0b_0000000000001000, - 0b_0000000000001000, - 0b_0000000000001000, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000001111000, + 0b_0000000010000100, + 0b_0000000010000100, + 0b_0000000010000100, + 0b_0000000001111100, + 0b_0000000000000100, + 0b_0000000000000100, + 0b_0000000000000100, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -535,24 +549,24 @@ const OSD_GLYPHS: &[Glyph] = &[ mask: [ 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000011110000, - 0b_0000000100001000, - 0b_0000001011110100, - 0b_0000001010010100, - 0b_0000001011110100, - 0b_0000000100000100, - 0b_0000000011110100, - 0b_0000000000010100, - 0b_0000001110010100, - 0b_0000001011110100, - 0b_0000000100001000, - 0b_0000000011110000, + 0b_0000000001111000, + 0b_0000000010000100, + 0b_0000000101111010, + 0b_0000000101001010, + 0b_0000000101111010, + 0b_0000000010000010, + 0b_0000000001111010, + 0b_0000000000001010, + 0b_0000000111001010, + 0b_0000000101111010, + 0b_0000000010000100, + 0b_0000000001111000, 0b_0000000000000000, 0b_0000000000000000 ], }, Glyph { - width: 9, + width: 7, sym: ':', bits: [ 0b_0000000000000000, @@ -560,14 +574,14 @@ const OSD_GLYPHS: &[Glyph] = &[ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000000111000, - 0b_0000000000111000, + 0b_0000000000011100, + 0b_0000000000011100, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000000111000, - 0b_0000000000111000, + 0b_0000000000011100, + 0b_0000000000011100, 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000 @@ -577,16 +591,56 @@ const OSD_GLYPHS: &[Glyph] = &[ 0b_0000000000000000, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000000111000, - 0b_0000000001111100, - 0b_0000000001111100, - 0b_0000000000111000, + 0b_0000000000011100, + 0b_0000000000111110, + 0b_0000000000111110, + 0b_0000000000011100, 0b_0000000000000000, 0b_0000000000000000, - 0b_0000000000111000, - 0b_0000000001111100, - 0b_0000000001111100, - 0b_0000000000111000, + 0b_0000000000011100, + 0b_0000000000111110, + 0b_0000000000111110, + 0b_0000000000011100, + 0b_0000000000000000, + 0b_0000000000000000 + ], + }, + Glyph { + width: 9, + sym: '/', + bits: [ + 0b_0000000000000000, + 0b_0000000000000000, + 0b_0000000000000000, + 0b_0000000000000100, + 0b_0000000000000100, + 0b_0000000000001000, + 0b_0000000000001000, + 0b_0000000000010000, + 0b_0000000000010000, + 0b_0000000000100000, + 0b_0000000000100000, + 0b_0000000001000000, + 0b_0000000001000000, + 0b_0000000000000000, + 0b_0000000000000000, + 0b_0000000000000000 + ], + mask: [ + 0b_0000000000000000, + 0b_0000000000000000, + 0b_0000000000001110, + 0b_0000000000001010, + 0b_0000000000001010, + 0b_0000000000010100, + 0b_0000000000010100, + 0b_0000000000101000, + 0b_0000000000101000, + 0b_0000000001010000, + 0b_0000000001010000, + 0b_0000000010100000, + 0b_0000000010100000, + 0b_0000000011100000, 0b_0000000000000000, 0b_0000000000000000 ], -- 2.39.5