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/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
35 typedef struct FieldOrderContext
{
37 int dst_tff
; ///< output bff/tff
38 int line_size
[4]; ///< bytes of pixel data per line for each plane
41 static int query_formats(AVFilterContext
*ctx
)
43 AVFilterFormats
*formats
;
44 enum AVPixelFormat pix_fmt
;
47 /** accept any input pixel format that is not hardware accelerated, not
48 * a bitstream format, and does not have vertically sub-sampled chroma */
50 const AVPixFmtDescriptor
*desc
= NULL
;
52 while ((desc
= av_pix_fmt_desc_next(desc
))) {
53 pix_fmt
= av_pix_fmt_desc_get_id(desc
);
54 if (!(desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
||
55 desc
->flags
& AV_PIX_FMT_FLAG_PAL
||
56 desc
->flags
& AV_PIX_FMT_FLAG_BITSTREAM
) &&
57 desc
->nb_components
&& !desc
->log2_chroma_h
&&
58 (ret
= ff_add_format(&formats
, pix_fmt
)) < 0) {
59 ff_formats_unref(&formats
);
63 ff_formats_ref(formats
, &ctx
->inputs
[0]->out_formats
);
64 ff_formats_ref(formats
, &ctx
->outputs
[0]->in_formats
);
70 static int config_input(AVFilterLink
*inlink
)
72 AVFilterContext
*ctx
= inlink
->dst
;
73 FieldOrderContext
*s
= ctx
->priv
;
75 return av_image_fill_linesizes(s
->line_size
, inlink
->format
, inlink
->w
);
78 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*frame
)
80 AVFilterContext
*ctx
= inlink
->dst
;
81 FieldOrderContext
*s
= ctx
->priv
;
82 AVFilterLink
*outlink
= ctx
->outputs
[0];
83 int h
, plane
, src_line_step
, dst_line_step
, line_size
, line
;
87 if (!frame
->interlaced_frame
||
88 frame
->top_field_first
== s
->dst_tff
) {
89 av_log(ctx
, AV_LOG_VERBOSE
,
91 frame
->interlaced_frame
?
92 "frame with same field order" : "progressive frame");
93 return ff_filter_frame(outlink
, frame
);
96 if (av_frame_is_writable(frame
)) {
99 out
= ff_get_video_buffer(outlink
, outlink
->w
, outlink
->h
);
101 av_frame_free(&frame
);
102 return AVERROR(ENOMEM
);
104 av_frame_copy_props(out
, frame
);
108 "picture will move %s one line\n",
109 s
->dst_tff
? "up" : "down");
111 for (plane
= 0; plane
< 4 && frame
->data
[plane
] && frame
->linesize
[plane
]; plane
++) {
112 dst_line_step
= out
->linesize
[plane
];
113 src_line_step
= frame
->linesize
[plane
];
114 line_size
= s
->line_size
[plane
];
115 dst
= out
->data
[plane
];
116 src
= frame
->data
[plane
];
118 /** Move every line up one line, working from
119 * the top to the bottom of the frame.
120 * The original top line is lost.
121 * The new last line is created as a copy of the
122 * penultimate line from that field. */
123 for (line
= 0; line
< h
; line
++) {
124 if (1 + line
< frame
->height
) {
125 memcpy(dst
, src
+ src_line_step
, line_size
);
127 memcpy(dst
, src
- 2 * src_line_step
, line_size
);
129 dst
+= dst_line_step
;
130 src
+= src_line_step
;
133 /** Move every line down one line, working from
134 * the bottom to the top of the frame.
135 * The original bottom line is lost.
136 * The new first line is created as a copy of the
137 * second line from that field. */
138 dst
+= (h
- 1) * dst_line_step
;
139 src
+= (h
- 1) * src_line_step
;
140 for (line
= h
- 1; line
>= 0 ; line
--) {
142 memcpy(dst
, src
- src_line_step
, line_size
);
144 memcpy(dst
, src
+ 2 * src_line_step
, line_size
);
146 dst
-= dst_line_step
;
147 src
-= src_line_step
;
151 out
->top_field_first
= s
->dst_tff
;
154 av_frame_free(&frame
);
155 return ff_filter_frame(outlink
, out
);
158 #define OFFSET(x) offsetof(FieldOrderContext, x)
159 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
161 static const AVOption fieldorder_options
[] = {
162 { "order", "output field order", OFFSET(dst_tff
), AV_OPT_TYPE_INT
, { .i64
= 1 }, 0, 1, FLAGS
, "order" },
163 { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST
, { .i64
= 0 }, .flags
=FLAGS
, .unit
= "order" },
164 { "tff", "top field first", 0, AV_OPT_TYPE_CONST
, { .i64
= 1 }, .flags
=FLAGS
, .unit
= "order" },
168 AVFILTER_DEFINE_CLASS(fieldorder
);
170 static const AVFilterPad avfilter_vf_fieldorder_inputs
[] = {
173 .type
= AVMEDIA_TYPE_VIDEO
,
174 .config_props
= config_input
,
175 .filter_frame
= filter_frame
,
180 static const AVFilterPad avfilter_vf_fieldorder_outputs
[] = {
183 .type
= AVMEDIA_TYPE_VIDEO
,
188 AVFilter ff_vf_fieldorder
= {
189 .name
= "fieldorder",
190 .description
= NULL_IF_CONFIG_SMALL("Set the field order."),
191 .priv_size
= sizeof(FieldOrderContext
),
192 .priv_class
= &fieldorder_class
,
193 .query_formats
= query_formats
,
194 .inputs
= avfilter_vf_fieldorder_inputs
,
195 .outputs
= avfilter_vf_fieldorder_outputs
,
196 .flags
= AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
,