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.

428 lines
14KB

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