/src/ffmpeg/libavcodec/bsf/noise.c
Line | Count | Source |
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 "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 { |
47 | | VAR_N, |
48 | | VAR_TB, |
49 | | VAR_PTS, |
50 | | VAR_DTS, |
51 | | VAR_NOPTS, |
52 | | VAR_STARTPTS, |
53 | | VAR_STARTDTS, |
54 | | VAR_DURATION, VAR_D, |
55 | | VAR_POS, |
56 | | VAR_SIZE, |
57 | | VAR_KEY, |
58 | | VAR_STATE, |
59 | | VAR_VARS_NB |
60 | | }; |
61 | | |
62 | | typedef struct NoiseContext { |
63 | | const AVClass *class; |
64 | | |
65 | | char *amount_str; |
66 | | char *drop_str; |
67 | | int dropamount; |
68 | | |
69 | | AVExpr *amount_pexpr; |
70 | | AVExpr *drop_pexpr; |
71 | | |
72 | | double var_values[VAR_VARS_NB]; |
73 | | |
74 | | unsigned int state; |
75 | | unsigned int pkt_idx; |
76 | | } NoiseContext; |
77 | | |
78 | | static int noise_init(AVBSFContext *ctx) |
79 | 428 | { |
80 | 428 | NoiseContext *s = ctx->priv_data; |
81 | 428 | const char *amount_str = s->amount_str; |
82 | 428 | int ret; |
83 | | |
84 | 428 | if (!amount_str) |
85 | 428 | amount_str = (!s->drop_str && !s->dropamount) ? "-1" : "0"; |
86 | | |
87 | 428 | if (ctx->par_in->codec_id == AV_CODEC_ID_WRAPPED_AVFRAME && |
88 | 0 | strcmp(amount_str, "0")) { |
89 | 0 | av_log(ctx, AV_LOG_ERROR, "Wrapped AVFrame noising is unsupported\n"); |
90 | 0 | return AVERROR_PATCHWELCOME; |
91 | 0 | } |
92 | | |
93 | 428 | ret = av_expr_parse(&s->amount_pexpr, amount_str, |
94 | 428 | var_names, NULL, NULL, NULL, NULL, 0, ctx); |
95 | 428 | if (ret < 0) { |
96 | 0 | av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for amount: %s\n", amount_str); |
97 | 0 | return ret; |
98 | 0 | } |
99 | | |
100 | 428 | if (s->drop_str && s->dropamount) { |
101 | 0 | av_log(ctx, AV_LOG_WARNING, "Both drop '%s' and dropamount=%d set. Ignoring dropamount.\n", |
102 | 0 | s->drop_str, s->dropamount); |
103 | 0 | s->dropamount = 0; |
104 | 0 | } |
105 | | |
106 | 428 | if (s->drop_str) { |
107 | 0 | ret = av_expr_parse(&s->drop_pexpr, s->drop_str, |
108 | 0 | var_names, NULL, NULL, NULL, NULL, 0, ctx); |
109 | 0 | if (ret < 0) { |
110 | 0 | av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for drop: %s\n", s->drop_str); |
111 | 0 | return ret; |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | 428 | s->var_values[VAR_TB] = ctx->time_base_out.den ? av_q2d(ctx->time_base_out) : 0; |
116 | 428 | s->var_values[VAR_NOPTS] = AV_NOPTS_VALUE; |
117 | 428 | s->var_values[VAR_STARTPTS] = AV_NOPTS_VALUE; |
118 | 428 | s->var_values[VAR_STARTDTS] = AV_NOPTS_VALUE; |
119 | 428 | s->var_values[VAR_STATE] = 0; |
120 | | |
121 | 428 | return 0; |
122 | 428 | } |
123 | | |
124 | | static int noise(AVBSFContext *ctx, AVPacket *pkt) |
125 | 16.9k | { |
126 | 16.9k | NoiseContext *s = ctx->priv_data; |
127 | 16.9k | int i, ret, amount, drop = 0; |
128 | 16.9k | double res; |
129 | | |
130 | 16.9k | ret = ff_bsf_get_packet_ref(ctx, pkt); |
131 | 16.9k | if (ret < 0) |
132 | 8.67k | return ret; |
133 | | |
134 | 8.24k | s->var_values[VAR_N] = s->pkt_idx++; |
135 | 8.24k | s->var_values[VAR_PTS] = pkt->pts; |
136 | 8.24k | s->var_values[VAR_DTS] = pkt->dts; |
137 | 8.24k | s->var_values[VAR_DURATION] = |
138 | 8.24k | s->var_values[VAR_D] = pkt->duration; |
139 | 8.24k | s->var_values[VAR_SIZE] = pkt->size; |
140 | 8.24k | s->var_values[VAR_KEY] = !!(pkt->flags & AV_PKT_FLAG_KEY); |
141 | 8.24k | s->var_values[VAR_POS] = pkt->pos; |
142 | | |
143 | 8.24k | if (s->var_values[VAR_STARTPTS] == AV_NOPTS_VALUE) |
144 | 8.24k | s->var_values[VAR_STARTPTS] = pkt->pts; |
145 | | |
146 | 8.24k | if (s->var_values[VAR_STARTDTS] == AV_NOPTS_VALUE) |
147 | 8.24k | s->var_values[VAR_STARTDTS] = pkt->dts; |
148 | | |
149 | 8.24k | res = av_expr_eval(s->amount_pexpr, s->var_values, NULL); |
150 | | |
151 | 8.24k | if (isnan(res)) |
152 | 0 | amount = 0; |
153 | 8.24k | else if (res < 0) |
154 | 8.24k | amount = (s->state % 10001 + 1); |
155 | 0 | else |
156 | 0 | amount = (int)res; |
157 | | |
158 | 8.24k | if (s->drop_str) { |
159 | 0 | res = av_expr_eval(s->drop_pexpr, s->var_values, NULL); |
160 | |
|
161 | 0 | if (isnan(res)) |
162 | 0 | drop = 0; |
163 | 0 | else if (res < 0) |
164 | 0 | drop = !(s->state % FFABS((int)res)); |
165 | 0 | else |
166 | 0 | drop = !!res; |
167 | 0 | } |
168 | | |
169 | 8.24k | if(s->dropamount) { |
170 | 0 | drop = !(s->state % s->dropamount); |
171 | 0 | } |
172 | | |
173 | 8.24k | av_log(ctx, AV_LOG_VERBOSE, "Stream #%d packet %d pts %"PRId64" - amount %d drop %d\n", |
174 | 8.24k | pkt->stream_index, (unsigned int)s->var_values[VAR_N], pkt->pts, amount, drop); |
175 | | |
176 | 8.24k | if (drop) { |
177 | 0 | s->var_values[VAR_STATE] = ++s->state; |
178 | 0 | av_packet_unref(pkt); |
179 | 0 | return AVERROR(EAGAIN); |
180 | 0 | } |
181 | | |
182 | 8.24k | if (amount) { |
183 | 8.24k | ret = av_packet_make_writable(pkt); |
184 | 8.24k | if (ret < 0) { |
185 | 0 | av_packet_unref(pkt); |
186 | 0 | return ret; |
187 | 0 | } |
188 | 8.24k | } |
189 | | |
190 | 6.41M | for (i = 0; i < pkt->size; i++) { |
191 | 6.40M | s->state += pkt->data[i] + 1; |
192 | 6.40M | if (amount && s->state % amount == 0) |
193 | 4.57M | pkt->data[i] = s->state; |
194 | 6.40M | } |
195 | | |
196 | 8.24k | s->var_values[VAR_STATE] = s->state; |
197 | | |
198 | 8.24k | return 0; |
199 | 8.24k | } |
200 | | |
201 | | static void noise_close(AVBSFContext *bsf) |
202 | 428 | { |
203 | 428 | NoiseContext *s = bsf->priv_data; |
204 | | |
205 | 428 | av_expr_free(s->amount_pexpr); |
206 | 428 | av_expr_free(s->drop_pexpr); |
207 | | s->amount_pexpr = s->drop_pexpr = NULL; |
208 | 428 | } |
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) |
212 | | static 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 | | |
219 | | static 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 | | |
226 | | const FFBitStreamFilter ff_noise_bsf = { |
227 | | .p.name = "noise", |
228 | | .p.priv_class = &noise_class, |
229 | | .priv_data_size = sizeof(NoiseContext), |
230 | | .init = noise_init, |
231 | | .close = noise_close, |
232 | | .filter = noise, |
233 | | }; |