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.

385 lines
10KB

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