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 start_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
00070 {
00071 FrameStepContext *framestep = inlink->dst->priv;
00072
00073 framestep->frame_selected = 0;
00074 if (!(framestep->frame_count++ % framestep->frame_step)) {
00075 inlink->cur_buf = NULL;
00076 framestep->frame_selected = 1;
00077 return ff_start_frame(inlink->dst->outputs[0], ref);
00078 }
00079 return 0;
00080 }
00081
00082 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00083 {
00084 FrameStepContext *framestep = inlink->dst->priv;
00085
00086 if (framestep->frame_selected)
00087 return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
00088 return 0;
00089 }
00090
00091 static int end_frame(AVFilterLink *inlink)
00092 {
00093 FrameStepContext *framestep = inlink->dst->priv;
00094
00095 if (framestep->frame_selected)
00096 return ff_end_frame(inlink->dst->outputs[0]);
00097 return 0;
00098 }
00099
00100 static int request_frame(AVFilterLink *outlink)
00101 {
00102 FrameStepContext *framestep = outlink->src->priv;
00103 AVFilterLink *inlink = outlink->src->inputs[0];
00104 int ret;
00105
00106 framestep->frame_selected = 0;
00107 do {
00108 ret = ff_request_frame(inlink);
00109 } while (!framestep->frame_selected && ret >= 0);
00110
00111 return ret;
00112 }
00113
00114 AVFilter avfilter_vf_framestep = {
00115 .name = "framestep",
00116 .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
00117 .init = init,
00118 .priv_size = sizeof(FrameStepContext),
00119
00120 .inputs = (const AVFilterPad[]) {
00121 {
00122 .name = "default",
00123 .type = AVMEDIA_TYPE_VIDEO,
00124 .get_video_buffer = ff_null_get_video_buffer,
00125 .start_frame = start_frame,
00126 .draw_slice = draw_slice,
00127 .end_frame = end_frame,
00128 },
00129 { .name = NULL }
00130 },
00131 .outputs = (const AVFilterPad[]) {
00132 {
00133 .name = "default",
00134 .type = AVMEDIA_TYPE_VIDEO,
00135 .config_props = config_output_props,
00136 .request_frame = request_frame,
00137 },
00138 { .name = NULL }
00139 },
00140 };