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 "libavutil/lfg.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/random_seed.h"
43 #define OFFSET(x) offsetof(PermsContext, x)
44 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
46 static const AVOption options[] = {
47 { "mode", "select permissions mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_NONE}, MODE_NONE, NB_MODES-1, FLAGS, "mode" },
48 { "none", "do nothing", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_NONE}, INT_MIN, INT_MAX, FLAGS, "mode" },
49 { "ro", "set all output frames read-only", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RO}, INT_MIN, INT_MAX, FLAGS, "mode" },
50 { "rw", "set all output frames writable", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RW}, INT_MIN, INT_MAX, FLAGS, "mode" },
51 { "toggle", "switch permissions", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_TOGGLE}, INT_MIN, INT_MAX, FLAGS, "mode" },
52 { "random", "set permissions randomly", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RANDOM}, INT_MIN, INT_MAX, FLAGS, "mode" },
53 { "seed", "set the seed for the random mode", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
57 static av_cold int init(AVFilterContext *ctx, const char *args)
59 PermsContext *perms = ctx->priv;
61 if (perms->mode == MODE_RANDOM) {
64 if (perms->random_seed == -1)
65 perms->random_seed = av_get_random_seed();
66 seed = perms->random_seed;
67 av_log(ctx, AV_LOG_INFO, "random seed: 0x%08x\n", seed);
68 av_lfg_init(&perms->lfg, seed);
75 static const char *perm_str[2] = { "RO", "RW" };
77 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
80 AVFilterContext *ctx = inlink->dst;
81 PermsContext *perms = ctx->priv;
83 enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
86 switch (perms->mode) {
87 case MODE_TOGGLE: out_perm = in_perm == RO ? RW : RO; break;
88 case MODE_RANDOM: out_perm = av_lfg_get(&perms->lfg) & 1 ? RW : RO; break;
89 case MODE_RO: out_perm = RO; break;
90 case MODE_RW: out_perm = RW; break;
91 default: out_perm = in_perm; break;
94 av_log(ctx, AV_LOG_VERBOSE, "%s -> %s%s\n",
95 perm_str[in_perm], perm_str[out_perm],
96 in_perm == out_perm ? " (no-op)" : "");
98 if (in_perm == RO && out_perm == RW) {
99 if ((ret = av_frame_make_writable(frame)) < 0)
101 } else if (in_perm == RW && out_perm == RO) {
102 out = av_frame_clone(frame);
104 return AVERROR(ENOMEM);
107 ret = ff_filter_frame(ctx->outputs[0], out);
109 if (in_perm == RW && out_perm == RO)
110 av_frame_free(&frame);
114 static const char *const shorthand[] = { "mode", NULL };
116 #if CONFIG_APERMS_FILTER
118 #define aperms_options options
119 AVFILTER_DEFINE_CLASS(aperms);
121 static const AVFilterPad aperms_inputs[] = {
124 .type = AVMEDIA_TYPE_AUDIO,
125 .filter_frame = filter_frame,
130 static const AVFilterPad aperms_outputs[] = {
133 .type = AVMEDIA_TYPE_AUDIO,
138 AVFilter avfilter_af_aperms = {
140 .description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
142 .priv_size = sizeof(PermsContext),
143 .inputs = aperms_inputs,
144 .outputs = aperms_outputs,
145 .priv_class = &aperms_class,
146 .shorthand = shorthand,
148 #endif /* CONFIG_APERMS_FILTER */
150 #if CONFIG_PERMS_FILTER
152 #define perms_options options
153 AVFILTER_DEFINE_CLASS(perms);
155 static const AVFilterPad perms_inputs[] = {
158 .type = AVMEDIA_TYPE_VIDEO,
159 .filter_frame = filter_frame,
164 static const AVFilterPad perms_outputs[] = {
167 .type = AVMEDIA_TYPE_VIDEO,
172 AVFilter avfilter_vf_perms = {
174 .description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
176 .priv_size = sizeof(PermsContext),
177 .inputs = perms_inputs,
178 .outputs = perms_outputs,
179 .priv_class = &perms_class,
180 .shorthand = shorthand,
182 #endif /* CONFIG_PERMS_FILTER */