1 /*
2  * Copyright (c) 2014 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 /*
25  * This file is a utilities file which supports JPEG Encode process
26  */
27 
28 #include <sys/types.h>
29 #include <stdio.h>
30 
31 #define MAX_JPEG_COMPONENTS 3 //only for Y, U and V
32 #define JPEG_Y 0
33 #define JPEG_U 1
34 #define JPEG_V 2
35 #define NUM_QUANT_ELEMENTS 64
36 #define NUM_MAX_HUFFTABLE 2
37 #define NUM_AC_RUN_SIZE_BITS 16
38 #define NUM_AC_CODE_WORDS_HUFFVAL 162
39 #define NUM_DC_RUN_SIZE_BITS 16
40 #define NUM_DC_CODE_WORDS_HUFFVAL 12
41 
42 #define BITSTREAM_ALLOCATE_STEPPING     4096
43 
44 struct __bitstream {
45     unsigned int *buffer;
46     int bit_offset;
47     int max_size_in_dword;
48 };
49 
50 typedef struct __bitstream bitstream;
51 
52 static unsigned int
swap32(unsigned int val)53 swap32(unsigned int val)
54 {
55     unsigned char *pval = (unsigned char *)&val;
56 
57     return ((pval[0] << 24)     |
58             (pval[1] << 16)     |
59             (pval[2] << 8)      |
60             (pval[3] << 0));
61 }
62 
63 static void
bitstream_start(bitstream * bs)64 bitstream_start(bitstream *bs)
65 {
66     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
67     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
68     assert(bs->buffer);
69     bs->bit_offset = 0;
70 }
71 
72 static void
bitstream_end(bitstream * bs)73 bitstream_end(bitstream *bs)
74 {
75     int pos = (bs->bit_offset >> 5);
76     int bit_offset = (bs->bit_offset & 0x1f);
77     int bit_left = 32 - bit_offset;
78 
79     if (bit_offset) {
80         bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
81     }
82 }
83 
84 static void
bitstream_put_ui(bitstream * bs,unsigned int val,int size_in_bits)85 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
86 {
87     int pos = (bs->bit_offset >> 5);
88     int bit_offset = (bs->bit_offset & 0x1f);
89     int bit_left = 32 - bit_offset;
90 
91     if (!size_in_bits)
92         return;
93 
94     if (size_in_bits < 32)
95         val &= ((1 << size_in_bits) - 1);
96 
97     bs->bit_offset += size_in_bits;
98 
99     if (bit_left > size_in_bits) {
100         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
101     } else {
102         size_in_bits -= bit_left;
103         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
104         bs->buffer[pos] = swap32(bs->buffer[pos]);
105 
106         if (pos + 1 == bs->max_size_in_dword) {
107             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
108             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
109             assert(bs->buffer);
110         }
111 
112         bs->buffer[pos + 1] = val;
113     }
114 }
115 
116 //As per Jpeg Spec ISO/IEC 10918-1, below values are assigned
117 enum jpeg_markers {
118 
119 //Define JPEG markers as 0xFFXX if you are adding the value directly to the buffer
120 //Else define marker as 0xXXFF if you are assigning the marker to a structure variable.
121 //This is needed because of the little-endedness of the IA
122 
123     SOI  = 0xFFD8, //Start of Image
124     EOI  = 0xFFD9, //End of Image
125     SOS  = 0xFFDA, //Start of Scan
126     DQT  = 0xFFDB, //Define Quantization Table
127     DRI  = 0xFFDD, //Define restart interval
128     RST0 = 0xFFD0, //Restart interval termination
129     DHT  = 0xFFC4, //Huffman table
130     SOF0 = 0xFFC0, //Baseline DCT
131     APP0 = 0xFFE0, //Application Segment
132     COM  = 0xFFFE  //Comment segment
133 };
134 
135 typedef struct _JPEGFrameHeader {
136 
137     uint16_t SOF;    //Start of Frame Header
138     uint16_t Lf;     //Length of Frame Header
139     uint8_t  P;      //Sample precision
140     uint16_t Y;      //Number of lines
141     uint16_t X;      //Number of samples per line
142     uint8_t  Nf;     //Number of image components in frame
143 
144     struct _JPEGComponent {
145         uint8_t Ci;    //Component identifier
146         uint8_t Hi: 4; //Horizontal sampling factor
147         uint8_t Vi: 4; //Vertical sampling factor
148         uint8_t Tqi;   //Quantization table destination selector
149     } JPEGComponent[MAX_JPEG_COMPONENTS];
150 
151 } JPEGFrameHeader;
152 
153 
154 typedef struct _JPEGScanHeader {
155 
156     uint16_t SOS;  //Start of Scan
157     uint16_t Ls;   //Length of Scan
158     uint8_t  Ns;   //Number of image components in the scan
159 
160     struct _ScanComponent {
161         uint8_t Csj;   //Scan component selector
162         uint8_t Tdj: 4; //DC Entropy coding table destination selector(Tdj:4 bits)
163         uint8_t Taj: 4; //AC Entropy coding table destination selector(Taj:4 bits)
164     } ScanComponent[MAX_JPEG_COMPONENTS];
165 
166     uint8_t Ss;    //Start of spectral or predictor selection, 0 for Baseline
167     uint8_t Se;    //End of spectral or predictor selection, 63 for Baseline
168     uint8_t Ah: 4; //Successive approximation bit position high, 0 for Baseline
169     uint8_t Al: 4; //Successive approximation bit position low, 0 for Baseline
170 
171 } JPEGScanHeader;
172 
173 
174 typedef struct _JPEGQuantSection {
175 
176     uint16_t DQT;    //Quantization table marker
177     uint16_t Lq;     //Length of Quantization table definition
178     uint8_t  Tq: 4;  //Quantization table destination identifier
179     uint8_t  Pq: 4;  //Quatization table precision. Should be 0 for 8-bit samples
180     uint8_t  Qk[NUM_QUANT_ELEMENTS]; //Quantization table elements
181 
182 } JPEGQuantSection;
183 
184 typedef struct _JPEGHuffSection {
185 
186     uint16_t DHT;                            //Huffman table marker
187     uint16_t Lh;                             //Huffman table definition length
188     uint8_t  Tc: 4;                          //Table class- 0:DC, 1:AC
189     uint8_t  Th: 4;                          //Huffman table destination identifier
190     uint8_t  Li[NUM_AC_RUN_SIZE_BITS];       //Number of Huffman codes of length i
191     uint8_t  Vij[NUM_AC_CODE_WORDS_HUFFVAL]; //Value associated with each Huffman code
192 
193 } JPEGHuffSection;
194 
195 
196 typedef struct _JPEGRestartSection {
197 
198     uint16_t DRI;  //Restart interval marker
199     uint16_t Lr;   //Legth of restart interval segment
200     uint16_t Ri;   //Restart interval
201 
202 } JPEGRestartSection;
203 
204 
205 typedef struct _JPEGCommentSection {
206 
207     uint16_t COM;  //Comment marker
208     uint16_t Lc;   //Comment segment length
209     uint8_t  Cmi;  //Comment byte
210 
211 } JPEGCommentSection;
212 
213 
214 typedef struct _JPEGAppSection {
215 
216     uint16_t APPn;  //Application data marker
217     uint16_t Lp;    //Application data segment length
218     uint8_t  Api;   //Application data byte
219 
220 } JPEGAppSection;
221 
222 //Luminance quantization table
223 //Source: Jpeg Spec ISO/IEC 10918-1, Annex K, Table K.1
224 uint8_t jpeg_luma_quant[NUM_QUANT_ELEMENTS] = {
225     16, 11, 10, 16, 24,  40,  51,  61,
226     12, 12, 14, 19, 26,  58,  60,  55,
227     14, 13, 16, 24, 40,  57,  69,  56,
228     14, 17, 22, 29, 51,  87,  80,  62,
229     18, 22, 37, 56, 68,  109, 103, 77,
230     24, 35, 55, 64, 81,  104, 113, 92,
231     49, 64, 78, 87, 103, 121, 120, 101,
232     72, 92, 95, 98, 112, 100, 103, 99
233 };
234 
235 //Luminance quantization table
236 //Source: Jpeg Spec ISO/IEC 10918-1, Annex K, Table K.2
237 uint8_t jpeg_chroma_quant[NUM_QUANT_ELEMENTS] = {
238     17, 18, 24, 47, 99, 99, 99, 99,
239     18, 21, 26, 66, 99, 99, 99, 99,
240     24, 26, 56, 99, 99, 99, 99, 99,
241     47, 66, 99, 99, 99, 99, 99, 99,
242     99, 99, 99, 99, 99, 99, 99, 99,
243     99, 99, 99, 99, 99, 99, 99, 99,
244     99, 99, 99, 99, 99, 99, 99, 99,
245     99, 99, 99, 99, 99, 99, 99, 99
246 };
247 
248 
249 //Zigzag scan order of the the Luma and Chroma components
250 //Note: Jpeg Spec ISO/IEC 10918-1, Figure A.6 shows the zigzag order differently.
251 //The Spec is trying to show the zigzag pattern with number positions. The below
252 //table will use the patter shown by A.6 and map the postion of the elements in the array
253 uint8_t jpeg_zigzag[] = {
254     0,   1,   8,   16,  9,   2,   3,   10,
255     17,  24,  32,  25,  18,  11,  4,   5,
256     12,  19,  26,  33,  40,  48,  41,  34,
257     27,  20,  13,  6,   7,   14,  21,  28,
258     35,  42,  49,  56,  57,  50,  43,  36,
259     29,  22,  15,  23,  30,  37,  44,  51,
260     58,  59,  52,  45,  38,  31,  39,  46,
261     53,  60,  61,  54,  47,  55,  62,  63
262 };
263 
264 
265 //Huffman table for Luminance DC Coefficients
266 //Reference Jpeg Spec ISO/IEC 10918-1, K.3.3.1
267 //K.3.3.1 is the summarized version of Table K.3
268 uint8_t jpeg_hufftable_luma_dc[] = {
269     //TcTh (Tc=0 since 0:DC, 1:AC; Th=0)
270     0x00,
271     //Li
272     0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273     //Vi
274     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
275 };
276 
277 //Huffman table for Chrominance DC Coefficients
278 //Reference Jpeg Spec ISO/IEC 10918-1, K.3.3.1
279 //K.3.3.1 is the summarized version of Table K.4
280 uint8_t jpeg_hufftable_chroma_dc[] = {
281     //TcTh (Tc=0 since 0:DC, 1:AC; Th=1)
282     0x01,
283     //Li
284     0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
285     //Vi
286     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
287 };
288 
289 
290 //Huffman table for Luminance AC Coefficients
291 //Reference Jpeg Spec ISO/IEC 10918-1, K.3.3.2
292 //K.3.3.2 is the summarized version of Table K.5
293 uint8_t jpeg_hufftable_luma_ac[] = {
294     //TcTh (Tc=1 since 0:DC, 1:AC; Th=0)
295     0x10,
296     //Li
297     0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D,
298     //Vi
299     0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
300     0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
301     0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
302     0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
303     0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
304     0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
305     0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
306     0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
307     0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
308     0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
309     0xF9, 0xFA
310 };
311 
312 //Huffman table for Chrominance AC Coefficients
313 //Reference Jpeg Spec ISO/IEC 10918-1, K.3.3.2
314 //K.3.3.2 is the summarized version of Table K.6
315 uint8_t jpeg_hufftable_chroma_ac[] = {
316     //TcTh (Tc=1 since 0:DC, 1:AC; Th=1)
317     0x11,
318     //Li
319     0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
320     //Vi
321     0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
322     0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
323     0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
324     0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
325     0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
326     0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
327     0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
328     0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
329     0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
330     0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
331     0xF9, 0xFA
332 };
333 
334 typedef struct _YUVComponentSpecs {
335     //One of 0(I420)/1(NV12)/2(UYVY)/3(YUY2)/4(Y8)/5(RGBA)>
336     unsigned int yuv_type;
337     // One of VA_RT_FORMAT_YUV420, VA_RT_FORMAT_YUV422, VA_RT_FORMAT_YUV400, VA_RT_FORMAT_YUV444, VA_RT_FORMAT_RGB32
338     unsigned int va_surface_format;
339     //One of VA_FOURCC_I420, VA_FOURCC_NV12, VA_FOURCC_UYVY, VA_FOURCC_YUY2, VA_FOURCC_Y800, VA_FOURCC_444P, VA_FOURCC_RGBA
340     unsigned int fourcc_val; //Using this field to evaluate the input file type.
341     //no.of. components
342     unsigned int num_components;
343     //Y horizontal subsample
344     unsigned int y_h_subsample;
345     //Y vertical subsample
346     unsigned int y_v_subsample;
347     //U horizontal subsample
348     unsigned int u_h_subsample;
349     //U vertical subsample
350     unsigned int u_v_subsample;
351     //V horizontal subsample
352     unsigned int v_h_subsample;
353     //V vertical subsample
354     unsigned int v_v_subsample;
355 } YUVComponentSpecs;
356