| @@ -18,7 +18,6 @@ · 17 unmodified lines |
| 18 | 18 | #include <float.h> |
| 19 | 19 | |
| 20 | 20 | #include "libavutil/avassert.h" |
| 21 | | −#include "libavutil/bprint.h" |
| 22 | 21 | #include "libavutil/common.h" |
| 23 | 22 | #include "libavutil/imgutils.h" |
| 24 | 23 | #include "libavutil/mem.h" |
| @@ -35,7 +34,6 @@ · 10 unmodified lines |
| 35 | 34 | // TODO: |
| 36 | 35 | // - separate peak-detection from tone-mapping kernel to solve |
| 37 | 36 | // one-frame-delay issue. |
| 38 | | −// - import colorspace matrix generation from vf_colorspace.c |
| 39 | 37 | // - more format support |
| 40 | 38 | |
| 41 | 39 | #define DETECTION_FRAMES 63 |
| @@ -73,16 +71,6 @@ typedef struct TonemapOpenCLContext { · 31 unmodified lines |
| 73 | 71 | cl_mem util_mem; |
| 74 | 72 | } TonemapOpenCLContext; |
| 75 | 73 | |
| 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 | | − |
| 86 | 74 | static const char *linearize_funcs[AVCOL_TRC_NB] = { |
| 87 | 75 | [AVCOL_TRC_SMPTE2084] = "eotf_st2084", |
| 88 | 76 | [AVCOL_TRC_ARIB_STD_B67] = "inverse_oetf_hlg", |
| @@ -93,11 +81,6 @@ static const char *delinearize_funcs[AVCOL_TRC_NB] = { · 4 unmodified lines |
| 93 | 81 | [AVCOL_TRC_BT2020_10] = "inverse_eotf_bt1886", |
| 94 | 82 | }; |
| 95 | 83 | |
| 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 | | − |
| 101 | 84 | static const struct PrimaryCoefficients primaries_table[AVCOL_PRI_NB] = { |
| 102 | 85 | [AVCOL_PRI_BT709] = { 0.640, 0.330, 0.300, 0.600, 0.150, 0.060 }, |
| 103 | 86 | [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 |
| 137 | 120 | { |
| 138 | 121 | TonemapOpenCLContext *ctx = avctx->priv; |
| 139 | 122 | 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; |
| 142 | 125 | cl_int cle; |
| 143 | 126 | int err; |
| 144 | 127 | AVBPrint header; |
| @@ -215,27 +198,37 @@ static int tonemap_opencl_init(AVFilterContext *avctx) · 70 unmodified lines |
| 215 | 198 | |
| 216 | 199 | if (rgb2rgb_passthrough) |
| 217 | 200 | 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; |
| 226 | 211 | } |
| 227 | 212 | |
| 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); |
| 232 | 227 | |
| 233 | | − luma_src = luma_coefficients[ctx->colorspace_in]; |
| 234 | | − luma_dst = luma_coefficients[ctx->colorspace_out]; |
| 235 | 228 | 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); |
| 237 | 230 | 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); |
| 239 | 232 | |
| 240 | 233 | av_bprintf(&header, "#define linearize %s\n", linearize_funcs[ctx->trc_in]); |
| 241 | 234 | av_bprintf(&header, "#define delinearize %s\n", |
| @@ -276,6 +269,7 @@ static int tonemap_opencl_init(AVFilterContext *avctx) · 34 unmodified lines |
| 276 | 269 | return 0; |
| 277 | 270 | |
| 278 | 271 | fail: |
| 272 | + av_bprint_finalize(&header, NULL); |
| 279 | 273 | if (ctx->util_mem) |
| 280 | 274 | clReleaseMemObject(ctx->util_mem); |
| 281 | 275 | if (ctx->command_queue) |