decoder_JPEG.h (7162B)
1 #ifndef DECODER_JPEG_H 2 #define DECODER_JPEG_H 3 4 #include "decoder.h" 5 #include <stdint.h> 6 #include <lua.h> 7 8 /* JPEG Markers */ 9 #define JPEG_SOI 0xFFD8 /* Start of Image */ 10 #define JPEG_EOI 0xFFD9 /* End of Image */ 11 #define JPEG_SOS 0xFFDA /* Start of Scan */ 12 #define JPEG_DQT 0xFFDB /* Define Quantization Table */ 13 #define JPEG_DNL 0xFFDC /* Define Number of Lines */ 14 #define JPEG_DRI 0xFFDD /* Define Restart Interval */ 15 #define JPEG_DHP 0xFFDE /* Define Hierarchical Progression */ 16 #define JPEG_EXP 0xFFDF /* Expand Reference Component */ 17 #define JPEG_DHT 0xFFC4 /* Define Huffman Table */ 18 #define JPEG_DAC 0xFFCC /* Define Arithmetic Coding */ 19 #define JPEG_COM 0xFFFE /* Comment */ 20 21 /* Frame markers - Start of Frame */ 22 #define JPEG_SOF0 0xFFC0 /* Baseline DCT */ 23 #define JPEG_SOF1 0xFFC1 /* Extended Sequential DCT */ 24 #define JPEG_SOF2 0xFFC2 /* Progressive DCT */ 25 #define JPEG_SOF3 0xFFC3 /* Lossless (sequential) */ 26 #define JPEG_SOF5 0xFFC5 /* Differential Sequential DCT */ 27 #define JPEG_SOF6 0xFFC6 /* Differential Progressive DCT */ 28 #define JPEG_SOF7 0xFFC7 /* Differential Lossless (sequential) */ 29 #define JPEG_SOF9 0xFFC9 /* Extended Sequential DCT, Arithmetic coding */ 30 #define JPEG_SOF10 0xFFCA /* Progressive DCT, Arithmetic coding */ 31 #define JPEG_SOF11 0xFFCB /* Lossless (sequential), Arithmetic coding */ 32 #define JPEG_SOF13 0xFFCD /* Differential Sequential DCT, Arithmetic coding */ 33 #define JPEG_SOF14 0xFFCE /* Differential Progressive DCT, Arithmetic coding */ 34 #define JPEG_SOF15 0xFFCF /* Differential Lossless, Arithmetic coding */ 35 36 /* Application markers */ 37 #define JPEG_APP0 0xFFE0 /* JFIF marker */ 38 #define JPEG_APP1 0xFFE1 /* EXIF marker */ 39 #define JPEG_APP2 0xFFE2 /* ICC Profile */ 40 #define JPEG_APP14 0xFFEE /* Adobe marker */ 41 42 /* Restart markers */ 43 #define JPEG_RST0 0xFFD0 44 #define JPEG_RST7 0xFFD7 45 46 /* Maximum values */ 47 #define JPEG_MAX_COMPONENTS 4 48 #define JPEG_MAX_HUFFMAN_TABLES 4 49 #define JPEG_MAX_QUANT_TABLES 4 50 #define JPEG_BLOCK_SIZE 64 /* 8x8 DCT block */ 51 52 /* Huffman table structure */ 53 typedef struct { 54 uint8_t bits[17]; /* Number of codes of each length (1-16) */ 55 uint8_t huffval[256]; /* Symbol values */ 56 /* Derived values for fast decoding */ 57 uint16_t huffcode[256]; /* Huffman codes */ 58 uint8_t huffsize[256]; /* Code sizes */ 59 int maxcode[18]; /* Maximum code of length k */ 60 int mincode[17]; /* Minimum code of length k */ 61 int valptr[17]; /* Index into huffval for code of length k */ 62 int num_symbols; /* Total number of symbols */ 63 } jpeg_huffman_table_t; 64 65 /* Quantization table structure */ 66 typedef struct { 67 uint16_t table[64]; /* Quantization values in zigzag order */ 68 int precision; /* 0 = 8-bit, 1 = 16-bit */ 69 } jpeg_quant_table_t; 70 71 /* Component structure */ 72 typedef struct { 73 uint8_t id; /* Component identifier */ 74 uint8_t h_samp; /* Horizontal sampling factor */ 75 uint8_t v_samp; /* Vertical sampling factor */ 76 uint8_t quant_table_id; /* Quantization table index */ 77 uint8_t dc_table_id; /* DC Huffman table index */ 78 uint8_t ac_table_id; /* AC Huffman table index */ 79 int16_t dc_pred; /* DC predictor for this component */ 80 int width; /* Component width in samples */ 81 int height; /* Component height in samples */ 82 int stride; /* Row stride in bytes */ 83 uint8_t* data; /* Decoded component data */ 84 } jpeg_component_t; 85 86 /* Scan structure for progressive JPEG */ 87 typedef struct { 88 uint8_t component_ids[JPEG_MAX_COMPONENTS]; 89 uint8_t num_components; 90 uint8_t ss; /* Start of spectral selection */ 91 uint8_t se; /* End of spectral selection */ 92 uint8_t ah; /* Successive approximation high */ 93 uint8_t al; /* Successive approximation low */ 94 } jpeg_scan_t; 95 96 /* Main JPEG decoder context */ 97 typedef struct { 98 /* Image dimensions */ 99 uint16_t width; 100 uint16_t height; 101 uint8_t num_components; 102 uint8_t precision; /* Sample precision (usually 8) */ 103 104 /* Frame type */ 105 uint8_t frame_type; /* SOF marker type */ 106 int is_progressive; 107 int is_baseline; 108 109 /* Sampling factors */ 110 uint8_t max_h_samp; 111 uint8_t max_v_samp; 112 113 /* MCU dimensions */ 114 int mcu_width; /* MCU width in pixels */ 115 int mcu_height; /* MCU height in pixels */ 116 int mcus_per_row; 117 int mcu_rows; 118 119 /* Restart interval */ 120 uint16_t restart_interval; 121 122 /* Tables */ 123 jpeg_huffman_table_t dc_tables[JPEG_MAX_HUFFMAN_TABLES]; 124 jpeg_huffman_table_t ac_tables[JPEG_MAX_HUFFMAN_TABLES]; 125 jpeg_quant_table_t quant_tables[JPEG_MAX_QUANT_TABLES]; 126 int dc_table_valid[JPEG_MAX_HUFFMAN_TABLES]; 127 int ac_table_valid[JPEG_MAX_HUFFMAN_TABLES]; 128 int quant_table_valid[JPEG_MAX_QUANT_TABLES]; 129 130 /* Components */ 131 jpeg_component_t components[JPEG_MAX_COMPONENTS]; 132 133 /* Bit reader state */ 134 const uint8_t* data; 135 uint32_t data_size; 136 uint32_t pos; 137 uint32_t bits_buffer; 138 int bits_count; 139 140 /* Progressive decoding */ 141 int16_t** coef_blocks; /* DCT coefficient blocks for progressive */ 142 int coef_blocks_count; 143 144 /* Error state */ 145 int error; 146 char error_msg[256]; 147 } jpeg_decoder_t; 148 149 /* Zigzag ordering for DCT coefficients */ 150 extern const uint8_t jpeg_zigzag[64]; 151 extern const uint8_t jpeg_unzigzag[64]; 152 153 /* IDCT constants */ 154 extern const int16_t jpeg_idct_scale[64]; 155 156 /* JPEG decoding functions */ 157 image_t* jpeg_decode(const uint8_t* data, uint32_t data_size); 158 image_t* jpeg_load_file(const char* filename); 159 160 /* Internal decoder functions */ 161 jpeg_decoder_t* jpeg_decoder_create(void); 162 void jpeg_decoder_destroy(jpeg_decoder_t* dec); 163 int jpeg_parse_markers(jpeg_decoder_t* dec, const uint8_t* data, uint32_t size); 164 int jpeg_decode_scan(jpeg_decoder_t* dec); 165 int jpeg_decode_mcu(jpeg_decoder_t* dec, int mcu_x, int mcu_y); 166 void jpeg_idct_block(int16_t* block, uint8_t* output, int stride); 167 void jpeg_ycbcr_to_rgb(jpeg_decoder_t* dec, image_t* img); 168 169 /* Huffman decoding */ 170 int jpeg_build_huffman_table(jpeg_huffman_table_t* table); 171 int jpeg_decode_huffman(jpeg_decoder_t* dec, jpeg_huffman_table_t* table); 172 int jpeg_receive_extend(jpeg_decoder_t* dec, int nbits); 173 174 /* Bit reading */ 175 int jpeg_get_bits(jpeg_decoder_t* dec, int nbits); 176 int jpeg_peek_bits(jpeg_decoder_t* dec, int nbits); 177 void jpeg_skip_bits(jpeg_decoder_t* dec, int nbits); 178 int jpeg_next_marker(jpeg_decoder_t* dec); 179 void jpeg_align_bits(jpeg_decoder_t* dec); 180 181 /* Progressive decoding */ 182 int jpeg_decode_dc_first(jpeg_decoder_t* dec, jpeg_component_t* comp, int16_t* block); 183 int jpeg_decode_dc_refine(jpeg_decoder_t* dec, int16_t* block, int al); 184 int jpeg_decode_ac_first(jpeg_decoder_t* dec, jpeg_component_t* comp, int16_t* block, int ss, int se); 185 int jpeg_decode_ac_refine(jpeg_decoder_t* dec, int16_t* block, int ss, int se, int al); 186 187 /* Lua bindings */ 188 int lua_jpeg_load(lua_State* L); 189 190 #endif /* DECODER_JPEG_H */