4 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5 * Copyright 2009-2011 D. R. Commander
7 * Based on the x86 SIMD extension for IJG JPEG library,
8 * Copyright (C) 1999-2006, MIYASAKA Masaru.
9 * For conditions of distribution and use, see copyright notice in jsimdext.inc
11 * This file contains the interface between the "normal" portions
12 * of the library and the SIMD implementations when running on
15 * Based on the stubs from 'jsimd_none.c'
18 #define JPEG_INTERNALS
19 #include "../jinclude.h"
20 #include "../jpeglib.h"
23 #include "../jsimddct.h"
30 static unsigned int simd_support = ~0;
32 #if defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)
34 #define SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT (1024 * 1024)
37 check_feature (char *buffer, char *feature)
42 if (strncmp(buffer, "Features", 8) != 0)
45 while (isspace(*buffer))
48 /* Check if 'feature' is present in the buffer as a separate word */
49 while ((p = strstr(buffer, feature))) {
50 if (p > buffer && !isspace(*(p - 1))) {
55 if (*p != 0 && !isspace(*p)) {
65 parse_proc_cpuinfo (int bufsize)
67 char *buffer = (char *)malloc(bufsize);
74 fd = fopen("/proc/cpuinfo", "r");
76 while (fgets(buffer, bufsize, fd)) {
77 if (!strchr(buffer, '\n') && !feof(fd)) {
78 /* "impossible" happened - insufficient size of the buffer! */
83 if (check_feature(buffer, "neon"))
84 simd_support |= JSIMD_ARM_NEON;
95 * Check what SIMD accelerations are supported.
97 * FIXME: This code is racy under a multi-threaded environment.
103 #if !defined(__ARM_NEON__) && defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)
104 int bufsize = 1024; /* an initial guess for the line buffer size limit */
107 if (simd_support != ~0U)
112 #if defined(__ARM_NEON__)
113 simd_support |= JSIMD_ARM_NEON;
114 #elif defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)
115 /* We still have a chance to use NEON regardless of globally used
116 * -mcpu/-mfpu options passed to gcc by performing runtime detection via
117 * /proc/cpuinfo parsing on linux/android */
118 while (!parse_proc_cpuinfo(bufsize)) {
120 if (bufsize > SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT)
125 /* Force different settings through environment variables */
126 env = getenv("JSIMD_FORCE_ARM_NEON");
127 if ((env != NULL) && (strcmp(env, "1") == 0))
128 simd_support &= JSIMD_ARM_NEON;
129 env = getenv("JSIMD_FORCE_NO_SIMD");
130 if ((env != NULL) && (strcmp(env, "1") == 0))
135 jsimd_can_rgb_ycc (void)
139 /* The code is optimised for these values only */
140 if (BITS_IN_JSAMPLE != 8)
142 if (sizeof(JDIMENSION) != 4)
144 if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
147 if (simd_support & JSIMD_ARM_NEON)
154 jsimd_can_rgb_gray (void)
162 jsimd_can_ycc_rgb (void)
166 /* The code is optimised for these values only */
167 if (BITS_IN_JSAMPLE != 8)
169 if (sizeof(JDIMENSION) != 4)
171 if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
173 if (simd_support & JSIMD_ARM_NEON)
180 jsimd_rgb_ycc_convert (j_compress_ptr cinfo,
181 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
182 JDIMENSION output_row, int num_rows)
184 void (*neonfct)(JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
186 switch(cinfo->in_color_space)
189 neonfct=jsimd_extrgb_ycc_convert_neon;
193 neonfct=jsimd_extrgbx_ycc_convert_neon;
196 neonfct=jsimd_extbgr_ycc_convert_neon;
200 neonfct=jsimd_extbgrx_ycc_convert_neon;
204 neonfct=jsimd_extxbgr_ycc_convert_neon;
208 neonfct=jsimd_extxrgb_ycc_convert_neon;
211 neonfct=jsimd_extrgb_ycc_convert_neon;
215 if (simd_support & JSIMD_ARM_NEON)
216 neonfct(cinfo->image_width, input_buf,
217 output_buf, output_row, num_rows);
221 jsimd_rgb_gray_convert (j_compress_ptr cinfo,
222 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
223 JDIMENSION output_row, int num_rows)
228 jsimd_ycc_rgb_convert (j_decompress_ptr cinfo,
229 JSAMPIMAGE input_buf, JDIMENSION input_row,
230 JSAMPARRAY output_buf, int num_rows)
232 void (*neonfct)(JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int);
234 switch(cinfo->out_color_space)
237 neonfct=jsimd_ycc_extrgb_convert_neon;
241 neonfct=jsimd_ycc_extrgbx_convert_neon;
244 neonfct=jsimd_ycc_extbgr_convert_neon;
248 neonfct=jsimd_ycc_extbgrx_convert_neon;
252 neonfct=jsimd_ycc_extxbgr_convert_neon;
256 neonfct=jsimd_ycc_extxrgb_convert_neon;
259 neonfct=jsimd_ycc_extrgb_convert_neon;
263 if (simd_support & JSIMD_ARM_NEON)
264 neonfct(cinfo->output_width, input_buf,
265 input_row, output_buf, num_rows);
269 jsimd_can_h2v2_downsample (void)
277 jsimd_can_h2v1_downsample (void)
285 jsimd_h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
286 JSAMPARRAY input_data, JSAMPARRAY output_data)
291 jsimd_h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
292 JSAMPARRAY input_data, JSAMPARRAY output_data)
297 jsimd_can_h2v2_upsample (void)
305 jsimd_can_h2v1_upsample (void)
313 jsimd_h2v2_upsample (j_decompress_ptr cinfo,
314 jpeg_component_info * compptr,
315 JSAMPARRAY input_data,
316 JSAMPARRAY * output_data_ptr)
321 jsimd_h2v1_upsample (j_decompress_ptr cinfo,
322 jpeg_component_info * compptr,
323 JSAMPARRAY input_data,
324 JSAMPARRAY * output_data_ptr)
329 jsimd_can_h2v2_fancy_upsample (void)
337 jsimd_can_h2v1_fancy_upsample (void)
341 /* The code is optimised for these values only */
342 if (BITS_IN_JSAMPLE != 8)
344 if (sizeof(JDIMENSION) != 4)
347 if (simd_support & JSIMD_ARM_NEON)
354 jsimd_h2v2_fancy_upsample (j_decompress_ptr cinfo,
355 jpeg_component_info * compptr,
356 JSAMPARRAY input_data,
357 JSAMPARRAY * output_data_ptr)
362 jsimd_h2v1_fancy_upsample (j_decompress_ptr cinfo,
363 jpeg_component_info * compptr,
364 JSAMPARRAY input_data,
365 JSAMPARRAY * output_data_ptr)
367 if (simd_support & JSIMD_ARM_NEON)
368 jsimd_h2v1_fancy_upsample_neon(cinfo->max_v_samp_factor,
369 compptr->downsampled_width, input_data, output_data_ptr);
373 jsimd_can_h2v2_merged_upsample (void)
381 jsimd_can_h2v1_merged_upsample (void)
389 jsimd_h2v2_merged_upsample (j_decompress_ptr cinfo,
390 JSAMPIMAGE input_buf,
391 JDIMENSION in_row_group_ctr,
392 JSAMPARRAY output_buf)
397 jsimd_h2v1_merged_upsample (j_decompress_ptr cinfo,
398 JSAMPIMAGE input_buf,
399 JDIMENSION in_row_group_ctr,
400 JSAMPARRAY output_buf)
405 jsimd_can_convsamp (void)
409 /* The code is optimised for these values only */
412 if (BITS_IN_JSAMPLE != 8)
414 if (sizeof(JDIMENSION) != 4)
416 if (sizeof(DCTELEM) != 2)
419 if (simd_support & JSIMD_ARM_NEON)
426 jsimd_can_convsamp_float (void)
434 jsimd_convsamp (JSAMPARRAY sample_data, JDIMENSION start_col,
437 if (simd_support & JSIMD_ARM_NEON)
438 jsimd_convsamp_neon(sample_data, start_col, workspace);
442 jsimd_convsamp_float (JSAMPARRAY sample_data, JDIMENSION start_col,
443 FAST_FLOAT * workspace)
448 jsimd_can_fdct_islow (void)
456 jsimd_can_fdct_ifast (void)
460 /* The code is optimised for these values only */
463 if (sizeof(DCTELEM) != 2)
466 if (simd_support & JSIMD_ARM_NEON)
473 jsimd_can_fdct_float (void)
481 jsimd_fdct_islow (DCTELEM * data)
486 jsimd_fdct_ifast (DCTELEM * data)
488 if (simd_support & JSIMD_ARM_NEON)
489 jsimd_fdct_ifast_neon(data);
493 jsimd_fdct_float (FAST_FLOAT * data)
498 jsimd_can_quantize (void)
502 /* The code is optimised for these values only */
505 if (sizeof(JCOEF) != 2)
507 if (sizeof(DCTELEM) != 2)
510 if (simd_support & JSIMD_ARM_NEON)
517 jsimd_can_quantize_float (void)
525 jsimd_quantize (JCOEFPTR coef_block, DCTELEM * divisors,
528 if (simd_support & JSIMD_ARM_NEON)
529 jsimd_quantize_neon(coef_block, divisors, workspace);
533 jsimd_quantize_float (JCOEFPTR coef_block, FAST_FLOAT * divisors,
534 FAST_FLOAT * workspace)
539 jsimd_can_idct_2x2 (void)
543 /* The code is optimised for these values only */
546 if (sizeof(JCOEF) != 2)
548 if (BITS_IN_JSAMPLE != 8)
550 if (sizeof(JDIMENSION) != 4)
552 if (sizeof(ISLOW_MULT_TYPE) != 2)
555 if ((simd_support & JSIMD_ARM_NEON))
562 jsimd_can_idct_4x4 (void)
566 /* The code is optimised for these values only */
569 if (sizeof(JCOEF) != 2)
571 if (BITS_IN_JSAMPLE != 8)
573 if (sizeof(JDIMENSION) != 4)
575 if (sizeof(ISLOW_MULT_TYPE) != 2)
578 if ((simd_support & JSIMD_ARM_NEON))
585 jsimd_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
586 JCOEFPTR coef_block, JSAMPARRAY output_buf,
587 JDIMENSION output_col)
589 if ((simd_support & JSIMD_ARM_NEON))
590 jsimd_idct_2x2_neon(compptr->dct_table, coef_block, output_buf, output_col);
594 jsimd_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
595 JCOEFPTR coef_block, JSAMPARRAY output_buf,
596 JDIMENSION output_col)
598 if ((simd_support & JSIMD_ARM_NEON))
599 jsimd_idct_4x4_neon(compptr->dct_table, coef_block, output_buf, output_col);
603 jsimd_can_idct_islow (void)
607 /* The code is optimised for these values only */
610 if (sizeof(JCOEF) != 2)
612 if (BITS_IN_JSAMPLE != 8)
614 if (sizeof(JDIMENSION) != 4)
616 if (sizeof(ISLOW_MULT_TYPE) != 2)
619 if (simd_support & JSIMD_ARM_NEON)
626 jsimd_can_idct_ifast (void)
630 /* The code is optimised for these values only */
633 if (sizeof(JCOEF) != 2)
635 if (BITS_IN_JSAMPLE != 8)
637 if (sizeof(JDIMENSION) != 4)
639 if (sizeof(IFAST_MULT_TYPE) != 2)
641 if (IFAST_SCALE_BITS != 2)
644 if ((simd_support & JSIMD_ARM_NEON))
651 jsimd_can_idct_float (void)
659 jsimd_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
660 JCOEFPTR coef_block, JSAMPARRAY output_buf,
661 JDIMENSION output_col)
663 if ((simd_support & JSIMD_ARM_NEON))
664 jsimd_idct_islow_neon(compptr->dct_table, coef_block, output_buf, output_col);
668 jsimd_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
669 JCOEFPTR coef_block, JSAMPARRAY output_buf,
670 JDIMENSION output_col)
672 if ((simd_support & JSIMD_ARM_NEON))
673 jsimd_idct_ifast_neon(compptr->dct_table, coef_block, output_buf, output_col);
677 jsimd_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
678 JCOEFPTR coef_block, JSAMPARRAY output_buf,
679 JDIMENSION output_col)