X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_lut3d.c;h=93053ed263462e2f59e304deac5309825242fcd8;hb=635067b75fce06928431ce9b9fcaee0c9b6b7280;hp=4d985c599ffae8e8c8ee080ed2e5361f5941878d;hpb=94d98330ed6c5562341315c26c1af92771a2e6de;p=ffmpeg diff --git a/libavfilter/vf_lut3d.c b/libavfilter/vf_lut3d.c index 4d985c599ff..93053ed2634 100644 --- a/libavfilter/vf_lut3d.c +++ b/libavfilter/vf_lut3d.c @@ -367,7 +367,7 @@ static int parse_dat(AVFilterContext *ctx, FILE *f) struct rgbvec *vec = &lut3d->lut[k][j][i]; if (k != 0 || j != 0 || i != 0) NEXT_LINE(skip_line(line)); - if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3) + if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3) return AVERROR_INVALIDDATA; } } @@ -384,7 +384,7 @@ static int parse_cube(AVFilterContext *ctx, FILE *f) float max[3] = {1.0, 1.0, 1.0}; while (fgets(line, sizeof(line), f)) { - if (!strncmp(line, "LUT_3D_SIZE ", 12)) { + if (!strncmp(line, "LUT_3D_SIZE", 11)) { int i, j, k; const int size = strtol(line + 12, NULL, 0); @@ -407,13 +407,15 @@ try_again: else if (!strncmp(line + 7, "MAX ", 4)) vals = max; if (!vals) return AVERROR_INVALIDDATA; - sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2); + av_sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2); av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n", min[0], min[1], min[2], max[0], max[1], max[2]); goto try_again; + } else if (!strncmp(line, "TITLE", 5)) { + goto try_again; } } while (skip_line(line)); - if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3) + if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3) return AVERROR_INVALIDDATA; vec->r *= max[0] - min[0]; vec->g *= max[1] - min[1]; @@ -446,7 +448,7 @@ static int parse_3dl(AVFilterContext *ctx, FILE *f) struct rgbvec *vec = &lut3d->lut[k][j][i]; NEXT_LINE(skip_line(line)); - if (sscanf(line, "%d %d %d", &r, &g, &b) != 3) + if (av_sscanf(line, "%d %d %d", &r, &g, &b) != 3) return AVERROR_INVALIDDATA; vec->r = r / scale; vec->g = g / scale; @@ -510,7 +512,7 @@ static int parse_m3d(AVFilterContext *ctx, FILE *f) float val[3]; NEXT_LINE(0); - if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3) + if (av_sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3) return AVERROR_INVALIDDATA; vec->r = val[rgb_map[0]] * scale; vec->g = val[rgb_map[1]] * scale; @@ -983,6 +985,8 @@ enum interp_1d_mode { INTERPOLATE_1D_NEAREST, INTERPOLATE_1D_LINEAR, INTERPOLATE_1D_CUBIC, + INTERPOLATE_1D_COSINE, + INTERPOLATE_1D_SPLINE, NB_INTERP_1D_MODE }; @@ -1023,7 +1027,7 @@ static int parse_cube_1d(AVFilterContext *ctx, FILE *f) float max[3] = {1.0, 1.0, 1.0}; while (fgets(line, sizeof(line), f)) { - if (!strncmp(line, "LUT_1D_SIZE ", 12)) { + if (!strncmp(line, "LUT_1D_SIZE", 11)) { const int size = strtol(line + 12, NULL, 0); int i; @@ -1042,18 +1046,20 @@ try_again: else if (!strncmp(line + 7, "MAX ", 4)) vals = max; if (!vals) return AVERROR_INVALIDDATA; - sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2); + av_sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2); av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n", min[0], min[1], min[2], max[0], max[1], max[2]); goto try_again; } else if (!strncmp(line, "LUT_1D_INPUT_RANGE ", 19)) { - sscanf(line + 19, "%f %f", min, max); + av_sscanf(line + 19, "%f %f", min, max); min[1] = min[2] = min[0]; max[1] = max[2] = max[0]; goto try_again; + } else if (!strncmp(line, "TITLE", 5)) { + goto try_again; } } while (skip_line(line)); - if (sscanf(line, "%f %f %f", &lut1d->lut[0][i], &lut1d->lut[1][i], &lut1d->lut[2][i]) != 3) + if (av_sscanf(line, "%f %f %f", &lut1d->lut[0][i], &lut1d->lut[1][i], &lut1d->lut[2][i]) != 3) return AVERROR_INVALIDDATA; lut1d->lut[0][i] *= max[0] - min[0]; lut1d->lut[1][i] *= max[1] - min[1]; @@ -1070,7 +1076,9 @@ static const AVOption lut1d_options[] = { { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_1D_LINEAR}, 0, NB_INTERP_1D_MODE-1, FLAGS, "interp_mode" }, { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, { "linear", "use values from the linear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_LINEAR}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, + { "cosine", "use values from the cosine interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_COSINE}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, { "cubic", "use values from the cubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_CUBIC}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, + { "spline", "use values from the spline interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_SPLINE}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, { NULL } }; @@ -1096,6 +1104,19 @@ static inline float interp_1d_linear(const LUT1DContext *lut1d, return lerpf(p, n, d); } +static inline float interp_1d_cosine(const LUT1DContext *lut1d, + int idx, const float s) +{ + const int prev = PREV(s); + const int next = NEXT1D(s); + const float d = s - prev; + const float p = lut1d->lut[idx][prev]; + const float n = lut1d->lut[idx][next]; + const float m = (1.f - cosf(d * M_PI)) * .5f; + + return lerpf(p, n, m); +} + static inline float interp_1d_cubic(const LUT1DContext *lut1d, int idx, const float s) { @@ -1119,6 +1140,27 @@ static inline float interp_1d_cubic(const LUT1DContext *lut1d, return a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3; } +static inline float interp_1d_spline(const LUT1DContext *lut1d, + int idx, const float s) +{ + const int prev = PREV(s); + const int next = NEXT1D(s); + const float x = s - prev; + float c0, c1, c2, c3; + + float y0 = lut1d->lut[idx][FFMAX(prev - 1, 0)]; + float y1 = lut1d->lut[idx][prev]; + float y2 = lut1d->lut[idx][next]; + float y3 = lut1d->lut[idx][FFMIN(next + 1, lut1d->lutsize - 1)]; + + c0 = y1; + c1 = .5f * (y2 - y0); + c2 = y0 - 2.5f * y1 + 2.f * y2 - .5f * y3; + c3 = .5f * (y3 - y0) + 1.5f * (y1 - y2); + + return ((c3 * x + c2) * x + c1) * x + c0; +} + #define DEFINE_INTERP_FUNC_PLANAR_1D(name, nbits, depth) \ static int interp_1d_##nbits##_##name##_p##depth(AVFilterContext *ctx, \ void *arg, int jobnr, \ @@ -1179,27 +1221,39 @@ static int interp_1d_##nbits##_##name##_p##depth(AVFilterContext *ctx, \ DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 8, 8) DEFINE_INTERP_FUNC_PLANAR_1D(linear, 8, 8) +DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 8, 8) DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 8, 8) +DEFINE_INTERP_FUNC_PLANAR_1D(spline, 8, 8) DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 9) DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 9) +DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 9) DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 9) +DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 9) DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 10) DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 10) +DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 10) DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 10) +DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 10) DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 12) DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 12) +DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 12) DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 12) +DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 12) DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 14) DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 14) +DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 14) DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 14) +DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 14) DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 16) DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 16) +DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 16) DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 16) +DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 16) #define DEFINE_INTERP_FUNC_1D(name, nbits) \ static int interp_1d_##nbits##_##name(AVFilterContext *ctx, void *arg, \ @@ -1247,11 +1301,15 @@ static int interp_1d_##nbits##_##name(AVFilterContext *ctx, void *arg, \ DEFINE_INTERP_FUNC_1D(nearest, 8) DEFINE_INTERP_FUNC_1D(linear, 8) +DEFINE_INTERP_FUNC_1D(cosine, 8) DEFINE_INTERP_FUNC_1D(cubic, 8) +DEFINE_INTERP_FUNC_1D(spline, 8) DEFINE_INTERP_FUNC_1D(nearest, 16) DEFINE_INTERP_FUNC_1D(linear, 16) +DEFINE_INTERP_FUNC_1D(cosine, 16) DEFINE_INTERP_FUNC_1D(cubic, 16) +DEFINE_INTERP_FUNC_1D(spline, 16) static int config_input_1d(AVFilterLink *inlink) { @@ -1303,7 +1361,9 @@ static int config_input_1d(AVFilterLink *inlink) switch (lut1d->interpolation) { case INTERPOLATE_1D_NEAREST: SET_FUNC_1D(nearest); break; case INTERPOLATE_1D_LINEAR: SET_FUNC_1D(linear); break; + case INTERPOLATE_1D_COSINE: SET_FUNC_1D(cosine); break; case INTERPOLATE_1D_CUBIC: SET_FUNC_1D(cubic); break; + case INTERPOLATE_1D_SPLINE: SET_FUNC_1D(spline); break; default: av_assert0(0); }