Branch data Line data Source code
1 : :
2 : : /*
3 : : * This file is part of libplacebo, which is normally licensed under the terms
4 : : * of the LGPL v2.1+. However, this file (film_grain.h) is also available under
5 : : * the terms of the more permissive MIT license:
6 : : *
7 : : * Copyright (c) 2018-2019 Niklas Haas
8 : : *
9 : : * Permission is hereby granted, free of charge, to any person obtaining a copy
10 : : * of this software and associated documentation files (the "Software"), to deal
11 : : * in the Software without restriction, including without limitation the rights
12 : : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 : : * copies of the Software, and to permit persons to whom the Software is
14 : : * furnished to do so, subject to the following conditions:
15 : : *
16 : : * The above copyright notice and this permission notice shall be included in all
17 : : * copies or substantial portions of the Software.
18 : : *
19 : : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 : : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 : : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 : : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 : : * SOFTWARE.
26 : : */
27 : :
28 : : #ifndef LIBPLACEBO_SHADERS_DEINTERLACING_H_
29 : : #define LIBPLACEBO_SHADERS_DEINTERLACING_H_
30 : :
31 : : #include <libplacebo/shaders.h>
32 : :
33 : : PL_API_BEGIN
34 : :
35 : : enum pl_field {
36 : : PL_FIELD_NONE = 0, // no deinterlacing
37 : : PL_FIELD_EVEN, // "top" fields, with even y coordinates
38 : : PL_FIELD_ODD, // "bottom" fields, with odd y coordinates
39 : :
40 : : // Convenience aliases
41 : : PL_FIELD_TOP = PL_FIELD_EVEN,
42 : : PL_FIELD_BOTTOM = PL_FIELD_ODD,
43 : : };
44 : :
45 : : static inline enum pl_field pl_field_other(enum pl_field field)
46 : : {
47 [ - - + ]: 80 : switch (field) {
48 : : case PL_FIELD_EVEN: return PL_FIELD_ODD;
49 : 0 : case PL_FIELD_ODD: return PL_FIELD_EVEN;
50 : 0 : default: return field;
51 : : }
52 : : }
53 : :
54 : : struct pl_field_pair {
55 : : // Top texture. If only this is specified, it's assumed to contain both
56 : : // fields in an interleaved fashion (MBAFF).
57 : : //
58 : : // Note: Support for separate fields (PAFF), is currently pending, so this
59 : : // is the only way to provide interlaced frames at the moment.
60 : : pl_tex top;
61 : : };
62 : :
63 : : #define pl_field_pair(...) ((struct pl_field_pair) { __VA_ARGS__ })
64 : :
65 : : struct pl_deinterlace_source {
66 : : // Previous, current and next source (interlaced) frames. `prev` and `next`
67 : : // may be NULL, but `cur` is required. If present, they must all have the
68 : : // exact same texture dimensions.
69 : : //
70 : : // Note: `prev` and `next` are only required for PL_DEINTERLACE_YADIF.
71 : : struct pl_field_pair prev, cur, next;
72 : :
73 : : // The parity of the current field to output. This field will be unmodified
74 : : // from `cur`, with the corresponding other field interpolated.
75 : : //
76 : : // If this is `PL_FIELD_NONE`, no deinterlacing is performed, and the
77 : : // texture is merely sampled as-is.
78 : : enum pl_field field;
79 : :
80 : : // The parity of the first frame in a stream. Set this the field that is
81 : : // (conceptually) ordered first in time.
82 : : //
83 : : // If this is `PL_FIELD_NONE`, it will instead default to `PL_FIELD_TOP`.
84 : : enum pl_field first_field;
85 : :
86 : : // Components to deinterlace. Components not specified will be ignored.
87 : : // Optional, if left as 0, all components will be deinterlaced.
88 : : uint8_t component_mask;
89 : : };
90 : :
91 : : #define pl_deinterlace_source(...) (&(struct pl_deinterlace_source) { __VA_ARGS__ })
92 : :
93 : : enum pl_deinterlace_algorithm {
94 : : // No-op deinterlacing, just sample the weaved frame un-touched.
95 : : PL_DEINTERLACE_WEAVE = 0,
96 : :
97 : : // Naive bob deinterlacing. Doubles the field lines vertically.
98 : : PL_DEINTERLACE_BOB,
99 : :
100 : : // "Yet another deinterlacing filter". Deinterlacer with temporal and
101 : : // spatial information. Based on FFmpeg's Yadif filter algorithm, but
102 : : // adapted slightly for the GPU.
103 : : PL_DEINTERLACE_YADIF,
104 : :
105 : : PL_DEINTERLACE_ALGORITHM_COUNT,
106 : : };
107 : :
108 : : // Returns whether or not an algorithm requires `prev`/`next` refs to be set.
109 : : static inline bool pl_deinterlace_needs_refs(enum pl_deinterlace_algorithm algo)
110 : : {
111 : : return algo == PL_DEINTERLACE_YADIF;
112 : : }
113 : :
114 : : struct pl_deinterlace_params {
115 : : // Algorithm to use. The recommended default is PL_DEINTERLACE_YADIF, which
116 : : // provides a good trade-off of quality and speed.
117 : : enum pl_deinterlace_algorithm algo;
118 : :
119 : : // Skip the spatial interlacing check. (PL_DEINTERLACE_YADIF only)
120 : : bool skip_spatial_check;
121 : : };
122 : :
123 : : #define PL_DEINTERLACE_DEFAULTS \
124 : : .algo = PL_DEINTERLACE_YADIF,
125 : :
126 : : #define pl_deinterlace_params(...) (&(struct pl_deinterlace_params) { PL_DEINTERLACE_DEFAULTS __VA_ARGS__ })
127 : : PL_API extern const struct pl_deinterlace_params pl_deinterlace_default_params;
128 : :
129 : : // Deinterlaces a set of interleaved source frames and outputs the result into
130 : : // `vec4 color`. If `params` is left as NULL, it defaults to
131 : : // `&pl_deinterlace_default_params`.
132 : : PL_API void pl_shader_deinterlace(pl_shader sh, const struct pl_deinterlace_source *src,
133 : : const struct pl_deinterlace_params *params);
134 : :
135 : : PL_API_END
136 : :
137 : : #endif // LIBPLACEBO_SHADERS_DEINTERLACING_H_
|