vf_select.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include "libavutil/eval.h"
27 #include "libavutil/fifo.h"
28 #include "libavutil/mathematics.h"
29 #include "avfilter.h"
30 
31 static const char *var_names[] = {
32  "E",
33  "PHI",
34  "PI",
35 
36  "TB",
37 
38  "pts",
39  "start_pts",
40  "prev_pts",
41  "prev_selected_pts",
42 
43  "t",
44  "start_t",
45  "prev_t",
46  "prev_selected_t",
47 
48  "pict_type",
49  "I",
50  "P",
51  "B",
52  "S",
53  "SI",
54  "SP",
55  "BI",
56 
57  "interlace_type",
58  "PROGRESSIVE",
59  "TOPFIRST",
60  "BOTTOMFIRST",
61 
62  "n",
63  "selected_n",
64  "prev_selected_n",
65 
66  "key",
67  "pos",
68 
69  NULL
70 };
71 
72 enum var_name {
76 
78 
83 
88 
97 
102 
106 
109 
111 };
112 
113 #define FIFO_SIZE 8
114 
115 typedef struct {
117  double var_values[VAR_VARS_NB];
118  double select;
121 } SelectContext;
122 
123 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
124 {
125  SelectContext *select = ctx->priv;
126  int ret;
127 
128  if ((ret = av_expr_parse(&select->expr, args ? args : "1",
129  var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
130  av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
131  return ret;
132  }
133 
135  if (!select->pending_frames) {
136  av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
137  return AVERROR(ENOMEM);
138  }
139  return 0;
140 }
141 
142 #define INTERLACE_TYPE_P 0
143 #define INTERLACE_TYPE_T 1
144 #define INTERLACE_TYPE_B 2
145 
146 static int config_input(AVFilterLink *inlink)
147 {
148  SelectContext *select = inlink->dst->priv;
149 
150  select->var_values[VAR_E] = M_E;
151  select->var_values[VAR_PHI] = M_PHI;
152  select->var_values[VAR_PI] = M_PI;
153 
154  select->var_values[VAR_N] = 0.0;
155  select->var_values[VAR_SELECTED_N] = 0.0;
156 
157  select->var_values[VAR_TB] = av_q2d(inlink->time_base);
158 
159  select->var_values[VAR_PREV_PTS] = NAN;
162  select->var_values[VAR_START_PTS] = NAN;
163  select->var_values[VAR_START_T] = NAN;
164 
170 
174 
175  return 0;
176 }
177 
178 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
179 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
180 
182 {
183  SelectContext *select = ctx->priv;
184  AVFilterLink *inlink = ctx->inputs[0];
185  double res;
186 
187  if (isnan(select->var_values[VAR_START_PTS]))
188  select->var_values[VAR_START_PTS] = TS2D(picref->pts);
189  if (isnan(select->var_values[VAR_START_T]))
190  select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
191 
192  select->var_values[VAR_PTS] = TS2D(picref->pts);
193  select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
194  select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
195  select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
196 
197  select->var_values[VAR_INTERLACE_TYPE] =
198  !picref->video->interlaced ? INTERLACE_TYPE_P :
200  select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
201 
202  res = av_expr_eval(select->expr, select->var_values, NULL);
203 
204  select->var_values[VAR_N] += 1.0;
205 
206  if (res) {
207  select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
209  select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
210  select->var_values[VAR_SELECTED_N] += 1.0;
211  }
212  return res;
213 }
214 
215 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
216 {
217  SelectContext *select = inlink->dst->priv;
218 
219  select->select = select_frame(inlink->dst, picref);
220  if (select->select) {
221  /* frame was requested through poll_frame */
222  if (select->cache_frames) {
223  if (!av_fifo_space(select->pending_frames))
224  av_log(inlink->dst, AV_LOG_ERROR,
225  "Buffering limit reached, cannot cache more frames\n");
226  else
227  av_fifo_generic_write(select->pending_frames, &picref,
228  sizeof(picref), NULL);
229  return;
230  }
231  avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
232  }
233 }
234 
235 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
236 {
237  SelectContext *select = inlink->dst->priv;
238 
239  if (select->select && !select->cache_frames)
240  avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
241 }
242 
243 static void end_frame(AVFilterLink *inlink)
244 {
245  SelectContext *select = inlink->dst->priv;
246  AVFilterBufferRef *picref = inlink->cur_buf;
247 
248  if (select->select) {
249  if (select->cache_frames)
250  return;
251  avfilter_end_frame(inlink->dst->outputs[0]);
252  }
253  avfilter_unref_buffer(picref);
254 }
255 
256 static int request_frame(AVFilterLink *outlink)
257 {
258  AVFilterContext *ctx = outlink->src;
259  SelectContext *select = ctx->priv;
260  AVFilterLink *inlink = outlink->src->inputs[0];
261  select->select = 0;
262 
263  if (av_fifo_size(select->pending_frames)) {
264  AVFilterBufferRef *picref;
265  av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
266  avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
267  avfilter_draw_slice(outlink, 0, outlink->h, 1);
268  avfilter_end_frame(outlink);
269  avfilter_unref_buffer(picref);
270  return 0;
271  }
272 
273  while (!select->select) {
274  int ret = avfilter_request_frame(inlink);
275  if (ret < 0)
276  return ret;
277  }
278 
279  return 0;
280 }
281 
282 static int poll_frame(AVFilterLink *outlink)
283 {
284  SelectContext *select = outlink->src->priv;
285  AVFilterLink *inlink = outlink->src->inputs[0];
286  int count, ret;
287 
288  if (!av_fifo_size(select->pending_frames)) {
289  if ((count = avfilter_poll_frame(inlink)) <= 0)
290  return count;
291  /* request frame from input, and apply select condition to it */
292  select->cache_frames = 1;
293  while (count-- && av_fifo_space(select->pending_frames)) {
294  ret = avfilter_request_frame(inlink);
295  if (ret < 0)
296  break;
297  }
298  select->cache_frames = 0;
299  }
300 
301  return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
302 }
303 
304 static av_cold void uninit(AVFilterContext *ctx)
305 {
306  SelectContext *select = ctx->priv;
307  AVFilterBufferRef *picref;
308 
309  av_expr_free(select->expr);
310  select->expr = NULL;
311 
312  while (select->pending_frames &&
313  av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
314  avfilter_unref_buffer(picref);
315  av_fifo_free(select->pending_frames);
316  select->pending_frames = NULL;
317 }
318 
320  .name = "select",
321  .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
322  .init = init,
323  .uninit = uninit,
324 
325  .priv_size = sizeof(SelectContext),
326 
327  .inputs = (AVFilterPad[]) {{ .name = "default",
328  .type = AVMEDIA_TYPE_VIDEO,
329  .get_video_buffer = avfilter_null_get_video_buffer,
330  .config_props = config_input,
331  .start_frame = start_frame,
332  .draw_slice = draw_slice,
333  .end_frame = end_frame },
334  { .name = NULL }},
335  .outputs = (AVFilterPad[]) {{ .name = "default",
336  .type = AVMEDIA_TYPE_VIDEO,
337  .poll_frame = poll_frame,
338  .request_frame = request_frame, },
339  { .name = NULL}},
340 };
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:105
int av_fifo_size(AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:52
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:141
static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
Definition: vf_select.c:181
static const char * var_names[]
Definition: vf_select.c:31
static int config_input(AVFilterLink *inlink)
Definition: vf_select.c:146
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:465
Switching Intra.
Definition: avutil.h:300
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:547
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
Definition: vf_select.c:123
#define av_cold
Definition: attributes.h:71
int top_field_first
field order
Definition: avfilter.h:111
AVFilter avfilter_vf_select
Definition: vf_select.c:319
void avfilter_end_frame(AVFilterLink *link)
Notifie the next filter that the current frame has finished.
Definition: avfilter.c:430
Definition: eval.c:119
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:82
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
Definition: vf_select.c:215
#define INTERLACE_TYPE_P
Definition: vf_select.c:142
var_name
Definition: vf_boxblur.c:43
int64_t pts
presentation timestamp.
Definition: avfilter.h:135
A filter pad used for either input or output.
Definition: avfilter.h:312
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:25
double var_values[VAR_VARS_NB]
Definition: vf_select.c:117
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void * priv
private data for use by the filter
Definition: avfilter.h:553
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
int avfilter_poll_frame(AVFilterLink *link)
Poll a frame from the filter chain.
Definition: avfilter.c:380
#define NAN
Definition: mathematics.h:54
int av_fifo_space(AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
Definition: fifo.c:57
#define M_E
Definition: ratecontrol.c:39
static void end_frame(AVFilterLink *inlink)
Definition: vf_select.c:243
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Send a slice to the next filter.
Definition: avfilter.c:447
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:38
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: avfilter.c:73
A reference to an AVFilterBuffer.
Definition: avfilter.h:124
NULL
Definition: eval.c:50
static int poll_frame(AVFilterLink *outlink)
Definition: vf_select.c:282
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:185
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
a very simple circular buffer FIFO implementation
enum AVPictureType pict_type
picture type of the frame
Definition: avfilter.h:112
Switching Predicted.
Definition: avutil.h:301
Filter definition.
Definition: avfilter.h:497
AVFilterBufferRef * avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
Add a new reference to a buffer.
Definition: avfilter.c:47
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Notify the next filter of the start of a frame.
Definition: avfilter.c:400
#define M_PHI
Definition: mathematics.h:42
int avfilter_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:369
const char * name
filter name
Definition: avfilter.h:498
AVFilterBufferRef * avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
get_video_buffer() handler for filters which simply pass video along
Definition: defaults.c:288
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:551
#define INTERLACE_TYPE_B
Definition: vf_select.c:144
#define FIFO_SIZE
Definition: vf_select.c:113
int interlaced
is frame interlaced
Definition: avfilter.h:110
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_select.c:304
Bi-dir predicted.
Definition: avutil.h:298
AVFifoBuffer * pending_frames
FIFO buffer of video frames.
Definition: vf_select.c:120
static int request_frame(AVFilterLink *outlink)
Definition: vf_select.c:256
int64_t pos
byte position in stream, -1 if unknown
Definition: avfilter.h:136
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:515
AVExpr * expr
Definition: vf_select.c:116
An instance of a filter.
Definition: avfilter.h:538
#define INTERLACE_TYPE_T
Definition: vf_select.c:143
int cache_frames
Definition: vf_select.c:119
double select
Definition: vf_select.c:118
#define M_PI
Definition: cos_tablegen.c:28
Predicted.
Definition: avutil.h:297
static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
Definition: vf_select.c:235
simple arithmetic expression evaluator
#define TS2D(ts)
Definition: vf_select.c:179