]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_fieldorder.c
libspeex: support ZygoAudio (quality 10 mode)
[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/pixdesc.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34
35 enum FieldOrder {
36     ORDER_TFF,
37     ORDER_BFF,
38     ORDER_NB,
39 };
40
41 typedef struct {
42     const AVClass *class;
43     enum FieldOrder order;
44     unsigned int dst_tff;      ///< output bff/tff
45     int          line_size[4]; ///< bytes of pixel data per line for each plane
46 } FieldOrderContext;
47
48 #define OFFSET(x) offsetof(FieldOrderContext, x)
49 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
50
51 static const AVOption fieldorder_options[] = {
52     { "order", "set output field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=ORDER_TFF}, 0, ORDER_NB-1, FLAGS, "order" },
53     { "tff",   "set top field first",    0, AV_OPT_TYPE_CONST, {.i64=ORDER_TFF}, .flags=FLAGS, .unit="order" },
54     { "bff",   "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=ORDER_BFF}, .flags=FLAGS, .unit="order" },
55     { NULL }
56 };
57
58 AVFILTER_DEFINE_CLASS(fieldorder);
59
60 static av_cold int init(AVFilterContext *ctx, const char *args)
61 {
62     FieldOrderContext *fieldorder = ctx->priv;
63     int ret;
64     static const char *shorthand[] = { "order", NULL };
65
66     fieldorder->class = &fieldorder_class;
67     av_opt_set_defaults(fieldorder);
68
69     if ((ret = av_opt_set_from_string(fieldorder, args, shorthand, "=", ":")) < 0)
70         return ret;
71
72     fieldorder->dst_tff = fieldorder->order == ORDER_TFF;
73     av_log(ctx, AV_LOG_VERBOSE, "tff:%d\n", fieldorder->dst_tff);
74
75     return 0;
76 }
77
78 static int query_formats(AVFilterContext *ctx)
79 {
80     AVFilterFormats  *formats;
81     enum AVPixelFormat pix_fmt;
82     int              ret;
83
84     /** accept any input pixel format that is not hardware accelerated, not
85      *  a bitstream format, and does not have vertically sub-sampled chroma */
86     if (ctx->inputs[0]) {
87         formats = NULL;
88         for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
89             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
90             if (!(desc->flags & PIX_FMT_HWACCEL ||
91                   desc->flags & PIX_FMT_BITSTREAM) &&
92                 desc->nb_components && !desc->log2_chroma_h &&
93                 (ret = ff_add_format(&formats, pix_fmt)) < 0) {
94                 ff_formats_unref(&formats);
95                 return ret;
96             }
97         }
98         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
99         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
100     }
101
102     return 0;
103 }
104
105 static int config_input(AVFilterLink *inlink)
106 {
107     AVFilterContext   *ctx        = inlink->dst;
108     FieldOrderContext *fieldorder = ctx->priv;
109     int               plane;
110
111     /** full an array with the number of bytes that the video
112      *  data occupies per line for each plane of the input video */
113     for (plane = 0; plane < 4; plane++) {
114         fieldorder->line_size[plane] = av_image_get_linesize(
115                 inlink->format,
116                 inlink->w,
117                 plane);
118     }
119
120     return 0;
121 }
122
123 static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
124 {
125     AVFilterContext   *ctx        = inlink->dst;
126     AVFilterLink      *outlink    = ctx->outputs[0];
127
128     return ff_get_video_buffer(outlink, w, h);
129 }
130
131 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
132 {
133     AVFilterContext   *ctx     = inlink->dst;
134     FieldOrderContext *s       = ctx->priv;
135     AVFilterLink      *outlink = ctx->outputs[0];
136     int h, plane, line_step, line_size, line;
137     uint8_t *data;
138
139     if (!frame->interlaced_frame ||
140         frame->top_field_first == s->dst_tff)
141         return ff_filter_frame(outlink, frame);
142
143     av_dlog(ctx,
144             "picture will move %s one line\n",
145             s->dst_tff ? "up" : "down");
146     h = frame->height;
147     for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
148         line_step = frame->linesize[plane];
149         line_size = s->line_size[plane];
150         data = frame->data[plane];
151         if (s->dst_tff) {
152             /** Move every line up one line, working from
153              *  the top to the bottom of the frame.
154              *  The original top line is lost.
155              *  The new last line is created as a copy of the
156              *  penultimate line from that field. */
157             for (line = 0; line < h; line++) {
158                 if (1 + line < frame->height) {
159                     memcpy(data, data + line_step, line_size);
160                 } else {
161                     memcpy(data, data - line_step - line_step, line_size);
162                 }
163                 data += line_step;
164             }
165         } else {
166             /** Move every line down one line, working from
167              *  the bottom to the top of the frame.
168              *  The original bottom line is lost.
169              *  The new first line is created as a copy of the
170              *  second line from that field. */
171             data += (h - 1) * line_step;
172             for (line = h - 1; line >= 0 ; line--) {
173                 if (line > 0) {
174                     memcpy(data, data - line_step, line_size);
175                 } else {
176                     memcpy(data, data + line_step + line_step, line_size);
177                 }
178                 data -= line_step;
179             }
180         }
181     }
182     frame->top_field_first = s->dst_tff;
183
184     return ff_filter_frame(outlink, frame);
185 }
186
187 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
188     {
189         .name             = "default",
190         .type             = AVMEDIA_TYPE_VIDEO,
191         .config_props     = config_input,
192         .get_video_buffer = get_video_buffer,
193         .filter_frame     = filter_frame,
194         .needs_writable   = 1,
195     },
196     { NULL }
197 };
198
199 static const AVFilterPad avfilter_vf_fieldorder_outputs[] = {
200     {
201         .name = "default",
202         .type = AVMEDIA_TYPE_VIDEO,
203     },
204     { NULL }
205 };
206
207 AVFilter avfilter_vf_fieldorder = {
208     .name          = "fieldorder",
209     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
210     .init          = init,
211     .priv_size     = sizeof(FieldOrderContext),
212     .query_formats = query_formats,
213     .inputs        = avfilter_vf_fieldorder_inputs,
214     .outputs       = avfilter_vf_fieldorder_outputs,
215     .priv_class    = &fieldorder_class,
216 };