Merge branch 'lane-filmgrain': spec 7.18.3 film grain synthesis, pixel-exact
Browse files at this commit3 changed files +1068 -34
- crates/ec-av1/src/film_grain.rs +892 -0
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/stream.rs +175 -34
crates/ec-av1/src/film_grain.rsadded+892 -0
| @@ -0,0 +1,892 @@ | |||
| 1 | +//! AV1 film grain synthesis (spec 7.18.3), ported bit-exact from libaom's | ||
| 2 | +//! `av1/decoder/grain_synthesis.c` (`add_film_grain_run` and its helpers), | ||
| 3 | +//! specialized to this crate's only picture shape: 8-bit, 4:2:0 planar | ||
| 4 | +//! (`chroma_subsamp_x = chroma_subsamp_y = 1` throughout, which is why every | ||
| 5 | +//! `<< (1 - subsamp)` in the reference collapses to a no-op and every | ||
| 6 | +//! `>> subsamp` collapses to `>> 1` below). | ||
| 7 | +//! | ||
| 8 | +//! Grain is synthesized only into the picture handed back to the caller for | ||
| 9 | +//! *output* -- the picture stored as a reference for later inter prediction | ||
| 10 | +//! stays the clean, pre-grain one (spec 7.18.3.1: the synthesized grain "is | ||
| 11 | +//! not stored anywhere and is not used for predicting subsequent frames"). | ||
| 12 | +//! [`crate::stream::decode_stream`] is the only caller: it applies this to | ||
| 13 | +//! the frame it pushes onto its output `Vec<Picture>`, after saving the | ||
| 14 | +//! clean decode into the reference slot bank. | ||
| 15 | + | ||
| 16 | +use crate::encode::Picture; | ||
| 17 | +use ec_av1_syntax::FilmGrainParams; | ||
| 18 | + | ||
| 19 | +// Samples with Gaussian distribution in the range of [-2048, 2047] (12 bits) | ||
| 20 | +// with zero mean and standard deviation of about 512 -- libaom | ||
| 21 | +// `av1/decoder/grain_synthesis.c`'s `gaussian_sequence`, transcribed | ||
| 22 | +// verbatim (spec 7.18.3.3 names this table `Gaussian_Sequence`). | ||
| 23 | +#[rustfmt::skip] | ||
| 24 | +const GAUSSIAN_SEQUENCE: [i32; 2048] = [ | ||
| 25 | + 56, 568, -180, 172, 124, -84, 172, -64, -900, 24, 820, 224, 1248, 996, 272, -8, -916, -388, -732, -104, -188, 800, 112, -652, -320, -376, 140, -252, 492, -168, 44, -788, 588, -584, 500, -228, 12, 680, 272, -476, 972, -100, 652, 368, 432, -196, -720, -192, 1000, -332, 652, -136, -552, -604, -4, 192, -220, -136, 1000, -52, 372, -96, -624, 124, -24, 396, 540, -12, -104, 640, 464, 244, -208, -84, 368, -528, -740, 248, -968, -848, 608, 376, -60, -292, -40, -156, 252, -292, 248, 224, -280, 400, -244, 244, -60, 76, -80, 212, 532, 340, 128, -36, 824, -352, -60, -264, -96, -612, 416, -704, 220, -204, 640, -160, 1220, -408, 900, 336, 20, -336, -96, -792, 304, 48, -28, -1232, -1172, -448, 104, -292, -520, 244, 60, -948, 0, -708, 268, 108, 356, -548, 488, -344, -136, 488, -196, -224, 656, -236, -1128, 60, 4, 140, 276, -676, -376, 168, -108, 464, 8, 564, 64, 240, 308, -300, -400, -456, -136, 56, 120, -408, -116, 436, 504, -232, 328, 844, -164, -84, 784, -168, 232, -224, 348, -376, 128, 568, 96, -1244, -288, 276, 848, 832, -360, 656, 464, -384, -332, -356, 728, -388, 160, -192, 468, 296, 224, 140, -776, -100, 280, 4, 196, 44, -36, -648, 932, 16, 1428, 28, 528, 808, 772, 20, 268, 88, -332, -284, 124, -384, -448, 208, -228, -1044, -328, 660, 380, -148, -300, 588, 240, 540, 28, 136, -88, -436, 256, 296, -1000, 1400, 0, -48, 1056, -136, 264, -528, -1108, 632, -484, -592, -344, 796, 124, -668, -768, 388, 1296, -232, -188, -200, -288, -4, 308, 100, -168, 256, -500, 204, -508, 648, -136, 372, -272, -120, -1004, -552, -548, -384, 548, -296, 428, -108, -8, -912, -324, -224, -88, -112, -220, -100, 996, -796, 548, 360, -216, 180, 428, -200, -212, 148, 96, 148, 284, 216, -412, -320, 120, -300, -384, -604, -572, -332, -8, -180, -176, 696, 116, -88, 628, 76, 44, -516, 240, -208, -40, 100, -592, 344, -308, -452, -228, 20, 916, -1752, -136, -340, -804, 140, 40, 512, 340, 248, 184, -492, 896, -156, 932, -628, 328, -688, -448, -616, -752, -100, 560, -1020, 180, -800, -64, 76, 576, 1068, 396, 660, 552, -108, -28, 320, -628, 312, -92, -92, -472, 268, 16, 560, 516, -672, -52, 492, -100, 260, 384, 284, 292, 304, -148, 88, -152, 1012, 1064, -228, 164, -376, -684, 592, -392, 156, 196, -524, -64, -884, 160, -176, 636, 648, 404, -396, -436, 864, 424, -728, 988, -604, 904, -592, 296, -224, 536, -176, -920, 436, -48, 1176, -884, 416, -776, -824, -884, 524, -548, -564, -68, -164, -96, 692, 364, -692, -1012, -68, 260, -480, 876, -1116, 452, -332, -352, 892, -1088, 1220, -676, 12, -292, 244, 496, 372, -32, 280, 200, 112, -440, -96, 24, -644, -184, 56, -432, 224, -980, 272, -260, 144, -436, 420, 356, 364, -528, 76, 172, -744, -368, 404, -752, -416, 684, -688, 72, 540, 416, 92, 444, 480, -72, -1416, 164, -1172, -68, 24, 424, 264, 1040, 128, -912, -524, -356, 64, 876, -12, 4, -88, 532, 272, -524, 320, 276, -508, 940, 24, -400, -120, 756, 60, 236, -412, 100, 376, -484, 400, -100, -740, -108, -260, 328, -268, 224, -200, -416, 184, -604, -564, -20, 296, 60, 892, -888, 60, 164, 68, -760, 216, -296, 904, -336, -28, 404, -356, -568, -208, -1480, -512, 296, 328, -360, -164, -1560, -776, 1156, -428, 164, -504, -112, 120, -216, -148, -264, 308, 32, 64, -72, 72, 116, 176, -64, -272, 460, -536, -784, -280, 348, 108, -752, -132, 524, -540, -776, 116, -296, -1196, -288, -560, 1040, -472, 116, -848, -1116, 116, 636, 696, 284, -176, 1016, 204, -864, -648, -248, 356, 972, -584, -204, 264, 880, 528, -24, -184, 116, 448, -144, 828, 524, 212, -212, 52, 12, 200, 268, -488, -404, -880, 824, -672, -40, 908, -248, 500, 716, -576, 492, -576, 16, 720, -108, 384, 124, 344, 280, 576, -500, 252, 104, -308, 196, -188, -8, 1268, 296, 1032, -1196, 436, 316, 372, -432, -200, -660, 704, -224, 596, -132, 268, 32, -452, 884, 104, -1008, 424, -1348, -280, 4, -1168, 368, 476, 696, 300, -8, 24, 180, -592, -196, 388, 304, 500, 724, -160, 244, -84, 272, -256, -420, 320, 208, -144, -156, 156, 364, 452, 28, 540, 316, 220, -644, -248, 464, 72, 360, 32, -388, 496, -680, -48, 208, -116, -408, 60, -604, -392, 548, -840, 784, -460, 656, -544, -388, -264, 908, -800, -628, -612, -568, 572, -220, 164, 288, -16, -308, 308, -112, -636, -760, 280, -668, 432, 364, 240, -196, 604, 340, 384, 196, 592, -44, -500, 432, -580, -132, 636, -76, 392, 4, -412, 540, 508, 328, -356, -36, 16, -220, -64, -248, -60, 24, -192, 368, 1040, 92, -24, -1044, -32, 40, 104, 148, 192, -136, -520, 56, -816, -224, 732, 392, 356, 212, -80, -424, -1008, -324, 588, -1496, 576, 460, -816, -848, 56, -580, -92, -1372, -112, -496, 200, 364, 52, -140, 48, -48, -60, 84, 72, 40, 132, -356, -268, -104, -284, -404, 732, -520, 164, -304, -540, 120, 328, -76, -460, 756, 388, 588, 236, -436, -72, -176, -404, -316, -148, 716, -604, 404, -72, -88, -888, -68, 944, 88, -220, -344, 960, 472, 460, -232, 704, 120, 832, -228, 692, -508, 132, -476, 844, -748, -364, -44, 1116, -1104, -1056, 76, 428, 552, -692, 60, 356, 96, -384, -188, -612, -576, 736, 508, 892, 352, -1132, 504, -24, -352, 324, 332, -600, -312, 292, 508, -144, -8, 484, 48, 284, -260, -240, 256, -100, -292, -204, -44, 472, -204, 908, -188, -1000, -256, 92, 1164, -392, 564, 356, 652, -28, -884, 256, 484, -192, 760, -176, 376, -524, -452, -436, 860, -736, 212, 124, 504, -476, 468, 76, -472, 552, -692, -944, -620, 740, -240, 400, 132, 20, 192, -196, 264, -668, -1012, -60, 296, -316, -828, 76, -156, 284, -768, -448, -832, 148, 248, 652, 616, 1236, 288, -328, -400, -124, 588, 220, 520, -696, 1032, 768, -740, -92, -272, 296, 448, -464, 412, -200, 392, 440, -200, 264, -152, -260, 320, 1032, 216, 320, -8, -64, 156, -1016, 1084, 1172, 536, 484, -432, 132, 372, -52, -256, 84, 116, -352, 48, 116, 304, -384, 412, 924, -300, 528, 628, 180, 648, 44, -980, -220, 1320, 48, 332, 748, 524, -268, -720, 540, -276, 564, -344, -208, -196, 436, 896, 88, -392, 132, 80, -964, -288, 568, 56, -48, -456, 888, 8, 552, -156, -292, 948, 288, 128, -716, -292, 1192, -152, 876, 352, -600, -260, -812, -468, -28, -120, -32, -44, 1284, 496, 192, 464, 312, -76, -516, -380, -456, -1012, -48, 308, -156, 36, 492, -156, -808, 188, 1652, 68, -120, -116, 316, 160, -140, 352, 808, -416, 592, 316, -480, 56, 528, -204, -568, 372, -232, 752, -344, 744, -4, 324, -416, -600, 768, 268, -248, -88, -132, -420, -432, 80, -288, 404, -316, -1216, -588, 520, -108, 92, -320, 368, -480, -216, -92, 1688, -300, 180, 1020, -176, 820, -68, -228, -260, 436, -904, 20, 40, -508, 440, -736, 312, 332, 204, 760, -372, 728, 96, -20, -632, -520, -560, 336, 1076, -64, -532, 776, 584, 192, 396, -728, -520, 276, -188, 80, -52, -612, -252, -48, 648, 212, -688, 228, -52, -260, 428, -412, -272, -404, 180, 816, -796, 48, 152, 484, -88, -216, 988, 696, 188, -528, 648, -116, -180, 316, 476, 12, -564, 96, 476, -252, -364, -376, -392, 556, -256, -576, 260, -352, 120, -16, -136, -260, -492, 72, 556, 660, 580, 616, 772, 436, 424, -32, -324, -1268, 416, -324, -80, 920, 160, 228, 724, 32, -516, 64, 384, 68, -128, 136, 240, 248, -204, -68, 252, -932, -120, -480, -628, -84, 192, 852, -404, -288, -132, 204, 100, 168, -68, -196, -868, 460, 1080, 380, -80, 244, 0, 484, -888, 64, 184, 352, 600, 460, 164, 604, -196, 320, -64, 588, -184, 228, 12, 372, 48, -848, -344, 224, 208, -200, 484, 128, -20, 272, -468, -840, 384, 256, -720, -520, -464, -580, 112, -120, 644, -356, -208, -608, -528, 704, 560, -424, 392, 828, 40, 84, 200, -152, 0, -144, 584, 280, -120, 80, -556, -972, -196, -472, 724, 80, 168, -32, 88, 160, -688, 0, 160, 356, 372, -776, 740, -128, 676, -248, -480, 4, -364, 96, 544, 232, -1032, 956, 236, 356, 20, -40, 300, 24, -676, -596, 132, 1120, -104, 532, -1096, 568, 648, 444, 508, 380, 188, -376, -604, 1488, 424, 24, 756, -220, -192, 716, 120, 920, 688, 168, 44, -460, 568, 284, 1144, 1160, 600, 424, 888, 656, -356, -320, 220, 316, -176, -724, -188, -816, -628, -348, -228, -380, 1012, -452, -660, 736, 928, 404, -696, -72, -268, -892, 128, 184, -344, -780, 360, 336, 400, 344, 428, 548, -112, 136, -228, -216, -820, -516, 340, 92, -136, 116, -300, 376, -244, 100, -316, -520, -284, -12, 824, 164, -548, -180, -128, 116, -924, -828, 268, -368, -580, 620, 192, 160, 0, -1676, 1068, 424, -56, -360, 468, -156, 720, 288, -528, 556, -364, 548, -148, 504, 316, 152, -648, -620, -684, -24, -376, -384, -108, -920, -1032, 768, 180, -264, -508, -1268, -260, -60, 300, -240, 988, 724, -376, -576, -212, -736, 556, 192, 1092, -620, -880, 376, -56, -4, -216, -32, 836, 268, 396, 1332, 864, -600, 100, 56, -412, -92, 356, 180, 884, -468, -436, 292, -388, -804, -704, -840, 368, -348, 140, -724, 1536, 940, 372, 112, -372, 436, -480, 1136, 296, -32, -228, 132, -48, -220, 868, -1016, -60, -1044, -464, 328, 916, 244, 12, -736, -296, 360, 468, -376, -108, -92, 788, 368, -56, 544, 400, -672, -420, 728, 16, 320, 44, -284, -380, -796, 488, 132, 204, -596, -372, 88, -152, -908, -636, -572, -624, -116, -692, -200, -56, 276, -88, 484, -324, 948, 864, 1000, -456, -184, -276, 292, -296, 156, 676, 320, 160, 908, -84, -1236, -288, -116, 260, -372, -644, 732, -756, -96, 84, 344, -520, 348, -688, 240, -84, 216, -1044, -136, -676, -396, -1500, 960, -40, 176, 168, 1516, 420, -504, -344, -364, -360, 1216, -940, -380, -212, 252, -660, -708, 484, -444, -152, 928, -120, 1112, 476, -260, 560, -148, -344, 108, -196, 228, -288, 504, 560, -328, -88, 288, -1008, 460, -228, 468, -836, -196, 76, 388, 232, 412, -1168, -716, -644, 756, -172, -356, -504, 116, 432, 528, 48, 476, -168, -608, 448, 160, -532, -272, 28, -676, -12, 828, 980, 456, 520, 104, -104, 256, -344, -4, -28, -368, -52, -524, -572, -556, -200, 768, 1124, -208, -512, 176, 232, 248, -148, -888, 604, -600, -304, 804, -156, -212, 488, -192, -804, -256, 368, -360, -916, -328, 228, -240, -448, -472, 856, -556, -364, 572, -12, -156, -368, -340, 432, 252, -752, -152, 288, 268, -580, -848, -592, 108, -76, 244, 312, -716, 592, -80, 436, 360, 4, -248, 160, 516, 584, 732, 44, -468, -280, -292, -156, -588, 28, 308, 912, 24, 124, 156, 180, -252, 944, -924, -772, -520, -428, -624, 300, -212, -1144, 32, -724, 800, -1128, -212, -1288, -848, 180, -416, 440, 192, -576, -792, -76, -1080, 80, -532, -352, -132, 380, -820, 148, 1112, 128, 164, 456, 700, -924, 144, -668, -384, 648, -832, 508, 552, -52, -100, -656, 208, -568, 748, -88, 680, 232, 300, 192, -408, -1012, -152, -252, -268, 272, -876, -664, -648, -332, -136, 16, 12, 1152, -28, 332, -536, 320, -672, -460, -316, 532, -260, 228, -40, 1052, -816, 180, 88, -496, -556, -672, -368, 428, 92, 356, 404, -408, 252, 196, -176, -556, 792, 268, 32, 372, 40, 96, -332, 328, 120, 372, -900, -40, 472, -264, -592, 952, 128, 656, 112, 664, -232, 420, 4, -344, -464, 556, 244, -416, -32, 252, 0, -412, 188, -696, 508, -476, 324, -1096, 656, -312, 560, 264, -136, 304, 160, -64, -580, 248, 336, -720, 560, -348, -288, -276, -196, -500, 852, -544, -236, -1128, -992, -776, 116, 56, 52, 860, 884, 212, -12, 168, 1020, 512, -552, 924, -148, 716, 188, 164, -340, -520, -184, 880, -152, -680, -208, -1156, -300, -528, -472, 364, 100, -744, -1056, -32, 540, 280, 144, -676, -32, -232, -280, -224, 96, 568, -76, 172, 148, 148, 104, 32, -296, -32, 788, -80, 32, -16, 280, 288, 944, 428, -484, | ||
| 26 | +]; | ||
| 27 | + | ||
| 28 | +const GAUSS_BITS: u32 = 11; | ||
| 29 | + | ||
| 30 | +const LEFT_PAD: i32 = 3; | ||
| 31 | +const RIGHT_PAD: i32 = 3; | ||
| 32 | +const TOP_PAD: i32 = 3; | ||
| 33 | +const BOTTOM_PAD: i32 = 0; | ||
| 34 | +const AR_PADDING: i32 = 3; | ||
| 35 | + | ||
| 36 | +const LUMA_SUBBLOCK: i32 = 32; | ||
| 37 | +const CHROMA_SUBBLOCK: i32 = 16; // LUMA_SUBBLOCK >> subsampling (always 1 here) | ||
| 38 | + | ||
| 39 | +// 73 x 82, and 38 x 44 for chroma -- the sizes the mission's own recon named. | ||
| 40 | +const LUMA_BLOCK_H: i32 = TOP_PAD + 2 * AR_PADDING + LUMA_SUBBLOCK * 2 + BOTTOM_PAD; | ||
| 41 | +const LUMA_BLOCK_W: i32 = | ||
| 42 | + LEFT_PAD + 2 * AR_PADDING + LUMA_SUBBLOCK * 2 + 2 * AR_PADDING + RIGHT_PAD; | ||
| 43 | +const CHROMA_BLOCK_H: i32 = TOP_PAD + AR_PADDING + CHROMA_SUBBLOCK * 2 + BOTTOM_PAD; | ||
| 44 | +const CHROMA_BLOCK_W: i32 = LEFT_PAD + AR_PADDING + CHROMA_SUBBLOCK * 2 + AR_PADDING + RIGHT_PAD; | ||
| 45 | + | ||
| 46 | +// bit_depth is always 8 in this crate: grain_center = 128 << (8 - 8) = 128. | ||
| 47 | +const GRAIN_MIN: i32 = -128; | ||
| 48 | +const GRAIN_MAX: i32 = 127; | ||
| 49 | + | ||
| 50 | +const MIN_LUMA_LEGAL: i32 = 16; | ||
| 51 | +const MAX_LUMA_LEGAL: i32 = 235; | ||
| 52 | +const MIN_CHROMA_LEGAL: i32 = 16; | ||
| 53 | +const MAX_CHROMA_LEGAL: i32 = 240; | ||
| 54 | + | ||
| 55 | +/// The spec's 16-bit LFSR (`get_random_number` / `init_random_generator`, | ||
| 56 | +/// libaom `grain_synthesis.c`), threaded as one piece of mutable state | ||
| 57 | +/// across synthesis in the exact call order libaom uses: a raw seed load for | ||
| 58 | +/// the luma template, then a fresh line-mixed reseed for each of the chroma | ||
| 59 | +/// templates and for each row of placement offsets. | ||
| 60 | +struct Rng(u16); | ||
| 61 | + | ||
| 62 | +impl Rng { | ||
| 63 | + fn raw(seed: u16) -> Rng { | ||
| 64 | + Rng(seed) | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /// `init_random_generator` (spec 7.18.3.5). | ||
| 68 | + fn line(luma_line: i32, seed: u16) -> Rng { | ||
| 69 | + let msb = (seed >> 8) & 255; | ||
| 70 | + let lsb = seed & 255; | ||
| 71 | + let mut r: u16 = (msb << 8) + lsb; | ||
| 72 | + let luma_num = luma_line >> 5; | ||
| 73 | + r ^= (((luma_num.wrapping_mul(37) + 178) & 255) as u16) << 8; | ||
| 74 | + r ^= ((luma_num.wrapping_mul(173) + 105) & 255) as u16; | ||
| 75 | + Rng(r) | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /// `get_random_number` (spec 7.18.3.4). | ||
| 79 | + fn get(&mut self, bits: u32) -> i32 { | ||
| 80 | + let r = self.0; | ||
| 81 | + let bit = ((r) ^ (r >> 1) ^ (r >> 3) ^ (r >> 12)) & 1; | ||
| 82 | + self.0 = (r >> 1) | (bit << 15); | ||
| 83 | + ((self.0 >> (16 - bits)) & ((1u16 << bits) - 1)) as i32 | ||
| 84 | + } | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +#[inline] | ||
| 88 | +fn at(buf: &[i32], stride: i32, row: i32, col: i32) -> i32 { | ||
| 89 | + buf[(row * stride + col) as usize] | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +#[inline] | ||
| 93 | +fn set_at(buf: &mut [i32], stride: i32, row: i32, col: i32, v: i32) { | ||
| 94 | + buf[(row * stride + col) as usize] = v; | ||
| 95 | +} | ||
| 96 | + | ||
| 97 | +/// `pred_pos_luma`/`pred_pos_chroma` (libaom `init_arrays`): the AR filter's | ||
| 98 | +/// neighbour taps, in `(delta_row, delta_col, kind)` order, `kind == 1` | ||
| 99 | +/// marking the one extra chroma tap that averages a 2x2 luma quad instead of | ||
| 100 | +/// reading its own plane (spec 7.18.3.3's `avgLuma` case, only present when | ||
| 101 | +/// `num_y_points > 0`). | ||
| 102 | +fn build_pred_pos(lag: i32, has_luma_avg: bool) -> Vec<(i32, i32, i32)> { | ||
| 103 | + let mut pos = Vec::new(); | ||
| 104 | + for row in -lag..0 { | ||
| 105 | + for col in -lag..=lag { | ||
| 106 | + pos.push((row, col, 0)); | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | + for col in -lag..0 { | ||
| 110 | + pos.push((0, col, 0)); | ||
| 111 | + } | ||
| 112 | + if has_luma_avg { | ||
| 113 | + pos.push((0, 0, 1)); | ||
| 114 | + } | ||
| 115 | + pos | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +/// `generate_luma_grain_block` (spec 7.18.3.3). | ||
| 119 | +fn generate_luma_grain_block( | ||
| 120 | + fg: &FilmGrainParams, | ||
| 121 | + pred_pos: &[(i32, i32, i32)], | ||
| 122 | + rng: &mut Rng, | ||
| 123 | +) -> Vec<i32> { | ||
| 124 | + let mut block = vec![0i32; (LUMA_BLOCK_H * LUMA_BLOCK_W) as usize]; | ||
| 125 | + if fg.num_y_points == 0 { | ||
| 126 | + return block; | ||
| 127 | + } | ||
| 128 | + let gauss_sec_shift = 4 + fg.grain_scale_shift as i32; // 12 - 8 + grain_scale_shift | ||
| 129 | + let ar_coeff_shift = fg.ar_coeff_shift_minus_6 as i32 + 6; | ||
| 130 | + let rounding_offset = 1i32 << (ar_coeff_shift - 1); | ||
| 131 | + | ||
| 132 | + for i in 0..LUMA_BLOCK_H { | ||
| 133 | + for j in 0..LUMA_BLOCK_W { | ||
| 134 | + let g = GAUSSIAN_SEQUENCE[rng.get(GAUSS_BITS) as usize]; | ||
| 135 | + let v = (g + ((1 << gauss_sec_shift) >> 1)) >> gauss_sec_shift; | ||
| 136 | + set_at(&mut block, LUMA_BLOCK_W, i, j, v); | ||
| 137 | + } | ||
| 138 | + } | ||
| 139 | + for i in TOP_PAD..LUMA_BLOCK_H - BOTTOM_PAD { | ||
| 140 | + for j in LEFT_PAD..LUMA_BLOCK_W - RIGHT_PAD { | ||
| 141 | + let mut wsum = 0i32; | ||
| 142 | + for (idx, &(dy, dx, _)) in pred_pos.iter().enumerate() { | ||
| 143 | + wsum += fg.ar_coeffs_y[idx] as i32 * at(&block, LUMA_BLOCK_W, i + dy, j + dx); | ||
| 144 | + } | ||
| 145 | + let v = (at(&block, LUMA_BLOCK_W, i, j) + ((wsum + rounding_offset) >> ar_coeff_shift)) | ||
| 146 | + .clamp(GRAIN_MIN, GRAIN_MAX); | ||
| 147 | + set_at(&mut block, LUMA_BLOCK_W, i, j, v); | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + block | ||
| 151 | +} | ||
| 152 | + | ||
| 153 | +/// `generate_chroma_grain_blocks` (spec 7.18.3.3), returning `(cb, cr)`. | ||
| 154 | +fn generate_chroma_grain_blocks( | ||
| 155 | + fg: &FilmGrainParams, | ||
| 156 | + pred_pos: &[(i32, i32, i32)], | ||
| 157 | + luma_block: &[i32], | ||
| 158 | + seed: u16, | ||
| 159 | +) -> (Vec<i32>, Vec<i32>) { | ||
| 160 | + let mut cb = vec![0i32; (CHROMA_BLOCK_H * CHROMA_BLOCK_W) as usize]; | ||
| 161 | + let mut cr = vec![0i32; (CHROMA_BLOCK_H * CHROMA_BLOCK_W) as usize]; | ||
| 162 | + let gauss_sec_shift = 4 + fg.grain_scale_shift as i32; | ||
| 163 | + let ar_coeff_shift = fg.ar_coeff_shift_minus_6 as i32 + 6; | ||
| 164 | + let rounding_offset = 1i32 << (ar_coeff_shift - 1); | ||
| 165 | + | ||
| 166 | + let apply_cb = fg.num_cb_points > 0 || fg.chroma_scaling_from_luma; | ||
| 167 | + let apply_cr = fg.num_cr_points > 0 || fg.chroma_scaling_from_luma; | ||
| 168 | + | ||
| 169 | + if apply_cb { | ||
| 170 | + let mut rng = Rng::line(7 << 5, seed); | ||
| 171 | + for i in 0..CHROMA_BLOCK_H { | ||
| 172 | + for j in 0..CHROMA_BLOCK_W { | ||
| 173 | + let g = GAUSSIAN_SEQUENCE[rng.get(GAUSS_BITS) as usize]; | ||
| 174 | + set_at( | ||
| 175 | + &mut cb, | ||
| 176 | + CHROMA_BLOCK_W, | ||
| 177 | + i, | ||
| 178 | + j, | ||
| 179 | + (g + ((1 << gauss_sec_shift) >> 1)) >> gauss_sec_shift, | ||
| 180 | + ); | ||
| 181 | + } | ||
| 182 | + } | ||
| 183 | + } | ||
| 184 | + if apply_cr { | ||
| 185 | + let mut rng = Rng::line(11 << 5, seed); | ||
| 186 | + for i in 0..CHROMA_BLOCK_H { | ||
| 187 | + for j in 0..CHROMA_BLOCK_W { | ||
| 188 | + let g = GAUSSIAN_SEQUENCE[rng.get(GAUSS_BITS) as usize]; | ||
| 189 | + set_at( | ||
| 190 | + &mut cr, | ||
| 191 | + CHROMA_BLOCK_W, | ||
| 192 | + i, | ||
| 193 | + j, | ||
| 194 | + (g + ((1 << gauss_sec_shift) >> 1)) >> gauss_sec_shift, | ||
| 195 | + ); | ||
| 196 | + } | ||
| 197 | + } | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + for i in TOP_PAD..CHROMA_BLOCK_H - BOTTOM_PAD { | ||
| 201 | + for j in LEFT_PAD..CHROMA_BLOCK_W - RIGHT_PAD { | ||
| 202 | + let mut wsum_cb = 0i32; | ||
| 203 | + let mut wsum_cr = 0i32; | ||
| 204 | + for (idx, &(dy, dx, kind)) in pred_pos.iter().enumerate() { | ||
| 205 | + if kind == 0 { | ||
| 206 | + wsum_cb += | ||
| 207 | + fg.ar_coeffs_cb[idx] as i32 * at(&cb, CHROMA_BLOCK_W, i + dy, j + dx); | ||
| 208 | + wsum_cr += | ||
| 209 | + fg.ar_coeffs_cr[idx] as i32 * at(&cr, CHROMA_BLOCK_W, i + dy, j + dx); | ||
| 210 | + } else { | ||
| 211 | + // `avgLuma` (spec 7.18.3.3): subsampling_x = subsampling_y = 1 | ||
| 212 | + // always here, so the 2x2 luma quad is exactly one 4:2:0 cell. | ||
| 213 | + let luma_y = ((i - TOP_PAD) << 1) + TOP_PAD; | ||
| 214 | + let luma_x = ((j - LEFT_PAD) << 1) + LEFT_PAD; | ||
| 215 | + let mut av = 0i32; | ||
| 216 | + for k in luma_y..luma_y + 2 { | ||
| 217 | + for l in luma_x..luma_x + 2 { | ||
| 218 | + av += at(luma_block, LUMA_BLOCK_W, k, l); | ||
| 219 | + } | ||
| 220 | + } | ||
| 221 | + av = (av + 2) >> 2; | ||
| 222 | + wsum_cb += fg.ar_coeffs_cb[idx] as i32 * av; | ||
| 223 | + wsum_cr += fg.ar_coeffs_cr[idx] as i32 * av; | ||
| 224 | + } | ||
| 225 | + } | ||
| 226 | + if apply_cb { | ||
| 227 | + let v = (at(&cb, CHROMA_BLOCK_W, i, j) | ||
| 228 | + + ((wsum_cb + rounding_offset) >> ar_coeff_shift)) | ||
| 229 | + .clamp(GRAIN_MIN, GRAIN_MAX); | ||
| 230 | + set_at(&mut cb, CHROMA_BLOCK_W, i, j, v); | ||
| 231 | + } | ||
| 232 | + if apply_cr { | ||
| 233 | + let v = (at(&cr, CHROMA_BLOCK_W, i, j) | ||
| 234 | + + ((wsum_cr + rounding_offset) >> ar_coeff_shift)) | ||
| 235 | + .clamp(GRAIN_MIN, GRAIN_MAX); | ||
| 236 | + set_at(&mut cr, CHROMA_BLOCK_W, i, j, v); | ||
| 237 | + } | ||
| 238 | + } | ||
| 239 | + } | ||
| 240 | + (cb, cr) | ||
| 241 | +} | ||
| 242 | + | ||
| 243 | +/// `init_scaling_function` (spec 7.18.3.2, piecewise-linear scaling LUT). | ||
| 244 | +fn build_scaling_lut(values: &[u8], scaling: &[u8], num_points: usize) -> [i32; 256] { | ||
| 245 | + let mut lut = [0i32; 256]; | ||
| 246 | + if num_points == 0 { | ||
| 247 | + return lut; | ||
| 248 | + } | ||
| 249 | + for i in 0..values[0] as usize { | ||
| 250 | + lut[i] = scaling[0] as i32; | ||
| 251 | + } | ||
| 252 | + for p in 0..num_points - 1 { | ||
| 253 | + let delta_y = scaling[p + 1] as i32 - scaling[p] as i32; | ||
| 254 | + let delta_x = values[p + 1] as i32 - values[p] as i32; | ||
| 255 | + let inner = (65536 + (delta_x >> 1)) / delta_x; | ||
| 256 | + let delta = delta_y as i64 * inner as i64; | ||
| 257 | + for x in 0..delta_x { | ||
| 258 | + lut[(values[p] as i32 + x) as usize] = | ||
| 259 | + scaling[p] as i32 + (((x as i64) * delta + 32768) >> 16) as i32; | ||
| 260 | + } | ||
| 261 | + } | ||
| 262 | + for i in values[num_points - 1] as usize..256 { | ||
| 263 | + lut[i] = scaling[num_points - 1] as i32; | ||
| 264 | + } | ||
| 265 | + lut | ||
| 266 | +} | ||
| 267 | + | ||
| 268 | +/// `add_noise_to_block` (spec 7.18.3.5), 8-bit / 4:2:0 only. `(py0, px0)` is | ||
| 269 | +/// the luma-plane pixel origin; the matching chroma origin is `(py0/2, | ||
| 270 | +/// px0/2)`. Grain is read from `(grain, stride, row0, col0)` triples so the | ||
| 271 | +/// same function serves both the raw 82x73/44x38 templates and the small | ||
| 272 | +/// blended col/line buffers below. | ||
| 273 | +#[allow(clippy::too_many_arguments)] | ||
| 274 | +fn add_noise_to_block( | ||
| 275 | + fg: &FilmGrainParams, | ||
| 276 | + mc_identity: bool, | ||
| 277 | + scaling_y: &[i32; 256], | ||
| 278 | + scaling_cb: &[i32; 256], | ||
| 279 | + scaling_cr: &[i32; 256], | ||
| 280 | + picture: &mut Picture, | ||
| 281 | + py0: i32, | ||
| 282 | + px0: i32, | ||
| 283 | + luma_grain: &[i32], | ||
| 284 | + lg_stride: i32, | ||
| 285 | + lg_row0: i32, | ||
| 286 | + lg_col0: i32, | ||
| 287 | + cb_grain: &[i32], | ||
| 288 | + cr_grain: &[i32], | ||
| 289 | + cg_stride: i32, | ||
| 290 | + cg_row0: i32, | ||
| 291 | + cg_col0: i32, | ||
| 292 | + half_h: i32, | ||
| 293 | + half_w: i32, | ||
| 294 | +) { | ||
| 295 | + let y_stride = picture.width as i32; | ||
| 296 | + let c_stride = (picture.width / 2) as i32; | ||
| 297 | + let scaling_shift = fg.grain_scaling_minus_8 as i32 + 8; | ||
| 298 | + | ||
| 299 | + let mut cb_mult = fg.cb_mult as i32 - 128; | ||
| 300 | + let mut cb_luma_mult = fg.cb_luma_mult as i32 - 128; | ||
| 301 | + let mut cb_offset = fg.cb_offset as i32 - 256; | ||
| 302 | + let mut cr_mult = fg.cr_mult as i32 - 128; | ||
| 303 | + let mut cr_luma_mult = fg.cr_luma_mult as i32 - 128; | ||
| 304 | + let mut cr_offset = fg.cr_offset as i32 - 256; | ||
| 305 | + if fg.chroma_scaling_from_luma { | ||
| 306 | + cb_mult = 0; | ||
| 307 | + cb_luma_mult = 64; | ||
| 308 | + cb_offset = 0; | ||
| 309 | + cr_mult = 0; | ||
| 310 | + cr_luma_mult = 64; | ||
| 311 | + cr_offset = 0; | ||
| 312 | + } | ||
| 313 | + let rounding_offset = 1i32 << (scaling_shift - 1); | ||
| 314 | + let apply_y = fg.num_y_points > 0; | ||
| 315 | + let apply_cb = fg.num_cb_points > 0 || fg.chroma_scaling_from_luma; | ||
| 316 | + let apply_cr = fg.num_cr_points > 0 || fg.chroma_scaling_from_luma; | ||
| 317 | + | ||
| 318 | + let (min_luma, max_luma, min_chroma, max_chroma) = if fg.clip_to_restricted_range { | ||
| 319 | + if mc_identity { | ||
| 320 | + ( | ||
| 321 | + MIN_LUMA_LEGAL, | ||
| 322 | + MAX_LUMA_LEGAL, | ||
| 323 | + MIN_LUMA_LEGAL, | ||
| 324 | + MAX_LUMA_LEGAL, | ||
| 325 | + ) | ||
| 326 | + } else { | ||
| 327 | + ( | ||
| 328 | + MIN_LUMA_LEGAL, | ||
| 329 | + MAX_LUMA_LEGAL, | ||
| 330 | + MIN_CHROMA_LEGAL, | ||
| 331 | + MAX_CHROMA_LEGAL, | ||
| 332 | + ) | ||
| 333 | + } | ||
| 334 | + } else { | ||
| 335 | + (0, 255, 0, 255) | ||
| 336 | + }; | ||
| 337 | + | ||
| 338 | + let cy0 = py0 / 2; | ||
| 339 | + let cx0 = px0 / 2; | ||
| 340 | + for i in 0..half_h { | ||
| 341 | + for j in 0..half_w { | ||
| 342 | + let ly = py0 + i * 2; | ||
| 343 | + let lx = px0 + j * 2; | ||
| 344 | + let l0 = (ly * y_stride + lx) as usize; | ||
| 345 | + let average_luma = (picture.y[l0] as i32 + picture.y[l0 + 1] as i32 + 1) >> 1; | ||
| 346 | + let cidx = ((cy0 + i) * c_stride + (cx0 + j)) as usize; | ||
| 347 | + let gidx = ((cg_row0 + i) * cg_stride + (cg_col0 + j)) as usize; | ||
| 348 | + if apply_cb { | ||
| 349 | + let scaled = (((average_luma * cb_luma_mult + cb_mult * picture.u[cidx] as i32) | ||
| 350 | + >> 6) | ||
| 351 | + + cb_offset) | ||
| 352 | + .clamp(0, 255); | ||
| 353 | + let delta = (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset) | ||
| 354 | + >> scaling_shift; | ||
| 355 | + picture.u[cidx] = | ||
| 356 | + ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u8; | ||
| 357 | + } | ||
| 358 | + if apply_cr { | ||
| 359 | + let scaled = (((average_luma * cr_luma_mult + cr_mult * picture.v[cidx] as i32) | ||
| 360 | + >> 6) | ||
| 361 | + + cr_offset) | ||
| 362 | + .clamp(0, 255); | ||
| 363 | + let delta = (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset) | ||
| 364 | + >> scaling_shift; | ||
| 365 | + picture.v[cidx] = | ||
| 366 | + ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u8; | ||
| 367 | + } | ||
| 368 | + } | ||
| 369 | + } | ||
| 370 | + if apply_y { | ||
| 371 | + for i in 0..half_h * 2 { | ||
| 372 | + for j in 0..half_w * 2 { | ||
| 373 | + let pidx = ((py0 + i) * y_stride + (px0 + j)) as usize; | ||
| 374 | + let gidx = ((lg_row0 + i) * lg_stride + (lg_col0 + j)) as usize; | ||
| 375 | + let delta = (scaling_y[picture.y[pidx] as usize] * luma_grain[gidx] | ||
| 376 | + + rounding_offset) | ||
| 377 | + >> scaling_shift; | ||
| 378 | + picture.y[pidx] = | ||
| 379 | + ((picture.y[pidx] as i32 + delta).clamp(min_luma, max_luma)) as u8; | ||
| 380 | + } | ||
| 381 | + } | ||
| 382 | + } | ||
| 383 | +} | ||
| 384 | + | ||
| 385 | +/// `ver_boundary_overlap` (spec 7.18.3.5), in place on `dst` (== the C's | ||
| 386 | +/// aliased `left` argument -- every call site overlaps them, and each row is | ||
| 387 | +/// read once then written once, so there is no hazard). | ||
| 388 | +#[allow(clippy::too_many_arguments)] | ||
| 389 | +fn ver_overlap_inplace( | ||
| 390 | + dst: &mut [i32], | ||
| 391 | + dst_stride: i32, | ||
| 392 | + dst_row0: i32, | ||
| 393 | + dst_col0: i32, | ||
| 394 | + right: &[i32], | ||
| 395 | + right_stride: i32, | ||
| 396 | + right_row0: i32, | ||
| 397 | + right_col0: i32, | ||
| 398 | + width: i32, | ||
| 399 | + height: i32, | ||
| 400 | +) { | ||
| 401 | + for h in 0..height { | ||
| 402 | + if width == 1 { | ||
| 403 | + let l = at(dst, dst_stride, dst_row0 + h, dst_col0); | ||
| 404 | + let r = at(right, right_stride, right_row0 + h, right_col0); | ||
| 405 | + set_at( | ||
| 406 | + dst, | ||
| 407 | + dst_stride, | ||
| 408 | + dst_row0 + h, | ||
| 409 | + dst_col0, | ||
| 410 | + ((l * 23 + r * 22 + 16) >> 5).clamp(GRAIN_MIN, GRAIN_MAX), | ||
| 411 | + ); | ||
| 412 | + } else { | ||
| 413 | + let l0 = at(dst, dst_stride, dst_row0 + h, dst_col0); | ||
| 414 | + let l1 = at(dst, dst_stride, dst_row0 + h, dst_col0 + 1); | ||
| 415 | + let r0 = at(right, right_stride, right_row0 + h, right_col0); | ||
| 416 | + let r1 = at(right, right_stride, right_row0 + h, right_col0 + 1); | ||
| 417 | + set_at( | ||
| 418 | + dst, | ||
| 419 | + dst_stride, | ||
| 420 | + dst_row0 + h, | ||
| 421 | + dst_col0, | ||
| 422 | + ((27 * l0 + 17 * r0 + 16) >> 5).clamp(GRAIN_MIN, GRAIN_MAX), | ||
| 423 | + ); | ||
| 424 | + set_at( | ||
| 425 | + dst, | ||
| 426 | + dst_stride, | ||
| 427 | + dst_row0 + h, | ||
| 428 | + dst_col0 + 1, | ||
| 429 | + ((17 * l1 + 27 * r1 + 16) >> 5).clamp(GRAIN_MIN, GRAIN_MAX), | ||
| 430 | + ); | ||
| 431 | + } | ||
| 432 | + } | ||
| 433 | +} | ||
| 434 | + | ||
| 435 | +/// `hor_boundary_overlap` (spec 7.18.3.5), in place on `dst` (== the C's | ||
| 436 | +/// aliased `top` argument, same no-hazard reasoning as above). | ||
| 437 | +#[allow(clippy::too_many_arguments)] | ||
| 438 | +fn hor_overlap_inplace( | ||
| 439 | + dst: &mut [i32], | ||
| 440 | + dst_stride: i32, | ||
| 441 | + dst_row0: i32, | ||
| 442 | + dst_col0: i32, | ||
| 443 | + bottom: &[i32], | ||
| 444 | + bot_stride: i32, | ||
| 445 | + bot_row0: i32, | ||
| 446 | + bot_col0: i32, | ||
| 447 | + width: i32, | ||
| 448 | + height: i32, | ||
| 449 | +) { | ||
| 450 | + if height == 1 { | ||
| 451 | + for w in 0..width { | ||
| 452 | + let t = at(dst, dst_stride, dst_row0, dst_col0 + w); | ||
| 453 | + let b = at(bottom, bot_stride, bot_row0, bot_col0 + w); | ||
| 454 | + set_at( | ||
| 455 | + dst, | ||
| 456 | + dst_stride, | ||
| 457 | + dst_row0, | ||
| 458 | + dst_col0 + w, | ||
| 459 | + ((t * 23 + b * 22 + 16) >> 5).clamp(GRAIN_MIN, GRAIN_MAX), | ||
| 460 | + ); | ||
| 461 | + } | ||
| 462 | + } else { | ||
| 463 | + for w in 0..width { | ||
| 464 | + let t0 = at(dst, dst_stride, dst_row0, dst_col0 + w); | ||
| 465 | + let t1 = at(dst, dst_stride, dst_row0 + 1, dst_col0 + w); | ||
| 466 | + let b0 = at(bottom, bot_stride, bot_row0, bot_col0 + w); | ||
| 467 | + let b1 = at(bottom, bot_stride, bot_row0 + 1, bot_col0 + w); | ||
| 468 | + set_at( | ||
| 469 | + dst, | ||
| 470 | + dst_stride, | ||
| 471 | + dst_row0, | ||
| 472 | + dst_col0 + w, | ||
| 473 | + ((27 * t0 + 17 * b0 + 16) >> 5).clamp(GRAIN_MIN, GRAIN_MAX), | ||
| 474 | + ); | ||
| 475 | + set_at( | ||
| 476 | + dst, | ||
| 477 | + dst_stride, | ||
| 478 | + dst_row0 + 1, | ||
| 479 | + dst_col0 + w, | ||
| 480 | + ((17 * t1 + 27 * b1 + 16) >> 5).clamp(GRAIN_MIN, GRAIN_MAX), | ||
| 481 | + ); | ||
| 482 | + } | ||
| 483 | + } | ||
| 484 | +} | ||
| 485 | + | ||
| 486 | +#[allow(clippy::too_many_arguments)] | ||
| 487 | +fn copy_area( | ||
| 488 | + dst: &mut [i32], | ||
| 489 | + dst_stride: i32, | ||
| 490 | + dst_row0: i32, | ||
| 491 | + dst_col0: i32, | ||
| 492 | + src: &[i32], | ||
| 493 | + src_stride: i32, | ||
| 494 | + src_row0: i32, | ||
| 495 | + src_col0: i32, | ||
| 496 | + width: i32, | ||
| 497 | + height: i32, | ||
| 498 | +) { | ||
| 499 | + for h in 0..height { | ||
| 500 | + for w in 0..width { | ||
| 501 | + let v = at(src, src_stride, src_row0 + h, src_col0 + w); | ||
| 502 | + set_at(dst, dst_stride, dst_row0 + h, dst_col0 + w, v); | ||
| 503 | + } | ||
| 504 | + } | ||
| 505 | +} | ||
| 506 | + | ||
| 507 | +/// `av1_add_film_grain`/`add_film_grain_run` (spec 7.18.3.5): synthesize and | ||
| 508 | +/// apply film grain to `picture`, returning a new grained picture. `picture` | ||
| 509 | +/// itself is unchanged -- it stays the clean reference-frame-bank copy. | ||
| 510 | +/// Returns `picture.clone()` untouched when `apply_grain` is unset. | ||
| 511 | +pub(crate) fn apply_grain(picture: &Picture, fg: &FilmGrainParams, mc_identity: bool) -> Picture { | ||
| 512 | + let mut out = picture.clone(); | ||
| 513 | + if !fg.apply_grain { | ||
| 514 | + return out; | ||
| 515 | + } | ||
| 516 | + let width = out.width as i32; | ||
| 517 | + let height = out.height as i32; | ||
| 518 | + let y_stride = width; | ||
| 519 | + let c_stride = width / 2; | ||
| 520 | + | ||
| 521 | + let lag = fg.ar_coeff_lag as i32; | ||
| 522 | + let pred_luma = build_pred_pos(lag, false); | ||
| 523 | + let pred_chroma = build_pred_pos(lag, fg.num_y_points > 0); | ||
| 524 | + | ||
| 525 | + let mut rng = Rng::raw(fg.grain_seed); | ||
| 526 | + let luma_template = generate_luma_grain_block(fg, &pred_luma, &mut rng); | ||
| 527 | + let (cb_template, cr_template) = | ||
| 528 | + generate_chroma_grain_blocks(fg, &pred_chroma, &luma_template, fg.grain_seed); | ||
| 529 | + | ||
| 530 | + let scaling_y = build_scaling_lut( | ||
| 531 | + &fg.point_y_value, | ||
| 532 | + &fg.point_y_scaling, | ||
| 533 | + fg.num_y_points as usize, | ||
| 534 | + ); | ||
| 535 | + let (scaling_cb, scaling_cr) = if fg.chroma_scaling_from_luma { | ||
| 536 | + (scaling_y, scaling_y) | ||
| 537 | + } else { | ||
| 538 | + ( | ||
| 539 | + build_scaling_lut( | ||
| 540 | + &fg.point_cb_value, | ||
| 541 | + &fg.point_cb_scaling, | ||
| 542 | + fg.num_cb_points as usize, | ||
| 543 | + ), | ||
| 544 | + build_scaling_lut( | ||
| 545 | + &fg.point_cr_value, | ||
| 546 | + &fg.point_cr_scaling, | ||
| 547 | + fg.num_cr_points as usize, | ||
| 548 | + ), | ||
| 549 | + ) | ||
| 550 | + }; | ||
| 551 | + | ||
| 552 | + let overlap = fg.overlap_flag; | ||
| 553 | + | ||
| 554 | + let mut y_line_buf = vec![0i32; (y_stride * 2).max(1) as usize]; | ||
| 555 | + let mut cb_line_buf = vec![0i32; c_stride.max(1) as usize]; | ||
| 556 | + let mut cr_line_buf = vec![0i32; c_stride.max(1) as usize]; | ||
| 557 | + let mut y_col_buf = vec![0i32; ((LUMA_SUBBLOCK + 2) * 2) as usize]; | ||
| 558 | + let mut cb_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | ||
| 559 | + let mut cr_col_buf = vec![0i32; (CHROMA_SUBBLOCK + 1) as usize]; | ||
| 560 | + | ||
| 561 | + let mut y = 0; | ||
| 562 | + while y < height / 2 { | ||
| 563 | + let mut row_rng = Rng::line(y * 2, fg.grain_seed); | ||
| 564 | + let mut x = 0; | ||
| 565 | + while x < width / 2 { | ||
| 566 | + let raw = row_rng.get(8); | ||
| 567 | + let offset_x = (raw >> 4) & 15; | ||
| 568 | + let offset_y = raw & 15; | ||
| 569 | + | ||
| 570 | + let luma_off_y = LEFT_PAD + 2 * AR_PADDING + (offset_y << 1); | ||
| 571 | + let luma_off_x = TOP_PAD + 2 * AR_PADDING + (offset_x << 1); | ||
| 572 | + let chroma_off_y = TOP_PAD + AR_PADDING + offset_y; | ||
| 573 | + let chroma_off_x = LEFT_PAD + AR_PADDING + offset_x; | ||
| 574 | + | ||
| 575 | + if overlap && x != 0 { | ||
| 576 | + let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); | ||
| 577 | + ver_overlap_inplace( | ||
| 578 | + &mut y_col_buf, | ||
| 579 | + 2, | ||
| 580 | + 0, | ||
| 581 | + 0, | ||
| 582 | + &luma_template, | ||
| 583 | + LUMA_BLOCK_W, | ||
| 584 | + luma_off_y, | ||
| 585 | + luma_off_x, | ||
| 586 | + 2, | ||
| 587 | + h_count, | ||
| 588 | + ); | ||
| 589 | + let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); | ||
| 590 | + ver_overlap_inplace( | ||
| 591 | + &mut cb_col_buf, | ||
| 592 | + 1, | ||
| 593 | + 0, | ||
| 594 | + 0, | ||
| 595 | + &cb_template, | ||
| 596 | + CHROMA_BLOCK_W, | ||
| 597 | + chroma_off_y, | ||
| 598 | + chroma_off_x, | ||
| 599 | + 1, | ||
| 600 | + hc_count, | ||
| 601 | + ); | ||
| 602 | + ver_overlap_inplace( | ||
| 603 | + &mut cr_col_buf, | ||
| 604 | + 1, | ||
| 605 | + 0, | ||
| 606 | + 0, | ||
| 607 | + &cr_template, | ||
| 608 | + CHROMA_BLOCK_W, | ||
| 609 | + chroma_off_y, | ||
| 610 | + chroma_off_x, | ||
| 611 | + 1, | ||
| 612 | + hc_count, | ||
| 613 | + ); | ||
| 614 | + | ||
| 615 | + let i = if y != 0 { 1 } else { 0 }; | ||
| 616 | + add_noise_to_block( | ||
| 617 | + fg, | ||
| 618 | + mc_identity, | ||
| 619 | + &scaling_y, | ||
| 620 | + &scaling_cb, | ||
| 621 | + &scaling_cr, | ||
| 622 | + &mut out, | ||
| 623 | + (y + i) << 1, | ||
| 624 | + x << 1, | ||
| 625 | + &y_col_buf, | ||
| 626 | + 2, | ||
| 627 | + i * 2, | ||
| 628 | + 0, | ||
| 629 | + &cb_col_buf, | ||
| 630 | + &cr_col_buf, | ||
| 631 | + 1, | ||
| 632 | + i, | ||
| 633 | + 0, | ||
| 634 | + (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, | ||
| 635 | + 1, | ||
| 636 | + ); | ||
| 637 | + } | ||
| 638 | + | ||
| 639 | + if overlap && y != 0 { | ||
| 640 | + if x != 0 { | ||
| 641 | + hor_overlap_inplace( | ||
| 642 | + &mut y_line_buf, | ||
| 643 | + y_stride, | ||
| 644 | + 0, | ||
| 645 | + x << 1, | ||
| 646 | + &y_col_buf, | ||
| 647 | + 2, | ||
| 648 | + 0, | ||
| 649 | + 0, | ||
| 650 | + 2, | ||
| 651 | + 2, | ||
| 652 | + ); | ||
| 653 | + hor_overlap_inplace( | ||
| 654 | + &mut cb_line_buf, | ||
| 655 | + c_stride, | ||
| 656 | + 0, | ||
| 657 | + x, | ||
| 658 | + &cb_col_buf, | ||
| 659 | + 1, | ||
| 660 | + 0, | ||
| 661 | + 0, | ||
| 662 | + 1, | ||
| 663 | + 1, | ||
| 664 | + ); | ||
| 665 | + hor_overlap_inplace( | ||
| 666 | + &mut cr_line_buf, | ||
| 667 | + c_stride, | ||
| 668 | + 0, | ||
| 669 | + x, | ||
| 670 | + &cr_col_buf, | ||
| 671 | + 1, | ||
| 672 | + 0, | ||
| 673 | + 0, | ||
| 674 | + 1, | ||
| 675 | + 1, | ||
| 676 | + ); | ||
| 677 | + } | ||
| 678 | + let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 679 | + let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 680 | + let lw = (LUMA_SUBBLOCK - (if x != 0 { 2 } else { 0 })).min(width - lcol0); | ||
| 681 | + hor_overlap_inplace( | ||
| 682 | + &mut y_line_buf, | ||
| 683 | + y_stride, | ||
| 684 | + 0, | ||
| 685 | + lcol0, | ||
| 686 | + &luma_template, | ||
| 687 | + LUMA_BLOCK_W, | ||
| 688 | + luma_off_y, | ||
| 689 | + luma_off_x + if x != 0 { 2 } else { 0 }, | ||
| 690 | + lw, | ||
| 691 | + 2, | ||
| 692 | + ); | ||
| 693 | + let cw = (CHROMA_SUBBLOCK - (if x != 0 { 1 } else { 0 })).min((width - lcol0) >> 1); | ||
| 694 | + hor_overlap_inplace( | ||
| 695 | + &mut cb_line_buf, | ||
| 696 | + c_stride, | ||
| 697 | + 0, | ||
| 698 | + ccol0, | ||
| 699 | + &cb_template, | ||
| 700 | + CHROMA_BLOCK_W, | ||
| 701 | + chroma_off_y, | ||
| 702 | + chroma_off_x + if x != 0 { 1 } else { 0 }, | ||
| 703 | + cw, | ||
| 704 | + 1, | ||
| 705 | + ); | ||
| 706 | + hor_overlap_inplace( | ||
| 707 | + &mut cr_line_buf, | ||
| 708 | + c_stride, | ||
| 709 | + 0, | ||
| 710 | + ccol0, | ||
| 711 | + &cr_template, | ||
| 712 | + CHROMA_BLOCK_W, | ||
| 713 | + chroma_off_y, | ||
| 714 | + chroma_off_x + if x != 0 { 1 } else { 0 }, | ||
| 715 | + cw, | ||
| 716 | + 1, | ||
| 717 | + ); | ||
| 718 | + | ||
| 719 | + add_noise_to_block( | ||
| 720 | + fg, | ||
| 721 | + mc_identity, | ||
| 722 | + &scaling_y, | ||
| 723 | + &scaling_cb, | ||
| 724 | + &scaling_cr, | ||
| 725 | + &mut out, | ||
| 726 | + y << 1, | ||
| 727 | + x << 1, | ||
| 728 | + &y_line_buf, | ||
| 729 | + y_stride, | ||
| 730 | + 0, | ||
| 731 | + x << 1, | ||
| 732 | + &cb_line_buf, | ||
| 733 | + &cr_line_buf, | ||
| 734 | + c_stride, | ||
| 735 | + 0, | ||
| 736 | + x, | ||
| 737 | + 1, | ||
| 738 | + (LUMA_SUBBLOCK >> 1).min(width / 2 - x), | ||
| 739 | + ); | ||
| 740 | + } | ||
| 741 | + | ||
| 742 | + let i = if overlap && y != 0 { 1 } else { 0 }; | ||
| 743 | + let j = if overlap && x != 0 { 1 } else { 0 }; | ||
| 744 | + add_noise_to_block( | ||
| 745 | + fg, | ||
| 746 | + mc_identity, | ||
| 747 | + &scaling_y, | ||
| 748 | + &scaling_cb, | ||
| 749 | + &scaling_cr, | ||
| 750 | + &mut out, | ||
| 751 | + (y + i) << 1, | ||
| 752 | + (x + j) << 1, | ||
| 753 | + &luma_template, | ||
| 754 | + LUMA_BLOCK_W, | ||
| 755 | + luma_off_y + (i << 1), | ||
| 756 | + luma_off_x + (j << 1), | ||
| 757 | + &cb_template, | ||
| 758 | + &cr_template, | ||
| 759 | + CHROMA_BLOCK_W, | ||
| 760 | + chroma_off_y + i, | ||
| 761 | + chroma_off_x + j, | ||
| 762 | + (LUMA_SUBBLOCK >> 1).min(height / 2 - y) - i, | ||
| 763 | + (LUMA_SUBBLOCK >> 1).min(width / 2 - x) - j, | ||
| 764 | + ); | ||
| 765 | + | ||
| 766 | + if overlap { | ||
| 767 | + if x != 0 { | ||
| 768 | + copy_area( | ||
| 769 | + &mut y_line_buf, | ||
| 770 | + y_stride, | ||
| 771 | + 0, | ||
| 772 | + x << 1, | ||
| 773 | + &y_col_buf, | ||
| 774 | + 2, | ||
| 775 | + LUMA_SUBBLOCK, | ||
| 776 | + 0, | ||
| 777 | + 2, | ||
| 778 | + 2, | ||
| 779 | + ); | ||
| 780 | + copy_area( | ||
| 781 | + &mut cb_line_buf, | ||
| 782 | + c_stride, | ||
| 783 | + 0, | ||
| 784 | + x, | ||
| 785 | + &cb_col_buf, | ||
| 786 | + 1, | ||
| 787 | + CHROMA_SUBBLOCK, | ||
| 788 | + 0, | ||
| 789 | + 1, | ||
| 790 | + 1, | ||
| 791 | + ); | ||
| 792 | + copy_area( | ||
| 793 | + &mut cr_line_buf, | ||
| 794 | + c_stride, | ||
| 795 | + 0, | ||
| 796 | + x, | ||
| 797 | + &cr_col_buf, | ||
| 798 | + 1, | ||
| 799 | + CHROMA_SUBBLOCK, | ||
| 800 | + 0, | ||
| 801 | + 1, | ||
| 802 | + 1, | ||
| 803 | + ); | ||
| 804 | + } | ||
| 805 | + let lcol0 = if x != 0 { (x + 1) << 1 } else { 0 }; | ||
| 806 | + let ccol0 = if x != 0 { x + 1 } else { 0 }; | ||
| 807 | + let lcut = if x != 0 { 2 } else { 0 }; | ||
| 808 | + let ccut = if x != 0 { 1 } else { 0 }; | ||
| 809 | + copy_area( | ||
| 810 | + &mut y_line_buf, | ||
| 811 | + y_stride, | ||
| 812 | + 0, | ||
| 813 | + lcol0, | ||
| 814 | + &luma_template, | ||
| 815 | + LUMA_BLOCK_W, | ||
| 816 | + luma_off_y + LUMA_SUBBLOCK, | ||
| 817 | + luma_off_x + lcut, | ||
| 818 | + LUMA_SUBBLOCK.min(width - (x << 1)) - lcut, | ||
| 819 | + 2, | ||
| 820 | + ); | ||
| 821 | + copy_area( | ||
| 822 | + &mut cb_line_buf, | ||
| 823 | + c_stride, | ||
| 824 | + 0, | ||
| 825 | + ccol0, | ||
| 826 | + &cb_template, | ||
| 827 | + CHROMA_BLOCK_W, | ||
| 828 | + chroma_off_y + CHROMA_SUBBLOCK, | ||
| 829 | + chroma_off_x + ccut, | ||
| 830 | + CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, | ||
| 831 | + 1, | ||
| 832 | + ); | ||
| 833 | + copy_area( | ||
| 834 | + &mut cr_line_buf, | ||
| 835 | + c_stride, | ||
| 836 | + 0, | ||
| 837 | + ccol0, | ||
| 838 | + &cr_template, | ||
| 839 | + CHROMA_BLOCK_W, | ||
| 840 | + chroma_off_y + CHROMA_SUBBLOCK, | ||
| 841 | + chroma_off_x + ccut, | ||
| 842 | + CHROMA_SUBBLOCK.min((width - (x << 1)) >> 1) - ccut, | ||
| 843 | + 1, | ||
| 844 | + ); | ||
| 845 | + | ||
| 846 | + let h_count = (LUMA_SUBBLOCK + 2).min(height - (y << 1)); | ||
| 847 | + copy_area( | ||
| 848 | + &mut y_col_buf, | ||
| 849 | + 2, | ||
| 850 | + 0, | ||
| 851 | + 0, | ||
| 852 | + &luma_template, | ||
| 853 | + LUMA_BLOCK_W, | ||
| 854 | + luma_off_y, | ||
| 855 | + luma_off_x + LUMA_SUBBLOCK, | ||
| 856 | + 2, | ||
| 857 | + h_count, | ||
| 858 | + ); | ||
| 859 | + let hc_count = (CHROMA_SUBBLOCK + 1).min((height - (y << 1)) >> 1); | ||
| 860 | + copy_area( | ||
| 861 | + &mut cb_col_buf, | ||
| 862 | + 1, | ||
| 863 | + 0, | ||
| 864 | + 0, | ||
| 865 | + &cb_template, | ||
| 866 | + CHROMA_BLOCK_W, | ||
| 867 | + chroma_off_y, | ||
| 868 | + chroma_off_x + CHROMA_SUBBLOCK, | ||
| 869 | + 1, | ||
| 870 | + hc_count, | ||
| 871 | + ); | ||
| 872 | + copy_area( | ||
| 873 | + &mut cr_col_buf, | ||
| 874 | + 1, | ||
| 875 | + 0, | ||
| 876 | + 0, | ||
| 877 | + &cr_template, | ||
| 878 | + CHROMA_BLOCK_W, | ||
| 879 | + chroma_off_y, | ||
| 880 | + chroma_off_x + CHROMA_SUBBLOCK, | ||
| 881 | + 1, | ||
| 882 | + hc_count, | ||
| 883 | + ); | ||
| 884 | + } | ||
| 885 | + | ||
| 886 | + x += LUMA_SUBBLOCK >> 1; | ||
| 887 | + } | ||
| 888 | + y += LUMA_SUBBLOCK >> 1; | ||
| 889 | + } | ||
| 890 | + | ||
| 891 | + out | ||
| 892 | +} | ||
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -18,6 +18,7 @@ mod cdf_state; | |||
| 18 | pub mod decode; | 18 | pub mod decode; |
| 19 | pub mod encode; | 19 | pub mod encode; |
| 20 | pub mod encoder; | 20 | pub mod encoder; |
| 21 | +mod film_grain; | ||
| 21 | pub mod frame; | 22 | pub mod frame; |
| 22 | pub mod intra; | 23 | pub mod intra; |
| 23 | pub mod mc; | 24 | pub mod mc; |
crates/ec-av1/src/stream.rsmodified+175 -34
| @@ -138,28 +138,6 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 138 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", | 138 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", |
| 139 | )); | 139 | )); |
| 140 | } | 140 | } |
| 141 | - // lane-av1golden7 r9: `apply_grain` (spec 7.18.3, the decoder-side | ||
| 142 | - // synthesis `film_grain_params` drives) is never applied by this | ||
| 143 | - // crate -- it returns the coded (denoised, for content aomenc's | ||
| 144 | - // `--tune-content=film` re-grains) base picture unchanged. Traced | ||
| 145 | - // live from `fixtures/golden6-mismatch.obu`: a frame-0 intra | ||
| 146 | - // keyframe with `apply_grain=true` mismatched ffmpeg on 3855/4096 | ||
| 147 | - // luma pixels, all explained by missing synthetic grain (this | ||
| 148 | - // decoder's smooth prediction+residual output visually matches | ||
| 149 | - // ffmpeg's grain-textured one; pixel-exact only where the grain | ||
| 150 | - // scaling curve happens to hit zero). This is what a round-8 hunt | ||
| 151 | - // mistook for a GOLDEN_FRAME-adjacent bug: the golden gate only | ||
| 152 | - // ever compares streams where `non_last_ref_hits` fired, and | ||
| 153 | - // `--tune-content=film` (which turns grain synthesis on) is part of | ||
| 154 | - // that same gate's own aomenc recipe -- pure survivor bias, not | ||
| 155 | - // causation. Refuse by name rather than silently return a | ||
| 156 | - // grain-free picture. | ||
| 157 | - if header.film_grain.apply_grain { | ||
| 158 | - return Err(Error::unsupported( | ||
| 159 | - "AV1 decode_stream", | ||
| 160 | - "a frame with apply_grain set (this decoder never synthesizes film grain, spec 7.18.3)", | ||
| 161 | - )); | ||
| 162 | - } | ||
| 163 | // Loop restoration carries per-restoration-unit symbols in the tile | 141 | // Loop restoration carries per-restoration-unit symbols in the tile |
| 164 | // (spec 5.11.57 `read_lr`, one group per LR unit reached from the | 142 | // (spec 5.11.57 `read_lr`, one group per LR unit reached from the |
| 165 | // superblock walk) that this decoder never reads -- same | 143 | // superblock walk) that this decoder never reads -- same |
| @@ -336,10 +314,22 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 336 | for i in 0..NUM_REF_FRAMES { | 314 | for i in 0..NUM_REF_FRAMES { |
| 337 | if header.refresh_frame_flags & (1 << i) != 0 { | 315 | if header.refresh_frame_flags & (1 << i) != 0 { |
| 338 | cdf_slots[i] = Some(stored_cdfs.clone()); | 316 | cdf_slots[i] = Some(stored_cdfs.clone()); |
| 317 | + // Spec 7.18.3.1: synthesized grain is never stored for later | ||
| 318 | + // prediction -- the reference bank always keeps the clean, | ||
| 319 | + // pre-grain decode, even though the frame pushed onto the | ||
| 320 | + // caller's output below carries the grained picture. | ||
| 339 | ref_slots[i] = Some(picture.clone()); | 321 | ref_slots[i] = Some(picture.clone()); |
| 340 | } | 322 | } |
| 341 | } | 323 | } |
| 342 | - pictures.push(picture); | 324 | + let output = if header.film_grain.apply_grain { |
| 325 | + let mc_identity = parser | ||
| 326 | + .sequence_header() | ||
| 327 | + .is_some_and(|seq| seq.color_config.matrix_coefficients == 0); | ||
| 328 | + crate::film_grain::apply_grain(&picture, &header.film_grain, mc_identity) | ||
| 329 | + } else { | ||
| 330 | + picture | ||
| 331 | + }; | ||
| 332 | + pictures.push(output); | ||
| 343 | } | 333 | } |
| 344 | Ok(pictures) | 334 | Ok(pictures) |
| 345 | } | 335 | } |
| @@ -2428,22 +2418,173 @@ mod tests { | |||
| 2428 | /// lane-av1golden7 r9's decisive fixture: a real aomenc stream (seed 59, | 2418 | /// lane-av1golden7 r9's decisive fixture: a real aomenc stream (seed 59, |
| 2429 | /// `--tune-content=film`, GOLDEN_FRAME firing downstream) whose frame-0 | 2419 | /// `--tune-content=film`, GOLDEN_FRAME firing downstream) whose frame-0 |
| 2430 | /// *intra keyframe* mismatched ffmpeg on 3855/4096 luma pixels -- traced | 2420 | /// *intra keyframe* mismatched ffmpeg on 3855/4096 luma pixels -- traced |
| 2431 | - /// to `apply_grain=true` (spec 7.18.3 grain synthesis, never | 2421 | + /// to `apply_grain=true` (spec 7.18.3 grain synthesis, now implemented in |
| 2432 | - /// implemented here), entirely unrelated to GOLDEN_FRAME or frame | 2422 | + /// `crate::film_grain`), entirely unrelated to GOLDEN_FRAME or frame |
| 2433 | - /// ordering. `decode_stream` now refuses `apply_grain` streams by name; | 2423 | + /// ordering. Frames beyond 0 in this fixture hit an unrelated, pre-existing |
| 2434 | - /// this pins that refusal rather than a silent grain-free mismatch. | 2424 | + /// gap (`decode_inter_block`'s GOLDEN_FRAME MC path, lane-av1golden7's own |
| 2425 | + /// `golden7-forwarding-mismatch.obu`), so this test truncates the stream to | ||
| 2426 | + /// the sequence header + frame-0 OBUs only -- exactly the span the | ||
| 2427 | + /// mismatch was traced to -- rather than reaching into `decode.rs`/`mc.rs` | ||
| 2428 | + /// to widen MC support, which is out of this lane's scope. | ||
| 2435 | #[test] | 2429 | #[test] |
| 2436 | - fn a_real_aomenc_stream_with_film_grain_refuses_by_name() { | 2430 | + fn a_real_aomenc_stream_with_film_grain_decodes_pixel_exact() { |
| 2431 | + if !have_ffmpeg() { | ||
| 2432 | + eprintln!("SKIP a_real_aomenc_stream_with_film_grain_decodes_pixel_exact: no ffmpeg"); | ||
| 2433 | + return; | ||
| 2434 | + } | ||
| 2437 | let data = std::fs::read(concat!( | 2435 | let data = std::fs::read(concat!( |
| 2438 | env!("CARGO_MANIFEST_DIR"), | 2436 | env!("CARGO_MANIFEST_DIR"), |
| 2439 | "/../../fixtures/golden6-mismatch.obu" | 2437 | "/../../fixtures/golden6-mismatch.obu" |
| 2440 | )) | 2438 | )) |
| 2441 | .unwrap(); | 2439 | .unwrap(); |
| 2442 | - let err = decode_stream(&data).expect_err("apply_grain stream must be refused"); | 2440 | + // Truncate right after the first `Frame` OBU (the key frame): every |
| 2443 | - let msg = err.to_string(); | 2441 | + // byte up to there is sequence header + frame-0, which is a complete, |
| 2444 | - assert!( | 2442 | + // independently decodable single-frame stream. |
| 2445 | - msg.contains("apply_grain"), | 2443 | + let mut parser = Av1Parser::new(); |
| 2446 | - "expected an apply_grain refusal, got: {msg}" | 2444 | + let mut pos = 0usize; |
| 2447 | - ); | 2445 | + let mut frame0_end = None; |
| 2446 | + while pos < data.len() { | ||
| 2447 | + let obu = parser.parse_obu(&data[pos..]).unwrap(); | ||
| 2448 | + pos += obu.total_size; | ||
| 2449 | + if matches!(obu.kind, ObuKind::Frame(..)) { | ||
| 2450 | + frame0_end = Some(pos); | ||
| 2451 | + break; | ||
| 2452 | + } | ||
| 2453 | + } | ||
| 2454 | + let data = &data[..frame0_end.expect("fixture has at least one frame OBU")]; | ||
| 2455 | + let (width, height) = (64usize, 64usize); | ||
| 2456 | + let ffmpeg_frames = ffmpeg_decode_sequence(data, width, height, 1); | ||
| 2457 | + let decoded = decode_stream(data).unwrap(); | ||
| 2458 | + assert_eq!(decoded.len(), 1); | ||
| 2459 | + let (got, want) = (&decoded[0], &ffmpeg_frames[0]); | ||
| 2460 | + assert_eq!(got.y, want.y, "frame 0 luma vs ffmpeg (film grain)"); | ||
| 2461 | + assert_eq!(got.u, want.u, "frame 0 U vs ffmpeg (film grain)"); | ||
| 2462 | + assert_eq!(got.v, want.v, "frame 0 V vs ffmpeg (film grain)"); | ||
| 2463 | + } | ||
| 2464 | + | ||
| 2465 | + /// A real-encoder sweep, not just the one pinned fixture above | ||
| 2466 | + /// (fixture-proves-symbol-not-signal class): several fresh | ||
| 2467 | + /// `--tune-content=film` draws at different seeds/content, each decoded | ||
| 2468 | + /// pixel-exact against ffmpeg's own AV1 decoder (which synthesizes grain | ||
| 2469 | + /// by default -- confirmed via `ffmpeg -h decoder=av1`, no | ||
| 2470 | + /// `-export_side_data`/grain-disable flag is passed anywhere in this | ||
| 2471 | + /// file). | ||
| 2472 | + #[test] | ||
| 2473 | + fn real_aomenc_film_grain_streams_decode_pixel_exact() { | ||
| 2474 | + if !have_ffmpeg() { | ||
| 2475 | + eprintln!("SKIP real_aomenc_film_grain_streams_decode_pixel_exact: no ffmpeg"); | ||
| 2476 | + return; | ||
| 2477 | + } | ||
| 2478 | + if !have_aomenc() { | ||
| 2479 | + eprintln!("SKIP real_aomenc_film_grain_streams_decode_pixel_exact: no aomenc"); | ||
| 2480 | + return; | ||
| 2481 | + } | ||
| 2482 | + let (width, height) = (64usize, 64usize); | ||
| 2483 | + for seed in [1u64, 2, 3] { | ||
| 2484 | + let dir = std::env::temp_dir().join(format!( | ||
| 2485 | + "ec-av1-filmgrain-gate-{}-{seed}", | ||
| 2486 | + std::process::id() | ||
| 2487 | + )); | ||
| 2488 | + std::fs::create_dir_all(&dir).unwrap(); | ||
| 2489 | + let yuv_path = dir.join("in.yuv"); | ||
| 2490 | + let obu_path = dir.join("out.obu"); | ||
| 2491 | + let frames = 3usize; | ||
| 2492 | + let luma = width * height; | ||
| 2493 | + let chroma = luma / 4; | ||
| 2494 | + let mut yuv = Vec::with_capacity((luma + 2 * chroma) * frames); | ||
| 2495 | + for f in 0..frames { | ||
| 2496 | + let card = panned_test_card(width, height, ((f as i32 + seed as i32) * 5) as i64); | ||
| 2497 | + yuv.extend_from_slice(&card.y); | ||
| 2498 | + yuv.extend_from_slice(&card.u); | ||
| 2499 | + yuv.extend_from_slice(&card.v); | ||
| 2500 | + } | ||
| 2501 | + std::fs::write(&yuv_path, &yuv).unwrap(); | ||
| 2502 | + let status = Command::new(aomenc_path()) | ||
| 2503 | + .args([ | ||
| 2504 | + &format!("--width={width}"), | ||
| 2505 | + &format!("--height={height}"), | ||
| 2506 | + "--input-bit-depth=8", | ||
| 2507 | + "--bit-depth=8", | ||
| 2508 | + "--fps=25/1", | ||
| 2509 | + &format!("--limit={frames}"), | ||
| 2510 | + // The full constraint set of the forwarding/golden gates, | ||
| 2511 | + // so the streams stay inside this decoder's supported | ||
| 2512 | + // envelope and the gate isolates grain synthesis alone; | ||
| 2513 | + // `--tune-content=film` is the one flag under test (it | ||
| 2514 | + // enables apply_grain). | ||
| 2515 | + "--codec=av1", | ||
| 2516 | + "--passes=1", | ||
| 2517 | + "--end-usage=q", | ||
| 2518 | + "--cq-level=32", | ||
| 2519 | + "--cpu-used=0", | ||
| 2520 | + "--lag-in-frames=0", | ||
| 2521 | + "--auto-alt-ref=0", | ||
| 2522 | + "--kf-max-dist=1000", | ||
| 2523 | + "--threads=1", | ||
| 2524 | + "--row-mt=0", | ||
| 2525 | + "--max-reference-frames=3", | ||
| 2526 | + "--reduced-reference-set=1", | ||
| 2527 | + "--enable-warped-motion=0", | ||
| 2528 | + "--enable-obmc=0", | ||
| 2529 | + "--enable-masked-comp=0", | ||
| 2530 | + "--enable-interintra-comp=0", | ||
| 2531 | + "--enable-dist-wtd-comp=0", | ||
| 2532 | + "--enable-diff-wtd-comp=0", | ||
| 2533 | + "--enable-onesided-comp=0", | ||
| 2534 | + "--enable-interintra-wedge=0", | ||
| 2535 | + "--enable-smooth-interintra=0", | ||
| 2536 | + "--enable-rect-partitions=0", | ||
| 2537 | + "--enable-ab-partitions=0", | ||
| 2538 | + "--enable-1to4-partitions=0", | ||
| 2539 | + "--enable-filter-intra=0", | ||
| 2540 | + "--enable-smooth-intra=0", | ||
| 2541 | + "--enable-paeth-intra=0", | ||
| 2542 | + "--enable-directional-intra=0", | ||
| 2543 | + "--enable-angle-delta=0", | ||
| 2544 | + "--enable-tx-size-search=0", | ||
| 2545 | + "--enable-cdef=0", | ||
| 2546 | + "--enable-restoration=0", | ||
| 2547 | + "--max-partition-size=32", | ||
| 2548 | + "--min-partition-size=32", | ||
| 2549 | + "--enable-palette=0", | ||
| 2550 | + "--enable-intrabc=0", | ||
| 2551 | + "--enable-cfl-intra=0", | ||
| 2552 | + "--tune-content=film", | ||
| 2553 | + "--obu", | ||
| 2554 | + "-o", | ||
| 2555 | + ]) | ||
| 2556 | + .arg(&obu_path) | ||
| 2557 | + .arg(&yuv_path) | ||
| 2558 | + .stdout(Stdio::null()) | ||
| 2559 | + .stderr(Stdio::null()) | ||
| 2560 | + .status() | ||
| 2561 | + .expect("aomenc failed to start"); | ||
| 2562 | + assert!(status.success(), "aomenc failed for seed {seed}"); | ||
| 2563 | + let encoded = std::fs::read(&obu_path).unwrap(); | ||
| 2564 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 2565 | + | ||
| 2566 | + let mut parser = ec_av1_syntax::Av1Parser::new(); | ||
| 2567 | + let mut pos = 0usize; | ||
| 2568 | + let mut saw_grain = false; | ||
| 2569 | + while pos < encoded.len() { | ||
| 2570 | + let obu = parser.parse_obu(&encoded[pos..]).unwrap(); | ||
| 2571 | + pos += obu.total_size; | ||
| 2572 | + if let ec_av1_syntax::ObuKind::Frame(header, _) = obu.kind | ||
| 2573 | + && header.film_grain.apply_grain | ||
| 2574 | + { | ||
| 2575 | + saw_grain = true; | ||
| 2576 | + } | ||
| 2577 | + } | ||
| 2578 | + assert!(saw_grain, "seed {seed}: aomenc did not turn on apply_grain"); | ||
| 2579 | + | ||
| 2580 | + let ffmpeg_frames = ffmpeg_decode_sequence(&encoded, width, height, frames); | ||
| 2581 | + let decoded = decode_stream(&encoded).unwrap(); | ||
| 2582 | + assert_eq!(decoded.len(), frames, "seed {seed}"); | ||
| 2583 | + for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { | ||
| 2584 | + assert_eq!(got.y, want.y, "seed {seed} frame {i} luma vs ffmpeg"); | ||
| 2585 | + assert_eq!(got.u, want.u, "seed {seed} frame {i} U vs ffmpeg"); | ||
| 2586 | + assert_eq!(got.v, want.v, "seed {seed} frame {i} V vs ffmpeg"); | ||
| 2587 | + } | ||
| 2588 | + } | ||
| 2448 | } | 2589 | } |
| 2449 | } | 2590 | } |