av1_grain/
lib.rs

1// Copyright (c) 2022-2022, The rav1e contributors. All rights reserved
2//
3// This source code is subject to the terms of the BSD 2 Clause License and
4// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
5// was not distributed with this source code in the LICENSE file, you can
6// obtain it at www.aomedia.org/license/software. If the Alliance for Open
7// Media Patent License 1.0 was not distributed with this source code in the
8// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9
10#[cfg(feature = "create")]
11mod create;
12#[cfg(feature = "diff")]
13mod diff;
14#[cfg(all(feature = "estimate", feature = "unstable"))]
15mod estimate;
16#[cfg(feature = "parse")]
17mod parse;
18mod util;
19
20use arrayvec::ArrayVec;
21#[cfg(feature = "create")]
22pub use create::*;
23#[cfg(feature = "diff")]
24pub use diff::*;
25#[cfg(all(feature = "estimate", feature = "unstable"))]
26pub use estimate::*;
27#[cfg(feature = "parse")]
28pub use parse::*;
29#[cfg(any(feature = "diff", feature = "estimate"))]
30pub use v_frame;
31
32/// The max number of luma scaling points for grain synthesis
33pub const NUM_Y_POINTS: usize = 14;
34/// The max number of scaling points per chroma plane for grain synthesis
35pub const NUM_UV_POINTS: usize = 10;
36/// The max number of luma coefficients for grain synthesis
37pub const NUM_Y_COEFFS: usize = 24;
38/// The max number of coefficients per chroma plane for grain synthesis
39pub const NUM_UV_COEFFS: usize = 25;
40
41/// A randomly generated u16 to be used as a starting random seed
42/// for grain synthesis. The idea behind using a constant random seed
43/// is so that encodes are deterministic and reproducible.
44pub const DEFAULT_GRAIN_SEED: u16 = 10956;
45
46pub type ScalingPoints = ArrayVec<[u8; 2], NUM_Y_POINTS>;
47
48/// Specifies parameters for enabling decoder-side grain synthesis for
49/// a segment of video from `start_time` to `end_time`.
50#[derive(Debug, Clone, PartialEq, Eq)]
51#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
52pub struct GrainTableSegment {
53    /// The beginning timestamp of this segment, in 10,000,000ths of a second.
54    pub start_time: u64,
55    /// The ending timestamp of this segment, not inclusive, in 10,000,000ths of
56    /// a second.
57    pub end_time: u64,
58
59    /// Values for the cutoffs and scale factors for luma scaling points
60    pub scaling_points_y: ArrayVec<[u8; 2], NUM_Y_POINTS>,
61    /// Values for the cutoffs and scale factors for Cb scaling points
62    pub scaling_points_cb: ArrayVec<[u8; 2], NUM_UV_POINTS>,
63    /// Values for the cutoffs and scale factors for Cr scaling points
64    pub scaling_points_cr: ArrayVec<[u8; 2], NUM_UV_POINTS>,
65
66    /// Determines the range and quantization step of the standard deviation
67    /// of film grain.
68    ///
69    /// Accepts values between `8..=11`.
70    pub scaling_shift: u8,
71
72    /// A factor specifying how many AR coefficients are provided,
73    /// based on the forumla `coeffs_len = (2 * ar_coeff_lag * (ar_coeff_lag +
74    /// 1))`.
75    ///
76    /// Accepts values between `0..=3`.
77    pub ar_coeff_lag: u8,
78    /// Values for the AR coefficients for luma scaling points
79    pub ar_coeffs_y: ArrayVec<i8, NUM_Y_COEFFS>,
80    /// Values for the AR coefficients for Cb scaling points
81    pub ar_coeffs_cb: ArrayVec<i8, NUM_UV_COEFFS>,
82    /// Values for the AR coefficients for Cr scaling points
83    pub ar_coeffs_cr: ArrayVec<i8, NUM_UV_COEFFS>,
84    /// Shift value: Specifies the range of acceptable AR coefficients
85    /// 6: [-2, 2)
86    /// 7: [-1, 1)
87    /// 8: [-0.5, 0.5)
88    /// 9: [-0.25, 0.25)
89    pub ar_coeff_shift: u8,
90    /// Multiplier to the grain strength of the Cb plane
91    pub cb_mult: u8,
92    /// Multiplier to the grain strength of the Cb plane inherited from the luma
93    /// plane
94    pub cb_luma_mult: u8,
95    /// A base value for the Cb plane grain
96    pub cb_offset: u16,
97    /// Multiplier to the grain strength of the Cr plane
98    pub cr_mult: u8,
99    /// Multiplier to the grain strength of the Cr plane inherited from the luma
100    /// plane
101    pub cr_luma_mult: u8,
102    /// A base value for the Cr plane grain
103    pub cr_offset: u16,
104
105    /// Whether film grain blocks should overlap or not
106    pub overlap_flag: bool,
107    /// Scale chroma grain from luma instead of providing chroma scaling points
108    pub chroma_scaling_from_luma: bool,
109    /// Specifies how much the Gaussian random numbers should be scaled down
110    /// during the grain synthesis process.
111    pub grain_scale_shift: u8,
112    /// Random seed used for generating grain
113    pub random_seed: u16,
114}