avfilter/vf_unsharp: add 10bit support

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
This commit is contained in:
Limin Wang 2019-10-14 18:27:05 +08:00
parent 0302728c9e
commit ee792ebe08
2 changed files with 90 additions and 75 deletions

View File

@ -48,9 +48,12 @@ typedef struct UnsharpContext {
UnsharpFilterParam luma; ///< luma parameters (width, height, amount) UnsharpFilterParam luma; ///< luma parameters (width, height, amount)
UnsharpFilterParam chroma; ///< chroma parameters (width, height, amount) UnsharpFilterParam chroma; ///< chroma parameters (width, height, amount)
int hsub, vsub; int hsub, vsub;
int bitdepth;
int bps;
int nb_threads; int nb_threads;
int opencl; int opencl;
int (* apply_unsharp)(AVFilterContext *ctx, AVFrame *in, AVFrame *out); int (* apply_unsharp)(AVFilterContext *ctx, AVFrame *in, AVFrame *out);
int (* unsharp_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
} UnsharpContext; } UnsharpContext;
#endif /* AVFILTER_UNSHARP_H */ #endif /* AVFILTER_UNSHARP_H */

View File

@ -57,81 +57,90 @@ typedef struct TheadData {
int height; int height;
} ThreadData; } ThreadData;
static int unsharp_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) #define DEF_UNSHARP_SLICE_FUNC(name, nbits) \
{ static int name##_##nbits(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
ThreadData *td = arg; { \
UnsharpFilterParam *fp = td->fp; ThreadData *td = arg; \
uint32_t **sc = fp->sc; UnsharpFilterParam *fp = td->fp; \
uint32_t *sr = fp->sr; UnsharpContext *s = ctx->priv; \
const uint8_t *src2 = NULL; //silence a warning uint32_t **sc = fp->sc; \
const int amount = fp->amount; uint32_t *sr = fp->sr; \
const int steps_x = fp->steps_x; const uint##nbits##_t *src2 = NULL; \
const int steps_y = fp->steps_y; const int amount = fp->amount; \
const int scalebits = fp->scalebits; const int steps_x = fp->steps_x; \
const int32_t halfscale = fp->halfscale; const int steps_y = fp->steps_y; \
const int scalebits = fp->scalebits; \
uint8_t *dst = td->dst; const int32_t halfscale = fp->halfscale; \
const uint8_t *src = td->src; \
const int dst_stride = td->dst_stride; uint##nbits##_t *dst = (uint##nbits##_t*)td->dst; \
const int src_stride = td->src_stride; const uint##nbits##_t *src = (const uint##nbits##_t *)td->src; \
const int width = td->width; int dst_stride = td->dst_stride; \
const int height = td->height; int src_stride = td->src_stride; \
const int sc_offset = jobnr * 2 * steps_y; const int width = td->width; \
const int sr_offset = jobnr * (MAX_MATRIX_SIZE - 1); const int height = td->height; \
const int slice_start = (height * jobnr) / nb_jobs; const int sc_offset = jobnr * 2 * steps_y; \
const int slice_end = (height * (jobnr+1)) / nb_jobs; const int sr_offset = jobnr * (MAX_MATRIX_SIZE - 1); \
const int slice_start = (height * jobnr) / nb_jobs; \
int32_t res; const int slice_end = (height * (jobnr+1)) / nb_jobs; \
int x, y, z; \
uint32_t tmp1, tmp2; int32_t res; \
int x, y, z; \
if (!amount) { uint32_t tmp1, tmp2; \
av_image_copy_plane(dst + slice_start * dst_stride, dst_stride, \
src + slice_start * src_stride, src_stride, if (!amount) { \
width, slice_end - slice_start); av_image_copy_plane(td->dst + slice_start * dst_stride, dst_stride, \
return 0; td->src + slice_start * src_stride, src_stride, \
} width * s->bps, slice_end - slice_start); \
return 0; \
for (y = 0; y < 2 * steps_y; y++) } \
memset(sc[sc_offset + y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x)); \
for (y = 0; y < 2 * steps_y; y++) \
// if this is not the first tile, we start from (slice_start - steps_y), memset(sc[sc_offset + y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x)); \
// so we can get smooth result at slice boundary \
if (slice_start > steps_y) { dst_stride = dst_stride / s->bps; \
src += (slice_start - steps_y) * src_stride; src_stride = src_stride / s->bps; \
dst += (slice_start - steps_y) * dst_stride; /* if this is not the first tile, we start from (slice_start - steps_y) */ \
} /* so we can get smooth result at slice boundary */ \
if (slice_start > steps_y) { \
for (y = -steps_y + slice_start; y < steps_y + slice_end; y++) { src += (slice_start - steps_y) * src_stride; \
if (y < height) dst += (slice_start - steps_y) * dst_stride; \
src2 = src; } \
\
memset(sr + sr_offset, 0, sizeof(sr[0]) * (2 * steps_x - 1)); for (y = -steps_y + slice_start; y < steps_y + slice_end; y++) { \
for (x = -steps_x; x < width + steps_x; x++) { if (y < height) \
tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x]; src2 = src; \
for (z = 0; z < steps_x * 2; z += 2) { \
tmp2 = sr[sr_offset + z + 0] + tmp1; sr[sr_offset + z + 0] = tmp1; memset(sr + sr_offset, 0, sizeof(sr[0]) * (2 * steps_x - 1)); \
tmp1 = sr[sr_offset + z + 1] + tmp2; sr[sr_offset + z + 1] = tmp2; for (x = -steps_x; x < width + steps_x; x++) { \
} tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x]; \
for (z = 0; z < steps_y * 2; z += 2) { for (z = 0; z < steps_x * 2; z += 2) { \
tmp2 = sc[sc_offset + z + 0][x + steps_x] + tmp1; sc[sc_offset + z + 0][x + steps_x] = tmp1; tmp2 = sr[sr_offset + z + 0] + tmp1; sr[sr_offset + z + 0] = tmp1; \
tmp1 = sc[sc_offset + z + 1][x + steps_x] + tmp2; sc[sc_offset + z + 1][x + steps_x] = tmp2; tmp1 = sr[sr_offset + z + 1] + tmp2; sr[sr_offset + z + 1] = tmp2; \
} } \
if (x >= steps_x && y >= (steps_y + slice_start)) { for (z = 0; z < steps_y * 2; z += 2) { \
const uint8_t *srx = src - steps_y * src_stride + x - steps_x; tmp2 = sc[sc_offset + z + 0][x + steps_x] + tmp1; \
uint8_t *dsx = dst - steps_y * dst_stride + x - steps_x; sc[sc_offset + z + 0][x + steps_x] = tmp1; \
tmp1 = sc[sc_offset + z + 1][x + steps_x] + tmp2; \
res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> 16); sc[sc_offset + z + 1][x + steps_x] = tmp2; \
*dsx = av_clip_uint8(res); } \
} if (x >= steps_x && y >= (steps_y + slice_start)) { \
} const uint##nbits##_t *srx = src - steps_y * src_stride + x - steps_x; \
if (y >= 0) { uint##nbits##_t *dsx = dst - steps_y * dst_stride + x - steps_x; \
dst += dst_stride; \
src += src_stride; res = (int32_t)*srx + ((((int32_t) * srx - \
} (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> (8+nbits)); \
} *dsx = av_clip_uint##nbits(res); \
return 0; } \
} \
if (y >= 0) { \
dst += dst_stride; \
src += src_stride; \
} \
} \
return 0; \
} }
DEF_UNSHARP_SLICE_FUNC(unsharp_slice, 16);
DEF_UNSHARP_SLICE_FUNC(unsharp_slice, 8);
static int apply_unsharp_c(AVFilterContext *ctx, AVFrame *in, AVFrame *out) static int apply_unsharp_c(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
{ {
@ -155,7 +164,7 @@ static int apply_unsharp_c(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
td.height = plane_h[i]; td.height = plane_h[i];
td.dst_stride = out->linesize[i]; td.dst_stride = out->linesize[i];
td.src_stride = in->linesize[i]; td.src_stride = in->linesize[i];
ctx->internal->execute(ctx, unsharp_slice, &td, NULL, FFMIN(plane_h[i], s->nb_threads)); ctx->internal->execute(ctx, s->unsharp_slice, &td, NULL, FFMIN(plane_h[i], s->nb_threads));
} }
return 0; return 0;
} }
@ -238,6 +247,9 @@ static int config_input(AVFilterLink *inlink)
s->hsub = desc->log2_chroma_w; s->hsub = desc->log2_chroma_w;
s->vsub = desc->log2_chroma_h; s->vsub = desc->log2_chroma_h;
s->bitdepth = desc->comp[0].depth;
s->bps = s->bitdepth > 8 ? 2 : 1;
s->unsharp_slice = s->bitdepth > 8 ? unsharp_slice_16 : unsharp_slice_8;
// ensure (height / nb_threads) > 4 * steps_y, // ensure (height / nb_threads) > 4 * steps_y,
// so that we don't have too much overlap between two threads // so that we don't have too much overlap between two threads