Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2017 Richard Ling |
3 |
|
|
* |
4 |
|
|
* This file is part of FFmpeg. |
5 |
|
|
* |
6 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; either |
9 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU Lesser General Public |
17 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
/* |
22 |
|
|
* Normalize RGB video (aka histogram stretching, contrast stretching). |
23 |
|
|
* See: https://en.wikipedia.org/wiki/Normalization_(image_processing) |
24 |
|
|
* |
25 |
|
|
* For each channel of each frame, the filter computes the input range and maps |
26 |
|
|
* it linearly to the user-specified output range. The output range defaults |
27 |
|
|
* to the full dynamic range from pure black to pure white. |
28 |
|
|
* |
29 |
|
|
* Naively maximising the dynamic range of each frame of video in isolation |
30 |
|
|
* may cause flickering (rapid changes in brightness of static objects in the |
31 |
|
|
* scene) when small dark or bright objects enter or leave the scene. This |
32 |
|
|
* filter can apply temporal smoothing to the input range to reduce flickering. |
33 |
|
|
* Temporal smoothing is similar to the auto-exposure (automatic gain control) |
34 |
|
|
* on a video camera, which performs the same function; and, like a video |
35 |
|
|
* camera, it may cause a period of over- or under-exposure of the video. |
36 |
|
|
* |
37 |
|
|
* The filter can normalize the R,G,B channels independently, which may cause |
38 |
|
|
* color shifting, or link them together as a single channel, which prevents |
39 |
|
|
* color shifting. More precisely, linked normalization preserves hue (as it's |
40 |
|
|
* defined in HSV/HSL color spaces) while independent normalization does not. |
41 |
|
|
* Independent normalization can be used to remove color casts, such as the |
42 |
|
|
* blue cast from underwater video, restoring more natural colors. The filter |
43 |
|
|
* can also combine independent and linked normalization in any ratio. |
44 |
|
|
* |
45 |
|
|
* Finally the overall strength of the filter can be adjusted, from no effect |
46 |
|
|
* to full normalization. |
47 |
|
|
* |
48 |
|
|
* The 5 AVOptions are: |
49 |
|
|
* blackpt, Colors which define the output range. The minimum input value |
50 |
|
|
* whitept is mapped to the blackpt. The maximum input value is mapped to |
51 |
|
|
* the whitept. The defaults are black and white respectively. |
52 |
|
|
* Specifying white for blackpt and black for whitept will give |
53 |
|
|
* color-inverted, normalized video. Shades of grey can be used |
54 |
|
|
* to reduce the dynamic range (contrast). Specifying saturated |
55 |
|
|
* colors here can create some interesting effects. |
56 |
|
|
* |
57 |
|
|
* smoothing The amount of temporal smoothing, expressed in frames (>=0). |
58 |
|
|
* the minimum and maximum input values of each channel are |
59 |
|
|
* smoothed using a rolling average over the current frame and |
60 |
|
|
* that many previous frames of video. Defaults to 0 (no temporal |
61 |
|
|
* smoothing). |
62 |
|
|
* |
63 |
|
|
* independence |
64 |
|
|
* Controls the ratio of independent (color shifting) channel |
65 |
|
|
* normalization to linked (color preserving) normalization. 0.0 |
66 |
|
|
* is fully linked, 1.0 is fully independent. Defaults to fully |
67 |
|
|
* independent. |
68 |
|
|
* |
69 |
|
|
* strength Overall strength of the filter. 1.0 is full strength. 0.0 is |
70 |
|
|
* a rather expensive no-op. Values in between can give a gentle |
71 |
|
|
* boost to low-contrast video without creating an artificial |
72 |
|
|
* over-processed look. The default is full strength. |
73 |
|
|
*/ |
74 |
|
|
|
75 |
|
|
#include "libavutil/intreadwrite.h" |
76 |
|
|
#include "libavutil/opt.h" |
77 |
|
|
#include "libavutil/pixdesc.h" |
78 |
|
|
#include "avfilter.h" |
79 |
|
|
#include "drawutils.h" |
80 |
|
|
#include "internal.h" |
81 |
|
|
#include "video.h" |
82 |
|
|
|
83 |
|
|
typedef struct NormalizeHistory { |
84 |
|
|
uint16_t *history; // History entries. |
85 |
|
|
uint64_t history_sum; // Sum of history entries. |
86 |
|
|
} NormalizeHistory; |
87 |
|
|
|
88 |
|
|
typedef struct NormalizeLocal { |
89 |
|
|
uint16_t in; // Original input byte value for this frame. |
90 |
|
|
float smoothed; // Smoothed input value [0,255]. |
91 |
|
|
float out; // Output value [0,255] |
92 |
|
|
} NormalizeLocal; |
93 |
|
|
|
94 |
|
|
typedef struct NormalizeContext { |
95 |
|
|
const AVClass *class; |
96 |
|
|
|
97 |
|
|
// Storage for the corresponding AVOptions |
98 |
|
|
uint8_t blackpt[4]; |
99 |
|
|
uint8_t whitept[4]; |
100 |
|
|
int smoothing; |
101 |
|
|
float independence; |
102 |
|
|
float strength; |
103 |
|
|
|
104 |
|
|
uint8_t co[4]; // Offsets to R,G,B,A bytes respectively in each pixel |
105 |
|
|
int depth; |
106 |
|
|
int sblackpt[4]; |
107 |
|
|
int swhitept[4]; |
108 |
|
|
int num_components; // Number of components in the pixel format |
109 |
|
|
int step; |
110 |
|
|
int history_len; // Number of frames to average; based on smoothing factor |
111 |
|
|
int frame_num; // Increments on each frame, starting from 0. |
112 |
|
|
|
113 |
|
|
// Per-extremum, per-channel history, for temporal smoothing. |
114 |
|
|
NormalizeHistory min[3], max[3]; // Min and max for each channel in {R,G,B}. |
115 |
|
|
uint16_t *history_mem; // Single allocation for above history entries |
116 |
|
|
|
117 |
|
|
uint16_t lut[3][65536]; // Lookup table |
118 |
|
|
|
119 |
|
|
void (*find_min_max)(struct NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]); |
120 |
|
|
void (*process)(struct NormalizeContext *s, AVFrame *in, AVFrame *out); |
121 |
|
|
} NormalizeContext; |
122 |
|
|
|
123 |
|
|
#define OFFSET(x) offsetof(NormalizeContext, x) |
124 |
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
125 |
|
|
#define FLAGSR AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
126 |
|
|
|
127 |
|
|
static const AVOption normalize_options[] = { |
128 |
|
|
{ "blackpt", "output color to which darkest input color is mapped", OFFSET(blackpt), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGSR }, |
129 |
|
|
{ "whitept", "output color to which brightest input color is mapped", OFFSET(whitept), AV_OPT_TYPE_COLOR, { .str = "white" }, 0, 0, FLAGSR }, |
130 |
|
|
{ "smoothing", "amount of temporal smoothing of the input range, to reduce flicker", OFFSET(smoothing), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX/8, FLAGS }, |
131 |
|
|
{ "independence", "proportion of independent to linked channel normalization", OFFSET(independence), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 1.0, FLAGSR }, |
132 |
|
|
{ "strength", "strength of filter, from no effect to full normalization", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 1.0, FLAGSR }, |
133 |
|
|
{ NULL } |
134 |
|
|
}; |
135 |
|
|
|
136 |
|
|
AVFILTER_DEFINE_CLASS(normalize); |
137 |
|
|
|
138 |
|
✗ |
static void find_min_max(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) |
139 |
|
|
{ |
140 |
|
✗ |
for (int c = 0; c < 3; c++) |
141 |
|
✗ |
min[c].in = max[c].in = in->data[0][s->co[c]]; |
142 |
|
✗ |
for (int y = 0; y < in->height; y++) { |
143 |
|
✗ |
uint8_t *inp = in->data[0] + y * in->linesize[0]; |
144 |
|
✗ |
for (int x = 0; x < in->width; x++) { |
145 |
|
✗ |
for (int c = 0; c < 3; c++) { |
146 |
|
✗ |
min[c].in = FFMIN(min[c].in, inp[s->co[c]]); |
147 |
|
✗ |
max[c].in = FFMAX(max[c].in, inp[s->co[c]]); |
148 |
|
|
} |
149 |
|
✗ |
inp += s->step; |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
✗ |
} |
153 |
|
|
|
154 |
|
✗ |
static void process(NormalizeContext *s, AVFrame *in, AVFrame *out) |
155 |
|
|
{ |
156 |
|
✗ |
for (int y = 0; y < in->height; y++) { |
157 |
|
✗ |
uint8_t *inp = in->data[0] + y * in->linesize[0]; |
158 |
|
✗ |
uint8_t *outp = out->data[0] + y * out->linesize[0]; |
159 |
|
✗ |
for (int x = 0; x < in->width; x++) { |
160 |
|
✗ |
for (int c = 0; c < 3; c++) |
161 |
|
✗ |
outp[s->co[c]] = s->lut[c][inp[s->co[c]]]; |
162 |
|
✗ |
if (s->num_components == 4) |
163 |
|
|
// Copy alpha as-is. |
164 |
|
✗ |
outp[s->co[3]] = inp[s->co[3]]; |
165 |
|
✗ |
inp += s->step; |
166 |
|
✗ |
outp += s->step; |
167 |
|
|
} |
168 |
|
|
} |
169 |
|
✗ |
} |
170 |
|
|
|
171 |
|
✗ |
static void find_min_max_planar(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) |
172 |
|
|
{ |
173 |
|
✗ |
min[0].in = max[0].in = in->data[2][0]; |
174 |
|
✗ |
min[1].in = max[1].in = in->data[0][0]; |
175 |
|
✗ |
min[2].in = max[2].in = in->data[1][0]; |
176 |
|
✗ |
for (int y = 0; y < in->height; y++) { |
177 |
|
✗ |
uint8_t *inrp = in->data[2] + y * in->linesize[2]; |
178 |
|
✗ |
uint8_t *ingp = in->data[0] + y * in->linesize[0]; |
179 |
|
✗ |
uint8_t *inbp = in->data[1] + y * in->linesize[1]; |
180 |
|
✗ |
for (int x = 0; x < in->width; x++) { |
181 |
|
✗ |
min[0].in = FFMIN(min[0].in, inrp[x]); |
182 |
|
✗ |
max[0].in = FFMAX(max[0].in, inrp[x]); |
183 |
|
✗ |
min[1].in = FFMIN(min[1].in, ingp[x]); |
184 |
|
✗ |
max[1].in = FFMAX(max[1].in, ingp[x]); |
185 |
|
✗ |
min[2].in = FFMIN(min[2].in, inbp[x]); |
186 |
|
✗ |
max[2].in = FFMAX(max[2].in, inbp[x]); |
187 |
|
|
} |
188 |
|
|
} |
189 |
|
✗ |
} |
190 |
|
|
|
191 |
|
✗ |
static void process_planar(NormalizeContext *s, AVFrame *in, AVFrame *out) |
192 |
|
|
{ |
193 |
|
✗ |
for (int y = 0; y < in->height; y++) { |
194 |
|
✗ |
uint8_t *inrp = in->data[2] + y * in->linesize[2]; |
195 |
|
✗ |
uint8_t *ingp = in->data[0] + y * in->linesize[0]; |
196 |
|
✗ |
uint8_t *inbp = in->data[1] + y * in->linesize[1]; |
197 |
|
✗ |
uint8_t *inap = in->data[3] + y * in->linesize[3]; |
198 |
|
✗ |
uint8_t *outrp = out->data[2] + y * out->linesize[2]; |
199 |
|
✗ |
uint8_t *outgp = out->data[0] + y * out->linesize[0]; |
200 |
|
✗ |
uint8_t *outbp = out->data[1] + y * out->linesize[1]; |
201 |
|
✗ |
uint8_t *outap = out->data[3] + y * out->linesize[3]; |
202 |
|
✗ |
for (int x = 0; x < in->width; x++) { |
203 |
|
✗ |
outrp[x] = s->lut[0][inrp[x]]; |
204 |
|
✗ |
outgp[x] = s->lut[1][ingp[x]]; |
205 |
|
✗ |
outbp[x] = s->lut[2][inbp[x]]; |
206 |
|
✗ |
if (s->num_components == 4) |
207 |
|
✗ |
outap[x] = inap[x]; |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
✗ |
} |
211 |
|
|
|
212 |
|
✗ |
static void find_min_max_16(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) |
213 |
|
|
{ |
214 |
|
✗ |
for (int c = 0; c < 3; c++) |
215 |
|
✗ |
min[c].in = max[c].in = AV_RN16(in->data[0] + 2 * s->co[c]); |
216 |
|
✗ |
for (int y = 0; y < in->height; y++) { |
217 |
|
✗ |
uint16_t *inp = (uint16_t *)(in->data[0] + y * in->linesize[0]); |
218 |
|
✗ |
for (int x = 0; x < in->width; x++) { |
219 |
|
✗ |
for (int c = 0; c < 3; c++) { |
220 |
|
✗ |
min[c].in = FFMIN(min[c].in, inp[s->co[c]]); |
221 |
|
✗ |
max[c].in = FFMAX(max[c].in, inp[s->co[c]]); |
222 |
|
|
} |
223 |
|
✗ |
inp += s->step; |
224 |
|
|
} |
225 |
|
|
} |
226 |
|
✗ |
} |
227 |
|
|
|
228 |
|
✗ |
static void process_16(NormalizeContext *s, AVFrame *in, AVFrame *out) |
229 |
|
|
{ |
230 |
|
✗ |
for (int y = 0; y < in->height; y++) { |
231 |
|
✗ |
uint16_t *inp = (uint16_t *)(in->data[0] + y * in->linesize[0]); |
232 |
|
✗ |
uint16_t *outp = (uint16_t *)(out->data[0] + y * out->linesize[0]); |
233 |
|
✗ |
for (int x = 0; x < in->width; x++) { |
234 |
|
✗ |
for (int c = 0; c < 3; c++) |
235 |
|
✗ |
outp[s->co[c]] = s->lut[c][inp[s->co[c]]]; |
236 |
|
✗ |
if (s->num_components == 4) |
237 |
|
|
// Copy alpha as-is. |
238 |
|
✗ |
outp[s->co[3]] = inp[s->co[3]]; |
239 |
|
✗ |
inp += s->step; |
240 |
|
✗ |
outp += s->step; |
241 |
|
|
} |
242 |
|
|
} |
243 |
|
✗ |
} |
244 |
|
|
|
245 |
|
✗ |
static void find_min_max_planar_16(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]) |
246 |
|
|
{ |
247 |
|
✗ |
min[0].in = max[0].in = AV_RN16(in->data[2]); |
248 |
|
✗ |
min[1].in = max[1].in = AV_RN16(in->data[0]); |
249 |
|
✗ |
min[2].in = max[2].in = AV_RN16(in->data[1]); |
250 |
|
✗ |
for (int y = 0; y < in->height; y++) { |
251 |
|
✗ |
uint16_t *inrp = (uint16_t *)(in->data[2] + y * in->linesize[2]); |
252 |
|
✗ |
uint16_t *ingp = (uint16_t *)(in->data[0] + y * in->linesize[0]); |
253 |
|
✗ |
uint16_t *inbp = (uint16_t *)(in->data[1] + y * in->linesize[1]); |
254 |
|
✗ |
for (int x = 0; x < in->width; x++) { |
255 |
|
✗ |
min[0].in = FFMIN(min[0].in, inrp[x]); |
256 |
|
✗ |
max[0].in = FFMAX(max[0].in, inrp[x]); |
257 |
|
✗ |
min[1].in = FFMIN(min[1].in, ingp[x]); |
258 |
|
✗ |
max[1].in = FFMAX(max[1].in, ingp[x]); |
259 |
|
✗ |
min[2].in = FFMIN(min[2].in, inbp[x]); |
260 |
|
✗ |
max[2].in = FFMAX(max[2].in, inbp[x]); |
261 |
|
|
} |
262 |
|
|
} |
263 |
|
✗ |
} |
264 |
|
|
|
265 |
|
✗ |
static void process_planar_16(NormalizeContext *s, AVFrame *in, AVFrame *out) |
266 |
|
|
{ |
267 |
|
✗ |
for (int y = 0; y < in->height; y++) { |
268 |
|
✗ |
uint16_t *inrp = (uint16_t *)(in->data[2] + y * in->linesize[2]); |
269 |
|
✗ |
uint16_t *ingp = (uint16_t *)(in->data[0] + y * in->linesize[0]); |
270 |
|
✗ |
uint16_t *inbp = (uint16_t *)(in->data[1] + y * in->linesize[1]); |
271 |
|
✗ |
uint16_t *inap = (uint16_t *)(in->data[3] + y * in->linesize[3]); |
272 |
|
✗ |
uint16_t *outrp = (uint16_t *)(out->data[2] + y * out->linesize[2]); |
273 |
|
✗ |
uint16_t *outgp = (uint16_t *)(out->data[0] + y * out->linesize[0]); |
274 |
|
✗ |
uint16_t *outbp = (uint16_t *)(out->data[1] + y * out->linesize[1]); |
275 |
|
✗ |
uint16_t *outap = (uint16_t *)(out->data[3] + y * out->linesize[3]); |
276 |
|
✗ |
for (int x = 0; x < in->width; x++) { |
277 |
|
✗ |
outrp[x] = s->lut[0][inrp[x]]; |
278 |
|
✗ |
outgp[x] = s->lut[1][ingp[x]]; |
279 |
|
✗ |
outbp[x] = s->lut[2][inbp[x]]; |
280 |
|
✗ |
if (s->num_components == 4) |
281 |
|
✗ |
outap[x] = inap[x]; |
282 |
|
|
} |
283 |
|
|
} |
284 |
|
✗ |
} |
285 |
|
|
|
286 |
|
|
// This function is the main guts of the filter. Normalizes the input frame |
287 |
|
|
// into the output frame. The frames are known to have the same dimensions |
288 |
|
|
// and pixel format. |
289 |
|
✗ |
static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) |
290 |
|
|
{ |
291 |
|
|
// Per-extremum, per-channel local variables. |
292 |
|
|
NormalizeLocal min[3], max[3]; // Min and max for each channel in {R,G,B}. |
293 |
|
|
|
294 |
|
|
float rgb_min_smoothed; // Min input range for linked normalization |
295 |
|
|
float rgb_max_smoothed; // Max input range for linked normalization |
296 |
|
|
int c; |
297 |
|
|
|
298 |
|
|
// First, scan the input frame to find, for each channel, the minimum |
299 |
|
|
// (min.in) and maximum (max.in) values present in the channel. |
300 |
|
✗ |
s->find_min_max(s, in, min, max); |
301 |
|
|
|
302 |
|
|
// Next, for each channel, push min.in and max.in into their respective |
303 |
|
|
// histories, to determine the min.smoothed and max.smoothed for this frame. |
304 |
|
|
{ |
305 |
|
✗ |
int history_idx = s->frame_num % s->history_len; |
306 |
|
|
// Assume the history is not yet full; num_history_vals is the number |
307 |
|
|
// of frames received so far including the current frame. |
308 |
|
✗ |
int num_history_vals = s->frame_num + 1; |
309 |
|
✗ |
if (s->frame_num >= s->history_len) { |
310 |
|
|
//The history is full; drop oldest value and cap num_history_vals. |
311 |
|
✗ |
for (c = 0; c < 3; c++) { |
312 |
|
✗ |
s->min[c].history_sum -= s->min[c].history[history_idx]; |
313 |
|
✗ |
s->max[c].history_sum -= s->max[c].history[history_idx]; |
314 |
|
|
} |
315 |
|
✗ |
num_history_vals = s->history_len; |
316 |
|
|
} |
317 |
|
|
// For each extremum, update history_sum and calculate smoothed value |
318 |
|
|
// as the rolling average of the history entries. |
319 |
|
✗ |
for (c = 0; c < 3; c++) { |
320 |
|
✗ |
s->min[c].history_sum += (s->min[c].history[history_idx] = min[c].in); |
321 |
|
✗ |
min[c].smoothed = s->min[c].history_sum / (float)num_history_vals; |
322 |
|
✗ |
s->max[c].history_sum += (s->max[c].history[history_idx] = max[c].in); |
323 |
|
✗ |
max[c].smoothed = s->max[c].history_sum / (float)num_history_vals; |
324 |
|
|
} |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
// Determine the input range for linked normalization. This is simply the |
328 |
|
|
// minimum of the per-channel minimums, and the maximum of the per-channel |
329 |
|
|
// maximums. |
330 |
|
✗ |
rgb_min_smoothed = FFMIN3(min[0].smoothed, min[1].smoothed, min[2].smoothed); |
331 |
|
✗ |
rgb_max_smoothed = FFMAX3(max[0].smoothed, max[1].smoothed, max[2].smoothed); |
332 |
|
|
|
333 |
|
|
// Now, process each channel to determine the input and output range and |
334 |
|
|
// build the lookup tables. |
335 |
|
✗ |
for (c = 0; c < 3; c++) { |
336 |
|
|
int in_val; |
337 |
|
|
// Adjust the input range for this channel [min.smoothed,max.smoothed] |
338 |
|
|
// by mixing in the correct proportion of the linked normalization |
339 |
|
|
// input range [rgb_min_smoothed,rgb_max_smoothed]. |
340 |
|
✗ |
min[c].smoothed = (min[c].smoothed * s->independence) |
341 |
|
✗ |
+ (rgb_min_smoothed * (1.0f - s->independence)); |
342 |
|
✗ |
max[c].smoothed = (max[c].smoothed * s->independence) |
343 |
|
✗ |
+ (rgb_max_smoothed * (1.0f - s->independence)); |
344 |
|
|
|
345 |
|
|
// Calculate the output range [min.out,max.out] as a ratio of the full- |
346 |
|
|
// strength output range [blackpt,whitept] and the original input range |
347 |
|
|
// [min.in,max.in], based on the user-specified filter strength. |
348 |
|
✗ |
min[c].out = (s->sblackpt[c] * s->strength) |
349 |
|
✗ |
+ (min[c].in * (1.0f - s->strength)); |
350 |
|
✗ |
max[c].out = (s->swhitept[c] * s->strength) |
351 |
|
✗ |
+ (max[c].in * (1.0f - s->strength)); |
352 |
|
|
|
353 |
|
|
// Now, build a lookup table which linearly maps the adjusted input range |
354 |
|
|
// [min.smoothed,max.smoothed] to the output range [min.out,max.out]. |
355 |
|
|
// Perform the linear interpolation for each x: |
356 |
|
|
// lut[x] = (int)(float(x - min.smoothed) * scale + max.out + 0.5) |
357 |
|
|
// where scale = (max.out - min.out) / (max.smoothed - min.smoothed) |
358 |
|
✗ |
if (min[c].smoothed == max[c].smoothed) { |
359 |
|
|
// There is no dynamic range to expand. No mapping for this channel. |
360 |
|
✗ |
for (in_val = min[c].in; in_val <= max[c].in; in_val++) |
361 |
|
✗ |
s->lut[c][in_val] = min[c].out; |
362 |
|
|
} else { |
363 |
|
|
// We must set lookup values for all values in the original input |
364 |
|
|
// range [min.in,max.in]. Since the original input range may be |
365 |
|
|
// larger than [min.smoothed,max.smoothed], some output values may |
366 |
|
|
// fall outside the [0,255] dynamic range. We need to clamp them. |
367 |
|
✗ |
float scale = (max[c].out - min[c].out) / (max[c].smoothed - min[c].smoothed); |
368 |
|
✗ |
for (in_val = min[c].in; in_val <= max[c].in; in_val++) { |
369 |
|
✗ |
int out_val = (in_val - min[c].smoothed) * scale + min[c].out + 0.5f; |
370 |
|
✗ |
out_val = av_clip_uintp2_c(out_val, s->depth); |
371 |
|
✗ |
s->lut[c][in_val] = out_val; |
372 |
|
|
} |
373 |
|
|
} |
374 |
|
|
} |
375 |
|
|
|
376 |
|
|
// Finally, process the pixels of the input frame using the lookup tables. |
377 |
|
✗ |
s->process(s, in, out); |
378 |
|
|
|
379 |
|
✗ |
s->frame_num++; |
380 |
|
✗ |
} |
381 |
|
|
|
382 |
|
|
// Now we define all the functions accessible from the ff_vf_normalize class, |
383 |
|
|
// which is ffmpeg's interface to our filter. See doc/filter_design.txt and |
384 |
|
|
// doc/writing_filters.txt for descriptions of what these interface functions |
385 |
|
|
// are expected to do. |
386 |
|
|
|
387 |
|
|
// The pixel formats that our filter supports. We should be able to process |
388 |
|
|
// any 8-bit RGB formats. 16-bit support might be useful one day. |
389 |
|
|
static const enum AVPixelFormat pixel_fmts[] = { |
390 |
|
|
AV_PIX_FMT_RGB24, |
391 |
|
|
AV_PIX_FMT_BGR24, |
392 |
|
|
AV_PIX_FMT_ARGB, |
393 |
|
|
AV_PIX_FMT_RGBA, |
394 |
|
|
AV_PIX_FMT_ABGR, |
395 |
|
|
AV_PIX_FMT_BGRA, |
396 |
|
|
AV_PIX_FMT_0RGB, |
397 |
|
|
AV_PIX_FMT_RGB0, |
398 |
|
|
AV_PIX_FMT_0BGR, |
399 |
|
|
AV_PIX_FMT_BGR0, |
400 |
|
|
AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48, |
401 |
|
|
AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64, |
402 |
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
403 |
|
|
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
404 |
|
|
AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, |
405 |
|
|
AV_PIX_FMT_NONE |
406 |
|
|
}; |
407 |
|
|
|
408 |
|
|
// At this point we know the pixel format used for both input and output. We |
409 |
|
|
// can also access the frame rate of the input video and allocate some memory |
410 |
|
|
// appropriately |
411 |
|
✗ |
static int config_input(AVFilterLink *inlink) |
412 |
|
|
{ |
413 |
|
✗ |
NormalizeContext *s = inlink->dst->priv; |
414 |
|
|
// Store offsets to R,G,B,A bytes respectively in each pixel |
415 |
|
✗ |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
416 |
|
|
int c, planar, scale; |
417 |
|
|
|
418 |
|
✗ |
ff_fill_rgba_map(s->co, inlink->format); |
419 |
|
✗ |
s->depth = desc->comp[0].depth; |
420 |
|
✗ |
scale = 1 << (s->depth - 8); |
421 |
|
✗ |
s->num_components = desc->nb_components; |
422 |
|
✗ |
s->step = av_get_padded_bits_per_pixel(desc) >> (3 + (s->depth > 8)); |
423 |
|
|
// Convert smoothing value to history_len (a count of frames to average, |
424 |
|
|
// must be at least 1). Currently this is a direct assignment, but the |
425 |
|
|
// smoothing value was originally envisaged as a number of seconds. In |
426 |
|
|
// future it would be nice to set history_len using a number of seconds, |
427 |
|
|
// but VFR video is currently an obstacle to doing so. |
428 |
|
✗ |
s->history_len = s->smoothing + 1; |
429 |
|
|
// Allocate the history buffers -- there are 6 -- one for each extrema. |
430 |
|
|
// s->smoothing is limited to INT_MAX/8, so that (s->history_len * 6) |
431 |
|
|
// can't overflow on 32bit causing a too-small allocation. |
432 |
|
✗ |
s->history_mem = av_malloc(s->history_len * 6 * sizeof(*s->history_mem)); |
433 |
|
✗ |
if (s->history_mem == NULL) |
434 |
|
✗ |
return AVERROR(ENOMEM); |
435 |
|
|
|
436 |
|
✗ |
for (c = 0; c < 3; c++) { |
437 |
|
✗ |
s->min[c].history = s->history_mem + (c*2) * s->history_len; |
438 |
|
✗ |
s->max[c].history = s->history_mem + (c*2+1) * s->history_len; |
439 |
|
✗ |
s->sblackpt[c] = scale * s->blackpt[c] + (s->blackpt[c] & (1 << (s->depth - 8))); |
440 |
|
✗ |
s->swhitept[c] = scale * s->whitept[c] + (s->whitept[c] & (1 << (s->depth - 8))); |
441 |
|
|
} |
442 |
|
|
|
443 |
|
✗ |
planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR; |
444 |
|
|
|
445 |
|
✗ |
if (s->depth <= 8) { |
446 |
|
✗ |
s->find_min_max = planar ? find_min_max_planar : find_min_max; |
447 |
|
✗ |
s->process = planar? process_planar : process; |
448 |
|
|
} else { |
449 |
|
✗ |
s->find_min_max = planar ? find_min_max_planar_16 : find_min_max_16; |
450 |
|
✗ |
s->process = planar? process_planar_16 : process_16; |
451 |
|
|
} |
452 |
|
|
|
453 |
|
✗ |
return 0; |
454 |
|
|
} |
455 |
|
|
|
456 |
|
|
// Free any memory allocations here |
457 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
458 |
|
|
{ |
459 |
|
✗ |
NormalizeContext *s = ctx->priv; |
460 |
|
|
|
461 |
|
✗ |
av_freep(&s->history_mem); |
462 |
|
✗ |
} |
463 |
|
|
|
464 |
|
|
// This function is pretty much standard from doc/writing_filters.txt. It |
465 |
|
|
// tries to do in-place filtering where possible, only allocating a new output |
466 |
|
|
// frame when absolutely necessary. |
467 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
468 |
|
|
{ |
469 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
470 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
471 |
|
✗ |
NormalizeContext *s = ctx->priv; |
472 |
|
|
AVFrame *out; |
473 |
|
|
// Set 'direct' if we can modify the input frame in-place. Otherwise we |
474 |
|
|
// need to retrieve a new frame from the output link. |
475 |
|
✗ |
int direct = av_frame_is_writable(in) && !ctx->is_disabled; |
476 |
|
|
|
477 |
|
✗ |
if (direct) { |
478 |
|
✗ |
out = in; |
479 |
|
|
} else { |
480 |
|
✗ |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
481 |
|
✗ |
if (!out) { |
482 |
|
✗ |
av_frame_free(&in); |
483 |
|
✗ |
return AVERROR(ENOMEM); |
484 |
|
|
} |
485 |
|
✗ |
av_frame_copy_props(out, in); |
486 |
|
|
} |
487 |
|
|
|
488 |
|
|
// Now we've got the input and output frames (which may be the same frame) |
489 |
|
|
// perform the filtering with our custom function. |
490 |
|
✗ |
normalize(s, in, out); |
491 |
|
|
|
492 |
|
✗ |
if (ctx->is_disabled) { |
493 |
|
✗ |
av_frame_free(&out); |
494 |
|
✗ |
return ff_filter_frame(outlink, in); |
495 |
|
|
} |
496 |
|
|
|
497 |
|
✗ |
if (!direct) |
498 |
|
✗ |
av_frame_free(&in); |
499 |
|
|
|
500 |
|
✗ |
return ff_filter_frame(outlink, out); |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
static const AVFilterPad inputs[] = { |
504 |
|
|
{ |
505 |
|
|
.name = "default", |
506 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
507 |
|
|
.filter_frame = filter_frame, |
508 |
|
|
.config_props = config_input, |
509 |
|
|
}, |
510 |
|
|
}; |
511 |
|
|
|
512 |
|
|
const AVFilter ff_vf_normalize = { |
513 |
|
|
.name = "normalize", |
514 |
|
|
.description = NULL_IF_CONFIG_SMALL("Normalize RGB video."), |
515 |
|
|
.priv_size = sizeof(NormalizeContext), |
516 |
|
|
.priv_class = &normalize_class, |
517 |
|
|
.uninit = uninit, |
518 |
|
|
FILTER_INPUTS(inputs), |
519 |
|
|
FILTER_OUTPUTS(ff_video_default_filterpad), |
520 |
|
|
FILTER_PIXFMTS_ARRAY(pixel_fmts), |
521 |
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, |
522 |
|
|
.process_command = ff_filter_process_command, |
523 |
|
|
}; |
524 |
|
|
|