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
21 #include "libavutil/avassert.h"
22 #include "libavutil/common.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
31 #include "vaapi_vpp.h"
33 #define MAX_REFERENCES 8
35 typedef struct DeintVAAPIContext {
36 VAAPIVPPContext vpp_ctx; // must be the first field
42 VAProcFilterCapDeinterlacing
43 deint_caps[VAProcDeinterlacingCount];
45 VAProcPipelineCaps pipeline_caps;
49 AVFrame *frame_queue[MAX_REFERENCES];
50 int extra_delay_for_timestamps;
53 static const char *deint_vaapi_mode_name(int mode)
56 #define D(name) case VAProcDeinterlacing ## name: return #name
67 static void deint_vaapi_pipeline_uninit(AVFilterContext *avctx)
69 DeintVAAPIContext *ctx = avctx->priv;
72 for (i = 0; i < ctx->queue_count; i++)
73 av_frame_free(&ctx->frame_queue[i]);
76 ff_vaapi_vpp_pipeline_uninit(avctx);
79 static int deint_vaapi_build_filter_params(AVFilterContext *avctx)
81 VAAPIVPPContext *vpp_ctx = avctx->priv;
82 DeintVAAPIContext *ctx = avctx->priv;
84 VAProcFilterParameterBufferDeinterlacing params;
87 ctx->nb_deint_caps = VAProcDeinterlacingCount;
88 vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display,
90 VAProcFilterDeinterlacing,
93 if (vas != VA_STATUS_SUCCESS) {
94 av_log(avctx, AV_LOG_ERROR, "Failed to query deinterlacing "
95 "caps: %d (%s).\n", vas, vaErrorStr(vas));
99 if (ctx->mode == VAProcDeinterlacingNone) {
100 for (i = 0; i < ctx->nb_deint_caps; i++) {
101 if (ctx->deint_caps[i].type > ctx->mode)
102 ctx->mode = ctx->deint_caps[i].type;
104 av_log(avctx, AV_LOG_VERBOSE, "Picking %d (%s) as default "
105 "deinterlacing mode.\n", ctx->mode,
106 deint_vaapi_mode_name(ctx->mode));
108 for (i = 0; i < ctx->nb_deint_caps; i++) {
109 if (ctx->deint_caps[i].type == ctx->mode)
112 if (i >= ctx->nb_deint_caps) {
113 av_log(avctx, AV_LOG_ERROR, "Deinterlacing mode %d (%s) is "
114 "not supported.\n", ctx->mode,
115 deint_vaapi_mode_name(ctx->mode));
116 return AVERROR(EINVAL);
120 params.type = VAProcFilterDeinterlacing;
121 params.algorithm = ctx->mode;
124 vas = ff_vaapi_vpp_make_param_buffers(avctx,
125 VAProcFilterParameterBufferType,
132 vas = vaQueryVideoProcPipelineCaps(vpp_ctx->hwctx->display,
134 &vpp_ctx->filter_buffers[0], 1,
135 &ctx->pipeline_caps);
136 if (vas != VA_STATUS_SUCCESS) {
137 av_log(avctx, AV_LOG_ERROR, "Failed to query pipeline "
138 "caps: %d (%s).\n", vas, vaErrorStr(vas));
142 ctx->extra_delay_for_timestamps = ctx->field_rate == 2 &&
143 ctx->pipeline_caps.num_backward_references == 0;
145 ctx->queue_depth = ctx->pipeline_caps.num_backward_references +
146 ctx->pipeline_caps.num_forward_references +
147 ctx->extra_delay_for_timestamps + 1;
148 if (ctx->queue_depth > MAX_REFERENCES) {
149 av_log(avctx, AV_LOG_ERROR, "Pipeline requires too many "
150 "references (%u forward, %u back).\n",
151 ctx->pipeline_caps.num_forward_references,
152 ctx->pipeline_caps.num_backward_references);
153 return AVERROR(ENOSYS);
159 static int deint_vaapi_config_output(AVFilterLink *outlink)
161 AVFilterLink *inlink = outlink->src->inputs[0];
162 AVFilterContext *avctx = outlink->src;
163 DeintVAAPIContext *ctx = avctx->priv;
166 err = ff_vaapi_vpp_config_output(outlink);
169 outlink->time_base = av_mul_q(inlink->time_base,
170 (AVRational) { 1, ctx->field_rate });
171 outlink->frame_rate = av_mul_q(inlink->frame_rate,
172 (AVRational) { ctx->field_rate, 1 });
177 static int deint_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
179 AVFilterContext *avctx = inlink->dst;
180 AVFilterLink *outlink = avctx->outputs[0];
181 VAAPIVPPContext *vpp_ctx = avctx->priv;
182 DeintVAAPIContext *ctx = avctx->priv;
183 AVFrame *output_frame = NULL;
184 VASurfaceID input_surface, output_surface;
185 VASurfaceID backward_references[MAX_REFERENCES];
186 VASurfaceID forward_references[MAX_REFERENCES];
187 VAProcPipelineParameterBuffer params;
188 VAProcFilterParameterBufferDeinterlacing *filter_params;
189 VARectangle input_region;
191 void *filter_params_addr = NULL;
192 int err, i, field, current_frame_index;
194 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
195 av_get_pix_fmt_name(input_frame->format),
196 input_frame->width, input_frame->height, input_frame->pts);
198 if (ctx->queue_count < ctx->queue_depth) {
199 ctx->frame_queue[ctx->queue_count++] = input_frame;
200 if (ctx->queue_count < ctx->queue_depth) {
201 // Need more reference surfaces before we can continue.
205 av_frame_free(&ctx->frame_queue[0]);
206 for (i = 0; i + 1 < ctx->queue_count; i++)
207 ctx->frame_queue[i] = ctx->frame_queue[i + 1];
208 ctx->frame_queue[i] = input_frame;
211 current_frame_index = ctx->pipeline_caps.num_forward_references;
213 input_frame = ctx->frame_queue[current_frame_index];
214 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
215 for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
216 forward_references[i] = (VASurfaceID)(uintptr_t)
217 ctx->frame_queue[current_frame_index - i - 1]->data[3];
218 for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
219 backward_references[i] = (VASurfaceID)(uintptr_t)
220 ctx->frame_queue[current_frame_index + i + 1]->data[3];
222 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
223 "deinterlace input.\n", input_surface);
224 av_log(avctx, AV_LOG_DEBUG, "Backward references:");
225 for (i = 0; i < ctx->pipeline_caps.num_backward_references; i++)
226 av_log(avctx, AV_LOG_DEBUG, " %#x", backward_references[i]);
227 av_log(avctx, AV_LOG_DEBUG, "\n");
228 av_log(avctx, AV_LOG_DEBUG, "Forward references:");
229 for (i = 0; i < ctx->pipeline_caps.num_forward_references; i++)
230 av_log(avctx, AV_LOG_DEBUG, " %#x", forward_references[i]);
231 av_log(avctx, AV_LOG_DEBUG, "\n");
233 for (field = 0; field < ctx->field_rate; field++) {
234 output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
235 vpp_ctx->output_height);
237 err = AVERROR(ENOMEM);
241 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
242 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for "
243 "deinterlace output.\n", output_surface);
245 memset(¶ms, 0, sizeof(params));
247 input_region = (VARectangle) {
250 .width = input_frame->width,
251 .height = input_frame->height,
254 params.surface = input_surface;
255 params.surface_region = &input_region;
256 params.surface_color_standard =
257 ff_vaapi_vpp_colour_standard(input_frame->colorspace);
259 params.output_region = NULL;
260 params.output_background_color = VAAPI_VPP_BACKGROUND_BLACK;
261 params.output_color_standard = params.surface_color_standard;
263 params.pipeline_flags = 0;
264 params.filter_flags = VA_FRAME_PICTURE;
266 if (!ctx->auto_enable || input_frame->interlaced_frame) {
267 vas = vaMapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0],
268 &filter_params_addr);
269 if (vas != VA_STATUS_SUCCESS) {
270 av_log(avctx, AV_LOG_ERROR, "Failed to map filter parameter "
271 "buffer: %d (%s).\n", vas, vaErrorStr(vas));
275 filter_params = filter_params_addr;
276 filter_params->flags = 0;
277 if (input_frame->top_field_first) {
278 filter_params->flags |= field ? VA_DEINTERLACING_BOTTOM_FIELD : 0;
280 filter_params->flags |= VA_DEINTERLACING_BOTTOM_FIELD_FIRST;
281 filter_params->flags |= field ? 0 : VA_DEINTERLACING_BOTTOM_FIELD;
283 filter_params_addr = NULL;
284 vas = vaUnmapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0]);
285 if (vas != VA_STATUS_SUCCESS)
286 av_log(avctx, AV_LOG_ERROR, "Failed to unmap filter parameter "
287 "buffer: %d (%s).\n", vas, vaErrorStr(vas));
289 params.filters = &vpp_ctx->filter_buffers[0];
290 params.num_filters = 1;
292 params.forward_references = forward_references;
293 params.num_forward_references =
294 ctx->pipeline_caps.num_forward_references;
295 params.backward_references = backward_references;
296 params.num_backward_references =
297 ctx->pipeline_caps.num_backward_references;
300 params.filters = NULL;
301 params.num_filters = 0;
304 err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_surface);
308 err = av_frame_copy_props(output_frame, input_frame);
312 if (ctx->field_rate == 2) {
314 output_frame->pts = 2 * input_frame->pts;
316 output_frame->pts = input_frame->pts +
317 ctx->frame_queue[current_frame_index + 1]->pts;
319 output_frame->interlaced_frame = 0;
321 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
322 av_get_pix_fmt_name(output_frame->format),
323 output_frame->width, output_frame->height, output_frame->pts);
325 err = ff_filter_frame(outlink, output_frame);
333 if (filter_params_addr)
334 vaUnmapBuffer(vpp_ctx->hwctx->display, vpp_ctx->filter_buffers[0]);
335 av_frame_free(&output_frame);
339 static av_cold int deint_vaapi_init(AVFilterContext *avctx)
341 VAAPIVPPContext *vpp_ctx = avctx->priv;
343 ff_vaapi_vpp_ctx_init(avctx);
344 vpp_ctx->pipeline_uninit = deint_vaapi_pipeline_uninit;
345 vpp_ctx->build_filter_params = deint_vaapi_build_filter_params;
346 vpp_ctx->output_format = AV_PIX_FMT_NONE;
351 #define OFFSET(x) offsetof(DeintVAAPIContext, x)
352 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
353 static const AVOption deint_vaapi_options[] = {
354 { "mode", "Deinterlacing mode",
355 OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VAProcDeinterlacingNone },
356 VAProcDeinterlacingNone, VAProcDeinterlacingCount - 1, FLAGS, "mode" },
357 { "default", "Use the highest-numbered (and therefore possibly most advanced) deinterlacing algorithm",
358 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingNone }, 0, 0, FLAGS, "mode" },
359 { "bob", "Use the bob deinterlacing algorithm",
360 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingBob }, 0, 0, FLAGS, "mode" },
361 { "weave", "Use the weave deinterlacing algorithm",
362 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingWeave }, 0, 0, FLAGS, "mode" },
363 { "motion_adaptive", "Use the motion adaptive deinterlacing algorithm",
364 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingMotionAdaptive }, 0, 0, FLAGS, "mode" },
365 { "motion_compensated", "Use the motion compensated deinterlacing algorithm",
366 0, AV_OPT_TYPE_CONST, { .i64 = VAProcDeinterlacingMotionCompensated }, 0, 0, FLAGS, "mode" },
368 { "rate", "Generate output at frame rate or field rate",
369 OFFSET(field_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 2, FLAGS, "rate" },
370 { "frame", "Output at frame rate (one frame of output for each field-pair)",
371 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "rate" },
372 { "field", "Output at field rate (one frame of output for each field)",
373 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "rate" },
375 { "auto", "Only deinterlace fields, passing frames through unchanged",
376 OFFSET(auto_enable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
381 static const AVClass deint_vaapi_class = {
382 .class_name = "deinterlace_vaapi",
383 .item_name = av_default_item_name,
384 .option = deint_vaapi_options,
385 .version = LIBAVUTIL_VERSION_INT,
388 static const AVFilterPad deint_vaapi_inputs[] = {
391 .type = AVMEDIA_TYPE_VIDEO,
392 .filter_frame = &deint_vaapi_filter_frame,
393 .config_props = &ff_vaapi_vpp_config_input,
398 static const AVFilterPad deint_vaapi_outputs[] = {
401 .type = AVMEDIA_TYPE_VIDEO,
402 .config_props = &deint_vaapi_config_output,
407 AVFilter ff_vf_deinterlace_vaapi = {
408 .name = "deinterlace_vaapi",
409 .description = NULL_IF_CONFIG_SMALL("Deinterlacing of VAAPI surfaces"),
410 .priv_size = sizeof(DeintVAAPIContext),
411 .init = &deint_vaapi_init,
412 .uninit = &ff_vaapi_vpp_ctx_uninit,
413 .query_formats = &ff_vaapi_vpp_query_formats,
414 .inputs = deint_vaapi_inputs,
415 .outputs = deint_vaapi_outputs,
416 .priv_class = &deint_vaapi_class,
417 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,