vf_hwmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "libavutil/buffer.h"
  19. #include "libavutil/hwcontext.h"
  20. #include "libavutil/log.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct HWMapContext {
  28. const AVClass *class;
  29. AVBufferRef *hwframes_ref;
  30. int mode;
  31. char *derive_device_type;
  32. int reverse;
  33. } HWMapContext;
  34. static int hwmap_query_formats(AVFilterContext *avctx)
  35. {
  36. int ret;
  37. if ((ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
  38. &avctx->inputs[0]->out_formats)) < 0 ||
  39. (ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
  40. &avctx->outputs[0]->in_formats)) < 0)
  41. return ret;
  42. return 0;
  43. }
  44. static int hwmap_config_output(AVFilterLink *outlink)
  45. {
  46. AVFilterContext *avctx = outlink->src;
  47. HWMapContext *ctx = avctx->priv;
  48. AVFilterLink *inlink = avctx->inputs[0];
  49. AVHWFramesContext *hwfc;
  50. AVBufferRef *device;
  51. const AVPixFmtDescriptor *desc;
  52. int err, device_is_derived;
  53. av_log(avctx, AV_LOG_DEBUG, "Configure hwmap %s -> %s.\n",
  54. av_get_pix_fmt_name(inlink->format),
  55. av_get_pix_fmt_name(outlink->format));
  56. av_buffer_unref(&ctx->hwframes_ref);
  57. device = avctx->hw_device_ctx;
  58. device_is_derived = 0;
  59. if (inlink->hw_frames_ctx) {
  60. hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
  61. if (ctx->derive_device_type) {
  62. enum AVHWDeviceType type;
  63. type = av_hwdevice_find_type_by_name(ctx->derive_device_type);
  64. if (type == AV_HWDEVICE_TYPE_NONE) {
  65. av_log(avctx, AV_LOG_ERROR, "Invalid device type.\n");
  66. err = AVERROR(EINVAL);
  67. goto fail;
  68. }
  69. err = av_hwdevice_ctx_create_derived(&device, type,
  70. hwfc->device_ref, 0);
  71. if (err < 0) {
  72. av_log(avctx, AV_LOG_ERROR, "Failed to created derived "
  73. "device context: %d.\n", err);
  74. goto fail;
  75. }
  76. device_is_derived = 1;
  77. }
  78. desc = av_pix_fmt_desc_get(outlink->format);
  79. if (!desc) {
  80. err = AVERROR(EINVAL);
  81. goto fail;
  82. }
  83. if (inlink->format == hwfc->format &&
  84. (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
  85. !ctx->reverse) {
  86. // Map between two hardware formats (including the case of
  87. // undoing an existing mapping).
  88. if (!device) {
  89. av_log(avctx, AV_LOG_ERROR, "A device reference is "
  90. "required to map to a hardware format.\n");
  91. err = AVERROR(EINVAL);
  92. goto fail;
  93. }
  94. err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref,
  95. outlink->format,
  96. device,
  97. inlink->hw_frames_ctx,
  98. ctx->mode);
  99. if (err < 0) {
  100. av_log(avctx, AV_LOG_ERROR, "Failed to create derived "
  101. "frames context: %d.\n", err);
  102. goto fail;
  103. }
  104. } else if (inlink->format == hwfc->format &&
  105. (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
  106. ctx->reverse) {
  107. // Map between two hardware formats, but do it in reverse.
  108. // Make a new hwframe context for the target type, and then
  109. // overwrite the input hwframe context with a derived context
  110. // mapped from that back to the source type.
  111. AVBufferRef *source;
  112. AVHWFramesContext *frames;
  113. ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
  114. if (!ctx->hwframes_ref) {
  115. err = AVERROR(ENOMEM);
  116. goto fail;
  117. }
  118. frames = (AVHWFramesContext*)ctx->hwframes_ref->data;
  119. frames->format = outlink->format;
  120. frames->sw_format = hwfc->sw_format;
  121. frames->width = hwfc->width;
  122. frames->height = hwfc->height;
  123. if (avctx->extra_hw_frames >= 0)
  124. frames->initial_pool_size = 2 + avctx->extra_hw_frames;
  125. err = av_hwframe_ctx_init(ctx->hwframes_ref);
  126. if (err < 0) {
  127. av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
  128. "target frames context: %d.\n", err);
  129. goto fail;
  130. }
  131. err = av_hwframe_ctx_create_derived(&source,
  132. inlink->format,
  133. hwfc->device_ref,
  134. ctx->hwframes_ref,
  135. ctx->mode);
  136. if (err < 0) {
  137. av_log(avctx, AV_LOG_ERROR, "Failed to create "
  138. "derived source frames context: %d.\n", err);
  139. goto fail;
  140. }
  141. // Here is the naughty bit. This overwriting changes what
  142. // ff_get_video_buffer() in the previous filter returns -
  143. // it will now give a frame allocated here mapped back to
  144. // the format it expects. If there were any additional
  145. // constraints on the output frames there then this may
  146. // break nastily.
  147. av_buffer_unref(&inlink->hw_frames_ctx);
  148. inlink->hw_frames_ctx = source;
  149. } else if ((outlink->format == hwfc->format &&
  150. inlink->format == hwfc->sw_format) ||
  151. inlink->format == hwfc->format) {
  152. // Map from a hardware format to a software format, or
  153. // undo an existing such mapping.
  154. ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
  155. if (!ctx->hwframes_ref) {
  156. err = AVERROR(ENOMEM);
  157. goto fail;
  158. }
  159. } else {
  160. // Non-matching formats - not supported.
  161. av_log(avctx, AV_LOG_ERROR, "Unsupported formats for "
  162. "hwmap: from %s (%s) to %s.\n",
  163. av_get_pix_fmt_name(inlink->format),
  164. av_get_pix_fmt_name(hwfc->format),
  165. av_get_pix_fmt_name(outlink->format));
  166. err = AVERROR(EINVAL);
  167. goto fail;
  168. }
  169. } else if (avctx->hw_device_ctx) {
  170. // Map from a software format to a hardware format. This
  171. // creates a new hwframe context like hwupload, but then
  172. // returns frames mapped from that to the previous link in
  173. // order to fill them without an additional copy.
  174. if (!device) {
  175. av_log(avctx, AV_LOG_ERROR, "A device reference is "
  176. "required to create new frames with reverse "
  177. "mapping.\n");
  178. err = AVERROR(EINVAL);
  179. goto fail;
  180. }
  181. ctx->reverse = 1;
  182. ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
  183. if (!ctx->hwframes_ref) {
  184. err = AVERROR(ENOMEM);
  185. goto fail;
  186. }
  187. hwfc = (AVHWFramesContext*)ctx->hwframes_ref->data;
  188. hwfc->format = outlink->format;
  189. hwfc->sw_format = inlink->format;
  190. hwfc->width = inlink->w;
  191. hwfc->height = inlink->h;
  192. if (avctx->extra_hw_frames >= 0)
  193. hwfc->initial_pool_size = 2 + avctx->extra_hw_frames;
  194. err = av_hwframe_ctx_init(ctx->hwframes_ref);
  195. if (err < 0) {
  196. av_log(avctx, AV_LOG_ERROR, "Failed to create frame "
  197. "context for reverse mapping: %d.\n", err);
  198. goto fail;
  199. }
  200. } else {
  201. av_log(avctx, AV_LOG_ERROR, "Mapping requires a hardware "
  202. "context (a device, or frames on input).\n");
  203. return AVERROR(EINVAL);
  204. }
  205. outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  206. if (!outlink->hw_frames_ctx) {
  207. err = AVERROR(ENOMEM);
  208. goto fail;
  209. }
  210. outlink->w = inlink->w;
  211. outlink->h = inlink->h;
  212. if (device_is_derived)
  213. av_buffer_unref(&device);
  214. return 0;
  215. fail:
  216. if (device_is_derived)
  217. av_buffer_unref(&device);
  218. av_buffer_unref(&ctx->hwframes_ref);
  219. return err;
  220. }
  221. static AVFrame *hwmap_get_buffer(AVFilterLink *inlink, int w, int h)
  222. {
  223. AVFilterContext *avctx = inlink->dst;
  224. AVFilterLink *outlink = avctx->outputs[0];
  225. HWMapContext *ctx = avctx->priv;
  226. if (ctx->reverse && !inlink->hw_frames_ctx) {
  227. AVFrame *src, *dst;
  228. int err;
  229. src = ff_get_video_buffer(outlink, w, h);
  230. if (!src) {
  231. av_log(avctx, AV_LOG_ERROR, "Failed to allocate source "
  232. "frame for software mapping.\n");
  233. return NULL;
  234. }
  235. dst = av_frame_alloc();
  236. if (!dst) {
  237. av_frame_free(&src);
  238. return NULL;
  239. }
  240. err = av_hwframe_map(dst, src, ctx->mode);
  241. if (err) {
  242. av_log(avctx, AV_LOG_ERROR, "Failed to map frame to "
  243. "software: %d.\n", err);
  244. av_frame_free(&src);
  245. av_frame_free(&dst);
  246. return NULL;
  247. }
  248. av_frame_free(&src);
  249. return dst;
  250. } else {
  251. return ff_default_get_video_buffer(inlink, w, h);
  252. }
  253. }
  254. static int hwmap_filter_frame(AVFilterLink *link, AVFrame *input)
  255. {
  256. AVFilterContext *avctx = link->dst;
  257. AVFilterLink *outlink = avctx->outputs[0];
  258. HWMapContext *ctx = avctx->priv;
  259. AVFrame *map = NULL;
  260. int err;
  261. av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  262. av_get_pix_fmt_name(input->format),
  263. input->width, input->height, input->pts);
  264. map = av_frame_alloc();
  265. if (!map) {
  266. err = AVERROR(ENOMEM);
  267. goto fail;
  268. }
  269. map->format = outlink->format;
  270. map->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  271. if (!map->hw_frames_ctx) {
  272. err = AVERROR(ENOMEM);
  273. goto fail;
  274. }
  275. if (ctx->reverse && !input->hw_frames_ctx) {
  276. // If we mapped backwards from hardware to software, we need
  277. // to attach the hardware frame context to the input frame to
  278. // make the mapping visible to av_hwframe_map().
  279. input->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  280. if (!input->hw_frames_ctx) {
  281. err = AVERROR(ENOMEM);
  282. goto fail;
  283. }
  284. }
  285. err = av_hwframe_map(map, input, ctx->mode);
  286. if (err < 0) {
  287. av_log(avctx, AV_LOG_ERROR, "Failed to map frame: %d.\n", err);
  288. goto fail;
  289. }
  290. err = av_frame_copy_props(map, input);
  291. if (err < 0)
  292. goto fail;
  293. av_frame_free(&input);
  294. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  295. av_get_pix_fmt_name(map->format),
  296. map->width, map->height, map->pts);
  297. return ff_filter_frame(outlink, map);
  298. fail:
  299. av_frame_free(&input);
  300. av_frame_free(&map);
  301. return err;
  302. }
  303. static av_cold void hwmap_uninit(AVFilterContext *avctx)
  304. {
  305. HWMapContext *ctx = avctx->priv;
  306. av_buffer_unref(&ctx->hwframes_ref);
  307. }
  308. #define OFFSET(x) offsetof(HWMapContext, x)
  309. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
  310. static const AVOption hwmap_options[] = {
  311. { "mode", "Frame mapping mode",
  312. OFFSET(mode), AV_OPT_TYPE_FLAGS,
  313. { .i64 = AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE },
  314. 0, INT_MAX, FLAGS, "mode" },
  315. { "read", "Mapping should be readable",
  316. 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_READ },
  317. INT_MIN, INT_MAX, FLAGS, "mode" },
  318. { "write", "Mapping should be writeable",
  319. 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_WRITE },
  320. INT_MIN, INT_MAX, FLAGS, "mode" },
  321. { "overwrite", "Mapping will always overwrite the entire frame",
  322. 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_OVERWRITE },
  323. INT_MIN, INT_MAX, FLAGS, "mode" },
  324. { "direct", "Mapping should not involve any copying",
  325. 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT },
  326. INT_MIN, INT_MAX, FLAGS, "mode" },
  327. { "derive_device", "Derive a new device of this type",
  328. OFFSET(derive_device_type), AV_OPT_TYPE_STRING,
  329. { .str = NULL }, 0, 0, FLAGS },
  330. { "reverse", "Map in reverse (create and allocate in the sink)",
  331. OFFSET(reverse), AV_OPT_TYPE_INT,
  332. { .i64 = 0 }, 0, 1, FLAGS },
  333. { NULL }
  334. };
  335. AVFILTER_DEFINE_CLASS(hwmap);
  336. static const AVFilterPad hwmap_inputs[] = {
  337. {
  338. .name = "default",
  339. .type = AVMEDIA_TYPE_VIDEO,
  340. .get_video_buffer = hwmap_get_buffer,
  341. .filter_frame = hwmap_filter_frame,
  342. },
  343. { NULL }
  344. };
  345. static const AVFilterPad hwmap_outputs[] = {
  346. {
  347. .name = "default",
  348. .type = AVMEDIA_TYPE_VIDEO,
  349. .config_props = hwmap_config_output,
  350. },
  351. { NULL }
  352. };
  353. AVFilter ff_vf_hwmap = {
  354. .name = "hwmap",
  355. .description = NULL_IF_CONFIG_SMALL("Map hardware frames"),
  356. .uninit = hwmap_uninit,
  357. .priv_size = sizeof(HWMapContext),
  358. .priv_class = &hwmap_class,
  359. .query_formats = hwmap_query_formats,
  360. .inputs = hwmap_inputs,
  361. .outputs = hwmap_outputs,
  362. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  363. };