libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

entropy_coder.cc (2049B)


      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/entropy_coder.h"
      7 
      8 #include <stddef.h>
      9 #include <stdint.h>
     10 
     11 #include <algorithm>
     12 #include <utility>
     13 #include <vector>
     14 
     15 #include "lib/jxl/ac_context.h"
     16 #include "lib/jxl/ac_strategy.h"
     17 #include "lib/jxl/base/bits.h"
     18 #include "lib/jxl/base/compiler_specific.h"
     19 #include "lib/jxl/base/status.h"
     20 #include "lib/jxl/coeff_order.h"
     21 #include "lib/jxl/coeff_order_fwd.h"
     22 #include "lib/jxl/dec_ans.h"
     23 #include "lib/jxl/dec_bit_reader.h"
     24 #include "lib/jxl/dec_context_map.h"
     25 #include "lib/jxl/epf.h"
     26 #include "lib/jxl/image.h"
     27 #include "lib/jxl/image_ops.h"
     28 #include "lib/jxl/pack_signed.h"
     29 
     30 namespace jxl {
     31 
     32 Status DecodeBlockCtxMap(BitReader* br, BlockCtxMap* block_ctx_map) {
     33   auto& dct = block_ctx_map->dc_thresholds;
     34   auto& qft = block_ctx_map->qf_thresholds;
     35   auto& ctx_map = block_ctx_map->ctx_map;
     36   bool is_default = static_cast<bool>(br->ReadFixedBits<1>());
     37   if (is_default) {
     38     *block_ctx_map = BlockCtxMap();
     39     return true;
     40   }
     41   block_ctx_map->num_dc_ctxs = 1;
     42   for (int j : {0, 1, 2}) {
     43     dct[j].resize(br->ReadFixedBits<4>());
     44     block_ctx_map->num_dc_ctxs *= dct[j].size() + 1;
     45     for (int& i : dct[j]) {
     46       i = UnpackSigned(U32Coder::Read(kDCThresholdDist, br));
     47     }
     48   }
     49   qft.resize(br->ReadFixedBits<4>());
     50   for (uint32_t& i : qft) {
     51     i = U32Coder::Read(kQFThresholdDist, br) + 1;
     52   }
     53 
     54   if (block_ctx_map->num_dc_ctxs * (qft.size() + 1) > 64) {
     55     return JXL_FAILURE("Invalid block context map: too big");
     56   }
     57 
     58   ctx_map.resize(3 * kNumOrders * block_ctx_map->num_dc_ctxs *
     59                  (qft.size() + 1));
     60   JXL_RETURN_IF_ERROR(DecodeContextMap(&ctx_map, &block_ctx_map->num_ctxs, br));
     61   if (block_ctx_map->num_ctxs > 16) {
     62     return JXL_FAILURE("Invalid block context map: too many distinct contexts");
     63   }
     64   return true;
     65 }
     66 
     67 constexpr uint8_t BlockCtxMap::kDefaultCtxMap[];  // from ac_context.h
     68 
     69 }  // namespace jxl