ffmpeg_hw.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. static 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. static 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. static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
  264. {
  265. const AVCodecHWConfig *config;
  266. HWDevice *dev;
  267. int i;
  268. for (i = 0;; i++) {
  269. config = avcodec_get_hw_config(codec, i);
  270. if (!config)
  271. return NULL;
  272. if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  273. continue;
  274. dev = hw_device_get_by_type(config->device_type);
  275. if (dev)
  276. return dev;
  277. }
  278. }
  279. int hw_device_setup_for_decode(InputStream *ist)
  280. {
  281. const AVCodecHWConfig *config;
  282. enum AVHWDeviceType type;
  283. HWDevice *dev = NULL;
  284. int err, auto_device = 0;
  285. if (ist->hwaccel_device) {
  286. dev = hw_device_get_by_name(ist->hwaccel_device);
  287. if (!dev) {
  288. if (ist->hwaccel_id == HWACCEL_AUTO) {
  289. auto_device = 1;
  290. } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
  291. type = ist->hwaccel_device_type;
  292. err = hw_device_init_from_type(type, ist->hwaccel_device,
  293. &dev);
  294. } else {
  295. // This will be dealt with by API-specific initialisation
  296. // (using hwaccel_device), so nothing further needed here.
  297. return 0;
  298. }
  299. } else {
  300. if (ist->hwaccel_id == HWACCEL_AUTO) {
  301. ist->hwaccel_device_type = dev->type;
  302. } else if (ist->hwaccel_device_type != dev->type) {
  303. av_log(NULL, AV_LOG_ERROR, "Invalid hwaccel device "
  304. "specified for decoder: device %s of type %s is not "
  305. "usable with hwaccel %s.\n", dev->name,
  306. av_hwdevice_get_type_name(dev->type),
  307. av_hwdevice_get_type_name(ist->hwaccel_device_type));
  308. return AVERROR(EINVAL);
  309. }
  310. }
  311. } else {
  312. if (ist->hwaccel_id == HWACCEL_AUTO) {
  313. auto_device = 1;
  314. } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
  315. type = ist->hwaccel_device_type;
  316. dev = hw_device_get_by_type(type);
  317. // When "-qsv_device device" is used, an internal QSV device named
  318. // as "__qsv_device" is created. Another QSV device is created too
  319. // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
  320. // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
  321. // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
  322. // To keep back-compatibility with the removed ad-hoc libmfx setup code,
  323. // call hw_device_get_by_name("__qsv_device") to select the internal QSV
  324. // device.
  325. if (!dev && type == AV_HWDEVICE_TYPE_QSV)
  326. dev = hw_device_get_by_name("__qsv_device");
  327. if (!dev)
  328. err = hw_device_init_from_type(type, NULL, &dev);
  329. } else {
  330. dev = hw_device_match_by_codec(ist->dec);
  331. if (!dev) {
  332. // No device for this codec, but not using generic hwaccel
  333. // and therefore may well not need one - ignore.
  334. return 0;
  335. }
  336. }
  337. }
  338. if (auto_device) {
  339. int i;
  340. if (!avcodec_get_hw_config(ist->dec, 0)) {
  341. // Decoder does not support any hardware devices.
  342. return 0;
  343. }
  344. for (i = 0; !dev; i++) {
  345. config = avcodec_get_hw_config(ist->dec, i);
  346. if (!config)
  347. break;
  348. type = config->device_type;
  349. dev = hw_device_get_by_type(type);
  350. if (dev) {
  351. av_log(NULL, AV_LOG_INFO, "Using auto "
  352. "hwaccel type %s with existing device %s.\n",
  353. av_hwdevice_get_type_name(type), dev->name);
  354. }
  355. }
  356. for (i = 0; !dev; i++) {
  357. config = avcodec_get_hw_config(ist->dec, i);
  358. if (!config)
  359. break;
  360. type = config->device_type;
  361. // Try to make a new device of this type.
  362. err = hw_device_init_from_type(type, ist->hwaccel_device,
  363. &dev);
  364. if (err < 0) {
  365. // Can't make a device of this type.
  366. continue;
  367. }
  368. if (ist->hwaccel_device) {
  369. av_log(NULL, AV_LOG_INFO, "Using auto "
  370. "hwaccel type %s with new device created "
  371. "from %s.\n", av_hwdevice_get_type_name(type),
  372. ist->hwaccel_device);
  373. } else {
  374. av_log(NULL, AV_LOG_INFO, "Using auto "
  375. "hwaccel type %s with new default device.\n",
  376. av_hwdevice_get_type_name(type));
  377. }
  378. }
  379. if (dev) {
  380. ist->hwaccel_device_type = type;
  381. } else {
  382. av_log(NULL, AV_LOG_INFO, "Auto hwaccel "
  383. "disabled: no device found.\n");
  384. ist->hwaccel_id = HWACCEL_NONE;
  385. return 0;
  386. }
  387. }
  388. if (!dev) {
  389. av_log(NULL, AV_LOG_ERROR, "No device available "
  390. "for decoder: device type %s needed for codec %s.\n",
  391. av_hwdevice_get_type_name(type), ist->dec->name);
  392. return err;
  393. }
  394. ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  395. if (!ist->dec_ctx->hw_device_ctx)
  396. return AVERROR(ENOMEM);
  397. return 0;
  398. }
  399. int hw_device_setup_for_encode(OutputStream *ost)
  400. {
  401. const AVCodecHWConfig *config;
  402. HWDevice *dev = NULL;
  403. AVBufferRef *frames_ref = NULL;
  404. int i;
  405. if (ost->filter) {
  406. frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter);
  407. if (frames_ref &&
  408. ((AVHWFramesContext*)frames_ref->data)->format ==
  409. ost->enc_ctx->pix_fmt) {
  410. // Matching format, will try to use hw_frames_ctx.
  411. } else {
  412. frames_ref = NULL;
  413. }
  414. }
  415. for (i = 0;; i++) {
  416. config = avcodec_get_hw_config(ost->enc_ctx->codec, i);
  417. if (!config)
  418. break;
  419. if (frames_ref &&
  420. config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
  421. (config->pix_fmt == AV_PIX_FMT_NONE ||
  422. config->pix_fmt == ost->enc_ctx->pix_fmt)) {
  423. av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input "
  424. "frames context (format %s) with %s encoder.\n",
  425. av_get_pix_fmt_name(ost->enc_ctx->pix_fmt),
  426. ost->enc_ctx->codec->name);
  427. ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
  428. if (!ost->enc_ctx->hw_frames_ctx)
  429. return AVERROR(ENOMEM);
  430. return 0;
  431. }
  432. if (!dev &&
  433. config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
  434. dev = hw_device_get_by_type(config->device_type);
  435. }
  436. if (dev) {
  437. av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s "
  438. "(type %s) with %s encoder.\n", dev->name,
  439. av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name);
  440. ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  441. if (!ost->enc_ctx->hw_device_ctx)
  442. return AVERROR(ENOMEM);
  443. } else {
  444. // No device required, or no device available.
  445. }
  446. return 0;
  447. }
  448. static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
  449. {
  450. InputStream *ist = avctx->opaque;
  451. AVFrame *output = NULL;
  452. enum AVPixelFormat output_format = ist->hwaccel_output_format;
  453. int err;
  454. if (input->format == output_format) {
  455. // Nothing to do.
  456. return 0;
  457. }
  458. output = av_frame_alloc();
  459. if (!output)
  460. return AVERROR(ENOMEM);
  461. output->format = output_format;
  462. err = av_hwframe_transfer_data(output, input, 0);
  463. if (err < 0) {
  464. av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
  465. "output frame: %d.\n", err);
  466. goto fail;
  467. }
  468. err = av_frame_copy_props(output, input);
  469. if (err < 0) {
  470. av_frame_unref(output);
  471. goto fail;
  472. }
  473. av_frame_unref(input);
  474. av_frame_move_ref(input, output);
  475. av_frame_free(&output);
  476. return 0;
  477. fail:
  478. av_frame_free(&output);
  479. return err;
  480. }
  481. int hwaccel_decode_init(AVCodecContext *avctx)
  482. {
  483. InputStream *ist = avctx->opaque;
  484. ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
  485. return 0;
  486. }
  487. int hw_device_setup_for_filter(FilterGraph *fg)
  488. {
  489. HWDevice *dev;
  490. int i;
  491. // Pick the last hardware device if the user doesn't pick the device for
  492. // filters explicitly with the filter_hw_device option.
  493. if (filter_hw_device)
  494. dev = filter_hw_device;
  495. else if (nb_hw_devices > 0) {
  496. dev = hw_devices[nb_hw_devices - 1];
  497. if (nb_hw_devices > 1)
  498. av_log(NULL, AV_LOG_WARNING, "There are %d hardware devices. device "
  499. "%s of type %s is picked for filters by default. Set hardware "
  500. "device explicitly with the filter_hw_device option if device "
  501. "%s is not usable for filters.\n",
  502. nb_hw_devices, dev->name,
  503. av_hwdevice_get_type_name(dev->type), dev->name);
  504. } else
  505. dev = NULL;
  506. if (dev) {
  507. for (i = 0; i < fg->graph->nb_filters; i++) {
  508. fg->graph->filters[i]->hw_device_ctx =
  509. av_buffer_ref(dev->device_ref);
  510. if (!fg->graph->filters[i]->hw_device_ctx)
  511. return AVERROR(ENOMEM);
  512. }
  513. }
  514. return 0;
  515. }