History

avfilter/vf_lenscorrection: add >8 depth support

Paul B Mahol committed 5 years ago

Commit 3cc2e151 parent 87598431 file · +119 −90Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_lenscorrection.c+119−90
@@ -39,6 +39,7 @@ typedef struct LenscorrectionCtx { · 38 unmodified lines
3939 int width;
4040 int height;
4141 int hsub, vsub;
42+ int depth;
4243 int nb_planes;
4344 double cx, cy, k1, k2;
4445 int interpolation;
@@ -66,113 +67,137 @@ AVFILTER_DEFINE_CLASS(lenscorrection); · 21 unmodified lines
6667 typedef struct ThreadData {
6768 AVFrame *in, *out;
6869 int w, h;
70+ int depth;
6971 int plane;
7072 int xcenter, ycenter;
7173 int32_t *correction;
7274 } ThreadData;
7375
74−static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
75−{
76− ThreadData *td = arg;
77− AVFrame *in = td->in;
78− AVFrame *out = td->out;
79−
80− const int w = td->w, h = td->h;
81− const int xcenter = td->xcenter;
82− const int ycenter = td->ycenter;
83− const int start = (h * job ) / nb_jobs;
84− const int end = (h * (job+1)) / nb_jobs;
85− const int plane = td->plane;
86− const int inlinesize = in->linesize[plane];
87− const int outlinesize = out->linesize[plane];
88− const uint8_t *indata = in->data[plane];
89− uint8_t *outrow = out->data[plane] + start * outlinesize;
90− int i;
91− for (i = start; i < end; i++, outrow += outlinesize) {
92− const int off_y = i - ycenter;
93− uint8_t *out = outrow;
94− int j;
95− for (j = 0; j < w; j++) {
96− const int off_x = j - xcenter;
97− const int64_t radius_mult = td->correction[j + i*w];
98− const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24);
99− const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24);
100− const char isvalid = x >= 0 && x < w && y >= 0 && y < h;
101− *out++ = isvalid ? indata[y * inlinesize + x] : 0;
102− }
103− }
104− return 0;
76+#define NEAREST(type, name) \
77+static int filter##name##_slice(AVFilterContext *ctx, void *arg, int job, \
78+ int nb_jobs) \
79+{ \
80+ ThreadData *td = arg; \
81+ AVFrame *in = td->in; \
82+ AVFrame *out = td->out; \
83+ \
84+ const int w = td->w, h = td->h; \
85+ const int xcenter = td->xcenter; \
86+ const int ycenter = td->ycenter; \
87+ const int start = (h * job ) / nb_jobs; \
88+ const int end = (h * (job+1)) / nb_jobs; \
89+ const int plane = td->plane; \
90+ const int inlinesize = in->linesize[plane] / sizeof(type); \
91+ const int outlinesize = out->linesize[plane] / sizeof(type); \
92+ const type *indata = (const type *)in->data[plane]; \
93+ type *outrow = (type *)out->data[plane] + start * outlinesize; \
94+ for (int i = start; i < end; i++, outrow += outlinesize) { \
95+ const int off_y = i - ycenter; \
96+ type *out = outrow; \
97+ for (int j = 0; j < w; j++) { \
98+ const int off_x = j - xcenter; \
99+ const int64_t radius_mult = td->correction[j + i*w]; \
100+ const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24); \
101+ const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24); \
102+ const char isvalid = x >= 0 && x < w && y >= 0 && y < h; \
103+ *out++ = isvalid ? indata[y * inlinesize + x] : 0; \
104+ } \
105+ } \
106+ return 0; \
105107 }
106108
107−static int filter_slice_bilinear(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
108−{
109− ThreadData *td = arg;
110− AVFrame *in = td->in;
111− AVFrame *out = td->out;
112−
113− const int64_t max = (1 << 24) - 1;
114− const int64_t add = (1 << 23);
115− const int w = td->w, h = td->h;
116− const int xcenter = td->xcenter;
117− const int ycenter = td->ycenter;
118− const int start = (h * job ) / nb_jobs;
119− const int end = (h * (job+1)) / nb_jobs;
120− const int plane = td->plane;
121− const int inlinesize = in->linesize[plane];
122− const int outlinesize = out->linesize[plane];
123− const uint8_t *indata = in->data[plane];
124− uint8_t *outrow = out->data[plane] + start * outlinesize;
125−
126− for (int i = start; i < end; i++, outrow += outlinesize) {
127− const int off_y = i - ycenter;
128− uint8_t *out = outrow;
129−
130− for (int j = 0; j < w; j++) {
131− const int off_x = j - xcenter;
132− const int64_t radius_mult = td->correction[j + i*w];
133− const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24);
134− const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24);
135− const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1;
136109
137− if (isvalid) {
138− const int nx = FFMIN(x + 1, w - 1);
139− const int ny = FFMIN(y + 1, h - 1);
140− const int64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max);
141− const int64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max);
142− const int64_t p0 = indata[ y * inlinesize + x];
143− const int64_t p1 = indata[ y * inlinesize + nx];
144− const int64_t p2 = indata[ny * inlinesize + x];
145− const int64_t p3 = indata[ny * inlinesize + nx];
146− int64_t sum = 0;
147−
148− sum += (max - du) * (max - dv) * p0;
149− sum += ( du) * (max - dv) * p1;
150− sum += (max - du) * ( dv) * p2;
151− sum += ( du) * ( dv) * p3;
152−
153− out[j] = av_clip_uint8((sum + (1LL << 47)) >> 48);
154− } else {
155− out[j] = 0;
156− }
157− }
158− }
159−
160− return 0;
110+NEAREST(uint8_t, 8)
111+NEAREST(uint16_t, 16)
112+
113+#define BILINEAR(type, name) \
114+static int filter##name##_slice_bilinear(AVFilterContext *ctx, void *arg, \
115+ int job, int nb_jobs) \
116+{ \
117+ ThreadData *td = arg; \
118+ AVFrame *in = td->in; \
119+ AVFrame *out = td->out; \
120+ \
121+ const int depth = td->depth; \
122+ const uint64_t max = (1 << 24) - 1; \
123+ const uint64_t add = (1 << 23); \
124+ const int w = td->w, h = td->h; \
125+ const int xcenter = td->xcenter; \
126+ const int ycenter = td->ycenter; \
127+ const int start = (h * job ) / nb_jobs; \
128+ const int end = (h * (job+1)) / nb_jobs; \
129+ const int plane = td->plane; \
130+ const int inlinesize = in->linesize[plane] / sizeof(type); \
131+ const int outlinesize = out->linesize[plane] / sizeof(type); \
132+ const type *indata = (const type *)in->data[plane]; \
133+ type *outrow = (type *)out->data[plane] + start * outlinesize; \
134+ \
135+ for (int i = start; i < end; i++, outrow += outlinesize) { \
136+ const int off_y = i - ycenter; \
137+ type *out = outrow; \
138+ \
139+ for (int j = 0; j < w; j++) { \
140+ const int off_x = j - xcenter; \
141+ const int64_t radius_mult = td->correction[j + i*w]; \
142+ const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24); \
143+ const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24); \
144+ const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1; \
145+ \
146+ if (isvalid) { \
147+ const int nx = FFMIN(x + 1, w - 1); \
148+ const int ny = FFMIN(y + 1, h - 1); \
149+ const uint64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max); \
150+ const uint64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max); \
151+ const uint64_t p0 = indata[ y * inlinesize + x]; \
152+ const uint64_t p1 = indata[ y * inlinesize + nx]; \
153+ const uint64_t p2 = indata[ny * inlinesize + x]; \
154+ const uint64_t p3 = indata[ny * inlinesize + nx]; \
155+ uint64_t sum = 0; \
156+ \
157+ sum += (max - du) * (max - dv) * p0; \
158+ sum += ( du) * (max - dv) * p1; \
159+ sum += (max - du) * ( dv) * p2; \
160+ sum += ( du) * ( dv) * p3; \
161+ \
162+ out[j] = av_clip_uintp2_c((sum + (1ULL << 47)) >> 48, depth); \
163+ } else { \
164+ out[j] = 0; \
165+ } \
166+ } \
167+ } \
168+ \
169+ return 0; \
161170 }
162171
172+BILINEAR(uint8_t, 8)
173+BILINEAR(uint16_t, 16)
174+
163175 static int query_formats(AVFilterContext *ctx)
164176 {
165177 static const enum AVPixelFormat pix_fmts[] = {
166− AV_PIX_FMT_GRAY8,
178+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
179+ AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
180+ AV_PIX_FMT_GRAY16,
167181 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
168182 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
169183 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
170184 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
171185 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
172186 AV_PIX_FMT_YUVJ411P,
173− AV_PIX_FMT_GBRP,
187+ AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
188+ AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
189+ AV_PIX_FMT_YUV440P10,
190+ AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
191+ AV_PIX_FMT_YUV440P12,
192+ AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
193+ AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
194+ AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
195+ AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
174196 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
175− AV_PIX_FMT_GBRAP,
197+ AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
198+ AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
199+ AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
200+ AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
176201 AV_PIX_FMT_NONE
177202 };
178203 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
@@ -197,14 +222,16 @@ static int config_props(AVFilterLink *outlink) · 18 unmodified lines
197222 LenscorrectionCtx *rect = ctx->priv;
198223 AVFilterLink *inlink = ctx->inputs[0];
199224 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
225+
226+ rect->depth = pixdesc->comp[0].depth;
200227 rect->hsub = pixdesc->log2_chroma_w;
201228 rect->vsub = pixdesc->log2_chroma_h;
202229 outlink->w = rect->width = inlink->w;
203230 outlink->h = rect->height = inlink->h;
204231 rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
205− rect->filter_slice = filter_slice;
232+ rect->filter_slice = rect->depth <= 8 ? filter8_slice : filter16_slice;
206233 if (rect->interpolation)
207− rect->filter_slice = filter_slice_bilinear;
234+ rect->filter_slice = rect->depth <= 8 ? filter8_slice_bilinear : filter16_slice_bilinear;
208235 return 0;
209236 }
210237
@@ -239,7 +266,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 28 unmodified lines
239266 .h = h,
240267 .xcenter = xcenter,
241268 .ycenter = ycenter,
242− .plane = plane};
269+ .plane = plane,
270+ .depth = rect->depth,
271+ };
243272
244273 if (!rect->correction[plane]) {
245274 int i,j;

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