2 * This file is part of Libav.
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.
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.
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
22 #include <va/va_vpp.h>
24 #include "libavutil/avassert.h"
25 #include "libavutil/hwcontext.h"
26 #include "libavutil/hwcontext_vaapi.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
36 #define MAX_REFERENCES 8
38 typedef struct DeintVAAPIContext {
41 AVVAAPIDeviceContext *hwctx;
42 AVBufferRef *device_ref;
48 VAContextID va_context;
50 AVBufferRef *input_frames_ref;
51 AVHWFramesContext *input_frames;
53 AVBufferRef *output_frames_ref;
54 AVHWFramesContext *output_frames;
58 VAProcFilterCapDeinterlacing
59 deint_caps[VAProcDeinterlacingCount];
61 VAProcPipelineCaps pipeline_caps;
65 AVFrame *frame_queue[MAX_REFERENCES];
67 VABufferID filter_buffer;
70 static const char *deint_vaapi_mode_name(int mode)
73 #define D(name) case VAProcDeinterlacing ## name: return #name
84 static int deint_vaapi_query_formats(AVFilterContext *avctx)
86 enum AVPixelFormat pix_fmts[] = {
87 AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
90 ff_formats_ref(ff_make_format_list(pix_fmts),
91 &avctx->inputs[0]->out_formats);
92 ff_formats_ref(ff_make_format_list(pix_fmts),
93 &avctx->outputs[0]->in_formats);
98 static int deint_vaapi_pipeline_uninit(AVFilterContext *avctx)
100 DeintVAAPIContext *ctx = avctx->priv;
103 for (i = 0; i < ctx->queue_count; i++)
104 av_frame_free(&ctx->frame_queue[i]);
105 ctx->queue_count = 0;
107 if (ctx->filter_buffer != VA_INVALID_ID) {
108 vaDestroyBuffer(ctx->hwctx->display, ctx->filter_buffer);
109 ctx->filter_buffer = VA_INVALID_ID;
112 if (ctx->va_context != VA_INVALID_ID) {
113 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
114 ctx->va_context = VA_INVALID_ID;
117 if (ctx->va_config != VA_INVALID_ID) {
118 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
119 ctx->va_config = VA_INVALID_ID;
122 av_buffer_unref(&ctx->device_ref);
128 static int deint_vaapi_config_input(AVFilterLink *inlink)
130 AVFilterContext *avctx = inlink->dst;
131 DeintVAAPIContext *ctx = avctx->priv;
133 deint_vaapi_pipeline_uninit(avctx);
135 if (!inlink->hw_frames_ctx) {
136 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
137 "required to associate the processing device.\n");
138 return AVERROR(EINVAL);
141 ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
142 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
147 static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
149 DeintVAAPIContext *ctx = avctx->priv;
151 VAProcFilterParameterBufferDeinterlacing params;
154 ctx->nb_deint_caps = VAProcDeinterlacingCount;
155 vas = vaQueryVideoProcFilterCaps(ctx->hwctx->display,
157 VAProcFilterDeinterlacing,
159 &ctx->nb_deint_caps);
160 if (vas != VA_STATUS_SUCCESS) {
161 av_log(avctx, AV_LOG_ERROR, "Failed to query deinterlacing "
162 "caps: %d (%s).\n", vas, vaErrorStr(vas));
166 if (ctx->mode == VAProcDeinterlacingNone) {
167 for (i = 0; i < ctx->nb_deint_caps; i++) {
168 if (ctx->deint_caps[i].type > ctx->mode)
169 ctx->mode = ctx->deint_caps[i].type;
171 av_log(avctx, AV_LOG_VERBOSE, "Picking %d (%s) as default "
172 "deinterlacing mode.\n", ctx->mode,
173 deint_vaapi_mode_name(ctx->mode));
175 for (i = 0; i < ctx->nb_deint_caps; i++) {
176 if (ctx->deint_caps[i].type == ctx->mode)
179 if (i >= ctx->nb_deint_caps) {
180 av_log(avctx, AV_LOG_ERROR, "Deinterlacing mode %d (%s) is "
181 "not supported.\n", ctx->mode,
182 deint_vaapi_mode_name(ctx->mode));
186 params.type = VAProcFilterDeinterlacing;
187 params.algorithm = ctx->mode;
190 av_assert0(ctx->filter_buffer == VA_INVALID_ID);
191 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
192 VAProcFilterParameterBufferType,
193 sizeof(params), 1, ¶ms,
194 &ctx->filter_buffer);
195 if (vas != VA_STATUS_SUCCESS) {
196 av_log(avctx, AV_LOG_ERROR, "Failed to create deinterlace "
197 "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
201 vas = vaQueryVideoProcPipelineCaps(ctx->hwctx->display,
203 &ctx->filter_buffer, 1,
204 &ctx->pipeline_caps);
205 if (vas != VA_STATUS_SUCCESS) {
206 av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
207 "caps: %d (%s).\n", vas, vaErrorStr(vas));
211 ctx->queue_depth = ctx->pipeline_caps.num_backward_references +
212 ctx->pipeline_caps.num_forward_references + 1;
213 if (ctx->queue_depth > MAX_REFERENCES) {
214 av_log(avctx, AV_LOG_ERROR, "Pipeline requires too many "
215 "references (%u forward, %u back).\n",
216 ctx->pipeline_caps.num_forward_references,
217 ctx->pipeline_caps.num_backward_references);
218 return AVERROR(ENOSYS);
224 static int deint_vaapi_config_output(AVFilterLink *outlink)
226 AVFilterContext *avctx = outlink->src;
227 DeintVAAPIContext *ctx = avctx->priv;
228 AVVAAPIHWConfig *hwconfig = NULL;
229 AVHWFramesConstraints *constraints = NULL;
230 AVVAAPIFramesContext *va_frames;
234 deint_vaapi_pipeline_uninit(avctx);
236 av_assert0(ctx->input_frames);
237 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
238 ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
240 ctx->output_width = ctx->input_frames->width;
241 ctx->output_height = ctx->input_frames->height;
243 av_assert0(ctx->va_config == VA_INVALID_ID);
244 vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
245 VAEntrypointVideoProc, 0, 0, &ctx->va_config);
246 if (vas != VA_STATUS_SUCCESS) {
247 av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
248 "config: %d (%s).\n", vas, vaErrorStr(vas));
253 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
255 err = AVERROR(ENOMEM);
258 hwconfig->config_id = ctx->va_config;
260 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
263 err = AVERROR(ENOMEM);
267 if (ctx->output_width < constraints->min_width ||
268 ctx->output_height < constraints->min_height ||
269 ctx->output_width > constraints->max_width ||
270 ctx->output_height > constraints->max_height) {
271 av_log(avctx, AV_LOG_ERROR, "Hardware does not support "
272 "deinterlacing to size %dx%d "
273 "(constraints: width %d-%d height %d-%d).\n",
274 ctx->output_width, ctx->output_height,
275 constraints->min_width, constraints->max_width,
276 constraints->min_height, constraints->max_height);
277 err = AVERROR(EINVAL);
281 ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
282 if (!ctx->output_frames_ref) {
283 av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
285 err = AVERROR(ENOMEM);
289 ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
291 ctx->output_frames->format = AV_PIX_FMT_VAAPI;
292 ctx->output_frames->sw_format = ctx->input_frames->sw_format;
293 ctx->output_frames->width = ctx->output_width;
294 ctx->output_frames->height = ctx->output_height;
296 // The number of output frames we need is determined by what follows
297 // the filter. If it's an encoder with complex frame reference
298 // structures then this could be very high.
299 ctx->output_frames->initial_pool_size = 10;
301 err = av_hwframe_ctx_init(ctx->output_frames_ref);
303 av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
304 "context for output: %d\n", err);
308 va_frames = ctx->output_frames->hwctx;
310 av_assert0(ctx->va_context == VA_INVALID_ID);
311 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
312 ctx->output_width, ctx->output_height, 0,
313 va_frames->surface_ids, va_frames->nb_surfaces,
315 if (vas != VA_STATUS_SUCCESS) {
316 av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
317 "context: %d (%s).\n", vas, vaErrorStr(vas));
322 err = deint_vaapi_build_filter_params(avctx);
326 outlink->w = ctx->output_width;
327 outlink->h = ctx->output_height;
329 outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
330 if (!outlink->hw_frames_ctx) {
331 err = AVERROR(ENOMEM);
336 av_hwframe_constraints_free(&constraints);
340 av_buffer_unref(&ctx->output_frames_ref);
342 av_hwframe_constraints_free(&constraints);
346 static int vaapi_proc_colour_standard(enum AVColorSpace av_cs)
349 #define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
351 CS(BT470BG, BT470BG);
352 CS(SMPTE170M, SMPTE170M);
353 CS(SMPTE240M, SMPTE240M);
356 return VAProcColorStandardNone;
360 static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
362 AVFilterContext *avctx = inlink->dst;
363 AVFilterLink *outlink = avctx->outputs[0];
364 DeintVAAPIContext *ctx = avctx->priv;
365 AVFrame *output_frame = NULL;
366 VASurfaceID input_surface, output_surface;
367 VASurfaceID backward_references[MAX_REFERENCES];
368 VASurfaceID forward_references[MAX_REFERENCES];
369 VAProcPipelineParameterBuffer params;
370 VAProcFilterParameterBufferDeinterlacing *filter_params;
371 VARectangle input_region;
372 VABufferID params_id;
374 void *filter_params_addr = NULL;
377 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
378 av_get_pix_fmt_name(input_frame->format),
379 input_frame->width, input_frame->height, input_frame->pts);
381 if (ctx->queue_count < ctx->queue_depth) {
382 ctx->frame_queue[ctx->queue_count++] = input_frame;
383 if (ctx->queue_count < ctx->queue_depth) {
384 // Need more reference surfaces before we can continue.
388 av_frame_free(&ctx->frame_queue[0]);
389 for (i = 0; i + 1 < ctx->queue_count; i++)
390 ctx->frame_queue[i] = ctx->frame_queue[i + 1];
391 ctx->frame_queue[i] = input_frame;
395 ctx->frame_queue[ctx->pipeline_caps.num_backward_references];
396 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
397 for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
398 backward_references[i] = (VASurfaceID)(uintptr_t)
399 ctx->frame_queue[ctx->pipeline_caps.num_backward_references -
401 for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
402 forward_references[i] = (VASurfaceID)(uintptr_t)
403 ctx->frame_queue[ctx->pipeline_caps.num_backward_references +
406 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
407 "deinterlace input.\n", input_surface);
408 av_log(avctx, AV_LOG_DEBUG, "Backward references:");
409 for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
410 av_log(avctx, AV_LOG_DEBUG, " %#x", backward_references[i]);
411 av_log(avctx, AV_LOG_DEBUG, "\n");
412 av_log(avctx, AV_LOG_DEBUG, "Forward references:");
413 for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
414 av_log(avctx, AV_LOG_DEBUG, " %#x", forward_references[i]);
415 av_log(avctx, AV_LOG_DEBUG, "\n");
417 output_frame = ff_get_video_buffer(outlink, ctx->output_width,
420 err = AVERROR(ENOMEM);
424 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
425 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
426 "deinterlace output.\n", output_surface);
428 memset(¶ms, 0, sizeof(params));
430 input_region = (VARectangle) {
433 .width = input_frame->width,
434 .height = input_frame->height,
437 params.surface = input_surface;
438 params.surface_region = &input_region;
439 params.surface_color_standard =
440 vaapi_proc_colour_standard(input_frame->colorspace);
442 params.output_region = NULL;
443 params.output_background_color = 0xff000000;
444 params.output_color_standard = params.surface_color_standard;
446 params.pipeline_flags = 0;
447 params.filter_flags = VA_FRAME_PICTURE;
449 vas = vaMapBuffer(ctx->hwctx->display, ctx->filter_buffer,
450 &filter_params_addr);
451 if (vas != VA_STATUS_SUCCESS) {
452 av_log(avctx, AV_LOG_ERROR, "Failed to map filter parameter "
453 "buffer: %d (%s).\n", vas, vaErrorStr(vas));
457 filter_params = filter_params_addr;
458 filter_params->flags = 0;
459 if (input_frame->interlaced_frame && !input_frame->top_field_first)
460 filter_params->flags |= VA_DEINTERLACING_BOTTOM_FIELD_FIRST;
461 filter_params_addr = NULL;
462 vas = vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
463 if (vas != VA_STATUS_SUCCESS)
464 av_log(avctx, AV_LOG_ERROR, "Failed to unmap filter parameter "
465 "buffer: %d (%s).\n", vas, vaErrorStr(vas));
467 params.filters = &ctx->filter_buffer;
468 params.num_filters = 1;
470 params.forward_references = forward_references;
471 params.num_forward_references =
472 ctx->pipeline_caps.num_forward_references;
473 params.backward_references = backward_references;
474 params.num_backward_references =
475 ctx->pipeline_caps.num_backward_references;
477 vas = vaBeginPicture(ctx->hwctx->display,
478 ctx->va_context, output_surface);
479 if (vas != VA_STATUS_SUCCESS) {
480 av_log(avctx, AV_LOG_ERROR, "Failed to attach new picture: "
481 "%d (%s).\n", vas, vaErrorStr(vas));
486 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
487 VAProcPipelineParameterBufferType,
488 sizeof(params), 1, ¶ms, ¶ms_id);
489 if (vas != VA_STATUS_SUCCESS) {
490 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
491 "%d (%s).\n", vas, vaErrorStr(vas));
493 goto fail_after_begin;
495 av_log(avctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
498 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
500 if (vas != VA_STATUS_SUCCESS) {
501 av_log(avctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
502 "%d (%s).\n", vas, vaErrorStr(vas));
504 goto fail_after_begin;
507 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
508 if (vas != VA_STATUS_SUCCESS) {
509 av_log(avctx, AV_LOG_ERROR, "Failed to start picture processing: "
510 "%d (%s).\n", vas, vaErrorStr(vas));
512 goto fail_after_render;
515 if (ctx->hwctx->driver_quirks &
516 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
517 vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
518 if (vas != VA_STATUS_SUCCESS) {
519 av_log(avctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
520 "%d (%s).\n", vas, vaErrorStr(vas));
525 err = av_frame_copy_props(output_frame, input_frame);
529 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
530 av_get_pix_fmt_name(output_frame->format),
531 output_frame->width, output_frame->height, output_frame->pts);
533 return ff_filter_frame(outlink, output_frame);
536 vaRenderPicture(ctx->hwctx->display, ctx->va_context, ¶ms_id, 1);
538 vaEndPicture(ctx->hwctx->display, ctx->va_context);
540 if (filter_params_addr)
541 vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
542 av_frame_free(&output_frame);
546 static av_cold int deint_vaapi_init(AVFilterContext *avctx)
548 DeintVAAPIContext *ctx = avctx->priv;
550 ctx->va_config = VA_INVALID_ID;
551 ctx->va_context = VA_INVALID_ID;
552 ctx->filter_buffer = VA_INVALID_ID;
558 static av_cold void deint_vaapi_uninit(AVFilterContext *avctx)
560 DeintVAAPIContext *ctx = avctx->priv;
563 deint_vaapi_pipeline_uninit(avctx);
565 av_buffer_unref(&ctx->input_frames_ref);
566 av_buffer_unref(&ctx->output_frames_ref);
567 av_buffer_unref(&ctx->device_ref);
570 #define OFFSET(x) offsetof(DeintVAAPIContext, x)
571 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM)
572 static const AVOption deint_vaapi_options[] = {
573 { "mode", "Deinterlacing mode",
574 OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VAProcDeinterlacingNone },
575 VAProcDeinterlacingNone, VAProcDeinterlacingCount - 1, FLAGS, "mode" },
576 { "default", "Use the highest-numbered (and therefore possibly most advanced) deinterlacing algorithm",
577 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingNone }, .unit = "mode" },
578 { "bob", "Use the bob deinterlacing algorithm",
579 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingBob }, .unit = "mode" },
580 { "weave", "Use the weave deinterlacing algorithm",
581 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingWeave }, .unit = "mode" },
582 { "motion_adaptive", "Use the motion adaptive deinterlacing algorithm",
583 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingMotionAdaptive }, .unit = "mode" },
584 { "motion_compensated", "Use the motion compensated deinterlacing algorithm",
585 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingMotionCompensated }, .unit = "mode" },
589 static const AVClass deint_vaapi_class = {
590 .class_name = "deinterlace_vaapi",
591 .item_name = av_default_item_name,
592 .option = deint_vaapi_options,
593 .version = LIBAVUTIL_VERSION_INT,
596 static const AVFilterPad deint_vaapi_inputs[] = {
599 .type = AVMEDIA_TYPE_VIDEO,
600 .filter_frame = &deint_vaapi_filter_frame,
601 .config_props = &deint_vaapi_config_input,
606 static const AVFilterPad deint_vaapi_outputs[] = {
609 .type = AVMEDIA_TYPE_VIDEO,
610 .config_props = &deint_vaapi_config_output,
615 AVFilter ff_vf_deinterlace_vaapi = {
616 .name = "deinterlace_vaapi",
617 .description = NULL_IF_CONFIG_SMALL("Deinterlacing of VAAPI surfaces"),
618 .priv_size = sizeof(DeintVAAPIContext),
619 .init = &deint_vaapi_init,
620 .uninit = &deint_vaapi_uninit,
621 .query_formats = &deint_vaapi_query_formats,
622 .inputs = deint_vaapi_inputs,
623 .outputs = deint_vaapi_outputs,
624 .priv_class = &deint_vaapi_class,
625 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,