History

avfilter/vf_phase: add >8 bit support

Paul B Mahol committed 6 years ago

Commit 314a7831 parent 732d77d2 files · +246 −148Browse files

Showing the changes to libavfilter (2 of 2 files). Show all

Unified · Split

2 files
  1. libavfilter/phase_template.c +183 −0
  2. libavfilter/vf_phase.c +63 −148
libavfilter/phase_template.c+183−0
@@ -0,0 +1,183 @@
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 "avfilter.h"
23#include "formats.h"
24#include "internal.h"
25#include "video.h"
26
27#undef pixel
28#undef accumulator
29#if DEPTH == 8
30#define pixel uint8_t
31#define accumulator int
32#else
33#define pixel uint16_t
34#define accumulator int64_t
35#endif
36
37#define fn3(a,b) a##_##b
38#define fn2(a,b) fn3(a,b)
39#define fn(a) fn2(a, DEPTH)
40
41/*
42 * This macro interpolates the value of both fields at a point halfway
43 * between lines and takes the squared difference. In field resolution
44 * the point is a quarter pixel below a line in one field and a quarter
45 * pixel above a line in other.
46 *
47 * (The result is actually multiplied by 25)
48 */
49#define DIFF(a, as, b, bs) ((t) = ((*(a) - (b)[bs]) << 2) + (a)[(as) << 1] - (b)[-(bs)], (t) * (t))
50
51/*
52 * Find which field combination has the smallest average squared difference
53 * between the fields.
54 */
55static enum PhaseMode fn(analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
56{
57 double bdiff, tdiff, pdiff;
58
59 if (mode == AUTO) {
60 mode = new->interlaced_frame ? new->top_field_first ?
61 TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
62 } else if (mode == AUTO_ANALYZE) {
63 mode = new->interlaced_frame ? new->top_field_first ?
64 TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
65 }
66
67 if (mode <= BOTTOM_FIRST) {
68 bdiff = pdiff = tdiff = 65536.0;
69 } else {
70 const double factor = 1. / (25. * (1 << (DEPTH - 8)) * (1 << (DEPTH - 8)));
71 const int ns = new->linesize[0] / sizeof(pixel);
72 const int os = old->linesize[0] / sizeof(pixel);
73 const pixel *nptr = (pixel *)new->data[0];
74 const pixel *optr = (pixel *)old->data[0];
75 const int h = new->height;
76 const int w = new->width;
77 accumulator bdif, tdif, pdif;
78 double scale;
79
80 int top = 0, t;
81 const pixel *rend, *end = nptr + (h - 2) * ns;
82
83 bdiff = pdiff = tdiff = 0.0;
84
85 nptr += ns;
86 optr += os;
87 while (nptr < end) {
88 pdif = tdif = bdif = 0;
89
90 switch (mode) {
91 case TOP_FIRST_ANALYZE:
92 if (top) {
93 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
94 pdif += DIFF(nptr, ns, nptr, ns);
95 tdif += DIFF(nptr, ns, optr, os);
96 }
97 } else {
98 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
99 pdif += DIFF(nptr, ns, nptr, ns);
100 tdif += DIFF(optr, os, nptr, ns);
101 }
102 }
103 break;
104 case BOTTOM_FIRST_ANALYZE:
105 if (top) {
106 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
107 pdif += DIFF(nptr, ns, nptr, ns);
108 bdif += DIFF(optr, os, nptr, ns);
109 }
110 } else {
111 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
112 pdif += DIFF(nptr, ns, nptr, ns);
113 bdif += DIFF(nptr, ns, optr, os);
114 }
115 }
116 break;
117 case ANALYZE:
118 if (top) {
119 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
120 tdif += DIFF(nptr, ns, optr, os);
121 bdif += DIFF(optr, os, nptr, ns);
122 }
123 } else {
124 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
125 bdif += DIFF(nptr, ns, optr, os);
126 tdif += DIFF(optr, os, nptr, ns);
127 }
128 }
129 break;
130 case FULL_ANALYZE:
131 if (top) {
132 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
133 pdif += DIFF(nptr, ns, nptr, ns);
134 tdif += DIFF(nptr, ns, optr, os);
135 bdif += DIFF(optr, os, nptr, ns);
136 }
137 } else {
138 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
139 pdif += DIFF(nptr, ns, nptr, ns);
140 bdif += DIFF(nptr, ns, optr, os);
141 tdif += DIFF(optr, os, nptr, ns);
142 }
143 }
144 break;
145 default:
146 av_assert0(0);
147 }
148
149 pdiff += (double)pdif;
150 tdiff += (double)tdif;
151 bdiff += (double)bdif;
152 nptr += ns - w;
153 optr += os - w;
154 top ^= 1;
155 }
156
157 scale = 1.0 / (w * (h - 3)) * factor;
158 pdiff *= scale;
159 tdiff *= scale;
160 bdiff *= scale;
161
162 if (mode == TOP_FIRST_ANALYZE) {
163 bdiff = 65536.0;
164 } else if (mode == BOTTOM_FIRST_ANALYZE) {
165 tdiff = 65536.0;
166 } else if (mode == ANALYZE) {
167 pdiff = 65536.0;
168 }
169
170 if (bdiff < pdiff && bdiff < tdiff) {
171 mode = BOTTOM_FIRST;
172 } else if (tdiff < pdiff && tdiff < bdiff) {
173 mode = TOP_FIRST;
174 } else {
175 mode = PROGRESSIVE;
176 }
177 }
178
179 av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
180 mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
181 tdiff, bdiff, pdiff);
182 return mode;
183}
libavfilter/vf_phase.c+63−148
@@ -39,6 +39,29 @@ enum PhaseMode { · 38 unmodified lines
39 AUTO_ANALYZE39 AUTO_ANALYZE
40};40};
41 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
42typedef struct PhaseContext {65typedef struct PhaseContext {
43 const AVClass *class;66 const AVClass *class;
44 int mode; ///<PhaseMode67 int mode; ///<PhaseMode
@@ -46,6 +69,8 @@ typedef struct PhaseContext { · 1 unmodified line
46 int nb_planes;69 int nb_planes;
47 int planeheight[4];70 int planeheight[4];
48 int linesize[4];71 int linesize[4];
72
73 enum PhaseMode (*analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new);
49} PhaseContext;74} PhaseContext;
50 75
51#define OFFSET(x) offsetof(PhaseContext, x)76#define OFFSET(x) offsetof(PhaseContext, x)
@@ -71,10 +96,33 @@ AVFILTER_DEFINE_CLASS(phase); · 19 unmodified lines
71static int query_formats(AVFilterContext *ctx)96static int query_formats(AVFilterContext *ctx)
72{97{
73 static const enum AVPixelFormat pix_fmts[] = {98 static const enum AVPixelFormat pix_fmts[] = {
74 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,99 AV_PIX_FMT_GRAY8,
75 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,100 AV_PIX_FMT_GRAY9,
76 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,101 AV_PIX_FMT_GRAY10,
77 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE102 AV_PIX_FMT_GRAY12,
103 AV_PIX_FMT_GRAY14,
104 AV_PIX_FMT_GRAY16,
105 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
106 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
107 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
108 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
109 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
110 AV_PIX_FMT_YUVJ411P,
111 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
112 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
113 AV_PIX_FMT_YUV440P10,
114 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
115 AV_PIX_FMT_YUV440P12,
116 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
117 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
118 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
119 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
120 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
121 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
122 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
123 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
124 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
125 AV_PIX_FMT_NONE
78 };126 };
79 127
80 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);128 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
@@ -89,6 +137,16 @@ static int config_input(AVFilterLink *inlink) · 8 unmodified lines
89 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);137 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
90 int ret;138 int ret;
91 139
140 switch (desc->comp[0].depth) {
141 case 8: s->analyze_plane = analyze_plane_8; break;
142 case 9: s->analyze_plane = analyze_plane_9; break;
143 case 10: s->analyze_plane = analyze_plane_10; break;
144 case 12: s->analyze_plane = analyze_plane_12; break;
145 case 14: s->analyze_plane = analyze_plane_14; break;
146 case 16: s->analyze_plane = analyze_plane_16; break;
147 default: av_assert0(0);
148 };
149
92 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)150 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
93 return ret;151 return ret;
94 152
@@ -100,149 +158,6 @@ static int config_input(AVFilterLink *inlink) · 5 unmodified lines
100 return 0;158 return 0;
101}159}
102 160
103/*
104 * This macro interpolates the value of both fields at a point halfway
105 * between lines and takes the squared difference. In field resolution
106 * the point is a quarter pixel below a line in one field and a quarter
107 * pixel above a line in other.
108 *
109 * (The result is actually multiplied by 25)
110 */
111#define DIFF(a, as, b, bs) ((t) = ((*(a) - (b)[bs]) << 2) + (a)[(as) << 1] - (b)[-(bs)], (t) * (t))
112
113/*
114 * Find which field combination has the smallest average squared difference
115 * between the fields.
116 */
117static enum PhaseMode analyze_plane(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
118{
119 double bdiff, tdiff, pdiff;
120
121 if (mode == AUTO) {
122 mode = new->interlaced_frame ? new->top_field_first ?
123 TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
124 } else if (mode == AUTO_ANALYZE) {
125 mode = new->interlaced_frame ? new->top_field_first ?
126 TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
127 }
128
129 if (mode <= BOTTOM_FIRST) {
130 bdiff = pdiff = tdiff = 65536.0;
131 } else {
132 const int ns = new->linesize[0];
133 const int os = old->linesize[0];
134 const uint8_t *nptr = new->data[0];
135 const uint8_t *optr = old->data[0];
136 const int h = new->height;
137 const int w = new->width;
138 int bdif, tdif, pdif;
139 double scale;
140
141 int top = 0, t;
142 const uint8_t *rend, *end = nptr + (h - 2) * ns;
143
144 bdiff = pdiff = tdiff = 0.0;
145
146 nptr += ns;
147 optr += os;
148 while (nptr < end) {
149 pdif = tdif = bdif = 0;
150
151 switch (mode) {
152 case TOP_FIRST_ANALYZE:
153 if (top) {
154 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
155 pdif += DIFF(nptr, ns, nptr, ns);
156 tdif += DIFF(nptr, ns, optr, os);
157 }
158 } else {
159 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
160 pdif += DIFF(nptr, ns, nptr, ns);
161 tdif += DIFF(optr, os, nptr, ns);
162 }
163 }
164 break;
165 case BOTTOM_FIRST_ANALYZE:
166 if (top) {
167 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
168 pdif += DIFF(nptr, ns, nptr, ns);
169 bdif += DIFF(optr, os, nptr, ns);
170 }
171 } else {
172 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
173 pdif += DIFF(nptr, ns, nptr, ns);
174 bdif += DIFF(nptr, ns, optr, os);
175 }
176 }
177 break;
178 case ANALYZE:
179 if (top) {
180 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
181 tdif += DIFF(nptr, ns, optr, os);
182 bdif += DIFF(optr, os, nptr, ns);
183 }
184 } else {
185 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
186 bdif += DIFF(nptr, ns, optr, os);
187 tdif += DIFF(optr, os, nptr, ns);
188 }
189 }
190 break;
191 case FULL_ANALYZE:
192 if (top) {
193 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
194 pdif += DIFF(nptr, ns, nptr, ns);
195 tdif += DIFF(nptr, ns, optr, os);
196 bdif += DIFF(optr, os, nptr, ns);
197 }
198 } else {
199 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
200 pdif += DIFF(nptr, ns, nptr, ns);
201 bdif += DIFF(nptr, ns, optr, os);
202 tdif += DIFF(optr, os, nptr, ns);
203 }
204 }
205 break;
206 default:
207 av_assert0(0);
208 }
209
210 pdiff += (double)pdif;
211 tdiff += (double)tdif;
212 bdiff += (double)bdif;
213 nptr += ns - w;
214 optr += os - w;
215 top ^= 1;
216 }
217
218 scale = 1.0 / (w * (h - 3)) / 25.0;
219 pdiff *= scale;
220 tdiff *= scale;
221 bdiff *= scale;
222
223 if (mode == TOP_FIRST_ANALYZE) {
224 bdiff = 65536.0;
225 } else if (mode == BOTTOM_FIRST_ANALYZE) {
226 tdiff = 65536.0;
227 } else if (mode == ANALYZE) {
228 pdiff = 65536.0;
229 }
230
231 if (bdiff < pdiff && bdiff < tdiff) {
232 mode = BOTTOM_FIRST;
233 } else if (tdiff < pdiff && tdiff < bdiff) {
234 mode = TOP_FIRST;
235 } else {
236 mode = PROGRESSIVE;
237 }
238 }
239
240 av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
241 mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
242 tdiff, bdiff, pdiff);
243 return mode;
244}
245
246static int filter_frame(AVFilterLink *inlink, AVFrame *in)161static int filter_frame(AVFilterLink *inlink, AVFrame *in)
247{162{
248 AVFilterContext *ctx = inlink->dst;163 AVFilterContext *ctx = inlink->dst;
@@ -272,7 +187,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 23 unmodified lines
272 s->frame = in;187 s->frame = in;
273 mode = PROGRESSIVE;188 mode = PROGRESSIVE;
274 } else {189 } else {
275 mode = analyze_plane(ctx, s->mode, s->frame, in);190 mode = s->analyze_plane(ctx, s->mode, s->frame, in);
276 }191 }
277 192
278 for (plane = 0; plane < s->nb_planes; plane++) {193 for (plane = 0; plane < s->nb_planes; plane++) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t