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.

374 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 "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. int ff_vaapi_vpp_colour_standard(enum AVColorSpace av_cs)
  202. {
  203. switch(av_cs) {
  204. #define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
  205. CS(BT709, BT709);
  206. CS(BT470BG, BT601);
  207. CS(SMPTE170M, SMPTE170M);
  208. CS(SMPTE240M, SMPTE240M);
  209. #undef CS
  210. default:
  211. return VAProcColorStandardNone;
  212. }
  213. }
  214. int ff_vaapi_vpp_make_param_buffers(AVFilterContext *avctx,
  215. int type,
  216. const void *data,
  217. size_t size,
  218. int count)
  219. {
  220. VAStatus vas;
  221. VABufferID buffer;
  222. VAAPIVPPContext *ctx = avctx->priv;
  223. av_assert0(ctx->nb_filter_buffers + 1 <= VAProcFilterCount);
  224. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  225. type, size, count, (void*)data, &buffer);
  226. if (vas != VA_STATUS_SUCCESS) {
  227. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
  228. "buffer (type %d): %d (%s).\n",
  229. type, vas, vaErrorStr(vas));
  230. return AVERROR(EIO);
  231. }
  232. ctx->filter_buffers[ctx->nb_filter_buffers++] = buffer;
  233. av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes, count %d) "
  234. "is %#x.\n", type, size, count, buffer);
  235. return 0;
  236. }
  237. int ff_vaapi_vpp_render_picture(AVFilterContext *avctx,
  238. VAProcPipelineParameterBuffer *params,
  239. VASurfaceID output_surface)
  240. {
  241. VABufferID params_id;
  242. VAStatus vas;
  243. int err = 0;
  244. VAAPIVPPContext *ctx = avctx->priv;
  245. vas = vaBeginPicture(ctx->hwctx->display,
  246. ctx->va_context, output_surface);
  247. if (vas != VA_STATUS_SUCCESS) {
  248. av_log(avctx, AV_LOG_ERROR, "Failed to attach new picture: "
  249. "%d (%s).\n", vas, vaErrorStr(vas));
  250. err = AVERROR(EIO);
  251. goto fail;
  252. }
  253. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  254. VAProcPipelineParameterBufferType,
  255. sizeof(*params), 1, params, &params_id);
  256. if (vas != VA_STATUS_SUCCESS) {
  257. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
  258. "%d (%s).\n", vas, vaErrorStr(vas));
  259. err = AVERROR(EIO);
  260. goto fail_after_begin;
  261. }
  262. av_log(avctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
  263. params_id);
  264. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  265. &params_id, 1);
  266. if (vas != VA_STATUS_SUCCESS) {
  267. av_log(avctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
  268. "%d (%s).\n", vas, vaErrorStr(vas));
  269. err = AVERROR(EIO);
  270. goto fail_after_begin;
  271. }
  272. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  273. if (vas != VA_STATUS_SUCCESS) {
  274. av_log(avctx, AV_LOG_ERROR, "Failed to start picture processing: "
  275. "%d (%s).\n", vas, vaErrorStr(vas));
  276. err = AVERROR(EIO);
  277. goto fail_after_render;
  278. }
  279. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  280. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
  281. vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
  282. if (vas != VA_STATUS_SUCCESS) {
  283. av_log(avctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
  284. "%d (%s).\n", vas, vaErrorStr(vas));
  285. // And ignore.
  286. }
  287. }
  288. return 0;
  289. // We want to make sure that if vaBeginPicture has been called, we also
  290. // call vaRenderPicture and vaEndPicture. These calls may well fail or
  291. // do something else nasty, but once we're in this failure case there
  292. // isn't much else we can do.
  293. fail_after_begin:
  294. vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
  295. fail_after_render:
  296. vaEndPicture(ctx->hwctx->display, ctx->va_context);
  297. fail:
  298. return err;
  299. }
  300. void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
  301. {
  302. int i;
  303. VAAPIVPPContext *ctx = avctx->priv;
  304. ctx->va_config = VA_INVALID_ID;
  305. ctx->va_context = VA_INVALID_ID;
  306. ctx->valid_ids = 1;
  307. for (i = 0; i < VAProcFilterCount; i++)
  308. ctx->filter_buffers[i] = VA_INVALID_ID;
  309. ctx->nb_filter_buffers = 0;
  310. }
  311. void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
  312. {
  313. VAAPIVPPContext *ctx = avctx->priv;
  314. if (ctx->valid_ids && ctx->pipeline_uninit)
  315. ctx->pipeline_uninit(avctx);
  316. av_buffer_unref(&ctx->input_frames_ref);
  317. av_buffer_unref(&ctx->device_ref);
  318. }