2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * filter for selecting which frame passes in the filterchain
26 #include "libavutil/eval.h"
27 #include "libavutil/fifo.h"
30 static const char *var_names[] = {
31 "E", ///< Euler number
32 "PHI", ///< golden ratio
37 "pts", ///< original pts in the file of the frame
38 "start_pts", ///< first PTS in the stream, expressed in TB units
39 "prev_pts", ///< previous frame PTS
40 "prev_selected_pts", ///< previous selected frame PTS
42 "t", ///< first PTS in seconds
43 "start_t", ///< first PTS in the stream, expressed in seconds
44 "prev_t", ///< previous frame time
45 "prev_selected_t", ///< previously selected time
47 "pict_type", ///< the type of picture in the movie
56 "interlace_type", ///< the frame interlace type
61 "n", ///< frame number (starting from zero)
62 "selected_n", ///< selected frame number (starting from zero)
63 "prev_selected_n", ///< number of the last selected frame
65 "key", ///< tell if the frame is a key frame
66 "pos", ///< original position in the file of the frame
81 VAR_PREV_SELECTED_PTS,
100 VAR_INTERLACE_TYPE_B,
116 double var_values[VAR_VARS_NB];
119 AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
122 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
124 SelectContext *select = ctx->priv;
127 if ((ret = av_expr_parse(&select->expr, args ? args : "1",
128 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
129 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
133 select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
134 if (!select->pending_frames) {
135 av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
136 return AVERROR(ENOMEM);
141 #define INTERLACE_TYPE_P 0
142 #define INTERLACE_TYPE_T 1
143 #define INTERLACE_TYPE_B 2
145 static int config_input(AVFilterLink *inlink)
147 SelectContext *select = inlink->dst->priv;
149 select->var_values[VAR_E] = M_E;
150 select->var_values[VAR_PHI] = M_PHI;
151 select->var_values[VAR_PI] = M_PI;
153 select->var_values[VAR_N] = 0.0;
154 select->var_values[VAR_SELECTED_N] = 0.0;
156 select->var_values[VAR_TB] = av_q2d(inlink->time_base);
158 select->var_values[VAR_PREV_PTS] = NAN;
159 select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
160 select->var_values[VAR_PREV_SELECTED_T] = NAN;
161 select->var_values[VAR_START_PTS] = NAN;
162 select->var_values[VAR_START_T] = NAN;
164 select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
165 select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
166 select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
167 select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
168 select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
170 select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
171 select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
172 select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;;
177 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
178 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
180 static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
182 SelectContext *select = ctx->priv;
183 AVFilterLink *inlink = ctx->inputs[0];
186 if (isnan(select->var_values[VAR_START_PTS]))
187 select->var_values[VAR_START_PTS] = TS2D(picref->pts);
188 if (isnan(select->var_values[VAR_START_T]))
189 select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
191 select->var_values[VAR_PTS] = TS2D(picref->pts);
192 select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
193 select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
194 select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
196 select->var_values[VAR_INTERLACE_TYPE] =
197 !picref->video->interlaced ? INTERLACE_TYPE_P :
198 picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
199 select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
201 res = av_expr_eval(select->expr, select->var_values, NULL);
202 av_log(inlink->dst, AV_LOG_DEBUG,
203 "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
205 (int)select->var_values[VAR_N],
206 (int)select->var_values[VAR_PTS],
207 select->var_values[VAR_T],
208 (int)select->var_values[VAR_POS],
209 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
210 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
211 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
212 (int)select->var_values[VAR_KEY],
213 av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
216 select->var_values[VAR_N] += 1.0;
219 select->var_values[VAR_PREV_SELECTED_N] = select->var_values[VAR_N];
220 select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
221 select->var_values[VAR_PREV_SELECTED_T] = select->var_values[VAR_T];
222 select->var_values[VAR_SELECTED_N] += 1.0;
227 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
229 SelectContext *select = inlink->dst->priv;
231 select->select = select_frame(inlink->dst, picref);
232 if (select->select) {
233 /* frame was requested through poll_frame */
234 if (select->cache_frames) {
235 if (!av_fifo_space(select->pending_frames))
236 av_log(inlink->dst, AV_LOG_ERROR,
237 "Buffering limit reached, cannot cache more frames\n");
239 av_fifo_generic_write(select->pending_frames, &picref,
240 sizeof(picref), NULL);
243 avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
247 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
249 SelectContext *select = inlink->dst->priv;
251 if (select->select && !select->cache_frames)
252 avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
255 static void end_frame(AVFilterLink *inlink)
257 SelectContext *select = inlink->dst->priv;
258 AVFilterBufferRef *picref = inlink->cur_buf;
260 if (select->select) {
261 if (select->cache_frames)
263 avfilter_end_frame(inlink->dst->outputs[0]);
265 avfilter_unref_buffer(picref);
268 static int request_frame(AVFilterLink *outlink)
270 AVFilterContext *ctx = outlink->src;
271 SelectContext *select = ctx->priv;
272 AVFilterLink *inlink = outlink->src->inputs[0];
275 if (av_fifo_size(select->pending_frames)) {
276 AVFilterBufferRef *picref;
277 av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
278 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
279 avfilter_draw_slice(outlink, 0, outlink->h, 1);
280 avfilter_end_frame(outlink);
281 avfilter_unref_buffer(picref);
285 while (!select->select) {
286 int ret = avfilter_request_frame(inlink);
294 static int poll_frame(AVFilterLink *outlink)
296 SelectContext *select = outlink->src->priv;
297 AVFilterLink *inlink = outlink->src->inputs[0];
300 if (!av_fifo_size(select->pending_frames)) {
301 if ((count = avfilter_poll_frame(inlink)) <= 0)
303 /* request frame from input, and apply select condition to it */
304 select->cache_frames = 1;
305 while (count-- && av_fifo_space(select->pending_frames)) {
306 ret = avfilter_request_frame(inlink);
310 select->cache_frames = 0;
313 return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
316 static av_cold void uninit(AVFilterContext *ctx)
318 SelectContext *select = ctx->priv;
319 AVFilterBufferRef *picref;
323 av_expr_free(select->expr);
326 if (select->pending_frames) {
327 for (i = 0; i < av_fifo_size(select->pending_frames)/sizeof(picref); i++) {
328 av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
329 avfilter_unref_buffer(picref);
331 av_fifo_free(select->pending_frames);
333 select->pending_frames = NULL;
336 AVFilter avfilter_vf_select = {
338 .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
342 .priv_size = sizeof(SelectContext),
344 .inputs = (AVFilterPad[]) {{ .name = "default",
345 .type = AVMEDIA_TYPE_VIDEO,
346 .get_video_buffer = avfilter_null_get_video_buffer,
347 .config_props = config_input,
348 .start_frame = start_frame,
349 .draw_slice = draw_slice,
350 .end_frame = end_frame },
352 .outputs = (AVFilterPad[]) {{ .name = "default",
353 .type = AVMEDIA_TYPE_VIDEO,
354 .poll_frame = poll_frame,
355 .request_frame = request_frame, },