• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2004 Ville Saari
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 General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * 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 #include "libavutil/avassert.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 enum PhaseMode {
31     PROGRESSIVE,
32     TOP_FIRST,
33     BOTTOM_FIRST,
34     TOP_FIRST_ANALYZE,
35     BOTTOM_FIRST_ANALYZE,
36     ANALYZE,
37     FULL_ANALYZE,
38     AUTO,
39     AUTO_ANALYZE
40 };
41 
42 #define DEPTH 8
43 #include "phase_template.c"
44 
45 #undef DEPTH
46 #define DEPTH 9
47 #include "phase_template.c"
48 
49 #undef DEPTH
50 #define DEPTH 10
51 #include "phase_template.c"
52 
53 #undef DEPTH
54 #define DEPTH 12
55 #include "phase_template.c"
56 
57 #undef DEPTH
58 #define DEPTH 14
59 #include "phase_template.c"
60 
61 #undef DEPTH
62 #define DEPTH 16
63 #include "phase_template.c"
64 
65 typedef struct PhaseContext {
66     const AVClass *class;
67     int mode;                   ///<PhaseMode
68     AVFrame *frame; /* previous frame */
69     int nb_planes;
70     int planeheight[4];
71     int linesize[4];
72 
73     enum PhaseMode (*analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new);
74 } PhaseContext;
75 
76 #define OFFSET(x) offsetof(PhaseContext, x)
77 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
78 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
79 
80 static const AVOption phase_options[] = {
81     { "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, "mode" },
82     CONST("p", "progressive",          PROGRESSIVE,          "mode"),
83     CONST("t", "top first",            TOP_FIRST,            "mode"),
84     CONST("b", "bottom first",         BOTTOM_FIRST,         "mode"),
85     CONST("T", "top first analyze",    TOP_FIRST_ANALYZE,    "mode"),
86     CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
87     CONST("u", "analyze",              ANALYZE,              "mode"),
88     CONST("U", "full analyze",         FULL_ANALYZE,         "mode"),
89     CONST("a", "auto",                 AUTO,                 "mode"),
90     CONST("A", "auto analyze",         AUTO_ANALYZE,         "mode"),
91     { NULL }
92 };
93 
94 AVFILTER_DEFINE_CLASS(phase);
95 
96 static const enum AVPixelFormat pix_fmts[] = {
97     AV_PIX_FMT_GRAY8,
98     AV_PIX_FMT_GRAY9,
99     AV_PIX_FMT_GRAY10,
100     AV_PIX_FMT_GRAY12,
101     AV_PIX_FMT_GRAY14,
102     AV_PIX_FMT_GRAY16,
103     AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
104     AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
105     AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
106     AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
107     AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
108     AV_PIX_FMT_YUVJ411P,
109     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
110     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
111     AV_PIX_FMT_YUV440P10,
112     AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
113     AV_PIX_FMT_YUV440P12,
114     AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
115     AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
116     AV_PIX_FMT_GBRP,   AV_PIX_FMT_GBRP9,  AV_PIX_FMT_GBRP10,
117     AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
118     AV_PIX_FMT_YUVA420P,  AV_PIX_FMT_YUVA422P,   AV_PIX_FMT_YUVA444P,
119     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
120     AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
121     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
122     AV_PIX_FMT_GBRAP,     AV_PIX_FMT_GBRAP10,    AV_PIX_FMT_GBRAP12,    AV_PIX_FMT_GBRAP16,
123     AV_PIX_FMT_NONE
124 };
125 
config_input(AVFilterLink * inlink)126 static int config_input(AVFilterLink *inlink)
127 {
128     PhaseContext *s = inlink->dst->priv;
129     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
130     int ret;
131 
132     switch (desc->comp[0].depth) {
133     case  8: s->analyze_plane = analyze_plane_8;  break;
134     case  9: s->analyze_plane = analyze_plane_9;  break;
135     case 10: s->analyze_plane = analyze_plane_10; break;
136     case 12: s->analyze_plane = analyze_plane_12; break;
137     case 14: s->analyze_plane = analyze_plane_14; break;
138     case 16: s->analyze_plane = analyze_plane_16; break;
139     default: av_assert0(0);
140     };
141 
142     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
143         return ret;
144 
145     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
146     s->planeheight[0] = s->planeheight[3] = inlink->h;
147 
148     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
149 
150     return 0;
151 }
152 
filter_frame(AVFilterLink * inlink,AVFrame * in)153 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
154 {
155     AVFilterContext *ctx = inlink->dst;
156     AVFilterLink *outlink = ctx->outputs[0];
157     PhaseContext *s = ctx->priv;
158     enum PhaseMode mode;
159     int plane, top, y;
160     AVFrame *out;
161 
162     if (ctx->is_disabled) {
163         av_frame_free(&s->frame);
164         /* we keep a reference to the previous frame so the filter can start
165          * being useful as soon as it's not disabled, avoiding the 1-frame
166          * delay. */
167         s->frame = av_frame_clone(in);
168         return ff_filter_frame(outlink, in);
169     }
170 
171     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
172     if (!out) {
173         av_frame_free(&in);
174         return AVERROR(ENOMEM);
175     }
176     av_frame_copy_props(out, in);
177 
178     if (!s->frame) {
179         s->frame = in;
180         mode = PROGRESSIVE;
181     } else {
182         mode = s->analyze_plane(ctx, s->mode, s->frame, in);
183     }
184 
185     for (plane = 0; plane < s->nb_planes; plane++) {
186         const uint8_t *buf = s->frame->data[plane];
187         const uint8_t *from = in->data[plane];
188         uint8_t *to = out->data[plane];
189 
190         for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
191             memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
192 
193             buf += s->frame->linesize[plane];
194             from += in->linesize[plane];
195             to += out->linesize[plane];
196         }
197     }
198 
199     if (in != s->frame)
200         av_frame_free(&s->frame);
201     s->frame = in;
202     return ff_filter_frame(outlink, out);
203 }
204 
uninit(AVFilterContext * ctx)205 static av_cold void uninit(AVFilterContext *ctx)
206 {
207     PhaseContext *s = ctx->priv;
208 
209     av_frame_free(&s->frame);
210 }
211 
212 static const AVFilterPad phase_inputs[] = {
213     {
214         .name         = "default",
215         .type         = AVMEDIA_TYPE_VIDEO,
216         .filter_frame = filter_frame,
217         .config_props = config_input,
218     },
219 };
220 
221 static const AVFilterPad phase_outputs[] = {
222     {
223         .name = "default",
224         .type = AVMEDIA_TYPE_VIDEO,
225     },
226 };
227 
228 const AVFilter ff_vf_phase = {
229     .name          = "phase",
230     .description   = NULL_IF_CONFIG_SMALL("Phase shift fields."),
231     .priv_size     = sizeof(PhaseContext),
232     .priv_class    = &phase_class,
233     .uninit        = uninit,
234     FILTER_INPUTS(phase_inputs),
235     FILTER_OUTPUTS(phase_outputs),
236     FILTER_PIXFMTS_ARRAY(pix_fmts),
237     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
238     .process_command = ff_filter_process_command,
239 };
240