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.

335 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 "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 *hwdevice_ref;
  30. AVBufferRef *hwframes_ref;
  31. int mode;
  32. int map_backwards;
  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. const AVPixFmtDescriptor *desc;
  51. int err;
  52. av_log(avctx, AV_LOG_DEBUG, "Configure hwmap %s -> %s.\n",
  53. av_get_pix_fmt_name(inlink->format),
  54. av_get_pix_fmt_name(outlink->format));
  55. if (inlink->hw_frames_ctx) {
  56. hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
  57. desc = av_pix_fmt_desc_get(outlink->format);
  58. if (!desc)
  59. return AVERROR(EINVAL);
  60. if (inlink->format == hwfc->format &&
  61. (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
  62. // Map between two hardware formats (including the case of
  63. // undoing an existing mapping).
  64. ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
  65. if (!ctx->hwdevice_ref) {
  66. err = AVERROR(ENOMEM);
  67. goto fail;
  68. }
  69. err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref,
  70. outlink->format,
  71. ctx->hwdevice_ref,
  72. inlink->hw_frames_ctx, 0);
  73. if (err < 0)
  74. goto fail;
  75. } else if ((outlink->format == hwfc->format &&
  76. inlink->format == hwfc->sw_format) ||
  77. inlink->format == hwfc->format) {
  78. // Map from a hardware format to a software format, or
  79. // undo an existing such mapping.
  80. ctx->hwdevice_ref = NULL;
  81. ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
  82. if (!ctx->hwframes_ref) {
  83. err = AVERROR(ENOMEM);
  84. goto fail;
  85. }
  86. } else {
  87. // Non-matching formats - not supported.
  88. av_log(avctx, AV_LOG_ERROR, "Unsupported formats for "
  89. "hwmap: from %s (%s) to %s.\n",
  90. av_get_pix_fmt_name(inlink->format),
  91. av_get_pix_fmt_name(hwfc->format),
  92. av_get_pix_fmt_name(outlink->format));
  93. err = AVERROR(EINVAL);
  94. goto fail;
  95. }
  96. } else if (avctx->hw_device_ctx) {
  97. // Map from a software format to a hardware format. This
  98. // creates a new hwframe context like hwupload, but then
  99. // returns frames mapped from that to the previous link in
  100. // order to fill them without an additional copy.
  101. ctx->map_backwards = 1;
  102. ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
  103. if (!ctx->hwdevice_ref) {
  104. err = AVERROR(ENOMEM);
  105. goto fail;
  106. }
  107. ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
  108. if (!ctx->hwframes_ref) {
  109. err = AVERROR(ENOMEM);
  110. goto fail;
  111. }
  112. hwfc = (AVHWFramesContext*)ctx->hwframes_ref->data;
  113. hwfc->format = outlink->format;
  114. hwfc->sw_format = inlink->format;
  115. hwfc->width = inlink->w;
  116. hwfc->height = inlink->h;
  117. err = av_hwframe_ctx_init(ctx->hwframes_ref);
  118. if (err < 0) {
  119. av_log(avctx, AV_LOG_ERROR, "Failed to create frame "
  120. "context for backward mapping: %d.\n", err);
  121. goto fail;
  122. }
  123. } else {
  124. av_log(avctx, AV_LOG_ERROR, "Mapping requires a hardware "
  125. "context (a device, or frames on input).\n");
  126. return AVERROR(EINVAL);
  127. }
  128. outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  129. if (!outlink->hw_frames_ctx) {
  130. err = AVERROR(ENOMEM);
  131. goto fail;
  132. }
  133. outlink->w = inlink->w;
  134. outlink->h = inlink->h;
  135. return 0;
  136. fail:
  137. av_buffer_unref(&ctx->hwframes_ref);
  138. av_buffer_unref(&ctx->hwdevice_ref);
  139. return err;
  140. }
  141. static AVFrame *hwmap_get_buffer(AVFilterLink *inlink, int w, int h)
  142. {
  143. AVFilterContext *avctx = inlink->dst;
  144. AVFilterLink *outlink = avctx->outputs[0];
  145. HWMapContext *ctx = avctx->priv;
  146. if (ctx->map_backwards) {
  147. AVFrame *src, *dst;
  148. int err;
  149. src = ff_get_video_buffer(outlink, w, h);
  150. if (!src) {
  151. av_log(avctx, AV_LOG_ERROR, "Failed to allocate source "
  152. "frame for software mapping.\n");
  153. return NULL;
  154. }
  155. dst = av_frame_alloc();
  156. if (!dst) {
  157. av_frame_free(&src);
  158. return NULL;
  159. }
  160. err = av_hwframe_map(dst, src, ctx->mode);
  161. if (err) {
  162. av_log(avctx, AV_LOG_ERROR, "Failed to map frame to "
  163. "software: %d.\n", err);
  164. av_frame_free(&src);
  165. av_frame_free(&dst);
  166. return NULL;
  167. }
  168. av_frame_free(&src);
  169. return dst;
  170. } else {
  171. return ff_default_get_video_buffer(inlink, w, h);
  172. }
  173. }
  174. static int hwmap_filter_frame(AVFilterLink *link, AVFrame *input)
  175. {
  176. AVFilterContext *avctx = link->dst;
  177. AVFilterLink *outlink = avctx->outputs[0];
  178. HWMapContext *ctx = avctx->priv;
  179. AVFrame *map = NULL;
  180. int err;
  181. av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  182. av_get_pix_fmt_name(input->format),
  183. input->width, input->height, input->pts);
  184. map = av_frame_alloc();
  185. if (!map) {
  186. err = AVERROR(ENOMEM);
  187. goto fail;
  188. }
  189. map->format = outlink->format;
  190. map->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  191. if (!map->hw_frames_ctx) {
  192. err = AVERROR(ENOMEM);
  193. goto fail;
  194. }
  195. if (ctx->map_backwards && !input->hw_frames_ctx) {
  196. // If we mapped backwards from hardware to software, we need
  197. // to attach the hardware frame context to the input frame to
  198. // make the mapping visible to av_hwframe_map().
  199. input->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
  200. if (!input->hw_frames_ctx) {
  201. err = AVERROR(ENOMEM);
  202. goto fail;
  203. }
  204. }
  205. err = av_hwframe_map(map, input, ctx->mode);
  206. if (err < 0) {
  207. av_log(avctx, AV_LOG_ERROR, "Failed to map frame: %d.\n", err);
  208. goto fail;
  209. }
  210. err = av_frame_copy_props(map, input);
  211. if (err < 0)
  212. goto fail;
  213. av_frame_free(&input);
  214. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  215. av_get_pix_fmt_name(map->format),
  216. map->width, map->height, map->pts);
  217. return ff_filter_frame(outlink, map);
  218. fail:
  219. av_frame_free(&input);
  220. av_frame_free(&map);
  221. return err;
  222. }
  223. static av_cold void hwmap_uninit(AVFilterContext *avctx)
  224. {
  225. HWMapContext *ctx = avctx->priv;
  226. av_buffer_unref(&ctx->hwframes_ref);
  227. av_buffer_unref(&ctx->hwdevice_ref);
  228. }
  229. #define OFFSET(x) offsetof(HWMapContext, x)
  230. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
  231. static const AVOption hwmap_options[] = {
  232. { "mode", "Frame mapping mode",
  233. OFFSET(mode), AV_OPT_TYPE_FLAGS,
  234. { .i64 = AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE },
  235. 0, INT_MAX, FLAGS, "mode" },
  236. { "read", "Mapping should be readable",
  237. 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_READ },
  238. INT_MIN, INT_MAX, FLAGS, "mode" },
  239. { "write", "Mapping should be writeable",
  240. 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_WRITE },
  241. INT_MIN, INT_MAX, FLAGS, "mode" },
  242. { "overwrite", "Mapping will always overwrite the entire frame",
  243. 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_OVERWRITE },
  244. INT_MIN, INT_MAX, FLAGS, "mode" },
  245. { "direct", "Mapping should not involve any copying",
  246. 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT },
  247. INT_MIN, INT_MAX, FLAGS, "mode" },
  248. { NULL }
  249. };
  250. AVFILTER_DEFINE_CLASS(hwmap);
  251. static const AVFilterPad hwmap_inputs[] = {
  252. {
  253. .name = "default",
  254. .type = AVMEDIA_TYPE_VIDEO,
  255. .get_video_buffer = hwmap_get_buffer,
  256. .filter_frame = hwmap_filter_frame,
  257. },
  258. { NULL }
  259. };
  260. static const AVFilterPad hwmap_outputs[] = {
  261. {
  262. .name = "default",
  263. .type = AVMEDIA_TYPE_VIDEO,
  264. .config_props = hwmap_config_output,
  265. },
  266. { NULL }
  267. };
  268. AVFilter ff_vf_hwmap = {
  269. .name = "hwmap",
  270. .description = NULL_IF_CONFIG_SMALL("Map hardware frames"),
  271. .uninit = hwmap_uninit,
  272. .priv_size = sizeof(HWMapContext),
  273. .priv_class = &hwmap_class,
  274. .query_formats = hwmap_query_formats,
  275. .inputs = hwmap_inputs,
  276. .outputs = hwmap_outputs,
  277. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  278. };