vf_hwmap: Add reverse mapping for hardware frames
[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 reverse;
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 !ctx->reverse) {
97 // Map between two hardware formats (including the case of
98 // undoing an existing mapping).
99
100 if (!device) {
101 av_log(avctx, AV_LOG_ERROR, "A device reference is "
102 "required to map to a hardware format.\n");
103 err = AVERROR(EINVAL);
104 goto fail;
105 }
106
107 err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref,
108 outlink->format,
109 device,
110 inlink->hw_frames_ctx, 0);
111 if (err < 0) {
112 av_log(avctx, AV_LOG_ERROR, "Failed to create derived "
113 "frames context: %d.\n", err);
114 goto fail;
115 }
116
117 } else if (inlink->format == hwfc->format &&
118 (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
119 ctx->reverse) {
120 // Map between two hardware formats, but do it in reverse.
121 // Make a new hwframe context for the target type, and then
122 // overwrite the input hwframe context with a derived context
123 // mapped from that back to the source type.
124 AVBufferRef *source;
125 AVHWFramesContext *frames;
126
127 ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
128 if (!ctx->hwframes_ref) {
129 err = AVERROR(ENOMEM);
130 goto fail;
131 }
132 frames = (AVHWFramesContext*)ctx->hwframes_ref->data;
133
134 frames->format = outlink->format;
135 frames->sw_format = hwfc->sw_format;
136 frames->width = hwfc->width;
137 frames->height = hwfc->height;
138 frames->initial_pool_size = 64;
139
140 err = av_hwframe_ctx_init(ctx->hwframes_ref);
141 if (err < 0) {
142 av_log(avctx, AV_LOG_ERROR, "Failed to initialise "
143 "target frames context: %d.\n", err);
144 goto fail;
145 }
146
147 err = av_hwframe_ctx_create_derived(&source,
148 inlink->format,
149 hwfc->device_ref,
150 ctx->hwframes_ref,
151 ctx->mode);
152 if (err < 0) {
153 av_log(avctx, AV_LOG_ERROR, "Failed to create "
154 "derived source frames context: %d.\n", err);
155 goto fail;
156 }
157
158 // Here is the naughty bit. This overwriting changes what
159 // ff_get_video_buffer() in the previous filter returns -
160 // it will now give a frame allocated here mapped back to
161 // the format it expects. If there were any additional
162 // constraints on the output frames there then this may
163 // break nastily.
164 av_buffer_unref(&inlink->hw_frames_ctx);
165 inlink->hw_frames_ctx = source;
166
167 } else if ((outlink->format == hwfc->format &&
168 inlink->format == hwfc->sw_format) ||
169 inlink->format == hwfc->format) {
170 // Map from a hardware format to a software format, or
171 // undo an existing such mapping.
172
173 ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
174 if (!ctx->hwframes_ref) {
175 err = AVERROR(ENOMEM);
176 goto fail;
177 }
178
179 } else {
180 // Non-matching formats - not supported.
181
182 av_log(avctx, AV_LOG_ERROR, "Unsupported formats for "
183 "hwmap: from %s (%s) to %s.\n",
184 av_get_pix_fmt_name(inlink->format),
185 av_get_pix_fmt_name(hwfc->format),
186 av_get_pix_fmt_name(outlink->format));
187 err = AVERROR(EINVAL);
188 goto fail;
189 }
190 } else if (avctx->hw_device_ctx) {
191 // Map from a software format to a hardware format. This
192 // creates a new hwframe context like hwupload, but then
193 // returns frames mapped from that to the previous link in
194 // order to fill them without an additional copy.
195
196 if (!device) {
197 av_log(avctx, AV_LOG_ERROR, "A device reference is "
198 "required to create new frames with reverse "
199 "mapping.\n");
200 err = AVERROR(EINVAL);
201 goto fail;
202 }
203
204 ctx->reverse = 1;
205
206 ctx->hwframes_ref = av_hwframe_ctx_alloc(device);
207 if (!ctx->hwframes_ref) {
208 err = AVERROR(ENOMEM);
209 goto fail;
210 }
211 hwfc = (AVHWFramesContext*)ctx->hwframes_ref->data;
212
213 hwfc->format = outlink->format;
214 hwfc->sw_format = inlink->format;
215 hwfc->width = inlink->w;
216 hwfc->height = inlink->h;
217
218 err = av_hwframe_ctx_init(ctx->hwframes_ref);
219 if (err < 0) {
220 av_log(avctx, AV_LOG_ERROR, "Failed to create frame "
221 "context for reverse mapping: %d.\n", err);
222 goto fail;
223 }
224
225 } else {
226 av_log(avctx, AV_LOG_ERROR, "Mapping requires a hardware "
227 "context (a device, or frames on input).\n");
228 return AVERROR(EINVAL);
229 }
230
231 outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
232 if (!outlink->hw_frames_ctx) {
233 err = AVERROR(ENOMEM);
234 goto fail;
235 }
236
237 outlink->w = inlink->w;
238 outlink->h = inlink->h;
239
240 return 0;
241
242 fail:
243 av_buffer_unref(&ctx->hwframes_ref);
244 return err;
245 }
246
247 static AVFrame *hwmap_get_buffer(AVFilterLink *inlink, int w, int h)
248 {
249 AVFilterContext *avctx = inlink->dst;
250 AVFilterLink *outlink = avctx->outputs[0];
251 HWMapContext *ctx = avctx->priv;
252
253 if (ctx->reverse && !inlink->hw_frames_ctx) {
254 AVFrame *src, *dst;
255 int err;
256
257 src = ff_get_video_buffer(outlink, w, h);
258 if (!src) {
259 av_log(avctx, AV_LOG_ERROR, "Failed to allocate source "
260 "frame for software mapping.\n");
261 return NULL;
262 }
263
264 dst = av_frame_alloc();
265 if (!dst) {
266 av_frame_free(&src);
267 return NULL;
268 }
269
270 err = av_hwframe_map(dst, src, ctx->mode);
271 if (err) {
272 av_log(avctx, AV_LOG_ERROR, "Failed to map frame to "
273 "software: %d.\n", err);
274 av_frame_free(&src);
275 av_frame_free(&dst);
276 return NULL;
277 }
278
279 av_frame_free(&src);
280 return dst;
281 } else {
282 return ff_default_get_video_buffer(inlink, w, h);
283 }
284 }
285
286 static int hwmap_filter_frame(AVFilterLink *link, AVFrame *input)
287 {
288 AVFilterContext *avctx = link->dst;
289 AVFilterLink *outlink = avctx->outputs[0];
290 HWMapContext *ctx = avctx->priv;
291 AVFrame *map = NULL;
292 int err;
293
294 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
295 av_get_pix_fmt_name(input->format),
296 input->width, input->height, input->pts);
297
298 map = av_frame_alloc();
299 if (!map) {
300 err = AVERROR(ENOMEM);
301 goto fail;
302 }
303
304 map->format = outlink->format;
305 map->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
306 if (!map->hw_frames_ctx) {
307 err = AVERROR(ENOMEM);
308 goto fail;
309 }
310
311 if (ctx->reverse && !input->hw_frames_ctx) {
312 // If we mapped backwards from hardware to software, we need
313 // to attach the hardware frame context to the input frame to
314 // make the mapping visible to av_hwframe_map().
315 input->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
316 if (!input->hw_frames_ctx) {
317 err = AVERROR(ENOMEM);
318 goto fail;
319 }
320 }
321
322 err = av_hwframe_map(map, input, ctx->mode);
323 if (err < 0) {
324 av_log(avctx, AV_LOG_ERROR, "Failed to map frame: %d.\n", err);
325 goto fail;
326 }
327
328 err = av_frame_copy_props(map, input);
329 if (err < 0)
330 goto fail;
331
332 av_frame_free(&input);
333
334 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
335 av_get_pix_fmt_name(map->format),
336 map->width, map->height, map->pts);
337
338 return ff_filter_frame(outlink, map);
339
340 fail:
341 av_frame_free(&input);
342 av_frame_free(&map);
343 return err;
344 }
345
346 static av_cold void hwmap_uninit(AVFilterContext *avctx)
347 {
348 HWMapContext *ctx = avctx->priv;
349
350 av_buffer_unref(&ctx->hwframes_ref);
351 }
352
353 #define OFFSET(x) offsetof(HWMapContext, x)
354 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM)
355 static const AVOption hwmap_options[] = {
356 { "mode", "Frame mapping mode",
357 OFFSET(mode), AV_OPT_TYPE_FLAGS,
358 { .i64 = AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE },
359 0, INT_MAX, FLAGS, "mode" },
360
361 { "read", "Mapping should be readable",
362 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_READ },
363 INT_MIN, INT_MAX, FLAGS, "mode" },
364 { "write", "Mapping should be writeable",
365 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_WRITE },
366 INT_MIN, INT_MAX, FLAGS, "mode" },
367 { "overwrite", "Mapping will always overwrite the entire frame",
368 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_OVERWRITE },
369 INT_MIN, INT_MAX, FLAGS, "mode" },
370 { "direct", "Mapping should not involve any copying",
371 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT },
372 INT_MIN, INT_MAX, FLAGS, "mode" },
373
374 { "derive_device", "Derive a new device of this type",
375 OFFSET(derive_device_type), AV_OPT_TYPE_STRING,
376 { .str = NULL }, 0, 0, FLAGS },
377 { "reverse", "Map in reverse (create and allocate in the sink)",
378 OFFSET(reverse), AV_OPT_TYPE_INT,
379 { .i64 = 0 }, 0, 1, FLAGS },
380
381 { NULL },
382 };
383
384 static const AVClass hwmap_class = {
385 .class_name = "hwmap",
386 .item_name = av_default_item_name,
387 .option = hwmap_options,
388 .version = LIBAVUTIL_VERSION_INT,
389 };
390
391 static const AVFilterPad hwmap_inputs[] = {
392 {
393 .name = "default",
394 .type = AVMEDIA_TYPE_VIDEO,
395 .get_video_buffer = &hwmap_get_buffer,
396 .filter_frame = &hwmap_filter_frame,
397 },
398 { NULL }
399 };
400
401 static const AVFilterPad hwmap_outputs[] = {
402 {
403 .name = "default",
404 .type = AVMEDIA_TYPE_VIDEO,
405 .config_props = &hwmap_config_output,
406 },
407 { NULL }
408 };
409
410 AVFilter ff_vf_hwmap = {
411 .name = "hwmap",
412 .description = NULL_IF_CONFIG_SMALL("Map hardware frames"),
413 .uninit = &hwmap_uninit,
414 .priv_size = sizeof(HWMapContext),
415 .priv_class = &hwmap_class,
416 .query_formats = &hwmap_query_formats,
417 .inputs = hwmap_inputs,
418 .outputs = hwmap_outputs,
419 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
420 };