FFmpeg
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 <stdlib.h>
22 
23 #include "bsf.h"
24 #include "bsf_internal.h"
25 
26 #include "libavutil/log.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/eval.h"
30 
31 static const char *const var_names[] = {
32  "n", ///< packet index, starting from zero
33  "tb", ///< timebase
34  "pts", ///< packet presentation timestamp
35  "dts", ///< packet decoding timestamp
36  "nopts", ///< AV_NOPTS_VALUE
37  "startpts", ///< first seen non-AV_NOPTS_VALUE packet timestamp
38  "startdts", ///< first seen non-AV_NOPTS_VALUE packet timestamp
39  "duration", "d", ///< packet duration
40  "pos", ///< original position of packet in its source
41  "size", ///< packet size
42  "key" , ///< packet keyframe flag
43  "state", ///< random-ish state
44  NULL
45 };
46 
47 enum var_name {
61 };
62 
63 typedef struct NoiseContext {
64  const AVClass *class;
65 
66  char *amount_str;
67  char *drop_str;
69 
72 
74 
75  unsigned int state;
76  unsigned int pkt_idx;
77 } NoiseContext;
78 
80 {
82  int ret;
83 
84  if (!s->amount_str) {
85  s->amount_str = (!s->drop_str && !s->dropamount) ? av_strdup("-1") : av_strdup("0");
86  if (!s->amount_str)
87  return AVERROR(ENOMEM);
88  }
89 
90  if (ctx->par_in->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME &&
91  strcmp(s->amount_str, "0")) {
92  av_log(ctx, AV_LOG_ERROR, "Wrapped AVFrame noising is unsupported\n");
93  return AVERROR_PATCHWELCOME;
94  }
95 
96  ret = av_expr_parse(&s->amount_pexpr, s->amount_str,
97  var_names, NULL, NULL, NULL, NULL, 0, ctx);
98  if (ret < 0) {
99  av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for amount: %s\n", s->amount_str);
100  return ret;
101  }
102 
103  if (s->drop_str && s->dropamount) {
104  av_log(ctx, AV_LOG_WARNING, "Both drop '%s' and dropamount=%d set. Ignoring dropamount.\n",
105  s->drop_str, s->dropamount);
106  s->dropamount = 0;
107  }
108 
109  if (s->drop_str) {
110  ret = av_expr_parse(&s->drop_pexpr, s->drop_str,
111  var_names, NULL, NULL, NULL, NULL, 0, ctx);
112  if (ret < 0) {
113  av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for drop: %s\n", s->drop_str);
114  return ret;
115  }
116  }
117 
118  s->var_values[VAR_TB] = ctx->time_base_out.den ? av_q2d(ctx->time_base_out) : 0;
119  s->var_values[VAR_NOPTS] = AV_NOPTS_VALUE;
120  s->var_values[VAR_STARTPTS] = AV_NOPTS_VALUE;
121  s->var_values[VAR_STARTDTS] = AV_NOPTS_VALUE;
122  s->var_values[VAR_STATE] = 0;
123 
124  return 0;
125 }
126 
128 {
130  int i, ret, amount, drop = 0;
131  double res;
132 
134  if (ret < 0)
135  return ret;
136 
137  s->var_values[VAR_N] = s->pkt_idx++;
138  s->var_values[VAR_PTS] = pkt->pts;
139  s->var_values[VAR_DTS] = pkt->dts;
140  s->var_values[VAR_DURATION] =
141  s->var_values[VAR_D] = pkt->duration;
142  s->var_values[VAR_SIZE] = pkt->size;
143  s->var_values[VAR_KEY] = !!(pkt->flags & AV_PKT_FLAG_KEY);
144  s->var_values[VAR_POS] = pkt->pos;
145 
146  if (s->var_values[VAR_STARTPTS] == AV_NOPTS_VALUE)
147  s->var_values[VAR_STARTPTS] = pkt->pts;
148 
149  if (s->var_values[VAR_STARTDTS] == AV_NOPTS_VALUE)
150  s->var_values[VAR_STARTDTS] = pkt->dts;
151 
152  res = av_expr_eval(s->amount_pexpr, s->var_values, NULL);
153 
154  if (isnan(res))
155  amount = 0;
156  else if (res < 0)
157  amount = (s->state % 10001 + 1);
158  else
159  amount = (int)res;
160 
161  if (s->drop_str) {
162  res = av_expr_eval(s->drop_pexpr, s->var_values, NULL);
163 
164  if (isnan(res))
165  drop = 0;
166  else if (res < 0)
167  drop = !(s->state % FFABS((int)res));
168  else
169  drop = !!res;
170  }
171 
172  if(s->dropamount) {
173  drop = !(s->state % s->dropamount);
174  }
175 
176  av_log(ctx, AV_LOG_VERBOSE, "Stream #%d packet %d pts %"PRId64" - amount %d drop %d\n",
177  pkt->stream_index, (unsigned int)s->var_values[VAR_N], pkt->pts, amount, drop);
178 
179  if (drop) {
180  s->var_values[VAR_STATE] = ++s->state;
182  return AVERROR(EAGAIN);
183  }
184 
185  if (amount) {
187  if (ret < 0) {
189  return ret;
190  }
191  }
192 
193  for (i = 0; i < pkt->size; i++) {
194  s->state += pkt->data[i] + 1;
195  if (amount && s->state % amount == 0)
196  pkt->data[i] = s->state;
197  }
198 
199  s->var_values[VAR_STATE] = s->state;
200 
201  return 0;
202 }
203 
204 static void noise_close(AVBSFContext *bsf)
205 {
206  NoiseContext *s = bsf->priv_data;
207 
208  av_expr_free(s->amount_pexpr);
209  av_expr_free(s->drop_pexpr);
210  s->amount_pexpr = s->drop_pexpr = NULL;
211 }
212 
213 #define OFFSET(x) offsetof(NoiseContext, x)
214 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_BSF_PARAM)
215 static const AVOption options[] = {
216  { "amount", NULL, OFFSET(amount_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
217  { "drop", NULL, OFFSET(drop_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
218  { "dropamount", NULL, OFFSET(dropamount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
219  { NULL },
220 };
221 
222 static const AVClass noise_class = {
223  .class_name = "noise",
224  .item_name = av_default_item_name,
225  .option = options,
226  .version = LIBAVUTIL_VERSION_INT,
227 };
228 
230  .p.name = "noise",
231  .p.priv_class = &noise_class,
232  .priv_data_size = sizeof(NoiseContext),
233  .init = noise_init,
234  .close = noise_close,
235  .filter = noise,
236 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:430
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
bsf_internal.h
var_name
var_name
Definition: noise.c:47
noise_class
static const AVClass noise_class
Definition: noise.c:222
AVBitStreamFilter::name
const char * name
Definition: bsf.h:112
noise_close
static void noise_close(AVBSFContext *bsf)
Definition: noise.c:204
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVOption
AVOption.
Definition: opt.h:429
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
filter
void(* filter)(uint8_t *src, int stride, int qscale)
Definition: h263dsp.c:29
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
ff_noise_bsf
const FFBitStreamFilter ff_noise_bsf
Definition: noise.c:229
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
NoiseContext::drop_pexpr
AVExpr * drop_pexpr
Definition: noise.c:71
av_expr_parse
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:710
VAR_KEY
@ VAR_KEY
Definition: noise.c:58
bsf.h
VAR_DURATION
@ VAR_DURATION
Definition: noise.c:55
NoiseContext::state
unsigned int state
Definition: noise.c:75
noise
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition: noise.c:127
VAR_TB
@ VAR_TB
Definition: noise.c:49
NoiseContext
Definition: noise.c:63
av_expr_free
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:358
noise_init
static int noise_init(AVBSFContext *ctx)
Definition: noise.c:79
VAR_NOPTS
@ VAR_NOPTS
Definition: noise.c:52
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
OFFSET
#define OFFSET(x)
Definition: noise.c:213
s
#define s(width, name)
Definition: cbs_vp9.c:198
var_names
static const char *const var_names[]
Definition: noise.c:31
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
FLAGS
#define FLAGS
Definition: noise.c:214
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:792
AVExpr
Definition: eval.c:158
VAR_N
@ VAR_N
Definition: noise.c:48
AV_CODEC_ID_WRAPPED_AVFRAME
@ AV_CODEC_ID_WRAPPED_AVFRAME
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: codec_id.h:609
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
VAR_POS
@ VAR_POS
Definition: noise.c:56
FFBitStreamFilter
Definition: bsf_internal.h:27
isnan
#define isnan(x)
Definition: libm.h:340
NoiseContext::dropamount
int dropamount
Definition: noise.c:68
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
options
Definition: swscale.c:42
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition: bsf_internal.h:31
VAR_STARTPTS
@ VAR_STARTPTS
Definition: noise.c:53
eval.h
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AVPacket::size
int size
Definition: packet.h:540
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:538
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
VAR_VARS_NB
@ VAR_VARS_NB
Definition: noise.c:60
VAR_STARTDTS
@ VAR_STARTDTS
Definition: noise.c:54
VAR_PTS
@ VAR_PTS
Definition: noise.c:50
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
NoiseContext::amount_pexpr
AVExpr * amount_pexpr
Definition: noise.c:70
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:83
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
VAR_SIZE
@ VAR_SIZE
Definition: noise.c:57
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
av_packet_make_writable
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:512
NoiseContext::amount_str
char * amount_str
Definition: noise.c:66
AVPacket::stream_index
int stream_index
Definition: packet.h:541
NoiseContext::var_values
double var_values[VAR_VARS_NB]
Definition: noise.c:73
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
VAR_D
@ VAR_D
Definition: noise.c:55
VAR_STATE
@ VAR_STATE
Definition: noise.c:59
mem.h
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
NoiseContext::drop_str
char * drop_str
Definition: noise.c:67
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:256
options
static const AVOption options[]
Definition: noise.c:215
AV_OPT_TYPE_STRING
@ 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:276
NoiseContext::pkt_idx
unsigned int pkt_idx
Definition: noise.c:76
VAR_DTS
@ VAR_DTS
Definition: noise.c:51
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328