vf_framestep.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
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 /**
22  * @file framestep filter, inspired on libmpcodecs/vf_framestep.c by
23  * Daniele Fornighieri <guru AT digitalfantasy it>.
24  */
25 
26 #include "libavutil/opt.h"
27 #include "avfilter.h"
28 #include "internal.h"
29 #include "video.h"
30 
31 typedef struct {
32  const AVClass *class;
35 
36 #define OFFSET(x) offsetof(FrameStepContext, x)
37 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
38 
39 static const AVOption framestep_options[] = {
40  { "step", "set frame step", OFFSET(frame_step), AV_OPT_TYPE_INT, {.i64=1}, 1, INT_MAX, FLAGS},
41  {NULL},
42 };
43 
44 AVFILTER_DEFINE_CLASS(framestep);
45 
46 static int config_output_props(AVFilterLink *outlink)
47 {
48  AVFilterContext *ctx = outlink->src;
49  FrameStepContext *framestep = ctx->priv;
50  AVFilterLink *inlink = ctx->inputs[0];
51 
52  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
53  outlink->frame_rate =
54  av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
55 
56  av_log(ctx, AV_LOG_VERBOSE, "step:%d frame_rate:%d/%d(%f) -> frame_rate:%d/%d(%f)\n",
57  framestep->frame_step,
58  inlink->frame_rate.num, inlink->frame_rate.den, av_q2d(inlink->frame_rate),
59  outlink->frame_rate.num, outlink->frame_rate.den, av_q2d(outlink->frame_rate));
60  return 0;
61 }
62 
63 static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
64 {
65  FrameStepContext *framestep = inlink->dst->priv;
66 
67  if (!(framestep->frame_count++ % framestep->frame_step)) {
68  return ff_filter_frame(inlink->dst->outputs[0], ref);
69  } else {
70  av_frame_free(&ref);
71  return 0;
72  }
73 }
74 
75 static const AVFilterPad framestep_inputs[] = {
76  {
77  .name = "default",
78  .type = AVMEDIA_TYPE_VIDEO,
79  .filter_frame = filter_frame,
80  },
81  { NULL }
82 };
83 
84 static const AVFilterPad framestep_outputs[] = {
85  {
86  .name = "default",
87  .type = AVMEDIA_TYPE_VIDEO,
88  .config_props = config_output_props,
89  },
90  { NULL }
91 };
92 
94  .name = "framestep",
95  .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
96  .priv_size = sizeof(FrameStepContext),
97  .priv_class = &framestep_class,
98  .inputs = framestep_inputs,
99  .outputs = framestep_outputs,
100 };
static const AVOption framestep_options[]
Definition: vf_framestep.c:39
#define FLAGS
Definition: vf_framestep.c:37
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
FIXME Range Coding of cr are ref
Definition: snow.txt:367
AVOption.
Definition: opt.h:251
static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
Definition: vf_framestep.c:63
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
int num
numerator
Definition: rational.h:44
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
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
AVOptions.
static int config_output_props(AVFilterLink *outlink)
Definition: vf_framestep.c:46
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
A filter pad used for either input or output.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
void * priv
private data for use by the filter
Definition: avfilter.h:545
static const AVFilterPad framestep_inputs[]
Definition: vf_framestep.c:75
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
AVFilter avfilter_vf_framestep
Definition: vf_framestep.c:93
#define AV_LOG_VERBOSE
Definition: log.h:157
static const AVFilterPad framestep_outputs[]
Definition: vf_framestep.c:84
#define OFFSET(x)
Definition: vf_framestep.c:36
NULL
Definition: eval.c:55
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
rational number numerator/denominator
Definition: rational.h:43
const char * name
filter name
Definition: avfilter.h:437
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
static int frame_count
Definition: muxing.c:234
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
AVFILTER_DEFINE_CLASS(framestep)
int den
denominator
Definition: rational.h:45
An instance of a filter.
Definition: avfilter.h:524
internal API functions
Frame requests may need to loop in order to be fulfilled.
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