FFmpeg
Loading...
Searching...
No Matches
vf_opencolorio.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Sam Richards
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "avfilter.h"
22#include "filters.h"
23#include "formats.h"
25#include "libavutil/opt.h"
26#include "libavutil/pixdesc.h"
27#include "libavutil/time.h"
28#include "ocio_wrapper.hpp"
29#include "video.h"
30
31typedef struct {
32 const AVClass *class;
36 char *display;
37 char *view;
42 char *out_format_string; // e.g. "rgb48le" which is converted to AVPixelFormat
43 // as output_format
44 int channels; // 3 or 4 depending on pixfmt
47
48typedef struct ThreadData {
49 AVFrame *in, *out;
51
52static int ocio_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
53{
54 OCIOContext *s = ctx->priv;
55 ThreadData *td = arg;
56 AVFrame *in = td->in;
57 AVFrame *out = td->out;
58 const int height = out->height;
59 const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
60 const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
61 const int slice_h = slice_end - slice_start;
62
63 return ocio_apply(ctx, s->ocio, in, out, slice_start, slice_h);
64}
65
66// Formats supported for output (also accepted as input).
80
81// All formats accepted as input, including half-float.
82// Half-float is input-only because swscale has no F16 write support.
83// We promote half-float to float on output, which is lossless. swscale's
84// legacy F16 read path also clamps to [0,1], and its new backend has no
85// half-float support yet.
86static const enum AVPixelFormat input_pix_fmts[] = {
87 // 8-bit
89 // 16-bit
91 // 10-bit
93 // 12-bit
95 // Half-float (input only)
97 // Float
100};
101
102
104 AVFilterFormatsConfig **cfg_in,
105 AVFilterFormatsConfig **cfg_out)
106{
107 static const enum AVPixelFormat float_fmts[] = {
109 };
110 const OCIOContext *s = ctx->priv;
111 const AVFilterFormats *in;
112 int has_f16 = 0;
113 int ret;
114
115 // Input accepts all supported formats including half-float
116 if (!cfg_in[0]->formats) {
118 &cfg_in[0]->formats);
119 if (ret < 0)
120 return ret;
121
122 // Returning AVERROR(EAGAIN) skips the generic defaults, but the
123 // graph may insert a converter on the input before this filter
124 // publishes anything, and that code expects these lists to exist.
125 if ((ret = ff_formats_ref(ff_all_color_spaces(), &cfg_in[0]->color_spaces)) < 0 ||
126 (ret = ff_formats_ref(ff_all_color_ranges(), &cfg_in[0]->color_ranges)) < 0 ||
127 (ret = ff_formats_ref(ff_all_alpha_modes(), &cfg_in[0]->alpha_modes)) < 0 ||
128 (ret = ff_formats_ref(ff_all_chroma_locations(), &cfg_in[0]->chroma_locations)) < 0)
129 return ret;
130 }
131
132 // The default output depends on the input, so wait until the input list
133 // has been merged with the upstream one. Until then it is still this
134 // filter's own list and says nothing about the actual input format.
135 // The merged list need not be a singleton. This inspects the link
136 // because the merge cannot be observed through cfg_in alone.
137 if (s->output_format == AV_PIX_FMT_NONE &&
138 ctx->inputs[0]->incfg.formats != cfg_in[0]->formats)
139 return AVERROR(EAGAIN);
140
141 // Only publish the output attributes together with the output pixel
142 // formats: a converter inserted on the output would otherwise find
143 // them set while the format list is still missing.
144 if ((ret = ff_formats_ref(cfg_in[0]->color_spaces, &cfg_out[0]->color_spaces)) < 0 ||
145 (ret = ff_formats_ref(cfg_in[0]->color_ranges, &cfg_out[0]->color_ranges)) < 0 ||
146 (ret = ff_formats_ref(cfg_in[0]->alpha_modes, &cfg_out[0]->alpha_modes)) < 0 ||
147 (ret = ff_formats_ref(cfg_in[0]->chroma_locations, &cfg_out[0]->chroma_locations)) < 0)
148 return ret;
149
150 // Output: if user specified a format, constrain to just that format
151 if (s->output_format != AV_PIX_FMT_NONE) {
152 const enum AVPixelFormat user_fmts[] = { s->output_format, AV_PIX_FMT_NONE };
154 &cfg_out[0]->formats);
155 }
156
157 // No user-specified format: the default output is the input format,
158 // except that half-float input is promoted to the matching float
159 // format, because swscale clips F16 to [0,1] when converting it.
160 in = cfg_in[0]->formats;
161 for (unsigned i = 0; i < in->nb_formats; i++) {
163 if (desc->comp[0].depth == 16 && (desc->flags & AV_PIX_FMT_FLAG_FLOAT))
164 has_f16 = 1;
165 }
166
167 // No half-float candidate: the output is the input format, so share
168 // the list and let downstream constraints narrow the input as well.
169 if (!has_f16)
170 return ff_formats_ref(cfg_in[0]->formats, &cfg_out[0]->formats);
171
172 // The input may be half-float (possibly among other candidates, e.g.
173 // behind zscale): only offer float output, which is lossless for every
174 // input format. Negotiation picks the one matching the input's alpha.
175 return ff_formats_ref(ff_make_pixel_format_list(float_fmts),
176 &cfg_out[0]->formats);
177}
178
179static int config_props(AVFilterLink *inlink)
180{
181 AVFilterContext *ctx = inlink->dst;
182 OCIOContext *s = ctx->priv;
184
185 if (!desc) {
186 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format\n");
187 return AVERROR(EINVAL);
188 }
189
190 // The output format was already constrained during negotiation.
191 // Read it from the negotiated output link if not set by the user.
192 if (s->output_format == AV_PIX_FMT_NONE)
193 s->output_format = ctx->outputs[0]->format;
194
195 s->channels = desc->nb_components; // 3 or 4
196
198 "Configuring OCIO for %s (bit depth: %d, channels: %d), output "
199 "format: (%s)\n",
200 av_get_pix_fmt_name(inlink->format), desc->comp[0].depth, s->channels,
201 av_get_pix_fmt_name(s->output_format));
202
203 // Now finalize the OCIO processor with the correct bit depth
204 int ret = ocio_finalize_processor(ctx, s->ocio, inlink->format, s->output_format);
205 if (ret < 0) {
207 "Failed to finalize OCIO processor for bit depth\n");
208 return AVERROR_EXTERNAL;
209 }
210
211 return 0;
212}
213
215{
216 OCIOContext *s = ctx->priv;
217 if (s->out_format_string != NULL) {
218 s->output_format = av_get_pix_fmt(s->out_format_string);
219 if (s->output_format == AV_PIX_FMT_NONE) {
220 av_log(ctx, AV_LOG_ERROR, "Unknown pixel format: '%s'\n",
221 s->out_format_string);
222 return AVERROR(EINVAL);
223 }
224 // Verify the format is a supported output format
225 int valid = 0;
226 for (int i = 0; output_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
227 if (s->output_format == output_pix_fmts[i]) {
228 valid = 1;
229 break;
230 }
231 }
232 if (!valid) {
234 "Unsupported output format: '%s'\n", s->out_format_string);
235 return AVERROR(EINVAL);
236 }
237 } else {
238 s->output_format = AV_PIX_FMT_NONE; // Default to same as input format (see later).
239 }
240
241 if (s->filetransform && strlen(s->filetransform) > 0) {
243 ctx, s->filetransform, s->inverse);
245 "Creating OCIO processor with FileTransform: %s, Inverse: %d\n",
246 s->filetransform, s->inverse);
247 } else if (s->output_space && strlen(s->output_space) > 0) {
249 ctx, s->config_path, s->input_space, s->output_space, s->context_params);
251 "Creating OCIO processor with config: %s, input: %s, output: %s\n",
252 s->config_path, s->input_space, s->output_space);
253 } else {
255 ctx, s->config_path, s->input_space, s->display, s->view, s->inverse, s->context_params);
257 "Creating OCIO processor with config: %s, input: %s, display: %s, "
258 "view: %s, Inverse: %d\n",
259 s->config_path, s->input_space, s->display, s->view, s->inverse);
260 }
261 if (!s->ocio) {
262 av_log(ctx, AV_LOG_ERROR, "Failed to create OCIO processor.\n");
263 return AVERROR(EINVAL);
264 }
265
266 return 0;
267}
268
270{
271 AVFilterContext *ctx = inlink->dst;
272 OCIOContext *s = ctx->priv;
274
275 if (!desc)
276 return AVERROR(EINVAL);
277
278 int ret;
280 ThreadData td;
281
282 if (s->output_format == inlink->format) {
283 /* No pixel-format conversion needed. If the input frame is
284 * writable we can apply OCIO in-place, otherwise allocate a
285 * separate output frame to avoid mutating shared buffers. */
288 } else {
290 if (!output_frame) {
292 return AVERROR(ENOMEM);
293 }
294 output_frame->format = s->output_format;
295 output_frame->width = frame->width;
296 output_frame->height = frame->height;
298
299 if (ret < 0) {
302 return ret;
303 }
305 }
306 } else {
307 // Allocate new output frame
309 if (!output_frame) {
311 return AVERROR(ENOMEM);
312 }
313 output_frame->format = s->output_format;
314 output_frame->width = frame->width;
315 output_frame->height = frame->height;
317
318 if (ret < 0) {
321 return ret;
322 }
324 }
325
326 td.in = frame;
327 td.out = output_frame;
328
329 // Use threads from context if set, otherwise let ffmpeg decide based on global settings or defaults
330 // Note: ctx->graph->nb_threads is usually the global thread count.
331 // ff_filter_get_nb_threads(ctx) gives the number of threads available for this filter.
332
333 int nb_jobs = ff_filter_get_nb_threads(ctx);
334
335 ret = ff_filter_execute(ctx, ocio_filter_slice, &td, NULL, FFMIN(output_frame->height, nb_jobs));
336
337 if (frame != output_frame)
339
340 if (ret < 0) {
341 av_log(ctx, AV_LOG_ERROR, "OCIO apply failed.\n");
342 return AVERROR(EINVAL);
343 }
344
345 return ff_filter_frame(ctx->outputs[0], output_frame);
346}
347
349{
350 OCIOContext *s = ctx->priv;
351 if (s->ocio) {
353 s->ocio = NULL;
354 }
355}
356
357#define OFFSET(x) offsetof(OCIOContext, x)
358#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
359
360static const AVOption ocio_options[] = {
361 { "config", "OCIO config path, overriding OCIO environment variable.", OFFSET(config_path), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
362 { "input", "Input color space", OFFSET(input_space), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
363 { "output", "Output color space", OFFSET(output_space), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
364 { "filetransform", "Specify a File Transform", OFFSET(filetransform), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
365 { "display", "Output display, used instead of output color space.", OFFSET(display), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
366 { "view", "View, output view transform, used in combination with display.", OFFSET(view), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
367 { "inverse", "Invert output display/view transform.", OFFSET(inverse), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
368 { "format", "Output video format", OFFSET(out_format_string), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
369 { "context_params", "OCIO context parameters", OFFSET(context_params), AV_OPT_TYPE_DICT, { .str = NULL }, 0, 0, FLAGS },{ NULL }
370};
371
373
374static const AVFilterPad inputs[] = {
375 {
376 .name = "default",
377 .type = AVMEDIA_TYPE_VIDEO,
378 .filter_frame = filter_frame,
379 .config_props = config_props,
380 },
381};
382
383static const AVFilterPad outputs[] = {
384 {.name = "default", .type = AVMEDIA_TYPE_VIDEO}};
385
387 .p.name = "ocio",
388 .p.description = NULL_IF_CONFIG_SMALL("Apply OCIO Display/View transform"),
389 .p.priv_class = &ocio_class,
391 .priv_size = sizeof(OCIOContext),
392 .init = init,
393 .uninit = uninit,
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static const AVFilterPad outputs[]
Definition af_aap.c:310
inverse
const FFFilter ff_vf_ocio
static FILE * out
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1721
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:857
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:596
#define NULL
Definition coverity.c:32
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:776
AVFilterFormats * ff_all_color_spaces(void)
Construct an AVFilterFormats representing all possible color spaces.
Definition formats.c:706
AVFilterFormats * ff_all_color_ranges(void)
Construct an AVFilterFormats representing all possible color ranges.
Definition formats.c:722
AVFilterFormats * ff_all_alpha_modes(void)
Construct an AVFilterFormats representing all possible alpha modes.
Definition formats.c:733
AVFilterFormats * ff_all_chroma_locations(void)
Construct an AVFilterFormats representing all possible chroma locations.
Definition formats.c:744
av_warn_unused_result AVFilterFormats * ff_make_pixel_format_list(const enum AVPixelFormat *fmts)
Create a list of supported pixel formats.
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition opt.h:289
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:172
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:538
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:209
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
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition h264dec.c:942
const char * arg
Definition jacosubdec.c:66
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:789
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:256
#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
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1711
void ocio_destroy_processor(AVFilterContext *ctx, OCIOHandle handle)
OCIOHandle ocio_create_display_view_processor(AVFilterContext *ctx, const char *config_path, const char *input_color_space, const char *display, const char *view, int inverse, AVDictionary *params)
OCIOHandle ocio_create_output_colorspace_processor(AVFilterContext *ctx, const char *config_path, const char *input_color_space, const char *output_color_space, AVDictionary *params)
int ocio_finalize_processor(AVFilterContext *ctx, OCIOHandle handle, int input_format, int output_format)
OCIOHandle ocio_create_file_transform_processor(AVFilterContext *ctx, const char *file_transform, int inverse)
int ocio_apply(AVFilterContext *ctx, OCIOHandle handle, AVFrame *input_frame, AVFrame *output_frame, int y_start, int height)
void * OCIOHandle
AVOptions.
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
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition pixdesc.c:3416
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
Definition pixdesc.h:158
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:572
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:587
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:567
#define AV_PIX_FMT_GBRAPF16
Definition pixfmt.h:586
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:538
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:568
#define AV_PIX_FMT_RGB48
Definition pixfmt.h:534
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
#define AV_PIX_FMT_GBRPF16
Definition pixfmt.h:585
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRAPF32
Definition pixfmt.h:588
formats
Definition signature.h:47
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:279
Lists of formats / etc.
Definition avfilter.h:120
AVFilterFormats * formats
List of supported formats (pixel or sample).
Definition avfilter.h:125
A list of supported formats for one end of a filter link.
Definition formats.h:64
int * formats
list of media formats
Definition formats.h:66
unsigned nb_formats
number of formats
Definition formats.h:65
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
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
char * config_path
char * input_space
AVDictionary * context_params
char * filetransform
char * output_space
OCIOHandle ocio
char * out_format_string
Used for passing data between threads.
Definition v210dec.c:35
AVFrame * out
#define av_log(a,...)
#define height
Definition dsp.h:99
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static enum AVPixelFormat output_pix_fmts[]
static const AVOption ocio_options[]
static enum AVPixelFormat input_pix_fmts[]
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int config_props(AVFilterLink *inlink)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static int ocio_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:851