loop_filter.cc (3912B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #include "lib/jxl/loop_filter.h" 7 8 #include "lib/jxl/base/status.h" 9 #include "lib/jxl/fields.h" 10 11 namespace jxl { 12 13 LoopFilter::LoopFilter() { Bundle::Init(this); } 14 Status LoopFilter::VisitFields(Visitor* JXL_RESTRICT visitor) { 15 // Must come before AllDefault. 16 17 if (visitor->AllDefault(*this, &all_default)) { 18 // Overwrite all serialized fields, but not any nonserialized_*. 19 visitor->SetDefault(this); 20 return true; 21 } 22 23 JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(true, &gab)); 24 if (visitor->Conditional(gab)) { 25 JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(false, &gab_custom)); 26 if (visitor->Conditional(gab_custom)) { 27 JXL_QUIET_RETURN_IF_ERROR( 28 visitor->F16(1.1 * 0.104699568f, &gab_x_weight1)); 29 JXL_QUIET_RETURN_IF_ERROR( 30 visitor->F16(1.1 * 0.055680538f, &gab_x_weight2)); 31 if (std::abs(1.0f + (gab_x_weight1 + gab_x_weight2) * 4) < 1e-8) { 32 return JXL_FAILURE( 33 "Gaborish x weights lead to near 0 unnormalized kernel"); 34 } 35 JXL_QUIET_RETURN_IF_ERROR( 36 visitor->F16(1.1 * 0.104699568f, &gab_y_weight1)); 37 JXL_QUIET_RETURN_IF_ERROR( 38 visitor->F16(1.1 * 0.055680538f, &gab_y_weight2)); 39 if (std::abs(1.0f + (gab_y_weight1 + gab_y_weight2) * 4) < 1e-8) { 40 return JXL_FAILURE( 41 "Gaborish y weights lead to near 0 unnormalized kernel"); 42 } 43 JXL_QUIET_RETURN_IF_ERROR( 44 visitor->F16(1.1 * 0.104699568f, &gab_b_weight1)); 45 JXL_QUIET_RETURN_IF_ERROR( 46 visitor->F16(1.1 * 0.055680538f, &gab_b_weight2)); 47 if (std::abs(1.0f + (gab_b_weight1 + gab_b_weight2) * 4) < 1e-8) { 48 return JXL_FAILURE( 49 "Gaborish b weights lead to near 0 unnormalized kernel"); 50 } 51 } 52 } 53 54 JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(2, 2, &epf_iters)); 55 if (visitor->Conditional(epf_iters > 0)) { 56 if (visitor->Conditional(!nonserialized_is_modular)) { 57 JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(false, &epf_sharp_custom)); 58 if (visitor->Conditional(epf_sharp_custom)) { 59 for (size_t i = 0; i < kEpfSharpEntries; ++i) { 60 JXL_QUIET_RETURN_IF_ERROR(visitor->F16( 61 float(i) / float(kEpfSharpEntries - 1), &epf_sharp_lut[i])); 62 } 63 } 64 } 65 66 JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(false, &epf_weight_custom)); 67 if (visitor->Conditional(epf_weight_custom)) { 68 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(40.0f, &epf_channel_scale[0])); 69 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(5.0f, &epf_channel_scale[1])); 70 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(3.5f, &epf_channel_scale[2])); 71 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(0.45f, &epf_pass1_zeroflush)); 72 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(0.6f, &epf_pass2_zeroflush)); 73 } 74 75 JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(false, &epf_sigma_custom)); 76 if (visitor->Conditional(epf_sigma_custom)) { 77 if (visitor->Conditional(!nonserialized_is_modular)) { 78 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(0.46f, &epf_quant_mul)); 79 } 80 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(0.9f, &epf_pass0_sigma_scale)); 81 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(6.5f, &epf_pass2_sigma_scale)); 82 JXL_QUIET_RETURN_IF_ERROR( 83 visitor->F16(0.6666666666666666f, &epf_border_sad_mul)); 84 } 85 if (visitor->Conditional(nonserialized_is_modular)) { 86 JXL_QUIET_RETURN_IF_ERROR(visitor->F16(1.0f, &epf_sigma_for_modular)); 87 if (epf_sigma_for_modular < 1e-8) { 88 return JXL_FAILURE("EPF: sigma for modular is too small"); 89 } 90 } 91 } 92 93 JXL_QUIET_RETURN_IF_ERROR(visitor->BeginExtensions(&extensions)); 94 // Extensions: in chronological order of being added to the format. 95 return visitor->EndExtensions(); 96 } 97 98 } // namespace jxl