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