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