-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathva.c
2483 lines (2121 loc) · 69.2 KB
/
va.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
/*
* Copyright (c) 2007 Intel Corporation. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define _GNU_SOURCE 1
#include "sysdeps.h"
#include "va.h"
#include "va_backend.h"
#include "va_backend_prot.h"
#include "va_backend_vpp.h"
#include "va_internal.h"
#include "va_trace.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include "compat_win32.h"
#define DRIVER_EXTENSION "_drv_video.dll"
#define DRIVER_PATH_STRING "%s\\%s%s"
#define ENV_VAR_SEPARATOR ";"
#else
#include <dlfcn.h>
#include <unistd.h>
#define DRIVER_EXTENSION "_drv_video.so"
#define DRIVER_PATH_STRING "%s/%s%s"
#define ENV_VAR_SEPARATOR ":"
#endif
#ifdef ANDROID
#include <log/log.h>
#endif
#define ASSERT assert
#define CHECK_VTABLE(s, ctx, func) if (!va_checkVtable(dpy, ctx->vtable->va##func, #func)) s = VA_STATUS_ERROR_UNIMPLEMENTED;
#define CHECK_MAXIMUM(s, ctx, var) if (!va_checkMaximum(dpy, ctx->max_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;
#define CHECK_STRING(s, ctx, var) if (!va_checkString(dpy, ctx->str_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#ifndef HAVE_SECURE_GETENV
static char * secure_getenv(const char *name)
{
#if defined(__MINGW32__) || defined(__MINGW64__)
if (getuid() == geteuid())
#else
if (getuid() == geteuid() && getgid() == getegid())
#endif
return getenv(name);
else
return NULL;
}
#endif
/*
* read a config "env" for libva.conf or from environment setting
* libva.conf has higher priority
* return 0: the "env" is set, and the value is copied into env_value
* 1: the env is not set
*/
int va_parseConfig(char *env, char *env_value)
{
char *token, *value, *saveptr;
char oneline[1024];
FILE *fp = NULL;
if (env == NULL)
return 1;
fp = fopen(SYSCONFDIR "/libva.conf", "r");
while (fp && (fgets(oneline, 1024, fp) != NULL)) {
if (strlen(oneline) == 1)
continue;
token = strtok_r(oneline, "=\n", &saveptr);
value = strtok_r(NULL, "=\n", &saveptr);
if (NULL == token || NULL == value)
continue;
if (strcmp(token, env) == 0) {
if (env_value) {
strncpy(env_value, value, 1024);
env_value[1023] = '\0';
}
fclose(fp);
return 0;
}
}
if (fp)
fclose(fp);
/* no setting in config file, use env setting */
value = secure_getenv(env);
if (value) {
if (env_value) {
strncpy(env_value, value, 1024);
env_value[1023] = '\0';
}
return 0;
}
return 1;
}
int vaDisplayIsValid(VADisplay dpy)
{
VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
return pDisplayContext &&
pDisplayContext->vadpy_magic == VA_DISPLAY_MAGIC &&
pDisplayContext->pDriverContext;
}
/*
* Global log level configured from the config file or environment, which sets
* whether default logging appears or not (always overridden by explicitly
* user-configured logging).
*/
static int default_log_level = 2;
static void default_log_error(void *user_context, const char *buffer)
{
if (default_log_level < 1)
return;
# ifdef ANDROID
ALOGE("%s", buffer);
# else
fprintf(stderr, "libva error: %s", buffer);
# endif
}
static void default_log_info(void *user_context, const char *buffer)
{
if (default_log_level < 2)
return;
# ifdef ANDROID
ALOGI("%s", buffer);
# else
fprintf(stderr, "libva info: %s", buffer);
# endif
}
/**
* Set the callback for error messages, or NULL for no logging.
* Returns the previous one, or NULL if it was disabled.
*/
VAMessageCallback vaSetErrorCallback(VADisplay dpy, VAMessageCallback callback, void *user_context)
{
VADisplayContextP dctx;
VAMessageCallback old_callback;
if (!vaDisplayIsValid(dpy))
return NULL;
dctx = (VADisplayContextP)dpy;
old_callback = dctx->error_callback;
dctx->error_callback = callback;
dctx->error_callback_user_context = user_context;
return old_callback;
}
/**
* Set the callback for info messages, or NULL for no logging.
* Returns the previous one, or NULL if it was disabled.
*/
VAMessageCallback vaSetInfoCallback(VADisplay dpy, VAMessageCallback callback, void *user_context)
{
VADisplayContextP dctx;
VAMessageCallback old_callback;
if (!vaDisplayIsValid(dpy))
return NULL;
dctx = (VADisplayContextP)dpy;
old_callback = dctx->info_callback;
dctx->info_callback = callback;
dctx->info_callback_user_context = user_context;
return old_callback;
}
static void va_MessagingInit()
{
char env_value[1024];
int ret;
if (va_parseConfig("LIBVA_MESSAGING_LEVEL", &env_value[0]) == 0) {
ret = sscanf(env_value, "%d", &default_log_level);
if (ret < 1 || default_log_level < 0 || default_log_level > 2)
default_log_level = 2;
}
}
void va_errorMessage(VADisplay dpy, const char *msg, ...)
{
VADisplayContextP dctx = (VADisplayContextP)dpy;
char buf[512], *dynbuf;
va_list args;
int n, len;
if (dctx->error_callback == NULL)
return;
va_start(args, msg);
len = vsnprintf(buf, sizeof(buf), msg, args);
va_end(args);
if (len >= (int)sizeof(buf)) {
dynbuf = malloc(len + 1);
if (!dynbuf)
return;
va_start(args, msg);
n = vsnprintf(dynbuf, len + 1, msg, args);
va_end(args);
if (n == len)
dctx->error_callback(dctx->error_callback_user_context, dynbuf);
free(dynbuf);
} else if (len > 0)
dctx->error_callback(dctx->error_callback_user_context, buf);
}
void va_infoMessage(VADisplay dpy, const char *msg, ...)
{
VADisplayContextP dctx = (VADisplayContextP)dpy;
char buf[512], *dynbuf;
va_list args;
int n, len;
if (dctx->info_callback == NULL)
return;
va_start(args, msg);
len = vsnprintf(buf, sizeof(buf), msg, args);
va_end(args);
if (len >= (int)sizeof(buf)) {
dynbuf = malloc(len + 1);
if (!dynbuf)
return;
va_start(args, msg);
n = vsnprintf(dynbuf, len + 1, msg, args);
va_end(args);
if (n == len)
dctx->info_callback(dctx->info_callback_user_context, dynbuf);
free(dynbuf);
} else if (len > 0)
dctx->info_callback(dctx->info_callback_user_context, buf);
}
static void va_driverErrorCallback(VADriverContextP ctx,
const char *message)
{
VADisplayContextP dctx = ctx->pDisplayContext;
if (!dctx)
return;
dctx->error_callback(dctx->error_callback_user_context, message);
}
static void va_driverInfoCallback(VADriverContextP ctx,
const char *message)
{
VADisplayContextP dctx = ctx->pDisplayContext;
if (!dctx)
return;
dctx->info_callback(dctx->info_callback_user_context, message);
}
VADisplayContextP va_newDisplayContext(void)
{
VADisplayContextP dctx = calloc(1, sizeof(*dctx));
if (!dctx)
return NULL;
dctx->vadpy_magic = VA_DISPLAY_MAGIC;
dctx->error_callback = default_log_error;
dctx->info_callback = default_log_info;
return dctx;
}
VADriverContextP va_newDriverContext(VADisplayContextP dctx)
{
VADriverContextP ctx = calloc(1, sizeof(*ctx));
if (!ctx)
return NULL;
dctx->pDriverContext = ctx;
ctx->pDisplayContext = dctx;
ctx->error_callback = va_driverErrorCallback;
ctx->info_callback = va_driverInfoCallback;
return ctx;
}
static bool va_checkVtable(VADisplay dpy, void *ptr, char *function)
{
if (!ptr) {
va_errorMessage(dpy, "No valid vtable entry for va%s\n", function);
return false;
}
return true;
}
static bool va_checkMaximum(VADisplay dpy, int value, char *variable)
{
if (!value) {
va_errorMessage(dpy, "Failed to define max_%s in init\n", variable);
return false;
}
return true;
}
static bool va_checkString(VADisplay dpy, const char* value, char *variable)
{
if (!value) {
va_errorMessage(dpy, "Failed to define str_%s in init\n", variable);
return false;
}
return true;
}
static inline int
va_getDriverInitName(char *name, int namelen, int major, int minor)
{
int ret = snprintf(name, namelen, "__vaDriverInit_%d_%d", major, minor);
return ret > 0 && ret < namelen;
}
static char *va_getDriverPath(const char *driver_dir, const char *driver_name)
{
int n = snprintf(0, 0, DRIVER_PATH_STRING, driver_dir, driver_name, DRIVER_EXTENSION);
if (n < 0)
return NULL;
char *driver_path = (char *) malloc(n + 1);
if (!driver_path)
return NULL;
n = snprintf(driver_path, n + 1, DRIVER_PATH_STRING,
driver_dir, driver_name, DRIVER_EXTENSION);
if (n < 0) {
free(driver_path);
return NULL;
}
return driver_path;
}
static VAStatus va_openDriver(VADisplay dpy, char *driver_name)
{
VADriverContextP ctx = CTX(dpy);
VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
char *search_path = NULL;
char *saveptr;
char *driver_dir;
if (geteuid() == getuid())
/* don't allow setuid apps to use LIBVA_DRIVERS_PATH */
search_path = secure_getenv("LIBVA_DRIVERS_PATH");
if (!search_path)
search_path = VA_DRIVERS_PATH;
search_path = strdup((const char *)search_path);
if (!search_path) {
va_errorMessage(dpy, "%s L%d Out of memory\n",
__FUNCTION__, __LINE__);
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
driver_dir = strtok_r(search_path, ENV_VAR_SEPARATOR, &saveptr);
while (driver_dir) {
void *handle = NULL;
char *driver_path = va_getDriverPath(driver_dir, driver_name);
if (!driver_path) {
va_errorMessage(dpy, "%s L%d Out of memory\n",
__FUNCTION__, __LINE__);
free(search_path);
return VA_STATUS_ERROR_ALLOCATION_FAILED;
}
va_infoMessage(dpy, "Trying to open %s\n", driver_path);
#if defined(RTLD_NODELETE) && !defined(ANDROID)
handle = dlopen(driver_path, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
#else
handle = dlopen(driver_path, RTLD_NOW | RTLD_GLOBAL);
#endif
if (!handle) {
/* Don't give errors for non-existing files */
if (0 == access(driver_path, F_OK))
va_errorMessage(dpy, "dlopen of %s failed: %s\n", driver_path, dlerror());
} else {
VADriverInit init_func = NULL;
char init_func_s[256];
int i;
struct {
int major;
int minor;
} compatible_versions[VA_MINOR_VERSION + 2];
for (i = 0; i <= VA_MINOR_VERSION; i ++) {
compatible_versions[i].major = VA_MAJOR_VERSION;
compatible_versions[i].minor = VA_MINOR_VERSION - i;
}
compatible_versions[i].major = -1;
compatible_versions[i].minor = -1;
for (i = 0; compatible_versions[i].major >= 0; i++) {
if (va_getDriverInitName(init_func_s, sizeof(init_func_s),
compatible_versions[i].major,
compatible_versions[i].minor)) {
init_func = (VADriverInit)dlsym(handle, init_func_s);
if (init_func) {
va_infoMessage(dpy, "Found init function %s\n", init_func_s);
break;
}
}
}
if (compatible_versions[i].major < 0) {
va_errorMessage(dpy, "%s has no function %s\n",
driver_path, init_func_s);
dlclose(handle);
} else {
struct VADriverVTable *vtable = ctx->vtable;
struct VADriverVTableVPP *vtable_vpp = ctx->vtable_vpp;
struct VADriverVTableProt *vtable_prot = ctx->vtable_prot;
vaStatus = VA_STATUS_SUCCESS;
if (!vtable) {
vtable = calloc(1, sizeof(*vtable));
if (!vtable)
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
}
ctx->vtable = vtable;
if (!vtable_vpp) {
vtable_vpp = calloc(1, sizeof(*vtable_vpp));
if (vtable_vpp)
vtable_vpp->version = VA_DRIVER_VTABLE_VPP_VERSION;
else
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
}
ctx->vtable_vpp = vtable_vpp;
if (!vtable_prot) {
vtable_prot = calloc(1, sizeof(*vtable_prot));
if (vtable_prot)
vtable_prot->version = VA_DRIVER_VTABLE_PROT_VERSION;
else
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
}
ctx->vtable_prot = vtable_prot;
if (init_func && VA_STATUS_SUCCESS == vaStatus)
vaStatus = (*init_func)(ctx);
if (VA_STATUS_SUCCESS == vaStatus) {
CHECK_MAXIMUM(vaStatus, ctx, profiles);
CHECK_MAXIMUM(vaStatus, ctx, entrypoints);
CHECK_MAXIMUM(vaStatus, ctx, attributes);
CHECK_MAXIMUM(vaStatus, ctx, image_formats);
CHECK_MAXIMUM(vaStatus, ctx, subpic_formats);
CHECK_STRING(vaStatus, ctx, vendor);
CHECK_VTABLE(vaStatus, ctx, Terminate);
CHECK_VTABLE(vaStatus, ctx, QueryConfigProfiles);
CHECK_VTABLE(vaStatus, ctx, QueryConfigEntrypoints);
CHECK_VTABLE(vaStatus, ctx, QueryConfigAttributes);
CHECK_VTABLE(vaStatus, ctx, CreateConfig);
CHECK_VTABLE(vaStatus, ctx, DestroyConfig);
CHECK_VTABLE(vaStatus, ctx, GetConfigAttributes);
CHECK_VTABLE(vaStatus, ctx, CreateSurfaces);
CHECK_VTABLE(vaStatus, ctx, DestroySurfaces);
CHECK_VTABLE(vaStatus, ctx, CreateContext);
CHECK_VTABLE(vaStatus, ctx, DestroyContext);
CHECK_VTABLE(vaStatus, ctx, CreateBuffer);
CHECK_VTABLE(vaStatus, ctx, BufferSetNumElements);
CHECK_VTABLE(vaStatus, ctx, MapBuffer);
CHECK_VTABLE(vaStatus, ctx, UnmapBuffer);
CHECK_VTABLE(vaStatus, ctx, DestroyBuffer);
CHECK_VTABLE(vaStatus, ctx, BeginPicture);
CHECK_VTABLE(vaStatus, ctx, RenderPicture);
CHECK_VTABLE(vaStatus, ctx, EndPicture);
CHECK_VTABLE(vaStatus, ctx, SyncSurface);
CHECK_VTABLE(vaStatus, ctx, QuerySurfaceStatus);
CHECK_VTABLE(vaStatus, ctx, QueryImageFormats);
CHECK_VTABLE(vaStatus, ctx, CreateImage);
CHECK_VTABLE(vaStatus, ctx, DeriveImage);
CHECK_VTABLE(vaStatus, ctx, DestroyImage);
CHECK_VTABLE(vaStatus, ctx, SetImagePalette);
CHECK_VTABLE(vaStatus, ctx, GetImage);
CHECK_VTABLE(vaStatus, ctx, PutImage);
CHECK_VTABLE(vaStatus, ctx, QuerySubpictureFormats);
CHECK_VTABLE(vaStatus, ctx, CreateSubpicture);
CHECK_VTABLE(vaStatus, ctx, DestroySubpicture);
CHECK_VTABLE(vaStatus, ctx, SetSubpictureImage);
CHECK_VTABLE(vaStatus, ctx, SetSubpictureChromakey);
CHECK_VTABLE(vaStatus, ctx, SetSubpictureGlobalAlpha);
CHECK_VTABLE(vaStatus, ctx, AssociateSubpicture);
CHECK_VTABLE(vaStatus, ctx, DeassociateSubpicture);
CHECK_VTABLE(vaStatus, ctx, QueryDisplayAttributes);
CHECK_VTABLE(vaStatus, ctx, GetDisplayAttributes);
CHECK_VTABLE(vaStatus, ctx, SetDisplayAttributes);
}
if (VA_STATUS_SUCCESS != vaStatus) {
va_errorMessage(dpy, "%s init failed\n", driver_path);
dlclose(handle);
}
if (VA_STATUS_SUCCESS == vaStatus)
ctx->handle = handle;
free(driver_path);
break;
}
}
free(driver_path);
driver_dir = strtok_r(NULL, ENV_VAR_SEPARATOR, &saveptr);
}
free(search_path);
return vaStatus;
}
VAPrivFunc vaGetLibFunc(VADisplay dpy, const char *func)
{
VADriverContextP ctx;
if (!vaDisplayIsValid(dpy))
return NULL;
ctx = CTX(dpy);
if (NULL == ctx->handle)
return NULL;
return (VAPrivFunc) dlsym(ctx->handle, func);
}
/*
* Returns a short english description of error_status
*/
const char *vaErrorStr(VAStatus error_status)
{
switch (error_status) {
case VA_STATUS_SUCCESS:
return "success (no error)";
case VA_STATUS_ERROR_OPERATION_FAILED:
return "operation failed";
case VA_STATUS_ERROR_ALLOCATION_FAILED:
return "resource allocation failed";
case VA_STATUS_ERROR_INVALID_DISPLAY:
return "invalid VADisplay";
case VA_STATUS_ERROR_INVALID_CONFIG:
return "invalid VAConfigID";
case VA_STATUS_ERROR_INVALID_CONTEXT:
return "invalid VAContextID";
case VA_STATUS_ERROR_INVALID_SURFACE:
return "invalid VASurfaceID";
case VA_STATUS_ERROR_INVALID_BUFFER:
return "invalid VABufferID";
case VA_STATUS_ERROR_INVALID_IMAGE:
return "invalid VAImageID";
case VA_STATUS_ERROR_INVALID_SUBPICTURE:
return "invalid VASubpictureID";
case VA_STATUS_ERROR_ATTR_NOT_SUPPORTED:
return "attribute not supported";
case VA_STATUS_ERROR_MAX_NUM_EXCEEDED:
return "list argument exceeds maximum number";
case VA_STATUS_ERROR_UNSUPPORTED_PROFILE:
return "the requested VAProfile is not supported";
case VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT:
return "the requested VAEntryPoint is not supported";
case VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT:
return "the requested RT Format is not supported";
case VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE:
return "the requested VABufferType is not supported";
case VA_STATUS_ERROR_SURFACE_BUSY:
return "surface is in use";
case VA_STATUS_ERROR_FLAG_NOT_SUPPORTED:
return "flag not supported";
case VA_STATUS_ERROR_INVALID_PARAMETER:
return "invalid parameter";
case VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED:
return "resolution not supported";
case VA_STATUS_ERROR_UNIMPLEMENTED:
return "the requested function is not implemented";
case VA_STATUS_ERROR_SURFACE_IN_DISPLAYING:
return "surface is in displaying (may by overlay)" ;
case VA_STATUS_ERROR_INVALID_IMAGE_FORMAT:
return "invalid VAImageFormat";
case VA_STATUS_ERROR_DECODING_ERROR:
return "internal decoding error";
case VA_STATUS_ERROR_ENCODING_ERROR:
return "internal encoding error";
case VA_STATUS_ERROR_INVALID_VALUE:
return "an invalid/unsupported value was supplied";
case VA_STATUS_ERROR_UNSUPPORTED_FILTER:
return "the requested filter is not supported";
case VA_STATUS_ERROR_INVALID_FILTER_CHAIN:
return "an invalid filter chain was supplied";
case VA_STATUS_ERROR_HW_BUSY:
return "HW busy now";
case VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE:
return "an unsupported memory type was supplied";
case VA_STATUS_ERROR_NOT_ENOUGH_BUFFER:
return "allocated memory size is not enough for input or output";
case VA_STATUS_ERROR_UNKNOWN:
return "unknown libva error";
}
return "unknown libva error / description missing";
}
VAStatus vaSetDriverName(
VADisplay dpy,
char *driver_name
)
{
VADriverContextP ctx;
VAStatus vaStatus = VA_STATUS_SUCCESS;
char *override_driver_name = NULL;
ctx = CTX(dpy);
if (strlen(driver_name) == 0 || strlen(driver_name) >= 256) {
vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
va_errorMessage(dpy, "vaSetDriverName returns %s\n",
vaErrorStr(vaStatus));
return vaStatus;
}
override_driver_name = strdup(driver_name);
if (!override_driver_name) {
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
va_errorMessage(dpy, "vaSetDriverName returns %s. Out of Memory\n",
vaErrorStr(vaStatus));
return vaStatus;
}
ctx->override_driver_name = override_driver_name;
return VA_STATUS_SUCCESS;
}
static VAStatus va_new_opendriver(VADisplay dpy)
{
VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
/* In the extreme case we can get up-to 5ish names. Pad that out to be on
* the safe side. In the worst case, the DRIVER BUG below will print and
* we'll be capped to the current selection.
*/
char *drivers[20] = { 0, };
unsigned int num_drivers = ARRAY_SIZE(drivers);
VAStatus vaStatus;
const char *driver_name_env;
VADriverContextP ctx;
/* XXX: The order is bonkers - env var should take highest priority, then
* override (which ought to be nuked) than native. It's not possible atm,
* since the DPY connect/init happens during the GetDriverNames.
*/
vaStatus = pDisplayContext->vaGetDriverNames(pDisplayContext, drivers, &num_drivers);
if (vaStatus != VA_STATUS_SUCCESS) {
/* Print and error yet continue, as per the above ordering note */
va_errorMessage(dpy, "vaGetDriverNames() failed with %s\n", vaErrorStr(vaStatus));
num_drivers = 0;
}
ctx = CTX(dpy);
driver_name_env = secure_getenv("LIBVA_DRIVER_NAME");
if ((ctx->override_driver_name) || (driver_name_env && (geteuid() == getuid()))) {
const char *driver = ctx->override_driver_name ?
ctx->override_driver_name : driver_name_env;
for (unsigned int i = 0; i < num_drivers; i++)
free(drivers[i]);
drivers[0] = strdup(driver);
num_drivers = 1;
va_infoMessage(dpy, "User %srequested driver '%s'\n",
ctx->override_driver_name ? "" : "environment variable ",
driver);
}
for (unsigned int i = 0; i < num_drivers; i++) {
/* The strdup() may have failed. Check here instead of a dozen+ places */
if (!drivers[i]) {
va_errorMessage(dpy, "%s:%d: Out of memory\n", __func__, __LINE__);
vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
break;
}
vaStatus = va_openDriver(dpy, drivers[i]);
va_infoMessage(dpy, "va_openDriver() returns %d\n", vaStatus);
if (vaStatus == VA_STATUS_SUCCESS)
break;
}
for (unsigned int i = 0; i < num_drivers; i++)
free(drivers[i]);
return vaStatus;
}
VAStatus vaInitialize(
VADisplay dpy,
int *major_version, /* out */
int *minor_version /* out */
)
{
VAStatus vaStatus;
CHECK_DISPLAY(dpy);
va_TraceInit(dpy);
va_MessagingInit();
va_infoMessage(dpy, "VA-API version %s\n", VA_VERSION_S);
vaStatus = va_new_opendriver(dpy);
*major_version = VA_MAJOR_VERSION;
*minor_version = VA_MINOR_VERSION;
VA_TRACE_LOG(va_TraceInitialize, dpy, major_version, minor_version);
VA_TRACE_RET(dpy, vaStatus);
return vaStatus;
}
/*
* After this call, all library internal resources will be cleaned up
*/
VAStatus vaTerminate(
VADisplay dpy
)
{
VAStatus vaStatus = VA_STATUS_SUCCESS;
VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
VADriverContextP old_ctx;
CHECK_DISPLAY(dpy);
old_ctx = CTX(dpy);
if (old_ctx->handle) {
vaStatus = old_ctx->vtable->vaTerminate(old_ctx);
dlclose(old_ctx->handle);
old_ctx->handle = NULL;
}
free(old_ctx->vtable);
old_ctx->vtable = NULL;
free(old_ctx->vtable_vpp);
old_ctx->vtable_vpp = NULL;
free(old_ctx->vtable_prot);
old_ctx->vtable_prot = NULL;
if (old_ctx->override_driver_name) {
free(old_ctx->override_driver_name);
old_ctx->override_driver_name = NULL;
}
VA_TRACE_LOG(va_TraceTerminate, dpy);
VA_TRACE_RET(dpy, vaStatus);
va_TraceEnd(dpy);
if (VA_STATUS_SUCCESS == vaStatus)
pDisplayContext->vaDestroy(pDisplayContext);
return vaStatus;
}
/*
* vaQueryVendorString returns a pointer to a zero-terminated string
* describing some aspects of the VA implemenation on a specific
* hardware accelerator. The format of the returned string is:
* <vendorname>-<major_version>-<minor_version>-<addtional_info>
* e.g. for the Intel GMA500 implementation, an example would be:
* "IntelGMA500-1.0-0.2-patch3
*/
const char *vaQueryVendorString(
VADisplay dpy
)
{
if (!vaDisplayIsValid(dpy))
return NULL;
return CTX(dpy)->str_vendor;
}
/* Get maximum number of profiles supported by the implementation */
int vaMaxNumProfiles(
VADisplay dpy
)
{
if (!vaDisplayIsValid(dpy))
return 0;
return CTX(dpy)->max_profiles;
}
/* Get maximum number of entrypoints supported by the implementation */
int vaMaxNumEntrypoints(
VADisplay dpy
)
{
if (!vaDisplayIsValid(dpy))
return 0;
return CTX(dpy)->max_entrypoints;
}
/* Get maximum number of attributs supported by the implementation */
int vaMaxNumConfigAttributes(
VADisplay dpy
)
{
if (!vaDisplayIsValid(dpy))
return 0;
return CTX(dpy)->max_attributes;
}
VAStatus vaQueryConfigEntrypoints(
VADisplay dpy,
VAProfile profile,
VAEntrypoint *entrypoints, /* out */
int *num_entrypoints /* out */
)
{
VADriverContextP ctx;
VAStatus vaStatus = VA_STATUS_SUCCESS;
CHECK_DISPLAY(dpy);
ctx = CTX(dpy);
vaStatus = ctx->vtable->vaQueryConfigEntrypoints(ctx, profile, entrypoints, num_entrypoints);
VA_TRACE_RET(dpy, vaStatus);
return vaStatus;
}
VAStatus vaGetConfigAttributes(
VADisplay dpy,
VAProfile profile,
VAEntrypoint entrypoint,
VAConfigAttrib *attrib_list, /* in/out */
int num_attribs
)
{
VADriverContextP ctx;
VAStatus vaStatus = VA_STATUS_SUCCESS;
CHECK_DISPLAY(dpy);
ctx = CTX(dpy);
vaStatus = ctx->vtable->vaGetConfigAttributes(ctx, profile, entrypoint, attrib_list, num_attribs);
VA_TRACE_RET(dpy, vaStatus);
return vaStatus;
}
VAStatus vaQueryConfigProfiles(
VADisplay dpy,
VAProfile *profile_list, /* out */
int *num_profiles /* out */
)
{
VADriverContextP ctx;
VAStatus vaStatus = VA_STATUS_SUCCESS;
CHECK_DISPLAY(dpy);
ctx = CTX(dpy);
vaStatus = ctx->vtable->vaQueryConfigProfiles(ctx, profile_list, num_profiles);
VA_TRACE_RET(dpy, vaStatus);
return vaStatus;
}
VAStatus vaCreateConfig(
VADisplay dpy,
VAProfile profile,
VAEntrypoint entrypoint,
VAConfigAttrib *attrib_list,
int num_attribs,
VAConfigID *config_id /* out */
)
{
VADriverContextP ctx;
VAStatus vaStatus = VA_STATUS_SUCCESS;
CHECK_DISPLAY(dpy);
ctx = CTX(dpy);
VA_TRACE_VVVA(dpy, CREATE_CONFIG, TRACE_BEGIN, profile, entrypoint, num_attribs, attrib_list);
vaStatus = ctx->vtable->vaCreateConfig(ctx, profile, entrypoint, attrib_list, num_attribs, config_id);
/* record the current entrypoint for further trace/fool determination */
VA_TRACE_ALL(va_TraceCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
VA_TRACE_RET(dpy, vaStatus);
VA_TRACE_PV(dpy, CREATE_CONFIG, TRACE_END, config_id, vaStatus);
return vaStatus;
}
VAStatus vaDestroyConfig(
VADisplay dpy,
VAConfigID config_id
)
{
VADriverContextP ctx;
VAStatus vaStatus = VA_STATUS_SUCCESS;
CHECK_DISPLAY(dpy);
ctx = CTX(dpy);
VA_TRACE_V(dpy, DESTROY_CONFIG, TRACE_BEGIN, config_id);
vaStatus = ctx->vtable->vaDestroyConfig(ctx, config_id);
VA_TRACE_ALL(va_TraceDestroyConfig, dpy, config_id);
VA_TRACE_RET(dpy, vaStatus);
VA_TRACE_V(dpy, DESTROY_CONFIG, TRACE_END, vaStatus);
return vaStatus;
}
VAStatus vaQueryConfigAttributes(
VADisplay dpy,
VAConfigID config_id,
VAProfile *profile, /* out */
VAEntrypoint *entrypoint, /* out */
VAConfigAttrib *attrib_list,/* out */
int *num_attribs /* out */
)
{
VADriverContextP ctx;
VAStatus vaStatus = VA_STATUS_SUCCESS;
CHECK_DISPLAY(dpy);
ctx = CTX(dpy);
vaStatus = ctx->vtable->vaQueryConfigAttributes(ctx, config_id, profile, entrypoint, attrib_list, num_attribs);
VA_TRACE_RET(dpy, vaStatus);
return vaStatus;
}
VAStatus vaQueryProcessingRate(
VADisplay dpy,
VAConfigID config_id,
VAProcessingRateParameter *proc_buf,
unsigned int *processing_rate /* out */
)
{
VADriverContextP ctx;
VAStatus vaStatus = VA_STATUS_SUCCESS;
CHECK_DISPLAY(dpy);
ctx = CTX(dpy);
if (!ctx->vtable->vaQueryProcessingRate)
vaStatus = VA_STATUS_ERROR_UNIMPLEMENTED;
else
vaStatus = ctx->vtable->vaQueryProcessingRate(ctx, config_id, proc_buf, processing_rate);
VA_TRACE_RET(dpy, vaStatus);
return vaStatus;
}
/* XXX: this is a slow implementation that will be removed */
static VAStatus
va_impl_query_surface_attributes(
VADriverContextP ctx,
VAConfigID config,
VASurfaceAttrib *out_attribs,
unsigned int *out_num_attribs_ptr
)
{
VASurfaceAttrib *attribs = NULL;
unsigned int num_attribs, n;
VASurfaceAttrib *out_attrib;
unsigned int out_num_attribs;
VAImageFormat *image_formats = NULL;
int num_image_formats, i;
VAStatus va_status;