2 * Copyright (c) 2011 Mark Himsley
4 * This file is part of FFmpeg.
6 * FFmpeg 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.
11 * FFmpeg 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * video field order filter, heavily influenced by vf_pad.c
26 #include "libavutil/opt.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
38 int dst_tff; ///< output bff/tff
39 int line_size[4]; ///< bytes of pixel data per line for each plane
42 static int query_formats(AVFilterContext *ctx)
44 AVFilterFormats *formats;
45 enum AVPixelFormat pix_fmt;
48 /** accept any input pixel format that is not hardware accelerated, not
49 * a bitstream format, and does not have vertically sub-sampled chroma */
52 for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
53 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
54 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
55 desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
56 desc->nb_components && !desc->log2_chroma_h &&
57 (ret = ff_add_format(&formats, pix_fmt)) < 0) {
58 ff_formats_unref(&formats);
62 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
63 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
69 static int config_input(AVFilterLink *inlink)
71 AVFilterContext *ctx = inlink->dst;
72 FieldOrderContext *s = ctx->priv;
74 return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
77 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
79 AVFilterContext *ctx = inlink->dst;
80 FieldOrderContext *s = ctx->priv;
81 AVFilterLink *outlink = ctx->outputs[0];
82 int h, plane, line_step, line_size, line;
85 if (!frame->interlaced_frame ||
86 frame->top_field_first == s->dst_tff)
87 return ff_filter_frame(outlink, frame);
90 "picture will move %s one line\n",
91 s->dst_tff ? "up" : "down");
93 for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
94 line_step = frame->linesize[plane];
95 line_size = s->line_size[plane];
96 data = frame->data[plane];
98 /** Move every line up one line, working from
99 * the top to the bottom of the frame.
100 * The original top line is lost.
101 * The new last line is created as a copy of the
102 * penultimate line from that field. */
103 for (line = 0; line < h; line++) {
104 if (1 + line < frame->height) {
105 memcpy(data, data + line_step, line_size);
107 memcpy(data, data - line_step - line_step, line_size);
112 /** Move every line down one line, working from
113 * the bottom to the top of the frame.
114 * The original bottom line is lost.
115 * The new first line is created as a copy of the
116 * second line from that field. */
117 data += (h - 1) * line_step;
118 for (line = h - 1; line >= 0 ; line--) {
120 memcpy(data, data - line_step, line_size);
122 memcpy(data, data + line_step + line_step, line_size);
128 frame->top_field_first = s->dst_tff;
130 return ff_filter_frame(outlink, frame);
133 #define OFFSET(x) offsetof(FieldOrderContext, x)
134 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
136 static const AVOption fieldorder_options[] = {
137 { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
138 { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
139 { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
143 AVFILTER_DEFINE_CLASS(fieldorder);
145 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
148 .type = AVMEDIA_TYPE_VIDEO,
149 .config_props = config_input,
150 .filter_frame = filter_frame,
156 static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
159 .type = AVMEDIA_TYPE_VIDEO,
164 AVFilter avfilter_vf_fieldorder = {
165 .name = "fieldorder",
166 .description = NULL_IF_CONFIG_SMALL("Set the field order."),
167 .priv_size = sizeof(FieldOrderContext),
168 .priv_class = &fieldorder_class,
169 .query_formats = query_formats,
170 .inputs = avfilter_vf_fieldorder_inputs,
171 .outputs = avfilter_vf_fieldorder_outputs,
172 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,