Merge branch 'fastjpeg' into working
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parents 5b53d20, fd98752 · Browse files · 1 file·+324 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+324 added−15 removed
- other:+324 added−15 removed
Provenance
- Commit
de385bb- Tree
2ca693a- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent 1
5b53d20- Parent 2
fd98752
1 file changed+324 added−15 removed
modified:stb_image.h+324 added-15 removed
25 unmodified lines26 PNM (PPM and PGM binary only)27 28 - decode from memory or through FILE (define STBI_NO_STDIO to remove code)29 - decode from arbitrary I/O callbacks30- - SIMD acceleration on x86/x6430+ - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)31 32 Full documentation under "DOCUMENTATION" below.33 34 177 unmodified lines212 // ===========================================================================213 //214 // SIMD support215 //216-// The JPEG decoder will automatically use SIMD kernels on x86 platforms217-// where supported.216+// The JPEG decoder will try to automatically use SIMD kernels on when217+// supported by the compiler.218 //219 // (The old do-it-yourself SIMD API is no longer supported in the current220 // code.)221 //222-// The code will automatically detect if the required SIMD instructions are223-// available, and fall back to the generic C version where they're not.222+// On x86, SSE2 will automatically be used when available; if not, the223+// generic C versions are used as a fall-back. On ARM targets, the typical224+// path is to have separate builds for NEON and non-NEON devices (at least225+// this is true for iOS and Android). Therefore, the NEON support is226+// toggled by a build flag: define STBI_NEON to get NEON loops.227 //228 // The output of the JPEG decoder is slightly different from versions where229 // SIMD support was introduced (that is, for versions before 1.49). The230 // difference is only +-1 in the 8-bit RGB channels, and only on a small315 unmodified lines546 }547 #endif548 #endif549 550+// ARM NEON551+#if defined(STBI_NO_SIMD) && defined(STBI_NEON)552+#undef STBI_NEON553+#endif554+555+#ifdef STBI_NEON556+#include <arm_neon.h>557+#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))558+#endif559+560 #ifndef STBI_SIMD_ALIGN561 #define STBI_SIMD_ALIGN(type, name) type name562 #endif563 1338 unmodified lines1902 }1903 1904 #endif // STBI_SSE21905 1906+#ifdef STBI_NEON1907+1908+// NEON integer IDCT. should produce bit-identical1909+// results to the generic C version.1910+static void stbi__idct_neon(stbi_uc *out, int out_stride, short data[64])1911+{1912+ int16x8_t row0, row1, row2, row3, row4, row5, row6, row7;1913+1914+ int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f));1915+ int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f));1916+ int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f));1917+ int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f));1918+ int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f));1919+ int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f));1920+ int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f));1921+ int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f));1922+ int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f));1923+ int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f));1924+ int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f));1925+ int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f));1926+1927+#define dct_long_mul(out, inq, coeff) \1928+ int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \1929+ int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff)1930+1931+#define dct_long_mac(out, acc, inq, coeff) \1932+ int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \1933+ int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff)1934+1935+#define dct_widen(out, inq) \1936+ int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \1937+ int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12)1938+1939+// wide add1940+#define dct_wadd(out, a, b) \1941+ int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \1942+ int32x4_t out##_h = vaddq_s32(a##_h, b##_h)1943+1944+// wide sub1945+#define dct_wsub(out, a, b) \1946+ int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \1947+ int32x4_t out##_h = vsubq_s32(a##_h, b##_h)1948+1949+// butterfly a/b, then shift using "shiftop" by "s" and pack1950+#define dct_bfly32o(out0,out1, a,b,shiftop,s) \1951+ { \1952+ dct_wadd(sum, a, b); \1953+ dct_wsub(dif, a, b); \1954+ out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \1955+ out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \1956+ }1957+1958+#define dct_pass(shiftop, shift) \1959+ { \1960+ /* even part */ \1961+ int16x8_t sum26 = vaddq_s16(row2, row6); \1962+ dct_long_mul(p1e, sum26, rot0_0); \1963+ dct_long_mac(t2e, p1e, row6, rot0_1); \1964+ dct_long_mac(t3e, p1e, row2, rot0_2); \1965+ int16x8_t sum04 = vaddq_s16(row0, row4); \1966+ int16x8_t dif04 = vsubq_s16(row0, row4); \1967+ dct_widen(t0e, sum04); \1968+ dct_widen(t1e, dif04); \1969+ dct_wadd(x0, t0e, t3e); \1970+ dct_wsub(x3, t0e, t3e); \1971+ dct_wadd(x1, t1e, t2e); \1972+ dct_wsub(x2, t1e, t2e); \1973+ /* odd part */ \1974+ int16x8_t sum15 = vaddq_s16(row1, row5); \1975+ int16x8_t sum17 = vaddq_s16(row1, row7); \1976+ int16x8_t sum35 = vaddq_s16(row3, row5); \1977+ int16x8_t sum37 = vaddq_s16(row3, row7); \1978+ int16x8_t sumodd = vaddq_s16(sum17, sum35); \1979+ dct_long_mul(p5o, sumodd, rot1_0); \1980+ dct_long_mac(p1o, p5o, sum17, rot1_1); \1981+ dct_long_mac(p2o, p5o, sum35, rot1_2); \1982+ dct_long_mul(p3o, sum37, rot2_0); \1983+ dct_long_mul(p4o, sum15, rot2_1); \1984+ dct_wadd(sump13o, p1o, p3o); \1985+ dct_wadd(sump24o, p2o, p4o); \1986+ dct_wadd(sump23o, p2o, p3o); \1987+ dct_wadd(sump14o, p1o, p4o); \1988+ dct_long_mac(x4, sump13o, row7, rot3_0); \1989+ dct_long_mac(x5, sump24o, row5, rot3_1); \1990+ dct_long_mac(x6, sump23o, row3, rot3_2); \1991+ dct_long_mac(x7, sump14o, row1, rot3_3); \1992+ dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \1993+ dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \1994+ dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \1995+ dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \1996+ }1997+1998+ // load1999+ row0 = vld1q_s16(data + 0*8);2000+ row1 = vld1q_s16(data + 1*8);2001+ row2 = vld1q_s16(data + 2*8);2002+ row3 = vld1q_s16(data + 3*8);2003+ row4 = vld1q_s16(data + 4*8);2004+ row5 = vld1q_s16(data + 5*8);2005+ row6 = vld1q_s16(data + 6*8);2006+ row7 = vld1q_s16(data + 7*8);2007+2008+ // add DC bias2009+ row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0));2010+2011+ // column pass2012+ dct_pass(vrshrn_n_s32, 10);2013+2014+ // 16bit 8x8 transpose2015+ {2016+// these three map to a single VTRN.16, VTRN.32, and VSWP, respectively.2017+// whether compilers actually get this is another story, sadly.2018+#define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; }2019+#define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); }2020+#define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); }2021+2022+ // pass 12023+ dct_trn16(row0, row1); // a0b0a2b2a4b4a6b62024+ dct_trn16(row2, row3);2025+ dct_trn16(row4, row5); 2026+ dct_trn16(row6, row7);2027+2028+ // pass 22029+ dct_trn32(row0, row2); // a0b0c0d0a4b4c4d42030+ dct_trn32(row1, row3);2031+ dct_trn32(row4, row6);2032+ dct_trn32(row5, row7);2033+2034+ // pass 32035+ dct_trn64(row0, row4); // a0b0c0d0e0f0g0h02036+ dct_trn64(row1, row5);2037+ dct_trn64(row2, row6);2038+ dct_trn64(row3, row7);2039+2040+#undef dct_trn162041+#undef dct_trn322042+#undef dct_trn642043+ }2044+2045+ // row pass2046+ // vrshrn_n_s32 only supports shifts up to 16, we need2047+ // 17. so do a non-rounding shift of 16 first then follow2048+ // up with a rounding shift by 1.2049+ dct_pass(vshrn_n_s32, 16);2050+2051+ {2052+ // pack and round2053+ uint8x8_t p0 = vqrshrun_n_s16(row0, 1);2054+ uint8x8_t p1 = vqrshrun_n_s16(row1, 1);2055+ uint8x8_t p2 = vqrshrun_n_s16(row2, 1);2056+ uint8x8_t p3 = vqrshrun_n_s16(row3, 1);2057+ uint8x8_t p4 = vqrshrun_n_s16(row4, 1);2058+ uint8x8_t p5 = vqrshrun_n_s16(row5, 1);2059+ uint8x8_t p6 = vqrshrun_n_s16(row6, 1);2060+ uint8x8_t p7 = vqrshrun_n_s16(row7, 1);2061+2062+ // again, these can translate into one instruction, but often don't.2063+#define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; }2064+#define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); }2065+#define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); }2066+2067+ // sadly can't use interleaved stores here since we only write2068+ // 8 bytes to each scan line!2069+2070+ // 8x8 8-bit transpose pass 12071+ dct_trn8_8(p0, p1);2072+ dct_trn8_8(p2, p3);2073+ dct_trn8_8(p4, p5);2074+ dct_trn8_8(p6, p7);2075+2076+ // pass 22077+ dct_trn8_16(p0, p2);2078+ dct_trn8_16(p1, p3);2079+ dct_trn8_16(p4, p6);2080+ dct_trn8_16(p5, p7);2081+2082+ // pass 32083+ dct_trn8_32(p0, p4);2084+ dct_trn8_32(p1, p5);2085+ dct_trn8_32(p2, p6);2086+ dct_trn8_32(p3, p7);2087+2088+ // store2089+ vst1_u8(out, p0); out += out_stride;2090+ vst1_u8(out, p1); out += out_stride;2091+ vst1_u8(out, p2); out += out_stride;2092+ vst1_u8(out, p3); out += out_stride;2093+ vst1_u8(out, p4); out += out_stride;2094+ vst1_u8(out, p5); out += out_stride;2095+ vst1_u8(out, p6); out += out_stride;2096+ vst1_u8(out, p7);2097+2098+#undef dct_trn8_82099+#undef dct_trn8_162100+#undef dct_trn8_322101+ }2102+2103+#undef dct_long_mul2104+#undef dct_long_mac2105+#undef dct_widen2106+#undef dct_wadd2107+#undef dct_wsub2108+#undef dct_bfly32o2109+#undef dct_pass2110+}2111+2112+#endif // STBI_NEON2113+2114 #define STBI__MARKER_none 0xff2115 // if there's a pending marker from the entropy stream, return that2116 // otherwise, fetch from the stream and get a marker. if there's no2117 // marker, return 0xff, which is never a valid marker value507 unmodified lines2625 2626 return out;2627 }2628 2408-#ifdef STBI_SSE22409-static stbi_uc *stbi__resample_row_hv_2_sse2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)2629+#if defined(STBI_SSE2) || defined(STBI_NEON)2630+static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)2631 {2632 // need to generate 2x2 samples for every one in input2633 int i=0,t0,t1;2413- __m128i bias = _mm_set1_epi16(8);2634 2635 if (w == 1) {2636 out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);2637 return out;3 unmodified lines2641 // process groups of 8 pixels for as long as we can.2642 // note we can't handle the last pixel in a row in this loop2643 // because we need to handle the filter boundary conditions.2644 for (; i < ((w-1) & ~7); i += 8) {2645+#if defined(STBI_SSE2)2646 // load and perform the vertical filtering pass2647 // this uses 3*x + y = 4*x + (y - x)2648 __m128i zero = _mm_setzero_si128();2649 __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i));3 unmodified lines2653 __m128i diff = _mm_sub_epi16(farw, nearw);2654 __m128i nears = _mm_slli_epi16(nearw, 2);2655 __m128i curr = _mm_add_epi16(nears, diff); // current row2656 2436- // horizontal filter works the same based on shifted of current2657+ // horizontal filter works the same based on shifted vers of current2658 // row. "prev" is current row shifted right by 1 pixel; we need to2659 // insert the previous pixel value (from t1).2660 // "next" is current row shifted left by 1 pixel, with first pixel2661 // of next block of 8 pixels added in.5 unmodified lines2667 // horizontal filter, polyphase implementation since it's convenient:2668 // even pixels = 3*cur + prev = cur*4 + (prev - cur)2669 // odd pixels = 3*cur + next = cur*4 + (next - cur)2670 // note the shared term.2671+ __m128i bias = _mm_set1_epi16(8);2672 __m128i curs = _mm_slli_epi16(curr, 2);2673 __m128i prvd = _mm_sub_epi16(prev, curr);2674 __m128i nxtd = _mm_sub_epi16(next, curr);2675 __m128i curb = _mm_add_epi16(curs, bias);8 unmodified lines2684 2685 // pack and write output2686 __m128i outv = _mm_packus_epi16(de0, de1);2687 _mm_storeu_si128((__m128i *) (out + i*2), outv);2688+#elif defined(STBI_NEON)2689+ // load and perform the vertical filtering pass2690+ // this uses 3*x + y = 4*x + (y - x)2691+ uint8x8_t farb = vld1_u8(in_far + i);2692+ uint8x8_t nearb = vld1_u8(in_near + i);2693+ int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb));2694+ int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2));2695+ int16x8_t curr = vaddq_s16(nears, diff); // current row2696 2697+ // horizontal filter works the same based on shifted vers of current2698+ // row. "prev" is current row shifted right by 1 pixel; we need to2699+ // insert the previous pixel value (from t1).2700+ // "next" is current row shifted left by 1 pixel, with first pixel2701+ // of next block of 8 pixels added in.2702+ int16x8_t prv0 = vextq_s16(curr, curr, 7);2703+ int16x8_t nxt0 = vextq_s16(curr, curr, 1);2704+ int16x8_t prev = vsetq_lane_s16(t1, prv0, 0);2705+ int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7);2706+2707+ // horizontal filter, polyphase implementation since it's convenient:2708+ // even pixels = 3*cur + prev = cur*4 + (prev - cur)2709+ // odd pixels = 3*cur + next = cur*4 + (next - cur)2710+ // note the shared term.2711+ int16x8_t curs = vshlq_n_s16(curr, 2);2712+ int16x8_t prvd = vsubq_s16(prev, curr);2713+ int16x8_t nxtd = vsubq_s16(next, curr);2714+ int16x8_t even = vaddq_s16(curs, prvd);2715+ int16x8_t odd = vaddq_s16(curs, nxtd);2716+2717+ // undo scaling and round, then store with even/odd phases interleaved2718+ uint8x8x2_t o;2719+ o.val[0] = vqrshrun_n_s16(even, 4);2720+ o.val[1] = vqrshrun_n_s16(odd, 4);2721+ vst2_u8(out + i*2, o);2722+#endif2723+2724 // "previous" value for next iter2725 t1 = 3*in_near[i+7] + in_far[i+7];2726 }2727 83 unmodified lines2811 }2812 }2813 #endif2814 2558-#ifdef STBI_SSE22559-static void stbi__YCbCr_to_RGB_sse2(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step)2815+#if defined(STBI_SSE2) || defined(STBI_NEON)2816+static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step)2817 {2818 int i = 0;2819 2820+#ifdef STBI_SSE22821 // step == 3 is pretty ugly on the final interleave, and i'm not convinced2822 // it's useful in practice (you wouldn't use it for textures, for example).2823 // so just accelerate step == 4 case.2566- //2567- // note: unlike the IDCT, this isn't bit-identical to the integer version.2824 if (step == 4) {2825 // this is a fairly straightforward implementation and not super-optimized.2826 __m128i signflip = _mm_set1_epi8(-0x80);2827 __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f));47 unmodified lines2875 _mm_storeu_si128((__m128i *) (out + 16), o1);2876 out += 32;2877 }2878 }2879+#endif2880 2881+#ifdef STBI_NEON2882+ // in this version, step=3 support would be easy to add. but is there demand?2883+ if (step == 4) {2884+ // this is a fairly straightforward implementation and not super-optimized.2885+ uint8x8_t signflip = vdup_n_u8(0x80);2886+ int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f));2887+ int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f));2888+ int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f));2889+ int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f));2890+2891+ for (; i+7 < count; i += 8) {2892+ // load2893+ uint8x8_t y_bytes = vld1_u8(y + i);2894+ uint8x8_t cr_bytes = vld1_u8(pcr + i);2895+ uint8x8_t cb_bytes = vld1_u8(pcb + i);2896+ int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip));2897+ int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip));2898+2899+ // expand to s162900+ int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4));2901+ int16x8_t crw = vshll_n_s8(cr_biased, 7);2902+ int16x8_t cbw = vshll_n_s8(cb_biased, 7);2903+2904+ // color transform2905+ int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0);2906+ int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0);2907+ int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1);2908+ int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1);2909+ int16x8_t rws = vaddq_s16(yws, cr0);2910+ int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1);2911+ int16x8_t bws = vaddq_s16(yws, cb1);2912+2913+ // undo scaling, round, convert to byte2914+ uint8x8x4_t o;2915+ o.val[0] = vqrshrun_n_s16(rws, 4);2916+ o.val[1] = vqrshrun_n_s16(gws, 4);2917+ o.val[2] = vqrshrun_n_s16(bws, 4);2918+ o.val[3] = vdup_n_u8(255);2919+2920+ // store, interleaving r/g/b/a2921+ vst4_u8(out, o);2922+ out += 8*4;2923+ }2924+ }2925+#endif2926+2927 for (; i < count; ++i) {2928 int y_fixed = (y[i] << 20) + (1<<19); // rounding2929 int r,g,b;2930 int cr = pcr[i] - 128;26 unmodified lines2957 #ifdef STBI_SSE22958 if (stbi__sse2_available()) {2959 j->idct_block_kernel = stbi__idct_sse2;2960 #ifndef STBI_JPEG_OLD2658- j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_sse2;2961+ j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;2962 #endif2660- j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_sse2;2963+ j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;2964 }2965 #endif2966+2967+#ifdef STBI_NEON2968+ j->idct_block_kernel = stbi__idct_neon;2969+ j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;2970+ j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;2971+#endif2972 }2973 2974 // clean up the temporary component buffers2975 static void stbi__cleanup_jpeg(stbi__jpeg *j)3122 unmodified lines