libjxl

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

dec_modular.h (5206B)


      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 #ifndef LIB_JXL_DEC_MODULAR_H_
      7 #define LIB_JXL_DEC_MODULAR_H_
      8 
      9 #include <stddef.h>
     10 
     11 #include <string>
     12 
     13 #include "lib/jxl/base/data_parallel.h"
     14 #include "lib/jxl/base/status.h"
     15 #include "lib/jxl/dec_bit_reader.h"
     16 #include "lib/jxl/dec_cache.h"
     17 #include "lib/jxl/frame_header.h"
     18 #include "lib/jxl/image.h"
     19 #include "lib/jxl/modular/encoding/encoding.h"
     20 #include "lib/jxl/modular/modular_image.h"
     21 
     22 namespace jxl {
     23 
     24 struct ModularStreamId {
     25   enum Kind {
     26     kGlobalData,
     27     kVarDCTDC,
     28     kModularDC,
     29     kACMetadata,
     30     kQuantTable,
     31     kModularAC
     32   };
     33   Kind kind;
     34   size_t quant_table_id;
     35   size_t group_id;  // DC or AC group id.
     36   size_t pass_id;   // Only for kModularAC.
     37   size_t ID(const FrameDimensions& frame_dim) const {
     38     size_t id = 0;
     39     switch (kind) {
     40       case kGlobalData:
     41         id = 0;
     42         break;
     43       case kVarDCTDC:
     44         id = 1 + group_id;
     45         break;
     46       case kModularDC:
     47         id = 1 + frame_dim.num_dc_groups + group_id;
     48         break;
     49       case kACMetadata:
     50         id = 1 + 2 * frame_dim.num_dc_groups + group_id;
     51         break;
     52       case kQuantTable:
     53         id = 1 + 3 * frame_dim.num_dc_groups + quant_table_id;
     54         break;
     55       case kModularAC:
     56         id = 1 + 3 * frame_dim.num_dc_groups + DequantMatrices::kNum +
     57              frame_dim.num_groups * pass_id + group_id;
     58         break;
     59     };
     60     return id;
     61   }
     62   static ModularStreamId Global() {
     63     return ModularStreamId{kGlobalData, 0, 0, 0};
     64   }
     65   static ModularStreamId VarDCTDC(size_t group_id) {
     66     return ModularStreamId{kVarDCTDC, 0, group_id, 0};
     67   }
     68   static ModularStreamId ModularDC(size_t group_id) {
     69     return ModularStreamId{kModularDC, 0, group_id, 0};
     70   }
     71   static ModularStreamId ACMetadata(size_t group_id) {
     72     return ModularStreamId{kACMetadata, 0, group_id, 0};
     73   }
     74   static ModularStreamId QuantTable(size_t quant_table_id) {
     75     JXL_ASSERT(quant_table_id < DequantMatrices::kNum);
     76     return ModularStreamId{kQuantTable, quant_table_id, 0, 0};
     77   }
     78   static ModularStreamId ModularAC(size_t group_id, size_t pass_id) {
     79     return ModularStreamId{kModularAC, 0, group_id, pass_id};
     80   }
     81   static size_t Num(const FrameDimensions& frame_dim, size_t passes) {
     82     return ModularAC(0, passes).ID(frame_dim);
     83   }
     84   std::string DebugString() const;
     85 };
     86 
     87 class ModularFrameDecoder {
     88  public:
     89   void Init(const FrameDimensions& frame_dim) { this->frame_dim = frame_dim; }
     90   Status DecodeGlobalInfo(BitReader* reader, const FrameHeader& frame_header,
     91                           bool allow_truncated_group);
     92   Status DecodeGroup(const FrameHeader& frame_header, const Rect& rect,
     93                      BitReader* reader, int minShift, int maxShift,
     94                      const ModularStreamId& stream, bool zerofill,
     95                      PassesDecoderState* dec_state,
     96                      RenderPipelineInput* render_pipeline_input,
     97                      bool allow_truncated, bool* should_run_pipeline = nullptr);
     98   // Decodes a VarDCT DC group (`group_id`) from the given `reader`.
     99   Status DecodeVarDCTDC(const FrameHeader& frame_header, size_t group_id,
    100                         BitReader* reader, PassesDecoderState* dec_state);
    101   // Decodes a VarDCT AC Metadata group (`group_id`) from the given `reader`.
    102   Status DecodeAcMetadata(const FrameHeader& frame_header, size_t group_id,
    103                           BitReader* reader, PassesDecoderState* dec_state);
    104   // Decodes a RAW quant table from `br` into the given `encoding`, of size
    105   // `required_size_x x required_size_y`. If `modular_frame_decoder` is passed,
    106   // its global tree is used, otherwise no global tree is used.
    107   static Status DecodeQuantTable(size_t required_size_x, size_t required_size_y,
    108                                  BitReader* br, QuantEncoding* encoding,
    109                                  size_t idx,
    110                                  ModularFrameDecoder* modular_frame_decoder);
    111   // if inplace is true, this can only be called once
    112   // if it is false, it can be called multiple times (e.g. for progressive
    113   // steps)
    114   Status FinalizeDecoding(const FrameHeader& frame_header,
    115                           PassesDecoderState* dec_state, jxl::ThreadPool* pool,
    116                           bool inplace);
    117   bool have_dc() const { return have_something; }
    118   void MaybeDropFullImage();
    119   bool UsesFullImage() const { return use_full_image; }
    120 
    121  private:
    122   Status ModularImageToDecodedRect(const FrameHeader& frame_header, Image& gi,
    123                                    PassesDecoderState* dec_state,
    124                                    jxl::ThreadPool* pool,
    125                                    RenderPipelineInput& render_pipeline_input,
    126                                    Rect modular_rect) const;
    127 
    128   Image full_image;
    129   std::vector<Transform> global_transform;
    130   FrameDimensions frame_dim;
    131   bool do_color;
    132   bool have_something;
    133   bool use_full_image = true;
    134   bool all_same_shift;
    135   Tree tree;
    136   ANSCode code;
    137   std::vector<uint8_t> context_map;
    138   GroupHeader global_header;
    139 };
    140 
    141 }  // namespace jxl
    142 
    143 #endif  // LIB_JXL_DEC_MODULAR_H_