2 * Copyright (c) 2011 Mark Himsley
4 * This file is part of Libav.
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.
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.
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
23 * video field order filter, heavily influenced by vf_pad.c
28 #include "libavutil/imgutils.h"
29 #include "libavutil/pixdesc.h"
37 unsigned int dst_tff; ///< output bff/tff
38 int line_size[4]; ///< bytes of pixel data per line for each plane
41 static av_cold int init(AVFilterContext *ctx, const char *args)
43 FieldOrderContext *fieldorder = ctx->priv;
45 const char *tff = "tff";
46 const char *bff = "bff";
49 fieldorder->dst_tff = 1;
50 } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) {
51 fieldorder->dst_tff = !!fieldorder->dst_tff;
52 } else if (!strcmp(tff, args)) {
53 fieldorder->dst_tff = 1;
54 } else if (!strcmp(bff, args)) {
55 fieldorder->dst_tff = 0;
57 av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args);
58 return AVERROR(EINVAL);
61 av_log(ctx, AV_LOG_VERBOSE, "output field order: %s\n",
62 fieldorder->dst_tff ? tff : bff);
67 static int query_formats(AVFilterContext *ctx)
69 AVFilterFormats *formats;
70 enum PixelFormat pix_fmt;
73 /** accept any input pixel format that is not hardware accelerated, not
74 * a bitstream format, and does not have vertically sub-sampled chroma */
77 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
78 if (!( av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL
79 || av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BITSTREAM)
80 && av_pix_fmt_descriptors[pix_fmt].nb_components
81 && !av_pix_fmt_descriptors[pix_fmt].log2_chroma_h
82 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
83 ff_formats_unref(&formats);
86 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
87 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
93 static int config_input(AVFilterLink *inlink)
95 AVFilterContext *ctx = inlink->dst;
96 FieldOrderContext *fieldorder = ctx->priv;
99 /** full an array with the number of bytes that the video
100 * data occupies per line for each plane of the input video */
101 for (plane = 0; plane < 4; plane++) {
102 fieldorder->line_size[plane] = av_image_get_linesize(
111 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
113 AVFilterContext *ctx = inlink->dst;
114 AVFilterLink *outlink = ctx->outputs[0];
116 return ff_get_video_buffer(outlink, perms, w, h);
119 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
121 AVFilterContext *ctx = inlink->dst;
122 AVFilterLink *outlink = ctx->outputs[0];
124 AVFilterBufferRef *outpicref;
126 outpicref = avfilter_ref_buffer(inpicref, ~0);
127 outlink->out_buf = outpicref;
129 ff_start_frame(outlink, outpicref);
132 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
134 AVFilterContext *ctx = inlink->dst;
135 FieldOrderContext *fieldorder = ctx->priv;
136 AVFilterLink *outlink = ctx->outputs[0];
138 AVFilterBufferRef *inpicref = inlink->cur_buf;
140 /** can only currently do slices if this filter is doing nothing
141 * because this filter is moving picture content, the output
142 * slice will contain different video lines than the input slice
143 * and that complexity will be added later */
144 if ( !inpicref->video->interlaced
145 || inpicref->video->top_field_first == fieldorder->dst_tff) {
146 ff_draw_slice(outlink, y, h, slice_dir);
150 static void end_frame(AVFilterLink *inlink)
152 AVFilterContext *ctx = inlink->dst;
153 FieldOrderContext *fieldorder = ctx->priv;
154 AVFilterLink *outlink = ctx->outputs[0];
156 AVFilterBufferRef *inpicref = inlink->cur_buf;
157 AVFilterBufferRef *outpicref = outlink->out_buf;
159 int h, plane, line_step, line_size, line;
160 uint8_t *cpy_src, *cpy_dst;
162 if ( inpicref->video->interlaced
163 && inpicref->video->top_field_first != fieldorder->dst_tff) {
165 "picture will move %s one line\n",
166 fieldorder->dst_tff ? "up" : "down");
167 h = inpicref->video->h;
168 for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) {
169 line_step = inpicref->linesize[plane];
170 line_size = fieldorder->line_size[plane];
171 cpy_src = inpicref->data[plane];
172 cpy_dst = outpicref->data[plane];
173 if (fieldorder->dst_tff) {
174 /** Move every line up one line, working from
175 * the top to the bottom of the frame.
176 * The original top line is lost.
177 * The new last line is created as a copy of the
178 * penultimate line from that field. */
179 for (line = 0; line < h; line++) {
180 if (1 + line < outpicref->video->h) {
181 memcpy(cpy_dst, cpy_src + line_step, line_size);
183 memcpy(cpy_dst, cpy_src - line_step - line_step, line_size);
185 cpy_src += line_step;
186 cpy_dst += line_step;
189 /** Move every line down one line, working from
190 * the bottom to the top of the frame.
191 * The original bottom line is lost.
192 * The new first line is created as a copy of the
193 * second line from that field. */
194 cpy_src += (h - 1) * line_step;
195 cpy_dst += (h - 1) * line_step;
196 for (line = h - 1; line >= 0 ; line--) {
198 memcpy(cpy_dst, cpy_src - line_step, line_size);
200 memcpy(cpy_dst, cpy_src + line_step + line_step, line_size);
202 cpy_src -= line_step;
203 cpy_dst -= line_step;
207 outpicref->video->top_field_first = fieldorder->dst_tff;
208 ff_draw_slice(outlink, 0, h, 1);
211 "not interlaced or field order already correct\n");
214 ff_end_frame(outlink);
215 avfilter_unref_buffer(inpicref);
218 AVFilter avfilter_vf_fieldorder = {
219 .name = "fieldorder",
220 .description = NULL_IF_CONFIG_SMALL("Set the field order."),
222 .priv_size = sizeof(FieldOrderContext),
223 .query_formats = query_formats,
224 .inputs = (AVFilterPad[]) {{ .name = "default",
225 .type = AVMEDIA_TYPE_VIDEO,
226 .config_props = config_input,
227 .start_frame = start_frame,
228 .get_video_buffer = get_video_buffer,
229 .draw_slice = draw_slice,
230 .end_frame = end_frame,
231 .min_perms = AV_PERM_READ,
232 .rej_perms = AV_PERM_REUSE2|AV_PERM_PRESERVE,},
234 .outputs = (AVFilterPad[]) {{ .name = "default",
235 .type = AVMEDIA_TYPE_VIDEO, },