-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommon.h
178 lines (139 loc) · 4.37 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#ifndef JPEG_COMMON_H
#define JPEG_COMMON_H
#include <stddef.h>
#include <stdint.h>
/**
* \brief Error codes
*
* The standard (C89, 6.1.3.3 Enumeration constants) states that
* an identifier declared as an enumeration constant has type \c int.
* Therefore, it is fine if the function returning these constants
* has return type \c int.
*/
enum {
/* 0x0000 successful completion */
RET_SUCCESS = 0x0000, /**< success */
/* 0x1xxx input/output errors */
RET_FAILURE_FILE_IO = 0x1000, /**< I/O error */
RET_FAILURE_FILE_UNSUPPORTED = 0x1001, /**< unsupported feature or file type */
RET_FAILURE_FILE_OPEN = 0x1002, /**< file open failure */
RET_FAILURE_FILE_SEEK = 0x1003,
/* 0x2xxx memory errors */
RET_FAILURE_MEMORY_ALLOCATION = 0x2000, /**< unable to allocate dynamic memory */
/* 0x3xxx general exceptions */
RET_FAILURE_LOGIC_ERROR = 0x3000, /**< faulty logic within the program */
RET_FAILURE_OVERFLOW_ERROR = 0x3001, /**< result is too large for the destination type */
/* 0x4xxx other */
RET_FAILURE_NO_MORE_DATA = 0x4000,
RET_LAST
};
/* zig-zag scan to raster scan */
static const uint8_t zigzag[64] = {
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63
};
#define RETURN_IF(err) \
do { \
if (err) { \
return (err); \
} \
} while (0)
struct qtable {
/* precision: Value 0 indicates 8-bit Qk values; value 1 indicates 16-bit Qk values. */
uint8_t Pq;
/* elements: in raster scan order */
uint16_t Q[64];
};
struct component {
/* Horizontal sampling factor, Vertical sampling factor */
uint8_t H, V;
/* Quantization table destination selector */
uint8_t Tq;
/* DC entropy coding table destination selector
* AC entropy coding table destination selector */
uint8_t Td, Ta;
/* blocks horizontally and vertically */
size_t b_x, b_y;
/* blocks of 64 integers */
struct int_block *int_buffer;
/* blocks of 64 floats */
struct flt_block *flt_buffer;
/* raster image */
float *frame_buffer;
};
/*
* This reflects DHT segment (B.2.4.2)
*/
struct htable {
/* Number of Huffman codes of length i */
uint8_t L[16];
/* Value associated with each Huffman code */
uint8_t V[16][255];
};
/*
* This reflects Annex C
*/
struct hcode {
/* unrolled htable.V[] */
uint8_t huff_val[16 * 255];
/* contains a list of code lengths */
size_t huff_size[256];
/* contains the Huffman codes corresponding to those lengths */
uint16_t huff_code[256];
/* the index of the last entry in the table */
size_t last_k;
/* EHUFCO and EHUFSI, are created by reordering the codes specified by
* HUFFCODE and HUFFSIZE according to the symbol values assigned to each code
*/
uint16_t e_huf_co[256];
size_t e_huf_si[256];
};
/* K.2 A procedure for generating the lists which specify a Huffman code table */
struct huffenc {
size_t freq[257];
size_t codesize[257];
int others[257];
size_t bits[33]; // 0..32, corresponds to htable.L[]
uint8_t huff_val[16 * 255]; // to hcode.huff_val[] => htable.V[]
};
struct context {
/* Specifies one of four possible destinations at the decoder into
* which the quantization table shall be installed */
struct qtable qtable[4];
/* Sample precision */
uint8_t P;
/* Number of lines, Number of samples per line */
uint16_t Y, X;
/* Number of image components in frame */
uint8_t Nf;
struct component component[256];
/* there are two types of tables, DC and AC; the identifiers are not unique accross these types */
/* indices: [0=DC/1=AC][identifier] */
struct htable htable[2][4];
struct hcode hcode[2][4];
struct huffenc huffenc[2][4];
/* Restart interval */
uint16_t Ri;
/* macroblocks horizontally and vertically */
size_t m_x, m_y;
/* seq. number */
size_t mblocks;
uint8_t max_H, max_V;
};
void init_huffenc(struct huffenc *huffenc);
int init_qtable(struct qtable *qtable);
int init_component(struct component *component);
int init_htable(struct htable *htable);
int init_context(struct context *context);
int alloc_buffers(struct component *component, size_t size);
void free_buffers(struct context *context);
size_t ceil_div(size_t n, size_t d);
int compute_no_blocks_and_alloc_buffers(struct context *context);
int clamp(int min, int val, int max);
#endif