FFmpeg
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"
25 #include "libavutil/random_seed.h"
26 #include "audio.h"
27 #include "video.h"
28 
29 enum mode {
36 };
37 
38 typedef struct PermsContext {
39  const AVClass *class;
41  int64_t random_seed;
42  int mode;
43 } PermsContext;
44 
45 #define OFFSET(x) offsetof(PermsContext, x)
46 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
47 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
48 
49 static const AVOption options[] = {
50  { "mode", "select permissions mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_NONE}, MODE_NONE, NB_MODES-1, TFLAGS, "mode" },
51  { "none", "do nothing", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_NONE}, 0, 0, TFLAGS, "mode" },
52  { "ro", "set all output frames read-only", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RO}, 0, 0, TFLAGS, "mode" },
53  { "rw", "set all output frames writable", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RW}, 0, 0, TFLAGS, "mode" },
54  { "toggle", "switch permissions", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_TOGGLE}, 0, 0, TFLAGS, "mode" },
55  { "random", "set permissions randomly", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_RANDOM}, 0, 0, TFLAGS, "mode" },
56  { "seed", "set the seed for the random mode", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
57  { NULL }
58 };
59 
61 {
62  PermsContext *s = ctx->priv;
63  uint32_t seed;
64 
65  if (s->random_seed == -1)
66  s->random_seed = av_get_random_seed();
67  seed = s->random_seed;
68  av_log(ctx, AV_LOG_INFO, "random seed: 0x%08"PRIx32"\n", seed);
69  av_lfg_init(&s->lfg, seed);
70 
71  return 0;
72 }
73 
74 enum perm { RO, RW };
75 static const char * const perm_str[2] = { "RO", "RW" };
76 
78 {
79  int ret;
80  AVFilterContext *ctx = inlink->dst;
81  PermsContext *s = ctx->priv;
82  AVFrame *out = frame;
83  enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
84  enum perm out_perm;
85 
86  switch (s->mode) {
87  case MODE_TOGGLE: out_perm = in_perm == RO ? RW : RO; break;
88  case MODE_RANDOM: out_perm = av_lfg_get(&s->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;
92  }
93 
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)" : "");
97 
98  if (in_perm == RO && out_perm == RW) {
99  if ((ret = av_frame_make_writable(frame)) < 0)
100  return ret;
101  } else if (in_perm == RW && out_perm == RO) {
103  if (!out)
104  return AVERROR(ENOMEM);
105  }
106 
107  ret = ff_filter_frame(ctx->outputs[0], out);
108 
109  if (in_perm == RW && out_perm == RO)
111  return ret;
112 }
113 
114 AVFILTER_DEFINE_CLASS_EXT(perms, "(a)perms", options);
115 
116 #if CONFIG_APERMS_FILTER
117 
118 static const AVFilterPad aperms_inputs[] = {
119  {
120  .name = "default",
121  .type = AVMEDIA_TYPE_AUDIO,
122  .filter_frame = filter_frame,
123  },
124 };
125 
126 static const AVFilterPad aperms_outputs[] = {
127  {
128  .name = "default",
129  .type = AVMEDIA_TYPE_AUDIO,
130  },
131 };
132 
133 const AVFilter ff_af_aperms = {
134  .name = "aperms",
135  .description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
136  .priv_class = &perms_class,
137  .init = init,
138  .priv_size = sizeof(PermsContext),
139  FILTER_INPUTS(aperms_inputs),
140  FILTER_OUTPUTS(aperms_outputs),
143  .process_command = ff_filter_process_command,
144 };
145 #endif /* CONFIG_APERMS_FILTER */
146 
147 #if CONFIG_PERMS_FILTER
148 
149 static const AVFilterPad perms_inputs[] = {
150  {
151  .name = "default",
152  .type = AVMEDIA_TYPE_VIDEO,
153  .filter_frame = filter_frame,
154  },
155 };
156 
157 static const AVFilterPad perms_outputs[] = {
158  {
159  .name = "default",
160  .type = AVMEDIA_TYPE_VIDEO,
161  },
162 };
163 
164 const AVFilter ff_vf_perms = {
165  .name = "perms",
166  .description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
167  .init = init,
168  .priv_size = sizeof(PermsContext),
169  FILTER_INPUTS(perms_inputs),
170  FILTER_OUTPUTS(perms_outputs),
171  .priv_class = &perms_class,
174  .process_command = ff_filter_process_command,
175 };
176 #endif /* CONFIG_PERMS_FILTER */
MODE_NONE
@ MODE_NONE
Definition: f_perms.c:30
OFFSET
#define OFFSET(x)
Definition: f_perms.c:45
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:54
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
PermsContext::mode
int mode
Definition: f_perms.c:42
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
av_frame_make_writable
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:540
AVOption
AVOption.
Definition: opt.h:251
PermsContext::random_seed
int64_t random_seed
Definition: f_perms.c:41
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
video.h
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
PermsContext::lfg
AVLFG lfg
Definition: f_perms.c:40
perm
perm
Definition: f_perms.c:74
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:256
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
lfg.h
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:464
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
RO
@ RO
Definition: f_perms.c:74
TFLAGS
#define TFLAGS
Definition: f_perms.c:47
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
seed
static unsigned int seed
Definition: videogen.c:78
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
ff_af_aperms
const AVFilter ff_af_aperms
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:523
ff_vf_perms
const AVFilter ff_vf_perms
PermsContext
Definition: f_perms.c:38
ff_filter_process_command
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:863
perm_str
static const char *const perm_str[2]
Definition: f_perms.c:75
MODE_TOGGLE
@ MODE_TOGGLE
Definition: f_perms.c:33
MODE_RW
@ MODE_RW
Definition: f_perms.c:32
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:152
RW
@ RW
Definition: f_perms.c:74
init
static av_cold int init(AVFilterContext *ctx)
Definition: f_perms.c:60
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(perms, "(a)perms", options)
MODE_RANDOM
@ MODE_RANDOM
Definition: f_perms.c:34
AVFilter
Filter definition.
Definition: avfilter.h:171
NB_MODES
@ NB_MODES
Definition: f_perms.c:35
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
FLAGS
#define FLAGS
Definition: f_perms.c:46
MODE_RO
@ MODE_RO
Definition: f_perms.c:31
random_seed.h
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
options
static const AVOption options[]
Definition: f_perms.c:49
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:143
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: f_perms.c:77
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234