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 "libavutil/lfg.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/random_seed.h"
24 #include "audio.h"
25 #include "video.h"
26 
27 enum mode {
34 };
35 
36 typedef struct {
37  const AVClass *class;
39  int64_t random_seed;
40  enum mode mode;
41 } PermsContext;
42 
43 #define OFFSET(x) offsetof(PermsContext, x)
44 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
45 
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 },
54  { NULL }
55 };
56 
57 static av_cold int init(AVFilterContext *ctx)
58 {
59  PermsContext *perms = ctx->priv;
60 
61  if (perms->mode == MODE_RANDOM) {
62  uint32_t seed;
63 
64  if (perms->random_seed == -1)
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);
69  }
70 
71  return 0;
72 }
73 
74 enum perm { RO, RW };
75 static const char *perm_str[2] = { "RO", "RW" };
76 
77 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
78 {
79  int ret;
80  AVFilterContext *ctx = inlink->dst;
81  PermsContext *perms = 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 (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;
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) {
102  out = av_frame_clone(frame);
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)
110  av_frame_free(&frame);
111  return ret;
112 }
113 
114 #if CONFIG_APERMS_FILTER
115 
116 #define aperms_options options
117 AVFILTER_DEFINE_CLASS(aperms);
118 
119 static const AVFilterPad aperms_inputs[] = {
120  {
121  .name = "default",
122  .type = AVMEDIA_TYPE_AUDIO,
123  .filter_frame = filter_frame,
124  },
125  { NULL }
126 };
127 
128 static const AVFilterPad aperms_outputs[] = {
129  {
130  .name = "default",
131  .type = AVMEDIA_TYPE_AUDIO,
132  },
133  { NULL }
134 };
135 
136 AVFilter avfilter_af_aperms = {
137  .name = "aperms",
138  .description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
139  .init = init,
140  .priv_size = sizeof(PermsContext),
141  .inputs = aperms_inputs,
142  .outputs = aperms_outputs,
143  .priv_class = &aperms_class,
144 };
145 #endif /* CONFIG_APERMS_FILTER */
146 
147 #if CONFIG_PERMS_FILTER
148 
149 #define perms_options options
150 AVFILTER_DEFINE_CLASS(perms);
151 
152 static const AVFilterPad perms_inputs[] = {
153  {
154  .name = "default",
155  .type = AVMEDIA_TYPE_VIDEO,
156  .filter_frame = filter_frame,
157  },
158  { NULL }
159 };
160 
161 static const AVFilterPad perms_outputs[] = {
162  {
163  .name = "default",
164  .type = AVMEDIA_TYPE_VIDEO,
165  },
166  { NULL }
167 };
168 
169 AVFilter avfilter_vf_perms = {
170  .name = "perms",
171  .description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
172  .init = init,
173  .priv_size = sizeof(PermsContext),
174  .inputs = perms_inputs,
175  .outputs = perms_outputs,
176  .priv_class = &perms_class,
177 };
178 #endif /* CONFIG_PERMS_FILTER */
Definition: lfg.h:25
Definition: f_perms.c:74
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
const char * name
Pad name.
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
mode
Definition: f_perms.c:27
AVOptions.
static const AVOption options[]
Definition: f_perms.c:46
frame
Definition: stft.m:14
A filter pad used for either input or output.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:377
AVLFG lfg
Definition: f_perms.c:38
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:361
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int64_t random_seed
Definition: f_perms.c:39
#define AV_LOG_VERBOSE
Definition: log.h:157
Definition: f_perms.c:74
ret
Definition: avfilter.c:821
#define FLAGS
Definition: f_perms.c:44
perm
Definition: f_perms.c:74
static const char * perm_str[2]
Definition: f_perms.c:75
AVFrame * av_frame_clone(AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:317
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: f_perms.c:77
NULL
Definition: eval.c:55
static unsigned int seed
Definition: videogen.c:78
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
const char * name
filter name
Definition: avfilter.h:437
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
#define OFFSET(x)
Definition: f_perms.c:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
enum mode mode
Definition: f_perms.c:40
#define AVFILTER_DEFINE_CLASS(fname)
An instance of a filter.
Definition: avfilter.h:524
#define AV_LOG_INFO
Definition: log.h:156
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:105
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 inputs
static av_cold int init(AVFilterContext *ctx)
Definition: f_perms.c:57