ffmpeg_hw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 "ffmpeg.h"
  21. static int nb_hw_devices;
  22. static HWDevice **hw_devices;
  23. static 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:device,key=value,key2=value2"
  86. // "type:device,key=value,key2=value2"
  87. // -> av_hwdevice_ctx_create()
  88. // "type=name@name"
  89. // "type@name"
  90. // -> av_hwdevice_ctx_create_derived()
  91. AVDictionary *options = NULL;
  92. char *type_name = NULL, *name = NULL, *device = NULL;
  93. enum AVHWDeviceType type;
  94. HWDevice *dev, *src;
  95. AVBufferRef *device_ref = NULL;
  96. int err;
  97. const char *errmsg, *p, *q;
  98. size_t k;
  99. k = strcspn(arg, ":=@");
  100. p = arg + k;
  101. type_name = av_strndup(arg, k);
  102. if (!type_name) {
  103. err = AVERROR(ENOMEM);
  104. goto fail;
  105. }
  106. type = av_hwdevice_find_type_by_name(type_name);
  107. if (type == AV_HWDEVICE_TYPE_NONE) {
  108. errmsg = "unknown device type";
  109. goto invalid;
  110. }
  111. if (*p == '=') {
  112. k = strcspn(p + 1, ":@");
  113. name = av_strndup(p + 1, k);
  114. if (!name) {
  115. err = AVERROR(ENOMEM);
  116. goto fail;
  117. }
  118. if (hw_device_get_by_name(name)) {
  119. errmsg = "named device already exists";
  120. goto invalid;
  121. }
  122. p += 1 + k;
  123. } else {
  124. name = hw_device_default_name(type);
  125. if (!name) {
  126. err = AVERROR(ENOMEM);
  127. goto fail;
  128. }
  129. }
  130. if (!*p) {
  131. // New device with no parameters.
  132. err = av_hwdevice_ctx_create(&device_ref, type,
  133. NULL, NULL, 0);
  134. if (err < 0)
  135. goto fail;
  136. } else if (*p == ':') {
  137. // New device with some parameters.
  138. ++p;
  139. q = strchr(p, ',');
  140. if (q) {
  141. device = av_strndup(p, q - p);
  142. if (!device) {
  143. err = AVERROR(ENOMEM);
  144. goto fail;
  145. }
  146. err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
  147. if (err < 0) {
  148. errmsg = "failed to parse options";
  149. goto invalid;
  150. }
  151. }
  152. err = av_hwdevice_ctx_create(&device_ref, type,
  153. device ? device : p, options, 0);
  154. if (err < 0)
  155. goto fail;
  156. } else if (*p == '@') {
  157. // Derive from existing device.
  158. src = hw_device_get_by_name(p + 1);
  159. if (!src) {
  160. errmsg = "invalid source device name";
  161. goto invalid;
  162. }
  163. err = av_hwdevice_ctx_create_derived(&device_ref, type,
  164. src->device_ref, 0);
  165. if (err < 0)
  166. goto fail;
  167. } else {
  168. errmsg = "parse error";
  169. goto invalid;
  170. }
  171. dev = hw_device_add();
  172. if (!dev) {
  173. err = AVERROR(ENOMEM);
  174. goto fail;
  175. }
  176. dev->name = name;
  177. dev->type = type;
  178. dev->device_ref = device_ref;
  179. if (dev_out)
  180. *dev_out = dev;
  181. name = NULL;
  182. err = 0;
  183. done:
  184. av_freep(&type_name);
  185. av_freep(&name);
  186. av_freep(&device);
  187. av_dict_free(&options);
  188. return err;
  189. invalid:
  190. av_log(NULL, AV_LOG_ERROR,
  191. "Invalid device specification \"%s\": %s\n", arg, errmsg);
  192. err = AVERROR(EINVAL);
  193. goto done;
  194. fail:
  195. av_log(NULL, AV_LOG_ERROR,
  196. "Device creation failed: %d.\n", err);
  197. av_buffer_unref(&device_ref);
  198. goto done;
  199. }
  200. static int hw_device_init_from_type(enum AVHWDeviceType type,
  201. const char *device,
  202. HWDevice **dev_out)
  203. {
  204. AVBufferRef *device_ref = NULL;
  205. HWDevice *dev;
  206. char *name;
  207. int err;
  208. name = hw_device_default_name(type);
  209. if (!name) {
  210. err = AVERROR(ENOMEM);
  211. goto fail;
  212. }
  213. err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
  214. if (err < 0) {
  215. av_log(NULL, AV_LOG_ERROR,
  216. "Device creation failed: %d.\n", err);
  217. goto fail;
  218. }
  219. dev = hw_device_add();
  220. if (!dev) {
  221. err = AVERROR(ENOMEM);
  222. goto fail;
  223. }
  224. dev->name = name;
  225. dev->type = type;
  226. dev->device_ref = device_ref;
  227. if (dev_out)
  228. *dev_out = dev;
  229. return 0;
  230. fail:
  231. av_freep(&name);
  232. av_buffer_unref(&device_ref);
  233. return err;
  234. }
  235. void hw_device_free_all(void)
  236. {
  237. int i;
  238. for (i = 0; i < nb_hw_devices; i++) {
  239. av_freep(&hw_devices[i]->name);
  240. av_buffer_unref(&hw_devices[i]->device_ref);
  241. av_freep(&hw_devices[i]);
  242. }
  243. av_freep(&hw_devices);
  244. nb_hw_devices = 0;
  245. }
  246. static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
  247. {
  248. const AVCodecHWConfig *config;
  249. HWDevice *dev;
  250. int i;
  251. for (i = 0;; i++) {
  252. config = avcodec_get_hw_config(codec, i);
  253. if (!config)
  254. return NULL;
  255. if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  256. continue;
  257. dev = hw_device_get_by_type(config->device_type);
  258. if (dev)
  259. return dev;
  260. }
  261. }
  262. int hw_device_setup_for_decode(InputStream *ist)
  263. {
  264. const AVCodecHWConfig *config;
  265. enum AVHWDeviceType type;
  266. HWDevice *dev = NULL;
  267. int err, auto_device = 0;
  268. if (ist->hwaccel_device) {
  269. dev = hw_device_get_by_name(ist->hwaccel_device);
  270. if (!dev) {
  271. if (ist->hwaccel_id == HWACCEL_AUTO) {
  272. auto_device = 1;
  273. } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
  274. type = ist->hwaccel_device_type;
  275. err = hw_device_init_from_type(type, ist->hwaccel_device,
  276. &dev);
  277. } else {
  278. // This will be dealt with by API-specific initialisation
  279. // (using hwaccel_device), so nothing further needed here.
  280. return 0;
  281. }
  282. } else {
  283. if (ist->hwaccel_id == HWACCEL_AUTO) {
  284. ist->hwaccel_device_type = dev->type;
  285. } else if (ist->hwaccel_device_type != dev->type) {
  286. av_log(ist->dec_ctx, AV_LOG_ERROR, "Invalid hwaccel device "
  287. "specified for decoder: device %s of type %s is not "
  288. "usable with hwaccel %s.\n", dev->name,
  289. av_hwdevice_get_type_name(dev->type),
  290. av_hwdevice_get_type_name(ist->hwaccel_device_type));
  291. return AVERROR(EINVAL);
  292. }
  293. }
  294. } else {
  295. if (ist->hwaccel_id == HWACCEL_AUTO) {
  296. auto_device = 1;
  297. } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
  298. type = ist->hwaccel_device_type;
  299. dev = hw_device_get_by_type(type);
  300. if (!dev)
  301. err = hw_device_init_from_type(type, NULL, &dev);
  302. } else {
  303. dev = hw_device_match_by_codec(ist->dec);
  304. if (!dev) {
  305. // No device for this codec, but not using generic hwaccel
  306. // and therefore may well not need one - ignore.
  307. return 0;
  308. }
  309. }
  310. }
  311. if (auto_device) {
  312. int i;
  313. if (!avcodec_get_hw_config(ist->dec, 0)) {
  314. // Decoder does not support any hardware devices.
  315. return 0;
  316. }
  317. for (i = 0; !dev; i++) {
  318. config = avcodec_get_hw_config(ist->dec, i);
  319. if (!config)
  320. break;
  321. type = config->device_type;
  322. dev = hw_device_get_by_type(type);
  323. if (dev) {
  324. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  325. "hwaccel type %s with existing device %s.\n",
  326. av_hwdevice_get_type_name(type), dev->name);
  327. }
  328. }
  329. for (i = 0; !dev; i++) {
  330. config = avcodec_get_hw_config(ist->dec, i);
  331. if (!config)
  332. break;
  333. type = config->device_type;
  334. // Try to make a new device of this type.
  335. err = hw_device_init_from_type(type, ist->hwaccel_device,
  336. &dev);
  337. if (err < 0) {
  338. // Can't make a device of this type.
  339. continue;
  340. }
  341. if (ist->hwaccel_device) {
  342. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  343. "hwaccel type %s with new device created "
  344. "from %s.\n", av_hwdevice_get_type_name(type),
  345. ist->hwaccel_device);
  346. } else {
  347. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  348. "hwaccel type %s with new default device.\n",
  349. av_hwdevice_get_type_name(type));
  350. }
  351. }
  352. if (dev) {
  353. ist->hwaccel_device_type = type;
  354. } else {
  355. av_log(ist->dec_ctx, AV_LOG_INFO, "Auto hwaccel "
  356. "disabled: no device found.\n");
  357. ist->hwaccel_id = HWACCEL_NONE;
  358. return 0;
  359. }
  360. }
  361. if (!dev) {
  362. av_log(ist->dec_ctx, AV_LOG_ERROR, "No device available "
  363. "for decoder: device type %s needed for codec %s.\n",
  364. av_hwdevice_get_type_name(type), ist->dec->name);
  365. return err;
  366. }
  367. ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  368. if (!ist->dec_ctx->hw_device_ctx)
  369. return AVERROR(ENOMEM);
  370. return 0;
  371. }
  372. int hw_device_setup_for_encode(OutputStream *ost)
  373. {
  374. HWDevice *dev;
  375. dev = hw_device_match_by_codec(ost->enc);
  376. if (dev) {
  377. ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  378. if (!ost->enc_ctx->hw_device_ctx)
  379. return AVERROR(ENOMEM);
  380. return 0;
  381. } else {
  382. // No device required, or no device available.
  383. return 0;
  384. }
  385. }
  386. static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
  387. {
  388. InputStream *ist = avctx->opaque;
  389. AVFrame *output = NULL;
  390. enum AVPixelFormat output_format = ist->hwaccel_output_format;
  391. int err;
  392. if (input->format == output_format) {
  393. // Nothing to do.
  394. return 0;
  395. }
  396. output = av_frame_alloc();
  397. if (!output)
  398. return AVERROR(ENOMEM);
  399. output->format = output_format;
  400. err = av_hwframe_transfer_data(output, input, 0);
  401. if (err < 0) {
  402. av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
  403. "output frame: %d.\n", err);
  404. goto fail;
  405. }
  406. err = av_frame_copy_props(output, input);
  407. if (err < 0) {
  408. av_frame_unref(output);
  409. goto fail;
  410. }
  411. av_frame_unref(input);
  412. av_frame_move_ref(input, output);
  413. av_frame_free(&output);
  414. return 0;
  415. fail:
  416. av_frame_free(&output);
  417. return err;
  418. }
  419. int hwaccel_decode_init(AVCodecContext *avctx)
  420. {
  421. InputStream *ist = avctx->opaque;
  422. ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
  423. return 0;
  424. }