]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_select.c
Merge commit 'd05f72c75445969cd7bdb1d860635c9880c67fb6'
[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 "libavutil/internal.h"
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33
34 #if CONFIG_AVCODEC
35 #include "libavcodec/dsputil.h"
36 #endif
37
38 static const char *const var_names[] = {
39     "TB",                ///< timebase
40
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
45
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
50
51     "pict_type",         ///< the type of picture in the movie
52     "I",
53     "P",
54     "B",
55     "S",
56     "SI",
57     "SP",
58     "BI",
59
60     "interlace_type",    ///< the frame interlace type
61     "PROGRESSIVE",
62     "TOPFIRST",
63     "BOTTOMFIRST",
64
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
68
69     "key",               ///< tell if the frame is a key frame
70     "pos",               ///< original position in the file of the frame
71
72     "scene",
73
74     NULL
75 };
76
77 enum var_name {
78     VAR_TB,
79
80     VAR_PTS,
81     VAR_START_PTS,
82     VAR_PREV_PTS,
83     VAR_PREV_SELECTED_PTS,
84
85     VAR_T,
86     VAR_START_T,
87     VAR_PREV_T,
88     VAR_PREV_SELECTED_T,
89
90     VAR_PICT_TYPE,
91     VAR_PICT_TYPE_I,
92     VAR_PICT_TYPE_P,
93     VAR_PICT_TYPE_B,
94     VAR_PICT_TYPE_S,
95     VAR_PICT_TYPE_SI,
96     VAR_PICT_TYPE_SP,
97     VAR_PICT_TYPE_BI,
98
99     VAR_INTERLACE_TYPE,
100     VAR_INTERLACE_TYPE_P,
101     VAR_INTERLACE_TYPE_T,
102     VAR_INTERLACE_TYPE_B,
103
104     VAR_N,
105     VAR_SELECTED_N,
106     VAR_PREV_SELECTED_N,
107
108     VAR_KEY,
109     VAR_POS,
110
111     VAR_SCENE,
112
113     VAR_VARS_NB
114 };
115
116 #define FIFO_SIZE 8
117
118 typedef struct {
119     AVExpr *expr;
120     double var_values[VAR_VARS_NB];
121     int do_scene_detect;            ///< 1 if the expression requires scene detection variables, 0 otherwise
122 #if CONFIG_AVCODEC
123     AVCodecContext *avctx;          ///< codec context required for the DSPContext (scene detect only)
124     DSPContext c;                   ///< context providing optimized SAD methods   (scene detect only)
125     double prev_mafd;               ///< previous MAFD                             (scene detect only)
126 #endif
127     AVFilterBufferRef *prev_picref; ///< previous frame                            (scene detect only)
128     double select;
129     int cache_frames;
130     AVFifoBuffer *pending_frames; ///< FIFO buffer of video frames
131 } SelectContext;
132
133 static av_cold int init(AVFilterContext *ctx, const char *args)
134 {
135     SelectContext *select = ctx->priv;
136     int ret;
137
138     if ((ret = av_expr_parse(&select->expr, args ? args : "1",
139                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
140         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
141         return ret;
142     }
143
144     select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
145     if (!select->pending_frames) {
146         av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
147         return AVERROR(ENOMEM);
148     }
149
150     select->do_scene_detect = args && strstr(args, "scene");
151     if (select->do_scene_detect && !CONFIG_AVCODEC) {
152         av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n");
153         return AVERROR(EINVAL);
154     }
155     return 0;
156 }
157
158 #define INTERLACE_TYPE_P 0
159 #define INTERLACE_TYPE_T 1
160 #define INTERLACE_TYPE_B 2
161
162 static int config_input(AVFilterLink *inlink)
163 {
164     SelectContext *select = inlink->dst->priv;
165
166     select->var_values[VAR_N]          = 0.0;
167     select->var_values[VAR_SELECTED_N] = 0.0;
168
169     select->var_values[VAR_TB] = av_q2d(inlink->time_base);
170
171     select->var_values[VAR_PREV_PTS]          = NAN;
172     select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
173     select->var_values[VAR_PREV_SELECTED_T]   = NAN;
174     select->var_values[VAR_START_PTS]         = NAN;
175     select->var_values[VAR_START_T]           = NAN;
176
177     select->var_values[VAR_PICT_TYPE_I]  = AV_PICTURE_TYPE_I;
178     select->var_values[VAR_PICT_TYPE_P]  = AV_PICTURE_TYPE_P;
179     select->var_values[VAR_PICT_TYPE_B]  = AV_PICTURE_TYPE_B;
180     select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
181     select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
182
183     select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
184     select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
185     select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
186
187     if (CONFIG_AVCODEC && select->do_scene_detect) {
188         select->avctx = avcodec_alloc_context3(NULL);
189         if (!select->avctx)
190             return AVERROR(ENOMEM);
191         dsputil_init(&select->c, select->avctx);
192     }
193     return 0;
194 }
195
196 #if CONFIG_AVCODEC
197 static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
198 {
199     double ret = 0;
200     SelectContext *select = ctx->priv;
201     AVFilterBufferRef *prev_picref = select->prev_picref;
202
203     if (prev_picref &&
204         picref->video->h    == prev_picref->video->h &&
205         picref->video->w    == prev_picref->video->w &&
206         picref->linesize[0] == prev_picref->linesize[0]) {
207         int x, y;
208         int64_t sad;
209         double mafd, diff;
210         uint8_t *p1 =      picref->data[0];
211         uint8_t *p2 = prev_picref->data[0];
212         const int linesize = picref->linesize[0];
213
214         for (sad = y = 0; y < picref->video->h; y += 8)
215             for (x = 0; x < linesize; x += 8)
216                 sad += select->c.sad[1](select,
217                                         p1 + y * linesize + x,
218                                         p2 + y * linesize + x,
219                                         linesize, 8);
220         emms_c();
221         mafd = sad / (picref->video->h * picref->video->w * 3);
222         diff = fabs(mafd - select->prev_mafd);
223         ret  = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
224         select->prev_mafd = mafd;
225         avfilter_unref_buffer(prev_picref);
226     }
227     select->prev_picref = avfilter_ref_buffer(picref, ~0);
228     return ret;
229 }
230 #endif
231
232 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
233 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
234
235 static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
236 {
237     SelectContext *select = ctx->priv;
238     AVFilterLink *inlink = ctx->inputs[0];
239     double res;
240
241     if (CONFIG_AVCODEC && select->do_scene_detect)
242         select->var_values[VAR_SCENE] = get_scene_score(ctx, picref);
243     if (isnan(select->var_values[VAR_START_PTS]))
244         select->var_values[VAR_START_PTS] = TS2D(picref->pts);
245     if (isnan(select->var_values[VAR_START_T]))
246         select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
247
248     select->var_values[VAR_PTS] = TS2D(picref->pts);
249     select->var_values[VAR_T  ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
250     select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
251     select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
252
253     select->var_values[VAR_INTERLACE_TYPE] =
254         !picref->video->interlaced     ? INTERLACE_TYPE_P :
255         picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
256     select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
257
258     res = av_expr_eval(select->expr, select->var_values, NULL);
259     av_log(inlink->dst, AV_LOG_DEBUG,
260            "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
261            "-> select:%f\n",
262            (int)select->var_values[VAR_N],
263            (int)select->var_values[VAR_PTS],
264            select->var_values[VAR_T],
265            (int)select->var_values[VAR_POS],
266            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
267            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
268            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
269            (int)select->var_values[VAR_KEY],
270            av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
271            res);
272
273     select->var_values[VAR_N] += 1.0;
274
275     if (res) {
276         select->var_values[VAR_PREV_SELECTED_N]   = select->var_values[VAR_N];
277         select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
278         select->var_values[VAR_PREV_SELECTED_T]   = select->var_values[VAR_T];
279         select->var_values[VAR_SELECTED_N] += 1.0;
280     }
281     return res;
282 }
283
284 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
285 {
286     SelectContext *select = inlink->dst->priv;
287
288     select->select = select_frame(inlink->dst, picref);
289     if (select->select) {
290         AVFilterBufferRef *buf_out;
291         /* frame was requested through poll_frame */
292         if (select->cache_frames) {
293             if (!av_fifo_space(select->pending_frames))
294                 av_log(inlink->dst, AV_LOG_ERROR,
295                        "Buffering limit reached, cannot cache more frames\n");
296             else
297                 av_fifo_generic_write(select->pending_frames, &picref,
298                                       sizeof(picref), NULL);
299             return 0;
300         }
301         buf_out = avfilter_ref_buffer(picref, ~0);
302         if (!buf_out)
303             return AVERROR(ENOMEM);
304         return ff_start_frame(inlink->dst->outputs[0], buf_out);
305     }
306
307     return 0;
308 }
309
310 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
311 {
312     SelectContext *select = inlink->dst->priv;
313
314     if (select->select && !select->cache_frames)
315         return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
316     return 0;
317 }
318
319 static int end_frame(AVFilterLink *inlink)
320 {
321     SelectContext *select = inlink->dst->priv;
322
323     if (select->select) {
324         if (select->cache_frames)
325             return 0;
326         return ff_end_frame(inlink->dst->outputs[0]);
327     }
328     return 0;
329 }
330
331 static int request_frame(AVFilterLink *outlink)
332 {
333     AVFilterContext *ctx = outlink->src;
334     SelectContext *select = ctx->priv;
335     AVFilterLink *inlink = outlink->src->inputs[0];
336     select->select = 0;
337
338     if (av_fifo_size(select->pending_frames)) {
339         AVFilterBufferRef *picref;
340         int ret;
341
342         av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
343         if ((ret = ff_start_frame(outlink, picref)) < 0 ||
344             (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
345             (ret = ff_end_frame(outlink)) < 0);
346
347         return ret;
348     }
349
350     while (!select->select) {
351         int ret = ff_request_frame(inlink);
352         if (ret < 0)
353             return ret;
354     }
355
356     return 0;
357 }
358
359 static int poll_frame(AVFilterLink *outlink)
360 {
361     SelectContext *select = outlink->src->priv;
362     AVFilterLink *inlink = outlink->src->inputs[0];
363     int count, ret;
364
365     if (!av_fifo_size(select->pending_frames)) {
366         if ((count = ff_poll_frame(inlink)) <= 0)
367             return count;
368         /* request frame from input, and apply select condition to it */
369         select->cache_frames = 1;
370         while (count-- && av_fifo_space(select->pending_frames)) {
371             ret = ff_request_frame(inlink);
372             if (ret < 0)
373                 break;
374         }
375         select->cache_frames = 0;
376     }
377
378     return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
379 }
380
381 static av_cold void uninit(AVFilterContext *ctx)
382 {
383     SelectContext *select = ctx->priv;
384     AVFilterBufferRef *picref;
385
386     av_expr_free(select->expr);
387     select->expr = NULL;
388
389     while (select->pending_frames &&
390            av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
391         avfilter_unref_buffer(picref);
392     av_fifo_free(select->pending_frames);
393     select->pending_frames = NULL;
394
395     if (select->do_scene_detect) {
396         avfilter_unref_bufferp(&select->prev_picref);
397         if (select->avctx) {
398             avcodec_close(select->avctx);
399             av_freep(&select->avctx);
400         }
401     }
402 }
403
404 static int query_formats(AVFilterContext *ctx)
405 {
406     SelectContext *select = ctx->priv;
407
408     if (!select->do_scene_detect) {
409         return ff_default_query_formats(ctx);
410     } else {
411         static const enum PixelFormat pix_fmts[] = {
412             PIX_FMT_RGB24, PIX_FMT_BGR24,
413             PIX_FMT_NONE
414         };
415         ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
416     }
417     return 0;
418 }
419
420 AVFilter avfilter_vf_select = {
421     .name      = "select",
422     .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
423     .init      = init,
424     .uninit    = uninit,
425     .query_formats = query_formats,
426
427     .priv_size = sizeof(SelectContext),
428
429     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
430                                           .type             = AVMEDIA_TYPE_VIDEO,
431                                           .get_video_buffer = ff_null_get_video_buffer,
432                                           .min_perms        = AV_PERM_PRESERVE,
433                                           .config_props     = config_input,
434                                           .start_frame      = start_frame,
435                                           .draw_slice       = draw_slice,
436                                           .end_frame        = end_frame },
437                                         { .name = NULL }},
438     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
439                                           .type             = AVMEDIA_TYPE_VIDEO,
440                                           .poll_frame       = poll_frame,
441                                           .request_frame    = request_frame, },
442                                         { .name = NULL}},
443 };