2 * Copyright (c) 2013 Clément Bœsch
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "config_components.h"
23 #include "libavutil/lfg.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/random_seed.h"
39 typedef struct PermsContext {
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
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, "mode" },
52 { "none", "do nothing", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_NONE}, 0, 0, TFLAGS, "mode" },
53 { "ro", "set all output frames read-only", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RO}, 0, 0, TFLAGS, "mode" },
54 { "rw", "set all output frames writable", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RW}, 0, 0, TFLAGS, "mode" },
55 { "toggle", "switch permissions", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_TOGGLE}, 0, 0, TFLAGS, "mode" },
56 { "random", "set permissions randomly", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RANDOM}, 0, 0, TFLAGS, "mode" },
57 { "seed", "set the seed for the random mode", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
61 static av_cold int init(AVFilterContext *ctx)
63 PermsContext *s = ctx->priv;
66 if (s->random_seed == -1)
67 s->random_seed = av_get_random_seed();
68 seed = s->random_seed;
69 av_log(ctx, AV_LOG_INFO, "random seed: 0x%08"PRIx32"\n", seed);
70 av_lfg_init(&s->lfg, seed);
76 static const char * const perm_str[2] = { "RO", "RW" };
78 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
81 AVFilterContext *ctx = inlink->dst;
82 PermsContext *s = ctx->priv;
84 enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
88 case MODE_TOGGLE: out_perm = in_perm == RO ? RW : RO; break;
89 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;
95 av_log(ctx, AV_LOG_VERBOSE, "%s -> %s%s\n",
96 perm_str[in_perm], perm_str[out_perm],
97 in_perm == out_perm ? " (no-op)" : "");
99 if (in_perm == RO && out_perm == RW) {
100 if ((ret = ff_inlink_make_frame_writable(inlink, &frame)) < 0)
103 } else if (in_perm == RW && out_perm == RO) {
104 out = av_frame_clone(frame);
106 return AVERROR(ENOMEM);
109 ret = ff_filter_frame(ctx->outputs[0], out);
111 if (in_perm == RW && out_perm == RO)
112 av_frame_free(&frame);
116 AVFILTER_DEFINE_CLASS_EXT(perms, "(a)perms", options);
118 #if CONFIG_APERMS_FILTER
120 static const AVFilterPad aperms_inputs[] = {
123 .type = AVMEDIA_TYPE_AUDIO,
124 .filter_frame = filter_frame,
128 const AVFilter ff_af_aperms = {
130 .description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
131 .priv_class = &perms_class,
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,
140 #endif /* CONFIG_APERMS_FILTER */
142 #if CONFIG_PERMS_FILTER
144 static const AVFilterPad perms_inputs[] = {
147 .type = AVMEDIA_TYPE_VIDEO,
148 .filter_frame = filter_frame,
152 const AVFilter ff_vf_perms = {
154 .description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
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,
164 #endif /* CONFIG_PERMS_FILTER */