summarylogtreecommitdiffstats
path: root/hevc-vaapi.diff
blob: 1688bedf3df61ad8e6cdffd8fa2b3974a8a8421d (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
diff --git a/plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c b/plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c
index 1598d39db..9b927a9e5 100644
--- a/plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c
+++ b/plugins/obs-ffmpeg/obs-ffmpeg-vaapi.c
@@ -72,10 +72,35 @@ struct vaapi_encoder {
 	bool initialized;
 };
 
-static const char *vaapi_getname(void *unused)
+/* Identify codecs, and some default values */
+struct type_data {
+	const int id;
+	const int profile;
+	const int level;
+	const char *name;
+	const char *rate_control;
+};
+
+static struct type_data h264_type = {
+	.id = AV_CODEC_ID_H264,
+	.profile = FF_PROFILE_H264_CONSTRAINED_BASELINE,
+	.level = 40,
+	.rate_control = "CBR",
+	.name = "FFMPEG VAAPI",
+};
+
+static struct type_data hevc_type = {
+	.id = AV_CODEC_ID_HEVC,
+	.profile = FF_PROFILE_HEVC_MAIN,
+	.level = 120,
+	.rate_control = "CBR",
+	.name = "FFMPEG VAAPI (hevc)",
+};
+
+static const char *vaapi_getname(void *type_data)
 {
-	UNUSED_PARAMETER(unused);
-	return "FFMPEG VAAPI";
+	struct type_data *data = type_data;
+	return data->name;
 }
 
 static inline bool valid_format(enum video_format format)
@@ -173,15 +198,15 @@ typedef struct {
 	bool maxrate;
 } rc_mode_t;
 
+/* Set "allowed" options per Rate Control */
+static const rc_mode_t RC_MODES[] = {
+	{.name = "CBR", .qp = false, .bitrate = true, .maxrate = false},
+	{.name = "CQP", .qp = true, .bitrate = false, .maxrate = false},
+	{.name = "VBR", .qp = false, .bitrate = true, .maxrate = true},
+	NULL};
+
 static const rc_mode_t *get_rc_mode(const char *name)
 {
-	/* Set "allowed" options per Rate Control */
-	static const rc_mode_t RC_MODES[] = {
-		{.name = "CBR", .qp = false, .bitrate = true, .maxrate = false},
-		{.name = "CQP", .qp = true, .bitrate = false, .maxrate = false},
-		{.name = "VBR", .qp = false, .bitrate = true, .maxrate = true},
-		NULL};
-
 	const rc_mode_t *rc_mode = RC_MODES;
 
 	while (!!rc_mode && strcmp(rc_mode->name, name) != 0)
@@ -281,6 +306,7 @@ static bool vaapi_update(void *data, obs_data_t *settings)
 
 	info("settings:\n"
 	     "\tdevice:       %s\n"
+	     "\tcodec:        %s\n"
 	     "\trate_control: %s\n"
 	     "\tprofile:      %d\n"
 	     "\tlevel:        %d\n"
@@ -291,9 +317,9 @@ static bool vaapi_update(void *data, obs_data_t *settings)
 	     "\twidth:        %d\n"
 	     "\theight:       %d\n"
 	     "\tb-frames:     %d\n",
-	     device, rate_control, profile, level, qp, bitrate, maxrate,
-	     enc->context->gop_size, enc->context->width, enc->context->height,
-	     enc->context->max_b_frames);
+	     device, enc->vaapi->name, rate_control, profile, level, qp,
+	     bitrate, maxrate, enc->context->gop_size, enc->context->width,
+	     enc->context->height, enc->context->max_b_frames);
 
 	return vaapi_init_codec(enc, device);
 }
@@ -347,9 +373,12 @@ static void *vaapi_create(obs_data_t *settings, obs_encoder_t *encoder)
 
 	if (vaapi_codec == AV_CODEC_ID_H264) {
 		enc->vaapi = avcodec_find_encoder_by_name("h264_vaapi");
+		enc->first_packet = true;
+	}
+	if (vaapi_codec == AV_CODEC_ID_HEVC) {
+		enc->vaapi = avcodec_find_encoder_by_name("hevc_vaapi");
+		enc->first_packet = false;
 	}
-
-	enc->first_packet = true;
 
 	blog(LOG_INFO, "---------------------------------");
 
@@ -485,7 +514,10 @@ static bool vaapi_encode(void *data, struct encoder_frame *frame,
 		packet->data = enc->buffer.array;
 		packet->size = enc->buffer.num;
 		packet->type = OBS_ENCODER_VIDEO;
-		packet->keyframe = obs_avc_keyframe(packet->data, packet->size);
+		packet->keyframe =
+			enc->vaapi->id == AV_CODEC_ID_H264
+				? obs_avc_keyframe(packet->data, packet->size)
+				: av_pkt.flags & AV_PKT_FLAG_KEY;
 		*received_packet = true;
 	} else {
 		*received_packet = false;
@@ -506,19 +538,20 @@ static void set_visible(obs_properties_t *ppts, const char *name, bool visible)
 	obs_property_set_visible(p, visible);
 }
 
-static void vaapi_defaults(obs_data_t *settings)
+static void vaapi_defaults(obs_data_t *settings, void *type_data)
 {
+	struct type_data *codec = type_data;
+
 	obs_data_set_default_string(settings, "vaapi_device",
 				    "/dev/dri/renderD128");
-	obs_data_set_default_int(settings, "vaapi_codec", AV_CODEC_ID_H264);
-	obs_data_set_default_int(settings, "profile",
-				 FF_PROFILE_H264_CONSTRAINED_BASELINE);
-	obs_data_set_default_int(settings, "level", 40);
+	obs_data_set_default_int(settings, "vaapi_codec", codec->id);
+	obs_data_set_default_int(settings, "profile", codec->profile);
+	obs_data_set_default_int(settings, "level", codec->level);
 	obs_data_set_default_int(settings, "bitrate", 2500);
 	obs_data_set_default_int(settings, "keyint_sec", 0);
 	obs_data_set_default_int(settings, "bf", 0);
-	obs_data_set_default_int(settings, "rendermode", 0);
-	obs_data_set_default_string(settings, "rate_control", "CBR");
+	obs_data_set_default_string(settings, "rate_control",
+				    codec->rate_control);
 	obs_data_set_default_int(settings, "qp", 20);
 	obs_data_set_default_int(settings, "maxrate", 0);
 }
@@ -566,9 +599,13 @@ static bool get_device_name_from_pci(struct pci_access *pacc, char *pci_slot,
 	return false;
 }
 
-static obs_properties_t *vaapi_properties(void *unused)
+static obs_properties_t *vaapi_properties(void *unused, void *type_data)
 {
 	UNUSED_PARAMETER(unused);
+	struct type_data *codec = type_data;
+	struct dstr name;
+
+	dstr_init(&name);
 
 	obs_properties_t *props = obs_properties_create();
 	obs_property_t *list;
@@ -632,40 +669,63 @@ static obs_properties_t *vaapi_properties(void *unused)
 		}
 	}
 
-	list = obs_properties_add_list(props, "vaapi_codec",
-				       obs_module_text("VAAPI.Codec"),
-				       OBS_COMBO_TYPE_LIST,
-				       OBS_COMBO_FORMAT_INT);
-
-	obs_property_list_add_int(list, "H.264 (default)", AV_CODEC_ID_H264);
-
-	list = obs_properties_add_list(props, "profile",
-				       obs_module_text("Profile"),
-				       OBS_COMBO_TYPE_LIST,
-				       OBS_COMBO_FORMAT_INT);
-	obs_property_list_add_int(list, "Constrained Baseline (default)",
-				  FF_PROFILE_H264_CONSTRAINED_BASELINE);
-	obs_property_list_add_int(list, "Main", FF_PROFILE_H264_MAIN);
-	obs_property_list_add_int(list, "High", FF_PROFILE_H264_HIGH);
-
-	list = obs_properties_add_list(props, "level", obs_module_text("Level"),
-				       OBS_COMBO_TYPE_LIST,
-				       OBS_COMBO_FORMAT_INT);
-	obs_property_list_add_int(list, "Auto", FF_LEVEL_UNKNOWN);
-	obs_property_list_add_int(list, "3.0", 30);
-	obs_property_list_add_int(list, "3.1", 31);
-	obs_property_list_add_int(list, "4.0 (default) (Compatibility mode)",
-				  40);
-	obs_property_list_add_int(list, "4.1", 41);
-	obs_property_list_add_int(list, "4.2", 42);
-	obs_property_list_add_int(list, "5.0", 50);
-	obs_property_list_add_int(list, "5.1", 51);
-	obs_property_list_add_int(list, "5.2", 52);
-
-	list = obs_properties_add_list(props, "rate_control",
-				       obs_module_text("RateControl"),
-				       OBS_COMBO_TYPE_LIST,
-				       OBS_COMBO_FORMAT_STRING);
+	if (codec->id == AV_CODEC_ID_H264) {
+		list = obs_properties_add_list(props, "profile",
+						obs_module_text("Profile"),
+						OBS_COMBO_TYPE_LIST,
+						OBS_COMBO_FORMAT_INT);
+		obs_property_list_add_int(list, "Constrained Baseline (default)",
+						FF_PROFILE_H264_CONSTRAINED_BASELINE);
+		obs_property_list_add_int(list, "Main", FF_PROFILE_H264_MAIN);
+		obs_property_list_add_int(list, "High", FF_PROFILE_H264_HIGH);
+
+		list = obs_properties_add_list(props, "level",
+						obs_module_text("Level"),
+						OBS_COMBO_TYPE_LIST,
+						OBS_COMBO_FORMAT_INT);
+		obs_property_list_add_int(list, "Auto", FF_LEVEL_UNKNOWN);
+		obs_property_list_add_int(list, "3.0", 30);
+		obs_property_list_add_int(list, "3.1", 31);
+		obs_property_list_add_int(list, "4.0 (default) (Compatibility mode)",
+						40);
+		obs_property_list_add_int(list, "4.1", 41);
+		obs_property_list_add_int(list, "4.2", 42);
+		obs_property_list_add_int(list, "5.0", 50);
+		obs_property_list_add_int(list, "5.1", 51);
+		obs_property_list_add_int(list, "5.2", 52);
+	} else if (codec->id == AV_CODEC_ID_HEVC) {
+		list = obs_properties_add_list(props, "profile",
+						obs_module_text("Profile"),
+						OBS_COMBO_TYPE_LIST,
+						OBS_COMBO_FORMAT_INT);
+		obs_property_list_add_int(list, "Main",	FF_PROFILE_HEVC_MAIN);
+		obs_property_list_add_int(list, "Main10", FF_PROFILE_HEVC_MAIN_10);
+		obs_property_list_add_int(list, "Rext", FF_PROFILE_HEVC_REXT);
+
+		list = obs_properties_add_list(props, "level",
+						obs_module_text("Level"),
+						OBS_COMBO_TYPE_LIST,
+						OBS_COMBO_FORMAT_INT);
+		obs_property_list_add_int(list, "Auto", FF_LEVEL_UNKNOWN);
+		obs_property_list_add_int(list, "1", 30);
+		obs_property_list_add_int(list, "2", 60);
+		obs_property_list_add_int(list, "2.1", 63);
+		obs_property_list_add_int(list, "3", 90);
+		obs_property_list_add_int(list, "3.1", 93);
+		obs_property_list_add_int(list, "4", 120);
+		obs_property_list_add_int(list, "4.1", 123);
+		obs_property_list_add_int(list, "5", 150);
+		obs_property_list_add_int(list, "5.1", 153);
+		obs_property_list_add_int(list, "5.2", 156);
+		obs_property_list_add_int(list, "6", 180);
+		obs_property_list_add_int(list, "6.1", 183);
+		obs_property_list_add_int(list, "6.2", 186);
+	}
+ 
+ 	list = obs_properties_add_list(props, "rate_control",
+ 				       obs_module_text("RateControl"),
+ 				       OBS_COMBO_TYPE_LIST,
+ 				       OBS_COMBO_FORMAT_STRING);
 	obs_property_list_add_string(list, "CBR (default)", "CBR");
 	obs_property_list_add_string(list, "CQP", "CQP");
 	obs_property_list_add_string(list, "VBR", "VBR");
@@ -687,6 +747,7 @@ static obs_properties_t *vaapi_properties(void *unused)
 			       obs_module_text("KeyframeIntervalSec"), 0, 20,
 			       1);
 
+	dstr_free(&name);
 	return props;
 }
 
@@ -716,11 +777,28 @@ struct obs_encoder_info vaapi_encoder_info = {
 	.create = vaapi_create,
 	.destroy = vaapi_destroy,
 	.encode = vaapi_encode,
-	.get_defaults = vaapi_defaults,
-	.get_properties = vaapi_properties,
+	.get_defaults2 = vaapi_defaults,
+	.get_properties2 = vaapi_properties,
+	.get_extra_data = vaapi_extra_data,
+	.get_sei_data = vaapi_sei_data,
+	.get_video_info = vaapi_video_info,
+	.type_data = &h264_type,
+};
+
+struct obs_encoder_info vaapi_hevc_encoder_info = {
+	.id = "ffmpeg_vaapi_hevc",
+	.type = OBS_ENCODER_VIDEO,
+	.codec = "hevc",
+	.get_name = vaapi_getname,
+	.create = vaapi_create,
+	.destroy = vaapi_destroy,
+	.encode = vaapi_encode,
+	.get_defaults2 = vaapi_defaults,
+	.get_properties2 = vaapi_properties,
 	.get_extra_data = vaapi_extra_data,
 	.get_sei_data = vaapi_sei_data,
 	.get_video_info = vaapi_video_info,
+	.type_data = &hevc_type,
 };
 
 #endif
diff --git a/plugins/obs-ffmpeg/obs-ffmpeg.c b/plugins/obs-ffmpeg/obs-ffmpeg.c
index b25201572..84594f2b6 100644
--- a/plugins/obs-ffmpeg/obs-ffmpeg.c
+++ b/plugins/obs-ffmpeg/obs-ffmpeg.c
@@ -37,6 +37,7 @@ extern struct obs_encoder_info aom_av1_encoder_info;
 
 #ifdef LIBAVUTIL_VAAPI_AVAILABLE
 extern struct obs_encoder_info vaapi_encoder_info;
+extern struct obs_encoder_info vaapi_hevc_encoder_info;
 #endif
 
 #ifndef __APPLE__
@@ -215,9 +216,9 @@ finish:
 #endif
 
 #ifdef LIBAVUTIL_VAAPI_AVAILABLE
-static bool vaapi_supported(void)
+static bool vaapi_supported(const char *codec_name)
 {
-	AVCodec *vaenc = avcodec_find_encoder_by_name("h264_vaapi");
+	AVCodec *vaenc = avcodec_find_encoder_by_name(codec_name);
 	return !!vaenc;
 }
 #endif
@@ -269,9 +270,13 @@ bool obs_module_load(void)
 		obs_register_encoder(&nvenc_encoder_info);
 	}
 #if !defined(_WIN32) && defined(LIBAVUTIL_VAAPI_AVAILABLE)
-	if (vaapi_supported()) {
-		blog(LOG_INFO, "FFMPEG VAAPI supported");
-		obs_register_encoder(&vaapi_encoder_info);
+	if (vaapi_supported("h264_vaapi")) {
+ 		blog(LOG_INFO, "FFMPEG VAAPI supported");
+ 		obs_register_encoder(&vaapi_encoder_info);
+ 	}
+	if (vaapi_supported("hevc_vaapi")) {
+		blog(LOG_INFO, "FFMPEG HEVC VAAPI supported");
+		obs_register_encoder(&vaapi_hevc_encoder_info);
 	}
 #endif
 #endif