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