History

avfilter/vf_ciescope: little optimizations

Paul B Mahol committed 4 years ago

Commit da4b7c21 parent 8b717a41 file · +167 −137Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_ciescope.c+167−137
@@ -62,12 +62,14 @@ typedef struct CiescopeContext { · 61 unmodified lines
6262 float contrast;
6363 int background;
6464
65− double log2lin[65536];
66− double igamma;
67− double i[3][3];
68− double m[3][3];
65+ float log2lin[65536];
66+ float igamma;
67+ float i[3][3];
68+ float m[3][3];
6969 AVFrame *f;
70− void (*filter)(AVFilterContext *ctx, AVFrame *in, double *cx, double *cy, int x, int y);
70+ void (*filter)(AVFilterContext *ctx, const uint8_t *ptr,
71+ ptrdiff_t linesize,
72+ float *cx, float *cy, int x, int y);
7173 } CiescopeContext;
7274
7375 #define OFFSET(x) offsetof(CiescopeContext, x)
@@ -163,11 +165,11 @@ static int config_output(AVFilterLink *outlink) · 89 unmodified lines
163165 point. */
164166
165167 struct ColorSystem {
166− double xRed, yRed, /* Red primary illuminant */
167− xGreen, yGreen, /* Green primary illuminant */
168− xBlue, yBlue, /* Blue primary illuminant */
169− xWhite, yWhite, /* White point */
170− gamma; /* gamma of nonlinear correction */
168+ float xRed, yRed, /* Red primary illuminant */
169+ xGreen, yGreen, /* Green primary illuminant */
170+ xBlue, yBlue, /* Blue primary illuminant */
171+ xWhite, yWhite, /* White point */
172+ gamma; /* gamma of nonlinear correction */
171173 };
172174
173175 static float const spectral_chromaticity[][3] = {
@@ -712,23 +714,23 @@ static struct ColorSystem CustomSystem = { · 538 unmodified lines
712714 */
713715
714716 static void
715−uv_to_xy(double const u,
716− double const v,
717− double * const xc,
718− double * const yc)
717+uv_to_xy(float const u,
718+ float const v,
719+ float *const xc,
720+ float *const yc)
719721 {
720722 /*
721723 Given 1970 coordinates u, v, determine 1931 chromaticities x, y
722724 */
723− *xc = 3*u / (2*u - 8*v + 4);
724− *yc = 2*v / (2*u - 8*v + 4);
725+ *xc = 3.f*u / (2.f*u - 8.f*v + 4.f);
726+ *yc = 2.f*v / (2.f*u - 8.f*v + 4.f);
725727 }
726728
727729 static void
728−upvp_to_xy(double const up,
729− double const vp,
730− double * const xc,
731− double * const yc)
730+upvp_to_xy(float const up,
731+ float const vp,
732+ float * const xc,
733+ float * const yc)
732734 {
733735 /*
734736 Given 1976 coordinates u', v', determine 1931 chromaticities x, y
@@ -738,48 +740,50 @@ upvp_to_xy(double const up, · 3 unmodified lines
738740 }
739741
740742 static void
741−xy_to_upvp(double xc,
742− double yc,
743− double * const up,
744− double * const vp)
743+xy_to_upvp(float xc,
744+ float yc,
745+ float * const up,
746+ float * const vp)
745747 {
746748 /*
747749 Given 1931 chromaticities x, y, determine 1976 coordinates u', v'
748750 */
749− *up = 4*xc / (- 2*xc + 12*yc + 3);
750− *vp = 9*yc / (- 2*xc + 12*yc + 3);
751+ const float scale = 1.f / (-2.f*xc + 12.f*yc + 3.f);
752+ *up = 4.f*xc * scale;
753+ *vp = 9.f*yc * scale;
751754 }
752755
753756 static void
754−xy_to_uv(double xc,
755− double yc,
756− double * const u,
757− double * const v)
757+xy_to_uv(float xc,
758+ float yc,
759+ float * const u,
760+ float * const v)
758761 {
759762 /*
760763 Given 1931 chromaticities x, y, determine 1960 coordinates u, v
761764 */
762− *u = 4*xc / (- 2*xc + 12*yc + 3);
763− *v = 6*yc / (- 2*xc + 12*yc + 3);
765+ const float scale = 1.f / (-2.f*xc + 12.f*yc + 3.f);
766+ *u = 4.f*xc * scale;
767+ *v = 6.f*yc * scale;
764768 }
765769
766770 static void
767−xyz_to_rgb(const double m[3][3],
768− double xc, double yc, double zc,
769− double * const r, double * const g, double * const b)
771+xyz_to_rgb(const float m[3][3],
772+ float xc, float yc, float zc,
773+ float * const r, float * const g, float * const b)
770774 {
771775 *r = m[0][0]*xc + m[0][1]*yc + m[0][2]*zc;
772776 *g = m[1][0]*xc + m[1][1]*yc + m[1][2]*zc;
773777 *b = m[2][0]*xc + m[2][1]*yc + m[2][2]*zc;
774778 }
775779
776−static void invert_matrix3x3(double in[3][3], double out[3][3])
780+static void invert_matrix3x3(float in[3][3], float out[3][3])
777781 {
778− double m00 = in[0][0], m01 = in[0][1], m02 = in[0][2],
782+ float m00 = in[0][0], m01 = in[0][1], m02 = in[0][2],
779783 m10 = in[1][0], m11 = in[1][1], m12 = in[1][2],
780784 m20 = in[2][0], m21 = in[2][1], m22 = in[2][2];
781785 int i, j;
782− double det;
786+ float det;
783787
784788 out[0][0] = (m11 * m22 - m21 * m12);
785789 out[0][1] = -(m01 * m22 - m21 * m02);
@@ -800,9 +804,9 @@ static void invert_matrix3x3(double in[3][3], double out[3][3]) · 14 unmodified lines
800804 }
801805 }
802806
803−static void get_rgb2xyz_matrix(struct ColorSystem system, double m[3][3])
807+static void get_rgb2xyz_matrix(struct ColorSystem system, float m[3][3])
804808 {
805− double S[3], X[4], Z[4];
809+ float S[3], X[4], Z[4];
806810 int i;
807811
808812 X[0] = system.xRed / system.yRed;
@@ -834,31 +838,32 @@ static void get_rgb2xyz_matrix(struct ColorSystem system, double m[3][3]) · 25 unmodified lines
834838 }
835839
836840 static void
837−rgb_to_xy(double rc,
838− double gc,
839− double bc,
840− double * const x,
841− double * const y,
842− double * const z,
843− const double m[3][3])
841+rgb_to_xy(float rc,
842+ float gc,
843+ float bc,
844+ float * const x,
845+ float * const y,
846+ float * const z,
847+ const float m[3][3])
844848 {
845− double sum;
849+ float scale;
846850
847851 *x = m[0][0] * rc + m[0][1] * gc + m[0][2] * bc;
848852 *y = m[1][0] * rc + m[1][1] * gc + m[1][2] * bc;
849853 *z = m[2][0] * rc + m[2][1] * gc + m[2][2] * bc;
850854
851− sum = *x + *y + *z;
852− if (sum == 0)
853− sum = 1;
854− *x = *x / sum;
855− *y = *y / sum;
855+ scale = *x + *y + *z;
856+ if (scale == 0.f)
857+ scale = 1.f;
858+ scale = 1.f / scale;
859+ *x = *x * scale;
860+ *y = *y * scale;
856861 }
857862
858863 static int
859−constrain_rgb(double * const r,
860− double * const g,
861− double * const b)
864+constrain_rgb(float * const r,
865+ float * const g,
866+ float * const b)
862867 {
863868 /*----------------------------------------------------------------------------
864869 If the requested RGB shade contains a negative weight for one of
@@ -866,7 +871,7 @@ constrain_rgb(double * const r, · 1 unmodified line
866871 the given triple of primaries. Desaturate it by adding white,
867872 equal quantities of R, G, and B, enough to make RGB all positive.
868873 -----------------------------------------------------------------------------*/
869− double w;
874+ float w;
870875
871876 /* Amount of white needed is w = - min(0, *r, *g, *b) */
872877 w = (0 < *r) ? 0 : *r;
@@ -886,7 +891,7 @@ constrain_rgb(double * const r, · 13 unmodified lines
886891
887892 static void
888893 gamma_correct(const struct ColorSystem * const cs,
889− double * const c)
894+ float * const c)
890895 {
891896 /*----------------------------------------------------------------------------
892897 Transform linear RGB values to nonlinear RGB values.
@@ -899,8 +904,8 @@ gamma_correct(const struct ColorSystem * const cs, · 6 unmodified lines
899904 http://www.inforamp.net/~poynton/ColorFAQ.html
900905 http://www.inforamp.net/~poynton/GammaFAQ.html
901906 -----------------------------------------------------------------------------*/
902− double gamma;
903− double cc;
907+ float gamma;
908+ float cc;
904909
905910 gamma = cs->gamma;
906911
@@ -922,9 +927,9 @@ gamma_correct(const struct ColorSystem * const cs, · 15 unmodified lines
922927
923928 static void
924929 gamma_correct_rgb(const struct ColorSystem * const cs,
925− double * const r,
926− double * const g,
927− double * const b)
930+ float * const r,
931+ float * const g,
932+ float * const b)
928933 {
929934 gamma_correct(cs, r);
930935 gamma_correct(cs, g);
@@ -938,24 +943,24 @@ gamma_correct_rgb(const struct ColorSystem * const cs, · 7 unmodified lines
938943 #define Sz(x) (((x) * (int)FFMIN(w, h)) / 512)
939944
940945 static void
941−monochrome_color_location(double waveLength, int w, int h,
946+monochrome_color_location(float waveLength, int w, int h,
942947 int cie, int *xP, int *yP)
943948 {
944949 const int ix = waveLength - 360;
945− const double pX = spectral_chromaticity[ix][0];
946− const double pY = spectral_chromaticity[ix][1];
947− const double pZ = spectral_chromaticity[ix][2];
948− const double px = pX / (pX + pY + pZ);
949− const double py = pY / (pX + pY + pZ);
950+ const float pX = spectral_chromaticity[ix][0];
951+ const float pY = spectral_chromaticity[ix][1];
952+ const float pZ = spectral_chromaticity[ix][2];
953+ const float px = pX / (pX + pY + pZ);
954+ const float py = pY / (pX + pY + pZ);
950955
951956 if (cie == LUV) {
952− double up, vp;
957+ float up, vp;
953958
954959 xy_to_upvp(px, py, &up, &vp);
955960 *xP = up * (w - 1);
956961 *yP = (h - 1) - vp * (h - 1);
957962 } else if (cie == UCS) {
958− double u, v;
963+ float u, v;
959964
960965 xy_to_uv(px, py, &u, &v);
961966 *xP = u * (w - 1);
@@ -1099,7 +1104,7 @@ fill_in_tongue(uint16_t* const pixels, · 137 unmodified lines
10991104 int const h,
11001105 uint16_t const maxval,
11011106 const struct ColorSystem * const cs,
1102− double const m[3][3],
1107+ float const m[3][3],
11031108 int const cie,
11041109 int const correct_gamma,
11051110 float const contrast)
@@ -1122,24 +1127,24 @@ fill_in_tongue(uint16_t* const pixels, · 16 unmodified lines
11221127 int x;
11231128
11241129 for (x = leftEdge; x <= rightEdge; ++x) {
1125− double cx, cy, cz, jr, jg, jb, jmax;
1130+ float cx, cy, cz, jr, jg, jb, jmax;
11261131 int r, g, b, mx = maxval;
11271132
11281133 if (cie == LUV) {
1129− double up, vp;
1130− up = ((double) x) / (w - 1);
1131− vp = 1.0 - ((double) y) / (h - 1);
1134+ float up, vp;
1135+ up = ((float) x) / (w - 1);
1136+ vp = 1.0 - ((float) y) / (h - 1);
11321137 upvp_to_xy(up, vp, &cx, &cy);
11331138 cz = 1.0 - (cx + cy);
11341139 } else if (cie == UCS) {
1135− double u, v;
1136− u = ((double) x) / (w - 1);
1137− v = 1.0 - ((double) y) / (h - 1);
1140+ float u, v;
1141+ u = ((float) x) / (w - 1);
1142+ v = 1.0 - ((float) y) / (h - 1);
11381143 uv_to_xy(u, v, &cx, &cy);
11391144 cz = 1.0 - (cx + cy);
11401145 } else if (cie == XYY) {
1141− cx = ((double) x) / (w - 1);
1142− cy = 1.0 - ((double) y) / (h - 1);
1146+ cx = ((float) x) / (w - 1);
1147+ cy = 1.0 - ((float) y) / (h - 1);
11431148 cz = 1.0 - (cx + cy);
11441149 } else {
11451150 av_assert0(0);
@@ -1189,12 +1194,12 @@ plot_white_point(uint16_t* pixels, · 43 unmodified lines
11891194 int wx, wy;
11901195
11911196 if (cie == LUV) {
1192− double wup, wvp;
1197+ float wup, wvp;
11931198 xy_to_upvp(cs->xWhite, cs->yWhite, &wup, &wvp);
11941199 wx = (w - 1) * wup;
11951200 wy = (h - 1) - ((int) ((h - 1) * wvp));
11961201 } else if (cie == UCS) {
1197− double wu, wv;
1202+ float wu, wv;
11981203 xy_to_uv(cs->xWhite, cs->yWhite, &wu, &wv);
11991204 wx = (w - 1) * wu;
12001205 wy = (h - 1) - ((int) ((h - 1) * wv));
@@ -1234,68 +1239,82 @@ static int draw_background(AVFilterContext *ctx) · 33 unmodified lines
12341239
12351240 tongue_outline(pixels, s->f->linesize[0] / 2, w, h, 65535, s->cie);
12361241
1237− fill_in_tongue(pixels, s->f->linesize[0] / 2, w, h, 65535, cs, (const double (*)[3])s->i, s->cie,
1242+ fill_in_tongue(pixels, s->f->linesize[0] / 2, w, h, 65535, cs, (const float (*)[3])s->i, s->cie,
12381243 s->correct_gamma, s->contrast);
12391244
12401245 return 0;
12411246 }
12421247
1243−static void filter_rgb48(AVFilterContext *ctx, AVFrame *in, double *cx, double *cy, int x, int y)
1248+static void filter_rgb48(AVFilterContext *ctx, const uint8_t *ptr,
1249+ ptrdiff_t linesize,
1250+ float *cx, float *cy, int x, int y)
12441251 {
12451252 CiescopeContext *s = ctx->priv;
1246− const uint16_t* src = (const uint16_t*)(in->data[0] + in->linesize[0] * y + x * 6);
1247− double r = src[0] / 65535.;
1248− double g = src[1] / 65535.;
1249− double b = src[2] / 65535.;
1250− double cz;
1251−
1252− rgb_to_xy(r, g, b, cx, cy, &cz, (const double (*)[3])s->m);
1253+ const float scale = 1. / 65535.;
1254+ const uint16_t *src = (const uint16_t*)(ptr + linesize * y + x * 6);
1255+ float r = src[0] * scale;
1256+ float g = src[1] * scale;
1257+ float b = src[2] * scale;
1258+ float cz;
1259+
1260+ rgb_to_xy(r, g, b, cx, cy, &cz, (const float (*)[3])s->m);
12531261 }
12541262
1255−static void filter_rgba64(AVFilterContext *ctx, AVFrame *in, double *cx, double *cy, int x, int y)
1263+static void filter_rgba64(AVFilterContext *ctx, const uint8_t *ptr,
1264+ ptrdiff_t linesize,
1265+ float *cx, float *cy, int x, int y)
12561266 {
12571267 CiescopeContext *s = ctx->priv;
1258− const uint16_t* src = (const uint16_t*)(in->data[0] + in->linesize[0] * y + x * 8);
1259− double r = src[0] / 65535.;
1260− double g = src[1] / 65535.;
1261− double b = src[2] / 65535.;
1262− double cz;
1263−
1264− rgb_to_xy(r, g, b, cx, cy, &cz, (const double (*)[3])s->m);
1268+ const float scale = 1. / 65535.;
1269+ const uint16_t *src = (const uint16_t*)(ptr + linesize * y + x * 8);
1270+ float r = src[0] * scale;
1271+ float g = src[1] * scale;
1272+ float b = src[2] * scale;
1273+ float cz;
1274+
1275+ rgb_to_xy(r, g, b, cx, cy, &cz, (const float (*)[3])s->m);
12651276 }
12661277
1267−static void filter_rgb24(AVFilterContext *ctx, AVFrame *in, double *cx, double *cy, int x, int y)
1278+static void filter_rgb24(AVFilterContext *ctx, const uint8_t *ptr,
1279+ ptrdiff_t linesize,
1280+ float *cx, float *cy, int x, int y)
12681281 {
12691282 CiescopeContext *s = ctx->priv;
1270− const uint8_t* src = in->data[0] + in->linesize[0] * y + x * 3;
1271− double r = src[0] / 255.;
1272− double g = src[1] / 255.;
1273− double b = src[2] / 255.;
1274− double cz;
1275−
1276− rgb_to_xy(r, g, b, cx, cy, &cz, (const double (*)[3])s->m);
1283+ const float scale = 1. / 255.;
1284+ const uint8_t *src = ptr + linesize * y + x * 3;
1285+ float r = src[0] * scale;
1286+ float g = src[1] * scale;
1287+ float b = src[2] * scale;
1288+ float cz;
1289+
1290+ rgb_to_xy(r, g, b, cx, cy, &cz, (const float (*)[3])s->m);
12771291 }
12781292
1279−static void filter_rgba(AVFilterContext *ctx, AVFrame *in, double *cx, double *cy, int x, int y)
1293+static void filter_rgba(AVFilterContext *ctx, const uint8_t *ptr,
1294+ ptrdiff_t linesize,
1295+ float *cx, float *cy, int x, int y)
12801296 {
12811297 CiescopeContext *s = ctx->priv;
1282− const uint8_t* src = in->data[0] + in->linesize[0] * y + x * 4;
1283− double r = src[0] / 255.;
1284− double g = src[1] / 255.;
1285− double b = src[2] / 255.;
1286− double cz;
1287−
1288− rgb_to_xy(r, g, b, cx, cy, &cz, (const double (*)[3])s->m);
1298+ const float scale = 1. / 255.;
1299+ const uint8_t *src = ptr + linesize * y + x * 4;
1300+ float r = src[0] * scale;
1301+ float g = src[1] * scale;
1302+ float b = src[2] * scale;
1303+ float cz;
1304+
1305+ rgb_to_xy(r, g, b, cx, cy, &cz, (const float (*)[3])s->m);
12891306 }
12901307
1291−static void filter_xyz(AVFilterContext *ctx, AVFrame *in, double *cx, double *cy, int x, int y)
1308+static void filter_xyz(AVFilterContext *ctx, const uint8_t *ptr,
1309+ ptrdiff_t linesize,
1310+ float *cx, float *cy, int x, int y)
12921311 {
12931312 CiescopeContext *s = ctx->priv;
1294− const uint16_t* src = (uint16_t *)(in->data[0] + in->linesize[0] * y + x * 6);
1295− double lx = s->log2lin[src[0]];
1296− double ly = s->log2lin[src[1]];
1297− double lz = s->log2lin[src[2]];
1298− double sum = lx + ly + lz;
1313+ const uint16_t* src = (uint16_t *)(ptr + linesize * y + x * 6);
1314+ float lx = s->log2lin[src[0]];
1315+ float ly = s->log2lin[src[1]];
1316+ float lz = s->log2lin[src[2]];
1317+ float sum = lx + ly + lz;
12991318
13001319 if (sum == 0)
13011320 sum = 1;
@@ -1315,7 +1334,7 @@ static void plot_gamuts(uint16_t *pixels, int linesize, int w, int h, · 13 unmodified lines
13151334 if (!((1 << i) & gamuts))
13161335 continue;
13171336 if (cie == LUV) {
1318− double wup, wvp;
1337+ float wup, wvp;
13191338 xy_to_upvp(cs->xRed, cs->yRed, &wup, &wvp);
13201339 rx = (w - 1) * wup;
13211340 ry = (h - 1) - ((int) ((h - 1) * wvp));
@@ -1326,7 +1345,7 @@ static void plot_gamuts(uint16_t *pixels, int linesize, int w, int h, · 4 unmodified lines
13261345 bx = (w - 1) * wup;
13271346 by = (h - 1) - ((int) ((h - 1) * wvp));
13281347 } else if (cie == UCS) {
1329− double wu, wv;
1348+ float wu, wv;
13301349 xy_to_uv(cs->xRed, cs->yRed, &wu, &wv);
13311350 rx = (w - 1) * wu;
13321351 ry = (h - 1) - ((int) ((h - 1) * wv));
@@ -1384,37 +1403,48 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 51 unmodified lines
13841403 }
13851404
13861405 for (y = 0; y < in->height; y++) {
1406+ const uint8_t *src = in->data[0];
1407+ const ptrdiff_t src_linesize = in->linesize[0];
1408+ uint16_t *dst = (uint16_t *)out->data[0];
1409+ const ptrdiff_t linesize = out->linesize[0] / 2;
1410+ const int w_1 = w - 1;
1411+ const int h_1 = h - 1;
1412+
13871413 for (x = 0; x < in->width; x++) {
1388− double cx, cy;
1389− uint16_t *dst;
1390− int wx, wy;
1414+ float cx, cy;
1415+ int wx, wy, pos;
1416+ int r, g, b;
13911417
1392− s->filter(ctx, in, &cx, &cy, x, y);
1418+ s->filter(ctx, src, src_linesize, &cx, &cy, x, y);
13931419
13941420 if (s->cie == LUV) {
1395− double up, vp;
1421+ float up, vp;
13961422 xy_to_upvp(cx, cy, &up, &vp);
13971423 cx = up;
13981424 cy = vp;
13991425 } else if (s->cie == UCS) {
1400− double u, v;
1426+ float u, v;
14011427 xy_to_uv(cx, cy, &u, &v);
14021428 cx = u;
14031429 cy = v;
14041430 }
14051431
1406− wx = (w - 1) * cx;
1407− wy = (h - 1) - ((h - 1) * cy);
1432+ wx = w_1 * cx;
1433+ wy = h_1 - h_1 * cy;
14081434
14091435 if (wx < 0 || wx >= w ||
14101436 wy < 0 || wy >= h)
14111437 continue;
14121438
1413− dst = (uint16_t *)(out->data[0] + wy * out->linesize[0] + wx * 8 + 0);
1414− dst[0] = FFMIN(dst[0] + i, 65535);
1415− dst[1] = FFMIN(dst[1] + i, 65535);
1416− dst[2] = FFMIN(dst[2] + i, 65535);
1417− dst[3] = 65535;
1439+ pos = wy * linesize + wx * 4;
1440+ r = dst[pos + 0] + i;
1441+ g = dst[pos + 1] + i;
1442+ b = dst[pos + 2] + i;
1443+
1444+ dst[pos + 0] = FFMIN(r, 65535);
1445+ dst[pos + 1] = FFMIN(g, 65535);
1446+ dst[pos + 2] = FFMIN(b, 65535);
1447+ dst[pos + 3] = 65535;
14181448 }
14191449 }
14201450

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