Debian Codegraph @

lib/libv4lconvert/jpeg.c

raw
1 /*
2 # (C) 2008-2011 Hans de Goede <[email protected]>
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU Lesser General Public License as published by
6 # the Free Software Foundation; either version 2.1 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17 */
18
19 #include <errno.h>
20 #include <stdlib.h>
21 #include "libv4lconvert-priv.h"
22 #ifdef HAVE_JPEG
23 #include "jpeg_memsrcdest.h"
24 #endif
25
26 int v4lconvert_decode_jpeg_tinyjpeg(struct v4lconvert_data *data,
27 unsigned char *src, int src_size, unsigned char *dest,
28 struct v4l2_format *fmt, unsigned int dest_pix_fmt, int flags)
29 {
30 int result = 0;
31 unsigned char *components[3];
32 unsigned int header_width, header_height;
33 unsigned int width = fmt->fmt.pix.width;
34 unsigned int height = fmt->fmt.pix.height;
35
36 if (!data->tinyjpeg) {
37 data->tinyjpeg = tinyjpeg_init();
38 if (!data->tinyjpeg)
39 return v4lconvert_oom_error(data);
40 }
41 flags |= TINYJPEG_FLAGS_MJPEG_TABLE;
42 tinyjpeg_set_flags(data->tinyjpeg, flags);
43 if (tinyjpeg_parse_header(data->tinyjpeg, src, src_size)) {
44 V4LCONVERT_ERR("parsing JPEG header: %s",
45 tinyjpeg_get_errorstring(data->tinyjpeg));
46 errno = EAGAIN;
47 return -1;
48 }
49 tinyjpeg_get_size(data->tinyjpeg, &header_width, &header_height);
50
51 if (data->control_flags & V4LCONTROL_ROTATED_90_JPEG) {
52 unsigned int tmp = width;
53 width = height;
54 height = tmp;
55 }
56
57 if (header_width != width || header_height != height) {
58 V4LCONVERT_ERR("unexpected width / height in JPEG header: "
59 "expected: %ux%u, header: %ux%u\n",
60 width, height, header_width, header_height);
61 errno = EIO;
62 return -1;
63 }
64 fmt->fmt.pix.width = header_width;
65 fmt->fmt.pix.height = header_height;
66
67 components[0] = dest;
68
69 switch (dest_pix_fmt) {
70 case V4L2_PIX_FMT_RGB24:
71 tinyjpeg_set_components(data->tinyjpeg, components, 1);
72 result = tinyjpeg_decode(data->tinyjpeg, TINYJPEG_FMT_RGB24);
73 break;
74 case V4L2_PIX_FMT_BGR24:
75 tinyjpeg_set_components(data->tinyjpeg, components, 1);
76 result = tinyjpeg_decode(data->tinyjpeg, TINYJPEG_FMT_BGR24);
77 break;
78 case V4L2_PIX_FMT_YUV420:
79 components[1] = components[0] + width * height;
80 components[2] = components[1] + width * height / 4;
81 tinyjpeg_set_components(data->tinyjpeg, components, 3);
82 result = tinyjpeg_decode(data->tinyjpeg, TINYJPEG_FMT_YUV420P);
83 break;
84 case V4L2_PIX_FMT_YVU420:
85 components[2] = components[0] + width * height;
86 components[1] = components[2] + width * height / 4;
87 tinyjpeg_set_components(data->tinyjpeg, components, 3);
88 result = tinyjpeg_decode(data->tinyjpeg, TINYJPEG_FMT_YUV420P);
89 break;
90 }
91
92 if (result) {
93 /* The JPEG header checked out ok but we got an error
94 during decompression. Some webcams, esp pixart and
95 sn9c20x based ones regulary generate corrupt frames,
96 which are best thrown away to avoid flashes in the
97 video stream. We use EPIPE to signal the upper layer
98 we have some video data, but it is incomplete.
99
100 The upper layer (usually libv4l2) should respond to
101 this by trying a number of times to get a new frame
102 and if that fails just passing up whatever we did
103 manage to decompress. */
104 V4LCONVERT_ERR("decompressing JPEG: %s",
105 tinyjpeg_get_errorstring(data->tinyjpeg));
106 errno = EPIPE;
107 return -1;
108 }
109 return 0;
110 }
111
112 #ifdef HAVE_JPEG
113
114 static void jerr_error_exit(j_common_ptr cinfo)
115 {
116 struct v4lconvert_data *data = cinfo->client_data;
117
118 longjmp(data->jerr_jmp_state, data->jerr_errno);
119 }
120
121 static void jerr_emit_message(j_common_ptr cinfo, int msg_level)
122 {
123 char buffer[JMSG_LENGTH_MAX];
124 struct v4lconvert_data *data = cinfo->client_data;
125
126 /* < -1 error, == -1 warning, >= 0 trace */
127 if (msg_level < -1)
128 return;
129
130 cinfo->err->format_message(cinfo, buffer);
131 snprintf(data->error_msg, V4LCONVERT_ERROR_MSG_SIZE,
132 "v4l-convert: libjpeg error: %s\n", buffer);
133 }
134
135 static void init_libjpeg_cinfo(struct v4lconvert_data *data)
136 {
137 struct jpeg_compress_struct cinfo;
138 unsigned char *jpeg_header = NULL;
139 #if JPEG_LIB_VERSION >= 90
140 size_t jpeg_header_size = 0;
141 #else
142 unsigned long jpeg_header_size = 0;
143 #endif
144
145 if (data->cinfo_initialized)
146 return;
147
148 /* Setup our error handling */
149 jpeg_std_error(&data->jerr);
150 data->jerr.error_exit = jerr_error_exit;
151 data->jerr.emit_message = jerr_emit_message;
152
153 /* Create a jpeg compression object with default params and write
154 default jpeg headers to a mem buffer, so that we can use them to
155 pre-fill a jpeg_decompress_struct with default quant and huffman
156 tables, so that libjpeg can be used to parse [m]jpg-s with
157 incomplete headers */
158 cinfo.err = &data->jerr;
159 cinfo.client_data = data;
160 jpeg_create_compress(&cinfo);
161 jpeg_mem_dest(&cinfo, &jpeg_header, &jpeg_header_size);
162 cinfo.input_components = 3;
163 cinfo.in_color_space = JCS_RGB;
164 jpeg_set_defaults(&cinfo);
165 jpeg_write_tables(&cinfo);
166 jpeg_destroy_compress(&cinfo);
167
168 /* Init the jpeg_decompress_struct */
169 data->cinfo.err = &data->jerr;
170 data->cinfo.client_data = data;
171 jpeg_create_decompress(&data->cinfo);
172 jpeg_mem_src(&data->cinfo, jpeg_header, jpeg_header_size);
173 jpeg_read_header(&data->cinfo, FALSE);
174
175 free(jpeg_header);
176 data->cinfo_initialized = 1;
177 }
178
179 static int decode_libjpeg_h_samp1(struct v4lconvert_data *data,
180 unsigned char *ydest, unsigned char *udest, unsigned char *vdest,
181 int v_samp)
182 {
183 struct jpeg_decompress_struct *cinfo = &data->cinfo;
184 int x, y;
185 unsigned char *uv_buf;
186 unsigned int width = cinfo->image_width;
187 JSAMPROW y_rows[16], u_rows[8], v_rows[8];
188 JSAMPARRAY rows[3] = { y_rows, u_rows, v_rows };
189
190 uv_buf = v4lconvert_alloc_buffer(width * 16,
191 &data->convert_pixfmt_buf,
192 &data->convert_pixfmt_buf_size);
193 if (!uv_buf)
194 return v4lconvert_oom_error(data);
195
196 for (y = 0; y < 8; y++) {
197 u_rows[y] = uv_buf;
198 uv_buf += width;
199 v_rows[y] = uv_buf;
200 uv_buf += width;
201 }
202 uv_buf -= width * 16;
203
204 while (cinfo->output_scanline < cinfo->image_height) {
205 for (y = 0; y < 8 * v_samp; y++) {
206 y_rows[y] = ydest;
207 ydest += cinfo->image_width;
208 }
209 y = jpeg_read_raw_data(cinfo, rows, 8 * v_samp);
210 if (y != 8 * v_samp)
211 return -1;
212
213 /* For v_samp == 1 skip copying uv vals every other time */
214 if (cinfo->output_scanline % 16)
215 continue;
216
217 /* Copy over every other u + v pixel for 8 lines */
218 for (y = 0; y < 8; y++) {
219 for (x = 0; x < width; x += 2) {
220 *udest++ = *uv_buf++;
221 uv_buf++;
222 }
223 for (x = 0; x < width; x += 2) {
224 *vdest++ = *uv_buf++;
225 uv_buf++;
226 }
227 }
228 uv_buf -= width * 16;
229 }
230 return 0;
231 }
232
233 static int decode_libjpeg_h_samp2(struct v4lconvert_data *data,
234 unsigned char *ydest, unsigned char *udest, unsigned char *vdest,
235 int v_samp)
236 {
237 struct jpeg_decompress_struct *cinfo = &data->cinfo;
238 int y;
239 unsigned int width = cinfo->image_width;
240 JSAMPROW y_rows[16], u_rows[8], v_rows[8];
241 JSAMPARRAY rows[3] = { y_rows, u_rows, v_rows };
242
243 while (cinfo->output_scanline < cinfo->image_height) {
244 for (y = 0; y < 8 * v_samp; y++) {
245 y_rows[y] = ydest;
246 ydest += width;
247 }
248 /*
249 * For v_samp == 1 were going to get 1 set of uv values per
250 * line, but we need only 1 set per 2 lines since our output
251 * has v_samp == 2. We store every 2 sets in 1 line,
252 * effectively using the second set for each output line.
253 */
254 if (v_samp == 1) {
255 for (y = 0; y < 8; y++) {
256 u_rows[y] = udest;
257 v_rows[y] = vdest;
258 y++;
259 u_rows[y] = udest;
260 v_rows[y] = vdest;
261 udest += width / 2;
262 vdest += width / 2;
263 }
264 } else { /* v_samp == 2 */
265 for (y = 0; y < 8; y++) {
266 u_rows[y] = udest;
267 v_rows[y] = vdest;
268 udest += width / 2;
269 vdest += width / 2;
270 }
271 }
272
273 y = jpeg_read_raw_data(cinfo, rows, 8 * v_samp);
274 if (y != 8 * v_samp)
275 return -1;
276 }
277 return 0;
278 }
279
280 int v4lconvert_decode_jpeg_libjpeg(struct v4lconvert_data *data,
281 unsigned char *src, int src_size, unsigned char *dest,
282 struct v4l2_format *fmt, unsigned int dest_pix_fmt)
283 {
284 unsigned int width = fmt->fmt.pix.width;
285 unsigned int height = fmt->fmt.pix.height;
286 int result = 0;
287
288 /* libjpeg errors before decoding the first line should signal EAGAIN */
289 data->jerr_errno = EAGAIN;
290 result = setjmp(data->jerr_jmp_state);
291 if (result) {
292 if (data->cinfo_initialized)
293 jpeg_abort_decompress(&data->cinfo);
294 errno = result;
295 return -1;
296 }
297
298 init_libjpeg_cinfo(data);
299
300 jpeg_mem_src(&data->cinfo, src, src_size);
301 jpeg_read_header(&data->cinfo, TRUE);
302
303 if (data->cinfo.image_width != width ||
304 data->cinfo.image_height != height) {
305 V4LCONVERT_ERR("unexpected width / height in JPEG header: "
306 "expected: %ux%u, header: %ux%u\n", width,
307 height, data->cinfo.image_width,
308 data->cinfo.image_height);
309 errno = EIO;
310 return -1;
311 }
312
313 if (data->cinfo.num_components != 3) {
314 V4LCONVERT_ERR("unexpected no components in JPEG: %d\n",
315 data->cinfo.num_components);
316 errno = EIO;
317 return -1;
318 }
319
320 if (dest_pix_fmt == V4L2_PIX_FMT_RGB24 ||
321 dest_pix_fmt == V4L2_PIX_FMT_BGR24) {
322 JSAMPROW row_pointer[1];
323
324 #ifdef JCS_EXTENSIONS
325 if (dest_pix_fmt == V4L2_PIX_FMT_BGR24)
326 data->cinfo.out_color_space = JCS_EXT_BGR;
327 #endif
328 row_pointer[0] = dest;
329 jpeg_start_decompress(&data->cinfo);
330 /* Make libjpeg errors report that we've got some data */
331 data->jerr_errno = EPIPE;
332 while (data->cinfo.output_scanline < height) {
333 jpeg_read_scanlines(&data->cinfo, row_pointer, 1);
334 row_pointer[0] += 3 * width;
335 }
336 jpeg_finish_decompress(&data->cinfo);
337 #ifndef JCS_EXTENSIONS
338 if (dest_pix_fmt == V4L2_PIX_FMT_BGR24)
339 v4lconvert_swap_rgb(dest, dest, width, height);
340 #endif
341 } else {
342 int h_samp, v_samp;
343 unsigned char *udest, *vdest;
344
345 if (data->cinfo.max_h_samp_factor == 2 &&
346 data->cinfo.cur_comp_info[0]->h_samp_factor == 2 &&
347 data->cinfo.cur_comp_info[1]->h_samp_factor == 1 &&
348 data->cinfo.cur_comp_info[2]->h_samp_factor == 1) {
349 h_samp = 2;
350 #if 0 /* HDG: untested, disable for now */
351 } else if (data->cinfo.max_h_samp_factor == 1 &&
352 data->cinfo.cur_comp_info[0]->h_samp_factor == 1 &&
353 data->cinfo.cur_comp_info[1]->h_samp_factor == 1 &&
354 data->cinfo.cur_comp_info[2]->h_samp_factor == 1) {
355 h_samp = 1;
356 #endif
357 } else {
358 fprintf(stderr,
359 "libv4lconvert: unsupported jpeg h-sampling "
360 "factors %d:%d:%d, please report this to "
361 "[email protected]\n",
362 data->cinfo.cur_comp_info[0]->h_samp_factor,
363 data->cinfo.cur_comp_info[1]->h_samp_factor,
364 data->cinfo.cur_comp_info[2]->h_samp_factor);
365 errno = EOPNOTSUPP;
366 return -1;
367 }
368
369 if (data->cinfo.max_v_samp_factor == 2 &&
370 data->cinfo.cur_comp_info[0]->v_samp_factor == 2 &&
371 data->cinfo.cur_comp_info[1]->v_samp_factor == 1 &&
372 data->cinfo.cur_comp_info[2]->v_samp_factor == 1) {
373 v_samp = 2;
374 } else if (data->cinfo.max_v_samp_factor == 1 &&
375 data->cinfo.cur_comp_info[0]->v_samp_factor == 1 &&
376 data->cinfo.cur_comp_info[1]->v_samp_factor == 1 &&
377 data->cinfo.cur_comp_info[2]->v_samp_factor == 1) {
378 v_samp = 1;
379 } else {
380 fprintf(stderr,
381 "libv4lconvert: unsupported jpeg v-sampling "
382 "factors %d:%d:%d, please report this to "
383 "[email protected]\n",
384 data->cinfo.cur_comp_info[0]->v_samp_factor,
385 data->cinfo.cur_comp_info[1]->v_samp_factor,
386 data->cinfo.cur_comp_info[2]->v_samp_factor);
387 errno = EOPNOTSUPP;
388 return -1;
389 }
390
391 /* We don't want any padding as that may overflow our dest */
392 if (width % (8 * h_samp) || height % (8 * v_samp)) {
393 V4LCONVERT_ERR(
394 "resolution is not a multiple of dctsize");
395 errno = EIO;
396 return -1;
397 }
398
399 if (dest_pix_fmt == V4L2_PIX_FMT_YVU420) {
400 vdest = dest + width * height;
401 udest = vdest + (width * height) / 4;
402 } else {
403 udest = dest + width * height;
404 vdest = udest + (width * height) / 4;
405 }
406
407 data->cinfo.raw_data_out = TRUE;
408 data->cinfo.do_fancy_upsampling = FALSE;
409 jpeg_start_decompress(&data->cinfo);
410 /* Make libjpeg errors report that we've got some data */
411 data->jerr_errno = EPIPE;
412 if (h_samp == 1) {
413 result = decode_libjpeg_h_samp1(data, dest, udest,
414 vdest, v_samp);
415 } else {
416 result = decode_libjpeg_h_samp2(data, dest, udest,
417 vdest, v_samp);
418 }
419 if (result)
420 jpeg_abort_decompress(&data->cinfo);
421 else
422 jpeg_finish_decompress(&data->cinfo);
423 }
424
425 return result;
426 }
427
428 #endif // HAVE_JPEG