FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/noise_bsf.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 65 80 81.2%
Branches: 33 52 63.5%

Line Branch Exec 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 <stdlib.h>
22
23 #include "bsf.h"
24 #include "bsf_internal.h"
25
26 #include "libavutil/avstring.h"
27 #include "libavutil/log.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 {
48 VAR_N,
49 VAR_TB,
50 VAR_PTS,
51 VAR_DTS,
52 VAR_NOPTS,
53 VAR_STARTPTS,
54 VAR_STARTDTS,
55 VAR_DURATION, VAR_D,
56 VAR_POS,
57 VAR_SIZE,
58 VAR_KEY,
59 VAR_STATE,
60 VAR_VARS_NB
61 };
62
63 typedef struct NoiseContext {
64 const AVClass *class;
65
66 char *amount_str;
67 char *drop_str;
68 int dropamount;
69
70 AVExpr *amount_pexpr;
71 AVExpr *drop_pexpr;
72
73 double var_values[VAR_VARS_NB];
74
75 unsigned int state;
76 unsigned int pkt_idx;
77 } NoiseContext;
78
79 2 static int noise_init(AVBSFContext *ctx)
80 {
81 2 NoiseContext *s = ctx->priv_data;
82 int ret;
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 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 2 ret = av_expr_parse(&s->amount_pexpr, s->amount_str,
91 var_names, NULL, NULL, NULL, NULL, 0, ctx);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
93 av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for amount: %s\n", s->amount_str);
94 return ret;
95 }
96
97
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 if (s->drop_str && s->dropamount) {
98 av_log(ctx, AV_LOG_WARNING, "Both drop '%s' and dropamount=%d set. Ignoring dropamount.\n",
99 s->drop_str, s->dropamount);
100 s->dropamount = 0;
101 }
102
103
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->drop_str) {
104 1 ret = av_expr_parse(&s->drop_pexpr, s->drop_str,
105 var_names, NULL, NULL, NULL, NULL, 0, ctx);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
107 av_log(ctx, AV_LOG_ERROR, "Error in parsing expr for drop: %s\n", s->drop_str);
108 return ret;
109 }
110 }
111
112
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->var_values[VAR_TB] = ctx->time_base_out.den ? av_q2d(ctx->time_base_out) : 0;
113 2 s->var_values[VAR_NOPTS] = AV_NOPTS_VALUE;
114 2 s->var_values[VAR_STARTPTS] = AV_NOPTS_VALUE;
115 2 s->var_values[VAR_STARTDTS] = AV_NOPTS_VALUE;
116 2 s->var_values[VAR_STATE] = 0;
117
118 2 return 0;
119 }
120
121 20 static int noise(AVBSFContext *ctx, AVPacket *pkt)
122 {
123 20 NoiseContext *s = ctx->priv_data;
124 20 int i, ret, amount, drop = 0;
125 double res;
126
127 20 ret = ff_bsf_get_packet_ref(ctx, pkt);
128
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (ret < 0)
129 10 return ret;
130
131 10 s->var_values[VAR_N] = s->pkt_idx++;
132 10 s->var_values[VAR_PTS] = pkt->pts;
133 10 s->var_values[VAR_DTS] = pkt->dts;
134 10 s->var_values[VAR_DURATION] =
135 10 s->var_values[VAR_D] = pkt->duration;
136 10 s->var_values[VAR_SIZE] = pkt->size;
137
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 s->var_values[VAR_KEY] = !!(pkt->flags & AV_PKT_FLAG_KEY);
138 10 s->var_values[VAR_POS] = pkt->pos;
139
140
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (s->var_values[VAR_STARTPTS] == AV_NOPTS_VALUE)
141 2 s->var_values[VAR_STARTPTS] = pkt->pts;
142
143
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (s->var_values[VAR_STARTDTS] == AV_NOPTS_VALUE)
144 2 s->var_values[VAR_STARTDTS] = pkt->dts;
145
146 10 res = av_expr_eval(s->amount_pexpr, s->var_values, NULL);
147
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (isnan(res))
149 amount = 0;
150
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 else if (res < 0)
151 5 amount = (s->state % 10001 + 1);
152 else
153 5 amount = (int)res;
154
155
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 if (s->drop_str) {
156 5 res = av_expr_eval(s->drop_pexpr, s->var_values, NULL);
157
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (isnan(res))
159 drop = 0;
160
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 else if (res < 0)
161 5 drop = !(s->state % FFABS((int)res));
162 else
163 drop = !!res;
164 }
165
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if(s->dropamount) {
167 drop = !(s->state % s->dropamount);
168 }
169
170 10 av_log(ctx, AV_LOG_VERBOSE, "Stream #%d packet %d pts %"PRId64" - amount %d drop %d\n",
171 10 pkt->stream_index, (unsigned int)s->var_values[VAR_N], pkt->pts, amount, drop);
172
173
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 if (drop) {
174 1 s->var_values[VAR_STATE] = ++s->state;
175 1 av_packet_unref(pkt);
176 1 return AVERROR(EAGAIN);
177 }
178
179
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (amount) {
180 9 ret = av_packet_make_writable(pkt);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0) {
182 av_packet_unref(pkt);
183 return ret;
184 }
185 }
186
187
2/2
✓ Branch 0 taken 18418 times.
✓ Branch 1 taken 9 times.
18427 for (i = 0; i < pkt->size; i++) {
188 18418 s->state += pkt->data[i] + 1;
189
3/4
✓ Branch 0 taken 18418 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4808 times.
✓ Branch 3 taken 13610 times.
18418 if (amount && s->state % amount == 0)
190 4808 pkt->data[i] = s->state;
191 }
192
193 9 s->var_values[VAR_STATE] = s->state;
194
195 9 return 0;
196 }
197
198 2 static void noise_close(AVBSFContext *bsf)
199 {
200 2 NoiseContext *s = bsf->priv_data;
201
202 2 av_expr_free(s->amount_pexpr);
203 2 av_expr_free(s->drop_pexpr);
204 2 s->amount_pexpr = s->drop_pexpr = NULL;
205 2 }
206
207 #define OFFSET(x) offsetof(NoiseContext, x)
208 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_BSF_PARAM)
209 static const AVOption options[] = {
210 { "amount", NULL, OFFSET(amount_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
211 { "drop", NULL, OFFSET(drop_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },
212 { "dropamount", NULL, OFFSET(dropamount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
213 { NULL },
214 };
215
216 static const AVClass noise_class = {
217 .class_name = "noise",
218 .item_name = av_default_item_name,
219 .option = options,
220 .version = LIBAVUTIL_VERSION_INT,
221 };
222
223 const AVBitStreamFilter ff_noise_bsf = {
224 .name = "noise",
225 .priv_data_size = sizeof(NoiseContext),
226 .priv_class = &noise_class,
227 .init = noise_init,
228 .close = noise_close,
229 .filter = noise,
230 };
231