You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

543 lines
15KB

  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 "libavfilter/buffersink.h"
  21. #include "ffmpeg.h"
  22. static int nb_hw_devices;
  23. static HWDevice **hw_devices;
  24. static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
  25. {
  26. HWDevice *found = NULL;
  27. int i;
  28. for (i = 0; i < nb_hw_devices; i++) {
  29. if (hw_devices[i]->type == type) {
  30. if (found)
  31. return NULL;
  32. found = hw_devices[i];
  33. }
  34. }
  35. return found;
  36. }
  37. HWDevice *hw_device_get_by_name(const char *name)
  38. {
  39. int i;
  40. for (i = 0; i < nb_hw_devices; i++) {
  41. if (!strcmp(hw_devices[i]->name, name))
  42. return hw_devices[i];
  43. }
  44. return NULL;
  45. }
  46. static HWDevice *hw_device_add(void)
  47. {
  48. int err;
  49. err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
  50. sizeof(*hw_devices));
  51. if (err) {
  52. nb_hw_devices = 0;
  53. return NULL;
  54. }
  55. hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
  56. if (!hw_devices[nb_hw_devices])
  57. return NULL;
  58. return hw_devices[nb_hw_devices++];
  59. }
  60. static char *hw_device_default_name(enum AVHWDeviceType type)
  61. {
  62. // Make an automatic name of the form "type%d". We arbitrarily
  63. // limit at 1000 anonymous devices of the same type - there is
  64. // probably something else very wrong if you get to this limit.
  65. const char *type_name = av_hwdevice_get_type_name(type);
  66. char *name;
  67. size_t index_pos;
  68. int index, index_limit = 1000;
  69. index_pos = strlen(type_name);
  70. name = av_malloc(index_pos + 4);
  71. if (!name)
  72. return NULL;
  73. for (index = 0; index < index_limit; index++) {
  74. snprintf(name, index_pos + 4, "%s%d", type_name, index);
  75. if (!hw_device_get_by_name(name))
  76. break;
  77. }
  78. if (index >= index_limit) {
  79. av_freep(&name);
  80. return NULL;
  81. }
  82. return name;
  83. }
  84. int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
  85. {
  86. // "type=name:device,key=value,key2=value2"
  87. // "type:device,key=value,key2=value2"
  88. // -> av_hwdevice_ctx_create()
  89. // "type=name@name"
  90. // "type@name"
  91. // -> av_hwdevice_ctx_create_derived()
  92. AVDictionary *options = NULL;
  93. const char *type_name = NULL, *name = NULL, *device = NULL;
  94. enum AVHWDeviceType type;
  95. HWDevice *dev, *src;
  96. AVBufferRef *device_ref = NULL;
  97. int err;
  98. const char *errmsg, *p, *q;
  99. size_t k;
  100. k = strcspn(arg, ":=@");
  101. p = arg + k;
  102. type_name = av_strndup(arg, k);
  103. if (!type_name) {
  104. err = AVERROR(ENOMEM);
  105. goto fail;
  106. }
  107. type = av_hwdevice_find_type_by_name(type_name);
  108. if (type == AV_HWDEVICE_TYPE_NONE) {
  109. errmsg = "unknown device type";
  110. goto invalid;
  111. }
  112. if (*p == '=') {
  113. k = strcspn(p + 1, ":@");
  114. name = av_strndup(p + 1, k);
  115. if (!name) {
  116. err = AVERROR(ENOMEM);
  117. goto fail;
  118. }
  119. if (hw_device_get_by_name(name)) {
  120. errmsg = "named device already exists";
  121. goto invalid;
  122. }
  123. p += 1 + k;
  124. } else {
  125. name = hw_device_default_name(type);
  126. if (!name) {
  127. err = AVERROR(ENOMEM);
  128. goto fail;
  129. }
  130. }
  131. if (!*p) {
  132. // New device with no parameters.
  133. err = av_hwdevice_ctx_create(&device_ref, type,
  134. NULL, NULL, 0);
  135. if (err < 0)
  136. goto fail;
  137. } else if (*p == ':') {
  138. // New device with some parameters.
  139. ++p;
  140. q = strchr(p, ',');
  141. if (q) {
  142. if (q - p > 0) {
  143. device = av_strndup(p, q - p);
  144. if (!device) {
  145. err = AVERROR(ENOMEM);
  146. goto fail;
  147. }
  148. }
  149. err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
  150. if (err < 0) {
  151. errmsg = "failed to parse options";
  152. goto invalid;
  153. }
  154. }
  155. err = av_hwdevice_ctx_create(&device_ref, type,
  156. q ? device : p[0] ? p : NULL,
  157. options, 0);
  158. if (err < 0)
  159. goto fail;
  160. } else if (*p == '@') {
  161. // Derive from existing device.
  162. src = hw_device_get_by_name(p + 1);
  163. if (!src) {
  164. errmsg = "invalid source device name";
  165. goto invalid;
  166. }
  167. err = av_hwdevice_ctx_create_derived(&device_ref, type,
  168. src->device_ref, 0);
  169. if (err < 0)
  170. goto fail;
  171. } else {
  172. errmsg = "parse error";
  173. goto invalid;
  174. }
  175. dev = hw_device_add();
  176. if (!dev) {
  177. err = AVERROR(ENOMEM);
  178. goto fail;
  179. }
  180. dev->name = name;
  181. dev->type = type;
  182. dev->device_ref = device_ref;
  183. if (dev_out)
  184. *dev_out = dev;
  185. name = NULL;
  186. err = 0;
  187. done:
  188. av_freep(&type_name);
  189. av_freep(&name);
  190. av_freep(&device);
  191. av_dict_free(&options);
  192. return err;
  193. invalid:
  194. av_log(NULL, AV_LOG_ERROR,
  195. "Invalid device specification \"%s\": %s\n", arg, errmsg);
  196. err = AVERROR(EINVAL);
  197. goto done;
  198. fail:
  199. av_log(NULL, AV_LOG_ERROR,
  200. "Device creation failed: %d.\n", err);
  201. av_buffer_unref(&device_ref);
  202. goto done;
  203. }
  204. static int hw_device_init_from_type(enum AVHWDeviceType type,
  205. const char *device,
  206. HWDevice **dev_out)
  207. {
  208. AVBufferRef *device_ref = NULL;
  209. HWDevice *dev;
  210. char *name;
  211. int err;
  212. name = hw_device_default_name(type);
  213. if (!name) {
  214. err = AVERROR(ENOMEM);
  215. goto fail;
  216. }
  217. err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
  218. if (err < 0) {
  219. av_log(NULL, AV_LOG_ERROR,
  220. "Device creation failed: %d.\n", err);
  221. goto fail;
  222. }
  223. dev = hw_device_add();
  224. if (!dev) {
  225. err = AVERROR(ENOMEM);
  226. goto fail;
  227. }
  228. dev->name = name;
  229. dev->type = type;
  230. dev->device_ref = device_ref;
  231. if (dev_out)
  232. *dev_out = dev;
  233. return 0;
  234. fail:
  235. av_freep(&name);
  236. av_buffer_unref(&device_ref);
  237. return err;
  238. }
  239. void hw_device_free_all(void)
  240. {
  241. int i;
  242. for (i = 0; i < nb_hw_devices; i++) {
  243. av_freep(&hw_devices[i]->name);
  244. av_buffer_unref(&hw_devices[i]->device_ref);
  245. av_freep(&hw_devices[i]);
  246. }
  247. av_freep(&hw_devices);
  248. nb_hw_devices = 0;
  249. }
  250. static HWDevice *hw_device_match_by_codec(const AVCodec *codec,
  251. enum AVPixelFormat format,
  252. int possible_methods,
  253. int *matched_methods)
  254. {
  255. const AVCodecHWConfig *config;
  256. HWDevice *dev;
  257. int i;
  258. for (i = 0;; i++) {
  259. config = avcodec_get_hw_config(codec, i);
  260. if (!config)
  261. return NULL;
  262. if (format != AV_PIX_FMT_NONE &&
  263. config->pix_fmt != AV_PIX_FMT_NONE &&
  264. config->pix_fmt != format)
  265. continue;
  266. if (!(config->methods & possible_methods))
  267. continue;
  268. dev = hw_device_get_by_type(config->device_type);
  269. if (dev) {
  270. if (matched_methods)
  271. *matched_methods = config->methods & possible_methods;
  272. return dev;
  273. }
  274. }
  275. }
  276. int hw_device_setup_for_decode(InputStream *ist)
  277. {
  278. const AVCodecHWConfig *config;
  279. enum AVHWDeviceType type;
  280. HWDevice *dev = NULL;
  281. int err, auto_device = 0;
  282. if (ist->hwaccel_device) {
  283. dev = hw_device_get_by_name(ist->hwaccel_device);
  284. if (!dev) {
  285. if (ist->hwaccel_id == HWACCEL_AUTO) {
  286. auto_device = 1;
  287. } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
  288. type = ist->hwaccel_device_type;
  289. err = hw_device_init_from_type(type, ist->hwaccel_device,
  290. &dev);
  291. } else {
  292. // This will be dealt with by API-specific initialisation
  293. // (using hwaccel_device), so nothing further needed here.
  294. return 0;
  295. }
  296. } else {
  297. if (ist->hwaccel_id == HWACCEL_AUTO) {
  298. ist->hwaccel_device_type = dev->type;
  299. } else if (ist->hwaccel_device_type != dev->type) {
  300. av_log(ist->dec_ctx, AV_LOG_ERROR, "Invalid hwaccel device "
  301. "specified for decoder: device %s of type %s is not "
  302. "usable with hwaccel %s.\n", dev->name,
  303. av_hwdevice_get_type_name(dev->type),
  304. av_hwdevice_get_type_name(ist->hwaccel_device_type));
  305. return AVERROR(EINVAL);
  306. }
  307. }
  308. } else {
  309. if (ist->hwaccel_id == HWACCEL_AUTO) {
  310. auto_device = 1;
  311. } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
  312. type = ist->hwaccel_device_type;
  313. dev = hw_device_get_by_type(type);
  314. if (!dev)
  315. err = hw_device_init_from_type(type, NULL, &dev);
  316. } else {
  317. dev = hw_device_match_by_codec(ist->dec, AV_PIX_FMT_NONE,
  318. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
  319. NULL);
  320. if (!dev) {
  321. // No device for this codec, but not using generic hwaccel
  322. // and therefore may well not need one - ignore.
  323. return 0;
  324. }
  325. }
  326. }
  327. if (auto_device) {
  328. int i;
  329. if (!avcodec_get_hw_config(ist->dec, 0)) {
  330. // Decoder does not support any hardware devices.
  331. return 0;
  332. }
  333. for (i = 0; !dev; i++) {
  334. config = avcodec_get_hw_config(ist->dec, i);
  335. if (!config)
  336. break;
  337. type = config->device_type;
  338. dev = hw_device_get_by_type(type);
  339. if (dev) {
  340. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  341. "hwaccel type %s with existing device %s.\n",
  342. av_hwdevice_get_type_name(type), dev->name);
  343. }
  344. }
  345. for (i = 0; !dev; i++) {
  346. config = avcodec_get_hw_config(ist->dec, i);
  347. if (!config)
  348. break;
  349. type = config->device_type;
  350. // Try to make a new device of this type.
  351. err = hw_device_init_from_type(type, ist->hwaccel_device,
  352. &dev);
  353. if (err < 0) {
  354. // Can't make a device of this type.
  355. continue;
  356. }
  357. if (ist->hwaccel_device) {
  358. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  359. "hwaccel type %s with new device created "
  360. "from %s.\n", av_hwdevice_get_type_name(type),
  361. ist->hwaccel_device);
  362. } else {
  363. av_log(ist->dec_ctx, AV_LOG_INFO, "Using auto "
  364. "hwaccel type %s with new default device.\n",
  365. av_hwdevice_get_type_name(type));
  366. }
  367. }
  368. if (dev) {
  369. ist->hwaccel_device_type = type;
  370. } else {
  371. av_log(ist->dec_ctx, AV_LOG_INFO, "Auto hwaccel "
  372. "disabled: no device found.\n");
  373. ist->hwaccel_id = HWACCEL_NONE;
  374. return 0;
  375. }
  376. }
  377. if (!dev) {
  378. av_log(ist->dec_ctx, AV_LOG_ERROR, "No device available "
  379. "for decoder: device type %s needed for codec %s.\n",
  380. av_hwdevice_get_type_name(type), ist->dec->name);
  381. return err;
  382. }
  383. ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  384. if (!ist->dec_ctx->hw_device_ctx)
  385. return AVERROR(ENOMEM);
  386. return 0;
  387. }
  388. int hw_device_setup_for_encode(OutputStream *ost)
  389. {
  390. HWDevice *dev;
  391. AVBufferRef *frames_ref;
  392. int methods = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX;
  393. int matched_methods;
  394. if (ost->filter) {
  395. frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter);
  396. if (frames_ref &&
  397. ((AVHWFramesContext*)frames_ref->data)->format ==
  398. ost->enc_ctx->pix_fmt)
  399. methods |= AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX;
  400. }
  401. dev = hw_device_match_by_codec(ost->enc, ost->enc_ctx->pix_fmt,
  402. methods, &matched_methods);
  403. if (dev) {
  404. if (matched_methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) {
  405. ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  406. if (!ost->enc_ctx->hw_device_ctx)
  407. return AVERROR(ENOMEM);
  408. }
  409. if (matched_methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX) {
  410. ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
  411. if (!ost->enc_ctx->hw_frames_ctx)
  412. return AVERROR(ENOMEM);
  413. }
  414. return 0;
  415. } else {
  416. // No device required, or no device available.
  417. return 0;
  418. }
  419. }
  420. static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
  421. {
  422. InputStream *ist = avctx->opaque;
  423. AVFrame *output = NULL;
  424. enum AVPixelFormat output_format = ist->hwaccel_output_format;
  425. int err;
  426. if (input->format == output_format) {
  427. // Nothing to do.
  428. return 0;
  429. }
  430. output = av_frame_alloc();
  431. if (!output)
  432. return AVERROR(ENOMEM);
  433. output->format = output_format;
  434. err = av_hwframe_transfer_data(output, input, 0);
  435. if (err < 0) {
  436. av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
  437. "output frame: %d.\n", err);
  438. goto fail;
  439. }
  440. err = av_frame_copy_props(output, input);
  441. if (err < 0) {
  442. av_frame_unref(output);
  443. goto fail;
  444. }
  445. av_frame_unref(input);
  446. av_frame_move_ref(input, output);
  447. av_frame_free(&output);
  448. return 0;
  449. fail:
  450. av_frame_free(&output);
  451. return err;
  452. }
  453. int hwaccel_decode_init(AVCodecContext *avctx)
  454. {
  455. InputStream *ist = avctx->opaque;
  456. ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
  457. return 0;
  458. }
  459. int hw_device_setup_for_filter(FilterGraph *fg)
  460. {
  461. HWDevice *dev;
  462. int i;
  463. // If the user has supplied exactly one hardware device then just
  464. // give it straight to every filter for convenience. If more than
  465. // one device is available then the user needs to pick one explcitly
  466. // with the filter_hw_device option.
  467. if (filter_hw_device)
  468. dev = filter_hw_device;
  469. else if (nb_hw_devices == 1)
  470. dev = hw_devices[0];
  471. else
  472. dev = NULL;
  473. if (dev) {
  474. for (i = 0; i < fg->graph->nb_filters; i++) {
  475. fg->graph->filters[i]->hw_device_ctx =
  476. av_buffer_ref(dev->device_ref);
  477. if (!fg->graph->filters[i]->hw_device_ctx)
  478. return AVERROR(ENOMEM);
  479. }
  480. }
  481. return 0;
  482. }