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
7373 */
7474
7575 #include "libavutil/imgutils.h"
76+#include "libavutil/intreadwrite.h"
7677 #include "libavutil/opt.h"
7778 #include "libavutil/pixdesc.h"
7879 #include "avfilter.h"
@@ -82,12 +83,12 @@ · 3 unmodified lines
8283 #include "video.h"
8384
8485 typedef struct NormalizeHistory {
85− uint8_t *history; // History entries.
86− uint32_t history_sum; // Sum of history entries.
86+ uint16_t *history; // History entries.
87+ uint64_t history_sum; // Sum of history entries.
8788 } NormalizeHistory;
8889
8990 typedef struct NormalizeLocal {
90− uint8_t in; // Original input byte value for this frame.
91+ uint16_t in; // Original input byte value for this frame.
9192 float smoothed; // Smoothed input value [0,255].
9293 float out; // Output value [0,255]
9394 } NormalizeLocal;
@@ -103,6 +104,9 @@ typedef struct NormalizeContext { · 9 unmodified lines
103104 float strength;
104105
105106 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];
106110 int num_components; // Number of components in the pixel format
107111 int step;
108112 int history_len; // Number of frames to average; based on smoothing factor
@@ -110,9 +114,9 @@ typedef struct NormalizeContext { · 1 unmodified line
110114
111115 // Per-extremum, per-channel history, for temporal smoothing.
112116 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 entries
117+ uint16_t *history_mem; // Single allocation for above history entries
114118
115− uint8_t lut[3][256]; // Lookup table
119+ uint16_t lut[3][65536]; // Lookup table
116120
117121 void (*find_min_max)(struct NormalizeContext *s, AVFrame *in, NormalizeLocal min[3], NormalizeLocal max[3]);
118122 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
207211 }
208212 }
209213
214+static 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+
230+static 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+
247+static 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+
267+static 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+
210288 // This function is the main guts of the filter. Normalizes the input frame
211289 // into the output frame. The frames are known to have the same dimensions
212290 // and pixel format.
@@ -269,9 +347,9 @@ static void normalize(NormalizeContext *s, AVFrame *in, AVFrame *out) · 56 unmodified lines
269347 // Calculate the output range [min.out,max.out] as a ratio of the full-
270348 // strength output range [blackpt,whitept] and the original input range
271349 // [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)
273351 + (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)
275353 + (max[c].in * (1.0f - s->strength));
276354
277355 // 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
291369 float scale = (max[c].out - min[c].out) / (max[c].smoothed - min[c].smoothed);
292370 for (in_val = min[c].in; in_val <= max[c].in; in_val++) {
293371 int out_val = (in_val - min[c].smoothed) * scale + min[c].out + 0.5f;
294− out_val = FFMAX(out_val, 0);
295− out_val = FFMIN(out_val, 255);
372+ out_val = av_clip_uintp2_c(out_val, s->depth);
296373 s->lut[c][in_val] = out_val;
297374 }
298375 }
@@ -324,8 +401,11 @@ static int query_formats(AVFilterContext *ctx) · 25 unmodified lines
324401 AV_PIX_FMT_RGB0,
325402 AV_PIX_FMT_0BGR,
326403 AV_PIX_FMT_BGR0,
327− AV_PIX_FMT_GBRAP,
328− AV_PIX_FMT_GBRP,
404+ AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
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,
329409 AV_PIX_FMT_NONE
330410 };
331411 // 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
345425 NormalizeContext *s = inlink->dst->priv;
346426 // Store offsets to R,G,B,A bytes respectively in each pixel
347427 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
348− int c, planar;
428+ int c, planar, scale;
349429
350430 ff_fill_rgba_map(s->co, inlink->format);
431+ s->depth = desc->comp[0].depth;
432+ scale = 1 << (s->depth - 8);
351433 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));
353435 // Convert smoothing value to history_len (a count of frames to average,
354436 // must be at least 1). Currently this is a direct assignment, but the
355437 // smoothing value was originally envisaged as a number of seconds. In
@@ -359,19 +441,26 @@ static int config_input(AVFilterLink *inlink) · 3 unmodified lines
359441 // Allocate the history buffers -- there are 6 -- one for each extrema.
360442 // s->smoothing is limited to INT_MAX/8, so that (s->history_len * 6)
361443 // 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));
363445 if (s->history_mem == NULL)
364446 return AVERROR(ENOMEM);
365447
366448 for (c = 0; c < 3; c++) {
367449 s->min[c].history = s->history_mem + (c*2) * s->history_len;
368450 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));
369453 }
370454
371455 planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
372456
373− s->find_min_max = planar ? find_min_max_planar : find_min_max;
374− s->process = planar? process_planar : process;
457+ if (s->depth <= 8) {
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+ }
375464
376465 return 0;
377466 }

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