FFmpeg: libavfilter/vf_tonemap_opencl.c Source File
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 */
19
25
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
49 };
50
53
59
72
76 };
77
81 };
82
91 };
92
94 double rgb2rgb[3][3]) {
95 double rgb2xyz[3][3],
xyz2rgb[3][3];
96
99
100 if (!in_primaries || !out_primaries)
102
107
108 return 0;
109 }
110
111 #define OPENCL_SOURCE_NB 3
112 // Average light level for SDR signals. This is equal to a signal level of 0.5
113 // under a typical presentation gamma of about 2.0.
115
117 {
119 int rgb2rgb_passthrough = 1;
122 cl_int cle;
123 int err;
126
128
129 switch(
ctx->tonemap) {
133 break;
136 ctx->param = (1.0f -
ctx->param) /
ctx->param;
137 break;
141 break;
142 }
143
146
147 // SDR peak is 1.0f
148 ctx->target_peak = 1.0f;
161 // checking valid value just because of limited implementaion
162 // please remove when more functionalities are implemented
171
180 ctx->scene_threshold);
183
184 if (
ctx->primaries_out !=
ctx->primaries_in) {
187 rgb2rgb_passthrough = 0;
188 }
191
194
196
197 if (rgb2rgb_passthrough)
199 else
201
202
204 if (!luma_src) {
209 }
210
212 if (!luma_dst) {
217 }
218
221
225
230
234
237
240
242 opencl_sources[0] =
header.str;
246
248 if (err < 0)
250
251 ctx->command_queue = clCreateCommandQueue(
ctx->ocf.hwctx->context,
252 ctx->ocf.hwctx->device_id,
253 0, &cle);
255 "command queue %d.\n", cle);
256
257 ctx->kernel = clCreateKernel(
ctx->ocf.program,
"tonemap", &cle);
259
261 clCreateBuffer(
ctx->ocf.hwctx->context, 0,
265
266 ctx->initialised = 1;
267 return 0;
268
272 clReleaseMemObject(
ctx->util_mem);
273 if (
ctx->command_queue)
274 clReleaseCommandQueue(
ctx->command_queue);
276 clReleaseKernel(
ctx->kernel);
277 return err;
278 }
279
281 {
287 else {
291 "only p010/nv12 supported now\n");
293 }
294 }
295
300
301 return 0;
302 }
303
308 size_t global_work[2];
309 size_t local_work[2];
310 cl_int cle;
311
318
319 local_work[0] = 16;
320 local_work[1] = 16;
321 // Note the work size based on uv plane, as we process a 2x2 quad in one workitem
323 1, 16);
324 if (err < 0)
325 return err;
326
327 cle = clEnqueueNDRangeKernel(
ctx->command_queue, kernel, 2,
NULL,
328 global_work, local_work,
331 return 0;
333 return err;
334 }
335
337 {
342 cl_int cle;
343 int err;
344 double peak =
ctx->peak;
345
347
351
352 if (!
input->hw_frames_ctx)
355
360 }
361
363 if (err < 0)
365
366 if (!peak)
368
371 if (
ctx->primaries != -1)
372 output->color_primaries =
ctx->primaries;
373 if (
ctx->colorspace != -1)
375 if (
ctx->range != -1)
377
380 ctx->colorspace_in =
input->colorspace;
381 ctx->colorspace_out =
output->colorspace;
382 ctx->primaries_in =
input->color_primaries;
383 ctx->primaries_out =
output->color_primaries;
386 ctx->chroma_loc =
output->chroma_location;
387
388 if (!
ctx->initialised) {
394 }
395
400 }
401
403 if (err < 0)
405 }
406
410 if (err < 0)
goto fail;
411 break;
412 default:
415 }
416
417 cle = clFinish(
ctx->command_queue);
419
421
423
427 #ifndef NDEBUG
428 {
429 uint32_t *ptr, *max_total_p, *avg_total_p, *frame_number_p;
430 float peak_detected, avg_detected;
432 ptr = (
void *)clEnqueueMapBuffer(
ctx->command_queue,
ctx->util_mem,
433 CL_TRUE, CL_MAP_READ, 0, map_size,
435 // For the layout of the util buffer, refer tonemap.cl
436 if (ptr) {
438 avg_total_p = max_total_p + 1;
439 frame_number_p = avg_total_p + 2;
443 peak_detected, avg_detected);
444 clEnqueueUnmapMemObject(
ctx->command_queue,
ctx->util_mem, ptr, 0,
446 }
447 }
448 #endif
449
451
453 clFinish(
ctx->command_queue);
456 return err;
457 }
458
460 {
462 cl_int cle;
463
465 clReleaseMemObject(
ctx->util_mem);
467 cle = clReleaseKernel(
ctx->kernel);
468 if (cle != CL_SUCCESS)
470 "kernel: %d.\n", cle);
471 }
472
473 if (
ctx->command_queue) {
474 cle = clReleaseCommandQueue(
ctx->command_queue);
475 if (cle != CL_SUCCESS)
477 "command queue: %d.\n", cle);
478 }
479
481 }
482
483 #define OFFSET(x) offsetof(TonemapOpenCLContext, x)
484 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
498 {
"matrix",
"set colorspace matrix",
OFFSET(colorspace),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
FLAGS, .unit =
"matrix" },
518 };
519
521
523 {
528 },
529 };
530
532 {
536 },
537 };
538
540 .
name =
"tonemap_opencl",
543 .priv_class = &tonemap_opencl_class,
551 };
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
#define AV_LOG_WARNING
Something somehow does not look correct.
AVPixelFormat
Pixel format.
enum AVPixelFormat format
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static const char *const tonemap_func[TONEMAP_MAX]
AVColorTransferCharacteristic
Color Transfer Characteristic.
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
set argument to specific Kernel.
AVWhitepointCoefficients wp
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Struct that contains both white point location and primaries location, providing the complete descrip...
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static int launch_kernel(AVFilterContext *avctx, cl_kernel kernel, AVFrame *output, AVFrame *input, float peak)
void ff_matrix_invert_3x3(const double in[3][3], double out[3][3])
static int tonemap_opencl_config_output(AVFilterLink *outlink)
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define FILTER_INPUTS(array)
This structure describes decoded (raw) audio or video data.
@ AVCOL_TRC_NB
Not part of ABI.
@ AVCOL_RANGE_JPEG
Full range content.
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...
static const char rgb2yuv[]
double ff_determine_signal_peak(AVFrame *in)
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.
Struct containing luma coefficients to be used for RGB to YUV/YCoCg, or similar calculations.
AVColorPrimaries
Chromaticity coordinates of the source primaries.
const char * name
Filter name.
A link between two filters.
enum AVColorSpace colorspace colorspace_in colorspace_out
static const char *const delinearize_funcs[AVCOL_TRC_NB]
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.
const char * av_color_space_name(enum AVColorSpace space)
void * priv
private data for use by the filter
enum AVChromaLocation chroma_loc
#define AV_BPRINT_SIZE_AUTOMATIC
static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in, const AVPixFmtDescriptor *desc, int x, int y, double peak)
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
A filter pad used for either input or output.
enum AVColorPrimaries primaries primaries_in primaries_out
static int tonemap_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
static int get_rgb2rgb_matrix(enum AVColorPrimaries in, enum AVColorPrimaries out, double rgb2rgb[3][3])
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const AVColorPrimariesDesc * av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
Retrieves a complete gamut description from an enum constant describing the color primaries.
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define FILTER_OUTPUTS(array)
static const AVFilterPad tonemap_opencl_outputs[]
static av_cold void tonemap_opencl_uninit(AVFilterContext *avctx)
const char * av_color_range_name(enum AVColorRange range)
void ff_matrix_mul_3x3(double dst[3][3], const double src1[3][3], const double src2[3][3])
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
cl_command_queue command_queue
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
const char * av_color_primaries_name(enum AVColorPrimaries primaries)
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
enum AVColorRange range range_in range_out
@ AVCOL_PRI_BT2020
ITU-R BT2020.
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
int(* init)(AVBSFContext *ctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
static const AVFilterPad tonemap_opencl_inputs[]
static const float sdr_avg
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
const char * ff_source_tonemap_cl
void ff_update_hdr_metadata(AVFrame *in, double peak)
static const float xyz2rgb[3][3]
AVFilterContext * src
source filter
static const uint8_t header[24]
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
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.
@ AVCOL_TRC_BT709
also ITU-R BT1361
AVChromaLocation
Location of chroma samples.
AVFILTER_DEFINE_CLASS(tonemap_opencl)
enum AVColorTransferCharacteristic trc trc_in trc_out
static void uninit(AVBSFContext *ctx)
void ff_fill_rgb2yuv_table(const AVLumaCoefficients *coeffs, double rgb2yuv[3][3])
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
int w
agreed upon image width
AVColorSpace
YUV colorspace type.
static int tonemap_opencl_init(AVFilterContext *avctx)
const char * ff_source_colorspace_common_cl
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.
const char * name
Pad name.
enum TonemapAlgorithm tonemap
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
const AVFilter ff_vf_tonemap_opencl
This struct describes a set or pool of "hardware" frames (i.e.
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
@ 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...
void ff_fill_rgb2xyz_table(const AVPrimaryCoefficients *coeffs, const AVWhitepointCoefficients *wp, double rgb2xyz[3][3])
void av_bprintf(AVBPrint *buf, const char *fmt,...)
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
int h
agreed upon image height
static const char *const linearize_funcs[AVCOL_TRC_NB]
@ AV_OPT_TYPE_INT
Underlying C type is int.
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
static const AVOption tonemap_opencl_options[]
AVPrimaryCoefficients prim
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
AVColorRange
Visual content value range.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
static void yuv2rgb(uint8_t *out, int ridx, int Y, int U, int V)
const char * av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
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.
AVFilterLink ** outputs
array of pointers to output links
Generated on Fri Aug 22 2025 13:59:21 for FFmpeg by
doxygen
1.8.17