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 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
69 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
70 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
71 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
72 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
73 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
74 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
75 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
76 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
77 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
78 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
79 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
80 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
81 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
82 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
83 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
84 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
85 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
86 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
87 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
88 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
89 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
90 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
91 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
92 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
93 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
94 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
95 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
96 { "in_pad", "input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
97 { "out_pad", "output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
98 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
99 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
100 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
101 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
102 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 180.f, FLAGS, "h_fov"},
103 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 180.f, FLAGS, "v_fov"},
104 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
105 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
106 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
107 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
108 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
109 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
110 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
114 AVFILTER_DEFINE_CLASS(v360);
116 static int query_formats(AVFilterContext *ctx)
118 static const enum AVPixelFormat pix_fmts[] = {
120 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
121 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
122 AV_PIX_FMT_YUVA444P16,
125 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
126 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
127 AV_PIX_FMT_YUVA422P16,
130 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
131 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
134 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
135 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
139 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
140 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
141 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
144 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
145 AV_PIX_FMT_YUV440P12,
148 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
149 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
150 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
153 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
154 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
155 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
164 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
165 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
166 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
169 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
170 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
173 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
174 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
175 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
180 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
182 return AVERROR(ENOMEM);
183 return ff_set_common_formats(ctx, fmts_list);
186 #define DEFINE_REMAP1_LINE(bits, div) \
187 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
188 ptrdiff_t in_linesize, \
189 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
191 const uint##bits##_t *s = (const uint##bits##_t *)src; \
192 uint##bits##_t *d = (uint##bits##_t *)dst; \
194 in_linesize /= div; \
196 for (int x = 0; x < width; x++) \
197 d[x] = s[v[x] * in_linesize + u[x]]; \
200 DEFINE_REMAP1_LINE( 8, 1)
201 DEFINE_REMAP1_LINE(16, 2)
203 typedef struct XYRemap {
210 * Generate remapping function with a given window size and pixel depth.
212 * @param ws size of interpolation window
213 * @param bits number of bits per pixel
215 #define DEFINE_REMAP(ws, bits) \
216 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
218 ThreadData *td = (ThreadData*)arg; \
219 const V360Context *s = ctx->priv; \
220 const AVFrame *in = td->in; \
221 AVFrame *out = td->out; \
223 for (int plane = 0; plane < s->nb_planes; plane++) { \
224 const int in_linesize = in->linesize[plane]; \
225 const int out_linesize = out->linesize[plane]; \
226 const int uv_linesize = s->uv_linesize[plane]; \
227 const uint8_t *src = in->data[plane]; \
228 uint8_t *dst = out->data[plane]; \
229 const int width = s->planewidth[plane]; \
230 const int height = s->planeheight[plane]; \
232 const int slice_start = (height * jobnr ) / nb_jobs; \
233 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
235 for (int y = slice_start; y < slice_end; y++) { \
236 const unsigned map = s->map[plane]; \
237 const uint16_t *u = s->u[map] + y * uv_linesize * ws * ws; \
238 const uint16_t *v = s->v[map] + y * uv_linesize * ws * ws; \
239 const int16_t *ker = s->ker[map] + y * uv_linesize * ws * ws; \
241 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
255 #define DEFINE_REMAP_LINE(ws, bits, div) \
256 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
257 ptrdiff_t in_linesize, \
258 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
260 const uint##bits##_t *s = (const uint##bits##_t *)src; \
261 uint##bits##_t *d = (uint##bits##_t *)dst; \
263 in_linesize /= div; \
265 for (int x = 0; x < width; x++) { \
266 const uint16_t *uu = u + x * ws * ws; \
267 const uint16_t *vv = v + x * ws * ws; \
268 const int16_t *kker = ker + x * ws * ws; \
271 for (int i = 0; i < ws; i++) { \
272 for (int j = 0; j < ws; j++) { \
273 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
277 d[x] = av_clip_uint##bits(tmp >> 14); \
281 DEFINE_REMAP_LINE(2, 8, 1)
282 DEFINE_REMAP_LINE(4, 8, 1)
283 DEFINE_REMAP_LINE(2, 16, 2)
284 DEFINE_REMAP_LINE(4, 16, 2)
286 void ff_v360_init(V360Context *s, int depth)
290 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
293 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
297 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
302 ff_v360_init_x86(s, depth);
306 * Save nearest pixel coordinates for remapping.
308 * @param du horizontal relative coordinate
309 * @param dv vertical relative coordinate
310 * @param r_tmp calculated 4x4 window
311 * @param u u remap data
312 * @param v v remap data
313 * @param ker ker remap data
315 static void nearest_kernel(float du, float dv, const XYRemap *r_tmp,
316 uint16_t *u, uint16_t *v, int16_t *ker)
318 const int i = roundf(dv) + 1;
319 const int j = roundf(du) + 1;
321 u[0] = r_tmp->u[i][j];
322 v[0] = r_tmp->v[i][j];
326 * Calculate kernel for bilinear interpolation.
328 * @param du horizontal relative coordinate
329 * @param dv vertical relative coordinate
330 * @param r_tmp calculated 4x4 window
331 * @param u u remap data
332 * @param v v remap data
333 * @param ker ker remap data
335 static void bilinear_kernel(float du, float dv, const XYRemap *r_tmp,
336 uint16_t *u, uint16_t *v, int16_t *ker)
340 for (i = 0; i < 2; i++) {
341 for (j = 0; j < 2; j++) {
342 u[i * 2 + j] = r_tmp->u[i + 1][j + 1];
343 v[i * 2 + j] = r_tmp->v[i + 1][j + 1];
347 ker[0] = (1.f - du) * (1.f - dv) * 16384;
348 ker[1] = du * (1.f - dv) * 16384;
349 ker[2] = (1.f - du) * dv * 16384;
350 ker[3] = du * dv * 16384;
354 * Calculate 1-dimensional cubic coefficients.
356 * @param t relative coordinate
357 * @param coeffs coefficients
359 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
361 const float tt = t * t;
362 const float ttt = t * t * t;
364 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
365 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
366 coeffs[2] = t + tt / 2.f - ttt / 2.f;
367 coeffs[3] = - t / 6.f + ttt / 6.f;
371 * Calculate kernel for bicubic interpolation.
373 * @param du horizontal relative coordinate
374 * @param dv vertical relative coordinate
375 * @param r_tmp calculated 4x4 window
376 * @param u u remap data
377 * @param v v remap data
378 * @param ker ker remap data
380 static void bicubic_kernel(float du, float dv, const XYRemap *r_tmp,
381 uint16_t *u, uint16_t *v, int16_t *ker)
387 calculate_bicubic_coeffs(du, du_coeffs);
388 calculate_bicubic_coeffs(dv, dv_coeffs);
390 for (i = 0; i < 4; i++) {
391 for (j = 0; j < 4; j++) {
392 u[i * 4 + j] = r_tmp->u[i][j];
393 v[i * 4 + j] = r_tmp->v[i][j];
394 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
400 * Calculate 1-dimensional lanczos coefficients.
402 * @param t relative coordinate
403 * @param coeffs coefficients
405 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
410 for (i = 0; i < 4; i++) {
411 const float x = M_PI * (t - i + 1);
415 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
420 for (i = 0; i < 4; i++) {
426 * Calculate kernel for lanczos interpolation.
428 * @param du horizontal relative coordinate
429 * @param dv vertical relative coordinate
430 * @param r_tmp calculated 4x4 window
431 * @param u u remap data
432 * @param v v remap data
433 * @param ker ker remap data
435 static void lanczos_kernel(float du, float dv, const XYRemap *r_tmp,
436 uint16_t *u, uint16_t *v, int16_t *ker)
442 calculate_lanczos_coeffs(du, du_coeffs);
443 calculate_lanczos_coeffs(dv, dv_coeffs);
445 for (i = 0; i < 4; i++) {
446 for (j = 0; j < 4; j++) {
447 u[i * 4 + j] = r_tmp->u[i][j];
448 v[i * 4 + j] = r_tmp->v[i][j];
449 ker[i * 4 + j] = du_coeffs[j] * dv_coeffs[i] * 16384;
455 * Modulo operation with only positive remainders.
460 * @return positive remainder of (a / b)
462 static inline int mod(int a, int b)
464 const int res = a % b;
473 * Convert char to corresponding direction.
474 * Used for cubemap options.
476 static int get_direction(char c)
497 * Convert char to corresponding rotation angle.
498 * Used for cubemap options.
500 static int get_rotation(char c)
517 * Convert char to corresponding rotation order.
519 static int get_rorder(char c)
537 * Prepare data for processing cubemap input format.
539 * @param ctx filter context
543 static int prepare_cube_in(AVFilterContext *ctx)
545 V360Context *s = ctx->priv;
547 for (int face = 0; face < NB_FACES; face++) {
548 const char c = s->in_forder[face];
552 av_log(ctx, AV_LOG_ERROR,
553 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
554 return AVERROR(EINVAL);
557 direction = get_direction(c);
558 if (direction == -1) {
559 av_log(ctx, AV_LOG_ERROR,
560 "Incorrect direction symbol '%c' in in_forder option.\n", c);
561 return AVERROR(EINVAL);
564 s->in_cubemap_face_order[direction] = face;
567 for (int face = 0; face < NB_FACES; face++) {
568 const char c = s->in_frot[face];
572 av_log(ctx, AV_LOG_ERROR,
573 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
574 return AVERROR(EINVAL);
577 rotation = get_rotation(c);
578 if (rotation == -1) {
579 av_log(ctx, AV_LOG_ERROR,
580 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
581 return AVERROR(EINVAL);
584 s->in_cubemap_face_rotation[face] = rotation;
591 * Prepare data for processing cubemap output format.
593 * @param ctx filter context
597 static int prepare_cube_out(AVFilterContext *ctx)
599 V360Context *s = ctx->priv;
601 for (int face = 0; face < NB_FACES; face++) {
602 const char c = s->out_forder[face];
606 av_log(ctx, AV_LOG_ERROR,
607 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
608 return AVERROR(EINVAL);
611 direction = get_direction(c);
612 if (direction == -1) {
613 av_log(ctx, AV_LOG_ERROR,
614 "Incorrect direction symbol '%c' in out_forder option.\n", c);
615 return AVERROR(EINVAL);
618 s->out_cubemap_direction_order[face] = direction;
621 for (int face = 0; face < NB_FACES; face++) {
622 const char c = s->out_frot[face];
626 av_log(ctx, AV_LOG_ERROR,
627 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
628 return AVERROR(EINVAL);
631 rotation = get_rotation(c);
632 if (rotation == -1) {
633 av_log(ctx, AV_LOG_ERROR,
634 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
635 return AVERROR(EINVAL);
638 s->out_cubemap_face_rotation[face] = rotation;
644 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
670 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
701 static void normalize_vector(float *vec)
703 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
711 * Calculate 3D coordinates on sphere for corresponding cubemap position.
712 * Common operation for every cubemap.
714 * @param s filter context
715 * @param uf horizontal cubemap coordinate [0, 1)
716 * @param vf vertical cubemap coordinate [0, 1)
717 * @param face face of cubemap
718 * @param vec coordinates on sphere
720 static void cube_to_xyz(const V360Context *s,
721 float uf, float vf, int face,
724 const int direction = s->out_cubemap_direction_order[face];
727 uf /= (1.f - s->out_pad);
728 vf /= (1.f - s->out_pad);
730 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
769 normalize_vector(vec);
773 * Calculate cubemap position for corresponding 3D coordinates on sphere.
774 * Common operation for every cubemap.
776 * @param s filter context
777 * @param vec coordinated on sphere
778 * @param uf horizontal cubemap coordinate [0, 1)
779 * @param vf vertical cubemap coordinate [0, 1)
780 * @param direction direction of view
782 static void xyz_to_cube(const V360Context *s,
784 float *uf, float *vf, int *direction)
786 const float phi = atan2f(vec[0], -vec[2]);
787 const float theta = asinf(-vec[1]);
788 float phi_norm, theta_threshold;
791 if (phi >= -M_PI_4 && phi < M_PI_4) {
794 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
796 phi_norm = phi + M_PI_2;
797 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
799 phi_norm = phi - M_PI_2;
802 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
805 theta_threshold = atanf(cosf(phi_norm));
806 if (theta > theta_threshold) {
808 } else if (theta < -theta_threshold) {
812 switch (*direction) {
814 *uf = vec[2] / vec[0];
815 *vf = -vec[1] / vec[0];
818 *uf = vec[2] / vec[0];
819 *vf = vec[1] / vec[0];
822 *uf = vec[0] / vec[1];
823 *vf = -vec[2] / vec[1];
826 *uf = -vec[0] / vec[1];
827 *vf = -vec[2] / vec[1];
830 *uf = -vec[0] / vec[2];
831 *vf = vec[1] / vec[2];
834 *uf = -vec[0] / vec[2];
835 *vf = -vec[1] / vec[2];
841 face = s->in_cubemap_face_order[*direction];
842 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
844 (*uf) *= s->input_mirror_modifier[0];
845 (*vf) *= s->input_mirror_modifier[1];
849 * Find position on another cube face in case of overflow/underflow.
850 * Used for calculation of interpolation window.
852 * @param s filter context
853 * @param uf horizontal cubemap coordinate
854 * @param vf vertical cubemap coordinate
855 * @param direction direction of view
856 * @param new_uf new horizontal cubemap coordinate
857 * @param new_vf new vertical cubemap coordinate
858 * @param face face position on cubemap
860 static void process_cube_coordinates(const V360Context *s,
861 float uf, float vf, int direction,
862 float *new_uf, float *new_vf, int *face)
865 * Cubemap orientation
872 * +-------+-------+-------+-------+ ^ e |
874 * | left | front | right | back | | g |
875 * +-------+-------+-------+-------+ v h v
881 *face = s->in_cubemap_face_order[direction];
882 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
884 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
885 // There are no pixels to use in this case
888 } else if (uf < -1.f) {
924 } else if (uf >= 1.f) {
960 } else if (vf < -1.f) {
996 } else if (vf >= 1.f) {
1038 *face = s->in_cubemap_face_order[direction];
1039 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1043 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1045 * @param s filter context
1046 * @param i horizontal position on frame [0, height)
1047 * @param j vertical position on frame [0, width)
1048 * @param width frame width
1049 * @param height frame height
1050 * @param vec coordinates on sphere
1052 static void cube3x2_to_xyz(const V360Context *s,
1053 int i, int j, int width, int height,
1056 const float ew = width / 3.f;
1057 const float eh = height / 2.f;
1059 const int u_face = floorf(i / ew);
1060 const int v_face = floorf(j / eh);
1061 const int face = u_face + 3 * v_face;
1063 const int u_shift = ceilf(ew * u_face);
1064 const int v_shift = ceilf(eh * v_face);
1065 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1066 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1068 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1069 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1071 cube_to_xyz(s, uf, vf, face, vec);
1075 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1077 * @param s filter context
1078 * @param vec coordinates on sphere
1079 * @param width frame width
1080 * @param height frame height
1081 * @param us horizontal coordinates for interpolation window
1082 * @param vs vertical coordinates for interpolation window
1083 * @param du horizontal relative coordinate
1084 * @param dv vertical relative coordinate
1086 static void xyz_to_cube3x2(const V360Context *s,
1087 const float *vec, int width, int height,
1088 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1090 const float ew = width / 3.f;
1091 const float eh = height / 2.f;
1096 int direction, face;
1099 xyz_to_cube(s, vec, &uf, &vf, &direction);
1101 uf *= (1.f - s->in_pad);
1102 vf *= (1.f - s->in_pad);
1104 face = s->in_cubemap_face_order[direction];
1107 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1108 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1110 uf = 0.5f * ewi * (uf + 1.f);
1111 vf = 0.5f * ehi * (vf + 1.f);
1119 for (i = -1; i < 3; i++) {
1120 for (j = -1; j < 3; j++) {
1121 int new_ui = ui + j;
1122 int new_vi = vi + i;
1123 int u_shift, v_shift;
1124 int new_ewi, new_ehi;
1126 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1127 face = s->in_cubemap_face_order[direction];
1131 u_shift = ceilf(ew * u_face);
1132 v_shift = ceilf(eh * v_face);
1134 uf = 2.f * new_ui / ewi - 1.f;
1135 vf = 2.f * new_vi / ehi - 1.f;
1137 uf /= (1.f - s->in_pad);
1138 vf /= (1.f - s->in_pad);
1140 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1142 uf *= (1.f - s->in_pad);
1143 vf *= (1.f - s->in_pad);
1147 u_shift = ceilf(ew * u_face);
1148 v_shift = ceilf(eh * v_face);
1149 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1150 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1152 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1153 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1156 us[i + 1][j + 1] = u_shift + new_ui;
1157 vs[i + 1][j + 1] = v_shift + new_vi;
1163 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1165 * @param s filter context
1166 * @param i horizontal position on frame [0, height)
1167 * @param j vertical position on frame [0, width)
1168 * @param width frame width
1169 * @param height frame height
1170 * @param vec coordinates on sphere
1172 static void cube1x6_to_xyz(const V360Context *s,
1173 int i, int j, int width, int height,
1176 const float ew = width;
1177 const float eh = height / 6.f;
1179 const int face = floorf(j / eh);
1181 const int v_shift = ceilf(eh * face);
1182 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1184 const float uf = 2.f * i / ew - 1.f;
1185 const float vf = 2.f * (j - v_shift) / ehi - 1.f;
1187 cube_to_xyz(s, uf, vf, face, vec);
1191 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1193 * @param s filter context
1194 * @param i horizontal position on frame [0, height)
1195 * @param j vertical position on frame [0, width)
1196 * @param width frame width
1197 * @param height frame height
1198 * @param vec coordinates on sphere
1200 static void cube6x1_to_xyz(const V360Context *s,
1201 int i, int j, int width, int height,
1204 const float ew = width / 6.f;
1205 const float eh = height;
1207 const int face = floorf(i / ew);
1209 const int u_shift = ceilf(ew * face);
1210 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1212 const float uf = 2.f * (i - u_shift) / ewi - 1.f;
1213 const float vf = 2.f * j / eh - 1.f;
1215 cube_to_xyz(s, uf, vf, face, vec);
1219 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1221 * @param s filter context
1222 * @param vec coordinates on sphere
1223 * @param width frame width
1224 * @param height frame height
1225 * @param us horizontal coordinates for interpolation window
1226 * @param vs vertical coordinates for interpolation window
1227 * @param du horizontal relative coordinate
1228 * @param dv vertical relative coordinate
1230 static void xyz_to_cube1x6(const V360Context *s,
1231 const float *vec, int width, int height,
1232 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1234 const float eh = height / 6.f;
1235 const int ewi = width;
1240 int direction, face;
1242 xyz_to_cube(s, vec, &uf, &vf, &direction);
1244 uf *= (1.f - s->in_pad);
1245 vf *= (1.f - s->in_pad);
1247 face = s->in_cubemap_face_order[direction];
1248 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1250 uf = 0.5f * ewi * (uf + 1.f);
1251 vf = 0.5f * ehi * (vf + 1.f);
1259 for (i = -1; i < 3; i++) {
1260 for (j = -1; j < 3; j++) {
1261 int new_ui = ui + j;
1262 int new_vi = vi + i;
1266 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1267 face = s->in_cubemap_face_order[direction];
1269 v_shift = ceilf(eh * face);
1271 uf = 2.f * new_ui / ewi - 1.f;
1272 vf = 2.f * new_vi / ehi - 1.f;
1274 uf /= (1.f - s->in_pad);
1275 vf /= (1.f - s->in_pad);
1277 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1279 uf *= (1.f - s->in_pad);
1280 vf *= (1.f - s->in_pad);
1282 v_shift = ceilf(eh * face);
1283 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1285 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1286 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1289 us[i + 1][j + 1] = new_ui;
1290 vs[i + 1][j + 1] = v_shift + new_vi;
1296 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1298 * @param s filter context
1299 * @param vec coordinates on sphere
1300 * @param width frame width
1301 * @param height frame height
1302 * @param us horizontal coordinates for interpolation window
1303 * @param vs vertical coordinates for interpolation window
1304 * @param du horizontal relative coordinate
1305 * @param dv vertical relative coordinate
1307 static void xyz_to_cube6x1(const V360Context *s,
1308 const float *vec, int width, int height,
1309 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1311 const float ew = width / 6.f;
1312 const int ehi = height;
1317 int direction, face;
1319 xyz_to_cube(s, vec, &uf, &vf, &direction);
1321 uf *= (1.f - s->in_pad);
1322 vf *= (1.f - s->in_pad);
1324 face = s->in_cubemap_face_order[direction];
1325 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1327 uf = 0.5f * ewi * (uf + 1.f);
1328 vf = 0.5f * ehi * (vf + 1.f);
1336 for (i = -1; i < 3; i++) {
1337 for (j = -1; j < 3; j++) {
1338 int new_ui = ui + j;
1339 int new_vi = vi + i;
1343 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1344 face = s->in_cubemap_face_order[direction];
1346 u_shift = ceilf(ew * face);
1348 uf = 2.f * new_ui / ewi - 1.f;
1349 vf = 2.f * new_vi / ehi - 1.f;
1351 uf /= (1.f - s->in_pad);
1352 vf /= (1.f - s->in_pad);
1354 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1356 uf *= (1.f - s->in_pad);
1357 vf *= (1.f - s->in_pad);
1359 u_shift = ceilf(ew * face);
1360 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1362 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1363 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1366 us[i + 1][j + 1] = u_shift + new_ui;
1367 vs[i + 1][j + 1] = new_vi;
1373 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1375 * @param s filter context
1376 * @param i horizontal position on frame [0, height)
1377 * @param j vertical position on frame [0, width)
1378 * @param width frame width
1379 * @param height frame height
1380 * @param vec coordinates on sphere
1382 static void equirect_to_xyz(const V360Context *s,
1383 int i, int j, int width, int height,
1386 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1387 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1389 const float sin_phi = sinf(phi);
1390 const float cos_phi = cosf(phi);
1391 const float sin_theta = sinf(theta);
1392 const float cos_theta = cosf(theta);
1394 vec[0] = cos_theta * sin_phi;
1395 vec[1] = -sin_theta;
1396 vec[2] = -cos_theta * cos_phi;
1400 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1402 * @param s filter context
1403 * @param i horizontal position on frame [0, height)
1404 * @param j vertical position on frame [0, width)
1405 * @param width frame width
1406 * @param height frame height
1407 * @param vec coordinates on sphere
1409 static void stereographic_to_xyz(const V360Context *s,
1410 int i, int j, int width, int height,
1413 const float x = ((2.f * i) / width - 1.f) * (s->h_fov / 180.f) * M_PI;
1414 const float y = ((2.f * j) / height - 1.f) * (s->v_fov / 90.f) * M_PI_2;
1415 const float xy = x * x + y * y;
1417 vec[0] = 2.f * x / (1.f + xy);
1418 vec[1] = (-1.f + xy) / (1.f + xy);
1419 vec[2] = 2.f * y / (1.f + xy);
1421 normalize_vector(vec);
1425 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1427 * @param s filter context
1428 * @param vec coordinates on sphere
1429 * @param width frame width
1430 * @param height frame height
1431 * @param us horizontal coordinates for interpolation window
1432 * @param vs vertical coordinates for interpolation window
1433 * @param du horizontal relative coordinate
1434 * @param dv vertical relative coordinate
1436 static void xyz_to_stereographic(const V360Context *s,
1437 const float *vec, int width, int height,
1438 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1440 const float x = av_clipf(vec[0] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[0];
1441 const float y = av_clipf(vec[2] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[1];
1446 uf = (x + 1.f) * width / 2.f;
1447 vf = (y + 1.f) * height / 2.f;
1454 for (i = -1; i < 3; i++) {
1455 for (j = -1; j < 3; j++) {
1456 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1457 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1463 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1465 * @param s filter context
1466 * @param vec coordinates on sphere
1467 * @param width frame width
1468 * @param height frame height
1469 * @param us horizontal coordinates for interpolation window
1470 * @param vs vertical coordinates for interpolation window
1471 * @param du horizontal relative coordinate
1472 * @param dv vertical relative coordinate
1474 static void xyz_to_equirect(const V360Context *s,
1475 const float *vec, int width, int height,
1476 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1478 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1479 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1484 uf = (phi / M_PI + 1.f) * width / 2.f;
1485 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1492 for (i = -1; i < 3; i++) {
1493 for (j = -1; j < 3; j++) {
1494 us[i + 1][j + 1] = mod(ui + j, width);
1495 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1501 * Prepare data for processing equi-angular cubemap input format.
1503 * @param ctx filter context
1505 * @return error code
1507 static int prepare_eac_in(AVFilterContext *ctx)
1509 V360Context *s = ctx->priv;
1511 if (s->ih_flip && s->iv_flip) {
1512 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
1513 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
1514 s->in_cubemap_face_order[UP] = TOP_LEFT;
1515 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
1516 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1517 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1518 } else if (s->ih_flip) {
1519 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
1520 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
1521 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
1522 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
1523 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1524 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1525 } else if (s->iv_flip) {
1526 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
1527 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
1528 s->in_cubemap_face_order[UP] = TOP_RIGHT;
1529 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
1530 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1531 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1533 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1534 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1535 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1536 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1537 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1538 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1542 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
1543 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
1544 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
1545 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
1546 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
1547 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
1549 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1550 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1551 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1552 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1553 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1554 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1561 * Prepare data for processing equi-angular cubemap output format.
1563 * @param ctx filter context
1565 * @return error code
1567 static int prepare_eac_out(AVFilterContext *ctx)
1569 V360Context *s = ctx->priv;
1571 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1572 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1573 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1574 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1575 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1576 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1578 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1579 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1580 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1581 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1582 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1583 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1589 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1591 * @param s filter context
1592 * @param i horizontal position on frame [0, height)
1593 * @param j vertical position on frame [0, width)
1594 * @param width frame width
1595 * @param height frame height
1596 * @param vec coordinates on sphere
1598 static void eac_to_xyz(const V360Context *s,
1599 int i, int j, int width, int height,
1602 const float pixel_pad = 2;
1603 const float u_pad = pixel_pad / width;
1604 const float v_pad = pixel_pad / height;
1606 int u_face, v_face, face;
1608 float l_x, l_y, l_z;
1610 float uf = (float)i / width;
1611 float vf = (float)j / height;
1613 // EAC has 2-pixel padding on faces except between faces on the same row
1614 // Padding pixels seems not to be stretched with tangent as regular pixels
1615 // Formulas below approximate original padding as close as I could get experimentally
1617 // Horizontal padding
1618 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1622 } else if (uf >= 3.f) {
1626 u_face = floorf(uf);
1627 uf = fmodf(uf, 1.f) - 0.5f;
1631 v_face = floorf(vf * 2.f);
1632 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1634 if (uf >= -0.5f && uf < 0.5f) {
1635 uf = tanf(M_PI_2 * uf);
1639 if (vf >= -0.5f && vf < 0.5f) {
1640 vf = tanf(M_PI_2 * vf);
1645 face = u_face + 3 * v_face;
1686 normalize_vector(vec);
1690 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
1692 * @param s filter context
1693 * @param vec coordinates on sphere
1694 * @param width frame width
1695 * @param height frame height
1696 * @param us horizontal coordinates for interpolation window
1697 * @param vs vertical coordinates for interpolation window
1698 * @param du horizontal relative coordinate
1699 * @param dv vertical relative coordinate
1701 static void xyz_to_eac(const V360Context *s,
1702 const float *vec, int width, int height,
1703 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1705 const float pixel_pad = 2;
1706 const float u_pad = pixel_pad / width;
1707 const float v_pad = pixel_pad / height;
1712 int direction, face;
1715 xyz_to_cube(s, vec, &uf, &vf, &direction);
1717 face = s->in_cubemap_face_order[direction];
1721 uf = M_2_PI * atanf(uf) + 0.5f;
1722 vf = M_2_PI * atanf(vf) + 0.5f;
1724 // These formulas are inversed from eac_to_xyz ones
1725 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
1726 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
1737 for (i = -1; i < 3; i++) {
1738 for (j = -1; j < 3; j++) {
1739 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1740 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1746 * Prepare data for processing flat output format.
1748 * @param ctx filter context
1750 * @return error code
1752 static int prepare_flat_out(AVFilterContext *ctx)
1754 V360Context *s = ctx->priv;
1756 const float h_angle = 0.5f * s->h_fov * M_PI / 180.f;
1757 const float v_angle = 0.5f * s->v_fov * M_PI / 180.f;
1759 const float sin_phi = sinf(h_angle);
1760 const float cos_phi = cosf(h_angle);
1761 const float sin_theta = sinf(v_angle);
1762 const float cos_theta = cosf(v_angle);
1764 s->flat_range[0] = cos_theta * sin_phi;
1765 s->flat_range[1] = sin_theta;
1766 s->flat_range[2] = -cos_theta * cos_phi;
1772 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
1774 * @param s filter context
1775 * @param i horizontal position on frame [0, height)
1776 * @param j vertical position on frame [0, width)
1777 * @param width frame width
1778 * @param height frame height
1779 * @param vec coordinates on sphere
1781 static void flat_to_xyz(const V360Context *s,
1782 int i, int j, int width, int height,
1785 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
1786 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
1787 const float l_z = s->flat_range[2];
1793 normalize_vector(vec);
1797 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
1799 * @param s filter context
1800 * @param vec coordinates on sphere
1801 * @param width frame width
1802 * @param height frame height
1803 * @param us horizontal coordinates for interpolation window
1804 * @param vs vertical coordinates for interpolation window
1805 * @param du horizontal relative coordinate
1806 * @param dv vertical relative coordinate
1808 static void xyz_to_dfisheye(const V360Context *s,
1809 const float *vec, int width, int height,
1810 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1812 const float scale = 1.f - s->in_pad;
1814 const float ew = width / 2.f;
1815 const float eh = height;
1817 const float phi = atan2f(-vec[1], -vec[0]) * s->input_mirror_modifier[0];
1818 const float theta = acosf(fabsf(vec[2])) / M_PI * s->input_mirror_modifier[1];
1820 float uf = (theta * cosf(phi) * scale + 0.5f) * ew;
1821 float vf = (theta * sinf(phi) * scale + 0.5f) * eh;
1830 u_shift = ceilf(ew);
1840 for (i = -1; i < 3; i++) {
1841 for (j = -1; j < 3; j++) {
1842 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
1843 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
1849 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
1851 * @param s filter context
1852 * @param i horizontal position on frame [0, height)
1853 * @param j vertical position on frame [0, width)
1854 * @param width frame width
1855 * @param height frame height
1856 * @param vec coordinates on sphere
1858 static void barrel_to_xyz(const V360Context *s,
1859 int i, int j, int width, int height,
1862 const float scale = 0.99f;
1863 float l_x, l_y, l_z;
1865 if (i < 4 * width / 5) {
1866 const float theta_range = M_PI / 4.f;
1868 const int ew = 4 * width / 5;
1869 const int eh = height;
1871 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
1872 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
1874 const float sin_phi = sinf(phi);
1875 const float cos_phi = cosf(phi);
1876 const float sin_theta = sinf(theta);
1877 const float cos_theta = cosf(theta);
1879 l_x = cos_theta * sin_phi;
1881 l_z = -cos_theta * cos_phi;
1883 const int ew = width / 5;
1884 const int eh = height / 2;
1889 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1890 vf = 2.f * (j ) / eh - 1.f;
1899 uf = 2.f * (i - 4 * ew) / ew - 1.f;
1900 vf = 2.f * (j - eh) / eh - 1.f;
1915 normalize_vector(vec);
1919 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
1921 * @param s filter context
1922 * @param vec coordinates on sphere
1923 * @param width frame width
1924 * @param height frame height
1925 * @param us horizontal coordinates for interpolation window
1926 * @param vs vertical coordinates for interpolation window
1927 * @param du horizontal relative coordinate
1928 * @param dv vertical relative coordinate
1930 static void xyz_to_barrel(const V360Context *s,
1931 const float *vec, int width, int height,
1932 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1934 const float scale = 0.99f;
1936 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1937 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1938 const float theta_range = M_PI / 4.f;
1941 int u_shift, v_shift;
1946 if (theta > -theta_range && theta < theta_range) {
1950 u_shift = s->ih_flip ? width / 5 : 0;
1953 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
1954 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
1959 u_shift = s->ih_flip ? 0 : 4 * ew;
1961 if (theta < 0.f) { // UP
1962 uf = vec[0] / vec[1];
1963 vf = -vec[2] / vec[1];
1966 uf = -vec[0] / vec[1];
1967 vf = -vec[2] / vec[1];
1971 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
1972 vf *= s->input_mirror_modifier[1];
1974 uf = 0.5f * ew * (uf * scale + 1.f);
1975 vf = 0.5f * eh * (vf * scale + 1.f);
1984 for (i = -1; i < 3; i++) {
1985 for (j = -1; j < 3; j++) {
1986 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
1987 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
1992 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
1994 for (int i = 0; i < 3; i++) {
1995 for (int j = 0; j < 3; j++) {
1998 for (int k = 0; k < 3; k++)
1999 sum += a[i][k] * b[k][j];
2007 * Calculate rotation matrix for yaw/pitch/roll angles.
2009 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2010 float rot_mat[3][3],
2011 const int rotation_order[3])
2013 const float yaw_rad = yaw * M_PI / 180.f;
2014 const float pitch_rad = pitch * M_PI / 180.f;
2015 const float roll_rad = roll * M_PI / 180.f;
2017 const float sin_yaw = sinf(-yaw_rad);
2018 const float cos_yaw = cosf(-yaw_rad);
2019 const float sin_pitch = sinf(pitch_rad);
2020 const float cos_pitch = cosf(pitch_rad);
2021 const float sin_roll = sinf(roll_rad);
2022 const float cos_roll = cosf(roll_rad);
2027 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2028 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2029 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2031 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2032 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2033 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2035 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2036 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2037 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2039 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2040 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2044 * Rotate vector with given rotation matrix.
2046 * @param rot_mat rotation matrix
2049 static inline void rotate(const float rot_mat[3][3],
2052 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2053 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2054 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2061 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2064 modifier[0] = h_flip ? -1.f : 1.f;
2065 modifier[1] = v_flip ? -1.f : 1.f;
2066 modifier[2] = d_flip ? -1.f : 1.f;
2069 static inline void mirror(const float *modifier, float *vec)
2071 vec[0] *= modifier[0];
2072 vec[1] *= modifier[1];
2073 vec[2] *= modifier[2];
2076 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2078 s->u[p] = av_calloc(s->uv_linesize[p] * s->planeheight[p], sizeof_uv);
2079 s->v[p] = av_calloc(s->uv_linesize[p] * s->planeheight[p], sizeof_uv);
2080 if (!s->u[p] || !s->v[p])
2081 return AVERROR(ENOMEM);
2083 s->ker[p] = av_calloc(s->uv_linesize[p] * s->planeheight[p], sizeof_ker);
2085 return AVERROR(ENOMEM);
2091 static int config_output(AVFilterLink *outlink)
2093 AVFilterContext *ctx = outlink->src;
2094 AVFilterLink *inlink = ctx->inputs[0];
2095 V360Context *s = ctx->priv;
2096 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2097 const int depth = desc->comp[0].depth;
2104 float output_mirror_modifier[3];
2105 void (*in_transform)(const V360Context *s,
2106 const float *vec, int width, int height,
2107 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv);
2108 void (*out_transform)(const V360Context *s,
2109 int i, int j, int width, int height,
2111 void (*calculate_kernel)(float du, float dv, const XYRemap *r_tmp,
2112 uint16_t *u, uint16_t *v, int16_t *ker);
2113 float rot_mat[3][3];
2115 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2116 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2118 switch (s->interp) {
2120 calculate_kernel = nearest_kernel;
2121 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2123 sizeof_uv = sizeof(uint16_t) * elements;
2127 calculate_kernel = bilinear_kernel;
2128 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2130 sizeof_uv = sizeof(uint16_t) * elements;
2131 sizeof_ker = sizeof(uint16_t) * elements;
2134 calculate_kernel = bicubic_kernel;
2135 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2137 sizeof_uv = sizeof(uint16_t) * elements;
2138 sizeof_ker = sizeof(uint16_t) * elements;
2141 calculate_kernel = lanczos_kernel;
2142 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2144 sizeof_uv = sizeof(uint16_t) * elements;
2145 sizeof_ker = sizeof(uint16_t) * elements;
2151 ff_v360_init(s, depth);
2153 for (int order = 0; order < NB_RORDERS; order++) {
2154 const char c = s->rorder[order];
2158 av_log(ctx, AV_LOG_ERROR,
2159 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2160 return AVERROR(EINVAL);
2163 rorder = get_rorder(c);
2165 av_log(ctx, AV_LOG_ERROR,
2166 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2167 return AVERROR(EINVAL);
2170 s->rotation_order[order] = rorder;
2174 case EQUIRECTANGULAR:
2175 in_transform = xyz_to_equirect;
2181 in_transform = xyz_to_cube3x2;
2182 err = prepare_cube_in(ctx);
2183 wf = inlink->w / 3.f * 4.f;
2187 in_transform = xyz_to_cube1x6;
2188 err = prepare_cube_in(ctx);
2189 wf = inlink->w * 4.f;
2190 hf = inlink->h / 3.f;
2193 in_transform = xyz_to_cube6x1;
2194 err = prepare_cube_in(ctx);
2195 wf = inlink->w / 3.f * 2.f;
2196 hf = inlink->h * 2.f;
2199 in_transform = xyz_to_eac;
2200 err = prepare_eac_in(ctx);
2202 hf = inlink->h / 9.f * 8.f;
2205 av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n");
2206 return AVERROR(EINVAL);
2208 in_transform = xyz_to_dfisheye;
2214 in_transform = xyz_to_barrel;
2216 wf = inlink->w / 5.f * 4.f;
2220 in_transform = xyz_to_stereographic;
2226 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2235 case EQUIRECTANGULAR:
2236 out_transform = equirect_to_xyz;
2242 out_transform = cube3x2_to_xyz;
2243 err = prepare_cube_out(ctx);
2244 w = roundf(wf / 4.f * 3.f);
2248 out_transform = cube1x6_to_xyz;
2249 err = prepare_cube_out(ctx);
2250 w = roundf(wf / 4.f);
2251 h = roundf(hf * 3.f);
2254 out_transform = cube6x1_to_xyz;
2255 err = prepare_cube_out(ctx);
2256 w = roundf(wf / 2.f * 3.f);
2257 h = roundf(hf / 2.f);
2260 out_transform = eac_to_xyz;
2261 err = prepare_eac_out(ctx);
2263 h = roundf(hf / 8.f * 9.f);
2266 out_transform = flat_to_xyz;
2267 err = prepare_flat_out(ctx);
2268 w = roundf(wf * s->flat_range[0] / s->flat_range[1] / 2.f);
2272 av_log(ctx, AV_LOG_ERROR, "Dual fisheye format is not accepted as output.\n");
2273 return AVERROR(EINVAL);
2275 out_transform = barrel_to_xyz;
2277 w = roundf(wf / 4.f * 5.f);
2281 out_transform = stereographic_to_xyz;
2283 w = FFMAX(roundf(wf), roundf(hf));
2287 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2295 // Override resolution with user values if specified
2296 if (s->width > 0 && s->height > 0) {
2299 } else if (s->width > 0 || s->height > 0) {
2300 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2301 return AVERROR(EINVAL);
2303 if (s->out_transpose)
2306 if (s->in_transpose)
2310 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2311 s->planeheight[0] = s->planeheight[3] = h;
2312 s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2313 s->planewidth[0] = s->planewidth[3] = w;
2315 for (int i = 0; i < 4; i++)
2316 s->uv_linesize[i] = FFALIGN(s->planewidth[i], 8);
2321 s->inplaneheight[1] = s->inplaneheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
2322 s->inplaneheight[0] = s->inplaneheight[3] = inlink->h;
2323 s->inplanewidth[1] = s->inplanewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
2324 s->inplanewidth[0] = s->inplanewidth[3] = inlink->w;
2325 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2327 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2328 s->nb_allocated = 1;
2329 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2330 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2331 } else if (desc->log2_chroma_h == desc->log2_chroma_w) {
2332 s->nb_allocated = 2;
2334 s->map[1] = s->map[2] = 1;
2336 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2337 allocate_plane(s, sizeof_uv, sizeof_ker, 1);
2339 s->nb_allocated = 3;
2344 allocate_plane(s, sizeof_uv, sizeof_ker, 0);
2345 allocate_plane(s, sizeof_uv, sizeof_ker, 1);
2346 allocate_plane(s, sizeof_uv, sizeof_ker, 2);
2349 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, rot_mat, s->rotation_order);
2350 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, output_mirror_modifier);
2352 // Calculate remap data
2353 for (p = 0; p < s->nb_allocated; p++) {
2354 const int width = s->planewidth[p];
2355 const int uv_linesize = s->uv_linesize[p];
2356 const int height = s->planeheight[p];
2357 const int in_width = s->inplanewidth[p];
2358 const int in_height = s->inplaneheight[p];
2364 for (i = 0; i < width; i++) {
2365 for (j = 0; j < height; j++) {
2366 uint16_t *u = s->u[p] + (j * uv_linesize + i) * elements;
2367 uint16_t *v = s->v[p] + (j * uv_linesize + i) * elements;
2368 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * elements;
2370 if (s->out_transpose)
2371 out_transform(s, j, i, height, width, vec);
2373 out_transform(s, i, j, width, height, vec);
2374 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2375 rotate(rot_mat, vec);
2376 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2377 normalize_vector(vec);
2378 mirror(output_mirror_modifier, vec);
2379 if (s->in_transpose)
2380 in_transform(s, vec, in_height, in_width, r_tmp.v, r_tmp.u, &du, &dv);
2382 in_transform(s, vec, in_width, in_height, r_tmp.u, r_tmp.v, &du, &dv);
2383 av_assert1(!isnan(du) && !isnan(dv));
2384 calculate_kernel(du, dv, &r_tmp, u, v, ker);
2392 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2394 AVFilterContext *ctx = inlink->dst;
2395 AVFilterLink *outlink = ctx->outputs[0];
2396 V360Context *s = ctx->priv;
2400 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2403 return AVERROR(ENOMEM);
2405 av_frame_copy_props(out, in);
2410 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2413 return ff_filter_frame(outlink, out);
2416 static av_cold void uninit(AVFilterContext *ctx)
2418 V360Context *s = ctx->priv;
2421 for (p = 0; p < s->nb_allocated; p++) {
2424 av_freep(&s->ker[p]);
2428 static const AVFilterPad inputs[] = {
2431 .type = AVMEDIA_TYPE_VIDEO,
2432 .filter_frame = filter_frame,
2437 static const AVFilterPad outputs[] = {
2440 .type = AVMEDIA_TYPE_VIDEO,
2441 .config_props = config_output,
2446 AVFilter ff_vf_v360 = {
2448 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
2449 .priv_size = sizeof(V360Context),
2451 .query_formats = query_formats,
2454 .priv_class = &v360_class,
2455 .flags = AVFILTER_FLAG_SLICE_THREADS,