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// Safety lints
11#![deny(bare_trait_objects)]
12#![deny(clippy::as_ptr_cast_mut)]
13#![deny(clippy::cast_ptr_alignment)]
14#![deny(clippy::large_stack_arrays)]
15#![deny(clippy::ptr_as_ptr)]
16#![deny(clippy::transmute_ptr_to_ptr)]
17#![deny(clippy::unwrap_used)]
18// Performance lints
19#![warn(clippy::cloned_instead_of_copied)]
20#![warn(clippy::inefficient_to_string)]
21#![warn(clippy::invalid_upcast_comparisons)]
22#![warn(clippy::iter_with_drain)]
23#![warn(clippy::large_types_passed_by_value)]
24#![warn(clippy::linkedlist)]
25#![warn(clippy::mutex_integer)]
26#![warn(clippy::naive_bytecount)]
27#![warn(clippy::needless_bitwise_bool)]
28#![warn(clippy::needless_collect)]
29#![warn(clippy::needless_pass_by_value)]
30#![warn(clippy::no_effect_underscore_binding)]
31#![warn(clippy::or_fun_call)]
32#![warn(clippy::stable_sort_primitive)]
33#![warn(clippy::suboptimal_flops)]
34#![warn(clippy::trivial_regex)]
35#![warn(clippy::trivially_copy_pass_by_ref)]
36#![warn(clippy::unnecessary_join)]
37#![warn(clippy::unused_async)]
38#![warn(clippy::zero_sized_map_values)]
39// Correctness lints
40#![deny(clippy::case_sensitive_file_extension_comparisons)]
41#![deny(clippy::copy_iterator)]
42#![deny(clippy::expl_impl_clone_on_copy)]
43#![deny(clippy::float_cmp)]
44#![warn(clippy::imprecise_flops)]
45#![deny(clippy::manual_instant_elapsed)]
46#![deny(clippy::match_same_arms)]
47#![deny(clippy::mem_forget)]
48#![warn(clippy::must_use_candidate)]
49#![deny(clippy::path_buf_push_overwrite)]
50#![deny(clippy::same_functions_in_if_condition)]
51#![warn(clippy::suspicious_operation_groupings)]
52#![deny(clippy::unchecked_duration_subtraction)]
53#![deny(clippy::unicode_not_nfc)]
54// Clarity/formatting lints
55#![warn(clippy::borrow_as_ptr)]
56#![warn(clippy::checked_conversions)]
57#![warn(clippy::default_trait_access)]
58#![warn(clippy::derive_partial_eq_without_eq)]
59#![warn(clippy::explicit_deref_methods)]
60#![warn(clippy::filter_map_next)]
61#![warn(clippy::flat_map_option)]
62#![warn(clippy::fn_params_excessive_bools)]
63#![warn(clippy::from_iter_instead_of_collect)]
64#![warn(clippy::if_not_else)]
65#![warn(clippy::implicit_clone)]
66#![warn(clippy::iter_not_returning_iterator)]
67#![warn(clippy::iter_on_empty_collections)]
68#![warn(clippy::macro_use_imports)]
69#![warn(clippy::manual_clamp)]
70#![warn(clippy::manual_let_else)]
71#![warn(clippy::manual_ok_or)]
72#![warn(clippy::manual_string_new)]
73#![warn(clippy::map_flatten)]
74#![warn(clippy::map_unwrap_or)]
75#![warn(clippy::match_bool)]
76#![warn(clippy::mut_mut)]
77#![warn(clippy::needless_borrow)]
78#![warn(clippy::needless_continue)]
79#![warn(clippy::option_if_let_else)]
80#![warn(clippy::range_minus_one)]
81#![warn(clippy::range_plus_one)]
82#![warn(clippy::redundant_else)]
83#![warn(clippy::ref_binding_to_reference)]
84#![warn(clippy::ref_option_ref)]
85#![warn(clippy::semicolon_if_nothing_returned)]
86#![warn(clippy::trait_duplication_in_bounds)]
87#![warn(clippy::type_repetition_in_bounds)]
88#![warn(clippy::unnested_or_patterns)]
89#![warn(clippy::unused_peekable)]
90#![warn(clippy::unused_rounding)]
91#![warn(clippy::unused_self)]
92#![warn(clippy::used_underscore_binding)]
93#![warn(clippy::verbose_bit_mask)]
94#![warn(clippy::verbose_file_reads)]
95// Documentation lints
96#![warn(clippy::doc_link_with_quotes)]
97#![warn(clippy::doc_markdown)]
98
99#[cfg(feature = "create")]
100mod create;
101#[cfg(feature = "diff")]
102mod diff;
103#[cfg(all(feature = "estimate", feature = "unstable"))]
104mod estimate;
105#[cfg(feature = "parse")]
106mod parse;
107mod util;
108
109use arrayvec::ArrayVec;
110#[cfg(feature = "create")]
111pub use create::*;
112#[cfg(feature = "diff")]
113pub use diff::*;
114#[cfg(all(feature = "estimate", feature = "unstable"))]
115pub use estimate::*;
116#[cfg(feature = "parse")]
117pub use parse::*;
118#[cfg(any(feature = "diff", feature = "estimate"))]
119pub use v_frame;
120
121/// The max number of luma scaling points for grain synthesis
122pub const NUM_Y_POINTS: usize = 14;
123/// The max number of scaling points per chroma plane for grain synthesis
124pub const NUM_UV_POINTS: usize = 10;
125/// The max number of luma coefficients for grain synthesis
126pub const NUM_Y_COEFFS: usize = 24;
127/// The max number of coefficients per chroma plane for grain synthesis
128pub const NUM_UV_COEFFS: usize = 25;
129
130/// A randomly generated u16 to be used as a starting random seed
131/// for grain synthesis. The idea behind using a constant random seed
132/// is so that encodes are deterministic and reproducible.
133pub const DEFAULT_GRAIN_SEED: u16 = 10956;
134
135pub type ScalingPoints = ArrayVec<[u8; 2], NUM_Y_POINTS>;
136
137/// Specifies parameters for enabling decoder-side grain synthesis for
138/// a segment of video from `start_time` to `end_time`.
139#[derive(Debug, Clone, PartialEq, Eq)]
140#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
141pub struct GrainTableSegment {
142    /// The beginning timestamp of this segment, in 10,000,000ths of a second.
143    pub start_time: u64,
144    /// The ending timestamp of this segment, not inclusive, in 10,000,000ths of
145    /// a second.
146    pub end_time: u64,
147
148    /// Values for the cutoffs and scale factors for luma scaling points
149    pub scaling_points_y: ArrayVec<[u8; 2], NUM_Y_POINTS>,
150    /// Values for the cutoffs and scale factors for Cb scaling points
151    pub scaling_points_cb: ArrayVec<[u8; 2], NUM_UV_POINTS>,
152    /// Values for the cutoffs and scale factors for Cr scaling points
153    pub scaling_points_cr: ArrayVec<[u8; 2], NUM_UV_POINTS>,
154
155    /// Determines the range and quantization step of the standard deviation
156    /// of film grain.
157    ///
158    /// Accepts values between `8..=11`.
159    pub scaling_shift: u8,
160
161    /// A factor specifying how many AR coefficients are provided,
162    /// based on the forumla `coeffs_len = (2 * ar_coeff_lag * (ar_coeff_lag +
163    /// 1))`.
164    ///
165    /// Accepts values between `0..=3`.
166    pub ar_coeff_lag: u8,
167    /// Values for the AR coefficients for luma scaling points
168    pub ar_coeffs_y: ArrayVec<i8, NUM_Y_COEFFS>,
169    /// Values for the AR coefficients for Cb scaling points
170    pub ar_coeffs_cb: ArrayVec<i8, NUM_UV_COEFFS>,
171    /// Values for the AR coefficients for Cr scaling points
172    pub ar_coeffs_cr: ArrayVec<i8, NUM_UV_COEFFS>,
173    /// Shift value: Specifies the range of acceptable AR coefficients
174    /// 6: [-2, 2)
175    /// 7: [-1, 1)
176    /// 8: [-0.5, 0.5)
177    /// 9: [-0.25, 0.25)
178    pub ar_coeff_shift: u8,
179    /// Multiplier to the grain strength of the Cb plane
180    pub cb_mult: u8,
181    /// Multiplier to the grain strength of the Cb plane inherited from the luma
182    /// plane
183    pub cb_luma_mult: u8,
184    /// A base value for the Cb plane grain
185    pub cb_offset: u16,
186    /// Multiplier to the grain strength of the Cr plane
187    pub cr_mult: u8,
188    /// Multiplier to the grain strength of the Cr plane inherited from the luma
189    /// plane
190    pub cr_luma_mult: u8,
191    /// A base value for the Cr plane grain
192    pub cr_offset: u16,
193
194    /// Whether film grain blocks should overlap or not
195    pub overlap_flag: bool,
196    /// Scale chroma grain from luma instead of providing chroma scaling points
197    pub chroma_scaling_from_luma: bool,
198    /// Specifies how much the Gaussian random numbers should be scaled down
199    /// during the grain synthesis process.
200    pub grain_scale_shift: u8,
201    /// Random seed used for generating grain
202    pub random_seed: u16,
203}