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.

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