| @@ -18,7 +18,6 @@ · 17 unmodified lines |
| 18 | #include <float.h> | 18 | #include <float.h> |
| 19 | | 19 | |
| 20 | #include "libavutil/avassert.h" | 20 | #include "libavutil/avassert.h" |
| 21 | #include "libavutil/bprint.h" | | |
| 22 | #include "libavutil/common.h" | 21 | #include "libavutil/common.h" |
| 23 | #include "libavutil/imgutils.h" | 22 | #include "libavutil/imgutils.h" |
| 24 | #include "libavutil/mem.h" | 23 | #include "libavutil/mem.h" |
| @@ -35,7 +34,6 @@ · 10 unmodified lines |
| 35 | // TODO: | 34 | // TODO: |
| 36 | // - separate peak-detection from tone-mapping kernel to solve | 35 | // - separate peak-detection from tone-mapping kernel to solve |
| 37 | // one-frame-delay issue. | 36 | // one-frame-delay issue. |
| 38 | // - import colorspace matrix generation from vf_colorspace.c | | |
| 39 | // - more format support | 37 | // - more format support |
| 40 | | 38 | |
| 41 | #define DETECTION_FRAMES 63 | 39 | #define DETECTION_FRAMES 63 |
| @@ -73,16 +71,6 @@ typedef struct TonemapOpenCLContext { · 31 unmodified lines |
| 73 | cl_mem util_mem; | 71 | cl_mem util_mem; |
| 74 | } TonemapOpenCLContext; | 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 | static const char *linearize_funcs[AVCOL_TRC_NB] = { | 74 | static const char *linearize_funcs[AVCOL_TRC_NB] = { |
| 87 | [AVCOL_TRC_SMPTE2084] = "eotf_st2084", | 75 | [AVCOL_TRC_SMPTE2084] = "eotf_st2084", |
| 88 | [AVCOL_TRC_ARIB_STD_B67] = "inverse_oetf_hlg", | 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 | [AVCOL_TRC_BT2020_10] = "inverse_eotf_bt1886", | 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 | static const struct PrimaryCoefficients primaries_table[AVCOL_PRI_NB] = { | 84 | static const struct PrimaryCoefficients primaries_table[AVCOL_PRI_NB] = { |
| 102 | [AVCOL_PRI_BT709] = { 0.640, 0.330, 0.300, 0.600, 0.150, 0.060 }, | 85 | [AVCOL_PRI_BT709] = { 0.640, 0.330, 0.300, 0.600, 0.150, 0.060 }, |
| 103 | [AVCOL_PRI_BT2020] = { 0.708, 0.292, 0.170, 0.797, 0.131, 0.046 }, | 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 | TonemapOpenCLContext *ctx = avctx->priv; | 121 | TonemapOpenCLContext *ctx = avctx->priv; |
| 139 | int rgb2rgb_passthrough = 1; | 122 | int rgb2rgb_passthrough = 1; |
| 140 | double rgb2rgb[3][3]; | 123 | double rgb2rgb[3][3], rgb2yuv[3][3], yuv2rgb[3][3]; |
| 141 | struct LumaCoefficients luma_src, luma_dst; | 124 | const struct LumaCoefficients *luma_src, *luma_dst; |
| 142 | cl_int cle; | 125 | cl_int cle; |
| 143 | int err; | 126 | int err; |
| 144 | AVBPrint header; | 127 | AVBPrint header; |
| @@ -215,27 +198,37 @@ static int tonemap_opencl_init(AVFilterContext *avctx) · 70 unmodified lines |
| 215 | | 198 | |
| 216 | if (rgb2rgb_passthrough) | 199 | if (rgb2rgb_passthrough) |
| 217 | av_bprintf(&header, "#define RGB2RGB_PASSTHROUGH\n"); | 200 | av_bprintf(&header, "#define RGB2RGB_PASSTHROUGH\n"); |
| 218 | else { | 201 | else |
| 219 | av_bprintf(&header, "__constant float rgb2rgb[9] = {\n"); | 202 | ff_opencl_print_const_matrix_3x3(&header, "rgb2rgb", rgb2rgb); |
| 220 | av_bprintf(&header, " %.4ff, %.4ff, %.4ff,\n", | 203 | |
| 221 | rgb2rgb[0][0], rgb2rgb[0][1], rgb2rgb[0][2]); | 204 | |
| 222 | av_bprintf(&header, " %.4ff, %.4ff, %.4ff,\n", | 205 | luma_src = ff_get_luma_coefficients(ctx->colorspace_in); |
| 223 | rgb2rgb[1][0], rgb2rgb[1][1], rgb2rgb[1][2]); | 206 | if (!luma_src) { |
| 224 | av_bprintf(&header, " %.4ff, %.4ff, %.4ff};\n", | 207 | err = AVERROR(EINVAL); |
| 225 | rgb2rgb[2][0], rgb2rgb[2][1], rgb2rgb[2][2]); | 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", | 213 | luma_dst = ff_get_luma_coefficients(ctx->colorspace_out); |
| 229 | rgb_coff[ctx->colorspace_in]); | 214 | if (!luma_dst) { |
| 230 | av_bprintf(&header, "#define yuv_matrix %s\n", | 215 | err = AVERROR(EINVAL); |
| 231 | yuv_coff[ctx->colorspace_out]); | 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 | av_bprintf(&header, "constant float3 luma_src = {%.4ff, %.4ff, %.4ff};\n", | 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 | av_bprintf(&header, "constant float3 luma_dst = {%.4ff, %.4ff, %.4ff};\n", | 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 | av_bprintf(&header, "#define linearize %s\n", linearize_funcs[ctx->trc_in]); | 233 | av_bprintf(&header, "#define linearize %s\n", linearize_funcs[ctx->trc_in]); |
| 241 | av_bprintf(&header, "#define delinearize %s\n", | 234 | av_bprintf(&header, "#define delinearize %s\n", |
| @@ -276,6 +269,7 @@ static int tonemap_opencl_init(AVFilterContext *avctx) · 34 unmodified lines |
| 276 | return 0; | 269 | return 0; |
| 277 | | 270 | |
| 278 | fail: | 271 | fail: |
| | 272 | av_bprint_finalize(&header, NULL); |
| 279 | if (ctx->util_mem) | 273 | if (ctx->util_mem) |
| 280 | clReleaseMemObject(ctx->util_mem); | 274 | clReleaseMemObject(ctx->util_mem); |
| 281 | if (ctx->command_queue) | 275 | if (ctx->command_queue) |