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