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