FFmpeg
Loading...
Searching...
No Matches
vf_hwmap.c
Go to the documentation of this file.
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
27#endif
28
29#include "avfilter.h"
30#include "filters.h"
31#include "formats.h"
32#include "video.h"
33
34typedef struct HWMapContext {
35 const AVClass *class;
36
38
39 int mode;
43
44static int hwmap_query_formats(const AVFilterContext *avctx,
45 AVFilterFormatsConfig **cfg_in,
46 AVFilterFormatsConfig **cfg_out)
47{
48 int ret;
49
51 &cfg_in[0]->formats)) < 0 ||
53 &cfg_out[0]->formats)) < 0)
54 return ret;
55
56 return 0;
57}
58
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);
67 AVBufferRef *device;
69 int err, device_is_derived;
70
71 av_log(avctx, AV_LOG_DEBUG, "Configure hwmap %s -> %s.\n",
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) {
82
83 if (ctx->derive_device_type) {
85
86 type = av_hwdevice_find_type_by_name(ctx->derive_device_type);
88 av_log(avctx, AV_LOG_ERROR, "Invalid device type.\n");
89 err = AVERROR(EINVAL);
90 goto fail;
91 }
92
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;
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.
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",
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
271fail:
272 if (device_is_derived)
273 av_buffer_unref(&device);
274 av_buffer_unref(&ctx->hwframes_ref);
275 return err;
276}
277
278static 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
297 if (!dst) {
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);
308 return NULL;
309 }
310
312 return dst;
313 } else {
314 return ff_default_get_video_buffer(inlink, w, h);
315 }
316}
317
318static 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",
328 input->width, input->height, input->pts);
329
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
372fail:
373 av_frame_free(&input);
375 return err;
376}
377
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)
387static const AVOption hwmap_options[] = {
388 { "mode", "Frame mapping mode",
391 0, INT_MAX, FLAGS, .unit = "mode" },
392
393 { "read", "Mapping should be readable",
395 INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
396 { "write", "Mapping should be writeable",
398 INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
399 { "overwrite", "Mapping will always overwrite the entire frame",
401 INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
402 { "direct", "Mapping should not involve any copying",
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)",
411 { .i64 = 0 }, 0, 1, FLAGS },
412
413 { NULL }
414};
415
417
418static 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
427static const AVFilterPad hwmap_outputs[] = {
428 {
429 .name = "default",
430 .type = AVMEDIA_TYPE_VIDEO,
431 .config_props = hwmap_config_output,
432 },
433};
434
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),
445 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
446};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFFilter ff_vf_hwmap
Definition vf_hwmap.c:435
static int frames
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
refcounted data buffer API
#define FLAGS
Definition cmdutils.c:596
#define NULL
Definition coverity.c:32
static uint64_t reverse(uint64_t p, unsigned int deg)
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition formats.c:602
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:187
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:140
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:104
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr, enum AVHWDeviceType type, AVBufferRef *src_ref, int flags)
Create a new device of the specified type from an existing device.
Definition hwcontext.c:718
int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx, enum AVPixelFormat format, AVBufferRef *derived_device_ctx, AVBufferRef *source_frame_ctx, int flags)
Create and initialise an AVHWFramesContext as a mapping of another existing AVHWFramesContext on a di...
Definition hwcontext.c:871
enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
Look up an AVHWDeviceType by name.
Definition hwcontext.c:110
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:337
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition hwcontext.c:263
int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
Map a hardware frame.
Definition hwcontext.c:793
@ AV_HWFRAME_MAP_READ
The mapping must be readable.
Definition hwcontext.h:515
@ AV_HWFRAME_MAP_DIRECT
The mapping must be direct.
Definition hwcontext.h:531
@ AV_HWFRAME_MAP_WRITE
The mapping must be writeable.
Definition hwcontext.h:519
@ AV_HWFRAME_MAP_OVERWRITE
The mapped frame will be overwritten completely in subsequent operations, so the current frame data n...
Definition hwcontext.h:525
AVHWDeviceType
Definition hwcontext.h:27
@ AV_HWDEVICE_TYPE_NONE
Definition hwcontext.h:28
An API-specific header for AV_HWDEVICE_TYPE_D3D11VA.
cl_device_type type
const VDPAUPixFmtMap * map
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
AVOptions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition pixfmt.h:336
formats
Definition signature.h:47
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
This struct is allocated as AVHWFramesContext.hwctx.
UINT BindFlags
D3D11_TEXTURE2D_DESC.BindFlags used for texture creation.
An instance of a filter.
Definition avfilter.h:273
int extra_hw_frames
Sets the number of extra hardware frames which the filter will allocate on its output links for use i...
Definition avfilter.h:352
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
AVBufferRef * hw_device_ctx
For filters which will create hardware frames, sets the device the filter should create them in.
Definition avfilter.h:336
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
int width
Definition frame.h:544
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition frame.h:769
int height
Definition frame.h:544
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition hwcontext.h:129
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int initial_pool_size
Initial size of the frame pool.
Definition hwcontext.h:190
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVBufferRef * hwframes_ref
Definition vf_hwmap.c:37
char * derive_device_type
Definition vf_hwmap.c:40
Definition swscale.c:71
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int hwmap_config_output(AVFilterLink *outlink)
Definition vf_hwmap.c:59
static const AVFilterPad hwmap_inputs[]
Definition vf_hwmap.c:418
static av_cold void hwmap_uninit(AVFilterContext *avctx)
Definition vf_hwmap.c:378
static const AVFilterPad hwmap_outputs[]
Definition vf_hwmap.c:427
static const AVOption hwmap_options[]
Definition vf_hwmap.c:387
static int hwmap_filter_frame(AVFilterLink *link, AVFrame *input)
Definition vf_hwmap.c:318
static int hwmap_query_formats(const AVFilterContext *avctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_hwmap.c:44
#define OFFSET(x)
Definition vf_hwmap.c:385
static AVFrame * hwmap_get_buffer(AVFilterLink *inlink, int w, int h)
Definition vf_hwmap.c:278
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
AVFrame * ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
Definition video.c:84