2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of Libav.
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.
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.
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
23 * filter for selecting which frame passes in the filterchain
26 #include "libavutil/eval.h"
27 #include "libavutil/fifo.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
35 static const char *const var_names[] = {
36 "E", ///< Euler number
37 "PHI", ///< golden ratio
42 "pts", ///< original pts in the file of the frame
43 "start_pts", ///< first PTS in the stream, expressed in TB units
44 "prev_pts", ///< previous frame PTS
45 "prev_selected_pts", ///< previous selected frame PTS
47 "t", ///< first PTS in seconds
48 "start_t", ///< first PTS in the stream, expressed in seconds
49 "prev_t", ///< previous frame time
50 "prev_selected_t", ///< previously selected time
52 "pict_type", ///< the type of picture in the movie
61 "interlace_type", ///< the frame interlace type
66 "n", ///< frame number (starting from zero)
67 "selected_n", ///< selected frame number (starting from zero)
68 "prev_selected_n", ///< number of the last selected frame
70 "key", ///< tell if the frame is a key frame
71 "pos", ///< original position in the file of the frame
86 VAR_PREV_SELECTED_PTS,
103 VAR_INTERLACE_TYPE_P,
104 VAR_INTERLACE_TYPE_T,
105 VAR_INTERLACE_TYPE_B,
118 typedef struct SelectContext {
119 const AVClass *class;
122 double var_values[VAR_VARS_NB];
125 AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
128 static av_cold int init(AVFilterContext *ctx)
130 SelectContext *select = ctx->priv;
133 if ((ret = av_expr_parse(&select->expr, select->expr_str,
134 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
135 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n",
140 select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFrame*));
141 if (!select->pending_frames) {
142 av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
143 return AVERROR(ENOMEM);
148 #define INTERLACE_TYPE_P 0
149 #define INTERLACE_TYPE_T 1
150 #define INTERLACE_TYPE_B 2
152 static int config_input(AVFilterLink *inlink)
154 SelectContext *select = inlink->dst->priv;
156 select->var_values[VAR_E] = M_E;
157 select->var_values[VAR_PHI] = M_PHI;
158 select->var_values[VAR_PI] = M_PI;
160 select->var_values[VAR_N] = 0.0;
161 select->var_values[VAR_SELECTED_N] = 0.0;
163 select->var_values[VAR_TB] = av_q2d(inlink->time_base);
165 select->var_values[VAR_PREV_PTS] = NAN;
166 select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
167 select->var_values[VAR_PREV_SELECTED_T] = NAN;
168 select->var_values[VAR_START_PTS] = NAN;
169 select->var_values[VAR_START_T] = NAN;
171 select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
172 select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
173 select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
174 select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
175 select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
177 select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
178 select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
179 select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;;
184 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
185 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
187 static int select_frame(AVFilterContext *ctx, AVFrame *frame)
189 SelectContext *select = ctx->priv;
190 AVFilterLink *inlink = ctx->inputs[0];
193 if (isnan(select->var_values[VAR_START_PTS]))
194 select->var_values[VAR_START_PTS] = TS2D(frame->pts);
195 if (isnan(select->var_values[VAR_START_T]))
196 select->var_values[VAR_START_T] = TS2D(frame->pts) * av_q2d(inlink->time_base);
198 select->var_values[VAR_PTS] = TS2D(frame->pts);
199 select->var_values[VAR_T ] = TS2D(frame->pts) * av_q2d(inlink->time_base);
200 select->var_values[VAR_PREV_PTS] = TS2D(frame->pts);
202 select->var_values[VAR_INTERLACE_TYPE] =
203 !frame->interlaced_frame ? INTERLACE_TYPE_P :
204 frame->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
205 select->var_values[VAR_PICT_TYPE] = frame->pict_type;
207 res = av_expr_eval(select->expr, select->var_values, NULL);
209 select->var_values[VAR_N] += 1.0;
212 select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
213 select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
214 select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
215 select->var_values[VAR_SELECTED_N] += 1.0;
220 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
222 SelectContext *select = inlink->dst->priv;
224 select->select = select_frame(inlink->dst, frame);
225 if (select->select) {
226 /* frame was requested through poll_frame */
227 if (select->cache_frames) {
228 if (!av_fifo_space(select->pending_frames)) {
229 av_log(inlink->dst, AV_LOG_ERROR,
230 "Buffering limit reached, cannot cache more frames\n");
231 av_frame_free(&frame);
233 av_fifo_generic_write(select->pending_frames, &frame,
234 sizeof(frame), NULL);
237 return ff_filter_frame(inlink->dst->outputs[0], frame);
240 av_frame_free(&frame);
244 static int request_frame(AVFilterLink *outlink)
246 AVFilterContext *ctx = outlink->src;
247 SelectContext *select = ctx->priv;
248 AVFilterLink *inlink = outlink->src->inputs[0];
251 if (av_fifo_size(select->pending_frames)) {
254 av_fifo_generic_read(select->pending_frames, &frame, sizeof(frame), NULL);
255 return ff_filter_frame(outlink, frame);
258 while (!select->select) {
259 int ret = ff_request_frame(inlink);
267 static int poll_frame(AVFilterLink *outlink)
269 SelectContext *select = outlink->src->priv;
270 AVFilterLink *inlink = outlink->src->inputs[0];
273 if (!av_fifo_size(select->pending_frames)) {
274 if ((count = ff_poll_frame(inlink)) <= 0)
276 /* request frame from input, and apply select condition to it */
277 select->cache_frames = 1;
278 while (count-- && av_fifo_space(select->pending_frames)) {
279 ret = ff_request_frame(inlink);
283 select->cache_frames = 0;
286 return av_fifo_size(select->pending_frames)/sizeof(AVFrame*);
289 static av_cold void uninit(AVFilterContext *ctx)
291 SelectContext *select = ctx->priv;
294 av_expr_free(select->expr);
297 while (select->pending_frames &&
298 av_fifo_generic_read(select->pending_frames, &frame, sizeof(frame), NULL) == sizeof(frame))
299 av_frame_free(&frame);
300 av_fifo_free(select->pending_frames);
301 select->pending_frames = NULL;
304 #define OFFSET(x) offsetof(SelectContext, x)
305 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
306 static const AVOption options[] = {
307 { "expr", "An expression to use for selecting frames", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS },
311 static const AVClass select_class = {
312 .class_name = "select",
313 .item_name = av_default_item_name,
315 .version = LIBAVUTIL_VERSION_INT,
318 static const AVFilterPad avfilter_vf_select_inputs[] = {
321 .type = AVMEDIA_TYPE_VIDEO,
322 .get_video_buffer = ff_null_get_video_buffer,
323 .config_props = config_input,
324 .filter_frame = filter_frame,
329 static const AVFilterPad avfilter_vf_select_outputs[] = {
332 .type = AVMEDIA_TYPE_VIDEO,
333 .poll_frame = poll_frame,
334 .request_frame = request_frame,
339 AVFilter ff_vf_select = {
341 .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
345 .priv_size = sizeof(SelectContext),
346 .priv_class = &select_class,
348 .inputs = avfilter_vf_select_inputs,
349 .outputs = avfilter_vf_select_outputs,