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 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
70 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
71 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
72 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
73 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
74 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
75 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
76 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
77 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
78 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
79 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
80 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
81 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
82 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
83 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
84 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
85 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
86 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
87 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
88 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
89 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
90 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
91 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
92 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
93 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
94 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
95 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
96 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
97 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
98 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
99 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
100 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
101 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
102 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
103 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
104 { "in_pad", "input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
105 { "out_pad", "output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
106 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
107 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
108 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
109 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
110 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "h_fov"},
111 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "v_fov"},
112 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "d_fov"},
113 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
114 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
115 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
116 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
117 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
118 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
119 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
123 AVFILTER_DEFINE_CLASS(v360);
125 static int query_formats(AVFilterContext *ctx)
127 static const enum AVPixelFormat pix_fmts[] = {
129 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
130 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
131 AV_PIX_FMT_YUVA444P16,
134 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
135 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
136 AV_PIX_FMT_YUVA422P16,
139 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
140 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
143 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
144 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
148 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
149 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
150 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
153 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
154 AV_PIX_FMT_YUV440P12,
157 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
158 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
159 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
162 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
163 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
164 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
173 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
174 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
175 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
178 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
179 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
182 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
183 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
184 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
189 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
191 return AVERROR(ENOMEM);
192 return ff_set_common_formats(ctx, fmts_list);
195 #define DEFINE_REMAP1_LINE(bits, div) \
196 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
197 ptrdiff_t in_linesize, \
198 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
200 const uint##bits##_t *s = (const uint##bits##_t *)src; \
201 uint##bits##_t *d = (uint##bits##_t *)dst; \
203 in_linesize /= div; \
205 for (int x = 0; x < width; x++) \
206 d[x] = s[v[x] * in_linesize + u[x]]; \
209 DEFINE_REMAP1_LINE( 8, 1)
210 DEFINE_REMAP1_LINE(16, 2)
213 * Generate remapping function with a given window size and pixel depth.
215 * @param ws size of interpolation window
216 * @param bits number of bits per pixel
218 #define DEFINE_REMAP(ws, bits) \
219 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
221 ThreadData *td = (ThreadData*)arg; \
222 const V360Context *s = ctx->priv; \
223 const AVFrame *in = td->in; \
224 AVFrame *out = td->out; \
226 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
227 for (int plane = 0; plane < s->nb_planes; plane++) { \
228 const int in_linesize = in->linesize[plane]; \
229 const int out_linesize = out->linesize[plane]; \
230 const int uv_linesize = s->uv_linesize[plane]; \
231 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
232 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
233 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
234 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
235 const uint8_t *src = in->data[plane] + in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
236 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
237 const int width = s->pr_width[plane]; \
238 const int height = s->pr_height[plane]; \
240 const int slice_start = (height * jobnr ) / nb_jobs; \
241 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
243 for (int y = slice_start; y < slice_end; y++) { \
244 const unsigned map = s->map[plane]; \
245 const uint16_t *u = s->u[map] + y * uv_linesize * ws * ws; \
246 const uint16_t *v = s->v[map] + y * uv_linesize * ws * ws; \
247 const int16_t *ker = s->ker[map] + y * uv_linesize * ws * ws; \
249 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
264 #define DEFINE_REMAP_LINE(ws, bits, div) \
265 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
266 ptrdiff_t in_linesize, \
267 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
269 const uint##bits##_t *s = (const uint##bits##_t *)src; \
270 uint##bits##_t *d = (uint##bits##_t *)dst; \
272 in_linesize /= div; \
274 for (int x = 0; x < width; x++) { \
275 const uint16_t *uu = u + x * ws * ws; \
276 const uint16_t *vv = v + x * ws * ws; \
277 const int16_t *kker = ker + x * ws * ws; \
280 for (int i = 0; i < ws; i++) { \
281 for (int j = 0; j < ws; j++) { \
282 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
286 d[x] = av_clip_uint##bits(tmp >> 14); \
290 DEFINE_REMAP_LINE(2, 8, 1)
291 DEFINE_REMAP_LINE(4, 8, 1)
292 DEFINE_REMAP_LINE(2, 16, 2)
293 DEFINE_REMAP_LINE(4, 16, 2)
295 void ff_v360_init(V360Context *s, int depth)
299 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
302 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
306 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
311 ff_v360_init_x86(s, depth);
315 * Save nearest pixel coordinates for remapping.
317 * @param du horizontal relative coordinate
318 * @param dv vertical relative coordinate
319 * @param rmap calculated 4x4 window
320 * @param u u remap data
321 * @param v v remap data
322 * @param ker ker remap data
324 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
325 uint16_t *u, uint16_t *v, int16_t *ker)
327 const int i = roundf(dv) + 1;
328 const int j = roundf(du) + 1;
330 u[0] = rmap->u[i][j];
331 v[0] = rmap->v[i][j];
335 * Calculate kernel for bilinear interpolation.
337 * @param du horizontal relative coordinate
338 * @param dv vertical relative coordinate
339 * @param rmap calculated 4x4 window
340 * @param u u remap data
341 * @param v v remap data
342 * @param ker ker remap data
344 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
345 uint16_t *u, uint16_t *v, int16_t *ker)
347 for (int i = 0; i < 2; i++) {
348 for (int j = 0; j < 2; j++) {
349 u[i * 2 + j] = rmap->u[i + 1][j + 1];
350 v[i * 2 + j] = rmap->v[i + 1][j + 1];
354 ker[0] = (1.f - du) * (1.f - dv) * 16384;
355 ker[1] = du * (1.f - dv) * 16384;
356 ker[2] = (1.f - du) * dv * 16384;
357 ker[3] = du * dv * 16384;
361 * Calculate 1-dimensional cubic coefficients.
363 * @param t relative coordinate
364 * @param coeffs coefficients
366 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
368 const float tt = t * t;
369 const float ttt = t * t * t;
371 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
372 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
373 coeffs[2] = t + tt / 2.f - ttt / 2.f;
374 coeffs[3] = - t / 6.f + ttt / 6.f;
378 * Calculate kernel for bicubic interpolation.
380 * @param du horizontal relative coordinate
381 * @param dv vertical relative coordinate
382 * @param rmap calculated 4x4 window
383 * @param u u remap data
384 * @param v v remap data
385 * @param ker ker remap data
387 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
388 uint16_t *u, uint16_t *v, int16_t *ker)
393 calculate_bicubic_coeffs(du, du_coeffs);
394 calculate_bicubic_coeffs(dv, dv_coeffs);
396 for (int i = 0; i < 4; i++) {
397 for (int j = 0; j < 4; j++) {
398 u[i * 4 + j] = rmap->u[i][j];
399 v[i * 4 + j] = rmap->v[i][j];
400 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
406 * Calculate 1-dimensional lanczos coefficients.
408 * @param t relative coordinate
409 * @param coeffs coefficients
411 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
415 for (int i = 0; i < 4; i++) {
416 const float x = M_PI * (t - i + 1);
420 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
425 for (int i = 0; i < 4; i++) {
431 * Calculate kernel for lanczos interpolation.
433 * @param du horizontal relative coordinate
434 * @param dv vertical relative coordinate
435 * @param rmap calculated 4x4 window
436 * @param u u remap data
437 * @param v v remap data
438 * @param ker ker remap data
440 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
441 uint16_t *u, uint16_t *v, int16_t *ker)
446 calculate_lanczos_coeffs(du, du_coeffs);
447 calculate_lanczos_coeffs(dv, dv_coeffs);
449 for (int i = 0; i < 4; i++) {
450 for (int j = 0; j < 4; j++) {
451 u[i * 4 + j] = rmap->u[i][j];
452 v[i * 4 + j] = rmap->v[i][j];
453 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
459 * Modulo operation with only positive remainders.
464 * @return positive remainder of (a / b)
466 static inline int mod(int a, int b)
468 const int res = a % b;
477 * Convert char to corresponding direction.
478 * Used for cubemap options.
480 static int get_direction(char c)
501 * Convert char to corresponding rotation angle.
502 * Used for cubemap options.
504 static int get_rotation(char c)
521 * Convert char to corresponding rotation order.
523 static int get_rorder(char c)
541 * Prepare data for processing cubemap input format.
543 * @param ctx filter context
547 static int prepare_cube_in(AVFilterContext *ctx)
549 V360Context *s = ctx->priv;
551 for (int face = 0; face < NB_FACES; face++) {
552 const char c = s->in_forder[face];
556 av_log(ctx, AV_LOG_ERROR,
557 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
558 return AVERROR(EINVAL);
561 direction = get_direction(c);
562 if (direction == -1) {
563 av_log(ctx, AV_LOG_ERROR,
564 "Incorrect direction symbol '%c' in in_forder option.\n", c);
565 return AVERROR(EINVAL);
568 s->in_cubemap_face_order[direction] = face;
571 for (int face = 0; face < NB_FACES; face++) {
572 const char c = s->in_frot[face];
576 av_log(ctx, AV_LOG_ERROR,
577 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
578 return AVERROR(EINVAL);
581 rotation = get_rotation(c);
582 if (rotation == -1) {
583 av_log(ctx, AV_LOG_ERROR,
584 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
585 return AVERROR(EINVAL);
588 s->in_cubemap_face_rotation[face] = rotation;
595 * Prepare data for processing cubemap output format.
597 * @param ctx filter context
601 static int prepare_cube_out(AVFilterContext *ctx)
603 V360Context *s = ctx->priv;
605 for (int face = 0; face < NB_FACES; face++) {
606 const char c = s->out_forder[face];
610 av_log(ctx, AV_LOG_ERROR,
611 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
612 return AVERROR(EINVAL);
615 direction = get_direction(c);
616 if (direction == -1) {
617 av_log(ctx, AV_LOG_ERROR,
618 "Incorrect direction symbol '%c' in out_forder option.\n", c);
619 return AVERROR(EINVAL);
622 s->out_cubemap_direction_order[face] = direction;
625 for (int face = 0; face < NB_FACES; face++) {
626 const char c = s->out_frot[face];
630 av_log(ctx, AV_LOG_ERROR,
631 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
632 return AVERROR(EINVAL);
635 rotation = get_rotation(c);
636 if (rotation == -1) {
637 av_log(ctx, AV_LOG_ERROR,
638 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
639 return AVERROR(EINVAL);
642 s->out_cubemap_face_rotation[face] = rotation;
648 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
674 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
705 static void normalize_vector(float *vec)
707 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
715 * Calculate 3D coordinates on sphere for corresponding cubemap position.
716 * Common operation for every cubemap.
718 * @param s filter context
719 * @param uf horizontal cubemap coordinate [0, 1)
720 * @param vf vertical cubemap coordinate [0, 1)
721 * @param face face of cubemap
722 * @param vec coordinates on sphere
724 static void cube_to_xyz(const V360Context *s,
725 float uf, float vf, int face,
728 const int direction = s->out_cubemap_direction_order[face];
731 uf /= (1.f - s->out_pad);
732 vf /= (1.f - s->out_pad);
734 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
775 normalize_vector(vec);
779 * Calculate cubemap position for corresponding 3D coordinates on sphere.
780 * Common operation for every cubemap.
782 * @param s filter context
783 * @param vec coordinated on sphere
784 * @param uf horizontal cubemap coordinate [0, 1)
785 * @param vf vertical cubemap coordinate [0, 1)
786 * @param direction direction of view
788 static void xyz_to_cube(const V360Context *s,
790 float *uf, float *vf, int *direction)
792 const float phi = atan2f(vec[0], -vec[2]);
793 const float theta = asinf(-vec[1]);
794 float phi_norm, theta_threshold;
797 if (phi >= -M_PI_4 && phi < M_PI_4) {
800 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
802 phi_norm = phi + M_PI_2;
803 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
805 phi_norm = phi - M_PI_2;
808 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
811 theta_threshold = atanf(cosf(phi_norm));
812 if (theta > theta_threshold) {
814 } else if (theta < -theta_threshold) {
818 switch (*direction) {
820 *uf = vec[2] / vec[0];
821 *vf = -vec[1] / vec[0];
824 *uf = vec[2] / vec[0];
825 *vf = vec[1] / vec[0];
828 *uf = vec[0] / vec[1];
829 *vf = -vec[2] / vec[1];
832 *uf = -vec[0] / vec[1];
833 *vf = -vec[2] / vec[1];
836 *uf = -vec[0] / vec[2];
837 *vf = vec[1] / vec[2];
840 *uf = -vec[0] / vec[2];
841 *vf = -vec[1] / vec[2];
847 face = s->in_cubemap_face_order[*direction];
848 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
850 (*uf) *= s->input_mirror_modifier[0];
851 (*vf) *= s->input_mirror_modifier[1];
855 * Find position on another cube face in case of overflow/underflow.
856 * Used for calculation of interpolation window.
858 * @param s filter context
859 * @param uf horizontal cubemap coordinate
860 * @param vf vertical cubemap coordinate
861 * @param direction direction of view
862 * @param new_uf new horizontal cubemap coordinate
863 * @param new_vf new vertical cubemap coordinate
864 * @param face face position on cubemap
866 static void process_cube_coordinates(const V360Context *s,
867 float uf, float vf, int direction,
868 float *new_uf, float *new_vf, int *face)
871 * Cubemap orientation
878 * +-------+-------+-------+-------+ ^ e |
880 * | left | front | right | back | | g |
881 * +-------+-------+-------+-------+ v h v
887 *face = s->in_cubemap_face_order[direction];
888 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
890 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
891 // There are no pixels to use in this case
894 } else if (uf < -1.f) {
930 } else if (uf >= 1.f) {
966 } else if (vf < -1.f) {
1002 } else if (vf >= 1.f) {
1004 switch (direction) {
1044 *face = s->in_cubemap_face_order[direction];
1045 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1049 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1051 * @param s filter context
1052 * @param i horizontal position on frame [0, width)
1053 * @param j vertical position on frame [0, height)
1054 * @param width frame width
1055 * @param height frame height
1056 * @param vec coordinates on sphere
1058 static void cube3x2_to_xyz(const V360Context *s,
1059 int i, int j, int width, int height,
1062 const float ew = width / 3.f;
1063 const float eh = height / 2.f;
1065 const int u_face = floorf(i / ew);
1066 const int v_face = floorf(j / eh);
1067 const int face = u_face + 3 * v_face;
1069 const int u_shift = ceilf(ew * u_face);
1070 const int v_shift = ceilf(eh * v_face);
1071 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1072 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1074 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1075 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1077 cube_to_xyz(s, uf, vf, face, vec);
1081 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1083 * @param s filter context
1084 * @param vec coordinates on sphere
1085 * @param width frame width
1086 * @param height frame height
1087 * @param us horizontal coordinates for interpolation window
1088 * @param vs vertical coordinates for interpolation window
1089 * @param du horizontal relative coordinate
1090 * @param dv vertical relative coordinate
1092 static void xyz_to_cube3x2(const V360Context *s,
1093 const float *vec, int width, int height,
1094 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1096 const float ew = width / 3.f;
1097 const float eh = height / 2.f;
1101 int direction, face;
1104 xyz_to_cube(s, vec, &uf, &vf, &direction);
1106 uf *= (1.f - s->in_pad);
1107 vf *= (1.f - s->in_pad);
1109 face = s->in_cubemap_face_order[direction];
1112 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1113 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1115 uf = 0.5f * ewi * (uf + 1.f);
1116 vf = 0.5f * ehi * (vf + 1.f);
1124 for (int i = -1; i < 3; i++) {
1125 for (int j = -1; j < 3; j++) {
1126 int new_ui = ui + j;
1127 int new_vi = vi + i;
1128 int u_shift, v_shift;
1129 int new_ewi, new_ehi;
1131 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1132 face = s->in_cubemap_face_order[direction];
1136 u_shift = ceilf(ew * u_face);
1137 v_shift = ceilf(eh * v_face);
1139 uf = 2.f * new_ui / ewi - 1.f;
1140 vf = 2.f * new_vi / ehi - 1.f;
1142 uf /= (1.f - s->in_pad);
1143 vf /= (1.f - s->in_pad);
1145 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1147 uf *= (1.f - s->in_pad);
1148 vf *= (1.f - s->in_pad);
1152 u_shift = ceilf(ew * u_face);
1153 v_shift = ceilf(eh * v_face);
1154 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1155 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1157 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1158 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1161 us[i + 1][j + 1] = u_shift + new_ui;
1162 vs[i + 1][j + 1] = v_shift + new_vi;
1168 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1170 * @param s filter context
1171 * @param i horizontal position on frame [0, width)
1172 * @param j vertical position on frame [0, height)
1173 * @param width frame width
1174 * @param height frame height
1175 * @param vec coordinates on sphere
1177 static void cube1x6_to_xyz(const V360Context *s,
1178 int i, int j, int width, int height,
1181 const float ew = width;
1182 const float eh = height / 6.f;
1184 const int face = floorf(j / eh);
1186 const int v_shift = ceilf(eh * face);
1187 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1189 const float uf = 2.f * i / ew - 1.f;
1190 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1192 cube_to_xyz(s, uf, vf, face, vec);
1196 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1198 * @param s filter context
1199 * @param i horizontal position on frame [0, width)
1200 * @param j vertical position on frame [0, height)
1201 * @param width frame width
1202 * @param height frame height
1203 * @param vec coordinates on sphere
1205 static void cube6x1_to_xyz(const V360Context *s,
1206 int i, int j, int width, int height,
1209 const float ew = width / 6.f;
1210 const float eh = height;
1212 const int face = floorf(i / ew);
1214 const int u_shift = ceilf(ew * face);
1215 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1217 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1218 const float vf = 2.f * j / eh - 1.f;
1220 cube_to_xyz(s, uf, vf, face, vec);
1224 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1226 * @param s filter context
1227 * @param vec coordinates on sphere
1228 * @param width frame width
1229 * @param height frame height
1230 * @param us horizontal coordinates for interpolation window
1231 * @param vs vertical coordinates for interpolation window
1232 * @param du horizontal relative coordinate
1233 * @param dv vertical relative coordinate
1235 static void xyz_to_cube1x6(const V360Context *s,
1236 const float *vec, int width, int height,
1237 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1239 const float eh = height / 6.f;
1240 const int ewi = width;
1244 int direction, face;
1246 xyz_to_cube(s, vec, &uf, &vf, &direction);
1248 uf *= (1.f - s->in_pad);
1249 vf *= (1.f - s->in_pad);
1251 face = s->in_cubemap_face_order[direction];
1252 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1254 uf = 0.5f * ewi * (uf + 1.f);
1255 vf = 0.5f * ehi * (vf + 1.f);
1263 for (int i = -1; i < 3; i++) {
1264 for (int j = -1; j < 3; j++) {
1265 int new_ui = ui + j;
1266 int new_vi = vi + i;
1270 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1271 face = s->in_cubemap_face_order[direction];
1273 v_shift = ceilf(eh * face);
1275 uf = 2.f * new_ui / ewi - 1.f;
1276 vf = 2.f * new_vi / ehi - 1.f;
1278 uf /= (1.f - s->in_pad);
1279 vf /= (1.f - s->in_pad);
1281 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1283 uf *= (1.f - s->in_pad);
1284 vf *= (1.f - s->in_pad);
1286 v_shift = ceilf(eh * face);
1287 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1289 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1290 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1293 us[i + 1][j + 1] = new_ui;
1294 vs[i + 1][j + 1] = v_shift + new_vi;
1300 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1302 * @param s filter context
1303 * @param vec coordinates on sphere
1304 * @param width frame width
1305 * @param height frame height
1306 * @param us horizontal coordinates for interpolation window
1307 * @param vs vertical coordinates for interpolation window
1308 * @param du horizontal relative coordinate
1309 * @param dv vertical relative coordinate
1311 static void xyz_to_cube6x1(const V360Context *s,
1312 const float *vec, int width, int height,
1313 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1315 const float ew = width / 6.f;
1316 const int ehi = height;
1320 int direction, face;
1322 xyz_to_cube(s, vec, &uf, &vf, &direction);
1324 uf *= (1.f - s->in_pad);
1325 vf *= (1.f - s->in_pad);
1327 face = s->in_cubemap_face_order[direction];
1328 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1330 uf = 0.5f * ewi * (uf + 1.f);
1331 vf = 0.5f * ehi * (vf + 1.f);
1339 for (int i = -1; i < 3; i++) {
1340 for (int j = -1; j < 3; j++) {
1341 int new_ui = ui + j;
1342 int new_vi = vi + i;
1346 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1347 face = s->in_cubemap_face_order[direction];
1349 u_shift = ceilf(ew * face);
1351 uf = 2.f * new_ui / ewi - 1.f;
1352 vf = 2.f * new_vi / ehi - 1.f;
1354 uf /= (1.f - s->in_pad);
1355 vf /= (1.f - s->in_pad);
1357 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1359 uf *= (1.f - s->in_pad);
1360 vf *= (1.f - s->in_pad);
1362 u_shift = ceilf(ew * face);
1363 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1365 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1366 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1369 us[i + 1][j + 1] = u_shift + new_ui;
1370 vs[i + 1][j + 1] = new_vi;
1376 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1378 * @param s filter context
1379 * @param i horizontal position on frame [0, width)
1380 * @param j vertical position on frame [0, height)
1381 * @param width frame width
1382 * @param height frame height
1383 * @param vec coordinates on sphere
1385 static void equirect_to_xyz(const V360Context *s,
1386 int i, int j, int width, int height,
1389 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1390 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1392 const float sin_phi = sinf(phi);
1393 const float cos_phi = cosf(phi);
1394 const float sin_theta = sinf(theta);
1395 const float cos_theta = cosf(theta);
1397 vec[0] = cos_theta * sin_phi;
1398 vec[1] = -sin_theta;
1399 vec[2] = -cos_theta * cos_phi;
1403 * Prepare data for processing stereographic output format.
1405 * @param ctx filter context
1407 * @return error code
1409 static int prepare_stereographic_out(AVFilterContext *ctx)
1411 V360Context *s = ctx->priv;
1413 const float h_angle = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1414 const float v_angle = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1416 s->flat_range[0] = h_angle;
1417 s->flat_range[1] = v_angle;
1423 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1425 * @param s filter context
1426 * @param i horizontal position on frame [0, width)
1427 * @param j vertical position on frame [0, height)
1428 * @param width frame width
1429 * @param height frame height
1430 * @param vec coordinates on sphere
1432 static void stereographic_to_xyz(const V360Context *s,
1433 int i, int j, int width, int height,
1436 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1437 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1438 const float xy = x * x + y * y;
1440 vec[0] = 2.f * x / (1.f + xy);
1441 vec[1] = (-1.f + xy) / (1.f + xy);
1442 vec[2] = 2.f * y / (1.f + xy);
1444 normalize_vector(vec);
1448 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1450 * @param s filter context
1451 * @param vec coordinates on sphere
1452 * @param width frame width
1453 * @param height frame height
1454 * @param us horizontal coordinates for interpolation window
1455 * @param vs vertical coordinates for interpolation window
1456 * @param du horizontal relative coordinate
1457 * @param dv vertical relative coordinate
1459 static void xyz_to_stereographic(const V360Context *s,
1460 const float *vec, int width, int height,
1461 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1463 const float x = av_clipf(vec[0] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[0];
1464 const float y = av_clipf(vec[2] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[1];
1468 uf = (x + 1.f) * width / 2.f;
1469 vf = (y + 1.f) * height / 2.f;
1476 for (int i = -1; i < 3; i++) {
1477 for (int j = -1; j < 3; j++) {
1478 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1479 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1485 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1487 * @param s filter context
1488 * @param vec coordinates on sphere
1489 * @param width frame width
1490 * @param height frame height
1491 * @param us horizontal coordinates for interpolation window
1492 * @param vs vertical coordinates for interpolation window
1493 * @param du horizontal relative coordinate
1494 * @param dv vertical relative coordinate
1496 static void xyz_to_equirect(const V360Context *s,
1497 const float *vec, int width, int height,
1498 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1500 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1501 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1505 uf = (phi / M_PI + 1.f) * width / 2.f;
1506 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1513 for (int i = -1; i < 3; i++) {
1514 for (int j = -1; j < 3; j++) {
1515 us[i + 1][j + 1] = mod(ui + j, width);
1516 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1522 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1524 * @param s filter context
1525 * @param vec coordinates on sphere
1526 * @param width frame width
1527 * @param height frame height
1528 * @param us horizontal coordinates for interpolation window
1529 * @param vs vertical coordinates for interpolation window
1530 * @param du horizontal relative coordinate
1531 * @param dv vertical relative coordinate
1533 static void xyz_to_mercator(const V360Context *s,
1534 const float *vec, int width, int height,
1535 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1537 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1538 const float theta = 0.5f * asinhf(vec[1] / sqrtf(1.f - vec[1] * vec[1])) * s->input_mirror_modifier[1];
1542 uf = (phi / M_PI + 1.f) * width / 2.f;
1543 vf = (theta / M_PI + 1.f) * height / 2.f;
1550 for (int i = -1; i < 3; i++) {
1551 for (int j = -1; j < 3; j++) {
1552 us[i + 1][j + 1] = mod(ui + j, width);
1553 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1559 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1561 * @param s filter context
1562 * @param i horizontal position on frame [0, width)
1563 * @param j vertical position on frame [0, height)
1564 * @param width frame width
1565 * @param height frame height
1566 * @param vec coordinates on sphere
1568 static void mercator_to_xyz(const V360Context *s,
1569 int i, int j, int width, int height,
1572 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1573 const float theta = atanf(sinhf(((2.f * j) / height - 1.f) * 2.f * M_PI));
1575 const float sin_phi = sinf(phi);
1576 const float cos_phi = cosf(phi);
1577 const float sin_theta = sinf(theta);
1578 const float cos_theta = cosf(theta);
1580 vec[0] = cos_theta * sin_phi;
1581 vec[1] = -sin_theta;
1582 vec[2] = -cos_theta * cos_phi;
1586 * Prepare data for processing equi-angular cubemap input format.
1588 * @param ctx filter context
1590 * @return error code
1592 static int prepare_eac_in(AVFilterContext *ctx)
1594 V360Context *s = ctx->priv;
1596 if (s->ih_flip && s->iv_flip) {
1597 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
1598 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
1599 s->in_cubemap_face_order[UP] = TOP_LEFT;
1600 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
1601 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1602 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1603 } else if (s->ih_flip) {
1604 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
1605 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
1606 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
1607 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
1608 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1609 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1610 } else if (s->iv_flip) {
1611 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
1612 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
1613 s->in_cubemap_face_order[UP] = TOP_RIGHT;
1614 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
1615 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1616 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1618 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1619 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1620 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1621 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1622 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1623 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1627 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
1628 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
1629 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
1630 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
1631 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
1632 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
1634 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1635 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1636 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1637 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1638 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1639 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1646 * Prepare data for processing equi-angular cubemap output format.
1648 * @param ctx filter context
1650 * @return error code
1652 static int prepare_eac_out(AVFilterContext *ctx)
1654 V360Context *s = ctx->priv;
1656 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1657 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1658 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1659 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1660 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1661 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1663 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1664 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1665 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1666 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1667 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1668 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1674 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1676 * @param s filter context
1677 * @param i horizontal position on frame [0, width)
1678 * @param j vertical position on frame [0, height)
1679 * @param width frame width
1680 * @param height frame height
1681 * @param vec coordinates on sphere
1683 static void eac_to_xyz(const V360Context *s,
1684 int i, int j, int width, int height,
1687 const float pixel_pad = 2;
1688 const float u_pad = pixel_pad / width;
1689 const float v_pad = pixel_pad / height;
1691 int u_face, v_face, face;
1693 float l_x, l_y, l_z;
1695 float uf = (float)i / width;
1696 float vf = (float)j / height;
1698 // EAC has 2-pixel padding on faces except between faces on the same row
1699 // Padding pixels seems not to be stretched with tangent as regular pixels
1700 // Formulas below approximate original padding as close as I could get experimentally
1702 // Horizontal padding
1703 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1707 } else if (uf >= 3.f) {
1711 u_face = floorf(uf);
1712 uf = fmodf(uf, 1.f) - 0.5f;
1716 v_face = floorf(vf * 2.f);
1717 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1719 if (uf >= -0.5f && uf < 0.5f) {
1720 uf = tanf(M_PI_2 * uf);
1724 if (vf >= -0.5f && vf < 0.5f) {
1725 vf = tanf(M_PI_2 * vf);
1730 face = u_face + 3 * v_face;
1771 normalize_vector(vec);
1775 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
1777 * @param s filter context
1778 * @param vec coordinates on sphere
1779 * @param width frame width
1780 * @param height frame height
1781 * @param us horizontal coordinates for interpolation window
1782 * @param vs vertical coordinates for interpolation window
1783 * @param du horizontal relative coordinate
1784 * @param dv vertical relative coordinate
1786 static void xyz_to_eac(const V360Context *s,
1787 const float *vec, int width, int height,
1788 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1790 const float pixel_pad = 2;
1791 const float u_pad = pixel_pad / width;
1792 const float v_pad = pixel_pad / height;
1796 int direction, face;
1799 xyz_to_cube(s, vec, &uf, &vf, &direction);
1801 face = s->in_cubemap_face_order[direction];
1805 uf = M_2_PI * atanf(uf) + 0.5f;
1806 vf = M_2_PI * atanf(vf) + 0.5f;
1808 // These formulas are inversed from eac_to_xyz ones
1809 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
1810 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
1821 for (int i = -1; i < 3; i++) {
1822 for (int j = -1; j < 3; j++) {
1823 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1824 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1830 * Prepare data for processing flat output format.
1832 * @param ctx filter context
1834 * @return error code
1836 static int prepare_flat_out(AVFilterContext *ctx)
1838 V360Context *s = ctx->priv;
1840 const float h_angle = 0.5f * s->h_fov * M_PI / 180.f;
1841 const float v_angle = 0.5f * s->v_fov * M_PI / 180.f;
1843 s->flat_range[0] = tanf(h_angle);
1844 s->flat_range[1] = tanf(v_angle);
1845 s->flat_range[2] = -1.f;
1851 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
1853 * @param s filter context
1854 * @param i horizontal position on frame [0, width)
1855 * @param j vertical position on frame [0, height)
1856 * @param width frame width
1857 * @param height frame height
1858 * @param vec coordinates on sphere
1860 static void flat_to_xyz(const V360Context *s,
1861 int i, int j, int width, int height,
1864 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
1865 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
1866 const float l_z = s->flat_range[2];
1872 normalize_vector(vec);
1876 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
1878 * @param s filter context
1879 * @param i horizontal position on frame [0, width)
1880 * @param j vertical position on frame [0, height)
1881 * @param width frame width
1882 * @param height frame height
1883 * @param vec coordinates on sphere
1885 static void dfisheye_to_xyz(const V360Context *s,
1886 int i, int j, int width, int height,
1889 const float scale = 1.f + s->out_pad;
1891 const float ew = width / 2.f;
1892 const float eh = height;
1894 const int ei = i >= ew ? i - ew : i;
1895 const float m = i >= ew ? -1.f : 1.f;
1897 const float uf = ((2.f * ei) / ew - 1.f) * scale;
1898 const float vf = ((2.f * j) / eh - 1.f) * scale;
1900 const float phi = M_PI + atan2f(vf, uf * m);
1901 const float theta = m * M_PI_2 * (1.f - hypotf(uf, vf));
1903 const float sin_phi = sinf(phi);
1904 const float cos_phi = cosf(phi);
1905 const float sin_theta = sinf(theta);
1906 const float cos_theta = cosf(theta);
1908 vec[0] = cos_theta * cos_phi;
1909 vec[1] = cos_theta * sin_phi;
1912 normalize_vector(vec);
1916 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
1918 * @param s filter context
1919 * @param vec coordinates on sphere
1920 * @param width frame width
1921 * @param height frame height
1922 * @param us horizontal coordinates for interpolation window
1923 * @param vs vertical coordinates for interpolation window
1924 * @param du horizontal relative coordinate
1925 * @param dv vertical relative coordinate
1927 static void xyz_to_dfisheye(const V360Context *s,
1928 const float *vec, int width, int height,
1929 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1931 const float scale = 1.f - s->in_pad;
1933 const float ew = width / 2.f;
1934 const float eh = height;
1936 const float phi = atan2f(-vec[1], -vec[0]) * s->input_mirror_modifier[0];
1937 const float theta = acosf(fabsf(vec[2])) / M_PI * s->input_mirror_modifier[1];
1939 float uf = (theta * cosf(phi) * scale + 0.5f) * ew;
1940 float vf = (theta * sinf(phi) * scale + 0.5f) * eh;
1948 u_shift = ceilf(ew);
1958 for (int i = -1; i < 3; i++) {
1959 for (int j = -1; j < 3; j++) {
1960 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
1961 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
1967 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
1969 * @param s filter context
1970 * @param i horizontal position on frame [0, width)
1971 * @param j vertical position on frame [0, height)
1972 * @param width frame width
1973 * @param height frame height
1974 * @param vec coordinates on sphere
1976 static void barrel_to_xyz(const V360Context *s,
1977 int i, int j, int width, int height,
1980 const float scale = 0.99f;
1981 float l_x, l_y, l_z;
1983 if (i < 4 * width / 5) {
1984 const float theta_range = M_PI_4;
1986 const int ew = 4 * width / 5;
1987 const int eh = height;
1989 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
1990 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
1992 const float sin_phi = sinf(phi);
1993 const float cos_phi = cosf(phi);
1994 const float sin_theta = sinf(theta);
1995 const float cos_theta = cosf(theta);
1997 l_x = cos_theta * sin_phi;
1999 l_z = -cos_theta * cos_phi;
2001 const int ew = width / 5;
2002 const int eh = height / 2;
2007 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2008 vf = 2.f * (j ) / eh - 1.f;
2017 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2018 vf = 2.f * (j - eh) / eh - 1.f;
2033 normalize_vector(vec);
2037 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2039 * @param s filter context
2040 * @param vec coordinates on sphere
2041 * @param width frame width
2042 * @param height frame height
2043 * @param us horizontal coordinates for interpolation window
2044 * @param vs vertical coordinates for interpolation window
2045 * @param du horizontal relative coordinate
2046 * @param dv vertical relative coordinate
2048 static void xyz_to_barrel(const V360Context *s,
2049 const float *vec, int width, int height,
2050 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2052 const float scale = 0.99f;
2054 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2055 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2056 const float theta_range = M_PI_4;
2059 int u_shift, v_shift;
2063 if (theta > -theta_range && theta < theta_range) {
2067 u_shift = s->ih_flip ? width / 5 : 0;
2070 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2071 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2076 u_shift = s->ih_flip ? 0 : 4 * ew;
2078 if (theta < 0.f) { // UP
2079 uf = vec[0] / vec[1];
2080 vf = -vec[2] / vec[1];
2083 uf = -vec[0] / vec[1];
2084 vf = -vec[2] / vec[1];
2088 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2089 vf *= s->input_mirror_modifier[1];
2091 uf = 0.5f * ew * (uf * scale + 1.f);
2092 vf = 0.5f * eh * (vf * scale + 1.f);
2101 for (int i = -1; i < 3; i++) {
2102 for (int j = -1; j < 3; j++) {
2103 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2104 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2109 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2111 for (int i = 0; i < 3; i++) {
2112 for (int j = 0; j < 3; j++) {
2115 for (int k = 0; k < 3; k++)
2116 sum += a[i][k] * b[k][j];
2124 * Calculate rotation matrix for yaw/pitch/roll angles.
2126 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2127 float rot_mat[3][3],
2128 const int rotation_order[3])
2130 const float yaw_rad = yaw * M_PI / 180.f;
2131 const float pitch_rad = pitch * M_PI / 180.f;
2132 const float roll_rad = roll * M_PI / 180.f;
2134 const float sin_yaw = sinf(-yaw_rad);
2135 const float cos_yaw = cosf(-yaw_rad);
2136 const float sin_pitch = sinf(pitch_rad);
2137 const float cos_pitch = cosf(pitch_rad);
2138 const float sin_roll = sinf(roll_rad);
2139 const float cos_roll = cosf(roll_rad);
2144 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2145 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2146 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2148 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2149 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2150 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2152 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2153 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2154 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2156 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2157 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2161 * Rotate vector with given rotation matrix.
2163 * @param rot_mat rotation matrix
2166 static inline void rotate(const float rot_mat[3][3],
2169 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2170 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2171 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2178 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2181 modifier[0] = h_flip ? -1.f : 1.f;
2182 modifier[1] = v_flip ? -1.f : 1.f;
2183 modifier[2] = d_flip ? -1.f : 1.f;
2186 static inline void mirror(const float *modifier, float *vec)
2188 vec[0] *= modifier[0];
2189 vec[1] *= modifier[1];
2190 vec[2] *= modifier[2];
2193 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2195 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2196 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2197 if (!s->u[p] || !s->v[p])
2198 return AVERROR(ENOMEM);
2200 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2202 return AVERROR(ENOMEM);
2208 static void fov_from_dfov(V360Context *s, float w, float h)
2210 const float da = tanf(0.5 * FFMIN(s->d_fov, 359.f) * M_PI / 180.f);
2211 const float d = hypotf(w, h);
2213 s->h_fov = atan2f(da * w, d) * 360.f / M_PI;
2214 s->v_fov = atan2f(da * h, d) * 360.f / M_PI;
2222 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2224 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2225 outw[0] = outw[3] = w;
2226 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2227 outh[0] = outh[3] = h;
2230 // Calculate remap data
2231 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2233 V360Context *s = ctx->priv;
2235 for (int p = 0; p < s->nb_allocated; p++) {
2236 const int width = s->pr_width[p];
2237 const int uv_linesize = s->uv_linesize[p];
2238 const int height = s->pr_height[p];
2239 const int in_width = s->inplanewidth[p];
2240 const int in_height = s->inplaneheight[p];
2241 const int slice_start = (height * jobnr ) / nb_jobs;
2242 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
2247 for (int j = slice_start; j < slice_end; j++) {
2248 for (int i = 0; i < width; i++) {
2249 uint16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2250 uint16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2251 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2253 if (s->out_transpose)
2254 s->out_transform(s, j, i, height, width, vec);
2256 s->out_transform(s, i, j, width, height, vec);
2257 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2258 rotate(s->rot_mat, vec);
2259 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2260 normalize_vector(vec);
2261 mirror(s->output_mirror_modifier, vec);
2262 if (s->in_transpose)
2263 s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2265 s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2266 av_assert1(!isnan(du) && !isnan(dv));
2267 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2275 static int config_output(AVFilterLink *outlink)
2277 AVFilterContext *ctx = outlink->src;
2278 AVFilterLink *inlink = ctx->inputs[0];
2279 V360Context *s = ctx->priv;
2280 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2281 const int depth = desc->comp[0].depth;
2286 int in_offset_h, in_offset_w;
2287 int out_offset_h, out_offset_w;
2289 int (*prepare_out)(AVFilterContext *ctx);
2291 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2292 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2294 switch (s->interp) {
2296 s->calculate_kernel = nearest_kernel;
2297 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2299 sizeof_uv = sizeof(uint16_t) * s->elements;
2303 s->calculate_kernel = bilinear_kernel;
2304 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2305 s->elements = 2 * 2;
2306 sizeof_uv = sizeof(uint16_t) * s->elements;
2307 sizeof_ker = sizeof(uint16_t) * s->elements;
2310 s->calculate_kernel = bicubic_kernel;
2311 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2312 s->elements = 4 * 4;
2313 sizeof_uv = sizeof(uint16_t) * s->elements;
2314 sizeof_ker = sizeof(uint16_t) * s->elements;
2317 s->calculate_kernel = lanczos_kernel;
2318 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2319 s->elements = 4 * 4;
2320 sizeof_uv = sizeof(uint16_t) * s->elements;
2321 sizeof_ker = sizeof(uint16_t) * s->elements;
2327 ff_v360_init(s, depth);
2329 for (int order = 0; order < NB_RORDERS; order++) {
2330 const char c = s->rorder[order];
2334 av_log(ctx, AV_LOG_ERROR,
2335 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2336 return AVERROR(EINVAL);
2339 rorder = get_rorder(c);
2341 av_log(ctx, AV_LOG_ERROR,
2342 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2343 return AVERROR(EINVAL);
2346 s->rotation_order[order] = rorder;
2349 switch (s->in_stereo) {
2353 in_offset_w = in_offset_h = 0;
2371 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
2372 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
2375 case EQUIRECTANGULAR:
2376 s->in_transform = xyz_to_equirect;
2382 s->in_transform = xyz_to_cube3x2;
2383 err = prepare_cube_in(ctx);
2388 s->in_transform = xyz_to_cube1x6;
2389 err = prepare_cube_in(ctx);
2394 s->in_transform = xyz_to_cube6x1;
2395 err = prepare_cube_in(ctx);
2400 s->in_transform = xyz_to_eac;
2401 err = prepare_eac_in(ctx);
2406 av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n");
2407 return AVERROR(EINVAL);
2409 s->in_transform = xyz_to_dfisheye;
2415 s->in_transform = xyz_to_barrel;
2421 s->in_transform = xyz_to_stereographic;
2427 s->in_transform = xyz_to_mercator;
2433 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2442 case EQUIRECTANGULAR:
2443 s->out_transform = equirect_to_xyz;
2449 s->out_transform = cube3x2_to_xyz;
2450 prepare_out = prepare_cube_out;
2451 w = roundf(wf / 4.f * 3.f);
2455 s->out_transform = cube1x6_to_xyz;
2456 prepare_out = prepare_cube_out;
2457 w = roundf(wf / 4.f);
2458 h = roundf(hf * 3.f);
2461 s->out_transform = cube6x1_to_xyz;
2462 prepare_out = prepare_cube_out;
2463 w = roundf(wf / 2.f * 3.f);
2464 h = roundf(hf / 2.f);
2467 s->out_transform = eac_to_xyz;
2468 prepare_out = prepare_eac_out;
2470 h = roundf(hf / 8.f * 9.f);
2473 s->out_transform = flat_to_xyz;
2474 prepare_out = prepare_flat_out;
2479 s->out_transform = dfisheye_to_xyz;
2485 s->out_transform = barrel_to_xyz;
2487 w = roundf(wf / 4.f * 5.f);
2491 s->out_transform = stereographic_to_xyz;
2492 prepare_out = prepare_stereographic_out;
2494 h = roundf(hf * 2.f);
2497 s->out_transform = mercator_to_xyz;
2503 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2507 // Override resolution with user values if specified
2508 if (s->width > 0 && s->height > 0) {
2511 } else if (s->width > 0 || s->height > 0) {
2512 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2513 return AVERROR(EINVAL);
2515 if (s->out_transpose)
2518 if (s->in_transpose)
2523 fov_from_dfov(s, w, h);
2526 err = prepare_out(ctx);
2531 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
2533 switch (s->out_stereo) {
2535 out_offset_w = out_offset_h = 0;
2551 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
2552 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
2554 for (int i = 0; i < 4; i++)
2555 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
2560 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2562 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2563 s->nb_allocated = 1;
2564 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2566 s->nb_allocated = 2;
2568 s->map[1] = s->map[2] = 1;
2572 for (int i = 0; i < s->nb_allocated; i++)
2573 allocate_plane(s, sizeof_uv, sizeof_ker, i);
2575 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
2576 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
2578 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2583 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2585 AVFilterContext *ctx = inlink->dst;
2586 AVFilterLink *outlink = ctx->outputs[0];
2587 V360Context *s = ctx->priv;
2591 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2594 return AVERROR(ENOMEM);
2596 av_frame_copy_props(out, in);
2601 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2604 return ff_filter_frame(outlink, out);
2607 static av_cold void uninit(AVFilterContext *ctx)
2609 V360Context *s = ctx->priv;
2611 for (int p = 0; p < s->nb_allocated; p++) {
2614 av_freep(&s->ker[p]);
2618 static const AVFilterPad inputs[] = {
2621 .type = AVMEDIA_TYPE_VIDEO,
2622 .filter_frame = filter_frame,
2627 static const AVFilterPad outputs[] = {
2630 .type = AVMEDIA_TYPE_VIDEO,
2631 .config_props = config_output,
2636 AVFilter ff_vf_v360 = {
2638 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
2639 .priv_size = sizeof(V360Context),
2641 .query_formats = query_formats,
2644 .priv_class = &v360_class,
2645 .flags = AVFILTER_FLAG_SLICE_THREADS,