]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_field.c
lavfi/framestep: switch to ff_filter_frame API
[ffmpeg] / libavfilter / vf_field.c
1 /*
2  * Copyright (c) 2003 Rich Felker
3  * Copyright (c) 2012 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * field filter, based on libmpcodecs/vf_field.c by Rich Felker
25  */
26
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "internal.h"
31
32 enum FieldType { FIELD_TYPE_TOP = 0, FIELD_TYPE_BOTTOM };
33
34 typedef struct {
35     const AVClass *class;
36     enum FieldType type;
37     int nb_planes;              ///< number of planes of the current format
38 } FieldContext;
39
40 #define OFFSET(x) offsetof(FieldContext, x)
41 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
42
43 static const AVOption field_options[] = {
44     {"type", "set field type (top or bottom)", OFFSET(type), AV_OPT_TYPE_INT, {.i64=FIELD_TYPE_TOP}, 0, 1, FLAGS, "field_type" },
45     {"top",    "select top field",    0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_TOP},    INT_MIN, INT_MAX, FLAGS, "field_type"},
46     {"bottom", "select bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FIELD_TYPE_BOTTOM}, INT_MIN, INT_MAX, FLAGS, "field_type"},
47
48     {NULL}
49 };
50
51 AVFILTER_DEFINE_CLASS(field);
52
53 static av_cold int init(AVFilterContext *ctx, const char *args)
54 {
55     FieldContext *field = ctx->priv;
56     static const char *shorthand[] = { "type", NULL };
57
58     field->class = &field_class;
59     av_opt_set_defaults(field);
60
61     return av_opt_set_from_string(field, args, shorthand, "=", ":");
62 }
63
64 static int config_props_output(AVFilterLink *outlink)
65 {
66     AVFilterContext *ctx = outlink->src;
67     FieldContext *field = ctx->priv;
68     AVFilterLink *inlink = ctx->inputs[0];
69     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
70     int i;
71
72     for (i = 0; i < desc->nb_components; i++)
73         field->nb_planes = FFMAX(field->nb_planes, desc->comp[i].plane);
74     field->nb_planes++;
75
76     outlink->w = inlink->w;
77     outlink->h = (inlink->h + (field->type == FIELD_TYPE_TOP)) / 2;
78
79     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d type:%s -> w:%d h:%d\n",
80            inlink->w, inlink->h, field->type == FIELD_TYPE_BOTTOM ? "bottom" : "top",
81            outlink->w, outlink->h);
82     return 0;
83 }
84
85 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
86 {
87     FieldContext *field = inlink->dst->priv;
88     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
89     AVFilterLink *outlink = inlink->dst->outputs[0];
90     int i;
91
92     if (!outpicref)
93         return AVERROR(ENOMEM);
94
95     outpicref->video->h = outlink->h;
96     outpicref->video->interlaced = 0;
97
98     for (i = 0; i < field->nb_planes; i++) {
99         if (field->type == FIELD_TYPE_BOTTOM)
100             outpicref->data[i] = inpicref->data[i] + inpicref->linesize[i];
101         outpicref->linesize[i] = 2 * inpicref->linesize[i];
102     }
103     return ff_start_frame(outlink, outpicref);
104 }
105
106 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
107 {
108     FieldContext *field = inlink->dst->priv;
109     int y1 = (y + (field->type == FIELD_TYPE_TOP)) / 2;
110     int h1 = (h + (field->type == FIELD_TYPE_TOP)) / 2;
111     return ff_draw_slice(inlink->dst->outputs[0], y1, h1, slice_dir);
112 }
113
114 static const AVFilterPad field_inputs[] = {
115     {
116         .name             = "default",
117         .type             = AVMEDIA_TYPE_VIDEO,
118         .get_video_buffer = ff_null_get_video_buffer,
119         .start_frame      = start_frame,
120         .draw_slice       = draw_slice,
121         .end_frame        = ff_null_end_frame,
122     },
123     { NULL }
124 };
125
126 static const AVFilterPad field_outputs[] = {
127     {
128         .name          = "default",
129         .type          = AVMEDIA_TYPE_VIDEO,
130         .config_props  = config_props_output,
131     },
132     { NULL }
133 };
134
135 AVFilter avfilter_vf_field = {
136     .name          = "field",
137     .description   = NULL_IF_CONFIG_SMALL("Extract a field from the input video."),
138
139     .priv_size     = sizeof(FieldContext),
140     .init          = init,
141
142     .inputs        = field_inputs,
143     .outputs       = field_outputs,
144     .priv_class    = &field_class,
145 };