From 7460b53e64c4ff4678b310789fa9a3c4f5096324 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sat, 3 Jun 2023 09:45:13 +0200 Subject: [PATCH] cinepakenc: factor out ELBG quantisation This will be useful in the upcoming fast VQ mode. --- nihav-commonfmt/src/codecs/cinepakenc.rs | 28 +++++++++++------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/nihav-commonfmt/src/codecs/cinepakenc.rs b/nihav-commonfmt/src/codecs/cinepakenc.rs index 99194ce..897bd3e 100644 --- a/nihav-commonfmt/src/codecs/cinepakenc.rs +++ b/nihav-commonfmt/src/codecs/cinepakenc.rs @@ -265,6 +265,16 @@ fn patch_size(bw: &mut ByteWriter, pos: u64) -> EncoderResult<()> { Ok(()) } +fn elbg_quant(entries: &[YUVCode], codebook: &mut [YUVCode]) -> usize { + let cb_len = quantise_median_cut::(entries, codebook); + if cb_len < codebook.len() { + cb_len + } else { + let mut elbg: ELBG = ELBG::new(codebook); + elbg.quantise(entries, codebook) + } +} + impl CinepakEncoder { fn new() -> Self { Self { @@ -604,24 +614,12 @@ impl CinepakEncoder { fn quant_vectors(&mut self) { match self.qmode { QuantMode::ELBG => { - let v1_len = quantise_median_cut::(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]); - let v4_len = if !self.force_v1 { - quantise_median_cut::(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]) + self.v1_len = elbg_quant(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]); + self.v4_len = if !self.force_v1 { + elbg_quant(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]) } else { 0 }; - self.v1_len = if v1_len < 256 { - v1_len - } else { - let mut elbg_v1: ELBG = ELBG::new(&self.v1_cur_cb[self.cur_strip]); - elbg_v1.quantise(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]) - }; - self.v4_len = if v4_len < 256 { - v4_len - } else { - let mut elbg_v4: ELBG = ELBG::new(&self.v4_cur_cb[self.cur_strip]); - elbg_v4.quantise(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]) - }; }, QuantMode::MedianCut => { self.v1_len = quantise_median_cut::(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]); -- 2.30.2