FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_perms.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 27 33 81.8%
Functions: 2 2 100.0%
Branches: 18 25 72.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 Clément Bœsch
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 "config_components.h"
22
23 #include "libavutil/lfg.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/random_seed.h"
26 #include "audio.h"
27 #include "filters.h"
28 #include "video.h"
29
30 enum mode {
31 MODE_NONE,
32 MODE_RO,
33 MODE_RW,
34 MODE_TOGGLE,
35 MODE_RANDOM,
36 NB_MODES
37 };
38
39 typedef struct PermsContext {
40 const AVClass *class;
41 AVLFG lfg;
42 int64_t random_seed;
43 int mode;
44 } PermsContext;
45
46 #define OFFSET(x) offsetof(PermsContext, x)
47 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
48 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
49
50 static const AVOption options[] = {
51 { "mode", "select permissions mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_NONE}, MODE_NONE, NB_MODES-1, TFLAGS, .unit = "mode" },
52 { "none", "do nothing", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_NONE}, 0, 0, TFLAGS, .unit = "mode" },
53 { "ro", "set all output frames read-only", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RO}, 0, 0, TFLAGS, .unit = "mode" },
54 { "rw", "set all output frames writable", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RW}, 0, 0, TFLAGS, .unit = "mode" },
55 { "toggle", "switch permissions", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_TOGGLE}, 0, 0, TFLAGS, .unit = "mode" },
56 { "random", "set permissions randomly", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RANDOM}, 0, 0, TFLAGS, .unit = "mode" },
57 { "seed", "set the seed for the random mode", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
58 { NULL }
59 };
60
61 28 static av_cold int init(AVFilterContext *ctx)
62 {
63 28 PermsContext *s = ctx->priv;
64 uint32_t seed;
65
66
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (s->random_seed == -1)
67 28 s->random_seed = av_get_random_seed();
68 28 seed = s->random_seed;
69 28 av_log(ctx, AV_LOG_INFO, "random seed: 0x%08"PRIx32"\n", seed);
70 28 av_lfg_init(&s->lfg, seed);
71
72 28 return 0;
73 }
74
75 enum perm { RO, RW };
76 static const char * const perm_str[2] = { "RO", "RW" };
77
78 497 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
79 {
80 int ret;
81 497 AVFilterContext *ctx = inlink->dst;
82 497 PermsContext *s = ctx->priv;
83 497 AVFrame *out = frame;
84 497 enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
85 enum perm out_perm;
86
87
1/5
✗ Branch 0 not taken.
✓ Branch 1 taken 497 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
497 switch (s->mode) {
88 case MODE_TOGGLE: out_perm = in_perm == RO ? RW : RO; break;
89 497 case MODE_RANDOM: out_perm = av_lfg_get(&s->lfg) & 1 ? RW : RO; break;
90 case MODE_RO: out_perm = RO; break;
91 case MODE_RW: out_perm = RW; break;
92 default: out_perm = in_perm; break;
93 }
94
95
2/2
✓ Branch 0 taken 239 times.
✓ Branch 1 taken 258 times.
497 av_log(ctx, AV_LOG_VERBOSE, "%s -> %s%s\n",
96 497 perm_str[in_perm], perm_str[out_perm],
97 in_perm == out_perm ? " (no-op)" : "");
98
99
4/4
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 334 times.
✓ Branch 2 taken 69 times.
✓ Branch 3 taken 94 times.
497 if (in_perm == RO && out_perm == RW) {
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 69 times.
69 if ((ret = ff_inlink_make_frame_writable(inlink, &frame)) < 0)
101 return ret;
102 69 out = frame;
103
4/4
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 189 times.
✓ Branch 3 taken 145 times.
428 } else if (in_perm == RW && out_perm == RO) {
104 189 out = av_frame_clone(frame);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 189 times.
189 if (!out)
106 return AVERROR(ENOMEM);
107 }
108
109 497 ret = ff_filter_frame(ctx->outputs[0], out);
110
111
4/4
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 163 times.
✓ Branch 2 taken 189 times.
✓ Branch 3 taken 145 times.
497 if (in_perm == RW && out_perm == RO)
112 189 av_frame_free(&frame);
113 497 return ret;
114 }
115
116 AVFILTER_DEFINE_CLASS_EXT(perms, "(a)perms", options);
117
118 #if CONFIG_APERMS_FILTER
119
120 static const AVFilterPad aperms_inputs[] = {
121 {
122 .name = "default",
123 .type = AVMEDIA_TYPE_AUDIO,
124 .filter_frame = filter_frame,
125 },
126 };
127
128 const AVFilter ff_af_aperms = {
129 .name = "aperms",
130 .description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
131 .priv_class = &perms_class,
132 .init = init,
133 .priv_size = sizeof(PermsContext),
134 FILTER_INPUTS(aperms_inputs),
135 FILTER_OUTPUTS(ff_audio_default_filterpad),
136 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
137 AVFILTER_FLAG_METADATA_ONLY,
138 .process_command = ff_filter_process_command,
139 };
140 #endif /* CONFIG_APERMS_FILTER */
141
142 #if CONFIG_PERMS_FILTER
143
144 static const AVFilterPad perms_inputs[] = {
145 {
146 .name = "default",
147 .type = AVMEDIA_TYPE_VIDEO,
148 .filter_frame = filter_frame,
149 },
150 };
151
152 const AVFilter ff_vf_perms = {
153 .name = "perms",
154 .description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
155 .init = init,
156 .priv_size = sizeof(PermsContext),
157 FILTER_INPUTS(perms_inputs),
158 FILTER_OUTPUTS(ff_video_default_filterpad),
159 .priv_class = &perms_class,
160 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
161 AVFILTER_FLAG_METADATA_ONLY,
162 .process_command = ff_filter_process_command,
163 };
164 #endif /* CONFIG_PERMS_FILTER */
165