]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_v360.c
avfilter/vf_v360: improve fisheye input format
[ffmpeg] / libavfilter / vf_v360.c
1 /*
2  * Copyright (c) 2019 Eugene Lyapustin
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * 360 video conversion filter.
24  * Principle of operation:
25  *
26  * (for each pixel in output frame)
27  * 1) Calculate OpenGL-like coordinates (x, y, z) for pixel position (i, j)
28  * 2) Apply 360 operations (rotation, mirror) to (x, y, z)
29  * 3) Calculate pixel position (u, v) in input frame
30  * 4) Calculate interpolation window and weight for each pixel
31  *
32  * (for each frame)
33  * 5) Remap input frame to output frame using precalculated data
34  */
35
36 #include <math.h>
37
38 #include "libavutil/avassert.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/opt.h"
42 #include "avfilter.h"
43 #include "formats.h"
44 #include "internal.h"
45 #include "video.h"
46 #include "v360.h"
47
48 typedef struct ThreadData {
49     AVFrame *in;
50     AVFrame *out;
51 } ThreadData;
52
53 #define OFFSET(x) offsetof(V360Context, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
55
56 static const AVOption v360_options[] = {
57     {     "input", "set input projection",              OFFSET(in), AV_OPT_TYPE_INT,    {.i64=EQUIRECTANGULAR}, 0,    NB_PROJECTIONS-1, FLAGS, "in" },
58     {         "e", "equirectangular",                            0, AV_OPT_TYPE_CONST,  {.i64=EQUIRECTANGULAR}, 0,                   0, FLAGS, "in" },
59     {  "equirect", "equirectangular",                            0, AV_OPT_TYPE_CONST,  {.i64=EQUIRECTANGULAR}, 0,                   0, FLAGS, "in" },
60     {      "c3x2", "cubemap 3x2",                                0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_3_2},     0,                   0, FLAGS, "in" },
61     {      "c6x1", "cubemap 6x1",                                0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_6_1},     0,                   0, FLAGS, "in" },
62     {       "eac", "equi-angular cubemap",                       0, AV_OPT_TYPE_CONST,  {.i64=EQUIANGULAR},     0,                   0, FLAGS, "in" },
63     {  "dfisheye", "dual fisheye",                               0, AV_OPT_TYPE_CONST,  {.i64=DUAL_FISHEYE},    0,                   0, FLAGS, "in" },
64     {      "flat", "regular video",                              0, AV_OPT_TYPE_CONST,  {.i64=FLAT},            0,                   0, FLAGS, "in" },
65     {"rectilinear", "regular video",                             0, AV_OPT_TYPE_CONST,  {.i64=FLAT},            0,                   0, FLAGS, "in" },
66     {  "gnomonic", "regular video",                              0, AV_OPT_TYPE_CONST,  {.i64=FLAT},            0,                   0, FLAGS, "in" },
67     {    "barrel", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "in" },
68     {        "fb", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "in" },
69     {      "c1x6", "cubemap 1x6",                                0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_1_6},     0,                   0, FLAGS, "in" },
70     {        "sg", "stereographic",                              0, AV_OPT_TYPE_CONST,  {.i64=STEREOGRAPHIC},   0,                   0, FLAGS, "in" },
71     {  "mercator", "mercator",                                   0, AV_OPT_TYPE_CONST,  {.i64=MERCATOR},        0,                   0, FLAGS, "in" },
72     {      "ball", "ball",                                       0, AV_OPT_TYPE_CONST,  {.i64=BALL},            0,                   0, FLAGS, "in" },
73     {    "hammer", "hammer",                                     0, AV_OPT_TYPE_CONST,  {.i64=HAMMER},          0,                   0, FLAGS, "in" },
74     {"sinusoidal", "sinusoidal",                                 0, AV_OPT_TYPE_CONST,  {.i64=SINUSOIDAL},      0,                   0, FLAGS, "in" },
75     {   "fisheye", "fisheye",                                    0, AV_OPT_TYPE_CONST,  {.i64=FISHEYE},         0,                   0, FLAGS, "in" },
76     {"cylindrical", "cylindrical",                               0, AV_OPT_TYPE_CONST,  {.i64=CYLINDRICAL},     0,                   0, FLAGS, "in" },
77     {    "output", "set output projection",            OFFSET(out), AV_OPT_TYPE_INT,    {.i64=CUBEMAP_3_2},     0,    NB_PROJECTIONS-1, FLAGS, "out" },
78     {         "e", "equirectangular",                            0, AV_OPT_TYPE_CONST,  {.i64=EQUIRECTANGULAR}, 0,                   0, FLAGS, "out" },
79     {  "equirect", "equirectangular",                            0, AV_OPT_TYPE_CONST,  {.i64=EQUIRECTANGULAR}, 0,                   0, FLAGS, "out" },
80     {      "c3x2", "cubemap 3x2",                                0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_3_2},     0,                   0, FLAGS, "out" },
81     {      "c6x1", "cubemap 6x1",                                0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_6_1},     0,                   0, FLAGS, "out" },
82     {       "eac", "equi-angular cubemap",                       0, AV_OPT_TYPE_CONST,  {.i64=EQUIANGULAR},     0,                   0, FLAGS, "out" },
83     {  "dfisheye", "dual fisheye",                               0, AV_OPT_TYPE_CONST,  {.i64=DUAL_FISHEYE},    0,                   0, FLAGS, "out" },
84     {      "flat", "regular video",                              0, AV_OPT_TYPE_CONST,  {.i64=FLAT},            0,                   0, FLAGS, "out" },
85     {"rectilinear", "regular video",                             0, AV_OPT_TYPE_CONST,  {.i64=FLAT},            0,                   0, FLAGS, "out" },
86     {  "gnomonic", "regular video",                              0, AV_OPT_TYPE_CONST,  {.i64=FLAT},            0,                   0, FLAGS, "out" },
87     {    "barrel", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "out" },
88     {        "fb", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "out" },
89     {      "c1x6", "cubemap 1x6",                                0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_1_6},     0,                   0, FLAGS, "out" },
90     {        "sg", "stereographic",                              0, AV_OPT_TYPE_CONST,  {.i64=STEREOGRAPHIC},   0,                   0, FLAGS, "out" },
91     {  "mercator", "mercator",                                   0, AV_OPT_TYPE_CONST,  {.i64=MERCATOR},        0,                   0, FLAGS, "out" },
92     {      "ball", "ball",                                       0, AV_OPT_TYPE_CONST,  {.i64=BALL},            0,                   0, FLAGS, "out" },
93     {    "hammer", "hammer",                                     0, AV_OPT_TYPE_CONST,  {.i64=HAMMER},          0,                   0, FLAGS, "out" },
94     {"sinusoidal", "sinusoidal",                                 0, AV_OPT_TYPE_CONST,  {.i64=SINUSOIDAL},      0,                   0, FLAGS, "out" },
95     {   "fisheye", "fisheye",                                    0, AV_OPT_TYPE_CONST,  {.i64=FISHEYE},         0,                   0, FLAGS, "out" },
96     {   "pannini", "pannini",                                    0, AV_OPT_TYPE_CONST,  {.i64=PANNINI},         0,                   0, FLAGS, "out" },
97     {"cylindrical", "cylindrical",                               0, AV_OPT_TYPE_CONST,  {.i64=CYLINDRICAL},     0,                   0, FLAGS, "out" },
98     {"perspective", "perspective",                               0, AV_OPT_TYPE_CONST,  {.i64=PERSPECTIVE},     0,                   0, FLAGS, "out" },
99     {    "interp", "set interpolation method",      OFFSET(interp), AV_OPT_TYPE_INT,    {.i64=BILINEAR},        0, NB_INTERP_METHODS-1, FLAGS, "interp" },
100     {      "near", "nearest neighbour",                          0, AV_OPT_TYPE_CONST,  {.i64=NEAREST},         0,                   0, FLAGS, "interp" },
101     {   "nearest", "nearest neighbour",                          0, AV_OPT_TYPE_CONST,  {.i64=NEAREST},         0,                   0, FLAGS, "interp" },
102     {      "line", "bilinear interpolation",                     0, AV_OPT_TYPE_CONST,  {.i64=BILINEAR},        0,                   0, FLAGS, "interp" },
103     {    "linear", "bilinear interpolation",                     0, AV_OPT_TYPE_CONST,  {.i64=BILINEAR},        0,                   0, FLAGS, "interp" },
104     {      "cube", "bicubic interpolation",                      0, AV_OPT_TYPE_CONST,  {.i64=BICUBIC},         0,                   0, FLAGS, "interp" },
105     {     "cubic", "bicubic interpolation",                      0, AV_OPT_TYPE_CONST,  {.i64=BICUBIC},         0,                   0, FLAGS, "interp" },
106     {      "lanc", "lanczos interpolation",                      0, AV_OPT_TYPE_CONST,  {.i64=LANCZOS},         0,                   0, FLAGS, "interp" },
107     {   "lanczos", "lanczos interpolation",                      0, AV_OPT_TYPE_CONST,  {.i64=LANCZOS},         0,                   0, FLAGS, "interp" },
108     {      "sp16", "spline16 interpolation",                     0, AV_OPT_TYPE_CONST,  {.i64=SPLINE16},        0,                   0, FLAGS, "interp" },
109     {  "spline16", "spline16 interpolation",                     0, AV_OPT_TYPE_CONST,  {.i64=SPLINE16},        0,                   0, FLAGS, "interp" },
110     {     "gauss", "gaussian interpolation",                     0, AV_OPT_TYPE_CONST,  {.i64=GAUSSIAN},        0,                   0, FLAGS, "interp" },
111     {  "gaussian", "gaussian interpolation",                     0, AV_OPT_TYPE_CONST,  {.i64=GAUSSIAN},        0,                   0, FLAGS, "interp" },
112     {         "w", "output width",                   OFFSET(width), AV_OPT_TYPE_INT,    {.i64=0},               0,           INT16_MAX, FLAGS, "w"},
113     {         "h", "output height",                 OFFSET(height), AV_OPT_TYPE_INT,    {.i64=0},               0,           INT16_MAX, FLAGS, "h"},
114     { "in_stereo", "input stereo format",        OFFSET(in_stereo), AV_OPT_TYPE_INT,    {.i64=STEREO_2D},       0,    NB_STEREO_FMTS-1, FLAGS, "stereo" },
115     {"out_stereo", "output stereo format",      OFFSET(out_stereo), AV_OPT_TYPE_INT,    {.i64=STEREO_2D},       0,    NB_STEREO_FMTS-1, FLAGS, "stereo" },
116     {        "2d", "2d mono",                                    0, AV_OPT_TYPE_CONST,  {.i64=STEREO_2D},       0,                   0, FLAGS, "stereo" },
117     {       "sbs", "side by side",                               0, AV_OPT_TYPE_CONST,  {.i64=STEREO_SBS},      0,                   0, FLAGS, "stereo" },
118     {        "tb", "top bottom",                                 0, AV_OPT_TYPE_CONST,  {.i64=STEREO_TB},       0,                   0, FLAGS, "stereo" },
119     { "in_forder", "input cubemap face order",   OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"},        0,     NB_DIRECTIONS-1, FLAGS, "in_forder"},
120     {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"},        0,     NB_DIRECTIONS-1, FLAGS, "out_forder"},
121     {   "in_frot", "input cubemap face rotation",  OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"},        0,     NB_DIRECTIONS-1, FLAGS, "in_frot"},
122     {  "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"},        0,     NB_DIRECTIONS-1, FLAGS, "out_frot"},
123     {    "in_pad", "percent input cubemap pads",    OFFSET(in_pad), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},           0.f,                 1.f, FLAGS, "in_pad"},
124     {   "out_pad", "percent output cubemap pads",  OFFSET(out_pad), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},           0.f,                 1.f, FLAGS, "out_pad"},
125     {   "fin_pad", "fixed input cubemap pads",     OFFSET(fin_pad), AV_OPT_TYPE_INT,    {.i64=0},               0,                 100, FLAGS, "fin_pad"},
126     {  "fout_pad", "fixed output cubemap pads",   OFFSET(fout_pad), AV_OPT_TYPE_INT,    {.i64=0},               0,                 100, FLAGS, "fout_pad"},
127     {       "yaw", "yaw rotation",                     OFFSET(yaw), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},        -180.f,               180.f, FLAGS, "yaw"},
128     {     "pitch", "pitch rotation",                 OFFSET(pitch), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},        -180.f,               180.f, FLAGS, "pitch"},
129     {      "roll", "roll rotation",                   OFFSET(roll), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},        -180.f,               180.f, FLAGS, "roll"},
130     {    "rorder", "rotation order",                OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"},           0,                   0, FLAGS, "rorder"},
131     {     "h_fov", "horizontal field of view",       OFFSET(h_fov), AV_OPT_TYPE_FLOAT,  {.dbl=90.f},     0.00001f,               360.f, FLAGS, "h_fov"},
132     {     "v_fov", "vertical field of view",         OFFSET(v_fov), AV_OPT_TYPE_FLOAT,  {.dbl=45.f},     0.00001f,               360.f, FLAGS, "v_fov"},
133     {     "d_fov", "diagonal field of view",         OFFSET(d_fov), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},           0.f,               360.f, FLAGS, "d_fov"},
134     {    "h_flip", "flip out video horizontally",   OFFSET(h_flip), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "h_flip"},
135     {    "v_flip", "flip out video vertically",     OFFSET(v_flip), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "v_flip"},
136     {    "d_flip", "flip out video indepth",        OFFSET(d_flip), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "d_flip"},
137     {   "ih_flip", "flip in video horizontally",   OFFSET(ih_flip), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "ih_flip"},
138     {   "iv_flip", "flip in video vertically",     OFFSET(iv_flip), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "iv_flip"},
139     {  "in_trans", "transpose video input",   OFFSET(in_transpose), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "in_transpose"},
140     { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "out_transpose"},
141     {    "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT,  {.dbl=90.f},     0.00001f,               360.f, FLAGS, "ih_fov"},
142     {    "iv_fov", "input vertical field of view",  OFFSET(iv_fov), AV_OPT_TYPE_FLOAT,  {.dbl=45.f},     0.00001f,               360.f, FLAGS, "iv_fov"},
143     {    "id_fov", "input diagonal field of view",  OFFSET(id_fov), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},           0.f,               360.f, FLAGS, "id_fov"},
144     { NULL }
145 };
146
147 AVFILTER_DEFINE_CLASS(v360);
148
149 static int query_formats(AVFilterContext *ctx)
150 {
151     static const enum AVPixelFormat pix_fmts[] = {
152         // YUVA444
153         AV_PIX_FMT_YUVA444P,   AV_PIX_FMT_YUVA444P9,
154         AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
155         AV_PIX_FMT_YUVA444P16,
156
157         // YUVA422
158         AV_PIX_FMT_YUVA422P,   AV_PIX_FMT_YUVA422P9,
159         AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
160         AV_PIX_FMT_YUVA422P16,
161
162         // YUVA420
163         AV_PIX_FMT_YUVA420P,   AV_PIX_FMT_YUVA420P9,
164         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
165
166         // YUVJ
167         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
168         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
169         AV_PIX_FMT_YUVJ411P,
170
171         // YUV444
172         AV_PIX_FMT_YUV444P,   AV_PIX_FMT_YUV444P9,
173         AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
174         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
175
176         // YUV440
177         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
178         AV_PIX_FMT_YUV440P12,
179
180         // YUV422
181         AV_PIX_FMT_YUV422P,   AV_PIX_FMT_YUV422P9,
182         AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
183         AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
184
185         // YUV420
186         AV_PIX_FMT_YUV420P,   AV_PIX_FMT_YUV420P9,
187         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
188         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
189
190         // YUV411
191         AV_PIX_FMT_YUV411P,
192
193         // YUV410
194         AV_PIX_FMT_YUV410P,
195
196         // GBR
197         AV_PIX_FMT_GBRP,   AV_PIX_FMT_GBRP9,
198         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
199         AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
200
201         // GBRA
202         AV_PIX_FMT_GBRAP,   AV_PIX_FMT_GBRAP10,
203         AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
204
205         // GRAY
206         AV_PIX_FMT_GRAY8,  AV_PIX_FMT_GRAY9,
207         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
208         AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
209
210         AV_PIX_FMT_NONE
211     };
212
213     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
214     if (!fmts_list)
215         return AVERROR(ENOMEM);
216     return ff_set_common_formats(ctx, fmts_list);
217 }
218
219 #define DEFINE_REMAP1_LINE(bits, div)                                                    \
220 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
221                                       ptrdiff_t in_linesize,                             \
222                                       const int16_t *const u, const int16_t *const v,    \
223                                       const int16_t *const ker)                          \
224 {                                                                                        \
225     const uint##bits##_t *const s = (const uint##bits##_t *const)src;                    \
226     uint##bits##_t *d = (uint##bits##_t *)dst;                                           \
227                                                                                          \
228     in_linesize /= div;                                                                  \
229                                                                                          \
230     for (int x = 0; x < width; x++)                                                      \
231         d[x] = s[v[x] * in_linesize + u[x]];                                             \
232 }
233
234 DEFINE_REMAP1_LINE( 8, 1)
235 DEFINE_REMAP1_LINE(16, 2)
236
237 /**
238  * Generate remapping function with a given window size and pixel depth.
239  *
240  * @param ws size of interpolation window
241  * @param bits number of bits per pixel
242  */
243 #define DEFINE_REMAP(ws, bits)                                                                             \
244 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)          \
245 {                                                                                                          \
246     ThreadData *td = arg;                                                                                  \
247     const V360Context *s = ctx->priv;                                                                      \
248     const AVFrame *in = td->in;                                                                            \
249     AVFrame *out = td->out;                                                                                \
250                                                                                                            \
251     for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) {                               \
252         for (int plane = 0; plane < s->nb_planes; plane++) {                                               \
253             const unsigned map = s->map[plane];                                                            \
254             const int in_linesize  = in->linesize[plane];                                                  \
255             const int out_linesize = out->linesize[plane];                                                 \
256             const int uv_linesize = s->uv_linesize[plane];                                                 \
257             const int in_offset_w = stereo ? s->in_offset_w[plane] : 0;                                    \
258             const int in_offset_h = stereo ? s->in_offset_h[plane] : 0;                                    \
259             const int out_offset_w = stereo ? s->out_offset_w[plane] : 0;                                  \
260             const int out_offset_h = stereo ? s->out_offset_h[plane] : 0;                                  \
261             const uint8_t *const src = in->data[plane] +                                                   \
262                                                    in_offset_h * in_linesize + in_offset_w * (bits >> 3);  \
263             uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3);    \
264             const int width = s->pr_width[plane];                                                          \
265             const int height = s->pr_height[plane];                                                        \
266                                                                                                            \
267             const int slice_start = (height *  jobnr     ) / nb_jobs;                                      \
268             const int slice_end   = (height * (jobnr + 1)) / nb_jobs;                                      \
269                                                                                                            \
270             for (int y = slice_start; y < slice_end; y++) {                                                \
271                 const int16_t *const u = s->u[map] + y * uv_linesize * ws * ws;                            \
272                 const int16_t *const v = s->v[map] + y * uv_linesize * ws * ws;                            \
273                 const int16_t *const ker = s->ker[map] + y * uv_linesize * ws * ws;                        \
274                                                                                                            \
275                 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker);                 \
276             }                                                                                              \
277         }                                                                                                  \
278     }                                                                                                      \
279                                                                                                            \
280     return 0;                                                                                              \
281 }
282
283 DEFINE_REMAP(1,  8)
284 DEFINE_REMAP(2,  8)
285 DEFINE_REMAP(4,  8)
286 DEFINE_REMAP(1, 16)
287 DEFINE_REMAP(2, 16)
288 DEFINE_REMAP(4, 16)
289
290 #define DEFINE_REMAP_LINE(ws, bits, div)                                                      \
291 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
292                                            ptrdiff_t in_linesize,                             \
293                                            const int16_t *const u, const int16_t *const v,    \
294                                            const int16_t *const ker)                          \
295 {                                                                                             \
296     const uint##bits##_t *const s = (const uint##bits##_t *const)src;                         \
297     uint##bits##_t *d = (uint##bits##_t *)dst;                                                \
298                                                                                               \
299     in_linesize /= div;                                                                       \
300                                                                                               \
301     for (int x = 0; x < width; x++) {                                                         \
302         const int16_t *const uu = u + x * ws * ws;                                            \
303         const int16_t *const vv = v + x * ws * ws;                                            \
304         const int16_t *const kker = ker + x * ws * ws;                                        \
305         int tmp = 0;                                                                          \
306                                                                                               \
307         for (int i = 0; i < ws; i++) {                                                        \
308             for (int j = 0; j < ws; j++) {                                                    \
309                 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]];   \
310             }                                                                                 \
311         }                                                                                     \
312                                                                                               \
313         d[x] = av_clip_uint##bits(tmp >> 14);                                                 \
314     }                                                                                         \
315 }
316
317 DEFINE_REMAP_LINE(2,  8, 1)
318 DEFINE_REMAP_LINE(4,  8, 1)
319 DEFINE_REMAP_LINE(2, 16, 2)
320 DEFINE_REMAP_LINE(4, 16, 2)
321
322 void ff_v360_init(V360Context *s, int depth)
323 {
324     switch (s->interp) {
325     case NEAREST:
326         s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
327         break;
328     case BILINEAR:
329         s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
330         break;
331     case BICUBIC:
332     case LANCZOS:
333     case SPLINE16:
334     case GAUSSIAN:
335         s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
336         break;
337     }
338
339     if (ARCH_X86)
340         ff_v360_init_x86(s, depth);
341 }
342
343 /**
344  * Save nearest pixel coordinates for remapping.
345  *
346  * @param du horizontal relative coordinate
347  * @param dv vertical relative coordinate
348  * @param rmap calculated 4x4 window
349  * @param u u remap data
350  * @param v v remap data
351  * @param ker ker remap data
352  */
353 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
354                            int16_t *u, int16_t *v, int16_t *ker)
355 {
356     const int i = lrintf(dv) + 1;
357     const int j = lrintf(du) + 1;
358
359     u[0] = rmap->u[i][j];
360     v[0] = rmap->v[i][j];
361 }
362
363 /**
364  * Calculate kernel for bilinear interpolation.
365  *
366  * @param du horizontal relative coordinate
367  * @param dv vertical relative coordinate
368  * @param rmap calculated 4x4 window
369  * @param u u remap data
370  * @param v v remap data
371  * @param ker ker remap data
372  */
373 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
374                             int16_t *u, int16_t *v, int16_t *ker)
375 {
376     for (int i = 0; i < 2; i++) {
377         for (int j = 0; j < 2; j++) {
378             u[i * 2 + j] = rmap->u[i + 1][j + 1];
379             v[i * 2 + j] = rmap->v[i + 1][j + 1];
380         }
381     }
382
383     ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
384     ker[1] = lrintf(       du  * (1.f - dv) * 16385.f);
385     ker[2] = lrintf((1.f - du) *        dv  * 16385.f);
386     ker[3] = lrintf(       du  *        dv  * 16385.f);
387 }
388
389 /**
390  * Calculate 1-dimensional cubic coefficients.
391  *
392  * @param t relative coordinate
393  * @param coeffs coefficients
394  */
395 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
396 {
397     const float tt  = t * t;
398     const float ttt = t * t * t;
399
400     coeffs[0] =     - t / 3.f + tt / 2.f - ttt / 6.f;
401     coeffs[1] = 1.f - t / 2.f - tt       + ttt / 2.f;
402     coeffs[2] =       t       + tt / 2.f - ttt / 2.f;
403     coeffs[3] =     - t / 6.f            + ttt / 6.f;
404 }
405
406 /**
407  * Calculate kernel for bicubic interpolation.
408  *
409  * @param du horizontal relative coordinate
410  * @param dv vertical relative coordinate
411  * @param rmap calculated 4x4 window
412  * @param u u remap data
413  * @param v v remap data
414  * @param ker ker remap data
415  */
416 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
417                            int16_t *u, int16_t *v, int16_t *ker)
418 {
419     float du_coeffs[4];
420     float dv_coeffs[4];
421
422     calculate_bicubic_coeffs(du, du_coeffs);
423     calculate_bicubic_coeffs(dv, dv_coeffs);
424
425     for (int i = 0; i < 4; i++) {
426         for (int j = 0; j < 4; j++) {
427             u[i * 4 + j] = rmap->u[i][j];
428             v[i * 4 + j] = rmap->v[i][j];
429             ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
430         }
431     }
432 }
433
434 /**
435  * Calculate 1-dimensional lanczos coefficients.
436  *
437  * @param t relative coordinate
438  * @param coeffs coefficients
439  */
440 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
441 {
442     float sum = 0.f;
443
444     for (int i = 0; i < 4; i++) {
445         const float x = M_PI * (t - i + 1);
446         if (x == 0.f) {
447             coeffs[i] = 1.f;
448         } else {
449             coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
450         }
451         sum += coeffs[i];
452     }
453
454     for (int i = 0; i < 4; i++) {
455         coeffs[i] /= sum;
456     }
457 }
458
459 /**
460  * Calculate kernel for lanczos interpolation.
461  *
462  * @param du horizontal relative coordinate
463  * @param dv vertical relative coordinate
464  * @param rmap calculated 4x4 window
465  * @param u u remap data
466  * @param v v remap data
467  * @param ker ker remap data
468  */
469 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
470                            int16_t *u, int16_t *v, int16_t *ker)
471 {
472     float du_coeffs[4];
473     float dv_coeffs[4];
474
475     calculate_lanczos_coeffs(du, du_coeffs);
476     calculate_lanczos_coeffs(dv, dv_coeffs);
477
478     for (int i = 0; i < 4; i++) {
479         for (int j = 0; j < 4; j++) {
480             u[i * 4 + j] = rmap->u[i][j];
481             v[i * 4 + j] = rmap->v[i][j];
482             ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
483         }
484     }
485 }
486
487 /**
488  * Calculate 1-dimensional spline16 coefficients.
489  *
490  * @param t relative coordinate
491  * @param coeffs coefficients
492  */
493 static void calculate_spline16_coeffs(float t, float *coeffs)
494 {
495     coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
496     coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
497     coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
498     coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
499 }
500
501 /**
502  * Calculate kernel for spline16 interpolation.
503  *
504  * @param du horizontal relative coordinate
505  * @param dv vertical relative coordinate
506  * @param rmap calculated 4x4 window
507  * @param u u remap data
508  * @param v v remap data
509  * @param ker ker remap data
510  */
511 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
512                             int16_t *u, int16_t *v, int16_t *ker)
513 {
514     float du_coeffs[4];
515     float dv_coeffs[4];
516
517     calculate_spline16_coeffs(du, du_coeffs);
518     calculate_spline16_coeffs(dv, dv_coeffs);
519
520     for (int i = 0; i < 4; i++) {
521         for (int j = 0; j < 4; j++) {
522             u[i * 4 + j] = rmap->u[i][j];
523             v[i * 4 + j] = rmap->v[i][j];
524             ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
525         }
526     }
527 }
528
529 /**
530  * Calculate 1-dimensional gaussian coefficients.
531  *
532  * @param t relative coordinate
533  * @param coeffs coefficients
534  */
535 static void calculate_gaussian_coeffs(float t, float *coeffs)
536 {
537     float sum = 0.f;
538
539     for (int i = 0; i < 4; i++) {
540         const float x = t - (i - 1);
541         if (x == 0.f) {
542             coeffs[i] = 1.f;
543         } else {
544             coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
545         }
546         sum += coeffs[i];
547     }
548
549     for (int i = 0; i < 4; i++) {
550         coeffs[i] /= sum;
551     }
552 }
553
554 /**
555  * Calculate kernel for gaussian interpolation.
556  *
557  * @param du horizontal relative coordinate
558  * @param dv vertical relative coordinate
559  * @param rmap calculated 4x4 window
560  * @param u u remap data
561  * @param v v remap data
562  * @param ker ker remap data
563  */
564 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
565                             int16_t *u, int16_t *v, int16_t *ker)
566 {
567     float du_coeffs[4];
568     float dv_coeffs[4];
569
570     calculate_gaussian_coeffs(du, du_coeffs);
571     calculate_gaussian_coeffs(dv, dv_coeffs);
572
573     for (int i = 0; i < 4; i++) {
574         for (int j = 0; j < 4; j++) {
575             u[i * 4 + j] = rmap->u[i][j];
576             v[i * 4 + j] = rmap->v[i][j];
577             ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
578         }
579     }
580 }
581
582 /**
583  * Modulo operation with only positive remainders.
584  *
585  * @param a dividend
586  * @param b divisor
587  *
588  * @return positive remainder of (a / b)
589  */
590 static inline int mod(int a, int b)
591 {
592     const int res = a % b;
593     if (res < 0) {
594         return res + b;
595     } else {
596         return res;
597     }
598 }
599
600 /**
601  * Convert char to corresponding direction.
602  * Used for cubemap options.
603  */
604 static int get_direction(char c)
605 {
606     switch (c) {
607     case 'r':
608         return RIGHT;
609     case 'l':
610         return LEFT;
611     case 'u':
612         return UP;
613     case 'd':
614         return DOWN;
615     case 'f':
616         return FRONT;
617     case 'b':
618         return BACK;
619     default:
620         return -1;
621     }
622 }
623
624 /**
625  * Convert char to corresponding rotation angle.
626  * Used for cubemap options.
627  */
628 static int get_rotation(char c)
629 {
630     switch (c) {
631     case '0':
632         return ROT_0;
633     case '1':
634         return ROT_90;
635     case '2':
636         return ROT_180;
637     case '3':
638         return ROT_270;
639     default:
640         return -1;
641     }
642 }
643
644 /**
645  * Convert char to corresponding rotation order.
646  */
647 static int get_rorder(char c)
648 {
649     switch (c) {
650     case 'Y':
651     case 'y':
652         return YAW;
653     case 'P':
654     case 'p':
655         return PITCH;
656     case 'R':
657     case 'r':
658         return ROLL;
659     default:
660         return -1;
661     }
662 }
663
664 /**
665  * Prepare data for processing cubemap input format.
666  *
667  * @param ctx filter context
668  *
669  * @return error code
670  */
671 static int prepare_cube_in(AVFilterContext *ctx)
672 {
673     V360Context *s = ctx->priv;
674
675     for (int face = 0; face < NB_FACES; face++) {
676         const char c = s->in_forder[face];
677         int direction;
678
679         if (c == '\0') {
680             av_log(ctx, AV_LOG_ERROR,
681                    "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
682             return AVERROR(EINVAL);
683         }
684
685         direction = get_direction(c);
686         if (direction == -1) {
687             av_log(ctx, AV_LOG_ERROR,
688                    "Incorrect direction symbol '%c' in in_forder option.\n", c);
689             return AVERROR(EINVAL);
690         }
691
692         s->in_cubemap_face_order[direction] = face;
693     }
694
695     for (int face = 0; face < NB_FACES; face++) {
696         const char c = s->in_frot[face];
697         int rotation;
698
699         if (c == '\0') {
700             av_log(ctx, AV_LOG_ERROR,
701                    "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
702             return AVERROR(EINVAL);
703         }
704
705         rotation = get_rotation(c);
706         if (rotation == -1) {
707             av_log(ctx, AV_LOG_ERROR,
708                    "Incorrect rotation symbol '%c' in in_frot option.\n", c);
709             return AVERROR(EINVAL);
710         }
711
712         s->in_cubemap_face_rotation[face] = rotation;
713     }
714
715     return 0;
716 }
717
718 /**
719  * Prepare data for processing cubemap output format.
720  *
721  * @param ctx filter context
722  *
723  * @return error code
724  */
725 static int prepare_cube_out(AVFilterContext *ctx)
726 {
727     V360Context *s = ctx->priv;
728
729     for (int face = 0; face < NB_FACES; face++) {
730         const char c = s->out_forder[face];
731         int direction;
732
733         if (c == '\0') {
734             av_log(ctx, AV_LOG_ERROR,
735                    "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
736             return AVERROR(EINVAL);
737         }
738
739         direction = get_direction(c);
740         if (direction == -1) {
741             av_log(ctx, AV_LOG_ERROR,
742                    "Incorrect direction symbol '%c' in out_forder option.\n", c);
743             return AVERROR(EINVAL);
744         }
745
746         s->out_cubemap_direction_order[face] = direction;
747     }
748
749     for (int face = 0; face < NB_FACES; face++) {
750         const char c = s->out_frot[face];
751         int rotation;
752
753         if (c == '\0') {
754             av_log(ctx, AV_LOG_ERROR,
755                    "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
756             return AVERROR(EINVAL);
757         }
758
759         rotation = get_rotation(c);
760         if (rotation == -1) {
761             av_log(ctx, AV_LOG_ERROR,
762                    "Incorrect rotation symbol '%c' in out_frot option.\n", c);
763             return AVERROR(EINVAL);
764         }
765
766         s->out_cubemap_face_rotation[face] = rotation;
767     }
768
769     return 0;
770 }
771
772 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
773 {
774     float tmp;
775
776     switch (rotation) {
777     case ROT_0:
778         break;
779     case ROT_90:
780         tmp =  *uf;
781         *uf = -*vf;
782         *vf =  tmp;
783         break;
784     case ROT_180:
785         *uf = -*uf;
786         *vf = -*vf;
787         break;
788     case ROT_270:
789         tmp = -*uf;
790         *uf =  *vf;
791         *vf =  tmp;
792         break;
793     default:
794         av_assert0(0);
795     }
796 }
797
798 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
799 {
800     float tmp;
801
802     switch (rotation) {
803     case ROT_0:
804         break;
805     case ROT_90:
806         tmp = -*uf;
807         *uf =  *vf;
808         *vf =  tmp;
809         break;
810     case ROT_180:
811         *uf = -*uf;
812         *vf = -*vf;
813         break;
814     case ROT_270:
815         tmp =  *uf;
816         *uf = -*vf;
817         *vf =  tmp;
818         break;
819     default:
820         av_assert0(0);
821     }
822 }
823
824 /**
825  * Normalize vector.
826  *
827  * @param vec vector
828  */
829 static void normalize_vector(float *vec)
830 {
831     const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
832
833     vec[0] /= norm;
834     vec[1] /= norm;
835     vec[2] /= norm;
836 }
837
838 /**
839  * Calculate 3D coordinates on sphere for corresponding cubemap position.
840  * Common operation for every cubemap.
841  *
842  * @param s filter private context
843  * @param uf horizontal cubemap coordinate [0, 1)
844  * @param vf vertical cubemap coordinate [0, 1)
845  * @param face face of cubemap
846  * @param vec coordinates on sphere
847  * @param scalew scale for uf
848  * @param scaleh scale for vf
849  */
850 static void cube_to_xyz(const V360Context *s,
851                         float uf, float vf, int face,
852                         float *vec, float scalew, float scaleh)
853 {
854     const int direction = s->out_cubemap_direction_order[face];
855     float l_x, l_y, l_z;
856
857     uf /= scalew;
858     vf /= scaleh;
859
860     rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
861
862     switch (direction) {
863     case RIGHT:
864         l_x =  1.f;
865         l_y = -vf;
866         l_z =  uf;
867         break;
868     case LEFT:
869         l_x = -1.f;
870         l_y = -vf;
871         l_z = -uf;
872         break;
873     case UP:
874         l_x =  uf;
875         l_y =  1.f;
876         l_z = -vf;
877         break;
878     case DOWN:
879         l_x =  uf;
880         l_y = -1.f;
881         l_z =  vf;
882         break;
883     case FRONT:
884         l_x =  uf;
885         l_y = -vf;
886         l_z = -1.f;
887         break;
888     case BACK:
889         l_x = -uf;
890         l_y = -vf;
891         l_z =  1.f;
892         break;
893     default:
894         av_assert0(0);
895     }
896
897     vec[0] = l_x;
898     vec[1] = l_y;
899     vec[2] = l_z;
900
901     normalize_vector(vec);
902 }
903
904 /**
905  * Calculate cubemap position for corresponding 3D coordinates on sphere.
906  * Common operation for every cubemap.
907  *
908  * @param s filter private context
909  * @param vec coordinated on sphere
910  * @param uf horizontal cubemap coordinate [0, 1)
911  * @param vf vertical cubemap coordinate [0, 1)
912  * @param direction direction of view
913  */
914 static void xyz_to_cube(const V360Context *s,
915                         const float *vec,
916                         float *uf, float *vf, int *direction)
917 {
918     const float phi   = atan2f(vec[0], -vec[2]);
919     const float theta = asinf(-vec[1]);
920     float phi_norm, theta_threshold;
921     int face;
922
923     if (phi >= -M_PI_4 && phi < M_PI_4) {
924         *direction = FRONT;
925         phi_norm = phi;
926     } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
927         *direction = LEFT;
928         phi_norm = phi + M_PI_2;
929     } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
930         *direction = RIGHT;
931         phi_norm = phi - M_PI_2;
932     } else {
933         *direction = BACK;
934         phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
935     }
936
937     theta_threshold = atanf(cosf(phi_norm));
938     if (theta > theta_threshold) {
939         *direction = DOWN;
940     } else if (theta < -theta_threshold) {
941         *direction = UP;
942     }
943
944     switch (*direction) {
945     case RIGHT:
946         *uf =  vec[2] / vec[0];
947         *vf = -vec[1] / vec[0];
948         break;
949     case LEFT:
950         *uf =  vec[2] / vec[0];
951         *vf =  vec[1] / vec[0];
952         break;
953     case UP:
954         *uf =  vec[0] / vec[1];
955         *vf = -vec[2] / vec[1];
956         break;
957     case DOWN:
958         *uf = -vec[0] / vec[1];
959         *vf = -vec[2] / vec[1];
960         break;
961     case FRONT:
962         *uf = -vec[0] / vec[2];
963         *vf =  vec[1] / vec[2];
964         break;
965     case BACK:
966         *uf = -vec[0] / vec[2];
967         *vf = -vec[1] / vec[2];
968         break;
969     default:
970         av_assert0(0);
971     }
972
973     face = s->in_cubemap_face_order[*direction];
974     rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
975
976     (*uf) *= s->input_mirror_modifier[0];
977     (*vf) *= s->input_mirror_modifier[1];
978 }
979
980 /**
981  * Find position on another cube face in case of overflow/underflow.
982  * Used for calculation of interpolation window.
983  *
984  * @param s filter private context
985  * @param uf horizontal cubemap coordinate
986  * @param vf vertical cubemap coordinate
987  * @param direction direction of view
988  * @param new_uf new horizontal cubemap coordinate
989  * @param new_vf new vertical cubemap coordinate
990  * @param face face position on cubemap
991  */
992 static void process_cube_coordinates(const V360Context *s,
993                                      float uf, float vf, int direction,
994                                      float *new_uf, float *new_vf, int *face)
995 {
996     /*
997      *  Cubemap orientation
998      *
999      *           width
1000      *         <------->
1001      *         +-------+
1002      *         |       |                              U
1003      *         | up    |                   h       ------->
1004      * +-------+-------+-------+-------+ ^ e      |
1005      * |       |       |       |       | | i    V |
1006      * | left  | front | right | back  | | g      |
1007      * +-------+-------+-------+-------+ v h      v
1008      *         |       |                   t
1009      *         | down  |
1010      *         +-------+
1011      */
1012
1013     *face = s->in_cubemap_face_order[direction];
1014     rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1015
1016     if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1017         // There are no pixels to use in this case
1018         *new_uf = uf;
1019         *new_vf = vf;
1020     } else if (uf < -1.f) {
1021         uf += 2.f;
1022         switch (direction) {
1023         case RIGHT:
1024             direction = FRONT;
1025             *new_uf =  uf;
1026             *new_vf =  vf;
1027             break;
1028         case LEFT:
1029             direction = BACK;
1030             *new_uf =  uf;
1031             *new_vf =  vf;
1032             break;
1033         case UP:
1034             direction = LEFT;
1035             *new_uf =  vf;
1036             *new_vf = -uf;
1037             break;
1038         case DOWN:
1039             direction = LEFT;
1040             *new_uf = -vf;
1041             *new_vf =  uf;
1042             break;
1043         case FRONT:
1044             direction = LEFT;
1045             *new_uf =  uf;
1046             *new_vf =  vf;
1047             break;
1048         case BACK:
1049             direction = RIGHT;
1050             *new_uf =  uf;
1051             *new_vf =  vf;
1052             break;
1053         default:
1054             av_assert0(0);
1055         }
1056     } else if (uf >= 1.f) {
1057         uf -= 2.f;
1058         switch (direction) {
1059         case RIGHT:
1060             direction = BACK;
1061             *new_uf =  uf;
1062             *new_vf =  vf;
1063             break;
1064         case LEFT:
1065             direction = FRONT;
1066             *new_uf =  uf;
1067             *new_vf =  vf;
1068             break;
1069         case UP:
1070             direction = RIGHT;
1071             *new_uf = -vf;
1072             *new_vf =  uf;
1073             break;
1074         case DOWN:
1075             direction = RIGHT;
1076             *new_uf =  vf;
1077             *new_vf = -uf;
1078             break;
1079         case FRONT:
1080             direction = RIGHT;
1081             *new_uf =  uf;
1082             *new_vf =  vf;
1083             break;
1084         case BACK:
1085             direction = LEFT;
1086             *new_uf =  uf;
1087             *new_vf =  vf;
1088             break;
1089         default:
1090             av_assert0(0);
1091         }
1092     } else if (vf < -1.f) {
1093         vf += 2.f;
1094         switch (direction) {
1095         case RIGHT:
1096             direction = UP;
1097             *new_uf =  vf;
1098             *new_vf = -uf;
1099             break;
1100         case LEFT:
1101             direction = UP;
1102             *new_uf = -vf;
1103             *new_vf =  uf;
1104             break;
1105         case UP:
1106             direction = BACK;
1107             *new_uf = -uf;
1108             *new_vf = -vf;
1109             break;
1110         case DOWN:
1111             direction = FRONT;
1112             *new_uf =  uf;
1113             *new_vf =  vf;
1114             break;
1115         case FRONT:
1116             direction = UP;
1117             *new_uf =  uf;
1118             *new_vf =  vf;
1119             break;
1120         case BACK:
1121             direction = UP;
1122             *new_uf = -uf;
1123             *new_vf = -vf;
1124             break;
1125         default:
1126             av_assert0(0);
1127         }
1128     } else if (vf >= 1.f) {
1129         vf -= 2.f;
1130         switch (direction) {
1131         case RIGHT:
1132             direction = DOWN;
1133             *new_uf = -vf;
1134             *new_vf =  uf;
1135             break;
1136         case LEFT:
1137             direction = DOWN;
1138             *new_uf =  vf;
1139             *new_vf = -uf;
1140             break;
1141         case UP:
1142             direction = FRONT;
1143             *new_uf =  uf;
1144             *new_vf =  vf;
1145             break;
1146         case DOWN:
1147             direction = BACK;
1148             *new_uf = -uf;
1149             *new_vf = -vf;
1150             break;
1151         case FRONT:
1152             direction = DOWN;
1153             *new_uf =  uf;
1154             *new_vf =  vf;
1155             break;
1156         case BACK:
1157             direction = DOWN;
1158             *new_uf = -uf;
1159             *new_vf = -vf;
1160             break;
1161         default:
1162             av_assert0(0);
1163         }
1164     } else {
1165         // Inside cube face
1166         *new_uf = uf;
1167         *new_vf = vf;
1168     }
1169
1170     *face = s->in_cubemap_face_order[direction];
1171     rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1172 }
1173
1174 /**
1175  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1176  *
1177  * @param s filter private context
1178  * @param i horizontal position on frame [0, width)
1179  * @param j vertical position on frame [0, height)
1180  * @param width frame width
1181  * @param height frame height
1182  * @param vec coordinates on sphere
1183  */
1184 static void cube3x2_to_xyz(const V360Context *s,
1185                            int i, int j, int width, int height,
1186                            float *vec)
1187 {
1188     const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width  / 3.f) : 1.f - s->out_pad;
1189     const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1190
1191     const float ew = width  / 3.f;
1192     const float eh = height / 2.f;
1193
1194     const int u_face = floorf(i / ew);
1195     const int v_face = floorf(j / eh);
1196     const int face = u_face + 3 * v_face;
1197
1198     const int u_shift = ceilf(ew * u_face);
1199     const int v_shift = ceilf(eh * v_face);
1200     const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1201     const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1202
1203     const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1204     const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1205
1206     cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1207 }
1208
1209 /**
1210  * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1211  *
1212  * @param s filter private context
1213  * @param vec coordinates on sphere
1214  * @param width frame width
1215  * @param height frame height
1216  * @param us horizontal coordinates for interpolation window
1217  * @param vs vertical coordinates for interpolation window
1218  * @param du horizontal relative coordinate
1219  * @param dv vertical relative coordinate
1220  */
1221 static void xyz_to_cube3x2(const V360Context *s,
1222                            const float *vec, int width, int height,
1223                            int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1224 {
1225     const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width  / 3.f) : 1.f - s->in_pad;
1226     const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1227     const float ew = width  / 3.f;
1228     const float eh = height / 2.f;
1229     float uf, vf;
1230     int ui, vi;
1231     int ewi, ehi;
1232     int direction, face;
1233     int u_face, v_face;
1234
1235     xyz_to_cube(s, vec, &uf, &vf, &direction);
1236
1237     uf *= scalew;
1238     vf *= scaleh;
1239
1240     face = s->in_cubemap_face_order[direction];
1241     u_face = face % 3;
1242     v_face = face / 3;
1243     ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1244     ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1245
1246     uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1247     vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1248
1249     ui = floorf(uf);
1250     vi = floorf(vf);
1251
1252     *du = uf - ui;
1253     *dv = vf - vi;
1254
1255     for (int i = -1; i < 3; i++) {
1256         for (int j = -1; j < 3; j++) {
1257             int new_ui = ui + j;
1258             int new_vi = vi + i;
1259             int u_shift, v_shift;
1260             int new_ewi, new_ehi;
1261
1262             if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1263                 face = s->in_cubemap_face_order[direction];
1264
1265                 u_face = face % 3;
1266                 v_face = face / 3;
1267                 u_shift = ceilf(ew * u_face);
1268                 v_shift = ceilf(eh * v_face);
1269             } else {
1270                 uf = 2.f * new_ui / ewi - 1.f;
1271                 vf = 2.f * new_vi / ehi - 1.f;
1272
1273                 uf /= scalew;
1274                 vf /= scaleh;
1275
1276                 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1277
1278                 uf *= scalew;
1279                 vf *= scaleh;
1280
1281                 u_face = face % 3;
1282                 v_face = face / 3;
1283                 u_shift = ceilf(ew * u_face);
1284                 v_shift = ceilf(eh * v_face);
1285                 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1286                 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1287
1288                 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1289                 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1290             }
1291
1292             us[i + 1][j + 1] = u_shift + new_ui;
1293             vs[i + 1][j + 1] = v_shift + new_vi;
1294         }
1295     }
1296 }
1297
1298 /**
1299  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1300  *
1301  * @param s filter private context
1302  * @param i horizontal position on frame [0, width)
1303  * @param j vertical position on frame [0, height)
1304  * @param width frame width
1305  * @param height frame height
1306  * @param vec coordinates on sphere
1307  */
1308 static void cube1x6_to_xyz(const V360Context *s,
1309                            int i, int j, int width, int height,
1310                            float *vec)
1311 {
1312     const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1313     const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1314
1315     const float ew = width;
1316     const float eh = height / 6.f;
1317
1318     const int face = floorf(j / eh);
1319
1320     const int v_shift = ceilf(eh * face);
1321     const int ehi = ceilf(eh * (face + 1)) - v_shift;
1322
1323     const float uf = 2.f * (i           + 0.5f) / ew  - 1.f;
1324     const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1325
1326     cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1327 }
1328
1329 /**
1330  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1331  *
1332  * @param s filter private context
1333  * @param i horizontal position on frame [0, width)
1334  * @param j vertical position on frame [0, height)
1335  * @param width frame width
1336  * @param height frame height
1337  * @param vec coordinates on sphere
1338  */
1339 static void cube6x1_to_xyz(const V360Context *s,
1340                            int i, int j, int width, int height,
1341                            float *vec)
1342 {
1343     const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f)   : 1.f - s->out_pad;
1344     const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1345
1346     const float ew = width / 6.f;
1347     const float eh = height;
1348
1349     const int face = floorf(i / ew);
1350
1351     const int u_shift = ceilf(ew * face);
1352     const int ewi = ceilf(ew * (face + 1)) - u_shift;
1353
1354     const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1355     const float vf = 2.f * (j           + 0.5f) / eh  - 1.f;
1356
1357     cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1358 }
1359
1360 /**
1361  * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1362  *
1363  * @param s filter private context
1364  * @param vec coordinates on sphere
1365  * @param width frame width
1366  * @param height frame height
1367  * @param us horizontal coordinates for interpolation window
1368  * @param vs vertical coordinates for interpolation window
1369  * @param du horizontal relative coordinate
1370  * @param dv vertical relative coordinate
1371  */
1372 static void xyz_to_cube1x6(const V360Context *s,
1373                            const float *vec, int width, int height,
1374                            int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1375 {
1376     const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1377     const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1378     const float eh = height / 6.f;
1379     const int ewi = width;
1380     float uf, vf;
1381     int ui, vi;
1382     int ehi;
1383     int direction, face;
1384
1385     xyz_to_cube(s, vec, &uf, &vf, &direction);
1386
1387     uf *= scalew;
1388     vf *= scaleh;
1389
1390     face = s->in_cubemap_face_order[direction];
1391     ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1392
1393     uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1394     vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1395
1396     ui = floorf(uf);
1397     vi = floorf(vf);
1398
1399     *du = uf - ui;
1400     *dv = vf - vi;
1401
1402     for (int i = -1; i < 3; i++) {
1403         for (int j = -1; j < 3; j++) {
1404             int new_ui = ui + j;
1405             int new_vi = vi + i;
1406             int v_shift;
1407             int new_ehi;
1408
1409             if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1410                 face = s->in_cubemap_face_order[direction];
1411
1412                 v_shift = ceilf(eh * face);
1413             } else {
1414                 uf = 2.f * new_ui / ewi - 1.f;
1415                 vf = 2.f * new_vi / ehi - 1.f;
1416
1417                 uf /= scalew;
1418                 vf /= scaleh;
1419
1420                 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1421
1422                 uf *= scalew;
1423                 vf *= scaleh;
1424
1425                 v_shift = ceilf(eh * face);
1426                 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1427
1428                 new_ui = av_clip(lrintf(0.5f *     ewi * (uf + 1.f)), 0,     ewi - 1);
1429                 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1430             }
1431
1432             us[i + 1][j + 1] =           new_ui;
1433             vs[i + 1][j + 1] = v_shift + new_vi;
1434         }
1435     }
1436 }
1437
1438 /**
1439  * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1440  *
1441  * @param s filter private context
1442  * @param vec coordinates on sphere
1443  * @param width frame width
1444  * @param height frame height
1445  * @param us horizontal coordinates for interpolation window
1446  * @param vs vertical coordinates for interpolation window
1447  * @param du horizontal relative coordinate
1448  * @param dv vertical relative coordinate
1449  */
1450 static void xyz_to_cube6x1(const V360Context *s,
1451                            const float *vec, int width, int height,
1452                            int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1453 {
1454     const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f)   : 1.f - s->in_pad;
1455     const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1456     const float ew = width / 6.f;
1457     const int ehi = height;
1458     float uf, vf;
1459     int ui, vi;
1460     int ewi;
1461     int direction, face;
1462
1463     xyz_to_cube(s, vec, &uf, &vf, &direction);
1464
1465     uf *= scalew;
1466     vf *= scaleh;
1467
1468     face = s->in_cubemap_face_order[direction];
1469     ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1470
1471     uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1472     vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1473
1474     ui = floorf(uf);
1475     vi = floorf(vf);
1476
1477     *du = uf - ui;
1478     *dv = vf - vi;
1479
1480     for (int i = -1; i < 3; i++) {
1481         for (int j = -1; j < 3; j++) {
1482             int new_ui = ui + j;
1483             int new_vi = vi + i;
1484             int u_shift;
1485             int new_ewi;
1486
1487             if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1488                 face = s->in_cubemap_face_order[direction];
1489
1490                 u_shift = ceilf(ew * face);
1491             } else {
1492                 uf = 2.f * new_ui / ewi - 1.f;
1493                 vf = 2.f * new_vi / ehi - 1.f;
1494
1495                 uf /= scalew;
1496                 vf /= scaleh;
1497
1498                 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1499
1500                 uf *= scalew;
1501                 vf *= scaleh;
1502
1503                 u_shift = ceilf(ew * face);
1504                 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1505
1506                 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1507                 new_vi = av_clip(lrintf(0.5f *     ehi * (vf + 1.f)), 0,     ehi - 1);
1508             }
1509
1510             us[i + 1][j + 1] = u_shift + new_ui;
1511             vs[i + 1][j + 1] =           new_vi;
1512         }
1513     }
1514 }
1515
1516 /**
1517  * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1518  *
1519  * @param s filter private context
1520  * @param i horizontal position on frame [0, width)
1521  * @param j vertical position on frame [0, height)
1522  * @param width frame width
1523  * @param height frame height
1524  * @param vec coordinates on sphere
1525  */
1526 static void equirect_to_xyz(const V360Context *s,
1527                             int i, int j, int width, int height,
1528                             float *vec)
1529 {
1530     const float phi   = ((2.f * i) / width  - 1.f) * M_PI;
1531     const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1532
1533     const float sin_phi   = sinf(phi);
1534     const float cos_phi   = cosf(phi);
1535     const float sin_theta = sinf(theta);
1536     const float cos_theta = cosf(theta);
1537
1538     vec[0] =  cos_theta * sin_phi;
1539     vec[1] = -sin_theta;
1540     vec[2] = -cos_theta * cos_phi;
1541 }
1542
1543 /**
1544  * Prepare data for processing stereographic output format.
1545  *
1546  * @param ctx filter context
1547  *
1548  * @return error code
1549  */
1550 static int prepare_stereographic_out(AVFilterContext *ctx)
1551 {
1552     V360Context *s = ctx->priv;
1553
1554     s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1555     s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1556
1557     return 0;
1558 }
1559
1560 /**
1561  * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1562  *
1563  * @param s filter private context
1564  * @param i horizontal position on frame [0, width)
1565  * @param j vertical position on frame [0, height)
1566  * @param width frame width
1567  * @param height frame height
1568  * @param vec coordinates on sphere
1569  */
1570 static void stereographic_to_xyz(const V360Context *s,
1571                                  int i, int j, int width, int height,
1572                                  float *vec)
1573 {
1574     const float x = ((2.f * i) / width  - 1.f) * s->flat_range[0];
1575     const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1576     const float xy = x * x + y * y;
1577
1578     vec[0] = 2.f * x / (1.f + xy);
1579     vec[1] = (-1.f + xy) / (1.f + xy);
1580     vec[2] = 2.f * y / (1.f + xy);
1581
1582     normalize_vector(vec);
1583 }
1584
1585 /**
1586  * Prepare data for processing stereographic input format.
1587  *
1588  * @param ctx filter context
1589  *
1590  * @return error code
1591  */
1592 static int prepare_stereographic_in(AVFilterContext *ctx)
1593 {
1594     V360Context *s = ctx->priv;
1595
1596     s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1597     s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1598
1599     return 0;
1600 }
1601
1602 /**
1603  * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1604  *
1605  * @param s filter private context
1606  * @param vec coordinates on sphere
1607  * @param width frame width
1608  * @param height frame height
1609  * @param us horizontal coordinates for interpolation window
1610  * @param vs vertical coordinates for interpolation window
1611  * @param du horizontal relative coordinate
1612  * @param dv vertical relative coordinate
1613  */
1614 static void xyz_to_stereographic(const V360Context *s,
1615                                  const float *vec, int width, int height,
1616                                  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1617 {
1618     const float x = vec[0] / (1.f - vec[1]) / s->iflat_range[0] * s->input_mirror_modifier[0];
1619     const float y = vec[2] / (1.f - vec[1]) / s->iflat_range[1] * s->input_mirror_modifier[1];
1620     float uf, vf;
1621     int visible, ui, vi;
1622
1623     uf = (x + 1.f) * width  / 2.f;
1624     vf = (y + 1.f) * height / 2.f;
1625     ui = floorf(uf);
1626     vi = floorf(vf);
1627
1628     visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1629
1630     *du = visible ? uf - ui : 0.f;
1631     *dv = visible ? vf - vi : 0.f;
1632
1633     for (int i = -1; i < 3; i++) {
1634         for (int j = -1; j < 3; j++) {
1635             us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width  - 1) : 0;
1636             vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1637         }
1638     }
1639 }
1640
1641 /**
1642  * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1643  *
1644  * @param s filter private context
1645  * @param vec coordinates on sphere
1646  * @param width frame width
1647  * @param height frame height
1648  * @param us horizontal coordinates for interpolation window
1649  * @param vs vertical coordinates for interpolation window
1650  * @param du horizontal relative coordinate
1651  * @param dv vertical relative coordinate
1652  */
1653 static void xyz_to_equirect(const V360Context *s,
1654                             const float *vec, int width, int height,
1655                             int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1656 {
1657     const float phi   = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1658     const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1659     float uf, vf;
1660     int ui, vi;
1661
1662     uf = (phi   / M_PI   + 1.f) * width  / 2.f;
1663     vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1664     ui = floorf(uf);
1665     vi = floorf(vf);
1666
1667     *du = uf - ui;
1668     *dv = vf - vi;
1669
1670     for (int i = -1; i < 3; i++) {
1671         for (int j = -1; j < 3; j++) {
1672             us[i + 1][j + 1] = mod(ui + j, width);
1673             vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1674         }
1675     }
1676 }
1677
1678 /**
1679  * Prepare data for processing flat input format.
1680  *
1681  * @param ctx filter context
1682  *
1683  * @return error code
1684  */
1685 static int prepare_flat_in(AVFilterContext *ctx)
1686 {
1687     V360Context *s = ctx->priv;
1688
1689     s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
1690     s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
1691
1692     return 0;
1693 }
1694
1695 /**
1696  * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
1697  *
1698  * @param s filter private context
1699  * @param vec coordinates on sphere
1700  * @param width frame width
1701  * @param height frame height
1702  * @param us horizontal coordinates for interpolation window
1703  * @param vs vertical coordinates for interpolation window
1704  * @param du horizontal relative coordinate
1705  * @param dv vertical relative coordinate
1706  */
1707 static void xyz_to_flat(const V360Context *s,
1708                         const float *vec, int width, int height,
1709                         int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1710 {
1711     const float theta = acosf(vec[2]);
1712     const float r = tanf(theta);
1713     const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
1714     const float zf = -vec[2];
1715     const float h = hypotf(vec[0], vec[1]);
1716     const float c = h <= 1e-6f ? 1.f : rr / h;
1717     float uf = -vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1718     float vf =  vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1719     int visible, ui, vi;
1720
1721     uf = zf >= 0.f ? (uf + 1.f) * width  / 2.f : 0.f;
1722     vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
1723
1724     ui = floorf(uf);
1725     vi = floorf(vf);
1726
1727     visible = vi >= 0 && vi < height && ui >= 0 && ui < width;
1728
1729     *du = uf - ui;
1730     *dv = vf - vi;
1731
1732     for (int i = -1; i < 3; i++) {
1733         for (int j = -1; j < 3; j++) {
1734             us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width  - 1) : 0;
1735             vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1736         }
1737     }
1738 }
1739
1740 /**
1741  * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1742  *
1743  * @param s filter private context
1744  * @param vec coordinates on sphere
1745  * @param width frame width
1746  * @param height frame height
1747  * @param us horizontal coordinates for interpolation window
1748  * @param vs vertical coordinates for interpolation window
1749  * @param du horizontal relative coordinate
1750  * @param dv vertical relative coordinate
1751  */
1752 static void xyz_to_mercator(const V360Context *s,
1753                             const float *vec, int width, int height,
1754                             int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1755 {
1756     const float phi   = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1757     const float theta = -vec[1] * s->input_mirror_modifier[1];
1758     float uf, vf;
1759     int ui, vi;
1760
1761     uf = (phi / M_PI + 1.f) * width / 2.f;
1762     vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1763     ui = floorf(uf);
1764     vi = floorf(vf);
1765
1766     *du = uf - ui;
1767     *dv = vf - vi;
1768
1769     for (int i = -1; i < 3; i++) {
1770         for (int j = -1; j < 3; j++) {
1771             us[i + 1][j + 1] = av_clip(ui + j, 0, width  - 1);
1772             vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1773         }
1774     }
1775 }
1776
1777 /**
1778  * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1779  *
1780  * @param s filter private context
1781  * @param i horizontal position on frame [0, width)
1782  * @param j vertical position on frame [0, height)
1783  * @param width frame width
1784  * @param height frame height
1785  * @param vec coordinates on sphere
1786  */
1787 static void mercator_to_xyz(const V360Context *s,
1788                             int i, int j, int width, int height,
1789                             float *vec)
1790 {
1791     const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1792     const float y   = ((2.f * j) / height - 1.f) * M_PI;
1793     const float div = expf(2.f * y) + 1.f;
1794
1795     const float sin_phi   = sinf(phi);
1796     const float cos_phi   = cosf(phi);
1797     const float sin_theta = -2.f * expf(y) / div;
1798     const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1799
1800     vec[0] = sin_theta * cos_phi;
1801     vec[1] = cos_theta;
1802     vec[2] = sin_theta * sin_phi;
1803 }
1804
1805 /**
1806  * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1807  *
1808  * @param s filter private context
1809  * @param vec coordinates on sphere
1810  * @param width frame width
1811  * @param height frame height
1812  * @param us horizontal coordinates for interpolation window
1813  * @param vs vertical coordinates for interpolation window
1814  * @param du horizontal relative coordinate
1815  * @param dv vertical relative coordinate
1816  */
1817 static void xyz_to_ball(const V360Context *s,
1818                         const float *vec, int width, int height,
1819                         int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1820 {
1821     const float l = hypotf(vec[0], vec[1]);
1822     const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1823     float uf, vf;
1824     int ui, vi;
1825
1826     uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width  * 0.5f;
1827     vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1828
1829     ui = floorf(uf);
1830     vi = floorf(vf);
1831
1832     *du = uf - ui;
1833     *dv = vf - vi;
1834
1835     for (int i = -1; i < 3; i++) {
1836         for (int j = -1; j < 3; j++) {
1837             us[i + 1][j + 1] = av_clip(ui + j, 0, width  - 1);
1838             vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1839         }
1840     }
1841 }
1842
1843 /**
1844  * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1845  *
1846  * @param s filter private context
1847  * @param i horizontal position on frame [0, width)
1848  * @param j vertical position on frame [0, height)
1849  * @param width frame width
1850  * @param height frame height
1851  * @param vec coordinates on sphere
1852  */
1853 static void ball_to_xyz(const V360Context *s,
1854                         int i, int j, int width, int height,
1855                         float *vec)
1856 {
1857     const float x = (2.f * i) / width  - 1.f;
1858     const float y = (2.f * j) / height - 1.f;
1859     const float l = hypotf(x, y);
1860
1861     if (l <= 1.f) {
1862         const float z = 2.f * l * sqrtf(1.f - l * l);
1863
1864         vec[0] =  z * x / (l > 0.f ? l : 1.f);
1865         vec[1] = -z * y / (l > 0.f ? l : 1.f);
1866         vec[2] = -1.f + 2.f * l * l;
1867     } else {
1868         vec[0] =  0.f;
1869         vec[1] = -1.f;
1870         vec[2] =  0.f;
1871     }
1872 }
1873
1874 /**
1875  * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1876  *
1877  * @param s filter private context
1878  * @param i horizontal position on frame [0, width)
1879  * @param j vertical position on frame [0, height)
1880  * @param width frame width
1881  * @param height frame height
1882  * @param vec coordinates on sphere
1883  */
1884 static void hammer_to_xyz(const V360Context *s,
1885                           int i, int j, int width, int height,
1886                           float *vec)
1887 {
1888     const float x = ((2.f * i) / width  - 1.f);
1889     const float y = ((2.f * j) / height - 1.f);
1890
1891     const float xx = x * x;
1892     const float yy = y * y;
1893
1894     const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1895
1896     const float a = M_SQRT2 * x * z;
1897     const float b = 2.f * z * z - 1.f;
1898
1899     const float aa = a * a;
1900     const float bb = b * b;
1901
1902     const float w = sqrtf(1.f - 2.f * yy * z * z);
1903
1904     vec[0] =  w * 2.f * a * b / (aa + bb);
1905     vec[1] = -M_SQRT2 * y * z;
1906     vec[2] = -w * (bb  - aa) / (aa + bb);
1907
1908     normalize_vector(vec);
1909 }
1910
1911 /**
1912  * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1913  *
1914  * @param s filter private context
1915  * @param vec coordinates on sphere
1916  * @param width frame width
1917  * @param height frame height
1918  * @param us horizontal coordinates for interpolation window
1919  * @param vs vertical coordinates for interpolation window
1920  * @param du horizontal relative coordinate
1921  * @param dv vertical relative coordinate
1922  */
1923 static void xyz_to_hammer(const V360Context *s,
1924                           const float *vec, int width, int height,
1925                           int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1926 {
1927     const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1928
1929     const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1930     const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1931     const float y = -vec[1] / z * s->input_mirror_modifier[1];
1932     float uf, vf;
1933     int ui, vi;
1934
1935     uf = (x + 1.f) * width  / 2.f;
1936     vf = (y + 1.f) * height / 2.f;
1937     ui = floorf(uf);
1938     vi = floorf(vf);
1939
1940     *du = uf - ui;
1941     *dv = vf - vi;
1942
1943     for (int i = -1; i < 3; i++) {
1944         for (int j = -1; j < 3; j++) {
1945             us[i + 1][j + 1] = av_clip(ui + j, 0, width  - 1);
1946             vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1947         }
1948     }
1949 }
1950
1951 /**
1952  * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
1953  *
1954  * @param s filter private context
1955  * @param i horizontal position on frame [0, width)
1956  * @param j vertical position on frame [0, height)
1957  * @param width frame width
1958  * @param height frame height
1959  * @param vec coordinates on sphere
1960  */
1961 static void sinusoidal_to_xyz(const V360Context *s,
1962                               int i, int j, int width, int height,
1963                               float *vec)
1964 {
1965     const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1966     const float phi   = ((2.f * i) / width  - 1.f) * M_PI / cosf(theta);
1967
1968     const float sin_phi   = sinf(phi);
1969     const float cos_phi   = cosf(phi);
1970     const float sin_theta = sinf(theta);
1971     const float cos_theta = cosf(theta);
1972
1973     vec[0] =  cos_theta * sin_phi;
1974     vec[1] = -sin_theta;
1975     vec[2] = -cos_theta * cos_phi;
1976
1977     normalize_vector(vec);
1978 }
1979
1980 /**
1981  * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
1982  *
1983  * @param s filter private context
1984  * @param vec coordinates on sphere
1985  * @param width frame width
1986  * @param height frame height
1987  * @param us horizontal coordinates for interpolation window
1988  * @param vs vertical coordinates for interpolation window
1989  * @param du horizontal relative coordinate
1990  * @param dv vertical relative coordinate
1991  */
1992 static void xyz_to_sinusoidal(const V360Context *s,
1993                               const float *vec, int width, int height,
1994                               int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1995 {
1996     const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1997     const float phi   = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
1998     float uf, vf;
1999     int ui, vi;
2000
2001     uf = (phi   / M_PI   + 1.f) * width  / 2.f;
2002     vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2003     ui = floorf(uf);
2004     vi = floorf(vf);
2005
2006     *du = uf - ui;
2007     *dv = vf - vi;
2008
2009     for (int i = -1; i < 3; i++) {
2010         for (int j = -1; j < 3; j++) {
2011             us[i + 1][j + 1] = av_clip(ui + j, 0, width  - 1);
2012             vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2013         }
2014     }
2015 }
2016
2017 /**
2018  * Prepare data for processing equi-angular cubemap input format.
2019  *
2020  * @param ctx filter context
2021  *
2022  * @return error code
2023  */
2024 static int prepare_eac_in(AVFilterContext *ctx)
2025 {
2026     V360Context *s = ctx->priv;
2027
2028     if (s->ih_flip && s->iv_flip) {
2029         s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2030         s->in_cubemap_face_order[LEFT]  = BOTTOM_RIGHT;
2031         s->in_cubemap_face_order[UP]    = TOP_LEFT;
2032         s->in_cubemap_face_order[DOWN]  = TOP_RIGHT;
2033         s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2034         s->in_cubemap_face_order[BACK]  = TOP_MIDDLE;
2035     } else if (s->ih_flip) {
2036         s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2037         s->in_cubemap_face_order[LEFT]  = TOP_RIGHT;
2038         s->in_cubemap_face_order[UP]    = BOTTOM_LEFT;
2039         s->in_cubemap_face_order[DOWN]  = BOTTOM_RIGHT;
2040         s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2041         s->in_cubemap_face_order[BACK]  = BOTTOM_MIDDLE;
2042     } else if (s->iv_flip) {
2043         s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2044         s->in_cubemap_face_order[LEFT]  = BOTTOM_LEFT;
2045         s->in_cubemap_face_order[UP]    = TOP_RIGHT;
2046         s->in_cubemap_face_order[DOWN]  = TOP_LEFT;
2047         s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2048         s->in_cubemap_face_order[BACK]  = TOP_MIDDLE;
2049     } else {
2050         s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2051         s->in_cubemap_face_order[LEFT]  = TOP_LEFT;
2052         s->in_cubemap_face_order[UP]    = BOTTOM_RIGHT;
2053         s->in_cubemap_face_order[DOWN]  = BOTTOM_LEFT;
2054         s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2055         s->in_cubemap_face_order[BACK]  = BOTTOM_MIDDLE;
2056     }
2057
2058     if (s->iv_flip) {
2059         s->in_cubemap_face_rotation[TOP_LEFT]      = ROT_270;
2060         s->in_cubemap_face_rotation[TOP_MIDDLE]    = ROT_90;
2061         s->in_cubemap_face_rotation[TOP_RIGHT]     = ROT_270;
2062         s->in_cubemap_face_rotation[BOTTOM_LEFT]   = ROT_0;
2063         s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2064         s->in_cubemap_face_rotation[BOTTOM_RIGHT]  = ROT_0;
2065     } else {
2066         s->in_cubemap_face_rotation[TOP_LEFT]      = ROT_0;
2067         s->in_cubemap_face_rotation[TOP_MIDDLE]    = ROT_0;
2068         s->in_cubemap_face_rotation[TOP_RIGHT]     = ROT_0;
2069         s->in_cubemap_face_rotation[BOTTOM_LEFT]   = ROT_270;
2070         s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2071         s->in_cubemap_face_rotation[BOTTOM_RIGHT]  = ROT_270;
2072     }
2073
2074     return 0;
2075 }
2076
2077 /**
2078  * Prepare data for processing equi-angular cubemap output format.
2079  *
2080  * @param ctx filter context
2081  *
2082  * @return error code
2083  */
2084 static int prepare_eac_out(AVFilterContext *ctx)
2085 {
2086     V360Context *s = ctx->priv;
2087
2088     s->out_cubemap_direction_order[TOP_LEFT]      = LEFT;
2089     s->out_cubemap_direction_order[TOP_MIDDLE]    = FRONT;
2090     s->out_cubemap_direction_order[TOP_RIGHT]     = RIGHT;
2091     s->out_cubemap_direction_order[BOTTOM_LEFT]   = DOWN;
2092     s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2093     s->out_cubemap_direction_order[BOTTOM_RIGHT]  = UP;
2094
2095     s->out_cubemap_face_rotation[TOP_LEFT]      = ROT_0;
2096     s->out_cubemap_face_rotation[TOP_MIDDLE]    = ROT_0;
2097     s->out_cubemap_face_rotation[TOP_RIGHT]     = ROT_0;
2098     s->out_cubemap_face_rotation[BOTTOM_LEFT]   = ROT_270;
2099     s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2100     s->out_cubemap_face_rotation[BOTTOM_RIGHT]  = ROT_270;
2101
2102     return 0;
2103 }
2104
2105 /**
2106  * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2107  *
2108  * @param s filter private context
2109  * @param i horizontal position on frame [0, width)
2110  * @param j vertical position on frame [0, height)
2111  * @param width frame width
2112  * @param height frame height
2113  * @param vec coordinates on sphere
2114  */
2115 static void eac_to_xyz(const V360Context *s,
2116                        int i, int j, int width, int height,
2117                        float *vec)
2118 {
2119     const float pixel_pad = 2;
2120     const float u_pad = pixel_pad / width;
2121     const float v_pad = pixel_pad / height;
2122
2123     int u_face, v_face, face;
2124
2125     float l_x, l_y, l_z;
2126
2127     float uf = (i + 0.5f) / width;
2128     float vf = (j + 0.5f) / height;
2129
2130     // EAC has 2-pixel padding on faces except between faces on the same row
2131     // Padding pixels seems not to be stretched with tangent as regular pixels
2132     // Formulas below approximate original padding as close as I could get experimentally
2133
2134     // Horizontal padding
2135     uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2136     if (uf < 0.f) {
2137         u_face = 0;
2138         uf -= 0.5f;
2139     } else if (uf >= 3.f) {
2140         u_face = 2;
2141         uf -= 2.5f;
2142     } else {
2143         u_face = floorf(uf);
2144         uf = fmodf(uf, 1.f) - 0.5f;
2145     }
2146
2147     // Vertical padding
2148     v_face = floorf(vf * 2.f);
2149     vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2150
2151     if (uf >= -0.5f && uf < 0.5f) {
2152         uf = tanf(M_PI_2 * uf);
2153     } else {
2154         uf = 2.f * uf;
2155     }
2156     if (vf >= -0.5f && vf < 0.5f) {
2157         vf = tanf(M_PI_2 * vf);
2158     } else {
2159         vf = 2.f * vf;
2160     }
2161
2162     face = u_face + 3 * v_face;
2163
2164     switch (face) {
2165     case TOP_LEFT:
2166         l_x = -1.f;
2167         l_y = -vf;
2168         l_z = -uf;
2169         break;
2170     case TOP_MIDDLE:
2171         l_x =  uf;
2172         l_y = -vf;
2173         l_z = -1.f;
2174         break;
2175     case TOP_RIGHT:
2176         l_x =  1.f;
2177         l_y = -vf;
2178         l_z =  uf;
2179         break;
2180     case BOTTOM_LEFT:
2181         l_x = -vf;
2182         l_y = -1.f;
2183         l_z =  uf;
2184         break;
2185     case BOTTOM_MIDDLE:
2186         l_x = -vf;
2187         l_y =  uf;
2188         l_z =  1.f;
2189         break;
2190     case BOTTOM_RIGHT:
2191         l_x = -vf;
2192         l_y =  1.f;
2193         l_z = -uf;
2194         break;
2195     default:
2196         av_assert0(0);
2197     }
2198
2199     vec[0] = l_x;
2200     vec[1] = l_y;
2201     vec[2] = l_z;
2202
2203     normalize_vector(vec);
2204 }
2205
2206 /**
2207  * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2208  *
2209  * @param s filter private context
2210  * @param vec coordinates on sphere
2211  * @param width frame width
2212  * @param height frame height
2213  * @param us horizontal coordinates for interpolation window
2214  * @param vs vertical coordinates for interpolation window
2215  * @param du horizontal relative coordinate
2216  * @param dv vertical relative coordinate
2217  */
2218 static void xyz_to_eac(const V360Context *s,
2219                        const float *vec, int width, int height,
2220                        int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2221 {
2222     const float pixel_pad = 2;
2223     const float u_pad = pixel_pad / width;
2224     const float v_pad = pixel_pad / height;
2225
2226     float uf, vf;
2227     int ui, vi;
2228     int direction, face;
2229     int u_face, v_face;
2230
2231     xyz_to_cube(s, vec, &uf, &vf, &direction);
2232
2233     face = s->in_cubemap_face_order[direction];
2234     u_face = face % 3;
2235     v_face = face / 3;
2236
2237     uf = M_2_PI * atanf(uf) + 0.5f;
2238     vf = M_2_PI * atanf(vf) + 0.5f;
2239
2240     // These formulas are inversed from eac_to_xyz ones
2241     uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2242     vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2243
2244     uf *= width;
2245     vf *= height;
2246
2247     uf -= 0.5f;
2248     vf -= 0.5f;
2249
2250     ui = floorf(uf);
2251     vi = floorf(vf);
2252
2253     *du = uf - ui;
2254     *dv = vf - vi;
2255
2256     for (int i = -1; i < 3; i++) {
2257         for (int j = -1; j < 3; j++) {
2258             us[i + 1][j + 1] = av_clip(ui + j, 0, width  - 1);
2259             vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2260         }
2261     }
2262 }
2263
2264 /**
2265  * Prepare data for processing flat output format.
2266  *
2267  * @param ctx filter context
2268  *
2269  * @return error code
2270  */
2271 static int prepare_flat_out(AVFilterContext *ctx)
2272 {
2273     V360Context *s = ctx->priv;
2274
2275     s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2276     s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2277
2278     return 0;
2279 }
2280
2281 /**
2282  * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2283  *
2284  * @param s filter private context
2285  * @param i horizontal position on frame [0, width)
2286  * @param j vertical position on frame [0, height)
2287  * @param width frame width
2288  * @param height frame height
2289  * @param vec coordinates on sphere
2290  */
2291 static void flat_to_xyz(const V360Context *s,
2292                         int i, int j, int width, int height,
2293                         float *vec)
2294 {
2295     const float l_x =  s->flat_range[0] * (2.f * i / width  - 1.f);
2296     const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2297
2298     vec[0] =  l_x;
2299     vec[1] =  l_y;
2300     vec[2] = -1.f;
2301
2302     normalize_vector(vec);
2303 }
2304
2305 /**
2306  * Prepare data for processing fisheye output format.
2307  *
2308  * @param ctx filter context
2309  *
2310  * @return error code
2311  */
2312 static int prepare_fisheye_out(AVFilterContext *ctx)
2313 {
2314     V360Context *s = ctx->priv;
2315
2316     s->flat_range[0] = s->h_fov / 180.f;
2317     s->flat_range[1] = s->v_fov / 180.f;
2318
2319     return 0;
2320 }
2321
2322 /**
2323  * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2324  *
2325  * @param s filter private context
2326  * @param i horizontal position on frame [0, width)
2327  * @param j vertical position on frame [0, height)
2328  * @param width frame width
2329  * @param height frame height
2330  * @param vec coordinates on sphere
2331  */
2332 static void fisheye_to_xyz(const V360Context *s,
2333                            int i, int j, int width, int height,
2334                            float *vec)
2335 {
2336     const float uf = s->flat_range[0] * ((2.f * i) / width  - 1.f);
2337     const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2338
2339     const float phi   = -atan2f(vf, uf);
2340     const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2341
2342     vec[0] = cosf(theta) * cosf(phi);
2343     vec[1] = cosf(theta) * sinf(phi);
2344     vec[2] = sinf(theta);
2345
2346     normalize_vector(vec);
2347 }
2348
2349 /**
2350  * Prepare data for processing fisheye input format.
2351  *
2352  * @param ctx filter context
2353  *
2354  * @return error code
2355  */
2356 static int prepare_fisheye_in(AVFilterContext *ctx)
2357 {
2358     V360Context *s = ctx->priv;
2359
2360     s->iflat_range[0] = s->ih_fov / 180.f;
2361     s->iflat_range[1] = s->iv_fov / 180.f;
2362
2363     return 0;
2364 }
2365
2366 /**
2367  * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2368  *
2369  * @param s filter private context
2370  * @param vec coordinates on sphere
2371  * @param width frame width
2372  * @param height frame height
2373  * @param us horizontal coordinates for interpolation window
2374  * @param vs vertical coordinates for interpolation window
2375  * @param du horizontal relative coordinate
2376  * @param dv vertical relative coordinate
2377  */
2378 static void xyz_to_fisheye(const V360Context *s,
2379                            const float *vec, int width, int height,
2380                            int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2381 {
2382     const float phi   = -atan2f(hypotf(vec[0], vec[1]), -vec[2]) / M_PI;
2383     const float theta = -atan2f(vec[0], vec[1]);
2384
2385     float uf = sinf(theta) * phi * s->input_mirror_modifier[0] / s->iflat_range[0];
2386     float vf = cosf(theta) * phi * s->input_mirror_modifier[1] / s->iflat_range[1];
2387
2388     const int visible = hypotf(uf, vf) <= 0.5f;
2389     int ui, vi;
2390
2391     uf = (uf + 0.5f) * width;
2392     vf = (vf + 0.5f) * height;
2393
2394     ui = floorf(uf);
2395     vi = floorf(vf);
2396
2397     *du = visible ? uf - ui : 0.f;
2398     *dv = visible ? vf - vi : 0.f;
2399
2400     for (int i = -1; i < 3; i++) {
2401         for (int j = -1; j < 3; j++) {
2402             us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width  - 1) : 0;
2403             vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2404         }
2405     }
2406 }
2407
2408 /**
2409  * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2410  *
2411  * @param s filter private context
2412  * @param i horizontal position on frame [0, width)
2413  * @param j vertical position on frame [0, height)
2414  * @param width frame width
2415  * @param height frame height
2416  * @param vec coordinates on sphere
2417  */
2418 static void pannini_to_xyz(const V360Context *s,
2419                            int i, int j, int width, int height,
2420                            float *vec)
2421 {
2422     const float uf = ((2.f * i) / width  - 1.f);
2423     const float vf = ((2.f * j) / height - 1.f);
2424
2425     const float d = s->h_fov;
2426     const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2427     const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2428     const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2429     const float S = (d + 1.f) / (d + clon);
2430     const float lon = -(M_PI + atan2f(uf, S * clon));
2431     const float lat = -atan2f(vf, S);
2432
2433     vec[0] = sinf(lon) * cosf(lat);
2434     vec[1] = sinf(lat);
2435     vec[2] = cosf(lon) * cosf(lat);
2436
2437     normalize_vector(vec);
2438 }
2439
2440 /**
2441  * Prepare data for processing cylindrical output format.
2442  *
2443  * @param ctx filter context
2444  *
2445  * @return error code
2446  */
2447 static int prepare_cylindrical_out(AVFilterContext *ctx)
2448 {
2449     V360Context *s = ctx->priv;
2450
2451     s->flat_range[0] = M_PI * s->h_fov / 360.f;
2452     s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2453
2454     return 0;
2455 }
2456
2457 /**
2458  * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2459  *
2460  * @param s filter private context
2461  * @param i horizontal position on frame [0, width)
2462  * @param j vertical position on frame [0, height)
2463  * @param width frame width
2464  * @param height frame height
2465  * @param vec coordinates on sphere
2466  */
2467 static void cylindrical_to_xyz(const V360Context *s,
2468                                int i, int j, int width, int height,
2469                                float *vec)
2470 {
2471     const float uf = s->flat_range[0] * ((2.f * i) / width  - 1.f);
2472     const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2473
2474     const float phi   = uf;
2475     const float theta = atanf(vf);
2476
2477     const float sin_phi   = sinf(phi);
2478     const float cos_phi   = cosf(phi);
2479     const float sin_theta = sinf(theta);
2480     const float cos_theta = cosf(theta);
2481
2482     vec[0] =  cos_theta * sin_phi;
2483     vec[1] = -sin_theta;
2484     vec[2] = -cos_theta * cos_phi;
2485
2486     normalize_vector(vec);
2487 }
2488
2489 /**
2490  * Prepare data for processing cylindrical input format.
2491  *
2492  * @param ctx filter context
2493  *
2494  * @return error code
2495  */
2496 static int prepare_cylindrical_in(AVFilterContext *ctx)
2497 {
2498     V360Context *s = ctx->priv;
2499
2500     s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
2501     s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2502
2503     return 0;
2504 }
2505
2506 /**
2507  * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
2508  *
2509  * @param s filter private context
2510  * @param vec coordinates on sphere
2511  * @param width frame width
2512  * @param height frame height
2513  * @param us horizontal coordinates for interpolation window
2514  * @param vs vertical coordinates for interpolation window
2515  * @param du horizontal relative coordinate
2516  * @param dv vertical relative coordinate
2517  */
2518 static void xyz_to_cylindrical(const V360Context *s,
2519                                const float *vec, int width, int height,
2520                                int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2521 {
2522     const float phi   = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
2523     const float theta = atan2f(-vec[1], hypotf(vec[0], vec[2])) * s->input_mirror_modifier[1] / s->iflat_range[1];
2524     int visible, ui, vi;
2525     float uf, vf;
2526
2527     uf = (phi + 1.f) * (width - 1) / 2.f;
2528     vf = (tanf(theta) + 1.f) * height / 2.f;
2529     ui = floorf(uf);
2530     vi = floorf(vf);
2531
2532     visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
2533               theta <=  M_PI * s->iv_fov / 180.f &&
2534               theta >= -M_PI * s->iv_fov / 180.f;
2535
2536     *du = uf - ui;
2537     *dv = vf - vi;
2538
2539     for (int i = -1; i < 3; i++) {
2540         for (int j = -1; j < 3; j++) {
2541             us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width  - 1) : 0;
2542             vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2543         }
2544     }
2545 }
2546
2547 /**
2548  * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
2549  *
2550  * @param s filter private context
2551  * @param i horizontal position on frame [0, width)
2552  * @param j vertical position on frame [0, height)
2553  * @param width frame width
2554  * @param height frame height
2555  * @param vec coordinates on sphere
2556  */
2557 static void perspective_to_xyz(const V360Context *s,
2558                                int i, int j, int width, int height,
2559                                float *vec)
2560 {
2561     const float uf = ((2.f * i) / width  - 1.f);
2562     const float vf = ((2.f * j) / height - 1.f);
2563     const float rh = hypotf(uf, vf);
2564     const float sinzz = 1.f - rh * rh;
2565     const float h = 1.f + s->v_fov;
2566     const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
2567     const float sinz2 = sinz * sinz;
2568
2569     if (sinz2 <= 1.f) {
2570         const float cosz = sqrtf(1.f - sinz2);
2571
2572         const float theta = asinf(cosz);
2573         const float phi   = atan2f(uf, vf);
2574
2575         const float sin_phi   = sinf(phi);
2576         const float cos_phi   = cosf(phi);
2577         const float sin_theta = sinf(theta);
2578         const float cos_theta = cosf(theta);
2579
2580         vec[0] =  cos_theta * sin_phi;
2581         vec[1] =  sin_theta;
2582         vec[2] = -cos_theta * cos_phi;
2583     } else {
2584         vec[0] =  0.f;
2585         vec[1] = -1.f;
2586         vec[2] =  0.f;
2587     }
2588
2589     normalize_vector(vec);
2590 }
2591
2592 /**
2593  * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2594  *
2595  * @param s filter private context
2596  * @param i horizontal position on frame [0, width)
2597  * @param j vertical position on frame [0, height)
2598  * @param width frame width
2599  * @param height frame height
2600  * @param vec coordinates on sphere
2601  */
2602 static void dfisheye_to_xyz(const V360Context *s,
2603                             int i, int j, int width, int height,
2604                             float *vec)
2605 {
2606     const float scale = 1.f + s->out_pad;
2607
2608     const float ew = width / 2.f;
2609     const float eh = height;
2610
2611     const int ei = i >= ew ? i - ew : i;
2612     const float m = i >= ew ? -1.f : 1.f;
2613
2614     const float uf = ((2.f * ei) / ew - 1.f) * scale;
2615     const float vf = ((2.f *  j) / eh - 1.f) * scale;
2616
2617     const float h     = hypotf(uf, vf);
2618     const float lh    = h > 0.f ? h : 1.f;
2619     const float theta = m * M_PI_2 * (1.f - h);
2620
2621     const float sin_theta = sinf(theta);
2622     const float cos_theta = cosf(theta);
2623
2624     vec[0] = cos_theta * m * -uf / lh;
2625     vec[1] = cos_theta *     -vf / lh;
2626     vec[2] = sin_theta;
2627
2628     normalize_vector(vec);
2629 }
2630
2631 /**
2632  * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2633  *
2634  * @param s filter private context
2635  * @param vec coordinates on sphere
2636  * @param width frame width
2637  * @param height frame height
2638  * @param us horizontal coordinates for interpolation window
2639  * @param vs vertical coordinates for interpolation window
2640  * @param du horizontal relative coordinate
2641  * @param dv vertical relative coordinate
2642  */
2643 static void xyz_to_dfisheye(const V360Context *s,
2644                             const float *vec, int width, int height,
2645                             int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2646 {
2647     const float scale = 1.f - s->in_pad;
2648
2649     const float ew = width / 2.f;
2650     const float eh = height;
2651
2652     const float h     = hypotf(vec[0], vec[1]);
2653     const float lh    = h > 0.f ? h : 1.f;
2654     const float theta = acosf(fabsf(vec[2])) / M_PI;
2655
2656     float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2657     float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2658
2659     int ui, vi;
2660     int u_shift;
2661
2662     if (vec[2] >= 0.f) {
2663         u_shift = 0;
2664     } else {
2665         u_shift = ceilf(ew);
2666         uf = ew - uf;
2667     }
2668
2669     ui = floorf(uf);
2670     vi = floorf(vf);
2671
2672     *du = uf - ui;
2673     *dv = vf - vi;
2674
2675     for (int i = -1; i < 3; i++) {
2676         for (int j = -1; j < 3; j++) {
2677             us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width  - 1);
2678             vs[i + 1][j + 1] = av_clip(          vi + i, 0, height - 1);
2679         }
2680     }
2681 }
2682
2683 /**
2684  * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2685  *
2686  * @param s filter private context
2687  * @param i horizontal position on frame [0, width)
2688  * @param j vertical position on frame [0, height)
2689  * @param width frame width
2690  * @param height frame height
2691  * @param vec coordinates on sphere
2692  */
2693 static void barrel_to_xyz(const V360Context *s,
2694                           int i, int j, int width, int height,
2695                           float *vec)
2696 {
2697     const float scale = 0.99f;
2698     float l_x, l_y, l_z;
2699
2700     if (i < 4 * width / 5) {
2701         const float theta_range = M_PI_4;
2702
2703         const int ew = 4 * width / 5;
2704         const int eh = height;
2705
2706         const float phi   = ((2.f * i) / ew - 1.f) * M_PI        / scale;
2707         const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2708
2709         const float sin_phi   = sinf(phi);
2710         const float cos_phi   = cosf(phi);
2711         const float sin_theta = sinf(theta);
2712         const float cos_theta = cosf(theta);
2713
2714         l_x =  cos_theta * sin_phi;
2715         l_y = -sin_theta;
2716         l_z = -cos_theta * cos_phi;
2717     } else {
2718         const int ew = width  / 5;
2719         const int eh = height / 2;
2720
2721         float uf, vf;
2722
2723         if (j < eh) {   // UP
2724             uf = 2.f * (i - 4 * ew) / ew  - 1.f;
2725             vf = 2.f * (j         ) / eh - 1.f;
2726
2727             uf /= scale;
2728             vf /= scale;
2729
2730             l_x =  uf;
2731             l_y =  1.f;
2732             l_z = -vf;
2733         } else {            // DOWN
2734             uf = 2.f * (i - 4 * ew) / ew - 1.f;
2735             vf = 2.f * (j -     eh) / eh - 1.f;
2736
2737             uf /= scale;
2738             vf /= scale;
2739
2740             l_x =  uf;
2741             l_y = -1.f;
2742             l_z =  vf;
2743         }
2744     }
2745
2746     vec[0] = l_x;
2747     vec[1] = l_y;
2748     vec[2] = l_z;
2749
2750     normalize_vector(vec);
2751 }
2752
2753 /**
2754  * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2755  *
2756  * @param s filter private context
2757  * @param vec coordinates on sphere
2758  * @param width frame width
2759  * @param height frame height
2760  * @param us horizontal coordinates for interpolation window
2761  * @param vs vertical coordinates for interpolation window
2762  * @param du horizontal relative coordinate
2763  * @param dv vertical relative coordinate
2764  */
2765 static void xyz_to_barrel(const V360Context *s,
2766                           const float *vec, int width, int height,
2767                           int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2768 {
2769     const float scale = 0.99f;
2770
2771     const float phi   = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2772     const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2773     const float theta_range = M_PI_4;
2774
2775     int ew, eh;
2776     int u_shift, v_shift;
2777     float uf, vf;
2778     int ui, vi;
2779
2780     if (theta > -theta_range && theta < theta_range) {
2781         ew = 4 * width / 5;
2782         eh = height;
2783
2784         u_shift = s->ih_flip ? width / 5 : 0;
2785         v_shift = 0;
2786
2787         uf = (phi   / M_PI        * scale + 1.f) * ew / 2.f;
2788         vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2789     } else {
2790         ew = width  / 5;
2791         eh = height / 2;
2792
2793         u_shift = s->ih_flip ? 0 : 4 * ew;
2794
2795         if (theta < 0.f) {  // UP
2796             uf =  vec[0] / vec[1];
2797             vf = -vec[2] / vec[1];
2798             v_shift = 0;
2799         } else {            // DOWN
2800             uf = -vec[0] / vec[1];
2801             vf = -vec[2] / vec[1];
2802             v_shift = eh;
2803         }
2804
2805         uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2806         vf *= s->input_mirror_modifier[1];
2807
2808         uf = 0.5f * ew * (uf * scale + 1.f);
2809         vf = 0.5f * eh * (vf * scale + 1.f);
2810     }
2811
2812     ui = floorf(uf);
2813     vi = floorf(vf);
2814
2815     *du = uf - ui;
2816     *dv = vf - vi;
2817
2818     for (int i = -1; i < 3; i++) {
2819         for (int j = -1; j < 3; j++) {
2820             us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2821             vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2822         }
2823     }
2824 }
2825
2826 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2827 {
2828     for (int i = 0; i < 3; i++) {
2829         for (int j = 0; j < 3; j++) {
2830             float sum = 0.f;
2831
2832             for (int k = 0; k < 3; k++)
2833                 sum += a[i][k] * b[k][j];
2834
2835             c[i][j] = sum;
2836         }
2837     }
2838 }
2839
2840 /**
2841  * Calculate rotation matrix for yaw/pitch/roll angles.
2842  */
2843 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2844                                              float rot_mat[3][3],
2845                                              const int rotation_order[3])
2846 {
2847     const float yaw_rad   = yaw   * M_PI / 180.f;
2848     const float pitch_rad = pitch * M_PI / 180.f;
2849     const float roll_rad  = roll  * M_PI / 180.f;
2850
2851     const float sin_yaw   = sinf(-yaw_rad);
2852     const float cos_yaw   = cosf(-yaw_rad);
2853     const float sin_pitch = sinf(pitch_rad);
2854     const float cos_pitch = cosf(pitch_rad);
2855     const float sin_roll  = sinf(roll_rad);
2856     const float cos_roll  = cosf(roll_rad);
2857
2858     float m[3][3][3];
2859     float temp[3][3];
2860
2861     m[0][0][0] =  cos_yaw;  m[0][0][1] = 0;          m[0][0][2] =  sin_yaw;
2862     m[0][1][0] =  0;        m[0][1][1] = 1;          m[0][1][2] =  0;
2863     m[0][2][0] = -sin_yaw;  m[0][2][1] = 0;          m[0][2][2] =  cos_yaw;
2864
2865     m[1][0][0] = 1;         m[1][0][1] = 0;          m[1][0][2] =  0;
2866     m[1][1][0] = 0;         m[1][1][1] = cos_pitch;  m[1][1][2] = -sin_pitch;
2867     m[1][2][0] = 0;         m[1][2][1] = sin_pitch;  m[1][2][2] =  cos_pitch;
2868
2869     m[2][0][0] = cos_roll;  m[2][0][1] = -sin_roll;  m[2][0][2] =  0;
2870     m[2][1][0] = sin_roll;  m[2][1][1] =  cos_roll;  m[2][1][2] =  0;
2871     m[2][2][0] = 0;         m[2][2][1] =  0;         m[2][2][2] =  1;
2872
2873     multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2874     multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2875 }
2876
2877 /**
2878  * Rotate vector with given rotation matrix.
2879  *
2880  * @param rot_mat rotation matrix
2881  * @param vec vector
2882  */
2883 static inline void rotate(const float rot_mat[3][3],
2884                           float *vec)
2885 {
2886     const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2887     const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2888     const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2889
2890     vec[0] = x_tmp;
2891     vec[1] = y_tmp;
2892     vec[2] = z_tmp;
2893 }
2894
2895 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2896                                        float *modifier)
2897 {
2898     modifier[0] = h_flip ? -1.f : 1.f;
2899     modifier[1] = v_flip ? -1.f : 1.f;
2900     modifier[2] = d_flip ? -1.f : 1.f;
2901 }
2902
2903 static inline void mirror(const float *modifier, float *vec)
2904 {
2905     vec[0] *= modifier[0];
2906     vec[1] *= modifier[1];
2907     vec[2] *= modifier[2];
2908 }
2909
2910 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2911 {
2912     s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2913     s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2914     if (!s->u[p] || !s->v[p])
2915         return AVERROR(ENOMEM);
2916     if (sizeof_ker) {
2917         s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2918         if (!s->ker[p])
2919             return AVERROR(ENOMEM);
2920     }
2921
2922     return 0;
2923 }
2924
2925 static void fov_from_dfov(float d_fov, float w, float h, float *h_fov, float *v_fov)
2926 {
2927     const float da = tanf(0.5 * FFMIN(d_fov, 359.f) * M_PI / 180.f);
2928     const float d = hypotf(w, h);
2929
2930     *h_fov = atan2f(da * w, d) * 360.f / M_PI;
2931     *v_fov = atan2f(da * h, d) * 360.f / M_PI;
2932
2933     if (*h_fov < 0.f)
2934         *h_fov += 360.f;
2935     if (*v_fov < 0.f)
2936         *v_fov += 360.f;
2937 }
2938
2939 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2940 {
2941     outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2942     outw[0] = outw[3] = w;
2943     outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2944     outh[0] = outh[3] = h;
2945 }
2946
2947 // Calculate remap data
2948 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2949 {
2950     V360Context *s = ctx->priv;
2951
2952     for (int p = 0; p < s->nb_allocated; p++) {
2953         const int width = s->pr_width[p];
2954         const int uv_linesize = s->uv_linesize[p];
2955         const int height = s->pr_height[p];
2956         const int in_width = s->inplanewidth[p];
2957         const int in_height = s->inplaneheight[p];
2958         const int slice_start = (height *  jobnr     ) / nb_jobs;
2959         const int slice_end   = (height * (jobnr + 1)) / nb_jobs;
2960         float du, dv;
2961         float vec[3];
2962         XYRemap rmap;
2963
2964         for (int j = slice_start; j < slice_end; j++) {
2965             for (int i = 0; i < width; i++) {
2966                 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2967                 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2968                 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2969
2970                 if (s->out_transpose)
2971                     s->out_transform(s, j, i, height, width, vec);
2972                 else
2973                     s->out_transform(s, i, j, width, height, vec);
2974                 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2975                 rotate(s->rot_mat, vec);
2976                 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2977                 normalize_vector(vec);
2978                 mirror(s->output_mirror_modifier, vec);
2979                 if (s->in_transpose)
2980                     s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2981                 else
2982                     s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2983                 av_assert1(!isnan(du) && !isnan(dv));
2984                 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2985             }
2986         }
2987     }
2988
2989     return 0;
2990 }
2991
2992 static int config_output(AVFilterLink *outlink)
2993 {
2994     AVFilterContext *ctx = outlink->src;
2995     AVFilterLink *inlink = ctx->inputs[0];
2996     V360Context *s = ctx->priv;
2997     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2998     const int depth = desc->comp[0].depth;
2999     int sizeof_uv;
3000     int sizeof_ker;
3001     int err;
3002     int h, w;
3003     int in_offset_h, in_offset_w;
3004     int out_offset_h, out_offset_w;
3005     float hf, wf;
3006     int (*prepare_out)(AVFilterContext *ctx);
3007
3008     s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
3009     s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
3010
3011     switch (s->interp) {
3012     case NEAREST:
3013         s->calculate_kernel = nearest_kernel;
3014         s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
3015         s->elements = 1;
3016         sizeof_uv = sizeof(int16_t) * s->elements;
3017         sizeof_ker = 0;
3018         break;
3019     case BILINEAR:
3020         s->calculate_kernel = bilinear_kernel;
3021         s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
3022         s->elements = 2 * 2;
3023         sizeof_uv = sizeof(int16_t) * s->elements;
3024         sizeof_ker = sizeof(int16_t) * s->elements;
3025         break;
3026     case BICUBIC:
3027         s->calculate_kernel = bicubic_kernel;
3028         s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3029         s->elements = 4 * 4;
3030         sizeof_uv = sizeof(int16_t) * s->elements;
3031         sizeof_ker = sizeof(int16_t) * s->elements;
3032         break;
3033     case LANCZOS:
3034         s->calculate_kernel = lanczos_kernel;
3035         s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3036         s->elements = 4 * 4;
3037         sizeof_uv = sizeof(int16_t) * s->elements;
3038         sizeof_ker = sizeof(int16_t) * s->elements;
3039         break;
3040     case SPLINE16:
3041         s->calculate_kernel = spline16_kernel;
3042         s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3043         s->elements = 4 * 4;
3044         sizeof_uv = sizeof(int16_t) * s->elements;
3045         sizeof_ker = sizeof(int16_t) * s->elements;
3046         break;
3047     case GAUSSIAN:
3048         s->calculate_kernel = gaussian_kernel;
3049         s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3050         s->elements = 4 * 4;
3051         sizeof_uv = sizeof(int16_t) * s->elements;
3052         sizeof_ker = sizeof(int16_t) * s->elements;
3053         break;
3054     default:
3055         av_assert0(0);
3056     }
3057
3058     ff_v360_init(s, depth);
3059
3060     for (int order = 0; order < NB_RORDERS; order++) {
3061         const char c = s->rorder[order];
3062         int rorder;
3063
3064         if (c == '\0') {
3065             av_log(ctx, AV_LOG_ERROR,
3066                    "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
3067             return AVERROR(EINVAL);
3068         }
3069
3070         rorder = get_rorder(c);
3071         if (rorder == -1) {
3072             av_log(ctx, AV_LOG_ERROR,
3073                    "Incorrect rotation order symbol '%c' in rorder option.\n", c);
3074             return AVERROR(EINVAL);
3075         }
3076
3077         s->rotation_order[order] = rorder;
3078     }
3079
3080     switch (s->in_stereo) {
3081     case STEREO_2D:
3082         w = inlink->w;
3083         h = inlink->h;
3084         in_offset_w = in_offset_h = 0;
3085         break;
3086     case STEREO_SBS:
3087         w = inlink->w / 2;
3088         h = inlink->h;
3089         in_offset_w = w;
3090         in_offset_h = 0;
3091         break;
3092     case STEREO_TB:
3093         w = inlink->w;
3094         h = inlink->h / 2;
3095         in_offset_w = 0;
3096         in_offset_h = h;
3097         break;
3098     default:
3099         av_assert0(0);
3100     }
3101
3102     set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
3103     set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
3104
3105     s->in_width = s->inplanewidth[0];
3106     s->in_height = s->inplaneheight[0];
3107
3108     if (s->id_fov > 0.f)
3109         fov_from_dfov(s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
3110
3111     if (s->in_transpose)
3112         FFSWAP(int, s->in_width, s->in_height);
3113
3114     switch (s->in) {
3115     case EQUIRECTANGULAR:
3116         s->in_transform = xyz_to_equirect;
3117         err = 0;
3118         wf = w;
3119         hf = h;
3120         break;
3121     case CUBEMAP_3_2:
3122         s->in_transform = xyz_to_cube3x2;
3123         err = prepare_cube_in(ctx);
3124         wf = w / 3.f * 4.f;
3125         hf = h;
3126         break;
3127     case CUBEMAP_1_6:
3128         s->in_transform = xyz_to_cube1x6;
3129         err = prepare_cube_in(ctx);
3130         wf = w * 4.f;
3131         hf = h / 3.f;
3132         break;
3133     case CUBEMAP_6_1:
3134         s->in_transform = xyz_to_cube6x1;
3135         err = prepare_cube_in(ctx);
3136         wf = w / 3.f * 2.f;
3137         hf = h * 2.f;
3138         break;
3139     case EQUIANGULAR:
3140         s->in_transform = xyz_to_eac;
3141         err = prepare_eac_in(ctx);
3142         wf = w;
3143         hf = h / 9.f * 8.f;
3144         break;
3145     case FLAT:
3146         s->in_transform = xyz_to_flat;
3147         err = prepare_flat_in(ctx);
3148         wf = w;
3149         hf = h;
3150         break;
3151     case PERSPECTIVE:
3152     case PANNINI:
3153         av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
3154         return AVERROR(EINVAL);
3155     case DUAL_FISHEYE:
3156         s->in_transform = xyz_to_dfisheye;
3157         err = 0;
3158         wf = w;
3159         hf = h;
3160         break;
3161     case BARREL:
3162         s->in_transform = xyz_to_barrel;
3163         err = 0;
3164         wf = w / 5.f * 4.f;
3165         hf = h;
3166         break;
3167     case STEREOGRAPHIC:
3168         s->in_transform = xyz_to_stereographic;
3169         err = prepare_stereographic_in(ctx);
3170         wf = w;
3171         hf = h / 2.f;
3172         break;
3173     case MERCATOR:
3174         s->in_transform = xyz_to_mercator;
3175         err = 0;
3176         wf = w;
3177         hf = h / 2.f;
3178         break;
3179     case BALL:
3180         s->in_transform = xyz_to_ball;
3181         err = 0;
3182         wf = w;
3183         hf = h / 2.f;
3184         break;
3185     case HAMMER:
3186         s->in_transform = xyz_to_hammer;
3187         err = 0;
3188         wf = w;
3189         hf = h;
3190         break;
3191     case SINUSOIDAL:
3192         s->in_transform = xyz_to_sinusoidal;
3193         err = 0;
3194         wf = w;
3195         hf = h;
3196         break;
3197     case FISHEYE:
3198         s->in_transform = xyz_to_fisheye;
3199         err = prepare_fisheye_in(ctx);
3200         wf = w * 2;
3201         hf = h;
3202         break;
3203     case CYLINDRICAL:
3204         s->in_transform = xyz_to_cylindrical;
3205         err = prepare_cylindrical_in(ctx);
3206         wf = w;
3207         hf = h * 2.f;
3208         break;
3209     default:
3210         av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
3211         return AVERROR_BUG;
3212     }
3213
3214     if (err != 0) {
3215         return err;
3216     }
3217
3218     switch (s->out) {
3219     case EQUIRECTANGULAR:
3220         s->out_transform = equirect_to_xyz;
3221         prepare_out = NULL;
3222         w = lrintf(wf);
3223         h = lrintf(hf);
3224         break;
3225     case CUBEMAP_3_2:
3226         s->out_transform = cube3x2_to_xyz;
3227         prepare_out = prepare_cube_out;
3228         w = lrintf(wf / 4.f * 3.f);
3229         h = lrintf(hf);
3230         break;
3231     case CUBEMAP_1_6:
3232         s->out_transform = cube1x6_to_xyz;
3233         prepare_out = prepare_cube_out;
3234         w = lrintf(wf / 4.f);
3235         h = lrintf(hf * 3.f);
3236         break;
3237     case CUBEMAP_6_1:
3238         s->out_transform = cube6x1_to_xyz;
3239         prepare_out = prepare_cube_out;
3240         w = lrintf(wf / 2.f * 3.f);
3241         h = lrintf(hf / 2.f);
3242         break;
3243     case EQUIANGULAR:
3244         s->out_transform = eac_to_xyz;
3245         prepare_out = prepare_eac_out;
3246         w = lrintf(wf);
3247         h = lrintf(hf / 8.f * 9.f);
3248         break;
3249     case FLAT:
3250         s->out_transform = flat_to_xyz;
3251         prepare_out = prepare_flat_out;
3252         w = lrintf(wf);
3253         h = lrintf(hf);
3254         break;
3255     case DUAL_FISHEYE:
3256         s->out_transform = dfisheye_to_xyz;
3257         prepare_out = NULL;
3258         w = lrintf(wf);
3259         h = lrintf(hf);
3260         break;
3261     case BARREL:
3262         s->out_transform = barrel_to_xyz;
3263         prepare_out = NULL;
3264         w = lrintf(wf / 4.f * 5.f);
3265         h = lrintf(hf);
3266         break;
3267     case STEREOGRAPHIC:
3268         s->out_transform = stereographic_to_xyz;
3269         prepare_out = prepare_stereographic_out;
3270         w = lrintf(wf);
3271         h = lrintf(hf * 2.f);
3272         break;
3273     case MERCATOR:
3274         s->out_transform = mercator_to_xyz;
3275         prepare_out = NULL;
3276         w = lrintf(wf);
3277         h = lrintf(hf * 2.f);
3278         break;
3279     case BALL:
3280         s->out_transform = ball_to_xyz;
3281         prepare_out = NULL;
3282         w = lrintf(wf);
3283         h = lrintf(hf * 2.f);
3284         break;
3285     case HAMMER:
3286         s->out_transform = hammer_to_xyz;
3287         prepare_out = NULL;
3288         w = lrintf(wf);
3289         h = lrintf(hf);
3290         break;
3291     case SINUSOIDAL:
3292         s->out_transform = sinusoidal_to_xyz;
3293         prepare_out = NULL;
3294         w = lrintf(wf);
3295         h = lrintf(hf);
3296         break;
3297     case FISHEYE:
3298         s->out_transform = fisheye_to_xyz;
3299         prepare_out = prepare_fisheye_out;
3300         w = lrintf(wf * 0.5f);
3301         h = lrintf(hf);
3302         break;
3303     case PANNINI:
3304         s->out_transform = pannini_to_xyz;
3305         prepare_out = NULL;
3306         w = lrintf(wf);
3307         h = lrintf(hf);
3308         break;
3309     case CYLINDRICAL:
3310         s->out_transform = cylindrical_to_xyz;
3311         prepare_out = prepare_cylindrical_out;
3312         w = lrintf(wf);
3313         h = lrintf(hf * 0.5f);
3314         break;
3315     case PERSPECTIVE:
3316         s->out_transform = perspective_to_xyz;
3317         prepare_out = NULL;
3318         w = lrintf(wf / 2.f);
3319         h = lrintf(hf);
3320         break;
3321     default:
3322         av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
3323         return AVERROR_BUG;
3324     }
3325
3326     // Override resolution with user values if specified
3327     if (s->width > 0 && s->height > 0) {
3328         w = s->width;
3329         h = s->height;
3330     } else if (s->width > 0 || s->height > 0) {
3331         av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
3332         return AVERROR(EINVAL);
3333     } else {
3334         if (s->out_transpose)
3335             FFSWAP(int, w, h);
3336
3337         if (s->in_transpose)
3338             FFSWAP(int, w, h);
3339     }
3340
3341     if (s->d_fov > 0.f)
3342         fov_from_dfov(s->d_fov, w, h, &s->h_fov, &s->v_fov);
3343
3344     if (prepare_out) {
3345         err = prepare_out(ctx);
3346         if (err != 0)
3347             return err;
3348     }
3349
3350     set_dimensions(s->pr_width, s->pr_height, w, h, desc);
3351
3352     s->out_width = s->pr_width[0];
3353     s->out_height = s->pr_height[0];
3354
3355     if (s->out_transpose)
3356         FFSWAP(int, s->out_width, s->out_height);
3357
3358     switch (s->out_stereo) {
3359     case STEREO_2D:
3360         out_offset_w = out_offset_h = 0;
3361         break;
3362     case STEREO_SBS:
3363         out_offset_w = w;
3364         out_offset_h = 0;
3365         w *= 2;
3366         break;
3367     case STEREO_TB:
3368         out_offset_w = 0;
3369         out_offset_h = h;
3370         h *= 2;
3371         break;
3372     default:
3373         av_assert0(0);
3374     }
3375
3376     set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
3377     set_dimensions(s->planewidth, s->planeheight, w, h, desc);
3378
3379     for (int i = 0; i < 4; i++)
3380         s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
3381
3382     outlink->h = h;
3383     outlink->w = w;
3384
3385     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
3386
3387     if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
3388         s->nb_allocated = 1;
3389         s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
3390     } else {
3391         s->nb_allocated = 2;
3392         s->map[0] = s->map[3] = 0;
3393         s->map[1] = s->map[2] = 1;
3394     }
3395
3396     for (int i = 0; i < s->nb_allocated; i++)
3397         allocate_plane(s, sizeof_uv, sizeof_ker, i);
3398
3399     calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
3400     set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
3401
3402     ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3403
3404     return 0;
3405 }
3406
3407 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3408 {
3409     AVFilterContext *ctx = inlink->dst;
3410     AVFilterLink *outlink = ctx->outputs[0];
3411     V360Context *s = ctx->priv;
3412     AVFrame *out;
3413     ThreadData td;
3414
3415     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3416     if (!out) {
3417         av_frame_free(&in);
3418         return AVERROR(ENOMEM);
3419     }
3420     av_frame_copy_props(out, in);
3421
3422     td.in = in;
3423     td.out = out;
3424
3425     ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3426
3427     av_frame_free(&in);
3428     return ff_filter_frame(outlink, out);
3429 }
3430
3431 static av_cold void uninit(AVFilterContext *ctx)
3432 {
3433     V360Context *s = ctx->priv;
3434
3435     for (int p = 0; p < s->nb_allocated; p++) {
3436         av_freep(&s->u[p]);
3437         av_freep(&s->v[p]);
3438         av_freep(&s->ker[p]);
3439     }
3440 }
3441
3442 static const AVFilterPad inputs[] = {
3443     {
3444         .name         = "default",
3445         .type         = AVMEDIA_TYPE_VIDEO,
3446         .filter_frame = filter_frame,
3447     },
3448     { NULL }
3449 };
3450
3451 static const AVFilterPad outputs[] = {
3452     {
3453         .name         = "default",
3454         .type         = AVMEDIA_TYPE_VIDEO,
3455         .config_props = config_output,
3456     },
3457     { NULL }
3458 };
3459
3460 AVFilter ff_vf_v360 = {
3461     .name          = "v360",
3462     .description   = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3463     .priv_size     = sizeof(V360Context),
3464     .uninit        = uninit,
3465     .query_formats = query_formats,
3466     .inputs        = inputs,
3467     .outputs       = outputs,
3468     .priv_class    = &v360_class,
3469     .flags         = AVFILTER_FLAG_SLICE_THREADS,
3470 };