2013-03-13 04:59:20 +00:00
|
|
|
/*
|
2013-03-26 01:41:05 +00:00
|
|
|
* Copyright (c) 2013 Clément Bœsch
|
|
|
|
*
|
2013-03-13 04:59:20 +00:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/lfg.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/random_seed.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "video.h"
|
|
|
|
|
|
|
|
enum mode {
|
|
|
|
MODE_NONE,
|
|
|
|
MODE_RO,
|
|
|
|
MODE_RW,
|
|
|
|
MODE_TOGGLE,
|
|
|
|
MODE_RANDOM,
|
|
|
|
NB_MODES
|
|
|
|
};
|
|
|
|
|
2017-05-12 18:00:49 +00:00
|
|
|
typedef struct PermsContext {
|
2013-03-13 04:59:20 +00:00
|
|
|
const AVClass *class;
|
|
|
|
AVLFG lfg;
|
2013-03-26 01:39:55 +00:00
|
|
|
int64_t random_seed;
|
2015-03-07 01:44:04 +00:00
|
|
|
int mode;
|
2013-03-13 04:59:20 +00:00
|
|
|
} PermsContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(PermsContext, x)
|
2013-04-12 13:45:28 +00:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
|
2013-03-13 04:59:20 +00:00
|
|
|
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "mode", "select permissions mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_NONE}, MODE_NONE, NB_MODES-1, FLAGS, "mode" },
|
|
|
|
{ "none", "do nothing", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_NONE}, INT_MIN, INT_MAX, FLAGS, "mode" },
|
|
|
|
{ "ro", "set all output frames read-only", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RO}, INT_MIN, INT_MAX, FLAGS, "mode" },
|
|
|
|
{ "rw", "set all output frames writable", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RW}, INT_MIN, INT_MAX, FLAGS, "mode" },
|
|
|
|
{ "toggle", "switch permissions", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_TOGGLE}, INT_MIN, INT_MAX, FLAGS, "mode" },
|
|
|
|
{ "random", "set permissions randomly", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RANDOM}, INT_MIN, INT_MAX, FLAGS, "mode" },
|
2013-03-26 01:39:55 +00:00
|
|
|
{ "seed", "set the seed for the random mode", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
|
2013-03-13 04:59:20 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-04-12 09:13:33 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2013-03-13 04:59:20 +00:00
|
|
|
{
|
2015-09-09 18:30:08 +00:00
|
|
|
PermsContext *s = ctx->priv;
|
2013-03-13 04:59:20 +00:00
|
|
|
|
2015-09-09 18:30:08 +00:00
|
|
|
if (s->mode == MODE_RANDOM) {
|
2013-03-26 01:39:55 +00:00
|
|
|
uint32_t seed;
|
|
|
|
|
2015-09-09 18:30:08 +00:00
|
|
|
if (s->random_seed == -1)
|
|
|
|
s->random_seed = av_get_random_seed();
|
|
|
|
seed = s->random_seed;
|
2017-03-27 19:31:46 +00:00
|
|
|
av_log(ctx, AV_LOG_INFO, "random seed: 0x%08"PRIx32"\n", seed);
|
2015-09-09 18:30:08 +00:00
|
|
|
av_lfg_init(&s->lfg, seed);
|
2013-03-26 01:39:55 +00:00
|
|
|
}
|
2013-03-13 04:59:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum perm { RO, RW };
|
2014-08-28 22:32:32 +00:00
|
|
|
static const char * const perm_str[2] = { "RO", "RW" };
|
2013-03-13 04:59:20 +00:00
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2015-09-09 18:30:08 +00:00
|
|
|
PermsContext *s = ctx->priv;
|
2013-03-13 04:59:20 +00:00
|
|
|
AVFrame *out = frame;
|
|
|
|
enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
|
|
|
|
enum perm out_perm;
|
|
|
|
|
2015-09-09 18:30:08 +00:00
|
|
|
switch (s->mode) {
|
2013-03-13 04:59:20 +00:00
|
|
|
case MODE_TOGGLE: out_perm = in_perm == RO ? RW : RO; break;
|
2015-09-09 18:30:08 +00:00
|
|
|
case MODE_RANDOM: out_perm = av_lfg_get(&s->lfg) & 1 ? RW : RO; break;
|
2013-03-13 04:59:20 +00:00
|
|
|
case MODE_RO: out_perm = RO; break;
|
|
|
|
case MODE_RW: out_perm = RW; break;
|
|
|
|
default: out_perm = in_perm; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "%s -> %s%s\n",
|
|
|
|
perm_str[in_perm], perm_str[out_perm],
|
|
|
|
in_perm == out_perm ? " (no-op)" : "");
|
|
|
|
|
|
|
|
if (in_perm == RO && out_perm == RW) {
|
|
|
|
if ((ret = av_frame_make_writable(frame)) < 0)
|
|
|
|
return ret;
|
|
|
|
} else if (in_perm == RW && out_perm == RO) {
|
|
|
|
out = av_frame_clone(frame);
|
|
|
|
if (!out)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ff_filter_frame(ctx->outputs[0], out);
|
|
|
|
|
|
|
|
if (in_perm == RW && out_perm == RO)
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_APERMS_FILTER
|
|
|
|
|
|
|
|
#define aperms_options options
|
|
|
|
AVFILTER_DEFINE_CLASS(aperms);
|
|
|
|
|
|
|
|
static const AVFilterPad aperms_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad aperms_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 10:50:56 +00:00
|
|
|
AVFilter ff_af_aperms = {
|
2013-03-13 04:59:20 +00:00
|
|
|
.name = "aperms",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
|
2013-03-26 01:24:03 +00:00
|
|
|
.init = init,
|
2013-03-13 04:59:20 +00:00
|
|
|
.priv_size = sizeof(PermsContext),
|
|
|
|
.inputs = aperms_inputs,
|
|
|
|
.outputs = aperms_outputs,
|
|
|
|
.priv_class = &aperms_class,
|
2020-12-07 20:05:37 +00:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
|
2013-03-13 04:59:20 +00:00
|
|
|
};
|
|
|
|
#endif /* CONFIG_APERMS_FILTER */
|
|
|
|
|
|
|
|
#if CONFIG_PERMS_FILTER
|
|
|
|
|
|
|
|
#define perms_options options
|
|
|
|
AVFILTER_DEFINE_CLASS(perms);
|
|
|
|
|
|
|
|
static const AVFilterPad perms_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad perms_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 10:50:56 +00:00
|
|
|
AVFilter ff_vf_perms = {
|
2013-03-13 04:59:20 +00:00
|
|
|
.name = "perms",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
|
2013-03-26 01:24:03 +00:00
|
|
|
.init = init,
|
2013-03-13 04:59:20 +00:00
|
|
|
.priv_size = sizeof(PermsContext),
|
|
|
|
.inputs = perms_inputs,
|
|
|
|
.outputs = perms_outputs,
|
|
|
|
.priv_class = &perms_class,
|
2020-12-07 20:05:37 +00:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
|
2013-03-13 04:59:20 +00:00
|
|
|
};
|
|
|
|
#endif /* CONFIG_PERMS_FILTER */
|