]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_v360.c
avfilter/vf_v360: support transposed input/output
[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 "libavutil/avassert.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/opt.h"
40 #include "avfilter.h"
41 #include "formats.h"
42 #include "internal.h"
43 #include "video.h"
44 #include "v360.h"
45
46 typedef struct ThreadData {
47     AVFrame *in;
48     AVFrame *out;
49 } ThreadData;
50
51 #define OFFSET(x) offsetof(V360Context, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53
54 static const AVOption v360_options[] = {
55     {     "input", "set input projection",              OFFSET(in), AV_OPT_TYPE_INT,    {.i64=EQUIRECTANGULAR}, 0,    NB_PROJECTIONS-1, FLAGS, "in" },
56     {         "e", "equirectangular",                            0, AV_OPT_TYPE_CONST,  {.i64=EQUIRECTANGULAR}, 0,                   0, FLAGS, "in" },
57     {      "c3x2", "cubemap3x2",                                 0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_3_2},     0,                   0, FLAGS, "in" },
58     {      "c6x1", "cubemap6x1",                                 0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_6_1},     0,                   0, FLAGS, "in" },
59     {       "eac", "equi-angular cubemap",                       0, AV_OPT_TYPE_CONST,  {.i64=EQUIANGULAR},     0,                   0, FLAGS, "in" },
60     {  "dfisheye", "dual fisheye",                               0, AV_OPT_TYPE_CONST,  {.i64=DUAL_FISHEYE},    0,                   0, FLAGS, "in" },
61     {    "barrel", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "in" },
62     {        "fb", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "in" },
63     {      "c1x6", "cubemap1x6",                                 0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_1_6},     0,                   0, FLAGS, "in" },
64     {    "output", "set output projection",            OFFSET(out), AV_OPT_TYPE_INT,    {.i64=CUBEMAP_3_2},     0,    NB_PROJECTIONS-1, FLAGS, "out" },
65     {         "e", "equirectangular",                            0, AV_OPT_TYPE_CONST,  {.i64=EQUIRECTANGULAR}, 0,                   0, FLAGS, "out" },
66     {      "c3x2", "cubemap3x2",                                 0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_3_2},     0,                   0, FLAGS, "out" },
67     {      "c6x1", "cubemap6x1",                                 0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_6_1},     0,                   0, FLAGS, "out" },
68     {       "eac", "equi-angular cubemap",                       0, AV_OPT_TYPE_CONST,  {.i64=EQUIANGULAR},     0,                   0, FLAGS, "out" },
69     {      "flat", "regular video",                              0, AV_OPT_TYPE_CONST,  {.i64=FLAT},            0,                   0, FLAGS, "out" },
70     {    "barrel", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "out" },
71     {        "fb", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "out" },
72     {      "c1x6", "cubemap1x6",                                 0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_1_6},     0,                   0, FLAGS, "out" },
73     {    "interp", "set interpolation method",      OFFSET(interp), AV_OPT_TYPE_INT,    {.i64=BILINEAR},        0, NB_INTERP_METHODS-1, FLAGS, "interp" },
74     {      "near", "nearest neighbour",                          0, AV_OPT_TYPE_CONST,  {.i64=NEAREST},         0,                   0, FLAGS, "interp" },
75     {   "nearest", "nearest neighbour",                          0, AV_OPT_TYPE_CONST,  {.i64=NEAREST},         0,                   0, FLAGS, "interp" },
76     {      "line", "bilinear interpolation",                     0, AV_OPT_TYPE_CONST,  {.i64=BILINEAR},        0,                   0, FLAGS, "interp" },
77     {    "linear", "bilinear interpolation",                     0, AV_OPT_TYPE_CONST,  {.i64=BILINEAR},        0,                   0, FLAGS, "interp" },
78     {      "cube", "bicubic interpolation",                      0, AV_OPT_TYPE_CONST,  {.i64=BICUBIC},         0,                   0, FLAGS, "interp" },
79     {     "cubic", "bicubic interpolation",                      0, AV_OPT_TYPE_CONST,  {.i64=BICUBIC},         0,                   0, FLAGS, "interp" },
80     {      "lanc", "lanczos interpolation",                      0, AV_OPT_TYPE_CONST,  {.i64=LANCZOS},         0,                   0, FLAGS, "interp" },
81     {   "lanczos", "lanczos interpolation",                      0, AV_OPT_TYPE_CONST,  {.i64=LANCZOS},         0,                   0, FLAGS, "interp" },
82     {         "w", "output width",                   OFFSET(width), AV_OPT_TYPE_INT,    {.i64=0},               0,           INT16_MAX, FLAGS, "w"},
83     {         "h", "output height",                 OFFSET(height), AV_OPT_TYPE_INT,    {.i64=0},               0,           INT16_MAX, FLAGS, "h"},
84     { "in_forder", "input cubemap face order",   OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"},        0,     NB_DIRECTIONS-1, FLAGS, "in_forder"},
85     {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"},        0,     NB_DIRECTIONS-1, FLAGS, "out_forder"},
86     {   "in_frot", "input cubemap face rotation",  OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"},        0,     NB_DIRECTIONS-1, FLAGS, "in_frot"},
87     {  "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"},        0,     NB_DIRECTIONS-1, FLAGS, "out_frot"},
88     {    "in_pad", "input cubemap pads",            OFFSET(in_pad), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},           0.f,                 1.f, FLAGS, "in_pad"},
89     {   "out_pad", "output cubemap pads",          OFFSET(out_pad), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},           0.f,                 1.f, FLAGS, "out_pad"},
90     {       "yaw", "yaw rotation",                     OFFSET(yaw), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},        -180.f,               180.f, FLAGS, "yaw"},
91     {     "pitch", "pitch rotation",                 OFFSET(pitch), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},        -180.f,               180.f, FLAGS, "pitch"},
92     {      "roll", "roll rotation",                   OFFSET(roll), AV_OPT_TYPE_FLOAT,  {.dbl=0.f},        -180.f,               180.f, FLAGS, "roll"},
93     {    "rorder", "rotation order",                OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"},           0,                   0, FLAGS, "rorder"},
94     {     "h_fov", "horizontal field of view",       OFFSET(h_fov), AV_OPT_TYPE_FLOAT,  {.dbl=90.f},          0.f,               180.f, FLAGS, "h_fov"},
95     {     "v_fov", "vertical field of view",         OFFSET(v_fov), AV_OPT_TYPE_FLOAT,  {.dbl=45.f},          0.f,                90.f, FLAGS, "v_fov"},
96     {    "h_flip", "flip out video horizontally",   OFFSET(h_flip), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "h_flip"},
97     {    "v_flip", "flip out video vertically",     OFFSET(v_flip), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "v_flip"},
98     {    "d_flip", "flip out video indepth",        OFFSET(d_flip), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "d_flip"},
99     {  "in_trans", "transpose video input",   OFFSET(in_transpose), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "in_transpose"},
100     { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL,   {.i64=0},               0,                   1, FLAGS, "out_transpose"},
101     { NULL }
102 };
103
104 AVFILTER_DEFINE_CLASS(v360);
105
106 static int query_formats(AVFilterContext *ctx)
107 {
108     static const enum AVPixelFormat pix_fmts[] = {
109         // YUVA444
110         AV_PIX_FMT_YUVA444P,   AV_PIX_FMT_YUVA444P9,
111         AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
112         AV_PIX_FMT_YUVA444P16,
113
114         // YUVA422
115         AV_PIX_FMT_YUVA422P,   AV_PIX_FMT_YUVA422P9,
116         AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
117         AV_PIX_FMT_YUVA422P16,
118
119         // YUVA420
120         AV_PIX_FMT_YUVA420P,   AV_PIX_FMT_YUVA420P9,
121         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
122
123         // YUVJ
124         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
125         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
126         AV_PIX_FMT_YUVJ411P,
127
128         // YUV444
129         AV_PIX_FMT_YUV444P,   AV_PIX_FMT_YUV444P9,
130         AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
131         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
132
133         // YUV440
134         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
135         AV_PIX_FMT_YUV440P12,
136
137         // YUV422
138         AV_PIX_FMT_YUV422P,   AV_PIX_FMT_YUV422P9,
139         AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
140         AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
141
142         // YUV420
143         AV_PIX_FMT_YUV420P,   AV_PIX_FMT_YUV420P9,
144         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
145         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
146
147         // YUV411
148         AV_PIX_FMT_YUV411P,
149
150         // YUV410
151         AV_PIX_FMT_YUV410P,
152
153         // GBR
154         AV_PIX_FMT_GBRP,   AV_PIX_FMT_GBRP9,
155         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
156         AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
157
158         // GBRA
159         AV_PIX_FMT_GBRAP,   AV_PIX_FMT_GBRAP10,
160         AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
161
162         // GRAY
163         AV_PIX_FMT_GRAY8,  AV_PIX_FMT_GRAY9,
164         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
165         AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
166
167         AV_PIX_FMT_NONE
168     };
169
170     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
171     if (!fmts_list)
172         return AVERROR(ENOMEM);
173     return ff_set_common_formats(ctx, fmts_list);
174 }
175
176 #define DEFINE_REMAP1_LINE(bits, div)                                                           \
177 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src,              \
178                                       ptrdiff_t in_linesize,                                    \
179                                       const uint16_t *u, const uint16_t *v, const int16_t *ker) \
180 {                                                                                               \
181     const uint##bits##_t *s = (const uint##bits##_t *)src;                                      \
182     uint##bits##_t *d = (uint##bits##_t *)dst;                                                  \
183                                                                                                 \
184     in_linesize /= div;                                                                         \
185                                                                                                 \
186     for (int x = 0; x < width; x++)                                                             \
187         d[x] = s[v[x] * in_linesize + u[x]];                                                    \
188 }
189
190 DEFINE_REMAP1_LINE( 8, 1)
191 DEFINE_REMAP1_LINE(16, 2)
192
193 typedef struct XYRemap {
194     uint16_t u[4][4];
195     uint16_t v[4][4];
196     float ker[4][4];
197 } XYRemap;
198
199 /**
200  * Generate remapping function with a given window size and pixel depth.
201  *
202  * @param ws size of interpolation window
203  * @param bits number of bits per pixel
204  */
205 #define DEFINE_REMAP(ws, bits)                                                                             \
206 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)          \
207 {                                                                                                          \
208     ThreadData *td = (ThreadData*)arg;                                                                     \
209     const V360Context *s = ctx->priv;                                                                      \
210     const AVFrame *in = td->in;                                                                            \
211     AVFrame *out = td->out;                                                                                \
212                                                                                                            \
213     for (int plane = 0; plane < s->nb_planes; plane++) {                                                   \
214         const int in_linesize  = in->linesize[plane];                                                      \
215         const int out_linesize = out->linesize[plane];                                                     \
216         const uint8_t *src = in->data[plane];                                                              \
217         uint8_t *dst = out->data[plane];                                                                   \
218         const int width = s->planewidth[plane];                                                            \
219         const int height = s->planeheight[plane];                                                          \
220                                                                                                            \
221         const int slice_start = (height *  jobnr     ) / nb_jobs;                                          \
222         const int slice_end   = (height * (jobnr + 1)) / nb_jobs;                                          \
223                                                                                                            \
224         for (int y = slice_start; y < slice_end; y++) {                                                    \
225             const unsigned map = s->map[plane];                                                            \
226             const uint16_t *u = s->u[map] + y * width * ws * ws;                                           \
227             const uint16_t *v = s->v[map] + y * width * ws * ws;                                           \
228             const int16_t *ker = s->ker[map] + y * width * ws * ws;                                        \
229                                                                                                            \
230             s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker);                     \
231         }                                                                                                  \
232     }                                                                                                      \
233                                                                                                            \
234     return 0;                                                                                              \
235 }
236
237 DEFINE_REMAP(1,  8)
238 DEFINE_REMAP(2,  8)
239 DEFINE_REMAP(4,  8)
240 DEFINE_REMAP(1, 16)
241 DEFINE_REMAP(2, 16)
242 DEFINE_REMAP(4, 16)
243
244 #define DEFINE_REMAP_LINE(ws, bits, div)                                                                   \
245 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src,                    \
246                                            ptrdiff_t in_linesize,                                          \
247                                            const uint16_t *u, const uint16_t *v, const int16_t *ker)       \
248 {                                                                                                          \
249     const uint##bits##_t *s = (const uint##bits##_t *)src;                                                 \
250     uint##bits##_t *d = (uint##bits##_t *)dst;                                                             \
251                                                                                                            \
252     in_linesize /= div;                                                                                    \
253                                                                                                            \
254     for (int x = 0; x < width; x++) {                                                                      \
255         const uint16_t *uu = u + x * ws * ws;                                                              \
256         const uint16_t *vv = v + x * ws * ws;                                                              \
257         const int16_t *kker = ker + x * ws * ws;                                                           \
258         int tmp = 0;                                                                                       \
259                                                                                                            \
260         for (int i = 0; i < ws; i++) {                                                                     \
261             for (int j = 0; j < ws; j++) {                                                                 \
262                 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]];                \
263             }                                                                                              \
264         }                                                                                                  \
265                                                                                                            \
266         d[x] = av_clip_uint##bits(tmp >> 14);                                                              \
267     }                                                                                                      \
268 }
269
270 DEFINE_REMAP_LINE(2,  8, 1)
271 DEFINE_REMAP_LINE(4,  8, 1)
272 DEFINE_REMAP_LINE(2, 16, 2)
273 DEFINE_REMAP_LINE(4, 16, 2)
274
275 void ff_v360_init(V360Context *s, int depth)
276 {
277     switch (s->interp) {
278     case NEAREST:
279         s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
280         break;
281     case BILINEAR:
282         s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
283         break;
284     case BICUBIC:
285     case LANCZOS:
286         s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
287         break;
288     }
289
290     if (ARCH_X86)
291         ff_v360_init_x86(s, depth);
292 }
293
294 /**
295  * Save nearest pixel coordinates for remapping.
296  *
297  * @param du horizontal relative coordinate
298  * @param dv vertical relative coordinate
299  * @param r_tmp calculated 4x4 window
300  * @param u u remap data
301  * @param v v remap data
302  * @param ker ker remap data
303  */
304 static void nearest_kernel(float du, float dv, const XYRemap *r_tmp,
305                            uint16_t *u, uint16_t *v, int16_t *ker)
306 {
307     const int i = roundf(dv) + 1;
308     const int j = roundf(du) + 1;
309
310     u[0] = r_tmp->u[i][j];
311     v[0] = r_tmp->v[i][j];
312 }
313
314 /**
315  * Calculate kernel for bilinear interpolation.
316  *
317  * @param du horizontal relative coordinate
318  * @param dv vertical relative coordinate
319  * @param r_tmp calculated 4x4 window
320  * @param u u remap data
321  * @param v v remap data
322  * @param ker ker remap data
323  */
324 static void bilinear_kernel(float du, float dv, const XYRemap *r_tmp,
325                             uint16_t *u, uint16_t *v, int16_t *ker)
326 {
327     int i, j;
328
329     for (i = 0; i < 2; i++) {
330         for (j = 0; j < 2; j++) {
331             u[i * 2 + j] = r_tmp->u[i + 1][j + 1];
332             v[i * 2 + j] = r_tmp->v[i + 1][j + 1];
333         }
334     }
335
336     ker[0] = (1.f - du) * (1.f - dv) * 16384;
337     ker[1] =        du  * (1.f - dv) * 16384;
338     ker[2] = (1.f - du) *        dv  * 16384;
339     ker[3] =        du  *        dv  * 16384;
340 }
341
342 /**
343  * Calculate 1-dimensional cubic coefficients.
344  *
345  * @param t relative coordinate
346  * @param coeffs coefficients
347  */
348 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
349 {
350     const float tt  = t * t;
351     const float ttt = t * t * t;
352
353     coeffs[0] =     - t / 3.f + tt / 2.f - ttt / 6.f;
354     coeffs[1] = 1.f - t / 2.f - tt       + ttt / 2.f;
355     coeffs[2] =       t       + tt / 2.f - ttt / 2.f;
356     coeffs[3] =     - t / 6.f            + ttt / 6.f;
357 }
358
359 /**
360  * Calculate kernel for bicubic interpolation.
361  *
362  * @param du horizontal relative coordinate
363  * @param dv vertical relative coordinate
364  * @param r_tmp calculated 4x4 window
365  * @param u u remap data
366  * @param v v remap data
367  * @param ker ker remap data
368  */
369 static void bicubic_kernel(float du, float dv, const XYRemap *r_tmp,
370                            uint16_t *u, uint16_t *v, int16_t *ker)
371 {
372     int i, j;
373     float du_coeffs[4];
374     float dv_coeffs[4];
375
376     calculate_bicubic_coeffs(du, du_coeffs);
377     calculate_bicubic_coeffs(dv, dv_coeffs);
378
379     for (i = 0; i < 4; i++) {
380         for (j = 0; j < 4; j++) {
381             u[i * 4 + j] = r_tmp->u[i][j];
382             v[i * 4 + j] = r_tmp->v[i][j];
383             ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
384         }
385     }
386 }
387
388 /**
389  * Calculate 1-dimensional lanczos coefficients.
390  *
391  * @param t relative coordinate
392  * @param coeffs coefficients
393  */
394 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
395 {
396     int i;
397     float sum = 0.f;
398
399     for (i = 0; i < 4; i++) {
400         const float x = M_PI * (t - i + 1);
401         if (x == 0.f) {
402             coeffs[i] = 1.f;
403         } else {
404             coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
405         }
406         sum += coeffs[i];
407     }
408
409     for (i = 0; i < 4; i++) {
410         coeffs[i] /= sum;
411     }
412 }
413
414 /**
415  * Calculate kernel for lanczos interpolation.
416  *
417  * @param du horizontal relative coordinate
418  * @param dv vertical relative coordinate
419  * @param r_tmp calculated 4x4 window
420  * @param u u remap data
421  * @param v v remap data
422  * @param ker ker remap data
423  */
424 static void lanczos_kernel(float du, float dv, const XYRemap *r_tmp,
425                            uint16_t *u, uint16_t *v, int16_t *ker)
426 {
427     int i, j;
428     float du_coeffs[4];
429     float dv_coeffs[4];
430
431     calculate_lanczos_coeffs(du, du_coeffs);
432     calculate_lanczos_coeffs(dv, dv_coeffs);
433
434     for (i = 0; i < 4; i++) {
435         for (j = 0; j < 4; j++) {
436             u[i * 4 + j] = r_tmp->u[i][j];
437             v[i * 4 + j] = r_tmp->v[i][j];
438             ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
439         }
440     }
441 }
442
443 /**
444  * Modulo operation with only positive remainders.
445  *
446  * @param a dividend
447  * @param b divisor
448  *
449  * @return positive remainder of (a / b)
450  */
451 static inline int mod(int a, int b)
452 {
453     const int res = a % b;
454     if (res < 0) {
455         return res + b;
456     } else {
457         return res;
458     }
459 }
460
461 /**
462  * Convert char to corresponding direction.
463  * Used for cubemap options.
464  */
465 static int get_direction(char c)
466 {
467     switch (c) {
468     case 'r':
469         return RIGHT;
470     case 'l':
471         return LEFT;
472     case 'u':
473         return UP;
474     case 'd':
475         return DOWN;
476     case 'f':
477         return FRONT;
478     case 'b':
479         return BACK;
480     default:
481         return -1;
482     }
483 }
484
485 /**
486  * Convert char to corresponding rotation angle.
487  * Used for cubemap options.
488  */
489 static int get_rotation(char c)
490 {
491     switch (c) {
492     case '0':
493         return ROT_0;
494     case '1':
495         return ROT_90;
496     case '2':
497         return ROT_180;
498     case '3':
499         return ROT_270;
500     default:
501         return -1;
502     }
503 }
504
505 /**
506  * Convert char to corresponding rotation order.
507  */
508 static int get_rorder(char c)
509 {
510     switch (c) {
511     case 'Y':
512     case 'y':
513         return YAW;
514     case 'P':
515     case 'p':
516         return PITCH;
517     case 'R':
518     case 'r':
519         return ROLL;
520     default:
521         return -1;
522     }
523 }
524
525 /**
526  * Prepare data for processing cubemap input format.
527  *
528  * @param ctx filter context
529  *
530  * @return error code
531  */
532 static int prepare_cube_in(AVFilterContext *ctx)
533 {
534     V360Context *s = ctx->priv;
535
536     for (int face = 0; face < NB_FACES; face++) {
537         const char c = s->in_forder[face];
538         int direction;
539
540         if (c == '\0') {
541             av_log(ctx, AV_LOG_ERROR,
542                    "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
543             return AVERROR(EINVAL);
544         }
545
546         direction = get_direction(c);
547         if (direction == -1) {
548             av_log(ctx, AV_LOG_ERROR,
549                    "Incorrect direction symbol '%c' in in_forder option.\n", c);
550             return AVERROR(EINVAL);
551         }
552
553         s->in_cubemap_face_order[direction] = face;
554     }
555
556     for (int face = 0; face < NB_FACES; face++) {
557         const char c = s->in_frot[face];
558         int rotation;
559
560         if (c == '\0') {
561             av_log(ctx, AV_LOG_ERROR,
562                    "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
563             return AVERROR(EINVAL);
564         }
565
566         rotation = get_rotation(c);
567         if (rotation == -1) {
568             av_log(ctx, AV_LOG_ERROR,
569                    "Incorrect rotation symbol '%c' in in_frot option.\n", c);
570             return AVERROR(EINVAL);
571         }
572
573         s->in_cubemap_face_rotation[face] = rotation;
574     }
575
576     return 0;
577 }
578
579 /**
580  * Prepare data for processing cubemap output format.
581  *
582  * @param ctx filter context
583  *
584  * @return error code
585  */
586 static int prepare_cube_out(AVFilterContext *ctx)
587 {
588     V360Context *s = ctx->priv;
589
590     for (int face = 0; face < NB_FACES; face++) {
591         const char c = s->out_forder[face];
592         int direction;
593
594         if (c == '\0') {
595             av_log(ctx, AV_LOG_ERROR,
596                    "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
597             return AVERROR(EINVAL);
598         }
599
600         direction = get_direction(c);
601         if (direction == -1) {
602             av_log(ctx, AV_LOG_ERROR,
603                    "Incorrect direction symbol '%c' in out_forder option.\n", c);
604             return AVERROR(EINVAL);
605         }
606
607         s->out_cubemap_direction_order[face] = direction;
608     }
609
610     for (int face = 0; face < NB_FACES; face++) {
611         const char c = s->out_frot[face];
612         int rotation;
613
614         if (c == '\0') {
615             av_log(ctx, AV_LOG_ERROR,
616                    "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
617             return AVERROR(EINVAL);
618         }
619
620         rotation = get_rotation(c);
621         if (rotation == -1) {
622             av_log(ctx, AV_LOG_ERROR,
623                    "Incorrect rotation symbol '%c' in out_frot option.\n", c);
624             return AVERROR(EINVAL);
625         }
626
627         s->out_cubemap_face_rotation[face] = rotation;
628     }
629
630     return 0;
631 }
632
633 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
634 {
635     float tmp;
636
637     switch (rotation) {
638     case ROT_0:
639         break;
640     case ROT_90:
641         tmp =  *uf;
642         *uf = -*vf;
643         *vf =  tmp;
644         break;
645     case ROT_180:
646         *uf = -*uf;
647         *vf = -*vf;
648         break;
649     case ROT_270:
650         tmp = -*uf;
651         *uf =  *vf;
652         *vf =  tmp;
653         break;
654     default:
655         av_assert0(0);
656     }
657 }
658
659 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
660 {
661     float tmp;
662
663     switch (rotation) {
664     case ROT_0:
665         break;
666     case ROT_90:
667         tmp = -*uf;
668         *uf =  *vf;
669         *vf =  tmp;
670         break;
671     case ROT_180:
672         *uf = -*uf;
673         *vf = -*vf;
674         break;
675     case ROT_270:
676         tmp =  *uf;
677         *uf = -*vf;
678         *vf =  tmp;
679         break;
680     default:
681         av_assert0(0);
682     }
683 }
684
685 /**
686  * Calculate 3D coordinates on sphere for corresponding cubemap position.
687  * Common operation for every cubemap.
688  *
689  * @param s filter context
690  * @param uf horizontal cubemap coordinate [0, 1)
691  * @param vf vertical cubemap coordinate [0, 1)
692  * @param face face of cubemap
693  * @param vec coordinates on sphere
694  */
695 static void cube_to_xyz(const V360Context *s,
696                         float uf, float vf, int face,
697                         float *vec)
698 {
699     const int direction = s->out_cubemap_direction_order[face];
700     float norm;
701     float l_x, l_y, l_z;
702
703     uf /= (1.f - s->out_pad);
704     vf /= (1.f - s->out_pad);
705
706     rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
707
708     switch (direction) {
709     case RIGHT:
710         l_x =  1.f;
711         l_y = -vf;
712         l_z =  uf;
713         break;
714     case LEFT:
715         l_x = -1.f;
716         l_y = -vf;
717         l_z = -uf;
718         break;
719     case UP:
720         l_x =  uf;
721         l_y =  1.f;
722         l_z = -vf;
723         break;
724     case DOWN:
725         l_x =  uf;
726         l_y = -1.f;
727         l_z =  vf;
728         break;
729     case FRONT:
730         l_x =  uf;
731         l_y = -vf;
732         l_z = -1.f;
733         break;
734     case BACK:
735         l_x = -uf;
736         l_y = -vf;
737         l_z =  1.f;
738         break;
739     }
740
741     norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
742     vec[0] = l_x / norm;
743     vec[1] = l_y / norm;
744     vec[2] = l_z / norm;
745 }
746
747 /**
748  * Calculate cubemap position for corresponding 3D coordinates on sphere.
749  * Common operation for every cubemap.
750  *
751  * @param s filter context
752  * @param vec coordinated on sphere
753  * @param uf horizontal cubemap coordinate [0, 1)
754  * @param vf vertical cubemap coordinate [0, 1)
755  * @param direction direction of view
756  */
757 static void xyz_to_cube(const V360Context *s,
758                         const float *vec,
759                         float *uf, float *vf, int *direction)
760 {
761     const float phi   = atan2f(vec[0], -vec[2]);
762     const float theta = asinf(-vec[1]);
763     float phi_norm, theta_threshold;
764     int face;
765
766     if (phi >= -M_PI_4 && phi < M_PI_4) {
767         *direction = FRONT;
768         phi_norm = phi;
769     } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
770         *direction = LEFT;
771         phi_norm = phi + M_PI_2;
772     } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
773         *direction = RIGHT;
774         phi_norm = phi - M_PI_2;
775     } else {
776         *direction = BACK;
777         phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
778     }
779
780     theta_threshold = atanf(cosf(phi_norm));
781     if (theta > theta_threshold) {
782         *direction = DOWN;
783     } else if (theta < -theta_threshold) {
784         *direction = UP;
785     }
786
787     switch (*direction) {
788     case RIGHT:
789         *uf =  vec[2] / vec[0];
790         *vf = -vec[1] / vec[0];
791         break;
792     case LEFT:
793         *uf =  vec[2] / vec[0];
794         *vf =  vec[1] / vec[0];
795         break;
796     case UP:
797         *uf =  vec[0] / vec[1];
798         *vf = -vec[2] / vec[1];
799         break;
800     case DOWN:
801         *uf = -vec[0] / vec[1];
802         *vf = -vec[2] / vec[1];
803         break;
804     case FRONT:
805         *uf = -vec[0] / vec[2];
806         *vf =  vec[1] / vec[2];
807         break;
808     case BACK:
809         *uf = -vec[0] / vec[2];
810         *vf = -vec[1] / vec[2];
811         break;
812     default:
813         av_assert0(0);
814     }
815
816     face = s->in_cubemap_face_order[*direction];
817     rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
818 }
819
820 /**
821  * Find position on another cube face in case of overflow/underflow.
822  * Used for calculation of interpolation window.
823  *
824  * @param s filter context
825  * @param uf horizontal cubemap coordinate
826  * @param vf vertical cubemap coordinate
827  * @param direction direction of view
828  * @param new_uf new horizontal cubemap coordinate
829  * @param new_vf new vertical cubemap coordinate
830  * @param face face position on cubemap
831  */
832 static void process_cube_coordinates(const V360Context *s,
833                                      float uf, float vf, int direction,
834                                      float *new_uf, float *new_vf, int *face)
835 {
836     /*
837      *  Cubemap orientation
838      *
839      *           width
840      *         <------->
841      *         +-------+
842      *         |       |                              U
843      *         | up    |                   h       ------->
844      * +-------+-------+-------+-------+ ^ e      |
845      * |       |       |       |       | | i    V |
846      * | left  | front | right | back  | | g      |
847      * +-------+-------+-------+-------+ v h      v
848      *         |       |                   t
849      *         | down  |
850      *         +-------+
851      */
852
853     *face = s->in_cubemap_face_order[direction];
854     rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
855
856     if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
857         // There are no pixels to use in this case
858         *new_uf = uf;
859         *new_vf = vf;
860     } else if (uf < -1.f) {
861         uf += 2.f;
862         switch (direction) {
863         case RIGHT:
864             direction = FRONT;
865             *new_uf =  uf;
866             *new_vf =  vf;
867             break;
868         case LEFT:
869             direction = BACK;
870             *new_uf =  uf;
871             *new_vf =  vf;
872             break;
873         case UP:
874             direction = LEFT;
875             *new_uf =  vf;
876             *new_vf = -uf;
877             break;
878         case DOWN:
879             direction = LEFT;
880             *new_uf = -vf;
881             *new_vf =  uf;
882             break;
883         case FRONT:
884             direction = LEFT;
885             *new_uf =  uf;
886             *new_vf =  vf;
887             break;
888         case BACK:
889             direction = RIGHT;
890             *new_uf =  uf;
891             *new_vf =  vf;
892             break;
893         default:
894             av_assert0(0);
895         }
896     } else if (uf >= 1.f) {
897         uf -= 2.f;
898         switch (direction) {
899         case RIGHT:
900             direction = BACK;
901             *new_uf =  uf;
902             *new_vf =  vf;
903             break;
904         case LEFT:
905             direction = FRONT;
906             *new_uf =  uf;
907             *new_vf =  vf;
908             break;
909         case UP:
910             direction = RIGHT;
911             *new_uf = -vf;
912             *new_vf =  uf;
913             break;
914         case DOWN:
915             direction = RIGHT;
916             *new_uf =  vf;
917             *new_vf = -uf;
918             break;
919         case FRONT:
920             direction = RIGHT;
921             *new_uf =  uf;
922             *new_vf =  vf;
923             break;
924         case BACK:
925             direction = LEFT;
926             *new_uf =  uf;
927             *new_vf =  vf;
928             break;
929         default:
930             av_assert0(0);
931         }
932     } else if (vf < -1.f) {
933         vf += 2.f;
934         switch (direction) {
935         case RIGHT:
936             direction = UP;
937             *new_uf =  vf;
938             *new_vf = -uf;
939             break;
940         case LEFT:
941             direction = UP;
942             *new_uf = -vf;
943             *new_vf =  uf;
944             break;
945         case UP:
946             direction = BACK;
947             *new_uf = -uf;
948             *new_vf = -vf;
949             break;
950         case DOWN:
951             direction = FRONT;
952             *new_uf =  uf;
953             *new_vf =  vf;
954             break;
955         case FRONT:
956             direction = UP;
957             *new_uf =  uf;
958             *new_vf =  vf;
959             break;
960         case BACK:
961             direction = UP;
962             *new_uf = -uf;
963             *new_vf = -vf;
964             break;
965         default:
966             av_assert0(0);
967         }
968     } else if (vf >= 1.f) {
969         vf -= 2.f;
970         switch (direction) {
971         case RIGHT:
972             direction = DOWN;
973             *new_uf = -vf;
974             *new_vf =  uf;
975             break;
976         case LEFT:
977             direction = DOWN;
978             *new_uf =  vf;
979             *new_vf = -uf;
980             break;
981         case UP:
982             direction = FRONT;
983             *new_uf =  uf;
984             *new_vf =  vf;
985             break;
986         case DOWN:
987             direction = BACK;
988             *new_uf = -uf;
989             *new_vf = -vf;
990             break;
991         case FRONT:
992             direction = DOWN;
993             *new_uf =  uf;
994             *new_vf =  vf;
995             break;
996         case BACK:
997             direction = DOWN;
998             *new_uf = -uf;
999             *new_vf = -vf;
1000             break;
1001         default:
1002             av_assert0(0);
1003         }
1004     } else {
1005         // Inside cube face
1006         *new_uf = uf;
1007         *new_vf = vf;
1008     }
1009
1010     *face = s->in_cubemap_face_order[direction];
1011     rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1012 }
1013
1014 /**
1015  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1016  *
1017  * @param s filter context
1018  * @param i horizontal position on frame [0, height)
1019  * @param j vertical position on frame [0, width)
1020  * @param width frame width
1021  * @param height frame height
1022  * @param vec coordinates on sphere
1023  */
1024 static void cube3x2_to_xyz(const V360Context *s,
1025                            int i, int j, int width, int height,
1026                            float *vec)
1027 {
1028     const float ew = width  / 3.f;
1029     const float eh = height / 2.f;
1030
1031     const int u_face = floorf(i / ew);
1032     const int v_face = floorf(j / eh);
1033     const int face = u_face + 3 * v_face;
1034
1035     const int u_shift = ceilf(ew * u_face);
1036     const int v_shift = ceilf(eh * v_face);
1037     const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1038     const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1039
1040     const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1041     const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1042
1043     cube_to_xyz(s, uf, vf, face, vec);
1044 }
1045
1046 /**
1047  * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1048  *
1049  * @param s filter context
1050  * @param vec coordinates on sphere
1051  * @param width frame width
1052  * @param height frame height
1053  * @param us horizontal coordinates for interpolation window
1054  * @param vs vertical coordinates for interpolation window
1055  * @param du horizontal relative coordinate
1056  * @param dv vertical relative coordinate
1057  */
1058 static void xyz_to_cube3x2(const V360Context *s,
1059                            const float *vec, int width, int height,
1060                            uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1061 {
1062     const float ew = width  / 3.f;
1063     const float eh = height / 2.f;
1064     float uf, vf;
1065     int ui, vi;
1066     int ewi, ehi;
1067     int i, j;
1068     int direction, face;
1069     int u_face, v_face;
1070
1071     xyz_to_cube(s, vec, &uf, &vf, &direction);
1072
1073     uf *= (1.f - s->in_pad);
1074     vf *= (1.f - s->in_pad);
1075
1076     face = s->in_cubemap_face_order[direction];
1077     u_face = face % 3;
1078     v_face = face / 3;
1079     ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1080     ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1081
1082     uf = 0.5f * ewi * (uf + 1.f);
1083     vf = 0.5f * ehi * (vf + 1.f);
1084
1085     ui = floorf(uf);
1086     vi = floorf(vf);
1087
1088     *du = uf - ui;
1089     *dv = vf - vi;
1090
1091     for (i = -1; i < 3; i++) {
1092         for (j = -1; j < 3; j++) {
1093             int new_ui = ui + j;
1094             int new_vi = vi + i;
1095             int u_shift, v_shift;
1096             int new_ewi, new_ehi;
1097
1098             if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1099                 face = s->in_cubemap_face_order[direction];
1100
1101                 u_face = face % 3;
1102                 v_face = face / 3;
1103                 u_shift = ceilf(ew * u_face);
1104                 v_shift = ceilf(eh * v_face);
1105             } else {
1106                 uf = 2.f * new_ui / ewi - 1.f;
1107                 vf = 2.f * new_vi / ehi - 1.f;
1108
1109                 uf /= (1.f - s->in_pad);
1110                 vf /= (1.f - s->in_pad);
1111
1112                 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1113
1114                 uf *= (1.f - s->in_pad);
1115                 vf *= (1.f - s->in_pad);
1116
1117                 u_face = face % 3;
1118                 v_face = face / 3;
1119                 u_shift = ceilf(ew * u_face);
1120                 v_shift = ceilf(eh * v_face);
1121                 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1122                 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1123
1124                 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1125                 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1126             }
1127
1128
1129             us[i + 1][j + 1] = u_shift + new_ui;
1130             vs[i + 1][j + 1] = v_shift + new_vi;
1131         }
1132     }
1133 }
1134
1135 /**
1136  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1137  *
1138  * @param s filter context
1139  * @param i horizontal position on frame [0, height)
1140  * @param j vertical position on frame [0, width)
1141  * @param width frame width
1142  * @param height frame height
1143  * @param vec coordinates on sphere
1144  */
1145 static void cube1x6_to_xyz(const V360Context *s,
1146                            int i, int j, int width, int height,
1147                            float *vec)
1148 {
1149     const float ew = width;
1150     const float eh = height / 6.f;
1151
1152     const int face = floorf(j / eh);
1153
1154     const int v_shift = ceilf(eh * face);
1155     const int ehi = ceilf(eh * (face + 1)) - v_shift;
1156
1157     const float uf = 2.f *  i            / ew  - 1.f;
1158     const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1159
1160     cube_to_xyz(s, uf, vf, face, vec);
1161 }
1162
1163 /**
1164  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1165  *
1166  * @param s filter context
1167  * @param i horizontal position on frame [0, height)
1168  * @param j vertical position on frame [0, width)
1169  * @param width frame width
1170  * @param height frame height
1171  * @param vec coordinates on sphere
1172  */
1173 static void cube6x1_to_xyz(const V360Context *s,
1174                            int i, int j, int width, int height,
1175                            float *vec)
1176 {
1177     const float ew = width / 6.f;
1178     const float eh = height;
1179
1180     const int face = floorf(i / ew);
1181
1182     const int u_shift = ceilf(ew * face);
1183     const int ewi = ceilf(ew * (face + 1)) - u_shift;
1184
1185     const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1186     const float vf = 2.f *  j            / eh  - 1.f;
1187
1188     cube_to_xyz(s, uf, vf, face, vec);
1189 }
1190
1191 /**
1192  * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1193  *
1194  * @param s filter context
1195  * @param vec coordinates on sphere
1196  * @param width frame width
1197  * @param height frame height
1198  * @param us horizontal coordinates for interpolation window
1199  * @param vs vertical coordinates for interpolation window
1200  * @param du horizontal relative coordinate
1201  * @param dv vertical relative coordinate
1202  */
1203 static void xyz_to_cube1x6(const V360Context *s,
1204                            const float *vec, int width, int height,
1205                            uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1206 {
1207     const float eh = height / 6.f;
1208     const int ewi = width;
1209     float uf, vf;
1210     int ui, vi;
1211     int ehi;
1212     int i, j;
1213     int direction, face;
1214
1215     xyz_to_cube(s, vec, &uf, &vf, &direction);
1216
1217     uf *= (1.f - s->in_pad);
1218     vf *= (1.f - s->in_pad);
1219
1220     face = s->in_cubemap_face_order[direction];
1221     ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1222
1223     uf = 0.5f * ewi * (uf + 1.f);
1224     vf = 0.5f * ehi * (vf + 1.f);
1225
1226     ui = floorf(uf);
1227     vi = floorf(vf);
1228
1229     *du = uf - ui;
1230     *dv = vf - vi;
1231
1232     for (i = -1; i < 3; i++) {
1233         for (j = -1; j < 3; j++) {
1234             int new_ui = ui + j;
1235             int new_vi = vi + i;
1236             int v_shift;
1237             int new_ehi;
1238
1239             if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1240                 face = s->in_cubemap_face_order[direction];
1241
1242                 v_shift = ceilf(eh * face);
1243             } else {
1244                 uf = 2.f * new_ui / ewi - 1.f;
1245                 vf = 2.f * new_vi / ehi - 1.f;
1246
1247                 uf /= (1.f - s->in_pad);
1248                 vf /= (1.f - s->in_pad);
1249
1250                 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1251
1252                 uf *= (1.f - s->in_pad);
1253                 vf *= (1.f - s->in_pad);
1254
1255                 v_shift = ceilf(eh * face);
1256                 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1257
1258                 new_ui = av_clip(roundf(0.5f *     ewi * (uf + 1.f)), 0,     ewi - 1);
1259                 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1260             }
1261
1262
1263             us[i + 1][j + 1] =           new_ui;
1264             vs[i + 1][j + 1] = v_shift + new_vi;
1265         }
1266     }
1267 }
1268
1269 /**
1270  * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1271  *
1272  * @param s filter context
1273  * @param vec coordinates on sphere
1274  * @param width frame width
1275  * @param height frame height
1276  * @param us horizontal coordinates for interpolation window
1277  * @param vs vertical coordinates for interpolation window
1278  * @param du horizontal relative coordinate
1279  * @param dv vertical relative coordinate
1280  */
1281 static void xyz_to_cube6x1(const V360Context *s,
1282                            const float *vec, int width, int height,
1283                            uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1284 {
1285     const float ew = width / 6.f;
1286     const int ehi = height;
1287     float uf, vf;
1288     int ui, vi;
1289     int ewi;
1290     int i, j;
1291     int direction, face;
1292
1293     xyz_to_cube(s, vec, &uf, &vf, &direction);
1294
1295     uf *= (1.f - s->in_pad);
1296     vf *= (1.f - s->in_pad);
1297
1298     face = s->in_cubemap_face_order[direction];
1299     ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1300
1301     uf = 0.5f * ewi * (uf + 1.f);
1302     vf = 0.5f * ehi * (vf + 1.f);
1303
1304     ui = floorf(uf);
1305     vi = floorf(vf);
1306
1307     *du = uf - ui;
1308     *dv = vf - vi;
1309
1310     for (i = -1; i < 3; i++) {
1311         for (j = -1; j < 3; j++) {
1312             int new_ui = ui + j;
1313             int new_vi = vi + i;
1314             int u_shift;
1315             int new_ewi;
1316
1317             if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1318                 face = s->in_cubemap_face_order[direction];
1319
1320                 u_shift = ceilf(ew * face);
1321             } else {
1322                 uf = 2.f * new_ui / ewi - 1.f;
1323                 vf = 2.f * new_vi / ehi - 1.f;
1324
1325                 uf /= (1.f - s->in_pad);
1326                 vf /= (1.f - s->in_pad);
1327
1328                 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1329
1330                 uf *= (1.f - s->in_pad);
1331                 vf *= (1.f - s->in_pad);
1332
1333                 u_shift = ceilf(ew * face);
1334                 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1335
1336                 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1337                 new_vi = av_clip(roundf(0.5f *     ehi * (vf + 1.f)), 0,     ehi - 1);
1338             }
1339
1340
1341             us[i + 1][j + 1] = u_shift + new_ui;
1342             vs[i + 1][j + 1] =           new_vi;
1343         }
1344     }
1345 }
1346
1347 /**
1348  * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1349  *
1350  * @param s filter context
1351  * @param i horizontal position on frame [0, height)
1352  * @param j vertical position on frame [0, width)
1353  * @param width frame width
1354  * @param height frame height
1355  * @param vec coordinates on sphere
1356  */
1357 static void equirect_to_xyz(const V360Context *s,
1358                             int i, int j, int width, int height,
1359                             float *vec)
1360 {
1361     const float phi   = ((2.f * i) / width  - 1.f) * M_PI;
1362     const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1363
1364     const float sin_phi   = sinf(phi);
1365     const float cos_phi   = cosf(phi);
1366     const float sin_theta = sinf(theta);
1367     const float cos_theta = cosf(theta);
1368
1369     vec[0] =  cos_theta * sin_phi;
1370     vec[1] = -sin_theta;
1371     vec[2] = -cos_theta * cos_phi;
1372 }
1373
1374 /**
1375  * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1376  *
1377  * @param s filter context
1378  * @param vec coordinates on sphere
1379  * @param width frame width
1380  * @param height frame height
1381  * @param us horizontal coordinates for interpolation window
1382  * @param vs vertical coordinates for interpolation window
1383  * @param du horizontal relative coordinate
1384  * @param dv vertical relative coordinate
1385  */
1386 static void xyz_to_equirect(const V360Context *s,
1387                             const float *vec, int width, int height,
1388                             uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1389 {
1390     const float phi   = atan2f(vec[0], -vec[2]);
1391     const float theta = asinf(-vec[1]);
1392     float uf, vf;
1393     int ui, vi;
1394     int i, j;
1395
1396     uf = (phi   / M_PI   + 1.f) * width  / 2.f;
1397     vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1398     ui = floorf(uf);
1399     vi = floorf(vf);
1400
1401     *du = uf - ui;
1402     *dv = vf - vi;
1403
1404     for (i = -1; i < 3; i++) {
1405         for (j = -1; j < 3; j++) {
1406             us[i + 1][j + 1] = mod(ui + j, width);
1407             vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1408         }
1409     }
1410 }
1411
1412 /**
1413  * Prepare data for processing equi-angular cubemap input format.
1414  *
1415  * @param ctx filter context
1416
1417  * @return error code
1418  */
1419 static int prepare_eac_in(AVFilterContext *ctx)
1420 {
1421     V360Context *s = ctx->priv;
1422
1423     s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1424     s->in_cubemap_face_order[LEFT]  = TOP_LEFT;
1425     s->in_cubemap_face_order[UP]    = BOTTOM_RIGHT;
1426     s->in_cubemap_face_order[DOWN]  = BOTTOM_LEFT;
1427     s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1428     s->in_cubemap_face_order[BACK]  = BOTTOM_MIDDLE;
1429
1430     s->in_cubemap_face_rotation[TOP_LEFT]      = ROT_0;
1431     s->in_cubemap_face_rotation[TOP_MIDDLE]    = ROT_0;
1432     s->in_cubemap_face_rotation[TOP_RIGHT]     = ROT_0;
1433     s->in_cubemap_face_rotation[BOTTOM_LEFT]   = ROT_270;
1434     s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1435     s->in_cubemap_face_rotation[BOTTOM_RIGHT]  = ROT_270;
1436
1437     return 0;
1438 }
1439
1440 /**
1441  * Prepare data for processing equi-angular cubemap output format.
1442  *
1443  * @param ctx filter context
1444  *
1445  * @return error code
1446  */
1447 static int prepare_eac_out(AVFilterContext *ctx)
1448 {
1449     V360Context *s = ctx->priv;
1450
1451     s->out_cubemap_direction_order[TOP_LEFT]      = LEFT;
1452     s->out_cubemap_direction_order[TOP_MIDDLE]    = FRONT;
1453     s->out_cubemap_direction_order[TOP_RIGHT]     = RIGHT;
1454     s->out_cubemap_direction_order[BOTTOM_LEFT]   = DOWN;
1455     s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1456     s->out_cubemap_direction_order[BOTTOM_RIGHT]  = UP;
1457
1458     s->out_cubemap_face_rotation[TOP_LEFT]      = ROT_0;
1459     s->out_cubemap_face_rotation[TOP_MIDDLE]    = ROT_0;
1460     s->out_cubemap_face_rotation[TOP_RIGHT]     = ROT_0;
1461     s->out_cubemap_face_rotation[BOTTOM_LEFT]   = ROT_270;
1462     s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1463     s->out_cubemap_face_rotation[BOTTOM_RIGHT]  = ROT_270;
1464
1465     return 0;
1466 }
1467
1468 /**
1469  * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1470  *
1471  * @param s filter context
1472  * @param i horizontal position on frame [0, height)
1473  * @param j vertical position on frame [0, width)
1474  * @param width frame width
1475  * @param height frame height
1476  * @param vec coordinates on sphere
1477  */
1478 static void eac_to_xyz(const V360Context *s,
1479                        int i, int j, int width, int height,
1480                        float *vec)
1481 {
1482     const float pixel_pad = 2;
1483     const float u_pad = pixel_pad / width;
1484     const float v_pad = pixel_pad / height;
1485
1486     int u_face, v_face, face;
1487
1488     float l_x, l_y, l_z;
1489     float norm;
1490
1491     float uf = (float)i / width;
1492     float vf = (float)j / height;
1493
1494     // EAC has 2-pixel padding on faces except between faces on the same row
1495     // Padding pixels seems not to be stretched with tangent as regular pixels
1496     // Formulas below approximate original padding as close as I could get experimentally
1497
1498     // Horizontal padding
1499     uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1500     if (uf < 0.f) {
1501         u_face = 0;
1502         uf -= 0.5f;
1503     } else if (uf >= 3.f) {
1504         u_face = 2;
1505         uf -= 2.5f;
1506     } else {
1507         u_face = floorf(uf);
1508         uf = fmodf(uf, 1.f) - 0.5f;
1509     }
1510
1511     // Vertical padding
1512     v_face = floorf(vf * 2.f);
1513     vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1514
1515     if (uf >= -0.5f && uf < 0.5f) {
1516         uf = tanf(M_PI_2 * uf);
1517     } else {
1518         uf = 2.f * uf;
1519     }
1520     if (vf >= -0.5f && vf < 0.5f) {
1521         vf = tanf(M_PI_2 * vf);
1522     } else {
1523         vf = 2.f * vf;
1524     }
1525
1526     face = u_face + 3 * v_face;
1527
1528     switch (face) {
1529     case TOP_LEFT:
1530         l_x = -1.f;
1531         l_y = -vf;
1532         l_z = -uf;
1533         break;
1534     case TOP_MIDDLE:
1535         l_x =  uf;
1536         l_y = -vf;
1537         l_z = -1.f;
1538         break;
1539     case TOP_RIGHT:
1540         l_x =  1.f;
1541         l_y = -vf;
1542         l_z =  uf;
1543         break;
1544     case BOTTOM_LEFT:
1545         l_x = -vf;
1546         l_y = -1.f;
1547         l_z =  uf;
1548         break;
1549     case BOTTOM_MIDDLE:
1550         l_x = -vf;
1551         l_y =  uf;
1552         l_z =  1.f;
1553         break;
1554     case BOTTOM_RIGHT:
1555         l_x = -vf;
1556         l_y =  1.f;
1557         l_z = -uf;
1558         break;
1559     default:
1560         av_assert0(0);
1561     }
1562
1563     norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1564     vec[0] = l_x / norm;
1565     vec[1] = l_y / norm;
1566     vec[2] = l_z / norm;
1567 }
1568
1569 /**
1570  * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
1571  *
1572  * @param s filter context
1573  * @param vec coordinates on sphere
1574  * @param width frame width
1575  * @param height frame height
1576  * @param us horizontal coordinates for interpolation window
1577  * @param vs vertical coordinates for interpolation window
1578  * @param du horizontal relative coordinate
1579  * @param dv vertical relative coordinate
1580  */
1581 static void xyz_to_eac(const V360Context *s,
1582                        const float *vec, int width, int height,
1583                        uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1584 {
1585     const float pixel_pad = 2;
1586     const float u_pad = pixel_pad / width;
1587     const float v_pad = pixel_pad / height;
1588
1589     float uf, vf;
1590     int ui, vi;
1591     int i, j;
1592     int direction, face;
1593     int u_face, v_face;
1594
1595     xyz_to_cube(s, vec, &uf, &vf, &direction);
1596
1597     face = s->in_cubemap_face_order[direction];
1598     u_face = face % 3;
1599     v_face = face / 3;
1600
1601     uf = M_2_PI * atanf(uf) + 0.5f;
1602     vf = M_2_PI * atanf(vf) + 0.5f;
1603
1604     // These formulas are inversed from eac_to_xyz ones
1605     uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
1606     vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
1607
1608     uf *= width;
1609     vf *= height;
1610
1611     ui = floorf(uf);
1612     vi = floorf(vf);
1613
1614     *du = uf - ui;
1615     *dv = vf - vi;
1616
1617     for (i = -1; i < 3; i++) {
1618         for (j = -1; j < 3; j++) {
1619             us[i + 1][j + 1] = av_clip(ui + j, 0, width  - 1);
1620             vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1621         }
1622     }
1623 }
1624
1625 /**
1626  * Prepare data for processing flat output format.
1627  *
1628  * @param ctx filter context
1629  *
1630  * @return error code
1631  */
1632 static int prepare_flat_out(AVFilterContext *ctx)
1633 {
1634     V360Context *s = ctx->priv;
1635
1636     const float h_angle = 0.5f * s->h_fov * M_PI / 180.f;
1637     const float v_angle = 0.5f * s->v_fov * M_PI / 180.f;
1638
1639     const float sin_phi   = sinf(h_angle);
1640     const float cos_phi   = cosf(h_angle);
1641     const float sin_theta = sinf(v_angle);
1642     const float cos_theta = cosf(v_angle);
1643
1644     s->flat_range[0] =  cos_theta * sin_phi;
1645     s->flat_range[1] =  sin_theta;
1646     s->flat_range[2] = -cos_theta * cos_phi;
1647
1648     return 0;
1649 }
1650
1651 /**
1652  * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
1653  *
1654  * @param s filter context
1655  * @param i horizontal position on frame [0, height)
1656  * @param j vertical position on frame [0, width)
1657  * @param width frame width
1658  * @param height frame height
1659  * @param vec coordinates on sphere
1660  */
1661 static void flat_to_xyz(const V360Context *s,
1662                         int i, int j, int width, int height,
1663                         float *vec)
1664 {
1665     const float l_x =  s->flat_range[0] * (2.f * i / width  - 1.f);
1666     const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
1667     const float l_z =  s->flat_range[2];
1668
1669     const float norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1670
1671     vec[0] = l_x / norm;
1672     vec[1] = l_y / norm;
1673     vec[2] = l_z / norm;
1674 }
1675
1676 /**
1677  * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
1678  *
1679  * @param s filter context
1680  * @param vec coordinates on sphere
1681  * @param width frame width
1682  * @param height frame height
1683  * @param us horizontal coordinates for interpolation window
1684  * @param vs vertical coordinates for interpolation window
1685  * @param du horizontal relative coordinate
1686  * @param dv vertical relative coordinate
1687  */
1688 static void xyz_to_dfisheye(const V360Context *s,
1689                             const float *vec, int width, int height,
1690                             uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1691 {
1692     const float scale = 1.f - s->in_pad;
1693
1694     const float ew = width / 2.f;
1695     const float eh = height;
1696
1697     const float phi   = atan2f(-vec[1], -vec[0]);
1698     const float theta = acosf(fabsf(vec[2])) / M_PI;
1699
1700     float uf = (theta * cosf(phi) * scale + 0.5f) * ew;
1701     float vf = (theta * sinf(phi) * scale + 0.5f) * eh;
1702
1703     int ui, vi;
1704     int u_shift;
1705     int i, j;
1706
1707     if (vec[2] >= 0) {
1708         u_shift = 0;
1709     } else {
1710         u_shift = ceilf(ew);
1711         uf = ew - uf;
1712     }
1713
1714     ui = floorf(uf);
1715     vi = floorf(vf);
1716
1717     *du = uf - ui;
1718     *dv = vf - vi;
1719
1720     for (i = -1; i < 3; i++) {
1721         for (j = -1; j < 3; j++) {
1722             us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width  - 1);
1723             vs[i + 1][j + 1] = av_clip(          vi + i, 0, height - 1);
1724         }
1725     }
1726 }
1727
1728 /**
1729  * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
1730  *
1731  * @param s filter context
1732  * @param i horizontal position on frame [0, height)
1733  * @param j vertical position on frame [0, width)
1734  * @param width frame width
1735  * @param height frame height
1736  * @param vec coordinates on sphere
1737  */
1738 static void barrel_to_xyz(const V360Context *s,
1739                           int i, int j, int width, int height,
1740                           float *vec)
1741 {
1742     const float scale = 0.99f;
1743     float l_x, l_y, l_z;
1744
1745     if (i < 4 * width / 5) {
1746         const float theta_range = M_PI / 4.f;
1747
1748         const int ew = 4 * width / 5;
1749         const int eh = height;
1750
1751         const float phi   = ((2.f * i) / ew - 1.f) * M_PI        / scale;
1752         const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
1753
1754         const float sin_phi   = sinf(phi);
1755         const float cos_phi   = cosf(phi);
1756         const float sin_theta = sinf(theta);
1757         const float cos_theta = cosf(theta);
1758
1759         l_x =  cos_theta * sin_phi;
1760         l_y = -sin_theta;
1761         l_z = -cos_theta * cos_phi;
1762     } else {
1763         const int ew = width  / 5;
1764         const int eh = height / 2;
1765
1766         float uf, vf;
1767         float norm;
1768
1769         if (j < eh) {   // UP
1770             uf = 2.f * (i - 4 * ew) / ew  - 1.f;
1771             vf = 2.f * (j         ) / eh - 1.f;
1772
1773             uf /= scale;
1774             vf /= scale;
1775
1776             l_x =  uf;
1777             l_y =  1.f;
1778             l_z = -vf;
1779         } else {            // DOWN
1780             uf = 2.f * (i - 4 * ew) / ew - 1.f;
1781             vf = 2.f * (j -     eh) / eh - 1.f;
1782
1783             uf /= scale;
1784             vf /= scale;
1785
1786             l_x =  uf;
1787             l_y = -1.f;
1788             l_z =  vf;
1789         }
1790
1791         norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z);
1792
1793         l_x /= norm;
1794         l_y /= norm;
1795         l_z /= norm;
1796     }
1797
1798     vec[0] = l_x;
1799     vec[1] = l_y;
1800     vec[2] = l_z;
1801 }
1802
1803 /**
1804  * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
1805  *
1806  * @param s filter context
1807  * @param vec coordinates on sphere
1808  * @param width frame width
1809  * @param height frame height
1810  * @param us horizontal coordinates for interpolation window
1811  * @param vs vertical coordinates for interpolation window
1812  * @param du horizontal relative coordinate
1813  * @param dv vertical relative coordinate
1814  */
1815 static void xyz_to_barrel(const V360Context *s,
1816                           const float *vec, int width, int height,
1817                           uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1818 {
1819     const float scale = 0.99f;
1820
1821     const float phi   = atan2f(vec[0], -vec[2]);
1822     const float theta = asinf(-vec[1]);
1823     const float theta_range = M_PI / 4.f;
1824
1825     int ew, eh;
1826     int u_shift, v_shift;
1827     float uf, vf;
1828     int ui, vi;
1829     int i, j;
1830
1831     if (theta > -theta_range && theta < theta_range) {
1832         ew = 4 * width / 5;
1833         eh = height;
1834
1835         u_shift = 0;
1836         v_shift = 0;
1837
1838         uf = (phi   / M_PI        * scale + 1.f) * ew / 2.f;
1839         vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
1840     } else {
1841         ew = width  / 5;
1842         eh = height / 2;
1843
1844         u_shift = 4 * ew;
1845
1846         if (theta < 0.f) {  // UP
1847             uf =  vec[0] / vec[1];
1848             vf = -vec[2] / vec[1];
1849             v_shift = 0;
1850         } else {            // DOWN
1851             uf = -vec[0] / vec[1];
1852             vf = -vec[2] / vec[1];
1853             v_shift = eh;
1854         }
1855
1856         uf = 0.5f * ew * (uf * scale + 1.f);
1857         vf = 0.5f * eh * (vf * scale + 1.f);
1858     }
1859
1860     ui = floorf(uf);
1861     vi = floorf(vf);
1862
1863     *du = uf - ui;
1864     *dv = vf - vi;
1865
1866     for (i = -1; i < 3; i++) {
1867         for (j = -1; j < 3; j++) {
1868             us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
1869             vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
1870         }
1871     }
1872
1873 }
1874
1875 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
1876 {
1877     for (int i = 0; i < 3; i++) {
1878         for (int j = 0; j < 3; j++) {
1879             float sum = 0;
1880
1881             for (int k = 0; k < 3; k++)
1882                 sum += a[i][k] * b[k][j];
1883
1884             c[i][j] = sum;
1885         }
1886     }
1887 }
1888
1889 /**
1890  * Calculate rotation matrix for yaw/pitch/roll angles.
1891  */
1892 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
1893                                              float rot_mat[3][3],
1894                                              const int rotation_order[3])
1895 {
1896     const float yaw_rad   = yaw   * M_PI / 180.f;
1897     const float pitch_rad = pitch * M_PI / 180.f;
1898     const float roll_rad  = roll  * M_PI / 180.f;
1899
1900     const float sin_yaw   = sinf(-yaw_rad);
1901     const float cos_yaw   = cosf(-yaw_rad);
1902     const float sin_pitch = sinf(pitch_rad);
1903     const float cos_pitch = cosf(pitch_rad);
1904     const float sin_roll  = sinf(roll_rad);
1905     const float cos_roll  = cosf(roll_rad);
1906
1907     float m[3][3][3];
1908     float temp[3][3];
1909
1910     m[0][0][0] =  cos_yaw;  m[0][0][1] = 0;          m[0][0][2] =  sin_yaw;
1911     m[0][1][0] =  0;        m[0][1][1] = 1;          m[0][1][2] =  0;
1912     m[0][2][0] = -sin_yaw;  m[0][2][1] = 0;          m[0][2][2] =  cos_yaw;
1913
1914     m[1][0][0] = 1;         m[1][0][1] = 0;          m[1][0][2] =  0;
1915     m[1][1][0] = 0;         m[1][1][1] = cos_pitch;  m[1][1][2] = -sin_pitch;
1916     m[1][2][0] = 0;         m[1][2][1] = sin_pitch;  m[1][2][2] =  cos_pitch;
1917
1918     m[2][0][0] = cos_roll;  m[2][0][1] = -sin_roll;  m[2][0][2] =  0;
1919     m[2][1][0] = sin_roll;  m[2][1][1] =  cos_roll;  m[2][1][2] =  0;
1920     m[2][2][0] = 0;         m[2][2][1] =  0;         m[2][2][2] =  1;
1921
1922     multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
1923     multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
1924 }
1925
1926 /**
1927  * Rotate vector with given rotation matrix.
1928  *
1929  * @param rot_mat rotation matrix
1930  * @param vec vector
1931  */
1932 static inline void rotate(const float rot_mat[3][3],
1933                           float *vec)
1934 {
1935     const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
1936     const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
1937     const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
1938
1939     vec[0] = x_tmp;
1940     vec[1] = y_tmp;
1941     vec[2] = z_tmp;
1942 }
1943
1944 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
1945                                        float *modifier)
1946 {
1947     modifier[0] = h_flip ? -1.f : 1.f;
1948     modifier[1] = v_flip ? -1.f : 1.f;
1949     modifier[2] = d_flip ? -1.f : 1.f;
1950 }
1951
1952 static inline void mirror(const float *modifier, float *vec)
1953 {
1954     vec[0] *= modifier[0];
1955     vec[1] *= modifier[1];
1956     vec[2] *= modifier[2];
1957 }
1958
1959 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
1960 {
1961     s->u[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_uv);
1962     s->v[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_uv);
1963     if (!s->u[p] || !s->v[p])
1964         return AVERROR(ENOMEM);
1965     if (sizeof_ker) {
1966         s->ker[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_ker);
1967         if (!s->ker[p])
1968             return AVERROR(ENOMEM);
1969     }
1970
1971     return 0;
1972 }
1973
1974 static int config_output(AVFilterLink *outlink)
1975 {
1976     AVFilterContext *ctx = outlink->src;
1977     AVFilterLink *inlink = ctx->inputs[0];
1978     V360Context *s = ctx->priv;
1979     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
1980     const int depth = desc->comp[0].depth;
1981     int sizeof_uv;
1982     int sizeof_ker;
1983     int elements;
1984     int err;
1985     int p, h, w;
1986     float hf, wf;
1987     float mirror_modifier[3];
1988     void (*in_transform)(const V360Context *s,
1989                          const float *vec, int width, int height,
1990                          uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv);
1991     void (*out_transform)(const V360Context *s,
1992                           int i, int j, int width, int height,
1993                           float *vec);
1994     void (*calculate_kernel)(float du, float dv, const XYRemap *r_tmp,
1995                              uint16_t *u, uint16_t *v, int16_t *ker);
1996     float rot_mat[3][3];
1997
1998     switch (s->interp) {
1999     case NEAREST:
2000         calculate_kernel = nearest_kernel;
2001         s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2002         elements = 1;
2003         sizeof_uv = sizeof(uint16_t) * elements;
2004         sizeof_ker = 0;
2005         break;
2006     case BILINEAR:
2007         calculate_kernel = bilinear_kernel;
2008         s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2009         elements = 2 * 2;
2010         sizeof_uv = sizeof(uint16_t) * elements;
2011         sizeof_ker = sizeof(uint16_t) * elements;
2012         break;
2013     case BICUBIC:
2014         calculate_kernel = bicubic_kernel;
2015         s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2016         elements = 4 * 4;
2017         sizeof_uv = sizeof(uint16_t) * elements;
2018         sizeof_ker = sizeof(uint16_t) * elements;
2019         break;
2020     case LANCZOS:
2021         calculate_kernel = lanczos_kernel;
2022         s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2023         elements = 4 * 4;
2024         sizeof_uv = sizeof(uint16_t) * elements;
2025         sizeof_ker = sizeof(uint16_t) * elements;
2026         break;
2027     default:
2028         av_assert0(0);
2029     }
2030
2031     ff_v360_init(s, depth);
2032
2033     for (int order = 0; order < NB_RORDERS; order++) {
2034         const char c = s->rorder[order];
2035         int rorder;
2036
2037         if (c == '\0') {
2038             av_log(ctx, AV_LOG_ERROR,
2039                    "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2040             return AVERROR(EINVAL);
2041         }
2042
2043         rorder = get_rorder(c);
2044         if (rorder == -1) {
2045             av_log(ctx, AV_LOG_ERROR,
2046                    "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2047             return AVERROR(EINVAL);
2048         }
2049
2050         s->rotation_order[order] = rorder;
2051     }
2052
2053     switch (s->in) {
2054     case EQUIRECTANGULAR:
2055         in_transform = xyz_to_equirect;
2056         err = 0;
2057         wf = inlink->w;
2058         hf = inlink->h;
2059         break;
2060     case CUBEMAP_3_2:
2061         in_transform = xyz_to_cube3x2;
2062         err = prepare_cube_in(ctx);
2063         wf = inlink->w / 3.f * 4.f;
2064         hf = inlink->h;
2065         break;
2066     case CUBEMAP_1_6:
2067         in_transform = xyz_to_cube1x6;
2068         err = prepare_cube_in(ctx);
2069         wf = inlink->w * 4.f;
2070         hf = inlink->h / 3.f;
2071         break;
2072     case CUBEMAP_6_1:
2073         in_transform = xyz_to_cube6x1;
2074         err = prepare_cube_in(ctx);
2075         wf = inlink->w / 3.f * 2.f;
2076         hf = inlink->h * 2.f;
2077         break;
2078     case EQUIANGULAR:
2079         in_transform = xyz_to_eac;
2080         err = prepare_eac_in(ctx);
2081         wf = inlink->w;
2082         hf = inlink->h / 9.f * 8.f;
2083         break;
2084     case FLAT:
2085         av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n");
2086         return AVERROR(EINVAL);
2087     case DUAL_FISHEYE:
2088         in_transform = xyz_to_dfisheye;
2089         err = 0;
2090         wf = inlink->w;
2091         hf = inlink->h;
2092         break;
2093     case BARREL:
2094         in_transform = xyz_to_barrel;
2095         err = 0;
2096         wf = inlink->w / 5.f * 4.f;
2097         hf = inlink->h;
2098         break;
2099     default:
2100         av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2101         return AVERROR_BUG;
2102     }
2103
2104     if (err != 0) {
2105         return err;
2106     }
2107
2108     switch (s->out) {
2109     case EQUIRECTANGULAR:
2110         out_transform = equirect_to_xyz;
2111         err = 0;
2112         w = roundf(wf);
2113         h = roundf(hf);
2114         break;
2115     case CUBEMAP_3_2:
2116         out_transform = cube3x2_to_xyz;
2117         err = prepare_cube_out(ctx);
2118         w = roundf(wf / 4.f * 3.f);
2119         h = roundf(hf);
2120         break;
2121     case CUBEMAP_1_6:
2122         out_transform = cube1x6_to_xyz;
2123         err = prepare_cube_out(ctx);
2124         w = roundf(wf / 4.f);
2125         h = roundf(hf * 3.f);
2126         break;
2127     case CUBEMAP_6_1:
2128         out_transform = cube6x1_to_xyz;
2129         err = prepare_cube_out(ctx);
2130         w = roundf(wf / 2.f * 3.f);
2131         h = roundf(hf / 2.f);
2132         break;
2133     case EQUIANGULAR:
2134         out_transform = eac_to_xyz;
2135         err = prepare_eac_out(ctx);
2136         w = roundf(wf);
2137         h = roundf(hf / 8.f * 9.f);
2138         break;
2139     case FLAT:
2140         out_transform = flat_to_xyz;
2141         err = prepare_flat_out(ctx);
2142         w = roundf(wf * s->flat_range[0] / s->flat_range[1] / 2.f);
2143         h = roundf(hf);
2144         break;
2145     case DUAL_FISHEYE:
2146         av_log(ctx, AV_LOG_ERROR, "Dual fisheye format is not accepted as output.\n");
2147         return AVERROR(EINVAL);
2148     case BARREL:
2149         out_transform = barrel_to_xyz;
2150         err = 0;
2151         w = roundf(wf / 4.f * 5.f);
2152         h = roundf(hf);
2153         break;
2154     default:
2155         av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2156         return AVERROR_BUG;
2157     }
2158
2159     if (err != 0) {
2160         return err;
2161     }
2162
2163     // Override resolution with user values if specified
2164     if (s->width > 0 && s->height > 0) {
2165         w = s->width;
2166         h = s->height;
2167     } else if (s->width > 0 || s->height > 0) {
2168         av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2169         return AVERROR(EINVAL);
2170     } else {
2171         if (s->out_transpose)
2172             FFSWAP(int, w, h);
2173
2174         if (s->in_transpose)
2175             FFSWAP(int, w, h);
2176     }
2177
2178     s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2179     s->planeheight[0] = s->planeheight[3] = h;
2180     s->planewidth[1]  = s->planewidth[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2181     s->planewidth[0]  = s->planewidth[3] = w;
2182
2183     outlink->h = h;
2184     outlink->w = w;
2185
2186     s->inplaneheight[1] = s->inplaneheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
2187     s->inplaneheight[0] = s->inplaneheight[3] = inlink->h;
2188     s->inplanewidth[1]  = s->inplanewidth[2]  = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
2189     s->inplanewidth[0]  = s->inplanewidth[3]  = inlink->w;
2190     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2191
2192     if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2193         s->nb_allocated = 1;
2194         s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2195         allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2196     } else if (desc->log2_chroma_h == desc->log2_chroma_w) {
2197         s->nb_allocated = 2;
2198         s->map[0] = 0;
2199         s->map[1] = s->map[2] = 1;
2200         s->map[3] = 0;
2201         allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2202         allocate_plane(s, sizeof_uv, sizeof_ker, 1);
2203     } else {
2204         s->nb_allocated = 3;
2205         s->map[0] = 0;
2206         s->map[1] = 1;
2207         s->map[2] = 2;
2208         s->map[3] = 0;
2209         allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2210         allocate_plane(s, sizeof_uv, sizeof_ker, 1);
2211         allocate_plane(s, sizeof_uv, sizeof_ker, 2);
2212     }
2213
2214     calculate_rotation_matrix(s->yaw, s->pitch, s->roll, rot_mat, s->rotation_order);
2215     set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, mirror_modifier);
2216
2217     // Calculate remap data
2218     for (p = 0; p < s->nb_allocated; p++) {
2219         const int width = s->planewidth[p];
2220         const int height = s->planeheight[p];
2221         const int in_width = s->inplanewidth[p];
2222         const int in_height = s->inplaneheight[p];
2223         float du, dv;
2224         float vec[3];
2225         XYRemap r_tmp;
2226         int i, j;
2227
2228         for (i = 0; i < width; i++) {
2229             for (j = 0; j < height; j++) {
2230                 uint16_t *u = s->u[p] + (j * width + i) * elements;
2231                 uint16_t *v = s->v[p] + (j * width + i) * elements;
2232                 int16_t *ker = s->ker[p] + (j * width + i) * elements;
2233
2234                 if (s->out_transpose)
2235                     out_transform(s, j, i, height, width, vec);
2236                 else
2237                     out_transform(s, i, j, width, height, vec);
2238                 rotate(rot_mat, vec);
2239                 mirror(mirror_modifier, vec);
2240                 if (s->in_transpose)
2241                     in_transform(s, vec, in_height, in_width, r_tmp.v, r_tmp.u, &du, &dv);
2242                 else
2243                     in_transform(s, vec, in_width, in_height, r_tmp.u, r_tmp.v, &du, &dv);
2244                 calculate_kernel(du, dv, &r_tmp, u, v, ker);
2245             }
2246         }
2247     }
2248
2249     return 0;
2250 }
2251
2252 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2253 {
2254     AVFilterContext *ctx = inlink->dst;
2255     AVFilterLink *outlink = ctx->outputs[0];
2256     V360Context *s = ctx->priv;
2257     AVFrame *out;
2258     ThreadData td;
2259
2260     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2261     if (!out) {
2262         av_frame_free(&in);
2263         return AVERROR(ENOMEM);
2264     }
2265     av_frame_copy_props(out, in);
2266
2267     td.in = in;
2268     td.out = out;
2269
2270     ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2271
2272     av_frame_free(&in);
2273     return ff_filter_frame(outlink, out);
2274 }
2275
2276 static av_cold void uninit(AVFilterContext *ctx)
2277 {
2278     V360Context *s = ctx->priv;
2279     int p;
2280
2281     for (p = 0; p < s->nb_allocated; p++) {
2282         av_freep(&s->u[p]);
2283         av_freep(&s->v[p]);
2284         av_freep(&s->ker[p]);
2285     }
2286 }
2287
2288 static const AVFilterPad inputs[] = {
2289     {
2290         .name         = "default",
2291         .type         = AVMEDIA_TYPE_VIDEO,
2292         .filter_frame = filter_frame,
2293     },
2294     { NULL }
2295 };
2296
2297 static const AVFilterPad outputs[] = {
2298     {
2299         .name         = "default",
2300         .type         = AVMEDIA_TYPE_VIDEO,
2301         .config_props = config_output,
2302     },
2303     { NULL }
2304 };
2305
2306 AVFilter ff_vf_v360 = {
2307     .name          = "v360",
2308     .description   = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
2309     .priv_size     = sizeof(V360Context),
2310     .uninit        = uninit,
2311     .query_formats = query_formats,
2312     .inputs        = inputs,
2313     .outputs       = outputs,
2314     .priv_class    = &v360_class,
2315     .flags         = AVFILTER_FLAG_SLICE_THREADS,
2316 };