ffmpeg_hw.c 13 KB

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