History

avfilter/vf_normalize: add support for >8 depth

Paul B Mahol committed 6 years ago

Commit feeef2f1 parent ac511731 file · +105 −16Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_normalize.c+105−16
@@ -73,6 +73,7 @@ · 72 unmodified lines
73 */73 */
74 74
75#include "libavutil/imgutils.h"75#include "libavutil/imgutils.h"
76#include "libavutil/intreadwrite.h"
76#include "libavutil/opt.h"77#include "libavutil/opt.h"
77#include "libavutil/pixdesc.h"78#include "libavutil/pixdesc.h"
78#include "avfilter.h"79#include "avfilter.h"
@@ -82,12 +83,12 @@ · 3 unmodified lines
82#include "video.h"83#include "video.h"
83 84
84typedef struct NormalizeHistory {85typedef struct NormalizeHistory {
85 uint8_t *history; // History entries.86 uint16_t *history; // History entries.
86 uint32_t history_sum; // Sum of history entries.87 uint64_t history_sum; // Sum of history entries.
87} NormalizeHistory;88} NormalizeHistory;
88 89
89typedef struct NormalizeLocal {90typedef struct NormalizeLocal {
90 uint8_t in; // Original input byte value for this frame.91 uint16_t in; // Original input byte value for this frame.
91 float smoothed; // Smoothed input value [0,255].92 float smoothed; // Smoothed input value [0,255].
92 float out; // Output value [0,255]93 float out; // Output value [0,255]
93} NormalizeLocal;94} NormalizeLocal;
@@ -103,6 +104,9 @@ typedef struct NormalizeContext { · 9 unmodified lines
103 float strength;104 float strength;
104 105
105 uint8_t co[4]; // Offsets to R,G,B,A bytes respectively in each pixel106 uint8_t co[4]; // Offsets to R,G,B,A bytes respectively in each pixel
107 int depth;
108 int sblackpt[4];
109 int swhitept[4];
106 int num_components; // Number of components in the pixel format110 int num_components; // Number of components in the pixel format
107 int step;111 int step;
108 int history_len; // Number of frames to average; based on smoothing factor112 int history_len; // Number of frames to average; based on smoothing factor
@@ -110,9 +114,9 @@ typedef struct NormalizeContext { · 1 unmodified line
110 114
111 // Per-extremum, per-channel history, for temporal smoothing.115 // Per-extremum, per-channel history, for temporal smoothing.
112 NormalizeHistory min[3], max[3]; // Min and max for each channel in {R,G,B}.116 NormalizeHistory min[3], max[3]; // Min and max for each channel in {R,G,B}.
113 uint8_t *history_mem; // Single allocation for above history entries117 uint16_t *history_mem; // Single allocation for above history entries
114 118
115 uint8_t lut[3][256]; // Lookup table119 uint16_t lut[3][65536]; // Lookup table
116 120
117 void (*find_min_max)(struct NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]);121 void (*find_min_max)(struct NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]);
118 void (*process)(struct NormalizeContext *s, AVFrame *in, AVFrame *out);122 void (*process)(struct NormalizeContext *s, AVFrame *in, AVFrame *out);
@@ -207,6 +211,80 @@ static void process_planar(NormalizeContext *s, AVFrame *in, AVFrame *out) · 88 unmodified lines
207 }211 }
208}212}
209 213
214static void find_min_max_16(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3])
215{
216 for (int c = 0; c < 3; c++)
217 min[c].in = max[c].in = AV_RN16(in->data[0] + 2 * s->co[c]);
218 for (int y = 0; y < in->height; y++) {
219 uint16_t *inp = (uint16_t *)(in->data[0] + y * in->linesize[0]);
220 for (int x = 0; x < in->width; x++) {
221 for (int c = 0; c < 3; c++) {
222 min[c].in = FFMIN(min[c].in, inp[s->co[c]]);
223 max[c].in = FFMAX(max[c].in, inp[s->co[c]]);
224 }
225 inp += s->step;
226 }
227 }
228}
229
230static void process_16(NormalizeContext *s, AVFrame *in, AVFrame *out)
231{
232 for (int y = 0; y < in->height; y++) {
233 uint16_t *inp = (uint16_t *)(in->data[0] + y * in->linesize[0]);
234 uint16_t *outp = (uint16_t *)(out->data[0] + y * out->linesize[0]);
235 for (int x = 0; x < in->width; x++) {
236 for (int c = 0; c < 3; c++)
237 outp[s->co[c]] = s->lut[c][inp[s->co[c]]];
238 if (s->num_components == 4)
239 // Copy alpha as-is.
240 outp[s->co[3]] = inp[s->co[3]];
241 inp += s->step;
242 outp += s->step;
243 }
244 }
245}
246
247static void find_min_max_planar_16(NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3])
248{
249 min[0].in = max[0].in = AV_RN16(in->data[2]);
250 min[1].in = max[1].in = AV_RN16(in->data[0]);
251 min[2].in = max[2].in = AV_RN16(in->data[1]);
252 for (int y = 0; y < in->height; y++) {
253 uint16_t *inrp = (uint16_t *)(in->data[2] + y * in->linesize[2]);
254 uint16_t *ingp = (uint16_t *)(in->data[0] + y * in->linesize[0]);
255 uint16_t *inbp = (uint16_t *)(in->data[1] + y * in->linesize[1]);
256 for (int x = 0; x < in->width; x++) {
257 min[0].in = FFMIN(min[0].in, inrp[x]);
258 max[0].in = FFMAX(max[0].in, inrp[x]);
259 min[1].in = FFMIN(min[1].in, ingp[x]);
260 max[1].in = FFMAX(max[1].in, ingp[x]);
261 min[2].in = FFMIN(min[2].in, inbp[x]);
262 max[2].in = FFMAX(max[2].in, inbp[x]);
263 }
264 }
265}
266
267static void process_planar_16(NormalizeContext *s, AVFrame *in, AVFrame *out)
268{
269 for (int y = 0; y < in->height; y++) {
270 uint16_t *inrp = (uint16_t *)(in->data[2] + y * in->linesize[2]);
271 uint16_t *ingp = (uint16_t *)(in->data[0] + y * in->linesize[0]);
272 uint16_t *inbp = (uint16_t *)(in->data[1] + y * in->linesize[1]);
273 uint16_t *inap = (uint16_t *)(in->data[3] + y * in->linesize[3]);
274 uint16_t *outrp = (uint16_t *)(out->data[2] + y * out->linesize[2]);
275 uint16_t *outgp = (uint16_t *)(out->data[0] + y * out->linesize[0]);
276 uint16_t *outbp = (uint16_t *)(out->data[1] + y * out->linesize[1]);
277 uint16_t *outap = (uint16_t *)(out->data[3] + y * out->linesize[3]);
278 for (int x = 0; x < in->width; x++) {
279 outrp[x] = s->lut[0][inrp[x]];
280 outgp[x] = s->lut[1][ingp[x]];
281 outbp[x] = s->lut[2][inbp[x]];
282 if (s->num_components == 4)
283 outap[x] = inap[x];
284 }
285 }
286}
287
210// This function is the main guts of the filter. Normalizes the input frame288// This function is the main guts of the filter. Normalizes the input frame
211// into the output frame. The frames are known to have the same dimensions289// into the output frame. The frames are known to have the same dimensions
212// and pixel format.290// and pixel format.
@@ -269,9 +347,9 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) · 56 unmodified lines
269 // Calculate the output range [min.out,max.out] as a ratio of the full-347 // Calculate the output range [min.out,max.out] as a ratio of the full-
270 // strength output range [blackpt,whitept] and the original input range348 // strength output range [blackpt,whitept] and the original input range
271 // [min.in,max.in], based on the user-specified filter strength.349 // [min.in,max.in], based on the user-specified filter strength.
272 min[c].out = (s->blackpt[c] * s->strength)350 min[c].out = (s->sblackpt[c] * s->strength)
273 + (min[c].in * (1.0f - s->strength));351 + (min[c].in * (1.0f - s->strength));
274 max[c].out = (s->whitept[c] * s->strength)352 max[c].out = (s->swhitept[c] * s->strength)
275 + (max[c].in * (1.0f - s->strength));353 + (max[c].in * (1.0f - s->strength));
276 354
277 // Now, build a lookup table which linearly maps the adjusted input range355 // Now, build a lookup table which linearly maps the adjusted input range
@@ -291,8 +369,7 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) · 13 unmodified lines
291 float scale = (max[c].out - min[c].out) / (max[c].smoothed - min[c].smoothed);369 float scale = (max[c].out - min[c].out) / (max[c].smoothed - min[c].smoothed);
292 for (in_val = min[c].in; in_val <= max[c].in; in_val++) {370 for (in_val = min[c].in; in_val <= max[c].in; in_val++) {
293 int out_val = (in_val - min[c].smoothed) * scale + min[c].out + 0.5f;371 int out_val = (in_val - min[c].smoothed) * scale + min[c].out + 0.5f;
294 out_val = FFMAX(out_val, 0);372 out_val = av_clip_uintp2_c(out_val, s->depth);
295 out_val = FFMIN(out_val, 255);
296 s->lut[c][in_val] = out_val;373 s->lut[c][in_val] = out_val;
297 }374 }
298 }375 }
@@ -324,8 +401,11 @@ static int query_formats(AVFilterContext *ctx) · 25 unmodified lines
324 AV_PIX_FMT_RGB0,401 AV_PIX_FMT_RGB0,
325 AV_PIX_FMT_0BGR,402 AV_PIX_FMT_0BGR,
326 AV_PIX_FMT_BGR0,403 AV_PIX_FMT_BGR0,
327 AV_PIX_FMT_GBRAP,404 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
328 AV_PIX_FMT_GBRP,405 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
406 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
407 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
408 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
329 AV_PIX_FMT_NONE409 AV_PIX_FMT_NONE
330 };410 };
331 // According to filter_design.txt, using ff_set_common_formats() this way411 // According to filter_design.txt, using ff_set_common_formats() this way
@@ -345,11 +425,13 @@ static int config_input(AVFilterLink *inlink) · 13 unmodified lines
345 NormalizeContext *s = inlink->dst->priv;425 NormalizeContext *s = inlink->dst->priv;
346 // Store offsets to R,G,B,A bytes respectively in each pixel426 // Store offsets to R,G,B,A bytes respectively in each pixel
347 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);427 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
348 int c, planar;428 int c, planar, scale;
349 429
350 ff_fill_rgba_map(s->co, inlink->format);430 ff_fill_rgba_map(s->co, inlink->format);
431 s->depth = desc->comp[0].depth;
432 scale = 1 << (s->depth - 8);
351 s->num_components = desc->nb_components;433 s->num_components = desc->nb_components;
352 s->step = av_get_padded_bits_per_pixel(desc) >> 3;434 s->step = av_get_padded_bits_per_pixel(desc) >> (3 + (s->depth > 8));
353 // Convert smoothing value to history_len (a count of frames to average,435 // Convert smoothing value to history_len (a count of frames to average,
354 // must be at least 1). Currently this is a direct assignment, but the436 // must be at least 1). Currently this is a direct assignment, but the
355 // smoothing value was originally envisaged as a number of seconds. In437 // smoothing value was originally envisaged as a number of seconds. In
@@ -359,19 +441,26 @@ static int config_input(AVFilterLink *inlink) · 3 unmodified lines
359 // Allocate the history buffers -- there are 6 -- one for each extrema.441 // Allocate the history buffers -- there are 6 -- one for each extrema.
360 // s->smoothing is limited to INT_MAX/8, so that (s->history_len * 6)442 // s->smoothing is limited to INT_MAX/8, so that (s->history_len * 6)
361 // can't overflow on 32bit causing a too-small allocation.443 // can't overflow on 32bit causing a too-small allocation.
362 s->history_mem = av_malloc(s->history_len * 6);444 s->history_mem = av_malloc(s->history_len * 6 * sizeof(*s->history_mem));
363 if (s->history_mem == NULL)445 if (s->history_mem == NULL)
364 return AVERROR(ENOMEM);446 return AVERROR(ENOMEM);
365 447
366 for (c = 0; c < 3; c++) {448 for (c = 0; c < 3; c++) {
367 s->min[c].history = s->history_mem + (c*2) * s->history_len;449 s->min[c].history = s->history_mem + (c*2) * s->history_len;
368 s->max[c].history = s->history_mem + (c*2+1) * s->history_len;450 s->max[c].history = s->history_mem + (c*2+1) * s->history_len;
451 s->sblackpt[c] = scale * s->blackpt[c] + (s->blackpt[c] >> (s->depth - 8));
452 s->swhitept[c] = scale * s->whitept[c] + (s->whitept[c] >> (s->depth - 8));
369 }453 }
370 454
371 planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;455 planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
372 456
373 s->find_min_max = planar ? find_min_max_planar : find_min_max;457 if (s->depth <= 8) {
374 s->process = planar? process_planar : process;458 s->find_min_max = planar ? find_min_max_planar : find_min_max;
459 s->process = planar? process_planar : process;
460 } else {
461 s->find_min_max = planar ? find_min_max_planar_16 : find_min_max_16;
462 s->process = planar? process_planar_16 : process_16;
463 }
375 464
376 return 0;465 return 0;
377}466}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t