1 /*
2  * Copyright (C) Aaron Holtzman - Aug 1999
3  *
4  * Strongly modified, most parts rewritten: A'rpi/ESP-team - 2000-2001
5  * (C) MPlayer developers
6  *
7  * This file is part of mpv.
8  *
9  * mpv is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * mpv is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef MPLAYER_VIDEO_OUT_H
24 #define MPLAYER_VIDEO_OUT_H
25 
26 #include <inttypes.h>
27 #include <stdbool.h>
28 
29 #include "video/img_format.h"
30 #include "common/common.h"
31 #include "options/options.h"
32 
33 enum {
34     // VO needs to redraw
35     VO_EVENT_EXPOSE                     = 1 << 0,
36     // VO needs to update state to a new window size
37     VO_EVENT_RESIZE                     = 1 << 1,
38     // The ICC profile needs to be reloaded
39     VO_EVENT_ICC_PROFILE_CHANGED        = 1 << 2,
40     // Some other window state changed (position, window state, fps)
41     VO_EVENT_WIN_STATE                  = 1 << 3,
42     // The ambient light conditions changed and need to be reloaded
43     VO_EVENT_AMBIENT_LIGHTING_CHANGED   = 1 << 4,
44     // Special mechanism for making resizing with Cocoa react faster
45     VO_EVENT_LIVE_RESIZING              = 1 << 5,
46     // For VOCTRL_GET_HIDPI_SCALE changes.
47     VO_EVENT_DPI                        = 1 << 6,
48     // Special thing for encode mode (vo_driver.initially_blocked).
49     // Part of VO_EVENTS_USER to make vo_is_ready_for_frame() work properly.
50     VO_EVENT_INITIAL_UNBLOCK            = 1 << 7,
51     VO_EVENT_FOCUS                      = 1 << 8,
52 
53     // Set of events the player core may be interested in.
54     VO_EVENTS_USER = VO_EVENT_RESIZE | VO_EVENT_WIN_STATE | VO_EVENT_DPI |
55                      VO_EVENT_INITIAL_UNBLOCK | VO_EVENT_FOCUS,
56 };
57 
58 enum mp_voctrl {
59     /* signal a device reset seek */
60     VOCTRL_RESET = 1,
61     /* Handle input and redraw events, called by vo_check_events() */
62     VOCTRL_CHECK_EVENTS,
63     /* signal a device pause */
64     VOCTRL_PAUSE,
65     /* start/resume playback */
66     VOCTRL_RESUME,
67 
68     VOCTRL_SET_PANSCAN,
69     VOCTRL_SET_EQUALIZER,
70 
71     // Triggered by any change to mp_vo_opts. This is for convenience. In theory,
72     // you could install your own listener.
73     VOCTRL_VO_OPTS_CHANGED,
74 
75     /* private to vo_gpu */
76     VOCTRL_LOAD_HWDEC_API,
77 
78     // Redraw the image previously passed to draw_image() (basically, repeat
79     // the previous draw_image call). If this is handled, the OSD should also
80     // be updated and redrawn. Optional; emulated if not available.
81     VOCTRL_REDRAW_FRAME,
82 
83     // Only used internally in vo_opengl_cb
84     VOCTRL_PREINIT,
85     VOCTRL_UNINIT,
86     VOCTRL_RECONFIG,
87 
88     VOCTRL_UPDATE_WINDOW_TITLE,         // char*
89     VOCTRL_UPDATE_PLAYBACK_STATE,       // struct voctrl_playback_state*
90 
91     VOCTRL_PERFORMANCE_DATA,            // struct voctrl_performance_data*
92 
93     VOCTRL_SET_CURSOR_VISIBILITY,       // bool*
94 
95     VOCTRL_KILL_SCREENSAVER,
96     VOCTRL_RESTORE_SCREENSAVER,
97 
98     // Return or set window size (not-fullscreen mode only - if fullscreened,
99     // these must access the not-fullscreened window size only).
100     VOCTRL_GET_UNFS_WINDOW_SIZE,        // int[2] (w/h)
101     VOCTRL_SET_UNFS_WINDOW_SIZE,        // int[2] (w/h)
102 
103     VOCTRL_GET_FOCUSED,                 // bool*
104 
105     // char *** (NULL terminated array compatible with CONF_TYPE_STRING_LIST)
106     // names for displays the window is on
107     VOCTRL_GET_DISPLAY_NAMES,
108 
109     // Retrieve window contents. (Normal screenshots use vo_get_current_frame().)
110     // Deprecated for VOCTRL_SCREENSHOT with corresponding flags.
111     VOCTRL_SCREENSHOT_WIN,              // struct mp_image**
112 
113     // A normal screenshot - VOs can react to this if vo_get_current_frame() is
114     // not sufficient.
115     VOCTRL_SCREENSHOT,                  // struct voctrl_screenshot*
116 
117     VOCTRL_UPDATE_RENDER_OPTS,
118 
119     VOCTRL_GET_ICC_PROFILE,             // bstr*
120     VOCTRL_GET_AMBIENT_LUX,             // int*
121     VOCTRL_GET_DISPLAY_FPS,             // double*
122     VOCTRL_GET_HIDPI_SCALE,             // double*
123     VOCTRL_GET_DISPLAY_RES,             // int[2]
124 
125     /* private to vo_gpu */
126     VOCTRL_EXTERNAL_RESIZE,
127 };
128 
129 #define VO_TRUE         true
130 #define VO_FALSE        false
131 #define VO_ERROR        -1
132 #define VO_NOTAVAIL     -2
133 #define VO_NOTIMPL      -3
134 
135 // VOCTRL_UPDATE_PLAYBACK_STATE
136 struct voctrl_playback_state {
137     bool taskbar_progress;
138     bool playing;
139     bool paused;
140     int percent_pos;
141 };
142 
143 // VOCTRL_PERFORMANCE_DATA
144 #define VO_PERF_SAMPLE_COUNT 256
145 
146 struct mp_pass_perf {
147     // times are all in nanoseconds
148     uint64_t last, avg, peak;
149     uint64_t samples[VO_PERF_SAMPLE_COUNT];
150     uint64_t count;
151 };
152 
153 #define VO_PASS_PERF_MAX 64
154 
155 struct mp_frame_perf {
156     int count;
157     struct mp_pass_perf perf[VO_PASS_PERF_MAX];
158     // The owner of this struct does not have ownership over the names, and
159     // they may change at any time - so this struct should not be stored
160     // anywhere or the results reused
161     char *desc[VO_PASS_PERF_MAX];
162 };
163 
164 struct voctrl_performance_data {
165     struct mp_frame_perf fresh, redraw;
166 };
167 
168 struct voctrl_screenshot {
169     bool scaled, subs, osd, high_bit_depth;
170     struct mp_image *res;
171 };
172 
173 enum {
174     // VO does handle mp_image_params.rotate in 90 degree steps
175     VO_CAP_ROTATE90     = 1 << 0,
176     // VO does framedrop itself (vo_vdpau). Untimed/encoding VOs never drop.
177     VO_CAP_FRAMEDROP    = 1 << 1,
178     // VO does not allow frames to be retained (vo_mediacodec_embed).
179     VO_CAP_NORETAIN     = 1 << 2,
180 };
181 
182 #define VO_MAX_REQ_FRAMES 10
183 
184 struct vo;
185 struct osd_state;
186 struct mp_image;
187 struct mp_image_params;
188 
189 struct vo_extra {
190     struct input_ctx *input_ctx;
191     struct osd_state *osd;
192     struct encode_lavc_context *encode_lavc_ctx;
193     void (*wakeup_cb)(void *ctx);
194     void *wakeup_ctx;
195 };
196 
197 struct vo_frame {
198     // If > 0, realtime when frame should be shown, in mp_time_us() units.
199     // If 0, present immediately.
200     int64_t pts;
201     // Approximate frame duration, in us.
202     int duration;
203     // Realtime of estimated distance between 2 vsync events.
204     double vsync_interval;
205     // "ideal" display time within the vsync
206     double vsync_offset;
207     // "ideal" frame duration (can be different from num_vsyncs*vsync_interval
208     // up to a vsync) - valid for the entire frame, i.e. not changed for repeats
209     double ideal_frame_duration;
210     // how often the frame will be repeated (does not include OSD redraws)
211     int num_vsyncs;
212     // Set if the current frame is repeated from the previous. It's guaranteed
213     // that the current is the same as the previous one, even if the image
214     // pointer is different.
215     // The repeat flag is set if exactly the same frame should be rendered
216     // again (and the OSD does not need to be redrawn).
217     // A repeat frame can be redrawn, in which case repeat==redraw==true, and
218     // OSD should be updated.
219     bool redraw, repeat;
220     // The frame is not in movement - e.g. redrawing while paused.
221     bool still;
222     // Frames are output as fast as possible, with implied vsync blocking.
223     bool display_synced;
224     // Dropping the frame is allowed if the VO is behind.
225     bool can_drop;
226     // The current frame to be drawn.
227     // Warning: When OSD should be redrawn in --force-window --idle mode, this
228     //          can be NULL. The VO should draw a black background, OSD on top.
229     struct mp_image *current;
230     // List of future images, starting with the current one. This does not
231     // care about repeated frames - it simply contains the next real frames.
232     // vo_set_queue_params() sets how many future frames this should include.
233     // The actual number of frames delivered to the VO can be lower.
234     // frames[0] is current, frames[1] is the next frame.
235     // Note that some future frames may never be sent as current frame to the
236     // VO if frames are dropped.
237     int num_frames;
238     struct mp_image *frames[VO_MAX_REQ_FRAMES];
239     // ID for frames[0] (== current). If current==NULL, the number is
240     // meaningless. Otherwise, it's an unique ID for the frame. The ID for
241     // a frame is guaranteed not to change (instant redraws will use the same
242     // ID). frames[n] has the ID frame_id+n, with the guarantee that frame
243     // drops or reconfigs will keep the guarantee.
244     // The ID is never 0 (unless num_frames==0). IDs are strictly monotonous.
245     uint64_t frame_id;
246 };
247 
248 // Presentation feedback. See get_vsync() for how backends should fill this
249 // struct.
250 struct vo_vsync_info {
251     // mp_time_us() timestamp at which the last queued frame will likely be
252     // displayed (this is in the future, unless the frame is instantly output).
253     // -1 if unset or unsupported.
254     // This implies the latency of the output.
255     int64_t last_queue_display_time;
256 
257     // Time between 2 vsync events in microseconds. The difference should be the
258     // from 2 times sampled from the same reference point (it should not be the
259     // difference between e.g. the end of scanout and the start of the next one;
260     // it must be continuous).
261     // -1 if unsupported.
262     //  0 if supported, but no value available yet. It is assumed that the value
263     //    becomes available after enough swap_buffers() calls were done.
264     // >0 values are taken for granted. Very bad things will happen if it's
265     //    inaccurate.
266     int64_t vsync_duration;
267 
268     // Number of skipped physical vsyncs at some point in time. Typically, this
269     // value is some time in the past by an offset that equals to the latency.
270     // This value is reset and newly sampled at every swap_buffers() call.
271     // This can be used to detect delayed frames iff you try to call
272     // swap_buffers() for every physical vsync.
273     // -1 if unset or unsupported.
274     int64_t skipped_vsyncs;
275 };
276 
277 struct vo_driver {
278     // Encoding functionality, which can be invoked via --o only.
279     bool encode;
280 
281     // This requires waiting for a VO_EVENT_INITIAL_UNBLOCK event before the
282     // first frame can be sent. Doing vo_reconfig*() calls is allowed though.
283     // Encode mode uses this, the core uses vo_is_ready_for_frame() to
284     // implicitly check for this.
285     bool initially_blocked;
286 
287     // VO_CAP_* bits
288     int caps;
289 
290     // Disable video timing, push frames as quickly as possible, never redraw.
291     bool untimed;
292 
293     const char *name;
294     const char *description;
295 
296     /*
297      *   returns: zero on successful initialization, non-zero on error.
298      */
299     int (*preinit)(struct vo *vo);
300 
301     /*
302      * Whether the given image format is supported and config() will succeed.
303      * format: one of IMGFMT_*
304      * returns: 0 on not supported, otherwise 1
305      */
306     int (*query_format)(struct vo *vo, int format);
307 
308     /*
309      * Initialize or reconfigure the display driver.
310      *   params: video parameters, like pixel format and frame size
311      * returns: < 0 on error, >= 0 on success
312      */
313     int (*reconfig)(struct vo *vo, struct mp_image_params *params);
314 
315     /*
316      * Like reconfig(), but provides the whole mp_image for which the change is
317      * required. (The image doesn't have to have real data.)
318      */
319     int (*reconfig2)(struct vo *vo, struct mp_image *img);
320 
321     /*
322      * Control interface
323      */
324     int (*control)(struct vo *vo, uint32_t request, void *data);
325 
326     /*
327      * lavc callback for direct rendering
328      *
329      * Optional. To make implementation easier, the callback is always run on
330      * the VO thread. The returned mp_image's destructor callback is also called
331      * on the VO thread, even if it's actually unref'ed from another thread.
332      *
333      * It is guaranteed that the last reference to an image is destroyed before
334      * ->uninit is called (except it's not - libmpv screenshots can hold the
335      * reference longer, fuck).
336      *
337      * The allocated image - or a part of it, can be passed to draw_frame(). The
338      * point of this mechanism is that the decoder directly renders to GPU
339      * staging memory, to avoid a memcpy on frame upload. But this is not a
340      * guarantee. A filter could change the data pointers or return a newly
341      * allocated image. It's even possible that only 1 plane uses the buffer
342      * allocated by the get_image function. The VO has to check for this.
343      *
344      * stride_align is always a value >=1 that is a power of 2. The stride
345      * values of the returned image must be divisible by this value.
346      *
347      * Currently, the returned image must have exactly 1 AVBufferRef set, for
348      * internal implementation simplicity.
349      *
350      * returns: an allocated, refcounted image; if NULL is returned, the caller
351      * will silently fallback to a default allocator
352      */
353     struct mp_image *(*get_image)(struct vo *vo, int imgfmt, int w, int h,
354                                   int stride_align);
355 
356     /*
357      * Thread-safe variant of get_image. Set at most one of these callbacks.
358      * This excludes _all_ synchronization magic. The only guarantee is that
359      * vo_driver.uninit is not called before this function returns.
360      */
361     struct mp_image *(*get_image_ts)(struct vo *vo, int imgfmt, int w, int h,
362                                      int stride_align);
363 
364     /*
365      * Render the given frame to the VO's backbuffer. This operation will be
366      * followed by a draw_osd and a flip_page[_timed] call.
367      * mpi belongs to the VO; the VO must free it eventually.
368      *
369      * This also should draw the OSD.
370      *
371      * Deprecated for draw_frame. A VO should have only either callback set.
372      */
373     void (*draw_image)(struct vo *vo, struct mp_image *mpi);
374 
375     /* Render the given frame. Note that this is also called when repeating
376      * or redrawing frames.
377      *
378      * frame is freed by the caller, but the callee can still modify the
379      * contained data and references.
380      */
381     void (*draw_frame)(struct vo *vo, struct vo_frame *frame);
382 
383     /*
384      * Blit/Flip buffer to the screen. Must be called after each frame!
385      */
386     void (*flip_page)(struct vo *vo);
387 
388     /*
389      * Return presentation feedback. The implementation should not touch fields
390      * it doesn't support; the info fields are preinitialized to neutral values.
391      * Usually called once after flip_page(), but can be called any time.
392      * The values returned by this are always relative to the last flip_page()
393      * call.
394      */
395     void (*get_vsync)(struct vo *vo, struct vo_vsync_info *info);
396 
397     /* These optional callbacks can be provided if the GUI framework used by
398      * the VO requires entering a message loop for receiving events and does
399      * not call vo_wakeup() from a separate thread when there are new events.
400      *
401      * wait_events() will wait for new events, until the timeout expires, or the
402      * function is interrupted. wakeup() is used to possibly interrupt the
403      * event loop (wakeup() itself must be thread-safe, and not call any other
404      * VO functions; it's the only vo_driver function with this requirement).
405      * wakeup() should behave like a binary semaphore; if wait_events() is not
406      * being called while wakeup() is, the next wait_events() call should exit
407      * immediately.
408      */
409     void (*wakeup)(struct vo *vo);
410     void (*wait_events)(struct vo *vo, int64_t until_time_us);
411 
412     /*
413      * Closes driver. Should restore the original state of the system.
414      */
415     void (*uninit)(struct vo *vo);
416 
417     // Size of private struct for automatic allocation (0 doesn't allocate)
418     int priv_size;
419 
420     // If not NULL, it's copied into the newly allocated private struct.
421     const void *priv_defaults;
422 
423     // List of options to parse into priv struct (requires priv_size to be set)
424     // This will register them as global options (with options_prefix), and
425     // copy the current value at VO creation time to the priv struct.
426     const struct m_option *options;
427 
428     // All options in the above array are prefixed with this string. (It's just
429     // for convenience and makes no difference in semantics.)
430     const char *options_prefix;
431 
432     // Registers global options that go to a separate options struct.
433     const struct m_sub_options *global_opts;
434 };
435 
436 struct vo {
437     const struct vo_driver *driver;
438     struct mp_log *log; // Using e.g. "[vo/vdpau]" as prefix
439     void *priv;
440     struct mpv_global *global;
441     struct vo_x11_state *x11;
442     struct vo_w32_state *w32;
443     struct vo_cocoa_state *cocoa;
444     struct vo_wayland_state *wl;
445     struct vo_android_state *android;
446     struct mp_hwdec_devices *hwdec_devs;
447     struct input_ctx *input_ctx;
448     struct osd_state *osd;
449     struct encode_lavc_context *encode_lavc_ctx;
450     struct vo_internal *in;
451     struct vo_extra extra;
452 
453     // --- The following fields are generally only changed during initialization.
454 
455     bool probing;
456 
457     // --- The following fields are only changed with vo_reconfig(), and can
458     //     be accessed unsynchronized (read-only).
459 
460     int config_ok;      // Last config call was successful?
461     struct mp_image_params *params; // Configured parameters (as in vo_reconfig)
462 
463     // --- The following fields can be accessed only by the VO thread, or from
464     //     anywhere _if_ the VO thread is suspended (use vo->dispatch).
465 
466     struct m_config_cache *opts_cache; // cache for ->opts
467     struct mp_vo_opts *opts;
468     struct m_config_cache *gl_opts_cache;
469     struct m_config_cache *eq_opts_cache;
470 
471     bool want_redraw;   // redraw as soon as possible
472 
473     // current window state
474     int dwidth;
475     int dheight;
476     float monitor_par;
477 };
478 
479 struct mpv_global;
480 struct vo *init_best_video_out(struct mpv_global *global, struct vo_extra *ex);
481 int vo_reconfig(struct vo *vo, struct mp_image_params *p);
482 int vo_reconfig2(struct vo *vo, struct mp_image *img);
483 
484 int vo_control(struct vo *vo, int request, void *data);
485 void vo_control_async(struct vo *vo, int request, void *data);
486 bool vo_is_ready_for_frame(struct vo *vo, int64_t next_pts);
487 void vo_queue_frame(struct vo *vo, struct vo_frame *frame);
488 void vo_wait_frame(struct vo *vo);
489 bool vo_still_displaying(struct vo *vo);
490 void vo_request_wakeup_on_done(struct vo *vo);
491 bool vo_has_frame(struct vo *vo);
492 void vo_redraw(struct vo *vo);
493 bool vo_want_redraw(struct vo *vo);
494 void vo_seek_reset(struct vo *vo);
495 void vo_destroy(struct vo *vo);
496 void vo_set_paused(struct vo *vo, bool paused);
497 int64_t vo_get_drop_count(struct vo *vo);
498 void vo_increment_drop_count(struct vo *vo, int64_t n);
499 int64_t vo_get_delayed_count(struct vo *vo);
500 void vo_query_formats(struct vo *vo, uint8_t *list);
501 void vo_event(struct vo *vo, int event);
502 int vo_query_and_reset_events(struct vo *vo, int events);
503 struct mp_image *vo_get_current_frame(struct vo *vo);
504 void vo_set_queue_params(struct vo *vo, int64_t offset_us, int num_req_frames);
505 int vo_get_num_req_frames(struct vo *vo);
506 int64_t vo_get_vsync_interval(struct vo *vo);
507 double vo_get_estimated_vsync_interval(struct vo *vo);
508 double vo_get_estimated_vsync_jitter(struct vo *vo);
509 double vo_get_display_fps(struct vo *vo);
510 double vo_get_delay(struct vo *vo);
511 void vo_discard_timing_info(struct vo *vo);
512 struct vo_frame *vo_get_current_vo_frame(struct vo *vo);
513 struct mp_image *vo_get_image(struct vo *vo, int imgfmt, int w, int h,
514                               int stride_align);
515 
516 void vo_wakeup(struct vo *vo);
517 void vo_wait_default(struct vo *vo, int64_t until_time);
518 
519 struct mp_keymap {
520   int from;
521   int to;
522 };
523 int lookup_keymap_table(const struct mp_keymap *map, int key);
524 
525 struct mp_osd_res;
526 void vo_get_src_dst_rects(struct vo *vo, struct mp_rect *out_src,
527                           struct mp_rect *out_dst, struct mp_osd_res *out_osd);
528 
529 struct vo_frame *vo_frame_ref(struct vo_frame *frame);
530 
531 #endif /* MPLAYER_VIDEO_OUT_H */
532