tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

entropy_coder.cc (1991B)


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