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.

253 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. static const AVOption hue_options[] = {
  48. { "h", "set the hue angle degrees", OFFSET(hue_deg), AV_OPT_TYPE_FLOAT,
  49. { -FLT_MAX }, -FLT_MAX, FLT_MAX, AV_OPT_FLAG_VIDEO_PARAM },
  50. { "H", "set the hue angle radians", OFFSET(hue), AV_OPT_TYPE_FLOAT,
  51. { -FLT_MAX }, -FLT_MAX, FLT_MAX, AV_OPT_FLAG_VIDEO_PARAM },
  52. { "s", "set the saturation value", OFFSET(saturation), AV_OPT_TYPE_FLOAT,
  53. { SAT_DEFAULT_VAL }, -10, 10, AV_OPT_FLAG_VIDEO_PARAM },
  54. { NULL }
  55. };
  56. AVFILTER_DEFINE_CLASS(hue);
  57. static av_cold int init(AVFilterContext *ctx, const char *args)
  58. {
  59. HueContext *hue = ctx->priv;
  60. int n, ret;
  61. char c1 = 0, c2 = 0;
  62. char *equal;
  63. hue->class = &hue_class;
  64. av_opt_set_defaults(hue);
  65. if (args) {
  66. /* named options syntax */
  67. if (equal = strchr(args, '=')) {
  68. if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
  69. return ret;
  70. if (hue->hue != -FLT_MAX && hue->hue_deg != -FLT_MAX) {
  71. av_log(ctx, AV_LOG_ERROR,
  72. "H and h options are incompatible and cannot be specified "
  73. "at the same time\n");
  74. return AVERROR(EINVAL);
  75. }
  76. /* compatibility h:s syntax */
  77. } else {
  78. n = sscanf(args, "%f%c%f%c", &hue->hue_deg, &c1, &hue->saturation, &c2);
  79. if (n != 1 && (n != 3 || c1 != ':')) {
  80. av_log(ctx, AV_LOG_ERROR,
  81. "Invalid syntax for argument '%s': "
  82. "must be in the form 'hue[:saturation]'\n", args);
  83. return AVERROR(EINVAL);
  84. }
  85. if (hue->saturation < -10 || hue->saturation > 10) {
  86. av_log(ctx, AV_LOG_ERROR,
  87. "Invalid value for saturation %0.1f: "
  88. "must be included between range -10 and +10\n", hue->saturation);
  89. return AVERROR(EINVAL);
  90. }
  91. }
  92. }
  93. if (hue->saturation == -FLT_MAX)
  94. hue->hue = SAT_DEFAULT_VAL;
  95. if (hue->hue == -FLT_MAX)
  96. hue->hue = HUE_DEFAULT_VAL;
  97. if (hue->hue_deg != -FLT_MAX)
  98. /* Convert angle from degrees to radians */
  99. hue->hue = hue->hue_deg * M_PI / 180;
  100. av_log(ctx, AV_LOG_VERBOSE, "hue:%f*PI hue_deg:%f saturation:%f\n",
  101. hue->hue/M_PI, hue->hue*180/M_PI, hue->saturation);
  102. return 0;
  103. }
  104. static av_cold void uninit(AVFilterContext *ctx)
  105. {
  106. HueContext *hue = ctx->priv;
  107. av_opt_free(hue);
  108. }
  109. static int query_formats(AVFilterContext *ctx)
  110. {
  111. static const enum PixelFormat pix_fmts[] = {
  112. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  113. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  114. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  115. PIX_FMT_YUVA420P,
  116. PIX_FMT_NONE
  117. };
  118. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  119. return 0;
  120. }
  121. static int config_props(AVFilterLink *inlink)
  122. {
  123. HueContext *hue = inlink->dst->priv;
  124. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
  125. hue->hsub = desc->log2_chroma_w;
  126. hue->vsub = desc->log2_chroma_h;
  127. /*
  128. * Scale the value to the norm of the resulting (U,V) vector, that is
  129. * the saturation.
  130. * This will be useful in the process_chrominance function.
  131. */
  132. hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
  133. hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
  134. return 0;
  135. }
  136. static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_linesize,
  137. uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
  138. int w, int h,
  139. const int32_t c, const int32_t s)
  140. {
  141. int32_t u, v, new_u, new_v;
  142. int i;
  143. /*
  144. * If we consider U and V as the components of a 2D vector then its angle
  145. * is the hue and the norm is the saturation
  146. */
  147. while (h--) {
  148. for (i = 0; i < w; i++) {
  149. /* Normalize the components from range [16;140] to [-112;112] */
  150. u = usrc[i] - 128;
  151. v = vsrc[i] - 128;
  152. /*
  153. * Apply the rotation of the vector : (c * u) - (s * v)
  154. * (s * u) + (c * v)
  155. * De-normalize the components (without forgetting to scale 128
  156. * by << 16)
  157. * Finally scale back the result by >> 16
  158. */
  159. new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
  160. new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
  161. /* Prevent a potential overflow */
  162. udst[i] = av_clip_uint8_c(new_u);
  163. vdst[i] = av_clip_uint8_c(new_v);
  164. }
  165. usrc += src_linesize;
  166. vsrc += src_linesize;
  167. udst += dst_linesize;
  168. vdst += dst_linesize;
  169. }
  170. }
  171. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  172. {
  173. HueContext *hue = inlink->dst->priv;
  174. AVFilterBufferRef *inpic = inlink->cur_buf;
  175. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  176. uint8_t *inrow[3], *outrow[3]; // 0 : Y, 1 : U, 2 : V
  177. int plane;
  178. inrow[0] = inpic->data[0] + y * inpic->linesize[0];
  179. outrow[0] = outpic->data[0] + y * outpic->linesize[0];
  180. for (plane = 1; plane < 3; plane++) {
  181. inrow[plane] = inpic->data[plane] + (y >> hue->vsub) * inpic->linesize[plane];
  182. outrow[plane] = outpic->data[plane] + (y >> hue->vsub) * outpic->linesize[plane];
  183. }
  184. av_image_copy_plane(outrow[0], outpic->linesize[0],
  185. inrow[0], inpic->linesize[0],
  186. inlink->w, inlink->h);
  187. process_chrominance(outrow[1], outrow[2], outpic->linesize[1],
  188. inrow[1], inrow[2], inpic->linesize[1],
  189. inlink->w >> hue->hsub, inlink->h >> hue->vsub,
  190. hue->hue_cos, hue->hue_sin);
  191. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  192. }
  193. AVFilter avfilter_vf_hue = {
  194. .name = "hue",
  195. .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
  196. .priv_size = sizeof(HueContext),
  197. .init = init,
  198. .uninit = uninit,
  199. .query_formats = query_formats,
  200. .inputs = (const AVFilterPad[]) {
  201. {
  202. .name = "default",
  203. .type = AVMEDIA_TYPE_VIDEO,
  204. .draw_slice = draw_slice,
  205. .config_props = config_props,
  206. .min_perms = AV_PERM_READ,
  207. },
  208. { .name = NULL }
  209. },
  210. .outputs = (const AVFilterPad[]) {
  211. {
  212. .name = "default",
  213. .type = AVMEDIA_TYPE_VIDEO,
  214. },
  215. { .name = NULL }
  216. }
  217. };