vaapi_vpp.c 24 KB

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