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