vf_scale_vaapi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 <va/va.h>
  20. #include <va/va_vpp.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/hwcontext.h"
  23. #include "libavutil/hwcontext_vaapi.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. typedef struct ScaleVAAPIContext {
  31. const AVClass *class;
  32. AVVAAPIDeviceContext *hwctx;
  33. AVBufferRef *device_ref;
  34. int valid_ids;
  35. VAConfigID va_config;
  36. VAContextID va_context;
  37. AVBufferRef *input_frames_ref;
  38. AVHWFramesContext *input_frames;
  39. AVBufferRef *output_frames_ref;
  40. AVHWFramesContext *output_frames;
  41. char *output_format_string;
  42. enum AVPixelFormat output_format;
  43. int output_width;
  44. int output_height;
  45. } ScaleVAAPIContext;
  46. static int scale_vaapi_query_formats(AVFilterContext *avctx)
  47. {
  48. enum AVPixelFormat pix_fmts[] = {
  49. AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
  50. };
  51. ff_formats_ref(ff_make_format_list(pix_fmts),
  52. &avctx->inputs[0]->out_formats);
  53. ff_formats_ref(ff_make_format_list(pix_fmts),
  54. &avctx->outputs[0]->in_formats);
  55. return 0;
  56. }
  57. static int scale_vaapi_pipeline_uninit(ScaleVAAPIContext *ctx)
  58. {
  59. if (ctx->va_context != VA_INVALID_ID) {
  60. vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  61. ctx->va_context = VA_INVALID_ID;
  62. }
  63. if (ctx->va_config != VA_INVALID_ID) {
  64. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  65. ctx->va_config = VA_INVALID_ID;
  66. }
  67. av_buffer_unref(&ctx->output_frames_ref);
  68. av_buffer_unref(&ctx->device_ref);
  69. ctx->hwctx = 0;
  70. return 0;
  71. }
  72. static int scale_vaapi_config_input(AVFilterLink *inlink)
  73. {
  74. AVFilterContext *avctx = inlink->dst;
  75. ScaleVAAPIContext *ctx = avctx->priv;
  76. scale_vaapi_pipeline_uninit(ctx);
  77. if (!inlink->hw_frames_ctx) {
  78. av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
  79. "required to associate the processing device.\n");
  80. return AVERROR(EINVAL);
  81. }
  82. ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
  83. ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
  84. return 0;
  85. }
  86. static int scale_vaapi_config_output(AVFilterLink *outlink)
  87. {
  88. AVFilterContext *avctx = outlink->src;
  89. ScaleVAAPIContext *ctx = avctx->priv;
  90. AVVAAPIHWConfig *hwconfig = NULL;
  91. AVHWFramesConstraints *constraints = NULL;
  92. AVVAAPIFramesContext *va_frames;
  93. VAStatus vas;
  94. int err, i;
  95. scale_vaapi_pipeline_uninit(ctx);
  96. ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
  97. ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
  98. av_assert0(ctx->va_config == VA_INVALID_ID);
  99. vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
  100. VAEntrypointVideoProc, 0, 0, &ctx->va_config);
  101. if (vas != VA_STATUS_SUCCESS) {
  102. av_log(ctx, AV_LOG_ERROR, "Failed to create processing pipeline "
  103. "config: %d (%s).\n", vas, vaErrorStr(vas));
  104. err = AVERROR(EIO);
  105. goto fail;
  106. }
  107. hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
  108. if (!hwconfig) {
  109. err = AVERROR(ENOMEM);
  110. goto fail;
  111. }
  112. hwconfig->config_id = ctx->va_config;
  113. constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
  114. hwconfig);
  115. if (!constraints) {
  116. err = AVERROR(ENOMEM);
  117. goto fail;
  118. }
  119. if (ctx->output_format == AV_PIX_FMT_NONE)
  120. ctx->output_format = ctx->input_frames->sw_format;
  121. if (constraints->valid_sw_formats) {
  122. for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
  123. if (ctx->output_format == constraints->valid_sw_formats[i])
  124. break;
  125. }
  126. if (constraints->valid_sw_formats[i] == AV_PIX_FMT_NONE) {
  127. av_log(ctx, AV_LOG_ERROR, "Hardware does not support output "
  128. "format %s.\n", av_get_pix_fmt_name(ctx->output_format));
  129. err = AVERROR(EINVAL);
  130. goto fail;
  131. }
  132. }
  133. if (ctx->output_width < constraints->min_width ||
  134. ctx->output_height < constraints->min_height ||
  135. ctx->output_width > constraints->max_width ||
  136. ctx->output_height > constraints->max_height) {
  137. av_log(ctx, AV_LOG_ERROR, "Hardware does not support scaling to "
  138. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  139. ctx->output_width, ctx->output_height,
  140. constraints->min_width, constraints->max_width,
  141. constraints->min_height, constraints->max_height);
  142. err = AVERROR(EINVAL);
  143. goto fail;
  144. }
  145. ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  146. if (!ctx->output_frames_ref) {
  147. av_log(ctx, AV_LOG_ERROR, "Failed to create HW frame context "
  148. "for output.\n");
  149. err = AVERROR(ENOMEM);
  150. goto fail;
  151. }
  152. ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
  153. ctx->output_frames->format = AV_PIX_FMT_VAAPI;
  154. ctx->output_frames->sw_format = ctx->output_format;
  155. ctx->output_frames->width = ctx->output_width;
  156. ctx->output_frames->height = ctx->output_height;
  157. // The number of output frames we need is determined by what follows
  158. // the filter. If it's an encoder with complex frame reference
  159. // structures then this could be very high.
  160. ctx->output_frames->initial_pool_size = 10;
  161. err = av_hwframe_ctx_init(ctx->output_frames_ref);
  162. if (err < 0) {
  163. av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
  164. "context for output: %d\n", err);
  165. goto fail;
  166. }
  167. va_frames = ctx->output_frames->hwctx;
  168. av_assert0(ctx->va_context == VA_INVALID_ID);
  169. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  170. ctx->output_width, ctx->output_height,
  171. VA_PROGRESSIVE,
  172. va_frames->surface_ids, va_frames->nb_surfaces,
  173. &ctx->va_context);
  174. if (vas != VA_STATUS_SUCCESS) {
  175. av_log(ctx, AV_LOG_ERROR, "Failed to create processing pipeline "
  176. "context: %d (%s).\n", vas, vaErrorStr(vas));
  177. return AVERROR(EIO);
  178. }
  179. outlink->w = ctx->output_width;
  180. outlink->h = ctx->output_height;
  181. outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
  182. if (!outlink->hw_frames_ctx) {
  183. err = AVERROR(ENOMEM);
  184. goto fail;
  185. }
  186. av_freep(&hwconfig);
  187. av_hwframe_constraints_free(&constraints);
  188. return 0;
  189. fail:
  190. av_buffer_unref(&ctx->output_frames_ref);
  191. av_freep(&hwconfig);
  192. av_hwframe_constraints_free(&constraints);
  193. return err;
  194. }
  195. static int vaapi_proc_colour_standard(enum AVColorSpace av_cs)
  196. {
  197. switch(av_cs) {
  198. #define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
  199. CS(BT709, BT709);
  200. CS(BT470BG, BT601);
  201. CS(SMPTE170M, SMPTE170M);
  202. CS(SMPTE240M, SMPTE240M);
  203. #undef CS
  204. default:
  205. return VAProcColorStandardNone;
  206. }
  207. }
  208. static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
  209. {
  210. AVFilterContext *avctx = inlink->dst;
  211. AVFilterLink *outlink = avctx->outputs[0];
  212. ScaleVAAPIContext *ctx = avctx->priv;
  213. AVFrame *output_frame = NULL;
  214. VASurfaceID input_surface, output_surface;
  215. VAProcPipelineParameterBuffer params;
  216. VABufferID params_id;
  217. VAStatus vas;
  218. int err;
  219. av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  220. av_get_pix_fmt_name(input_frame->format),
  221. input_frame->width, input_frame->height, input_frame->pts);
  222. if (ctx->va_context == VA_INVALID_ID)
  223. return AVERROR(EINVAL);
  224. input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
  225. av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for scale input.\n",
  226. input_surface);
  227. output_frame = av_frame_alloc();
  228. if (!output_frame) {
  229. av_log(ctx, AV_LOG_ERROR, "Failed to allocate output frame.");
  230. err = AVERROR(ENOMEM);
  231. goto fail;
  232. }
  233. err = av_hwframe_get_buffer(ctx->output_frames_ref, output_frame, 0);
  234. if (err < 0) {
  235. av_log(ctx, AV_LOG_ERROR, "Failed to get surface for "
  236. "output: %d\n.", err);
  237. }
  238. output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
  239. av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for scale output.\n",
  240. output_surface);
  241. memset(&params, 0, sizeof(params));
  242. params.surface = input_surface;
  243. params.surface_region = 0;
  244. params.surface_color_standard =
  245. vaapi_proc_colour_standard(input_frame->colorspace);
  246. params.output_region = 0;
  247. params.output_background_color = 0xff000000;
  248. params.output_color_standard = params.surface_color_standard;
  249. params.pipeline_flags = 0;
  250. params.filter_flags = VA_FILTER_SCALING_HQ;
  251. vas = vaBeginPicture(ctx->hwctx->display,
  252. ctx->va_context, output_surface);
  253. if (vas != VA_STATUS_SUCCESS) {
  254. av_log(ctx, AV_LOG_ERROR, "Failed to attach new picture: "
  255. "%d (%s).\n", vas, vaErrorStr(vas));
  256. err = AVERROR(EIO);
  257. goto fail;
  258. }
  259. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  260. VAProcPipelineParameterBufferType,
  261. sizeof(params), 1, &params, &params_id);
  262. if (vas != VA_STATUS_SUCCESS) {
  263. av_log(ctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
  264. "%d (%s).\n", vas, vaErrorStr(vas));
  265. err = AVERROR(EIO);
  266. goto fail_after_begin;
  267. }
  268. av_log(ctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
  269. params_id);
  270. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  271. &params_id, 1);
  272. if (vas != VA_STATUS_SUCCESS) {
  273. av_log(ctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
  274. "%d (%s).\n", vas, vaErrorStr(vas));
  275. err = AVERROR(EIO);
  276. goto fail_after_begin;
  277. }
  278. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  279. if (vas != VA_STATUS_SUCCESS) {
  280. av_log(ctx, AV_LOG_ERROR, "Failed to start picture processing: "
  281. "%d (%s).\n", vas, vaErrorStr(vas));
  282. err = AVERROR(EIO);
  283. goto fail_after_render;
  284. }
  285. // This doesn't get freed automatically for some reason.
  286. vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
  287. if (vas != VA_STATUS_SUCCESS) {
  288. av_log(ctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
  289. "%d (%s).\n", vas, vaErrorStr(vas));
  290. err = AVERROR(EIO);
  291. goto fail;
  292. }
  293. av_frame_copy_props(output_frame, input_frame);
  294. av_frame_free(&input_frame);
  295. av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  296. av_get_pix_fmt_name(output_frame->format),
  297. output_frame->width, output_frame->height, output_frame->pts);
  298. return ff_filter_frame(outlink, output_frame);
  299. // We want to make sure that if vaBeginPicture has been called, we also
  300. // call vaRenderPicture and vaEndPicture. These calls may well fail or
  301. // do something else nasty, but once we're in this failure case there
  302. // isn't much else we can do.
  303. fail_after_begin:
  304. vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
  305. fail_after_render:
  306. vaEndPicture(ctx->hwctx->display, ctx->va_context);
  307. fail:
  308. av_frame_free(&input_frame);
  309. av_frame_free(&output_frame);
  310. return err;
  311. }
  312. static av_cold int scale_vaapi_init(AVFilterContext *avctx)
  313. {
  314. ScaleVAAPIContext *ctx = avctx->priv;
  315. ctx->va_config = VA_INVALID_ID;
  316. ctx->va_context = VA_INVALID_ID;
  317. ctx->valid_ids = 1;
  318. if (ctx->output_format_string) {
  319. ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
  320. if (ctx->output_format == AV_PIX_FMT_NONE) {
  321. av_log(ctx, AV_LOG_ERROR, "Invalid output format.\n");
  322. return AVERROR(EINVAL);
  323. }
  324. } else {
  325. // Use the input format once that is configured.
  326. ctx->output_format = AV_PIX_FMT_NONE;
  327. }
  328. return 0;
  329. }
  330. static av_cold void scale_vaapi_uninit(AVFilterContext *avctx)
  331. {
  332. ScaleVAAPIContext *ctx = avctx->priv;
  333. if (ctx->valid_ids)
  334. scale_vaapi_pipeline_uninit(ctx);
  335. av_buffer_unref(&ctx->input_frames_ref);
  336. av_buffer_unref(&ctx->output_frames_ref);
  337. av_buffer_unref(&ctx->device_ref);
  338. }
  339. #define OFFSET(x) offsetof(ScaleVAAPIContext, x)
  340. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
  341. static const AVOption scale_vaapi_options[] = {
  342. { "w", "Output video width",
  343. OFFSET(output_width), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
  344. { "h", "Output video height",
  345. OFFSET(output_height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
  346. { "format", "Output video format (software format of hardware frames)",
  347. OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
  348. { NULL },
  349. };
  350. static const AVClass scale_vaapi_class = {
  351. .class_name = "scale_vaapi",
  352. .item_name = av_default_item_name,
  353. .option = scale_vaapi_options,
  354. .version = LIBAVUTIL_VERSION_INT,
  355. };
  356. static const AVFilterPad scale_vaapi_inputs[] = {
  357. {
  358. .name = "default",
  359. .type = AVMEDIA_TYPE_VIDEO,
  360. .filter_frame = &scale_vaapi_filter_frame,
  361. .config_props = &scale_vaapi_config_input,
  362. },
  363. { NULL }
  364. };
  365. static const AVFilterPad scale_vaapi_outputs[] = {
  366. {
  367. .name = "default",
  368. .type = AVMEDIA_TYPE_VIDEO,
  369. .config_props = &scale_vaapi_config_output,
  370. },
  371. { NULL }
  372. };
  373. AVFilter ff_vf_scale_vaapi = {
  374. .name = "scale_vaapi",
  375. .description = NULL_IF_CONFIG_SMALL("Scale to/from VAAPI surfaces."),
  376. .priv_size = sizeof(ScaleVAAPIContext),
  377. .init = &scale_vaapi_init,
  378. .uninit = &scale_vaapi_uninit,
  379. .query_formats = &scale_vaapi_query_formats,
  380. .inputs = scale_vaapi_inputs,
  381. .outputs = scale_vaapi_outputs,
  382. .priv_class = &scale_vaapi_class,
  383. };