4 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5 * Copyright (C) 2011, Nokia Corporation and/or its subsidiary(-ies).
6 * Copyright (C) 2009-2011, 2013-2014, 2016, D. R. Commander.
7 * Copyright (C) 2015-2016, Matthieu Darbois.
9 * Based on the x86 SIMD extension for IJG JPEG library,
10 * Copyright (C) 1999-2006, MIYASAKA Masaru.
11 * For conditions of distribution and use, see copyright notice in jsimdext.inc
13 * This file contains the interface between the "normal" portions
14 * of the library and the SIMD implementations when running on a
15 * 32-bit ARM architecture.
18 #define JPEG_INTERNALS
19 #include "../jinclude.h"
20 #include "../jpeglib.h"
23 #include "../jsimddct.h"
31 //Changes for JPEG GAMMA enhancement in thumbnail
35 static unsigned int simd_support = ~0;
36 static unsigned int simd_huffman = 1;
38 #if defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)
40 #define SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT (1024 * 1024)
43 check_feature (char *buffer, char *feature)
48 if (strncmp(buffer, "Features", 8) != 0)
51 while (isspace(*buffer))
54 /* Check if 'feature' is present in the buffer as a separate word */
55 while ((p = strstr(buffer, feature))) {
56 if (p > buffer && !isspace(*(p - 1))) {
61 if (*p != 0 && !isspace(*p)) {
71 parse_proc_cpuinfo (int bufsize)
73 char *buffer = (char *)malloc(bufsize);
80 fd = fopen("/proc/cpuinfo", "r");
82 while (fgets(buffer, bufsize, fd)) {
83 if (!strchr(buffer, '\n') && !feof(fd)) {
84 /* "impossible" happened - insufficient size of the buffer! */
89 if (check_feature(buffer, "neon"))
90 simd_support |= JSIMD_ARM_NEON;
101 * Check what SIMD accelerations are supported.
103 * FIXME: This code is racy under a multi-threaded environment.
109 #if !defined(__ARM_NEON__) && defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)
110 int bufsize = 1024; /* an initial guess for the line buffer size limit */
113 if (simd_support != ~0U)
118 #if defined(__ARM_NEON__)
119 simd_support |= JSIMD_ARM_NEON;
120 #elif defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)
121 /* We still have a chance to use NEON regardless of globally used
122 * -mcpu/-mfpu options passed to gcc by performing runtime detection via
123 * /proc/cpuinfo parsing on linux/android */
124 while (!parse_proc_cpuinfo(bufsize)) {
126 if (bufsize > SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT)
131 /* Force different settings through environment variables */
132 env = getenv("JSIMD_FORCENEON");
133 if ((env != NULL) && (strcmp(env, "1") == 0))
134 simd_support = JSIMD_ARM_NEON;
135 env = getenv("JSIMD_FORCENONE");
136 if ((env != NULL) && (strcmp(env, "1") == 0))
138 env = getenv("JSIMD_NOHUFFENC");
139 if ((env != NULL) && (strcmp(env, "1") == 0))
144 jsimd_can_rgb_ycc (void)
148 /* The code is optimised for these values only */
149 if (BITS_IN_JSAMPLE != 8)
151 if (sizeof(JDIMENSION) != 4)
153 if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
156 if (simd_support & JSIMD_ARM_NEON)
163 jsimd_can_rgb_gray (void)
171 jsimd_can_ycc_rgb (void)
175 /* The code is optimised for these values only */
176 if (BITS_IN_JSAMPLE != 8)
178 if (sizeof(JDIMENSION) != 4)
180 if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
183 if (simd_support & JSIMD_ARM_NEON)
190 jsimd_can_ycc_rgb565 (void)
194 /* The code is optimised for these values only */
195 if (BITS_IN_JSAMPLE != 8)
197 if (sizeof(JDIMENSION) != 4)
200 if (simd_support & JSIMD_ARM_NEON)
207 jsimd_rgb_ycc_convert (j_compress_ptr cinfo,
208 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
209 JDIMENSION output_row, int num_rows)
211 void (*neonfct)(JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
213 switch(cinfo->in_color_space) {
215 neonfct=jsimd_extrgb_ycc_convert_neon;
219 neonfct=jsimd_extrgbx_ycc_convert_neon;
222 neonfct=jsimd_extbgr_ycc_convert_neon;
226 neonfct=jsimd_extbgrx_ycc_convert_neon;
230 neonfct=jsimd_extxbgr_ycc_convert_neon;
234 neonfct=jsimd_extxrgb_ycc_convert_neon;
237 neonfct=jsimd_extrgb_ycc_convert_neon;
241 neonfct(cinfo->image_width, input_buf, output_buf, output_row, num_rows);
245 jsimd_rgb_gray_convert (j_compress_ptr cinfo,
246 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
247 JDIMENSION output_row, int num_rows)
252 jsimd_ycc_rgb_convert (j_decompress_ptr cinfo,
253 JSAMPIMAGE input_buf, JDIMENSION input_row,
254 JSAMPARRAY output_buf, int num_rows)
256 void (*neonfct)(JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int);
258 switch(cinfo->out_color_space) {
260 neonfct=jsimd_ycc_extrgb_convert_neon;
264 neonfct=jsimd_ycc_extrgbx_convert_neon;
267 neonfct=jsimd_ycc_extbgr_convert_neon;
271 neonfct=jsimd_ycc_extbgrx_convert_neon;
275 neonfct=jsimd_ycc_extxbgr_convert_neon;
279 neonfct=jsimd_ycc_extxrgb_convert_neon;
282 neonfct=jsimd_ycc_extrgb_convert_neon;
287 if (simd_support & JSIMD_ARM_NEON) {
288 neonfct(cinfo->output_width, input_buf, input_row, output_buf, num_rows);
289 PickColor* pickColor = cinfo->pick_color_data;
290 if(pickColor && pickColor->enablePickColor && output_buf) {
291 int w = cinfo->output_width;
292 unsigned char *ptr = *output_buf;
293 if(pickColor->perc <= 0) {
294 w = pickColor->x2 - pickColor->x1 + 1;
295 ptr = (*output_buf) + (pickColor->x1 * 3);
297 jsimd_pick_color(ptr, pickColor, w);
301 neonfct(cinfo->output_width, input_buf, input_row, output_buf, num_rows);
306 jsimd_ycc_rgb565_convert (j_decompress_ptr cinfo,
307 JSAMPIMAGE input_buf, JDIMENSION input_row,
308 JSAMPARRAY output_buf, int num_rows)
310 jsimd_ycc_rgb565_convert_neon(cinfo->output_width, input_buf, input_row,
311 output_buf, num_rows);
315 jsimd_can_h2v2_downsample (void)
323 jsimd_can_h2v1_downsample (void)
331 jsimd_h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
332 JSAMPARRAY input_data, JSAMPARRAY output_data)
337 jsimd_h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
338 JSAMPARRAY input_data, JSAMPARRAY output_data)
343 jsimd_can_h2v2_upsample (void)
351 jsimd_can_h2v1_upsample (void)
359 jsimd_h2v2_upsample (j_decompress_ptr cinfo,
360 jpeg_component_info *compptr,
361 JSAMPARRAY input_data,
362 JSAMPARRAY *output_data_ptr)
367 jsimd_h2v1_upsample (j_decompress_ptr cinfo,
368 jpeg_component_info *compptr,
369 JSAMPARRAY input_data,
370 JSAMPARRAY *output_data_ptr)
375 jsimd_can_h2v2_fancy_upsample (void)
383 jsimd_can_h2v1_fancy_upsample (void)
387 /* The code is optimised for these values only */
388 if (BITS_IN_JSAMPLE != 8)
390 if (sizeof(JDIMENSION) != 4)
393 if (simd_support & JSIMD_ARM_NEON)
400 jsimd_h2v2_fancy_upsample (j_decompress_ptr cinfo,
401 jpeg_component_info *compptr,
402 JSAMPARRAY input_data,
403 JSAMPARRAY *output_data_ptr)
408 jsimd_h2v1_fancy_upsample (j_decompress_ptr cinfo,
409 jpeg_component_info *compptr,
410 JSAMPARRAY input_data,
411 JSAMPARRAY *output_data_ptr)
413 jsimd_h2v1_fancy_upsample_neon(cinfo->max_v_samp_factor,
414 compptr->downsampled_width, input_data,
419 jsimd_can_h2v2_merged_upsample (void)
427 jsimd_can_h2v1_merged_upsample (void)
435 jsimd_h2v2_merged_upsample (j_decompress_ptr cinfo,
436 JSAMPIMAGE input_buf,
437 JDIMENSION in_row_group_ctr,
438 JSAMPARRAY output_buf)
443 jsimd_h2v1_merged_upsample (j_decompress_ptr cinfo,
444 JSAMPIMAGE input_buf,
445 JDIMENSION in_row_group_ctr,
446 JSAMPARRAY output_buf)
451 jsimd_can_convsamp (void)
455 /* The code is optimised for these values only */
458 if (BITS_IN_JSAMPLE != 8)
460 if (sizeof(JDIMENSION) != 4)
462 if (sizeof(DCTELEM) != 2)
465 if (simd_support & JSIMD_ARM_NEON)
472 jsimd_can_convsamp_float (void)
480 jsimd_convsamp (JSAMPARRAY sample_data, JDIMENSION start_col,
483 jsimd_convsamp_neon(sample_data, start_col, workspace);
487 jsimd_convsamp_float (JSAMPARRAY sample_data, JDIMENSION start_col,
488 FAST_FLOAT *workspace)
493 jsimd_can_fdct_islow (void)
501 jsimd_can_fdct_ifast (void)
505 /* The code is optimised for these values only */
508 if (sizeof(DCTELEM) != 2)
511 if (simd_support & JSIMD_ARM_NEON)
518 jsimd_can_fdct_float (void)
526 jsimd_fdct_islow (DCTELEM *data)
531 jsimd_fdct_ifast (DCTELEM *data)
533 jsimd_fdct_ifast_neon(data);
537 jsimd_fdct_float (FAST_FLOAT *data)
542 jsimd_can_quantize (void)
546 /* The code is optimised for these values only */
549 if (sizeof(JCOEF) != 2)
551 if (sizeof(DCTELEM) != 2)
554 if (simd_support & JSIMD_ARM_NEON)
561 jsimd_can_quantize_float (void)
569 jsimd_quantize (JCOEFPTR coef_block, DCTELEM *divisors,
572 jsimd_quantize_neon(coef_block, divisors, workspace);
576 jsimd_quantize_float (JCOEFPTR coef_block, FAST_FLOAT *divisors,
577 FAST_FLOAT *workspace)
582 jsimd_can_idct_2x2 (void)
586 /* The code is optimised for these values only */
589 if (sizeof(JCOEF) != 2)
591 if (BITS_IN_JSAMPLE != 8)
593 if (sizeof(JDIMENSION) != 4)
595 if (sizeof(ISLOW_MULT_TYPE) != 2)
598 if (simd_support & JSIMD_ARM_NEON)
605 jsimd_can_idct_4x4 (void)
609 /* The code is optimised for these values only */
612 if (sizeof(JCOEF) != 2)
614 if (BITS_IN_JSAMPLE != 8)
616 if (sizeof(JDIMENSION) != 4)
618 if (sizeof(ISLOW_MULT_TYPE) != 2)
621 if (simd_support & JSIMD_ARM_NEON)
628 jsimd_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
629 JCOEFPTR coef_block, JSAMPARRAY output_buf,
630 JDIMENSION output_col)
632 jsimd_idct_2x2_neon(compptr->dct_table, coef_block, output_buf,
637 jsimd_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info *compptr,
638 JCOEFPTR coef_block, JSAMPARRAY output_buf,
639 JDIMENSION output_col)
641 jsimd_idct_4x4_neon(compptr->dct_table, coef_block, output_buf,
646 jsimd_can_idct_islow (void)
650 /* The code is optimised for these values only */
653 if (sizeof(JCOEF) != 2)
655 if (BITS_IN_JSAMPLE != 8)
657 if (sizeof(JDIMENSION) != 4)
659 if (sizeof(ISLOW_MULT_TYPE) != 2)
662 if (simd_support & JSIMD_ARM_NEON)
669 jsimd_can_idct_ifast (void)
673 /* The code is optimised for these values only */
676 if (sizeof(JCOEF) != 2)
678 if (BITS_IN_JSAMPLE != 8)
680 if (sizeof(JDIMENSION) != 4)
682 if (sizeof(IFAST_MULT_TYPE) != 2)
684 if (IFAST_SCALE_BITS != 2)
687 if (simd_support & JSIMD_ARM_NEON)
694 jsimd_can_idct_float (void)
702 jsimd_idct_islow (j_decompress_ptr cinfo, jpeg_component_info *compptr,
703 JCOEFPTR coef_block, JSAMPARRAY output_buf,
704 JDIMENSION output_col)
706 jsimd_idct_islow_neon(compptr->dct_table, coef_block, output_buf,
711 jsimd_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info *compptr,
712 JCOEFPTR coef_block, JSAMPARRAY output_buf,
713 JDIMENSION output_col)
715 jsimd_idct_ifast_neon(compptr->dct_table, coef_block, output_buf,
720 jsimd_idct_float (j_decompress_ptr cinfo, jpeg_component_info *compptr,
721 JCOEFPTR coef_block, JSAMPARRAY output_buf,
722 JDIMENSION output_col)
727 jsimd_can_huff_encode_one_block (void)
733 if (sizeof(JCOEF) != 2)
736 if (simd_support & JSIMD_ARM_NEON && simd_huffman)
743 jsimd_huff_encode_one_block (void *state, JOCTET *buffer, JCOEFPTR block,
744 int last_dc_val, c_derived_tbl *dctbl,
745 c_derived_tbl *actbl)
747 return jsimd_huff_encode_one_block_neon(state, buffer, block, last_dc_val,