• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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     av_log(inlink->dst, AV_LOG_DEBUG,
00204            "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
00205            "-> select:%f\n",
00206            (int)select->var_values[VAR_N],
00207            (int)select->var_values[VAR_PTS],
00208            select->var_values[VAR_T],
00209            (int)select->var_values[VAR_POS],
00210            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
00211            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
00212            select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
00213            (int)select->var_values[VAR_KEY],
00214            av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
00215            res);
00216 
00217     select->var_values[VAR_N] += 1.0;
00218 
00219     if (res) {
00220         select->var_values[VAR_PREV_SELECTED_N]   = select->var_values[VAR_N];
00221         select->var_values[VAR_PREV_SELECTED_PTS] = select->var_values[VAR_PTS];
00222         select->var_values[VAR_PREV_SELECTED_T]   = select->var_values[VAR_T];
00223         select->var_values[VAR_SELECTED_N] += 1.0;
00224     }
00225     return res;
00226 }
00227 
00228 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
00229 {
00230     SelectContext *select = inlink->dst->priv;
00231 
00232     select->select = select_frame(inlink->dst, picref);
00233     if (select->select) {
00234         /* frame was requested through poll_frame */
00235         if (select->cache_frames) {
00236             if (!av_fifo_space(select->pending_frames))
00237                 av_log(inlink->dst, AV_LOG_ERROR,
00238                        "Buffering limit reached, cannot cache more frames\n");
00239             else
00240                 av_fifo_generic_write(select->pending_frames, &picref,
00241                                       sizeof(picref), NULL);
00242             return;
00243         }
00244         avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0));
00245     }
00246 }
00247 
00248 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00249 {
00250     SelectContext *select = inlink->dst->priv;
00251 
00252     if (select->select && !select->cache_frames)
00253         avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
00254 }
00255 
00256 static void end_frame(AVFilterLink *inlink)
00257 {
00258     SelectContext *select = inlink->dst->priv;
00259     AVFilterBufferRef *picref = inlink->cur_buf;
00260 
00261     if (select->select) {
00262         if (select->cache_frames)
00263             return;
00264         avfilter_end_frame(inlink->dst->outputs[0]);
00265     }
00266     avfilter_unref_buffer(picref);
00267 }
00268 
00269 static int request_frame(AVFilterLink *outlink)
00270 {
00271     AVFilterContext *ctx = outlink->src;
00272     SelectContext *select = ctx->priv;
00273     AVFilterLink *inlink = outlink->src->inputs[0];
00274     select->select = 0;
00275 
00276     if (av_fifo_size(select->pending_frames)) {
00277         AVFilterBufferRef *picref;
00278         av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL);
00279         avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
00280         avfilter_draw_slice(outlink, 0, outlink->h, 1);
00281         avfilter_end_frame(outlink);
00282         avfilter_unref_buffer(picref);
00283         return 0;
00284     }
00285 
00286     while (!select->select) {
00287         int ret = avfilter_request_frame(inlink);
00288         if (ret < 0)
00289             return ret;
00290     }
00291 
00292     return 0;
00293 }
00294 
00295 static int poll_frame(AVFilterLink *outlink)
00296 {
00297     SelectContext *select = outlink->src->priv;
00298     AVFilterLink *inlink = outlink->src->inputs[0];
00299     int count, ret;
00300 
00301     if (!av_fifo_size(select->pending_frames)) {
00302         if ((count = avfilter_poll_frame(inlink)) <= 0)
00303             return count;
00304         /* request frame from input, and apply select condition to it */
00305         select->cache_frames = 1;
00306         while (count-- && av_fifo_space(select->pending_frames)) {
00307             ret = avfilter_request_frame(inlink);
00308             if (ret < 0)
00309                 break;
00310         }
00311         select->cache_frames = 0;
00312     }
00313 
00314     return av_fifo_size(select->pending_frames)/sizeof(AVFilterBufferRef *);
00315 }
00316 
00317 static av_cold void uninit(AVFilterContext *ctx)
00318 {
00319     SelectContext *select = ctx->priv;
00320     AVFilterBufferRef *picref;
00321 
00322     av_expr_free(select->expr);
00323     select->expr = NULL;
00324 
00325     while (select->pending_frames &&
00326            av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL) == sizeof(picref))
00327         avfilter_unref_buffer(picref);
00328     av_fifo_free(select->pending_frames);
00329     select->pending_frames = NULL;
00330 }
00331 
00332 AVFilter avfilter_vf_select = {
00333     .name      = "select",
00334     .description = NULL_IF_CONFIG_SMALL("Select frames to pass in output."),
00335     .init      = init,
00336     .uninit    = uninit,
00337 
00338     .priv_size = sizeof(SelectContext),
00339 
00340     .inputs    = (AVFilterPad[]) {{ .name             = "default",
00341                                     .type             = AVMEDIA_TYPE_VIDEO,
00342                                     .get_video_buffer = avfilter_null_get_video_buffer,
00343                                     .config_props     = config_input,
00344                                     .start_frame      = start_frame,
00345                                     .draw_slice       = draw_slice,
00346                                     .end_frame        = end_frame },
00347                                   { .name = NULL }},
00348     .outputs   = (AVFilterPad[]) {{ .name             = "default",
00349                                     .type             = AVMEDIA_TYPE_VIDEO,
00350                                     .poll_frame       = poll_frame,
00351                                     .request_frame    = request_frame, },
00352                                   { .name = NULL}},
00353 };
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1