libjxl

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

jpeg_data.h (7095B)


      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 // Data structures that represent the non-pixel contents of a jpeg file.
      7 
      8 #ifndef LIB_JXL_JPEG_JPEG_DATA_H_
      9 #define LIB_JXL_JPEG_JPEG_DATA_H_
     10 
     11 #include <stddef.h>
     12 #include <stdint.h>
     13 
     14 #include <array>
     15 #include <vector>
     16 
     17 #include "lib/jxl/common.h"  // JPEGXL_ENABLE_TRANSCODE_JPEG
     18 #include "lib/jxl/fields.h"
     19 #include "lib/jxl/frame_dimensions.h"
     20 
     21 namespace jxl {
     22 namespace jpeg {
     23 
     24 constexpr int kMaxComponents = 4;
     25 constexpr int kMaxQuantTables = 4;
     26 constexpr int kMaxHuffmanTables = 4;
     27 constexpr size_t kJpegHuffmanMaxBitLength = 16;
     28 constexpr int kJpegHuffmanAlphabetSize = 256;
     29 constexpr int kJpegDCAlphabetSize = 12;
     30 constexpr int kMaxDHTMarkers = 512;
     31 constexpr int kMaxDimPixels = 65535;
     32 constexpr uint8_t kApp1 = 0xE1;
     33 constexpr uint8_t kApp2 = 0xE2;
     34 const uint8_t kIccProfileTag[12] = "ICC_PROFILE";
     35 const uint8_t kExifTag[6] = "Exif\0";
     36 const uint8_t kXMPTag[29] = "http://ns.adobe.com/xap/1.0/";
     37 
     38 /* clang-format off */
     39 constexpr uint32_t kJPEGNaturalOrder[80] = {
     40   0,   1,  8, 16,  9,  2,  3, 10,
     41   17, 24, 32, 25, 18, 11,  4,  5,
     42   12, 19, 26, 33, 40, 48, 41, 34,
     43   27, 20, 13,  6,  7, 14, 21, 28,
     44   35, 42, 49, 56, 57, 50, 43, 36,
     45   29, 22, 15, 23, 30, 37, 44, 51,
     46   58, 59, 52, 45, 38, 31, 39, 46,
     47   53, 60, 61, 54, 47, 55, 62, 63,
     48   // extra entries for safety in decoder
     49   63, 63, 63, 63, 63, 63, 63, 63,
     50   63, 63, 63, 63, 63, 63, 63, 63
     51 };
     52 
     53 constexpr uint32_t kJPEGZigZagOrder[64] = {
     54   0,   1,  5,  6, 14, 15, 27, 28,
     55   2,   4,  7, 13, 16, 26, 29, 42,
     56   3,   8, 12, 17, 25, 30, 41, 43,
     57   9,  11, 18, 24, 31, 40, 44, 53,
     58   10, 19, 23, 32, 39, 45, 52, 54,
     59   20, 22, 33, 38, 46, 51, 55, 60,
     60   21, 34, 37, 47, 50, 56, 59, 61,
     61   35, 36, 48, 49, 57, 58, 62, 63
     62 };
     63 /* clang-format on */
     64 
     65 // Quantization values for an 8x8 pixel block.
     66 struct JPEGQuantTable {
     67   std::array<int32_t, kDCTBlockSize> values;
     68   uint32_t precision = 0;
     69   // The index of this quantization table as it was parsed from the input JPEG.
     70   // Each DQT marker segment contains an 'index' field, and we save this index
     71   // here. Valid values are 0 to 3.
     72   uint32_t index = 0;
     73   // Set to true if this table is the last one within its marker segment.
     74   bool is_last = true;
     75 };
     76 
     77 // Huffman code and decoding lookup table used for DC and AC coefficients.
     78 struct JPEGHuffmanCode {
     79   // Bit length histogram.
     80   std::array<uint32_t, kJpegHuffmanMaxBitLength + 1> counts = {};
     81   // Symbol values sorted by increasing bit lengths.
     82   std::array<uint32_t, kJpegHuffmanAlphabetSize + 1> values = {};
     83   // The index of the Huffman code in the current set of Huffman codes. For AC
     84   // component Huffman codes, 0x10 is added to the index.
     85   int slot_id = 0;
     86   // Set to true if this Huffman code is the last one within its marker segment.
     87   bool is_last = true;
     88 };
     89 
     90 // Huffman table indexes used for one component of one scan.
     91 struct JPEGComponentScanInfo {
     92   uint32_t comp_idx;
     93   uint32_t dc_tbl_idx;
     94   uint32_t ac_tbl_idx;
     95 };
     96 
     97 // Contains information that is used in one scan.
     98 struct JPEGScanInfo {
     99   // Parameters used for progressive scans (named the same way as in the spec):
    100   //   Ss : Start of spectral band in zig-zag sequence.
    101   //   Se : End of spectral band in zig-zag sequence.
    102   //   Ah : Successive approximation bit position, high.
    103   //   Al : Successive approximation bit position, low.
    104   uint32_t Ss;
    105   uint32_t Se;
    106   uint32_t Ah;
    107   uint32_t Al;
    108   uint32_t num_components = 0;
    109   std::array<JPEGComponentScanInfo, 4> components;
    110   // Last codestream pass that is needed to write this scan.
    111   uint32_t last_needed_pass = 0;
    112 
    113   // Extra information required for bit-precise JPEG file reconstruction.
    114 
    115   // Set of block indexes where the JPEG encoder has to flush the end-of-block
    116   // runs and refinement bits.
    117   std::vector<uint32_t> reset_points;
    118   // The number of extra zero runs (Huffman symbol 0xf0) before the end of
    119   // block (if nonzero), indexed by block index.
    120   // All of these symbols can be omitted without changing the pixel values, but
    121   // some jpeg encoders put these at the end of blocks.
    122   typedef struct {
    123     uint32_t block_idx;
    124     uint32_t num_extra_zero_runs;
    125   } ExtraZeroRunInfo;
    126   std::vector<ExtraZeroRunInfo> extra_zero_runs;
    127 };
    128 
    129 typedef int16_t coeff_t;
    130 
    131 // Represents one component of a jpeg file.
    132 struct JPEGComponent {
    133   JPEGComponent()
    134       : id(0),
    135         h_samp_factor(1),
    136         v_samp_factor(1),
    137         quant_idx(0),
    138         width_in_blocks(0),
    139         height_in_blocks(0) {}
    140 
    141   // One-byte id of the component.
    142   uint32_t id;
    143   // Horizontal and vertical sampling factors.
    144   // In interleaved mode, each minimal coded unit (MCU) has
    145   // h_samp_factor x v_samp_factor DCT blocks from this component.
    146   int h_samp_factor;
    147   int v_samp_factor;
    148   // The index of the quantization table used for this component.
    149   uint32_t quant_idx;
    150   // The dimensions of the component measured in 8x8 blocks.
    151   uint32_t width_in_blocks;
    152   uint32_t height_in_blocks;
    153   // The DCT coefficients of this component, laid out block-by-block, divided
    154   // through the quantization matrix values.
    155   std::vector<coeff_t> coeffs;
    156 };
    157 
    158 enum class AppMarkerType : uint32_t {
    159   kUnknown = 0,
    160   kICC = 1,
    161   kExif = 2,
    162   kXMP = 3,
    163 };
    164 
    165 // Represents a parsed jpeg file.
    166 struct JPEGData : public Fields {
    167   JPEGData()
    168       : width(0), height(0), restart_interval(0), has_zero_padding_bit(false) {}
    169 
    170   JXL_FIELDS_NAME(JPEGData)
    171 #if JPEGXL_ENABLE_TRANSCODE_JPEG
    172   // Doesn't serialize everything - skips brotli-encoded data and what is
    173   // already encoded in the codestream.
    174   Status VisitFields(Visitor* visitor) override;
    175 #else
    176   Status VisitFields(Visitor* /* visitor */) override {
    177     JXL_UNREACHABLE("JPEG transcoding support not enabled");
    178   }
    179 #endif  // JPEGXL_ENABLE_TRANSCODE_JPEG
    180 
    181   void CalculateMcuSize(const JPEGScanInfo& scan, int* MCUs_per_row,
    182                         int* MCU_rows) const;
    183 
    184   int width;
    185   int height;
    186   uint32_t restart_interval;
    187   std::vector<std::vector<uint8_t>> app_data;
    188   std::vector<AppMarkerType> app_marker_type;
    189   std::vector<std::vector<uint8_t>> com_data;
    190   std::vector<JPEGQuantTable> quant;
    191   std::vector<JPEGHuffmanCode> huffman_code;
    192   std::vector<JPEGComponent> components;
    193   std::vector<JPEGScanInfo> scan_info;
    194   std::vector<uint8_t> marker_order;
    195   std::vector<std::vector<uint8_t>> inter_marker_data;
    196   std::vector<uint8_t> tail_data;
    197 
    198   // Extra information required for bit-precise JPEG file reconstruction.
    199 
    200   bool has_zero_padding_bit;
    201   std::vector<uint8_t> padding_bits;
    202 };
    203 
    204 #if JPEGXL_ENABLE_TRANSCODE_JPEG
    205 // Set ICC profile in jpeg_data.
    206 Status SetJPEGDataFromICC(const std::vector<uint8_t>& icc,
    207                           jpeg::JPEGData* jpeg_data);
    208 #else
    209 static JXL_INLINE Status SetJPEGDataFromICC(
    210     const std::vector<uint8_t>& /* icc */, jpeg::JPEGData* /* jpeg_data */) {
    211   JXL_UNREACHABLE("JPEG transcoding support not enabled");
    212 }
    213 #endif  // JPEGXL_ENABLE_TRANSCODE_JPEG
    214 
    215 }  // namespace jpeg
    216 }  // namespace jxl
    217 
    218 #endif  // LIB_JXL_JPEG_JPEG_DATA_H_