xref: /aosp_15_r20/external/virglrenderer/src/virgl_video.h (revision bbecb9d118dfdb95f99bd754f8fa9be01f189df3)
1 /**************************************************************************
2  *
3  * Copyright (C) 2022 Kylin Software Co., Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  **************************************************************************/
24 
25 /**
26  * @file
27  * General video encoding and decoding interface.
28  *
29  * This file provides a general video interface, which mainly contains
30  * two objects:
31  *
32  * virgl_video_buffer:
33  *   Buffer for storing raw YUV formatted data. In VA-API based
34  *   implementations, it is usually associated with a surface.
35  *
36  * virgl_video_codec:
37  *   Represents an encoder or decoder. In VA-API based implementations, it
38  *   usually corresponds to a context.
39  *
40  * @author Feng Jiang <[email protected]>
41  */
42 
43 #ifndef VIRGL_VIDEO_H
44 #define VIRGL_VIDEO_H
45 
46 #include <stdint.h>
47 #include <stdbool.h>
48 
49 #include "pipe/p_format.h"
50 #include "pipe/p_video_enums.h"
51 
52 struct virgl_video_codec;
53 struct virgl_video_buffer;
54 union virgl_caps;
55 union virgl_picture_desc;
56 
57 struct virgl_video_create_codec_args {
58     enum pipe_video_profile profile;
59     enum pipe_video_entrypoint entrypoint;
60     enum pipe_video_chroma_format chroma_format;
61     uint32_t level;
62     uint32_t width;
63     uint32_t height;
64     uint32_t max_references;
65     uint32_t flags;
66     void *opaque;
67 };
68 
69 struct virgl_video_create_buffer_args {
70     enum pipe_format format;
71     uint32_t width;
72     uint32_t height;
73     bool interlaced;
74     void *opaque;
75 };
76 
77 /* flags for virgl_video_dma_buffers */
78 #define VIRGL_VIDEO_DMABUF_READ_ONLY        0x0001
79 #define VIRGL_VIDEO_DMABUF_WRITE_ONLY       0x0002
80 #define VIRGL_VIDEO_DMABUF_READ_WRITE       0x0003
81 
82 struct virgl_video_dma_buf {
83     struct virgl_video_buffer *buf;
84 
85     uint32_t drm_format;
86     uint32_t width;
87     uint32_t height;
88     uint32_t flags;
89 
90     uint32_t num_planes;
91     struct virgl_video_dma_buf_plane {
92         uint32_t drm_format;
93         int fd;
94         uint32_t size;
95         int modifier;
96         uint32_t offset;
97         uint32_t pitch;
98     } planes[4];
99 };
100 
101 /*
102  * Use callback functions instead of directly exporting the video buffer
103  * through an interface like virgl_video_export_buffer() is because the
104  * underlying implementation may not be VA-API. The callback function can
105  * better shield the underlying logic differences.
106  */
107 struct virgl_video_callbacks {
108     /* Callback when decoding is complete, used to download the decoded picture
109      * from the video buffer */
110     void (*decode_completed)(struct virgl_video_codec *codec,
111                              const struct virgl_video_dma_buf *dmabuf);
112 
113     /* Upload the picture data to be encoded to the video buffer */
114     void (*encode_upload_picture)(struct virgl_video_codec *codec,
115                                   const struct virgl_video_dma_buf *dmabuf);
116 
117     /* Callback when encoding is complete, used to download the encoded data
118      * and reference picture */
119     void (*encode_completed)(struct virgl_video_codec *codec,
120                              const struct virgl_video_dma_buf *src_buf,
121                              const struct virgl_video_dma_buf *ref_buf,
122                              unsigned num_coded_bufs,
123                              const void * const *coded_bufs,
124                              const unsigned *coded_sizes);
125 };
126 
127 int virgl_video_init(int drm_fd,
128                      struct virgl_video_callbacks *cbs,
129                      unsigned int flags);
130 void virgl_video_destroy(void);
131 
132 int virgl_video_fill_caps(union virgl_caps *caps);
133 
134 struct virgl_video_codec *virgl_video_create_codec(
135         const struct virgl_video_create_codec_args *args);
136 void virgl_video_destroy_codec(struct virgl_video_codec *codec);
137 uint32_t virgl_video_codec_profile(const struct virgl_video_codec *codec);
138 void *virgl_video_codec_opaque_data(struct virgl_video_codec *codec);
139 
140 struct virgl_video_buffer *virgl_video_create_buffer(
141         const struct virgl_video_create_buffer_args *args);
142 void virgl_video_destroy_buffer(struct virgl_video_buffer *buffer);
143 uint32_t virgl_video_buffer_id(const struct virgl_video_buffer *buffer);
144 void *virgl_video_buffer_opaque_data(struct virgl_video_buffer *buffer);
145 
146 int virgl_video_begin_frame(struct virgl_video_codec *codec,
147                             struct virgl_video_buffer *target);
148 int virgl_video_decode_bitstream(struct virgl_video_codec *codec,
149                                  struct virgl_video_buffer *target,
150                                  const union virgl_picture_desc *desc,
151                                  unsigned num_buffers,
152                                  const void * const *buffers,
153                                  const unsigned *sizes);
154 int virgl_video_encode_bitstream(struct virgl_video_codec *codec,
155                                  struct virgl_video_buffer *source,
156                                  const union virgl_picture_desc *desc);
157 int virgl_video_end_frame(struct virgl_video_codec *codec,
158                           struct virgl_video_buffer *target);
159 
160 #endif /* VIRGL_VIDEO_H */
161 
162