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 * const var_names[] = {
00031 "TB",
00032
00033 "pts",
00034 "start_pts",
00035 "prev_pts",
00036 "prev_selected_pts",
00037
00038 "t",
00039 "start_t",
00040 "prev_t",
00041 "prev_selected_t",
00042
00043 "pict_type",
00044 "I",
00045 "P",
00046 "B",
00047 "S",
00048 "SI",
00049 "SP",
00050 "BI",
00051
00052 "interlace_type",
00053 "PROGRESSIVE",
00054 "TOPFIRST",
00055 "BOTTOMFIRST",
00056
00057 "n",
00058 "selected_n",
00059 "prev_selected_n",
00060
00061 "key",
00062 "pos",
00063
00064 NULL
00065 };
00066
00067 enum var_name {
00068 VAR_TB,
00069
00070 VAR_PTS,
00071 VAR_START_PTS,
00072 VAR_PREV_PTS,
00073 VAR_PREV_SELECTED_PTS,
00074
00075 VAR_T,
00076 VAR_START_T,
00077 VAR_PREV_T,
00078 VAR_PREV_SELECTED_T,
00079
00080 VAR_PICT_TYPE,
00081 VAR_PICT_TYPE_I,
00082 VAR_PICT_TYPE_P,
00083 VAR_PICT_TYPE_B,
00084 VAR_PICT_TYPE_S,
00085 VAR_PICT_TYPE_SI,
00086 VAR_PICT_TYPE_SP,
00087 VAR_PICT_TYPE_BI,
00088
00089 VAR_INTERLACE_TYPE,
00090 VAR_INTERLACE_TYPE_P,
00091 VAR_INTERLACE_TYPE_T,
00092 VAR_INTERLACE_TYPE_B,
00093
00094 VAR_N,
00095 VAR_SELECTED_N,
00096 VAR_PREV_SELECTED_N,
00097
00098 VAR_KEY,
00099 VAR_POS,
00100
00101 VAR_VARS_NB
00102 };
00103
00104 #define FIFO_SIZE 8
00105
00106 typedef struct {
00107 AVExpr *expr;
00108 double var_values[VAR_VARS_NB];
00109 double select;
00110 int cache_frames;
00111 AVFifoBuffer *pending_frames;
00112 } SelectContext;
00113
00114 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00115 {
00116 SelectContext *select = ctx->priv;
00117 int ret;
00118
00119 if ((ret = av_expr_parse(&select->expr, args ? args : "1",
00120 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
00121 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
00122 return ret;
00123 }
00124
00125 select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*));
00126 if (!select->pending_frames) {
00127 av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n");
00128 return AVERROR(ENOMEM);
00129 }
00130 return 0;
00131 }
00132
00133 #define INTERLACE_TYPE_P 0
00134 #define INTERLACE_TYPE_T 1
00135 #define INTERLACE_TYPE_B 2
00136
00137 static int config_input(AVFilterLink *inlink)
00138 {
00139 SelectContext *select = inlink->dst->priv;
00140
00141 select->var_values[VAR_N] = 0.0;
00142 select->var_values[VAR_SELECTED_N] = 0.0;
00143
00144 select->var_values[VAR_TB] = av_q2d(inlink->time_base);
00145
00146 select->var_values[VAR_PREV_PTS] = NAN;
00147 select->var_values[VAR_PREV_SELECTED_PTS] = NAN;
00148 select->var_values[VAR_PREV_SELECTED_T] = NAN;
00149 select->var_values[VAR_START_PTS] = NAN;
00150 select->var_values[VAR_START_T] = NAN;
00151
00152 select->var_values[VAR_PICT_TYPE_I] = AV_PICTURE_TYPE_I;
00153 select->var_values[VAR_PICT_TYPE_P] = AV_PICTURE_TYPE_P;
00154 select->var_values[VAR_PICT_TYPE_B] = AV_PICTURE_TYPE_B;
00155 select->var_values[VAR_PICT_TYPE_SI] = AV_PICTURE_TYPE_SI;
00156 select->var_values[VAR_PICT_TYPE_SP] = AV_PICTURE_TYPE_SP;
00157
00158 select->var_values[VAR_INTERLACE_TYPE_P] = INTERLACE_TYPE_P;
00159 select->var_values[VAR_INTERLACE_TYPE_T] = INTERLACE_TYPE_T;
00160 select->var_values[VAR_INTERLACE_TYPE_B] = INTERLACE_TYPE_B;
00161
00162 return 0;
00163 }
00164
00165 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
00166 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
00167
00168 static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *picref)
00169 {
00170 SelectContext *select = ctx->priv;
00171 AVFilterLink *inlink = ctx->inputs[0];
00172 double res;
00173
00174 if (isnan(select->var_values[VAR_START_PTS]))
00175 select->var_values[VAR_START_PTS] = TS2D(picref->pts);
00176 if (isnan(select->var_values[VAR_START_T]))
00177 select->var_values[VAR_START_T] = TS2D(picref->pts) * av_q2d(inlink->time_base);
00178
00179 select->var_values[VAR_PTS] = TS2D(picref->pts);
00180 select->var_values[VAR_T ] = TS2D(picref->pts) * av_q2d(inlink->time_base);
00181 select->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
00182 select->var_values[VAR_PREV_PTS] = TS2D(picref ->pts);
00183
00184 select->var_values[VAR_INTERLACE_TYPE] =
00185 !picref->video->interlaced ? INTERLACE_TYPE_P :
00186 picref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
00187 select->var_values[VAR_PICT_TYPE] = picref->video->pict_type;
00188
00189 res = av_expr_eval(select->expr, select->var_values, NULL);
00190 av_log(inlink->dst, AV_LOG_DEBUG,
00191 "n:%d pts:%d t:%f pos:%d interlace_type:%c key:%d pict_type:%c "
00192 "-> select:%f\n",
00193 (int)select->var_values[VAR_N],
00194 (int)select->var_values[VAR_PTS],
00195 select->var_values[VAR_T],
00196 (int)select->var_values[VAR_POS],
00197 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_P ? 'P' :
00198 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_T ? 'T' :
00199 select->var_values[VAR_INTERLACE_TYPE] == INTERLACE_TYPE_B ? 'B' : '?',
00200 (int)select->var_values[VAR_KEY],
00201 av_get_picture_type_char(select->var_values[VAR_PICT_TYPE]),
00202 res);
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
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
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 = (const 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 = (const AVFilterPad[]) {{ .name = "default",
00336 .type = AVMEDIA_TYPE_VIDEO,
00337 .poll_frame = poll_frame,
00338 .request_frame = request_frame, },
00339 { .name = NULL}},
00340 };