]> git.nihav.org Git - nihav.git/commitdiff
vp6enc: fix always-on loop filter in MC and add an option for turning it on master
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 21 Nov 2026 10:29:24 +0000 (11:29 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 21 Nov 2026 10:29:24 +0000 (11:29 +0100)
GitHub user Hailey-Ross reported this bug for VP6 decoder, encoder had it too.

nihav-duck/src/codecs/vp6enc/dsp.rs
nihav-duck/src/codecs/vp6enc/mb.rs
nihav-duck/src/codecs/vp6enc/mod.rs

index d0481f1ffa0efc0fe40ea12c92ba0b39352e1094..924287cc7006cb10deae9c30b84be708bd29465c 100644 (file)
@@ -230,10 +230,11 @@ pub struct MVEstimator {
     filter_alpha:   usize,
     loop_thr:       i16,
     mv_range:       i16,
+    loop_filter:    bool,
 }
 
 impl MVEstimator {
-    pub fn new(ref_frame: NAVideoBufferRef<u8>, mc_buf: NAVideoBufferRef<u8>, loop_thr: i16, mv_range: i16) -> Self {
+    pub fn new(ref_frame: NAVideoBufferRef<u8>, mc_buf: NAVideoBufferRef<u8>, loop_thr: i16, mv_range: i16, loop_filter: bool) -> Self {
         Self {
             ref_blk:        [[0; 64]; 6],
             ref_frame, mc_buf,
@@ -245,6 +246,7 @@ impl MVEstimator {
             filter_alpha:   0,
             loop_thr,
             mv_range,
+            loop_filter,
         }
     }
     pub fn mc_block(&mut self, dst_idx: usize, plane: usize, x: usize, y: usize, mv: MV) {
@@ -256,15 +258,17 @@ impl MVEstimator {
             };
         let tmp_blk = self.mc_buf.get_data_mut().unwrap();
         get_block(tmp_blk, 16, self.ref_frame.clone(), plane, x, y, sx, sy);
-        if (msx & 7) != 0 {
-            let foff = (8 - (sx & 7)) as usize;
-            let off = 2 + foff;
-            vp31_loop_filter(tmp_blk, off, 1, 16, 12, self.loop_thr);
-        }
-        if (msy & 7) != 0 {
-            let foff = (8 - (sy & 7)) as usize;
-            let off = (2 + foff) * 16;
-            vp31_loop_filter(tmp_blk, off, 16, 1, 12, self.loop_thr);
+        if self.loop_filter {
+            if (msx & 7) != 0 {
+                let foff = (8 - (sx & 7)) as usize;
+                let off = 2 + foff;
+                vp31_loop_filter(tmp_blk, off, 1, 16, 12, self.loop_thr);
+            }
+            if (msy & 7) != 0 {
+                let foff = (8 - (sy & 7)) as usize;
+                let off = (2 + foff) * 16;
+                vp31_loop_filter(tmp_blk, off, 16, 1, 12, self.loop_thr);
+            }
         }
         let copy_mode = (mx == 0) && (my == 0);
         let mut bicubic = !copy_mode && is_luma && self.bicubic;
index 0f6d0168f3867b184ce5a97aaf66686d5ad9ca46..bed4ca92ce001c66c055f6460d602ee8479cb434 100644 (file)
@@ -156,6 +156,7 @@ pub struct FrameEncoder {
 
     pub me_mode:    MVSearchMode,
     pub me_range:   i16,
+    pub loop_filter:  bool,
 }
 
 macro_rules! read_block {
@@ -353,7 +354,7 @@ impl FrameEncoder {
 
         let mut cur_blk = [[0u8; 64]; 6];
 
-        let mut mv_est = MVEstimator::new(ref_frame, mc_buf, loop_thr, self.me_range);
+        let mut mv_est = MVEstimator::new(ref_frame, mc_buf, loop_thr, self.me_range, self.loop_filter);
 
         let mut mv_search = self.me_mode.create_search();
 
@@ -401,7 +402,7 @@ impl FrameEncoder {
         let mut cur_blk = [[0u8; 64]; 6];
         self.src_mbs[mb_pos].fill(&mut cur_blk);
 
-        let mut mv_est = MVEstimator::new(ref_frame, mc_buf, loop_thr, self.me_range);
+        let mut mv_est = MVEstimator::new(ref_frame, mc_buf, loop_thr, self.me_range, self.loop_filter);
 
         let mut mv_search = self.me_mode.create_search();
 
@@ -543,9 +544,9 @@ impl FrameEncoder {
 
         let mut cur_blk = [[0u8; 64]; 6];
 
-        let mut mv_est = MVEstimator::new(ref_frame.clone(), mc_buf.clone(), loop_thr, self.me_range);
+        let mut mv_est = MVEstimator::new(ref_frame.clone(), mc_buf.clone(), loop_thr, self.me_range, self.loop_filter);
         let mut mv_est_g = if let Some(gold_frm) = gold_frame {
-                Some(MVEstimator::new(gold_frm, mc_buf.clone(), loop_thr, self.me_range))
+                Some(MVEstimator::new(gold_frm, mc_buf.clone(), loop_thr, self.me_range, self.loop_filter))
             } else {
                 None
             };
index b0e860f180e8d250afac12a2d1372382e89bc310..35b1bdce20ae93e09fd95ac94b790a63e59b3efa 100644 (file)
@@ -190,6 +190,7 @@ struct VP6Encoder {
     last_gold:  bool,
     me_mode:    MVSearchMode,
     me_range:   i16,
+    use_lf:     bool,
 
     force_q:    Option<usize>,
     fast:       bool,
@@ -227,6 +228,7 @@ impl VP6Encoder {
             last_gold:  false,
             me_mode:    MVSearchMode::default(),
             me_range:   16,
+            use_lf:     false,
 
             force_q:    None,
             fast:       false,
@@ -466,7 +468,6 @@ impl VP6Encoder {
         self.stats.reset();
 
         let multistream = self.huffman || self.version != VERSION_VP60;
-        let loop_filter = false;
 
         self.fenc.prepare_intra_blocks();
         self.fenc.prepare_inter_blocks(false);
@@ -480,7 +481,7 @@ impl VP6Encoder {
         self.fenc.apply_dc_prediction(&mut self.dc_pred);
         self.fenc.predict_mvs();
 
-        self.write_inter_frame(bw, quant, multistream, loop_filter, golden_frame)?;
+        self.write_inter_frame(bw, quant, multistream, self.fenc.loop_filter, golden_frame)?;
 
         Ok(golden_frame)
     }
@@ -570,7 +571,6 @@ impl VP6Encoder {
         self.stats.reset();
 
         let multistream = self.huffman || self.version != VERSION_VP60;
-        let loop_filter = false;
 
         let last_frm = self.last_frame.get_vbuf().unwrap();
         let gold_frm = if !self.last_gold {
@@ -585,7 +585,7 @@ impl VP6Encoder {
         self.fenc.predict_mvs();
         self.estimate_blocks(false);
 
-        self.write_inter_frame(bw, quant, multistream, loop_filter, golden_frame)?;
+        self.write_inter_frame(bw, quant, multistream, self.fenc.loop_filter, golden_frame)?;
 
         Ok(false)
     }
@@ -738,12 +738,14 @@ impl NAEncoder for VP6Encoder {
             let golden_frame = if is_intra {
                     self.encode_intra(&mut bw, quant)?
                 } else if !self.fast {
+                    self.fenc.loop_filter = self.use_lf && self.profile == VP6_ADVANCED_PROFILE;
                     self.fenc.estimate_mvs(self.last_frame.get_vbuf().unwrap(), self.mc_buf.clone(), false);
                     if !self.last_gold {
                         self.fenc.estimate_mvs(self.gold_frame.get_vbuf().unwrap(), self.mc_buf.clone(), true);
                     }
                     self.encode_inter(&mut bw, quant)?
                 } else {
+                    self.fenc.loop_filter = false;
                     self.encode_inter_fast(&mut bw, quant)?
                 };
             self.fenc.reconstruct_frame(&mut self.dc_pred, self.last_frame.get_vbuf().unwrap());
@@ -786,6 +788,7 @@ impl NAEncoder for VP6Encoder {
 const HUFFMAN_OPTION: &str = "huffman";
 const QUANT_OPTION: &str = "quant";
 const VERSION_OPTION: &str = "version";
+const LF_OPTION: &str = "loopfilt";
 const MV_SEARCH_OPTION: &str = "mv_mode";
 const MV_RANGE_OPTION: &str = "mv_range";
 const FAST_OPTION: &str = "fast";
@@ -809,6 +812,9 @@ const ENCODER_OPTS: &[NAOptionDefinition] = &[
     NAOptionDefinition {
         name: MV_RANGE_OPTION, description: "motion search range (in pixels)",
         opt_type: NAOptionDefinitionType::Int(Some(0), Some(30)) },
+    NAOptionDefinition {
+        name: LF_OPTION, description: "use loop filter",
+        opt_type: NAOptionDefinitionType::Bool },
     NAOptionDefinition {
         name: FAST_OPTION, description: "faster (but worse) encoding",
         opt_type: NAOptionDefinitionType::Bool },
@@ -831,6 +837,11 @@ impl NAOptionHandler for VP6Encoder {
                                 self.huffman = bval;
                             }
                         },
+                        LF_OPTION => {
+                            if let NAValue::Bool(bval) = option.value {
+                                self.use_lf = bval;
+                            }
+                        },
                         VERSION_OPTION => {
                             if let NAValue::String(ref string) = option.value {
                                 self.version = match string.as_str() {
@@ -960,7 +971,7 @@ mod test {
         let enc_options = &[
                 NAOption { name: super::QUANT_OPTION, value: NAValue::Int(42) },
             ];
-        encode_test("vp6-bool.avi", enc_options, &[0xb57f49e5, 0x6b48accd, 0xc28fadb3, 0xc89a30d2]);
+        encode_test("vp6-bool.avi", enc_options, &[0x32d6e50d, 0xb627c751, 0xc7f2f8e0, 0x66cc8791]);
     }
     #[test]
     fn test_vp6_encoder_fast() {
@@ -968,13 +979,13 @@ mod test {
                 NAOption { name: super::QUANT_OPTION, value: NAValue::Int(42) },
                 NAOption { name: super::FAST_OPTION, value: NAValue::Bool(true) },
             ];
-        encode_test("vp6-fast.avi", enc_options, &[0xb8037ce1, 0xc00ade72, 0x3c0b73c2, 0xbfc4113d]);
+        encode_test("vp6-fast.avi", enc_options, &[0xb87856e2, 0x4b061f55, 0x15f83bef, 0x0211f5cd]);
     }
     #[test]
     fn test_vp6_encoder_rc() {
         let enc_options = &[
             ];
-        encode_test("vp6-rc.avi", enc_options, &[0x2164fb25, 0xe91984ac, 0x22316452, 0x45b65730]);
+        encode_test("vp6-rc.avi", enc_options, &[0x91253bb3, 0x90b4b4af, 0x04c414ea, 0x7a76d616]);
     }
     #[test]
     fn test_vp6_encoder_huff() {
@@ -982,6 +993,6 @@ mod test {
                 NAOption { name: super::HUFFMAN_OPTION, value: NAValue::Bool(true) },
                 NAOption { name: super::QUANT_OPTION, value: NAValue::Int(42) },
             ];
-        encode_test("vp6-huff.avi", enc_options, &[0x6e9bb23d, 0xde296d92, 0x4c225bae, 0x3651e31f]);
+        encode_test("vp6-huff.avi", enc_options, &[0xb843eebc, 0x4c7a24a3, 0x040906b5, 0x4c86b1aa]);
     }
 }