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/pixdesc.h"
25 #include "vaapi_vpp.h"
27 int ff_vaapi_vpp_query_formats(AVFilterContext *avctx)
29 enum AVPixelFormat pix_fmts[] = {
30 AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
34 if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
35 &avctx->inputs[0]->out_formats)) < 0)
37 if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
38 &avctx->outputs[0]->in_formats)) < 0)
44 void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
46 VAAPIVPPContext *ctx = avctx->priv;
48 for (i = 0; i < ctx->nb_filter_buffers; i++) {
49 if (ctx->filter_buffers[i] != VA_INVALID_ID) {
50 vaDestroyBuffer(ctx->hwctx->display, ctx->filter_buffers[i]);
51 ctx->filter_buffers[i] = VA_INVALID_ID;
54 ctx->nb_filter_buffers = 0;
56 if (ctx->va_context != VA_INVALID_ID) {
57 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
58 ctx->va_context = VA_INVALID_ID;
61 if (ctx->va_config != VA_INVALID_ID) {
62 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
63 ctx->va_config = VA_INVALID_ID;
66 av_buffer_unref(&ctx->device_ref);
70 int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
72 AVFilterContext *avctx = inlink->dst;
73 VAAPIVPPContext *ctx = avctx->priv;
75 if (ctx->pipeline_uninit)
76 ctx->pipeline_uninit(avctx);
78 if (!inlink->hw_frames_ctx) {
79 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
80 "required to associate the processing device.\n");
81 return AVERROR(EINVAL);
84 ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
85 if (!ctx->input_frames_ref) {
86 av_log(avctx, AV_LOG_ERROR, "A input frames reference create "
88 return AVERROR(ENOMEM);
90 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
95 int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
97 AVFilterContext *avctx = outlink->src;
98 VAAPIVPPContext *ctx = avctx->priv;
99 AVVAAPIHWConfig *hwconfig = NULL;
100 AVHWFramesConstraints *constraints = NULL;
101 AVHWFramesContext *output_frames;
102 AVVAAPIFramesContext *va_frames;
106 if (ctx->pipeline_uninit)
107 ctx->pipeline_uninit(avctx);
109 if (!ctx->output_width)
110 ctx->output_width = avctx->inputs[0]->w;
111 if (!ctx->output_height)
112 ctx->output_height = avctx->inputs[0]->h;
114 av_assert0(ctx->input_frames);
115 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
116 if (!ctx->device_ref) {
117 av_log(avctx, AV_LOG_ERROR, "A device reference create "
119 return AVERROR(ENOMEM);
121 ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
123 av_assert0(ctx->va_config == VA_INVALID_ID);
124 vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
125 VAEntrypointVideoProc, NULL, 0, &ctx->va_config);
126 if (vas != VA_STATUS_SUCCESS) {
127 av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
128 "config: %d (%s).\n", vas, vaErrorStr(vas));
133 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
135 err = AVERROR(ENOMEM);
138 hwconfig->config_id = ctx->va_config;
140 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
143 err = AVERROR(ENOMEM);
147 if (ctx->output_format == AV_PIX_FMT_NONE)
148 ctx->output_format = ctx->input_frames->sw_format;
149 if (constraints->valid_sw_formats) {
150 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
151 if (ctx->output_format == constraints->valid_sw_formats[i])
154 if (constraints->valid_sw_formats[i] == AV_PIX_FMT_NONE) {
155 av_log(avctx, AV_LOG_ERROR, "Hardware does not support output "
156 "format %s.\n", av_get_pix_fmt_name(ctx->output_format));
157 err = AVERROR(EINVAL);
162 if (ctx->output_width < constraints->min_width ||
163 ctx->output_height < constraints->min_height ||
164 ctx->output_width > constraints->max_width ||
165 ctx->output_height > constraints->max_height) {
166 av_log(avctx, AV_LOG_ERROR, "Hardware does not support scaling to "
167 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
168 ctx->output_width, ctx->output_height,
169 constraints->min_width, constraints->max_width,
170 constraints->min_height, constraints->max_height);
171 err = AVERROR(EINVAL);
175 outlink->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->device_ref);
176 if (!outlink->hw_frames_ctx) {
177 av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
179 err = AVERROR(ENOMEM);
183 output_frames = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
185 output_frames->format = AV_PIX_FMT_VAAPI;
186 output_frames->sw_format = ctx->output_format;
187 output_frames->width = ctx->output_width;
188 output_frames->height = ctx->output_height;
190 output_frames->initial_pool_size = 4;
192 err = ff_filter_init_hw_frames(avctx, outlink, 10);
196 err = av_hwframe_ctx_init(outlink->hw_frames_ctx);
198 av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
199 "context for output: %d\n", err);
203 va_frames = output_frames->hwctx;
205 av_assert0(ctx->va_context == VA_INVALID_ID);
206 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
207 ctx->output_width, ctx->output_height,
209 va_frames->surface_ids, va_frames->nb_surfaces,
211 if (vas != VA_STATUS_SUCCESS) {
212 av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
213 "context: %d (%s).\n", vas, vaErrorStr(vas));
217 outlink->w = ctx->output_width;
218 outlink->h = ctx->output_height;
220 if (ctx->build_filter_params) {
221 err = ctx->build_filter_params(avctx);
227 av_hwframe_constraints_free(&constraints);
231 av_buffer_unref(&outlink->hw_frames_ctx);
233 av_hwframe_constraints_free(&constraints);
237 int ff_vaapi_vpp_colour_standard(enum AVColorSpace av_cs)
240 #define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
243 CS(SMPTE170M, SMPTE170M);
244 CS(SMPTE240M, SMPTE240M);
247 return VAProcColorStandardNone;
251 int ff_vaapi_vpp_init_params(AVFilterContext *avctx,
252 VAProcPipelineParameterBuffer *params,
253 const AVFrame *input_frame,
254 AVFrame *output_frame)
256 VAAPIVPPContext *ctx = avctx->priv;
257 VASurfaceID input_surface;
259 ctx->input_region = (VARectangle) {
260 .x = input_frame->crop_left,
261 .y = input_frame->crop_top,
262 .width = input_frame->width -
263 (input_frame->crop_left + input_frame->crop_right),
264 .height = input_frame->height -
265 (input_frame->crop_top + input_frame->crop_bottom),
267 output_frame->crop_top = 0;
268 output_frame->crop_bottom = 0;
269 output_frame->crop_left = 0;
270 output_frame->crop_right = 0;
272 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3],
274 *params = (VAProcPipelineParameterBuffer) {
275 .surface = input_surface,
276 .surface_region = &ctx->input_region,
277 .surface_color_standard =
278 ff_vaapi_vpp_colour_standard(input_frame->colorspace),
279 .output_region = NULL,
280 .output_background_color = VAAPI_VPP_BACKGROUND_BLACK,
281 .output_color_standard =
282 ff_vaapi_vpp_colour_standard(input_frame->colorspace),
284 .filter_flags = VA_FRAME_PICTURE,
286 // Filter and reference data filled by the filter itself.
288 #if VA_CHECK_VERSION(1, 1, 0)
289 .rotation_state = VA_ROTATION_NONE,
290 .mirror_state = VA_MIRROR_NONE,
297 int ff_vaapi_vpp_make_param_buffers(AVFilterContext *avctx,
305 VAAPIVPPContext *ctx = avctx->priv;
307 av_assert0(ctx->nb_filter_buffers + 1 <= VAProcFilterCount);
309 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
310 type, size, count, (void*)data, &buffer);
311 if (vas != VA_STATUS_SUCCESS) {
312 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
313 "buffer (type %d): %d (%s).\n",
314 type, vas, vaErrorStr(vas));
318 ctx->filter_buffers[ctx->nb_filter_buffers++] = buffer;
320 av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes, count %d) "
321 "is %#x.\n", type, size, count, buffer);
326 int ff_vaapi_vpp_render_picture(AVFilterContext *avctx,
327 VAProcPipelineParameterBuffer *params,
328 AVFrame *output_frame)
330 VAAPIVPPContext *ctx = avctx->priv;
331 VASurfaceID output_surface;
332 VABufferID params_id;
336 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
338 vas = vaBeginPicture(ctx->hwctx->display,
339 ctx->va_context, output_surface);
340 if (vas != VA_STATUS_SUCCESS) {
341 av_log(avctx, AV_LOG_ERROR, "Failed to attach new picture: "
342 "%d (%s).\n", vas, vaErrorStr(vas));
347 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
348 VAProcPipelineParameterBufferType,
349 sizeof(*params), 1, params, ¶ms_id);
350 if (vas != VA_STATUS_SUCCESS) {
351 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
352 "%d (%s).\n", vas, vaErrorStr(vas));
354 goto fail_after_begin;
356 av_log(avctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
359 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
361 if (vas != VA_STATUS_SUCCESS) {
362 av_log(avctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
363 "%d (%s).\n", vas, vaErrorStr(vas));
365 goto fail_after_begin;
368 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
369 if (vas != VA_STATUS_SUCCESS) {
370 av_log(avctx, AV_LOG_ERROR, "Failed to start picture processing: "
371 "%d (%s).\n", vas, vaErrorStr(vas));
373 goto fail_after_render;
376 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
377 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
378 vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
379 if (vas != VA_STATUS_SUCCESS) {
380 av_log(avctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
381 "%d (%s).\n", vas, vaErrorStr(vas));
388 // We want to make sure that if vaBeginPicture has been called, we also
389 // call vaRenderPicture and vaEndPicture. These calls may well fail or
390 // do something else nasty, but once we're in this failure case there
391 // isn't much else we can do.
393 vaRenderPicture(ctx->hwctx->display, ctx->va_context, ¶ms_id, 1);
395 vaEndPicture(ctx->hwctx->display, ctx->va_context);
400 void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
403 VAAPIVPPContext *ctx = avctx->priv;
405 ctx->va_config = VA_INVALID_ID;
406 ctx->va_context = VA_INVALID_ID;
409 for (i = 0; i < VAProcFilterCount; i++)
410 ctx->filter_buffers[i] = VA_INVALID_ID;
411 ctx->nb_filter_buffers = 0;
414 void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
416 VAAPIVPPContext *ctx = avctx->priv;
417 if (ctx->valid_ids && ctx->pipeline_uninit)
418 ctx->pipeline_uninit(avctx);
420 av_buffer_unref(&ctx->input_frames_ref);
421 av_buffer_unref(&ctx->device_ref);