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.

389 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 = NULL;
  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. av_buffer_unref(&device_ref);
  189. goto done;
  190. }
  191. void hw_device_free_all(void)
  192. {
  193. int i;
  194. for (i = 0; i < nb_hw_devices; i++) {
  195. av_freep(&hw_devices[i]->name);
  196. av_buffer_unref(&hw_devices[i]->device_ref);
  197. av_freep(&hw_devices[i]);
  198. }
  199. av_freep(&hw_devices);
  200. nb_hw_devices = 0;
  201. }
  202. static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
  203. {
  204. int i;
  205. if (hwaccel_id == HWACCEL_NONE)
  206. return AV_HWDEVICE_TYPE_NONE;
  207. for (i = 0; hwaccels[i].name; i++) {
  208. if (hwaccels[i].id == hwaccel_id)
  209. return hwaccels[i].device_type;
  210. }
  211. return AV_HWDEVICE_TYPE_NONE;
  212. }
  213. static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
  214. {
  215. const char *type_name;
  216. enum AVHWDeviceType type;
  217. for (type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);
  218. type != AV_HWDEVICE_TYPE_NONE;
  219. type = av_hwdevice_iterate_types(type)) {
  220. type_name = av_hwdevice_get_type_name(type);
  221. if (strstr(codec_name, type_name))
  222. return type;
  223. }
  224. return AV_HWDEVICE_TYPE_NONE;
  225. }
  226. int hw_device_setup_for_decode(InputStream *ist)
  227. {
  228. enum AVHWDeviceType type;
  229. HWDevice *dev;
  230. const char *type_name;
  231. int err;
  232. if (ist->hwaccel_device) {
  233. dev = hw_device_get_by_name(ist->hwaccel_device);
  234. if (!dev) {
  235. char *tmp;
  236. size_t len;
  237. type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
  238. if (type == AV_HWDEVICE_TYPE_NONE) {
  239. // No match - this isn't necessarily invalid, though,
  240. // because an explicit device might not be needed or
  241. // the hwaccel setup could be handled elsewhere.
  242. return 0;
  243. }
  244. type_name = av_hwdevice_get_type_name(type);
  245. len = strlen(type_name) + 1 +
  246. strlen(ist->hwaccel_device) + 1;
  247. tmp = av_malloc(len);
  248. if (!tmp)
  249. return AVERROR(ENOMEM);
  250. snprintf(tmp, len, "%s:%s", type_name, ist->hwaccel_device);
  251. err = hw_device_init_from_string(tmp, &dev);
  252. av_free(tmp);
  253. if (err < 0)
  254. return err;
  255. }
  256. } else {
  257. if (ist->hwaccel_id != HWACCEL_NONE)
  258. type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
  259. else
  260. type = hw_device_match_type_in_name(ist->dec->name);
  261. if (type != AV_HWDEVICE_TYPE_NONE) {
  262. dev = hw_device_get_by_type(type);
  263. if (!dev) {
  264. hw_device_init_from_string(av_hwdevice_get_type_name(type),
  265. &dev);
  266. }
  267. } else {
  268. // No device required.
  269. return 0;
  270. }
  271. }
  272. if (!dev) {
  273. av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available "
  274. "for decoder (device type %s for codec %s).\n",
  275. av_hwdevice_get_type_name(type), ist->dec->name);
  276. return 0;
  277. }
  278. ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  279. if (!ist->dec_ctx->hw_device_ctx)
  280. return AVERROR(ENOMEM);
  281. return 0;
  282. }
  283. int hw_device_setup_for_encode(OutputStream *ost)
  284. {
  285. enum AVHWDeviceType type;
  286. HWDevice *dev;
  287. type = hw_device_match_type_in_name(ost->enc->name);
  288. if (type != AV_HWDEVICE_TYPE_NONE) {
  289. dev = hw_device_get_by_type(type);
  290. if (!dev) {
  291. av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available "
  292. "for encoder (device type %s for codec %s).\n",
  293. av_hwdevice_get_type_name(type), ost->enc->name);
  294. return 0;
  295. }
  296. ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  297. if (!ost->enc_ctx->hw_device_ctx)
  298. return AVERROR(ENOMEM);
  299. return 0;
  300. } else {
  301. // No device required.
  302. return 0;
  303. }
  304. }
  305. static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
  306. {
  307. InputStream *ist = avctx->opaque;
  308. AVFrame *output = NULL;
  309. enum AVPixelFormat output_format = ist->hwaccel_output_format;
  310. int err;
  311. if (input->format == output_format) {
  312. // Nothing to do.
  313. return 0;
  314. }
  315. output = av_frame_alloc();
  316. if (!output)
  317. return AVERROR(ENOMEM);
  318. output->format = output_format;
  319. err = av_hwframe_transfer_data(output, input, 0);
  320. if (err < 0) {
  321. av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
  322. "output frame: %d.\n", err);
  323. goto fail;
  324. }
  325. err = av_frame_copy_props(output, input);
  326. if (err < 0) {
  327. av_frame_unref(output);
  328. goto fail;
  329. }
  330. av_frame_unref(input);
  331. av_frame_move_ref(input, output);
  332. av_frame_free(&output);
  333. return 0;
  334. fail:
  335. av_frame_free(&output);
  336. return err;
  337. }
  338. int hwaccel_decode_init(AVCodecContext *avctx)
  339. {
  340. InputStream *ist = avctx->opaque;
  341. ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
  342. return 0;
  343. }