rav2d 0.3.0

AV2 video decoder in Rust
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
use crate::headers::FilmGrainData;
use crate::intops::iclip;
use crate::tables::GAUSSIAN_SEQUENCE;

pub const GRAIN_WIDTH: usize = 82;
pub const GRAIN_HEIGHT: usize = 73;
pub const SUB_GRAIN_WIDTH: usize = 44;
pub const SUB_GRAIN_HEIGHT: usize = 38;

pub fn get_random_number(bits: u32, state: &mut u32) -> u32 {
    let r = *state;
    let bit = (r ^ (r >> 1) ^ (r >> 3) ^ (r >> 12)) & 1;
    *state = (r >> 1) | (bit << 15);
    (*state >> (16 - bits)) & ((1 << bits) - 1)
}

pub fn round2(x: i32, shift: u32) -> i32 {
    (x + ((1 << shift) >> 1)) >> shift
}

pub fn generate_scaling_8bpc(points: &[[u8; 2]], scaling: &mut [u8; 256]) {
    let num = points.len();
    if num == 0 {
        scaling.fill(0);
        return;
    }

    let first_x = points[0][0] as usize;
    scaling[..first_x].fill(points[0][1]);

    for i in 0..num - 1 {
        let bx = points[i][0] as i32;
        let by = points[i][1] as i32;
        let ex = points[i + 1][0] as i32;
        let ey = points[i + 1][1] as i32;
        let dx = ex - bx;
        let dy = ey - by;
        debug_assert!(dx > 0);
        let delta = dy * ((0x10000 + (dx >> 1)) / dx);
        let mut d = 0x8000i32;
        for x in 0..dx as usize {
            scaling[bx as usize + x] = (by + (d >> 16)) as u8;
            d += delta;
        }
    }

    let n = points[num - 1][0] as usize;
    scaling[n..].fill(points[num - 1][1]);
}

pub fn generate_scaling_hbd(bitdepth: u32, points: &[[u8; 2]], scaling: &mut [u8]) {
    debug_assert!(bitdepth > 8);
    let shift_x = bitdepth - 8;
    let scaling_size = 1usize << bitdepth;
    let num = points.len();

    if num == 0 {
        scaling[..scaling_size].fill(0);
        return;
    }

    scaling[..(points[0][0] as usize) << shift_x].fill(points[0][1]);

    for i in 0..num - 1 {
        let bx = points[i][0] as i32;
        let by = points[i][1] as i32;
        let ex = points[i + 1][0] as i32;
        let ey = points[i + 1][1] as i32;
        let dx = ex - bx;
        let dy = ey - by;
        debug_assert!(dx > 0);
        let delta = dy * ((0x10000 + (dx >> 1)) / dx);
        let mut d = 0x8000i32;
        for x in 0..dx {
            scaling[((bx + x) << shift_x) as usize] = (by + (d >> 16)) as u8;
            d += delta;
        }
    }

    let n = (points[num - 1][0] as usize) << shift_x;
    scaling[n..scaling_size].fill(points[num - 1][1]);

    let pad = 1i32 << shift_x;
    let rnd = pad >> 1;
    for i in 0..num - 1 {
        let bx = (points[i][0] as i32) << shift_x;
        let ex = (points[i + 1][0] as i32) << shift_x;
        let dx = ex - bx;
        let mut x = 0;
        while x < dx {
            let base = scaling[(bx + x) as usize] as i32;
            let range = scaling[(bx + x + pad) as usize] as i32 - base;
            let mut r = rnd;
            for n in 1..pad {
                r += range;
                scaling[(bx + x + n) as usize] = (base + (r >> shift_x)) as u8;
            }
            x += pad;
        }
    }
}

pub fn generate_grain_y(
    buf: &mut [[i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
    data: &FilmGrainData,
    mut seed: u32,
) {
    let shift = 4 + data.grain_scale_shift;
    let grain_ctr = 128;
    let grain_min = -grain_ctr;
    let grain_max = grain_ctr - 1;

    for y in 0..GRAIN_HEIGHT {
        for x in 0..GRAIN_WIDTH {
            let value = get_random_number(11, &mut seed) as usize;
            buf[y][x] = round2(GAUSSIAN_SEQUENCE[value] as i32, shift as u32) as i16;
        }
    }

    let ar_pad = 3usize;
    let ar_lag = data.ar_coeff_lag as usize;

    for y in ar_pad..GRAIN_HEIGHT {
        for x in ar_pad..GRAIN_WIDTH - ar_pad {
            let coeff = &data.ar_coeffs[0];
            let mut sum = 0i32;
            let mut ci = 0usize;
            for dy in (y.wrapping_sub(ar_lag))..=y {
                let dx_start = x.wrapping_sub(ar_lag);
                let dx_end = if dy == y { x } else { x + ar_lag + 1 };
                for dx in dx_start..dx_end {
                    if dy == y && dx == x {
                        break;
                    }
                    sum += coeff[ci] as i32 * buf[dy][dx] as i32;
                    ci += 1;
                }
            }

            let grain = buf[y][x] as i32 + round2(sum, data.ar_coeff_shift as u32);
            buf[y][x] = iclip(grain, grain_min, grain_max) as i16;
        }
    }
}

pub fn generate_grain_uv(
    buf: &mut [[i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
    buf_y: &[[i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
    data: &FilmGrainData,
    mut seed: u32,
    uv: usize,
    subx: bool,
    suby: bool,
) {
    seed ^= if uv != 0 { 0x49d8 } else { 0xb524 };
    let shift = 4 + data.grain_scale_shift;
    let grain_ctr = 128;
    let grain_min = -grain_ctr;
    let grain_max = grain_ctr - 1;

    let chroma_w = if subx { SUB_GRAIN_WIDTH } else { GRAIN_WIDTH };
    let chroma_h = if suby { SUB_GRAIN_HEIGHT } else { GRAIN_HEIGHT };

    for y in 0..chroma_h {
        for x in 0..chroma_w {
            let value = get_random_number(11, &mut seed) as usize;
            buf[y][x] = round2(GAUSSIAN_SEQUENCE[value] as i32, shift as u32) as i16;
        }
    }

    let ar_pad = 3usize;
    let ar_lag = data.ar_coeff_lag as usize;
    let subx_i = subx as usize;
    let suby_i = suby as usize;

    for y in ar_pad..chroma_h {
        for x in ar_pad..chroma_w - ar_pad {
            let coeff = &data.ar_coeffs[1 + uv];
            let mut sum = 0i32;
            let mut ci = 0usize;
            'outer: for dy in (y.wrapping_sub(ar_lag))..=y {
                let dx_start = x.wrapping_sub(ar_lag);
                // Current row stops at (and includes) the center pixel dx==x,
                // which carries the luma-correlation term (dav2d: the dx==0,dy==0
                // case uses the final AR coeff for the luma contribution, then
                // breaks). Off rows span the full [-ar_lag, +ar_lag] window.
                let dx_end = if dy == y { x + 1 } else { x + ar_lag + 1 };
                for dx in dx_start..dx_end {
                    if dy == y && dx == x {
                        if data.num_points[0] > 0 {
                            let luma_x = ((x - ar_pad) << subx_i) + ar_pad;
                            let luma_y = ((y - ar_pad) << suby_i) + ar_pad;
                            let mut luma = 0i32;
                            for i in 0..=suby_i {
                                for j in 0..=subx_i {
                                    luma += buf_y[luma_y + i][luma_x + j] as i32;
                                }
                            }
                            luma = round2(luma, (subx_i + suby_i) as u32);
                            sum += luma * coeff[ci] as i32;
                        }
                        break 'outer;
                    }
                    sum += coeff[ci] as i32 * buf[dy][dx] as i32;
                    ci += 1;
                }
            }

            let grain = buf[y][x] as i32 + round2(sum, data.ar_coeff_shift as u32);
            buf[y][x] = iclip(grain, grain_min, grain_max) as i16;
        }
    }
}

pub fn sample_lut(
    grain_lut: &[[i16; GRAIN_WIDTH]],
    bs: usize,
    offsets: &[[[i32; 2]; 2]; 2],
    subx: usize,
    suby: usize,
    bx: usize,
    by: usize,
    x: usize,
    y: usize,
) -> i16 {
    let off = &offsets[bx][by];
    let offx = 3 + (2 >> subx) * (3 + off[1] as usize);
    let offy = 3 + (2 >> suby) * (3 + off[0] as usize);
    grain_lut[offy + y + (bs >> suby) * by][offx + x + (bs >> subx) * bx]
}

pub fn fgy_32x32xn_8bpc(
    dst: &mut [u8],
    src: &[u8],
    stride: usize,
    data: &FilmGrainData,
    in_seed: u32,
    pw: usize,
    scaling: &[u8; 256],
    grain_lut: &[[i16; GRAIN_WIDTH]],
    bh: i32,
    row_num: i32,
) {
    let rows = 1 + (data.overlap_flag && row_num > 0) as usize;
    let grain_ctr = 128;
    let grain_min = -grain_ctr;
    let grain_max = grain_ctr - 1;
    let bs = (16 << data.block_size) as usize;

    let (min_value, max_value) = if data.clip_to_restricted_range {
        (16i32, 235i32)
    } else {
        (0, 255)
    };

    let mut seed = [0u32; 2];
    for i in 0..rows {
        seed[i] = in_seed;
        seed[i] ^= ((((row_num - i as i32) * 37 + 178) & 0xFF) as u32) << 8;
        seed[i] ^= (((row_num - i as i32) * 173 + 105) & 0xFF) as u32;
    }

    let mut offsets = [[[0i32; 2]; 2]; 2];
    let w: [[i32; 2]; 2] = [[27, 17], [17, 27]];

    let mut bx = 0usize;
    while bx < pw {
        let bw = bs.min(pw - bx) as i32;

        if data.overlap_flag && bx > 0 {
            for i in 0..rows {
                for n in 0..2 {
                    offsets[1][i][n] = offsets[0][i][n];
                }
            }
        }

        for i in 0..rows {
            for n in 0..2 {
                offsets[0][i][n] = (((3 - data.block_size) as u32
                    * get_random_number(9, &mut seed[i]))
                    >> 6) as i32;
                for _ in 0..3 {
                    get_random_number(16, &mut seed[i]);
                }
            }
        }

        let ystart = if data.overlap_flag && row_num > 0 {
            2.min(bh)
        } else {
            0
        };
        let xstart = if data.overlap_flag && bx > 0 {
            2.min(bw)
        } else {
            0
        };

        for y in ystart..bh {
            for x in xstart..bw {
                let grain =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 0, 0, x as usize, y as usize) as i32;
                let si = y as usize * stride + x as usize + bx;
                let noise = round2(
                    scaling[src[si] as usize] as i32 * grain,
                    data.scaling_shift as u32,
                );
                dst[si] = iclip(src[si] as i32 + noise, min_value, max_value) as u8;
            }
            for x in 0..xstart {
                let grain =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 0, 0, x as usize, y as usize) as i32;
                let old =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 1, 0, x as usize, y as usize) as i32;
                let grain = iclip(
                    round2(old * w[x as usize][0] + grain * w[x as usize][1], 5),
                    grain_min,
                    grain_max,
                );
                let si = y as usize * stride + x as usize + bx;
                let noise = round2(
                    scaling[src[si] as usize] as i32 * grain,
                    data.scaling_shift as u32,
                );
                dst[si] = iclip(src[si] as i32 + noise, min_value, max_value) as u8;
            }
        }

        for y in 0..ystart {
            for x in xstart..bw {
                let grain =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 0, 0, x as usize, y as usize) as i32;
                let old =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 0, 1, x as usize, y as usize) as i32;
                let grain = iclip(
                    round2(old * w[y as usize][0] + grain * w[y as usize][1], 5),
                    grain_min,
                    grain_max,
                );
                let si = y as usize * stride + x as usize + bx;
                let noise = round2(
                    scaling[src[si] as usize] as i32 * grain,
                    data.scaling_shift as u32,
                );
                dst[si] = iclip(src[si] as i32 + noise, min_value, max_value) as u8;
            }
            for x in 0..xstart {
                let mut top =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 0, 1, x as usize, y as usize) as i32;
                let old =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 1, 1, x as usize, y as usize) as i32;
                top = iclip(
                    round2(old * w[x as usize][0] + top * w[x as usize][1], 5),
                    grain_min,
                    grain_max,
                );

                let mut grain =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 0, 0, x as usize, y as usize) as i32;
                let old2 =
                    sample_lut(grain_lut, bs, &offsets, 0, 0, 1, 0, x as usize, y as usize) as i32;
                grain = iclip(
                    round2(old2 * w[x as usize][0] + grain * w[x as usize][1], 5),
                    grain_min,
                    grain_max,
                );

                grain = iclip(
                    round2(top * w[y as usize][0] + grain * w[y as usize][1], 5),
                    grain_min,
                    grain_max,
                );
                let si = y as usize * stride + x as usize + bx;
                let noise = round2(
                    scaling[src[si] as usize] as i32 * grain,
                    data.scaling_shift as u32,
                );
                dst[si] = iclip(src[si] as i32 + noise, min_value, max_value) as u8;
            }
        }

        bx += bs;
    }
}

pub fn fguv_32x32xn_8bpc(
    dst: &mut [u8],
    src: &[u8],
    stride: usize,
    data: &FilmGrainData,
    in_seed: u32,
    pw: usize,
    scaling: &[u8; 256],
    grain_lut: &[[i16; GRAIN_WIDTH]],
    bh: i32,
    row_num: i32,
    luma_row: &[u8],
    luma_stride: usize,
    uv: usize,
    is_id: bool,
    sx: usize,
    sy: usize,
) {
    let rows = 1 + (data.overlap_flag && row_num > 0) as usize;
    let grain_ctr = 128;
    let grain_min = -grain_ctr;
    let grain_max = grain_ctr - 1;
    let bs = (16 << data.block_size) as usize;

    let (min_value, max_value) = if data.clip_to_restricted_range {
        (16i32, if is_id { 235 } else { 240 })
    } else {
        (0, 255)
    };

    let mut seed = [0u32; 2];
    for i in 0..rows {
        seed[i] = in_seed;
        seed[i] ^= ((((row_num - i as i32) * 37 + 178) & 0xFF) as u32) << 8;
        seed[i] ^= (((row_num - i as i32) * 173 + 105) & 0xFF) as u32;
    }

    let mut offsets = [[[0i32; 2]; 2]; 2];
    let w: [[[i32; 2]; 2]; 2] = [[[27, 17], [17, 27]], [[23, 22], [0, 0]]];

    let mut bx = 0usize;
    while bx < pw {
        let bw = ((bs >> sx).min(pw - bx)) as i32;

        if data.overlap_flag && bx > 0 {
            for i in 0..rows {
                for n in 0..2 {
                    offsets[1][i][n] = offsets[0][i][n];
                }
            }
        }

        for i in 0..rows {
            for n in 0..2 {
                offsets[0][i][n] = (((3 - data.block_size) as u32
                    * get_random_number(9, &mut seed[i]))
                    >> 6) as i32;
                for _ in 0..3 {
                    get_random_number(16, &mut seed[i]);
                }
            }
        }

        let ystart = if data.overlap_flag && row_num > 0 {
            (2 >> sy as i32).min(bh)
        } else {
            0
        };
        let xstart = if data.overlap_flag && bx > 0 {
            (2 >> sx as i32).min(bw)
        } else {
            0
        };

        macro_rules! add_noise_uv {
            ($x:expr, $y:expr, $grain:expr) => {{
                let lx = (bx + $x as usize) << sx;
                let ly = ($y as usize) << sy;
                let luma = luma_row[ly * luma_stride + lx];
                let avg: u8 = if sx != 0 {
                    ((luma as u16 + luma_row[ly * luma_stride + lx + 1] as u16 + 1) >> 1) as u8
                } else {
                    luma
                };
                let si = $y as usize * stride + bx + $x as usize;
                let val = if !data.chroma_scaling_from_luma {
                    let combined =
                        avg as i32 * data.uv_luma_mult[uv] + src[si] as i32 * data.uv_mult[uv];
                    iclip((combined >> 6) + data.uv_offset[uv], 0, 255) as usize
                } else {
                    avg as usize
                };
                let noise = round2(scaling[val] as i32 * $grain, data.scaling_shift as u32);
                dst[si] = iclip(src[si] as i32 + noise, min_value, max_value) as u8;
            }};
        }

        for y in ystart..bh {
            for x in xstart..bw {
                let grain = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 0, 0, x as usize, y as usize,
                ) as i32;
                add_noise_uv!(x, y, grain);
            }
            for x in 0..xstart {
                let grain = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 0, 0, x as usize, y as usize,
                ) as i32;
                let old = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 1, 0, x as usize, y as usize,
                ) as i32;
                let grain = iclip(
                    round2(old * w[sx][x as usize][0] + grain * w[sx][x as usize][1], 5),
                    grain_min,
                    grain_max,
                );
                add_noise_uv!(x, y, grain);
            }
        }

        for y in 0..ystart {
            for x in xstart..bw {
                let grain = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 0, 0, x as usize, y as usize,
                ) as i32;
                let old = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 0, 1, x as usize, y as usize,
                ) as i32;
                let grain = iclip(
                    round2(old * w[sy][y as usize][0] + grain * w[sy][y as usize][1], 5),
                    grain_min,
                    grain_max,
                );
                add_noise_uv!(x, y, grain);
            }
            for x in 0..xstart {
                let mut top = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 0, 1, x as usize, y as usize,
                ) as i32;
                let old = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 1, 1, x as usize, y as usize,
                ) as i32;
                top = iclip(
                    round2(old * w[sx][x as usize][0] + top * w[sx][x as usize][1], 5),
                    grain_min,
                    grain_max,
                );

                let mut grain = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 0, 0, x as usize, y as usize,
                ) as i32;
                let old2 = sample_lut(
                    grain_lut, bs, &offsets, sx, sy, 1, 0, x as usize, y as usize,
                ) as i32;
                grain = iclip(
                    round2(
                        old2 * w[sx][x as usize][0] + grain * w[sx][x as usize][1],
                        5,
                    ),
                    grain_min,
                    grain_max,
                );

                grain = iclip(
                    round2(top * w[sy][y as usize][0] + grain * w[sy][y as usize][1], 5),
                    grain_min,
                    grain_max,
                );
                add_noise_uv!(x, y, grain);
            }
        }

        bx += bs >> sx;
    }
}

pub struct GrainLut {
    pub y: [[i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
    pub u: [[i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
    pub v: [[i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
}

impl GrainLut {
    pub fn new() -> Self {
        Self {
            y: [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
            u: [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
            v: [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT],
        }
    }
}

impl Default for GrainLut {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(clippy::too_many_arguments)]
pub fn prep_grain_8bpc(
    fgd: &FilmGrainData,
    grain_lut: &mut GrainLut,
    scaling: &mut [Vec<u8>; 3],
    seed: u32,
    ss_x: bool,
    ss_y: bool,
) {
    // Generate grain LUTs as needed (dav2d_prep_grain, fg_apply_tmpl.c). The Y
    // LUT is ALWAYS generated: the chroma LUTs derive from it. The chroma LUTs
    // are generated with the plane's subsampling so the sub-grid dimensions
    // match the picture layout (dav2d selects generate_grain_uv[layout-1]).
    generate_grain_y(&mut grain_lut.y, fgd, seed);

    if fgd.num_points[1] > 0 || fgd.chroma_scaling_from_luma {
        generate_grain_uv(&mut grain_lut.u, &grain_lut.y, fgd, seed, 0, ss_x, ss_y);
    }
    if fgd.num_points[2] > 0 || fgd.chroma_scaling_from_luma {
        generate_grain_uv(&mut grain_lut.v, &grain_lut.y, fgd, seed, 1, ss_x, ss_y);
    }

    // Scaling LUTs (dav2d generates one per plane with scaling points).
    if fgd.num_points[0] > 0 {
        scaling[0].resize(256, 0);
        generate_scaling_8bpc(
            &fgd.points[0][..fgd.num_points[0] as usize],
            scaling[0].as_mut_slice().try_into().unwrap(),
        );
    }

    if !fgd.chroma_scaling_from_luma {
        for uv in 0..2 {
            if fgd.num_points[uv + 1] > 0 {
                scaling[uv + 1].resize(256, 0);
                generate_scaling_8bpc(
                    &fgd.points[uv + 1][..fgd.num_points[uv + 1] as usize],
                    scaling[uv + 1].as_mut_slice().try_into().unwrap(),
                );
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn apply_grain_row_8bpc(
    dst_y: &mut [u8],
    dst_u: &mut [u8],
    dst_v: &mut [u8],
    src_y: &[u8],
    src_u: &[u8],
    src_v: &[u8],
    y_stride: isize,
    uv_stride: isize,
    fgd: &FilmGrainData,
    grain_lut: &GrainLut,
    scaling: &[Vec<u8>; 3],
    w: usize,
    h: usize,
    row: usize,
    seed: u32,
    ss_x: bool,
    ss_y: bool,
) {
    // Grain block height in luma rows: 16 or 32 per `block_size` (dav2d
    // dav2d_apply_grain_row, fg_apply_tmpl.c: `bs = 16 << data->block_size`).
    let bs = (16usize) << fgd.block_size;
    let row_start = row * bs;

    if fgd.num_points[0] > 0 && !scaling[0].is_empty() {
        // Luma block height, clamped to the remaining image rows (dav2d
        // `bh = imin(out->p.h - row*bs, bs)`).
        let bh = (h - row_start).min(bs);
        let y_off = row_start * y_stride.unsigned_abs();
        let src_slice = if y_off < src_y.len() {
            &src_y[y_off..]
        } else {
            return;
        };
        let dst_slice = if y_off < dst_y.len() {
            &mut dst_y[y_off..]
        } else {
            return;
        };

        fgy_32x32xn_8bpc(
            dst_slice,
            src_slice,
            y_stride.unsigned_abs(),
            fgd,
            seed,
            w,
            scaling[0].as_slice().try_into().unwrap(),
            &grain_lut.y,
            bh as i32,
            row as i32,
        );
    }

    let has_uv = |uv: usize| -> bool {
        (fgd.num_points[uv + 1] > 0 || fgd.chroma_scaling_from_luma)
            && !scaling_for_uv(scaling, fgd, uv).is_empty()
    };

    // Chroma block height (dav2d `bh = (imin(out->p.h - row*bs, bs) + ss_y) >> ss_y`).
    let ch = (((h - row_start).min(bs) + ss_y as usize) >> ss_y as usize) as i32;
    let cw = (w + ss_x as usize) >> ss_x as usize;
    let uv_off = (row_start >> (ss_y as usize)) * uv_stride.unsigned_abs();
    let luma_off = row_start * y_stride.unsigned_abs();

    if has_uv(0) && uv_off < src_u.len() && uv_off < dst_u.len() {
        let uv_scaling: &[u8; 256] = scaling_for_uv(scaling, fgd, 0).try_into().unwrap();
        fguv_32x32xn_8bpc(
            &mut dst_u[uv_off..],
            &src_u[uv_off..],
            uv_stride.unsigned_abs(),
            fgd,
            seed,
            cw,
            uv_scaling,
            &grain_lut.u,
            ch,
            row as i32,
            &src_y[luma_off..],
            y_stride.unsigned_abs(),
            0,
            fgd.mc_identity,
            ss_x as usize,
            ss_y as usize,
        );
    }

    if has_uv(1) && uv_off < src_v.len() && uv_off < dst_v.len() {
        let uv_scaling: &[u8; 256] = scaling_for_uv(scaling, fgd, 1).try_into().unwrap();
        fguv_32x32xn_8bpc(
            &mut dst_v[uv_off..],
            &src_v[uv_off..],
            uv_stride.unsigned_abs(),
            fgd,
            seed,
            cw,
            uv_scaling,
            &grain_lut.v,
            ch,
            row as i32,
            &src_y[luma_off..],
            y_stride.unsigned_abs(),
            1,
            fgd.mc_identity,
            ss_x as usize,
            ss_y as usize,
        );
    }
}

fn scaling_for_uv<'a>(scaling: &'a [Vec<u8>; 3], fgd: &FilmGrainData, uv: usize) -> &'a [u8] {
    if fgd.chroma_scaling_from_luma {
        &scaling[0]
    } else {
        &scaling[uv + 1]
    }
}

#[allow(clippy::too_many_arguments)]
pub fn apply_grain_8bpc(
    dst_y: &mut [u8],
    dst_u: &mut [u8],
    dst_v: &mut [u8],
    src_y: &[u8],
    src_u: &[u8],
    src_v: &[u8],
    y_stride: isize,
    uv_stride: isize,
    fgd: &FilmGrainData,
    w: usize,
    h: usize,
    seed: u32,
    ss_x: bool,
    ss_y: bool,
) {
    apply_grain_8bpc_mt(
        dst_y, dst_u, dst_v, src_y, src_u, src_v, y_stride, uv_stride, fgd, w, h, seed, ss_x, ss_y,
        1,
    );
}

/// Film grain synthesis with optional row-band parallelism.
///
/// The output is partitioned into independent `bs`-tall row bands
/// (`dav2d_apply_grain`'s tiling). Each band writes only rows `[row*bs, …)` of
/// the destination planes and reads only the (read-only) ungrained `src` planes
/// plus the precomputed `grain_lut`/`scaling`; the per-pixel grain RNG is
/// re-derived from absolute position inside the kernels. The bands therefore
/// touch disjoint output memory and share no mutable state, so distributing them
/// across threads yields output byte-identical to the sequential loop.
///
/// `n_threads <= 1` runs the exact sequential loop (single-thread path
/// unchanged).
#[allow(clippy::too_many_arguments)]
pub fn apply_grain_8bpc_mt(
    dst_y: &mut [u8],
    dst_u: &mut [u8],
    dst_v: &mut [u8],
    src_y: &[u8],
    src_u: &[u8],
    src_v: &[u8],
    y_stride: isize,
    uv_stride: isize,
    fgd: &FilmGrainData,
    w: usize,
    h: usize,
    seed: u32,
    ss_x: bool,
    ss_y: bool,
    n_threads: u32,
) {
    let mut grain_lut = GrainLut::new();
    let mut scaling = [Vec::new(), Vec::new(), Vec::new()];

    prep_grain_8bpc(fgd, &mut grain_lut, &mut scaling, seed, ss_x, ss_y);

    // Row tiling matches dav2d_apply_grain: `bs = 16 << block_size`,
    // `rows = (h + bs - 1) / bs` (fg_apply_tmpl.c).
    let bs = (16usize) << fgd.block_size;
    let rows = h.div_ceil(bs);

    if n_threads <= 1 || rows <= 1 {
        for row in 0..rows {
            apply_grain_row_8bpc(
                dst_y, dst_u, dst_v, src_y, src_u, src_v, y_stride, uv_stride, fgd, &grain_lut,
                &scaling, w, h, row, seed, ss_x, ss_y,
            );
        }
        return;
    }

    // Parallel path: one job per row band. Each band writes disjoint output rows
    // (verified above), so the raw destination pointers are split across jobs
    // without aliasing. Pointers are wrapped in a Send marker because each band's
    // writes stay within its own row range.
    struct DstPtrs {
        y: *mut u8,
        y_len: usize,
        u: *mut u8,
        u_len: usize,
        v: *mut u8,
        v_len: usize,
    }
    // SAFETY: each row band writes only its disjoint output rows; the pointers
    // are never used to access another band's memory, so sharing them across
    // threads introduces no data race.
    unsafe impl Send for DstPtrs {}
    unsafe impl Sync for DstPtrs {}
    let dst = DstPtrs {
        y: dst_y.as_mut_ptr(),
        y_len: dst_y.len(),
        u: dst_u.as_mut_ptr(),
        u_len: dst_u.len(),
        v: dst_v.as_mut_ptr(),
        v_len: dst_v.len(),
    };
    let dst = &dst;
    let grain_lut = &grain_lut;
    let scaling = &scaling;

    let jobs: Vec<Box<dyn FnOnce() + Send + '_>> = (0..rows)
        .map(|row| {
            Box::new(move || {
                // SAFETY: reconstruct each plane slice from its base pointer for
                // the full plane length; the row function only writes within
                // `[row*bs*stride, …)` for this band's `bh` rows, which is
                // disjoint from every other band.
                let dst_y = unsafe { std::slice::from_raw_parts_mut(dst.y, dst.y_len) };
                let dst_u = unsafe { std::slice::from_raw_parts_mut(dst.u, dst.u_len) };
                let dst_v = unsafe { std::slice::from_raw_parts_mut(dst.v, dst.v_len) };
                apply_grain_row_8bpc(
                    dst_y, dst_u, dst_v, src_y, src_u, src_v, y_stride, uv_stride, fgd, grain_lut,
                    scaling, w, h, row, seed, ss_x, ss_y,
                );
            }) as Box<dyn FnOnce() + Send + '_>
        })
        .collect();

    crate::thread_task::run_disjoint_jobs(n_threads, jobs);
}

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

    #[test]
    fn test_get_random_number_lfsr() {
        let mut state = 0xAAAAu32;
        let v = get_random_number(8, &mut state);
        assert!(v < 256);
        assert_ne!(state, 0xAAAA);
    }

    #[test]
    fn test_get_random_number_sequence() {
        let mut state = 1u32;
        let mut vals = Vec::new();
        for _ in 0..100 {
            vals.push(get_random_number(4, &mut state));
        }
        assert!(vals.iter().any(|&v| v != vals[0]));
    }

    #[test]
    fn test_round2_basic() {
        assert_eq!(round2(10, 1), 5);
        assert_eq!(round2(11, 1), 6);
        assert_eq!(round2(7, 2), 2);
    }

    #[test]
    fn test_round2_negative() {
        assert_eq!(round2(-10, 1), -5);
    }

    #[test]
    fn test_generate_scaling_empty() {
        let mut scaling = [0u8; 256];
        generate_scaling_8bpc(&[], &mut scaling);
        assert!(scaling.iter().all(|&v| v == 0));
    }

    #[test]
    fn test_generate_scaling_single_point() {
        let mut scaling = [0u8; 256];
        generate_scaling_8bpc(&[[128, 200]], &mut scaling);
        assert!(scaling[..128].iter().all(|&v| v == 200));
        assert!(scaling[128..].iter().all(|&v| v == 200));
    }

    #[test]
    fn test_generate_scaling_two_points() {
        let mut scaling = [0u8; 256];
        generate_scaling_8bpc(&[[0, 0], [255, 255]], &mut scaling);
        assert_eq!(scaling[0], 0);
        assert!(scaling[128] > 100);
        assert_eq!(scaling[255], 255);
    }

    #[test]
    fn test_generate_scaling_monotonic() {
        let mut scaling = [0u8; 256];
        generate_scaling_8bpc(&[[0, 10], [100, 50], [200, 50]], &mut scaling);
        for i in 0..99 {
            assert!(scaling[i] <= scaling[i + 1] || scaling[i + 1] <= scaling[i]);
        }
    }

    fn make_grain_data(ar_lag: i32, grain_scale_shift: i32) -> FilmGrainData {
        let mut d = FilmGrainData::default();
        d.ar_coeff_lag = ar_lag;
        d.grain_scale_shift = grain_scale_shift;
        d.ar_coeff_shift = 6;
        d.num_points = [0, 0, 0];
        d
    }

    #[test]
    fn test_generate_grain_y_no_ar() {
        let mut buf = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data = make_grain_data(0, 0);
        generate_grain_y(&mut buf, &data, 1234);
        let nonzero = buf.iter().flat_map(|r| r.iter()).any(|&v| v != 0);
        assert!(nonzero);
    }

    #[test]
    fn test_generate_grain_y_bounded() {
        let mut buf = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data = make_grain_data(0, 0);
        generate_grain_y(&mut buf, &data, 5678);
        for row in &buf {
            for &v in row.iter() {
                assert!(v >= -128 && v <= 127, "grain value {} out of 8bpc range", v);
            }
        }
    }

    #[test]
    fn test_generate_grain_y_deterministic() {
        let mut buf1 = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let mut buf2 = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data = make_grain_data(1, 0);
        generate_grain_y(&mut buf1, &data, 42);
        generate_grain_y(&mut buf2, &data, 42);
        assert_eq!(buf1, buf2);
    }

    #[test]
    fn test_generate_grain_y_ar_lag1() {
        let mut buf = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let mut data = make_grain_data(1, 0);
        data.ar_coeffs[0][0] = 10;
        data.ar_coeffs[0][1] = -5;
        data.ar_coeffs[0][2] = 3;
        generate_grain_y(&mut buf, &data, 999);
        for row in &buf {
            for &v in row.iter() {
                assert!(v >= -128 && v <= 127);
            }
        }
    }

    #[test]
    fn test_generate_grain_uv_no_sub() {
        let mut buf = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let buf_y = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data = make_grain_data(0, 0);
        generate_grain_uv(&mut buf, &buf_y, &data, 100, 0, false, false);
        let nonzero = buf.iter().flat_map(|r| r.iter()).any(|&v| v != 0);
        assert!(nonzero);
    }

    #[test]
    fn test_generate_grain_uv_with_sub() {
        let mut buf = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let buf_y = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data = make_grain_data(0, 0);
        generate_grain_uv(&mut buf, &buf_y, &data, 100, 0, true, true);
        for y in 0..SUB_GRAIN_HEIGHT {
            for x in 0..SUB_GRAIN_WIDTH {
                assert!(buf[y][x] >= -128 && buf[y][x] <= 127);
            }
        }
    }

    #[test]
    fn test_generate_grain_uv_seed_xor() {
        let mut buf_u = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let mut buf_v = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let buf_y = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data = make_grain_data(0, 0);
        generate_grain_uv(&mut buf_u, &buf_y, &data, 100, 0, false, false);
        generate_grain_uv(&mut buf_v, &buf_y, &data, 100, 1, false, false);
        assert_ne!(buf_u[0][0], buf_v[0][0]);
    }

    #[test]
    fn test_generate_grain_uv_with_luma_correlation() {
        let mut buf = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let buf_y = [[64i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let mut data = make_grain_data(1, 0);
        data.num_points[0] = 1;
        data.ar_coeffs[1][0] = 5;
        data.ar_coeffs[1][1] = -3;
        data.ar_coeffs[1][2] = 2;
        generate_grain_uv(&mut buf, &buf_y, &data, 42, 0, false, false);
        for row in &buf[..GRAIN_HEIGHT] {
            for &v in row[..GRAIN_WIDTH].iter() {
                assert!(v >= -128 && v <= 127);
            }
        }
    }

    #[test]
    fn test_sample_lut_basic() {
        let mut lut = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        lut[10][15] = 42;
        let offsets = [[[0i32; 2]; 2]; 2];
        let v = sample_lut(&lut, 32, &offsets, 0, 0, 0, 0, 9, 4);
        let _ = v;
    }

    fn make_fgy_data() -> FilmGrainData {
        let mut d = FilmGrainData::default();
        d.scaling_shift = 8;
        d.block_size = 0;
        d.overlap_flag = false;
        d.clip_to_restricted_range = false;
        d
    }

    #[test]
    fn test_fgy_32x32xn_no_overlap() {
        let pw = 16;
        let bh = 4;
        let stride = pw;
        let src = vec![128u8; stride * bh as usize];
        let mut dst = vec![0u8; stride * bh as usize];
        let mut scaling = [0u8; 256];
        scaling[128] = 64;
        let mut grain_lut = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data_gen = make_grain_data(0, 0);
        generate_grain_y(&mut grain_lut, &data_gen, 42);
        let data = make_fgy_data();
        fgy_32x32xn_8bpc(
            &mut dst, &src, stride, &data, 100, pw, &scaling, &grain_lut, bh, 0,
        );
        let modified = dst.iter().zip(src.iter()).any(|(&d, &s)| d != s);
        assert!(modified);
    }

    #[test]
    fn test_fgy_32x32xn_output_bounded() {
        let pw = 32;
        let bh = 8;
        let stride = pw;
        let src: Vec<u8> = (0..stride * bh as usize)
            .map(|i| (i & 0xFF) as u8)
            .collect();
        let mut dst = vec![0u8; stride * bh as usize];
        let mut scaling = [128u8; 256];
        for i in 0..256 {
            scaling[i] = i as u8;
        }
        let mut grain_lut = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data_gen = make_grain_data(0, 0);
        generate_grain_y(&mut grain_lut, &data_gen, 7777);
        let data = make_fgy_data();
        fgy_32x32xn_8bpc(
            &mut dst, &src, stride, &data, 200, pw, &scaling, &grain_lut, bh, 0,
        );
        let has_variety = dst.windows(2).any(|w| w[0] != w[1]);
        assert!(has_variety);
    }

    #[test]
    fn test_fgy_32x32xn_restricted_range() {
        let pw = 16;
        let bh = 4;
        let stride = pw;
        let src = vec![0u8; stride * bh as usize];
        let mut dst = vec![0u8; stride * bh as usize];
        let scaling = [255u8; 256];
        let mut grain_lut = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data_gen = make_grain_data(0, 0);
        generate_grain_y(&mut grain_lut, &data_gen, 42);
        let mut data = make_fgy_data();
        data.clip_to_restricted_range = true;
        fgy_32x32xn_8bpc(
            &mut dst, &src, stride, &data, 100, pw, &scaling, &grain_lut, bh, 0,
        );
        for &v in &dst {
            assert!(v >= 16 && v <= 235, "restricted range violated: {}", v);
        }
    }

    #[test]
    fn test_fgy_32x32xn_deterministic() {
        let pw = 16;
        let bh = 4;
        let stride = pw;
        let src = vec![100u8; stride * bh as usize];
        let mut dst1 = vec![0u8; stride * bh as usize];
        let mut dst2 = vec![0u8; stride * bh as usize];
        let scaling = [50u8; 256];
        let mut grain_lut = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data_gen = make_grain_data(0, 0);
        generate_grain_y(&mut grain_lut, &data_gen, 42);
        let data = make_fgy_data();
        fgy_32x32xn_8bpc(
            &mut dst1, &src, stride, &data, 999, pw, &scaling, &grain_lut, bh, 0,
        );
        fgy_32x32xn_8bpc(
            &mut dst2, &src, stride, &data, 999, pw, &scaling, &grain_lut, bh, 0,
        );
        assert_eq!(dst1, dst2);
    }

    #[test]
    fn test_fgy_32x32xn_zero_scaling() {
        let pw = 16;
        let bh = 4;
        let stride = pw;
        let src = vec![128u8; stride * bh as usize];
        let mut dst = vec![0u8; stride * bh as usize];
        let scaling = [0u8; 256];
        let grain_lut = [[50i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data = make_fgy_data();
        fgy_32x32xn_8bpc(
            &mut dst, &src, stride, &data, 100, pw, &scaling, &grain_lut, bh, 0,
        );
        assert!(dst.iter().all(|&v| v == 128));
    }

    #[test]
    fn test_fguv_32x32xn_basic() {
        let pw = 16;
        let bh = 4;
        let stride = pw;
        let src = vec![128u8; stride * bh as usize];
        let mut dst = vec![0u8; stride * bh as usize];
        let luma = vec![128u8; pw * 2 * bh as usize * 2];
        let mut scaling = [0u8; 256];
        scaling[128] = 64;
        let mut grain_lut = [[0i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let data_gen = make_grain_data(0, 0);
        generate_grain_y(&mut grain_lut, &data_gen, 42);
        let mut data = make_fgy_data();
        data.uv_mult = [0; 2];
        data.uv_luma_mult = [64; 2];
        data.uv_offset = [0; 2];
        fguv_32x32xn_8bpc(
            &mut dst,
            &src,
            stride,
            &data,
            100,
            pw,
            &scaling,
            &grain_lut,
            bh,
            0,
            &luma,
            pw * 2,
            0,
            false,
            1,
            1,
        );
        let modified = dst.iter().zip(src.iter()).any(|(&d, &s)| d != s);
        assert!(modified);
    }

    #[test]
    fn test_fguv_32x32xn_chroma_from_luma() {
        let pw = 16;
        let bh = 4;
        let stride = pw;
        let src = vec![128u8; stride * bh as usize];
        let mut dst = vec![0u8; stride * bh as usize];
        let luma = vec![200u8; pw * bh as usize];
        let mut scaling = [0u8; 256];
        scaling[200] = 100;
        let grain_lut = [[50i16; GRAIN_WIDTH]; GRAIN_HEIGHT];
        let mut data = make_fgy_data();
        data.chroma_scaling_from_luma = true;
        fguv_32x32xn_8bpc(
            &mut dst, &src, stride, &data, 100, pw, &scaling, &grain_lut, bh, 0, &luma, pw, 0,
            true, 0, 0,
        );
        let modified = dst.iter().zip(src.iter()).any(|(&d, &s)| d != s);
        assert!(modified);
    }

    #[test]
    fn test_generate_scaling_hbd_empty() {
        let mut scaling = vec![0xFFu8; 1024];
        generate_scaling_hbd(10, &[], &mut scaling);
        assert!(scaling[..1024].iter().all(|&x| x == 0));
    }

    #[test]
    fn test_generate_scaling_hbd_single_point() {
        let mut scaling = vec![0u8; 1024];
        generate_scaling_hbd(10, &[[128, 200]], &mut scaling);
        assert_eq!(scaling[0], 200);
        assert_eq!(scaling[511], 200);
        assert_eq!(scaling[512], 200);
        assert_eq!(scaling[1023], 200);
    }

    #[test]
    fn test_generate_scaling_hbd_two_points() {
        let mut scaling = vec![0u8; 1024];
        generate_scaling_hbd(10, &[[0, 0], [255, 255]], &mut scaling);
        assert_eq!(scaling[0], 0);
        assert_eq!(scaling[1023], 255);
        assert!(scaling[512] > 120 && scaling[512] < 140);
    }

    #[test]
    fn test_generate_scaling_hbd_monotonic() {
        let mut scaling = vec![0u8; 1024];
        generate_scaling_hbd(10, &[[0, 0], [100, 100]], &mut scaling);
        for i in 1..400 {
            assert!(scaling[i] >= scaling[i - 1], "not monotonic at {}", i);
        }
    }

    #[test]
    fn test_generate_scaling_hbd_interpolation_fills_gaps() {
        let mut scaling = vec![0u8; 1024];
        generate_scaling_hbd(10, &[[0, 10], [128, 200]], &mut scaling);
        // shift_x=2, pad=4. Positions 1,2,3 should be interpolated (not zero)
        assert!(scaling[1] > 0);
        assert!(scaling[2] > 0);
        assert!(scaling[3] > 0);
    }
}