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