FFmpeg
noise_bsf.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/opt.h"
28 #include "libavutil/eval.h"
29 
30 static 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 
46 enum var_name {
60 };
61 
62 typedef struct NoiseContext {
63  const AVClass *class;
64 
65  char *amount_str;
66  char *drop_str;
68 
71 
73 
74  unsigned int state;
75  unsigned int pkt_idx;
76 } NoiseContext;
77 
79 {
81  int ret;
82 
83  if (!s->amount_str) {
84  s->amount_str = (!s->drop_str && !s->dropamount) ? av_strdup("-1") : av_strdup("0");
85  if (!s->amount_str)
86  return AVERROR(ENOMEM);
87  }
88 
89  ret = av_expr_parse(&s->amount_pexpr, s->amount_str,
90  var_names, NULL, NULL, NULL, NULL, 0, ctx);
91  if (ret < 0) {
92  av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for amount: %s\n", s->amount_str);
93  return ret;
94  }
95 
96  if (s->drop_str && s->dropamount) {
97  av_log(ctx, AV_LOG_WARNING, "Both drop '%s' and dropamount=%d set. Ignoring dropamount.\n",
98  s->drop_str, s->dropamount);
99  s->dropamount = 0;
100  }
101 
102  if (s->drop_str) {
103  ret = av_expr_parse(&s->drop_pexpr, s->drop_str,
104  var_names, NULL, NULL, NULL, NULL, 0, ctx);
105  if (ret < 0) {
106  av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for drop: %s\n", s->drop_str);
107  return ret;
108  }
109  }
110 
111  s->var_values[VAR_TB] = ctx->time_base_out.den ? av_q2d(ctx->time_base_out) : 0;
112  s->var_values[VAR_NOPTS] = AV_NOPTS_VALUE;
113  s->var_values[VAR_STARTPTS] = AV_NOPTS_VALUE;
114  s->var_values[VAR_STARTDTS] = AV_NOPTS_VALUE;
115  s->var_values[VAR_STATE] = 0;
116 
117  return 0;
118 }
119 
121 {
123  int i, ret, amount, drop = 0;
124  double res;
125 
127  if (ret < 0)
128  return ret;
129 
130  s->var_values[VAR_N] = s->pkt_idx++;
131  s->var_values[VAR_PTS] = pkt->pts;
132  s->var_values[VAR_DTS] = pkt->dts;
133  s->var_values[VAR_DURATION] =
134  s->var_values[VAR_D] = pkt->duration;
135  s->var_values[VAR_SIZE] = pkt->size;
136  s->var_values[VAR_KEY] = !!(pkt->flags & AV_PKT_FLAG_KEY);
137  s->var_values[VAR_POS] = pkt->pos;
138 
139  if (s->var_values[VAR_STARTPTS] == AV_NOPTS_VALUE)
140  s->var_values[VAR_STARTPTS] = pkt->pts;
141 
142  if (s->var_values[VAR_STARTDTS] == AV_NOPTS_VALUE)
143  s->var_values[VAR_STARTDTS] = pkt->dts;
144 
145  res = av_expr_eval(s->amount_pexpr, s->var_values, NULL);
146 
147  if (isnan(res))
148  amount = 0;
149  else if (res < 0)
150  amount = (s->state % 10001 + 1);
151  else
152  amount = (int)res;
153 
154  if (s->drop_str) {
155  res = av_expr_eval(s->drop_pexpr, s->var_values, NULL);
156 
157  if (isnan(res))
158  drop = 0;
159  else if (res < 0)
160  drop = !(s->state % FFABS((int)res));
161  else
162  drop = !!res;
163  }
164 
165  if(s->dropamount) {
166  drop = !(s->state % s->dropamount);
167  }
168 
169  av_log(ctx, AV_LOG_VERBOSE, "Stream #%d packet %d pts %"PRId64" - amount %d drop %d\n",
170  pkt->stream_index, (unsigned int)s->var_values[VAR_N], pkt->pts, amount, drop);
171 
172  if (drop) {
173  s->var_values[VAR_STATE] = ++s->state;
175  return AVERROR(EAGAIN);
176  }
177 
178  if (amount) {
180  if (ret < 0) {
182  return ret;
183  }
184  }
185 
186  for (i = 0; i < pkt->size; i++) {
187  s->state += pkt->data[i] + 1;
188  if (amount && s->state % amount == 0)
189  pkt->data[i] = s->state;
190  }
191 
192  s->var_values[VAR_STATE] = s->state;
193 
194  return 0;
195 }
196 
197 static void noise_close(AVBSFContext *bsf)
198 {
199  NoiseContext *s = bsf->priv_data;
200 
201  av_expr_free(s->amount_pexpr);
202  av_expr_free(s->drop_pexpr);
203  s->amount_pexpr = s->drop_pexpr = NULL;
204 }
205 
206 #define OFFSET(x) offsetof(NoiseContext, x)
207 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_BSF_PARAM)
208 static const AVOption options[] = {
209  { "amount", NULL, OFFSET(amount_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
210  { "drop", NULL, OFFSET(drop_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
211  { "dropamount", NULL, OFFSET(dropamount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
212  { NULL },
213 };
214 
215 static const AVClass noise_class = {
216  .class_name = "noise",
217  .item_name = av_default_item_name,
218  .option = options,
219  .version = LIBAVUTIL_VERSION_INT,
220 };
221 
223  .p.name = "noise",
224  .p.priv_class = &noise_class,
225  .priv_data_size = sizeof(NoiseContext),
226  .init = noise_init,
227  .close = noise_close,
228  .filter = noise,
229 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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_D
@ VAR_D
Definition: noise_bsf.c:54
var_names
static const char *const var_names[]
Definition: noise_bsf.c:30
AVBitStreamFilter::name
const char * name
Definition: bsf.h:112
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
VAR_SIZE
@ VAR_SIZE
Definition: noise_bsf.c:56
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:429
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
NoiseContext::drop_pexpr
AVExpr * drop_pexpr
Definition: noise_bsf.c:70
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:685
init
static int init
Definition: av_tx.c:47
bsf.h
FLAGS
#define FLAGS
Definition: noise_bsf.c:207
VAR_NOPTS
@ VAR_NOPTS
Definition: noise_bsf.c:51
NoiseContext::state
unsigned int state
Definition: noise_bsf.c:74
NoiseContext
Definition: noise_bsf.c:62
av_expr_free
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:336
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:256
noise_init
static int noise_init(AVBSFContext *ctx)
Definition: noise_bsf.c:78
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
var_name
var_name
Definition: noise_bsf.c:46
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:766
AVExpr
Definition: eval.c:157
VAR_VARS_NB
@ VAR_VARS_NB
Definition: noise_bsf.c:59
VAR_KEY
@ VAR_KEY
Definition: noise_bsf.c:57
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
FFBitStreamFilter
Definition: bsf_internal.h:27
isnan
#define isnan(x)
Definition: libm.h:340
NoiseContext::dropamount
int dropamount
Definition: noise_bsf.c:67
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
VAR_POS
@ VAR_POS
Definition: noise_bsf.c:55
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition: bsf_internal.h:31
VAR_PTS
@ VAR_PTS
Definition: noise_bsf.c:49
eval.h
VAR_DURATION
@ VAR_DURATION
Definition: noise_bsf.c:54
AVPacket::size
int size
Definition: packet.h:375
VAR_N
@ VAR_N
Definition: noise_bsf.c:47
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:373
VAR_STATE
@ VAR_STATE
Definition: noise_bsf.c:58
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:380
OFFSET
#define OFFSET(x)
Definition: noise_bsf.c:206
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
ff_noise_bsf
const FFBitStreamFilter ff_noise_bsf
Definition: noise_bsf.c:222
VAR_STARTDTS
@ VAR_STARTDTS
Definition: noise_bsf.c:53
NoiseContext::amount_pexpr
AVExpr * amount_pexpr
Definition: noise_bsf.c:69
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:71
noise
static int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition: noise_bsf.c:120
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
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: avpacket.c:504
NoiseContext::amount_str
char * amount_str
Definition: noise_bsf.c:65
AVPacket::stream_index
int stream_index
Definition: packet.h:376
NoiseContext::var_values
double var_values[VAR_VARS_NB]
Definition: noise_bsf.c:72
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:280
VAR_TB
@ VAR_TB
Definition: noise_bsf.c:48
options
static const AVOption options[]
Definition: noise_bsf.c:208
AVPacket
This structure stores compressed data.
Definition: packet.h:351
VAR_DTS
@ VAR_DTS
Definition: noise_bsf.c:50
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:394
NoiseContext::drop_str
char * drop_str
Definition: noise_bsf.c:66
noise_close
static void noise_close(AVBSFContext *bsf)
Definition: noise_bsf.c:197
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:257
VAR_STARTPTS
@ VAR_STARTPTS
Definition: noise_bsf.c:52
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
NoiseContext::pkt_idx
unsigned int pkt_idx
Definition: noise_bsf.c:75
int
int
Definition: ffmpeg_filter.c:155
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1241
noise_class
static const AVClass noise_class
Definition: noise_bsf.c:215