+static int vibrance_slice8p(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
+{
+ VibranceContext *s = avctx->priv;
+ AVFrame *frame = arg;
+ const int step = s->step;
+ const int width = frame->width;
+ const int height = frame->height;
+ const float scale = 1.f / 255.f;
+ const float gc = s->lcoeffs[0];
+ const float bc = s->lcoeffs[1];
+ const float rc = s->lcoeffs[2];
+ const uint8_t roffset = s->rgba_map[R];
+ const uint8_t goffset = s->rgba_map[G];
+ const uint8_t boffset = s->rgba_map[B];
+ const float intensity = s->intensity;
+ const float alternate = s->alternate ? 1.f : -1.f;
+ const float gintensity = intensity * s->balance[0];
+ const float bintensity = intensity * s->balance[1];
+ const float rintensity = intensity * s->balance[2];
+ const float sgintensity = alternate * FFSIGN(gintensity);
+ const float sbintensity = alternate * FFSIGN(bintensity);
+ const float srintensity = alternate * FFSIGN(rintensity);
+ const int slice_start = (height * jobnr) / nb_jobs;
+ const int slice_end = (height * (jobnr + 1)) / nb_jobs;
+ const int linesize = frame->linesize[0];
+ uint8_t *ptr = frame->data[0] + slice_start * linesize;
+
+ for (int y = slice_start; y < slice_end; y++) {
+ for (int x = 0; x < width; x++) {
+ float g = ptr[x * step + goffset] * scale;
+ float b = ptr[x * step + boffset] * scale;
+ float r = ptr[x * step + roffset] * scale;
+ float max_color = FFMAX3(r, g, b);
+ float min_color = FFMIN3(r, g, b);
+ float color_saturation = max_color - min_color;
+ float luma = g * gc + r * rc + b * bc;
+ const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
+ const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
+ const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
+
+ g = lerpf(luma, g, cg);
+ b = lerpf(luma, b, cb);
+ r = lerpf(luma, r, cr);
+
+ ptr[x * step + goffset] = av_clip_uint8(g * 255.f);
+ ptr[x * step + boffset] = av_clip_uint8(b * 255.f);
+ ptr[x * step + roffset] = av_clip_uint8(r * 255.f);
+ }
+
+ ptr += linesize;
+ }
+
+ return 0;
+}
+
+static int vibrance_slice16p(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
+{
+ VibranceContext *s = avctx->priv;
+ AVFrame *frame = arg;
+ const int step = s->step;
+ const int depth = s->depth;
+ const float max = (1 << depth) - 1;
+ const float scale = 1.f / max;
+ const float gc = s->lcoeffs[0];
+ const float bc = s->lcoeffs[1];
+ const float rc = s->lcoeffs[2];
+ const uint8_t roffset = s->rgba_map[R];
+ const uint8_t goffset = s->rgba_map[G];
+ const uint8_t boffset = s->rgba_map[B];
+ const int width = frame->width;
+ const int height = frame->height;
+ const float intensity = s->intensity;
+ const float alternate = s->alternate ? 1.f : -1.f;
+ const float gintensity = intensity * s->balance[0];
+ const float bintensity = intensity * s->balance[1];
+ const float rintensity = intensity * s->balance[2];
+ const float sgintensity = alternate * FFSIGN(gintensity);
+ const float sbintensity = alternate * FFSIGN(bintensity);
+ const float srintensity = alternate * FFSIGN(rintensity);
+ const int slice_start = (height * jobnr) / nb_jobs;
+ const int slice_end = (height * (jobnr + 1)) / nb_jobs;
+ const int linesize = frame->linesize[0] / 2;
+ uint16_t *ptr = (uint16_t *)frame->data[0] + slice_start * linesize;
+
+ for (int y = slice_start; y < slice_end; y++) {
+ for (int x = 0; x < width; x++) {
+ float g = ptr[x * step + goffset] * scale;
+ float b = ptr[x * step + boffset] * scale;
+ float r = ptr[x * step + roffset] * scale;
+ float max_color = FFMAX3(r, g, b);
+ float min_color = FFMIN3(r, g, b);
+ float color_saturation = max_color - min_color;
+ float luma = g * gc + r * rc + b * bc;
+ const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
+ const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
+ const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
+
+ g = lerpf(luma, g, cg);
+ b = lerpf(luma, b, cb);
+ r = lerpf(luma, r, cr);
+
+ ptr[x * step + goffset] = av_clip_uintp2_c(g * max, depth);
+ ptr[x * step + boffset] = av_clip_uintp2_c(b * max, depth);
+ ptr[x * step + roffset] = av_clip_uintp2_c(r * max, depth);
+ }
+
+ ptr += linesize;
+ }
+
+ return 0;
+}
+