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_HEADERS_H_ 7 #define LIB_JXL_HEADERS_H_ 8 9 // Codestream headers, also stored in CodecInOut. 10 11 #include <stddef.h> 12 #include <stdint.h> 13 14 #include "lib/jxl/aux_out_fwd.h" 15 #include "lib/jxl/base/compiler_specific.h" 16 #include "lib/jxl/base/status.h" 17 #include "lib/jxl/dec_bit_reader.h" 18 #include "lib/jxl/enc_bit_writer.h" 19 #include "lib/jxl/field_encodings.h" 20 21 namespace jxl { 22 23 // Reserved by ISO/IEC 10918-1. LF causes files opened in text mode to be 24 // rejected because the marker changes to 0x0D instead. The 0xFF prefix also 25 // ensures there were no 7-bit transmission limitations. 26 static constexpr uint8_t kCodestreamMarker = 0x0A; 27 28 // Compact representation of image dimensions (best case: 9 bits) so decoders 29 // can preallocate early. 30 class SizeHeader : public Fields { 31 public: 32 // All fields are valid after reading at most this many bits. WriteSizeHeader 33 // verifies this matches Bundle::MaxBits(SizeHeader). 34 static constexpr size_t kMaxBits = 78; 35 36 SizeHeader(); 37 JXL_FIELDS_NAME(SizeHeader) 38 39 Status VisitFields(Visitor* JXL_RESTRICT visitor) override; 40 41 Status Set(size_t xsize, size_t ysize); 42 43 size_t xsize() const; ysize()44 size_t ysize() const { 45 return small_ ? ((ysize_div8_minus_1_ + 1) * 8) : ysize_; 46 } 47 48 private: 49 bool small_; // xsize and ysize <= 256 and divisible by 8. 50 51 uint32_t ysize_div8_minus_1_; 52 uint32_t ysize_; 53 54 uint32_t ratio_; 55 uint32_t xsize_div8_minus_1_; 56 uint32_t xsize_; 57 }; 58 59 // (Similar to SizeHeader but different encoding because previews are smaller) 60 class PreviewHeader : public Fields { 61 public: 62 PreviewHeader(); 63 JXL_FIELDS_NAME(PreviewHeader) 64 65 Status VisitFields(Visitor* JXL_RESTRICT visitor) override; 66 67 Status Set(size_t xsize, size_t ysize); 68 69 size_t xsize() const; ysize()70 size_t ysize() const { return div8_ ? (ysize_div8_ * 8) : ysize_; } 71 72 private: 73 bool div8_; // xsize and ysize divisible by 8. 74 75 uint32_t ysize_div8_; 76 uint32_t ysize_; 77 78 uint32_t ratio_; 79 uint32_t xsize_div8_; 80 uint32_t xsize_; 81 }; 82 83 struct AnimationHeader : public Fields { 84 AnimationHeader(); 85 JXL_FIELDS_NAME(AnimationHeader) 86 87 Status VisitFields(Visitor* JXL_RESTRICT visitor) override; 88 89 // Ticks per second (expressed as rational number to support NTSC) 90 uint32_t tps_numerator; 91 uint32_t tps_denominator; 92 93 uint32_t num_loops; // 0 means to repeat infinitely. 94 95 bool have_timecodes; 96 }; 97 98 Status ReadSizeHeader(BitReader* JXL_RESTRICT reader, 99 SizeHeader* JXL_RESTRICT size); 100 101 Status WriteSizeHeader(const SizeHeader& size, BitWriter* JXL_RESTRICT writer, 102 size_t layer, AuxOut* aux_out); 103 104 } // namespace jxl 105 106 #endif // LIB_JXL_HEADERS_H_ 107