| @@ -170,78 +170,6 @@ typedef struct ColorSpaceContext { · 169 unmodified lines |
| 170 | // FIXME dithering if bitdepth goes down? | 170 | // FIXME dithering if bitdepth goes down? |
| 171 | // FIXME bitexact for fate integration? | 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 | // FIXME I'm pretty sure gamma22/28 also have a linear toe slope, but I can't | 173 | // FIXME I'm pretty sure gamma22/28 also have a linear toe slope, but I can't |
| 246 | // find any actual tables that document their real values... | 174 | // find any actual tables that document their real values... |
| 247 | // See http://www.13thmonkey.org/~boris/gammacorrection/ first graph why it matters | 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 | s->in_rng = in->color_range; | 597 | s->in_rng = in->color_range; |
| 670 | if (s->user_irng != AVCOL_RANGE_UNSPECIFIED) | 598 | if (s->user_irng != AVCOL_RANGE_UNSPECIFIED) |
| 671 | s->in_rng = s->user_irng; | 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 | if (!s->in_lumacoef) { | 601 | if (!s->in_lumacoef) { |
| 674 | av_log(ctx, AV_LOG_ERROR, | 602 | av_log(ctx, AV_LOG_ERROR, |
| 675 | "Unsupported input colorspace %d (%s)\n", | 603 | "Unsupported input colorspace %d (%s)\n", |
| @@ -682,7 +610,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 6 unmodified lines |
| 682 | if (!s->out_lumacoef) { | 610 | if (!s->out_lumacoef) { |
| 683 | s->out_csp = out->colorspace; | 611 | s->out_csp = out->colorspace; |
| 684 | s->out_rng = out->color_range; | 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 | if (!s->out_lumacoef) { | 614 | if (!s->out_lumacoef) { |
| 687 | if (s->out_csp == AVCOL_SPC_UNSPECIFIED) { | 615 | if (s->out_csp == AVCOL_SPC_UNSPECIFIED) { |
| 688 | if (s->user_all == CS_UNSPECIFIED) { | 616 | if (s->user_all == CS_UNSPECIFIED) { |
| @@ -724,7 +652,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 35 unmodified lines |
| 724 | } | 652 | } |
| 725 | for (n = 0; n < 8; n++) | 653 | for (n = 0; n < 8; n++) |
| 726 | s->yuv_offset[0][n] = off; | 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 | ff_matrix_invert_3x3(rgb2yuv, yuv2rgb); | 656 | ff_matrix_invert_3x3(rgb2yuv, yuv2rgb); |
| 729 | bits = 1 << (in_desc->comp[0].depth - 1); | 657 | bits = 1 << (in_desc->comp[0].depth - 1); |
| 730 | for (n = 0; n < 3; n++) { | 658 | for (n = 0; n < 3; n++) { |
| @@ -757,7 +685,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 26 unmodified lines |
| 757 | } | 685 | } |
| 758 | for (n = 0; n < 8; n++) | 686 | for (n = 0; n < 8; n++) |
| 759 | s->yuv_offset[1][n] = off; | 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 | bits = 1 << (29 - out_desc->comp[0].depth); | 689 | bits = 1 << (29 - out_desc->comp[0].depth); |
| 762 | for (out_rng = s->out_y_rng, n = 0; n < 3; n++, out_rng = s->out_uv_rng) { | 690 | for (out_rng = s->out_y_rng, n = 0; n < 3; n++, out_rng = s->out_uv_rng) { |
| 763 | for (m = 0; m < 3; m++) { | 691 | for (m = 0; m < 3; m++) { |