27 #include "libavutil/opt.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/lfg.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/pixdesc.h"
38 #define MAX_NOISE 5120
39 #define MAX_SHIFT 1024
40 #define MAX_RES (MAX_NOISE-MAX_SHIFT)
42 #define NOISE_UNIFORM 1
43 #define NOISE_TEMPORAL 2
44 #define NOISE_AVERAGED 8
45 #define NOISE_PATTERN 16
73 #define OFFSET(x) offsetof(NoiseContext, x)
74 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
76 #define NOISE_PARAMS(name, x, param) \
77 {#name"_seed", "set component #"#x" noise seed", OFFSET(param.seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, FLAGS}, \
78 {#name"_strength", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
79 {#name"s", "set component #"#x" strength", OFFSET(param.strength), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
80 {#name"_flags", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
81 {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
82 {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"}, \
83 {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN}, 0, 0, FLAGS, #name"_flags"}, \
84 {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"}, \
85 {"u", "uniform noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM}, 0, 0, FLAGS, #name"_flags"},
98 static const int8_t
patt[4] = { -1, 0, 1, 0 };
100 #define RAND_N(range) ((int) ((double) range * av_lfg_get(lfg) / (UINT_MAX + 1.0)))
115 for (i = 0, j = 0; i <
MAX_NOISE; i++, j++) {
119 noise[i] = (
RAND_N(strength) - strength / 2) / 6
120 +
patt[j % 4] * strength * 0.25 / 3;
122 noise[i] = (
RAND_N(strength) - strength / 2) / 3;
126 noise[i] = (
RAND_N(strength) - strength / 2) / 2
127 +
patt[j % 4] * strength * 0.25;
129 noise[i] =
RAND_N(strength) - strength / 2;
133 double x1, x2, w, y1;
135 x1 = 2.0 *
av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
136 x2 = 2.0 *
av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
137 w = x1 * x1 + x2 * x2;
140 w = sqrt((-2.0 * log(w)) / w);
142 y1 *= strength / sqrt(3.0);
145 y1 +=
patt[j % 4] * strength * 0.35;
147 y1 = av_clipf(y1, -128, 127);
157 for (j = 0; j < 3; j++)
208 for (i = 0; i <
len; i++) {
209 int v = src[i] + noise[i];
211 dst[i] = av_clip_uint8(v);
215 #define ASMALIGN(ZEROBITS) ".p2align " #ZEROBITS "\n\t"
225 "mov %3, %%"REG_a
" \n\t"
226 "pcmpeqb %%mm7, %%mm7 \n\t"
227 "psllw $15, %%mm7 \n\t"
228 "packsswb %%mm7, %%mm7 \n\t"
231 "movq (%0, %%"REG_a
"), %%mm0 \n\t"
232 "movq (%1, %%"REG_a
"), %%mm1 \n\t"
233 "pxor %%mm7, %%mm0 \n\t"
234 "paddsb %%mm1, %%mm0 \n\t"
235 "pxor %%mm7, %%mm0 \n\t"
236 "movq %%mm0, (%2, %%"REG_a
") \n\t"
237 "add $8, %%"REG_a
" \n\t"
239 ::
"r" (src+mmx_len),
"r" (noise+mmx_len),
"r" (dst+mmx_len),
"g" (-mmx_len)
243 line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
250 #if HAVE_MMXEXT_INLINE
255 "mov %3, %%"REG_a
" \n\t"
256 "pcmpeqb %%mm7, %%mm7 \n\t"
257 "psllw $15, %%mm7 \n\t"
258 "packsswb %%mm7, %%mm7 \n\t"
261 "movq (%0, %%"REG_a
"), %%mm0 \n\t"
262 "movq (%1, %%"REG_a
"), %%mm1 \n\t"
263 "pxor %%mm7, %%mm0 \n\t"
264 "paddsb %%mm1, %%mm0 \n\t"
265 "pxor %%mm7, %%mm0 \n\t"
266 "movntq %%mm0, (%2, %%"REG_a
") \n\t"
267 "add $8, %%"REG_a
" \n\t"
269 ::
"r" (src+mmx_len),
"r" (noise+mmx_len),
"r" (dst+mmx_len),
"g" (-mmx_len)
273 line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
281 int8_t *src2 = (int8_t*)src;
283 for (i = 0; i <
len; i++) {
284 const int n = shift[0][i] + shift[1][i] + shift[2][i];
285 dst[i] = src2[i] + ((n * src2[i]) >> 7);
296 "mov %5, %%"REG_a
" \n\t"
299 "movq (%1, %%"REG_a
"), %%mm1 \n\t"
300 "movq (%0, %%"REG_a
"), %%mm0 \n\t"
301 "paddb (%2, %%"REG_a
"), %%mm1 \n\t"
302 "paddb (%3, %%"REG_a
"), %%mm1 \n\t"
303 "movq %%mm0, %%mm2 \n\t"
304 "movq %%mm1, %%mm3 \n\t"
305 "punpcklbw %%mm0, %%mm0 \n\t"
306 "punpckhbw %%mm2, %%mm2 \n\t"
307 "punpcklbw %%mm1, %%mm1 \n\t"
308 "punpckhbw %%mm3, %%mm3 \n\t"
309 "pmulhw %%mm0, %%mm1 \n\t"
310 "pmulhw %%mm2, %%mm3 \n\t"
311 "paddw %%mm1, %%mm1 \n\t"
312 "paddw %%mm3, %%mm3 \n\t"
313 "paddw %%mm0, %%mm1 \n\t"
314 "paddw %%mm2, %%mm3 \n\t"
315 "psrlw $8, %%mm1 \n\t"
316 "psrlw $8, %%mm3 \n\t"
317 "packuswb %%mm3, %%mm1 \n\t"
318 "movq %%mm1, (%4, %%"REG_a
") \n\t"
319 "add $8, %%"REG_a
" \n\t"
321 ::
"r" (src+mmx_len),
"r" (shift[0]+mmx_len),
"r" (shift[1]+mmx_len),
"r" (shift[2]+mmx_len),
322 "r" (dst+mmx_len),
"g" (-mmx_len)
327 int8_t *
shift2[3]={shift[0]+mmx_len, shift[1]+mmx_len, shift[2]+mmx_len};
334 int dst_linesize,
int src_linesize,
349 for (y = start; y <
end; y++) {
350 const int ix = y & (
MAX_RES - 1);
373 for (plane = 0; plane < s->
nb_planes; plane++) {
375 const int start = (height * jobnr ) / nb_jobs;
376 const int end = (height * (jobnr+1)) / nb_jobs;
380 s->
bytewidth[plane], start, end, s, plane);
419 for (i = 0; i < 4; i++) {
430 for (i = 0; i < 4; i++) {
455 for (i = 0; i < 4; i++)
486 .priv_class = &noise_class,
static const AVFilterPad noise_inputs[]
#define AV_CPU_FLAG_MMXEXT
SSE integer functions or AMD MMX ext.
static int shift(int a, int b)
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
const char * name
Filter name.
void * priv
private data for use by the filter
static const AVFilterPad outputs[]
static int config_input(AVFilterLink *inlink)
int h
agreed upon image height
static const AVOption noise_options[]
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
static int query_formats(AVFilterContext *ctx)
static enum AVSampleFormat formats[]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
void(* line_noise)(uint8_t *dst, const uint8_t *src, int8_t *noise, int len, int shift)
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
const char * name
Pad name.
static const AVFilterPad noise_outputs[]
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
static av_cold int end(AVCodecContext *avctx)
static av_cold int init(AVFilterContext *ctx)
#define FF_CEIL_RSHIFT(a, b)
static void noise(uint8_t *dst, const uint8_t *src, int dst_linesize, int src_linesize, int width, int start, int end, NoiseContext *n, int comp)
int nb_threads
Maximum number of threads used by filters in this graph.
static void line_noise_mmx(uint8_t *dst, const uint8_t *src, int8_t *noise, int len, int shift)
#define ASMALIGN(ZEROBITS)
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
A filter pad used for either input or output.
A link between two filters.
uint16_t depth_minus1
number of bits in the component minus 1
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NOISE_PARAMS(name, x, param)
int w
agreed upon image width
static void line_noise_c(uint8_t *dst, const uint8_t *src, int8_t *noise, int len, int shift)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int8_t * prev_shift[MAX_RES][3]
int format
agreed upon media format
Main libavfilter public API header.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
AVFilterLink ** outputs
array of pointers to output links
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static void line_noise_mmxext(uint8_t *dst, const uint8_t *src, int8_t *noise, int len, int shift)
void av_lfg_init(AVLFG *c, unsigned int seed)
#define AV_CPU_FLAG_MMX
standard MMX
typedef void(RENAME(mix_any_func_type))
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static const int8_t patt[4]
BYTE int const BYTE int int int height
Describe the class of an AVClass context structure.
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.
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
static const AVFilterPad inputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
static void line_noise_avg_mmx(uint8_t *dst, const uint8_t *src, int len, int8_t **shift)
static av_cold int init_noise(NoiseContext *n, int comp)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
AVFilterContext * dst
dest filter
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
AVFilter avfilter_vf_noise
static const int shift2[6]
void(* line_noise_avg)(uint8_t *dst, const uint8_t *src, int len, int8_t **shift)
struct AVFilterGraph * graph
filtergraph this filter belongs to
avfilter_execute_func * execute
static void line_noise_avg_c(uint8_t *dst, const uint8_t *src, int len, int8_t **shift)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;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);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define AVFILTER_DEFINE_CLASS(fname)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
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.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define HAVE_MMXEXT_INLINE
static av_cold void uninit(AVFilterContext *ctx)