| 1 | /* Copyright (C) 2018 the mpv developers |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | */ |
| 15 | |
| 16 | #ifndef MPV_CLIENT_API_RENDER_H_ |
| 17 | #define MPV_CLIENT_API_RENDER_H_ |
| 18 | |
| 19 | #include "client.h" |
| 20 | |
| 21 | #ifdef __cplusplus |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
| 25 | /** |
| 26 | * Overview |
| 27 | * -------- |
| 28 | * |
| 29 | * This API can be used to make mpv render using supported graphic APIs (such |
| 30 | * as OpenGL). It can be used to handle video display. |
| 31 | * |
| 32 | * The renderer needs to be created with mpv_render_context_create() before |
| 33 | * you start playback (or otherwise cause a VO to be created). Then (with most |
| 34 | * backends) mpv_render_context_render() can be used to explicitly render the |
| 35 | * current video frame. Use mpv_render_context_set_update_callback() to get |
| 36 | * notified when there is a new frame to draw. |
| 37 | * |
| 38 | * Preferably rendering should be done in a separate thread. If you call |
| 39 | * normal libmpv API functions on the renderer thread, deadlocks can result |
| 40 | * (these are made non-fatal with timeouts, but user experience will obviously |
| 41 | * suffer). See "Threading" section below. |
| 42 | * |
| 43 | * You can output and embed video without this API by setting the mpv "wid" |
| 44 | * option to a native window handle (see "Embedding the video window" section |
| 45 | * in the client.h header). In general, using the render API is recommended, |
| 46 | * because window embedding can cause various issues, especially with GUI |
| 47 | * toolkits and certain platforms. |
| 48 | * |
| 49 | * Supported backends |
| 50 | * ------------------ |
| 51 | * |
| 52 | * Direct3D 11: via MPV_RENDER_API_TYPE_D3D11, see render_d3d11.h header. |
| 53 | * OpenGL: via MPV_RENDER_API_TYPE_OPENGL, see render_gl.h header. |
| 54 | * Software: via MPV_RENDER_API_TYPE_SW, see section "Software renderer" |
| 55 | * |
| 56 | * Threading |
| 57 | * --------- |
| 58 | * |
| 59 | * You are recommended to do rendering on a separate thread than normal libmpv |
| 60 | * use. |
| 61 | * |
| 62 | * The mpv_render_* functions can be called from any thread, under the |
| 63 | * following conditions: |
| 64 | * - only one of the mpv_render_* functions can be called at the same time |
| 65 | * (unless they belong to different mpv cores created by mpv_create()) |
| 66 | * - never can be called from within the callbacks set with |
| 67 | * mpv_set_wakeup_callback() or mpv_render_context_set_update_callback() |
| 68 | * - if the OpenGL backend is used, for all functions the OpenGL context |
| 69 | * must be "current" in the calling thread, and it must be the same OpenGL |
| 70 | * context as the mpv_render_context was created with. Otherwise, undefined |
| 71 | * behavior will occur. |
| 72 | * - the thread does not call libmpv API functions other than the mpv_render_* |
| 73 | * functions, except APIs which are declared as safe (see below). Likewise, |
| 74 | * there must be no lock or wait dependency from the render thread to a |
| 75 | * thread using other libmpv functions. Basically, the situation that your |
| 76 | * render thread waits for a "not safe" libmpv API function to return must |
| 77 | * not happen. If you ignore this requirement, deadlocks can happen, which |
| 78 | * are made non-fatal with timeouts; then playback quality will be degraded, |
| 79 | * and the message |
| 80 | * mpv_render_context_render() not being called or stuck. |
| 81 | * is logged. If you set MPV_RENDER_PARAM_ADVANCED_CONTROL, you promise that |
| 82 | * this won't happen, and must absolutely guarantee it, or a real deadlock |
| 83 | * will freeze the mpv core thread forever. |
| 84 | * |
| 85 | * libmpv functions which are safe to call from a render thread are: |
| 86 | * - functions marked with "Safe to be called from mpv render API threads." |
| 87 | * - client.h functions which don't have an explicit or implicit mpv_handle |
| 88 | * parameter |
| 89 | * - mpv_render_* functions; but only for the same mpv_render_context pointer. |
| 90 | * If the pointer is different, mpv_render_context_free() is not safe. (The |
| 91 | * reason is that if MPV_RENDER_PARAM_ADVANCED_CONTROL is set, it may have |
| 92 | * to process still queued requests from the core, which it can do only for |
| 93 | * the current context, while requests for other contexts would deadlock. |
| 94 | * Also, it may have to wait and block for the core to terminate the video |
| 95 | * chain to make sure no resources are used after context destruction.) |
| 96 | * - if the mpv_handle parameter refers to a different mpv core than the one |
| 97 | * you're rendering for (very obscure, but allowed) |
| 98 | * |
| 99 | * Note about old libmpv version: |
| 100 | * |
| 101 | * Before API version 1.105 (basically in mpv 0.29.x), simply enabling |
| 102 | * MPV_RENDER_PARAM_ADVANCED_CONTROL could cause deadlock issues. This can |
| 103 | * be worked around by setting the "vd-lavc-dr" option to "no". |
| 104 | * In addition, you were required to call all mpv_render*() API functions |
| 105 | * from the same thread on which mpv_render_context_create() was originally |
| 106 | * run (for the same the mpv_render_context). Not honoring it led to UB |
| 107 | * (deadlocks, use of invalid mp_thread handles), even if you moved your GL |
| 108 | * context to a different thread correctly. |
| 109 | * These problems were addressed in API version 1.105 (mpv 0.30.0). |
| 110 | * |
| 111 | * Context and handle lifecycle |
| 112 | * ---------------------------- |
| 113 | * |
| 114 | * Video initialization will fail if the render context was not initialized yet |
| 115 | * (with mpv_render_context_create()), or it will revert to a VO that creates |
| 116 | * its own window. |
| 117 | * |
| 118 | * Currently, there can be only 1 mpv_render_context at a time per mpv core. |
| 119 | * |
| 120 | * Calling mpv_render_context_free() while a VO is using the render context is |
| 121 | * active will disable video. |
| 122 | * |
| 123 | * You must free the context with mpv_render_context_free() before the mpv core |
| 124 | * is destroyed. If this doesn't happen, undefined behavior will result. |
| 125 | * |
| 126 | * Software renderer |
| 127 | * ----------------- |
| 128 | * |
| 129 | * MPV_RENDER_API_TYPE_SW provides an extremely simple (but slow) renderer to |
| 130 | * memory surfaces. You probably don't want to use this. Use other render API |
| 131 | * types, or other methods of video embedding. |
| 132 | * |
| 133 | * Use mpv_render_context_create() with MPV_RENDER_PARAM_API_TYPE set to |
| 134 | * MPV_RENDER_API_TYPE_SW. |
| 135 | * |
| 136 | * Call mpv_render_context_render() with various MPV_RENDER_PARAM_SW_* fields |
| 137 | * to render the video frame to an in-memory surface. The following fields are |
| 138 | * required: MPV_RENDER_PARAM_SW_SIZE, MPV_RENDER_PARAM_SW_FORMAT, |
| 139 | * MPV_RENDER_PARAM_SW_STRIDE, MPV_RENDER_PARAM_SW_POINTER. |
| 140 | * |
| 141 | * This method of rendering is very slow, because everything, including color |
| 142 | * conversion, scaling, and OSD rendering, is done on the CPU, single-threaded. |
| 143 | * In particular, large video or display sizes, as well as presence of OSD or |
| 144 | * subtitles can make it too slow for realtime. As with other software rendering |
| 145 | * VOs, setting "sw-fast" may help. Enabling or disabling zimg may help, |
| 146 | * depending on the platform. |
| 147 | * |
| 148 | * In addition, certain multimedia job creation measures like HDR may not work |
| 149 | * properly, and will have to be manually handled by for example inserting |
| 150 | * filters. |
| 151 | * |
| 152 | * This API is not really suitable to extract individual frames from video etc. |
| 153 | * (basically non-playback uses) - there are better libraries for this. It can |
| 154 | * be used this way, but it may be clunky and tricky. |
| 155 | * |
| 156 | * Further notes: |
| 157 | * - MPV_RENDER_PARAM_FLIP_Y is currently ignored (unsupported) |
| 158 | * - MPV_RENDER_PARAM_DEPTH is ignored (meaningless) |
| 159 | */ |
| 160 | |
| 161 | /** |
| 162 | * Opaque context, returned by mpv_render_context_create(). |
| 163 | */ |
| 164 | typedef struct mpv_render_context mpv_render_context; |
| 165 | |
| 166 | /** |
| 167 | * Parameters for mpv_render_param (which is used in a few places such as |
| 168 | * mpv_render_context_create(). |
| 169 | * |
| 170 | * Also see mpv_render_param for conventions and how to use it. |
| 171 | */ |
| 172 | typedef enum mpv_render_param_type { |
| 173 | /** |
| 174 | * Not a valid value, but also used to terminate a params array. Its value |
| 175 | * is always guaranteed to be 0 (even if the ABI changes in the future). |
| 176 | */ |
| 177 | MPV_RENDER_PARAM_INVALID = 0, |
| 178 | /** |
| 179 | * The render API to use. Valid for mpv_render_context_create(). |
| 180 | * |
| 181 | * Type: char* |
| 182 | * |
| 183 | * Defined APIs: |
| 184 | * |
| 185 | * MPV_RENDER_API_TYPE_OPENGL: |
| 186 | * OpenGL desktop 2.1 or later (preferably core profile compatible to |
| 187 | * OpenGL 3.2), or OpenGLES 2.0 or later. |
| 188 | * Providing MPV_RENDER_PARAM_OPENGL_INIT_PARAMS is required. |
| 189 | * It is expected that an OpenGL context is valid and "current" when |
| 190 | * calling mpv_render_* functions (unless specified otherwise). It |
| 191 | * must be the same context for the same mpv_render_context. |
| 192 | */ |
| 193 | MPV_RENDER_PARAM_API_TYPE = 1, |
| 194 | /** |
| 195 | * Required parameters for initializing the OpenGL renderer. Valid for |
| 196 | * mpv_render_context_create(). |
| 197 | * Type: mpv_opengl_init_params* |
| 198 | */ |
| 199 | MPV_RENDER_PARAM_OPENGL_INIT_PARAMS = 2, |
| 200 | /** |
| 201 | * Describes a GL render target. Valid for mpv_render_context_render(). |
| 202 | * Type: mpv_opengl_fbo* |
| 203 | */ |
| 204 | MPV_RENDER_PARAM_OPENGL_FBO = 3, |
| 205 | /** |
| 206 | * Control flipped rendering. Valid for mpv_render_context_render(). |
| 207 | * Type: int* |
| 208 | * If the value is set to 0, render normally. Otherwise, render it flipped, |
| 209 | * which is needed e.g. when rendering to an OpenGL default framebuffer |
| 210 | * (which has a flipped coordinate system). |
| 211 | */ |
| 212 | MPV_RENDER_PARAM_FLIP_Y = 4, |
| 213 | /** |
| 214 | * Control surface depth. Valid for mpv_render_context_render(). |
| 215 | * Type: int* |
| 216 | * This implies the depth of the surface passed to the render function in |
| 217 | * bits per channel. If omitted or set to 0, the renderer will use the |
| 218 | * depth of the texture format with the Direct3D 11 backend, and assume 8 |
| 219 | * otherwise. Typically used to control dithering. |
| 220 | */ |
| 221 | MPV_RENDER_PARAM_DEPTH = 5, |
| 222 | /** |
| 223 | * ICC profile blob. Valid for mpv_render_context_set_parameter(). |
| 224 | * Type: mpv_byte_array* |
| 225 | * Set an ICC profile for use with the "icc-profile-auto" option. (If the |
| 226 | * option is not enabled, the ICC data will not be used.) |
| 227 | */ |
| 228 | MPV_RENDER_PARAM_ICC_PROFILE = 6, |
| 229 | /** |
| 230 | * Deprecated |
| 231 | * Ambient light in lux. Valid for mpv_render_context_set_parameter(). |
| 232 | * Type: int* |
| 233 | * This can be used for automatic gamma correction. |
| 234 | */ |
| 235 | MPV_RENDER_PARAM_AMBIENT_LIGHT = 7, |
| 236 | /** |
| 237 | * X11 Display, sometimes used for hwdec. Valid for |
| 238 | * mpv_render_context_create(). The Display must stay valid for the lifetime |
| 239 | * of the mpv_render_context. |
| 240 | * Type: Display* |
| 241 | */ |
| 242 | MPV_RENDER_PARAM_X11_DISPLAY = 8, |
| 243 | /** |
| 244 | * Wayland display, sometimes used for hwdec. Valid for |
| 245 | * mpv_render_context_create(). The wl_display must stay valid for the |
| 246 | * lifetime of the mpv_render_context. |
| 247 | * Type: struct wl_display* |
| 248 | */ |
| 249 | MPV_RENDER_PARAM_WL_DISPLAY = 9, |
| 250 | /** |
| 251 | * Better control about rendering and enabling some advanced features. Valid |
| 252 | * for mpv_render_context_create(). |
| 253 | * |
| 254 | * This conflates multiple requirements the API user promises to abide if |
| 255 | * this option is enabled: |
| 256 | * |
| 257 | * - The API user's render thread, which is calling the mpv_render_*() |
| 258 | * functions, never waits for the core. Otherwise deadlocks can happen. |
| 259 | * See "Threading" section. |
| 260 | * - The callback set with mpv_render_context_set_update_callback() can now |
| 261 | * be called even if there is no new frame. The API user should call the |
| 262 | * mpv_render_context_update() function, and interpret the return value |
| 263 | * for whether a new frame should be rendered. |
| 264 | * - Correct functionality is impossible if the update callback is not set, |
| 265 | * or not set soon enough after mpv_render_context_create() (the core can |
| 266 | * block while waiting for you to call mpv_render_context_update(), and |
| 267 | * if the update callback is not correctly set, it will deadlock, or |
| 268 | * block for too long). |
| 269 | * |
| 270 | * In general, setting this option will enable the following features (and |
| 271 | * possibly more): |
| 272 | * |
| 273 | * - "Direct rendering", which means the player decodes directly to a |
| 274 | * texture, which saves a copy per video frame ("vd-lavc-dr" option |
| 275 | * needs to be enabled, and the rendering backend as well as the |
| 276 | * underlying GPU API/driver needs to have support for it). |
| 277 | * - Rendering screenshots with the GPU API if supported by the backend |
| 278 | * (instead of using a suboptimal software fallback via libswscale). |
| 279 | * |
| 280 | * Warning: do not just add this without reading the "Threading" section |
| 281 | * above, and then wondering that deadlocks happen. The |
| 282 | * requirements are tricky. But also note that even if advanced |
| 283 | * control is disabled, not adhering to the rules will lead to |
| 284 | * playback problems. Enabling advanced controls simply makes |
| 285 | * violating these rules fatal. |
| 286 | * |
| 287 | * Type: int*: 0 for disable (default), 1 for enable |
| 288 | */ |
| 289 | MPV_RENDER_PARAM_ADVANCED_CONTROL = 10, |
| 290 | /** |
| 291 | * Return information about the next frame to render. Valid for |
| 292 | * mpv_render_context_get_info(). |
| 293 | * |
| 294 | * Type: mpv_render_frame_info* |
| 295 | * |
| 296 | * It strictly returns information about the _next_ frame. The implication |
| 297 | * is that e.g. mpv_render_context_update()'s return value will have |
| 298 | * MPV_RENDER_UPDATE_FRAME set, and the user is supposed to call |
| 299 | * mpv_render_context_render(). If there is no next frame, then the |
| 300 | * return value will have is_valid set to 0. |
| 301 | */ |
| 302 | MPV_RENDER_PARAM_NEXT_FRAME_INFO = 11, |
| 303 | /** |
| 304 | * Enable or disable video timing. Valid for mpv_render_context_render(). |
| 305 | * |
| 306 | * Type: int*: 0 for disable, 1 for enable (default) |
| 307 | * |
| 308 | * When video is timed to audio, the player attempts to render video a bit |
| 309 | * ahead, and then do a blocking wait until the target display time is |
| 310 | * reached. This blocks mpv_render_context_render() for up to the amount |
| 311 | * specified with the "video-timing-offset" global option. You can set |
| 312 | * this parameter to 0 to disable this kind of waiting. If you do, it's |
| 313 | * recommended to use the target time value in mpv_render_frame_info to |
| 314 | * wait yourself, or to set the "video-timing-offset" to 0 instead. |
| 315 | * |
| 316 | * Disabling this without doing anything in addition will result in A/V sync |
| 317 | * being slightly off. |
| 318 | */ |
| 319 | MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME = 12, |
| 320 | /** |
| 321 | * Use to skip rendering in mpv_render_context_render(). |
| 322 | * |
| 323 | * Type: int*: 0 for rendering (default), 1 for skipping |
| 324 | * |
| 325 | * If this is set, you don't need to pass a target surface to the render |
| 326 | * function (and if you do, it's completely ignored). This can still call |
| 327 | * into the lower level APIs (i.e. if you use OpenGL, the OpenGL context |
| 328 | * must be set). |
| 329 | * |
| 330 | * Be aware that the render API will consider this frame as having been |
| 331 | * rendered. All other normal rules also apply, for example about whether |
| 332 | * you have to call mpv_render_context_report_swap(). It also does timing |
| 333 | * in the same way. |
| 334 | */ |
| 335 | MPV_RENDER_PARAM_SKIP_RENDERING = 13, |
| 336 | /** |
| 337 | * Deprecated. Not supported. Use MPV_RENDER_PARAM_DRM_DISPLAY_V2 instead. |
| 338 | * Type : struct mpv_opengl_drm_params* |
| 339 | */ |
| 340 | MPV_RENDER_PARAM_DRM_DISPLAY = 14, |
| 341 | /** |
| 342 | * DRM draw surface size, contains draw surface dimensions. |
| 343 | * Valid for mpv_render_context_create(). |
| 344 | * Type : struct mpv_opengl_drm_draw_surface_size* |
| 345 | */ |
| 346 | MPV_RENDER_PARAM_DRM_DRAW_SURFACE_SIZE = 15, |
| 347 | /** |
| 348 | * DRM display, contains drm display handles. |
| 349 | * Valid for mpv_render_context_create(). |
| 350 | * Type : struct mpv_opengl_drm_params_v2* |
| 351 | */ |
| 352 | MPV_RENDER_PARAM_DRM_DISPLAY_V2 = 16, |
| 353 | /** |
| 354 | * MPV_RENDER_API_TYPE_SW only: rendering target surface size, mandatory. |
| 355 | * Valid for MPV_RENDER_API_TYPE_SW & mpv_render_context_render(). |
| 356 | * Type: int[2] (e.g.: int s[2] = {w, h}; param.data = &s[0];) |
| 357 | * |
| 358 | * The video frame is transformed as with other VOs. Typically, this means |
| 359 | * the video gets scaled and black bars are added if the video size or |
| 360 | * aspect ratio mismatches with the target size. |
| 361 | */ |
| 362 | MPV_RENDER_PARAM_SW_SIZE = 17, |
| 363 | /** |
| 364 | * MPV_RENDER_API_TYPE_SW only: rendering target surface pixel format, |
| 365 | * mandatory. |
| 366 | * Valid for MPV_RENDER_API_TYPE_SW & mpv_render_context_render(). |
| 367 | * Type: char* (e.g.: char *f = "rgb0"; param.data = f;) |
| 368 | * |
| 369 | * Valid values are: |
| 370 | * "rgb0", "bgr0", "0bgr", "0rgb" |
| 371 | * 4 bytes per pixel RGB, 1 byte (8 bit) per component, component bytes |
| 372 | * with increasing address from left to right (e.g. "rgb0" has r at |
| 373 | * address 0), the "0" component contains uninitialized garbage (often |
| 374 | * the value 0, but not necessarily; the bad naming is inherited from |
| 375 | * FFmpeg) |
| 376 | * Pixel alignment size: 4 bytes |
| 377 | * "rgb24" |
| 378 | * 3 bytes per pixel RGB. This is strongly discouraged because it is |
| 379 | * very slow. |
| 380 | * Pixel alignment size: 1 bytes |
| 381 | * other |
| 382 | * The API may accept other pixel formats, using mpv internal format |
| 383 | * names, as long as it's internally marked as RGB, has exactly 1 |
| 384 | * plane, and is supported as conversion output. It is not a good idea |
| 385 | * to rely on any of these. Their semantics and handling could change. |
| 386 | */ |
| 387 | MPV_RENDER_PARAM_SW_FORMAT = 18, |
| 388 | /** |
| 389 | * MPV_RENDER_API_TYPE_SW only: rendering target surface bytes per line, |
| 390 | * mandatory. |
| 391 | * Valid for MPV_RENDER_API_TYPE_SW & mpv_render_context_render(). |
| 392 | * Type: size_t* |
| 393 | * |
| 394 | * This is the number of bytes between a pixel (x, y) and (x, y + 1) on the |
| 395 | * target surface. It must be a multiple of the pixel size, and have space |
| 396 | * for the surface width as specified by MPV_RENDER_PARAM_SW_SIZE. |
| 397 | * |
| 398 | * Both stride and pointer value should be a multiple of 64 to facilitate |
| 399 | * fast SIMD operation. Lower alignment might trigger slower code paths, |
| 400 | * and in the worst case, will copy the entire target frame. If mpv is built |
| 401 | * with zimg (and zimg is not disabled), the performance impact might be |
| 402 | * less. |
| 403 | * In either cases, the pointer and stride must be aligned at least to the |
| 404 | * pixel alignment size. Otherwise, crashes and undefined behavior is |
| 405 | * possible on platforms which do not support unaligned accesses (either |
| 406 | * through normal memory access or aligned SIMD memory access instructions). |
| 407 | */ |
| 408 | MPV_RENDER_PARAM_SW_STRIDE = 19, |
| 409 | /* |
| 410 | * MPV_RENDER_API_TYPE_SW only: rendering target surface pixel data pointer, |
| 411 | * mandatory. |
| 412 | * Valid for MPV_RENDER_API_TYPE_SW & mpv_render_context_render(). |
| 413 | * Type: void* |
| 414 | * |
| 415 | * This points to the first pixel at the left/top corner (0, 0). In |
| 416 | * particular, each line y starts at (pointer + stride * y). Upon rendering, |
| 417 | * all data between pointer and (pointer + stride * h) is overwritten. |
| 418 | * Whether the padding between (w, y) and (0, y + 1) is overwritten is left |
| 419 | * unspecified (it should not be, but unfortunately some scaler backends |
| 420 | * will do it anyway). It is assumed that even the padding after the last |
| 421 | * line (starting at bytepos(w, h) until (pointer + stride * h)) is |
| 422 | * writable. |
| 423 | * |
| 424 | * See MPV_RENDER_PARAM_SW_STRIDE for alignment requirements. |
| 425 | */ |
| 426 | MPV_RENDER_PARAM_SW_POINTER = 20, |
| 427 | /** |
| 428 | * Required parameters for initializing the Direct3D 11 renderer. Valid |
| 429 | * for mpv_render_context_create(). |
| 430 | * Type: mpv_d3d11_init_params* |
| 431 | */ |
| 432 | MPV_RENDER_PARAM_D3D11_INIT_PARAMS = 21, |
| 433 | /** |
| 434 | * Describes a D3D11 render target. Valid for mpv_render_context_render(). |
| 435 | * Type: mpv_d3d11_fbo* |
| 436 | */ |
| 437 | MPV_RENDER_PARAM_D3D11_FBO = 22, |
| 438 | } mpv_render_param_type; |
| 439 | |
| 440 | /** |
| 441 | * For backwards compatibility with the old naming of |
| 442 | * MPV_RENDER_PARAM_DRM_DRAW_SURFACE_SIZE |
| 443 | */ |
| 444 | #define MPV_RENDER_PARAM_DRM_OSD_SIZE MPV_RENDER_PARAM_DRM_DRAW_SURFACE_SIZE |
| 445 | |
| 446 | /** |
| 447 | * Used to pass arbitrary parameters to some mpv_render_* functions. The |
| 448 | * meaning of the data parameter is determined by the type, and each |
| 449 | * MPV_RENDER_PARAM_* documents what type the value must point to. |
| 450 | * |
| 451 | * Each value documents the required data type as the pointer you cast to |
| 452 | * void* and set on mpv_render_param.data. For example, if MPV_RENDER_PARAM_FOO |
| 453 | * documents the type as Something* , then the code should look like this: |
| 454 | * |
| 455 | * Something foo = {...}; |
| 456 | * mpv_render_param param; |
| 457 | * param.type = MPV_RENDER_PARAM_FOO; |
| 458 | * param.data = & foo; |
| 459 | * |
| 460 | * Normally, the data field points to exactly 1 object. If the type is char*, |
| 461 | * it points to a 0-terminated string. |
| 462 | * |
| 463 | * In all cases (unless documented otherwise) the pointers need to remain |
| 464 | * valid during the call only. Unless otherwise documented, the API functions |
| 465 | * will not write to the params array or any data pointed to it. |
| 466 | * |
| 467 | * As a convention, parameter arrays are always terminated by type==0. There |
| 468 | * is no specific order of the parameters required. The order of the 2 fields in |
| 469 | * this struct is guaranteed (even after ABI changes). |
| 470 | */ |
| 471 | typedef struct mpv_render_param { |
| 472 | enum mpv_render_param_type type; |
| 473 | void *data; |
| 474 | } mpv_render_param; |
| 475 | |
| 476 | |
| 477 | /** |
| 478 | * Predefined values for MPV_RENDER_PARAM_API_TYPE. |
| 479 | */ |
| 480 | // See render_d3d11.h |
| 481 | #define MPV_RENDER_API_TYPE_D3D11 "d3d11" |
| 482 | // See render_gl.h |
| 483 | #define MPV_RENDER_API_TYPE_OPENGL "opengl" |
| 484 | // See section "Software renderer" |
| 485 | #define MPV_RENDER_API_TYPE_SW "sw" |
| 486 | |
| 487 | /** |
| 488 | * Flags used in mpv_render_frame_info.flags. Each value represents a bit in it. |
| 489 | */ |
| 490 | typedef enum mpv_render_frame_info_flag { |
| 491 | /** |
| 492 | * Set if there is actually a next frame. If unset, there is no next frame |
| 493 | * yet, and other flags and fields that require a frame to be queued will |
| 494 | * be unset. |
| 495 | * |
| 496 | * This is set for _any_ kind of frame, even for redraw requests. |
| 497 | * |
| 498 | * Note that when this is unset, it simply means no new frame was |
| 499 | * decoded/queued yet, not necessarily that the end of the video was |
| 500 | * reached. A new frame can be queued after some time. |
| 501 | * |
| 502 | * If the return value of mpv_render_context_render() had the |
| 503 | * MPV_RENDER_UPDATE_FRAME flag set, this flag will usually be set as well, |
| 504 | * unless the frame is rendered, or discarded by other asynchronous events. |
| 505 | */ |
| 506 | MPV_RENDER_FRAME_INFO_PRESENT = 1 << 0, |
| 507 | /** |
| 508 | * If set, the frame is not an actual new video frame, but a redraw request. |
| 509 | * For example if the video is paused, and an option that affects video |
| 510 | * rendering was changed (or any other reason), an update request can be |
| 511 | * issued and this flag will be set. |
| 512 | * |
| 513 | * Typically, redraw frames will not be subject to video timing. |
| 514 | * |
| 515 | * Implies MPV_RENDER_FRAME_INFO_PRESENT. |
| 516 | */ |
| 517 | MPV_RENDER_FRAME_INFO_REDRAW = 1 << 1, |
| 518 | /** |
| 519 | * If set, this is supposed to reproduce the previous frame perfectly. This |
| 520 | * is usually used for certain "video-sync" options ("display-..." modes). |
| 521 | * Typically the renderer will blit the video from a FBO. Unset otherwise. |
| 522 | * |
| 523 | * Implies MPV_RENDER_FRAME_INFO_PRESENT. |
| 524 | */ |
| 525 | MPV_RENDER_FRAME_INFO_REPEAT = 1 << 2, |
| 526 | /** |
| 527 | * If set, the player timing code expects that the user thread blocks on |
| 528 | * vsync (by either delaying the render call, or by making a call to |
| 529 | * mpv_render_context_report_swap() at vsync time). |
| 530 | * |
| 531 | * Implies MPV_RENDER_FRAME_INFO_PRESENT. |
| 532 | */ |
| 533 | MPV_RENDER_FRAME_INFO_BLOCK_VSYNC = 1 << 3, |
| 534 | } mpv_render_frame_info_flag; |
| 535 | |
| 536 | /** |
| 537 | * Information about the next video frame that will be rendered. Can be |
| 538 | * retrieved with MPV_RENDER_PARAM_NEXT_FRAME_INFO. |
| 539 | */ |
| 540 | typedef struct mpv_render_frame_info { |
| 541 | /** |
| 542 | * A bitset of mpv_render_frame_info_flag values (i.e. multiple flags are |
| 543 | * combined with bitwise or). |
| 544 | */ |
| 545 | uint64_t flags; |
| 546 | /** |
| 547 | * Absolute time at which the frame is supposed to be displayed. This is in |
| 548 | * the same unit and base as the time returned by mpv_get_time_us(). For |
| 549 | * frames that are redrawn, or if vsync locked video timing is used (see |
| 550 | * "video-sync" option), then this can be 0. The "video-timing-offset" |
| 551 | * option determines how much "headroom" the render thread gets (but a high |
| 552 | * enough frame rate can reduce it anyway). mpv_render_context_render() will |
| 553 | * normally block until the time is elapsed, unless you pass it |
| 554 | * MPV_RENDER_PARAM_BLOCK_FOR_TARGET_TIME = 0. |
| 555 | */ |
| 556 | int64_t target_time; |
| 557 | } mpv_render_frame_info; |
| 558 | |
| 559 | /** |
| 560 | * Initialize the renderer state. Depending on the backend used, this will |
| 561 | * access the underlying GPU API and initialize its own objects. |
| 562 | * |
| 563 | * You must free the context with mpv_render_context_free(). Not doing so before |
| 564 | * the mpv core is destroyed may result in memory leaks or crashes. |
| 565 | * |
| 566 | * Currently, only at most 1 context can exists per mpv core (it represents the |
| 567 | * main video output). |
| 568 | * |
| 569 | * You should pass the following parameters: |
| 570 | * - MPV_RENDER_PARAM_API_TYPE to select the underlying backend/GPU API. |
| 571 | * - Backend-specific init parameter, like MPV_RENDER_PARAM_OPENGL_INIT_PARAMS. |
| 572 | * - Setting MPV_RENDER_PARAM_ADVANCED_CONTROL and following its rules is |
| 573 | * strongly recommended. |
| 574 | * - If you want to use hwdec, possibly hwdec interop resources. |
| 575 | * |
| 576 | * @param res set to the context (on success) or NULL (on failure). The value |
| 577 | * is never read and always overwritten. |
| 578 | * @param mpv handle used to get the core (the mpv_render_context won't depend |
| 579 | * on this specific handle, only the core referenced by it) |
| 580 | * @param params an array of parameters, terminated by type==0. It's left |
| 581 | * unspecified what happens with unknown parameters. At least |
| 582 | * MPV_RENDER_PARAM_API_TYPE is required, and most backends will |
| 583 | * require another backend-specific parameter. |
| 584 | * @return error code, including but not limited to: |
| 585 | * MPV_ERROR_UNSUPPORTED: the OpenGL version is not supported |
| 586 | * (or required extensions are missing) |
| 587 | * MPV_ERROR_NOT_IMPLEMENTED: an unknown API type was provided, or |
| 588 | * support for the requested API was not |
| 589 | * built in the used libmpv binary. |
| 590 | * MPV_ERROR_INVALID_PARAMETER: at least one of the provided parameters was |
| 591 | * not valid. |
| 592 | */ |
| 593 | MPV_EXPORT int mpv_render_context_create(mpv_render_context **res, mpv_handle *mpv, |
| 594 | mpv_render_param *params); |
| 595 | |
| 596 | /** |
| 597 | * Attempt to change a single parameter. Not all backends and parameter types |
| 598 | * support all kinds of changes. |
| 599 | * |
| 600 | * @param ctx a valid render context |
| 601 | * @param param the parameter type and data that should be set |
| 602 | * @return error code. If a parameter could actually be changed, this returns |
| 603 | * success, otherwise an error code depending on the parameter type |
| 604 | * and situation. |
| 605 | */ |
| 606 | MPV_EXPORT int mpv_render_context_set_parameter(mpv_render_context *ctx, |
| 607 | mpv_render_param param); |
| 608 | |
| 609 | /** |
| 610 | * Retrieve information from the render context. This is NOT a counterpart to |
| 611 | * mpv_render_context_set_parameter(), because you generally can't read |
| 612 | * parameters set with it, and this function is not meant for this purpose. |
| 613 | * Instead, this is for communicating information from the renderer back to the |
| 614 | * user. See mpv_render_param_type; entries which support this function |
| 615 | * explicitly mention it, and for other entries you can assume it will fail. |
| 616 | * |
| 617 | * You pass param with param.type set and param.data pointing to a variable |
| 618 | * of the required data type. The function will then overwrite that variable |
| 619 | * with the returned value (at least on success). |
| 620 | * |
| 621 | * @param ctx a valid render context |
| 622 | * @param param the parameter type and data that should be retrieved |
| 623 | * @return error code. If a parameter could actually be retrieved, this returns |
| 624 | * success, otherwise an error code depending on the parameter type |
| 625 | * and situation. MPV_ERROR_NOT_IMPLEMENTED is used for unknown |
| 626 | * param.type, or if retrieving it is not supported. |
| 627 | */ |
| 628 | MPV_EXPORT int mpv_render_context_get_info(mpv_render_context *ctx, |
| 629 | mpv_render_param param); |
| 630 | |
| 631 | typedef void (*mpv_render_update_fn)(void *cb_ctx); |
| 632 | |
| 633 | /** |
| 634 | * Set the callback that notifies you when a new video frame is available, or |
| 635 | * if the video display configuration somehow changed and requires a redraw. |
| 636 | * Similar to mpv_set_wakeup_callback(), you must not call any mpv API from |
| 637 | * the callback, and all the other listed restrictions apply (such as not |
| 638 | * exiting the callback by throwing exceptions). |
| 639 | * |
| 640 | * This can be called from any thread, except from an update callback. In case |
| 641 | * of the OpenGL backend, no OpenGL state or API is accessed. |
| 642 | * |
| 643 | * Calling this will raise an update callback immediately. |
| 644 | * |
| 645 | * @param callback callback(callback_ctx) is called if the frame should be |
| 646 | * redrawn |
| 647 | * @param callback_ctx opaque argument to the callback |
| 648 | */ |
| 649 | MPV_EXPORT void mpv_render_context_set_update_callback(mpv_render_context *ctx, |
| 650 | mpv_render_update_fn callback, |
| 651 | void *callback_ctx); |
| 652 | |
| 653 | /** |
| 654 | * The API user is supposed to call this when the update callback was invoked |
| 655 | * (like all mpv_render_* functions, this has to happen on the render thread, |
| 656 | * and _not_ from the update callback itself). |
| 657 | * |
| 658 | * This is optional if MPV_RENDER_PARAM_ADVANCED_CONTROL was not set (default). |
| 659 | * Otherwise, it's a hard requirement that this is called after each update |
| 660 | * callback. If multiple update callback happened, and the function could not |
| 661 | * be called sooner, it's OK to call it once after the last callback. |
| 662 | * |
| 663 | * If an update callback happens during or after this function, the function |
| 664 | * must be called again at the soonest possible time. |
| 665 | * |
| 666 | * If MPV_RENDER_PARAM_ADVANCED_CONTROL was set, this will do additional work |
| 667 | * such as allocating textures for the video decoder. |
| 668 | * |
| 669 | * @return a bitset of mpv_render_update_flag values (i.e. multiple flags are |
| 670 | * combined with bitwise or). Typically, this will tell the API user |
| 671 | * what should happen next. E.g. if the MPV_RENDER_UPDATE_FRAME flag is |
| 672 | * set, mpv_render_context_render() should be called. If flags unknown |
| 673 | * to the API user are set, or if the return value is 0, nothing needs |
| 674 | * to be done. |
| 675 | */ |
| 676 | MPV_EXPORT uint64_t mpv_render_context_update(mpv_render_context *ctx); |
| 677 | |
| 678 | /** |
| 679 | * Flags returned by mpv_render_context_update(). Each value represents a bit |
| 680 | * in the function's return value. |
| 681 | */ |
| 682 | typedef enum mpv_render_update_flag { |
| 683 | /** |
| 684 | * A new video frame must be rendered. mpv_render_context_render() must be |
| 685 | * called. |
| 686 | */ |
| 687 | MPV_RENDER_UPDATE_FRAME = 1 << 0, |
| 688 | } mpv_render_context_flag; |
| 689 | |
| 690 | /** |
| 691 | * Render video. |
| 692 | * |
| 693 | * Typically renders the video to a target surface provided via mpv_render_param |
| 694 | * (the details depend on the backend in use). Options like "panscan" are |
| 695 | * applied to determine which part of the video should be visible and how the |
| 696 | * video should be scaled. You can change these options at runtime by using the |
| 697 | * mpv property API. |
| 698 | * |
| 699 | * The renderer will reconfigure itself every time the target surface |
| 700 | * configuration (such as size) is changed. |
| 701 | * |
| 702 | * This function implicitly pulls a video frame from the internal queue and |
| 703 | * renders it. If no new frame is available, the previous frame is redrawn. |
| 704 | * The update callback set with mpv_render_context_set_update_callback() |
| 705 | * notifies you when a new frame was added. The details potentially depend on |
| 706 | * the backends and the provided parameters. |
| 707 | * |
| 708 | * Generally, libmpv will invoke your update callback some time before the video |
| 709 | * frame should be shown, and then lets this function block until the supposed |
| 710 | * display time. This will limit your rendering to video FPS. You can prevent |
| 711 | * this by setting the "video-timing-offset" global option to 0. (This applies |
| 712 | * only to "audio" video sync mode.) |
| 713 | * |
| 714 | * You should pass the following parameters: |
| 715 | * - Backend-specific target object, such as MPV_RENDER_PARAM_OPENGL_FBO. |
| 716 | * - Possibly transformations, such as MPV_RENDER_PARAM_FLIP_Y. |
| 717 | * |
| 718 | * @param ctx a valid render context |
| 719 | * @param params an array of parameters, terminated by type==0. Which parameters |
| 720 | * are required depends on the backend. It's left unspecified what |
| 721 | * happens with unknown parameters. |
| 722 | * @return error code |
| 723 | */ |
| 724 | MPV_EXPORT int mpv_render_context_render(mpv_render_context *ctx, mpv_render_param *params); |
| 725 | |
| 726 | /** |
| 727 | * Tell the renderer that a frame was flipped at the given time. This is |
| 728 | * optional, but can help the player to achieve better timing. |
| 729 | * |
| 730 | * Note that calling this at least once informs libmpv that you will use this |
| 731 | * function. If you use it inconsistently, expect bad video playback. |
| 732 | * |
| 733 | * If this is called while no video is initialized, it is ignored. |
| 734 | * |
| 735 | * @param ctx a valid render context |
| 736 | */ |
| 737 | MPV_EXPORT void mpv_render_context_report_swap(mpv_render_context *ctx); |
| 738 | |
| 739 | /** |
| 740 | * Destroy the mpv renderer state. |
| 741 | * |
| 742 | * If video is still active (e.g. a file playing), video will be disabled |
| 743 | * forcefully. |
| 744 | * |
| 745 | * @param ctx a valid render context. After this function returns, this is not |
| 746 | * a valid pointer anymore. NULL is also allowed and does nothing. |
| 747 | */ |
| 748 | MPV_EXPORT void mpv_render_context_free(mpv_render_context *ctx); |
| 749 | |
| 750 | #ifdef MPV_CPLUGIN_DYNAMIC_SYM |
| 751 | |
| 752 | MPV_DEFINE_SYM_PTR(mpv_render_context_create) |
| 753 | #define mpv_render_context_create pfn_mpv_render_context_create |
| 754 | MPV_DEFINE_SYM_PTR(mpv_render_context_set_parameter) |
| 755 | #define mpv_render_context_set_parameter pfn_mpv_render_context_set_parameter |
| 756 | MPV_DEFINE_SYM_PTR(mpv_render_context_get_info) |
| 757 | #define mpv_render_context_get_info pfn_mpv_render_context_get_info |
| 758 | MPV_DEFINE_SYM_PTR(mpv_render_context_set_update_callback) |
| 759 | #define mpv_render_context_set_update_callback pfn_mpv_render_context_set_update_callback |
| 760 | MPV_DEFINE_SYM_PTR(mpv_render_context_update) |
| 761 | #define mpv_render_context_update pfn_mpv_render_context_update |
| 762 | MPV_DEFINE_SYM_PTR(mpv_render_context_render) |
| 763 | #define mpv_render_context_render pfn_mpv_render_context_render |
| 764 | MPV_DEFINE_SYM_PTR(mpv_render_context_report_swap) |
| 765 | #define mpv_render_context_report_swap pfn_mpv_render_context_report_swap |
| 766 | MPV_DEFINE_SYM_PTR(mpv_render_context_free) |
| 767 | #define mpv_render_context_free pfn_mpv_render_context_free |
| 768 | |
| 769 | #endif |
| 770 | |
| 771 | #ifdef __cplusplus |
| 772 | } |
| 773 | #endif |
| 774 | |
| 775 | #endif |
| 776 | |