2 * This file is part of FFmpeg.
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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/common.h"
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"
37 #define MAX_REFERENCES 8
39 typedef struct DeintVAAPIContext {
42 AVVAAPIDeviceContext *hwctx;
43 AVBufferRef *device_ref;
51 VAContextID va_context;
53 AVBufferRef *input_frames_ref;
54 AVHWFramesContext *input_frames;
56 AVBufferRef *output_frames_ref;
57 AVHWFramesContext *output_frames;
61 VAProcFilterCapDeinterlacing
62 deint_caps[VAProcDeinterlacingCount];
64 VAProcPipelineCaps pipeline_caps;
68 AVFrame *frame_queue[MAX_REFERENCES];
69 int extra_delay_for_timestamps;
71 VABufferID filter_buffer;
74 static const char *deint_vaapi_mode_name(int mode)
77 #define D(name) case VAProcDeinterlacing ## name: return #name
88 static int deint_vaapi_query_formats(AVFilterContext *avctx)
90 enum AVPixelFormat pix_fmts[] = {
91 AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
95 if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
96 &avctx->inputs[0]->out_formats)) < 0)
98 if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
99 &avctx->outputs[0]->in_formats)) < 0)
105 static int deint_vaapi_pipeline_uninit(AVFilterContext *avctx)
107 DeintVAAPIContext *ctx = avctx->priv;
110 for (i = 0; i < ctx->queue_count; i++)
111 av_frame_free(&ctx->frame_queue[i]);
112 ctx->queue_count = 0;
114 if (ctx->filter_buffer != VA_INVALID_ID) {
115 vaDestroyBuffer(ctx->hwctx->display, ctx->filter_buffer);
116 ctx->filter_buffer = VA_INVALID_ID;
119 if (ctx->va_context != VA_INVALID_ID) {
120 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
121 ctx->va_context = VA_INVALID_ID;
124 if (ctx->va_config != VA_INVALID_ID) {
125 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
126 ctx->va_config = VA_INVALID_ID;
129 av_buffer_unref(&ctx->device_ref);
135 static int deint_vaapi_config_input(AVFilterLink *inlink)
137 AVFilterContext *avctx = inlink->dst;
138 DeintVAAPIContext *ctx = avctx->priv;
140 deint_vaapi_pipeline_uninit(avctx);
142 if (!inlink->hw_frames_ctx) {
143 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
144 "required to associate the processing device.\n");
145 return AVERROR(EINVAL);
148 ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
149 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
154 static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
156 DeintVAAPIContext *ctx = avctx->priv;
158 VAProcFilterParameterBufferDeinterlacing params;
161 ctx->nb_deint_caps = VAProcDeinterlacingCount;
162 vas = vaQueryVideoProcFilterCaps(ctx->hwctx->display,
164 VAProcFilterDeinterlacing,
166 &ctx->nb_deint_caps);
167 if (vas != VA_STATUS_SUCCESS) {
168 av_log(avctx, AV_LOG_ERROR, "Failed to query deinterlacing "
169 "caps: %d (%s).\n", vas, vaErrorStr(vas));
173 if (ctx->mode == VAProcDeinterlacingNone) {
174 for (i = 0; i < ctx->nb_deint_caps; i++) {
175 if (ctx->deint_caps[i].type > ctx->mode)
176 ctx->mode = ctx->deint_caps[i].type;
178 av_log(avctx, AV_LOG_VERBOSE, "Picking %d (%s) as default "
179 "deinterlacing mode.\n", ctx->mode,
180 deint_vaapi_mode_name(ctx->mode));
182 for (i = 0; i < ctx->nb_deint_caps; i++) {
183 if (ctx->deint_caps[i].type == ctx->mode)
186 if (i >= ctx->nb_deint_caps) {
187 av_log(avctx, AV_LOG_ERROR, "Deinterlacing mode %d (%s) is "
188 "not supported.\n", ctx->mode,
189 deint_vaapi_mode_name(ctx->mode));
193 params.type = VAProcFilterDeinterlacing;
194 params.algorithm = ctx->mode;
197 av_assert0(ctx->filter_buffer == VA_INVALID_ID);
198 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
199 VAProcFilterParameterBufferType,
200 sizeof(params), 1, ¶ms,
201 &ctx->filter_buffer);
202 if (vas != VA_STATUS_SUCCESS) {
203 av_log(avctx, AV_LOG_ERROR, "Failed to create deinterlace "
204 "parameter buffer: %d (%s).\n", vas, vaErrorStr(vas));
208 vas = vaQueryVideoProcPipelineCaps(ctx->hwctx->display,
210 &ctx->filter_buffer, 1,
211 &ctx->pipeline_caps);
212 if (vas != VA_STATUS_SUCCESS) {
213 av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
214 "caps: %d (%s).\n", vas, vaErrorStr(vas));
218 ctx->extra_delay_for_timestamps = ctx->field_rate == 2 &&
219 ctx->pipeline_caps.num_backward_references == 0;
221 ctx->queue_depth = ctx->pipeline_caps.num_backward_references +
222 ctx->pipeline_caps.num_forward_references +
223 ctx->extra_delay_for_timestamps + 1;
224 if (ctx->queue_depth > MAX_REFERENCES) {
225 av_log(avctx, AV_LOG_ERROR, "Pipeline requires too many "
226 "references (%u forward, %u back).\n",
227 ctx->pipeline_caps.num_forward_references,
228 ctx->pipeline_caps.num_backward_references);
229 return AVERROR(ENOSYS);
235 static int deint_vaapi_config_output(AVFilterLink *outlink)
237 AVFilterContext *avctx = outlink->src;
238 AVFilterLink *inlink = avctx->inputs[0];
239 DeintVAAPIContext *ctx = avctx->priv;
240 AVVAAPIHWConfig *hwconfig = NULL;
241 AVHWFramesConstraints *constraints = NULL;
242 AVVAAPIFramesContext *va_frames;
246 deint_vaapi_pipeline_uninit(avctx);
248 av_assert0(ctx->input_frames);
249 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
250 ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
252 ctx->output_width = ctx->input_frames->width;
253 ctx->output_height = ctx->input_frames->height;
255 av_assert0(ctx->va_config == VA_INVALID_ID);
256 vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
257 VAEntrypointVideoProc, 0, 0, &ctx->va_config);
258 if (vas != VA_STATUS_SUCCESS) {
259 av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
260 "config: %d (%s).\n", vas, vaErrorStr(vas));
265 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
267 err = AVERROR(ENOMEM);
270 hwconfig->config_id = ctx->va_config;
272 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
275 err = AVERROR(ENOMEM);
279 if (ctx->output_width < constraints->min_width ||
280 ctx->output_height < constraints->min_height ||
281 ctx->output_width > constraints->max_width ||
282 ctx->output_height > constraints->max_height) {
283 av_log(avctx, AV_LOG_ERROR, "Hardware does not support "
284 "deinterlacing to size %dx%d "
285 "(constraints: width %d-%d height %d-%d).\n",
286 ctx->output_width, ctx->output_height,
287 constraints->min_width, constraints->max_width,
288 constraints->min_height, constraints->max_height);
289 err = AVERROR(EINVAL);
293 ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
294 if (!ctx->output_frames_ref) {
295 av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
297 err = AVERROR(ENOMEM);
301 ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
303 ctx->output_frames->format = AV_PIX_FMT_VAAPI;
304 ctx->output_frames->sw_format = ctx->input_frames->sw_format;
305 ctx->output_frames->width = ctx->output_width;
306 ctx->output_frames->height = ctx->output_height;
308 // The number of output frames we need is determined by what follows
309 // the filter. If it's an encoder with complex frame reference
310 // structures then this could be very high.
311 ctx->output_frames->initial_pool_size = 10;
313 err = av_hwframe_ctx_init(ctx->output_frames_ref);
315 av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
316 "context for output: %d\n", err);
320 va_frames = ctx->output_frames->hwctx;
322 av_assert0(ctx->va_context == VA_INVALID_ID);
323 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
324 ctx->output_width, ctx->output_height, 0,
325 va_frames->surface_ids, va_frames->nb_surfaces,
327 if (vas != VA_STATUS_SUCCESS) {
328 av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
329 "context: %d (%s).\n", vas, vaErrorStr(vas));
334 err = deint_vaapi_build_filter_params(avctx);
338 outlink->w = inlink->w;
339 outlink->h = inlink->h;
341 outlink->time_base = av_mul_q(inlink->time_base,
342 (AVRational) { 1, ctx->field_rate });
343 outlink->frame_rate = av_mul_q(inlink->frame_rate,
344 (AVRational) { ctx->field_rate, 1 });
346 outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
347 if (!outlink->hw_frames_ctx) {
348 err = AVERROR(ENOMEM);
353 av_hwframe_constraints_free(&constraints);
357 av_buffer_unref(&ctx->output_frames_ref);
359 av_hwframe_constraints_free(&constraints);
363 static int vaapi_proc_colour_standard(enum AVColorSpace av_cs)
366 #define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
368 CS(BT470BG, BT470BG);
369 CS(SMPTE170M, SMPTE170M);
370 CS(SMPTE240M, SMPTE240M);
373 return VAProcColorStandardNone;
377 static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
379 AVFilterContext *avctx = inlink->dst;
380 AVFilterLink *outlink = avctx->outputs[0];
381 DeintVAAPIContext *ctx = avctx->priv;
382 AVFrame *output_frame = NULL;
383 VASurfaceID input_surface, output_surface;
384 VASurfaceID backward_references[MAX_REFERENCES];
385 VASurfaceID forward_references[MAX_REFERENCES];
386 VAProcPipelineParameterBuffer params;
387 VAProcFilterParameterBufferDeinterlacing *filter_params;
388 VARectangle input_region;
389 VABufferID params_id;
391 void *filter_params_addr = NULL;
392 int err, i, field, current_frame_index;
394 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
395 av_get_pix_fmt_name(input_frame->format),
396 input_frame->width, input_frame->height, input_frame->pts);
398 if (ctx->queue_count < ctx->queue_depth) {
399 ctx->frame_queue[ctx->queue_count++] = input_frame;
400 if (ctx->queue_count < ctx->queue_depth) {
401 // Need more reference surfaces before we can continue.
405 av_frame_free(&ctx->frame_queue[0]);
406 for (i = 0; i + 1 < ctx->queue_count; i++)
407 ctx->frame_queue[i] = ctx->frame_queue[i + 1];
408 ctx->frame_queue[i] = input_frame;
411 current_frame_index = ctx->pipeline_caps.num_forward_references;
413 input_frame = ctx->frame_queue[current_frame_index];
414 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
415 for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
416 forward_references[i] = (VASurfaceID)(uintptr_t)
417 ctx->frame_queue[current_frame_index - i - 1]->data[3];
418 for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
419 backward_references[i] = (VASurfaceID)(uintptr_t)
420 ctx->frame_queue[current_frame_index + i + 1]->data[3];
422 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
423 "deinterlace input.\n", input_surface);
424 av_log(avctx, AV_LOG_DEBUG, "Backward references:");
425 for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
426 av_log(avctx, AV_LOG_DEBUG, " %#x", backward_references[i]);
427 av_log(avctx, AV_LOG_DEBUG, "\n");
428 av_log(avctx, AV_LOG_DEBUG, "Forward references:");
429 for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
430 av_log(avctx, AV_LOG_DEBUG, " %#x", forward_references[i]);
431 av_log(avctx, AV_LOG_DEBUG, "\n");
433 for (field = 0; field < ctx->field_rate; field++) {
434 output_frame = ff_get_video_buffer(outlink, ctx->output_width,
437 err = AVERROR(ENOMEM);
441 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
442 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
443 "deinterlace output.\n", output_surface);
445 memset(¶ms, 0, sizeof(params));
447 input_region = (VARectangle) {
450 .width = input_frame->width,
451 .height = input_frame->height,
454 params.surface = input_surface;
455 params.surface_region = &input_region;
456 params.surface_color_standard =
457 vaapi_proc_colour_standard(input_frame->colorspace);
459 params.output_region = NULL;
460 params.output_background_color = 0xff000000;
461 params.output_color_standard = params.surface_color_standard;
463 params.pipeline_flags = 0;
464 params.filter_flags = VA_FRAME_PICTURE;
466 if (!ctx->auto_enable || input_frame->interlaced_frame) {
467 vas = vaMapBuffer(ctx->hwctx->display, ctx->filter_buffer,
468 &filter_params_addr);
469 if (vas != VA_STATUS_SUCCESS) {
470 av_log(avctx, AV_LOG_ERROR, "Failed to map filter parameter "
471 "buffer: %d (%s).\n", vas, vaErrorStr(vas));
475 filter_params = filter_params_addr;
476 filter_params->flags = 0;
477 if (input_frame->top_field_first) {
478 filter_params->flags |= field ? VA_DEINTERLACING_BOTTOM_FIELD : 0;
480 filter_params->flags |= VA_DEINTERLACING_BOTTOM_FIELD_FIRST;
481 filter_params->flags |= field ? 0 : VA_DEINTERLACING_BOTTOM_FIELD;
483 filter_params_addr = NULL;
484 vas = vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
485 if (vas != VA_STATUS_SUCCESS)
486 av_log(avctx, AV_LOG_ERROR, "Failed to unmap filter parameter "
487 "buffer: %d (%s).\n", vas, vaErrorStr(vas));
489 params.filters = &ctx->filter_buffer;
490 params.num_filters = 1;
492 params.forward_references = forward_references;
493 params.num_forward_references =
494 ctx->pipeline_caps.num_forward_references;
495 params.backward_references = backward_references;
496 params.num_backward_references =
497 ctx->pipeline_caps.num_backward_references;
500 params.filters = NULL;
501 params.num_filters = 0;
504 vas = vaBeginPicture(ctx->hwctx->display,
505 ctx->va_context, output_surface);
506 if (vas != VA_STATUS_SUCCESS) {
507 av_log(avctx, AV_LOG_ERROR, "Failed to attach new picture: "
508 "%d (%s).\n", vas, vaErrorStr(vas));
513 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
514 VAProcPipelineParameterBufferType,
515 sizeof(params), 1, ¶ms, ¶ms_id);
516 if (vas != VA_STATUS_SUCCESS) {
517 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
518 "%d (%s).\n", vas, vaErrorStr(vas));
520 goto fail_after_begin;
522 av_log(avctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
525 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
527 if (vas != VA_STATUS_SUCCESS) {
528 av_log(avctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
529 "%d (%s).\n", vas, vaErrorStr(vas));
531 goto fail_after_begin;
534 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
535 if (vas != VA_STATUS_SUCCESS) {
536 av_log(avctx, AV_LOG_ERROR, "Failed to start picture processing: "
537 "%d (%s).\n", vas, vaErrorStr(vas));
539 goto fail_after_render;
542 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
543 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
544 vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
545 if (vas != VA_STATUS_SUCCESS) {
546 av_log(avctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
547 "%d (%s).\n", vas, vaErrorStr(vas));
552 err = av_frame_copy_props(output_frame, input_frame);
556 if (ctx->field_rate == 2) {
558 output_frame->pts = 2 * input_frame->pts;
560 output_frame->pts = input_frame->pts +
561 ctx->frame_queue[current_frame_index + 1]->pts;
563 output_frame->interlaced_frame = 0;
565 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
566 av_get_pix_fmt_name(output_frame->format),
567 output_frame->width, output_frame->height, output_frame->pts);
569 err = ff_filter_frame(outlink, output_frame);
577 vaRenderPicture(ctx->hwctx->display, ctx->va_context, ¶ms_id, 1);
579 vaEndPicture(ctx->hwctx->display, ctx->va_context);
581 if (filter_params_addr)
582 vaUnmapBuffer(ctx->hwctx->display, ctx->filter_buffer);
583 av_frame_free(&output_frame);
587 static av_cold int deint_vaapi_init(AVFilterContext *avctx)
589 DeintVAAPIContext *ctx = avctx->priv;
591 ctx->va_config = VA_INVALID_ID;
592 ctx->va_context = VA_INVALID_ID;
593 ctx->filter_buffer = VA_INVALID_ID;
599 static av_cold void deint_vaapi_uninit(AVFilterContext *avctx)
601 DeintVAAPIContext *ctx = avctx->priv;
604 deint_vaapi_pipeline_uninit(avctx);
606 av_buffer_unref(&ctx->input_frames_ref);
607 av_buffer_unref(&ctx->output_frames_ref);
608 av_buffer_unref(&ctx->device_ref);
611 #define OFFSET(x) offsetof(DeintVAAPIContext, x)
612 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
613 static const AVOption deint_vaapi_options[] = {
614 { "mode", "Deinterlacing mode",
615 OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VAProcDeinterlacingNone },
616 VAProcDeinterlacingNone, VAProcDeinterlacingCount - 1, FLAGS, "mode" },
617 { "default", "Use the highest-numbered (and therefore possibly most advanced) deinterlacing algorithm",
618 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingNone }, .unit = "mode" },
619 { "bob", "Use the bob deinterlacing algorithm",
620 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingBob }, .unit = "mode" },
621 { "weave", "Use the weave deinterlacing algorithm",
622 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingWeave }, .unit = "mode" },
623 { "motion_adaptive", "Use the motion adaptive deinterlacing algorithm",
624 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingMotionAdaptive }, .unit = "mode" },
625 { "motion_compensated", "Use the motion compensated deinterlacing algorithm",
626 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingMotionCompensated }, .unit = "mode" },
628 { "rate", "Generate output at frame rate or field rate",
629 OFFSET(field_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 2, FLAGS, "rate" },
630 { "frame", "Output at frame rate (one frame of output for each field-pair)",
631 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "rate" },
632 { "field", "Output at field rate (one frame of output for each field)",
633 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, .unit = "rate" },
635 { "auto", "Only deinterlace fields, passing frames through unchanged",
636 OFFSET(auto_enable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
641 static const AVClass deint_vaapi_class = {
642 .class_name = "deinterlace_vaapi",
643 .item_name = av_default_item_name,
644 .option = deint_vaapi_options,
645 .version = LIBAVUTIL_VERSION_INT,
648 static const AVFilterPad deint_vaapi_inputs[] = {
651 .type = AVMEDIA_TYPE_VIDEO,
652 .filter_frame = &deint_vaapi_filter_frame,
653 .config_props = &deint_vaapi_config_input,
658 static const AVFilterPad deint_vaapi_outputs[] = {
661 .type = AVMEDIA_TYPE_VIDEO,
662 .config_props = &deint_vaapi_config_output,
667 AVFilter ff_vf_deinterlace_vaapi = {
668 .name = "deinterlace_vaapi",
669 .description = NULL_IF_CONFIG_SMALL("Deinterlacing of VAAPI surfaces"),
670 .priv_size = sizeof(DeintVAAPIContext),
671 .init = &deint_vaapi_init,
672 .uninit = &deint_vaapi_uninit,
673 .query_formats = &deint_vaapi_query_formats,
674 .inputs = deint_vaapi_inputs,
675 .outputs = deint_vaapi_outputs,
676 .priv_class = &deint_vaapi_class,
677 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,