]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_select.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vf_select.c
1 /*
2  * Copyright (c) 2011 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
23  * filter for selecting which frame passes in the filterchain
24  */
25
26 #include "libavutil/eval.h"
27 #include "libavutil/fifo.h"
28 #include "avfilter.h"
29 #include "video.h"
30
31 static const char *const var_names[] = {
32     "TB",                ///< timebase
33
34     "pts",               ///< original pts in the file of the frame
35     "start_pts",         ///< first PTS in the stream, expressed in TB units
36     "prev_pts",          ///< previous frame PTS
37     "prev_selected_pts", ///< previous selected frame PTS
38
39     "t",                 ///< first PTS in seconds
40     "start_t",           ///< first PTS in the stream, expressed in seconds
41     "prev_t",            ///< previous frame time
42     "prev_selected_t",   ///< previously selected time
43
44     "pict_type",         ///< the type of picture in the movie
45     "I",
46     "P",
47     "B",
48     "S",
49     "SI",
50     "SP",
51     "BI",
52
53     "interlace_type",    ///< the frame interlace type
54     "PROGRESSIVE",
55     "TOPFIRST",
56     "BOTTOMFIRST",
57
58     "n",                 ///< frame number (starting from zero)
59     "selected_n",        ///< selected frame number (starting from zero)
60     "prev_selected_n",   ///< number of the last selected frame
61
62     "key",               ///< tell if the frame is a key frame
63     "pos",               ///< original position in the file of the frame
64
65     NULL
66 };
67
68 enum var_name {
69     VAR_TB,
70
71     VAR_PTS,
72     VAR_START_PTS,
73     VAR_PREV_PTS,
74     VAR_PREV_SELECTED_PTS,
75
76     VAR_T,
77     VAR_START_T,
78     VAR_PREV_T,
79     VAR_PREV_SELECTED_T,
80
81     VAR_PICT_TYPE,
82     VAR_PICT_TYPE_I,
83     VAR_PICT_TYPE_P,
84     VAR_PICT_TYPE_B,
85     VAR_PICT_TYPE_S,
86     VAR_PICT_TYPE_SI,
87     VAR_PICT_TYPE_SP,
88     VAR_PICT_TYPE_BI,
89
90     VAR_INTERLACE_TYPE,
91     VAR_INTERLACE_TYPE_P,
92     VAR_INTERLACE_TYPE_T,
93     VAR_INTERLACE_TYPE_B,
94
95     VAR_N,
96     VAR_SELECTED_N,
97     VAR_PREV_SELECTED_N,
98
99     VAR_KEY,
100     VAR_POS,
101
102     VAR_VARS_NB
103 };
104
105 #define FIFO_SIZE 8
106
107 typedef struct {
108     AVExpr *expr;
109     double var_values[VAR_VARS_NB];
110     double select;
111     int cache_frames;
112     AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
113 } SelectContext;
114
115 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
116 {
117     SelectContext *select = ctx->priv;
118     int ret;
119
120     if ((ret = av_expr_parse(&select->expr, args ? args : "1",
121                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
122         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
123         return ret;
124     }
125
126     select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
127     if (!select->pending_frames) {
128         av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
129         return AVERROR(ENOMEM);
130     }
131     return 0;
132 }
133
134 #define INTERLACE_TYPE_P 0
135 #define INTERLACE_TYPE_T 1
136 #define INTERLACE_TYPE_B 2
137
138 static int config_input(AVFilterLink *inlink)
139 {
140     SelectContext *select = inlink->dst->priv;
141
142     select->var_values[VAR_N]          = 0.0;
143     select->var_values[VAR_SELECTED_N] = 0.0;
144
145     select->var_values[VAR_TB] = av_q2d(inlink->time_base);
146
147     select->var_values[VAR_PREV_PTS]          = NAN;
148     select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
149     select->var_values[VAR_PREV_SELECTED_T]   = NAN;
150     select->var_values[VAR_START_PTS]         = NAN;
151     select->var_values[VAR_START_T]           = NAN;
152
153     select->var_values[VAR_PICT_TYPE_I]  = AV_PICTURE_TYPE_I;
154     select->var_values[VAR_PICT_TYPE_P]  = AV_PICTURE_TYPE_P;
155     select->var_values[VAR_PICT_TYPE_B]  = AV_PICTURE_TYPE_B;
156     select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
157     select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
158
159     select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
160     select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
161     select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
162
163     return 0;
164 }
165
166 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
167 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
168
169 static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
170 {
171     SelectContext *select = ctx->priv;
172     AVFilterLink *inlink = ctx->inputs[0];
173     double res;
174
175     if (isnan(select->var_values[VAR_START_PTS]))
176         select->var_values[VAR_START_PTS] = TS2D(picref->pts);
177     if (isnan(select->var_values[VAR_START_T]))
178         select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
179
180     select->var_values[VAR_PTS] = TS2D(picref->pts);
181     select->var_values[VAR_T  ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
182     select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
183     select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
184
185     select->var_values[VAR_INTERLACE_TYPE] =
186         !picref->video->interlaced     ? INTERLACE_TYPE_P :
187         picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
188     select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
189
190     res = av_expr_eval(select->expr, select->var_values, NULL);
191     av_log(inlink->dst, AV_LOG_DEBUG,
192            "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
193            "-> select:%f\n",
194            (int)select->var_values[VAR_N],
195            (int)select->var_values[VAR_PTS],
196            select->var_values[VAR_T],
197            (int)select->var_values[VAR_POS],
198            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
199            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
200            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
201            (int)select->var_values[VAR_KEY],
202            av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
203            res);
204
205     select->var_values[VAR_N] += 1.0;
206
207     if (res) {
208         select->var_values[VAR_PREV_SELECTED_N]   = select->var_values[VAR_N];
209         select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
210         select->var_values[VAR_PREV_SELECTED_T]   = select->var_values[VAR_T];
211         select->var_values[VAR_SELECTED_N] += 1.0;
212     }
213     return res;
214 }
215
216 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
217 {
218     SelectContext *select = inlink->dst->priv;
219
220     select->select = select_frame(inlink->dst, picref);
221     if (select->select) {
222         /* frame was requested through poll_frame */
223         if (select->cache_frames) {
224             if (!av_fifo_space(select->pending_frames))
225                 av_log(inlink->dst, AV_LOG_ERROR,
226                        "Buffering limit reached, cannot cache more frames\n");
227             else
228                 av_fifo_generic_write(select->pending_frames, &picref,
229                                       sizeof(picref), NULL);
230             return;
231         }
232         avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
233     }
234 }
235
236 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
237 {
238     SelectContext *select = inlink->dst->priv;
239
240     if (select->select && !select->cache_frames)
241         avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
242 }
243
244 static void end_frame(AVFilterLink *inlink)
245 {
246     SelectContext *select = inlink->dst->priv;
247     AVFilterBufferRef *picref = inlink->cur_buf;
248
249     if (select->select) {
250         if (select->cache_frames)
251             return;
252         avfilter_end_frame(inlink->dst->outputs[0]);
253     }
254     avfilter_unref_buffer(picref);
255 }
256
257 static int request_frame(AVFilterLink *outlink)
258 {
259     AVFilterContext *ctx = outlink->src;
260     SelectContext *select = ctx->priv;
261     AVFilterLink *inlink = outlink->src->inputs[0];
262     select->select = 0;
263
264     if (av_fifo_size(select->pending_frames)) {
265         AVFilterBufferRef *picref;
266         av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
267         avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
268         avfilter_draw_slice(outlink, 0, outlink->h, 1);
269         avfilter_end_frame(outlink);
270         avfilter_unref_buffer(picref);
271         return 0;
272     }
273
274     while (!select->select) {
275         int ret = avfilter_request_frame(inlink);
276         if (ret < 0)
277             return ret;
278     }
279
280     return 0;
281 }
282
283 static int poll_frame(AVFilterLink *outlink)
284 {
285     SelectContext *select = outlink->src->priv;
286     AVFilterLink *inlink = outlink->src->inputs[0];
287     int count, ret;
288
289     if (!av_fifo_size(select->pending_frames)) {
290         if ((count = avfilter_poll_frame(inlink)) <= 0)
291             return count;
292         /* request frame from input, and apply select condition to it */
293         select->cache_frames = 1;
294         while (count-- && av_fifo_space(select->pending_frames)) {
295             ret = avfilter_request_frame(inlink);
296             if (ret < 0)
297                 break;
298         }
299         select->cache_frames = 0;
300     }
301
302     return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
303 }
304
305 static av_cold void uninit(AVFilterContext *ctx)
306 {
307     SelectContext *select = ctx->priv;
308     AVFilterBufferRef *picref;
309
310     av_expr_free(select->expr);
311     select->expr = NULL;
312
313     while (select->pending_frames &&
314            av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
315         avfilter_unref_buffer(picref);
316     av_fifo_free(select->pending_frames);
317     select->pending_frames = NULL;
318 }
319
320 AVFilter avfilter_vf_select = {
321     .name      = "select",
322     .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
323     .init      = init,
324     .uninit    = uninit,
325
326     .priv_size = sizeof(SelectContext),
327
328     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
329                                     .type             = AVMEDIA_TYPE_VIDEO,
330                                     .get_video_buffer = ff_null_get_video_buffer,
331                                     .config_props     = config_input,
332                                     .start_frame      = start_frame,
333                                     .draw_slice       = draw_slice,
334                                     .end_frame        = end_frame },
335                                   { .name = NULL }},
336     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
337                                     .type             = AVMEDIA_TYPE_VIDEO,
338                                     .poll_frame       = poll_frame,
339                                     .request_frame    = request_frame, },
340                                   { .name = NULL}},
341 };