vf_limiter.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "libavutil/attributes.h"
  19. #include "libavutil/common.h"
  20. #include "libavutil/imgutils.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 "limiter.h"
  27. #include "video.h"
  28. typedef struct ThreadData {
  29. AVFrame *in;
  30. AVFrame *out;
  31. } ThreadData;
  32. typedef struct LimiterContext {
  33. const AVClass *class;
  34. int min;
  35. int max;
  36. int planes;
  37. int nb_planes;
  38. int linesize[4];
  39. int width[4];
  40. int height[4];
  41. LimiterDSPContext dsp;
  42. } LimiterContext;
  43. #define OFFSET(x) offsetof(LimiterContext, x)
  44. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  45. static const AVOption limiter_options[] = {
  46. { "min", "set min value", OFFSET(min), AV_OPT_TYPE_INT, {.i64=0}, 0, 65535, .flags = FLAGS },
  47. { "max", "set max value", OFFSET(max), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, .flags = FLAGS },
  48. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags = FLAGS },
  49. { NULL }
  50. };
  51. AVFILTER_DEFINE_CLASS(limiter);
  52. static av_cold int init(AVFilterContext *ctx)
  53. {
  54. LimiterContext *s = ctx->priv;
  55. if (s->min > s->max)
  56. return AVERROR(EINVAL);
  57. return 0;
  58. }
  59. static int query_formats(AVFilterContext *ctx)
  60. {
  61. static const enum AVPixelFormat pix_fmts[] = {
  62. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  63. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  64. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  65. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  66. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  67. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  68. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  69. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  70. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  71. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  72. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  73. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  74. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  75. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  76. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  77. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  78. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  79. AV_PIX_FMT_NONE
  80. };
  81. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  82. if (!fmts_list)
  83. return AVERROR(ENOMEM);
  84. return ff_set_common_formats(ctx, fmts_list);
  85. }
  86. static void limiter8(const uint8_t *src, uint8_t *dst,
  87. ptrdiff_t slinesize, ptrdiff_t dlinesize,
  88. int w, int h, int min, int max)
  89. {
  90. int x, y;
  91. for (y = 0; y < h; y++) {
  92. for (x = 0; x < w; x++) {
  93. dst[x] = av_clip(src[x], min, max);
  94. }
  95. dst += dlinesize;
  96. src += slinesize;
  97. }
  98. }
  99. static void limiter16(const uint8_t *ssrc, uint8_t *ddst,
  100. ptrdiff_t slinesize, ptrdiff_t dlinesize,
  101. int w, int h, int min, int max)
  102. {
  103. const uint16_t *src = (const uint16_t *)ssrc;
  104. uint16_t *dst = (uint16_t *)ddst;
  105. int x, y;
  106. dlinesize /= 2;
  107. slinesize /= 2;
  108. for (y = 0; y < h; y++) {
  109. for (x = 0; x < w; x++) {
  110. dst[x] = av_clip(src[x], min, max);
  111. }
  112. dst += dlinesize;
  113. src += slinesize;
  114. }
  115. }
  116. static int config_props(AVFilterLink *inlink)
  117. {
  118. AVFilterContext *ctx = inlink->dst;
  119. LimiterContext *s = ctx->priv;
  120. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  121. int depth, vsub, hsub, ret;
  122. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  123. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  124. return ret;
  125. depth = desc->comp[0].depth;
  126. hsub = desc->log2_chroma_w;
  127. vsub = desc->log2_chroma_h;
  128. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  129. s->height[0] = s->height[3] = inlink->h;
  130. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  131. s->width[0] = s->width[3] = inlink->w;
  132. s->max = FFMIN(s->max, (1 << depth) - 1);
  133. s->min = FFMIN(s->min, (1 << depth) - 1);
  134. if (depth == 8) {
  135. s->dsp.limiter = limiter8;
  136. } else {
  137. s->dsp.limiter = limiter16;
  138. }
  139. if (ARCH_X86)
  140. ff_limiter_init_x86(&s->dsp, desc->comp[0].depth);
  141. return 0;
  142. }
  143. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  144. {
  145. LimiterContext *s = ctx->priv;
  146. ThreadData *td = arg;
  147. AVFrame *in = td->in;
  148. AVFrame *out = td->out;
  149. int p;
  150. for (p = 0; p < s->nb_planes; p++) {
  151. const int h = s->height[p];
  152. const int slice_start = (h * jobnr) / nb_jobs;
  153. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  154. if (!((1 << p) & s->planes)) {
  155. if (out != in)
  156. av_image_copy_plane(out->data[p] + slice_start * out->linesize[p],
  157. out->linesize[p],
  158. in->data[p] + slice_start * in->linesize[p],
  159. in->linesize[p],
  160. s->linesize[p], slice_end - slice_start);
  161. continue;
  162. }
  163. s->dsp.limiter(in->data[p] + slice_start * in->linesize[p],
  164. out->data[p] + slice_start * out->linesize[p],
  165. in->linesize[p], out->linesize[p],
  166. s->width[p], slice_end - slice_start,
  167. s->min, s->max);
  168. }
  169. return 0;
  170. }
  171. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  172. {
  173. AVFilterContext *ctx = inlink->dst;
  174. LimiterContext *s = ctx->priv;
  175. AVFilterLink *outlink = ctx->outputs[0];
  176. ThreadData td;
  177. AVFrame *out;
  178. if (av_frame_is_writable(in)) {
  179. out = in;
  180. } else {
  181. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  182. if (!out) {
  183. av_frame_free(&in);
  184. return AVERROR(ENOMEM);
  185. }
  186. av_frame_copy_props(out, in);
  187. }
  188. td.out = out;
  189. td.in = in;
  190. ctx->internal->execute(ctx, filter_slice, &td, NULL,
  191. FFMIN(s->height[2], ff_filter_get_nb_threads(ctx)));
  192. if (out != in)
  193. av_frame_free(&in);
  194. return ff_filter_frame(outlink, out);
  195. }
  196. static const AVFilterPad inputs[] = {
  197. {
  198. .name = "default",
  199. .type = AVMEDIA_TYPE_VIDEO,
  200. .filter_frame = filter_frame,
  201. .config_props = config_props,
  202. },
  203. { NULL }
  204. };
  205. static const AVFilterPad outputs[] = {
  206. {
  207. .name = "default",
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. },
  210. { NULL }
  211. };
  212. AVFilter ff_vf_limiter = {
  213. .name = "limiter",
  214. .description = NULL_IF_CONFIG_SMALL("Limit pixels components to the specified range."),
  215. .priv_size = sizeof(LimiterContext),
  216. .priv_class = &limiter_class,
  217. .init = init,
  218. .query_formats = query_formats,
  219. .inputs = inputs,
  220. .outputs = outputs,
  221. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  222. };