These functions can be reused by other colorspace filters, so move them to common file. No functional changes.
Showing the changes to libavfilter (3 of 3 files). Show all
Unified · Split
| @@ -93,6 +93,77 @@ void ff_fill_rgb2xyz_table(const struct PrimaryCoefficients *coeffs, · 92 unmodified lines | ||
| 93 | 93 | rgb2xyz[2][1] *= sg; |
| 94 | 94 | rgb2xyz[2][2] *= sb; |
| 95 | 95 | } |
| 96 | +static const double ycgco_matrix[3][3] = | |
| 97 | +{ | |
| 98 | + { 0.25, 0.5, 0.25 }, | |
| 99 | + { -0.25, 0.5, -0.25 }, | |
| 100 | + { 0.5, 0, -0.5 }, | |
| 101 | +}; | |
| 102 | + | |
| 103 | +static const double gbr_matrix[3][3] = | |
| 104 | +{ | |
| 105 | + { 0, 1, 0 }, | |
| 106 | + { 0, -0.5, 0.5 }, | |
| 107 | + { 0.5, -0.5, 0 }, | |
| 108 | +}; | |
| 109 | + | |
| 110 | +/* | |
| 111 | + * All constants explained in e.g. https://linuxtv.org/downloads/v4l-dvb-apis/ch02s06.html | |
| 112 | + * The older ones (bt470bg/m) are also explained in their respective ITU docs | |
| 113 | + * (e.g. https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf) | |
| 114 | + * whereas the newer ones can typically be copied directly from wikipedia :) | |
| 115 | + */ | |
| 116 | +static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = { | |
| 117 | + [AVCOL_SPC_FCC] = { 0.30, 0.59, 0.11 }, | |
| 118 | + [AVCOL_SPC_BT470BG] = { 0.299, 0.587, 0.114 }, | |
| 119 | + [AVCOL_SPC_SMPTE170M] = { 0.299, 0.587, 0.114 }, | |
| 120 | + [AVCOL_SPC_BT709] = { 0.2126, 0.7152, 0.0722 }, | |
| 121 | + [AVCOL_SPC_SMPTE240M] = { 0.212, 0.701, 0.087 }, | |
| 122 | + [AVCOL_SPC_YCOCG] = { 0.25, 0.5, 0.25 }, | |
| 123 | + [AVCOL_SPC_RGB] = { 1, 1, 1 }, | |
| 124 | + [AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 }, | |
| 125 | + [AVCOL_SPC_BT2020_CL] = { 0.2627, 0.6780, 0.0593 }, | |
| 126 | +}; | |
| 127 | + | |
| 128 | +const struct LumaCoefficients *ff_get_luma_coefficients(enum AVColorSpace csp) | |
| 129 | +{ | |
| 130 | + const struct LumaCoefficients *coeffs; | |
| 131 | + | |
| 132 | + if (csp >= AVCOL_SPC_NB) | |
| 133 | + return NULL; | |
| 134 | + coeffs = &luma_coefficients[csp]; | |
| 135 | + if (!coeffs->cr) | |
| 136 | + return NULL; | |
| 137 | + | |
| 138 | + return coeffs; | |
| 139 | +} | |
| 140 | + | |
| 141 | +void ff_fill_rgb2yuv_table(const struct LumaCoefficients *coeffs, | |
| 142 | + double rgb2yuv[3][3]) | |
| 143 | +{ | |
| 144 | + double bscale, rscale; | |
| 145 | + | |
| 146 | + // special ycgco matrix | |
| 147 | + if (coeffs->cr == 0.25 && coeffs->cg == 0.5 && coeffs->cb == 0.25) { | |
| 148 | + memcpy(rgb2yuv, ycgco_matrix, sizeof(double) * 9); | |
| 149 | + return; | |
| 150 | + } else if (coeffs->cr == 1 && coeffs->cg == 1 && coeffs->cb == 1) { | |
| 151 | + memcpy(rgb2yuv, gbr_matrix, sizeof(double) * 9); | |
| 152 | + return; | |
| 153 | + } | |
| 154 | + | |
| 155 | + rgb2yuv[0][0] = coeffs->cr; | |
| 156 | + rgb2yuv[0][1] = coeffs->cg; | |
| 157 | + rgb2yuv[0][2] = coeffs->cb; | |
| 158 | + bscale = 0.5 / (coeffs->cb - 1.0); | |
| 159 | + rscale = 0.5 / (coeffs->cr - 1.0); | |
| 160 | + rgb2yuv[1][0] = bscale * coeffs->cr; | |
| 161 | + rgb2yuv[1][1] = bscale * coeffs->cg; | |
| 162 | + rgb2yuv[1][2] = 0.5; | |
| 163 | + rgb2yuv[2][0] = 0.5; | |
| 164 | + rgb2yuv[2][1] = rscale * coeffs->cg; | |
| 165 | + rgb2yuv[2][2] = rscale * coeffs->cb; | |
| 166 | +} | |
| 96 | 167 | |
| 97 | 168 | double ff_determine_signal_peak(AVFrame *in) |
| 98 | 169 | { |
| @@ -44,6 +44,10 @@ void ff_fill_rgb2xyz_table(const struct PrimaryCoefficients *coeffs, · 43 unmodified lines | ||
| 44 | 44 | const struct WhitepointCoefficients *wp, |
| 45 | 45 | double rgb2xyz[3][3]); |
| 46 | 46 | |
| 47 | +const struct LumaCoefficients *ff_get_luma_coefficients(enum AVColorSpace csp); | |
| 48 | +void ff_fill_rgb2yuv_table(const struct LumaCoefficients *coeffs, | |
| 49 | + double rgb2yuv[3][3]); | |
| 50 | + | |
| 47 | 51 | double ff_determine_signal_peak(AVFrame *in); |
| 48 | 52 | void ff_update_hdr_metadata(AVFrame *in, double peak); |
| 49 | 53 | |
| @@ -170,78 +170,6 @@ typedef struct ColorSpaceContext { · 169 unmodified lines | ||
| 170 | 170 | // FIXME dithering if bitdepth goes down? |
| 171 | 171 | // FIXME bitexact for fate integration? |
| 172 | 172 | |
| 173 | −static const double ycgco_matrix[3][3] = | |
| 174 | −{ | |
| 175 | − { 0.25, 0.5, 0.25 }, | |
| 176 | − { -0.25, 0.5, -0.25 }, | |
| 177 | − { 0.5, 0, -0.5 }, | |
| 178 | −}; | |
| 179 | − | |
| 180 | −static const double gbr_matrix[3][3] = | |
| 181 | −{ | |
| 182 | − { 0, 1, 0 }, | |
| 183 | − { 0, -0.5, 0.5 }, | |
| 184 | − { 0.5, -0.5, 0 }, | |
| 185 | −}; | |
| 186 | − | |
| 187 | −/* | |
| 188 | − * All constants explained in e.g. https://linuxtv.org/downloads/v4l-dvb-apis/ch02s06.html | |
| 189 | − * The older ones (bt470bg/m) are also explained in their respective ITU docs | |
| 190 | − * (e.g. https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf) | |
| 191 | − * whereas the newer ones can typically be copied directly from wikipedia :) | |
| 192 | − */ | |
| 193 | −static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = { | |
| 194 | − [AVCOL_SPC_FCC] = { 0.30, 0.59, 0.11 }, | |
| 195 | − [AVCOL_SPC_BT470BG] = { 0.299, 0.587, 0.114 }, | |
| 196 | − [AVCOL_SPC_SMPTE170M] = { 0.299, 0.587, 0.114 }, | |
| 197 | − [AVCOL_SPC_BT709] = { 0.2126, 0.7152, 0.0722 }, | |
| 198 | − [AVCOL_SPC_SMPTE240M] = { 0.212, 0.701, 0.087 }, | |
| 199 | − [AVCOL_SPC_YCOCG] = { 0.25, 0.5, 0.25 }, | |
| 200 | − [AVCOL_SPC_RGB] = { 1, 1, 1 }, | |
| 201 | − [AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 }, | |
| 202 | − [AVCOL_SPC_BT2020_CL] = { 0.2627, 0.6780, 0.0593 }, | |
| 203 | −}; | |
| 204 | − | |
| 205 | −static const struct LumaCoefficients *get_luma_coefficients(enum AVColorSpace csp) | |
| 206 | −{ | |
| 207 | − const struct LumaCoefficients *coeffs; | |
| 208 | − | |
| 209 | − if (csp >= AVCOL_SPC_NB) | |
| 210 | − return NULL; | |
| 211 | − coeffs = &luma_coefficients[csp]; | |
| 212 | − if (!coeffs->cr) | |
| 213 | − return NULL; | |
| 214 | − | |
| 215 | − return coeffs; | |
| 216 | −} | |
| 217 | − | |
| 218 | −static void fill_rgb2yuv_table(const struct LumaCoefficients *coeffs, | |
| 219 | − double rgb2yuv[3][3]) | |
| 220 | −{ | |
| 221 | − double bscale, rscale; | |
| 222 | − | |
| 223 | − // special ycgco matrix | |
| 224 | − if (coeffs->cr == 0.25 && coeffs->cg == 0.5 && coeffs->cb == 0.25) { | |
| 225 | − memcpy(rgb2yuv, ycgco_matrix, sizeof(double) * 9); | |
| 226 | − return; | |
| 227 | − } else if (coeffs->cr == 1 && coeffs->cg == 1 && coeffs->cb == 1) { | |
| 228 | − memcpy(rgb2yuv, gbr_matrix, sizeof(double) * 9); | |
| 229 | − return; | |
| 230 | − } | |
| 231 | − | |
| 232 | − rgb2yuv[0][0] = coeffs->cr; | |
| 233 | − rgb2yuv[0][1] = coeffs->cg; | |
| 234 | − rgb2yuv[0][2] = coeffs->cb; | |
| 235 | − bscale = 0.5 / (coeffs->cb - 1.0); | |
| 236 | − rscale = 0.5 / (coeffs->cr - 1.0); | |
| 237 | − rgb2yuv[1][0] = bscale * coeffs->cr; | |
| 238 | − rgb2yuv[1][1] = bscale * coeffs->cg; | |
| 239 | − rgb2yuv[1][2] = 0.5; | |
| 240 | − rgb2yuv[2][0] = 0.5; | |
| 241 | − rgb2yuv[2][1] = rscale * coeffs->cg; | |
| 242 | − rgb2yuv[2][2] = rscale * coeffs->cb; | |
| 243 | −} | |
| 244 | − | |
| 245 | 173 | // FIXME I'm pretty sure gamma22/28 also have a linear toe slope, but I can't |
| 246 | 174 | // find any actual tables that document their real values... |
| 247 | 175 | // See http://www.13thmonkey.org/~boris/gammacorrection/ first graph why it matters |
| @@ -669,7 +597,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 421 unmodified lines | ||
| 669 | 597 | s->in_rng = in->color_range; |
| 670 | 598 | if (s->user_irng != AVCOL_RANGE_UNSPECIFIED) |
| 671 | 599 | s->in_rng = s->user_irng; |
| 672 | − s->in_lumacoef = get_luma_coefficients(s->in_csp); | |
| 600 | + s->in_lumacoef = ff_get_luma_coefficients(s->in_csp); | |
| 673 | 601 | if (!s->in_lumacoef) { |
| 674 | 602 | av_log(ctx, AV_LOG_ERROR, |
| 675 | 603 | "Unsupported input colorspace %d (%s)\n", |
| @@ -682,7 +610,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 6 unmodified lines | ||
| 682 | 610 | if (!s->out_lumacoef) { |
| 683 | 611 | s->out_csp = out->colorspace; |
| 684 | 612 | s->out_rng = out->color_range; |
| 685 | − s->out_lumacoef = get_luma_coefficients(s->out_csp); | |
| 613 | + s->out_lumacoef = ff_get_luma_coefficients(s->out_csp); | |
| 686 | 614 | if (!s->out_lumacoef) { |
| 687 | 615 | if (s->out_csp == AVCOL_SPC_UNSPECIFIED) { |
| 688 | 616 | if (s->user_all == CS_UNSPECIFIED) { |
| @@ -724,7 +652,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 35 unmodified lines | ||
| 724 | 652 | } |
| 725 | 653 | for (n = 0; n < 8; n++) |
| 726 | 654 | s->yuv_offset[0][n] = off; |
| 727 | − fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv); | |
| 655 | + ff_fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv); | |
| 728 | 656 | ff_matrix_invert_3x3(rgb2yuv, yuv2rgb); |
| 729 | 657 | bits = 1 << (in_desc->comp[0].depth - 1); |
| 730 | 658 | for (n = 0; n < 3; n++) { |
| @@ -757,7 +685,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 26 unmodified lines | ||
| 757 | 685 | } |
| 758 | 686 | for (n = 0; n < 8; n++) |
| 759 | 687 | s->yuv_offset[1][n] = off; |
| 760 | − fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv); | |
| 688 | + ff_fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv); | |
| 761 | 689 | bits = 1 << (29 - out_desc->comp[0].depth); |
| 762 | 690 | for (out_rng = s->out_y_rng, n = 0; n < 3; n++, out_rng = s->out_uv_rng) { |
| 763 | 691 | for (m = 0; m < 3; m++) { |