FFmpeg  4.0
vf_limiter.c
Go to the documentation of this file.
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 
19 #include "libavutil/attributes.h"
20 #include "libavutil/common.h"
21 #include "libavutil/eval.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "limiter.h"
29 #include "video.h"
30 
31 typedef struct LimiterContext {
32  const AVClass *class;
33  int min;
34  int max;
35  int planes;
36  int nb_planes;
37  int linesize[4];
38  int width[4];
39  int height[4];
40 
43 
44 #define OFFSET(x) offsetof(LimiterContext, x)
45 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
46 
47 static const AVOption limiter_options[] = {
48  { "min", "set min value", OFFSET(min), AV_OPT_TYPE_INT, {.i64=0}, 0, 65535, .flags = FLAGS },
49  { "max", "set max value", OFFSET(max), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, .flags = FLAGS },
50  { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags = FLAGS },
51  { NULL }
52 };
53 
54 AVFILTER_DEFINE_CLASS(limiter);
55 
57 {
58  LimiterContext *s = ctx->priv;
59 
60  if (s->min > s->max)
61  return AVERROR(EINVAL);
62  return 0;
63 }
64 
66 {
67  static const enum AVPixelFormat pix_fmts[] = {
86  };
87 
88  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
89  if (!fmts_list)
90  return AVERROR(ENOMEM);
91  return ff_set_common_formats(ctx, fmts_list);
92 }
93 
94 static void limiter8(const uint8_t *src, uint8_t *dst,
95  ptrdiff_t slinesize, ptrdiff_t dlinesize,
96  int w, int h, int min, int max)
97 {
98  int x, y;
99 
100  for (y = 0; y < h; y++) {
101  for (x = 0; x < w; x++) {
102  dst[x] = av_clip(src[x], min, max);
103  }
104 
105  dst += dlinesize;
106  src += slinesize;
107  }
108 }
109 
110 static void limiter16(const uint8_t *ssrc, uint8_t *ddst,
111  ptrdiff_t slinesize, ptrdiff_t dlinesize,
112  int w, int h, int min, int max)
113 {
114  const uint16_t *src = (const uint16_t *)ssrc;
115  uint16_t *dst = (uint16_t *)ddst;
116  int x, y;
117 
118  dlinesize /= 2;
119  slinesize /= 2;
120 
121  for (y = 0; y < h; y++) {
122  for (x = 0; x < w; x++) {
123  dst[x] = av_clip(src[x], min, max);
124  }
125 
126  dst += dlinesize;
127  src += slinesize;
128  }
129 }
130 
131 static int config_props(AVFilterLink *inlink)
132 {
133  AVFilterContext *ctx = inlink->dst;
134  LimiterContext *s = ctx->priv;
136  int vsub, hsub, ret;
137 
139 
140  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
141  return ret;
142 
143  hsub = desc->log2_chroma_w;
144  vsub = desc->log2_chroma_h;
145  s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
146  s->height[0] = s->height[3] = inlink->h;
147  s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
148  s->width[0] = s->width[3] = inlink->w;
149 
150  if (desc->comp[0].depth == 8) {
151  s->dsp.limiter = limiter8;
152  s->max = FFMIN(s->max, 255);
153  s->min = FFMIN(s->min, 255);
154  } else {
155  s->dsp.limiter = limiter16;
156  }
157 
158  if (ARCH_X86)
159  ff_limiter_init_x86(&s->dsp, desc->comp[0].depth);
160 
161  return 0;
162 }
163 
164 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
165 {
166  AVFilterContext *ctx = inlink->dst;
167  LimiterContext *s = ctx->priv;
168  AVFilterLink *outlink = ctx->outputs[0];
169  AVFrame *out;
170  int p;
171 
172  if (av_frame_is_writable(in)) {
173  out = in;
174  } else {
175  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
176  if (!out) {
177  av_frame_free(&in);
178  return AVERROR(ENOMEM);
179  }
180  av_frame_copy_props(out, in);
181  }
182 
183  for (p = 0; p < s->nb_planes; p++) {
184  if (!((1 << p) & s->planes)) {
185  if (out != in)
186  av_image_copy_plane(out->data[p], out->linesize[p], in->data[p], in->linesize[p],
187  s->linesize[p], s->height[p]);
188  continue;
189  }
190 
191  s->dsp.limiter(in->data[p], out->data[p],
192  in->linesize[p], out->linesize[p],
193  s->width[p], s->height[p],
194  s->min, s->max);
195  }
196 
197  if (out != in)
198  av_frame_free(&in);
199 
200  return ff_filter_frame(outlink, out);
201 }
202 
203 static const AVFilterPad inputs[] = {
204  {
205  .name = "default",
206  .type = AVMEDIA_TYPE_VIDEO,
207  .filter_frame = filter_frame,
208  .config_props = config_props,
209  },
210  { NULL }
211 };
212 
213 static const AVFilterPad outputs[] = {
214  {
215  .name = "default",
216  .type = AVMEDIA_TYPE_VIDEO,
217  },
218  { NULL }
219 };
220 
222  .name = "limiter",
223  .description = NULL_IF_CONFIG_SMALL("Limit pixels components to the specified range."),
224  .priv_size = sizeof(LimiterContext),
225  .priv_class = &limiter_class,
226  .init = init,
228  .inputs = inputs,
229  .outputs = outputs,
231 };
static av_cold int init(AVFilterContext *ctx)
Definition: vf_limiter.c:56
#define NULL
Definition: coverity.c:32
#define OFFSET(x)
Definition: vf_limiter.c:44
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:407
const char * s
Definition: avisynth_c.h:768
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:401
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2363
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVOption.
Definition: opt.h:246
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:378
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:388
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:404
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2403
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:65
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:164
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:384
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:349
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:372
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
#define src
Definition: vp8dsp.c:254
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_limiter.c:164
Macro definitions for various function/variable attributes.
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:350
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:125
const char * name
Pad name.
Definition: internal.h:60
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:351
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
static void limiter16(const uint8_t *ssrc, uint8_t *ddst, ptrdiff_t slinesize, ptrdiff_t dlinesize, int w, int h, int min, int max)
Definition: vf_limiter.c:110
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:97
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
int linesize[4]
Definition: vf_limiter.c:37
AVOptions.
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:400
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:383
static const AVFilterPad inputs[]
Definition: vf_limiter.c:203
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:96
static int flags
Definition: log.c:55
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:75
void(* limiter)(const uint8_t *src, uint8_t *dst, ptrdiff_t slinesize, ptrdiff_t dlinesize, int w, int h, int min, int max)
Definition: limiter.h:26
int width[4]
Definition: vf_limiter.c:38
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:381
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:373
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:406
static const AVFilterPad outputs[]
Definition: vf_limiter.c:213
A filter pad used for either input or output.
Definition: internal.h:54
LimiterDSPContext dsp
Definition: vf_limiter.c:41
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:172
static const AVOption limiter_options[]
Definition: vf_limiter.c:47
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
AVFilter ff_vf_limiter
Definition: vf_limiter.c:221
#define ARCH_X86
Definition: config.h:38
#define AVERROR(e)
Definition: error.h:43
static int config_props(AVFilterLink *inlink)
Definition: vf_limiter.c:131
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:408
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:389
AVFILTER_DEFINE_CLASS(limiter)
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:371
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:390
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:366
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:387
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:352
#define FFMIN(a, b)
Definition: common.h:96
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:74
uint8_t w
Definition: llviddspenc.c:38
int height[4]
Definition: vf_limiter.c:39
AVFormatContext * ctx
Definition: movenc.c:48
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:405
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:367
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:386
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:379
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:376
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:592
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:173
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:368
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:68
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:89
const char * name
Filter name.
Definition: avfilter.h:148
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:374
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:365
static int query_formats(AVFilterContext *ctx)
Definition: vf_limiter.c:65
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:377
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:385
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:369
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
#define FLAGS
Definition: vf_limiter.c:45
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
common internal and external API header
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:211
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:402
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:76
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
static void limiter8(const uint8_t *src, uint8_t *dst, ptrdiff_t slinesize, ptrdiff_t dlinesize, int w, int h, int min, int max)
Definition: vf_limiter.c:94
A list of supported formats for one end of a filter link.
Definition: formats.h:64
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:254
An instance of a filter.
Definition: avfilter.h:338
FILE * out
Definition: movenc.c:54
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:95
void ff_limiter_init_x86(LimiterDSPContext *dsp, int bpp)
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:338
internal API functions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:380
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:652
simple arithmetic expression evaluator
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58