FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vaapi_vpp.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 0 349 0.0%
Functions: 0 17 0.0%
Branches: 0 181 0.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include <string.h>
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/pixdesc.h"
24 #include "formats.h"
25 #include "internal.h"
26 #include "vaapi_vpp.h"
27
28 int ff_vaapi_vpp_query_formats(AVFilterContext *avctx)
29 {
30 enum AVPixelFormat pix_fmts[] = {
31 AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
32 };
33 int err;
34
35 if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
36 &avctx->inputs[0]->outcfg.formats)) < 0)
37 return err;
38 if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
39 &avctx->outputs[0]->incfg.formats)) < 0)
40 return err;
41
42 if ((err = ff_set_common_all_color_spaces(avctx)) < 0 ||
43 (err = ff_set_common_all_color_ranges(avctx)) < 0)
44 return err;
45
46 return 0;
47 }
48
49 void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
50 {
51 VAAPIVPPContext *ctx = avctx->priv;
52 int i;
53 for (i = 0; i < ctx->nb_filter_buffers; i++) {
54 if (ctx->filter_buffers[i] != VA_INVALID_ID) {
55 vaDestroyBuffer(ctx->hwctx->display, ctx->filter_buffers[i]);
56 ctx->filter_buffers[i] = VA_INVALID_ID;
57 }
58 }
59 ctx->nb_filter_buffers = 0;
60
61 if (ctx->va_context != VA_INVALID_ID) {
62 vaDestroyContext(ctx->hwctx->display, ctx->va_context);
63 ctx->va_context = VA_INVALID_ID;
64 }
65
66 if (ctx->va_config != VA_INVALID_ID) {
67 vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
68 ctx->va_config = VA_INVALID_ID;
69 }
70
71 av_buffer_unref(&ctx->device_ref);
72 ctx->hwctx = NULL;
73 }
74
75 int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
76 {
77 AVFilterContext *avctx = inlink->dst;
78 VAAPIVPPContext *ctx = avctx->priv;
79
80 if (ctx->pipeline_uninit)
81 ctx->pipeline_uninit(avctx);
82
83 if (!inlink->hw_frames_ctx) {
84 av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
85 "required to associate the processing device.\n");
86 return AVERROR(EINVAL);
87 }
88
89 ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
90 if (!ctx->input_frames_ref) {
91 av_log(avctx, AV_LOG_ERROR, "A input frames reference create "
92 "failed.\n");
93 return AVERROR(ENOMEM);
94 }
95 ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
96
97 return 0;
98 }
99
100 int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
101 {
102 AVFilterContext *avctx = outlink->src;
103 AVFilterLink *inlink = avctx->inputs[0];
104 VAAPIVPPContext *ctx = avctx->priv;
105 AVVAAPIHWConfig *hwconfig = NULL;
106 AVHWFramesConstraints *constraints = NULL;
107 AVHWFramesContext *output_frames;
108 AVVAAPIFramesContext *va_frames;
109 VAStatus vas;
110 int err, i;
111
112 if (ctx->pipeline_uninit)
113 ctx->pipeline_uninit(avctx);
114
115 if (!ctx->output_width)
116 ctx->output_width = avctx->inputs[0]->w;
117 if (!ctx->output_height)
118 ctx->output_height = avctx->inputs[0]->h;
119
120 outlink->w = ctx->output_width;
121 outlink->h = ctx->output_height;
122
123 if (ctx->passthrough) {
124 if (inlink->hw_frames_ctx)
125 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
126 av_log(ctx, AV_LOG_VERBOSE, "Using VAAPI filter passthrough mode.\n");
127
128 return 0;
129 }
130
131 av_assert0(ctx->input_frames);
132 ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
133 if (!ctx->device_ref) {
134 av_log(avctx, AV_LOG_ERROR, "A device reference create "
135 "failed.\n");
136 return AVERROR(ENOMEM);
137 }
138 ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
139
140 av_assert0(ctx->va_config == VA_INVALID_ID);
141 vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
142 VAEntrypointVideoProc, NULL, 0, &ctx->va_config);
143 if (vas != VA_STATUS_SUCCESS) {
144 av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
145 "config: %d (%s).\n", vas, vaErrorStr(vas));
146 err = AVERROR(EIO);
147 goto fail;
148 }
149
150 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
151 if (!hwconfig) {
152 err = AVERROR(ENOMEM);
153 goto fail;
154 }
155 hwconfig->config_id = ctx->va_config;
156
157 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
158 hwconfig);
159 if (!constraints) {
160 err = AVERROR(ENOMEM);
161 goto fail;
162 }
163
164 if (ctx->output_format == AV_PIX_FMT_NONE)
165 ctx->output_format = ctx->input_frames->sw_format;
166 if (constraints->valid_sw_formats) {
167 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
168 if (ctx->output_format == constraints->valid_sw_formats[i])
169 break;
170 }
171 if (constraints->valid_sw_formats[i] == AV_PIX_FMT_NONE) {
172 av_log(avctx, AV_LOG_ERROR, "Hardware does not support output "
173 "format %s.\n", av_get_pix_fmt_name(ctx->output_format));
174 err = AVERROR(EINVAL);
175 goto fail;
176 }
177 }
178
179 if (ctx->output_width < constraints->min_width ||
180 ctx->output_height < constraints->min_height ||
181 ctx->output_width > constraints->max_width ||
182 ctx->output_height > constraints->max_height) {
183 av_log(avctx, AV_LOG_ERROR, "Hardware does not support scaling to "
184 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
185 ctx->output_width, ctx->output_height,
186 constraints->min_width, constraints->max_width,
187 constraints->min_height, constraints->max_height);
188 err = AVERROR(EINVAL);
189 goto fail;
190 }
191
192 outlink->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->device_ref);
193 if (!outlink->hw_frames_ctx) {
194 av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
195 "for output.\n");
196 err = AVERROR(ENOMEM);
197 goto fail;
198 }
199
200 output_frames = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
201
202 output_frames->format = AV_PIX_FMT_VAAPI;
203 output_frames->sw_format = ctx->output_format;
204 output_frames->width = ctx->output_width;
205 output_frames->height = ctx->output_height;
206
207 if (CONFIG_VAAPI_1)
208 output_frames->initial_pool_size = 0;
209 else
210 output_frames->initial_pool_size = 4;
211
212 err = ff_filter_init_hw_frames(avctx, outlink, 10);
213 if (err < 0)
214 goto fail;
215
216 err = av_hwframe_ctx_init(outlink->hw_frames_ctx);
217 if (err < 0) {
218 av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
219 "context for output: %d\n", err);
220 goto fail;
221 }
222
223 va_frames = output_frames->hwctx;
224
225 av_assert0(ctx->va_context == VA_INVALID_ID);
226 av_assert0(output_frames->initial_pool_size ||
227 (va_frames->surface_ids == NULL && va_frames->nb_surfaces == 0));
228 vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
229 ctx->output_width, ctx->output_height,
230 VA_PROGRESSIVE,
231 va_frames->surface_ids, va_frames->nb_surfaces,
232 &ctx->va_context);
233 if (vas != VA_STATUS_SUCCESS) {
234 av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
235 "context: %d (%s).\n", vas, vaErrorStr(vas));
236 return AVERROR(EIO);
237 }
238
239 if (ctx->build_filter_params) {
240 err = ctx->build_filter_params(avctx);
241 if (err < 0)
242 goto fail;
243 }
244
245 av_freep(&hwconfig);
246 av_hwframe_constraints_free(&constraints);
247 return 0;
248
249 fail:
250 av_buffer_unref(&outlink->hw_frames_ctx);
251 av_freep(&hwconfig);
252 av_hwframe_constraints_free(&constraints);
253 return err;
254 }
255
256 typedef struct VAAPIColourProperties {
257 VAProcColorStandardType va_color_standard;
258
259 enum AVColorPrimaries color_primaries;
260 enum AVColorTransferCharacteristic color_trc;
261 enum AVColorSpace colorspace;
262
263 uint8_t va_chroma_sample_location;
264 uint8_t va_color_range;
265
266 enum AVColorRange color_range;
267 enum AVChromaLocation chroma_sample_location;
268 } VAAPIColourProperties;
269
270 static const VAAPIColourProperties vaapi_colour_standard_map[] = {
271 { VAProcColorStandardBT601, 5, 6, 5 },
272 { VAProcColorStandardBT601, 6, 6, 6 },
273 { VAProcColorStandardBT709, 1, 1, 1 },
274 { VAProcColorStandardBT470M, 4, 4, 4 },
275 { VAProcColorStandardBT470BG, 5, 5, 5 },
276 { VAProcColorStandardSMPTE170M, 6, 6, 6 },
277 { VAProcColorStandardSMPTE240M, 7, 7, 7 },
278 { VAProcColorStandardGenericFilm, 8, 1, 1 },
279 #if VA_CHECK_VERSION(1, 1, 0)
280 { VAProcColorStandardSRGB, 1, 13, 0 },
281 { VAProcColorStandardXVYCC601, 1, 11, 5 },
282 { VAProcColorStandardXVYCC709, 1, 11, 1 },
283 { VAProcColorStandardBT2020, 9, 14, 9 },
284 #endif
285 };
286
287 static void vaapi_vpp_fill_colour_standard(VAAPIColourProperties *props,
288 VAProcColorStandardType *vacs,
289 int nb_vacs)
290 {
291 const VAAPIColourProperties *t;
292 int i, j, score, best_score, worst_score;
293 VAProcColorStandardType best_standard;
294
295 #if VA_CHECK_VERSION(1, 3, 0)
296 // If the driver supports explicit use of the standard values then just
297 // use them and avoid doing any mapping. (The driver may not support
298 // some particular code point, but it still has enough information to
299 // make a better fallback choice than we do in that case.)
300 for (i = 0; i < nb_vacs; i++) {
301 if (vacs[i] == VAProcColorStandardExplicit) {
302 props->va_color_standard = VAProcColorStandardExplicit;
303 return;
304 }
305 }
306 #endif
307
308 // Give scores to the possible options and choose the lowest one.
309 // An exact match will score zero and therefore always be chosen, as
310 // will a partial match where all unmatched elements are explicitly
311 // unspecified. If no options match at all then just pass "none" to
312 // the driver and let it make its own choice.
313 best_standard = VAProcColorStandardNone;
314 best_score = -1;
315 worst_score = 4 * (props->colorspace != AVCOL_SPC_UNSPECIFIED &&
316 props->colorspace != AVCOL_SPC_RGB) +
317 2 * (props->color_trc != AVCOL_TRC_UNSPECIFIED) +
318 (props->color_primaries != AVCOL_PRI_UNSPECIFIED);
319
320 if (worst_score == 0) {
321 // No properties are specified, so we aren't going to be able to
322 // make a useful choice.
323 props->va_color_standard = VAProcColorStandardNone;
324 return;
325 }
326
327 for (i = 0; i < nb_vacs; i++) {
328 for (j = 0; j < FF_ARRAY_ELEMS(vaapi_colour_standard_map); j++) {
329 t = &vaapi_colour_standard_map[j];
330 if (t->va_color_standard != vacs[i])
331 continue;
332
333 score = 0;
334 if (props->colorspace != AVCOL_SPC_UNSPECIFIED &&
335 props->colorspace != AVCOL_SPC_RGB)
336 score += 4 * (props->colorspace != t->colorspace);
337 if (props->color_trc != AVCOL_TRC_UNSPECIFIED)
338 score += 2 * (props->color_trc != t->color_trc);
339 if (props->color_primaries != AVCOL_PRI_UNSPECIFIED)
340 score += (props->color_primaries != t->color_primaries);
341
342 // Only include choices which matched something.
343 if (score < worst_score &&
344 (best_score == -1 || score < best_score)) {
345 best_score = score;
346 best_standard = t->va_color_standard;
347 }
348 }
349 }
350 props->va_color_standard = best_standard;
351 }
352
353 static void vaapi_vpp_fill_chroma_sample_location(VAAPIColourProperties *props)
354 {
355 #if VA_CHECK_VERSION(1, 1, 0)
356 static const struct {
357 enum AVChromaLocation av;
358 uint8_t va;
359 } csl_map[] = {
360 { AVCHROMA_LOC_UNSPECIFIED, VA_CHROMA_SITING_UNKNOWN },
361 { AVCHROMA_LOC_LEFT, VA_CHROMA_SITING_VERTICAL_CENTER |
362 VA_CHROMA_SITING_HORIZONTAL_LEFT },
363 { AVCHROMA_LOC_CENTER, VA_CHROMA_SITING_VERTICAL_CENTER |
364 VA_CHROMA_SITING_HORIZONTAL_CENTER },
365 { AVCHROMA_LOC_TOPLEFT, VA_CHROMA_SITING_VERTICAL_TOP |
366 VA_CHROMA_SITING_HORIZONTAL_LEFT },
367 { AVCHROMA_LOC_TOP, VA_CHROMA_SITING_VERTICAL_TOP |
368 VA_CHROMA_SITING_HORIZONTAL_CENTER },
369 { AVCHROMA_LOC_BOTTOMLEFT, VA_CHROMA_SITING_VERTICAL_BOTTOM |
370 VA_CHROMA_SITING_HORIZONTAL_LEFT },
371 { AVCHROMA_LOC_BOTTOM, VA_CHROMA_SITING_VERTICAL_BOTTOM |
372 VA_CHROMA_SITING_HORIZONTAL_CENTER },
373 };
374 int i;
375
376 for (i = 0; i < FF_ARRAY_ELEMS(csl_map); i++) {
377 if (props->chroma_sample_location == csl_map[i].av) {
378 props->va_chroma_sample_location = csl_map[i].va;
379 return;
380 }
381 }
382 props->va_chroma_sample_location = VA_CHROMA_SITING_UNKNOWN;
383 #else
384 props->va_chroma_sample_location = 0;
385 #endif
386 }
387
388 static void vaapi_vpp_fill_colour_range(VAAPIColourProperties *props)
389 {
390 #if VA_CHECK_VERSION(1, 1, 0)
391 switch (props->color_range) {
392 case AVCOL_RANGE_MPEG:
393 props->va_color_range = VA_SOURCE_RANGE_REDUCED;
394 break;
395 case AVCOL_RANGE_JPEG:
396 props->va_color_range = VA_SOURCE_RANGE_FULL;
397 break;
398 case AVCOL_RANGE_UNSPECIFIED:
399 default:
400 props->va_color_range = VA_SOURCE_RANGE_UNKNOWN;
401 }
402 #else
403 props->va_color_range = 0;
404 #endif
405 }
406
407 static void vaapi_vpp_fill_colour_properties(AVFilterContext *avctx,
408 VAAPIColourProperties *props,
409 VAProcColorStandardType *vacs,
410 int nb_vacs)
411 {
412 vaapi_vpp_fill_colour_standard(props, vacs, nb_vacs);
413 vaapi_vpp_fill_chroma_sample_location(props);
414 vaapi_vpp_fill_colour_range(props);
415
416 av_log(avctx, AV_LOG_DEBUG, "Mapped colour properties %s %s/%s/%s %s "
417 "to VA standard %d chroma siting %#x range %#x.\n",
418 av_color_range_name(props->color_range),
419 av_color_space_name(props->colorspace),
420 av_color_primaries_name(props->color_primaries),
421 av_color_transfer_name(props->color_trc),
422 av_chroma_location_name(props->chroma_sample_location),
423 props->va_color_standard,
424 props->va_chroma_sample_location, props->va_color_range);
425 }
426
427 static int vaapi_vpp_frame_is_rgb(const AVFrame *frame)
428 {
429 const AVHWFramesContext *hwfc;
430 const AVPixFmtDescriptor *desc;
431 av_assert0(frame->format == AV_PIX_FMT_VAAPI &&
432 frame->hw_frames_ctx);
433 hwfc = (const AVHWFramesContext*)frame->hw_frames_ctx->data;
434 desc = av_pix_fmt_desc_get(hwfc->sw_format);
435 av_assert0(desc);
436 return !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
437 }
438
439 static int vaapi_vpp_colour_properties(AVFilterContext *avctx,
440 VAProcPipelineParameterBuffer *params,
441 const AVFrame *input_frame,
442 AVFrame *output_frame)
443 {
444 VAAPIVPPContext *ctx = avctx->priv;
445 VAAPIColourProperties input_props, output_props;
446 VAProcPipelineCaps caps;
447 VAStatus vas;
448
449 vas = vaQueryVideoProcPipelineCaps(ctx->hwctx->display, ctx->va_context,
450 ctx->filter_buffers, ctx->nb_filter_buffers,
451 &caps);
452 if (vas != VA_STATUS_SUCCESS) {
453 av_log(avctx, AV_LOG_ERROR, "Failed to query capabilities for "
454 "colour standard support: %d (%s).\n", vas, vaErrorStr(vas));
455 return AVERROR_EXTERNAL;
456 }
457
458 input_props = (VAAPIColourProperties) {
459 .colorspace = vaapi_vpp_frame_is_rgb(input_frame)
460 ? AVCOL_SPC_RGB : input_frame->colorspace,
461 .color_primaries = input_frame->color_primaries,
462 .color_trc = input_frame->color_trc,
463 .color_range = input_frame->color_range,
464 .chroma_sample_location = input_frame->chroma_location,
465 };
466
467 vaapi_vpp_fill_colour_properties(avctx, &input_props,
468 caps.input_color_standards,
469 caps.num_input_color_standards);
470
471 output_props = (VAAPIColourProperties) {
472 .colorspace = vaapi_vpp_frame_is_rgb(output_frame)
473 ? AVCOL_SPC_RGB : output_frame->colorspace,
474 .color_primaries = output_frame->color_primaries,
475 .color_trc = output_frame->color_trc,
476 .color_range = output_frame->color_range,
477 .chroma_sample_location = output_frame->chroma_location,
478 };
479 vaapi_vpp_fill_colour_properties(avctx, &output_props,
480 caps.output_color_standards,
481 caps.num_output_color_standards);
482
483 // If the properties weren't filled completely in the output frame and
484 // we chose a fixed standard then fill the known values in here.
485 #if VA_CHECK_VERSION(1, 3, 0)
486 if (output_props.va_color_standard != VAProcColorStandardExplicit)
487 #endif
488 {
489 const VAAPIColourProperties *output_standard = NULL;
490 int i;
491
492 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_colour_standard_map); i++) {
493 if (output_props.va_color_standard ==
494 vaapi_colour_standard_map[i].va_color_standard) {
495 output_standard = &vaapi_colour_standard_map[i];
496 break;
497 }
498 }
499 if (output_standard) {
500 output_frame->colorspace = vaapi_vpp_frame_is_rgb(output_frame)
501 ? AVCOL_SPC_RGB : output_standard->colorspace;
502 output_frame->color_primaries = output_standard->color_primaries;
503 output_frame->color_trc = output_standard->color_trc;
504 }
505 }
506
507 params->surface_color_standard = input_props.va_color_standard;
508 params->output_color_standard = output_props.va_color_standard;
509
510 #if VA_CHECK_VERSION(1, 1, 0)
511 params->input_color_properties = (VAProcColorProperties) {
512 .chroma_sample_location = input_props.va_chroma_sample_location,
513 .color_range = input_props.va_color_range,
514 #if VA_CHECK_VERSION(1, 3, 0)
515 .colour_primaries = input_props.color_primaries,
516 .transfer_characteristics = input_props.color_trc,
517 .matrix_coefficients = input_props.colorspace,
518 #endif
519 };
520 params->output_color_properties = (VAProcColorProperties) {
521 .chroma_sample_location = output_props.va_chroma_sample_location,
522 .color_range = output_props.va_color_range,
523 #if VA_CHECK_VERSION(1, 3, 0)
524 .colour_primaries = output_props.color_primaries,
525 .transfer_characteristics = output_props.color_trc,
526 .matrix_coefficients = output_props.colorspace,
527 #endif
528 };
529 #endif
530
531 return 0;
532 }
533
534 int ff_vaapi_vpp_init_params(AVFilterContext *avctx,
535 VAProcPipelineParameterBuffer *params,
536 const AVFrame *input_frame,
537 AVFrame *output_frame)
538 {
539 VAAPIVPPContext *ctx = avctx->priv;
540 int err;
541
542 ctx->input_region = (VARectangle) {
543 .x = input_frame->crop_left,
544 .y = input_frame->crop_top,
545 .width = input_frame->width -
546 (input_frame->crop_left + input_frame->crop_right),
547 .height = input_frame->height -
548 (input_frame->crop_top + input_frame->crop_bottom),
549 };
550 output_frame->crop_top = 0;
551 output_frame->crop_bottom = 0;
552 output_frame->crop_left = 0;
553 output_frame->crop_right = 0;
554
555 *params = (VAProcPipelineParameterBuffer) {
556 .surface = ff_vaapi_vpp_get_surface_id(input_frame),
557 .surface_region = &ctx->input_region,
558 .output_region = NULL,
559 .output_background_color = VAAPI_VPP_BACKGROUND_BLACK,
560 .pipeline_flags = 0,
561 .filter_flags = VA_FRAME_PICTURE,
562
563 // Filter and reference data filled by the filter itself.
564
565 #if VA_CHECK_VERSION(1, 1, 0)
566 .rotation_state = VA_ROTATION_NONE,
567 .mirror_state = VA_MIRROR_NONE,
568 #endif
569 };
570
571 err = vaapi_vpp_colour_properties(avctx, params,
572 input_frame, output_frame);
573 if (err < 0)
574 return err;
575
576 av_log(avctx, AV_LOG_DEBUG, "Filter frame from surface %#x to %#x.\n",
577 ff_vaapi_vpp_get_surface_id(input_frame),
578 ff_vaapi_vpp_get_surface_id(output_frame));
579
580 return 0;
581 }
582
583 int ff_vaapi_vpp_make_param_buffers(AVFilterContext *avctx,
584 int type,
585 const void *data,
586 size_t size,
587 int count)
588 {
589 VAStatus vas;
590 VABufferID buffer;
591 VAAPIVPPContext *ctx = avctx->priv;
592
593 av_assert0(ctx->nb_filter_buffers + 1 <= VAProcFilterCount);
594
595 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
596 type, size, count, (void*)data, &buffer);
597 if (vas != VA_STATUS_SUCCESS) {
598 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
599 "buffer (type %d): %d (%s).\n",
600 type, vas, vaErrorStr(vas));
601 return AVERROR(EIO);
602 }
603
604 ctx->filter_buffers[ctx->nb_filter_buffers++] = buffer;
605
606 av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes, count %d) "
607 "is %#x.\n", type, size, count, buffer);
608 return 0;
609 }
610
611 static int vaapi_vpp_render_single_pipeline_buffer(AVFilterContext *avctx,
612 VAProcPipelineParameterBuffer *params,
613 VABufferID *params_id)
614 {
615 VAAPIVPPContext *ctx = avctx->priv;
616 VAStatus vas;
617
618 vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
619 VAProcPipelineParameterBufferType,
620 sizeof(*params), 1, params, params_id);
621 if (vas != VA_STATUS_SUCCESS) {
622 av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
623 "%d (%s).\n", vas, vaErrorStr(vas));
624 *params_id = VA_INVALID_ID;
625
626 return AVERROR(EIO);
627 }
628 av_log(avctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n", *params_id);
629
630 vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context, params_id, 1);
631 if (vas != VA_STATUS_SUCCESS) {
632 av_log(avctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
633 "%d (%s).\n", vas, vaErrorStr(vas));
634 return AVERROR(EIO);
635 }
636
637 return 0;
638 }
639
640 int ff_vaapi_vpp_render_pictures(AVFilterContext *avctx,
641 VAProcPipelineParameterBuffer *params_list,
642 int cout,
643 AVFrame *output_frame)
644 {
645 VAAPIVPPContext *ctx = avctx->priv;
646 VABufferID *params_ids;
647 VAStatus vas;
648 int err;
649
650 params_ids = (VABufferID *)av_malloc_array(cout, sizeof(VABufferID));
651 if (!params_ids)
652 return AVERROR(ENOMEM);
653
654 for (int i = 0; i < cout; i++)
655 params_ids[i] = VA_INVALID_ID;
656
657 vas = vaBeginPicture(ctx->hwctx->display,
658 ctx->va_context, ff_vaapi_vpp_get_surface_id(output_frame));
659 if (vas != VA_STATUS_SUCCESS) {
660 av_log(avctx, AV_LOG_ERROR, "Failed to attach new picture: "
661 "%d (%s).\n", vas, vaErrorStr(vas));
662 err = AVERROR(EIO);
663 goto fail;
664 }
665
666 for (int i = 0; i < cout; i++) {
667 err = vaapi_vpp_render_single_pipeline_buffer(avctx, &params_list[i], &params_ids[i]);
668 if (err)
669 goto fail_after_begin;
670 }
671
672 vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
673 if (vas != VA_STATUS_SUCCESS) {
674 av_log(avctx, AV_LOG_ERROR, "Failed to start picture processing: "
675 "%d (%s).\n", vas, vaErrorStr(vas));
676 err = AVERROR(EIO);
677 goto fail_after_render;
678 }
679
680 if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
681 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
682 for (int i = 0; i < cout && params_ids[i] != VA_INVALID_ID; i++) {
683 vas = vaDestroyBuffer(ctx->hwctx->display, params_ids[i]);
684 if (vas != VA_STATUS_SUCCESS) {
685 av_log(avctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
686 "%d (%s).\n", vas, vaErrorStr(vas));
687 // And ignore.
688 }
689 }
690 }
691
692 av_freep(&params_ids);
693 return 0;
694
695 // We want to make sure that if vaBeginPicture has been called, we also
696 // call vaRenderPicture and vaEndPicture. These calls may well fail or
697 // do something else nasty, but once we're in this failure case there
698 // isn't much else we can do.
699 fail_after_begin:
700 vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_ids[0], 1);
701 fail_after_render:
702 vaEndPicture(ctx->hwctx->display, ctx->va_context);
703 fail:
704 av_freep(&params_ids);
705 return err;
706 }
707
708 int ff_vaapi_vpp_render_picture(AVFilterContext *avctx,
709 VAProcPipelineParameterBuffer *params,
710 AVFrame *output_frame)
711 {
712 return ff_vaapi_vpp_render_pictures(avctx, params, 1, output_frame);
713 }
714
715 void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
716 {
717 int i;
718 VAAPIVPPContext *ctx = avctx->priv;
719
720 ctx->va_config = VA_INVALID_ID;
721 ctx->va_context = VA_INVALID_ID;
722 ctx->valid_ids = 1;
723
724 for (i = 0; i < VAProcFilterCount; i++)
725 ctx->filter_buffers[i] = VA_INVALID_ID;
726 ctx->nb_filter_buffers = 0;
727 }
728
729 void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
730 {
731 VAAPIVPPContext *ctx = avctx->priv;
732 if (ctx->valid_ids && ctx->pipeline_uninit)
733 ctx->pipeline_uninit(avctx);
734
735 av_buffer_unref(&ctx->input_frames_ref);
736 av_buffer_unref(&ctx->device_ref);
737 }
738