2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/buffer.h"
20 #include "libavutil/hwcontext.h"
21 #include "libavutil/log.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
30 typedef struct HWMapContext {
33 AVBufferRef *hwframes_ref;
36 char *derive_device_type;
40 static int hwmap_query_formats(AVFilterContext *avctx)
44 if ((ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
45 &avctx->inputs[0]->out_formats)) < 0 ||
46 (ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
47 &avctx->outputs[0]->in_formats)) < 0)
53 static int hwmap_config_output(AVFilterLink *outlink)
55 AVFilterContext *avctx = outlink->src;
56 HWMapContext *ctx = avctx->priv;
57 AVFilterLink *inlink = avctx->inputs[0];
58 AVHWFramesContext *hwfc;
60 const AVPixFmtDescriptor *desc;
61 int err, device_is_derived;
63 av_log(avctx, AV_LOG_DEBUG, "Configure hwmap %s -> %s.\n",
64 av_get_pix_fmt_name(inlink->format),
65 av_get_pix_fmt_name(outlink->format));
67 av_buffer_unref(&ctx->hwframes_ref);
69 device = avctx->hw_device_ctx;
70 device_is_derived = 0;
72 if (inlink->hw_frames_ctx) {
73 hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
75 if (ctx->derive_device_type) {
76 enum AVHWDeviceType type;
78 type = av_hwdevice_find_type_by_name(ctx->derive_device_type);
79 if (type == AV_HWDEVICE_TYPE_NONE) {
80 av_log(avctx, AV_LOG_ERROR, "Invalid device type.\n");
81 err = AVERROR(EINVAL);
85 err = av_hwdevice_ctx_create_derived(&device, type,
88 av_log(avctx, AV_LOG_ERROR, "Failed to created derived "
89 "device context: %d.\n", err);
92 device_is_derived = 1;
95 desc = av_pix_fmt_desc_get(outlink->format);
97 err = AVERROR(EINVAL);
101 if (inlink->format == hwfc->format &&
102 (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
104 // Map between two hardware formats (including the case of
105 // undoing an existing mapping).
108 av_log(avctx, AV_LOG_ERROR, "A device reference is "
109 "required to map to a hardware format.\n");
110 err = AVERROR(EINVAL);
114 err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref,
117 inlink->hw_frames_ctx, 0);
119 av_log(avctx, AV_LOG_ERROR, "Failed to create derived "
120 "frames context: %d.\n", err);
124 } else if (inlink->format == hwfc->format &&
125 (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
127 // Map between two hardware formats, but do it in reverse.
128 // Make a new hwframe context for the target type, and then
129 // overwrite the input hwframe context with a derived context
130 // mapped from that back to the source type.
132 AVHWFramesContext *frames;
134 ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
135 if (!ctx->hwframes_ref) {
136 err = AVERROR(ENOMEM);
139 frames = (AVHWFramesContext*)ctx->hwframes_ref->data;
141 frames->format = outlink->format;
142 frames->sw_format = hwfc->sw_format;
143 frames->width = hwfc->width;
144 frames->height = hwfc->height;
145 frames->initial_pool_size = 64;
147 err = av_hwframe_ctx_init(ctx->hwframes_ref);
149 av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
150 "target frames context: %d.\n", err);
154 err = av_hwframe_ctx_create_derived(&source,
160 av_log(avctx, AV_LOG_ERROR, "Failed to create "
161 "derived source frames context: %d.\n", err);
165 // Here is the naughty bit. This overwriting changes what
166 // ff_get_video_buffer() in the previous filter returns -
167 // it will now give a frame allocated here mapped back to
168 // the format it expects. If there were any additional
169 // constraints on the output frames there then this may
171 av_buffer_unref(&inlink->hw_frames_ctx);
172 inlink->hw_frames_ctx = source;
174 } else if ((outlink->format == hwfc->format &&
175 inlink->format == hwfc->sw_format) ||
176 inlink->format == hwfc->format) {
177 // Map from a hardware format to a software format, or
178 // undo an existing such mapping.
180 ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
181 if (!ctx->hwframes_ref) {
182 err = AVERROR(ENOMEM);
187 // Non-matching formats - not supported.
189 av_log(avctx, AV_LOG_ERROR, "Unsupported formats for "
190 "hwmap: from %s (%s) to %s.\n",
191 av_get_pix_fmt_name(inlink->format),
192 av_get_pix_fmt_name(hwfc->format),
193 av_get_pix_fmt_name(outlink->format));
194 err = AVERROR(EINVAL);
197 } else if (avctx->hw_device_ctx) {
198 // Map from a software format to a hardware format. This
199 // creates a new hwframe context like hwupload, but then
200 // returns frames mapped from that to the previous link in
201 // order to fill them without an additional copy.
204 av_log(avctx, AV_LOG_ERROR, "A device reference is "
205 "required to create new frames with reverse "
207 err = AVERROR(EINVAL);
213 ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
214 if (!ctx->hwframes_ref) {
215 err = AVERROR(ENOMEM);
218 hwfc = (AVHWFramesContext*)ctx->hwframes_ref->data;
220 hwfc->format = outlink->format;
221 hwfc->sw_format = inlink->format;
222 hwfc->width = inlink->w;
223 hwfc->height = inlink->h;
225 err = av_hwframe_ctx_init(ctx->hwframes_ref);
227 av_log(avctx, AV_LOG_ERROR, "Failed to create frame "
228 "context for reverse mapping: %d.\n", err);
233 av_log(avctx, AV_LOG_ERROR, "Mapping requires a hardware "
234 "context (a device, or frames on input).\n");
235 return AVERROR(EINVAL);
238 outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
239 if (!outlink->hw_frames_ctx) {
240 err = AVERROR(ENOMEM);
244 outlink->w = inlink->w;
245 outlink->h = inlink->h;
247 if (device_is_derived)
248 av_buffer_unref(&device);
252 if (device_is_derived)
253 av_buffer_unref(&device);
254 av_buffer_unref(&ctx->hwframes_ref);
258 static AVFrame *hwmap_get_buffer(AVFilterLink *inlink, int w, int h)
260 AVFilterContext *avctx = inlink->dst;
261 AVFilterLink *outlink = avctx->outputs[0];
262 HWMapContext *ctx = avctx->priv;
264 if (ctx->reverse && !inlink->hw_frames_ctx) {
268 src = ff_get_video_buffer(outlink, w, h);
270 av_log(avctx, AV_LOG_ERROR, "Failed to allocate source "
271 "frame for software mapping.\n");
275 dst = av_frame_alloc();
281 err = av_hwframe_map(dst, src, ctx->mode);
283 av_log(avctx, AV_LOG_ERROR, "Failed to map frame to "
284 "software: %d.\n", err);
293 return ff_default_get_video_buffer(inlink, w, h);
297 static int hwmap_filter_frame(AVFilterLink *link, AVFrame *input)
299 AVFilterContext *avctx = link->dst;
300 AVFilterLink *outlink = avctx->outputs[0];
301 HWMapContext *ctx = avctx->priv;
305 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
306 av_get_pix_fmt_name(input->format),
307 input->width, input->height, input->pts);
309 map = av_frame_alloc();
311 err = AVERROR(ENOMEM);
315 map->format = outlink->format;
316 map->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
317 if (!map->hw_frames_ctx) {
318 err = AVERROR(ENOMEM);
322 if (ctx->reverse && !input->hw_frames_ctx) {
323 // If we mapped backwards from hardware to software, we need
324 // to attach the hardware frame context to the input frame to
325 // make the mapping visible to av_hwframe_map().
326 input->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
327 if (!input->hw_frames_ctx) {
328 err = AVERROR(ENOMEM);
333 err = av_hwframe_map(map, input, ctx->mode);
335 av_log(avctx, AV_LOG_ERROR, "Failed to map frame: %d.\n", err);
339 err = av_frame_copy_props(map, input);
343 av_frame_free(&input);
345 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
346 av_get_pix_fmt_name(map->format),
347 map->width, map->height, map->pts);
349 return ff_filter_frame(outlink, map);
352 av_frame_free(&input);
357 static av_cold void hwmap_uninit(AVFilterContext *avctx)
359 HWMapContext *ctx = avctx->priv;
361 av_buffer_unref(&ctx->hwframes_ref);
364 #define OFFSET(x) offsetof(HWMapContext, x)
365 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
366 static const AVOption hwmap_options[] = {
367 { "mode", "Frame mapping mode",
368 OFFSET(mode), AV_OPT_TYPE_FLAGS,
369 { .i64 = AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE },
370 0, INT_MAX, FLAGS, "mode" },
372 { "read", "Mapping should be readable",
373 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_READ },
374 INT_MIN, INT_MAX, FLAGS, "mode" },
375 { "write", "Mapping should be writeable",
376 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_WRITE },
377 INT_MIN, INT_MAX, FLAGS, "mode" },
378 { "overwrite", "Mapping will always overwrite the entire frame",
379 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_OVERWRITE },
380 INT_MIN, INT_MAX, FLAGS, "mode" },
381 { "direct", "Mapping should not involve any copying",
382 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT },
383 INT_MIN, INT_MAX, FLAGS, "mode" },
385 { "derive_device", "Derive a new device of this type",
386 OFFSET(derive_device_type), AV_OPT_TYPE_STRING,
387 { .str = NULL }, 0, 0, FLAGS },
388 { "reverse", "Map in reverse (create and allocate in the sink)",
389 OFFSET(reverse), AV_OPT_TYPE_INT,
390 { .i64 = 0 }, 0, 1, FLAGS },
395 AVFILTER_DEFINE_CLASS(hwmap);
397 static const AVFilterPad hwmap_inputs[] = {
400 .type = AVMEDIA_TYPE_VIDEO,
401 .get_video_buffer = hwmap_get_buffer,
402 .filter_frame = hwmap_filter_frame,
407 static const AVFilterPad hwmap_outputs[] = {
410 .type = AVMEDIA_TYPE_VIDEO,
411 .config_props = hwmap_config_output,
416 AVFilter ff_vf_hwmap = {
418 .description = NULL_IF_CONFIG_SMALL("Map hardware frames"),
419 .uninit = hwmap_uninit,
420 .priv_size = sizeof(HWMapContext),
421 .priv_class = &hwmap_class,
422 .query_formats = hwmap_query_formats,
423 .inputs = hwmap_inputs,
424 .outputs = hwmap_outputs,
425 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,