[FFmpeg-devel] [PATCH] avfilter: add panorama filter

Michael Niedermayer michaelni at gmx.at
Sat Dec 5 20:45:55 CET 2015


On Sat, Dec 05, 2015 at 06:38:47PM +0100, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
>  libavfilter/Makefile      |   1 +
>  libavfilter/allfilters.c  |   1 +
>  libavfilter/vf_panorama.c | 437 ++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 439 insertions(+)
>  create mode 100644 libavfilter/vf_panorama.c
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 8884d1d..5e0321f 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -193,6 +193,7 @@ OBJS-$(CONFIG_OWDENOISE_FILTER)              += vf_owdenoise.o
>  OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
>  OBJS-$(CONFIG_PALETTEGEN_FILTER)             += vf_palettegen.o
>  OBJS-$(CONFIG_PALETTEUSE_FILTER)             += vf_paletteuse.o dualinput.o framesync.o
> +OBJS-$(CONFIG_PANORAMA_FILTER)               += vf_panorama.o
>  OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
>  OBJS-$(CONFIG_PERSPECTIVE_FILTER)            += vf_perspective.o
>  OBJS-$(CONFIG_PHASE_FILTER)                  += vf_phase.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 0eeef53..952bddf 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -214,6 +214,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER(PAD,            pad,            vf);
>      REGISTER_FILTER(PALETTEGEN,     palettegen,     vf);
>      REGISTER_FILTER(PALETTEUSE,     paletteuse,     vf);
> +    REGISTER_FILTER(PANORAMA,       panorama,       vf);
>      REGISTER_FILTER(PERMS,          perms,          vf);
>      REGISTER_FILTER(PERSPECTIVE,    perspective,    vf);
>      REGISTER_FILTER(PHASE,          phase,          vf);
> diff --git a/libavfilter/vf_panorama.c b/libavfilter/vf_panorama.c
> new file mode 100644
> index 0000000..b6bba64
> --- /dev/null
> +++ b/libavfilter/vf_panorama.c
> @@ -0,0 +1,437 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "libavutil/avassert.h"
> +#include "libavutil/eval.h"
> +#include "libavutil/imgutils.h"
> +#include "libavutil/pixdesc.h"
> +#include "libavutil/opt.h"
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +enum Projections {
> +    SPHERE,
> +    CUBE,
> +    NB_PROJECTIONS,
> +};
> +
> +enum Faces {
> +    LEFT,
> +    FRONT,
> +    RIGHT,
> +    TOP,
> +    BACK,
> +    DOWN,
> +};
> +
> +struct XYRemap {
> +    int vi, ui;
> +    int v2, u2;
> +    double a, b, c, d;
> +};
> +
> +typedef struct PanoramaContext {
> +    const AVClass *class;
> +    int in, out;
> +
> +    int planewidth[4], planeheight[4];
> +    int inplanewidth[4], inplaneheight[4];
> +    int nb_planes;
> +
> +    struct XYRemap *remap[4];
> +
> +    int (*panorama)(struct PanoramaContext *s,
> +                    const uint8_t *src, uint8_t *dst,
> +                    int width, int height,
> +                    int in_linesize, int out_linesize,
> +                    struct XYRemap *remap);
> +} PanoramaContext;
> +
> +#define OFFSET(x) offsetof(PanoramaContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +
> +static const AVOption panorama_options[] = {
> +    { "input",  "set input projection",   OFFSET(in), AV_OPT_TYPE_INT,   {.i64=SPHERE}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
> +    {     "s", "spheric",                          0, AV_OPT_TYPE_CONST, {.i64=SPHERE}, 0,                0, FLAGS, "in" },
> +    {     "c", "cubic",                            0, AV_OPT_TYPE_CONST, {.i64=CUBE},   0,                0, FLAGS, "in" },
> +    { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT,   {.i64=CUBE},   0, NB_PROJECTIONS-1, FLAGS, "out" },
> +    {     "s", "spheric",                          0, AV_OPT_TYPE_CONST, {.i64=SPHERE}, 0,                0, FLAGS, "out" },
> +    {     "c", "cubic",                            0, AV_OPT_TYPE_CONST, {.i64=CUBE},   0,                0, FLAGS, "out" },
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(panorama);
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    static const enum AVPixelFormat pix_fmts[] = {
> +        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
> +        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
> +        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
> +        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
> +    };
> +
> +    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
> +    if (!fmts_list)
> +        return AVERROR(ENOMEM);
> +    return ff_set_common_formats(ctx, fmts_list);
> +}
> +
> +static int bilinear(PanoramaContext *s,
> +                    const uint8_t *src, uint8_t *dst,
> +                    int width, int height,
> +                    int in_linesize, int out_linesize,
> +                    struct XYRemap *remap)
> +{
> +    double A, B, C, D;
> +    int x, y;
> +
> +    for (y = 0; y < height; y++) {
> +        uint8_t *d = dst + y * out_linesize;
> +        for (x = 0; x < width; x++) {
> +            struct XYRemap *r = &remap[y * width + x];
> +
> +            A = src[r->vi * in_linesize + r->ui];
> +            B = src[r->vi * in_linesize + r->u2];
> +            C = src[r->v2 * in_linesize + r->ui];
> +            D = src[r->v2 * in_linesize + r->u2];
> +            *d++ = round(A * r->a + B * r->b + C * r->c + D * r->d);
> +        }
> +    }
> +
> +    return 0;
> +}
> +
> +static int nearest(PanoramaContext *s,
> +                   const uint8_t *src, uint8_t *dst,
> +                   int width, int height,
> +                   int in_linesize, int out_linesize,
> +                   struct XYRemap *remap)
> +{
> +    double A;
> +    int x, y;
> +
> +    for (y = 0; y < height; y++) {
> +        uint8_t *d = dst + y * out_linesize;
> +        for (x = 0; x < width; x++) {
> +            struct XYRemap *r = &remap[y * width + x];
> +
> +            A = src[r->vi * in_linesize + r->ui];
> +            *d++ = A;
> +        }
> +    }
> +
> +    return 0;
> +}
> +
> +static void to_cube_xyz(int i, int j, int face, double edge, double *x, double *y, double *z)
> +{
> +    double a = 2.0 * i / edge;
> +    double b = 2.0 * j / edge;
> +
> +    if (face == BACK) {
> +        *x = -1.    ;
> +        *y =  9. - a;
> +        *z =  1. - b;
> +    } else if (face == LEFT) {
> +        *x =  a  - 1;
> +        *y = -1.    ;
> +        *z =  1. - b;
> +    } else if (face == FRONT) {
> +        *x =  1.    ;
> +        *y =  a  - 3;
> +        *z =  1. - b;
> +    } else if (face == RIGHT) {
> +        *x =  5. - a;
> +        *y =  1.    ;
> +        *z =  1. - b;
> +    } else if (face == TOP) {
> +        *x =  b  - 1;
> +        *y =  a  - 7;
> +        *z =  1.     ;
> +    } else if (face == DOWN) {
> +        *x = -b  + 1;
> +        *y =  a  -11;
> +        *z = -1.    ;
> +    }
> +}
> +
> +static void to_sphere_xyz(double theta, double phi, double *x, double *y, double *z)
> +{
> +    *x = cos(phi) * cos(theta);
> +    *y = sin(phi);
> +    *z = cos(phi) * sin(theta);
> +}
> +
> +static void locate(double axis, double x, double y, double rad, int radius, double *ox, double *oy)
> +{
> +    double ratio = radius / axis;
> +    double temp;
> +
> +    *ox = ratio * x;
> +    *oy = ratio * y;
> +
> +    temp = *ox;
> +    *ox = *ox * cos(rad) - *oy * sin(rad);
> +    *oy = temp * sin(rad) + *oy * cos(rad);
> +    *ox += radius;
> +    *oy += radius;
> +}
> +
> +static inline int equal(double a, double b, double epsilon)
> +{
> +    return fabs(a - b) < epsilon;
> +}
> +
> +static inline int smaller(double a, double b, double epsilon)
> +{
> +    return ((a - b) < 0.0) && (!equal(a, b, epsilon));
> +}
> +
> +static inline int in_range(double rd, double small, double large, double res)
> +{
> +   return    !smaller(rd, small, res)
> +          &&  smaller(rd, large, res);
> +}
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> +    AVFilterContext *ctx = outlink->src;
> +    AVFilterLink *inlink = ctx->inputs[0];
> +    PanoramaContext *s = ctx->priv;
> +    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
> +    int p, h, w;
> +
> +    if (s->in == SPHERE && s->out == CUBE) {
> +        w = inlink->w / 4 * 6;
> +        h = inlink->h / 2;
> +    } else if (s->in == CUBE && s->out == SPHERE) {
> +        w = inlink->w / 6 * 4;
> +        h = inlink->h * 2;
> +    } else if (s->in == s->out) {
> +        w = inlink->w;
> +        h = inlink->h;
> +    } else {
> +        av_assert0(0);
> +    }
> +
> +    s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
> +    s->planeheight[0] = s->planeheight[3] = h;
> +    s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
> +    s->planewidth[0] = s->planewidth[3] = w;
> +
> +    outlink->h = h;
> +    outlink->w = w;
> +
> +    s->inplaneheight[1] = s->inplaneheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
> +    s->inplaneheight[0] = s->inplaneheight[3] = inlink->h;
> +    s->inplanewidth[1]  = s->inplanewidth[2]  = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
> +    s->inplanewidth[0]  = s->inplanewidth[3]  = inlink->w;
> +    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
> +
> +    for (p = 0; p < s->nb_planes; p++) {
> +        s->remap[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof(struct XYRemap));
> +        if (!s->remap[p])
> +            return AVERROR(ENOMEM);
> +    }
> +
> +    if (s->in == SPHERE && s->out == CUBE) {
> +        for (p = 0; p < s->nb_planes; p++) {
> +            int face, ui, vi, u2, v2;
> +            double theta, R, phi, uf, vf, mu, nu, x, y, z;
> +            int edge = s->planewidth[p] / 6;
> +            int width = s->planewidth[p];
> +            int height = s->planeheight[p];
> +            int in_width = s->inplanewidth[p];
> +            int in_height = s->inplaneheight[p];
> +            int i, j;
> +
> +            for (i = 0; i < width; i++) {
> +                face = i / edge;
> +
> +                for (j = 0; j < height; j++) {
> +                    struct XYRemap *r = &s->remap[p][j * width + i];
> +
> +                    to_cube_xyz(i, j, face, edge, &x, &y, &z);
> +                    theta = atan2(y, x);
> +                    R = hypot(x , y);
> +                    phi = atan2(z , R);
> +                    uf = (2.0 * edge * (theta + M_PI) / M_PI);
> +                    vf = (2.0 * edge * (M_PI/2 - phi) / M_PI);
> +
> +                    ui = floor(uf);
> +                    vi = floor(vf);
> +                    u2 = ui + 1;
> +                    v2 = vi + 1;
> +                    mu = uf - ui;
> +                    nu = vf - vi;
> +                    r->vi = av_clip(vi, 0, in_height - 1);
> +                    r->ui = ui % in_width;
> +                    r->v2 = av_clip(v2, 0, in_height - 1);
> +                    r->u2 = u2 % in_width;
> +                    r->a = (1 - mu) * (1 - nu);
> +                    r->b =  mu * (1 - nu);
> +                    r->c = (1 - mu) * nu;
> +                    r->d = mu * nu;
> +                }
> +            }
> +        }
> +        s->panorama = bilinear;
> +    } else if (s->in == CUBE && s->out == SPHERE) {
> +        for (p = 0; p < s->nb_planes; p++) {
> +            double theta, theta_norm, phi, phi_threshold, x, y, z, ox, oy;
> +            int width = s->planewidth[p];
> +            int height = s->planeheight[p];
> +            int in_width = s->inplanewidth[p];
> +            double res = M_PI_4 / (in_width / 6) / 10.0;
> +            int radius = in_width / 12;
> +            int face, i, j;
> +
> +            for (i = 0; i < width; i++) {
> +                for (j = 0; j < height; j++) {
> +                    struct XYRemap *r = &s->remap[p][j * width + i];
> +
> +                    x = (2. * i) / width - 1.;
> +                    y = (2. * j) / height - 1.;
> +                    theta = x * M_PI;
> +                    phi   = y * M_PI_2;
> +                    to_sphere_xyz(theta, phi, &x, &y, &z);
> +
> +                    if (in_range(theta, -M_PI_4, M_PI_4, res)) {
> +                        face = FRONT;
> +                        theta_norm = theta;
> +                    } else if (in_range(theta, -(M_PI_2 + M_PI_4), -M_PI_4, res)) {
> +                        face = LEFT;
> +                        theta_norm = theta + M_PI_2;
> +                    } else if (in_range(theta, M_PI_4, M_PI_2 + M_PI_4, res)) {
> +                        face = RIGHT;
> +                        theta_norm = theta - M_PI_2;
> +                    } else {
> +                        face = BACK;
> +                        theta_norm = theta + ((theta > 0) ? -M_PI : M_PI);
> +                    }
> +
> +                    phi_threshold = atan2(1., 1. / cos(theta_norm));
> +                    if (phi > phi_threshold)
> +                        face = DOWN;
> +                    else if (phi < -phi_threshold)
> +                        face = TOP;

> +                    else
> +                        ;

gcc "complains" about this
libavfilter/vf_panorama.c: In function ‘config_output’:
libavfilter/vf_panorama.c:337:25: warning: suggest braces around empty body in an ‘else’ statement [-Wempty-body]


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The educated differ from the uneducated as much as the living from the
dead. -- Aristotle 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20151205/9ec3be3c/attachment.sig>


More information about the ffmpeg-devel mailing list