Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
jpeg.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * jpeg.c - JPEG compression for SRV-1 robot
3  * Copyright (C) 2005-2009 Surveyor Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details (www.gnu.org/licenses)
14  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 #include "jpeg.h"
17 
23 static inline unsigned char svs_size_code(int w)
24 {
25  // 1=(40,30) 2=(128,96) 3=(160,120) 5=(320,240) 7=(640,480) 9=(1280,1024);
26  if (w <= 40) {
27  return '1';
28  }
29  if (w <= 128) {
30  return '2';
31  }
32  if (w <= 160) {
33  return '3';
34  }
35  if (w <= 320) {
36  return '4';
37  }
38  if (w <= 640) {
39  return '7';
40  }
41  return '9';
42 }
43 
44 int jpeg_create_svs_header(unsigned char *jpegbuf, int32_t size, int w)
45 {
46  // SVS Surveyor Jpeg UDP format
47  uint32_t s = size;
48  uint8_t *p = (uint8_t *) & s;
49  jpegbuf[0] = '#';
50  jpegbuf[1] = '#';
51  jpegbuf[2] = 'I';
52  jpegbuf[3] = 'M';
53  jpegbuf[4] = 'J';
54  jpegbuf[5] = svs_size_code(w);
55  jpegbuf[6] = p[0];
56  jpegbuf[7] = p[1];
57  jpegbuf[8] = p[2];
58  jpegbuf[9] = 0x00;
59  return size + 10;
60 }
61 
62 
63 typedef struct JPEG_ENCODER_STRUCTURE {
70 
73 
79 
83 
85 
86 
88 //static void jpeg_initialize_quantization_tables(uint32_t);
89 
91 
94 
96 
97 static void jpeg_levelshift(int16_t *);
98 static void jpeg_DCT(int16_t *);
99 
100 static void jpeg_quantization(int16_t *, uint16_t *);
102 
104 
105 //static int16_t fdct_coeff[8] = {0x5a82, 0x5a82, 0x30fb, 0x7641, 0x18f8, 0x7d8a, 0x471c, 0x6a6d};
106 //static int16_t fdct_temp[64];
107 
109  0x0000, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006,
110  0x000E, 0x001E, 0x003E, 0x007E, 0x00FE, 0x01FE
111 };
112 
114  0x0002, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
115  0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009
116 };
117 
119  0x0000, 0x0001, 0x0002, 0x0006, 0x000E, 0x001E,
120  0x003E, 0x007E, 0x00FE, 0x01FE, 0x03FE, 0x07FE
121 };
122 
124  0x0002, 0x0002, 0x0002, 0x0003, 0x0004, 0x0005,
125  0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B
126 };
127 
129  0x000A,
130  0x0000, 0x0001, 0x0004, 0x000B, 0x001A, 0x0078, 0x00F8, 0x03F6, 0xFF82, 0xFF83,
131  0x000C, 0x001B, 0x0079, 0x01F6, 0x07F6, 0xFF84, 0xFF85, 0xFF86, 0xFF87, 0xFF88,
132  0x001C, 0x00F9, 0x03F7, 0x0FF4, 0xFF89, 0xFF8A, 0xFF8b, 0xFF8C, 0xFF8D, 0xFF8E,
133  0x003A, 0x01F7, 0x0FF5, 0xFF8F, 0xFF90, 0xFF91, 0xFF92, 0xFF93, 0xFF94, 0xFF95,
134  0x003B, 0x03F8, 0xFF96, 0xFF97, 0xFF98, 0xFF99, 0xFF9A, 0xFF9B, 0xFF9C, 0xFF9D,
135  0x007A, 0x07F7, 0xFF9E, 0xFF9F, 0xFFA0, 0xFFA1, 0xFFA2, 0xFFA3, 0xFFA4, 0xFFA5,
136  0x007B, 0x0FF6, 0xFFA6, 0xFFA7, 0xFFA8, 0xFFA9, 0xFFAA, 0xFFAB, 0xFFAC, 0xFFAD,
137  0x00FA, 0x0FF7, 0xFFAE, 0xFFAF, 0xFFB0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4, 0xFFB5,
138  0x01F8, 0x7FC0, 0xFFB6, 0xFFB7, 0xFFB8, 0xFFB9, 0xFFBA, 0xFFBB, 0xFFBC, 0xFFBD,
139  0x01F9, 0xFFBE, 0xFFBF, 0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6,
140  0x01FA, 0xFFC7, 0xFFC8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD, 0xFFCE, 0xFFCF,
141  0x03F9, 0xFFD0, 0xFFD1, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7, 0xFFD8,
142  0x03FA, 0xFFD9, 0xFFDA, 0xFFDB, 0xFFDC, 0xFFDD, 0xFFDE, 0xFFDF, 0xFFE0, 0xFFE1,
143  0x07F8, 0xFFE2, 0xFFE3, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7, 0xFFE8, 0xFFE9, 0xFFEA,
144  0xFFEB, 0xFFEC, 0xFFED, 0xFFEE, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFF2, 0xFFF3, 0xFFF4,
145  0xFFF5, 0xFFF6, 0xFFF7, 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE,
146  0x07F9
147 };
148 
150  0x0004,
151  0x0002, 0x0002, 0x0003, 0x0004, 0x0005, 0x0007, 0x0008, 0x000A, 0x0010, 0x0010,
152  0x0004, 0x0005, 0x0007, 0x0009, 0x000B, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
153  0x0005, 0x0008, 0x000A, 0x000C, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
154  0x0006, 0x0009, 0x000C, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
155  0x0006, 0x000A, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
156  0x0007, 0x000B, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
157  0x0007, 0x000C, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
158  0x0008, 0x000C, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
159  0x0009, 0x000F, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
160  0x0009, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
161  0x0009, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
162  0x000A, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
163  0x000A, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
164  0x000B, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
165  0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
166  0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
167  0x000B
168 };
169 
171  0x0000,
172  0x0001, 0x0004, 0x000A, 0x0018, 0x0019, 0x0038, 0x0078, 0x01F4, 0x03F6, 0x0FF4,
173  0x000B, 0x0039, 0x00F6, 0x01F5, 0x07F6, 0x0FF5, 0xFF88, 0xFF89, 0xFF8A, 0xFF8B,
174  0x001A, 0x00F7, 0x03F7, 0x0FF6, 0x7FC2, 0xFF8C, 0xFF8D, 0xFF8E, 0xFF8F, 0xFF90,
175  0x001B, 0x00F8, 0x03F8, 0x0FF7, 0xFF91, 0xFF92, 0xFF93, 0xFF94, 0xFF95, 0xFF96,
176  0x003A, 0x01F6, 0xFF97, 0xFF98, 0xFF99, 0xFF9A, 0xFF9B, 0xFF9C, 0xFF9D, 0xFF9E,
177  0x003B, 0x03F9, 0xFF9F, 0xFFA0, 0xFFA1, 0xFFA2, 0xFFA3, 0xFFA4, 0xFFA5, 0xFFA6,
178  0x0079, 0x07F7, 0xFFA7, 0xFFA8, 0xFFA9, 0xFFAA, 0xFFAB, 0xFFAC, 0xFFAD, 0xFFAE,
179  0x007A, 0x07F8, 0xFFAF, 0xFFB0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4, 0xFFB5, 0xFFB6,
180  0x00F9, 0xFFB7, 0xFFB8, 0xFFB9, 0xFFBA, 0xFFBB, 0xFFBC, 0xFFBD, 0xFFBE, 0xFFBF,
181  0x01F7, 0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6, 0xFFC7, 0xFFC8,
182  0x01F8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD, 0xFFCE, 0xFFCF, 0xFFD0, 0xFFD1,
183  0x01F9, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7, 0xFFD8, 0xFFD9, 0xFFDA,
184  0x01FA, 0xFFDB, 0xFFDC, 0xFFDD, 0xFFDE, 0xFFDF, 0xFFE0, 0xFFE1, 0xFFE2, 0xFFE3,
185  0x07F9, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7, 0xFFE8, 0xFFE9, 0xFFEA, 0xFFEb, 0xFFEC,
186  0x3FE0, 0xFFED, 0xFFEE, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFF2, 0xFFF3, 0xFFF4, 0xFFF5,
187  0x7FC3, 0xFFF6, 0xFFF7, 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE,
188  0x03FA
189 };
190 
192  0x0002,
193  0x0002, 0x0003, 0x0004, 0x0005, 0x0005, 0x0006, 0x0007, 0x0009, 0x000A, 0x000C,
194  0x0004, 0x0006, 0x0008, 0x0009, 0x000B, 0x000C, 0x0010, 0x0010, 0x0010, 0x0010,
195  0x0005, 0x0008, 0x000A, 0x000C, 0x000F, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
196  0x0005, 0x0008, 0x000A, 0x000C, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
197  0x0006, 0x0009, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
198  0x0006, 0x000A, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
199  0x0007, 0x000B, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
200  0x0007, 0x000B, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
201  0x0008, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
202  0x0009, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
203  0x0009, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
204  0x0009, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
205  0x0009, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
206  0x000B, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
207  0x000E, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
208  0x000F, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
209  0x000A
210 };
211 
212 static uint8_t bitsize [] = {
213  0, 1, 2, 2, 3, 3, 3, 3,
214  4, 4, 4, 4, 4, 4, 4, 4,
215  5, 5, 5, 5, 5, 5, 5, 5,
216  5, 5, 5, 5, 5, 5, 5, 5,
217  6, 6, 6, 6, 6, 6, 6, 6,
218  6, 6, 6, 6, 6, 6, 6, 6,
219  6, 6, 6, 6, 6, 6, 6, 6,
220  6, 6, 6, 6, 6, 6, 6, 6,
221  7, 7, 7, 7, 7, 7, 7, 7,
222  7, 7, 7, 7, 7, 7, 7, 7,
223  7, 7, 7, 7, 7, 7, 7, 7,
224  7, 7, 7, 7, 7, 7, 7, 7,
225  7, 7, 7, 7, 7, 7, 7, 7,
226  7, 7, 7, 7, 7, 7, 7, 7,
227  7, 7, 7, 7, 7, 7, 7, 7,
228  7, 7, 7, 7, 7, 7, 7, 7,
229  8, 8, 8, 8, 8, 8, 8, 8,
230  8, 8, 8, 8, 8, 8, 8, 8,
231  8, 8, 8, 8, 8, 8, 8, 8,
232  8, 8, 8, 8, 8, 8, 8, 8,
233  8, 8, 8, 8, 8, 8, 8, 8,
234  8, 8, 8, 8, 8, 8, 8, 8,
235  8, 8, 8, 8, 8, 8, 8, 8,
236  8, 8, 8, 8, 8, 8, 8, 8,
237  8, 8, 8, 8, 8, 8, 8, 8,
238  8, 8, 8, 8, 8, 8, 8, 8,
239  8, 8, 8, 8, 8, 8, 8, 8,
240  8, 8, 8, 8, 8, 8, 8, 8,
241  8, 8, 8, 8, 8, 8, 8, 8,
242  8, 8, 8, 8, 8, 8, 8, 8,
243  8, 8, 8, 8, 8, 8, 8, 8,
244  8, 8, 8, 8, 8, 8, 8, 8
245 };
246 
247 static uint8_t markerdata [] = {
248  0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
249 
250  0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
251 
252  0xFF, 0xC4, 0x00, 0x1F, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
253 
254  0xFF, 0xC4, 0x00, 0xB5, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
255 };
256 
257 
258 static uint8_t zigzag_table [] = {
259  0, 1, 5, 6, 14, 15, 27, 28,
260  2, 4, 7, 13, 16, 26, 29, 42,
261  3, 8, 12, 17, 25, 30, 41, 43,
262  9, 11, 18, 24, 31, 40, 44, 53,
263  10, 19, 23, 32, 39, 45, 52, 54,
264  20, 22, 33, 38, 46, 51, 55, 60,
265  21, 34, 37, 47, 50, 56, 59, 61,
266  35, 36, 48, 49, 57, 58, 62, 63
267 };
268 /*static uint8_t luminance_quant_table [] = {
269  16, 11, 10, 16, 24, 40, 51, 61,
270  12, 12, 14, 19, 26, 58, 60, 55,
271  14, 13, 16, 24, 40, 57, 69, 56,
272  14, 17, 22, 29, 51, 87, 80, 62,
273  18, 22, 37, 56, 68, 109, 103, 77,
274  24, 35, 55, 64, 81, 104, 113, 92,
275  49, 64, 78, 87, 103, 121, 120, 101,
276  72, 92, 95, 98, 112, 100, 103, 99
277 };
278 
279 static uint8_t chrominance_quant_table [] = {
280  17, 18, 24, 47, 99, 99, 99, 99,
281  18, 21, 26, 66, 99, 99, 99, 99,
282  24, 26, 56, 99, 99, 99, 99, 99,
283  47, 66, 99, 99, 99, 99, 99, 99,
284  99, 99, 99, 99, 99, 99, 99, 99,
285  99, 99, 99, 99, 99, 99, 99, 99,
286  99, 99, 99, 99, 99, 99, 99, 99,
287  99, 99, 99, 99, 99, 99, 99, 99
288 };*/
289 
290 #define JPEG_BLOCK_SIZE 64
295 
298 //static int16_t Y3 [JPEG_BLOCK_SIZE];
299 //static int16_t Y4 [JPEG_BLOCK_SIZE];
303 static uint32_t lcode = 0;
304 static uint16_t bitindex = 0;
305 
306 
307 void (*read_format)(JPEG_ENCODER_STRUCTURE *jpeg_encoder_structure, uint8_t *input_ptr);
308 
309 static void jpeg_initialization(JPEG_ENCODER_STRUCTURE *jpeg, uint32_t image_format, uint32_t image_width, uint32_t image_height)
310 {
311  uint16_t mcu_width, mcu_height, bytes_per_pixel;
312 
313  if (image_format == FOUR_ZERO_ZERO) {
314  jpeg->mcu_width = mcu_width = 8;
315  jpeg->mcu_height = mcu_height = 8;
316 
317  jpeg->horizontal_mcus = (uint16_t)((image_width + mcu_width - 1) >> 3);
318  jpeg->vertical_mcus = (uint16_t)((image_height + mcu_height - 1) >> 3);
319 
320  bytes_per_pixel = 1;
322  } else {
323  jpeg->mcu_width = mcu_width = 16;
324  jpeg->horizontal_mcus = (uint16_t)((image_width + mcu_width - 1) >> 4);
325 
326  jpeg->mcu_height = mcu_height = 8;
327  jpeg->vertical_mcus = (uint16_t)((image_height + mcu_height - 1) >> 3);
328  bytes_per_pixel = 2;
330  }
331 
332  jpeg->rows_in_bottom_mcus = (uint16_t)(image_height - (jpeg->vertical_mcus - 1) * mcu_height);
333  jpeg->cols_in_right_mcus = (uint16_t)(image_width - (jpeg->horizontal_mcus - 1) * mcu_width);
334 
335  jpeg->length_minus_mcu_width = (uint16_t)((image_width - mcu_width) * bytes_per_pixel);
336  jpeg->length_minus_width = (uint16_t)((image_width - jpeg->cols_in_right_mcus) * bytes_per_pixel);
337 
338  jpeg->mcu_width_size = (uint16_t)(mcu_width * bytes_per_pixel);
339 
340  jpeg->offset = (uint16_t)((image_width * (mcu_height - 1) - (mcu_width - jpeg->cols_in_right_mcus)) * bytes_per_pixel);
341 
342  jpeg->ldc1 = 0;
343  jpeg->ldc2 = 0;
344  jpeg->ldc3 = 0;
345  lcode = 0;
346  bitindex = 0;
347 }
348 
350 
352 // Q = 0-99
353 
354 /*
355  * Table K.1 from JPEG spec.
356  */
357 static const int jpeg_luma_quantizer[64] = {
358  16, 11, 10, 16, 24, 40, 51, 61,
359  12, 12, 14, 19, 26, 58, 60, 55,
360  14, 13, 16, 24, 40, 57, 69, 56,
361  14, 17, 22, 29, 51, 87, 80, 62,
362  18, 22, 37, 56, 68, 109, 103, 77,
363  24, 35, 55, 64, 81, 104, 113, 92,
364  49, 64, 78, 87, 103, 121, 120, 101,
365  72, 92, 95, 98, 112, 100, 103, 99
366 };
367 
368 /*
369  * Table K.2 from JPEG spec.
370  */
371 static const int jpeg_chroma_quantizer[64] = {
372  17, 18, 24, 47, 99, 99, 99, 99,
373  18, 21, 26, 66, 99, 99, 99, 99,
374  24, 26, 56, 99, 99, 99, 99, 99,
375  47, 66, 99, 99, 99, 99, 99, 99,
376  99, 99, 99, 99, 99, 99, 99, 99,
377  99, 99, 99, 99, 99, 99, 99, 99,
378  99, 99, 99, 99, 99, 99, 99, 99,
379  99, 99, 99, 99, 99, 99, 99, 99
380 };
381 
382 /*
383  * Call MakeTables with the Q factor and two u_char[64] return arrays
384  */
385 void MakeTables(int q);
386 void MakeTables(int q)
387 {
388  int i;
389  int factor = q;
390 
391  if (q < 1) { factor = 1; }
392  if (q > 99) { factor = 99; }
393  if (q < 50) {
394  q = 5000 / factor;
395  } else {
396  q = 200 - factor * 2;
397  }
398 
399 
400  for (i = 0; i < 64; i++) {
401  int lq = (jpeg_luma_quantizer[i] * q + 50) / 100;
402  int cq = (jpeg_chroma_quantizer[i] * q + 50) / 100;
403 
404  /* Limit the quantizers to 1 <= q <= 255 */
405  if (lq < 1) { lq = 1; }
406  else if (lq > 255) { lq = 255; }
407  Lqt [i] = (uint8_t) lq;
408  ILqt [i] = 0x8000 / lq;
409 
410  if (cq < 1) { cq = 1; }
411  else if (cq > 255) { cq = 255; }
412  Cqt [i] = (uint8_t) cq;
413  //ICqt [i] = DSP_Division (0x8000, value);
414  ICqt [i] = 0x8000 / cq;
415  }
416 }
417 
425 void jpeg_encode_image(struct image_t *in, struct image_t *out, uint32_t quality_factor, bool_t add_dri_header)
426 {
427  uint16_t i, j;
428  uint8_t *output_ptr = out->buf;
429  uint8_t *input_ptr = in->buf;
430  uint32_t image_format = FOUR_ZERO_ZERO;
431 
432  if (in->type == IMAGE_YUV422) {
433  image_format = FOUR_TWO_TWO;
434  }
435  else if (in->type == IMAGE_GRAYSCALE) {
436  image_format = FOUR_ZERO_ZERO;
437  }
438 
439  JPEG_ENCODER_STRUCTURE JpegStruct;
440  JPEG_ENCODER_STRUCTURE *jpeg_encoder_structure = &JpegStruct;
441 
442  /* Initialization of JPEG control structure */
443  jpeg_initialization(jpeg_encoder_structure, image_format, in->w, in->h);
444 
445  /* Quantization Table Initialization */
446  //jpeg_initialize_quantization_tables (quality_factor);
447 
448  MakeTables(quality_factor);
449 
450  /* Writing Marker Data */
451  if (add_dri_header) {
452  output_ptr = jpeg_write_markers(output_ptr, image_format, in->w, in->h);
453  }
454 
455  for (i = 1; i <= jpeg_encoder_structure->vertical_mcus; i++) {
456  if (i < jpeg_encoder_structure->vertical_mcus) {
457  jpeg_encoder_structure->rows = jpeg_encoder_structure->mcu_height;
458  } else {
459  jpeg_encoder_structure->rows = jpeg_encoder_structure->rows_in_bottom_mcus;
460  }
461 
462  for (j = 1; j <= jpeg_encoder_structure->horizontal_mcus; j++) {
463  if (j < jpeg_encoder_structure->horizontal_mcus) {
464  jpeg_encoder_structure->cols = jpeg_encoder_structure->mcu_width;
465  jpeg_encoder_structure->incr = jpeg_encoder_structure->length_minus_mcu_width;
466  } else {
467  jpeg_encoder_structure->cols = jpeg_encoder_structure->cols_in_right_mcus;
468  jpeg_encoder_structure->incr = jpeg_encoder_structure->length_minus_width;
469  }
470 
471  read_format(jpeg_encoder_structure, input_ptr);
472 
473  /* Encode the data in MCU */
474  output_ptr = jpeg_encodeMCU(jpeg_encoder_structure, image_format, output_ptr);
475 
476  input_ptr += jpeg_encoder_structure->mcu_width_size;
477  }
478 
479  input_ptr += jpeg_encoder_structure->offset;
480  }
481 
482  /* Close Routine */
483  output_ptr = jpeg_close_bitstream(output_ptr);
484  out->w = in->w;
485  out->h = in->h;
486  out->buf_size = output_ptr - (uint8_t *)out->buf;
487 }
488 
489 static uint8_t *jpeg_encodeMCU(JPEG_ENCODER_STRUCTURE *jpeg_encoder_structure, uint32_t image_format, uint8_t *output_ptr)
490 {
492  jpeg_DCT(Y1);
494  output_ptr = jpeg_huffman(jpeg_encoder_structure, 1, output_ptr);
495 
496  if (image_format == FOUR_TWO_TWO) {
498  jpeg_DCT(Y2);
500  output_ptr = jpeg_huffman(jpeg_encoder_structure, 1, output_ptr);
501 
503  jpeg_DCT(CB);
505  output_ptr = jpeg_huffman(jpeg_encoder_structure, 2, output_ptr);
506 
508  jpeg_DCT(CR);
510  output_ptr = jpeg_huffman(jpeg_encoder_structure, 3, output_ptr);
511  }
512  return output_ptr;
513 }
514 
515 /* Level shifting to get 8 bit SIGNED values for the data */
516 static void jpeg_levelshift(int16_t *const data)
517 {
518  int16_t i;
519 
520  for (i = 63; i >= 0; i--) {
521  data [i] -= 128;
522  }
523 }
524 
525 /* DCT for One block(8x8) */
526 static void jpeg_DCT(int16_t *data)
527 {
528  //_r8x8dct(data, fdct_coeff, fdct_temp);
529 
530 
531  uint16_t i;
532  int32_t x0, x1, x2, x3, x4, x5, x6, x7, x8;
533 
534  static const uint16_t c1 = 1420; // cos PI/16 * root(2)
535  static const uint16_t c2 = 1338; // cos PI/8 * root(2)
536  static const uint16_t c3 = 1204; // cos 3PI/16 * root(2)
537  static const uint16_t c5 = 805; // cos 5PI/16 * root(2)
538  static const uint16_t c6 = 554; // cos 3PI/8 * root(2)
539  static const uint16_t c7 = 283; // cos 7PI/16 * root(2)
540 
541  static const uint16_t s1 = 3;
542  static const uint16_t s2 = 10;
543  static const uint16_t s3 = 13;
544 
545  for (i = 8; i > 0; i--) {
546  x8 = data [0] + data [7];
547  x0 = data [0] - data [7];
548 
549  x7 = data [1] + data [6];
550  x1 = data [1] - data [6];
551 
552  x6 = data [2] + data [5];
553  x2 = data [2] - data [5];
554 
555  x5 = data [3] + data [4];
556  x3 = data [3] - data [4];
557 
558  x4 = x8 + x5;
559  x8 -= x5;
560 
561  x5 = x7 + x6;
562  x7 -= x6;
563 
564  data [0] = (int16_t)(x4 + x5);
565  data [4] = (int16_t)(x4 - x5);
566 
567  data [2] = (int16_t)((x8 * c2 + x7 * c6) >> s2);
568  data [6] = (int16_t)((x8 * c6 - x7 * c2) >> s2);
569 
570  data [7] = (int16_t)((x0 * c7 - x1 * c5 + x2 * c3 - x3 * c1) >> s2);
571  data [5] = (int16_t)((x0 * c5 - x1 * c1 + x2 * c7 + x3 * c3) >> s2);
572  data [3] = (int16_t)((x0 * c3 - x1 * c7 - x2 * c1 - x3 * c5) >> s2);
573  data [1] = (int16_t)((x0 * c1 + x1 * c3 + x2 * c5 + x3 * c7) >> s2);
574 
575  data += 8;
576  }
577 
578  data -= 64;
579 
580  for (i = 8; i > 0; i--) {
581  x8 = data [0] + data [56];
582  x0 = data [0] - data [56];
583 
584  x7 = data [8] + data [48];
585  x1 = data [8] - data [48];
586 
587  x6 = data [16] + data [40];
588  x2 = data [16] - data [40];
589 
590  x5 = data [24] + data [32];
591  x3 = data [24] - data [32];
592 
593  x4 = x8 + x5;
594  x8 -= x5;
595 
596  x5 = x7 + x6;
597  x7 -= x6;
598 
599  data [0] = (int16_t)((x4 + x5) >> s1);
600  data [32] = (int16_t)((x4 - x5) >> s1);
601 
602  data [16] = (int16_t)((x8 * c2 + x7 * c6) >> s3);
603  data [48] = (int16_t)((x8 * c6 - x7 * c2) >> s3);
604 
605  data [56] = (int16_t)((x0 * c7 - x1 * c5 + x2 * c3 - x3 * c1) >> s3);
606  data [40] = (int16_t)((x0 * c5 - x1 * c1 + x2 * c7 + x3 * c3) >> s3);
607  data [24] = (int16_t)((x0 * c3 - x1 * c7 - x2 * c1 - x3 * c5) >> s3);
608  data [8] = (int16_t)((x0 * c1 + x1 * c3 + x2 * c5 + x3 * c7) >> s3);
609 
610  data++;
611  }
612 }
613 
614 #define PUTBITS \
615  { \
616  bits_in_next_word = (int16_t) (bitindex + numbits - 32); \
617  if (bits_in_next_word < 0) \
618  { \
619  lcode = (lcode << numbits) | data; \
620  bitindex += numbits; \
621  } \
622  else \
623  { \
624  lcode = (lcode << (32 - bitindex)) | (data >> bits_in_next_word); \
625  if ((*output_ptr++ = (uint8_t)(lcode >> 24)) == 0xff) \
626  *output_ptr++ = 0; \
627  if ((*output_ptr++ = (uint8_t)(lcode >> 16)) == 0xff) \
628  *output_ptr++ = 0; \
629  if ((*output_ptr++ = (uint8_t)(lcode >> 8)) == 0xff) \
630  *output_ptr++ = 0; \
631  if ((*output_ptr++ = (uint8_t) lcode) == 0xff) \
632  *output_ptr++ = 0; \
633  lcode = data; \
634  bitindex = bits_in_next_word; \
635  } \
636  }
637 
638 static uint8_t *jpeg_huffman(JPEG_ENCODER_STRUCTURE *jpeg_encoder_structure, uint16_t component, uint8_t *output_ptr)
639 {
640  uint16_t i;
641  uint16_t *DcCodeTable, *DcSizeTable, *AcCodeTable, *AcSizeTable;
642 
643  int16_t *Temp_Ptr, Coeff, LastDc;
644  uint16_t AbsCoeff, HuffCode, HuffSize, RunLength = 0, DataSize = 0, index;
645 
646  int16_t bits_in_next_word;
647  uint16_t numbits;
648  uint32_t data;
649 
650  Temp_Ptr = Temp;
651  Coeff = *Temp_Ptr++;
652 
653  if (component == 1) {
654  DcCodeTable = luminance_dc_code_table;
655  DcSizeTable = luminance_dc_size_table;
656  AcCodeTable = luminance_ac_code_table;
657  AcSizeTable = luminance_ac_size_table;
658 
659  LastDc = jpeg_encoder_structure->ldc1;
660  jpeg_encoder_structure->ldc1 = Coeff;
661  } else {
662  DcCodeTable = chrominance_dc_code_table;
663  DcSizeTable = chrominance_dc_size_table;
664  AcCodeTable = chrominance_ac_code_table;
665  AcSizeTable = chrominance_ac_size_table;
666 
667  if (component == 2) {
668  LastDc = jpeg_encoder_structure->ldc2;
669  jpeg_encoder_structure->ldc2 = Coeff;
670  } else {
671  LastDc = jpeg_encoder_structure->ldc3;
672  jpeg_encoder_structure->ldc3 = Coeff;
673  }
674  }
675 
676  Coeff -= LastDc;
677 
678  AbsCoeff = (Coeff < 0) ? -Coeff-- : Coeff;
679 
680  while (AbsCoeff != 0) {
681  AbsCoeff >>= 1;
682  DataSize++;
683  }
684 
685  HuffCode = DcCodeTable [DataSize];
686  HuffSize = DcSizeTable [DataSize];
687 
688  Coeff &= (1 << DataSize) - 1;
689  data = (HuffCode << DataSize) | Coeff;
690  numbits = HuffSize + DataSize;
691 
692  PUTBITS
693 
694  for (i = 63; i > 0; i--) {
695  if ((Coeff = *Temp_Ptr++) != 0) {
696  while (RunLength > 15) {
697  RunLength -= 16;
698  data = AcCodeTable [161];
699  numbits = AcSizeTable [161];
700  PUTBITS
701  }
702 
703  AbsCoeff = (Coeff < 0) ? -Coeff-- : Coeff;
704 
705  if (AbsCoeff >> 8 == 0) {
706  DataSize = bitsize [AbsCoeff];
707  } else {
708  DataSize = bitsize [AbsCoeff >> 8] + 8;
709  }
710 
711  index = RunLength * 10 + DataSize;
712  HuffCode = AcCodeTable [index];
713  HuffSize = AcSizeTable [index];
714 
715  Coeff &= (1 << DataSize) - 1;
716  data = (HuffCode << DataSize) | Coeff;
717  numbits = HuffSize + DataSize;
718 
719  PUTBITS
720  RunLength = 0;
721  } else {
722  RunLength++;
723  }
724  }
725 
726  if (RunLength != 0) {
727  data = AcCodeTable [0];
728  numbits = AcSizeTable [0];
729  PUTBITS
730  }
731  return output_ptr;
732 }
733 
734 /* For bit Stuffing and EOI marker */
736 {
737  uint16_t i, count;
738  uint8_t *ptr;
739 
740  if (bitindex > 0) {
741  lcode <<= (32 - bitindex);
742  //for (i=0; i<bitindex; i++)
743  // lcode |= (0x00000001 << i);
744 
745  count = (bitindex + 7) >> 3;
746 
747  ptr = (uint8_t *) &lcode + 3;
748 
749  for (i = 0; i < count; i++)
750  if ((*output_ptr++ = *ptr--) == 0xff) {
751  *output_ptr++ = 0;
752  }
753  }
754 
755  // End of image marker
756  *output_ptr++ = 0xFF;
757  *output_ptr++ = 0xD9;
758  return output_ptr;
759 }
760 
761 static uint8_t *jpeg_write_markers(uint8_t *output_ptr, uint32_t image_format, uint32_t image_width, uint32_t image_height)
762 {
763  uint16_t i, header_length;
764  uint8_t number_of_components;
765 
766  // Start of image marker
767  *output_ptr++ = 0xFF;
768  *output_ptr++ = 0xD8;
769 
770  // Quantization table marker
771  *output_ptr++ = 0xFF;
772  *output_ptr++ = 0xDB;
773 
774  // Quantization table length
775  *output_ptr++ = 0x00;
776  *output_ptr++ = 0x43;
777 
778  // Pq, Tq
779  *output_ptr++ = 0x00;
780 
781  // Lqt table
782  for (i = 0; i < 64; i++) {
783  *output_ptr++ = Lqt [i];
784  }
785 
786  // Quantization table marker
787  *output_ptr++ = 0xFF;
788  *output_ptr++ = 0xDB;
789 
790  // Quantization table length
791  *output_ptr++ = 0x00;
792  *output_ptr++ = 0x43;
793 
794  // Pq, Tq
795  *output_ptr++ = 0x01;
796 
797  // Cqt table
798  for (i = 0; i < 64; i++) {
799  *output_ptr++ = Cqt [i];
800  }
801 
802  if (image_format == FOUR_ZERO_ZERO) {
803  number_of_components = 1;
804  } else {
805  number_of_components = 3;
806  }
807 
808  // Frame header(SOF)
809 
810  // Start of frame marker
811  *output_ptr++ = 0xFF;
812  *output_ptr++ = 0xC0;
813 
814  header_length = (uint16_t)(8 + 3 * number_of_components);
815 
816  // Frame header length
817  *output_ptr++ = (uint8_t)(header_length >> 8);
818  *output_ptr++ = (uint8_t) header_length;
819 
820  // Precision (P)
821  *output_ptr++ = 0x08;
822 
823  // image height
824  *output_ptr++ = (uint8_t)(image_height >> 8);
825  *output_ptr++ = (uint8_t) image_height;
826 
827  // image width
828  *output_ptr++ = (uint8_t)(image_width >> 8);
829  *output_ptr++ = (uint8_t) image_width;
830 
831  // Nf
832  *output_ptr++ = number_of_components;
833 
834  if (image_format == FOUR_ZERO_ZERO) {
835  *output_ptr++ = 0x01;
836  *output_ptr++ = 0x11;
837  *output_ptr++ = 0x00;
838  } else {
839  *output_ptr++ = 0x01;
840 
841  if (image_format == FOUR_TWO_TWO) {
842  *output_ptr++ = 0x21;
843  } else {
844  *output_ptr++ = 0x11;
845  }
846 
847  *output_ptr++ = 0x00;
848 
849  *output_ptr++ = 0x02;
850  *output_ptr++ = 0x11;
851  *output_ptr++ = 0x01;
852 
853  *output_ptr++ = 0x03;
854  *output_ptr++ = 0x11;
855  *output_ptr++ = 0x01;
856  }
857 
858  // huffman table(DHT)
859 
860  for (i = 0; i < sizeof(markerdata); i++) {
861  *output_ptr++ = markerdata [i];
862  }
863 
864 
865  // Scan header(SOF)
866 
867  // Start of scan marker
868  *output_ptr++ = 0xFF;
869  *output_ptr++ = 0xDA;
870 
871  header_length = (uint16_t)(6 + (number_of_components << 1));
872 
873  // Scan header length
874  *output_ptr++ = (uint8_t)(header_length >> 8);
875  *output_ptr++ = (uint8_t) header_length;
876 
877  // Ns
878  *output_ptr++ = number_of_components;
879 
880  if (image_format == FOUR_ZERO_ZERO) {
881  *output_ptr++ = 0x01;
882  *output_ptr++ = 0x00;
883  } else {
884  *output_ptr++ = 0x01;
885  *output_ptr++ = 0x00;
886 
887  *output_ptr++ = 0x02;
888  *output_ptr++ = 0x11;
889 
890  *output_ptr++ = 0x03;
891  *output_ptr++ = 0x11;
892  }
893 
894  *output_ptr++ = 0x00;
895  *output_ptr++ = 0x3F;
896  *output_ptr++ = 0x00;
897  return output_ptr;
898 }
899 
900 /* Multiply Quantization table with quality factor to get LQT and CQT
901  factor ranges from 1 to 8; 1 = highest quality, 8 = lowest quality */
902 /*static void jpeg_initialize_quantization_tables(uint32_t quality_factor)
903 {
904  uint16_t i, index;
905  uint32_t value;
906 
907  if (quality_factor < 1) {
908  quality_factor = 1;
909  }
910  if (quality_factor > 8) {
911  quality_factor = 8;
912  }
913  quality_factor = ((quality_factor * 3) - 2) * 128; //converts range[1:8] to [1:22]
914 
915  for (i = 0; i < 64; i++) {
916  index = zigzag_table [i];
917 
918  // luminance quantization table * quality factor
919  value = luminance_quant_table [i] * quality_factor;
920  value = (value + 0x200) >> 10;
921 
922  if (value < 2) {
923  value = 2;
924  } else if (value > 255) {
925  value = 255;
926  }
927 
928  Lqt [index] = (uint8_t) value;
929  //ILqt [i] = DSP_Division (0x8000, value);
930  ILqt [i] = 0x8000 / value;
931 
932  // chrominance quantization table * quality factor
933  value = chrominance_quant_table [i] * quality_factor;
934  value = (value + 0x200) >> 10;
935 
936  if (value < 2) {
937  value = 2;
938  } else if (value > 255) {
939  value = 255;
940  }
941 
942  Cqt [index] = (uint8_t) value;
943  //ICqt [i] = DSP_Division (0x8000, value);
944  ICqt [i] = 0x8000 / value;
945  }
946 }*/
947 
948 /* multiply DCT Coefficients with Quantization table and store in ZigZag location */
949 static void jpeg_quantization(int16_t *const data, uint16_t *const quant_table_ptr)
950 {
951  int16_t i;
952  int32_t value;
953 
954  for (i = 63; i >= 0; i--) {
955  value = data [i] * quant_table_ptr [i];
956  value = (value + 0x4000) >> 15;
957 
958  Temp [zigzag_table [i]] = (int16_t) value;
959  }
960 }
961 
962 static void jpeg_read_400_format(JPEG_ENCODER_STRUCTURE *jpeg_encoder_structure, uint8_t *input_ptr)
963 {
964  int32_t i, j;
965  int16_t *Y1_Ptr = Y1;
966 
967  uint16_t rows = jpeg_encoder_structure->rows;
968  uint16_t cols = jpeg_encoder_structure->cols;
969  uint16_t incr = jpeg_encoder_structure->incr;
970 
971  for (i = rows; i > 0; i--) {
972  for (j = cols; j > 0; j--) {
973  *Y1_Ptr++ = *input_ptr++;
974  }
975 
976  for (j = 8 - cols; j > 0; j--) {
977  *Y1_Ptr = *(Y1_Ptr - 1);
978  Y1_Ptr++;
979  }
980 
981  input_ptr += incr;
982  }
983 
984  for (i = 8 - rows; i > 0; i--) {
985  for (j = 8; j > 0; j--) {
986  *Y1_Ptr = *(Y1_Ptr - 8);
987  Y1_Ptr++;
988  }
989  }
990 }
991 
992 static void jpeg_read_422_format(JPEG_ENCODER_STRUCTURE *jpeg_encoder_structure, uint8_t *input_ptr)
993 {
994  int32_t i, j;
995  uint16_t Y1_cols, Y2_cols;
996 
997  int16_t *Y1_Ptr = Y1;
998  int16_t *Y2_Ptr = Y2;
999  int16_t *CB_Ptr = CB;
1000  int16_t *CR_Ptr = CR;
1001 
1002  uint16_t rows = jpeg_encoder_structure->rows;
1003  uint16_t cols = jpeg_encoder_structure->cols;
1004  uint16_t incr = jpeg_encoder_structure->incr;
1005 
1006  if (cols <= 8) {
1007  Y1_cols = cols;
1008  Y2_cols = 0;
1009  } else {
1010  Y1_cols = 8;
1011  Y2_cols = (uint16_t)(cols - 8);
1012  }
1013 
1014  for (i = rows; i > 0; i--) {
1015  for (j = Y1_cols >> 1; j > 0; j--) {
1016  *CB_Ptr++ = *input_ptr++;
1017  *Y1_Ptr++ = *input_ptr++;
1018  *CR_Ptr++ = *input_ptr++;
1019  *Y1_Ptr++ = *input_ptr++;
1020  }
1021 
1022  for (j = Y2_cols >> 1; j > 0; j--) {
1023  *CB_Ptr++ = *input_ptr++;
1024  *Y2_Ptr++ = *input_ptr++;
1025  *CR_Ptr++ = *input_ptr++;
1026  *Y2_Ptr++ = *input_ptr++;
1027  }
1028 
1029  if (cols <= 8) {
1030  for (j = 8 - Y1_cols; j > 0; j--) {
1031  *Y1_Ptr = *(Y1_Ptr - 1);
1032  Y1_Ptr++;
1033  }
1034 
1035  for (j = 8 - Y2_cols; j > 0; j--) {
1036  *Y2_Ptr = *(Y1_Ptr - 1);
1037  Y2_Ptr++;
1038  }
1039  } else {
1040  for (j = 8 - Y2_cols; j > 0; j--) {
1041  *Y2_Ptr = *(Y2_Ptr - 1);
1042  Y2_Ptr++;
1043  }
1044  }
1045 
1046  for (j = (16 - cols) >> 1; j > 0; j--) {
1047  *CB_Ptr = *(CB_Ptr - 1); CB_Ptr++;
1048  *CR_Ptr = *(CR_Ptr - 1); CR_Ptr++;
1049  }
1050 
1051  input_ptr += incr;
1052  }
1053 
1054  for (i = 8 - rows; i > 0; i--) {
1055  for (j = 8; j > 0; j--) {
1056  *Y1_Ptr = *(Y1_Ptr - 8); Y1_Ptr++;
1057  *Y2_Ptr = *(Y2_Ptr - 8); Y2_Ptr++;
1058  *CB_Ptr = *(CB_Ptr - 8); CB_Ptr++;
1059  *CR_Ptr = *(CR_Ptr - 8); CR_Ptr++;
1060  }
1061  }
1062 }
1063 
#define FOUR_TWO_TWO
Definition: jpeg.h:30
unsigned short uint16_t
Definition: types.h:16
static int16_t CB[JPEG_BLOCK_SIZE]
Definition: jpeg.c:300
static uint8_t * jpeg_close_bitstream(uint8_t *)
Definition: jpeg.c:735
static void jpeg_initialization(JPEG_ENCODER_STRUCTURE *, uint32_t, uint32_t, uint32_t)
Definition: jpeg.c:309
static uint16_t luminance_dc_size_table[]
Definition: jpeg.c:113
uint32_t buf_size
The buffer size.
Definition: image.h:49
uint16_t offset
Definition: jpeg.c:78
static uint8_t markerdata[]
Definition: jpeg.c:247
static uint16_t ICqt[JPEG_BLOCK_SIZE]
Definition: jpeg.c:294
static uint8_t * jpeg_write_markers(uint8_t *, uint32_t, uint32_t, uint32_t)
Definition: jpeg.c:761
static uint16_t chrominance_ac_size_table[]
Definition: jpeg.c:191
uint16_t value
Definition: adc_arch.c:586
static uint8_t Lqt[JPEG_BLOCK_SIZE]
Definition: jpeg.c:291
static uint8_t * jpeg_encodeMCU(JPEG_ENCODER_STRUCTURE *, uint32_t, uint8_t *)
Definition: jpeg.c:489
static uint8_t Cqt[JPEG_BLOCK_SIZE]
Definition: jpeg.c:292
uint16_t horizontal_mcus
Definition: jpeg.c:66
#define PUTBITS
Definition: jpeg.c:614
uint16_t length_minus_width
Definition: jpeg.c:75
static unsigned char svs_size_code(int w)
Definition: jpeg.c:23
Definition: image.h:42
static uint16_t ILqt[JPEG_BLOCK_SIZE]
Definition: jpeg.c:293
static void jpeg_DCT(int16_t *)
Definition: jpeg.c:526
uint16_t length_minus_mcu_width
Definition: jpeg.c:74
static uint16_t luminance_ac_code_table[]
Definition: jpeg.c:128
static const int jpeg_chroma_quantizer[64]
Definition: jpeg.c:371
uint16_t cols
Definition: jpeg.c:72
static int16_t Y1[JPEG_BLOCK_SIZE]
Definition: jpeg.c:296
uint16_t vertical_mcus
Definition: jpeg.c:67
static void jpeg_read_422_format(JPEG_ENCODER_STRUCTURE *, uint8_t *)
Definition: jpeg.c:992
uint16_t incr
Definition: jpeg.c:76
void jpeg_encode_image(struct image_t *in, struct image_t *out, uint32_t quality_factor, bool_t add_dri_header)
Encode an YUV422 image.
Definition: jpeg.c:425
static void jpeg_levelshift(int16_t *)
Definition: jpeg.c:516
static uint16_t chrominance_ac_code_table[]
Definition: jpeg.c:170
#define FOUR_ZERO_ZERO
Definition: jpeg.h:28
uint16_t mcu_height
Definition: jpeg.c:65
static uint16_t bitindex
Definition: jpeg.c:304
static int16_t CR[JPEG_BLOCK_SIZE]
Definition: jpeg.c:301
uint16_t w
Image width.
Definition: image.h:44
unsigned long uint32_t
Definition: types.h:18
static int16_t Y2[JPEG_BLOCK_SIZE]
Definition: jpeg.c:297
uint16_t h
Image height.
Definition: image.h:45
signed short int16_t
Definition: types.h:17
static uint16_t c1
Definition: baro_MS5534A.c:203
void * buf
Image buffer (depending on the image_type)
Definition: image.h:50
static const int jpeg_luma_quantizer[64]
Definition: jpeg.c:357
void(* read_format)(JPEG_ENCODER_STRUCTURE *jpeg_encoder_structure, uint8_t *input_ptr)
Definition: jpeg.c:307
#define JPEG_BLOCK_SIZE
Definition: jpeg.c:290
uint16_t cols_in_right_mcus
Definition: jpeg.c:68
static uint16_t c6
Definition: baro_MS5534A.c:203
Encode images with the use of the JPEG encoding.
uint16_t rows
Definition: jpeg.c:71
static uint16_t c3
Definition: baro_MS5534A.c:203
struct JPEG_ENCODER_STRUCTURE JPEG_ENCODER_STRUCTURE
signed long int32_t
Definition: types.h:19
static uint8_t bitsize[]
Definition: jpeg.c:212
static uint16_t chrominance_dc_size_table[]
Definition: jpeg.c:123
static uint16_t luminance_dc_code_table[]
Definition: jpeg.c:108
unsigned char uint8_t
Definition: types.h:14
static uint32_t lcode
Definition: jpeg.c:303
static int16_t Temp[JPEG_BLOCK_SIZE]
Definition: jpeg.c:302
static uint16_t c2
Definition: baro_MS5534A.c:203
UYVY format (uint16 per pixel)
Definition: image.h:35
static float p[2][2]
int jpeg_create_svs_header(unsigned char *jpegbuf, int32_t size, int w)
Definition: jpeg.c:44
uint16_t rows_in_bottom_mcus
Definition: jpeg.c:69
Grayscale image with only the Y part (uint8 per pixel)
Definition: image.h:36
static uint8_t zigzag_table[]
Definition: jpeg.c:258
uint16_t mcu_width
Definition: jpeg.c:64
static uint16_t chrominance_dc_code_table[]
Definition: jpeg.c:118
static uint16_t luminance_ac_size_table[]
Definition: jpeg.c:149
void MakeTables(int q)
Definition: jpeg.c:386
uint16_t mcu_width_size
Definition: jpeg.c:77
static void jpeg_read_400_format(JPEG_ENCODER_STRUCTURE *, uint8_t *)
Definition: jpeg.c:962
static uint8_t * jpeg_huffman(JPEG_ENCODER_STRUCTURE *, uint16_t, uint8_t *)
Definition: jpeg.c:638
static void jpeg_quantization(int16_t *, uint16_t *)
Definition: jpeg.c:949
enum image_type type
The image type.
Definition: image.h:43