You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
12KB

  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 "vaapi_vpp.h"
  23. int ff_vaapi_vpp_query_formats(AVFilterContext *avctx)
  24. {
  25. enum AVPixelFormat pix_fmts[] = {
  26. AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
  27. };
  28. int err;
  29. if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
  30. &avctx->inputs[0]->out_formats)) < 0)
  31. return err;
  32. if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
  33. &avctx->outputs[0]->in_formats)) < 0)
  34. return err;
  35. return 0;
  36. }
  37. void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
  38. {
  39. VAAPIVPPContext *ctx = avctx->priv;
  40. int i;
  41. for (i = 0; i < ctx->nb_filter_buffers; i++) {
  42. if (ctx->filter_buffers[i] != VA_INVALID_ID) {
  43. vaDestroyBuffer(ctx->hwctx->display, ctx->filter_buffers[i]);
  44. ctx->filter_buffers[i] = VA_INVALID_ID;
  45. }
  46. }
  47. ctx->nb_filter_buffers = 0;
  48. if (ctx->va_context != VA_INVALID_ID) {
  49. vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  50. ctx->va_context = VA_INVALID_ID;
  51. }
  52. if (ctx->va_config != VA_INVALID_ID) {
  53. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  54. ctx->va_config = VA_INVALID_ID;
  55. }
  56. av_buffer_unref(&ctx->output_frames_ref);
  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. AVVAAPIFramesContext *va_frames;
  87. VAStatus vas;
  88. int err, i;
  89. if (ctx->pipeline_uninit)
  90. ctx->pipeline_uninit(avctx);
  91. if (!ctx->output_width)
  92. ctx->output_width = avctx->inputs[0]->w;
  93. if (!ctx->output_height)
  94. ctx->output_height = avctx->inputs[0]->h;
  95. av_assert0(ctx->input_frames);
  96. ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
  97. if (!ctx->device_ref) {
  98. av_log(avctx, AV_LOG_ERROR, "A device reference create "
  99. "failed.\n");
  100. return AVERROR(ENOMEM);
  101. }
  102. ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
  103. av_assert0(ctx->va_config == VA_INVALID_ID);
  104. vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
  105. VAEntrypointVideoProc, NULL, 0, &ctx->va_config);
  106. if (vas != VA_STATUS_SUCCESS) {
  107. av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
  108. "config: %d (%s).\n", vas, vaErrorStr(vas));
  109. err = AVERROR(EIO);
  110. goto fail;
  111. }
  112. hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
  113. if (!hwconfig) {
  114. err = AVERROR(ENOMEM);
  115. goto fail;
  116. }
  117. hwconfig->config_id = ctx->va_config;
  118. constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
  119. hwconfig);
  120. if (!constraints) {
  121. err = AVERROR(ENOMEM);
  122. goto fail;
  123. }
  124. if (ctx->output_format == AV_PIX_FMT_NONE)
  125. ctx->output_format = ctx->input_frames->sw_format;
  126. if (constraints->valid_sw_formats) {
  127. for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
  128. if (ctx->output_format == constraints->valid_sw_formats[i])
  129. break;
  130. }
  131. if (constraints->valid_sw_formats[i] == AV_PIX_FMT_NONE) {
  132. av_log(avctx, AV_LOG_ERROR, "Hardware does not support output "
  133. "format %s.\n", av_get_pix_fmt_name(ctx->output_format));
  134. err = AVERROR(EINVAL);
  135. goto fail;
  136. }
  137. }
  138. if (ctx->output_width < constraints->min_width ||
  139. ctx->output_height < constraints->min_height ||
  140. ctx->output_width > constraints->max_width ||
  141. ctx->output_height > constraints->max_height) {
  142. av_log(avctx, AV_LOG_ERROR, "Hardware does not support scaling to "
  143. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  144. ctx->output_width, ctx->output_height,
  145. constraints->min_width, constraints->max_width,
  146. constraints->min_height, constraints->max_height);
  147. err = AVERROR(EINVAL);
  148. goto fail;
  149. }
  150. ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  151. if (!ctx->output_frames_ref) {
  152. av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
  153. "for output.\n");
  154. err = AVERROR(ENOMEM);
  155. goto fail;
  156. }
  157. ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
  158. ctx->output_frames->format = AV_PIX_FMT_VAAPI;
  159. ctx->output_frames->sw_format = ctx->output_format;
  160. ctx->output_frames->width = ctx->output_width;
  161. ctx->output_frames->height = ctx->output_height;
  162. // The number of output frames we need is determined by what follows
  163. // the filter. If it's an encoder with complex frame reference
  164. // structures then this could be very high.
  165. ctx->output_frames->initial_pool_size = 10;
  166. err = av_hwframe_ctx_init(ctx->output_frames_ref);
  167. if (err < 0) {
  168. av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
  169. "context for output: %d\n", err);
  170. goto fail;
  171. }
  172. va_frames = ctx->output_frames->hwctx;
  173. av_assert0(ctx->va_context == VA_INVALID_ID);
  174. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  175. ctx->output_width, ctx->output_height,
  176. VA_PROGRESSIVE,
  177. va_frames->surface_ids, va_frames->nb_surfaces,
  178. &ctx->va_context);
  179. if (vas != VA_STATUS_SUCCESS) {
  180. av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
  181. "context: %d (%s).\n", vas, vaErrorStr(vas));
  182. return AVERROR(EIO);
  183. }
  184. outlink->w = ctx->output_width;
  185. outlink->h = ctx->output_height;
  186. if (ctx->build_filter_params) {
  187. err = ctx->build_filter_params(avctx);
  188. if (err < 0)
  189. goto fail;
  190. }
  191. outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
  192. if (!outlink->hw_frames_ctx) {
  193. err = AVERROR(ENOMEM);
  194. goto fail;
  195. }
  196. av_freep(&hwconfig);
  197. av_hwframe_constraints_free(&constraints);
  198. return 0;
  199. fail:
  200. av_buffer_unref(&ctx->output_frames_ref);
  201. av_freep(&hwconfig);
  202. av_hwframe_constraints_free(&constraints);
  203. return err;
  204. }
  205. int ff_vaapi_vpp_colour_standard(enum AVColorSpace av_cs)
  206. {
  207. switch(av_cs) {
  208. #define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
  209. CS(BT709, BT709);
  210. CS(BT470BG, BT601);
  211. CS(SMPTE170M, SMPTE170M);
  212. CS(SMPTE240M, SMPTE240M);
  213. #undef CS
  214. default:
  215. return VAProcColorStandardNone;
  216. }
  217. }
  218. int ff_vaapi_vpp_make_param_buffers(AVFilterContext *avctx,
  219. int type,
  220. const void *data,
  221. size_t size,
  222. int count)
  223. {
  224. VAStatus vas;
  225. VABufferID buffer;
  226. VAAPIVPPContext *ctx = avctx->priv;
  227. av_assert0(ctx->nb_filter_buffers + 1 <= VAProcFilterCount);
  228. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  229. type, size, count, (void*)data, &buffer);
  230. if (vas != VA_STATUS_SUCCESS) {
  231. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
  232. "buffer (type %d): %d (%s).\n",
  233. type, vas, vaErrorStr(vas));
  234. return AVERROR(EIO);
  235. }
  236. ctx->filter_buffers[ctx->nb_filter_buffers++] = buffer;
  237. av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes, count %d) "
  238. "is %#x.\n", type, size, count, buffer);
  239. return 0;
  240. }
  241. int ff_vaapi_vpp_render_picture(AVFilterContext *avctx,
  242. VAProcPipelineParameterBuffer *params,
  243. VASurfaceID output_surface)
  244. {
  245. VABufferID params_id;
  246. VAStatus vas;
  247. int err = 0;
  248. VAAPIVPPContext *ctx = avctx->priv;
  249. vas = vaBeginPicture(ctx->hwctx->display,
  250. ctx->va_context, output_surface);
  251. if (vas != VA_STATUS_SUCCESS) {
  252. av_log(avctx, AV_LOG_ERROR, "Failed to attach new picture: "
  253. "%d (%s).\n", vas, vaErrorStr(vas));
  254. err = AVERROR(EIO);
  255. goto fail;
  256. }
  257. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  258. VAProcPipelineParameterBufferType,
  259. sizeof(*params), 1, params, &params_id);
  260. if (vas != VA_STATUS_SUCCESS) {
  261. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
  262. "%d (%s).\n", vas, vaErrorStr(vas));
  263. err = AVERROR(EIO);
  264. goto fail_after_begin;
  265. }
  266. av_log(avctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
  267. params_id);
  268. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  269. &params_id, 1);
  270. if (vas != VA_STATUS_SUCCESS) {
  271. av_log(avctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
  272. "%d (%s).\n", vas, vaErrorStr(vas));
  273. err = AVERROR(EIO);
  274. goto fail_after_begin;
  275. }
  276. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  277. if (vas != VA_STATUS_SUCCESS) {
  278. av_log(avctx, AV_LOG_ERROR, "Failed to start picture processing: "
  279. "%d (%s).\n", vas, vaErrorStr(vas));
  280. err = AVERROR(EIO);
  281. goto fail_after_render;
  282. }
  283. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  284. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
  285. vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
  286. if (vas != VA_STATUS_SUCCESS) {
  287. av_log(avctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
  288. "%d (%s).\n", vas, vaErrorStr(vas));
  289. // And ignore.
  290. }
  291. }
  292. return 0;
  293. // We want to make sure that if vaBeginPicture has been called, we also
  294. // call vaRenderPicture and vaEndPicture. These calls may well fail or
  295. // do something else nasty, but once we're in this failure case there
  296. // isn't much else we can do.
  297. fail_after_begin:
  298. vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
  299. fail_after_render:
  300. vaEndPicture(ctx->hwctx->display, ctx->va_context);
  301. fail:
  302. return err;
  303. }
  304. void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
  305. {
  306. int i;
  307. VAAPIVPPContext *ctx = avctx->priv;
  308. ctx->va_config = VA_INVALID_ID;
  309. ctx->va_context = VA_INVALID_ID;
  310. ctx->valid_ids = 1;
  311. for (i = 0; i < VAProcFilterCount; i++)
  312. ctx->filter_buffers[i] = VA_INVALID_ID;
  313. ctx->nb_filter_buffers = 0;
  314. }
  315. void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
  316. {
  317. VAAPIVPPContext *ctx = avctx->priv;
  318. if (ctx->valid_ids && ctx->pipeline_uninit)
  319. ctx->pipeline_uninit(avctx);
  320. av_buffer_unref(&ctx->input_frames_ref);
  321. av_buffer_unref(&ctx->output_frames_ref);
  322. av_buffer_unref(&ctx->device_ref);
  323. }