]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fieldorder.c
Merge commit '364af376f343d4706c4cdb7ab9fe0863994e6c01'
[ffmpeg] / libavfilter / vf_fieldorder.c
1 /*
2  * Copyright (c) 2011 Mark Himsley
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * video field order filter, heavily influenced by vf_pad.c
24  */
25
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34
35 typedef struct {
36     const AVClass *class;
37     int dst_tff;               ///< output bff/tff
38     int          line_size[4]; ///< bytes of pixel data per line for each plane
39 } FieldOrderContext;
40
41 static int query_formats(AVFilterContext *ctx)
42 {
43     AVFilterFormats  *formats;
44     enum AVPixelFormat pix_fmt;
45     int              ret;
46
47     /** accept any input pixel format that is not hardware accelerated, not
48      *  a bitstream format, and does not have vertically sub-sampled chroma */
49     if (ctx->inputs[0]) {
50         formats = NULL;
51         for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
52             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
53             if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
54                   desc->flags & AV_PIX_FMT_FLAG_PAL     ||
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);
59                 return ret;
60             }
61         }
62         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
63         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
64     }
65
66     return 0;
67 }
68
69 static int config_input(AVFilterLink *inlink)
70 {
71     AVFilterContext   *ctx = inlink->dst;
72     FieldOrderContext *s   = ctx->priv;
73
74     return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
75 }
76
77 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
78 {
79     AVFilterContext   *ctx     = inlink->dst;
80     FieldOrderContext *s       = ctx->priv;
81     AVFilterLink      *outlink = ctx->outputs[0];
82     int h, plane, src_line_step, dst_line_step, line_size, line;
83     uint8_t *dst, *src;
84     AVFrame *out;
85
86     if (!frame->interlaced_frame ||
87         frame->top_field_first == s->dst_tff)
88         return ff_filter_frame(outlink, frame);
89
90     if (av_frame_is_writable(frame)) {
91         out = frame;
92     } else {
93         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
94         if (!out) {
95             av_frame_free(&frame);
96             return AVERROR(ENOMEM);
97         }
98         av_frame_copy_props(out, frame);
99     }
100
101     av_dlog(ctx,
102             "picture will move %s one line\n",
103             s->dst_tff ? "up" : "down");
104     h = frame->height;
105     for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
106         dst_line_step = out->linesize[plane];
107         src_line_step = frame->linesize[plane];
108         line_size = s->line_size[plane];
109         dst = out->data[plane];
110         src = frame->data[plane];
111         if (s->dst_tff) {
112             /** Move every line up one line, working from
113              *  the top to the bottom of the frame.
114              *  The original top line is lost.
115              *  The new last line is created as a copy of the
116              *  penultimate line from that field. */
117             for (line = 0; line < h; line++) {
118                 if (1 + line < frame->height) {
119                     memcpy(dst, src + src_line_step, line_size);
120                 } else {
121                     memcpy(dst, src - 2 * src_line_step, line_size);
122                 }
123                 dst += dst_line_step;
124                 src += src_line_step;
125             }
126         } else {
127             /** Move every line down one line, working from
128              *  the bottom to the top of the frame.
129              *  The original bottom line is lost.
130              *  The new first line is created as a copy of the
131              *  second line from that field. */
132             dst += (h - 1) * dst_line_step;
133             src += (h - 1) * src_line_step;
134             for (line = h - 1; line >= 0 ; line--) {
135                 if (line > 0) {
136                     memcpy(dst, src - src_line_step, line_size);
137                 } else {
138                     memcpy(dst, src + 2 * src_line_step, line_size);
139                 }
140                 dst -= dst_line_step;
141                 src -= src_line_step;
142             }
143         }
144     }
145     out->top_field_first = s->dst_tff;
146
147     if (frame != out)
148         av_frame_free(&frame);
149     return ff_filter_frame(outlink, out);
150 }
151
152 #define OFFSET(x) offsetof(FieldOrderContext, x)
153 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
154
155 static const AVOption fieldorder_options[] = {
156     { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
157         { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
158         { "tff", "top field first",    0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
159     { NULL }
160 };
161
162 AVFILTER_DEFINE_CLASS(fieldorder);
163
164 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
165     {
166         .name         = "default",
167         .type         = AVMEDIA_TYPE_VIDEO,
168         .config_props = config_input,
169         .filter_frame = filter_frame,
170     },
171     { NULL }
172 };
173
174 static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
175     {
176         .name = "default",
177         .type = AVMEDIA_TYPE_VIDEO,
178     },
179     { NULL }
180 };
181
182 AVFilter avfilter_vf_fieldorder = {
183     .name          = "fieldorder",
184     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
185     .priv_size     = sizeof(FieldOrderContext),
186     .priv_class    = &fieldorder_class,
187     .query_formats = query_formats,
188     .inputs        = avfilter_vf_fieldorder_inputs,
189     .outputs       = avfilter_vf_fieldorder_outputs,
190     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
191 };