| 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_JPEG_DEC_JPEG_SERIALIZATION_STATE_H_ |
| 7 |
#define LIB_JXL_JPEG_DEC_JPEG_SERIALIZATION_STATE_H_ |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <deque> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
#include "lib/jxl/jpeg/dec_jpeg_output_chunk.h" |
| 14 |
#include "lib/jxl/jpeg/jpeg_data.h" |
| 15 |
|
| 16 |
namespace jxl { |
| 17 |
namespace jpeg { |
| 18 |
|
| 19 |
struct HuffmanCodeTable { |
| 20 |
int8_t depth[256]; |
| 21 |
uint16_t code[256]; |
| 22 |
bool initialized = false; |
| 23 |
void InitDepths(int8_t value = 0) { |
| 24 |
std::fill(std::begin(depth), std::end(depth), value); |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
// Handles the packing of bits into output bytes. |
| 29 |
struct JpegBitWriter { |
| 30 |
bool healthy; |
| 31 |
std::deque<OutputChunk>* output; |
| 32 |
OutputChunk chunk; |
| 33 |
uint8_t* data; |
| 34 |
size_t pos; |
| 35 |
uint64_t put_buffer; |
| 36 |
int put_bits; |
| 37 |
}; |
| 38 |
|
| 39 |
// Holds data that is buffered between 8x8 blocks in progressive mode. |
| 40 |
struct DCTCodingState { |
| 41 |
// The run length of end-of-band symbols in a progressive scan. |
| 42 |
int eob_run_; |
| 43 |
// The huffman table to be used when flushing the state. |
| 44 |
HuffmanCodeTable* cur_ac_huff_; |
| 45 |
// The sequence of currently buffered refinement bits for a successive |
| 46 |
// approximation scan (one where Ah > 0). |
| 47 |
std::vector<uint16_t> refinement_bits_; |
| 48 |
size_t refinement_bits_count_ = 0; |
| 49 |
}; |
| 50 |
|
| 51 |
struct EncodeScanState { |
| 52 |
enum Stage { HEAD, BODY }; |
| 53 |
|
| 54 |
Stage stage = HEAD; |
| 55 |
|
| 56 |
int mcu_y; |
| 57 |
JpegBitWriter bw; |
| 58 |
coeff_t last_dc_coeff[kMaxComponents] = {0}; |
| 59 |
int restarts_to_go; |
| 60 |
int next_restart_marker; |
| 61 |
int block_scan_index; |
| 62 |
DCTCodingState coding_state; |
| 63 |
size_t extra_zero_runs_pos; |
| 64 |
int next_extra_zero_run_index; |
| 65 |
size_t next_reset_point_pos; |
| 66 |
int next_reset_point; |
| 67 |
}; |
| 68 |
|
| 69 |
struct SerializationState { |
| 70 |
enum Stage { |
| 71 |
STAGE_INIT, |
| 72 |
STAGE_SERIALIZE_SECTION, |
| 73 |
STAGE_DONE, |
| 74 |
STAGE_ERROR, |
| 75 |
}; |
| 76 |
|
| 77 |
Stage stage = STAGE_INIT; |
| 78 |
|
| 79 |
std::deque<OutputChunk> output_queue; |
| 80 |
|
| 81 |
size_t section_index = 0; |
| 82 |
int dht_index = 0; |
| 83 |
int dqt_index = 0; |
| 84 |
int app_index = 0; |
| 85 |
int com_index = 0; |
| 86 |
int data_index = 0; |
| 87 |
int scan_index = 0; |
| 88 |
std::vector<HuffmanCodeTable> dc_huff_table; |
| 89 |
std::vector<HuffmanCodeTable> ac_huff_table; |
| 90 |
const uint8_t* pad_bits = nullptr; |
| 91 |
const uint8_t* pad_bits_end = nullptr; |
| 92 |
bool seen_dri_marker = false; |
| 93 |
bool is_progressive = false; |
| 94 |
|
| 95 |
EncodeScanState scan_state; |
| 96 |
}; |
| 97 |
|
| 98 |
} // namespace jpeg |
| 99 |
} // namespace jxl |
| 100 |
|
| 101 |
#endif // LIB_JXL_JPEG_DEC_JPEG_SERIALIZATION_STATE_H_ |