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