Bump to 2.0.6
[platform/upstream/libjpeg-turbo.git] / simd / arm / jsimd.c
1 /*
2  * jsimd_arm.c
3  *
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, 2018, D. R. Commander.
7  * Copyright (C) 2015-2016, 2018, Matthieu Darbois.
8  * Copyright (C) 2019, Google LLC.
9  *
10  * Based on the x86 SIMD extension for IJG JPEG library,
11  * Copyright (C) 1999-2006, MIYASAKA Masaru.
12  * For conditions of distribution and use, see copyright notice in jsimdext.inc
13  *
14  * This file contains the interface between the "normal" portions
15  * of the library and the SIMD implementations when running on a
16  * 32-bit Arm architecture.
17  */
18
19 #define JPEG_INTERNALS
20 #include "../../jinclude.h"
21 #include "../../jpeglib.h"
22 #include "../../jsimd.h"
23 #include "../../jdct.h"
24 #include "../../jsimddct.h"
25 #include "../jsimd.h"
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #if _USE_PRODUCT_TV
32 //Changes for JPEG GAMMA enhancement in thumbnail
33 #include <unistd.h>
34 #endif
35
36 static unsigned int simd_support = ~0;
37 static unsigned int simd_huffman = 1;
38
39 #if !defined(__ARM_NEON__) && (defined(__linux__) || defined(ANDROID) || defined(__ANDROID__))
40
41 #define SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT  (1024 * 1024)
42
43 LOCAL(int)
44 check_feature(char *buffer, char *feature)
45 {
46   char *p;
47
48   if (*feature == 0)
49     return 0;
50   if (strncmp(buffer, "Features", 8) != 0)
51     return 0;
52   buffer += 8;
53   while (isspace(*buffer))
54     buffer++;
55
56   /* Check if 'feature' is present in the buffer as a separate word */
57   while ((p = strstr(buffer, feature))) {
58     if (p > buffer && !isspace(*(p - 1))) {
59       buffer++;
60       continue;
61     }
62     p += strlen(feature);
63     if (*p != 0 && !isspace(*p)) {
64       buffer++;
65       continue;
66     }
67     return 1;
68   }
69   return 0;
70 }
71
72 LOCAL(int)
73 parse_proc_cpuinfo(int bufsize)
74 {
75   char *buffer = (char *)malloc(bufsize);
76   FILE *fd;
77
78   simd_support = 0;
79
80   if (!buffer)
81     return 0;
82
83   fd = fopen("/proc/cpuinfo", "r");
84   if (fd) {
85     while (fgets(buffer, bufsize, fd)) {
86       if (!strchr(buffer, '\n') && !feof(fd)) {
87         /* "impossible" happened - insufficient size of the buffer! */
88         fclose(fd);
89         free(buffer);
90         return 0;
91       }
92       if (check_feature(buffer, "neon"))
93         simd_support |= JSIMD_NEON;
94     }
95     fclose(fd);
96   }
97   free(buffer);
98   return 1;
99 }
100
101 #endif
102
103 /*
104  * Check what SIMD accelerations are supported.
105  *
106  * FIXME: This code is racy under a multi-threaded environment.
107  */
108 LOCAL(void)
109 init_simd(void)
110 {
111 #ifndef NO_GETENV
112   char *env = NULL;
113 #endif
114 #if !defined(__ARM_NEON__) && (defined(__linux__) || defined(ANDROID) || defined(__ANDROID__))
115   int bufsize = 1024; /* an initial guess for the line buffer size limit */
116 #endif
117
118   if (simd_support != ~0U)
119     return;
120
121   simd_support = 0;
122
123 #if defined(__ARM_NEON__)
124   simd_support |= JSIMD_NEON;
125 #elif defined(__linux__) || defined(ANDROID) || defined(__ANDROID__)
126   /* We still have a chance to use Neon regardless of globally used
127    * -mcpu/-mfpu options passed to gcc by performing runtime detection via
128    * /proc/cpuinfo parsing on linux/android */
129   while (!parse_proc_cpuinfo(bufsize)) {
130     bufsize *= 2;
131     if (bufsize > SOMEWHAT_SANE_PROC_CPUINFO_SIZE_LIMIT)
132       break;
133   }
134 #endif
135
136 #ifndef NO_GETENV
137   /* Force different settings through environment variables */
138   env = getenv("JSIMD_FORCENEON");
139   if ((env != NULL) && (strcmp(env, "1") == 0))
140     simd_support = JSIMD_NEON;
141   env = getenv("JSIMD_FORCENONE");
142   if ((env != NULL) && (strcmp(env, "1") == 0))
143     simd_support = 0;
144   env = getenv("JSIMD_NOHUFFENC");
145   if ((env != NULL) && (strcmp(env, "1") == 0))
146     simd_huffman = 0;
147 #endif
148 }
149
150 GLOBAL(int)
151 jsimd_can_rgb_ycc(void)
152 {
153   init_simd();
154
155   /* The code is optimised for these values only */
156   if (BITS_IN_JSAMPLE != 8)
157     return 0;
158   if (sizeof(JDIMENSION) != 4)
159     return 0;
160   if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
161     return 0;
162
163   if (simd_support & JSIMD_NEON)
164     return 1;
165
166   return 0;
167 }
168
169 GLOBAL(int)
170 jsimd_can_rgb_gray(void)
171 {
172   return 0;
173 }
174
175 GLOBAL(int)
176 jsimd_can_ycc_rgb(void)
177 {
178   init_simd();
179
180   /* The code is optimised for these values only */
181   if (BITS_IN_JSAMPLE != 8)
182     return 0;
183   if (sizeof(JDIMENSION) != 4)
184     return 0;
185   if ((RGB_PIXELSIZE != 3) && (RGB_PIXELSIZE != 4))
186     return 0;
187
188   if (simd_support & JSIMD_NEON)
189     return 1;
190
191   return 0;
192 }
193
194 GLOBAL(int)
195 jsimd_can_ycc_rgb565(void)
196 {
197   init_simd();
198
199   /* The code is optimised for these values only */
200   if (BITS_IN_JSAMPLE != 8)
201     return 0;
202   if (sizeof(JDIMENSION) != 4)
203     return 0;
204
205   if (simd_support & JSIMD_NEON)
206     return 1;
207
208   return 0;
209 }
210
211 GLOBAL(void)
212 jsimd_rgb_ycc_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
213                       JSAMPIMAGE output_buf, JDIMENSION output_row,
214                       int num_rows)
215 {
216   void (*neonfct) (JDIMENSION, JSAMPARRAY, JSAMPIMAGE, JDIMENSION, int);
217
218   switch (cinfo->in_color_space) {
219   case JCS_EXT_RGB:
220     neonfct = jsimd_extrgb_ycc_convert_neon;
221     break;
222   case JCS_EXT_RGBX:
223   case JCS_EXT_RGBA:
224     neonfct = jsimd_extrgbx_ycc_convert_neon;
225     break;
226   case JCS_EXT_BGR:
227     neonfct = jsimd_extbgr_ycc_convert_neon;
228     break;
229   case JCS_EXT_BGRX:
230   case JCS_EXT_BGRA:
231     neonfct = jsimd_extbgrx_ycc_convert_neon;
232     break;
233   case JCS_EXT_XBGR:
234   case JCS_EXT_ABGR:
235     neonfct = jsimd_extxbgr_ycc_convert_neon;
236     break;
237   case JCS_EXT_XRGB:
238   case JCS_EXT_ARGB:
239     neonfct = jsimd_extxrgb_ycc_convert_neon;
240     break;
241   default:
242     neonfct = jsimd_extrgb_ycc_convert_neon;
243     break;
244   }
245
246   neonfct(cinfo->image_width, input_buf, output_buf, output_row, num_rows);
247 }
248
249 GLOBAL(void)
250 jsimd_rgb_gray_convert(j_compress_ptr cinfo, JSAMPARRAY input_buf,
251                        JSAMPIMAGE output_buf, JDIMENSION output_row,
252                        int num_rows)
253 {
254 }
255
256 GLOBAL(void)
257 jsimd_ycc_rgb_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
258                       JDIMENSION input_row, JSAMPARRAY output_buf,
259                       int num_rows)
260 {
261   void (*neonfct) (JDIMENSION, JSAMPIMAGE, JDIMENSION, JSAMPARRAY, int);
262
263   switch (cinfo->out_color_space) {
264   case JCS_EXT_RGB:
265     neonfct = jsimd_ycc_extrgb_convert_neon;
266     break;
267   case JCS_EXT_RGBX:
268   case JCS_EXT_RGBA:
269     neonfct = jsimd_ycc_extrgbx_convert_neon;
270     break;
271   case JCS_EXT_BGR:
272     neonfct = jsimd_ycc_extbgr_convert_neon;
273     break;
274   case JCS_EXT_BGRX:
275   case JCS_EXT_BGRA:
276     neonfct = jsimd_ycc_extbgrx_convert_neon;
277     break;
278   case JCS_EXT_XBGR:
279   case JCS_EXT_ABGR:
280     neonfct = jsimd_ycc_extxbgr_convert_neon;
281     break;
282   case JCS_EXT_XRGB:
283   case JCS_EXT_ARGB:
284     neonfct = jsimd_ycc_extxrgb_convert_neon;
285     break;
286   default:
287     neonfct = jsimd_ycc_extrgb_convert_neon;
288     break;
289   }
290
291 #if _USE_PRODUCT_TV
292   if (simd_support & JSIMD_NEON) {
293     neonfct(cinfo->output_width, input_buf, input_row, output_buf, num_rows);
294     PickColor* pickColor = cinfo->pick_color_data;
295     if(pickColor && pickColor->enablePickColor && output_buf) {
296       int w = cinfo->output_width;
297       unsigned char *ptr = *output_buf;
298       if(pickColor->perc <= 0) {
299         w = pickColor->x2 - pickColor->x1 + 1;
300         ptr = (*output_buf) + (pickColor->x1 * 3);
301       }
302       jsimd_pick_color(ptr, pickColor, w);
303     }
304   }
305 #else
306   neonfct(cinfo->output_width, input_buf, input_row, output_buf, num_rows);
307 #endif
308 }
309
310 GLOBAL(void)
311 jsimd_ycc_rgb565_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
312                          JDIMENSION input_row, JSAMPARRAY output_buf,
313                          int num_rows)
314 {
315   jsimd_ycc_rgb565_convert_neon(cinfo->output_width, input_buf, input_row,
316                                 output_buf, num_rows);
317 }
318
319 GLOBAL(int)
320 jsimd_can_h2v2_downsample(void)
321 {
322   return 0;
323 }
324
325 GLOBAL(int)
326 jsimd_can_h2v1_downsample(void)
327 {
328   return 0;
329 }
330
331 GLOBAL(void)
332 jsimd_h2v2_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
333                       JSAMPARRAY input_data, JSAMPARRAY output_data)
334 {
335 }
336
337 GLOBAL(void)
338 jsimd_h2v1_downsample(j_compress_ptr cinfo, jpeg_component_info *compptr,
339                       JSAMPARRAY input_data, JSAMPARRAY output_data)
340 {
341 }
342
343 GLOBAL(int)
344 jsimd_can_h2v2_upsample(void)
345 {
346   return 0;
347 }
348
349 GLOBAL(int)
350 jsimd_can_h2v1_upsample(void)
351 {
352   return 0;
353 }
354
355 GLOBAL(void)
356 jsimd_h2v2_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
357                     JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
358 {
359 }
360
361 GLOBAL(void)
362 jsimd_h2v1_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
363                     JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
364 {
365 }
366
367 GLOBAL(int)
368 jsimd_can_h2v2_fancy_upsample(void)
369 {
370   return 0;
371 }
372
373 GLOBAL(int)
374 jsimd_can_h2v1_fancy_upsample(void)
375 {
376   init_simd();
377
378   /* The code is optimised for these values only */
379   if (BITS_IN_JSAMPLE != 8)
380     return 0;
381   if (sizeof(JDIMENSION) != 4)
382     return 0;
383
384   if (simd_support & JSIMD_NEON)
385     return 1;
386
387   return 0;
388 }
389
390 GLOBAL(void)
391 jsimd_h2v2_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
392                           JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
393 {
394 }
395
396 GLOBAL(void)
397 jsimd_h2v1_fancy_upsample(j_decompress_ptr cinfo, jpeg_component_info *compptr,
398                           JSAMPARRAY input_data, JSAMPARRAY *output_data_ptr)
399 {
400   jsimd_h2v1_fancy_upsample_neon(cinfo->max_v_samp_factor,
401                                  compptr->downsampled_width, input_data,
402                                  output_data_ptr);
403 }
404
405 GLOBAL(int)
406 jsimd_can_h2v2_merged_upsample(void)
407 {
408   return 0;
409 }
410
411 GLOBAL(int)
412 jsimd_can_h2v1_merged_upsample(void)
413 {
414   return 0;
415 }
416
417 GLOBAL(void)
418 jsimd_h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
419                            JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
420 {
421 }
422
423 GLOBAL(void)
424 jsimd_h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
425                            JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
426 {
427 }
428
429 GLOBAL(int)
430 jsimd_can_convsamp(void)
431 {
432   init_simd();
433
434   /* The code is optimised for these values only */
435   if (DCTSIZE != 8)
436     return 0;
437   if (BITS_IN_JSAMPLE != 8)
438     return 0;
439   if (sizeof(JDIMENSION) != 4)
440     return 0;
441   if (sizeof(DCTELEM) != 2)
442     return 0;
443
444   if (simd_support & JSIMD_NEON)
445     return 1;
446
447   return 0;
448 }
449
450 GLOBAL(int)
451 jsimd_can_convsamp_float(void)
452 {
453   return 0;
454 }
455
456 GLOBAL(void)
457 jsimd_convsamp(JSAMPARRAY sample_data, JDIMENSION start_col,
458                DCTELEM *workspace)
459 {
460   jsimd_convsamp_neon(sample_data, start_col, workspace);
461 }
462
463 GLOBAL(void)
464 jsimd_convsamp_float(JSAMPARRAY sample_data, JDIMENSION start_col,
465                      FAST_FLOAT *workspace)
466 {
467 }
468
469 GLOBAL(int)
470 jsimd_can_fdct_islow(void)
471 {
472   return 0;
473 }
474
475 GLOBAL(int)
476 jsimd_can_fdct_ifast(void)
477 {
478   init_simd();
479
480   /* The code is optimised for these values only */
481   if (DCTSIZE != 8)
482     return 0;
483   if (sizeof(DCTELEM) != 2)
484     return 0;
485
486   if (simd_support & JSIMD_NEON)
487     return 1;
488
489   return 0;
490 }
491
492 GLOBAL(int)
493 jsimd_can_fdct_float(void)
494 {
495   return 0;
496 }
497
498 GLOBAL(void)
499 jsimd_fdct_islow(DCTELEM *data)
500 {
501 }
502
503 GLOBAL(void)
504 jsimd_fdct_ifast(DCTELEM *data)
505 {
506   jsimd_fdct_ifast_neon(data);
507 }
508
509 GLOBAL(void)
510 jsimd_fdct_float(FAST_FLOAT *data)
511 {
512 }
513
514 GLOBAL(int)
515 jsimd_can_quantize(void)
516 {
517   init_simd();
518
519   /* The code is optimised for these values only */
520   if (DCTSIZE != 8)
521     return 0;
522   if (sizeof(JCOEF) != 2)
523     return 0;
524   if (sizeof(DCTELEM) != 2)
525     return 0;
526
527   if (simd_support & JSIMD_NEON)
528     return 1;
529
530   return 0;
531 }
532
533 GLOBAL(int)
534 jsimd_can_quantize_float(void)
535 {
536   return 0;
537 }
538
539 GLOBAL(void)
540 jsimd_quantize(JCOEFPTR coef_block, DCTELEM *divisors, DCTELEM *workspace)
541 {
542   jsimd_quantize_neon(coef_block, divisors, workspace);
543 }
544
545 GLOBAL(void)
546 jsimd_quantize_float(JCOEFPTR coef_block, FAST_FLOAT *divisors,
547                      FAST_FLOAT *workspace)
548 {
549 }
550
551 GLOBAL(int)
552 jsimd_can_idct_2x2(void)
553 {
554   init_simd();
555
556   /* The code is optimised for these values only */
557   if (DCTSIZE != 8)
558     return 0;
559   if (sizeof(JCOEF) != 2)
560     return 0;
561   if (BITS_IN_JSAMPLE != 8)
562     return 0;
563   if (sizeof(JDIMENSION) != 4)
564     return 0;
565   if (sizeof(ISLOW_MULT_TYPE) != 2)
566     return 0;
567
568   if (simd_support & JSIMD_NEON)
569     return 1;
570
571   return 0;
572 }
573
574 GLOBAL(int)
575 jsimd_can_idct_4x4(void)
576 {
577   init_simd();
578
579   /* The code is optimised for these values only */
580   if (DCTSIZE != 8)
581     return 0;
582   if (sizeof(JCOEF) != 2)
583     return 0;
584   if (BITS_IN_JSAMPLE != 8)
585     return 0;
586   if (sizeof(JDIMENSION) != 4)
587     return 0;
588   if (sizeof(ISLOW_MULT_TYPE) != 2)
589     return 0;
590
591   if (simd_support & JSIMD_NEON)
592     return 1;
593
594   return 0;
595 }
596
597 GLOBAL(void)
598 jsimd_idct_2x2(j_decompress_ptr cinfo, jpeg_component_info *compptr,
599                JCOEFPTR coef_block, JSAMPARRAY output_buf,
600                JDIMENSION output_col)
601 {
602   jsimd_idct_2x2_neon(compptr->dct_table, coef_block, output_buf, output_col);
603 }
604
605 GLOBAL(void)
606 jsimd_idct_4x4(j_decompress_ptr cinfo, jpeg_component_info *compptr,
607                JCOEFPTR coef_block, JSAMPARRAY output_buf,
608                JDIMENSION output_col)
609 {
610   jsimd_idct_4x4_neon(compptr->dct_table, coef_block, output_buf, output_col);
611 }
612
613 GLOBAL(int)
614 jsimd_can_idct_islow(void)
615 {
616   init_simd();
617
618   /* The code is optimised for these values only */
619   if (DCTSIZE != 8)
620     return 0;
621   if (sizeof(JCOEF) != 2)
622     return 0;
623   if (BITS_IN_JSAMPLE != 8)
624     return 0;
625   if (sizeof(JDIMENSION) != 4)
626     return 0;
627   if (sizeof(ISLOW_MULT_TYPE) != 2)
628     return 0;
629
630   if (simd_support & JSIMD_NEON)
631     return 1;
632
633   return 0;
634 }
635
636 GLOBAL(int)
637 jsimd_can_idct_ifast(void)
638 {
639   init_simd();
640
641   /* The code is optimised for these values only */
642   if (DCTSIZE != 8)
643     return 0;
644   if (sizeof(JCOEF) != 2)
645     return 0;
646   if (BITS_IN_JSAMPLE != 8)
647     return 0;
648   if (sizeof(JDIMENSION) != 4)
649     return 0;
650   if (sizeof(IFAST_MULT_TYPE) != 2)
651     return 0;
652   if (IFAST_SCALE_BITS != 2)
653     return 0;
654
655   if (simd_support & JSIMD_NEON)
656     return 1;
657
658   return 0;
659 }
660
661 GLOBAL(int)
662 jsimd_can_idct_float(void)
663 {
664   return 0;
665 }
666
667 GLOBAL(void)
668 jsimd_idct_islow(j_decompress_ptr cinfo, jpeg_component_info *compptr,
669                  JCOEFPTR coef_block, JSAMPARRAY output_buf,
670                  JDIMENSION output_col)
671 {
672   jsimd_idct_islow_neon(compptr->dct_table, coef_block, output_buf,
673                         output_col);
674 }
675
676 GLOBAL(void)
677 jsimd_idct_ifast(j_decompress_ptr cinfo, jpeg_component_info *compptr,
678                  JCOEFPTR coef_block, JSAMPARRAY output_buf,
679                  JDIMENSION output_col)
680 {
681   jsimd_idct_ifast_neon(compptr->dct_table, coef_block, output_buf,
682                         output_col);
683 }
684
685 GLOBAL(void)
686 jsimd_idct_float(j_decompress_ptr cinfo, jpeg_component_info *compptr,
687                  JCOEFPTR coef_block, JSAMPARRAY output_buf,
688                  JDIMENSION output_col)
689 {
690 }
691
692 GLOBAL(int)
693 jsimd_can_huff_encode_one_block(void)
694 {
695   init_simd();
696
697   if (DCTSIZE != 8)
698     return 0;
699   if (sizeof(JCOEF) != 2)
700     return 0;
701
702   if (simd_support & JSIMD_NEON && simd_huffman)
703     return 1;
704
705   return 0;
706 }
707
708 GLOBAL(JOCTET *)
709 jsimd_huff_encode_one_block(void *state, JOCTET *buffer, JCOEFPTR block,
710                             int last_dc_val, c_derived_tbl *dctbl,
711                             c_derived_tbl *actbl)
712 {
713   return jsimd_huff_encode_one_block_neon(state, buffer, block, last_dc_val,
714                                           dctbl, actbl);
715 }
716
717 GLOBAL(int)
718 jsimd_can_encode_mcu_AC_first_prepare(void)
719 {
720   return 0;
721 }
722
723 GLOBAL(void)
724 jsimd_encode_mcu_AC_first_prepare(const JCOEF *block,
725                                   const int *jpeg_natural_order_start, int Sl,
726                                   int Al, JCOEF *values, size_t *zerobits)
727 {
728 }
729
730 GLOBAL(int)
731 jsimd_can_encode_mcu_AC_refine_prepare(void)
732 {
733   return 0;
734 }
735
736 GLOBAL(int)
737 jsimd_encode_mcu_AC_refine_prepare(const JCOEF *block,
738                                    const int *jpeg_natural_order_start, int Sl,
739                                    int Al, JCOEF *absvalues, size_t *bits)
740 {
741   return 0;
742 }