1 /*
2 * Copyright © 2020, VideoLAN and dav1d authors
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <inttypes.h>
28 #include <string.h>
29
30 #include "dav1d/dav1d.h"
31
32 #include <SDL.h>
33 #if HAVE_PLACEBO
34 # include <libplacebo/config.h>
35 #endif
36
37 // Check libplacebo Vulkan rendering
38 #if HAVE_VULKAN && defined(SDL_VIDEO_VULKAN)
39 # if defined(PL_HAVE_VULKAN) && PL_HAVE_VULKAN
40 # define HAVE_RENDERER_PLACEBO 1
41 # define HAVE_PLACEBO_VULKAN 1
42 # endif
43 #endif
44
45 // Check libplacebo OpenGL rendering
46 #if defined(PL_HAVE_OPENGL) && PL_HAVE_OPENGL
47 # define HAVE_RENDERER_PLACEBO 1
48 # define HAVE_PLACEBO_OPENGL 1
49 #endif
50
51 #ifndef HAVE_RENDERER_PLACEBO
52 #define HAVE_RENDERER_PLACEBO 0
53 #endif
54 #ifndef HAVE_PLACEBO_VULKAN
55 #define HAVE_PLACEBO_VULKAN 0
56 #endif
57 #ifndef HAVE_PLACEBO_OPENGL
58 #define HAVE_PLACEBO_OPENGL 0
59 #endif
60
61 /**
62 * Settings structure
63 * Hold all settings available for the player,
64 * this is usually filled by parsing arguments
65 * from the console.
66 */
67 typedef struct {
68 const char *inputfile;
69 const char *renderer_name;
70 int highquality;
71 int untimed;
72 int zerocopy;
73 int gpugrain;
74 int fullscreen;
75 } Dav1dPlaySettings;
76
77 #define WINDOW_WIDTH 910
78 #define WINDOW_HEIGHT 512
79
80 enum {
81 DAV1D_EVENT_NEW_FRAME,
82 DAV1D_EVENT_SEEK_FRAME,
83 DAV1D_EVENT_DEC_QUIT
84 };
85
86 /**
87 * Renderer info
88 */
89 typedef struct rdr_info
90 {
91 // Renderer name
92 const char *name;
93 // Cookie passed to the renderer implementation callbacks
94 void *cookie;
95 // Callback to create the renderer
96 void* (*create_renderer)(const Dav1dPlaySettings *settings);
97 // Callback to destroy the renderer
98 void (*destroy_renderer)(void *cookie);
99 // Callback to the render function that renders a prevously sent frame
100 void (*render)(void *cookie, const Dav1dPlaySettings *settings);
101 // Callback to the send frame function, _may_ also unref dav1d_pic!
102 int (*update_frame)(void *cookie, Dav1dPicture *dav1d_pic,
103 const Dav1dPlaySettings *settings);
104 // Callback for alloc/release pictures (optional)
105 int (*alloc_pic)(Dav1dPicture *pic, void *cookie);
106 void (*release_pic)(Dav1dPicture *pic, void *cookie);
107 // Whether or not this renderer can apply on-GPU film grain synthesis
108 int supports_gpu_grain;
109 } Dav1dPlayRenderInfo;
110
111 extern const Dav1dPlayRenderInfo rdr_placebo_vk;
112 extern const Dav1dPlayRenderInfo rdr_placebo_gl;
113 extern const Dav1dPlayRenderInfo rdr_sdl;
114
115 // Available renderes ordered by priority
116 static const Dav1dPlayRenderInfo* const dp_renderers[] = {
117 &rdr_placebo_vk,
118 &rdr_placebo_gl,
119 &rdr_sdl,
120 };
121
dp_get_renderer(const char * name)122 static inline const Dav1dPlayRenderInfo *dp_get_renderer(const char *name)
123 {
124 for (size_t i = 0; i < (sizeof(dp_renderers)/sizeof(*dp_renderers)); ++i)
125 {
126 if (dp_renderers[i]->name == NULL)
127 continue;
128
129 if (name == NULL || strcmp(name, dp_renderers[i]->name) == 0) {
130 return dp_renderers[i];
131 }
132 }
133 return NULL;
134 }
135
dp_create_sdl_window(int window_flags)136 static inline SDL_Window *dp_create_sdl_window(int window_flags)
137 {
138 SDL_Window *win;
139 window_flags |= SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI;
140
141 win = SDL_CreateWindow("Dav1dPlay", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
142 WINDOW_WIDTH, WINDOW_HEIGHT, window_flags);
143 SDL_SetWindowResizable(win, SDL_TRUE);
144
145 return win;
146 }
147