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