-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathobs-qsv11.c
1422 lines (1164 loc) · 43 KB
/
obs-qsv11.c
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is provided under a dual BSD/GPLv2 license. When using or
redistributing this file, you may do so under either license.
GPL LICENSE SUMMARY
Copyright(c) Oct. 2015 Intel Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program 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.
Contact Information:
Seung-Woo Kim, seung-woo.kim@intel.com
705 5th Ave S #500, Seattle, WA 98104
BSD LICENSE
Copyright(c) <date> Intel Corporation.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <inttypes.h>
#include <util/dstr.h>
#include <util/darray.h>
#include <util/platform.h>
#include <util/threading.h>
#include <obs-module.h>
#include <obs-hevc.h>
#include <obs-avc.h>
#include "QSV_Encoder.h"
#include "common_utils.h"
#define do_log(level, format, ...) \
blog(level, "[qsv encoder: '%s'] " format, obs_encoder_get_name(obsqsv->encoder), ##__VA_ARGS__)
#define error(format, ...) do_log(LOG_ERROR, format, ##__VA_ARGS__)
#define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
#define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
#define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
#ifndef GS_INVALID_HANDLE
#define GS_INVALID_HANDLE (uint32_t) - 1
#endif
/* ------------------------------------------------------------------------- */
struct obs_qsv {
obs_encoder_t *encoder;
enum qsv_codec codec;
qsv_param_t params;
qsv_t *context;
DARRAY(uint8_t) packet_data;
uint8_t *extra_data;
uint8_t *sei;
size_t extra_data_size;
size_t sei_size;
os_performance_token_t *performance_token;
uint32_t roi_increment;
};
/* ------------------------------------------------------------------------- */
static pthread_mutex_t g_QsvLock = PTHREAD_MUTEX_INITIALIZER;
static unsigned short g_verMajor;
static unsigned short g_verMinor;
static const char *obs_qsv_getname_v1(void *type_data)
{
UNUSED_PARAMETER(type_data);
return "QuickSync H.264 (v1 deprecated)";
}
static const char *obs_qsv_getname(void *type_data)
{
UNUSED_PARAMETER(type_data);
return "QuickSync H.264";
}
static const char *obs_qsv_getname_av1(void *type_data)
{
UNUSED_PARAMETER(type_data);
return "QuickSync AV1";
}
static const char *obs_qsv_getname_hevc(void *type_data)
{
UNUSED_PARAMETER(type_data);
return "QuickSync HEVC";
}
static void clear_data(struct obs_qsv *obsqsv)
{
if (obsqsv->context) {
pthread_mutex_lock(&g_QsvLock);
qsv_encoder_close(obsqsv->context);
obsqsv->context = NULL;
pthread_mutex_unlock(&g_QsvLock);
// bfree(obsqsv->sei);
bfree(obsqsv->extra_data);
// obsqsv->sei = NULL;
obsqsv->extra_data = NULL;
}
}
static void obs_qsv_destroy(void *data)
{
struct obs_qsv *obsqsv = (struct obs_qsv *)data;
if (obsqsv) {
os_end_high_performance(obsqsv->performance_token);
clear_data(obsqsv);
da_free(obsqsv->packet_data);
bfree(obsqsv);
}
}
static void obs_qsv_defaults(obs_data_t *settings, int ver, enum qsv_codec codec)
{
obs_data_set_default_string(settings, "target_usage", "TU4");
obs_data_set_default_int(settings, "bitrate", 2500);
obs_data_set_default_int(settings, "max_bitrate", 3000);
obs_data_set_default_string(settings, "profile", codec == QSV_CODEC_AVC ? "high" : "main");
obs_data_set_default_string(settings, "rate_control", "CBR");
obs_data_set_default_int(settings, "__ver", ver);
obs_data_set_default_int(settings, "cqp", 23);
obs_data_set_default_int(settings, "qpi", 23);
obs_data_set_default_int(settings, "qpp", 23);
obs_data_set_default_int(settings, "qpb", 23);
obs_data_set_default_int(settings, "icq_quality", 23);
obs_data_set_default_int(settings, "keyint_sec", 0);
obs_data_set_default_string(settings, "latency", "normal");
obs_data_set_default_int(settings, "bframes", 3);
obs_data_set_default_bool(settings, "repeat_headers", false);
}
static void obs_qsv_defaults_h264_v1(obs_data_t *settings)
{
obs_qsv_defaults(settings, 1, QSV_CODEC_AVC);
}
static void obs_qsv_defaults_h264_v2(obs_data_t *settings)
{
obs_qsv_defaults(settings, 2, QSV_CODEC_AVC);
}
static void obs_qsv_defaults_av1(obs_data_t *settings)
{
obs_qsv_defaults(settings, 2, QSV_CODEC_AV1);
}
static void obs_qsv_defaults_hevc(obs_data_t *settings)
{
obs_qsv_defaults(settings, 2, QSV_CODEC_HEVC);
}
static inline void add_strings(obs_property_t *list, const char *const *strings)
{
while (*strings) {
obs_property_list_add_string(list, *strings, *strings);
strings++;
}
}
static inline void add_translated_strings(obs_property_t *list, const char *const *keys, const char *const *strings)
{
while (*keys && *strings) {
obs_property_list_add_string(list, obs_module_text(*keys), *strings);
keys++;
strings++;
}
}
#define TEXT_SPEED obs_module_text("TargetUsage")
#define TEXT_TARGET_BITRATE obs_module_text("Bitrate")
#define TEXT_MAX_BITRATE obs_module_text("MaxBitrate")
#define TEXT_PROFILE obs_module_text("Profile")
#define TEXT_LATENCY obs_module_text("Latency")
#define TEXT_RATE_CONTROL obs_module_text("RateControl")
#define TEXT_ICQ_QUALITY obs_module_text("ICQQuality")
#define TEXT_KEYINT_SEC obs_module_text("KeyframeIntervalSec")
#define TEXT_BFRAMES obs_module_text("BFrames")
static bool update_latency(obs_data_t *settings)
{
bool update = false;
int async_depth = 4;
if (obs_data_item_byname(settings, "async_depth") != NULL) {
async_depth = (int)obs_data_get_int(settings, "async_depth");
obs_data_erase(settings, "async_depth");
update = true;
}
int la_depth = 15;
if (obs_data_item_byname(settings, "la_depth") != NULL) {
la_depth = (int)obs_data_get_int(settings, "la_depth");
obs_data_erase(settings, "la_depth");
update = true;
}
const char *rate_control = obs_data_get_string(settings, "rate_control");
bool lookahead = false;
if (astrcmpi(rate_control, "LA_CBR") == 0) {
obs_data_set_string(settings, "rate_control", "CBR");
lookahead = true;
} else if (astrcmpi(rate_control, "LA_VBR") == 0) {
obs_data_set_string(settings, "rate_control", "VBR");
lookahead = true;
} else if (astrcmpi(rate_control, "LA_ICQ") == 0) {
obs_data_set_string(settings, "rate_control", "ICQ");
lookahead = true;
}
if (update) {
if (lookahead) {
if (la_depth == 0 || la_depth >= 15)
obs_data_set_string(settings, "latency", "normal");
else
obs_data_set_string(settings, "latency", "low");
} else {
if (async_depth != 1)
obs_data_set_string(settings, "latency", "normal");
else
obs_data_set_string(settings, "latency", "ultra-low");
}
}
return true;
}
static bool update_ratecontrol(obs_data_t *settings)
{
const char *rate_control = obs_data_get_string(settings, "rate_control");
if (astrcmpi(rate_control, "VCM") == 0) {
obs_data_set_string(settings, "rate_control", "CBR");
} else if (astrcmpi(rate_control, "AVBR") == 0) {
obs_data_set_string(settings, "rate_control", "VBR");
}
return true;
}
static void update_targetusage(obs_data_t *settings)
{
const char *target_usage = obs_data_get_string(settings, "target_usage");
if (astrcmpi(target_usage, "veryslow") == 0 || astrcmpi(target_usage, "quality") == 0)
obs_data_set_string(settings, "target_usage", "TU1");
else if (astrcmpi(target_usage, "slower") == 0)
obs_data_set_string(settings, "target_usage", "TU2");
else if (astrcmpi(target_usage, "slow") == 0)
obs_data_set_string(settings, "target_usage", "TU3");
else if (astrcmpi(target_usage, "medium") == 0 || astrcmpi(target_usage, "balanced") == 0)
obs_data_set_string(settings, "target_usage", "TU4");
else if (astrcmpi(target_usage, "fast") == 0)
obs_data_set_string(settings, "target_usage", "TU5");
else if (astrcmpi(target_usage, "faster") == 0)
obs_data_set_string(settings, "target_usage", "TU6");
else if (astrcmpi(target_usage, "veryfast") == 0 || astrcmpi(target_usage, "speed") == 0)
obs_data_set_string(settings, "target_usage", "TU7");
}
static bool rate_control_modified(obs_properties_t *ppts, obs_property_t *p, obs_data_t *settings)
{
const char *rate_control = obs_data_get_string(settings, "rate_control");
bool bVisible = astrcmpi(rate_control, "VBR") == 0;
p = obs_properties_get(ppts, "max_bitrate");
obs_property_set_visible(p, bVisible);
bVisible = astrcmpi(rate_control, "CQP") == 0 || astrcmpi(rate_control, "ICQ") == 0;
p = obs_properties_get(ppts, "bitrate");
obs_property_set_visible(p, !bVisible);
bVisible = astrcmpi(rate_control, "CQP") == 0;
p = obs_properties_get(ppts, "qpi");
if (p)
obs_property_set_visible(p, bVisible);
p = obs_properties_get(ppts, "qpb");
if (p)
obs_property_set_visible(p, bVisible);
p = obs_properties_get(ppts, "qpp");
if (p)
obs_property_set_visible(p, bVisible);
p = obs_properties_get(ppts, "cqp");
if (p)
obs_property_set_visible(p, bVisible);
bVisible = astrcmpi(rate_control, "ICQ") == 0;
p = obs_properties_get(ppts, "icq_quality");
obs_property_set_visible(p, bVisible);
update_latency(settings);
update_targetusage(settings);
update_ratecontrol(settings);
return true;
}
static inline void add_rate_controls(obs_property_t *list, const struct qsv_rate_control_info *rc)
{
enum qsv_cpu_platform plat = qsv_get_cpu_platform();
while (rc->name) {
if (!rc->haswell_or_greater || (plat >= QSV_CPU_PLATFORM_HSW || plat == QSV_CPU_PLATFORM_UNKNOWN))
obs_property_list_add_string(list, rc->name, rc->name);
rc++;
}
}
static obs_properties_t *obs_qsv_props(enum qsv_codec codec, void *unused, int ver)
{
UNUSED_PARAMETER(unused);
obs_properties_t *props = obs_properties_create();
obs_property_t *prop;
prop = obs_properties_add_list(props, "rate_control", TEXT_RATE_CONTROL, OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
add_rate_controls(prop, qsv_ratecontrols);
obs_property_set_modified_callback(prop, rate_control_modified);
prop = obs_properties_add_int(props, "bitrate", TEXT_TARGET_BITRATE, 50, 10000000, 50);
obs_property_int_set_suffix(prop, " Kbps");
prop = obs_properties_add_int(props, "max_bitrate", TEXT_MAX_BITRATE, 50, 10000000, 50);
obs_property_int_set_suffix(prop, " Kbps");
if (ver >= 2) {
obs_properties_add_int(props, "cqp", "CQP", 1, codec == QSV_CODEC_AV1 ? 63 : 51, 1);
} else {
obs_properties_add_int(props, "qpi", "QPI", 1, 51, 1);
obs_properties_add_int(props, "qpp", "QPP", 1, 51, 1);
obs_properties_add_int(props, "qpb", "QPB", 1, 51, 1);
}
obs_properties_add_int(props, "icq_quality", TEXT_ICQ_QUALITY, 1, 51, 1);
prop = obs_properties_add_list(props, "target_usage", TEXT_SPEED, OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
add_translated_strings(prop, qsv_usage_translation_keys, qsv_usage_names);
prop = obs_properties_add_list(props, "profile", TEXT_PROFILE, OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
if (codec == QSV_CODEC_AVC)
add_strings(prop, qsv_profile_names);
else if (codec == QSV_CODEC_AV1)
add_strings(prop, qsv_profile_names_av1);
else if (codec == QSV_CODEC_HEVC)
add_strings(prop, qsv_profile_names_hevc);
prop = obs_properties_add_int(props, "keyint_sec", TEXT_KEYINT_SEC, 0, 20, 1);
obs_property_int_set_suffix(prop, " s");
prop = obs_properties_add_list(props, "latency", TEXT_LATENCY, OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
add_strings(prop, qsv_latency_names);
obs_property_set_long_description(prop, obs_module_text("Latency.ToolTip"));
obs_properties_add_int(props, "bframes", TEXT_BFRAMES, 0, 3, 1);
return props;
}
static obs_properties_t *obs_qsv_props_h264(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_qsv_props(QSV_CODEC_AVC, unused, 1);
}
static obs_properties_t *obs_qsv_props_h264_v2(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_qsv_props(QSV_CODEC_AVC, unused, 2);
}
static obs_properties_t *obs_qsv_props_av1(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_qsv_props(QSV_CODEC_AV1, unused, 2);
}
static obs_properties_t *obs_qsv_props_hevc(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_qsv_props(QSV_CODEC_HEVC, unused, 2);
}
static void update_params(struct obs_qsv *obsqsv, obs_data_t *settings)
{
video_t *video = obs_encoder_video(obsqsv->encoder);
const struct video_output_info *voi = video_output_get_info(video);
update_latency(settings);
update_targetusage(settings);
const char *target_usage = obs_data_get_string(settings, "target_usage");
const char *profile = obs_data_get_string(settings, "profile");
const char *rate_control = obs_data_get_string(settings, "rate_control");
const char *latency = obs_data_get_string(settings, "latency");
int target_bitrate = (int)obs_data_get_int(settings, "bitrate");
int max_bitrate = (int)obs_data_get_int(settings, "max_bitrate");
int qpi = (int)obs_data_get_int(settings, "qpi");
int qpp = (int)obs_data_get_int(settings, "qpp");
int qpb = (int)obs_data_get_int(settings, "qpb");
int cqp = (int)obs_data_get_int(settings, "cqp");
int ver = (int)obs_data_get_int(settings, "__ver");
int icq_quality = (int)obs_data_get_int(settings, "icq_quality");
int keyint_sec = (int)obs_data_get_int(settings, "keyint_sec");
bool cbr_override = obs_data_get_bool(settings, "cbr");
int bFrames = (int)obs_data_get_int(settings, "bframes");
bool repeat_headers = obs_data_get_bool(settings, "repeat_headers");
const char *codec = "";
if (obs_data_has_user_value(settings, "bf"))
bFrames = (int)obs_data_get_int(settings, "bf");
enum qsv_cpu_platform plat = qsv_get_cpu_platform();
if (plat == QSV_CPU_PLATFORM_IVB || plat == QSV_CPU_PLATFORM_SNB)
bFrames = 0;
int width = (int)obs_encoder_get_width(obsqsv->encoder);
int height = (int)obs_encoder_get_height(obsqsv->encoder);
if (astrcmpi(target_usage, "TU1") == 0)
obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_BEST_QUALITY;
else if (astrcmpi(target_usage, "TU4") == 0)
obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_BALANCED;
else if (astrcmpi(target_usage, "TU7") == 0)
obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_BEST_SPEED;
else if (astrcmpi(target_usage, "TU2") == 0)
obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_2;
else if (astrcmpi(target_usage, "TU3") == 0)
obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_3;
else if (astrcmpi(target_usage, "TU5") == 0)
obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_5;
else if (astrcmpi(target_usage, "TU6") == 0)
obsqsv->params.nTargetUsage = MFX_TARGETUSAGE_6;
if (obsqsv->codec == QSV_CODEC_AVC) {
codec = "H.264";
if (astrcmpi(profile, "baseline") == 0)
obsqsv->params.nCodecProfile = MFX_PROFILE_AVC_BASELINE;
else if (astrcmpi(profile, "main") == 0)
obsqsv->params.nCodecProfile = MFX_PROFILE_AVC_MAIN;
else if (astrcmpi(profile, "high") == 0)
obsqsv->params.nCodecProfile = MFX_PROFILE_AVC_HIGH;
} else if (obsqsv->codec == QSV_CODEC_HEVC) {
codec = "HEVC";
if (astrcmpi(profile, "main") == 0) {
obsqsv->params.nCodecProfile = MFX_PROFILE_HEVC_MAIN;
if (obs_encoder_video_tex_active(obsqsv->encoder, VIDEO_FORMAT_P010)) {
blog(LOG_WARNING, "[qsv encoder] Forcing main10 for P010");
obsqsv->params.nCodecProfile = MFX_PROFILE_HEVC_MAIN10;
}
} else if (astrcmpi(profile, "main10") == 0) {
obsqsv->params.nCodecProfile = MFX_PROFILE_HEVC_MAIN10;
}
} else if (obsqsv->codec == QSV_CODEC_AV1) {
codec = "AV1";
obsqsv->params.nCodecProfile = MFX_PROFILE_AV1_MAIN;
}
obsqsv->params.VideoFormat = 5;
obsqsv->params.VideoFullRange = voi->range == VIDEO_RANGE_FULL;
switch (voi->colorspace) {
case VIDEO_CS_601:
obsqsv->params.ColourPrimaries = 6;
obsqsv->params.TransferCharacteristics = 6;
obsqsv->params.MatrixCoefficients = 6;
obsqsv->params.ChromaSampleLocTypeTopField = 0;
obsqsv->params.ChromaSampleLocTypeBottomField = 0;
break;
case VIDEO_CS_DEFAULT:
case VIDEO_CS_709:
obsqsv->params.ColourPrimaries = 1;
obsqsv->params.TransferCharacteristics = 1;
obsqsv->params.MatrixCoefficients = 1;
obsqsv->params.ChromaSampleLocTypeTopField = 0;
obsqsv->params.ChromaSampleLocTypeBottomField = 0;
break;
case VIDEO_CS_SRGB:
obsqsv->params.ColourPrimaries = 1;
obsqsv->params.TransferCharacteristics = 13;
obsqsv->params.MatrixCoefficients = 1;
obsqsv->params.ChromaSampleLocTypeTopField = 0;
obsqsv->params.ChromaSampleLocTypeBottomField = 0;
break;
case VIDEO_CS_2100_PQ:
obsqsv->params.ColourPrimaries = 9;
obsqsv->params.TransferCharacteristics = 16;
obsqsv->params.MatrixCoefficients = 9;
obsqsv->params.ChromaSampleLocTypeTopField = 2;
obsqsv->params.ChromaSampleLocTypeBottomField = 2;
break;
case VIDEO_CS_2100_HLG:
obsqsv->params.ColourPrimaries = 9;
obsqsv->params.TransferCharacteristics = 18;
obsqsv->params.MatrixCoefficients = 9;
obsqsv->params.ChromaSampleLocTypeTopField = 2;
obsqsv->params.ChromaSampleLocTypeBottomField = 2;
}
const bool pq = voi->colorspace == VIDEO_CS_2100_PQ;
const bool hlg = voi->colorspace == VIDEO_CS_2100_HLG;
if (pq || hlg) {
const int hdr_nominal_peak_level = pq ? (int)obs_get_video_hdr_nominal_peak_level() : (hlg ? 1000 : 0);
obsqsv->params.DisplayPrimariesX[0] = 13250;
obsqsv->params.DisplayPrimariesX[1] = 7500;
obsqsv->params.DisplayPrimariesX[2] = 34000;
obsqsv->params.DisplayPrimariesY[0] = 34500;
obsqsv->params.DisplayPrimariesY[1] = 3000;
obsqsv->params.DisplayPrimariesY[2] = 16000;
obsqsv->params.WhitePointX = 15635;
obsqsv->params.WhitePointY = 16450;
obsqsv->params.MaxDisplayMasteringLuminance = hdr_nominal_peak_level * 10000;
obsqsv->params.MinDisplayMasteringLuminance = 0;
obsqsv->params.MaxContentLightLevel = hdr_nominal_peak_level;
obsqsv->params.MaxPicAverageLightLevel = hdr_nominal_peak_level;
}
/* internal convenience parameter, overrides rate control param
* XXX: Deprecated */
if (cbr_override) {
warn("\"cbr\" setting has been deprecated for all encoders! "
"Please set \"rate_control\" to \"CBR\" instead. "
"Forcing CBR mode. "
"(Note to all: this is why you shouldn't use strings for "
"common settings)");
rate_control = "CBR";
}
if (astrcmpi(rate_control, "CBR") == 0)
obsqsv->params.nRateControl = MFX_RATECONTROL_CBR;
else if (astrcmpi(rate_control, "VBR") == 0)
obsqsv->params.nRateControl = MFX_RATECONTROL_VBR;
else if (astrcmpi(rate_control, "CQP") == 0)
obsqsv->params.nRateControl = MFX_RATECONTROL_CQP;
else if (astrcmpi(rate_control, "ICQ") == 0)
obsqsv->params.nRateControl = MFX_RATECONTROL_ICQ;
obsqsv->params.nLADEPTH = (mfxU16)0;
if (astrcmpi(latency, "ultra-low") == 0) {
obsqsv->params.nAsyncDepth = 1;
} else if (astrcmpi(latency, "low") == 0) {
obsqsv->params.nAsyncDepth = 4;
if (obsqsv->params.nRateControl == MFX_RATECONTROL_CBR ||
obsqsv->params.nRateControl == MFX_RATECONTROL_VBR)
obsqsv->params.nLADEPTH = 30;
} else if (astrcmpi(latency, "normal") == 0) {
obsqsv->params.nAsyncDepth = 4;
if (obsqsv->params.nRateControl == MFX_RATECONTROL_CBR ||
obsqsv->params.nRateControl == MFX_RATECONTROL_VBR)
obsqsv->params.nLADEPTH = 60;
}
if (obsqsv->params.nLADEPTH > 0) {
if (obsqsv->params.nLADEPTH > 100)
obsqsv->params.nLADEPTH = 100;
else if (obsqsv->params.nLADEPTH < 10)
obsqsv->params.nLADEPTH = 10;
}
if (ver == 1) {
obsqsv->params.nQPI = (mfxU16)qpi;
obsqsv->params.nQPP = (mfxU16)qpp;
obsqsv->params.nQPB = (mfxU16)qpb;
} else {
int actual_cqp = cqp;
if (obsqsv->codec == QSV_CODEC_AV1)
actual_cqp *= 4;
obsqsv->params.nQPI = actual_cqp;
obsqsv->params.nQPP = actual_cqp;
obsqsv->params.nQPB = actual_cqp;
}
obsqsv->params.nTargetBitRate = (mfxU16)target_bitrate;
obsqsv->params.nMaxBitRate = (mfxU16)max_bitrate;
obsqsv->params.nWidth = (mfxU16)width;
obsqsv->params.nHeight = (mfxU16)height;
obsqsv->params.nFpsNum = (mfxU16)voi->fps_num;
obsqsv->params.nFpsDen = (mfxU16)voi->fps_den;
obsqsv->params.nbFrames = (mfxU16)bFrames;
obsqsv->params.nKeyIntSec = (mfxU16)keyint_sec;
obsqsv->params.nICQQuality = (mfxU16)icq_quality;
obsqsv->params.bRepeatHeaders = repeat_headers;
info("settings:\n"
"\tcodec: %s\n"
"\trate_control: %s",
codec, rate_control);
if (obsqsv->params.nRateControl != MFX_RATECONTROL_ICQ && obsqsv->params.nRateControl != MFX_RATECONTROL_CQP)
blog(LOG_INFO, "\ttarget_bitrate: %d", (int)obsqsv->params.nTargetBitRate);
if (obsqsv->params.nRateControl == MFX_RATECONTROL_VBR)
blog(LOG_INFO, "\tmax_bitrate: %d", (int)obsqsv->params.nMaxBitRate);
if (obsqsv->params.nRateControl == MFX_RATECONTROL_ICQ)
blog(LOG_INFO, "\tICQ Quality: %d", (int)obsqsv->params.nICQQuality);
if (obsqsv->params.nLADEPTH)
blog(LOG_INFO, "\tLookahead Depth:%d", (int)obsqsv->params.nLADEPTH);
if (obsqsv->params.nRateControl == MFX_RATECONTROL_CQP)
blog(LOG_INFO,
"\tqpi: %d\n"
"\tqpb: %d\n"
"\tqpp: %d",
qpi, qpb, qpp);
blog(LOG_INFO,
"\ttarget_usage: %s\n"
"\tprofile: %s\n"
"\tkeyint: %d\n"
"\tlatency: %s\n"
"\tb-frames: %d\n"
"\tfps_num: %d\n"
"\tfps_den: %d\n"
"\twidth: %d\n"
"\theight: %d",
target_usage, profile, keyint_sec, latency, bFrames, voi->fps_num, voi->fps_den, width, height);
info("debug info:");
}
static bool update_settings(struct obs_qsv *obsqsv, obs_data_t *settings)
{
update_params(obsqsv, settings);
return true;
}
static void load_hevc_headers(struct obs_qsv *obsqsv)
{
DARRAY(uint8_t) header;
DARRAY(uint8_t) sei;
da_init(header);
da_init(sei);
uint8_t *pVPS, *pSPS, *pPPS;
uint16_t nVPS, nSPS, nPPS;
qsv_hevc_encoder_headers(obsqsv->context, &pVPS, &pSPS, &pPPS, &nVPS, &nSPS, &nPPS);
da_push_back_array(header, pVPS, nVPS);
da_push_back_array(header, pSPS, nSPS);
da_push_back_array(header, pPPS, nPPS);
obsqsv->extra_data = header.array;
obsqsv->extra_data_size = header.num;
obsqsv->sei = sei.array;
obsqsv->sei_size = sei.num;
}
static void load_headers(struct obs_qsv *obsqsv)
{
DARRAY(uint8_t) header;
static uint8_t sei = 0;
// Not sure if SEI is needed.
// Just filling in empty meaningless SEI message.
// Seems to work fine.
// DARRAY(uint8_t) sei;
da_init(header);
// da_init(sei);
uint8_t *pSPS, *pPPS;
uint16_t nSPS, nPPS;
qsv_encoder_headers(obsqsv->context, &pSPS, &pPPS, &nSPS, &nPPS);
da_push_back_array(header, pSPS, nSPS);
// AV1 does not need PPS
if (obsqsv->codec != QSV_CODEC_AV1)
da_push_back_array(header, pPPS, nPPS);
obsqsv->extra_data = header.array;
obsqsv->extra_data_size = header.num;
obsqsv->sei = &sei;
obsqsv->sei_size = 1;
}
static bool obs_qsv_update(void *data, obs_data_t *settings)
{
struct obs_qsv *obsqsv = data;
obsqsv->params.nTargetBitRate = (mfxU16)obs_data_get_int(settings, "bitrate");
if (!qsv_encoder_reconfig(obsqsv->context, &obsqsv->params)) {
warn("Failed to reconfigure");
return false;
}
return true;
}
static void *obs_qsv_create(enum qsv_codec codec, obs_data_t *settings, obs_encoder_t *encoder, bool useTexAlloc)
{
struct obs_qsv *obsqsv = bzalloc(sizeof(struct obs_qsv));
obsqsv->encoder = encoder;
obsqsv->codec = codec;
video_t *video = obs_encoder_video(encoder);
const struct video_output_info *voi = video_output_get_info(video);
switch (voi->format) {
case VIDEO_FORMAT_I010:
case VIDEO_FORMAT_P010:
if (codec == QSV_CODEC_AVC) {
const char *const text = obs_module_text("10bitUnsupportedAvc");
obs_encoder_set_last_error(encoder, text);
error("%s", text);
bfree(obsqsv);
return NULL;
}
obsqsv->params.video_fmt_10bit = true;
break;
case VIDEO_FORMAT_P216:
case VIDEO_FORMAT_P416: {
const char *const text = obs_module_text("16bitUnsupported");
obs_encoder_set_last_error(encoder, text);
error("%s", text);
bfree(obsqsv);
return NULL;
}
default:
switch (voi->colorspace) {
case VIDEO_CS_2100_PQ:
case VIDEO_CS_2100_HLG: {
const char *const text = obs_module_text("8bitUnsupportedHdr");
obs_encoder_set_last_error(encoder, text);
error("%s", text);
bfree(obsqsv);
return NULL;
}
}
}
if (update_settings(obsqsv, settings)) {
pthread_mutex_lock(&g_QsvLock);
obsqsv->context = qsv_encoder_open(&obsqsv->params, codec, useTexAlloc);
pthread_mutex_unlock(&g_QsvLock);
if (obsqsv->context == NULL)
warn("qsv failed to load");
else if (obsqsv->codec == QSV_CODEC_HEVC)
load_hevc_headers(obsqsv);
else
load_headers(obsqsv);
} else {
warn("bad settings specified");
}
qsv_encoder_version(&g_verMajor, &g_verMinor);
blog(LOG_INFO,
"\tmajor: %d\n"
"\tminor: %d",
g_verMajor, g_verMinor);
if (!obsqsv->context) {
bfree(obsqsv);
return NULL;
}
obsqsv->performance_token = os_request_high_performance("qsv encoding");
return obsqsv;
}
static void *obs_qsv_create_h264(obs_data_t *settings, obs_encoder_t *encoder)
{
return obs_qsv_create(QSV_CODEC_AVC, settings, encoder, false);
}
static void *obs_qsv_create_av1(obs_data_t *settings, obs_encoder_t *encoder)
{
return obs_qsv_create(QSV_CODEC_AV1, settings, encoder, false);
}
static void *obs_qsv_create_hevc(obs_data_t *settings, obs_encoder_t *encoder)
{
return obs_qsv_create(QSV_CODEC_HEVC, settings, encoder, false);
}
static void *obs_qsv_create_tex(enum qsv_codec codec, obs_data_t *settings, obs_encoder_t *encoder,
const char *fallback_id)
{
struct obs_video_info ovi;
obs_get_video_info(&ovi);
if (!adapters[ovi.adapter].is_intel) {
blog(LOG_INFO, ">>> app not on intel GPU, fall back to old qsv encoder");
return obs_encoder_create_rerouted(encoder, (const char *)fallback_id);
}
if (codec == QSV_CODEC_AV1 && !adapters[ovi.adapter].supports_av1) {
blog(LOG_INFO, ">>> cap on different device, fall back to non-texture sharing AV1 qsv encoder");
return obs_encoder_create_rerouted(encoder, (const char *)fallback_id);
}
bool gpu_texture_active = obs_encoder_video_tex_active(encoder, VIDEO_FORMAT_NV12);
if (codec != QSV_CODEC_AVC)
gpu_texture_active = gpu_texture_active || obs_encoder_video_tex_active(encoder, VIDEO_FORMAT_P010);
if (!gpu_texture_active) {
blog(LOG_INFO, ">>> gpu tex not active, fall back to old qsv encoder");
return obs_encoder_create_rerouted(encoder, (const char *)fallback_id);
}
if (obs_encoder_scaling_enabled(encoder)) {
if (!obs_encoder_gpu_scaling_enabled(encoder)) {
blog(LOG_INFO, ">>> encoder CPU scaling active, fall back to old qsv encoder");
return obs_encoder_create_rerouted(encoder, (const char *)fallback_id);
}
blog(LOG_INFO, ">>> encoder GPU scaling active");
}
blog(LOG_INFO, ">>> new qsv encoder");
return obs_qsv_create(codec, settings, encoder, true);
}
static void *obs_qsv_create_tex_h264(obs_data_t *settings, obs_encoder_t *encoder)
{
return obs_qsv_create_tex(QSV_CODEC_AVC, settings, encoder, "obs_qsv11_soft");
}
static void *obs_qsv_create_tex_h264_v2(obs_data_t *settings, obs_encoder_t *encoder)
{
return obs_qsv_create_tex(QSV_CODEC_AVC, settings, encoder, "obs_qsv11_soft_v2");
}
static void *obs_qsv_create_tex_av1(obs_data_t *settings, obs_encoder_t *encoder)
{
return obs_qsv_create_tex(QSV_CODEC_AV1, settings, encoder, "obs_qsv11_av1_soft");
}
static void *obs_qsv_create_tex_hevc(obs_data_t *settings, obs_encoder_t *encoder)
{
return obs_qsv_create_tex(QSV_CODEC_HEVC, settings, encoder, "obs_qsv11_hevc_soft");
}
static bool obs_qsv_extra_data(void *data, uint8_t **extra_data, size_t *size)
{
struct obs_qsv *obsqsv = data;
if (!obsqsv->context)
return false;
*extra_data = obsqsv->extra_data;
*size = obsqsv->extra_data_size;
return true;
}
static bool obs_qsv_sei(void *data, uint8_t **sei, size_t *size)
{
struct obs_qsv *obsqsv = data;
if (!obsqsv->context)
return false;
*sei = obsqsv->sei;
*size = obsqsv->sei_size;
return true;
}
static inline bool valid_format(enum video_format format)
{
return format == VIDEO_FORMAT_NV12;
}
static inline bool valid_av1_format(enum video_format format)
{
return format == VIDEO_FORMAT_NV12 || format == VIDEO_FORMAT_P010;
}
static inline void cap_resolution(struct obs_qsv *obsqsv, struct video_scale_info *info)
{
enum qsv_cpu_platform qsv_platform = qsv_get_cpu_platform();
uint32_t width = obs_encoder_get_width(obsqsv->encoder);
uint32_t height = obs_encoder_get_height(obsqsv->encoder);
if (adapters[adapter_index].is_dgpu == true)
qsv_platform = QSV_CPU_PLATFORM_UNKNOWN;
info->height = height;
info->width = width;
if (qsv_platform <= QSV_CPU_PLATFORM_IVB && qsv_platform != QSV_CPU_PLATFORM_UNKNOWN) {
if (width > 1920) {
info->width = 1920;
}
if (height > 1200) {
info->height = 1200;
}
}
}
static void obs_qsv_video_info(void *data, struct video_scale_info *info)
{
struct obs_qsv *obsqsv = data;
enum video_format pref_format;
pref_format = obs_encoder_get_preferred_video_format(obsqsv->encoder);
if (!valid_format(pref_format)) {
pref_format = valid_format(info->format) ? info->format : VIDEO_FORMAT_NV12;
}
info->format = pref_format;
cap_resolution(obsqsv, info);
}
static void obs_qsv_video_plus_hdr_info(void *data, struct video_scale_info *info)
{
struct obs_qsv *obsqsv = data;
enum video_format pref_format;
pref_format = obs_encoder_get_preferred_video_format(obsqsv->encoder);
if (!valid_av1_format(pref_format)) {
pref_format = valid_av1_format(info->format) ? info->format : VIDEO_FORMAT_NV12;
}
info->format = pref_format;
cap_resolution(obsqsv, info);
}
static mfxU64 ts_obs_to_mfx(int64_t ts, const struct video_output_info *voi)
{
return ts * 90000 / voi->fps_num;
}
static int64_t ts_mfx_to_obs(mfxI64 ts, const struct video_output_info *voi)
{
int64_t div = 90000 * (int64_t)voi->fps_den;
/* Round to the nearest integer multiple of `voi->fps_den`. */
if (ts < 0)
return (ts * voi->fps_num - div / 2) / div * voi->fps_den;
else
return (ts * voi->fps_num + div / 2) / div * voi->fps_den;
}
static void parse_packet(struct obs_qsv *obsqsv, struct encoder_packet *packet, mfxBitstream *pBS,
const struct video_output_info *voi, bool *received_packet)
{
uint8_t *start, *end;
int type;