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 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1587 * @param s filter private context
1588 * @param vec coordinates on sphere
1589 * @param width frame width
1590 * @param height frame height
1591 * @param us horizontal coordinates for interpolation window
1592 * @param vs vertical coordinates for interpolation window
1593 * @param du horizontal relative coordinate
1594 * @param dv vertical relative coordinate
1596 static void xyz_to_stereographic(const V360Context *s,
1597 const float *vec, int width, int height,
1598 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1600 const float x = av_clipf(vec[0] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[0];
1601 const float y = av_clipf(vec[2] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[1];
1605 uf = (x + 1.f) * width / 2.f;
1606 vf = (y + 1.f) * height / 2.f;
1613 for (int i = -1; i < 3; i++) {
1614 for (int j = -1; j < 3; j++) {
1615 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1616 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1622 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1624 * @param s filter private context
1625 * @param vec coordinates on sphere
1626 * @param width frame width
1627 * @param height frame height
1628 * @param us horizontal coordinates for interpolation window
1629 * @param vs vertical coordinates for interpolation window
1630 * @param du horizontal relative coordinate
1631 * @param dv vertical relative coordinate
1633 static void xyz_to_equirect(const V360Context *s,
1634 const float *vec, int width, int height,
1635 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1637 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1638 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1642 uf = (phi / M_PI + 1.f) * width / 2.f;
1643 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1650 for (int i = -1; i < 3; i++) {
1651 for (int j = -1; j < 3; j++) {
1652 us[i + 1][j + 1] = mod(ui + j, width);
1653 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1659 * Prepare data for processing flat input format.
1661 * @param ctx filter context
1663 * @return error code
1665 static int prepare_flat_in(AVFilterContext *ctx)
1667 V360Context *s = ctx->priv;
1669 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
1670 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
1676 * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
1678 * @param s filter private context
1679 * @param vec coordinates on sphere
1680 * @param width frame width
1681 * @param height frame height
1682 * @param us horizontal coordinates for interpolation window
1683 * @param vs vertical coordinates for interpolation window
1684 * @param du horizontal relative coordinate
1685 * @param dv vertical relative coordinate
1687 static void xyz_to_flat(const V360Context *s,
1688 const float *vec, int width, int height,
1689 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1691 const float theta = acosf(vec[2]);
1692 const float r = tanf(theta);
1693 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
1694 const float zf = -vec[2];
1695 const float h = hypotf(vec[0], vec[1]);
1696 const float c = h <= 1e-6f ? 1.f : rr / h;
1697 float uf = -vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1698 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1699 int visible, ui, vi;
1701 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
1702 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
1707 visible = vi >= 0 && vi < height && ui >= 0 && ui < width;
1712 for (int i = -1; i < 3; i++) {
1713 for (int j = -1; j < 3; j++) {
1714 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1715 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1721 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1723 * @param s filter private context
1724 * @param vec coordinates on sphere
1725 * @param width frame width
1726 * @param height frame height
1727 * @param us horizontal coordinates for interpolation window
1728 * @param vs vertical coordinates for interpolation window
1729 * @param du horizontal relative coordinate
1730 * @param dv vertical relative coordinate
1732 static void xyz_to_mercator(const V360Context *s,
1733 const float *vec, int width, int height,
1734 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1736 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1737 const float theta = -vec[1] * s->input_mirror_modifier[1];
1741 uf = (phi / M_PI + 1.f) * width / 2.f;
1742 vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1749 for (int i = -1; i < 3; i++) {
1750 for (int j = -1; j < 3; j++) {
1751 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1752 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1758 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1760 * @param s filter private context
1761 * @param i horizontal position on frame [0, width)
1762 * @param j vertical position on frame [0, height)
1763 * @param width frame width
1764 * @param height frame height
1765 * @param vec coordinates on sphere
1767 static void mercator_to_xyz(const V360Context *s,
1768 int i, int j, int width, int height,
1771 const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1772 const float y = ((2.f * j) / height - 1.f) * M_PI;
1773 const float div = expf(2.f * y) + 1.f;
1775 const float sin_phi = sinf(phi);
1776 const float cos_phi = cosf(phi);
1777 const float sin_theta = -2.f * expf(y) / div;
1778 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1780 vec[0] = sin_theta * cos_phi;
1782 vec[2] = sin_theta * sin_phi;
1786 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1788 * @param s filter private context
1789 * @param vec coordinates on sphere
1790 * @param width frame width
1791 * @param height frame height
1792 * @param us horizontal coordinates for interpolation window
1793 * @param vs vertical coordinates for interpolation window
1794 * @param du horizontal relative coordinate
1795 * @param dv vertical relative coordinate
1797 static void xyz_to_ball(const V360Context *s,
1798 const float *vec, int width, int height,
1799 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1801 const float l = hypotf(vec[0], vec[1]);
1802 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1806 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1807 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1815 for (int i = -1; i < 3; i++) {
1816 for (int j = -1; j < 3; j++) {
1817 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1818 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1824 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1826 * @param s filter private context
1827 * @param i horizontal position on frame [0, width)
1828 * @param j vertical position on frame [0, height)
1829 * @param width frame width
1830 * @param height frame height
1831 * @param vec coordinates on sphere
1833 static void ball_to_xyz(const V360Context *s,
1834 int i, int j, int width, int height,
1837 const float x = (2.f * i) / width - 1.f;
1838 const float y = (2.f * j) / height - 1.f;
1839 const float l = hypotf(x, y);
1842 const float z = 2.f * l * sqrtf(1.f - l * l);
1844 vec[0] = z * x / (l > 0.f ? l : 1.f);
1845 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1846 vec[2] = -1.f + 2.f * l * l;
1855 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1857 * @param s filter private context
1858 * @param i horizontal position on frame [0, width)
1859 * @param j vertical position on frame [0, height)
1860 * @param width frame width
1861 * @param height frame height
1862 * @param vec coordinates on sphere
1864 static void hammer_to_xyz(const V360Context *s,
1865 int i, int j, int width, int height,
1868 const float x = ((2.f * i) / width - 1.f);
1869 const float y = ((2.f * j) / height - 1.f);
1871 const float xx = x * x;
1872 const float yy = y * y;
1874 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1876 const float a = M_SQRT2 * x * z;
1877 const float b = 2.f * z * z - 1.f;
1879 const float aa = a * a;
1880 const float bb = b * b;
1882 const float w = sqrtf(1.f - 2.f * yy * z * z);
1884 vec[0] = w * 2.f * a * b / (aa + bb);
1885 vec[1] = -M_SQRT2 * y * z;
1886 vec[2] = -w * (bb - aa) / (aa + bb);
1888 normalize_vector(vec);
1892 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1894 * @param s filter private context
1895 * @param vec coordinates on sphere
1896 * @param width frame width
1897 * @param height frame height
1898 * @param us horizontal coordinates for interpolation window
1899 * @param vs vertical coordinates for interpolation window
1900 * @param du horizontal relative coordinate
1901 * @param dv vertical relative coordinate
1903 static void xyz_to_hammer(const V360Context *s,
1904 const float *vec, int width, int height,
1905 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1907 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1909 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1910 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1911 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1915 uf = (x + 1.f) * width / 2.f;
1916 vf = (y + 1.f) * height / 2.f;
1923 for (int i = -1; i < 3; i++) {
1924 for (int j = -1; j < 3; j++) {
1925 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1926 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1932 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
1934 * @param s filter private context
1935 * @param i horizontal position on frame [0, width)
1936 * @param j vertical position on frame [0, height)
1937 * @param width frame width
1938 * @param height frame height
1939 * @param vec coordinates on sphere
1941 static void sinusoidal_to_xyz(const V360Context *s,
1942 int i, int j, int width, int height,
1945 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1946 const float phi = ((2.f * i) / width - 1.f) * M_PI / cosf(theta);
1948 const float sin_phi = sinf(phi);
1949 const float cos_phi = cosf(phi);
1950 const float sin_theta = sinf(theta);
1951 const float cos_theta = cosf(theta);
1953 vec[0] = cos_theta * sin_phi;
1954 vec[1] = -sin_theta;
1955 vec[2] = -cos_theta * cos_phi;
1957 normalize_vector(vec);
1961 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
1963 * @param s filter private context
1964 * @param vec coordinates on sphere
1965 * @param width frame width
1966 * @param height frame height
1967 * @param us horizontal coordinates for interpolation window
1968 * @param vs vertical coordinates for interpolation window
1969 * @param du horizontal relative coordinate
1970 * @param dv vertical relative coordinate
1972 static void xyz_to_sinusoidal(const V360Context *s,
1973 const float *vec, int width, int height,
1974 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1976 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1977 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
1981 uf = (phi / M_PI + 1.f) * width / 2.f;
1982 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1989 for (int i = -1; i < 3; i++) {
1990 for (int j = -1; j < 3; j++) {
1991 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1992 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1998 * Prepare data for processing equi-angular cubemap input format.
2000 * @param ctx filter context
2002 * @return error code
2004 static int prepare_eac_in(AVFilterContext *ctx)
2006 V360Context *s = ctx->priv;
2008 if (s->ih_flip && s->iv_flip) {
2009 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2010 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2011 s->in_cubemap_face_order[UP] = TOP_LEFT;
2012 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2013 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2014 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2015 } else if (s->ih_flip) {
2016 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2017 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2018 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2019 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2020 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2021 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2022 } else if (s->iv_flip) {
2023 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2024 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2025 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2026 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2027 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2028 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2030 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2031 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2032 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2033 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2034 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2035 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2039 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2040 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2041 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2042 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2043 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2044 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2046 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2047 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2048 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2049 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2050 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2051 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2058 * Prepare data for processing equi-angular cubemap output format.
2060 * @param ctx filter context
2062 * @return error code
2064 static int prepare_eac_out(AVFilterContext *ctx)
2066 V360Context *s = ctx->priv;
2068 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2069 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2070 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2071 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2072 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2073 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2075 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2076 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2077 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2078 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2079 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2080 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2086 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2088 * @param s filter private context
2089 * @param i horizontal position on frame [0, width)
2090 * @param j vertical position on frame [0, height)
2091 * @param width frame width
2092 * @param height frame height
2093 * @param vec coordinates on sphere
2095 static void eac_to_xyz(const V360Context *s,
2096 int i, int j, int width, int height,
2099 const float pixel_pad = 2;
2100 const float u_pad = pixel_pad / width;
2101 const float v_pad = pixel_pad / height;
2103 int u_face, v_face, face;
2105 float l_x, l_y, l_z;
2107 float uf = (i + 0.5f) / width;
2108 float vf = (j + 0.5f) / height;
2110 // EAC has 2-pixel padding on faces except between faces on the same row
2111 // Padding pixels seems not to be stretched with tangent as regular pixels
2112 // Formulas below approximate original padding as close as I could get experimentally
2114 // Horizontal padding
2115 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2119 } else if (uf >= 3.f) {
2123 u_face = floorf(uf);
2124 uf = fmodf(uf, 1.f) - 0.5f;
2128 v_face = floorf(vf * 2.f);
2129 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2131 if (uf >= -0.5f && uf < 0.5f) {
2132 uf = tanf(M_PI_2 * uf);
2136 if (vf >= -0.5f && vf < 0.5f) {
2137 vf = tanf(M_PI_2 * vf);
2142 face = u_face + 3 * v_face;
2183 normalize_vector(vec);
2187 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2189 * @param s filter private context
2190 * @param vec coordinates on sphere
2191 * @param width frame width
2192 * @param height frame height
2193 * @param us horizontal coordinates for interpolation window
2194 * @param vs vertical coordinates for interpolation window
2195 * @param du horizontal relative coordinate
2196 * @param dv vertical relative coordinate
2198 static void xyz_to_eac(const V360Context *s,
2199 const float *vec, int width, int height,
2200 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2202 const float pixel_pad = 2;
2203 const float u_pad = pixel_pad / width;
2204 const float v_pad = pixel_pad / height;
2208 int direction, face;
2211 xyz_to_cube(s, vec, &uf, &vf, &direction);
2213 face = s->in_cubemap_face_order[direction];
2217 uf = M_2_PI * atanf(uf) + 0.5f;
2218 vf = M_2_PI * atanf(vf) + 0.5f;
2220 // These formulas are inversed from eac_to_xyz ones
2221 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2222 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2236 for (int i = -1; i < 3; i++) {
2237 for (int j = -1; j < 3; j++) {
2238 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2239 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2245 * Prepare data for processing flat output format.
2247 * @param ctx filter context
2249 * @return error code
2251 static int prepare_flat_out(AVFilterContext *ctx)
2253 V360Context *s = ctx->priv;
2255 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2256 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2262 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2264 * @param s filter private context
2265 * @param i horizontal position on frame [0, width)
2266 * @param j vertical position on frame [0, height)
2267 * @param width frame width
2268 * @param height frame height
2269 * @param vec coordinates on sphere
2271 static void flat_to_xyz(const V360Context *s,
2272 int i, int j, int width, int height,
2275 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2276 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2282 normalize_vector(vec);
2286 * Prepare data for processing fisheye output format.
2288 * @param ctx filter context
2290 * @return error code
2292 static int prepare_fisheye_out(AVFilterContext *ctx)
2294 V360Context *s = ctx->priv;
2296 s->flat_range[0] = s->h_fov / 180.f;
2297 s->flat_range[1] = s->v_fov / 180.f;
2303 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2305 * @param s filter private context
2306 * @param i horizontal position on frame [0, width)
2307 * @param j vertical position on frame [0, height)
2308 * @param width frame width
2309 * @param height frame height
2310 * @param vec coordinates on sphere
2312 static void fisheye_to_xyz(const V360Context *s,
2313 int i, int j, int width, int height,
2316 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2317 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2319 const float phi = -atan2f(vf, uf);
2320 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2322 vec[0] = cosf(theta) * cosf(phi);
2323 vec[1] = cosf(theta) * sinf(phi);
2324 vec[2] = sinf(theta);
2326 normalize_vector(vec);
2330 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2332 * @param s filter private context
2333 * @param i horizontal position on frame [0, width)
2334 * @param j vertical position on frame [0, height)
2335 * @param width frame width
2336 * @param height frame height
2337 * @param vec coordinates on sphere
2339 static void pannini_to_xyz(const V360Context *s,
2340 int i, int j, int width, int height,
2343 const float uf = ((2.f * i) / width - 1.f);
2344 const float vf = ((2.f * j) / height - 1.f);
2346 const float d = s->h_fov;
2347 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2348 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2349 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2350 const float S = (d + 1.f) / (d + clon);
2351 const float lon = -(M_PI + atan2f(uf, S * clon));
2352 const float lat = -atan2f(vf, S);
2354 vec[0] = sinf(lon) * cosf(lat);
2356 vec[2] = cosf(lon) * cosf(lat);
2358 normalize_vector(vec);
2362 * Prepare data for processing cylindrical output format.
2364 * @param ctx filter context
2366 * @return error code
2368 static int prepare_cylindrical_out(AVFilterContext *ctx)
2370 V360Context *s = ctx->priv;
2372 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2373 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2379 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2381 * @param s filter private context
2382 * @param i horizontal position on frame [0, width)
2383 * @param j vertical position on frame [0, height)
2384 * @param width frame width
2385 * @param height frame height
2386 * @param vec coordinates on sphere
2388 static void cylindrical_to_xyz(const V360Context *s,
2389 int i, int j, int width, int height,
2392 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2393 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2395 const float phi = uf;
2396 const float theta = atanf(vf);
2398 const float sin_phi = sinf(phi);
2399 const float cos_phi = cosf(phi);
2400 const float sin_theta = sinf(theta);
2401 const float cos_theta = cosf(theta);
2403 vec[0] = cos_theta * sin_phi;
2404 vec[1] = -sin_theta;
2405 vec[2] = -cos_theta * cos_phi;
2407 normalize_vector(vec);
2411 * Prepare data for processing cylindrical input format.
2413 * @param ctx filter context
2415 * @return error code
2417 static int prepare_cylindrical_in(AVFilterContext *ctx)
2419 V360Context *s = ctx->priv;
2421 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
2422 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2428 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
2430 * @param s filter private context
2431 * @param vec coordinates on sphere
2432 * @param width frame width
2433 * @param height frame height
2434 * @param us horizontal coordinates for interpolation window
2435 * @param vs vertical coordinates for interpolation window
2436 * @param du horizontal relative coordinate
2437 * @param dv vertical relative coordinate
2439 static void xyz_to_cylindrical(const V360Context *s,
2440 const float *vec, int width, int height,
2441 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2443 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
2444 const float theta = atan2f(-vec[1], hypotf(vec[0], vec[2])) * s->input_mirror_modifier[1] / s->iflat_range[1];
2445 int visible, ui, vi;
2448 uf = (phi + 1.f) * (width - 1) / 2.f;
2449 vf = (tanf(theta) + 1.f) * height / 2.f;
2453 visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
2454 theta <= M_PI * s->iv_fov / 180.f &&
2455 theta >= -M_PI * s->iv_fov / 180.f;
2460 for (int i = -1; i < 3; i++) {
2461 for (int j = -1; j < 3; j++) {
2462 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
2463 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2469 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
2471 * @param s filter private context
2472 * @param i horizontal position on frame [0, width)
2473 * @param j vertical position on frame [0, height)
2474 * @param width frame width
2475 * @param height frame height
2476 * @param vec coordinates on sphere
2478 static void perspective_to_xyz(const V360Context *s,
2479 int i, int j, int width, int height,
2482 const float uf = ((2.f * i) / width - 1.f);
2483 const float vf = ((2.f * j) / height - 1.f);
2484 const float rh = hypotf(uf, vf);
2485 const float sinzz = 1.f - rh * rh;
2486 const float h = 1.f + s->v_fov;
2487 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
2488 const float sinz2 = sinz * sinz;
2491 const float cosz = sqrtf(1.f - sinz2);
2493 const float theta = asinf(cosz);
2494 const float phi = atan2f(uf, vf);
2496 const float sin_phi = sinf(phi);
2497 const float cos_phi = cosf(phi);
2498 const float sin_theta = sinf(theta);
2499 const float cos_theta = cosf(theta);
2501 vec[0] = cos_theta * sin_phi;
2503 vec[2] = -cos_theta * cos_phi;
2510 normalize_vector(vec);
2514 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2516 * @param s filter private context
2517 * @param i horizontal position on frame [0, width)
2518 * @param j vertical position on frame [0, height)
2519 * @param width frame width
2520 * @param height frame height
2521 * @param vec coordinates on sphere
2523 static void dfisheye_to_xyz(const V360Context *s,
2524 int i, int j, int width, int height,
2527 const float scale = 1.f + s->out_pad;
2529 const float ew = width / 2.f;
2530 const float eh = height;
2532 const int ei = i >= ew ? i - ew : i;
2533 const float m = i >= ew ? -1.f : 1.f;
2535 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2536 const float vf = ((2.f * j) / eh - 1.f) * scale;
2538 const float h = hypotf(uf, vf);
2539 const float lh = h > 0.f ? h : 1.f;
2540 const float theta = m * M_PI_2 * (1.f - h);
2542 const float sin_theta = sinf(theta);
2543 const float cos_theta = cosf(theta);
2545 vec[0] = cos_theta * m * -uf / lh;
2546 vec[1] = cos_theta * -vf / lh;
2549 normalize_vector(vec);
2553 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2555 * @param s filter private context
2556 * @param vec coordinates on sphere
2557 * @param width frame width
2558 * @param height frame height
2559 * @param us horizontal coordinates for interpolation window
2560 * @param vs vertical coordinates for interpolation window
2561 * @param du horizontal relative coordinate
2562 * @param dv vertical relative coordinate
2564 static void xyz_to_dfisheye(const V360Context *s,
2565 const float *vec, int width, int height,
2566 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2568 const float scale = 1.f - s->in_pad;
2570 const float ew = width / 2.f;
2571 const float eh = height;
2573 const float h = hypotf(vec[0], vec[1]);
2574 const float lh = h > 0.f ? h : 1.f;
2575 const float theta = acosf(fabsf(vec[2])) / M_PI;
2577 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2578 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2583 if (vec[2] >= 0.f) {
2586 u_shift = ceilf(ew);
2596 for (int i = -1; i < 3; i++) {
2597 for (int j = -1; j < 3; j++) {
2598 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2599 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2605 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2607 * @param s filter private context
2608 * @param i horizontal position on frame [0, width)
2609 * @param j vertical position on frame [0, height)
2610 * @param width frame width
2611 * @param height frame height
2612 * @param vec coordinates on sphere
2614 static void barrel_to_xyz(const V360Context *s,
2615 int i, int j, int width, int height,
2618 const float scale = 0.99f;
2619 float l_x, l_y, l_z;
2621 if (i < 4 * width / 5) {
2622 const float theta_range = M_PI_4;
2624 const int ew = 4 * width / 5;
2625 const int eh = height;
2627 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2628 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2630 const float sin_phi = sinf(phi);
2631 const float cos_phi = cosf(phi);
2632 const float sin_theta = sinf(theta);
2633 const float cos_theta = cosf(theta);
2635 l_x = cos_theta * sin_phi;
2637 l_z = -cos_theta * cos_phi;
2639 const int ew = width / 5;
2640 const int eh = height / 2;
2645 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2646 vf = 2.f * (j ) / eh - 1.f;
2655 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2656 vf = 2.f * (j - eh) / eh - 1.f;
2671 normalize_vector(vec);
2675 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2677 * @param s filter private context
2678 * @param vec coordinates on sphere
2679 * @param width frame width
2680 * @param height frame height
2681 * @param us horizontal coordinates for interpolation window
2682 * @param vs vertical coordinates for interpolation window
2683 * @param du horizontal relative coordinate
2684 * @param dv vertical relative coordinate
2686 static void xyz_to_barrel(const V360Context *s,
2687 const float *vec, int width, int height,
2688 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2690 const float scale = 0.99f;
2692 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2693 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2694 const float theta_range = M_PI_4;
2697 int u_shift, v_shift;
2701 if (theta > -theta_range && theta < theta_range) {
2705 u_shift = s->ih_flip ? width / 5 : 0;
2708 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2709 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2714 u_shift = s->ih_flip ? 0 : 4 * ew;
2716 if (theta < 0.f) { // UP
2717 uf = vec[0] / vec[1];
2718 vf = -vec[2] / vec[1];
2721 uf = -vec[0] / vec[1];
2722 vf = -vec[2] / vec[1];
2726 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2727 vf *= s->input_mirror_modifier[1];
2729 uf = 0.5f * ew * (uf * scale + 1.f);
2730 vf = 0.5f * eh * (vf * scale + 1.f);
2739 for (int i = -1; i < 3; i++) {
2740 for (int j = -1; j < 3; j++) {
2741 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2742 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2747 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2749 for (int i = 0; i < 3; i++) {
2750 for (int j = 0; j < 3; j++) {
2753 for (int k = 0; k < 3; k++)
2754 sum += a[i][k] * b[k][j];
2762 * Calculate rotation matrix for yaw/pitch/roll angles.
2764 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2765 float rot_mat[3][3],
2766 const int rotation_order[3])
2768 const float yaw_rad = yaw * M_PI / 180.f;
2769 const float pitch_rad = pitch * M_PI / 180.f;
2770 const float roll_rad = roll * M_PI / 180.f;
2772 const float sin_yaw = sinf(-yaw_rad);
2773 const float cos_yaw = cosf(-yaw_rad);
2774 const float sin_pitch = sinf(pitch_rad);
2775 const float cos_pitch = cosf(pitch_rad);
2776 const float sin_roll = sinf(roll_rad);
2777 const float cos_roll = cosf(roll_rad);
2782 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2783 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2784 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2786 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2787 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2788 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2790 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2791 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2792 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2794 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2795 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2799 * Rotate vector with given rotation matrix.
2801 * @param rot_mat rotation matrix
2804 static inline void rotate(const float rot_mat[3][3],
2807 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2808 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2809 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2816 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2819 modifier[0] = h_flip ? -1.f : 1.f;
2820 modifier[1] = v_flip ? -1.f : 1.f;
2821 modifier[2] = d_flip ? -1.f : 1.f;
2824 static inline void mirror(const float *modifier, float *vec)
2826 vec[0] *= modifier[0];
2827 vec[1] *= modifier[1];
2828 vec[2] *= modifier[2];
2831 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2833 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2834 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2835 if (!s->u[p] || !s->v[p])
2836 return AVERROR(ENOMEM);
2838 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2840 return AVERROR(ENOMEM);
2846 static void fov_from_dfov(float d_fov, float w, float h, float *h_fov, float *v_fov)
2848 const float da = tanf(0.5 * FFMIN(d_fov, 359.f) * M_PI / 180.f);
2849 const float d = hypotf(w, h);
2851 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
2852 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
2860 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2862 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2863 outw[0] = outw[3] = w;
2864 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2865 outh[0] = outh[3] = h;
2868 // Calculate remap data
2869 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2871 V360Context *s = ctx->priv;
2873 for (int p = 0; p < s->nb_allocated; p++) {
2874 const int width = s->pr_width[p];
2875 const int uv_linesize = s->uv_linesize[p];
2876 const int height = s->pr_height[p];
2877 const int in_width = s->inplanewidth[p];
2878 const int in_height = s->inplaneheight[p];
2879 const int slice_start = (height * jobnr ) / nb_jobs;
2880 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
2885 for (int j = slice_start; j < slice_end; j++) {
2886 for (int i = 0; i < width; i++) {
2887 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2888 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2889 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2891 if (s->out_transpose)
2892 s->out_transform(s, j, i, height, width, vec);
2894 s->out_transform(s, i, j, width, height, vec);
2895 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2896 rotate(s->rot_mat, vec);
2897 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2898 normalize_vector(vec);
2899 mirror(s->output_mirror_modifier, vec);
2900 if (s->in_transpose)
2901 s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2903 s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2904 av_assert1(!isnan(du) && !isnan(dv));
2905 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2913 static int config_output(AVFilterLink *outlink)
2915 AVFilterContext *ctx = outlink->src;
2916 AVFilterLink *inlink = ctx->inputs[0];
2917 V360Context *s = ctx->priv;
2918 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2919 const int depth = desc->comp[0].depth;
2924 int in_offset_h, in_offset_w;
2925 int out_offset_h, out_offset_w;
2927 int (*prepare_out)(AVFilterContext *ctx);
2929 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2930 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2932 switch (s->interp) {
2934 s->calculate_kernel = nearest_kernel;
2935 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2937 sizeof_uv = sizeof(int16_t) * s->elements;
2941 s->calculate_kernel = bilinear_kernel;
2942 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2943 s->elements = 2 * 2;
2944 sizeof_uv = sizeof(int16_t) * s->elements;
2945 sizeof_ker = sizeof(int16_t) * s->elements;
2948 s->calculate_kernel = bicubic_kernel;
2949 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2950 s->elements = 4 * 4;
2951 sizeof_uv = sizeof(int16_t) * s->elements;
2952 sizeof_ker = sizeof(int16_t) * s->elements;
2955 s->calculate_kernel = lanczos_kernel;
2956 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2957 s->elements = 4 * 4;
2958 sizeof_uv = sizeof(int16_t) * s->elements;
2959 sizeof_ker = sizeof(int16_t) * s->elements;
2962 s->calculate_kernel = spline16_kernel;
2963 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2964 s->elements = 4 * 4;
2965 sizeof_uv = sizeof(int16_t) * s->elements;
2966 sizeof_ker = sizeof(int16_t) * s->elements;
2969 s->calculate_kernel = gaussian_kernel;
2970 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2971 s->elements = 4 * 4;
2972 sizeof_uv = sizeof(int16_t) * s->elements;
2973 sizeof_ker = sizeof(int16_t) * s->elements;
2979 ff_v360_init(s, depth);
2981 for (int order = 0; order < NB_RORDERS; order++) {
2982 const char c = s->rorder[order];
2986 av_log(ctx, AV_LOG_ERROR,
2987 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2988 return AVERROR(EINVAL);
2991 rorder = get_rorder(c);
2993 av_log(ctx, AV_LOG_ERROR,
2994 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2995 return AVERROR(EINVAL);
2998 s->rotation_order[order] = rorder;
3001 switch (s->in_stereo) {
3005 in_offset_w = in_offset_h = 0;
3023 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
3024 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
3026 s->in_width = s->inplanewidth[0];
3027 s->in_height = s->inplaneheight[0];
3029 if (s->id_fov > 0.f)
3030 fov_from_dfov(s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
3032 if (s->in_transpose)
3033 FFSWAP(int, s->in_width, s->in_height);
3036 case EQUIRECTANGULAR:
3037 s->in_transform = xyz_to_equirect;
3043 s->in_transform = xyz_to_cube3x2;
3044 err = prepare_cube_in(ctx);
3049 s->in_transform = xyz_to_cube1x6;
3050 err = prepare_cube_in(ctx);
3055 s->in_transform = xyz_to_cube6x1;
3056 err = prepare_cube_in(ctx);
3061 s->in_transform = xyz_to_eac;
3062 err = prepare_eac_in(ctx);
3067 s->in_transform = xyz_to_flat;
3068 err = prepare_flat_in(ctx);
3075 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
3076 return AVERROR(EINVAL);
3078 s->in_transform = xyz_to_dfisheye;
3084 s->in_transform = xyz_to_barrel;
3090 s->in_transform = xyz_to_stereographic;
3096 s->in_transform = xyz_to_mercator;
3102 s->in_transform = xyz_to_ball;
3108 s->in_transform = xyz_to_hammer;
3114 s->in_transform = xyz_to_sinusoidal;
3120 s->in_transform = xyz_to_cylindrical;
3121 err = prepare_cylindrical_in(ctx);
3126 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
3135 case EQUIRECTANGULAR:
3136 s->out_transform = equirect_to_xyz;
3142 s->out_transform = cube3x2_to_xyz;
3143 prepare_out = prepare_cube_out;
3144 w = lrintf(wf / 4.f * 3.f);
3148 s->out_transform = cube1x6_to_xyz;
3149 prepare_out = prepare_cube_out;
3150 w = lrintf(wf / 4.f);
3151 h = lrintf(hf * 3.f);
3154 s->out_transform = cube6x1_to_xyz;
3155 prepare_out = prepare_cube_out;
3156 w = lrintf(wf / 2.f * 3.f);
3157 h = lrintf(hf / 2.f);
3160 s->out_transform = eac_to_xyz;
3161 prepare_out = prepare_eac_out;
3163 h = lrintf(hf / 8.f * 9.f);
3166 s->out_transform = flat_to_xyz;
3167 prepare_out = prepare_flat_out;
3172 s->out_transform = dfisheye_to_xyz;
3178 s->out_transform = barrel_to_xyz;
3180 w = lrintf(wf / 4.f * 5.f);
3184 s->out_transform = stereographic_to_xyz;
3185 prepare_out = prepare_stereographic_out;
3187 h = lrintf(hf * 2.f);
3190 s->out_transform = mercator_to_xyz;
3193 h = lrintf(hf * 2.f);
3196 s->out_transform = ball_to_xyz;
3199 h = lrintf(hf * 2.f);
3202 s->out_transform = hammer_to_xyz;
3208 s->out_transform = sinusoidal_to_xyz;
3214 s->out_transform = fisheye_to_xyz;
3215 prepare_out = prepare_fisheye_out;
3216 w = lrintf(wf * 0.5f);
3220 s->out_transform = pannini_to_xyz;
3226 s->out_transform = cylindrical_to_xyz;
3227 prepare_out = prepare_cylindrical_out;
3229 h = lrintf(hf * 0.5f);
3232 s->out_transform = perspective_to_xyz;
3234 w = lrintf(wf / 2.f);
3238 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
3242 // Override resolution with user values if specified
3243 if (s->width > 0 && s->height > 0) {
3246 } else if (s->width > 0 || s->height > 0) {
3247 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
3248 return AVERROR(EINVAL);
3250 if (s->out_transpose)
3253 if (s->in_transpose)
3258 fov_from_dfov(s->d_fov, w, h, &s->h_fov, &s->v_fov);
3261 err = prepare_out(ctx);
3266 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
3268 s->out_width = s->pr_width[0];
3269 s->out_height = s->pr_height[0];
3271 if (s->out_transpose)
3272 FFSWAP(int, s->out_width, s->out_height);
3274 switch (s->out_stereo) {
3276 out_offset_w = out_offset_h = 0;
3292 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
3293 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
3295 for (int i = 0; i < 4; i++)
3296 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
3301 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
3303 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
3304 s->nb_allocated = 1;
3305 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
3307 s->nb_allocated = 2;
3308 s->map[0] = s->map[3] = 0;
3309 s->map[1] = s->map[2] = 1;
3312 for (int i = 0; i < s->nb_allocated; i++)
3313 allocate_plane(s, sizeof_uv, sizeof_ker, i);
3315 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
3316 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
3318 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3323 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3325 AVFilterContext *ctx = inlink->dst;
3326 AVFilterLink *outlink = ctx->outputs[0];
3327 V360Context *s = ctx->priv;
3331 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3334 return AVERROR(ENOMEM);
3336 av_frame_copy_props(out, in);
3341 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3344 return ff_filter_frame(outlink, out);
3347 static av_cold void uninit(AVFilterContext *ctx)
3349 V360Context *s = ctx->priv;
3351 for (int p = 0; p < s->nb_allocated; p++) {
3354 av_freep(&s->ker[p]);
3358 static const AVFilterPad inputs[] = {
3361 .type = AVMEDIA_TYPE_VIDEO,
3362 .filter_frame = filter_frame,
3367 static const AVFilterPad outputs[] = {
3370 .type = AVMEDIA_TYPE_VIDEO,
3371 .config_props = config_output,
3376 AVFilter ff_vf_v360 = {
3378 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3379 .priv_size = sizeof(V360Context),
3381 .query_formats = query_formats,
3384 .priv_class = &v360_class,
3385 .flags = AVFILTER_FLAG_SLICE_THREADS,