History

avfilter/vf_colorcorrect: add median analyze mode

Paul B Mahol committed 5 years ago

Commit dc34bf41 parent 0cacef52 files · +141 −8Browse files

Unified · Split

2 files
  1. doc/filters.texi +9 −1
  2. libavfilter/vf_colorcorrect.c +132 −7
doc/filters.texi+9−1
@@ -8235,7 +8235,15 @@ Default value is 1. · 8234 unmodified lines
8235 8235
8236@item analyze8236@item analyze
8237If set to anything other than @code{manual} it will analyze every frame and use derived8237If set to anything other than @code{manual} it will analyze every frame and use derived
8238parameters for filtering output frame. Can be @code{manual} or @code{average} or @code{minmax}.8238parameters for filtering output frame.
8239
8240Possible values are:
8241@table @samp
8242@item manual
8243@item average
8244@item minmax
8245@item median
8246@end table
8239Default value is @code{manual}.8247Default value is @code{manual}.
8240@end table8248@end table
8241 8249
libavfilter/vf_colorcorrect.c+132−7
@@ -31,6 +31,7 @@ typedef enum AnalyzeMode { · 30 unmodified lines
31 MANUAL,31 MANUAL,
32 AVERAGE,32 AVERAGE,
33 MINMAX,33 MINMAX,
34 MEDIAN,
34 NB_ANALYZE35 NB_ANALYZE
35} AnalyzeMode;36} AnalyzeMode;
36 37
@@ -49,6 +50,9 @@ typedef struct ColorCorrectContext { · 12 unmodified lines
49 int planeheight[4];50 int planeheight[4];
50 int planewidth[4];51 int planewidth[4];
51 52
53 unsigned *uhistogram;
54 unsigned *vhistogram;
55
52 float (*analyzeret)[4];56 float (*analyzeret)[4];
53 57
54 int (*do_analyze)(AVFilterContext *s, void *arg,58 int (*do_analyze)(AVFilterContext *s, void *arg,
@@ -191,6 +195,114 @@ static int minmax_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_job · 136 unmodified lines
191 return 0;195 return 0;
192}196}
193 197
198static int median_8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
199{
200 ColorCorrectContext *s = ctx->priv;
201 AVFrame *frame = arg;
202 const float imax = s->imax;
203 const int width = s->planewidth[1];
204 const int height = s->planeheight[1];
205 const int ulinesize = frame->linesize[1];
206 const int vlinesize = frame->linesize[2];
207 const uint8_t *uptr = (const uint8_t *)frame->data[1];
208 const uint8_t *vptr = (const uint8_t *)frame->data[2];
209 unsigned *uhistogram = s->uhistogram;
210 unsigned *vhistogram = s->vhistogram;
211 const int half_size = width * height / 2;
212 int umedian = s->max, vmedian = s->max;
213 unsigned ucnt = 0, vcnt = 0;
214
215 memset(uhistogram, 0, sizeof(*uhistogram) * (s->max + 1));
216 memset(vhistogram, 0, sizeof(*vhistogram) * (s->max + 1));
217
218 for (int y = 0; y < height; y++) {
219 for (int x = 0; x < width; x++) {
220 uhistogram[uptr[x]]++;
221 vhistogram[vptr[x]]++;
222 }
223
224 uptr += ulinesize;
225 vptr += vlinesize;
226 }
227
228 for (int i = 0; i < s->max + 1; i++) {
229 ucnt += uhistogram[i];
230 if (ucnt >= half_size) {
231 umedian = i;
232 break;
233 }
234 }
235
236 for (int i = 0; i < s->max + 1; i++) {
237 vcnt += vhistogram[i];
238 if (vcnt >= half_size) {
239 vmedian = i;
240 break;
241 }
242 }
243
244 s->analyzeret[0][0] = imax * umedian - 0.5f;
245 s->analyzeret[0][1] = imax * vmedian - 0.5f;
246 s->analyzeret[0][2] = imax * umedian - 0.5f;
247 s->analyzeret[0][3] = imax * vmedian - 0.5f;
248
249 return 0;
250}
251
252static int median_16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
253{
254 ColorCorrectContext *s = ctx->priv;
255 AVFrame *frame = arg;
256 const float imax = s->imax;
257 const int width = s->planewidth[1];
258 const int height = s->planeheight[1];
259 const int ulinesize = frame->linesize[1] / 2;
260 const int vlinesize = frame->linesize[2] / 2;
261 const uint16_t *uptr = (const uint16_t *)frame->data[1];
262 const uint16_t *vptr = (const uint16_t *)frame->data[2];
263 unsigned *uhistogram = s->uhistogram;
264 unsigned *vhistogram = s->vhistogram;
265 const int half_size = width * height / 2;
266 int umedian = s->max, vmedian = s->max;
267 unsigned ucnt = 0, vcnt = 0;
268
269 memset(uhistogram, 0, sizeof(*uhistogram) * (s->max + 1));
270 memset(vhistogram, 0, sizeof(*vhistogram) * (s->max + 1));
271
272 for (int y = 0; y < height; y++) {
273 for (int x = 0; x < width; x++) {
274 uhistogram[uptr[x]]++;
275 vhistogram[vptr[x]]++;
276 }
277
278 uptr += ulinesize;
279 vptr += vlinesize;
280 }
281
282 for (int i = 0; i < s->max + 1; i++) {
283 ucnt += uhistogram[i];
284 if (ucnt >= half_size) {
285 umedian = i;
286 break;
287 }
288 }
289
290 for (int i = 0; i < s->max + 1; i++) {
291 vcnt += vhistogram[i];
292 if (vcnt >= half_size) {
293 vmedian = i;
294 break;
295 }
296 }
297
298 s->analyzeret[0][0] = imax * umedian - 0.5f;
299 s->analyzeret[0][1] = imax * vmedian - 0.5f;
300 s->analyzeret[0][2] = imax * umedian - 0.5f;
301 s->analyzeret[0][3] = imax * vmedian - 0.5f;
302
303 return 0;
304}
305
194#define PROCESS() \306#define PROCESS() \
195 float y = yptr[x * chroma_w] * imax; \307 float y = yptr[x * chroma_w] * imax; \
196 float u = uptr[x] * imax - .5f; \308 float u = uptr[x] * imax - .5f; \
@@ -285,24 +397,25 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) · 88 unmodified lines
285{397{
286 AVFilterContext *ctx = inlink->dst;398 AVFilterContext *ctx = inlink->dst;
287 ColorCorrectContext *s = ctx->priv;399 ColorCorrectContext *s = ctx->priv;
288 const int nb_threads = FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx));400 const int nb_threads = s->analyze == MEDIAN ? 1 : FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx));
289 401
290 if (s->analyze) {402 if (s->analyze) {
403 const int nb_athreads = s->analyze == MEDIAN ? 1 : nb_threads;
291 float bl = 0.f, rl = 0.f, bh = 0.f, rh = 0.f;404 float bl = 0.f, rl = 0.f, bh = 0.f, rh = 0.f;
292 405
293 ff_filter_execute(ctx, s->do_analyze, frame, NULL, nb_threads);406 ff_filter_execute(ctx, s->do_analyze, frame, NULL, nb_athreads);
294 407
295 for (int i = 0; i < nb_threads; i++) {408 for (int i = 0; i < nb_athreads; i++) {
296 bl += s->analyzeret[i][0];409 bl += s->analyzeret[i][0];
297 rl += s->analyzeret[i][1];410 rl += s->analyzeret[i][1];
298 bh += s->analyzeret[i][2];411 bh += s->analyzeret[i][2];
299 rh += s->analyzeret[i][3];412 rh += s->analyzeret[i][3];
300 }413 }
301 414
302 bl /= nb_threads;415 bl /= nb_athreads;
303 rl /= nb_threads;416 rl /= nb_athreads;
304 bh /= nb_threads;417 bh /= nb_athreads;
305 rh /= nb_threads;418 rh /= nb_athreads;
306 419
307 s->bl = -bl;420 s->bl = -bl;
308 s->rl = -rl;421 s->rl = -rl;
@@ -347,6 +460,14 @@ static av_cold int config_input(AVFilterLink *inlink) · 38 unmodified lines
347 s->imax = 1.f / s->max;460 s->imax = 1.f / s->max;
348 s->do_slice = s->depth <= 8 ? colorcorrect_slice8 : colorcorrect_slice16;461 s->do_slice = s->depth <= 8 ? colorcorrect_slice8 : colorcorrect_slice16;
349 462
463 s->uhistogram = av_calloc(s->max == 255 ? 256 : 65536, sizeof(*s->uhistogram));
464 if (!s->uhistogram)
465 return AVERROR(ENOMEM);
466
467 s->vhistogram = av_calloc(s->max == 255 ? 256 : 65536, sizeof(*s->vhistogram));
468 if (!s->vhistogram)
469 return AVERROR(ENOMEM);
470
350 s->analyzeret = av_calloc(inlink->h, sizeof(*s->analyzeret));471 s->analyzeret = av_calloc(inlink->h, sizeof(*s->analyzeret));
351 if (!s->analyzeret)472 if (!s->analyzeret)
352 return AVERROR(ENOMEM);473 return AVERROR(ENOMEM);
@@ -360,6 +481,9 @@ static av_cold int config_input(AVFilterLink *inlink) · 7 unmodified lines
360 case MINMAX:481 case MINMAX:
361 s->do_analyze = s->depth <= 8 ? minmax_slice8 : minmax_slice16;482 s->do_analyze = s->depth <= 8 ? minmax_slice8 : minmax_slice16;
362 break;483 break;
484 case MEDIAN:
485 s->do_analyze = s->depth <= 8 ? median_8 : median_16;
486 break;
363 default:487 default:
364 return AVERROR_BUG;488 return AVERROR_BUG;
365 }489 }
@@ -411,6 +535,7 @@ static const AVOption colorcorrect_options[] = { · 45 unmodified lines
411 { "manual", "manually set options", 0, AV_OPT_TYPE_CONST, {.i64=MANUAL}, 0, 0, VF, "analyze" },535 { "manual", "manually set options", 0, AV_OPT_TYPE_CONST, {.i64=MANUAL}, 0, 0, VF, "analyze" },
412 { "average", "use average pixels", 0, AV_OPT_TYPE_CONST, {.i64=AVERAGE}, 0, 0, VF, "analyze" },536 { "average", "use average pixels", 0, AV_OPT_TYPE_CONST, {.i64=AVERAGE}, 0, 0, VF, "analyze" },
413 { "minmax", "use minmax pixels", 0, AV_OPT_TYPE_CONST, {.i64=MINMAX}, 0, 0, VF, "analyze" },537 { "minmax", "use minmax pixels", 0, AV_OPT_TYPE_CONST, {.i64=MINMAX}, 0, 0, VF, "analyze" },
538 { "median", "use median pixels", 0, AV_OPT_TYPE_CONST, {.i64=MEDIAN}, 0, 0, VF, "analyze" },
414 { NULL }539 { NULL }
415};540};
416 541

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