hwdevice.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdio.h>
  19. #include "libavutil/hwcontext.h"
  20. static int test_derivation(AVBufferRef *src_ref, const char *src_name)
  21. {
  22. enum AVHWDeviceType derived_type;
  23. const char *derived_name;
  24. AVBufferRef *derived_ref = NULL, *back_ref = NULL;
  25. AVHWDeviceContext *src_dev, *derived_dev;
  26. int err;
  27. src_dev = (AVHWDeviceContext*)src_ref->data;
  28. derived_type = AV_HWDEVICE_TYPE_NONE;
  29. while (1) {
  30. derived_type = av_hwdevice_iterate_types(derived_type);
  31. if (derived_type == AV_HWDEVICE_TYPE_NONE)
  32. break;
  33. derived_name = av_hwdevice_get_type_name(derived_type);
  34. err = av_hwdevice_ctx_create_derived(&derived_ref, derived_type,
  35. src_ref, 0);
  36. if (err < 0) {
  37. fprintf(stderr, "Unable to derive %s -> %s: %d.\n",
  38. src_name, derived_name, err);
  39. continue;
  40. }
  41. derived_dev = (AVHWDeviceContext*)derived_ref->data;
  42. if (derived_dev->type != derived_type) {
  43. fprintf(stderr, "Device derived as type %d has type %d.\n",
  44. derived_type, derived_dev->type);
  45. goto fail;
  46. }
  47. if (derived_type == src_dev->type) {
  48. if (derived_dev != src_dev) {
  49. fprintf(stderr, "Derivation of %s from itself succeeded "
  50. "but did not return the same device.\n", src_name);
  51. goto fail;
  52. }
  53. av_buffer_unref(&derived_ref);
  54. continue;
  55. }
  56. err = av_hwdevice_ctx_create_derived(&back_ref, src_dev->type,
  57. derived_ref, 0);
  58. if (err < 0) {
  59. fprintf(stderr, "Derivation %s to %s succeeded, but derivation "
  60. "back again failed: %d.\n",
  61. src_name, derived_name, err);
  62. goto fail;
  63. }
  64. if (back_ref->data != src_ref->data) {
  65. fprintf(stderr, "Derivation %s to %s succeeded, but derivation "
  66. "back again did not return the original device.\n",
  67. src_name, derived_name);
  68. goto fail;
  69. }
  70. fprintf(stderr, "Successfully tested derivation %s -> %s.\n",
  71. src_name, derived_name);
  72. av_buffer_unref(&derived_ref);
  73. av_buffer_unref(&back_ref);
  74. }
  75. return 0;
  76. fail:
  77. av_buffer_unref(&derived_ref);
  78. av_buffer_unref(&back_ref);
  79. return -1;
  80. }
  81. static int test_device(enum AVHWDeviceType type, const char *name,
  82. const char *device, AVDictionary *opts, int flags)
  83. {
  84. AVBufferRef *ref;
  85. AVHWDeviceContext *dev;
  86. int err;
  87. err = av_hwdevice_ctx_create(&ref, type, device, opts, flags);
  88. if (err < 0) {
  89. fprintf(stderr, "Failed to create %s device: %d.\n", name, err);
  90. return 1;
  91. }
  92. dev = (AVHWDeviceContext*)ref->data;
  93. if (dev->type != type) {
  94. fprintf(stderr, "Device created as type %d has type %d.\n",
  95. type, dev->type);
  96. av_buffer_unref(&ref);
  97. return -1;
  98. }
  99. fprintf(stderr, "Device type %s successfully created.\n", name);
  100. err = test_derivation(ref, name);
  101. av_buffer_unref(&ref);
  102. return err;
  103. }
  104. static const struct {
  105. enum AVHWDeviceType type;
  106. const char *possible_devices[5];
  107. } test_devices[] = {
  108. { AV_HWDEVICE_TYPE_CUDA,
  109. { "0", "1", "2" } },
  110. { AV_HWDEVICE_TYPE_DRM,
  111. { "/dev/dri/card0", "/dev/dri/card1",
  112. "/dev/dri/renderD128", "/dev/dri/renderD129" } },
  113. { AV_HWDEVICE_TYPE_DXVA2,
  114. { "0", "1", "2" } },
  115. { AV_HWDEVICE_TYPE_D3D11VA,
  116. { "0", "1", "2" } },
  117. { AV_HWDEVICE_TYPE_OPENCL,
  118. { "0.0", "0.1", "1.0", "1.1" } },
  119. { AV_HWDEVICE_TYPE_VAAPI,
  120. { "/dev/dri/renderD128", "/dev/dri/renderD129", ":0" } },
  121. };
  122. static int test_device_type(enum AVHWDeviceType type)
  123. {
  124. enum AVHWDeviceType check;
  125. const char *name;
  126. int i, j, found, err;
  127. name = av_hwdevice_get_type_name(type);
  128. if (!name) {
  129. fprintf(stderr, "No name available for device type %d.\n", type);
  130. return -1;
  131. }
  132. check = av_hwdevice_find_type_by_name(name);
  133. if (check != type) {
  134. fprintf(stderr, "Type %d maps to name %s maps to type %d.\n",
  135. type, name, check);
  136. return -1;
  137. }
  138. found = 0;
  139. err = test_device(type, name, NULL, NULL, 0);
  140. if (err < 0) {
  141. fprintf(stderr, "Test failed for %s with default options.\n", name);
  142. return -1;
  143. }
  144. if (err == 0) {
  145. fprintf(stderr, "Test passed for %s with default options.\n", name);
  146. ++found;
  147. }
  148. for (i = 0; i < FF_ARRAY_ELEMS(test_devices); i++) {
  149. if (test_devices[i].type != type)
  150. continue;
  151. for (j = 0; test_devices[i].possible_devices[j]; j++) {
  152. err = test_device(type, name,
  153. test_devices[i].possible_devices[j],
  154. NULL, 0);
  155. if (err < 0) {
  156. fprintf(stderr, "Test failed for %s with device %s.\n",
  157. name, test_devices[i].possible_devices[j]);
  158. return -1;
  159. }
  160. if (err == 0) {
  161. fprintf(stderr, "Test passed for %s with device %s.\n",
  162. name, test_devices[i].possible_devices[j]);
  163. ++found;
  164. }
  165. }
  166. }
  167. return !found;
  168. }
  169. int main(void)
  170. {
  171. enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
  172. int pass, fail, skip, err;
  173. pass = fail = skip = 0;
  174. while (1) {
  175. type = av_hwdevice_iterate_types(type);
  176. if (type == AV_HWDEVICE_TYPE_NONE)
  177. break;
  178. err = test_device_type(type);
  179. if (err == 0)
  180. ++pass;
  181. else if (err < 0)
  182. ++fail;
  183. else
  184. ++skip;
  185. }
  186. fprintf(stderr, "Attempted to test %d device types: "
  187. "%d passed, %d failed, %d skipped.\n",
  188. pass + fail + skip, pass, fail, skip);
  189. return fail > 0;
  190. }