00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "avfilter.h"
00027 #include "internal.h"
00028 #include "video.h"
00029
00030 typedef struct {
00031 int frame_step, frame_count, frame_selected;
00032 } FrameStepContext;
00033
00034 static av_cold int init(AVFilterContext *ctx, const char *args)
00035 {
00036 FrameStepContext *framestep = ctx->priv;
00037 char *tailptr;
00038 long int n = 1;
00039
00040 if (args) {
00041 n = strtol(args, &tailptr, 10);
00042 if (*tailptr || n <= 0 || n >= INT_MAX) {
00043 av_log(ctx, AV_LOG_ERROR,
00044 "Invalid argument '%s', must be a positive integer <= INT_MAX\n", args);
00045 return AVERROR(EINVAL);
00046 }
00047 }
00048
00049 framestep->frame_step = n;
00050 return 0;
00051 }
00052
00053 static int config_output_props(AVFilterLink *outlink)
00054 {
00055 AVFilterContext *ctx = outlink->src;
00056 FrameStepContext *framestep = ctx->priv;
00057 AVFilterLink *inlink = ctx->inputs[0];
00058
00059 outlink->frame_rate =
00060 av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
00061
00062 av_log(ctx, AV_LOG_VERBOSE, "step:%d frame_rate:%d/%d(%f) -> frame_rate:%d/%d(%f)\n",
00063 framestep->frame_step,
00064 inlink->frame_rate.num, inlink->frame_rate.den, av_q2d(inlink->frame_rate),
00065 outlink->frame_rate.num, outlink->frame_rate.den, av_q2d(outlink->frame_rate));
00066 return 0;
00067 }
00068
00069 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
00070 {
00071 FrameStepContext *framestep = inlink->dst->priv;
00072
00073 if (!(framestep->frame_count++ % framestep->frame_step)) {
00074 framestep->frame_selected = 1;
00075 return ff_filter_frame(inlink->dst->outputs[0], ref);
00076 } else {
00077 framestep->frame_selected = 0;
00078 avfilter_unref_buffer(ref);
00079 return 0;
00080 }
00081 }
00082
00083 static int request_frame(AVFilterLink *outlink)
00084 {
00085 FrameStepContext *framestep = outlink->src->priv;
00086 AVFilterLink *inlink = outlink->src->inputs[0];
00087 int ret;
00088
00089 framestep->frame_selected = 0;
00090 do {
00091 ret = ff_request_frame(inlink);
00092 } while (!framestep->frame_selected && ret >= 0);
00093
00094 return ret;
00095 }
00096
00097 static const AVFilterPad framestep_inputs[] = {
00098 {
00099 .name = "default",
00100 .type = AVMEDIA_TYPE_VIDEO,
00101 .get_video_buffer = ff_null_get_video_buffer,
00102 .filter_frame = filter_frame,
00103 },
00104 { NULL }
00105 };
00106
00107 static const AVFilterPad framestep_outputs[] = {
00108 {
00109 .name = "default",
00110 .type = AVMEDIA_TYPE_VIDEO,
00111 .config_props = config_output_props,
00112 .request_frame = request_frame,
00113 },
00114 { NULL }
00115 };
00116
00117 AVFilter avfilter_vf_framestep = {
00118 .name = "framestep",
00119 .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
00120 .init = init,
00121 .priv_size = sizeof(FrameStepContext),
00122 .inputs = framestep_inputs,
00123 .outputs = framestep_outputs,
00124 };