FFmpeg
Loading...
Searching...
No Matches
vf_tonemap_opencl.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#include <float.h>
19
20#include "libavutil/avassert.h"
21#include "libavutil/common.h"
22#include "libavutil/imgutils.h"
23#include "libavutil/opt.h"
24#include "libavutil/pixdesc.h"
25
26#include "avfilter.h"
27#include "filters.h"
28#include "opencl.h"
29#include "opencl_source.h"
30#include "video.h"
31#include "colorspace.h"
32
33// TODO:
34// - separate peak-detection from tone-mapping kernel to solve
35// one-frame-delay issue.
36// - more format support
37
38#define DETECTION_FRAMES 63
39
50
51typedef struct TonemapOpenCLContext {
53
54 enum AVColorSpace colorspace, colorspace_in, colorspace_out;
57 enum AVColorRange range, range_in, range_out;
59
60 /* enum TonemapAlgorithm */
63 double peak;
64 double param;
69 cl_kernel kernel;
70 cl_command_queue command_queue;
71 cl_mem util_mem;
73
74static const char *const linearize_funcs[] = {
75 [AVCOL_TRC_SMPTE2084] = "eotf_st2084",
76 [AVCOL_TRC_ARIB_STD_B67] = "inverse_oetf_hlg",
77};
78
79static const char *const delinearize_funcs[] = {
80 [AVCOL_TRC_BT709] = "inverse_eotf_bt1886",
81 [AVCOL_TRC_BT2020_10] = "inverse_eotf_bt1886",
82};
83
84static const char *const tonemap_func[TONEMAP_MAX] = {
85 [TONEMAP_NONE] = "direct",
86 [TONEMAP_LINEAR] = "linear",
87 [TONEMAP_GAMMA] = "gamma",
88 [TONEMAP_CLIP] = "clip",
89 [TONEMAP_REINHARD] = "reinhard",
90 [TONEMAP_HABLE] = "hable",
91 [TONEMAP_MOBIUS] = "mobius",
92};
93
95 double rgb2rgb[3][3]) {
96 double rgb2xyz[3][3], xyz2rgb[3][3];
97
100
101 if (!in_primaries || !out_primaries)
102 return AVERROR(EINVAL);
103
104 ff_fill_rgb2xyz_table(&out_primaries->prim, &out_primaries->wp, rgb2xyz);
106 ff_fill_rgb2xyz_table(&in_primaries->prim, &in_primaries->wp, rgb2xyz);
107 ff_matrix_mul_3x3(rgb2rgb, rgb2xyz, xyz2rgb);
108
109 return 0;
110}
111
112#define OPENCL_SOURCE_NB 3
113// Average light level for SDR signals. This is equal to a signal level of 0.5
114// under a typical presentation gamma of about 2.0.
115static const float sdr_avg = 0.25f;
116
118{
119 unsigned util_init[2 * DETECTION_FRAMES + 7] = { 0 };
120 TonemapOpenCLContext *ctx = avctx->priv;
121 int rgb2rgb_passthrough = 1;
122 double rgb2rgb[3][3], rgb2yuv[3][3], yuv2rgb[3][3];
123 const AVLumaCoefficients *luma_src, *luma_dst;
124 cl_int cle;
125 int err;
126 AVBPrint header;
127 const char *opencl_sources[OPENCL_SOURCE_NB];
128
130
131 switch(ctx->tonemap) {
132 case TONEMAP_GAMMA:
133 if (isnan(ctx->param))
134 ctx->param = 1.8f;
135 break;
136 case TONEMAP_REINHARD:
137 if (!isnan(ctx->param))
138 ctx->param = (1.0f - ctx->param) / ctx->param;
139 break;
140 case TONEMAP_MOBIUS:
141 if (isnan(ctx->param))
142 ctx->param = 0.3f;
143 break;
144 }
145
146 if (isnan(ctx->param))
147 ctx->param = 1.0f;
148
149 // SDR peak is 1.0f
150 ctx->target_peak = 1.0f;
151 av_log(ctx, AV_LOG_DEBUG, "tone mapping transfer from %s to %s\n",
153 av_color_transfer_name(ctx->trc_out));
154 av_log(ctx, AV_LOG_DEBUG, "mapping colorspace from %s to %s\n",
155 av_color_space_name(ctx->colorspace_in),
156 av_color_space_name(ctx->colorspace_out));
157 av_log(ctx, AV_LOG_DEBUG, "mapping primaries from %s to %s\n",
158 av_color_primaries_name(ctx->primaries_in),
159 av_color_primaries_name(ctx->primaries_out));
160 av_log(ctx, AV_LOG_DEBUG, "mapping range from %s to %s\n",
161 av_color_range_name(ctx->range_in),
162 av_color_range_name(ctx->range_out));
163 // checking valid value just because of limited implementation
164 // please remove when more functionalities are implemented
165 av_assert0(ctx->trc_out == AVCOL_TRC_BT709 ||
166 ctx->trc_out == AVCOL_TRC_BT2020_10);
168 ctx->trc_in == AVCOL_TRC_ARIB_STD_B67);
169 av_assert0(ctx->colorspace_in == AVCOL_SPC_BT2020_NCL ||
170 ctx->colorspace_in == AVCOL_SPC_BT709);
171 av_assert0(ctx->primaries_in == AVCOL_PRI_BT2020 ||
172 ctx->primaries_in == AVCOL_PRI_BT709);
173
174 av_bprintf(&header, "__constant const float tone_param = %.4ff;\n",
175 ctx->param);
176 av_bprintf(&header, "__constant const float desat_param = %.4ff;\n",
177 ctx->desat_param);
178 av_bprintf(&header, "__constant const float target_peak = %.4ff;\n",
179 ctx->target_peak);
180 av_bprintf(&header, "__constant const float sdr_avg = %.4ff;\n", sdr_avg);
181 av_bprintf(&header, "__constant const float scene_threshold = %.4ff;\n",
182 ctx->scene_threshold);
183 av_bprintf(&header, "#define TONE_FUNC %s\n", tonemap_func[ctx->tonemap]);
184 av_bprintf(&header, "#define DETECTION_FRAMES %d\n", DETECTION_FRAMES);
185
186 if (ctx->primaries_out != ctx->primaries_in) {
187 if ((err = get_rgb2rgb_matrix(ctx->primaries_in, ctx->primaries_out, rgb2rgb)) < 0)
188 goto fail;
189 rgb2rgb_passthrough = 0;
190 }
191 if (ctx->range_in == AVCOL_RANGE_JPEG)
192 av_bprintf(&header, "#define FULL_RANGE_IN\n");
193
194 if (ctx->range_out == AVCOL_RANGE_JPEG)
195 av_bprintf(&header, "#define FULL_RANGE_OUT\n");
196
197 av_bprintf(&header, "#define chroma_loc %d\n", (int)ctx->chroma_loc);
198
199 if (rgb2rgb_passthrough)
200 av_bprintf(&header, "#define RGB2RGB_PASSTHROUGH\n");
201 else
202 ff_opencl_print_const_matrix_3x3(&header, "rgb2rgb", rgb2rgb);
203
204
205 luma_src = av_csp_luma_coeffs_from_avcsp(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;
211 }
212
213 luma_dst = av_csp_luma_coeffs_from_avcsp(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
223
227
228 av_bprintf(&header, "constant float3 luma_src = {%.4ff, %.4ff, %.4ff};\n",
229 av_q2d(luma_src->cr), av_q2d(luma_src->cg), av_q2d(luma_src->cb));
230 av_bprintf(&header, "constant float3 luma_dst = {%.4ff, %.4ff, %.4ff};\n",
231 av_q2d(luma_dst->cr), av_q2d(luma_dst->cg), av_q2d(luma_dst->cb));
232
233 av_bprintf(&header, "#define linearize %s\n", linearize_funcs[ctx->trc_in]);
234 av_bprintf(&header, "#define delinearize %s\n",
235 delinearize_funcs[ctx->trc_out]);
236
237 if (ctx->trc_in == AVCOL_TRC_ARIB_STD_B67)
238 av_bprintf(&header, "#define ootf_impl ootf_hlg\n");
239
240 if (ctx->trc_out == AVCOL_TRC_ARIB_STD_B67)
241 av_bprintf(&header, "#define inverse_ootf_impl inverse_ootf_hlg\n");
242
243 av_log(avctx, AV_LOG_DEBUG, "Generated OpenCL header:\n%s\n", header.str);
244 opencl_sources[0] = header.str;
245 opencl_sources[1] = ff_source_tonemap_cl;
246 opencl_sources[2] = ff_source_colorspace_common_cl;
247 err = ff_opencl_filter_load_program(avctx, opencl_sources, OPENCL_SOURCE_NB);
248
250 if (err < 0)
251 goto fail;
252
253 ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
254 ctx->ocf.hwctx->device_id,
255 0, &cle);
256 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
257 "command queue %d.\n", cle);
258
259 ctx->kernel = clCreateKernel(ctx->ocf.program, "tonemap", &cle);
260 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create kernel %d.\n", cle);
261
262 // The kernel keeps its peak detection state here and expects it to
263 // start at zero, but a buffer created without a host pointer has
264 // undefined contents.
265 ctx->util_mem =
266 clCreateBuffer(ctx->ocf.hwctx->context, CL_MEM_COPY_HOST_PTR,
267 sizeof(util_init), util_init, &cle);
268 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create util buffer: %d.\n", cle);
269
270 ctx->initialised = 1;
271 return 0;
272
273fail:
275 if (ctx->util_mem)
276 clReleaseMemObject(ctx->util_mem);
277 if (ctx->command_queue)
278 clReleaseCommandQueue(ctx->command_queue);
279 if (ctx->kernel)
280 clReleaseKernel(ctx->kernel);
281 return err;
282}
283
285{
286 AVFilterContext *avctx = outlink->src;
287 TonemapOpenCLContext *s = avctx->priv;
288 int ret;
289 if (s->format == AV_PIX_FMT_NONE)
290 av_log(avctx, AV_LOG_WARNING, "format not set, use default format NV12\n");
291 else {
292 if (s->format != AV_PIX_FMT_P010 &&
293 s->format != AV_PIX_FMT_NV12) {
294 av_log(avctx, AV_LOG_ERROR, "unsupported output format,"
295 "only p010/nv12 supported now\n");
296 return AVERROR(EINVAL);
297 }
298 }
299
300 s->ocf.output_format = s->format == AV_PIX_FMT_NONE ? AV_PIX_FMT_NV12 : s->format;
302 if (ret < 0)
303 return ret;
304
305 return 0;
306}
307
308static int launch_kernel(AVFilterContext *avctx, cl_kernel kernel,
309 AVFrame *output, AVFrame *input, float peak) {
310 TonemapOpenCLContext *ctx = avctx->priv;
311 int err = AVERROR(ENOSYS);
312 size_t global_work[2];
313 size_t local_work[2];
314 cl_int cle;
315
316 CL_SET_KERNEL_ARG(kernel, 0, cl_mem, &output->data[0]);
317 CL_SET_KERNEL_ARG(kernel, 1, cl_mem, &input->data[0]);
318 CL_SET_KERNEL_ARG(kernel, 2, cl_mem, &output->data[1]);
319 CL_SET_KERNEL_ARG(kernel, 3, cl_mem, &input->data[1]);
320 CL_SET_KERNEL_ARG(kernel, 4, cl_mem, &ctx->util_mem);
321 CL_SET_KERNEL_ARG(kernel, 5, cl_float, &peak);
322
323 local_work[0] = 16;
324 local_work[1] = 16;
325 // Note the work size based on uv plane, as we process a 2x2 quad in one workitem
326 err = ff_opencl_filter_work_size_from_image(avctx, global_work, output,
327 1, 16);
328 if (err < 0)
329 return err;
330
331 cle = clEnqueueNDRangeKernel(ctx->command_queue, kernel, 2, NULL,
332 global_work, local_work,
333 0, NULL, NULL);
334 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle);
335 return 0;
336fail:
337 return err;
338}
339
341{
342 AVFilterContext *avctx = inlink->dst;
343 AVFilterLink *outlink = avctx->outputs[0];
344 TonemapOpenCLContext *ctx = avctx->priv;
345 AVFrame *output = NULL;
346 cl_int cle;
347 int err;
348 double peak = ctx->peak;
349
350 AVHWFramesContext *input_frames_ctx;
351
352 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
354 input->width, input->height, input->pts);
355
356 if (!input->hw_frames_ctx)
357 return AVERROR(EINVAL);
358 input_frames_ctx = (AVHWFramesContext*)input->hw_frames_ctx->data;
359
360 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
361 if (!output) {
362 err = AVERROR(ENOMEM);
363 goto fail;
364 }
365
366 err = av_frame_copy_props(output, input);
367 if (err < 0)
368 goto fail;
369
370 if (!peak)
371 peak = ff_determine_signal_peak(input);
372
373 if (ctx->trc != -1)
374 output->color_trc = ctx->trc;
375 if (ctx->primaries != -1)
376 output->color_primaries = ctx->primaries;
377 if (ctx->colorspace != -1)
378 output->colorspace = ctx->colorspace;
379 if (ctx->range != -1)
380 output->color_range = ctx->range;
381
382 ctx->trc_in = input->color_trc;
383 ctx->trc_out = output->color_trc;
384 ctx->colorspace_in = input->colorspace;
385 ctx->colorspace_out = output->colorspace;
386 ctx->primaries_in = input->color_primaries;
387 ctx->primaries_out = output->color_primaries;
388 ctx->range_in = input->color_range;
389 ctx->range_out = output->color_range;
390 ctx->chroma_loc = output->chroma_location;
391
392 if (!ctx->initialised) {
393 if (!(input->color_trc == AVCOL_TRC_SMPTE2084 ||
395 av_log(ctx, AV_LOG_ERROR, "unsupported transfer function characteristic.\n");
396 err = AVERROR(ENOSYS);
397 goto fail;
398 }
399
400 if (input_frames_ctx->sw_format != AV_PIX_FMT_P010) {
401 av_log(ctx, AV_LOG_ERROR, "unsupported format in tonemap_opencl.\n");
402 err = AVERROR(ENOSYS);
403 goto fail;
404 }
405
406 err = tonemap_opencl_init(avctx);
407 if (err < 0)
408 goto fail;
409 }
410
411 switch(input_frames_ctx->sw_format) {
412 case AV_PIX_FMT_P010:
413 err = launch_kernel(avctx, ctx->kernel, output, input, peak);
414 if (err < 0) goto fail;
415 break;
416 default:
417 err = AVERROR(ENOSYS);
418 goto fail;
419 }
420
421 cle = clFinish(ctx->command_queue);
422 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
423
424 av_frame_free(&input);
425
426 ff_update_hdr_metadata(output, ctx->target_peak);
427
428 av_log(ctx, AV_LOG_DEBUG, "Tone-mapping output: %s, %ux%u (%"PRId64").\n",
430 output->width, output->height, output->pts);
431#ifndef NDEBUG
432 {
433 uint32_t *ptr, *max_total_p, *avg_total_p, *frame_number_p;
434 float peak_detected, avg_detected;
435 unsigned map_size = (2 * DETECTION_FRAMES + 7) * sizeof(unsigned);
436 ptr = (void *)clEnqueueMapBuffer(ctx->command_queue, ctx->util_mem,
437 CL_TRUE, CL_MAP_READ, 0, map_size,
438 0, NULL, NULL, &cle);
439 // For the layout of the util buffer, refer tonemap.cl
440 if (ptr) {
441 max_total_p = ptr + 2 * (DETECTION_FRAMES + 1) + 1;
442 avg_total_p = max_total_p + 1;
443 frame_number_p = avg_total_p + 2;
444 peak_detected = (float)*max_total_p / (REFERENCE_WHITE * (*frame_number_p));
445 avg_detected = (float)*avg_total_p / (REFERENCE_WHITE * (*frame_number_p));
446 av_log(ctx, AV_LOG_DEBUG, "peak %f, avg %f will be used for next frame\n",
447 peak_detected, avg_detected);
448 clEnqueueUnmapMemObject(ctx->command_queue, ctx->util_mem, ptr, 0,
449 NULL, NULL);
450 }
451 }
452#endif
453
454 return ff_filter_frame(outlink, output);
455
456fail:
457 clFinish(ctx->command_queue);
458 av_frame_free(&input);
459 av_frame_free(&output);
460 return err;
461}
462
464{
465 TonemapOpenCLContext *ctx = avctx->priv;
466 cl_int cle;
467
468 if (ctx->util_mem)
469 clReleaseMemObject(ctx->util_mem);
470 if (ctx->kernel) {
471 cle = clReleaseKernel(ctx->kernel);
472 if (cle != CL_SUCCESS)
473 av_log(avctx, AV_LOG_ERROR, "Failed to release "
474 "kernel: %d.\n", cle);
475 }
476
477 if (ctx->command_queue) {
478 cle = clReleaseCommandQueue(ctx->command_queue);
479 if (cle != CL_SUCCESS)
480 av_log(avctx, AV_LOG_ERROR, "Failed to release "
481 "command queue: %d.\n", cle);
482 }
483
485}
486
487#define OFFSET(x) offsetof(TonemapOpenCLContext, x)
488#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
490 { "tonemap", "tonemap algorithm selection", OFFSET(tonemap), AV_OPT_TYPE_INT, {.i64 = TONEMAP_NONE}, TONEMAP_NONE, TONEMAP_MAX - 1, FLAGS, .unit = "tonemap" },
491 { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_NONE}, 0, 0, FLAGS, .unit = "tonemap" },
492 { "linear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_LINEAR}, 0, 0, FLAGS, .unit = "tonemap" },
493 { "gamma", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_GAMMA}, 0, 0, FLAGS, .unit = "tonemap" },
494 { "clip", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_CLIP}, 0, 0, FLAGS, .unit = "tonemap" },
495 { "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, .unit = "tonemap" },
496 { "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, .unit = "tonemap" },
497 { "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, .unit = "tonemap" },
498 { "transfer", "set transfer characteristic", OFFSET(trc), AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_BT709}, -1, INT_MAX, FLAGS, .unit = "transfer" },
499 { "t", "set transfer characteristic", OFFSET(trc), AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_BT709}, -1, INT_MAX, FLAGS, .unit = "transfer" },
500 { "bt709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709}, 0, 0, FLAGS, .unit = "transfer" },
501 { "bt2020", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10}, 0, 0, FLAGS, .unit = "transfer" },
502 { "matrix", "set colorspace matrix", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, .unit = "matrix" },
503 { "m", "set colorspace matrix", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, .unit = "matrix" },
504 { "bt709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_SPC_BT709}, 0, 0, FLAGS, .unit = "matrix" },
505 { "bt2020", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_SPC_BT2020_NCL}, 0, 0, FLAGS, .unit = "matrix" },
506 { "primaries", "set color primaries", OFFSET(primaries), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, .unit = "primaries" },
507 { "p", "set color primaries", OFFSET(primaries), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, .unit = "primaries" },
508 { "bt709", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_PRI_BT709}, 0, 0, FLAGS, .unit = "primaries" },
509 { "bt2020", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_PRI_BT2020}, 0, 0, FLAGS, .unit = "primaries" },
510 { "range", "set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, .unit = "range" },
511 { "r", "set color range", OFFSET(range), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS, .unit = "range" },
512 { "tv", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, .unit = "range" },
513 { "pc", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, .unit = "range" },
514 { "limited", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_MPEG}, 0, 0, FLAGS, .unit = "range" },
515 { "full", 0, 0, AV_OPT_TYPE_CONST, {.i64 = AVCOL_RANGE_JPEG}, 0, 0, FLAGS, .unit = "range" },
516 { "format", "output pixel format", OFFSET(format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, AV_PIX_FMT_NONE, INT_MAX, FLAGS, .unit = "fmt" },
517 { "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS },
518 { "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS },
519 { "desat", "desaturation parameter", OFFSET(desat_param), AV_OPT_TYPE_DOUBLE, {.dbl = 0.5}, 0, DBL_MAX, FLAGS },
520 { "threshold", "scene detection threshold", OFFSET(scene_threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.2}, 0, DBL_MAX, FLAGS },
521 { NULL }
522};
523
524AVFILTER_DEFINE_CLASS(tonemap_opencl);
525
527 {
528 .name = "default",
529 .type = AVMEDIA_TYPE_VIDEO,
530 .filter_frame = &tonemap_opencl_filter_frame,
531 .config_props = &ff_opencl_filter_config_input,
532 },
533};
534
536 {
537 .name = "default",
538 .type = AVMEDIA_TYPE_VIDEO,
539 .config_props = &tonemap_opencl_config_output,
540 },
541};
542
544 .p.name = "tonemap_opencl",
545 .p.description = NULL_IF_CONFIG_SMALL("Perform HDR to SDR conversion with tonemapping."),
546 .p.priv_class = &tonemap_opencl_class,
547 .p.flags = AVFILTER_FLAG_HWDEVICE,
548 .priv_size = sizeof(TonemapOpenCLContext),
554 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
555};
static const char *const format[]
Definition af_aiir.c:445
const FFFilter ff_vf_tonemap_opencl
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
Main libavfilter public API header.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:121
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
#define AV_BPRINT_SIZE_AUTOMATIC
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:596
static void fn rgb2yuv(uint8_t *_yuv[3], const ptrdiff_t yuv_stride[3], int16_t *rgb[3], ptrdiff_t s, int w, int h, const int16_t rgb2yuv_coeffs[3][3][8], const int16_t yuv_offset[8])
common internal and external API header
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
static void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
Definition g2meet.c:264
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
Definition opt.h:306
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:193
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const struct AVLumaCoefficients * av_csp_luma_coeffs_from_avcsp(enum AVColorSpace csp)
Retrieves the Luma coefficients necessary to construct a conversion matrix from an enum constant desc...
Definition csp.c:58
const AVColorPrimariesDesc * av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
Retrieves a complete gamut description from an enum constant describing the color primaries.
Definition csp.c:95
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
void ff_matrix_mul_3x3(double dst[3][3], const double src1[3][3], const double src2[3][3])
Definition colorspace.c:54
void ff_fill_rgb2xyz_table(const AVPrimaryCoefficients *coeffs, const AVWhitepointCoefficients *wp, double rgb2xyz[3][3])
Definition colorspace.c:79
double ff_determine_signal_peak(AVFrame *in)
Definition colorspace.c:155
void ff_matrix_invert_3x3(const double in[3][3], double out[3][3])
Definition colorspace.c:27
void ff_update_hdr_metadata(AVFrame *in, double peak)
Definition colorspace.c:180
void ff_fill_rgb2yuv_table(const AVLumaCoefficients *coeffs, double rgb2yuv[3][3])
Definition colorspace.c:127
#define REFERENCE_WHITE
Definition colorspace.h:27
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:269
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define NAN
enum AVColorPrimaries primaries
enum AVColorRange range
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition opencl.c:144
int ff_opencl_filter_load_program(AVFilterContext *avctx, const char **program_source_array, int nb_strings)
Load a new OpenCL program from strings in memory.
Definition opencl.c:159
int ff_opencl_filter_config_input(AVFilterLink *inlink)
Check that the input link contains a suitable hardware frames context and extract the device from it.
Definition opencl.c:46
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition opencl.c:135
int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx, size_t *work_size, AVFrame *frame, int plane, int block_alignment)
Find the work size needed needed for a given plane of an image.
Definition opencl.c:266
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition opencl.c:83
void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str, double mat[3][3])
Print a 3x3 matrix into a buffer as __constant array, which could be included in an OpenCL program.
Definition opencl.c:329
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
set argument to specific Kernel.
Definition opencl.h:61
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
Definition opencl.h:74
const char * ff_source_tonemap_cl
const char * ff_source_colorspace_common_cl
AVOptions.
const char * av_color_space_name(enum AVColorSpace space)
Definition pixdesc.c:3884
const char * av_color_range_name(enum AVColorRange range)
Definition pixdesc.c:3800
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
Definition pixdesc.c:3851
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
Definition pixdesc.c:3818
AVChromaLocation
Location of chroma samples.
Definition pixfmt.h:811
AVColorRange
Visual content value range.
Definition pixfmt.h:757
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
#define AV_PIX_FMT_P010
Definition pixfmt.h:613
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition pixfmt.h:358
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:647
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition pixfmt.h:649
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition pixfmt.h:658
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:677
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition pixfmt.h:694
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
Definition pixfmt.h:698
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
Definition pixfmt.h:692
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition pixfmt.h:679
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:711
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:713
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition pixfmt.h:722
static const uint8_t header[24]
Definition sdr2.c:68
uint8_t * data
The data buffer.
Definition buffer.h:90
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition csp.h:78
AVWhitepointCoefficients wp
Definition csp.h:79
AVPrimaryCoefficients prim
Definition csp.h:80
An instance of a filter.
Definition avfilter.h:279
void * priv
private data for use by the filter
Definition avfilter.h:294
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:291
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:581
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
enum AVChromaLocation chroma_location
Definition frame.h:746
int width
Definition frame.h:551
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition frame.h:779
int height
Definition frame.h:551
enum AVColorPrimaries color_primaries
Definition frame.h:735
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition frame.h:733
enum AVColorSpace colorspace
YUV colorspace type.
Definition frame.h:744
enum AVColorTransferCharacteristic color_trc
Definition frame.h:737
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:566
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
Struct containing luma coefficients to be used for RGB to YUV/YCoCg, or similar calculations.
Definition csp.h:48
AVRational cb
Definition csp.h:49
AVRational cr
Definition csp.h:49
AVRational cg
Definition csp.h:49
AVOption.
Definition opt.h:428
cl_command_queue command_queue
enum AVColorRange range range_in range_out
enum AVColorTransferCharacteristic trc trc_in trc_out
enum AVChromaLocation chroma_loc
enum AVPixelFormat format
OpenCLFilterContext ocf
enum AVColorPrimaries primaries primaries_in primaries_out
enum AVColorSpace colorspace colorspace_in colorspace_out
#define av_log(a,...)
static const float xyz2rgb[3][3]
Definition tiff.c:1912
TonemapAlgorithm
Definition vf_tonemap.c:42
@ TONEMAP_HABLE
Definition vf_tonemap.c:48
@ TONEMAP_LINEAR
Definition vf_tonemap.c:44
@ TONEMAP_MAX
Definition vf_tonemap.c:50
@ TONEMAP_MOBIUS
Definition vf_tonemap.c:49
@ TONEMAP_REINHARD
Definition vf_tonemap.c:47
@ TONEMAP_NONE
Definition vf_tonemap.c:43
@ TONEMAP_CLIP
Definition vf_tonemap.c:46
@ TONEMAP_GAMMA
Definition vf_tonemap.c:45
static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in, const AVPixFmtDescriptor *desc, int x, int y, double peak)
Definition vf_tonemap.c:110
static const char *const delinearize_funcs[]
static int launch_kernel(AVFilterContext *avctx, cl_kernel kernel, AVFrame *output, AVFrame *input, float peak)
static const char *const tonemap_func[TONEMAP_MAX]
static const AVOption tonemap_opencl_options[]
static const char *const linearize_funcs[]
static int tonemap_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
static const AVFilterPad tonemap_opencl_outputs[]
static int get_rgb2rgb_matrix(enum AVColorPrimaries in, enum AVColorPrimaries out, double rgb2rgb[3][3])
#define OFFSET(x)
static int tonemap_opencl_config_output(AVFilterLink *outlink)
static const float sdr_avg
static av_cold void tonemap_opencl_uninit(AVFilterContext *avctx)
#define DETECTION_FRAMES
#define OPENCL_SOURCE_NB
static const AVFilterPad tonemap_opencl_inputs[]
static int tonemap_opencl_init(AVFilterContext *avctx)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:120