FFmpeg
Loading...
Searching...
No Matches
f_perms.c
Go to the documentation of this file.
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"
26#include "audio.h"
27#include "filters.h"
28#include "video.h"
29
38
39typedef struct PermsContext {
40 const AVClass *class;
43 int mode;
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
50static 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
62{
63 PermsContext *s = ctx->priv;
64 uint32_t seed;
65
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);
71
72 return 0;
73}
74
75enum perm { RO, RW };
76static const char * const perm_str[2] = { "RO", "RW" };
77
79{
80 int ret;
81 AVFilterContext *ctx = inlink->dst;
82 PermsContext *s = ctx->priv;
83 AVFrame *out = frame;
84 enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
85 enum perm out_perm;
86
87 switch (s->mode) {
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;
93 }
94
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)" : "");
98
99 if (in_perm == RO && out_perm == RW) {
100 if ((ret = ff_inlink_make_frame_writable(inlink, &frame)) < 0)
101 return ret;
102 out = frame;
103 } else if (in_perm == RW && out_perm == RO) {
105 if (!out)
106 return AVERROR(ENOMEM);
107 }
108
109 ret = ff_filter_frame(ctx->outputs[0], out);
110
111 if (in_perm == RW && out_perm == RO)
113 return ret;
114}
115
117
118#if CONFIG_APERMS_FILTER
119
120static const AVFilterPad aperms_inputs[] = {
121 {
122 .name = "default",
123 .type = AVMEDIA_TYPE_AUDIO,
124 .filter_frame = filter_frame,
125 },
126};
127
128const FFFilter ff_af_aperms = {
129 .p.name = "aperms",
130 .p.description= NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
131 .p.priv_class= &perms_class,
134 .init = init,
135 .priv_size = sizeof(PermsContext),
136 FILTER_INPUTS(aperms_inputs),
138 .process_command = ff_filter_process_command,
139};
140#endif /* CONFIG_APERMS_FILTER */
141
142#if CONFIG_PERMS_FILTER
143
144static const AVFilterPad perms_inputs[] = {
145 {
146 .name = "default",
147 .type = AVMEDIA_TYPE_VIDEO,
148 .filter_frame = filter_frame,
149 },
150};
151
152const FFFilter ff_vf_perms = {
153 .p.name = "perms",
154 .p.description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
155 .p.priv_class = &perms_class,
158 .init = init,
159 .priv_size = sizeof(PermsContext),
160 FILTER_INPUTS(perms_inputs),
162 .process_command = ff_filter_process_command,
163};
164#endif /* CONFIG_PERMS_FILTER */
#define TFLAGS
Definition af_afade.c:66
@ NB_MODES
Definition af_afftdn.c:48
const FFFilter ff_af_aperms
const FFFilter ff_vf_perms
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
Make sure a frame is writable.
Definition avfilter.c:1567
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ MODE_RW
Definition f_perms.c:33
@ MODE_NONE
Definition f_perms.c:31
@ MODE_RANDOM
Definition f_perms.c:35
@ MODE_TOGGLE
Definition f_perms.c:34
@ MODE_RO
Definition f_perms.c:32
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition f_perms.c:78
static const char *const perm_str[2]
Definition f_perms.c:76
#define OFFSET(x)
Definition f_perms.c:46
perm
Definition f_perms.c:75
@ RO
Definition f_perms.c:75
@ RW
Definition f_perms.c:75
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
AVOption.
Definition opt.h:428
int64_t random_seed
Definition f_perms.c:42
AVLFG lfg
Definition f_perms.c:41
Definition swscale.c:71
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
static unsigned int seed
Definition videogen.c:78