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