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