FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_perms.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 27 35 77.1%
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 504 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
79 {
80 int ret;
81 504 AVFilterContext *ctx = inlink->dst;
82 504 PermsContext *s = ctx->priv;
83 504 AVFrame *out = frame;
84 504 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 504 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
504 switch (s->mode) {
88 ✗ case MODE_TOGGLE: out_perm = in_perm == RO ? RW : RO; break;
89 504 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 242 times.
✓ Branch 1 taken 262 times.
504 av_log(ctx, AV_LOG_VERBOSE, "%s -> %s%s\n",
96 504 perm_str[in_perm], perm_str[out_perm],
97 in_perm == out_perm ? " (no-op)" : "");
98
99
4/4
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 359 times.
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 68 times.
504 if (in_perm == RO && out_perm == RW) {
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if ((ret = ff_inlink_make_frame_writable(inlink, &frame)) < 0) {
101 ✗ av_frame_free(&frame);
102 ✗ return ret;
103 }
104 77 out = frame;
105
4/4
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 185 times.
✓ Branch 3 taken 174 times.
427 } else if (in_perm == RW && out_perm == RO) {
106 185 out = av_frame_clone(frame);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (!out) {
108 ✗ av_frame_free(&frame);
109 ✗ return AVERROR(ENOMEM);
110 }
111 }
112
113 504 ret = ff_filter_frame(ctx->outputs[0], out);
114
115
4/4
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 145 times.
✓ Branch 2 taken 185 times.
✓ Branch 3 taken 174 times.
504 if (in_perm == RW && out_perm == RO)
116 185 av_frame_free(&frame);
117 504 return ret;
118 }
119
120 AVFILTER_DEFINE_CLASS_EXT(perms, "(a)perms", options);
121
122 #if CONFIG_APERMS_FILTER
123
124 static const AVFilterPad aperms_inputs[] = {
125 {
126 .name = "default",
127 .type = AVMEDIA_TYPE_AUDIO,
128 .filter_frame = filter_frame,
129 },
130 };
131
132 const FFFilter ff_af_aperms = {
133 .p.name = "aperms",
134 .p.description= NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
135 .p.priv_class= &perms_class,
136 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
137 AVFILTER_FLAG_METADATA_ONLY,
138 .init = init,
139 .priv_size = sizeof(PermsContext),
140 FILTER_INPUTS(aperms_inputs),
141 FILTER_OUTPUTS(ff_audio_default_filterpad),
142 .process_command = ff_filter_process_command,
143 };
144 #endif /* CONFIG_APERMS_FILTER */
145
146 #if CONFIG_PERMS_FILTER
147
148 static const AVFilterPad perms_inputs[] = {
149 {
150 .name = "default",
151 .type = AVMEDIA_TYPE_VIDEO,
152 .filter_frame = filter_frame,
153 },
154 };
155
156 const FFFilter ff_vf_perms = {
157 .p.name = "perms",
158 .p.description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
159 .p.priv_class = &perms_class,
160 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
161 AVFILTER_FLAG_METADATA_ONLY,
162 .init = init,
163 .priv_size = sizeof(PermsContext),
164 FILTER_INPUTS(perms_inputs),
165 FILTER_OUTPUTS(ff_video_default_filterpad),
166 .process_command = ff_filter_process_command,
167 .flags_internal = FF_FILTER_FLAG_DYNAMIC_FRAME_PARAMS,
168 };
169 #endif /* CONFIG_PERMS_FILTER */
170