use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError};
use fluxframe_core::frame::{FrameBuffer, FrameMeta, PixelFormat, VideoFrame};
use fluxframe_core::{FluxError, IdleConfig, IdlePlaceholderKind};
#[cfg(feature = "image-fill")]
use fluxframe_effects::processing::image_loader::{ImageLoadError, decode_rgb_bounded};
const BT601_KR_Q8: i32 = 77;
const BT601_KG_Q8: i32 = 150;
const BT601_KB_Q8: i32 = 29;
const BT601_CB_R_Q8: i32 = -43;
const BT601_CB_G_Q8: i32 = -85;
const BT601_CB_B_Q8: i32 = 128;
const BT601_CR_R_Q8: i32 = 128;
const BT601_CR_G_Q8: i32 = -107;
const BT601_CR_B_Q8: i32 = -21;
const BT601_ROUND_Q8: i32 = 128;
const BT601_CHROMA_BIAS: i32 = 128;
pub(crate) trait Placeholder: Send + Sync {
fn render(&self, width: u32, height: u32, format: PixelFormat)
-> Result<VideoFrame, FluxError>;
}
pub(crate) fn build(
cfg: &IdleConfig,
width: u32,
height: u32,
) -> Result<Box<dyn Placeholder>, FluxError> {
if width == 0 || height == 0 {
return Err(FluxError::Config {
reason: format!("placeholder asked for zero-sized frame {width}x{height}"),
hint: None,
});
}
match cfg.placeholder {
IdlePlaceholderKind::Color => Ok(Box::new(ColorPlaceholder::new(
cfg.placeholder_rgb,
width,
height,
))),
IdlePlaceholderKind::Image => {
#[cfg(feature = "image-fill")]
{
let path = cfg
.placeholder_path
.as_ref()
.ok_or_else(|| FluxError::Config {
reason: "idle.placeholder = \"image\" requires idle.placeholder_path"
.into(),
hint: Some("set idle.placeholder_path to a PNG/JPEG file".into()),
})?;
ImagePlaceholder::load(path.as_path(), width, height)
.map(|p| Box::new(p) as Box<dyn Placeholder>)
}
#[cfg(not(feature = "image-fill"))]
{
Err(FluxError::Config {
reason: "image placeholders require the `image-fill` cargo feature".into(),
hint: Some(
"rebuild with --features image-fill or switch idle.placeholder to \"color\""
.into(),
),
})
}
}
_ => Err(FluxError::Config {
reason: format!("unsupported idle placeholder kind {:?}", cfg.placeholder),
hint: Some("update fluxframe-cli to handle the new IdlePlaceholderKind variant".into()),
}),
}
}
pub(crate) struct ColorPlaceholder {
width: u32,
height: u32,
rgb: Vec<u8>,
cache: ConvertedCache,
}
impl ColorPlaceholder {
fn new(rgb: [u8; 3], width: u32, height: u32) -> Self {
let pixels = (width as usize) * (height as usize);
let mut buf = vec![0u8; pixels * 3];
for chunk in buf.chunks_exact_mut(3) {
chunk.copy_from_slice(&rgb);
}
Self {
width,
height,
rgb: buf,
cache: ConvertedCache::default(),
}
}
}
impl Placeholder for ColorPlaceholder {
fn render(
&self,
width: u32,
height: u32,
format: PixelFormat,
) -> Result<VideoFrame, FluxError> {
ensure_dims_match(self.width, self.height, width, height)?;
let bytes = self
.cache
.get_or_convert(&self.rgb, width, height, format)?;
wrap_into_frame(FrameBuffer::Shared(bytes), width, height, format)
}
}
#[cfg(feature = "image-fill")]
pub(crate) struct ImagePlaceholder {
width: u32,
height: u32,
rgb: Vec<u8>,
cache: ConvertedCache,
}
#[cfg(feature = "image-fill")]
impl ImagePlaceholder {
fn load(path: &std::path::Path, width: u32, height: u32) -> Result<Self, FluxError> {
let decoded = decode_rgb_bounded(path).map_err(|e: ImageLoadError| FluxError::Config {
reason: e.to_string(),
hint: Some("ensure the path exists and contains a valid PNG/JPEG".into()),
})?;
let src = image::RgbImage::from_raw(decoded.width, decoded.height, decoded.data).expect(
"decode_rgb_bounded guarantees data.len() == width * height * 3 and non-zero dims",
);
let resized =
image::imageops::resize(&src, width, height, image::imageops::FilterType::Triangle);
Ok(Self {
width,
height,
rgb: resized.into_raw(),
cache: ConvertedCache::default(),
})
}
}
#[cfg(feature = "image-fill")]
impl Placeholder for ImagePlaceholder {
fn render(
&self,
width: u32,
height: u32,
format: PixelFormat,
) -> Result<VideoFrame, FluxError> {
ensure_dims_match(self.width, self.height, width, height)?;
let bytes = self
.cache
.get_or_convert(&self.rgb, width, height, format)?;
wrap_into_frame(FrameBuffer::Shared(bytes), width, height, format)
}
}
type ConvertedMap = HashMap<(u32, u32, PixelFormat), Arc<[u8]>>;
#[derive(Default)]
struct ConvertedCache {
map: Mutex<ConvertedMap>,
}
impl ConvertedCache {
fn get_or_convert(
&self,
rgb: &[u8],
width: u32,
height: u32,
format: PixelFormat,
) -> Result<Arc<[u8]>, FluxError> {
let key = (width, height, format);
let mut map = self.map.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(bytes) = map.get(&key) {
return Ok(Arc::clone(bytes));
}
let bytes: Arc<[u8]> = Arc::from(convert_rgb_to(rgb, width, height, format)?);
map.insert(key, Arc::clone(&bytes));
Ok(bytes)
}
}
fn ensure_dims_match(
cached_w: u32,
cached_h: u32,
asked_w: u32,
asked_h: u32,
) -> Result<(), FluxError> {
if cached_w == asked_w && cached_h == asked_h {
Ok(())
} else {
Err(FluxError::Config {
reason: format!(
"placeholder built for {cached_w}x{cached_h}, asked for {asked_w}x{asked_h}"
),
hint: Some("supervisor must rebuild the placeholder when output dims change".into()),
})
}
}
fn wrap_into_frame(
buffer: FrameBuffer,
width: u32,
height: u32,
format: PixelFormat,
) -> Result<VideoFrame, FluxError> {
VideoFrame::new_packed(buffer, width, height, format, FrameMeta::default()).ok_or_else(|| {
FluxError::Config {
reason: format!(
"placeholder frame construction failed for {format:?} {width}x{height}"
),
hint: None,
}
})
}
fn convert_rgb_to(
rgb: &[u8],
width: u32,
height: u32,
target: PixelFormat,
) -> Result<Vec<u8>, FluxError> {
let pixels = (width as usize) * (height as usize);
debug_assert_eq!(rgb.len(), pixels * 3, "RGB source has wrong length");
match target {
PixelFormat::Rgb => Ok(rgb.to_vec()),
PixelFormat::Bgr => Ok(rgb_to_bgr(rgb, pixels)),
PixelFormat::Rgba => Ok(rgb_to_rgba(rgb, pixels)),
PixelFormat::Gray8 => Ok(rgb_to_gray8(rgb, pixels)),
PixelFormat::Yuy2 => rgb_to_yuy2(rgb, width, height),
PixelFormat::Nv12 => Err(FluxError::Config {
reason: "idle placeholder does not support NV12 appsrc format".into(),
hint: Some(
"set input.format to RGB / RGBA / BGR / YUY2 / GRAY8 — \
videoconvert handles the NV12 sink leg automatically"
.into(),
),
}),
_ => Err(FluxError::Config {
reason: format!("idle placeholder does not support pixel format {target:?}"),
hint: Some("add a converter or change input.format".into()),
}),
}
}
fn rgb_to_bgr(rgb: &[u8], pixels: usize) -> Vec<u8> {
let mut out = vec![0u8; pixels * 3];
for (src, dst) in rgb.chunks_exact(3).zip(out.chunks_exact_mut(3)) {
dst[0] = src[2];
dst[1] = src[1];
dst[2] = src[0];
}
out
}
fn rgb_to_rgba(rgb: &[u8], pixels: usize) -> Vec<u8> {
let mut out = vec![0u8; pixels * 4];
for (src, dst) in rgb.chunks_exact(3).zip(out.chunks_exact_mut(4)) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = 0xFF;
}
out
}
fn rgb_to_gray8(rgb: &[u8], pixels: usize) -> Vec<u8> {
let mut out = vec![0u8; pixels];
for (src, dst) in rgb.chunks_exact(3).zip(out.iter_mut()) {
let r = i32::from(src[0]);
let g = i32::from(src[1]);
let b = i32::from(src[2]);
let y = (BT601_KR_Q8 * r + BT601_KG_Q8 * g + BT601_KB_Q8 * b + BT601_ROUND_Q8) >> 8;
*dst = clamp_u8(y);
}
out
}
fn rgb_to_yuy2(rgb: &[u8], width: u32, height: u32) -> Result<Vec<u8>, FluxError> {
if width % 2 != 0 {
return Err(FluxError::Config {
reason: format!("YUY2 placeholder requires even width, got {width}"),
hint: None,
});
}
let pixels = (width as usize) * (height as usize);
let mut out = vec![0u8; pixels * 2];
let row_rgb = (width as usize) * 3;
let row_yuy2 = (width as usize) * 2;
for (rgb_row, yuy2_row) in rgb
.chunks_exact(row_rgb)
.zip(out.chunks_exact_mut(row_yuy2))
{
for (rgb_pair, yuy2_word) in rgb_row.chunks_exact(6).zip(yuy2_row.chunks_exact_mut(4)) {
let (y0, u0, v0) = rgb_to_ycbcr(rgb_pair[0], rgb_pair[1], rgb_pair[2]);
let (y1, u1, v1) = rgb_to_ycbcr(rgb_pair[3], rgb_pair[4], rgb_pair[5]);
let u = ((u32::from(u0) + u32::from(u1) + 1) >> 1) as u8;
let v = ((u32::from(v0) + u32::from(v1) + 1) >> 1) as u8;
yuy2_word[0] = y0;
yuy2_word[1] = u;
yuy2_word[2] = y1;
yuy2_word[3] = v;
}
}
Ok(out)
}
fn rgb_to_ycbcr(r: u8, g: u8, b: u8) -> (u8, u8, u8) {
let r = i32::from(r);
let g = i32::from(g);
let b = i32::from(b);
let y = (BT601_KR_Q8 * r + BT601_KG_Q8 * g + BT601_KB_Q8 * b + BT601_ROUND_Q8) >> 8;
let cb = ((BT601_CB_R_Q8 * r + BT601_CB_G_Q8 * g + BT601_CB_B_Q8 * b + BT601_ROUND_Q8) >> 8)
+ BT601_CHROMA_BIAS;
let cr = ((BT601_CR_R_Q8 * r + BT601_CR_G_Q8 * g + BT601_CR_B_Q8 * b + BT601_ROUND_Q8) >> 8)
+ BT601_CHROMA_BIAS;
(clamp_u8(y), clamp_u8(cb), clamp_u8(cr))
}
#[inline]
fn clamp_u8(v: i32) -> u8 {
v.clamp(0, 255) as u8
}
#[cfg(test)]
mod tests {
use super::*;
fn color_cfg(rgb: [u8; 3]) -> IdleConfig {
IdleConfig {
enabled: true,
placeholder_rgb: rgb,
..IdleConfig::default()
}
}
#[test]
fn color_renders_rgb() {
let p = build(&color_cfg([10, 20, 30]), 4, 2).expect("build");
let frame = p.render(4, 2, PixelFormat::Rgb).expect("render rgb");
assert_eq!(frame.width, 4);
assert_eq!(frame.height, 2);
assert_eq!(frame.format, PixelFormat::Rgb);
let data = frame.data.as_slice();
assert_eq!(data.len(), 4 * 2 * 3);
for chunk in data.chunks_exact(3) {
assert_eq!(chunk, [10, 20, 30]);
}
}
#[test]
fn color_converts_to_bgr() {
let p = build(&color_cfg([10, 20, 30]), 2, 1).expect("build");
let frame = p.render(2, 1, PixelFormat::Bgr).expect("render bgr");
let data = frame.data.as_slice();
for chunk in data.chunks_exact(3) {
assert_eq!(chunk, [30, 20, 10]);
}
}
#[test]
fn color_converts_to_rgba_with_full_alpha() {
let p = build(&color_cfg([10, 20, 30]), 2, 1).expect("build");
let frame = p.render(2, 1, PixelFormat::Rgba).expect("render rgba");
let data = frame.data.as_slice();
for chunk in data.chunks_exact(4) {
assert_eq!(chunk, [10, 20, 30, 0xFF]);
}
}
#[test]
fn color_converts_to_yuy2() {
let p = build(&color_cfg([255, 0, 0]), 2, 1).expect("build");
let frame = p.render(2, 1, PixelFormat::Yuy2).expect("render yuy2");
let data = frame.data.as_slice();
assert_eq!(data.len(), 4);
assert_eq!(data[0], data[2], "Y plane is uniform for a solid fill");
}
#[test]
fn color_rejects_yuy2_with_odd_width() {
let p = build(&color_cfg([255, 0, 0]), 3, 1).expect("build");
match p.render(3, 1, PixelFormat::Yuy2) {
Ok(_) => panic!("odd width must be rejected"),
Err(e) => assert!(format!("{e}").contains("even width"), "{e}"),
}
}
#[test]
fn color_converts_to_gray8() {
let p = build(&color_cfg([255, 255, 255]), 2, 2).expect("build");
let frame = p.render(2, 2, PixelFormat::Gray8).expect("render gray");
let data = frame.data.as_slice();
assert_eq!(data.len(), 4);
for v in data {
assert!(*v > 250, "luma {v} should be near 255");
}
}
#[test]
fn color_rejects_nv12() {
let p = build(&color_cfg([10, 20, 30]), 4, 2).expect("build");
match p.render(4, 2, PixelFormat::Nv12) {
Ok(_) => panic!("nv12 should be rejected"),
Err(e) => assert!(format!("{e}").contains("NV12"), "{e}"),
}
}
#[test]
fn build_rejects_zero_dimensions() {
match build(&color_cfg([0, 0, 0]), 0, 10) {
Ok(_) => panic!("zero width must be rejected"),
Err(e) => assert!(format!("{e}").contains("zero-sized")),
}
}
#[test]
fn build_color_placeholder_from_config() {
let p = build(&color_cfg([1, 2, 3]), 2, 1).expect("build color");
let frame = p.render(2, 1, PixelFormat::Rgb).expect("render");
assert_eq!(frame.data.as_slice(), [1, 2, 3, 1, 2, 3]);
}
#[cfg(feature = "image-fill")]
#[test]
fn build_image_placeholder_requires_path() {
let cfg = IdleConfig {
enabled: true,
placeholder: IdlePlaceholderKind::Image,
placeholder_path: None,
..IdleConfig::default()
};
match build(&cfg, 4, 2) {
Ok(_) => panic!("image without path must fail"),
Err(e) => assert!(format!("{e}").contains("placeholder_path")),
}
}
#[cfg(not(feature = "image-fill"))]
#[test]
fn build_image_placeholder_rejects_without_image_fill_feature() {
let cfg = IdleConfig {
enabled: true,
placeholder: IdlePlaceholderKind::Image,
placeholder_path: Some(std::path::PathBuf::from("/tmp/bg.png")),
..IdleConfig::default()
};
match build(&cfg, 4, 2) {
Ok(_) => panic!("image kind must fail without image-fill feature"),
Err(e) => {
let msg = format!("{e}");
assert!(
msg.contains("image-fill"),
"error must mention the feature flag, got: {msg}"
);
}
}
}
#[test]
fn repeated_render_is_byte_identical_via_cache() {
let p = build(&color_cfg([10, 20, 30]), 4, 2).expect("build");
let first = p.render(4, 2, PixelFormat::Rgb).expect("render 1");
let second = p.render(4, 2, PixelFormat::Rgb).expect("render 2");
assert_eq!(first.data.as_slice(), second.data.as_slice());
let bgr = p.render(4, 2, PixelFormat::Bgr).expect("render bgr");
assert_eq!(bgr.format, PixelFormat::Bgr);
for chunk in bgr.data.as_slice().chunks_exact(3) {
assert_eq!(chunk, [30, 20, 10]);
}
}
#[test]
fn render_rejects_dim_mismatch() {
let p = build(&color_cfg([1, 2, 3]), 2, 1).expect("build");
match p.render(4, 1, PixelFormat::Rgb) {
Ok(_) => panic!("dim mismatch must be rejected"),
Err(e) => {
let msg = format!("{e}");
assert!(
msg.contains("built for 2x1") && msg.contains("asked for 4x1"),
"expected dim-mismatch reason, got: {msg}"
);
}
}
}
}