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.

478 lines
13KB

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