FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_hwmap.c
Date: 2026-09-24 14:13:09
Exec Total Coverage
Lines: 0 187 0.0%
Functions: 0 5 0.0%
Branches: 0 86 0.0%

Line Branch Exec Source
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
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"
24
25 #if CONFIG_D3D11VA
26 #include "libavutil/hwcontext_d3d11va.h"
27 #endif
28
29 #include "avfilter.h"
30 #include "filters.h"
31 #include "formats.h"
32 #include "video.h"
33
34 typedef struct HWMapContext {
35 const AVClass *class;
36
37 AVBufferRef *hwframes_ref;
38
39 int mode;
40 char *derive_device_type;
41 int reverse;
42 } HWMapContext;
43
44 static int hwmap_query_formats(const AVFilterContext *avctx,
45 AVFilterFormatsConfig **cfg_in,
46 AVFilterFormatsConfig **cfg_out)
47 {
48 int ret;
49
50 if ((ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
51 &cfg_in[0]->formats)) < 0 ||
52 (ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
53 &cfg_out[0]->formats)) < 0)
54 return ret;
55
56 return 0;
57 }
58
59 static int hwmap_config_output(AVFilterLink *outlink)
60 {
61 FilterLink *outl = ff_filter_link(outlink);
62 AVFilterContext *avctx = outlink->src;
63 HWMapContext *ctx = avctx->priv;
64 AVFilterLink *inlink = avctx->inputs[0];
65 FilterLink *inl = ff_filter_link(inlink);
66 AVHWFramesContext *hwfc;
67 AVBufferRef *device;
68 const AVPixFmtDescriptor *desc;
69 int err, device_is_derived;
70
71 av_log(avctx, AV_LOG_DEBUG, "Configure hwmap %s -> %s.\n",
72 av_get_pix_fmt_name(inlink->format),
73 av_get_pix_fmt_name(outlink->format));
74
75 av_buffer_unref(&ctx->hwframes_ref);
76
77 device = avctx->hw_device_ctx;
78 device_is_derived = 0;
79
80 if (inl->hw_frames_ctx) {
81 hwfc = (AVHWFramesContext*)inl->hw_frames_ctx->data;
82
83 if (ctx->derive_device_type) {
84 enum AVHWDeviceType type;
85
86 type = av_hwdevice_find_type_by_name(ctx->derive_device_type);
87 if (type == AV_HWDEVICE_TYPE_NONE) {
88 av_log(avctx, AV_LOG_ERROR, "Invalid device type.\n");
89 err = AVERROR(EINVAL);
90 goto fail;
91 }
92
93 err = av_hwdevice_ctx_create_derived(&device, type,
94 hwfc->device_ref, 0);
95 if (err < 0) {
96 av_log(avctx, AV_LOG_ERROR, "Failed to created derived "
97 "device context: %d.\n", err);
98 goto fail;
99 }
100 device_is_derived = 1;
101 }
102
103 desc = av_pix_fmt_desc_get(outlink->format);
104 if (!desc) {
105 err = AVERROR(EINVAL);
106 goto fail;
107 }
108
109 if (inlink->format == hwfc->format &&
110 (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
111 !ctx->reverse) {
112 // Map between two hardware formats (including the case of
113 // undoing an existing mapping).
114
115 if (!device) {
116 av_log(avctx, AV_LOG_ERROR, "A device reference is "
117 "required to map to a hardware format.\n");
118 err = AVERROR(EINVAL);
119 goto fail;
120 }
121
122 err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref,
123 outlink->format,
124 device,
125 inl->hw_frames_ctx,
126 ctx->mode);
127 if (err < 0) {
128 av_log(avctx, AV_LOG_ERROR, "Failed to create derived "
129 "frames context: %d.\n", err);
130 goto fail;
131 }
132
133 } else if (inlink->format == hwfc->format &&
134 (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
135 ctx->reverse) {
136 // Map between two hardware formats, but do it in reverse.
137 // Make a new hwframe context for the target type, and then
138 // overwrite the input hwframe context with a derived context
139 // mapped from that back to the source type.
140 AVBufferRef *source;
141 AVHWFramesContext *frames;
142
143 ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
144 if (!ctx->hwframes_ref) {
145 err = AVERROR(ENOMEM);
146 goto fail;
147 }
148 frames = (AVHWFramesContext*)ctx->hwframes_ref->data;
149
150 frames->format = outlink->format;
151 frames->sw_format = hwfc->sw_format;
152 frames->width = hwfc->width;
153 frames->height = hwfc->height;
154
155 if (avctx->extra_hw_frames >= 0)
156 frames->initial_pool_size = 2 + avctx->extra_hw_frames;
157
158 #if CONFIG_D3D11VA
159 if (frames->format == AV_PIX_FMT_D3D11) {
160 AVD3D11VAFramesContext *frames_d3d11 = frames->hwctx;
161 frames_d3d11->BindFlags = D3D11_BIND_DECODER | D3D11_BIND_SHADER_RESOURCE;
162 }
163 #endif
164 err = av_hwframe_ctx_init(ctx->hwframes_ref);
165 if (err < 0) {
166 av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
167 "target frames context: %d.\n", err);
168 goto fail;
169 }
170
171 err = av_hwframe_ctx_create_derived(&source,
172 inlink->format,
173 hwfc->device_ref,
174 ctx->hwframes_ref,
175 ctx->mode);
176 if (err < 0) {
177 av_log(avctx, AV_LOG_ERROR, "Failed to create "
178 "derived source frames context: %d.\n", err);
179 goto fail;
180 }
181
182 // Here is the naughty bit. This overwriting changes what
183 // ff_get_video_buffer() in the previous filter returns -
184 // it will now give a frame allocated here mapped back to
185 // the format it expects. If there were any additional
186 // constraints on the output frames there then this may
187 // break nastily.
188 av_buffer_unref(&inl->hw_frames_ctx);
189 inl->hw_frames_ctx = source;
190
191 } else if ((outlink->format == hwfc->format &&
192 inlink->format == hwfc->sw_format) ||
193 inlink->format == hwfc->format) {
194 // Map from a hardware format to a software format, or
195 // undo an existing such mapping.
196
197 ctx->hwframes_ref = av_buffer_ref(inl->hw_frames_ctx);
198 if (!ctx->hwframes_ref) {
199 err = AVERROR(ENOMEM);
200 goto fail;
201 }
202
203 } else {
204 // Non-matching formats - not supported.
205
206 av_log(avctx, AV_LOG_ERROR, "Unsupported formats for "
207 "hwmap: from %s (%s) to %s.\n",
208 av_get_pix_fmt_name(inlink->format),
209 av_get_pix_fmt_name(hwfc->format),
210 av_get_pix_fmt_name(outlink->format));
211 err = AVERROR(EINVAL);
212 goto fail;
213 }
214 } else if (avctx->hw_device_ctx) {
215 // Map from a software format to a hardware format. This
216 // creates a new hwframe context like hwupload, but then
217 // returns frames mapped from that to the previous link in
218 // order to fill them without an additional copy.
219
220 if (!device) {
221 av_log(avctx, AV_LOG_ERROR, "A device reference is "
222 "required to create new frames with reverse "
223 "mapping.\n");
224 err = AVERROR(EINVAL);
225 goto fail;
226 }
227
228 ctx->reverse = 1;
229
230 ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
231 if (!ctx->hwframes_ref) {
232 err = AVERROR(ENOMEM);
233 goto fail;
234 }
235 hwfc = (AVHWFramesContext*)ctx->hwframes_ref->data;
236
237 hwfc->format = outlink->format;
238 hwfc->sw_format = inlink->format;
239 hwfc->width = inlink->w;
240 hwfc->height = inlink->h;
241
242 if (avctx->extra_hw_frames >= 0)
243 hwfc->initial_pool_size = 2 + avctx->extra_hw_frames;
244
245 err = av_hwframe_ctx_init(ctx->hwframes_ref);
246 if (err < 0) {
247 av_log(avctx, AV_LOG_ERROR, "Failed to create frame "
248 "context for reverse mapping: %d.\n", err);
249 goto fail;
250 }
251
252 } else {
253 av_log(avctx, AV_LOG_ERROR, "Mapping requires a hardware "
254 "context (a device, or frames on input).\n");
255 return AVERROR(EINVAL);
256 }
257
258 outl->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
259 if (!outl->hw_frames_ctx) {
260 err = AVERROR(ENOMEM);
261 goto fail;
262 }
263
264 outlink->w = inlink->w;
265 outlink->h = inlink->h;
266
267 if (device_is_derived)
268 av_buffer_unref(&device);
269 return 0;
270
271 fail:
272 if (device_is_derived)
273 av_buffer_unref(&device);
274 av_buffer_unref(&ctx->hwframes_ref);
275 return err;
276 }
277
278 static AVFrame *hwmap_get_buffer(AVFilterLink *inlink, int w, int h)
279 {
280 FilterLink *l = ff_filter_link(inlink);
281 AVFilterContext *avctx = inlink->dst;
282 AVFilterLink *outlink = avctx->outputs[0];
283 HWMapContext *ctx = avctx->priv;
284
285 if (ctx->reverse && !l->hw_frames_ctx) {
286 AVFrame *src, *dst;
287 int err;
288
289 src = ff_get_video_buffer(outlink, w, h);
290 if (!src) {
291 av_log(avctx, AV_LOG_ERROR, "Failed to allocate source "
292 "frame for software mapping.\n");
293 return NULL;
294 }
295
296 dst = av_frame_alloc();
297 if (!dst) {
298 av_frame_free(&src);
299 return NULL;
300 }
301
302 err = av_hwframe_map(dst, src, ctx->mode);
303 if (err) {
304 av_log(avctx, AV_LOG_ERROR, "Failed to map frame to "
305 "software: %d.\n", err);
306 av_frame_free(&src);
307 av_frame_free(&dst);
308 return NULL;
309 }
310
311 av_frame_free(&src);
312 return dst;
313 } else {
314 return ff_default_get_video_buffer(inlink, w, h);
315 }
316 }
317
318 static int hwmap_filter_frame(AVFilterLink *link, AVFrame *input)
319 {
320 AVFilterContext *avctx = link->dst;
321 AVFilterLink *outlink = avctx->outputs[0];
322 HWMapContext *ctx = avctx->priv;
323 AVFrame *map = NULL;
324 int err;
325
326 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
327 av_get_pix_fmt_name(input->format),
328 input->width, input->height, input->pts);
329
330 map = av_frame_alloc();
331 if (!map) {
332 err = AVERROR(ENOMEM);
333 goto fail;
334 }
335
336 map->format = outlink->format;
337 map->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
338 if (!map->hw_frames_ctx) {
339 err = AVERROR(ENOMEM);
340 goto fail;
341 }
342
343 if (ctx->reverse && !input->hw_frames_ctx) {
344 // If we mapped backwards from hardware to software, we need
345 // to attach the hardware frame context to the input frame to
346 // make the mapping visible to av_hwframe_map().
347 input->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
348 if (!input->hw_frames_ctx) {
349 err = AVERROR(ENOMEM);
350 goto fail;
351 }
352 }
353
354 err = av_hwframe_map(map, input, ctx->mode);
355 if (err < 0) {
356 av_log(avctx, AV_LOG_ERROR, "Failed to map frame: %d.\n", err);
357 goto fail;
358 }
359
360 err = av_frame_copy_props(map, input);
361 if (err < 0)
362 goto fail;
363
364 av_frame_free(&input);
365
366 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
367 av_get_pix_fmt_name(map->format),
368 map->width, map->height, map->pts);
369
370 return ff_filter_frame(outlink, map);
371
372 fail:
373 av_frame_free(&input);
374 av_frame_free(&map);
375 return err;
376 }
377
378 static av_cold void hwmap_uninit(AVFilterContext *avctx)
379 {
380 HWMapContext *ctx = avctx->priv;
381
382 av_buffer_unref(&ctx->hwframes_ref);
383 }
384
385 #define OFFSET(x) offsetof(HWMapContext, x)
386 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
387 static const AVOption hwmap_options[] = {
388 { "mode", "Frame mapping mode",
389 OFFSET(mode), AV_OPT_TYPE_FLAGS,
390 { .i64 = AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE },
391 0, INT_MAX, FLAGS, .unit = "mode" },
392
393 { "read", "Mapping should be readable",
394 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_READ },
395 INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
396 { "write", "Mapping should be writeable",
397 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_WRITE },
398 INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
399 { "overwrite", "Mapping will always overwrite the entire frame",
400 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_OVERWRITE },
401 INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
402 { "direct", "Mapping should not involve any copying",
403 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT },
404 INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
405
406 { "derive_device", "Derive a new device of this type",
407 OFFSET(derive_device_type), AV_OPT_TYPE_STRING,
408 { .str = NULL }, 0, 0, FLAGS },
409 { "reverse", "Map in reverse (create and allocate in the sink)",
410 OFFSET(reverse), AV_OPT_TYPE_INT,
411 { .i64 = 0 }, 0, 1, FLAGS },
412
413 { NULL }
414 };
415
416 AVFILTER_DEFINE_CLASS(hwmap);
417
418 static const AVFilterPad hwmap_inputs[] = {
419 {
420 .name = "default",
421 .type = AVMEDIA_TYPE_VIDEO,
422 .get_buffer.video = hwmap_get_buffer,
423 .filter_frame = hwmap_filter_frame,
424 },
425 };
426
427 static const AVFilterPad hwmap_outputs[] = {
428 {
429 .name = "default",
430 .type = AVMEDIA_TYPE_VIDEO,
431 .config_props = hwmap_config_output,
432 },
433 };
434
435 const FFFilter ff_vf_hwmap = {
436 .p.name = "hwmap",
437 .p.description = NULL_IF_CONFIG_SMALL("Map hardware frames"),
438 .p.priv_class = &hwmap_class,
439 .p.flags = AVFILTER_FLAG_HWDEVICE,
440 .uninit = hwmap_uninit,
441 .priv_size = sizeof(HWMapContext),
442 FILTER_INPUTS(hwmap_inputs),
443 FILTER_OUTPUTS(hwmap_outputs),
444 FILTER_QUERY_FUNC2(hwmap_query_formats),
445 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
446 };
447