]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fieldorder.c
vf_fieldorder: switch to an AVOptions-based system.
[ffmpeg] / libavfilter / vf_fieldorder.c
1 /*
2  * Copyright (c) 2011 Mark Himsley
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * video field order filter, heavily influenced by vf_pad.c
24  */
25
26 /* #define DEBUG */
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "libavutil/imgutils.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "video.h"
39
40 typedef struct
41 {
42     const AVClass *class;
43     int dst_tff;               ///< output bff/tff
44     int          line_size[4]; ///< bytes of pixel data per line for each plane
45 } FieldOrderContext;
46
47 static int query_formats(AVFilterContext *ctx)
48 {
49     AVFilterFormats  *formats;
50     enum AVPixelFormat pix_fmt;
51     int              ret;
52
53     /** accept any input pixel format that is not hardware accelerated, not
54      *  a bitstream format, and does not have vertically sub-sampled chroma */
55     if (ctx->inputs[0]) {
56         formats = NULL;
57         for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
58             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
59             if (!(desc->flags & PIX_FMT_HWACCEL ||
60                   desc->flags & PIX_FMT_BITSTREAM) &&
61                 desc->nb_components && !desc->log2_chroma_h &&
62                 (ret = ff_add_format(&formats, pix_fmt)) < 0) {
63                 ff_formats_unref(&formats);
64                 return ret;
65             }
66         }
67         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
68         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
69     }
70
71     return 0;
72 }
73
74 static int config_input(AVFilterLink *inlink)
75 {
76     AVFilterContext   *ctx        = inlink->dst;
77     FieldOrderContext *fieldorder = ctx->priv;
78     int               plane;
79
80     /** full an array with the number of bytes that the video
81      *  data occupies per line for each plane of the input video */
82     for (plane = 0; plane < 4; plane++) {
83         fieldorder->line_size[plane] = av_image_get_linesize(
84                 inlink->format,
85                 inlink->w,
86                 plane);
87     }
88
89     return 0;
90 }
91
92 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
93 {
94     AVFilterContext   *ctx        = inlink->dst;
95     AVFilterLink      *outlink    = ctx->outputs[0];
96
97     return ff_get_video_buffer(outlink, w, h);
98 }
99
100 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
101 {
102     AVFilterContext   *ctx     = inlink->dst;
103     FieldOrderContext *s       = ctx->priv;
104     AVFilterLink      *outlink = ctx->outputs[0];
105     int h, plane, line_step, line_size, line;
106     uint8_t *data;
107
108     if (!frame->interlaced_frame ||
109         frame->top_field_first == s->dst_tff)
110         return ff_filter_frame(outlink, frame);
111
112     av_dlog(ctx,
113             "picture will move %s one line\n",
114             s->dst_tff ? "up" : "down");
115     h = frame->height;
116     for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
117         line_step = frame->linesize[plane];
118         line_size = s->line_size[plane];
119         data = frame->data[plane];
120         if (s->dst_tff) {
121             /** Move every line up one line, working from
122              *  the top to the bottom of the frame.
123              *  The original top line is lost.
124              *  The new last line is created as a copy of the
125              *  penultimate line from that field. */
126             for (line = 0; line < h; line++) {
127                 if (1 + line < frame->height) {
128                     memcpy(data, data + line_step, line_size);
129                 } else {
130                     memcpy(data, data - line_step - line_step, line_size);
131                 }
132                 data += line_step;
133             }
134         } else {
135             /** Move every line down one line, working from
136              *  the bottom to the top of the frame.
137              *  The original bottom line is lost.
138              *  The new first line is created as a copy of the
139              *  second line from that field. */
140             data += (h - 1) * line_step;
141             for (line = h - 1; line >= 0 ; line--) {
142                 if (line > 0) {
143                     memcpy(data, data - line_step, line_size);
144                 } else {
145                     memcpy(data, data + line_step + line_step, line_size);
146                 }
147                 data -= line_step;
148             }
149         }
150     }
151     frame->top_field_first = s->dst_tff;
152
153     return ff_filter_frame(outlink, frame);
154 }
155
156 #define OFFSET(x) offsetof(FieldOrderContext, x)
157 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
158 static const AVOption options[] = {
159     { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
160         { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .unit = "order" },
161         { "tff", "top field first",    0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .unit = "order" },
162     { NULL },
163 };
164
165 static const AVClass fieldorder_class = {
166     .class_name = "fieldorder",
167     .item_name  = av_default_item_name,
168     .option     = options,
169     .version    = LIBAVUTIL_VERSION_INT,
170 };
171
172 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
173     {
174         .name             = "default",
175         .type             = AVMEDIA_TYPE_VIDEO,
176         .config_props     = config_input,
177         .get_video_buffer = get_video_buffer,
178         .filter_frame     = filter_frame,
179         .needs_writable   = 1,
180     },
181     { NULL }
182 };
183
184 static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
185     {
186         .name = "default",
187         .type = AVMEDIA_TYPE_VIDEO,
188     },
189     { NULL }
190 };
191
192 AVFilter avfilter_vf_fieldorder = {
193     .name          = "fieldorder",
194     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
195     .priv_size     = sizeof(FieldOrderContext),
196     .priv_class    = &fieldorder_class,
197     .query_formats = query_formats,
198     .inputs        = avfilter_vf_fieldorder_inputs,
199     .outputs       = avfilter_vf_fieldorder_outputs,
200 };