1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*
* Copyright (C) 2024 Mikhail Burakov. This file is part of streamer.
*
* streamer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* streamer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with streamer. If not, see <https://www.gnu.org/licenses/>.
*/
#include "encode_context.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <va/va.h>
#include <va/va_drmcommon.h>
#include <va/va_wayland.h>
#include "io_context.h"
#include "util.h"
struct EncodeContext {
struct IoContext* io_context;
size_t width;
size_t height;
VADisplay display;
VAConfigID config_id;
uint32_t packed_headers;
VAConfigAttribValEncHEVCFeatures hevc_features;
VAConfigAttribValEncHEVCBlockSizes hevc_block_sizes;
};
static const char* VaErrorString(VAStatus error) {
static const char* kVaErrorStrings[] = {
"VA_STATUS_SUCCESS",
"VA_STATUS_ERROR_OPERATION_FAILED",
"VA_STATUS_ERROR_ALLOCATION_FAILED",
"VA_STATUS_ERROR_INVALID_DISPLAY",
"VA_STATUS_ERROR_INVALID_CONFIG",
"VA_STATUS_ERROR_INVALID_CONTEXT",
"VA_STATUS_ERROR_INVALID_SURFACE",
"VA_STATUS_ERROR_INVALID_BUFFER",
"VA_STATUS_ERROR_INVALID_IMAGE",
"VA_STATUS_ERROR_INVALID_SUBPICTURE",
"VA_STATUS_ERROR_ATTR_NOT_SUPPORTED",
"VA_STATUS_ERROR_MAX_NUM_EXCEEDED",
"VA_STATUS_ERROR_UNSUPPORTED_PROFILE",
"VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT",
"VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT",
"VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE",
"VA_STATUS_ERROR_SURFACE_BUSY",
"VA_STATUS_ERROR_FLAG_NOT_SUPPORTED",
"VA_STATUS_ERROR_INVALID_PARAMETER",
"VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED",
"VA_STATUS_ERROR_UNIMPLEMENTED",
"VA_STATUS_ERROR_SURFACE_IN_DISPLAYING",
"VA_STATUS_ERROR_INVALID_IMAGE_FORMAT",
"VA_STATUS_ERROR_DECODING_ERROR",
"VA_STATUS_ERROR_ENCODING_ERROR",
"VA_STATUS_ERROR_INVALID_VALUE",
"???",
"???",
"???",
"???",
"???",
"???",
"VA_STATUS_ERROR_UNSUPPORTED_FILTER",
"VA_STATUS_ERROR_INVALID_FILTER_CHAIN",
"VA_STATUS_ERROR_HW_BUSY",
"???",
"VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE",
"VA_STATUS_ERROR_NOT_ENOUGH_BUFFER",
"VA_STATUS_ERROR_TIMEDOUT",
};
return VA_STATUS_SUCCESS <= error &&
error < VA_STATUS_SUCCESS + (int)LENGTH(kVaErrorStrings)
? kVaErrorStrings[error - VA_STATUS_SUCCESS]
: "???";
}
static bool InitializeCodecCaps(struct EncodeContext* encode_context) {
VAConfigAttrib attrib_list[] = {
{.type = VAConfigAttribEncPackedHeaders},
{.type = VAConfigAttribEncHEVCFeatures},
{.type = VAConfigAttribEncHEVCBlockSizes},
};
VAStatus status = vaGetConfigAttributes(
encode_context->display, VAProfileHEVCMain, VAEntrypointEncSlice,
attrib_list, LENGTH(attrib_list));
if (status != VA_STATUS_SUCCESS) {
LOG("Failed to get va config attributes (%s)", VaErrorString(status));
return false;
}
if (attrib_list[0].value == VA_ATTRIB_NOT_SUPPORTED) {
LOG("VAConfigAttribEncPackedHeaders is not supported");
} else {
LOG("VAConfigAttribEncPackedHeaders is 0x%08x", attrib_list[0].value);
encode_context->packed_headers = attrib_list[0].value;
}
if (attrib_list[1].value == VA_ATTRIB_NOT_SUPPORTED) {
LOG("VAConfigAttribEncHEVCFeatures is not supported");
encode_context->hevc_features = (VAConfigAttribValEncHEVCFeatures){
.bits =
{
// mburakov: ffmpeg hardcodes these for i965 Skylake driver.
.separate_colour_planes = 0, // Table 6-1
.scaling_lists = 0, // No scaling lists
.amp = 1, // hardcoded
.sao = 0, // hardcoded
.pcm = 0, // hardcoded
.temporal_mvp = 0, // hardcoded
.strong_intra_smoothing = 0, // TODO
.dependent_slices = 0, // No slice segments
.sign_data_hiding = 0, // TODO
.constrained_intra_pred = 0, // TODO
.transform_skip = 0, // defaulted
.cu_qp_delta = 0, // Fixed quality
.weighted_prediction = 0, // TODO
.transquant_bypass = 0, // TODO
.deblocking_filter_disable = 0, // TODO
},
};
} else {
LOG("VAConfigAttribEncHEVCFeatures is 0x%08x", attrib_list[1].value);
encode_context->hevc_features.value = attrib_list[1].value;
}
if (attrib_list[2].value == VA_ATTRIB_NOT_SUPPORTED) {
LOG("VAConfigAttribEncHEVCBlockSizes is not supported");
encode_context->hevc_block_sizes = (VAConfigAttribValEncHEVCBlockSizes){
.bits =
{
// mburakov: ffmpeg hardcodes these for i965 Skylake driver.
.log2_max_coding_tree_block_size_minus3 = 2, // hardcoded
.log2_min_coding_tree_block_size_minus3 = 0, // TODO
.log2_min_luma_coding_block_size_minus3 = 0, // hardcoded
.log2_max_luma_transform_block_size_minus2 = 3, // hardcoded
.log2_min_luma_transform_block_size_minus2 = 0, // hardcoded
.max_max_transform_hierarchy_depth_inter = 3, // hardcoded
.min_max_transform_hierarchy_depth_inter = 0, // defaulted
.max_max_transform_hierarchy_depth_intra = 3, // hardcoded
.min_max_transform_hierarchy_depth_intra = 0, // defaulted
.log2_max_pcm_coding_block_size_minus3 = 0, // TODO
.log2_min_pcm_coding_block_size_minus3 = 0, // TODO
},
};
} else {
LOG("VAConfigAttribEncHEVCBlockSizes is 0x%08x", attrib_list[2].value);
encode_context->hevc_block_sizes.value = attrib_list[2].value;
}
return true;
}
struct EncodeContext* EncodeContextCreate(struct IoContext* io_context,
uint32_t width, uint32_t height,
struct wl_display* display) {
LOG("Initializing encoder context for %ux%u resolution", width, height);
struct EncodeContext* encode_context = malloc(sizeof(struct EncodeContext));
if (!encode_context) {
LOG("Failed to allocate encode context (%s)", strerror(errno));
return NULL;
}
*encode_context = (struct EncodeContext){
.io_context = io_context,
.width = width,
.height = height,
.display = vaGetDisplayWl(display),
};
if (!encode_context->display) {
LOG("Failed to get VA display");
goto rollback_encode_context;
}
int major, minor;
VAStatus status = vaInitialize(encode_context->display, &major, &minor);
if (status != VA_STATUS_SUCCESS) {
LOG("Failed to initialize VA (%s)", VaErrorString(status));
goto rollback_display;
}
LOG("Initialized VA %d.%d", major, minor);
VAConfigAttrib attrib_list[] = {
{.type = VAConfigAttribRTFormat, .value = VA_RT_FORMAT_YUV420},
};
status = vaCreateConfig(encode_context->display, VAProfileHEVCMain,
VAEntrypointEncSlice, attrib_list,
LENGTH(attrib_list), &encode_context->config_id);
if (status != VA_STATUS_SUCCESS) {
LOG("Failed to create VA config (%s)", VaErrorString(status));
goto rollback_display;
}
if (!InitializeCodecCaps(encode_context)) {
LOG("Failed to initialize codec caps");
goto rollback_config_id;
}
return encode_context;
rollback_config_id:
assert(vaDestroyConfig(encode_context->display, encode_context->config_id) ==
VA_STATUS_SUCCESS);
rollback_display:
assert(vaTerminate(encode_context->display) == VA_STATUS_SUCCESS);
rollback_encode_context:
free(encode_context);
return NULL;
}
struct EncodeContextFrame* EncodeContextDequeue(
struct EncodeContext* encode_context) {
(void)encode_context;
// TODO(mburakov): Implement this!
return NULL;
}
bool EncodeContextQueue(struct EncodeContext* encode_context,
struct EncodeContextFrame* encode_context_frame,
bool encode) {
(void)encode_context;
(void)encode_context_frame;
(void)encode;
// TODO(mburakov): Implement this!
return true;
}
void EncodeContextDestroy(struct EncodeContext* encode_context) {
assert(vaDestroyConfig(encode_context->display, encode_context->config_id) ==
VA_STATUS_SUCCESS);
assert(vaTerminate(encode_context->display) == VA_STATUS_SUCCESS);
free(encode_context);
}
|