From 2adc81f0a2459a565437009ff6f4ace1dca3d46c Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Thu, 23 Aug 2012 12:32:13 +0200 Subject: vf_dlopen: add a generic filter to load external filters Usage: -vf dlopen=filename.so:args... Examples of such filters are provided in TOOLS/vf_dlopen/ --- TOOLS/vf_dlopen/Makefile | 24 +++++ TOOLS/vf_dlopen/filterutils.c | 19 ++++ TOOLS/vf_dlopen/filterutils.h | 6 ++ TOOLS/vf_dlopen/showqscale.c | 99 +++++++++++++++++ TOOLS/vf_dlopen/telecine.c | 242 ++++++++++++++++++++++++++++++++++++++++++ TOOLS/vf_dlopen/tile.c | 157 +++++++++++++++++++++++++++ 6 files changed, 547 insertions(+) create mode 100644 TOOLS/vf_dlopen/Makefile create mode 100644 TOOLS/vf_dlopen/filterutils.c create mode 100644 TOOLS/vf_dlopen/filterutils.h create mode 100644 TOOLS/vf_dlopen/showqscale.c create mode 100644 TOOLS/vf_dlopen/telecine.c create mode 100644 TOOLS/vf_dlopen/tile.c (limited to 'TOOLS') diff --git a/TOOLS/vf_dlopen/Makefile b/TOOLS/vf_dlopen/Makefile new file mode 100644 index 0000000000..2fa4e740c2 --- /dev/null +++ b/TOOLS/vf_dlopen/Makefile @@ -0,0 +1,24 @@ +FILTERS = showqscale telecine tile +COMMON = filterutils.o + +OBJECTS = $(patsubst %,%.o,$(FILTERS)) $(COMMON) +HEADERS = $(wildcard *.h) +OUT = $(patsubst %,%.so,$(FILTERS)) + +CFLAGS ?= -Wall -Wextra -O3 -march=native -mtune=native + +CPPFLAGS += -I../../libmpcodecs +CFLAGS += -fPIC +LDFLAGS += -shared -fPIC + +all: $(OUT) + +clean: + $(RM) $(OBJECTS) $(OUT) + +%.so: %.o $(COMMON) + $(CC) $(LDFLAGS) $(LIBS) -o $@ $(COMMON) $< + +# FIXME replace this by real dependency tracking +%.o: %.c $(HEADERS) + diff --git a/TOOLS/vf_dlopen/filterutils.c b/TOOLS/vf_dlopen/filterutils.c new file mode 100644 index 0000000000..e2f14092de --- /dev/null +++ b/TOOLS/vf_dlopen/filterutils.c @@ -0,0 +1,19 @@ +#include +#include + +#include "filterutils.h" + +void copy_plane( + unsigned char *dest, unsigned dest_stride, + const unsigned char *src, unsigned src_stride, + unsigned length, + unsigned rows + ) +{ + unsigned i; + assert(dest_stride >= length); + assert(src_stride >= length); + for (i = 0; i < rows; ++i) + memcpy(&dest[dest_stride * i], &src[src_stride * i], length); +} + diff --git a/TOOLS/vf_dlopen/filterutils.h b/TOOLS/vf_dlopen/filterutils.h new file mode 100644 index 0000000000..4b4229d8ea --- /dev/null +++ b/TOOLS/vf_dlopen/filterutils.h @@ -0,0 +1,6 @@ +void copy_plane( + unsigned char *dest, unsigned dest_stride, + const unsigned char *src, unsigned src_stride, + unsigned length, + unsigned rows + ); diff --git a/TOOLS/vf_dlopen/showqscale.c b/TOOLS/vf_dlopen/showqscale.c new file mode 100644 index 0000000000..9bece60a4f --- /dev/null +++ b/TOOLS/vf_dlopen/showqscale.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include + +#include "vf_dlopen.h" + +/* + * qscale visualizer + * + * usage: -vf dlopen=./showqscale.so + * + * uses reddish colors for high QPs, and greenish colors for low QPs + */ + +#define PLANE_Y 0 +#define PLANE_U 1 +#define PLANE_V 2 + +static int qs_put_image(struct vf_dlopen_context *ctx) +{ + unsigned int x, y, p; + + assert(ctx->inpic.planes == ctx->outpic[0].planes); + + for (p = 0; p < ctx->outpic[0].planes; ++p) { + assert(ctx->inpic.planewidth[p] == ctx->outpic[0].planewidth[p]); + assert(ctx->inpic.planeheight[p] == ctx->outpic[0].planeheight[p]); + if ((p == PLANE_U || p == PLANE_V) && ctx->inpic_qscale) + continue; +#if 0 + // copy as is + for (y = 0; y < ctx->outpic[0].planeheight[p]; ++y) + memcpy( + &ctx->outpic[0].plane[p][ctx->outpic[0].planestride[p] * y], + &inpic[ctx->outpic[0].planeofs[p] + ctx->inpic.planestride[p] * y], + ctx->outpic[0].planewidth[p] + ); +#else + // reduce contrast + for (y = 0; y < ctx->outpic[0].planeheight[p]; ++y) + for (x = 0; x < ctx->outpic[0].planewidth[p]; ++x) + ctx->outpic[0].plane[p][ctx->outpic[0].planestride[p] * y + x] = + 0x20 + ((ctx->inpic.plane[p][ctx->inpic.planestride[p] * y + x] * 3) >> 2); +#endif + } + + if (ctx->inpic_qscale) { + int qmin = 255; + int qmax = -255; + + // clear U plane + p = PLANE_U; + for (y = 0; y < ctx->outpic[0].planeheight[p]; ++y) + memset( + &ctx->outpic[0].plane[p][ctx->outpic[0].planestride[p] * y], + 0x80, + ctx->outpic[0].planewidth[p] + ); + + // replace V by the qp (0 = green, 12 = red) + p = PLANE_V; + for (y = 0; y < ctx->outpic[0].planeheight[p]; ++y) + for (x = 0; x < ctx->outpic[0].planewidth[p]; ++x) { + int q = ctx->inpic_qscale[ + (x >> (ctx->inpic_qscaleshift - ctx->inpic.planexshift[p])) + + (y >> (ctx->inpic_qscaleshift - ctx->inpic.planeyshift[p])) * ctx->inpic_qscalestride]; + if (q < qmin) + qmin = q; + if (q > qmax) + qmax = q; + int v = 128 + 21 * (q - 6); // range: 0 = green, 12 = red + if (v < 0) + v = 0; + if (v > 255) + v = 255; + ctx->outpic[0].plane[p][ctx->outpic[0].planestride[p] * y + x] = v; + } + + // printf("qscale range: %d .. %d\n", qmin, qmax); + } + + ctx->outpic[0].pts = ctx->inpic.pts; + return 1; +} + +int vf_dlopen_getcontext(struct vf_dlopen_context *ctx, int argc, const char **argv) +{ + VF_DLOPEN_CHECK_VERSION(ctx); + (void) argc; + (void) argv; + static struct vf_dlopen_formatpair map[] = { + { "yv12", "yv12" }, + { NULL, NULL } + }; + ctx->format_mapping = map; + ctx->put_image = qs_put_image; + return 1; +} diff --git a/TOOLS/vf_dlopen/telecine.c b/TOOLS/vf_dlopen/telecine.c new file mode 100644 index 0000000000..109fef2d6b --- /dev/null +++ b/TOOLS/vf_dlopen/telecine.c @@ -0,0 +1,242 @@ +#include +#include +#include +#include + +#include "vf_dlopen.h" +#include "filterutils.h" + +#define MIN(a,b) ((a)<(b)?(a):(b)) + +/* + * telecine filter + * + * usage: -vf dlopen=./telecine.so:t:32 + * + * Parameter: first parameter is "t" for top field first, "b" for bottom field first + * then digits (0-9) for how many fields a frame is to be displayed + * + * Typical patterns (see http://en.wikipedia.org/wiki/Telecine): + * + * NTSC output (30i): + * 27.5p: 32222 + * 24p: 23 (classic) + * 24p: 2332 (preferred) + * 20p: 33 + * 18p: 334 + * 16p: 3444 + * + * PAL output (25i): + * 27.5p: 12222 + * 24p: 222222222223 ("Euro pulldown") + * 16.67p: 33 + * 16p: 33333334 + */ + +typedef struct { + int firstfield; + const char *pattern; + unsigned int pattern_pos; + unsigned char *buffer_plane[4]; + size_t buffer_size[4]; + int pts_num; + int pts_denom; + int occupied; + double lastpts_in; + double lastpts_out; +} tc_data_t; + +static int tc_config(struct vf_dlopen_context *ctx) +{ + // we may return more than one pic! + tc_data_t *tc = ctx->priv; + const char *p; + int max = 0; + tc->pts_num = 0; + tc->pts_denom = 0; + for (p = tc->pattern; *p; ++p) { + if (*p - '0' > max) + max = *p - '0'; + tc->pts_num += 2; + tc->pts_denom += *p - '0'; + } + ctx->out_cnt = (max + 1) / 2; + printf( + "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n", + tc->pattern, ctx->out_cnt, tc->pts_num, tc->pts_denom); + return 1; +} + +static int tc_put_image(struct vf_dlopen_context *ctx) +{ + tc_data_t *tc = ctx->priv; + + unsigned p; + unsigned np = ctx->outpic[0].planes; + assert(ctx->inpic.planes == ctx->outpic[0].planes); + + int need_reinit = 0; + + // fix buffers + for (p = 0; p < np; ++p) { + size_t sz = ctx->inpic.planestride[p] * ctx->inpic.planeheight[p]; + if (sz != tc->buffer_size[p]) { + if (p == 0 && tc->buffer_plane[p]) + printf("WARNING: reinitializing telecine buffers.\n"); + tc->buffer_plane[p] = realloc(tc->buffer_plane[p], sz); + tc->buffer_size[p] = sz; + need_reinit = 1; + } + } + + // too big pts change? reinit + if (ctx->inpic.pts < tc->lastpts_in || ctx->inpic.pts > tc->lastpts_in + 0.5) + need_reinit = 1; + + if (need_reinit) { + // initialize telecine + tc->pattern_pos = 0; + tc->occupied = 0; + tc->lastpts_in = ctx->inpic.pts; + tc->lastpts_out = ctx->inpic.pts; + } + + int len = tc->pattern[tc->pattern_pos] - '0'; + unsigned nout; + double delta = ctx->inpic.pts - tc->lastpts_in; + tc->lastpts_in = ctx->inpic.pts; + + for (nout = 0; nout < ctx->out_cnt; ++nout) { + for (p = 0; p < np; ++p) { + assert(ctx->inpic.planewidth[p] == ctx->outpic[nout].planewidth[p]); + assert(ctx->inpic.planeheight[p] == ctx->outpic[nout].planeheight[p]); + } + } + nout = 0; + + if (tc->pattern_pos == 0 && !tc->occupied) { + // at the start of the pattern, reset pts + double newpts = ctx->inpic.pts - (delta * tc->pts_num) / tc->pts_denom; + // printf("pts reset: %f -> %f (delta: %f)\n", tc->lastpts_out, newpts, newpts - tc->lastpts_out); + tc->lastpts_out = newpts; + } + ++tc->pattern_pos; + if (!tc->pattern[tc->pattern_pos]) + tc->pattern_pos = 0; + + if (len == 0) { + // do not output any field from this frame + return 0; + } + + if (tc->occupied) { + for (p = 0; p < np; ++p) { + // fill in the EARLIER field from the buffered pic + copy_plane( + &ctx->outpic[nout].plane[p][ctx->outpic[nout].planestride[p] * tc->firstfield], + ctx->outpic[nout].planestride[p] * 2, + &tc->buffer_plane[p][ctx->inpic.planestride[p] * tc->firstfield], + ctx->inpic.planestride[p] * 2, + MIN(ctx->inpic.planestride[p], ctx->outpic[nout].planestride[p]), + (ctx->inpic.planeheight[p] - tc->firstfield + 1) / 2 + ); + // fill in the LATER field from the new pic + copy_plane( + &ctx->outpic[nout].plane[p][ctx->outpic[nout].planestride[p] * !tc->firstfield], + ctx->outpic[nout].planestride[p] * 2, + &ctx->inpic.plane[p][ctx->inpic.planestride[p] * !tc->firstfield], + ctx->inpic.planestride[p] * 2, + MIN(ctx->inpic.planestride[p], ctx->outpic[nout].planestride[p]), + (ctx->inpic.planeheight[p] - !tc->firstfield + 1) / 2 + ); + } + tc->lastpts_out += (delta * tc->pts_num) / tc->pts_denom; + ctx->outpic[nout].pts = tc->lastpts_out; + // printf("pts written: %f\n", ctx->outpic[nout].pts); + ++nout; + --len; + tc->occupied = 0; + } + + while (len >= 2) { + // output THIS image as-is + for (p = 0; p < np; ++p) + copy_plane( + ctx->outpic[nout].plane[p], ctx->outpic[nout].planestride[p], + ctx->inpic.plane[p], ctx->inpic.planestride[p], + MIN(ctx->inpic.planestride[p], ctx->outpic[nout].planestride[p]), + ctx->inpic.planeheight[p] + ); + tc->lastpts_out += (delta * tc->pts_num) / tc->pts_denom; + ctx->outpic[nout].pts = tc->lastpts_out; + // printf("pts written: %f\n", ctx->outpic[nout].pts); + ++nout; + len -= 2; + } + + if (len >= 1) { + // copy THIS image to the buffer, we need it later + for (p = 0; p < np; ++p) + copy_plane( + &tc->buffer_plane[p][0], ctx->inpic.planestride[p], + &ctx->inpic.plane[p][0], ctx->inpic.planestride[p], + ctx->inpic.planestride[p], + ctx->inpic.planeheight[p] + ); + tc->occupied = 1; + } + + return nout; +} + +void tc_uninit(struct vf_dlopen_context *ctx) +{ + tc_data_t *tc = ctx->priv; + free(tc->buffer_plane[3]); + free(tc->buffer_plane[2]); + free(tc->buffer_plane[1]); + free(tc->buffer_plane[0]); + free(tc); +} + +int vf_dlopen_getcontext(struct vf_dlopen_context *ctx, int argc, const char **argv) +{ + VF_DLOPEN_CHECK_VERSION(ctx); + + const char *a0 = (argc < 1) ? "t" : argv[0]; + const char *a1 = (argc < 2) ? "23" : argv[1]; + + if (!a0[0] || a0[1] || !a1[0] || argc > 2) + return -1; + + tc_data_t *tc = malloc(sizeof(tc_data_t)); + memset(tc, 0, sizeof(*tc)); + + if (a0[0] == 't') + tc->firstfield = 0; + else if (a0[0] == 'b') + tc->firstfield = 1; + else { + printf("telecine: invalid first field\n"); + free(tc); + return -1; + } + + tc->pattern = a1; + + const char *p; + for (p = tc->pattern; *p; ++p) + if (*p < '0' || *p > '9') { + printf("telecine: invalid pattern\n"); + free(tc); + return -1; + } + + ctx->priv = tc; + ctx->format_mapping = NULL; // anything goes + ctx->config = tc_config; + ctx->put_image = tc_put_image; + ctx->uninit = tc_uninit; + + return 1; +} diff --git a/TOOLS/vf_dlopen/tile.c b/TOOLS/vf_dlopen/tile.c new file mode 100644 index 0000000000..0e9a14fb62 --- /dev/null +++ b/TOOLS/vf_dlopen/tile.c @@ -0,0 +1,157 @@ +#include +#include +#include +#include + +#include "vf_dlopen.h" +#include "filterutils.h" + +/* + * tile filter + * + * usage: -vf dlopen=./tile.so:4:3 + * + * only supports rgb24 and yv12 for now + * in theory can support any format where rows are a multiple of bytes, and the + * multiple is known + */ + +#define ALLFORMATS \ + /* format bytes xmul ymul */ \ + FORMAT("rgb24", 3, 1, 1) \ + FORMAT("yv12", 1, 2, 2) + +typedef struct { + int rows, cols; + unsigned char *buffer_plane[4]; + size_t buffer_size[4]; + int pos; + int pixelbytes; +} tile_data_t; + +static int tile_config(struct vf_dlopen_context *ctx) +{ + // we may return more than one pic! + tile_data_t *tile = ctx->priv; + + ctx->out_width = tile->cols * ctx->in_width; + ctx->out_height = tile->rows * ctx->in_height; + ctx->out_d_width = tile->cols * ctx->in_d_width; + ctx->out_d_height = tile->rows * ctx->in_d_height; + +#define FORMAT(fmt,sz,xmul,ymul) \ + if (!strcmp(ctx->in_fmt, fmt)) { \ + if (ctx->in_width % xmul || ctx->in_height % ymul) { \ + printf("Format " fmt " requires width to be a multiple of %d and height to be a multiple of %d\n", \ + xmul, ymul); \ + return -1; \ + } \ + tile->pixelbytes = sz; \ + } else + ALLFORMATS +#undef FORMAT + { + printf("Format %s is not in the list, how come?\n", ctx->in_fmt); + return -1; + } + + return 1; +} + +static int tile_put_image(struct vf_dlopen_context *ctx) +{ + tile_data_t *tile = ctx->priv; + + unsigned p; + unsigned np = ctx->outpic[0].planes; + assert(ctx->inpic.planes == ctx->outpic[0].planes); + + // fix buffers + for (p = 0; p < np; ++p) { + size_t sz = ctx->outpic->planestride[p] * ctx->outpic->planeheight[p]; + if (sz != tile->buffer_size[p]) { + if (p == 0 && tile->buffer_plane[p]) + printf("WARNING: reinitializing output buffers.\n"); + tile->buffer_plane[p] = realloc(tile->buffer_plane[p], sz); + tile->buffer_size[p] = sz; + tile->pos = 0; + } + } + + for (p = 0; p < np; ++p) { + assert(ctx->inpic.planewidth[p] * tile->cols == ctx->outpic->planewidth[p]); + assert(ctx->inpic.planeheight[p] * tile->rows == ctx->outpic->planeheight[p]); + } + + // copy this frame + for (p = 0; p < np; ++p) + copy_plane( + &tile->buffer_plane[p][ctx->outpic->planestride[p] * ctx->inpic.planeheight[p] * (tile->pos / tile->cols) + tile->pixelbytes * ctx->inpic.planewidth[p] * (tile->pos % tile->cols)], + ctx->outpic->planestride[p], + ctx->inpic.plane[p], + ctx->inpic.planestride[p], + tile->pixelbytes * ctx->inpic.planewidth[p], + ctx->inpic.planeheight[p] + ); + + ++tile->pos; + if (tile->pos == tile->rows * tile->cols) { + // copy THIS image to the buffer, we need it later + for (p = 0; p < np; ++p) + copy_plane( + ctx->outpic->plane[p], ctx->outpic->planestride[p], + &tile->buffer_plane[p][0], ctx->outpic->planestride[p], + tile->pixelbytes * ctx->outpic->planewidth[p], + ctx->outpic->planeheight[p] + ); + ctx->outpic->pts = ctx->inpic.pts; + tile->pos = 0; + return 1; + } + + return 0; +} + +void tile_uninit(struct vf_dlopen_context *ctx) +{ + tile_data_t *tile = ctx->priv; + free(tile->buffer_plane[3]); + free(tile->buffer_plane[2]); + free(tile->buffer_plane[1]); + free(tile->buffer_plane[0]); + free(tile); +} + +int vf_dlopen_getcontext(struct vf_dlopen_context *ctx, int argc, const char **argv) +{ + VF_DLOPEN_CHECK_VERSION(ctx); + + if (argc != 2) + return -1; + + tile_data_t *tile = malloc(sizeof(tile_data_t)); + memset(tile, 0, sizeof(*tile)); + + tile->cols = atoi(argv[0]); + tile->rows = atoi(argv[1]); + + if (!tile->rows || !tile->cols) { + printf("tile: invalid rows/cols\n"); + free(tile); + return -1; + } + + ctx->priv = tile; + static struct vf_dlopen_formatpair map[] = { +#define FORMAT(fmt,sz,xmul,ymul) {fmt, NULL}, + ALLFORMATS +#undef FORMAT + {NULL, NULL} + }; + ctx->format_mapping = map; + ctx->config = tile_config; + ctx->put_image = tile_put_image; + ctx->uninit = tile_uninit; + + return 1; +} -- cgit v1.2.3