2 * Copyright (c) 2019 Eugene Lyapustin
4 * This file is part of FFmpeg.
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.
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.
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
23 * 360 video conversion filter.
24 * Principle of operation:
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
33 * 5) Remap input frame to output frame using precalculated data
38 #include "libavutil/avassert.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/opt.h"
48 typedef struct ThreadData {
53 #define OFFSET(x) offsetof(V360Context, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56 static const AVOption v360_options[] = {
57 { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
58 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
59 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
60 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
61 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
62 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
63 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
64 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
65 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
66 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
67 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
68 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
69 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
70 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
71 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
72 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
73 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
74 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" },
75 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" },
76 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
77 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
78 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
79 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
80 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
81 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
82 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
83 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
84 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
85 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
86 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
87 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
88 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
89 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
90 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
91 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
92 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
93 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
94 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
95 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
96 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
97 {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
98 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
99 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
100 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
101 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
102 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
103 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
104 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
105 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
106 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
107 { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
108 { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
109 { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
110 { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
111 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
112 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
113 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
114 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
115 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
116 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
117 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
118 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
119 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
120 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
121 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
122 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
123 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
124 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fin_pad"},
125 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fout_pad"},
126 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
127 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
128 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
129 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
130 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "h_fov"},
131 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "v_fov"},
132 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "d_fov"},
133 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
134 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
135 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
136 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
137 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
138 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
139 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
140 { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "ih_fov"},
141 { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "iv_fov"},
142 { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "id_fov"},
146 AVFILTER_DEFINE_CLASS(v360);
148 static int query_formats(AVFilterContext *ctx)
150 static const enum AVPixelFormat pix_fmts[] = {
152 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
153 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
154 AV_PIX_FMT_YUVA444P16,
157 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
158 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
159 AV_PIX_FMT_YUVA422P16,
162 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
163 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
166 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
167 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
171 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
172 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
173 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
176 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
177 AV_PIX_FMT_YUV440P12,
180 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
181 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
182 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
185 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
186 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
187 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
196 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
197 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
198 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
201 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
202 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
205 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
206 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
207 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
212 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
214 return AVERROR(ENOMEM);
215 return ff_set_common_formats(ctx, fmts_list);
218 #define DEFINE_REMAP1_LINE(bits, div) \
219 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
220 ptrdiff_t in_linesize, \
221 const int16_t *const u, const int16_t *const v, \
222 const int16_t *const ker) \
224 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
225 uint##bits##_t *d = (uint##bits##_t *)dst; \
227 in_linesize /= div; \
229 for (int x = 0; x < width; x++) \
230 d[x] = s[v[x] * in_linesize + u[x]]; \
233 DEFINE_REMAP1_LINE( 8, 1)
234 DEFINE_REMAP1_LINE(16, 2)
237 * Generate remapping function with a given window size and pixel depth.
239 * @param ws size of interpolation window
240 * @param bits number of bits per pixel
242 #define DEFINE_REMAP(ws, bits) \
243 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
245 ThreadData *td = arg; \
246 const V360Context *s = ctx->priv; \
247 const AVFrame *in = td->in; \
248 AVFrame *out = td->out; \
250 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
251 for (int plane = 0; plane < s->nb_planes; plane++) { \
252 const unsigned map = s->map[plane]; \
253 const int in_linesize = in->linesize[plane]; \
254 const int out_linesize = out->linesize[plane]; \
255 const int uv_linesize = s->uv_linesize[plane]; \
256 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
257 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
258 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
259 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
260 const uint8_t *const src = in->data[plane] + \
261 in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
262 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
263 const int width = s->pr_width[plane]; \
264 const int height = s->pr_height[plane]; \
266 const int slice_start = (height * jobnr ) / nb_jobs; \
267 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
269 for (int y = slice_start; y < slice_end; y++) { \
270 const int16_t *const u = s->u[map] + y * uv_linesize * ws * ws; \
271 const int16_t *const v = s->v[map] + y * uv_linesize * ws * ws; \
272 const int16_t *const ker = s->ker[map] + y * uv_linesize * ws * ws; \
274 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
289 #define DEFINE_REMAP_LINE(ws, bits, div) \
290 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
291 ptrdiff_t in_linesize, \
292 const int16_t *const u, const int16_t *const v, \
293 const int16_t *const ker) \
295 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
296 uint##bits##_t *d = (uint##bits##_t *)dst; \
298 in_linesize /= div; \
300 for (int x = 0; x < width; x++) { \
301 const int16_t *const uu = u + x * ws * ws; \
302 const int16_t *const vv = v + x * ws * ws; \
303 const int16_t *const kker = ker + x * ws * ws; \
306 for (int i = 0; i < ws; i++) { \
307 for (int j = 0; j < ws; j++) { \
308 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
312 d[x] = av_clip_uint##bits(tmp >> 14); \
316 DEFINE_REMAP_LINE(2, 8, 1)
317 DEFINE_REMAP_LINE(4, 8, 1)
318 DEFINE_REMAP_LINE(2, 16, 2)
319 DEFINE_REMAP_LINE(4, 16, 2)
321 void ff_v360_init(V360Context *s, int depth)
325 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
328 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
334 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
339 ff_v360_init_x86(s, depth);
343 * Save nearest pixel coordinates for remapping.
345 * @param du horizontal relative coordinate
346 * @param dv vertical relative coordinate
347 * @param rmap calculated 4x4 window
348 * @param u u remap data
349 * @param v v remap data
350 * @param ker ker remap data
352 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
353 int16_t *u, int16_t *v, int16_t *ker)
355 const int i = lrintf(dv) + 1;
356 const int j = lrintf(du) + 1;
358 u[0] = rmap->u[i][j];
359 v[0] = rmap->v[i][j];
363 * Calculate kernel for bilinear interpolation.
365 * @param du horizontal relative coordinate
366 * @param dv vertical relative coordinate
367 * @param rmap calculated 4x4 window
368 * @param u u remap data
369 * @param v v remap data
370 * @param ker ker remap data
372 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
373 int16_t *u, int16_t *v, int16_t *ker)
375 for (int i = 0; i < 2; i++) {
376 for (int j = 0; j < 2; j++) {
377 u[i * 2 + j] = rmap->u[i + 1][j + 1];
378 v[i * 2 + j] = rmap->v[i + 1][j + 1];
382 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
383 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
384 ker[2] = lrintf((1.f - du) * dv * 16385.f);
385 ker[3] = lrintf( du * dv * 16385.f);
389 * Calculate 1-dimensional cubic coefficients.
391 * @param t relative coordinate
392 * @param coeffs coefficients
394 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
396 const float tt = t * t;
397 const float ttt = t * t * t;
399 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
400 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
401 coeffs[2] = t + tt / 2.f - ttt / 2.f;
402 coeffs[3] = - t / 6.f + ttt / 6.f;
406 * Calculate kernel for bicubic interpolation.
408 * @param du horizontal relative coordinate
409 * @param dv vertical relative coordinate
410 * @param rmap calculated 4x4 window
411 * @param u u remap data
412 * @param v v remap data
413 * @param ker ker remap data
415 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
416 int16_t *u, int16_t *v, int16_t *ker)
421 calculate_bicubic_coeffs(du, du_coeffs);
422 calculate_bicubic_coeffs(dv, dv_coeffs);
424 for (int i = 0; i < 4; i++) {
425 for (int j = 0; j < 4; j++) {
426 u[i * 4 + j] = rmap->u[i][j];
427 v[i * 4 + j] = rmap->v[i][j];
428 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
434 * Calculate 1-dimensional lanczos coefficients.
436 * @param t relative coordinate
437 * @param coeffs coefficients
439 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
443 for (int i = 0; i < 4; i++) {
444 const float x = M_PI * (t - i + 1);
448 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
453 for (int i = 0; i < 4; i++) {
459 * Calculate kernel for lanczos interpolation.
461 * @param du horizontal relative coordinate
462 * @param dv vertical relative coordinate
463 * @param rmap calculated 4x4 window
464 * @param u u remap data
465 * @param v v remap data
466 * @param ker ker remap data
468 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
469 int16_t *u, int16_t *v, int16_t *ker)
474 calculate_lanczos_coeffs(du, du_coeffs);
475 calculate_lanczos_coeffs(dv, dv_coeffs);
477 for (int i = 0; i < 4; i++) {
478 for (int j = 0; j < 4; j++) {
479 u[i * 4 + j] = rmap->u[i][j];
480 v[i * 4 + j] = rmap->v[i][j];
481 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
487 * Calculate 1-dimensional spline16 coefficients.
489 * @param t relative coordinate
490 * @param coeffs coefficients
492 static void calculate_spline16_coeffs(float t, float *coeffs)
494 coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
495 coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
496 coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
497 coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
501 * Calculate kernel for spline16 interpolation.
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
510 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
511 int16_t *u, int16_t *v, int16_t *ker)
516 calculate_spline16_coeffs(du, du_coeffs);
517 calculate_spline16_coeffs(dv, dv_coeffs);
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);
529 * Calculate 1-dimensional gaussian coefficients.
531 * @param t relative coordinate
532 * @param coeffs coefficients
534 static void calculate_gaussian_coeffs(float t, float *coeffs)
538 for (int i = 0; i < 4; i++) {
539 const float x = t - (i - 1);
543 coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
548 for (int i = 0; i < 4; i++) {
554 * Calculate kernel for gaussian interpolation.
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
563 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
564 int16_t *u, int16_t *v, int16_t *ker)
569 calculate_gaussian_coeffs(du, du_coeffs);
570 calculate_gaussian_coeffs(dv, dv_coeffs);
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);
582 * Modulo operation with only positive remainders.
587 * @return positive remainder of (a / b)
589 static inline int mod(int a, int b)
591 const int res = a % b;
600 * Convert char to corresponding direction.
601 * Used for cubemap options.
603 static int get_direction(char c)
624 * Convert char to corresponding rotation angle.
625 * Used for cubemap options.
627 static int get_rotation(char c)
644 * Convert char to corresponding rotation order.
646 static int get_rorder(char c)
664 * Prepare data for processing cubemap input format.
666 * @param ctx filter context
670 static int prepare_cube_in(AVFilterContext *ctx)
672 V360Context *s = ctx->priv;
674 for (int face = 0; face < NB_FACES; face++) {
675 const char c = s->in_forder[face];
679 av_log(ctx, AV_LOG_ERROR,
680 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
681 return AVERROR(EINVAL);
684 direction = get_direction(c);
685 if (direction == -1) {
686 av_log(ctx, AV_LOG_ERROR,
687 "Incorrect direction symbol '%c' in in_forder option.\n", c);
688 return AVERROR(EINVAL);
691 s->in_cubemap_face_order[direction] = face;
694 for (int face = 0; face < NB_FACES; face++) {
695 const char c = s->in_frot[face];
699 av_log(ctx, AV_LOG_ERROR,
700 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
701 return AVERROR(EINVAL);
704 rotation = get_rotation(c);
705 if (rotation == -1) {
706 av_log(ctx, AV_LOG_ERROR,
707 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
708 return AVERROR(EINVAL);
711 s->in_cubemap_face_rotation[face] = rotation;
718 * Prepare data for processing cubemap output format.
720 * @param ctx filter context
724 static int prepare_cube_out(AVFilterContext *ctx)
726 V360Context *s = ctx->priv;
728 for (int face = 0; face < NB_FACES; face++) {
729 const char c = s->out_forder[face];
733 av_log(ctx, AV_LOG_ERROR,
734 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
735 return AVERROR(EINVAL);
738 direction = get_direction(c);
739 if (direction == -1) {
740 av_log(ctx, AV_LOG_ERROR,
741 "Incorrect direction symbol '%c' in out_forder option.\n", c);
742 return AVERROR(EINVAL);
745 s->out_cubemap_direction_order[face] = direction;
748 for (int face = 0; face < NB_FACES; face++) {
749 const char c = s->out_frot[face];
753 av_log(ctx, AV_LOG_ERROR,
754 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
755 return AVERROR(EINVAL);
758 rotation = get_rotation(c);
759 if (rotation == -1) {
760 av_log(ctx, AV_LOG_ERROR,
761 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
762 return AVERROR(EINVAL);
765 s->out_cubemap_face_rotation[face] = rotation;
771 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
797 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
828 static void normalize_vector(float *vec)
830 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
838 * Calculate 3D coordinates on sphere for corresponding cubemap position.
839 * Common operation for every cubemap.
841 * @param s filter private context
842 * @param uf horizontal cubemap coordinate [0, 1)
843 * @param vf vertical cubemap coordinate [0, 1)
844 * @param face face of cubemap
845 * @param vec coordinates on sphere
846 * @param scalew scale for uf
847 * @param scaleh scale for vf
849 static void cube_to_xyz(const V360Context *s,
850 float uf, float vf, int face,
851 float *vec, float scalew, float scaleh)
853 const int direction = s->out_cubemap_direction_order[face];
859 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
900 normalize_vector(vec);
904 * Calculate cubemap position for corresponding 3D coordinates on sphere.
905 * Common operation for every cubemap.
907 * @param s filter private context
908 * @param vec coordinated on sphere
909 * @param uf horizontal cubemap coordinate [0, 1)
910 * @param vf vertical cubemap coordinate [0, 1)
911 * @param direction direction of view
913 static void xyz_to_cube(const V360Context *s,
915 float *uf, float *vf, int *direction)
917 const float phi = atan2f(vec[0], -vec[2]);
918 const float theta = asinf(-vec[1]);
919 float phi_norm, theta_threshold;
922 if (phi >= -M_PI_4 && phi < M_PI_4) {
925 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
927 phi_norm = phi + M_PI_2;
928 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
930 phi_norm = phi - M_PI_2;
933 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
936 theta_threshold = atanf(cosf(phi_norm));
937 if (theta > theta_threshold) {
939 } else if (theta < -theta_threshold) {
943 switch (*direction) {
945 *uf = vec[2] / vec[0];
946 *vf = -vec[1] / vec[0];
949 *uf = vec[2] / vec[0];
950 *vf = vec[1] / vec[0];
953 *uf = vec[0] / vec[1];
954 *vf = -vec[2] / vec[1];
957 *uf = -vec[0] / vec[1];
958 *vf = -vec[2] / vec[1];
961 *uf = -vec[0] / vec[2];
962 *vf = vec[1] / vec[2];
965 *uf = -vec[0] / vec[2];
966 *vf = -vec[1] / vec[2];
972 face = s->in_cubemap_face_order[*direction];
973 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
975 (*uf) *= s->input_mirror_modifier[0];
976 (*vf) *= s->input_mirror_modifier[1];
980 * Find position on another cube face in case of overflow/underflow.
981 * Used for calculation of interpolation window.
983 * @param s filter private context
984 * @param uf horizontal cubemap coordinate
985 * @param vf vertical cubemap coordinate
986 * @param direction direction of view
987 * @param new_uf new horizontal cubemap coordinate
988 * @param new_vf new vertical cubemap coordinate
989 * @param face face position on cubemap
991 static void process_cube_coordinates(const V360Context *s,
992 float uf, float vf, int direction,
993 float *new_uf, float *new_vf, int *face)
996 * Cubemap orientation
1003 * +-------+-------+-------+-------+ ^ e |
1005 * | left | front | right | back | | g |
1006 * +-------+-------+-------+-------+ v h v
1012 *face = s->in_cubemap_face_order[direction];
1013 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1015 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1016 // There are no pixels to use in this case
1019 } else if (uf < -1.f) {
1021 switch (direction) {
1055 } else if (uf >= 1.f) {
1057 switch (direction) {
1091 } else if (vf < -1.f) {
1093 switch (direction) {
1127 } else if (vf >= 1.f) {
1129 switch (direction) {
1169 *face = s->in_cubemap_face_order[direction];
1170 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1174 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1176 * @param s filter private context
1177 * @param i horizontal position on frame [0, width)
1178 * @param j vertical position on frame [0, height)
1179 * @param width frame width
1180 * @param height frame height
1181 * @param vec coordinates on sphere
1183 static void cube3x2_to_xyz(const V360Context *s,
1184 int i, int j, int width, int height,
1187 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 3.f) : 1.f - s->out_pad;
1188 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1190 const float ew = width / 3.f;
1191 const float eh = height / 2.f;
1193 const int u_face = floorf(i / ew);
1194 const int v_face = floorf(j / eh);
1195 const int face = u_face + 3 * v_face;
1197 const int u_shift = ceilf(ew * u_face);
1198 const int v_shift = ceilf(eh * v_face);
1199 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1200 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1202 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1203 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1205 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1209 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1211 * @param s filter private context
1212 * @param vec coordinates on sphere
1213 * @param width frame width
1214 * @param height frame height
1215 * @param us horizontal coordinates for interpolation window
1216 * @param vs vertical coordinates for interpolation window
1217 * @param du horizontal relative coordinate
1218 * @param dv vertical relative coordinate
1220 static void xyz_to_cube3x2(const V360Context *s,
1221 const float *vec, int width, int height,
1222 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1224 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 3.f) : 1.f - s->in_pad;
1225 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1226 const float ew = width / 3.f;
1227 const float eh = height / 2.f;
1231 int direction, face;
1234 xyz_to_cube(s, vec, &uf, &vf, &direction);
1239 face = s->in_cubemap_face_order[direction];
1242 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1243 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1245 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1246 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1254 for (int i = -1; i < 3; i++) {
1255 for (int j = -1; j < 3; j++) {
1256 int new_ui = ui + j;
1257 int new_vi = vi + i;
1258 int u_shift, v_shift;
1259 int new_ewi, new_ehi;
1261 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1262 face = s->in_cubemap_face_order[direction];
1266 u_shift = ceilf(ew * u_face);
1267 v_shift = ceilf(eh * v_face);
1269 uf = 2.f * new_ui / ewi - 1.f;
1270 vf = 2.f * new_vi / ehi - 1.f;
1275 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1282 u_shift = ceilf(ew * u_face);
1283 v_shift = ceilf(eh * v_face);
1284 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1285 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1287 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1288 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1291 us[i + 1][j + 1] = u_shift + new_ui;
1292 vs[i + 1][j + 1] = v_shift + new_vi;
1298 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1300 * @param s filter private context
1301 * @param i horizontal position on frame [0, width)
1302 * @param j vertical position on frame [0, height)
1303 * @param width frame width
1304 * @param height frame height
1305 * @param vec coordinates on sphere
1307 static void cube1x6_to_xyz(const V360Context *s,
1308 int i, int j, int width, int height,
1311 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1312 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1314 const float ew = width;
1315 const float eh = height / 6.f;
1317 const int face = floorf(j / eh);
1319 const int v_shift = ceilf(eh * face);
1320 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1322 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1323 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1325 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1329 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1331 * @param s filter private context
1332 * @param i horizontal position on frame [0, width)
1333 * @param j vertical position on frame [0, height)
1334 * @param width frame width
1335 * @param height frame height
1336 * @param vec coordinates on sphere
1338 static void cube6x1_to_xyz(const V360Context *s,
1339 int i, int j, int width, int height,
1342 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f) : 1.f - s->out_pad;
1343 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1345 const float ew = width / 6.f;
1346 const float eh = height;
1348 const int face = floorf(i / ew);
1350 const int u_shift = ceilf(ew * face);
1351 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1353 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1354 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1356 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1360 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1362 * @param s filter private context
1363 * @param vec coordinates on sphere
1364 * @param width frame width
1365 * @param height frame height
1366 * @param us horizontal coordinates for interpolation window
1367 * @param vs vertical coordinates for interpolation window
1368 * @param du horizontal relative coordinate
1369 * @param dv vertical relative coordinate
1371 static void xyz_to_cube1x6(const V360Context *s,
1372 const float *vec, int width, int height,
1373 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1375 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1376 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1377 const float eh = height / 6.f;
1378 const int ewi = width;
1382 int direction, face;
1384 xyz_to_cube(s, vec, &uf, &vf, &direction);
1389 face = s->in_cubemap_face_order[direction];
1390 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1392 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1393 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1401 for (int i = -1; i < 3; i++) {
1402 for (int j = -1; j < 3; j++) {
1403 int new_ui = ui + j;
1404 int new_vi = vi + i;
1408 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1409 face = s->in_cubemap_face_order[direction];
1411 v_shift = ceilf(eh * face);
1413 uf = 2.f * new_ui / ewi - 1.f;
1414 vf = 2.f * new_vi / ehi - 1.f;
1419 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1424 v_shift = ceilf(eh * face);
1425 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1427 new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1428 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1431 us[i + 1][j + 1] = new_ui;
1432 vs[i + 1][j + 1] = v_shift + new_vi;
1438 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1440 * @param s filter private context
1441 * @param vec coordinates on sphere
1442 * @param width frame width
1443 * @param height frame height
1444 * @param us horizontal coordinates for interpolation window
1445 * @param vs vertical coordinates for interpolation window
1446 * @param du horizontal relative coordinate
1447 * @param dv vertical relative coordinate
1449 static void xyz_to_cube6x1(const V360Context *s,
1450 const float *vec, int width, int height,
1451 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1453 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f) : 1.f - s->in_pad;
1454 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1455 const float ew = width / 6.f;
1456 const int ehi = height;
1460 int direction, face;
1462 xyz_to_cube(s, vec, &uf, &vf, &direction);
1467 face = s->in_cubemap_face_order[direction];
1468 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1470 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1471 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1479 for (int i = -1; i < 3; i++) {
1480 for (int j = -1; j < 3; j++) {
1481 int new_ui = ui + j;
1482 int new_vi = vi + i;
1486 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1487 face = s->in_cubemap_face_order[direction];
1489 u_shift = ceilf(ew * face);
1491 uf = 2.f * new_ui / ewi - 1.f;
1492 vf = 2.f * new_vi / ehi - 1.f;
1497 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1502 u_shift = ceilf(ew * face);
1503 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1505 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1506 new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1509 us[i + 1][j + 1] = u_shift + new_ui;
1510 vs[i + 1][j + 1] = new_vi;
1516 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1518 * @param s filter private context
1519 * @param i horizontal position on frame [0, width)
1520 * @param j vertical position on frame [0, height)
1521 * @param width frame width
1522 * @param height frame height
1523 * @param vec coordinates on sphere
1525 static void equirect_to_xyz(const V360Context *s,
1526 int i, int j, int width, int height,
1529 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1530 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1532 const float sin_phi = sinf(phi);
1533 const float cos_phi = cosf(phi);
1534 const float sin_theta = sinf(theta);
1535 const float cos_theta = cosf(theta);
1537 vec[0] = cos_theta * sin_phi;
1538 vec[1] = -sin_theta;
1539 vec[2] = -cos_theta * cos_phi;
1543 * Prepare data for processing stereographic output format.
1545 * @param ctx filter context
1547 * @return error code
1549 static int prepare_stereographic_out(AVFilterContext *ctx)
1551 V360Context *s = ctx->priv;
1553 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1554 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1560 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1562 * @param s filter private context
1563 * @param i horizontal position on frame [0, width)
1564 * @param j vertical position on frame [0, height)
1565 * @param width frame width
1566 * @param height frame height
1567 * @param vec coordinates on sphere
1569 static void stereographic_to_xyz(const V360Context *s,
1570 int i, int j, int width, int height,
1573 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1574 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1575 const float xy = x * x + y * y;
1577 vec[0] = 2.f * x / (1.f + xy);
1578 vec[1] = (-1.f + xy) / (1.f + xy);
1579 vec[2] = 2.f * y / (1.f + xy);
1581 normalize_vector(vec);
1585 * Prepare data for processing stereographic input format.
1587 * @param ctx filter context
1589 * @return error code
1591 static int prepare_stereographic_in(AVFilterContext *ctx)
1593 V360Context *s = ctx->priv;
1595 s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1596 s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1602 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1604 * @param s filter private context
1605 * @param vec coordinates on sphere
1606 * @param width frame width
1607 * @param height frame height
1608 * @param us horizontal coordinates for interpolation window
1609 * @param vs vertical coordinates for interpolation window
1610 * @param du horizontal relative coordinate
1611 * @param dv vertical relative coordinate
1613 static void xyz_to_stereographic(const V360Context *s,
1614 const float *vec, int width, int height,
1615 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1617 const float x = vec[0] / (1.f - vec[1]) / s->iflat_range[0] * s->input_mirror_modifier[0];
1618 const float y = vec[2] / (1.f - vec[1]) / s->iflat_range[1] * s->input_mirror_modifier[1];
1620 int visible, ui, vi;
1622 uf = (x + 1.f) * width / 2.f;
1623 vf = (y + 1.f) * height / 2.f;
1627 visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1629 *du = visible ? uf - ui : 0.f;
1630 *dv = visible ? vf - vi : 0.f;
1632 for (int i = -1; i < 3; i++) {
1633 for (int j = -1; j < 3; j++) {
1634 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1635 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1641 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1643 * @param s filter private context
1644 * @param vec coordinates on sphere
1645 * @param width frame width
1646 * @param height frame height
1647 * @param us horizontal coordinates for interpolation window
1648 * @param vs vertical coordinates for interpolation window
1649 * @param du horizontal relative coordinate
1650 * @param dv vertical relative coordinate
1652 static void xyz_to_equirect(const V360Context *s,
1653 const float *vec, int width, int height,
1654 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1656 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1657 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1661 uf = (phi / M_PI + 1.f) * width / 2.f;
1662 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1669 for (int i = -1; i < 3; i++) {
1670 for (int j = -1; j < 3; j++) {
1671 us[i + 1][j + 1] = mod(ui + j, width);
1672 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1678 * Prepare data for processing flat input format.
1680 * @param ctx filter context
1682 * @return error code
1684 static int prepare_flat_in(AVFilterContext *ctx)
1686 V360Context *s = ctx->priv;
1688 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
1689 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
1695 * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
1697 * @param s filter private context
1698 * @param vec coordinates on sphere
1699 * @param width frame width
1700 * @param height frame height
1701 * @param us horizontal coordinates for interpolation window
1702 * @param vs vertical coordinates for interpolation window
1703 * @param du horizontal relative coordinate
1704 * @param dv vertical relative coordinate
1706 static void xyz_to_flat(const V360Context *s,
1707 const float *vec, int width, int height,
1708 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1710 const float theta = acosf(vec[2]);
1711 const float r = tanf(theta);
1712 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
1713 const float zf = -vec[2];
1714 const float h = hypotf(vec[0], vec[1]);
1715 const float c = h <= 1e-6f ? 1.f : rr / h;
1716 float uf = -vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1717 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1718 int visible, ui, vi;
1720 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
1721 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
1726 visible = vi >= 0 && vi < height && ui >= 0 && ui < width;
1731 for (int i = -1; i < 3; i++) {
1732 for (int j = -1; j < 3; j++) {
1733 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1734 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1740 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1742 * @param s filter private context
1743 * @param vec coordinates on sphere
1744 * @param width frame width
1745 * @param height frame height
1746 * @param us horizontal coordinates for interpolation window
1747 * @param vs vertical coordinates for interpolation window
1748 * @param du horizontal relative coordinate
1749 * @param dv vertical relative coordinate
1751 static void xyz_to_mercator(const V360Context *s,
1752 const float *vec, int width, int height,
1753 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1755 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1756 const float theta = -vec[1] * s->input_mirror_modifier[1];
1760 uf = (phi / M_PI + 1.f) * width / 2.f;
1761 vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1768 for (int i = -1; i < 3; i++) {
1769 for (int j = -1; j < 3; j++) {
1770 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1771 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1777 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1779 * @param s filter private context
1780 * @param i horizontal position on frame [0, width)
1781 * @param j vertical position on frame [0, height)
1782 * @param width frame width
1783 * @param height frame height
1784 * @param vec coordinates on sphere
1786 static void mercator_to_xyz(const V360Context *s,
1787 int i, int j, int width, int height,
1790 const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1791 const float y = ((2.f * j) / height - 1.f) * M_PI;
1792 const float div = expf(2.f * y) + 1.f;
1794 const float sin_phi = sinf(phi);
1795 const float cos_phi = cosf(phi);
1796 const float sin_theta = -2.f * expf(y) / div;
1797 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1799 vec[0] = sin_theta * cos_phi;
1801 vec[2] = sin_theta * sin_phi;
1805 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1807 * @param s filter private context
1808 * @param vec coordinates on sphere
1809 * @param width frame width
1810 * @param height frame height
1811 * @param us horizontal coordinates for interpolation window
1812 * @param vs vertical coordinates for interpolation window
1813 * @param du horizontal relative coordinate
1814 * @param dv vertical relative coordinate
1816 static void xyz_to_ball(const V360Context *s,
1817 const float *vec, int width, int height,
1818 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1820 const float l = hypotf(vec[0], vec[1]);
1821 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1825 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1826 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1834 for (int i = -1; i < 3; i++) {
1835 for (int j = -1; j < 3; j++) {
1836 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1837 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1843 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1845 * @param s filter private context
1846 * @param i horizontal position on frame [0, width)
1847 * @param j vertical position on frame [0, height)
1848 * @param width frame width
1849 * @param height frame height
1850 * @param vec coordinates on sphere
1852 static void ball_to_xyz(const V360Context *s,
1853 int i, int j, int width, int height,
1856 const float x = (2.f * i) / width - 1.f;
1857 const float y = (2.f * j) / height - 1.f;
1858 const float l = hypotf(x, y);
1861 const float z = 2.f * l * sqrtf(1.f - l * l);
1863 vec[0] = z * x / (l > 0.f ? l : 1.f);
1864 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1865 vec[2] = -1.f + 2.f * l * l;
1874 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1876 * @param s filter private context
1877 * @param i horizontal position on frame [0, width)
1878 * @param j vertical position on frame [0, height)
1879 * @param width frame width
1880 * @param height frame height
1881 * @param vec coordinates on sphere
1883 static void hammer_to_xyz(const V360Context *s,
1884 int i, int j, int width, int height,
1887 const float x = ((2.f * i) / width - 1.f);
1888 const float y = ((2.f * j) / height - 1.f);
1890 const float xx = x * x;
1891 const float yy = y * y;
1893 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1895 const float a = M_SQRT2 * x * z;
1896 const float b = 2.f * z * z - 1.f;
1898 const float aa = a * a;
1899 const float bb = b * b;
1901 const float w = sqrtf(1.f - 2.f * yy * z * z);
1903 vec[0] = w * 2.f * a * b / (aa + bb);
1904 vec[1] = -M_SQRT2 * y * z;
1905 vec[2] = -w * (bb - aa) / (aa + bb);
1907 normalize_vector(vec);
1911 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1913 * @param s filter private context
1914 * @param vec coordinates on sphere
1915 * @param width frame width
1916 * @param height frame height
1917 * @param us horizontal coordinates for interpolation window
1918 * @param vs vertical coordinates for interpolation window
1919 * @param du horizontal relative coordinate
1920 * @param dv vertical relative coordinate
1922 static void xyz_to_hammer(const V360Context *s,
1923 const float *vec, int width, int height,
1924 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1926 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1928 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1929 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1930 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1934 uf = (x + 1.f) * width / 2.f;
1935 vf = (y + 1.f) * height / 2.f;
1942 for (int i = -1; i < 3; i++) {
1943 for (int j = -1; j < 3; j++) {
1944 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1945 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1951 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
1953 * @param s filter private context
1954 * @param i horizontal position on frame [0, width)
1955 * @param j vertical position on frame [0, height)
1956 * @param width frame width
1957 * @param height frame height
1958 * @param vec coordinates on sphere
1960 static void sinusoidal_to_xyz(const V360Context *s,
1961 int i, int j, int width, int height,
1964 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1965 const float phi = ((2.f * i) / width - 1.f) * M_PI / cosf(theta);
1967 const float sin_phi = sinf(phi);
1968 const float cos_phi = cosf(phi);
1969 const float sin_theta = sinf(theta);
1970 const float cos_theta = cosf(theta);
1972 vec[0] = cos_theta * sin_phi;
1973 vec[1] = -sin_theta;
1974 vec[2] = -cos_theta * cos_phi;
1976 normalize_vector(vec);
1980 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
1982 * @param s filter private context
1983 * @param vec coordinates on sphere
1984 * @param width frame width
1985 * @param height frame height
1986 * @param us horizontal coordinates for interpolation window
1987 * @param vs vertical coordinates for interpolation window
1988 * @param du horizontal relative coordinate
1989 * @param dv vertical relative coordinate
1991 static void xyz_to_sinusoidal(const V360Context *s,
1992 const float *vec, int width, int height,
1993 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1995 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1996 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
2000 uf = (phi / M_PI + 1.f) * width / 2.f;
2001 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2008 for (int i = -1; i < 3; i++) {
2009 for (int j = -1; j < 3; j++) {
2010 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2011 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2017 * Prepare data for processing equi-angular cubemap input format.
2019 * @param ctx filter context
2021 * @return error code
2023 static int prepare_eac_in(AVFilterContext *ctx)
2025 V360Context *s = ctx->priv;
2027 if (s->ih_flip && s->iv_flip) {
2028 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2029 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2030 s->in_cubemap_face_order[UP] = TOP_LEFT;
2031 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2032 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2033 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2034 } else if (s->ih_flip) {
2035 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2036 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2037 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2038 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2039 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2040 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2041 } else if (s->iv_flip) {
2042 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2043 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2044 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2045 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2046 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2047 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2049 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2050 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2051 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2052 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2053 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2054 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2058 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2059 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2060 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2061 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2062 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2063 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2065 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2066 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2067 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2068 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2069 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2070 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2077 * Prepare data for processing equi-angular cubemap output format.
2079 * @param ctx filter context
2081 * @return error code
2083 static int prepare_eac_out(AVFilterContext *ctx)
2085 V360Context *s = ctx->priv;
2087 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2088 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2089 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2090 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2091 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2092 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2094 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2095 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2096 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2097 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2098 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2099 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2105 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2107 * @param s filter private context
2108 * @param i horizontal position on frame [0, width)
2109 * @param j vertical position on frame [0, height)
2110 * @param width frame width
2111 * @param height frame height
2112 * @param vec coordinates on sphere
2114 static void eac_to_xyz(const V360Context *s,
2115 int i, int j, int width, int height,
2118 const float pixel_pad = 2;
2119 const float u_pad = pixel_pad / width;
2120 const float v_pad = pixel_pad / height;
2122 int u_face, v_face, face;
2124 float l_x, l_y, l_z;
2126 float uf = (i + 0.5f) / width;
2127 float vf = (j + 0.5f) / height;
2129 // EAC has 2-pixel padding on faces except between faces on the same row
2130 // Padding pixels seems not to be stretched with tangent as regular pixels
2131 // Formulas below approximate original padding as close as I could get experimentally
2133 // Horizontal padding
2134 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2138 } else if (uf >= 3.f) {
2142 u_face = floorf(uf);
2143 uf = fmodf(uf, 1.f) - 0.5f;
2147 v_face = floorf(vf * 2.f);
2148 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2150 if (uf >= -0.5f && uf < 0.5f) {
2151 uf = tanf(M_PI_2 * uf);
2155 if (vf >= -0.5f && vf < 0.5f) {
2156 vf = tanf(M_PI_2 * vf);
2161 face = u_face + 3 * v_face;
2202 normalize_vector(vec);
2206 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2208 * @param s filter private context
2209 * @param vec coordinates on sphere
2210 * @param width frame width
2211 * @param height frame height
2212 * @param us horizontal coordinates for interpolation window
2213 * @param vs vertical coordinates for interpolation window
2214 * @param du horizontal relative coordinate
2215 * @param dv vertical relative coordinate
2217 static void xyz_to_eac(const V360Context *s,
2218 const float *vec, int width, int height,
2219 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2221 const float pixel_pad = 2;
2222 const float u_pad = pixel_pad / width;
2223 const float v_pad = pixel_pad / height;
2227 int direction, face;
2230 xyz_to_cube(s, vec, &uf, &vf, &direction);
2232 face = s->in_cubemap_face_order[direction];
2236 uf = M_2_PI * atanf(uf) + 0.5f;
2237 vf = M_2_PI * atanf(vf) + 0.5f;
2239 // These formulas are inversed from eac_to_xyz ones
2240 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2241 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2255 for (int i = -1; i < 3; i++) {
2256 for (int j = -1; j < 3; j++) {
2257 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2258 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2264 * Prepare data for processing flat output format.
2266 * @param ctx filter context
2268 * @return error code
2270 static int prepare_flat_out(AVFilterContext *ctx)
2272 V360Context *s = ctx->priv;
2274 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2275 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2281 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2283 * @param s filter private context
2284 * @param i horizontal position on frame [0, width)
2285 * @param j vertical position on frame [0, height)
2286 * @param width frame width
2287 * @param height frame height
2288 * @param vec coordinates on sphere
2290 static void flat_to_xyz(const V360Context *s,
2291 int i, int j, int width, int height,
2294 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2295 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2301 normalize_vector(vec);
2305 * Prepare data for processing fisheye output format.
2307 * @param ctx filter context
2309 * @return error code
2311 static int prepare_fisheye_out(AVFilterContext *ctx)
2313 V360Context *s = ctx->priv;
2315 s->flat_range[0] = s->h_fov / 180.f;
2316 s->flat_range[1] = s->v_fov / 180.f;
2322 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2324 * @param s filter private context
2325 * @param i horizontal position on frame [0, width)
2326 * @param j vertical position on frame [0, height)
2327 * @param width frame width
2328 * @param height frame height
2329 * @param vec coordinates on sphere
2331 static void fisheye_to_xyz(const V360Context *s,
2332 int i, int j, int width, int height,
2335 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2336 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2338 const float phi = -atan2f(vf, uf);
2339 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2341 vec[0] = cosf(theta) * cosf(phi);
2342 vec[1] = cosf(theta) * sinf(phi);
2343 vec[2] = sinf(theta);
2345 normalize_vector(vec);
2349 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2351 * @param s filter private context
2352 * @param i horizontal position on frame [0, width)
2353 * @param j vertical position on frame [0, height)
2354 * @param width frame width
2355 * @param height frame height
2356 * @param vec coordinates on sphere
2358 static void pannini_to_xyz(const V360Context *s,
2359 int i, int j, int width, int height,
2362 const float uf = ((2.f * i) / width - 1.f);
2363 const float vf = ((2.f * j) / height - 1.f);
2365 const float d = s->h_fov;
2366 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2367 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2368 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2369 const float S = (d + 1.f) / (d + clon);
2370 const float lon = -(M_PI + atan2f(uf, S * clon));
2371 const float lat = -atan2f(vf, S);
2373 vec[0] = sinf(lon) * cosf(lat);
2375 vec[2] = cosf(lon) * cosf(lat);
2377 normalize_vector(vec);
2381 * Prepare data for processing cylindrical output format.
2383 * @param ctx filter context
2385 * @return error code
2387 static int prepare_cylindrical_out(AVFilterContext *ctx)
2389 V360Context *s = ctx->priv;
2391 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2392 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2398 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2400 * @param s filter private context
2401 * @param i horizontal position on frame [0, width)
2402 * @param j vertical position on frame [0, height)
2403 * @param width frame width
2404 * @param height frame height
2405 * @param vec coordinates on sphere
2407 static void cylindrical_to_xyz(const V360Context *s,
2408 int i, int j, int width, int height,
2411 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2412 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2414 const float phi = uf;
2415 const float theta = atanf(vf);
2417 const float sin_phi = sinf(phi);
2418 const float cos_phi = cosf(phi);
2419 const float sin_theta = sinf(theta);
2420 const float cos_theta = cosf(theta);
2422 vec[0] = cos_theta * sin_phi;
2423 vec[1] = -sin_theta;
2424 vec[2] = -cos_theta * cos_phi;
2426 normalize_vector(vec);
2430 * Prepare data for processing cylindrical input format.
2432 * @param ctx filter context
2434 * @return error code
2436 static int prepare_cylindrical_in(AVFilterContext *ctx)
2438 V360Context *s = ctx->priv;
2440 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
2441 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2447 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
2449 * @param s filter private context
2450 * @param vec coordinates on sphere
2451 * @param width frame width
2452 * @param height frame height
2453 * @param us horizontal coordinates for interpolation window
2454 * @param vs vertical coordinates for interpolation window
2455 * @param du horizontal relative coordinate
2456 * @param dv vertical relative coordinate
2458 static void xyz_to_cylindrical(const V360Context *s,
2459 const float *vec, int width, int height,
2460 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2462 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
2463 const float theta = atan2f(-vec[1], hypotf(vec[0], vec[2])) * s->input_mirror_modifier[1] / s->iflat_range[1];
2464 int visible, ui, vi;
2467 uf = (phi + 1.f) * (width - 1) / 2.f;
2468 vf = (tanf(theta) + 1.f) * height / 2.f;
2472 visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
2473 theta <= M_PI * s->iv_fov / 180.f &&
2474 theta >= -M_PI * s->iv_fov / 180.f;
2479 for (int i = -1; i < 3; i++) {
2480 for (int j = -1; j < 3; j++) {
2481 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
2482 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2488 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
2490 * @param s filter private context
2491 * @param i horizontal position on frame [0, width)
2492 * @param j vertical position on frame [0, height)
2493 * @param width frame width
2494 * @param height frame height
2495 * @param vec coordinates on sphere
2497 static void perspective_to_xyz(const V360Context *s,
2498 int i, int j, int width, int height,
2501 const float uf = ((2.f * i) / width - 1.f);
2502 const float vf = ((2.f * j) / height - 1.f);
2503 const float rh = hypotf(uf, vf);
2504 const float sinzz = 1.f - rh * rh;
2505 const float h = 1.f + s->v_fov;
2506 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
2507 const float sinz2 = sinz * sinz;
2510 const float cosz = sqrtf(1.f - sinz2);
2512 const float theta = asinf(cosz);
2513 const float phi = atan2f(uf, vf);
2515 const float sin_phi = sinf(phi);
2516 const float cos_phi = cosf(phi);
2517 const float sin_theta = sinf(theta);
2518 const float cos_theta = cosf(theta);
2520 vec[0] = cos_theta * sin_phi;
2522 vec[2] = -cos_theta * cos_phi;
2529 normalize_vector(vec);
2533 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2535 * @param s filter private context
2536 * @param i horizontal position on frame [0, width)
2537 * @param j vertical position on frame [0, height)
2538 * @param width frame width
2539 * @param height frame height
2540 * @param vec coordinates on sphere
2542 static void dfisheye_to_xyz(const V360Context *s,
2543 int i, int j, int width, int height,
2546 const float scale = 1.f + s->out_pad;
2548 const float ew = width / 2.f;
2549 const float eh = height;
2551 const int ei = i >= ew ? i - ew : i;
2552 const float m = i >= ew ? -1.f : 1.f;
2554 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2555 const float vf = ((2.f * j) / eh - 1.f) * scale;
2557 const float h = hypotf(uf, vf);
2558 const float lh = h > 0.f ? h : 1.f;
2559 const float theta = m * M_PI_2 * (1.f - h);
2561 const float sin_theta = sinf(theta);
2562 const float cos_theta = cosf(theta);
2564 vec[0] = cos_theta * m * -uf / lh;
2565 vec[1] = cos_theta * -vf / lh;
2568 normalize_vector(vec);
2572 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2574 * @param s filter private context
2575 * @param vec coordinates on sphere
2576 * @param width frame width
2577 * @param height frame height
2578 * @param us horizontal coordinates for interpolation window
2579 * @param vs vertical coordinates for interpolation window
2580 * @param du horizontal relative coordinate
2581 * @param dv vertical relative coordinate
2583 static void xyz_to_dfisheye(const V360Context *s,
2584 const float *vec, int width, int height,
2585 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2587 const float scale = 1.f - s->in_pad;
2589 const float ew = width / 2.f;
2590 const float eh = height;
2592 const float h = hypotf(vec[0], vec[1]);
2593 const float lh = h > 0.f ? h : 1.f;
2594 const float theta = acosf(fabsf(vec[2])) / M_PI;
2596 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2597 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2602 if (vec[2] >= 0.f) {
2605 u_shift = ceilf(ew);
2615 for (int i = -1; i < 3; i++) {
2616 for (int j = -1; j < 3; j++) {
2617 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2618 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2624 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2626 * @param s filter private context
2627 * @param i horizontal position on frame [0, width)
2628 * @param j vertical position on frame [0, height)
2629 * @param width frame width
2630 * @param height frame height
2631 * @param vec coordinates on sphere
2633 static void barrel_to_xyz(const V360Context *s,
2634 int i, int j, int width, int height,
2637 const float scale = 0.99f;
2638 float l_x, l_y, l_z;
2640 if (i < 4 * width / 5) {
2641 const float theta_range = M_PI_4;
2643 const int ew = 4 * width / 5;
2644 const int eh = height;
2646 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2647 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2649 const float sin_phi = sinf(phi);
2650 const float cos_phi = cosf(phi);
2651 const float sin_theta = sinf(theta);
2652 const float cos_theta = cosf(theta);
2654 l_x = cos_theta * sin_phi;
2656 l_z = -cos_theta * cos_phi;
2658 const int ew = width / 5;
2659 const int eh = height / 2;
2664 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2665 vf = 2.f * (j ) / eh - 1.f;
2674 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2675 vf = 2.f * (j - eh) / eh - 1.f;
2690 normalize_vector(vec);
2694 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2696 * @param s filter private context
2697 * @param vec coordinates on sphere
2698 * @param width frame width
2699 * @param height frame height
2700 * @param us horizontal coordinates for interpolation window
2701 * @param vs vertical coordinates for interpolation window
2702 * @param du horizontal relative coordinate
2703 * @param dv vertical relative coordinate
2705 static void xyz_to_barrel(const V360Context *s,
2706 const float *vec, int width, int height,
2707 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2709 const float scale = 0.99f;
2711 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2712 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2713 const float theta_range = M_PI_4;
2716 int u_shift, v_shift;
2720 if (theta > -theta_range && theta < theta_range) {
2724 u_shift = s->ih_flip ? width / 5 : 0;
2727 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2728 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2733 u_shift = s->ih_flip ? 0 : 4 * ew;
2735 if (theta < 0.f) { // UP
2736 uf = vec[0] / vec[1];
2737 vf = -vec[2] / vec[1];
2740 uf = -vec[0] / vec[1];
2741 vf = -vec[2] / vec[1];
2745 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2746 vf *= s->input_mirror_modifier[1];
2748 uf = 0.5f * ew * (uf * scale + 1.f);
2749 vf = 0.5f * eh * (vf * scale + 1.f);
2758 for (int i = -1; i < 3; i++) {
2759 for (int j = -1; j < 3; j++) {
2760 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2761 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2766 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2768 for (int i = 0; i < 3; i++) {
2769 for (int j = 0; j < 3; j++) {
2772 for (int k = 0; k < 3; k++)
2773 sum += a[i][k] * b[k][j];
2781 * Calculate rotation matrix for yaw/pitch/roll angles.
2783 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2784 float rot_mat[3][3],
2785 const int rotation_order[3])
2787 const float yaw_rad = yaw * M_PI / 180.f;
2788 const float pitch_rad = pitch * M_PI / 180.f;
2789 const float roll_rad = roll * M_PI / 180.f;
2791 const float sin_yaw = sinf(-yaw_rad);
2792 const float cos_yaw = cosf(-yaw_rad);
2793 const float sin_pitch = sinf(pitch_rad);
2794 const float cos_pitch = cosf(pitch_rad);
2795 const float sin_roll = sinf(roll_rad);
2796 const float cos_roll = cosf(roll_rad);
2801 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2802 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2803 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2805 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2806 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2807 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2809 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2810 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2811 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2813 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2814 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2818 * Rotate vector with given rotation matrix.
2820 * @param rot_mat rotation matrix
2823 static inline void rotate(const float rot_mat[3][3],
2826 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2827 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2828 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2835 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2838 modifier[0] = h_flip ? -1.f : 1.f;
2839 modifier[1] = v_flip ? -1.f : 1.f;
2840 modifier[2] = d_flip ? -1.f : 1.f;
2843 static inline void mirror(const float *modifier, float *vec)
2845 vec[0] *= modifier[0];
2846 vec[1] *= modifier[1];
2847 vec[2] *= modifier[2];
2850 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2852 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2853 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2854 if (!s->u[p] || !s->v[p])
2855 return AVERROR(ENOMEM);
2857 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2859 return AVERROR(ENOMEM);
2865 static void fov_from_dfov(float d_fov, float w, float h, float *h_fov, float *v_fov)
2867 const float da = tanf(0.5 * FFMIN(d_fov, 359.f) * M_PI / 180.f);
2868 const float d = hypotf(w, h);
2870 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
2871 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
2879 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2881 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2882 outw[0] = outw[3] = w;
2883 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2884 outh[0] = outh[3] = h;
2887 // Calculate remap data
2888 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2890 V360Context *s = ctx->priv;
2892 for (int p = 0; p < s->nb_allocated; p++) {
2893 const int width = s->pr_width[p];
2894 const int uv_linesize = s->uv_linesize[p];
2895 const int height = s->pr_height[p];
2896 const int in_width = s->inplanewidth[p];
2897 const int in_height = s->inplaneheight[p];
2898 const int slice_start = (height * jobnr ) / nb_jobs;
2899 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
2904 for (int j = slice_start; j < slice_end; j++) {
2905 for (int i = 0; i < width; i++) {
2906 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2907 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2908 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2910 if (s->out_transpose)
2911 s->out_transform(s, j, i, height, width, vec);
2913 s->out_transform(s, i, j, width, height, vec);
2914 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2915 rotate(s->rot_mat, vec);
2916 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2917 normalize_vector(vec);
2918 mirror(s->output_mirror_modifier, vec);
2919 if (s->in_transpose)
2920 s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2922 s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2923 av_assert1(!isnan(du) && !isnan(dv));
2924 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2932 static int config_output(AVFilterLink *outlink)
2934 AVFilterContext *ctx = outlink->src;
2935 AVFilterLink *inlink = ctx->inputs[0];
2936 V360Context *s = ctx->priv;
2937 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2938 const int depth = desc->comp[0].depth;
2943 int in_offset_h, in_offset_w;
2944 int out_offset_h, out_offset_w;
2946 int (*prepare_out)(AVFilterContext *ctx);
2948 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2949 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2951 switch (s->interp) {
2953 s->calculate_kernel = nearest_kernel;
2954 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2956 sizeof_uv = sizeof(int16_t) * s->elements;
2960 s->calculate_kernel = bilinear_kernel;
2961 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2962 s->elements = 2 * 2;
2963 sizeof_uv = sizeof(int16_t) * s->elements;
2964 sizeof_ker = sizeof(int16_t) * s->elements;
2967 s->calculate_kernel = bicubic_kernel;
2968 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2969 s->elements = 4 * 4;
2970 sizeof_uv = sizeof(int16_t) * s->elements;
2971 sizeof_ker = sizeof(int16_t) * s->elements;
2974 s->calculate_kernel = lanczos_kernel;
2975 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2976 s->elements = 4 * 4;
2977 sizeof_uv = sizeof(int16_t) * s->elements;
2978 sizeof_ker = sizeof(int16_t) * s->elements;
2981 s->calculate_kernel = spline16_kernel;
2982 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2983 s->elements = 4 * 4;
2984 sizeof_uv = sizeof(int16_t) * s->elements;
2985 sizeof_ker = sizeof(int16_t) * s->elements;
2988 s->calculate_kernel = gaussian_kernel;
2989 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2990 s->elements = 4 * 4;
2991 sizeof_uv = sizeof(int16_t) * s->elements;
2992 sizeof_ker = sizeof(int16_t) * s->elements;
2998 ff_v360_init(s, depth);
3000 for (int order = 0; order < NB_RORDERS; order++) {
3001 const char c = s->rorder[order];
3005 av_log(ctx, AV_LOG_ERROR,
3006 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
3007 return AVERROR(EINVAL);
3010 rorder = get_rorder(c);
3012 av_log(ctx, AV_LOG_ERROR,
3013 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
3014 return AVERROR(EINVAL);
3017 s->rotation_order[order] = rorder;
3020 switch (s->in_stereo) {
3024 in_offset_w = in_offset_h = 0;
3042 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
3043 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
3045 s->in_width = s->inplanewidth[0];
3046 s->in_height = s->inplaneheight[0];
3048 if (s->id_fov > 0.f)
3049 fov_from_dfov(s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
3051 if (s->in_transpose)
3052 FFSWAP(int, s->in_width, s->in_height);
3055 case EQUIRECTANGULAR:
3056 s->in_transform = xyz_to_equirect;
3062 s->in_transform = xyz_to_cube3x2;
3063 err = prepare_cube_in(ctx);
3068 s->in_transform = xyz_to_cube1x6;
3069 err = prepare_cube_in(ctx);
3074 s->in_transform = xyz_to_cube6x1;
3075 err = prepare_cube_in(ctx);
3080 s->in_transform = xyz_to_eac;
3081 err = prepare_eac_in(ctx);
3086 s->in_transform = xyz_to_flat;
3087 err = prepare_flat_in(ctx);
3094 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
3095 return AVERROR(EINVAL);
3097 s->in_transform = xyz_to_dfisheye;
3103 s->in_transform = xyz_to_barrel;
3109 s->in_transform = xyz_to_stereographic;
3110 err = prepare_stereographic_in(ctx);
3115 s->in_transform = xyz_to_mercator;
3121 s->in_transform = xyz_to_ball;
3127 s->in_transform = xyz_to_hammer;
3133 s->in_transform = xyz_to_sinusoidal;
3139 s->in_transform = xyz_to_cylindrical;
3140 err = prepare_cylindrical_in(ctx);
3145 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
3154 case EQUIRECTANGULAR:
3155 s->out_transform = equirect_to_xyz;
3161 s->out_transform = cube3x2_to_xyz;
3162 prepare_out = prepare_cube_out;
3163 w = lrintf(wf / 4.f * 3.f);
3167 s->out_transform = cube1x6_to_xyz;
3168 prepare_out = prepare_cube_out;
3169 w = lrintf(wf / 4.f);
3170 h = lrintf(hf * 3.f);
3173 s->out_transform = cube6x1_to_xyz;
3174 prepare_out = prepare_cube_out;
3175 w = lrintf(wf / 2.f * 3.f);
3176 h = lrintf(hf / 2.f);
3179 s->out_transform = eac_to_xyz;
3180 prepare_out = prepare_eac_out;
3182 h = lrintf(hf / 8.f * 9.f);
3185 s->out_transform = flat_to_xyz;
3186 prepare_out = prepare_flat_out;
3191 s->out_transform = dfisheye_to_xyz;
3197 s->out_transform = barrel_to_xyz;
3199 w = lrintf(wf / 4.f * 5.f);
3203 s->out_transform = stereographic_to_xyz;
3204 prepare_out = prepare_stereographic_out;
3206 h = lrintf(hf * 2.f);
3209 s->out_transform = mercator_to_xyz;
3212 h = lrintf(hf * 2.f);
3215 s->out_transform = ball_to_xyz;
3218 h = lrintf(hf * 2.f);
3221 s->out_transform = hammer_to_xyz;
3227 s->out_transform = sinusoidal_to_xyz;
3233 s->out_transform = fisheye_to_xyz;
3234 prepare_out = prepare_fisheye_out;
3235 w = lrintf(wf * 0.5f);
3239 s->out_transform = pannini_to_xyz;
3245 s->out_transform = cylindrical_to_xyz;
3246 prepare_out = prepare_cylindrical_out;
3248 h = lrintf(hf * 0.5f);
3251 s->out_transform = perspective_to_xyz;
3253 w = lrintf(wf / 2.f);
3257 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
3261 // Override resolution with user values if specified
3262 if (s->width > 0 && s->height > 0) {
3265 } else if (s->width > 0 || s->height > 0) {
3266 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
3267 return AVERROR(EINVAL);
3269 if (s->out_transpose)
3272 if (s->in_transpose)
3277 fov_from_dfov(s->d_fov, w, h, &s->h_fov, &s->v_fov);
3280 err = prepare_out(ctx);
3285 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
3287 s->out_width = s->pr_width[0];
3288 s->out_height = s->pr_height[0];
3290 if (s->out_transpose)
3291 FFSWAP(int, s->out_width, s->out_height);
3293 switch (s->out_stereo) {
3295 out_offset_w = out_offset_h = 0;
3311 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
3312 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
3314 for (int i = 0; i < 4; i++)
3315 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
3320 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
3322 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
3323 s->nb_allocated = 1;
3324 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
3326 s->nb_allocated = 2;
3327 s->map[0] = s->map[3] = 0;
3328 s->map[1] = s->map[2] = 1;
3331 for (int i = 0; i < s->nb_allocated; i++)
3332 allocate_plane(s, sizeof_uv, sizeof_ker, i);
3334 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
3335 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
3337 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3342 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3344 AVFilterContext *ctx = inlink->dst;
3345 AVFilterLink *outlink = ctx->outputs[0];
3346 V360Context *s = ctx->priv;
3350 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3353 return AVERROR(ENOMEM);
3355 av_frame_copy_props(out, in);
3360 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3363 return ff_filter_frame(outlink, out);
3366 static av_cold void uninit(AVFilterContext *ctx)
3368 V360Context *s = ctx->priv;
3370 for (int p = 0; p < s->nb_allocated; p++) {
3373 av_freep(&s->ker[p]);
3377 static const AVFilterPad inputs[] = {
3380 .type = AVMEDIA_TYPE_VIDEO,
3381 .filter_frame = filter_frame,
3386 static const AVFilterPad outputs[] = {
3389 .type = AVMEDIA_TYPE_VIDEO,
3390 .config_props = config_output,
3395 AVFilter ff_vf_v360 = {
3397 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3398 .priv_size = sizeof(V360Context),
3400 .query_formats = query_formats,
3403 .priv_class = &v360_class,
3404 .flags = AVFILTER_FLAG_SLICE_THREADS,