ffmpeg_hw.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <string.h>
  19. #include "libavutil/mem.h"
  20. #include "ffmpeg.h"
  21. static int nb_hw_devices;
  22. static HWDevice **hw_devices;
  23. HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
  24. {
  25. HWDevice *found = NULL;
  26. int i;
  27. for (i = 0; i < nb_hw_devices; i++) {
  28. if (hw_devices[i]->type == type) {
  29. if (found)
  30. return NULL;
  31. found = hw_devices[i];
  32. }
  33. }
  34. return found;
  35. }
  36. HWDevice *hw_device_get_by_name(const char *name)
  37. {
  38. int i;
  39. for (i = 0; i < nb_hw_devices; i++) {
  40. if (!strcmp(hw_devices[i]->name, name))
  41. return hw_devices[i];
  42. }
  43. return NULL;
  44. }
  45. static HWDevice *hw_device_add(void)
  46. {
  47. int err;
  48. err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
  49. sizeof(*hw_devices));
  50. if (err) {
  51. nb_hw_devices = 0;
  52. return NULL;
  53. }
  54. hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
  55. if (!hw_devices[nb_hw_devices])
  56. return NULL;
  57. return hw_devices[nb_hw_devices++];
  58. }
  59. static char *hw_device_default_name(enum AVHWDeviceType type)
  60. {
  61. // Make an automatic name of the form "type%d". We arbitrarily
  62. // limit at 1000 anonymous devices of the same type - there is
  63. // probably something else very wrong if you get to this limit.
  64. const char *type_name = av_hwdevice_get_type_name(type);
  65. char *name;
  66. size_t index_pos;
  67. int index, index_limit = 1000;
  68. index_pos = strlen(type_name);
  69. name = av_malloc(index_pos + 4);
  70. if (!name)
  71. return NULL;
  72. for (index = 0; index < index_limit; index++) {
  73. snprintf(name, index_pos + 4, "%s%d", type_name, index);
  74. if (!hw_device_get_by_name(name))
  75. break;
  76. }
  77. if (index >= index_limit) {
  78. av_freep(&name);
  79. return NULL;
  80. }
  81. return name;
  82. }
  83. int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
  84. {
  85. // "type=name"
  86. // "type=name,key=value,key2=value2"
  87. // "type=name:device,key=value,key2=value2"
  88. // "type:device,key=value,key2=value2"
  89. // -> av_hwdevice_ctx_create()
  90. // "type=name@name"
  91. // "type@name"
  92. // -> av_hwdevice_ctx_create_derived()
  93. AVDictionary *options = NULL;
  94. const char *type_name = NULL, *name = NULL, *device = NULL;
  95. enum AVHWDeviceType type;
  96. HWDevice *dev, *src;
  97. AVBufferRef *device_ref = NULL;
  98. int err;
  99. const char *errmsg, *p, *q;
  100. size_t k;
  101. k = strcspn(arg, ":=@");
  102. p = arg + k;
  103. type_name = av_strndup(arg, k);
  104. if (!type_name) {
  105. err = AVERROR(ENOMEM);
  106. goto fail;
  107. }
  108. type = av_hwdevice_find_type_by_name(type_name);
  109. if (type == AV_HWDEVICE_TYPE_NONE) {
  110. errmsg = "unknown device type";
  111. goto invalid;
  112. }
  113. if (*p == '=') {
  114. k = strcspn(p + 1, ":@,");
  115. name = av_strndup(p + 1, k);
  116. if (!name) {
  117. err = AVERROR(ENOMEM);
  118. goto fail;
  119. }
  120. if (hw_device_get_by_name(name)) {
  121. errmsg = "named device already exists";
  122. goto invalid;
  123. }
  124. p += 1 + k;
  125. } else {
  126. name = hw_device_default_name(type);
  127. if (!name) {
  128. err = AVERROR(ENOMEM);
  129. goto fail;
  130. }
  131. }
  132. if (!*p) {
  133. // New device with no parameters.
  134. err = av_hwdevice_ctx_create(&device_ref, type,
  135. NULL, NULL, 0);
  136. if (err < 0)
  137. goto fail;
  138. } else if (*p == ':') {
  139. // New device with some parameters.
  140. ++p;
  141. q = strchr(p, ',');
  142. if (q) {
  143. if (q - p > 0) {
  144. device = av_strndup(p, q - p);
  145. if (!device) {
  146. err = AVERROR(ENOMEM);
  147. goto fail;
  148. }
  149. }
  150. err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
  151. if (err < 0) {
  152. errmsg = "failed to parse options";
  153. goto invalid;
  154. }
  155. }
  156. err = av_hwdevice_ctx_create(&device_ref, type,
  157. q ? device : p[0] ? p : NULL,
  158. options, 0);
  159. if (err < 0)
  160. goto fail;
  161. } else if (*p == '@') {
  162. // Derive from existing device.
  163. src = hw_device_get_by_name(p + 1);
  164. if (!src) {
  165. errmsg = "invalid source device name";
  166. goto invalid;
  167. }
  168. err = av_hwdevice_ctx_create_derived(&device_ref, type,
  169. src->device_ref, 0);
  170. if (err < 0)
  171. goto fail;
  172. } else if (*p == ',') {
  173. err = av_dict_parse_string(&options, p + 1, "=", ",", 0);
  174. if (err < 0) {
  175. errmsg = "failed to parse options";
  176. goto invalid;
  177. }
  178. err = av_hwdevice_ctx_create(&device_ref, type,
  179. NULL, options, 0);
  180. if (err < 0)
  181. goto fail;
  182. } else {
  183. errmsg = "parse error";
  184. goto invalid;
  185. }
  186. dev = hw_device_add();
  187. if (!dev) {
  188. err = AVERROR(ENOMEM);
  189. goto fail;
  190. }
  191. dev->name = name;
  192. dev->type = type;
  193. dev->device_ref = device_ref;
  194. if (dev_out)
  195. *dev_out = dev;
  196. name = NULL;
  197. err = 0;
  198. done:
  199. av_freep(&type_name);
  200. av_freep(&name);
  201. av_freep(&device);
  202. av_dict_free(&options);
  203. return err;
  204. invalid:
  205. av_log(NULL, AV_LOG_ERROR,
  206. "Invalid device specification \"%s\": %s\n", arg, errmsg);
  207. err = AVERROR(EINVAL);
  208. goto done;
  209. fail:
  210. av_log(NULL, AV_LOG_ERROR,
  211. "Device creation failed: %d.\n", err);
  212. av_buffer_unref(&device_ref);
  213. goto done;
  214. }
  215. int hw_device_init_from_type(enum AVHWDeviceType type,
  216. const char *device,
  217. HWDevice **dev_out)
  218. {
  219. AVBufferRef *device_ref = NULL;
  220. HWDevice *dev;
  221. char *name;
  222. int err;
  223. name = hw_device_default_name(type);
  224. if (!name) {
  225. err = AVERROR(ENOMEM);
  226. goto fail;
  227. }
  228. err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
  229. if (err < 0) {
  230. av_log(NULL, AV_LOG_ERROR,
  231. "Device creation failed: %d.\n", err);
  232. goto fail;
  233. }
  234. dev = hw_device_add();
  235. if (!dev) {
  236. err = AVERROR(ENOMEM);
  237. goto fail;
  238. }
  239. dev->name = name;
  240. dev->type = type;
  241. dev->device_ref = device_ref;
  242. if (dev_out)
  243. *dev_out = dev;
  244. return 0;
  245. fail:
  246. av_freep(&name);
  247. av_buffer_unref(&device_ref);
  248. return err;
  249. }
  250. void hw_device_free_all(void)
  251. {
  252. int i;
  253. for (i = 0; i < nb_hw_devices; i++) {
  254. av_freep(&hw_devices[i]->name);
  255. av_buffer_unref(&hw_devices[i]->device_ref);
  256. av_freep(&hw_devices[i]);
  257. }
  258. av_freep(&hw_devices);
  259. nb_hw_devices = 0;
  260. }
  261. AVBufferRef *hw_device_for_filter(void)
  262. {
  263. // Pick the last hardware device if the user doesn't pick the device for
  264. // filters explicitly with the filter_hw_device option.
  265. if (filter_hw_device)
  266. return filter_hw_device->device_ref;
  267. else if (nb_hw_devices > 0) {
  268. HWDevice *dev = hw_devices[nb_hw_devices - 1];
  269. if (nb_hw_devices > 1)
  270. av_log(NULL, AV_LOG_WARNING, "There are %d hardware devices. device "
  271. "%s of type %s is picked for filters by default. Set hardware "
  272. "device explicitly with the filter_hw_device option if device "
  273. "%s is not usable for filters.\n",
  274. nb_hw_devices, dev->name,
  275. av_hwdevice_get_type_name(dev->type), dev->name);
  276. return dev->device_ref;
  277. }
  278. return NULL;
  279. }