introduce a way for encoder to manifest its capabilities
[nihav.git] / nihav-commonfmt / src / codecs / cinepakenc.rs
index 38f7cecb5c0365e28871caf8e75cf0210af12f43..f4ac9b24fd375c52b4f5717fcb4aac1fbd3e2e42 100644 (file)
@@ -169,7 +169,7 @@ impl MaskWriter {
         }
     }
     fn reset(&mut self) {
-        self.masks.truncate(0);
+        self.masks.clear();
         self.mask = 0;
         self.pos = 0;
     }
@@ -237,6 +237,7 @@ struct CinepakEncoder {
     qmode:      QuantMode,
     quality:    u8,
     nstrips:    usize,
+    force_v1:   bool,
     cur_strip:  usize,
     v1_entries: Vec<YUVCode>,
     v4_entries: Vec<YUVCode>,
@@ -277,6 +278,7 @@ impl CinepakEncoder {
             key_int:    25,
             quality:    0,
             nstrips:    2,
+            force_v1:   false,
             cur_strip:  0,
             v1_entries: Vec::new(),
             v4_entries: Vec::new(),
@@ -303,8 +305,8 @@ impl CinepakEncoder {
         let mut voff = in_frm.get_offset(2) + start / 2 * vstride;
         let (width, _) = in_frm.get_dimensions(0);
         let data = in_frm.get_data();
-        self.v1_entries.truncate(0);
-        self.v4_entries.truncate(0);
+        self.v1_entries.clear();
+        self.v4_entries.clear();
         for _ in (start..end).step_by(4) {
             for x in (0..width).step_by(4) {
                 let mut yblk = [0; 16];
@@ -360,18 +362,18 @@ impl CinepakEncoder {
         }
         (idx as u8, min_dist)
     }
-    fn can_update_cb(new_cb: &[YUVCode; 256], old_cb: &[YUVCode; 256], cb_size: usize) -> bool {
+    fn can_update_cb(new_cb: &[YUVCode], old_cb: &[YUVCode], cb_size: usize) -> bool {
         let mut skip_count = 0;
         for (new, old) in new_cb.iter().zip(old_cb.iter()) {
             if new == old {
                 skip_count += 1;
             }
         }
-        let full_size = cb_size * 256;
-        let upd_size = cb_size * (256 - skip_count) + 64;
+        let full_size = cb_size * new_cb.len();
+        let upd_size = cb_size * (new_cb.len() - skip_count) + (new_cb.len() + 31) / 32 * 4;
         upd_size < full_size
     }
-    fn write_cb(bw: &mut ByteWriter, mut id: u8, new_cb: &[YUVCode; 256], old_cb: &[YUVCode; 256], grayscale: bool, update: bool) -> EncoderResult<()> {
+    fn write_cb(bw: &mut ByteWriter, mut id: u8, new_cb: &[YUVCode], old_cb: &[YUVCode], grayscale: bool, update: bool, num_elem: usize) -> EncoderResult<()> {
         if grayscale {
             id |= 4;
         }
@@ -382,7 +384,7 @@ impl CinepakEncoder {
         bw.write_u24be(0)?;
         let chunk_pos = bw.tell();
         if !update {
-            for entry in new_cb.iter() {
+            for entry in new_cb.iter().take(num_elem) {
                 bw.write_buf(&entry.y)?;
                 if !grayscale {
                     bw.write_byte(entry.u ^ 0x80)?;
@@ -390,8 +392,8 @@ impl CinepakEncoder {
                 }
             }
         } else {
-            let mut end = 256;
-            for (i, (ncw, ocw)) in new_cb.iter().rev().zip(old_cb.iter().rev()).enumerate() {
+            let mut end = num_elem;
+            for (i, (ncw, ocw)) in new_cb.iter().zip(old_cb.iter()).enumerate().take(num_elem).rev() {
                 if ncw == ocw {
                     end = i;
                 } else {
@@ -530,7 +532,7 @@ impl CinepakEncoder {
         }
     }
     fn calc_skip_dist(&mut self, in_frm: &NAVideoBuffer<u8>, start: usize, end: usize) {
-        self.skip_dist.truncate(0);
+        self.skip_dist.clear();
         if let Some(ref ref_frm) = self.lastfrm {
             let rystride  = ref_frm.get_stride(0);
             let mut ryoff = ref_frm.get_offset(0) + start * rystride;
@@ -615,19 +617,35 @@ impl CinepakEncoder {
                 }
 
                 self.v1_len = elbg_v1.quantise(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]);
-                self.v4_len = elbg_v4.quantise(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]);
+                self.v4_len = if !self.force_v1 { elbg_v4.quantise(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]) } else { 0 };
             },
             QuantMode::Hybrid => {
-                quantise_median_cut::<YUVCode, YUVCodeSum>(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]);
-                quantise_median_cut::<YUVCode, YUVCodeSum>(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]);
-                let mut elbg_v1: ELBG<YUVCode, YUVCodeSum> = ELBG::new(&self.v1_cur_cb[self.cur_strip]);
-                let mut elbg_v4: ELBG<YUVCode, YUVCodeSum> = ELBG::new(&self.v4_cur_cb[self.cur_strip]);
-                self.v1_len = elbg_v1.quantise(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]);
-                self.v4_len = elbg_v4.quantise(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]);
+                let v1_len = quantise_median_cut::<YUVCode, YUVCodeSum>(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]);
+                let v4_len = if !self.force_v1 {
+                        quantise_median_cut::<YUVCode, YUVCodeSum>(&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<YUVCode, YUVCodeSum> = 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<YUVCode, YUVCodeSum> = 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::<YUVCode, YUVCodeSum>(&self.v1_entries, &mut self.v1_cur_cb[self.cur_strip]);
-                self.v4_len = quantise_median_cut::<YUVCode, YUVCodeSum>(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]);
+                if !self.force_v1 {
+                    self.v4_len = quantise_median_cut::<YUVCode, YUVCodeSum>(&self.v4_entries, &mut self.v4_cur_cb[self.cur_strip]);
+                } else {
+                    self.v4_len = 0;
+                }
             },
         };
 
@@ -677,13 +695,13 @@ impl CinepakEncoder {
                 }
             }
 
-            self.v1_idx.truncate(0);
-            self.v4_idx.truncate(0);
+            self.v1_idx.clear();
+            self.v4_idx.clear();
             self.masks.reset();
 
             for (v1_entry, v4_entries) in self.v1_entries.iter().zip(self.v4_entries.chunks(4)) {
                 let (v1_idx, v1_dist) = Self::find_nearest(&self.v1_cur_cb[self.cur_strip][..self.v1_len], *v1_entry);
-                if v1_dist == 0 {
+                if v1_dist == 0 || self.force_v1 {
                     self.masks.put_v1();
                     self.v1_idx.push(v1_idx);
                     continue;
@@ -708,8 +726,8 @@ impl CinepakEncoder {
             let mut is_intra_strip = start_line == 0;
             let (upd_v1, upd_v4) = if !is_intra_strip {
                     let cb_size = if self.grayscale { 4 } else { 6 };
-                    (Self::can_update_cb(&self.v1_cur_cb[self.cur_strip], &self.v1_cb[self.cur_strip], cb_size),
-                     Self::can_update_cb(&self.v4_cur_cb[self.cur_strip], &self.v4_cb[self.cur_strip], cb_size))
+                    (Self::can_update_cb(&self.v1_cur_cb[self.cur_strip][..self.v1_len], &self.v1_cb[self.cur_strip][..self.v1_len], cb_size),
+                     Self::can_update_cb(&self.v4_cur_cb[self.cur_strip][..self.v4_len], &self.v4_cb[self.cur_strip][..self.v4_len], cb_size))
                 } else {
                     (false, false)
                 };
@@ -724,8 +742,8 @@ impl CinepakEncoder {
             bw.write_u16be((end_line - start_line) as u16)?;
             bw.write_u16be(width as u16)?;
 
-            Self::write_cb(bw, 0x20, &self.v4_cur_cb[self.cur_strip], &self.v4_cb[self.cur_strip], self.grayscale, upd_v4)?;
-            Self::write_cb(bw, 0x22, &self.v1_cur_cb[self.cur_strip], &self.v1_cb[self.cur_strip], self.grayscale, upd_v1)?;
+            Self::write_cb(bw, 0x20, &self.v4_cur_cb[self.cur_strip], &self.v4_cb[self.cur_strip], self.grayscale, upd_v4, self.v4_len)?;
+            Self::write_cb(bw, 0x22, &self.v1_cur_cb[self.cur_strip], &self.v1_cb[self.cur_strip], self.grayscale, upd_v1, self.v1_len)?;
 
             self.render_stripe(true, start_line, end_line);
 
@@ -810,8 +828,8 @@ impl CinepakEncoder {
                 }
             }
 
-            self.v1_idx.truncate(0);
-            self.v4_idx.truncate(0);
+            self.v1_idx.clear();
+            self.v4_idx.clear();
             self.masks.reset();
 
             let mut skip_iter = self.skip_dist.iter();
@@ -828,7 +846,7 @@ impl CinepakEncoder {
                 } else {
                     self.masks.put_inter(false);
                 }
-                if v1_dist == 0 {
+                if v1_dist == 0 || self.force_v1 {
                     self.masks.put_v1();
                     self.v1_idx.push(v1_idx);
                     continue;
@@ -852,8 +870,8 @@ impl CinepakEncoder {
 
             let (upd_v1, upd_v4) = {
                     let cb_size = if self.grayscale { 4 } else { 6 };
-                    (Self::can_update_cb(&self.v1_cur_cb[self.cur_strip], &self.v1_cb[self.cur_strip], cb_size),
-                     Self::can_update_cb(&self.v4_cur_cb[self.cur_strip], &self.v4_cb[self.cur_strip], cb_size))
+                    (Self::can_update_cb(&self.v1_cur_cb[self.cur_strip][..self.v1_len], &self.v1_cb[self.cur_strip][..self.v1_len], cb_size),
+                     Self::can_update_cb(&self.v4_cur_cb[self.cur_strip][..self.v4_len], &self.v4_cb[self.cur_strip][..self.v4_len], cb_size))
                 };
             bw.write_byte(0x11)?;
             bw.write_u24be(0)?; // strip size
@@ -863,8 +881,8 @@ impl CinepakEncoder {
             bw.write_u16be((end_line - start_line) as u16)?;
             bw.write_u16be(width as u16)?;
 
-            Self::write_cb(bw, 0x20, &self.v4_cur_cb[self.cur_strip], &self.v4_cb[self.cur_strip], self.grayscale, upd_v4)?;
-            Self::write_cb(bw, 0x22, &self.v1_cur_cb[self.cur_strip], &self.v1_cb[self.cur_strip], self.grayscale, upd_v1)?;
+            Self::write_cb(bw, 0x20, &self.v4_cur_cb[self.cur_strip], &self.v4_cb[self.cur_strip], self.grayscale, upd_v4, self.v4_len)?;
+            Self::write_cb(bw, 0x22, &self.v1_cur_cb[self.cur_strip], &self.v1_cb[self.cur_strip], self.grayscale, upd_v1, self.v1_len)?;
 
             self.render_stripe(false, start_line, end_line);
 
@@ -883,7 +901,7 @@ impl CinepakEncoder {
             let mut skip = true;
             for mask in self.masks.masks.iter() {
                 bw.write_u32be(*mask)?;
-                if *mask == 0 { continue; }
+                if *mask == 0 && skip { continue; }
                 let mut bit = 1 << 31;
                 while bit > 0 {
                     if skip {
@@ -925,9 +943,10 @@ impl NAEncoder for CinepakEncoder {
     fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> {
         match encinfo.format {
             NACodecTypeInfo::None => {
-                let mut ofmt = EncodeParameters::default();
-                ofmt.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, true, YUV420_FORMAT));
-                Ok(ofmt)
+                Ok(EncodeParameters {
+                        format: NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, true, YUV420_FORMAT)),
+                        ..Default::default()
+                    })
             },
             NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
             NACodecTypeInfo::Video(vinfo) => {
@@ -939,6 +958,7 @@ impl NAEncoder for CinepakEncoder {
             }
         }
     }
+    fn get_capabilities(&self) -> u64 { 0 }
     fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> {
         match encinfo.format {
             NACodecTypeInfo::None => Err(EncoderError::FormatError),
@@ -1032,6 +1052,9 @@ const ENCODER_OPTS: &[NAOptionDefinition] = &[
     NAOptionDefinition {
         name: "quant_mode", description: "Quantisation mode",
         opt_type: NAOptionDefinitionType::String(Some(&["elbg", "hybrid", "mediancut"])) },
+    NAOptionDefinition {
+        name: "force_v1", description: "Force coarse (V1-only) mode",
+        opt_type: NAOptionDefinitionType::Bool },
 ];
 
 impl NAOptionHandler for CinepakEncoder {
@@ -1052,8 +1075,8 @@ impl NAOptionHandler for CinepakEncoder {
                             }
                         },
                         "quant_mode" => {
-                            if let NAValue::String(ref str) = option.value {
-                                match str.as_str() {
+                            if let NAValue::String(ref strval) = option.value {
+                                match strval.as_str() {
                                     "elbg"      => self.qmode = QuantMode::ELBG,
                                     "hybrid"    => self.qmode = QuantMode::Hybrid,
                                     "mediancut" => self.qmode = QuantMode::MedianCut,
@@ -1061,6 +1084,11 @@ impl NAOptionHandler for CinepakEncoder {
                                 };
                             }
                         },
+                        "force_v1" => {
+                            if let NAValue::Bool(val) = option.value {
+                                self.force_v1 = val;
+                            }
+                        },
                         _ => {},
                     };
                 }
@@ -1072,6 +1100,7 @@ impl NAOptionHandler for CinepakEncoder {
             KEYFRAME_OPTION => Some(NAValue::Int(i64::from(self.key_int))),
             "nstrips" => Some(NAValue::Int(self.nstrips as i64)),
             "quant_mode" => Some(NAValue::String(self.qmode.to_string())),
+            "force_v1" => Some(NAValue::Bool(self.force_v1)),
             _ => None,
         }
     }
@@ -1100,6 +1129,7 @@ mod test {
         let mut enc_reg = RegisteredEncoders::new();
         generic_register_all_encoders(&mut enc_reg);
 
+        // sample: https://samples.mplayerhq.hu/V-codecs/UCOD/TalkingHead_352x288.avi
         let dec_config = DecoderTestParams {
                 demuxer:        "avi",
                 in_name:        "assets/Misc/TalkingHead_352x288.avi",
@@ -1128,8 +1158,8 @@ mod test {
                 tb_den:  0,
                 flags:   0,
             };
-        //test_encoding_to_file(&dec_config, &enc_config, enc_params);
-        test_encoding_md5(&dec_config, &enc_config, enc_params,
-                          &[0xd73cb3c7, 0x30d59f90, 0x1d6e0e28, 0x5b72cc0c]);
+        //test_encoding_to_file(&dec_config, &enc_config, enc_params, &[]);
+        test_encoding_md5(&dec_config, &enc_config, enc_params, &[],
+                          &[0x1d4690c8, 0x3b15b4b3, 0xc2df3c7b, 0x1a25b159]);
     }
 }