Repository navigation
Expand file tree
/
Copy pathwtpc_image.h
More file actions
5545 lines (5234 loc) · 262 KB
/
Copy pathwtpc_image.h
File metadata and controls
5545 lines (5234 loc) · 262 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
WTPC - Small Wavelet Thumbnail & Preview Codec - https://github.com/lieff/miniwtpc
No warranty implied; use at your own risk.
LICENSE: Dual-licensed under the Public Domain and the MIT License.
=== API ===
typedef enum {
WTPC_ENC_AUTO = 0,
WTPC_ENC_HUFFMAN = 1,
WTPC_ENC_EBCOT = 2
} wtpc_enc_mode;
typedef struct {
int encoded_bytes; - output number of bytes
int result_q; - resulting quantization factor if target_bytes provided, or same as 'quality' if target_bytes=0
int search_steps; - number of iterations to search target bytes quantization
int ebcot; - 1 = ebcot or 0 = huffman mode for best pick if auto encode_mode used
int huffman_y_size; - in bits if not picked static table
int huffman_u_size;
int huffman_v_size;
int huffman_y_table; - 0..NUM_DEF_TABLES-1 - static, NUM_DEF_TABLES - custom written in bitstream
int huffman_u_table;
int huffman_v_table;
} wtpc_enc_info;
unsigned char *wtpc_encode_mem(const unsigned char *rgb, wtpc_enc_info *info,
int w, int h, int target_bytes, int quality, int chroma_420,
int encode_mode, int huf_extra_ctx, int has_alpha, int stride, int block_size);
Encode an RGB/RGBA image in memory. Returns malloc'd WTPC bitstream,
or NULL on error. Caller must free().
rgb : input pixels, h rows of stride bytes each.
Each row has w pixels, 3 bytes/pixel (RGB) or 4 (RGBA).
info : output struct, filled with encoding details (may be NULL).
w, h : image dimensions (>= 1).
target_bytes : desired output size in bytes. 0 = use 'quality' instead.
When > 0, the encoder does a binary search over the
quality range [1..MAX_QUALITY] to hit the target.
quality : quantization level 1..MAX_QUALITY (1024). Lower = better
quality / larger file. Used only when target_bytes == 0.
chroma_420 : 0 = 4:4:4 (full chroma), 1 = 4:2:0 (half chroma).
4:2:0 saves ~15-30% bytes with minor visual loss.
encode_mode : WTPC_ENC_AUTO (auto-pick ebcot/huffman),
WTPC_ENC_HUFFMAN, WTPC_ENC_EBCOT.
huf_extra_ctx : 0 = single Huffman table (faster),
1 = two context-switched tables (slightly better).
has_alpha : 0 = RGB (3 channels), 1 = RGBA (4 channels).
stride : bytes per row (0 = tightly packed = w * pixel_bytes).
Allows BMP-like padded data without repacking.
block_size : 0 = normal encode, or precinct size for EBCOT block mode
(32, 64, 128, 256, 512, 1024, 2048). Full-image wavelet,
entropy coding split into NxN blocks for speed. 3-4x faster
on large images; only valid with EBCOT (not Huffman).
unsigned char *wtpc_decode_mem(const unsigned char *data, int data_len,
int *w, int *h, int *out_quality, int *out_comp);
Decode a WTPC bitstream from memory. Returns malloc'd pixel buffer
(w*h*3 for RGB, w*h*4 for RGBA). Caller must free().
data : input WTPC bitstream bytes.
data_len : number of bytes in 'data'.
w, h : output image dimensions.
out_quality : quality level used for encoding (may be NULL).
out_comp : number of color components: 3 = RGB, 4 = RGBA (may be NULL).
int wtpc_encode_file(const char *out_path, const unsigned char *rgb,
wtpc_enc_info *info, int w, int h, int target_bytes, int quality,
int chroma_420, int encode_mode, int huf_extra_ctx, int has_alpha, int stride, int block_size);
Same as wtpc_encode_mem but writes directly to a file.
Returns 0 on success, -1 on error.
unsigned char *wtpc_decode_file(const char *in_path,
int *w, int *h, int *out_quality, int *out_comp);
Same as wtpc_decode_mem but reads from a file.
=== Wavelet Hash API (ultra-compact, ThumbHash-like) ===
uint8_t *wtpc_hash_encode_mem(const uint8_t *rgb, int w, int h, int *out_len);
Encode an RGB image (w,h <= 256) to a compact wavelet hash.
Uses CDF 9/7 wavelet + YUV color space, auto-budget from image size.
Returns malloc'd hash buffer, or NULL on error. Caller must free().
rgb : input pixels, tightly packed RGB (w*h*3 bytes).
w, h : image dimensions (1..256).
out_len : filled with hash size in bytes.
uint8_t *wtpc_hash_decode_mem(const uint8_t *hash, int hash_len, int *w, int *h);
Decode a wavelet hash back to w x h RGB image.
Returns malloc'd pixel buffer (w*h*3 bytes), or NULL on error. Caller must free().
hash : input hash bytes (from wtpc_hash_encode_mem).
hash_len : number of bytes in hash.
w, h : filled with decoded image dimensions.
=== Build-time options ===
#define WTPC_NO_STDIO : exclude file I/O functions.
#define DEBUG_WAVELET : dump wavelet coefficient images (needs stb).
#define STANDARD_CDF97 : enable standard CDF 9/7 K-scaling.
#define BAC_USE_TABLE : use 64 KB reciprocal lookup table for
BAC division (~+1-3% speed, 64 KB memory).
Default: 64-bit integer division.
#define WTPC_TUNE_PARAMS : mutable quantization tables for grid-search tuning.
#define WTPC_TUNE_CTX : tune ebcot contexts
#define WTPC_NO_SIMD : do not use sse/avx/neon intrinsics.
#define WTPC_RC_ONLY_LESS_THAN_TARGET : rate control never overshoots
target_bytes (picks the largest size <= target
instead of the closest). Implied by
WTPC_TUNE_PARAMS.
*/
#ifndef WTPC_INCLUDE_WTPC_IMAGE_H
#define WTPC_INCLUDE_WTPC_IMAGE_H
/*#define WTPC_NO_STDIO*/ /* exclude stdio versions */
/*#define DEBUG_WAVELET*/ /* dump wavelet coefficients, requires stb image */
/*#define STANDARD_CDF97*/ /* enable standard CDF 9/7 K-scaling */
/*#define BAC_USE_TABLE*/ /* reciprocal table for BAC division (~+1-3% speed, 64 KB) */
/*#define WTPC_RC_ONLY_LESS_THAN_TARGET*/ /* rate control: never overshoot target_bytes.
Implied by WTPC_TUNE_PARAMS; add explicitly
for tuning builds to avoid metric cheating. */
/*#define WTPC_NO_SIMD*/ /* do not use sse/avx/neon intrinsics */
/*#define WTPC_TUNE_CTX*/ /* tune ebcot contexts */
#ifndef WTPC_NO_STDIO
#include <stdio.h>
#endif /* WTPC_NO_STDIO */
#include <stdint.h>
#define MAX_QUALITY 1024
/* EBCOT: skip flag encoding for first N subbands (mostly non-empty in practice). */
#define SB_SKIP_FLAGS 15
#if defined(_MSC_VER)
#define WTPC_RESTRICT __restrict
#elif defined(__GNUC__) || defined(__clang__)
#define WTPC_RESTRICT __restrict__
#else
#define WTPC_RESTRICT
#endif
#if defined(__GNUC__) || defined(__clang__)
#define WTPC_LIKELY(x) __builtin_expect(!!(x), 1)
#define WTPC_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define WTPC_LIKELY(x) (x)
#define WTPC_UNLIKELY(x) (x)
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* Encoding mode for wtpc_encode_mem / wtpc_encode_file */
typedef enum {
WTPC_ENC_AUTO = 0, /* auto-pick smaller of ebcot/huffman */
WTPC_ENC_HUFFMAN = 1, /* force Huffman */
WTPC_ENC_EBCOT = 2 /* force EBCOT */
} wtpc_enc_mode;
typedef struct {
int encoded_bytes;
int alpha_one; /* original image has alpha, but it's all 1.0 */
int result_q; /* if target_bytes provided */
int search_steps; /* number of iterations to search target bytes quantization */
int ebcot; /* 1 = ebcot or 0 = huffman mode for best pick */
int huffman_y_size; /* in bits if not picked static table */
int huffman_u_size;
int huffman_v_size;
int huffman_y_table; /* 0..NUM_DEF_TABLES-1 - static, NUM_DEF_TABLES - custom written in bitstream */
int huffman_u_table;
int huffman_v_table;
} wtpc_enc_info;
typedef struct {
uint8_t *data;
size_t byte_pos;
size_t buf_size;
uint32_t cache;
int bits_in_cache;
} Bitstream;
uint8_t *wtpc_hash_encode_mem(const uint8_t *rgb, int w, int h, int *out_len);
uint8_t *wtpc_hash_decode_mem(const uint8_t *hash, int hash_len, int *w, int *h);
unsigned char *wtpc_encode_mem(const unsigned char *rgb, wtpc_enc_info *info, int w, int h, int target_bytes, int quality, int chroma_420, int encode_mode, int huf_extra_ctx, int has_alpha, int stride, int block_size);
unsigned char *wtpc_decode_mem(const unsigned char *data, int data_len, int *w, int *h, int *out_quality, int *out_comp);
#ifndef WTPC_NO_STDIO
int wtpc_encode_file(const char *out_path, const unsigned char *rgb, wtpc_enc_info *info, int w, int h, int target_bytes, int quality, int chroma_420, int encode_mode, int huf_extra_ctx, int has_alpha, int stride, int block_size);
unsigned char *wtpc_decode_file(const char *in_path, int *w, int *h, int *out_quality, int *out_comp);
#endif /* WTPC_NO_STDIO */
#ifdef __cplusplus
}
#endif
#endif /* WTPC_INCLUDE_WTPC_H */
#ifdef WTPC_IMAGE_IMPLEMENTATION
/* Rate control "never overshoot" mode: pick the largest encoding that still fits
under target_bytes rather than the one closest to it. Tuning always needs this
(overshoot would cheat the metrics), so WTPC_TUNE_PARAMS implies it. */
#if defined(WTPC_TUNE_PARAMS) && !defined(WTPC_RC_ONLY_LESS_THAN_TARGET) && !defined(WTPC_TUNE_PARAMS_KEEP_RC)
#define WTPC_RC_ONLY_LESS_THAN_TARGET
#endif
#ifdef _WIN32
#define inline __inline
#include <intrin.h>
#pragma intrinsic(_BitScanReverse)
static __inline int msvc_clz32(unsigned int x) {
unsigned long idx; _BitScanReverse(&idx, x); return 31 - (int)idx;
}
#define __builtin_clz(x) msvc_clz32((unsigned int)(x))
#endif
static void bitstream_init(Bitstream *bs, const uint8_t *buffer, size_t buf_size) {
bs->data = (uint8_t*)buffer;
bs->byte_pos = 0;
bs->buf_size = buf_size;
bs->cache = 0;
bs->bits_in_cache = 0;
}
static void bitstream_flush(Bitstream *bs) {
if (bs->bits_in_cache > 0) {
if (bs->byte_pos >= bs->buf_size) { bs->bits_in_cache = 0; return; }
bs->data[bs->byte_pos++] = (uint8_t)(bs->cache << (8 - bs->bits_in_cache));
bs->cache = 0;
bs->bits_in_cache = 0;
}
}
static int bitstream_bits(Bitstream *bs) {
return bs->byte_pos*8+bs->bits_in_cache;
}
static int bitstream_bytes(Bitstream *bs) {
return bs->byte_pos;
}
static void put_bits(Bitstream *bs, uint32_t value, int num_bits) {
value &= ((1ULL << num_bits) - 1);
bs->cache = (bs->cache << num_bits) | value;
bs->bits_in_cache += num_bits;
while (bs->bits_in_cache >= 8) {
bs->bits_in_cache -= 8;
if (bs->byte_pos >= bs->buf_size) { bs->byte_pos++; return; } /* overflow guard */
bs->data[bs->byte_pos++] = (uint8_t)(bs->cache >> bs->bits_in_cache);
}
}
static void bitstream_refill(Bitstream *bs) {
while (bs->bits_in_cache <= 24 && bs->byte_pos < bs->buf_size) {
bs->cache = (bs->cache << 8) | bs->data[bs->byte_pos++];
bs->bits_in_cache += 8;
}
}
static uint32_t get_bits_slow(Bitstream *bs, int num_bits) {
uint32_t result = 0;
/* Take remaining bits from current cache */
if (bs->bits_in_cache > 0) {
int take = bs->bits_in_cache < num_bits ? bs->bits_in_cache : num_bits;
bs->bits_in_cache -= take;
result = (bs->cache >> bs->bits_in_cache) & ((1U << take) - 1);
num_bits -= take;
}
/* If still need bits, refill a fresh 32-bit word and take from it */
if (num_bits > 0) {
bs->cache = 0; bs->bits_in_cache = 0;
while (bs->bits_in_cache < 32 && bs->byte_pos < bs->buf_size) {
bs->cache = (bs->cache << 8) | bs->data[bs->byte_pos++];
bs->bits_in_cache += 8;
}
int take = bs->bits_in_cache < num_bits ? bs->bits_in_cache : num_bits;
bs->bits_in_cache -= take;
result = (result << take) | ((bs->cache >> bs->bits_in_cache) & ((1U << take) - 1));
}
/* Top up cache for subsequent fast-path hits */
bitstream_refill(bs);
return result;
}
static inline uint32_t get_bits(Bitstream *bs, int num_bits) {
if (bs->bits_in_cache < num_bits)
return get_bits_slow(bs, num_bits);
bs->bits_in_cache -= num_bits;
return (bs->cache >> bs->bits_in_cache) & ((1U << num_bits) - 1);
}
/* --- Big-endian uint16 (endian-agnostic file format) --- */
static inline void put_u16(uint8_t *dst, uint16_t v) {
dst[0] = (uint8_t)(v >> 8);
dst[1] = (uint8_t)(v);
}
static inline uint16_t get_u16(const uint8_t *src) {
return ((uint16_t)src[0] << 8) | src[1];
}
/* --- Exp-Golomb coding (unsigned, order k=0) --- */
static void put_eg(Bitstream *bs, uint32_t v) {
v++; /* EG(k=0): code = value + 1, so 0->"1", 1->"010", 2->"011", ... */
if (v < 2) { put_bits(bs, 1, 1); return; }
int m = 31 - __builtin_clz(v); /* floor(log2(v)), v>=2 -> clz safe */
for (int i = 0; i < m; i++) put_bits(bs, 0, 1);
put_bits(bs, 1, 1);
put_bits(bs, v - (1u << m), m);
}
/* Optimized Exp-Golomb decode. Fast path: run=0 (single '1' bit, ~90% of calls).
Slow path: CLZ-based leading-zero count reads up to 24 bits at once. */
static uint32_t get_eg_slow(Bitstream *bs) {
int m = 0;
for (;;) {
bitstream_refill(bs);
int peek_bits = bs->bits_in_cache < 24 ? bs->bits_in_cache : 24;
if (peek_bits == 0) return 0;
uint32_t peek = (bs->cache >> (bs->bits_in_cache - peek_bits)) & ((1u << peek_bits) - 1);
if (peek != 0) {
int zm = __builtin_clz(peek) - (32 - peek_bits);
m += zm;
get_bits(bs, zm + 1); /* consume zm zeros + the '1' */
break;
}
m += peek_bits;
get_bits(bs, peek_bits); /* consume all zeros, continue */
}
if (m >= 31) return 0;
uint32_t v = 1u << m;
if (m > 0) v |= get_bits(bs, m);
return v - 1;
}
static inline uint32_t get_eg(Bitstream *bs) {
/* Fast path: run=0 -> code is just a single '1' bit */
if (bs->bits_in_cache > 0 && ((bs->cache >> (bs->bits_in_cache - 1)) & 1)) {
bs->bits_in_cache--;
return 0;
}
return get_eg_slow(bs);
}
/* BT.601 YUV color conversion. */
/* Y, U, V all centered around 0 (Y in -128..127, U/V in -128..127) so that */
/* wavelet LL does not accumulate a large DC bias (halves worst-case LL coeff, */
/* improving int16 headroom on large images and giving symmetric dead-zone). */
static inline void rgb_to_yuv(uint8_t r, uint8_t g, uint8_t b, float *y, float *u, float *v) {
*y = 0.299000f * r + 0.587000f * g + 0.114000f * b - 128.0f;
*u = -0.168736f * r - 0.331264f * g + 0.500000f * b;
*v = 0.500000f * r - 0.418688f * g - 0.081312f * b;
}
static inline void yuv_to_rgb(float y, float u, float v, uint8_t *r, uint8_t *g, uint8_t *b) {
y += 128.0f;
float r_f = y + 1.40200f * v;
float g_f = y - 0.34414f * u - 0.71414f * v;
float b_f = y + 1.77200f * u;
*r = (uint8_t)(r_f < 0 ? 0 : (r_f > 255 ? 255 : roundf(r_f)));
*g = (uint8_t)(g_f < 0 ? 0 : (g_f > 255 ? 255 : roundf(g_f)));
*b = (uint8_t)(b_f < 0 ? 0 : (b_f > 255 ? 255 : roundf(b_f)));
}
/* --- 4:2:0 chroma down/up sampling (high quality) --- */
static void chroma_down_420(float * WTPC_RESTRICT src, int w, int h, float * WTPC_RESTRICT dst, int dw, int dh) {
for (int y = 0; y < dh; y++) {
for (int x = 0; x < dw; x++) {
int sx0 = x * w / dw, sy0 = y * h / dh;
int sx1 = (x + 1) * w / dw, sy1 = (y + 1) * h / dh;
if (sx1 <= sx0) sx1 = sx0 + 1;
if (sy1 <= sy0) sy1 = sy0 + 1;
float sum = 0.0f; int cnt = 0;
for (int sy = sy0; sy < sy1; sy++)
for (int sx = sx0; sx < sx1; sx++) { sum += src[sy*w + sx]; cnt++; }
dst[y*dw + x] = sum / (float)cnt;
}
}
}
static void chroma_up_420(float * WTPC_RESTRICT src, int sw, int sh, float * WTPC_RESTRICT dst, int dw, int dh) {
for (int y = 0; y < dh; y++) {
/* Downsampled sample i sits at full-res center (i+0.5)*dw/sw. */
/* For target full-res y the corresponding src coordinate is y*sh/dh-0.5. */
/* Clamp to [0, sh-1] so the fractional part stays in [0, 1). */
float fy = ((float)y + 0.5f) * sh / dh - 0.5f;
if (fy < 0.0f) fy = 0.0f;
int iy = (int)fy; if (iy >= sh-1) iy = (sh > 1) ? sh-2 : 0;
int iy1 = (iy + 1 < sh) ? iy + 1 : iy; /* clamp neighbour (sh==1) */
float wy = fy - (float)iy;
for (int x = 0; x < dw; x++) {
float fx = ((float)x + 0.5f) * sw / dw - 0.5f;
if (fx < 0.0f) fx = 0.0f;
int ix = (int)fx; if (ix >= sw-1) ix = (sw > 1) ? sw-2 : 0;
int ix1 = (ix + 1 < sw) ? ix + 1 : ix; /* clamp neighbour (sw==1) */
float wx = fx - (float)ix;
float v00 = src[iy*sw + ix], v10 = src[iy*sw + ix1];
float v01 = src[iy1*sw + ix], v11 = src[iy1*sw + ix1];
dst[y*dw + x] = v00*(1-wx)*(1-wy) + v10*wx*(1-wy) + v01*(1-wx)*wy + v11*wx*wy;
}
}
}
/* --- CDF 9/7 WAVELET (2D LIFTING, JPEG2000 LOSSY) --- */
/* Lifting constants (double precision). */
/* Symmetric boundary extension. */
/* Standard K-scaling (CDF97_K=1.1496) omitted - compensated by quantization. Set STANDARD_CDF97 to enable. */
#define CDF97_A -1.586134342059924
#define CDF97_B -0.052980118572961
#define CDF97_G 0.882911075530934
#ifdef STANDARD_CDF97
#define CDF97_D 0.443506852043971
#else
/* 0.48 was measured ~+0.3 ssimulacra2 at low bitrates on the full thumbnail set (peak of the 0.46-0.50 plateau) */
#ifdef WTPC_TUNE_CDF97_D
static float g_cdf97_d = 0.48f;
#define CDF97_D g_cdf97_d
#else
#define CDF97_D 0.48
#endif
#endif
#ifdef STANDARD_CDF97
#define CDF97_K 1.14960439886025f
#define CDF97_INVK (1.0f / CDF97_K)
#else
#define CDF97_K 1.0f
#define CDF97_INVK 1.0f
#endif
#define WTPC_CPU_SSE2 (1u<<0)
#define WTPC_CPU_AVX (1u<<1)
#define WTPC_CPU_NEON (1u<<2)
static unsigned wtpc_cpu_flags_ = 0;
static int wtpc_cpu_done_ = 0;
static void wtpc_cpu_init_(void) {
if (wtpc_cpu_done_) return;
wtpc_cpu_done_ = 1;
/* SSE2 - baseline on x86-64, compile-time */
#if (defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2))
wtpc_cpu_flags_ |= WTPC_CPU_SSE2;
#endif
/* AVX - runtime probe (may be compiled but CPU lacks it) */
#if defined(__AVX__) || ((defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)))
#if defined(__GNUC__) || defined(__clang__)
if (__builtin_cpu_supports("avx")) wtpc_cpu_flags_ |= WTPC_CPU_AVX;
#elif defined(_MSC_VER)
{ int r_[4]; __cpuidex(r_, 1, 0); if (r_[2] & (1<<28)) wtpc_cpu_flags_ |= WTPC_CPU_AVX; }
#endif
#endif
/* NEON - baseline on AArch64, optional on ARMv7 */
#if defined(__aarch64__)
wtpc_cpu_flags_ |= WTPC_CPU_NEON;
#elif defined(__ARM_NEON)
wtpc_cpu_flags_ |= WTPC_CPU_NEON; /* compiler guarantees NEON is present */
#endif
}
/* ================================================================
* SSE2 4-wide wrappers (compiled on x86-64, no runtime flag needed)
* ================================================================ */
#if (defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2)) && !defined(WTPC_NO_SIMD)
#include <emmintrin.h>
#define SIMD4_W 4
typedef __m128 simd4_f;
static inline simd4_f simd4_set1(float x) { return _mm_set1_ps(x); }
static inline simd4_f simd4_load(const float *p) { return _mm_loadu_ps(p); }
static inline void simd4_store(float *p, simd4_f v) { _mm_storeu_ps(p, v); }
static inline simd4_f simd4_add(simd4_f a, simd4_f b) { return _mm_add_ps(a,b); }
static inline simd4_f simd4_sub(simd4_f a, simd4_f b) { return _mm_sub_ps(a,b); }
static inline simd4_f simd4_mul(simd4_f a, simd4_f b) { return _mm_mul_ps(a,b); }
static inline simd4_f simd4_unpacklo(simd4_f a, simd4_f b) { return _mm_unpacklo_ps(a,b); }
static inline simd4_f simd4_unpackhi(simd4_f a, simd4_f b) { return _mm_unpackhi_ps(a,b); }
#define WTPC_HAS_SSE2 1
#endif
/* ================================================================
* AVX 8-wide wrappers (compiled on GCC/Clang w/ target attr)
* ================================================================ */
#if (defined(__AVX__) || ((defined(__GNUC__) || defined(__clang__)) && (defined(__x86_64__) || defined(__i386__)))) && !defined(WTPC_NO_SIMD)
#include <immintrin.h>
#define SIMD8_W 8
typedef __m256 simd8_f;
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC push_options
#pragma GCC target("avx")
#endif
static inline simd8_f simd8_set1(float x) { return _mm256_set1_ps(x); }
static inline simd8_f simd8_load(const float *p) { return _mm256_loadu_ps(p); }
static inline void simd8_store(float *p, simd8_f v) { _mm256_storeu_ps(p, v); }
static inline simd8_f simd8_add(simd8_f a, simd8_f b) { return _mm256_add_ps(a,b); }
static inline simd8_f simd8_sub(simd8_f a, simd8_f b) { return _mm256_sub_ps(a,b); }
static inline simd8_f simd8_mul(simd8_f a, simd8_f b) { return _mm256_mul_ps(a,b); }
/* AVX unpack interleaves within 128-bit lanes -> manual fix */
static inline simd8_f simd8_unpacklo(simd8_f a, simd8_f b) {
__m128 al=_mm256_castps256_ps128(a), bl=_mm256_castps256_ps128(b);
__m128 r0=_mm_unpacklo_ps(al,bl), r1=_mm_unpackhi_ps(al,bl);
return _mm256_insertf128_ps(_mm256_castps128_ps256(r0), r1, 1);
}
static inline simd8_f simd8_unpackhi(simd8_f a, simd8_f b) {
__m128 ah=_mm256_extractf128_ps(a,1), bh=_mm256_extractf128_ps(b,1);
__m128 r0=_mm_unpacklo_ps(ah,bh), r1=_mm_unpackhi_ps(ah,bh);
return _mm256_insertf128_ps(_mm256_castps128_ps256(r0), r1, 1);
}
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC pop_options
#endif
#define WTPC_HAS_AVX 1
#endif
/* ================================================================
* NEON 4-wide wrappers (AArch64 / ARMv7+NEON)
* ================================================================ */
#if (defined(__aarch64__) || defined(__ARM_NEON)) && !defined(WTPC_NO_SIMD)
#include <arm_neon.h>
#define SIMDN_W 4
typedef float32x4_t simdn_f;
static inline simdn_f simdn_set1(float x) { return vdupq_n_f32(x); }
static inline simdn_f simdn_load(const float *p) { return vld1q_f32(p); }
static inline void simdn_store(float *p, simdn_f v) { vst1q_f32(p, v); }
static inline simdn_f simdn_add(simdn_f a, simdn_f b) { return vaddq_f32(a,b); }
static inline simdn_f simdn_sub(simdn_f a, simdn_f b) { return vsubq_f32(a,b); }
static inline simdn_f simdn_mul(simdn_f a, simdn_f b) { return vmulq_f32(a,b); }
/* NEON vzipq_f32 interleaves two registers */
static inline simdn_f simdn_unpacklo(simdn_f a, simdn_f b) {
float32x4x2_t z = vzipq_f32(a, b); return z.val[0]; /* a0,b0,a1,b1 */
}
static inline simdn_f simdn_unpackhi(simdn_f a, simdn_f b) {
float32x4x2_t z = vzipq_f32(a, b); return z.val[1]; /* a2,b2,a3,b3 */
}
#define WTPC_HAS_NEON 1
#endif
/* ================================================================
* X-macros: generate one variant per (prefix, simd_type, SIMD_W)
*
* WTPC_FWD_HORIZ(FN, sf, SW, pf) - forward horizontal lifting
* WTPC_FWD_VERT (FN, sf, SW, pf) - forward vertical lifting
* WTPC_INV_HORIZ(FN, sf, SW, pf) - inverse horizontal lifting
* WTPC_INV_VERT (FN, sf, SW, pf) - inverse vertical lifting
* ================================================================ */
#define WTPC_FWD_HORIZ(FN, sf, SW, pf) \
static void FN(float *data, int stride, int w, int h, \
float *tl, float *th) \
{ \
if (w <= 1) return; \
int lw = (w + 1) / 2, hw = w / 2; \
const int MS = SW; \
const sf vA = pf##_set1(CDF97_A), vB = pf##_set1(CDF97_B); \
const sf vG = pf##_set1(CDF97_G), vD = pf##_set1(CDF97_D); \
const sf vK = pf##_set1(CDF97_K), vIK = pf##_set1(CDF97_INVK); \
for (int row = 0; row < h; ++row) { \
float *r = data + row * stride; \
int j; \
for (j = 0; j < hw; ++j) { \
tl[j] = r[2*j]; \
th[j] = r[2*j + 1]; \
} \
if (w & 1) tl[lw - 1] = r[w - 1]; \
/* step 1: predict1 h[j] += A * (l[j] + l[j+1]) */ \
for (j = 0; j + MS < lw && j + MS - 1 < hw; j += MS) { \
sf a = pf##_load(&tl[j]), b = pf##_load(&tl[j + 1]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th[j]); \
c = pf##_add(c, pf##_mul(vA, s)); \
pf##_store(&th[j], c); \
} \
for (; j < hw; ++j) { \
int jn = (j + 1 < lw) ? j + 1 : lw - 1; \
th[j] += CDF97_A * (tl[j] + tl[jn]); \
} \
/* step 2: update1 l[j] += B * (h[j-1] + h[j]) */ \
tl[0] += CDF97_B * (th[0] + th[0]); \
for (j = 1; j + MS < lw && j + MS - 1 < hw; j += MS) { \
sf a = pf##_load(&th[j - 1]), b = pf##_load(&th[j]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl[j]); \
c = pf##_add(c, pf##_mul(vB, s)); \
pf##_store(&tl[j], c); \
} \
for (; j < lw; ++j) { \
int jp = (j > 0) ? j - 1 : 0; \
int jj = (j < hw) ? j : hw - 1; \
tl[j] += CDF97_B * (th[jp] + th[jj]); \
} \
/* step 3: predict2 h[j] += G * (l[j] + l[j+1]) */ \
for (j = 0; j + MS < lw && j + MS - 1 < hw; j += MS) { \
sf a = pf##_load(&tl[j]), b = pf##_load(&tl[j + 1]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th[j]); \
c = pf##_add(c, pf##_mul(vG, s)); \
pf##_store(&th[j], c); \
} \
for (; j < hw; ++j) { \
int jn = (j + 1 < lw) ? j + 1 : lw - 1; \
th[j] += CDF97_G * (tl[j] + tl[jn]); \
} \
/* step 4: update2 l[j] += D * (h[j-1] + h[j]) */ \
tl[0] += CDF97_D * (th[0] + th[0]); \
for (j = 1; j + MS < lw && j + MS - 1 < hw; j += MS) { \
sf a = pf##_load(&th[j - 1]), b = pf##_load(&th[j]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl[j]); \
c = pf##_add(c, pf##_mul(vD, s)); \
pf##_store(&tl[j], c); \
} \
for (; j < lw; ++j) { \
int jp = (j > 0) ? j - 1 : 0; \
int jj = (j < hw) ? j : hw - 1; \
tl[j] += CDF97_D * (th[jp] + th[jj]); \
} \
/* write-back: LL then HL (contiguous, K-scaled) */ \
for (j = 0; j + MS - 1 < lw; j += MS) { \
pf##_store(&r[j], pf##_mul(pf##_load(&tl[j]), vK)); \
} \
for (; j < lw; ++j) r[j] = tl[j] * CDF97_K; \
for (j = 0; j + MS < lw && j + MS - 1 < hw; j += MS) { \
pf##_store(&r[lw + j], pf##_mul(pf##_load(&th[j]), vIK)); \
} \
for (; j < hw; ++j) r[lw + j] = th[j] * CDF97_INVK; \
} \
}
#define WTPC_FWD_VERT(FN, sf, SW, pf) \
static void FN(float *data, int stride, int w, int h, \
float *tl4, float *th4) \
{ \
if (h <= 1) return; \
int lh = (h + 1) / 2, hh = h / 2; \
const sf vA = pf##_set1(CDF97_A), vB = pf##_set1(CDF97_B); \
const sf vG = pf##_set1(CDF97_G), vD = pf##_set1(CDF97_D); \
const sf vK = pf##_set1(CDF97_K), vIK = pf##_set1(CDF97_INVK); \
for (int col = 0; col < w; col += SW) { \
int nc = (w - col >= SW) ? SW : (w - col); \
/* deinterleave rows into tl4[i*SW+c], th4[i*SW+c] */ \
for (int i = 0; i < hh; ++i) { \
for (int c = 0; c < nc; ++c) { \
tl4[i*SW + c] = data[(2*i) * stride + col + c]; \
th4[i*SW + c] = data[(2*i + 1) * stride + col + c]; \
} \
} \
if (h & 1) { \
for (int c = 0; c < nc; ++c) \
tl4[(lh - 1)*SW + c] = data[(h - 1) * stride + col + c]; \
} \
if (nc == SW) { \
/* ---- step 1: predict1 ---- */ \
for (int i = 0; i + 1 < hh; ++i) { \
sf a = pf##_load(&tl4[i*SW]), b = pf##_load(&tl4[(i + 1)*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th4[i*SW]); \
c = pf##_add(c, pf##_mul(vA, s)); \
pf##_store(&th4[i*SW], c); \
} \
{ \
int i = hh - 1, in = (i + 1 < lh) ? i + 1 : lh - 1; \
sf a = pf##_load(&tl4[i*SW]), b = pf##_load(&tl4[in*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th4[i*SW]); \
c = pf##_add(c, pf##_mul(vA, s)); \
pf##_store(&th4[i*SW], c); \
} \
/* ---- step 2: update1 ---- */ \
{ \
sf a = pf##_load(&th4[0]); \
sf s = pf##_add(a, a); \
sf b = pf##_load(&tl4[0]); \
b = pf##_add(b, pf##_mul(vB, s)); \
pf##_store(&tl4[0], b); \
} \
for (int i = 1; i < hh; ++i) { \
sf a = pf##_load(&th4[(i - 1)*SW]), b = pf##_load(&th4[i*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl4[i*SW]); \
c = pf##_add(c, pf##_mul(vB, s)); \
pf##_store(&tl4[i*SW], c); \
} \
for (int i = hh; i < lh; ++i) { \
sf a = pf##_load(&th4[(i - 1)*SW]), b = pf##_load(&th4[(hh - 1)*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl4[i*SW]); \
c = pf##_add(c, pf##_mul(vB, s)); \
pf##_store(&tl4[i*SW], c); \
} \
/* ---- step 3: predict2 ---- */ \
for (int i = 0; i + 1 < hh; ++i) { \
sf a = pf##_load(&tl4[i*SW]), b = pf##_load(&tl4[(i + 1)*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th4[i*SW]); \
c = pf##_add(c, pf##_mul(vG, s)); \
pf##_store(&th4[i*SW], c); \
} \
{ \
int i = hh - 1, in = (i + 1 < lh) ? i + 1 : lh - 1; \
sf a = pf##_load(&tl4[i*SW]), b = pf##_load(&tl4[in*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th4[i*SW]); \
c = pf##_add(c, pf##_mul(vG, s)); \
pf##_store(&th4[i*SW], c); \
} \
/* ---- step 4: update2 ---- */ \
{ \
sf a = pf##_load(&th4[0]); \
sf s = pf##_add(a, a); \
sf b = pf##_load(&tl4[0]); \
b = pf##_add(b, pf##_mul(vD, s)); \
pf##_store(&tl4[0], b); \
} \
for (int i = 1; i < hh; ++i) { \
sf a = pf##_load(&th4[(i - 1)*SW]), b = pf##_load(&th4[i*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl4[i*SW]); \
c = pf##_add(c, pf##_mul(vD, s)); \
pf##_store(&tl4[i*SW], c); \
} \
for (int i = hh; i < lh; ++i) { \
sf a = pf##_load(&th4[(i - 1)*SW]), b = pf##_load(&th4[(hh - 1)*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl4[i*SW]); \
c = pf##_add(c, pf##_mul(vD, s)); \
pf##_store(&tl4[i*SW], c); \
} \
/* write-back: LL rows then HH rows */ \
for (int i = 0; i < lh; ++i) { \
sf v = pf##_load(&tl4[i*SW]); \
pf##_store(&data[i * stride + col], pf##_mul(v, vK)); \
} \
for (int i = 0; i < hh; ++i) { \
sf v = pf##_load(&th4[i*SW]); \
pf##_store(&data[(lh + i) * stride + col], pf##_mul(v, vIK)); \
} \
} else { \
/* nc < SW: scalar tail for partial column group */ \
for (int i = 0; i < hh; ++i) { \
int in = (i + 1 < lh) ? i + 1 : lh - 1; \
for (int c = 0; c < nc; ++c) \
th4[i*SW + c] += CDF97_A * (tl4[i*SW + c] + tl4[in*SW + c]); \
} \
for (int i = 0; i < lh; ++i) { \
int ip = (i > 0) ? i - 1 : 0, ii = (i < hh) ? i : hh - 1; \
for (int c = 0; c < nc; ++c) \
tl4[i*SW + c] += CDF97_B * (th4[ip*SW + c] + th4[ii*SW + c]); \
} \
for (int i = 0; i < hh; ++i) { \
int in = (i + 1 < lh) ? i + 1 : lh - 1; \
for (int c = 0; c < nc; ++c) \
th4[i*SW + c] += CDF97_G * (tl4[i*SW + c] + tl4[in*SW + c]); \
} \
for (int i = 0; i < lh; ++i) { \
int ip = (i > 0) ? i - 1 : 0, ii = (i < hh) ? i : hh - 1; \
for (int c = 0; c < nc; ++c) \
tl4[i*SW + c] += CDF97_D * (th4[ip*SW + c] + th4[ii*SW + c]); \
} \
for (int i = 0; i < lh; ++i) \
for (int c = 0; c < nc; ++c) \
data[i * stride + col + c] = tl4[i*SW + c] * CDF97_K; \
for (int i = 0; i < hh; ++i) \
for (int c = 0; c < nc; ++c) \
data[(lh + i) * stride + col + c] = th4[i*SW + c] * CDF97_INVK; \
} \
} \
}
#define WTPC_INV_HORIZ(FN, sf, SW, pf) \
static void FN(float *data, int stride, int w, int h, \
float *tl, float *th) \
{ \
if (w <= 1) return; \
int lw = (w + 1) / 2, hw = w / 2; \
const int MS = SW; \
const sf vA = pf##_set1(CDF97_A), vB = pf##_set1(CDF97_B); \
const sf vG = pf##_set1(CDF97_G), vD = pf##_set1(CDF97_D); \
for (int row = 0; row < h; ++row) { \
float *r = data + row * stride; \
int j; \
/* copy LL + HH back to temp */ \
for (j = 0; j < lw; ++j) tl[j] = r[j]; \
for (j = 0; j < hw; ++j) th[j] = r[lw + j]; \
/* inv update2: l[j] -= D * (h[j-1] + h[j]) */ \
tl[0] -= CDF97_D * (th[0] + th[0]); \
for (j = 1; j + MS < lw && j + MS - 1 < hw; j += MS) { \
sf a = pf##_load(&th[j - 1]), b = pf##_load(&th[j]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl[j]); \
c = pf##_sub(c, pf##_mul(vD, s)); \
pf##_store(&tl[j], c); \
} \
for (; j < lw; ++j) { \
int jp = (j > 0) ? j - 1 : 0; \
int jj = (j < hw) ? j : hw - 1; \
tl[j] -= CDF97_D * (th[jp] + th[jj]); \
} \
/* inv predict2: h[j] -= G * (l[j] + l[j+1]) */ \
for (j = 0; j + MS < lw && j + MS - 1 < hw; j += MS) { \
sf a = pf##_load(&tl[j]), b = pf##_load(&tl[j + 1]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th[j]); \
c = pf##_sub(c, pf##_mul(vG, s)); \
pf##_store(&th[j], c); \
} \
for (; j < hw; ++j) { \
int jn = (j + 1 < lw) ? j + 1 : lw - 1; \
th[j] -= CDF97_G * (tl[j] + tl[jn]); \
} \
/* inv update1: l[j] -= B * (h[j-1] + h[j]) */ \
tl[0] -= CDF97_B * (th[0] + th[0]); \
for (j = 1; j + MS < lw && j + MS - 1 < hw; j += MS) { \
sf a = pf##_load(&th[j - 1]), b = pf##_load(&th[j]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl[j]); \
c = pf##_sub(c, pf##_mul(vB, s)); \
pf##_store(&tl[j], c); \
} \
for (; j < lw; ++j) { \
int jp = (j > 0) ? j - 1 : 0; \
int jj = (j < hw) ? j : hw - 1; \
tl[j] -= CDF97_B * (th[jp] + th[jj]); \
} \
/* inv predict1: h[j] -= A * (l[j] + l[j+1]) */ \
for (j = 0; j + MS < lw && j + MS - 1 < hw; j += MS) { \
sf a = pf##_load(&tl[j]), b = pf##_load(&tl[j + 1]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th[j]); \
c = pf##_sub(c, pf##_mul(vA, s)); \
pf##_store(&th[j], c); \
} \
for (; j < hw; ++j) { \
int jn = (j + 1 < lw) ? j + 1 : lw - 1; \
th[j] -= CDF97_A * (tl[j] + tl[jn]); \
} \
/* write-back interleaved: r[2*j]=l, r[2*j+1]=h */ \
for (j = 0; j + MS - 1 < hw; j += MS) { \
sf l = pf##_load(&tl[j]), h = pf##_load(&th[j]); \
sf lo = pf##_unpacklo(l, h), hi = pf##_unpackhi(l, h); \
pf##_store(&r[2*j], lo); \
pf##_store(&r[2*j + MS], hi); \
} \
for (; j < hw; ++j) { \
r[2*j] = tl[j]; \
r[2*j + 1] = th[j]; \
} \
for (j = hw; j < lw; ++j) r[2*j] = tl[j]; \
} \
}
#define WTPC_INV_VERT(FN, sf, SW, pf) \
static void FN(float *data, int stride, int w, int h, \
float *tl4, float *th4) \
{ \
if (h <= 1) return; \
int lh = (h + 1) / 2, hh = h / 2; \
const sf vA = pf##_set1(CDF97_A), vB = pf##_set1(CDF97_B); \
const sf vG = pf##_set1(CDF97_G), vD = pf##_set1(CDF97_D); \
for (int col = 0; col < w; col += SW) { \
int nc = (w - col >= SW) ? SW : (w - col); \
/* load LL and HH for these columns */ \
for (int i = 0; i < lh; ++i) \
for (int c = 0; c < nc; ++c) \
tl4[i*SW + c] = data[i * stride + col + c]; \
for (int i = 0; i < hh; ++i) \
for (int c = 0; c < nc; ++c) \
th4[i*SW + c] = data[(lh + i) * stride + col + c]; \
if (nc == SW) { \
/* inv update2 */ \
{ \
sf a = pf##_load(&th4[0]); \
sf s = pf##_add(a, a); \
sf c = pf##_load(&tl4[0]); \
c = pf##_sub(c, pf##_mul(vD, s)); \
pf##_store(&tl4[0], c); \
} \
for (int i = 1; i < hh; ++i) { \
sf a = pf##_load(&th4[(i - 1)*SW]), b = pf##_load(&th4[i*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl4[i*SW]); \
c = pf##_sub(c, pf##_mul(vD, s)); \
pf##_store(&tl4[i*SW], c); \
} \
for (int i = hh; i < lh; ++i) { \
sf a = pf##_load(&th4[(i - 1)*SW]), b = pf##_load(&th4[(hh - 1)*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl4[i*SW]); \
c = pf##_sub(c, pf##_mul(vD, s)); \
pf##_store(&tl4[i*SW], c); \
} \
/* inv predict2 */ \
for (int i = 0; i + 1 < hh; ++i) { \
sf a = pf##_load(&tl4[i*SW]), b = pf##_load(&tl4[(i + 1)*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th4[i*SW]); \
c = pf##_sub(c, pf##_mul(vG, s)); \
pf##_store(&th4[i*SW], c); \
} \
{ \
int i = hh - 1, in = (i + 1 < lh) ? i + 1 : lh - 1; \
sf a = pf##_load(&tl4[i*SW]), b = pf##_load(&tl4[in*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th4[i*SW]); \
c = pf##_sub(c, pf##_mul(vG, s)); \
pf##_store(&th4[i*SW], c); \
} \
/* inv update1 */ \
{ \
sf a = pf##_load(&th4[0]); \
sf s = pf##_add(a, a); \
sf c = pf##_load(&tl4[0]); \
c = pf##_sub(c, pf##_mul(vB, s)); \
pf##_store(&tl4[0], c); \
} \
for (int i = 1; i < hh; ++i) { \
sf a = pf##_load(&th4[(i - 1)*SW]), b = pf##_load(&th4[i*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl4[i*SW]); \
c = pf##_sub(c, pf##_mul(vB, s)); \
pf##_store(&tl4[i*SW], c); \
} \
for (int i = hh; i < lh; ++i) { \
sf a = pf##_load(&th4[(i - 1)*SW]), b = pf##_load(&th4[(hh - 1)*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&tl4[i*SW]); \
c = pf##_sub(c, pf##_mul(vB, s)); \
pf##_store(&tl4[i*SW], c); \
} \
/* inv predict1 */ \
for (int i = 0; i + 1 < hh; ++i) { \
sf a = pf##_load(&tl4[i*SW]), b = pf##_load(&tl4[(i + 1)*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th4[i*SW]); \
c = pf##_sub(c, pf##_mul(vA, s)); \
pf##_store(&th4[i*SW], c); \
} \
{ \
int i = hh - 1, in = (i + 1 < lh) ? i + 1 : lh - 1; \
sf a = pf##_load(&tl4[i*SW]), b = pf##_load(&tl4[in*SW]); \
sf s = pf##_add(a, b); \
sf c = pf##_load(&th4[i*SW]); \
c = pf##_sub(c, pf##_mul(vA, s)); \
pf##_store(&th4[i*SW], c); \
} \
/* write-back interleaved: even rows = LL, odd rows = HH */ \
for (int i = 0; i < lh; ++i) { \
sf v = pf##_load(&tl4[i*SW]); \
pf##_store(&data[(2*i) * stride + col], v); \
} \
for (int i = 0; i < hh; ++i) { \
sf v = pf##_load(&th4[i*SW]); \
pf##_store(&data[(2*i + 1) * stride + col], v); \
} \
} else { \
/* scalar tail for partial column group */ \
for (int i = 0; i < lh; ++i) { \
int ip = (i > 0) ? i - 1 : 0, ii = (i < hh) ? i : hh - 1; \
for (int c = 0; c < nc; ++c) \
tl4[i*SW + c] -= CDF97_D * (th4[ip*SW + c] + th4[ii*SW + c]); \
} \
for (int i = 0; i < hh; ++i) { \
int in = (i + 1 < lh) ? i + 1 : lh - 1; \
for (int c = 0; c < nc; ++c) \
th4[i*SW + c] -= CDF97_G * (tl4[i*SW + c] + tl4[in*SW + c]); \
} \
for (int i = 0; i < lh; ++i) { \
int ip = (i > 0) ? i - 1 : 0, ii = (i < hh) ? i : hh - 1; \
for (int c = 0; c < nc; ++c) \
tl4[i*SW + c] -= CDF97_B * (th4[ip*SW + c] + th4[ii*SW + c]); \
} \
for (int i = 0; i < hh; ++i) { \
int in = (i + 1 < lh) ? i + 1 : lh - 1; \
for (int c = 0; c < nc; ++c) \
th4[i*SW + c] -= CDF97_A * (tl4[i*SW + c] + tl4[in*SW + c]); \
} \
for (int i = 0; i < lh; ++i) \
for (int c = 0; c < nc; ++c) \
data[(2*i) * stride + col + c] = tl4[i*SW + c]; \
for (int i = 0; i < hh; ++i) \
for (int c = 0; c < nc; ++c) \
data[(2*i + 1) * stride + col + c] = th4[i*SW + c]; \
} \
} \
}
#ifdef WTPC_HAS_SSE2
WTPC_FWD_HORIZ(fwd_horiz_sse2, simd4_f, SIMD4_W, simd4)
WTPC_FWD_VERT (fwd_vert_sse2, simd4_f, SIMD4_W, simd4)
WTPC_INV_HORIZ(inv_horiz_sse2, simd4_f, SIMD4_W, simd4)
WTPC_INV_VERT (inv_vert_sse2, simd4_f, SIMD4_W, simd4)
#endif
#ifdef WTPC_HAS_AVX
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC push_options
#pragma GCC target("avx")
#endif
WTPC_FWD_HORIZ(fwd_horiz_avx, simd8_f, SIMD8_W, simd8)
WTPC_FWD_VERT (fwd_vert_avx, simd8_f, SIMD8_W, simd8)
WTPC_INV_HORIZ(inv_horiz_avx, simd8_f, SIMD8_W, simd8)
WTPC_INV_VERT (inv_vert_avx, simd8_f, SIMD8_W, simd8)
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC pop_options
#endif
#endif
#ifdef WTPC_HAS_NEON
WTPC_FWD_HORIZ(fwd_horiz_neon, simdn_f, SIMDN_W, simdn)
WTPC_FWD_VERT (fwd_vert_neon, simdn_f, SIMDN_W, simdn)
WTPC_INV_HORIZ(inv_horiz_neon, simdn_f, SIMDN_W, simdn)
WTPC_INV_VERT (inv_vert_neon, simdn_f, SIMDN_W, simdn)