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 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
65 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
66 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
67 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
68 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
69 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
70 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
71 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
72 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
73 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
74 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
75 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
76 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
77 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
78 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
79 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
80 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
81 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
82 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
83 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
84 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
85 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
86 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
87 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
88 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
89 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
90 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
91 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
92 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
93 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
94 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
95 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
96 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
97 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
98 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
99 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
100 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
101 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
102 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
103 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
104 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
105 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
106 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
107 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
108 { "in_pad", "input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
109 { "out_pad", "output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
110 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
111 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
112 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
113 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
114 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "h_fov"},
115 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "v_fov"},
116 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "d_fov"},
117 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
118 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
119 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
120 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
121 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
122 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
123 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
127 AVFILTER_DEFINE_CLASS(v360);
129 static int query_formats(AVFilterContext *ctx)
131 static const enum AVPixelFormat pix_fmts[] = {
133 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
134 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
135 AV_PIX_FMT_YUVA444P16,
138 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
139 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
140 AV_PIX_FMT_YUVA422P16,
143 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
144 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
147 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
148 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
152 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
153 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
154 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
157 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
158 AV_PIX_FMT_YUV440P12,
161 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
162 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
163 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
166 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
167 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
168 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
177 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
178 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
179 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
182 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
183 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
186 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
187 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
188 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
193 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
195 return AVERROR(ENOMEM);
196 return ff_set_common_formats(ctx, fmts_list);
199 #define DEFINE_REMAP1_LINE(bits, div) \
200 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
201 ptrdiff_t in_linesize, \
202 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
204 const uint##bits##_t *s = (const uint##bits##_t *)src; \
205 uint##bits##_t *d = (uint##bits##_t *)dst; \
207 in_linesize /= div; \
209 for (int x = 0; x < width; x++) \
210 d[x] = s[v[x] * in_linesize + u[x]]; \
213 DEFINE_REMAP1_LINE( 8, 1)
214 DEFINE_REMAP1_LINE(16, 2)
217 * Generate remapping function with a given window size and pixel depth.
219 * @param ws size of interpolation window
220 * @param bits number of bits per pixel
222 #define DEFINE_REMAP(ws, bits) \
223 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
225 ThreadData *td = (ThreadData*)arg; \
226 const V360Context *s = ctx->priv; \
227 const AVFrame *in = td->in; \
228 AVFrame *out = td->out; \
230 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
231 for (int plane = 0; plane < s->nb_planes; plane++) { \
232 const int in_linesize = in->linesize[plane]; \
233 const int out_linesize = out->linesize[plane]; \
234 const int uv_linesize = s->uv_linesize[plane]; \
235 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
236 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
237 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
238 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
239 const uint8_t *src = in->data[plane] + in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
240 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
241 const int width = s->pr_width[plane]; \
242 const int height = s->pr_height[plane]; \
244 const int slice_start = (height * jobnr ) / nb_jobs; \
245 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
247 for (int y = slice_start; y < slice_end; y++) { \
248 const unsigned map = s->map[plane]; \
249 const uint16_t *u = s->u[map] + y * uv_linesize * ws * ws; \
250 const uint16_t *v = s->v[map] + y * uv_linesize * ws * ws; \
251 const int16_t *ker = s->ker[map] + y * uv_linesize * ws * ws; \
253 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
268 #define DEFINE_REMAP_LINE(ws, bits, div) \
269 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
270 ptrdiff_t in_linesize, \
271 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
273 const uint##bits##_t *s = (const uint##bits##_t *)src; \
274 uint##bits##_t *d = (uint##bits##_t *)dst; \
276 in_linesize /= div; \
278 for (int x = 0; x < width; x++) { \
279 const uint16_t *uu = u + x * ws * ws; \
280 const uint16_t *vv = v + x * ws * ws; \
281 const int16_t *kker = ker + x * ws * ws; \
284 for (int i = 0; i < ws; i++) { \
285 for (int j = 0; j < ws; j++) { \
286 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
290 d[x] = av_clip_uint##bits(tmp >> 14); \
294 DEFINE_REMAP_LINE(2, 8, 1)
295 DEFINE_REMAP_LINE(4, 8, 1)
296 DEFINE_REMAP_LINE(2, 16, 2)
297 DEFINE_REMAP_LINE(4, 16, 2)
299 void ff_v360_init(V360Context *s, int depth)
303 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
306 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
310 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
315 ff_v360_init_x86(s, depth);
319 * Save nearest pixel coordinates for remapping.
321 * @param du horizontal relative coordinate
322 * @param dv vertical relative coordinate
323 * @param rmap calculated 4x4 window
324 * @param u u remap data
325 * @param v v remap data
326 * @param ker ker remap data
328 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
329 uint16_t *u, uint16_t *v, int16_t *ker)
331 const int i = roundf(dv) + 1;
332 const int j = roundf(du) + 1;
334 u[0] = rmap->u[i][j];
335 v[0] = rmap->v[i][j];
339 * Calculate kernel for bilinear interpolation.
341 * @param du horizontal relative coordinate
342 * @param dv vertical relative coordinate
343 * @param rmap calculated 4x4 window
344 * @param u u remap data
345 * @param v v remap data
346 * @param ker ker remap data
348 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
349 uint16_t *u, uint16_t *v, int16_t *ker)
351 for (int i = 0; i < 2; i++) {
352 for (int j = 0; j < 2; j++) {
353 u[i * 2 + j] = rmap->u[i + 1][j + 1];
354 v[i * 2 + j] = rmap->v[i + 1][j + 1];
358 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
359 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
360 ker[2] = lrintf((1.f - du) * dv * 16385.f);
361 ker[3] = lrintf( du * dv * 16385.f);
365 * Calculate 1-dimensional cubic coefficients.
367 * @param t relative coordinate
368 * @param coeffs coefficients
370 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
372 const float tt = t * t;
373 const float ttt = t * t * t;
375 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
376 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
377 coeffs[2] = t + tt / 2.f - ttt / 2.f;
378 coeffs[3] = - t / 6.f + ttt / 6.f;
382 * Calculate kernel for bicubic interpolation.
384 * @param du horizontal relative coordinate
385 * @param dv vertical relative coordinate
386 * @param rmap calculated 4x4 window
387 * @param u u remap data
388 * @param v v remap data
389 * @param ker ker remap data
391 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
392 uint16_t *u, uint16_t *v, int16_t *ker)
397 calculate_bicubic_coeffs(du, du_coeffs);
398 calculate_bicubic_coeffs(dv, dv_coeffs);
400 for (int i = 0; i < 4; i++) {
401 for (int j = 0; j < 4; j++) {
402 u[i * 4 + j] = rmap->u[i][j];
403 v[i * 4 + j] = rmap->v[i][j];
404 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
410 * Calculate 1-dimensional lanczos coefficients.
412 * @param t relative coordinate
413 * @param coeffs coefficients
415 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
419 for (int i = 0; i < 4; i++) {
420 const float x = M_PI * (t - i + 1);
424 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
429 for (int i = 0; i < 4; i++) {
435 * Calculate kernel for lanczos interpolation.
437 * @param du horizontal relative coordinate
438 * @param dv vertical relative coordinate
439 * @param rmap calculated 4x4 window
440 * @param u u remap data
441 * @param v v remap data
442 * @param ker ker remap data
444 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
445 uint16_t *u, uint16_t *v, int16_t *ker)
450 calculate_lanczos_coeffs(du, du_coeffs);
451 calculate_lanczos_coeffs(dv, dv_coeffs);
453 for (int i = 0; i < 4; i++) {
454 for (int j = 0; j < 4; j++) {
455 u[i * 4 + j] = rmap->u[i][j];
456 v[i * 4 + j] = rmap->v[i][j];
457 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
463 * Modulo operation with only positive remainders.
468 * @return positive remainder of (a / b)
470 static inline int mod(int a, int b)
472 const int res = a % b;
481 * Convert char to corresponding direction.
482 * Used for cubemap options.
484 static int get_direction(char c)
505 * Convert char to corresponding rotation angle.
506 * Used for cubemap options.
508 static int get_rotation(char c)
525 * Convert char to corresponding rotation order.
527 static int get_rorder(char c)
545 * Prepare data for processing cubemap input format.
547 * @param ctx filter context
551 static int prepare_cube_in(AVFilterContext *ctx)
553 V360Context *s = ctx->priv;
555 for (int face = 0; face < NB_FACES; face++) {
556 const char c = s->in_forder[face];
560 av_log(ctx, AV_LOG_ERROR,
561 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
562 return AVERROR(EINVAL);
565 direction = get_direction(c);
566 if (direction == -1) {
567 av_log(ctx, AV_LOG_ERROR,
568 "Incorrect direction symbol '%c' in in_forder option.\n", c);
569 return AVERROR(EINVAL);
572 s->in_cubemap_face_order[direction] = face;
575 for (int face = 0; face < NB_FACES; face++) {
576 const char c = s->in_frot[face];
580 av_log(ctx, AV_LOG_ERROR,
581 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
582 return AVERROR(EINVAL);
585 rotation = get_rotation(c);
586 if (rotation == -1) {
587 av_log(ctx, AV_LOG_ERROR,
588 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
589 return AVERROR(EINVAL);
592 s->in_cubemap_face_rotation[face] = rotation;
599 * Prepare data for processing cubemap output format.
601 * @param ctx filter context
605 static int prepare_cube_out(AVFilterContext *ctx)
607 V360Context *s = ctx->priv;
609 for (int face = 0; face < NB_FACES; face++) {
610 const char c = s->out_forder[face];
614 av_log(ctx, AV_LOG_ERROR,
615 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
616 return AVERROR(EINVAL);
619 direction = get_direction(c);
620 if (direction == -1) {
621 av_log(ctx, AV_LOG_ERROR,
622 "Incorrect direction symbol '%c' in out_forder option.\n", c);
623 return AVERROR(EINVAL);
626 s->out_cubemap_direction_order[face] = direction;
629 for (int face = 0; face < NB_FACES; face++) {
630 const char c = s->out_frot[face];
634 av_log(ctx, AV_LOG_ERROR,
635 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
636 return AVERROR(EINVAL);
639 rotation = get_rotation(c);
640 if (rotation == -1) {
641 av_log(ctx, AV_LOG_ERROR,
642 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
643 return AVERROR(EINVAL);
646 s->out_cubemap_face_rotation[face] = rotation;
652 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
678 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
709 static void normalize_vector(float *vec)
711 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
719 * Calculate 3D coordinates on sphere for corresponding cubemap position.
720 * Common operation for every cubemap.
722 * @param s filter private context
723 * @param uf horizontal cubemap coordinate [0, 1)
724 * @param vf vertical cubemap coordinate [0, 1)
725 * @param face face of cubemap
726 * @param vec coordinates on sphere
728 static void cube_to_xyz(const V360Context *s,
729 float uf, float vf, int face,
732 const int direction = s->out_cubemap_direction_order[face];
735 uf /= (1.f - s->out_pad);
736 vf /= (1.f - s->out_pad);
738 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
779 normalize_vector(vec);
783 * Calculate cubemap position for corresponding 3D coordinates on sphere.
784 * Common operation for every cubemap.
786 * @param s filter private context
787 * @param vec coordinated on sphere
788 * @param uf horizontal cubemap coordinate [0, 1)
789 * @param vf vertical cubemap coordinate [0, 1)
790 * @param direction direction of view
792 static void xyz_to_cube(const V360Context *s,
794 float *uf, float *vf, int *direction)
796 const float phi = atan2f(vec[0], -vec[2]);
797 const float theta = asinf(-vec[1]);
798 float phi_norm, theta_threshold;
801 if (phi >= -M_PI_4 && phi < M_PI_4) {
804 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
806 phi_norm = phi + M_PI_2;
807 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
809 phi_norm = phi - M_PI_2;
812 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
815 theta_threshold = atanf(cosf(phi_norm));
816 if (theta > theta_threshold) {
818 } else if (theta < -theta_threshold) {
822 switch (*direction) {
824 *uf = vec[2] / vec[0];
825 *vf = -vec[1] / vec[0];
828 *uf = vec[2] / vec[0];
829 *vf = vec[1] / vec[0];
832 *uf = vec[0] / vec[1];
833 *vf = -vec[2] / vec[1];
836 *uf = -vec[0] / vec[1];
837 *vf = -vec[2] / vec[1];
840 *uf = -vec[0] / vec[2];
841 *vf = vec[1] / vec[2];
844 *uf = -vec[0] / vec[2];
845 *vf = -vec[1] / vec[2];
851 face = s->in_cubemap_face_order[*direction];
852 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
854 (*uf) *= s->input_mirror_modifier[0];
855 (*vf) *= s->input_mirror_modifier[1];
859 * Find position on another cube face in case of overflow/underflow.
860 * Used for calculation of interpolation window.
862 * @param s filter private context
863 * @param uf horizontal cubemap coordinate
864 * @param vf vertical cubemap coordinate
865 * @param direction direction of view
866 * @param new_uf new horizontal cubemap coordinate
867 * @param new_vf new vertical cubemap coordinate
868 * @param face face position on cubemap
870 static void process_cube_coordinates(const V360Context *s,
871 float uf, float vf, int direction,
872 float *new_uf, float *new_vf, int *face)
875 * Cubemap orientation
882 * +-------+-------+-------+-------+ ^ e |
884 * | left | front | right | back | | g |
885 * +-------+-------+-------+-------+ v h v
891 *face = s->in_cubemap_face_order[direction];
892 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
894 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
895 // There are no pixels to use in this case
898 } else if (uf < -1.f) {
934 } else if (uf >= 1.f) {
970 } else if (vf < -1.f) {
1006 } else if (vf >= 1.f) {
1008 switch (direction) {
1048 *face = s->in_cubemap_face_order[direction];
1049 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1053 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1055 * @param s filter private context
1056 * @param i horizontal position on frame [0, width)
1057 * @param j vertical position on frame [0, height)
1058 * @param width frame width
1059 * @param height frame height
1060 * @param vec coordinates on sphere
1062 static void cube3x2_to_xyz(const V360Context *s,
1063 int i, int j, int width, int height,
1066 const float ew = width / 3.f;
1067 const float eh = height / 2.f;
1069 const int u_face = floorf(i / ew);
1070 const int v_face = floorf(j / eh);
1071 const int face = u_face + 3 * v_face;
1073 const int u_shift = ceilf(ew * u_face);
1074 const int v_shift = ceilf(eh * v_face);
1075 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1076 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1078 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1079 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1081 cube_to_xyz(s, uf, vf, face, vec);
1085 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1087 * @param s filter private context
1088 * @param vec coordinates on sphere
1089 * @param width frame width
1090 * @param height frame height
1091 * @param us horizontal coordinates for interpolation window
1092 * @param vs vertical coordinates for interpolation window
1093 * @param du horizontal relative coordinate
1094 * @param dv vertical relative coordinate
1096 static void xyz_to_cube3x2(const V360Context *s,
1097 const float *vec, int width, int height,
1098 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1100 const float ew = width / 3.f;
1101 const float eh = height / 2.f;
1105 int direction, face;
1108 xyz_to_cube(s, vec, &uf, &vf, &direction);
1110 uf *= (1.f - s->in_pad);
1111 vf *= (1.f - s->in_pad);
1113 face = s->in_cubemap_face_order[direction];
1116 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1117 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1119 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1120 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1128 for (int i = -1; i < 3; i++) {
1129 for (int j = -1; j < 3; j++) {
1130 int new_ui = ui + j;
1131 int new_vi = vi + i;
1132 int u_shift, v_shift;
1133 int new_ewi, new_ehi;
1135 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1136 face = s->in_cubemap_face_order[direction];
1140 u_shift = ceilf(ew * u_face);
1141 v_shift = ceilf(eh * v_face);
1143 uf = 2.f * new_ui / ewi - 1.f;
1144 vf = 2.f * new_vi / ehi - 1.f;
1146 uf /= (1.f - s->in_pad);
1147 vf /= (1.f - s->in_pad);
1149 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1151 uf *= (1.f - s->in_pad);
1152 vf *= (1.f - s->in_pad);
1156 u_shift = ceilf(ew * u_face);
1157 v_shift = ceilf(eh * v_face);
1158 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1159 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1161 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1162 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1165 us[i + 1][j + 1] = u_shift + new_ui;
1166 vs[i + 1][j + 1] = v_shift + new_vi;
1172 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1174 * @param s filter private context
1175 * @param i horizontal position on frame [0, width)
1176 * @param j vertical position on frame [0, height)
1177 * @param width frame width
1178 * @param height frame height
1179 * @param vec coordinates on sphere
1181 static void cube1x6_to_xyz(const V360Context *s,
1182 int i, int j, int width, int height,
1185 const float ew = width;
1186 const float eh = height / 6.f;
1188 const int face = floorf(j / eh);
1190 const int v_shift = ceilf(eh * face);
1191 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1193 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1194 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1196 cube_to_xyz(s, uf, vf, face, vec);
1200 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1202 * @param s filter private context
1203 * @param i horizontal position on frame [0, width)
1204 * @param j vertical position on frame [0, height)
1205 * @param width frame width
1206 * @param height frame height
1207 * @param vec coordinates on sphere
1209 static void cube6x1_to_xyz(const V360Context *s,
1210 int i, int j, int width, int height,
1213 const float ew = width / 6.f;
1214 const float eh = height;
1216 const int face = floorf(i / ew);
1218 const int u_shift = ceilf(ew * face);
1219 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1221 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1222 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1224 cube_to_xyz(s, uf, vf, face, vec);
1228 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1230 * @param s filter private context
1231 * @param vec coordinates on sphere
1232 * @param width frame width
1233 * @param height frame height
1234 * @param us horizontal coordinates for interpolation window
1235 * @param vs vertical coordinates for interpolation window
1236 * @param du horizontal relative coordinate
1237 * @param dv vertical relative coordinate
1239 static void xyz_to_cube1x6(const V360Context *s,
1240 const float *vec, int width, int height,
1241 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1243 const float eh = height / 6.f;
1244 const int ewi = width;
1248 int direction, face;
1250 xyz_to_cube(s, vec, &uf, &vf, &direction);
1252 uf *= (1.f - s->in_pad);
1253 vf *= (1.f - s->in_pad);
1255 face = s->in_cubemap_face_order[direction];
1256 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1258 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1259 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1267 for (int i = -1; i < 3; i++) {
1268 for (int j = -1; j < 3; j++) {
1269 int new_ui = ui + j;
1270 int new_vi = vi + i;
1274 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1275 face = s->in_cubemap_face_order[direction];
1277 v_shift = ceilf(eh * face);
1279 uf = 2.f * new_ui / ewi - 1.f;
1280 vf = 2.f * new_vi / ehi - 1.f;
1282 uf /= (1.f - s->in_pad);
1283 vf /= (1.f - s->in_pad);
1285 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1287 uf *= (1.f - s->in_pad);
1288 vf *= (1.f - s->in_pad);
1290 v_shift = ceilf(eh * face);
1291 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1293 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1294 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1297 us[i + 1][j + 1] = new_ui;
1298 vs[i + 1][j + 1] = v_shift + new_vi;
1304 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1306 * @param s filter private context
1307 * @param vec coordinates on sphere
1308 * @param width frame width
1309 * @param height frame height
1310 * @param us horizontal coordinates for interpolation window
1311 * @param vs vertical coordinates for interpolation window
1312 * @param du horizontal relative coordinate
1313 * @param dv vertical relative coordinate
1315 static void xyz_to_cube6x1(const V360Context *s,
1316 const float *vec, int width, int height,
1317 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1319 const float ew = width / 6.f;
1320 const int ehi = height;
1324 int direction, face;
1326 xyz_to_cube(s, vec, &uf, &vf, &direction);
1328 uf *= (1.f - s->in_pad);
1329 vf *= (1.f - s->in_pad);
1331 face = s->in_cubemap_face_order[direction];
1332 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1334 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1335 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1343 for (int i = -1; i < 3; i++) {
1344 for (int j = -1; j < 3; j++) {
1345 int new_ui = ui + j;
1346 int new_vi = vi + i;
1350 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1351 face = s->in_cubemap_face_order[direction];
1353 u_shift = ceilf(ew * face);
1355 uf = 2.f * new_ui / ewi - 1.f;
1356 vf = 2.f * new_vi / ehi - 1.f;
1358 uf /= (1.f - s->in_pad);
1359 vf /= (1.f - s->in_pad);
1361 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1363 uf *= (1.f - s->in_pad);
1364 vf *= (1.f - s->in_pad);
1366 u_shift = ceilf(ew * face);
1367 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1369 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1370 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1373 us[i + 1][j + 1] = u_shift + new_ui;
1374 vs[i + 1][j + 1] = new_vi;
1380 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1382 * @param s filter private context
1383 * @param i horizontal position on frame [0, width)
1384 * @param j vertical position on frame [0, height)
1385 * @param width frame width
1386 * @param height frame height
1387 * @param vec coordinates on sphere
1389 static void equirect_to_xyz(const V360Context *s,
1390 int i, int j, int width, int height,
1393 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1394 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1396 const float sin_phi = sinf(phi);
1397 const float cos_phi = cosf(phi);
1398 const float sin_theta = sinf(theta);
1399 const float cos_theta = cosf(theta);
1401 vec[0] = cos_theta * sin_phi;
1402 vec[1] = -sin_theta;
1403 vec[2] = -cos_theta * cos_phi;
1407 * Prepare data for processing stereographic output format.
1409 * @param ctx filter context
1411 * @return error code
1413 static int prepare_stereographic_out(AVFilterContext *ctx)
1415 V360Context *s = ctx->priv;
1417 const float h_angle = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1418 const float v_angle = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1420 s->flat_range[0] = h_angle;
1421 s->flat_range[1] = v_angle;
1427 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1429 * @param s filter private context
1430 * @param i horizontal position on frame [0, width)
1431 * @param j vertical position on frame [0, height)
1432 * @param width frame width
1433 * @param height frame height
1434 * @param vec coordinates on sphere
1436 static void stereographic_to_xyz(const V360Context *s,
1437 int i, int j, int width, int height,
1440 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1441 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1442 const float xy = x * x + y * y;
1444 vec[0] = 2.f * x / (1.f + xy);
1445 vec[1] = (-1.f + xy) / (1.f + xy);
1446 vec[2] = 2.f * y / (1.f + xy);
1448 normalize_vector(vec);
1452 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1454 * @param s filter private context
1455 * @param vec coordinates on sphere
1456 * @param width frame width
1457 * @param height frame height
1458 * @param us horizontal coordinates for interpolation window
1459 * @param vs vertical coordinates for interpolation window
1460 * @param du horizontal relative coordinate
1461 * @param dv vertical relative coordinate
1463 static void xyz_to_stereographic(const V360Context *s,
1464 const float *vec, int width, int height,
1465 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1467 const float x = av_clipf(vec[0] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[0];
1468 const float y = av_clipf(vec[2] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[1];
1472 uf = (x + 1.f) * width / 2.f;
1473 vf = (y + 1.f) * height / 2.f;
1480 for (int i = -1; i < 3; i++) {
1481 for (int j = -1; j < 3; j++) {
1482 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1483 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1489 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1491 * @param s filter private context
1492 * @param vec coordinates on sphere
1493 * @param width frame width
1494 * @param height frame height
1495 * @param us horizontal coordinates for interpolation window
1496 * @param vs vertical coordinates for interpolation window
1497 * @param du horizontal relative coordinate
1498 * @param dv vertical relative coordinate
1500 static void xyz_to_equirect(const V360Context *s,
1501 const float *vec, int width, int height,
1502 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1504 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1505 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1509 uf = (phi / M_PI + 1.f) * width / 2.f;
1510 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1517 for (int i = -1; i < 3; i++) {
1518 for (int j = -1; j < 3; j++) {
1519 us[i + 1][j + 1] = mod(ui + j, width);
1520 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1526 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1528 * @param s filter private context
1529 * @param vec coordinates on sphere
1530 * @param width frame width
1531 * @param height frame height
1532 * @param us horizontal coordinates for interpolation window
1533 * @param vs vertical coordinates for interpolation window
1534 * @param du horizontal relative coordinate
1535 * @param dv vertical relative coordinate
1537 static void xyz_to_mercator(const V360Context *s,
1538 const float *vec, int width, int height,
1539 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1541 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1542 const float theta = 0.5f * asinhf(vec[1] / sqrtf(1.f - vec[1] * vec[1])) * s->input_mirror_modifier[1];
1546 uf = (phi / M_PI + 1.f) * width / 2.f;
1547 vf = (theta / M_PI + 1.f) * height / 2.f;
1554 for (int i = -1; i < 3; i++) {
1555 for (int j = -1; j < 3; j++) {
1556 us[i + 1][j + 1] = mod(ui + j, width);
1557 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1563 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1565 * @param s filter private context
1566 * @param i horizontal position on frame [0, width)
1567 * @param j vertical position on frame [0, height)
1568 * @param width frame width
1569 * @param height frame height
1570 * @param vec coordinates on sphere
1572 static void mercator_to_xyz(const V360Context *s,
1573 int i, int j, int width, int height,
1576 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1577 const float theta = atanf(sinhf(((2.f * j) / height - 1.f) * 2.f * M_PI));
1579 const float sin_phi = sinf(phi);
1580 const float cos_phi = cosf(phi);
1581 const float sin_theta = sinf(theta);
1582 const float cos_theta = cosf(theta);
1584 vec[0] = cos_theta * sin_phi;
1585 vec[1] = -sin_theta;
1586 vec[2] = -cos_theta * cos_phi;
1590 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1592 * @param s filter private context
1593 * @param vec coordinates on sphere
1594 * @param width frame width
1595 * @param height frame height
1596 * @param us horizontal coordinates for interpolation window
1597 * @param vs vertical coordinates for interpolation window
1598 * @param du horizontal relative coordinate
1599 * @param dv vertical relative coordinate
1601 static void xyz_to_ball(const V360Context *s,
1602 const float *vec, int width, int height,
1603 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1605 const float l = hypotf(vec[0], vec[1]);
1606 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1610 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1611 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1619 for (int i = -1; i < 3; i++) {
1620 for (int j = -1; j < 3; j++) {
1621 us[i + 1][j + 1] = mod(ui + j, width);
1622 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1628 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1630 * @param s filter private context
1631 * @param i horizontal position on frame [0, width)
1632 * @param j vertical position on frame [0, height)
1633 * @param width frame width
1634 * @param height frame height
1635 * @param vec coordinates on sphere
1637 static void ball_to_xyz(const V360Context *s,
1638 int i, int j, int width, int height,
1641 const float x = (2.f * i) / width - 1.f;
1642 const float y = (2.f * j) / height - 1.f;
1643 const float l = hypotf(x, y);
1646 const float z = 2.f * l * sqrtf(1.f - l * l);
1648 vec[0] = z * x / (l > 0.f ? l : 1.f);
1649 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1650 vec[2] = -1.f + 2.f * l * l;
1659 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1661 * @param s filter private context
1662 * @param i horizontal position on frame [0, width)
1663 * @param j vertical position on frame [0, height)
1664 * @param width frame width
1665 * @param height frame height
1666 * @param vec coordinates on sphere
1668 static void hammer_to_xyz(const V360Context *s,
1669 int i, int j, int width, int height,
1672 const float x = ((2.f * i) / width - 1.f);
1673 const float y = ((2.f * j) / height - 1.f);
1675 const float xx = x * x;
1676 const float yy = y * y;
1678 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1680 const float a = M_SQRT2 * x * z;
1681 const float b = 2.f * z * z - 1.f;
1683 const float aa = a * a;
1684 const float bb = b * b;
1686 const float w = sqrtf(1.f - 2.f * yy * z * z);
1688 vec[0] = w * 2.f * a * b / (aa + bb);
1689 vec[1] = -M_SQRT2 * y * z;
1690 vec[2] = -w * (bb - aa) / (aa + bb);
1692 normalize_vector(vec);
1696 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1698 * @param s filter private context
1699 * @param vec coordinates on sphere
1700 * @param width frame width
1701 * @param height frame height
1702 * @param us horizontal coordinates for interpolation window
1703 * @param vs vertical coordinates for interpolation window
1704 * @param du horizontal relative coordinate
1705 * @param dv vertical relative coordinate
1707 static void xyz_to_hammer(const V360Context *s,
1708 const float *vec, int width, int height,
1709 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1711 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1713 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1714 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1715 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1719 uf = (x + 1.f) * width / 2.f;
1720 vf = (y + 1.f) * height / 2.f;
1727 for (int i = -1; i < 3; i++) {
1728 for (int j = -1; j < 3; j++) {
1729 us[i + 1][j + 1] = mod(ui + j, width);
1730 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1736 * Prepare data for processing equi-angular cubemap input format.
1738 * @param ctx filter context
1740 * @return error code
1742 static int prepare_eac_in(AVFilterContext *ctx)
1744 V360Context *s = ctx->priv;
1746 if (s->ih_flip && s->iv_flip) {
1747 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
1748 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
1749 s->in_cubemap_face_order[UP] = TOP_LEFT;
1750 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
1751 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1752 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1753 } else if (s->ih_flip) {
1754 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
1755 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
1756 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
1757 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
1758 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1759 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1760 } else if (s->iv_flip) {
1761 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
1762 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
1763 s->in_cubemap_face_order[UP] = TOP_RIGHT;
1764 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
1765 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1766 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1768 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1769 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1770 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1771 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1772 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1773 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1777 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
1778 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
1779 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
1780 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
1781 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
1782 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
1784 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1785 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1786 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1787 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1788 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1789 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1796 * Prepare data for processing equi-angular cubemap output format.
1798 * @param ctx filter context
1800 * @return error code
1802 static int prepare_eac_out(AVFilterContext *ctx)
1804 V360Context *s = ctx->priv;
1806 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1807 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1808 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1809 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1810 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1811 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1813 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1814 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1815 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1816 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1817 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1818 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1824 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap 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 eac_to_xyz(const V360Context *s,
1834 int i, int j, int width, int height,
1837 const float pixel_pad = 2;
1838 const float u_pad = pixel_pad / width;
1839 const float v_pad = pixel_pad / height;
1841 int u_face, v_face, face;
1843 float l_x, l_y, l_z;
1845 float uf = (i + 0.5f) / width;
1846 float vf = (j + 0.5f) / height;
1848 // EAC has 2-pixel padding on faces except between faces on the same row
1849 // Padding pixels seems not to be stretched with tangent as regular pixels
1850 // Formulas below approximate original padding as close as I could get experimentally
1852 // Horizontal padding
1853 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1857 } else if (uf >= 3.f) {
1861 u_face = floorf(uf);
1862 uf = fmodf(uf, 1.f) - 0.5f;
1866 v_face = floorf(vf * 2.f);
1867 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1869 if (uf >= -0.5f && uf < 0.5f) {
1870 uf = tanf(M_PI_2 * uf);
1874 if (vf >= -0.5f && vf < 0.5f) {
1875 vf = tanf(M_PI_2 * vf);
1880 face = u_face + 3 * v_face;
1921 normalize_vector(vec);
1925 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
1927 * @param s filter private context
1928 * @param vec coordinates on sphere
1929 * @param width frame width
1930 * @param height frame height
1931 * @param us horizontal coordinates for interpolation window
1932 * @param vs vertical coordinates for interpolation window
1933 * @param du horizontal relative coordinate
1934 * @param dv vertical relative coordinate
1936 static void xyz_to_eac(const V360Context *s,
1937 const float *vec, int width, int height,
1938 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1940 const float pixel_pad = 2;
1941 const float u_pad = pixel_pad / width;
1942 const float v_pad = pixel_pad / height;
1946 int direction, face;
1949 xyz_to_cube(s, vec, &uf, &vf, &direction);
1951 face = s->in_cubemap_face_order[direction];
1955 uf = M_2_PI * atanf(uf) + 0.5f;
1956 vf = M_2_PI * atanf(vf) + 0.5f;
1958 // These formulas are inversed from eac_to_xyz ones
1959 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
1960 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
1974 for (int i = -1; i < 3; i++) {
1975 for (int j = -1; j < 3; j++) {
1976 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1977 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1983 * Prepare data for processing flat output format.
1985 * @param ctx filter context
1987 * @return error code
1989 static int prepare_flat_out(AVFilterContext *ctx)
1991 V360Context *s = ctx->priv;
1993 const float h_angle = 0.5f * s->h_fov * M_PI / 180.f;
1994 const float v_angle = 0.5f * s->v_fov * M_PI / 180.f;
1996 s->flat_range[0] = tanf(h_angle);
1997 s->flat_range[1] = tanf(v_angle);
2003 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2005 * @param s filter private context
2006 * @param i horizontal position on frame [0, width)
2007 * @param j vertical position on frame [0, height)
2008 * @param width frame width
2009 * @param height frame height
2010 * @param vec coordinates on sphere
2012 static void flat_to_xyz(const V360Context *s,
2013 int i, int j, int width, int height,
2016 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2017 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2023 normalize_vector(vec);
2027 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2029 * @param s filter private context
2030 * @param i horizontal position on frame [0, width)
2031 * @param j vertical position on frame [0, height)
2032 * @param width frame width
2033 * @param height frame height
2034 * @param vec coordinates on sphere
2036 static void dfisheye_to_xyz(const V360Context *s,
2037 int i, int j, int width, int height,
2040 const float scale = 1.f + s->out_pad;
2042 const float ew = width / 2.f;
2043 const float eh = height;
2045 const int ei = i >= ew ? i - ew : i;
2046 const float m = i >= ew ? -1.f : 1.f;
2048 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2049 const float vf = ((2.f * j) / eh - 1.f) * scale;
2051 const float h = hypotf(uf, vf);
2052 const float lh = h > 0.f ? h : 1.f;
2053 const float theta = m * M_PI_2 * (1.f - h);
2055 const float sin_theta = sinf(theta);
2056 const float cos_theta = cosf(theta);
2058 vec[0] = cos_theta * uf / lh;
2059 vec[1] = cos_theta * -vf / lh;
2062 normalize_vector(vec);
2066 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2068 * @param s filter private context
2069 * @param vec coordinates on sphere
2070 * @param width frame width
2071 * @param height frame height
2072 * @param us horizontal coordinates for interpolation window
2073 * @param vs vertical coordinates for interpolation window
2074 * @param du horizontal relative coordinate
2075 * @param dv vertical relative coordinate
2077 static void xyz_to_dfisheye(const V360Context *s,
2078 const float *vec, int width, int height,
2079 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2081 const float scale = 1.f - s->in_pad;
2083 const float ew = width / 2.f;
2084 const float eh = height;
2086 const float phi = atan2f(-vec[1], -vec[0]) * s->input_mirror_modifier[0];
2087 const float theta = acosf(fabsf(vec[2])) / M_PI * s->input_mirror_modifier[1];
2089 float uf = (theta * cosf(phi) * scale + 0.5f) * ew;
2090 float vf = (theta * sinf(phi) * scale + 0.5f) * eh;
2098 u_shift = ceilf(ew);
2108 for (int i = -1; i < 3; i++) {
2109 for (int j = -1; j < 3; j++) {
2110 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2111 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2117 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2119 * @param s filter private context
2120 * @param i horizontal position on frame [0, width)
2121 * @param j vertical position on frame [0, height)
2122 * @param width frame width
2123 * @param height frame height
2124 * @param vec coordinates on sphere
2126 static void barrel_to_xyz(const V360Context *s,
2127 int i, int j, int width, int height,
2130 const float scale = 0.99f;
2131 float l_x, l_y, l_z;
2133 if (i < 4 * width / 5) {
2134 const float theta_range = M_PI_4;
2136 const int ew = 4 * width / 5;
2137 const int eh = height;
2139 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2140 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2142 const float sin_phi = sinf(phi);
2143 const float cos_phi = cosf(phi);
2144 const float sin_theta = sinf(theta);
2145 const float cos_theta = cosf(theta);
2147 l_x = cos_theta * sin_phi;
2149 l_z = -cos_theta * cos_phi;
2151 const int ew = width / 5;
2152 const int eh = height / 2;
2157 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2158 vf = 2.f * (j ) / eh - 1.f;
2167 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2168 vf = 2.f * (j - eh) / eh - 1.f;
2183 normalize_vector(vec);
2187 * Calculate frame position in barrel facebook's 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_barrel(const V360Context *s,
2199 const float *vec, int width, int height,
2200 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2202 const float scale = 0.99f;
2204 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2205 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2206 const float theta_range = M_PI_4;
2209 int u_shift, v_shift;
2213 if (theta > -theta_range && theta < theta_range) {
2217 u_shift = s->ih_flip ? width / 5 : 0;
2220 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2221 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2226 u_shift = s->ih_flip ? 0 : 4 * ew;
2228 if (theta < 0.f) { // UP
2229 uf = vec[0] / vec[1];
2230 vf = -vec[2] / vec[1];
2233 uf = -vec[0] / vec[1];
2234 vf = -vec[2] / vec[1];
2238 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2239 vf *= s->input_mirror_modifier[1];
2241 uf = 0.5f * ew * (uf * scale + 1.f);
2242 vf = 0.5f * eh * (vf * scale + 1.f);
2251 for (int i = -1; i < 3; i++) {
2252 for (int j = -1; j < 3; j++) {
2253 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2254 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2259 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2261 for (int i = 0; i < 3; i++) {
2262 for (int j = 0; j < 3; j++) {
2265 for (int k = 0; k < 3; k++)
2266 sum += a[i][k] * b[k][j];
2274 * Calculate rotation matrix for yaw/pitch/roll angles.
2276 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2277 float rot_mat[3][3],
2278 const int rotation_order[3])
2280 const float yaw_rad = yaw * M_PI / 180.f;
2281 const float pitch_rad = pitch * M_PI / 180.f;
2282 const float roll_rad = roll * M_PI / 180.f;
2284 const float sin_yaw = sinf(-yaw_rad);
2285 const float cos_yaw = cosf(-yaw_rad);
2286 const float sin_pitch = sinf(pitch_rad);
2287 const float cos_pitch = cosf(pitch_rad);
2288 const float sin_roll = sinf(roll_rad);
2289 const float cos_roll = cosf(roll_rad);
2294 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2295 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2296 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2298 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2299 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2300 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2302 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2303 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2304 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2306 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2307 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2311 * Rotate vector with given rotation matrix.
2313 * @param rot_mat rotation matrix
2316 static inline void rotate(const float rot_mat[3][3],
2319 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2320 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2321 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2328 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2331 modifier[0] = h_flip ? -1.f : 1.f;
2332 modifier[1] = v_flip ? -1.f : 1.f;
2333 modifier[2] = d_flip ? -1.f : 1.f;
2336 static inline void mirror(const float *modifier, float *vec)
2338 vec[0] *= modifier[0];
2339 vec[1] *= modifier[1];
2340 vec[2] *= modifier[2];
2343 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2345 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2346 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2347 if (!s->u[p] || !s->v[p])
2348 return AVERROR(ENOMEM);
2350 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2352 return AVERROR(ENOMEM);
2358 static void fov_from_dfov(V360Context *s, float w, float h)
2360 const float da = tanf(0.5 * FFMIN(s->d_fov, 359.f) * M_PI / 180.f);
2361 const float d = hypotf(w, h);
2363 s->h_fov = atan2f(da * w, d) * 360.f / M_PI;
2364 s->v_fov = atan2f(da * h, d) * 360.f / M_PI;
2372 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2374 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2375 outw[0] = outw[3] = w;
2376 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2377 outh[0] = outh[3] = h;
2380 // Calculate remap data
2381 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2383 V360Context *s = ctx->priv;
2385 for (int p = 0; p < s->nb_allocated; p++) {
2386 const int width = s->pr_width[p];
2387 const int uv_linesize = s->uv_linesize[p];
2388 const int height = s->pr_height[p];
2389 const int in_width = s->inplanewidth[p];
2390 const int in_height = s->inplaneheight[p];
2391 const int slice_start = (height * jobnr ) / nb_jobs;
2392 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
2397 for (int j = slice_start; j < slice_end; j++) {
2398 for (int i = 0; i < width; i++) {
2399 uint16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2400 uint16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2401 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2403 if (s->out_transpose)
2404 s->out_transform(s, j, i, height, width, vec);
2406 s->out_transform(s, i, j, width, height, vec);
2407 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2408 rotate(s->rot_mat, vec);
2409 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2410 normalize_vector(vec);
2411 mirror(s->output_mirror_modifier, vec);
2412 if (s->in_transpose)
2413 s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2415 s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2416 av_assert1(!isnan(du) && !isnan(dv));
2417 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2425 static int config_output(AVFilterLink *outlink)
2427 AVFilterContext *ctx = outlink->src;
2428 AVFilterLink *inlink = ctx->inputs[0];
2429 V360Context *s = ctx->priv;
2430 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2431 const int depth = desc->comp[0].depth;
2436 int in_offset_h, in_offset_w;
2437 int out_offset_h, out_offset_w;
2439 int (*prepare_out)(AVFilterContext *ctx);
2441 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2442 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2444 switch (s->interp) {
2446 s->calculate_kernel = nearest_kernel;
2447 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2449 sizeof_uv = sizeof(uint16_t) * s->elements;
2453 s->calculate_kernel = bilinear_kernel;
2454 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2455 s->elements = 2 * 2;
2456 sizeof_uv = sizeof(uint16_t) * s->elements;
2457 sizeof_ker = sizeof(uint16_t) * s->elements;
2460 s->calculate_kernel = bicubic_kernel;
2461 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2462 s->elements = 4 * 4;
2463 sizeof_uv = sizeof(uint16_t) * s->elements;
2464 sizeof_ker = sizeof(uint16_t) * s->elements;
2467 s->calculate_kernel = lanczos_kernel;
2468 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2469 s->elements = 4 * 4;
2470 sizeof_uv = sizeof(uint16_t) * s->elements;
2471 sizeof_ker = sizeof(uint16_t) * s->elements;
2477 ff_v360_init(s, depth);
2479 for (int order = 0; order < NB_RORDERS; order++) {
2480 const char c = s->rorder[order];
2484 av_log(ctx, AV_LOG_ERROR,
2485 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2486 return AVERROR(EINVAL);
2489 rorder = get_rorder(c);
2491 av_log(ctx, AV_LOG_ERROR,
2492 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2493 return AVERROR(EINVAL);
2496 s->rotation_order[order] = rorder;
2499 switch (s->in_stereo) {
2503 in_offset_w = in_offset_h = 0;
2521 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
2522 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
2525 case EQUIRECTANGULAR:
2526 s->in_transform = xyz_to_equirect;
2532 s->in_transform = xyz_to_cube3x2;
2533 err = prepare_cube_in(ctx);
2538 s->in_transform = xyz_to_cube1x6;
2539 err = prepare_cube_in(ctx);
2544 s->in_transform = xyz_to_cube6x1;
2545 err = prepare_cube_in(ctx);
2550 s->in_transform = xyz_to_eac;
2551 err = prepare_eac_in(ctx);
2556 av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n");
2557 return AVERROR(EINVAL);
2559 s->in_transform = xyz_to_dfisheye;
2565 s->in_transform = xyz_to_barrel;
2571 s->in_transform = xyz_to_stereographic;
2577 s->in_transform = xyz_to_mercator;
2583 s->in_transform = xyz_to_ball;
2589 s->in_transform = xyz_to_hammer;
2595 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2604 case EQUIRECTANGULAR:
2605 s->out_transform = equirect_to_xyz;
2611 s->out_transform = cube3x2_to_xyz;
2612 prepare_out = prepare_cube_out;
2613 w = roundf(wf / 4.f * 3.f);
2617 s->out_transform = cube1x6_to_xyz;
2618 prepare_out = prepare_cube_out;
2619 w = roundf(wf / 4.f);
2620 h = roundf(hf * 3.f);
2623 s->out_transform = cube6x1_to_xyz;
2624 prepare_out = prepare_cube_out;
2625 w = roundf(wf / 2.f * 3.f);
2626 h = roundf(hf / 2.f);
2629 s->out_transform = eac_to_xyz;
2630 prepare_out = prepare_eac_out;
2632 h = roundf(hf / 8.f * 9.f);
2635 s->out_transform = flat_to_xyz;
2636 prepare_out = prepare_flat_out;
2641 s->out_transform = dfisheye_to_xyz;
2647 s->out_transform = barrel_to_xyz;
2649 w = roundf(wf / 4.f * 5.f);
2653 s->out_transform = stereographic_to_xyz;
2654 prepare_out = prepare_stereographic_out;
2656 h = roundf(hf * 2.f);
2659 s->out_transform = mercator_to_xyz;
2665 s->out_transform = ball_to_xyz;
2668 h = roundf(hf * 2.f);
2671 s->out_transform = hammer_to_xyz;
2677 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2681 // Override resolution with user values if specified
2682 if (s->width > 0 && s->height > 0) {
2685 } else if (s->width > 0 || s->height > 0) {
2686 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2687 return AVERROR(EINVAL);
2689 if (s->out_transpose)
2692 if (s->in_transpose)
2697 fov_from_dfov(s, w, h);
2700 err = prepare_out(ctx);
2705 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
2707 switch (s->out_stereo) {
2709 out_offset_w = out_offset_h = 0;
2725 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
2726 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
2728 for (int i = 0; i < 4; i++)
2729 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
2734 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2736 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2737 s->nb_allocated = 1;
2738 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2740 s->nb_allocated = 2;
2742 s->map[1] = s->map[2] = 1;
2746 for (int i = 0; i < s->nb_allocated; i++)
2747 allocate_plane(s, sizeof_uv, sizeof_ker, i);
2749 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
2750 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
2752 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2757 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2759 AVFilterContext *ctx = inlink->dst;
2760 AVFilterLink *outlink = ctx->outputs[0];
2761 V360Context *s = ctx->priv;
2765 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2768 return AVERROR(ENOMEM);
2770 av_frame_copy_props(out, in);
2775 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2778 return ff_filter_frame(outlink, out);
2781 static av_cold void uninit(AVFilterContext *ctx)
2783 V360Context *s = ctx->priv;
2785 for (int p = 0; p < s->nb_allocated; p++) {
2788 av_freep(&s->ker[p]);
2792 static const AVFilterPad inputs[] = {
2795 .type = AVMEDIA_TYPE_VIDEO,
2796 .filter_frame = filter_frame,
2801 static const AVFilterPad outputs[] = {
2804 .type = AVMEDIA_TYPE_VIDEO,
2805 .config_props = config_output,
2810 AVFilter ff_vf_v360 = {
2812 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
2813 .priv_size = sizeof(V360Context),
2815 .query_formats = query_formats,
2818 .priv_class = &v360_class,
2819 .flags = AVFILTER_FLAG_SLICE_THREADS,