diff --git a/libavfilter/opencl/colorspace_common.cl b/libavfilter/opencl/colorspace_common.cl
index 94a4dd0..1d68a54 100644
--- a/libavfilter/opencl/colorspace_common.cl
+++ b/libavfilter/opencl/colorspace_common.cl
@@ -39,31 +39,6 @@ constant const float ST2084_C1 = 0.8359375f;
 constant const float ST2084_C2 = 18.8515625f;
 constant const float ST2084_C3 = 18.6875f;
 
-__constant float yuv2rgb_bt2020[] = {
-    1.0f, 0.0f, 1.4746f,
-    1.0f, -0.16455f, -0.57135f,
-    1.0f, 1.8814f, 0.0f
-};
-
-__constant float yuv2rgb_bt709[] = {
-    1.0f, 0.0f, 1.5748f,
-    1.0f, -0.18732f, -0.46812f,
-    1.0f, 1.8556f, 0.0f
-};
-
-__constant float rgb2yuv_bt709[] = {
-    0.2126f, 0.7152f, 0.0722f,
-    -0.11457f, -0.38543f, 0.5f,
-    0.5f, -0.45415f, -0.04585f
-};
-
-__constant float rgb2yuv_bt2020[] ={
-    0.2627f, 0.678f, 0.0593f,
-    -0.1396f, -0.36037f, 0.5f,
-    0.5f, -0.4598f, -0.0402f,
-};
-
-
 float get_luma_dst(float3 c) {
     return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;
 }
diff --git a/libavfilter/vf_tonemap_opencl.c b/libavfilter/vf_tonemap_opencl.c
index ae3f98d..315ead4 100644
--- a/libavfilter/vf_tonemap_opencl.c
+++ b/libavfilter/vf_tonemap_opencl.c
@@ -18,7 +18,6 @@
 #include <float.h>
 
 #include "libavutil/avassert.h"
-#include "libavutil/bprint.h"
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/mem.h"
@@ -35,7 +34,6 @@
 // TODO:
 // - separate peak-detection from tone-mapping kernel to solve
 //    one-frame-delay issue.
-// - import colorspace matrix generation from vf_colorspace.c
 // - more format support
 
 #define DETECTION_FRAMES 63
@@ -73,16 +71,6 @@ typedef struct TonemapOpenCLContext {
     cl_mem                util_mem;
 } TonemapOpenCLContext;
 
-static const char *yuv_coff[AVCOL_SPC_NB] = {
-    [AVCOL_SPC_BT709] = "rgb2yuv_bt709",
-    [AVCOL_SPC_BT2020_NCL] = "rgb2yuv_bt2020",
-};
-
-static const char *rgb_coff[AVCOL_SPC_NB] = {
-    [AVCOL_SPC_BT709] = "yuv2rgb_bt709",
-    [AVCOL_SPC_BT2020_NCL] = "yuv2rgb_bt2020",
-};
-
 static const char *linearize_funcs[AVCOL_TRC_NB] = {
     [AVCOL_TRC_SMPTE2084] = "eotf_st2084",
     [AVCOL_TRC_ARIB_STD_B67] = "inverse_oetf_hlg",
@@ -93,11 +81,6 @@ static const char *delinearize_funcs[AVCOL_TRC_NB] = {
     [AVCOL_TRC_BT2020_10] = "inverse_eotf_bt1886",
 };
 
-static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = {
-    [AVCOL_SPC_BT709]      = { 0.2126, 0.7152, 0.0722 },
-    [AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 },
-};
-
 static const struct PrimaryCoefficients primaries_table[AVCOL_PRI_NB] = {
     [AVCOL_PRI_BT709]  = { 0.640, 0.330, 0.300, 0.600, 0.150, 0.060 },
     [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)
 {
     TonemapOpenCLContext *ctx = avctx->priv;
     int rgb2rgb_passthrough = 1;
-    double rgb2rgb[3][3];
-    struct LumaCoefficients luma_src, luma_dst;
+    double rgb2rgb[3][3], rgb2yuv[3][3], yuv2rgb[3][3];
+    const struct LumaCoefficients *luma_src, *luma_dst;
     cl_int cle;
     int err;
     AVBPrint header;
@@ -215,27 +198,37 @@ static int tonemap_opencl_init(AVFilterContext *avctx)
 
     if (rgb2rgb_passthrough)
         av_bprintf(&header, "#define RGB2RGB_PASSTHROUGH\n");
-    else {
-        av_bprintf(&header, "__constant float rgb2rgb[9] = {\n");
-        av_bprintf(&header, "    %.4ff, %.4ff, %.4ff,\n",
-                   rgb2rgb[0][0], rgb2rgb[0][1], rgb2rgb[0][2]);
-        av_bprintf(&header, "    %.4ff, %.4ff, %.4ff,\n",
-                   rgb2rgb[1][0], rgb2rgb[1][1], rgb2rgb[1][2]);
-        av_bprintf(&header, "    %.4ff, %.4ff, %.4ff};\n",
-                   rgb2rgb[2][0], rgb2rgb[2][1], rgb2rgb[2][2]);
+    else
+        ff_opencl_print_const_matrix_3x3(&header, "rgb2rgb", rgb2rgb);
+
+
+    luma_src = ff_get_luma_coefficients(ctx->colorspace_in);
+    if (!luma_src) {
+        err = AVERROR(EINVAL);
+        av_log(avctx, AV_LOG_ERROR, "unsupported input colorspace %d (%s)\n",
+               ctx->colorspace_in, av_color_space_name(ctx->colorspace_in));
+        goto fail;
     }
 
-    av_bprintf(&header, "#define rgb_matrix %s\n",
-               rgb_coff[ctx->colorspace_in]);
-    av_bprintf(&header, "#define yuv_matrix %s\n",
-               yuv_coff[ctx->colorspace_out]);
+    luma_dst = ff_get_luma_coefficients(ctx->colorspace_out);
+    if (!luma_dst) {
+        err = AVERROR(EINVAL);
+        av_log(avctx, AV_LOG_ERROR, "unsupported output colorspace %d (%s)\n",
+               ctx->colorspace_out, av_color_space_name(ctx->colorspace_out));
+        goto fail;
+    }
+
+    ff_fill_rgb2yuv_table(luma_dst, rgb2yuv);
+    ff_opencl_print_const_matrix_3x3(&header, "yuv_matrix", rgb2yuv);
+
+    ff_fill_rgb2yuv_table(luma_src, rgb2yuv);
+    ff_matrix_invert_3x3(rgb2yuv, yuv2rgb);
+    ff_opencl_print_const_matrix_3x3(&header, "rgb_matrix", yuv2rgb);
 
-    luma_src = luma_coefficients[ctx->colorspace_in];
-    luma_dst = luma_coefficients[ctx->colorspace_out];
     av_bprintf(&header, "constant float3 luma_src = {%.4ff, %.4ff, %.4ff};\n",
-               luma_src.cr, luma_src.cg, luma_src.cb);
+               luma_src->cr, luma_src->cg, luma_src->cb);
     av_bprintf(&header, "constant float3 luma_dst = {%.4ff, %.4ff, %.4ff};\n",
-               luma_dst.cr, luma_dst.cg, luma_dst.cb);
+               luma_dst->cr, luma_dst->cg, luma_dst->cb);
 
     av_bprintf(&header, "#define linearize %s\n", linearize_funcs[ctx->trc_in]);
     av_bprintf(&header, "#define delinearize %s\n",
@@ -276,6 +269,7 @@ static int tonemap_opencl_init(AVFilterContext *avctx)
     return 0;
 
 fail:
+    av_bprint_finalize(&header, NULL);
     if (ctx->util_mem)
         clReleaseMemObject(ctx->util_mem);
     if (ctx->command_queue)
