vf_procamp_vaapi.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // ProcAmp Min/Max/Default Values
  28. #define BRIGHTNESS_MIN -100.0F
  29. #define BRIGHTNESS_MAX 100.0F
  30. #define BRIGHTNESS_DEFAULT 0.0F
  31. #define CONTRAST_MIN 0.0F
  32. #define CONTRAST_MAX 10.0F
  33. #define CONTRAST_DEFAULT 1.0F
  34. #define HUE_MIN -180.0F
  35. #define HUE_MAX 180.0F
  36. #define HUE_DEFAULT 0.0F
  37. #define SATURATION_MIN 0.0F
  38. #define SATURATION_MAX 10.0F
  39. #define SATURATION_DEFAULT 1.0F
  40. typedef struct ProcampVAAPIContext {
  41. VAAPIVPPContext vpp_ctx; // must be the first field
  42. float bright;
  43. float hue;
  44. float saturation;
  45. float contrast;
  46. } ProcampVAAPIContext;
  47. static float map(float x, float in_min, float in_max, float out_min, float out_max)
  48. {
  49. double slope, output;
  50. slope = 1.0 * (out_max - out_min) / (in_max - in_min);
  51. output = out_min + slope * (x - in_min);
  52. return (float)output;
  53. }
  54. static int procamp_vaapi_build_filter_params(AVFilterContext *avctx)
  55. {
  56. VAAPIVPPContext *vpp_ctx = avctx->priv;
  57. ProcampVAAPIContext *ctx = avctx->priv;
  58. VAStatus vas;
  59. VAProcFilterParameterBufferColorBalance procamp_params[4];
  60. VAProcFilterCapColorBalance procamp_caps[VAProcColorBalanceCount];
  61. int num_caps;
  62. int i = 0;
  63. memset(&procamp_params, 0, sizeof(procamp_params));
  64. memset(&procamp_caps, 0, sizeof(procamp_caps));
  65. num_caps = VAProcColorBalanceCount;
  66. vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
  67. VAProcFilterColorBalance, &procamp_caps, &num_caps);
  68. if (vas != VA_STATUS_SUCCESS) {
  69. av_log(avctx, AV_LOG_ERROR, "Failed to query procamp "
  70. "filter caps: %d (%s).\n", vas, vaErrorStr(vas));
  71. return AVERROR(EIO);
  72. }
  73. /* brightness */
  74. procamp_params[i].type = VAProcFilterColorBalance;
  75. procamp_params[i].attrib = VAProcColorBalanceBrightness;
  76. procamp_params[i].value = map(ctx->bright, BRIGHTNESS_MIN, BRIGHTNESS_MAX,
  77. procamp_caps[VAProcColorBalanceBrightness-1].range.min_value,
  78. procamp_caps[VAProcColorBalanceBrightness-1].range.max_value);
  79. i++;
  80. /* contrast */
  81. procamp_params[i].type = VAProcFilterColorBalance;
  82. procamp_params[i].attrib = VAProcColorBalanceContrast;
  83. procamp_params[i].value = map(ctx->contrast, CONTRAST_MIN, CONTRAST_MAX,
  84. procamp_caps[VAProcColorBalanceContrast-1].range.min_value,
  85. procamp_caps[VAProcColorBalanceContrast-1].range.max_value);
  86. i++;
  87. /* hue */
  88. procamp_params[i].type = VAProcFilterColorBalance;
  89. procamp_params[i].attrib = VAProcColorBalanceHue;
  90. procamp_params[i].value = map(ctx->hue, HUE_MIN, HUE_MAX,
  91. procamp_caps[VAProcColorBalanceHue-1].range.min_value,
  92. procamp_caps[VAProcColorBalanceHue-1].range.max_value);
  93. i++;
  94. /* saturation */
  95. procamp_params[i].type = VAProcFilterColorBalance;
  96. procamp_params[i].attrib = VAProcColorBalanceSaturation;
  97. procamp_params[i].value = map(ctx->saturation, SATURATION_MIN, SATURATION_MAX,
  98. procamp_caps[VAProcColorBalanceSaturation-1].range.min_value,
  99. procamp_caps[VAProcColorBalanceSaturation-1].range.max_value);
  100. i++;
  101. return ff_vaapi_vpp_make_param_buffers(avctx,
  102. VAProcFilterParameterBufferType,
  103. &procamp_params,
  104. sizeof(procamp_params[0]),
  105. i);
  106. }
  107. static int procamp_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
  108. {
  109. AVFilterContext *avctx = inlink->dst;
  110. AVFilterLink *outlink = avctx->outputs[0];
  111. VAAPIVPPContext *vpp_ctx = avctx->priv;
  112. AVFrame *output_frame = NULL;
  113. VAProcPipelineParameterBuffer params;
  114. int err;
  115. av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
  116. av_get_pix_fmt_name(input_frame->format),
  117. input_frame->width, input_frame->height, input_frame->pts);
  118. if (vpp_ctx->va_context == VA_INVALID_ID)
  119. return AVERROR(EINVAL);
  120. output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
  121. vpp_ctx->output_height);
  122. if (!output_frame) {
  123. err = AVERROR(ENOMEM);
  124. goto fail;
  125. }
  126. err = av_frame_copy_props(output_frame, input_frame);
  127. if (err < 0)
  128. return err;
  129. err = ff_vaapi_vpp_init_params(avctx, &params,
  130. input_frame, output_frame);
  131. if (err < 0)
  132. goto fail;
  133. params.filters = &vpp_ctx->filter_buffers[0];
  134. params.num_filters = 1;
  135. err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
  136. if (err < 0)
  137. goto fail;
  138. av_frame_free(&input_frame);
  139. av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
  140. av_get_pix_fmt_name(output_frame->format),
  141. output_frame->width, output_frame->height, output_frame->pts);
  142. return ff_filter_frame(outlink, output_frame);
  143. fail:
  144. av_frame_free(&input_frame);
  145. av_frame_free(&output_frame);
  146. return err;
  147. }
  148. static av_cold int procamp_vaapi_init(AVFilterContext *avctx)
  149. {
  150. VAAPIVPPContext *vpp_ctx = avctx->priv;
  151. ff_vaapi_vpp_ctx_init(avctx);
  152. vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
  153. vpp_ctx->build_filter_params = procamp_vaapi_build_filter_params;
  154. vpp_ctx->output_format = AV_PIX_FMT_NONE;
  155. return 0;
  156. }
  157. #define OFFSET(x) offsetof(ProcampVAAPIContext, x)
  158. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
  159. static const AVOption procamp_vaapi_options[] = {
  160. { "b", "Output video brightness",
  161. OFFSET(bright), AV_OPT_TYPE_FLOAT, { .dbl = BRIGHTNESS_DEFAULT }, BRIGHTNESS_MIN, BRIGHTNESS_MAX, .flags = FLAGS },
  162. { "brightness", "Output video brightness",
  163. OFFSET(bright), AV_OPT_TYPE_FLOAT, { .dbl = BRIGHTNESS_DEFAULT }, BRIGHTNESS_MIN, BRIGHTNESS_MAX, .flags = FLAGS },
  164. { "s", "Output video saturation",
  165. OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = SATURATION_DEFAULT }, SATURATION_MIN, SATURATION_MAX, .flags = FLAGS },
  166. { "saturatio", "Output video saturation",
  167. OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = SATURATION_DEFAULT }, SATURATION_MIN, SATURATION_MAX, .flags = FLAGS },
  168. { "c", "Output video contrast",
  169. OFFSET(contrast), AV_OPT_TYPE_FLOAT, { .dbl = CONTRAST_DEFAULT }, CONTRAST_MIN, CONTRAST_MAX, .flags = FLAGS },
  170. { "contrast", "Output video contrast",
  171. OFFSET(contrast), AV_OPT_TYPE_FLOAT, { .dbl = CONTRAST_DEFAULT }, CONTRAST_MIN, CONTRAST_MAX, .flags = FLAGS },
  172. { "h", "Output video hue",
  173. OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = HUE_DEFAULT }, HUE_MIN, HUE_MAX, .flags = FLAGS },
  174. { "hue", "Output video hue",
  175. OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = HUE_DEFAULT }, HUE_MIN, HUE_MAX, .flags = FLAGS },
  176. { NULL },
  177. };
  178. AVFILTER_DEFINE_CLASS(procamp_vaapi);
  179. static const AVFilterPad procamp_vaapi_inputs[] = {
  180. {
  181. .name = "default",
  182. .type = AVMEDIA_TYPE_VIDEO,
  183. .filter_frame = &procamp_vaapi_filter_frame,
  184. .config_props = &ff_vaapi_vpp_config_input,
  185. },
  186. { NULL }
  187. };
  188. static const AVFilterPad procamp_vaapi_outputs[] = {
  189. {
  190. .name = "default",
  191. .type = AVMEDIA_TYPE_VIDEO,
  192. .config_props = &ff_vaapi_vpp_config_output,
  193. },
  194. { NULL }
  195. };
  196. AVFilter ff_vf_procamp_vaapi = {
  197. .name = "procamp_vaapi",
  198. .description = NULL_IF_CONFIG_SMALL("ProcAmp (color balance) adjustments for hue, saturation, brightness, contrast"),
  199. .priv_size = sizeof(ProcampVAAPIContext),
  200. .init = &procamp_vaapi_init,
  201. .uninit = &ff_vaapi_vpp_ctx_uninit,
  202. .query_formats = &ff_vaapi_vpp_query_formats,
  203. .inputs = procamp_vaapi_inputs,
  204. .outputs = procamp_vaapi_outputs,
  205. .priv_class = &procamp_vaapi_class,
  206. .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
  207. };