Spaces:
Runtime error
Runtime error
File size: 6,809 Bytes
b86f76f |
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 |
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include "libavutil/hwcontext.h"
static int test_derivation(AVBufferRef *src_ref, const char *src_name)
{
enum AVHWDeviceType derived_type;
const char *derived_name;
AVBufferRef *derived_ref = NULL, *back_ref = NULL;
AVHWDeviceContext *src_dev, *derived_dev;
int err;
src_dev = (AVHWDeviceContext*)src_ref->data;
derived_type = AV_HWDEVICE_TYPE_NONE;
while (1) {
derived_type = av_hwdevice_iterate_types(derived_type);
if (derived_type == AV_HWDEVICE_TYPE_NONE)
break;
derived_name = av_hwdevice_get_type_name(derived_type);
err = av_hwdevice_ctx_create_derived(&derived_ref, derived_type,
src_ref, 0);
if (err < 0) {
fprintf(stderr, "Unable to derive %s -> %s: %d.\n",
src_name, derived_name, err);
continue;
}
derived_dev = (AVHWDeviceContext*)derived_ref->data;
if (derived_dev->type != derived_type) {
fprintf(stderr, "Device derived as type %d has type %d.\n",
derived_type, derived_dev->type);
goto fail;
}
if (derived_type == src_dev->type) {
if (derived_dev != src_dev) {
fprintf(stderr, "Derivation of %s from itself succeeded "
"but did not return the same device.\n", src_name);
goto fail;
}
av_buffer_unref(&derived_ref);
continue;
}
err = av_hwdevice_ctx_create_derived(&back_ref, src_dev->type,
derived_ref, 0);
if (err < 0) {
fprintf(stderr, "Derivation %s to %s succeeded, but derivation "
"back again failed: %d.\n",
src_name, derived_name, err);
goto fail;
}
if (back_ref->data != src_ref->data) {
fprintf(stderr, "Derivation %s to %s succeeded, but derivation "
"back again did not return the original device.\n",
src_name, derived_name);
goto fail;
}
fprintf(stderr, "Successfully tested derivation %s -> %s.\n",
src_name, derived_name);
av_buffer_unref(&derived_ref);
av_buffer_unref(&back_ref);
}
return 0;
fail:
av_buffer_unref(&derived_ref);
av_buffer_unref(&back_ref);
return -1;
}
static int test_device(enum AVHWDeviceType type, const char *name,
const char *device, AVDictionary *opts, int flags)
{
AVBufferRef *ref;
AVHWDeviceContext *dev;
int err;
err = av_hwdevice_ctx_create(&ref, type, device, opts, flags);
if (err < 0) {
fprintf(stderr, "Failed to create %s device: %d.\n", name, err);
return 1;
}
dev = (AVHWDeviceContext*)ref->data;
if (dev->type != type) {
fprintf(stderr, "Device created as type %d has type %d.\n",
type, dev->type);
av_buffer_unref(&ref);
return -1;
}
fprintf(stderr, "Device type %s successfully created.\n", name);
err = test_derivation(ref, name);
av_buffer_unref(&ref);
return err;
}
static const struct {
enum AVHWDeviceType type;
const char *possible_devices[5];
} test_devices[] = {
{ AV_HWDEVICE_TYPE_CUDA,
{ "0", "1", "2" } },
{ AV_HWDEVICE_TYPE_DRM,
{ "/dev/dri/card0", "/dev/dri/card1",
"/dev/dri/renderD128", "/dev/dri/renderD129" } },
{ AV_HWDEVICE_TYPE_DXVA2,
{ "0", "1", "2" } },
{ AV_HWDEVICE_TYPE_D3D11VA,
{ "0", "1", "2" } },
{ AV_HWDEVICE_TYPE_OPENCL,
{ "0.0", "0.1", "1.0", "1.1" } },
{ AV_HWDEVICE_TYPE_VAAPI,
{ "/dev/dri/renderD128", "/dev/dri/renderD129", ":0" } },
};
static int test_device_type(enum AVHWDeviceType type)
{
enum AVHWDeviceType check;
const char *name;
int i, j, found, err;
name = av_hwdevice_get_type_name(type);
if (!name) {
fprintf(stderr, "No name available for device type %d.\n", type);
return -1;
}
check = av_hwdevice_find_type_by_name(name);
if (check != type) {
fprintf(stderr, "Type %d maps to name %s maps to type %d.\n",
type, name, check);
return -1;
}
found = 0;
err = test_device(type, name, NULL, NULL, 0);
if (err < 0) {
fprintf(stderr, "Test failed for %s with default options.\n", name);
return -1;
}
if (err == 0) {
fprintf(stderr, "Test passed for %s with default options.\n", name);
++found;
}
for (i = 0; i < FF_ARRAY_ELEMS(test_devices); i++) {
if (test_devices[i].type != type)
continue;
for (j = 0; test_devices[i].possible_devices[j]; j++) {
err = test_device(type, name,
test_devices[i].possible_devices[j],
NULL, 0);
if (err < 0) {
fprintf(stderr, "Test failed for %s with device %s.\n",
name, test_devices[i].possible_devices[j]);
return -1;
}
if (err == 0) {
fprintf(stderr, "Test passed for %s with device %s.\n",
name, test_devices[i].possible_devices[j]);
++found;
}
}
}
return !found;
}
int main(void)
{
enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
int pass, fail, skip, err;
pass = fail = skip = 0;
while (1) {
type = av_hwdevice_iterate_types(type);
if (type == AV_HWDEVICE_TYPE_NONE)
break;
err = test_device_type(type);
if (err == 0)
++pass;
else if (err < 0)
++fail;
else
++skip;
}
fprintf(stderr, "Attempted to test %d device types: "
"%d passed, %d failed, %d skipped.\n",
pass + fail + skip, pass, fail, skip);
return fail > 0;
}
|