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"
34 static const char *const var_names
[] = {
35 "E", ///< Euler number
36 "PHI", ///< golden ratio
41 "pts", ///< original pts in the file of the frame
42 "start_pts", ///< first PTS in the stream, expressed in TB units
43 "prev_pts", ///< previous frame PTS
44 "prev_selected_pts", ///< previous selected frame PTS
46 "t", ///< first PTS in seconds
47 "start_t", ///< first PTS in the stream, expressed in seconds
48 "prev_t", ///< previous frame time
49 "prev_selected_t", ///< previously selected time
51 "pict_type", ///< the type of picture in the movie
60 "interlace_type", ///< the frame interlace type
65 "n", ///< frame number (starting from zero)
66 "selected_n", ///< selected frame number (starting from zero)
67 "prev_selected_n", ///< number of the last selected frame
69 "key", ///< tell if the frame is a key frame
70 "pos", ///< original position in the file of the frame
85 VAR_PREV_SELECTED_PTS
,
102 VAR_INTERLACE_TYPE_P
,
103 VAR_INTERLACE_TYPE_T
,
104 VAR_INTERLACE_TYPE_B
,
119 double var_values
[VAR_VARS_NB
];
122 AVFifoBuffer
*pending_frames
; ///< FIFO buffer of video frames
125 static av_cold
int init(AVFilterContext
*ctx
, const char *args
)
127 SelectContext
*select
= ctx
->priv
;
130 if ((ret
= av_expr_parse(&select
->expr
, args
? args
: "1",
131 var_names
, NULL
, NULL
, NULL
, NULL
, 0, ctx
)) < 0) {
132 av_log(ctx
, AV_LOG_ERROR
, "Error while parsing expression '%s'\n", args
);
136 select
->pending_frames
= av_fifo_alloc(FIFO_SIZE
*sizeof(AVFrame
*));
137 if (!select
->pending_frames
) {
138 av_log(ctx
, AV_LOG_ERROR
, "Failed to allocate pending frames buffer.\n");
139 return AVERROR(ENOMEM
);
144 #define INTERLACE_TYPE_P 0
145 #define INTERLACE_TYPE_T 1
146 #define INTERLACE_TYPE_B 2
148 static int config_input(AVFilterLink
*inlink
)
150 SelectContext
*select
= inlink
->dst
->priv
;
152 select
->var_values
[VAR_E
] = M_E
;
153 select
->var_values
[VAR_PHI
] = M_PHI
;
154 select
->var_values
[VAR_PI
] = M_PI
;
156 select
->var_values
[VAR_N
] = 0.0;
157 select
->var_values
[VAR_SELECTED_N
] = 0.0;
159 select
->var_values
[VAR_TB
] = av_q2d(inlink
->time_base
);
161 select
->var_values
[VAR_PREV_PTS
] = NAN
;
162 select
->var_values
[VAR_PREV_SELECTED_PTS
] = NAN
;
163 select
->var_values
[VAR_PREV_SELECTED_T
] = NAN
;
164 select
->var_values
[VAR_START_PTS
] = NAN
;
165 select
->var_values
[VAR_START_T
] = NAN
;
167 select
->var_values
[VAR_PICT_TYPE_I
] = AV_PICTURE_TYPE_I
;
168 select
->var_values
[VAR_PICT_TYPE_P
] = AV_PICTURE_TYPE_P
;
169 select
->var_values
[VAR_PICT_TYPE_B
] = AV_PICTURE_TYPE_B
;
170 select
->var_values
[VAR_PICT_TYPE_SI
] = AV_PICTURE_TYPE_SI
;
171 select
->var_values
[VAR_PICT_TYPE_SP
] = AV_PICTURE_TYPE_SP
;
173 select
->var_values
[VAR_INTERLACE_TYPE_P
] = INTERLACE_TYPE_P
;
174 select
->var_values
[VAR_INTERLACE_TYPE_T
] = INTERLACE_TYPE_T
;
175 select
->var_values
[VAR_INTERLACE_TYPE_B
] = INTERLACE_TYPE_B
;;
180 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
181 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
183 static int select_frame(AVFilterContext
*ctx
, AVFrame
*frame
)
185 SelectContext
*select
= ctx
->priv
;
186 AVFilterLink
*inlink
= ctx
->inputs
[0];
189 if (isnan(select
->var_values
[VAR_START_PTS
]))
190 select
->var_values
[VAR_START_PTS
] = TS2D(frame
->pts
);
191 if (isnan(select
->var_values
[VAR_START_T
]))
192 select
->var_values
[VAR_START_T
] = TS2D(frame
->pts
) * av_q2d(inlink
->time_base
);
194 select
->var_values
[VAR_PTS
] = TS2D(frame
->pts
);
195 select
->var_values
[VAR_T
] = TS2D(frame
->pts
) * av_q2d(inlink
->time_base
);
196 select
->var_values
[VAR_PREV_PTS
] = TS2D(frame
->pts
);
198 select
->var_values
[VAR_INTERLACE_TYPE
] =
199 !frame
->interlaced_frame
? INTERLACE_TYPE_P
:
200 frame
->top_field_first
? INTERLACE_TYPE_T
: INTERLACE_TYPE_B
;
201 select
->var_values
[VAR_PICT_TYPE
] = frame
->pict_type
;
203 res
= av_expr_eval(select
->expr
, select
->var_values
, NULL
);
204 av_log(inlink
->dst
, AV_LOG_DEBUG
,
205 "n:%d pts:%d t:%f interlace_type:%c key:%d pict_type:%c "
207 (int)select
->var_values
[VAR_N
],
208 (int)select
->var_values
[VAR_PTS
],
209 select
->var_values
[VAR_T
],
210 select
->var_values
[VAR_INTERLACE_TYPE
] == INTERLACE_TYPE_P
? 'P' :
211 select
->var_values
[VAR_INTERLACE_TYPE
] == INTERLACE_TYPE_T
? 'T' :
212 select
->var_values
[VAR_INTERLACE_TYPE
] == INTERLACE_TYPE_B
? 'B' : '?',
213 (int)select
->var_values
[VAR_KEY
],
214 av_get_picture_type_char(select
->var_values
[VAR_PICT_TYPE
]),
217 select
->var_values
[VAR_N
] += 1.0;
220 select
->var_values
[VAR_PREV_SELECTED_N
] = select
->var_values
[VAR_N
];
221 select
->var_values
[VAR_PREV_SELECTED_PTS
] = select
->var_values
[VAR_PTS
];
222 select
->var_values
[VAR_PREV_SELECTED_T
] = select
->var_values
[VAR_T
];
223 select
->var_values
[VAR_SELECTED_N
] += 1.0;
228 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*frame
)
230 SelectContext
*select
= inlink
->dst
->priv
;
232 select
->select
= select_frame(inlink
->dst
, frame
);
233 if (select
->select
) {
234 /* frame was requested through poll_frame */
235 if (select
->cache_frames
) {
236 if (!av_fifo_space(select
->pending_frames
)) {
237 av_log(inlink
->dst
, AV_LOG_ERROR
,
238 "Buffering limit reached, cannot cache more frames\n");
239 av_frame_free(&frame
);
241 av_fifo_generic_write(select
->pending_frames
, &frame
,
242 sizeof(frame
), NULL
);
245 return ff_filter_frame(inlink
->dst
->outputs
[0], frame
);
248 av_frame_free(&frame
);
252 static int request_frame(AVFilterLink
*outlink
)
254 AVFilterContext
*ctx
= outlink
->src
;
255 SelectContext
*select
= ctx
->priv
;
256 AVFilterLink
*inlink
= outlink
->src
->inputs
[0];
259 if (av_fifo_size(select
->pending_frames
)) {
262 av_fifo_generic_read(select
->pending_frames
, &frame
, sizeof(frame
), NULL
);
263 return ff_filter_frame(outlink
, frame
);
266 while (!select
->select
) {
267 int ret
= ff_request_frame(inlink
);
275 static int poll_frame(AVFilterLink
*outlink
)
277 SelectContext
*select
= outlink
->src
->priv
;
278 AVFilterLink
*inlink
= outlink
->src
->inputs
[0];
281 if (!av_fifo_size(select
->pending_frames
)) {
282 if ((count
= ff_poll_frame(inlink
)) <= 0)
284 /* request frame from input, and apply select condition to it */
285 select
->cache_frames
= 1;
286 while (count
-- && av_fifo_space(select
->pending_frames
)) {
287 ret
= ff_request_frame(inlink
);
291 select
->cache_frames
= 0;
294 return av_fifo_size(select
->pending_frames
)/sizeof(AVFrame
*);
297 static av_cold
void uninit(AVFilterContext
*ctx
)
299 SelectContext
*select
= ctx
->priv
;
302 av_expr_free(select
->expr
);
305 while (select
->pending_frames
&&
306 av_fifo_generic_read(select
->pending_frames
, &frame
, sizeof(frame
), NULL
) == sizeof(frame
))
307 av_frame_free(&frame
);
308 av_fifo_free(select
->pending_frames
);
309 select
->pending_frames
= NULL
;
312 static const AVFilterPad avfilter_vf_select_inputs
[] = {
315 .type
= AVMEDIA_TYPE_VIDEO
,
316 .get_video_buffer
= ff_null_get_video_buffer
,
317 .config_props
= config_input
,
318 .filter_frame
= filter_frame
,
323 static const AVFilterPad avfilter_vf_select_outputs
[] = {
326 .type
= AVMEDIA_TYPE_VIDEO
,
327 .poll_frame
= poll_frame
,
328 .request_frame
= request_frame
,
333 AVFilter avfilter_vf_select
= {
335 .description
= NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
339 .priv_size
= sizeof(SelectContext
),
341 .inputs
= avfilter_vf_select_inputs
,
342 .outputs
= avfilter_vf_select_outputs
,