ffmpeg_hw.c 8.3 KB

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