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.

420 lines
13KB

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