pixo 0.4.1

A minimal-dependency, high-performance image compression library
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
//! Discrete Cosine Transform (DCT) implementation for JPEG.
//!
//! Provides both floating-point and fixed-point (integer) implementations of the
//! AAN (Arai-Agui-Nakajima) fast DCT algorithm for efficiency.
//! The AAN algorithm uses only 5 multiplications and 29 additions per 8-point DCT,
//! compared to 64 multiplications in the naive approach.
//!
//! The integer DCT matches libjpeg's jfdctint.c for consistent results with
//! standard JPEG decoders and slightly better compression characteristics.
//!
//! On ARM64, NEON SIMD is used to process multiple rows/columns in parallel.

use std::f32::consts::{FRAC_1_SQRT_2, PI};

#[cfg(target_arch = "aarch64")]
use std::arch::aarch64::*;

// =============================================================================
// Fixed-point (Integer) DCT Implementation
// =============================================================================
//
// Based on libjpeg's jfdctint.c - uses 13-bit fixed-point arithmetic.
// This produces coefficients that match the JPEG standard more precisely
// and can result in better compression due to more predictable coefficient
// distributions.

/// Fixed-point scale factor (13 bits of fractional precision, like libjpeg)
const CONST_BITS: i32 = 13;
const PASS1_BITS: i32 = 2;

#[inline(always)]
fn fix_mul(a: i32, b: i32) -> i32 {
    ((a as i64 * b as i64) >> CONST_BITS) as i32
}

/// Fixed-point constants for the DCT (scaled by 2^13)
/// These match libjpeg's jfdctint.c exactly
const FIX_0_298631336: i32 = 2446; // FIX(0.298631336)
const FIX_0_390180644: i32 = 3196; // FIX(0.390180644)
const FIX_0_541196100: i32 = 4433; // FIX(0.541196100)
const FIX_0_765366865: i32 = 6270; // FIX(0.765366865)
const FIX_0_899976223: i32 = 7373; // FIX(0.899976223)
const FIX_1_175875602: i32 = 9633; // FIX(1.175875602)
const FIX_1_501321110: i32 = 12299; // FIX(1.501321110)
const FIX_1_847759065: i32 = 15137; // FIX(1.847759065)
const FIX_1_961570560: i32 = 16069; // FIX(1.961570560)
const FIX_2_053119869: i32 = 16819; // FIX(2.053119869)
const FIX_2_562915447: i32 = 20995; // FIX(2.562915447)
const FIX_3_072711026: i32 = 25172; // FIX(3.072711026)

/// Perform 2D DCT on an 8x8 block using fixed-point (integer) AAN algorithm.
///
/// This matches libjpeg's jfdctint.c implementation for compatibility with
/// standard JPEG decoders. Input values should be level-shifted (-128 for 8-bit).
///
/// # Arguments
/// * `block` - 64 pixel values in row-major order, level-shifted to -128..127 range
///
/// # Returns
/// 64 DCT coefficients ready for quantization
pub fn dct_2d_integer(block: &[i16; 64]) -> [i32; 64] {
    let mut workspace = [0i32; 64];

    // Pass 1: process rows
    for row in 0..8 {
        let row_offset = row * 8;

        // Load input row and convert to i32
        let d0 = block[row_offset] as i32;
        let d1 = block[row_offset + 1] as i32;
        let d2 = block[row_offset + 2] as i32;
        let d3 = block[row_offset + 3] as i32;
        let d4 = block[row_offset + 4] as i32;
        let d5 = block[row_offset + 5] as i32;
        let d6 = block[row_offset + 6] as i32;
        let d7 = block[row_offset + 7] as i32;

        // Even part per LL&M figure 1 --- note that published figure is faulty;
        // rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
        let tmp0 = d0 + d7;
        let tmp1 = d1 + d6;
        let tmp2 = d2 + d5;
        let tmp3 = d3 + d4;

        let tmp10 = tmp0 + tmp3;
        let tmp12 = tmp0 - tmp3;
        let tmp11 = tmp1 + tmp2;
        let tmp13 = tmp1 - tmp2;

        let tmp0 = d0 - d7;
        let tmp1 = d1 - d6;
        let tmp2 = d2 - d5;
        let tmp3 = d3 - d4;

        // Apply unsigned->signed conversion
        workspace[row_offset] = (tmp10 + tmp11) << PASS1_BITS;
        workspace[row_offset + 4] = (tmp10 - tmp11) << PASS1_BITS;

        let z1 = fix_mul(tmp12 + tmp13, FIX_0_541196100);
        workspace[row_offset + 2] = z1 + fix_mul(tmp12, FIX_0_765366865);
        workspace[row_offset + 6] = z1 - fix_mul(tmp13, FIX_1_847759065);

        // Odd part per figure 8 --- note paper omits factor of sqrt(2).
        let tmp10 = tmp0 + tmp3;
        let tmp11 = tmp1 + tmp2;
        let tmp12 = tmp0 + tmp2;
        let tmp13 = tmp1 + tmp3;
        let z1 = fix_mul(tmp12 + tmp13, FIX_1_175875602);

        let tmp0 = fix_mul(tmp0, FIX_1_501321110);
        let tmp1 = fix_mul(tmp1, FIX_3_072711026);
        let tmp2 = fix_mul(tmp2, FIX_2_053119869);
        let tmp3 = fix_mul(tmp3, FIX_0_298631336);
        let tmp10 = fix_mul(tmp10, -FIX_0_899976223);
        let tmp11 = fix_mul(tmp11, -FIX_2_562915447);
        let tmp12 = fix_mul(tmp12, -FIX_0_390180644) + z1;
        let tmp13 = fix_mul(tmp13, -FIX_1_961570560) + z1;

        workspace[row_offset + 1] = tmp0 + tmp10 + tmp12;
        workspace[row_offset + 3] = tmp1 + tmp11 + tmp13;
        workspace[row_offset + 5] = tmp2 + tmp11 + tmp12;
        workspace[row_offset + 7] = tmp3 + tmp10 + tmp13;
    }

    // Pass 2: process columns
    let mut result = [0i32; 64];
    for col in 0..8 {
        let d0 = workspace[col];
        let d1 = workspace[col + 8];
        let d2 = workspace[col + 16];
        let d3 = workspace[col + 24];
        let d4 = workspace[col + 32];
        let d5 = workspace[col + 40];
        let d6 = workspace[col + 48];
        let d7 = workspace[col + 56];

        // Even part
        let tmp0 = d0 + d7;
        let tmp1 = d1 + d6;
        let tmp2 = d2 + d5;
        let tmp3 = d3 + d4;

        let tmp10 = tmp0 + tmp3;
        let tmp12 = tmp0 - tmp3;
        let tmp11 = tmp1 + tmp2;
        let tmp13 = tmp1 - tmp2;

        let tmp0 = d0 - d7;
        let tmp1 = d1 - d6;
        let tmp2 = d2 - d5;
        let tmp3 = d3 - d4;

        // Final output stage: descale and output
        // We need to descale by PASS1_BITS + CONST_BITS - 3 (the 3 is for the 8x8 normalization)
        let descale = PASS1_BITS + 3;
        result[col] = (tmp10 + tmp11 + (1 << (descale - 1))) >> descale;
        result[col + 32] = (tmp10 - tmp11 + (1 << (descale - 1))) >> descale;

        let z1 = fix_mul(tmp12 + tmp13, FIX_0_541196100);
        result[col + 16] = (z1 + fix_mul(tmp12, FIX_0_765366865) + (1 << (descale - 1))) >> descale;
        result[col + 48] = (z1 - fix_mul(tmp13, FIX_1_847759065) + (1 << (descale - 1))) >> descale;

        // Odd part
        let tmp10 = tmp0 + tmp3;
        let tmp11 = tmp1 + tmp2;
        let tmp12 = tmp0 + tmp2;
        let tmp13 = tmp1 + tmp3;
        let z1 = fix_mul(tmp12 + tmp13, FIX_1_175875602);

        let tmp0 = fix_mul(tmp0, FIX_1_501321110);
        let tmp1 = fix_mul(tmp1, FIX_3_072711026);
        let tmp2 = fix_mul(tmp2, FIX_2_053119869);
        let tmp3 = fix_mul(tmp3, FIX_0_298631336);
        let tmp10 = fix_mul(tmp10, -FIX_0_899976223);
        let tmp11 = fix_mul(tmp11, -FIX_2_562915447);
        let tmp12 = fix_mul(tmp12, -FIX_0_390180644) + z1;
        let tmp13 = fix_mul(tmp13, -FIX_1_961570560) + z1;

        result[col + 8] = (tmp0 + tmp10 + tmp12 + (1 << (descale - 1))) >> descale;
        result[col + 24] = (tmp1 + tmp11 + tmp13 + (1 << (descale - 1))) >> descale;
        result[col + 40] = (tmp2 + tmp11 + tmp12 + (1 << (descale - 1))) >> descale;
        result[col + 56] = (tmp3 + tmp10 + tmp13 + (1 << (descale - 1))) >> descale;
    }

    result
}

// =============================================================================
// ARM64 NEON DCT Implementation
// =============================================================================
//
// Processes all 8 rows/columns in parallel using NEON SIMD for ~2x speedup on Apple Silicon.
// This implementation keeps values in NEON registers throughout the computation.

/// Perform 2D DCT on an 8x8 block using NEON SIMD acceleration.
///
/// This provides significant speedup on ARM64 processors by processing
/// all 8 elements of a row/column in parallel using 128-bit NEON registers.
#[cfg(target_arch = "aarch64")]
pub fn dct_2d_integer_neon(block: &[i16; 64]) -> [i32; 64] {
    // Safety: NEON is always available on aarch64
    unsafe { dct_2d_integer_neon_impl(block) }
}

#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
unsafe fn dct_2d_integer_neon_impl(block: &[i16; 64]) -> [i32; 64] {
    let mut workspace = [0i32; 64];

    // Pass 1: Process all 8 rows using NEON
    // Each row is processed keeping values in registers as much as possible
    for row in 0..8 {
        let offset = row * 8;

        // Load the row as 8 x i16, convert to 2 x 4 x i32
        let row_s16 = vld1q_s16(block[offset..].as_ptr());
        let lo = vmovl_s16(vget_low_s16(row_s16));
        let hi = vmovl_high_s16(row_s16);

        // Process this row using NEON butterfly operations
        let result = dct_row_neon_vectorized(lo, hi);

        // Store result
        vst1q_s32(workspace[offset..].as_mut_ptr(), result.0);
        vst1q_s32(workspace[offset + 4..].as_mut_ptr(), result.1);
    }

    // Pass 2: Process columns using NEON with transpose
    let mut result = [0i32; 64];

    // Load workspace into 8 vectors (one per row)
    let mut rows: [int32x4x2_t; 8] = std::mem::zeroed();
    for row in 0..8 {
        rows[row].0 = vld1q_s32(workspace[row * 8..].as_ptr());
        rows[row].1 = vld1q_s32(workspace[row * 8 + 4..].as_ptr());
    }

    // Transpose the 8x8 matrix in-place using NEON
    // This allows us to process columns as rows
    let transposed = transpose_8x8_neon(&rows);

    // Process each transposed row (which is a column) using the same DCT
    for col in 0..8 {
        let col_result = dct_column_neon_vectorized(transposed[col].0, transposed[col].1);

        // Store back - need to distribute to result positions
        // col_result[0..8] goes to result[col], result[col+8], ..., result[col+56]
        let mut temp = [0i32; 8];
        vst1q_s32(temp[0..4].as_mut_ptr(), col_result.0);
        vst1q_s32(temp[4..8].as_mut_ptr(), col_result.1);

        for row in 0..8 {
            result[row * 8 + col] = temp[row];
        }
    }

    result
}

/// Process one DCT row keeping values in NEON registers.
/// Returns (low 4 coefficients, high 4 coefficients).
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
#[inline]
unsafe fn dct_row_neon_vectorized(lo: int32x4_t, hi: int32x4_t) -> (int32x4_t, int32x4_t) {
    // Extract d0..d7 - we need to work with individual values for the butterfly
    // but we'll use NEON for the heavy lifting

    // Reverse hi to get d7, d6, d5, d4
    let hi_rev = vrev64q_s32(hi);
    let hi_rev = vextq_s32(hi_rev, hi_rev, 2);

    // Even part: tmp0 = d0+d7, tmp1 = d1+d6, tmp2 = d2+d5, tmp3 = d3+d4
    let even_sum = vaddq_s32(lo, hi_rev); // [d0+d7, d1+d6, d2+d5, d3+d4]
    let even_diff = vsubq_s32(lo, hi_rev); // [d0-d7, d1-d6, d2-d5, d3-d4]

    // tmp10 = tmp0 + tmp3 = (d0+d7) + (d3+d4)
    // tmp11 = tmp1 + tmp2 = (d1+d6) + (d2+d5)
    // tmp12 = tmp0 - tmp3 = (d0+d7) - (d3+d4)
    // tmp13 = tmp1 - tmp2 = (d1+d6) - (d2+d5)

    let tmp0 = vgetq_lane_s32(even_sum, 0);
    let tmp1 = vgetq_lane_s32(even_sum, 1);
    let tmp2 = vgetq_lane_s32(even_sum, 2);
    let tmp3 = vgetq_lane_s32(even_sum, 3);

    let tmp10 = tmp0 + tmp3;
    let tmp11 = tmp1 + tmp2;
    let tmp12 = tmp0 - tmp3;
    let tmp13 = tmp1 - tmp2;

    let tmp0_odd = vgetq_lane_s32(even_diff, 0);
    let tmp1_odd = vgetq_lane_s32(even_diff, 1);
    let tmp2_odd = vgetq_lane_s32(even_diff, 2);
    let tmp3_odd = vgetq_lane_s32(even_diff, 3);

    // Compute outputs
    let out0 = (tmp10 + tmp11) << PASS1_BITS;
    let out4 = (tmp10 - tmp11) << PASS1_BITS;

    let z1 = fix_mul(tmp12 + tmp13, FIX_0_541196100);
    let out2 = z1 + fix_mul(tmp12, FIX_0_765366865);
    let out6 = z1 - fix_mul(tmp13, FIX_1_847759065);

    // Odd part
    let tmp10_o = tmp0_odd + tmp3_odd;
    let tmp11_o = tmp1_odd + tmp2_odd;
    let tmp12_o = tmp0_odd + tmp2_odd;
    let tmp13_o = tmp1_odd + tmp3_odd;
    let z1_o = fix_mul(tmp12_o + tmp13_o, FIX_1_175875602);

    let tmp0_m = fix_mul(tmp0_odd, FIX_1_501321110);
    let tmp1_m = fix_mul(tmp1_odd, FIX_3_072711026);
    let tmp2_m = fix_mul(tmp2_odd, FIX_2_053119869);
    let tmp3_m = fix_mul(tmp3_odd, FIX_0_298631336);
    let tmp10_m = fix_mul(tmp10_o, -FIX_0_899976223);
    let tmp11_m = fix_mul(tmp11_o, -FIX_2_562915447);
    let tmp12_m = fix_mul(tmp12_o, -FIX_0_390180644) + z1_o;
    let tmp13_m = fix_mul(tmp13_o, -FIX_1_961570560) + z1_o;

    let out1 = tmp0_m + tmp10_m + tmp12_m;
    let out3 = tmp1_m + tmp11_m + tmp13_m;
    let out5 = tmp2_m + tmp11_m + tmp12_m;
    let out7 = tmp3_m + tmp10_m + tmp13_m;

    // Pack into NEON vectors
    let result_lo = vsetq_lane_s32(out0, vdupq_n_s32(0), 0);
    let result_lo = vsetq_lane_s32(out1, result_lo, 1);
    let result_lo = vsetq_lane_s32(out2, result_lo, 2);
    let result_lo = vsetq_lane_s32(out3, result_lo, 3);

    let result_hi = vsetq_lane_s32(out4, vdupq_n_s32(0), 0);
    let result_hi = vsetq_lane_s32(out5, result_hi, 1);
    let result_hi = vsetq_lane_s32(out6, result_hi, 2);
    let result_hi = vsetq_lane_s32(out7, result_hi, 3);

    (result_lo, result_hi)
}

/// Process one DCT column with descaling.
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
#[inline]
unsafe fn dct_column_neon_vectorized(lo: int32x4_t, hi: int32x4_t) -> (int32x4_t, int32x4_t) {
    // Similar to row but with descaling at the end
    let hi_rev = vrev64q_s32(hi);
    let hi_rev = vextq_s32(hi_rev, hi_rev, 2);

    let even_sum = vaddq_s32(lo, hi_rev);
    let even_diff = vsubq_s32(lo, hi_rev);

    let tmp0 = vgetq_lane_s32(even_sum, 0);
    let tmp1 = vgetq_lane_s32(even_sum, 1);
    let tmp2 = vgetq_lane_s32(even_sum, 2);
    let tmp3 = vgetq_lane_s32(even_sum, 3);

    let tmp10 = tmp0 + tmp3;
    let tmp11 = tmp1 + tmp2;
    let tmp12 = tmp0 - tmp3;
    let tmp13 = tmp1 - tmp2;

    let tmp0_odd = vgetq_lane_s32(even_diff, 0);
    let tmp1_odd = vgetq_lane_s32(even_diff, 1);
    let tmp2_odd = vgetq_lane_s32(even_diff, 2);
    let tmp3_odd = vgetq_lane_s32(even_diff, 3);

    let descale = PASS1_BITS + 3;
    let round = 1 << (descale - 1);

    let out0 = (tmp10 + tmp11 + round) >> descale;
    let out4 = (tmp10 - tmp11 + round) >> descale;

    let z1 = fix_mul(tmp12 + tmp13, FIX_0_541196100);
    let out2 = (z1 + fix_mul(tmp12, FIX_0_765366865) + round) >> descale;
    let out6 = (z1 - fix_mul(tmp13, FIX_1_847759065) + round) >> descale;

    // Odd part
    let tmp10_o = tmp0_odd + tmp3_odd;
    let tmp11_o = tmp1_odd + tmp2_odd;
    let tmp12_o = tmp0_odd + tmp2_odd;
    let tmp13_o = tmp1_odd + tmp3_odd;
    let z1_o = fix_mul(tmp12_o + tmp13_o, FIX_1_175875602);

    let tmp0_m = fix_mul(tmp0_odd, FIX_1_501321110);
    let tmp1_m = fix_mul(tmp1_odd, FIX_3_072711026);
    let tmp2_m = fix_mul(tmp2_odd, FIX_2_053119869);
    let tmp3_m = fix_mul(tmp3_odd, FIX_0_298631336);
    let tmp10_m = fix_mul(tmp10_o, -FIX_0_899976223);
    let tmp11_m = fix_mul(tmp11_o, -FIX_2_562915447);
    let tmp12_m = fix_mul(tmp12_o, -FIX_0_390180644) + z1_o;
    let tmp13_m = fix_mul(tmp13_o, -FIX_1_961570560) + z1_o;

    let out1 = (tmp0_m + tmp10_m + tmp12_m + round) >> descale;
    let out3 = (tmp1_m + tmp11_m + tmp13_m + round) >> descale;
    let out5 = (tmp2_m + tmp11_m + tmp12_m + round) >> descale;
    let out7 = (tmp3_m + tmp10_m + tmp13_m + round) >> descale;

    // Pack into NEON vectors
    let result_lo = vsetq_lane_s32(out0, vdupq_n_s32(0), 0);
    let result_lo = vsetq_lane_s32(out1, result_lo, 1);
    let result_lo = vsetq_lane_s32(out2, result_lo, 2);
    let result_lo = vsetq_lane_s32(out3, result_lo, 3);

    let result_hi = vsetq_lane_s32(out4, vdupq_n_s32(0), 0);
    let result_hi = vsetq_lane_s32(out5, result_hi, 1);
    let result_hi = vsetq_lane_s32(out6, result_hi, 2);
    let result_hi = vsetq_lane_s32(out7, result_hi, 3);

    (result_lo, result_hi)
}

/// Transpose an 8x8 matrix of i32 values using NEON.
/// Input: 8 rows, each as (lo: int32x4_t, hi: int32x4_t)
/// Output: 8 columns in the same format
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
#[inline]
unsafe fn transpose_8x8_neon(rows: &[int32x4x2_t; 8]) -> [int32x4x2_t; 8] {
    // Transpose in 4x4 blocks, then combine
    // This is more efficient than element-by-element extraction

    // First, transpose the four 4x4 quadrants
    // Top-left quadrant (rows 0-3, cols 0-3)
    let tl0 = vtrn1q_s32(rows[0].0, rows[1].0);
    let tl1 = vtrn2q_s32(rows[0].0, rows[1].0);
    let tl2 = vtrn1q_s32(rows[2].0, rows[3].0);
    let tl3 = vtrn2q_s32(rows[2].0, rows[3].0);

    let tl_r0 = vreinterpretq_s32_s64(vtrn1q_s64(
        vreinterpretq_s64_s32(tl0),
        vreinterpretq_s64_s32(tl2),
    ));
    let tl_r1 = vreinterpretq_s32_s64(vtrn1q_s64(
        vreinterpretq_s64_s32(tl1),
        vreinterpretq_s64_s32(tl3),
    ));
    let tl_r2 = vreinterpretq_s32_s64(vtrn2q_s64(
        vreinterpretq_s64_s32(tl0),
        vreinterpretq_s64_s32(tl2),
    ));
    let tl_r3 = vreinterpretq_s32_s64(vtrn2q_s64(
        vreinterpretq_s64_s32(tl1),
        vreinterpretq_s64_s32(tl3),
    ));

    // Top-right quadrant (rows 0-3, cols 4-7)
    let tr0 = vtrn1q_s32(rows[0].1, rows[1].1);
    let tr1 = vtrn2q_s32(rows[0].1, rows[1].1);
    let tr2 = vtrn1q_s32(rows[2].1, rows[3].1);
    let tr3 = vtrn2q_s32(rows[2].1, rows[3].1);

    let tr_r0 = vreinterpretq_s32_s64(vtrn1q_s64(
        vreinterpretq_s64_s32(tr0),
        vreinterpretq_s64_s32(tr2),
    ));
    let tr_r1 = vreinterpretq_s32_s64(vtrn1q_s64(
        vreinterpretq_s64_s32(tr1),
        vreinterpretq_s64_s32(tr3),
    ));
    let tr_r2 = vreinterpretq_s32_s64(vtrn2q_s64(
        vreinterpretq_s64_s32(tr0),
        vreinterpretq_s64_s32(tr2),
    ));
    let tr_r3 = vreinterpretq_s32_s64(vtrn2q_s64(
        vreinterpretq_s64_s32(tr1),
        vreinterpretq_s64_s32(tr3),
    ));

    // Bottom-left quadrant (rows 4-7, cols 0-3)
    let bl0 = vtrn1q_s32(rows[4].0, rows[5].0);
    let bl1 = vtrn2q_s32(rows[4].0, rows[5].0);
    let bl2 = vtrn1q_s32(rows[6].0, rows[7].0);
    let bl3 = vtrn2q_s32(rows[6].0, rows[7].0);

    let bl_r0 = vreinterpretq_s32_s64(vtrn1q_s64(
        vreinterpretq_s64_s32(bl0),
        vreinterpretq_s64_s32(bl2),
    ));
    let bl_r1 = vreinterpretq_s32_s64(vtrn1q_s64(
        vreinterpretq_s64_s32(bl1),
        vreinterpretq_s64_s32(bl3),
    ));
    let bl_r2 = vreinterpretq_s32_s64(vtrn2q_s64(
        vreinterpretq_s64_s32(bl0),
        vreinterpretq_s64_s32(bl2),
    ));
    let bl_r3 = vreinterpretq_s32_s64(vtrn2q_s64(
        vreinterpretq_s64_s32(bl1),
        vreinterpretq_s64_s32(bl3),
    ));

    // Bottom-right quadrant (rows 4-7, cols 4-7)
    let br0 = vtrn1q_s32(rows[4].1, rows[5].1);
    let br1 = vtrn2q_s32(rows[4].1, rows[5].1);
    let br2 = vtrn1q_s32(rows[6].1, rows[7].1);
    let br3 = vtrn2q_s32(rows[6].1, rows[7].1);

    let br_r0 = vreinterpretq_s32_s64(vtrn1q_s64(
        vreinterpretq_s64_s32(br0),
        vreinterpretq_s64_s32(br2),
    ));
    let br_r1 = vreinterpretq_s32_s64(vtrn1q_s64(
        vreinterpretq_s64_s32(br1),
        vreinterpretq_s64_s32(br3),
    ));
    let br_r2 = vreinterpretq_s32_s64(vtrn2q_s64(
        vreinterpretq_s64_s32(br0),
        vreinterpretq_s64_s32(br2),
    ));
    let br_r3 = vreinterpretq_s32_s64(vtrn2q_s64(
        vreinterpretq_s64_s32(br1),
        vreinterpretq_s64_s32(br3),
    ));

    // Combine quadrants into output rows
    // Output row 0: tl_r0 (cols 0-3 from rows 0-3) + bl_r0 (cols 0-3 from rows 4-7)
    [
        int32x4x2_t(tl_r0, bl_r0),
        int32x4x2_t(tl_r1, bl_r1),
        int32x4x2_t(tl_r2, bl_r2),
        int32x4x2_t(tl_r3, bl_r3),
        int32x4x2_t(tr_r0, br_r0),
        int32x4x2_t(tr_r1, br_r1),
        int32x4x2_t(tr_r2, br_r2),
        int32x4x2_t(tr_r3, br_r3),
    ]
}

/// Select the best DCT implementation for the current platform.
/// On ARM64, uses NEON acceleration; on x86_64 with AVX2, uses AVX2 acceleration;
/// otherwise uses the scalar integer DCT.
///
/// Includes a fast path for constant blocks (common in flat image regions).
#[inline]
pub fn dct_2d_fast(block: &[i16; 64]) -> [i32; 64] {
    // Fast path: check if all values are the same (constant block)
    // This is common in flat image regions and provides ~2x speedup for those blocks
    let first = block[0];
    let is_constant = block[1..].iter().all(|&x| x == first);

    if is_constant {
        // For a constant block, only the DC coefficient is non-zero
        // DC = sum of all values = 64 * value, scaled by DCT normalization
        // After the two-pass DCT with PASS1_BITS scaling, DC = 8 * value
        let mut result = [0i32; 64];
        result[0] = (first as i32) * 8;
        return result;
    }

    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("avx2") {
            // Safety: we just checked for AVX2 support
            return unsafe { crate::simd::x86_64::dct_2d_avx2(block) };
        }
        dct_2d_integer(block)
    }

    #[cfg(target_arch = "aarch64")]
    {
        dct_2d_integer_neon(block)
    }

    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
    {
        dct_2d_integer(block)
    }
}

pub fn quantize_block_integer(dct: &[i32; 64], quant_table: &[u16; 64]) -> [i16; 64] {
    let mut result = [0i16; 64];
    for i in 0..64 {
        // Round to nearest by adding half the divisor before dividing
        let q = quant_table[i] as i32;
        let coef = dct[i];
        if coef >= 0 {
            result[i] = ((coef + (q >> 1)) / q) as i16;
        } else {
            result[i] = ((coef - (q >> 1)) / q) as i16;
        }
    }
    result
}

// =============================================================================
// Floating-point DCT Implementation (original)
// =============================================================================

// AAN DCT constants - precomputed trigonometric values
// These are the scale factors for the AAN algorithm
const A1: f32 = FRAC_1_SQRT_2; // cos(4*pi/16) = 1/sqrt(2)
const A2: f32 = 0.541_196_1; // cos(6*pi/16) - cos(2*pi/16)
const A3: f32 = FRAC_1_SQRT_2; // cos(4*pi/16) = 1/sqrt(2)
const A4: f32 = 1.306_562_9; // cos(2*pi/16) + cos(6*pi/16)
const A5: f32 = 0.382_683_43; // cos(6*pi/16)

// Post-scaling factors for the AAN algorithm to produce correctly normalized DCT output
// These are: s[k] = 1/(4 * c[k]) where c[k] = cos(k*pi/16) for k > 0, c[0] = 1/sqrt(2)
const S: [f32; 8] = [
    0.353_553_4, // 1/(2*sqrt(2))
    0.254_897_8, // 1/(4*cos(pi/16))
    0.270_598_1, // 1/(4*cos(2*pi/16))
    0.300_672_4, // 1/(4*cos(3*pi/16))
    0.353_553_4, // 1/(4*cos(4*pi/16)) = 1/(2*sqrt(2))
    0.449_988_1, // 1/(4*cos(5*pi/16))
    0.653_281_5, // 1/(4*cos(6*pi/16))
    1.281_457_8, // 1/(4*cos(7*pi/16))
];

/// Perform 2D DCT on an 8x8 block using AAN fast DCT algorithm.
///
/// Uses the separable property: 2D DCT = 1D DCT on rows, then 1D DCT on columns.
/// Each 1D DCT uses the AAN algorithm with only 5 multiplications.
pub fn dct_2d(block: &[f32; 64]) -> [f32; 64] {
    let mut temp = [0.0f32; 64];
    let mut result = [0.0f32; 64];

    // 1D DCT on rows using AAN
    for row in 0..8 {
        let row_start = row * 8;
        let mut row_data = [0.0f32; 8];
        row_data.copy_from_slice(&block[row_start..row_start + 8]);
        aan_dct_1d(&mut row_data);
        temp[row_start..row_start + 8].copy_from_slice(&row_data);
    }

    // 1D DCT on columns using AAN
    for col in 0..8 {
        let mut col_data = [0.0f32; 8];

        for row in 0..8 {
            col_data[row] = temp[row * 8 + col];
        }

        aan_dct_1d(&mut col_data);

        for row in 0..8 {
            result[row * 8 + col] = col_data[row];
        }
    }

    result
}

/// Perform 1D DCT on 8 values using the AAN algorithm.
///
/// The AAN algorithm uses only 5 multiplications and 29 additions,
/// compared to 64 multiplications in the naive O(n²) approach.
/// Based on: Arai, Agui, and Nakajima, "A Fast DCT-SQ Scheme for Images", 1988.
#[inline]
fn aan_dct_1d(data: &mut [f32; 8]) {
    // Stage 1: Initial butterfly operations
    let tmp0 = data[0] + data[7];
    let tmp7 = data[0] - data[7];
    let tmp1 = data[1] + data[6];
    let tmp6 = data[1] - data[6];
    let tmp2 = data[2] + data[5];
    let tmp5 = data[2] - data[5];
    let tmp3 = data[3] + data[4];
    let tmp4 = data[3] - data[4];

    // Stage 2: Even part - process tmp0, tmp1, tmp2, tmp3
    let tmp10 = tmp0 + tmp3;
    let tmp13 = tmp0 - tmp3;
    let tmp11 = tmp1 + tmp2;
    let tmp12 = tmp1 - tmp2;

    data[0] = tmp10 + tmp11;
    data[4] = tmp10 - tmp11;

    // Rotation for indices 2 and 6
    let z1 = (tmp12 + tmp13) * A1; // A1 = cos(4*pi/16)
    data[2] = tmp13 + z1;
    data[6] = tmp13 - z1;

    // Stage 3: Odd part - process tmp4, tmp5, tmp6, tmp7
    let tmp10 = tmp4 + tmp5;
    let tmp11 = tmp5 + tmp6;
    let tmp12 = tmp6 + tmp7;

    // The rotator is modified from the standard AAN algorithm
    // to handle the odd part correctly
    let z5 = (tmp10 - tmp12) * A5; // A5 = cos(6*pi/16)
    let z2 = tmp10 * A2 + z5; // A2 = cos(6*pi/16) - cos(2*pi/16)
    let z4 = tmp12 * A4 + z5; // A4 = cos(2*pi/16) + cos(6*pi/16)
    let z3 = tmp11 * A3; // A3 = cos(4*pi/16)

    let z11 = tmp7 + z3;
    let z13 = tmp7 - z3;

    data[5] = z13 + z2;
    data[3] = z13 - z2;
    data[1] = z11 + z4;
    data[7] = z11 - z4;

    // Apply post-scaling to get properly normalized DCT coefficients
    for i in 0..8 {
        data[i] *= S[i];
    }
}

// Keep the old implementation for reference and for the IDCT
/// Precomputed cosine values for IDCT.
/// cos_table[i][j] = cos((2*i + 1) * j * PI / 16)
const COS_TABLE: [[f32; 8]; 8] = precompute_cos_table();

/// Precompute the cosine table at compile time.
const fn precompute_cos_table() -> [[f32; 8]; 8] {
    let mut table = [[0.0f32; 8]; 8];
    let mut i = 0;
    while i < 8 {
        let mut j = 0;
        while j < 8 {
            let angle = ((2 * i + 1) * j) as f32 * PI / 16.0;
            table[i][j] = cos_approx(angle);
            j += 1;
        }
        i += 1;
    }
    table
}

/// Approximate cosine for const fn (Taylor series).
const fn cos_approx(x: f32) -> f32 {
    let mut x = x;
    while x > PI {
        x -= 2.0 * PI;
    }
    while x < -PI {
        x += 2.0 * PI;
    }

    let x2 = x * x;
    let x4 = x2 * x2;
    let x6 = x4 * x2;
    let x8 = x4 * x4;

    1.0 - x2 / 2.0 + x4 / 24.0 - x6 / 720.0 + x8 / 40320.0
}

/// Normalization factors for IDCT.
/// alpha(0) = 1/sqrt(2), alpha(k) = 1 for k > 0
const ALPHA: [f32; 8] = [
    FRAC_1_SQRT_2, // 1/sqrt(2)
    1.0,
    1.0,
    1.0,
    1.0,
    1.0,
    1.0,
    1.0,
];

/// Perform inverse 2D DCT on an 8x8 block.
#[allow(dead_code)]
pub fn idct_2d(block: &[f32; 64]) -> [f32; 64] {
    let mut temp = [0.0f32; 64];
    let mut result = [0.0f32; 64];

    // 1D IDCT on columns
    for col in 0..8 {
        let mut col_in = [0.0f32; 8];
        let mut col_out = [0.0f32; 8];

        for row in 0..8 {
            col_in[row] = block[row * 8 + col];
        }

        idct_1d(&col_in, &mut col_out);

        for row in 0..8 {
            temp[row * 8 + col] = col_out[row];
        }
    }

    // 1D IDCT on rows
    for row in 0..8 {
        let row_start = row * 8;
        idct_1d(
            &temp[row_start..row_start + 8],
            &mut result[row_start..row_start + 8],
        );
    }

    result
}

fn idct_1d(input: &[f32], output: &mut [f32]) {
    debug_assert_eq!(input.len(), 8);
    debug_assert_eq!(output.len(), 8);

    for n in 0..8 {
        let mut sum = 0.0f32;
        for k in 0..8 {
            sum += ALPHA[k] * input[k] * COS_TABLE[n][k];
        }
        output[n] = 0.5 * sum;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dct_dc_component() {
        // All zeros should give all zeros
        let block = [0.0f32; 64];
        let result = dct_2d(&block);
        for &val in &result {
            assert!((val).abs() < 0.001);
        }
    }

    #[test]
    fn test_dct_constant_block() {
        // Constant block: DC component should be non-zero, AC should be small
        let block = [100.0f32; 64];
        let result = dct_2d(&block);

        // DC component (index 0) should be large
        assert!(result[0].abs() > 100.0);

        // AC components should be relatively small compared to DC
        // (some numerical error is expected with the const fn cos approximation)
        for &val in result.iter().skip(1) {
            assert!(val.abs() < 5.0, "AC component too large: {val}");
        }
    }

    #[test]
    fn test_dct_idct_roundtrip() {
        let mut block = [0.0f32; 64];
        for (i, item) in block.iter_mut().enumerate() {
            *item = (i as f32 * 4.0) - 128.0;
        }

        let dct = dct_2d(&block);
        let recovered = idct_2d(&dct);

        // Allow some numerical error due to const fn cos approximation
        for i in 0..64 {
            assert!(
                (block[i] - recovered[i]).abs() < 5.0,
                "Mismatch at {}: {} vs {}",
                i,
                block[i],
                recovered[i]
            );
        }
    }

    #[test]
    fn test_cos_table_values() {
        // cos(0) = 1
        assert!((COS_TABLE[0][0] - 1.0).abs() < 0.0001);

        // cos(pi/4) = 1/sqrt(2) ≈ 0.707
        // This is cos((2*0 + 1) * 2 * PI / 16) = cos(PI/8)
        assert!((COS_TABLE[0][2] - (PI / 8.0).cos()).abs() < 0.001);
    }

    // ==========================================================================
    // Integer DCT Tests
    // ==========================================================================

    #[test]
    fn test_integer_dct_zeros() {
        // All zeros should give all zeros
        let block = [0i16; 64];
        let result = dct_2d_integer(&block);
        for &val in &result {
            assert_eq!(val, 0);
        }
    }

    #[test]
    fn test_integer_dct_constant_block() {
        // Constant block (level-shifted): DC should be large, AC should be zero/small
        let block = [100i16; 64]; // Represents 228 in original (100 + 128)
        let result = dct_2d_integer(&block);

        // DC component should be large and positive
        assert!(result[0] > 100, "DC too small: {}", result[0]);

        // AC components should be zero or very small for a constant block
        for (i, &val) in result.iter().enumerate().skip(1) {
            assert!(val.abs() <= 1, "AC component at {i} too large: {val}");
        }
    }

    #[test]
    fn test_integer_dct_energy_preservation() {
        // Test that integer DCT preserves energy reasonably
        // (The integer and float DCT use different algorithms with different scaling,
        // so we test properties rather than exact values)
        let mut block = [0i16; 64];

        // Create a gradient pattern - values in range -128..127
        for row in 0..8 {
            for col in 0..8 {
                let val = (row as i32 + col as i32) * 16 - 112;
                block[row * 8 + col] = val.clamp(-128, 127) as i16;
            }
        }

        let result = dct_2d_integer(&block);

        // DC coefficient should capture the average
        // For our gradient, average is around 0, so DC should be small
        assert!(
            result[0].abs() < 50,
            "DC coefficient unexpectedly large: {}",
            result[0]
        );

        // Low frequency AC coefficients should have most of the energy
        // for a smooth gradient
        let low_freq_energy: i64 = result[..16].iter().map(|&x| (x as i64).pow(2)).sum();
        let high_freq_energy: i64 = result[48..].iter().map(|&x| (x as i64).pow(2)).sum();

        assert!(
            low_freq_energy > high_freq_energy,
            "Low freq energy {low_freq_energy} should exceed high freq energy {high_freq_energy}"
        );
    }

    #[test]
    fn test_integer_quantize() {
        let mut block = [0i16; 64];
        block[0] = 100; // Level-shifted pixel value

        let dct = dct_2d_integer(&block);

        // Create a simple quantization table
        let mut quant = [16u16; 64];
        quant[0] = 16; // DC quantizer

        let quantized = quantize_block_integer(&dct, &quant);

        // DC should be quantized to a non-zero value
        assert!(quantized[0] != 0, "DC was quantized to zero");
    }

    // ==========================================================================
    // Extended DCT Tests for Coverage
    // ==========================================================================

    #[test]
    fn test_dct_2d_fast_matches_integer() {
        // dct_2d_fast should call dct_2d_integer on non-aarch64
        let mut block = [0i16; 64];
        for i in 0..64 {
            block[i] = ((i as i32 * 7) % 256 - 128) as i16;
        }

        let fast_result = dct_2d_fast(&block);
        let int_result = dct_2d_integer(&block);

        // On non-aarch64, these should be identical
        #[cfg(not(target_arch = "aarch64"))]
        {
            assert_eq!(fast_result, int_result);
        }

        // On aarch64, just verify it produces reasonable output
        #[cfg(target_arch = "aarch64")]
        {
            // Both should have similar DC coefficients
            assert!(
                (fast_result[0] - int_result[0]).abs() < 5,
                "DC mismatch: {} vs {}",
                fast_result[0],
                int_result[0]
            );
        }
    }

    #[test]
    fn test_dct_2d_fast_constant_block_shortcut() {
        // Test that the constant block shortcut in dct_2d_fast works correctly
        // When all pixels are the same value, only DC should be non-zero

        // Test with value 0
        let block_zero = [0i16; 64];
        let result = dct_2d_fast(&block_zero);
        assert_eq!(result[0], 0, "DC should be 0 for zero block");
        for (i, &val) in result.iter().enumerate().skip(1) {
            assert_eq!(val, 0, "AC component at {i} should be 0 for constant block");
        }

        // Test with positive constant value
        let block_pos = [50i16; 64];
        let result = dct_2d_fast(&block_pos);
        assert_eq!(
            result[0],
            50 * 8,
            "DC should be value * 8 for constant block"
        );
        for (i, &val) in result.iter().enumerate().skip(1) {
            assert_eq!(val, 0, "AC component at {i} should be 0 for constant block");
        }

        // Test with negative constant value
        let block_neg = [-30i16; 64];
        let result = dct_2d_fast(&block_neg);
        assert_eq!(
            result[0],
            -30 * 8,
            "DC should be value * 8 for negative constant"
        );
        for (i, &val) in result.iter().enumerate().skip(1) {
            assert_eq!(val, 0, "AC component at {i} should be 0 for constant block");
        }

        // Test with max value
        let block_max = [127i16; 64];
        let result = dct_2d_fast(&block_max);
        assert_eq!(
            result[0],
            127 * 8,
            "DC should be value * 8 for max constant"
        );

        // Test with min value
        let block_min = [-128i16; 64];
        let result = dct_2d_fast(&block_min);
        assert_eq!(
            result[0],
            -128 * 8,
            "DC should be value * 8 for min constant"
        );
    }

    #[test]
    fn test_dct_2d_fast_non_constant_block() {
        // Test that non-constant blocks don't take the shortcut
        // Use a gradient pattern that will produce significant AC components
        let mut block = [0i16; 64];
        for i in 0..64 {
            block[i] = (i as i16) - 32; // Range from -32 to 31
        }

        let result = dct_2d_fast(&block);

        // DC should be non-zero for this pattern
        // The sum of -32 to 31 is (-32 + 31) * 32 = -32, so DC should be small but present

        // AC components should be non-zero for a gradient
        let non_zero_ac = result.iter().skip(1).filter(|&&v| v != 0).count();
        assert!(
            non_zero_ac > 0,
            "Gradient block should have non-zero AC components"
        );
    }

    #[test]
    fn test_quantize_block_integer_negative_values() {
        // Test quantization with negative DCT coefficients
        let mut dct = [0i32; 64];
        dct[0] = 100;
        dct[1] = -50;
        dct[2] = 75;
        dct[3] = -25;

        let quant = [16u16; 64];
        let quantized = quantize_block_integer(&dct, &quant);

        // Positive: (100 + 8) / 16 = 6
        assert_eq!(quantized[0], 6);
        // Negative: (-50 - 8) / 16 = -3
        assert_eq!(quantized[1], -3);
        // Positive: (75 + 8) / 16 = 5
        assert_eq!(quantized[2], 5);
        // Negative: (-25 - 8) / 16 = -2
        assert_eq!(quantized[3], -2);
    }

    #[test]
    fn test_quantize_block_integer_various_quant_tables() {
        let dct = [100i32; 64];

        // Test with different quantization values
        let quant_low = [8u16; 64];
        let quant_high = [64u16; 64];

        let q_low = quantize_block_integer(&dct, &quant_low);
        let q_high = quantize_block_integer(&dct, &quant_high);

        // Lower quantizer = larger quantized value
        assert!(q_low[0] > q_high[0]);
        assert_eq!(q_low[0], (100 + 4) / 8); // 13
        assert_eq!(q_high[0], (100 + 32) / 64); // 2
    }

    #[test]
    fn test_quantize_block_integer_edge_values() {
        let mut dct = [0i32; 64];
        // Test with values that round exactly
        dct[0] = 16; // Should round to 1 with quant=16
        dct[1] = 8; // Edge case: exactly half
        dct[2] = 7; // Just under half

        let quant = [16u16; 64];
        let quantized = quantize_block_integer(&dct, &quant);

        // (16 + 8) / 16 = 1
        assert_eq!(quantized[0], 1);
        // (8 + 8) / 16 = 1
        assert_eq!(quantized[1], 1);
        // (7 + 8) / 16 = 0
        assert_eq!(quantized[2], 0);
    }

    #[test]
    fn test_integer_dct_checkerboard() {
        // Checkerboard pattern should produce high-frequency components
        let mut block = [0i16; 64];
        for row in 0..8 {
            for col in 0..8 {
                block[row * 8 + col] = if (row + col) % 2 == 0 { 100 } else { -100 };
            }
        }

        let result = dct_2d_integer(&block);

        // Checkerboard has high frequency content, so AC components should be significant
        // The (7,7) frequency component should be largest for perfect checkerboard
        let ac_energy: i64 = result[1..].iter().map(|&x| (x as i64).pow(2)).sum();
        assert!(ac_energy > 0, "Checkerboard should have AC energy");
    }

    #[test]
    fn test_integer_dct_horizontal_stripes() {
        // Horizontal stripes pattern
        let mut block = [0i16; 64];
        for row in 0..8 {
            let val = if row % 2 == 0 { 100i16 } else { -100 };
            for col in 0..8 {
                block[row * 8 + col] = val;
            }
        }

        let result = dct_2d_integer(&block);

        // DC should be near zero (equal amounts of +100 and -100)
        assert!(result[0].abs() < 10, "DC should be small: {}", result[0]);
    }

    #[test]
    fn test_integer_dct_vertical_stripes() {
        // Vertical stripes pattern
        let mut block = [0i16; 64];
        for row in 0..8 {
            for col in 0..8 {
                block[row * 8 + col] = if col % 2 == 0 { 100 } else { -100 };
            }
        }

        let result = dct_2d_integer(&block);

        // DC should be near zero
        assert!(result[0].abs() < 10, "DC should be small: {}", result[0]);
    }

    #[test]
    fn test_integer_dct_extreme_values() {
        // Test with maximum positive values
        let block_max = [127i16; 64];
        let result_max = dct_2d_integer(&block_max);
        assert!(
            result_max[0] > 0,
            "DC should be positive for positive block"
        );

        // Test with maximum negative values
        let block_min = [-128i16; 64];
        let result_min = dct_2d_integer(&block_min);
        assert!(
            result_min[0] < 0,
            "DC should be negative for negative block"
        );
    }

    #[test]
    fn test_float_dct_gradient() {
        // Test float DCT with a gradient
        let mut block = [0.0f32; 64];
        for row in 0..8 {
            for col in 0..8 {
                block[row * 8 + col] = (row + col) as f32 * 10.0;
            }
        }

        let result = dct_2d(&block);

        // DC should capture the average (sum / 64)
        let _avg: f32 = block.iter().sum::<f32>() / 64.0;
        // DC coefficient is scaled, so just verify it's proportional
        assert!(result[0] > 0.0, "DC should be positive");
    }

    #[test]
    fn test_float_dct_single_pixel() {
        // Only one non-zero pixel
        let mut block = [0.0f32; 64];
        block[0] = 100.0;

        let result = dct_2d(&block);

        // Should have energy spread across frequencies
        let total_energy: f32 = result.iter().map(|&x| x * x).sum();
        assert!(total_energy > 0.0, "Should have energy");
    }

    #[test]
    fn test_idct_2d_dc_only() {
        // IDCT of a block with only DC coefficient
        let mut block = [0.0f32; 64];
        block[0] = 100.0;

        let result = idct_2d(&block);

        // All pixels should be approximately equal (DC spreads evenly)
        let avg = result.iter().sum::<f32>() / 64.0;
        for &val in &result {
            assert!(
                (val - avg).abs() < 1.0,
                "DC-only IDCT should produce uniform values"
            );
        }
    }

    #[test]
    fn test_cos_table_symmetry() {
        // Verify cos table has expected symmetry properties
        for i in 0..8 {
            // cos(0) for any row should be 1
            assert!(
                (COS_TABLE[i][0] - 1.0).abs() < 0.01,
                "COS_TABLE[{i}][0] should be ~1.0"
            );
        }
    }

    #[test]
    fn test_alpha_values() {
        // First element should be 1/sqrt(2)
        assert!((ALPHA[0] - FRAC_1_SQRT_2).abs() < 0.0001);
        // All other elements should be 1.0
        for &a in &ALPHA[1..] {
            assert!((a - 1.0).abs() < 0.0001);
        }
    }

    #[test]
    fn test_aan_scale_factors() {
        // Verify S array has positive values
        for &s in &S {
            assert!(s > 0.0, "Scale factor should be positive");
            assert!(s < 2.0, "Scale factor should be less than 2");
        }
    }

    #[test]
    fn test_fix_mul_basic() {
        // Test the fixed-point multiplication helper
        let result = fix_mul(8192, 8192); // Both are 1.0 in 13-bit fixed point
        assert_eq!(result, 8192); // 1.0 * 1.0 = 1.0

        let result = fix_mul(16384, 8192); // 2.0 * 1.0
        assert_eq!(result, 16384); // = 2.0

        let result = fix_mul(8192, 4096); // 1.0 * 0.5
        assert_eq!(result, 4096); // = 0.5
    }
}