ffmpeg_hw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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: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 {
  173. errmsg = "parse error";
  174. goto invalid;
  175. }
  176. dev = hw_device_add();
  177. if (!dev) {
  178. err = AVERROR(ENOMEM);
  179. goto fail;
  180. }
  181. dev->name = name;
  182. dev->type = type;
  183. dev->device_ref = device_ref;
  184. if (dev_out)
  185. *dev_out = dev;
  186. name = NULL;
  187. err = 0;
  188. done:
  189. av_freep(&type_name);
  190. av_freep(&name);
  191. av_freep(&device);
  192. av_dict_free(&options);
  193. return err;
  194. invalid:
  195. av_log(NULL, AV_LOG_ERROR,
  196. "Invalid device specification \"%s\": %s\n", arg, errmsg);
  197. err = AVERROR(EINVAL);
  198. goto done;
  199. fail:
  200. av_log(NULL, AV_LOG_ERROR,
  201. "Device creation failed: %d.\n", err);
  202. av_buffer_unref(&device_ref);
  203. goto done;
  204. }
  205. static int hw_device_init_from_type(enum AVHWDeviceType type,
  206. const char *device,
  207. HWDevice **dev_out)
  208. {
  209. AVBufferRef *device_ref = NULL;
  210. HWDevice *dev;
  211. char *name;
  212. int err;
  213. name = hw_device_default_name(type);
  214. if (!name) {
  215. err = AVERROR(ENOMEM);
  216. goto fail;
  217. }
  218. err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
  219. if (err < 0) {
  220. av_log(NULL, AV_LOG_ERROR,
  221. "Device creation failed: %d.\n", err);
  222. goto fail;
  223. }
  224. dev = hw_device_add();
  225. if (!dev) {
  226. err = AVERROR(ENOMEM);
  227. goto fail;
  228. }
  229. dev->name = name;
  230. dev->type = type;
  231. dev->device_ref = device_ref;
  232. if (dev_out)
  233. *dev_out = dev;
  234. return 0;
  235. fail:
  236. av_freep(&name);
  237. av_buffer_unref(&device_ref);
  238. return err;
  239. }
  240. void hw_device_free_all(void)
  241. {
  242. int i;
  243. for (i = 0; i < nb_hw_devices; i++) {
  244. av_freep(&hw_devices[i]->name);
  245. av_buffer_unref(&hw_devices[i]->device_ref);
  246. av_freep(&hw_devices[i]);
  247. }
  248. av_freep(&hw_devices);
  249. nb_hw_devices = 0;
  250. }
  251. static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
  252. {
  253. const AVCodecHWConfig *config;
  254. HWDevice *dev;
  255. int i;
  256. for (i = 0;; i++) {
  257. config = avcodec_get_hw_config(codec, i);
  258. if (!config)
  259. return NULL;
  260. if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  261. continue;
  262. dev = hw_device_get_by_type(config->device_type);
  263. if (dev)
  264. return dev;
  265. }
  266. }
  267. int hw_device_setup_for_decode(InputStream *ist)
  268. {
  269. const AVCodecHWConfig *config;
  270. enum AVHWDeviceType type;
  271. HWDevice *dev = NULL;
  272. int err, auto_device = 0;
  273. if (ist->hwaccel_device) {
  274. dev = hw_device_get_by_name(ist->hwaccel_device);
  275. if (!dev) {
  276. if (ist->hwaccel_id == HWACCEL_AUTO) {
  277. auto_device = 1;
  278. } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
  279. type = ist->hwaccel_device_type;
  280. err = hw_device_init_from_type(type, ist->hwaccel_device,
  281. &dev);
  282. } else {
  283. // This will be dealt with by API-specific initialisation
  284. // (using hwaccel_device), so nothing further needed here.
  285. return 0;
  286. }
  287. } else {
  288. if (ist->hwaccel_id == HWACCEL_AUTO) {
  289. ist->hwaccel_device_type = dev->type;
  290. } else if (ist->hwaccel_device_type != dev->type) {
  291. av_log(ist->dec_ctx, AV_LOG_ERROR, "Invalid hwaccel device "
  292. "specified for decoder: device %s of type %s is not "
  293. "usable with hwaccel %s.\n", dev->name,
  294. av_hwdevice_get_type_name(dev->type),
  295. av_hwdevice_get_type_name(ist->hwaccel_device_type));
  296. return AVERROR(EINVAL);
  297. }
  298. }
  299. } else {
  300. if (ist->hwaccel_id == HWACCEL_AUTO) {
  301. auto_device = 1;
  302. } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
  303. type = ist->hwaccel_device_type;
  304. dev = hw_device_get_by_type(type);
  305. if (!dev)
  306. err = hw_device_init_from_type(type, NULL, &dev);
  307. } else {
  308. dev = hw_device_match_by_codec(ist->dec);
  309. if (!dev) {
  310. // No device for this codec, but not using generic hwaccel
  311. // and therefore may well not need one - ignore.
  312. return 0;
  313. }
  314. }
  315. }
  316. if (auto_device) {
  317. int i;
  318. if (!avcodec_get_hw_config(ist->dec, 0)) {
  319. // Decoder does not support any hardware devices.
  320. return 0;
  321. }
  322. for (i = 0; !dev; i++) {
  323. config = avcodec_get_hw_config(ist->dec, i);
  324. if (!config)
  325. break;
  326. type = config->device_type;
  327. dev = hw_device_get_by_type(type);
  328. if (dev) {
  329. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  330. "hwaccel type %s with existing device %s.\n",
  331. av_hwdevice_get_type_name(type), dev->name);
  332. }
  333. }
  334. for (i = 0; !dev; i++) {
  335. config = avcodec_get_hw_config(ist->dec, i);
  336. if (!config)
  337. break;
  338. type = config->device_type;
  339. // Try to make a new device of this type.
  340. err = hw_device_init_from_type(type, ist->hwaccel_device,
  341. &dev);
  342. if (err < 0) {
  343. // Can't make a device of this type.
  344. continue;
  345. }
  346. if (ist->hwaccel_device) {
  347. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  348. "hwaccel type %s with new device created "
  349. "from %s.\n", av_hwdevice_get_type_name(type),
  350. ist->hwaccel_device);
  351. } else {
  352. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  353. "hwaccel type %s with new default device.\n",
  354. av_hwdevice_get_type_name(type));
  355. }
  356. }
  357. if (dev) {
  358. ist->hwaccel_device_type = type;
  359. } else {
  360. av_log(ist->dec_ctx, AV_LOG_INFO, "Auto hwaccel "
  361. "disabled: no device found.\n");
  362. ist->hwaccel_id = HWACCEL_NONE;
  363. return 0;
  364. }
  365. }
  366. if (!dev) {
  367. av_log(ist->dec_ctx, AV_LOG_ERROR, "No device available "
  368. "for decoder: device type %s needed for codec %s.\n",
  369. av_hwdevice_get_type_name(type), ist->dec->name);
  370. return err;
  371. }
  372. ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  373. if (!ist->dec_ctx->hw_device_ctx)
  374. return AVERROR(ENOMEM);
  375. return 0;
  376. }
  377. int hw_device_setup_for_encode(OutputStream *ost)
  378. {
  379. const AVCodecHWConfig *config;
  380. HWDevice *dev = NULL;
  381. AVBufferRef *frames_ref = NULL;
  382. int i;
  383. if (ost->filter) {
  384. frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter);
  385. if (frames_ref &&
  386. ((AVHWFramesContext*)frames_ref->data)->format ==
  387. ost->enc_ctx->pix_fmt) {
  388. // Matching format, will try to use hw_frames_ctx.
  389. } else {
  390. frames_ref = NULL;
  391. }
  392. }
  393. for (i = 0;; i++) {
  394. config = avcodec_get_hw_config(ost->enc, i);
  395. if (!config)
  396. break;
  397. if (frames_ref &&
  398. config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
  399. (config->pix_fmt == AV_PIX_FMT_NONE ||
  400. config->pix_fmt == ost->enc_ctx->pix_fmt)) {
  401. av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input "
  402. "frames context (format %s) with %s encoder.\n",
  403. av_get_pix_fmt_name(ost->enc_ctx->pix_fmt),
  404. ost->enc->name);
  405. ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
  406. if (!ost->enc_ctx->hw_frames_ctx)
  407. return AVERROR(ENOMEM);
  408. return 0;
  409. }
  410. if (!dev &&
  411. config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
  412. dev = hw_device_get_by_type(config->device_type);
  413. }
  414. if (dev) {
  415. av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s "
  416. "(type %s) with %s encoder.\n", dev->name,
  417. av_hwdevice_get_type_name(dev->type), ost->enc->name);
  418. ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  419. if (!ost->enc_ctx->hw_device_ctx)
  420. return AVERROR(ENOMEM);
  421. } else {
  422. // No device required, or no device available.
  423. }
  424. return 0;
  425. }
  426. static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
  427. {
  428. InputStream *ist = avctx->opaque;
  429. AVFrame *output = NULL;
  430. enum AVPixelFormat output_format = ist->hwaccel_output_format;
  431. int err;
  432. if (input->format == output_format) {
  433. // Nothing to do.
  434. return 0;
  435. }
  436. output = av_frame_alloc();
  437. if (!output)
  438. return AVERROR(ENOMEM);
  439. output->format = output_format;
  440. err = av_hwframe_transfer_data(output, input, 0);
  441. if (err < 0) {
  442. av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
  443. "output frame: %d.\n", err);
  444. goto fail;
  445. }
  446. err = av_frame_copy_props(output, input);
  447. if (err < 0) {
  448. av_frame_unref(output);
  449. goto fail;
  450. }
  451. av_frame_unref(input);
  452. av_frame_move_ref(input, output);
  453. av_frame_free(&output);
  454. return 0;
  455. fail:
  456. av_frame_free(&output);
  457. return err;
  458. }
  459. int hwaccel_decode_init(AVCodecContext *avctx)
  460. {
  461. InputStream *ist = avctx->opaque;
  462. ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
  463. return 0;
  464. }
  465. int hw_device_setup_for_filter(FilterGraph *fg)
  466. {
  467. HWDevice *dev;
  468. int i;
  469. // If the user has supplied exactly one hardware device then just
  470. // give it straight to every filter for convenience. If more than
  471. // one device is available then the user needs to pick one explcitly
  472. // with the filter_hw_device option.
  473. if (filter_hw_device)
  474. dev = filter_hw_device;
  475. else if (nb_hw_devices == 1)
  476. dev = hw_devices[0];
  477. else
  478. dev = NULL;
  479. if (dev) {
  480. for (i = 0; i < fg->graph->nb_filters; i++) {
  481. fg->graph->filters[i]->hw_device_ctx =
  482. av_buffer_ref(dev->device_ref);
  483. if (!fg->graph->filters[i]->hw_device_ctx)
  484. return AVERROR(ENOMEM);
  485. }
  486. }
  487. return 0;
  488. }