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