vf_vibrance.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "libavutil/imgutils.h"
  22. #include "avfilter.h"
  23. #include "formats.h"
  24. #include "internal.h"
  25. #include "video.h"
  26. typedef struct VibranceContext {
  27. const AVClass *class;
  28. float intensity;
  29. float balance[3];
  30. float lcoeffs[3];
  31. int alternate;
  32. int depth;
  33. int (*do_slice)(AVFilterContext *s, void *arg,
  34. int jobnr, int nb_jobs);
  35. } VibranceContext;
  36. static inline float lerpf(float v0, float v1, float f)
  37. {
  38. return v0 + (v1 - v0) * f;
  39. }
  40. static int vibrance_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  41. {
  42. VibranceContext *s = avctx->priv;
  43. AVFrame *frame = arg;
  44. const int width = frame->width;
  45. const int height = frame->height;
  46. const float scale = 1.f / 255.f;
  47. const float gc = s->lcoeffs[0];
  48. const float bc = s->lcoeffs[1];
  49. const float rc = s->lcoeffs[2];
  50. const float intensity = s->intensity;
  51. const float alternate = s->alternate ? 1.f : -1.f;
  52. const float gintensity = intensity * s->balance[0];
  53. const float bintensity = intensity * s->balance[1];
  54. const float rintensity = intensity * s->balance[2];
  55. const float sgintensity = alternate * FFSIGN(gintensity);
  56. const float sbintensity = alternate * FFSIGN(bintensity);
  57. const float srintensity = alternate * FFSIGN(rintensity);
  58. const int slice_start = (height * jobnr) / nb_jobs;
  59. const int slice_end = (height * (jobnr + 1)) / nb_jobs;
  60. const int glinesize = frame->linesize[0];
  61. const int blinesize = frame->linesize[1];
  62. const int rlinesize = frame->linesize[2];
  63. uint8_t *gptr = frame->data[0] + slice_start * glinesize;
  64. uint8_t *bptr = frame->data[1] + slice_start * blinesize;
  65. uint8_t *rptr = frame->data[2] + slice_start * rlinesize;
  66. for (int y = slice_start; y < slice_end; y++) {
  67. for (int x = 0; x < width; x++) {
  68. float g = gptr[x] * scale;
  69. float b = bptr[x] * scale;
  70. float r = rptr[x] * scale;
  71. float max_color = FFMAX3(r, g, b);
  72. float min_color = FFMIN3(r, g, b);
  73. float color_saturation = max_color - min_color;
  74. float luma = g * gc + r * rc + b * bc;
  75. const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
  76. const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
  77. const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
  78. g = lerpf(luma, g, cg);
  79. b = lerpf(luma, b, cb);
  80. r = lerpf(luma, r, cr);
  81. gptr[x] = av_clip_uint8(g * 255.f);
  82. bptr[x] = av_clip_uint8(b * 255.f);
  83. rptr[x] = av_clip_uint8(r * 255.f);
  84. }
  85. gptr += glinesize;
  86. bptr += blinesize;
  87. rptr += rlinesize;
  88. }
  89. return 0;
  90. }
  91. static int vibrance_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
  92. {
  93. VibranceContext *s = avctx->priv;
  94. AVFrame *frame = arg;
  95. const int depth = s->depth;
  96. const float max = (1 << depth) - 1;
  97. const float scale = 1.f / max;
  98. const float gc = s->lcoeffs[0];
  99. const float bc = s->lcoeffs[1];
  100. const float rc = s->lcoeffs[2];
  101. const int width = frame->width;
  102. const int height = frame->height;
  103. const float intensity = s->intensity;
  104. const float alternate = s->alternate ? 1.f : -1.f;
  105. const float gintensity = intensity * s->balance[0];
  106. const float bintensity = intensity * s->balance[1];
  107. const float rintensity = intensity * s->balance[2];
  108. const float sgintensity = alternate * FFSIGN(gintensity);
  109. const float sbintensity = alternate * FFSIGN(bintensity);
  110. const float srintensity = alternate * FFSIGN(rintensity);
  111. const int slice_start = (height * jobnr) / nb_jobs;
  112. const int slice_end = (height * (jobnr + 1)) / nb_jobs;
  113. const int glinesize = frame->linesize[0] / 2;
  114. const int blinesize = frame->linesize[1] / 2;
  115. const int rlinesize = frame->linesize[2] / 2;
  116. uint16_t *gptr = (uint16_t *)frame->data[0] + slice_start * glinesize;
  117. uint16_t *bptr = (uint16_t *)frame->data[1] + slice_start * blinesize;
  118. uint16_t *rptr = (uint16_t *)frame->data[2] + slice_start * rlinesize;
  119. for (int y = slice_start; y < slice_end; y++) {
  120. for (int x = 0; x < width; x++) {
  121. float g = gptr[x] * scale;
  122. float b = bptr[x] * scale;
  123. float r = rptr[x] * scale;
  124. float max_color = FFMAX3(r, g, b);
  125. float min_color = FFMIN3(r, g, b);
  126. float color_saturation = max_color - min_color;
  127. float luma = g * gc + r * rc + b * bc;
  128. const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
  129. const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
  130. const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
  131. g = lerpf(luma, g, cg);
  132. b = lerpf(luma, b, cb);
  133. r = lerpf(luma, r, cr);
  134. gptr[x] = av_clip_uintp2_c(g * max, depth);
  135. bptr[x] = av_clip_uintp2_c(b * max, depth);
  136. rptr[x] = av_clip_uintp2_c(r * max, depth);
  137. }
  138. gptr += glinesize;
  139. bptr += blinesize;
  140. rptr += rlinesize;
  141. }
  142. return 0;
  143. }
  144. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  145. {
  146. AVFilterContext *avctx = link->dst;
  147. VibranceContext *s = avctx->priv;
  148. int res;
  149. if (res = avctx->internal->execute(avctx, s->do_slice, frame, NULL,
  150. FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
  151. return res;
  152. return ff_filter_frame(avctx->outputs[0], frame);
  153. }
  154. static av_cold int query_formats(AVFilterContext *avctx)
  155. {
  156. static const enum AVPixelFormat pixel_fmts[] = {
  157. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  158. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
  159. AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  160. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  161. AV_PIX_FMT_NONE
  162. };
  163. AVFilterFormats *formats = NULL;
  164. formats = ff_make_format_list(pixel_fmts);
  165. if (!formats)
  166. return AVERROR(ENOMEM);
  167. return ff_set_common_formats(avctx, formats);
  168. }
  169. static av_cold int config_input(AVFilterLink *inlink)
  170. {
  171. AVFilterContext *avctx = inlink->dst;
  172. VibranceContext *s = avctx->priv;
  173. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  174. s->depth = desc->comp[0].depth;
  175. s->do_slice = s->depth <= 8 ? vibrance_slice8 : vibrance_slice16;
  176. return 0;
  177. }
  178. static const AVFilterPad vibrance_inputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .needs_writable = 1,
  183. .filter_frame = filter_frame,
  184. .config_props = config_input,
  185. },
  186. { NULL }
  187. };
  188. static const AVFilterPad vibrance_outputs[] = {
  189. {
  190. .name = "default",
  191. .type = AVMEDIA_TYPE_VIDEO,
  192. },
  193. { NULL }
  194. };
  195. #define OFFSET(x) offsetof(VibranceContext, x)
  196. #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  197. static const AVOption vibrance_options[] = {
  198. { "intensity", "set the intensity value", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0}, -2, 2, VF },
  199. { "rbal", "set the red balance value", OFFSET(balance[2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
  200. { "gbal", "set the green balance value", OFFSET(balance[0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
  201. { "bbal", "set the blue balance value", OFFSET(balance[1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
  202. { "rlum", "set the red luma coefficient", OFFSET(lcoeffs[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.072186}, 0, 1, VF },
  203. { "glum", "set the green luma coefficient", OFFSET(lcoeffs[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.715158}, 0, 1, VF },
  204. { "blum", "set the blue luma coefficient", OFFSET(lcoeffs[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.212656}, 0, 1, VF },
  205. { "alternate", "use alternate colors", OFFSET(alternate), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
  206. { NULL }
  207. };
  208. AVFILTER_DEFINE_CLASS(vibrance);
  209. AVFilter ff_vf_vibrance = {
  210. .name = "vibrance",
  211. .description = NULL_IF_CONFIG_SMALL("Boost or alter saturation."),
  212. .priv_size = sizeof(VibranceContext),
  213. .priv_class = &vibrance_class,
  214. .query_formats = query_formats,
  215. .inputs = vibrance_inputs,
  216. .outputs = vibrance_outputs,
  217. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  218. };