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
39constant const float ST2084_C2 = 18.8515625f;39constant const float ST2084_C2 = 18.8515625f;
40constant const float ST2084_C3 = 18.6875f;40constant const float ST2084_C3 = 18.6875f;
41 41
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
67float get_luma_dst(float3 c) {42float get_luma_dst(float3 c) {
68 return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;43 return luma_dst.x * c.x + luma_dst.y * c.y + luma_dst.z * c.z;
69}44}
libavfilter/vf_tonemap_opencl.c+29−35
@@ -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 solve35// - 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 support37// - more format support
40 38
41#define DETECTION_FRAMES 6339#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
76static const char *yuv_coff[AVCOL_SPC_NB] = {
77 [AVCOL_SPC_BT709] = "rgb2yuv_bt709",
78 [AVCOL_SPC_BT2020_NCL] = "rgb2yuv_bt2020",
79};
80
81static const char *rgb_coff[AVCOL_SPC_NB] = {
82 [AVCOL_SPC_BT709] = "yuv2rgb_bt709",
83 [AVCOL_SPC_BT2020_NCL] = "yuv2rgb_bt2020",
84};
85
86static const char *linearize_funcs[AVCOL_TRC_NB] = {74static 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
96static 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
101static const struct PrimaryCoefficients primaries_table[AVCOL_PRI_NB] = {84static 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
278fail:271fail:
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)

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