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