vf_misc_vaapi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/mem.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "vaapi_vpp.h"
  27. // Denoise min/max/default Values
  28. #define DENOISE_MIN 0
  29. #define DENOISE_MAX 64
  30. #define DENOISE_DEFAULT 0
  31. // Sharpness min/max/default values
  32. #define SHARPNESS_MIN 0
  33. #define SHARPNESS_MAX 64
  34. #define SHARPNESS_DEFAULT 44
  35. typedef struct DenoiseVAAPIContext {
  36. VAAPIVPPContext vpp_ctx; // must be the first field
  37. int denoise; // enable denoise algo.
  38. } DenoiseVAAPIContext;
  39. typedef struct SharpnessVAAPIContext {
  40. VAAPIVPPContext vpp_ctx; // must be the first field
  41. int sharpness; // enable sharpness.
  42. } SharpnessVAAPIContext;
  43. static float map(int x, int in_min, int in_max, float out_min, float out_max)
  44. {
  45. double slope, output;
  46. slope = 1.0 * (out_max - out_min) / (in_max - in_min);
  47. output = out_min + slope * (x - in_min);
  48. return (float)output;
  49. }
  50. static int denoise_vaapi_build_filter_params(AVFilterContext *avctx)
  51. {
  52. VAAPIVPPContext *vpp_ctx = avctx->priv;
  53. DenoiseVAAPIContext *ctx = avctx->priv;
  54. VAProcFilterCap caps;
  55. VAStatus vas;
  56. uint32_t num_caps = 1;
  57. VAProcFilterParameterBuffer denoise;
  58. vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
  59. VAProcFilterNoiseReduction,
  60. &caps, &num_caps);
  61. if (vas != VA_STATUS_SUCCESS) {
  62. av_log(avctx, AV_LOG_ERROR, "Failed to query denoise caps "
  63. "context: %d (%s).\n", vas, vaErrorStr(vas));
  64. return AVERROR(EIO);
  65. }
  66. denoise.type = VAProcFilterNoiseReduction;
  67. denoise.value = map(ctx->denoise, DENOISE_MIN, DENOISE_MAX,
  68. caps.range.min_value,
  69. caps.range.max_value);
  70. return ff_vaapi_vpp_make_param_buffers(avctx,
  71. VAProcFilterParameterBufferType,
  72. &denoise, sizeof(denoise), 1);
  73. }
  74. static int sharpness_vaapi_build_filter_params(AVFilterContext *avctx)
  75. {
  76. VAAPIVPPContext *vpp_ctx = avctx->priv;
  77. SharpnessVAAPIContext *ctx = avctx->priv;
  78. VAProcFilterCap caps;
  79. VAStatus vas;
  80. uint32_t num_caps = 1;
  81. VAProcFilterParameterBuffer sharpness;
  82. vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
  83. VAProcFilterSharpening,
  84. &caps, &num_caps);
  85. if (vas != VA_STATUS_SUCCESS) {
  86. av_log(avctx, AV_LOG_ERROR, "Failed to query sharpness caps "
  87. "context: %d (%s).\n", vas, vaErrorStr(vas));
  88. return AVERROR(EIO);
  89. }
  90. sharpness.type = VAProcFilterSharpening;
  91. sharpness.value = map(ctx->sharpness,
  92. SHARPNESS_MIN, SHARPNESS_MAX,
  93. caps.range.min_value,
  94. caps.range.max_value);
  95. return ff_vaapi_vpp_make_param_buffers(avctx,
  96. VAProcFilterParameterBufferType,
  97. &sharpness, sizeof(sharpness), 1);
  98. }
  99. static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
  100. {
  101. AVFilterContext *avctx = inlink->dst;
  102. AVFilterLink *outlink = avctx->outputs[0];
  103. VAAPIVPPContext *vpp_ctx = avctx->priv;
  104. AVFrame *output_frame = NULL;
  105. VAProcPipelineParameterBuffer params;
  106. int err;
  107. av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  108. av_get_pix_fmt_name(input_frame->format),
  109. input_frame->width, input_frame->height, input_frame->pts);
  110. if (vpp_ctx->va_context == VA_INVALID_ID)
  111. return AVERROR(EINVAL);
  112. output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
  113. vpp_ctx->output_height);
  114. if (!output_frame) {
  115. err = AVERROR(ENOMEM);
  116. goto fail;
  117. }
  118. err = av_frame_copy_props(output_frame, input_frame);
  119. if (err < 0)
  120. return err;
  121. err = ff_vaapi_vpp_init_params(avctx, &params,
  122. input_frame, output_frame);
  123. if (err < 0)
  124. goto fail;
  125. if (vpp_ctx->nb_filter_buffers) {
  126. params.filters = &vpp_ctx->filter_buffers[0];
  127. params.num_filters = vpp_ctx->nb_filter_buffers;
  128. }
  129. err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
  130. if (err < 0)
  131. goto fail;
  132. av_frame_free(&input_frame);
  133. av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  134. av_get_pix_fmt_name(output_frame->format),
  135. output_frame->width, output_frame->height, output_frame->pts);
  136. return ff_filter_frame(outlink, output_frame);
  137. fail:
  138. av_frame_free(&input_frame);
  139. av_frame_free(&output_frame);
  140. return err;
  141. }
  142. static av_cold int denoise_vaapi_init(AVFilterContext *avctx)
  143. {
  144. VAAPIVPPContext *vpp_ctx = avctx->priv;
  145. ff_vaapi_vpp_ctx_init(avctx);
  146. vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
  147. vpp_ctx->build_filter_params = denoise_vaapi_build_filter_params;
  148. vpp_ctx->output_format = AV_PIX_FMT_NONE;
  149. return 0;
  150. }
  151. static av_cold int sharpness_vaapi_init(AVFilterContext *avctx)
  152. {
  153. VAAPIVPPContext *vpp_ctx = avctx->priv;
  154. ff_vaapi_vpp_ctx_init(avctx);
  155. vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
  156. vpp_ctx->build_filter_params = sharpness_vaapi_build_filter_params;
  157. vpp_ctx->output_format = AV_PIX_FMT_NONE;
  158. return 0;
  159. }
  160. #define DOFFSET(x) offsetof(DenoiseVAAPIContext, x)
  161. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
  162. static const AVOption denoise_vaapi_options[] = {
  163. { "denoise", "denoise level",
  164. DOFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = DENOISE_DEFAULT }, DENOISE_MIN, DENOISE_MAX, .flags = FLAGS },
  165. { NULL },
  166. };
  167. #define SOFFSET(x) offsetof(SharpnessVAAPIContext, x)
  168. static const AVOption sharpness_vaapi_options[] = {
  169. { "sharpness", "sharpness level",
  170. SOFFSET(sharpness), AV_OPT_TYPE_INT, { .i64 = SHARPNESS_DEFAULT }, SHARPNESS_MIN, SHARPNESS_MAX, .flags = FLAGS },
  171. { NULL },
  172. };
  173. AVFILTER_DEFINE_CLASS(denoise_vaapi);
  174. AVFILTER_DEFINE_CLASS(sharpness_vaapi);
  175. static const AVFilterPad misc_vaapi_inputs[] = {
  176. {
  177. .name = "default",
  178. .type = AVMEDIA_TYPE_VIDEO,
  179. .filter_frame = &misc_vaapi_filter_frame,
  180. .config_props = &ff_vaapi_vpp_config_input,
  181. },
  182. { NULL }
  183. };
  184. static const AVFilterPad misc_vaapi_outputs[] = {
  185. {
  186. .name = "default",
  187. .type = AVMEDIA_TYPE_VIDEO,
  188. .config_props = &ff_vaapi_vpp_config_output,
  189. },
  190. { NULL }
  191. };
  192. AVFilter ff_vf_denoise_vaapi = {
  193. .name = "denoise_vaapi",
  194. .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for de-noise"),
  195. .priv_size = sizeof(DenoiseVAAPIContext),
  196. .init = &denoise_vaapi_init,
  197. .uninit = &ff_vaapi_vpp_ctx_uninit,
  198. .query_formats = &ff_vaapi_vpp_query_formats,
  199. .inputs = misc_vaapi_inputs,
  200. .outputs = misc_vaapi_outputs,
  201. .priv_class = &denoise_vaapi_class,
  202. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  203. };
  204. AVFilter ff_vf_sharpness_vaapi = {
  205. .name = "sharpness_vaapi",
  206. .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for sharpness"),
  207. .priv_size = sizeof(SharpnessVAAPIContext),
  208. .init = &sharpness_vaapi_init,
  209. .uninit = &ff_vaapi_vpp_ctx_uninit,
  210. .query_formats = &ff_vaapi_vpp_query_formats,
  211. .inputs = misc_vaapi_inputs,
  212. .outputs = misc_vaapi_outputs,
  213. .priv_class = &sharpness_vaapi_class,
  214. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  215. };