libavfilter/vf_select.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Stefano Sabatini
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 #include "libavutil/eval.h"
00027 #include "libavutil/fifo.h"
00028 #include "libavutil/mathematics.h"
00029 #include "avfilter.h"
00030 
00031 static const char *var_names[] = {
00032     "E",                 
00033     "PHI",               
00034     "PI",                
00035 
00036     "TB",                
00037 
00038     "pts",               
00039     "start_pts",         
00040     "prev_pts",          
00041     "prev_selected_pts", 
00042 
00043     "t",                 
00044     "start_t",           
00045     "prev_t",            
00046     "prev_selected_t",   
00047 
00048     "pict_type",         
00049     "I",
00050     "P",
00051     "B",
00052     "S",
00053     "SI",
00054     "SP",
00055     "BI",
00056 
00057     "interlace_type",    
00058     "PROGRESSIVE",
00059     "TOPFIRST",
00060     "BOTTOMFIRST",
00061 
00062     "n",                 
00063     "selected_n",        
00064     "prev_selected_n",   
00065 
00066     "key",               
00067     "pos",               
00068 
00069     NULL
00070 };
00071 
00072 enum var_name {
00073     VAR_E,
00074     VAR_PHI,
00075     VAR_PI,
00076 
00077     VAR_TB,
00078 
00079     VAR_PTS,
00080     VAR_START_PTS,
00081     VAR_PREV_PTS,
00082     VAR_PREV_SELECTED_PTS,
00083 
00084     VAR_T,
00085     VAR_START_T,
00086     VAR_PREV_T,
00087     VAR_PREV_SELECTED_T,
00088 
00089     VAR_PICT_TYPE,
00090     VAR_PICT_TYPE_I,
00091     VAR_PICT_TYPE_P,
00092     VAR_PICT_TYPE_B,
00093     VAR_PICT_TYPE_S,
00094     VAR_PICT_TYPE_SI,
00095     VAR_PICT_TYPE_SP,
00096     VAR_PICT_TYPE_BI,
00097 
00098     VAR_INTERLACE_TYPE,
00099     VAR_INTERLACE_TYPE_P,
00100     VAR_INTERLACE_TYPE_T,
00101     VAR_INTERLACE_TYPE_B,
00102 
00103     VAR_N,
00104     VAR_SELECTED_N,
00105     VAR_PREV_SELECTED_N,
00106 
00107     VAR_KEY,
00108     VAR_POS,
00109 
00110     VAR_VARS_NB
00111 };
00112 
00113 #define FIFO_SIZE 8
00114 
00115 typedef struct {
00116     AVExpr *expr;
00117     double var_values[VAR_VARS_NB];
00118     double select;
00119     int cache_frames;
00120     AVFifoBuffer *pending_frames; 
00121 } SelectContext;
00122 
00123 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00124 {
00125     SelectContext *select = ctx->priv;
00126     int ret;
00127 
00128     if ((ret = av_expr_parse(&select->expr, args ? args : "1",
00129                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
00130         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
00131         return ret;
00132     }
00133 
00134     select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
00135     if (!select->pending_frames) {
00136         av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
00137         return AVERROR(ENOMEM);
00138     }
00139     return 0;
00140 }
00141 
00142 #define INTERLACE_TYPE_P 0
00143 #define INTERLACE_TYPE_T 1
00144 #define INTERLACE_TYPE_B 2
00145 
00146 static int config_input(AVFilterLink *inlink)
00147 {
00148     SelectContext *select = inlink->dst->priv;
00149 
00150     select->var_values[VAR_E]   = M_E;
00151     select->var_values[VAR_PHI] = M_PHI;
00152     select->var_values[VAR_PI]  = M_PI;
00153 
00154     select->var_values[VAR_N]          = 0.0;
00155     select->var_values[VAR_SELECTED_N] = 0.0;
00156 
00157     select->var_values[VAR_TB] = av_q2d(inlink->time_base);
00158 
00159     select->var_values[VAR_PREV_PTS]          = NAN;
00160     select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
00161     select->var_values[VAR_PREV_SELECTED_T]   = NAN;
00162     select->var_values[VAR_START_PTS]         = NAN;
00163     select->var_values[VAR_START_T]           = NAN;
00164 
00165     select->var_values[VAR_PICT_TYPE_I]  = AV_PICTURE_TYPE_I;
00166     select->var_values[VAR_PICT_TYPE_P]  = AV_PICTURE_TYPE_P;
00167     select->var_values[VAR_PICT_TYPE_B]  = AV_PICTURE_TYPE_B;
00168     select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
00169     select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
00170 
00171     select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
00172     select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
00173     select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;;
00174 
00175     return 0;
00176 }
00177 
00178 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
00179 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
00180 
00181 static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
00182 {
00183     SelectContext *select = ctx->priv;
00184     AVFilterLink *inlink = ctx->inputs[0];
00185     double res;
00186 
00187     if (isnan(select->var_values[VAR_START_PTS]))
00188         select->var_values[VAR_START_PTS] = TS2D(picref->pts);
00189     if (isnan(select->var_values[VAR_START_T]))
00190         select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
00191 
00192     select->var_values[VAR_PTS] = TS2D(picref->pts);
00193     select->var_values[VAR_T  ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
00194     select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
00195     select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
00196 
00197     select->var_values[VAR_INTERLACE_TYPE] =
00198         !picref->video->interlaced     ? INTERLACE_TYPE_P :
00199         picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
00200     select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
00201 
00202     res = av_expr_eval(select->expr, select->var_values, NULL);
00203 
00204     select->var_values[VAR_N] += 1.0;
00205 
00206     if (res) {
00207         select->var_values[VAR_PREV_SELECTED_N]   = select->var_values[VAR_N];
00208         select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
00209         select->var_values[VAR_PREV_SELECTED_T]   = select->var_values[VAR_T];
00210         select->var_values[VAR_SELECTED_N] += 1.0;
00211     }
00212     return res;
00213 }
00214 
00215 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
00216 {
00217     SelectContext *select = inlink->dst->priv;
00218 
00219     select->select = select_frame(inlink->dst, picref);
00220     if (select->select) {
00221         /* frame was requested through poll_frame */
00222         if (select->cache_frames) {
00223             if (!av_fifo_space(select->pending_frames))
00224                 av_log(inlink->dst, AV_LOG_ERROR,
00225                        "Buffering limit reached, cannot cache more frames\n");
00226             else
00227                 av_fifo_generic_write(select->pending_frames, &picref,
00228                                       sizeof(picref), NULL);
00229             return;
00230         }
00231         avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
00232     }
00233 }
00234 
00235 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00236 {
00237     SelectContext *select = inlink->dst->priv;
00238 
00239     if (select->select && !select->cache_frames)
00240         avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
00241 }
00242 
00243 static void end_frame(AVFilterLink *inlink)
00244 {
00245     SelectContext *select = inlink->dst->priv;
00246     AVFilterBufferRef *picref = inlink->cur_buf;
00247 
00248     if (select->select) {
00249         if (select->cache_frames)
00250             return;
00251         avfilter_end_frame(inlink->dst->outputs[0]);
00252     }
00253     avfilter_unref_buffer(picref);
00254 }
00255 
00256 static int request_frame(AVFilterLink *outlink)
00257 {
00258     AVFilterContext *ctx = outlink->src;
00259     SelectContext *select = ctx->priv;
00260     AVFilterLink *inlink = outlink->src->inputs[0];
00261     select->select = 0;
00262 
00263     if (av_fifo_size(select->pending_frames)) {
00264         AVFilterBufferRef *picref;
00265         av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
00266         avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
00267         avfilter_draw_slice(outlink, 0, outlink->h, 1);
00268         avfilter_end_frame(outlink);
00269         avfilter_unref_buffer(picref);
00270         return 0;
00271     }
00272 
00273     while (!select->select) {
00274         int ret = avfilter_request_frame(inlink);
00275         if (ret < 0)
00276             return ret;
00277     }
00278 
00279     return 0;
00280 }
00281 
00282 static int poll_frame(AVFilterLink *outlink)
00283 {
00284     SelectContext *select = outlink->src->priv;
00285     AVFilterLink *inlink = outlink->src->inputs[0];
00286     int count, ret;
00287 
00288     if (!av_fifo_size(select->pending_frames)) {
00289         if ((count = avfilter_poll_frame(inlink)) <= 0)
00290             return count;
00291         /* request frame from input, and apply select condition to it */
00292         select->cache_frames = 1;
00293         while (count-- && av_fifo_space(select->pending_frames)) {
00294             ret = avfilter_request_frame(inlink);
00295             if (ret < 0)
00296                 break;
00297         }
00298         select->cache_frames = 0;
00299     }
00300 
00301     return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
00302 }
00303 
00304 static av_cold void uninit(AVFilterContext *ctx)
00305 {
00306     SelectContext *select = ctx->priv;
00307     AVFilterBufferRef *picref;
00308 
00309     av_expr_free(select->expr);
00310     select->expr = NULL;
00311 
00312     while (select->pending_frames &&
00313            av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
00314         avfilter_unref_buffer(picref);
00315     av_fifo_free(select->pending_frames);
00316     select->pending_frames = NULL;
00317 }
00318 
00319 AVFilter avfilter_vf_select = {
00320     .name      = "select",
00321     .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
00322     .init      = init,
00323     .uninit    = uninit,
00324 
00325     .priv_size = sizeof(SelectContext),
00326 
00327     .inputs    = (AVFilterPad[]) {{ .name             = "default",
00328                                     .type             = AVMEDIA_TYPE_VIDEO,
00329                                     .get_video_buffer = avfilter_null_get_video_buffer,
00330                                     .config_props     = config_input,
00331                                     .start_frame      = start_frame,
00332                                     .draw_slice       = draw_slice,
00333                                     .end_frame        = end_frame },
00334                                   { .name = NULL }},
00335     .outputs   = (AVFilterPad[]) {{ .name             = "default",
00336                                     .type             = AVMEDIA_TYPE_VIDEO,
00337                                     .poll_frame       = poll_frame,
00338                                     .request_frame    = request_frame, },
00339                                   { .name = NULL}},
00340 };