X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_waveform.c;h=8191da2792a6ca721e57caca5c2f32980dff5840;hb=f7db77bd8785d1715d3e7ed7e69bd1cc991f2d07;hp=02a7046f33d43c354c22125a3dea36e83de80320;hpb=1325ac4c93f27dd56ebce37d8045d7ceb64dc645;p=ffmpeg diff --git a/libavfilter/vf_waveform.c b/libavfilter/vf_waveform.c index 02a7046f33d..8191da2792a 100644 --- a/libavfilter/vf_waveform.c +++ b/libavfilter/vf_waveform.c @@ -29,6 +29,14 @@ #include "internal.h" #include "video.h" +typedef struct ThreadData { + AVFrame *in; + AVFrame *out; + int component; + int offset_y; + int offset_x; +} ThreadData; + enum FilterType { LOWPASS, FLAT, @@ -36,6 +44,8 @@ enum FilterType { CHROMA, COLOR, ACOLOR, + XFLAT, + YFLAT, NB_FILTERS }; @@ -53,6 +63,14 @@ enum ScaleType { NB_SCALES }; +enum GraticuleType { + GRAT_NONE, + GRAT_GREEN, + GRAT_ORANGE, + GRAT_INVERT, + NB_GRATICULES +}; + typedef struct GraticuleLine { const char *name; uint16_t pos; @@ -89,15 +107,22 @@ typedef struct WaveformContext { int max; int size; int scale; + uint8_t grat_yuva_color[4]; int shift_w[4], shift_h[4]; GraticuleLines *glines; int nb_glines; - void (*waveform)(struct WaveformContext *s, - AVFrame *in, AVFrame *out, - int component, int intensity, - int offset_y, int offset_x, - int column, int mirror); + int rgb; + float ftint[2]; + int tint[2]; + + int (*waveform_slice)(AVFilterContext *ctx, void *arg, + int jobnr, int nb_jobs); void (*graticulef)(struct WaveformContext *s, AVFrame *out); + void (*blend_line)(uint8_t *dst, int size, int linesize, float o1, float o2, + int v, int step); + void (*draw_text)(AVFrame *out, int x, int y, int mult, + float o1, float o2, const char *txt, + const uint8_t color[4]); const AVPixFmtDescriptor *desc; const AVPixFmtDescriptor *odesc; } WaveformContext; @@ -135,10 +160,14 @@ static const AVOption waveform_options[] = { { "chroma", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CHROMA}, 0, 0, FLAGS, "filter" }, { "color", NULL, 0, AV_OPT_TYPE_CONST, {.i64=COLOR}, 0, 0, FLAGS, "filter" }, { "acolor", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ACOLOR}, 0, 0, FLAGS, "filter" }, - { "graticule", "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "graticule" }, - { "g", "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "graticule" }, - { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "graticule" }, - { "green", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "graticule" }, + { "xflat", NULL, 0, AV_OPT_TYPE_CONST, {.i64=XFLAT}, 0, 0, FLAGS, "filter" }, + { "yflat", NULL, 0, AV_OPT_TYPE_CONST, {.i64=YFLAT}, 0, 0, FLAGS, "filter" }, + { "graticule", "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_GRATICULES-1, FLAGS, "graticule" }, + { "g", "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_GRATICULES-1, FLAGS, "graticule" }, + { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRAT_NONE}, 0, 0, FLAGS, "graticule" }, + { "green", NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRAT_GREEN}, 0, 0, FLAGS, "graticule" }, + { "orange", NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRAT_ORANGE}, 0, 0, FLAGS, "graticule" }, + { "invert", NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRAT_INVERT}, 0, 0, FLAGS, "graticule" }, { "opacity", "set graticule opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS }, { "o", "set graticule opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS }, { "flags", "set graticule flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, 3, FLAGS, "flags" }, @@ -152,6 +181,10 @@ static const AVOption waveform_options[] = { { "ire", NULL, 0, AV_OPT_TYPE_CONST, {.i64=IRE}, 0, 0, FLAGS, "scale" }, { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS }, { "b", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS }, + { "tint0", "set 1st tint", OFFSET(ftint[0]), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS}, + { "t0", "set 1st tint", OFFSET(ftint[0]), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS}, + { "tint1", "set 2nd tint", OFFSET(ftint[1]), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS}, + { "t1", "set 2nd tint", OFFSET(ftint[1]), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS}, { NULL } }; @@ -172,6 +205,7 @@ static const enum AVPixelFormat in_lowpass_pix_fmts[] = { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_NONE }; @@ -189,6 +223,7 @@ static const enum AVPixelFormat in_color_pix_fmts[] = { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_NONE }; @@ -204,6 +239,7 @@ static const enum AVPixelFormat in_flat_pix_fmts[] = { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_NONE }; @@ -243,7 +279,7 @@ static const enum AVPixelFormat out_yuv10_lowpass_pix_fmts[] = { }; static const enum AVPixelFormat out_yuv12_lowpass_pix_fmts[] = { - AV_PIX_FMT_YUV444P12, + AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_NONE }; @@ -267,30 +303,25 @@ static const enum AVPixelFormat out_gray12_lowpass_pix_fmts[] = { AV_PIX_FMT_NONE }; -static const enum AVPixelFormat flat_pix_fmts[] = { - AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, - AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, - AV_PIX_FMT_YUV444P12, - AV_PIX_FMT_NONE -}; - static int query_formats(AVFilterContext *ctx) { WaveformContext *s = ctx->priv; const enum AVPixelFormat *out_pix_fmts; const enum AVPixelFormat *in_pix_fmts; - const AVPixFmtDescriptor *desc; - AVFilterFormats *avff; - int depth, rgb, i, ret, ncomp; + const AVPixFmtDescriptor *desc, *desc2; + AVFilterFormats *avff, *avff2; + int depth, depth2, rgb, i, ret, ncomp, ncomp2; - if (!ctx->inputs[0]->in_formats || - !ctx->inputs[0]->in_formats->nb_formats) { + if (!ctx->inputs[0]->incfg.formats || + !ctx->inputs[0]->incfg.formats->nb_formats) { return AVERROR(EAGAIN); } switch (s->filter) { case LOWPASS: in_pix_fmts = in_lowpass_pix_fmts; break; case CHROMA: + case XFLAT: + case YFLAT: case AFLAT: case FLAT: in_pix_fmts = in_flat_pix_fmts; break; case ACOLOR: @@ -298,16 +329,22 @@ static int query_formats(AVFilterContext *ctx) default: return AVERROR_BUG; } - if (!ctx->inputs[0]->out_formats) { - if ((ret = ff_formats_ref(ff_make_format_list(in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0) + if (!ctx->inputs[0]->outcfg.formats) { + if ((ret = ff_formats_ref(ff_make_format_list(in_pix_fmts), &ctx->inputs[0]->outcfg.formats)) < 0) return ret; } - avff = ctx->inputs[0]->in_formats; + avff = ctx->inputs[0]->incfg.formats; + avff2 = ctx->inputs[0]->outcfg.formats; desc = av_pix_fmt_desc_get(avff->formats[0]); + desc2 = av_pix_fmt_desc_get(avff2->formats[0]); ncomp = desc->nb_components; + ncomp2 = desc2->nb_components; rgb = desc->flags & AV_PIX_FMT_FLAG_RGB; depth = desc->comp[0].depth; + depth2 = desc2->comp[0].depth; + if (ncomp != ncomp2 || depth != depth2) + return AVERROR(EAGAIN); for (i = 1; i < avff->nb_formats; i++) { desc = av_pix_fmt_desc_get(avff->formats[i]); if (rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB) || @@ -341,7 +378,7 @@ static int query_formats(AVFilterContext *ctx) out_pix_fmts = out_yuv12_lowpass_pix_fmts; else return AVERROR(EAGAIN); - if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0) + if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->incfg.formats)) < 0) return ret; return 0; @@ -619,38 +656,60 @@ static void update(uint8_t *target, int max, int intensity) *target = 255; } +static void update_cr(uint8_t *target, int unused, int intensity) +{ + if (*target - intensity > 0) + *target -= intensity; + else + *target = 0; +} + +static void update16_cr(uint16_t *target, int unused, int intensity, int limit) +{ + if (*target - intensity > 0) + *target -= intensity; + else + *target = 0; +} + static av_always_inline void lowpass16(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; + const int dplane = (s->rgb || s->display == OVERLAY) ? plane : 0; const int shift_w = s->shift_w[component]; const int shift_h = s->shift_h[component]; const int src_linesize = in->linesize[plane] / 2; - const int dst_linesize = out->linesize[plane] / 2; + const int dst_linesize = out->linesize[dplane] / 2; const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1); const int limit = s->max - 1; const int max = limit - intensity; const int src_h = AV_CEIL_RSHIFT(in->height, shift_h); const int src_w = AV_CEIL_RSHIFT(in->width, shift_w); - const uint16_t *src_data = (const uint16_t *)in->data[plane]; - uint16_t *dst_data = (uint16_t *)out->data[plane] + offset_y * dst_linesize + offset_x; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; + const int step = column ? 1 << shift_w : 1 << shift_h; + const uint16_t *src_data = (const uint16_t *)in->data[plane] + sliceh_start * src_linesize; + uint16_t *dst_data = (uint16_t *)out->data[dplane] + (offset_y + sliceh_start * step) * dst_linesize + offset_x; uint16_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1); uint16_t * const dst_line = (mirror ? dst_bottom_line : dst_data); - const int step = column ? 1 << shift_w : 1 << shift_h; const uint16_t *p; int y; if (!column && mirror) dst_data += s->size; - for (y = 0; y < src_h; y++) { - const uint16_t *src_data_end = src_data + src_w; - uint16_t *dst = dst_line; + for (y = sliceh_start; y < sliceh_end; y++) { + const uint16_t *src_data_end = src_data + slicew_end; + uint16_t *dst = dst_line + slicew_start * step; - for (p = src_data; p < src_data_end; p++) { + for (p = src_data + slicew_start; p < src_data_end; p++) { uint16_t *target; int i = 0, v = FFMIN(*p, limit); @@ -675,18 +734,75 @@ static av_always_inline void lowpass16(WaveformContext *s, dst_data += dst_linesize * step; } - envelope16(s, out, plane, plane, column ? offset_x : offset_y); + if (s->display != OVERLAY && column && !s->rgb) { + const int mult = s->max / 256; + const int bg = s->bg_color[0] * mult; + const int t0 = s->tint[0]; + const int t1 = s->tint[1]; + uint16_t *dst0, *dst1; + const uint16_t *src; + int x; + + src = (const uint16_t *)(out->data[0]) + offset_y * dst_linesize + offset_x; + dst0 = (uint16_t *)(out->data[1]) + offset_y * dst_linesize + offset_x; + dst1 = (uint16_t *)(out->data[2]) + offset_y * dst_linesize + offset_x; + for (y = 0; y < s->max; y++) { + for (x = slicew_start * step; x < slicew_end * step; x++) { + if (src[x] != bg) { + dst0[x] = t0; + dst1[x] = t1; + } + } + + src += dst_linesize; + dst0 += dst_linesize; + dst1 += dst_linesize; + } + } else if (s->display != OVERLAY && !s->rgb) { + const int mult = s->max / 256; + const int bg = s->bg_color[0] * mult; + const int t0 = s->tint[0]; + const int t1 = s->tint[1]; + uint16_t *dst0, *dst1; + const uint16_t *src; + int x; + + src = (const uint16_t *)out->data[0] + (offset_y + sliceh_start * step) * dst_linesize + offset_x; + dst0 = (uint16_t *)(out->data[1]) + (offset_y + sliceh_start * step) * dst_linesize + offset_x; + dst1 = (uint16_t *)(out->data[2]) + (offset_y + sliceh_start * step) * dst_linesize + offset_x; + for (y = sliceh_start * step; y < sliceh_end * step; y++) { + for (x = 0; x < s->max; x++) { + if (src[x] != bg) { + dst0[x] = t0; + dst1[x] = t1; + } + } + + src += dst_linesize; + dst0 += dst_linesize; + dst1 += dst_linesize; + } + } } -#define LOWPASS16_FUNC(name, column, mirror) \ -static void lowpass16_##name(WaveformContext *s, \ - AVFrame *in, AVFrame *out, \ - int component, int intensity, \ - int offset_y, int offset_x, \ - int unused1, int unused2) \ -{ \ - lowpass16(s, in, out, component, intensity, \ - offset_y, offset_x, column, mirror); \ +#define LOWPASS16_FUNC(name, column, mirror) \ +static int lowpass16_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + lowpass16(s, in, out, component, s->intensity, \ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } LOWPASS16_FUNC(column_mirror, 1, 1) @@ -698,93 +814,132 @@ static av_always_inline void lowpass(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; + const int dplane = (s->rgb || s->display == OVERLAY) ? plane : 0; const int shift_w = s->shift_w[component]; const int shift_h = s->shift_h[component]; const int src_linesize = in->linesize[plane]; - const int dst_linesize = out->linesize[plane]; + const int dst_linesize = out->linesize[dplane]; const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1); const int max = 255 - intensity; const int src_h = AV_CEIL_RSHIFT(in->height, shift_h); const int src_w = AV_CEIL_RSHIFT(in->width, shift_w); - const uint8_t *src_data = in->data[plane]; - uint8_t *dst_data = out->data[plane] + offset_y * dst_linesize + offset_x; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; + const int step = column ? 1 << shift_w : 1 << shift_h; + const uint8_t *src_data = in->data[plane] + sliceh_start * src_linesize; + uint8_t *dst_data = out->data[dplane] + (offset_y + sliceh_start * step) * dst_linesize + offset_x; uint8_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1); uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data); - const int step = column ? 1 << shift_w : 1 << shift_h; const uint8_t *p; int y; if (!column && mirror) dst_data += s->size; - for (y = 0; y < src_h; y++) { - const uint8_t *src_data_end = src_data + src_w; - uint8_t *dst = dst_line; + for (y = sliceh_start; y < sliceh_end; y++) { + const uint8_t *src_data_end = src_data + slicew_end; + uint8_t *dst = dst_line + slicew_start * step; - for (p = src_data; p < src_data_end; p++) { + for (p = src_data + slicew_start; p < src_data_end; p++) { uint8_t *target; + int i = 0; + if (column) { - target = dst + dst_signed_linesize * *p; - dst += step; - update(target, max, intensity); + do { + target = dst++ + dst_signed_linesize * *p; + update(target, max, intensity); + } while (++i < step); } else { uint8_t *row = dst_data; - if (mirror) - target = row - *p - 1; - else - target = row + *p; - update(target, max, intensity); - row += dst_linesize; + do { + if (mirror) + target = row - *p - 1; + else + target = row + *p; + update(target, max, intensity); + row += dst_linesize; + } while (++i < step); } } src_data += src_linesize; dst_data += dst_linesize * step; } - if (column && step > 1) { - const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width; + if (s->display != OVERLAY && column && !s->rgb) { + const int bg = s->bg_color[0]; const int dst_h = 256; - uint8_t *dst; - int x, z; - - dst = out->data[plane] + offset_y * dst_linesize + offset_x; + const int t0 = s->tint[0]; + const int t1 = s->tint[1]; + uint8_t *dst0, *dst1; + const uint8_t *src; + int x; + + src = out->data[0] + offset_y * dst_linesize + offset_x; + dst0 = out->data[1] + offset_y * dst_linesize + offset_x; + dst1 = out->data[2] + offset_y * dst_linesize + offset_x; for (y = 0; y < dst_h; y++) { - for (x = 0; x < dst_w; x+=step) { - for (z = 1; z < step; z++) { - dst[x + z] = dst[x]; + for (x = slicew_start * step; x < slicew_end * step; x++) { + if (src[x] != bg) { + dst0[x] = t0; + dst1[x] = t1; } } - dst += dst_linesize; + + src += dst_linesize; + dst0 += dst_linesize; + dst1 += dst_linesize; } - } else if (step > 1) { - const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height; + } else if (s->display != OVERLAY && !s->rgb) { + const int bg = s->bg_color[0]; const int dst_w = 256; - uint8_t *dst; - int z; + const int t0 = s->tint[0]; + const int t1 = s->tint[1]; + uint8_t *dst0, *dst1; + const uint8_t *src; + int x; + + src = out->data[0] + (offset_y + sliceh_start * step) * dst_linesize + offset_x; + dst0 = out->data[1] + (offset_y + sliceh_start * step) * dst_linesize + offset_x; + dst1 = out->data[2] + (offset_y + sliceh_start * step) * dst_linesize + offset_x; + for (y = sliceh_start * step; y < sliceh_end * step; y++) { + for (x = 0; x < dst_w; x++) { + if (src[x] != bg) { + dst0[x] = t0; + dst1[x] = t1; + } + } - dst = out->data[plane] + offset_y * dst_linesize + offset_x; - for (y = 0; y < dst_h; y+=step) { - for (z = 1; z < step; z++) - memcpy(dst + dst_linesize * z, dst, dst_w); - dst += dst_linesize * step; + src += dst_linesize; + dst0 += dst_linesize; + dst1 += dst_linesize; } } - - envelope(s, out, plane, plane, column ? offset_x : offset_y); } -#define LOWPASS_FUNC(name, column, mirror) \ -static void lowpass_##name(WaveformContext *s, \ - AVFrame *in, AVFrame *out, \ - int component, int intensity, \ - int offset_y, int offset_x, \ - int unused1, int unused2) \ -{ \ - lowpass(s, in, out, component, intensity, \ - offset_y, offset_x, column, mirror); \ +#define LOWPASS_FUNC(name, column, mirror) \ +static int lowpass_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + lowpass(s, in, out, component, s->intensity, \ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } LOWPASS_FUNC(column_mirror, 1, 1) @@ -796,7 +951,8 @@ static av_always_inline void flat16(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; const int c0_linesize = in->linesize[ plane + 0 ] / 2; @@ -815,13 +971,17 @@ static av_always_inline void flat16(WaveformContext *s, const int mid = s->max / 2; const int src_h = in->height; const int src_w = in->width; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; int x, y; if (column) { const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); - for (x = 0; x < src_w; x++) { + for (x = slicew_start; x < slicew_end; x++) { const uint16_t *c0_data = (uint16_t *)in->data[plane + 0]; const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp]; const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp]; @@ -855,18 +1015,18 @@ static av_always_inline void flat16(WaveformContext *s, } } } else { - const uint16_t *c0_data = (uint16_t *)in->data[plane]; - const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp]; - const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp]; - uint16_t *d0_data = (uint16_t *)(out->data[plane]) + offset_y * d0_linesize + offset_x; - uint16_t *d1_data = (uint16_t *)(out->data[(plane + 1) % s->ncomp]) + offset_y * d1_linesize + offset_x; + const uint16_t *c0_data = (uint16_t *)(in->data[plane]) + (sliceh_start >> c0_shift_h) * c0_linesize; + const uint16_t *c1_data = (uint16_t *)(in->data[(plane + 1) % s->ncomp]) + (sliceh_start >> c1_shift_h) * c1_linesize; + const uint16_t *c2_data = (uint16_t *)(in->data[(plane + 2) % s->ncomp]) + (sliceh_start >> c2_shift_h) * c2_linesize; + uint16_t *d0_data = (uint16_t *)(out->data[plane]) + (offset_y + sliceh_start) * d0_linesize + offset_x; + uint16_t *d1_data = (uint16_t *)(out->data[(plane + 1) % s->ncomp]) + (offset_y + sliceh_start) * d1_linesize + offset_x; if (mirror) { d0_data += s->size - 1; d1_data += s->size - 1; } - for (y = 0; y < src_h; y++) { + for (y = sliceh_start; y < sliceh_end; y++) { for (x = 0; x < src_w; x++) { const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + s->max; const int c1 = FFMIN(FFABS(c1_data[x >> c1_shift_w] - mid) + FFABS(c2_data[x >> c2_shift_w] - mid), limit); @@ -899,16 +1059,39 @@ static av_always_inline void flat16(WaveformContext *s, d1_data += d1_linesize; } } +} - envelope16(s, out, plane, plane, column ? offset_x : offset_y); - envelope16(s, out, plane, (plane + 1) % s->ncomp, column ? offset_x : offset_y); +#define FLAT16_FUNC(name, column, mirror) \ +static int flat16_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + flat16(s, in, out, component, s->intensity, \ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } +FLAT16_FUNC(column_mirror, 1, 1) +FLAT16_FUNC(column, 1, 0) +FLAT16_FUNC(row_mirror, 0, 1) +FLAT16_FUNC(row, 0, 0) + static av_always_inline void flat(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; const int c0_linesize = in->linesize[ plane + 0 ]; @@ -925,13 +1108,17 @@ static av_always_inline void flat(WaveformContext *s, const int max = 255 - intensity; const int src_h = in->height; const int src_w = in->width; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; int x, y; if (column) { const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); - for (x = 0; x < src_w; x++) { + for (x = slicew_start; x < slicew_end; x++) { const uint8_t *c0_data = in->data[plane + 0]; const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp]; const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp]; @@ -965,20 +1152,20 @@ static av_always_inline void flat(WaveformContext *s, } } } else { - const uint8_t *c0_data = in->data[plane]; - const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp]; - const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp]; - uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x; - uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; + const uint8_t *c0_data = in->data[plane] + (sliceh_start >> c0_shift_h) * c0_linesize; + const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; + const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; + uint8_t *d0_data = out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x; + uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x; if (mirror) { d0_data += s->size - 1; d1_data += s->size - 1; } - for (y = 0; y < src_h; y++) { + for (y = sliceh_start; y < sliceh_end; y++) { for (x = 0; x < src_w; x++) { - int c0 = c0_data[x >> c0_shift_w] + 256; + const int c0 = c0_data[x >> c0_shift_w] + 256; const int c1 = FFABS(c1_data[x >> c1_shift_w] - 128) + FFABS(c2_data[x >> c2_shift_w] - 128); uint8_t *target; @@ -1009,266 +1196,331 @@ static av_always_inline void flat(WaveformContext *s, d1_data += d1_linesize; } } - - envelope(s, out, plane, plane, column ? offset_x : offset_y); - envelope(s, out, plane, (plane + 1) % s->ncomp, column ? offset_x : offset_y); } -static av_always_inline void aflat16(WaveformContext *s, - AVFrame *in, AVFrame *out, - int component, int intensity, - int offset_y, int offset_x, - int column, int mirror) -{ - const int plane = s->desc->comp[component].plane; - const int c0_linesize = in->linesize[ plane + 0 ] / 2; - const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2; - const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2; - const int c0_shift_w = s->shift_w[ component + 0 ]; - const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp]; - const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp]; - const int c0_shift_h = s->shift_h[ component + 0 ]; - const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; - const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; - const int d0_linesize = out->linesize[ plane + 0 ] / 2; - const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2; - const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2; - const int limit = s->max - 1; - const int max = limit - intensity; - const int mid = s->max / 2; - const int src_h = in->height; - const int src_w = in->width; - int x, y; - - if (column) { - const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); - const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); - const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1); - - for (x = 0; x < src_w; x++) { - const uint16_t *c0_data = (uint16_t *)in->data[plane + 0]; - const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp]; - const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp]; - uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x; - uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; - uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; - uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1); - uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data); - uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1); - uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data); - uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1); - uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data); - - for (y = 0; y < src_h; y++) { - const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + mid; - const int c1 = FFMIN(c1_data[x >> c1_shift_w], limit) - mid; - const int c2 = FFMIN(c2_data[x >> c2_shift_w], limit) - mid; - uint16_t *target; - - target = d0 + x + d0_signed_linesize * c0; - update16(target, max, intensity, limit); - - target = d1 + x + d1_signed_linesize * (c0 + c1); - update16(target, max, intensity, limit); - - target = d2 + x + d2_signed_linesize * (c0 + c2); - update16(target, max, intensity, limit); - - if (!c0_shift_h || (y & c0_shift_h)) - c0_data += c0_linesize; - if (!c1_shift_h || (y & c1_shift_h)) - c1_data += c1_linesize; - if (!c2_shift_h || (y & c2_shift_h)) - c2_data += c2_linesize; - d0_data += d0_linesize; - d1_data += d1_linesize; - d2_data += d2_linesize; - } - } - } else { - const uint16_t *c0_data = (uint16_t *)in->data[plane]; - const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp]; - const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp]; - uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x; - uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; - uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; - - if (mirror) { - d0_data += s->size - 1; - d1_data += s->size - 1; - d2_data += s->size - 1; - } - - for (y = 0; y < src_h; y++) { - for (x = 0; x < src_w; x++) { - const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + mid; - const int c1 = FFMIN(c1_data[x >> c1_shift_w], limit) - mid; - const int c2 = FFMIN(c2_data[x >> c2_shift_w], limit) - mid; - uint16_t *target; - - if (mirror) { - target = d0_data - c0; - update16(target, max, intensity, limit); - target = d1_data - (c0 + c1); - update16(target, max, intensity, limit); - target = d2_data - (c0 + c2); - update16(target, max, intensity, limit); - } else { - target = d0_data + c0; - update16(target, max, intensity, limit); - target = d1_data + (c0 + c1); - update16(target, max, intensity, limit); - target = d2_data + (c0 + c2); - update16(target, max, intensity, limit); - } - } - - if (!c0_shift_h || (y & c0_shift_h)) - c0_data += c0_linesize; - if (!c1_shift_h || (y & c1_shift_h)) - c1_data += c1_linesize; - if (!c2_shift_h || (y & c2_shift_h)) - c2_data += c2_linesize; - d0_data += d0_linesize; - d1_data += d1_linesize; - d2_data += d2_linesize; - } - } - - envelope16(s, out, plane, (plane + 0) % s->ncomp, column ? offset_x : offset_y); - envelope16(s, out, plane, (plane + 1) % s->ncomp, column ? offset_x : offset_y); - envelope16(s, out, plane, (plane + 2) % s->ncomp, column ? offset_x : offset_y); +#define FLAT_FUNC(name, column, mirror) \ +static int flat_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + flat(s, in, out, component, s->intensity, \ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } -static av_always_inline void aflat(WaveformContext *s, - AVFrame *in, AVFrame *out, - int component, int intensity, - int offset_y, int offset_x, - int column, int mirror) -{ - const int plane = s->desc->comp[component].plane; - const int c0_linesize = in->linesize[ plane + 0 ]; - const int c1_linesize = in->linesize[(plane + 1) % s->ncomp]; - const int c2_linesize = in->linesize[(plane + 2) % s->ncomp]; - const int c0_shift_w = s->shift_w[ component + 0 ]; - const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp]; - const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp]; - const int c0_shift_h = s->shift_h[ component + 0 ]; - const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; - const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; - const int d0_linesize = out->linesize[ plane + 0 ]; - const int d1_linesize = out->linesize[(plane + 1) % s->ncomp]; - const int d2_linesize = out->linesize[(plane + 2) % s->ncomp]; - const int max = 255 - intensity; - const int src_h = in->height; - const int src_w = in->width; - int x, y; - - if (column) { - const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); - const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); - const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1); - - for (x = 0; x < src_w; x++) { - const uint8_t *c0_data = in->data[plane + 0]; - const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp]; - const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp]; - uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x; - uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; - uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; - uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1); - uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data); - uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1); - uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data); - uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1); - uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data); - - for (y = 0; y < src_h; y++) { - const int c0 = c0_data[x >> c0_shift_w] + 128; - const int c1 = c1_data[x >> c1_shift_w] - 128; - const int c2 = c2_data[x >> c2_shift_w] - 128; - uint8_t *target; - - target = d0 + x + d0_signed_linesize * c0; - update(target, max, intensity); - - target = d1 + x + d1_signed_linesize * (c0 + c1); - update(target, max, intensity); - - target = d2 + x + d2_signed_linesize * (c0 + c2); - update(target, max, intensity); - - if (!c0_shift_h || (y & c0_shift_h)) - c0_data += c0_linesize; - if (!c1_shift_h || (y & c1_shift_h)) - c1_data += c1_linesize; - if (!c1_shift_h || (y & c1_shift_h)) - c2_data += c1_linesize; - d0_data += d0_linesize; - d1_data += d1_linesize; - d2_data += d2_linesize; - } - } - } else { - const uint8_t *c0_data = in->data[plane]; - const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp]; - const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp]; - uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x; - uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; - uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; - - if (mirror) { - d0_data += s->size - 1; - d1_data += s->size - 1; - d2_data += s->size - 1; - } - - for (y = 0; y < src_h; y++) { - for (x = 0; x < src_w; x++) { - const int c0 = c0_data[x >> c0_shift_w] + 128; - const int c1 = c1_data[x >> c1_shift_w] - 128; - const int c2 = c2_data[x >> c2_shift_w] - 128; - uint8_t *target; - - if (mirror) { - target = d0_data - c0; - update(target, max, intensity); - target = d1_data - (c0 + c1); - update(target, max, intensity); - target = d2_data - (c0 + c2); - update(target, max, intensity); - } else { - target = d0_data + c0; - update(target, max, intensity); - target = d1_data + (c0 + c1); - update(target, max, intensity); - target = d2_data + (c0 + c2); - update(target, max, intensity); - } - } - - if (!c0_shift_h || (y & c0_shift_h)) - c0_data += c0_linesize; - if (!c1_shift_h || (y & c1_shift_h)) - c1_data += c1_linesize; - if (!c2_shift_h || (y & c2_shift_h)) - c2_data += c2_linesize; - d0_data += d0_linesize; - d1_data += d1_linesize; - d2_data += d2_linesize; - } - } +FLAT_FUNC(column_mirror, 1, 1) +FLAT_FUNC(column, 1, 0) +FLAT_FUNC(row_mirror, 0, 1) +FLAT_FUNC(row, 0, 0) + +#define AFLAT16(name, update_cb, update_cr, column, mirror) \ +static int name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + const int intensity = s->intensity; \ + const int plane = s->desc->comp[component].plane; \ + const int c0_linesize = in->linesize[ plane + 0 ] / 2; \ + const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2; \ + const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2; \ + const int c0_shift_w = s->shift_w[ component + 0 ]; \ + const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp]; \ + const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp]; \ + const int c0_shift_h = s->shift_h[ component + 0 ]; \ + const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; \ + const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; \ + const int d0_linesize = out->linesize[ plane + 0 ] / 2; \ + const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2; \ + const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2; \ + const int limit = s->max - 1; \ + const int max = limit - intensity; \ + const int mid = s->max / 2; \ + const int src_h = in->height; \ + const int src_w = in->width; \ + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; \ + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; \ + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; \ + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; \ + int x, y; \ + \ + if (column) { \ + const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); \ + const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); \ + const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1); \ + \ + for (x = slicew_start; x < slicew_end; x++) { \ + const uint16_t *c0_data = (uint16_t *)in->data[plane + 0]; \ + const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp]; \ + const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp]; \ + uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x; \ + uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; \ + uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; \ + uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1); \ + uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data); \ + uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1); \ + uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data); \ + uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1); \ + uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data); \ + \ + for (y = 0; y < src_h; y++) { \ + const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + mid; \ + const int c1 = FFMIN(c1_data[x >> c1_shift_w], limit) - mid; \ + const int c2 = FFMIN(c2_data[x >> c2_shift_w], limit) - mid; \ + uint16_t *target; \ + \ + target = d0 + x + d0_signed_linesize * c0; \ + update16(target, max, intensity, limit); \ + \ + target = d1 + x + d1_signed_linesize * (c0 + c1); \ + update_cb(target, max, intensity, limit); \ + \ + target = d2 + x + d2_signed_linesize * (c0 + c2); \ + update_cr(target, max, intensity, limit); \ + \ + if (!c0_shift_h || (y & c0_shift_h)) \ + c0_data += c0_linesize; \ + if (!c1_shift_h || (y & c1_shift_h)) \ + c1_data += c1_linesize; \ + if (!c2_shift_h || (y & c2_shift_h)) \ + c2_data += c2_linesize; \ + d0_data += d0_linesize; \ + d1_data += d1_linesize; \ + d2_data += d2_linesize; \ + } \ + } \ + } else { \ + const uint16_t *c0_data = (uint16_t *)in->data[plane] + (sliceh_start >> c0_shift_h) * c0_linesize; \ + const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; \ + const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; \ + uint16_t *d0_data = (uint16_t *)out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x; \ + uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x; \ + uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x; \ + \ + if (mirror) { \ + d0_data += s->size - 1; \ + d1_data += s->size - 1; \ + d2_data += s->size - 1; \ + } \ + \ + for (y = sliceh_start; y < sliceh_end; y++) { \ + for (x = 0; x < src_w; x++) { \ + const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + mid; \ + const int c1 = FFMIN(c1_data[x >> c1_shift_w], limit) - mid; \ + const int c2 = FFMIN(c2_data[x >> c2_shift_w], limit) - mid; \ + uint16_t *target; \ + \ + if (mirror) { \ + target = d0_data - c0; \ + update16(target, max, intensity, limit); \ + target = d1_data - (c0 + c1); \ + update_cb(target, max, intensity, limit); \ + target = d2_data - (c0 + c2); \ + update_cr(target, max, intensity, limit); \ + } else { \ + target = d0_data + c0; \ + update16(target, max, intensity, limit); \ + target = d1_data + (c0 + c1); \ + update_cb(target, max, intensity, limit); \ + target = d2_data + (c0 + c2); \ + update_cr(target, max, intensity, limit); \ + } \ + } \ + \ + if (!c0_shift_h || (y & c0_shift_h)) \ + c0_data += c0_linesize; \ + if (!c1_shift_h || (y & c1_shift_h)) \ + c1_data += c1_linesize; \ + if (!c2_shift_h || (y & c2_shift_h)) \ + c2_data += c2_linesize; \ + d0_data += d0_linesize; \ + d1_data += d1_linesize; \ + d2_data += d2_linesize; \ + } \ + } \ + return 0; \ +} - envelope(s, out, plane, (plane + 0) % s->ncomp, column ? offset_x : offset_y); - envelope(s, out, plane, (plane + 1) % s->ncomp, column ? offset_x : offset_y); - envelope(s, out, plane, (plane + 2) % s->ncomp, column ? offset_x : offset_y); +#define AFLAT(name, update_cb, update_cr, column, mirror) \ +static int name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + const int src_h = in->height; \ + const int src_w = in->width; \ + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; \ + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; \ + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; \ + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; \ + const int intensity = s->intensity; \ + const int plane = s->desc->comp[component].plane; \ + const int c0_linesize = in->linesize[ plane + 0 ]; \ + const int c1_linesize = in->linesize[(plane + 1) % s->ncomp]; \ + const int c2_linesize = in->linesize[(plane + 2) % s->ncomp]; \ + const int c0_shift_w = s->shift_w[ component + 0 ]; \ + const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp]; \ + const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp]; \ + const int c0_shift_h = s->shift_h[ component + 0 ]; \ + const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; \ + const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; \ + const int d0_linesize = out->linesize[ plane + 0 ]; \ + const int d1_linesize = out->linesize[(plane + 1) % s->ncomp]; \ + const int d2_linesize = out->linesize[(plane + 2) % s->ncomp]; \ + const int max = 255 - intensity; \ + int x, y; \ + \ + if (column) { \ + const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); \ + const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); \ + const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1); \ + \ + for (x = slicew_start; x < slicew_end; x++) { \ + const uint8_t *c0_data = in->data[plane + 0]; \ + const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp]; \ + const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp]; \ + uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x; \ + uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; \ + uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; \ + uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1); \ + uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data); \ + uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1); \ + uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data); \ + uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1); \ + uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data); \ + \ + for (y = 0; y < src_h; y++) { \ + const int c0 = c0_data[x >> c0_shift_w] + 128; \ + const int c1 = c1_data[x >> c1_shift_w] - 128; \ + const int c2 = c2_data[x >> c2_shift_w] - 128; \ + uint8_t *target; \ + \ + target = d0 + x + d0_signed_linesize * c0; \ + update(target, max, intensity); \ + \ + target = d1 + x + d1_signed_linesize * (c0 + c1); \ + update_cb(target, max, intensity); \ + \ + target = d2 + x + d2_signed_linesize * (c0 + c2); \ + update_cr(target, max, intensity); \ + \ + if (!c0_shift_h || (y & c0_shift_h)) \ + c0_data += c0_linesize; \ + if (!c1_shift_h || (y & c1_shift_h)) \ + c1_data += c1_linesize; \ + if (!c2_shift_h || (y & c2_shift_h)) \ + c2_data += c2_linesize; \ + d0_data += d0_linesize; \ + d1_data += d1_linesize; \ + d2_data += d2_linesize; \ + } \ + } \ + } else { \ + const uint8_t *c0_data = in->data[plane] + (sliceh_start >> c0_shift_h) * c0_linesize; \ + const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; \ + const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; \ + uint8_t *d0_data = out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x; \ + uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x; \ + uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x; \ + \ + if (mirror) { \ + d0_data += s->size - 1; \ + d1_data += s->size - 1; \ + d2_data += s->size - 1; \ + } \ + \ + for (y = sliceh_start; y < sliceh_end; y++) { \ + for (x = 0; x < src_w; x++) { \ + const int c0 = c0_data[x >> c0_shift_w] + 128; \ + const int c1 = c1_data[x >> c1_shift_w] - 128; \ + const int c2 = c2_data[x >> c2_shift_w] - 128; \ + uint8_t *target; \ + \ + if (mirror) { \ + target = d0_data - c0; \ + update(target, max, intensity); \ + target = d1_data - (c0 + c1); \ + update_cb(target, max, intensity); \ + target = d2_data - (c0 + c2); \ + update_cr(target, max, intensity); \ + } else { \ + target = d0_data + c0; \ + update(target, max, intensity); \ + target = d1_data + (c0 + c1); \ + update_cb(target, max, intensity); \ + target = d2_data + (c0 + c2); \ + update_cr(target, max, intensity); \ + } \ + } \ + \ + if (!c0_shift_h || (y & c0_shift_h)) \ + c0_data += c0_linesize; \ + if (!c1_shift_h || (y & c1_shift_h)) \ + c1_data += c1_linesize; \ + if (!c2_shift_h || (y & c2_shift_h)) \ + c2_data += c2_linesize; \ + d0_data += d0_linesize; \ + d1_data += d1_linesize; \ + d2_data += d2_linesize; \ + } \ + } \ + return 0; \ } +AFLAT16(aflat16_row, update16, update16, 0, 0) +AFLAT16(aflat16_row_mirror, update16, update16, 0, 1) +AFLAT16(aflat16_column, update16, update16, 1, 0) +AFLAT16(aflat16_column_mirror, update16, update16, 1, 1) +AFLAT16(xflat16_row, update16, update16_cr, 0, 0) +AFLAT16(xflat16_row_mirror, update16, update16_cr, 0, 1) +AFLAT16(xflat16_column, update16, update16_cr, 1, 0) +AFLAT16(xflat16_column_mirror, update16, update16_cr, 1, 1) +AFLAT16(yflat16_row, update16_cr, update16_cr, 0, 0) +AFLAT16(yflat16_row_mirror, update16_cr, update16_cr, 0, 1) +AFLAT16(yflat16_column, update16_cr, update16_cr, 1, 0) +AFLAT16(yflat16_column_mirror, update16_cr, update16_cr, 1, 1) + +AFLAT(aflat_row, update, update, 0, 0) +AFLAT(aflat_row_mirror, update, update, 0, 1) +AFLAT(aflat_column, update, update, 1, 0) +AFLAT(aflat_column_mirror, update, update, 1, 1) +AFLAT(xflat_row, update, update_cr, 0, 0) +AFLAT(xflat_row_mirror, update, update_cr, 0, 1) +AFLAT(xflat_column, update, update_cr, 1, 0) +AFLAT(xflat_column_mirror, update, update_cr, 1, 1) +AFLAT(yflat_row, update_cr, update_cr, 0, 0) +AFLAT(yflat_row_mirror, update_cr, update_cr, 0, 1) +AFLAT(yflat_column, update_cr, update_cr, 1, 0) +AFLAT(yflat_column_mirror, update_cr, update_cr, 1, 1) + static av_always_inline void chroma16(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; const int c0_linesize = in->linesize[(plane + 1) % s->ncomp] / 2; @@ -1283,12 +1535,16 @@ static av_always_inline void chroma16(WaveformContext *s, const int c1_shift_h = s->shift_h[(component + 2) % s->ncomp]; const int src_h = in->height; const int src_w = in->width; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; int x, y; if (column) { const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1); - for (x = 0; x < src_w; x++) { + for (x = slicew_start; x < slicew_end; x++) { const uint16_t *c0_data = (uint16_t *)in->data[(plane + 1) % s->ncomp]; const uint16_t *c1_data = (uint16_t *)in->data[(plane + 2) % s->ncomp]; uint16_t *dst_data = (uint16_t *)out->data[plane] + offset_y * dst_linesize + offset_x; @@ -1311,13 +1567,13 @@ static av_always_inline void chroma16(WaveformContext *s, } } } else { - const uint16_t *c0_data = (uint16_t *)in->data[(plane + 1) % s->ncomp]; - const uint16_t *c1_data = (uint16_t *)in->data[(plane + 2) % s->ncomp]; - uint16_t *dst_data = (uint16_t *)out->data[plane] + offset_y * dst_linesize + offset_x; + const uint16_t *c0_data = (uint16_t *)in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c0_shift_h) * c0_linesize; + const uint16_t *c1_data = (uint16_t *)in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; + uint16_t *dst_data = (uint16_t *)out->data[plane] + (offset_y + sliceh_start) * dst_linesize + offset_x; if (mirror) dst_data += s->size - 1; - for (y = 0; y < src_h; y++) { + for (y = sliceh_start; y < sliceh_end; y++) { for (x = 0; x < src_w; x++) { const int sum = FFMIN(FFABS(c0_data[x >> c0_shift_w] - mid) + FFABS(c1_data[x >> c1_shift_w] - mid - 1), limit); uint16_t *target; @@ -1338,17 +1594,47 @@ static av_always_inline void chroma16(WaveformContext *s, dst_data += dst_linesize; } } +} - envelope16(s, out, plane, plane, column ? offset_x : offset_y); +#define CHROMA16_FUNC(name, column, mirror) \ +static int chroma16_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + chroma16(s, in, out, component, s->intensity,\ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } +CHROMA16_FUNC(column_mirror, 1, 1) +CHROMA16_FUNC(column, 1, 0) +CHROMA16_FUNC(row_mirror, 0, 1) +CHROMA16_FUNC(row, 0, 0) + static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; + const int src_h = in->height; + const int src_w = in->width; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; const int c0_linesize = in->linesize[(plane + 1) % s->ncomp]; const int c1_linesize = in->linesize[(plane + 2) % s->ncomp]; const int dst_linesize = out->linesize[plane]; @@ -1357,14 +1643,12 @@ static av_always_inline void chroma(WaveformContext *s, const int c1_shift_w = s->shift_w[(component + 2) % s->ncomp]; const int c0_shift_h = s->shift_h[(component + 1) % s->ncomp]; const int c1_shift_h = s->shift_h[(component + 2) % s->ncomp]; - const int src_h = in->height; - const int src_w = in->width; int x, y; if (column) { const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1); - for (x = 0; x < src_w; x++) { + for (x = slicew_start; x < slicew_end; x++) { const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp]; const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp]; uint8_t *dst_data = out->data[plane] + offset_y * dst_linesize + offset_x; @@ -1387,13 +1671,13 @@ static av_always_inline void chroma(WaveformContext *s, } } } else { - const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp]; - const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp]; - uint8_t *dst_data = out->data[plane] + offset_y * dst_linesize + offset_x; + const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c0_shift_h) * c0_linesize; + const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; + uint8_t *dst_data = out->data[plane] + (offset_y + sliceh_start) * dst_linesize + offset_x; if (mirror) dst_data += s->size - 1; - for (y = 0; y < src_h; y++) { + for (y = sliceh_start; y < sliceh_end; y++) { for (x = 0; x < src_w; x++) { const int sum = FFABS(c0_data[x >> c0_shift_w] - 128) + FFABS(c1_data[x >> c1_shift_w] - 127); uint8_t *target; @@ -1414,35 +1698,63 @@ static av_always_inline void chroma(WaveformContext *s, dst_data += dst_linesize; } } +} - envelope(s, out, plane, plane, column ? offset_x : offset_y); +#define CHROMA_FUNC(name, column, mirror) \ +static int chroma_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + chroma(s, in, out, component, s->intensity, \ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } +CHROMA_FUNC(column_mirror, 1, 1) +CHROMA_FUNC(column, 1, 0) +CHROMA_FUNC(row_mirror, 0, 1) +CHROMA_FUNC(row, 0, 0) + static av_always_inline void color16(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; const int limit = s->max - 1; - const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0]; - const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp]; - const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp]; + const int src_h = in->height; + const int src_w = in->width; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; const int c0_linesize = in->linesize[ plane + 0 ] / 2; const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2; const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2; + const int c0_shift_h = s->shift_h[ component + 0 ]; + const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; + const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; + const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0] + (sliceh_start >> c0_shift_h) * c0_linesize; + const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; + const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; const int d0_linesize = out->linesize[ plane + 0 ] / 2; const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2; const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2; const int c0_shift_w = s->shift_w[ component + 0 ]; const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp]; const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp]; - const int c0_shift_h = s->shift_h[ component + 0 ]; - const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; - const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; - const int src_h = in->height; - const int src_w = in->width; int x, y; if (column) { @@ -1460,7 +1772,7 @@ static av_always_inline void color16(WaveformContext *s, uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data); for (y = 0; y < src_h; y++) { - for (x = 0; x < src_w; x++) { + for (x = slicew_start; x < slicew_end; x++) { const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit); const int c1 = c1_data[x >> c1_shift_w]; const int c2 = c2_data[x >> c2_shift_w]; @@ -1481,9 +1793,9 @@ static av_always_inline void color16(WaveformContext *s, d2_data += d2_linesize; } } else { - uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x; - uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; - uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; + uint16_t *d0_data = (uint16_t *)out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x; + uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x; + uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x; if (mirror) { d0_data += s->size - 1; @@ -1491,7 +1803,7 @@ static av_always_inline void color16(WaveformContext *s, d2_data += s->size - 1; } - for (y = 0; y < src_h; y++) { + for (y = sliceh_start; y < sliceh_end; y++) { for (x = 0; x < src_w; x++) { const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit); const int c1 = c1_data[x >> c1_shift_w]; @@ -1519,37 +1831,65 @@ static av_always_inline void color16(WaveformContext *s, d2_data += d2_linesize; } } +} - envelope16(s, out, plane, plane, column ? offset_x : offset_y); +#define COLOR16_FUNC(name, column, mirror) \ +static int color16_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + color16(s, in, out, component, s->intensity, \ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } +COLOR16_FUNC(column_mirror, 1, 1) +COLOR16_FUNC(column, 1, 0) +COLOR16_FUNC(row_mirror, 0, 1) +COLOR16_FUNC(row, 0, 0) + static av_always_inline void color(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; - const uint8_t *c0_data = in->data[plane + 0]; - const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp]; - const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp]; + const int src_h = in->height; + const int src_w = in->width; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; const int c0_linesize = in->linesize[ plane + 0 ]; const int c1_linesize = in->linesize[(plane + 1) % s->ncomp]; const int c2_linesize = in->linesize[(plane + 2) % s->ncomp]; + const int c0_shift_h = s->shift_h[ component + 0 ]; + const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; + const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; + const uint8_t *c0_data = in->data[plane] + (sliceh_start >> c0_shift_h) * c0_linesize; + const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; + const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; const int d0_linesize = out->linesize[ plane + 0 ]; const int d1_linesize = out->linesize[(plane + 1) % s->ncomp]; const int d2_linesize = out->linesize[(plane + 2) % s->ncomp]; const int c0_shift_w = s->shift_w[ component + 0 ]; const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp]; const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp]; - const int c0_shift_h = s->shift_h[ component + 0 ]; - const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; - const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; - const int src_h = in->height; - const int src_w = in->width; int x, y; - if (s->mode) { + if (column) { const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1); @@ -1564,7 +1904,7 @@ static av_always_inline void color(WaveformContext *s, uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data); for (y = 0; y < src_h; y++) { - for (x = 0; x < src_w; x++) { + for (x = slicew_start; x < slicew_end; x++) { const int c0 = c0_data[x >> c0_shift_w]; const int c1 = c1_data[x >> c1_shift_w]; const int c2 = c2_data[x >> c2_shift_w]; @@ -1585,9 +1925,9 @@ static av_always_inline void color(WaveformContext *s, d2_data += d2_linesize; } } else { - uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x; - uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; - uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; + uint8_t *d0_data = out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x; + uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x; + uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x; if (mirror) { d0_data += s->size - 1; @@ -1595,7 +1935,7 @@ static av_always_inline void color(WaveformContext *s, d2_data += s->size - 1; } - for (y = 0; y < src_h; y++) { + for (y = sliceh_start; y < sliceh_end; y++) { for (x = 0; x < src_w; x++) { const int c0 = c0_data[x >> c0_shift_w]; const int c1 = c1_data[x >> c1_shift_w]; @@ -1623,39 +1963,67 @@ static av_always_inline void color(WaveformContext *s, d2_data += d2_linesize; } } +} - envelope(s, out, plane, plane, column ? offset_x : offset_y); +#define COLOR_FUNC(name, column, mirror) \ +static int color_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + color(s, in, out, component, s->intensity, \ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } +COLOR_FUNC(column_mirror, 1, 1) +COLOR_FUNC(column, 1, 0) +COLOR_FUNC(row_mirror, 0, 1) +COLOR_FUNC(row, 0, 0) + static av_always_inline void acolor16(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; const int limit = s->max - 1; const int max = limit - intensity; - const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0]; - const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp]; - const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp]; + const int src_h = in->height; + const int src_w = in->width; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; + const int c0_shift_h = s->shift_h[ component + 0 ]; + const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; + const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; const int c0_linesize = in->linesize[ plane + 0 ] / 2; const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2; const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2; + const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0] + (sliceh_start >> c0_shift_h) * c0_linesize; + const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; + const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; const int d0_linesize = out->linesize[ plane + 0 ] / 2; const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2; const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2; const int c0_shift_w = s->shift_w[ component + 0 ]; const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp]; const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp]; - const int c0_shift_h = s->shift_h[ component + 0 ]; - const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; - const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; - const int src_h = in->height; - const int src_w = in->width; int x, y; - if (s->mode) { + if (column) { const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1); @@ -1670,7 +2038,7 @@ static av_always_inline void acolor16(WaveformContext *s, uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data); for (y = 0; y < src_h; y++) { - for (x = 0; x < src_w; x++) { + for (x = slicew_start; x < slicew_end; x++) { const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit); const int c1 = c1_data[x >> c1_shift_w]; const int c2 = c2_data[x >> c2_shift_w]; @@ -1691,9 +2059,9 @@ static av_always_inline void acolor16(WaveformContext *s, d2_data += d2_linesize; } } else { - uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x; - uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; - uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; + uint16_t *d0_data = (uint16_t *)out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x; + uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x; + uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x; if (mirror) { d0_data += s->size - 1; @@ -1701,7 +2069,7 @@ static av_always_inline void acolor16(WaveformContext *s, d2_data += s->size - 1; } - for (y = 0; y < src_h; y++) { + for (y = sliceh_start; y < sliceh_end; y++) { for (x = 0; x < src_w; x++) { const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit); const int c1 = c1_data[x >> c1_shift_w]; @@ -1729,38 +2097,66 @@ static av_always_inline void acolor16(WaveformContext *s, d2_data += d2_linesize; } } +} - envelope16(s, out, plane, plane, column ? offset_x : offset_y); +#define ACOLOR16_FUNC(name, column, mirror) \ +static int acolor16_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + acolor16(s, in, out, component, s->intensity,\ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } +ACOLOR16_FUNC(column_mirror, 1, 1) +ACOLOR16_FUNC(column, 1, 0) +ACOLOR16_FUNC(row_mirror, 0, 1) +ACOLOR16_FUNC(row, 0, 0) + static av_always_inline void acolor(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, - int column, int mirror) + int column, int mirror, + int jobnr, int nb_jobs) { const int plane = s->desc->comp[component].plane; - const uint8_t *c0_data = in->data[plane + 0]; - const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp]; - const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp]; - const int c0_linesize = in->linesize[ plane + 0 ]; - const int c1_linesize = in->linesize[(plane + 1) % s->ncomp]; - const int c2_linesize = in->linesize[(plane + 2) % s->ncomp]; - const int d0_linesize = out->linesize[ plane + 0 ]; - const int d1_linesize = out->linesize[(plane + 1) % s->ncomp]; - const int d2_linesize = out->linesize[(plane + 2) % s->ncomp]; + const int src_h = in->height; + const int src_w = in->width; + const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0; + const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h; + const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0; + const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w; const int c0_shift_w = s->shift_w[ component + 0 ]; const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp]; const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp]; const int c0_shift_h = s->shift_h[ component + 0 ]; const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp]; const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp]; + const int c0_linesize = in->linesize[ plane + 0 ]; + const int c1_linesize = in->linesize[(plane + 1) % s->ncomp]; + const int c2_linesize = in->linesize[(plane + 2) % s->ncomp]; + const uint8_t *c0_data = in->data[plane + 0] + (sliceh_start >> c0_shift_h) * c0_linesize; + const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; + const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; + const int d0_linesize = out->linesize[ plane + 0 ]; + const int d1_linesize = out->linesize[(plane + 1) % s->ncomp]; + const int d2_linesize = out->linesize[(plane + 2) % s->ncomp]; const int max = 255 - intensity; - const int src_h = in->height; - const int src_w = in->width; int x, y; - if (s->mode) { + if (column) { const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1); const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1); const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1); @@ -1775,7 +2171,7 @@ static av_always_inline void acolor(WaveformContext *s, uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data); for (y = 0; y < src_h; y++) { - for (x = 0; x < src_w; x++) { + for (x = slicew_start; x < slicew_end; x++) { const int c0 = c0_data[x >> c0_shift_w]; const int c1 = c1_data[x >> c1_shift_w]; const int c2 = c2_data[x >> c2_shift_w]; @@ -1796,9 +2192,9 @@ static av_always_inline void acolor(WaveformContext *s, d2_data += d2_linesize; } } else { - uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x; - uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; - uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; + uint8_t *d0_data = out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x; + uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x; + uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x; if (mirror) { d0_data += s->size - 1; @@ -1806,7 +2202,7 @@ static av_always_inline void acolor(WaveformContext *s, d2_data += s->size - 1; } - for (y = 0; y < src_h; y++) { + for (y = sliceh_start; y < sliceh_end; y++) { for (x = 0; x < src_w; x++) { const int c0 = c0_data[x >> c0_shift_w]; const int c1 = c1_data[x >> c1_shift_w]; @@ -1834,12 +2230,34 @@ static av_always_inline void acolor(WaveformContext *s, d2_data += d2_linesize; } } +} - envelope(s, out, plane, plane, column ? offset_x : offset_y); +#define ACOLOR_FUNC(name, column, mirror) \ +static int acolor_##name(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + WaveformContext *s = ctx->priv; \ + ThreadData *td = arg; \ + AVFrame *in = td->in; \ + AVFrame *out = td->out; \ + int component = td->component; \ + int offset_y = td->offset_y; \ + int offset_x = td->offset_x; \ + \ + acolor(s, in, out, component, s->intensity, \ + offset_y, offset_x, column, mirror, \ + jobnr, nb_jobs); \ + \ + return 0; \ } +ACOLOR_FUNC(column_mirror, 1, 1) +ACOLOR_FUNC(column, 1, 0) +ACOLOR_FUNC(row_mirror, 0, 1) +ACOLOR_FUNC(row, 0, 0) + static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 }; -static const uint8_t green_yuva_color[4] = { 255, 0, 0, 255 }; static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 }; static const GraticuleLines aflat_digital8[] = { @@ -2165,8 +2583,9 @@ static void blend_vline(uint8_t *dst, int height, int linesize, float o1, float } } -static void blend_vline16(uint16_t *dst, int height, int linesize, float o1, float o2, int v, int step) +static void blend_vline16(uint8_t *ddst, int height, int linesize, float o1, float o2, int v, int step) { + uint16_t *dst = (uint16_t *)ddst; int y; for (y = 0; y < height; y += step) { @@ -2176,7 +2595,7 @@ static void blend_vline16(uint16_t *dst, int height, int linesize, float o1, flo } } -static void blend_hline(uint8_t *dst, int width, float o1, float o2, int v, int step) +static void blend_hline(uint8_t *dst, int width, int unused, float o1, float o2, int v, int step) { int x; @@ -2185,8 +2604,9 @@ static void blend_hline(uint8_t *dst, int width, float o1, float o2, int v, int } } -static void blend_hline16(uint16_t *dst, int width, float o1, float o2, int v, int step) +static void blend_hline16(uint8_t *ddst, int width, int unused, float o1, float o2, int v, int step) { + uint16_t *dst = (uint16_t *)ddst; int x; for (x = 0; x < width; x += step) { @@ -2194,7 +2614,7 @@ static void blend_hline16(uint16_t *dst, int width, float o1, float o2, int v, i } } -static void draw_htext(AVFrame *out, int x, int y, float o1, float o2, const char *txt, const uint8_t color[4]) +static void draw_htext(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4]) { const uint8_t *font; int font_height; @@ -2246,7 +2666,7 @@ static void draw_htext16(AVFrame *out, int x, int y, int mult, float o1, float o } } -static void draw_vtext(AVFrame *out, int x, int y, float o1, float o2, const char *txt, const uint8_t color[4]) +static void draw_vtext(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4]) { const uint8_t *font; int font_height; @@ -2296,43 +2716,188 @@ static void draw_vtext16(AVFrame *out, int x, int y, int mult, float o1, float o } } +static void iblend_vline(uint8_t *dst, int height, int linesize, float o1, float o2, int v, int step) +{ + int y; + + for (y = 0; y < height; y += step) { + dst[0] = (v - dst[0]) * o1 + dst[0] * o2; + + dst += linesize * step; + } +} + +static void iblend_vline16(uint8_t *ddst, int height, int linesize, float o1, float o2, int v, int step) +{ + uint16_t *dst = (uint16_t *)ddst; + int y; + + for (y = 0; y < height; y += step) { + dst[0] = (v - dst[0]) * o1 + dst[0] * o2; + + dst += (linesize / 2) * step; + } +} + +static void iblend_hline(uint8_t *dst, int width, int unused, float o1, float o2, int v, int step) +{ + int x; + + for (x = 0; x < width; x += step) { + dst[x] = (v - dst[x]) * o1 + dst[x] * o2; + } +} + +static void iblend_hline16(uint8_t *ddst, int width, int unused, float o1, float o2, int v, int step) +{ + uint16_t *dst = (uint16_t *)ddst; + int x; + + for (x = 0; x < width; x += step) { + dst[x] = (v - dst[x]) * o1 + dst[x] * o2; + } +} + +static void idraw_htext(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4]) +{ + const uint8_t *font; + int font_height; + int i, plane; + + font = avpriv_cga_font, font_height = 8; + + for (plane = 0; plane < 4 && out->data[plane]; plane++) { + for (i = 0; txt[i]; i++) { + int char_y, mask; + int v = color[plane]; + + uint8_t *p = out->data[plane] + y * out->linesize[plane] + (x + i * 8); + for (char_y = 0; char_y < font_height; char_y++) { + for (mask = 0x80; mask; mask >>= 1) { + if (font[txt[i] * font_height + char_y] & mask) + p[0] = p[0] * o2 + (v - p[0]) * o1; + p++; + } + p += out->linesize[plane] - 8; + } + } + } +} + +static void idraw_htext16(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4]) +{ + const uint8_t *font; + int font_height; + int i, plane; + + font = avpriv_cga_font, font_height = 8; + + for (plane = 0; plane < 4 && out->data[plane]; plane++) { + for (i = 0; txt[i]; i++) { + int char_y, mask; + int v = color[plane] * mult; + + uint16_t *p = (uint16_t *)(out->data[plane] + y * out->linesize[plane]) + (x + i * 8); + for (char_y = 0; char_y < font_height; char_y++) { + for (mask = 0x80; mask; mask >>= 1) { + if (font[txt[i] * font_height + char_y] & mask) + p[0] = p[0] * o2 + (v - p[0]) * o1; + p++; + } + p += out->linesize[plane] / 2 - 8; + } + } + } +} + +static void idraw_vtext(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4]) +{ + const uint8_t *font; + int font_height; + int i, plane; + + font = avpriv_cga_font, font_height = 8; + + for (plane = 0; plane < 4 && out->data[plane]; plane++) { + for (i = 0; txt[i]; i++) { + int char_y, mask; + int v = color[plane]; + + for (char_y = font_height - 1; char_y >= 0; char_y--) { + uint8_t *p = out->data[plane] + (y + i * 10) * out->linesize[plane] + x; + for (mask = 0x80; mask; mask >>= 1) { + if (font[txt[i] * font_height + font_height - 1 - char_y] & mask) + p[char_y] = p[char_y] * o2 + (v - p[char_y]) * o1; + p += out->linesize[plane]; + } + } + } + } +} + +static void idraw_vtext16(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4]) +{ + const uint8_t *font; + int font_height; + int i, plane; + + font = avpriv_cga_font, font_height = 8; + + for (plane = 0; plane < 4 && out->data[plane]; plane++) { + for (i = 0; txt[i]; i++) { + int char_y, mask; + int v = color[plane] * mult; + + for (char_y = 0; char_y < font_height; char_y++) { + uint16_t *p = (uint16_t *)(out->data[plane] + (y + i * 10) * out->linesize[plane]) + x; + for (mask = 0x80; mask; mask >>= 1) { + if (font[txt[i] * font_height + font_height - 1 - char_y] & mask) + p[char_y] = p[char_y] * o2 + (v - p[char_y]) * o1; + p += out->linesize[plane] / 2; + } + } + } + } +} + static void graticule_none(WaveformContext *s, AVFrame *out) { } -static void graticule_green_row(WaveformContext *s, AVFrame *out) +static void graticule_row(WaveformContext *s, AVFrame *out) { const int step = (s->flags & 2) + 1; const float o1 = s->opacity; const float o2 = 1. - o1; const int height = s->display == PARADE ? out->height / s->acomp : out->height; - int k = 0, c, p, l, offset_x = 0, offset_y = 0; + int C, k = 0, c, p, l, offset_x = 0, offset_y = 0; for (c = 0; c < s->ncomp; c++) { if (!((1 << c) & s->pcomp) || (!s->display && k > 0)) continue; k++; + C = s->rgb ? 0 : c; for (p = 0; p < s->ncomp; p++) { - const int v = green_yuva_color[p]; + const int v = s->grat_yuva_color[p]; for (l = 0; l < s->nb_glines; l++) { - const uint16_t pos = s->glines[l].line[c].pos; + const uint16_t pos = s->glines[l].line[C].pos; int x = offset_x + (s->mirror ? s->size - 1 - pos : pos); uint8_t *dst = out->data[p] + offset_y * out->linesize[p] + x; - blend_vline(dst, height, out->linesize[p], o1, o2, v, step); + s->blend_line(dst, height, out->linesize[p], o1, o2, v, step); } } for (l = 0; l < s->nb_glines && (s->flags & 1); l++) { - const char *name = s->glines[l].line[c].name; - const uint16_t pos = s->glines[l].line[c].pos; + const char *name = s->glines[l].line[C].name; + const uint16_t pos = s->glines[l].line[C].pos; int x = offset_x + (s->mirror ? s->size - 1 - pos : pos) - 10; if (x < 0) x = 4; - draw_vtext(out, x, offset_y + 2, o1, o2, name, green_yuva_color); + s->draw_text(out, x, offset_y + 2, 1, o1, o2, name, s->grat_yuva_color); } offset_x += s->size * (s->display == STACK); @@ -2340,40 +2905,41 @@ static void graticule_green_row(WaveformContext *s, AVFrame *out) } } -static void graticule16_green_row(WaveformContext *s, AVFrame *out) +static void graticule16_row(WaveformContext *s, AVFrame *out) { const int step = (s->flags & 2) + 1; const float o1 = s->opacity; const float o2 = 1. - o1; - const int mult = s->size / 256; + const int mult = s->max / 256; const int height = s->display == PARADE ? out->height / s->acomp : out->height; - int k = 0, c, p, l, offset_x = 0, offset_y = 0; + int C, k = 0, c, p, l, offset_x = 0, offset_y = 0; for (c = 0; c < s->ncomp; c++) { if (!((1 << c) & s->pcomp) || (!s->display && k > 0)) continue; k++; + C = s->rgb ? 0 : c; for (p = 0; p < s->ncomp; p++) { - const int v = green_yuva_color[p] * mult; + const int v = s->grat_yuva_color[p] * mult; for (l = 0; l < s->nb_glines ; l++) { - const uint16_t pos = s->glines[l].line[c].pos; + const uint16_t pos = s->glines[l].line[C].pos; int x = offset_x + (s->mirror ? s->size - 1 - pos : pos); - uint16_t *dst = (uint16_t *)(out->data[p] + offset_y * out->linesize[p]) + x; + uint8_t *dst = (uint8_t *)(out->data[p] + offset_y * out->linesize[p]) + x * 2; - blend_vline16(dst, height, out->linesize[p], o1, o2, v, step); + s->blend_line(dst, height, out->linesize[p], o1, o2, v, step); } } for (l = 0; l < s->nb_glines && (s->flags & 1); l++) { - const char *name = s->glines[l].line[c].name; - const uint16_t pos = s->glines[l].line[c].pos; + const char *name = s->glines[l].line[C].name; + const uint16_t pos = s->glines[l].line[C].pos; int x = offset_x + (s->mirror ? s->size - 1 - pos : pos) - 10; if (x < 0) x = 4; - draw_vtext16(out, x, offset_y + 2, mult, o1, o2, name, green_yuva_color); + s->draw_text(out, x, offset_y + 2, mult, o1, o2, name, s->grat_yuva_color); } offset_x += s->size * (s->display == STACK); @@ -2381,39 +2947,40 @@ static void graticule16_green_row(WaveformContext *s, AVFrame *out) } } -static void graticule_green_column(WaveformContext *s, AVFrame *out) +static void graticule_column(WaveformContext *s, AVFrame *out) { const int step = (s->flags & 2) + 1; const float o1 = s->opacity; const float o2 = 1. - o1; const int width = s->display == PARADE ? out->width / s->acomp : out->width; - int k = 0, c, p, l, offset_y = 0, offset_x = 0; + int C, k = 0, c, p, l, offset_y = 0, offset_x = 0; for (c = 0; c < s->ncomp; c++) { if ((!((1 << c) & s->pcomp) || (!s->display && k > 0))) continue; k++; + C = s->rgb ? 0 : c; for (p = 0; p < s->ncomp; p++) { - const int v = green_yuva_color[p]; + const int v = s->grat_yuva_color[p]; for (l = 0; l < s->nb_glines ; l++) { - const uint16_t pos = s->glines[l].line[c].pos; + const uint16_t pos = s->glines[l].line[C].pos; int y = offset_y + (s->mirror ? s->size - 1 - pos : pos); uint8_t *dst = out->data[p] + y * out->linesize[p] + offset_x; - blend_hline(dst, width, o1, o2, v, step); + s->blend_line(dst, width, 1, o1, o2, v, step); } } for (l = 0; l < s->nb_glines && (s->flags & 1); l++) { - const char *name = s->glines[l].line[c].name; - const uint16_t pos = s->glines[l].line[c].pos; + const char *name = s->glines[l].line[C].name; + const uint16_t pos = s->glines[l].line[C].pos; int y = offset_y + (s->mirror ? s->size - 1 - pos : pos) - 10; if (y < 0) y = 4; - draw_htext(out, 2 + offset_x, y, o1, o2, name, green_yuva_color); + s->draw_text(out, 2 + offset_x, y, 1, o1, o2, name, s->grat_yuva_color); } offset_y += s->size * (s->display == STACK); @@ -2421,40 +2988,41 @@ static void graticule_green_column(WaveformContext *s, AVFrame *out) } } -static void graticule16_green_column(WaveformContext *s, AVFrame *out) +static void graticule16_column(WaveformContext *s, AVFrame *out) { const int step = (s->flags & 2) + 1; const float o1 = s->opacity; const float o2 = 1. - o1; - const int mult = s->size / 256; + const int mult = s->max / 256; const int width = s->display == PARADE ? out->width / s->acomp : out->width; - int k = 0, c, p, l, offset_x = 0, offset_y = 0; + int C, k = 0, c, p, l, offset_x = 0, offset_y = 0; for (c = 0; c < s->ncomp; c++) { if ((!((1 << c) & s->pcomp) || (!s->display && k > 0))) continue; k++; + C = s->rgb ? 0 : c; for (p = 0; p < s->ncomp; p++) { - const int v = green_yuva_color[p] * mult; + const int v = s->grat_yuva_color[p] * mult; for (l = 0; l < s->nb_glines ; l++) { - const uint16_t pos = s->glines[l].line[c].pos; + const uint16_t pos = s->glines[l].line[C].pos; int y = offset_y + (s->mirror ? s->size - 1 - pos : pos); - uint16_t *dst = (uint16_t *)(out->data[p] + y * out->linesize[p]) + offset_x; + uint8_t *dst = (uint8_t *)(out->data[p] + y * out->linesize[p]) + offset_x * 2; - blend_hline16(dst, width, o1, o2, v, step); + s->blend_line(dst, width, 1, o1, o2, v, step); } } for (l = 0; l < s->nb_glines && (s->flags & 1); l++) { - const char *name = s->glines[l].line[c].name; - const uint16_t pos = s->glines[l].line[c].pos; + const char *name = s->glines[l].line[C].name; + const uint16_t pos = s->glines[l].line[C].pos; int y = offset_y + (s->mirror ? s->size - 1 - pos: pos) - 10; if (y < 0) y = 4; - draw_htext16(out, 2 + offset_x, y, mult, o1, o2, name, green_yuva_color); + s->draw_text(out, 2 + offset_x, y, mult, o1, o2, name, s->grat_yuva_color); } offset_y += s->size * (s->display == STACK); @@ -2481,6 +3049,8 @@ static int config_input(AVFilterLink *inlink) s->graticulef = graticule_none; switch (s->filter) { + case XFLAT: + case YFLAT: case AFLAT: s->size = 256 * 2; break; case FLAT: s->size = 256 * 3; break; default: s->size = 256; break; @@ -2488,54 +3058,89 @@ static int config_input(AVFilterLink *inlink) switch (s->filter | ((s->bits > 8) << 4) | (s->mode << 8) | (s->mirror << 12)) { - case 0x1100: s->waveform = lowpass_column_mirror; break; - case 0x1000: s->waveform = lowpass_row_mirror; break; - case 0x0100: s->waveform = lowpass_column; break; - case 0x0000: s->waveform = lowpass_row; break; - case 0x1110: s->waveform = lowpass16_column_mirror; break; - case 0x1010: s->waveform = lowpass16_row_mirror; break; - case 0x0110: s->waveform = lowpass16_column; break; - case 0x0010: s->waveform = lowpass16_row; break; - case 0x1101: - case 0x1001: - case 0x0101: - case 0x0001: s->waveform = flat; break; - case 0x1111: - case 0x1011: - case 0x0111: - case 0x0011: s->waveform = flat16; break; - case 0x1102: - case 0x1002: - case 0x0102: - case 0x0002: s->waveform = aflat; break; - case 0x1112: - case 0x1012: - case 0x0112: - case 0x0012: s->waveform = aflat16; break; - case 0x1103: - case 0x1003: - case 0x0103: - case 0x0003: s->waveform = chroma; break; - case 0x1113: - case 0x1013: - case 0x0113: - case 0x0013: s->waveform = chroma16; break; - case 0x1104: - case 0x1004: - case 0x0104: - case 0x0004: s->waveform = color; break; - case 0x1114: - case 0x1014: - case 0x0114: - case 0x0014: s->waveform = color16; break; - case 0x1105: - case 0x1005: - case 0x0105: - case 0x0005: s->waveform = acolor; break; - case 0x1115: - case 0x1015: - case 0x0115: - case 0x0015: s->waveform = acolor16; break; + case 0x1100: s->waveform_slice = lowpass_column_mirror; break; + case 0x1000: s->waveform_slice = lowpass_row_mirror; break; + case 0x0100: s->waveform_slice = lowpass_column; break; + case 0x0000: s->waveform_slice = lowpass_row; break; + case 0x1110: s->waveform_slice = lowpass16_column_mirror; break; + case 0x1010: s->waveform_slice = lowpass16_row_mirror; break; + case 0x0110: s->waveform_slice = lowpass16_column; break; + case 0x0010: s->waveform_slice = lowpass16_row; break; + case 0x1101: s->waveform_slice = flat_column_mirror; break; + case 0x1001: s->waveform_slice = flat_row_mirror; break; + case 0x0101: s->waveform_slice = flat_column; break; + case 0x0001: s->waveform_slice = flat_row; break; + case 0x1111: s->waveform_slice = flat16_column_mirror; break; + case 0x1011: s->waveform_slice = flat16_row_mirror; break; + case 0x0111: s->waveform_slice = flat16_column; break; + case 0x0011: s->waveform_slice = flat16_row; break; + case 0x1102: s->waveform_slice = aflat_column_mirror; break; + case 0x1002: s->waveform_slice = aflat_row_mirror; break; + case 0x0102: s->waveform_slice = aflat_column; break; + case 0x0002: s->waveform_slice = aflat_row; break; + case 0x1112: s->waveform_slice = aflat16_column_mirror; break; + case 0x1012: s->waveform_slice = aflat16_row_mirror; break; + case 0x0112: s->waveform_slice = aflat16_column; break; + case 0x0012: s->waveform_slice = aflat16_row; break; + case 0x1103: s->waveform_slice = chroma_column_mirror; break; + case 0x1003: s->waveform_slice = chroma_row_mirror; break; + case 0x0103: s->waveform_slice = chroma_column; break; + case 0x0003: s->waveform_slice = chroma_row; break; + case 0x1113: s->waveform_slice = chroma16_column_mirror; break; + case 0x1013: s->waveform_slice = chroma16_row_mirror; break; + case 0x0113: s->waveform_slice = chroma16_column; break; + case 0x0013: s->waveform_slice = chroma16_row; break; + case 0x1104: s->waveform_slice = color_column_mirror; break; + case 0x1004: s->waveform_slice = color_row_mirror; break; + case 0x0104: s->waveform_slice = color_column; break; + case 0x0004: s->waveform_slice = color_row; break; + case 0x1114: s->waveform_slice = color16_column_mirror; break; + case 0x1014: s->waveform_slice = color16_row_mirror; break; + case 0x0114: s->waveform_slice = color16_column; break; + case 0x0014: s->waveform_slice = color16_row; break; + case 0x1105: s->waveform_slice = acolor_column_mirror; break; + case 0x1005: s->waveform_slice = acolor_row_mirror; break; + case 0x0105: s->waveform_slice = acolor_column; break; + case 0x0005: s->waveform_slice = acolor_row; break; + case 0x1115: s->waveform_slice = acolor16_column_mirror; break; + case 0x1015: s->waveform_slice = acolor16_row_mirror; break; + case 0x0115: s->waveform_slice = acolor16_column; break; + case 0x0015: s->waveform_slice = acolor16_row; break; + case 0x1106: s->waveform_slice = xflat_column_mirror; break; + case 0x1006: s->waveform_slice = xflat_row_mirror; break; + case 0x0106: s->waveform_slice = xflat_column; break; + case 0x0006: s->waveform_slice = xflat_row; break; + case 0x1116: s->waveform_slice = xflat16_column_mirror; break; + case 0x1016: s->waveform_slice = xflat16_row_mirror; break; + case 0x0116: s->waveform_slice = xflat16_column; break; + case 0x0016: s->waveform_slice = xflat16_row; break; + case 0x1107: s->waveform_slice = yflat_column_mirror; break; + case 0x1007: s->waveform_slice = yflat_row_mirror; break; + case 0x0107: s->waveform_slice = yflat_column; break; + case 0x0007: s->waveform_slice = yflat_row; break; + case 0x1117: s->waveform_slice = yflat16_column_mirror; break; + case 0x1017: s->waveform_slice = yflat16_row_mirror; break; + case 0x0117: s->waveform_slice = yflat16_column; break; + case 0x0017: s->waveform_slice = yflat16_row; break; + } + + s->grat_yuva_color[0] = 255; + s->grat_yuva_color[1] = s->graticule == GRAT_INVERT ? 255 : 0; + s->grat_yuva_color[2] = s->graticule == GRAT_ORANGE || s->graticule == GRAT_INVERT ? 255 : 0; + s->grat_yuva_color[3] = 255; + + if (s->mode == 0 && s->graticule != GRAT_INVERT) { + s->blend_line = s->bits <= 8 ? blend_vline : blend_vline16; + s->draw_text = s->bits <= 8 ? draw_vtext : draw_vtext16; + } else if (s->graticule != GRAT_INVERT) { + s->blend_line = s->bits <= 8 ? blend_hline : blend_hline16; + s->draw_text = s->bits <= 8 ? draw_htext : draw_htext16; + } else if (s->mode == 0 && s->graticule == GRAT_INVERT) { + s->blend_line = s->bits <= 8 ? iblend_vline : iblend_vline16; + s->draw_text = s->bits <= 8 ? idraw_vtext : idraw_vtext16; + } else if (s->graticule == GRAT_INVERT) { + s->blend_line = s->bits <= 8 ? iblend_hline : iblend_hline16; + s->draw_text = s->bits <= 8 ? idraw_htext : idraw_htext16; } switch (s->filter) { @@ -2544,11 +3149,13 @@ static int config_input(AVFilterLink *inlink) case ACOLOR: case CHROMA: case AFLAT: + case XFLAT: + case YFLAT: case FLAT: - if (s->graticule && s->mode == 1) - s->graticulef = s->bits > 8 ? graticule16_green_column : graticule_green_column; - else if (s->graticule && s->mode == 0) - s->graticulef = s->bits > 8 ? graticule16_green_row : graticule_green_row; + if (s->graticule > GRAT_NONE && s->mode == 1) + s->graticulef = s->bits > 8 ? graticule16_column : graticule_column; + else if (s->graticule > GRAT_NONE && s->mode == 0) + s->graticulef = s->bits > 8 ? graticule16_row : graticule_row; break; } @@ -2611,6 +3218,8 @@ static int config_input(AVFilterLink *inlink) break; } break; + case XFLAT: + case YFLAT: case AFLAT: switch (s->scale) { case DIGITAL: @@ -2671,14 +3280,17 @@ static int config_input(AVFilterLink *inlink) s->size = s->size << (s->bits - 8); + s->tint[0] = .5f * (s->ftint[0] + 1.f) * (s->size - 1); + s->tint[1] = .5f * (s->ftint[1] + 1.f) * (s->size - 1); + switch (inlink->format) { case AV_PIX_FMT_GBRAP: case AV_PIX_FMT_GBRP: case AV_PIX_FMT_GBRP9: case AV_PIX_FMT_GBRP10: case AV_PIX_FMT_GBRP12: + s->rgb = 1; memcpy(s->bg_color, black_gbrp_color, sizeof(s->bg_color)); - s->graticulef = graticule_none; break; default: memcpy(s->bg_color, black_yuva_color, sizeof(s->bg_color)); @@ -2701,6 +3313,9 @@ static int config_output(AVFilterLink *outlink) comp++; } s->acomp = comp; + if (s->acomp == 0) + return AVERROR(EINVAL); + s->odesc = av_pix_fmt_desc_get(outlink->format); s->dcomp = s->odesc->nb_components; @@ -2784,6 +3399,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) for (k = 0, i = 0; k < s->ncomp; k++) { if ((1 << k) & s->pcomp) { + const int plane = s->desc->comp[k].plane; + ThreadData td; int offset_y; int offset_x; @@ -2794,7 +3411,51 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) offset_y = s->mode ? i++ * s->size * !!s->display : 0; offset_x = s->mode ? 0 : i++ * s->size * !!s->display; } - s->waveform(s, in, out, k, s->intensity, offset_y, offset_x, s->mode, s->mirror); + + td.in = in; + td.out = out; + td.component = k; + td.offset_y = offset_y; + td.offset_x = offset_x; + ctx->internal->execute(ctx, s->waveform_slice, &td, NULL, ff_filter_get_nb_threads(ctx)); + switch (s->filter) { + case LOWPASS: + if (s->bits <= 8) + envelope(s, out, plane, s->rgb || s->display == OVERLAY ? plane : 0, s->mode ? offset_x : offset_y); + else + envelope16(s, out, plane, s->rgb || s->display == OVERLAY ? plane : 0, s->mode ? offset_x : offset_y); + break; + case ACOLOR: + case CHROMA: + case COLOR: + if (s->bits <= 8) + envelope(s, out, plane, plane, s->mode ? offset_x : offset_y); + else + envelope16(s, out, plane, plane, s->mode ? offset_x : offset_y); + break; + case FLAT: + if (s->bits <= 8) { + envelope(s, out, plane, plane, s->mode ? offset_x : offset_y); + envelope(s, out, plane, (plane + 1) % s->ncomp, s->mode ? offset_x : offset_y); + } else { + envelope16(s, out, plane, plane, s->mode ? offset_x : offset_y); + envelope16(s, out, plane, (plane + 1) % s->ncomp, s->mode ? offset_x : offset_y); + } + break; + case AFLAT: + case XFLAT: + case YFLAT: + if (s->bits <= 8) { + envelope(s, out, plane, (plane + 0) % s->ncomp, s->mode ? offset_x : offset_y); + envelope(s, out, plane, (plane + 1) % s->ncomp, s->mode ? offset_x : offset_y); + envelope(s, out, plane, (plane + 2) % s->ncomp, s->mode ? offset_x : offset_y); + } else { + envelope16(s, out, plane, (plane + 0) % s->ncomp, s->mode ? offset_x : offset_y); + envelope16(s, out, plane, (plane + 1) % s->ncomp, s->mode ? offset_x : offset_y); + envelope16(s, out, plane, (plane + 2) % s->ncomp, s->mode ? offset_x : offset_y); + } + break; + } } } s->graticulef(s, out); @@ -2838,4 +3499,5 @@ AVFilter ff_vf_waveform = { .uninit = uninit, .inputs = inputs, .outputs = outputs, + .flags = AVFILTER_FLAG_SLICE_THREADS, };