/src/vvdec/source/Lib/FilmGrain/FilmGrain.h
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | The copyright in this software is being made available under the Clear BSD |
3 | | License, included below. No patent rights, trademark rights and/or |
4 | | other Intellectual Property Rights other than the copyrights concerning |
5 | | the Software are granted under this license. |
6 | | |
7 | | The Clear BSD License |
8 | | |
9 | | Copyright (c) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC Authors. |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without modification, |
13 | | are permitted (subject to the limitations in the disclaimer below) provided that |
14 | | the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the copyright holder nor the names of its |
24 | | contributors may be used to endorse or promote products derived from this |
25 | | software without specific prior written permission. |
26 | | |
27 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
28 | | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
29 | | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
31 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
32 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
33 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
34 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
35 | | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
36 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | |
41 | | ------------------------------------------------------------------------------------------- */ |
42 | | |
43 | | /* This file is based on VFGS, available on |
44 | | * https://github.com/InterDigitalInc/VersatileFilmGrain |
45 | | * |
46 | | * VFGS implements film grain synthesis as a hardware model: it simulates the |
47 | | * output of a cost-effective hardware implementation in a video display |
48 | | * pipeline. Also, the C code is split into "fw" (firmware) and "hw" (hardware) |
49 | | * parts, and as self-explanatory as possible. See VFGS github repository for |
50 | | * more details. |
51 | | * |
52 | | * The VFGS github repository also contains other tools to experiment with film |
53 | | * grain synthesis (e.g. a graphical display and tuning tool for FGC SEI |
54 | | * message). |
55 | | */ |
56 | | |
57 | | #pragma once |
58 | | |
59 | | #include "FilmGrainImpl.h" |
60 | | |
61 | | #include <cstring> |
62 | | #include <vector> |
63 | | #include <memory> |
64 | | |
65 | | #include "vvdec/sei.h" |
66 | | #include "vvdec/vvdec.h" |
67 | | |
68 | | |
69 | | namespace vvdec |
70 | | { |
71 | | |
72 | 0 | #define SEI_MAX_MODEL_VALUES 6 |
73 | | |
74 | | struct fgs_sei |
75 | | { |
76 | 0 | fgs_sei() { memset( this, 0, sizeof( *this ) ); } |
77 | | |
78 | | uint8_t model_id; |
79 | | uint8_t log2_scale_factor; |
80 | | uint8_t comp_model_present_flag[3]; |
81 | | uint16_t num_intensity_intervals[3]; |
82 | | uint8_t num_model_values[3]; |
83 | | uint8_t intensity_interval_lower_bound[3][256]; |
84 | | uint8_t intensity_interval_upper_bound[3][256]; |
85 | | int16_t comp_model_value[3][256][SEI_MAX_MODEL_VALUES]; |
86 | | }; |
87 | | |
88 | | class FilmGrain |
89 | | { |
90 | | std::unique_ptr<FilmGrainImpl> m_impl; |
91 | | |
92 | | uint32_t m_line_rnd = 0xdeadbeef; |
93 | | uint32_t m_line_rnd_up = 0xdeadbeef; |
94 | | uint32_t m_prev_frame_line_rnd_up = 0xdeadbeef; |
95 | | |
96 | | std::vector<uint32_t> m_line_seeds; |
97 | | fgs_sei fgs; |
98 | | |
99 | | public: |
100 | | FilmGrain(); |
101 | 0 | ~FilmGrain() = default; |
102 | | |
103 | | void updateFGC( vvdecSEIFilmGrainCharacteristics* fgc ); |
104 | 0 | void setDepth( int depth ) { m_impl->set_depth( depth ); } |
105 | | void setColorFormat( vvdecColorFormat fmt ); |
106 | | void prepareBlockSeeds( int width, int height ); |
107 | | |
108 | | void add_grain_line( void* Y, void* U, void* V, int y, int width ); |
109 | | |
110 | | private: |
111 | | void set_seed( uint32_t seed ); |
112 | | void init_sei(); |
113 | | }; |
114 | | |
115 | | } // namespace vvdec |