History

lavfi/colorspace: move some functions to common file

These functions can be reused by other colorspace filters, so move them to common file. No functional changes.

Ruiling Song committed 7 years ago

Commit d0f37981 parent 61cb5053 files · +79 −76Browse files

Unified · Split

3 files
  1. libavfilter/colorspace.c +71 −0
  2. libavfilter/colorspace.h +4 −0
  3. libavfilter/vf_colorspace.c +4 −76
libavfilter/colorspace.c+71−0
@@ -93,6 +93,77 @@ void ff_fill_rgb2xyz_table(const struct PrimaryCoefficients *coeffs, · 92 unmodified lines
9393 rgb2xyz[2][1] *= sg;
9494 rgb2xyz[2][2] *= sb;
9595 }
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+}
96167
97168 double ff_determine_signal_peak(AVFrame *in)
98169 {
libavfilter/colorspace.h+4−0
@@ -44,6 +44,10 @@ void ff_fill_rgb2xyz_table(const struct PrimaryCoefficients *coeffs, · 43 unmodified lines
4444 const struct WhitepointCoefficients *wp,
4545 double rgb2xyz[3][3]);
4646
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+
4751 double ff_determine_signal_peak(AVFrame *in);
4852 void ff_update_hdr_metadata(AVFrame *in, double peak);
4953
libavfilter/vf_colorspace.c+4−76
@@ -170,78 +170,6 @@ typedef struct ColorSpaceContext { · 169 unmodified lines
170170 // FIXME dithering if bitdepth goes down?
171171 // FIXME bitexact for fate integration?
172172
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−
245173 // FIXME I'm pretty sure gamma22/28 also have a linear toe slope, but I can't
246174 // find any actual tables that document their real values...
247175 // 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
669597 s->in_rng = in->color_range;
670598 if (s->user_irng != AVCOL_RANGE_UNSPECIFIED)
671599 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);
673601 if (!s->in_lumacoef) {
674602 av_log(ctx, AV_LOG_ERROR,
675603 "Unsupported input colorspace %d (%s)\n",
@@ -682,7 +610,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 6 unmodified lines
682610 if (!s->out_lumacoef) {
683611 s->out_csp = out->colorspace;
684612 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);
686614 if (!s->out_lumacoef) {
687615 if (s->out_csp == AVCOL_SPC_UNSPECIFIED) {
688616 if (s->user_all == CS_UNSPECIFIED) {
@@ -724,7 +652,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 35 unmodified lines
724652 }
725653 for (n = 0; n < 8; n++)
726654 s->yuv_offset[0][n] = off;
727− fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv);
655+ ff_fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv);
728656 ff_matrix_invert_3x3(rgb2yuv, yuv2rgb);
729657 bits = 1 << (in_desc->comp[0].depth - 1);
730658 for (n = 0; n < 3; n++) {
@@ -757,7 +685,7 @@ static int create_filtergraph(AVFilterContext *ctx, · 26 unmodified lines
757685 }
758686 for (n = 0; n < 8; n++)
759687 s->yuv_offset[1][n] = off;
760− fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv);
688+ ff_fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv);
761689 bits = 1 << (29 - out_desc->comp[0].depth);
762690 for (out_rng = s->out_y_rng, n = 0; n < 3; n++, out_rng = s->out_uv_rng) {
763691 for (m = 0; m < 3; m++) {

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