| @@ -26,6 +26,7 @@ · 25 unmodified lines |
| 26 | 26 | #include "formats.h" |
| 27 | 27 | #include "internal.h" |
| 28 | 28 | #include "video.h" |
| 29 | +#include "preserve_color.h" |
| 29 | 30 | |
| 30 | 31 | #define R 0 |
| 31 | 32 | #define G 1 |
| @@ -40,6 +41,7 @@ typedef struct Range { · 8 unmodified lines |
| 40 | 41 | typedef struct ColorLevelsContext { |
| 41 | 42 | const AVClass *class; |
| 42 | 43 | Range range[4]; |
| 44 | + int preserve_color; |
| 43 | 45 | |
| 44 | 46 | int nb_comp; |
| 45 | 47 | int bpp; |
| @@ -47,7 +49,7 @@ typedef struct ColorLevelsContext { · 1 unmodified line |
| 47 | 49 | uint8_t rgba_map[4]; |
| 48 | 50 | int linesize; |
| 49 | 51 | |
| 50 | | − int (*colorlevels_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
| 52 | + int (*colorlevels_slice[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
| 51 | 53 | } ColorLevelsContext; |
| 52 | 54 | |
| 53 | 55 | #define OFFSET(x) offsetof(ColorLevelsContext, x) |
| @@ -69,6 +71,14 @@ static const AVOption colorlevels_options[] = { · 15 unmodified lines |
| 69 | 71 | { "gomax", "set output green white point", OFFSET(range[G].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS }, |
| 70 | 72 | { "bomax", "set output blue white point", OFFSET(range[B].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS }, |
| 71 | 73 | { "aomax", "set output alpha white point", OFFSET(range[A].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS }, |
| 74 | + { "preserve", "set preserve color mode", OFFSET(preserve_color), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_PRESERVE-1, FLAGS, "preserve" }, |
| 75 | + { "none", "disabled", 0, AV_OPT_TYPE_CONST, {.i64=P_NONE}, 0, 0, FLAGS, "preserve" }, |
| 76 | + { "lum", "luminance", 0, AV_OPT_TYPE_CONST, {.i64=P_LUM}, 0, 0, FLAGS, "preserve" }, |
| 77 | + { "max", "max", 0, AV_OPT_TYPE_CONST, {.i64=P_MAX}, 0, 0, FLAGS, "preserve" }, |
| 78 | + { "avg", "average", 0, AV_OPT_TYPE_CONST, {.i64=P_AVG}, 0, 0, FLAGS, "preserve" }, |
| 79 | + { "sum", "sum", 0, AV_OPT_TYPE_CONST, {.i64=P_SUM}, 0, 0, FLAGS, "preserve" }, |
| 80 | + { "nrm", "norm", 0, AV_OPT_TYPE_CONST, {.i64=P_NRM}, 0, 0, FLAGS, "preserve" }, |
| 81 | + { "pwr", "power", 0, AV_OPT_TYPE_CONST, {.i64=P_PWR}, 0, 0, FLAGS, "preserve" }, |
| 72 | 82 | { NULL } |
| 73 | 83 | }; |
| 74 | 84 | |
| @@ -104,7 +114,7 @@ typedef struct ThreadData { · 29 unmodified lines |
| 104 | 114 | int omin[4]; |
| 105 | 115 | } ThreadData; |
| 106 | 116 | |
| 107 | | −#define DO_COMMON(type, clip) \ |
| 117 | +#define DO_COMMON(type, clip, preserve) \ |
| 108 | 118 | ColorLevelsContext *s = ctx->priv; \ |
| 109 | 119 | const ThreadData *td = arg; \ |
| 110 | 120 | const int linesize = s->linesize; \ |
| @@ -143,9 +153,35 @@ typedef struct ThreadData { · 32 unmodified lines |
| 143 | 153 | \ |
| 144 | 154 | for (int y = slice_start; y < slice_end; y++) { \ |
| 145 | 155 | for (int x = 0; x < linesize; x += step) { \ |
| 146 | | − dst_r[x] = clip((src_r[x] - imin_r) * coeff_r + omin_r); \ |
| 147 | | − dst_g[x] = clip((src_g[x] - imin_g) * coeff_g + omin_g); \ |
| 148 | | − dst_b[x] = clip((src_b[x] - imin_b) * coeff_b + omin_b); \ |
| 156 | + int ir, ig, ib, or, og, ob; \ |
| 157 | + ir = src_r[x]; \ |
| 158 | + ig = src_g[x]; \ |
| 159 | + ib = src_b[x]; \ |
| 160 | + if (preserve) { \ |
| 161 | + float ratio, icolor, ocolor, max = (1<<(8*sizeof(type)))-1; \ |
| 162 | + \ |
| 163 | + or = (ir - imin_r) * coeff_r + omin_r; \ |
| 164 | + og = (ig - imin_g) * coeff_g + omin_g; \ |
| 165 | + ob = (ib - imin_b) * coeff_b + omin_b; \ |
| 166 | + \ |
| 167 | + preserve_color(s->preserve_color, ir, ig, ib, or, og, ob, max, \ |
| 168 | + &icolor, &ocolor); \ |
| 169 | + if (ocolor > 0.f) { \ |
| 170 | + ratio = icolor / ocolor; \ |
| 171 | + \ |
| 172 | + or *= ratio; \ |
| 173 | + og *= ratio; \ |
| 174 | + ob *= ratio; \ |
| 175 | + } \ |
| 176 | + \ |
| 177 | + dst_r[x] = clip(or); \ |
| 178 | + dst_g[x] = clip(og); \ |
| 179 | + dst_b[x] = clip(ob); \ |
| 180 | + } else { \ |
| 181 | + dst_r[x] = clip((ir - imin_r) * coeff_r + omin_r); \ |
| 182 | + dst_g[x] = clip((ig - imin_g) * coeff_g + omin_g); \ |
| 183 | + dst_b[x] = clip((ib - imin_b) * coeff_b + omin_b); \ |
| 184 | + } \ |
| 149 | 185 | } \ |
| 150 | 186 | \ |
| 151 | 187 | for (int x = 0; x < linesize && s->nb_comp == 4; x += step) \ |
| @@ -164,14 +200,28 @@ typedef struct ThreadData { · 12 unmodified lines |
| 164 | 200 | |
| 165 | 201 | static int colorlevels_slice_8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
| 166 | 202 | { |
| 167 | | − DO_COMMON(uint8_t, av_clip_uint8) |
| 203 | + DO_COMMON(uint8_t, av_clip_uint8, 0) |
| 168 | 204 | |
| 169 | 205 | return 0; |
| 170 | 206 | } |
| 171 | 207 | |
| 172 | 208 | static int colorlevels_slice_16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
| 173 | 209 | { |
| 174 | | − DO_COMMON(uint16_t, av_clip_uint16) |
| 210 | + DO_COMMON(uint16_t, av_clip_uint16, 0) |
| 211 | + |
| 212 | + return 0; |
| 213 | +} |
| 214 | + |
| 215 | +static int colorlevels_preserve_slice_8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
| 216 | +{ |
| 217 | + DO_COMMON(uint8_t, av_clip_uint8, 1) |
| 218 | + |
| 219 | + return 0; |
| 220 | +} |
| 221 | + |
| 222 | +static int colorlevels_preserve_slice_16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
| 223 | +{ |
| 224 | + DO_COMMON(uint16_t, av_clip_uint16, 1) |
| 175 | 225 | |
| 176 | 226 | return 0; |
| 177 | 227 | } |
| @@ -188,9 +238,12 @@ static int config_input(AVFilterLink *inlink) · 10 unmodified lines |
| 188 | 238 | s->linesize = inlink->w * s->step; |
| 189 | 239 | ff_fill_rgba_map(s->rgba_map, inlink->format); |
| 190 | 240 | |
| 191 | | − s->colorlevels_slice = colorlevels_slice_8; |
| 192 | | − if (s->bpp == 2) |
| 193 | | − s->colorlevels_slice = colorlevels_slice_16; |
| 241 | + s->colorlevels_slice[0] = colorlevels_slice_8; |
| 242 | + s->colorlevels_slice[1] = colorlevels_preserve_slice_8; |
| 243 | + if (s->bpp == 2) { |
| 244 | + s->colorlevels_slice[0] = colorlevels_slice_16; |
| 245 | + s->colorlevels_slice[1] = colorlevels_preserve_slice_16; |
| 246 | + } |
| 194 | 247 | |
| 195 | 248 | return 0; |
| 196 | 249 | } |
| @@ -304,7 +357,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 107 unmodified lines |
| 304 | 357 | break; |
| 305 | 358 | } |
| 306 | 359 | |
| 307 | | − ff_filter_execute(ctx, s->colorlevels_slice, &td, NULL, |
| 360 | + ff_filter_execute(ctx, s->colorlevels_slice[s->preserve_color > 0], &td, NULL, |
| 308 | 361 | FFMIN(inlink->h, ff_filter_get_nb_threads(ctx))); |
| 309 | 362 | |
| 310 | 363 | if (in != out) |