avfilter/vf_lenscorrection: add >8 depth support

This commit is contained in:
Paul B Mahol 2021-01-25 12:01:49 +01:00
parent 87598435aa
commit 3cc2e15b91

View File

@ -39,6 +39,7 @@ typedef struct LenscorrectionCtx {
int width; int width;
int height; int height;
int hsub, vsub; int hsub, vsub;
int depth;
int nb_planes; int nb_planes;
double cx, cy, k1, k2; double cx, cy, k1, k2;
int interpolation; int interpolation;
@ -66,113 +67,137 @@ AVFILTER_DEFINE_CLASS(lenscorrection);
typedef struct ThreadData { typedef struct ThreadData {
AVFrame *in, *out; AVFrame *in, *out;
int w, h; int w, h;
int depth;
int plane; int plane;
int xcenter, ycenter; int xcenter, ycenter;
int32_t *correction; int32_t *correction;
} ThreadData; } ThreadData;
static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs) #define NEAREST(type, name) \
{ static int filter##name##_slice(AVFilterContext *ctx, void *arg, int job, \
ThreadData *td = arg; int nb_jobs) \
AVFrame *in = td->in; { \
AVFrame *out = td->out; ThreadData *td = arg; \
AVFrame *in = td->in; \
const int w = td->w, h = td->h; AVFrame *out = td->out; \
const int xcenter = td->xcenter; \
const int ycenter = td->ycenter; const int w = td->w, h = td->h; \
const int start = (h * job ) / nb_jobs; const int xcenter = td->xcenter; \
const int end = (h * (job+1)) / nb_jobs; const int ycenter = td->ycenter; \
const int plane = td->plane; const int start = (h * job ) / nb_jobs; \
const int inlinesize = in->linesize[plane]; const int end = (h * (job+1)) / nb_jobs; \
const int outlinesize = out->linesize[plane]; const int plane = td->plane; \
const uint8_t *indata = in->data[plane]; const int inlinesize = in->linesize[plane] / sizeof(type); \
uint8_t *outrow = out->data[plane] + start * outlinesize; const int outlinesize = out->linesize[plane] / sizeof(type); \
int i; const type *indata = (const type *)in->data[plane]; \
for (i = start; i < end; i++, outrow += outlinesize) { type *outrow = (type *)out->data[plane] + start * outlinesize; \
const int off_y = i - ycenter; for (int i = start; i < end; i++, outrow += outlinesize) { \
uint8_t *out = outrow; const int off_y = i - ycenter; \
int j; type *out = outrow; \
for (j = 0; j < w; j++) { for (int j = 0; j < w; j++) { \
const int off_x = j - xcenter; const int off_x = j - xcenter; \
const int64_t radius_mult = td->correction[j + i*w]; const int64_t radius_mult = td->correction[j + i*w]; \
const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24); const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24); \
const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24); const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24); \
const char isvalid = x >= 0 && x < w && y >= 0 && y < h; const char isvalid = x >= 0 && x < w && y >= 0 && y < h; \
*out++ = isvalid ? indata[y * inlinesize + x] : 0; *out++ = isvalid ? indata[y * inlinesize + x] : 0; \
} } \
} } \
return 0; return 0; \
} }
static int filter_slice_bilinear(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
{
ThreadData *td = arg;
AVFrame *in = td->in;
AVFrame *out = td->out;
const int64_t max = (1 << 24) - 1; NEAREST(uint8_t, 8)
const int64_t add = (1 << 23); NEAREST(uint16_t, 16)
const int w = td->w, h = td->h;
const int xcenter = td->xcenter;
const int ycenter = td->ycenter;
const int start = (h * job ) / nb_jobs;
const int end = (h * (job+1)) / nb_jobs;
const int plane = td->plane;
const int inlinesize = in->linesize[plane];
const int outlinesize = out->linesize[plane];
const uint8_t *indata = in->data[plane];
uint8_t *outrow = out->data[plane] + start * outlinesize;
for (int i = start; i < end; i++, outrow += outlinesize) { #define BILINEAR(type, name) \
const int off_y = i - ycenter; static int filter##name##_slice_bilinear(AVFilterContext *ctx, void *arg, \
uint8_t *out = outrow; int job, int nb_jobs) \
{ \
for (int j = 0; j < w; j++) { ThreadData *td = arg; \
const int off_x = j - xcenter; AVFrame *in = td->in; \
const int64_t radius_mult = td->correction[j + i*w]; AVFrame *out = td->out; \
const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24); \
const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24); const int depth = td->depth; \
const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1; const uint64_t max = (1 << 24) - 1; \
const uint64_t add = (1 << 23); \
if (isvalid) { const int w = td->w, h = td->h; \
const int nx = FFMIN(x + 1, w - 1); const int xcenter = td->xcenter; \
const int ny = FFMIN(y + 1, h - 1); const int ycenter = td->ycenter; \
const int64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max); const int start = (h * job ) / nb_jobs; \
const int64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max); const int end = (h * (job+1)) / nb_jobs; \
const int64_t p0 = indata[ y * inlinesize + x]; const int plane = td->plane; \
const int64_t p1 = indata[ y * inlinesize + nx]; const int inlinesize = in->linesize[plane] / sizeof(type); \
const int64_t p2 = indata[ny * inlinesize + x]; const int outlinesize = out->linesize[plane] / sizeof(type); \
const int64_t p3 = indata[ny * inlinesize + nx]; const type *indata = (const type *)in->data[plane]; \
int64_t sum = 0; type *outrow = (type *)out->data[plane] + start * outlinesize; \
\
sum += (max - du) * (max - dv) * p0; for (int i = start; i < end; i++, outrow += outlinesize) { \
sum += ( du) * (max - dv) * p1; const int off_y = i - ycenter; \
sum += (max - du) * ( dv) * p2; type *out = outrow; \
sum += ( du) * ( dv) * p3; \
for (int j = 0; j < w; j++) { \
out[j] = av_clip_uint8((sum + (1LL << 47)) >> 48); const int off_x = j - xcenter; \
} else { const int64_t radius_mult = td->correction[j + i*w]; \
out[j] = 0; const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24); \
} const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24); \
} const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1; \
} \
if (isvalid) { \
return 0; const int nx = FFMIN(x + 1, w - 1); \
const int ny = FFMIN(y + 1, h - 1); \
const uint64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max); \
const uint64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max); \
const uint64_t p0 = indata[ y * inlinesize + x]; \
const uint64_t p1 = indata[ y * inlinesize + nx]; \
const uint64_t p2 = indata[ny * inlinesize + x]; \
const uint64_t p3 = indata[ny * inlinesize + nx]; \
uint64_t sum = 0; \
\
sum += (max - du) * (max - dv) * p0; \
sum += ( du) * (max - dv) * p1; \
sum += (max - du) * ( dv) * p2; \
sum += ( du) * ( dv) * p3; \
\
out[j] = av_clip_uintp2_c((sum + (1ULL << 47)) >> 48, depth); \
} else { \
out[j] = 0; \
} \
} \
} \
\
return 0; \
} }
BILINEAR(uint8_t, 8)
BILINEAR(uint16_t, 16)
static int query_formats(AVFilterContext *ctx) static int query_formats(AVFilterContext *ctx)
{ {
static const enum AVPixelFormat pix_fmts[] = { static const enum AVPixelFormat pix_fmts[] = {
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
AV_PIX_FMT_GRAY16,
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ411P,
AV_PIX_FMT_GBRP, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
AV_PIX_FMT_YUV440P10,
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
AV_PIX_FMT_YUV440P12,
AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
AV_PIX_FMT_GBRAP, AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
AV_PIX_FMT_NONE AV_PIX_FMT_NONE
}; };
AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
@ -197,14 +222,16 @@ static int config_props(AVFilterLink *outlink)
LenscorrectionCtx *rect = ctx->priv; LenscorrectionCtx *rect = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0]; AVFilterLink *inlink = ctx->inputs[0];
const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format); const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
rect->depth = pixdesc->comp[0].depth;
rect->hsub = pixdesc->log2_chroma_w; rect->hsub = pixdesc->log2_chroma_w;
rect->vsub = pixdesc->log2_chroma_h; rect->vsub = pixdesc->log2_chroma_h;
outlink->w = rect->width = inlink->w; outlink->w = rect->width = inlink->w;
outlink->h = rect->height = inlink->h; outlink->h = rect->height = inlink->h;
rect->nb_planes = av_pix_fmt_count_planes(inlink->format); rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
rect->filter_slice = filter_slice; rect->filter_slice = rect->depth <= 8 ? filter8_slice : filter16_slice;
if (rect->interpolation) if (rect->interpolation)
rect->filter_slice = filter_slice_bilinear; rect->filter_slice = rect->depth <= 8 ? filter8_slice_bilinear : filter16_slice_bilinear;
return 0; return 0;
} }
@ -239,7 +266,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
.h = h, .h = h,
.xcenter = xcenter, .xcenter = xcenter,
.ycenter = ycenter, .ycenter = ycenter,
.plane = plane}; .plane = plane,
.depth = rect->depth,
};
if (!rect->correction[plane]) { if (!rect->correction[plane]) {
int i,j; int i,j;