FFmpeg
Loading...
Searching...
No Matches
noise.c
Go to the documentation of this file.
1/*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stddef.h>
22
23#include "libavcodec/bsf.h"
25
26#include "libavutil/log.h"
27#include "libavutil/opt.h"
28#include "libavutil/eval.h"
29
30static const char *const var_names[] = {
31 "n", ///< packet index, starting from zero
32 "tb", ///< timebase
33 "pts", ///< packet presentation timestamp
34 "dts", ///< packet decoding timestamp
35 "nopts", ///< AV_NOPTS_VALUE
36 "startpts", ///< first seen non-AV_NOPTS_VALUE packet timestamp
37 "startdts", ///< first seen non-AV_NOPTS_VALUE packet timestamp
38 "duration", "d", ///< packet duration
39 "pos", ///< original position of packet in its source
40 "size", ///< packet size
41 "key" , ///< packet keyframe flag
42 "state", ///< random-ish state
43 NULL
44};
45
61
62typedef struct NoiseContext {
63 const AVClass *class;
64
66 char *drop_str;
68
71
73
74 unsigned int state;
75 unsigned int pkt_idx;
77
79{
80 NoiseContext *s = ctx->priv_data;
81 const char *amount_str = s->amount_str;
82 int ret;
83
84 if (!amount_str)
85 amount_str = (!s->drop_str && !s->dropamount) ? "-1" : "0";
86
87 if (ctx->par_in->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME &&
88 strcmp(amount_str, "0")) {
89 av_log(ctx, AV_LOG_ERROR, "Wrapped AVFrame noising is unsupported\n");
91 }
92
93 ret = av_expr_parse(&s->amount_pexpr, amount_str,
94 var_names, NULL, NULL, NULL, NULL, 0, ctx);
95 if (ret < 0) {
96 av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for amount: %s\n", amount_str);
97 return ret;
98 }
99
100 if (s->drop_str && s->dropamount) {
101 av_log(ctx, AV_LOG_WARNING, "Both drop '%s' and dropamount=%d set. Ignoring dropamount.\n",
102 s->drop_str, s->dropamount);
103 s->dropamount = 0;
104 }
105
106 if (s->drop_str) {
107 ret = av_expr_parse(&s->drop_pexpr, s->drop_str,
108 var_names, NULL, NULL, NULL, NULL, 0, ctx);
109 if (ret < 0) {
110 av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for drop: %s\n", s->drop_str);
111 return ret;
112 }
113 }
114
115 s->var_values[VAR_TB] = ctx->time_base_out.den ? av_q2d(ctx->time_base_out) : 0;
116 s->var_values[VAR_NOPTS] = AV_NOPTS_VALUE;
117 s->var_values[VAR_STARTPTS] = AV_NOPTS_VALUE;
118 s->var_values[VAR_STARTDTS] = AV_NOPTS_VALUE;
119 s->var_values[VAR_STATE] = 0;
120
121 return 0;
122}
123
125{
126 NoiseContext *s = ctx->priv_data;
127 int i, ret, amount, drop = 0;
128 double res;
129
131 if (ret < 0)
132 return ret;
133
134 s->var_values[VAR_N] = s->pkt_idx++;
135 s->var_values[VAR_PTS] = pkt->pts;
136 s->var_values[VAR_DTS] = pkt->dts;
137 s->var_values[VAR_DURATION] =
138 s->var_values[VAR_D] = pkt->duration;
139 s->var_values[VAR_SIZE] = pkt->size;
140 s->var_values[VAR_KEY] = !!(pkt->flags & AV_PKT_FLAG_KEY);
141 s->var_values[VAR_POS] = pkt->pos;
142
143 if (s->var_values[VAR_STARTPTS] == AV_NOPTS_VALUE)
144 s->var_values[VAR_STARTPTS] = pkt->pts;
145
146 if (s->var_values[VAR_STARTDTS] == AV_NOPTS_VALUE)
147 s->var_values[VAR_STARTDTS] = pkt->dts;
148
149 res = av_expr_eval(s->amount_pexpr, s->var_values, NULL);
150
151 if (isnan(res))
152 amount = 0;
153 else if (res < 0)
154 amount = (s->state % 10001 + 1);
155 else
156 amount = (int)res;
157
158 if (s->drop_str) {
159 res = av_expr_eval(s->drop_pexpr, s->var_values, NULL);
160
161 if (isnan(res))
162 drop = 0;
163 else if (res < 0)
164 drop = !(s->state % FFABS((int)res));
165 else
166 drop = !!res;
167 }
168
169 if(s->dropamount) {
170 drop = !(s->state % s->dropamount);
171 }
172
173 av_log(ctx, AV_LOG_VERBOSE, "Stream #%d packet %d pts %"PRId64" - amount %d drop %d\n",
174 pkt->stream_index, (unsigned int)s->var_values[VAR_N], pkt->pts, amount, drop);
175
176 if (drop) {
177 s->var_values[VAR_STATE] = ++s->state;
179 return AVERROR(EAGAIN);
180 }
181
182 if (amount) {
184 if (ret < 0) {
186 return ret;
187 }
188 }
189
190 for (i = 0; i < pkt->size; i++) {
191 s->state += pkt->data[i] + 1;
192 if (amount && s->state % amount == 0)
193 pkt->data[i] = s->state;
194 }
195
196 s->var_values[VAR_STATE] = s->state;
197
198 return 0;
199}
200
201static void noise_close(AVBSFContext *bsf)
202{
203 NoiseContext *s = bsf->priv_data;
204
205 av_expr_free(s->amount_pexpr);
206 av_expr_free(s->drop_pexpr);
207 s->amount_pexpr = s->drop_pexpr = NULL;
208}
209
210#define OFFSET(x) offsetof(NoiseContext, x)
211#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_BSF_PARAM)
212static const AVOption options[] = {
213 { "amount", NULL, OFFSET(amount_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
214 { "drop", NULL, OFFSET(drop_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
215 { "dropamount", NULL, OFFSET(dropamount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
216 { NULL },
217};
218
219static const AVClass noise_class = {
220 .class_name = "noise",
221 .item_name = av_default_item_name,
222 .option = options,
223 .version = LIBAVUTIL_VERSION_INT,
224};
225
227 .p.name = "noise",
228 .p.priv_class = &noise_class,
229 .priv_data_size = sizeof(NoiseContext),
230 .init = noise_init,
232 .filter = noise,
233};
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
const FFBitStreamFilter ff_noise_bsf
Definition noise.c:226
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition bsf.c:254
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition eval.c:368
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition eval.c:824
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition eval.c:735
simple arithmetic expression evaluator
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
@ AV_CODEC_ID_WRAPPED_AVFRAME
Passthrough codec, AVFrames wrapped in AVPacket.
Definition codec_id.h:621
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible.
Definition packet.c:516
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define isnan(x)
Definition libm.h:342
var_name
Definition noise.c:46
@ VAR_STARTPTS
Definition noise.c:52
@ VAR_STATE
Definition noise.c:58
@ VAR_PTS
Definition noise.c:49
@ VAR_POS
Definition noise.c:55
@ VAR_KEY
Definition noise.c:57
@ VAR_TB
Definition noise.c:48
@ VAR_NOPTS
Definition noise.c:51
@ VAR_DTS
Definition noise.c:50
@ VAR_N
Definition noise.c:47
@ VAR_VARS_NB
Definition noise.c:59
@ VAR_DURATION
Definition noise.c:54
@ VAR_STARTDTS
Definition noise.c:53
@ VAR_SIZE
Definition noise.c:56
@ VAR_D
Definition noise.c:54
static void noise_close(AVBSFContext *bsf)
Definition noise.c:201
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition noise.c:124
static const AVClass noise_class
Definition noise.c:219
static int noise_init(AVBSFContext *ctx)
Definition noise.c:78
static const char *const var_names[]
Definition noise.c:30
#define OFFSET(x)
Definition noise.c:210
AVOptions.
The bitstream filter state.
Definition bsf.h:68
void * priv_data
Opaque filter-specific private data.
Definition bsf.h:83
Describe the class of an AVClass context structure.
Definition log.h:76
Definition eval.c:171
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
AVExpr * drop_pexpr
Definition noise.c:70
char * amount_str
Definition noise.c:65
double var_values[VAR_VARS_NB]
Definition noise.c:72
AVExpr * amount_pexpr
Definition noise.c:69
unsigned int state
Definition noise.c:74
int dropamount
Definition noise.c:67
char * drop_str
Definition noise.c:66
unsigned int pkt_idx
Definition noise.c:75
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static AVFormatContext * ctx
Definition movenc.c:49