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.

255 lines
8.1KB

  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer
  3. * Copyright (c) 2012 Jeremy Tran
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @file
  23. * Apply a hue/saturation filter to the input video
  24. * Ported from MPlayer libmpcodecs/vf_hue.c.
  25. */
  26. #include <float.h>
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. #define HUE_DEFAULT_VAL 0
  35. #define SAT_DEFAULT_VAL 1
  36. typedef struct {
  37. const AVClass *class;
  38. float hue_deg; /* hue expressed in degrees */
  39. float hue; /* hue expressed in radians */
  40. float saturation;
  41. int hsub;
  42. int vsub;
  43. int32_t hue_sin;
  44. int32_t hue_cos;
  45. } HueContext;
  46. #define OFFSET(x) offsetof(HueContext, x)
  47. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  48. static const AVOption hue_options[] = {
  49. { "h", "set the hue angle degrees", OFFSET(hue_deg), AV_OPT_TYPE_FLOAT,
  50. { -FLT_MAX }, -FLT_MAX, FLT_MAX, FLAGS },
  51. { "H", "set the hue angle radians", OFFSET(hue), AV_OPT_TYPE_FLOAT,
  52. { -FLT_MAX }, -FLT_MAX, FLT_MAX, FLAGS },
  53. { "s", "set the saturation value", OFFSET(saturation), AV_OPT_TYPE_FLOAT,
  54. { SAT_DEFAULT_VAL }, -10, 10, FLAGS },
  55. { NULL }
  56. };
  57. AVFILTER_DEFINE_CLASS(hue);
  58. static av_cold int init(AVFilterContext *ctx, const char *args)
  59. {
  60. HueContext *hue = ctx->priv;
  61. int n, ret;
  62. char c1 = 0, c2 = 0;
  63. char *equal;
  64. hue->class = &hue_class;
  65. av_opt_set_defaults(hue);
  66. if (args) {
  67. /* named options syntax */
  68. if (equal = strchr(args, '=')) {
  69. if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
  70. return ret;
  71. if (hue->hue != -FLT_MAX && hue->hue_deg != -FLT_MAX) {
  72. av_log(ctx, AV_LOG_ERROR,
  73. "H and h options are incompatible and cannot be specified "
  74. "at the same time\n");
  75. return AVERROR(EINVAL);
  76. }
  77. /* compatibility h:s syntax */
  78. } else {
  79. n = sscanf(args, "%f%c%f%c", &hue->hue_deg, &c1, &hue->saturation, &c2);
  80. if (n != 1 && (n != 3 || c1 != ':')) {
  81. av_log(ctx, AV_LOG_ERROR,
  82. "Invalid syntax for argument '%s': "
  83. "must be in the form 'hue[:saturation]'\n", args);
  84. return AVERROR(EINVAL);
  85. }
  86. if (hue->saturation < -10 || hue->saturation > 10) {
  87. av_log(ctx, AV_LOG_ERROR,
  88. "Invalid value for saturation %0.1f: "
  89. "must be included between range -10 and +10\n", hue->saturation);
  90. return AVERROR(EINVAL);
  91. }
  92. }
  93. }
  94. if (hue->saturation == -FLT_MAX)
  95. hue->hue = SAT_DEFAULT_VAL;
  96. if (hue->hue == -FLT_MAX)
  97. hue->hue = HUE_DEFAULT_VAL;
  98. if (hue->hue_deg != -FLT_MAX)
  99. /* Convert angle from degrees to radians */
  100. hue->hue = hue->hue_deg * M_PI / 180;
  101. av_log(ctx, AV_LOG_VERBOSE, "hue:%f*PI hue_deg:%f saturation:%f\n",
  102. hue->hue/M_PI, hue->hue*180/M_PI, hue->saturation);
  103. return 0;
  104. }
  105. static av_cold void uninit(AVFilterContext *ctx)
  106. {
  107. HueContext *hue = ctx->priv;
  108. av_opt_free(hue);
  109. }
  110. static int query_formats(AVFilterContext *ctx)
  111. {
  112. static const enum PixelFormat pix_fmts[] = {
  113. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  114. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  115. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  116. PIX_FMT_YUVA420P,
  117. PIX_FMT_NONE
  118. };
  119. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  120. return 0;
  121. }
  122. static int config_props(AVFilterLink *inlink)
  123. {
  124. HueContext *hue = inlink->dst->priv;
  125. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
  126. hue->hsub = desc->log2_chroma_w;
  127. hue->vsub = desc->log2_chroma_h;
  128. /*
  129. * Scale the value to the norm of the resulting (U,V) vector, that is
  130. * the saturation.
  131. * This will be useful in the process_chrominance function.
  132. */
  133. hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
  134. hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
  135. return 0;
  136. }
  137. static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_linesize,
  138. uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
  139. int w, int h,
  140. const int32_t c, const int32_t s)
  141. {
  142. int32_t u, v, new_u, new_v;
  143. int i;
  144. /*
  145. * If we consider U and V as the components of a 2D vector then its angle
  146. * is the hue and the norm is the saturation
  147. */
  148. while (h--) {
  149. for (i = 0; i < w; i++) {
  150. /* Normalize the components from range [16;140] to [-112;112] */
  151. u = usrc[i] - 128;
  152. v = vsrc[i] - 128;
  153. /*
  154. * Apply the rotation of the vector : (c * u) - (s * v)
  155. * (s * u) + (c * v)
  156. * De-normalize the components (without forgetting to scale 128
  157. * by << 16)
  158. * Finally scale back the result by >> 16
  159. */
  160. new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
  161. new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
  162. /* Prevent a potential overflow */
  163. udst[i] = av_clip_uint8_c(new_u);
  164. vdst[i] = av_clip_uint8_c(new_v);
  165. }
  166. usrc += src_linesize;
  167. vsrc += src_linesize;
  168. udst += dst_linesize;
  169. vdst += dst_linesize;
  170. }
  171. }
  172. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  173. {
  174. HueContext *hue = inlink->dst->priv;
  175. AVFilterBufferRef *inpic = inlink->cur_buf;
  176. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  177. uint8_t *inrow[3], *outrow[3]; // 0 : Y, 1 : U, 2 : V
  178. int plane;
  179. inrow[0] = inpic->data[0] + y * inpic->linesize[0];
  180. outrow[0] = outpic->data[0] + y * outpic->linesize[0];
  181. for (plane = 1; plane < 3; plane++) {
  182. inrow[plane] = inpic->data[plane] + (y >> hue->vsub) * inpic->linesize[plane];
  183. outrow[plane] = outpic->data[plane] + (y >> hue->vsub) * outpic->linesize[plane];
  184. }
  185. av_image_copy_plane(outrow[0], outpic->linesize[0],
  186. inrow[0], inpic->linesize[0],
  187. inlink->w, inlink->h);
  188. process_chrominance(outrow[1], outrow[2], outpic->linesize[1],
  189. inrow[1], inrow[2], inpic->linesize[1],
  190. inlink->w >> hue->hsub, inlink->h >> hue->vsub,
  191. hue->hue_cos, hue->hue_sin);
  192. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  193. }
  194. AVFilter avfilter_vf_hue = {
  195. .name = "hue",
  196. .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
  197. .priv_size = sizeof(HueContext),
  198. .init = init,
  199. .uninit = uninit,
  200. .query_formats = query_formats,
  201. .inputs = (const AVFilterPad[]) {
  202. {
  203. .name = "default",
  204. .type = AVMEDIA_TYPE_VIDEO,
  205. .draw_slice = draw_slice,
  206. .config_props = config_props,
  207. .min_perms = AV_PERM_READ,
  208. },
  209. { .name = NULL }
  210. },
  211. .outputs = (const AVFilterPad[]) {
  212. {
  213. .name = "default",
  214. .type = AVMEDIA_TYPE_VIDEO,
  215. },
  216. { .name = NULL }
  217. },
  218. .priv_class = &hue_class,
  219. };