1 /*
2  * This file is part of libplacebo.
3  *
4  * libplacebo 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  * libplacebo 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
12  * GNU 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 libplacebo.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef LIBPLACEBO_RENDERER_H_
19 #define LIBPLACEBO_RENDERER_H_
20 
21 #include <libplacebo/config.h>
22 #include <libplacebo/colorspace.h>
23 #include <libplacebo/filters.h>
24 #include <libplacebo/gpu.h>
25 #include <libplacebo/shaders/av1.h>
26 #include <libplacebo/shaders/colorspace.h>
27 #include <libplacebo/shaders/lut.h>
28 #include <libplacebo/shaders/sampling.h>
29 #include <libplacebo/shaders/custom.h>
30 #include <libplacebo/swapchain.h>
31 
32 PL_API_BEGIN
33 
34 // Thread-safety: Unsafe
35 typedef PL_STRUCT(pl_renderer) *pl_renderer;
36 
37 // Creates a new renderer object, which is backed by a GPU context. This is a
38 // high-level object that takes care of the rendering chain as a whole, from
39 // the source textures to the finished frame.
40 pl_renderer pl_renderer_create(pl_log log, pl_gpu gpu);
41 void pl_renderer_destroy(pl_renderer *rr);
42 
43 // Saves the internal shader cache of this renderer into an abstract cache
44 // object that can be saved to disk and later re-loaded to speed up
45 // recompilation of shaders. See `pl_dispatch_save` for more information.
46 size_t pl_renderer_save(pl_renderer rr, uint8_t *out_cache);
47 
48 // Load the result of a previous `pl_renderer_save` call. See
49 // `pl_dispatch_load` for more information.
50 void pl_renderer_load(pl_renderer rr, const uint8_t *cache);
51 
52 enum pl_lut_type {
53     PL_LUT_UNKNOWN = 0,
54     PL_LUT_NATIVE,      // applied to raw image contents (after fixing bit depth)
55     PL_LUT_NORMALIZED,  // applied to normalized RGB values
56     PL_LUT_CONVERSION,  // LUT fully replaces color conversion
57 
58     // Note: When using a PL_LUT_CONVERSION to replace the YUV->RGB conversion,
59     // `pl_render_params.color_adjustment` is no longer applied. Similarly,
60     // when using a PL_LUT_CONVERSION to replace the image->target color space
61     // conversion, `pl_render_params.color_map_params` are ignored.
62     //
63     // Note: For LUTs attached to the output frame, PL_LUT_CONVERSION should
64     // instead perform the inverse (RGB->native) conversion.
65     //
66     // Note: PL_LUT_UNKNOWN tries inferring the meaning of the LUT from the
67     // LUT's tagged metadata, and otherwise falls back to PL_LUT_NATIVE.
68 };
69 
70 enum pl_render_stage {
71     PL_RENDER_STAGE_FRAME,  // full frame redraws, for fresh/uncached frames
72     PL_RENDER_STAGE_BLEND,  // the output blend pass (only for pl_render_image_mix)
73     PL_RENDER_STAGE_COUNT,
74 };
75 
76 struct pl_render_info {
77     const struct pl_dispatch_info *pass;    // information about the shader
78     enum pl_render_stage stage;             // the associated render stage
79 
80     // For PL_RENDER_STAGE_FRAME, this specifies the chronological index
81     // of this pass within the frame (starting at `index == 0`).
82     //
83     // For PL_RENDER_STAGE_BLEND, this specifies the number of frames
84     // being blended (since that results in a different shader).
85     int index;
86 };
87 
88 // Represents the options used for rendering. These affect the quality of
89 // the result.
90 struct pl_render_params {
91     // Configures the algorithms used for upscaling and downscaling,
92     // respectively. If left as NULL, then libplacebo will only use inexpensive
93     // sampling (bilinear or neareast neighbour depending on the capabilities
94     // of the hardware / texture).
95     //
96     // Note: Setting `downscaler` to NULL also implies `skip_anti_aliasing`,
97     // since the built-in GPU sampling algorithms can't anti-alias.
98     //
99     // Note: If set to the same address as the built-in `pl_filter_bicubic`,
100     // `pl_filter_nearest` etc.; libplacebo will also use the more efficient
101     // direct sampling algorithm where possible without quality loss.
102     const struct pl_filter_config *upscaler;
103     const struct pl_filter_config *downscaler;
104 
105     // The number of entries for the scaler LUTs. Defaults to 64 if left unset.
106     int lut_entries;
107 
108     // The anti-ringing strength to apply to non-polar filters. See the
109     // equivalent option in `pl_sample_filter_params` for more information.
110     float antiringing_strength;
111 
112     // Configures the algorithm used for frame mixing (when using
113     // `pl_render_image_mix`). Ignored otherwise. As a special requirement,
114     // this must be a filter config with `polar` set to false, since it's only
115     // used for 1D mixing and thus only 1D filters are compatible.
116     //
117     // If set to NULL, frame mixing is disabled, in which case
118     // `pl_render_image_mix` behaves as `pl_render_image`, also completely
119     // bypassing the mixing cache.
120     const struct pl_filter_config *frame_mixer;
121 
122     // Configures the settings used to deband source textures. Leaving this as
123     // NULL disables debanding.
124     //
125     // Note: The `deband_params.grain` setting is automatically adjusted to
126     // prevent blowing up on HDR sources. The user need not account for this.
127     const struct pl_deband_params *deband_params;
128 
129     // Configures the settings used to sigmoidize the image before upscaling.
130     // This is not always used. If NULL, disables sigmoidization.
131     const struct pl_sigmoid_params *sigmoid_params;
132 
133     // Configures the color adjustment parameters used to decode the color.
134     // This can be used to apply additional artistic settings such as
135     // desaturation, etc. If NULL, defaults to &pl_color_adjustment_neutral.
136     const struct pl_color_adjustment *color_adjustment;
137 
138     // Configures the settings used to detect the peak of the source content,
139     // for HDR sources. Has no effect on SDR content. If NULL, peak detection
140     // is disabled.
141     const struct pl_peak_detect_params *peak_detect_params;
142 
143     // Configures the settings used to tone map from HDR to SDR, or from higher
144     // gamut to standard gamut content. If NULL, defaults to
145     // `&pl_color_map_default_params`.
146     const struct pl_color_map_params *color_map_params;
147 
148     // Configures the settings used to dither to the output depth. Leaving this
149     // as NULL disables dithering.
150     const struct pl_dither_params *dither_params;
151 
152     // Configures the settings used to handle ICC profiles, if required. If
153     // NULL, defaults to `&pl_icc_default_params`.
154     const struct pl_icc_params *icc_params;
155 
156     // Configures the settings used to simulate color blindness, if desired.
157     // If NULL, this feature is disabled.
158     const struct pl_cone_params *cone_params;
159 
160     // Configures output blending. When rendering to the final target, the
161     // framebuffer contents will be blended using this blend mode. Requires
162     // that the target format has PL_FMT_CAP_BLENDABLE. NULL disables blending.
163     const struct pl_blend_params *blend_params;
164 
165     // List of custom user shaders / hooks.
166     // See <libplacebo/shaders/custom.h> for more information.
167     const struct pl_hook * const *hooks;
168     int num_hooks;
169 
170     // Color mapping LUT. If present, this will be applied as part of the
171     // image being rendered, in normalized RGB space.
172     //
173     // Note: In this context, PL_LUT_NATIVE means "gamma light" and
174     // PL_LUT_NORMALIZED means "linear light". For HDR signals, normalized LUTs
175     // are scaled so 1.0 corresponds to the `pl_color_transfer_nominal_peak`.
176     //
177     // Note: A PL_LUT_CONVERSION fully replaces the color adaptation from
178     // `image` to `target`, including any tone-mapping (if necessary). It has
179     // the same representation as PL_LUT_NATIVE, so in this case the input
180     // and output are (respectively) non-linear light RGB.
181     const struct pl_custom_lut *lut;
182     enum pl_lut_type lut_type;
183 
184     // If the image being rendered does not span the entire size of the target,
185     // it will be cleared explicitly using this background color (RGB). To
186     // disable this logic, set `skip_target_clearing`.
187     float background_color[3];
188     float background_transparency; // 0.0 for opaque, 1.0 for fully transparent
189     bool skip_target_clearing;
190 
191     // --- Performance / quality trade-off options:
192     // These should generally be left off where quality is desired, as they can
193     // degrade the result quite noticeably; but may be useful for older or
194     // slower hardware. Note that libplacebo will automatically disable
195     // advanced features on hardware where they are unsupported, regardless of
196     // these settings. So only enable them if you need a performance bump.
197 
198     // Disables anti-aliasing on downscaling. This will result in moiré
199     // artifacts and nasty, jagged pixels when downscaling, except for some
200     // very limited special cases (e.g. bilinear downsampling to exactly 0.5x).
201     //
202     // Significantly speeds up downscaling with high downscaling ratios.
203     bool skip_anti_aliasing;
204 
205     // Cutoff value for polar sampling. See the equivalent option in
206     // `pl_sample_filter_params` for more information.
207     float polar_cutoff;
208 
209     // Allows the peak detection result to be delayed by up to a single frame,
210     // which can sometimes (not always) allow skipping some otherwise redundant
211     // sampling work. Only relevant when peak detection is active (i.e.
212     // params->peak_detect_params is set and the source is HDR).
213     bool allow_delayed_peak_detect;
214 
215     // Normally, when the size of the `target` used with `pl_render_image_mix`
216     // changes, or the render parameters are updated, the internal cache of
217     // mixed frames must be discarded in order to re-render all required
218     // frames. Setting this option to `true` will skip the cache invalidation
219     // and instead re-use the existing frames (with bilinear scaling to the new
220     // size if necessary), which comes at a quality loss shortly after a
221     // resize, but should make it much more smooth.
222     bool preserve_mixing_cache;
223 
224     // --- Performance tuning / debugging options
225     // These may affect performance or may make debugging problems easier,
226     // but shouldn't have any effect on the quality.
227 
228     // Disables linearization / sigmoidization before scaling. This might be
229     // useful when tracking down unexpected image artifacts or excessing
230     // ringing, but it shouldn't normally be necessary.
231     bool disable_linear_scaling;
232 
233     // Forces the use of the "general" scaling algorithms even when using the
234     // special-cased built-in presets like `pl_filter_bicubic`. Basically, this
235     // disables the more efficient implementations in favor of the slower,
236     // general-purpose ones.
237     bool disable_builtin_scalers;
238 
239     // Forces the use of an ICC 3DLUT, even in cases where the use of one is
240     // unnecessary. This is slower, but may improve the quality of the gamut
241     // reduction step, if one is performed.
242     bool force_icc_lut;
243 
244     // Ignore ICC profiles attached to either `image` or `target`.
245     // Note: A LUT may still be generated if `force_icc_lut` is also enabled.
246     bool ignore_icc_profiles;
247 
248     // Forces the use of dithering, even when rendering to 16-bit FBOs. This is
249     // generally pretty pointless because most 16-bit FBOs have high enough
250     // depth that rounding errors are below the human perception threshold,
251     // but this can be used to test the dither code.
252     bool force_dither;
253 
254     // Completely overrides the use of FBOs, as if there were no renderable
255     // texture format available. This disables most features.
256     bool disable_fbos;
257 
258     // If this is true, all shaders will be generated as "dynamic" shaders,
259     // with any compile-time constants being replaced by runtime-adjustable
260     // values. This is generally a performance loss, but has the advantage of
261     // being able to freely change parameters without triggering shader
262     // recompilations.
263     //
264     // It's a good idea to enable while presenting configurable settings to the
265     // user, but it should be set to false once those values are "dialed in".
266     bool dynamic_constants;
267 
268     // This callback is invoked for every pass successfully executed in the
269     // process of rendering a frame. Optional.
270     //
271     // Note: `info` is only valid until this function returns.
272     void (*info_callback)(void *priv, const struct pl_render_info *info);
273     void *info_priv;
274 
275     // --- Deprecated aliases
276     const struct pl_icc_params *lut3d_params PL_DEPRECATED; // fallback for `icc_params`
277     bool force_3dlut PL_DEPRECATED; // fallback for `force_icc_lut`
278 
279     // --- Deprecated/removed fields
280     bool disable_overlay_sampling PL_DEPRECATED; // no longer used
281 };
282 
283 // This contains the default/recommended options for reasonable image quality,
284 // while also not being too terribly slow. All of the *_params structs are
285 // defaulted to the corresponding *_default_params, except for deband_params,
286 // which is disabled by default.
287 //
288 // This should be fine on most integrated GPUs, but if it's too slow, consider
289 // setting the params to {0} instead, or alternatively setting
290 // `pl_render_params.disable_fbos` to true.
291 extern const struct pl_render_params pl_render_default_params;
292 
293 // This contains a higher quality preset for better image quality at the cost
294 // of quite a bit of performance. In addition to the settings implied by
295 // `pl_render_default_params`, it sets the upscaler to `pl_filter_ewa_lanczos`,
296 // and enables debanding. This should only really be used with a discrete GPU
297 // and where maximum image quality is desired.
298 extern const struct pl_render_params pl_render_high_quality_params;
299 
300 // Special filter config for the built-in oversampling algorithm. This is an
301 // opaque filter with no meaningful representation. though it has one tunable
302 // parameter controlling the threshold at which to switch back to ordinary
303 // nearest neighbour sampling. (See `pl_shader_sample_oversample`)
304 extern const struct pl_filter_config pl_filter_oversample;
305 
306 // Backwards compatibility
307 #define pl_oversample_frame_mixer pl_filter_oversample
308 
309 // A list of recommended frame mixer presets, terminated by {0}
310 extern const struct pl_filter_preset pl_frame_mixers[];
311 extern const int pl_num_frame_mixers; // excluding trailing {0}
312 
313 // A list of recommended scaler presets, terminated by {0}. This is almost
314 // equivalent to `pl_filter_presets` with the exception of including extra
315 // built-in filters that don't map to the `pl_filter` architecture.
316 extern const struct pl_filter_preset pl_scale_filters[];
317 extern const int pl_num_scale_filters; // excluding trailing {0}
318 
319 #define PL_MAX_PLANES 4
320 
321 // High level description of a single slice of an image. This basically
322 // represents a single 2D plane, with any number of components
323 struct pl_plane {
324     // The texture underlying this plane. The texture must be 2D, and must
325     // have specific parameters set depending on what the plane is being used
326     // for (see `pl_render_image`).
327     pl_tex texture;
328 
329     // The preferred behaviour when sampling outside of this texture. Optional,
330     // since the default (PL_TEX_ADDRESS_CLAMP) is very reasonable.
331     enum pl_tex_address_mode address_mode;
332 
333     // Describes the number and interpretation of the components in this plane.
334     // This defines the mapping from component index to the canonical component
335     // order (RGBA, YCbCrA or XYZA). It's worth pointing out that this is
336     // completely separate from `texture->format.sample_order`. The latter is
337     // essentially irrelevant/transparent for the API user, since it just
338     // determines which order the texture data shows up as inside the GLSL
339     // shader; whereas this field controls the actual meaning of the component.
340     //
341     // Example; if the user has a plane with just {Y} and a plane with just
342     // {Cb Cr}, and a GPU that only supports bgra formats, you would still
343     // specify the component mapping as {0} and {1 2} respectively, even though
344     // the GPU is sampling the data in the order BGRA. Use -1 for "ignored"
345     // components.
346     int components;           // number of relevant components
347     int component_mapping[4]; // semantic index of each component
348 
349     // Controls the sample offset, relative to the "reference" dimensions. For
350     // an example of what to set here, see `pl_chroma_location_offset`. Note
351     // that this is given in unit of reference pixels. For a graphical example,
352     // imagine you have a 2x2 image with a 1x1 (subsampled) plane. Without any
353     // shift (0.0), the situation looks like this:
354     //
355     // X-------X  X = reference pixel
356     // |       |  P = plane pixel
357     // |   P   |
358     // |       |
359     // X-------X
360     //
361     // For 4:2:0 subsampling, this corresponds to PL_CHROMA_CENTER. If the
362     // shift_x was instead set to -0.5, the `P` pixel would be offset to the
363     // left by half the separation between the reference (`X` pixels), resulting
364     // in the following:
365     //
366     // X-------X  X = reference pixel
367     // |       |  P = plane pixel
368     // P       |
369     // |       |
370     // X-------X
371     //
372     // For 4:2:0 subsampling, this corresponds to PL_CHROMA_LEFT.
373     //
374     // Note: It's recommended to fill this using `pl_chroma_location_offset` on
375     // the chroma planes.
376     float shift_x, shift_y;
377 };
378 
379 enum pl_overlay_mode {
380     PL_OVERLAY_NORMAL = 0, // treat the texture as a normal, full-color texture
381     PL_OVERLAY_MONOCHROME, // treat the texture as a single-component alpha map
382     PL_OVERLAY_MODE_COUNT,
383 };
384 
385 struct pl_overlay_part {
386     struct pl_rect2df src; // source coordinate with respect to `tex`
387     struct pl_rect2d dst;  // target coordinates with respect to the frame
388 
389     // If `mode` is PL_OVERLAY_MONOCHROME, then this specifies the color of
390     // this overlay part. The color is multiplied into the sampled texture's
391     // first channel.
392     float color[4];
393 };
394 
395 // A struct representing an image overlay (e.g. for subtitles or on-screen
396 // status messages, controls, ...)
397 struct pl_overlay {
398     // The texture containing the backing data for overlay parts. Must have
399     // `params.sampleable` set.
400     pl_tex tex;
401 
402     // This controls the coloring mode of this overlay.
403     enum pl_overlay_mode mode;
404 
405     // This controls the colorspace information for this overlay. The contents
406     // of the texture / the value of `color` are interpreted according to this.
407     struct pl_color_repr repr;
408     struct pl_color_space color;
409 
410     // The number of parts for this overlay.
411     const struct pl_overlay_part *parts;
412     int num_parts;
413 
414     // (Deprecated) These fields exist for backwards compatibility. They must
415     // not be used as the same times as `tex`. They are interpreted as an
416     // overlay with a single part.
417     struct pl_plane plane PL_DEPRECATED;
418     struct pl_rect2d rect PL_DEPRECATED; // analog to `pl_overlay_part.dst`
419     float base_color[3] PL_DEPRECATED; // analog to `pl_overlay_part.color`
420 };
421 
422 // High-level description of a complete frame, including metadata and planes
423 struct pl_frame {
424     // Each frame is split up into some number of planes, each of which may
425     // carry several components and be of any size / offset.
426     int num_planes;
427     struct pl_plane planes[PL_MAX_PLANES];
428 
429     // Color representation / encoding / semantics of this frame.
430     struct pl_color_repr repr;
431     struct pl_color_space color;
432 
433     // Optional ICC profile associated with this frame.
434     struct pl_icc_profile profile;
435 
436     // Optional LUT associated with this frame.
437     const struct pl_custom_lut *lut;
438     enum pl_lut_type lut_type;
439 
440     // The logical crop / rectangle containing the valid information, relative
441     // to the reference plane's dimensions (e.g. luma). Pixels outside of this
442     // rectangle will ostensibly be ignored, but note that this is not a hard
443     // guarantee. In particular, scaler filters may end up sampling outside of
444     // this crop. This rect may be flipped, and may be partially or wholly
445     // outside the bounds of the underlying textures. (Optional)
446     //
447     // Note that `pl_render_image` will map the input crop directly to the
448     // output crop, stretching and scaling as needed. If you wish to preserve
449     // the aspect ratio, use a dedicated function like pl_rect2df_aspect_copy.
450     struct pl_rect2df crop;
451 
452     // A list of additional overlays to render directly on top of this frame.
453     // These overlays will be treated as though they were part of the frame
454     // data, and can be used for things like subtitles or on-screen displays.
455     const struct pl_overlay *overlays;
456     int num_overlays;
457 
458     // Note on subsampling and plane correspondence: All planes belonging to
459     // the same frame will only be stretched by an integer multiple (or inverse
460     // thereof) in order to match the reference dimensions of this image. For
461     // example, suppose you have an 8x4 image. A valid plane scaling would be
462     // 4x2 -> 8x4 or 4x4 -> 4x4, but not 6x4 -> 8x4. So if a 6x4 plane is
463     // given, then it would be treated like a cropped 8x4 plane (since 1.0 is
464     // the closest scaling ratio to the actual ratio of 1.3).
465     //
466     // For an explanation of why this makes sense, consider the relatively
467     // common example of a subsampled, oddly sized (e.g. jpeg) image. In such
468     // cases, for example a 35x23 image, the 4:2:0 subsampled chroma plane
469     // would have to end up as 17.5x11.5, which gets rounded up to 18x12 by
470     // implementations. So in this example, the 18x12 chroma plane would get
471     // treated by libplacebo as an oversized chroma plane - i.e. the plane
472     // would get sampled as if it was 17.5 pixels wide and 11.5 pixels large.
473 
474     // Associated AV1 grain params (see <libplacebo/shaders/av1.h>). This is
475     // entirely optional, the default of {0} corresponds to no extra grain.
476     //
477     // Note: This is ignored for the `target` of `pl_render_image`, since
478     // un-applying grain makes little sense.
479     struct pl_av1_grain_data av1_grain;
480 
481     // Ignored by libplacebo. May be useful for users.
482     void *user_data;
483 
484     // Deprecated fields provided merely for backwards compatibility. The
485     // use of these should be discontinued as soon as possible.
486     int width PL_DEPRECATED; // ignored
487     int height PL_DEPRECATED;
488     uint64_t signature PL_DEPRECATED; // ignored
489     pl_tex fbo PL_DEPRECATED; // fallback for `target.planes`
490     struct pl_rect2df src_rect PL_DEPRECATED; // fallback for `image.crop`
491     struct pl_rect2df dst_rect PL_DEPRECATED; // fallback for `target.crop`
492 };
493 
494 // Helper function to infer the chroma location offset for each plane in a
495 // frame. This is equivalent to calling `pl_chroma_location_offset` on all
496 // subsampled planes' shift_x/shift_y variables.
497 void pl_frame_set_chroma_location(struct pl_frame *frame,
498                                   enum pl_chroma_location chroma_loc);
499 
500 // Fills in a `pl_frame` based on a swapchain frame's FBO and metadata.
501 void pl_frame_from_swapchain(struct pl_frame *out_frame,
502                              const struct pl_swapchain_frame *frame);
503 
504 // Helper function to determine if a frame is logically cropped or not. In
505 // particular, this is useful in determining whether or not an output frame
506 // needs to be cleared before rendering or not.
507 bool pl_frame_is_cropped(const struct pl_frame *frame);
508 
509 // Helper function to reset a frame to a given RGB color. If the frame's
510 // color representation is something other than RGB, the clear color will
511 // be adjusted accordingly. `clear_color` should be non-premultiplied.
512 void pl_frame_clear_rgba(pl_gpu gpu, const struct pl_frame *frame,
513                          const float clear_color[4]);
514 
515 // Like `pl_frame_clear_rgba` but without an alpha channel.
pl_frame_clear(pl_gpu gpu,const struct pl_frame * frame,const float clear_color[3])516 static inline void pl_frame_clear(pl_gpu gpu, const struct pl_frame *frame,
517                                   const float clear_color[3])
518 {
519     const float clear_color_rgba[4] = { clear_color[0], clear_color[1], clear_color[2], 1.0 };
520     pl_frame_clear_rgba(gpu, frame, clear_color_rgba);
521 }
522 
523 // Deprecated aliases, provided for backwards compatibility
524 #define pl_image pl_frame
525 #define pl_render_target pl_frame
526 
pl_image_set_chroma_location(struct pl_frame * frame,enum pl_chroma_location chroma_loc)527 static PL_DEPRECATED inline void pl_image_set_chroma_location(
528         struct pl_frame *frame, enum pl_chroma_location chroma_loc)
529 {
530     return pl_frame_set_chroma_location(frame, chroma_loc);
531 }
532 
pl_render_target_set_chroma_location(struct pl_frame * frame,enum pl_chroma_location chroma_loc)533 static PL_DEPRECATED inline void pl_render_target_set_chroma_location(
534         struct pl_frame *frame, enum pl_chroma_location chroma_loc)
535 {
536     return pl_frame_set_chroma_location(frame, chroma_loc);
537 }
538 
pl_render_target_from_swapchain(struct pl_frame * out_frame,const struct pl_swapchain_frame * frame)539 static PL_DEPRECATED inline void pl_render_target_from_swapchain(
540         struct pl_frame *out_frame, const struct pl_swapchain_frame *frame)
541 {
542     return pl_frame_from_swapchain(out_frame, frame);
543 }
544 
pl_render_target_partial(const struct pl_frame * frame)545 static PL_DEPRECATED inline bool pl_render_target_partial(
546         const struct pl_frame *frame)
547 {
548     return pl_frame_is_cropped(frame);
549 }
550 
551 // Render a single image to a target using the given parameters. This is
552 // fully dynamic, i.e. the params can change at any time. libplacebo will
553 // internally detect and flush whatever caches are invalidated as a result of
554 // changing colorspace, size etc.
555 //
556 // Required plane capabilities:
557 // - Planes in `image` must be `sampleable`
558 // - Planes in `target` must be `renderable`
559 //
560 // Recommended plane capabilities: (Optional, but good for performance)
561 // - Planes in `image` should have `sample_mode` PL_TEX_SAMPLE_LINEAR
562 // - Planes in `target` should be `storable`
563 // - Planes in `target` should have `blit_dst`
564 //
565 // Note on lifetime: Once this call returns, the passed structures may be
566 // freely overwritten or discarded by the caller, even the referenced
567 // `pl_tex` objects may be freely reused.
568 //
569 // Note on overlays: `image.overlays` will be rendered directly onto the image,
570 // which means they get affected by things like scaling and frame mixing.
571 // `target.overlays` will also be rendered, but directly onto the target. They
572 // don't even need to be inside `target.crop`.
573 //
574 // Note: `image` may be NULL, in which case `target.overlays` will still be
575 // rendered, but nothing else.
576 bool pl_render_image(pl_renderer rr, const struct pl_frame *image,
577                      const struct pl_frame *target,
578                      const struct pl_render_params *params);
579 
580 // Flushes the internal state of this renderer. This is normally not needed,
581 // even if the image parameters, colorspace or target configuration change,
582 // since libplacebo will internally detect such circumstances and recreate
583 // outdated resources automatically. Doing this explicitly *may* be useful to
584 // purge some state related to things like HDR peak detection or frame mixing,
585 // so calling it is a good idea if the content source is expected to change
586 // dramatically (e.g. when switching to a different file).
587 void pl_renderer_flush_cache(pl_renderer rr);
588 
589 // Represents a mixture of input frames, distributed temporally.
590 //
591 // NOTE: Frames must be sorted by timestamp, i.e. `timestamps` must be
592 // monotonically increasing.
593 struct pl_frame_mix {
594     // The number of frames in this mixture. The number of frames should be
595     // sufficient to meet the needs of the configured frame mixer. See the
596     // section below for more information.
597     //
598     // If the number of frames is 0, this call will be equivalent to
599     // `pl_render_image` with `image == NULL`. If the number of frames is 1,
600     // this is equivalent to `pl_render_image` with that image.
601     int num_frames;
602 
603     // A list of the frames themselves. The frames can have different
604     // colorspaces, configurations of planes, or even sizes.
605     //
606     // Note: This is a list of pointers, to avoid users having to copy
607     // around `pl_frame` structs when re-organizing this array.
608     const struct pl_frame **frames;
609 
610     // A list of unique signatures, one for each frame. These are used to
611     // identify frames across calls to this function, so it's crucial that they
612     // be both unique per-frame but also stable across invocations of
613     // `pl_render_frame_mix`.
614     const uint64_t *signatures;
615 
616     // A list of relative timestamps for each frame. These are relative to the
617     // time of the vsync being drawn, i.e. this function will render the frame
618     // that will be made visible at timestamp 0.0. The values are expected to
619     // be normalized such that a separation of 1.0 corresponds to roughly one
620     // nominal source frame duration. So a constant framerate video file will
621     // always have timestamps like e.g. {-2.3, -1.3, -0.3, 0.7, 1.7, 2.7},
622     // using an example radius of 3.
623     //
624     // In cases where the framerate is variable (e.g. VFR video), the choice of
625     // what to scale to use can be difficult to answer. A typical choice would
626     // be either to use the canonical (container-tagged) framerate, or the
627     // highest momentary framerate, as a reference. If all else fails, you
628     // could also use the display's framerate.
629     //
630     // Note: This function assumes zero-order-hold semantics, i.e. the frame at
631     // timestamp 0.7 is intended to remain visible until timestamp 1.7, when
632     // the next frame replaces it.
633     const float *timestamps;
634 
635     // The duration for which the vsync being drawn will be held, using the
636     // same scale as `timestamps`. If the display has an unknown or variable
637     // frame-rate (e.g. Adaptive Sync), then you're probably better off not
638     // using this function and instead just painting the frames directly using
639     // `pl_render_frame` at the correct PTS.
640     //
641     // As an example, if `vsync_duration` is 0.4, then it's assumed that the
642     // vsync being painted is visible for the period [0.0, 0.4].
643     float vsync_duration;
644 
645     // Explanation of the frame mixing radius: The algorithm chosen in
646     // `pl_render_params.frame_mixer` has a canonical radius equal to
647     // `pl_filter_config.kernel->radius`. This means that the frame mixing
648     // algorithm will (only) need to consult all of the frames that have a
649     // distance within the interval [-radius, radius]. As such, the user should
650     // include all such frames in `frames`, but may prune or omit frames that
651     // lie outside it.
652     //
653     // The built-in frame mixing (`pl_render_params.frame_mixer == NULL`) has
654     // no concept of radius, it just always needs access to the "current" and
655     // "next" frames.
656 };
657 
658 // Helper function to calculate the frame mixing radius.
pl_frame_mix_radius(const struct pl_render_params * params)659 static inline float pl_frame_mix_radius(const struct pl_render_params *params)
660 {
661     // For backwards compatibility, allow !frame_mixer->kernel
662     if (!params->frame_mixer || !params->frame_mixer->kernel)
663         return 0.0;
664 
665     return params->frame_mixer->kernel->radius;
666 }
667 
668 // Render a mixture of images to the target using the given parameters. This
669 // functions much like a generalization of `pl_render_image`, for when the API
670 // user has more control over the frame queue / vsync loop, and can provide a
671 // few frames from the past and future + timestamp information.
672 //
673 // This allows libplacebo to perform rudimentary frame mixing / interpolation,
674 // in order to eliminate judder artifacts typically associated with
675 // source/display frame rate mismatch.
676 bool pl_render_image_mix(pl_renderer rr, const struct pl_frame_mix *images,
677                          const struct pl_frame *target,
678                          const struct pl_render_params *params);
679 
680 PL_API_END
681 
682 #endif // LIBPLACEBO_RENDERER_H_
683