h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_scale_vaapi.c
CommitLineData
98114d70
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 <string.h>
20
21#include <va/va.h>
22#include <va/va_vpp.h>
23
24#include "libavutil/avassert.h"
b128be17 25#include "libavutil/common.h"
98114d70
MT
26#include "libavutil/hwcontext.h"
27#include "libavutil/hwcontext_vaapi.h"
28#include "libavutil/mem.h"
29#include "libavutil/opt.h"
30#include "libavutil/pixdesc.h"
31
32#include "avfilter.h"
33#include "formats.h"
34#include "internal.h"
7e2561fa 35#include "video.h"
98114d70
MT
36
37typedef struct ScaleVAAPIContext {
38 const AVClass *class;
39
40 AVVAAPIDeviceContext *hwctx;
41 AVBufferRef *device_ref;
42
43 int valid_ids;
44 VAConfigID va_config;
45 VAContextID va_context;
46
47 AVBufferRef *input_frames_ref;
48 AVHWFramesContext *input_frames;
49
98114d70
MT
50 char *output_format_string;
51 enum AVPixelFormat output_format;
52 int output_width;
53 int output_height;
54
55} ScaleVAAPIContext;
56
57
58static int scale_vaapi_query_formats(AVFilterContext *avctx)
59{
60 enum AVPixelFormat pix_fmts[] = {
61 AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
62 };
63
64 ff_formats_ref(ff_make_format_list(pix_fmts),
65 &avctx->inputs[0]->out_formats);
66 ff_formats_ref(ff_make_format_list(pix_fmts),
67 &avctx->outputs[0]->in_formats);
68
69 return 0;
70}
71
72static int scale_vaapi_pipeline_uninit(ScaleVAAPIContext *ctx)
73{
74 if (ctx->va_context != VA_INVALID_ID) {
75 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
76 ctx->va_context = VA_INVALID_ID;
77 }
78
79 if (ctx->va_config != VA_INVALID_ID) {
80 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
81 ctx->va_config = VA_INVALID_ID;
82 }
83
98114d70
MT
84 av_buffer_unref(&ctx->device_ref);
85 ctx->hwctx = 0;
86
87 return 0;
88}
89
90static int scale_vaapi_config_input(AVFilterLink *inlink)
91{
92 AVFilterContext *avctx = inlink->dst;
93 ScaleVAAPIContext *ctx = avctx->priv;
94
95 scale_vaapi_pipeline_uninit(ctx);
96
97 if (!inlink->hw_frames_ctx) {
98 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
99 "required to associate the processing device.\n");
100 return AVERROR(EINVAL);
101 }
102
103 ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
104 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
105
106 return 0;
107}
108
109static int scale_vaapi_config_output(AVFilterLink *outlink)
110{
111 AVFilterContext *avctx = outlink->src;
112 ScaleVAAPIContext *ctx = avctx->priv;
113 AVVAAPIHWConfig *hwconfig = NULL;
114 AVHWFramesConstraints *constraints = NULL;
b128be17 115 AVHWFramesContext *output_frames;
98114d70
MT
116 AVVAAPIFramesContext *va_frames;
117 VAStatus vas;
118 int err, i;
119
120 scale_vaapi_pipeline_uninit(ctx);
121
122 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
123 ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
124
125 av_assert0(ctx->va_config == VA_INVALID_ID);
126 vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
127 VAEntrypointVideoProc, 0, 0, &ctx->va_config);
128 if (vas != VA_STATUS_SUCCESS) {
129 av_log(ctx, AV_LOG_ERROR, "Failed to create processing pipeline "
130 "config: %d (%s).\n", vas, vaErrorStr(vas));
131 err = AVERROR(EIO);
132 goto fail;
133 }
134
135 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
136 if (!hwconfig) {
137 err = AVERROR(ENOMEM);
138 goto fail;
139 }
140 hwconfig->config_id = ctx->va_config;
141
142 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
143 hwconfig);
144 if (!constraints) {
145 err = AVERROR(ENOMEM);
146 goto fail;
147 }
148
149 if (ctx->output_format == AV_PIX_FMT_NONE)
150 ctx->output_format = ctx->input_frames->sw_format;
151 if (constraints->valid_sw_formats) {
152 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
153 if (ctx->output_format == constraints->valid_sw_formats[i])
154 break;
155 }
156 if (constraints->valid_sw_formats[i] == AV_PIX_FMT_NONE) {
157 av_log(ctx, AV_LOG_ERROR, "Hardware does not support output "
158 "format %s.\n", av_get_pix_fmt_name(ctx->output_format));
159 err = AVERROR(EINVAL);
160 goto fail;
161 }
162 }
163
164 if (ctx->output_width < constraints->min_width ||
165 ctx->output_height < constraints->min_height ||
166 ctx->output_width > constraints->max_width ||
167 ctx->output_height > constraints->max_height) {
168 av_log(ctx, AV_LOG_ERROR, "Hardware does not support scaling to "
169 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
170 ctx->output_width, ctx->output_height,
171 constraints->min_width, constraints->max_width,
172 constraints->min_height, constraints->max_height);
173 err = AVERROR(EINVAL);
174 goto fail;
175 }
176
b128be17
MT
177 outlink->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->device_ref);
178 if (!outlink->hw_frames_ctx) {
98114d70
MT
179 av_log(ctx, AV_LOG_ERROR, "Failed to create HW frame context "
180 "for output.\n");
181 err = AVERROR(ENOMEM);
182 goto fail;
183 }
184
b128be17
MT
185 output_frames = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
186
187 output_frames->format = AV_PIX_FMT_VAAPI;
188 output_frames->sw_format = ctx->output_format;
189 output_frames->width = ctx->output_width;
190 output_frames->height = ctx->output_height;
98114d70 191
b128be17 192 output_frames->initial_pool_size = 4;
98114d70 193
b128be17
MT
194 err = ff_filter_init_hw_frames(avctx, outlink, 10);
195 if (err < 0)
196 goto fail;
98114d70 197
b128be17 198 err = av_hwframe_ctx_init(outlink->hw_frames_ctx);
98114d70
MT
199 if (err < 0) {
200 av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
201 "context for output: %d\n", err);
202 goto fail;
203 }
204
b128be17 205 va_frames = output_frames->hwctx;
98114d70
MT
206
207 av_assert0(ctx->va_context == VA_INVALID_ID);
208 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
209 ctx->output_width, ctx->output_height,
210 VA_PROGRESSIVE,
211 va_frames->surface_ids, va_frames->nb_surfaces,
212 &ctx->va_context);
213 if (vas != VA_STATUS_SUCCESS) {
214 av_log(ctx, AV_LOG_ERROR, "Failed to create processing pipeline "
215 "context: %d (%s).\n", vas, vaErrorStr(vas));
216 return AVERROR(EIO);
217 }
218
219 outlink->w = ctx->output_width;
220 outlink->h = ctx->output_height;
221
98114d70
MT
222 av_freep(&hwconfig);
223 av_hwframe_constraints_free(&constraints);
224 return 0;
225
226fail:
b128be17 227 av_buffer_unref(&outlink->hw_frames_ctx);
98114d70
MT
228 av_freep(&hwconfig);
229 av_hwframe_constraints_free(&constraints);
230 return err;
231}
232
233static int vaapi_proc_colour_standard(enum AVColorSpace av_cs)
234{
235 switch(av_cs) {
236#define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
237 CS(BT709, BT709);
238 CS(BT470BG, BT601);
239 CS(SMPTE170M, SMPTE170M);
240 CS(SMPTE240M, SMPTE240M);
241#undef CS
242 default:
243 return VAProcColorStandardNone;
244 }
245}
246
247static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
248{
249 AVFilterContext *avctx = inlink->dst;
250 AVFilterLink *outlink = avctx->outputs[0];
251 ScaleVAAPIContext *ctx = avctx->priv;
252 AVFrame *output_frame = NULL;
253 VASurfaceID input_surface, output_surface;
254 VAProcPipelineParameterBuffer params;
255 VABufferID params_id;
bdf7610e 256 VARectangle input_region;
98114d70
MT
257 VAStatus vas;
258 int err;
259
260 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
261 av_get_pix_fmt_name(input_frame->format),
262 input_frame->width, input_frame->height, input_frame->pts);
263
264 if (ctx->va_context == VA_INVALID_ID)
265 return AVERROR(EINVAL);
266
267 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
268 av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for scale input.\n",
269 input_surface);
270
7e2561fa
MT
271 output_frame = ff_get_video_buffer(outlink, ctx->output_width,
272 ctx->output_height);
98114d70 273 if (!output_frame) {
98114d70
MT
274 err = AVERROR(ENOMEM);
275 goto fail;
276 }
277
98114d70
MT
278 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
279 av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for scale output.\n",
280 output_surface);
281
282 memset(&params, 0, sizeof(params));
283
bdf7610e
MT
284 // If there were top/left cropping, it could be taken into
285 // account here.
286 input_region = (VARectangle) {
287 .x = 0,
288 .y = 0,
289 .width = input_frame->width,
290 .height = input_frame->height,
291 };
292
98114d70 293 params.surface = input_surface;
bdf7610e 294 params.surface_region = &input_region;
98114d70
MT
295 params.surface_color_standard =
296 vaapi_proc_colour_standard(input_frame->colorspace);
297
298 params.output_region = 0;
299 params.output_background_color = 0xff000000;
300 params.output_color_standard = params.surface_color_standard;
301
302 params.pipeline_flags = 0;
303 params.filter_flags = VA_FILTER_SCALING_HQ;
304
305 vas = vaBeginPicture(ctx->hwctx->display,
306 ctx->va_context, output_surface);
307 if (vas != VA_STATUS_SUCCESS) {
308 av_log(ctx, AV_LOG_ERROR, "Failed to attach new picture: "
309 "%d (%s).\n", vas, vaErrorStr(vas));
310 err = AVERROR(EIO);
311 goto fail;
312 }
313
314 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
315 VAProcPipelineParameterBufferType,
316 sizeof(params), 1, &params, &params_id);
317 if (vas != VA_STATUS_SUCCESS) {
318 av_log(ctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
319 "%d (%s).\n", vas, vaErrorStr(vas));
320 err = AVERROR(EIO);
321 goto fail_after_begin;
322 }
323 av_log(ctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
324 params_id);
325
326 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
327 &params_id, 1);
328 if (vas != VA_STATUS_SUCCESS) {
329 av_log(ctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
330 "%d (%s).\n", vas, vaErrorStr(vas));
331 err = AVERROR(EIO);
332 goto fail_after_begin;
333 }
334
335 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
336 if (vas != VA_STATUS_SUCCESS) {
337 av_log(ctx, AV_LOG_ERROR, "Failed to start picture processing: "
338 "%d (%s).\n", vas, vaErrorStr(vas));
339 err = AVERROR(EIO);
340 goto fail_after_render;
341 }
342
bfc83acf 343 if (HAVE_VAAPI_1 || ctx->hwctx->driver_quirks &
582d4211
MT
344 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
345 vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
346 if (vas != VA_STATUS_SUCCESS) {
347 av_log(ctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
348 "%d (%s).\n", vas, vaErrorStr(vas));
349 // And ignore.
350 }
98114d70
MT
351 }
352
353 av_frame_copy_props(output_frame, input_frame);
354 av_frame_free(&input_frame);
355
356 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
357 av_get_pix_fmt_name(output_frame->format),
358 output_frame->width, output_frame->height, output_frame->pts);
359
360 return ff_filter_frame(outlink, output_frame);
361
362 // We want to make sure that if vaBeginPicture has been called, we also
363 // call vaRenderPicture and vaEndPicture. These calls may well fail or
364 // do something else nasty, but once we're in this failure case there
365 // isn't much else we can do.
366fail_after_begin:
367 vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
368fail_after_render:
369 vaEndPicture(ctx->hwctx->display, ctx->va_context);
370fail:
371 av_frame_free(&input_frame);
372 av_frame_free(&output_frame);
373 return err;
374}
375
376static av_cold int scale_vaapi_init(AVFilterContext *avctx)
377{
378 ScaleVAAPIContext *ctx = avctx->priv;
379
380 ctx->va_config = VA_INVALID_ID;
381 ctx->va_context = VA_INVALID_ID;
382 ctx->valid_ids = 1;
383
384 if (ctx->output_format_string) {
385 ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
386 if (ctx->output_format == AV_PIX_FMT_NONE) {
387 av_log(ctx, AV_LOG_ERROR, "Invalid output format.\n");
388 return AVERROR(EINVAL);
389 }
390 } else {
391 // Use the input format once that is configured.
392 ctx->output_format = AV_PIX_FMT_NONE;
393 }
394
395 return 0;
396}
397
398static av_cold void scale_vaapi_uninit(AVFilterContext *avctx)
399{
400 ScaleVAAPIContext *ctx = avctx->priv;
401
402 if (ctx->valid_ids)
403 scale_vaapi_pipeline_uninit(ctx);
404
405 av_buffer_unref(&ctx->input_frames_ref);
98114d70
MT
406 av_buffer_unref(&ctx->device_ref);
407}
408
409
410#define OFFSET(x) offsetof(ScaleVAAPIContext, x)
411#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM)
412static const AVOption scale_vaapi_options[] = {
413 { "w", "Output video width",
414 OFFSET(output_width), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
415 { "h", "Output video height",
416 OFFSET(output_height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
417 { "format", "Output video format (software format of hardware frames)",
418 OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
419 { NULL },
420};
421
422static const AVClass scale_vaapi_class = {
423 .class_name = "scale_vaapi",
424 .item_name = av_default_item_name,
425 .option = scale_vaapi_options,
426 .version = LIBAVUTIL_VERSION_INT,
427};
428
429static const AVFilterPad scale_vaapi_inputs[] = {
430 {
431 .name = "default",
432 .type = AVMEDIA_TYPE_VIDEO,
433 .filter_frame = &scale_vaapi_filter_frame,
434 .config_props = &scale_vaapi_config_input,
435 },
436 { NULL }
437};
438
439static const AVFilterPad scale_vaapi_outputs[] = {
440 {
441 .name = "default",
442 .type = AVMEDIA_TYPE_VIDEO,
443 .config_props = &scale_vaapi_config_output,
444 },
445 { NULL }
446};
447
448AVFilter ff_vf_scale_vaapi = {
449 .name = "scale_vaapi",
450 .description = NULL_IF_CONFIG_SMALL("Scale to/from VAAPI surfaces."),
451 .priv_size = sizeof(ScaleVAAPIContext),
452 .init = &scale_vaapi_init,
453 .uninit = &scale_vaapi_uninit,
454 .query_formats = &scale_vaapi_query_formats,
455 .inputs = scale_vaapi_inputs,
456 .outputs = scale_vaapi_outputs,
457 .priv_class = &scale_vaapi_class,
e3fb74f7 458 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
98114d70 459};