FFmpeg  4.4.9
vf_v360.c
Go to the documentation of this file.
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 "filters.h"
44 #include "formats.h"
45 #include "internal.h"
46 #include "video.h"
47 #include "v360.h"
48 
49 typedef struct ThreadData {
50  AVFrame *in;
51  AVFrame *out;
52 } ThreadData;
53 
54 #define OFFSET(x) offsetof(V360Context, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
57 
58 static const AVOption v360_options[] = {
59  { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
60  { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
61  { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
62  { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
63  { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
64  { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
65  { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
66  { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
67  {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
68  { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
69  { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
70  { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
71  { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
72  { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
73  { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
74  { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
75  { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
76  {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" },
77  { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "in" },
78  { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "in" },
79  {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" },
80  {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "in" },
81  {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "in" },
82  { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "in" },
83  { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
84  { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
85  { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "in" },
86  { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "in" },
87  {"octahedron", "octahedron", 0, AV_OPT_TYPE_CONST, {.i64=OCTAHEDRON}, 0, 0, FLAGS, "in" },
88  { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
89  { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
90  { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
91  { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
92  { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
93  { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
94  { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
95  { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
96  {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
97  { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
98  { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
99  { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
100  { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
101  { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
102  { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
103  { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
104  { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
105  {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
106  { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
107  { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
108  {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
109  {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
110  {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "out" },
111  {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "out" },
112  { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "out" },
113  { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
114  { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
115  { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "out" },
116  { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "out" },
117  {"octahedron", "octahedron", 0, AV_OPT_TYPE_CONST, {.i64=OCTAHEDRON}, 0, 0, FLAGS, "out" },
118  { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
119  { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
120  { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
121  { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
122  { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
123  { "lagrange9", "lagrange9 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LAGRANGE9}, 0, 0, FLAGS, "interp" },
124  { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
125  { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
126  { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
127  { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
128  { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
129  { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
130  { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
131  { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
132  { "mitchell", "mitchell interpolation", 0, AV_OPT_TYPE_CONST, {.i64=MITCHELL}, 0, 0, FLAGS, "interp" },
133  { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
134  { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
135  { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
136  {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
137  { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
138  { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
139  { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
140  { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
141  {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
142  { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
143  { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
144  { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 0.1,TFLAGS, "in_pad"},
145  { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 0.1,TFLAGS, "out_pad"},
146  { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fin_pad"},
147  { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fout_pad"},
148  { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "yaw"},
149  { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"},
150  { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"},
151  { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"},
152  { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"},
153  { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"},
154  { "d_fov", "output diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"},
155  { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"},
156  { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"},
157  { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "d_flip"},
158  { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "ih_flip"},
159  { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"},
160  { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
161  { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
162  { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"},
163  { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"},
164  { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"},
165  {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
166  { NULL }
167 };
168 
170 
172 {
173  V360Context *s = ctx->priv;
174  static const enum AVPixelFormat pix_fmts[] = {
175  // YUVA444
179 
180  // YUVA422
184 
185  // YUVA420
188 
189  // YUVJ
193 
194  // YUV444
198 
199  // YUV440
202 
203  // YUV422
207 
208  // YUV420
212 
213  // YUV411
215 
216  // YUV410
218 
219  // GBR
223 
224  // GBRA
227 
228  // GRAY
232 
234  };
235  static const enum AVPixelFormat alpha_pix_fmts[] = {
247  };
248 
250  if (!fmts_list)
251  return AVERROR(ENOMEM);
252  return ff_set_common_formats(ctx, fmts_list);
253 }
254 
255 #define DEFINE_REMAP1_LINE(bits, div) \
256 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
257  ptrdiff_t in_linesize, \
258  const int16_t *const u, const int16_t *const v, \
259  const int16_t *const ker) \
260 { \
261  const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
262  uint##bits##_t *d = (uint##bits##_t *)dst; \
263  \
264  in_linesize /= div; \
265  \
266  for (int x = 0; x < width; x++) \
267  d[x] = s[v[x] * in_linesize + u[x]]; \
268 }
269 
270 DEFINE_REMAP1_LINE( 8, 1)
271 DEFINE_REMAP1_LINE(16, 2)
272 
273 /**
274  * Generate remapping function with a given window size and pixel depth.
275  *
276  * @param ws size of interpolation window
277  * @param bits number of bits per pixel
278  */
279 #define DEFINE_REMAP(ws, bits) \
280 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
281 { \
282  ThreadData *td = arg; \
283  const V360Context *s = ctx->priv; \
284  const SliceXYRemap *r = &s->slice_remap[jobnr]; \
285  const AVFrame *in = td->in; \
286  AVFrame *out = td->out; \
287  \
288  \
289  for (int stereo = 0; stereo < 1 + (s->out_stereo > STEREO_2D); stereo++) { \
290  for (int plane = 0; plane < s->nb_planes; plane++) { \
291  const unsigned map = s->map[plane]; \
292  const int in_linesize = in->linesize[plane]; \
293  const int out_linesize = out->linesize[plane]; \
294  const int uv_linesize = s->uv_linesize[plane]; \
295  const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
296  const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
297  const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
298  const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
299  const uint8_t *const src = in->data[plane] + \
300  in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
301  uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
302  const uint8_t *mask = plane == 3 ? r->mask : NULL; \
303  const int width = s->pr_width[plane]; \
304  const int height = s->pr_height[plane]; \
305  \
306  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs); \
307  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs); \
308  \
309  for (int y = slice_start; y < slice_end && !mask; y++) { \
310  const int16_t *const u = r->u[map] + (y - slice_start) * (int64_t)uv_linesize * ws * ws; \
311  const int16_t *const v = r->v[map] + (y - slice_start) * (int64_t)uv_linesize * ws * ws; \
312  const int16_t *const ker = r->ker[map] + (y - slice_start) * (int64_t)uv_linesize * ws * ws;\
313  \
314  s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
315  } \
316  \
317  for (int y = slice_start; y < slice_end && mask; y++) { \
318  memcpy(dst + y * out_linesize, mask + \
319  (y - slice_start) * width * (bits >> 3), width * (bits >> 3)); \
320  } \
321  } \
322  } \
323  \
324  return 0; \
325 }
326 
327 DEFINE_REMAP(1, 8)
328 DEFINE_REMAP(2, 8)
329 DEFINE_REMAP(3, 8)
330 DEFINE_REMAP(4, 8)
331 DEFINE_REMAP(1, 16)
332 DEFINE_REMAP(2, 16)
333 DEFINE_REMAP(3, 16)
334 DEFINE_REMAP(4, 16)
335 
336 #define DEFINE_REMAP_LINE(ws, bits, div) \
337 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
338  ptrdiff_t in_linesize, \
339  const int16_t *const u, const int16_t *const v, \
340  const int16_t *const ker) \
341 { \
342  const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
343  uint##bits##_t *d = (uint##bits##_t *)dst; \
344  \
345  in_linesize /= div; \
346  \
347  for (int x = 0; x < width; x++) { \
348  const int16_t *const uu = u + x * ws * ws; \
349  const int16_t *const vv = v + x * ws * ws; \
350  const int16_t *const kker = ker + x * ws * ws; \
351  int tmp = 0; \
352  \
353  for (int i = 0; i < ws; i++) { \
354  const int iws = i * ws; \
355  for (int j = 0; j < ws; j++) { \
356  tmp += kker[iws + j] * s[vv[iws + j] * in_linesize + uu[iws + j]]; \
357  } \
358  } \
359  \
360  d[x] = av_clip_uint##bits(tmp >> 14); \
361  } \
362 }
363 
364 DEFINE_REMAP_LINE(2, 8, 1)
365 DEFINE_REMAP_LINE(3, 8, 1)
366 DEFINE_REMAP_LINE(4, 8, 1)
367 DEFINE_REMAP_LINE(2, 16, 2)
368 DEFINE_REMAP_LINE(3, 16, 2)
369 DEFINE_REMAP_LINE(4, 16, 2)
370 
371 void ff_v360_init(V360Context *s, int depth)
372 {
373  switch (s->interp) {
374  case NEAREST:
375  s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
376  break;
377  case BILINEAR:
378  s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
379  break;
380  case LAGRANGE9:
381  s->remap_line = depth <= 8 ? remap3_8bit_line_c : remap3_16bit_line_c;
382  break;
383  case BICUBIC:
384  case LANCZOS:
385  case SPLINE16:
386  case GAUSSIAN:
387  case MITCHELL:
388  s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
389  break;
390  }
391 
392  if (ARCH_X86)
393  ff_v360_init_x86(s, depth);
394 }
395 
396 /**
397  * Save nearest pixel coordinates for remapping.
398  *
399  * @param du horizontal relative coordinate
400  * @param dv vertical relative coordinate
401  * @param rmap calculated 4x4 window
402  * @param u u remap data
403  * @param v v remap data
404  * @param ker ker remap data
405  */
406 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
407  int16_t *u, int16_t *v, int16_t *ker)
408 {
409  const int i = lrintf(dv) + 1;
410  const int j = lrintf(du) + 1;
411 
412  u[0] = rmap->u[i][j];
413  v[0] = rmap->v[i][j];
414 }
415 
416 /**
417  * Calculate kernel for bilinear interpolation.
418  *
419  * @param du horizontal relative coordinate
420  * @param dv vertical relative coordinate
421  * @param rmap calculated 4x4 window
422  * @param u u remap data
423  * @param v v remap data
424  * @param ker ker remap data
425  */
426 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
427  int16_t *u, int16_t *v, int16_t *ker)
428 {
429  for (int i = 0; i < 2; i++) {
430  for (int j = 0; j < 2; j++) {
431  u[i * 2 + j] = rmap->u[i + 1][j + 1];
432  v[i * 2 + j] = rmap->v[i + 1][j + 1];
433  }
434  }
435 
436  ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
437  ker[1] = lrintf( du * (1.f - dv) * 16385.f);
438  ker[2] = lrintf((1.f - du) * dv * 16385.f);
439  ker[3] = lrintf( du * dv * 16385.f);
440 }
441 
442 /**
443  * Calculate 1-dimensional lagrange coefficients.
444  *
445  * @param t relative coordinate
446  * @param coeffs coefficients
447  */
448 static inline void calculate_lagrange_coeffs(float t, float *coeffs)
449 {
450  coeffs[0] = (t - 1.f) * (t - 2.f) * 0.5f;
451  coeffs[1] = -t * (t - 2.f);
452  coeffs[2] = t * (t - 1.f) * 0.5f;
453 }
454 
455 /**
456  * Calculate kernel for lagrange interpolation.
457  *
458  * @param du horizontal relative coordinate
459  * @param dv vertical relative coordinate
460  * @param rmap calculated 4x4 window
461  * @param u u remap data
462  * @param v v remap data
463  * @param ker ker remap data
464  */
465 static void lagrange_kernel(float du, float dv, const XYRemap *rmap,
466  int16_t *u, int16_t *v, int16_t *ker)
467 {
468  float du_coeffs[3];
469  float dv_coeffs[3];
470 
471  calculate_lagrange_coeffs(du, du_coeffs);
472  calculate_lagrange_coeffs(dv, dv_coeffs);
473 
474  for (int i = 0; i < 3; i++) {
475  for (int j = 0; j < 3; j++) {
476  u[i * 3 + j] = rmap->u[i + 1][j + 1];
477  v[i * 3 + j] = rmap->v[i + 1][j + 1];
478  ker[i * 3 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
479  }
480  }
481 }
482 
483 /**
484  * Calculate 1-dimensional cubic coefficients.
485  *
486  * @param t relative coordinate
487  * @param coeffs coefficients
488  */
489 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
490 {
491  const float tt = t * t;
492  const float ttt = t * t * t;
493 
494  coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
495  coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
496  coeffs[2] = t + tt / 2.f - ttt / 2.f;
497  coeffs[3] = - t / 6.f + ttt / 6.f;
498 }
499 
500 /**
501  * Calculate kernel for bicubic interpolation.
502  *
503  * @param du horizontal relative coordinate
504  * @param dv vertical relative coordinate
505  * @param rmap calculated 4x4 window
506  * @param u u remap data
507  * @param v v remap data
508  * @param ker ker remap data
509  */
510 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
511  int16_t *u, int16_t *v, int16_t *ker)
512 {
513  float du_coeffs[4];
514  float dv_coeffs[4];
515 
516  calculate_bicubic_coeffs(du, du_coeffs);
517  calculate_bicubic_coeffs(dv, dv_coeffs);
518 
519  for (int i = 0; i < 4; i++) {
520  for (int j = 0; j < 4; j++) {
521  u[i * 4 + j] = rmap->u[i][j];
522  v[i * 4 + j] = rmap->v[i][j];
523  ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
524  }
525  }
526 }
527 
528 /**
529  * Calculate 1-dimensional lanczos coefficients.
530  *
531  * @param t relative coordinate
532  * @param coeffs coefficients
533  */
534 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
535 {
536  float sum = 0.f;
537 
538  for (int i = 0; i < 4; i++) {
539  const float x = M_PI * (t - i + 1);
540  if (x == 0.f) {
541  coeffs[i] = 1.f;
542  } else {
543  coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
544  }
545  sum += coeffs[i];
546  }
547 
548  for (int i = 0; i < 4; i++) {
549  coeffs[i] /= sum;
550  }
551 }
552 
553 /**
554  * Calculate kernel for lanczos interpolation.
555  *
556  * @param du horizontal relative coordinate
557  * @param dv vertical relative coordinate
558  * @param rmap calculated 4x4 window
559  * @param u u remap data
560  * @param v v remap data
561  * @param ker ker remap data
562  */
563 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
564  int16_t *u, int16_t *v, int16_t *ker)
565 {
566  float du_coeffs[4];
567  float dv_coeffs[4];
568 
569  calculate_lanczos_coeffs(du, du_coeffs);
570  calculate_lanczos_coeffs(dv, dv_coeffs);
571 
572  for (int i = 0; i < 4; i++) {
573  for (int j = 0; j < 4; j++) {
574  u[i * 4 + j] = rmap->u[i][j];
575  v[i * 4 + j] = rmap->v[i][j];
576  ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
577  }
578  }
579 }
580 
581 /**
582  * Calculate 1-dimensional spline16 coefficients.
583  *
584  * @param t relative coordinate
585  * @param coeffs coefficients
586  */
587 static void calculate_spline16_coeffs(float t, float *coeffs)
588 {
589  coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
590  coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
591  coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
592  coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
593 }
594 
595 /**
596  * Calculate kernel for spline16 interpolation.
597  *
598  * @param du horizontal relative coordinate
599  * @param dv vertical relative coordinate
600  * @param rmap calculated 4x4 window
601  * @param u u remap data
602  * @param v v remap data
603  * @param ker ker remap data
604  */
605 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
606  int16_t *u, int16_t *v, int16_t *ker)
607 {
608  float du_coeffs[4];
609  float dv_coeffs[4];
610 
611  calculate_spline16_coeffs(du, du_coeffs);
612  calculate_spline16_coeffs(dv, dv_coeffs);
613 
614  for (int i = 0; i < 4; i++) {
615  for (int j = 0; j < 4; j++) {
616  u[i * 4 + j] = rmap->u[i][j];
617  v[i * 4 + j] = rmap->v[i][j];
618  ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
619  }
620  }
621 }
622 
623 /**
624  * Calculate 1-dimensional gaussian coefficients.
625  *
626  * @param t relative coordinate
627  * @param coeffs coefficients
628  */
629 static void calculate_gaussian_coeffs(float t, float *coeffs)
630 {
631  float sum = 0.f;
632 
633  for (int i = 0; i < 4; i++) {
634  const float x = t - (i - 1);
635  if (x == 0.f) {
636  coeffs[i] = 1.f;
637  } else {
638  coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
639  }
640  sum += coeffs[i];
641  }
642 
643  for (int i = 0; i < 4; i++) {
644  coeffs[i] /= sum;
645  }
646 }
647 
648 /**
649  * Calculate kernel for gaussian interpolation.
650  *
651  * @param du horizontal relative coordinate
652  * @param dv vertical relative coordinate
653  * @param rmap calculated 4x4 window
654  * @param u u remap data
655  * @param v v remap data
656  * @param ker ker remap data
657  */
658 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
659  int16_t *u, int16_t *v, int16_t *ker)
660 {
661  float du_coeffs[4];
662  float dv_coeffs[4];
663 
664  calculate_gaussian_coeffs(du, du_coeffs);
665  calculate_gaussian_coeffs(dv, dv_coeffs);
666 
667  for (int i = 0; i < 4; i++) {
668  for (int j = 0; j < 4; j++) {
669  u[i * 4 + j] = rmap->u[i][j];
670  v[i * 4 + j] = rmap->v[i][j];
671  ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
672  }
673  }
674 }
675 
676 /**
677  * Calculate 1-dimensional cubic_bc_spline coefficients.
678  *
679  * @param t relative coordinate
680  * @param coeffs coefficients
681  */
682 static void calculate_cubic_bc_coeffs(float t, float *coeffs,
683  float b, float c)
684 {
685  float sum = 0.f;
686  float p0 = (6.f - 2.f * b) / 6.f,
687  p2 = (-18.f + 12.f * b + 6.f * c) / 6.f,
688  p3 = (12.f - 9.f * b - 6.f * c) / 6.f,
689  q0 = (8.f * b + 24.f * c) / 6.f,
690  q1 = (-12.f * b - 48.f * c) / 6.f,
691  q2 = (6.f * b + 30.f * c) / 6.f,
692  q3 = (-b - 6.f * c) / 6.f;
693 
694  for (int i = 0; i < 4; i++) {
695  const float x = fabsf(t - i + 1.f);
696  if (x < 1.f) {
697  coeffs[i] = (p0 + x * x * (p2 + x * p3)) *
698  (p0 + x * x * (p2 + x * p3 / 2.f) / 4.f);
699  } else if (x < 2.f) {
700  coeffs[i] = (q0 + x * (q1 + x * (q2 + x * q3))) *
701  (q0 + x * (q1 + x * (q2 + x / 2.f * q3) / 2.f) / 2.f);
702  } else {
703  coeffs[i] = 0.f;
704  }
705  sum += coeffs[i];
706  }
707 
708  for (int i = 0; i < 4; i++) {
709  coeffs[i] /= sum;
710  }
711 }
712 
713 /**
714  * Calculate kernel for mitchell interpolation.
715  *
716  * @param du horizontal relative coordinate
717  * @param dv vertical relative coordinate
718  * @param rmap calculated 4x4 window
719  * @param u u remap data
720  * @param v v remap data
721  * @param ker ker remap data
722  */
723 static void mitchell_kernel(float du, float dv, const XYRemap *rmap,
724  int16_t *u, int16_t *v, int16_t *ker)
725 {
726  float du_coeffs[4];
727  float dv_coeffs[4];
728 
729  calculate_cubic_bc_coeffs(du, du_coeffs, 1.f / 3.f, 1.f / 3.f);
730  calculate_cubic_bc_coeffs(dv, dv_coeffs, 1.f / 3.f, 1.f / 3.f);
731 
732  for (int i = 0; i < 4; i++) {
733  for (int j = 0; j < 4; j++) {
734  u[i * 4 + j] = rmap->u[i][j];
735  v[i * 4 + j] = rmap->v[i][j];
736  ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
737  }
738  }
739 }
740 
741 /**
742  * Modulo operation with only positive remainders.
743  *
744  * @param a dividend
745  * @param b divisor
746  *
747  * @return positive remainder of (a / b)
748  */
749 static inline int mod(int a, int b)
750 {
751  const int res = a % b;
752  if (res < 0) {
753  return res + b;
754  } else {
755  return res;
756  }
757 }
758 
759 /**
760  * Reflect y operation.
761  *
762  * @param y input vertical position
763  * @param h input height
764  */
765 static inline int reflecty(int y, int h)
766 {
767  if (y < 0) {
768  y = -y;
769  } else if (y >= h) {
770  y = 2 * h - 1 - y;
771  }
772 
773  return av_clip(y, 0, h - 1);
774 }
775 
776 /**
777  * Reflect x operation for equirect.
778  *
779  * @param x input horizontal position
780  * @param y input vertical position
781  * @param w input width
782  * @param h input height
783  */
784 static inline int ereflectx(int x, int y, int w, int h)
785 {
786  if (y < 0 || y >= h)
787  x += w / 2;
788 
789  return mod(x, w);
790 }
791 
792 /**
793  * Reflect x operation.
794  *
795  * @param x input horizontal position
796  * @param y input vertical position
797  * @param w input width
798  * @param h input height
799  */
800 static inline int reflectx(int x, int y, int w, int h)
801 {
802  if (y < 0 || y >= h)
803  return av_clip(w - 1 - x, 0, w - 1);
804 
805  return mod(x, w);
806 }
807 
808 /**
809  * Convert char to corresponding direction.
810  * Used for cubemap options.
811  */
812 static int get_direction(char c)
813 {
814  switch (c) {
815  case 'r':
816  return RIGHT;
817  case 'l':
818  return LEFT;
819  case 'u':
820  return UP;
821  case 'd':
822  return DOWN;
823  case 'f':
824  return FRONT;
825  case 'b':
826  return BACK;
827  default:
828  return -1;
829  }
830 }
831 
832 /**
833  * Convert char to corresponding rotation angle.
834  * Used for cubemap options.
835  */
836 static int get_rotation(char c)
837 {
838  switch (c) {
839  case '0':
840  return ROT_0;
841  case '1':
842  return ROT_90;
843  case '2':
844  return ROT_180;
845  case '3':
846  return ROT_270;
847  default:
848  return -1;
849  }
850 }
851 
852 /**
853  * Convert char to corresponding rotation order.
854  */
855 static int get_rorder(char c)
856 {
857  switch (c) {
858  case 'Y':
859  case 'y':
860  return YAW;
861  case 'P':
862  case 'p':
863  return PITCH;
864  case 'R':
865  case 'r':
866  return ROLL;
867  default:
868  return -1;
869  }
870 }
871 
872 /**
873  * Prepare data for processing cubemap input format.
874  *
875  * @param ctx filter context
876  *
877  * @return error code
878  */
880 {
881  V360Context *s = ctx->priv;
882 
883  for (int face = 0; face < NB_FACES; face++) {
884  const char c = s->in_forder[face];
885  int direction;
886 
887  if (c == '\0') {
889  "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
890  return AVERROR(EINVAL);
891  }
892 
893  direction = get_direction(c);
894  if (direction == -1) {
896  "Incorrect direction symbol '%c' in in_forder option.\n", c);
897  return AVERROR(EINVAL);
898  }
899 
900  s->in_cubemap_face_order[direction] = face;
901  }
902 
903  for (int face = 0; face < NB_FACES; face++) {
904  const char c = s->in_frot[face];
905  int rotation;
906 
907  if (c == '\0') {
909  "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
910  return AVERROR(EINVAL);
911  }
912 
913  rotation = get_rotation(c);
914  if (rotation == -1) {
916  "Incorrect rotation symbol '%c' in in_frot option.\n", c);
917  return AVERROR(EINVAL);
918  }
919 
920  s->in_cubemap_face_rotation[face] = rotation;
921  }
922 
923  return 0;
924 }
925 
926 /**
927  * Prepare data for processing cubemap output format.
928  *
929  * @param ctx filter context
930  *
931  * @return error code
932  */
934 {
935  V360Context *s = ctx->priv;
936 
937  for (int face = 0; face < NB_FACES; face++) {
938  const char c = s->out_forder[face];
939  int direction;
940 
941  if (c == '\0') {
943  "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
944  return AVERROR(EINVAL);
945  }
946 
947  direction = get_direction(c);
948  if (direction == -1) {
950  "Incorrect direction symbol '%c' in out_forder option.\n", c);
951  return AVERROR(EINVAL);
952  }
953 
954  s->out_cubemap_direction_order[face] = direction;
955  }
956 
957  for (int face = 0; face < NB_FACES; face++) {
958  const char c = s->out_frot[face];
959  int rotation;
960 
961  if (c == '\0') {
963  "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
964  return AVERROR(EINVAL);
965  }
966 
967  rotation = get_rotation(c);
968  if (rotation == -1) {
970  "Incorrect rotation symbol '%c' in out_frot option.\n", c);
971  return AVERROR(EINVAL);
972  }
973 
974  s->out_cubemap_face_rotation[face] = rotation;
975  }
976 
977  return 0;
978 }
979 
980 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
981 {
982  float tmp;
983 
984  switch (rotation) {
985  case ROT_0:
986  break;
987  case ROT_90:
988  tmp = *uf;
989  *uf = -*vf;
990  *vf = tmp;
991  break;
992  case ROT_180:
993  *uf = -*uf;
994  *vf = -*vf;
995  break;
996  case ROT_270:
997  tmp = -*uf;
998  *uf = *vf;
999  *vf = tmp;
1000  break;
1001  default:
1002  av_assert0(0);
1003  }
1004 }
1005 
1006 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
1007 {
1008  float tmp;
1009 
1010  switch (rotation) {
1011  case ROT_0:
1012  break;
1013  case ROT_90:
1014  tmp = -*uf;
1015  *uf = *vf;
1016  *vf = tmp;
1017  break;
1018  case ROT_180:
1019  *uf = -*uf;
1020  *vf = -*vf;
1021  break;
1022  case ROT_270:
1023  tmp = *uf;
1024  *uf = -*vf;
1025  *vf = tmp;
1026  break;
1027  default:
1028  av_assert0(0);
1029  }
1030 }
1031 
1032 /**
1033  * Normalize vector.
1034  *
1035  * @param vec vector
1036  */
1037 static void normalize_vector(float *vec)
1038 {
1039  const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
1040 
1041  vec[0] /= norm;
1042  vec[1] /= norm;
1043  vec[2] /= norm;
1044 }
1045 
1046 /**
1047  * Calculate 3D coordinates on sphere for corresponding cubemap position.
1048  * Common operation for every cubemap.
1049  *
1050  * @param s filter private context
1051  * @param uf horizontal cubemap coordinate [0, 1)
1052  * @param vf vertical cubemap coordinate [0, 1)
1053  * @param face face of cubemap
1054  * @param vec coordinates on sphere
1055  * @param scalew scale for uf
1056  * @param scaleh scale for vf
1057  */
1058 static void cube_to_xyz(const V360Context *s,
1059  float uf, float vf, int face,
1060  float *vec, float scalew, float scaleh)
1061 {
1062  const int direction = s->out_cubemap_direction_order[face];
1063  float l_x, l_y, l_z;
1064 
1065  uf /= scalew;
1066  vf /= scaleh;
1067 
1068  rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
1069 
1070  switch (direction) {
1071  case RIGHT:
1072  l_x = 1.f;
1073  l_y = vf;
1074  l_z = -uf;
1075  break;
1076  case LEFT:
1077  l_x = -1.f;
1078  l_y = vf;
1079  l_z = uf;
1080  break;
1081  case UP:
1082  l_x = uf;
1083  l_y = -1.f;
1084  l_z = vf;
1085  break;
1086  case DOWN:
1087  l_x = uf;
1088  l_y = 1.f;
1089  l_z = -vf;
1090  break;
1091  case FRONT:
1092  l_x = uf;
1093  l_y = vf;
1094  l_z = 1.f;
1095  break;
1096  case BACK:
1097  l_x = -uf;
1098  l_y = vf;
1099  l_z = -1.f;
1100  break;
1101  default:
1102  av_assert0(0);
1103  }
1104 
1105  vec[0] = l_x;
1106  vec[1] = l_y;
1107  vec[2] = l_z;
1108 
1109  normalize_vector(vec);
1110 }
1111 
1112 /**
1113  * Calculate cubemap position for corresponding 3D coordinates on sphere.
1114  * Common operation for every cubemap.
1115  *
1116  * @param s filter private context
1117  * @param vec coordinated on sphere
1118  * @param uf horizontal cubemap coordinate [0, 1)
1119  * @param vf vertical cubemap coordinate [0, 1)
1120  * @param direction direction of view
1121  */
1122 static void xyz_to_cube(const V360Context *s,
1123  const float *vec,
1124  float *uf, float *vf, int *direction)
1125 {
1126  const float phi = atan2f(vec[0], vec[2]);
1127  const float theta = asinf(vec[1]);
1128  float phi_norm, theta_threshold;
1129  int face;
1130 
1131  if (phi >= -M_PI_4 && phi < M_PI_4) {
1132  *direction = FRONT;
1133  phi_norm = phi;
1134  } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
1135  *direction = LEFT;
1136  phi_norm = phi + M_PI_2;
1137  } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
1138  *direction = RIGHT;
1139  phi_norm = phi - M_PI_2;
1140  } else {
1141  *direction = BACK;
1142  phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
1143  }
1144 
1145  theta_threshold = atanf(cosf(phi_norm));
1146  if (theta > theta_threshold) {
1147  *direction = DOWN;
1148  } else if (theta < -theta_threshold) {
1149  *direction = UP;
1150  }
1151 
1152  switch (*direction) {
1153  case RIGHT:
1154  *uf = -vec[2] / vec[0];
1155  *vf = vec[1] / vec[0];
1156  break;
1157  case LEFT:
1158  *uf = -vec[2] / vec[0];
1159  *vf = -vec[1] / vec[0];
1160  break;
1161  case UP:
1162  *uf = -vec[0] / vec[1];
1163  *vf = -vec[2] / vec[1];
1164  break;
1165  case DOWN:
1166  *uf = vec[0] / vec[1];
1167  *vf = -vec[2] / vec[1];
1168  break;
1169  case FRONT:
1170  *uf = vec[0] / vec[2];
1171  *vf = vec[1] / vec[2];
1172  break;
1173  case BACK:
1174  *uf = vec[0] / vec[2];
1175  *vf = -vec[1] / vec[2];
1176  break;
1177  default:
1178  av_assert0(0);
1179  }
1180 
1181  face = s->in_cubemap_face_order[*direction];
1182  rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
1183 }
1184 
1185 /**
1186  * Find position on another cube face in case of overflow/underflow.
1187  * Used for calculation of interpolation window.
1188  *
1189  * @param s filter private context
1190  * @param uf horizontal cubemap coordinate
1191  * @param vf vertical cubemap coordinate
1192  * @param direction direction of view
1193  * @param new_uf new horizontal cubemap coordinate
1194  * @param new_vf new vertical cubemap coordinate
1195  * @param face face position on cubemap
1196  */
1198  float uf, float vf, int direction,
1199  float *new_uf, float *new_vf, int *face)
1200 {
1201  /*
1202  * Cubemap orientation
1203  *
1204  * width
1205  * <------->
1206  * +-------+
1207  * | | U
1208  * | up | h ------->
1209  * +-------+-------+-------+-------+ ^ e |
1210  * | | | | | | i V |
1211  * | left | front | right | back | | g |
1212  * +-------+-------+-------+-------+ v h v
1213  * | | t
1214  * | down |
1215  * +-------+
1216  */
1217 
1218  *face = s->in_cubemap_face_order[direction];
1219  rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1220 
1221  if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1222  // There are no pixels to use in this case
1223  *new_uf = uf;
1224  *new_vf = vf;
1225  } else if (uf < -1.f) {
1226  uf += 2.f;
1227  switch (direction) {
1228  case RIGHT:
1229  direction = FRONT;
1230  *new_uf = uf;
1231  *new_vf = vf;
1232  break;
1233  case LEFT:
1234  direction = BACK;
1235  *new_uf = uf;
1236  *new_vf = vf;
1237  break;
1238  case UP:
1239  direction = LEFT;
1240  *new_uf = vf;
1241  *new_vf = -uf;
1242  break;
1243  case DOWN:
1244  direction = LEFT;
1245  *new_uf = -vf;
1246  *new_vf = uf;
1247  break;
1248  case FRONT:
1249  direction = LEFT;
1250  *new_uf = uf;
1251  *new_vf = vf;
1252  break;
1253  case BACK:
1254  direction = RIGHT;
1255  *new_uf = uf;
1256  *new_vf = vf;
1257  break;
1258  default:
1259  av_assert0(0);
1260  }
1261  } else if (uf >= 1.f) {
1262  uf -= 2.f;
1263  switch (direction) {
1264  case RIGHT:
1265  direction = BACK;
1266  *new_uf = uf;
1267  *new_vf = vf;
1268  break;
1269  case LEFT:
1270  direction = FRONT;
1271  *new_uf = uf;
1272  *new_vf = vf;
1273  break;
1274  case UP:
1275  direction = RIGHT;
1276  *new_uf = -vf;
1277  *new_vf = uf;
1278  break;
1279  case DOWN:
1280  direction = RIGHT;
1281  *new_uf = vf;
1282  *new_vf = -uf;
1283  break;
1284  case FRONT:
1285  direction = RIGHT;
1286  *new_uf = uf;
1287  *new_vf = vf;
1288  break;
1289  case BACK:
1290  direction = LEFT;
1291  *new_uf = uf;
1292  *new_vf = vf;
1293  break;
1294  default:
1295  av_assert0(0);
1296  }
1297  } else if (vf < -1.f) {
1298  vf += 2.f;
1299  switch (direction) {
1300  case RIGHT:
1301  direction = UP;
1302  *new_uf = vf;
1303  *new_vf = -uf;
1304  break;
1305  case LEFT:
1306  direction = UP;
1307  *new_uf = -vf;
1308  *new_vf = uf;
1309  break;
1310  case UP:
1311  direction = BACK;
1312  *new_uf = -uf;
1313  *new_vf = -vf;
1314  break;
1315  case DOWN:
1316  direction = FRONT;
1317  *new_uf = uf;
1318  *new_vf = vf;
1319  break;
1320  case FRONT:
1321  direction = UP;
1322  *new_uf = uf;
1323  *new_vf = vf;
1324  break;
1325  case BACK:
1326  direction = UP;
1327  *new_uf = -uf;
1328  *new_vf = -vf;
1329  break;
1330  default:
1331  av_assert0(0);
1332  }
1333  } else if (vf >= 1.f) {
1334  vf -= 2.f;
1335  switch (direction) {
1336  case RIGHT:
1337  direction = DOWN;
1338  *new_uf = -vf;
1339  *new_vf = uf;
1340  break;
1341  case LEFT:
1342  direction = DOWN;
1343  *new_uf = vf;
1344  *new_vf = -uf;
1345  break;
1346  case UP:
1347  direction = FRONT;
1348  *new_uf = uf;
1349  *new_vf = vf;
1350  break;
1351  case DOWN:
1352  direction = BACK;
1353  *new_uf = -uf;
1354  *new_vf = -vf;
1355  break;
1356  case FRONT:
1357  direction = DOWN;
1358  *new_uf = uf;
1359  *new_vf = vf;
1360  break;
1361  case BACK:
1362  direction = DOWN;
1363  *new_uf = -uf;
1364  *new_vf = -vf;
1365  break;
1366  default:
1367  av_assert0(0);
1368  }
1369  } else {
1370  // Inside cube face
1371  *new_uf = uf;
1372  *new_vf = vf;
1373  }
1374 
1375  *face = s->in_cubemap_face_order[direction];
1376  rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1377 }
1378 
1379 /**
1380  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1381  *
1382  * @param s filter private context
1383  * @param i horizontal position on frame [0, width)
1384  * @param j vertical position on frame [0, height)
1385  * @param width frame width
1386  * @param height frame height
1387  * @param vec coordinates on sphere
1388  */
1389 static int cube3x2_to_xyz(const V360Context *s,
1390  int i, int j, int width, int height,
1391  float *vec)
1392 {
1393  const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
1394  const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
1395 
1396  const float ew = width / 3.f;
1397  const float eh = height / 2.f;
1398 
1399  const int u_face = floorf(i / ew);
1400  const int v_face = floorf(j / eh);
1401  const int face = u_face + 3 * v_face;
1402 
1403  const int u_shift = ceilf(ew * u_face);
1404  const int v_shift = ceilf(eh * v_face);
1405  const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1406  const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1407 
1408  const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1409  const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1410 
1411  cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1412 
1413  return 1;
1414 }
1415 
1416 /**
1417  * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1418  *
1419  * @param s filter private context
1420  * @param vec coordinates on sphere
1421  * @param width frame width
1422  * @param height frame height
1423  * @param us horizontal coordinates for interpolation window
1424  * @param vs vertical coordinates for interpolation window
1425  * @param du horizontal relative coordinate
1426  * @param dv vertical relative coordinate
1427  */
1428 static int xyz_to_cube3x2(const V360Context *s,
1429  const float *vec, int width, int height,
1430  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1431 {
1432  const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
1433  const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
1434  const float ew = width / 3.f;
1435  const float eh = height / 2.f;
1436  float uf, vf;
1437  int ui, vi;
1438  int ewi, ehi;
1439  int direction, face;
1440  int u_face, v_face;
1441 
1442  xyz_to_cube(s, vec, &uf, &vf, &direction);
1443 
1444  uf *= scalew;
1445  vf *= scaleh;
1446 
1447  face = s->in_cubemap_face_order[direction];
1448  u_face = face % 3;
1449  v_face = face / 3;
1450  ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1451  ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1452 
1453  uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1454  vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1455 
1456  ui = floorf(uf);
1457  vi = floorf(vf);
1458 
1459  *du = uf - ui;
1460  *dv = vf - vi;
1461 
1462  for (int i = 0; i < 4; i++) {
1463  for (int j = 0; j < 4; j++) {
1464  int new_ui = ui + j - 1;
1465  int new_vi = vi + i - 1;
1466  int u_shift, v_shift;
1467  int new_ewi, new_ehi;
1468 
1469  if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1470  face = s->in_cubemap_face_order[direction];
1471 
1472  u_face = face % 3;
1473  v_face = face / 3;
1474  u_shift = ceilf(ew * u_face);
1475  v_shift = ceilf(eh * v_face);
1476  } else {
1477  uf = 2.f * new_ui / ewi - 1.f;
1478  vf = 2.f * new_vi / ehi - 1.f;
1479 
1480  uf /= scalew;
1481  vf /= scaleh;
1482 
1483  process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1484 
1485  uf *= scalew;
1486  vf *= scaleh;
1487 
1488  u_face = face % 3;
1489  v_face = face / 3;
1490  u_shift = ceilf(ew * u_face);
1491  v_shift = ceilf(eh * v_face);
1492  new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1493  new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1494 
1495  new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1496  new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1497  }
1498 
1499  us[i][j] = u_shift + new_ui;
1500  vs[i][j] = v_shift + new_vi;
1501  }
1502  }
1503 
1504  return 1;
1505 }
1506 
1507 /**
1508  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1509  *
1510  * @param s filter private context
1511  * @param i horizontal position on frame [0, width)
1512  * @param j vertical position on frame [0, height)
1513  * @param width frame width
1514  * @param height frame height
1515  * @param vec coordinates on sphere
1516  */
1517 static int cube1x6_to_xyz(const V360Context *s,
1518  int i, int j, int width, int height,
1519  float *vec)
1520 {
1521  const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / width : 1.f - s->out_pad;
1522  const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 6.f) : 1.f - s->out_pad;
1523 
1524  const float ew = width;
1525  const float eh = height / 6.f;
1526 
1527  const int face = floorf(j / eh);
1528 
1529  const int v_shift = ceilf(eh * face);
1530  const int ehi = ceilf(eh * (face + 1)) - v_shift;
1531 
1532  const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1533  const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1534 
1535  cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1536 
1537  return 1;
1538 }
1539 
1540 /**
1541  * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1542  *
1543  * @param s filter private context
1544  * @param i horizontal position on frame [0, width)
1545  * @param j vertical position on frame [0, height)
1546  * @param width frame width
1547  * @param height frame height
1548  * @param vec coordinates on sphere
1549  */
1550 static int cube6x1_to_xyz(const V360Context *s,
1551  int i, int j, int width, int height,
1552  float *vec)
1553 {
1554  const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 6.f) : 1.f - s->out_pad;
1555  const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / height : 1.f - s->out_pad;
1556 
1557  const float ew = width / 6.f;
1558  const float eh = height;
1559 
1560  const int face = floorf(i / ew);
1561 
1562  const int u_shift = ceilf(ew * face);
1563  const int ewi = ceilf(ew * (face + 1)) - u_shift;
1564 
1565  const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1566  const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1567 
1568  cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1569 
1570  return 1;
1571 }
1572 
1573 /**
1574  * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1575  *
1576  * @param s filter private context
1577  * @param vec coordinates on sphere
1578  * @param width frame width
1579  * @param height frame height
1580  * @param us horizontal coordinates for interpolation window
1581  * @param vs vertical coordinates for interpolation window
1582  * @param du horizontal relative coordinate
1583  * @param dv vertical relative coordinate
1584  */
1585 static int xyz_to_cube1x6(const V360Context *s,
1586  const float *vec, int width, int height,
1587  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1588 {
1589  const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / width : 1.f - s->in_pad;
1590  const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 6.f) : 1.f - s->in_pad;
1591  const float eh = height / 6.f;
1592  const int ewi = width;
1593  float uf, vf;
1594  int ui, vi;
1595  int ehi;
1596  int direction, face;
1597 
1598  xyz_to_cube(s, vec, &uf, &vf, &direction);
1599 
1600  uf *= scalew;
1601  vf *= scaleh;
1602 
1603  face = s->in_cubemap_face_order[direction];
1604  ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1605 
1606  uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1607  vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1608 
1609  ui = floorf(uf);
1610  vi = floorf(vf);
1611 
1612  *du = uf - ui;
1613  *dv = vf - vi;
1614 
1615  for (int i = 0; i < 4; i++) {
1616  for (int j = 0; j < 4; j++) {
1617  int new_ui = ui + j - 1;
1618  int new_vi = vi + i - 1;
1619  int v_shift;
1620  int new_ehi;
1621 
1622  if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1623  face = s->in_cubemap_face_order[direction];
1624 
1625  v_shift = ceilf(eh * face);
1626  } else {
1627  uf = 2.f * new_ui / ewi - 1.f;
1628  vf = 2.f * new_vi / ehi - 1.f;
1629 
1630  uf /= scalew;
1631  vf /= scaleh;
1632 
1633  process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1634 
1635  uf *= scalew;
1636  vf *= scaleh;
1637 
1638  v_shift = ceilf(eh * face);
1639  new_ehi = ceilf(eh * (face + 1)) - v_shift;
1640 
1641  new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1642  new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1643  }
1644 
1645  us[i][j] = new_ui;
1646  vs[i][j] = v_shift + new_vi;
1647  }
1648  }
1649 
1650  return 1;
1651 }
1652 
1653 /**
1654  * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1655  *
1656  * @param s filter private context
1657  * @param vec coordinates on sphere
1658  * @param width frame width
1659  * @param height frame height
1660  * @param us horizontal coordinates for interpolation window
1661  * @param vs vertical coordinates for interpolation window
1662  * @param du horizontal relative coordinate
1663  * @param dv vertical relative coordinate
1664  */
1665 static int xyz_to_cube6x1(const V360Context *s,
1666  const float *vec, int width, int height,
1667  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1668 {
1669  const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 6.f) : 1.f - s->in_pad;
1670  const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / height : 1.f - s->in_pad;
1671  const float ew = width / 6.f;
1672  const int ehi = height;
1673  float uf, vf;
1674  int ui, vi;
1675  int ewi;
1676  int direction, face;
1677 
1678  xyz_to_cube(s, vec, &uf, &vf, &direction);
1679 
1680  uf *= scalew;
1681  vf *= scaleh;
1682 
1683  face = s->in_cubemap_face_order[direction];
1684  ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1685 
1686  uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1687  vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1688 
1689  ui = floorf(uf);
1690  vi = floorf(vf);
1691 
1692  *du = uf - ui;
1693  *dv = vf - vi;
1694 
1695  for (int i = 0; i < 4; i++) {
1696  for (int j = 0; j < 4; j++) {
1697  int new_ui = ui + j - 1;
1698  int new_vi = vi + i - 1;
1699  int u_shift;
1700  int new_ewi;
1701 
1702  if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1703  face = s->in_cubemap_face_order[direction];
1704 
1705  u_shift = ceilf(ew * face);
1706  } else {
1707  uf = 2.f * new_ui / ewi - 1.f;
1708  vf = 2.f * new_vi / ehi - 1.f;
1709 
1710  uf /= scalew;
1711  vf /= scaleh;
1712 
1713  process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1714 
1715  uf *= scalew;
1716  vf *= scaleh;
1717 
1718  u_shift = ceilf(ew * face);
1719  new_ewi = ceilf(ew * (face + 1)) - u_shift;
1720 
1721  new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1722  new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1723  }
1724 
1725  us[i][j] = u_shift + new_ui;
1726  vs[i][j] = new_vi;
1727  }
1728  }
1729 
1730  return 1;
1731 }
1732 
1733 /**
1734  * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1735  *
1736  * @param s filter private context
1737  * @param i horizontal position on frame [0, width)
1738  * @param j vertical position on frame [0, height)
1739  * @param width frame width
1740  * @param height frame height
1741  * @param vec coordinates on sphere
1742  */
1743 static int equirect_to_xyz(const V360Context *s,
1744  int i, int j, int width, int height,
1745  float *vec)
1746 {
1747  const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
1748  const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1749 
1750  const float sin_phi = sinf(phi);
1751  const float cos_phi = cosf(phi);
1752  const float sin_theta = sinf(theta);
1753  const float cos_theta = cosf(theta);
1754 
1755  vec[0] = cos_theta * sin_phi;
1756  vec[1] = sin_theta;
1757  vec[2] = cos_theta * cos_phi;
1758 
1759  return 1;
1760 }
1761 
1762 /**
1763  * Calculate 3D coordinates on sphere for corresponding frame position in half equirectangular format.
1764  *
1765  * @param s filter private context
1766  * @param i horizontal position on frame [0, width)
1767  * @param j vertical position on frame [0, height)
1768  * @param width frame width
1769  * @param height frame height
1770  * @param vec coordinates on sphere
1771  */
1772 static int hequirect_to_xyz(const V360Context *s,
1773  int i, int j, int width, int height,
1774  float *vec)
1775 {
1776  const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI_2;
1777  const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1778 
1779  const float sin_phi = sinf(phi);
1780  const float cos_phi = cosf(phi);
1781  const float sin_theta = sinf(theta);
1782  const float cos_theta = cosf(theta);
1783 
1784  vec[0] = cos_theta * sin_phi;
1785  vec[1] = sin_theta;
1786  vec[2] = cos_theta * cos_phi;
1787 
1788  return 1;
1789 }
1790 
1791 /**
1792  * Prepare data for processing stereographic output format.
1793  *
1794  * @param ctx filter context
1795  *
1796  * @return error code
1797  */
1799 {
1800  V360Context *s = ctx->priv;
1801 
1802  s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1803  s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1804 
1805  return 0;
1806 }
1807 
1808 /**
1809  * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1810  *
1811  * @param s filter private context
1812  * @param i horizontal position on frame [0, width)
1813  * @param j vertical position on frame [0, height)
1814  * @param width frame width
1815  * @param height frame height
1816  * @param vec coordinates on sphere
1817  */
1819  int i, int j, int width, int height,
1820  float *vec)
1821 {
1822  const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1823  const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1824  const float r = hypotf(x, y);
1825  const float theta = atanf(r) * 2.f;
1826  const float sin_theta = sinf(theta);
1827 
1828  if (r > 0.f) {
1829  vec[0] = x / r * sin_theta;
1830  vec[1] = y / r * sin_theta;
1831  vec[2] = cosf(theta);
1832  } else {
1833  vec[0] = vec[1] = 0.f;
1834  vec[2] = 1.f;
1835  }
1836 
1837  normalize_vector(vec);
1838 
1839  return 1;
1840 }
1841 
1842 /**
1843  * Prepare data for processing stereographic input format.
1844  *
1845  * @param ctx filter context
1846  *
1847  * @return error code
1848  */
1850 {
1851  V360Context *s = ctx->priv;
1852 
1853  s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1854  s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1855 
1856  return 0;
1857 }
1858 
1859 /**
1860  * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1861  *
1862  * @param s filter private context
1863  * @param vec coordinates on sphere
1864  * @param width frame width
1865  * @param height frame height
1866  * @param us horizontal coordinates for interpolation window
1867  * @param vs vertical coordinates for interpolation window
1868  * @param du horizontal relative coordinate
1869  * @param dv vertical relative coordinate
1870  */
1872  const float *vec, int width, int height,
1873  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1874 {
1875  const float theta = acosf(vec[2]);
1876  const float r = tanf(theta * 0.5f);
1877  const float c = r / hypotf(vec[0], vec[1]);
1878  const float x = vec[0] * c / s->iflat_range[0];
1879  const float y = vec[1] * c / s->iflat_range[1];
1880 
1881  const float uf = (x + 1.f) * width / 2.f;
1882  const float vf = (y + 1.f) * height / 2.f;
1883 
1884  const int ui = floorf(uf);
1885  const int vi = floorf(vf);
1886 
1887  const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1888 
1889  *du = visible ? uf - ui : 0.f;
1890  *dv = visible ? vf - vi : 0.f;
1891 
1892  for (int i = 0; i < 4; i++) {
1893  for (int j = 0; j < 4; j++) {
1894  us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1895  vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1896  }
1897  }
1898 
1899  return visible;
1900 }
1901 
1902 /**
1903  * Prepare data for processing equisolid output format.
1904  *
1905  * @param ctx filter context
1906  *
1907  * @return error code
1908  */
1910 {
1911  V360Context *s = ctx->priv;
1912 
1913  s->flat_range[0] = sinf(s->h_fov * M_PI / 720.f);
1914  s->flat_range[1] = sinf(s->v_fov * M_PI / 720.f);
1915 
1916  return 0;
1917 }
1918 
1919 /**
1920  * Calculate 3D coordinates on sphere for corresponding frame position in equisolid format.
1921  *
1922  * @param s filter private context
1923  * @param i horizontal position on frame [0, width)
1924  * @param j vertical position on frame [0, height)
1925  * @param width frame width
1926  * @param height frame height
1927  * @param vec coordinates on sphere
1928  */
1929 static int equisolid_to_xyz(const V360Context *s,
1930  int i, int j, int width, int height,
1931  float *vec)
1932 {
1933  const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1934  const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1935  const float r = hypotf(x, y);
1936  const float theta = asinf(r) * 2.f;
1937  const float sin_theta = sinf(theta);
1938 
1939  if (r > 0.f) {
1940  vec[0] = x / r * sin_theta;
1941  vec[1] = y / r * sin_theta;
1942  vec[2] = cosf(theta);
1943  } else {
1944  vec[0] = vec[1] = 0.f;
1945  vec[2] = 1.f;
1946  }
1947 
1948  normalize_vector(vec);
1949 
1950  return 1;
1951 }
1952 
1953 /**
1954  * Prepare data for processing equisolid input format.
1955  *
1956  * @param ctx filter context
1957  *
1958  * @return error code
1959  */
1961 {
1962  V360Context *s = ctx->priv;
1963 
1964  s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1965  s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1966 
1967  return 0;
1968 }
1969 
1970 /**
1971  * Calculate frame position in equisolid format for corresponding 3D coordinates on sphere.
1972  *
1973  * @param s filter private context
1974  * @param vec coordinates on sphere
1975  * @param width frame width
1976  * @param height frame height
1977  * @param us horizontal coordinates for interpolation window
1978  * @param vs vertical coordinates for interpolation window
1979  * @param du horizontal relative coordinate
1980  * @param dv vertical relative coordinate
1981  */
1982 static int xyz_to_equisolid(const V360Context *s,
1983  const float *vec, int width, int height,
1984  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1985 {
1986  const float theta = acosf(vec[2]);
1987  const float r = sinf(theta * 0.5f);
1988  const float c = r / hypotf(vec[0], vec[1]);
1989  const float x = vec[0] * c / s->iflat_range[0];
1990  const float y = vec[1] * c / s->iflat_range[1];
1991 
1992  const float uf = (x + 1.f) * width / 2.f;
1993  const float vf = (y + 1.f) * height / 2.f;
1994 
1995  const int ui = floorf(uf);
1996  const int vi = floorf(vf);
1997 
1998  const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1999 
2000  *du = visible ? uf - ui : 0.f;
2001  *dv = visible ? vf - vi : 0.f;
2002 
2003  for (int i = 0; i < 4; i++) {
2004  for (int j = 0; j < 4; j++) {
2005  us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2006  vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2007  }
2008  }
2009 
2010  return visible;
2011 }
2012 
2013 /**
2014  * Prepare data for processing orthographic output format.
2015  *
2016  * @param ctx filter context
2017  *
2018  * @return error code
2019  */
2021 {
2022  V360Context *s = ctx->priv;
2023 
2024  s->flat_range[0] = sinf(FFMIN(s->h_fov, 180.f) * M_PI / 360.f);
2025  s->flat_range[1] = sinf(FFMIN(s->v_fov, 180.f) * M_PI / 360.f);
2026 
2027  return 0;
2028 }
2029 
2030 /**
2031  * Calculate 3D coordinates on sphere for corresponding frame position in orthographic format.
2032  *
2033  * @param s filter private context
2034  * @param i horizontal position on frame [0, width)
2035  * @param j vertical position on frame [0, height)
2036  * @param width frame width
2037  * @param height frame height
2038  * @param vec coordinates on sphere
2039  */
2041  int i, int j, int width, int height,
2042  float *vec)
2043 {
2044  const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
2045  const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
2046  const float r = hypotf(x, y);
2047  const float theta = asinf(r);
2048 
2049  vec[0] = x;
2050  vec[1] = y;
2051  vec[2] = cosf(theta);
2052 
2053  normalize_vector(vec);
2054 
2055  return 1;
2056 }
2057 
2058 /**
2059  * Prepare data for processing orthographic input format.
2060  *
2061  * @param ctx filter context
2062  *
2063  * @return error code
2064  */
2066 {
2067  V360Context *s = ctx->priv;
2068 
2069  s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 180.f) * M_PI / 360.f);
2070  s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 180.f) * M_PI / 360.f);
2071 
2072  return 0;
2073 }
2074 
2075 /**
2076  * Calculate frame position in orthographic format for corresponding 3D coordinates on sphere.
2077  *
2078  * @param s filter private context
2079  * @param vec coordinates on sphere
2080  * @param width frame width
2081  * @param height frame height
2082  * @param us horizontal coordinates for interpolation window
2083  * @param vs vertical coordinates for interpolation window
2084  * @param du horizontal relative coordinate
2085  * @param dv vertical relative coordinate
2086  */
2088  const float *vec, int width, int height,
2089  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2090 {
2091  const float theta = acosf(vec[2]);
2092  const float r = sinf(theta);
2093  const float c = r / hypotf(vec[0], vec[1]);
2094  const float x = vec[0] * c / s->iflat_range[0];
2095  const float y = vec[1] * c / s->iflat_range[1];
2096 
2097  const float uf = (x + 1.f) * width / 2.f;
2098  const float vf = (y + 1.f) * height / 2.f;
2099 
2100  const int ui = floorf(uf);
2101  const int vi = floorf(vf);
2102 
2103  const int visible = vec[2] >= 0.f && isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
2104 
2105  *du = visible ? uf - ui : 0.f;
2106  *dv = visible ? vf - vi : 0.f;
2107 
2108  for (int i = 0; i < 4; i++) {
2109  for (int j = 0; j < 4; j++) {
2110  us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2111  vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2112  }
2113  }
2114 
2115  return visible;
2116 }
2117 
2118 /**
2119  * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
2120  *
2121  * @param s filter private context
2122  * @param vec coordinates on sphere
2123  * @param width frame width
2124  * @param height frame height
2125  * @param us horizontal coordinates for interpolation window
2126  * @param vs vertical coordinates for interpolation window
2127  * @param du horizontal relative coordinate
2128  * @param dv vertical relative coordinate
2129  */
2130 static int xyz_to_equirect(const V360Context *s,
2131  const float *vec, int width, int height,
2132  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2133 {
2134  const float phi = atan2f(vec[0], vec[2]);
2135  const float theta = asinf(vec[1]);
2136 
2137  const float uf = (phi / M_PI + 1.f) * width / 2.f;
2138  const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2139 
2140  const int ui = floorf(uf);
2141  const int vi = floorf(vf);
2142 
2143  *du = uf - ui;
2144  *dv = vf - vi;
2145 
2146  for (int i = 0; i < 4; i++) {
2147  for (int j = 0; j < 4; j++) {
2148  us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
2149  vs[i][j] = reflecty(vi + i - 1, height);
2150  }
2151  }
2152 
2153  return 1;
2154 }
2155 
2156 /**
2157  * Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
2158  *
2159  * @param s filter private context
2160  * @param vec coordinates on sphere
2161  * @param width frame width
2162  * @param height frame height
2163  * @param us horizontal coordinates for interpolation window
2164  * @param vs vertical coordinates for interpolation window
2165  * @param du horizontal relative coordinate
2166  * @param dv vertical relative coordinate
2167  */
2168 static int xyz_to_hequirect(const V360Context *s,
2169  const float *vec, int width, int height,
2170  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2171 {
2172  const float phi = atan2f(vec[0], vec[2]);
2173  const float theta = asinf(vec[1]);
2174 
2175  const float uf = (phi / M_PI_2 + 1.f) * width / 2.f;
2176  const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2177 
2178  const int ui = floorf(uf);
2179  const int vi = floorf(vf);
2180 
2181  const int visible = phi >= -M_PI_2 && phi <= M_PI_2;
2182 
2183  *du = uf - ui;
2184  *dv = vf - vi;
2185 
2186  for (int i = 0; i < 4; i++) {
2187  for (int j = 0; j < 4; j++) {
2188  us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2189  vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2190  }
2191  }
2192 
2193  return visible;
2194 }
2195 
2196 /**
2197  * Prepare data for processing flat input format.
2198  *
2199  * @param ctx filter context
2200  *
2201  * @return error code
2202  */
2204 {
2205  V360Context *s = ctx->priv;
2206 
2207  s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
2208  s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2209 
2210  return 0;
2211 }
2212 
2213 /**
2214  * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
2215  *
2216  * @param s filter private context
2217  * @param vec coordinates on sphere
2218  * @param width frame width
2219  * @param height frame height
2220  * @param us horizontal coordinates for interpolation window
2221  * @param vs vertical coordinates for interpolation window
2222  * @param du horizontal relative coordinate
2223  * @param dv vertical relative coordinate
2224  */
2225 static int xyz_to_flat(const V360Context *s,
2226  const float *vec, int width, int height,
2227  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2228 {
2229  const float theta = acosf(vec[2]);
2230  const float r = tanf(theta);
2231  const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
2232  const float zf = vec[2];
2233  const float h = hypotf(vec[0], vec[1]);
2234  const float c = h <= 1e-6f ? 1.f : rr / h;
2235  float uf = vec[0] * c / s->iflat_range[0];
2236  float vf = vec[1] * c / s->iflat_range[1];
2237  int visible, ui, vi;
2238 
2239  uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
2240  vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
2241 
2242  ui = floorf(uf);
2243  vi = floorf(vf);
2244 
2245  visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
2246 
2247  *du = uf - ui;
2248  *dv = vf - vi;
2249 
2250  for (int i = 0; i < 4; i++) {
2251  for (int j = 0; j < 4; j++) {
2252  us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2253  vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2254  }
2255  }
2256 
2257  return visible;
2258 }
2259 
2260 /**
2261  * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
2262  *
2263  * @param s filter private context
2264  * @param vec coordinates on sphere
2265  * @param width frame width
2266  * @param height frame height
2267  * @param us horizontal coordinates for interpolation window
2268  * @param vs vertical coordinates for interpolation window
2269  * @param du horizontal relative coordinate
2270  * @param dv vertical relative coordinate
2271  */
2272 static int xyz_to_mercator(const V360Context *s,
2273  const float *vec, int width, int height,
2274  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2275 {
2276  const float phi = atan2f(vec[0], vec[2]);
2277  const float theta = vec[1];
2278 
2279  const float uf = (phi / M_PI + 1.f) * width / 2.f;
2280  const float vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
2281 
2282  const int ui = floorf(uf);
2283  const int vi = floorf(vf);
2284 
2285  *du = uf - ui;
2286  *dv = vf - vi;
2287 
2288  for (int i = 0; i < 4; i++) {
2289  for (int j = 0; j < 4; j++) {
2290  us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2291  vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2292  }
2293  }
2294 
2295  return 1;
2296 }
2297 
2298 /**
2299  * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
2300  *
2301  * @param s filter private context
2302  * @param i horizontal position on frame [0, width)
2303  * @param j vertical position on frame [0, height)
2304  * @param width frame width
2305  * @param height frame height
2306  * @param vec coordinates on sphere
2307  */
2308 static int mercator_to_xyz(const V360Context *s,
2309  int i, int j, int width, int height,
2310  float *vec)
2311 {
2312  const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI + M_PI_2;
2313  const float y = ((2.f * j + 1.f) / height - 1.f) * M_PI;
2314  const float div = expf(2.f * y) + 1.f;
2315 
2316  const float sin_phi = sinf(phi);
2317  const float cos_phi = cosf(phi);
2318  const float sin_theta = 2.f * expf(y) / div;
2319  const float cos_theta = (expf(2.f * y) - 1.f) / div;
2320 
2321  vec[0] = -sin_theta * cos_phi;
2322  vec[1] = cos_theta;
2323  vec[2] = sin_theta * sin_phi;
2324 
2325  return 1;
2326 }
2327 
2328 /**
2329  * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
2330  *
2331  * @param s filter private context
2332  * @param vec coordinates on sphere
2333  * @param width frame width
2334  * @param height frame height
2335  * @param us horizontal coordinates for interpolation window
2336  * @param vs vertical coordinates for interpolation window
2337  * @param du horizontal relative coordinate
2338  * @param dv vertical relative coordinate
2339  */
2340 static int xyz_to_ball(const V360Context *s,
2341  const float *vec, int width, int height,
2342  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2343 {
2344  const float l = hypotf(vec[0], vec[1]);
2345  const float r = sqrtf(1.f - vec[2]) / M_SQRT2;
2346 
2347  const float uf = (1.f + r * vec[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
2348  const float vf = (1.f + r * vec[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
2349 
2350  const int ui = floorf(uf);
2351  const int vi = floorf(vf);
2352 
2353  *du = uf - ui;
2354  *dv = vf - vi;
2355 
2356  for (int i = 0; i < 4; i++) {
2357  for (int j = 0; j < 4; j++) {
2358  us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2359  vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2360  }
2361  }
2362 
2363  return 1;
2364 }
2365 
2366 /**
2367  * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
2368  *
2369  * @param s filter private context
2370  * @param i horizontal position on frame [0, width)
2371  * @param j vertical position on frame [0, height)
2372  * @param width frame width
2373  * @param height frame height
2374  * @param vec coordinates on sphere
2375  */
2376 static int ball_to_xyz(const V360Context *s,
2377  int i, int j, int width, int height,
2378  float *vec)
2379 {
2380  const float x = (2.f * i + 1.f) / width - 1.f;
2381  const float y = (2.f * j + 1.f) / height - 1.f;
2382  const float l = hypotf(x, y);
2383 
2384  if (l <= 1.f) {
2385  const float z = 2.f * l * sqrtf(1.f - l * l);
2386 
2387  vec[0] = z * x / (l > 0.f ? l : 1.f);
2388  vec[1] = z * y / (l > 0.f ? l : 1.f);
2389  vec[2] = 1.f - 2.f * l * l;
2390  } else {
2391  vec[0] = 0.f;
2392  vec[1] = 1.f;
2393  vec[2] = 0.f;
2394  return 0;
2395  }
2396 
2397  return 1;
2398 }
2399 
2400 /**
2401  * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
2402  *
2403  * @param s filter private context
2404  * @param i horizontal position on frame [0, width)
2405  * @param j vertical position on frame [0, height)
2406  * @param width frame width
2407  * @param height frame height
2408  * @param vec coordinates on sphere
2409  */
2410 static int hammer_to_xyz(const V360Context *s,
2411  int i, int j, int width, int height,
2412  float *vec)
2413 {
2414  const float x = ((2.f * i + 1.f) / width - 1.f);
2415  const float y = ((2.f * j + 1.f) / height - 1.f);
2416 
2417  const float xx = x * x;
2418  const float yy = y * y;
2419 
2420  const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
2421 
2422  const float a = M_SQRT2 * x * z;
2423  const float b = 2.f * z * z - 1.f;
2424 
2425  const float aa = a * a;
2426  const float bb = b * b;
2427 
2428  const float w = sqrtf(1.f - 2.f * yy * z * z);
2429 
2430  vec[0] = w * 2.f * a * b / (aa + bb);
2431  vec[1] = M_SQRT2 * y * z;
2432  vec[2] = w * (bb - aa) / (aa + bb);
2433 
2434  normalize_vector(vec);
2435 
2436  return 1;
2437 }
2438 
2439 /**
2440  * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
2441  *
2442  * @param s filter private context
2443  * @param vec coordinates on sphere
2444  * @param width frame width
2445  * @param height frame height
2446  * @param us horizontal coordinates for interpolation window
2447  * @param vs vertical coordinates for interpolation window
2448  * @param du horizontal relative coordinate
2449  * @param dv vertical relative coordinate
2450  */
2451 static int xyz_to_hammer(const V360Context *s,
2452  const float *vec, int width, int height,
2453  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2454 {
2455  const float theta = atan2f(vec[0], vec[2]);
2456 
2457  const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
2458  const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
2459  const float y = vec[1] / z;
2460 
2461  const float uf = (x + 1.f) * width / 2.f;
2462  const float vf = (y + 1.f) * height / 2.f;
2463 
2464  const int ui = floorf(uf);
2465  const int vi = floorf(vf);
2466 
2467  *du = uf - ui;
2468  *dv = vf - vi;
2469 
2470  for (int i = 0; i < 4; i++) {
2471  for (int j = 0; j < 4; j++) {
2472  us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2473  vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2474  }
2475  }
2476 
2477  return 1;
2478 }
2479 
2480 /**
2481  * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
2482  *
2483  * @param s filter private context
2484  * @param i horizontal position on frame [0, width)
2485  * @param j vertical position on frame [0, height)
2486  * @param width frame width
2487  * @param height frame height
2488  * @param vec coordinates on sphere
2489  */
2490 static int sinusoidal_to_xyz(const V360Context *s,
2491  int i, int j, int width, int height,
2492  float *vec)
2493 {
2494  const float theta = ((2.f * j + 1.f) / height - 1.f) * M_PI_2;
2495  const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI / cosf(theta);
2496 
2497  const float sin_phi = sinf(phi);
2498  const float cos_phi = cosf(phi);
2499  const float sin_theta = sinf(theta);
2500  const float cos_theta = cosf(theta);
2501 
2502  vec[0] = cos_theta * sin_phi;
2503  vec[1] = sin_theta;
2504  vec[2] = cos_theta * cos_phi;
2505 
2506  normalize_vector(vec);
2507 
2508  return 1;
2509 }
2510 
2511 /**
2512  * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
2513  *
2514  * @param s filter private context
2515  * @param vec coordinates on sphere
2516  * @param width frame width
2517  * @param height frame height
2518  * @param us horizontal coordinates for interpolation window
2519  * @param vs vertical coordinates for interpolation window
2520  * @param du horizontal relative coordinate
2521  * @param dv vertical relative coordinate
2522  */
2523 static int xyz_to_sinusoidal(const V360Context *s,
2524  const float *vec, int width, int height,
2525  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2526 {
2527  const float theta = asinf(vec[1]);
2528  const float phi = atan2f(vec[0], vec[2]) * cosf(theta);
2529 
2530  const float uf = (phi / M_PI + 1.f) * width / 2.f;
2531  const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2532 
2533  const int ui = floorf(uf);
2534  const int vi = floorf(vf);
2535 
2536  *du = uf - ui;
2537  *dv = vf - vi;
2538 
2539  for (int i = 0; i < 4; i++) {
2540  for (int j = 0; j < 4; j++) {
2541  us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2542  vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2543  }
2544  }
2545 
2546  return 1;
2547 }
2548 
2549 /**
2550  * Prepare data for processing equi-angular cubemap input format.
2551  *
2552  * @param ctx filter context
2553  *
2554  * @return error code
2555  */
2557 {
2558  V360Context *s = ctx->priv;
2559 
2560  s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2561  s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2562  s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2563  s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2564  s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2565  s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2566 
2567  s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2568  s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2569  s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2570  s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2571  s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2572  s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2573 
2574  return 0;
2575 }
2576 
2577 /**
2578  * Prepare data for processing equi-angular cubemap output format.
2579  *
2580  * @param ctx filter context
2581  *
2582  * @return error code
2583  */
2585 {
2586  V360Context *s = ctx->priv;
2587 
2588  s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2589  s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2590  s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2591  s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2592  s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2593  s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2594 
2595  s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2596  s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2597  s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2598  s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2599  s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2600  s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2601 
2602  return 0;
2603 }
2604 
2605 /**
2606  * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2607  *
2608  * @param s filter private context
2609  * @param i horizontal position on frame [0, width)
2610  * @param j vertical position on frame [0, height)
2611  * @param width frame width
2612  * @param height frame height
2613  * @param vec coordinates on sphere
2614  */
2615 static int eac_to_xyz(const V360Context *s,
2616  int i, int j, int width, int height,
2617  float *vec)
2618 {
2619  const float pixel_pad = 2;
2620  const float u_pad = pixel_pad / width;
2621  const float v_pad = pixel_pad / height;
2622 
2623  int u_face, v_face, face;
2624 
2625  float l_x, l_y, l_z;
2626 
2627  float uf = (i + 0.5f) / width;
2628  float vf = (j + 0.5f) / height;
2629 
2630  // EAC has 2-pixel padding on faces except between faces on the same row
2631  // Padding pixels seems not to be stretched with tangent as regular pixels
2632  // Formulas below approximate original padding as close as I could get experimentally
2633 
2634  // Horizontal padding
2635  uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2636  if (uf < 0.f) {
2637  u_face = 0;
2638  uf -= 0.5f;
2639  } else if (uf >= 3.f) {
2640  u_face = 2;
2641  uf -= 2.5f;
2642  } else {
2643  u_face = floorf(uf);
2644  uf = fmodf(uf, 1.f) - 0.5f;
2645  }
2646 
2647  // Vertical padding
2648  v_face = floorf(vf * 2.f);
2649  vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2650 
2651  if (uf >= -0.5f && uf < 0.5f) {
2652  uf = tanf(M_PI_2 * uf);
2653  } else {
2654  uf = 2.f * uf;
2655  }
2656  if (vf >= -0.5f && vf < 0.5f) {
2657  vf = tanf(M_PI_2 * vf);
2658  } else {
2659  vf = 2.f * vf;
2660  }
2661 
2662  face = u_face + 3 * v_face;
2663 
2664  switch (face) {
2665  case TOP_LEFT:
2666  l_x = -1.f;
2667  l_y = vf;
2668  l_z = uf;
2669  break;
2670  case TOP_MIDDLE:
2671  l_x = uf;
2672  l_y = vf;
2673  l_z = 1.f;
2674  break;
2675  case TOP_RIGHT:
2676  l_x = 1.f;
2677  l_y = vf;
2678  l_z = -uf;
2679  break;
2680  case BOTTOM_LEFT:
2681  l_x = -vf;
2682  l_y = 1.f;
2683  l_z = -uf;
2684  break;
2685  case BOTTOM_MIDDLE:
2686  l_x = -vf;
2687  l_y = -uf;
2688  l_z = -1.f;
2689  break;
2690  case BOTTOM_RIGHT:
2691  l_x = -vf;
2692  l_y = -1.f;
2693  l_z = uf;
2694  break;
2695  default:
2696  av_assert0(0);
2697  }
2698 
2699  vec[0] = l_x;
2700  vec[1] = l_y;
2701  vec[2] = l_z;
2702 
2703  normalize_vector(vec);
2704 
2705  return 1;
2706 }
2707 
2708 /**
2709  * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2710  *
2711  * @param s filter private context
2712  * @param vec coordinates on sphere
2713  * @param width frame width
2714  * @param height frame height
2715  * @param us horizontal coordinates for interpolation window
2716  * @param vs vertical coordinates for interpolation window
2717  * @param du horizontal relative coordinate
2718  * @param dv vertical relative coordinate
2719  */
2720 static int xyz_to_eac(const V360Context *s,
2721  const float *vec, int width, int height,
2722  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2723 {
2724  const float pixel_pad = 2;
2725  const float u_pad = pixel_pad / width;
2726  const float v_pad = pixel_pad / height;
2727 
2728  float uf, vf;
2729  int ui, vi;
2730  int direction, face;
2731  int u_face, v_face;
2732 
2733  xyz_to_cube(s, vec, &uf, &vf, &direction);
2734 
2735  face = s->in_cubemap_face_order[direction];
2736  u_face = face % 3;
2737  v_face = face / 3;
2738 
2739  uf = M_2_PI * atanf(uf) + 0.5f;
2740  vf = M_2_PI * atanf(vf) + 0.5f;
2741 
2742  // These formulas are inversed from eac_to_xyz ones
2743  uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2744  vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2745 
2746  uf *= width;
2747  vf *= height;
2748 
2749  uf -= 0.5f;
2750  vf -= 0.5f;
2751 
2752  ui = floorf(uf);
2753  vi = floorf(vf);
2754 
2755  *du = uf - ui;
2756  *dv = vf - vi;
2757 
2758  for (int i = 0; i < 4; i++) {
2759  for (int j = 0; j < 4; j++) {
2760  us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2761  vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2762  }
2763  }
2764 
2765  return 1;
2766 }
2767 
2768 /**
2769  * Prepare data for processing flat output format.
2770  *
2771  * @param ctx filter context
2772  *
2773  * @return error code
2774  */
2776 {
2777  V360Context *s = ctx->priv;
2778 
2779  s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2780  s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2781 
2782  return 0;
2783 }
2784 
2785 /**
2786  * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2787  *
2788  * @param s filter private context
2789  * @param i horizontal position on frame [0, width)
2790  * @param j vertical position on frame [0, height)
2791  * @param width frame width
2792  * @param height frame height
2793  * @param vec coordinates on sphere
2794  */
2795 static int flat_to_xyz(const V360Context *s,
2796  int i, int j, int width, int height,
2797  float *vec)
2798 {
2799  const float l_x = s->flat_range[0] * ((2.f * i + 0.5f) / width - 1.f);
2800  const float l_y = s->flat_range[1] * ((2.f * j + 0.5f) / height - 1.f);
2801 
2802  vec[0] = l_x;
2803  vec[1] = l_y;
2804  vec[2] = 1.f;
2805 
2806  normalize_vector(vec);
2807 
2808  return 1;
2809 }
2810 
2811 /**
2812  * Prepare data for processing fisheye output format.
2813  *
2814  * @param ctx filter context
2815  *
2816  * @return error code
2817  */
2819 {
2820  V360Context *s = ctx->priv;
2821 
2822  s->flat_range[0] = s->h_fov / 180.f;
2823  s->flat_range[1] = s->v_fov / 180.f;
2824 
2825  return 0;
2826 }
2827 
2828 /**
2829  * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2830  *
2831  * @param s filter private context
2832  * @param i horizontal position on frame [0, width)
2833  * @param j vertical position on frame [0, height)
2834  * @param width frame width
2835  * @param height frame height
2836  * @param vec coordinates on sphere
2837  */
2838 static int fisheye_to_xyz(const V360Context *s,
2839  int i, int j, int width, int height,
2840  float *vec)
2841 {
2842  const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2843  const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2844 
2845  const float phi = atan2f(vf, uf);
2846  const float theta = M_PI_2 * (1.f - hypotf(uf, vf));
2847 
2848  const float sin_phi = sinf(phi);
2849  const float cos_phi = cosf(phi);
2850  const float sin_theta = sinf(theta);
2851  const float cos_theta = cosf(theta);
2852 
2853  vec[0] = cos_theta * cos_phi;
2854  vec[1] = cos_theta * sin_phi;
2855  vec[2] = sin_theta;
2856 
2857  normalize_vector(vec);
2858 
2859  return 1;
2860 }
2861 
2862 /**
2863  * Prepare data for processing fisheye input format.
2864  *
2865  * @param ctx filter context
2866  *
2867  * @return error code
2868  */
2870 {
2871  V360Context *s = ctx->priv;
2872 
2873  s->iflat_range[0] = s->ih_fov / 180.f;
2874  s->iflat_range[1] = s->iv_fov / 180.f;
2875 
2876  return 0;
2877 }
2878 
2879 /**
2880  * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2881  *
2882  * @param s filter private context
2883  * @param vec coordinates on sphere
2884  * @param width frame width
2885  * @param height frame height
2886  * @param us horizontal coordinates for interpolation window
2887  * @param vs vertical coordinates for interpolation window
2888  * @param du horizontal relative coordinate
2889  * @param dv vertical relative coordinate
2890  */
2891 static int xyz_to_fisheye(const V360Context *s,
2892  const float *vec, int width, int height,
2893  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2894 {
2895  const float h = hypotf(vec[0], vec[1]);
2896  const float lh = h > 0.f ? h : 1.f;
2897  const float phi = atan2f(h, vec[2]) / M_PI;
2898 
2899  float uf = vec[0] / lh * phi / s->iflat_range[0];
2900  float vf = vec[1] / lh * phi / s->iflat_range[1];
2901 
2902  const int visible = hypotf(uf, vf) <= 0.5f;
2903  int ui, vi;
2904 
2905  uf = (uf + 0.5f) * width;
2906  vf = (vf + 0.5f) * height;
2907 
2908  ui = floorf(uf);
2909  vi = floorf(vf);
2910 
2911  *du = visible ? uf - ui : 0.f;
2912  *dv = visible ? vf - vi : 0.f;
2913 
2914  for (int i = 0; i < 4; i++) {
2915  for (int j = 0; j < 4; j++) {
2916  us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2917  vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2918  }
2919  }
2920 
2921  return visible;
2922 }
2923 
2924 /**
2925  * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2926  *
2927  * @param s filter private context
2928  * @param i horizontal position on frame [0, width)
2929  * @param j vertical position on frame [0, height)
2930  * @param width frame width
2931  * @param height frame height
2932  * @param vec coordinates on sphere
2933  */
2934 static int pannini_to_xyz(const V360Context *s,
2935  int i, int j, int width, int height,
2936  float *vec)
2937 {
2938  const float uf = ((2.f * i + 1.f) / width - 1.f);
2939  const float vf = ((2.f * j + 1.f) / height - 1.f);
2940 
2941  const float d = s->h_fov;
2942  const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2943  const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2944  const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2945  const float S = (d + 1.f) / (d + clon);
2946  const float lon = atan2f(uf, S * clon);
2947  const float lat = atan2f(vf, S);
2948 
2949  vec[0] = sinf(lon) * cosf(lat);
2950  vec[1] = sinf(lat);
2951  vec[2] = cosf(lon) * cosf(lat);
2952 
2953  normalize_vector(vec);
2954 
2955  return 1;
2956 }
2957 
2958 /**
2959  * Calculate frame position in pannini format for corresponding 3D coordinates on sphere.
2960  *
2961  * @param s filter private context
2962  * @param vec coordinates on sphere
2963  * @param width frame width
2964  * @param height frame height
2965  * @param us horizontal coordinates for interpolation window
2966  * @param vs vertical coordinates for interpolation window
2967  * @param du horizontal relative coordinate
2968  * @param dv vertical relative coordinate
2969  */
2970 static int xyz_to_pannini(const V360Context *s,
2971  const float *vec, int width, int height,
2972  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2973 {
2974  const float phi = atan2f(vec[0], vec[2]);
2975  const float theta = asinf(vec[1]);
2976 
2977  const float d = s->ih_fov;
2978  const float S = (d + 1.f) / (d + cosf(phi));
2979 
2980  const float x = S * sinf(phi);
2981  const float y = S * tanf(theta);
2982 
2983  const float uf = (x + 1.f) * width / 2.f;
2984  const float vf = (y + 1.f) * height / 2.f;
2985 
2986  const int ui = floorf(uf);
2987  const int vi = floorf(vf);
2988 
2989  const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width && vec[2] >= 0.f;
2990 
2991  *du = uf - ui;
2992  *dv = vf - vi;
2993 
2994  for (int i = 0; i < 4; i++) {
2995  for (int j = 0; j < 4; j++) {
2996  us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2997  vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2998  }
2999  }
3000 
3001  return visible;
3002 }
3003 
3004 /**
3005  * Prepare data for processing cylindrical output format.
3006  *
3007  * @param ctx filter context
3008  *
3009  * @return error code
3010  */
3012 {
3013  V360Context *s = ctx->priv;
3014 
3015  s->flat_range[0] = M_PI * s->h_fov / 360.f;
3016  s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
3017 
3018  return 0;
3019 }
3020 
3021 /**
3022  * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
3023  *
3024  * @param s filter private context
3025  * @param i horizontal position on frame [0, width)
3026  * @param j vertical position on frame [0, height)
3027  * @param width frame width
3028  * @param height frame height
3029  * @param vec coordinates on sphere
3030  */
3032  int i, int j, int width, int height,
3033  float *vec)
3034 {
3035  const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f);
3036  const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
3037 
3038  const float phi = uf;
3039  const float theta = atanf(vf);
3040 
3041  const float sin_phi = sinf(phi);
3042  const float cos_phi = cosf(phi);
3043  const float sin_theta = sinf(theta);
3044  const float cos_theta = cosf(theta);
3045 
3046  vec[0] = cos_theta * sin_phi;
3047  vec[1] = sin_theta;
3048  vec[2] = cos_theta * cos_phi;
3049 
3050  normalize_vector(vec);
3051 
3052  return 1;
3053 }
3054 
3055 /**
3056  * Prepare data for processing cylindrical input format.
3057  *
3058  * @param ctx filter context
3059  *
3060  * @return error code
3061  */
3063 {
3064  V360Context *s = ctx->priv;
3065 
3066  s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
3067  s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
3068 
3069  return 0;
3070 }
3071 
3072 /**
3073  * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
3074  *
3075  * @param s filter private context
3076  * @param vec coordinates on sphere
3077  * @param width frame width
3078  * @param height frame height
3079  * @param us horizontal coordinates for interpolation window
3080  * @param vs vertical coordinates for interpolation window
3081  * @param du horizontal relative coordinate
3082  * @param dv vertical relative coordinate
3083  */
3085  const float *vec, int width, int height,
3086  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3087 {
3088  const float phi = atan2f(vec[0], vec[2]) / s->iflat_range[0];
3089  const float theta = asinf(vec[1]);
3090 
3091  const float uf = (phi + 1.f) * (width - 1) / 2.f;
3092  const float vf = (tanf(theta) / s->iflat_range[1] + 1.f) * height / 2.f;
3093 
3094  const int ui = floorf(uf);
3095  const int vi = floorf(vf);
3096 
3097  const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
3098  theta <= M_PI * s->iv_fov / 180.f &&
3099  theta >= -M_PI * s->iv_fov / 180.f;
3100 
3101  *du = uf - ui;
3102  *dv = vf - vi;
3103 
3104  for (int i = 0; i < 4; i++) {
3105  for (int j = 0; j < 4; j++) {
3106  us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
3107  vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
3108  }
3109  }
3110 
3111  return visible;
3112 }
3113 
3114 /**
3115  * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
3116  *
3117  * @param s filter private context
3118  * @param i horizontal position on frame [0, width)
3119  * @param j vertical position on frame [0, height)
3120  * @param width frame width
3121  * @param height frame height
3122  * @param vec coordinates on sphere
3123  */
3125  int i, int j, int width, int height,
3126  float *vec)
3127 {
3128  const float uf = ((2.f * i + 1.f) / width - 1.f);
3129  const float vf = ((2.f * j + 1.f) / height - 1.f);
3130  const float rh = hypotf(uf, vf);
3131  const float sinzz = 1.f - rh * rh;
3132  const float h = 1.f + s->v_fov;
3133  const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
3134  const float sinz2 = sinz * sinz;
3135 
3136  if (sinz2 <= 1.f) {
3137  const float cosz = sqrtf(1.f - sinz2);
3138 
3139  const float theta = asinf(cosz);
3140  const float phi = atan2f(uf, vf);
3141 
3142  const float sin_phi = sinf(phi);
3143  const float cos_phi = cosf(phi);
3144  const float sin_theta = sinf(theta);
3145  const float cos_theta = cosf(theta);
3146 
3147  vec[0] = cos_theta * sin_phi;
3148  vec[1] = cos_theta * cos_phi;
3149  vec[2] = sin_theta;
3150  } else {
3151  vec[0] = 0.f;
3152  vec[1] = 1.f;
3153  vec[2] = 0.f;
3154  return 0;
3155  }
3156 
3157  return 1;
3158 }
3159 
3160 /**
3161  * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
3162  *
3163  * @param s filter private context
3164  * @param i horizontal position on frame [0, width)
3165  * @param j vertical position on frame [0, height)
3166  * @param width frame width
3167  * @param height frame height
3168  * @param vec coordinates on sphere
3169  */
3171  int i, int j, int width, int height,
3172  float *vec)
3173 {
3174  const float uf = (float)i / width;
3175  const float vf = (float)j / height;
3176 
3177  vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
3178  vec[1] = 1.f - vf * 2.f;
3179  vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
3180 
3181  normalize_vector(vec);
3182 
3183  return 1;
3184 }
3185 
3186 /**
3187  * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
3188  *
3189  * @param s filter private context
3190  * @param vec coordinates on sphere
3191  * @param width frame width
3192  * @param height frame height
3193  * @param us horizontal coordinates for interpolation window
3194  * @param vs vertical coordinates for interpolation window
3195  * @param du horizontal relative coordinate
3196  * @param dv vertical relative coordinate
3197  */
3199  const float *vec, int width, int height,
3200  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3201 {
3202  const float d0 = vec[0] * 1.f + vec[1] * 1.f + vec[2] *-1.f;
3203  const float d1 = vec[0] *-1.f + vec[1] *-1.f + vec[2] *-1.f;
3204  const float d2 = vec[0] * 1.f + vec[1] *-1.f + vec[2] * 1.f;
3205  const float d3 = vec[0] *-1.f + vec[1] * 1.f + vec[2] * 1.f;
3206  const float d = FFMAX(d0, FFMAX3(d1, d2, d3));
3207 
3208  float uf, vf, x, y, z;
3209  int ui, vi;
3210 
3211  x = vec[0] / d;
3212  y = vec[1] / d;
3213  z = -vec[2] / d;
3214 
3215  vf = 0.5f - y * 0.5f;
3216 
3217  if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
3218  (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
3219  uf = 0.25f * x + 0.25f;
3220  } else {
3221  uf = 0.75f - 0.25f * x;
3222  }
3223 
3224  uf *= width;
3225  vf *= height;
3226 
3227  ui = floorf(uf);
3228  vi = floorf(vf);
3229 
3230  *du = uf - ui;
3231  *dv = vf - vi;
3232 
3233  for (int i = 0; i < 4; i++) {
3234  for (int j = 0; j < 4; j++) {
3235  us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
3236  vs[i][j] = reflecty(vi + i - 1, height);
3237  }
3238  }
3239 
3240  return 1;
3241 }
3242 
3243 /**
3244  * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
3245  *
3246  * @param s filter private context
3247  * @param i horizontal position on frame [0, width)
3248  * @param j vertical position on frame [0, height)
3249  * @param width frame width
3250  * @param height frame height
3251  * @param vec coordinates on sphere
3252  */
3253 static int dfisheye_to_xyz(const V360Context *s,
3254  int i, int j, int width, int height,
3255  float *vec)
3256 {
3257  const float ew = width / 2.f;
3258  const float eh = height;
3259 
3260  const int ei = i >= ew ? i - ew : i;
3261  const float m = i >= ew ? 1.f : -1.f;
3262 
3263  const float uf = s->flat_range[0] * ((2.f * ei) / ew - 1.f);
3264  const float vf = s->flat_range[1] * ((2.f * j + 1.f) / eh - 1.f);
3265 
3266  const float h = hypotf(uf, vf);
3267  const float lh = h > 0.f ? h : 1.f;
3268  const float theta = m * M_PI_2 * (1.f - h);
3269 
3270  const float sin_theta = sinf(theta);
3271  const float cos_theta = cosf(theta);
3272 
3273  vec[0] = cos_theta * m * uf / lh;
3274  vec[1] = cos_theta * vf / lh;
3275  vec[2] = sin_theta;
3276 
3277  normalize_vector(vec);
3278 
3279  return 1;
3280 }
3281 
3282 /**
3283  * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
3284  *
3285  * @param s filter private context
3286  * @param vec coordinates on sphere
3287  * @param width frame width
3288  * @param height frame height
3289  * @param us horizontal coordinates for interpolation window
3290  * @param vs vertical coordinates for interpolation window
3291  * @param du horizontal relative coordinate
3292  * @param dv vertical relative coordinate
3293  */
3294 static int xyz_to_dfisheye(const V360Context *s,
3295  const float *vec, int width, int height,
3296  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3297 {
3298  const float ew = width / 2.f;
3299  const float eh = height;
3300 
3301  const float h = hypotf(vec[0], vec[1]);
3302  const float lh = h > 0.f ? h : 1.f;
3303  const float theta = acosf(fabsf(vec[2])) / M_PI;
3304 
3305  float uf = (theta * (vec[0] / lh) / s->iflat_range[0] + 0.5f) * ew;
3306  float vf = (theta * (vec[1] / lh) / s->iflat_range[1] + 0.5f) * eh;
3307 
3308  int ui, vi;
3309  int u_shift;
3310 
3311  if (vec[2] >= 0.f) {
3312  u_shift = ceilf(ew);
3313  } else {
3314  u_shift = 0;
3315  uf = ew - uf;
3316  }
3317 
3318  ui = floorf(uf);
3319  vi = floorf(vf);
3320 
3321  *du = uf - ui;
3322  *dv = vf - vi;
3323 
3324  for (int i = 0; i < 4; i++) {
3325  for (int j = 0; j < 4; j++) {
3326  us[i][j] = av_clip(u_shift + ui + j - 1, 0, width - 1);
3327  vs[i][j] = av_clip( vi + i - 1, 0, height - 1);
3328  }
3329  }
3330 
3331  return 1;
3332 }
3333 
3334 /**
3335  * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
3336  *
3337  * @param s filter private context
3338  * @param i horizontal position on frame [0, width)
3339  * @param j vertical position on frame [0, height)
3340  * @param width frame width
3341  * @param height frame height
3342  * @param vec coordinates on sphere
3343  */
3344 static int barrel_to_xyz(const V360Context *s,
3345  int i, int j, int width, int height,
3346  float *vec)
3347 {
3348  const float scale = 0.99f;
3349  float l_x, l_y, l_z;
3350 
3351  if (i < 4 * width / 5) {
3352  const float theta_range = M_PI_4;
3353 
3354  const int ew = 4 * width / 5;
3355  const int eh = height;
3356 
3357  const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
3358  const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
3359 
3360  const float sin_phi = sinf(phi);
3361  const float cos_phi = cosf(phi);
3362  const float sin_theta = sinf(theta);
3363  const float cos_theta = cosf(theta);
3364 
3365  l_x = cos_theta * sin_phi;
3366  l_y = sin_theta;
3367  l_z = cos_theta * cos_phi;
3368  } else {
3369  const int ew = width / 5;
3370  const int eh = height / 2;
3371 
3372  float uf, vf;
3373 
3374  if (j < eh) { // UP
3375  uf = 2.f * (i - 4 * ew) / ew - 1.f;
3376  vf = 2.f * (j ) / eh - 1.f;
3377 
3378  uf /= scale;
3379  vf /= scale;
3380 
3381  l_x = uf;
3382  l_y = -1.f;
3383  l_z = vf;
3384  } else { // DOWN
3385  uf = 2.f * (i - 4 * ew) / ew - 1.f;
3386  vf = 2.f * (j - eh) / eh - 1.f;
3387 
3388  uf /= scale;
3389  vf /= scale;
3390 
3391  l_x = uf;
3392  l_y = 1.f;
3393  l_z = -vf;
3394  }
3395  }
3396 
3397  vec[0] = l_x;
3398  vec[1] = l_y;
3399  vec[2] = l_z;
3400 
3401  normalize_vector(vec);
3402 
3403  return 1;
3404 }
3405 
3406 /**
3407  * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
3408  *
3409  * @param s filter private context
3410  * @param vec coordinates on sphere
3411  * @param width frame width
3412  * @param height frame height
3413  * @param us horizontal coordinates for interpolation window
3414  * @param vs vertical coordinates for interpolation window
3415  * @param du horizontal relative coordinate
3416  * @param dv vertical relative coordinate
3417  */
3418 static int xyz_to_barrel(const V360Context *s,
3419  const float *vec, int width, int height,
3420  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3421 {
3422  const float scale = 0.99f;
3423 
3424  const float phi = atan2f(vec[0], vec[2]);
3425  const float theta = asinf(vec[1]);
3426  const float theta_range = M_PI_4;
3427 
3428  int ew, eh;
3429  int u_shift, v_shift;
3430  float uf, vf;
3431  int ui, vi;
3432 
3433  if (theta > -theta_range && theta < theta_range) {
3434  ew = 4 * width / 5;
3435  eh = height;
3436 
3437  u_shift = 0;
3438  v_shift = 0;
3439 
3440  uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
3441  vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
3442  } else {
3443  ew = width / 5;
3444  eh = height / 2;
3445 
3446  u_shift = 4 * ew;
3447 
3448  if (theta < 0.f) { // UP
3449  uf = -vec[0] / vec[1];
3450  vf = -vec[2] / vec[1];
3451  v_shift = 0;
3452  } else { // DOWN
3453  uf = vec[0] / vec[1];
3454  vf = -vec[2] / vec[1];
3455  v_shift = eh;
3456  }
3457 
3458  uf = 0.5f * ew * (uf * scale + 1.f);
3459  vf = 0.5f * eh * (vf * scale + 1.f);
3460  }
3461 
3462  ui = floorf(uf);
3463  vi = floorf(vf);
3464 
3465  *du = uf - ui;
3466  *dv = vf - vi;
3467 
3468  for (int i = 0; i < 4; i++) {
3469  for (int j = 0; j < 4; j++) {
3470  us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3471  vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3472  }
3473  }
3474 
3475  return 1;
3476 }
3477 
3478 /**
3479  * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
3480  *
3481  * @param s filter private context
3482  * @param vec coordinates on sphere
3483  * @param width frame width
3484  * @param height frame height
3485  * @param us horizontal coordinates for interpolation window
3486  * @param vs vertical coordinates for interpolation window
3487  * @param du horizontal relative coordinate
3488  * @param dv vertical relative coordinate
3489  */
3491  const float *vec, int width, int height,
3492  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3493 {
3494  const float phi = atan2f(vec[0], vec[2]);
3495  const float theta = asinf(vec[1]);
3496 
3497  const float theta_range = M_PI_4;
3498 
3499  int ew, eh;
3500  int u_shift, v_shift;
3501  float uf, vf;
3502  int ui, vi;
3503 
3504  if (theta >= -theta_range && theta <= theta_range) {
3505  const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
3506  const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
3507 
3508  ew = width / 3 * 2;
3509  eh = height / 2;
3510 
3511  u_shift = 0;
3512  v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
3513 
3514  uf = fmodf(phi, M_PI_2) / M_PI_2;
3515  vf = theta / M_PI_4;
3516 
3517  if (v_shift)
3518  uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
3519 
3520  uf = (uf * scalew + 1.f) * width / 3.f;
3521  vf = (vf * scaleh + 1.f) * height / 4.f;
3522  } else {
3523  const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
3524  const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
3525  int v_offset = 0;
3526 
3527  ew = width / 3;
3528  eh = height / 4;
3529 
3530  u_shift = 2 * ew;
3531 
3532  if (theta <= 0.f && theta >= -M_PI_2 &&
3533  phi <= M_PI_2 && phi >= -M_PI_2) {
3534  uf = -vec[0] / vec[1];
3535  vf = -vec[2] / vec[1];
3536  v_shift = 0;
3537  v_offset = -eh;
3538  } else if (theta >= 0.f && theta <= M_PI_2 &&
3539  phi <= M_PI_2 && phi >= -M_PI_2) {
3540  uf = vec[0] / vec[1];
3541  vf = -vec[2] / vec[1];
3542  v_shift = height * 0.25f;
3543  } else if (theta <= 0.f && theta >= -M_PI_2) {
3544  uf = vec[0] / vec[1];
3545  vf = vec[2] / vec[1];
3546  v_shift = height * 0.5f;
3547  v_offset = -eh;
3548  } else {
3549  uf = -vec[0] / vec[1];
3550  vf = vec[2] / vec[1];
3551  v_shift = height * 0.75f;
3552  }
3553 
3554  uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
3555  vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
3556  }
3557 
3558  ui = floorf(uf);
3559  vi = floorf(vf);
3560 
3561  *du = uf - ui;
3562  *dv = vf - vi;
3563 
3564  for (int i = 0; i < 4; i++) {
3565  for (int j = 0; j < 4; j++) {
3566  us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3567  vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3568  }
3569  }
3570 
3571  return 1;
3572 }
3573 
3574 /**
3575  * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
3576  *
3577  * @param s filter private context
3578  * @param i horizontal position on frame [0, width)
3579  * @param j vertical position on frame [0, height)
3580  * @param width frame width
3581  * @param height frame height
3582  * @param vec coordinates on sphere
3583  */
3585  int i, int j, int width, int height,
3586  float *vec)
3587 {
3588  const float x = (i + 0.5f) / width;
3589  const float y = (j + 0.5f) / height;
3590  float l_x, l_y, l_z;
3591 
3592  if (x < 2.f / 3.f) {
3593  const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
3594  const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
3595 
3596  const float back = floorf(y * 2.f);
3597 
3598  const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back) * M_PI;
3599  const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
3600 
3601  const float sin_phi = sinf(phi);
3602  const float cos_phi = cosf(phi);
3603  const float sin_theta = sinf(theta);
3604  const float cos_theta = cosf(theta);
3605 
3606  l_x = cos_theta * sin_phi;
3607  l_y = sin_theta;
3608  l_z = cos_theta * cos_phi;
3609  } else {
3610  const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
3611  const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
3612 
3613  const int face = floorf(y * 4.f);
3614  float uf, vf;
3615 
3616  uf = x * 3.f - 2.f;
3617 
3618  switch (face) {
3619  case 0:
3620  vf = y * 2.f;
3621  uf = 1.f - uf;
3622  vf = 0.5f - vf;
3623 
3624  l_x = (0.5f - uf) / scalew;
3625  l_y = -0.5f;
3626  l_z = (0.5f - vf) / scaleh;
3627  break;
3628  case 1:
3629  vf = y * 2.f;
3630  uf = 1.f - uf;
3631  vf = 1.f - (vf - 0.5f);
3632 
3633  l_x = (0.5f - uf) / scalew;
3634  l_y = 0.5f;
3635  l_z = (-0.5f + vf) / scaleh;
3636  break;
3637  case 2:
3638  vf = y * 2.f - 0.5f;
3639  vf = 1.f - (1.f - vf);
3640 
3641  l_x = (0.5f - uf) / scalew;
3642  l_y = -0.5f;
3643  l_z = (0.5f - vf) / scaleh;
3644  break;
3645  case 3:
3646  vf = y * 2.f - 1.5f;
3647 
3648  l_x = (0.5f - uf) / scalew;
3649  l_y = 0.5f;
3650  l_z = (-0.5f + vf) / scaleh;
3651  break;
3652  default:
3653  av_assert0(0);
3654  }
3655  }
3656 
3657  vec[0] = l_x;
3658  vec[1] = l_y;
3659  vec[2] = l_z;
3660 
3661  normalize_vector(vec);
3662 
3663  return 1;
3664 }
3665 
3666 /**
3667  * Calculate 3D coordinates on sphere for corresponding frame position in tspyramid format.
3668  *
3669  * @param s filter private context
3670  * @param i horizontal position on frame [0, width)
3671  * @param j vertical position on frame [0, height)
3672  * @param width frame width
3673  * @param height frame height
3674  * @param vec coordinates on sphere
3675  */
3676 static int tspyramid_to_xyz(const V360Context *s,
3677  int i, int j, int width, int height,
3678  float *vec)
3679 {
3680  const float x = (i + 0.5f) / width;
3681  const float y = (j + 0.5f) / height;
3682 
3683  if (x < 0.5f) {
3684  vec[0] = x * 4.f - 1.f;
3685  vec[1] = (y * 2.f - 1.f);
3686  vec[2] = 1.f;
3687  } else if (x >= 0.6875f && x < 0.8125f &&
3688  y >= 0.375f && y < 0.625f) {
3689  vec[0] = -(x - 0.6875f) * 16.f + 1.f;
3690  vec[1] = (y - 0.375f) * 8.f - 1.f;
3691  vec[2] = -1.f;
3692  } else if (0.5f <= x && x < 0.6875f &&
3693  ((0.f <= y && y < 0.375f && y >= 2.f * (x - 0.5f)) ||
3694  (0.375f <= y && y < 0.625f) ||
3695  (0.625f <= y && y < 1.f && y <= 2.f * (1.f - x)))) {
3696  vec[0] = 1.f;
3697  vec[1] = 2.f * (y - 2.f * x + 1.f) / (3.f - 4.f * x) - 1.f;
3698  vec[2] = -2.f * (x - 0.5f) / 0.1875f + 1.f;
3699  } else if (0.8125f <= x && x < 1.f &&
3700  ((0.f <= y && y < 0.375f && x >= (1.f - y / 2.f)) ||
3701  (0.375f <= y && y < 0.625f) ||
3702  (0.625f <= y && y < 1.f && y <= (2.f * x - 1.f)))) {
3703  vec[0] = -1.f;
3704  vec[1] = 2.f * (y + 2.f * x - 2.f) / (4.f * x - 3.f) - 1.f;
3705  vec[2] = 2.f * (x - 0.8125f) / 0.1875f - 1.f;
3706  } else if (0.f <= y && y < 0.375f &&
3707  ((0.5f <= x && x < 0.8125f && y < 2.f * (x - 0.5f)) ||
3708  (0.6875f <= x && x < 0.8125f) ||
3709  (0.8125f <= x && x < 1.f && x < (1.f - y / 2.f)))) {
3710  vec[0] = 2.f * (1.f - x - 0.5f * y) / (0.5f - y) - 1.f;
3711  vec[1] = -1.f;
3712  vec[2] = 2.f * (0.375f - y) / 0.375f - 1.f;
3713  } else {
3714  vec[0] = 2.f * (0.5f - x + 0.5f * y) / (y - 0.5f) - 1.f;
3715  vec[1] = 1.f;
3716  vec[2] = -2.f * (1.f - y) / 0.375f + 1.f;
3717  }
3718 
3719  normalize_vector(vec);
3720 
3721  return 1;
3722 }
3723 
3724 /**
3725  * Calculate frame position in tspyramid format for corresponding 3D coordinates on sphere.
3726  *
3727  * @param s filter private context
3728  * @param vec coordinates on sphere
3729  * @param width frame width
3730  * @param height frame height
3731  * @param us horizontal coordinates for interpolation window
3732  * @param vs vertical coordinates for interpolation window
3733  * @param du horizontal relative coordinate
3734  * @param dv vertical relative coordinate
3735  */
3736 static int xyz_to_tspyramid(const V360Context *s,
3737  const float *vec, int width, int height,
3738  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3739 {
3740  float uf, vf;
3741  int ui, vi;
3742  int face;
3743 
3744  xyz_to_cube(s, vec, &uf, &vf, &face);
3745 
3746  uf = (uf + 1.f) * 0.5f;
3747  vf = (vf + 1.f) * 0.5f;
3748 
3749  switch (face) {
3750  case UP:
3751  uf = 0.1875f * vf - 0.375f * uf * vf - 0.125f * uf + 0.8125f;
3752  vf = 0.375f - 0.375f * vf;
3753  break;
3754  case FRONT:
3755  uf = 0.5f * uf;
3756  break;
3757  case DOWN:
3758  uf = 1.f - 0.1875f * vf - 0.5f * uf + 0.375f * uf * vf;
3759  vf = 1.f - 0.375f * vf;
3760  break;
3761  case LEFT:
3762  vf = 0.25f * vf + 0.75f * uf * vf - 0.375f * uf + 0.375f;
3763  uf = 0.1875f * uf + 0.8125f;
3764  break;
3765  case RIGHT:
3766  vf = 0.375f * uf - 0.75f * uf * vf + vf;
3767  uf = 0.1875f * uf + 0.5f;
3768  break;
3769  case BACK:
3770  uf = 0.125f * uf + 0.6875f;
3771  vf = 0.25f * vf + 0.375f;
3772  break;
3773  }
3774 
3775  uf *= width;
3776  vf *= height;
3777 
3778  ui = floorf(uf);
3779  vi = floorf(vf);
3780 
3781  *du = uf - ui;
3782  *dv = vf - vi;
3783 
3784  for (int i = 0; i < 4; i++) {
3785  for (int j = 0; j < 4; j++) {
3786  us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
3787  vs[i][j] = reflecty(vi + i - 1, height);
3788  }
3789  }
3790 
3791  return 1;
3792 }
3793 
3794 /**
3795  * Calculate 3D coordinates on sphere for corresponding frame position in octahedron format.
3796  *
3797  * @param s filter private context
3798  * @param i horizontal position on frame [0, width)
3799  * @param j vertical position on frame [0, height)
3800  * @param width frame width
3801  * @param height frame height
3802  * @param vec coordinates on sphere
3803  */
3804 static int octahedron_to_xyz(const V360Context *s,
3805  int i, int j, int width, int height,
3806  float *vec)
3807 {
3808  const float x = ((i + 0.5f) / width) * 2.f - 1.f;
3809  const float y = ((j + 0.5f) / height) * 2.f - 1.f;
3810  const float ax = fabsf(x);
3811  const float ay = fabsf(y);
3812 
3813  vec[2] = 1.f - (ax + ay);
3814  if (ax + ay > 1.f) {
3815  vec[0] = (1.f - ay) * FFSIGN(x);
3816  vec[1] = (1.f - ax) * FFSIGN(y);
3817  } else {
3818  vec[0] = x;
3819  vec[1] = y;
3820  }
3821 
3822  normalize_vector(vec);
3823 
3824  return 1;
3825 }
3826 
3827 /**
3828  * Calculate frame position in octahedron format for corresponding 3D coordinates on sphere.
3829  *
3830  * @param s filter private context
3831  * @param vec coordinates on sphere
3832  * @param width frame width
3833  * @param height frame height
3834  * @param us horizontal coordinates for interpolation window
3835  * @param vs vertical coordinates for interpolation window
3836  * @param du horizontal relative coordinate
3837  * @param dv vertical relative coordinate
3838  */
3839 static int xyz_to_octahedron(const V360Context *s,
3840  const float *vec, int width, int height,
3841  int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3842 {
3843  float uf, vf, zf;
3844  int ui, vi;
3845  float div = fabsf(vec[0]) + fabsf(vec[1]) + fabsf(vec[2]);
3846 
3847  uf = vec[0] / div;
3848  vf = vec[1] / div;
3849  zf = vec[2];
3850 
3851  if (zf < 0.f) {
3852  zf = vf;
3853  vf = (1.f - fabsf(uf)) * FFSIGN(zf);
3854  uf = (1.f - fabsf(zf)) * FFSIGN(uf);
3855  }
3856 
3857  uf = uf * 0.5f + 0.5f;
3858  vf = vf * 0.5f + 0.5f;
3859 
3860  uf *= width;
3861  vf *= height;
3862 
3863  ui = floorf(uf);
3864  vi = floorf(vf);
3865 
3866  *du = uf - ui;
3867  *dv = vf - vi;
3868 
3869  for (int i = 0; i < 4; i++) {
3870  for (int j = 0; j < 4; j++) {
3871  us[i][j] = av_clip(ui + j - 1, 0, width - 1);
3872  vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
3873  }
3874  }
3875 
3876  return 1;
3877 }
3878 
3879 static void multiply_quaternion(float c[4], const float a[4], const float b[4])
3880 {
3881  c[0] = a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3];
3882  c[1] = a[1] * b[0] + a[0] * b[1] + a[2] * b[3] - a[3] * b[2];
3883  c[2] = a[2] * b[0] + a[0] * b[2] + a[3] * b[1] - a[1] * b[3];
3884  c[3] = a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1];
3885 }
3886 
3887 static void conjugate_quaternion(float d[4], const float q[4])
3888 {
3889  d[0] = q[0];
3890  d[1] = -q[1];
3891  d[2] = -q[2];
3892  d[3] = -q[3];
3893 }
3894 
3895 /**
3896  * Calculate rotation quaternion for yaw/pitch/roll angles.
3897  */
3898 static inline void calculate_rotation(float yaw, float pitch, float roll,
3899  float rot_quaternion[2][4],
3900  const int rotation_order[3])
3901 {
3902  const float yaw_rad = yaw * M_PI / 180.f;
3903  const float pitch_rad = pitch * M_PI / 180.f;
3904  const float roll_rad = roll * M_PI / 180.f;
3905 
3906  const float sin_yaw = sinf(yaw_rad * 0.5f);
3907  const float cos_yaw = cosf(yaw_rad * 0.5f);
3908  const float sin_pitch = sinf(pitch_rad * 0.5f);
3909  const float cos_pitch = cosf(pitch_rad * 0.5f);
3910  const float sin_roll = sinf(roll_rad * 0.5f);
3911  const float cos_roll = cosf(roll_rad * 0.5f);
3912 
3913  float m[3][4];
3914  float tmp[2][4];
3915 
3916  m[0][0] = cos_yaw; m[0][1] = 0.f; m[0][2] = sin_yaw; m[0][3] = 0.f;
3917  m[1][0] = cos_pitch; m[1][1] = sin_pitch; m[1][2] = 0.f; m[1][3] = 0.f;
3918  m[2][0] = cos_roll; m[2][1] = 0.f; m[2][2] = 0.f; m[2][3] = sin_roll;
3919 
3920  multiply_quaternion(tmp[0], rot_quaternion[0], m[rotation_order[0]]);
3921  multiply_quaternion(tmp[1], tmp[0], m[rotation_order[1]]);
3922  multiply_quaternion(rot_quaternion[0], tmp[1], m[rotation_order[2]]);
3923 
3924  conjugate_quaternion(rot_quaternion[1], rot_quaternion[0]);
3925 }
3926 
3927 /**
3928  * Rotate vector with given rotation quaternion.
3929  *
3930  * @param rot_quaternion rotation quaternion
3931  * @param vec vector
3932  */
3933 static inline void rotate(const float rot_quaternion[2][4],
3934  float *vec)
3935 {
3936  float qv[4], temp[4], rqv[4];
3937 
3938  qv[0] = 0.f;
3939  qv[1] = vec[0];
3940  qv[2] = vec[1];
3941  qv[3] = vec[2];
3942 
3943  multiply_quaternion(temp, rot_quaternion[0], qv);
3944  multiply_quaternion(rqv, temp, rot_quaternion[1]);
3945 
3946  vec[0] = rqv[1];
3947  vec[1] = rqv[2];
3948  vec[2] = rqv[3];
3949 }
3950 
3951 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
3952  float *modifier)
3953 {
3954  modifier[0] = h_flip ? -1.f : 1.f;
3955  modifier[1] = v_flip ? -1.f : 1.f;
3956  modifier[2] = d_flip ? -1.f : 1.f;
3957 }
3958 
3959 static inline void mirror(const float *modifier, float *vec)
3960 {
3961  vec[0] *= modifier[0];
3962  vec[1] *= modifier[1];
3963  vec[2] *= modifier[2];
3964 }
3965 
3966 static inline void input_flip(int16_t u[4][4], int16_t v[4][4], int w, int h, int hflip, int vflip)
3967 {
3968  if (hflip) {
3969  for (int i = 0; i < 4; i++) {
3970  for (int j = 0; j < 4; j++)
3971  u[i][j] = w - 1 - u[i][j];
3972  }
3973  }
3974 
3975  if (vflip) {
3976  for (int i = 0; i < 4; i++) {
3977  for (int j = 0; j < 4; j++)
3978  v[i][j] = h - 1 - v[i][j];
3979  }
3980  }
3981 }
3982 
3983 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
3984 {
3985  const int pr_height = s->pr_height[p];
3986 
3987  for (int n = 0; n < s->nb_threads; n++) {
3988  SliceXYRemap *r = &s->slice_remap[n];
3989  const int slice_start = (pr_height * n ) / s->nb_threads;
3990  const int slice_end = (pr_height * (n + 1)) / s->nb_threads;
3991  const int height = slice_end - slice_start;
3992 
3993  if (!r->u[p])
3994  r->u[p] = av_calloc(s->uv_linesize[p] * height, sizeof_uv);
3995  if (!r->v[p])
3996  r->v[p] = av_calloc(s->uv_linesize[p] * height, sizeof_uv);
3997  if (!r->u[p] || !r->v[p])
3998  return AVERROR(ENOMEM);
3999  if (sizeof_ker) {
4000  if (!r->ker[p])
4001  r->ker[p] = av_calloc(s->uv_linesize[p] * height, sizeof_ker);
4002  if (!r->ker[p])
4003  return AVERROR(ENOMEM);
4004  }
4005 
4006  if (sizeof_mask && !p) {
4007  if (!r->mask)
4008  r->mask = av_calloc(s->pr_width[p] * height, sizeof_mask);
4009  if (!r->mask)
4010  return AVERROR(ENOMEM);
4011  }
4012  }
4013 
4014  return 0;
4015 }
4016 
4017 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
4018 {
4019  switch (format) {
4020  case ORTHOGRAPHIC:
4021  {
4022  const float d = 0.5f * hypotf(w, h);
4023  const float l = sinf(d_fov * M_PI / 360.f) / d;
4024 
4025  *h_fov = asinf(w * 0.5 * l) * 360.f / M_PI;
4026  *v_fov = asinf(h * 0.5 * l) * 360.f / M_PI;
4027 
4028  if (d_fov > 180.f) {
4029  *h_fov = 180.f - *h_fov;
4030  *v_fov = 180.f - *v_fov;
4031  }
4032  }
4033  break;
4034  case EQUISOLID:
4035  {
4036  const float d = 0.5f * hypotf(w, h);
4037  const float l = d / (sinf(d_fov * M_PI / 720.f));
4038 
4039  *h_fov = 2.f * asinf(w * 0.5f / l) * 360.f / M_PI;
4040  *v_fov = 2.f * asinf(h * 0.5f / l) * 360.f / M_PI;
4041  }
4042  break;
4043  case STEREOGRAPHIC:
4044  {
4045  const float d = 0.5f * hypotf(w, h);
4046  const float l = d / (tanf(d_fov * M_PI / 720.f));
4047 
4048  *h_fov = 2.f * atan2f(w * 0.5f, l) * 360.f / M_PI;
4049  *v_fov = 2.f * atan2f(h * 0.5f, l) * 360.f / M_PI;
4050  }
4051  break;
4052  case DUAL_FISHEYE:
4053  {
4054  const float d = 0.5f * hypotf(w * 0.5f, h);
4055 
4056  *h_fov = d / w * 2.f * d_fov;
4057  *v_fov = d / h * d_fov;
4058  }
4059  break;
4060  case FISHEYE:
4061  {
4062  const float d = 0.5f * hypotf(w, h);
4063 
4064  *h_fov = d / w * d_fov;
4065  *v_fov = d / h * d_fov;
4066  }
4067  break;
4068  case FLAT:
4069  default:
4070  {
4071  const float da = tanf(0.5f * FFMIN(d_fov, 359.f) * M_PI / 180.f);
4072  const float d = hypotf(w, h);
4073 
4074  *h_fov = atan2f(da * w, d) * 360.f / M_PI;
4075  *v_fov = atan2f(da * h, d) * 360.f / M_PI;
4076 
4077  if (*h_fov < 0.f)
4078  *h_fov += 360.f;
4079  if (*v_fov < 0.f)
4080  *v_fov += 360.f;
4081  }
4082  break;
4083  }
4084 }
4085 
4086 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
4087 {
4088  outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
4089  outw[0] = outw[3] = w;
4090  outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
4091  outh[0] = outh[3] = h;
4092 }
4093 
4094 // Calculate remap data
4095 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
4096 {
4097  V360Context *s = ctx->priv;
4098  SliceXYRemap *r = &s->slice_remap[jobnr];
4099 
4100  for (int p = 0; p < s->nb_allocated; p++) {
4101  const int max_value = s->max_value;
4102  const int width = s->pr_width[p];
4103  const int uv_linesize = s->uv_linesize[p];
4104  const int height = s->pr_height[p];
4105  const int in_width = s->inplanewidth[p];
4106  const int in_height = s->inplaneheight[p];
4107  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
4108  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
4109  const int elements = s->elements;
4110  float du, dv;
4111  float vec[3];
4112  XYRemap rmap;
4113 
4114  for (int j = slice_start; j < slice_end; j++) {
4115  for (int i = 0; i < width; i++) {
4116  int16_t *u = r->u[p] + ((j - slice_start) * (int64_t)uv_linesize + i) * elements;
4117  int16_t *v = r->v[p] + ((j - slice_start) * (int64_t)uv_linesize + i) * elements;
4118  int16_t *ker = r->ker[p] + ((j - slice_start) * (int64_t)uv_linesize + i) * elements;
4119  uint8_t *mask8 = (p || !r->mask) ? NULL : r->mask + ((j - slice_start) * s->pr_width[0] + i);
4120  uint16_t *mask16 = (p || !r->mask) ? NULL : (uint16_t *)r->mask + ((j - slice_start) * s->pr_width[0] + i);
4121  int in_mask, out_mask;
4122 
4123  if (s->out_transpose)
4124  out_mask = s->out_transform(s, j, i, height, width, vec);
4125  else
4126  out_mask = s->out_transform(s, i, j, width, height, vec);
4127  if (!isfinite(vec[0]) || !isfinite(vec[1]) || !isfinite(vec[2])) {
4128  vec[0] = vec[1] = 0.f;
4129  vec[2] = 1.f;
4130  }
4131  av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
4132  rotate(s->rot_quaternion, vec);
4133  av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
4134  normalize_vector(vec);
4135  mirror(s->output_mirror_modifier, vec);
4136  if (s->in_transpose)
4137  in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
4138  else
4139  in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
4140  input_flip(rmap.u, rmap.v, in_width, in_height, s->ih_flip, s->iv_flip);
4141  av_assert1(!isnan(du) && !isnan(dv));
4142  s->calculate_kernel(du, dv, &rmap, u, v, ker);
4143 
4144  if (!p && r->mask) {
4145  if (s->mask_size == 1) {
4146  mask8[0] = 255 * (out_mask & in_mask);
4147  } else {
4148  mask16[0] = max_value * (out_mask & in_mask);
4149  }
4150  }
4151  }
4152  }
4153  }
4154 
4155  return 0;
4156 }
4157 
4159  float val, int *dim)
4160 {
4161  if (!isfinite(val) || val < 1.f || val > INT16_MAX) {
4163  "Output %s %g is outside the allowed range [1, %d].\n",
4164  name, val, INT16_MAX);
4165  return AVERROR(EINVAL);
4166  }
4167 
4168  *dim = lrintf(val);
4169  return 0;
4170 }
4171 
4172 static void projection_min_size(int projection, int *min_w, int *min_h)
4173 {
4174  switch (projection) {
4175  case CUBEMAP_3_2: *min_w = 3; *min_h = 2; break;
4176  case CUBEMAP_1_6: *min_w = 1; *min_h = 6; break;
4177  case CUBEMAP_6_1: *min_w = 6; *min_h = 1; break;
4178  case EQUIANGULAR: *min_w = 5; *min_h = 9; break;
4179  case BARREL: *min_w = 5; *min_h = 2; break;
4180  case BARREL_SPLIT: *min_w = 3; *min_h = 4; break;
4181  case DUAL_FISHEYE: *min_w = 2; *min_h = 1; break;
4182  default: *min_w = 1; *min_h = 1; break;
4183  }
4184 }
4185 
4186 static int config_output(AVFilterLink *outlink)
4187 {
4188  AVFilterContext *ctx = outlink->src;
4189  AVFilterLink *inlink = ctx->inputs[0];
4190  V360Context *s = ctx->priv;
4192  const int depth = desc->comp[0].depth;
4193  const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
4194  int sizeof_uv;
4195  int sizeof_ker;
4196  int err;
4197  int h, w;
4198  int in_offset_h, in_offset_w;
4199  int out_offset_h, out_offset_w;
4200  float hf, wf;
4201  int (*prepare_out)(AVFilterContext *ctx);
4202  int have_alpha;
4203 
4204  s->max_value = (1 << depth) - 1;
4205 
4206  switch (s->interp) {
4207  case NEAREST:
4208  s->calculate_kernel = nearest_kernel;
4209  s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
4210  s->elements = 1;
4211  sizeof_uv = sizeof(int16_t) * s->elements;
4212  sizeof_ker = 0;
4213  break;
4214  case BILINEAR:
4215  s->calculate_kernel = bilinear_kernel;
4216  s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
4217  s->elements = 2 * 2;
4218  sizeof_uv = sizeof(int16_t) * s->elements;
4219  sizeof_ker = sizeof(int16_t) * s->elements;
4220  break;
4221  case LAGRANGE9:
4222  s->calculate_kernel = lagrange_kernel;
4223  s->remap_slice = depth <= 8 ? remap3_8bit_slice : remap3_16bit_slice;
4224  s->elements = 3 * 3;
4225  sizeof_uv = sizeof(int16_t) * s->elements;
4226  sizeof_ker = sizeof(int16_t) * s->elements;
4227  break;
4228  case BICUBIC:
4229  s->calculate_kernel = bicubic_kernel;
4230  s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4231  s->elements = 4 * 4;
4232  sizeof_uv = sizeof(int16_t) * s->elements;
4233  sizeof_ker = sizeof(int16_t) * s->elements;
4234  break;
4235  case LANCZOS:
4236  s->calculate_kernel = lanczos_kernel;
4237  s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4238  s->elements = 4 * 4;
4239  sizeof_uv = sizeof(int16_t) * s->elements;
4240  sizeof_ker = sizeof(int16_t) * s->elements;
4241  break;
4242  case SPLINE16:
4243  s->calculate_kernel = spline16_kernel;
4244  s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4245  s->elements = 4 * 4;
4246  sizeof_uv = sizeof(int16_t) * s->elements;
4247  sizeof_ker = sizeof(int16_t) * s->elements;
4248  break;
4249  case GAUSSIAN:
4250  s->calculate_kernel = gaussian_kernel;
4251  s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4252  s->elements = 4 * 4;
4253  sizeof_uv = sizeof(int16_t) * s->elements;
4254  sizeof_ker = sizeof(int16_t) * s->elements;
4255  break;
4256  case MITCHELL:
4257  s->calculate_kernel = mitchell_kernel;
4258  s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4259  s->elements = 4 * 4;
4260  sizeof_uv = sizeof(int16_t) * s->elements;
4261  sizeof_ker = sizeof(int16_t) * s->elements;
4262  break;
4263  default:
4264  av_assert0(0);
4265  }
4266 
4267  ff_v360_init(s, depth);
4268 
4269  for (int order = 0; order < NB_RORDERS; order++) {
4270  const char c = s->rorder[order];
4271  int rorder;
4272 
4273  if (c == '\0') {
4275  "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
4276  s->rotation_order[0] = YAW;
4277  s->rotation_order[1] = PITCH;
4278  s->rotation_order[2] = ROLL;
4279  break;
4280  }
4281 
4282  rorder = get_rorder(c);
4283  if (rorder == -1) {
4285  "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
4286  s->rotation_order[0] = YAW;
4287  s->rotation_order[1] = PITCH;
4288  s->rotation_order[2] = ROLL;
4289  break;
4290  }
4291 
4292  s->rotation_order[order] = rorder;
4293  }
4294 
4295  switch (s->in_stereo) {
4296  case STEREO_2D:
4297  w = inlink->w;
4298  h = inlink->h;
4299  in_offset_w = in_offset_h = 0;
4300  break;
4301  case STEREO_SBS:
4302  w = inlink->w / 2;
4303  h = inlink->h;
4304  in_offset_w = w;
4305  in_offset_h = 0;
4306  break;
4307  case STEREO_TB:
4308  w = inlink->w;
4309  h = inlink->h / 2;
4310  in_offset_w = 0;
4311  in_offset_h = h;
4312  break;
4313  default:
4314  av_assert0(0);
4315  }
4316 
4317  set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
4318  set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
4319 
4320  s->in_width = s->inplanewidth[0];
4321  s->in_height = s->inplaneheight[0];
4322 
4323  if (s->id_fov > 0.f)
4324  fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
4325 
4326  if (s->in_transpose)
4327  FFSWAP(int, s->in_width, s->in_height);
4328 
4329  // The remap code stores input coordinates in int16_t
4330  if (s->in_width < 1 || s->in_width > INT16_MAX ||
4331  s->in_height < 1 || s->in_height > INT16_MAX) {
4333  "Input dimensions %dx%d are outside the allowed range [1, %d].\n",
4334  s->in_width, s->in_height, INT16_MAX);
4335  return AVERROR(EINVAL);
4336  }
4337 
4338  {
4339  int min_w, min_h;
4340  const int pw = s->in_transpose ? AV_CEIL_RSHIFT(h, desc->log2_chroma_h)
4341  : AV_CEIL_RSHIFT(w, desc->log2_chroma_w);
4342  const int ph = s->in_transpose ? AV_CEIL_RSHIFT(w, desc->log2_chroma_w)
4343  : AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
4344 
4345  projection_min_size(s->in, &min_w, &min_h);
4346  if (pw < min_w || ph < min_h) {
4348  "Input %dx%d is too small for the input projection "
4349  "(requires at least %dx%d per plane).\n", pw, ph, min_w, min_h);
4350  return AVERROR(EINVAL);
4351  }
4352  }
4353 
4354  switch (s->in) {
4355  case EQUIRECTANGULAR:
4356  s->in_transform = xyz_to_equirect;
4357  err = 0;
4358  wf = w;
4359  hf = h;
4360  break;
4361  case CUBEMAP_3_2:
4362  s->in_transform = xyz_to_cube3x2;
4363  err = prepare_cube_in(ctx);
4364  wf = w / 3.f * 4.f;
4365  hf = h;
4366  break;
4367  case CUBEMAP_1_6:
4368  s->in_transform = xyz_to_cube1x6;
4369  err = prepare_cube_in(ctx);
4370  wf = w * 4.f;
4371  hf = h / 3.f;
4372  break;
4373  case CUBEMAP_6_1:
4374  s->in_transform = xyz_to_cube6x1;
4375  err = prepare_cube_in(ctx);
4376  wf = w / 3.f * 2.f;
4377  hf = h * 2.f;
4378  break;
4379  case EQUIANGULAR:
4380  s->in_transform = xyz_to_eac;
4381  err = prepare_eac_in(ctx);
4382  wf = w;
4383  hf = h / 9.f * 8.f;
4384  break;
4385  case FLAT:
4386  s->in_transform = xyz_to_flat;
4387  err = prepare_flat_in(ctx);
4388  wf = w;
4389  hf = h;
4390  break;
4391  case PERSPECTIVE:
4392  av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
4393  return AVERROR(EINVAL);
4394  case DUAL_FISHEYE:
4395  s->in_transform = xyz_to_dfisheye;
4396  err = prepare_fisheye_in(ctx);
4397  wf = w;
4398  hf = h;
4399  break;
4400  case BARREL:
4401  s->in_transform = xyz_to_barrel;
4402  err = 0;
4403  wf = w / 5.f * 4.f;
4404  hf = h;
4405  break;
4406  case STEREOGRAPHIC:
4407  s->in_transform = xyz_to_stereographic;
4409  wf = w;
4410  hf = h / 2.f;
4411  break;
4412  case MERCATOR:
4413  s->in_transform = xyz_to_mercator;
4414  err = 0;
4415  wf = w;
4416  hf = h / 2.f;
4417  break;
4418  case BALL:
4419  s->in_transform = xyz_to_ball;
4420  err = 0;
4421  wf = w;
4422  hf = h / 2.f;
4423  break;
4424  case HAMMER:
4425  s->in_transform = xyz_to_hammer;
4426  err = 0;
4427  wf = w;
4428  hf = h;
4429  break;
4430  case SINUSOIDAL:
4431  s->in_transform = xyz_to_sinusoidal;
4432  err = 0;
4433  wf = w;
4434  hf = h;
4435  break;
4436  case FISHEYE:
4437  s->in_transform = xyz_to_fisheye;
4438  err = prepare_fisheye_in(ctx);
4439  wf = w * 2;
4440  hf = h;
4441  break;
4442  case PANNINI:
4443  s->in_transform = xyz_to_pannini;
4444  err = 0;
4445  wf = w;
4446  hf = h;
4447  break;
4448  case CYLINDRICAL:
4449  s->in_transform = xyz_to_cylindrical;
4450  err = prepare_cylindrical_in(ctx);
4451  wf = w;
4452  hf = h * 2.f;
4453  break;
4454  case TETRAHEDRON:
4455  s->in_transform = xyz_to_tetrahedron;
4456  err = 0;
4457  wf = w;
4458  hf = h;
4459  break;
4460  case BARREL_SPLIT:
4461  s->in_transform = xyz_to_barrelsplit;
4462  err = 0;
4463  wf = w * 4.f / 3.f;
4464  hf = h;
4465  break;
4466  case TSPYRAMID:
4467  s->in_transform = xyz_to_tspyramid;
4468  err = 0;
4469  wf = w;
4470  hf = h;
4471  break;
4472  case HEQUIRECTANGULAR:
4473  s->in_transform = xyz_to_hequirect;
4474  err = 0;
4475  wf = w * 2.f;
4476  hf = h;
4477  break;
4478  case EQUISOLID:
4479  s->in_transform = xyz_to_equisolid;
4480  err = prepare_equisolid_in(ctx);
4481  wf = w;
4482  hf = h / 2.f;
4483  break;
4484  case ORTHOGRAPHIC:
4485  s->in_transform = xyz_to_orthographic;
4487  wf = w;
4488  hf = h / 2.f;
4489  break;
4490  case OCTAHEDRON:
4491  s->in_transform = xyz_to_octahedron;
4492  err = 0;
4493  wf = w;
4494  hf = h / 2.f;
4495  break;
4496  default:
4497  av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
4498  return AVERROR_BUG;
4499  }
4500 
4501  if (err != 0) {
4502  return err;
4503  }
4504 
4505  switch (s->out) {
4506  case EQUIRECTANGULAR:
4507  s->out_transform = equirect_to_xyz;
4508  prepare_out = NULL;
4509  w = lrintf(wf);
4510  h = lrintf(hf);
4511  break;
4512  case CUBEMAP_3_2:
4513  s->out_transform = cube3x2_to_xyz;
4514  prepare_out = prepare_cube_out;
4515  w = lrintf(wf / 4.f * 3.f);
4516  h = lrintf(hf);
4517  break;
4518  case CUBEMAP_1_6:
4519  s->out_transform = cube1x6_to_xyz;
4520  prepare_out = prepare_cube_out;
4521  w = lrintf(wf / 4.f);
4522  h = lrintf(hf * 3.f);
4523  break;
4524  case CUBEMAP_6_1:
4525  s->out_transform = cube6x1_to_xyz;
4526  prepare_out = prepare_cube_out;
4527  w = lrintf(wf / 2.f * 3.f);
4528  h = lrintf(hf / 2.f);
4529  break;
4530  case EQUIANGULAR:
4531  s->out_transform = eac_to_xyz;
4532  prepare_out = prepare_eac_out;
4533  w = lrintf(wf);
4534  h = lrintf(hf / 8.f * 9.f);
4535  break;
4536  case FLAT:
4537  s->out_transform = flat_to_xyz;
4538  prepare_out = prepare_flat_out;
4539  w = lrintf(wf);
4540  h = lrintf(hf);
4541  break;
4542  case DUAL_FISHEYE:
4543  s->out_transform = dfisheye_to_xyz;
4544  prepare_out = prepare_fisheye_out;
4545  w = lrintf(wf);
4546  h = lrintf(hf);
4547  break;
4548  case BARREL:
4549  s->out_transform = barrel_to_xyz;
4550  prepare_out = NULL;
4551  w = lrintf(wf / 4.f * 5.f);
4552  h = lrintf(hf);
4553  break;
4554  case STEREOGRAPHIC:
4555  s->out_transform = stereographic_to_xyz;
4556  prepare_out = prepare_stereographic_out;
4557  w = lrintf(wf);
4558  h = lrintf(hf * 2.f);
4559  break;
4560  case MERCATOR:
4561  s->out_transform = mercator_to_xyz;
4562  prepare_out = NULL;
4563  w = lrintf(wf);
4564  h = lrintf(hf * 2.f);
4565  break;
4566  case BALL:
4567  s->out_transform = ball_to_xyz;
4568  prepare_out = NULL;
4569  w = lrintf(wf);
4570  h = lrintf(hf * 2.f);
4571  break;
4572  case HAMMER:
4573  s->out_transform = hammer_to_xyz;
4574  prepare_out = NULL;
4575  w = lrintf(wf);
4576  h = lrintf(hf);
4577  break;
4578  case SINUSOIDAL:
4579  s->out_transform = sinusoidal_to_xyz;
4580  prepare_out = NULL;
4581  w = lrintf(wf);
4582  h = lrintf(hf);
4583  break;
4584  case FISHEYE:
4585  s->out_transform = fisheye_to_xyz;
4586  prepare_out = prepare_fisheye_out;
4587  w = lrintf(wf * 0.5f);
4588  h = lrintf(hf);
4589  break;
4590  case PANNINI:
4591  s->out_transform = pannini_to_xyz;
4592  prepare_out = NULL;
4593  w = lrintf(wf);
4594  h = lrintf(hf);
4595  break;
4596  case CYLINDRICAL:
4597  s->out_transform = cylindrical_to_xyz;
4598  prepare_out = prepare_cylindrical_out;
4599  w = lrintf(wf);
4600  h = lrintf(hf * 0.5f);
4601  break;
4602  case PERSPECTIVE:
4603  s->out_transform = perspective_to_xyz;
4604  prepare_out = NULL;
4605  w = lrintf(wf / 2.f);
4606  h = lrintf(hf);
4607  break;
4608  case TETRAHEDRON:
4609  s->out_transform = tetrahedron_to_xyz;
4610  prepare_out = NULL;
4611  w = lrintf(wf);
4612  h = lrintf(hf);
4613  break;
4614  case BARREL_SPLIT:
4615  s->out_transform = barrelsplit_to_xyz;
4616  prepare_out = NULL;
4617  w = lrintf(wf / 4.f * 3.f);
4618  h = lrintf(hf);
4619  break;
4620  case TSPYRAMID:
4621  s->out_transform = tspyramid_to_xyz;
4622  prepare_out = NULL;
4623  w = lrintf(wf);
4624  h = lrintf(hf);
4625  break;
4626  case HEQUIRECTANGULAR:
4627  s->out_transform = hequirect_to_xyz;
4628  prepare_out = NULL;
4629  w = lrintf(wf / 2.f);
4630  h = lrintf(hf);
4631  break;
4632  case EQUISOLID:
4633  s->out_transform = equisolid_to_xyz;
4634  prepare_out = prepare_equisolid_out;
4635  w = lrintf(wf);
4636  h = lrintf(hf * 2.f);
4637  break;
4638  case ORTHOGRAPHIC:
4639  s->out_transform = orthographic_to_xyz;
4640  prepare_out = prepare_orthographic_out;
4641  w = lrintf(wf);
4642  h = lrintf(hf * 2.f);
4643  break;
4644  case OCTAHEDRON:
4645  s->out_transform = octahedron_to_xyz;
4646  prepare_out = NULL;
4647  w = lrintf(wf);
4648  h = lrintf(hf * 2.f);
4649  break;
4650  default:
4651  av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
4652  return AVERROR_BUG;
4653  }
4654 
4655  // Override resolution with user values if specified
4656  if (s->width > 0 && s->height <= 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
4657  s->out == FLAT && s->d_fov == 0.f) {
4658  w = s->width;
4659  err = get_output_dimension(ctx, "height",
4660  w / tanf(s->h_fov * M_PI / 360.f) * tanf(s->v_fov * M_PI / 360.f), &h);
4661  if (err < 0)
4662  return err;
4663  } else if (s->width <= 0 && s->height > 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
4664  s->out == FLAT && s->d_fov == 0.f) {
4665  h = s->height;
4666  err = get_output_dimension(ctx, "width",
4667  h / tanf(s->v_fov * M_PI / 360.f) * tanf(s->h_fov * M_PI / 360.f), &w);
4668  if (err < 0)
4669  return err;
4670  } else if (s->width > 0 && s->height > 0) {
4671  w = s->width;
4672  h = s->height;
4673  } else if (s->width > 0 || s->height > 0) {
4674  av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
4675  return AVERROR(EINVAL);
4676  } else {
4677  if (s->out_transpose)
4678  FFSWAP(int, w, h);
4679 
4680  if (s->in_transpose)
4681  FFSWAP(int, w, h);
4682  }
4683 
4684  if (w < 1 || w > INT16_MAX || h < 1 || h > INT16_MAX) {
4686  "Output dimensions %dx%d are outside the allowed range [1, %d].\n",
4687  w, h, INT16_MAX);
4688  return AVERROR(EINVAL);
4689  }
4690 
4691  s->width = w;
4692  s->height = h;
4693 
4694  if (s->d_fov > 0.f)
4695  fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
4696 
4697  if (prepare_out) {
4698  err = prepare_out(ctx);
4699  if (err != 0)
4700  return err;
4701  }
4702 
4703  set_dimensions(s->pr_width, s->pr_height, w, h, desc);
4704 
4705  {
4706  int min_w, min_h;
4707  const int pw = s->out_transpose ? AV_CEIL_RSHIFT(h, desc->log2_chroma_h)
4708  : AV_CEIL_RSHIFT(w, desc->log2_chroma_w);
4709  const int ph = s->out_transpose ? AV_CEIL_RSHIFT(w, desc->log2_chroma_w)
4710  : AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
4711 
4712  projection_min_size(s->out, &min_w, &min_h);
4713  if (pw < min_w || ph < min_h) {
4715  "Output %dx%d is too small for the output projection "
4716  "(requires at least %dx%d per plane).\n", pw, ph, min_w, min_h);
4717  return AVERROR(EINVAL);
4718  }
4719  }
4720 
4721  switch (s->out_stereo) {
4722  case STEREO_2D:
4723  out_offset_w = out_offset_h = 0;
4724  break;
4725  case STEREO_SBS:
4726  out_offset_w = w;
4727  out_offset_h = 0;
4728  w *= 2;
4729  break;
4730  case STEREO_TB:
4731  out_offset_w = 0;
4732  out_offset_h = h;
4733  h *= 2;
4734  break;
4735  default:
4736  av_assert0(0);
4737  }
4738 
4739  set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
4740  set_dimensions(s->planewidth, s->planeheight, w, h, desc);
4741 
4742  for (int i = 0; i < 4; i++)
4743  s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
4744 
4745  outlink->h = h;
4746  outlink->w = w;
4747 
4748  s->nb_threads = FFMIN(outlink->h, ff_filter_get_nb_threads(ctx));
4749  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
4750  have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
4751 
4752  if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
4753  s->nb_allocated = 1;
4754  s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
4755  } else {
4756  s->nb_allocated = 2;
4757  s->map[0] = s->map[3] = 0;
4758  s->map[1] = s->map[2] = 1;
4759  }
4760 
4761  if (!s->slice_remap)
4762  s->slice_remap = av_calloc(s->nb_threads, sizeof(*s->slice_remap));
4763  if (!s->slice_remap)
4764  return AVERROR(ENOMEM);
4765 
4766  for (int i = 0; i < s->nb_allocated; i++) {
4767  err = allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
4768  if (err < 0)
4769  return err;
4770  }
4771 
4772  calculate_rotation(s->yaw, s->pitch, s->roll,
4773  s->rot_quaternion, s->rotation_order);
4774 
4775  set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
4776 
4777  ctx->internal->execute(ctx, v360_slice, NULL, NULL, s->nb_threads);
4778 
4779  return 0;
4780 }
4781 
4782 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
4783 {
4784  AVFilterContext *ctx = inlink->dst;
4785  AVFilterLink *outlink = ctx->outputs[0];
4786  V360Context *s = ctx->priv;
4787  AVFrame *out;
4788  ThreadData td;
4789 
4790  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
4791  if (!out) {
4792  av_frame_free(&in);
4793  return AVERROR(ENOMEM);
4794  }
4796 
4797  td.in = in;
4798  td.out = out;
4799 
4800  ctx->internal->execute(ctx, s->remap_slice, &td, NULL, s->nb_threads);
4801 
4802  av_frame_free(&in);
4803  return ff_filter_frame(outlink, out);
4804 }
4805 
4806 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
4807  char *res, int res_len, int flags)
4808 {
4809  V360Context *s = ctx->priv;
4810  int ret;
4811 
4812  s->yaw = s->pitch = s->roll = 0.f;
4813 
4814  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
4815  if (ret < 0)
4816  return ret;
4817 
4818  return config_output(ctx->outputs[0]);
4819 }
4820 
4822 {
4823  V360Context *s = ctx->priv;
4824 
4825  s->rot_quaternion[0][0] = 1.f;
4826  s->rot_quaternion[0][1] = s->rot_quaternion[0][2] = s->rot_quaternion[0][3] = 0.f;
4827 
4828  return 0;
4829 }
4830 
4832 {
4833  V360Context *s = ctx->priv;
4834 
4835  for (int n = 0; n < s->nb_threads && s->slice_remap; n++) {
4836  SliceXYRemap *r = &s->slice_remap[n];
4837 
4838  for (int p = 0; p < s->nb_allocated; p++) {
4839  av_freep(&r->u[p]);
4840  av_freep(&r->v[p]);
4841  av_freep(&r->ker[p]);
4842  }
4843 
4844  av_freep(&r->mask);
4845  }
4846 
4847  av_freep(&s->slice_remap);
4848 }
4849 
4850 static const AVFilterPad inputs[] = {
4851  {
4852  .name = "default",
4853  .type = AVMEDIA_TYPE_VIDEO,
4854  .filter_frame = filter_frame,
4855  },
4856  { NULL }
4857 };
4858 
4859 static const AVFilterPad outputs[] = {
4860  {
4861  .name = "default",
4862  .type = AVMEDIA_TYPE_VIDEO,
4863  .config_props = config_output,
4864  },
4865  { NULL }
4866 };
4867 
4869  .name = "v360",
4870  .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
4871  .priv_size = sizeof(V360Context),
4872  .init = init,
4873  .uninit = uninit,
4875  .inputs = inputs,
4876  .outputs = outputs,
4877  .priv_class = &v360_class,
4880 };
static double val(void *priv, double ch)
Definition: aeval.c:76
static const char *const format[]
Definition: af_aiir.c:457
#define av_always_inline
Definition: attributes.h:45
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:885
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:805
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:274
#define us(width, name, range_min, range_max, subs,...)
Definition: cbs_h2645.c:288
#define ui(width, name)
Definition: cbs_mpeg2.c:43
#define s(width, name)
Definition: cbs_vp9.c:257
#define LEFT
Definition: cdgraphics.c:166
#define RIGHT
Definition: cdgraphics.c:167
#define FFMAX3(a, b, c)
Definition: common.h:104
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define FF_CEIL_RSHIFT
Definition: common.h:61
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define av_clip
Definition: common.h:122
#define FFMAX(a, b)
Definition: common.h:103
#define av_clipf
Definition: common.h:170
#define FFSIGN(a)
Definition: common.h:73
#define ARCH_X86
Definition: config.h:39
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static __device__ float fabsf(float a)
Definition: cuda_runtime.h:181
static __device__ float floorf(float a)
Definition: cuda_runtime.h:172
static __device__ float ceilf(float a)
Definition: cuda_runtime.h:175
int
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition: filters.h:282
#define S(s, c, i)
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
static const int16_t alpha[]
Definition: ilbcdata.h:55
misc image utilities
int i
Definition: input.c:407
const char * arg
Definition: jacosubdec.c:67
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
#define isfinite(x)
Definition: libm.h:359
#define isnan(x)
Definition: libm.h:340
#define atanf(x)
Definition: libm.h:40
#define sinf(x)
Definition: libm.h:419
#define cosf(x)
Definition: libm.h:78
#define expf(x)
Definition: libm.h:283
#define atan2f(y, x)
Definition: libm.h:45
#define lrintf(x)
Definition: libm_mips.h:70
const char * desc
Definition: libsvtav1.c:83
uint8_t w
Definition: llviddspenc.c:39
#define FFALIGN(x, a)
Definition: macros.h:48
#define M_SQRT2
Definition: mathematics.h:61
#define M_PI_2
Definition: mathematics.h:55
#define M_PI
Definition: mathematics.h:52
#define TOP_LEFT
Definition: movtextdec.c:49
#define TOP_RIGHT
Definition: movtextdec.c:51
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2035
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:179
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:420
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:410
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:405
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:379
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:421
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:414
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:397
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:441
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:436
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:434
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:415
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:416
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:433
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:437
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:407
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:439
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:408
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:382
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:401
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:419
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:442
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:418
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:435
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:417
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:440
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
const char * name
Definition: qsvenc.c:46
#define td
Definition: regdef.h:70
static const ElemCat * elements[ELEMENT_COUNT]
Definition: signature.h:566
An instance of a filter.
Definition: avfilter.h:341
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
Used for passing data between threads.
Definition: dsddec.c:67
AVFrame * out
Definition: af_adeclick.c:505
AVFrame * in
Definition: af_adenorm.c:223
Definition: v360.h:107
int16_t u[4][4]
Definition: v360.h:108
int16_t v[4][4]
Definition: v360.h:109
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
static const uint8_t q1[256]
Definition: twofish.c:96
static const uint8_t q0[256]
Definition: twofish.c:77
@ NB_FACES
Definition: v360.h:79
@ TOP_MIDDLE
Definition: v360.h:74
@ BOTTOM_RIGHT
Definition: v360.h:78
@ BOTTOM_LEFT
Definition: v360.h:76
@ BOTTOM_MIDDLE
Definition: v360.h:77
@ FRONT
Axis -Z.
Definition: v360.h:87
@ DOWN
Axis -Y.
Definition: v360.h:86
@ NB_DIRECTIONS
Definition: v360.h:89
@ UP
Axis +Y.
Definition: v360.h:85
@ BACK
Axis +Z.
Definition: v360.h:88
@ ROT_90
Definition: v360.h:94
@ ROT_270
Definition: v360.h:96
@ ROT_0
Definition: v360.h:93
@ ROT_180
Definition: v360.h:95
@ SPLINE16
Definition: v360.h:66
@ NB_INTERP_METHODS
Definition: v360.h:69
@ LAGRANGE9
Definition: v360.h:63
@ MITCHELL
Definition: v360.h:68
@ BILINEAR
Definition: v360.h:62
@ BICUBIC
Definition: v360.h:64
@ LANCZOS
Definition: v360.h:65
@ GAUSSIAN
Definition: v360.h:67
void ff_v360_init_x86(V360Context *s, int depth)
Definition: vf_v360_init.c:44
@ EQUISOLID
Definition: v360.h:54
@ FLAT
Definition: v360.h:37
@ FISHEYE
Definition: v360.h:46
@ DUAL_FISHEYE
Definition: v360.h:38
@ EQUIRECTANGULAR
Definition: v360.h:33
@ PERSPECTIVE
Definition: v360.h:49
@ TSPYRAMID
Definition: v360.h:52
@ CUBEMAP_3_2
Definition: v360.h:34
@ CUBEMAP_6_1
Definition: v360.h:35
@ SINUSOIDAL
Definition: v360.h:45
@ HEQUIRECTANGULAR
Definition: v360.h:53
@ OCTAHEDRON
Definition: v360.h:56
@ HAMMER
Definition: v360.h:44
@ CUBEMAP_1_6
Definition: v360.h:40
@ STEREOGRAPHIC
Definition: v360.h:41
@ CYLINDRICAL
Definition: v360.h:48
@ BALL
Definition: v360.h:43
@ PANNINI
Definition: v360.h:47
@ BARREL_SPLIT
Definition: v360.h:51
@ EQUIANGULAR
Definition: v360.h:36
@ BARREL
Definition: v360.h:39
@ NB_PROJECTIONS
Definition: v360.h:57
@ TETRAHEDRON
Definition: v360.h:50
@ MERCATOR
Definition: v360.h:42
@ ORTHOGRAPHIC
Definition: v360.h:55
@ STEREO_SBS
Definition: v360.h:27
@ STEREO_TB
Definition: v360.h:28
@ STEREO_2D
Definition: v360.h:26
@ NB_STEREO_FMTS
Definition: v360.h:29
@ ROLL
Definition: v360.h:103
@ NB_RORDERS
Definition: v360.h:104
@ YAW
Definition: v360.h:101
@ PITCH
Definition: v360.h:102
const char * b
Definition: vf_curves.c:119
const char * r
Definition: vf_curves.c:117
#define NEAREST(type, name)
else temp
Definition: vf_mcdeint.c:259
if(ret< 0)
Definition: vf_mcdeint.c:282
static enum AVPixelFormat alpha_pix_fmts[]
Definition: vf_overlay.c:156
static int prepare_orthographic_in(AVFilterContext *ctx)
Prepare data for processing orthographic input format.
Definition: vf_v360.c:2065
static int prepare_cylindrical_in(AVFilterContext *ctx)
Prepare data for processing cylindrical input format.
Definition: vf_v360.c:3062
static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
Definition: vf_v360.c:4086
static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_v360.c:4095
AVFilter ff_vf_v360
Definition: vf_v360.c:4868
static void nearest_kernel(float du, float dv, const XYRemap *rmap, int16_t *u, int16_t *v, int16_t *ker)
Save nearest pixel coordinates for remapping.
Definition: vf_v360.c:406
static int flat_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in flat format.
Definition: vf_v360.c:2795
static void calculate_cubic_bc_coeffs(float t, float *coeffs, float b, float c)
Calculate 1-dimensional cubic_bc_spline coefficients.
Definition: vf_v360.c:682
static int hequirect_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in half equirectangular format.
Definition: vf_v360.c:1772
static void spline16_kernel(float du, float dv, const XYRemap *rmap, int16_t *u, int16_t *v, int16_t *ker)
Calculate kernel for spline16 interpolation.
Definition: vf_v360.c:605
static int prepare_eac_in(AVFilterContext *ctx)
Prepare data for processing equi-angular cubemap input format.
Definition: vf_v360.c:2556
static int xyz_to_hequirect(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2168
static int xyz_to_mercator(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2272
static int xyz_to_cube6x1(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:1665
static void calculate_bicubic_coeffs(float t, float *coeffs)
Calculate 1-dimensional cubic coefficients.
Definition: vf_v360.c:489
static void rotate(const float rot_quaternion[2][4], float *vec)
Rotate vector with given rotation quaternion.
Definition: vf_v360.c:3933
#define DEFINE_REMAP1_LINE(bits, div)
Definition: vf_v360.c:255
static int xyz_to_barrel(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:3418
static int cube3x2_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
Definition: vf_v360.c:1389
static int get_output_dimension(AVFilterContext *ctx, const char *name, float val, int *dim)
Definition: vf_v360.c:4158
static int prepare_eac_out(AVFilterContext *ctx)
Prepare data for processing equi-angular cubemap output format.
Definition: vf_v360.c:2584
static int xyz_to_equisolid(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in equisolid format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:1982
static int barrelsplit_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format...
Definition: vf_v360.c:3584
static int prepare_flat_out(AVFilterContext *ctx)
Prepare data for processing flat output format.
Definition: vf_v360.c:2775
static void input_flip(int16_t u[4][4], int16_t v[4][4], int w, int h, int hflip, int vflip)
Definition: vf_v360.c:3966
static int xyz_to_eac(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2720
#define DEFINE_REMAP(ws, bits)
Generate remapping function with a given window size and pixel depth.
Definition: vf_v360.c:279
static int xyz_to_fisheye(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2891
static int dfisheye_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
Definition: vf_v360.c:3253
static int sinusoidal_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
Definition: vf_v360.c:2490
static int xyz_to_octahedron(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in octahedron format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:3839
static int ball_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in ball format.
Definition: vf_v360.c:2376
static void calculate_lanczos_coeffs(float t, float *coeffs)
Calculate 1-dimensional lanczos coefficients.
Definition: vf_v360.c:534
static void calculate_gaussian_coeffs(float t, float *coeffs)
Calculate 1-dimensional gaussian coefficients.
Definition: vf_v360.c:629
static void conjugate_quaternion(float d[4], const float q[4])
Definition: vf_v360.c:3887
static void normalize_vector(float *vec)
Normalize vector.
Definition: vf_v360.c:1037
static int get_direction(char c)
Convert char to corresponding direction.
Definition: vf_v360.c:812
void ff_v360_init(V360Context *s, int depth)
Definition: vf_v360.c:371
static int equirect_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
Definition: vf_v360.c:1743
static int cube1x6_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
Definition: vf_v360.c:1517
static void projection_min_size(int projection, int *min_w, int *min_h)
Definition: vf_v360.c:4172
#define TFLAGS
Definition: vf_v360.c:56
static void calculate_spline16_coeffs(float t, float *coeffs)
Calculate 1-dimensional spline16 coefficients.
Definition: vf_v360.c:587
static int reflectx(int x, int y, int w, int h)
Reflect x operation.
Definition: vf_v360.c:800
static int query_formats(AVFilterContext *ctx)
Definition: vf_v360.c:171
static int xyz_to_dfisheye(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:3294
static int xyz_to_pannini(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in pannini format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2970
static int orthographic_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in orthographic format.
Definition: vf_v360.c:2040
#define FLAGS
Definition: vf_v360.c:55
static const AVFilterPad inputs[]
Definition: vf_v360.c:4850
static int prepare_cube_in(AVFilterContext *ctx)
Prepare data for processing cubemap input format.
Definition: vf_v360.c:879
static int prepare_fisheye_in(AVFilterContext *ctx)
Prepare data for processing fisheye input format.
Definition: vf_v360.c:2869
static void mirror(const float *modifier, float *vec)
Definition: vf_v360.c:3959
static void lanczos_kernel(float du, float dv, const XYRemap *rmap, int16_t *u, int16_t *v, int16_t *ker)
Calculate kernel for lanczos interpolation.
Definition: vf_v360.c:563
static void cube_to_xyz(const V360Context *s, float uf, float vf, int face, float *vec, float scalew, float scaleh)
Calculate 3D coordinates on sphere for corresponding cubemap position.
Definition: vf_v360.c:1058
static int perspective_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
Definition: vf_v360.c:3124
static const AVFilterPad outputs[]
Definition: vf_v360.c:4859
static int ereflectx(int x, int y, int w, int h)
Reflect x operation for equirect.
Definition: vf_v360.c:784
static int prepare_equisolid_out(AVFilterContext *ctx)
Prepare data for processing equisolid output format.
Definition: vf_v360.c:1909
static void gaussian_kernel(float du, float dv, const XYRemap *rmap, int16_t *u, int16_t *v, int16_t *ker)
Calculate kernel for gaussian interpolation.
Definition: vf_v360.c:658
static int prepare_fisheye_out(AVFilterContext *ctx)
Prepare data for processing fisheye output format.
Definition: vf_v360.c:2818
static int tspyramid_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in tspyramid format.
Definition: vf_v360.c:3676
static void process_cube_coordinates(const V360Context *s, float uf, float vf, int direction, float *new_uf, float *new_vf, int *face)
Find position on another cube face in case of overflow/underflow.
Definition: vf_v360.c:1197
static int get_rotation(char c)
Convert char to corresponding rotation angle.
Definition: vf_v360.c:836
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_v360.c:4782
static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
Definition: vf_v360.c:3983
static void rotate_cube_face(float *uf, float *vf, int rotation)
Definition: vf_v360.c:980
static int xyz_to_barrelsplit(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere...
Definition: vf_v360.c:3490
static int xyz_to_cylindrical(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:3084
static int xyz_to_stereographic(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:1871
static int reflecty(int y, int h)
Reflect y operation.
Definition: vf_v360.c:765
static void bicubic_kernel(float du, float dv, const XYRemap *rmap, int16_t *u, int16_t *v, int16_t *ker)
Calculate kernel for bicubic interpolation.
Definition: vf_v360.c:510
static int stereographic_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
Definition: vf_v360.c:1818
static void set_mirror_modifier(int h_flip, int v_flip, int d_flip, float *modifier)
Definition: vf_v360.c:3951
static int xyz_to_cube1x6(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:1585
static int xyz_to_flat(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in flat format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2225
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_v360.c:4806
static av_cold int init(AVFilterContext *ctx)
Definition: vf_v360.c:4821
static void lagrange_kernel(float du, float dv, const XYRemap *rmap, int16_t *u, int16_t *v, int16_t *ker)
Calculate kernel for lagrange interpolation.
Definition: vf_v360.c:465
static int xyz_to_orthographic(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in orthographic format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2087
static void xyz_to_cube(const V360Context *s, const float *vec, float *uf, float *vf, int *direction)
Calculate cubemap position for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:1122
static int cube6x1_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
Definition: vf_v360.c:1550
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_v360.c:4831
static int prepare_stereographic_out(AVFilterContext *ctx)
Prepare data for processing stereographic output format.
Definition: vf_v360.c:1798
static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
Definition: vf_v360.c:4017
static int xyz_to_equirect(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2130
static int prepare_cylindrical_out(AVFilterContext *ctx)
Prepare data for processing cylindrical output format.
Definition: vf_v360.c:3011
static int xyz_to_cube3x2(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:1428
static int xyz_to_hammer(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2451
static int pannini_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
Definition: vf_v360.c:2934
static void mitchell_kernel(float du, float dv, const XYRemap *rmap, int16_t *u, int16_t *v, int16_t *ker)
Calculate kernel for mitchell interpolation.
Definition: vf_v360.c:723
static int xyz_to_sinusoidal(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2523
static int mercator_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
Definition: vf_v360.c:2308
static void calculate_lagrange_coeffs(float t, float *coeffs)
Calculate 1-dimensional lagrange coefficients.
Definition: vf_v360.c:448
static int xyz_to_tetrahedron(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:3198
#define OFFSET(x)
Definition: vf_v360.c:54
static int config_output(AVFilterLink *outlink)
Definition: vf_v360.c:4186
static int prepare_flat_in(AVFilterContext *ctx)
Prepare data for processing flat input format.
Definition: vf_v360.c:2203
static int prepare_orthographic_out(AVFilterContext *ctx)
Prepare data for processing orthographic output format.
Definition: vf_v360.c:2020
static int tetrahedron_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
Definition: vf_v360.c:3170
static int cylindrical_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
Definition: vf_v360.c:3031
static int hammer_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
Definition: vf_v360.c:2410
static int prepare_equisolid_in(AVFilterContext *ctx)
Prepare data for processing equisolid input format.
Definition: vf_v360.c:1960
static int barrel_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
Definition: vf_v360.c:3344
static int equisolid_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in equisolid format.
Definition: vf_v360.c:1929
static void multiply_quaternion(float c[4], const float a[4], const float b[4])
Definition: vf_v360.c:3879
AVFILTER_DEFINE_CLASS(v360)
#define DEFINE_REMAP_LINE(ws, bits, div)
Definition: vf_v360.c:336
static int xyz_to_ball(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in ball format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:2340
static void calculate_rotation(float yaw, float pitch, float roll, float rot_quaternion[2][4], const int rotation_order[3])
Calculate rotation quaternion for yaw/pitch/roll angles.
Definition: vf_v360.c:3898
static int get_rorder(char c)
Convert char to corresponding rotation order.
Definition: vf_v360.c:855
static int eac_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
Definition: vf_v360.c:2615
static int octahedron_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in octahedron format.
Definition: vf_v360.c:3804
static int xyz_to_tspyramid(const V360Context *s, const float *vec, int width, int height, int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
Calculate frame position in tspyramid format for corresponding 3D coordinates on sphere.
Definition: vf_v360.c:3736
static int fisheye_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec)
Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
Definition: vf_v360.c:2838
static void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
Definition: vf_v360.c:1006
static int prepare_cube_out(AVFilterContext *ctx)
Prepare data for processing cubemap output format.
Definition: vf_v360.c:933
static const AVOption v360_options[]
Definition: vf_v360.c:58
static int mod(int a, int b)
Modulo operation with only positive remainders.
Definition: vf_v360.c:749
static int prepare_stereographic_in(AVFilterContext *ctx)
Prepare data for processing stereographic input format.
Definition: vf_v360.c:1849
static void bilinear_kernel(float du, float dv, const XYRemap *rmap, int16_t *u, int16_t *v, int16_t *ker)
Calculate kernel for bilinear interpolation.
Definition: vf_v360.c:426
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104
int dim
static double c[64]