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