Skip to main content

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