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.

388 lines
10KB

  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. int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
  59. {
  60. // "type=name:device,key=value,key2=value2"
  61. // "type:device,key=value,key2=value2"
  62. // -> av_hwdevice_ctx_create()
  63. // "type=name@name"
  64. // "type@name"
  65. // -> av_hwdevice_ctx_create_derived()
  66. AVDictionary *options = NULL;
  67. char *type_name = NULL, *name = NULL, *device = NULL;
  68. enum AVHWDeviceType type;
  69. HWDevice *dev, *src;
  70. AVBufferRef *device_ref;
  71. int err;
  72. const char *errmsg, *p, *q;
  73. size_t k;
  74. k = strcspn(arg, ":=@");
  75. p = arg + k;
  76. type_name = av_strndup(arg, k);
  77. if (!type_name) {
  78. err = AVERROR(ENOMEM);
  79. goto fail;
  80. }
  81. type = av_hwdevice_find_type_by_name(type_name);
  82. if (type == AV_HWDEVICE_TYPE_NONE) {
  83. errmsg = "unknown device type";
  84. goto invalid;
  85. }
  86. if (*p == '=') {
  87. k = strcspn(p + 1, ":@");
  88. name = av_strndup(p + 1, k);
  89. if (!name) {
  90. err = AVERROR(ENOMEM);
  91. goto fail;
  92. }
  93. if (hw_device_get_by_name(name)) {
  94. errmsg = "named device already exists";
  95. goto invalid;
  96. }
  97. p += 1 + k;
  98. } else {
  99. // Give the device an automatic name of the form "type%d".
  100. // We arbitrarily limit at 1000 anonymous devices of the same
  101. // type - there is probably something else very wrong if you
  102. // get to this limit.
  103. size_t index_pos;
  104. int index, index_limit = 1000;
  105. index_pos = strlen(type_name);
  106. name = av_malloc(index_pos + 4);
  107. if (!name) {
  108. err = AVERROR(ENOMEM);
  109. goto fail;
  110. }
  111. for (index = 0; index < index_limit; index++) {
  112. snprintf(name, index_pos + 4, "%s%d", type_name, index);
  113. if (!hw_device_get_by_name(name))
  114. break;
  115. }
  116. if (index >= index_limit) {
  117. errmsg = "too many devices";
  118. goto invalid;
  119. }
  120. }
  121. if (!*p) {
  122. // New device with no parameters.
  123. err = av_hwdevice_ctx_create(&device_ref, type,
  124. NULL, NULL, 0);
  125. if (err < 0)
  126. goto fail;
  127. } else if (*p == ':') {
  128. // New device with some parameters.
  129. ++p;
  130. q = strchr(p, ',');
  131. if (q) {
  132. device = av_strndup(p, q - p);
  133. if (!device) {
  134. err = AVERROR(ENOMEM);
  135. goto fail;
  136. }
  137. err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
  138. if (err < 0) {
  139. errmsg = "failed to parse options";
  140. goto invalid;
  141. }
  142. }
  143. err = av_hwdevice_ctx_create(&device_ref, type,
  144. device ? device : p, options, 0);
  145. if (err < 0)
  146. goto fail;
  147. } else if (*p == '@') {
  148. // Derive from existing device.
  149. src = hw_device_get_by_name(p + 1);
  150. if (!src) {
  151. errmsg = "invalid source device name";
  152. goto invalid;
  153. }
  154. err = av_hwdevice_ctx_create_derived(&device_ref, type,
  155. src->device_ref, 0);
  156. if (err < 0)
  157. goto fail;
  158. } else {
  159. errmsg = "parse error";
  160. goto invalid;
  161. }
  162. dev = hw_device_add();
  163. if (!dev) {
  164. err = AVERROR(ENOMEM);
  165. goto fail;
  166. }
  167. dev->name = name;
  168. dev->type = type;
  169. dev->device_ref = device_ref;
  170. if (dev_out)
  171. *dev_out = dev;
  172. name = NULL;
  173. err = 0;
  174. done:
  175. av_freep(&type_name);
  176. av_freep(&name);
  177. av_freep(&device);
  178. av_dict_free(&options);
  179. return err;
  180. invalid:
  181. av_log(NULL, AV_LOG_ERROR,
  182. "Invalid device specification \"%s\": %s\n", arg, errmsg);
  183. err = AVERROR(EINVAL);
  184. goto done;
  185. fail:
  186. av_log(NULL, AV_LOG_ERROR,
  187. "Device creation failed: %d.\n", err);
  188. goto done;
  189. }
  190. void hw_device_free_all(void)
  191. {
  192. int i;
  193. for (i = 0; i < nb_hw_devices; i++) {
  194. av_freep(&hw_devices[i]->name);
  195. av_buffer_unref(&hw_devices[i]->device_ref);
  196. av_freep(&hw_devices[i]);
  197. }
  198. av_freep(&hw_devices);
  199. nb_hw_devices = 0;
  200. }
  201. static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
  202. {
  203. int i;
  204. if (hwaccel_id == HWACCEL_NONE)
  205. return AV_HWDEVICE_TYPE_NONE;
  206. for (i = 0; hwaccels[i].name; i++) {
  207. if (hwaccels[i].id == hwaccel_id)
  208. return hwaccels[i].device_type;
  209. }
  210. return AV_HWDEVICE_TYPE_NONE;
  211. }
  212. static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
  213. {
  214. const char *type_name;
  215. enum AVHWDeviceType type;
  216. for (type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);
  217. type != AV_HWDEVICE_TYPE_NONE;
  218. type = av_hwdevice_iterate_types(type)) {
  219. type_name = av_hwdevice_get_type_name(type);
  220. if (strstr(codec_name, type_name))
  221. return type;
  222. }
  223. return AV_HWDEVICE_TYPE_NONE;
  224. }
  225. int hw_device_setup_for_decode(InputStream *ist)
  226. {
  227. enum AVHWDeviceType type;
  228. HWDevice *dev;
  229. const char *type_name;
  230. int err;
  231. if (ist->hwaccel_device) {
  232. dev = hw_device_get_by_name(ist->hwaccel_device);
  233. if (!dev) {
  234. char *tmp;
  235. size_t len;
  236. type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
  237. if (type == AV_HWDEVICE_TYPE_NONE) {
  238. // No match - this isn't necessarily invalid, though,
  239. // because an explicit device might not be needed or
  240. // the hwaccel setup could be handled elsewhere.
  241. return 0;
  242. }
  243. type_name = av_hwdevice_get_type_name(type);
  244. len = strlen(type_name) + 1 +
  245. strlen(ist->hwaccel_device) + 1;
  246. tmp = av_malloc(len);
  247. if (!tmp)
  248. return AVERROR(ENOMEM);
  249. snprintf(tmp, len, "%s:%s", type_name, ist->hwaccel_device);
  250. err = hw_device_init_from_string(tmp, &dev);
  251. av_free(tmp);
  252. if (err < 0)
  253. return err;
  254. }
  255. } else {
  256. if (ist->hwaccel_id != HWACCEL_NONE)
  257. type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
  258. else
  259. type = hw_device_match_type_in_name(ist->dec->name);
  260. if (type != AV_HWDEVICE_TYPE_NONE) {
  261. dev = hw_device_get_by_type(type);
  262. if (!dev) {
  263. hw_device_init_from_string(av_hwdevice_get_type_name(type),
  264. &dev);
  265. }
  266. } else {
  267. // No device required.
  268. return 0;
  269. }
  270. }
  271. if (!dev) {
  272. av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available "
  273. "for decoder (device type %s for codec %s).\n",
  274. av_hwdevice_get_type_name(type), ist->dec->name);
  275. return 0;
  276. }
  277. ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  278. if (!ist->dec_ctx->hw_device_ctx)
  279. return AVERROR(ENOMEM);
  280. return 0;
  281. }
  282. int hw_device_setup_for_encode(OutputStream *ost)
  283. {
  284. enum AVHWDeviceType type;
  285. HWDevice *dev;
  286. type = hw_device_match_type_in_name(ost->enc->name);
  287. if (type != AV_HWDEVICE_TYPE_NONE) {
  288. dev = hw_device_get_by_type(type);
  289. if (!dev) {
  290. av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available "
  291. "for encoder (device type %s for codec %s).\n",
  292. av_hwdevice_get_type_name(type), ost->enc->name);
  293. return 0;
  294. }
  295. ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  296. if (!ost->enc_ctx->hw_device_ctx)
  297. return AVERROR(ENOMEM);
  298. return 0;
  299. } else {
  300. // No device required.
  301. return 0;
  302. }
  303. }
  304. static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
  305. {
  306. InputStream *ist = avctx->opaque;
  307. AVFrame *output = NULL;
  308. enum AVPixelFormat output_format = ist->hwaccel_output_format;
  309. int err;
  310. if (input->format == output_format) {
  311. // Nothing to do.
  312. return 0;
  313. }
  314. output = av_frame_alloc();
  315. if (!output)
  316. return AVERROR(ENOMEM);
  317. output->format = output_format;
  318. err = av_hwframe_transfer_data(output, input, 0);
  319. if (err < 0) {
  320. av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
  321. "output frame: %d.\n", err);
  322. goto fail;
  323. }
  324. err = av_frame_copy_props(output, input);
  325. if (err < 0) {
  326. av_frame_unref(output);
  327. goto fail;
  328. }
  329. av_frame_unref(input);
  330. av_frame_move_ref(input, output);
  331. av_frame_free(&output);
  332. return 0;
  333. fail:
  334. av_frame_free(&output);
  335. return err;
  336. }
  337. int hwaccel_decode_init(AVCodecContext *avctx)
  338. {
  339. InputStream *ist = avctx->opaque;
  340. ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
  341. return 0;
  342. }