History

lavfi/tonemap_opencl: reuse color matrix calculation from colorspace.c

Ruiling Song committed 7 years ago

Commit 8b951cd1 parent 25931222 files · +29 −60Browse files

Unified · Split

2 files
  1. libavfilter/opencl/colorspace_common.cl +0 −25
  2. libavfilter/vf_tonemap_opencl.c +29 −35
libavfilter/opencl/colorspace_common.cl+0−25
@@ -39,31 +39,6 @@ constant const float ST2084_C1 = 0.8359375f; · 38 unmodified lines
3939 constant const float ST2084_C2 = 18.8515625f;
4040 constant const float ST2084_C3 = 18.6875f;
4141
42−__constant float yuv2rgb_bt2020[] = {
43− 1.0f, 0.0f, 1.4746f,
44− 1.0f, -0.16455f, -0.57135f,
45− 1.0f, 1.8814f, 0.0f
46−};
47−
48−__constant float yuv2rgb_bt709[] = {
49− 1.0f, 0.0f, 1.5748f,
50− 1.0f, -0.18732f, -0.46812f,
51− 1.0f, 1.8556f, 0.0f
52−};
53−
54−__constant float rgb2yuv_bt709[] = {
55− 0.2126f, 0.7152f, 0.0722f,
56− -0.11457f, -0.38543f, 0.5f,
57− 0.5f, -0.45415f, -0.04585f
58−};
59−
60−__constant float rgb2yuv_bt2020[] ={
61− 0.2627f, 0.678f, 0.0593f,
62− -0.1396f, -0.36037f, 0.5f,
63− 0.5f, -0.4598f, -0.0402f,
64−};
65−
66−
6742 float get_luma_dst(float3 c) {
6843 return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;
6944 }
libavfilter/vf_tonemap_opencl.c+29−35
@@ -18,7 +18,6 @@ · 17 unmodified lines
1818 #include <float.h>
1919
2020 #include "libavutil/avassert.h"
21−#include "libavutil/bprint.h"
2221 #include "libavutil/common.h"
2322 #include "libavutil/imgutils.h"
2423 #include "libavutil/mem.h"
@@ -35,7 +34,6 @@ · 10 unmodified lines
3534 // TODO:
3635 // - separate peak-detection from tone-mapping kernel to solve
3736 // one-frame-delay issue.
38−// - import colorspace matrix generation from vf_colorspace.c
3937 // - more format support
4038
4139 #define DETECTION_FRAMES 63
@@ -73,16 +71,6 @@ typedef struct TonemapOpenCLContext { · 31 unmodified lines
7371 cl_mem util_mem;
7472 } TonemapOpenCLContext;
7573
76−static const char *yuv_coff[AVCOL_SPC_NB] = {
77− [AVCOL_SPC_BT709] = "rgb2yuv_bt709",
78− [AVCOL_SPC_BT2020_NCL] = "rgb2yuv_bt2020",
79−};
80−
81−static const char *rgb_coff[AVCOL_SPC_NB] = {
82− [AVCOL_SPC_BT709] = "yuv2rgb_bt709",
83− [AVCOL_SPC_BT2020_NCL] = "yuv2rgb_bt2020",
84−};
85−
8674 static const char *linearize_funcs[AVCOL_TRC_NB] = {
8775 [AVCOL_TRC_SMPTE2084] = "eotf_st2084",
8876 [AVCOL_TRC_ARIB_STD_B67] = "inverse_oetf_hlg",
@@ -93,11 +81,6 @@ static const char *delinearize_funcs[AVCOL_TRC_NB] = { · 4 unmodified lines
9381 [AVCOL_TRC_BT2020_10] = "inverse_eotf_bt1886",
9482 };
9583
96−static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = {
97− [AVCOL_SPC_BT709] = { 0.2126, 0.7152, 0.0722 },
98− [AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 },
99−};
100−
10184 static const struct PrimaryCoefficients primaries_table[AVCOL_PRI_NB] = {
10285 [AVCOL_PRI_BT709] = { 0.640, 0.330, 0.300, 0.600, 0.150, 0.060 },
10386 [AVCOL_PRI_BT2020] = { 0.708, 0.292, 0.170, 0.797, 0.131, 0.046 },
@@ -137,8 +120,8 @@ static int tonemap_opencl_init(AVFilterContext *avctx) · 33 unmodified lines
137120 {
138121 TonemapOpenCLContext *ctx = avctx->priv;
139122 int rgb2rgb_passthrough = 1;
140− double rgb2rgb[3][3];
141− struct LumaCoefficients luma_src, luma_dst;
123+ double rgb2rgb[3][3], rgb2yuv[3][3], yuv2rgb[3][3];
124+ const struct LumaCoefficients *luma_src, *luma_dst;
142125 cl_int cle;
143126 int err;
144127 AVBPrint header;
@@ -215,27 +198,37 @@ static int tonemap_opencl_init(AVFilterContext *avctx) · 70 unmodified lines
215198
216199 if (rgb2rgb_passthrough)
217200 av_bprintf(&header, "#define RGB2RGB_PASSTHROUGH\n");
218− else {
219− av_bprintf(&header, "__constant float rgb2rgb[9] = {\n");
220− av_bprintf(&header, " %.4ff, %.4ff, %.4ff,\n",
221− rgb2rgb[0][0], rgb2rgb[0][1], rgb2rgb[0][2]);
222− av_bprintf(&header, " %.4ff, %.4ff, %.4ff,\n",
223− rgb2rgb[1][0], rgb2rgb[1][1], rgb2rgb[1][2]);
224− av_bprintf(&header, " %.4ff, %.4ff, %.4ff};\n",
225− rgb2rgb[2][0], rgb2rgb[2][1], rgb2rgb[2][2]);
201+ else
202+ ff_opencl_print_const_matrix_3x3(&header, "rgb2rgb", rgb2rgb);
203+
204+
205+ luma_src = ff_get_luma_coefficients(ctx->colorspace_in);
206+ if (!luma_src) {
207+ err = AVERROR(EINVAL);
208+ av_log(avctx, AV_LOG_ERROR, "unsupported input colorspace %d (%s)\n",
209+ ctx->colorspace_in, av_color_space_name(ctx->colorspace_in));
210+ goto fail;
226211 }
227212
228− av_bprintf(&header, "#define rgb_matrix %s\n",
229− rgb_coff[ctx->colorspace_in]);
230− av_bprintf(&header, "#define yuv_matrix %s\n",
231− yuv_coff[ctx->colorspace_out]);
213+ luma_dst = ff_get_luma_coefficients(ctx->colorspace_out);
214+ if (!luma_dst) {
215+ err = AVERROR(EINVAL);
216+ av_log(avctx, AV_LOG_ERROR, "unsupported output colorspace %d (%s)\n",
217+ ctx->colorspace_out, av_color_space_name(ctx->colorspace_out));
218+ goto fail;
219+ }
220+
221+ ff_fill_rgb2yuv_table(luma_dst, rgb2yuv);
222+ ff_opencl_print_const_matrix_3x3(&header, "yuv_matrix", rgb2yuv);
223+
224+ ff_fill_rgb2yuv_table(luma_src, rgb2yuv);
225+ ff_matrix_invert_3x3(rgb2yuv, yuv2rgb);
226+ ff_opencl_print_const_matrix_3x3(&header, "rgb_matrix", yuv2rgb);
232227
233− luma_src = luma_coefficients[ctx->colorspace_in];
234− luma_dst = luma_coefficients[ctx->colorspace_out];
235228 av_bprintf(&header, "constant float3 luma_src = {%.4ff, %.4ff, %.4ff};\n",
236− luma_src.cr, luma_src.cg, luma_src.cb);
229+ luma_src->cr, luma_src->cg, luma_src->cb);
237230 av_bprintf(&header, "constant float3 luma_dst = {%.4ff, %.4ff, %.4ff};\n",
238− luma_dst.cr, luma_dst.cg, luma_dst.cb);
231+ luma_dst->cr, luma_dst->cg, luma_dst->cb);
239232
240233 av_bprintf(&header, "#define linearize %s\n", linearize_funcs[ctx->trc_in]);
241234 av_bprintf(&header, "#define delinearize %s\n",
@@ -276,6 +269,7 @@ static int tonemap_opencl_init(AVFilterContext *avctx) · 34 unmodified lines
276269 return 0;
277270
278271 fail:
272+ av_bprint_finalize(&header, NULL);
279273 if (ctx->util_mem)
280274 clReleaseMemObject(ctx->util_mem);
281275 if (ctx->command_queue)

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